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