fix some more incorrect casts to CanvasNote rather than CanvasNoteEvent (fixes crashe...
[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 = (float) shortest/_region->session().frame_rate();
309         
310         /* ms are now in seconds */
311
312         char const * sunits;
313         
314         if (ms >= 60.0) {
315                 sunits = _("minutes");
316                 ms /= 60.0;
317         } else if (ms < 1.0) {
318                 sunits = _("msecs");
319                 ms *= 1000.0;
320         } else {
321                 sunits = _("secs");
322         }
323
324         string text = string_compose (ngettext ("%1 silent segment", "%1 silent segments", silences.size()), silences.size())
325                 + ", "
326                 + string_compose (_("shortest = %1 %2"), ms, sunits);
327
328         if (seen_audible) {
329                 /* ms are now in seconds */
330                 double ma = shortest_audible / _region->session().frame_rate();
331                 char const * aunits;
332                 
333                 if (ma >= 60.0) {
334                         aunits = _("minutes");
335                         ma /= 60.0;
336                 } else if (ma < 1.0) {
337                         aunits = _("msecs");
338                         ma *= 1000.0;
339                 } else {
340                         aunits = _("secs");
341                 }
342
343                 text += string_compose (_("\n  (shortest audible segment = %1 %2)"), ma, aunits);
344         }
345
346         _silence_text->property_text() = text.c_str ();
347
348
349 void
350 RegionView::hide_silent_frames ()
351 {
352         for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
353                 (*i)->hide ();
354         }
355         _silence_text->hide();
356 }
357
358 void
359 RegionView::drop_silent_frames ()
360 {
361         for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
362                 delete *i;
363         }
364         _silent_frames.clear ();
365
366         delete _silence_text;
367         _silence_text = 0;
368 }
369
370 gint
371 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
372 {
373         switch (ev->type) {
374         case GDK_BUTTON_RELEASE:
375                 static_cast<RegionView*>(arg)->lock_toggle ();
376                 return TRUE;
377                 break;
378         default:
379                 break;
380         }
381         return FALSE;
382 }
383
384 void
385 RegionView::lock_toggle ()
386 {
387         _region->set_locked (!_region->locked());
388 }
389
390 void
391 RegionView::region_changed (const PropertyChange& what_changed)
392 {
393         ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
394
395         if (what_changed.contains (ARDOUR::bounds_change)) {
396                 region_resized (what_changed);
397                 region_sync_changed ();
398         }
399         if (what_changed.contains (ARDOUR::Properties::muted)) {
400                 region_muted ();
401         }
402         if (what_changed.contains (ARDOUR::Properties::opaque)) {
403                 region_opacity ();
404         }
405         if (what_changed.contains (ARDOUR::Properties::name)) {
406                 region_renamed ();
407         }
408         if (what_changed.contains (ARDOUR::Properties::sync_position)) {
409                 region_sync_changed ();
410         }
411         if (what_changed.contains (ARDOUR::Properties::locked)) {
412                 region_locked ();
413         }
414 }
415
416 void
417 RegionView::region_locked ()
418 {
419         /* name will show locked status */
420         region_renamed ();
421 }
422
423 void
424 RegionView::region_resized (const PropertyChange& what_changed)
425 {
426         double unit_length;
427
428         if (what_changed.contains (ARDOUR::Properties::position)) {
429                 set_position (_region->position(), 0);
430                 _time_converter.set_origin_b (_region->position());
431         }
432
433         PropertyChange s_and_l;
434         s_and_l.add (ARDOUR::Properties::start);
435         s_and_l.add (ARDOUR::Properties::length);
436
437         if (what_changed.contains (s_and_l)) {
438
439                 set_duration (_region->length(), 0);
440
441                 unit_length = _region->length() / samples_per_unit;
442
443                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
444
445                         (*i)->set_duration (unit_length);
446
447                 }
448         }
449 }
450
451 void
452 RegionView::reset_width_dependent_items (double pixel_width)
453 {
454         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
455         _pixel_width = pixel_width;
456 }
457
458 void
459 RegionView::region_muted ()
460 {
461         set_frame_color ();
462         region_renamed ();
463 }
464
465 void
466 RegionView::region_opacity ()
467 {
468         set_frame_color ();
469 }
470
471 void
472 RegionView::raise_to_top ()
473 {
474         _region->raise_to_top ();
475 }
476
477 void
478 RegionView::lower_to_bottom ()
479 {
480         _region->lower_to_bottom ();
481 }
482
483 bool
484 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
485 {
486         double delta;
487         bool ret;
488
489         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
490                 return false;
491         }
492
493         if (ignored) {
494                 *ignored = delta;
495         }
496
497         if (delta) {
498                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
499                         (*i)->group->move (delta, 0.0);
500                 }
501         }
502
503         return ret;
504 }
505
506 void
507 RegionView::set_samples_per_unit (gdouble spu)
508 {
509         TimeAxisViewItem::set_samples_per_unit (spu);
510
511         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
512                 (*i)->set_samples_per_unit (spu);
513                 (*i)->set_duration (_region->length() / samples_per_unit);
514         }
515
516         region_sync_changed ();
517 }
518
519 bool
520 RegionView::set_duration (framecnt_t frames, void *src)
521 {
522         if (!TimeAxisViewItem::set_duration (frames, src)) {
523                 return false;
524         }
525
526         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
527                 (*i)->set_duration (_region->length() / samples_per_unit);
528         }
529
530         return true;
531 }
532
533 void
534 RegionView::set_colors ()
535 {
536         TimeAxisViewItem::set_colors ();
537
538         if (sync_mark) {
539                 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
540                 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
541         }
542 }
543
544 void
545 RegionView::set_frame_color ()
546 {
547         if (_region->opaque()) {
548                 fill_opacity = 130;
549         } else {
550                 fill_opacity = 60;
551         }
552
553         TimeAxisViewItem::set_frame_color ();
554 }
555
556 void
557 RegionView::fake_set_opaque (bool yn)
558 {
559        if (yn) {
560                fill_opacity = 130;
561        } else {
562                fill_opacity = 60;
563        }
564
565        set_frame_color ();
566 }
567
568 void
569 RegionView::show_region_editor ()
570 {
571         if (editor == 0) {
572                 editor = new RegionEditor (trackview.session(), region());
573         }
574
575         editor->present ();
576         editor->show_all();
577 }
578
579 void
580 RegionView::hide_region_editor()
581 {
582         if (editor) {
583                 editor->hide_all ();
584         }
585 }
586
587 std::string
588 RegionView::make_name () const
589 {
590         std::string str;
591
592         // XXX nice to have some good icons for this
593
594         if (_region->locked()) {
595                 str += '>';
596                 str += _region->name();
597                 str += '<';
598         } else if (_region->position_locked()) {
599                 str += '{';
600                 str += _region->name();
601                 str += '}';
602         } else {
603                 str = _region->name();
604         }
605
606         if (_region->muted()) {
607                 str = string ("!") + str;
608         }
609
610         return str;
611 }
612
613 void
614 RegionView::region_renamed ()
615 {
616         std::string str = make_name ();
617
618         set_item_name (str, this);
619         set_name_text (str);
620         reset_width_dependent_items (_pixel_width);
621 }
622
623 void
624 RegionView::region_sync_changed ()
625 {
626         int sync_dir;
627         framecnt_t sync_offset;
628
629         sync_offset = _region->sync_offset (sync_dir);
630
631         if (sync_offset == 0) {
632                 /* no need for a sync mark */
633                 if (sync_mark) {
634                         sync_mark->hide();
635                         sync_line->hide ();
636                 }
637                 return;
638         }
639
640         if (!sync_mark) {
641
642                 /* points set below */
643
644                 sync_mark = new ArdourCanvas::Polygon (*group);
645                 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour
646
647                 sync_line = new ArdourCanvas::Line (*group);
648                 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255);     // fill_color;                   // FIXME make a themeable colour        
649                 sync_line->property_width_pixels() = 1;
650         }
651
652         /* this has to handle both a genuine change of position, a change of samples_per_unit,
653            and a change in the bounds of the _region->
654          */
655
656         if (sync_offset == 0) {
657
658                 /* no sync mark - its the start of the region */
659
660                 sync_mark->hide();
661                 sync_line->hide ();
662
663         } else {
664
665                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
666
667                         /* no sync mark - its out of the bounds of the region */
668
669                         sync_mark->hide();
670                         sync_line->hide ();
671
672                 } else {
673
674                         /* lets do it */
675
676                         Points points;
677
678                         //points = sync_mark->property_points().get_value();
679
680                         double offset = sync_offset / samples_per_unit;
681                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
682                         points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
683                         points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
684                         points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
685                         sync_mark->property_points().set_value (points);
686                         sync_mark->show ();
687
688                         points.clear ();
689                         points.push_back (Gnome::Art::Point (offset, 0));
690                         points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
691
692                         sync_line->property_points().set_value (points);
693                         sync_line->show ();
694                 }
695         }
696 }
697
698 void
699 RegionView::move (double x_delta, double y_delta)
700 {
701         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
702                 return;
703         }
704
705         get_canvas_group()->move (x_delta, y_delta);
706
707         /* note: ghosts never leave their tracks so y_delta for them is always zero */
708
709         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
710                 (*i)->group->move (x_delta, 0.0);
711         }
712 }
713
714 void
715 RegionView::remove_ghost_in (TimeAxisView& tv)
716 {
717         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
718                 if (&(*i)->trackview == &tv) {
719                         delete *i;
720                         break;
721                 }
722         }
723 }
724
725 void
726 RegionView::remove_ghost (GhostRegion* ghost)
727 {
728         if (in_destructor) {
729                 return;
730         }
731
732         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
733                 if (*i == ghost) {
734                         ghosts.erase (i);
735                         break;
736                 }
737         }
738 }
739
740 uint32_t
741 RegionView::get_fill_color ()
742 {
743         return fill_color;
744 }
745
746 void
747 RegionView::set_height (double h)
748 {
749         TimeAxisViewItem::set_height(h);
750
751         if (sync_line) {
752                 Points points;
753                 int sync_dir;
754                 framecnt_t sync_offset;
755                 sync_offset = _region->sync_offset (sync_dir);
756                 double offset = sync_offset / samples_per_unit;
757
758                 points.push_back (Gnome::Art::Point (offset, 0));
759                 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
760                 sync_line->property_points().set_value (points);
761         }
762
763         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
764                 (*i)->property_y2() = h + 1;
765         }
766
767         for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
768                 (*i)->property_y2() = h + 1;
769         }
770
771 }
772
773 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
774  *  which uses them. */
775 void
776 RegionView::update_coverage_frames (LayerDisplay d)
777 {
778         /* remove old coverage frames */
779         for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
780                 delete *i;
781         }
782
783         _coverage_frames.clear ();
784
785         if (d != Stacked) {
786                 /* don't do coverage frames unless we're in stacked mode */
787                 return;
788         }
789
790         boost::shared_ptr<Playlist> pl (_region->playlist ());
791         if (!pl) {
792                 return;
793         }
794
795         framepos_t const position = _region->first_frame ();
796         framepos_t t = position;
797         framepos_t const end = _region->last_frame ();
798
799         ArdourCanvas::SimpleRect* cr = 0;
800         bool me = false;
801
802         uint32_t const color = frame->property_fill_color_rgba ();
803         uint32_t const base_alpha = UINT_RGBA_A (color);
804
805         while (t < end) {
806
807                 t++;
808
809                 /* is this region is on top at time t? */
810                 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
811
812                 /* finish off any old rect, if required */
813                 if (cr && me != new_me) {
814                         cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
815                 }
816
817                 /* start off any new rect, if required */
818                 if (cr == 0 || me != new_me) {
819                         cr = new ArdourCanvas::SimpleRect (*group);
820                         _coverage_frames.push_back (cr);
821                         cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
822                         cr->property_y1() = 1;
823                         cr->property_y2() = _height + 1;
824                         cr->property_outline_pixels() = 0;
825                         /* areas that will be played get a lower alpha */
826                         uint32_t alpha = base_alpha;
827                         if (new_me) {
828                                 alpha /= 2;
829                         }
830                         cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
831                 }
832
833                 t = pl->find_next_region_boundary (t, 1);
834                 me = new_me;
835         }
836
837         if (cr) {
838                 /* finish off the last rectangle */
839                 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
840         }
841
842         if (frame_handle_start) {
843                 frame_handle_start->raise_to_top ();
844         }
845
846         if (frame_handle_end) {
847                 frame_handle_end->raise_to_top ();
848         }
849
850         if (name_highlight) {
851                 name_highlight->raise_to_top ();
852         }
853
854         if (name_pixbuf) {
855                 name_pixbuf->raise_to_top ();
856         }
857 }
858
859 void
860 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
861 {
862         if (_region->locked()) {
863                 return;
864         }
865
866         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
867         double const speed = rtv.track()->speed ();
868
869         framepos_t const pre_trim_first_frame = _region->first_frame();
870
871         _region->trim_front ((framepos_t) (new_bound * speed), this);
872
873         if (no_overlap) {
874                 // Get the next region on the left of this region and shrink/expand it.
875                 boost::shared_ptr<Playlist> playlist (_region->playlist());
876                 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
877
878                 bool regions_touching = false;
879
880                 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
881                         regions_touching = true;
882                 }
883
884                 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
885                 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
886                         region_left->trim_end (_region->first_frame() - 1, this);
887                 }
888         }
889
890         region_changed (ARDOUR::bounds_change);
891 }
892
893 void
894 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
895 {
896         if (_region->locked()) {
897                 return;
898         }
899
900         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
901         double const speed = rtv.track()->speed ();
902
903         framepos_t const pre_trim_last_frame = _region->last_frame();
904
905         _region->trim_end ((framepos_t) (new_bound * speed), this);
906
907         if (no_overlap) {
908                 // Get the next region on the right of this region and shrink/expand it.
909                 boost::shared_ptr<Playlist> playlist (_region->playlist());
910                 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
911
912                 bool regions_touching = false;
913
914                 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
915                         regions_touching = true;
916                 }
917
918                 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
919                 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
920                         region_right->trim_front (_region->last_frame() + 1, this);
921                 }
922
923                 region_changed (ARDOUR::bounds_change);
924                         
925         } else {
926                 region_changed (PropertyChange (ARDOUR::Properties::length));
927         }
928 }
929
930
931 void
932 RegionView::thaw_after_trim ()
933 {
934         if (_region->locked()) {
935                 return;
936         }
937
938         _region->resume_property_changes ();
939 }
940
941
942 void
943 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
944 {
945         if (_region->locked()) {
946                 return;
947         }
948
949         framepos_t new_bound;
950
951         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
952         double const speed = rtv.track()->speed ();
953
954         if (left_direction) {
955                 if (swap_direction) {
956                         new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
957                 } else {
958                         new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
959                 }
960         } else {
961                 if (swap_direction) {
962                         new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
963                 } else {
964                         new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
965                 }
966         }
967
968         _region->trim_start ((framepos_t) (new_bound * speed), this);
969         region_changed (PropertyChange (ARDOUR::Properties::start));
970 }