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