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