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