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>
27 #include "pbd/stacktrace.h"
29 #include "ardour/playlist.h"
30 #include "ardour/audioregion.h"
31 #include "ardour/audiosource.h"
32 #include "ardour/audio_diskstream.h"
33 #include "ardour/session.h"
35 #include "ardour_ui.h"
36 #include "global_signals.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"
49 #include "rgb_macros.h"
50 #include "gui_thread.h"
55 using namespace ARDOUR;
57 using namespace Editing;
58 using namespace ArdourCanvas;
60 static const int32_t sync_mark_width = 9;
62 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
64 RegionView::RegionView (ArdourCanvas::Group* parent,
66 boost::shared_ptr<ARDOUR::Region> r,
68 Gdk::Color const & basic_color,
70 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false, automation,
71 TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
72 TimeAxisViewItem::ShowNameHighlight| TimeAxisViewItem::ShowFrame))
77 , current_visible_sync_position(0.0)
79 , _enable_display(false)
81 , in_destructor(false)
82 , wait_for_data(false)
83 , _time_converter(r->session().tempo_map(), r->position())
85 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
88 RegionView::RegionView (const RegionView& other)
89 : sigc::trackable(other)
90 , TimeAxisViewItem (other)
91 , _time_converter(other._time_converter)
93 /* derived concrete type will call init () */
95 _region = other._region;
96 current_visible_sync_position = other.current_visible_sync_position;
98 _pixel_width = other._pixel_width;
100 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
103 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
104 : sigc::trackable(other)
105 , TimeAxisViewItem (other)
106 , _time_converter(other._time_converter)
108 /* this is a pseudo-copy constructor used when dragging regions
109 around on the canvas.
112 /* derived concrete type will call init () */
114 _region = other_region;
115 current_visible_sync_position = other.current_visible_sync_position;
117 _pixel_width = other._pixel_width;
119 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
122 RegionView::RegionView (ArdourCanvas::Group* parent,
124 boost::shared_ptr<ARDOUR::Region> r,
126 Gdk::Color const & basic_color,
128 TimeAxisViewItem::Visibility visibility)
129 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
134 , current_visible_sync_position(0.0)
136 , _enable_display(false)
138 , in_destructor(false)
139 , wait_for_data(false)
140 , _time_converter(r->session().tempo_map(), r->position())
145 RegionView::init (Gdk::Color const & basic_color, bool wfd)
149 in_destructor = false;
156 compute_colors (basic_color);
158 if (name_highlight) {
159 name_highlight->set_data ("regionview", this);
160 name_highlight->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
162 if (frame_handle_start) {
163 frame_handle_start->set_data ("regionview", this);
164 frame_handle_start->set_data ("isleft", (void*) 1);
165 frame_handle_start->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
166 frame_handle_start->raise_to_top();
169 if (frame_handle_end) {
170 frame_handle_end->set_data ("regionview", this);
171 frame_handle_end->set_data ("isleft", (void*) 0);
172 frame_handle_end->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
173 frame_handle_end->raise_to_top();
178 name_pixbuf->set_data ("regionview", this);
179 name_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_pixbuf, this));
183 _enable_display = true;
186 set_height (trackview.current_height());
188 _region->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed, this, _1), gui_context());
190 group->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
194 ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
196 /* XXX sync mark drag? */
199 RegionView::~RegionView ()
201 in_destructor = true;
203 for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
207 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
215 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
218 case GDK_BUTTON_RELEASE:
219 static_cast<RegionView*>(arg)->lock_toggle ();
229 RegionView::lock_toggle ()
231 _region->set_locked (!_region->locked());
235 RegionView::region_changed (const PropertyChange& what_changed)
237 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
239 if (what_changed.contains (ARDOUR::bounds_change)) {
240 region_resized (what_changed);
241 region_sync_changed ();
243 if (what_changed.contains (ARDOUR::Properties::muted)) {
246 if (what_changed.contains (ARDOUR::Properties::opaque)) {
249 if (what_changed.contains (ARDOUR::Properties::name)) {
252 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
253 region_sync_changed ();
255 if (what_changed.contains (ARDOUR::Properties::locked)) {
261 RegionView::region_locked ()
263 /* name will show locked status */
268 RegionView::region_resized (const PropertyChange& what_changed)
272 if (what_changed.contains (ARDOUR::Properties::position)) {
273 set_position (_region->position(), 0);
274 _time_converter.set_origin_b (_region->position());
277 PropertyChange s_and_l;
278 s_and_l.add (ARDOUR::Properties::start);
279 s_and_l.add (ARDOUR::Properties::length);
281 if (what_changed.contains (s_and_l)) {
283 set_duration (_region->length(), 0);
285 unit_length = _region->length() / samples_per_unit;
287 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
289 (*i)->set_duration (unit_length);
296 RegionView::reset_width_dependent_items (double pixel_width)
298 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
299 _pixel_width = pixel_width;
303 RegionView::region_muted ()
310 RegionView::region_opacity ()
316 RegionView::raise_to_top ()
318 _region->raise_to_top ();
322 RegionView::lower_to_bottom ()
324 _region->lower_to_bottom ();
328 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
333 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
342 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
343 (*i)->group->move (delta, 0.0);
351 RegionView::set_samples_per_unit (gdouble spu)
353 TimeAxisViewItem::set_samples_per_unit (spu);
355 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
356 (*i)->set_samples_per_unit (spu);
357 (*i)->set_duration (_region->length() / samples_per_unit);
360 region_sync_changed ();
364 RegionView::set_duration (framecnt_t frames, void *src)
366 if (!TimeAxisViewItem::set_duration (frames, src)) {
370 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
371 (*i)->set_duration (_region->length() / samples_per_unit);
378 RegionView::set_colors ()
380 TimeAxisViewItem::set_colors ();
383 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
384 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
389 RegionView::set_frame_color ()
391 if (_region->opaque()) {
397 TimeAxisViewItem::set_frame_color ();
401 RegionView::fake_set_opaque (bool yn)
413 RegionView::show_region_editor ()
416 editor = new RegionEditor (trackview.session(), region());
424 RegionView::hide_region_editor()
432 RegionView::make_name () const
436 // XXX nice to have some good icons for this
438 if (_region->locked()) {
440 str += _region->name();
442 } else if (_region->position_locked()) {
444 str += _region->name();
447 str = _region->name();
450 if (_region->muted()) {
451 str = string ("!") + str;
458 RegionView::region_renamed ()
460 std::string str = make_name ();
462 set_item_name (str, this);
464 reset_width_dependent_items (_pixel_width);
468 RegionView::region_sync_changed ()
471 framecnt_t sync_offset;
473 sync_offset = _region->sync_offset (sync_dir);
475 if (sync_offset == 0) {
476 /* no need for a sync mark */
486 /* points set below */
488 sync_mark = new ArdourCanvas::Polygon (*group);
489 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
491 sync_line = new ArdourCanvas::Line (*group);
492 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
493 sync_line->property_width_pixels() = 1;
496 /* this has to handle both a genuine change of position, a change of samples_per_unit,
497 and a change in the bounds of the _region->
500 if (sync_offset == 0) {
502 /* no sync mark - its the start of the region */
509 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
511 /* no sync mark - its out of the bounds of the region */
522 //points = sync_mark->property_points().get_value();
524 double offset = sync_offset / samples_per_unit;
525 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
526 points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
527 points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
528 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
529 sync_mark->property_points().set_value (points);
533 points.push_back (Gnome::Art::Point (offset, 0));
534 points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
536 sync_line->property_points().set_value (points);
543 RegionView::move (double x_delta, double y_delta)
545 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
549 get_canvas_group()->move (x_delta, y_delta);
551 /* note: ghosts never leave their tracks so y_delta for them is always zero */
553 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
554 (*i)->group->move (x_delta, 0.0);
559 RegionView::remove_ghost_in (TimeAxisView& tv)
561 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
562 if (&(*i)->trackview == &tv) {
570 RegionView::remove_ghost (GhostRegion* ghost)
576 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
585 RegionView::get_fill_color ()
591 RegionView::set_height (double h)
593 TimeAxisViewItem::set_height(h);
598 framecnt_t sync_offset;
599 sync_offset = _region->sync_offset (sync_dir);
600 double offset = sync_offset / samples_per_unit;
602 points.push_back (Gnome::Art::Point (offset, 0));
603 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
604 sync_line->property_points().set_value (points);
607 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
608 (*i)->property_y2() = h + 1;
612 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
613 * which uses them. */
615 RegionView::update_coverage_frames (LayerDisplay d)
617 /* remove old coverage frames */
618 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
622 _coverage_frames.clear ();
625 /* don't do coverage frames unless we're in stacked mode */
629 boost::shared_ptr<Playlist> pl (_region->playlist ());
634 framepos_t const position = _region->first_frame ();
635 framepos_t t = position;
636 framepos_t const end = _region->last_frame ();
638 ArdourCanvas::SimpleRect* cr = 0;
641 uint32_t const color = frame->property_fill_color_rgba ();
642 uint32_t const base_alpha = UINT_RGBA_A (color);
648 /* is this region is on top at time t? */
649 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
651 /* finish off any old rect, if required */
652 if (cr && me != new_me) {
653 cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
656 /* start off any new rect, if required */
657 if (cr == 0 || me != new_me) {
658 cr = new ArdourCanvas::SimpleRect (*group);
659 _coverage_frames.push_back (cr);
660 cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
661 cr->property_y1() = 1;
662 cr->property_y2() = _height + 1;
663 cr->property_outline_pixels() = 0;
664 /* areas that will be played get a lower alpha */
665 uint32_t alpha = base_alpha;
669 cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
672 t = pl->find_next_region_boundary (t, 1);
677 /* finish off the last rectangle */
678 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
681 if (frame_handle_start) {
682 frame_handle_start->raise_to_top ();
685 if (frame_handle_end) {
686 frame_handle_end->raise_to_top ();
689 if (name_highlight) {
690 name_highlight->raise_to_top ();
694 name_pixbuf->raise_to_top ();
699 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
701 if (_region->locked()) {
705 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
706 double const speed = rtv.track()->speed ();
708 framepos_t const pre_trim_first_frame = _region->first_frame();
710 _region->trim_front ((framepos_t) (new_bound * speed), this);
713 // Get the next region on the left of this region and shrink/expand it.
714 boost::shared_ptr<Playlist> playlist (_region->playlist());
715 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
717 bool regions_touching = false;
719 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
720 regions_touching = true;
723 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
724 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
725 region_left->trim_end (_region->first_frame() - 1, this);
729 region_changed (ARDOUR::bounds_change);
733 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
735 if (_region->locked()) {
739 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
740 double const speed = rtv.track()->speed ();
742 framepos_t const pre_trim_last_frame = _region->last_frame();
744 _region->trim_end ((framepos_t) (new_bound * speed), this);
747 // Get the next region on the right of this region and shrink/expand it.
748 boost::shared_ptr<Playlist> playlist (_region->playlist());
749 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
751 bool regions_touching = false;
753 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
754 regions_touching = true;
757 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
758 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
759 region_right->trim_front (_region->last_frame() + 1, this);
762 region_changed (ARDOUR::bounds_change);
765 region_changed (PropertyChange (ARDOUR::Properties::length));
771 RegionView::thaw_after_trim ()
773 if (_region->locked()) {
777 _region->resume_property_changes ();
782 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
784 if (_region->locked()) {
788 framepos_t new_bound;
790 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
791 double const speed = rtv.track()->speed ();
793 if (left_direction) {
794 if (swap_direction) {
795 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
797 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
800 if (swap_direction) {
801 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
803 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
807 _region->trim_start ((framepos_t) (new_bound * speed), this);
808 region_changed (PropertyChange (ARDOUR::Properties::start));