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