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