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