Merged with trunk R846
[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         boost::shared_ptr<Diskstream> 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         boost::shared_ptr<Diskstream> 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         boost::shared_ptr<Diskstream> 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         boost::shared_ptr<Diskstream> 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                 editor.get_selection().toggle (*tracks);
882                 break;
883                 
884         case Selection::Set:
885                 editor.get_selection().set (*tracks);
886                 break;
887
888         case Selection::Extend:
889                 /* not defined yet */
890                 break;
891         }
892
893         delete tracks;
894 }
895
896 void
897 RouteTimeAxisView::set_selected_points (PointSelection& points)
898 {
899         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
900                 (*i)->set_selected_points (points);
901         }
902 }
903
904 void
905 RouteTimeAxisView::set_selected_regionviews (RegionSelection& regions)
906 {
907         _view->set_selected_regionviews (regions);
908 }
909
910 void
911 RouteTimeAxisView::get_selectables (jack_nframes_t start, jack_nframes_t end, double top, double bot, list<Selectable*>& results)
912 {
913         double speed = 1.0;
914         
915         if (get_diskstream() != 0) {
916                 speed = get_diskstream()->speed();
917         }
918         
919         jack_nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
920         jack_nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
921
922         if (_view && ((top < 0.0 && bot < 0.0)) || touched (top, bot)) {
923                 _view->get_selectables (start_adjusted, end_adjusted, results);
924         }
925
926         /* pick up visible automation tracks */
927         
928         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
929                 if (!(*i)->hidden()) {
930                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
931                 }
932         }
933 }
934
935 void
936 RouteTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
937 {
938         if (_view) {
939                 _view->get_inverted_selectables (sel, results);
940         }
941
942         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
943                 if (!(*i)->hidden()) {
944                         (*i)->get_inverted_selectables (sel, results);
945                 }
946         }
947
948         return;
949 }
950
951 RouteGroup*
952 RouteTimeAxisView::edit_group() const
953 {
954         return _route->edit_group();
955 }
956
957 string
958 RouteTimeAxisView::name() const
959 {
960         return _route->name();
961 }
962
963 Playlist *
964 RouteTimeAxisView::playlist () const 
965 {
966         boost::shared_ptr<Diskstream> ds;
967
968         if ((ds = get_diskstream()) != 0) {
969                 return ds->playlist(); 
970         } else {
971                 return 0; 
972         }
973 }
974
975 void
976 RouteTimeAxisView::name_entry_changed ()
977 {
978         string x;
979
980         x = name_entry.get_text ();
981         
982         if (x == _route->name()) {
983                 return;
984         }
985
986         if (x.length() == 0) {
987                 name_entry.set_text (_route->name());
988                 return;
989         }
990
991         strip_whitespace_edges(x);
992
993         if (_session.route_name_unique (x)) {
994                 _route->set_name (x, this);
995         } else {
996                 ARDOUR_UI::instance()->popup_error (_("a track already exists with that name"));
997                 name_entry.set_text (_route->name());
998         }
999 }
1000
1001 void
1002 RouteTimeAxisView::visual_click ()
1003 {
1004         popup_display_menu (0);
1005 }
1006
1007 void
1008 RouteTimeAxisView::hide_click ()
1009 {
1010         editor.hide_track_in_display (*this);
1011 }
1012
1013 Region*
1014 RouteTimeAxisView::find_next_region (jack_nframes_t pos, RegionPoint point, int32_t dir)
1015 {
1016         boost::shared_ptr<Diskstream> stream;
1017         Playlist *playlist;
1018
1019         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1020                 return playlist->find_next_region (pos, point, dir);
1021         }
1022
1023         return 0;
1024 }
1025
1026 bool
1027 RouteTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1028 {
1029         Playlist* what_we_got;
1030         boost::shared_ptr<Diskstream> ds = get_diskstream();
1031         Playlist* playlist;
1032         bool ret = false;
1033
1034         if (ds == 0) {
1035                 /* route is a bus, not a track */
1036                 return false;
1037         }
1038
1039         playlist = ds->playlist();
1040
1041
1042         TimeSelection time (selection.time);
1043         float speed = ds->speed();
1044         if (speed != 1.0f) {
1045                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1046                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1047                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1048                 }
1049         }
1050         
1051         XMLNode &before = playlist->get_state();
1052         switch (op) {
1053         case Cut:
1054                 if ((what_we_got = playlist->cut (time)) != 0) {
1055                         editor.get_cut_buffer().add (what_we_got);
1056                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1057                         ret = true;
1058                 }
1059                 break;
1060         case Copy:
1061                 if ((what_we_got = playlist->copy (time)) != 0) {
1062                         editor.get_cut_buffer().add (what_we_got);
1063                 }
1064                 break;
1065
1066         case Clear:
1067                 if ((what_we_got = playlist->cut (time)) != 0) {
1068                         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1069                         what_we_got->unref ();
1070                         ret = true;
1071                 }
1072                 break;
1073         }
1074
1075         return ret;
1076 }
1077
1078 bool
1079 RouteTimeAxisView::paste (jack_nframes_t pos, float times, Selection& selection, size_t nth)
1080 {
1081         if (!is_track()) {
1082                 return false;
1083         }
1084
1085         Playlist* playlist = get_diskstream()->playlist();
1086         PlaylistSelection::iterator p;
1087         
1088         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth);
1089
1090         if (p == selection.playlists.end()) {
1091                 return false;
1092         }
1093
1094         if (get_diskstream()->speed() != 1.0f)
1095                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1096         
1097         XMLNode &before = playlist->get_state();
1098         playlist->paste (**p, pos, times);
1099         _session.add_command( new MementoCommand<Playlist>(*playlist, &before, &playlist->get_state()));
1100
1101         return true;
1102 }
1103
1104
1105 list<TimeAxisView*>
1106 RouteTimeAxisView::get_child_list()
1107 {
1108   
1109         list<TimeAxisView*>redirect_children;
1110         
1111         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1112                 if (!(*i)->hidden()) {
1113                         redirect_children.push_back(*i);
1114                 }
1115         }
1116         return redirect_children;
1117 }
1118
1119
1120 void
1121 RouteTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1122 {
1123         using namespace Menu_Helpers;
1124
1125         if (!menu || !is_track()) {
1126                 return;
1127         }
1128
1129         MenuList& playlist_items = menu->items();
1130         menu->set_name ("ArdourContextMenu");
1131         playlist_items.clear();
1132
1133         if (playlist_menu) {
1134                 delete playlist_menu;
1135         }
1136         playlist_menu = new Menu;
1137         playlist_menu->set_name ("ArdourContextMenu");
1138
1139         playlist_items.push_back (MenuElem (string_compose (_("Current: %1"), get_diskstream()->playlist()->name())));
1140         playlist_items.push_back (SeparatorElem());
1141         
1142         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &RouteTimeAxisView::rename_current_playlist)));
1143         playlist_items.push_back (SeparatorElem());
1144
1145         playlist_items.push_back (MenuElem (_("New"), mem_fun(editor, &PublicEditor::new_playlists)));
1146         playlist_items.push_back (MenuElem (_("New Copy"), mem_fun(editor, &PublicEditor::copy_playlists)));
1147         playlist_items.push_back (SeparatorElem());
1148         playlist_items.push_back (MenuElem (_("Clear Current"), mem_fun(editor, &PublicEditor::clear_playlists)));
1149         playlist_items.push_back (SeparatorElem());
1150         playlist_items.push_back (MenuElem(_("Select"), mem_fun(*this, &RouteTimeAxisView::show_playlist_selector)));
1151
1152 }
1153
1154 void
1155 RouteTimeAxisView::show_playlist_selector ()
1156 {
1157         editor.playlist_selector().show_for (this);
1158 }
1159
1160 void
1161 RouteTimeAxisView::map_frozen ()
1162 {
1163         if (!is_track()) {
1164                 return;
1165         }
1166
1167         ENSURE_GUI_THREAD (mem_fun(*this, &RouteTimeAxisView::map_frozen));
1168
1169         switch (track()->freeze_state()) {
1170         case Track::Frozen:
1171                 playlist_button.set_sensitive (false);
1172                 rec_enable_button->set_sensitive (false);
1173                 break;
1174         default:
1175                 playlist_button.set_sensitive (true);
1176                 rec_enable_button->set_sensitive (true);
1177                 break;
1178         }
1179 }
1180
1181 void
1182 RouteTimeAxisView::color_handler (ColorID id, uint32_t val)
1183 {
1184         switch (id) {
1185         case cTimeStretchOutline:
1186                 timestretch_rect->property_outline_color_rgba() = val;
1187                 break;
1188         case cTimeStretchFill:
1189                 timestretch_rect->property_fill_color_rgba() = val;
1190                 break;
1191         default:
1192                 break;
1193         }
1194 }
1195
1196 bool
1197 RouteTimeAxisView::select_me (GdkEventButton* ev)
1198 {
1199         editor.get_selection().add (this);
1200         return false;
1201 }
1202
1203 void
1204 RouteTimeAxisView::show_all_automation ()
1205 {
1206         no_redraw = true;
1207
1208         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1209                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1210                         if ((*ii)->view == 0) {
1211                                 add_redirect_automation_curve ((*i)->redirect, (*ii)->what);
1212                         } 
1213
1214                         (*ii)->menu_item->set_active (true);
1215                 }
1216         }
1217
1218         no_redraw = false;
1219
1220          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1221 }
1222
1223 void
1224 RouteTimeAxisView::show_existing_automation ()
1225 {
1226         no_redraw = true;
1227
1228         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1229                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1230                         if ((*ii)->view != 0) {
1231                                 (*ii)->menu_item->set_active (true);
1232                         }
1233                 }
1234         }
1235
1236         no_redraw = false;
1237
1238          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1239 }
1240
1241 void
1242 RouteTimeAxisView::hide_all_automation ()
1243 {
1244         no_redraw = true;
1245
1246         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1247                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1248                         (*ii)->menu_item->set_active (false);
1249                 }
1250         }
1251
1252         no_redraw = false;
1253          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1254 }
1255
1256
1257 void
1258 RouteTimeAxisView::region_view_added (RegionView* rv)
1259 {
1260         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1261                 AutomationTimeAxisView* atv;
1262
1263                 if ((atv = dynamic_cast<AutomationTimeAxisView*> (*i)) != 0) {
1264                         rv->add_ghost (*atv);
1265                 }
1266         }
1267 }
1268
1269 void
1270 RouteTimeAxisView::add_ghost_to_redirect (RegionView* rv, AutomationTimeAxisView* atv)
1271 {
1272         rv->add_ghost (*atv);
1273 }
1274
1275 RouteTimeAxisView::RedirectAutomationInfo::~RedirectAutomationInfo ()
1276 {
1277         for (vector<RedirectAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1278                 delete *i;
1279         }
1280 }
1281
1282
1283 RouteTimeAxisView::RedirectAutomationNode::~RedirectAutomationNode ()
1284 {
1285         parent.remove_ran (this);
1286
1287         if (view) {
1288                 delete view;
1289         }
1290 }
1291
1292 void
1293 RouteTimeAxisView::remove_ran (RedirectAutomationNode* ran)
1294 {
1295         if (ran->view) {
1296                 remove_child (ran->view);
1297         }
1298 }
1299
1300 RouteTimeAxisView::RedirectAutomationNode*
1301 RouteTimeAxisView::find_redirect_automation_node (boost::shared_ptr<Redirect> redirect, uint32_t what)
1302 {
1303         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1304
1305                 if ((*i)->redirect == redirect) {
1306
1307                         for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1308                                 if ((*ii)->what == what) {
1309                                         return *ii;
1310                                 }
1311                         }
1312                 }
1313         }
1314
1315         return 0;
1316 }
1317
1318 // FIXME: duplicated in midi_time_axis.cc
1319 static string 
1320 legalize_for_xml_node (string str)
1321 {
1322         string::size_type pos;
1323         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=:";
1324         string legal;
1325
1326         legal = str;
1327         pos = 0;
1328
1329         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1330                 legal.replace (pos, 1, "_");
1331                 pos += 1;
1332         }
1333
1334         return legal;
1335 }
1336
1337
1338 void
1339 RouteTimeAxisView::add_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
1340 {
1341         RedirectAutomationLine* ral;
1342         string name;
1343         RedirectAutomationNode* ran;
1344
1345         if ((ran = find_redirect_automation_node (redirect, what)) == 0) {
1346                 fatal << _("programming error: ")
1347                       << string_compose (X_("redirect automation curve for %1:%2 not registered with audio track!"),
1348                                   redirect->name(), what)
1349                       << endmsg;
1350                 /*NOTREACHED*/
1351                 return;
1352         }
1353
1354         if (ran->view) {
1355                 return;
1356         }
1357
1358         name = redirect->describe_parameter (what);
1359
1360         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1361
1362         char state_name[256];
1363         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (redirect->name()).c_str(), what);
1364
1365         ran->view = new RedirectAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, name, what, *redirect, state_name);
1366
1367         ral = new RedirectAutomationLine (name, 
1368                                           *redirect, what, _session, *ran->view,
1369                                           *ran->view->canvas_display, redirect->automation_list (what));
1370         
1371         ral->set_line_color (color_map[cRedirectAutomationLine]);
1372         ral->queue_reset ();
1373
1374         ran->view->add_line (*ral);
1375
1376         ran->view->Hiding.connect (bind (mem_fun(*this, &RouteTimeAxisView::redirect_automation_track_hidden), ran, redirect));
1377
1378         if (!ran->view->marked_for_display()) {
1379                 ran->view->hide ();
1380         } else {
1381                 ran->menu_item->set_active (true);
1382         }
1383
1384         add_child (ran->view);
1385
1386         _view->foreach_regionview (bind (mem_fun(*this, &RouteTimeAxisView::add_ghost_to_redirect), ran->view));
1387
1388         redirect->mark_automation_visible (what, true);
1389 }
1390
1391 void
1392 RouteTimeAxisView::redirect_automation_track_hidden (RouteTimeAxisView::RedirectAutomationNode* ran, boost::shared_ptr<Redirect> r)
1393 {
1394         if (!_hidden) {
1395                 ran->menu_item->set_active (false);
1396         }
1397
1398         r->mark_automation_visible (ran->what, false);
1399
1400          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1401 }
1402
1403 void
1404 RouteTimeAxisView::add_existing_redirect_automation_curves (boost::shared_ptr<Redirect> redirect)
1405 {
1406         set<uint32_t> s;
1407         RedirectAutomationLine *ral;
1408
1409         redirect->what_has_visible_automation (s);
1410
1411         for (set<uint32_t>::iterator i = s.begin(); i != s.end(); ++i) {
1412                 
1413                 if ((ral = find_redirect_automation_curve (redirect, *i)) != 0) {
1414                         ral->queue_reset ();
1415                 } else {
1416                         add_redirect_automation_curve (redirect, (*i));
1417                 }
1418         }
1419 }
1420
1421 void
1422 RouteTimeAxisView::add_redirect_to_subplugin_menu (boost::shared_ptr<Redirect> r)
1423 {
1424         using namespace Menu_Helpers;
1425         RedirectAutomationInfo *rai;
1426         list<RedirectAutomationInfo*>::iterator x;
1427         
1428         const std::set<uint32_t>& automatable = r->what_can_be_automated ();
1429         std::set<uint32_t> has_visible_automation;
1430
1431         r->what_has_visible_automation(has_visible_automation);
1432
1433         if (automatable.empty()) {
1434                 return;
1435         }
1436
1437         for (x = redirect_automation.begin(); x != redirect_automation.end(); ++x) {
1438                 if ((*x)->redirect == r) {
1439                         break;
1440                 }
1441         }
1442
1443         if (x == redirect_automation.end()) {
1444
1445                 rai = new RedirectAutomationInfo (r);
1446                 redirect_automation.push_back (rai);
1447
1448         } else {
1449
1450                 rai = *x;
1451
1452         }
1453
1454         /* any older menu was deleted at the top of redirects_changed()
1455            when we cleared the subplugin menu.
1456         */
1457
1458         rai->menu = manage (new Menu);
1459         MenuList& items = rai->menu->items();
1460         rai->menu->set_name ("ArdourContextMenu");
1461
1462         items.clear ();
1463
1464         for (std::set<uint32_t>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
1465
1466                 RedirectAutomationNode* ran;
1467                 CheckMenuItem* mitem;
1468                 
1469                 string name = r->describe_parameter (*i);
1470                 
1471                 items.push_back (CheckMenuElem (name));
1472                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
1473
1474                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
1475                         mitem->set_active(true);
1476                 }
1477
1478                 if ((ran = find_redirect_automation_node (r, *i)) == 0) {
1479
1480                         /* new item */
1481                         
1482                         ran = new RedirectAutomationNode (*i, mitem, *this);
1483                         
1484                         rai->lines.push_back (ran);
1485
1486                 } else {
1487
1488                         ran->menu_item = mitem;
1489
1490                 }
1491
1492                 mitem->signal_toggled().connect (bind (mem_fun(*this, &RouteTimeAxisView::redirect_menu_item_toggled), rai, ran));
1493         }
1494
1495         /* add the menu for this redirect, because the subplugin
1496            menu is always cleared at the top of redirects_changed().
1497            this is the result of some poor design in gtkmm and/or
1498            GTK+.
1499         */
1500
1501         subplugin_menu.items().push_back (MenuElem (r->name(), *rai->menu));
1502         rai->valid = true;
1503 }
1504
1505 void
1506 RouteTimeAxisView::redirect_menu_item_toggled (RouteTimeAxisView::RedirectAutomationInfo* rai,
1507                                                RouteTimeAxisView::RedirectAutomationNode* ran)
1508 {
1509         bool showit = ran->menu_item->get_active();
1510         bool redraw = false;
1511
1512         if (ran->view == 0 && showit) {
1513                 add_redirect_automation_curve (rai->redirect, ran->what);
1514                 redraw = true;
1515         }
1516
1517         if (showit != ran->view->marked_for_display()) {
1518
1519                 if (showit) {
1520                         ran->view->set_marked_for_display (true);
1521                         ran->view->canvas_display->show();
1522                 } else {
1523                         rai->redirect->mark_automation_visible (ran->what, true);
1524                         ran->view->set_marked_for_display (false);
1525                         ran->view->hide ();
1526                 }
1527
1528                 redraw = true;
1529
1530         }
1531
1532         if (redraw && !no_redraw) {
1533
1534                 /* now trigger a redisplay */
1535                 
1536                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1537
1538         }
1539 }
1540
1541 void
1542 RouteTimeAxisView::redirects_changed (void *src)
1543 {
1544         using namespace Menu_Helpers;
1545
1546         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1547                 (*i)->valid = false;
1548         }
1549
1550         subplugin_menu.items().clear ();
1551
1552         _route->foreach_redirect (this, &RouteTimeAxisView::add_redirect_to_subplugin_menu);
1553         _route->foreach_redirect (this, &RouteTimeAxisView::add_existing_redirect_automation_curves);
1554
1555         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ) {
1556
1557                 list<RedirectAutomationInfo*>::iterator tmp;
1558
1559                 tmp = i;
1560                 ++tmp;
1561
1562                 if (!(*i)->valid) {
1563
1564                         delete *i;
1565                         redirect_automation.erase (i);
1566
1567                 } 
1568
1569                 i = tmp;
1570         }
1571
1572         /* change in visibility was possible */
1573
1574         _route->gui_changed ("track_height", this);
1575 }
1576
1577 RedirectAutomationLine *
1578 RouteTimeAxisView::find_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
1579 {
1580         RedirectAutomationNode* ran;
1581
1582         if ((ran = find_redirect_automation_node (redirect, what)) != 0) {
1583                 if (ran->view) {
1584                         return dynamic_cast<RedirectAutomationLine*> (ran->view->lines.front());
1585                 } 
1586         }
1587
1588         return 0;
1589 }
1590
1591 void
1592 RouteTimeAxisView::reset_redirect_automation_curves ()
1593 {
1594         for (vector<RedirectAutomationLine*>::iterator i = redirect_automation_curves.begin(); i != redirect_automation_curves.end(); ++i) {
1595                 (*i)->reset();
1596         }
1597 }
1598