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