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