Clean up library inheritance (colors.h, utils.h)
[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 (frame_handle_start) {
160                 frame_handle_start->set_data ("regionview", this);
161                 frame_handle_start->set_data ("isleft", (void*) 1);
162                 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
163                 frame_handle_start->raise_to_top();
164         }
165
166         if (frame_handle_end) {
167                 frame_handle_end->set_data ("regionview", this);
168                 frame_handle_end->set_data ("isleft", (void*) 0);
169                 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
170                 frame_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_frames.begin (); i != _coverage_frames.end (); ++i) {
203                 delete *i;
204         }
205
206         drop_silent_frames ();
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_frames (const AudioIntervalResult& silences, double /*threshold*/)
222 {
223         framecnt_t shortest = max_framecnt;
224
225         /* remove old silent frames */
226         drop_silent_frames ();
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_frames.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         framecnt_t shortest_audible = max_framecnt;
254
255         framecnt_t s = _region->start();
256         for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
257                 framecnt_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         framecnt_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().frame_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_framepos) {
301                 /* ms are now in seconds */
302                 double ma = (float) shortest_audible / _region->session().frame_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_frames ()
323 {
324         for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
325                 (*i)->hide ();
326         }
327         _silence_text->hide();
328 }
329
330 void
331 RegionView::drop_silent_frames ()
332 {
333         for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
334                 delete *i;
335         }
336         _silent_frames.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::sync_position)) {
381                 region_sync_changed ();
382         }
383         if (what_changed.contains (ARDOUR::Properties::locked)) {
384                 region_locked ();
385         }
386 }
387
388 void
389 RegionView::region_locked ()
390 {
391         /* name will show locked status */
392         region_renamed ();
393 }
394
395 void
396 RegionView::region_resized (const PropertyChange& what_changed)
397 {
398         double unit_length;
399
400         if (what_changed.contains (ARDOUR::Properties::position)) {
401                 set_position (_region->position(), 0);
402         }
403
404         PropertyChange s_and_l;
405         s_and_l.add (ARDOUR::Properties::start);
406         s_and_l.add (ARDOUR::Properties::length);
407
408         if (what_changed.contains (s_and_l)) {
409
410                 set_duration (_region->length(), 0);
411
412                 unit_length = _region->length() / samples_per_pixel;
413
414                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
415
416                         (*i)->set_duration (unit_length);
417
418                 }
419         }
420 }
421
422 void
423 RegionView::reset_width_dependent_items (double pixel_width)
424 {
425         TimeAxisViewItem::reset_width_dependent_items (pixel_width);
426         _pixel_width = pixel_width;
427 }
428
429 void
430 RegionView::region_muted ()
431 {
432         set_frame_color ();
433         region_renamed ();
434 }
435
436 void
437 RegionView::region_opacity ()
438 {
439         set_frame_color ();
440 }
441
442 void
443 RegionView::raise_to_top ()
444 {
445         _region->raise_to_top ();
446 }
447
448 void
449 RegionView::lower_to_bottom ()
450 {
451         _region->lower_to_bottom ();
452 }
453
454 bool
455 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
456 {
457         double delta;
458         bool ret;
459
460         if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
461                 return false;
462         }
463
464         if (ignored) {
465                 *ignored = delta;
466         }
467
468         if (delta) {
469                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
470                         (*i)->group->move (ArdourCanvas::Duple (delta, 0.0));
471                 }
472         }
473
474         return ret;
475 }
476
477 void
478 RegionView::set_samples_per_pixel (double fpp)
479 {
480         TimeAxisViewItem::set_samples_per_pixel (fpp);
481
482         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
483                 (*i)->set_samples_per_pixel (fpp);
484                 (*i)->set_duration (_region->length() / fpp);
485         }
486
487         region_sync_changed ();
488 }
489
490 bool
491 RegionView::set_duration (framecnt_t frames, void *src)
492 {
493         if (!TimeAxisViewItem::set_duration (frames, src)) {
494                 return false;
495         }
496
497         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
498                 (*i)->set_duration (_region->length() / samples_per_pixel);
499         }
500
501         return true;
502 }
503
504 void
505 RegionView::set_colors ()
506 {
507         TimeAxisViewItem::set_colors ();
508         set_sync_mark_color ();
509 }
510
511 void
512 RegionView::set_sync_mark_color ()
513 {
514         if (sync_mark) {
515                 Gtkmm2ext::Color c = UIConfiguration::instance().color ("sync mark");
516                 sync_mark->set_fill_color (c);
517                 sync_mark->set_outline_color (c);
518                 sync_line->set_outline_color (c);
519         }
520 }
521
522 uint32_t
523 RegionView::get_fill_color () const
524 {
525         Gtkmm2ext::Color f = TimeAxisViewItem::get_fill_color();
526         char const *modname;
527
528         if (_region->opaque() && (!ARDOUR::Profile->get_mixbus() || (!_dragging && !_region->muted ()))) {
529                 modname = "opaque region base";
530         } else {
531                 modname = "transparent region base";
532         }
533
534         return Gtkmm2ext::HSV(f).mod (UIConfiguration::instance().modifier (modname)).color ();
535 }
536
537 void
538 RegionView::show_region_editor ()
539 {
540         if (editor == 0) {
541                 editor = new RegionEditor (trackview.session(), region());
542         }
543
544         editor->present ();
545         editor->show_all();
546 }
547
548 void
549 RegionView::hide_region_editor()
550 {
551         if (editor) {
552                 editor->hide_all ();
553         }
554 }
555
556 std::string
557 RegionView::make_name () const
558 {
559         std::string str;
560
561         // XXX nice to have some good icons for this
562
563         if (_region->locked()) {
564                 str += '>';
565                 str += _region->name();
566                 str += '<';
567         } else if (_region->position_locked()) {
568                 str += '{';
569                 str += _region->name();
570                 str += '}';
571         } else if (_region->video_locked()) {
572                 str += '[';
573                 str += _region->name();
574                 str += ']';
575         } else {
576                 str = _region->name();
577         }
578
579         if (_region->muted()) {
580                 str = string ("!") + str;
581         }
582
583         return str;
584 }
585
586 void
587 RegionView::region_renamed ()
588 {
589         std::string str = make_name ();
590
591         set_item_name (str, this);
592         set_name_text (str);
593 }
594
595 void
596 RegionView::region_sync_changed ()
597 {
598         int sync_dir;
599         framecnt_t sync_offset;
600
601         sync_offset = _region->sync_offset (sync_dir);
602
603         if (sync_offset == 0) {
604                 /* no need for a sync mark */
605                 if (sync_mark) {
606                         sync_mark->hide();
607                         sync_line->hide ();
608                 }
609                 return;
610         }
611
612         if (!sync_mark) {
613
614                 /* points set below */
615
616                 sync_mark = new ArdourCanvas::Polygon (group);
617                 CANVAS_DEBUG_NAME (sync_mark, string_compose ("sync mark for %1", get_item_name()));
618                 sync_line = new ArdourCanvas::Line (group);
619                 CANVAS_DEBUG_NAME (sync_line, string_compose ("sync mark for %1", get_item_name()));
620
621                 set_sync_mark_color ();
622         }
623
624         /* this has to handle both a genuine change of position, a change of samples_per_pixel
625            and a change in the bounds of the _region->
626          */
627
628         if (sync_offset == 0) {
629
630                 /* no sync mark - its the start of the region */
631
632                 sync_mark->hide();
633                 sync_line->hide ();
634
635         } else {
636
637                 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
638
639                         /* no sync mark - its out of the bounds of the region */
640
641                         sync_mark->hide();
642                         sync_line->hide ();
643
644                 } else {
645
646                         /* lets do it */
647
648                         Points points;
649
650                         //points = sync_mark->property_points().get_value();
651
652                         double offset = sync_offset / samples_per_pixel;
653                         points.push_back (ArdourCanvas::Duple (offset - ((sync_mark_width-1)/2), 1));
654                         points.push_back (ArdourCanvas::Duple (offset + ((sync_mark_width-1)/2), 1));
655                         points.push_back (ArdourCanvas::Duple (offset, sync_mark_width - 1));
656                         points.push_back (ArdourCanvas::Duple (offset - ((sync_mark_width-1)/2), 1));
657                         sync_mark->set (points);
658                         sync_mark->show ();
659
660                         sync_line->set (ArdourCanvas::Duple (offset, 0), ArdourCanvas::Duple (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
661                         sync_line->show ();
662                 }
663         }
664 }
665
666 void
667 RegionView::move (double x_delta, double y_delta)
668 {
669         if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
670                 return;
671         }
672
673         /* items will not prevent Item::move() moving
674          * them to a negative x-axis coordinate, which
675          * is legal, but we don't want that here.
676          */
677
678         ArdourCanvas::Item *item = get_canvas_group ();
679
680         if (item->position().x + x_delta < 0) {
681                 x_delta = -item->position().x; /* move it to zero */
682         }
683
684         item->move (ArdourCanvas::Duple (x_delta, y_delta));
685
686         /* note: ghosts never leave their tracks so y_delta for them is always zero */
687
688         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
689                 (*i)->group->move (ArdourCanvas::Duple (x_delta, 0.0));
690         }
691 }
692
693 void
694 RegionView::remove_ghost_in (TimeAxisView& tv)
695 {
696         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
697                 if (&(*i)->trackview == &tv) {
698                         delete *i;
699                         break;
700                 }
701         }
702 }
703
704 void
705 RegionView::remove_ghost (GhostRegion* ghost)
706 {
707         if (in_destructor) {
708                 return;
709         }
710
711         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
712                 if (*i == ghost) {
713                         ghosts.erase (i);
714                         break;
715                 }
716         }
717 }
718
719 void
720 RegionView::set_height (double h)
721 {
722         TimeAxisViewItem::set_height(h);
723
724         if (sync_line) {
725                 Points points;
726                 int sync_dir;
727                 framecnt_t sync_offset;
728                 sync_offset = _region->sync_offset (sync_dir);
729                 double offset = sync_offset / samples_per_pixel;
730
731                 sync_line->set (
732                         ArdourCanvas::Duple (offset, 0),
733                         ArdourCanvas::Duple (offset, h - NAME_HIGHLIGHT_SIZE)
734                         );
735         }
736
737         for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
738                 (*i)->set_y1 (h + 1);
739         }
740
741         for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
742                 (*i)->set_y1 (h + 1);
743         }
744
745 }
746
747 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
748  *  which uses them. */
749 void
750 RegionView::update_coverage_frames (LayerDisplay d)
751 {
752         /* remove old coverage frames */
753         for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
754                 delete *i;
755         }
756
757         _coverage_frames.clear ();
758
759         if (d != Stacked) {
760                 /* don't do coverage frames unless we're in stacked mode */
761                 return;
762         }
763
764         boost::shared_ptr<Playlist> pl (_region->playlist ());
765         if (!pl) {
766                 return;
767         }
768
769         framepos_t const position = _region->first_frame ();
770         framepos_t t = position;
771         framepos_t const end = _region->last_frame ();
772
773         ArdourCanvas::Rectangle* cr = 0;
774         bool me = false;
775
776         /* the color that will be used to show parts of regions that will not be heard */
777         uint32_t const non_playing_color = UIConfiguration::instance().color_mod ("covered region", "covered region base");
778
779
780         while (t < end) {
781
782                 t++;
783
784                 /* is this region is on top at time t? */
785                 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
786                 /* finish off any old rect, if required */
787                 if (cr && me != new_me) {
788                         cr->set_x1 (trackview.editor().sample_to_pixel (t - position));
789                 }
790
791                 /* start off any new rect, if required */
792                 if (cr == 0 || me != new_me) {
793                         cr = new ArdourCanvas::Rectangle (group);
794                         _coverage_frames.push_back (cr);
795                         cr->set_x0 (trackview.editor().sample_to_pixel (t - position));
796                         cr->set_y0 (1);
797                         cr->set_y1 (_height + 1);
798                         cr->set_outline (false);
799                         cr->set_ignore_events (true);
800                         if (new_me) {
801                                 cr->set_fill_color (UINT_RGBA_CHANGE_A (non_playing_color, 0));
802                         } else {
803                                 cr->set_fill_color (non_playing_color);
804                         }
805                 }
806                 t = pl->find_next_region_boundary (t, 1);
807                 if (t < 0) {
808                         break;
809                 }
810                 me = new_me;
811         }
812
813         t = pl->find_next_region_boundary (t, 1);
814
815         if (cr) {
816                 /* finish off the last rectangle */
817                 cr->set_x1 (trackview.editor().sample_to_pixel (end - position));
818         }
819
820         if (frame_handle_start) {
821                 frame_handle_start->raise_to_top ();
822         }
823
824         if (frame_handle_end) {
825                 frame_handle_end->raise_to_top ();
826         }
827
828         if (name_highlight) {
829                 name_highlight->raise_to_top ();
830         }
831
832         if (name_text) {
833                 name_text->raise_to_top ();
834         }
835 }
836
837 bool
838 RegionView::trim_front (framepos_t new_bound, bool no_overlap, const int32_t sub_num)
839 {
840         if (_region->locked()) {
841                 return false;
842         }
843
844         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
845         double const speed = rtv.track()->speed ();
846
847         framepos_t const pre_trim_first_frame = _region->first_frame();
848
849         const framepos_t speed_bound = (framepos_t) (new_bound * speed);
850
851         if (_region->position() == speed_bound) {
852                 return false;
853         }
854
855         _region->trim_front (speed_bound, sub_num);
856
857         if (no_overlap) {
858                 // Get the next region on the left of this region and shrink/expand it.
859                 boost::shared_ptr<Playlist> playlist (_region->playlist());
860                 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
861
862                 bool regions_touching = false;
863
864                 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
865                         regions_touching = true;
866                 }
867
868                 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
869                 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
870                         region_left->trim_end (_region->first_frame() - 1);
871                 }
872         }
873
874         region_changed (ARDOUR::bounds_change);
875
876         return (pre_trim_first_frame != _region->first_frame());  //return true if we actually changed something
877 }
878
879 bool
880 RegionView::trim_end (framepos_t new_bound, bool no_overlap, const int32_t sub_num)
881 {
882         if (_region->locked()) {
883                 return false;
884         }
885
886         RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
887         double const speed = rtv.track()->speed ();
888
889         framepos_t const pre_trim_last_frame = _region->last_frame();
890
891         _region->trim_end ((framepos_t) (new_bound * speed), sub_num);
892
893         if (no_overlap) {
894                 // Get the next region on the right of this region and shrink/expand it.
895                 boost::shared_ptr<Playlist> playlist (_region->playlist());
896                 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
897
898                 bool regions_touching = false;
899
900                 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
901                         regions_touching = true;
902                 }
903
904                 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
905                 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
906                         region_right->trim_front (_region->last_frame() + 1, sub_num);
907                 }
908
909                 region_changed (ARDOUR::bounds_change);
910
911         } else {
912                 region_changed (PropertyChange (ARDOUR::Properties::length));
913         }
914
915         return (pre_trim_last_frame != _region->last_frame());  //return true if we actually changed something
916 }
917
918
919 void
920 RegionView::thaw_after_trim ()
921 {
922         if (_region->locked()) {
923                 return;
924         }
925
926         _region->resume_property_changes ();
927 }
928
929
930 void
931 RegionView::move_contents (frameoffset_t distance)
932 {
933         if (_region->locked()) {
934                 return;
935         }
936         _region->move_start (distance);
937         region_changed (PropertyChange (ARDOUR::Properties::start));
938 }
939
940 /** Snap a frame offset within our region using the current snap settings.
941  *  @param x Frame offset from this region's position.
942  *  @param ensure_snap whether to ignore snap_mode (in the case of SnapOff) and magnetic snap.
943  *  Used when inverting snap mode logic with key modifiers, or snap distance calculation.
944  *  @return Snapped frame offset from this region's position.
945  */
946 MusicFrame
947 RegionView::snap_frame_to_frame (frameoffset_t x, bool ensure_snap) const
948 {
949         PublicEditor& editor = trackview.editor();
950         /* x is region relative, convert it to global absolute frames */
951         framepos_t const session_frame = x + _region->position();
952
953         /* try a snap in either direction */
954         MusicFrame frame (session_frame, 0);
955         editor.snap_to (frame, RoundNearest, false, ensure_snap);
956
957         /* if we went off the beginning of the region, snap forwards */
958         if (frame.frame < _region->position ()) {
959                 frame.frame = session_frame;
960                 editor.snap_to (frame, RoundUpAlways, false, ensure_snap);
961         }
962
963         /* back to region relative, keeping the relevant divisor */
964         return MusicFrame (frame.frame - _region->position(), frame.division);
965 }