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