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