Clean up checks.
[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 (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 (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                 /* show or hide the TimeAxisView */
351                 if (visible) {
352                         tv->set_marked_for_display (true);
353                         position += tv->show_at (position, n, &edit_controls_vbox);
354                         tv->clip_to_viewport ();
355                 } else {
356                         tv->set_marked_for_display (false);
357                         tv->hide ();
358                 }
359
360                 n++;
361                 
362         }
363
364         full_canvas_height = position + canvas_timebars_vsize;
365         vertical_adjustment.set_upper (full_canvas_height);
366         if ((vertical_adjustment.get_value() + canvas_height) > vertical_adjustment.get_upper()) {
367                 /* 
368                    We're increasing the size of the canvas while the bottom is visible.
369                    We scroll down to keep in step with the controls layout.
370                 */
371                 vertical_adjustment.set_value (full_canvas_height - canvas_height);
372         } 
373
374         if (!route_redisplay_does_not_reset_order_keys && !route_redisplay_does_not_sync_order_keys) {
375                 session->sync_order_keys (_order_key);
376         }
377 }
378
379 void
380 Editor::hide_all_tracks (bool with_select)
381 {
382         TreeModel::Children rows = route_display_model->children();
383         TreeModel::Children::iterator i;
384
385         no_route_list_redisplay = true;
386
387         for (i = rows.begin(); i != rows.end(); ++i) {
388                 
389                 TreeModel::Row row = (*i);
390                 TimeAxisView *tv = row[route_display_columns.tv];
391
392                 if (tv == 0) {
393                         continue;
394                 }
395                 
396                 row[route_display_columns.visible] = false;
397         }
398
399         no_route_list_redisplay = false;
400         redisplay_route_list ();
401
402         /* XXX this seems like a hack and half, but its not clear where to put this
403            otherwise.
404         */
405
406         //reset_scrolling_region ();
407 }
408
409 void
410 Editor::build_route_list_menu ()
411 {
412         using namespace Menu_Helpers;
413         using namespace Gtk;
414
415         route_list_menu = new Menu;
416         
417         MenuList& items = route_list_menu->items();
418         route_list_menu->set_name ("ArdourContextMenu");
419
420         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::show_all_routes)));
421         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::hide_all_routes)));
422         items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Editor::show_all_audiotracks)));
423         items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Editor::hide_all_audiotracks)));
424         items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Editor::show_all_audiobus)));
425         items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Editor::hide_all_audiobus)));
426
427 }
428
429 void
430 Editor::set_all_tracks_visibility (bool yn)
431 {
432         TreeModel::Children rows = route_display_model->children();
433         TreeModel::Children::iterator i;
434
435         no_route_list_redisplay = true;
436
437         for (i = rows.begin(); i != rows.end(); ++i) {
438
439                 TreeModel::Row row = (*i);
440                 TimeAxisView* tv = row[route_display_columns.tv];
441
442                 if (tv == 0) {
443                         continue;
444                 }
445                 
446                 (*i)[route_display_columns.visible] = yn;
447         }
448
449         no_route_list_redisplay = false;
450         redisplay_route_list ();
451 }
452
453 void
454 Editor::set_all_audio_visibility (int tracks, bool yn) 
455 {
456         TreeModel::Children rows = route_display_model->children();
457         TreeModel::Children::iterator i;
458
459         no_route_list_redisplay = true;
460
461         for (i = rows.begin(); i != rows.end(); ++i) {
462                 TreeModel::Row row = (*i);
463                 TimeAxisView* tv = row[route_display_columns.tv];
464                 AudioTimeAxisView* atv;
465
466                 if (tv == 0) {
467                         continue;
468                 }
469
470                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
471                         switch (tracks) {
472                         case 0:
473                                 (*i)[route_display_columns.visible] = yn;
474                                 break;
475
476                         case 1:
477                                 if (atv->is_audio_track()) {
478                                         (*i)[route_display_columns.visible] = yn;
479                                 }
480                                 break;
481                                 
482                         case 2:
483                                 if (!atv->is_audio_track()) {
484                                         (*i)[route_display_columns.visible] = yn;
485                                 }
486                                 break;
487                         }
488                 }
489         }
490
491         no_route_list_redisplay = false;
492         redisplay_route_list ();
493 }
494
495 void
496 Editor::hide_all_routes ()
497 {
498         set_all_tracks_visibility (false);
499 }
500
501 void
502 Editor::show_all_routes ()
503 {
504         set_all_tracks_visibility (true);
505 }
506
507 void
508 Editor::show_all_audiobus ()
509 {
510         set_all_audio_visibility (2, true);
511 }
512 void
513 Editor::hide_all_audiobus ()
514 {
515         set_all_audio_visibility (2, false);
516 }
517
518 void
519 Editor::show_all_audiotracks()
520 {
521         set_all_audio_visibility (1, true);
522 }
523 void
524 Editor::hide_all_audiotracks ()
525 {
526         set_all_audio_visibility (1, false);
527 }
528
529 bool
530 Editor::route_list_display_button_press (GdkEventButton* ev)
531 {
532         if (Keyboard::is_context_menu_event (ev)) {
533                 show_route_list_menu ();
534                 return true;
535         }
536
537         TreeIter iter;
538         TreeModel::Path path;
539         TreeViewColumn* column;
540         int cellx;
541         int celly;
542         
543         if (!route_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
544                 return false;
545         }
546
547         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
548         case 0:
549                 if ((iter = route_display_model->get_iter (path))) {
550                         TimeAxisView* tv = (*iter)[route_display_columns.tv];
551                         if (tv) {
552                                 bool visible = (*iter)[route_display_columns.visible];
553                                 (*iter)[route_display_columns.visible] = !visible;
554                         }
555                 }
556                 return true;
557
558         case 1:
559                 /* allow normal processing to occur */
560                 return false;
561
562         default:
563                 break;
564         }
565
566         return false;
567 }
568
569 void
570 Editor::show_route_list_menu()
571 {
572         if (route_list_menu == 0) {
573                 build_route_list_menu ();
574         }
575
576         route_list_menu->popup (1, gtk_get_current_event_time());
577 }
578
579 bool
580 Editor::route_list_selection_filter (const Glib::RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
581 {
582         return true;
583 }
584
585 struct EditorOrderRouteSorter {
586     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
587             /* use of ">" forces the correct sort order */
588             return a->order_key (_order_key) < b->order_key (_order_key);
589     }
590 };
591
592 void
593 Editor::initial_route_list_display ()
594 {
595         boost::shared_ptr<RouteList> routes = session->get_routes();
596         RouteList r (*routes);
597         EditorOrderRouteSorter sorter;
598
599         r.sort (sorter);
600         
601         no_route_list_redisplay = true;
602
603         route_display_model->clear ();
604
605         handle_new_route (r);
606
607         no_route_list_redisplay = false;
608
609         redisplay_route_list ();
610 }
611
612 void
613 Editor::track_list_reorder (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter, int* new_order)
614 {
615         route_redisplay_does_not_sync_order_keys = true;
616         session->set_remote_control_ids();
617         redisplay_route_list ();
618         route_redisplay_does_not_sync_order_keys = false;
619 }
620
621 void
622 Editor::route_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
623 {
624         /* never reset order keys because of a property change */
625         route_redisplay_does_not_reset_order_keys = true;
626         session->set_remote_control_ids();
627         redisplay_route_list ();
628         route_redisplay_does_not_reset_order_keys = false;
629 }
630
631 void
632 Editor::route_list_delete (const Gtk::TreeModel::Path& path)
633 {
634         /* this could require an order reset & sync */
635         session->set_remote_control_ids();
636         ignore_route_list_reorder = true;
637         redisplay_route_list ();
638         ignore_route_list_reorder = false;
639 }
640
641 void  
642 Editor::route_list_display_drag_data_received (const RefPtr<Gdk::DragContext>& context,
643                                                 int x, int y, 
644                                                 const SelectionData& data,
645                                                 guint info, guint time)
646 {
647         if (data.get_target() == "GTK_TREE_MODEL_ROW") {
648                 route_list_display.on_drag_data_received (context, x, y, data, info, time);
649                 return;
650         }
651         context->drag_finish (true, false, time);
652 }
653
654 RouteTimeAxisView*
655 Editor::get_route_view_by_id (PBD::ID& id)
656 {
657         RouteTimeAxisView* v;
658
659         for(TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
660                 if((v = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
661                         if(v->route()->id() == id) {
662                                 return v;
663                         }
664                 }
665         }
666
667         return 0;
668 }
669
670 void
671 Editor::foreach_time_axis_view (sigc::slot<void,TimeAxisView&> theslot)
672 {
673         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
674                 theslot (**i);
675         }
676 }
677
678 void
679 Editor::move_selected_tracks (bool up)
680 {
681         if (selection->tracks.empty()) {
682                 return;
683         }
684
685         typedef std::pair<TimeAxisView*,boost::shared_ptr<Route> > ViewRoute;
686         std::list<ViewRoute> view_routes;
687         std::vector<int> neworder;
688         TreeModel::Children rows = route_display_model->children();
689         TreeModel::Children::iterator ri;
690
691         for (ri = rows.begin(); ri != rows.end(); ++ri) {
692                 TimeAxisView* tv = (*ri)[route_display_columns.tv];
693                 boost::shared_ptr<Route> route = (*ri)[route_display_columns.route];
694
695                 view_routes.push_back (ViewRoute (tv, route));
696         }
697
698         list<ViewRoute>::iterator trailing;
699         list<ViewRoute>::iterator leading;
700         
701         if (up) {
702                 
703                 trailing = view_routes.begin();
704                 leading = view_routes.begin();
705                 
706                 ++leading;
707                 
708                 while (leading != view_routes.end()) {
709                         if (selection->selected (leading->first)) {
710                                 view_routes.insert (trailing, ViewRoute (leading->first, leading->second));
711                                 leading = view_routes.erase (leading);
712                         } else {
713                                 ++leading;
714                                 ++trailing;
715                         }
716                 }
717
718         } else {
719
720                 /* if we could use reverse_iterator in list::insert, this code
721                    would be a beautiful reflection of the code above. but we can't
722                    and so it looks like a bit of a mess.
723                 */
724
725                 trailing = view_routes.end();
726                 leading = view_routes.end();
727
728                 --leading; if (leading == view_routes.begin()) { return; }
729                 --leading;
730                 --trailing;
731
732                 while (1) {
733
734                         if (selection->selected (leading->first)) {
735                                 list<ViewRoute>::iterator tmp;
736
737                                 /* need to insert *after* trailing, not *before* it,
738                                    which is what insert (iter, val) normally does.
739                                 */
740
741                                 tmp = trailing;
742                                 tmp++;
743
744                                 view_routes.insert (tmp, ViewRoute (leading->first, leading->second));
745                                         
746                                 /* can't use iter = cont.erase (iter); form here, because
747                                    we need iter to move backwards.
748                                 */
749
750                                 tmp = leading;
751                                 --tmp;
752
753                                 bool done = false;
754
755                                 if (leading == view_routes.begin()) {
756                                         /* the one we've just inserted somewhere else
757                                            was the first in the list. erase this copy,
758                                            and then break, because we're done.
759                                         */
760                                         done = true;
761                                 }
762
763                                 view_routes.erase (leading);
764                                 
765                                 if (done) {
766                                         break;
767                                 }
768
769                                 leading = tmp;
770
771                         } else {
772                                 if (leading == view_routes.begin()) {
773                                         break;
774                                 }
775                                 --leading;
776                                 --trailing;
777                         }
778                 };
779         }
780
781         for (leading = view_routes.begin(); leading != view_routes.end(); ++leading) {
782                 neworder.push_back (leading->second->order_key (_order_key));
783         }
784
785         route_display_model->reorder (neworder);
786
787         session->sync_order_keys (_order_key);
788 }