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