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