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