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