static MonitorSection Action -- fixes #7098
[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->pack_start (*_group_tabs, PACK_SHRINK);
141         b->pack_start (strip_packer);
142         b->show_all ();
143
144         scroller.add (*b);
145         scroller.set_policy (Gtk::POLICY_ALWAYS, Gtk::POLICY_AUTOMATIC);
146
147         setup_track_display ();
148
149         group_model = ListStore::create (group_columns);
150         group_display.set_model (group_model);
151         group_display.append_column (_("Show"), group_columns.visible);
152         group_display.append_column (_("Group"), group_columns.text);
153         group_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
154         group_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
155         group_display.get_column (0)->set_expand(false);
156         group_display.get_column (1)->set_expand(true);
157         group_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
158         group_display.set_name ("EditGroupList");
159         group_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
160         group_display.set_reorderable (true);
161         group_display.set_headers_visible (true);
162         group_display.set_rules_hint (true);
163         group_display.set_can_focus(false);
164
165         /* name is directly editable */
166
167         CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
168         name_cell->property_editable() = true;
169         name_cell->signal_edited().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_name_edit));
170
171         /* use checkbox for the active column */
172
173         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*>(group_display.get_column_cell_renderer (0));
174         active_cell->property_activatable() = true;
175         active_cell->property_radio() = false;
176
177         group_model->signal_row_changed().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_change));
178         /* We use this to notice drag-and-drop reorders of the group list */
179         group_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::route_group_row_deleted));
180         group_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::group_display_button_press), false);
181
182         group_display_scroller.add (group_display);
183         group_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
184
185         HBox* route_group_display_button_box = manage (new HBox());
186
187         Button* route_group_add_button = manage (new Button ());
188         Button* route_group_remove_button = manage (new Button ());
189
190         Widget* w;
191
192         w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
193         w->show();
194         route_group_add_button->add (*w);
195
196         w = manage (new Image (Stock::REMOVE, ICON_SIZE_BUTTON));
197         w->show();
198         route_group_remove_button->add (*w);
199
200         route_group_display_button_box->set_homogeneous (true);
201
202         route_group_add_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_route_group));
203         route_group_remove_button->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::remove_selected_route_group));
204
205         route_group_display_button_box->add (*route_group_add_button);
206         route_group_display_button_box->add (*route_group_remove_button);
207
208         group_display_vbox.pack_start (group_display_scroller, true, true);
209         group_display_vbox.pack_start (*route_group_display_button_box, false, false);
210
211         group_display_frame.set_name ("BaseFrame");
212         group_display_frame.set_shadow_type (Gtk::SHADOW_IN);
213         group_display_frame.add (group_display_vbox);
214
215
216         list<TargetEntry> target_list;
217         target_list.push_back (TargetEntry ("PluginPresetPtr"));
218
219         favorite_plugins_model = PluginTreeStore::create (favorite_plugins_columns);
220         favorite_plugins_display.set_model (favorite_plugins_model);
221         favorite_plugins_display.append_column (_("Favorite Plugins"), favorite_plugins_columns.name);
222         favorite_plugins_display.set_name ("EditGroupList");
223         favorite_plugins_display.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
224         favorite_plugins_display.set_reorderable (false);
225         favorite_plugins_display.set_headers_visible (true);
226         favorite_plugins_display.set_rules_hint (true);
227         favorite_plugins_display.set_can_focus (false);
228         favorite_plugins_display.set_tooltip_column (0);
229         favorite_plugins_display.add_object_drag (favorite_plugins_columns.plugin.index(), "PluginFavoritePtr");
230         favorite_plugins_display.set_drag_column (favorite_plugins_columns.name.index());
231         favorite_plugins_display.add_drop_targets (target_list);
232         favorite_plugins_display.signal_row_activated().connect (sigc::mem_fun (*this, &Mixer_UI::plugin_row_activated));
233         favorite_plugins_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::plugin_row_button_press), false);
234         favorite_plugins_display.signal_drop.connect (sigc::mem_fun (*this, &Mixer_UI::plugin_drop));
235         favorite_plugins_display.signal_row_expanded().connect (sigc::mem_fun (*this, &Mixer_UI::save_favorite_ui_state));
236         favorite_plugins_display.signal_row_collapsed().connect (sigc::mem_fun (*this, &Mixer_UI::save_favorite_ui_state));
237         favorite_plugins_model->signal_row_has_child_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::sync_treeview_favorite_ui_state));
238
239         favorite_plugins_scroller.add (favorite_plugins_display);
240         favorite_plugins_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
241
242         favorite_plugins_frame.set_name ("BaseFrame");
243         favorite_plugins_frame.set_shadow_type (Gtk::SHADOW_IN);
244         favorite_plugins_frame.add (favorite_plugins_scroller);
245
246         rhs_pane1.add (favorite_plugins_frame);
247         rhs_pane1.add (track_display_frame);
248
249         rhs_pane2.add (rhs_pane1);
250         rhs_pane2.add (group_display_frame);
251
252         list_vpacker.pack_start (rhs_pane2, true, true);
253
254         string vca_text = _("Control Masters");
255         Gtk::HBox* vca_top_padding = manage (new Gtk::HBox);
256         vca_top_padding->set_size_request (-1, 2);
257         vca_vpacker.pack_start (*vca_top_padding, false, false);
258
259         vca_label.set_text (vca_text);
260         vca_label_bar.set_size_request (-1, 16); /* must match height in GroupTabs::set_size_request() */
261
262         vca_label_bar.set_name (X_("VCALabelBar"));
263         vca_label_bar.add (vca_label);
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_top_padding->show ();
352         vca_scroller.show();
353         vca_vpacker.show();
354         vca_hpacker.show();
355         vca_label_bar.show();
356         vca_label.show();
357         vca_scroller_base.show();
358         out_packer.show();
359         list_hpane.show();
360         group_display.show();
361         favorite_plugins_display.show();
362
363         MixerStrip::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_strip, this, _1), gui_context());
364
365         /* handle escape */
366
367         ARDOUR_UI::instance()->Escape.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::escape, this), gui_context());
368
369 #ifndef DEFER_PLUGIN_SELECTOR_LOAD
370         _plugin_selector = new PluginSelector (PluginManager::instance ());
371 #else
372 #error implement deferred Plugin-Favorite list
373 #endif
374         PluginManager::instance ().PluginListChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
375         PluginManager::instance ().PluginStatusesChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
376         ARDOUR::Plugin::PresetsChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::refill_favorite_plugins, this), gui_context());
377 }
378
379 Mixer_UI::~Mixer_UI ()
380 {
381         if (_monitor_section) {
382                 monitor_section_detached ();
383                 delete _monitor_section;
384         }
385         delete _plugin_selector;
386 }
387
388 void
389 Mixer_UI::escape ()
390 {
391         select_none ();
392 }
393
394 void
395 Mixer_UI::track_editor_selection ()
396 {
397         PublicEditor::instance().get_selection().TracksChanged.connect (sigc::mem_fun (*this, &Mixer_UI::follow_editor_selection));
398 }
399
400 Gtk::Window*
401 Mixer_UI::use_own_window (bool and_fill_it)
402 {
403         bool new_window = !own_window();
404
405         Gtk::Window* win = Tabbable::use_own_window (and_fill_it);
406
407
408         if (win && new_window) {
409                 win->set_name ("MixerWindow");
410                 ARDOUR_UI::instance()->setup_toplevel_window (*win, _("Mixer"), this);
411                 win->signal_scroll_event().connect (sigc::mem_fun (*this, &Mixer_UI::on_scroll_event), false);
412                 win->signal_event().connect (sigc::bind (sigc::ptr_fun (&Keyboard::catch_user_event_for_pre_dialog_focus), win));
413                 win->set_data ("ardour-bindings", bindings);
414                 update_title ();
415         }
416
417         return win;
418 }
419
420 void
421 Mixer_UI::show_window ()
422 {
423         Tabbable::show_window ();
424
425         /* show/hide group tabs as required */
426         parameter_changed ("show-group-tabs");
427
428         /* now reset each strips width so the right widgets are shown */
429
430         TreeModel::Children rows = track_model->children();
431         TreeModel::Children::iterator ri;
432
433         for (ri = rows.begin(); ri != rows.end(); ++ri) {
434                 AxisView* av = (*ri)[stripable_columns.strip];
435                 MixerStrip* ms = dynamic_cast<MixerStrip*> (av);
436                 if (!ms) {
437                         continue;
438                 }
439                 ms->set_width_enum (ms->get_width_enum (), ms->width_owner());
440                 /* Fix visibility of mixer strip stuff */
441                 ms->parameter_changed (X_("mixer-element-visibility"));
442         }
443
444         /* force focus into main area */
445         scroller_base.grab_focus ();
446 }
447
448 void
449 Mixer_UI::remove_master (VCAMasterStrip* vms)
450 {
451         if (_session && _session->deletion_in_progress()) {
452                 /* its all being taken care of */
453                 return;
454         }
455
456         TreeModel::Children rows = track_model->children();
457         TreeModel::Children::iterator ri;
458
459         for (ri = rows.begin(); ri != rows.end(); ++ri) {
460                 if ((*ri)[stripable_columns.strip] == vms) {
461                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
462                         track_model->erase (ri);
463                         break;
464                 }
465         }
466 }
467
468 bool
469 Mixer_UI::masters_scroller_button_release (GdkEventButton* ev)
470 {
471         using namespace Menu_Helpers;
472
473         if (Keyboard::is_context_menu_event (ev)) {
474                 ARDOUR_UI::instance()->add_route ();
475                 return true;
476         }
477
478         return false;
479 }
480
481 void
482 Mixer_UI::add_masters (VCAList& vlist)
483 {
484         StripableList sl;
485
486         for (VCAList::iterator v = vlist.begin(); v != vlist.end(); ++v) {
487                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
488         }
489
490         add_stripables (sl);
491 }
492
493 void
494 Mixer_UI::add_routes (RouteList& rlist)
495 {
496         StripableList sl;
497
498         for (RouteList::iterator r = rlist.begin(); r != rlist.end(); ++r) {
499                 sl.push_back (*r);
500         }
501
502         add_stripables (sl);
503 }
504
505 void
506 Mixer_UI::add_stripables (StripableList& slist)
507 {
508         Gtk::TreeModel::Children::iterator insert_iter = track_model->children().end();
509         bool from_scratch = (track_model->children().size() == 0);
510         uint32_t nroutes = 0;
511
512         slist.sort (StripablePresentationInfoSorter());
513
514         for (Gtk::TreeModel::Children::iterator it = track_model->children().begin(); it != track_model->children().end(); ++it) {
515                 boost::shared_ptr<Stripable> s = (*it)[stripable_columns.stripable];
516
517                 if (!s) {
518                         continue;
519                 }
520
521                 nroutes++;
522
523                 if (s->presentation_info().order() == (slist.front()->presentation_info().order() + slist.size())) {
524                         insert_iter = it;
525                         break;
526                 }
527         }
528
529         MixerStrip* strip;
530
531
532         try {
533                 PBD::Unwinder<bool> uw (no_track_list_redisplay, true);
534
535                 track_display.set_model (Glib::RefPtr<ListStore>());
536
537                 for (StripableList::iterator s = slist.begin(); s != slist.end(); ++s) {
538
539                         boost::shared_ptr<Route> route;
540                         boost::shared_ptr<VCA> vca;
541
542                         if ((vca  = boost::dynamic_pointer_cast<VCA> (*s))) {
543
544                                 VCAMasterStrip* vms = new VCAMasterStrip (_session, vca);
545
546                                 TreeModel::Row row = *(track_model->append());
547
548                                 row[stripable_columns.text] = vca->name();
549                                 row[stripable_columns.visible] = vms->marked_for_display ();
550                                 row[stripable_columns.strip] = vms;
551                                 row[stripable_columns.stripable] = vca;
552
553                                 vms->CatchDeletion.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::remove_master, this, _1), gui_context());
554
555                         } else if ((route = boost::dynamic_pointer_cast<Route> (*s))) {
556
557                                 if (route->is_auditioner()) {
558                                         continue;
559                                 }
560
561                                 if (route->is_monitor()) {
562
563                                         if (!_monitor_section) {
564                                                 _monitor_section = new MonitorSection (_session);
565
566                                                 XMLNode* mnode = ARDOUR_UI::instance()->tearoff_settings (X_("monitor-section"));
567                                                 if (mnode) {
568                                                         _monitor_section->tearoff().set_state (*mnode);
569                                                 }
570                                         }
571
572                                         out_packer.pack_end (_monitor_section->tearoff(), false, false);
573                                         _monitor_section->set_session (_session);
574                                         _monitor_section->tearoff().show_all ();
575
576                                         _monitor_section->tearoff().Detach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_detached));
577                                         _monitor_section->tearoff().Attach.connect (sigc::mem_fun(*this, &Mixer_UI::monitor_section_attached));
578
579                                         monitor_section_attached ();
580
581                                         route->DropReferences.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::monitor_section_going_away, this), gui_context());
582
583                                         /* no regular strip shown for control out */
584
585                                         continue;
586                                 }
587
588                                 strip = new MixerStrip (*this, _session, route);
589                                 strips.push_back (strip);
590
591                                 UIConfiguration::instance().get_default_narrow_ms() ? _strip_width = Narrow : _strip_width = Wide;
592
593                                 if (strip->width_owner() != strip) {
594                                         strip->set_width_enum (_strip_width, this);
595                                 }
596
597                                 show_strip (strip);
598
599                                 if (!route->is_master()) {
600
601                                         TreeModel::Row row = *(track_model->insert (insert_iter));
602
603                                         row[stripable_columns.text] = route->name();
604                                         row[stripable_columns.visible] = strip->marked_for_display();
605                                         row[stripable_columns.stripable] = route;
606                                         row[stripable_columns.strip] = strip;
607
608                                         if (nroutes != 0) {
609                                                 _selection.add (strip);
610                                         }
611
612                                 } else {
613
614                                         out_packer.pack_start (*strip, false, false);
615                                         strip->set_packed (true);
616                                 }
617
618                                 strip->WidthChanged.connect (sigc::mem_fun(*this, &Mixer_UI::strip_width_changed));
619                                 strip->signal_button_release_event().connect (sigc::bind (sigc::mem_fun(*this, &Mixer_UI::strip_button_release_event), strip));
620                         }
621
622                         (*s)->presentation_info().PropertyChanged.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::stripable_property_changed, this, _1, boost::weak_ptr<Stripable>(*s)), gui_context());
623                         (*s)->PropertyChanged.connect (*this, invalidator(*this), boost::bind (&Mixer_UI::stripable_property_changed, this, _1, boost::weak_ptr<Stripable>(*s)), gui_context());
624                                                 }
625
626         } catch (const std::exception& e) {
627                 error << string_compose (_("Error adding GUI elements for new tracks/busses %1"), e.what()) << endmsg;
628         }
629
630         track_display.set_model (track_model);
631
632         if (!from_scratch) {
633                 sync_presentation_info_from_treeview ();
634         }
635
636         redisplay_track_list ();
637 }
638
639 void
640 Mixer_UI::deselect_all_strip_processors ()
641 {
642         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
643                 (*i)->deselect_all_processors();
644         }
645 }
646
647 void
648 Mixer_UI::select_strip (MixerStrip& ms, bool add)
649 {
650         if (add) {
651                 _selection.add (&ms);
652         } else {
653                 _selection.set (&ms);
654         }
655 }
656
657 void
658 Mixer_UI::select_none ()
659 {
660         _selection.clear_routes();
661         deselect_all_strip_processors();
662 }
663
664 void
665 Mixer_UI::delete_processors ()
666 {
667         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
668                 (*i)->delete_processors();
669         }
670 }
671
672
673 void
674 Mixer_UI::remove_strip (MixerStrip* strip)
675 {
676         if (_session && _session->deletion_in_progress()) {
677                 /* its all being taken care of */
678                 return;
679         }
680
681         TreeModel::Children rows = track_model->children();
682         TreeModel::Children::iterator ri;
683         list<MixerStrip *>::iterator i;
684
685         if ((i = find (strips.begin(), strips.end(), strip)) != strips.end()) {
686                 strips.erase (i);
687         }
688
689         for (ri = rows.begin(); ri != rows.end(); ++ri) {
690                 if ((*ri)[stripable_columns.strip] == strip) {
691                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
692                         track_model->erase (ri);
693                         break;
694                 }
695         }
696 }
697
698 void
699 Mixer_UI::sync_presentation_info_from_treeview ()
700 {
701         if (ignore_reorder || !_session || _session->deletion_in_progress()) {
702                 return;
703         }
704
705         TreeModel::Children rows = track_model->children();
706
707         if (rows.empty()) {
708                 return;
709         }
710
711         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync presentation info from treeview\n");
712
713         TreeModel::Children::iterator ri;
714         bool change = false;
715         uint32_t order = 0;
716
717         // special case master if it's got PI order 0 lets keep it there
718         if (_session->master_out() && (_session->master_out()->presentation_info().order() == 0)) {
719                 order++;
720         }
721
722         for (ri = rows.begin(); ri != rows.end(); ++ri) {
723                 bool visible = (*ri)[stripable_columns.visible];
724                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
725
726                 if (!stripable) {
727                         continue;
728                 }
729
730                 /* Monitor and Auditioner do not get their presentation
731                  * info reset here.
732                  */
733
734                 if (stripable->is_monitor() || stripable->is_auditioner()) {
735                         continue;
736                 }
737
738                 /* Master also doesn't get set here but since the editor allows
739                  * it to be reordered, we need to preserve its ordering.
740                  */
741
742                 stripable->presentation_info().set_hidden (!visible);
743
744                 // master may not get set here, but if it is zero keep it there
745                 if (stripable->is_master() && (stripable->presentation_info().order() == 0)) {
746                         continue;
747                 }
748
749                 if (order != stripable->presentation_info().order()) {
750                         stripable->set_presentation_order (order, false);
751                         change = true;
752                 }
753
754                 ++order;
755         }
756
757         if (change) {
758                 DEBUG_TRACE (DEBUG::OrderKeys, "... notify PI change from mixer GUI\n");
759                 _session->notify_presentation_info_change ();
760                 _session->set_dirty();
761         }
762 }
763
764 void
765 Mixer_UI::sync_treeview_from_presentation_info ()
766 {
767         if (!_session || _session->deletion_in_progress()) {
768                 return;
769         }
770
771         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync model from presentation info.\n");
772
773         /* we could get here after either a change in the Mixer or Editor sort
774          * order, but either way, the mixer order keys reflect the intended
775          * order for the GUI, so reorder the treeview model to match it.
776          */
777
778         vector<int> neworder;
779         TreeModel::Children rows = track_model->children();
780         uint32_t old_order = 0;
781         bool changed = false;
782
783         if (rows.empty()) {
784                 return;
785         }
786
787         OrderingKeys sorted;
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->presentation_info().order()));
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
1284         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
1285
1286                 AxisView* av = (*i)[stripable_columns.strip];
1287                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1288
1289                 if (!strip) {
1290                         /* we're in the middle of changing a row, don't worry */
1291                         continue;
1292                 }
1293
1294                 if (!strip->route()) {
1295                         /* non-route element */
1296                         continue;
1297                 }
1298
1299                 if (strip->route()->is_master() || strip->route()->is_monitor()) {
1300                         continue;
1301                 }
1302
1303                 if (strip->route()->slaved_to (vca)) {
1304
1305                         if (strip->packed()) {
1306                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1307                         } else {
1308                                 strip_packer.pack_start (*strip, false, false);
1309                                 strip->set_packed (true);
1310                         }
1311
1312                 } else {
1313
1314                         if (strip->packed()) {
1315                                 strip_packer.remove (*strip);
1316                                 strip->set_packed (false);
1317                         }
1318                 }
1319         }
1320 }
1321
1322 void
1323 Mixer_UI::redisplay_track_list ()
1324 {
1325         if (no_track_list_redisplay) {
1326                 return;
1327         }
1328
1329         boost::shared_ptr<VCA> sv = spilled_vca.lock ();
1330
1331         if (sv) {
1332                 spill_redisplay (sv);
1333                 return;
1334         }
1335
1336         TreeModel::Children rows = track_model->children();
1337         TreeModel::Children::iterator i;
1338         uint32_t n_masters = 0;
1339
1340         container_clear (vca_hpacker);
1341         vca_hpacker.pack_end (vca_scroller_base, true, true);
1342
1343         for (i = rows.begin(); i != rows.end(); ++i) {
1344
1345                 AxisView* s = (*i)[stripable_columns.strip];
1346                 bool const visible = (*i)[stripable_columns.visible];
1347                 boost::shared_ptr<Stripable> stripable = (*i)[stripable_columns.stripable];
1348
1349                 if (!s) {
1350                         /* we're in the middle of changing a row, don't worry */
1351                         continue;
1352                 }
1353
1354                 VCAMasterStrip* vms;
1355
1356                 if ((vms = dynamic_cast<VCAMasterStrip*> (s))) {
1357                         if (visible) {
1358                                 vca_hpacker.pack_start (*vms, false, false);
1359                                 vms->show ();
1360                                 n_masters++;
1361                         }
1362                         continue;
1363                 }
1364
1365                 MixerStrip* strip = dynamic_cast<MixerStrip*> (s);
1366
1367                 if (!strip) {
1368                         continue;
1369                 }
1370
1371                 if (visible) {
1372
1373                         if (strip->packed()) {
1374                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1375                         } else {
1376                                 strip_packer.pack_start (*strip, false, false);
1377                                 strip->set_packed (true);
1378                         }
1379
1380                 } else {
1381
1382                         if (stripable->is_master() || stripable->is_monitor()) {
1383                                 /* do nothing, these cannot be hidden */
1384                         } else {
1385                                 if (strip->packed()) {
1386                                         strip_packer.remove (*strip);
1387                                         strip->set_packed (false);
1388                                 }
1389                         }
1390                 }
1391         }
1392
1393         /* update visibility of VCA assign buttons */
1394
1395         if (n_masters == 0) {
1396                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::remove_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1397                 vca_vpacker.hide ();
1398         } else {
1399                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::add_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1400                 vca_vpacker.show ();
1401         }
1402
1403         _group_tabs->set_dirty ();
1404 }
1405
1406 void
1407 Mixer_UI::strip_width_changed ()
1408 {
1409         _group_tabs->set_dirty ();
1410
1411 #ifdef __APPLE__
1412         TreeModel::Children rows = track_model->children();
1413         TreeModel::Children::iterator i;
1414         long order;
1415
1416         for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
1417                 AxisView* av = (*i)[stripable_columns.strip];
1418                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1419
1420                 if (strip == 0) {
1421                         continue;
1422                 }
1423
1424                 bool visible = (*i)[stripable_columns.visible];
1425
1426                 if (visible) {
1427                         strip->queue_draw();
1428                 }
1429         }
1430 #endif
1431
1432 }
1433
1434 struct PresentationInfoMixerSorter
1435 {
1436         bool operator() (boost::shared_ptr<Stripable> a, boost::shared_ptr<Stripable> b) {
1437                 if (a->is_master()) {
1438                         /* master after everything else */
1439                         return false;
1440                 } else if (b->is_master()) {
1441                         /* everything else before master */
1442                         return true;
1443                 }
1444                 return a->presentation_info().order () < b->presentation_info().order ();
1445         }
1446 };
1447
1448 void
1449 Mixer_UI::initial_track_display ()
1450 {
1451         StripableList sl;
1452
1453         boost::shared_ptr<RouteList> routes = _session->get_routes();
1454
1455         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
1456                 sl.push_back (*r);
1457         }
1458
1459         VCAList vcas = _session->vca_manager().vcas();
1460
1461         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
1462                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
1463         }
1464
1465         sl.sort (PresentationInfoMixerSorter());
1466
1467         {
1468                 /* These are also used inside ::add_stripables() but we need
1469                  *  them here because we're going to clear the track_model also.
1470                  */
1471                 Unwinder<bool> uw1 (no_track_list_redisplay, true);
1472                 Unwinder<bool> uw2 (ignore_reorder, true);
1473
1474                 track_model->clear ();
1475                 add_stripables (sl);
1476         }
1477
1478         redisplay_track_list ();
1479 }
1480
1481 void
1482 Mixer_UI::show_track_list_menu ()
1483 {
1484         if (track_menu == 0) {
1485                 build_track_menu ();
1486         }
1487
1488         track_menu->popup (1, gtk_get_current_event_time());
1489 }
1490
1491 bool
1492 Mixer_UI::track_display_button_press (GdkEventButton* ev)
1493 {
1494         if (Keyboard::is_context_menu_event (ev)) {
1495                 show_track_list_menu ();
1496                 return true;
1497         }
1498         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 1)) {
1499                 TreeModel::Path path;
1500                 TreeViewColumn* column;
1501                 int cellx, celly;
1502                 if (track_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1503                         TreeIter iter = track_model->get_iter (path);
1504                         if ((*iter)[stripable_columns.visible]) {
1505                                 boost::shared_ptr<ARDOUR::Stripable> s = (*iter)[stripable_columns.stripable];
1506                                 move_stripable_into_view (s);
1507                         }
1508                 }
1509         }
1510
1511         return false;
1512 }
1513
1514 void
1515 Mixer_UI::move_stripable_into_view (boost::shared_ptr<ARDOUR::Stripable> s)
1516 {
1517         if (!scroller.get_hscrollbar()) {
1518                 return;
1519         }
1520         bool found = false;
1521         int x0 = 0;
1522         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
1523                 if ((*i)->route() == s) {
1524                         int y;
1525                         found = true;
1526                         (*i)->translate_coordinates (strip_packer, 0, 0, x0, y);
1527                         break;
1528                 }
1529         }
1530         if (!found) {
1531                 return;
1532         }
1533
1534         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1535         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), x0 - 1.0)));
1536 }
1537
1538 void
1539 Mixer_UI::build_track_menu ()
1540 {
1541         using namespace Menu_Helpers;
1542         using namespace Gtk;
1543
1544         track_menu = new Menu;
1545         track_menu->set_name ("ArdourContextMenu");
1546         MenuList& items = track_menu->items();
1547
1548         items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
1549         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
1550         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
1551         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
1552         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
1553         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
1554         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::show_all_miditracks)));
1555         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::hide_all_miditracks)));
1556
1557 }
1558
1559 void
1560 Mixer_UI::stripable_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> ws)
1561 {
1562         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
1563                 return;
1564         }
1565
1566         boost::shared_ptr<Stripable> s = ws.lock ();
1567
1568         if (!s) {
1569                 return;
1570         }
1571
1572         TreeModel::Children rows = track_model->children();
1573         TreeModel::Children::iterator i;
1574
1575         for (i = rows.begin(); i != rows.end(); ++i) {
1576                 boost::shared_ptr<Stripable> ss = (*i)[stripable_columns.stripable];
1577
1578                 if (s == ss) {
1579
1580                         if (what_changed.contains (ARDOUR::Properties::name)) {
1581                                 (*i)[stripable_columns.text] = s->name();
1582                         }
1583
1584                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
1585                                 (*i)[stripable_columns.visible] = !s->presentation_info().hidden();
1586                                 redisplay_track_list ();
1587                         }
1588
1589                         return;
1590                 }
1591         }
1592
1593         error << _("track display list item for renamed strip not found!") << endmsg;
1594 }
1595
1596 bool
1597 Mixer_UI::group_display_button_press (GdkEventButton* ev)
1598 {
1599         TreeModel::Path path;
1600         TreeViewColumn* column;
1601         int cellx;
1602         int celly;
1603
1604         if (!group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1605                 _group_tabs->get_menu(0)->popup (1, ev->time);
1606                 return true;
1607         }
1608
1609         TreeIter iter = group_model->get_iter (path);
1610         if (!iter) {
1611                 _group_tabs->get_menu(0)->popup (1, ev->time);
1612                 return true;
1613         }
1614
1615         RouteGroup* group = (*iter)[group_columns.group];
1616
1617         if (Keyboard::is_context_menu_event (ev)) {
1618                 _group_tabs->get_menu(group)->popup (1, ev->time);
1619                 return true;
1620         }
1621
1622         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
1623         case 1:
1624                 if (Keyboard::is_edit_event (ev)) {
1625                         if (group) {
1626                                 // edit_route_group (group);
1627 #ifdef __APPLE__
1628                                 group_display.queue_draw();
1629 #endif
1630                                 return true;
1631                         }
1632                 }
1633                 break;
1634
1635         case 0:
1636         {
1637                 bool visible = (*iter)[group_columns.visible];
1638                 (*iter)[group_columns.visible] = !visible;
1639 #ifdef __APPLE__
1640                 group_display.queue_draw();
1641 #endif
1642                 return true;
1643         }
1644
1645         default:
1646                 break;
1647         }
1648
1649         return false;
1650  }
1651
1652 void
1653 Mixer_UI::activate_all_route_groups ()
1654 {
1655         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
1656 }
1657
1658 void
1659 Mixer_UI::disable_all_route_groups ()
1660 {
1661         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
1662 }
1663
1664 void
1665 Mixer_UI::route_groups_changed ()
1666 {
1667         ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
1668
1669         _in_group_rebuild_or_clear = true;
1670
1671         /* just rebuild the while thing */
1672
1673         group_model->clear ();
1674
1675 #if 0
1676         /* this is currently not used,
1677          * Mixer_UI::group_display_button_press() has a case for it,
1678          * and a commented edit_route_group() but that's n/a since 2011.
1679          *
1680          * This code is left as reminder that
1681          * row[group_columns.group] = 0 has special meaning.
1682          */
1683         {
1684                 TreeModel::Row row;
1685                 row = *(group_model->append());
1686                 row[group_columns.visible] = true;
1687                 row[group_columns.text] = (_("-all-"));
1688                 row[group_columns.group] = 0;
1689         }
1690 #endif
1691
1692         _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
1693
1694         _group_tabs->set_dirty ();
1695         _in_group_rebuild_or_clear = false;
1696 }
1697
1698 void
1699 Mixer_UI::new_route_group ()
1700 {
1701         _group_tabs->run_new_group_dialog (0, false);
1702 }
1703
1704 void
1705 Mixer_UI::remove_selected_route_group ()
1706 {
1707         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1708         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1709
1710         if (rows.empty()) {
1711                 return;
1712         }
1713
1714         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1715         TreeIter iter;
1716
1717         /* selection mode is single, so rows.begin() is it */
1718
1719         if ((iter = group_model->get_iter (*i))) {
1720
1721                 RouteGroup* rg = (*iter)[group_columns.group];
1722
1723                 if (rg) {
1724                         _session->remove_route_group (*rg);
1725                 }
1726         }
1727 }
1728
1729 void
1730 Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
1731 {
1732         if (in_group_row_change) {
1733                 return;
1734         }
1735
1736         /* force an update of any mixer strips that are using this group,
1737            otherwise mix group names don't change in mixer strips
1738         */
1739
1740         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1741                 if ((*i)->route_group() == group) {
1742                         (*i)->route_group_changed();
1743                 }
1744         }
1745
1746         TreeModel::iterator i;
1747         TreeModel::Children rows = group_model->children();
1748         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1749
1750         in_group_row_change = true;
1751
1752         for (i = rows.begin(); i != rows.end(); ++i) {
1753                 if ((*i)[group_columns.group] == group) {
1754                         (*i)[group_columns.visible] = !group->is_hidden ();
1755                         (*i)[group_columns.text] = group->name ();
1756                         break;
1757                 }
1758         }
1759
1760         in_group_row_change = false;
1761
1762         if (change.contains (Properties::name)) {
1763                 _group_tabs->set_dirty ();
1764         }
1765
1766         for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
1767                 if ((*j)->route_group() == group) {
1768                         if (group->is_hidden ()) {
1769                                 hide_strip (*j);
1770                         } else {
1771                                 show_strip (*j);
1772                         }
1773                 }
1774         }
1775 }
1776
1777 void
1778 Mixer_UI::show_mixer_list (bool yn)
1779 {
1780         if (yn) {
1781                 list_vpacker.show ();
1782         } else {
1783                 list_vpacker.hide ();
1784         }
1785
1786         _show_mixer_list = yn;
1787 }
1788
1789 void
1790 Mixer_UI::show_monitor_section (bool yn)
1791 {
1792         if (!monitor_section()) {
1793                 return;
1794         }
1795         if (monitor_section()->tearoff().torn_off()) {
1796                 return;
1797         }
1798
1799         if (yn) {
1800                 monitor_section()->tearoff().show();
1801         } else {
1802                 monitor_section()->tearoff().hide();
1803         }
1804 }
1805
1806 void
1807 Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
1808 {
1809         RouteGroup* group;
1810         TreeIter iter;
1811
1812         if ((iter = group_model->get_iter (path))) {
1813
1814                 if ((group = (*iter)[group_columns.group]) == 0) {
1815                         return;
1816                 }
1817
1818                 if (new_text != group->name()) {
1819                         group->set_name (new_text);
1820                 }
1821         }
1822 }
1823
1824 void
1825 Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
1826 {
1827         RouteGroup* group;
1828
1829         if (in_group_row_change) {
1830                 return;
1831         }
1832
1833         if ((group = (*iter)[group_columns.group]) == 0) {
1834                 return;
1835         }
1836
1837         std::string name = (*iter)[group_columns.text];
1838
1839         if (name != group->name()) {
1840                 group->set_name (name);
1841         }
1842
1843         bool hidden = !(*iter)[group_columns.visible];
1844
1845         if (hidden != group->is_hidden ()) {
1846                 group->set_hidden (hidden, this);
1847         }
1848 }
1849
1850 /** Called when a group model row is deleted, but also when the model is
1851  *  reordered by a user drag-and-drop; the latter is what we are
1852  *  interested in here.
1853  */
1854 void
1855 Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
1856 {
1857         if (_in_group_rebuild_or_clear) {
1858                 return;
1859         }
1860
1861         /* Re-write the session's route group list so that the new order is preserved */
1862
1863         list<RouteGroup*> new_list;
1864
1865         Gtk::TreeModel::Children children = group_model->children();
1866         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
1867                 RouteGroup* g = (*i)[group_columns.group];
1868                 if (g) {
1869                         new_list.push_back (g);
1870                 }
1871         }
1872
1873         _session->reorder_route_groups (new_list);
1874 }
1875
1876
1877 void
1878 Mixer_UI::add_route_group (RouteGroup* group)
1879 {
1880         ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
1881         bool focus = false;
1882
1883         in_group_row_change = true;
1884
1885         TreeModel::Row row = *(group_model->append());
1886         row[group_columns.visible] = !group->is_hidden ();
1887         row[group_columns.group] = group;
1888         if (!group->name().empty()) {
1889                 row[group_columns.text] = group->name();
1890         } else {
1891                 row[group_columns.text] = _("unnamed");
1892                 focus = true;
1893         }
1894
1895         group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
1896
1897         if (focus) {
1898                 TreeViewColumn* col = group_display.get_column (0);
1899                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
1900                 group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
1901         }
1902
1903         _group_tabs->set_dirty ();
1904
1905         in_group_row_change = false;
1906 }
1907
1908 bool
1909 Mixer_UI::strip_scroller_button_release (GdkEventButton* ev)
1910 {
1911         using namespace Menu_Helpers;
1912
1913         if (Keyboard::is_context_menu_event (ev)) {
1914                 ARDOUR_UI::instance()->add_route ();
1915                 return true;
1916         }
1917
1918         return false;
1919 }
1920
1921 void
1922 Mixer_UI::scroller_drag_data_received (const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& data, guint info, guint time)
1923 {
1924         printf ("Mixer_UI::scroller_drag_data_received\n");
1925         if (data.get_target() != "PluginFavoritePtr") {
1926                 context->drag_finish (false, false, time);
1927                 return;
1928         }
1929
1930         const void * d = data.get_data();
1931         const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
1932
1933         PluginPresetList nfos;
1934         TreeView* source;
1935         tv->get_object_drag_data (nfos, &source);
1936
1937         Route::ProcessorList pl;
1938         bool ok = false;
1939
1940         for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
1941                 PluginPresetPtr ppp = (*i);
1942                 PluginInfoPtr pip = ppp->_pip;
1943                 if (!pip->is_instrument ()) {
1944                         continue;
1945                 }
1946                 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);
1947                 ok = true;
1948         }
1949
1950         context->drag_finish (ok, false, time);
1951 }
1952
1953 void
1954 Mixer_UI::set_strip_width (Width w, bool save)
1955 {
1956         _strip_width = w;
1957
1958         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
1959                 (*i)->set_width_enum (w, save ? (*i)->width_owner() : this);
1960         }
1961 }
1962
1963
1964 struct PluginStateSorter {
1965 public:
1966         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
1967                 std::list<std::string>::const_iterator aiter = std::find(_user.begin(), _user.end(), (*a).unique_id);
1968                 std::list<std::string>::const_iterator biter = std::find(_user.begin(), _user.end(), (*b).unique_id);
1969                 if (aiter != _user.end() && biter != _user.end()) {
1970                         return std::distance (_user.begin(), aiter)  < std::distance (_user.begin(), biter);
1971                 }
1972                 if (aiter != _user.end()) {
1973                         return true;
1974                 }
1975                 if (biter != _user.end()) {
1976                         return false;
1977                 }
1978                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
1979         }
1980
1981         PluginStateSorter(std::list<std::string> user) : _user (user)  {}
1982 private:
1983         std::list<std::string> _user;
1984 };
1985
1986 int
1987 Mixer_UI::set_state (const XMLNode& node, int version)
1988 {
1989         XMLProperty const * prop;
1990         LocaleGuard lg;
1991
1992         Tabbable::set_state (node, version);
1993
1994         if ((prop = node.property ("narrow-strips"))) {
1995                 if (string_is_affirmative (prop->value())) {
1996                         set_strip_width (Narrow);
1997                 } else {
1998                         set_strip_width (Wide);
1999                 }
2000         }
2001
2002         if ((prop = node.property ("show-mixer"))) {
2003                 if (string_is_affirmative (prop->value())) {
2004                        _visible = true;
2005                 }
2006         }
2007
2008         if ((prop = node.property ("maximised"))) {
2009                 bool yn = string_is_affirmative (prop->value());
2010                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMaximalMixer"));
2011                 assert (act);
2012                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2013                 bool fs = tact && tact->get_active();
2014                 if (yn ^ fs) {
2015                         ActionManager::do_action ("Common", "ToggleMaximalMixer");
2016                 }
2017         }
2018
2019         if ((prop = node.property ("show-mixer-list"))) {
2020                 bool yn = string_is_affirmative (prop->value());
2021                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMixerList"));
2022                 assert (act);
2023                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2024
2025                 /* do it twice to force the change */
2026                 tact->set_active (!yn);
2027                 tact->set_active (yn);
2028         }
2029
2030
2031         XMLNode* plugin_order;
2032         if ((plugin_order = find_named_node (node, "PluginOrder")) != 0) {
2033                 store_current_favorite_order ();
2034                 std::list<string> order;
2035                 const XMLNodeList& kids = plugin_order->children("PluginInfo");
2036                 XMLNodeConstIterator i;
2037                 for (i = kids.begin(); i != kids.end(); ++i) {
2038                         if ((prop = (*i)->property ("unique-id"))) {
2039                                 std::string unique_id = prop->value();
2040                                 order.push_back (unique_id);
2041                                 if ((prop = (*i)->property ("expanded"))) {
2042                                         favorite_ui_state[unique_id] = string_is_affirmative (prop->value());
2043                                 }
2044                         }
2045                 }
2046                 PluginStateSorter cmp (order);
2047                 favorite_order.sort (cmp);
2048                 sync_treeview_from_favorite_order ();
2049         }
2050         return 0;
2051 }
2052
2053 XMLNode&
2054 Mixer_UI::get_state ()
2055 {
2056         XMLNode* node = new XMLNode (X_("Mixer"));
2057         char buf[128];
2058         LocaleGuard lg;
2059
2060         node->add_child_nocopy (Tabbable::get_state());
2061
2062         snprintf(buf,sizeof(buf), "%f", rhs_pane1.get_divider());
2063         node->add_property(X_("mixer-rhs-pane1-pos"), string(buf));
2064         snprintf(buf,sizeof(buf), "%f", rhs_pane2.get_divider());
2065         node->add_property(X_("mixer-rhs_pane2-pos"), string(buf));
2066         snprintf(buf,sizeof(buf), "%f", list_hpane.get_divider());
2067         node->add_property(X_("mixer-list-hpane-pos"), string(buf));
2068         snprintf(buf,sizeof(buf), "%f", inner_pane.get_divider());
2069         node->add_property(X_("mixer-inner-pane-pos"), string(buf));
2070
2071         node->add_property ("narrow-strips", _strip_width == Narrow ? "yes" : "no");
2072         node->add_property ("show-mixer", _visible ? "yes" : "no");
2073         node->add_property ("show-mixer-list", _show_mixer_list ? "yes" : "no");
2074         node->add_property ("maximised", _maximised ? "yes" : "no");
2075
2076         store_current_favorite_order ();
2077         XMLNode* plugin_order = new XMLNode ("PluginOrder");
2078         int cnt = 0;
2079         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i, ++cnt) {
2080                 XMLNode* p = new XMLNode ("PluginInfo");
2081                 p->add_property ("sort", cnt);
2082                 p->add_property ("unique-id", (*i)->unique_id);
2083                 if (favorite_ui_state.find ((*i)->unique_id) != favorite_ui_state.end ()) {
2084                         p->add_property ("expanded", favorite_ui_state[(*i)->unique_id]);
2085                 }
2086                 plugin_order->add_child_nocopy (*p);
2087         }
2088         node->add_child_nocopy (*plugin_order);
2089
2090         return *node;
2091 }
2092
2093 void
2094 Mixer_UI::scroll_left ()
2095 {
2096         if (!scroller.get_hscrollbar()) return;
2097         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2098         int sc_w = scroller.get_width();
2099         int sp_w = strip_packer.get_width();
2100         if (sp_w <= sc_w) {
2101                 return;
2102         }
2103         int lp = adj->get_value();
2104         int lm = 0;
2105         using namespace Gtk::Box_Helpers;
2106         const BoxList& strips = strip_packer.children();
2107         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2108                 lm += i->get_widget()->get_width ();
2109                 if (lm >= lp) {
2110                         lm -= i->get_widget()->get_width ();
2111                         break;
2112                 }
2113         }
2114         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2115 }
2116
2117 void
2118 Mixer_UI::scroll_right ()
2119 {
2120         if (!scroller.get_hscrollbar()) return;
2121         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2122         int sc_w = scroller.get_width();
2123         int sp_w = strip_packer.get_width();
2124         if (sp_w <= sc_w) {
2125                 return;
2126         }
2127         int lp = adj->get_value();
2128         int lm = 0;
2129         using namespace Gtk::Box_Helpers;
2130         const BoxList& strips = strip_packer.children();
2131         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2132                 lm += i->get_widget()->get_width ();
2133                 if (lm > lp + 1) {
2134                         break;
2135                 }
2136         }
2137         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2138 }
2139
2140 bool
2141 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
2142 {
2143         switch (ev->direction) {
2144         case GDK_SCROLL_LEFT:
2145                 scroll_left ();
2146                 return true;
2147         case GDK_SCROLL_UP:
2148                 if (ev->state & Keyboard::TertiaryModifier) {
2149                         scroll_left ();
2150                         return true;
2151                 }
2152                 return false;
2153
2154         case GDK_SCROLL_RIGHT:
2155                 scroll_right ();
2156                 return true;
2157
2158         case GDK_SCROLL_DOWN:
2159                 if (ev->state & Keyboard::TertiaryModifier) {
2160                         scroll_right ();
2161                         return true;
2162                 }
2163                 return false;
2164         }
2165
2166         return false;
2167 }
2168
2169
2170 void
2171 Mixer_UI::parameter_changed (string const & p)
2172 {
2173         if (p == "show-group-tabs") {
2174                 bool const s = _session->config.get_show_group_tabs ();
2175                 if (s) {
2176                         _group_tabs->show ();
2177                 } else {
2178                         _group_tabs->hide ();
2179                 }
2180         } else if (p == "default-narrow_ms") {
2181                 bool const s = UIConfiguration::instance().get_default_narrow_ms ();
2182                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2183                         (*i)->set_width_enum (s ? Narrow : Wide, this);
2184                 }
2185         } else if (p == "use-monitor-bus") {
2186                 if (_session && !_session->monitor_out()) {
2187                         monitor_section_detached ();
2188                 }
2189         }
2190 }
2191
2192 void
2193 Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
2194 {
2195         g->set_active (a, this);
2196 }
2197
2198 PluginSelector*
2199 Mixer_UI::plugin_selector()
2200 {
2201 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
2202         if (!_plugin_selector)
2203                 _plugin_selector = new PluginSelector (PluginManager::instance());
2204 #endif
2205
2206         return _plugin_selector;
2207 }
2208
2209 void
2210 Mixer_UI::setup_track_display ()
2211 {
2212         track_model = ListStore::create (stripable_columns);
2213         track_display.set_model (track_model);
2214         track_display.append_column (_("Show"), stripable_columns.visible);
2215         track_display.append_column (_("Strips"), stripable_columns.text);
2216         track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
2217         track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
2218         track_display.get_column (0)->set_expand(false);
2219         track_display.get_column (1)->set_expand(true);
2220         track_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
2221         track_display.set_name (X_("EditGroupList"));
2222         track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
2223         track_display.set_reorderable (true);
2224         track_display.set_headers_visible (true);
2225         track_display.set_can_focus(false);
2226
2227         track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
2228         track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
2229
2230         CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (0));
2231         track_list_visible_cell->property_activatable() = true;
2232         track_list_visible_cell->property_radio() = false;
2233         track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
2234
2235         track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
2236
2237         track_display_scroller.add (track_display);
2238         track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
2239
2240         VBox* v = manage (new VBox);
2241         v->show ();
2242         v->pack_start (track_display_scroller, true, true);
2243
2244         Button* b = manage (new Button);
2245         b->show ();
2246         Widget* w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
2247         w->show ();
2248         b->add (*w);
2249
2250         b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
2251
2252         v->pack_start (*b, false, false);
2253
2254         track_display_frame.set_name("BaseFrame");
2255         track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
2256         track_display_frame.add (*v);
2257
2258         track_display_scroller.show();
2259         track_display_frame.show();
2260         track_display.show();
2261 }
2262
2263 void
2264 Mixer_UI::new_track_or_bus ()
2265 {
2266         ARDOUR_UI::instance()->add_route ();
2267 }
2268
2269 void
2270 Mixer_UI::update_title ()
2271 {
2272         if (!own_window()) {
2273                 return;
2274         }
2275
2276         if (_session) {
2277                 string n;
2278
2279                 if (_session->snap_name() != _session->name()) {
2280                         n = _session->snap_name ();
2281                 } else {
2282                         n = _session->name ();
2283                 }
2284
2285                 if (_session->dirty ()) {
2286                         n = "*" + n;
2287                 }
2288
2289                 WindowTitle title (n);
2290                 title += S_("Window|Mixer");
2291                 title += Glib::get_application_name ();
2292                 own_window()->set_title (title.get_string());
2293
2294         } else {
2295
2296                 WindowTitle title (S_("Window|Mixer"));
2297                 title += Glib::get_application_name ();
2298                 own_window()->set_title (title.get_string());
2299         }
2300 }
2301
2302 MixerStrip*
2303 Mixer_UI::strip_by_x (int x)
2304 {
2305         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2306                 int x1, x2, y;
2307
2308                 (*i)->translate_coordinates (_content, 0, 0, x1, y);
2309                 x2 = x1 + (*i)->get_width();
2310
2311                 if (x >= x1 && x <= x2) {
2312                         return (*i);
2313                 }
2314         }
2315
2316         return 0;
2317 }
2318
2319 void
2320 Mixer_UI::set_axis_targets_for_operation ()
2321 {
2322         _axis_targets.clear ();
2323
2324         if (!_selection.empty()) {
2325                 _axis_targets = _selection.axes;
2326                 return;
2327         }
2328
2329 //  removed "implicit" selections of strips, after discussion on IRC
2330
2331 }
2332
2333 void
2334 Mixer_UI::monitor_section_going_away ()
2335 {
2336         if (_monitor_section) {
2337                 monitor_section_detached ();
2338                 out_packer.remove (_monitor_section->tearoff());
2339                 _monitor_section->set_session (0);
2340                 delete _monitor_section;
2341                 _monitor_section = 0;
2342         }
2343 }
2344
2345 void
2346 Mixer_UI::toggle_midi_input_active (bool flip_others)
2347 {
2348         boost::shared_ptr<RouteList> rl (new RouteList);
2349         bool onoff = false;
2350
2351         set_axis_targets_for_operation ();
2352
2353         for (AxisViewSelection::iterator r = _axis_targets.begin(); r != _axis_targets.end(); ++r) {
2354                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> ((*r)->stripable());
2355
2356                 if (mt) {
2357                         rl->push_back (mt);
2358                         onoff = !mt->input_active();
2359                 }
2360         }
2361
2362         _session->set_exclusive_input_active (rl, onoff, flip_others);
2363 }
2364
2365 void
2366 Mixer_UI::maximise_mixer_space ()
2367 {
2368         if (!own_window()) {
2369                 return;
2370         }
2371
2372         if (_maximised) {
2373                 return;
2374         }
2375
2376         _window->fullscreen ();
2377         _maximised = true;
2378 }
2379
2380 void
2381 Mixer_UI::restore_mixer_space ()
2382 {
2383         if (!own_window()) {
2384                 return;
2385         }
2386
2387         if (!_maximised) {
2388                 return;
2389         }
2390
2391         own_window()->unfullscreen();
2392         _maximised = false;
2393 }
2394
2395 void
2396 Mixer_UI::monitor_section_attached ()
2397 {
2398         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2399         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2400         act->set_sensitive (true);
2401         tact->set_active ();
2402 }
2403
2404 void
2405 Mixer_UI::monitor_section_detached ()
2406 {
2407         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2408         act->set_sensitive (false);
2409 }
2410
2411 void
2412 Mixer_UI::store_current_favorite_order ()
2413 {
2414         typedef Gtk::TreeModel::Children type_children;
2415         type_children children = favorite_plugins_model->children();
2416         favorite_order.clear();
2417         for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
2418         {
2419                 Gtk::TreeModel::Row row = *iter;
2420                 ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2421                 favorite_order.push_back (ppp->_pip);
2422                 std::string name = row[favorite_plugins_columns.name];
2423                 favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2424         }
2425 }
2426
2427 void
2428 Mixer_UI::save_favorite_ui_state (const TreeModel::iterator& iter, const TreeModel::Path& path)
2429 {
2430         Gtk::TreeModel::Row row = *iter;
2431         ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2432         assert (ppp);
2433         favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2434 }
2435
2436 void
2437 Mixer_UI::refiller (PluginInfoList& result, const PluginInfoList& plugs)
2438 {
2439         PluginManager& manager (PluginManager::instance());
2440         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
2441                 if (manager.get_status (*i) != PluginManager::Favorite) {
2442                         continue;
2443                 }
2444                 result.push_back (*i);
2445         }
2446 }
2447
2448 struct PluginCustomSorter {
2449 public:
2450         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2451                 PluginInfoList::const_iterator aiter = _user.begin();
2452                 PluginInfoList::const_iterator biter = _user.begin();
2453                 while (aiter != _user.end()) { if ((*aiter)->unique_id == a->unique_id) { break; } ++aiter; }
2454                 while (biter != _user.end()) { if ((*biter)->unique_id == b->unique_id) { break; } ++biter; }
2455
2456                 if (aiter != _user.end() && biter != _user.end()) {
2457                         return std::distance (_user.begin(), aiter) < std::distance (_user.begin(), biter);
2458                 }
2459                 if (aiter != _user.end()) {
2460                         return true;
2461                 }
2462                 if (biter != _user.end()) {
2463                         return false;
2464                 }
2465                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2466         }
2467         PluginCustomSorter(PluginInfoList user) : _user (user)  {}
2468 private:
2469         PluginInfoList _user;
2470 };
2471
2472 void
2473 Mixer_UI::refill_favorite_plugins ()
2474 {
2475         PluginInfoList plugs;
2476         PluginManager& mgr (PluginManager::instance());
2477
2478 #ifdef LV2_SUPPORT
2479         refiller (plugs, mgr.lv2_plugin_info ());
2480 #endif
2481 #ifdef WINDOWS_VST_SUPPORT
2482         refiller (plugs, mgr.windows_vst_plugin_info ());
2483 #endif
2484 #ifdef LXVST_SUPPORT
2485         refiller (plugs, mgr.lxvst_plugin_info ());
2486 #endif
2487 #ifdef AUDIOUNIT_SUPPORT
2488         refiller (plugs, mgr.au_plugin_info ());
2489 #endif
2490         refiller (plugs, mgr.ladspa_plugin_info ());
2491         refiller (plugs, mgr.lua_plugin_info ());
2492
2493         store_current_favorite_order ();
2494
2495         PluginCustomSorter cmp (favorite_order);
2496         plugs.sort (cmp);
2497
2498         favorite_order = plugs;
2499
2500         sync_treeview_from_favorite_order ();
2501 }
2502
2503 void
2504 Mixer_UI::sync_treeview_favorite_ui_state (const TreeModel::Path& path, const TreeModel::iterator&)
2505 {
2506         TreeIter iter;
2507         if (!(iter = favorite_plugins_model->get_iter (path))) {
2508                 return;
2509         }
2510         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2511         if (!ppp) {
2512                 return;
2513         }
2514         PluginInfoPtr pip = ppp->_pip;
2515         if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2516                 if (favorite_ui_state[pip->unique_id]) {
2517                         favorite_plugins_display.expand_row (path, true);
2518                 }
2519         }
2520 }
2521
2522 void
2523 Mixer_UI::sync_treeview_from_favorite_order ()
2524 {
2525         favorite_plugins_model->clear ();
2526         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i) {
2527                 PluginInfoPtr pip = (*i);
2528
2529                 TreeModel::Row newrow = *(favorite_plugins_model->append());
2530                 newrow[favorite_plugins_columns.name] = (*i)->name;
2531                 newrow[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip));
2532                 if (!_session) {
2533                         continue;
2534                 }
2535
2536                 vector<ARDOUR::Plugin::PresetRecord> presets = (*i)->get_presets (true);
2537                 for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator j = presets.begin(); j != presets.end(); ++j) {
2538                         Gtk::TreeModel::Row child_row = *(favorite_plugins_model->append (newrow.children()));
2539                         child_row[favorite_plugins_columns.name] = (*j).label;
2540                         child_row[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip, &(*j)));
2541                 }
2542                 if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2543                         if (favorite_ui_state[pip->unique_id]) {
2544                                 favorite_plugins_display.expand_row (favorite_plugins_model->get_path(newrow), true);
2545                         }
2546                 }
2547         }
2548 }
2549
2550 void
2551 Mixer_UI::popup_note_context_menu (GdkEventButton *ev)
2552 {
2553         using namespace Gtk::Menu_Helpers;
2554
2555         Gtk::Menu* m = manage (new Menu);
2556         MenuList& items = m->items ();
2557
2558         if (_selection.axes.empty()) {
2559                 items.push_back (MenuElem (_("No Track/Bus is selected.")));
2560         } else {
2561                 items.push_back (MenuElem (_("Add at the top"),
2562                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddTop)));
2563                 items.push_back (MenuElem (_("Add Pre-Fader"),
2564                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPreFader)));
2565                 items.push_back (MenuElem (_("Add Post-Fader"),
2566                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPostFader)));
2567                 items.push_back (MenuElem (_("Add at the end"),
2568                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddBottom)));
2569         }
2570
2571         items.push_back (SeparatorElem());
2572
2573         items.push_back (MenuElem (_("Remove from favorites"), sigc::mem_fun (*this, &Mixer_UI::remove_selected_from_favorites)));
2574
2575         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2576         if (ppp && ppp->_preset.valid && ppp->_preset.user) {
2577                 // we cannot currently delete AU presets
2578                 if (!ppp->_pip || ppp->_pip->type != AudioUnit) {
2579                         items.push_back (MenuElem (_("Delete Preset"), sigc::mem_fun (*this, &Mixer_UI::delete_selected_preset)));
2580                 }
2581         }
2582
2583         m->popup (ev->button, ev->time);
2584 }
2585
2586 bool
2587 Mixer_UI::plugin_row_button_press (GdkEventButton *ev)
2588 {
2589         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 3) ) {
2590                 TreeModel::Path path;
2591                 TreeViewColumn* column;
2592                 int cellx, celly;
2593                 if (favorite_plugins_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
2594                         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2595                         if (selection) {
2596                                 selection->unselect_all();
2597                                 selection->select(path);
2598                         }
2599                 }
2600                 ARDOUR::PluginPresetPtr ppp = selected_plugin();
2601                 if (ppp) {
2602                         popup_note_context_menu (ev);
2603                 }
2604         }
2605         return false;
2606 }
2607
2608
2609 PluginPresetPtr
2610 Mixer_UI::selected_plugin ()
2611 {
2612         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2613         if (!selection) {
2614                 return PluginPresetPtr();
2615         }
2616         Gtk::TreeModel::iterator iter = selection->get_selected();
2617         if (!iter) {
2618                 return PluginPresetPtr();
2619         }
2620         return (*iter)[favorite_plugins_columns.plugin];
2621 }
2622
2623 void
2624 Mixer_UI::add_selected_processor (ProcessorPosition pos)
2625 {
2626         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2627         if (ppp) {
2628                 add_favorite_processor (ppp, pos);
2629         }
2630 }
2631
2632 void
2633 Mixer_UI::delete_selected_preset ()
2634 {
2635         if (!_session) {
2636                 return;
2637         }
2638         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2639         if (!ppp || !ppp->_preset.valid || !ppp->_preset.user) {
2640                 return;
2641         }
2642         PluginPtr plugin = ppp->_pip->load (*_session);
2643         plugin->get_presets();
2644         plugin->remove_preset (ppp->_preset.label);
2645 }
2646
2647 void
2648 Mixer_UI::remove_selected_from_favorites ()
2649 {
2650         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2651         if (!ppp) {
2652                 return;
2653         }
2654         PluginManager::PluginStatusType status = PluginManager::Normal;
2655         PluginManager& manager (PluginManager::instance());
2656
2657         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2658         manager.save_statuses ();
2659 }
2660
2661 void
2662 Mixer_UI::plugin_row_activated (const TreeModel::Path& path, TreeViewColumn* column)
2663 {
2664         TreeIter iter;
2665         if (!(iter = favorite_plugins_model->get_iter (path))) {
2666                 return;
2667         }
2668         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2669         add_favorite_processor (ppp, AddPreFader); // TODO: preference?!
2670 }
2671
2672 void
2673 Mixer_UI::add_favorite_processor (ARDOUR::PluginPresetPtr ppp, ProcessorPosition pos)
2674 {
2675         if (!_session || _selection.axes.empty()) {
2676                 return;
2677         }
2678
2679         PluginInfoPtr pip = ppp->_pip;
2680         for (AxisViewSelection::iterator i = _selection.axes.begin(); i != _selection.axes.end(); ++i) {
2681                 boost::shared_ptr<ARDOUR::Route> rt = boost::dynamic_pointer_cast<ARDOUR::Route> ((*i)->stripable());
2682
2683                 if (!rt) {
2684                         continue;
2685                 }
2686
2687                 PluginPtr p = pip->load (*_session);
2688
2689                 if (!p) {
2690                         continue;
2691                 }
2692
2693                 if (ppp->_preset.valid) {
2694                         p->load_preset (ppp->_preset);
2695                 }
2696
2697                 Route::ProcessorStreams err;
2698                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
2699
2700                 switch (pos) {
2701                         case AddTop:
2702                                 rt->add_processor_by_index (processor, 0, &err, Config->get_new_plugins_active ());
2703                                 break;
2704                         case AddPreFader:
2705                                 rt->add_processor (processor, PreFader, &err, Config->get_new_plugins_active ());
2706                                 break;
2707                         case AddPostFader:
2708                                 {
2709                                         int idx = 0;
2710                                         int pos = 0;
2711                                         for (;;++idx) {
2712                                                 boost::shared_ptr<Processor> np = rt->nth_processor (idx);
2713                                                 if (!np) {
2714                                                         break;
2715                                                 }
2716                                                 if (!np->display_to_user()) {
2717                                                         continue;
2718                                                 }
2719                                                 if (boost::dynamic_pointer_cast<Amp> (np) && // Fader, not Trim
2720                                                                 boost::dynamic_pointer_cast<Amp> (np)->gain_control()->parameter().type() == GainAutomation) {
2721                                                         break;
2722                                                 }
2723                                                 ++pos;
2724                                         }
2725                                         rt->add_processor_by_index (processor, ++pos, &err, Config->get_new_plugins_active ());
2726                                 }
2727                                 break;
2728                         case AddBottom:
2729                                 rt->add_processor_by_index (processor, -1, &err, Config->get_new_plugins_active ());
2730                                 break;
2731                 }
2732         }
2733 }
2734
2735 bool
2736 PluginTreeStore::row_drop_possible_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& data) const
2737 {
2738         if (data.get_target() != "GTK_TREE_MODEL_ROW") {
2739                 return false;
2740         }
2741
2742         // only allow to re-order top-level items
2743         TreePath src;
2744         if (TreePath::get_from_selection_data (data, src)) {
2745                 if (src.up() && src.up()) {
2746                         return false;
2747                 }
2748         }
2749
2750         // don't allow to drop as child-rows.
2751         Gtk::TreeModel::Path _dest = dest; // un const
2752         const bool is_child = _dest.up (); // explicit bool for clang
2753         if (!is_child || _dest.empty ()) {
2754                 return true;
2755         }
2756         return false;
2757 }
2758
2759 void
2760 Mixer_UI::plugin_drop (const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData& data)
2761 {
2762         if (data.get_target() != "PluginPresetPtr") {
2763                 return;
2764         }
2765         if (data.get_length() != sizeof (PluginPresetPtr)) {
2766                 return;
2767         }
2768         const void *d = data.get_data();
2769         const PluginPresetPtr ppp = *(static_cast<const PluginPresetPtr*> (d));
2770
2771         PluginManager::PluginStatusType status = PluginManager::Favorite;
2772         PluginManager& manager (PluginManager::instance());
2773
2774         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2775         manager.save_statuses ();
2776 }
2777
2778 void
2779 Mixer_UI::do_vca_assign (boost::shared_ptr<VCA> vca)
2780 {
2781         /* call protected MixerActor:: method */
2782         vca_assign (vca);
2783 }
2784
2785 void
2786 Mixer_UI::do_vca_unassign (boost::shared_ptr<VCA> vca)
2787 {
2788         /* call protected MixerActor:: method */
2789         vca_unassign (vca);
2790 }
2791
2792 void
2793 Mixer_UI::show_vca_slaves (boost::shared_ptr<VCA> vca)
2794 {
2795         boost::shared_ptr<VCA> v = spilled_vca.lock();
2796         if (v != vca) {
2797                 spilled_vca = vca;
2798                 show_vca_change (vca); /* EMIT SIGNAL */
2799                 if (vca) {
2800                         _group_tabs->hide ();
2801                 } else {
2802                         _group_tabs->show ();
2803                 }
2804                 redisplay_track_list ();
2805         }
2806 }
2807
2808 bool
2809 Mixer_UI::showing_vca_slaves_for (boost::shared_ptr<VCA> vca) const
2810 {
2811        return vca == spilled_vca.lock();
2812 }
2813
2814 void
2815 Mixer_UI::register_actions ()
2816 {
2817         Glib::RefPtr<ActionGroup> group = myactions.create_action_group (X_("Mixer"));
2818
2819         myactions.register_action (group, "solo", _("Toggle Solo on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::solo_action));
2820         myactions.register_action (group, "mute", _("Toggle Mute on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::mute_action));
2821         myactions.register_action (group, "recenable", _("Toggle Rec-enable on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::rec_enable_action));
2822         myactions.register_action (group, "increment-gain", _("Decrease Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_up_action));
2823         myactions.register_action (group, "decrement-gain", _("Increase Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_down_action));
2824         myactions.register_action (group, "unity-gain", _("Set Gain to 0dB on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::unity_gain_action));
2825
2826
2827         myactions.register_action (group, "copy-processors", _("Copy Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::copy_processors));
2828         myactions.register_action (group, "cut-processors", _("Cut Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::cut_processors));
2829         myactions.register_action (group, "paste-processors", _("Paste Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::paste_processors));
2830         myactions.register_action (group, "delete-processors", _("Delete Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::delete_processors));
2831         myactions.register_action (group, "select-all-processors", _("Select All (visible) Processors"), sigc::mem_fun (*this, &Mixer_UI::select_all_processors));
2832         myactions.register_action (group, "toggle-processors", _("Toggle Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::toggle_processors));
2833         myactions.register_action (group, "ab-plugins", _("Toggle Selected Plugins"), sigc::mem_fun (*this, &Mixer_UI::ab_plugins));
2834         myactions.register_action (group, "select-none", _("Deselect all strips and processors"), sigc::mem_fun (*this, &Mixer_UI::select_none));
2835
2836         myactions.register_action (group, "scroll-left", _("Scroll Mixer Window to the left"), sigc::mem_fun (*this, &Mixer_UI::scroll_left));
2837         myactions.register_action (group, "scroll-right", _("Scroll Mixer Window to the right"), sigc::mem_fun (*this, &Mixer_UI::scroll_right));
2838
2839         myactions.register_action (group, "toggle-midi-input-active", _("Toggle MIDI Input Active for Mixer-Selected Tracks/Busses"),
2840                                    sigc::bind (sigc::mem_fun (*this, &Mixer_UI::toggle_midi_input_active), false));
2841 }
2842
2843 void
2844 Mixer_UI::load_bindings ()
2845 {
2846         bindings = Bindings::get_bindings (X_("Mixer"), myactions);
2847 }
2848
2849 template<class T> void
2850 Mixer_UI::control_action (boost::shared_ptr<T> (Stripable::*get_control)() const)
2851 {
2852         boost::shared_ptr<ControlList> cl (new ControlList);
2853         boost::shared_ptr<AutomationControl> ac;
2854         bool val = false;
2855         bool have_val = false;
2856
2857         set_axis_targets_for_operation ();
2858
2859         BOOST_FOREACH(AxisView* r, _axis_targets) {
2860                 boost::shared_ptr<Stripable> s = r->stripable();
2861                 if (s) {
2862                         ac = (s.get()->*get_control)();
2863                         if (ac) {
2864                                 cl->push_back (ac);
2865                                 if (!have_val) {
2866                                         val = !ac->get_value();
2867                                         have_val = true;
2868                                 }
2869                         }
2870                 }
2871         }
2872
2873         _session->set_controls (cl,  val, Controllable::UseGroup);
2874 }
2875
2876 void
2877 Mixer_UI::solo_action ()
2878 {
2879         control_action (&Stripable::solo_control);
2880 }
2881
2882 void
2883 Mixer_UI::mute_action ()
2884 {
2885         control_action (&Stripable::mute_control);
2886 }
2887
2888 void
2889 Mixer_UI::rec_enable_action ()
2890 {
2891         control_action (&Stripable::rec_enable_control);
2892 }
2893
2894 void
2895 Mixer_UI::step_gain_up_action ()
2896 {
2897         set_axis_targets_for_operation ();
2898
2899         BOOST_FOREACH(AxisView* r, _axis_targets) {
2900                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2901                 if (ms) {
2902                         ms->step_gain_up ();
2903                 }
2904         }
2905 }
2906
2907 void
2908 Mixer_UI::step_gain_down_action ()
2909 {
2910         set_axis_targets_for_operation ();
2911
2912         BOOST_FOREACH(AxisView* r, _axis_targets) {
2913                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2914                 if (ms) {
2915                         ms->step_gain_down ();
2916                 }
2917         }
2918 }
2919
2920 void
2921 Mixer_UI::unity_gain_action ()
2922 {
2923         set_axis_targets_for_operation ();
2924
2925         BOOST_FOREACH(AxisView* r, _axis_targets) {
2926                 boost::shared_ptr<Stripable> s = r->stripable();
2927                 if (s) {
2928                         boost::shared_ptr<AutomationControl> ac = s->gain_control();
2929                         if (ac) {
2930                                 ac->set_value (1.0, Controllable::UseGroup);
2931                         }
2932                 }
2933         }
2934 }
2935
2936 void
2937 Mixer_UI::copy_processors ()
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->copy_processors ();
2945                 }
2946         }
2947 }
2948 void
2949 Mixer_UI::cut_processors ()
2950 {
2951         set_axis_targets_for_operation ();
2952
2953         BOOST_FOREACH(AxisView* r, _axis_targets) {
2954                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2955                 if (ms) {
2956                         ms->cut_processors ();
2957                 }
2958         }
2959 }
2960 void
2961 Mixer_UI::paste_processors ()
2962 {
2963         set_axis_targets_for_operation ();
2964
2965         BOOST_FOREACH(AxisView* r, _axis_targets) {
2966                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2967                 if (ms) {
2968                         ms->paste_processors ();
2969                 }
2970         }
2971 }
2972 void
2973 Mixer_UI::select_all_processors ()
2974 {
2975         set_axis_targets_for_operation ();
2976
2977         BOOST_FOREACH(AxisView* r, _axis_targets) {
2978                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2979                 if (ms) {
2980                         ms->select_all_processors ();
2981                 }
2982         }
2983 }
2984 void
2985 Mixer_UI::toggle_processors ()
2986 {
2987         set_axis_targets_for_operation ();
2988
2989         BOOST_FOREACH(AxisView* r, _axis_targets) {
2990                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2991                 if (ms) {
2992                         ms->toggle_processors ();
2993                 }
2994         }
2995 }
2996 void
2997 Mixer_UI::ab_plugins ()
2998 {
2999         set_axis_targets_for_operation ();
3000
3001         BOOST_FOREACH(AxisView* r, _axis_targets) {
3002                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3003                 if (ms) {
3004                         ms->ab_plugins ();
3005                 }
3006         }
3007 }
3008
3009 void
3010 Mixer_UI::vca_assign (boost::shared_ptr<VCA> vca)
3011 {
3012         set_axis_targets_for_operation ();
3013 #if 0
3014         BOOST_FOREACH(AxisView* r, _axis_targets) {
3015                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3016                 if (ms) {
3017                         ms->vca_assign (vca);
3018                 }
3019         }
3020 #endif
3021 }
3022
3023 void
3024 Mixer_UI::vca_unassign (boost::shared_ptr<VCA> vca)
3025 {
3026         set_axis_targets_for_operation ();
3027 #if 0
3028         BOOST_FOREACH(AxisView* r, _axis_targets) {
3029                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3030                 if (ms) {
3031                         ms->vca_unassign (vca);
3032                 }
3033         }
3034 #endif
3035 }