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