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