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