6f8acc9651763572c4dba4c0b67075d35456b3d7
[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/audio_diskstream.h"
41 #include "ardour/audioplaylist.h"
42 #include "ardour/event_type_map.h"
43 #include "ardour/location.h"
44 #include "ardour/panner.h"
45 #include "ardour/playlist.h"
46 #include "ardour/processor.h"
47 #include "ardour/profile.h"
48 #include "ardour/session.h"
49 #include "ardour/session_playlist.h"
50 #include "ardour/utils.h"
51
52 #include "ardour_ui.h"
53 #include "audio_time_axis.h"
54 #include "automation_line.h"
55 #include "canvas_impl.h"
56 #include "crossfade_view.h"
57 #include "enums.h"
58 #include "automation_time_axis.h"
59 #include "keyboard.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 AudioTimeAxisView::AudioTimeAxisView (PublicEditor& ed, Session& sess, boost::shared_ptr<Route> rt, Canvas& canvas)
78         : AxisView(sess)
79         , RouteTimeAxisView(ed, sess, rt, canvas)
80 {
81         // Make sure things are sane...
82         assert(!is_track() || is_audio_track());
83
84         subplugin_menu.set_name ("ArdourContextMenu");
85         waveform_item = 0;
86
87         _view = new AudioStreamView (*this);
88
89         ignore_toggle = false;
90
91         mute_button->set_active (false);
92         solo_button->set_active (false);
93         
94         if (is_audio_track()) {
95                 controls_ebox.set_name ("AudioTrackControlsBaseUnselected");
96         } else { // bus
97                 controls_ebox.set_name ("AudioBusControlsBaseUnselected");
98         }
99
100         ensure_xml_node ();
101
102         set_state (*xml_node);
103
104         /* if set_state above didn't create a gain automation child, we need to make one */
105         if (automation_track (GainAutomation) == 0) {
106                 create_automation_child (GainAutomation, false);
107         }
108         
109         if (_route->panner()) {
110                 _route->panner()->Changed.connect (bind (
111                                 mem_fun(*this, &AudioTimeAxisView::ensure_pan_views),
112                                 false));
113         }
114
115         /* map current state of the route */
116
117         processors_changed ();
118         reset_processor_automation_curves ();
119         ensure_pan_views (false);
120         update_control_names ();
121
122         if (is_audio_track()) {
123
124                 /* ask for notifications of any new RegionViews */
125                 _view->RegionViewAdded.connect (mem_fun(*this, &AudioTimeAxisView::region_view_added));
126
127                 if (!_editor.have_idled()) {
128                         /* first idle will do what we need */
129                 } else {
130                         first_idle ();
131                 } 
132
133         } else {
134                 post_construct ();
135         }
136 }
137
138 AudioTimeAxisView::~AudioTimeAxisView ()
139 {
140 }
141
142 void
143 AudioTimeAxisView::first_idle ()
144 {
145         _view->attach ();
146         post_construct ();
147 }
148
149 AudioStreamView*
150 AudioTimeAxisView::audio_view()
151 {
152         return dynamic_cast<AudioStreamView*>(_view);
153 }
154
155 guint32
156 AudioTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
157 {
158         ensure_xml_node ();
159         xml_node->add_property ("shown-editor", "yes");
160                 
161         return TimeAxisView::show_at (y, nth, parent);
162 }
163
164 void
165 AudioTimeAxisView::hide ()
166 {
167         ensure_xml_node ();
168         xml_node->add_property ("shown-editor", "no");
169
170         TimeAxisView::hide ();
171 }
172
173
174 void
175 AudioTimeAxisView::append_extra_display_menu_items ()
176 {
177         using namespace Menu_Helpers;
178
179         MenuList& items = display_menu->items();
180
181         // crossfade stuff
182         if (!Profile->get_sae()) {
183                 items.push_back (MenuElem (_("Hide all crossfades"), mem_fun(*this, &AudioTimeAxisView::hide_all_xfades)));
184                 items.push_back (MenuElem (_("Show all crossfades"), mem_fun(*this, &AudioTimeAxisView::show_all_xfades)));
185         }
186
187         // waveform menu
188         Menu *waveform_menu = manage(new Menu);
189         MenuList& waveform_items = waveform_menu->items();
190         waveform_menu->set_name ("ArdourContextMenu");
191         
192         waveform_items.push_back (CheckMenuElem (_("Show waveforms"), mem_fun(*this, &AudioTimeAxisView::toggle_waveforms)));
193         waveform_item = static_cast<CheckMenuItem *> (&waveform_items.back());
194         ignore_toggle = true;
195         waveform_item->set_active (_editor.show_waveforms());
196         ignore_toggle = false;
197
198         waveform_items.push_back (SeparatorElem());
199         
200         RadioMenuItem::Group group;
201         
202         waveform_items.push_back (RadioMenuElem (group, _("Traditional"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Traditional)));
203         traditional_item = static_cast<RadioMenuItem *> (&waveform_items.back());
204
205         if (!Profile->get_sae()) {
206                 waveform_items.push_back (RadioMenuElem (group, _("Rectified"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_shape), Rectified)));
207                 rectified_item = static_cast<RadioMenuItem *> (&waveform_items.back());
208         } else {
209                 rectified_item = 0;
210         }
211
212         waveform_items.push_back (SeparatorElem());
213         
214         RadioMenuItem::Group group2;
215
216         waveform_items.push_back (RadioMenuElem (group2, _("Linear"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_scale), LinearWaveform)));
217         linearscale_item = static_cast<RadioMenuItem *> (&waveform_items.back());
218
219         waveform_items.push_back (RadioMenuElem (group2, _("Logarithmic"), bind (mem_fun(*this, &AudioTimeAxisView::set_waveform_scale), LogWaveform)));
220         logscale_item = static_cast<RadioMenuItem *> (&waveform_items.back());
221
222         // setting initial item state
223         AudioStreamView* asv = audio_view();
224         if (asv) {
225                 ignore_toggle = true;
226                 if (asv->get_waveform_shape() == Rectified && rectified_item) {
227                         rectified_item->set_active(true);
228                 } else {
229                         traditional_item->set_active(true);
230                 }
231
232                 if (asv->get_waveform_scale() == LogWaveform) 
233                         logscale_item->set_active(true);
234                 else linearscale_item->set_active(true);
235                 ignore_toggle = false;
236         }
237
238         items.push_back (MenuElem (_("Waveform"), *waveform_menu));
239
240 }
241         
242 Gtk::Menu*
243 AudioTimeAxisView::build_mode_menu()
244 {
245         using namespace Menu_Helpers;
246
247         Menu* mode_menu = manage (new Menu);
248         MenuList& items = mode_menu->items();
249         mode_menu->set_name ("ArdourContextMenu");
250
251         RadioMenuItem::Group mode_group;
252
253         items.push_back (RadioMenuElem (mode_group, _("Normal"),
254                                 bind (mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::Normal)));
255         normal_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
256
257         items.push_back (RadioMenuElem (mode_group, _("Non Overlapping"),
258                                 bind (mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::NonLayered)));
259         non_layered_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
260
261         items.push_back (RadioMenuElem (mode_group, _("Tape"),
262                                 bind (mem_fun (*this, &AudioTimeAxisView::set_track_mode), ARDOUR::Destructive)));
263         destructive_track_mode_item = dynamic_cast<RadioMenuItem*>(&items.back());
264
265         switch (track()->mode()) {
266                 case ARDOUR::Destructive:
267                         destructive_track_mode_item->set_active ();
268                         break;
269                 case ARDOUR::NonLayered:
270                         non_layered_track_mode_item->set_active ();
271                         break;
272                 case ARDOUR::Normal:
273                         normal_track_mode_item->set_active ();
274                         break;
275         }
276
277         return mode_menu;
278 }
279
280 void
281 AudioTimeAxisView::toggle_waveforms ()
282 {
283         AudioStreamView* asv = audio_view();
284         assert(asv);
285
286         if (asv && waveform_item && !ignore_toggle) {
287                 asv->set_show_waveforms (waveform_item->get_active());
288         }
289 }
290
291 void
292 AudioTimeAxisView::set_show_waveforms (bool yn)
293 {
294         AudioStreamView* asv = audio_view();
295         assert(asv);
296
297         if (waveform_item) {
298                 waveform_item->set_active (yn);
299         } else {
300                 asv->set_show_waveforms (yn);
301         }
302 }
303
304 void
305 AudioTimeAxisView::set_show_waveforms_recording (bool yn)
306 {
307         AudioStreamView* asv = audio_view();
308
309         if (asv) {
310                 asv->set_show_waveforms_recording (yn);
311         }
312 }
313
314 void
315 AudioTimeAxisView::set_waveform_shape (WaveformShape shape)
316 {
317         AudioStreamView* asv = audio_view();
318
319         if (asv && !ignore_toggle) {
320                 asv->set_waveform_shape (shape);
321         }
322
323         map_frozen ();
324 }       
325
326 void
327 AudioTimeAxisView::set_waveform_scale (WaveformScale scale)
328 {
329         AudioStreamView* asv = audio_view();
330
331         if (asv && !ignore_toggle) {
332                 asv->set_waveform_scale (scale);
333         }
334
335         map_frozen ();
336 }       
337
338 void
339 AudioTimeAxisView::create_automation_child (const Evoral::Parameter& param, bool show)
340 {
341         if (param.type() == GainAutomation) {
342
343                 boost::shared_ptr<AutomationControl> c = _route->gain_control();
344                 if (!c) {
345                         error << "Route has no gain automation, unable to add automation track view." << endmsg;
346                         return;
347                 }
348
349                 boost::shared_ptr<AutomationTimeAxisView> gain_track(new AutomationTimeAxisView (_session,
350                                 _route, _route, c,
351                                 _editor,
352                                 *this,
353                                 false,
354                                 parent_canvas,
355                                 _route->describe_parameter(param)));
356
357                 add_automation_child(Evoral::Parameter(GainAutomation), gain_track, show);
358
359         } else if (param.type() == PanAutomation) {
360
361                 ensure_xml_node ();
362                 ensure_pan_views (show);
363
364         } else {
365                 error << "AudioTimeAxisView: unknown automation child " << EventTypeMap::instance().to_symbol(param) << endmsg;
366         }
367 }
368
369 /** Ensure that we have the appropriate AutomationTimeAxisViews for the
370  *  panners that we have.
371  *
372  *  @param show true to show any new views that we create, otherwise false.
373  */
374 void
375 AudioTimeAxisView::ensure_pan_views (bool show)
376 {
377         if (!_route->panner()) {
378                 return;
379         }
380
381         const set<Evoral::Parameter>& params = _route->panner()->what_can_be_automated();
382         set<Evoral::Parameter>::iterator p;
383
384         for (p = params.begin(); p != params.end(); ++p) {
385                 boost::shared_ptr<ARDOUR::AutomationControl> pan_control
386                         = boost::dynamic_pointer_cast<ARDOUR::AutomationControl>(
387                                 _route->panner()->data().control(*p));
388                 
389                 if (pan_control->parameter().type() == NullAutomation) {
390                         error << "Pan control has NULL automation type!" << endmsg;
391                         continue;
392                 }
393
394                 if (automation_child (pan_control->parameter ()).get () == 0) {
395
396                         /* we don't already have an AutomationTimeAxisView for this parameter */
397
398                         std::string const name = _route->describe_parameter (pan_control->parameter ());
399
400                         boost::shared_ptr<AutomationTimeAxisView> pan_track (
401                                 new AutomationTimeAxisView (_session,
402                                                             _route, _route, pan_control, 
403                                                             _editor,
404                                                             *this,
405                                                             false,
406                                                             parent_canvas,
407                                                             name));
408                         
409                         add_automation_child (*p, pan_track, show);
410                 }
411         }
412 }
413 #if 0
414 void
415 AudioTimeAxisView::toggle_gain_track ()
416 {
417         bool showit = gain_automation_item->get_active();
418
419         if (showit != gain_track->marked_for_display()) {
420                 if (showit) {
421                         gain_track->set_marked_for_display (true);
422                         gain_track->canvas_display->show();
423                         gain_track->canvas_background->show();
424                         gain_track->get_state_node()->add_property ("shown", X_("yes"));
425                 } else {
426                         gain_track->set_marked_for_display (false);
427                         gain_track->hide ();
428                         gain_track->get_state_node()->add_property ("shown", X_("no"));
429                 }
430
431                 /* now trigger a redisplay */
432                 
433                 if (!no_redraw) {
434                          _route->gui_changed (X_("visible_tracks"), (void *) 0); /* EMIT_SIGNAL */
435                 }
436         }
437 }
438
439 void
440 AudioTimeAxisView::gain_hidden ()
441 {
442         gain_track->get_state_node()->add_property (X_("shown"), X_("no"));
443
444         if (gain_automation_item && !_hidden) {
445                 gain_automation_item->set_active (false);
446         }
447
448          _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
449 }
450
451 void
452 AudioTimeAxisView::toggle_pan_track ()
453 {
454         bool showit = pan_automation_item->get_active();
455
456         if (showit != pan_track->marked_for_display()) {
457                 if (showit) {
458                         pan_track->set_marked_for_display (true);
459                         pan_track->canvas_display->show();
460                         pan_track->canvas_background->show();
461                         pan_track->get_state_node()->add_property ("shown", X_("yes"));
462                 } else {
463                         pan_track->set_marked_for_display (false);
464                         pan_track->hide ();
465                         pan_track->get_state_node()->add_property ("shown", X_("no"));
466                 }
467
468                 /* now trigger a redisplay */
469         }
470 }
471 #endif
472                 
473 void
474 AudioTimeAxisView::show_all_automation ()
475 {
476         no_redraw = true;
477
478         RouteTimeAxisView::show_all_automation ();
479
480         no_redraw = false;
481
482          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
483 }
484
485 void
486 AudioTimeAxisView::show_existing_automation ()
487 {
488         no_redraw = true;
489
490         RouteTimeAxisView::show_existing_automation ();
491
492         no_redraw = false;
493
494          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
495 }
496
497 void
498 AudioTimeAxisView::hide_all_automation ()
499 {
500         no_redraw = true;
501
502         RouteTimeAxisView::hide_all_automation();
503
504         no_redraw = false;
505          _route->gui_changed ("track_height", (void *) 0); /* EMIT_SIGNAL */
506 }
507
508 void
509 AudioTimeAxisView::show_all_xfades ()
510 {
511         AudioStreamView* asv = audio_view();
512
513         if (asv) {
514                 asv->show_all_xfades ();
515         }
516 }
517
518 void
519 AudioTimeAxisView::hide_all_xfades ()
520 {
521         AudioStreamView* asv = audio_view();
522         
523         if (asv) {
524                 asv->hide_all_xfades ();
525         }
526 }
527
528 void
529 AudioTimeAxisView::hide_dependent_views (TimeAxisViewItem& tavi)
530 {
531         AudioStreamView* asv = audio_view();
532         AudioRegionView* rv;
533
534         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
535                 asv->hide_xfades_involving (*rv);
536         }
537 }
538
539 void
540 AudioTimeAxisView::reveal_dependent_views (TimeAxisViewItem& tavi)
541 {
542         AudioStreamView* asv = audio_view();
543         AudioRegionView* rv;
544
545         if (asv && (rv = dynamic_cast<AudioRegionView*>(&tavi)) != 0) {
546                 asv->reveal_xfades_involving (*rv);
547         }
548 }
549
550 void
551 AudioTimeAxisView::route_active_changed ()
552 {
553         RouteTimeAxisView::route_active_changed ();
554         update_control_names ();
555 }
556
557
558 /**
559  *    Set up the names of the controls so that they are coloured
560  *    correctly depending on whether this route is inactive or
561  *    selected.
562  */
563
564 void
565 AudioTimeAxisView::update_control_names ()
566 {
567         if (is_audio_track()) {
568                 if (_route->active()) {
569                         controls_base_selected_name = "AudioTrackControlsBaseSelected";
570                         controls_base_unselected_name = "AudioTrackControlsBaseUnselected";
571                 } else {
572                         controls_base_selected_name = "AudioTrackControlsBaseInactiveSelected";
573                         controls_base_unselected_name = "AudioTrackControlsBaseInactiveUnselected";
574                 }
575         } else {
576                 if (_route->active()) {
577                         controls_base_selected_name = "BusControlsBaseSelected";
578                         controls_base_unselected_name = "BusControlsBaseUnselected";
579                 } else {
580                         controls_base_selected_name = "BusControlsBaseInactiveSelected";
581                         controls_base_unselected_name = "BusControlsBaseInactiveUnselected";
582                 }
583         }
584
585         if (get_selected()) {
586                 controls_ebox.set_name (controls_base_selected_name);
587         } else {
588                 controls_ebox.set_name (controls_base_unselected_name);
589         }
590 }
591