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