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