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