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