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