optionally tie together editor+mixer display orders; provide GUI control for timecode...
[ardour.git] / gtk2_ardour / editor_route_list.cc
1 /*
2     Copyright (C) 2000 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 <algorithm>
21 #include <cstdlib>
22 #include <cmath>
23
24 #include "editor.h"
25 #include "keyboard.h"
26 #include "ardour_ui.h"
27 #include "audio_time_axis.h"
28 #include "mixer_strip.h"
29 #include "gui_thread.h"
30 #include "actions.h"
31
32 #include <ardour/route.h>
33 #include <ardour/audio_track.h>
34
35 #include "i18n.h"
36
37 using namespace sigc;
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace Gtk;
41
42 void
43 Editor::handle_new_route (Session::RouteList& routes)
44 {
45         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_new_route), routes));
46         
47         TimeAxisView *tv;
48         AudioTimeAxisView *atv;
49         TreeModel::Row parent;
50         TreeModel::Row row;
51
52         ignore_route_list_reorder = true;
53         ignore_route_order_sync = true;
54         no_route_list_redisplay = true;
55
56         for (Session::RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
57                 boost::shared_ptr<Route> route = (*x);
58
59                 if (route->hidden()) {
60                         continue;
61                 }
62                 
63                 tv = new AudioTimeAxisView (*this, *session, route, track_canvas);
64                 
65                 row = *(route_display_model->append ());
66                 
67                 row[route_display_columns.route] = route;
68                 row[route_display_columns.text] = route->name();
69                 row[route_display_columns.visible] = tv->marked_for_display();
70                 row[route_display_columns.tv] = tv;
71
72                 track_views.push_back (tv);
73                 
74                 if ((atv = dynamic_cast<AudioTimeAxisView*> (tv)) != 0) {
75                         /* added a new fresh one at the end */
76                         if (atv->route()->order_key(N_("editor")) == -1) {
77                                 atv->route()->set_order_key (N_("editor"), route_display_model->children().size()-1);
78                         }
79                 }
80
81                 tv->set_old_order_key (route_display_model->children().size() - 1);
82                 route->gui_changed.connect (mem_fun(*this, &Editor::handle_gui_changes));
83                 tv->GoingAway.connect (bind (mem_fun(*this, &Editor::remove_route), tv));
84         }
85
86         ignore_route_list_reorder = false;
87         ignore_route_order_sync = false;
88         no_route_list_redisplay = false;
89
90         redisplay_route_list ();
91
92         if (show_editor_mixer_when_tracks_arrive) {
93                 show_editor_mixer (true);
94         }
95
96         editor_mixer_button.set_sensitive(true);
97 }
98
99 void
100 Editor::handle_gui_changes (const string & what, void *src)
101 {
102         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_gui_changes), what, src));
103         
104         if (what == "track_height") {
105                 redisplay_route_list ();
106         }
107 }
108
109 void
110 Editor::remove_route (TimeAxisView *tv)
111 {
112         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::remove_route), tv));
113
114         TrackViewList::iterator i;
115         TreeModel::Children rows = route_display_model->children();
116         TreeModel::Children::iterator ri;
117
118         for (ri = rows.begin(); ri != rows.end(); ++ri) {
119                 if ((*ri)[route_display_columns.tv] == tv) {
120                         route_display_model->erase (ri);
121                         break;
122                 }
123         }
124
125         if ((i = find (track_views.begin(), track_views.end(), tv)) != track_views.end()) {
126                 track_views.erase (i);
127         }
128
129         /* since the editor mixer goes away when you remove a route, set the
130          * button to inactive and untick the menu option
131          */
132
133         editor_mixer_button.set_active(false);
134         ActionManager::uncheck_toggleaction ("<Actions>/Editor/show-editor-mixer");
135
136         /* and disable if all tracks and/or routes are gone */
137
138         if (track_views.size() == 0) {
139                 editor_mixer_button.set_sensitive(false);
140         }
141 }
142
143 void
144 Editor::route_name_changed (TimeAxisView *tv)
145 {
146         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::route_name_changed), tv));
147         
148         TreeModel::Children rows = route_display_model->children();
149         TreeModel::Children::iterator i;
150         
151         for (i = rows.begin(); i != rows.end(); ++i) {
152                 if ((*i)[route_display_columns.tv] == tv) {
153                         (*i)[route_display_columns.text] = tv->name();
154                         break;
155                 }
156         } 
157
158 }
159
160 void
161 Editor::hide_track_in_display (TimeAxisView& tv)
162 {
163         TreeModel::Children rows = route_display_model->children();
164         TreeModel::Children::iterator i;
165
166         for (i = rows.begin(); i != rows.end(); ++i) {
167                 if ((*i)[route_display_columns.tv] == &tv) { 
168                         (*i)[route_display_columns.visible] = false;
169                         break;
170                 }
171         }
172
173         AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (&tv);
174
175         if (atv && current_mixer_strip && (atv->route() == current_mixer_strip->route())) {
176                 // this will hide the mixer strip
177                 set_selected_mixer_strip (tv);
178         }
179 }
180
181 void
182 Editor::show_track_in_display (TimeAxisView& tv)
183 {
184         TreeModel::Children rows = route_display_model->children();
185         TreeModel::Children::iterator i;
186         
187         for (i = rows.begin(); i != rows.end(); ++i) {
188                 if ((*i)[route_display_columns.tv] == &tv) { 
189                         (*i)[route_display_columns.visible] = true;
190                         tv.set_marked_for_display (true);
191                         break;
192                 }
193         }
194 }
195
196 void
197 Editor::sync_order_keys ()
198 {
199         vector<int> neworder;
200         TreeModel::Children rows = route_display_model->children();
201         TreeModel::Children::iterator ri;
202
203         if (ignore_route_order_sync || !session || (session->state_of_the_state() & Session::Loading) || rows.empty()) {
204                 return;
205         }
206
207         for (ri = rows.begin(); ri != rows.end(); ++ri) {
208                 neworder.push_back (0);
209         }
210
211         for (ri = rows.begin(); ri != rows.end(); ++ri) {
212                 TimeAxisView* tv = (*ri)[route_display_columns.tv];
213                 boost::shared_ptr<Route> route = (*ri)[route_display_columns.route];
214                 neworder[route->order_key (X_("signal"))] = tv->old_order_key ();
215         }
216
217         ignore_route_list_reorder = true;
218         route_display_model->reorder (neworder);
219         ignore_route_list_reorder = false;
220 }
221
222 void
223 Editor::redisplay_route_list ()
224 {
225         TreeModel::Children rows = route_display_model->children();
226         TreeModel::Children::iterator i;
227         uint32_t position;
228         uint32_t order;
229         int n;
230
231         if (no_route_list_redisplay) {
232                 return;
233         }
234
235         if (session && (rows.size() > session->nroutes())) {
236                 /* temporary condition during a drag-n-drop */
237                 return;
238         }
239
240         for (n = 0, order = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
241                 TimeAxisView *tv = (*i)[route_display_columns.tv];
242                 boost::shared_ptr<Route> route = (*i)[route_display_columns.route];
243
244                 if (tv == 0) {
245                         // just a "title" row
246                         continue;
247                 }
248
249                 if (!ignore_route_list_reorder) {
250                         
251                         /* this reorder is caused by user action, so reassign sort order keys
252                            to tracks.
253                         */
254                         
255                         route->set_order_key (N_("editor"), order);
256                 }
257
258                 tv->set_old_order_key (order);
259
260                 bool visible = (*i)[route_display_columns.visible];
261
262                 if (visible) {
263                         tv->set_marked_for_display (true);
264                         position += tv->show_at (position, n, &edit_controls_vbox);
265                 } else {
266                         tv->hide ();
267                 }
268                 
269                 ++order;
270                 ++n;
271         }
272
273         full_canvas_height = position;
274
275         /* make sure the cursors stay on top of every newly added track */
276
277         cursor_group->raise_to_top ();
278
279         reset_scrolling_region ();
280
281         if (Config->get_sync_all_route_ordering() && !ignore_route_list_reorder) {
282                 ignore_route_order_sync = true;
283                 Route::SyncOrderKeys (); // EMIT SIGNAL
284                 ignore_route_order_sync = false;
285         }
286 }
287
288 void
289 Editor::hide_all_tracks (bool with_select)
290 {
291         TreeModel::Children rows = route_display_model->children();
292         TreeModel::Children::iterator i;
293
294         no_route_list_redisplay = true;
295
296         for (i = rows.begin(); i != rows.end(); ++i) {
297                 
298                 TreeModel::Row row = (*i);
299                 TimeAxisView *tv = row[route_display_columns.tv];
300
301                 if (tv == 0) {
302                         continue;
303                 }
304                 
305                 row[route_display_columns.visible] = false;
306         }
307
308         no_route_list_redisplay = false;
309         redisplay_route_list ();
310
311         /* XXX this seems like a hack and half, but its not clear where to put this
312            otherwise.
313         */
314
315         reset_scrolling_region ();
316 }
317
318 void
319 Editor::build_route_list_menu ()
320 {
321         using namespace Menu_Helpers;
322         using namespace Gtk;
323
324         route_list_menu = new Menu;
325         
326         MenuList& items = route_list_menu->items();
327         route_list_menu->set_name ("ArdourContextMenu");
328
329         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::show_all_routes)));
330         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::hide_all_routes)));
331         items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Editor::show_all_audiotracks)));
332         items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Editor::hide_all_audiotracks)));
333         items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Editor::show_all_audiobus)));
334         items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Editor::hide_all_audiobus)));
335
336 }
337
338 void
339 Editor::set_all_tracks_visibility (bool yn)
340 {
341         TreeModel::Children rows = route_display_model->children();
342         TreeModel::Children::iterator i;
343
344         no_route_list_redisplay = true;
345
346         for (i = rows.begin(); i != rows.end(); ++i) {
347
348                 TreeModel::Row row = (*i);
349                 TimeAxisView* tv = row[route_display_columns.tv];
350
351                 if (tv == 0) {
352                         continue;
353                 }
354                 
355                 (*i)[route_display_columns.visible] = yn;
356         }
357
358         no_route_list_redisplay = false;
359         redisplay_route_list ();
360 }
361
362 void
363 Editor::set_all_audio_visibility (int tracks, bool yn) 
364 {
365         TreeModel::Children rows = route_display_model->children();
366         TreeModel::Children::iterator i;
367
368         no_route_list_redisplay = true;
369
370         for (i = rows.begin(); i != rows.end(); ++i) {
371                 TreeModel::Row row = (*i);
372                 TimeAxisView* tv = row[route_display_columns.tv];
373                 AudioTimeAxisView* atv;
374
375                 if (tv == 0) {
376                         continue;
377                 }
378
379                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
380                         switch (tracks) {
381                         case 0:
382                                 (*i)[route_display_columns.visible] = yn;
383                                 break;
384
385                         case 1:
386                                 if (atv->is_audio_track()) {
387                                         (*i)[route_display_columns.visible] = yn;
388                                 }
389                                 break;
390                                 
391                         case 2:
392                                 if (!atv->is_audio_track()) {
393                                         (*i)[route_display_columns.visible] = yn;
394                                 }
395                                 break;
396                         }
397                 }
398         }
399
400         no_route_list_redisplay = false;
401         redisplay_route_list ();
402 }
403
404 void
405 Editor::hide_all_routes ()
406 {
407         set_all_tracks_visibility (false);
408 }
409
410 void
411 Editor::show_all_routes ()
412 {
413         set_all_tracks_visibility (true);
414 }
415
416 void
417 Editor::show_all_audiobus ()
418 {
419         set_all_audio_visibility (2, true);
420 }
421 void
422 Editor::hide_all_audiobus ()
423 {
424         set_all_audio_visibility (2, false);
425 }
426
427 void
428 Editor::show_all_audiotracks()
429 {
430         set_all_audio_visibility (1, true);
431 }
432 void
433 Editor::hide_all_audiotracks ()
434 {
435         set_all_audio_visibility (1, false);
436 }
437
438 bool
439 Editor::route_list_display_button_press (GdkEventButton* ev)
440 {
441         if (Keyboard::is_context_menu_event (ev)) {
442                 show_route_list_menu ();
443                 return true;
444         }
445
446         TreeIter iter;
447         TreeModel::Path path;
448         TreeViewColumn* column;
449         int cellx;
450         int celly;
451         
452         if (!route_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
453                 return false;
454         }
455
456         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
457         case 0:
458                 if ((iter = route_display_model->get_iter (path))) {
459                         TimeAxisView* tv = (*iter)[route_display_columns.tv];
460                         if (tv) {
461                                 bool visible = (*iter)[route_display_columns.visible];
462                                 (*iter)[route_display_columns.visible] = !visible;
463                         }
464                 }
465                 return true;
466
467         case 1:
468                 /* allow normal processing to occur */
469                 return false;
470
471         default:
472                 break;
473         }
474
475         return false;
476 }
477
478 void
479 Editor::show_route_list_menu()
480 {
481         if (route_list_menu == 0) {
482                 build_route_list_menu ();
483         }
484
485         route_list_menu->popup (1, gtk_get_current_event_time());
486 }
487
488 bool
489 Editor::route_list_selection_filter (const Glib::RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
490 {
491         return true;
492 }
493
494 struct EditorOrderRouteSorter {
495     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
496             /* use of ">" forces the correct sort order */
497             return a->order_key ("editor") < b->order_key ("editor");
498     }
499 };
500
501 void
502 Editor::initial_route_list_display ()
503 {
504         boost::shared_ptr<Session::RouteList> routes = session->get_routes();
505         Session::RouteList r (*routes);
506         EditorOrderRouteSorter sorter;
507
508         r.sort (sorter);
509         
510         no_route_list_redisplay = true;
511
512         route_display_model->clear ();
513
514         handle_new_route (r);
515
516         no_route_list_redisplay = false;
517
518         redisplay_route_list ();
519 }
520
521 void
522 Editor::track_list_reorder (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter, int* new_order)
523 {
524         session->set_remote_control_ids();
525         redisplay_route_list ();
526 }
527
528 void
529 Editor::route_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
530 {
531         session->set_remote_control_ids();
532         redisplay_route_list ();
533 }
534
535 void
536 Editor::route_list_delete (const Gtk::TreeModel::Path& path)
537 {
538         session->set_remote_control_ids();
539         redisplay_route_list ();
540 }