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