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