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