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