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