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