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