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