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