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