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