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"
36 #include "canvas/utils.h"
37 #include "canvas/colors.h"
39 #include "ardour_ui.h"
40 #include "global_signals.h"
41 #include "streamview.h"
42 #include "region_view.h"
43 #include "automation_region_view.h"
44 #include "route_time_axis.h"
45 #include "public_editor.h"
46 #include "region_editor.h"
47 #include "ghostregion.h"
48 #include "route_time_axis.h"
49 #include "ui_config.h"
51 #include "rgb_macros.h"
52 #include "gui_thread.h"
57 using namespace ARDOUR;
58 using namespace ARDOUR_UI_UTILS;
60 using namespace Editing;
62 using namespace ArdourCanvas;
64 static const int32_t sync_mark_width = 9;
66 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
68 RegionView::RegionView (ArdourCanvas::Container* parent,
70 boost::shared_ptr<ARDOUR::Region> r,
74 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false, automation,
75 (automation ? TimeAxisViewItem::ShowFrame :
76 TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
77 TimeAxisViewItem::ShowNameHighlight| TimeAxisViewItem::ShowFrame)))
82 , current_visible_sync_position(0.0)
84 , _enable_display(false)
86 , in_destructor(false)
87 , wait_for_data(false)
89 , _region_relative_time_converter(r->session().tempo_map(), r->position())
90 , _source_relative_time_converter(r->session().tempo_map(), r->position() - r->start())
92 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&RegionView::remove_ghost, this, _1), gui_context());
95 RegionView::RegionView (const RegionView& other)
96 : sigc::trackable(other)
97 , TimeAxisViewItem (other)
99 , _region_relative_time_converter(other.region_relative_time_converter())
100 , _source_relative_time_converter(other.source_relative_time_converter())
102 /* derived concrete type will call init () */
104 _region = other._region;
105 current_visible_sync_position = other.current_visible_sync_position;
107 _pixel_width = other._pixel_width;
109 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&RegionView::remove_ghost, this, _1), gui_context());
112 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
113 : sigc::trackable(other)
114 , TimeAxisViewItem (other)
116 , _region_relative_time_converter(other_region->session().tempo_map(), other_region->position())
117 , _source_relative_time_converter(other_region->session().tempo_map(), other_region->position() - other_region->start())
119 /* this is a pseudo-copy constructor used when dragging regions
120 around on the canvas.
123 /* derived concrete type will call init () */
125 _region = other_region;
126 current_visible_sync_position = other.current_visible_sync_position;
128 _pixel_width = other._pixel_width;
130 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), boost::bind (&RegionView::remove_ghost, this, _1), gui_context());
133 RegionView::RegionView (ArdourCanvas::Container* parent,
135 boost::shared_ptr<ARDOUR::Region> r,
137 uint32_t basic_color,
139 TimeAxisViewItem::Visibility visibility)
140 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
145 , current_visible_sync_position(0.0)
147 , _enable_display(false)
149 , in_destructor(false)
150 , wait_for_data(false)
152 , _region_relative_time_converter(r->session().tempo_map(), r->position())
153 , _source_relative_time_converter(r->session().tempo_map(), r->position() - r->start())
158 RegionView::init (bool wfd)
162 in_destructor = false;
169 if (name_highlight) {
170 name_highlight->set_data ("regionview", this);
171 name_highlight->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
174 if (frame_handle_start) {
175 frame_handle_start->set_data ("regionview", this);
176 frame_handle_start->set_data ("isleft", (void*) 1);
177 frame_handle_start->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
178 frame_handle_start->raise_to_top();
181 if (frame_handle_end) {
182 frame_handle_end->set_data ("regionview", this);
183 frame_handle_end->set_data ("isleft", (void*) 0);
184 frame_handle_end->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
185 frame_handle_end->raise_to_top();
189 name_text->set_data ("regionview", this);
190 name_text->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_text, this));
194 _enable_display = true;
197 set_height (trackview.current_height());
199 _region->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RegionView::region_changed, this, _1), gui_context());
203 ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
205 /* XXX sync mark drag? */
208 RegionView::~RegionView ()
210 in_destructor = true;
212 for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
216 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
220 drop_silent_frames ();
226 RegionView::canvas_group_event (GdkEvent* event)
228 if (!in_destructor) {
229 return trackview.editor().canvas_region_view_event (event, group, this);
234 RegionView::set_silent_frames (const AudioIntervalResult& silences, double /*threshold*/)
236 framecnt_t shortest = max_framecnt;
238 /* remove old silent frames */
239 drop_silent_frames ();
241 if (silences.empty()) {
245 uint32_t const color = ARDOUR_UI::config()->get_Silence();
247 for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
249 ArdourCanvas::Rectangle* cr = new ArdourCanvas::TimeRectangle (group);
250 cr->set_ignore_events (true);
251 _silent_frames.push_back (cr);
253 /* coordinates for the rect are relative to the regionview origin */
255 cr->set_x0 (trackview.editor().sample_to_pixel (i->first - _region->start()));
256 cr->set_x1 (trackview.editor().sample_to_pixel (i->second - _region->start()));
258 cr->set_y1 (_height - 2);
259 cr->set_outline (false);
260 cr->set_fill_color (color);
262 shortest = min (shortest, i->second - i->first);
265 /* Find shortest audible segment */
266 framecnt_t shortest_audible = max_framecnt;
268 framecnt_t s = _region->start();
269 for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
270 framecnt_t const dur = i->first - s;
272 shortest_audible = min (shortest_audible, dur);
278 framecnt_t const dur = _region->start() + _region->length() - 1 - s;
280 shortest_audible = min (shortest_audible, dur);
283 _silence_text = new ArdourCanvas::Text (group);
284 _silence_text->set_ignore_events (true);
285 _silence_text->set_font_description (get_font_for_style (N_("SilenceText")));
286 _silence_text->set_color (ARDOUR_UI::config()->get_SilenceText());
288 /* both positions are relative to the region start offset in source */
290 _silence_text->set_x_position (trackview.editor().sample_to_pixel (silences.front().first - _region->start()) + 10.0);
291 _silence_text->set_y_position (20.0);
293 double ms = (float) shortest/_region->session().frame_rate();
295 /* ms are now in seconds */
300 sunits = _("minutes");
302 } else if (ms < 1.0) {
309 string text = string_compose (ngettext ("%1 silent segment", "%1 silent segments", silences.size()), silences.size())
311 + string_compose (_("shortest = %1 %2"), ms, sunits);
313 if (shortest_audible != max_framepos) {
314 /* ms are now in seconds */
315 double ma = (float) shortest_audible / _region->session().frame_rate();
319 aunits = _("minutes");
321 } else if (ma < 1.0) {
328 text += string_compose (_("\n (shortest audible segment = %1 %2)"), ma, aunits);
331 _silence_text->set (text);
335 RegionView::hide_silent_frames ()
337 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
340 _silence_text->hide();
344 RegionView::drop_silent_frames ()
346 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
349 _silent_frames.clear ();
351 delete _silence_text;
356 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
359 case GDK_BUTTON_RELEASE:
360 static_cast<RegionView*>(arg)->lock_toggle ();
370 RegionView::lock_toggle ()
372 _region->set_locked (!_region->locked());
376 RegionView::region_changed (const PropertyChange& what_changed)
378 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
380 if (what_changed.contains (ARDOUR::bounds_change)) {
381 region_resized (what_changed);
382 region_sync_changed ();
384 if (what_changed.contains (ARDOUR::Properties::muted)) {
387 if (what_changed.contains (ARDOUR::Properties::opaque)) {
390 if (what_changed.contains (ARDOUR::Properties::name)) {
393 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
394 region_sync_changed ();
396 if (what_changed.contains (ARDOUR::Properties::locked)) {
399 if (what_changed.contains (ARDOUR::Properties::locked)) {
400 /* name will show locked status */
406 RegionView::region_locked ()
408 /* name will show locked status */
413 RegionView::region_resized (const PropertyChange& what_changed)
417 if (what_changed.contains (ARDOUR::Properties::position)) {
418 set_position (_region->position(), 0);
419 _region_relative_time_converter.set_origin_b (_region->position());
422 if (what_changed.contains (ARDOUR::Properties::start) || what_changed.contains (ARDOUR::Properties::position)) {
423 _source_relative_time_converter.set_origin_b (_region->position() - _region->start());
426 PropertyChange s_and_l;
427 s_and_l.add (ARDOUR::Properties::start);
428 s_and_l.add (ARDOUR::Properties::length);
430 if (what_changed.contains (s_and_l)) {
432 set_duration (_region->length(), 0);
434 unit_length = _region->length() / samples_per_pixel;
436 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
438 (*i)->set_duration (unit_length);
445 RegionView::reset_width_dependent_items (double pixel_width)
447 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
448 _pixel_width = pixel_width;
452 RegionView::region_muted ()
459 RegionView::region_opacity ()
465 RegionView::raise_to_top ()
467 _region->raise_to_top ();
471 RegionView::lower_to_bottom ()
473 _region->lower_to_bottom ();
477 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
482 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
491 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
492 (*i)->group->move (ArdourCanvas::Duple (delta, 0.0));
500 RegionView::set_samples_per_pixel (double fpp)
502 TimeAxisViewItem::set_samples_per_pixel (fpp);
504 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
505 (*i)->set_samples_per_pixel (fpp);
506 (*i)->set_duration (_region->length() / fpp);
509 region_sync_changed ();
513 RegionView::set_duration (framecnt_t frames, void *src)
515 if (!TimeAxisViewItem::set_duration (frames, src)) {
519 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
520 (*i)->set_duration (_region->length() / samples_per_pixel);
527 RegionView::fill_opacity () const
529 if (!_region->opaque()) {
533 uint32_t normal_tavi_opacity = TimeAxisViewItem::fill_opacity ();
535 return normal_tavi_opacity;
539 RegionView::set_colors ()
541 TimeAxisViewItem::set_colors ();
544 /* XXX: make these colours themable */
545 sync_mark->set_fill_color (ArdourCanvas::rgba_to_color (0, 1.0, 0, 1.0));
546 sync_line->set_outline_color (ArdourCanvas::rgba_to_color (0, 1.0, 0, 1.0));
551 RegionView::set_frame_color ()
553 TimeAxisViewItem::set_frame_color ();
557 RegionView::show_region_editor ()
560 editor = new RegionEditor (trackview.session(), region());
568 RegionView::hide_region_editor()
576 RegionView::make_name () const
580 // XXX nice to have some good icons for this
582 if (_region->locked()) {
584 str += _region->name();
586 } else if (_region->position_locked()) {
588 str += _region->name();
590 } else if (_region->video_locked()) {
592 str += _region->name();
595 str = _region->name();
598 if (_region->muted()) {
599 str = string ("!") + str;
606 RegionView::region_renamed ()
608 std::string str = make_name ();
610 set_item_name (str, this);
612 reset_width_dependent_items (_pixel_width);
616 RegionView::region_sync_changed ()
619 framecnt_t sync_offset;
621 sync_offset = _region->sync_offset (sync_dir);
623 if (sync_offset == 0) {
624 /* no need for a sync mark */
634 /* points set below */
636 sync_mark = new ArdourCanvas::Polygon (group);
637 CANVAS_DEBUG_NAME (sync_mark, string_compose ("sync mark for %1", get_item_name()));
638 sync_mark->set_fill_color (ArdourCanvas::rgba_to_color (0, 1.0, 0, 1.0)); // FIXME make a themeable colour
640 sync_line = new ArdourCanvas::Line (group);
641 CANVAS_DEBUG_NAME (sync_line, string_compose ("sync mark for %1", get_item_name()));
642 sync_line->set_outline_color (ArdourCanvas::rgba_to_color (0, 1.0, 0, 1.0)); // FIXME make a themeable colour
645 /* this has to handle both a genuine change of position, a change of samples_per_pixel
646 and a change in the bounds of the _region->
649 if (sync_offset == 0) {
651 /* no sync mark - its the start of the region */
658 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
660 /* no sync mark - its out of the bounds of the region */
671 //points = sync_mark->property_points().get_value();
673 double offset = sync_offset / samples_per_pixel;
674 points.push_back (ArdourCanvas::Duple (offset - ((sync_mark_width-1)/2), 1));
675 points.push_back (ArdourCanvas::Duple (offset + ((sync_mark_width-1)/2), 1));
676 points.push_back (ArdourCanvas::Duple (offset, sync_mark_width - 1));
677 points.push_back (ArdourCanvas::Duple (offset - ((sync_mark_width-1)/2), 1));
678 sync_mark->set (points);
681 sync_line->set (ArdourCanvas::Duple (offset, 0), ArdourCanvas::Duple (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
688 RegionView::move (double x_delta, double y_delta)
690 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
694 /* items will not prevent Item::move() moving
695 * them to a negative x-axis coordinate, which
696 * is legal, but we don't want that here.
699 ArdourCanvas::Item *item = get_canvas_group ();
701 if (item->position().x + x_delta < 0) {
702 x_delta = -item->position().x; /* move it to zero */
705 item->move (ArdourCanvas::Duple (x_delta, y_delta));
707 /* note: ghosts never leave their tracks so y_delta for them is always zero */
709 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
710 (*i)->group->move (ArdourCanvas::Duple (x_delta, 0.0));
715 RegionView::remove_ghost_in (TimeAxisView& tv)
717 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
718 if (&(*i)->trackview == &tv) {
726 RegionView::remove_ghost (GhostRegion* ghost)
732 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
741 RegionView::set_height (double h)
743 TimeAxisViewItem::set_height(h);
748 framecnt_t sync_offset;
749 sync_offset = _region->sync_offset (sync_dir);
750 double offset = sync_offset / samples_per_pixel;
753 ArdourCanvas::Duple (offset, 0),
754 ArdourCanvas::Duple (offset, h - NAME_HIGHLIGHT_SIZE)
758 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
759 (*i)->set_y1 (h + 1);
762 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
763 (*i)->set_y1 (h + 1);
768 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
769 * which uses them. */
771 RegionView::update_coverage_frames (LayerDisplay d)
773 /* remove old coverage frames */
774 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
778 _coverage_frames.clear ();
781 /* don't do coverage frames unless we're in stacked mode */
785 boost::shared_ptr<Playlist> pl (_region->playlist ());
790 framepos_t const position = _region->first_frame ();
791 framepos_t t = position;
792 framepos_t const end = _region->last_frame ();
794 ArdourCanvas::Rectangle* cr = 0;
797 /* the color that will be used to show parts of regions that will not be heard */
798 uint32_t const non_playing_color = ARDOUR_UI::config()->get_CoveredRegion ();
804 /* is this region is on top at time t? */
805 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
807 /* finish off any old rect, if required */
808 if (cr && me != new_me) {
809 cr->set_x1 (trackview.editor().sample_to_pixel (t - position));
812 /* start off any new rect, if required */
813 if (cr == 0 || me != new_me) {
814 cr = new ArdourCanvas::TimeRectangle (group);
815 _coverage_frames.push_back (cr);
816 cr->set_x0 (trackview.editor().sample_to_pixel (t - position));
818 cr->set_y1 (_height + 1);
819 cr->set_outline (false);
820 cr->set_ignore_events (true);
822 cr->set_fill_color (UINT_RGBA_CHANGE_A (non_playing_color, 0));
824 cr->set_fill_color (non_playing_color);
828 t = pl->find_next_region_boundary (t, 1);
833 /* finish off the last rectangle */
834 cr->set_x1 (trackview.editor().sample_to_pixel (end - position));
837 if (frame_handle_start) {
838 frame_handle_start->raise_to_top ();
841 if (frame_handle_end) {
842 frame_handle_end->raise_to_top ();
845 if (name_highlight) {
846 name_highlight->raise_to_top ();
850 name_text->raise_to_top ();
855 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
857 if (_region->locked()) {
861 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
862 double const speed = rtv.track()->speed ();
864 framepos_t const pre_trim_first_frame = _region->first_frame();
866 _region->trim_front ((framepos_t) (new_bound * speed));
869 // Get the next region on the left of this region and shrink/expand it.
870 boost::shared_ptr<Playlist> playlist (_region->playlist());
871 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
873 bool regions_touching = false;
875 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
876 regions_touching = true;
879 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
880 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
881 region_left->trim_end (_region->first_frame() - 1);
885 region_changed (ARDOUR::bounds_change);
887 return (pre_trim_first_frame != _region->first_frame()); //return true if we actually changed something
891 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
893 if (_region->locked()) {
897 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
898 double const speed = rtv.track()->speed ();
900 framepos_t const pre_trim_last_frame = _region->last_frame();
902 _region->trim_end ((framepos_t) (new_bound * speed));
905 // Get the next region on the right of this region and shrink/expand it.
906 boost::shared_ptr<Playlist> playlist (_region->playlist());
907 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
909 bool regions_touching = false;
911 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
912 regions_touching = true;
915 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
916 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
917 region_right->trim_front (_region->last_frame() + 1);
920 region_changed (ARDOUR::bounds_change);
923 region_changed (PropertyChange (ARDOUR::Properties::length));
926 return (pre_trim_last_frame != _region->last_frame()); //return true if we actually changed something
931 RegionView::thaw_after_trim ()
933 if (_region->locked()) {
937 _region->resume_property_changes ();
942 RegionView::move_contents (frameoffset_t distance)
944 if (_region->locked()) {
947 _region->move_start (distance);
948 region_changed (PropertyChange (ARDOUR::Properties::start));
951 /** Snap a frame offset within our region using the current snap settings.
952 * @param x Frame offset from this region's position.
953 * @return Snapped frame offset from this region's position.
956 RegionView::snap_frame_to_frame (frameoffset_t x) const
958 PublicEditor& editor = trackview.editor();
960 /* x is region relative, convert it to global absolute frames */
961 framepos_t const session_frame = x + _region->position();
963 /* try a snap in either direction */
964 framepos_t frame = session_frame;
965 editor.snap_to (frame, 0);
967 /* if we went off the beginning of the region, snap forwards */
968 if (frame < _region->position ()) {
969 frame = session_frame;
970 editor.snap_to (frame, 1);
973 /* back to region relative */
974 return frame - _region->position();