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/audioregion.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/audio_diskstream.h"
32 #include "ardour/session.h"
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"
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"
50 #include "rgb_macros.h"
51 #include "gui_thread.h"
56 using namespace ARDOUR;
58 using namespace Editing;
60 using namespace ArdourCanvas;
62 static const int32_t sync_mark_width = 9;
64 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
66 RegionView::RegionView (ArdourCanvas::Group* parent,
68 boost::shared_ptr<ARDOUR::Region> r,
70 Gdk::Color const & basic_color,
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))
79 , current_visible_sync_position(0.0)
81 , _enable_display(false)
83 , in_destructor(false)
84 , wait_for_data(false)
86 , _time_converter(r->session().tempo_map(), r->position())
88 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
91 RegionView::RegionView (const RegionView& other)
92 : sigc::trackable(other)
93 , TimeAxisViewItem (other)
95 , _time_converter(other._time_converter)
97 /* derived concrete type will call init () */
99 _region = other._region;
100 current_visible_sync_position = other.current_visible_sync_position;
102 _pixel_width = other._pixel_width;
104 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
107 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
108 : sigc::trackable(other)
109 , TimeAxisViewItem (other)
111 , _time_converter(other._time_converter)
113 /* this is a pseudo-copy constructor used when dragging regions
114 around on the canvas.
117 /* derived concrete type will call init () */
119 _region = other_region;
120 current_visible_sync_position = other.current_visible_sync_position;
122 _pixel_width = other._pixel_width;
124 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
127 RegionView::RegionView (ArdourCanvas::Group* parent,
129 boost::shared_ptr<ARDOUR::Region> r,
131 Gdk::Color const & basic_color,
133 TimeAxisViewItem::Visibility visibility)
134 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
139 , current_visible_sync_position(0.0)
141 , _enable_display(false)
143 , in_destructor(false)
144 , wait_for_data(false)
146 , _time_converter(r->session().tempo_map(), r->position())
151 RegionView::init (Gdk::Color const & basic_color, bool wfd)
155 in_destructor = false;
162 compute_colors (basic_color);
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));
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();
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();
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));
189 _enable_display = true;
192 set_height (trackview.current_height());
194 _region->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed, this, _1), gui_context());
196 group->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
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::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
217 drop_silent_frames ();
223 RegionView::set_silent_frames (const AudioIntervalResult& silences, double threshold)
225 framecnt_t shortest = max_framecnt;
226 framecnt_t shortest_audible = max_framecnt;
228 /* remove old silent frames */
229 drop_silent_frames ();
231 if (silences.empty()) {
238 bool seen_audible = false;
239 AudioIntervalResult::const_iterator s;
240 uint32_t const color = ARDOUR_UI::config()->canvasvar_Silence.get();
242 start = _region->start();
243 s = silences.begin();
245 if (s->first == start) {
246 /* segment starting at zero is silent */
250 /* segment starting at zero is audible, and begins at the start of the region in the source */
255 while (start < _region->start() + _region->length()) {
257 framecnt_t interval_duration = end - start;
259 if (interval_duration > 0) {
262 ArdourCanvas::SimpleRect* cr = new ArdourCanvas::SimpleRect (*group);
263 _silent_frames.push_back (cr);
265 /* coordinates for the rect are relative to the regionview origin */
267 cr->property_x1() = trackview.editor().frame_to_pixel (s->first - _region->start());
268 cr->property_x2() = trackview.editor().frame_to_pixel (s->second - _region->start());
269 cr->property_y1() = 1;
270 cr->property_y2() = _height - 2;
271 cr->property_outline_pixels() = 0;
272 cr->property_fill_color_rgba () = color;
274 if (interval_duration < shortest) {
275 shortest = interval_duration;
278 } else if (interval_duration > 0) {
280 if (interval_duration < shortest_audible) {
281 shortest_audible = interval_duration;
286 in_silence = !in_silence;
289 if (s == silences.end()) {
290 end = _region->start() + _region->length();
298 _silence_text = new ArdourCanvas::NoEventText (*group);
299 _silence_text->property_font_desc() = *(get_font_for_style (N_("SilenceText")));
300 _silence_text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SilenceText.get();
301 _silence_text->property_anchor() = ANCHOR_NW;
303 /* both positions are relative to the region start offset in source */
305 _silence_text->property_x() = trackview.editor().frame_to_pixel (silences.front().first - _region->start()) + 10.0;
306 _silence_text->property_y() = 20.0;
312 if (silences.size() > 1) {
313 noun = _("silent segments");
315 noun = _("silent segment");
318 ms = (float) shortest/_region->session().frame_rate();
320 /* ms are now in seconds */
323 sunits = _("minutes");
325 } else if (ms < 1.0) {
333 /* ms are now in seconds */
334 double ma = shortest_audible / _region->session().frame_rate();
338 aunits = _("minutes");
340 } else if (ma < 1.0) {
347 _silence_text->property_text() = string_compose (_("%1 %2, shortest = %3 %4\n (shortest audible segment = %5 %6)"),
348 silences.size(), noun,
349 ms, sunits, ma, aunits).c_str();
351 _silence_text->property_text() = string_compose (_("%1 %2, shortest = %3 %4"),
352 silences.size(), noun, ms, sunits).c_str();
357 RegionView::hide_silent_frames ()
359 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
362 _silence_text->hide();
366 RegionView::drop_silent_frames ()
368 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
371 _silent_frames.clear ();
373 delete _silence_text;
378 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
381 case GDK_BUTTON_RELEASE:
382 static_cast<RegionView*>(arg)->lock_toggle ();
392 RegionView::lock_toggle ()
394 _region->set_locked (!_region->locked());
398 RegionView::region_changed (const PropertyChange& what_changed)
400 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
402 if (what_changed.contains (ARDOUR::bounds_change)) {
403 region_resized (what_changed);
404 region_sync_changed ();
406 if (what_changed.contains (ARDOUR::Properties::muted)) {
409 if (what_changed.contains (ARDOUR::Properties::opaque)) {
412 if (what_changed.contains (ARDOUR::Properties::name)) {
415 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
416 region_sync_changed ();
418 if (what_changed.contains (ARDOUR::Properties::locked)) {
424 RegionView::region_locked ()
426 /* name will show locked status */
431 RegionView::region_resized (const PropertyChange& what_changed)
435 if (what_changed.contains (ARDOUR::Properties::position)) {
436 set_position (_region->position(), 0);
437 _time_converter.set_origin_b (_region->position());
440 PropertyChange s_and_l;
441 s_and_l.add (ARDOUR::Properties::start);
442 s_and_l.add (ARDOUR::Properties::length);
444 if (what_changed.contains (s_and_l)) {
446 set_duration (_region->length(), 0);
448 unit_length = _region->length() / samples_per_unit;
450 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
452 (*i)->set_duration (unit_length);
459 RegionView::reset_width_dependent_items (double pixel_width)
461 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
462 _pixel_width = pixel_width;
466 RegionView::region_muted ()
473 RegionView::region_opacity ()
479 RegionView::raise_to_top ()
481 _region->raise_to_top ();
485 RegionView::lower_to_bottom ()
487 _region->lower_to_bottom ();
491 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
496 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
505 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
506 (*i)->group->move (delta, 0.0);
514 RegionView::set_samples_per_unit (gdouble spu)
516 TimeAxisViewItem::set_samples_per_unit (spu);
518 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
519 (*i)->set_samples_per_unit (spu);
520 (*i)->set_duration (_region->length() / samples_per_unit);
523 region_sync_changed ();
527 RegionView::set_duration (framecnt_t frames, void *src)
529 if (!TimeAxisViewItem::set_duration (frames, src)) {
533 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
534 (*i)->set_duration (_region->length() / samples_per_unit);
541 RegionView::set_colors ()
543 TimeAxisViewItem::set_colors ();
546 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
547 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
552 RegionView::set_frame_color ()
554 if (_region->opaque()) {
560 TimeAxisViewItem::set_frame_color ();
564 RegionView::fake_set_opaque (bool yn)
576 RegionView::show_region_editor ()
579 editor = new RegionEditor (trackview.session(), region());
587 RegionView::hide_region_editor()
595 RegionView::make_name () const
599 // XXX nice to have some good icons for this
601 if (_region->locked()) {
603 str += _region->name();
605 } else if (_region->position_locked()) {
607 str += _region->name();
610 str = _region->name();
613 if (_region->muted()) {
614 str = string ("!") + str;
621 RegionView::region_renamed ()
623 std::string str = make_name ();
625 set_item_name (str, this);
627 reset_width_dependent_items (_pixel_width);
631 RegionView::region_sync_changed ()
634 framecnt_t sync_offset;
636 sync_offset = _region->sync_offset (sync_dir);
638 if (sync_offset == 0) {
639 /* no need for a sync mark */
649 /* points set below */
651 sync_mark = new ArdourCanvas::Polygon (*group);
652 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
654 sync_line = new ArdourCanvas::Line (*group);
655 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
656 sync_line->property_width_pixels() = 1;
659 /* this has to handle both a genuine change of position, a change of samples_per_unit,
660 and a change in the bounds of the _region->
663 if (sync_offset == 0) {
665 /* no sync mark - its the start of the region */
672 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
674 /* no sync mark - its out of the bounds of the region */
685 //points = sync_mark->property_points().get_value();
687 double offset = sync_offset / samples_per_unit;
688 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
689 points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
690 points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
691 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
692 sync_mark->property_points().set_value (points);
696 points.push_back (Gnome::Art::Point (offset, 0));
697 points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
699 sync_line->property_points().set_value (points);
706 RegionView::move (double x_delta, double y_delta)
708 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
712 get_canvas_group()->move (x_delta, y_delta);
714 /* note: ghosts never leave their tracks so y_delta for them is always zero */
716 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
717 (*i)->group->move (x_delta, 0.0);
722 RegionView::remove_ghost_in (TimeAxisView& tv)
724 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
725 if (&(*i)->trackview == &tv) {
733 RegionView::remove_ghost (GhostRegion* ghost)
739 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
748 RegionView::get_fill_color ()
754 RegionView::set_height (double h)
756 TimeAxisViewItem::set_height(h);
761 framecnt_t sync_offset;
762 sync_offset = _region->sync_offset (sync_dir);
763 double offset = sync_offset / samples_per_unit;
765 points.push_back (Gnome::Art::Point (offset, 0));
766 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
767 sync_line->property_points().set_value (points);
770 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
771 (*i)->property_y2() = h + 1;
774 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
775 (*i)->property_y2() = h + 1;
780 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
781 * which uses them. */
783 RegionView::update_coverage_frames (LayerDisplay d)
785 /* remove old coverage frames */
786 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
790 _coverage_frames.clear ();
793 /* don't do coverage frames unless we're in stacked mode */
797 boost::shared_ptr<Playlist> pl (_region->playlist ());
802 framepos_t const position = _region->first_frame ();
803 framepos_t t = position;
804 framepos_t const end = _region->last_frame ();
806 ArdourCanvas::SimpleRect* cr = 0;
809 uint32_t const color = frame->property_fill_color_rgba ();
810 uint32_t const base_alpha = UINT_RGBA_A (color);
816 /* is this region is on top at time t? */
817 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
819 /* finish off any old rect, if required */
820 if (cr && me != new_me) {
821 cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
824 /* start off any new rect, if required */
825 if (cr == 0 || me != new_me) {
826 cr = new ArdourCanvas::SimpleRect (*group);
827 _coverage_frames.push_back (cr);
828 cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
829 cr->property_y1() = 1;
830 cr->property_y2() = _height + 1;
831 cr->property_outline_pixels() = 0;
832 /* areas that will be played get a lower alpha */
833 uint32_t alpha = base_alpha;
837 cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
840 t = pl->find_next_region_boundary (t, 1);
845 /* finish off the last rectangle */
846 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
849 if (frame_handle_start) {
850 frame_handle_start->raise_to_top ();
853 if (frame_handle_end) {
854 frame_handle_end->raise_to_top ();
857 if (name_highlight) {
858 name_highlight->raise_to_top ();
862 name_pixbuf->raise_to_top ();
867 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
869 if (_region->locked()) {
873 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
874 double const speed = rtv.track()->speed ();
876 framepos_t const pre_trim_first_frame = _region->first_frame();
878 _region->trim_front ((framepos_t) (new_bound * speed), this);
881 // Get the next region on the left of this region and shrink/expand it.
882 boost::shared_ptr<Playlist> playlist (_region->playlist());
883 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
885 bool regions_touching = false;
887 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
888 regions_touching = true;
891 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
892 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
893 region_left->trim_end (_region->first_frame() - 1, this);
897 region_changed (ARDOUR::bounds_change);
901 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
903 if (_region->locked()) {
907 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
908 double const speed = rtv.track()->speed ();
910 framepos_t const pre_trim_last_frame = _region->last_frame();
912 _region->trim_end ((framepos_t) (new_bound * speed), this);
915 // Get the next region on the right of this region and shrink/expand it.
916 boost::shared_ptr<Playlist> playlist (_region->playlist());
917 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
919 bool regions_touching = false;
921 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
922 regions_touching = true;
925 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
926 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
927 region_right->trim_front (_region->last_frame() + 1, this);
930 region_changed (ARDOUR::bounds_change);
933 region_changed (PropertyChange (ARDOUR::Properties::length));
939 RegionView::thaw_after_trim ()
941 if (_region->locked()) {
945 _region->resume_property_changes ();
950 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
952 if (_region->locked()) {
956 framepos_t new_bound;
958 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
959 double const speed = rtv.track()->speed ();
961 if (left_direction) {
962 if (swap_direction) {
963 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
965 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
968 if (swap_direction) {
969 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
971 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
975 _region->trim_start ((framepos_t) (new_bound * speed), this);
976 region_changed (PropertyChange (ARDOUR::Properties::start));