Large nasty commit in the form of a 5000 line patch chock-full of completely
[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
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27
28 #include <sigc++/bind.h>
29
30 #include <pbd/error.h>
31 #include <pbd/stl_delete.h>
32 #include <pbd/whitespace.h>
33
34 #include <gtkmm2ext/bindable_button.h>
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/selector.h>
37 #include <gtkmm2ext/stop_signal.h>
38 #include <gtkmm2ext/utils.h>
39
40 #include <ardour/audioplaylist.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/insert.h>
43 #include <ardour/ladspa_plugin.h>
44 #include <ardour/location.h>
45 #include <ardour/panner.h>
46 #include <ardour/playlist.h>
47 #include <ardour/session.h>
48 #include <ardour/session_playlist.h>
49 #include <ardour/utils.h>
50
51 #include "ardour_ui.h"
52 #include "audio_time_axis.h"
53 #include "automation_gain_line.h"
54 #include "automation_pan_line.h"
55 #include "automation_time_axis.h"
56 #include "canvas_impl.h"
57 #include "crossfade_view.h"
58 #include "enums.h"
59 #include "gain_automation_time_axis.h"
60 #include "gui_thread.h"
61 #include "keyboard.h"
62 #include "pan_automation_time_axis.h"
63 #include "playlist_selector.h"
64 #include "plugin_selector.h"
65 #include "plugin_ui.h"
66 #include "point_selection.h"
67 #include "prompter.h"
68 #include "public_editor.h"
69 #include "redirect_automation_line.h"
70 #include "redirect_automation_time_axis.h"
71 #include "regionview.h"
72 #include "rgb_macros.h"
73 #include "selection.h"
74 #include "simplerect.h"
75 #include "streamview.h"
76 #include "utils.h"
77
78 #include <ardour/audio_track.h>
79
80 #include "i18n.h"
81
82 using namespace ARDOUR;
83 using namespace PBD;
84 using namespace LADSPA;
85 using namespace Gtk;
86 using namespace Editing;
87
88
89 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session& sess, Route& rt, Canvas& canvas)
90         : AxisView(sess),
91           RouteUI(rt, sess, _("m"), _("s"), _("r")), // mute, solo, and record
92           TimeAxisView(sess,ed,(TimeAxisView*) 0, canvas),
93           parent_canvas (canvas),
94           button_table (3, 3),
95           edit_group_button (_("g")), // group
96           playlist_button (_("p")), 
97           size_button (_("h")), // height
98           automation_button (_("a")),
99           visual_button (_("v"))
100
101 {
102         _has_state = true;
103         subplugin_menu.set_name ("ArdourContextMenu");
104         playlist_menu = 0;
105         playlist_action_menu = 0;
106         automation_action_menu = 0;
107         gain_track = 0;
108         pan_track = 0;
109         view = 0;
110         timestretch_rect = 0;
111         waveform_item = 0;
112         pan_automation_item = 0;
113         gain_automation_item = 0;
114         no_redraw = false;
115
116         view = new StreamView (*this);
117
118         add_gain_automation_child ();
119         add_pan_automation_child ();
120
121         ignore_toggle = false;
122
123         rec_enable_button->set_active (false);
124         mute_button->set_active (false);
125         solo_button->set_active (false);
126         
127         rec_enable_button->set_name ("TrackRecordEnableButton");
128         mute_button->set_name ("TrackMuteButton");
129         solo_button->set_name ("SoloButton");
130         edit_group_button.set_name ("TrackGroupButton");
131         playlist_button.set_name ("TrackPlaylistButton");
132         automation_button.set_name ("TrackAutomationButton");
133         size_button.set_name ("TrackSizeButton");
134         visual_button.set_name ("TrackVisualButton");
135         hide_button.set_name ("TrackRemoveButton");
136
137         hide_button.add (*(manage (new Image (get_xpm("small_x.xpm")))));
138
139         solo_button->signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
140         mute_button->signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
141         rec_enable_button->signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
142         playlist_button.signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
143         automation_button.signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
144         size_button.signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
145         visual_button.signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
146         hide_button.signal_button_press_event().connect (mem_fun (*this, &AudioTimeAxisView::select_me), false);
147
148         solo_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::solo_press), false);
149         solo_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::solo_release), false);
150         mute_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::mute_press), false);
151         mute_button->signal_button_release_event().connect (mem_fun(*this, &RouteUI::mute_release), false);
152         rec_enable_button->signal_button_press_event().connect (mem_fun(*this, &RouteUI::rec_enable_press));
153         edit_group_button.signal_button_release_event().connect (mem_fun(*this, &AudioTimeAxisView::edit_click), false);
154         playlist_button.signal_clicked().connect (mem_fun(*this, &AudioTimeAxisView::playlist_click));
155         automation_button.signal_clicked().connect (mem_fun(*this, &AudioTimeAxisView::automation_click));
156         size_button.signal_button_release_event().connect (mem_fun(*this, &AudioTimeAxisView::size_click), false);
157         visual_button.signal_clicked().connect (mem_fun(*this, &AudioTimeAxisView::visual_click));
158         hide_button.signal_clicked().connect (mem_fun(*this, &AudioTimeAxisView::hide_click));
159
160         if (is_audio_track()) {
161                 controls_table.attach (*rec_enable_button, 5, 6, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
162         }
163         controls_table.attach (*mute_button, 6, 7, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
164         controls_table.attach (*solo_button, 7, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::FILL|Gtk::EXPAND, 0, 0);
165
166         controls_table.attach (edit_group_button, 6, 7, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
167
168         ARDOUR_UI::instance()->tooltips().set_tip(*rec_enable_button, _("Record"));
169         ARDOUR_UI::instance()->tooltips().set_tip(*solo_button,_("Solo"));
170         ARDOUR_UI::instance()->tooltips().set_tip(*mute_button,_("Mute"));
171         ARDOUR_UI::instance()->tooltips().set_tip(edit_group_button,_("Edit Group"));
172         ARDOUR_UI::instance()->tooltips().set_tip(size_button,_("Display Height"));
173         ARDOUR_UI::instance()->tooltips().set_tip(playlist_button,_("Playlist"));
174         ARDOUR_UI::instance()->tooltips().set_tip(automation_button, _("Automation"));
175         ARDOUR_UI::instance()->tooltips().set_tip(visual_button, _("Visual options"));
176         ARDOUR_UI::instance()->tooltips().set_tip(hide_button, _("Hide this track"));
177         
178         label_view ();
179
180         controls_table.attach (hide_button, 0, 1, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
181         controls_table.attach (visual_button, 1, 2, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
182         controls_table.attach (size_button, 2, 3, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
183         controls_table.attach (automation_button, 3, 4, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
184
185         if (is_audio_track() && audio_track()->mode() == ARDOUR::Normal) {
186                 controls_table.attach (playlist_button, 5, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
187         }
188
189         /* remove focus from the buttons */
190         
191         automation_button.unset_flags (Gtk::CAN_FOCUS);
192         solo_button->unset_flags (Gtk::CAN_FOCUS);
193         mute_button->unset_flags (Gtk::CAN_FOCUS);
194         edit_group_button.unset_flags (Gtk::CAN_FOCUS);
195         size_button.unset_flags (Gtk::CAN_FOCUS);
196         playlist_button.unset_flags (Gtk::CAN_FOCUS);
197         hide_button.unset_flags (Gtk::CAN_FOCUS);
198         visual_button.unset_flags (Gtk::CAN_FOCUS);
199
200         /* map current state of the route */
201
202         update_diskstream_display ();
203         solo_changed(0);
204         mute_changed(0);
205         redirects_changed (0);
206         reset_redirect_automation_curves ();
207         y_position = -1;
208
209         ensure_xml_node ();
210
211         set_state (*xml_node);
212         
213         _route.mute_changed.connect (mem_fun(*this, &RouteUI::mute_changed));
214         _route.solo_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
215         _route.redirects_changed.connect (mem_fun(*this, &AudioTimeAxisView::redirects_changed));
216         _route.name_changed.connect (mem_fun(*this, &AudioTimeAxisView::route_name_changed));
217         _route.solo_safe_changed.connect (mem_fun(*this, &RouteUI::solo_changed));
218         _route.panner().Changed.connect (mem_fun(*this, &AudioTimeAxisView::update_pans));
219
220         if (is_audio_track()) {
221
222                 /* track */
223
224                 audio_track()->FreezeChange.connect (mem_fun(*this, &AudioTimeAxisView::map_frozen));
225
226                 audio_track()->diskstream_changed.connect (mem_fun(*this, &AudioTimeAxisView::diskstream_changed));
227                 get_diskstream()->speed_changed.connect (mem_fun(*this, &AudioTimeAxisView::speed_changed));
228
229                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
230                 controls_base_selected_name = "AudioTrackControlsBaseSelected";
231                 controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
232
233                 /* ask for notifications of any new RegionViews */
234
235                 view->AudioRegionViewAdded.connect (mem_fun(*this, &AudioTimeAxisView::region_view_added));
236
237                 view->attach ();
238
239                 /* pick up the correct freeze state */
240
241                 map_frozen ();
242
243         } else {
244
245                 /* bus */
246
247                 controls_ebox.set_name ("BusControlsBaseUnselected");
248                 controls_base_selected_name = "BusControlsBaseSelected";
249                 controls_base_unselected_name = "BusControlsBaseUnselected";
250         }
251
252         editor.ZoomChanged.connect (mem_fun(*this, &AudioTimeAxisView::reset_samples_per_unit));
253         ColorChanged.connect (mem_fun (*this, &AudioTimeAxisView::color_handler));
254 }
255
256 AudioTimeAxisView::~AudioTimeAxisView ()
257 {
258         GoingAway (); /* EMIT_SIGNAL */
259
260         if (playlist_menu) {
261                 delete playlist_menu;
262                 playlist_menu = 0;
263         }
264   
265         if (playlist_action_menu) {
266                 delete playlist_action_menu;
267                 playlist_action_menu = 0;
268         }
269
270         vector_delete (&redirect_automation_curves);
271
272         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
273                 delete *i;
274         }
275
276         if (view) {
277                 delete view;
278                 view = 0;
279         }
280 }
281
282 guint32
283 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
284 {
285         ensure_xml_node ();
286         xml_node->add_property ("shown_editor", "yes");
287                 
288         return TimeAxisView::show_at (y, nth, parent);
289 }
290
291 void
292 AudioTimeAxisView::hide ()
293 {
294         ensure_xml_node ();
295         xml_node->add_property ("shown_editor", "no");
296
297         TimeAxisView::hide ();
298 }
299
300 void
301 AudioTimeAxisView::set_playlist (AudioPlaylist *newplaylist)
302 {
303         AudioPlaylist *pl;
304
305         modified_connection.disconnect ();
306         state_changed_connection.disconnect ();
307         
308         if ((pl = dynamic_cast<AudioPlaylist*> (playlist())) != 0) {
309                 state_changed_connection = pl->StateChanged.connect (mem_fun(*this, &AudioTimeAxisView::playlist_state_changed));
310                 modified_connection = pl->Modified.connect (mem_fun(*this, &AudioTimeAxisView::playlist_modified));
311         }
312 }
313
314 void
315 AudioTimeAxisView::playlist_modified ()
316 {
317 }
318
319 gint
320 AudioTimeAxisView::edit_click (GdkEventButton *ev)
321 {
322         if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
323                 _route.set_edit_group (0, this);
324                 return FALSE;
325         } 
326
327         using namespace Menu_Helpers;
328
329         MenuList& items = edit_group_menu.items ();
330         RadioMenuItem::Group group;
331
332         items.clear ();
333         items.push_back (RadioMenuElem (group, _("No group"), 
334                                         bind (mem_fun(*this, &AudioTimeAxisView::set_edit_group_from_menu), (RouteGroup *) 0)));
335         
336         if (_route.edit_group() == 0) {
337                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
338         }
339         
340         _session.foreach_edit_group (bind (mem_fun (*this, &AudioTimeAxisView::add_edit_group_menu_item), &group));
341         edit_group_menu.popup (ev->button, ev->time);
342
343         return FALSE;
344 }
345
346 void
347 AudioTimeAxisView::add_edit_group_menu_item (RouteGroup *eg, RadioMenuItem::Group* group)
348 {
349         using namespace Menu_Helpers;
350
351         MenuList &items = edit_group_menu.items();
352
353         cerr << "adding edit group called " << eg->name() << endl;
354
355         items.push_back (RadioMenuElem (*group, eg->name(), bind (mem_fun(*this, &AudioTimeAxisView::set_edit_group_from_menu), eg)));
356         if (_route.edit_group() == eg) {
357                 static_cast<RadioMenuItem*>(&items.back())->set_active ();
358         }
359 }
360
361 void
362 AudioTimeAxisView::set_edit_group_from_menu (RouteGroup *eg)
363
364 {
365         _route.set_edit_group (eg, this);
366 }
367
368 void
369 AudioTimeAxisView::playlist_state_changed (Change ignored)
370 {
371         // ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioTimeAxisView::playlist_state_changed), ignored));
372         // why are we here ?
373 }
374
375 void
376 AudioTimeAxisView::playlist_changed ()
377
378 {
379         label_view ();
380
381         if (is_audio_track()) {
382                 set_playlist (dynamic_cast<AudioPlaylist*>(get_diskstream()->playlist()));
383         }
384 }
385
386 void
387 AudioTimeAxisView::label_view ()
388 {
389         string x = _route.name();
390
391         if (x != name_entry.get_text()) {
392                 name_entry.set_text (x);
393         }
394
395         ARDOUR_UI::instance()->tooltips().set_tip (name_entry, x);
396 }
397
398 void
399 AudioTimeAxisView::route_name_changed (void *src)
400 {
401         editor.route_name_changed (this);
402         label_view ();
403 }
404
405 void
406 AudioTimeAxisView::take_name_changed (void *src)
407
408 {
409         if (src != this) {
410                 label_view ();
411         }
412 }
413
414 void
415 AudioTimeAxisView::playlist_click ()
416 {
417         // always build a new action menu
418         
419         if (playlist_action_menu == 0) {
420                 playlist_action_menu = new Menu;
421                 playlist_action_menu->set_name ("ArdourContextMenu");
422         }
423         
424         build_playlist_menu(playlist_action_menu);
425
426         playlist_action_menu->popup (1, 0);
427 }
428
429 void
430 AudioTimeAxisView::automation_click ()
431 {
432         if (automation_action_menu == 0) {
433                 /* this seems odd, but the automation action
434                    menu is built as part of the display menu.
435                 */
436                 build_display_menu ();
437         }
438         automation_action_menu->popup (1, 0);
439 }
440
441 void
442 AudioTimeAxisView::show_timestretch (jack_nframes_t start, jack_nframes_t end)
443 {
444         double x1;
445         double x2;
446         double y2;
447         
448         TimeAxisView::show_timestretch (start, end);
449
450         hide_timestretch ();
451
452 #if 0   
453         if (ts.empty()) {
454                 return;
455         }
456
457
458         /* check that the time selection was made in our route, or our edit group.
459            remember that edit_group() == 0 implies the route is *not* in a edit group.
460         */
461
462         if (!(ts.track == this || (ts.group != 0 && ts.group == _route.edit_group()))) {
463                 /* this doesn't apply to us */
464                 return;
465         }
466
467         /* ignore it if our edit group is not active */
468         
469         if ((ts.track != this) && _route.edit_group() && !_route.edit_group()->is_active()) {
470                 return;
471         }
472 #endif
473
474         if (timestretch_rect == 0) {
475                 timestretch_rect = new SimpleRect (*canvas_display);
476                 timestretch_rect->property_x1() =  0.0;
477                 timestretch_rect->property_y1() =  0.0;
478                 timestretch_rect->property_x2() =  0.0;
479                 timestretch_rect->property_y2() =  0.0;
480                 timestretch_rect->property_fill_color_rgba() =  color_map[cTimeStretchFill];
481                 timestretch_rect->property_outline_color_rgba() = color_map[cTimeStretchOutline];
482         }
483
484         timestretch_rect->show ();
485         timestretch_rect->raise_to_top ();
486
487         x1 = start / editor.get_current_zoom();
488         x2 = (end - 1) / editor.get_current_zoom();
489         y2 = height - 2;
490         
491         timestretch_rect->property_x1() = x1;
492         timestretch_rect->property_y1() = 1.0;
493         timestretch_rect->property_x2() = x2;
494         timestretch_rect->property_y2() = y2;
495 }
496
497 void
498 AudioTimeAxisView::hide_timestretch ()
499 {
500         TimeAxisView::hide_timestretch ();
501
502         if (timestretch_rect) {
503                 timestretch_rect->hide ();
504         }
505 }
506
507 void
508 AudioTimeAxisView::show_selection (TimeSelection& ts)
509 {
510
511 #if 0
512         /* ignore it if our edit group is not active or if the selection was started
513            in some other track or edit group (remember that edit_group() == 0 means
514            that the track is not in an edit group).
515         */
516
517         if (((ts.track != this && !is_child (ts.track)) && _route.edit_group() && !_route.edit_group()->is_active()) ||
518             (!(ts.track == this || is_child (ts.track) || (ts.group != 0 && ts.group == _route.edit_group())))) {
519                 hide_selection ();
520                 return;
521         }
522 #endif
523
524         TimeAxisView::show_selection (ts);
525 }
526
527 void
528 AudioTimeAxisView::set_state (const XMLNode& node)
529 {
530         const XMLProperty *prop;
531         
532         TimeAxisView::set_state (node);
533         
534         if ((prop = node.property ("shown_editor")) != 0) {
535                 if (prop->value() == "no") {
536                         _marked_for_display = false;
537                 } else {
538                         _marked_for_display = true;
539                 }
540         } else {
541                 _marked_for_display = true;
542         }
543         
544         XMLNodeList nlist = node.children();
545         XMLNodeConstIterator niter;
546         XMLNode *child_node;
547         
548         
549         show_gain_automation = false;
550         show_pan_automation  = false;
551         
552         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
553                 child_node = *niter;
554
555                 if (child_node->name() == "gain") {
556                         XMLProperty *prop=child_node->property ("shown");
557                         
558                         if (prop != 0) {
559                                 if (prop->value() == "yes") {
560                                         show_gain_automation = true;
561                                 }
562                         }
563                         continue;
564                 }
565                 
566                 if (child_node->name() == "pan") {
567                         XMLProperty *prop=child_node->property ("shown");
568                         
569                         if (prop != 0) {
570                                 if (prop->value() == "yes") {
571                                         show_pan_automation = true;
572                                 }                       
573                         }
574                         continue;
575                 }
576         }
577 }
578
579 void
580 AudioTimeAxisView::set_height (TrackHeight h)
581 {
582         bool height_changed = (height == 0) || (h != height_style);
583
584         TimeAxisView::set_height (h);
585
586         ensure_xml_node ();
587
588         view->set_height ((double) height);
589
590         switch (height_style) {
591         case Largest:
592                 xml_node->add_property ("track_height", "largest");
593                 show_name_entry ();
594                 hide_name_label ();
595                 controls_table.show_all();
596                 break;
597         case Large:
598                 xml_node->add_property ("track_height", "large");
599                 show_name_entry ();
600                 hide_name_label ();
601                 controls_table.show_all();
602                 break;
603         case Larger:
604                 xml_node->add_property ("track_height", "larger");
605                 show_name_entry ();
606                 hide_name_label ();
607                 controls_table.show_all();
608                 break;
609         case Normal:
610                 xml_node->add_property ("track_height", "normal");
611                 show_name_entry ();
612                 hide_name_label ();
613                 controls_table.show_all();
614                 break;
615         case Smaller:
616                 xml_node->add_property ("track_height", "smaller");
617                 controls_table.show_all ();
618                 show_name_entry ();
619                 hide_name_label ();
620                 edit_group_button.hide ();
621                 hide_button.hide ();
622                 visual_button.hide ();
623                 size_button.hide ();
624                 automation_button.hide ();
625                 playlist_button.hide ();
626                 break;
627         case Small:
628                 xml_node->add_property ("track_height", "small");
629                 controls_table.hide_all ();
630                 controls_table.show ();
631                 hide_name_entry ();
632                 show_name_label ();
633                 name_label.set_text (_route.name());
634                 break;
635         }
636
637         if (height_changed) {
638                 /* only emit the signal if the height really changed */
639                  _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
640         }
641 }
642
643 void
644 AudioTimeAxisView::select_track_color ()
645 {
646         if (RouteUI::choose_color ()) {
647
648                 if (view) {
649                         view->apply_color (_color, StreamView::RegionColor);
650                 }
651         }
652 }
653
654 void
655 AudioTimeAxisView::reset_redirect_automation_curves ()
656 {
657         for (vector<RedirectAutomationLine*>::iterator i = redirect_automation_curves.begin(); i != redirect_automation_curves.end(); ++i) {
658                 (*i)->reset();
659         }
660 }
661
662 void
663 AudioTimeAxisView::reset_samples_per_unit ()
664 {
665         set_samples_per_unit (editor.get_current_zoom());
666 }
667
668 void
669 AudioTimeAxisView::set_samples_per_unit (double spu)
670 {
671         double speed = 1.0;
672
673         if (get_diskstream() != 0) {
674                 speed = get_diskstream()->speed();
675         }
676         
677         if (view) {
678                 view->set_samples_per_unit (spu * speed);
679         }
680
681         TimeAxisView::set_samples_per_unit (spu * speed);
682 }
683
684 void
685 AudioTimeAxisView::build_display_menu ()
686 {
687         using namespace Menu_Helpers;
688
689         /* get the size menu ready */
690
691         build_size_menu ();
692
693         /* prepare it */
694
695         TimeAxisView::build_display_menu ();
696
697         /* now fill it with our stuff */
698
699         MenuList& items = display_menu->items();
700         display_menu->set_name ("ArdourContextMenu");
701         
702         items.push_back (MenuElem (_("Height"), *size_menu));
703         items.push_back (MenuElem (_("Color"), mem_fun(*this, &AudioTimeAxisView::select_track_color)));
704
705
706         items.push_back (SeparatorElem());
707         items.push_back (MenuElem (_("Hide all crossfades"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
708         items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
709         items.push_back (SeparatorElem());
710
711         build_remote_control_menu ();
712         items.push_back (MenuElem (_("Remote Control ID"), *remote_control_menu));
713
714         automation_action_menu = manage (new Menu);
715         MenuList& automation_items = automation_action_menu->items();
716         automation_action_menu->set_name ("ArdourContextMenu");
717         
718         automation_items.push_back (MenuElem (_("Show all automation"),
719                                               mem_fun(*this, &AudioTimeAxisView::show_all_automation)));
720
721         automation_items.push_back (MenuElem (_("Show existing automation"),
722                                               mem_fun(*this, &AudioTimeAxisView::show_existing_automation)));
723
724         automation_items.push_back (MenuElem (_("Hide all automation"),
725                                               mem_fun(*this, &AudioTimeAxisView::hide_all_automation)));
726
727         automation_items.push_back (SeparatorElem());
728
729         automation_items.push_back (CheckMenuElem (_("Fader"), 
730                                                    mem_fun(*this, &AudioTimeAxisView::toggle_gain_track)));
731         gain_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
732         gain_automation_item->set_active(show_gain_automation);
733
734         automation_items.push_back (CheckMenuElem (_("Pan"),
735                                                    mem_fun(*this, &AudioTimeAxisView::toggle_pan_track)));
736         pan_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
737         pan_automation_item->set_active(show_pan_automation);
738
739         automation_items.push_back (MenuElem (_("Plugins"), subplugin_menu));
740
741         items.push_back (MenuElem (_("Automation"), *automation_action_menu));
742
743         Menu *waveform_menu = manage(new Menu);
744         MenuList& waveform_items = waveform_menu->items();
745         waveform_menu->set_name ("ArdourContextMenu");
746         
747         waveform_items.push_back (CheckMenuElem (_("Show waveforms"), mem_fun(*this, &AudioTimeAxisView::toggle_waveforms)));
748         waveform_item = static_cast<CheckMenuItem *> (&waveform_items.back());
749         ignore_toggle = true;
750         waveform_item->set_active (editor.show_waveforms());
751         ignore_toggle = false;
752
753         RadioMenuItem::Group group;
754
755         waveform_items.push_back (RadioMenuElem (group, _("Traditional"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Traditional)));
756         traditional_item = static_cast<RadioMenuItem *> (&waveform_items.back());
757
758         waveform_items.push_back (RadioMenuElem (group, _("Rectified"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Rectified)));
759         rectified_item = static_cast<RadioMenuItem *> (&waveform_items.back());
760
761         items.push_back (MenuElem (_("Waveform"), *waveform_menu));
762
763         if (is_audio_track()) {
764
765                 Menu* alignment_menu = manage (new Menu);
766                 MenuList& alignment_items = alignment_menu->items();
767                 alignment_menu->set_name ("ArdourContextMenu");
768
769                 RadioMenuItem::Group align_group;
770                 
771                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with existing material"), bind (mem_fun(*this, &AudioTimeAxisView::set_align_style), ExistingMaterial)));
772                 align_existing_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
773                 if (get_diskstream()->alignment_style() == ExistingMaterial) {
774                         align_existing_item->set_active();
775                 }
776                 alignment_items.push_back (RadioMenuElem (align_group, _("Align with capture time"), bind (mem_fun(*this, &AudioTimeAxisView::set_align_style), CaptureTime)));
777                 align_capture_item = dynamic_cast<RadioMenuItem*>(&alignment_items.back());
778                 if (get_diskstream()->alignment_style() == CaptureTime) {
779                         align_capture_item->set_active();
780                 }
781                 
782                 items.push_back (MenuElem (_("Alignment"), *alignment_menu));
783
784                 get_diskstream()->AlignmentStyleChanged.connect (mem_fun(*this, &AudioTimeAxisView::align_style_changed));
785         }
786
787         items.push_back (SeparatorElem());
788         items.push_back (CheckMenuElem (_("Active"), mem_fun(*this, &RouteUI::toggle_route_active)));
789         route_active_menu_item = dynamic_cast<CheckMenuItem *> (&items.back());
790         route_active_menu_item->set_active (_route.active());
791
792         items.push_back (SeparatorElem());
793         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &RouteUI::remove_this_route)));
794
795 }
796
797 void
798 AudioTimeAxisView::align_style_changed ()
799 {
800         switch (get_diskstream()->alignment_style()) {
801         case ExistingMaterial:
802                 if (!align_existing_item->get_active()) {
803                         align_existing_item->set_active();
804                 }
805                 break;
806         case CaptureTime:
807                 if (!align_capture_item->get_active()) {
808                         align_capture_item->set_active();
809                 }
810                 break;
811         }
812 }
813
814 void
815 AudioTimeAxisView::set_align_style (AlignStyle style)
816 {
817         get_diskstream()->set_align_style (style);
818 }
819
820 void
821 AudioTimeAxisView::rename_current_playlist ()
822 {
823         ArdourPrompter prompter (true);
824         string name;
825
826         AudioPlaylist *pl;
827         AudioDiskstream *ds;
828
829         if (((ds = get_diskstream()) == 0) || ds->destructive()
830                         || ((pl = dynamic_cast<AudioPlaylist*>(ds->playlist())) == 0)) {
831                 return;
832         }
833
834         prompter.set_prompt (_("Name for playlist"));
835         prompter.set_initial_text (pl->name());
836         prompter.add_button (_("Rename"), Gtk::RESPONSE_ACCEPT);
837         prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
838
839         switch (prompter.run ()) {
840         case Gtk::RESPONSE_ACCEPT:
841                 prompter.get_result (name);
842                 if (name.length()) {
843                         pl->set_name (name);
844                 }
845                 break;
846
847         default:
848                 break;
849         }
850 }
851
852 void
853 AudioTimeAxisView::use_copy_playlist (bool prompt)
854 {
855         AudioPlaylist *pl;
856         AudioDiskstream *ds;
857         string name;
858
859         if (((ds = get_diskstream()) == 0) || ds->destructive()
860                         || ((pl = dynamic_cast<AudioPlaylist*>(ds->playlist())) == 0)) {
861                 return;
862         }
863         
864         name = Playlist::bump_name (pl->name(), _session);
865
866         if (prompt) {
867
868                 ArdourPrompter prompter (true);
869                 
870                 prompter.set_prompt (_("Name for Playlist"));
871                 prompter.set_initial_text (name);
872                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
873                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
874                 prompter.show_all ();
875                 
876                 switch (prompter.run ()) {
877                 case Gtk::RESPONSE_ACCEPT:
878                         prompter.get_result (name);
879                         break;
880                         
881                 default:
882                         return;
883                 }
884         }
885
886         if (name.length()) {
887                 ds->use_copy_playlist ();
888                 pl = dynamic_cast<AudioPlaylist*>(ds->playlist());
889                 pl->set_name (name);
890         }
891 }
892
893 void
894 AudioTimeAxisView::use_new_playlist (bool prompt)
895 {
896         AudioPlaylist *pl;
897         AudioDiskstream *ds;
898         string name;
899
900         if (((ds = get_diskstream()) == 0) || ds->destructive()
901                         || ((pl = dynamic_cast<AudioPlaylist*>(ds->playlist())) == 0)) {
902                 return;
903         }
904         
905         name = Playlist::bump_name (pl->name(), _session);
906
907         if (prompt) {
908                 
909                 ArdourPrompter prompter (true);
910                 
911                 prompter.set_prompt (_("Name for Playlist"));
912                 prompter.set_initial_text (name);
913                 prompter.add_button (Gtk::Stock::NEW, Gtk::RESPONSE_ACCEPT);
914                 prompter.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
915                 
916                 switch (prompter.run ()) {
917                 case Gtk::RESPONSE_ACCEPT:
918                         prompter.get_result (name);
919                         break;
920                         
921                 default:
922                         return;
923                 }
924         }
925
926         if (name.length()) {
927                 ds->use_new_playlist ();
928                 pl = dynamic_cast<AudioPlaylist*>(ds->playlist());
929                 pl->set_name (name);
930         }
931 }
932
933 void
934 AudioTimeAxisView::clear_playlist ()
935 {
936         AudioPlaylist *pl;
937         AudioDiskstream *ds;
938         
939         if ((ds = get_diskstream()) != 0) {
940                 if ((pl = dynamic_cast<AudioPlaylist*>(ds->playlist())) != 0) {
941                         editor.clear_playlist (*pl);
942                 }
943         }
944 }
945
946 void
947 AudioTimeAxisView::toggle_waveforms ()
948 {
949         if (view && waveform_item && !ignore_toggle) {
950                 view->set_show_waveforms (waveform_item->get_active());
951         }
952 }
953
954 void
955 AudioTimeAxisView::set_show_waveforms (bool yn)
956 {
957         if (waveform_item) {
958                 waveform_item->set_active (yn);
959         } else {
960                 view->set_show_waveforms (yn);
961         }
962 }
963
964 void
965 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
966 {
967         if (view) {
968                 view->set_show_waveforms_recording (yn);
969         }
970 }
971
972 void
973 AudioTimeAxisView::set_waveform_shape (WaveformShape shape)
974 {
975         if (view) {
976                 view->set_waveform_shape (shape);
977         }
978 }
979
980 void
981 AudioTimeAxisView::speed_changed ()
982 {
983         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &AudioTimeAxisView::reset_samples_per_unit));
984 }
985
986 void
987 AudioTimeAxisView::diskstream_changed (void *src)
988 {
989         Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &AudioTimeAxisView::update_diskstream_display));
990 }       
991
992 void
993 AudioTimeAxisView::update_diskstream_display ()
994 {
995         AudioDiskstream *ds;
996
997         if ((ds = get_diskstream()) != 0) {
998                 set_playlist (dynamic_cast<AudioPlaylist*> (ds->playlist ()));
999         }
1000
1001         map_frozen ();
1002 }       
1003
1004 void
1005 AudioTimeAxisView::selection_click (GdkEventButton* ev)
1006 {
1007         PublicEditor::TrackViewList* tracks = editor.get_valid_views (this, _route.edit_group());
1008
1009         switch (Keyboard::selection_type (ev->state)) {
1010         case Selection::Toggle:
1011                 /* XXX this is not right */
1012                 editor.get_selection().add (*tracks);
1013                 break;
1014                 
1015         case Selection::Set:
1016                 editor.get_selection().set (*tracks);
1017                 break;
1018
1019         case Selection::Extend:
1020                 /* not defined yet */
1021                 break;
1022         }
1023
1024         delete tracks;
1025 }
1026
1027 void
1028 AudioTimeAxisView::set_selected_regionviews (AudioRegionSelection& regions)
1029 {
1030         if (view) {
1031                 view->set_selected_regionviews (regions);
1032         }
1033 }
1034
1035 void
1036 AudioTimeAxisView::set_selected_points (PointSelection& points)
1037 {
1038         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1039                 (*i)->set_selected_points (points);
1040         }
1041 }
1042
1043 void
1044 AudioTimeAxisView::get_selectables (jack_nframes_t start, jack_nframes_t end, double top, double bot, list<Selectable*>& results)
1045 {
1046         double speed = 1.0;
1047         
1048         if (get_diskstream() != 0) {
1049                 speed = get_diskstream()->speed();
1050         }
1051         
1052         jack_nframes_t start_adjusted = session_frame_to_track_frame(start, speed);
1053         jack_nframes_t end_adjusted   = session_frame_to_track_frame(end, speed);
1054
1055         if (view && ((top < 0.0 && bot < 0.0)) || touched (top, bot)) {
1056                 view->get_selectables (start_adjusted, end_adjusted, results);
1057         }
1058
1059         /* pick up visible automation tracks */
1060         
1061         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1062                 if (!(*i)->hidden()) {
1063                         (*i)->get_selectables (start_adjusted, end_adjusted, top, bot, results);
1064                 }
1065         }
1066 }
1067
1068 void
1069 AudioTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
1070 {
1071         if (view) {
1072                 view->get_inverted_selectables (sel, results);
1073         }
1074
1075         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1076                 if (!(*i)->hidden()) {
1077                         (*i)->get_inverted_selectables (sel, results);
1078                 }
1079         }
1080
1081         return;
1082 }
1083
1084 RouteGroup*
1085 AudioTimeAxisView::edit_group() const
1086 {
1087         return _route.edit_group();
1088 }
1089
1090 string
1091 AudioTimeAxisView::name() const
1092 {
1093         return _route.name();
1094 }
1095
1096 Playlist *
1097 AudioTimeAxisView::playlist () const 
1098 {
1099         AudioDiskstream *ds;
1100
1101         if ((ds = get_diskstream()) != 0) {
1102                 return ds->playlist(); 
1103         } else {
1104                 return 0; 
1105         }
1106 }
1107
1108 void
1109 AudioTimeAxisView::name_entry_changed ()
1110 {
1111         string x;
1112
1113         x = name_entry.get_text ();
1114         
1115         if (x == _route.name()) {
1116                 return;
1117         }
1118
1119         if (x.length() == 0) {
1120                 name_entry.set_text (_route.name());
1121                 return;
1122         }
1123
1124         strip_whitespace_edges(x);
1125
1126         if (_session.route_name_unique (x)) {
1127                 _route.set_name (x, this);
1128         } else {
1129                 ARDOUR_UI::instance()->popup_error (_("a track already exists with that name"));
1130                 name_entry.set_text (_route.name());
1131         }
1132 }
1133
1134 void
1135 AudioTimeAxisView::visual_click ()
1136 {
1137         popup_display_menu (0);
1138 }
1139
1140 void
1141 AudioTimeAxisView::hide_click ()
1142 {
1143         editor.hide_track_in_display (*this);
1144 }
1145
1146 Region*
1147 AudioTimeAxisView::find_next_region (jack_nframes_t pos, RegionPoint point, int32_t dir)
1148 {
1149         AudioDiskstream *stream;
1150         Playlist *playlist;
1151
1152         if ((stream = get_diskstream()) != 0 && (playlist = stream->playlist()) != 0) {
1153                 return playlist->find_next_region (pos, point, dir);
1154         }
1155
1156         return 0;
1157 }
1158
1159 void
1160 AudioTimeAxisView::add_gain_automation_child ()
1161 {
1162         XMLProperty* prop;
1163         AutomationLine* line;
1164
1165         gain_track = new GainAutomationTimeAxisView (_session,
1166                                                      _route,
1167                                                      editor,
1168                                                      *this,
1169                                                      parent_canvas,
1170                                                      _("gain"),
1171                                                      _route.gain_automation_curve());
1172         
1173         line = new AutomationGainLine ("automation gain",
1174                                        _session,
1175                                        *gain_track,
1176                                        *gain_track->canvas_display,
1177                                        _route.gain_automation_curve());
1178
1179         line->set_line_color (color_map[cAutomationLine]);
1180         
1181
1182         gain_track->add_line (*line);
1183
1184         add_child (gain_track);
1185
1186         gain_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::gain_hidden));
1187
1188         bool hideit = true;
1189         
1190         XMLNode* node;
1191
1192         if ((node = gain_track->get_state_node()) != 0) {
1193                 if  ((prop = node->property ("shown")) != 0) {
1194                         if (prop->value() == "yes") {
1195                                 hideit = false;
1196                         }
1197                 } 
1198         }
1199
1200         if (hideit) {
1201                 gain_track->hide ();
1202         }
1203 }
1204
1205 void
1206 AudioTimeAxisView::add_pan_automation_child ()
1207 {
1208         XMLProperty* prop;
1209
1210         pan_track = new PanAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, _("pan"));
1211
1212         update_pans ();
1213         
1214         add_child (pan_track);
1215
1216         pan_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::pan_hidden));
1217
1218         ensure_xml_node ();
1219         bool hideit = true;
1220         
1221         XMLNode* node;
1222
1223         if ((node = pan_track->get_state_node()) != 0) {
1224                 if ((prop = node->property ("shown")) != 0) {
1225                         if (prop->value() == "yes") {
1226                                 hideit = false;
1227                         }
1228                 } 
1229         }
1230
1231         if (hideit) {
1232                 pan_track->hide ();
1233         }
1234 }
1235
1236 void
1237 AudioTimeAxisView::update_pans ()
1238 {
1239         Panner::iterator p;
1240         
1241         pan_track->clear_lines ();
1242         
1243         /* we don't draw lines for "greater than stereo" panning.
1244          */
1245
1246         if (_route.n_outputs() > 2) {
1247                 return;
1248         }
1249
1250         for (p = _route.panner().begin(); p != _route.panner().end(); ++p) {
1251
1252                 AutomationLine* line;
1253
1254                 line = new AutomationPanLine ("automation pan", _session, *pan_track,
1255                                               *pan_track->canvas_display, 
1256                                               (*p)->automation());
1257
1258                 if (p == _route.panner().begin()) {
1259                         /* first line is a nice orange */
1260                         line->set_line_color (color_map[cLeftPanAutomationLine]);
1261                 } else {
1262                         /* second line is a nice blue */
1263                         line->set_line_color (color_map[cRightPanAutomationLine]);
1264                 }
1265
1266                 pan_track->add_line (*line);
1267         }
1268 }
1269                 
1270 void
1271 AudioTimeAxisView::toggle_gain_track ()
1272 {
1273
1274         bool showit = gain_automation_item->get_active();
1275
1276         if (showit != gain_track->marked_for_display()) {
1277                 if (showit) {
1278                         gain_track->set_marked_for_display (true);
1279                         gain_track->canvas_display->show();
1280                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
1281                 } else {
1282                         gain_track->set_marked_for_display (false);
1283                         gain_track->hide ();
1284                         gain_track->get_state_node()->add_property ("shown", X_("no"));
1285                 }
1286
1287                 /* now trigger a redisplay */
1288                 
1289                 if (!no_redraw) {
1290                          _route.gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
1291                 }
1292         }
1293 }
1294
1295 void
1296 AudioTimeAxisView::gain_hidden ()
1297 {
1298         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
1299
1300         if (gain_automation_item && !_hidden) {
1301                 gain_automation_item->set_active (false);
1302         }
1303
1304          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1305 }
1306
1307 void
1308 AudioTimeAxisView::toggle_pan_track ()
1309 {
1310         bool showit = pan_automation_item->get_active();
1311
1312         if (showit != pan_track->marked_for_display()) {
1313                 if (showit) {
1314                         pan_track->set_marked_for_display (true);
1315                         pan_track->canvas_display->show();
1316                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
1317                 } else {
1318                         pan_track->set_marked_for_display (false);
1319                         pan_track->hide ();
1320                         pan_track->get_state_node()->add_property ("shown", X_("no"));
1321                 }
1322
1323                 /* now trigger a redisplay */
1324                 
1325                 if (!no_redraw) {
1326                          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1327                 }
1328         }
1329 }
1330
1331 void
1332 AudioTimeAxisView::pan_hidden ()
1333 {
1334         pan_track->get_state_node()->add_property ("shown", "no");
1335
1336         if (pan_automation_item && !_hidden) {
1337                 pan_automation_item->set_active (false);
1338         }
1339
1340          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1341 }
1342
1343 AudioTimeAxisView::RedirectAutomationInfo::~RedirectAutomationInfo ()
1344 {
1345         for (vector<RedirectAutomationNode*>::iterator i = lines.begin(); i != lines.end(); ++i) {
1346                 delete *i;
1347         }
1348 }
1349
1350
1351 AudioTimeAxisView::RedirectAutomationNode::~RedirectAutomationNode ()
1352 {
1353         parent.remove_ran (this);
1354
1355         if (view) {
1356                 delete view;
1357         }
1358 }
1359
1360 void
1361 AudioTimeAxisView::remove_ran (RedirectAutomationNode* ran)
1362 {
1363         if (ran->view) {
1364                 remove_child (ran->view);
1365         }
1366 }
1367
1368 AudioTimeAxisView::RedirectAutomationNode*
1369 AudioTimeAxisView::find_redirect_automation_node (Redirect *redirect, uint32_t what)
1370 {
1371         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1372
1373                 if ((*i)->redirect == redirect) {
1374
1375                         for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1376                                 if ((*ii)->what == what) {
1377                                         return *ii;
1378                                 }
1379                         }
1380                 }
1381         }
1382
1383         return 0;
1384 }
1385
1386 static string 
1387 legalize_for_xml_node (string str)
1388 {
1389         string::size_type pos;
1390         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=:";
1391         string legal;
1392
1393         legal = str;
1394         pos = 0;
1395
1396         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
1397                 legal.replace (pos, 1, "_");
1398                 pos += 1;
1399         }
1400
1401         return legal;
1402 }
1403
1404
1405 void
1406 AudioTimeAxisView::add_redirect_automation_curve (Redirect *redirect, uint32_t what)
1407 {
1408         RedirectAutomationLine* ral;
1409         string name;
1410         RedirectAutomationNode* ran;
1411
1412         if ((ran = find_redirect_automation_node (redirect, what)) == 0) {
1413                 fatal << _("programming error: ")
1414                       << string_compose (X_("redirect automation curve for %1:%2 not registered with audio track!"),
1415                                   redirect->name(), what)
1416                       << endmsg;
1417                 /*NOTREACHED*/
1418                 return;
1419         }
1420
1421         if (ran->view) {
1422                 return;
1423         }
1424
1425         name = redirect->describe_parameter (what);
1426
1427         /* create a string that is a legal XML node name that can be used to refer to this redirect+port combination */
1428
1429         char state_name[256];
1430         snprintf (state_name, sizeof (state_name), "Redirect-%s-%" PRIu32, legalize_for_xml_node (redirect->name()).c_str(), what);
1431
1432         ran->view = new RedirectAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, name, what, *redirect, state_name);
1433
1434         ral = new RedirectAutomationLine (name, 
1435                                           *redirect, what, _session, *ran->view,
1436                                           *ran->view->canvas_display, redirect->automation_list (what));
1437         
1438         ral->set_line_color (color_map[cRedirectAutomationLine]);
1439         ral->queue_reset ();
1440
1441         ran->view->add_line (*ral);
1442
1443         ran->view->Hiding.connect (bind (mem_fun(*this, &AudioTimeAxisView::redirect_automation_track_hidden), ran, redirect));
1444
1445         if (!ran->view->marked_for_display()) {
1446                 ran->view->hide ();
1447         } else {
1448                 ran->menu_item->set_active (true);
1449         }
1450
1451         add_child (ran->view);
1452
1453         view->foreach_regionview (bind (mem_fun(*this, &AudioTimeAxisView::add_ghost_to_redirect), ran->view));
1454
1455         redirect->mark_automation_visible (what, true);
1456 }
1457
1458 void
1459 AudioTimeAxisView::redirect_automation_track_hidden (AudioTimeAxisView::RedirectAutomationNode* ran, Redirect* r)
1460 {
1461         if (!_hidden) {
1462                 ran->menu_item->set_active (false);
1463         }
1464
1465         r->mark_automation_visible (ran->what, false);
1466
1467          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1468 }
1469
1470 void
1471 AudioTimeAxisView::add_existing_redirect_automation_curves (Redirect *redirect)
1472 {
1473         set<uint32_t> s;
1474         RedirectAutomationLine *ral;
1475
1476         redirect->what_has_visible_automation (s);
1477
1478         for (set<uint32_t>::iterator i = s.begin(); i != s.end(); ++i) {
1479                 
1480                 if ((ral = find_redirect_automation_curve (redirect, *i)) != 0) {
1481                         ral->queue_reset ();
1482                 } else {
1483                         add_redirect_automation_curve (redirect, (*i));
1484                 }
1485         }
1486 }
1487
1488 void
1489 AudioTimeAxisView::add_redirect_to_subplugin_menu (Redirect* r)
1490 {
1491         using namespace Menu_Helpers;
1492         RedirectAutomationInfo *rai;
1493         list<RedirectAutomationInfo*>::iterator x;
1494         
1495         const std::set<uint32_t>& automatable = r->what_can_be_automated ();
1496         std::set<uint32_t> has_visible_automation;
1497
1498         r->what_has_visible_automation(has_visible_automation);
1499
1500         if (automatable.empty()) {
1501                 return;
1502         }
1503
1504         for (x = redirect_automation.begin(); x != redirect_automation.end(); ++x) {
1505                 if ((*x)->redirect == r) {
1506                         break;
1507                 }
1508         }
1509
1510         if (x == redirect_automation.end()) {
1511
1512                 rai = new RedirectAutomationInfo (r);
1513                 redirect_automation.push_back (rai);
1514
1515         } else {
1516
1517                 rai = *x;
1518
1519         }
1520
1521         /* any older menu was deleted at the top of redirects_changed()
1522            when we cleared the subplugin menu.
1523         */
1524
1525         rai->menu = manage (new Menu);
1526         MenuList& items = rai->menu->items();
1527         rai->menu->set_name ("ArdourContextMenu");
1528
1529         items.clear ();
1530
1531         for (std::set<uint32_t>::const_iterator i = automatable.begin(); i != automatable.end(); ++i) {
1532
1533                 RedirectAutomationNode* ran;
1534                 CheckMenuItem* mitem;
1535                 
1536                 string name = r->describe_parameter (*i);
1537                 
1538                 items.push_back (CheckMenuElem (name));
1539                 mitem = dynamic_cast<CheckMenuItem*> (&items.back());
1540
1541                 if (has_visible_automation.find((*i)) != has_visible_automation.end()) {
1542                         mitem->set_active(true);
1543                 }
1544
1545                 if ((ran = find_redirect_automation_node (r, *i)) == 0) {
1546
1547                         /* new item */
1548                         
1549                         ran = new RedirectAutomationNode (*i, mitem, *this);
1550                         
1551                         rai->lines.push_back (ran);
1552
1553                 } else {
1554
1555                         ran->menu_item = mitem;
1556
1557                 }
1558
1559                 mitem->signal_toggled().connect (bind (mem_fun(*this, &AudioTimeAxisView::redirect_menu_item_toggled), rai, ran));
1560         }
1561
1562         /* add the menu for this redirect, because the subplugin
1563            menu is always cleared at the top of redirects_changed().
1564            this is the result of some poor design in gtkmm and/or
1565            GTK+.
1566         */
1567
1568         subplugin_menu.items().push_back (MenuElem (r->name(), *rai->menu));
1569         rai->valid = true;
1570 }
1571
1572 void
1573 AudioTimeAxisView::redirect_menu_item_toggled (AudioTimeAxisView::RedirectAutomationInfo* rai,
1574                                                AudioTimeAxisView::RedirectAutomationNode* ran)
1575 {
1576         bool showit = ran->menu_item->get_active();
1577         bool redraw = false;
1578
1579         if (ran->view == 0 && showit) {
1580                 add_redirect_automation_curve (rai->redirect, ran->what);
1581                 redraw = true;
1582         }
1583
1584         if (showit != ran->view->marked_for_display()) {
1585
1586                 if (showit) {
1587                         ran->view->set_marked_for_display (true);
1588                         ran->view->canvas_display->show();
1589                 } else {
1590                         rai->redirect->mark_automation_visible (ran->what, true);
1591                         ran->view->set_marked_for_display (false);
1592                         ran->view->hide ();
1593                 }
1594
1595                 redraw = true;
1596
1597         }
1598
1599         if (redraw && !no_redraw) {
1600
1601                 /* now trigger a redisplay */
1602                 
1603                  _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1604
1605         }
1606 }
1607
1608 void
1609 AudioTimeAxisView::redirects_changed (void *src)
1610 {
1611         using namespace Menu_Helpers;
1612
1613         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1614                 (*i)->valid = false;
1615         }
1616
1617         subplugin_menu.items().clear ();
1618
1619         _route.foreach_redirect (this, &AudioTimeAxisView::add_redirect_to_subplugin_menu);
1620         _route.foreach_redirect (this, &AudioTimeAxisView::add_existing_redirect_automation_curves);
1621
1622         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ) {
1623
1624                 list<RedirectAutomationInfo*>::iterator tmp;
1625
1626                 tmp = i;
1627                 ++tmp;
1628
1629                 if (!(*i)->valid) {
1630
1631                         delete *i;
1632                         redirect_automation.erase (i);
1633
1634                 } 
1635
1636                 i = tmp;
1637         }
1638
1639         /* change in visibility was possible */
1640
1641         _route.gui_changed ("track_height", this);
1642 }
1643
1644 RedirectAutomationLine *
1645 AudioTimeAxisView::find_redirect_automation_curve (Redirect *redirect, uint32_t what)
1646 {
1647         RedirectAutomationNode* ran;
1648
1649         if ((ran = find_redirect_automation_node (redirect, what)) != 0) {
1650                 if (ran->view) {
1651                         return dynamic_cast<RedirectAutomationLine*> (ran->view->lines.front());
1652                 } 
1653         }
1654
1655         return 0;
1656 }
1657
1658 void
1659 AudioTimeAxisView::show_all_automation ()
1660 {
1661         no_redraw = true;
1662
1663         pan_automation_item->set_active (true);
1664         gain_automation_item->set_active (true);
1665         
1666         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1667                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1668                         if ((*ii)->view == 0) {
1669                                 add_redirect_automation_curve ((*i)->redirect, (*ii)->what);
1670                         } 
1671
1672                         (*ii)->menu_item->set_active (true);
1673                 }
1674         }
1675
1676         no_redraw = false;
1677
1678          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1679 }
1680
1681 void
1682 AudioTimeAxisView::show_existing_automation ()
1683 {
1684         no_redraw = true;
1685
1686         pan_automation_item->set_active (true);
1687         gain_automation_item->set_active (true);
1688
1689         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1690                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1691                         if ((*ii)->view != 0) {
1692                                 (*ii)->menu_item->set_active (true);
1693                         }
1694                 }
1695         }
1696
1697         no_redraw = false;
1698
1699          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1700 }
1701
1702 void
1703 AudioTimeAxisView::hide_all_automation ()
1704 {
1705         no_redraw = true;
1706
1707         pan_automation_item->set_active (false);
1708         gain_automation_item->set_active (false);
1709
1710         for (list<RedirectAutomationInfo*>::iterator i = redirect_automation.begin(); i != redirect_automation.end(); ++i) {
1711                 for (vector<RedirectAutomationNode*>::iterator ii = (*i)->lines.begin(); ii != (*i)->lines.end(); ++ii) {
1712                         (*ii)->menu_item->set_active (false);
1713                 }
1714         }
1715
1716         no_redraw = false;
1717          _route.gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
1718 }
1719
1720 bool
1721 AudioTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
1722 {
1723         Playlist* what_we_got;
1724         AudioDiskstream* ds = get_diskstream();
1725         Playlist* playlist;
1726         bool ret = false;
1727
1728         if (ds == 0) {
1729                 /* route is a bus, not a track */
1730                 return false;
1731         }
1732
1733         playlist = ds->playlist();
1734
1735
1736         TimeSelection time (selection.time);
1737         float speed = ds->speed();
1738         if (speed != 1.0f) {
1739                 for (TimeSelection::iterator i = time.begin(); i != time.end(); ++i) {
1740                         (*i).start = session_frame_to_track_frame((*i).start, speed);
1741                         (*i).end   = session_frame_to_track_frame((*i).end,   speed);
1742                 }
1743         }
1744         
1745         switch (op) {
1746         case Cut:
1747                 _session.add_undo (playlist->get_memento());
1748                 if ((what_we_got = playlist->cut (time)) != 0) {
1749                         editor.get_cut_buffer().add (what_we_got);
1750                         _session.add_redo_no_execute (playlist->get_memento());
1751                         ret = true;
1752                 }
1753                 break;
1754         case Copy:
1755                 if ((what_we_got = playlist->copy (time)) != 0) {
1756                         editor.get_cut_buffer().add (what_we_got);
1757                 }
1758                 break;
1759
1760         case Clear:
1761                 _session.add_undo (playlist->get_memento());
1762                 if ((what_we_got = playlist->cut (time)) != 0) {
1763                         _session.add_redo_no_execute (playlist->get_memento());
1764                         what_we_got->unref ();
1765                         ret = true;
1766                 }
1767                 break;
1768         }
1769
1770         return ret;
1771 }
1772
1773 bool
1774 AudioTimeAxisView::paste (jack_nframes_t pos, float times, Selection& selection, size_t nth)
1775 {
1776         if (!is_audio_track()) {
1777                 return false;
1778         }
1779
1780         Playlist* playlist = get_diskstream()->playlist();
1781         PlaylistSelection::iterator p;
1782         
1783         for (p = selection.playlists.begin(); p != selection.playlists.end() && nth; ++p, --nth);
1784
1785         if (p == selection.playlists.end()) {
1786                 return false;
1787         }
1788
1789         if (get_diskstream()->speed() != 1.0f)
1790                 pos = session_frame_to_track_frame(pos, get_diskstream()->speed() );
1791         
1792         _session.add_undo (playlist->get_memento());
1793         playlist->paste (**p, pos, times);
1794         _session.add_redo_no_execute (playlist->get_memento());
1795
1796         return true;
1797 }
1798
1799 void
1800 AudioTimeAxisView::region_view_added (AudioRegionView* arv)
1801 {
1802         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1803                 AutomationTimeAxisView* atv;
1804
1805                 if ((atv = dynamic_cast<AutomationTimeAxisView*> (*i)) != 0) {
1806                         arv->add_ghost (*atv);
1807                 }
1808         }
1809 }
1810
1811 void
1812 AudioTimeAxisView::add_ghost_to_redirect (AudioRegionView* arv, AutomationTimeAxisView* atv)
1813 {
1814         arv->add_ghost (*atv);
1815 }
1816
1817 list<TimeAxisView*>
1818 AudioTimeAxisView::get_child_list()
1819 {
1820   
1821         list<TimeAxisView*>redirect_children;
1822         
1823         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
1824                 if (!(*i)->hidden()) {
1825                         redirect_children.push_back(*i);
1826                 }
1827         }
1828         return redirect_children;
1829 }
1830
1831
1832 void
1833 AudioTimeAxisView::build_playlist_menu (Gtk::Menu * menu)
1834 {
1835         using namespace Menu_Helpers;
1836
1837         if (!menu || !is_audio_track()) {
1838                 return;
1839         }
1840
1841         MenuList& playlist_items = menu->items();
1842         menu->set_name ("ArdourContextMenu");
1843         playlist_items.clear();
1844
1845         if (playlist_menu) {
1846                 delete playlist_menu;
1847         }
1848         playlist_menu = new Menu;
1849         playlist_menu->set_name ("ArdourContextMenu");
1850
1851         playlist_items.push_back (MenuElem (string_compose (_("Current: %1"), get_diskstream()->playlist()->name())));
1852         playlist_items.push_back (SeparatorElem());
1853         
1854         playlist_items.push_back (MenuElem (_("Rename"), mem_fun(*this, &AudioTimeAxisView::rename_current_playlist)));
1855         playlist_items.push_back (SeparatorElem());
1856
1857         playlist_items.push_back (MenuElem (_("New"), mem_fun(editor, &PublicEditor::new_playlists)));
1858         playlist_items.push_back (MenuElem (_("New Copy"), mem_fun(editor, &PublicEditor::copy_playlists)));
1859         playlist_items.push_back (SeparatorElem());
1860         playlist_items.push_back (MenuElem (_("Clear Current"), mem_fun(editor, &PublicEditor::clear_playlists)));
1861         playlist_items.push_back (SeparatorElem());
1862         playlist_items.push_back (MenuElem(_("Select"), mem_fun(*this, &AudioTimeAxisView::show_playlist_selector)));
1863
1864 }
1865
1866 void
1867 AudioTimeAxisView::show_playlist_selector ()
1868 {
1869         editor.playlist_selector().show_for (this);
1870 }
1871
1872
1873 void
1874 AudioTimeAxisView::map_frozen ()
1875 {
1876         if (!is_audio_track()) {
1877                 return;
1878         }
1879
1880         ENSURE_GUI_THREAD (mem_fun(*this, &AudioTimeAxisView::map_frozen));
1881
1882
1883         switch (audio_track()->freeze_state()) {
1884         case AudioTrack::Frozen:
1885                 playlist_button.set_sensitive (false);
1886                 rec_enable_button->set_sensitive (false);
1887                 break;
1888         default:
1889                 playlist_button.set_sensitive (true);
1890                 rec_enable_button->set_sensitive (true);
1891                 break;
1892         }
1893 }
1894
1895 void
1896 AudioTimeAxisView::show_all_xfades ()
1897 {
1898         if (view) {
1899                 view->show_all_xfades ();
1900         }
1901 }
1902
1903 void
1904 AudioTimeAxisView::hide_all_xfades ()
1905 {
1906         if (view) {
1907                 view->hide_all_xfades ();
1908         }
1909 }
1910
1911 void
1912 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
1913 {
1914         AudioRegionView* rv;
1915
1916         if (view && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
1917                 view->hide_xfades_involving (*rv);
1918         }
1919 }
1920
1921 void
1922 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
1923 {
1924         AudioRegionView* rv;
1925
1926         if (view && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
1927                 view->reveal_xfades_involving (*rv);
1928         }
1929 }
1930
1931 void
1932 AudioTimeAxisView::route_active_changed ()
1933 {
1934         RouteUI::route_active_changed ();
1935
1936         if (is_audio_track()) {
1937                 if (_route.active()) {
1938                         controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
1939                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
1940                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
1941                 } else {
1942                         controls_ebox.set_name ("AudioTrackControlsBaseInactiveUnselected");
1943                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
1944                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
1945                 }
1946         } else {
1947                 if (_route.active()) {
1948                         controls_ebox.set_name ("BusControlsBaseUnselected");
1949                         controls_base_selected_name = "BusControlsBaseSelected";
1950                         controls_base_unselected_name = "BusControlsBaseUnselected";
1951                 } else {
1952                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
1953                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
1954                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
1955                 }
1956         }
1957 }
1958
1959 XMLNode* 
1960 AudioTimeAxisView::get_child_xml_node (const string & childname)
1961 {
1962         return RouteUI::get_child_xml_node (childname);
1963 }
1964
1965 void
1966 AudioTimeAxisView::color_handler (ColorID id, uint32_t val)
1967 {
1968         switch (id) {
1969         case cTimeStretchOutline:
1970                 timestretch_rect->property_outline_color_rgba() = val;
1971                 break;
1972         case cTimeStretchFill:
1973                 timestretch_rect->property_fill_color_rgba() = val;
1974                 break;
1975         default:
1976                 break;
1977         }
1978 }
1979
1980 bool
1981 AudioTimeAxisView::select_me (GdkEventButton* ev)
1982 {
1983         editor.get_selection().add (this);
1984         return false;
1985 }