remove all lines to avoid recompiles after commits
[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
24 #include "editor.h"
25 #include "keyboard.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         full_canvas_height = position;
270
271         /* make sure the cursors stay on top of every newly added track */
272
273         cursor_group->raise_to_top ();
274
275         reset_scrolling_region ();
276 }
277
278 void
279 Editor::hide_all_tracks (bool with_select)
280 {
281         TreeModel::Children rows = route_display_model->children();
282         TreeModel::Children::iterator i;
283
284         no_route_list_redisplay = true;
285
286         for (i = rows.begin(); i != rows.end(); ++i) {
287                 
288                 TreeModel::Row row = (*i);
289                 TimeAxisView *tv = row[route_display_columns.tv];
290
291                 if (tv == 0) {
292                         continue;
293                 }
294                 
295                 row[route_display_columns.visible] = false;
296         }
297
298         no_route_list_redisplay = false;
299         redisplay_route_list ();
300
301         /* XXX this seems like a hack and half, but its not clear where to put this
302            otherwise.
303         */
304
305         reset_scrolling_region ();
306 }
307
308 void
309 Editor::build_route_list_menu ()
310 {
311         using namespace Menu_Helpers;
312         using namespace Gtk;
313
314         route_list_menu = new Menu;
315         
316         MenuList& items = route_list_menu->items();
317         route_list_menu->set_name ("ArdourContextMenu");
318
319         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::show_all_routes)));
320         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::hide_all_routes)));
321         items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Editor::show_all_audiotracks)));
322         items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Editor::hide_all_audiotracks)));
323         items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Editor::show_all_audiobus)));
324         items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Editor::hide_all_audiobus)));
325
326 }
327
328 void
329 Editor::set_all_tracks_visibility (bool yn)
330 {
331         TreeModel::Children rows = route_display_model->children();
332         TreeModel::Children::iterator i;
333
334         no_route_list_redisplay = true;
335
336         for (i = rows.begin(); i != rows.end(); ++i) {
337
338                 TreeModel::Row row = (*i);
339                 TimeAxisView* tv = row[route_display_columns.tv];
340
341                 if (tv == 0) {
342                         continue;
343                 }
344                 
345                 (*i)[route_display_columns.visible] = yn;
346         }
347
348         no_route_list_redisplay = false;
349         redisplay_route_list ();
350 }
351
352 void
353 Editor::set_all_audio_visibility (int tracks, bool yn) 
354 {
355         TreeModel::Children rows = route_display_model->children();
356         TreeModel::Children::iterator i;
357
358         no_route_list_redisplay = true;
359
360         for (i = rows.begin(); i != rows.end(); ++i) {
361                 TreeModel::Row row = (*i);
362                 TimeAxisView* tv = row[route_display_columns.tv];
363                 AudioTimeAxisView* atv;
364
365                 if (tv == 0) {
366                         continue;
367                 }
368
369                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
370                         switch (tracks) {
371                         case 0:
372                                 (*i)[route_display_columns.visible] = yn;
373                                 break;
374
375                         case 1:
376                                 if (atv->is_audio_track()) {
377                                         (*i)[route_display_columns.visible] = yn;
378                                 }
379                                 break;
380                                 
381                         case 2:
382                                 if (!atv->is_audio_track()) {
383                                         (*i)[route_display_columns.visible] = yn;
384                                 }
385                                 break;
386                         }
387                 }
388         }
389
390         no_route_list_redisplay = false;
391         redisplay_route_list ();
392 }
393
394 void
395 Editor::hide_all_routes ()
396 {
397         set_all_tracks_visibility (false);
398 }
399
400 void
401 Editor::show_all_routes ()
402 {
403         set_all_tracks_visibility (true);
404 }
405
406 void
407 Editor::show_all_audiobus ()
408 {
409         set_all_audio_visibility (2, true);
410 }
411 void
412 Editor::hide_all_audiobus ()
413 {
414         set_all_audio_visibility (2, false);
415 }
416
417 void
418 Editor::show_all_audiotracks()
419 {
420         set_all_audio_visibility (1, true);
421 }
422 void
423 Editor::hide_all_audiotracks ()
424 {
425         set_all_audio_visibility (1, false);
426 }
427
428 bool
429 Editor::route_list_display_button_press (GdkEventButton* ev)
430 {
431         if (Keyboard::is_context_menu_event (ev)) {
432                 show_route_list_menu ();
433                 return true;
434         }
435
436         TreeIter iter;
437         TreeModel::Path path;
438         TreeViewColumn* column;
439         int cellx;
440         int celly;
441         
442         if (!route_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
443                 return false;
444         }
445
446         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
447         case 0:
448                 if ((iter = route_display_model->get_iter (path))) {
449                         TimeAxisView* tv = (*iter)[route_display_columns.tv];
450                         if (tv) {
451                                 bool visible = (*iter)[route_display_columns.visible];
452                                 (*iter)[route_display_columns.visible] = !visible;
453                         }
454                 }
455                 return true;
456
457         case 1:
458                 /* allow normal processing to occur */
459                 return false;
460
461         default:
462                 break;
463         }
464
465         return false;
466 }
467
468 void
469 Editor::show_route_list_menu()
470 {
471         if (route_list_menu == 0) {
472                 build_route_list_menu ();
473         }
474
475         route_list_menu->popup (1, gtk_get_current_event_time());
476 }
477
478 bool
479 Editor::route_list_selection_filter (const Glib::RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
480 {
481         return true;
482 }
483
484 struct EditorOrderRouteSorter {
485     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
486             /* use of ">" forces the correct sort order */
487             return a->order_key ("editor") < b->order_key ("editor");
488     }
489 };
490
491 void
492 Editor::initial_route_list_display ()
493 {
494         boost::shared_ptr<Session::RouteList> routes = session->get_routes();
495         Session::RouteList r (*routes);
496         EditorOrderRouteSorter sorter;
497
498         r.sort (sorter);
499         
500         no_route_list_redisplay = true;
501
502         route_display_model->clear ();
503
504         handle_new_route (r);
505
506         no_route_list_redisplay = false;
507
508         redisplay_route_list ();
509 }
510
511 void
512 Editor::route_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
513 {
514         session->set_remote_control_ids();
515         redisplay_route_list ();
516 }
517
518 void
519 Editor::route_list_delete (const Gtk::TreeModel::Path& path)
520 {
521         session->set_remote_control_ids();
522         redisplay_route_list ();
523 }