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