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