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;
308 double ms = (float) shortest/_region->session().frame_rate();
310 /* ms are now in seconds */
315 sunits = _("minutes");
317 } else if (ms < 1.0) {
324 string text = string_compose (ngettext ("%1 silent segment", "%1 silent segments", silences.size()), silences.size())
326 + string_compose (_("shortest = %1 %2"), ms, sunits);
329 /* ms are now in seconds */
330 double ma = shortest_audible / _region->session().frame_rate();
334 aunits = _("minutes");
336 } else if (ma < 1.0) {
343 text += string_compose (_("\n (shortest audible segment = %1 %2)"), ma, aunits);
346 _silence_text->property_text() = text.c_str ();
350 RegionView::hide_silent_frames ()
352 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
355 _silence_text->hide();
359 RegionView::drop_silent_frames ()
361 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
364 _silent_frames.clear ();
366 delete _silence_text;
371 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
374 case GDK_BUTTON_RELEASE:
375 static_cast<RegionView*>(arg)->lock_toggle ();
385 RegionView::lock_toggle ()
387 _region->set_locked (!_region->locked());
391 RegionView::region_changed (const PropertyChange& what_changed)
393 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
395 if (what_changed.contains (ARDOUR::bounds_change)) {
396 region_resized (what_changed);
397 region_sync_changed ();
399 if (what_changed.contains (ARDOUR::Properties::muted)) {
402 if (what_changed.contains (ARDOUR::Properties::opaque)) {
405 if (what_changed.contains (ARDOUR::Properties::name)) {
408 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
409 region_sync_changed ();
411 if (what_changed.contains (ARDOUR::Properties::locked)) {
417 RegionView::region_locked ()
419 /* name will show locked status */
424 RegionView::region_resized (const PropertyChange& what_changed)
428 if (what_changed.contains (ARDOUR::Properties::position)) {
429 set_position (_region->position(), 0);
430 _time_converter.set_origin_b (_region->position());
433 PropertyChange s_and_l;
434 s_and_l.add (ARDOUR::Properties::start);
435 s_and_l.add (ARDOUR::Properties::length);
437 if (what_changed.contains (s_and_l)) {
439 set_duration (_region->length(), 0);
441 unit_length = _region->length() / samples_per_unit;
443 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
445 (*i)->set_duration (unit_length);
452 RegionView::reset_width_dependent_items (double pixel_width)
454 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
455 _pixel_width = pixel_width;
459 RegionView::region_muted ()
466 RegionView::region_opacity ()
472 RegionView::raise_to_top ()
474 _region->raise_to_top ();
478 RegionView::lower_to_bottom ()
480 _region->lower_to_bottom ();
484 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
489 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
498 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
499 (*i)->group->move (delta, 0.0);
507 RegionView::set_samples_per_unit (gdouble spu)
509 TimeAxisViewItem::set_samples_per_unit (spu);
511 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
512 (*i)->set_samples_per_unit (spu);
513 (*i)->set_duration (_region->length() / samples_per_unit);
516 region_sync_changed ();
520 RegionView::set_duration (framecnt_t frames, void *src)
522 if (!TimeAxisViewItem::set_duration (frames, src)) {
526 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
527 (*i)->set_duration (_region->length() / samples_per_unit);
534 RegionView::set_colors ()
536 TimeAxisViewItem::set_colors ();
539 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
540 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
545 RegionView::set_frame_color ()
547 if (_region->opaque()) {
553 TimeAxisViewItem::set_frame_color ();
557 RegionView::fake_set_opaque (bool yn)
569 RegionView::show_region_editor ()
572 editor = new RegionEditor (trackview.session(), region());
580 RegionView::hide_region_editor()
588 RegionView::make_name () const
592 // XXX nice to have some good icons for this
594 if (_region->locked()) {
596 str += _region->name();
598 } else if (_region->position_locked()) {
600 str += _region->name();
603 str = _region->name();
606 if (_region->muted()) {
607 str = string ("!") + str;
614 RegionView::region_renamed ()
616 std::string str = make_name ();
618 set_item_name (str, this);
620 reset_width_dependent_items (_pixel_width);
624 RegionView::region_sync_changed ()
627 framecnt_t sync_offset;
629 sync_offset = _region->sync_offset (sync_dir);
631 if (sync_offset == 0) {
632 /* no need for a sync mark */
642 /* points set below */
644 sync_mark = new ArdourCanvas::Polygon (*group);
645 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
647 sync_line = new ArdourCanvas::Line (*group);
648 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
649 sync_line->property_width_pixels() = 1;
652 /* this has to handle both a genuine change of position, a change of samples_per_unit,
653 and a change in the bounds of the _region->
656 if (sync_offset == 0) {
658 /* no sync mark - its the start of the region */
665 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
667 /* no sync mark - its out of the bounds of the region */
678 //points = sync_mark->property_points().get_value();
680 double offset = sync_offset / samples_per_unit;
681 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
682 points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
683 points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
684 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
685 sync_mark->property_points().set_value (points);
689 points.push_back (Gnome::Art::Point (offset, 0));
690 points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
692 sync_line->property_points().set_value (points);
699 RegionView::move (double x_delta, double y_delta)
701 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
705 get_canvas_group()->move (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 (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::get_fill_color ()
747 RegionView::set_height (double h)
749 TimeAxisViewItem::set_height(h);
754 framecnt_t sync_offset;
755 sync_offset = _region->sync_offset (sync_dir);
756 double offset = sync_offset / samples_per_unit;
758 points.push_back (Gnome::Art::Point (offset, 0));
759 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
760 sync_line->property_points().set_value (points);
763 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
764 (*i)->property_y2() = h + 1;
767 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
768 (*i)->property_y2() = h + 1;
773 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
774 * which uses them. */
776 RegionView::update_coverage_frames (LayerDisplay d)
778 /* remove old coverage frames */
779 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
783 _coverage_frames.clear ();
786 /* don't do coverage frames unless we're in stacked mode */
790 boost::shared_ptr<Playlist> pl (_region->playlist ());
795 framepos_t const position = _region->first_frame ();
796 framepos_t t = position;
797 framepos_t const end = _region->last_frame ();
799 ArdourCanvas::SimpleRect* cr = 0;
802 uint32_t const color = frame->property_fill_color_rgba ();
803 uint32_t const base_alpha = UINT_RGBA_A (color);
809 /* is this region is on top at time t? */
810 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
812 /* finish off any old rect, if required */
813 if (cr && me != new_me) {
814 cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
817 /* start off any new rect, if required */
818 if (cr == 0 || me != new_me) {
819 cr = new ArdourCanvas::SimpleRect (*group);
820 _coverage_frames.push_back (cr);
821 cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
822 cr->property_y1() = 1;
823 cr->property_y2() = _height + 1;
824 cr->property_outline_pixels() = 0;
825 /* areas that will be played get a lower alpha */
826 uint32_t alpha = base_alpha;
830 cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
833 t = pl->find_next_region_boundary (t, 1);
838 /* finish off the last rectangle */
839 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
842 if (frame_handle_start) {
843 frame_handle_start->raise_to_top ();
846 if (frame_handle_end) {
847 frame_handle_end->raise_to_top ();
850 if (name_highlight) {
851 name_highlight->raise_to_top ();
855 name_pixbuf->raise_to_top ();
860 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
862 if (_region->locked()) {
866 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
867 double const speed = rtv.track()->speed ();
869 framepos_t const pre_trim_first_frame = _region->first_frame();
871 _region->trim_front ((framepos_t) (new_bound * speed), this);
874 // Get the next region on the left of this region and shrink/expand it.
875 boost::shared_ptr<Playlist> playlist (_region->playlist());
876 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
878 bool regions_touching = false;
880 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
881 regions_touching = true;
884 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
885 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
886 region_left->trim_end (_region->first_frame() - 1, this);
890 region_changed (ARDOUR::bounds_change);
894 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
896 if (_region->locked()) {
900 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
901 double const speed = rtv.track()->speed ();
903 framepos_t const pre_trim_last_frame = _region->last_frame();
905 _region->trim_end ((framepos_t) (new_bound * speed), this);
908 // Get the next region on the right of this region and shrink/expand it.
909 boost::shared_ptr<Playlist> playlist (_region->playlist());
910 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
912 bool regions_touching = false;
914 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
915 regions_touching = true;
918 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
919 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
920 region_right->trim_front (_region->last_frame() + 1, this);
923 region_changed (ARDOUR::bounds_change);
926 region_changed (PropertyChange (ARDOUR::Properties::length));
932 RegionView::thaw_after_trim ()
934 if (_region->locked()) {
938 _region->resume_property_changes ();
943 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
945 if (_region->locked()) {
949 framepos_t new_bound;
951 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
952 double const speed = rtv.track()->speed ();
954 if (left_direction) {
955 if (swap_direction) {
956 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
958 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
961 if (swap_direction) {
962 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
964 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
968 _region->trim_start ((framepos_t) (new_bound * speed), this);
969 region_changed (PropertyChange (ARDOUR::Properties::start));