s/stringcr_t/const string &/
[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     $Id$
19 */
20
21 #include <algorithm>
22 #include <cstdlib>
23 #include <cmath>
24
25 #include "editor.h"
26 #include "ardour_ui.h"
27 #include "audio_time_axis.h"
28 #include "mixer_strip.h"
29 #include "gui_thread.h"
30
31 #include <ardour/route.h>
32
33 #include "i18n.h"
34
35 using namespace sigc;
36 using namespace ARDOUR;
37 using namespace Gtk;
38
39 void
40 Editor::handle_new_route_p (Route* route)
41 {
42         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_new_route_p), route));
43         handle_new_route (*route);
44 }
45
46 void
47 Editor::handle_new_route (Route& route)
48 {
49         TimeAxisView *tv;
50         AudioTimeAxisView *atv;
51         TreeModel::Row row = *(route_display_model->append());
52
53         if (route.hidden()) {
54                 return;
55         }
56                 
57         tv = new AudioTimeAxisView (*this, *session, route, track_canvas);
58
59         track_views.push_back (tv);
60
61         row[route_display_columns.text] = route.name();
62         row[route_display_columns.tv] = tv;
63
64         ignore_route_list_reorder = true;
65         
66         if (tv->marked_for_display()) {
67                 route_list.get_selection()->select (row);
68         }
69
70         if ((atv = dynamic_cast<AudioTimeAxisView*> (tv)) != 0) {
71                 /* added a new fresh one at the end */
72                 if (atv->route().order_key(N_("editor")) == -1) {
73                         atv->route().set_order_key (N_("editor"), route_display_model->children().size()-1);
74                 }
75         }
76
77         ignore_route_list_reorder = false;
78         
79         route.gui_changed.connect (mem_fun(*this, &Editor::handle_gui_changes));
80
81         tv->GoingAway.connect (bind (mem_fun(*this, &Editor::remove_route), tv));
82         
83         editor_mixer_button.set_sensitive(true);
84         
85 }
86
87 void
88 Editor::handle_gui_changes (const string & what, void *src)
89 {
90         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::handle_gui_changes), what, src));
91         
92         if (what == "track_height") {
93                 route_list_reordered ();
94         }
95 }
96
97
98 void
99 Editor::remove_route (TimeAxisView *tv)
100 {
101         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::remove_route), tv));
102
103         
104         TrackViewList::iterator i;
105         TreeModel::Children rows = route_display_model->children();
106         TreeModel::Children::iterator ri;
107
108         if ((i = find (track_views.begin(), track_views.end(), tv)) != track_views.end()) {
109                 track_views.erase (i);
110         }
111
112         for (ri = rows.begin(); ri != rows.end(); ++ri) {
113                 if ((*ri)[route_display_columns.tv] == tv) {
114                         route_display_model->erase (ri);
115                         break;
116                 }
117         }
118         /* since the editor mixer goes away when you remove a route, set the
119          * button to inacttive 
120          */
121         editor_mixer_button.set_active(false);
122
123         /* and disable if all tracks and/or routes are gone */
124
125         if (track_views.size() == 0) {
126                 editor_mixer_button.set_sensitive(false);
127         }
128 }
129
130 void
131 Editor::route_name_changed (TimeAxisView *tv)
132 {
133         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::route_name_changed), tv));
134         
135         TreeModel::Children rows = route_display_model->children();
136         TreeModel::Children::iterator i;
137         
138         for (i = rows.begin(); i != rows.end(); ++i) {
139                 if ((*i)[route_display_columns.tv] == tv) {
140                         (*i)[route_display_columns.text] = tv->name();
141                         break;
142                 }
143         } 
144
145 }
146
147 void
148 Editor::route_display_selection_changed ()
149 {
150
151         TimeAxisView *tv;
152         TreeModel::Children rows = route_display_model->children();
153         TreeModel::Children::iterator i;
154         Glib::RefPtr<TreeSelection> selection = route_list.get_selection();
155
156         for (i = rows.begin(); i != rows.end(); ++i) {
157                 tv = (*i)[route_display_columns.tv];
158                 if (!selection->is_selected (i)) {
159                         tv->set_marked_for_display  (false);
160                 } else {
161                         AudioTimeAxisView *atv;
162                         tv->set_marked_for_display (true);
163                         if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
164                                 if (current_mixer_strip && &(atv->route()) == &(current_mixer_strip->route())) {
165                                         // this will hide the mixer strip
166                                         set_selected_mixer_strip(*atv);
167                                 }
168                         }
169                 }
170         }
171         
172         route_list_reordered ();
173 }
174
175 void
176 Editor::unselect_strip_in_display (TimeAxisView& tv)
177 {
178         TreeModel::Children rows = route_display_model->children();
179         TreeModel::Children::iterator i;
180         Glib::RefPtr<TreeSelection> selection = route_list.get_selection();
181         
182         for (i = rows.begin(); i != rows.end(); ++i) {
183                 if ((*i)[route_display_columns.tv] == &tv) { 
184                        selection->unselect (*i);
185                 }
186         }
187 }
188
189 void
190 Editor::select_strip_in_display (TimeAxisView* tv)
191 {
192         TreeModel::Children rows = route_display_model->children();
193         TreeModel::Children::iterator i;
194         Glib::RefPtr<TreeSelection> selection = route_list.get_selection();
195         
196         for (i = rows.begin(); i != rows.end(); ++i) {
197                 if ((*i)[route_display_columns.tv] == tv) { 
198                        selection->select (*i);
199                 }
200         }
201 }
202
203 void
204 Editor::queue_route_list_reordered ()
205
206 {
207         /* the problem here is that we are called *before* the
208            list has been reordered. so just queue up
209            the actual re-drawer to happen once the re-ordering
210            is complete.
211         */
212
213         Glib::signal_idle().connect (mem_fun(*this, &Editor::route_list_reordered));
214 }
215
216 void
217 Editor::redisplay_route_list ()
218 {
219         route_list_reordered ();
220 }
221
222 gint
223 Editor::route_list_reordered ()
224 {
225         TreeModel::Children rows = route_display_model->children();
226         TreeModel::Children::iterator i;
227         long order;
228         int n;
229         
230         for (n = 0, order = 0, i = rows.begin(); i != rows.end(); ++i, ++order) {
231                 TimeAxisView *tv = (*i)[route_display_columns.tv];
232                 AudioTimeAxisView* at; 
233                 if (!ignore_route_list_reorder) {
234                         
235                                 /* this reorder is caused by user action, so reassign sort order keys
236                                    to tracks.
237                                 */
238                         
239                         if ((at = dynamic_cast<AudioTimeAxisView*> (tv)) != 0) {
240                                 at->route().set_order_key (N_("editor"), order);
241                         }
242                 }
243                 if (tv->marked_for_display()) {
244                         order += tv->show_at (order, n, &edit_controls_vbox);
245                         order += track_spacing;
246                 } else {
247                         tv->hide ();
248                 }
249                 
250                 n++;
251                 
252         }
253
254         // controls_layout.queue_resize ();
255         reset_scrolling_region ();
256         return FALSE;
257 }
258
259 void
260 Editor::hide_all_tracks (bool with_select)
261 {
262         TreeModel::Children rows = route_display_model->children();
263         TreeModel::Children::iterator i;
264
265         // GTK2FIX
266         // track_display_list.freeze ();
267         
268         for (i = rows.begin(); i != rows.end(); ++i) {
269                 
270                 TreeModel::Row row = (*i);
271                 TimeAxisView *tv = row[route_display_columns.tv];
272                 
273                 if (with_select) {
274                         route_list.get_selection()->unselect (i);
275                 } else {
276                         tv->set_marked_for_display (false);
277                         tv->hide();
278                 
279                 }
280         }
281         //route_list.thaw ();
282         reset_scrolling_region ();
283 }
284
285 void
286 Editor::route_list_column_click ()
287 {
288         if (route_list_menu == 0) {
289                 build_route_list_menu ();
290         }
291
292         route_list_menu->popup (0, 0);
293 }
294
295 void
296 Editor::build_route_list_menu ()
297 {
298         using namespace Menu_Helpers;
299         using namespace Gtk;
300
301
302         route_list_menu = new Menu;
303         
304         MenuList& items = route_list_menu->items();
305         route_list_menu->set_name ("ArdourContextMenu");
306
307         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::select_all_routes)));
308         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::unselect_all_routes)));
309         items.push_back (MenuElem (_("Show All AbstractTracks"), mem_fun(*this, &Editor::select_all_audiotracks)));
310         items.push_back (MenuElem (_("Hide All AbstractTracks"), mem_fun(*this, &Editor::unselect_all_audiotracks)));
311         items.push_back (MenuElem (_("Show All AudioBus"), mem_fun(*this, &Editor::select_all_audiobus)));
312         items.push_back (MenuElem (_("Hide All AudioBus"), mem_fun(*this, &Editor::unselect_all_audiobus)));
313
314 }
315
316 void
317 Editor::unselect_all_routes ()
318 {
319         hide_all_tracks (true);
320 }
321
322 void
323 Editor::select_all_routes ()
324
325 {
326         TreeModel::Children rows = route_display_model->children();
327         TreeModel::Children::iterator i;
328
329         for (i = rows.begin(); i != rows.end(); ++i) {
330                 route_list.get_selection()->select (i);
331         }
332 }
333
334 void
335 Editor::select_all_audiotracks () 
336 {
337         TreeModel::Children rows = route_display_model->children();
338         TreeModel::Children::iterator i;
339
340         for (i = rows.begin(); i != rows.end(); ++i) {
341         TreeModel::Row row = (*i);
342                 TimeAxisView* tv = row[route_display_columns.tv];
343                 AudioTimeAxisView* atv;
344
345                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
346                         if (atv->is_audio_track()) {
347                                 route_list.get_selection()->select (i);
348
349                         }
350                 }
351         }
352
353 }
354
355 void
356 Editor::unselect_all_audiotracks () 
357 {
358         TreeModel::Children rows = route_display_model->children();
359         TreeModel::Children::iterator i;
360
361         for (i = rows.begin(); i != rows.end(); ++i) {
362                 TreeModel::Row row = (*i);
363                 TimeAxisView *tv = row[route_display_columns.tv];
364                 AudioTimeAxisView* atv;
365
366                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
367                         if (atv->is_audio_track()) {
368                                 route_list.get_selection()->unselect (i);
369
370                         }
371                 }
372         }
373
374 }
375
376 void
377 Editor::select_all_audiobus () 
378 {
379         TreeModel::Children rows = route_display_model->children();
380         TreeModel::Children::iterator i;
381
382         for (i = rows.begin(); i != rows.end(); ++i) {
383                 TreeModel::Row row = (*i);
384                 TimeAxisView* tv = row[route_display_columns.tv];
385                 AudioTimeAxisView* atv;
386
387                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
388                         if (!atv->is_audio_track()) {
389                                 route_list.get_selection()->select (i);
390
391                         }
392                 }
393         }
394
395 }
396
397 void
398 Editor::unselect_all_audiobus () 
399 {
400         TreeModel::Children rows = route_display_model->children();
401         TreeModel::Children::iterator i;
402
403         for (i = rows.begin(); i != rows.end(); ++i) {
404                 TreeModel::Row row = (*i);
405                 TimeAxisView* tv = row[route_display_columns.tv];
406                 AudioTimeAxisView* atv;
407
408                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
409                         if (!atv->is_audio_track()) {
410                                 route_list.get_selection()->unselect (i);
411
412                         }
413                 }
414         }
415
416 }
417
418 gint
419 Editor::route_list_compare_func (TreeModel::iterator a, TreeModel::iterator b)
420 {
421         TimeAxisView *tv1;
422         TimeAxisView *tv2;
423         AudioTimeAxisView *atv1;
424         AudioTimeAxisView *atv2;
425         Route* ra;
426         Route* rb;
427
428         tv1 = (*a)[route_display_columns.tv];
429         tv2 = (*b)[route_display_columns.tv];
430
431         if ((atv1 = dynamic_cast<AudioTimeAxisView*>(tv1)) == 0 ||
432             (atv2 = dynamic_cast<AudioTimeAxisView*>(tv2)) == 0) {
433                 return FALSE;
434         }
435
436         ra = &atv1->route();
437         rb = &atv2->route();
438
439         /* use of ">" forces the correct sort order */
440
441         return ra->order_key ("editor") > rb->order_key ("editor");
442 }
443