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