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