45c8700a6a78a95dee7087829e2734dbe59b789a
[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     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cmath>
23 #include <cassert>
24
25 #include <algorithm>
26 #include <string>
27 #include <vector>
28
29 #include <sigc++/bind.h>
30
31 #include <pbd/error.h>
32 #include <pbd/stl_delete.h>
33 #include <pbd/memento_command.h>
34
35 #include <gtkmm2ext/gtk_ui.h>
36 #include <gtkmm2ext/selector.h>
37 #include <gtkmm2ext/stop_signal.h>
38 #include <gtkmm2ext/bindable_button.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include <ardour/audioplaylist.h>
42 #include <ardour/audio_diskstream.h>
43 #include <ardour/insert.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 "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
79 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
80         : AxisView(sess)
81         , RouteTimeAxisView(ed, sess, rt, canvas)
82 {
83         // Make sure things are sane...
84         assert(!is_track() || is_audio_track());
85
86         subplugin_menu.set_name ("ArdourContextMenu");
87         gain_track = 0;
88         pan_track = 0;
89         waveform_item = 0;
90         pan_automation_item = 0;
91         gain_automation_item = 0;
92
93         _view = new AudioStreamView (*this);
94
95         add_gain_automation_child ();
96         add_pan_automation_child ();
97
98         ignore_toggle = false;
99
100         if (is_audio_track())
101                 controls_ebox.set_name ("AudioTimeAxisViewControlsBaseUnselected");
102         else // bus
103                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
104
105         ensure_xml_node ();
106
107         set_state (*xml_node);
108         
109         _route->panner().Changed.connect (mem_fun(*this, &AudioTimeAxisView::update_pans));
110
111         if (is_audio_track()) {
112
113                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
114                 controls_base_selected_name = "AudioTrackControlsBaseSelected";
115                 controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
116
117                 /* ask for notifications of any new RegionViews */
118                 _view->RegionViewAdded.connect (mem_fun(*this, &AudioTimeAxisView::region_view_added));
119                 _view->attach ();
120
121         } else { /* bus */
122
123                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
124                 controls_base_selected_name = "AudioBusControlsBaseSelected";
125                 controls_base_unselected_name = "AudioBusControlsBaseUnselected";
126         }
127
128         post_construct ();
129 }
130
131 AudioTimeAxisView::~AudioTimeAxisView ()
132 {
133 }
134
135 AudioStreamView*
136 AudioTimeAxisView::audio_view()
137 {
138         return dynamic_cast<AudioStreamView*>(_view);
139 }
140
141 guint32
142 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
143 {
144         ensure_xml_node ();
145         xml_node->add_property ("shown_editor", "yes");
146                 
147         return TimeAxisView::show_at (y, nth, parent);
148 }
149
150 void
151 AudioTimeAxisView::hide ()
152 {
153         ensure_xml_node ();
154         xml_node->add_property ("shown_editor", "no");
155
156         TimeAxisView::hide ();
157 }
158
159 void
160 AudioTimeAxisView::set_state (const XMLNode& node)
161 {
162         const XMLProperty *prop;
163         
164         TimeAxisView::set_state (node);
165         
166         if ((prop = node.property ("shown_editor")) != 0) {
167                 if (prop->value() == "no") {
168                         _marked_for_display = false;
169                 } else {
170                         _marked_for_display = true;
171                 }
172         } else {
173                 _marked_for_display = true;
174         }
175         
176         XMLNodeList nlist = node.children();
177         XMLNodeConstIterator niter;
178         XMLNode *child_node;
179         
180         
181         show_gain_automation = false;
182         show_pan_automation  = false;
183         
184         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
185                 child_node = *niter;
186
187                 if (child_node->name() == "gain") {
188                         XMLProperty *prop=child_node->property ("shown");
189                         
190                         if (prop != 0) {
191                                 if (prop->value() == "yes") {
192                                         show_gain_automation = true;
193                                 }
194                         }
195                         continue;
196                 }
197                 
198                 if (child_node->name() == "pan") {
199                         XMLProperty *prop=child_node->property ("shown");
200                         
201                         if (prop != 0) {
202                                 if (prop->value() == "yes") {
203                                         show_pan_automation = true;
204                                 }                       
205                         }
206                         continue;
207                 }
208         }
209 }
210
211 void
212 AudioTimeAxisView::build_automation_action_menu ()
213 {
214         using namespace Menu_Helpers;
215
216         RouteTimeAxisView::build_automation_action_menu ();
217
218         MenuList& automation_items = automation_action_menu->items();
219         
220         automation_items.push_back (SeparatorElem());
221
222         automation_items.push_back (CheckMenuElem (_("Fader"), 
223                                                    mem_fun(*this, &AudioTimeAxisView::toggle_gain_track)));
224         gain_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
225         gain_automation_item->set_active(show_gain_automation);
226
227         automation_items.push_back (CheckMenuElem (_("Pan"),
228                                                    mem_fun(*this, &AudioTimeAxisView::toggle_pan_track)));
229         pan_automation_item = static_cast<CheckMenuItem*> (&automation_items.back());
230         pan_automation_item->set_active(show_pan_automation);
231         
232 }
233
234 void
235 AudioTimeAxisView::append_extra_display_menu_items ()
236 {
237         using namespace Menu_Helpers;
238
239         MenuList& items = display_menu->items();
240
241         // crossfade stuff
242         items.push_back (MenuElem (_("Hide all crossfades"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
243         items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
244
245         // waveform menu
246         Menu *waveform_menu = manage(new Menu);
247         MenuList& waveform_items = waveform_menu->items();
248         waveform_menu->set_name ("ArdourContextMenu");
249         
250         waveform_items.push_back (CheckMenuElem (_("Show waveforms"), mem_fun(*this, &AudioTimeAxisView::toggle_waveforms)));
251         waveform_item = static_cast<CheckMenuItem *> (&waveform_items.back());
252         ignore_toggle = true;
253         waveform_item->set_active (editor.show_waveforms());
254         ignore_toggle = false;
255
256         RadioMenuItem::Group group;
257
258         waveform_items.push_back (RadioMenuElem (group, _("Traditional"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Traditional)));
259         traditional_item = static_cast<RadioMenuItem *> (&waveform_items.back());
260
261         waveform_items.push_back (RadioMenuElem (group, _("Rectified"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Rectified)));
262         rectified_item = static_cast<RadioMenuItem *> (&waveform_items.back());
263
264         items.push_back (MenuElem (_("Waveform"), *waveform_menu));
265 }
266
267 void
268 AudioTimeAxisView::toggle_waveforms ()
269 {
270         AudioStreamView* asv = audio_view();
271         assert(asv);
272
273         if (asv && waveform_item && !ignore_toggle) {
274                 asv->set_show_waveforms (waveform_item->get_active());
275         }
276 }
277
278 void
279 AudioTimeAxisView::set_show_waveforms (bool yn)
280 {
281         AudioStreamView* asv = audio_view();
282         assert(asv);
283
284         if (waveform_item) {
285                 waveform_item->set_active (yn);
286         } else {
287                 asv->set_show_waveforms (yn);
288         }
289 }
290
291 void
292 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
293 {
294         AudioStreamView* asv = audio_view();
295
296         if (asv) {
297                 asv->set_show_waveforms_recording (yn);
298         }
299 }
300
301 void
302 AudioTimeAxisView::set_waveform_shape (WaveformShape shape)
303 {
304         AudioStreamView* asv = audio_view();
305
306         if (asv) {
307                 asv->set_waveform_shape (shape);
308         }
309
310         map_frozen ();
311 }       
312
313 void
314 AudioTimeAxisView::add_gain_automation_child ()
315 {
316         XMLProperty* prop;
317         AutomationLine* line;
318
319         gain_track = new GainAutomationTimeAxisView (_session,
320                                                      _route,
321                                                      editor,
322                                                      *this,
323                                                      parent_canvas,
324                                                      _("gain"),
325                                                      _route->gain_automation_curve());
326         
327         line = new AutomationGainLine ("automation gain",
328                                        _session,
329                                        *gain_track,
330                                        *gain_track->canvas_display,
331                                        _route->gain_automation_curve());
332
333         line->set_line_color (color_map[cAutomationLine]);
334         
335
336         gain_track->add_line (*line);
337
338         add_child (gain_track);
339
340         gain_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::gain_hidden));
341
342         bool hideit = true;
343         
344         XMLNode* node;
345
346         if ((node = gain_track->get_state_node()) != 0) {
347                 if  ((prop = node->property ("shown")) != 0) {
348                         if (prop->value() == "yes") {
349                                 hideit = false;
350                         }
351                 } 
352         }
353
354         if (hideit) {
355                 gain_track->hide ();
356         }
357 }
358
359 void
360 AudioTimeAxisView::add_pan_automation_child ()
361 {
362         XMLProperty* prop;
363
364         pan_track = new PanAutomationTimeAxisView (_session, _route, editor, *this, parent_canvas, _("pan"));
365
366         update_pans ();
367         
368         add_child (pan_track);
369
370         pan_track->Hiding.connect (mem_fun(*this, &AudioTimeAxisView::pan_hidden));
371
372         ensure_xml_node ();
373         bool hideit = true;
374         
375         XMLNode* node;
376
377         if ((node = pan_track->get_state_node()) != 0) {
378                 if ((prop = node->property ("shown")) != 0) {
379                         if (prop->value() == "yes") {
380                                 hideit = false;
381                         }
382                 } 
383         }
384
385         if (hideit) {
386                 pan_track->hide ();
387         }
388 }
389
390 void
391 AudioTimeAxisView::update_pans ()
392 {
393         Panner::iterator p;
394         
395         pan_track->clear_lines ();
396         
397         /* we don't draw lines for "greater than stereo" panning.
398          */
399
400         if (_route->n_outputs() > 2) {
401                 return;
402         }
403
404         for (p = _route->panner().begin(); p != _route->panner().end(); ++p) {
405
406                 AutomationLine* line;
407
408                 line = new AutomationPanLine ("automation pan", _session, *pan_track,
409                                               *pan_track->canvas_display, 
410                                               (*p)->automation());
411
412                 if (p == _route->panner().begin()) {
413                         /* first line is a nice orange */
414                         line->set_line_color (color_map[cLeftPanAutomationLine]);
415                 } else {
416                         /* second line is a nice blue */
417                         line->set_line_color (color_map[cRightPanAutomationLine]);
418                 }
419
420                 pan_track->add_line (*line);
421         }
422 }
423                 
424 void
425 AudioTimeAxisView::toggle_gain_track ()
426 {
427
428         bool showit = gain_automation_item->get_active();
429
430         if (showit != gain_track->marked_for_display()) {
431                 if (showit) {
432                         gain_track->set_marked_for_display (true);
433                         gain_track->canvas_display->show();
434                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
435                 } else {
436                         gain_track->set_marked_for_display (false);
437                         gain_track->hide ();
438                         gain_track->get_state_node()->add_property ("shown", X_("no"));
439                 }
440
441                 /* now trigger a redisplay */
442                 
443                 if (!no_redraw) {
444                          _route->gui_changed (X_("track_height"), (void *) 0); /* EMIT_SIGNAL */
445                 }
446         }
447 }
448
449 void
450 AudioTimeAxisView::gain_hidden ()
451 {
452         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
453
454         if (gain_automation_item && !_hidden) {
455                 gain_automation_item->set_active (false);
456         }
457
458          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
459 }
460
461 void
462 AudioTimeAxisView::toggle_pan_track ()
463 {
464         bool showit = pan_automation_item->get_active();
465
466         if (showit != pan_track->marked_for_display()) {
467                 if (showit) {
468                         pan_track->set_marked_for_display (true);
469                         pan_track->canvas_display->show();
470                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
471                 } else {
472                         pan_track->set_marked_for_display (false);
473                         pan_track->hide ();
474                         pan_track->get_state_node()->add_property ("shown", X_("no"));
475                 }
476
477                 /* now trigger a redisplay */
478                 
479                 if (!no_redraw) {
480                          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
481                 }
482         }
483 }
484
485 void
486 AudioTimeAxisView::pan_hidden ()
487 {
488         pan_track->get_state_node()->add_property ("shown", "no");
489
490         if (pan_automation_item && !_hidden) {
491                 pan_automation_item->set_active (false);
492         }
493
494          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
495 }
496
497 void
498 AudioTimeAxisView::show_all_automation ()
499 {
500         no_redraw = true;
501
502         pan_automation_item->set_active (true);
503         gain_automation_item->set_active (true);
504         
505         RouteTimeAxisView::show_all_automation ();
506
507         no_redraw = false;
508
509          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
510 }
511
512 void
513 AudioTimeAxisView::show_existing_automation ()
514 {
515         no_redraw = true;
516
517         pan_automation_item->set_active (true);
518         gain_automation_item->set_active (true);
519
520         RouteTimeAxisView::show_existing_automation ();
521
522         no_redraw = false;
523
524          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
525 }
526
527 void
528 AudioTimeAxisView::hide_all_automation ()
529 {
530         no_redraw = true;
531
532         pan_automation_item->set_active (false);
533         gain_automation_item->set_active (false);
534
535         RouteTimeAxisView::hide_all_automation();
536
537         no_redraw = false;
538          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
539 }
540
541 void
542 AudioTimeAxisView::show_all_xfades ()
543 {
544         AudioStreamView* asv = audio_view();
545
546         if (asv) {
547                 asv->show_all_xfades ();
548         }
549 }
550
551 void
552 AudioTimeAxisView::hide_all_xfades ()
553 {
554         AudioStreamView* asv = audio_view();
555         
556         if (asv) {
557                 asv->hide_all_xfades ();
558         }
559 }
560
561 void
562 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
563 {
564         AudioStreamView* asv = audio_view();
565         AudioRegionView* rv;
566
567         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
568                 asv->hide_xfades_involving (*rv);
569         }
570 }
571
572 void
573 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
574 {
575         AudioStreamView* asv = audio_view();
576         AudioRegionView* rv;
577
578         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
579                 asv->reveal_xfades_involving (*rv);
580         }
581 }
582
583 void
584 AudioTimeAxisView::route_active_changed ()
585 {
586         RouteTimeAxisView::route_active_changed ();
587
588         if (is_audio_track()) {
589                 if (_route->active()) {
590                         controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
591                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
592                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
593                 } else {
594                         controls_ebox.set_name ("AudioTrackControlsBaseInactiveUnselected");
595                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
596                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
597                 }
598         } else {
599                 if (_route->active()) {
600                         controls_ebox.set_name ("BusControlsBaseUnselected");
601                         controls_base_selected_name = "BusControlsBaseSelected";
602                         controls_base_unselected_name = "BusControlsBaseUnselected";
603                 } else {
604                         controls_ebox.set_name ("BusControlsBaseInactiveUnselected");
605                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
606                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
607                 }
608         }
609 }
610
611 XMLNode* 
612 AudioTimeAxisView::get_child_xml_node (const string & childname)
613 {
614         return RouteUI::get_child_xml_node (childname);
615 }
616