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