Mixer strip list: click to move strip into view
[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         for (ri = rows.begin(); ri != rows.end(); ++ri) {
718                 bool visible = (*ri)[stripable_columns.visible];
719                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
720
721                 if (!stripable) {
722                         continue;
723                 }
724
725                 /* Monitor and Auditioner do not get their presentation
726                  * info reset here.
727                  */
728
729                 if (stripable->is_monitor() || stripable->is_auditioner()) {
730                         continue;
731                 }
732
733                 /* Master also doesn't get set here but since the editor allows
734                  * it to be reordered, we need to preserve its ordering.
735                  */
736
737                 stripable->presentation_info().set_hidden (!visible);
738
739                 if (order != stripable->presentation_info().order()) {
740                         stripable->set_presentation_order (order, false);
741                         change = true;
742                 }
743
744                 ++order;
745         }
746
747         if (change) {
748                 DEBUG_TRACE (DEBUG::OrderKeys, "... notify PI change from mixer GUI\n");
749                 _session->notify_presentation_info_change ();
750         }
751 }
752
753 void
754 Mixer_UI::sync_treeview_from_presentation_info ()
755 {
756         if (!_session || _session->deletion_in_progress()) {
757                 return;
758         }
759
760         DEBUG_TRACE (DEBUG::OrderKeys, "mixer sync model from presentation info.\n");
761
762         /* we could get here after either a change in the Mixer or Editor sort
763          * order, but either way, the mixer order keys reflect the intended
764          * order for the GUI, so reorder the treeview model to match it.
765          */
766
767         vector<int> neworder;
768         TreeModel::Children rows = track_model->children();
769         uint32_t old_order = 0;
770         bool changed = false;
771
772         if (rows.empty()) {
773                 return;
774         }
775
776         OrderingKeys sorted;
777
778         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
779                 boost::shared_ptr<Stripable> stripable = (*ri)[stripable_columns.stripable];
780                 sorted.push_back (OrderKeys (old_order, stripable->presentation_info().order()));
781         }
782
783         SortByNewDisplayOrder cmp;
784
785         sort (sorted.begin(), sorted.end(), cmp);
786         neworder.assign (sorted.size(), 0);
787
788         uint32_t n = 0;
789
790         for (OrderingKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
791
792                 neworder[n] = sr->old_display_order;
793
794                 if (sr->old_display_order != n) {
795                         changed = true;
796                 }
797         }
798
799         if (changed) {
800                 Unwinder<bool> uw (ignore_reorder, true);
801                 track_model->reorder (neworder);
802         }
803
804         redisplay_track_list ();
805 }
806
807 void
808 Mixer_UI::follow_editor_selection ()
809 {
810         if (_following_editor_selection) {
811                 return;
812         }
813
814         _following_editor_selection = true;
815         _selection.block_routes_changed (true);
816
817         TrackSelection& s (PublicEditor::instance().get_selection().tracks);
818
819         _selection.clear_routes ();
820
821         for (TrackViewList::iterator i = s.begin(); i != s.end(); ++i) {
822                 TimeAxisView* tav = dynamic_cast<TimeAxisView*> (*i);
823                 if (tav) {
824                         AxisView* axis = axis_by_stripable (tav->stripable());
825                         if (axis) {
826                                 _selection.add (axis);
827                         }
828                 }
829         }
830
831         _following_editor_selection = false;
832         _selection.block_routes_changed (false);
833 }
834
835
836 MixerStrip*
837 Mixer_UI::strip_by_route (boost::shared_ptr<Route> r) const
838 {
839         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
840                 if ((*i)->route() == r) {
841                         return (*i);
842                 }
843         }
844
845         return 0;
846 }
847
848 AxisView*
849 Mixer_UI::axis_by_stripable (boost::shared_ptr<Stripable> s) const
850 {
851         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
852                 if ((*i)->stripable() == s) {
853                         return (*i);
854                 }
855         }
856
857         return 0;
858 }
859
860 bool
861 Mixer_UI::strip_button_release_event (GdkEventButton *ev, MixerStrip *strip)
862 {
863         if (ev->button == 1) {
864                 if (_selection.selected (strip)) {
865                         /* primary-click: toggle selection state of strip */
866                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
867                                 _selection.remove (strip);
868                         } else if (_selection.axes.size() > 1) {
869                                 /* de-select others */
870                                 _selection.set (strip);
871                         }
872                 } else {
873                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
874                                 _selection.add (strip);
875                         } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::RangeSelectModifier)) {
876
877                                 /* extend selection */
878
879                                 vector<MixerStrip*> tmp;
880                                 bool accumulate = false;
881                                 bool found_another = false;
882
883                                 tmp.push_back (strip);
884
885                                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
886                                         if ((*i) == strip) {
887                                                 /* hit clicked strip, start accumulating till we hit the first
888                                                    selected strip
889                                                 */
890                                                 if (accumulate) {
891                                                         /* done */
892                                                         break;
893                                                 } else {
894                                                         accumulate = true;
895                                                 }
896                                         } else if (_selection.selected (*i)) {
897                                                 /* hit selected strip. if currently accumulating others,
898                                                    we're done. if not accumulating others, start doing so.
899                                                 */
900                                                 found_another = true;
901                                                 if (accumulate) {
902                                                         /* done */
903                                                         break;
904                                                 } else {
905                                                         accumulate = true;
906                                                 }
907                                         } else {
908                                                 if (accumulate) {
909                                                         tmp.push_back (*i);
910                                                 }
911                                         }
912                                 }
913
914                                 if (found_another) {
915                                         for (vector<MixerStrip*>::iterator i = tmp.begin(); i != tmp.end(); ++i) {
916                                                 _selection.add (*i);
917                                         }
918                                 } else
919                                         _selection.set (strip);  //user wants to start a range selection, but there aren't any others selected yet
920                         } else {
921                                 _selection.set (strip);
922                         }
923                 }
924         }
925
926         return true;
927 }
928
929 void
930 Mixer_UI::set_session (Session* sess)
931 {
932         SessionHandlePtr::set_session (sess);
933
934         if (_plugin_selector) {
935                 _plugin_selector->set_session (_session);
936         }
937
938         _group_tabs->set_session (sess);
939
940         if (!_session) {
941                 return;
942         }
943
944         refill_favorite_plugins();
945
946         XMLNode* node = ARDOUR_UI::instance()->mixer_settings();
947         set_state (*node, 0);
948
949         update_title ();
950
951         initial_track_display ();
952
953         _session->RouteAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_routes, this, _1), gui_context());
954         _session->route_group_added.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_route_group, this, _1), gui_context());
955         _session->route_group_removed.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
956         _session->route_groups_reordered.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::route_groups_changed, this), gui_context());
957         _session->config.ParameterChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context());
958         _session->DirtyChanged.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
959         _session->StateSaved.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::update_title, this), gui_context());
960
961         _session->vca_manager().VCAAdded.connect (_session_connections, invalidator (*this), boost::bind (&Mixer_UI::add_masters, this, _1), gui_context());
962
963         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::parameter_changed, this, _1), gui_context ());
964
965         route_groups_changed ();
966
967         if (_visible) {
968                 show_window();
969         }
970         start_updating ();
971 }
972
973 void
974 Mixer_UI::session_going_away ()
975 {
976         ENSURE_GUI_THREAD (*this, &Mixer_UI::session_going_away);
977
978         _in_group_rebuild_or_clear = true;
979         group_model->clear ();
980         _in_group_rebuild_or_clear = false;
981
982         _selection.clear ();
983         track_model->clear ();
984
985         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
986                 delete (*i);
987         }
988
989         if (_monitor_section) {
990                 _monitor_section->tearoff().hide_visible ();
991         }
992
993         monitor_section_detached ();
994
995         strips.clear ();
996
997         stop_updating ();
998
999         SessionHandlePtr::session_going_away ();
1000
1001         _session = 0;
1002         update_title ();
1003 }
1004
1005 void
1006 Mixer_UI::track_visibility_changed (std::string const & path)
1007 {
1008         if (_session && _session->deletion_in_progress()) {
1009                 return;
1010         }
1011
1012         TreeIter iter;
1013
1014         if ((iter = track_model->get_iter (path))) {
1015
1016                 AxisView* av = (*iter)[stripable_columns.strip];
1017                 bool visible = (*iter)[stripable_columns.visible];
1018
1019                 if (av->set_marked_for_display (!visible)) {
1020                         update_track_visibility ();
1021                 }
1022         }
1023 }
1024
1025 void
1026 Mixer_UI::update_track_visibility ()
1027 {
1028         TreeModel::Children rows = track_model->children();
1029         TreeModel::Children::iterator i;
1030
1031         {
1032                 Unwinder<bool> uw (no_track_list_redisplay, true);
1033
1034                 for (i = rows.begin(); i != rows.end(); ++i) {
1035                         AxisView* av = (*i)[stripable_columns.strip];
1036                         (*i)[stripable_columns.visible] = av->marked_for_display ();
1037                 }
1038
1039                 /* force presentation catch up with visibility changes
1040                  */
1041
1042                 sync_presentation_info_from_treeview ();
1043         }
1044
1045         redisplay_track_list ();
1046 }
1047
1048 void
1049 Mixer_UI::show_strip (MixerStrip* ms)
1050 {
1051         TreeModel::Children rows = track_model->children();
1052         TreeModel::Children::iterator i;
1053
1054         for (i = rows.begin(); i != rows.end(); ++i) {
1055
1056                 AxisView* av = (*i)[stripable_columns.strip];
1057                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1058                 if (strip == ms) {
1059                         (*i)[stripable_columns.visible] = true;
1060                         av->set_marked_for_display (true);
1061                         update_track_visibility ();
1062                         break;
1063                 }
1064         }
1065 }
1066
1067 void
1068 Mixer_UI::hide_strip (MixerStrip* ms)
1069 {
1070         TreeModel::Children rows = track_model->children();
1071         TreeModel::Children::iterator i;
1072
1073         for (i = rows.begin(); i != rows.end(); ++i) {
1074
1075                 AxisView* av = (*i)[stripable_columns.strip];
1076                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1077                 if (strip == ms) {
1078                         (*i)[stripable_columns.visible] = false;
1079                         av->set_marked_for_display (false);
1080                         update_track_visibility ();
1081                         break;
1082                 }
1083         }
1084 }
1085
1086 gint
1087 Mixer_UI::start_updating ()
1088 {
1089     fast_screen_update_connection = Timers::super_rapid_connect (sigc::mem_fun(*this, &Mixer_UI::fast_update_strips));
1090     return 0;
1091 }
1092
1093 gint
1094 Mixer_UI::stop_updating ()
1095 {
1096     fast_screen_update_connection.disconnect();
1097     return 0;
1098 }
1099
1100 void
1101 Mixer_UI::fast_update_strips ()
1102 {
1103         if (_content.is_mapped () && _session) {
1104                 for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1105                         (*i)->fast_update ();
1106                 }
1107         }
1108 }
1109
1110 void
1111 Mixer_UI::set_all_strips_visibility (bool yn)
1112 {
1113         TreeModel::Children rows = track_model->children();
1114         TreeModel::Children::iterator i;
1115
1116         {
1117                 Unwinder<bool> uw (no_track_list_redisplay, true);
1118
1119                 for (i = rows.begin(); i != rows.end(); ++i) {
1120
1121                         AxisView* av = (*i)[stripable_columns.strip];
1122                         MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1123
1124                         if (!strip) {
1125                                 continue;
1126                         }
1127
1128                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1129                                 continue;
1130                         }
1131
1132                         (*i)[stripable_columns.visible] = yn;
1133                 }
1134         }
1135
1136         redisplay_track_list ();
1137 }
1138
1139
1140 void
1141 Mixer_UI::set_all_audio_midi_visibility (int tracks, bool yn)
1142 {
1143         TreeModel::Children rows = track_model->children();
1144         TreeModel::Children::iterator i;
1145
1146         {
1147                 Unwinder<bool> uw (no_track_list_redisplay, true);
1148
1149                 for (i = rows.begin(); i != rows.end(); ++i) {
1150
1151                         AxisView* av = (*i)[stripable_columns.strip];
1152                         MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1153
1154                         if (!strip) {
1155                                 continue;
1156                         }
1157
1158                         if (strip->route()->is_master() || strip->route()->is_monitor()) {
1159                                 continue;
1160                         }
1161
1162                         boost::shared_ptr<AudioTrack> at = strip->audio_track();
1163                         boost::shared_ptr<MidiTrack> mt = strip->midi_track();
1164
1165                         switch (tracks) {
1166                         case 0:
1167                                 (*i)[stripable_columns.visible] = yn;
1168                                 break;
1169
1170                         case 1:
1171                                 if (at) { /* track */
1172                                         (*i)[stripable_columns.visible] = yn;
1173                                 }
1174                                 break;
1175
1176                         case 2:
1177                                 if (!at && !mt) { /* bus */
1178                                         (*i)[stripable_columns.visible] = yn;
1179                                 }
1180                                 break;
1181
1182                         case 3:
1183                                 if (mt) { /* midi-track */
1184                                         (*i)[stripable_columns.visible] = yn;
1185                                 }
1186                                 break;
1187                         }
1188                 }
1189         }
1190
1191         redisplay_track_list ();
1192 }
1193
1194 void
1195 Mixer_UI::hide_all_routes ()
1196 {
1197         set_all_strips_visibility (false);
1198 }
1199
1200 void
1201 Mixer_UI::show_all_routes ()
1202 {
1203         set_all_strips_visibility (true);
1204 }
1205
1206 void
1207 Mixer_UI::show_all_audiobus ()
1208 {
1209         set_all_audio_midi_visibility (2, true);
1210 }
1211 void
1212 Mixer_UI::hide_all_audiobus ()
1213 {
1214         set_all_audio_midi_visibility (2, false);
1215 }
1216
1217 void
1218 Mixer_UI::show_all_audiotracks()
1219 {
1220         set_all_audio_midi_visibility (1, true);
1221 }
1222 void
1223 Mixer_UI::hide_all_audiotracks ()
1224 {
1225         set_all_audio_midi_visibility (1, false);
1226 }
1227
1228 void
1229 Mixer_UI::show_all_miditracks()
1230 {
1231         set_all_audio_midi_visibility (3, true);
1232 }
1233 void
1234 Mixer_UI::hide_all_miditracks ()
1235 {
1236         set_all_audio_midi_visibility (3, false);
1237 }
1238
1239 void
1240 Mixer_UI::track_list_reorder (const TreeModel::Path&, const TreeModel::iterator&, int* /*new_order*/)
1241 {
1242         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview reordered\n");
1243         sync_presentation_info_from_treeview ();
1244 }
1245
1246 void
1247 Mixer_UI::track_list_delete (const Gtk::TreeModel::Path&)
1248 {
1249         /* this happens as the second step of a DnD within the treeview as well
1250            as when a row/route is actually deleted.
1251
1252            if it was a deletion then we have to force a redisplay because
1253            order keys may not have changed.
1254         */
1255
1256         DEBUG_TRACE (DEBUG::OrderKeys, "mixer UI treeview row deleted\n");
1257         sync_presentation_info_from_treeview ();
1258
1259         if (_route_deletion_in_progress) {
1260                 redisplay_track_list ();
1261         }
1262 }
1263
1264 void
1265 Mixer_UI::spill_redisplay (boost::shared_ptr<VCA> vca)
1266 {
1267         TreeModel::Children rows = track_model->children();
1268
1269         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
1270
1271                 AxisView* av = (*i)[stripable_columns.strip];
1272                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1273
1274                 if (!strip) {
1275                         /* we're in the middle of changing a row, don't worry */
1276                         continue;
1277                 }
1278
1279                 if (!strip->route()) {
1280                         /* non-route element */
1281                         continue;
1282                 }
1283
1284                 if (strip->route()->is_master() || strip->route()->is_monitor()) {
1285                         continue;
1286                 }
1287
1288                 if (strip->route()->slaved_to (vca)) {
1289
1290                         if (strip->packed()) {
1291                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1292                         } else {
1293                                 strip_packer.pack_start (*strip, false, false);
1294                                 strip->set_packed (true);
1295                         }
1296
1297                 } else {
1298
1299                         if (strip->packed()) {
1300                                 strip_packer.remove (*strip);
1301                                 strip->set_packed (false);
1302                         }
1303                 }
1304         }
1305 }
1306
1307 void
1308 Mixer_UI::redisplay_track_list ()
1309 {
1310         if (no_track_list_redisplay) {
1311                 return;
1312         }
1313
1314         boost::shared_ptr<VCA> sv = spilled_vca.lock ();
1315
1316         if (sv) {
1317                 spill_redisplay (sv);
1318                 return;
1319         }
1320
1321         TreeModel::Children rows = track_model->children();
1322         TreeModel::Children::iterator i;
1323         uint32_t n_masters = 0;
1324
1325         container_clear (vca_hpacker);
1326         vca_hpacker.pack_end (vca_scroller_base, true, true);
1327
1328         for (i = rows.begin(); i != rows.end(); ++i) {
1329
1330                 AxisView* s = (*i)[stripable_columns.strip];
1331                 bool const visible = (*i)[stripable_columns.visible];
1332                 boost::shared_ptr<Stripable> stripable = (*i)[stripable_columns.stripable];
1333
1334                 if (!s) {
1335                         /* we're in the middle of changing a row, don't worry */
1336                         continue;
1337                 }
1338
1339                 VCAMasterStrip* vms;
1340
1341                 if ((vms = dynamic_cast<VCAMasterStrip*> (s))) {
1342                         if (visible) {
1343                                 vca_hpacker.pack_start (*vms, false, false);
1344                                 vms->show ();
1345                                 n_masters++;
1346                         }
1347                         continue;
1348                 }
1349
1350                 MixerStrip* strip = dynamic_cast<MixerStrip*> (s);
1351
1352                 if (!strip) {
1353                         continue;
1354                 }
1355
1356                 if (visible) {
1357
1358                         if (strip->packed()) {
1359                                 strip_packer.reorder_child (*strip, -1); /* put at end */
1360                         } else {
1361                                 strip_packer.pack_start (*strip, false, false);
1362                                 strip->set_packed (true);
1363                         }
1364
1365                 } else {
1366
1367                         if (stripable->is_master() || stripable->is_monitor()) {
1368                                 /* do nothing, these cannot be hidden */
1369                         } else {
1370                                 if (strip->packed()) {
1371                                         strip_packer.remove (*strip);
1372                                         strip->set_packed (false);
1373                                 }
1374                         }
1375                 }
1376         }
1377
1378         /* update visibility of VCA assign buttons */
1379
1380         if (n_masters == 0) {
1381                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::remove_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1382                 vca_vpacker.hide ();
1383         } else {
1384                 UIConfiguration::instance().set_mixer_strip_visibility (VisibilityGroup::add_element (UIConfiguration::instance().get_mixer_strip_visibility(), X_("VCA")));
1385                 vca_vpacker.show ();
1386         }
1387
1388         _group_tabs->set_dirty ();
1389 }
1390
1391 void
1392 Mixer_UI::strip_width_changed ()
1393 {
1394         _group_tabs->set_dirty ();
1395
1396 #ifdef __APPLE__
1397         TreeModel::Children rows = track_model->children();
1398         TreeModel::Children::iterator i;
1399         long order;
1400
1401         for (order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
1402                 AxisView* av = (*i)[stripable_columns.strip];
1403                 MixerStrip* strip = dynamic_cast<MixerStrip*> (av);
1404
1405                 if (strip == 0) {
1406                         continue;
1407                 }
1408
1409                 bool visible = (*i)[stripable_columns.visible];
1410
1411                 if (visible) {
1412                         strip->queue_draw();
1413                 }
1414         }
1415 #endif
1416
1417 }
1418
1419 struct PresentationInfoMixerSorter
1420 {
1421         bool operator() (boost::shared_ptr<Stripable> a, boost::shared_ptr<Stripable> b) {
1422                 if (a->is_master()) {
1423                         /* master after everything else */
1424                         return false;
1425                 } else if (b->is_master()) {
1426                         /* everything else before master */
1427                         return true;
1428                 }
1429                 return a->presentation_info().order () < b->presentation_info().order ();
1430         }
1431 };
1432
1433 void
1434 Mixer_UI::initial_track_display ()
1435 {
1436         StripableList sl;
1437
1438         boost::shared_ptr<RouteList> routes = _session->get_routes();
1439
1440         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
1441                 sl.push_back (*r);
1442         }
1443
1444         VCAList vcas = _session->vca_manager().vcas();
1445
1446         for (VCAList::iterator v = vcas.begin(); v != vcas.end(); ++v) {
1447                 sl.push_back (boost::dynamic_pointer_cast<Stripable> (*v));
1448         }
1449
1450         sl.sort (PresentationInfoMixerSorter());
1451
1452         {
1453                 /* These are also used inside ::add_stripables() but we need
1454                  *  them here because we're going to clear the track_model also.
1455                  */
1456                 Unwinder<bool> uw1 (no_track_list_redisplay, true);
1457                 Unwinder<bool> uw2 (ignore_reorder, true);
1458
1459                 track_model->clear ();
1460                 add_stripables (sl);
1461         }
1462
1463         redisplay_track_list ();
1464 }
1465
1466 void
1467 Mixer_UI::show_track_list_menu ()
1468 {
1469         if (track_menu == 0) {
1470                 build_track_menu ();
1471         }
1472
1473         track_menu->popup (1, gtk_get_current_event_time());
1474 }
1475
1476 bool
1477 Mixer_UI::track_display_button_press (GdkEventButton* ev)
1478 {
1479         if (Keyboard::is_context_menu_event (ev)) {
1480                 show_track_list_menu ();
1481                 return true;
1482         }
1483         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 1)) {
1484                 TreeModel::Path path;
1485                 TreeViewColumn* column;
1486                 int cellx, celly;
1487                 if (track_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1488                         TreeIter iter = track_model->get_iter (path);
1489                         if ((*iter)[stripable_columns.visible]) {
1490                                 boost::shared_ptr<ARDOUR::Stripable> s = (*iter)[stripable_columns.stripable];
1491                                 move_stripable_into_view (s);
1492                         }
1493                 }
1494         }
1495
1496         return false;
1497 }
1498
1499 void
1500 Mixer_UI::move_stripable_into_view (boost::shared_ptr<ARDOUR::Stripable> s)
1501 {
1502         if (!scroller.get_hscrollbar()) {
1503                 return;
1504         }
1505         bool found = false;
1506         int x0 = 0;
1507         int x1 = 0;
1508         for (list<MixerStrip *>::const_iterator i = strips.begin(); i != strips.end(); ++i) {
1509                 if ((*i)->route() == s) {
1510                         int y;
1511                         found = true;
1512                         (*i)->translate_coordinates (strip_packer, 0, 0, x0, y);
1513                         x1 = x0 + (*i)->get_width ();
1514                         break;
1515                 }
1516         }
1517         if (!found) {
1518                 return;
1519         }
1520
1521         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
1522         int sl = adj->get_value();
1523         int sr = sl + scroller.get_width();
1524
1525         if (x0 < sl) {
1526                 scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), x0 - 1.0)));
1527         }
1528         else if (x1 > sr) {
1529                 // TODO: align left side of left most track, if possible
1530                 double re = x1 - scroller.get_width();
1531                 scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), re)));
1532         }
1533 }
1534
1535 void
1536 Mixer_UI::build_track_menu ()
1537 {
1538         using namespace Menu_Helpers;
1539         using namespace Gtk;
1540
1541         track_menu = new Menu;
1542         track_menu->set_name ("ArdourContextMenu");
1543         MenuList& items = track_menu->items();
1544
1545         items.push_back (MenuElem (_("Show All"), sigc::mem_fun(*this, &Mixer_UI::show_all_routes)));
1546         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun(*this, &Mixer_UI::hide_all_routes)));
1547         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiotracks)));
1548         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiotracks)));
1549         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::show_all_audiobus)));
1550         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun(*this, &Mixer_UI::hide_all_audiobus)));
1551         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::show_all_miditracks)));
1552         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &Mixer_UI::hide_all_miditracks)));
1553
1554 }
1555
1556 void
1557 Mixer_UI::stripable_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> ws)
1558 {
1559         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
1560                 return;
1561         }
1562
1563         boost::shared_ptr<Stripable> s = ws.lock ();
1564
1565         if (!s) {
1566                 return;
1567         }
1568
1569         TreeModel::Children rows = track_model->children();
1570         TreeModel::Children::iterator i;
1571
1572         for (i = rows.begin(); i != rows.end(); ++i) {
1573                 boost::shared_ptr<Stripable> ss = (*i)[stripable_columns.stripable];
1574
1575                 if (s == ss) {
1576
1577                         if (what_changed.contains (ARDOUR::Properties::name)) {
1578                                 (*i)[stripable_columns.text] = s->name();
1579                         }
1580
1581                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
1582                                 (*i)[stripable_columns.visible] = !s->presentation_info().hidden();
1583                                 redisplay_track_list ();
1584                         }
1585
1586                         return;
1587                 }
1588         }
1589
1590         error << _("track display list item for renamed strip not found!") << endmsg;
1591 }
1592
1593 bool
1594 Mixer_UI::group_display_button_press (GdkEventButton* ev)
1595 {
1596         TreeModel::Path path;
1597         TreeViewColumn* column;
1598         int cellx;
1599         int celly;
1600
1601         if (!group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
1602                 _group_tabs->get_menu(0)->popup (1, ev->time);
1603                 return true;
1604         }
1605
1606         TreeIter iter = group_model->get_iter (path);
1607         if (!iter) {
1608                 _group_tabs->get_menu(0)->popup (1, ev->time);
1609                 return true;
1610         }
1611
1612         RouteGroup* group = (*iter)[group_columns.group];
1613
1614         if (Keyboard::is_context_menu_event (ev)) {
1615                 _group_tabs->get_menu(group)->popup (1, ev->time);
1616                 return true;
1617         }
1618
1619         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
1620         case 1:
1621                 if (Keyboard::is_edit_event (ev)) {
1622                         if (group) {
1623                                 // edit_route_group (group);
1624 #ifdef __APPLE__
1625                                 group_display.queue_draw();
1626 #endif
1627                                 return true;
1628                         }
1629                 }
1630                 break;
1631
1632         case 0:
1633         {
1634                 bool visible = (*iter)[group_columns.visible];
1635                 (*iter)[group_columns.visible] = !visible;
1636 #ifdef __APPLE__
1637                 group_display.queue_draw();
1638 #endif
1639                 return true;
1640         }
1641
1642         default:
1643                 break;
1644         }
1645
1646         return false;
1647  }
1648
1649 void
1650 Mixer_UI::activate_all_route_groups ()
1651 {
1652         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), true));
1653 }
1654
1655 void
1656 Mixer_UI::disable_all_route_groups ()
1657 {
1658         _session->foreach_route_group (sigc::bind (sigc::mem_fun (*this, &Mixer_UI::set_route_group_activation), false));
1659 }
1660
1661 void
1662 Mixer_UI::route_groups_changed ()
1663 {
1664         ENSURE_GUI_THREAD (*this, &Mixer_UI::route_groups_changed);
1665
1666         _in_group_rebuild_or_clear = true;
1667
1668         /* just rebuild the while thing */
1669
1670         group_model->clear ();
1671
1672 #if 0
1673         /* this is currently not used,
1674          * Mixer_UI::group_display_button_press() has a case for it,
1675          * and a commented edit_route_group() but that's n/a since 2011.
1676          *
1677          * This code is left as reminder that
1678          * row[group_columns.group] = 0 has special meaning.
1679          */
1680         {
1681                 TreeModel::Row row;
1682                 row = *(group_model->append());
1683                 row[group_columns.visible] = true;
1684                 row[group_columns.text] = (_("-all-"));
1685                 row[group_columns.group] = 0;
1686         }
1687 #endif
1688
1689         _session->foreach_route_group (sigc::mem_fun (*this, &Mixer_UI::add_route_group));
1690
1691         _group_tabs->set_dirty ();
1692         _in_group_rebuild_or_clear = false;
1693 }
1694
1695 void
1696 Mixer_UI::new_route_group ()
1697 {
1698         _group_tabs->run_new_group_dialog (0, false);
1699 }
1700
1701 void
1702 Mixer_UI::remove_selected_route_group ()
1703 {
1704         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1705         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
1706
1707         if (rows.empty()) {
1708                 return;
1709         }
1710
1711         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
1712         TreeIter iter;
1713
1714         /* selection mode is single, so rows.begin() is it */
1715
1716         if ((iter = group_model->get_iter (*i))) {
1717
1718                 RouteGroup* rg = (*iter)[group_columns.group];
1719
1720                 if (rg) {
1721                         _session->remove_route_group (*rg);
1722                 }
1723         }
1724 }
1725
1726 void
1727 Mixer_UI::route_group_property_changed (RouteGroup* group, const PropertyChange& change)
1728 {
1729         if (in_group_row_change) {
1730                 return;
1731         }
1732
1733         /* force an update of any mixer strips that are using this group,
1734            otherwise mix group names don't change in mixer strips
1735         */
1736
1737         for (list<MixerStrip *>::iterator i = strips.begin(); i != strips.end(); ++i) {
1738                 if ((*i)->route_group() == group) {
1739                         (*i)->route_group_changed();
1740                 }
1741         }
1742
1743         TreeModel::iterator i;
1744         TreeModel::Children rows = group_model->children();
1745         Glib::RefPtr<TreeSelection> selection = group_display.get_selection();
1746
1747         in_group_row_change = true;
1748
1749         for (i = rows.begin(); i != rows.end(); ++i) {
1750                 if ((*i)[group_columns.group] == group) {
1751                         (*i)[group_columns.visible] = !group->is_hidden ();
1752                         (*i)[group_columns.text] = group->name ();
1753                         break;
1754                 }
1755         }
1756
1757         in_group_row_change = false;
1758
1759         if (change.contains (Properties::name)) {
1760                 _group_tabs->set_dirty ();
1761         }
1762
1763         for (list<MixerStrip*>::iterator j = strips.begin(); j != strips.end(); ++j) {
1764                 if ((*j)->route_group() == group) {
1765                         if (group->is_hidden ()) {
1766                                 hide_strip (*j);
1767                         } else {
1768                                 show_strip (*j);
1769                         }
1770                 }
1771         }
1772 }
1773
1774 void
1775 Mixer_UI::show_mixer_list (bool yn)
1776 {
1777         if (yn) {
1778                 list_vpacker.show ();
1779         } else {
1780                 list_vpacker.hide ();
1781         }
1782
1783         _show_mixer_list = yn;
1784 }
1785
1786 void
1787 Mixer_UI::show_monitor_section (bool yn)
1788 {
1789         if (!monitor_section()) {
1790                 return;
1791         }
1792         if (monitor_section()->tearoff().torn_off()) {
1793                 return;
1794         }
1795
1796         if (yn) {
1797                 monitor_section()->tearoff().show();
1798         } else {
1799                 monitor_section()->tearoff().hide();
1800         }
1801 }
1802
1803 void
1804 Mixer_UI::route_group_name_edit (const std::string& path, const std::string& new_text)
1805 {
1806         RouteGroup* group;
1807         TreeIter iter;
1808
1809         if ((iter = group_model->get_iter (path))) {
1810
1811                 if ((group = (*iter)[group_columns.group]) == 0) {
1812                         return;
1813                 }
1814
1815                 if (new_text != group->name()) {
1816                         group->set_name (new_text);
1817                 }
1818         }
1819 }
1820
1821 void
1822 Mixer_UI::route_group_row_change (const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator& iter)
1823 {
1824         RouteGroup* group;
1825
1826         if (in_group_row_change) {
1827                 return;
1828         }
1829
1830         if ((group = (*iter)[group_columns.group]) == 0) {
1831                 return;
1832         }
1833
1834         std::string name = (*iter)[group_columns.text];
1835
1836         if (name != group->name()) {
1837                 group->set_name (name);
1838         }
1839
1840         bool hidden = !(*iter)[group_columns.visible];
1841
1842         if (hidden != group->is_hidden ()) {
1843                 group->set_hidden (hidden, this);
1844         }
1845 }
1846
1847 /** Called when a group model row is deleted, but also when the model is
1848  *  reordered by a user drag-and-drop; the latter is what we are
1849  *  interested in here.
1850  */
1851 void
1852 Mixer_UI::route_group_row_deleted (Gtk::TreeModel::Path const &)
1853 {
1854         if (_in_group_rebuild_or_clear) {
1855                 return;
1856         }
1857
1858         /* Re-write the session's route group list so that the new order is preserved */
1859
1860         list<RouteGroup*> new_list;
1861
1862         Gtk::TreeModel::Children children = group_model->children();
1863         for (Gtk::TreeModel::Children::iterator i = children.begin(); i != children.end(); ++i) {
1864                 RouteGroup* g = (*i)[group_columns.group];
1865                 if (g) {
1866                         new_list.push_back (g);
1867                 }
1868         }
1869
1870         _session->reorder_route_groups (new_list);
1871 }
1872
1873
1874 void
1875 Mixer_UI::add_route_group (RouteGroup* group)
1876 {
1877         ENSURE_GUI_THREAD (*this, &Mixer_UI::add_route_group, group)
1878         bool focus = false;
1879
1880         in_group_row_change = true;
1881
1882         TreeModel::Row row = *(group_model->append());
1883         row[group_columns.visible] = !group->is_hidden ();
1884         row[group_columns.group] = group;
1885         if (!group->name().empty()) {
1886                 row[group_columns.text] = group->name();
1887         } else {
1888                 row[group_columns.text] = _("unnamed");
1889                 focus = true;
1890         }
1891
1892         group->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&Mixer_UI::route_group_property_changed, this, group, _1), gui_context());
1893
1894         if (focus) {
1895                 TreeViewColumn* col = group_display.get_column (0);
1896                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(group_display.get_column_cell_renderer (1));
1897                 group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
1898         }
1899
1900         _group_tabs->set_dirty ();
1901
1902         in_group_row_change = false;
1903 }
1904
1905 bool
1906 Mixer_UI::strip_scroller_button_release (GdkEventButton* ev)
1907 {
1908         using namespace Menu_Helpers;
1909
1910         if (Keyboard::is_context_menu_event (ev)) {
1911                 ARDOUR_UI::instance()->add_route ();
1912                 return true;
1913         }
1914
1915         return false;
1916 }
1917
1918 void
1919 Mixer_UI::scroller_drag_data_received (const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& data, guint info, guint time)
1920 {
1921         printf ("Mixer_UI::scroller_drag_data_received\n");
1922         if (data.get_target() != "PluginFavoritePtr") {
1923                 context->drag_finish (false, false, time);
1924                 return;
1925         }
1926
1927         const void * d = data.get_data();
1928         const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>* tv = reinterpret_cast<const Gtkmm2ext::DnDTreeView<ARDOUR::PluginPresetPtr>*>(d);
1929
1930         PluginPresetList nfos;
1931         TreeView* source;
1932         tv->get_object_drag_data (nfos, &source);
1933
1934         Route::ProcessorList pl;
1935         bool ok = false;
1936
1937         for (list<PluginPresetPtr>::const_iterator i = nfos.begin(); i != nfos.end(); ++i) {
1938                 PluginPresetPtr ppp = (*i);
1939                 PluginInfoPtr pip = ppp->_pip;
1940                 if (!pip->is_instrument ()) {
1941                         continue;
1942                 }
1943                 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);
1944                 ok = true;
1945         }
1946
1947         context->drag_finish (ok, false, time);
1948 }
1949
1950 void
1951 Mixer_UI::set_strip_width (Width w, bool save)
1952 {
1953         _strip_width = w;
1954
1955         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
1956                 (*i)->set_width_enum (w, save ? (*i)->width_owner() : this);
1957         }
1958 }
1959
1960
1961 struct PluginStateSorter {
1962 public:
1963         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
1964                 std::list<std::string>::const_iterator aiter = std::find(_user.begin(), _user.end(), (*a).unique_id);
1965                 std::list<std::string>::const_iterator biter = std::find(_user.begin(), _user.end(), (*b).unique_id);
1966                 if (aiter != _user.end() && biter != _user.end()) {
1967                         return std::distance (_user.begin(), aiter)  < std::distance (_user.begin(), biter);
1968                 }
1969                 if (aiter != _user.end()) {
1970                         return true;
1971                 }
1972                 if (biter != _user.end()) {
1973                         return false;
1974                 }
1975                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
1976         }
1977
1978         PluginStateSorter(std::list<std::string> user) : _user (user)  {}
1979 private:
1980         std::list<std::string> _user;
1981 };
1982
1983 int
1984 Mixer_UI::set_state (const XMLNode& node, int version)
1985 {
1986         XMLProperty const * prop;
1987         LocaleGuard lg;
1988
1989         Tabbable::set_state (node, version);
1990
1991         if ((prop = node.property ("narrow-strips"))) {
1992                 if (string_is_affirmative (prop->value())) {
1993                         set_strip_width (Narrow);
1994                 } else {
1995                         set_strip_width (Wide);
1996                 }
1997         }
1998
1999         if ((prop = node.property ("show-mixer"))) {
2000                 if (string_is_affirmative (prop->value())) {
2001                        _visible = true;
2002                 }
2003         }
2004
2005         if ((prop = node.property ("maximised"))) {
2006                 bool yn = string_is_affirmative (prop->value());
2007                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMaximalMixer"));
2008                 assert (act);
2009                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2010                 bool fs = tact && tact->get_active();
2011                 if (yn ^ fs) {
2012                         ActionManager::do_action ("Common", "ToggleMaximalMixer");
2013                 }
2014         }
2015
2016         if ((prop = node.property ("show-mixer-list"))) {
2017                 bool yn = string_is_affirmative (prop->value());
2018                 Glib::RefPtr<Action> act = ActionManager::get_action (X_("Common"), X_("ToggleMixerList"));
2019                 assert (act);
2020                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2021
2022                 /* do it twice to force the change */
2023                 tact->set_active (!yn);
2024                 tact->set_active (yn);
2025         }
2026
2027
2028         XMLNode* plugin_order;
2029         if ((plugin_order = find_named_node (node, "PluginOrder")) != 0) {
2030                 store_current_favorite_order ();
2031                 std::list<string> order;
2032                 const XMLNodeList& kids = plugin_order->children("PluginInfo");
2033                 XMLNodeConstIterator i;
2034                 for (i = kids.begin(); i != kids.end(); ++i) {
2035                         if ((prop = (*i)->property ("unique-id"))) {
2036                                 std::string unique_id = prop->value();
2037                                 order.push_back (unique_id);
2038                                 if ((prop = (*i)->property ("expanded"))) {
2039                                         favorite_ui_state[unique_id] = string_is_affirmative (prop->value());
2040                                 }
2041                         }
2042                 }
2043                 PluginStateSorter cmp (order);
2044                 favorite_order.sort (cmp);
2045                 sync_treeview_from_favorite_order ();
2046         }
2047         return 0;
2048 }
2049
2050 XMLNode&
2051 Mixer_UI::get_state ()
2052 {
2053         XMLNode* node = new XMLNode (X_("Mixer"));
2054         char buf[128];
2055         LocaleGuard lg;
2056
2057         node->add_child_nocopy (Tabbable::get_state());
2058
2059         snprintf(buf,sizeof(buf), "%f", rhs_pane1.get_divider());
2060         node->add_property(X_("mixer-rhs-pane1-pos"), string(buf));
2061         snprintf(buf,sizeof(buf), "%f", rhs_pane2.get_divider());
2062         node->add_property(X_("mixer-rhs_pane2-pos"), string(buf));
2063         snprintf(buf,sizeof(buf), "%f", list_hpane.get_divider());
2064         node->add_property(X_("mixer-list-hpane-pos"), string(buf));
2065         snprintf(buf,sizeof(buf), "%f", inner_pane.get_divider());
2066         node->add_property(X_("mixer-inner-pane-pos"), string(buf));
2067
2068         node->add_property ("narrow-strips", _strip_width == Narrow ? "yes" : "no");
2069         node->add_property ("show-mixer", _visible ? "yes" : "no");
2070         node->add_property ("show-mixer-list", _show_mixer_list ? "yes" : "no");
2071         node->add_property ("maximised", _maximised ? "yes" : "no");
2072
2073         store_current_favorite_order ();
2074         XMLNode* plugin_order = new XMLNode ("PluginOrder");
2075         int cnt = 0;
2076         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i, ++cnt) {
2077                 XMLNode* p = new XMLNode ("PluginInfo");
2078                 p->add_property ("sort", cnt);
2079                 p->add_property ("unique-id", (*i)->unique_id);
2080                 if (favorite_ui_state.find ((*i)->unique_id) != favorite_ui_state.end ()) {
2081                         p->add_property ("expanded", favorite_ui_state[(*i)->unique_id]);
2082                 }
2083                 plugin_order->add_child_nocopy (*p);
2084         }
2085         node->add_child_nocopy (*plugin_order);
2086
2087         return *node;
2088 }
2089
2090 void
2091 Mixer_UI::scroll_left ()
2092 {
2093         if (!scroller.get_hscrollbar()) return;
2094         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2095         int sc_w = scroller.get_width();
2096         int sp_w = strip_packer.get_width();
2097         if (sp_w <= sc_w) {
2098                 return;
2099         }
2100         int lp = adj->get_value();
2101         int lm = 0;
2102         using namespace Gtk::Box_Helpers;
2103         const BoxList& strips = strip_packer.children();
2104         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2105                 lm += i->get_widget()->get_width ();
2106                 if (lm >= lp) {
2107                         lm -= i->get_widget()->get_width ();
2108                         break;
2109                 }
2110         }
2111         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2112 }
2113
2114 void
2115 Mixer_UI::scroll_right ()
2116 {
2117         if (!scroller.get_hscrollbar()) return;
2118         Adjustment* adj = scroller.get_hscrollbar()->get_adjustment();
2119         int sc_w = scroller.get_width();
2120         int sp_w = strip_packer.get_width();
2121         if (sp_w <= sc_w) {
2122                 return;
2123         }
2124         int lp = adj->get_value();
2125         int lm = 0;
2126         using namespace Gtk::Box_Helpers;
2127         const BoxList& strips = strip_packer.children();
2128         for (BoxList::const_iterator i = strips.begin(); i != strips.end(); ++i) {
2129                 lm += i->get_widget()->get_width ();
2130                 if (lm > lp + 1) {
2131                         break;
2132                 }
2133         }
2134         scroller.get_hscrollbar()->set_value (max (adj->get_lower(), min (adj->get_upper(), lm - 1.0)));
2135 }
2136
2137 bool
2138 Mixer_UI::on_scroll_event (GdkEventScroll* ev)
2139 {
2140         switch (ev->direction) {
2141         case GDK_SCROLL_LEFT:
2142                 scroll_left ();
2143                 return true;
2144         case GDK_SCROLL_UP:
2145                 if (ev->state & Keyboard::TertiaryModifier) {
2146                         scroll_left ();
2147                         return true;
2148                 }
2149                 return false;
2150
2151         case GDK_SCROLL_RIGHT:
2152                 scroll_right ();
2153                 return true;
2154
2155         case GDK_SCROLL_DOWN:
2156                 if (ev->state & Keyboard::TertiaryModifier) {
2157                         scroll_right ();
2158                         return true;
2159                 }
2160                 return false;
2161         }
2162
2163         return false;
2164 }
2165
2166
2167 void
2168 Mixer_UI::parameter_changed (string const & p)
2169 {
2170         if (p == "show-group-tabs") {
2171                 bool const s = _session->config.get_show_group_tabs ();
2172                 if (s) {
2173                         _group_tabs->show ();
2174                 } else {
2175                         _group_tabs->hide ();
2176                 }
2177         } else if (p == "default-narrow_ms") {
2178                 bool const s = UIConfiguration::instance().get_default_narrow_ms ();
2179                 for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2180                         (*i)->set_width_enum (s ? Narrow : Wide, this);
2181                 }
2182         } else if (p == "use-monitor-bus") {
2183                 if (_session && !_session->monitor_out()) {
2184                         monitor_section_detached ();
2185                 }
2186         }
2187 }
2188
2189 void
2190 Mixer_UI::set_route_group_activation (RouteGroup* g, bool a)
2191 {
2192         g->set_active (a, this);
2193 }
2194
2195 PluginSelector*
2196 Mixer_UI::plugin_selector()
2197 {
2198 #ifdef DEFER_PLUGIN_SELECTOR_LOAD
2199         if (!_plugin_selector)
2200                 _plugin_selector = new PluginSelector (PluginManager::instance());
2201 #endif
2202
2203         return _plugin_selector;
2204 }
2205
2206 void
2207 Mixer_UI::setup_track_display ()
2208 {
2209         track_model = ListStore::create (stripable_columns);
2210         track_display.set_model (track_model);
2211         track_display.append_column (_("Show"), stripable_columns.visible);
2212         track_display.append_column (_("Strips"), stripable_columns.text);
2213         track_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
2214         track_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
2215         track_display.get_column (0)->set_expand(false);
2216         track_display.get_column (1)->set_expand(true);
2217         track_display.get_column (1)->set_sizing (Gtk::TREE_VIEW_COLUMN_FIXED);
2218         track_display.set_name (X_("EditGroupList"));
2219         track_display.get_selection()->set_mode (Gtk::SELECTION_NONE);
2220         track_display.set_reorderable (true);
2221         track_display.set_headers_visible (true);
2222         track_display.set_can_focus(false);
2223
2224         track_model->signal_row_deleted().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_delete));
2225         track_model->signal_rows_reordered().connect (sigc::mem_fun (*this, &Mixer_UI::track_list_reorder));
2226
2227         CellRendererToggle* track_list_visible_cell = dynamic_cast<CellRendererToggle*>(track_display.get_column_cell_renderer (0));
2228         track_list_visible_cell->property_activatable() = true;
2229         track_list_visible_cell->property_radio() = false;
2230         track_list_visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &Mixer_UI::track_visibility_changed));
2231
2232         track_display.signal_button_press_event().connect (sigc::mem_fun (*this, &Mixer_UI::track_display_button_press), false);
2233
2234         track_display_scroller.add (track_display);
2235         track_display_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
2236
2237         VBox* v = manage (new VBox);
2238         v->show ();
2239         v->pack_start (track_display_scroller, true, true);
2240
2241         Button* b = manage (new Button);
2242         b->show ();
2243         Widget* w = manage (new Image (Stock::ADD, ICON_SIZE_BUTTON));
2244         w->show ();
2245         b->add (*w);
2246
2247         b->signal_clicked().connect (sigc::mem_fun (*this, &Mixer_UI::new_track_or_bus));
2248
2249         v->pack_start (*b, false, false);
2250
2251         track_display_frame.set_name("BaseFrame");
2252         track_display_frame.set_shadow_type (Gtk::SHADOW_IN);
2253         track_display_frame.add (*v);
2254
2255         track_display_scroller.show();
2256         track_display_frame.show();
2257         track_display.show();
2258 }
2259
2260 void
2261 Mixer_UI::new_track_or_bus ()
2262 {
2263         ARDOUR_UI::instance()->add_route ();
2264 }
2265
2266 void
2267 Mixer_UI::update_title ()
2268 {
2269         if (!own_window()) {
2270                 return;
2271         }
2272
2273         if (_session) {
2274                 string n;
2275
2276                 if (_session->snap_name() != _session->name()) {
2277                         n = _session->snap_name ();
2278                 } else {
2279                         n = _session->name ();
2280                 }
2281
2282                 if (_session->dirty ()) {
2283                         n = "*" + n;
2284                 }
2285
2286                 WindowTitle title (n);
2287                 title += S_("Window|Mixer");
2288                 title += Glib::get_application_name ();
2289                 own_window()->set_title (title.get_string());
2290
2291         } else {
2292
2293                 WindowTitle title (S_("Window|Mixer"));
2294                 title += Glib::get_application_name ();
2295                 own_window()->set_title (title.get_string());
2296         }
2297 }
2298
2299 MixerStrip*
2300 Mixer_UI::strip_by_x (int x)
2301 {
2302         for (list<MixerStrip*>::iterator i = strips.begin(); i != strips.end(); ++i) {
2303                 int x1, x2, y;
2304
2305                 (*i)->translate_coordinates (_content, 0, 0, x1, y);
2306                 x2 = x1 + (*i)->get_width();
2307
2308                 if (x >= x1 && x <= x2) {
2309                         return (*i);
2310                 }
2311         }
2312
2313         return 0;
2314 }
2315
2316 void
2317 Mixer_UI::set_axis_targets_for_operation ()
2318 {
2319         _axis_targets.clear ();
2320
2321         if (!_selection.empty()) {
2322                 _axis_targets = _selection.axes;
2323                 return;
2324         }
2325
2326 //  removed "implicit" selections of strips, after discussion on IRC
2327
2328 }
2329
2330 void
2331 Mixer_UI::monitor_section_going_away ()
2332 {
2333         if (_monitor_section) {
2334                 monitor_section_detached ();
2335                 out_packer.remove (_monitor_section->tearoff());
2336                 _monitor_section->set_session (0);
2337                 delete _monitor_section;
2338                 _monitor_section = 0;
2339         }
2340 }
2341
2342 void
2343 Mixer_UI::toggle_midi_input_active (bool flip_others)
2344 {
2345         boost::shared_ptr<RouteList> rl (new RouteList);
2346         bool onoff = false;
2347
2348         set_axis_targets_for_operation ();
2349
2350         for (AxisViewSelection::iterator r = _axis_targets.begin(); r != _axis_targets.end(); ++r) {
2351                 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> ((*r)->stripable());
2352
2353                 if (mt) {
2354                         rl->push_back (mt);
2355                         onoff = !mt->input_active();
2356                 }
2357         }
2358
2359         _session->set_exclusive_input_active (rl, onoff, flip_others);
2360 }
2361
2362 void
2363 Mixer_UI::maximise_mixer_space ()
2364 {
2365         if (!own_window()) {
2366                 return;
2367         }
2368
2369         if (_maximised) {
2370                 return;
2371         }
2372
2373         _window->fullscreen ();
2374         _maximised = true;
2375 }
2376
2377 void
2378 Mixer_UI::restore_mixer_space ()
2379 {
2380         if (!own_window()) {
2381                 return;
2382         }
2383
2384         if (!_maximised) {
2385                 return;
2386         }
2387
2388         own_window()->unfullscreen();
2389         _maximised = false;
2390 }
2391
2392 void
2393 Mixer_UI::monitor_section_attached ()
2394 {
2395         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2396         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic(act);
2397         act->set_sensitive (true);
2398         tact->set_active ();
2399 }
2400
2401 void
2402 Mixer_UI::monitor_section_detached ()
2403 {
2404         Glib::RefPtr<Action> act = ActionManager::get_action ("Common", "ToggleMonitorSection");
2405         act->set_sensitive (false);
2406 }
2407
2408 void
2409 Mixer_UI::store_current_favorite_order ()
2410 {
2411         typedef Gtk::TreeModel::Children type_children;
2412         type_children children = favorite_plugins_model->children();
2413         favorite_order.clear();
2414         for(type_children::iterator iter = children.begin(); iter != children.end(); ++iter)
2415         {
2416                 Gtk::TreeModel::Row row = *iter;
2417                 ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2418                 favorite_order.push_back (ppp->_pip);
2419                 std::string name = row[favorite_plugins_columns.name];
2420                 favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2421         }
2422 }
2423
2424 void
2425 Mixer_UI::save_favorite_ui_state (const TreeModel::iterator& iter, const TreeModel::Path& path)
2426 {
2427         Gtk::TreeModel::Row row = *iter;
2428         ARDOUR::PluginPresetPtr ppp = row[favorite_plugins_columns.plugin];
2429         assert (ppp);
2430         favorite_ui_state[(*ppp->_pip).unique_id] = favorite_plugins_display.row_expanded (favorite_plugins_model->get_path(iter));
2431 }
2432
2433 void
2434 Mixer_UI::refiller (PluginInfoList& result, const PluginInfoList& plugs)
2435 {
2436         PluginManager& manager (PluginManager::instance());
2437         for (PluginInfoList::const_iterator i = plugs.begin(); i != plugs.end(); ++i) {
2438                 if (manager.get_status (*i) != PluginManager::Favorite) {
2439                         continue;
2440                 }
2441                 result.push_back (*i);
2442         }
2443 }
2444
2445 struct PluginCustomSorter {
2446 public:
2447         bool operator() (PluginInfoPtr a, PluginInfoPtr b) const {
2448                 PluginInfoList::const_iterator aiter = _user.begin();
2449                 PluginInfoList::const_iterator biter = _user.begin();
2450                 while (aiter != _user.end()) { if ((*aiter)->unique_id == a->unique_id) { break; } ++aiter; }
2451                 while (biter != _user.end()) { if ((*biter)->unique_id == b->unique_id) { break; } ++biter; }
2452
2453                 if (aiter != _user.end() && biter != _user.end()) {
2454                         return std::distance (_user.begin(), aiter) < std::distance (_user.begin(), biter);
2455                 }
2456                 if (aiter != _user.end()) {
2457                         return true;
2458                 }
2459                 if (biter != _user.end()) {
2460                         return false;
2461                 }
2462                 return ARDOUR::cmp_nocase((*a).name, (*b).name) == -1;
2463         }
2464         PluginCustomSorter(PluginInfoList user) : _user (user)  {}
2465 private:
2466         PluginInfoList _user;
2467 };
2468
2469 void
2470 Mixer_UI::refill_favorite_plugins ()
2471 {
2472         PluginInfoList plugs;
2473         PluginManager& mgr (PluginManager::instance());
2474
2475 #ifdef LV2_SUPPORT
2476         refiller (plugs, mgr.lv2_plugin_info ());
2477 #endif
2478 #ifdef WINDOWS_VST_SUPPORT
2479         refiller (plugs, mgr.windows_vst_plugin_info ());
2480 #endif
2481 #ifdef LXVST_SUPPORT
2482         refiller (plugs, mgr.lxvst_plugin_info ());
2483 #endif
2484 #ifdef AUDIOUNIT_SUPPORT
2485         refiller (plugs, mgr.au_plugin_info ());
2486 #endif
2487         refiller (plugs, mgr.ladspa_plugin_info ());
2488         refiller (plugs, mgr.lua_plugin_info ());
2489
2490         store_current_favorite_order ();
2491
2492         PluginCustomSorter cmp (favorite_order);
2493         plugs.sort (cmp);
2494
2495         favorite_order = plugs;
2496
2497         sync_treeview_from_favorite_order ();
2498 }
2499
2500 void
2501 Mixer_UI::sync_treeview_favorite_ui_state (const TreeModel::Path& path, const TreeModel::iterator&)
2502 {
2503         TreeIter iter;
2504         if (!(iter = favorite_plugins_model->get_iter (path))) {
2505                 return;
2506         }
2507         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2508         if (!ppp) {
2509                 return;
2510         }
2511         PluginInfoPtr pip = ppp->_pip;
2512         if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2513                 if (favorite_ui_state[pip->unique_id]) {
2514                         favorite_plugins_display.expand_row (path, true);
2515                 }
2516         }
2517 }
2518
2519 void
2520 Mixer_UI::sync_treeview_from_favorite_order ()
2521 {
2522         favorite_plugins_model->clear ();
2523         for (PluginInfoList::const_iterator i = favorite_order.begin(); i != favorite_order.end(); ++i) {
2524                 PluginInfoPtr pip = (*i);
2525
2526                 TreeModel::Row newrow = *(favorite_plugins_model->append());
2527                 newrow[favorite_plugins_columns.name] = (*i)->name;
2528                 newrow[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip));
2529                 if (!_session) {
2530                         continue;
2531                 }
2532
2533                 vector<ARDOUR::Plugin::PresetRecord> presets = (*i)->get_presets (true);
2534                 for (vector<ARDOUR::Plugin::PresetRecord>::const_iterator j = presets.begin(); j != presets.end(); ++j) {
2535                         Gtk::TreeModel::Row child_row = *(favorite_plugins_model->append (newrow.children()));
2536                         child_row[favorite_plugins_columns.name] = (*j).label;
2537                         child_row[favorite_plugins_columns.plugin] = PluginPresetPtr (new PluginPreset(pip, &(*j)));
2538                 }
2539                 if (favorite_ui_state.find (pip->unique_id) != favorite_ui_state.end ()) {
2540                         if (favorite_ui_state[pip->unique_id]) {
2541                                 favorite_plugins_display.expand_row (favorite_plugins_model->get_path(newrow), true);
2542                         }
2543                 }
2544         }
2545 }
2546
2547 void
2548 Mixer_UI::popup_note_context_menu (GdkEventButton *ev)
2549 {
2550         using namespace Gtk::Menu_Helpers;
2551
2552         Gtk::Menu* m = manage (new Menu);
2553         MenuList& items = m->items ();
2554
2555         if (_selection.axes.empty()) {
2556                 items.push_back (MenuElem (_("No Track/Bus is selected.")));
2557         } else {
2558                 items.push_back (MenuElem (_("Add at the top"),
2559                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddTop)));
2560                 items.push_back (MenuElem (_("Add Pre-Fader"),
2561                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPreFader)));
2562                 items.push_back (MenuElem (_("Add Post-Fader"),
2563                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddPostFader)));
2564                 items.push_back (MenuElem (_("Add at the end"),
2565                                         sigc::bind (sigc::mem_fun (*this, &Mixer_UI::add_selected_processor), AddBottom)));
2566         }
2567
2568         items.push_back (SeparatorElem());
2569
2570         items.push_back (MenuElem (_("Remove from favorites"), sigc::mem_fun (*this, &Mixer_UI::remove_selected_from_favorites)));
2571
2572         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2573         if (ppp && ppp->_preset.valid && ppp->_preset.user) {
2574                 // we cannot currently delete AU presets
2575                 if (!ppp->_pip || ppp->_pip->type != AudioUnit) {
2576                         items.push_back (MenuElem (_("Delete Preset"), sigc::mem_fun (*this, &Mixer_UI::delete_selected_preset)));
2577                 }
2578         }
2579
2580         m->popup (ev->button, ev->time);
2581 }
2582
2583 bool
2584 Mixer_UI::plugin_row_button_press (GdkEventButton *ev)
2585 {
2586         if ((ev->type == GDK_BUTTON_PRESS) && (ev->button == 3) ) {
2587                 TreeModel::Path path;
2588                 TreeViewColumn* column;
2589                 int cellx, celly;
2590                 if (favorite_plugins_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
2591                         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2592                         if (selection) {
2593                                 selection->unselect_all();
2594                                 selection->select(path);
2595                         }
2596                 }
2597                 ARDOUR::PluginPresetPtr ppp = selected_plugin();
2598                 if (ppp) {
2599                         popup_note_context_menu (ev);
2600                 }
2601         }
2602         return false;
2603 }
2604
2605
2606 PluginPresetPtr
2607 Mixer_UI::selected_plugin ()
2608 {
2609         Glib::RefPtr<Gtk::TreeView::Selection> selection = favorite_plugins_display.get_selection();
2610         if (!selection) {
2611                 return PluginPresetPtr();
2612         }
2613         Gtk::TreeModel::iterator iter = selection->get_selected();
2614         if (!iter) {
2615                 return PluginPresetPtr();
2616         }
2617         return (*iter)[favorite_plugins_columns.plugin];
2618 }
2619
2620 void
2621 Mixer_UI::add_selected_processor (ProcessorPosition pos)
2622 {
2623         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2624         if (ppp) {
2625                 add_favorite_processor (ppp, pos);
2626         }
2627 }
2628
2629 void
2630 Mixer_UI::delete_selected_preset ()
2631 {
2632         if (!_session) {
2633                 return;
2634         }
2635         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2636         if (!ppp || !ppp->_preset.valid || !ppp->_preset.user) {
2637                 return;
2638         }
2639         PluginPtr plugin = ppp->_pip->load (*_session);
2640         plugin->get_presets();
2641         plugin->remove_preset (ppp->_preset.label);
2642 }
2643
2644 void
2645 Mixer_UI::remove_selected_from_favorites ()
2646 {
2647         ARDOUR::PluginPresetPtr ppp = selected_plugin();
2648         if (!ppp) {
2649                 return;
2650         }
2651         PluginManager::PluginStatusType status = PluginManager::Normal;
2652         PluginManager& manager (PluginManager::instance());
2653
2654         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2655         manager.save_statuses ();
2656 }
2657
2658 void
2659 Mixer_UI::plugin_row_activated (const TreeModel::Path& path, TreeViewColumn* column)
2660 {
2661         TreeIter iter;
2662         if (!(iter = favorite_plugins_model->get_iter (path))) {
2663                 return;
2664         }
2665         ARDOUR::PluginPresetPtr ppp = (*iter)[favorite_plugins_columns.plugin];
2666         add_favorite_processor (ppp, AddPreFader); // TODO: preference?!
2667 }
2668
2669 void
2670 Mixer_UI::add_favorite_processor (ARDOUR::PluginPresetPtr ppp, ProcessorPosition pos)
2671 {
2672         if (!_session || _selection.axes.empty()) {
2673                 return;
2674         }
2675
2676         PluginInfoPtr pip = ppp->_pip;
2677         for (AxisViewSelection::iterator i = _selection.axes.begin(); i != _selection.axes.end(); ++i) {
2678                 boost::shared_ptr<ARDOUR::Route> rt = boost::dynamic_pointer_cast<ARDOUR::Route> ((*i)->stripable());
2679
2680                 if (!rt) {
2681                         continue;
2682                 }
2683
2684                 PluginPtr p = pip->load (*_session);
2685
2686                 if (!p) {
2687                         continue;
2688                 }
2689
2690                 if (ppp->_preset.valid) {
2691                         p->load_preset (ppp->_preset);
2692                 }
2693
2694                 Route::ProcessorStreams err;
2695                 boost::shared_ptr<Processor> processor (new PluginInsert (*_session, p));
2696
2697                 switch (pos) {
2698                         case AddTop:
2699                                 rt->add_processor_by_index (processor, 0, &err, Config->get_new_plugins_active ());
2700                                 break;
2701                         case AddPreFader:
2702                                 rt->add_processor (processor, PreFader, &err, Config->get_new_plugins_active ());
2703                                 break;
2704                         case AddPostFader:
2705                                 {
2706                                         int idx = 0;
2707                                         int pos = 0;
2708                                         for (;;++idx) {
2709                                                 boost::shared_ptr<Processor> np = rt->nth_processor (idx);
2710                                                 if (!np) {
2711                                                         break;
2712                                                 }
2713                                                 if (!np->display_to_user()) {
2714                                                         continue;
2715                                                 }
2716                                                 if (boost::dynamic_pointer_cast<Amp> (np) && // Fader, not Trim
2717                                                                 boost::dynamic_pointer_cast<Amp> (np)->gain_control()->parameter().type() == GainAutomation) {
2718                                                         break;
2719                                                 }
2720                                                 ++pos;
2721                                         }
2722                                         rt->add_processor_by_index (processor, ++pos, &err, Config->get_new_plugins_active ());
2723                                 }
2724                                 break;
2725                         case AddBottom:
2726                                 rt->add_processor_by_index (processor, -1, &err, Config->get_new_plugins_active ());
2727                                 break;
2728                 }
2729         }
2730 }
2731
2732 bool
2733 PluginTreeStore::row_drop_possible_vfunc(const Gtk::TreeModel::Path& dest, const Gtk::SelectionData& data) const
2734 {
2735         if (data.get_target() != "GTK_TREE_MODEL_ROW") {
2736                 return false;
2737         }
2738
2739         // only allow to re-order top-level items
2740         TreePath src;
2741         if (TreePath::get_from_selection_data (data, src)) {
2742                 if (src.up() && src.up()) {
2743                         return false;
2744                 }
2745         }
2746
2747         // don't allow to drop as child-rows.
2748         Gtk::TreeModel::Path _dest = dest; // un const
2749         const bool is_child = _dest.up (); // explicit bool for clang
2750         if (!is_child || _dest.empty ()) {
2751                 return true;
2752         }
2753         return false;
2754 }
2755
2756 void
2757 Mixer_UI::plugin_drop (const Glib::RefPtr<Gdk::DragContext>&, const Gtk::SelectionData& data)
2758 {
2759         if (data.get_target() != "PluginPresetPtr") {
2760                 return;
2761         }
2762         if (data.get_length() != sizeof (PluginPresetPtr)) {
2763                 return;
2764         }
2765         const void *d = data.get_data();
2766         const PluginPresetPtr ppp = *(static_cast<const PluginPresetPtr*> (d));
2767
2768         PluginManager::PluginStatusType status = PluginManager::Favorite;
2769         PluginManager& manager (PluginManager::instance());
2770
2771         manager.set_status (ppp->_pip->type, ppp->_pip->unique_id, status);
2772         manager.save_statuses ();
2773 }
2774
2775 void
2776 Mixer_UI::do_vca_assign (boost::shared_ptr<VCA> vca)
2777 {
2778         /* call protected MixerActor:: method */
2779         vca_assign (vca);
2780 }
2781
2782 void
2783 Mixer_UI::do_vca_unassign (boost::shared_ptr<VCA> vca)
2784 {
2785         /* call protected MixerActor:: method */
2786         vca_unassign (vca);
2787 }
2788
2789 void
2790 Mixer_UI::show_vca_slaves (boost::shared_ptr<VCA> vca)
2791 {
2792         boost::shared_ptr<VCA> v = spilled_vca.lock();
2793         if (v != vca) {
2794                 spilled_vca = vca;
2795                 show_vca_change (vca); /* EMIT SIGNAL */
2796                 if (vca) {
2797                         _group_tabs->hide ();
2798                 } else {
2799                         _group_tabs->show ();
2800                 }
2801                 redisplay_track_list ();
2802         }
2803 }
2804
2805 bool
2806 Mixer_UI::showing_vca_slaves_for (boost::shared_ptr<VCA> vca) const
2807 {
2808        return vca == spilled_vca.lock();
2809 }
2810
2811 void
2812 Mixer_UI::register_actions ()
2813 {
2814         Glib::RefPtr<ActionGroup> group = myactions.create_action_group (X_("Mixer"));
2815
2816         myactions.register_action (group, "solo", _("Toggle Solo on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::solo_action));
2817         myactions.register_action (group, "mute", _("Toggle Mute on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::mute_action));
2818         myactions.register_action (group, "recenable", _("Toggle Rec-enable on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::rec_enable_action));
2819         myactions.register_action (group, "increment-gain", _("Decrease Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_up_action));
2820         myactions.register_action (group, "decrement-gain", _("Increase Gain on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::step_gain_down_action));
2821         myactions.register_action (group, "unity-gain", _("Set Gain to 0dB on Mixer-Selected Tracks/Busses"), sigc::mem_fun (*this, &Mixer_UI::unity_gain_action));
2822
2823
2824         myactions.register_action (group, "copy-processors", _("Copy Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::copy_processors));
2825         myactions.register_action (group, "cut-processors", _("Cut Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::cut_processors));
2826         myactions.register_action (group, "paste-processors", _("Paste Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::paste_processors));
2827         myactions.register_action (group, "delete-processors", _("Delete Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::delete_processors));
2828         myactions.register_action (group, "select-all-processors", _("Select All (visible) Processors"), sigc::mem_fun (*this, &Mixer_UI::select_all_processors));
2829         myactions.register_action (group, "toggle-processors", _("Toggle Selected Processors"), sigc::mem_fun (*this, &Mixer_UI::toggle_processors));
2830         myactions.register_action (group, "ab-plugins", _("Toggle Selected Plugins"), sigc::mem_fun (*this, &Mixer_UI::ab_plugins));
2831         myactions.register_action (group, "select-none", _("Deselect all strips and processors"), sigc::mem_fun (*this, &Mixer_UI::select_none));
2832
2833         myactions.register_action (group, "scroll-left", _("Scroll Mixer Window to the left"), sigc::mem_fun (*this, &Mixer_UI::scroll_left));
2834         myactions.register_action (group, "scroll-right", _("Scroll Mixer Window to the left"), sigc::mem_fun (*this, &Mixer_UI::scroll_right));
2835
2836         myactions.register_action (group, "toggle-midi-input-active", _("Toggle MIDI Input Active for Mixer-Selected Tracks/Busses"),
2837                                    sigc::bind (sigc::mem_fun (*this, &Mixer_UI::toggle_midi_input_active), false));
2838 }
2839
2840 void
2841 Mixer_UI::load_bindings ()
2842 {
2843         bindings = Bindings::get_bindings (X_("Mixer"), myactions);
2844 }
2845
2846 template<class T> void
2847 Mixer_UI::control_action (boost::shared_ptr<T> (Stripable::*get_control)() const)
2848 {
2849         boost::shared_ptr<ControlList> cl (new ControlList);
2850         boost::shared_ptr<AutomationControl> ac;
2851         bool val = false;
2852         bool have_val = false;
2853
2854         set_axis_targets_for_operation ();
2855
2856         BOOST_FOREACH(AxisView* r, _axis_targets) {
2857                 boost::shared_ptr<Stripable> s = r->stripable();
2858                 if (s) {
2859                         ac = (s.get()->*get_control)();
2860                         if (ac) {
2861                                 cl->push_back (ac);
2862                                 if (!have_val) {
2863                                         val = !ac->get_value();
2864                                         have_val = true;
2865                                 }
2866                         }
2867                 }
2868         }
2869
2870         _session->set_controls (cl,  val, Controllable::UseGroup);
2871 }
2872
2873 void
2874 Mixer_UI::solo_action ()
2875 {
2876         control_action (&Stripable::solo_control);
2877 }
2878
2879 void
2880 Mixer_UI::mute_action ()
2881 {
2882         control_action (&Stripable::mute_control);
2883 }
2884
2885 void
2886 Mixer_UI::rec_enable_action ()
2887 {
2888         control_action (&Stripable::rec_enable_control);
2889 }
2890
2891 void
2892 Mixer_UI::step_gain_up_action ()
2893 {
2894         set_axis_targets_for_operation ();
2895
2896         BOOST_FOREACH(AxisView* r, _axis_targets) {
2897                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2898                 if (ms) {
2899                         ms->step_gain_up ();
2900                 }
2901         }
2902 }
2903
2904 void
2905 Mixer_UI::step_gain_down_action ()
2906 {
2907         set_axis_targets_for_operation ();
2908
2909         BOOST_FOREACH(AxisView* r, _axis_targets) {
2910                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2911                 if (ms) {
2912                         ms->step_gain_down ();
2913                 }
2914         }
2915 }
2916
2917 void
2918 Mixer_UI::unity_gain_action ()
2919 {
2920         set_axis_targets_for_operation ();
2921
2922         BOOST_FOREACH(AxisView* r, _axis_targets) {
2923                 boost::shared_ptr<Stripable> s = r->stripable();
2924                 if (s) {
2925                         boost::shared_ptr<AutomationControl> ac = s->gain_control();
2926                         if (ac) {
2927                                 ac->set_value (1.0, Controllable::UseGroup);
2928                         }
2929                 }
2930         }
2931 }
2932
2933 void
2934 Mixer_UI::copy_processors ()
2935 {
2936         set_axis_targets_for_operation ();
2937
2938         BOOST_FOREACH(AxisView* r, _axis_targets) {
2939                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2940                 if (ms) {
2941                         ms->copy_processors ();
2942                 }
2943         }
2944 }
2945 void
2946 Mixer_UI::cut_processors ()
2947 {
2948         set_axis_targets_for_operation ();
2949
2950         BOOST_FOREACH(AxisView* r, _axis_targets) {
2951                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2952                 if (ms) {
2953                         ms->cut_processors ();
2954                 }
2955         }
2956 }
2957 void
2958 Mixer_UI::paste_processors ()
2959 {
2960         set_axis_targets_for_operation ();
2961
2962         BOOST_FOREACH(AxisView* r, _axis_targets) {
2963                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2964                 if (ms) {
2965                         ms->paste_processors ();
2966                 }
2967         }
2968 }
2969 void
2970 Mixer_UI::select_all_processors ()
2971 {
2972         set_axis_targets_for_operation ();
2973
2974         BOOST_FOREACH(AxisView* r, _axis_targets) {
2975                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2976                 if (ms) {
2977                         ms->select_all_processors ();
2978                 }
2979         }
2980 }
2981 void
2982 Mixer_UI::toggle_processors ()
2983 {
2984         set_axis_targets_for_operation ();
2985
2986         BOOST_FOREACH(AxisView* r, _axis_targets) {
2987                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
2988                 if (ms) {
2989                         ms->toggle_processors ();
2990                 }
2991         }
2992 }
2993 void
2994 Mixer_UI::ab_plugins ()
2995 {
2996         set_axis_targets_for_operation ();
2997
2998         BOOST_FOREACH(AxisView* r, _axis_targets) {
2999                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3000                 if (ms) {
3001                         ms->ab_plugins ();
3002                 }
3003         }
3004 }
3005
3006 void
3007 Mixer_UI::vca_assign (boost::shared_ptr<VCA> vca)
3008 {
3009         set_axis_targets_for_operation ();
3010 #if 0
3011         BOOST_FOREACH(AxisView* r, _axis_targets) {
3012                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3013                 if (ms) {
3014                         ms->vca_assign (vca);
3015                 }
3016         }
3017 #endif
3018 }
3019
3020 void
3021 Mixer_UI::vca_unassign (boost::shared_ptr<VCA> vca)
3022 {
3023         set_axis_targets_for_operation ();
3024 #if 0
3025         BOOST_FOREACH(AxisView* r, _axis_targets) {
3026                 MixerStrip* ms = dynamic_cast<MixerStrip*> (r);
3027                 if (ms) {
3028                         ms->vca_unassign (vca);
3029                 }
3030         }
3031 #endif
3032 }