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