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