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