merge from trunk
[ardour.git] / gtk2_ardour / route_time_axis.cc
1 /*
2     Copyright (C) 2006 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 #include <cstdlib>
20 #include <cmath>
21 #include <cassert>
22
23 #include <algorithm>
24 #include <string>
25 #include <vector>
26
27 #include <sigc++/bind.h>
28
29 #include <pbd/error.h>
30 #include <pbd/stl_delete.h>
31 #include <pbd/whitespace.h>
32 #include <pbd/memento_command.h>
33
34 #include <gtkmm/menu.h>
35 #include <gtkmm/menuitem.h>
36 #include <gtkmm2ext/gtk_ui.h>
37 #include <gtkmm2ext/selector.h>
38 #include <gtkmm2ext/stop_signal.h>
39 #include <gtkmm2ext/bindable_button.h>
40 #include <gtkmm2ext/utils.h>
41
42 #include <ardour/playlist.h>
43 #include <ardour/diskstream.h>
44 #include <ardour/insert.h>
45 #include <ardour/ladspa_plugin.h>
46 #include <ardour/location.h>
47 #include <ardour/panner.h>
48 #include <ardour/playlist.h>
49 #include <ardour/session.h>
50 #include <ardour/session_playlist.h>
51 #include <ardour/utils.h>
52
53 #include "ardour_ui.h"
54 #include "route_time_axis.h"
55 #include "automation_time_axis.h"
56 #include "redirect_automation_time_axis.h"
57 #include "redirect_automation_line.h"
58 #include "canvas_impl.h"
59 #include "crossfade_view.h"
60 #include "enums.h"
61 #include "gui_thread.h"
62 #include "keyboard.h"
63 #include "playlist_selector.h"
64 #include "point_selection.h"
65 #include "prompter.h"
66 #include "public_editor.h"
67 #include "region_view.h"
68 #include "rgb_macros.h"
69 #include "selection.h"
70 #include "simplerect.h"
71 #include "streamview.h"
72 #include "utils.h"
73
74 #include <ardour/track.h>
75
76 #include "i18n.h"
77
78 using namespace ARDOUR;
79 using namespace PBD;
80 using namespace Gtk;
81 using namespace Editing;
82
83
84 RouteTimeAxisView::RouteTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
85         : AxisView(sess),
86           RouteUI(rt, sess, _("m"), _("s"), _("r")), // mute, solo, and record
87           TimeAxisView(sess,ed,(TimeAxisView*) 0, canvas),
88           parent_canvas (canvas),
89           button_table (3, 3),
90           edit_group_button (_("g")), // group
91           playlist_button (_("p")), 
92           size_button (_("h")), // height
93           automation_button (_("a")),
94           visual_button (_("v"))
95
96 {
97         _has_state = true;
98         playlist_menu = 0;
99         playlist_action_menu = 0;
100         automation_action_menu = 0;
101         _view = 0;
102         timestretch_rect = 0;
103         no_redraw = false;
104
105         ignore_toggle = false;
106
107         mute_button->set_active (false);
108         solo_button->set_active (false);
109         
110         mute_button->set_name ("TrackMuteButton");
111         solo_button->set_name ("SoloButton");
112         edit_group_button.set_name ("TrackGroupButton");
113         playlist_button.set_name ("TrackPlaylistButton");
114         automation_button.set_name ("TrackAutomationButton");
115         size_button.set_name ("TrackSizeButton");
116         visual_button.set_name ("TrackVisualButton");
117         hide_button.set_name ("TrackRemoveButton");
118
119         hide_button.add (*(manage (new Image (get_xpm("small_x.xpm")))));
120
121         solo_button->signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
122         mute_button->signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
123         playlist_button.signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
124         automation_button.signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
125         size_button.signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
126         visual_button.signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
127         hide_button.signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
128
129         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
130         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release), false);
131         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
132         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release), false);
133         edit_group_button.signal_button_release_event().connect (mem_fun(*this, &RouteTimeAxisView::edit_click), false);
134         playlist_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::playlist_click));
135         automation_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::automation_click));
136         size_button.signal_button_release_event().connect (mem_fun(*this, &RouteTimeAxisView::size_click), false);
137         visual_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::visual_click));
138         hide_button.signal_clicked().connect (mem_fun(*this, &RouteTimeAxisView::hide_click));
139
140         if (is_track()) {
141                 rec_enable_button->set_active (false);
142                 rec_enable_button->set_name ("TrackRecordEnableButton");
143                 rec_enable_button->signal_button_press_event().connect (mem_fun (*this, &RouteTimeAxisView::select_me), false);
144                 rec_enable_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::rec_enable_press));
145                 controls_table.attach (*rec_enable_button, 5, 6, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
146                 ARDOUR_UI::instance()->tooltips().set_tip(*rec_enable_button, _("Record"));
147         }
148
149         controls_table.attach (*mute_button, 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
150         controls_table.attach (*solo_button, 7, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::FILL|Gtk::EXPAND, 0, 0);
151
152         controls_table.attach (edit_group_button, 6, 7, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
153
154         ARDOUR_UI::instance()->tooltips().set_tip(*solo_button,_("Solo"));
155         ARDOUR_UI::instance()->tooltips().set_tip(*mute_button,_("Mute"));
156         ARDOUR_UI::instance()->tooltips().set_tip(edit_group_button,_("Edit Group"));
157         ARDOUR_UI::instance()->tooltips().set_tip(size_button,_("Display Height"));
158         ARDOUR_UI::instance()->tooltips().set_tip(playlist_button,_("Playlist"));
159         ARDOUR_UI::instance()->tooltips().set_tip(automation_button, _("Automation"));
160         ARDOUR_UI::instance()->tooltips().set_tip(visual_button, _("Visual options"));
161         ARDOUR_UI::instance()->tooltips().set_tip(hide_button, _("Hide this track"));
162         
163         label_view ();
164
165         controls_table.attach (hide_button, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
166         controls_table.attach (visual_button, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
167         controls_table.attach (size_button, 2, 3, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
168         controls_table.attach (automation_button, 3, 4, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
169
170         if (is_track() && track()->mode() == ARDOUR::Normal) {
171                 controls_table.attach (playlist_button, 5, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
172         }
173
174         /* remove focus from the buttons */
175         
176         automation_button.unset_flags (Gtk::CAN_FOCUS);
177         solo_button->unset_flags (Gtk::CAN_FOCUS);
178         mute_button->unset_flags (Gtk::CAN_FOCUS);
179         edit_group_button.unset_flags (Gtk::CAN_FOCUS);
180         size_button.unset_flags (Gtk::CAN_FOCUS);
181         playlist_button.unset_flags (Gtk::CAN_FOCUS);
182         hide_button.unset_flags (Gtk::CAN_FOCUS);
183         visual_button.unset_flags (Gtk::CAN_FOCUS);
184
185         /* map current state of the route */
186
187         update_diskstream_display ();
188         solo_changed(0);
189         mute_changed(0);
190         //redirects_changed (0);
191         //reset_redirect_automation_curves ();
192         y_position = -1;
193
194         _route->mute_changed.connect (mem_fun(*this, &RouteUI::mute_changed));
195         _route->solo_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
196         _route->redirects_changed.connect (mem_fun(*this, &RouteTimeAxisView::redirects_changed));
197         _route->name_changed.connect (mem_fun(*this, &RouteTimeAxisView::route_name_changed));
198         _route->solo_safe_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
199
200         if (is_track()) {
201
202                 track()->FreezeChange.connect (mem_fun(*this, &RouteTimeAxisView::map_frozen));
203                 track()->DiskstreamChanged.connect (mem_fun(*this, &RouteTimeAxisView::diskstream_changed));
204                 get_diskstream()->SpeedChanged.connect (mem_fun(*this, &RouteTimeAxisView::speed_changed));
205
206                 /* ask for notifications of any new RegionViews */
207                 // FIXME: _view is NULL, but it would be nice to attach this here :/
208                 //_view->RegionViewAdded.connect (mem_fun(*this, &RouteTimeAxisView::region_view_added));
209                 //_view->attach ();
210
211                 /* pick up the correct freeze state */
212                 map_frozen ();
213
214         }
215
216         editor.ZoomChanged.connect (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
217         ColorChanged.connect (mem_fun (*this, &RouteTimeAxisView::color_handler));
218 }
219
220 RouteTimeAxisView::~RouteTimeAxisView ()
221 {
222         GoingAway (); /* EMIT_SIGNAL */
223
224         vector_delete (&redirect_automation_curves);
225
226         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
227                 delete *i;
228         }
229
230         if (playlist_menu) {
231                 delete playlist_menu;
232                 playlist_menu = 0;
233         }
234   
235         if (playlist_action_menu) {
236                 delete playlist_action_menu;
237                 playlist_action_menu = 0;
238         }
239
240         if (_view) {
241                 delete _view;
242                 _view = 0;
243         }
244 }
245
246 void
247 RouteTimeAxisView::set_playlist (Playlist *newplaylist)
248 {
249         Playlist *pl = playlist();
250         assert(pl);
251
252         modified_connection.disconnect ();
253         state_changed_connection.disconnect ();
254         
255         state_changed_connection = pl->StateChanged.connect (mem_fun(*this, &RouteTimeAxisView::playlist_state_changed));
256         modified_connection = pl->Modified.connect (mem_fun(*this, &RouteTimeAxisView::playlist_modified));
257 }
258
259 void
260 RouteTimeAxisView::playlist_modified ()
261 {
262 }
263
264 gint
265 RouteTimeAxisView::edit_click (GdkEventButton *ev)
266 {
267         if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
268                 _route->set_edit_group (0, this);
269                 return FALSE;
270         } 
271
272         using namespace Menu_Helpers;
273
274         MenuList& items = edit_group_menu.items ();
275         RadioMenuItem::Group group;
276
277         items.clear ();
278         items.push_back (RadioMenuElem (group, _("No group"), 
279                                         bind (mem_fun(*this, &RouteTimeAxisView::set_edit_group_from_menu), (RouteGroup *) 0)));
280         
281         if (_route->edit_group() == 0) {
282                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
283         }
284         
285         _session.foreach_edit_group (bind (mem_fun (*this, &RouteTimeAxisView::add_edit_group_menu_item), &group));
286         edit_group_menu.popup (ev->button, ev->time);
287
288         return FALSE;
289 }
290
291 void
292 RouteTimeAxisView::add_edit_group_menu_item (RouteGroup *eg, RadioMenuItem::Group* group)
293 {
294         using namespace Menu_Helpers;
295
296         MenuList &items = edit_group_menu.items();
297
298         cerr << "adding edit group " << eg->name() << endl;
299
300         items.push_back (RadioMenuElem (*group, eg->name(), bind (mem_fun(*this, &RouteTimeAxisView::set_edit_group_from_menu), eg)));
301         if (_route->edit_group() == eg) {
302                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
303         }
304 }
305
306 void
307 RouteTimeAxisView::set_edit_group_from_menu (RouteGroup *eg)
308
309 {
310         _route->set_edit_group (eg, this);
311 }
312
313 void
314 RouteTimeAxisView::playlist_state_changed (Change ignored)
315 {
316         // ENSURE_GUI_THREAD (bind (mem_fun(*this, &RouteTimeAxisView::playlist_state_changed), ignored));
317         // why are we here ?
318 }
319
320 void
321 RouteTimeAxisView::playlist_changed ()
322
323 {
324         label_view ();
325
326         if (is_track()) {
327                 set_playlist (dynamic_cast<Playlist*>(get_diskstream()->playlist()));
328         }
329 }
330
331 void
332 RouteTimeAxisView::label_view ()
333 {
334         string x = _route->name();
335
336         if (x != name_entry.get_text()) {
337                 name_entry.set_text (x);
338         }
339
340         ARDOUR_UI::instance()->tooltips().set_tip (name_entry, x);
341 }
342
343 void
344 RouteTimeAxisView::route_name_changed (void *src)
345 {
346         editor.route_name_changed (this);
347         label_view ();
348 }
349
350 void
351 RouteTimeAxisView::take_name_changed (void *src)
352
353 {
354         if (src != this) {
355                 label_view ();
356         }
357 }
358
359 void
360 RouteTimeAxisView::playlist_click ()
361 {
362         // always build a new action menu
363         
364         if (playlist_action_menu == 0) {
365                 playlist_action_menu = new Menu;
366                 playlist_action_menu->set_name ("ArdourContextMenu");
367         }
368         
369         build_playlist_menu(playlist_action_menu);
370
371         playlist_action_menu->popup (1, 0);
372 }
373
374 void
375 RouteTimeAxisView::automation_click ()
376 {
377         if (automation_action_menu == 0) {
378                 /* this seems odd, but the automation action
379                    menu is built as part of the display menu.
380                 */
381                 build_display_menu ();
382         }
383         automation_action_menu->popup (1, 0);
384 }
385
386 void
387 RouteTimeAxisView::build_automation_action_menu ()
388 {
389         using namespace Menu_Helpers;
390
391         automation_action_menu = manage (new Menu);
392         MenuList& automation_items = automation_action_menu->items();
393         automation_action_menu->set_name ("ArdourContextMenu");
394         
395         automation_items.push_back (MenuElem (_("Show all automation"),
396                                               mem_fun(*this, &RouteTimeAxisView::show_all_automation)));
397
398         automation_items.push_back (MenuElem (_("Show existing automation"),
399                                               mem_fun(*this, &RouteTimeAxisView::show_existing_automation)));
400
401         automation_items.push_back (MenuElem (_("Hide all automation"),
402                                               mem_fun(*this, &RouteTimeAxisView::hide_all_automation)));
403
404         automation_items.push_back (MenuElem (_("Plugins"), subplugin_menu));
405 }
406
407 void
408 RouteTimeAxisView::build_display_menu ()
409 {
410         using namespace Menu_Helpers;
411
412         /* get the size menu ready */
413
414         build_size_menu ();
415
416         /* prepare it */
417
418         TimeAxisView::build_display_menu ();
419
420         /* now fill it with our stuff */
421
422         MenuList& items = display_menu->items();
423         display_menu->set_name ("ArdourContextMenu");
424         
425         items.push_back (MenuElem (_("Height"), *size_menu));
426         items.push_back (MenuElem (_("Color"), mem_fun(*this, &RouteTimeAxisView::select_track_color)));
427
428         items.push_back (SeparatorElem());
429
430         build_remote_control_menu ();
431         items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
432
433         build_automation_action_menu ();
434         items.push_back (MenuElem (_("Automation"), *automation_action_menu));
435
436         // Hook for derived classes to add type specific stuff
437         items.push_back (SeparatorElem());
438         append_extra_display_menu_items ();
439         items.push_back (SeparatorElem());
440         
441         if (is_track()) {
442
443                 Menu* alignment_menu = manage (new Menu);
444                 MenuList& alignment_items = alignment_menu->items();
445                 alignment_menu->set_name ("ArdourContextMenu");
446
447                 RadioMenuItem::Group align_group;
448                 
449                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"),
450                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), ExistingMaterial)));
451                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
452                 if (get_diskstream()->alignment_style() == ExistingMaterial)
453                         align_existing_item->set_active();
454                 
455                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"),
456                         bind (mem_fun(*this, &RouteTimeAxisView::set_align_style), CaptureTime)));
457                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
458                 if (get_diskstream()->alignment_style() == CaptureTime)
459                         align_capture_item->set_active();
460                 
461                 items.push_back (MenuElem (_("Alignment"), *alignment_menu));
462
463                 get_diskstream()->AlignmentStyleChanged.connect (
464                         mem_fun(*this, &RouteTimeAxisView::align_style_changed));
465         }
466
467         items.push_back (SeparatorElem());
468         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
469         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
470         route_active_menu_item->set_active (_route->active());
471
472         items.push_back (SeparatorElem());
473         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
474 }
475
476
477 void
478 RouteTimeAxisView::show_timestretch (jack_nframes_t start, jack_nframes_t end)
479 {
480         double x1;
481         double x2;
482         double y2;
483         
484         TimeAxisView::show_timestretch (start, end);
485
486         hide_timestretch ();
487
488 #if 0   
489         if (ts.empty()) {
490                 return;
491         }
492
493
494         /* check that the time selection was made in our route, or our edit group.
495            remember that edit_group() == 0 implies the route is *not* in a edit group.
496         */
497
498         if (!(ts.track == this || (ts.group != 0 && ts.group == _route->edit_group()))) {
499                 /* this doesn't apply to us */
500                 return;
501         }
502
503         /* ignore it if our edit group is not active */
504         
505         if ((ts.track != this) && _route->edit_group() && !_route->edit_group()->is_active()) {
506                 return;
507         }
508 #endif
509
510         if (timestretch_rect == 0) {
511                 timestretch_rect = new SimpleRect (*canvas_display);
512                 timestretch_rect->property_x1() =  0.0;
513                 timestretch_rect->property_y1() =  0.0;
514                 timestretch_rect->property_x2() =  0.0;
515                 timestretch_rect->property_y2() =  0.0;
516                 timestretch_rect->property_fill_color_rgba() =  color_map[cTimeStretchFill];
517                 timestretch_rect->property_outline_color_rgba() = color_map[cTimeStretchOutline];
518         }
519
520         timestretch_rect->show ();
521         timestretch_rect->raise_to_top ();
522
523         x1 = start / editor.get_current_zoom();
524         x2 = (end - 1) / editor.get_current_zoom();
525         y2 = height - 2;
526         
527         timestretch_rect->property_x1() = x1;
528         timestretch_rect->property_y1() = 1.0;
529         timestretch_rect->property_x2() = x2;
530         timestretch_rect->property_y2() = y2;
531 }
532
533 void
534 RouteTimeAxisView::hide_timestretch ()
535 {
536         TimeAxisView::hide_timestretch ();
537
538         if (timestretch_rect) {
539                 timestretch_rect->hide ();
540         }
541 }
542
543 void
544 RouteTimeAxisView::show_selection (TimeSelection& ts)
545 {
546
547 #if 0
548         /* ignore it if our edit group is not active or if the selection was started
549            in some other track or edit group (remember that edit_group() == 0 means
550            that the track is not in an edit group).
551         */
552
553         if (((ts.track != this && !is_child (ts.track)) && _route->edit_group() && !_route->edit_group()->is_active()) ||
554             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route->edit_group())))) {
555                 hide_selection ();
556                 return;
557         }
558 #endif
559
560         TimeAxisView::show_selection (ts);
561 }
562
563 void
564 RouteTimeAxisView::set_height (TrackHeight h)
565 {
566         bool height_changed = (height == 0) || (h != height_style);
567
568         TimeAxisView::set_height (h);
569
570         ensure_xml_node ();
571
572         _view->set_height ((double) height);
573
574         switch (height_style) {
575         case Largest:
576                 xml_node->add_property ("track_height", "largest");
577                 break;
578
579         case Large:
580                 xml_node->add_property ("track_height", "large");
581                 break;
582
583         case Larger:
584                 xml_node->add_property ("track_height", "larger");
585                 break;
586
587         case Normal:
588                 xml_node->add_property ("track_height", "normal");
589                 break;
590
591         case Smaller:
592                 xml_node->add_property ("track_height", "smaller");
593                 break;
594
595         case Small:
596                 xml_node->add_property ("track_height", "small");
597                 break;
598         }
599
600         switch (height_style) {
601         case Largest:
602         case Large:
603         case Larger:
604         case Normal:
605                 show_name_entry ();
606                 hide_name_label ();
607
608                 mute_button->show_all();
609                 solo_button->show_all();
610                 if (rec_enable_button)
611                         rec_enable_button->show_all();
612
613                 edit_group_button.show_all();
614                 hide_button.show_all();
615                 visual_button.show_all();
616                 size_button.show_all();
617                 automation_button.show_all();
618                 
619                 if (is_track() && track()->mode() == ARDOUR::Normal) {
620                         playlist_button.show_all();
621                 }
622                 break;
623
624         case Smaller:
625                 show_name_entry ();
626                 hide_name_label ();
627
628                 mute_button->show_all();
629                 solo_button->show_all();
630                 if (rec_enable_button)
631                         rec_enable_button->show_all();
632
633                 edit_group_button.hide ();
634                 hide_button.hide ();
635                 visual_button.hide ();
636                 size_button.hide ();
637                 automation_button.hide ();
638                 
639                 if (is_track() && track()->mode() == ARDOUR::Normal) {
640                         playlist_button.hide ();
641                 }
642                 break;
643
644         case Small:
645                 hide_name_entry ();
646                 show_name_label ();
647
648                 mute_button->hide();
649                 solo_button->hide();
650                 if (rec_enable_button)
651                         rec_enable_button->hide();
652
653                 edit_group_button.hide ();
654                 hide_button.hide ();
655                 visual_button.hide ();
656                 size_button.hide ();
657                 automation_button.hide ();
658                 playlist_button.hide ();
659                 name_label.set_text (_route->name());
660                 break;
661         }
662
663         if (height_changed) {
664                 /* only emit the signal if the height really changed */
665                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
666         }
667 }
668
669 void
670 RouteTimeAxisView::select_track_color ()
671 {
672         if (RouteUI::choose_color ()) {
673
674                 if (_view) {
675                         _view->apply_color (_color, StreamView::RegionColor);
676                 }
677         }
678 }
679
680 void
681 RouteTimeAxisView::reset_samples_per_unit ()
682 {
683         set_samples_per_unit (editor.get_current_zoom());
684 }
685
686 void
687 RouteTimeAxisView::set_samples_per_unit (double spu)
688 {
689         double speed = 1.0;
690
691         if (get_diskstream() != 0) {
692                 speed = get_diskstream()->speed();
693         }
694         
695         if (_view) {
696                 _view->set_samples_per_unit (spu * speed);
697         }
698
699         TimeAxisView::set_samples_per_unit (spu * speed);
700 }
701
702 void
703 RouteTimeAxisView::align_style_changed ()
704 {
705         switch (get_diskstream()->alignment_style()) {
706         case ExistingMaterial:
707                 if (!align_existing_item->get_active()) {
708                         align_existing_item->set_active();
709                 }
710                 break;
711         case CaptureTime:
712                 if (!align_capture_item->get_active()) {
713                         align_capture_item->set_active();
714                 }
715                 break;
716         }
717 }
718
719 void
720 RouteTimeAxisView::set_align_style (AlignStyle style)
721 {
722         get_diskstream()->set_align_style (style);
723 }
724
725 void
726 RouteTimeAxisView::rename_current_playlist ()
727 {
728         ArdourPrompter prompter (true);
729         string name;
730
731         Diskstream *const ds = get_diskstream();
732         if (!ds || ds->destructive())
733                 return;
734
735         Playlist *const pl = ds->playlist();
736         if (!pl)
737                 return;
738
739         prompter.set_prompt (_("Name for playlist"));
740         prompter.set_initial_text (pl->name());
741         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
742         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
743
744         switch (prompter.run ()) {
745         case Gtk::RESPONSE_ACCEPT:
746                 prompter.get_result (name);
747                 if (name.length()) {
748                         pl->set_name (name);
749                 }
750                 break;
751
752         default:
753                 break;
754         }
755 }
756
757 void
758 RouteTimeAxisView::use_copy_playlist (bool prompt)
759 {
760         string name;
761         
762         Diskstream *const ds = get_diskstream();
763         if (!ds || ds->destructive())
764                 return;
765
766         Playlist *const pl = ds->playlist();
767         if (!pl)
768                 return;
769
770         name = Playlist::bump_name (pl->name(), _session);
771
772         if (prompt) {
773
774                 ArdourPrompter prompter (true);
775                 
776                 prompter.set_prompt (_("Name for Playlist"));
777                 prompter.set_initial_text (name);
778                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
779                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
780                 prompter.show_all ();
781                 
782                 switch (prompter.run ()) {
783                 case Gtk::RESPONSE_ACCEPT:
784                         prompter.get_result (name);
785                         break;
786                         
787                 default:
788                         return;
789                 }
790         }
791
792         if (name.length()) {
793                 ds->use_copy_playlist ();
794                 pl->set_name (name);
795         }
796 }
797
798 void
799 RouteTimeAxisView::use_new_playlist (bool prompt)
800 {
801         string name;
802         
803         Diskstream *const ds = get_diskstream();
804         if (!ds || ds->destructive())
805                 return;
806
807         Playlist *const pl = ds->playlist();
808         if (!pl)
809                 return;
810
811         name = Playlist::bump_name (pl->name(), _session);
812
813         if (prompt) {
814                 
815                 ArdourPrompter prompter (true);
816                 
817                 prompter.set_prompt (_("Name for Playlist"));
818                 prompter.set_initial_text (name);
819                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
820                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
821                 
822                 switch (prompter.run ()) {
823                 case Gtk::RESPONSE_ACCEPT:
824                         prompter.get_result (name);
825                         break;
826                         
827                 default:
828                         return;
829                 }
830         }
831
832         if (name.length()) {
833                 ds->use_new_playlist ();
834                 pl->set_name (name);
835         }
836 }
837
838 void
839 RouteTimeAxisView::clear_playlist ()
840 {
841         Diskstream *const ds = get_diskstream();
842         if (!ds || ds->destructive())
843                 return;
844
845         Playlist *const pl = ds->playlist();
846         if (!pl)
847                 return;
848
849         editor.clear_playlist (*pl);
850 }
851
852 void
853 RouteTimeAxisView::speed_changed ()
854 {
855         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::reset_samples_per_unit));
856 }
857
858 void
859 RouteTimeAxisView::diskstream_changed ()
860 {
861         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &RouteTimeAxisView::update_diskstream_display));
862 }       
863
864 void
865 RouteTimeAxisView::update_diskstream_display ()
866 {
867         if (!get_diskstream()) // bus
868                 return;
869
870         set_playlist (get_diskstream()->playlist());
871         map_frozen ();
872 }       
873
874 void
875 RouteTimeAxisView::selection_click (GdkEventButton* ev)
876 {
877         PublicEditor::TrackViewList* tracks = editor.get_valid_views (this, _route->edit_group());
878
879         switch (Keyboard::selection_type (ev->state)) {
880         case Selection::Toggle:
881                 /* XXX this is not right */
882                 editor.get_selection().add (*tracks);
883                 break;
884                 
885         case Selection::Set:
886                 editor.get_selection().set (*tracks);
887                 break;
888
889         case Selection::Extend:
890                 /* not defined yet */
891                 break;
892         }
893
894         delete tracks;
895 }
896
897 void
898 RouteTimeAxisView::set_selected_points (PointSelection& points)
899 {
900         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
901                 (*i)->set_selected_points (points);
902         }
903 }
904
905 void
906 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
907 {
908         _view->set_selected_regionviews (regions);
909 }
910
911 void
912 RouteTimeAxisView::get_selectables (jack_nframes_t start, jack_nframes_t end, double top, double bot, list<Selectable*>& results)
913 {
914         double speed = 1.0;
915         
916         if (get_diskstream() != 0) {
917                 speed = get_diskstream()->speed();
918         }
919         
920         jack_nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
921         jack_nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
922
923         if (_view && ((top < 0.0 && bot < 0.0)) || touched (top, bot)) {
924                 _view->get_selectables (start_adjusted, end_adjusted, results);
925         }
926
927         /* pick up visible automation tracks */
928         
929         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
930                 if (!(*i)->hidden()) {
931                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
932                 }
933         }
934 }
935
936 void
937 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
938 {
939         if (_view) {
940                 _view->get_inverted_selectables (sel, results);
941         }
942
943         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
944                 if (!(*i)->hidden()) {
945                         (*i)->get_inverted_selectables (sel, results);
946                 }
947         }
948
949         return;
950 }
951
952 RouteGroup*
953 RouteTimeAxisView::edit_group() const
954 {
955         return _route->edit_group();
956 }
957
958 string
959 RouteTimeAxisView::name() const
960 {
961         return _route->name();
962 }
963
964 Playlist *
965 RouteTimeAxisView::playlist () const 
966 {
967         Diskstream *ds;
968
969         if ((ds = get_diskstream()) != 0) {
970                 return ds->playlist(); 
971         } else {
972                 return 0; 
973         }
974 }
975
976 void
977 RouteTimeAxisView::name_entry_changed ()
978 {
979         string x;
980
981         x = name_entry.get_text ();
982         
983         if (x == _route->name()) {
984                 return;
985         }
986
987         if (x.length() == 0) {
988                 name_entry.set_text (_route->name());
989                 return;
990         }
991
992         strip_whitespace_edges(x);
993
994         if (_session.route_name_unique (x)) {
995                 _route->set_name (x, this);
996         } else {
997                 ARDOUR_UI::instance()->popup_error (_("a track already exists with that name"));
998                 name_entry.set_text (_route->name());
999         }
1000 }
1001
1002 void
1003 RouteTimeAxisView::visual_click ()
1004 {
1005         popup_display_menu (0);
1006 }
1007
1008 void
1009 RouteTimeAxisView::hide_click ()
1010 {
1011         editor.hide_track_in_display (*this);
1012 }
1013
1014 Region*
1015 RouteTimeAxisView::find_next_region (jack_nframes_t pos, RegionPoint point, int32_t dir)
1016 {
1017         Diskstream *stream;
1018         Playlist *playlist;
1019
1020         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1021                 return playlist->find_next_region (pos, point, dir);
1022         }
1023
1024         return 0;
1025 }
1026
1027 bool
1028 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1029 {
1030         Playlist* what_we_got;
1031         Diskstream* ds = get_diskstream();
1032         Playlist* playlist;
1033         bool ret = false;
1034
1035         if (ds == 0) {
1036                 /* route is a bus, not a track */
1037                 return false;
1038         }
1039
1040         playlist = ds->playlist();
1041
1042
1043         TimeSelection time (selection.time);
1044         float speed = ds->speed();
1045         if (speed != 1.0f) {
1046                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1047                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1048                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1049                 }
1050         }
1051         
1052         XMLNode &before = playlist->get_state();
1053         switch (op) {
1054         case Cut:
1055                 if ((what_we_got = playlist->cut (time)) != 0) {
1056                         editor.get_cut_buffer().add (what_we_got);
1057                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1058                         ret = true;
1059                 }
1060                 break;
1061         case Copy:
1062                 if ((what_we_got = playlist->copy (time)) != 0) {
1063                         editor.get_cut_buffer().add (what_we_got);
1064                 }
1065                 break;
1066
1067         case Clear:
1068                 if ((what_we_got = playlist->cut (time)) != 0) {
1069                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1070                         what_we_got->unref ();
1071                         ret = true;
1072                 }
1073                 break;
1074         }
1075
1076         return ret;
1077 }
1078
1079 bool
1080 RouteTimeAxisView::paste (jack_nframes_t pos, float times, Selection& selection, size_t nth)
1081 {
1082         if (!is_track()) {
1083                 return false;
1084         }
1085
1086         Playlist* playlist = get_diskstream()->playlist();
1087         PlaylistSelection::iterator p;
1088         
1089         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth);
1090
1091         if (p == selection.playlists.end()) {
1092                 return false;
1093         }
1094
1095         if (get_diskstream()->speed() != 1.0f)
1096                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1097         
1098         XMLNode &before = playlist->get_state();
1099         playlist->paste (**p, pos, times);
1100         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1101
1102         return true;
1103 }
1104
1105
1106 list<TimeAxisView*>
1107 RouteTimeAxisView::get_child_list()
1108 {
1109   
1110         list<TimeAxisView*>redirect_children;
1111         
1112         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1113                 if (!(*i)->hidden()) {
1114                         redirect_children.push_back(*i);
1115                 }
1116         }
1117         return redirect_children;
1118 }
1119
1120
1121 void
1122 RouteTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1123 {
1124         using namespace Menu_Helpers;
1125
1126         if (!menu || !is_track()) {
1127                 return;
1128         }
1129
1130         MenuList& playlist_items = menu->items();
1131         menu->set_name ("ArdourContextMenu");
1132         playlist_items.clear();
1133
1134         if (playlist_menu) {
1135                 delete playlist_menu;
1136         }
1137         playlist_menu = new Menu;
1138         playlist_menu->set_name ("ArdourContextMenu");
1139
1140         playlist_items.push_back (MenuElem (string_compose (_("Current: %1"), get_diskstream()->playlist()->name())));
1141         playlist_items.push_back (SeparatorElem());
1142         
1143         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1144         playlist_items.push_back (SeparatorElem());
1145
1146         playlist_items.push_back (MenuElem (_("New"), mem_fun(editor, &PublicEditor::new_playlists)));
1147         playlist_items.push_back (MenuElem (_("New Copy"), mem_fun(editor, &PublicEditor::copy_playlists)));
1148         playlist_items.push_back (SeparatorElem());
1149         playlist_items.push_back (MenuElem (_("Clear Current"), mem_fun(editor, &PublicEditor::clear_playlists)));
1150         playlist_items.push_back (SeparatorElem());
1151         playlist_items.push_back (MenuElem(_("Select"), mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1152
1153 }
1154
1155 void
1156 RouteTimeAxisView::show_playlist_selector ()
1157 {
1158         editor.playlist_selector().show_for (this);
1159 }
1160
1161 void
1162 RouteTimeAxisView::map_frozen ()
1163 {
1164         if (!is_track()) {
1165                 return;
1166         }
1167
1168         ENSURE_GUI_THREAD (mem_fun(*this, &RouteTimeAxisView::map_frozen));
1169
1170         switch (track()->freeze_state()) {
1171         case Track::Frozen:
1172                 playlist_button.set_sensitive (false);
1173                 rec_enable_button->set_sensitive (false);
1174                 break;
1175         default:
1176                 playlist_button.set_sensitive (true);
1177                 rec_enable_button->set_sensitive (true);
1178                 break;
1179         }
1180 }
1181
1182 void
1183 RouteTimeAxisView::color_handler (ColorID id, uint32_t val)
1184 {
1185         switch (id) {
1186         case cTimeStretchOutline:
1187                 timestretch_rect->property_outline_color_rgba() = val;
1188                 break;
1189         case cTimeStretchFill:
1190                 timestretch_rect->property_fill_color_rgba() = val;
1191                 break;
1192         default:
1193                 break;
1194         }
1195 }
1196
1197 bool
1198 RouteTimeAxisView::select_me (GdkEventButton* ev)
1199 {
1200         editor.get_selection().add (this);
1201         return false;
1202 }
1203
1204 void
1205 RouteTimeAxisView::show_all_automation ()
1206 {
1207         no_redraw = true;
1208
1209         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1210                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1211                         if ((*ii)->view == 0) {
1212                                 add_redirect_automation_curve ((*i)->redirect, (*ii)->what);
1213                         } 
1214
1215                         (*ii)->menu_item->set_active (true);
1216                 }
1217         }
1218
1219         no_redraw = false;
1220
1221          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1222 }
1223
1224 void
1225 RouteTimeAxisView::show_existing_automation ()
1226 {
1227         no_redraw = true;
1228
1229         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1230                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1231                         if ((*ii)->view != 0) {
1232                                 (*ii)->menu_item->set_active (true);
1233                         }
1234                 }
1235         }
1236
1237         no_redraw = false;
1238
1239          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1240 }
1241
1242 void
1243 RouteTimeAxisView::hide_all_automation ()
1244 {
1245         no_redraw = true;
1246
1247         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1248                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1249                         (*ii)->menu_item->set_active (false);
1250                 }
1251         }
1252
1253         no_redraw = false;
1254          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1255 }
1256
1257
1258 void
1259 RouteTimeAxisView::region_view_added (RegionView* rv)
1260 {
1261         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1262                 AutomationTimeAxisView* atv;
1263
1264                 if ((atv = dynamic_cast<AutomationTimeAxisView*> (*i)) != 0) {
1265                         rv->add_ghost (*atv);
1266                 }
1267         }
1268 }
1269
1270 void
1271 RouteTimeAxisView::add_ghost_to_redirect (RegionView* rv, AutomationTimeAxisView* atv)
1272 {
1273         rv->add_ghost (*atv);
1274 }
1275
1276 RouteTimeAxisView::RedirectAutomationInfo::~RedirectAutomationInfo ()
1277 {
1278         for (vector<RedirectAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1279                 delete *i;
1280         }
1281 }
1282
1283
1284 RouteTimeAxisView::RedirectAutomationNode::~RedirectAutomationNode ()
1285 {
1286         parent.remove_ran (this);
1287
1288         if (view) {
1289                 delete view;
1290         }
1291 }
1292
1293 void
1294 RouteTimeAxisView::remove_ran (RedirectAutomationNode* ran)
1295 {
1296         if (ran->view) {
1297                 remove_child (ran->view);
1298         }
1299 }
1300
1301 RouteTimeAxisView::RedirectAutomationNode*
1302 RouteTimeAxisView::find_redirect_automation_node (boost::shared_ptr<Redirect> redirect, uint32_t what)
1303 {
1304         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1305
1306                 if ((*i)->redirect == redirect) {
1307
1308                         for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1309                                 if ((*ii)->what == what) {
1310                                         return *ii;
1311                                 }
1312                         }
1313                 }
1314         }
1315
1316         return 0;
1317 }
1318
1319 // FIXME: duplicated in midi_time_axis.cc
1320 static string 
1321 legalize_for_xml_node (string str)
1322 {
1323         string::size_type pos;
1324         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=:";
1325         string legal;
1326
1327         legal = str;
1328         pos = 0;
1329
1330         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1331                 legal.replace (pos, 1, "_");
1332                 pos += 1;
1333         }
1334
1335         return legal;
1336 }
1337
1338
1339 void
1340 RouteTimeAxisView::add_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
1341 {
1342         RedirectAutomationLine* ral;
1343         string name;
1344         RedirectAutomationNode* ran;
1345
1346         if ((ran = find_redirect_automation_node (redirect, what)) == 0) {
1347                 fatal << _("programming error: ")
1348                       << string_compose (X_("redirect automation curve for %1:%2 not registered with audio track!"),
1349                                   redirect->name(), what)
1350                       << endmsg;
1351                 /*NOTREACHED*/
1352                 return;
1353         }
1354
1355         if (ran->view) {
1356                 return;
1357         }
1358
1359         name = redirect->describe_parameter (what);
1360
1361         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1362
1363         char state_name[256];
1364         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (redirect->name()).c_str(), what);
1365
1366         ran->view = new RedirectAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, name, what, *redirect, state_name);
1367
1368         ral = new RedirectAutomationLine (name, 
1369                                           *redirect, what, _session, *ran->view,
1370                                           *ran->view->canvas_display, redirect->automation_list (what));
1371         
1372         ral->set_line_color (color_map[cRedirectAutomationLine]);
1373         ral->queue_reset ();
1374
1375         ran->view->add_line (*ral);
1376
1377         ran->view->Hiding.connect (bind (mem_fun(*this, &RouteTimeAxisView::redirect_automation_track_hidden), ran, redirect));
1378
1379         if (!ran->view->marked_for_display()) {
1380                 ran->view->hide ();
1381         } else {
1382                 ran->menu_item->set_active (true);
1383         }
1384
1385         add_child (ran->view);
1386
1387         _view->foreach_regionview (bind (mem_fun(*this, &RouteTimeAxisView::add_ghost_to_redirect), ran->view));
1388
1389         redirect->mark_automation_visible (what, true);
1390 }
1391
1392 void
1393 RouteTimeAxisView::redirect_automation_track_hidden (RouteTimeAxisView::RedirectAutomationNode* ran, boost::shared_ptr<Redirect> r)
1394 {
1395         if (!_hidden) {
1396                 ran->menu_item->set_active (false);
1397         }
1398
1399         r->mark_automation_visible (ran->what, false);
1400
1401          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1402 }
1403
1404 void
1405 RouteTimeAxisView::add_existing_redirect_automation_curves (boost::shared_ptr<Redirect> redirect)
1406 {
1407         set<uint32_t> s;
1408         RedirectAutomationLine *ral;
1409
1410         redirect->what_has_visible_automation (s);
1411
1412         for (set<uint32_t>::iterator i = s.begin(); i != s.end(); ++i) {
1413                 
1414                 if ((ral = find_redirect_automation_curve (redirect, *i)) != 0) {
1415                         ral->queue_reset ();
1416                 } else {
1417                         add_redirect_automation_curve (redirect, (*i));
1418                 }
1419         }
1420 }
1421
1422 void
1423 RouteTimeAxisView::add_redirect_to_subplugin_menu (boost::shared_ptr<Redirect> r)
1424 {
1425         using namespace Menu_Helpers;
1426         RedirectAutomationInfo *rai;
1427         list<RedirectAutomationInfo*>::iterator x;
1428         
1429         const std::set<uint32_t>& automatable = r->what_can_be_automated ();
1430         std::set<uint32_t> has_visible_automation;
1431
1432         r->what_has_visible_automation(has_visible_automation);
1433
1434         if (automatable.empty()) {
1435                 return;
1436         }
1437
1438         for (x = redirect_automation.begin(); x != redirect_automation.end(); ++x) {
1439                 if ((*x)->redirect == r) {
1440                         break;
1441                 }
1442         }
1443
1444         if (x == redirect_automation.end()) {
1445
1446                 rai = new RedirectAutomationInfo (r);
1447                 redirect_automation.push_back (rai);
1448
1449         } else {
1450
1451                 rai = *x;
1452
1453         }
1454
1455         /* any older menu was deleted at the top of redirects_changed()
1456            when we cleared the subplugin menu.
1457         */
1458
1459         rai->menu = manage (new Menu);
1460         MenuList& items = rai->menu->items();
1461         rai->menu->set_name ("ArdourContextMenu");
1462
1463         items.clear ();
1464
1465         for (std::set<uint32_t>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
1466
1467                 RedirectAutomationNode* ran;
1468                 CheckMenuItem* mitem;
1469                 
1470                 string name = r->describe_parameter (*i);
1471                 
1472                 items.push_back (CheckMenuElem (name));
1473                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
1474
1475                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
1476                         mitem->set_active(true);
1477                 }
1478
1479                 if ((ran = find_redirect_automation_node (r, *i)) == 0) {
1480
1481                         /* new item */
1482                         
1483                         ran = new RedirectAutomationNode (*i, mitem, *this);
1484                         
1485                         rai->lines.push_back (ran);
1486
1487                 } else {
1488
1489                         ran->menu_item = mitem;
1490
1491                 }
1492
1493                 mitem->signal_toggled().connect (bind (mem_fun(*this, &RouteTimeAxisView::redirect_menu_item_toggled), rai, ran));
1494         }
1495
1496         /* add the menu for this redirect, because the subplugin
1497            menu is always cleared at the top of redirects_changed().
1498            this is the result of some poor design in gtkmm and/or
1499            GTK+.
1500         */
1501
1502         subplugin_menu.items().push_back (MenuElem (r->name(), *rai->menu));
1503         rai->valid = true;
1504 }
1505
1506 void
1507 RouteTimeAxisView::redirect_menu_item_toggled (RouteTimeAxisView::RedirectAutomationInfo* rai,
1508                                                RouteTimeAxisView::RedirectAutomationNode* ran)
1509 {
1510         bool showit = ran->menu_item->get_active();
1511         bool redraw = false;
1512
1513         if (ran->view == 0 && showit) {
1514                 add_redirect_automation_curve (rai->redirect, ran->what);
1515                 redraw = true;
1516         }
1517
1518         if (showit != ran->view->marked_for_display()) {
1519
1520                 if (showit) {
1521                         ran->view->set_marked_for_display (true);
1522                         ran->view->canvas_display->show();
1523                 } else {
1524                         rai->redirect->mark_automation_visible (ran->what, true);
1525                         ran->view->set_marked_for_display (false);
1526                         ran->view->hide ();
1527                 }
1528
1529                 redraw = true;
1530
1531         }
1532
1533         if (redraw && !no_redraw) {
1534
1535                 /* now trigger a redisplay */
1536                 
1537                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1538
1539         }
1540 }
1541
1542 void
1543 RouteTimeAxisView::redirects_changed (void *src)
1544 {
1545         using namespace Menu_Helpers;
1546
1547         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1548                 (*i)->valid = false;
1549         }
1550
1551         subplugin_menu.items().clear ();
1552
1553         _route->foreach_redirect (this, &RouteTimeAxisView::add_redirect_to_subplugin_menu);
1554         _route->foreach_redirect (this, &RouteTimeAxisView::add_existing_redirect_automation_curves);
1555
1556         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ) {
1557
1558                 list<RedirectAutomationInfo*>::iterator tmp;
1559
1560                 tmp = i;
1561                 ++tmp;
1562
1563                 if (!(*i)->valid) {
1564
1565                         delete *i;
1566                         redirect_automation.erase (i);
1567
1568                 } 
1569
1570                 i = tmp;
1571         }
1572
1573         /* change in visibility was possible */
1574
1575         _route->gui_changed ("track_height", this);
1576 }
1577
1578 RedirectAutomationLine *
1579 RouteTimeAxisView::find_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
1580 {
1581         RedirectAutomationNode* ran;
1582
1583         if ((ran = find_redirect_automation_node (redirect, what)) != 0) {
1584                 if (ran->view) {
1585                         return dynamic_cast<RedirectAutomationLine*> (ran->view->lines.front());
1586                 } 
1587         }
1588
1589         return 0;
1590 }
1591
1592 void
1593 RouteTimeAxisView::reset_redirect_automation_curves ()
1594 {
1595         for (vector<RedirectAutomationLine*>::iterator i = redirect_automation_curves.begin(); i != redirect_automation_curves.end(); ++i) {
1596                 (*i)->reset();
1597         }
1598 }
1599