make sure that when extending the selection in the editor and mixer, the clicked...
[ardour.git] / gtk2_ardour / mixer_ui.cc
1 /*
2     Copyright (C) 2000-2004 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <algorithm>
25 #include <map>
26 #include <sigc++/bind.h>
27
28 #include <boost/foreach.hpp>
29
30 #include <gtkmm/accelmap.h>
31
32 #include "pbd/convert.h"
33 #include "pbd/stacktrace.h"
34 #include "pbd/unwind.h"
35
36 #include <glibmm/threads.h>
37
38 #include <gtkmm2ext/gtk_ui.h>
39 #include <gtkmm2ext/keyboard.h>
40 #include <gtkmm2ext/utils.h>
41 #include <gtkmm2ext/tearoff.h>
42 #include <gtkmm2ext/window_title.h>
43 #include <gtkmm2ext/doi.h>
44
45 #include "ardour/amp.h"
46 #include "ardour/debug.h"
47 #include "ardour/audio_track.h"
48 #include "ardour/midi_track.h"
49 #include "ardour/plugin_manager.h"
50 #include "ardour/route_group.h"
51 #include "ardour/session.h"
52 #include "ardour/vca.h"
53 #include "ardour/vca_manager.h"
54
55 #include "keyboard.h"
56 #include "mixer_ui.h"
57 #include "mixer_strip.h"
58 #include "monitor_section.h"
59 #include "plugin_selector.h"
60 #include "public_editor.h"
61 #include "mouse_cursors.h"
62 #include "ardour_ui.h"
63 #include "prompter.h"
64 #include "utils.h"
65 #include "route_sorter.h"
66 #include "actions.h"
67 #include "gui_thread.h"
68 #include "mixer_group_tabs.h"
69 #include "route_sorter.h"
70 #include "timers.h"
71 #include "ui_config.h"
72 #include "vca_master_strip.h"
73
74 #include "pbd/i18n.h"
75
76 using namespace ARDOUR;
77 using namespace ARDOUR_UI_UTILS;
78 using namespace PBD;
79 using namespace Gtk;
80 using namespace Glib;
81 using namespace Gtkmm2ext;
82 using namespace std;
83
84 using PBD::atoi;
85 using PBD::Unwinder;
86
87 Mixer_UI* Mixer_UI::_instance = 0;
88
89 Mixer_UI*
90 Mixer_UI::instance ()
91 {
92         if (!_instance) {
93                 _instance  = new Mixer_UI;
94         }
95
96         return _instance;
97 }
98
99 Mixer_UI::Mixer_UI ()
100         : Tabbable (_content, _("Mixer"))
101         , no_track_list_redisplay (false)
102         , in_group_row_change (false)
103         , track_menu (0)
104         , _monitor_section (0)
105         , _plugin_selector (0)
106         , _strip_width (UIConfiguration::instance().get_default_narrow_ms() ? Narrow : Wide)
107         , ignore_reorder (false)
108         , _in_group_rebuild_or_clear (false)
109         , _route_deletion_in_progress (false)
110         , _maximised (false)
111         , _show_mixer_list (true)
112         , myactions (X_("mixer"))
113 {
114         register_actions ();
115         load_bindings ();
116         _content.set_data ("ardour-bindings", bindings);
117
118         PresentationInfo::Change.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::presentation_info_changed, this, _1), gui_context());
119
120         scroller.set_can_default (true);
121         // set_default (scroller);
122
123         scroller_base.set_flags (Gtk::CAN_FOCUS);
124         scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
125         scroller_base.set_name ("MixerWindow");
126         scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
127
128         /* set up drag-n-drop */
129         vector<TargetEntry> target_table;
130         target_table.push_back (TargetEntry ("PluginFavoritePtr"));
131         scroller_base.drag_dest_set (target_table);
132         scroller_base.signal_drag_data_received().connect (sigc::mem_fun(*this, &Mixer_UI::scroller_drag_data_received));
133
134         // add as last item of strip packer
135         strip_packer.pack_end (scroller_base, true, true);
136
137         _group_tabs = new MixerGroupTabs (this);
138         VBox* b = manage (new VBox);
139         b->set_spacing (0);
140         b->set_border_width (0);
141         b->pack_start (*_group_tabs, PACK_SHRINK);
142         b->pack_start (strip_packer);
143         b->show_all ();
144         b->signal_scroll_event().connect (sigc::mem_fun (*this, &Mixer_UI::on_scroll_event), false);
145
146         scroller.add (*b);
147         scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
148
149         setup_track_display ();
150
151         group_model = ListStore::create (group_columns);
152         group_display.set_model (group_model);
153         group_display.append_column (_("Show"), group_columns.visible);
154         group_display.append_column (_("Group"), group_columns.text);
155         group_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
156         group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
157         group_display.get_column (0)->set_expand(false);
158         group_display.get_column (1)->set_expand(true);
159         group_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
160         group_display.set_name ("EditGroupList");
161         group_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
162         group_display.set_reorderable (true);
163         group_display.set_headers_visible (true);
164         group_display.set_rules_hint (true);
165         group_display.set_can_focus(false);
166
167         /* name is directly editable */
168
169         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
170         name_cell->property_editable() = true;
171         name_cell->signal_edited().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_name_edit));
172
173         /* use checkbox for the active column */
174
175         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*>(group_display.get_column_cell_renderer (0));
176         active_cell->property_activatable() = true;
177         active_cell->property_radio() = false;
178
179         group_model->signal_row_changed().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_change));
180         /* We use this to notice drag-and-drop reorders of the group list */
181         group_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_deleted));
182         group_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::group_display_button_press), false);
183
184         group_display_scroller.add (group_display);
185         group_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
186
187         HBox* route_group_display_button_box = manage (new HBox());
188
189         Button* route_group_add_button = manage (new Button ());
190         Button* route_group_remove_button = manage (new Button ());
191
192         Widget* w;
193
194         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
195         w->show();
196         route_group_add_button->add (*w);
197
198         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
199         w->show();
200         route_group_remove_button->add (*w);
201
202         route_group_display_button_box->set_homogeneous (true);
203
204         route_group_add_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_route_group));
205         route_group_remove_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::remove_selected_route_group));
206
207         route_group_display_button_box->add (*route_group_add_button);
208         route_group_display_button_box->add (*route_group_remove_button);
209
210         group_display_vbox.pack_start (group_display_scroller, true, true);
211         group_display_vbox.pack_start (*route_group_display_button_box, false, false);
212
213         group_display_frame.set_name ("BaseFrame");
214         group_display_frame.set_shadow_type (Gtk::SHADOW_IN);
215         group_display_frame.add (group_display_vbox);
216
217
218         list<TargetEntry> target_list;
219         target_list.push_back (TargetEntry ("PluginPresetPtr"));
220
221         favorite_plugins_model = PluginTreeStore::create (favorite_plugins_columns);
222         favorite_plugins_display.set_model (favorite_plugins_model);
223         favorite_plugins_display.append_column (_("Favorite Plugins"), favorite_plugins_columns.name);
224         favorite_plugins_display.set_name ("EditGroupList");
225         favorite_plugins_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
226         favorite_plugins_display.set_reorderable (false);
227         favorite_plugins_display.set_headers_visible (true);
228         favorite_plugins_display.set_rules_hint (true);
229         favorite_plugins_display.set_can_focus (false);
230         favorite_plugins_display.set_tooltip_column (0);
231         favorite_plugins_display.add_object_drag (favorite_plugins_columns.plugin.index(), "PluginFavoritePtr");
232         favorite_plugins_display.set_drag_column (favorite_plugins_columns.name.index());
233         favorite_plugins_display.add_drop_targets (target_list);
234         favorite_plugins_display.signal_row_activated().connect (sigc::mem_fun (*this, &Mixer_UI::plugin_row_activated));
235         favorite_plugins_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::plugin_row_button_press), false);
236         favorite_plugins_display.signal_drop.connect (sigc::mem_fun (*this, &Mixer_UI::plugin_drop));
237         favorite_plugins_display.signal_row_expanded().connect (sigc::mem_fun (*this, &Mixer_UI::save_favorite_ui_state));
238         favorite_plugins_display.signal_row_collapsed().connect (sigc::mem_fun (*this, &Mixer_UI::save_favorite_ui_state));
239         favorite_plugins_model->signal_row_has_child_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::sync_treeview_favorite_ui_state));
240
241         favorite_plugins_scroller.add (favorite_plugins_display);
242         favorite_plugins_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
243
244         favorite_plugins_frame.set_name ("BaseFrame");
245         favorite_plugins_frame.set_shadow_type (Gtk::SHADOW_IN);
246         favorite_plugins_frame.add (favorite_plugins_scroller);
247
248         rhs_pane1.add (favorite_plugins_frame);
249         rhs_pane1.add (track_display_frame);
250
251         rhs_pane2.add (rhs_pane1);
252         rhs_pane2.add (group_display_frame);
253
254         list_vpacker.pack_start (rhs_pane2, true, true);
255
256         vca_label_bar.set_size_request (-1, 16 + 1); /* must match height in GroupTabs::set_size_request()  + 1 border px*/
257         vca_vpacker.pack_start (vca_label_bar, false, false);
258
259         vca_scroller_base.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
260         vca_scroller_base.set_name (X_("MixerWindow"));
261         vca_scroller_base.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::masters_scroller_button_release), false);
262         vca_hpacker.pack_end (vca_scroller_base, true, true);
263
264         vca_scroller.add (vca_hpacker);
265         vca_scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
266         vca_scroller.signal_button_release_event().connect (sigc::mem_fun(*this, &Mixer_UI::strip_scroller_button_release));
267
268         vca_vpacker.pack_start (vca_scroller, true, true);
269
270         inner_pane.add (scroller);
271         inner_pane.add (vca_vpacker);
272
273         global_hpacker.pack_start (inner_pane, true, true);
274         global_hpacker.pack_start (out_packer, false, false);
275
276         list_hpane.set_check_divider_position (true);
277         list_hpane.add (list_vpacker);
278         list_hpane.add (global_hpacker);
279         list_hpane.set_child_minsize (list_vpacker, 1);
280
281
282         XMLNode const * settings = ARDOUR_UI::instance()->mixer_settings();
283         XMLProperty const * prop;
284         float fract;
285
286         {
287                 LocaleGuard lg;
288
289                 if (!settings || ((prop = settings->property ("mixer-rhs-pane1-pos")) == 0) || ((fract = atof (prop->value())) > 1.0)) {
290                         rhs_pane1.set_divider (0, 0.6f);
291                 } else {
292                         rhs_pane1.set_divider (0, fract);
293                 }
294                 if (!settings || ((prop = settings->property ("mixer-rhs-pane2-pos")) == 0) || ((fract = atof (prop->value())) > 1.0)) {
295                         rhs_pane2.set_divider (0, 0.7f);
296                 } else {
297                         rhs_pane2.set_divider (0, fract);
298                 }
299                 if (!settings || ((prop = settings->property ("mixer-list-hpane-pos")) == 0) || ((fract = atof (prop->value())) > 1.0)) {
300                         list_hpane.set_divider (0, 0.2f);
301                 } else {
302                         list_hpane.set_divider (0, fract);
303                 }
304                 if (!settings || ((prop = settings->property ("mixer-inner-pane-pos")) == 0)  || ((fract = atof (prop->value())) > 1.0)) {
305                         inner_pane.set_divider (0, 0.8f);
306                 } else {
307                         inner_pane.set_divider (0, atof (prop->value()));
308                 }
309         }
310
311         rhs_pane1.set_drag_cursor (*PublicEditor::instance().cursors()->expand_up_down);
312         rhs_pane2.set_drag_cursor (*PublicEditor::instance().cursors()->expand_up_down);
313         list_hpane.set_drag_cursor (*PublicEditor::instance().cursors()->expand_left_right);
314         inner_pane.set_drag_cursor (*PublicEditor::instance().cursors()->expand_left_right);
315
316         _content.pack_start (list_hpane, true, true);
317
318         update_title ();
319
320         route_group_display_button_box->show();
321         route_group_add_button->show();
322         route_group_remove_button->show();
323
324         _content.show ();
325         _content.set_name ("MixerWindow");
326
327         global_hpacker.show();
328         scroller.show();
329         scroller_base.show();
330         scroller_hpacker.show();
331         mixer_scroller_vpacker.show();
332         list_vpacker.show();
333         group_display_button_label.show();
334         group_display_button.show();
335         group_display_scroller.show();
336         favorite_plugins_scroller.show();
337         group_display_vbox.show();
338         group_display_frame.show();
339         favorite_plugins_frame.show();
340         rhs_pane1.show();
341         rhs_pane2.show();
342         strip_packer.show();
343         inner_pane.show();
344         vca_scroller.show();
345         vca_vpacker.show();
346         vca_hpacker.show();
347         vca_label_bar.show();
348         vca_label.show();
349         vca_scroller_base.show();
350         out_packer.show();
351         list_hpane.show();
352         group_display.show();
353         favorite_plugins_display.show();
354
355         MixerStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_strip, this, _1), gui_context());
356
357         /* handle escape */
358
359         ARDOUR_UI::instance()->Escape.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::escape, this), gui_context());
360
361 #ifndef DEFER_PLUGIN_SELECTOR_LOAD
362         _plugin_selector = new PluginSelector (PluginManager::instance ());
363 #else
364 #error implement deferred Plugin-Favorite list
365 #endif
366         PluginManager::instance ().PluginListChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
367         PluginManager::instance ().PluginStatusesChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
368         ARDOUR::Plugin::PresetsChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
369 }
370
371 Mixer_UI::~Mixer_UI ()
372 {
373         if (_monitor_section) {
374                 monitor_section_detached ();
375                 delete _monitor_section;
376         }
377         delete _plugin_selector;
378 }
379
380 void
381 Mixer_UI::escape ()
382 {
383         select_none ();
384 }
385
386 Gtk::Window*
387 Mixer_UI::use_own_window (bool and_fill_it)
388 {
389         bool new_window = !own_window();
390
391         Gtk::Window* win = Tabbable::use_own_window (and_fill_it);
392
393         if (win && new_window) {
394                 win->set_name ("MixerWindow");
395                 ARDOUR_UI::instance()->setup_toplevel_window (*win, _("Mixer"), this);
396                 win->signal_event().connect (sigc::bind (sigc::ptr_fun (&Keyboard::catch_user_event_for_pre_dialog_focus), win));
397                 win->set_data ("ardour-bindings", bindings);
398                 update_title ();
399                 if (!win->get_focus()) {
400                         /* set focus widget to something, anything */
401                         win->set_focus (scroller);
402                 }
403         }
404
405         return win;
406 }
407
408 void
409 Mixer_UI::show_window ()
410 {
411         Tabbable::show_window ();
412
413         /* show/hide group tabs as required */
414         parameter_changed ("show-group-tabs");
415
416         /* now reset each strips width so the right widgets are shown */
417
418         TreeModel::Children rows = track_model->children();
419         TreeModel::Children::iterator ri;
420
421         for (ri = rows.begin(); ri != rows.end(); ++ri) {
422                 AxisView* av = (*ri)[stripable_columns.strip];
423                 MixerStrip* ms = dynamic_cast<MixerStrip*> (av);
424                 if (!ms) {
425                         continue;
426                 }
427                 ms->set_width_enum (ms->get_width_enum (), ms->width_owner());
428                 /* Fix visibility of mixer strip stuff */
429                 ms->parameter_changed (X_("mixer-element-visibility"));
430         }
431
432         /* force focus into main area */
433         scroller_base.grab_focus ();
434 }
435
436 void
437 Mixer_UI::remove_master (VCAMasterStrip* vms)
438 {
439         if (_session && _session->deletion_in_progress()) {
440                 /* its all being taken care of */
441                 return;
442         }
443
444         TreeModel::Children rows = track_model->children();
445         TreeModel::Children::iterator ri;
446
447         for (ri = rows.begin(); ri != rows.end(); ++ri) {
448                 if ((*ri)[stripable_columns.strip] == vms) {
449                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
450                         track_model->erase (ri);
451                         break;
452                 }
453         }
454 }
455
456 bool
457 Mixer_UI::masters_scroller_button_release (GdkEventButton* ev)
458 {
459         using namespace Menu_Helpers;
460
461         if (Keyboard::is_context_menu_event (ev)) {
462                 ARDOUR_UI::instance()->add_route ();
463                 return true;
464         }
465
466         return false;
467 }
468
469 void
470 Mixer_UI::add_masters (VCAList& vlist)
471 {
472         StripableList sl;
473
474         for (VCAList::iterator v = vlist.begin(); v != vlist.end(); ++v) {
475                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
476         }
477
478         add_stripables (sl);
479 }
480
481 void
482 Mixer_UI::add_routes (RouteList& rlist)
483 {
484         StripableList sl;
485
486         for (RouteList::iterator r = rlist.begin(); r != rlist.end(); ++r) {
487                 sl.push_back (*r);
488         }
489
490         add_stripables (sl);
491 }
492
493 void
494 Mixer_UI::add_stripables (StripableList& slist)
495 {
496         Gtk::TreeModel::Children::iterator insert_iter = track_model->children().end();
497         bool from_scratch = (track_model->children().size() == 0);
498         uint32_t nroutes = 0;
499
500         slist.sort (StripablePresentationInfoSorter());
501
502         for (Gtk::TreeModel::Children::iterator it = track_model->children().begin(); it != track_model->children().end(); ++it) {
503                 boost::shared_ptr<Stripable> s = (*it)[stripable_columns.stripable];
504
505                 if (!s) {
506                         continue;
507                 }
508
509                 nroutes++;
510
511                 if (s->presentation_info().order() == (slist.front()->presentation_info().order() + slist.size())) {
512                         insert_iter = it;
513                         break;
514                 }
515         }
516
517         MixerStrip* strip;
518
519
520         try {
521                 PBD::Unwinder<bool> uw (no_track_list_redisplay, true);
522
523                 track_display.set_model (Glib::RefPtr<ListStore>());
524
525                 for (StripableList::iterator s = slist.begin(); s != slist.end(); ++s) {
526
527                         boost::shared_ptr<Route> route;
528                         boost::shared_ptr<VCA> vca;
529
530                         if ((vca  = boost::dynamic_pointer_cast<VCA> (*s))) {
531
532                                 VCAMasterStrip* vms = new VCAMasterStrip (_session, vca);
533
534                                 TreeModel::Row row = *(track_model->append());
535
536                                 row[stripable_columns.text] = vca->name();
537                                 row[stripable_columns.visible] = vms->marked_for_display ();
538                                 row[stripable_columns.strip] = vms;
539                                 row[stripable_columns.stripable] = vca;
540
541                                 vms->CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_master, this, _1), gui_context());
542
543                         } else if ((route = boost::dynamic_pointer_cast<Route> (*s))) {
544
545                                 if (route->is_auditioner()) {
546                                         continue;
547                                 }
548
549                                 if (route->is_monitor()) {
550
551                                         if (!_monitor_section) {
552                                                 _monitor_section = new MonitorSection (_session);
553
554                                                 XMLNode* mnode = ARDOUR_UI::instance()->tearoff_settings (X_("monitor-section"));
555                                                 if (mnode) {
556                                                         _monitor_section->tearoff().set_state (*mnode);
557                                                 }
558                                         }
559
560                                         out_packer.pack_end (_monitor_section->tearoff(), false, false);
561                                         _monitor_section->set_session (_session);
562                                         _monitor_section->tearoff().show_all ();
563
564                                         _monitor_section->tearoff().Detach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_detached));
565                                         _monitor_section->tearoff().Attach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_attached));
566
567                                         monitor_section_attached ();
568
569                                         route->DropReferences.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::monitor_section_going_away, this), gui_context());
570
571                                         /* no regular strip shown for control out */
572
573                                         continue;
574                                 }
575
576                                 strip = new MixerStrip (*this, _session, route);
577                                 strips.push_back (strip);
578
579                                 UIConfiguration::instance().get_default_narrow_ms() ? _strip_width = Narrow : _strip_width = Wide;
580
581                                 if (strip->width_owner() != strip) {
582                                         strip->set_width_enum (_strip_width, this);
583                                 }
584
585                                 show_strip (strip);
586
587                                 if (!route->is_master()) {
588
589                                         TreeModel::Row row = *(track_model->insert (insert_iter));
590
591                                         row[stripable_columns.text] = route->name();
592                                         row[stripable_columns.visible] = strip->marked_for_display();
593                                         row[stripable_columns.stripable] = route;
594                                         row[stripable_columns.strip] = strip;
595
596                                         if (nroutes != 0) {
597                                                 _selection.add (strip);
598                                         }
599
600                                 } else {
601
602                                         out_packer.pack_start (*strip, false, false);
603                                         strip->set_packed (true);
604                                 }
605
606                                 strip->WidthChanged.connect (sigc::mem_fun(*this, &Mixer_UI::strip_width_changed));
607                                 strip->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
608                         }
609
610                         (*s)->presentation_info().PropertyChanged.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::stripable_property_changed, this, _1, boost::weak_ptr<Stripable>(*s)), gui_context());
611                         (*s)->PropertyChanged.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::stripable_property_changed, this, _1, boost::weak_ptr<Stripable>(*s)), gui_context());
612                 }
613
614         } catch (const std::exception& e) {
615                 error << string_compose (_("Error adding GUI elements for new tracks/busses %1"), e.what()) << endmsg;
616         }
617
618         track_display.set_model (track_model);
619
620         if (!from_scratch) {
621                 sync_presentation_info_from_treeview ();
622         }
623
624         redisplay_track_list ();
625 }
626
627 void
628 Mixer_UI::deselect_all_strip_processors ()
629 {
630         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
631                 (*i)->deselect_all_processors();
632         }
633 }
634
635 void
636 Mixer_UI::select_strip (MixerStrip& ms, bool add)
637 {
638         if (add) {
639                 _selection.add (&ms);
640         } else {
641                 _selection.set (&ms);
642         }
643 }
644
645 void
646 Mixer_UI::select_none ()
647 {
648         _selection.clear_routes();
649         deselect_all_strip_processors();
650 }
651
652 void
653 Mixer_UI::delete_processors ()
654 {
655         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
656                 (*i)->delete_processors();
657         }
658 }
659
660
661 void
662 Mixer_UI::remove_strip (MixerStrip* strip)
663 {
664         if (_session && _session->deletion_in_progress()) {
665                 /* its all being taken care of */
666                 return;
667         }
668
669         TreeModel::Children rows = track_model->children();
670         TreeModel::Children::iterator ri;
671         list<MixerStrip *>::iterator i;
672
673         if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
674                 strips.erase (i);
675         }
676
677         for (ri = rows.begin(); ri != rows.end(); ++ri) {
678                 if ((*ri)[stripable_columns.strip] == strip) {
679                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
680                         track_model->erase (ri);
681                         break;
682                 }
683         }
684 }
685
686 void
687 Mixer_UI::presentation_info_changed (PropertyChange const & what_changed)
688 {
689         PropertyChange soh;
690         soh.add (Properties::selected);
691         soh.add (Properties::order);
692         soh.add (Properties::hidden);
693
694         if (what_changed.contains (soh)) {
695                 sync_treeview_from_presentation_info (what_changed);
696         }
697 }
698
699 void
700 Mixer_UI::sync_presentation_info_from_treeview ()
701 {
702         if (ignore_reorder || !_session || _session->deletion_in_progress()) {
703                 return;
704         }
705
706         TreeModel::Children rows = track_model->children();
707
708         if (rows.empty()) {
709                 return;
710         }
711
712         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync presentation info from treeview\n");
713
714         TreeModel::Children::iterator ri;
715         bool change = false;
716         uint32_t order = 0;
717
718         OrderingKeys sorted;
719         const size_t cmp_max = rows.size ();
720
721         // special case master if it's got PI order 0 lets keep it there
722         if (_session->master_out() && (_session->master_out()->presentation_info().order() == 0)) {
723                 order++;
724         }
725
726         PresentationInfo::ChangeSuspender cs;
727
728         for (ri = rows.begin(); ri != rows.end(); ++ri) {
729                 bool visible = (*ri)[stripable_columns.visible];
730                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
731
732                 if (!stripable) {
733                         continue;
734                 }
735
736                 /* Monitor and Auditioner do not get their presentation
737                  * info reset here.
738                  */
739
740                 if (stripable->is_monitor() || stripable->is_auditioner()) {
741                         continue;
742                 }
743
744                 /* Master also doesn't get set here but since the editor allows
745                  * it to be reordered, we need to preserve its ordering.
746                  */
747
748                 stripable->presentation_info().set_hidden (!visible);
749
750                 // master may not get set here, but if it is zero keep it there
751                 if (stripable->is_master() && (stripable->presentation_info().order() == 0)) {
752                         continue;
753                 }
754
755                 if (order != stripable->presentation_info().order()) {
756                         stripable->set_presentation_order (order);
757                         change = true;
758                 }
759
760                 sorted.push_back (OrderKeys (order, stripable, cmp_max));
761
762                 ++order;
763         }
764
765         if (!change) {
766                 // VCA (and Mixbus) special cases according to SortByNewDisplayOrder
767                 uint32_t n = 0;
768                 SortByNewDisplayOrder cmp;
769                 sort (sorted.begin(), sorted.end(), cmp);
770                 for (OrderingKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
771                         if (_session->master_out() && (_session->master_out()->presentation_info().order() == n)) {
772                                 ++n;
773                         }
774                         if (sr->old_display_order != n) {
775                                 change = true;
776                                 break;
777                         }
778                 }
779                 if (change) {
780                         n = 0;
781                         for (OrderingKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
782                                 if (_session->master_out() && (_session->master_out()->presentation_info().order() == n)) {
783                                         ++n;
784                                 }
785                                 if (sr->stripable->presentation_info().order() != n) {
786                                         sr->stripable->set_presentation_order (n);
787                                 }
788                         }
789                 }
790         }
791
792         if (change) {
793                 DEBUG_TRACE (DEBUG::OrderKeys, "... notify PI change from mixer GUI\n");
794                 _session->set_dirty();
795         }
796 }
797
798 void
799 Mixer_UI::sync_treeview_from_presentation_info (PropertyChange const & what_changed)
800 {
801         if (!_session || _session->deletion_in_progress()) {
802                 return;
803         }
804
805         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync model from presentation info.\n");
806
807         /* we could get here after either a change in the Mixer or Editor sort
808          * order, but either way, the mixer order keys reflect the intended
809          * order for the GUI, so reorder the treeview model to match it.
810          */
811
812         vector<int> neworder;
813         TreeModel::Children rows = track_model->children();
814         uint32_t old_order = 0;
815         bool changed = false;
816
817         if (rows.empty()) {
818                 return;
819         }
820
821         OrderingKeys sorted;
822         const size_t cmp_max = rows.size ();
823
824         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
825                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
826                 sorted.push_back (OrderKeys (old_order, stripable, cmp_max));
827         }
828
829         SortByNewDisplayOrder cmp;
830
831         sort (sorted.begin(), sorted.end(), cmp);
832         neworder.assign (sorted.size(), 0);
833
834         uint32_t n = 0;
835
836         for (OrderingKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
837
838                 neworder[n] = sr->old_display_order;
839
840                 if (sr->old_display_order != n) {
841                         changed = true;
842                 }
843         }
844
845         if (changed) {
846                 Unwinder<bool> uw (ignore_reorder, true);
847                 track_model->reorder (neworder);
848         }
849
850         if (what_changed.contains (Properties::selected)) {
851
852                 PresentationInfo::ChangeSuspender cs;
853
854                 for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
855                         boost::shared_ptr<Stripable> stripable = (*i)->stripable();
856                         if (stripable && stripable->presentation_info().selected()) {
857                                 _selection.add (*i);
858                         } else {
859                                 _selection.remove (*i);
860                         }
861                 }
862
863                 if (!_selection.axes.empty() && !PublicEditor::instance().track_selection_change_without_scroll ()) {
864                         move_stripable_into_view ((*_selection.axes.begin())->stripable());
865                 }
866         }
867
868         redisplay_track_list ();
869 }
870
871
872 MixerStrip*
873 Mixer_UI::strip_by_route (boost::shared_ptr<Route> r) const
874 {
875         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
876                 if ((*i)->route() == r) {
877                         return (*i);
878                 }
879         }
880
881         return 0;
882 }
883
884 MixerStrip*
885 Mixer_UI::strip_by_stripable (boost::shared_ptr<Stripable> s) const
886 {
887         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
888                 if ((*i)->stripable() == s) {
889                         return (*i);
890                 }
891         }
892
893         return 0;
894 }
895
896 AxisView*
897 Mixer_UI::axis_by_stripable (boost::shared_ptr<Stripable> s) const
898 {
899         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
900                 if ((*i)->stripable() == s) {
901                         return (*i);
902                 }
903         }
904
905         return 0;
906 }
907
908 bool
909 Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
910 {
911         if (ev->button == 1) {
912                 if (_selection.selected (strip)) {
913                         /* primary-click: toggle selection state of strip */
914                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
915                                 _selection.remove (strip);
916                         } else if (_selection.axes.size() > 1) {
917                                 /* de-select others */
918                                 _selection.set (strip);
919                         }
920                 } else {
921                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
922                                 _selection.add (strip);
923                         } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
924
925                                 /* extend selection */
926
927                                 vector<MixerStrip*> tmp;
928                                 bool accumulate = false;
929                                 bool found_another = false;
930
931                                 OrderingKeys sorted;
932                                 const size_t cmp_max = strips.size ();
933                                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
934                                         sorted.push_back (OrderKeys (-1, (*i)->stripable(), cmp_max));
935                                 }
936                                 SortByNewDisplayOrder cmp;
937                                 sort (sorted.begin(), sorted.end(), cmp);
938
939                                 for (OrderingKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr) {
940                                         MixerStrip* ms = strip_by_stripable (sr->stripable);
941                                         assert (ms);
942
943                                         if (ms == strip) {
944                                                 /* hit clicked strip, start accumulating till we hit the first
945                                                    selected strip
946                                                 */
947                                                 if (accumulate) {
948                                                         /* done */
949                                                         break;
950                                                 } else {
951                                                         accumulate = true;
952                                                 }
953                                         } else if (_selection.selected (ms)) {
954                                                 /* hit selected strip. if currently accumulating others,
955                                                    we're done. if not accumulating others, start doing so.
956                                                 */
957                                                 found_another = true;
958                                                 if (accumulate) {
959                                                         /* done */
960                                                         break;
961                                                 } else {
962                                                         accumulate = true;
963                                                 }
964                                         } else {
965                                                 if (accumulate) {
966                                                         tmp.push_back (ms);
967                                                 }
968                                         }
969                                 }
970
971                                 tmp.push_back (strip);
972
973                                 if (found_another) {
974                                         PresentationInfo::ChangeSuspender cs;
975                                         for (vector<MixerStrip*>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
976                                                 _selection.add (*i);
977                                         }
978                                 } else {
979                                         _selection.set (strip);  //user wants to start a range selection, but there aren't any others selected yet
980                                 }
981                         } else {
982                                 _selection.set (strip);
983                         }
984                 }
985         }
986
987         return true;
988 }
989
990 void
991 Mixer_UI::set_session (Session* sess)
992 {
993         SessionHandlePtr::set_session (sess);
994
995         if (_plugin_selector) {
996                 _plugin_selector->set_session (_session);
997         }
998
999         _group_tabs->set_session (sess);
1000
1001         if (_monitor_section) {
1002                 _monitor_section->set_session (_session);
1003         }
1004
1005         if (!_session) {
1006                 return;
1007         }
1008
1009         refill_favorite_plugins();
1010
1011         XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
1012         set_state (*node, 0);
1013
1014         update_title ();
1015
1016         initial_track_display ();
1017
1018         _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_routes, this, _1), gui_context());
1019         _session->route_group_added.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_route_group, this, _1), gui_context());
1020         _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
1021         _session->route_groups_reordered.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
1022         _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
1023         _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
1024         _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
1025
1026         _session->vca_manager().VCAAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_masters, this, _1), gui_context());
1027
1028         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
1029
1030         route_groups_changed ();
1031
1032         if (_visible) {
1033                 show_window();
1034         }
1035         start_updating ();
1036 }
1037
1038 void
1039 Mixer_UI::session_going_away ()
1040 {
1041         ENSURE_GUI_THREAD (*this, &Mixer_UI::session_going_away);
1042
1043         _in_group_rebuild_or_clear = true;
1044         group_model->clear ();
1045         _in_group_rebuild_or_clear = false;
1046
1047         _selection.clear ();
1048         track_model->clear ();
1049
1050         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1051                 delete (*i);
1052         }
1053
1054         if (_monitor_section) {
1055                 _monitor_section->tearoff().hide_visible ();
1056         }
1057
1058         monitor_section_detached ();
1059
1060         strips.clear ();
1061
1062         stop_updating ();
1063
1064         SessionHandlePtr::session_going_away ();
1065
1066         _session = 0;
1067         update_title ();
1068 }
1069
1070 void
1071 Mixer_UI::track_visibility_changed (std::string const & path)
1072 {
1073         if (_session && _session->deletion_in_progress()) {
1074                 return;
1075         }
1076
1077         TreeIter iter;
1078
1079         if ((iter = track_model->get_iter (path))) {
1080
1081                 AxisView* av = (*iter)[stripable_columns.strip];
1082                 bool visible = (*iter)[stripable_columns.visible];
1083
1084                 if (av->set_marked_for_display (!visible)) {
1085                         update_track_visibility ();
1086                 }
1087         }
1088 }
1089
1090 void
1091 Mixer_UI::update_track_visibility ()
1092 {
1093         TreeModel::Children rows = track_model->children();
1094         TreeModel::Children::iterator i;
1095
1096         {
1097                 Unwinder<bool> uw (no_track_list_redisplay, true);
1098
1099                 for (i = rows.begin(); i != rows.end(); ++i) {
1100                         AxisView* av = (*i)[stripable_columns.strip];
1101                         (*i)[stripable_columns.visible] = av->marked_for_display ();
1102                 }
1103
1104                 /* force presentation catch up with visibility changes
1105                  */
1106
1107                 sync_presentation_info_from_treeview ();
1108         }
1109
1110         redisplay_track_list ();
1111 }
1112
1113 void
1114 Mixer_UI::show_strip (MixerStrip* ms)
1115 {
1116         TreeModel::Children rows = track_model->children();
1117         TreeModel::Children::iterator i;
1118
1119         for (i = rows.begin(); i != rows.end(); ++i) {
1120
1121                 AxisView* av = (*i)[stripable_columns.strip];
1122                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1123                 if (strip == ms) {
1124                         (*i)[stripable_columns.visible] = true;
1125                         av->set_marked_for_display (true);
1126                         update_track_visibility ();
1127                         break;
1128                 }
1129         }
1130 }
1131
1132 void
1133 Mixer_UI::hide_strip (MixerStrip* ms)
1134 {
1135         TreeModel::Children rows = track_model->children();
1136         TreeModel::Children::iterator i;
1137
1138         for (i = rows.begin(); i != rows.end(); ++i) {
1139
1140                 AxisView* av = (*i)[stripable_columns.strip];
1141                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1142                 if (strip == ms) {
1143                         (*i)[stripable_columns.visible] = false;
1144                         av->set_marked_for_display (false);
1145                         update_track_visibility ();
1146                         break;
1147                 }
1148         }
1149 }
1150
1151 gint
1152 Mixer_UI::start_updating ()
1153 {
1154     fast_screen_update_connection = Timers::super_rapid_connect (sigc::mem_fun(*this, &Mixer_UI::fast_update_strips));
1155     return 0;
1156 }
1157
1158 gint
1159 Mixer_UI::stop_updating ()
1160 {
1161     fast_screen_update_connection.disconnect();
1162     return 0;
1163 }
1164
1165 void
1166 Mixer_UI::fast_update_strips ()
1167 {
1168         if (_content.is_mapped () && _session) {
1169                 for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1170                         (*i)->fast_update ();
1171                 }
1172         }
1173 }
1174
1175 void
1176 Mixer_UI::set_all_strips_visibility (bool yn)
1177 {
1178         TreeModel::Children rows = track_model->children();
1179         TreeModel::Children::iterator i;
1180
1181         {
1182                 Unwinder<bool> uw (no_track_list_redisplay, true);
1183
1184                 for (i = rows.begin(); i != rows.end(); ++i) {
1185
1186                         AxisView* av = (*i)[stripable_columns.strip];
1187                         MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1188
1189                         if (!strip) {
1190                                 continue;
1191                         }
1192
1193                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1194                                 continue;
1195                         }
1196
1197                         (*i)[stripable_columns.visible] = yn;
1198                 }
1199         }
1200
1201         redisplay_track_list ();
1202 }
1203
1204
1205 void
1206 Mixer_UI::set_all_audio_midi_visibility (int tracks, bool yn)
1207 {
1208         TreeModel::Children rows = track_model->children();
1209         TreeModel::Children::iterator i;
1210
1211         {
1212                 Unwinder<bool> uw (no_track_list_redisplay, true);
1213
1214                 for (i = rows.begin(); i != rows.end(); ++i) {
1215
1216                         AxisView* av = (*i)[stripable_columns.strip];
1217                         MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1218
1219                         if (!strip) {
1220                                 continue;
1221                         }
1222
1223                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1224                                 continue;
1225                         }
1226
1227                         boost::shared_ptr<AudioTrack> at = strip->audio_track();
1228                         boost::shared_ptr<MidiTrack> mt = strip->midi_track();
1229
1230                         switch (tracks) {
1231                         case 0:
1232                                 (*i)[stripable_columns.visible] = yn;
1233                                 break;
1234
1235                         case 1:
1236                                 if (at) { /* track */
1237                                         (*i)[stripable_columns.visible] = yn;
1238                                 }
1239                                 break;
1240
1241                         case 2:
1242                                 if (!at && !mt) { /* bus */
1243                                         (*i)[stripable_columns.visible] = yn;
1244                                 }
1245                                 break;
1246
1247                         case 3:
1248                                 if (mt) { /* midi-track */
1249                                         (*i)[stripable_columns.visible] = yn;
1250                                 }
1251                                 break;
1252                         }
1253                 }
1254         }
1255
1256         redisplay_track_list ();
1257 }
1258
1259 void
1260 Mixer_UI::hide_all_routes ()
1261 {
1262         set_all_strips_visibility (false);
1263 }
1264
1265 void
1266 Mixer_UI::show_all_routes ()
1267 {
1268         set_all_strips_visibility (true);
1269 }
1270
1271 void
1272 Mixer_UI::show_all_audiobus ()
1273 {
1274         set_all_audio_midi_visibility (2, true);
1275 }
1276 void
1277 Mixer_UI::hide_all_audiobus ()
1278 {
1279         set_all_audio_midi_visibility (2, false);
1280 }
1281
1282 void
1283 Mixer_UI::show_all_audiotracks()
1284 {
1285         set_all_audio_midi_visibility (1, true);
1286 }
1287 void
1288 Mixer_UI::hide_all_audiotracks ()
1289 {
1290         set_all_audio_midi_visibility (1, false);
1291 }
1292
1293 void
1294 Mixer_UI::show_all_miditracks()
1295 {
1296         set_all_audio_midi_visibility (3, true);
1297 }
1298 void
1299 Mixer_UI::hide_all_miditracks ()
1300 {
1301         set_all_audio_midi_visibility (3, false);
1302 }
1303
1304 void
1305 Mixer_UI::track_list_reorder (const TreeModel::Path&, const TreeModel::iterator&, int* /*new_order*/)
1306 {
1307         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview reordered\n");
1308         sync_presentation_info_from_treeview ();
1309 }
1310
1311 void
1312 Mixer_UI::track_list_delete (const Gtk::TreeModel::Path&)
1313 {
1314         /* this happens as the second step of a DnD within the treeview as well
1315            as when a row/route is actually deleted.
1316
1317            if it was a deletion then we have to force a redisplay because
1318            order keys may not have changed.
1319         */
1320
1321         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview row deleted\n");
1322         sync_presentation_info_from_treeview ();
1323
1324         if (_route_deletion_in_progress) {
1325                 redisplay_track_list ();
1326         }
1327 }
1328
1329 void
1330 Mixer_UI::spill_redisplay (boost::shared_ptr<VCA> vca)
1331 {
1332         TreeModel::Children rows = track_model->children();
1333         std::list<boost::shared_ptr<VCA> > vcas;
1334         vcas.push_back (vca);
1335
1336         for (TreeModel::Children::const_iterator i = rows.begin(); i != rows.end(); ++i) {
1337                 AxisView* av = (*i)[stripable_columns.strip];
1338                 VCAMasterStrip* vms = dynamic_cast<VCAMasterStrip*> (av);
1339                 if (vms && vms->vca()->slaved_to (vca)) {
1340                         vcas.push_back (vms->vca());
1341                 }
1342         }
1343
1344         for (TreeModel::Children::const_iterator i = rows.begin(); i != rows.end(); ++i) {
1345
1346                 AxisView* av = (*i)[stripable_columns.strip];
1347                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1348                 bool const visible = (*i)[stripable_columns.visible];
1349
1350                 if (!strip) {
1351                         /* we're in the middle of changing a row, don't worry */
1352                         continue;
1353                 }
1354
1355                 if (!strip->route()) {
1356                         /* non-route element */
1357                         continue;
1358                 }
1359
1360                 if (strip->route()->is_master() || strip->route()->is_monitor()) {
1361                         continue;
1362                 }
1363
1364                 bool slaved = false;
1365                 for (std::list<boost::shared_ptr<VCA> >::const_iterator m = vcas.begin(); m != vcas.end(); ++m) {
1366                         if (strip->route()->slaved_to (*m)) {
1367                                 slaved = true;
1368                                 break;
1369                         }
1370                 }
1371
1372                 if (slaved && visible) {
1373
1374                         if (strip->packed()) {
1375                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1376                         } else {
1377                                 strip_packer.pack_start (*strip, false, false);
1378                                 strip->set_packed (true);
1379                         }
1380
1381                 } else {
1382
1383                         if (strip->packed()) {
1384                                 strip_packer.remove (*strip);
1385                                 strip->set_packed (false);
1386                         }
1387                 }
1388         }
1389 }
1390
1391 void
1392 Mixer_UI::redisplay_track_list ()
1393 {
1394         if (no_track_list_redisplay) {
1395                 return;
1396         }
1397
1398         boost::shared_ptr<Stripable> ss = spilled_strip.lock ();
1399         if (ss) {
1400                 boost::shared_ptr<VCA> sv = boost::dynamic_pointer_cast<VCA> (ss);
1401                 if (sv) {
1402                         spill_redisplay (sv);
1403                         return;
1404                 }
1405         }
1406
1407         TreeModel::Children rows = track_model->children();
1408         TreeModel::Children::iterator i;
1409         uint32_t n_masters = 0;
1410
1411         container_clear (vca_hpacker);
1412         vca_hpacker.pack_end (vca_scroller_base, true, true);
1413
1414         for (i = rows.begin(); i != rows.end(); ++i) {
1415
1416                 AxisView* s = (*i)[stripable_columns.strip];
1417                 bool const visible = (*i)[stripable_columns.visible];
1418                 boost::shared_ptr<Stripable> stripable = (*i)[stripable_columns.stripable];
1419
1420                 if (!s) {
1421                         /* we're in the middle of changing a row, don't worry */
1422                         continue;
1423                 }
1424
1425                 VCAMasterStrip* vms;
1426
1427                 if ((vms = dynamic_cast<VCAMasterStrip*> (s))) {
1428                         if (visible) {
1429                                 vca_hpacker.pack_start (*vms, false, false);
1430                                 vms->show ();
1431                                 n_masters++;
1432                         }
1433                         continue;
1434                 }
1435
1436                 MixerStrip* strip = dynamic_cast<MixerStrip*> (s);
1437
1438                 if (!strip) {
1439                         continue;
1440                 }
1441
1442                 if (visible) {
1443
1444                         if (strip->packed()) {
1445                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1446                         } else {
1447                                 strip_packer.pack_start (*strip, false, false);
1448                                 strip->set_packed (true);
1449                         }
1450
1451                 } else {
1452
1453                         if (stripable->is_master() || stripable->is_monitor()) {
1454                                 /* do nothing, these cannot be hidden */
1455                         } else {
1456                                 if (strip->packed()) {
1457                                         strip_packer.remove (*strip);
1458                                         strip->set_packed (false);
1459                                 }
1460                         }
1461                 }
1462         }
1463
1464         /* update visibility of VCA assign buttons */
1465
1466         if (n_masters == 0) {
1467                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::remove_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1468                 vca_vpacker.hide ();
1469         } else {
1470                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::add_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1471                 vca_vpacker.show ();
1472         }
1473
1474         _group_tabs->set_dirty ();
1475 }
1476
1477 void
1478 Mixer_UI::strip_width_changed ()
1479 {
1480         _group_tabs->set_dirty ();
1481
1482 #ifdef __APPLE__
1483         TreeModel::Children rows = track_model->children();
1484         TreeModel::Children::iterator i;
1485         long order;
1486
1487         for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
1488                 AxisView* av = (*i)[stripable_columns.strip];
1489                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1490
1491                 if (strip == 0) {
1492                         continue;
1493                 }
1494
1495                 bool visible = (*i)[stripable_columns.visible];
1496
1497                 if (visible) {
1498                         strip->queue_draw();
1499                 }
1500         }
1501 #endif
1502
1503 }
1504
1505 struct PresentationInfoMixerSorter
1506 {
1507         bool operator() (boost::shared_ptr<Stripable> a, boost::shared_ptr<Stripable> b) {
1508                 if (a->is_master()) {
1509                         /* master after everything else */
1510                         return false;
1511                 } else if (b->is_master()) {
1512                         /* everything else before master */
1513                         return true;
1514                 }
1515                 return a->presentation_info().order () < b->presentation_info().order ();
1516         }
1517 };
1518
1519 void
1520 Mixer_UI::initial_track_display ()
1521 {
1522         StripableList sl;
1523
1524         boost::shared_ptr<RouteList> routes = _session->get_routes();
1525
1526         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
1527                 sl.push_back (*r);
1528         }
1529
1530         VCAList vcas = _session->vca_manager().vcas();
1531
1532         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
1533                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
1534         }
1535
1536         sl.sort (PresentationInfoMixerSorter());
1537
1538         {
1539                 /* These are also used inside ::add_stripables() but we need
1540                  *  them here because we're going to clear the track_model also.
1541                  */
1542                 Unwinder<bool> uw1 (no_track_list_redisplay, true);
1543                 Unwinder<bool> uw2 (ignore_reorder, true);
1544
1545                 track_model->clear ();
1546                 add_stripables (sl);
1547         }
1548
1549         sync_treeview_from_presentation_info (Properties::order);
1550 }
1551
1552 void
1553 Mixer_UI::show_track_list_menu ()
1554 {
1555         if (track_menu == 0) {
1556                 build_track_menu ();
1557         }
1558
1559         track_menu->popup (1, gtk_get_current_event_time());
1560 }
1561
1562 bool
1563 Mixer_UI::track_display_button_press (GdkEventButton* ev)
1564 {
1565         if (Keyboard::is_context_menu_event (ev)) {
1566                 show_track_list_menu ();
1567                 return true;
1568         }
1569         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 1)) {
1570                 TreeModel::Path path;
1571                 TreeViewColumn* column;
1572                 int cellx, celly;
1573                 if (track_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1574                         TreeIter iter = track_model->get_iter (path);
1575                         if ((*iter)[stripable_columns.visible]) {
1576                                 boost::shared_ptr<ARDOUR::Stripable> s = (*iter)[stripable_columns.stripable];
1577                                 move_stripable_into_view (s);
1578                         }
1579                 }
1580         }
1581
1582         return false;
1583 }
1584
1585 void
1586 Mixer_UI::move_stripable_into_view (boost::shared_ptr<ARDOUR::Stripable> s)
1587 {
1588         if (!scroller.get_hscrollbar()) {
1589                 return;
1590         }
1591         if (s->presentation_info().special () || s->presentation_info().flag_match (PresentationInfo::VCA)) {
1592                 return;
1593         }
1594 #ifdef MIXBUS
1595         if (s->mixbus ()) {
1596                 return;
1597         }
1598 #endif
1599         bool found = false;
1600         int x0 = 0;
1601         Gtk::Allocation alloc;
1602         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
1603                 if ((*i)->route() == s) {
1604                         int y;
1605                         found = true;
1606                         (*i)->translate_coordinates (strip_packer, 0, 0, x0, y);
1607                         alloc = (*i)->get_allocation ();
1608                         break;
1609                 }
1610         }
1611         if (!found) {
1612                 return;
1613         }
1614
1615         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1616
1617         if (x0 < adj->get_value()) {
1618                 adj->set_value (max (adj->get_lower(), min (adj->get_upper(), (double) x0)));
1619         } else if (x0 + alloc.get_width() >= adj->get_value() + adj->get_page_size()) {
1620                 int x1 = x0 + alloc.get_width() - adj->get_page_size();
1621                 adj->set_value (max (adj->get_lower(), min (adj->get_upper(), (double) x1)));
1622         }
1623 }
1624
1625 void
1626 Mixer_UI::build_track_menu ()
1627 {
1628         using namespace Menu_Helpers;
1629         using namespace Gtk;
1630
1631         track_menu = new Menu;
1632         track_menu->set_name ("ArdourContextMenu");
1633         MenuList& items = track_menu->items();
1634
1635         items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
1636         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
1637         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
1638         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
1639         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
1640         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
1641         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::show_all_miditracks)));
1642         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::hide_all_miditracks)));
1643
1644 }
1645
1646 void
1647 Mixer_UI::stripable_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> ws)
1648 {
1649         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
1650                 return;
1651         }
1652
1653         boost::shared_ptr<Stripable> s = ws.lock ();
1654
1655         if (!s) {
1656                 return;
1657         }
1658
1659         TreeModel::Children rows = track_model->children();
1660         TreeModel::Children::iterator i;
1661
1662         for (i = rows.begin(); i != rows.end(); ++i) {
1663                 boost::shared_ptr<Stripable> ss = (*i)[stripable_columns.stripable];
1664
1665                 if (s == ss) {
1666
1667                         if (what_changed.contains (ARDOUR::Properties::name)) {
1668                                 (*i)[stripable_columns.text] = s->name();
1669                         }
1670
1671                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
1672                                 (*i)[stripable_columns.visible] = !s->presentation_info().hidden();
1673                                 redisplay_track_list ();
1674                         }
1675
1676                         return;
1677                 }
1678         }
1679
1680         error << _("track display list item for renamed strip not found!") << endmsg;
1681 }
1682
1683 bool
1684 Mixer_UI::group_display_button_press (GdkEventButton* ev)
1685 {
1686         TreeModel::Path path;
1687         TreeViewColumn* column;
1688         int cellx;
1689         int celly;
1690
1691         if (!group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1692                 _group_tabs->get_menu(0)->popup (1, ev->time);
1693                 return true;
1694         }
1695
1696         TreeIter iter = group_model->get_iter (path);
1697         if (!iter) {
1698                 _group_tabs->get_menu(0)->popup (1, ev->time);
1699                 return true;
1700         }
1701
1702         RouteGroup* group = (*iter)[group_columns.group];
1703
1704         if (Keyboard::is_context_menu_event (ev)) {
1705                 _group_tabs->get_menu(group)->popup (1, ev->time);
1706                 return true;
1707         }
1708
1709         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
1710         case 1:
1711                 if (Keyboard::is_edit_event (ev)) {
1712                         if (group) {
1713                                 // edit_route_group (group);
1714 #ifdef __APPLE__
1715                                 group_display.queue_draw();
1716 #endif
1717                                 return true;
1718                         }
1719                 }
1720                 break;
1721
1722         case 0:
1723         {
1724                 bool visible = (*iter)[group_columns.visible];
1725                 (*iter)[group_columns.visible] = !visible;
1726 #ifdef __APPLE__
1727                 group_display.queue_draw();
1728 #endif
1729                 return true;
1730         }
1731
1732         default:
1733                 break;
1734         }
1735
1736         return false;
1737  }
1738
1739 void
1740 Mixer_UI::activate_all_route_groups ()
1741 {
1742         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
1743 }
1744
1745 void
1746 Mixer_UI::disable_all_route_groups ()
1747 {
1748         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
1749 }
1750
1751 void
1752 Mixer_UI::route_groups_changed ()
1753 {
1754         ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
1755
1756         _in_group_rebuild_or_clear = true;
1757
1758         /* just rebuild the while thing */
1759
1760         group_model->clear ();
1761
1762 #if 0
1763         /* this is currently not used,
1764          * Mixer_UI::group_display_button_press() has a case for it,
1765          * and a commented edit_route_group() but that's n/a since 2011.
1766          *
1767          * This code is left as reminder that
1768          * row[group_columns.group] = 0 has special meaning.
1769          */
1770         {
1771                 TreeModel::Row row;
1772                 row = *(group_model->append());
1773                 row[group_columns.visible] = true;
1774                 row[group_columns.text] = (_("-all-"));
1775                 row[group_columns.group] = 0;
1776         }
1777 #endif
1778
1779         _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
1780
1781         _group_tabs->set_dirty ();
1782         _in_group_rebuild_or_clear = false;
1783 }
1784
1785 void
1786 Mixer_UI::new_route_group ()
1787 {
1788         _group_tabs->run_new_group_dialog (0, false);
1789 }
1790
1791 void
1792 Mixer_UI::remove_selected_route_group ()
1793 {
1794         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1795         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1796
1797         if (rows.empty()) {
1798                 return;
1799         }
1800
1801         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1802         TreeIter iter;
1803
1804         /* selection mode is single, so rows.begin() is it */
1805
1806         if ((iter = group_model->get_iter (*i))) {
1807
1808                 RouteGroup* rg = (*iter)[group_columns.group];
1809
1810                 if (rg) {
1811                         _session->remove_route_group (*rg);
1812                 }
1813         }
1814 }
1815
1816 void
1817 Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
1818 {
1819         if (in_group_row_change) {
1820                 return;
1821         }
1822
1823         /* force an update of any mixer strips that are using this group,
1824            otherwise mix group names don't change in mixer strips
1825         */
1826
1827         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1828                 if ((*i)->route_group() == group) {
1829                         (*i)->route_group_changed();
1830                 }
1831         }
1832
1833         TreeModel::iterator i;
1834         TreeModel::Children rows = group_model->children();
1835         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1836
1837         in_group_row_change = true;
1838
1839         for (i = rows.begin(); i != rows.end(); ++i) {
1840                 if ((*i)[group_columns.group] == group) {
1841                         (*i)[group_columns.visible] = !group->is_hidden ();
1842                         (*i)[group_columns.text] = group->name ();
1843                         break;
1844                 }
1845         }
1846
1847         in_group_row_change = false;
1848
1849         if (change.contains (Properties::name)) {
1850                 _group_tabs->set_dirty ();
1851         }
1852
1853         for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
1854                 if ((*j)->route_group() == group) {
1855                         if (group->is_hidden ()) {
1856                                 hide_strip (*j);
1857                         } else {
1858                                 show_strip (*j);
1859                         }
1860                 }
1861         }
1862 }
1863
1864 void
1865 Mixer_UI::show_mixer_list (bool yn)
1866 {
1867         if (yn) {
1868                 list_vpacker.show ();
1869         } else {
1870                 list_vpacker.hide ();
1871         }
1872
1873         _show_mixer_list = yn;
1874 }
1875
1876 void
1877 Mixer_UI::show_monitor_section (bool yn)
1878 {
1879         if (!monitor_section()) {
1880                 return;
1881         }
1882         if (monitor_section()->tearoff().torn_off()) {
1883                 return;
1884         }
1885
1886         if (yn) {
1887                 monitor_section()->tearoff().show();
1888         } else {
1889                 monitor_section()->tearoff().hide();
1890         }
1891 }
1892
1893 void
1894 Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
1895 {
1896         RouteGroup* group;
1897         TreeIter iter;
1898
1899         if ((iter = group_model->get_iter (path))) {
1900
1901                 if ((group = (*iter)[group_columns.group]) == 0) {
1902                         return;
1903                 }
1904
1905                 if (new_text != group->name()) {
1906                         group->set_name (new_text);
1907                 }
1908         }
1909 }
1910
1911 void
1912 Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
1913 {
1914         RouteGroup* group;
1915
1916         if (in_group_row_change) {
1917                 return;
1918         }
1919
1920         if ((group = (*iter)[group_columns.group]) == 0) {
1921                 return;
1922         }
1923
1924         std::string name = (*iter)[group_columns.text];
1925
1926         if (name != group->name()) {
1927                 group->set_name (name);
1928         }
1929
1930         bool hidden = !(*iter)[group_columns.visible];
1931
1932         if (hidden != group->is_hidden ()) {
1933                 group->set_hidden (hidden, this);
1934         }
1935 }
1936
1937 /** Called when a group model row is deleted, but also when the model is
1938  *  reordered by a user drag-and-drop; the latter is what we are
1939  *  interested in here.
1940  */
1941 void
1942 Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
1943 {
1944         if (_in_group_rebuild_or_clear) {
1945                 return;
1946         }
1947
1948         /* Re-write the session's route group list so that the new order is preserved */
1949
1950         list<RouteGroup*> new_list;
1951
1952         Gtk::TreeModel::Children children = group_model->children();
1953         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
1954                 RouteGroup* g = (*i)[group_columns.group];
1955                 if (g) {
1956                         new_list.push_back (g);
1957                 }
1958         }
1959
1960         _session->reorder_route_groups (new_list);
1961 }
1962
1963
1964 void
1965 Mixer_UI::add_route_group (RouteGroup* group)
1966 {
1967         ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
1968         bool focus = false;
1969
1970         in_group_row_change = true;
1971
1972         TreeModel::Row row = *(group_model->append());
1973         row[group_columns.visible] = !group->is_hidden ();
1974         row[group_columns.group] = group;
1975         if (!group->name().empty()) {
1976                 row[group_columns.text] = group->name();
1977         } else {
1978                 row[group_columns.text] = _("unnamed");
1979                 focus = true;
1980         }
1981
1982         group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
1983
1984         if (focus) {
1985                 TreeViewColumn* col = group_display.get_column (0);
1986                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
1987                 group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
1988         }
1989
1990         _group_tabs->set_dirty ();
1991
1992         in_group_row_change = false;
1993 }
1994
1995 bool
1996 Mixer_UI::strip_scroller_button_release (GdkEventButton* ev)
1997 {
1998         using namespace Menu_Helpers;
1999
2000         if (Keyboard::is_context_menu_event (ev)) {
2001                 ARDOUR_UI::instance()->add_route ();
2002                 return true;
2003         }
2004
2005         return false;
2006 }
2007
2008 void
2009 Mixer_UI::scroller_drag_data_received (const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& data, guint info, guint time)
2010 {
2011         printf ("Mixer_UI::scroller_drag_data_received\n");
2012         if (data.get_target() != "PluginFavoritePtr") {
2013                 context->drag_finish (false, false, time);
2014                 return;
2015         }
2016
2017         const void * d = data.get_data();
2018         const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
2019
2020         PluginPresetList nfos;
2021         TreeView* source;
2022         tv->get_object_drag_data (nfos, &source);
2023
2024         Route::ProcessorList pl;
2025         bool ok = false;
2026
2027         for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
2028                 PluginPresetPtr ppp = (*i);
2029                 PluginInfoPtr pip = ppp->_pip;
2030                 if (!pip->is_instrument ()) {
2031                         continue;
2032                 }
2033                 ARDOUR_UI::instance()->session_add_midi_track ((RouteGroup*) 0, 1, _("MIDI"), Config->get_strict_io (), pip, ppp->_preset.valid ? &ppp->_preset : 0, PresentationInfo::max_order);
2034                 ok = true;
2035         }
2036
2037         context->drag_finish (ok, false, time);
2038 }
2039
2040 void
2041 Mixer_UI::set_strip_width (Width w, bool save)
2042 {
2043         _strip_width = w;
2044
2045         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2046                 (*i)->set_width_enum (w, save ? (*i)->width_owner() : this);
2047         }
2048 }
2049
2050
2051 struct PluginStateSorter {
2052 public:
2053         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2054                 std::list<std::string>::const_iterator aiter = std::find(_user.begin(), _user.end(), (*a).unique_id);
2055                 std::list<std::string>::const_iterator biter = std::find(_user.begin(), _user.end(), (*b).unique_id);
2056                 if (aiter != _user.end() && biter != _user.end()) {
2057                         return std::distance (_user.begin(), aiter)  < std::distance (_user.begin(), biter);
2058                 }
2059                 if (aiter != _user.end()) {
2060                         return true;
2061                 }
2062                 if (biter != _user.end()) {
2063                         return false;
2064                 }
2065                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2066         }
2067
2068         PluginStateSorter(std::list<std::string> user) : _user (user)  {}
2069 private:
2070         std::list<std::string> _user;
2071 };
2072
2073 int
2074 Mixer_UI::set_state (const XMLNode& node, int version)
2075 {
2076         XMLProperty const * prop;
2077         LocaleGuard lg;
2078
2079         Tabbable::set_state (node, version);
2080
2081         if ((prop = node.property ("narrow-strips"))) {
2082                 if (string_is_affirmative (prop->value())) {
2083                         set_strip_width (Narrow);
2084                 } else {
2085                         set_strip_width (Wide);
2086                 }
2087         }
2088
2089         if ((prop = node.property ("show-mixer"))) {
2090                 if (string_is_affirmative (prop->value())) {
2091                        _visible = true;
2092                 }
2093         }
2094
2095         if ((prop = node.property ("maximised"))) {
2096                 bool yn = string_is_affirmative (prop->value());
2097                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMaximalMixer"));
2098                 assert (act);
2099                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2100                 bool fs = tact && tact->get_active();
2101                 if (yn ^ fs) {
2102                         ActionManager::do_action ("Common", "ToggleMaximalMixer");
2103                 }
2104         }
2105
2106         if ((prop = node.property ("show-mixer-list"))) {
2107                 bool yn = string_is_affirmative (prop->value());
2108                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMixerList"));
2109                 assert (act);
2110                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2111
2112                 /* do it twice to force the change */
2113                 tact->set_active (!yn);
2114                 tact->set_active (yn);
2115         }
2116
2117
2118         XMLNode* plugin_order;
2119         if ((plugin_order = find_named_node (node, "PluginOrder")) != 0) {
2120                 store_current_favorite_order ();
2121                 std::list<string> order;
2122                 const XMLNodeList& kids = plugin_order->children("PluginInfo");
2123                 XMLNodeConstIterator i;
2124                 for (i = kids.begin(); i != kids.end(); ++i) {
2125                         if ((prop = (*i)->property ("unique-id"))) {
2126                                 std::string unique_id = prop->value();
2127                                 order.push_back (unique_id);
2128                                 if ((prop = (*i)->property ("expanded"))) {
2129                                         favorite_ui_state[unique_id] = string_is_affirmative (prop->value());
2130                                 }
2131                         }
2132                 }
2133                 PluginStateSorter cmp (order);
2134                 favorite_order.sort (cmp);
2135                 sync_treeview_from_favorite_order ();
2136         }
2137         return 0;
2138 }
2139
2140 XMLNode&
2141 Mixer_UI::get_state ()
2142 {
2143         XMLNode* node = new XMLNode (X_("Mixer"));
2144         char buf[128];
2145         LocaleGuard lg;
2146
2147         node->add_child_nocopy (Tabbable::get_state());
2148
2149         snprintf(buf,sizeof(buf), "%f", rhs_pane1.get_divider());
2150         node->add_property(X_("mixer-rhs-pane1-pos"), string(buf));
2151         snprintf(buf,sizeof(buf), "%f", rhs_pane2.get_divider());
2152         node->add_property(X_("mixer-rhs_pane2-pos"), string(buf));
2153         snprintf(buf,sizeof(buf), "%f", list_hpane.get_divider());
2154         node->add_property(X_("mixer-list-hpane-pos"), string(buf));
2155         snprintf(buf,sizeof(buf), "%f", inner_pane.get_divider());
2156         node->add_property(X_("mixer-inner-pane-pos"), string(buf));
2157
2158         node->add_property ("narrow-strips", _strip_width == Narrow ? "yes" : "no");
2159         node->add_property ("show-mixer", _visible ? "yes" : "no");
2160         node->add_property ("show-mixer-list", _show_mixer_list ? "yes" : "no");
2161         node->add_property ("maximised", _maximised ? "yes" : "no");
2162
2163         store_current_favorite_order ();
2164         XMLNode* plugin_order = new XMLNode ("PluginOrder");
2165         int cnt = 0;
2166         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i, ++cnt) {
2167                 XMLNode* p = new XMLNode ("PluginInfo");
2168                 p->add_property ("sort", cnt);
2169                 p->add_property ("unique-id", (*i)->unique_id);
2170                 if (favorite_ui_state.find ((*i)->unique_id) != favorite_ui_state.end ()) {
2171                         p->add_property ("expanded", favorite_ui_state[(*i)->unique_id]);
2172                 }
2173                 plugin_order->add_child_nocopy (*p);
2174         }
2175         node->add_child_nocopy (*plugin_order);
2176
2177         return *node;
2178 }
2179
2180 void
2181 Mixer_UI::scroll_left ()
2182 {
2183         if (!scroller.get_hscrollbar()) return;
2184         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2185         int sc_w = scroller.get_width();
2186         int sp_w = strip_packer.get_width();
2187         if (sp_w <= sc_w) {
2188                 return;
2189         }
2190         int lp = adj->get_value();
2191         int lm = 0;
2192         using namespace Gtk::Box_Helpers;
2193         const BoxList& strips = strip_packer.children();
2194         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2195                 lm += i->get_widget()->get_width ();
2196                 if (lm >= lp) {
2197                         lm -= i->get_widget()->get_width ();
2198                         break;
2199                 }
2200         }
2201         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2202 }
2203
2204 void
2205 Mixer_UI::scroll_right ()
2206 {
2207         if (!scroller.get_hscrollbar()) return;
2208         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2209         int sc_w = scroller.get_width();
2210         int sp_w = strip_packer.get_width();
2211         if (sp_w <= sc_w) {
2212                 return;
2213         }
2214         int lp = adj->get_value();
2215         int lm = 0;
2216         using namespace Gtk::Box_Helpers;
2217         const BoxList& strips = strip_packer.children();
2218         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2219                 lm += i->get_widget()->get_width ();
2220                 if (lm > lp + 1) {
2221                         break;
2222                 }
2223         }
2224         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2225 }
2226
2227 bool
2228 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
2229 {
2230         switch (ev->direction) {
2231         case GDK_SCROLL_LEFT:
2232                 scroll_left ();
2233                 return true;
2234         case GDK_SCROLL_UP:
2235                 if (ev->state & Keyboard::TertiaryModifier) {
2236                         scroll_left ();
2237                         return true;
2238                 }
2239                 return false;
2240
2241         case GDK_SCROLL_RIGHT:
2242                 scroll_right ();
2243                 return true;
2244
2245         case GDK_SCROLL_DOWN:
2246                 if (ev->state & Keyboard::TertiaryModifier) {
2247                         scroll_right ();
2248                         return true;
2249                 }
2250                 return false;
2251         }
2252
2253         return false;
2254 }
2255
2256
2257 void
2258 Mixer_UI::parameter_changed (string const & p)
2259 {
2260         if (p == "show-group-tabs") {
2261                 bool const s = _session->config.get_show_group_tabs ();
2262                 if (s) {
2263                         _group_tabs->show ();
2264                 } else {
2265                         _group_tabs->hide ();
2266                 }
2267         } else if (p == "default-narrow_ms") {
2268                 bool const s = UIConfiguration::instance().get_default_narrow_ms ();
2269                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2270                         (*i)->set_width_enum (s ? Narrow : Wide, this);
2271                 }
2272         } else if (p == "use-monitor-bus") {
2273                 if (_session && !_session->monitor_out()) {
2274                         monitor_section_detached ();
2275                 }
2276         }
2277 }
2278
2279 void
2280 Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
2281 {
2282         g->set_active (a, this);
2283 }
2284
2285 PluginSelector*
2286 Mixer_UI::plugin_selector()
2287 {
2288 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
2289         if (!_plugin_selector)
2290                 _plugin_selector = new PluginSelector (PluginManager::instance());
2291 #endif
2292
2293         return _plugin_selector;
2294 }
2295
2296 void
2297 Mixer_UI::setup_track_display ()
2298 {
2299         track_model = ListStore::create (stripable_columns);
2300         track_display.set_model (track_model);
2301         track_display.append_column (_("Show"), stripable_columns.visible);
2302         track_display.append_column (_("Strips"), stripable_columns.text);
2303         track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
2304         track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
2305         track_display.get_column (0)->set_expand(false);
2306         track_display.get_column (1)->set_expand(true);
2307         track_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
2308         track_display.set_name (X_("EditGroupList"));
2309         track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
2310         track_display.set_reorderable (true);
2311         track_display.set_headers_visible (true);
2312         track_display.set_can_focus(false);
2313
2314         track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
2315         track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
2316
2317         CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (0));
2318         track_list_visible_cell->property_activatable() = true;
2319         track_list_visible_cell->property_radio() = false;
2320         track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
2321
2322         track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
2323
2324         track_display_scroller.add (track_display);
2325         track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
2326
2327         VBox* v = manage (new VBox);
2328         v->show ();
2329         v->pack_start (track_display_scroller, true, true);
2330
2331         Button* b = manage (new Button);
2332         b->show ();
2333         Widget* w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
2334         w->show ();
2335         b->add (*w);
2336
2337         b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
2338
2339         v->pack_start (*b, false, false);
2340
2341         track_display_frame.set_name("BaseFrame");
2342         track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
2343         track_display_frame.add (*v);
2344
2345         track_display_scroller.show();
2346         track_display_frame.show();
2347         track_display.show();
2348 }
2349
2350 void
2351 Mixer_UI::new_track_or_bus ()
2352 {
2353         ARDOUR_UI::instance()->add_route ();
2354 }
2355
2356 void
2357 Mixer_UI::update_title ()
2358 {
2359         if (!own_window()) {
2360                 return;
2361         }
2362
2363         if (_session) {
2364                 string n;
2365
2366                 if (_session->snap_name() != _session->name()) {
2367                         n = _session->snap_name ();
2368                 } else {
2369                         n = _session->name ();
2370                 }
2371
2372                 if (_session->dirty ()) {
2373                         n = "*" + n;
2374                 }
2375
2376                 WindowTitle title (n);
2377                 title += S_("Window|Mixer");
2378                 title += Glib::get_application_name ();
2379                 own_window()->set_title (title.get_string());
2380
2381         } else {
2382
2383                 WindowTitle title (S_("Window|Mixer"));
2384                 title += Glib::get_application_name ();
2385                 own_window()->set_title (title.get_string());
2386         }
2387 }
2388
2389 MixerStrip*
2390 Mixer_UI::strip_by_x (int x)
2391 {
2392         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2393                 int x1, x2, y;
2394
2395                 (*i)->translate_coordinates (_content, 0, 0, x1, y);
2396                 x2 = x1 + (*i)->get_width();
2397
2398                 if (x >= x1 && x <= x2) {
2399                         return (*i);
2400                 }
2401         }
2402
2403         return 0;
2404 }
2405
2406 void
2407 Mixer_UI::set_axis_targets_for_operation ()
2408 {
2409         _axis_targets.clear ();
2410
2411         if (!_selection.empty()) {
2412                 _axis_targets = _selection.axes;
2413                 return;
2414         }
2415
2416 //  removed "implicit" selections of strips, after discussion on IRC
2417
2418 }
2419
2420 void
2421 Mixer_UI::monitor_section_going_away ()
2422 {
2423         if (_monitor_section) {
2424                 monitor_section_detached ();
2425                 out_packer.remove (_monitor_section->tearoff());
2426                 _monitor_section->set_session (0);
2427                 delete _monitor_section;
2428                 _monitor_section = 0;
2429         }
2430 }
2431
2432 void
2433 Mixer_UI::toggle_midi_input_active (bool flip_others)
2434 {
2435         boost::shared_ptr<RouteList> rl (new RouteList);
2436         bool onoff = false;
2437
2438         set_axis_targets_for_operation ();
2439
2440         for (AxisViewSelection::iterator r = _axis_targets.begin(); r != _axis_targets.end(); ++r) {
2441                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> ((*r)->stripable());
2442
2443                 if (mt) {
2444                         rl->push_back (mt);
2445                         onoff = !mt->input_active();
2446                 }
2447         }
2448
2449         _session->set_exclusive_input_active (rl, onoff, flip_others);
2450 }
2451
2452 void
2453 Mixer_UI::maximise_mixer_space ()
2454 {
2455         if (!own_window()) {
2456                 return;
2457         }
2458
2459         if (_maximised) {
2460                 return;
2461         }
2462
2463         _window->fullscreen ();
2464         _maximised = true;
2465 }
2466
2467 void
2468 Mixer_UI::restore_mixer_space ()
2469 {
2470         if (!own_window()) {
2471                 return;
2472         }
2473
2474         if (!_maximised) {
2475                 return;
2476         }
2477
2478         own_window()->unfullscreen();
2479         _maximised = false;
2480 }
2481
2482 void
2483 Mixer_UI::monitor_section_attached ()
2484 {
2485         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2486         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2487         act->set_sensitive (true);
2488         tact->set_active ();
2489 }
2490
2491 void
2492 Mixer_UI::monitor_section_detached ()
2493 {
2494         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2495         act->set_sensitive (false);
2496 }
2497
2498 void
2499 Mixer_UI::store_current_favorite_order ()
2500 {
2501         typedef Gtk::TreeModel::Children type_children;
2502         type_children children = favorite_plugins_model->children();
2503         favorite_order.clear();
2504         for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
2505         {
2506                 Gtk::TreeModel::Row row = *iter;
2507                 ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2508                 favorite_order.push_back (ppp->_pip);
2509                 std::string name = row[favorite_plugins_columns.name];
2510                 favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2511         }
2512 }
2513
2514 void
2515 Mixer_UI::save_favorite_ui_state (const TreeModel::iterator& iter, const TreeModel::Path& path)
2516 {
2517         Gtk::TreeModel::Row row = *iter;
2518         ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2519         assert (ppp);
2520         favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2521 }
2522
2523 void
2524 Mixer_UI::refiller (PluginInfoList& result, const PluginInfoList& plugs)
2525 {
2526         PluginManager& manager (PluginManager::instance());
2527         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
2528                 if (manager.get_status (*i) != PluginManager::Favorite) {
2529                         continue;
2530                 }
2531                 result.push_back (*i);
2532         }
2533 }
2534
2535 struct PluginCustomSorter {
2536 public:
2537         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2538                 PluginInfoList::const_iterator aiter = _user.begin();
2539                 PluginInfoList::const_iterator biter = _user.begin();
2540                 while (aiter != _user.end()) { if ((*aiter)->unique_id == a->unique_id) { break; } ++aiter; }
2541                 while (biter != _user.end()) { if ((*biter)->unique_id == b->unique_id) { break; } ++biter; }
2542
2543                 if (aiter != _user.end() && biter != _user.end()) {
2544                         return std::distance (_user.begin(), aiter) < std::distance (_user.begin(), biter);
2545                 }
2546                 if (aiter != _user.end()) {
2547                         return true;
2548                 }
2549                 if (biter != _user.end()) {
2550                         return false;
2551                 }
2552                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2553         }
2554         PluginCustomSorter(PluginInfoList user) : _user (user)  {}
2555 private:
2556         PluginInfoList _user;
2557 };
2558
2559 void
2560 Mixer_UI::refill_favorite_plugins ()
2561 {
2562         PluginInfoList plugs;
2563         PluginManager& mgr (PluginManager::instance());
2564
2565 #ifdef LV2_SUPPORT
2566         refiller (plugs, mgr.lv2_plugin_info ());
2567 #endif
2568 #ifdef WINDOWS_VST_SUPPORT
2569         refiller (plugs, mgr.windows_vst_plugin_info ());
2570 #endif
2571 #ifdef LXVST_SUPPORT
2572         refiller (plugs, mgr.lxvst_plugin_info ());
2573 #endif
2574 #ifdef MACVST_SUPPORT
2575         refiller (plugs, mgr.mac_vst_plugin_info ());
2576 #endif
2577 #ifdef AUDIOUNIT_SUPPORT
2578         refiller (plugs, mgr.au_plugin_info ());
2579 #endif
2580         refiller (plugs, mgr.ladspa_plugin_info ());
2581         refiller (plugs, mgr.lua_plugin_info ());
2582
2583         store_current_favorite_order ();
2584
2585         PluginCustomSorter cmp (favorite_order);
2586         plugs.sort (cmp);
2587
2588         favorite_order = plugs;
2589
2590         sync_treeview_from_favorite_order ();
2591 }
2592
2593 void
2594 Mixer_UI::sync_treeview_favorite_ui_state (const TreeModel::Path& path, const TreeModel::iterator&)
2595 {
2596         TreeIter iter;
2597         if (!(iter = favorite_plugins_model->get_iter (path))) {
2598                 return;
2599         }
2600         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2601         if (!ppp) {
2602                 return;
2603         }
2604         PluginInfoPtr pip = ppp->_pip;
2605         if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2606                 if (favorite_ui_state[pip->unique_id]) {
2607                         favorite_plugins_display.expand_row (path, true);
2608                 }
2609         }
2610 }
2611
2612 void
2613 Mixer_UI::sync_treeview_from_favorite_order ()
2614 {
2615         favorite_plugins_model->clear ();
2616         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i) {
2617                 PluginInfoPtr pip = (*i);
2618
2619                 TreeModel::Row newrow = *(favorite_plugins_model->append());
2620                 newrow[favorite_plugins_columns.name] = (*i)->name;
2621                 newrow[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip));
2622                 if (!_session) {
2623                         continue;
2624                 }
2625
2626                 vector<ARDOUR::Plugin::PresetRecord> presets = (*i)->get_presets (true);
2627                 for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator j = presets.begin(); j != presets.end(); ++j) {
2628                         Gtk::TreeModel::Row child_row = *(favorite_plugins_model->append (newrow.children()));
2629                         child_row[favorite_plugins_columns.name] = (*j).label;
2630                         child_row[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip, &(*j)));
2631                 }
2632                 if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2633                         if (favorite_ui_state[pip->unique_id]) {
2634                                 favorite_plugins_display.expand_row (favorite_plugins_model->get_path(newrow), true);
2635                         }
2636                 }
2637         }
2638 }
2639
2640 void
2641 Mixer_UI::popup_note_context_menu (GdkEventButton *ev)
2642 {
2643         using namespace Gtk::Menu_Helpers;
2644
2645         Gtk::Menu* m = manage (new Menu);
2646         MenuList& items = m->items ();
2647
2648         if (_selection.axes.empty()) {
2649                 items.push_back (MenuElem (_("No Track/Bus is selected.")));
2650         } else {
2651                 items.push_back (MenuElem (_("Add at the top"),
2652                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddTop)));
2653                 items.push_back (MenuElem (_("Add Pre-Fader"),
2654                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPreFader)));
2655                 items.push_back (MenuElem (_("Add Post-Fader"),
2656                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPostFader)));
2657                 items.push_back (MenuElem (_("Add at the end"),
2658                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddBottom)));
2659         }
2660
2661         items.push_back (SeparatorElem());
2662
2663         items.push_back (MenuElem (_("Remove from favorites"), sigc::mem_fun (*this, &Mixer_UI::remove_selected_from_favorites)));
2664
2665         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2666         if (ppp && ppp->_preset.valid && ppp->_preset.user) {
2667                 // we cannot currently delete AU presets
2668                 if (!ppp->_pip || ppp->_pip->type != AudioUnit) {
2669                         items.push_back (MenuElem (_("Delete Preset"), sigc::mem_fun (*this, &Mixer_UI::delete_selected_preset)));
2670                 }
2671         }
2672
2673         m->popup (ev->button, ev->time);
2674 }
2675
2676 bool
2677 Mixer_UI::plugin_row_button_press (GdkEventButton *ev)
2678 {
2679         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 3) ) {
2680                 TreeModel::Path path;
2681                 TreeViewColumn* column;
2682                 int cellx, celly;
2683                 if (favorite_plugins_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
2684                         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2685                         if (selection) {
2686                                 selection->unselect_all();
2687                                 selection->select(path);
2688                         }
2689                 }
2690                 ARDOUR::PluginPresetPtr ppp = selected_plugin();
2691                 if (ppp) {
2692                         popup_note_context_menu (ev);
2693                 }
2694         }
2695         return false;
2696 }
2697
2698
2699 PluginPresetPtr
2700 Mixer_UI::selected_plugin ()
2701 {
2702         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2703         if (!selection) {
2704                 return PluginPresetPtr();
2705         }
2706         Gtk::TreeModel::iterator iter = selection->get_selected();
2707         if (!iter) {
2708                 return PluginPresetPtr();
2709         }
2710         return (*iter)[favorite_plugins_columns.plugin];
2711 }
2712
2713 void
2714 Mixer_UI::add_selected_processor (ProcessorPosition pos)
2715 {
2716         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2717         if (ppp) {
2718                 add_favorite_processor (ppp, pos);
2719         }
2720 }
2721
2722 void
2723 Mixer_UI::delete_selected_preset ()
2724 {
2725         if (!_session) {
2726                 return;
2727         }
2728         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2729         if (!ppp || !ppp->_preset.valid || !ppp->_preset.user) {
2730                 return;
2731         }
2732         PluginPtr plugin = ppp->_pip->load (*_session);
2733         plugin->get_presets();
2734         plugin->remove_preset (ppp->_preset.label);
2735 }
2736
2737 void
2738 Mixer_UI::remove_selected_from_favorites ()
2739 {
2740         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2741         if (!ppp) {
2742                 return;
2743         }
2744         PluginManager::PluginStatusType status = PluginManager::Normal;
2745         PluginManager& manager (PluginManager::instance());
2746
2747         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2748         manager.save_statuses ();
2749 }
2750
2751 void
2752 Mixer_UI::plugin_row_activated (const TreeModel::Path& path, TreeViewColumn* column)
2753 {
2754         TreeIter iter;
2755         if (!(iter = favorite_plugins_model->get_iter (path))) {
2756                 return;
2757         }
2758         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2759         add_favorite_processor (ppp, AddPreFader); // TODO: preference?!
2760 }
2761
2762 void
2763 Mixer_UI::add_favorite_processor (ARDOUR::PluginPresetPtr ppp, ProcessorPosition pos)
2764 {
2765         if (!_session || _selection.axes.empty()) {
2766                 return;
2767         }
2768
2769         PluginInfoPtr pip = ppp->_pip;
2770         for (AxisViewSelection::iterator i = _selection.axes.begin(); i != _selection.axes.end(); ++i) {
2771                 boost::shared_ptr<ARDOUR::Route> rt = boost::dynamic_pointer_cast<ARDOUR::Route> ((*i)->stripable());
2772
2773                 if (!rt) {
2774                         continue;
2775                 }
2776
2777                 PluginPtr p = pip->load (*_session);
2778
2779                 if (!p) {
2780                         continue;
2781                 }
2782
2783                 if (ppp->_preset.valid) {
2784                         p->load_preset (ppp->_preset);
2785                 }
2786
2787                 Route::ProcessorStreams err;
2788                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
2789
2790                 switch (pos) {
2791                         case AddTop:
2792                                 rt->add_processor_by_index (processor, 0, &err, Config->get_new_plugins_active ());
2793                                 break;
2794                         case AddPreFader:
2795                                 rt->add_processor (processor, PreFader, &err, Config->get_new_plugins_active ());
2796                                 break;
2797                         case AddPostFader:
2798                                 {
2799                                         int idx = 0;
2800                                         int pos = 0;
2801                                         for (;;++idx) {
2802                                                 boost::shared_ptr<Processor> np = rt->nth_processor (idx);
2803                                                 if (!np) {
2804                                                         break;
2805                                                 }
2806                                                 if (!np->display_to_user()) {
2807                                                         continue;
2808                                                 }
2809                                                 if (boost::dynamic_pointer_cast<Amp> (np) && // Fader, not Trim
2810                                                                 boost::dynamic_pointer_cast<Amp> (np)->gain_control()->parameter().type() == GainAutomation) {
2811                                                         break;
2812                                                 }
2813                                                 ++pos;
2814                                         }
2815                                         rt->add_processor_by_index (processor, ++pos, &err, Config->get_new_plugins_active ());
2816                                 }
2817                                 break;
2818                         case AddBottom:
2819                                 rt->add_processor_by_index (processor, -1, &err, Config->get_new_plugins_active ());
2820                                 break;
2821                 }
2822         }
2823 }
2824
2825 bool
2826 PluginTreeStore::row_drop_possible_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& data) const
2827 {
2828         if (data.get_target() != "GTK_TREE_MODEL_ROW") {
2829                 return false;
2830         }
2831
2832         // only allow to re-order top-level items
2833         TreePath src;
2834         if (TreePath::get_from_selection_data (data, src)) {
2835                 if (src.up() && src.up()) {
2836                         return false;
2837                 }
2838         }
2839
2840         // don't allow to drop as child-rows.
2841         Gtk::TreeModel::Path _dest = dest; // un const
2842         const bool is_child = _dest.up (); // explicit bool for clang
2843         if (!is_child || _dest.empty ()) {
2844                 return true;
2845         }
2846         return false;
2847 }
2848
2849 void
2850 Mixer_UI::plugin_drop (const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData& data)
2851 {
2852         if (data.get_target() != "PluginPresetPtr") {
2853                 return;
2854         }
2855         if (data.get_length() != sizeof (PluginPresetPtr)) {
2856                 return;
2857         }
2858         const void *d = data.get_data();
2859         const PluginPresetPtr ppp = *(static_cast<const PluginPresetPtr*> (d));
2860
2861         PluginManager::PluginStatusType status = PluginManager::Favorite;
2862         PluginManager& manager (PluginManager::instance());
2863
2864         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2865         manager.save_statuses ();
2866 }
2867
2868 void
2869 Mixer_UI::do_vca_assign (boost::shared_ptr<VCA> vca)
2870 {
2871         /* call protected MixerActor:: method */
2872         vca_assign (vca);
2873 }
2874
2875 void
2876 Mixer_UI::do_vca_unassign (boost::shared_ptr<VCA> vca)
2877 {
2878         /* call protected MixerActor:: method */
2879         vca_unassign (vca);
2880 }
2881
2882 void
2883 Mixer_UI::show_spill (boost::shared_ptr<Stripable> s)
2884 {
2885         boost::shared_ptr<Stripable> ss = spilled_strip.lock();
2886         if (ss != s) {
2887                 spilled_strip = s;
2888                 show_spill_change (s); /* EMIT SIGNAL */
2889                 if (s) {
2890                         _group_tabs->hide ();
2891                 } else {
2892                         _group_tabs->show ();
2893                 }
2894                 redisplay_track_list ();
2895         }
2896 }
2897
2898 bool
2899 Mixer_UI::showing_spill_for (boost::shared_ptr<Stripable> s) const
2900 {
2901         return s == spilled_strip.lock();
2902 }
2903
2904 void
2905 Mixer_UI::register_actions ()
2906 {
2907         Glib::RefPtr<ActionGroup> group = myactions.create_action_group (X_("Mixer"));
2908
2909         myactions.register_action (group, "solo", _("Toggle Solo on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::solo_action));
2910         myactions.register_action (group, "mute", _("Toggle Mute on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::mute_action));
2911         myactions.register_action (group, "recenable", _("Toggle Rec-enable on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::rec_enable_action));
2912         myactions.register_action (group, "increment-gain", _("Decrease Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_up_action));
2913         myactions.register_action (group, "decrement-gain", _("Increase Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_down_action));
2914         myactions.register_action (group, "unity-gain", _("Set Gain to 0dB on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::unity_gain_action));
2915
2916
2917         myactions.register_action (group, "copy-processors", _("Copy Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::copy_processors));
2918         myactions.register_action (group, "cut-processors", _("Cut Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::cut_processors));
2919         myactions.register_action (group, "paste-processors", _("Paste Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::paste_processors));
2920         myactions.register_action (group, "delete-processors", _("Delete Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::delete_processors));
2921         myactions.register_action (group, "select-all-processors", _("Select All (visible) Processors"), sigc::mem_fun (*this, &Mixer_UI::select_all_processors));
2922         myactions.register_action (group, "toggle-processors", _("Toggle Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::toggle_processors));
2923         myactions.register_action (group, "ab-plugins", _("Toggle Selected Plugins"), sigc::mem_fun (*this, &Mixer_UI::ab_plugins));
2924         myactions.register_action (group, "select-none", _("Deselect all strips and processors"), sigc::mem_fun (*this, &Mixer_UI::select_none));
2925
2926         myactions.register_action (group, "scroll-left", _("Scroll Mixer Window to the left"), sigc::mem_fun (*this, &Mixer_UI::scroll_left));
2927         myactions.register_action (group, "scroll-right", _("Scroll Mixer Window to the right"), sigc::mem_fun (*this, &Mixer_UI::scroll_right));
2928
2929         myactions.register_action (group, "toggle-midi-input-active", _("Toggle MIDI Input Active for Mixer-Selected Tracks/Busses"),
2930                                    sigc::bind (sigc::mem_fun (*this, &Mixer_UI::toggle_midi_input_active), false));
2931 }
2932
2933 void
2934 Mixer_UI::load_bindings ()
2935 {
2936         bindings = Bindings::get_bindings (X_("Mixer"), myactions);
2937 }
2938
2939 template<class T> void
2940 Mixer_UI::control_action (boost::shared_ptr<T> (Stripable::*get_control)() const)
2941 {
2942         boost::shared_ptr<ControlList> cl (new ControlList);
2943         boost::shared_ptr<AutomationControl> ac;
2944         bool val = false;
2945         bool have_val = false;
2946
2947         set_axis_targets_for_operation ();
2948
2949         BOOST_FOREACH(AxisView* r, _axis_targets) {
2950                 boost::shared_ptr<Stripable> s = r->stripable();
2951                 if (s) {
2952                         ac = (s.get()->*get_control)();
2953                         if (ac) {
2954                                 cl->push_back (ac);
2955                                 if (!have_val) {
2956                                         val = !ac->get_value();
2957                                         have_val = true;
2958                                 }
2959                         }
2960                 }
2961         }
2962
2963         _session->set_controls (cl,  val, Controllable::UseGroup);
2964 }
2965
2966 void
2967 Mixer_UI::solo_action ()
2968 {
2969         control_action (&Stripable::solo_control);
2970 }
2971
2972 void
2973 Mixer_UI::mute_action ()
2974 {
2975         control_action (&Stripable::mute_control);
2976 }
2977
2978 void
2979 Mixer_UI::rec_enable_action ()
2980 {
2981         control_action (&Stripable::rec_enable_control);
2982 }
2983
2984 void
2985 Mixer_UI::step_gain_up_action ()
2986 {
2987         set_axis_targets_for_operation ();
2988
2989         BOOST_FOREACH(AxisView* r, _axis_targets) {
2990                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2991                 if (ms) {
2992                         ms->step_gain_up ();
2993                 }
2994         }
2995 }
2996
2997 void
2998 Mixer_UI::step_gain_down_action ()
2999 {
3000         set_axis_targets_for_operation ();
3001
3002         BOOST_FOREACH(AxisView* r, _axis_targets) {
3003                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3004                 if (ms) {
3005                         ms->step_gain_down ();
3006                 }
3007         }
3008 }
3009
3010 void
3011 Mixer_UI::unity_gain_action ()
3012 {
3013         set_axis_targets_for_operation ();
3014
3015         BOOST_FOREACH(AxisView* r, _axis_targets) {
3016                 boost::shared_ptr<Stripable> s = r->stripable();
3017                 if (s) {
3018                         boost::shared_ptr<AutomationControl> ac = s->gain_control();
3019                         if (ac) {
3020                                 ac->set_value (1.0, Controllable::UseGroup);
3021                         }
3022                 }
3023         }
3024 }
3025
3026 void
3027 Mixer_UI::copy_processors ()
3028 {
3029         set_axis_targets_for_operation ();
3030
3031         BOOST_FOREACH(AxisView* r, _axis_targets) {
3032                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3033                 if (ms) {
3034                         ms->copy_processors ();
3035                 }
3036         }
3037 }
3038 void
3039 Mixer_UI::cut_processors ()
3040 {
3041         set_axis_targets_for_operation ();
3042
3043         BOOST_FOREACH(AxisView* r, _axis_targets) {
3044                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3045                 if (ms) {
3046                         ms->cut_processors ();
3047                 }
3048         }
3049 }
3050 void
3051 Mixer_UI::paste_processors ()
3052 {
3053         set_axis_targets_for_operation ();
3054
3055         BOOST_FOREACH(AxisView* r, _axis_targets) {
3056                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3057                 if (ms) {
3058                         ms->paste_processors ();
3059                 }
3060         }
3061 }
3062 void
3063 Mixer_UI::select_all_processors ()
3064 {
3065         set_axis_targets_for_operation ();
3066
3067         BOOST_FOREACH(AxisView* r, _axis_targets) {
3068                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3069                 if (ms) {
3070                         ms->select_all_processors ();
3071                 }
3072         }
3073 }
3074 void
3075 Mixer_UI::toggle_processors ()
3076 {
3077         set_axis_targets_for_operation ();
3078
3079         BOOST_FOREACH(AxisView* r, _axis_targets) {
3080                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3081                 if (ms) {
3082                         ms->toggle_processors ();
3083                 }
3084         }
3085 }
3086 void
3087 Mixer_UI::ab_plugins ()
3088 {
3089         set_axis_targets_for_operation ();
3090
3091         BOOST_FOREACH(AxisView* r, _axis_targets) {
3092                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3093                 if (ms) {
3094                         ms->ab_plugins ();
3095                 }
3096         }
3097 }
3098
3099 void
3100 Mixer_UI::vca_assign (boost::shared_ptr<VCA> vca)
3101 {
3102         set_axis_targets_for_operation ();
3103         BOOST_FOREACH(AxisView* r, _axis_targets) {
3104                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3105                 if (ms) {
3106                         ms->vca_assign (vca);
3107                 }
3108         }
3109 }
3110
3111 void
3112 Mixer_UI::vca_unassign (boost::shared_ptr<VCA> vca)
3113 {
3114         set_axis_targets_for_operation ();
3115         BOOST_FOREACH(AxisView* r, _axis_targets) {
3116                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3117                 if (ms) {
3118                         ms->vca_unassign (vca);
3119                 }
3120         }
3121 }