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