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