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