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