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