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