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