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