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