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