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