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