Make MIDI selection actually show up.
[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 <list>
21 #include <vector>
22 #include <algorithm>
23 #include <cstdlib>
24 #include <cmath>
25 #include <cassert>
26
27 #include "editor.h"
28 #include "keyboard.h"
29 #include "ardour_ui.h"
30 #include "audio_time_axis.h"
31 #include "midi_time_axis.h"
32 #include "mixer_strip.h"
33 #include "gui_thread.h"
34 #include "actions.h"
35
36 #include <pbd/unknown_type.h>
37
38 #include <ardour/route.h>
39
40 #include "i18n.h"
41
42 using namespace sigc;
43 using namespace ARDOUR;
44 using namespace PBD;
45 using namespace Gtk;
46 using namespace Glib;
47
48 const char* _order_key = N_("editor");
49
50 void
51 Editor::handle_new_route (Session::RouteList& routes)
52 {
53         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_new_route), routes));
54         
55         TimeAxisView *tv;
56         RouteTimeAxisView *rtv;
57         TreeModel::Row parent;
58         TreeModel::Row row;
59
60         route_redisplay_does_not_sync_order_keys = true;
61         no_route_list_redisplay = true;
62
63         for (Session::RouteList::iterator x = routes.begin(); x != routes.end(); ++x) {
64                 boost::shared_ptr<Route> route = (*x);
65
66                 if (route->is_hidden()) {
67                         continue;
68                 }
69                 
70                 if (route->default_type() == ARDOUR::DataType::AUDIO)
71                         tv = new AudioTimeAxisView (*this, *session, route, *track_canvas);
72                 else if (route->default_type() == ARDOUR::DataType::MIDI)
73                         tv = new MidiTimeAxisView (*this, *session, route, *track_canvas);
74                 else
75                         throw unknown_type();
76
77                 //cerr << "Editor::handle_new_route() called on " << route->name() << endl;//DEBUG
78 #if 0
79                 if (route_display_model->children().size() == 0) {
80                         
81                         /* set up basic entries */
82                         
83                         TreeModel::Row row;
84                         
85                         row = *(route_display_model->append());  // path = "0"
86                         row[route_display_columns.text] = _("Busses");
87                         row[route_display_columns.tv] = 0;
88                         row = *(route_display_model->append());  // path = "1"
89                         row[route_display_columns.text] = _("Tracks");
90                         row[route_display_columns.tv] = 0;
91                         
92                 }
93                 
94                 if (dynamic_cast<AudioTrack*>(route.get()) != 0) {
95                         TreeModel::iterator iter = route_display_model->get_iter ("1");  // audio tracks 
96                         parent = *iter;
97                 } else {
98                         TreeModel::iterator iter = route_display_model->get_iter ("0");  // busses
99                         parent = *iter;
100                 }
101                 
102                 
103                 row = *(route_display_model->append (parent.children()));
104 #else 
105                 row = *(route_display_model->append ());
106 #endif
107
108                 // cerr << route->name() << " marked for display ? " << tv->marked_for_display() << endl;
109                 
110                 row[route_display_columns.text] = route->name();
111                 row[route_display_columns.visible] = tv->marked_for_display();
112                 row[route_display_columns.tv] = tv;
113                 row[route_display_columns.route] = route;
114
115                 track_views.push_back (tv);
116                 
117                 ignore_route_list_reorder = true;
118                 
119                 if ((rtv = dynamic_cast<RouteTimeAxisView*> (tv)) != 0) {
120                         /* added a new fresh one at the end */
121                         if (rtv->route()->order_key(_order_key) == -1) {
122                                 rtv->route()->set_order_key (_order_key, route_display_model->children().size()-1);
123                         }
124                         rtv->effective_gain_display ();
125                 }
126                 
127                 ignore_route_list_reorder = false;
128
129                 route->gui_changed.connect (mem_fun(*this, &Editor::handle_gui_changes));
130                 
131                 tv->GoingAway.connect (bind (mem_fun(*this, &Editor::remove_route), tv));
132         }
133
134         no_route_list_redisplay = false;
135
136         redisplay_route_list ();
137
138         if (show_editor_mixer_when_tracks_arrive) {
139                 show_editor_mixer (true);
140         }
141
142         editor_list_button.set_sensitive(true);
143         route_redisplay_does_not_sync_order_keys = false;
144 }
145
146 void
147 Editor::handle_gui_changes (const string & what, void *src)
148 {
149         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_gui_changes), what, src));
150
151         if (what == "track_height") {
152                 /* Optional :make tracks change height while it happens, instead 
153                    of on first-idle
154                 */
155                 //track_canvas->update_now ();
156                 redisplay_route_list ();
157         }
158
159         if (what == "visible_tracks") {
160                 redisplay_route_list ();
161         }
162 }
163
164 void
165 Editor::remove_route (TimeAxisView *tv)
166 {
167         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::remove_route), tv));
168
169         TrackViewList::iterator i;
170         TreeModel::Children rows = route_display_model->children();
171         TreeModel::Children::iterator ri;
172
173         if (tv == entered_track) {
174                 entered_track = 0;
175         }
176
177         /* the core model has changed, there is no need to sync 
178            view orders.
179         */
180
181         route_redisplay_does_not_sync_order_keys = true;
182
183         for (ri = rows.begin(); ri != rows.end(); ++ri) {
184                 if ((*ri)[route_display_columns.tv] == tv) {
185                         route_display_model->erase (ri);
186                         break;
187                 }
188         }
189
190         route_redisplay_does_not_sync_order_keys = false;
191
192         if ((i = find (track_views.begin(), track_views.end(), tv)) != track_views.end()) {
193                 track_views.erase (i);
194         }
195
196         /* since the editor mixer goes away when you remove a route, set the
197          * button to inactive and untick the menu option
198          */
199
200         ActionManager::uncheck_toggleaction ("<Actions>/Editor/show-editor-mixer");
201 }
202
203 void
204 Editor::route_name_changed (TimeAxisView *tv)
205 {
206         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::route_name_changed), tv));
207         
208         TreeModel::Children rows = route_display_model->children();
209         TreeModel::Children::iterator i;
210         
211         for (i = rows.begin(); i != rows.end(); ++i) {
212                 if ((*i)[route_display_columns.tv] == tv) {
213                         (*i)[route_display_columns.text] = tv->name();
214                         break;
215                 }
216         } 
217 }
218
219 void
220 Editor::update_route_visibility ()
221 {
222         TreeModel::Children rows = route_display_model->children();
223         TreeModel::Children::iterator i;
224         
225         no_route_list_redisplay = true;
226
227         for (i = rows.begin(); i != rows.end(); ++i) {
228                 TimeAxisView *tv = (*i)[route_display_columns.tv];
229                 (*i)[route_display_columns.visible] = tv->marked_for_display ();
230                 cerr << "marked " << tv->name() << " for display = " << tv->marked_for_display() << endl;
231         }
232
233         no_route_list_redisplay = false;
234         redisplay_route_list ();
235 }
236
237 void
238 Editor::hide_track_in_display (TimeAxisView& tv, bool temponly)
239 {
240         TreeModel::Children rows = route_display_model->children();
241         TreeModel::Children::iterator i;
242
243         for (i = rows.begin(); i != rows.end(); ++i) {
244                 if ((*i)[route_display_columns.tv] == &tv) { 
245                         (*i)[route_display_columns.visible] = false;
246                         break;
247                 }
248         }
249
250         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (&tv);
251
252         if (rtv && current_mixer_strip && (rtv->route() == current_mixer_strip->route())) {
253                 // this will hide the mixer strip
254                 set_selected_mixer_strip (tv);
255         }
256 }
257
258 void
259 Editor::show_track_in_display (TimeAxisView& tv)
260 {
261         TreeModel::Children rows = route_display_model->children();
262         TreeModel::Children::iterator i;
263         
264         for (i = rows.begin(); i != rows.end(); ++i) {
265                 if ((*i)[route_display_columns.tv] == &tv) { 
266                         (*i)[route_display_columns.visible] = true;
267                         break;
268                 }
269         }
270 }
271
272 void
273 Editor::route_list_reordered (const TreeModel::Path& path,const TreeModel::iterator& iter,int* what)
274 {
275         redisplay_route_list ();
276 }
277
278
279 void
280 Editor::sync_order_keys (const char *src)
281 {
282         vector<int> neworder;
283         TreeModel::Children rows = route_display_model->children();
284         TreeModel::Children::iterator ri;
285
286         if ((strcmp (src, _order_key) == 0) || !session || (session->state_of_the_state() & Session::Loading) || rows.empty()) {
287                 return;
288         }
289
290         for (ri = rows.begin(); ri != rows.end(); ++ri) {
291                 neworder.push_back (0);
292         }
293
294         bool changed = false;
295         int order;
296
297         for (order = 0, ri = rows.begin(); ri != rows.end(); ++ri, ++order) {
298                 boost::shared_ptr<Route> route = (*ri)[route_display_columns.route];
299
300                 int old_key = order;
301                 int new_key = route->order_key (_order_key);
302
303                 neworder[new_key] = old_key;
304
305                 if (new_key != old_key) {
306                         changed = true;
307                 }
308         }
309
310         if (changed) {
311                 route_redisplay_does_not_reset_order_keys = true;
312                 route_display_model->reorder (neworder);
313                 route_redisplay_does_not_reset_order_keys = false;
314         }
315 }
316
317 void
318 Editor::redisplay_route_list ()
319 {
320         TreeModel::Children rows = route_display_model->children();
321         TreeModel::Children::iterator i;
322         uint32_t position;
323         uint32_t order;
324         int n;
325
326         if (no_route_list_redisplay) {
327                 return;
328         }
329
330         for (n = 0, order = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
331                 TimeAxisView *tv = (*i)[route_display_columns.tv];
332                 boost::shared_ptr<Route> route = (*i)[route_display_columns.route];
333
334                 if (tv == 0) {
335                         // just a "title" row
336                         continue;
337                 }
338
339                 if (!route_redisplay_does_not_reset_order_keys) {
340                         
341                         /* this reorder is caused by user action, so reassign sort order keys
342                            to tracks.
343                         */
344                         
345                         route->set_order_key (_order_key, order);
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                         tv->clip_to_viewport ();
354                 } else {
355                         tv->set_marked_for_display (false);
356                         tv->hide ();
357                 }
358
359                 n++;
360                 
361         }
362
363         full_canvas_height = position + canvas_timebars_vsize;
364         vertical_adjustment.set_upper (full_canvas_height);
365         if ((vertical_adjustment.get_value() + canvas_height) > vertical_adjustment.get_upper()) {
366                 /* 
367                    We're increasing the size of the canvas while the bottom is visible.
368                    We scroll down to keep in step with the controls layout.
369                 */
370                 vertical_adjustment.set_value (full_canvas_height - canvas_height);
371         } 
372
373         if (!route_redisplay_does_not_reset_order_keys && !route_redisplay_does_not_sync_order_keys) {
374                 session->sync_order_keys (_order_key);
375         }
376 }
377
378 void
379 Editor::hide_all_tracks (bool with_select)
380 {
381         TreeModel::Children rows = route_display_model->children();
382         TreeModel::Children::iterator i;
383
384         no_route_list_redisplay = true;
385
386         for (i = rows.begin(); i != rows.end(); ++i) {
387                 
388                 TreeModel::Row row = (*i);
389                 TimeAxisView *tv = row[route_display_columns.tv];
390
391                 if (tv == 0) {
392                         continue;
393                 }
394                 
395                 row[route_display_columns.visible] = false;
396         }
397
398         no_route_list_redisplay = false;
399         redisplay_route_list ();
400
401         /* XXX this seems like a hack and half, but its not clear where to put this
402            otherwise.
403         */
404
405         //reset_scrolling_region ();
406 }
407
408 void
409 Editor::build_route_list_menu ()
410 {
411         using namespace Menu_Helpers;
412         using namespace Gtk;
413
414         route_list_menu = new Menu;
415         
416         MenuList& items = route_list_menu->items();
417         route_list_menu->set_name ("ArdourContextMenu");
418
419         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::show_all_routes)));
420         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::hide_all_routes)));
421         items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Editor::show_all_audiotracks)));
422         items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Editor::hide_all_audiotracks)));
423         items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Editor::show_all_audiobus)));
424         items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Editor::hide_all_audiobus)));
425
426 }
427
428 void
429 Editor::set_all_tracks_visibility (bool yn)
430 {
431         TreeModel::Children rows = route_display_model->children();
432         TreeModel::Children::iterator i;
433
434         no_route_list_redisplay = true;
435
436         for (i = rows.begin(); i != rows.end(); ++i) {
437
438                 TreeModel::Row row = (*i);
439                 TimeAxisView* tv = row[route_display_columns.tv];
440
441                 if (tv == 0) {
442                         continue;
443                 }
444                 
445                 (*i)[route_display_columns.visible] = yn;
446         }
447
448         no_route_list_redisplay = false;
449         redisplay_route_list ();
450 }
451
452 void
453 Editor::set_all_audio_visibility (int tracks, bool yn) 
454 {
455         TreeModel::Children rows = route_display_model->children();
456         TreeModel::Children::iterator i;
457
458         no_route_list_redisplay = true;
459
460         for (i = rows.begin(); i != rows.end(); ++i) {
461                 TreeModel::Row row = (*i);
462                 TimeAxisView* tv = row[route_display_columns.tv];
463                 AudioTimeAxisView* atv;
464
465                 if (tv == 0) {
466                         continue;
467                 }
468
469                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
470                         switch (tracks) {
471                         case 0:
472                                 (*i)[route_display_columns.visible] = yn;
473                                 break;
474
475                         case 1:
476                                 if (atv->is_audio_track()) {
477                                         (*i)[route_display_columns.visible] = yn;
478                                 }
479                                 break;
480                                 
481                         case 2:
482                                 if (!atv->is_audio_track()) {
483                                         (*i)[route_display_columns.visible] = yn;
484                                 }
485                                 break;
486                         }
487                 }
488         }
489
490         no_route_list_redisplay = false;
491         redisplay_route_list ();
492 }
493
494 void
495 Editor::hide_all_routes ()
496 {
497         set_all_tracks_visibility (false);
498 }
499
500 void
501 Editor::show_all_routes ()
502 {
503         set_all_tracks_visibility (true);
504 }
505
506 void
507 Editor::show_all_audiobus ()
508 {
509         set_all_audio_visibility (2, true);
510 }
511 void
512 Editor::hide_all_audiobus ()
513 {
514         set_all_audio_visibility (2, false);
515 }
516
517 void
518 Editor::show_all_audiotracks()
519 {
520         set_all_audio_visibility (1, true);
521 }
522 void
523 Editor::hide_all_audiotracks ()
524 {
525         set_all_audio_visibility (1, false);
526 }
527
528 bool
529 Editor::route_list_display_button_press (GdkEventButton* ev)
530 {
531         if (Keyboard::is_context_menu_event (ev)) {
532                 show_route_list_menu ();
533                 return true;
534         }
535
536         TreeIter iter;
537         TreeModel::Path path;
538         TreeViewColumn* column;
539         int cellx;
540         int celly;
541         
542         if (!route_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
543                 return false;
544         }
545
546         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
547         case 0:
548                 if ((iter = route_display_model->get_iter (path))) {
549                         TimeAxisView* tv = (*iter)[route_display_columns.tv];
550                         if (tv) {
551                                 bool visible = (*iter)[route_display_columns.visible];
552                                 (*iter)[route_display_columns.visible] = !visible;
553                         }
554                 }
555                 return true;
556
557         case 1:
558                 /* allow normal processing to occur */
559                 return false;
560
561         default:
562                 break;
563         }
564
565         return false;
566 }
567
568 void
569 Editor::show_route_list_menu()
570 {
571         if (route_list_menu == 0) {
572                 build_route_list_menu ();
573         }
574
575         route_list_menu->popup (1, gtk_get_current_event_time());
576 }
577
578 bool
579 Editor::route_list_selection_filter (const Glib::RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
580 {
581         return true;
582 }
583
584 struct EditorOrderRouteSorter {
585     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
586             /* use of ">" forces the correct sort order */
587             return a->order_key (_order_key) < b->order_key (_order_key);
588     }
589 };
590
591 void
592 Editor::initial_route_list_display ()
593 {
594         boost::shared_ptr<Session::RouteList> routes = session->get_routes();
595         Session::RouteList r (*routes);
596         EditorOrderRouteSorter sorter;
597
598         r.sort (sorter);
599         
600         no_route_list_redisplay = true;
601
602         route_display_model->clear ();
603
604         handle_new_route (r);
605
606         no_route_list_redisplay = false;
607
608         redisplay_route_list ();
609 }
610
611 void
612 Editor::track_list_reorder (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter, int* new_order)
613 {
614         route_redisplay_does_not_sync_order_keys = true;
615         session->set_remote_control_ids();
616         redisplay_route_list ();
617         route_redisplay_does_not_sync_order_keys = false;
618 }
619
620 void
621 Editor::route_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
622 {
623         /* never reset order keys because of a property change */
624         route_redisplay_does_not_reset_order_keys = true;
625         session->set_remote_control_ids();
626         redisplay_route_list ();
627         route_redisplay_does_not_reset_order_keys = false;
628 }
629
630 void
631 Editor::route_list_delete (const Gtk::TreeModel::Path& path)
632 {
633         /* this could require an order reset & sync */
634         session->set_remote_control_ids();
635         ignore_route_list_reorder = true;
636         redisplay_route_list ();
637         ignore_route_list_reorder = false;
638 }
639
640 void  
641 Editor::route_list_display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
642                                                 int x, int y, 
643                                                 const SelectionData& data,
644                                                 guint info, guint time)
645 {
646         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
647                 route_list_display.on_drag_data_received (context, x, y, data, info, time);
648                 return;
649         }
650         context->drag_finish (true, false, time);
651 }
652
653 RouteTimeAxisView*
654 Editor::get_route_view_by_id (PBD::ID& id)
655 {
656         RouteTimeAxisView* v;
657
658         for(TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
659                 if((v = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
660                         if(v->route()->id() == id) {
661                                 return v;
662                         }
663                 }
664         }
665
666         return 0;
667 }
668
669 void
670 Editor::foreach_time_axis_view (sigc::slot<void,TimeAxisView&> theslot)
671 {
672         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
673                 theslot (**i);
674         }
675 }
676
677 void
678 Editor::move_selected_tracks (bool up)
679 {
680         if (selection->tracks.empty()) {
681                 return;
682         }
683
684         typedef std::pair<TimeAxisView*,boost::shared_ptr<Route> > ViewRoute;
685         std::list<ViewRoute> view_routes;
686         std::vector<int> neworder;
687         TreeModel::Children rows = route_display_model->children();
688         TreeModel::Children::iterator ri;
689
690         for (ri = rows.begin(); ri != rows.end(); ++ri) {
691                 TimeAxisView* tv = (*ri)[route_display_columns.tv];
692                 boost::shared_ptr<Route> route = (*ri)[route_display_columns.route];
693
694                 view_routes.push_back (ViewRoute (tv, route));
695         }
696
697         list<ViewRoute>::iterator trailing;
698         list<ViewRoute>::iterator leading;
699         
700         if (up) {
701                 
702                 trailing = view_routes.begin();
703                 leading = view_routes.begin();
704                 
705                 ++leading;
706                 
707                 while (leading != view_routes.end()) {
708                         if (selection->selected (leading->first)) {
709                                 view_routes.insert (trailing, ViewRoute (leading->first, leading->second));
710                                 leading = view_routes.erase (leading);
711                         } else {
712                                 ++leading;
713                                 ++trailing;
714                         }
715                 }
716
717         } else {
718
719                 /* if we could use reverse_iterator in list::insert, this code
720                    would be a beautiful reflection of the code above. but we can't
721                    and so it looks like a bit of a mess.
722                 */
723
724                 trailing = view_routes.end();
725                 leading = view_routes.end();
726
727                 --leading; if (leading == view_routes.begin()) { return; }
728                 --leading;
729                 --trailing;
730
731                 while (1) {
732
733                         if (selection->selected (leading->first)) {
734                                 list<ViewRoute>::iterator tmp;
735
736                                 /* need to insert *after* trailing, not *before* it,
737                                    which is what insert (iter, val) normally does.
738                                 */
739
740                                 tmp = trailing;
741                                 tmp++;
742
743                                 view_routes.insert (tmp, ViewRoute (leading->first, leading->second));
744                                         
745                                 /* can't use iter = cont.erase (iter); form here, because
746                                    we need iter to move backwards.
747                                 */
748
749                                 tmp = leading;
750                                 --tmp;
751
752                                 bool done = false;
753
754                                 if (leading == view_routes.begin()) {
755                                         /* the one we've just inserted somewhere else
756                                            was the first in the list. erase this copy,
757                                            and then break, because we're done.
758                                         */
759                                         done = true;
760                                 }
761
762                                 view_routes.erase (leading);
763                                 
764                                 if (done) {
765                                         break;
766                                 }
767
768                                 leading = tmp;
769
770                         } else {
771                                 if (leading == view_routes.begin()) {
772                                         break;
773                                 }
774                                 --leading;
775                                 --trailing;
776                         }
777                 };
778         }
779
780         for (leading = view_routes.begin(); leading != view_routes.end(); ++leading) {
781                 neworder.push_back (leading->second->order_key (_order_key));
782         }
783
784         route_display_model->reorder (neworder);
785
786         session->sync_order_keys (_order_key);
787 }