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