Fix thinkos in cubasish theme
[ardour.git] / gtk2_ardour / editor_routes.cc
1 /*
2  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2013-2015 Nick Mainsbridge <mainsbridge@gmail.com>
6  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7  * Copyright (C) 2016-2018 Len Ovens <len@ovenwerks.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <cstdlib>
25 #include <cassert>
26 #include <cmath>
27 #include <list>
28 #include <vector>
29 #include <algorithm>
30
31 #include "pbd/unknown_type.h"
32 #include "pbd/unwind.h"
33
34 #include "ardour/debug.h"
35 #include "ardour/audio_track.h"
36 #include "ardour/midi_track.h"
37 #include "ardour/route.h"
38 #include "ardour/selection.h"
39 #include "ardour/session.h"
40 #include "ardour/solo_isolate_control.h"
41 #include "ardour/utils.h"
42 #include "ardour/vca.h"
43 #include "ardour/vca_manager.h"
44
45 #include "gtkmm2ext/cell_renderer_pixbuf_multi.h"
46 #include "gtkmm2ext/cell_renderer_pixbuf_toggle.h"
47 #include "gtkmm2ext/treeutils.h"
48
49 #include "widgets/tooltips.h"
50
51 #include "actions.h"
52 #include "ardour_ui.h"
53 #include "audio_time_axis.h"
54 #include "editor.h"
55 #include "editor_group_tabs.h"
56 #include "editor_routes.h"
57 #include "gui_thread.h"
58 #include "keyboard.h"
59 #include "midi_time_axis.h"
60 #include "mixer_strip.h"
61 #include "plugin_setup_dialog.h"
62 #include "route_sorter.h"
63 #include "vca_time_axis.h"
64 #include "utils.h"
65
66 #include "pbd/i18n.h"
67
68 using namespace std;
69 using namespace ARDOUR;
70 using namespace ArdourWidgets;
71 using namespace ARDOUR_UI_UTILS;
72 using namespace PBD;
73 using namespace Gtk;
74 using namespace Gtkmm2ext;
75 using namespace Glib;
76 using Gtkmm2ext::Keyboard;
77
78 struct ColumnInfo {
79         int         index;
80         const char* label;
81         const char* tooltip;
82 };
83
84 EditorRoutes::EditorRoutes (Editor* e)
85         : EditorComponent (e)
86         , _ignore_reorder (false)
87         , _ignore_selection_change (false)
88         , column_does_not_select (false)
89         , _no_redisplay (false)
90         , _adding_routes (false)
91         , _route_deletion_in_progress (false)
92         , _redisplay_on_resume (false)
93         , _redisplay_active (0)
94         , _queue_tv_update (0)
95         , _menu (0)
96         , old_focus (0)
97         , name_editable (0)
98 {
99         static const int column_width = 22;
100
101         _scroller.add (_display);
102         _scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
103
104         _model = ListStore::create (_columns);
105         _display.set_model (_model);
106
107         _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorRoutes::select_function));
108
109         // Record enable toggle
110         CellRendererPixbufMulti* rec_col_renderer = manage (new CellRendererPixbufMulti());
111
112         rec_col_renderer->set_pixbuf (0, ::get_icon("record-normal-disabled"));
113         rec_col_renderer->set_pixbuf (1, ::get_icon("record-normal-in-progress"));
114         rec_col_renderer->set_pixbuf (2, ::get_icon("record-normal-enabled"));
115         rec_col_renderer->set_pixbuf (3, ::get_icon("record-step"));
116         rec_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_rec_enable_changed));
117
118         rec_state_column = manage (new TreeViewColumn("R", *rec_col_renderer));
119
120         rec_state_column->add_attribute(rec_col_renderer->property_state(), _columns.rec_state);
121         rec_state_column->add_attribute(rec_col_renderer->property_visible(), _columns.is_track);
122
123         rec_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
124         rec_state_column->set_alignment(ALIGN_CENTER);
125         rec_state_column->set_expand(false);
126         rec_state_column->set_fixed_width(column_width);
127
128
129         // Record safe toggle
130         CellRendererPixbufMulti* rec_safe_renderer = manage (new CellRendererPixbufMulti ());
131
132         rec_safe_renderer->set_pixbuf (0, ::get_icon("rec-safe-disabled"));
133         rec_safe_renderer->set_pixbuf (1, ::get_icon("rec-safe-enabled"));
134         rec_safe_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_rec_safe_toggled));
135
136         rec_safe_column = manage (new TreeViewColumn(_("RS"), *rec_safe_renderer));
137         rec_safe_column->add_attribute(rec_safe_renderer->property_state(), _columns.rec_safe);
138         rec_safe_column->add_attribute(rec_safe_renderer->property_visible(), _columns.is_track);
139         rec_safe_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
140         rec_safe_column->set_alignment(ALIGN_CENTER);
141         rec_safe_column->set_expand(false);
142         rec_safe_column->set_fixed_width(column_width);
143
144
145         // MIDI Input Active
146
147         CellRendererPixbufMulti* input_active_col_renderer = manage (new CellRendererPixbufMulti());
148         input_active_col_renderer->set_pixbuf (0, ::get_icon("midi-input-inactive"));
149         input_active_col_renderer->set_pixbuf (1, ::get_icon("midi-input-active"));
150         input_active_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_input_active_changed));
151
152         input_active_column = manage (new TreeViewColumn ("I", *input_active_col_renderer));
153
154         input_active_column->add_attribute(input_active_col_renderer->property_state(), _columns.is_input_active);
155         input_active_column->add_attribute (input_active_col_renderer->property_visible(), _columns.is_midi);
156
157         input_active_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
158         input_active_column->set_alignment(ALIGN_CENTER);
159         input_active_column->set_expand(false);
160         input_active_column->set_fixed_width(column_width);
161
162         // Mute enable toggle
163         CellRendererPixbufMulti* mute_col_renderer = manage (new CellRendererPixbufMulti());
164
165         mute_col_renderer->set_pixbuf (Gtkmm2ext::Off, ::get_icon("mute-disabled"));
166         mute_col_renderer->set_pixbuf (Gtkmm2ext::ImplicitActive, ::get_icon("muted-by-others"));
167         mute_col_renderer->set_pixbuf (Gtkmm2ext::ExplicitActive, ::get_icon("mute-enabled"));
168         mute_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_mute_enable_toggled));
169
170         mute_state_column = manage (new TreeViewColumn("M", *mute_col_renderer));
171
172         mute_state_column->add_attribute(mute_col_renderer->property_state(), _columns.mute_state);
173         mute_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
174         mute_state_column->set_alignment(ALIGN_CENTER);
175         mute_state_column->set_expand(false);
176         mute_state_column->set_fixed_width(15);
177
178         // Solo enable toggle
179         CellRendererPixbufMulti* solo_col_renderer = manage (new CellRendererPixbufMulti());
180
181         solo_col_renderer->set_pixbuf (Gtkmm2ext::Off, ::get_icon("solo-disabled"));
182         solo_col_renderer->set_pixbuf (Gtkmm2ext::ExplicitActive, ::get_icon("solo-enabled"));
183         solo_col_renderer->set_pixbuf (Gtkmm2ext::ImplicitActive, ::get_icon("soloed-by-others"));
184         solo_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_enable_toggled));
185
186         solo_state_column = manage (new TreeViewColumn("S", *solo_col_renderer));
187
188         solo_state_column->add_attribute(solo_col_renderer->property_state(), _columns.solo_state);
189         solo_state_column->add_attribute(solo_col_renderer->property_visible(), _columns.solo_visible);
190         solo_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
191         solo_state_column->set_alignment(ALIGN_CENTER);
192         solo_state_column->set_expand(false);
193         solo_state_column->set_fixed_width(column_width);
194
195         // Solo isolate toggle
196         CellRendererPixbufMulti* solo_iso_renderer = manage (new CellRendererPixbufMulti());
197
198         solo_iso_renderer->set_pixbuf (0, ::get_icon("solo-isolate-disabled"));
199         solo_iso_renderer->set_pixbuf (1, ::get_icon("solo-isolate-enabled"));
200         solo_iso_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_isolate_toggled));
201
202         solo_isolate_state_column = manage (new TreeViewColumn("SI", *solo_iso_renderer));
203
204         solo_isolate_state_column->add_attribute(solo_iso_renderer->property_state(), _columns.solo_isolate_state);
205         solo_isolate_state_column->add_attribute(solo_iso_renderer->property_visible(), _columns.solo_lock_iso_visible);
206         solo_isolate_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
207         solo_isolate_state_column->set_alignment(ALIGN_CENTER);
208         solo_isolate_state_column->set_expand(false);
209         solo_isolate_state_column->set_fixed_width(column_width);
210
211         // Solo safe toggle
212         CellRendererPixbufMulti* solo_safe_renderer = manage (new CellRendererPixbufMulti ());
213
214         solo_safe_renderer->set_pixbuf (0, ::get_icon("solo-safe-disabled"));
215         solo_safe_renderer->set_pixbuf (1, ::get_icon("solo-safe-enabled"));
216         solo_safe_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_safe_toggled));
217
218         solo_safe_state_column = manage (new TreeViewColumn(_("SS"), *solo_safe_renderer));
219         solo_safe_state_column->add_attribute(solo_safe_renderer->property_state(), _columns.solo_safe_state);
220         solo_safe_state_column->add_attribute(solo_safe_renderer->property_visible(), _columns.solo_lock_iso_visible);
221         solo_safe_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
222         solo_safe_state_column->set_alignment(ALIGN_CENTER);
223         solo_safe_state_column->set_expand(false);
224         solo_safe_state_column->set_fixed_width(column_width);
225
226         _name_column = _display.append_column ("", _columns.text) - 1;
227         _visible_column = _display.append_column ("", _columns.visible) - 1;
228         _active_column = _display.append_column ("", _columns.active) - 1;
229
230         name_column = _display.get_column (_name_column);
231         visible_column = _display.get_column (_visible_column);
232         active_column = _display.get_column (_active_column);
233
234         _display.append_column (*input_active_column);
235         _display.append_column (*rec_state_column);
236         _display.append_column (*rec_safe_column);
237         _display.append_column (*mute_state_column);
238         _display.append_column (*solo_state_column);
239         _display.append_column (*solo_isolate_state_column);
240         _display.append_column (*solo_safe_state_column);
241
242
243         TreeViewColumn* col;
244         Gtk::Label* l;
245
246         ColumnInfo ci[] = {
247                 { 0,  _("Name"),        _("Track/Bus Name") },
248                 { 1, S_("Visible|V"),   _("Track/Bus visible ?") },
249                 { 2, S_("Active|A"),    _("Track/Bus active ?") },
250                 { 3, S_("MidiInput|I"), _("MIDI input enabled") },
251                 { 4, S_("Rec|R"),       _("Record enabled") },
252                 { 5, S_("Rec|RS"),      _("Record Safe") },
253                 { 6, S_("Mute|M"),      _("Muted") },
254                 { 7, S_("Solo|S"),      _("Soloed") },
255                 { 8, S_("SoloIso|SI"),  _("Solo Isolated") },
256                 { 9, S_("SoloLock|SS"), _("Solo Safe (Locked)") },
257                 { -1, 0, 0 }
258         };
259
260         for (int i = 0; ci[i].index >= 0; ++i) {
261                 col = _display.get_column (ci[i].index);
262                 l = manage (new Label (ci[i].label));
263                 set_tooltip (*l, ci[i].tooltip);
264                 col->set_widget (*l);
265                 l->show ();
266         }
267
268         _display.set_headers_visible (true);
269         _display.get_selection()->set_mode (SELECTION_MULTIPLE);
270         _display.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::selection_changed));
271         _display.set_reorderable (true);
272         _display.set_name (X_("EditGroupList"));
273         _display.set_rules_hint (true);
274         _display.set_size_request (100, -1);
275
276         CellRendererText* name_cell = dynamic_cast<CellRendererText*> (_display.get_column_cell_renderer (_name_column));
277
278         assert (name_cell);
279         name_cell->signal_editing_started().connect (sigc::mem_fun (*this, &EditorRoutes::name_edit_started));
280
281         TreeViewColumn* name_column = _display.get_column (_name_column);
282
283         assert (name_column);
284
285         name_column->add_attribute (name_cell->property_editable(), _columns.name_editable);
286         name_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
287         name_column->set_expand(true);
288         name_column->set_min_width(50);
289
290         name_cell->property_editable() = true;
291         name_cell->signal_edited().connect (sigc::mem_fun (*this, &EditorRoutes::name_edit));
292
293         // Set the visible column cell renderer to radio toggle
294         CellRendererToggle* visible_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (_visible_column));
295
296         visible_cell->property_activatable() = true;
297         visible_cell->property_radio() = false;
298         visible_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRoutes::visible_changed));
299
300         TreeViewColumn* visible_col = dynamic_cast<TreeViewColumn*> (_display.get_column (_visible_column));
301         visible_col->set_expand(false);
302         visible_col->set_sizing(TREE_VIEW_COLUMN_FIXED);
303         visible_col->set_fixed_width(30);
304         visible_col->set_alignment(ALIGN_CENTER);
305
306         CellRendererToggle* active_cell = dynamic_cast<CellRendererToggle*> (_display.get_column_cell_renderer (_active_column));
307
308         active_cell->property_activatable() = true;
309         active_cell->property_radio() = false;
310         active_cell->signal_toggled().connect (sigc::mem_fun (*this, &EditorRoutes::active_changed));
311
312         TreeViewColumn* active_col = dynamic_cast<TreeViewColumn*> (_display.get_column (_active_column));
313         active_col->set_expand (false);
314         active_col->set_sizing (TREE_VIEW_COLUMN_FIXED);
315         active_col->set_fixed_width (30);
316         active_col->set_alignment (ALIGN_CENTER);
317         active_col->add_attribute (active_cell->property_visible(), _columns.no_vca);
318
319         _model->signal_row_deleted().connect (sigc::mem_fun (*this, &EditorRoutes::row_deleted));
320         _model->signal_rows_reordered().connect (sigc::mem_fun (*this, &EditorRoutes::reordered));
321
322         _display.signal_button_press_event().connect (sigc::mem_fun (*this, &EditorRoutes::button_press), false);
323         _display.signal_button_release_event().connect (sigc::mem_fun (*this, &EditorRoutes::button_release), false);
324         _scroller.signal_key_press_event().connect (sigc::mem_fun(*this, &EditorRoutes::key_press), false);
325
326         _scroller.signal_focus_in_event().connect (sigc::mem_fun (*this, &EditorRoutes::focus_in), false);
327         _scroller.signal_focus_out_event().connect (sigc::mem_fun (*this, &EditorRoutes::focus_out));
328
329         _display.signal_enter_notify_event().connect (sigc::mem_fun (*this, &EditorRoutes::enter_notify), false);
330         _display.signal_leave_notify_event().connect (sigc::mem_fun (*this, &EditorRoutes::leave_notify), false);
331
332         _display.set_enable_search (false);
333
334         Route::PluginSetup.connect_same_thread (*this, boost::bind (&EditorRoutes::plugin_setup, this, _1, _2, _3));
335 }
336
337 EditorRoutes::~EditorRoutes ()
338 {
339         delete _menu;
340 }
341
342 bool
343 EditorRoutes::focus_in (GdkEventFocus*)
344 {
345         Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
346
347         if (win) {
348                 old_focus = win->get_focus ();
349         } else {
350                 old_focus = 0;
351         }
352
353         name_editable = 0;
354
355         /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
356         return true;
357 }
358
359 bool
360 EditorRoutes::focus_out (GdkEventFocus*)
361 {
362         if (old_focus) {
363                 old_focus->grab_focus ();
364                 old_focus = 0;
365         }
366
367         return false;
368 }
369
370 bool
371 EditorRoutes::enter_notify (GdkEventCrossing*)
372 {
373         if (name_editable) {
374                 return true;
375         }
376
377         Keyboard::magic_widget_grab_focus ();
378         return false;
379 }
380
381 bool
382 EditorRoutes::leave_notify (GdkEventCrossing*)
383 {
384         if (old_focus) {
385                 old_focus->grab_focus ();
386                 old_focus = 0;
387         }
388
389         Keyboard::magic_widget_drop_focus ();
390         return false;
391 }
392
393 void
394 EditorRoutes::set_session (Session* s)
395 {
396         SessionHandlePtr::set_session (s);
397
398         initial_display ();
399
400         if (_session) {
401                 _session->SoloChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::solo_changed_so_update_mute, this), gui_context());
402                 _session->RecordStateChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
403
404                 /* TODO: check if these needs to be tied in with DisplaySuspender
405                  * Given that the UI is single-threaded and DisplaySuspender is only used
406                  * in loops in the UI thread all should be fine.
407                  */
408                 _session->BatchUpdateStart.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::suspend_redisplay, this), gui_context());
409                 _session->BatchUpdateEnd.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::resume_redisplay, this), gui_context());
410         }
411 }
412
413 void
414 EditorRoutes::on_input_active_changed (std::string const & path_string)
415 {
416         // Get the model row that has been toggled.
417         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
418
419         TimeAxisView* tv = row[_columns.tv];
420         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*> (tv);
421
422         if (rtv) {
423                 boost::shared_ptr<MidiTrack> mt;
424                 mt = rtv->midi_track();
425                 if (mt) {
426                         mt->set_input_active (!mt->input_active());
427                 }
428         }
429 }
430
431 void
432 EditorRoutes::on_tv_rec_enable_changed (std::string const & path_string)
433 {
434         // Get the model row that has been toggled.
435         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
436
437         TimeAxisView* tv = row[_columns.tv];
438         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
439
440         if (!stv || !stv->stripable()) {
441                 return;
442         }
443
444         boost::shared_ptr<AutomationControl> ac = stv->stripable()->rec_enable_control();
445
446         if (ac) {
447                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
448         }
449 }
450
451 void
452 EditorRoutes::on_tv_rec_safe_toggled (std::string const & path_string)
453 {
454         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
455         TimeAxisView* tv = row[_columns.tv];
456         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
457
458         if (!stv || !stv->stripable()) {
459                 return;
460         }
461
462         boost::shared_ptr<AutomationControl> ac (stv->stripable()->rec_safe_control());
463
464         if (ac) {
465                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
466         }
467 }
468
469 void
470 EditorRoutes::on_tv_mute_enable_toggled (std::string const & path_string)
471 {
472         // Get the model row that has been toggled.
473         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
474
475         TimeAxisView *tv = row[_columns.tv];
476         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
477
478         if (!stv || !stv->stripable()) {
479                 return;
480         }
481
482         boost::shared_ptr<AutomationControl> ac (stv->stripable()->mute_control());
483
484         if (ac) {
485                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
486         }
487 }
488
489 void
490 EditorRoutes::on_tv_solo_enable_toggled (std::string const & path_string)
491 {
492         // Get the model row that has been toggled.
493         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
494
495         TimeAxisView *tv = row[_columns.tv];
496         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
497
498         if (!stv || !stv->stripable()) {
499                 return;
500         }
501
502         boost::shared_ptr<AutomationControl> ac (stv->stripable()->solo_control());
503
504         if (ac) {
505                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
506         }
507 }
508
509 void
510 EditorRoutes::on_tv_solo_isolate_toggled (std::string const & path_string)
511 {
512         // Get the model row that has been toggled.
513         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
514
515         TimeAxisView *tv = row[_columns.tv];
516         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
517
518         if (!stv || !stv->stripable()) {
519                 return;
520         }
521
522         boost::shared_ptr<AutomationControl> ac (stv->stripable()->solo_isolate_control());
523
524         if (ac) {
525                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
526         }
527 }
528
529 void
530 EditorRoutes::on_tv_solo_safe_toggled (std::string const & path_string)
531 {
532         // Get the model row that has been toggled.
533         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
534
535         TimeAxisView *tv = row[_columns.tv];
536         StripableTimeAxisView* stv = dynamic_cast<StripableTimeAxisView*> (tv);
537
538         if (!stv || !stv->stripable()) {
539                 return;
540         }
541
542         boost::shared_ptr<AutomationControl> ac (stv->stripable()->solo_safe_control());
543
544         if (ac) {
545                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
546         }
547 }
548
549 void
550 EditorRoutes::build_menu ()
551 {
552         using namespace Menu_Helpers;
553         using namespace Gtk;
554
555         _menu = new Menu;
556
557         MenuList& items = _menu->items();
558         _menu->set_name ("ArdourContextMenu");
559
560         items.push_back (MenuElem (_("Show All"), sigc::mem_fun (*this, &EditorRoutes::show_all_routes)));
561         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun (*this, &EditorRoutes::hide_all_routes)));
562         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun (*this, &EditorRoutes::show_all_audiotracks)));
563         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun (*this, &EditorRoutes::hide_all_audiotracks)));
564         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &EditorRoutes::show_all_miditracks)));
565         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &EditorRoutes::hide_all_miditracks)));
566         items.push_back (MenuElem (_("Show All Busses"), sigc::mem_fun (*this, &EditorRoutes::show_all_audiobus)));
567         items.push_back (MenuElem (_("Hide All Busses"), sigc::mem_fun (*this, &EditorRoutes::hide_all_audiobus)));
568         items.push_back (MenuElem (_("Only Show Tracks with Regions Under Playhead"), sigc::mem_fun (*this, &EditorRoutes::show_tracks_with_regions_at_playhead)));
569 }
570
571 void
572 EditorRoutes::redisplay_real ()
573 {
574         TreeModel::Children rows = _model->children();
575         TreeModel::Children::iterator i;
576         uint32_t position;
577
578         /* n will be the count of tracks plus children (updated by TimeAxisView::show_at),
579          * so we will use that to know where to put things.
580          */
581         int n;
582
583         for (n = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
584                 TimeAxisView *tv = (*i)[_columns.tv];
585
586                 if (tv == 0) {
587                         // just a "title" row
588                         continue;
589                 }
590
591                 bool visible = tv->marked_for_display ();
592
593                 /* show or hide the TimeAxisView */
594                 if (visible) {
595                         position += tv->show_at (position, n, &_editor->edit_controls_vbox);
596                 } else {
597                         tv->hide ();
598                 }
599
600                 n++;
601         }
602
603         /* whenever we go idle, update the track view list to reflect the new order.
604          * we can't do this here, because we could mess up something that is traversing
605          * the track order and has caused a redisplay of the list.
606          */
607         Glib::signal_idle().connect (sigc::mem_fun (*_editor, &Editor::sync_track_view_list_and_routes));
608
609         _editor->reset_controls_layout_height (position);
610         _editor->reset_controls_layout_width ();
611         _editor->_full_canvas_height = position;
612
613         if ((_editor->vertical_adjustment.get_value() + _editor->_visible_canvas_height) > _editor->vertical_adjustment.get_upper()) {
614                 /*
615                  * We're increasing the size of the canvas while the bottom is visible.
616                  * We scroll down to keep in step with the controls layout.
617                  */
618                 _editor->vertical_adjustment.set_value (_editor->_full_canvas_height - _editor->_visible_canvas_height);
619         }
620 }
621
622 void
623 EditorRoutes::redisplay ()
624 {
625         if (!_session || _session->deletion_in_progress()) {
626                 return;
627         }
628
629         if (_no_redisplay) {
630                 _redisplay_on_resume = true;
631                 return;
632         }
633
634         // model deprecated g_atomic_int_exchange_and_add(, 1)
635         g_atomic_int_inc(const_cast<gint*>(&_redisplay_active));
636         if (!g_atomic_int_compare_and_exchange (const_cast<gint*>(&_redisplay_active), 1, 1)) {
637                 /* recursive re-display can happen if redisplay shows/hides a TrackView
638                  * which has children and their display status changes as result.
639                  */
640                 return;
641         }
642
643         redisplay_real ();
644
645         while (!g_atomic_int_compare_and_exchange (const_cast<gint*>(&_redisplay_active), 1, 0)) {
646                 g_atomic_int_set(const_cast<gint*>(&_redisplay_active), 1);
647                 redisplay_real ();
648         }
649 }
650
651 void
652 EditorRoutes::row_deleted (Gtk::TreeModel::Path const &)
653 {
654         if (!_session || _session->deletion_in_progress()) {
655                 return;
656         }
657         /* this happens as the second step of a DnD within the treeview, and
658          * when a route is actually removed. we don't differentiate between
659          * the two cases.
660          *
661          * note that the sync_presentation_info_from_treeview() step may not
662          * actually change any presentation info (e.g. the last track may be
663          * removed, so all other tracks keep the same presentation info), which
664          * means that no redisplay would happen. so we have to force a
665          * redisplay.
666          */
667
668         DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview row deleted\n");
669
670         DisplaySuspender ds;
671         sync_presentation_info_from_treeview ();
672 }
673
674 void
675 EditorRoutes::reordered (TreeModel::Path const &, TreeModel::iterator const &, int* /*what*/)
676 {
677         /* reordering implies that RID's will change, so
678            sync_presentation_info_from_treeview() will cause a redisplay.
679         */
680
681         DEBUG_TRACE (DEBUG::OrderKeys, "editor routes treeview reordered\n");
682         sync_presentation_info_from_treeview ();
683 }
684
685 void
686 EditorRoutes::visible_changed (std::string const & path)
687 {
688         if (_session && _session->deletion_in_progress()) {
689                 return;
690         }
691
692         DisplaySuspender ds;
693         TreeIter iter;
694
695         if ((iter = _model->get_iter (path))) {
696                 TimeAxisView* tv = (*iter)[_columns.tv];
697                 if (tv) {
698                         bool visible = (*iter)[_columns.visible];
699
700                         if (tv->set_marked_for_display (!visible)) {
701                                 update_visibility ();
702                         }
703                 }
704         }
705 }
706
707 void
708 EditorRoutes::active_changed (std::string const & path)
709 {
710         if (_session && _session->deletion_in_progress ()) {
711                 return;
712         }
713
714         Gtk::TreeModel::Row row = *_model->get_iter (path);
715         boost::shared_ptr<Stripable> stripable = row[_columns.stripable];
716         boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (stripable);
717         if (route) {
718                 bool const active = row[_columns.active];
719                 route->set_active (!active, this);
720         }
721 }
722
723 void
724 EditorRoutes::time_axis_views_added (list<TimeAxisView*> tavs)
725 {
726         PBD::Unwinder<bool> at (_adding_routes, true);
727         bool from_scratch = (_model->children().size() == 0);
728         Gtk::TreeModel::Children::iterator insert_iter = _model->children().end();
729
730         for (Gtk::TreeModel::Children::iterator it = _model->children().begin(); it != _model->children().end(); ++it) {
731
732                 boost::shared_ptr<Stripable> r = (*it)[_columns.stripable];
733
734                 if (r->presentation_info().order() == (tavs.front()->stripable()->presentation_info().order() + tavs.size())) {
735                         insert_iter = it;
736                         break;
737                 }
738         }
739
740         {
741                 PBD::Unwinder<bool> uw (_ignore_selection_change, true);
742                 _display.set_model (Glib::RefPtr<ListStore>());
743         }
744
745         for (list<TimeAxisView*>::iterator x = tavs.begin(); x != tavs.end(); ++x) {
746
747                 VCATimeAxisView* vtav = dynamic_cast<VCATimeAxisView*> (*x);
748                 RouteTimeAxisView* rtav = dynamic_cast<RouteTimeAxisView*> (*x);
749
750                 TreeModel::Row row = *(_model->insert (insert_iter));
751
752                 boost::shared_ptr<Stripable> stripable;
753                 boost::shared_ptr<MidiTrack> midi_trk;
754
755                 if (vtav) {
756
757                         stripable = vtav->vca();
758
759                         row[_columns.is_track] = false;
760                         row[_columns.is_input_active] = false;
761                         row[_columns.is_midi] = false;
762                         row[_columns.no_vca] = false;
763
764                 } else if (rtav) {
765
766                         stripable = rtav->route ();
767                         midi_trk= boost::dynamic_pointer_cast<MidiTrack> (stripable);
768
769                         row[_columns.is_track] = (boost::dynamic_pointer_cast<Track> (stripable) != 0);
770                         row[_columns.no_vca] = true;
771
772                         if (midi_trk) {
773                                 row[_columns.is_input_active] = midi_trk->input_active ();
774                                 row[_columns.is_midi] = true;
775                         } else {
776                                 row[_columns.is_input_active] = false;
777                                 row[_columns.is_midi] = false;
778                         }
779                 }
780
781                 if (!stripable) {
782                         continue;
783                 }
784
785                 row[_columns.text] = stripable->name();
786                 row[_columns.visible] = (*x)->marked_for_display();
787                 row[_columns.active] = true;
788                 row[_columns.tv] = *x;
789                 row[_columns.stripable] = stripable;
790                 row[_columns.mute_state] = RouteUI::mute_active_state (_session, stripable);
791                 row[_columns.solo_state] = RouteUI::solo_active_state (stripable);
792                 row[_columns.solo_visible] = !stripable->is_master ();
793                 row[_columns.solo_lock_iso_visible] = row[_columns.solo_visible] && row[_columns.no_vca];
794                 row[_columns.solo_isolate_state] = RouteUI::solo_isolate_active_state (stripable);
795                 row[_columns.solo_safe_state] = RouteUI::solo_safe_active_state (stripable);
796                 row[_columns.name_editable] = true;
797
798                 boost::weak_ptr<Stripable> ws (stripable);
799
800                 /* for now, we need both of these. PropertyChanged covers on
801                  * pre-defined, "global" things of interest to a
802                  * UI. gui_changed covers arbitrary, un-enumerated, un-typed
803                  * changes that may only be of interest to a particular
804                  * UI (e.g. track-height is not of any relevant to OSC)
805                  */
806
807                 stripable->gui_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::handle_gui_changes, this, _1, _2), gui_context());
808                 stripable->PropertyChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::route_property_changed, this, _1, ws), gui_context());
809                 stripable->presentation_info().PropertyChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::route_property_changed, this, _1, ws), gui_context());
810
811                 if (boost::dynamic_pointer_cast<Track> (stripable)) {
812                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (stripable);
813                         t->rec_enable_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
814                         t->rec_safe_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
815                 }
816
817                 if (midi_trk) {
818                         midi_trk->StepEditStatusChange.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
819                         midi_trk->InputActiveChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_input_active_display, this), gui_context());
820                 }
821
822                 boost::shared_ptr<AutomationControl> ac;
823
824                 if ((ac = stripable->mute_control()) != 0) {
825                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_mute_display, this), gui_context());
826                 }
827                 if ((ac = stripable->solo_control()) != 0) {
828                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_display, this), gui_context());
829                 }
830                 if ((ac = stripable->solo_isolate_control()) != 0) {
831                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_isolate_display, this), gui_context());
832                 }
833                 if ((ac = stripable->solo_safe_control()) != 0) {
834                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_safe_display, this), gui_context());
835                 }
836
837                 if (rtav) {
838                         rtav->route()->active_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_active_display, this), gui_context ());
839                 }
840         }
841
842         update_rec_display ();
843         update_mute_display ();
844         update_solo_display ();
845         update_solo_isolate_display ();
846         update_solo_safe_display ();
847         update_input_active_display ();
848         update_active_display ();
849
850         {
851                 PBD::Unwinder<bool> uw (_ignore_selection_change, true);
852                 _display.set_model (_model);
853         }
854
855         /* now update route order keys from the treeview/track display order */
856
857         if (!from_scratch) {
858                 sync_presentation_info_from_treeview ();
859         }
860
861         redisplay ();
862 }
863
864 void
865 EditorRoutes::handle_gui_changes (string const & what, void*)
866 {
867         if (_adding_routes) {
868                 return;
869         }
870
871         if (what == "track_height") {
872                 /* Optional :make tracks change height while it happens, instead
873                    of on first-idle
874                 */
875                 redisplay ();
876         }
877
878         if (what == "visible_tracks") {
879                 redisplay ();
880         }
881 }
882
883 void
884 EditorRoutes::route_removed (TimeAxisView *tv)
885 {
886         ENSURE_GUI_THREAD (*this, &EditorRoutes::route_removed, tv)
887
888         TreeModel::Children rows = _model->children();
889         TreeModel::Children::iterator ri;
890
891         PBD::Unwinder<bool> uw (_ignore_selection_change, true);
892
893         for (ri = rows.begin(); ri != rows.end(); ++ri) {
894                 if ((*ri)[_columns.tv] == tv) {
895                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
896                         _model->erase (ri);
897                         break;
898                 }
899         }
900 }
901
902 void
903 EditorRoutes::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> s)
904 {
905         if (!what_changed.contains (ARDOUR::Properties::hidden) && !what_changed.contains (ARDOUR::Properties::name)) {
906                 return;
907         }
908
909         if (_adding_routes) {
910                 return;
911         }
912
913         boost::shared_ptr<Stripable> stripable = s.lock ();
914
915         if (!stripable) {
916                 return;
917         }
918
919         TreeModel::Children rows = _model->children();
920         TreeModel::Children::iterator i;
921
922         for (i = rows.begin(); i != rows.end(); ++i) {
923
924                 boost::shared_ptr<Stripable> ss = (*i)[_columns.stripable];
925
926                 if (ss == stripable) {
927
928                         if (what_changed.contains (ARDOUR::Properties::name)) {
929                                 (*i)[_columns.text] = stripable->name();
930                                 break;
931                         }
932
933                         if (what_changed.contains (ARDOUR::Properties::hidden)) {
934                                 (*i)[_columns.visible] = !stripable->presentation_info().hidden();
935                                 redisplay ();
936
937                         }
938
939                         break;
940                 }
941         }
942 }
943
944 void
945 EditorRoutes::update_active_display ()
946 {
947         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
948                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
949         }
950 }
951
952 void
953 EditorRoutes::update_visibility ()
954 {
955         TreeModel::Children rows = _model->children();
956         TreeModel::Children::iterator i;
957
958         DisplaySuspender ds;
959
960         for (i = rows.begin(); i != rows.end(); ++i) {
961                 TimeAxisView *tv = (*i)[_columns.tv];
962                 (*i)[_columns.visible] = tv->marked_for_display ();
963         }
964
965         /* force route order keys catch up with visibility changes */
966
967         sync_presentation_info_from_treeview ();
968 }
969
970 void
971 EditorRoutes::hide_track_in_display (TimeAxisView& tv)
972 {
973         TreeModel::Children rows = _model->children();
974         TreeModel::Children::iterator i;
975
976         for (i = rows.begin(); i != rows.end(); ++i) {
977                 if ((*i)[_columns.tv] == &tv) {
978                         tv.set_marked_for_display (false);
979                         (*i)[_columns.visible] = false;
980                         redisplay ();
981                         break;
982                 }
983         }
984 }
985
986 void
987 EditorRoutes::show_track_in_display (TimeAxisView& tv)
988 {
989         TreeModel::Children rows = _model->children();
990         TreeModel::Children::iterator i;
991
992
993         for (i = rows.begin(); i != rows.end(); ++i) {
994                 if ((*i)[_columns.tv] == &tv) {
995                         tv.set_marked_for_display (true);
996                         (*i)[_columns.visible] = true;
997                         redisplay ();
998                         break;
999                 }
1000         }
1001 }
1002
1003 void
1004 EditorRoutes::sync_presentation_info_from_treeview ()
1005 {
1006         if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
1007                 return;
1008         }
1009
1010         TreeModel::Children rows = _model->children();
1011
1012         if (rows.empty()) {
1013                 return;
1014         }
1015
1016         DEBUG_TRACE (DEBUG::OrderKeys, "editor sync presentation info from treeview\n");
1017
1018         bool change = false;
1019         PresentationInfo::order_t order = 0;
1020
1021         PresentationInfo::ChangeSuspender cs;
1022
1023         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri) {
1024                 boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1025                 bool visible = (*ri)[_columns.visible];
1026
1027 #ifndef NDEBUG // these should not exist in the treeview
1028                 assert (stripable);
1029                 if (stripable->is_monitor() || stripable->is_auditioner()) {
1030                         assert (0);
1031                         continue;
1032                 }
1033 #endif
1034
1035                 stripable->presentation_info().set_hidden (!visible);
1036
1037                 if (order != stripable->presentation_info().order()) {
1038                         stripable->set_presentation_order (order);
1039                         change = true;
1040                 }
1041                 ++order;
1042         }
1043
1044         change |= _session->ensure_stripable_sort_order ();
1045
1046         if (change) {
1047                 _session->set_dirty();
1048         }
1049 }
1050
1051 void
1052 EditorRoutes::sync_treeview_from_presentation_info (PropertyChange const & what_changed)
1053 {
1054         /* Some route order key(s) have been changed, make sure that
1055            we update out tree/list model and GUI to reflect the change.
1056         */
1057
1058         if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
1059                 return;
1060         }
1061
1062         DEBUG_TRACE (DEBUG::OrderKeys, "editor sync model from presentation info.\n");
1063
1064         PropertyChange hidden_or_order;
1065         hidden_or_order.add (Properties::hidden);
1066         hidden_or_order.add (Properties::order);
1067
1068         TreeModel::Children rows = _model->children();
1069
1070         bool changed = false;
1071
1072         if (what_changed.contains (hidden_or_order)) {
1073                 vector<int> neworder;
1074                 uint32_t old_order = 0;
1075
1076                 if (rows.empty()) {
1077                         return;
1078                 }
1079
1080                 TreeOrderKeys sorted;
1081                 for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
1082                         boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1083                         /* use global order */
1084                         sorted.push_back (TreeOrderKey (old_order, stripable));
1085                 }
1086
1087                 TreeOrderKeySorter cmp;
1088
1089                 sort (sorted.begin(), sorted.end(), cmp);
1090                 neworder.assign (sorted.size(), 0);
1091
1092                 uint32_t n = 0;
1093
1094                 for (TreeOrderKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
1095
1096                         neworder[n] = sr->old_display_order;
1097
1098                         if (sr->old_display_order != n) {
1099                                 changed = true;
1100                         }
1101                 }
1102
1103                 if (changed) {
1104                         Unwinder<bool> uw (_ignore_reorder, true);
1105                         /* prevent traverse_cells: assertion 'row_path != NULL'
1106                          * in case of DnD re-order: row-removed + row-inserted.
1107                          *
1108                          * The rows (stripables) are not actually removed from the model,
1109                          * but only from the display in the DnDTreeView.
1110                          * ->reorder() will fail to find the row_path.
1111                          * (re-order drag -> remove row -> sync PI from TV -> notify -> sync TV from PI -> crash)
1112                          */
1113                         Unwinder<bool> uw2 (_ignore_selection_change, true);
1114
1115                         _display.unset_model();
1116                         _model->reorder (neworder);
1117                         _display.set_model (_model);
1118                 }
1119         }
1120
1121         if (changed || what_changed.contains (Properties::selected)) {
1122                 /* by the time this is invoked, the GUI Selection model has
1123                  * already updated itself.
1124                  */
1125                 PBD::Unwinder<bool> uw (_ignore_selection_change, true);
1126
1127                 /* set the treeview model selection state */
1128                 for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri) {
1129                         boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1130                         if (stripable && stripable->is_selected()) {
1131                                 _display.get_selection()->select (*ri);
1132                         } else {
1133                                 _display.get_selection()->unselect (*ri);
1134                         }
1135                 }
1136         }
1137
1138         redisplay ();
1139 }
1140
1141 void
1142 EditorRoutes::hide_all_tracks (bool /*with_select*/)
1143 {
1144         TreeModel::Children rows = _model->children();
1145         TreeModel::Children::iterator i;
1146
1147         DisplaySuspender ds;
1148
1149         for (i = rows.begin(); i != rows.end(); ++i) {
1150
1151                 TreeModel::Row row = (*i);
1152                 TimeAxisView *tv = row[_columns.tv];
1153
1154                 if (tv == 0) {
1155                         continue;
1156                 }
1157
1158                 row[_columns.visible] = false;
1159         }
1160 }
1161
1162 void
1163 EditorRoutes::set_all_tracks_visibility (bool yn)
1164 {
1165         TreeModel::Children rows = _model->children();
1166         TreeModel::Children::iterator i;
1167
1168         DisplaySuspender ds;
1169
1170         for (i = rows.begin(); i != rows.end(); ++i) {
1171
1172                 TreeModel::Row row = (*i);
1173                 TimeAxisView* tv = row[_columns.tv];
1174
1175                 if (tv == 0) {
1176                         continue;
1177                 }
1178
1179                 tv->set_marked_for_display (yn);
1180                 (*i)[_columns.visible] = yn;
1181         }
1182
1183         /* force route order keys catch up with visibility changes
1184          */
1185
1186         sync_presentation_info_from_treeview ();
1187 }
1188
1189 void
1190 EditorRoutes::set_all_audio_midi_visibility (int tracks, bool yn)
1191 {
1192         TreeModel::Children rows = _model->children();
1193         TreeModel::Children::iterator i;
1194
1195         DisplaySuspender ds;
1196
1197         for (i = rows.begin(); i != rows.end(); ++i) {
1198
1199                 TreeModel::Row row = (*i);
1200                 TimeAxisView* tv = row[_columns.tv];
1201
1202                 AudioTimeAxisView* atv;
1203                 MidiTimeAxisView* mtv;
1204
1205                 if (tv == 0) {
1206                         continue;
1207                 }
1208
1209                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
1210                         switch (tracks) {
1211                         case 0:
1212                                 atv->set_marked_for_display (yn);
1213                                 (*i)[_columns.visible] = yn;
1214                                 break;
1215
1216                         case 1:
1217                                 if (atv->is_audio_track()) {
1218                                         atv->set_marked_for_display (yn);
1219                                         (*i)[_columns.visible] = yn;
1220                                 }
1221                                 break;
1222
1223                         case 2:
1224                                 if (!atv->is_audio_track()) {
1225                                         atv->set_marked_for_display (yn);
1226                                         (*i)[_columns.visible] = yn;
1227                                 }
1228                                 break;
1229                         }
1230                 }
1231                 else if ((mtv = dynamic_cast<MidiTimeAxisView*>(tv)) != 0) {
1232                         switch (tracks) {
1233                         case 0:
1234                                 mtv->set_marked_for_display (yn);
1235                                 (*i)[_columns.visible] = yn;
1236                                 break;
1237
1238                         case 3:
1239                                 if (mtv->is_midi_track()) {
1240                                         mtv->set_marked_for_display (yn);
1241                                         (*i)[_columns.visible] = yn;
1242                                 }
1243                                 break;
1244                         }
1245                 }
1246         }
1247
1248         /* force route order keys catch up with visibility changes
1249          */
1250
1251         sync_presentation_info_from_treeview ();
1252 }
1253
1254 void
1255 EditorRoutes::hide_all_routes ()
1256 {
1257         set_all_tracks_visibility (false);
1258 }
1259
1260 void
1261 EditorRoutes::show_all_routes ()
1262 {
1263         set_all_tracks_visibility (true);
1264 }
1265
1266 void
1267 EditorRoutes::show_all_audiotracks()
1268 {
1269         set_all_audio_midi_visibility (1, true);
1270 }
1271 void
1272 EditorRoutes::hide_all_audiotracks ()
1273 {
1274         set_all_audio_midi_visibility (1, false);
1275 }
1276
1277 void
1278 EditorRoutes::show_all_audiobus ()
1279 {
1280         set_all_audio_midi_visibility (2, true);
1281 }
1282 void
1283 EditorRoutes::hide_all_audiobus ()
1284 {
1285         set_all_audio_midi_visibility (2, false);
1286 }
1287
1288 void
1289 EditorRoutes::show_all_miditracks()
1290 {
1291         set_all_audio_midi_visibility (3, true);
1292 }
1293 void
1294 EditorRoutes::hide_all_miditracks ()
1295 {
1296         set_all_audio_midi_visibility (3, false);
1297 }
1298
1299 bool
1300 EditorRoutes::key_press (GdkEventKey* ev)
1301 {
1302         TreeViewColumn *col;
1303         boost::shared_ptr<RouteList> rl (new RouteList);
1304         TreePath path;
1305
1306         switch (ev->keyval) {
1307                 case GDK_Tab:
1308                 case GDK_ISO_Left_Tab:
1309
1310                         /* If we appear to be editing something, leave that cleanly and appropriately. */
1311                         if (name_editable) {
1312                                 name_editable->editing_done ();
1313                                 name_editable = 0;
1314                         }
1315
1316                         col = _display.get_column (_name_column); // select&focus on name column
1317
1318                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
1319                                 treeview_select_previous (_display, _model, col);
1320                         } else {
1321                                 treeview_select_next (_display, _model, col);
1322                         }
1323
1324                         return true;
1325                         break;
1326
1327                 case 'm':
1328                         if (get_relevant_routes (rl)) {
1329                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::mute_control), rl->front()->muted() ? 0.0 : 1.0, Controllable::NoGroup);
1330                         }
1331                         return true;
1332                         break;
1333
1334                 case 's':
1335                         if (get_relevant_routes (rl)) {
1336                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::solo_control), rl->front()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
1337                         }
1338                         return true;
1339                         break;
1340
1341                 case 'r':
1342                         if (get_relevant_routes (rl)) {
1343                                 for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
1344                                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (*r);
1345                                         if (t) {
1346                                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::rec_enable_control), !t->rec_enable_control()->get_value(), Controllable::NoGroup);
1347                                                 break;
1348                                         }
1349                                 }
1350                         }
1351                         break;
1352
1353                 default:
1354                         break;
1355         }
1356
1357         return false;
1358 }
1359
1360 bool
1361 EditorRoutes::get_relevant_routes (boost::shared_ptr<RouteList> rl)
1362 {
1363         TimeAxisView* tv;
1364         RouteTimeAxisView* rtv;
1365         RefPtr<TreeSelection> selection = _display.get_selection();
1366         TreePath path;
1367         TreeIter iter;
1368
1369         if (selection->count_selected_rows() != 0) {
1370
1371                 /* use selection */
1372
1373                 RefPtr<TreeModel> tm = RefPtr<TreeModel>::cast_dynamic (_model);
1374                 iter = selection->get_selected (tm);
1375
1376         } else {
1377                 /* use mouse pointer */
1378
1379                 int x, y;
1380                 int bx, by;
1381
1382                 _display.get_pointer (x, y);
1383                 _display.convert_widget_to_bin_window_coords (x, y, bx, by);
1384
1385                 if (_display.get_path_at_pos (bx, by, path)) {
1386                         iter = _model->get_iter (path);
1387                 }
1388         }
1389
1390         if (iter) {
1391                 tv = (*iter)[_columns.tv];
1392                 if (tv) {
1393                         rtv = dynamic_cast<RouteTimeAxisView*>(tv);
1394                         if (rtv) {
1395                                 rl->push_back (rtv->route());
1396                         }
1397                 }
1398         }
1399
1400         return !rl->empty();
1401 }
1402
1403 bool
1404 EditorRoutes::select_function(const Glib::RefPtr<Gtk::TreeModel>&, const Gtk::TreeModel::Path&, bool)
1405 {
1406         return !column_does_not_select;
1407 }
1408
1409 bool
1410 EditorRoutes::button_release (GdkEventButton*)
1411 {
1412         column_does_not_select = false;
1413         return false;
1414 }
1415
1416 bool
1417 EditorRoutes::button_press (GdkEventButton* ev)
1418 {
1419         if (Keyboard::is_context_menu_event (ev)) {
1420                 if (_menu == 0) {
1421                         build_menu ();
1422                 }
1423                 _menu->popup (ev->button, ev->time);
1424                 return true;
1425         }
1426
1427         TreeModel::Path path;
1428         TreeViewColumn *tvc;
1429         int cell_x;
1430         int cell_y;
1431
1432         if (!_display.get_path_at_pos ((int) ev->x, (int) ev->y, path, tvc, cell_x, cell_y)) {
1433                 /* cancel selection */
1434                 _display.get_selection()->unselect_all ();
1435                 /* end any editing by grabbing focus */
1436                 _display.grab_focus ();
1437                 return true;
1438         }
1439
1440         if ((tvc == rec_state_column) ||
1441             (tvc == rec_safe_column) ||
1442             (tvc == input_active_column) ||
1443             (tvc == mute_state_column) ||
1444             (tvc == solo_state_column) ||
1445             (tvc == solo_safe_state_column) ||
1446             (tvc == solo_isolate_state_column) ||
1447             (tvc == visible_column) ||
1448             (tvc == active_column)) {
1449                 column_does_not_select = true;
1450         }
1451
1452         //Scroll editor canvas to selected track
1453         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1454
1455                 Gtk::TreeModel::Row row = *_model->get_iter (path);
1456                 TimeAxisView *tv = row[_columns.tv];
1457
1458                 if (tv) {
1459                         _editor->ensure_time_axis_view_is_visible (*tv, true);
1460                 }
1461         }
1462
1463         return false;
1464 }
1465
1466 void
1467 EditorRoutes::selection_changed ()
1468 {
1469         if (_ignore_selection_change || column_does_not_select) {
1470                 return;
1471         }
1472
1473         _editor->begin_reversible_selection_op (X_("Select Track from Route List"));
1474
1475         if (_display.get_selection()->count_selected_rows() > 0) {
1476
1477                 TreeIter iter;
1478                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1479                 TrackViewList selected;
1480
1481                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1482
1483                         if ((iter = _model->get_iter (*i))) {
1484
1485                                 TimeAxisView* tv = (*iter)[_columns.tv];
1486                                 selected.push_back (tv);
1487                         }
1488
1489                 }
1490
1491                 _editor->get_selection().set (selected);
1492                 _editor->ensure_time_axis_view_is_visible (*(selected.front()), true);
1493
1494         } else {
1495                 _editor->get_selection().clear_tracks ();
1496         }
1497
1498         _editor->commit_reversible_selection_op ();
1499 }
1500
1501 void
1502 EditorRoutes::initial_display ()
1503 {
1504
1505         if (!_session) {
1506                 clear ();
1507                 return;
1508         }
1509
1510         DisplaySuspender ds;
1511         _model->clear ();
1512
1513         StripableList s;
1514
1515         _session->get_stripables (s);
1516         _editor->add_stripables (s);
1517
1518         sync_treeview_from_presentation_info (Properties::order);
1519 }
1520
1521 struct ViewStripable {
1522         TimeAxisView* tav;
1523         boost::shared_ptr<Stripable> stripable;
1524
1525         ViewStripable (TimeAxisView* t, boost::shared_ptr<Stripable> s)
1526                 : tav (t), stripable (s) {}
1527 };
1528
1529 void
1530 EditorRoutes::move_selected_tracks (bool up)
1531 {
1532         TimeAxisView* scroll_to = 0;
1533         StripableList sl;
1534         _session->get_stripables (sl);
1535
1536         if (sl.size() < 2) {
1537                 /* nope */
1538                 return;
1539         }
1540
1541         sl.sort (Stripable::Sorter());
1542
1543         std::list<ViewStripable> view_stripables;
1544
1545         /* build a list that includes time axis view information */
1546
1547         for (StripableList::const_iterator sli = sl.begin(); sli != sl.end(); ++sli) {
1548                 TimeAxisView* tv = _editor->time_axis_view_from_stripable (*sli);
1549                 view_stripables.push_back (ViewStripable (tv, *sli));
1550         }
1551
1552         /* for each selected stripable, move it above or below the adjacent
1553          * stripable that has a time-axis view representation here. If there's
1554          * no such representation, then
1555          */
1556
1557         list<ViewStripable>::iterator unselected_neighbour;
1558         list<ViewStripable>::iterator vsi;
1559
1560         {
1561                 PresentationInfo::ChangeSuspender cs;
1562
1563                 if (up) {
1564                         unselected_neighbour = view_stripables.end ();
1565                         vsi = view_stripables.begin();
1566
1567                         while (vsi != view_stripables.end()) {
1568
1569                                 if (vsi->stripable->is_selected()) {
1570
1571                                         if (unselected_neighbour != view_stripables.end()) {
1572
1573                                                 PresentationInfo::order_t unselected_neighbour_order = unselected_neighbour->stripable->presentation_info().order();
1574                                                 PresentationInfo::order_t my_order = vsi->stripable->presentation_info().order();
1575
1576                                                 unselected_neighbour->stripable->set_presentation_order (my_order);
1577                                                 vsi->stripable->set_presentation_order (unselected_neighbour_order);
1578
1579                                                 if (!scroll_to) {
1580                                                         scroll_to = vsi->tav;
1581                                                 }
1582                                         }
1583
1584                                 } else {
1585
1586                                         if (vsi->tav) {
1587                                                 unselected_neighbour = vsi;
1588                                         }
1589
1590                                 }
1591
1592                                 ++vsi;
1593                         }
1594
1595                 } else {
1596
1597                         unselected_neighbour = view_stripables.end();
1598                         vsi = unselected_neighbour;
1599
1600                         do {
1601
1602                                 --vsi;
1603
1604                                 if (vsi->stripable->is_selected()) {
1605
1606                                         if (unselected_neighbour != view_stripables.end()) {
1607
1608                                                 PresentationInfo::order_t unselected_neighbour_order = unselected_neighbour->stripable->presentation_info().order();
1609                                                 PresentationInfo::order_t my_order = vsi->stripable->presentation_info().order();
1610
1611                                                 unselected_neighbour->stripable->set_presentation_order (my_order);
1612                                                 vsi->stripable->set_presentation_order (unselected_neighbour_order);
1613
1614                                                 if (!scroll_to) {
1615                                                         scroll_to = vsi->tav;
1616                                                 }
1617                                         }
1618
1619                                 } else {
1620
1621                                         if (vsi->tav) {
1622                                                 unselected_neighbour = vsi;
1623                                         }
1624
1625                                 }
1626
1627                         } while (vsi != view_stripables.begin());
1628                 }
1629         }
1630
1631         if (scroll_to) {
1632                 _editor->ensure_time_axis_view_is_visible (*scroll_to, false);
1633         }
1634 }
1635
1636 void
1637 EditorRoutes::update_input_active_display ()
1638 {
1639         TreeModel::Children rows = _model->children();
1640         TreeModel::Children::iterator i;
1641
1642         for (i = rows.begin(); i != rows.end(); ++i) {
1643                 boost::shared_ptr<Stripable> stripable = (*i)[_columns.stripable];
1644
1645                 if (boost::dynamic_pointer_cast<Track> (stripable)) {
1646                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (stripable);
1647
1648                         if (mt) {
1649                                 (*i)[_columns.is_input_active] = mt->input_active();
1650                         }
1651                 }
1652         }
1653 }
1654
1655 void
1656 EditorRoutes::update_rec_display ()
1657 {
1658         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1659                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1660         }
1661 }
1662
1663 bool
1664 EditorRoutes::idle_update_mute_rec_solo_etc()
1665 {
1666         g_atomic_int_set (const_cast<gint*>(&_queue_tv_update), 0);
1667         TreeModel::Children rows = _model->children();
1668         TreeModel::Children::iterator i;
1669
1670         for (i = rows.begin(); i != rows.end(); ++i) {
1671                 boost::shared_ptr<Stripable> stripable = (*i)[_columns.stripable];
1672                 boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (stripable);
1673                 (*i)[_columns.mute_state] = RouteUI::mute_active_state (_session, stripable);
1674                 (*i)[_columns.solo_state] = RouteUI::solo_active_state (stripable);
1675                 (*i)[_columns.solo_isolate_state] = RouteUI::solo_isolate_active_state (stripable) ? 1 : 0;
1676                 (*i)[_columns.solo_safe_state] = RouteUI::solo_safe_active_state (stripable) ? 1 : 0;
1677                 if (route) {
1678                         (*i)[_columns.active] = route->active ();
1679                 } else {
1680                         (*i)[_columns.active] = true;
1681                 }
1682
1683                 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track>(route));
1684
1685                 if (trk) {
1686                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (route);
1687
1688                         if (trk->rec_enable_control()->get_value()) {
1689                                 if (_session->record_status() == Session::Recording) {
1690                                         (*i)[_columns.rec_state] = 1;
1691                                 } else {
1692                                         (*i)[_columns.rec_state] = 2;
1693                                 }
1694                         } else if (mt && mt->step_editing()) {
1695                                 (*i)[_columns.rec_state] = 3;
1696                         } else {
1697                                 (*i)[_columns.rec_state] = 0;
1698                         }
1699
1700                         (*i)[_columns.rec_safe] = trk->rec_safe_control()->get_value();
1701                         (*i)[_columns.name_editable] = !trk->rec_enable_control()->get_value();
1702                 }
1703         }
1704
1705         return false; // do not call again (until needed)
1706 }
1707
1708
1709 void
1710 EditorRoutes::update_mute_display ()
1711 {
1712         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1713                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1714         }
1715 }
1716
1717 void
1718 EditorRoutes::update_solo_display ()
1719 {
1720         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1721                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1722         }
1723 }
1724
1725 void
1726 EditorRoutes::update_solo_isolate_display ()
1727 {
1728         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1729                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1730         }
1731 }
1732
1733 void
1734 EditorRoutes::update_solo_safe_display ()
1735 {
1736         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1737                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1738         }
1739 }
1740
1741 list<TimeAxisView*>
1742 EditorRoutes::views () const
1743 {
1744         list<TimeAxisView*> v;
1745         for (TreeModel::Children::iterator i = _model->children().begin(); i != _model->children().end(); ++i) {
1746                 v.push_back ((*i)[_columns.tv]);
1747         }
1748
1749         return v;
1750 }
1751
1752 void
1753 EditorRoutes::clear ()
1754 {
1755         PBD::Unwinder<bool> uw (_ignore_selection_change, true);
1756         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1757         _model->clear ();
1758         _display.set_model (_model);
1759 }
1760
1761 void
1762 EditorRoutes::name_edit_started (CellEditable* ce, const Glib::ustring&)
1763 {
1764         name_editable = ce;
1765
1766         /* give it a special name */
1767
1768         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1769
1770         if (e) {
1771                 e->set_name (X_("RouteNameEditorEntry"));
1772         }
1773 }
1774
1775 void
1776 EditorRoutes::name_edit (std::string const & path, std::string const & new_text)
1777 {
1778         name_editable = 0;
1779
1780         TreeIter iter = _model->get_iter (path);
1781
1782         if (!iter) {
1783                 return;
1784         }
1785
1786         boost::shared_ptr<Stripable> stripable = (*iter)[_columns.stripable];
1787
1788         if (stripable && stripable->name() != new_text) {
1789                 stripable->set_name (new_text);
1790         }
1791 }
1792
1793 void
1794 EditorRoutes::solo_changed_so_update_mute ()
1795 {
1796         update_mute_display ();
1797 }
1798
1799 void
1800 EditorRoutes::show_tracks_with_regions_at_playhead ()
1801 {
1802         boost::shared_ptr<RouteList> const r = _session->get_routes_with_regions_at (_session->transport_sample ());
1803
1804         set<TimeAxisView*> show;
1805         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
1806                 TimeAxisView* tav = _editor->time_axis_view_from_stripable (*i);
1807                 if (tav) {
1808                         show.insert (tav);
1809                 }
1810         }
1811
1812         DisplaySuspender ds;
1813
1814         TreeModel::Children rows = _model->children ();
1815         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
1816                 TimeAxisView* tv = (*i)[_columns.tv];
1817                 bool to_show = (show.find (tv) != show.end());
1818
1819                 tv->set_marked_for_display (to_show);
1820                 (*i)[_columns.visible] = to_show;
1821         }
1822
1823         /* force route order keys catch up with visibility changes
1824          */
1825
1826         sync_presentation_info_from_treeview ();
1827 }
1828
1829 int
1830 EditorRoutes::plugin_setup (boost::shared_ptr<Route> r, boost::shared_ptr<PluginInsert> pi, ARDOUR::Route::PluginSetupOptions flags)
1831 {
1832         PluginSetupDialog psd (r, pi, flags);
1833         int rv = psd.run ();
1834         return rv + (psd.fan_out() ? 4 : 0);
1835 }