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