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