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