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