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