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