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