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