update use of PresentationInfo/Stripable API
[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 "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         , _no_redisplay (false)
81         , _adding_routes (false)
82         , _route_deletion_in_progress (false)
83         , _redisplay_on_resume (false)
84         , _redisplay_active (0)
85         , _queue_tv_update (0)
86         , _menu (0)
87         , old_focus (0)
88         , selection_countdown (0)
89         , name_editable (0)
90 {
91         static const int column_width = 22;
92
93         _scroller.add (_display);
94         _scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
95
96         _model = ListStore::create (_columns);
97         _display.set_model (_model);
98
99         // Record enable toggle
100         CellRendererPixbufMulti* rec_col_renderer = manage (new CellRendererPixbufMulti());
101
102         rec_col_renderer->set_pixbuf (0, ::get_icon("record-normal-disabled"));
103         rec_col_renderer->set_pixbuf (1, ::get_icon("record-normal-in-progress"));
104         rec_col_renderer->set_pixbuf (2, ::get_icon("record-normal-enabled"));
105         rec_col_renderer->set_pixbuf (3, ::get_icon("record-step"));
106         rec_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_rec_enable_changed));
107
108         TreeViewColumn* rec_state_column = manage (new TreeViewColumn("R", *rec_col_renderer));
109
110         rec_state_column->add_attribute(rec_col_renderer->property_state(), _columns.rec_state);
111         rec_state_column->add_attribute(rec_col_renderer->property_visible(), _columns.is_track);
112
113         rec_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
114         rec_state_column->set_alignment(ALIGN_CENTER);
115         rec_state_column->set_expand(false);
116         rec_state_column->set_fixed_width(column_width);
117
118
119         // Record safe toggle
120         CellRendererPixbufMulti* rec_safe_renderer = manage (new CellRendererPixbufMulti ());
121
122         rec_safe_renderer->set_pixbuf (0, ::get_icon("rec-safe-disabled"));
123         rec_safe_renderer->set_pixbuf (1, ::get_icon("rec-safe-enabled"));
124         rec_safe_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_rec_safe_toggled));
125
126         TreeViewColumn* rec_safe_column = manage (new TreeViewColumn(_("RS"), *rec_safe_renderer));
127         rec_safe_column->add_attribute(rec_safe_renderer->property_state(), _columns.rec_safe);
128         rec_safe_column->add_attribute(rec_safe_renderer->property_visible(), _columns.is_track);
129         rec_safe_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
130         rec_safe_column->set_alignment(ALIGN_CENTER);
131         rec_safe_column->set_expand(false);
132         rec_safe_column->set_fixed_width(column_width);
133
134
135         // MIDI Input Active
136
137         CellRendererPixbufMulti* input_active_col_renderer = manage (new CellRendererPixbufMulti());
138         input_active_col_renderer->set_pixbuf (0, ::get_icon("midi-input-inactive"));
139         input_active_col_renderer->set_pixbuf (1, ::get_icon("midi-input-active"));
140         input_active_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_input_active_changed));
141
142         TreeViewColumn* input_active_column = manage (new TreeViewColumn ("I", *input_active_col_renderer));
143
144         input_active_column->add_attribute(input_active_col_renderer->property_state(), _columns.is_input_active);
145         input_active_column->add_attribute (input_active_col_renderer->property_visible(), _columns.is_midi);
146
147         input_active_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
148         input_active_column->set_alignment(ALIGN_CENTER);
149         input_active_column->set_expand(false);
150         input_active_column->set_fixed_width(column_width);
151
152         // Mute enable toggle
153         CellRendererPixbufMulti* mute_col_renderer = manage (new CellRendererPixbufMulti());
154
155         mute_col_renderer->set_pixbuf (Gtkmm2ext::Off, ::get_icon("mute-disabled"));
156         mute_col_renderer->set_pixbuf (Gtkmm2ext::ImplicitActive, ::get_icon("muted-by-others"));
157         mute_col_renderer->set_pixbuf (Gtkmm2ext::ExplicitActive, ::get_icon("mute-enabled"));
158         mute_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_mute_enable_toggled));
159
160         TreeViewColumn* mute_state_column = manage (new TreeViewColumn("M", *mute_col_renderer));
161
162         mute_state_column->add_attribute(mute_col_renderer->property_state(), _columns.mute_state);
163         mute_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
164         mute_state_column->set_alignment(ALIGN_CENTER);
165         mute_state_column->set_expand(false);
166         mute_state_column->set_fixed_width(15);
167
168         // Solo enable toggle
169         CellRendererPixbufMulti* solo_col_renderer = manage (new CellRendererPixbufMulti());
170
171         solo_col_renderer->set_pixbuf (Gtkmm2ext::Off, ::get_icon("solo-disabled"));
172         solo_col_renderer->set_pixbuf (Gtkmm2ext::ExplicitActive, ::get_icon("solo-enabled"));
173         solo_col_renderer->set_pixbuf (Gtkmm2ext::ImplicitActive, ::get_icon("soloed-by-others"));
174         solo_col_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_enable_toggled));
175
176         TreeViewColumn* solo_state_column = manage (new TreeViewColumn("S", *solo_col_renderer));
177
178         solo_state_column->add_attribute(solo_col_renderer->property_state(), _columns.solo_state);
179         solo_state_column->add_attribute(solo_col_renderer->property_visible(), _columns.solo_visible);
180         solo_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
181         solo_state_column->set_alignment(ALIGN_CENTER);
182         solo_state_column->set_expand(false);
183         solo_state_column->set_fixed_width(column_width);
184
185         // Solo isolate toggle
186         CellRendererPixbufMulti* solo_iso_renderer = manage (new CellRendererPixbufMulti());
187
188         solo_iso_renderer->set_pixbuf (0, ::get_icon("solo-isolate-disabled"));
189         solo_iso_renderer->set_pixbuf (1, ::get_icon("solo-isolate-enabled"));
190         solo_iso_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_isolate_toggled));
191
192         TreeViewColumn* solo_isolate_state_column = manage (new TreeViewColumn("SI", *solo_iso_renderer));
193
194         solo_isolate_state_column->add_attribute(solo_iso_renderer->property_state(), _columns.solo_isolate_state);
195         solo_isolate_state_column->add_attribute(solo_iso_renderer->property_visible(), _columns.solo_visible);
196         solo_isolate_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
197         solo_isolate_state_column->set_alignment(ALIGN_CENTER);
198         solo_isolate_state_column->set_expand(false);
199         solo_isolate_state_column->set_fixed_width(column_width);
200
201         // Solo safe toggle
202         CellRendererPixbufMulti* solo_safe_renderer = manage (new CellRendererPixbufMulti ());
203
204         solo_safe_renderer->set_pixbuf (0, ::get_icon("solo-safe-disabled"));
205         solo_safe_renderer->set_pixbuf (1, ::get_icon("solo-safe-enabled"));
206         solo_safe_renderer->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::on_tv_solo_safe_toggled));
207
208         TreeViewColumn* solo_safe_state_column = manage (new TreeViewColumn(_("SS"), *solo_safe_renderer));
209         solo_safe_state_column->add_attribute(solo_safe_renderer->property_state(), _columns.solo_safe_state);
210         solo_safe_state_column->add_attribute(solo_safe_renderer->property_visible(), _columns.solo_visible);
211         solo_safe_state_column->set_sizing(TREE_VIEW_COLUMN_FIXED);
212         solo_safe_state_column->set_alignment(ALIGN_CENTER);
213         solo_safe_state_column->set_expand(false);
214         solo_safe_state_column->set_fixed_width(column_width);
215
216         _name_column = _display.append_column ("", _columns.text) - 1;
217         _visible_column = _display.append_column ("", _columns.visible) - 1;
218         _active_column = _display.append_column ("", _columns.active) - 1;
219
220         _display.append_column (*input_active_column);
221         _display.append_column (*rec_state_column);
222         _display.append_column (*rec_safe_column);
223         _display.append_column (*mute_state_column);
224         _display.append_column (*solo_state_column);
225         _display.append_column (*solo_isolate_state_column);
226         _display.append_column (*solo_safe_state_column);
227
228
229         TreeViewColumn* col;
230         Gtk::Label* l;
231
232         ColumnInfo ci[] = {
233                 { 0,  _("Name"),        _("Track/Bus Name") },
234                 { 1, S_("Visible|V"),   _("Track/Bus visible ?") },
235                 { 2, S_("Active|A"),    _("Track/Bus active ?") },
236                 { 3, S_("MidiInput|I"), _("MIDI input enabled") },
237                 { 4, S_("Rec|R"),       _("Record enabled") },
238                 { 5, S_("Rec|RS"),      _("Record Safe") },
239                 { 6, S_("Mute|M"),      _("Muted") },
240                 { 7, S_("Solo|S"),      _("Soloed") },
241                 { 8, S_("SoloIso|SI"),  _("Solo Isolated") },
242                 { 9, S_("SoloLock|SS"), _("Solo Safe (Locked)") },
243                 { -1, 0, 0 }
244         };
245
246         for (int i = 0; ci[i].index >= 0; ++i) {
247                 col = _display.get_column (ci[i].index);
248                 l = manage (new Label (ci[i].label));
249                 set_tooltip (*l, ci[i].tooltip);
250                 col->set_widget (*l);
251                 l->show ();
252         }
253
254         _display.set_headers_visible (true);
255         _display.get_selection()->set_mode (SELECTION_SINGLE);
256         _display.get_selection()->set_select_function (sigc::mem_fun (*this, &EditorRoutes::selection_filter));
257         _display.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &EditorRoutes::selection_changed));
258         _display.set_reorderable (true);
259         _display.set_name (X_("EditGroupList"));
260         _display.set_rules_hint (true);
261         _display.set_size_request (100, -1);
262         _display.add_object_drag (_columns.stripable.index(), "routes");
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         PresentationInfo::Change.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::sync_treeview_from_presentation_info, this), gui_context());
322 }
323
324 bool
325 EditorRoutes::focus_in (GdkEventFocus*)
326 {
327         Window* win = dynamic_cast<Window*> (_scroller.get_toplevel ());
328
329         if (win) {
330                 old_focus = win->get_focus ();
331         } else {
332                 old_focus = 0;
333         }
334
335         name_editable = 0;
336
337         /* try to do nothing on focus in (doesn't work, hence selection_count nonsense) */
338         return true;
339 }
340
341 bool
342 EditorRoutes::focus_out (GdkEventFocus*)
343 {
344         if (old_focus) {
345                 old_focus->grab_focus ();
346                 old_focus = 0;
347         }
348
349         return false;
350 }
351
352 bool
353 EditorRoutes::enter_notify (GdkEventCrossing*)
354 {
355         if (name_editable) {
356                 return true;
357         }
358
359         /* arm counter so that ::selection_filter() will deny selecting anything for the
360          * next two attempts to change selection status.
361          */
362         selection_countdown = 2;
363         _scroller.grab_focus ();
364         Keyboard::magic_widget_grab_focus ();
365         return false;
366 }
367
368 bool
369 EditorRoutes::leave_notify (GdkEventCrossing*)
370 {
371         selection_countdown = 0;
372
373         if (old_focus) {
374                 old_focus->grab_focus ();
375                 old_focus = 0;
376         }
377
378         Keyboard::magic_widget_drop_focus ();
379         return false;
380 }
381
382 void
383 EditorRoutes::set_session (Session* s)
384 {
385         SessionHandlePtr::set_session (s);
386
387         initial_display ();
388
389         if (_session) {
390                 _session->SoloChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::solo_changed_so_update_mute, this), gui_context());
391                 _session->RecordStateChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
392
393                 /* TODO: check if these needs to be tied in with DisplaySuspender
394                  * Given that the UI is single-threaded and DisplaySuspender is only used
395                  * in loops in the UI thread all should be fine.
396                  */
397                 _session->BatchUpdateStart.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::suspend_redisplay, this), gui_context());
398                 _session->BatchUpdateEnd.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::resume_redisplay, this), gui_context());
399         }
400 }
401
402 void
403 EditorRoutes::on_input_active_changed (std::string const & path_string)
404 {
405         // Get the model row that has been toggled.
406         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
407
408         TimeAxisView* tv = row[_columns.tv];
409         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*> (tv);
410
411         if (rtv) {
412                 boost::shared_ptr<MidiTrack> mt;
413                 mt = rtv->midi_track();
414                 if (mt) {
415                         mt->set_input_active (!mt->input_active());
416                 }
417         }
418 }
419
420 void
421 EditorRoutes::on_tv_rec_enable_changed (std::string const & path_string)
422 {
423         // Get the model row that has been toggled.
424         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
425
426         TimeAxisView* tv = row[_columns.tv];
427         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*> (tv);
428
429         if (!rtv) {
430                 return;
431         }
432
433         boost::shared_ptr<AutomationControl> ac = rtv->route()->rec_enable_control();
434
435         if (ac) {
436                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
437         }
438 }
439
440 void
441 EditorRoutes::on_tv_rec_safe_toggled (std::string const & path_string)
442 {
443         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
444         TimeAxisView* tv = row[_columns.tv];
445         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*> (tv);
446
447         if (!rtv) {
448                 return;
449         }
450
451         boost::shared_ptr<AutomationControl> ac (rtv->route()->rec_safe_control());
452
453         if (ac) {
454                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
455         }
456 }
457
458 void
459 EditorRoutes::on_tv_mute_enable_toggled (std::string const & path_string)
460 {
461         // Get the model row that has been toggled.
462         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
463
464         TimeAxisView *tv = row[_columns.tv];
465         RouteTimeAxisView *rtv = dynamic_cast<RouteTimeAxisView*> (tv);
466
467         if (!rtv) {
468                 return;
469         }
470
471         boost::shared_ptr<AutomationControl> ac (rtv->route()->mute_control());
472
473         if (ac) {
474                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
475         }
476 }
477
478 void
479 EditorRoutes::on_tv_solo_enable_toggled (std::string const & path_string)
480 {
481         // Get the model row that has been toggled.
482         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
483
484         TimeAxisView *tv = row[_columns.tv];
485         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (tv);
486
487         if (!rtv) {
488                 return;
489         }
490
491         boost::shared_ptr<AutomationControl> ac (rtv->route()->solo_control());
492
493         if (ac) {
494                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
495         }
496 }
497
498 void
499 EditorRoutes::on_tv_solo_isolate_toggled (std::string const & path_string)
500 {
501         // Get the model row that has been toggled.
502         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
503
504         TimeAxisView *tv = row[_columns.tv];
505         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (tv);
506
507         if (!rtv) {
508                 return;
509         }
510
511         boost::shared_ptr<AutomationControl> ac (rtv->route()->solo_isolate_control());
512
513         if (ac) {
514                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
515         }
516 }
517
518 void
519 EditorRoutes::on_tv_solo_safe_toggled (std::string const & path_string)
520 {
521         // Get the model row that has been toggled.
522         Gtk::TreeModel::Row row = *_model->get_iter (Gtk::TreeModel::Path (path_string));
523
524         TimeAxisView *tv = row[_columns.tv];
525         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (tv);
526
527         if (!rtv) {
528                 return;
529         }
530
531         boost::shared_ptr<AutomationControl> ac (rtv->route()->solo_safe_control());
532
533         if (ac) {
534                 ac->set_value (!ac->get_value(), Controllable::UseGroup);
535         }
536 }
537
538 void
539 EditorRoutes::build_menu ()
540 {
541         using namespace Menu_Helpers;
542         using namespace Gtk;
543
544         _menu = new Menu;
545
546         MenuList& items = _menu->items();
547         _menu->set_name ("ArdourContextMenu");
548
549         items.push_back (MenuElem (_("Show All"), sigc::mem_fun (*this, &EditorRoutes::show_all_routes)));
550         items.push_back (MenuElem (_("Hide All"), sigc::mem_fun (*this, &EditorRoutes::hide_all_routes)));
551         items.push_back (MenuElem (_("Show All Audio Tracks"), sigc::mem_fun (*this, &EditorRoutes::show_all_audiotracks)));
552         items.push_back (MenuElem (_("Hide All Audio Tracks"), sigc::mem_fun (*this, &EditorRoutes::hide_all_audiotracks)));
553         items.push_back (MenuElem (_("Show All Audio Busses"), sigc::mem_fun (*this, &EditorRoutes::show_all_audiobus)));
554         items.push_back (MenuElem (_("Hide All Audio Busses"), sigc::mem_fun (*this, &EditorRoutes::hide_all_audiobus)));
555         items.push_back (MenuElem (_("Show All Midi Tracks"), sigc::mem_fun (*this, &EditorRoutes::show_all_miditracks)));
556         items.push_back (MenuElem (_("Hide All Midi Tracks"), sigc::mem_fun (*this, &EditorRoutes::hide_all_miditracks)));
557         items.push_back (MenuElem (_("Only Show Tracks with Regions Under Playhead"), sigc::mem_fun (*this, &EditorRoutes::show_tracks_with_regions_at_playhead)));
558 }
559
560 void
561 EditorRoutes::show_menu ()
562 {
563         if (_menu == 0) {
564                 build_menu ();
565         }
566
567         _menu->popup (1, gtk_get_current_event_time());
568 }
569
570 void
571 EditorRoutes::redisplay_real ()
572 {
573         TreeModel::Children rows = _model->children();
574         TreeModel::Children::iterator i;
575         uint32_t position;
576
577         /* n will be the count of tracks plus children (updated by TimeAxisView::show_at),
578          * so we will use that to know where to put things.
579          */
580         int n;
581
582         for (n = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
583                 TimeAxisView *tv = (*i)[_columns.tv];
584                 boost::shared_ptr<Stripable> route = (*i)[_columns.stripable];
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                 if (rtav) {
796                         rtav->route()->gui_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::handle_gui_changes, this, _1, _2), gui_context());
797                 }
798                 stripable->PropertyChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::route_property_changed, this, _1, ws), gui_context());
799
800                 if (boost::dynamic_pointer_cast<Track> (stripable)) {
801                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (stripable);
802                         t->rec_enable_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
803                         t->rec_safe_control()->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
804                 }
805
806                 if (midi_trk) {
807                         midi_trk->StepEditStatusChange.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_rec_display, this), gui_context());
808                         midi_trk->InputActiveChanged.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_input_active_display, this), gui_context());
809                 }
810
811                 boost::shared_ptr<AutomationControl> ac;
812
813                 if ((ac = stripable->mute_control()) != 0) {
814                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_mute_display, this), gui_context());
815                 }
816                 if ((ac = stripable->solo_control()) != 0) {
817                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_display, this), gui_context());
818                 }
819                 if ((ac = stripable->solo_isolate_control()) != 0) {
820                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_isolate_display, this), gui_context());
821                 }
822                 if ((ac = stripable->solo_safe_control()) != 0) {
823                         ac->Changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_solo_safe_display, this), gui_context());
824                 }
825
826                 if (rtav) {
827                         rtav->route()->active_changed.connect (*this, MISSING_INVALIDATOR, boost::bind (&EditorRoutes::update_active_display, this), gui_context ());
828                 }
829         }
830
831         update_rec_display ();
832         update_mute_display ();
833         update_solo_display ();
834         update_solo_isolate_display ();
835         update_solo_safe_display ();
836         update_input_active_display ();
837         update_active_display ();
838
839         _display.set_model (_model);
840
841         /* now update route order keys from the treeview/track display order */
842         if (!from_scratch) {
843                 sync_presentation_info_from_treeview ();
844         }
845 }
846
847 void
848 EditorRoutes::handle_gui_changes (string const & what, void*)
849 {
850         if (_adding_routes) {
851                 return;
852         }
853
854         if (what == "track_height") {
855                 /* Optional :make tracks change height while it happens, instead
856                    of on first-idle
857                 */
858                 redisplay ();
859         }
860
861         if (what == "visible_tracks") {
862                 redisplay ();
863         }
864 }
865
866 void
867 EditorRoutes::route_removed (TimeAxisView *tv)
868 {
869         ENSURE_GUI_THREAD (*this, &EditorRoutes::route_removed, tv)
870
871         TreeModel::Children rows = _model->children();
872         TreeModel::Children::iterator ri;
873
874         for (ri = rows.begin(); ri != rows.end(); ++ri) {
875                 if ((*ri)[_columns.tv] == tv) {
876                         PBD::Unwinder<bool> uw (_route_deletion_in_progress, true);
877                         _model->erase (ri);
878                         break;
879                 }
880         }
881
882         /* the deleted signal for the treeview/model will take
883            care of any updates.
884         */
885 }
886
887 void
888 EditorRoutes::route_property_changed (const PropertyChange& what_changed, boost::weak_ptr<Stripable> s)
889 {
890         if (!what_changed.contains (ARDOUR::Properties::name)) {
891                 return;
892         }
893
894         ENSURE_GUI_THREAD (*this, &EditorRoutes::route_name_changed, r)
895
896         boost::shared_ptr<Stripable> stripable = s.lock ();
897
898         if (!stripable) {
899                 return;
900         }
901
902         TreeModel::Children rows = _model->children();
903         TreeModel::Children::iterator i;
904
905         for (i = rows.begin(); i != rows.end(); ++i) {
906                 boost::shared_ptr<Stripable> ss = (*i)[_columns.stripable];
907                 if (ss == stripable) {
908                         (*i)[_columns.text] = stripable->name();
909                         break;
910                 }
911         }
912 }
913
914 void
915 EditorRoutes::update_active_display ()
916 {
917         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
918                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
919         }
920 }
921
922 void
923 EditorRoutes::update_visibility ()
924 {
925         TreeModel::Children rows = _model->children();
926         TreeModel::Children::iterator i;
927
928         DisplaySuspender ds;
929
930         for (i = rows.begin(); i != rows.end(); ++i) {
931                 TimeAxisView *tv = (*i)[_columns.tv];
932                 (*i)[_columns.visible] = tv->marked_for_display ();
933         }
934
935         /* force route order keys catch up with visibility changes
936          */
937
938         sync_presentation_info_from_treeview ();
939 }
940
941 void
942 EditorRoutes::hide_track_in_display (TimeAxisView& tv)
943 {
944         TreeModel::Children rows = _model->children();
945         TreeModel::Children::iterator i;
946
947         for (i = rows.begin(); i != rows.end(); ++i) {
948                 if ((*i)[_columns.tv] == &tv) {
949                         tv.set_marked_for_display (false);
950                         (*i)[_columns.visible] = false;
951                         redisplay ();
952                         break;
953                 }
954         }
955 }
956
957 void
958 EditorRoutes::show_track_in_display (TimeAxisView& tv)
959 {
960         TreeModel::Children rows = _model->children();
961         TreeModel::Children::iterator i;
962
963
964         for (i = rows.begin(); i != rows.end(); ++i) {
965                 if ((*i)[_columns.tv] == &tv) {
966                         tv.set_marked_for_display (true);
967                         (*i)[_columns.visible] = true;
968                         redisplay ();
969                         break;
970                 }
971         }
972 }
973
974 void
975 EditorRoutes::reset_remote_control_ids ()
976 {
977         if (Config->get_remote_model() == UserOrdered || !_session || _session->deletion_in_progress()) {
978                 return;
979         }
980
981         sync_presentation_info_from_treeview ();
982 }
983 void
984 EditorRoutes::sync_presentation_info_from_treeview ()
985 {
986         if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
987                 return;
988         }
989
990         TreeModel::Children rows = _model->children();
991
992         if (rows.empty()) {
993                 return;
994         }
995
996         DEBUG_TRACE (DEBUG::OrderKeys, "editor sync presentation info from treeview\n");
997
998         TreeModel::Children::iterator ri;
999         bool change = false;
1000         PresentationInfo::order_t order = 0;
1001
1002         for (ri = rows.begin(); ri != rows.end(); ++ri) {
1003
1004                 boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1005                 bool visible = (*ri)[_columns.visible];
1006
1007                 /* Monitor and Auditioner do not get their presentation
1008                  * info reset here.
1009                  */
1010
1011                 if (stripable->is_monitor() || stripable->is_auditioner()) {
1012                         continue;
1013                 }
1014
1015                 if (!visible) {
1016                         stripable->presentation_info().set_flag (PresentationInfo::Hidden);
1017                 } else {
1018                         stripable->presentation_info().unset_flag (PresentationInfo::Hidden);
1019                 }
1020
1021                 cerr << "Would change PI go for " << stripable->name() << " to " << order << " currently " << stripable->presentation_info().order() << endl;
1022
1023                 if (order != stripable->presentation_info().order()) {
1024                         stripable->set_presentation_order_explicit (order);
1025                         change = true;
1026                 }
1027
1028                 ++order;
1029         }
1030
1031         if (change) {
1032                 DEBUG_TRACE (DEBUG::OrderKeys, "... notify PI change from editor GUI\n");
1033                 _session->notify_presentation_info_change ();
1034         }
1035 }
1036
1037 void
1038 EditorRoutes::sync_treeview_from_presentation_info ()
1039 {
1040         /* Some route order key(s) have been changed, make sure that
1041            we update out tree/list model and GUI to reflect the change.
1042         */
1043
1044         if (_ignore_reorder || !_session || _session->deletion_in_progress()) {
1045                 return;
1046         }
1047
1048         DEBUG_TRACE (DEBUG::OrderKeys, "editor sync model from presentation info.\n");
1049
1050         vector<int> neworder;
1051         TreeModel::Children rows = _model->children();
1052         uint32_t old_order = 0;
1053         bool changed = false;
1054
1055         if (rows.empty()) {
1056                 return;
1057         }
1058
1059         OrderingKeys sorted;
1060
1061         for (TreeModel::Children::iterator ri = rows.begin(); ri != rows.end(); ++ri, ++old_order) {
1062                 boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1063                 /* use global order */
1064                 sorted.push_back (OrderKeys (old_order, stripable->presentation_info().order()));
1065         }
1066
1067         SortByNewDisplayOrder cmp;
1068
1069         sort (sorted.begin(), sorted.end(), cmp);
1070         neworder.assign (sorted.size(), 0);
1071
1072         uint32_t n = 0;
1073
1074         for (OrderingKeys::iterator sr = sorted.begin(); sr != sorted.end(); ++sr, ++n) {
1075
1076                 neworder[n] = sr->old_display_order;
1077
1078                 if (sr->old_display_order != n) {
1079                         changed = true;
1080                 }
1081         }
1082
1083         if (changed) {
1084                 Unwinder<bool> uw (_ignore_reorder, true);
1085                 _model->reorder (neworder);
1086         }
1087
1088         redisplay ();
1089 }
1090
1091 void
1092 EditorRoutes::hide_all_tracks (bool /*with_select*/)
1093 {
1094         TreeModel::Children rows = _model->children();
1095         TreeModel::Children::iterator i;
1096
1097         DisplaySuspender ds;
1098
1099         for (i = rows.begin(); i != rows.end(); ++i) {
1100
1101                 TreeModel::Row row = (*i);
1102                 TimeAxisView *tv = row[_columns.tv];
1103
1104                 if (tv == 0) {
1105                         continue;
1106                 }
1107
1108                 row[_columns.visible] = false;
1109         }
1110 }
1111
1112 void
1113 EditorRoutes::set_all_tracks_visibility (bool yn)
1114 {
1115         TreeModel::Children rows = _model->children();
1116         TreeModel::Children::iterator i;
1117
1118         DisplaySuspender ds;
1119
1120         for (i = rows.begin(); i != rows.end(); ++i) {
1121
1122                 TreeModel::Row row = (*i);
1123                 TimeAxisView* tv = row[_columns.tv];
1124
1125                 if (tv == 0) {
1126                         continue;
1127                 }
1128
1129                 tv->set_marked_for_display (yn);
1130                 (*i)[_columns.visible] = yn;
1131         }
1132
1133         /* force route order keys catch up with visibility changes
1134          */
1135
1136         sync_presentation_info_from_treeview ();
1137 }
1138
1139 void
1140 EditorRoutes::set_all_audio_midi_visibility (int tracks, bool yn)
1141 {
1142         TreeModel::Children rows = _model->children();
1143         TreeModel::Children::iterator i;
1144
1145         DisplaySuspender ds;
1146
1147         for (i = rows.begin(); i != rows.end(); ++i) {
1148
1149                 TreeModel::Row row = (*i);
1150                 TimeAxisView* tv = row[_columns.tv];
1151
1152                 AudioTimeAxisView* atv;
1153                 MidiTimeAxisView* mtv;
1154
1155                 if (tv == 0) {
1156                         continue;
1157                 }
1158
1159                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
1160                         switch (tracks) {
1161                         case 0:
1162                                 atv->set_marked_for_display (yn);
1163                                 (*i)[_columns.visible] = yn;
1164                                 break;
1165
1166                         case 1:
1167                                 if (atv->is_audio_track()) {
1168                                         atv->set_marked_for_display (yn);
1169                                         (*i)[_columns.visible] = yn;
1170                                 }
1171                                 break;
1172
1173                         case 2:
1174                                 if (!atv->is_audio_track()) {
1175                                         atv->set_marked_for_display (yn);
1176                                         (*i)[_columns.visible] = yn;
1177                                 }
1178                                 break;
1179                         }
1180                 }
1181                 else if ((mtv = dynamic_cast<MidiTimeAxisView*>(tv)) != 0) {
1182                         switch (tracks) {
1183                         case 0:
1184                                 mtv->set_marked_for_display (yn);
1185                                 (*i)[_columns.visible] = yn;
1186                                 break;
1187
1188                         case 3:
1189                                 if (mtv->is_midi_track()) {
1190                                         mtv->set_marked_for_display (yn);
1191                                         (*i)[_columns.visible] = yn;
1192                                 }
1193                                 break;
1194                         }
1195                 }
1196         }
1197
1198         /* force route order keys catch up with visibility changes
1199          */
1200
1201         sync_presentation_info_from_treeview ();
1202 }
1203
1204 void
1205 EditorRoutes::hide_all_routes ()
1206 {
1207         set_all_tracks_visibility (false);
1208 }
1209
1210 void
1211 EditorRoutes::show_all_routes ()
1212 {
1213         set_all_tracks_visibility (true);
1214 }
1215
1216 void
1217 EditorRoutes::show_all_audiotracks()
1218 {
1219         set_all_audio_midi_visibility (1, true);
1220 }
1221 void
1222 EditorRoutes::hide_all_audiotracks ()
1223 {
1224         set_all_audio_midi_visibility (1, false);
1225 }
1226
1227 void
1228 EditorRoutes::show_all_audiobus ()
1229 {
1230         set_all_audio_midi_visibility (2, true);
1231 }
1232 void
1233 EditorRoutes::hide_all_audiobus ()
1234 {
1235         set_all_audio_midi_visibility (2, false);
1236 }
1237
1238 void
1239 EditorRoutes::show_all_miditracks()
1240 {
1241         set_all_audio_midi_visibility (3, true);
1242 }
1243 void
1244 EditorRoutes::hide_all_miditracks ()
1245 {
1246         set_all_audio_midi_visibility (3, false);
1247 }
1248
1249 bool
1250 EditorRoutes::key_press (GdkEventKey* ev)
1251 {
1252         TreeViewColumn *col;
1253         boost::shared_ptr<RouteList> rl (new RouteList);
1254         TreePath path;
1255
1256         switch (ev->keyval) {
1257                 case GDK_Tab:
1258                 case GDK_ISO_Left_Tab:
1259
1260                         /* If we appear to be editing something, leave that cleanly and appropriately. */
1261                         if (name_editable) {
1262                                 name_editable->editing_done ();
1263                                 name_editable = 0;
1264                         }
1265
1266                         col = _display.get_column (_name_column); // select&focus on name column
1267
1268                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
1269                                 treeview_select_previous (_display, _model, col);
1270                         } else {
1271                                 treeview_select_next (_display, _model, col);
1272                         }
1273
1274                         return true;
1275                         break;
1276
1277                 case 'm':
1278                         if (get_relevant_routes (rl)) {
1279                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::mute_control), rl->front()->muted() ? 0.0 : 1.0, Controllable::NoGroup);
1280                         }
1281                         return true;
1282                         break;
1283
1284                 case 's':
1285                         if (get_relevant_routes (rl)) {
1286                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::solo_control), rl->front()->self_soloed() ? 0.0 : 1.0, Controllable::NoGroup);
1287                         }
1288                         return true;
1289                         break;
1290
1291                 case 'r':
1292                         if (get_relevant_routes (rl)) {
1293                                 for (RouteList::const_iterator r = rl->begin(); r != rl->end(); ++r) {
1294                                         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (*r);
1295                                         if (t) {
1296                                                 _session->set_controls (route_list_to_control_list (rl, &Stripable::rec_enable_control), !t->rec_enable_control()->get_value(), Controllable::NoGroup);
1297                                                 break;
1298                                         }
1299                                 }
1300                         }
1301                         break;
1302
1303                 default:
1304                         break;
1305         }
1306
1307         return false;
1308 }
1309
1310 bool
1311 EditorRoutes::get_relevant_routes (boost::shared_ptr<RouteList> rl)
1312 {
1313         TimeAxisView* tv;
1314         RouteTimeAxisView* rtv;
1315         RefPtr<TreeSelection> selection = _display.get_selection();
1316         TreePath path;
1317         TreeIter iter;
1318
1319         if (selection->count_selected_rows() != 0) {
1320
1321                 /* use selection */
1322
1323                 RefPtr<TreeModel> tm = RefPtr<TreeModel>::cast_dynamic (_model);
1324                 iter = selection->get_selected (tm);
1325
1326         } else {
1327                 /* use mouse pointer */
1328
1329                 int x, y;
1330                 int bx, by;
1331
1332                 _display.get_pointer (x, y);
1333                 _display.convert_widget_to_bin_window_coords (x, y, bx, by);
1334
1335                 if (_display.get_path_at_pos (bx, by, path)) {
1336                         iter = _model->get_iter (path);
1337                 }
1338         }
1339
1340         if (iter) {
1341                 tv = (*iter)[_columns.tv];
1342                 if (tv) {
1343                         rtv = dynamic_cast<RouteTimeAxisView*>(tv);
1344                         if (rtv) {
1345                                 rl->push_back (rtv->route());
1346                         }
1347                 }
1348         }
1349
1350         return !rl->empty();
1351 }
1352
1353 bool
1354 EditorRoutes::button_press (GdkEventButton* ev)
1355 {
1356         if (Keyboard::is_context_menu_event (ev)) {
1357                 show_menu ();
1358                 return true;
1359         }
1360
1361         TreeModel::Path path;
1362         TreeViewColumn *tvc;
1363         int cell_x;
1364         int cell_y;
1365
1366         if (!_display.get_path_at_pos ((int) ev->x, (int) ev->y, path, tvc, cell_x, cell_y)) {
1367                 /* cancel selection */
1368                 _display.get_selection()->unselect_all ();
1369                 /* end any editing by grabbing focus */
1370                 _display.grab_focus ();
1371                 return true;
1372         }
1373
1374         //Scroll editor canvas to selected track
1375         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
1376
1377                 Gtk::TreeModel::Row row = *_model->get_iter (path);
1378                 TimeAxisView *tv = row[_columns.tv];
1379
1380                 if (tv) {
1381                         _editor->ensure_time_axis_view_is_visible (*tv, true);
1382                 }
1383         }
1384
1385         return false;
1386 }
1387
1388 void
1389 EditorRoutes::selection_changed ()
1390 {
1391         _editor->begin_reversible_selection_op (X_("Select Track from Route List"));
1392
1393         if (_display.get_selection()->count_selected_rows() > 0) {
1394
1395                 TreeIter iter;
1396                 TreeView::Selection::ListHandle_Path rows = _display.get_selection()->get_selected_rows ();
1397                 TrackViewList selected;
1398
1399                 _editor->get_selection().clear_regions ();
1400
1401                 for (TreeView::Selection::ListHandle_Path::iterator i = rows.begin(); i != rows.end(); ++i) {
1402
1403                         if ((iter = _model->get_iter (*i))) {
1404
1405                                 TimeAxisView* tv = (*iter)[_columns.tv];
1406                                 selected.push_back (tv);
1407                         }
1408
1409                 }
1410
1411                 _editor->get_selection().set (selected);
1412                 _editor->ensure_time_axis_view_is_visible (*(selected.front()), true);
1413
1414         } else {
1415                 _editor->get_selection().clear_tracks ();
1416         }
1417
1418         _editor->commit_reversible_selection_op ();
1419 }
1420
1421 bool
1422 EditorRoutes::selection_filter (Glib::RefPtr<TreeModel> const &, TreeModel::Path const&, bool /*selected*/)
1423 {
1424         if (selection_countdown) {
1425                 if (--selection_countdown == 0) {
1426                         return true;
1427                 } else {
1428                         /* no selection yet ... */
1429                         return false;
1430                 }
1431         }
1432         return true;
1433 }
1434
1435 struct PresentationInfoRouteSorter
1436 {
1437         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1438                 if (a->is_master()) {
1439                         /* master before everything else */
1440                         return true;
1441                 } else if (b->is_master()) {
1442                         /* everything else before master */
1443                         return false;
1444                 }
1445                 return a->presentation_info().order () < b->presentation_info().order ();
1446         }
1447 };
1448
1449 struct PresentationInfoEditorSorter
1450 {
1451         bool operator() (boost::shared_ptr<Stripable> a, boost::shared_ptr<Stripable> b) {
1452                 if (a->is_master()) {
1453                         /* master before everything else */
1454                         return true;
1455                 } else if (b->is_master()) {
1456                         /* everything else before master */
1457                         return false;
1458                 }
1459                 return a->presentation_info().order () < b->presentation_info().order ();
1460         }
1461 };
1462
1463 struct PresentationInfoVCASorter
1464 {
1465         bool operator() (boost::shared_ptr<VCA> a, boost::shared_ptr<VCA> b) {
1466                 return a->presentation_info().order () < b->presentation_info().order ();
1467         }
1468 };
1469
1470 void
1471 EditorRoutes::initial_display ()
1472 {
1473         DisplaySuspender ds;
1474         _model->clear ();
1475
1476         if (!_session) {
1477                 return;
1478         }
1479
1480         StripableList s;
1481
1482         RouteList r (*_session->get_routes());
1483         for (RouteList::iterator ri = r.begin(); ri != r.end(); ++ri) {
1484                 s.push_back (*ri);
1485         }
1486
1487         _editor->add_routes (r);
1488
1489         VCAList v (_session->vca_manager().vcas());
1490         for (VCAList::iterator vi = v.begin(); vi != v.end(); ++vi) {
1491                 s.push_back (*vi);
1492         }
1493         s.sort (PresentationInfoEditorSorter ());
1494
1495         _editor->add_vcas (v);
1496 }
1497
1498 void
1499 EditorRoutes::display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
1500                                              int x, int y,
1501                                              const SelectionData& data,
1502                                              guint info, guint time)
1503 {
1504         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
1505                 _display.on_drag_data_received (context, x, y, data, info, time);
1506                 return;
1507         }
1508
1509         context->drag_finish (true, false, time);
1510 }
1511
1512 struct ViewStripable {
1513         TimeAxisView* tav;
1514         boost::shared_ptr<Stripable> stripable;
1515         uint32_t old_order;
1516
1517         ViewStripable (TimeAxisView* t, boost::shared_ptr<Stripable> s, uint32_t n)
1518                 : tav (t), stripable (s), old_order (n) {}
1519 };
1520
1521 void
1522 EditorRoutes::move_selected_tracks (bool up)
1523 {
1524         if (_editor->selection->tracks.empty()) {
1525                 return;
1526         }
1527
1528         std::list<ViewStripable> view_stripables;
1529         std::vector<int> neworder;
1530         TreeModel::Children rows = _model->children();
1531         TreeModel::Children::iterator ri;
1532         TreeModel::Children::size_type n;
1533
1534         for (n = 0, ri = rows.begin(); ri != rows.end(); ++ri, ++n) {
1535                 TimeAxisView* tv = (*ri)[_columns.tv];
1536                 boost::shared_ptr<Stripable> stripable = (*ri)[_columns.stripable];
1537                 view_stripables.push_back (ViewStripable (tv, stripable, n));
1538         }
1539
1540         list<ViewStripable>::iterator trailing;
1541         list<ViewStripable>::iterator leading;
1542
1543         if (up) {
1544
1545                 trailing = view_stripables.begin();
1546                 leading = view_stripables.begin();
1547
1548                 ++leading;
1549
1550                 while (leading != view_stripables.end()) {
1551                         if (_editor->selection->selected (leading->tav)) {
1552                                 view_stripables.insert (trailing, ViewStripable (*leading));
1553                                 leading = view_stripables.erase (leading);
1554                         } else {
1555                                 ++leading;
1556                                 ++trailing;
1557                         }
1558                 }
1559
1560         } else {
1561
1562                 /* if we could use reverse_iterator in list::insert, this code
1563                    would be a beautiful reflection of the code above. but we can't
1564                    and so it looks like a bit of a mess.
1565                 */
1566
1567                 trailing = view_stripables.end();
1568                 leading = view_stripables.end();
1569
1570                 --leading; if (leading == view_stripables.begin()) { return; }
1571                 --leading;
1572                 --trailing;
1573
1574                 while (1) {
1575
1576                         if (_editor->selection->selected (leading->tav)) {
1577                                 list<ViewStripable>::iterator tmp;
1578
1579                                 /* need to insert *after* trailing, not *before* it,
1580                                    which is what insert (iter, val) normally does.
1581                                 */
1582
1583                                 tmp = trailing;
1584                                 tmp++;
1585
1586                                 view_stripables.insert (tmp, ViewStripable (*leading));
1587
1588                                 /* can't use iter = cont.erase (iter); form here, because
1589                                    we need iter to move backwards.
1590                                 */
1591
1592                                 tmp = leading;
1593                                 --tmp;
1594
1595                                 bool done = false;
1596
1597                                 if (leading == view_stripables.begin()) {
1598                                         /* the one we've just inserted somewhere else
1599                                            was the first in the list. erase this copy,
1600                                            and then break, because we're done.
1601                                         */
1602                                         done = true;
1603                                 }
1604
1605                                 view_stripables.erase (leading);
1606
1607                                 if (done) {
1608                                         break;
1609                                 }
1610
1611                                 leading = tmp;
1612
1613                         } else {
1614                                 if (leading == view_stripables.begin()) {
1615                                         break;
1616                                 }
1617                                 --leading;
1618                                 --trailing;
1619                         }
1620                 };
1621         }
1622
1623         for (leading = view_stripables.begin(); leading != view_stripables.end(); ++leading) {
1624                 neworder.push_back (leading->old_order);
1625 #ifndef NDEBUG
1626                 if (leading->old_order != neworder.size() - 1) {
1627                         DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("move %1 to %2\n", leading->old_order, neworder.size() - 1));
1628                 }
1629 #endif
1630
1631         }
1632 #ifndef NDEBUG
1633         DEBUG_TRACE (DEBUG::OrderKeys, "New order after moving tracks:\n");
1634         for (vector<int>::iterator i = neworder.begin(); i != neworder.end(); ++i) {
1635                 DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("\t%1\n", *i));
1636         }
1637         DEBUG_TRACE (DEBUG::OrderKeys, "-------\n");
1638 #endif
1639
1640
1641         _model->reorder (neworder);
1642 }
1643
1644 void
1645 EditorRoutes::update_input_active_display ()
1646 {
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
1653                 if (boost::dynamic_pointer_cast<Track> (stripable)) {
1654                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (stripable);
1655
1656                         if (mt) {
1657                                 (*i)[_columns.is_input_active] = mt->input_active();
1658                         }
1659                 }
1660         }
1661 }
1662
1663 void
1664 EditorRoutes::update_rec_display ()
1665 {
1666         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1667                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1668         }
1669 }
1670
1671 bool
1672 EditorRoutes::idle_update_mute_rec_solo_etc()
1673 {
1674         g_atomic_int_set (const_cast<gint*>(&_queue_tv_update), 0);
1675         TreeModel::Children rows = _model->children();
1676         TreeModel::Children::iterator i;
1677
1678         for (i = rows.begin(); i != rows.end(); ++i) {
1679                 boost::shared_ptr<Stripable> stripable = (*i)[_columns.stripable];
1680                 boost::shared_ptr<Route> route = boost::dynamic_pointer_cast<Route> (stripable);
1681                 (*i)[_columns.mute_state] = RouteUI::mute_active_state (_session, stripable);
1682                 (*i)[_columns.solo_state] = RouteUI::solo_active_state (stripable);
1683                 (*i)[_columns.solo_isolate_state] = RouteUI::solo_isolate_active_state (stripable) ? 1 : 0;
1684                 (*i)[_columns.solo_safe_state] = RouteUI::solo_safe_active_state (stripable) ? 1 : 0;
1685                 if (route) {
1686                         (*i)[_columns.active] = route->active ();
1687                 } else {
1688                         (*i)[_columns.active] = true;
1689                 }
1690
1691                 boost::shared_ptr<Track> trk (boost::dynamic_pointer_cast<Track>(route));
1692
1693                 if (trk) {
1694                         boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (route);
1695
1696                         if (trk->rec_enable_control()->get_value()) {
1697                                 if (_session->record_status() == Session::Recording) {
1698                                         (*i)[_columns.rec_state] = 1;
1699                                 } else {
1700                                         (*i)[_columns.rec_state] = 2;
1701                                 }
1702                         } else if (mt && mt->step_editing()) {
1703                                 (*i)[_columns.rec_state] = 3;
1704                         } else {
1705                                 (*i)[_columns.rec_state] = 0;
1706                         }
1707
1708                         (*i)[_columns.rec_safe] = trk->rec_safe_control()->get_value();
1709                         (*i)[_columns.name_editable] = !trk->rec_enable_control()->get_value();
1710                 }
1711         }
1712
1713         return false; // do not call again (until needed)
1714 }
1715
1716
1717 void
1718 EditorRoutes::update_mute_display ()
1719 {
1720         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1721                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1722         }
1723 }
1724
1725 void
1726 EditorRoutes::update_solo_display ()
1727 {
1728         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1729                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1730         }
1731 }
1732
1733 void
1734 EditorRoutes::update_solo_isolate_display ()
1735 {
1736         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1737                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1738         }
1739 }
1740
1741 void
1742 EditorRoutes::update_solo_safe_display ()
1743 {
1744         if (g_atomic_int_compare_and_exchange (const_cast<gint*>(&_queue_tv_update), 0, 1)) {
1745                 Glib::signal_idle().connect (sigc::mem_fun (*this, &EditorRoutes::idle_update_mute_rec_solo_etc));
1746         }
1747 }
1748
1749 list<TimeAxisView*>
1750 EditorRoutes::views () const
1751 {
1752         list<TimeAxisView*> v;
1753         for (TreeModel::Children::iterator i = _model->children().begin(); i != _model->children().end(); ++i) {
1754                 v.push_back ((*i)[_columns.tv]);
1755         }
1756
1757         return v;
1758 }
1759
1760 void
1761 EditorRoutes::clear ()
1762 {
1763         _display.set_model (Glib::RefPtr<Gtk::TreeStore> (0));
1764         _model->clear ();
1765         _display.set_model (_model);
1766 }
1767
1768 void
1769 EditorRoutes::name_edit_started (CellEditable* ce, const Glib::ustring&)
1770 {
1771         name_editable = ce;
1772
1773         /* give it a special name */
1774
1775         Gtk::Entry *e = dynamic_cast<Gtk::Entry*> (ce);
1776
1777         if (e) {
1778                 e->set_name (X_("RouteNameEditorEntry"));
1779         }
1780 }
1781
1782 void
1783 EditorRoutes::name_edit (std::string const & path, std::string const & new_text)
1784 {
1785         name_editable = 0;
1786
1787         TreeIter iter = _model->get_iter (path);
1788
1789         if (!iter) {
1790                 return;
1791         }
1792
1793         boost::shared_ptr<Stripable> stripable = (*iter)[_columns.stripable];
1794
1795         if (stripable && stripable->name() != new_text) {
1796                 stripable->set_name (new_text);
1797         }
1798 }
1799
1800 void
1801 EditorRoutes::solo_changed_so_update_mute ()
1802 {
1803         update_mute_display ();
1804 }
1805
1806 void
1807 EditorRoutes::show_tracks_with_regions_at_playhead ()
1808 {
1809         boost::shared_ptr<RouteList> const r = _session->get_routes_with_regions_at (_session->transport_frame ());
1810
1811         set<TimeAxisView*> show;
1812         for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
1813                 TimeAxisView* tav = _editor->axis_view_from_route (*i);
1814                 if (tav) {
1815                         show.insert (tav);
1816                 }
1817         }
1818
1819         DisplaySuspender ds;
1820
1821         TreeModel::Children rows = _model->children ();
1822         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
1823                 TimeAxisView* tv = (*i)[_columns.tv];
1824                 bool to_show = (show.find (tv) != show.end());
1825
1826                 tv->set_marked_for_display (to_show);
1827                 (*i)[_columns.visible] = to_show;
1828         }
1829
1830         /* force route order keys catch up with visibility changes
1831          */
1832
1833         sync_presentation_info_from_treeview ();
1834 }
1835
1836 int
1837 EditorRoutes::plugin_setup (boost::shared_ptr<Route> r, boost::shared_ptr<PluginInsert> pi, ARDOUR::Route::PluginSetupOptions flags)
1838 {
1839         PluginSetupDialog psd (r, pi, flags);
1840         return psd.run ();
1841 }