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