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