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