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