Fixed overflow issue. Code originally meant to truncate the 64 bit integer did not...
[ardour.git] / gtk2_ardour / audio_time_axis.cc
1 /*
2     Copyright (C) 2000-2006 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 */
19
20 #include <cstdlib>
21 #include <cmath>
22 #include <cassert>
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/memento_command.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/location.h>
44 #include <ardour/panner.h>
45 #include <ardour/playlist.h>
46 #include <ardour/profile.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 "canvas_impl.h"
56 #include "crossfade_view.h"
57 #include "enums.h"
58 #include "gain_automation_time_axis.h"
59 #include "keyboard.h"
60 #include "pan_automation_time_axis.h"
61 #include "playlist_selector.h"
62 #include "prompter.h"
63 #include "public_editor.h"
64 #include "audio_region_view.h"
65 #include "simplerect.h"
66 #include "audio_streamview.h"
67 #include "utils.h"
68
69 #include <ardour/audio_track.h>
70
71 #include "i18n.h"
72
73 using namespace ARDOUR;
74 using namespace PBD;
75 using namespace Gtk;
76 using namespace Editing;
77
78 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
79         : AxisView(sess)
80         , RouteTimeAxisView(ed, sess, rt, canvas)
81 {
82         // Make sure things are sane...
83         assert(!is_track() || is_audio_track());
84
85         subplugin_menu.set_name ("ArdourContextMenu");
86         gain_track = 0;
87         pan_track = 0;
88         waveform_item = 0;
89         pan_automation_item = 0;
90         gain_automation_item = 0;
91
92         _view = new AudioStreamView (*this);
93
94         add_gain_automation_child ();
95         add_pan_automation_child ();
96
97         ignore_toggle = false;
98
99         if (is_audio_track())
100                 controls_ebox.set_name ("AudioTimeAxisViewControlsBaseUnselected");
101         else // bus
102                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
103
104         ensure_xml_node ();
105
106         set_state (*xml_node);
107         
108         _route->panner().Changed.connect (mem_fun(*this, &AudioTimeAxisView::update_pans));
109
110         update_control_names ();
111
112         if (is_audio_track()) {
113
114                 /* ask for notifications of any new RegionViews */
115                 _view->RegionViewAdded.connect (mem_fun(*this, &AudioTimeAxisView::region_view_added));
116
117                 if (!editor.have_idled()) {
118                         /* first idle will do what we need */
119                 } else {
120                         first_idle ();
121                 } 
122
123         } else {
124                 post_construct ();
125         }
126 }
127
128 AudioTimeAxisView::~AudioTimeAxisView ()
129 {
130 }
131
132 void
133 AudioTimeAxisView::first_idle ()
134 {
135         _view->attach ();
136         post_construct ();
137 }
138
139 AudioStreamView*
140 AudioTimeAxisView::audio_view()
141 {
142         return dynamic_cast<AudioStreamView*>(_view);
143 }
144
145 guint32
146 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
147 {
148         ensure_xml_node ();
149         xml_node->add_property ("shown_editor", "yes");
150                 
151         return TimeAxisView::show_at (y, nth, parent);
152 }
153
154 void
155 AudioTimeAxisView::hide ()
156 {
157         ensure_xml_node ();
158         xml_node->add_property ("shown_editor", "no");
159
160         TimeAxisView::hide ();
161 }
162
163 int
164 AudioTimeAxisView::set_state (const XMLNode& node)
165 {
166         const XMLProperty *prop;
167         int ret;
168
169         if ((ret = TimeAxisView::set_state (node)) != 0) {
170                 return ret;
171         }
172
173         /* if the TimeAxisView had marked this not for display,
174            then do not allow this to override it.
175         */
176
177         if ((prop = node.property ("marked_for_display")) == 0) {
178                 if ((prop = node.property ("shown_editor")) != 0) {
179                         if (prop->value() == "no") {
180                                 _marked_for_display = false;
181                         } else {
182                                 _marked_for_display = true;
183                         }
184                 } else {
185                         _marked_for_display = true;
186                 }
187         }
188         
189         XMLNodeList nlist = node.children();
190         XMLNodeConstIterator niter;
191         XMLNode *child_node;
192         
193         
194         show_gain_automation = false;
195         show_pan_automation  = false;
196         
197         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
198                 child_node = *niter;
199
200                 if (child_node->name() == "gain") {
201                         XMLProperty *prop=child_node->property ("shown");
202                         
203                         if (prop != 0) {
204                                 if (prop->value() == "yes") {
205                                         show_gain_automation = true;
206                                 }
207                         }
208                         continue;
209                 }
210                 
211                 if (child_node->name() == "pan") {
212                         XMLProperty *prop=child_node->property ("shown");
213                         
214                         if (prop != 0) {
215                                 if (prop->value() == "yes") {
216                                         show_pan_automation = true;
217                                 }                       
218                         }
219                         continue;
220                 }
221         }
222
223         return 0;
224 }
225
226 void
227 AudioTimeAxisView::build_automation_action_menu ()
228 {
229         using namespace Menu_Helpers;
230
231         RouteTimeAxisView::build_automation_action_menu ();
232
233         MenuList& automation_items = automation_action_menu->items();
234         
235         automation_items.push_back (SeparatorElem());
236
237         automation_items.push_back (CheckMenuElem (_("Fader"), 
238                                                    mem_fun(*this, &AudioTimeAxisView::toggle_gain_track)));
239         gain_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
240         gain_automation_item->set_active(show_gain_automation);
241
242         automation_items.push_back (CheckMenuElem (_("Pan"),
243                                                    mem_fun(*this, &AudioTimeAxisView::toggle_pan_track)));
244         pan_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
245         pan_automation_item->set_active(show_pan_automation);
246         
247 }
248
249 void
250 AudioTimeAxisView::append_extra_display_menu_items ()
251 {
252         using namespace Menu_Helpers;
253
254         MenuList& items = display_menu->items();
255
256         // crossfade stuff
257         if (!Profile->get_sae()) {
258                 items.push_back (MenuElem (_("Hide all crossfades"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
259                 items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
260         }
261
262         // waveform menu
263         Menu *waveform_menu = manage(new Menu);
264         MenuList& waveform_items = waveform_menu->items();
265         waveform_menu->set_name ("ArdourContextMenu");
266         
267         waveform_items.push_back (CheckMenuElem (_("Show waveforms"), mem_fun(*this, &AudioTimeAxisView::toggle_waveforms)));
268         waveform_item = static_cast<CheckMenuItem *> (&waveform_items.back());
269         ignore_toggle = true;
270         waveform_item->set_active (editor.show_waveforms());
271         ignore_toggle = false;
272
273         waveform_items.push_back (SeparatorElem());
274         
275         RadioMenuItem::Group group;
276         
277         waveform_items.push_back (RadioMenuElem (group, _("Traditional"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Traditional)));
278         traditional_item = static_cast<RadioMenuItem *> (&waveform_items.back());
279
280         if (!Profile->get_sae()) {
281                 waveform_items.push_back (RadioMenuElem (group, _("Rectified"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Rectified)));
282                 rectified_item = static_cast<RadioMenuItem *> (&waveform_items.back());
283         } else {
284                 rectified_item = 0;
285         }
286
287         waveform_items.push_back (SeparatorElem());
288         
289         RadioMenuItem::Group group2;
290
291         waveform_items.push_back (RadioMenuElem (group2, _("Linear"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_scale), LinearWaveform)));
292         linearscale_item = static_cast<RadioMenuItem *> (&waveform_items.back());
293
294         waveform_items.push_back (RadioMenuElem (group2, _("Logarithmic"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_scale), LogWaveform)));
295         logscale_item = static_cast<RadioMenuItem *> (&waveform_items.back());
296
297         // setting initial item state
298         AudioStreamView* asv = audio_view();
299         if (asv) {
300                 ignore_toggle = true;
301                 if (asv->get_waveform_shape() == Rectified && rectified_item) {
302                         rectified_item->set_active(true);
303                 } else {
304                         traditional_item->set_active(true);
305                 }
306
307                 if (asv->get_waveform_scale() == LogWaveform) 
308                         logscale_item->set_active(true);
309                 else linearscale_item->set_active(true);
310                 ignore_toggle = false;
311         }
312
313         items.push_back (MenuElem (_("Waveform"), *waveform_menu));
314 }
315
316 void
317 AudioTimeAxisView::toggle_waveforms ()
318 {
319         AudioStreamView* asv = audio_view();
320         assert(asv);
321
322         if (asv && waveform_item && !ignore_toggle) {
323                 asv->set_show_waveforms (waveform_item->get_active());
324         }
325 }
326
327 void
328 AudioTimeAxisView::set_show_waveforms (bool yn)
329 {
330         AudioStreamView* asv = audio_view();
331         assert(asv);
332
333         if (waveform_item) {
334                 waveform_item->set_active (yn);
335         } else {
336                 asv->set_show_waveforms (yn);
337         }
338 }
339
340 void
341 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
342 {
343         AudioStreamView* asv = audio_view();
344
345         if (asv) {
346                 asv->set_show_waveforms_recording (yn);
347         }
348 }
349
350 void
351 AudioTimeAxisView::set_waveform_shape (WaveformShape shape)
352 {
353         AudioStreamView* asv = audio_view();
354
355         if (asv && !ignore_toggle) {
356                 asv->set_waveform_shape (shape);
357         }
358
359         map_frozen ();
360 }       
361
362 void
363 AudioTimeAxisView::set_waveform_scale (WaveformScale scale)
364 {
365         AudioStreamView* asv = audio_view();
366
367         if (asv && !ignore_toggle) {
368                 asv->set_waveform_scale (scale);
369         }
370
371         map_frozen ();
372 }       
373
374 void
375 AudioTimeAxisView::add_gain_automation_child ()
376 {
377         XMLProperty* prop;
378         AutomationLine* line;
379
380         gain_track = new GainAutomationTimeAxisView (_session,
381                                                      _route,
382                                                      editor,
383                                                      *this,
384                                                      parent_canvas,
385                                                      _("gain"),
386                                                      _route->gain_automation_curve());
387         
388         line = new AutomationGainLine ("automation gain",
389                                        _session,
390                                        *gain_track,
391                                        *gain_track->canvas_display,
392                                        _route->gain_automation_curve());
393
394         line->set_line_color (ARDOUR_UI::config()->canvasvar_AutomationLine.get());
395         
396
397         gain_track->add_line (*line);
398
399         add_child (gain_track);
400
401         gain_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::gain_hidden));
402
403         bool hideit = true;
404         
405         XMLNode* node;
406
407         if ((node = gain_track->get_state_node()) != 0) {
408                 if  ((prop = node->property ("shown")) != 0) {
409                         if (prop->value() == "yes") {
410                                 hideit = false;
411                         }
412                 } 
413         }
414
415         if (hideit) {
416                 gain_track->hide ();
417         }
418 }
419
420 void
421 AudioTimeAxisView::add_pan_automation_child ()
422 {
423         XMLProperty* prop;
424
425         pan_track = new PanAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, _("pan"));
426
427         update_pans ();
428         
429         add_child (pan_track);
430
431         pan_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::pan_hidden));
432
433         ensure_xml_node ();
434         bool hideit = true;
435         
436         XMLNode* node;
437
438         if ((node = pan_track->get_state_node()) != 0) {
439                 if ((prop = node->property ("shown")) != 0) {
440                         if (prop->value() == "yes") {
441                                 hideit = false;
442                         }
443                 } 
444         }
445
446         if (hideit) {
447                 pan_track->hide ();
448         }
449 }
450
451 void
452 AudioTimeAxisView::update_pans ()
453 {
454         Panner::iterator p;
455         
456         pan_track->clear_lines ();
457         
458         /* we don't draw lines for "greater than stereo" panning.
459          */
460
461         if (_route->n_outputs() > 2) {
462                 return;
463         }
464
465         for (p = _route->panner().begin(); p != _route->panner().end(); ++p) {
466
467                 AutomationLine* line;
468
469                 line = new AutomationPanLine ("automation pan", _session, *pan_track,
470                                               *pan_track->canvas_display, 
471                                               (*p)->automation());
472
473                 if (p == _route->panner().begin()) {
474                         line->set_line_color (ARDOUR_UI::config()->canvasvar_AutomationLine.get());
475                 } else {
476                         line->set_line_color (ARDOUR_UI::config()->canvasvar_AutomationLine.get());
477                 }
478
479                 pan_track->add_line (*line);
480         }
481 }
482                 
483 void
484 AudioTimeAxisView::toggle_gain_track ()
485 {
486         bool showit = gain_automation_item->get_active();
487
488         if (showit != gain_track->marked_for_display()) {
489                 if (showit) {
490                         gain_track->set_marked_for_display (true);
491                         gain_track->canvas_display->show();
492                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
493                 } else {
494                         gain_track->set_marked_for_display (false);
495                         gain_track->hide ();
496                         gain_track->get_state_node()->add_property ("shown", X_("no"));
497                 }
498
499                 /* now trigger a redisplay */
500                 
501                 if (!no_redraw) {
502                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
503                 }
504         }
505 }
506
507 void
508 AudioTimeAxisView::gain_hidden ()
509 {
510         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
511
512         if (gain_automation_item && !_hidden) {
513                 gain_automation_item->set_active (false);
514         }
515
516          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
517 }
518
519 void
520 AudioTimeAxisView::toggle_pan_track ()
521 {
522         bool showit = pan_automation_item->get_active();
523
524         if (showit != pan_track->marked_for_display()) {
525                 if (showit) {
526                         pan_track->set_marked_for_display (true);
527                         pan_track->canvas_display->show();
528                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
529                 } else {
530                         pan_track->set_marked_for_display (false);
531                         pan_track->hide ();
532                         pan_track->get_state_node()->add_property ("shown", X_("no"));
533                 }
534
535                 /* now trigger a redisplay */
536                 
537                 if (!no_redraw) {
538                          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
539                 }
540         }
541 }
542
543 void
544 AudioTimeAxisView::pan_hidden ()
545 {
546         pan_track->get_state_node()->add_property ("shown", "no");
547
548         if (pan_automation_item && !_hidden) {
549                 pan_automation_item->set_active (false);
550         }
551
552          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
553 }
554
555 void
556 AudioTimeAxisView::show_all_automation ()
557 {
558         no_redraw = true;
559
560         pan_automation_item->set_active (true);
561         gain_automation_item->set_active (true);
562         
563         RouteTimeAxisView::show_all_automation ();
564
565         no_redraw = false;
566
567          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
568 }
569
570 void
571 AudioTimeAxisView::show_existing_automation ()
572 {
573         no_redraw = true;
574
575         pan_automation_item->set_active (true);
576         gain_automation_item->set_active (true);
577
578         RouteTimeAxisView::show_existing_automation ();
579
580         no_redraw = false;
581
582          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
583 }
584
585 void
586 AudioTimeAxisView::hide_all_automation ()
587 {
588         no_redraw = true;
589
590         pan_automation_item->set_active (false);
591         gain_automation_item->set_active (false);
592
593         RouteTimeAxisView::hide_all_automation();
594
595         no_redraw = false;
596          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
597 }
598
599 void
600 AudioTimeAxisView::show_all_xfades ()
601 {
602         AudioStreamView* asv = audio_view();
603
604         if (asv) {
605                 asv->show_all_xfades ();
606         }
607 }
608
609 void
610 AudioTimeAxisView::hide_all_xfades ()
611 {
612         AudioStreamView* asv = audio_view();
613         
614         if (asv) {
615                 asv->hide_all_xfades ();
616         }
617 }
618
619 void
620 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
621 {
622         AudioStreamView* asv = audio_view();
623         AudioRegionView* rv;
624
625         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
626                 asv->hide_xfades_involving (*rv);
627         }
628 }
629
630 void
631 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
632 {
633         AudioStreamView* asv = audio_view();
634         AudioRegionView* rv;
635
636         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
637                 asv->reveal_xfades_involving (*rv);
638         }
639 }
640
641 void
642 AudioTimeAxisView::route_active_changed ()
643 {
644         RouteTimeAxisView::route_active_changed ();
645         update_control_names ();
646 }
647
648
649 /**
650  *    Set up the names of the controls so that they are coloured
651  *    correctly depending on whether this route is inactive or
652  *    selected.
653  */
654
655 void
656 AudioTimeAxisView::update_control_names ()
657 {
658         if (is_audio_track()) {
659                 if (_route->active()) {
660                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
661                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
662                 } else {
663                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
664                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
665                 }
666         } else {
667                 if (_route->active()) {
668                         controls_base_selected_name = "BusControlsBaseSelected";
669                         controls_base_unselected_name = "BusControlsBaseUnselected";
670                 } else {
671                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
672                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
673                 }
674         }
675
676         if (get_selected()) {
677                 controls_ebox.set_name (controls_base_selected_name);
678         } else {
679                 controls_ebox.set_name (controls_base_unselected_name);
680         }
681 }
682
683 XMLNode* 
684 AudioTimeAxisView::get_child_xml_node (const string & childname)
685 {
686         return RouteUI::get_child_xml_node (childname);
687 }
688