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