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