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