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