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