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