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