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