Merged with trunk R708
[ardour.git] / gtk2_ardour / audio_time_axis.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cmath>
23 #include <cassert>
24
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28
29 #include <sigc++/bind.h>
30
31 #include <pbd/error.h>
32 #include <pbd/stl_delete.h>
33 #include <pbd/whitespace.h>
34
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/selector.h>
37 #include <gtkmm2ext/stop_signal.h>
38 #include <gtkmm2ext/bindable_button.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include <ardour/audioplaylist.h>
42 #include <ardour/audio_diskstream.h>
43 #include <ardour/insert.h>
44 #include <ardour/ladspa_plugin.h>
45 #include <ardour/location.h>
46 #include <ardour/panner.h>
47 #include <ardour/playlist.h>
48 #include <ardour/session.h>
49 #include <ardour/session_playlist.h>
50 #include <ardour/utils.h>
51
52 #include "ardour_ui.h"
53 #include "audio_time_axis.h"
54 #include "automation_gain_line.h"
55 #include "automation_pan_line.h"
56 #include "automation_time_axis.h"
57 #include "canvas_impl.h"
58 #include "crossfade_view.h"
59 #include "enums.h"
60 #include "gain_automation_time_axis.h"
61 #include "gui_thread.h"
62 #include "keyboard.h"
63 #include "pan_automation_time_axis.h"
64 #include "playlist_selector.h"
65 #include "plugin_selector.h"
66 #include "plugin_ui.h"
67 #include "point_selection.h"
68 #include "prompter.h"
69 #include "public_editor.h"
70 #include "redirect_automation_line.h"
71 #include "redirect_automation_time_axis.h"
72 #include "audio_regionview.h"
73 #include "rgb_macros.h"
74 #include "selection.h"
75 #include "simplerect.h"
76 #include "audio_streamview.h"
77 #include "utils.h"
78
79 #include <ardour/audio_track.h>
80
81 #include "i18n.h"
82
83 using namespace ARDOUR;
84 using namespace PBD;
85 using namespace Gtk;
86 using namespace Editing;
87
88
89 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
90         : AxisView(sess), // FIXME: won't compile without this, why??
91         RouteTimeAxisView(ed, sess, rt, canvas)
92 {
93         subplugin_menu.set_name ("ArdourContextMenu");
94         gain_track = 0;
95         pan_track = 0;
96         waveform_item = 0;
97         pan_automation_item = 0;
98         gain_automation_item = 0;
99
100         _view = new AudioStreamView (*this);
101
102         add_gain_automation_child ();
103         add_pan_automation_child ();
104
105         ignore_toggle = false;
106
107         mute_button->set_active (false);
108         solo_button->set_active (false);
109         
110         if (is_audio_track())
111                 controls_ebox.set_name ("AudioTimeAxisViewControlsBaseUnselected");
112         else // bus
113                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
114
115         /* map current state of the route */
116
117         redirects_changed (0);
118         reset_redirect_automation_curves ();
119
120         ensure_xml_node ();
121
122         set_state (*xml_node);
123         
124         _route->panner().Changed.connect (mem_fun(*this, &AudioTimeAxisView::update_pans));
125
126         if (is_audio_track()) {
127
128                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
129                 controls_base_selected_name = "AudioTrackControlsBaseSelected";
130                 controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
131
132                 /* ask for notifications of any new RegionViews */
133                 _view->RegionViewAdded.connect (mem_fun(*this, &AudioTimeAxisView::region_view_added));
134                 _view->attach ();
135
136         } else { /* bus */
137
138                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
139                 controls_base_selected_name = "AudioBusControlsBaseSelected";
140                 controls_base_unselected_name = "AudioBusControlsBaseUnselected";
141         }
142 }
143
144 AudioTimeAxisView::~AudioTimeAxisView ()
145 {
146         vector_delete (&redirect_automation_curves);
147
148         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
149                 delete *i;
150         }
151 }
152
153 AudioStreamView*
154 AudioTimeAxisView::audio_view()
155 {
156         return dynamic_cast<AudioStreamView*>(_view);
157 }
158
159 guint32
160 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
161 {
162         ensure_xml_node ();
163         xml_node->add_property ("shown_editor", "yes");
164                 
165         return TimeAxisView::show_at (y, nth, parent);
166 }
167
168 void
169 AudioTimeAxisView::hide ()
170 {
171         ensure_xml_node ();
172         xml_node->add_property ("shown_editor", "no");
173
174         TimeAxisView::hide ();
175 }
176
177 void
178 AudioTimeAxisView::set_state (const XMLNode& node)
179 {
180         const XMLProperty *prop;
181         
182         TimeAxisView::set_state (node);
183         
184         if ((prop = node.property ("shown_editor")) != 0) {
185                 if (prop->value() == "no") {
186                         _marked_for_display = false;
187                 } else {
188                         _marked_for_display = true;
189                 }
190         } else {
191                 _marked_for_display = true;
192         }
193         
194         XMLNodeList nlist = node.children();
195         XMLNodeConstIterator niter;
196         XMLNode *child_node;
197         
198         
199         show_gain_automation = false;
200         show_pan_automation  = false;
201         
202         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
203                 child_node = *niter;
204
205                 if (child_node->name() == "gain") {
206                         XMLProperty *prop=child_node->property ("shown");
207                         
208                         if (prop != 0) {
209                                 if (prop->value() == "yes") {
210                                         show_gain_automation = true;
211                                 }
212                         }
213                         continue;
214                 }
215                 
216                 if (child_node->name() == "pan") {
217                         XMLProperty *prop=child_node->property ("shown");
218                         
219                         if (prop != 0) {
220                                 if (prop->value() == "yes") {
221                                         show_pan_automation = true;
222                                 }                       
223                         }
224                         continue;
225                 }
226         }
227 }
228
229 void
230 AudioTimeAxisView::reset_redirect_automation_curves ()
231 {
232         for (vector<RedirectAutomationLine*>::iterator i = redirect_automation_curves.begin(); i != redirect_automation_curves.end(); ++i) {
233                 (*i)->reset();
234         }
235 }
236
237 void
238 AudioTimeAxisView::build_display_menu ()
239 {
240         using namespace Menu_Helpers;
241
242         /* get the size menu ready */
243
244         build_size_menu ();
245
246         /* prepare it */
247
248         TimeAxisView::build_display_menu ();
249
250         /* now fill it with our stuff */
251
252         MenuList& items = display_menu->items();
253         display_menu->set_name ("ArdourContextMenu");
254         
255         items.push_back (MenuElem (_("Height"), *size_menu));
256         items.push_back (MenuElem (_("Color"), mem_fun(*this, &AudioTimeAxisView::select_track_color)));
257
258
259         items.push_back (SeparatorElem());
260         items.push_back (MenuElem (_("Hide all crossfades"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
261         items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
262         items.push_back (SeparatorElem());
263
264         build_remote_control_menu ();
265         items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
266
267         automation_action_menu = manage (new Menu);
268         MenuList& automation_items = automation_action_menu->items();
269         automation_action_menu->set_name ("ArdourContextMenu");
270         
271         automation_items.push_back (MenuElem (_("Show all automation"),
272                                               mem_fun(*this, &AudioTimeAxisView::show_all_automation)));
273
274         automation_items.push_back (MenuElem (_("Show existing automation"),
275                                               mem_fun(*this, &AudioTimeAxisView::show_existing_automation)));
276
277         automation_items.push_back (MenuElem (_("Hide all automation"),
278                                               mem_fun(*this, &AudioTimeAxisView::hide_all_automation)));
279
280         automation_items.push_back (SeparatorElem());
281
282         automation_items.push_back (CheckMenuElem (_("Fader"), 
283                                                    mem_fun(*this, &AudioTimeAxisView::toggle_gain_track)));
284         gain_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
285         gain_automation_item->set_active(show_gain_automation);
286
287         automation_items.push_back (CheckMenuElem (_("Pan"),
288                                                    mem_fun(*this, &AudioTimeAxisView::toggle_pan_track)));
289         pan_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
290         pan_automation_item->set_active(show_pan_automation);
291
292         automation_items.push_back (MenuElem (_("Plugins"), subplugin_menu));
293
294         items.push_back (MenuElem (_("Automation"), *automation_action_menu));
295
296         Menu *waveform_menu = manage(new Menu);
297         MenuList& waveform_items = waveform_menu->items();
298         waveform_menu->set_name ("ArdourContextMenu");
299         
300         waveform_items.push_back (CheckMenuElem (_("Show waveforms"), mem_fun(*this, &AudioTimeAxisView::toggle_waveforms)));
301         waveform_item = static_cast<CheckMenuItem *> (&waveform_items.back());
302         ignore_toggle = true;
303         waveform_item->set_active (editor.show_waveforms());
304         ignore_toggle = false;
305
306         RadioMenuItem::Group group;
307
308         waveform_items.push_back (RadioMenuElem (group, _("Traditional"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Traditional)));
309         traditional_item = static_cast<RadioMenuItem *> (&waveform_items.back());
310
311         waveform_items.push_back (RadioMenuElem (group, _("Rectified"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Rectified)));
312         rectified_item = static_cast<RadioMenuItem *> (&waveform_items.back());
313
314         items.push_back (MenuElem (_("Waveform"), *waveform_menu));
315
316         if (is_audio_track()) {
317
318                 Menu* alignment_menu = manage (new Menu);
319                 MenuList& alignment_items = alignment_menu->items();
320                 alignment_menu->set_name ("ArdourContextMenu");
321
322                 RadioMenuItem::Group align_group;
323                 
324                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"), bind (mem_fun(*this, &AudioTimeAxisView::set_align_style), ExistingMaterial)));
325                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
326                 if (get_diskstream()->alignment_style() == ExistingMaterial) {
327                         align_existing_item->set_active();
328                 }
329                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"), bind (mem_fun(*this, &AudioTimeAxisView::set_align_style), CaptureTime)));
330                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
331                 if (get_diskstream()->alignment_style() == CaptureTime) {
332                         align_capture_item->set_active();
333                 }
334                 
335                 items.push_back (MenuElem (_("Alignment"), *alignment_menu));
336
337                 get_diskstream()->AlignmentStyleChanged.connect (mem_fun(*this, &AudioTimeAxisView::align_style_changed));
338         }
339
340         items.push_back (SeparatorElem());
341         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
342         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
343         route_active_menu_item->set_active (_route->active());
344
345         items.push_back (SeparatorElem());
346         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
347
348 }
349
350 void
351 AudioTimeAxisView::toggle_waveforms ()
352 {
353         AudioStreamView* asv = audio_view();
354         assert(asv);
355
356         if (asv && waveform_item && !ignore_toggle) {
357                 asv->set_show_waveforms (waveform_item->get_active());
358         }
359 }
360
361 void
362 AudioTimeAxisView::set_show_waveforms (bool yn)
363 {
364         AudioStreamView* asv = audio_view();
365         assert(asv);
366
367         if (waveform_item) {
368                 waveform_item->set_active (yn);
369         } else {
370                 asv->set_show_waveforms (yn);
371         }
372 }
373
374 void
375 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
376 {
377         AudioStreamView* asv = audio_view();
378
379         if (asv) {
380                 asv->set_show_waveforms_recording (yn);
381         }
382 }
383
384 void
385 AudioTimeAxisView::set_waveform_shape (WaveformShape shape)
386 {
387         AudioStreamView* asv = audio_view();
388
389         if (asv) {
390                 asv->set_waveform_shape (shape);
391         }
392
393         map_frozen ();
394 }       
395
396 void
397 AudioTimeAxisView::set_selected_regionviews (RegionSelection& regions)
398 {
399         AudioStreamView* asv = audio_view();
400
401         if (asv) {
402                 asv->set_selected_regionviews (regions);
403         }
404 }
405
406 void
407 AudioTimeAxisView::add_gain_automation_child ()
408 {
409         XMLProperty* prop;
410         AutomationLine* line;
411
412         gain_track = new GainAutomationTimeAxisView (_session,
413                                                      _route,
414                                                      editor,
415                                                      *this,
416                                                      parent_canvas,
417                                                      _("gain"),
418                                                      _route->gain_automation_curve());
419         
420         line = new AutomationGainLine ("automation gain",
421                                        _session,
422                                        *gain_track,
423                                        *gain_track->canvas_display,
424                                        _route->gain_automation_curve());
425
426         line->set_line_color (color_map[cAutomationLine]);
427         
428
429         gain_track->add_line (*line);
430
431         add_child (gain_track);
432
433         gain_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::gain_hidden));
434
435         bool hideit = true;
436         
437         XMLNode* node;
438
439         if ((node = gain_track->get_state_node()) != 0) {
440                 if  ((prop = node->property ("shown")) != 0) {
441                         if (prop->value() == "yes") {
442                                 hideit = false;
443                         }
444                 } 
445         }
446
447         if (hideit) {
448                 gain_track->hide ();
449         }
450 }
451
452 void
453 AudioTimeAxisView::add_pan_automation_child ()
454 {
455         XMLProperty* prop;
456
457         pan_track = new PanAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, _("pan"));
458
459         update_pans ();
460         
461         add_child (pan_track);
462
463         pan_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::pan_hidden));
464
465         ensure_xml_node ();
466         bool hideit = true;
467         
468         XMLNode* node;
469
470         if ((node = pan_track->get_state_node()) != 0) {
471                 if ((prop = node->property ("shown")) != 0) {
472                         if (prop->value() == "yes") {
473                                 hideit = false;
474                         }
475                 } 
476         }
477
478         if (hideit) {
479                 pan_track->hide ();
480         }
481 }
482
483 void
484 AudioTimeAxisView::update_pans ()
485 {
486         Panner::iterator p;
487         
488         pan_track->clear_lines ();
489         
490         /* we don't draw lines for "greater than stereo" panning.
491          */
492
493         if (_route->n_outputs() > 2) {
494                 return;
495         }
496
497         for (p = _route->panner().begin(); p != _route->panner().end(); ++p) {
498
499                 AutomationLine* line;
500
501                 line = new AutomationPanLine ("automation pan", _session, *pan_track,
502                                               *pan_track->canvas_display, 
503                                               (*p)->automation());
504
505                 if (p == _route->panner().begin()) {
506                         /* first line is a nice orange */
507                         line->set_line_color (color_map[cLeftPanAutomationLine]);
508                 } else {
509                         /* second line is a nice blue */
510                         line->set_line_color (color_map[cRightPanAutomationLine]);
511                 }
512
513                 pan_track->add_line (*line);
514         }
515 }
516                 
517 void
518 AudioTimeAxisView::toggle_gain_track ()
519 {
520
521         bool showit = gain_automation_item->get_active();
522
523         if (showit != gain_track->marked_for_display()) {
524                 if (showit) {
525                         gain_track->set_marked_for_display (true);
526                         gain_track->canvas_display->show();
527                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
528                 } else {
529                         gain_track->set_marked_for_display (false);
530                         gain_track->hide ();
531                         gain_track->get_state_node()->add_property ("shown", X_("no"));
532                 }
533
534                 /* now trigger a redisplay */
535                 
536                 if (!no_redraw) {
537                          _route->gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
538                 }
539         }
540 }
541
542 void
543 AudioTimeAxisView::gain_hidden ()
544 {
545         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
546
547         if (gain_automation_item && !_hidden) {
548                 gain_automation_item->set_active (false);
549         }
550
551          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
552 }
553
554 void
555 AudioTimeAxisView::toggle_pan_track ()
556 {
557         bool showit = pan_automation_item->get_active();
558
559         if (showit != pan_track->marked_for_display()) {
560                 if (showit) {
561                         pan_track->set_marked_for_display (true);
562                         pan_track->canvas_display->show();
563                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
564                 } else {
565                         pan_track->set_marked_for_display (false);
566                         pan_track->hide ();
567                         pan_track->get_state_node()->add_property ("shown", X_("no"));
568                 }
569
570                 /* now trigger a redisplay */
571                 
572                 if (!no_redraw) {
573                          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
574                 }
575         }
576 }
577
578 void
579 AudioTimeAxisView::pan_hidden ()
580 {
581         pan_track->get_state_node()->add_property ("shown", "no");
582
583         if (pan_automation_item && !_hidden) {
584                 pan_automation_item->set_active (false);
585         }
586
587          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
588 }
589
590 AudioTimeAxisView::RedirectAutomationInfo::~RedirectAutomationInfo ()
591 {
592         for (vector<RedirectAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
593                 delete *i;
594         }
595 }
596
597
598 AudioTimeAxisView::RedirectAutomationNode::~RedirectAutomationNode ()
599 {
600         parent.remove_ran (this);
601
602         if (view) {
603                 delete view;
604         }
605 }
606
607 void
608 AudioTimeAxisView::remove_ran (RedirectAutomationNode* ran)
609 {
610         if (ran->view) {
611                 remove_child (ran->view);
612         }
613 }
614
615 AudioTimeAxisView::RedirectAutomationNode*
616 AudioTimeAxisView::find_redirect_automation_node (boost::shared_ptr<Redirect> redirect, uint32_t what)
617 {
618         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
619
620                 if ((*i)->redirect == redirect) {
621
622                         for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
623                                 if ((*ii)->what == what) {
624                                         return *ii;
625                                 }
626                         }
627                 }
628         }
629
630         return 0;
631 }
632
633 // FIXME: duplicated in midi_time_axis.cc
634 static string 
635 legalize_for_xml_node (string str)
636 {
637         string::size_type pos;
638         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=:";
639         string legal;
640
641         legal = str;
642         pos = 0;
643
644         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
645                 legal.replace (pos, 1, "_");
646                 pos += 1;
647         }
648
649         return legal;
650 }
651
652
653 void
654 AudioTimeAxisView::add_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
655 {
656         RedirectAutomationLine* ral;
657         string name;
658         RedirectAutomationNode* ran;
659
660         if ((ran = find_redirect_automation_node (redirect, what)) == 0) {
661                 fatal << _("programming error: ")
662                       << string_compose (X_("redirect automation curve for %1:%2 not registered with audio track!"),
663                                   redirect->name(), what)
664                       << endmsg;
665                 /*NOTREACHED*/
666                 return;
667         }
668
669         if (ran->view) {
670                 return;
671         }
672
673         name = redirect->describe_parameter (what);
674
675         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
676
677         char state_name[256];
678         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (redirect->name()).c_str(), what);
679
680         ran->view = new RedirectAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, name, what, *redirect, state_name);
681
682         ral = new RedirectAutomationLine (name, 
683                                           *redirect, what, _session, *ran->view,
684                                           *ran->view->canvas_display, redirect->automation_list (what));
685         
686         ral->set_line_color (color_map[cRedirectAutomationLine]);
687         ral->queue_reset ();
688
689         ran->view->add_line (*ral);
690
691         ran->view->Hiding.connect (bind (mem_fun(*this, &AudioTimeAxisView::redirect_automation_track_hidden), ran, redirect));
692
693         if (!ran->view->marked_for_display()) {
694                 ran->view->hide ();
695         } else {
696                 ran->menu_item->set_active (true);
697         }
698
699         add_child (ran->view);
700
701         audio_view()->foreach_regionview (bind (mem_fun(*this, &AudioTimeAxisView::add_ghost_to_redirect), ran->view));
702
703         redirect->mark_automation_visible (what, true);
704 }
705
706 void
707 AudioTimeAxisView::redirect_automation_track_hidden (AudioTimeAxisView::RedirectAutomationNode* ran, boost::shared_ptr<Redirect> r)
708 {
709         if (!_hidden) {
710                 ran->menu_item->set_active (false);
711         }
712
713         r->mark_automation_visible (ran->what, false);
714
715          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
716 }
717
718 void
719 AudioTimeAxisView::add_existing_redirect_automation_curves (boost::shared_ptr<Redirect> redirect)
720 {
721         set<uint32_t> s;
722         RedirectAutomationLine *ral;
723
724         redirect->what_has_visible_automation (s);
725
726         for (set<uint32_t>::iterator i = s.begin(); i != s.end(); ++i) {
727                 
728                 if ((ral = find_redirect_automation_curve (redirect, *i)) != 0) {
729                         ral->queue_reset ();
730                 } else {
731                         add_redirect_automation_curve (redirect, (*i));
732                 }
733         }
734 }
735
736 void
737 AudioTimeAxisView::add_redirect_to_subplugin_menu (boost::shared_ptr<Redirect> r)
738 {
739         using namespace Menu_Helpers;
740         RedirectAutomationInfo *rai;
741         list<RedirectAutomationInfo*>::iterator x;
742         
743         const std::set<uint32_t>& automatable = r->what_can_be_automated ();
744         std::set<uint32_t> has_visible_automation;
745
746         r->what_has_visible_automation(has_visible_automation);
747
748         if (automatable.empty()) {
749                 return;
750         }
751
752         for (x = redirect_automation.begin(); x != redirect_automation.end(); ++x) {
753                 if ((*x)->redirect == r) {
754                         break;
755                 }
756         }
757
758         if (x == redirect_automation.end()) {
759
760                 rai = new RedirectAutomationInfo (r);
761                 redirect_automation.push_back (rai);
762
763         } else {
764
765                 rai = *x;
766
767         }
768
769         /* any older menu was deleted at the top of redirects_changed()
770            when we cleared the subplugin menu.
771         */
772
773         rai->menu = manage (new Menu);
774         MenuList& items = rai->menu->items();
775         rai->menu->set_name ("ArdourContextMenu");
776
777         items.clear ();
778
779         for (std::set<uint32_t>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
780
781                 RedirectAutomationNode* ran;
782                 CheckMenuItem* mitem;
783                 
784                 string name = r->describe_parameter (*i);
785                 
786                 items.push_back (CheckMenuElem (name));
787                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
788
789                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
790                         mitem->set_active(true);
791                 }
792
793                 if ((ran = find_redirect_automation_node (r, *i)) == 0) {
794
795                         /* new item */
796                         
797                         ran = new RedirectAutomationNode (*i, mitem, *this);
798                         
799                         rai->lines.push_back (ran);
800
801                 } else {
802
803                         ran->menu_item = mitem;
804
805                 }
806
807                 mitem->signal_toggled().connect (bind (mem_fun(*this, &AudioTimeAxisView::redirect_menu_item_toggled), rai, ran));
808         }
809
810         /* add the menu for this redirect, because the subplugin
811            menu is always cleared at the top of redirects_changed().
812            this is the result of some poor design in gtkmm and/or
813            GTK+.
814         */
815
816         subplugin_menu.items().push_back (MenuElem (r->name(), *rai->menu));
817         rai->valid = true;
818 }
819
820 void
821 AudioTimeAxisView::redirect_menu_item_toggled (AudioTimeAxisView::RedirectAutomationInfo* rai,
822                                                AudioTimeAxisView::RedirectAutomationNode* ran)
823 {
824         bool showit = ran->menu_item->get_active();
825         bool redraw = false;
826
827         if (ran->view == 0 && showit) {
828                 add_redirect_automation_curve (rai->redirect, ran->what);
829                 redraw = true;
830         }
831
832         if (showit != ran->view->marked_for_display()) {
833
834                 if (showit) {
835                         ran->view->set_marked_for_display (true);
836                         ran->view->canvas_display->show();
837                 } else {
838                         rai->redirect->mark_automation_visible (ran->what, true);
839                         ran->view->set_marked_for_display (false);
840                         ran->view->hide ();
841                 }
842
843                 redraw = true;
844
845         }
846
847         if (redraw && !no_redraw) {
848
849                 /* now trigger a redisplay */
850                 
851                  _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
852
853         }
854 }
855
856 void
857 AudioTimeAxisView::redirects_changed (void *src)
858 {
859         using namespace Menu_Helpers;
860
861         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
862                 (*i)->valid = false;
863         }
864
865         subplugin_menu.items().clear ();
866
867         _route->foreach_redirect (this, &AudioTimeAxisView::add_redirect_to_subplugin_menu);
868         _route->foreach_redirect (this, &AudioTimeAxisView::add_existing_redirect_automation_curves);
869
870         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ) {
871
872                 list<RedirectAutomationInfo*>::iterator tmp;
873
874                 tmp = i;
875                 ++tmp;
876
877                 if (!(*i)->valid) {
878
879                         delete *i;
880                         redirect_automation.erase (i);
881
882                 } 
883
884                 i = tmp;
885         }
886
887         /* change in visibility was possible */
888
889         _route->gui_changed ("track_height", this);
890 }
891
892 RedirectAutomationLine *
893 AudioTimeAxisView::find_redirect_automation_curve (boost::shared_ptr<Redirect> redirect, uint32_t what)
894 {
895         RedirectAutomationNode* ran;
896
897         if ((ran = find_redirect_automation_node (redirect, what)) != 0) {
898                 if (ran->view) {
899                         return dynamic_cast<RedirectAutomationLine*> (ran->view->lines.front());
900                 } 
901         }
902
903         return 0;
904 }
905
906 void
907 AudioTimeAxisView::show_all_automation ()
908 {
909         no_redraw = true;
910
911         pan_automation_item->set_active (true);
912         gain_automation_item->set_active (true);
913         
914         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
915                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
916                         if ((*ii)->view == 0) {
917                                 add_redirect_automation_curve ((*i)->redirect, (*ii)->what);
918                         } 
919
920                         (*ii)->menu_item->set_active (true);
921                 }
922         }
923
924         no_redraw = false;
925
926          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
927 }
928
929 void
930 AudioTimeAxisView::show_existing_automation ()
931 {
932         no_redraw = true;
933
934         pan_automation_item->set_active (true);
935         gain_automation_item->set_active (true);
936
937         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
938                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
939                         if ((*ii)->view != 0) {
940                                 (*ii)->menu_item->set_active (true);
941                         }
942                 }
943         }
944
945         no_redraw = false;
946
947          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
948 }
949
950 void
951 AudioTimeAxisView::hide_all_automation ()
952 {
953         no_redraw = true;
954
955         pan_automation_item->set_active (false);
956         gain_automation_item->set_active (false);
957
958         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
959                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
960                         (*ii)->menu_item->set_active (false);
961                 }
962         }
963
964         no_redraw = false;
965          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
966 }
967
968 void
969 AudioTimeAxisView::region_view_added (RegionView* rv)
970 {
971         assert(dynamic_cast<AudioRegionView*>(rv));
972
973         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
974                 AutomationTimeAxisView* atv;
975
976                 if ((atv = dynamic_cast<AutomationTimeAxisView*> (*i)) != 0) {
977                         rv->add_ghost (*atv);
978                 }
979         }
980 }
981
982 void
983 AudioTimeAxisView::add_ghost_to_redirect (RegionView* rv, AutomationTimeAxisView* atv)
984 {
985         rv->add_ghost (*atv);
986 }
987
988 void
989 AudioTimeAxisView::show_all_xfades ()
990 {
991         AudioStreamView* asv = audio_view();
992
993         if (asv) {
994                 asv->show_all_xfades ();
995         }
996 }
997
998 void
999 AudioTimeAxisView::hide_all_xfades ()
1000 {
1001         AudioStreamView* asv = audio_view();
1002         
1003         if (asv) {
1004                 asv->hide_all_xfades ();
1005         }
1006 }
1007
1008 void
1009 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
1010 {
1011         AudioStreamView* asv = audio_view();
1012         AudioRegionView* rv;
1013
1014         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
1015                 asv->hide_xfades_involving (*rv);
1016         }
1017 }
1018
1019 void
1020 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
1021 {
1022         AudioStreamView* asv = audio_view();
1023         AudioRegionView* rv;
1024
1025         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
1026                 asv->reveal_xfades_involving (*rv);
1027         }
1028 }
1029
1030 void
1031 AudioTimeAxisView::route_active_changed ()
1032 {
1033         RouteUI::route_active_changed ();
1034
1035         if (is_audio_track()) {
1036                 if (_route->active()) {
1037                         controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
1038                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
1039                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
1040                 } else {
1041                         controls_ebox.set_name ("AudioTrackControlsBaseInactiveUnselected");
1042                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
1043                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
1044                 }
1045         } else {
1046                 if (_route->active()) {
1047                         controls_ebox.set_name ("BusControlsBaseUnselected");
1048                         controls_base_selected_name = "BusControlsBaseSelected";
1049                         controls_base_unselected_name = "BusControlsBaseUnselected";
1050                 } else {
1051                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
1052                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
1053                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
1054                 }
1055         }
1056 }
1057
1058 XMLNode* 
1059 AudioTimeAxisView::get_child_xml_node (const string & childname)
1060 {
1061         return RouteUI::get_child_xml_node (childname);
1062 }
1063