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