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