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