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