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