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