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