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