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