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