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