fix dragging that involves locked regions; auto-rebinding patch for people to experim...
[ardour.git] / gtk2_ardour / audio_region_view.cc
1 /*
2     Copyright (C) 2001-2006 Paul Davis 
3
4     This program is free software; you can r>edistribute 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 <cmath>
21 #include <cassert>
22 #include <algorithm>
23
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27
28 #include <ardour/playlist.h>
29 #include <ardour/audioregion.h>
30 #include <ardour/audiosource.h>
31 #include <ardour/audio_diskstream.h>
32 #include <ardour/profile.h>
33 #include <pbd/memento_command.h>
34 #include <pbd/stacktrace.h>
35
36 #include "streamview.h"
37 #include "audio_region_view.h"
38 #include "audio_time_axis.h"
39 #include "simplerect.h"
40 #include "simpleline.h"
41 #include "waveview.h"
42 #include "public_editor.h"
43 #include "audio_region_editor.h"
44 #include "region_gain_line.h"
45 #include "ghostregion.h"
46 #include "audio_time_axis.h"
47 #include "utils.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50 #include "ardour_ui.h"
51
52 #include "i18n.h"
53
54 #define MUTED_ALPHA 10
55
56 using namespace sigc;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Editing;
60 using namespace ArdourCanvas;
61
62 static const int32_t sync_mark_width = 9;
63
64 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
65                                   Gdk::Color& basic_color)
66         : RegionView (parent, tv, r, spu, basic_color)
67         , sync_mark(0)
68         , zero_line(0)
69         , fade_in_shape(0)
70         , fade_out_shape(0)
71         , fade_in_handle(0)
72         , fade_out_handle(0)
73         , gain_line(0)
74         , _amplitude_above_axis(1.0)
75         , _flags(0)
76         , fade_color(0)
77 {
78 }
79
80 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu, 
81                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
82         : RegionView (parent, tv, r, spu, basic_color, visibility)
83         , sync_mark(0)
84         , zero_line(0)
85         , fade_in_shape(0)
86         , fade_out_shape(0)
87         , fade_in_handle(0)
88         , fade_out_handle(0)
89         , gain_line(0)
90         , _amplitude_above_axis(1.0)
91         , _flags(0)
92         , fade_color(0)
93 {
94 }
95
96
97 AudioRegionView::AudioRegionView (const AudioRegionView& other)
98         : RegionView (other)
99         , zero_line(0)
100         , fade_in_shape(0)
101         , fade_out_shape(0)
102         , fade_in_handle(0)
103         , fade_out_handle(0)
104         , gain_line(0)
105         , _amplitude_above_axis(1.0)
106         , _flags(0)
107         , fade_color(0)
108
109 {
110         Gdk::Color c;
111         int r,g,b,a;
112
113         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
114         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
115         
116         init (c, true);
117 }
118
119 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
120         : RegionView (other, boost::shared_ptr<Region> (other_region))
121         , zero_line(0)
122         , fade_in_shape(0)
123         , fade_out_shape(0)
124         , fade_in_handle(0)
125         , fade_out_handle(0)
126         , gain_line(0)
127         , _amplitude_above_axis(1.0)
128         , _flags(0)
129         , fade_color(0)
130
131 {
132         Gdk::Color c;
133         int r,g,b,a;
134
135         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
136         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
137         
138         init (c, true);
139 }
140
141 void
142 AudioRegionView::init (Gdk::Color& basic_color, bool wfd)
143 {
144         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
145         // where order is important and where it isn't...
146         
147         RegionView::init (basic_color, wfd);
148         
149         XMLNode *node;
150
151         _amplitude_above_axis = 1.0;
152         zero_line             = 0;
153         _flags                = 0;
154
155         if ((node = _region->extra_xml ("GUI")) != 0) {
156                 set_flags (node);
157         } else {
158                 _flags = WaveformVisible;
159                 store_flags ();
160         }
161
162         if (trackview.editor.new_regionviews_display_gain()) {
163                 _flags |= EnvelopeVisible;
164         }
165
166         compute_colors (basic_color);
167         
168         create_waves ();
169
170         fade_in_shape = new ArdourCanvas::Polygon (*group);
171         fade_in_shape->property_fill_color_rgba() = fade_color;
172         fade_in_shape->set_data ("regionview", this);
173         
174         fade_out_shape = new ArdourCanvas::Polygon (*group);
175         fade_out_shape->property_fill_color_rgba() = fade_color;
176         fade_out_shape->set_data ("regionview", this);
177
178
179         {
180                 uint32_t r,g,b,a;
181                 UINT_TO_RGBA(fill_color,&r,&g,&b,&a);
182         
183
184                 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
185                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
186                 fade_in_handle->property_outline_pixels() = 0;
187                 fade_in_handle->property_y1() = 2.0;
188                 fade_in_handle->property_y2() = 7.0;
189                 
190                 fade_in_handle->set_data ("regionview", this);
191                 
192                 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
193                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
194                 fade_out_handle->property_outline_pixels() = 0;
195                 fade_out_handle->property_y1() = 2.0;
196                 fade_out_handle->property_y2() = 7.0;
197                 
198                 fade_out_handle->set_data ("regionview", this);
199         }
200
201         string foo = _region->name();
202         foo += ':';
203         foo += "gain";
204
205         if (!Profile->get_sae()) {
206                 gain_line = new AudioRegionGainLine (foo, trackview.session(), *this, *group, audio_region()->envelope());
207
208                 if (!(_flags & EnvelopeVisible)) {
209                         gain_line->hide ();
210                 } else {
211                         gain_line->show ();
212                 }
213
214                 gain_line->reset ();
215         }
216
217         set_height (trackview.height);
218
219         region_muted ();
220         region_sync_changed ();
221         region_resized (BoundsChanged);
222         set_waveview_data_src();
223         region_locked ();
224         envelope_active_changed ();
225         fade_in_active_changed ();
226         fade_out_active_changed ();
227
228         _region->StateChanged.connect (mem_fun(*this, &AudioRegionView::region_changed));
229
230         fade_in_shape->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_event), fade_in_shape, this));
231         fade_in_handle->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
232         fade_out_shape->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_event), fade_out_shape, this));
233         fade_out_handle->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
234
235         set_colors ();
236
237         /* XXX sync mark drag? */
238 }
239
240 AudioRegionView::~AudioRegionView ()
241 {
242         in_destructor = true;
243
244         RegionViewGoingAway (this); /* EMIT_SIGNAL */
245
246         for (vector<GnomeCanvasWaveViewCache *>::iterator cache = wave_caches.begin(); cache != wave_caches.end() ; ++cache) {
247                 gnome_canvas_waveview_cache_destroy (*cache);
248         }
249
250         /* all waveviews etc will be destroyed when the group is destroyed */
251
252         if (gain_line) {
253                 delete gain_line;
254         }
255 }
256
257 boost::shared_ptr<ARDOUR::AudioRegion>
258 AudioRegionView::audio_region() const
259 {
260         // "Guaranteed" to succeed...
261         return boost::dynamic_pointer_cast<AudioRegion>(_region);
262 }
263
264 void
265 AudioRegionView::region_changed (Change what_changed)
266 {
267         ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionView::region_changed), what_changed));
268
269         RegionView::region_changed(what_changed);
270
271         if (what_changed & AudioRegion::ScaleAmplitudeChanged) {
272                 region_scale_amplitude_changed ();
273         }
274         if (what_changed & AudioRegion::FadeInChanged) {
275                 fade_in_changed ();
276         }
277         if (what_changed & AudioRegion::FadeOutChanged) {
278                 fade_out_changed ();
279         }
280         if (what_changed & AudioRegion::FadeInActiveChanged) {
281                 fade_in_active_changed ();
282         }
283         if (what_changed & AudioRegion::FadeOutActiveChanged) {
284                 fade_out_active_changed ();
285         }
286         if (what_changed & AudioRegion::EnvelopeActiveChanged) {
287                 envelope_active_changed ();
288         }
289 }
290
291 void
292 AudioRegionView::fade_in_changed ()
293 {
294         reset_fade_in_shape ();
295 }
296
297 void
298 AudioRegionView::fade_out_changed ()
299 {
300         reset_fade_out_shape ();
301 }
302 void
303 AudioRegionView::fade_in_active_changed ()
304 {
305         uint32_t r,g,b,a;
306         uint32_t col;
307         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
308
309         if (audio_region()->fade_in_active()) {
310                 col = RGBA_TO_UINT(r,g,b,120);
311                 fade_in_shape->property_fill_color_rgba() = col;
312                 fade_in_shape->property_width_pixels() = 0;
313                 fade_in_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,0);
314         } else { 
315                 col = RGBA_TO_UINT(r,g,b,0);
316                 fade_in_shape->property_fill_color_rgba() = col;
317                 fade_in_shape->property_width_pixels() = 1;
318                 fade_in_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,255);
319         }
320 }
321
322 void
323 AudioRegionView::fade_out_active_changed ()
324 {
325         uint32_t r,g,b,a;
326         uint32_t col;
327         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
328
329         if (audio_region()->fade_out_active()) {
330                 col = RGBA_TO_UINT(r,g,b,120);
331                 fade_out_shape->property_fill_color_rgba() = col;
332                 fade_out_shape->property_width_pixels() = 0;
333                 fade_out_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,0);
334         } else { 
335                 col = RGBA_TO_UINT(r,g,b,0);
336                 fade_out_shape->property_fill_color_rgba() = col;
337                 fade_out_shape->property_width_pixels() = 1;
338                 fade_out_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,255);
339         }
340 }
341
342
343 void
344 AudioRegionView::region_scale_amplitude_changed ()
345 {
346         ENSURE_GUI_THREAD (mem_fun(*this, &AudioRegionView::region_scale_amplitude_changed));
347
348         for (uint32_t n = 0; n < waves.size(); ++n) {
349                 // force a reload of the cache
350                 waves[n]->property_data_src() = _region.get();
351         }
352 }
353
354 void
355 AudioRegionView::region_resized (Change what_changed)
356 {
357         RegionView::region_resized(what_changed);
358
359         if (what_changed & Change (StartChanged|LengthChanged)) {
360
361                 for (uint32_t n = 0; n < waves.size(); ++n) {
362                         waves[n]->property_region_start() = _region->start();
363                 }
364                 
365                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
366
367                         for (vector<WaveView*>::iterator w = (*i)->waves.begin(); w != (*i)->waves.end(); ++w) {
368                                 (*w)->property_region_start() = _region->start();
369                         }
370                 }
371         }
372 }
373
374 void
375 AudioRegionView::reset_width_dependent_items (double pixel_width)
376 {
377         RegionView::reset_width_dependent_items(pixel_width);
378         assert(_pixel_width == pixel_width);
379
380         if (zero_line) {
381                 zero_line->property_x2() = pixel_width - 1.0;
382         }
383
384         if (fade_in_handle) {
385                 if (pixel_width <= 6.0) {
386                         fade_in_handle->hide();
387                         fade_out_handle->hide();
388                 } else {
389                         if (_height < 5.0) {
390                                 fade_in_handle->hide();
391                                 fade_out_handle->hide();
392                         } else {
393                                 fade_in_handle->show();
394                                 fade_out_handle->show();
395                         }
396                 }
397         }
398
399         reset_fade_shapes ();
400 }
401
402 void
403 AudioRegionView::region_muted ()
404 {
405         RegionView::region_muted();
406
407         for (uint32_t n=0; n < waves.size(); ++n) {
408                 if (_region->muted()) {
409                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
410                 } else {
411                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
412                 }
413         }
414 }
415
416
417 void
418 AudioRegionView::set_height (gdouble height)
419 {
420         RegionView::set_height (height);
421
422         uint32_t wcnt = waves.size();
423
424         // FIXME: ick
425         TimeAxisViewItem::set_height (height - 2);
426         
427         _height = height;
428
429         for (uint32_t n=0; n < wcnt; ++n) {
430                 gdouble ht;
431
432                 if ((height) <= NAME_HIGHLIGHT_THRESH) {
433                         ht = ((height-2*wcnt) / (double) wcnt);
434                 } else {
435                         ht = (((height-2*wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
436                 }
437                 
438                 gdouble yoff = n * (ht+1);
439                 
440                 waves[n]->property_height() = ht;
441                 waves[n]->property_y() = yoff + 2;
442         }
443
444         if (gain_line) {
445                 if ((height/wcnt) < NAME_HIGHLIGHT_SIZE) {
446                         gain_line->hide ();
447                 } else {
448                         if (_flags & EnvelopeVisible) {
449                                 gain_line->show ();
450                         }
451                 }
452                 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE));
453         }
454
455         manage_zero_line ();
456         reset_fade_shapes ();
457         
458         if (name_text) {
459                 name_text->raise_to_top();
460         }
461 }
462
463 void
464 AudioRegionView::manage_zero_line ()
465 {
466         if (!zero_line) {
467                 return;
468         }
469
470         if (_height >= 100) {
471                 gdouble wave_midpoint = (_height - NAME_HIGHLIGHT_SIZE) / 2.0;
472                 zero_line->property_y1() = wave_midpoint;
473                 zero_line->property_y2() = wave_midpoint;
474                 zero_line->show();
475         } else {
476                 zero_line->hide();
477         }
478 }
479
480 void
481 AudioRegionView::reset_fade_shapes ()
482 {
483         reset_fade_in_shape ();
484         reset_fade_out_shape ();
485 }
486
487 void
488 AudioRegionView::reset_fade_in_shape ()
489 {
490         reset_fade_in_shape_width ((nframes_t) audio_region()->fade_in().back()->when);
491 }
492         
493 void
494 AudioRegionView::reset_fade_in_shape_width (nframes_t width)
495 {
496         if (fade_in_handle == 0) {
497                 return;
498         }
499
500         /* smallest size for a fade is 64 frames */
501
502         width = std::max ((nframes_t) 64, width);
503
504         Points* points;
505         double pwidth = width / samples_per_unit;
506         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
507         double h; 
508         
509         if (_height < 5) {
510                 fade_in_shape->hide();
511                 fade_in_handle->hide();
512                 return;
513         }
514
515         double handle_center;
516         handle_center = pwidth;
517         
518         if (handle_center > 7.0) {
519                 handle_center -= 3.0;
520         } else {
521                 handle_center = 3.0;
522         }
523
524         fade_in_handle->property_x1() =  handle_center - 3.0;
525         fade_in_handle->property_x2() =  handle_center + 3.0;
526         
527         if (pwidth < 5) {
528                 fade_in_shape->hide();
529                 return;
530         }
531
532         fade_in_shape->show();
533
534         float curve[npoints];
535         audio_region()->fade_in().get_vector (0, audio_region()->fade_in().back()->when, curve, npoints);
536
537         points = get_canvas_points ("fade in shape", npoints+3);
538
539         if (_height > NAME_HIGHLIGHT_THRESH) {
540                 h = _height - NAME_HIGHLIGHT_SIZE;
541         } else {
542                 h = _height;
543         }
544
545         /* points *MUST* be in anti-clockwise order */
546
547         uint32_t pi, pc;
548         double xdelta = pwidth/npoints;
549
550         for (pi = 0, pc = 0; pc < npoints; ++pc) {
551                 (*points)[pi].set_x(1 + (pc * xdelta));
552                 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
553         }
554         
555         /* fold back */
556
557         (*points)[pi].set_x(pwidth);
558         (*points)[pi++].set_y(2);
559
560         (*points)[pi].set_x(1);
561         (*points)[pi++].set_y(2);
562
563         /* connect the dots ... */
564
565         (*points)[pi] = (*points)[0];
566         
567         fade_in_shape->property_points() = *points;
568         delete points;
569 }
570
571 void
572 AudioRegionView::reset_fade_out_shape ()
573 {
574         reset_fade_out_shape_width ((nframes_t) audio_region()->fade_out().back()->when);
575 }
576
577 void
578 AudioRegionView::reset_fade_out_shape_width (nframes_t width)
579 {       
580         if (fade_out_handle == 0) {
581                 return;
582         }
583
584         /* smallest size for a fade is 64 frames */
585
586         width = std::max ((nframes_t) 64, width);
587
588         Points* points;
589         double pwidth = width / samples_per_unit;
590         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
591         double h;
592
593         if (_height < 5) {
594                 fade_out_shape->hide();
595                 fade_out_handle->hide();
596                 return;
597         }
598
599         double handle_center;
600         handle_center = (_region->length() - width) / samples_per_unit;
601         
602         if (handle_center > 7.0) {
603                 handle_center -= 3.0;
604         } else {
605                 handle_center = 3.0;
606         }
607         
608         fade_out_handle->property_x1() =  handle_center - 3.0;
609         fade_out_handle->property_x2() =  handle_center + 3.0;
610
611         /* don't show shape if its too small */
612         
613         if (pwidth < 5) {
614                 fade_out_shape->hide();
615                 return;
616         } 
617         
618         fade_out_shape->show();
619
620         float curve[npoints];
621         audio_region()->fade_out().get_vector (0, audio_region()->fade_out().back()->when, curve, npoints);
622
623         if (_height > NAME_HIGHLIGHT_THRESH) {
624                 h = _height - NAME_HIGHLIGHT_SIZE;
625         } else {
626                 h = _height;
627         }
628
629         /* points *MUST* be in anti-clockwise order */
630
631         points = get_canvas_points ("fade out shape", npoints+3);
632
633         uint32_t pi, pc;
634         double xdelta = pwidth/npoints;
635
636         for (pi = 0, pc = 0; pc < npoints; ++pc) {
637                 (*points)[pi].set_x(_pixel_width - 1 - pwidth + (pc*xdelta));
638                 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
639         }
640         
641         /* fold back */
642
643         (*points)[pi].set_x(_pixel_width);
644         (*points)[pi++].set_y(h);
645
646         (*points)[pi].set_x(_pixel_width);
647         (*points)[pi++].set_y(2);
648
649         /* connect the dots ... */
650
651         (*points)[pi] = (*points)[0];
652
653         fade_out_shape->property_points() = *points;
654         delete points;
655 }
656
657 void
658 AudioRegionView::set_samples_per_unit (gdouble spu)
659 {
660         RegionView::set_samples_per_unit (spu);
661
662         if (_flags & WaveformVisible) {
663                 for (uint32_t n=0; n < waves.size(); ++n) {
664                         waves[n]->property_samples_per_unit() = spu;
665                 }
666         }
667
668         if (gain_line) {
669                 gain_line->reset ();
670         }
671
672         reset_fade_shapes ();
673 }
674
675 void
676 AudioRegionView::set_amplitude_above_axis (gdouble spp)
677 {
678         for (uint32_t n=0; n < waves.size(); ++n) {
679                 waves[n]->property_amplitude_above_axis() = spp;
680         }
681 }
682
683 void
684 AudioRegionView::compute_colors (Gdk::Color& basic_color)
685 {
686         RegionView::compute_colors(basic_color);
687         
688         uint32_t r, g, b, a;
689
690         /* gain color computed in envelope_active_changed() */
691
692         UINT_TO_RGBA (fill_color, &r, &g, &b, &a);
693         fade_color = RGBA_TO_UINT(r,g,b,120);
694 }
695
696 void
697 AudioRegionView::set_colors ()
698 {
699         RegionView::set_colors();
700         
701         if (gain_line) {
702                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
703         }
704
705         for (uint32_t n=0; n < waves.size(); ++n) {
706                 if (_region->muted()) {
707                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
708                 } else {
709                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
710                 }
711
712                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
713                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
714         }
715 }
716
717 void
718 AudioRegionView::show_region_editor ()
719 {
720         if (editor == 0) {
721                 editor = new AudioRegionEditor (trackview.session(), audio_region(), *this);
722                 // GTK2FIX : how to ensure float without realizing
723                 // editor->realize ();
724                 // trackview.editor.ensure_float (*editor);
725         } 
726
727         editor->present ();
728         editor->show_all();
729 }
730
731 void
732 AudioRegionView::set_waveform_visible (bool yn)
733 {
734         if (((_flags & WaveformVisible) != yn)) {
735                 if (yn) {
736                         for (uint32_t n=0; n < waves.size(); ++n) {
737                                 /* make sure the zoom level is correct, since we don't update
738                                    this when waveforms are hidden.
739                                 */
740                                 waves[n]->property_samples_per_unit() = samples_per_unit;
741                                 waves[n]->show();
742                         }
743                         _flags |= WaveformVisible;
744                 } else {
745                         for (uint32_t n=0; n < waves.size(); ++n) {
746                                 waves[n]->hide();
747                         }
748                         _flags &= ~WaveformVisible;
749                 }
750                 store_flags ();
751         }
752 }
753
754 void
755 AudioRegionView::temporarily_hide_envelope ()
756 {
757         if (gain_line) {
758                 gain_line->hide ();
759         }
760 }
761
762 void
763 AudioRegionView::unhide_envelope ()
764 {
765         if (gain_line && (_flags & EnvelopeVisible)) {
766                 gain_line->show ();
767         }
768 }
769
770 void
771 AudioRegionView::set_envelope_visible (bool yn)
772 {
773         if (gain_line && ((_flags & EnvelopeVisible) != yn)) {
774                 if (yn) {
775                         gain_line->show ();
776                         _flags |= EnvelopeVisible;
777                 } else {
778                         gain_line->hide ();
779                         _flags &= ~EnvelopeVisible;
780                 }
781                 store_flags ();
782         }
783 }
784
785 void
786 AudioRegionView::create_waves ()
787 {
788         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
789         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
790
791         if (!atv.get_diskstream()) {
792                 return;
793         }
794
795         uint32_t nchans = atv.get_diskstream()->n_channels();
796
797         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data << " and channels = " << nchans << endl;
798         
799         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
800         for (uint32_t n = 0; n < nchans; ++n) {
801                 tmp_waves.push_back (0);
802         }
803
804         for (uint32_t n = 0; n < nchans; ++n) {
805                 
806                 if (n >= audio_region()->n_channels()) {
807                         break;
808                 }
809                 
810                 wave_caches.push_back (WaveView::create_cache ());
811
812                 // cerr << "\tchannel " << n << endl;
813
814                 if (wait_for_data) {
815                         if (audio_region()->source(n)->peaks_ready (bind (mem_fun(*this, &AudioRegionView::peaks_ready_handler), n), data_ready_connection)) {
816                                 // cerr << "\tData is ready\n";
817                                 create_one_wave (n, true);
818                         } else {
819                                 // cerr << "\tdata is not ready\n";
820                                 // we'll get a PeaksReady signal from the source in the future
821                                 // and will call create_one_wave(n) then.
822                         }
823                         
824                 } else {
825                         // cerr << "\tdon't delay, display today!\n";
826                         create_one_wave (n, true);
827                 }
828
829         }
830 }
831
832 void
833 AudioRegionView::create_one_wave (uint32_t which, bool direct)
834 {
835         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
836         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
837         uint32_t nchans = atv.get_diskstream()->n_channels();
838         uint32_t n;
839         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
840         gdouble ht;
841
842         if (trackview.height < NAME_HIGHLIGHT_SIZE) {
843                 ht = ((trackview.height) / (double) nchans);
844         } else {
845                 ht = ((trackview.height - NAME_HIGHLIGHT_SIZE) / (double) nchans);
846         }
847
848         gdouble yoff = which * ht;
849
850         WaveView *wave = new WaveView(*group);
851
852         wave->property_data_src() = (gpointer) _region.get();
853         wave->property_cache() =  wave_caches[which];
854         wave->property_cache_updater() = true;
855         wave->property_channel() =  which;
856         wave->property_length_function() = (gpointer) region_length_from_c;
857         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
858         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
859         wave->property_x() =  0.0;
860         wave->property_y() =  yoff;
861         wave->property_height() =  (double) ht;
862         wave->property_samples_per_unit() =  samples_per_unit;
863         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
864         wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
865         wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
866         wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
867         wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
868         wave->property_region_start() = _region->start();
869         wave->property_rectified() = (bool) (_flags & WaveformRectified);
870         wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
871
872         if (!(_flags & WaveformVisible)) {
873                 wave->hide();
874         }
875
876         /* note: calling this function is serialized by the lock
877            held in the peak building thread that signals that
878            peaks are ready for use *or* by the fact that it is
879            called one by one from the GUI thread.
880         */
881
882         if (which < nchans) {
883                 tmp_waves[which] = wave;
884         } else {
885                 /* n-channel track, >n-channel source */
886         }
887         
888         /* see if we're all ready */
889         
890         for (n = 0; n < nchans; ++n) {
891                 if (tmp_waves[n] == 0) {
892                         break;
893                 }
894         }
895
896         if (n == nwaves && waves.empty()) {
897                 /* all waves are ready */
898                 tmp_waves.resize(nwaves);
899
900                 waves = tmp_waves;
901                 tmp_waves.clear ();
902
903                 /* all waves created, don't hook into peaks ready anymore */
904                 data_ready_connection.disconnect ();            
905
906                 if (!zero_line) {
907                         zero_line = new ArdourCanvas::SimpleLine (*group);
908                         zero_line->property_x1() = (gdouble) 1.0;
909                         zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
910                         zero_line->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
911                         manage_zero_line ();
912                 }
913         }
914 }
915
916 void
917 AudioRegionView::peaks_ready_handler (uint32_t which)
918 {
919         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &AudioRegionView::create_one_wave), which, false));
920         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
921 }
922
923 void
924 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
925 {
926         if (gain_line == 0) {
927                 return;
928         }
929
930         double x, y;
931
932         /* don't create points that can't be seen */
933
934         set_envelope_visible (true);
935         
936         x = ev->button.x;
937         y = ev->button.y;
938
939         item->w2i (x, y);
940
941         nframes_t fx = trackview.editor.pixel_to_frame (x);
942
943         if (fx > _region->length()) {
944                 return;
945         }
946
947         /* compute vertical fractional position */
948
949         y = 1.0 - (y / (trackview.height - NAME_HIGHLIGHT_SIZE));
950         
951         /* map using gain line */
952
953         gain_line->view_to_model_y (y);
954
955         trackview.session().begin_reversible_command (_("add gain control point"));
956         XMLNode &before = audio_region()->envelope().get_state();
957
958         if (!audio_region()->envelope_active()) {
959                 XMLNode &region_before = audio_region()->get_state();
960                 audio_region()->set_envelope_active(true);
961                 XMLNode &region_after = audio_region()->get_state();
962                 trackview.session().add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
963         }
964
965         audio_region()->envelope().add (fx, y);
966         
967         XMLNode &after = audio_region()->envelope().get_state();
968         trackview.session().add_command (new MementoCommand<Curve>(audio_region()->envelope(), &before, &after));
969         trackview.session().commit_reversible_command ();
970 }
971
972 void
973 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
974 {
975         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
976         audio_region()->envelope().erase (cp->model);
977 }
978
979 void
980 AudioRegionView::store_flags()
981 {
982         XMLNode *node = new XMLNode ("GUI");
983
984         node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
985         node->add_property ("envelope-visible", (_flags & EnvelopeVisible) ? "yes" : "no");
986         node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
987         node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
988
989         _region->add_extra_xml (*node);
990 }
991
992 void
993 AudioRegionView::set_flags (XMLNode* node)
994 {
995         XMLProperty *prop;
996
997         if ((prop = node->property ("waveform-visible")) != 0) {
998                 if (prop->value() == "yes") {
999                         _flags |= WaveformVisible;
1000                 }
1001         }
1002
1003         if ((prop = node->property ("envelope-visible")) != 0) {
1004                 if (prop->value() == "yes") {
1005                         _flags |= EnvelopeVisible;
1006                 }
1007         }
1008
1009         if ((prop = node->property ("waveform-rectified")) != 0) {
1010                 if (prop->value() == "yes") {
1011                         _flags |= WaveformRectified;
1012                 }
1013         }
1014
1015         if ((prop = node->property ("waveform-logscaled")) != 0) {
1016                 if (prop->value() == "yes") {
1017                         _flags |= WaveformLogScaled;
1018                 }
1019         }
1020 }
1021         
1022 void
1023 AudioRegionView::set_waveform_shape (WaveformShape shape)
1024 {
1025         bool yn;
1026
1027         /* this slightly odd approach is to leave the door open to 
1028            other "shapes" such as spectral displays, etc.
1029         */
1030
1031         switch (shape) {
1032         case Rectified:
1033                 yn = true;
1034                 break;
1035
1036         default:
1037                 yn = false;
1038                 break;
1039         }
1040
1041         if (yn != (bool) (_flags & WaveformRectified)) {
1042                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1043                         (*wave)->property_rectified() = yn;
1044                 }
1045
1046                 if (zero_line) {
1047                         if (yn) {
1048                                 zero_line->hide();
1049                         } else {
1050                                 zero_line->show();
1051                         }
1052                 }
1053
1054                 if (yn) {
1055                         _flags |= WaveformRectified;
1056                 } else {
1057                         _flags &= ~WaveformRectified;
1058                 }
1059                 store_flags ();
1060         }
1061 }
1062
1063 void
1064 AudioRegionView::set_waveform_scale (WaveformScale scale)
1065 {
1066         bool yn = (scale == LogWaveform);
1067
1068         if (yn != (bool) (_flags & WaveformLogScaled)) {
1069                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1070                         (*wave)->property_logscaled() = yn;
1071                 }
1072
1073                 if (yn) {
1074                         _flags |= WaveformLogScaled;
1075                 } else {
1076                         _flags &= ~WaveformLogScaled;
1077                 }
1078                 store_flags ();
1079         }
1080 }
1081
1082
1083 GhostRegion*
1084 AudioRegionView::add_ghost (AutomationTimeAxisView& atv)
1085 {
1086         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1087         assert(rtv);
1088
1089         double unit_position = _region->position () / samples_per_unit;
1090         GhostRegion* ghost = new GhostRegion (atv, unit_position);
1091         uint32_t nchans;
1092         
1093         nchans = rtv->get_diskstream()->n_channels();
1094
1095         for (uint32_t n = 0; n < nchans; ++n) {
1096                 
1097                 if (n >= audio_region()->n_channels()) {
1098                         break;
1099                 }
1100                 
1101                 WaveView *wave = new WaveView(*ghost->group);
1102
1103                 wave->property_data_src() = _region.get();
1104                 wave->property_cache() =  wave_caches[n];
1105                 wave->property_cache_updater() = false;
1106                 wave->property_channel() = n;
1107                 wave->property_length_function() = (gpointer)region_length_from_c;
1108                 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1109                 wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
1110                 wave->property_x() =  0.0;
1111                 wave->property_samples_per_unit() =  samples_per_unit;
1112                 wave->property_amplitude_above_axis() =  _amplitude_above_axis;
1113                 wave->property_wave_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWave.get();
1114                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWave.get();
1115                 wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWaveClip.get();
1116                 wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_GhostTrackZeroLine.get();
1117                 wave->property_region_start() = _region->start();
1118
1119                 ghost->waves.push_back(wave);
1120         }
1121
1122         ghost->set_height ();
1123         ghost->set_duration (_region->length() / samples_per_unit);
1124         ghosts.push_back (ghost);
1125
1126         ghost->GoingAway.connect (mem_fun(*this, &AudioRegionView::remove_ghost));
1127
1128         return ghost;
1129 }
1130
1131 void
1132 AudioRegionView::entered ()
1133 {
1134         if (gain_line && _flags & EnvelopeVisible) {
1135                 gain_line->show_all_control_points ();
1136         }
1137
1138         uint32_t r,g,b,a;
1139         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1140         a=255;
1141         
1142         if (fade_in_handle) {
1143                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1144                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1145         }
1146 }
1147
1148 void
1149 AudioRegionView::exited ()
1150 {
1151         if (gain_line) {
1152                 gain_line->hide_all_but_selected_control_points ();
1153         }
1154         
1155         uint32_t r,g,b,a;
1156         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1157         a=0;
1158         
1159         if (fade_in_handle) {
1160                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1161                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1162         }
1163 }
1164
1165 void
1166 AudioRegionView::envelope_active_changed ()
1167 {
1168         if (gain_line) {
1169                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1170         }
1171 }
1172
1173 void
1174 AudioRegionView::set_waveview_data_src()
1175 {
1176
1177         double unit_length= _region->length() / samples_per_unit;
1178
1179         for (uint32_t n = 0; n < waves.size(); ++n) {
1180                 // TODO: something else to let it know the channel
1181                 waves[n]->property_data_src() = _region.get();
1182         }
1183         
1184         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1185                 
1186                 (*i)->set_duration (unit_length);
1187                 
1188                 for (vector<WaveView*>::iterator w = (*i)->waves.begin(); w != (*i)->waves.end(); ++w) {
1189                         (*w)->property_data_src() = _region.get();
1190                 }
1191         }
1192
1193 }
1194
1195 void
1196 AudioRegionView::color_handler ()
1197 {
1198         set_colors ();
1199         envelope_active_changed();
1200 }
1201
1202 void
1203 AudioRegionView::set_frame_color ()
1204 {
1205         if (!frame) {
1206                 return;
1207         }
1208
1209         if (_region->opaque()) {
1210                 fill_opacity = 130;
1211         } else {
1212                 fill_opacity = 0;
1213         }
1214
1215         uint32_t r,g,b,a;
1216         
1217         if (_selected && should_show_selection) {
1218                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get(), &r, &g, &b, &a);
1219                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1220
1221                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1222                         if (_region->muted()) {
1223                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1224                         } else {
1225                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1226                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1227                         }
1228                 }
1229         } else {
1230                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_FrameBase.get(), &r, &g, &b, &a);
1231                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1232
1233                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1234                         if (_region->muted()) {
1235                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1236                         } else {
1237                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1238                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1239                         }
1240                 }
1241         }
1242 }