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