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