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