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