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