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