Add a session file type icon for OSX, remove unused editor_mixer_button, use a consta...
[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
99 void
100 Editor::handle_gui_changes (const string & what, void *src)
101 {
102         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_gui_changes), what, src));
103         
104         if (what == "track_height") {
105                 /* Optional :make tracks change height while it happens, instead 
106                    of on first-idle
107                 */
108                 //track_canvas->update_now ();
109                 redisplay_route_list ();
110         }
111
112         if (what == "visible_tracks") {
113                 redisplay_route_list ();
114         }
115 }
116
117 void
118 Editor::remove_route (TimeAxisView *tv)
119 {
120         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::remove_route), tv));
121
122         TrackViewList::iterator i;
123         TreeModel::Children rows = route_display_model->children();
124         TreeModel::Children::iterator ri;
125
126         if (tv == entered_track) {
127                 entered_track = 0;
128         }
129
130         /* Decrement old order keys for tracks `above' the one that is being removed */
131         for (ri = rows.begin(); ri != rows.end(); ++ri) {
132                 TimeAxisView* v = (*ri)[route_display_columns.tv];
133                 if (v->old_order_key() > tv->old_order_key()) {
134                         v->set_old_order_key (v->old_order_key() - 1);
135                 }
136         }
137
138         for (ri = rows.begin(); ri != rows.end(); ++ri) {
139                 if ((*ri)[route_display_columns.tv] == tv) {
140                         route_display_model->erase (ri);
141                         break;
142                 }
143         }
144
145         if ((i = find (track_views.begin(), track_views.end(), tv)) != track_views.end()) {
146                 track_views.erase (i);
147         }
148
149         /* since the editor mixer goes away when you remove a route, set the
150          * button to inactive and untick the menu option
151          */
152
153         ActionManager::uncheck_toggleaction ("<Actions>/Editor/show-editor-mixer");
154 }
155
156 void
157 Editor::route_name_changed (TimeAxisView *tv)
158 {
159         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::route_name_changed), tv));
160         
161         TreeModel::Children rows = route_display_model->children();
162         TreeModel::Children::iterator i;
163         
164         for (i = rows.begin(); i != rows.end(); ++i) {
165                 if ((*i)[route_display_columns.tv] == tv) {
166                         (*i)[route_display_columns.text] = tv->name();
167                         break;
168                 }
169         } 
170 }
171
172 void
173 Editor::update_route_visibility ()
174 {
175         TreeModel::Children rows = route_display_model->children();
176         TreeModel::Children::iterator i;
177         
178         no_route_list_redisplay = true;
179
180         for (i = rows.begin(); i != rows.end(); ++i) {
181                 TimeAxisView *tv = (*i)[route_display_columns.tv];
182                 (*i)[route_display_columns.visible] = tv->marked_for_display ();
183         }
184
185         no_route_list_redisplay = false;
186         redisplay_route_list ();
187 }
188
189 void
190 Editor::hide_track_in_display (TimeAxisView& tv, bool temponly)
191 {
192         TreeModel::Children rows = route_display_model->children();
193         TreeModel::Children::iterator i;
194
195         for (i = rows.begin(); i != rows.end(); ++i) {
196                 if ((*i)[route_display_columns.tv] == &tv) { 
197                         (*i)[route_display_columns.visible] = false;
198                         // if (temponly) {
199                         tv.set_marked_for_display (false);
200                         // }
201                         break;
202                 }
203         }
204
205         AudioTimeAxisView* atv = dynamic_cast<AudioTimeAxisView*> (&tv);
206
207         if (atv && current_mixer_strip && (atv->route() == current_mixer_strip->route())) {
208                 // this will hide the mixer strip
209                 set_selected_mixer_strip (tv);
210         }
211 }
212
213 void
214 Editor::show_track_in_display (TimeAxisView& tv)
215 {
216         TreeModel::Children rows = route_display_model->children();
217         TreeModel::Children::iterator i;
218         
219         for (i = rows.begin(); i != rows.end(); ++i) {
220                 if ((*i)[route_display_columns.tv] == &tv) { 
221                         (*i)[route_display_columns.visible] = true;
222                         tv.set_marked_for_display (true);
223                         break;
224                 }
225         }
226 }
227
228 void
229 Editor::sync_order_keys ()
230 {
231         vector<int> neworder;
232         TreeModel::Children rows = route_display_model->children();
233         TreeModel::Children::iterator ri;
234
235         if (ignore_route_order_sync || !session || (session->state_of_the_state() & Session::Loading) || rows.empty()) {
236                 return;
237         }
238
239         for (ri = rows.begin(); ri != rows.end(); ++ri) {
240                 neworder.push_back (0);
241         }
242
243         for (ri = rows.begin(); ri != rows.end(); ++ri) {
244                 TimeAxisView* tv = (*ri)[route_display_columns.tv];
245                 boost::shared_ptr<Route> route = (*ri)[route_display_columns.route];
246                 neworder[route->order_key (X_("editor"))] = tv->old_order_key ();
247         }
248
249         ignore_route_list_reorder = true;
250         route_display_model->reorder (neworder);
251         ignore_route_list_reorder = false;
252 }
253
254 void
255 Editor::redisplay_route_list ()
256 {
257         TreeModel::Children rows = route_display_model->children();
258         TreeModel::Children::iterator i;
259         uint32_t position;
260         uint32_t order;
261         int n;
262
263         if (no_route_list_redisplay) {
264                 return;
265         }
266
267         if (session && (rows.size() > session->nroutes())) {
268                 /* temporary condition during a drag-n-drop */
269                 return;
270         }
271
272         for (n = 0, order = 0, position = 0, i = rows.begin(); i != rows.end(); ++i) {
273                 TimeAxisView *tv = (*i)[route_display_columns.tv];
274                 boost::shared_ptr<Route> route = (*i)[route_display_columns.route];
275
276                 if (tv == 0) {
277                         // just a "title" row
278                         continue;
279                 }
280
281                 if (!ignore_route_list_reorder) {
282                         
283                         /* this reorder is caused by user action, so reassign sort order keys
284                            to tracks.
285                         */
286                         
287                         route->set_order_key (N_("editor"), order);
288                 }
289
290                 tv->set_old_order_key (order);
291
292                 bool visible = (*i)[route_display_columns.visible];
293
294                 if (visible) {
295                         tv->set_marked_for_display (true);
296                         position += tv->show_at (position, n, &edit_controls_vbox);
297                 } else {
298                         tv->hide ();
299                 }
300                 
301                 ++order;
302                 ++n;
303         }
304
305         full_canvas_height = position;
306
307         vertical_adjustment.set_upper (position + canvas_timebars_vsize);
308         if ((vertical_adjustment.get_value() + canvas_height) > vertical_adjustment.get_upper()) {
309                 /* 
310                    We're increasing the size of the canvas while the bottom is visible.
311                    We scroll down to keep in step with the controls layout.
312                 */
313                 vertical_adjustment.set_value (position + canvas_timebars_vsize - canvas_height);
314         } 
315
316         if (Config->get_sync_all_route_ordering() && !ignore_route_list_reorder) {
317                 ignore_route_order_sync = true;
318                 Route::SyncOrderKeys (); // EMIT SIGNAL
319                 ignore_route_order_sync = false;
320         }
321 }
322
323 void
324 Editor::hide_all_tracks (bool with_select)
325 {
326         TreeModel::Children rows = route_display_model->children();
327         TreeModel::Children::iterator i;
328
329         no_route_list_redisplay = true;
330
331         for (i = rows.begin(); i != rows.end(); ++i) {
332                 
333                 TreeModel::Row row = (*i);
334                 TimeAxisView *tv = row[route_display_columns.tv];
335
336                 if (tv == 0) {
337                         continue;
338                 }
339                 
340                 row[route_display_columns.visible] = false;
341         }
342
343         no_route_list_redisplay = false;
344         redisplay_route_list ();
345
346         /* XXX this seems like a hack and half, but its not clear where to put this
347            otherwise.
348         */
349
350         //reset_scrolling_region ();
351 }
352
353 void
354 Editor::build_route_list_menu ()
355 {
356         using namespace Menu_Helpers;
357         using namespace Gtk;
358
359         route_list_menu = new Menu;
360         
361         MenuList& items = route_list_menu->items();
362         route_list_menu->set_name ("ArdourContextMenu");
363
364         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::show_all_routes)));
365         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::hide_all_routes)));
366         items.push_back (MenuElem (_("Show All Audio Tracks"), mem_fun(*this, &Editor::show_all_audiotracks)));
367         items.push_back (MenuElem (_("Hide All Audio Tracks"), mem_fun(*this, &Editor::hide_all_audiotracks)));
368         items.push_back (MenuElem (_("Show All Audio Busses"), mem_fun(*this, &Editor::show_all_audiobus)));
369         items.push_back (MenuElem (_("Hide All Audio Busses"), mem_fun(*this, &Editor::hide_all_audiobus)));
370
371 }
372
373 void
374 Editor::set_all_tracks_visibility (bool yn)
375 {
376         TreeModel::Children rows = route_display_model->children();
377         TreeModel::Children::iterator i;
378
379         no_route_list_redisplay = true;
380
381         for (i = rows.begin(); i != rows.end(); ++i) {
382
383                 TreeModel::Row row = (*i);
384                 TimeAxisView* tv = row[route_display_columns.tv];
385
386                 if (tv == 0) {
387                         continue;
388                 }
389                 
390                 (*i)[route_display_columns.visible] = yn;
391         }
392
393         no_route_list_redisplay = false;
394         redisplay_route_list ();
395 }
396
397 void
398 Editor::set_all_audio_visibility (int tracks, bool yn) 
399 {
400         TreeModel::Children rows = route_display_model->children();
401         TreeModel::Children::iterator i;
402
403         no_route_list_redisplay = true;
404
405         for (i = rows.begin(); i != rows.end(); ++i) {
406                 TreeModel::Row row = (*i);
407                 TimeAxisView* tv = row[route_display_columns.tv];
408                 AudioTimeAxisView* atv;
409
410                 if (tv == 0) {
411                         continue;
412                 }
413
414                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
415                         switch (tracks) {
416                         case 0:
417                                 (*i)[route_display_columns.visible] = yn;
418                                 break;
419
420                         case 1:
421                                 if (atv->is_audio_track()) {
422                                         (*i)[route_display_columns.visible] = yn;
423                                 }
424                                 break;
425                                 
426                         case 2:
427                                 if (!atv->is_audio_track()) {
428                                         (*i)[route_display_columns.visible] = yn;
429                                 }
430                                 break;
431                         }
432                 }
433         }
434
435         no_route_list_redisplay = false;
436         redisplay_route_list ();
437 }
438
439 void
440 Editor::hide_all_routes ()
441 {
442         set_all_tracks_visibility (false);
443 }
444
445 void
446 Editor::show_all_routes ()
447 {
448         set_all_tracks_visibility (true);
449 }
450
451 void
452 Editor::show_all_audiobus ()
453 {
454         set_all_audio_visibility (2, true);
455 }
456 void
457 Editor::hide_all_audiobus ()
458 {
459         set_all_audio_visibility (2, false);
460 }
461
462 void
463 Editor::show_all_audiotracks()
464 {
465         set_all_audio_visibility (1, true);
466 }
467 void
468 Editor::hide_all_audiotracks ()
469 {
470         set_all_audio_visibility (1, false);
471 }
472
473 bool
474 Editor::route_list_display_button_press (GdkEventButton* ev)
475 {
476         if (Keyboard::is_context_menu_event (ev)) {
477                 show_route_list_menu ();
478                 return true;
479         }
480
481         TreeIter iter;
482         TreeModel::Path path;
483         TreeViewColumn* column;
484         int cellx;
485         int celly;
486         
487         if (!route_list_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
488                 return false;
489         }
490
491         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
492         case 0:
493                 if ((iter = route_display_model->get_iter (path))) {
494                         TimeAxisView* tv = (*iter)[route_display_columns.tv];
495                         if (tv) {
496                                 bool visible = (*iter)[route_display_columns.visible];
497                                 (*iter)[route_display_columns.visible] = !visible;
498                         }
499                 }
500                 return true;
501
502         case 1:
503                 /* allow normal processing to occur */
504                 return false;
505
506         default:
507                 break;
508         }
509
510         return false;
511 }
512
513 void
514 Editor::show_route_list_menu()
515 {
516         if (route_list_menu == 0) {
517                 build_route_list_menu ();
518         }
519
520         route_list_menu->popup (1, gtk_get_current_event_time());
521 }
522
523 bool
524 Editor::route_list_selection_filter (const Glib::RefPtr<TreeModel>& model, const TreeModel::Path& path, bool yn)
525 {
526         return true;
527 }
528
529 struct EditorOrderRouteSorter {
530     bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
531             /* use of ">" forces the correct sort order */
532             return a->order_key ("editor") < b->order_key ("editor");
533     }
534 };
535
536 void
537 Editor::initial_route_list_display ()
538 {
539         boost::shared_ptr<Session::RouteList> routes = session->get_routes();
540         Session::RouteList r (*routes);
541         EditorOrderRouteSorter sorter;
542
543         r.sort (sorter);
544         
545         no_route_list_redisplay = true;
546
547         route_display_model->clear ();
548
549         handle_new_route (r);
550
551         no_route_list_redisplay = false;
552
553         redisplay_route_list ();
554 }
555
556 void
557 Editor::track_list_reorder (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter, int* new_order)
558 {
559         session->set_remote_control_ids();
560         redisplay_route_list ();
561 }
562
563 void
564 Editor::route_list_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
565 {
566         session->set_remote_control_ids();
567         redisplay_route_list ();
568 }
569
570 void
571 Editor::route_list_delete (const Gtk::TreeModel::Path& path)
572 {
573         session->set_remote_control_ids();
574         ignore_route_list_reorder = true;
575         redisplay_route_list ();
576         ignore_route_list_reorder = false;
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 }