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