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