2 Copyright (C) 2001-2006 Paul Davis
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.
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.
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.
26 #include <gtkmm2ext/gtk_ui.h>
28 #include "ardour/playlist.h"
29 #include "ardour/session.h"
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"
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"
49 #include "rgb_macros.h"
50 #include "gui_thread.h"
55 using namespace ARDOUR;
57 using namespace Editing;
59 using namespace ArdourCanvas;
61 static const int32_t sync_mark_width = 9;
63 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
65 RegionView::RegionView (ArdourCanvas::Group* parent,
67 boost::shared_ptr<ARDOUR::Region> r,
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)))
79 , current_visible_sync_position(0.0)
81 , _enable_display(false)
83 , in_destructor(false)
84 , wait_for_data(false)
86 , _region_relative_time_converter(r->session().tempo_map(), r->position())
87 , _source_relative_time_converter(r->session().tempo_map(), r->position() - r->start())
89 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&RegionView::remove_ghost, this, _1), gui_context());
92 RegionView::RegionView (const RegionView& other)
93 : sigc::trackable(other)
94 , TimeAxisViewItem (other)
96 , _region_relative_time_converter(other.region_relative_time_converter())
97 , _source_relative_time_converter(other.source_relative_time_converter())
99 /* derived concrete type will call init () */
101 _region = other._region;
102 current_visible_sync_position = other.current_visible_sync_position;
104 _pixel_width = other._pixel_width;
106 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&RegionView::remove_ghost, this, _1), gui_context());
109 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
110 : sigc::trackable(other)
111 , TimeAxisViewItem (other)
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())
116 /* this is a pseudo-copy constructor used when dragging regions
117 around on the canvas.
120 /* derived concrete type will call init () */
122 _region = other_region;
123 current_visible_sync_position = other.current_visible_sync_position;
125 _pixel_width = other._pixel_width;
127 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&RegionView::remove_ghost, this, _1), gui_context());
130 RegionView::RegionView (ArdourCanvas::Group* parent,
132 boost::shared_ptr<ARDOUR::Region> r,
134 uint32_t basic_color,
136 TimeAxisViewItem::Visibility visibility)
137 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
142 , current_visible_sync_position(0.0)
144 , _enable_display(false)
146 , in_destructor(false)
147 , wait_for_data(false)
149 , _region_relative_time_converter(r->session().tempo_map(), r->position())
150 , _source_relative_time_converter(r->session().tempo_map(), r->position() - r->start())
155 RegionView::init (bool wfd)
159 in_destructor = false;
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));
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();
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();
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));
191 _enable_display = true;
194 set_height (trackview.current_height());
196 _region->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RegionView::region_changed, this, _1), gui_context());
200 ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
202 /* XXX sync mark drag? */
205 RegionView::~RegionView ()
207 in_destructor = true;
209 for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
213 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
217 drop_silent_frames ();
223 RegionView::canvas_group_event (GdkEvent* event)
225 return trackview.editor().canvas_region_view_event (event, group, this);
229 RegionView::set_silent_frames (const AudioIntervalResult& silences, double /*threshold*/)
231 framecnt_t shortest = max_framecnt;
233 /* remove old silent frames */
234 drop_silent_frames ();
236 if (silences.empty()) {
240 uint32_t const color = ARDOUR_UI::config()->get_canvasvar_Silence();
242 for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
244 ArdourCanvas::Rectangle* cr = new ArdourCanvas::Rectangle (group);
245 cr->set_ignore_events (true);
246 _silent_frames.push_back (cr);
248 /* coordinates for the rect are relative to the regionview origin */
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()));
253 cr->set_y1 (_height - 2);
254 cr->set_outline (false);
255 cr->set_fill_color (color);
257 shortest = min (shortest, i->second - i->first);
260 /* Find shortest audible segment */
261 framecnt_t shortest_audible = max_framecnt;
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;
267 shortest_audible = min (shortest_audible, dur);
273 framecnt_t const dur = _region->start() + _region->length() - 1 - s;
275 shortest_audible = min (shortest_audible, dur);
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());
283 /* both positions are relative to the region start offset in source */
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);
288 double ms = (float) shortest/_region->session().frame_rate();
290 /* ms are now in seconds */
295 sunits = _("minutes");
297 } else if (ms < 1.0) {
304 string text = string_compose (ngettext ("%1 silent segment", "%1 silent segments", silences.size()), silences.size())
306 + string_compose (_("shortest = %1 %2"), ms, sunits);
308 if (shortest_audible != max_framepos) {
309 /* ms are now in seconds */
310 double ma = (float) shortest_audible / _region->session().frame_rate();
314 aunits = _("minutes");
316 } else if (ma < 1.0) {
323 text += string_compose (_("\n (shortest audible segment = %1 %2)"), ma, aunits);
326 _silence_text->set (text);
330 RegionView::hide_silent_frames ()
332 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
335 _silence_text->hide();
339 RegionView::drop_silent_frames ()
341 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
344 _silent_frames.clear ();
346 delete _silence_text;
351 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
354 case GDK_BUTTON_RELEASE:
355 static_cast<RegionView*>(arg)->lock_toggle ();
365 RegionView::lock_toggle ()
367 _region->set_locked (!_region->locked());
371 RegionView::region_changed (const PropertyChange& what_changed)
373 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
375 if (what_changed.contains (ARDOUR::bounds_change)) {
376 region_resized (what_changed);
377 region_sync_changed ();
379 if (what_changed.contains (ARDOUR::Properties::muted)) {
382 if (what_changed.contains (ARDOUR::Properties::opaque)) {
385 if (what_changed.contains (ARDOUR::Properties::name)) {
388 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
389 region_sync_changed ();
391 if (what_changed.contains (ARDOUR::Properties::locked)) {
394 if (what_changed.contains (ARDOUR::Properties::locked)) {
395 /* name will show locked status */
401 RegionView::region_locked ()
403 /* name will show locked status */
408 RegionView::region_resized (const PropertyChange& what_changed)
412 if (what_changed.contains (ARDOUR::Properties::position)) {
413 set_position (_region->position(), 0);
414 _region_relative_time_converter.set_origin_b (_region->position());
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());
421 PropertyChange s_and_l;
422 s_and_l.add (ARDOUR::Properties::start);
423 s_and_l.add (ARDOUR::Properties::length);
425 if (what_changed.contains (s_and_l)) {
427 set_duration (_region->length(), 0);
429 unit_length = _region->length() / samples_per_pixel;
431 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
433 (*i)->set_duration (unit_length);
440 RegionView::reset_width_dependent_items (double pixel_width)
442 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
443 _pixel_width = pixel_width;
447 RegionView::region_muted ()
454 RegionView::region_opacity ()
460 RegionView::raise_to_top ()
462 _region->raise_to_top ();
466 RegionView::lower_to_bottom ()
468 _region->lower_to_bottom ();
472 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
477 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
486 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
487 (*i)->group->move (ArdourCanvas::Duple (delta, 0.0));
495 RegionView::set_samples_per_pixel (double fpp)
497 TimeAxisViewItem::set_samples_per_pixel (fpp);
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);
504 region_sync_changed ();
508 RegionView::set_duration (framecnt_t frames, void *src)
510 if (!TimeAxisViewItem::set_duration (frames, src)) {
514 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
515 (*i)->set_duration (_region->length() / samples_per_pixel);
522 RegionView::set_colors ()
524 TimeAxisViewItem::set_colors ();
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));
534 RegionView::set_frame_color ()
540 if (!_region->opaque()) {
544 TimeAxisViewItem::set_frame_color ();
548 RegionView::show_region_editor ()
551 editor = new RegionEditor (trackview.session(), region());
559 RegionView::hide_region_editor()
567 RegionView::make_name () const
571 // XXX nice to have some good icons for this
573 if (_region->locked()) {
575 str += _region->name();
577 } else if (_region->position_locked()) {
579 str += _region->name();
581 } else if (_region->video_locked()) {
583 str += _region->name();
586 str = _region->name();
589 if (_region->muted()) {
590 str = string ("!") + str;
597 RegionView::region_renamed ()
599 std::string str = make_name ();
601 set_item_name (str, this);
603 reset_width_dependent_items (_pixel_width);
607 RegionView::region_sync_changed ()
610 framecnt_t sync_offset;
612 sync_offset = _region->sync_offset (sync_dir);
614 if (sync_offset == 0) {
615 /* no need for a sync mark */
625 /* points set below */
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
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
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->
640 if (sync_offset == 0) {
642 /* no sync mark - its the start of the region */
649 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
651 /* no sync mark - its out of the bounds of the region */
662 //points = sync_mark->property_points().get_value();
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);
672 sync_line->set (ArdourCanvas::Duple (offset, 0), ArdourCanvas::Duple (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
679 RegionView::move (double x_delta, double y_delta)
681 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
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.
690 ArdourCanvas::Item *item = get_canvas_group ();
692 if (item->position().x + x_delta < 0) {
693 x_delta = -item->position().x; /* move it to zero */
696 item->move (ArdourCanvas::Duple (x_delta, y_delta));
698 /* note: ghosts never leave their tracks so y_delta for them is always zero */
700 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
701 (*i)->group->move (ArdourCanvas::Duple (x_delta, 0.0));
706 RegionView::remove_ghost_in (TimeAxisView& tv)
708 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
709 if (&(*i)->trackview == &tv) {
717 RegionView::remove_ghost (GhostRegion* ghost)
723 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
732 RegionView::set_height (double h)
734 TimeAxisViewItem::set_height(h);
739 framecnt_t sync_offset;
740 sync_offset = _region->sync_offset (sync_dir);
741 double offset = sync_offset / samples_per_pixel;
744 ArdourCanvas::Duple (offset, 0),
745 ArdourCanvas::Duple (offset, h - NAME_HIGHLIGHT_SIZE)
749 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
750 (*i)->set_y1 (h + 1);
753 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
754 (*i)->set_y1 (h + 1);
759 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
760 * which uses them. */
762 RegionView::update_coverage_frames (LayerDisplay d)
764 /* remove old coverage frames */
765 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
769 _coverage_frames.clear ();
772 /* don't do coverage frames unless we're in stacked mode */
776 boost::shared_ptr<Playlist> pl (_region->playlist ());
781 framepos_t const position = _region->first_frame ();
782 framepos_t t = position;
783 framepos_t const end = _region->last_frame ();
785 ArdourCanvas::Rectangle* cr = 0;
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 ();
795 /* is this region is on top at time t? */
796 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
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));
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));
809 cr->set_y1 (_height + 1);
810 cr->set_outline (false);
811 cr->set_ignore_events (true);
813 cr->set_fill_color (UINT_RGBA_CHANGE_A (non_playing_color, 0));
815 cr->set_fill_color (non_playing_color);
819 t = pl->find_next_region_boundary (t, 1);
824 /* finish off the last rectangle */
825 cr->set_x1 (trackview.editor().sample_to_pixel (end - position));
828 if (frame_handle_start) {
829 frame_handle_start->raise_to_top ();
832 if (frame_handle_end) {
833 frame_handle_end->raise_to_top ();
836 if (name_highlight) {
837 name_highlight->raise_to_top ();
841 name_text->raise_to_top ();
846 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
848 if (_region->locked()) {
852 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
853 double const speed = rtv.track()->speed ();
855 framepos_t const pre_trim_first_frame = _region->first_frame();
857 _region->trim_front ((framepos_t) (new_bound * speed));
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);
864 bool regions_touching = false;
866 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
867 regions_touching = true;
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);
876 region_changed (ARDOUR::bounds_change);
878 return (pre_trim_first_frame != _region->first_frame()); //return true if we actually changed something
882 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
884 if (_region->locked()) {
888 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
889 double const speed = rtv.track()->speed ();
891 framepos_t const pre_trim_last_frame = _region->last_frame();
893 _region->trim_end ((framepos_t) (new_bound * speed));
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);
900 bool regions_touching = false;
902 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
903 regions_touching = true;
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);
911 region_changed (ARDOUR::bounds_change);
914 region_changed (PropertyChange (ARDOUR::Properties::length));
917 return (pre_trim_last_frame != _region->last_frame()); //return true if we actually changed something
922 RegionView::thaw_after_trim ()
924 if (_region->locked()) {
928 _region->resume_property_changes ();
933 RegionView::move_contents (frameoffset_t distance)
935 if (_region->locked()) {
938 _region->move_start (distance);
939 region_changed (PropertyChange (ARDOUR::Properties::start));
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.
947 RegionView::snap_frame_to_frame (frameoffset_t x) const
949 PublicEditor& editor = trackview.editor();
951 /* x is region relative, convert it to global absolute frames */
952 framepos_t const session_frame = x + _region->position();
954 /* try a snap in either direction */
955 framepos_t frame = session_frame;
956 editor.snap_to (frame, 0);
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);
964 /* back to region relative */
965 return frame - _region->position();