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