convert property_foo().set_value(bar) to property_foo() = bar
[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 (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  (true);
160                 } else {
161                         AudioTimeAxisView *atv;
162                         tv->set_marked_for_display (false);
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         edit_controls_scroller.queue_resize ();
254         reset_scrolling_region ();
255         return FALSE;
256 }
257
258 void
259 Editor::hide_all_tracks (bool with_select)
260 {
261         TreeModel::Children rows = route_display_model->children();
262         TreeModel::Children::iterator i;
263
264         // GTK2FIX
265         // track_display_list.freeze ();
266         
267         for (i = rows.begin(); i != rows.end(); ++i) {
268                 
269                 TreeModel::Row row = (*i);
270                 TimeAxisView *tv = row[route_display_columns.tv];
271                 
272                 if (with_select) {
273                         route_list.get_selection()->unselect (i);
274                 } else {
275                         tv->set_marked_for_display (false);
276                         tv->hide();
277                 
278                 }
279         }
280         //route_list.thaw ();
281         reset_scrolling_region ();
282 }
283
284 void
285 Editor::route_list_column_click ()
286 {
287         if (route_list_menu == 0) {
288                 build_route_list_menu ();
289         }
290
291         route_list_menu->popup (0, 0);
292 }
293
294 void
295 Editor::build_route_list_menu ()
296 {
297         using namespace Menu_Helpers;
298         using namespace Gtk;
299
300
301         route_list_menu = new Menu;
302         
303         MenuList& items = route_list_menu->items();
304         route_list_menu->set_name ("ArdourContextMenu");
305
306         items.push_back (MenuElem (_("Show All"), mem_fun(*this, &Editor::select_all_routes)));
307         items.push_back (MenuElem (_("Hide All"), mem_fun(*this, &Editor::unselect_all_routes)));
308         items.push_back (MenuElem (_("Show All AbstractTracks"), mem_fun(*this, &Editor::select_all_audiotracks)));
309         items.push_back (MenuElem (_("Hide All AbstractTracks"), mem_fun(*this, &Editor::unselect_all_audiotracks)));
310         items.push_back (MenuElem (_("Show All AudioBus"), mem_fun(*this, &Editor::select_all_audiobus)));
311         items.push_back (MenuElem (_("Hide All AudioBus"), mem_fun(*this, &Editor::unselect_all_audiobus)));
312
313 }
314
315 void
316 Editor::unselect_all_routes ()
317 {
318         hide_all_tracks (true);
319 }
320
321 void
322 Editor::select_all_routes ()
323
324 {
325         TreeModel::Children rows = route_display_model->children();
326         TreeModel::Children::iterator i;
327
328         for (i = rows.begin(); i != rows.end(); ++i) {
329                 route_list.get_selection()->select (i);
330         }
331 }
332
333 void
334 Editor::select_all_audiotracks () 
335 {
336         TreeModel::Children rows = route_display_model->children();
337         TreeModel::Children::iterator i;
338
339         for (i = rows.begin(); i != rows.end(); ++i) {
340         TreeModel::Row row = (*i);
341                 TimeAxisView* tv = row[route_display_columns.tv];
342                 AudioTimeAxisView* atv;
343
344                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
345                         if (atv->is_audio_track()) {
346                                 route_list.get_selection()->select (i);
347
348                         }
349                 }
350         }
351
352 }
353
354 void
355 Editor::unselect_all_audiotracks () 
356 {
357         TreeModel::Children rows = route_display_model->children();
358         TreeModel::Children::iterator i;
359
360         for (i = rows.begin(); i != rows.end(); ++i) {
361                 TreeModel::Row row = (*i);
362                 TimeAxisView *tv = row[route_display_columns.tv];
363                 AudioTimeAxisView* atv;
364
365                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
366                         if (atv->is_audio_track()) {
367                                 route_list.get_selection()->unselect (i);
368
369                         }
370                 }
371         }
372
373 }
374
375 void
376 Editor::select_all_audiobus () 
377 {
378         TreeModel::Children rows = route_display_model->children();
379         TreeModel::Children::iterator i;
380
381         for (i = rows.begin(); i != rows.end(); ++i) {
382                 TreeModel::Row row = (*i);
383                 TimeAxisView* tv = row[route_display_columns.tv];
384                 AudioTimeAxisView* atv;
385
386                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
387                         if (!atv->is_audio_track()) {
388                                 route_list.get_selection()->select (i);
389
390                         }
391                 }
392         }
393
394 }
395
396 void
397 Editor::unselect_all_audiobus () 
398 {
399         TreeModel::Children rows = route_display_model->children();
400         TreeModel::Children::iterator i;
401
402         for (i = rows.begin(); i != rows.end(); ++i) {
403                 TreeModel::Row row = (*i);
404                 TimeAxisView* tv = row[route_display_columns.tv];
405                 AudioTimeAxisView* atv;
406
407                 if ((atv = dynamic_cast<AudioTimeAxisView*>(tv)) != 0) {
408                         if (!atv->is_audio_track()) {
409                                 route_list.get_selection()->unselect (i);
410
411                         }
412                 }
413         }
414
415 }
416
417 gint
418 Editor::route_list_compare_func (TreeModel::iterator a, TreeModel::iterator b)
419 {
420         TimeAxisView *tv1;
421         TimeAxisView *tv2;
422         AudioTimeAxisView *atv1;
423         AudioTimeAxisView *atv2;
424         Route* ra;
425         Route* rb;
426
427         tv1 = (*a)[route_display_columns.tv];
428         tv2 = (*b)[route_display_columns.tv];
429
430         if ((atv1 = dynamic_cast<AudioTimeAxisView*>(tv1)) == 0 ||
431             (atv2 = dynamic_cast<AudioTimeAxisView*>(tv2)) == 0) {
432                 return FALSE;
433         }
434
435         ra = &atv1->route();
436         rb = &atv2->route();
437
438         /* use of ">" forces the correct sort order */
439
440         return ra->order_key ("editor") > rb->order_key ("editor");
441 }
442