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