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