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