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