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