e84d25c9f3a9f2385cd53b748d5fe7891c4f7d3c
[ardour.git] / gtk2_ardour / region_view.cc
1 /*
2     Copyright (C) 2001-2006 Paul Davis
3
4     This program is free software; you can redistribute 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
20 #include <cmath>
21 #include <cassert>
22 #include <algorithm>
23
24 #include <gtkmm.h>
25
26 #include <gtkmm2ext/gtk_ui.h>
27
28 #include "ardour/playlist.h"
29 #include "ardour/audioregion.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/audio_diskstream.h"
32 #include "ardour/session.h"
33
34 #include "ardour_ui.h"
35 #include "global_signals.h"
36 #include "canvas-noevent-text.h"
37 #include "streamview.h"
38 #include "region_view.h"
39 #include "automation_region_view.h"
40 #include "route_time_axis.h"
41 #include "simplerect.h"
42 #include "simpleline.h"
43 #include "waveview.h"
44 #include "public_editor.h"
45 #include "region_editor.h"
46 #include "ghostregion.h"
47 #include "route_time_axis.h"
48 #include "ui_config.h"
49 #include "utils.h"
50 #include "rgb_macros.h"
51 #include "gui_thread.h"
52
53 #include "i18n.h"
54
55 using namespace std;
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace Editing;
59 using namespace Gtk;
60 using namespace ArdourCanvas;
61
62 static const int32_t sync_mark_width = 9;
63
64 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
65
66 RegionView::RegionView (ArdourCanvas::Group*              parent,
67                         TimeAxisView&                     tv,
68                         boost::shared_ptr<ARDOUR::Region> r,
69                         double                            spu,
70                         Gdk::Color const &                basic_color,
71                         bool                              automation)
72         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false, automation,
73                             TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
74                                                           TimeAxisViewItem::ShowNameHighlight| TimeAxisViewItem::ShowFrame))
75         , _region (r)
76         , sync_mark(0)
77         , sync_line(0)
78         , editor(0)
79         , current_visible_sync_position(0.0)
80         , valid(false)
81         , _enable_display(false)
82         , _pixel_width(1.0)
83         , in_destructor(false)
84         , wait_for_data(false)
85         , _silence_text (0)
86         , _time_converter(r->session().tempo_map(), r->position())
87 {
88         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
89 }
90
91 RegionView::RegionView (const RegionView& other)
92         : sigc::trackable(other)
93         , TimeAxisViewItem (other)
94         , _silence_text (0)
95         , _time_converter(other._time_converter)
96 {
97         /* derived concrete type will call init () */
98
99         _region = other._region;
100         current_visible_sync_position = other.current_visible_sync_position;
101         valid = false;
102         _pixel_width = other._pixel_width;
103
104         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
105 }
106
107 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
108         : sigc::trackable(other)
109         , TimeAxisViewItem (other)
110         , _silence_text (0)
111         , _time_converter(other._time_converter)
112 {
113         /* this is a pseudo-copy constructor used when dragging regions
114            around on the canvas.
115         */
116
117         /* derived concrete type will call init () */
118
119         _region = other_region;
120         current_visible_sync_position = other.current_visible_sync_position;
121         valid = false;
122         _pixel_width = other._pixel_width;
123
124         GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
125 }
126
127 RegionView::RegionView (ArdourCanvas::Group*         parent,
128                         TimeAxisView&                tv,
129                         boost::shared_ptr<ARDOUR::Region> r,
130                         double                       spu,
131                         Gdk::Color const &           basic_color,
132                         bool                         recording,
133                         TimeAxisViewItem::Visibility visibility)
134         : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
135         , _region (r)
136         , sync_mark(0)
137         , sync_line(0)
138         , editor(0)
139         , current_visible_sync_position(0.0)
140         , valid(false)
141         , _enable_display(false)
142         , _pixel_width(1.0)
143         , in_destructor(false)
144         , wait_for_data(false)
145         , _silence_text (0)
146         , _time_converter(r->session().tempo_map(), r->position())
147 {
148 }
149
150 void
151 RegionView::init (Gdk::Color const & basic_color, bool wfd)
152 {
153         editor        = 0;
154         valid         = true;
155         in_destructor = false;
156         wait_for_data = wfd;
157         sync_mark     = 0;
158         sync_line     = 0;
159         sync_mark     = 0;
160         sync_line     = 0;
161
162         compute_colors (basic_color);
163
164         if (name_highlight) {
165                 name_highlight->set_data ("regionview", this);
166                 name_highlight->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
167
168                 if (frame_handle_start) {
169                         frame_handle_start->set_data ("regionview", this);
170                         frame_handle_start->set_data ("isleft", (void*) 1);
171                         frame_handle_start->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
172                         frame_handle_start->raise_to_top();
173                 }
174
175                 if (frame_handle_end) {
176                         frame_handle_end->set_data ("regionview", this);
177                         frame_handle_end->set_data ("isleft", (void*) 0);
178                         frame_handle_end->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
179                         frame_handle_end->raise_to_top();
180                 }
181         }
182
183         if (name_pixbuf) {
184                 name_pixbuf->set_data ("regionview", this);
185                 name_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_pixbuf, this));
186         }
187
188         if (wfd) {
189                 _enable_display = true;
190         }
191
192         set_height (trackview.current_height());
193
194         _region->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed, this, _1), gui_context());
195         
196         group->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
197
198         set_colors ();
199
200         ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
201
202         /* XXX sync mark drag? */
203 }
204
205 RegionView::~RegionView ()
206 {
207         in_destructor = true;
208
209         for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
210                 delete *g;
211         }
212
213         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
214                 delete *i;
215         }
216
217         drop_silent_frames ();
218
219         delete editor;
220 }
221
222 void
223 RegionView::set_silent_frames (const AudioIntervalResult& silences, double threshold)
224 {
225         framecnt_t shortest = max_framecnt;
226         framecnt_t shortest_audible = max_framecnt;
227
228         /* remove old silent frames */
229         drop_silent_frames ();
230
231         if (silences.empty()) {
232                 return;
233         }
234
235         framepos_t start;
236         framepos_t end;
237         bool in_silence;
238         bool seen_audible = false;
239         AudioIntervalResult::const_iterator s;
240         uint32_t const color = ARDOUR_UI::config()->canvasvar_Silence.get();
241
242         start = _region->start();
243         s = silences.begin();
244
245         if (s->first == start) {
246                 /* segment starting at zero is silent */
247                 end = s->second;
248                 in_silence = true;
249         } else {
250                 /* segment starting at zero is audible, and begins at the start of the region in the source */
251                 end = s->first;
252                 in_silence = false;
253         }
254
255         while (start < _region->start() + _region->length()) {
256
257                 framecnt_t interval_duration = end - start;
258
259                 if (interval_duration > 0) {
260                         if (in_silence) {
261                                 
262                                 ArdourCanvas::SimpleRect* cr = new ArdourCanvas::SimpleRect (*group);
263                                 _silent_frames.push_back (cr);
264                                 
265                                 /* coordinates for the rect are relative to the regionview origin */
266                                 
267                                 cr->property_x1() = trackview.editor().frame_to_pixel (s->first - _region->start());
268                                 cr->property_x2() = trackview.editor().frame_to_pixel (s->second - _region->start());
269                                 cr->property_y1() = 1;
270                                 cr->property_y2() = _height - 2;
271                                 cr->property_outline_pixels() = 0;
272                                 cr->property_fill_color_rgba () = color;
273                                 
274                                 if (interval_duration < shortest) {
275                                         shortest = interval_duration;
276                                 }
277                                 
278                         } else if (interval_duration > 0) {
279                                 seen_audible = true;
280                                 if (interval_duration < shortest_audible) {
281                                         shortest_audible = interval_duration;
282                                 }
283                         }
284                         
285                         start = end;
286                         in_silence = !in_silence;
287                         ++s;
288                         
289                         if (s == silences.end()) {
290                                 end = _region->start() + _region->length();
291                         } else {
292                                 end = s->first;
293                         }
294                 }
295         }
296
297
298         _silence_text = new ArdourCanvas::NoEventText (*group);
299         _silence_text->property_font_desc() = *(get_font_for_style (N_("SilenceText")));
300         _silence_text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SilenceText.get();                                                
301         _silence_text->property_anchor() = ANCHOR_NW;
302         
303         /* both positions are relative to the region start offset in source */
304         
305         _silence_text->property_x() = trackview.editor().frame_to_pixel (silences.front().first - _region->start()) + 10.0;
306         _silence_text->property_y() = 20.0;
307         
308         double ms;
309         char const * sunits;
310         char const * noun;
311         
312         if (silences.size() > 1) {
313                 noun = _("silent segments");
314         } else {
315                 noun = _("silent segment");
316         }
317         
318         ms = (float) shortest/_region->session().frame_rate();
319         
320         /* ms are now in seconds */
321         
322         if (ms >= 60.0) {
323                 sunits = _("minutes");
324                 ms /= 60.0;
325         } else if (ms < 1.0) {
326                 sunits = _("msecs");
327                 ms *= 1000.0;
328         } else {
329                 sunits = _("secs");
330         }
331         
332         if (seen_audible) {
333                 /* ms are now in seconds */
334                 double ma = shortest_audible / _region->session().frame_rate();
335                 char const * aunits;
336                 
337                 if (ma >= 60.0) {
338                         aunits = _("minutes");
339                         ma /= 60.0;
340                 } else if (ma < 1.0) {
341                         aunits = _("msecs");
342                         ma *= 1000.0;
343                 } else {
344                         aunits = _("secs");
345                 }
346                 
347                 _silence_text->property_text() = string_compose (_("%1 %2, shortest = %3 %4\n  (shortest audible segment = %5 %6)"),
348                                                                  silences.size(), noun, 
349                                                                  ms, sunits, ma, aunits).c_str();
350         } else {
351                 _silence_text->property_text() = string_compose (_("%1 %2, shortest = %3 %4"),
352                                                                  silences.size(), noun, ms, sunits).c_str();
353         }
354
355
356 void
357 RegionView::hide_silent_frames ()
358 {
359         for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
360                 (*i)->hide ();
361         }
362         _silence_text->hide();
363 }
364
365 void
366 RegionView::drop_silent_frames ()
367 {
368         for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
369                 delete *i;
370         }
371         _silent_frames.clear ();
372
373         delete _silence_text;
374         _silence_text = 0;
375 }
376
377 gint
378 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
379 {
380         switch (ev->type) {
381         case GDK_BUTTON_RELEASE:
382                 static_cast<RegionView*>(arg)->lock_toggle ();
383                 return TRUE;
384                 break;
385         default:
386                 break;
387         }
388         return FALSE;
389 }
390
391 void
392 RegionView::lock_toggle ()
393 {
394         _region->set_locked (!_region->locked());
395 }
396
397 void
398 RegionView::region_changed (const PropertyChange& what_changed)
399 {
400         ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
401
402         if (what_changed.contains (ARDOUR::bounds_change)) {
403                 region_resized (what_changed);
404                 region_sync_changed ();
405         }
406         if (what_changed.contains (ARDOUR::Properties::muted)) {
407                 region_muted ();
408         }
409         if (what_changed.contains (ARDOUR::Properties::opaque)) {
410                 region_opacity ();
411         }
412         if (what_changed.contains (ARDOUR::Properties::name)) {
413                 region_renamed ();
414         }
415         if (what_changed.contains (ARDOUR::Properties::sync_position)) {
416                 region_sync_changed ();
417         }
418         if (what_changed.contains (ARDOUR::Properties::locked)) {
419                 region_locked ();
420         }
421 }
422
423 void
424 RegionView::region_locked ()
425 {
426         /* name will show locked status */
427         region_renamed ();
428 }
429
430 void
431 RegionView::region_resized (const PropertyChange& what_changed)
432 {
433         double unit_length;
434
435         if (what_changed.contains (ARDOUR::Properties::position)) {
436                 set_position (_region->position(), 0);
437                 _time_converter.set_origin_b (_region->position());
438         }
439
440         PropertyChange s_and_l;
441         s_and_l.add (ARDOUR::Properties::start);
442         s_and_l.add (ARDOUR::Properties::length);
443
444         if (what_changed.contains (s_and_l)) {
445
446                 set_duration (_region->length(), 0);
447
448                 unit_length = _region->length() / samples_per_unit;
449
450                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
451
452                         (*i)->set_duration (unit_length);
453
454                 }
455         }
456 }
457
458 void
459 RegionView::reset_width_dependent_items (double pixel_width)
460 {
461         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
462         _pixel_width = pixel_width;
463 }
464
465 void
466 RegionView::region_muted ()
467 {
468         set_frame_color ();
469         region_renamed ();
470 }
471
472 void
473 RegionView::region_opacity ()
474 {
475         set_frame_color ();
476 }
477
478 void
479 RegionView::raise_to_top ()
480 {
481         _region->raise_to_top ();
482 }
483
484 void
485 RegionView::lower_to_bottom ()
486 {
487         _region->lower_to_bottom ();
488 }
489
490 bool
491 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
492 {
493         double delta;
494         bool ret;
495
496         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
497                 return false;
498         }
499
500         if (ignored) {
501                 *ignored = delta;
502         }
503
504         if (delta) {
505                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
506                         (*i)->group->move (delta, 0.0);
507                 }
508         }
509
510         return ret;
511 }
512
513 void
514 RegionView::set_samples_per_unit (gdouble spu)
515 {
516         TimeAxisViewItem::set_samples_per_unit (spu);
517
518         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
519                 (*i)->set_samples_per_unit (spu);
520                 (*i)->set_duration (_region->length() / samples_per_unit);
521         }
522
523         region_sync_changed ();
524 }
525
526 bool
527 RegionView::set_duration (framecnt_t frames, void *src)
528 {
529         if (!TimeAxisViewItem::set_duration (frames, src)) {
530                 return false;
531         }
532
533         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
534                 (*i)->set_duration (_region->length() / samples_per_unit);
535         }
536
537         return true;
538 }
539
540 void
541 RegionView::set_colors ()
542 {
543         TimeAxisViewItem::set_colors ();
544
545         if (sync_mark) {
546                 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
547                 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
548         }
549 }
550
551 void
552 RegionView::set_frame_color ()
553 {
554         if (_region->opaque()) {
555                 fill_opacity = 130;
556         } else {
557                 fill_opacity = 60;
558         }
559
560         TimeAxisViewItem::set_frame_color ();
561 }
562
563 void
564 RegionView::fake_set_opaque (bool yn)
565 {
566        if (yn) {
567                fill_opacity = 130;
568        } else {
569                fill_opacity = 60;
570        }
571
572        set_frame_color ();
573 }
574
575 void
576 RegionView::show_region_editor ()
577 {
578         if (editor == 0) {
579                 editor = new RegionEditor (trackview.session(), region());
580         }
581
582         editor->present ();
583         editor->show_all();
584 }
585
586 void
587 RegionView::hide_region_editor()
588 {
589         if (editor) {
590                 editor->hide_all ();
591         }
592 }
593
594 std::string
595 RegionView::make_name () const
596 {
597         std::string str;
598
599         // XXX nice to have some good icons for this
600
601         if (_region->locked()) {
602                 str += '>';
603                 str += _region->name();
604                 str += '<';
605         } else if (_region->position_locked()) {
606                 str += '{';
607                 str += _region->name();
608                 str += '}';
609         } else {
610                 str = _region->name();
611         }
612
613         if (_region->muted()) {
614                 str = string ("!") + str;
615         }
616
617         return str;
618 }
619
620 void
621 RegionView::region_renamed ()
622 {
623         std::string str = make_name ();
624
625         set_item_name (str, this);
626         set_name_text (str);
627         reset_width_dependent_items (_pixel_width);
628 }
629
630 void
631 RegionView::region_sync_changed ()
632 {
633         int sync_dir;
634         framecnt_t sync_offset;
635
636         sync_offset = _region->sync_offset (sync_dir);
637
638         if (sync_offset == 0) {
639                 /* no need for a sync mark */
640                 if (sync_mark) {
641                         sync_mark->hide();
642                         sync_line->hide ();
643                 }
644                 return;
645         }
646
647         if (!sync_mark) {
648
649                 /* points set below */
650
651                 sync_mark = new ArdourCanvas::Polygon (*group);
652                 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
653
654                 sync_line = new ArdourCanvas::Line (*group);
655                 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour        
656                 sync_line->property_width_pixels() = 1;
657         }
658
659         /* this has to handle both a genuine change of position, a change of samples_per_unit,
660            and a change in the bounds of the _region->
661          */
662
663         if (sync_offset == 0) {
664
665                 /* no sync mark - its the start of the region */
666
667                 sync_mark->hide();
668                 sync_line->hide ();
669
670         } else {
671
672                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
673
674                         /* no sync mark - its out of the bounds of the region */
675
676                         sync_mark->hide();
677                         sync_line->hide ();
678
679                 } else {
680
681                         /* lets do it */
682
683                         Points points;
684
685                         //points = sync_mark->property_points().get_value();
686
687                         double offset = sync_offset / samples_per_unit;
688                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
689                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
690                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
691                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
692                         sync_mark->property_points().set_value (points);
693                         sync_mark->show ();
694
695                         points.clear ();
696                         points.push_back (Gnome::Art::Point (offset, 0));
697                         points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
698
699                         sync_line->property_points().set_value (points);
700                         sync_line->show ();
701                 }
702         }
703 }
704
705 void
706 RegionView::move (double x_delta, double y_delta)
707 {
708         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
709                 return;
710         }
711
712         get_canvas_group()->move (x_delta, y_delta);
713
714         /* note: ghosts never leave their tracks so y_delta for them is always zero */
715
716         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
717                 (*i)->group->move (x_delta, 0.0);
718         }
719 }
720
721 void
722 RegionView::remove_ghost_in (TimeAxisView& tv)
723 {
724         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
725                 if (&(*i)->trackview == &tv) {
726                         delete *i;
727                         break;
728                 }
729         }
730 }
731
732 void
733 RegionView::remove_ghost (GhostRegion* ghost)
734 {
735         if (in_destructor) {
736                 return;
737         }
738
739         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
740                 if (*i == ghost) {
741                         ghosts.erase (i);
742                         break;
743                 }
744         }
745 }
746
747 uint32_t
748 RegionView::get_fill_color ()
749 {
750         return fill_color;
751 }
752
753 void
754 RegionView::set_height (double h)
755 {
756         TimeAxisViewItem::set_height(h);
757
758         if (sync_line) {
759                 Points points;
760                 int sync_dir;
761                 framecnt_t sync_offset;
762                 sync_offset = _region->sync_offset (sync_dir);
763                 double offset = sync_offset / samples_per_unit;
764
765                 points.push_back (Gnome::Art::Point (offset, 0));
766                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
767                 sync_line->property_points().set_value (points);
768         }
769
770         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
771                 (*i)->property_y2() = h + 1;
772         }
773
774         for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
775                 (*i)->property_y2() = h + 1;
776         }
777
778 }
779
780 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
781  *  which uses them. */
782 void
783 RegionView::update_coverage_frames (LayerDisplay d)
784 {
785         /* remove old coverage frames */
786         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
787                 delete *i;
788         }
789
790         _coverage_frames.clear ();
791
792         if (d != Stacked) {
793                 /* don't do coverage frames unless we're in stacked mode */
794                 return;
795         }
796
797         boost::shared_ptr<Playlist> pl (_region->playlist ());
798         if (!pl) {
799                 return;
800         }
801
802         framepos_t const position = _region->first_frame ();
803         framepos_t t = position;
804         framepos_t const end = _region->last_frame ();
805
806         ArdourCanvas::SimpleRect* cr = 0;
807         bool me = false;
808
809         uint32_t const color = frame->property_fill_color_rgba ();
810         uint32_t const base_alpha = UINT_RGBA_A (color);
811
812         while (t < end) {
813
814                 t++;
815
816                 /* is this region is on top at time t? */
817                 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
818
819                 /* finish off any old rect, if required */
820                 if (cr && me != new_me) {
821                         cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
822                 }
823
824                 /* start off any new rect, if required */
825                 if (cr == 0 || me != new_me) {
826                         cr = new ArdourCanvas::SimpleRect (*group);
827                         _coverage_frames.push_back (cr);
828                         cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
829                         cr->property_y1() = 1;
830                         cr->property_y2() = _height + 1;
831                         cr->property_outline_pixels() = 0;
832                         /* areas that will be played get a lower alpha */
833                         uint32_t alpha = base_alpha;
834                         if (new_me) {
835                                 alpha /= 2;
836                         }
837                         cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
838                 }
839
840                 t = pl->find_next_region_boundary (t, 1);
841                 me = new_me;
842         }
843
844         if (cr) {
845                 /* finish off the last rectangle */
846                 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
847         }
848
849         if (frame_handle_start) {
850                 frame_handle_start->raise_to_top ();
851         }
852
853         if (frame_handle_end) {
854                 frame_handle_end->raise_to_top ();
855         }
856
857         if (name_highlight) {
858                 name_highlight->raise_to_top ();
859         }
860
861         if (name_pixbuf) {
862                 name_pixbuf->raise_to_top ();
863         }
864 }
865
866 void
867 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
868 {
869         if (_region->locked()) {
870                 return;
871         }
872
873         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
874         double const speed = rtv.track()->speed ();
875
876         framepos_t const pre_trim_first_frame = _region->first_frame();
877
878         _region->trim_front ((framepos_t) (new_bound * speed), this);
879
880         if (no_overlap) {
881                 // Get the next region on the left of this region and shrink/expand it.
882                 boost::shared_ptr<Playlist> playlist (_region->playlist());
883                 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
884
885                 bool regions_touching = false;
886
887                 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
888                         regions_touching = true;
889                 }
890
891                 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
892                 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
893                         region_left->trim_end (_region->first_frame() - 1, this);
894                 }
895         }
896
897         region_changed (ARDOUR::bounds_change);
898 }
899
900 void
901 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
902 {
903         if (_region->locked()) {
904                 return;
905         }
906
907         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
908         double const speed = rtv.track()->speed ();
909
910         framepos_t const pre_trim_last_frame = _region->last_frame();
911
912         _region->trim_end ((framepos_t) (new_bound * speed), this);
913
914         if (no_overlap) {
915                 // Get the next region on the right of this region and shrink/expand it.
916                 boost::shared_ptr<Playlist> playlist (_region->playlist());
917                 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
918
919                 bool regions_touching = false;
920
921                 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
922                         regions_touching = true;
923                 }
924
925                 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
926                 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
927                         region_right->trim_front (_region->last_frame() + 1, this);
928                 }
929
930                 region_changed (ARDOUR::bounds_change);
931                         
932         } else {
933                 region_changed (PropertyChange (ARDOUR::Properties::length));
934         }
935 }
936
937
938 void
939 RegionView::thaw_after_trim ()
940 {
941         if (_region->locked()) {
942                 return;
943         }
944
945         _region->resume_property_changes ();
946 }
947
948
949 void
950 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
951 {
952         if (_region->locked()) {
953                 return;
954         }
955
956         framepos_t new_bound;
957
958         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
959         double const speed = rtv.track()->speed ();
960
961         if (left_direction) {
962                 if (swap_direction) {
963                         new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
964                 } else {
965                         new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
966                 }
967         } else {
968                 if (swap_direction) {
969                         new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
970                 } else {
971                         new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
972                 }
973         }
974
975         _region->trim_start ((framepos_t) (new_bound * speed), this);
976         region_changed (PropertyChange (ARDOUR::Properties::start));
977 }