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