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