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