make link (rubberband) work
[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                 /* make tracks change height while it happens, instead 
152                    of on first-idle
153                 */
154                 track_canvas->update_now ();
155                 redisplay_route_list ();
156         }
157
158         if (what == "visible_tracks") {
159                 redisplay_route_list ();
160         }
161 }
162
163 void
164 Editor::remove_route (TimeAxisView *tv)
165 {
166         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::remove_route), tv));
167
168         TrackViewList::iterator i;
169         TreeModel::Children rows = route_display_model->children();
170         TreeModel::Children::iterator ri;
171
172         if (tv == entered_track) {
173                 entered_track = 0;
174         }
175
176         /* Decrement old order keys for tracks `above' the one that is being removed */
177         for (ri = rows.begin(); ri != rows.end(); ++ri) {
178                 TimeAxisView* v = (*ri)[route_display_columns.tv];
179                 if (v->old_order_key() > tv->old_order_key()) {
180                         v->set_old_order_key (v->old_order_key() - 1);
181                 }
182         }
183
184         for (ri = rows.begin(); ri != rows.end(); ++ri) {
185                 if ((*ri)[route_display_columns.tv] == tv) {
186                         route_display_model->erase (ri);
187                         break;
188                 }
189         }
190
191         if ((i = find (track_views.begin(), track_views.end(), tv)) != track_views.end()) {
192                 track_views.erase (i);
193         }
194
195         /* since the editor mixer goes away when you remove a route, set the
196          * button to inactive and untick the menu option
197          */
198
199         editor_mixer_button.set_active(false);
200         ActionManager::uncheck_toggleaction ("<Actions>/Editor/show-editor-mixer");
201
202         /* and disable if all tracks and/or routes are gone */
203
204         if (track_views.size() == 0) {
205                 editor_mixer_button.set_sensitive(false);
206         }
207 }
208
209 void
210 Editor::route_name_changed (TimeAxisView *tv)
211 {
212         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::route_name_changed), tv));
213         
214         TreeModel::Children rows = route_display_model->children();
215         TreeModel::Children::iterator i;
216         
217         for (i = rows.begin(); i != rows.end(); ++i) {
218                 if ((*i)[route_display_columns.tv] == tv) {
219                         (*i)[route_display_columns.text] = tv->name();
220                         break;
221                 }
222         } 
223 }
224
225 void
226 Editor::update_route_visibility ()
227 {
228         TreeModel::Children rows = route_display_model->children();
229         TreeModel::Children::iterator i;
230         
231         no_route_list_redisplay = true;
232
233         for (i = rows.begin(); i != rows.end(); ++i) {
234                 TimeAxisView *tv = (*i)[route_display_columns.tv];
235                 (*i)[route_display_columns.visible] = tv->marked_for_display ();
236         }
237
238         no_route_list_redisplay = false;
239         redisplay_route_list ();
240 }
241
242 void
243 Editor::hide_track_in_display (TimeAxisView& tv, bool temponly)
244 {
245         TreeModel::Children rows = route_display_model->children();
246         TreeModel::Children::iterator i;
247
248         for (i = rows.begin(); i != rows.end(); ++i) {
249                 if ((*i)[route_display_columns.tv] == &tv) { 
250                         (*i)[route_display_columns.visible] = false;
251                         // if (temponly) {
252                         tv.set_marked_for_display (false);
253                         // }
254                         break;
255                 }
256         }
257
258         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
259
260         if (rtv && current_mixer_strip && (rtv->route() == current_mixer_strip->route())) {
261                 // this will hide the mixer strip
262                 set_selected_mixer_strip (tv);
263         }
264 }
265
266 void
267 Editor::show_track_in_display (TimeAxisView& tv)
268 {
269         TreeModel::Children rows = route_display_model->children();
270         TreeModel::Children::iterator i;
271         
272         for (i = rows.begin(); i != rows.end(); ++i) {
273                 if ((*i)[route_display_columns.tv] == &tv) { 
274                         (*i)[route_display_columns.visible] = true;
275                         tv.set_marked_for_display (true);
276                         break;
277                 }
278         }
279 }
280
281 void
282 Editor::route_list_reordered (const TreeModel::Path& path,const TreeModel::iterator& iter,int* what)
283 {
284         redisplay_route_list ();
285 }
286
287
288 void
289 Editor::sync_order_keys ()
290 {
291         vector<int> neworder;
292         TreeModel::Children rows = route_display_model->children();
293         TreeModel::Children::iterator ri;
294
295         if (ignore_route_order_sync || !session || (session->state_of_the_state() & Session::Loading) || rows.empty()) {
296                 return;
297         }
298
299         for (ri = rows.begin(); ri != rows.end(); ++ri) {
300                 neworder.push_back (0);
301         }
302
303         for (ri = rows.begin(); ri != rows.end(); ++ri) {
304                 TimeAxisView* tv = (*ri)[route_display_columns.tv];
305                 boost::shared_ptr<Route> route = (*ri)[route_display_columns.route];
306                 neworder[route->order_key (X_("editor"))] = tv->old_order_key ();
307         }
308
309         ignore_route_list_reorder = true;
310         route_display_model->reorder (neworder);
311         ignore_route_list_reorder = false;
312 }
313
314 void
315 Editor::redisplay_route_list ()
316 {
317         TreeModel::Children rows = route_display_model->children();
318         TreeModel::Children::iterator i;
319         uint32_t position;
320         uint32_t order;
321         int n;
322         
323         if (no_route_list_redisplay) {
324                 return;
325         }
326
327         for (n = 0, order = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
328                 TimeAxisView *tv = (*i)[route_display_columns.tv];
329                 RouteTimeAxisView* rt; 
330
331                 if (tv == 0) {
332                         // just a "title" row
333                         continue;
334                 }
335
336                 if (!ignore_route_list_reorder) {
337                         
338                         /* this reorder is caused by user action, so reassign sort order keys
339                            to tracks.
340                         */
341                         
342                         if ((rt = dynamic_cast<RouteTimeAxisView*> (tv)) != 0) {
343                                 rt->route()->set_order_key (N_("editor"), order);
344                                 ++order;
345                         }
346                 }
347
348                 bool visible = (*i)[route_display_columns.visible];
349
350                 if (visible) {
351                         tv->set_marked_for_display (true);
352                         position += tv->show_at (position, n, &edit_controls_vbox);
353                 } else {
354                         tv->hide ();
355                 }
356                 
357                 n++;
358                 
359         }
360
361         full_canvas_height = position;
362
363         /* make sure the cursors stay on top of every newly added track */
364
365         cursor_group->raise_to_top ();
366
367         //reset_scrolling_region ();
368
369         if (Config->get_sync_all_route_ordering() && !ignore_route_list_reorder) {
370                 ignore_route_order_sync = true;
371                 Route::SyncOrderKeys (); // EMIT SIGNAL
372                 ignore_route_order_sync = false;
373         }
374
375 }
376
377 void
378 Editor::hide_all_tracks (bool with_select)
379 {
380         TreeModel::Children rows = route_display_model->children();
381         TreeModel::Children::iterator i;
382
383         no_route_list_redisplay = true;
384
385         for (i = rows.begin(); i != rows.end(); ++i) {
386                 
387                 TreeModel::Row row = (*i);
388                 TimeAxisView *tv = row[route_display_columns.tv];
389
390                 if (tv == 0) {
391                         continue;
392                 }
393                 
394                 row[route_display_columns.visible] = false;
395         }
396
397         no_route_list_redisplay = false;
398         redisplay_route_list ();
399
400         /* XXX this seems like a hack and half, but its not clear where to put this
401            otherwise.
402         */
403
404         //reset_scrolling_region ();
405 }
406
407 void
408 Editor::build_route_list_menu ()
409 {
410         using namespace Menu_Helpers;
411         using namespace Gtk;
412
413         route_list_menu = new Menu;
414         
415         MenuList& items = route_list_menu->items();
416         route_list_menu->set_name ("ArdourContextMenu");
417
418         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::show_all_routes)));
419         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::hide_all_routes)));
420         items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Editor::show_all_audiotracks)));
421         items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Editor::hide_all_audiotracks)));
422         items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Editor::show_all_audiobus)));
423         items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Editor::hide_all_audiobus)));
424
425 }
426
427 void
428 Editor::set_all_tracks_visibility (bool yn)
429 {
430         TreeModel::Children rows = route_display_model->children();
431         TreeModel::Children::iterator i;
432
433         no_route_list_redisplay = true;
434
435         for (i = rows.begin(); i != rows.end(); ++i) {
436
437                 TreeModel::Row row = (*i);
438                 TimeAxisView* tv = row[route_display_columns.tv];
439
440                 if (tv == 0) {
441                         continue;
442                 }
443                 
444                 (*i)[route_display_columns.visible] = yn;
445         }
446
447         no_route_list_redisplay = false;
448         redisplay_route_list ();
449 }
450
451 void
452 Editor::set_all_audio_visibility (int tracks, bool yn) 
453 {
454         TreeModel::Children rows = route_display_model->children();
455         TreeModel::Children::iterator i;
456
457         no_route_list_redisplay = true;
458
459         for (i = rows.begin(); i != rows.end(); ++i) {
460                 TreeModel::Row row = (*i);
461                 TimeAxisView* tv = row[route_display_columns.tv];
462                 AudioTimeAxisView* atv;
463
464                 if (tv == 0) {
465                         continue;
466                 }
467
468                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
469                         switch (tracks) {
470                         case 0:
471                                 (*i)[route_display_columns.visible] = yn;
472                                 break;
473
474                         case 1:
475                                 if (atv->is_audio_track()) {
476                                         (*i)[route_display_columns.visible] = yn;
477                                 }
478                                 break;
479                                 
480                         case 2:
481                                 if (!atv->is_audio_track()) {
482                                         (*i)[route_display_columns.visible] = yn;
483                                 }
484                                 break;
485                         }
486                 }
487         }
488
489         no_route_list_redisplay = false;
490         redisplay_route_list ();
491 }
492
493 void
494 Editor::hide_all_routes ()
495 {
496         set_all_tracks_visibility (false);
497 }
498
499 void
500 Editor::show_all_routes ()
501 {
502         set_all_tracks_visibility (true);
503 }
504
505 void
506 Editor::show_all_audiobus ()
507 {
508         set_all_audio_visibility (2, true);
509 }
510 void
511 Editor::hide_all_audiobus ()
512 {
513         set_all_audio_visibility (2, false);
514 }
515
516 void
517 Editor::show_all_audiotracks()
518 {
519         set_all_audio_visibility (1, true);
520 }
521 void
522 Editor::hide_all_audiotracks ()
523 {
524         set_all_audio_visibility (1, false);
525 }
526
527 bool
528 Editor::route_list_display_button_press (GdkEventButton* ev)
529 {
530         if (Keyboard::is_context_menu_event (ev)) {
531                 show_route_list_menu ();
532                 return true;
533         }
534
535         TreeIter iter;
536         TreeModel::Path path;
537         TreeViewColumn* column;
538         int cellx;
539         int celly;
540         
541         if (!route_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
542                 return false;
543         }
544
545         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
546         case 0:
547                 if ((iter = route_display_model->get_iter (path))) {
548                         TimeAxisView* tv = (*iter)[route_display_columns.tv];
549                         if (tv) {
550                                 bool visible = (*iter)[route_display_columns.visible];
551                                 (*iter)[route_display_columns.visible] = !visible;
552                         }
553                 }
554                 return true;
555
556         case 1:
557                 /* allow normal processing to occur */
558                 return false;
559
560         default:
561                 break;
562         }
563
564         return false;
565 }
566
567 void
568 Editor::show_route_list_menu()
569 {
570         if (route_list_menu == 0) {
571                 build_route_list_menu ();
572         }
573
574         route_list_menu->popup (1, gtk_get_current_event_time());
575 }
576
577 bool
578 Editor::route_list_selection_filter (const Glib::RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
579 {
580         return true;
581 }
582
583 struct EditorOrderRouteSorter {
584     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
585             /* use of ">" forces the correct sort order */
586             return a->order_key ("editor") < b->order_key ("editor");
587     }
588 };
589
590 void
591 Editor::initial_route_list_display ()
592 {
593         boost::shared_ptr<Session::RouteList> routes = session->get_routes();
594         Session::RouteList r (*routes);
595         EditorOrderRouteSorter sorter;
596
597         r.sort (sorter);
598         
599         no_route_list_redisplay = true;
600
601         route_display_model->clear ();
602
603         handle_new_route (r);
604
605         no_route_list_redisplay = false;
606
607         redisplay_route_list ();
608 }
609
610 void
611 Editor::track_list_reorder (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter, int* new_order)
612 {
613         session->set_remote_control_ids();
614         redisplay_route_list ();
615 }
616
617 void
618 Editor::route_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
619 {
620         session->set_remote_control_ids();
621         redisplay_route_list ();
622 }
623
624 void
625 Editor::route_list_delete (const Gtk::TreeModel::Path& path)
626 {
627         session->set_remote_control_ids();
628         ignore_route_list_reorder = true;
629         redisplay_route_list ();
630         ignore_route_list_reorder = false;
631 }
632
633 void  
634 Editor::route_list_display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
635                                                 int x, int y, 
636                                                 const SelectionData& data,
637                                                 guint info, guint time)
638 {
639         cerr << "RouteLD::dddr target = " << data.get_target() << endl;
640         
641         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
642                 cerr << "Delete drag data drop to treeview\n";
643                 route_list_display.on_drag_data_received (context, x, y, data, info, time);
644                 return;
645         }
646         cerr << "some other kind of drag\n";
647         context->drag_finish (true, false, time);
648 }
649
650 RouteTimeAxisView*
651 Editor::get_route_view_by_id (PBD::ID& id)
652 {
653         RouteTimeAxisView* v;
654
655         for(TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
656                 if((v = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
657                         if(v->route()->id() == id) {
658                                 return v;
659                         }
660                 }
661         }
662
663         return 0;
664 }
665
666 void
667 Editor::foreach_time_axis_view (sigc::slot<void,TimeAxisView&> theslot)
668 {
669         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
670                 theslot (**i);
671         }
672 }