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