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