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