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