hopefully fix up selection of color for region name text, under all conditions
[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 pi;
608         boost::shared_ptr<const Evoral::ControlList> list (audio_region()->fade_in());
609         Evoral::ControlList::const_iterator x;
610         double length = list->length();
611
612         points.assign (list->size(), Duple());
613
614         for (x = list->begin(), pi = 0; x != list->end(); ++x, ++pi) {
615                 points[pi].x = 1.0 + (pwidth * ((*x)->when/length));
616                 points[pi].y = effective_height - ((*x)->value * effective_height);
617         }
618
619         /* draw the line */
620
621         redraw_start_xfade_to (ar, width, points, effective_height, handle_left);
622
623         /* ensure trim handle stays on top */
624         if (frame_handle_start) {
625                 frame_handle_start->raise_to_top();
626         }
627 }
628
629 void
630 AudioRegionView::reset_fade_out_shape ()
631 {
632         reset_fade_out_shape_width (audio_region(), (framecnt_t) audio_region()->fade_out()->back()->when);
633 }
634
635 void
636 AudioRegionView::reset_fade_out_shape_width (boost::shared_ptr<AudioRegion> ar, framecnt_t width, bool drag_active)
637 {
638         trim_fade_out_drag_active = drag_active;
639         if (fade_out_handle == 0) {
640                 return;
641         }
642
643         /* smallest size for a fade is 64 frames */
644
645         width = std::max ((framecnt_t) 64, width);
646
647         double const pwidth = rint(trackview.editor().sample_to_pixel (width));
648
649         /* the right edge should be right on the region frame is the pixel
650          * width is zero. Hence the additional + 1.0 at the end.
651          */
652
653         double const handle_right = rint(trackview.editor().sample_to_pixel (_region->length()) + TimeAxisViewItem::RIGHT_EDGE_SHIFT - pwidth);
654         double const trim_handle_right = rint(trackview.editor().sample_to_pixel (_region->length()) + TimeAxisViewItem::RIGHT_EDGE_SHIFT);
655
656         /* Put the fade out handle so that its right side is at the end-of-fade line;
657          */
658         fade_out_handle->set_x0 (1 + handle_right - handle_size);
659         fade_out_handle->set_x1 (1 + handle_right);
660         if (fade_out_trim_handle) {
661                 fade_out_trim_handle->set_x0 (1 + trim_handle_right - handle_size);
662                 fade_out_trim_handle->set_x1 (1 + trim_handle_right);
663         }
664
665         if (fade_out_handle->visible()) {
666                 //see comment for drag_start
667                 entered(false);
668         }
669         /* don't show shape if its too small */
670
671         if (pwidth < 5) {
672                 hide_end_xfade();
673                 return;
674         }
675
676         if (!trackview.session()->config.get_show_region_fades()) {
677                 hide_end_xfade();
678                 return;
679         }
680
681         double effective_height;
682
683         if (_height >= NAME_HIGHLIGHT_THRESH) {
684                 effective_height = _height - NAME_HIGHLIGHT_SIZE;
685         } else {
686                 effective_height = _height;
687         }
688
689         /* points *MUST* be in anti-clockwise order */
690         
691         Points points;
692         Points::size_type pi;
693         boost::shared_ptr<const Evoral::ControlList> list (audio_region()->fade_out());
694         Evoral::ControlList::const_iterator x;
695         double length = list->length();
696
697         points.assign (list->size(), Duple());
698
699         for (x = list->begin(), pi = 0; x != list->end(); ++x, ++pi) {
700                 points[pi].x = 1.0 + _pixel_width - pwidth + (pwidth * ((*x)->when/length));
701                 points[pi].y = effective_height - ((*x)->value * effective_height);
702         }
703
704         /* draw the line */
705
706         redraw_end_xfade_to (ar, width, points, effective_height, handle_right, pwidth);
707
708         /* ensure trim handle stays on top */
709         if (frame_handle_end) {
710                 frame_handle_end->raise_to_top();
711         }
712 }
713
714 framepos_t
715 AudioRegionView::get_fade_in_shape_width ()
716 {
717         return audio_region()->fade_in()->back()->when;
718 }
719
720 framepos_t
721 AudioRegionView::get_fade_out_shape_width ()
722 {
723         return audio_region()->fade_out()->back()->when;
724 }
725
726
727 void
728 AudioRegionView::redraw_start_xfade ()
729 {
730         boost::shared_ptr<AudioRegion> ar (audio_region());
731
732         if (!ar->fade_in() || ar->fade_in()->empty()) {
733                 return;
734         }
735
736         show_start_xfade();
737         reset_fade_in_shape_width (ar, ar->fade_in()->back()->when);
738 }
739
740 void
741 AudioRegionView::redraw_start_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t /*width*/, Points& points, double effective_height,
742                                         double rect_width)
743 {
744         if (points.size() < 2) {
745                 return;
746         }
747
748         if (!start_xfade_curve) {
749                 start_xfade_curve = new ArdourCanvas::XFadeCurve (group, ArdourCanvas::XFadeCurve::Start);
750                 CANVAS_DEBUG_NAME (start_xfade_curve, string_compose ("xfade start out line for %1", region()->name()));
751                 start_xfade_curve->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
752                 start_xfade_curve->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
753                 start_xfade_curve->set_ignore_events (true);
754         }
755         if (!start_xfade_rect) {
756                 start_xfade_rect = new ArdourCanvas::Rectangle (group);
757                 CANVAS_DEBUG_NAME (start_xfade_rect, string_compose ("xfade start rect for %1", region()->name()));
758                 start_xfade_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
759                 start_xfade_rect->set_fill (false);
760                 start_xfade_rect->set_outline (false);
761                 start_xfade_rect->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::RIGHT));
762                 start_xfade_rect->set_outline_width (0.5);
763                 start_xfade_rect->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_start_xfade_event), start_xfade_rect, this));
764                 start_xfade_rect->set_data ("regionview", this);
765         }
766
767         start_xfade_rect->set (ArdourCanvas::Rect (0.0, 0.0, rect_width, effective_height));
768
769         /* fade out line */
770
771         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_in ();
772         Points ipoints;
773         Points::size_type npoints;
774
775         if (!inverse) {
776
777                 /* there is no explicit inverse fade in curve, so take the
778                  * regular fade in curve given to use as "points" (already a
779                  * set of coordinates), and convert to the inverse shape.
780                  */
781
782                 npoints = points.size();
783                 ipoints.assign (npoints, Duple());
784
785                 for (Points::size_type i = 0, pci = 0; i < npoints; ++i, ++pci) {
786                         ArdourCanvas::Duple &p (ipoints[pci]);
787                         /* leave x-axis alone but invert with respect to y-axis */
788                         p.y = effective_height - points[pci].y;
789                 }
790
791         } else {
792
793                 /* there is an explicit inverse fade in curve. Grab the points
794                    and convert them into coordinates for the inverse fade in
795                    line.
796                 */
797
798                 npoints = inverse->size();
799                 ipoints.assign (npoints, Duple());
800                 
801                 Evoral::ControlList::const_iterator x;
802                 Points::size_type pi;
803                 double length = inverse->length();
804
805                 for (x = inverse->begin(), pi = 0; x != inverse->end(); ++x, ++pi) {
806                         ArdourCanvas::Duple& p (ipoints[pi]);
807                         p.x = 1.0 + (rect_width * ((*x)->when/length));
808                         p.y = effective_height - ((*x)->value * effective_height);
809                 }
810         }
811
812         start_xfade_curve->set_inout (points, ipoints);
813
814         show_start_xfade();
815 }
816
817 void
818 AudioRegionView::redraw_end_xfade ()
819 {
820         boost::shared_ptr<AudioRegion> ar (audio_region());
821
822         if (!ar->fade_out() || ar->fade_out()->empty()) {
823                 return;
824         }
825
826         show_end_xfade();
827         
828         reset_fade_out_shape_width (ar, ar->fade_out()->back()->when);
829 }
830
831 void
832 AudioRegionView::redraw_end_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t width, Points& points, double effective_height,
833                                       double rect_edge, double rect_width)
834 {
835         if (points.size() < 2) {
836                 return;
837         }
838
839         if (!end_xfade_curve) {
840                 end_xfade_curve = new ArdourCanvas::XFadeCurve (group, ArdourCanvas::XFadeCurve::End);
841                 CANVAS_DEBUG_NAME (end_xfade_curve, string_compose ("xfade end out line for %1", region()->name()));
842                 end_xfade_curve->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
843                 end_xfade_curve->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
844                 end_xfade_curve->set_ignore_events (true);
845         }
846
847         if (!end_xfade_rect) {
848                 end_xfade_rect = new ArdourCanvas::Rectangle (group);
849                 CANVAS_DEBUG_NAME (end_xfade_rect, string_compose ("xfade end rect for %1", region()->name()));
850                 end_xfade_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
851                 end_xfade_rect->set_fill (false);
852                 end_xfade_rect->set_outline (false);
853                 end_xfade_rect->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::LEFT));
854                 end_xfade_rect->set_outline_width (0.5);
855                 end_xfade_rect->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_end_xfade_event), end_xfade_rect, this));
856                 end_xfade_rect->set_data ("regionview", this);
857         }
858
859         end_xfade_rect->set (ArdourCanvas::Rect (rect_edge, 0.0, rect_edge + rect_width + TimeAxisViewItem::RIGHT_EDGE_SHIFT, effective_height));
860
861         /* fade in line */
862
863         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_out ();
864         Points ipoints;
865         Points::size_type npoints;
866
867         if (!inverse) {
868
869                 /* there is no explicit inverse fade out curve, so take the
870                  * regular fade out curve given to use as "points" (already a
871                  * set of coordinates), and convert to the inverse shape.
872                  */
873
874                 npoints = points.size();
875                 ipoints.assign (npoints, Duple());
876
877                 Points::size_type pci;
878
879                 for (pci = 0; pci < npoints; ++pci) {
880                         ArdourCanvas::Duple &p (ipoints[pci]);
881                         p.y = effective_height - points[pci].y;
882                 }
883
884         } else {
885
886                 /* there is an explicit inverse fade out curve. Grab the points
887                    and convert them into coordinates for the inverse fade out
888                    line.
889                 */
890
891                 npoints = inverse->size();
892                 ipoints.assign (npoints, Duple());
893                 
894                 const double rend = trackview.editor().sample_to_pixel (_region->length() - width);
895                 
896                 Evoral::ControlList::const_iterator x;
897                 Points::size_type i;
898                 Points::size_type pi;
899                 double length = inverse->length();
900
901                 for (x = inverse->begin(), i = 0, pi = 0; x != inverse->end(); ++x, ++pi, ++i) {
902                         ArdourCanvas::Duple& p (ipoints[pi]);
903                         p.x = 1.0 + (rect_width * ((*x)->when/length)) + rend;
904                         p.y = effective_height - ((*x)->value * effective_height);
905                 }
906         }
907
908         end_xfade_curve->set_inout (ipoints, points);
909
910         show_end_xfade();
911 }
912
913 void
914 AudioRegionView::hide_xfades ()
915 {
916         hide_start_xfade ();
917         hide_end_xfade ();
918 }
919
920 void
921 AudioRegionView::hide_start_xfade ()
922 {
923         if (start_xfade_curve) {
924                 start_xfade_curve->hide();
925         }
926         if (start_xfade_rect) {
927                 start_xfade_rect->hide ();
928         }
929
930         _start_xfade_visible = false;
931 }
932
933 void
934 AudioRegionView::hide_end_xfade ()
935 {
936         if (end_xfade_curve) {
937                 end_xfade_curve->hide();
938         }
939         if (end_xfade_rect) {
940                 end_xfade_rect->hide ();
941         }
942
943         _end_xfade_visible = false;
944 }
945
946 void
947 AudioRegionView::show_start_xfade ()
948 {
949         if (start_xfade_curve) {
950                 start_xfade_curve->show();
951         }
952         if (start_xfade_rect) {
953                 start_xfade_rect->show ();
954         }
955
956         _start_xfade_visible = true;
957 }
958
959 void
960 AudioRegionView::show_end_xfade ()
961 {
962         if (end_xfade_curve) {
963                 end_xfade_curve->show();
964         }
965         if (end_xfade_rect) {
966                 end_xfade_rect->show ();
967         }
968
969         _end_xfade_visible = true;
970 }
971
972 void
973 AudioRegionView::set_samples_per_pixel (gdouble fpp)
974 {
975         RegionView::set_samples_per_pixel (fpp);
976
977         if (Config->get_show_waveforms ()) {
978                 for (uint32_t n = 0; n < waves.size(); ++n) {
979                         waves[n]->set_samples_per_pixel (fpp);
980                 }
981         }
982
983         if (gain_line) {
984                 gain_line->reset ();
985         }
986
987         reset_fade_shapes ();
988 }
989
990 void
991 AudioRegionView::set_amplitude_above_axis (gdouble a)
992 {
993         for (uint32_t n=0; n < waves.size(); ++n) {
994                 waves[n]->set_amplitude_above_axis (a);
995         }
996 }
997
998 void
999 AudioRegionView::compute_colors (Gdk::Color const & basic_color)
1000 {
1001         RegionView::compute_colors (basic_color);
1002
1003         /* gain color computed in envelope_active_changed() */
1004
1005         fade_color = UINT_RGBA_CHANGE_A (fill_color, 120);
1006 }
1007
1008 void
1009 AudioRegionView::set_colors ()
1010 {
1011         RegionView::set_colors();
1012
1013         if (gain_line) {
1014                 gain_line->set_line_color (audio_region()->envelope_active() ? 
1015                                            ARDOUR_UI::config()->get_canvasvar_GainLine() : 
1016                                            ARDOUR_UI::config()->get_canvasvar_GainLineInactive());
1017         }
1018
1019         set_waveform_colors ();
1020
1021         if (start_xfade_curve) {
1022                 start_xfade_curve->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
1023                 start_xfade_curve->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
1024         }
1025         if (end_xfade_curve) {
1026                 end_xfade_curve->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
1027                 end_xfade_curve->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
1028         }
1029
1030         if (start_xfade_rect) {
1031                 start_xfade_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
1032         }
1033         if (end_xfade_rect) {
1034                 end_xfade_rect->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
1035         }
1036 }
1037
1038 void
1039 AudioRegionView::setup_waveform_visibility ()
1040 {
1041         if (Config->get_show_waveforms ()) {
1042                 for (uint32_t n = 0; n < waves.size(); ++n) {
1043                         /* make sure the zoom level is correct, since we don't update
1044                            this when waveforms are hidden.
1045                         */
1046                         // CAIROCANVAS
1047                         // waves[n]->set_samples_per_pixel (_samples_per_pixel);
1048                         waves[n]->show();
1049                 }
1050         } else {
1051                 for (uint32_t n = 0; n < waves.size(); ++n) {
1052                         waves[n]->hide();
1053                 }
1054         }
1055 }
1056
1057 void
1058 AudioRegionView::temporarily_hide_envelope ()
1059 {
1060         if (gain_line) {
1061                 gain_line->hide ();
1062         }
1063 }
1064
1065 void
1066 AudioRegionView::unhide_envelope ()
1067 {
1068         update_envelope_visibility ();
1069 }
1070
1071 void
1072 AudioRegionView::update_envelope_visibility ()
1073 {
1074         if (!gain_line) {
1075                 return;
1076         }
1077
1078         if (Config->get_show_region_gain() || trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1079                 gain_line->add_visibility (AutomationLine::Line);
1080         } else {
1081                 gain_line->hide ();
1082         }
1083 }
1084
1085 void
1086 AudioRegionView::create_waves ()
1087 {
1088         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
1089         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
1090
1091         if (!atv.track()) {
1092                 return;
1093         }
1094
1095         ChanCount nchans = atv.track()->n_channels();
1096
1097         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
1098         //              << " and channels = " << nchans.n_audio() << endl;
1099
1100         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
1101         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
1102                 tmp_waves.push_back (0);
1103         }
1104
1105         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
1106                 delete *i;
1107         }
1108
1109         _data_ready_connections.clear ();
1110
1111         for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
1112                 _data_ready_connections.push_back (0);
1113         }
1114
1115         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
1116
1117                 if (n >= audio_region()->n_channels()) {
1118                         break;
1119                 }
1120
1121                 // cerr << "\tchannel " << n << endl;
1122
1123                 if (wait_for_data) {
1124                         if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
1125                                 // cerr << "\tData is ready\n";
1126                                 create_one_wave (n, true);
1127                         } else {
1128                                 // cerr << "\tdata is not ready\n";
1129                                 // we'll get a PeaksReady signal from the source in the future
1130                                 // and will call create_one_wave(n) then.
1131                         }
1132
1133                 } else {
1134                         // cerr << "\tdon't delay, display today!\n";
1135                         create_one_wave (n, true);
1136                 }
1137
1138         }
1139 }
1140
1141 void
1142 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
1143 {
1144         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
1145         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
1146         uint32_t nchans = atv.track()->n_channels().n_audio();
1147         uint32_t n;
1148         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
1149         gdouble ht;
1150
1151         if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
1152                 ht = ((trackview.current_height()) / (double) nchans);
1153         } else {
1154                 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
1155         }
1156
1157         gdouble yoff = which * ht;
1158
1159         WaveView *wave = new WaveView (group, audio_region ());
1160         CANVAS_DEBUG_NAME (wave, string_compose ("wave view for chn %1 of %2", which, get_item_name()));
1161         
1162         wave->set_channel (which);
1163         wave->set_y_position (yoff);
1164         wave->set_height (ht);
1165         wave->set_samples_per_pixel (samples_per_pixel);
1166         wave->set_show_zero_line (true);
1167         
1168         switch (Config->get_waveform_shape()) {
1169         case Rectified:
1170                 wave->set_shape (WaveView::Rectified);
1171                 break;
1172         default:
1173                 wave->set_shape (WaveView::Normal);
1174         }
1175                 
1176         wave->set_logscaled (Config->get_waveform_scale() == Logarithmic);
1177
1178         if (!Config->get_show_waveforms ()) {
1179                 wave->hide();
1180         }
1181
1182         /* note: calling this function is serialized by the lock
1183            held in the peak building thread that signals that
1184            peaks are ready for use *or* by the fact that it is
1185            called one by one from the GUI thread.
1186         */
1187
1188         if (which < nchans) {
1189                 tmp_waves[which] = wave;
1190         } else {
1191                 /* n-channel track, >n-channel source */
1192         }
1193
1194         /* see if we're all ready */
1195
1196         for (n = 0; n < nchans; ++n) {
1197                 if (tmp_waves[n] == 0) {
1198                         break;
1199                 }
1200         }
1201
1202         if (n == nwaves && waves.empty()) {
1203                 /* all waves are ready */
1204                 tmp_waves.resize(nwaves);
1205
1206                 waves = tmp_waves;
1207                 tmp_waves.clear ();
1208
1209                 /* all waves created, don't hook into peaks ready anymore */
1210                 delete _data_ready_connections[which];
1211                 _data_ready_connections[which] = 0;
1212         }
1213 }
1214
1215 void
1216 AudioRegionView::peaks_ready_handler (uint32_t which)
1217 {
1218         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AudioRegionView::create_one_wave, this, which, false));
1219         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
1220 }
1221
1222 void
1223 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev, bool with_guard_points)
1224 {
1225         if (!gain_line) {
1226                 return;
1227         }
1228
1229         double x, y;
1230
1231         /* don't create points that can't be seen */
1232
1233         update_envelope_visibility ();
1234
1235         x = ev->button.x;
1236         y = ev->button.y;
1237
1238         item->canvas_to_item (x, y);
1239
1240         framepos_t fx = trackview.editor().pixel_to_sample (x);
1241
1242         if (fx > _region->length()) {
1243                 return;
1244         }
1245
1246         /* compute vertical fractional position */
1247
1248         y = 1.0 - (y / (_height - NAME_HIGHLIGHT_SIZE));
1249
1250         /* map using gain line */
1251
1252         gain_line->view_to_model_coord (x, y);
1253
1254         /* XXX STATEFUL: can't convert to stateful diff until we
1255            can represent automation data with it.
1256         */
1257
1258         trackview.session()->begin_reversible_command (_("add gain control point"));
1259         XMLNode &before = audio_region()->envelope()->get_state();
1260
1261         if (!audio_region()->envelope_active()) {
1262                 XMLNode &region_before = audio_region()->get_state();
1263                 audio_region()->set_envelope_active(true);
1264                 XMLNode &region_after = audio_region()->get_state();
1265                 trackview.session()->add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
1266         }
1267
1268         audio_region()->envelope()->add (fx, y, with_guard_points);
1269
1270         XMLNode &after = audio_region()->envelope()->get_state();
1271         trackview.session()->add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1272         trackview.session()->commit_reversible_command ();
1273 }
1274
1275 void
1276 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent* /*ev*/)
1277 {
1278         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1279         audio_region()->envelope()->erase (cp->model());
1280 }
1281
1282 GhostRegion*
1283 AudioRegionView::add_ghost (TimeAxisView& tv)
1284 {
1285         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1286         assert(rtv);
1287
1288         double unit_position = _region->position () / samples_per_pixel;
1289         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1290         uint32_t nchans;
1291
1292         nchans = rtv->track()->n_channels().n_audio();
1293
1294         for (uint32_t n = 0; n < nchans; ++n) {
1295
1296                 if (n >= audio_region()->n_channels()) {
1297                         break;
1298                 }
1299
1300                 WaveView *wave = new WaveView (ghost->group, audio_region());
1301                 CANVAS_DEBUG_NAME (wave, string_compose ("ghost wave for %1", get_item_name()));
1302
1303                 wave->set_channel (n);
1304                 wave->set_samples_per_pixel (samples_per_pixel);
1305                 wave->set_amplitude_above_axis (_amplitude_above_axis);
1306
1307                 ghost->waves.push_back(wave);
1308         }
1309
1310         ghost->set_height ();
1311         ghost->set_duration (_region->length() / samples_per_pixel);
1312         ghost->set_colors();
1313         ghosts.push_back (ghost);
1314
1315         return ghost;
1316 }
1317
1318 void
1319 AudioRegionView::entered (bool internal_editing)
1320 {
1321         trackview.editor().set_current_trimmable (_region);
1322         trackview.editor().set_current_movable (_region);
1323         
1324         if (gain_line) {
1325                 /* these may or may not be visible depending on mouse mode */
1326                 gain_line->add_visibility (AutomationLine::ControlPoints);
1327         }
1328
1329         if (!internal_editing) {
1330                 if (start_xfade_rect) {
1331                         start_xfade_rect->set_outline (true);
1332                 }
1333                 if (end_xfade_rect) {
1334                         end_xfade_rect->set_outline (true);
1335                 }
1336                 if (fade_in_handle) {
1337                         fade_in_handle->show ();
1338                         fade_in_handle->raise_to_top ();
1339                 }
1340                 if (fade_out_handle) {
1341                         fade_out_handle->show ();
1342                         fade_out_handle->raise_to_top ();
1343                 }
1344                 if (fade_in_trim_handle) {
1345                         boost::shared_ptr<AudioRegion> ar (audio_region());
1346                         if (!ar->locked() && (ar->fade_in()->back()->when > 64 || (ar->can_trim() & Trimmable::FrontTrimEarlier))) {
1347                                 fade_in_trim_handle->show ();
1348                                 fade_in_trim_handle->raise_to_top ();
1349                         } else {
1350                                 fade_in_trim_handle->hide ();
1351                         }
1352                 }
1353                 if (fade_out_trim_handle) {
1354                         boost::shared_ptr<AudioRegion> ar (audio_region());
1355                         if (!ar->locked() && (ar->fade_out()->back()->when > 64 || (ar->can_trim() & Trimmable::EndTrimLater))) {
1356                                 fade_out_trim_handle->show ();
1357                                 fade_out_trim_handle->raise_to_top ();
1358                         } else {
1359                                 fade_out_trim_handle->hide ();
1360                         }
1361                 }
1362         }
1363 }
1364
1365 void
1366 AudioRegionView::exited ()
1367 {
1368         trackview.editor().set_current_trimmable (boost::shared_ptr<Trimmable>());
1369         trackview.editor().set_current_movable (boost::shared_ptr<Movable>());
1370
1371         if (gain_line) {
1372                 gain_line->remove_visibility (AutomationLine::ControlPoints);
1373         }
1374
1375         if (fade_in_handle)       { fade_in_handle->hide(); }
1376         if (fade_out_handle)      { fade_out_handle->hide(); }
1377         if (fade_in_trim_handle)  { fade_in_trim_handle->hide(); }
1378         if (fade_out_trim_handle) { fade_out_trim_handle->hide(); }
1379         if (start_xfade_rect)     { start_xfade_rect->set_outline (false); }
1380         if (end_xfade_rect)       { end_xfade_rect->set_outline (false); }
1381 }
1382
1383 void
1384 AudioRegionView::envelope_active_changed ()
1385 {
1386         if (gain_line) {
1387                 gain_line->set_line_color (audio_region()->envelope_active() ? 
1388                                            ARDOUR_UI::config()->get_canvasvar_GainLine() : 
1389                                            ARDOUR_UI::config()->get_canvasvar_GainLineInactive());
1390                 update_envelope_visibility ();
1391         }
1392 }
1393
1394 void
1395 AudioRegionView::color_handler ()
1396 {
1397         //case cMutedWaveForm:
1398         //case cWaveForm:
1399         //case cWaveFormClip:
1400         //case cZeroLine:
1401         set_colors ();
1402
1403         //case cGainLineInactive:
1404         //case cGainLine:
1405         envelope_active_changed();
1406
1407 }
1408
1409 void
1410 AudioRegionView::set_waveform_colors ()
1411 {
1412         for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1413                 set_one_waveform_color (*w);
1414         }
1415 }
1416
1417 void
1418 AudioRegionView::set_one_waveform_color (ArdourCanvas::WaveView* wave)
1419 {
1420         ArdourCanvas::Color fill;
1421         ArdourCanvas::Color outline;
1422         
1423         if (_selected) {
1424                 if (_region->muted()) {
1425                         /* hide outline with zero alpha */
1426                         outline = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_SelectedWaveForm(), 0);
1427                         fill = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_SelectedWaveFormFill(), MUTED_ALPHA);
1428                 } else {
1429                         outline = ARDOUR_UI::config()->get_canvasvar_SelectedWaveForm();
1430                         fill = ARDOUR_UI::config()->get_canvasvar_SelectedWaveFormFill();
1431                 }
1432         } else {
1433                 if (_recregion) {
1434                         outline = ARDOUR_UI::config()->get_canvasvar_RecWaveForm();
1435                         fill = ARDOUR_UI::config()->get_canvasvar_RecWaveFormFill();
1436                 } else {
1437                         if (_region->muted()) {
1438                                 /* hide outline with zero alpha */
1439                                 outline = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_WaveForm(), 0); 
1440                                 fill = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_WaveFormFill(), MUTED_ALPHA);
1441                         } else {
1442                                 outline = ARDOUR_UI::config()->get_canvasvar_WaveForm();
1443                                 fill = ARDOUR_UI::config()->get_canvasvar_WaveFormFill();
1444                         }
1445                 }
1446         }
1447
1448         if (ARDOUR_UI::config()->get_color_regions_using_track_color()) {
1449
1450                 /* just use a slightly transparent version of the selected
1451                  * color so that some of the track color bleeds through
1452                  */
1453
1454                 double r, g, b, a;
1455                 ArdourCanvas::color_to_rgba (fill, r, g, b, a);
1456                 fill = ArdourCanvas::rgba_to_color (r, g, b, 0.85); /* magic number, not user controllable */
1457                 outline = ARDOUR_UI::config()->get_canvasvar_WaveForm();
1458         }
1459
1460         wave->set_fill_color (fill);
1461         wave->set_outline_color (outline);
1462         wave->set_clip_color (ARDOUR_UI::config()->get_canvasvar_WaveFormClip());
1463         wave->set_zero_color (ARDOUR_UI::config()->get_canvasvar_ZeroLine());
1464 }
1465
1466 void
1467 AudioRegionView::set_frame_color ()
1468 {
1469         if (!frame) {
1470                 return;
1471         }
1472
1473         if (_region->opaque()) {
1474                 fill_opacity = 130;
1475         } else {
1476                 fill_opacity = 0;
1477         }
1478
1479         TimeAxisViewItem::set_frame_color ();
1480
1481         set_waveform_colors ();
1482 }
1483
1484 void
1485 AudioRegionView::set_fade_visibility (bool yn)
1486 {
1487         if (yn) {
1488                 if (start_xfade_curve)    { start_xfade_curve->show (); }
1489                 if (end_xfade_curve)      { end_xfade_curve->show (); }
1490                 if (start_xfade_rect)     { start_xfade_rect->show (); }
1491                 if (end_xfade_rect)       { end_xfade_rect->show (); }
1492                 } else {
1493                 if (start_xfade_curve)    { start_xfade_curve->hide(); }
1494                 if (end_xfade_curve)      { end_xfade_curve->hide(); }
1495                 if (fade_in_handle)       { fade_in_handle->hide(); }
1496                 if (fade_out_handle)      { fade_out_handle->hide(); }
1497                 if (fade_in_trim_handle)  { fade_in_trim_handle->hide(); }
1498                 if (fade_out_trim_handle) { fade_out_trim_handle->hide(); }
1499                 if (start_xfade_rect)     { start_xfade_rect->hide (); }
1500                 if (end_xfade_rect)       { end_xfade_rect->hide (); }
1501                 if (start_xfade_rect)     { start_xfade_rect->set_outline (false); }
1502                 if (end_xfade_rect)       { end_xfade_rect->set_outline (false); }
1503         }
1504 }
1505
1506 void
1507 AudioRegionView::update_coverage_frames (LayerDisplay d)
1508 {
1509         RegionView::update_coverage_frames (d);
1510
1511         if (fade_in_handle)       { fade_in_handle->raise_to_top (); }
1512         if (fade_out_handle)      { fade_out_handle->raise_to_top (); }
1513         if (fade_in_trim_handle)  { fade_in_trim_handle->raise_to_top (); }
1514         if (fade_out_trim_handle) { fade_out_trim_handle->raise_to_top (); }
1515 }
1516
1517 void
1518 AudioRegionView::show_region_editor ()
1519 {
1520         if (editor == 0) {
1521                 editor = new AudioRegionEditor (trackview.session(), audio_region());
1522         }
1523
1524         editor->present ();
1525         editor->show_all();
1526 }
1527
1528 void
1529 AudioRegionView::transients_changed ()
1530 {
1531         AnalysisFeatureList analysis_features = _region->transients();
1532
1533         while (feature_lines.size() < analysis_features.size()) {
1534
1535                 ArdourCanvas::Line* canvas_item = new ArdourCanvas::Line(group);
1536                 CANVAS_DEBUG_NAME (canvas_item, string_compose ("transient group for %1", region()->name()));
1537
1538                 canvas_item->set (ArdourCanvas::Duple (-1.0, 2.0),
1539                                   ArdourCanvas::Duple (1.0, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
1540
1541                 canvas_item->raise_to_top ();
1542                 canvas_item->show ();
1543
1544                 canvas_item->set_data ("regionview", this);
1545                 canvas_item->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), canvas_item, this));
1546
1547                 feature_lines.push_back (make_pair(0, canvas_item));
1548         }
1549
1550         while (feature_lines.size() > analysis_features.size()) {
1551                 ArdourCanvas::Line* line = feature_lines.back().second;
1552                 feature_lines.pop_back ();
1553                 delete line;
1554         }
1555
1556         AnalysisFeatureList::const_iterator i;
1557         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1558
1559         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1560
1561                 float *pos = new float;
1562                 *pos = trackview.editor().sample_to_pixel (*i);
1563
1564                 (*l).second->set (
1565                         ArdourCanvas::Duple (*pos, 2.0),
1566                         ArdourCanvas::Duple (*pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1)
1567                         );
1568
1569                 (*l).second->set_data ("position", pos);
1570                 (*l).first = *i;
1571         }
1572 }
1573
1574 void
1575 AudioRegionView::update_transient(float /*old_pos*/, float new_pos)
1576 {
1577         /* Find frame at old pos, calulate new frame then update region transients*/
1578         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1579
1580         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1581
1582                 /* Line has been updated in drag so we compare to new_pos */
1583
1584                 float* pos = (float*) (*l).second->get_data ("position");
1585
1586                 if (rint(new_pos) == rint(*pos)) {
1587
1588                     framepos_t old_frame = (*l).first;
1589                     framepos_t new_frame = trackview.editor().pixel_to_sample (new_pos);
1590
1591                     _region->update_transient (old_frame, new_frame);
1592
1593                     break;
1594                 }
1595         }
1596 }
1597
1598 void
1599 AudioRegionView::remove_transient(float pos)
1600 {
1601         /* Find frame at old pos, calulate new frame then update region transients*/
1602         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1603
1604         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1605
1606                 /* Line has been updated in drag so we compare to new_pos */
1607                 float *line_pos = (float*) (*l).second->get_data ("position");
1608
1609                 if (rint(pos) == rint(*line_pos)) {
1610                     _region->remove_transient ((*l).first);
1611                     break;
1612                 }
1613         }
1614 }
1615
1616 void
1617 AudioRegionView::thaw_after_trim ()
1618 {
1619         RegionView::thaw_after_trim ();
1620         unhide_envelope ();
1621         drag_end ();
1622 }
1623
1624
1625 void
1626 AudioRegionView::show_xfades ()
1627 {
1628         show_start_xfade ();
1629         show_end_xfade ();
1630 }
1631
1632 void
1633 AudioRegionView::drag_start ()
1634 {
1635         TimeAxisViewItem::drag_start ();
1636
1637         //we used to hide xfades here.  I don't see the point with the new model, but we can re-implement if needed
1638 }
1639
1640 void
1641 AudioRegionView::drag_end ()
1642 {
1643         TimeAxisViewItem::drag_end ();
1644         //see comment for drag_start
1645
1646         if (fade_in_handle && fade_in_handle->visible()) {
1647                 // lenght of region or fade changed, re-check
1648                 // if fade_in_trim_handle or fade_out_trim_handle should
1649                 // be visible. -- If the fade_in_handle is visible
1650                 // we have focus and are not in internal edit mode.
1651                 entered(false);
1652         }
1653 }
1654
1655 void
1656 AudioRegionView::parameter_changed (string const & p)
1657 {
1658         if (p == "show-waveforms") {
1659                 setup_waveform_visibility ();
1660         }
1661 }