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 "canvas-noevent-text.h"
38 #include "streamview.h"
39 #include "region_view.h"
40 #include "automation_region_view.h"
41 #include "route_time_axis.h"
42 #include "simplerect.h"
43 #include "simpleline.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;
59 using namespace Editing;
61 using namespace ArdourCanvas;
63 static const int32_t sync_mark_width = 9;
65 PBD::Signal1<void,RegionView*> RegionView::RegionViewGoingAway;
67 RegionView::RegionView (ArdourCanvas::Group* parent,
69 boost::shared_ptr<ARDOUR::Region> r,
71 Gdk::Color const & basic_color,
73 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false, automation,
74 TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText|
75 TimeAxisViewItem::ShowNameHighlight| TimeAxisViewItem::ShowFrame))
80 , current_visible_sync_position(0.0)
82 , _enable_display(false)
84 , in_destructor(false)
85 , wait_for_data(false)
87 , _time_converter(r->session().tempo_map(), r->position())
89 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
92 RegionView::RegionView (const RegionView& other)
93 : sigc::trackable(other)
94 , TimeAxisViewItem (other)
96 , _time_converter(other._time_converter)
98 /* derived concrete type will call init () */
100 _region = other._region;
101 current_visible_sync_position = other.current_visible_sync_position;
103 _pixel_width = other._pixel_width;
105 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
108 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
109 : sigc::trackable(other)
110 , TimeAxisViewItem (other)
112 , _time_converter(other._time_converter)
114 /* this is a pseudo-copy constructor used when dragging regions
115 around on the canvas.
118 /* derived concrete type will call init () */
120 _region = other_region;
121 current_visible_sync_position = other.current_visible_sync_position;
123 _pixel_width = other._pixel_width;
125 GhostRegion::CatchDeletion.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost, this, _1), gui_context());
128 RegionView::RegionView (ArdourCanvas::Group* parent,
130 boost::shared_ptr<ARDOUR::Region> r,
132 Gdk::Color const & basic_color,
134 TimeAxisViewItem::Visibility visibility)
135 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
140 , current_visible_sync_position(0.0)
142 , _enable_display(false)
144 , in_destructor(false)
145 , wait_for_data(false)
147 , _time_converter(r->session().tempo_map(), r->position())
152 RegionView::init (Gdk::Color const & basic_color, bool wfd)
156 in_destructor = false;
163 compute_colors (basic_color);
165 if (name_highlight) {
166 name_highlight->set_data ("regionview", this);
167 name_highlight->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
169 if (frame_handle_start) {
170 frame_handle_start->set_data ("regionview", this);
171 frame_handle_start->set_data ("isleft", (void*) 1);
172 frame_handle_start->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_start, this));
173 frame_handle_start->raise_to_top();
176 if (frame_handle_end) {
177 frame_handle_end->set_data ("regionview", this);
178 frame_handle_end->set_data ("isleft", (void*) 0);
179 frame_handle_end->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event), frame_handle_end, this));
180 frame_handle_end->raise_to_top();
185 name_pixbuf->set_data ("regionview", this);
186 name_pixbuf->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_pixbuf, this));
190 _enable_display = true;
193 set_height (trackview.current_height());
195 _region->PropertyChanged.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed, this, _1), gui_context());
197 group->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event), group, this));
201 ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
203 /* XXX sync mark drag? */
206 RegionView::~RegionView ()
208 in_destructor = true;
210 for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
214 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
218 drop_silent_frames ();
224 RegionView::set_silent_frames (const AudioIntervalResult& silences)
226 framecnt_t shortest = max_framecnt;
228 /* remove old silent frames */
229 drop_silent_frames ();
231 if (!silences.empty()) {
232 uint32_t const color = ARDOUR_UI::config()->canvasvar_Silence.get();
234 for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
237 ArdourCanvas::SimpleRect* cr = new ArdourCanvas::SimpleRect (*group);
238 _silent_frames.push_back (cr);
239 cr->property_x1() = trackview.editor().frame_to_pixel ((*i).first);
240 cr->property_y1() = 1;
241 cr->property_y2() = _height - 2;
242 cr->property_outline_pixels() = 0;
243 cr->property_fill_color_rgba () = color;
244 cr->property_x2() = trackview.editor().frame_to_pixel ((*i).first + (*i).second);
246 if ((*i).second < shortest) {
247 shortest= (*i).second;
251 _silence_text = new ArdourCanvas::NoEventText (*group);
252 _silence_text->property_font_desc() = *(get_font_for_style (N_("VerboseCanvasCusor")));
253 _silence_text->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SilenceText.get();
254 _silence_text->property_anchor() = ANCHOR_NW;
256 /* both positions are relative to the RV start */
258 _silence_text->property_x() = trackview.editor().frame_to_pixel (silences.front().first) + 10.0;
259 _silence_text->property_y() = 20.0;
264 ms = (float) shortest/_region->session().frame_rate();
266 /* ms are now in seconds */
269 sunits = _("minutes");
271 } else if (ms < 1.0) {
280 _silence_text->property_text() = string_compose (_("%1 silent segments, shortest = %2 %3"),
281 silences.size(), ms, sunits).c_str();
286 RegionView::hide_silent_frames ()
288 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
291 _silence_text->hide();
295 RegionView::drop_silent_frames ()
297 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
300 _silent_frames.clear ();
301 delete _silence_text;
306 RegionView::show_silent_frames ()
308 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin (); i != _silent_frames.end (); ++i) {
311 _silence_text->show ();
315 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
318 case GDK_BUTTON_RELEASE:
319 static_cast<RegionView*>(arg)->lock_toggle ();
329 RegionView::lock_toggle ()
331 _region->set_locked (!_region->locked());
335 RegionView::region_changed (const PropertyChange& what_changed)
337 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
339 if (what_changed.contains (ARDOUR::bounds_change)) {
340 region_resized (what_changed);
341 region_sync_changed ();
343 if (what_changed.contains (ARDOUR::Properties::muted)) {
346 if (what_changed.contains (ARDOUR::Properties::opaque)) {
349 if (what_changed.contains (ARDOUR::Properties::name)) {
352 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
353 region_sync_changed ();
355 if (what_changed.contains (ARDOUR::Properties::locked)) {
361 RegionView::region_locked ()
363 /* name will show locked status */
368 RegionView::region_resized (const PropertyChange& what_changed)
372 if (what_changed.contains (ARDOUR::Properties::position)) {
373 set_position (_region->position(), 0);
374 _time_converter.set_origin_b (_region->position());
377 PropertyChange s_and_l;
378 s_and_l.add (ARDOUR::Properties::start);
379 s_and_l.add (ARDOUR::Properties::length);
381 if (what_changed.contains (s_and_l)) {
383 set_duration (_region->length(), 0);
385 unit_length = _region->length() / samples_per_unit;
387 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
389 (*i)->set_duration (unit_length);
396 RegionView::reset_width_dependent_items (double pixel_width)
398 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
399 _pixel_width = pixel_width;
403 RegionView::region_muted ()
410 RegionView::region_opacity ()
416 RegionView::raise_to_top ()
418 _region->raise_to_top ();
422 RegionView::lower_to_bottom ()
424 _region->lower_to_bottom ();
428 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
433 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
442 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
443 (*i)->group->move (delta, 0.0);
451 RegionView::set_samples_per_unit (gdouble spu)
453 TimeAxisViewItem::set_samples_per_unit (spu);
455 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
456 (*i)->set_samples_per_unit (spu);
457 (*i)->set_duration (_region->length() / samples_per_unit);
460 region_sync_changed ();
464 RegionView::set_duration (framecnt_t frames, void *src)
466 if (!TimeAxisViewItem::set_duration (frames, src)) {
470 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
471 (*i)->set_duration (_region->length() / samples_per_unit);
478 RegionView::set_colors ()
480 TimeAxisViewItem::set_colors ();
483 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
484 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
489 RegionView::set_frame_color ()
491 if (_region->opaque()) {
497 TimeAxisViewItem::set_frame_color ();
501 RegionView::fake_set_opaque (bool yn)
513 RegionView::show_region_editor ()
516 editor = new RegionEditor (trackview.session(), region());
524 RegionView::hide_region_editor()
532 RegionView::make_name () const
536 // XXX nice to have some good icons for this
538 if (_region->locked()) {
540 str += _region->name();
542 } else if (_region->position_locked()) {
544 str += _region->name();
547 str = _region->name();
550 if (_region->muted()) {
551 str = string ("!") + str;
558 RegionView::region_renamed ()
560 std::string str = make_name ();
562 set_item_name (str, this);
564 reset_width_dependent_items (_pixel_width);
568 RegionView::region_sync_changed ()
571 framecnt_t sync_offset;
573 sync_offset = _region->sync_offset (sync_dir);
575 if (sync_offset == 0) {
576 /* no need for a sync mark */
586 /* points set below */
588 sync_mark = new ArdourCanvas::Polygon (*group);
589 sync_mark->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
591 sync_line = new ArdourCanvas::Line (*group);
592 sync_line->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
593 sync_line->property_width_pixels() = 1;
596 /* this has to handle both a genuine change of position, a change of samples_per_unit,
597 and a change in the bounds of the _region->
600 if (sync_offset == 0) {
602 /* no sync mark - its the start of the region */
609 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
611 /* no sync mark - its out of the bounds of the region */
622 //points = sync_mark->property_points().get_value();
624 double offset = sync_offset / samples_per_unit;
625 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
626 points.push_back (Gnome::Art::Point (offset + ((sync_mark_width-1)/2), 1));
627 points.push_back (Gnome::Art::Point (offset, sync_mark_width - 1));
628 points.push_back (Gnome::Art::Point (offset - ((sync_mark_width-1)/2), 1));
629 sync_mark->property_points().set_value (points);
633 points.push_back (Gnome::Art::Point (offset, 0));
634 points.push_back (Gnome::Art::Point (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
636 sync_line->property_points().set_value (points);
643 RegionView::move (double x_delta, double y_delta)
645 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
649 get_canvas_group()->move (x_delta, y_delta);
651 /* note: ghosts never leave their tracks so y_delta for them is always zero */
653 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
654 (*i)->group->move (x_delta, 0.0);
659 RegionView::remove_ghost_in (TimeAxisView& tv)
661 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
662 if (&(*i)->trackview == &tv) {
670 RegionView::remove_ghost (GhostRegion* ghost)
676 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
685 RegionView::get_fill_color ()
691 RegionView::set_height (double h)
693 TimeAxisViewItem::set_height(h);
698 framecnt_t sync_offset;
699 sync_offset = _region->sync_offset (sync_dir);
700 double offset = sync_offset / samples_per_unit;
702 points.push_back (Gnome::Art::Point (offset, 0));
703 points.push_back (Gnome::Art::Point (offset, h - NAME_HIGHLIGHT_SIZE));
704 sync_line->property_points().set_value (points);
707 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin(); i != _coverage_frames.end(); ++i) {
708 (*i)->property_y2() = h + 1;
711 for (list<ArdourCanvas::SimpleRect*>::iterator i = _silent_frames.begin(); i != _silent_frames.end(); ++i) {
712 (*i)->property_y2() = h + 1;
717 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
718 * which uses them. */
720 RegionView::update_coverage_frames (LayerDisplay d)
722 /* remove old coverage frames */
723 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
727 _coverage_frames.clear ();
730 /* don't do coverage frames unless we're in stacked mode */
734 boost::shared_ptr<Playlist> pl (_region->playlist ());
739 framepos_t const position = _region->first_frame ();
740 framepos_t t = position;
741 framepos_t const end = _region->last_frame ();
743 ArdourCanvas::SimpleRect* cr = 0;
746 uint32_t const color = frame->property_fill_color_rgba ();
747 uint32_t const base_alpha = UINT_RGBA_A (color);
753 /* is this region is on top at time t? */
754 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
756 /* finish off any old rect, if required */
757 if (cr && me != new_me) {
758 cr->property_x2() = trackview.editor().frame_to_pixel (t - position);
761 /* start off any new rect, if required */
762 if (cr == 0 || me != new_me) {
763 cr = new ArdourCanvas::SimpleRect (*group);
764 _coverage_frames.push_back (cr);
765 cr->property_x1() = trackview.editor().frame_to_pixel (t - position);
766 cr->property_y1() = 1;
767 cr->property_y2() = _height + 1;
768 cr->property_outline_pixels() = 0;
769 /* areas that will be played get a lower alpha */
770 uint32_t alpha = base_alpha;
774 cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
777 t = pl->find_next_region_boundary (t, 1);
782 /* finish off the last rectangle */
783 cr->property_x2() = trackview.editor().frame_to_pixel (end - position);
786 if (frame_handle_start) {
787 frame_handle_start->raise_to_top ();
790 if (frame_handle_end) {
791 frame_handle_end->raise_to_top ();
794 if (name_highlight) {
795 name_highlight->raise_to_top ();
799 name_pixbuf->raise_to_top ();
804 RegionView::trim_front (framepos_t new_bound, bool no_overlap)
806 if (_region->locked()) {
810 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
811 double const speed = rtv.track()->speed ();
813 framepos_t const pre_trim_first_frame = _region->first_frame();
815 _region->trim_front ((framepos_t) (new_bound * speed), this);
818 // Get the next region on the left of this region and shrink/expand it.
819 boost::shared_ptr<Playlist> playlist (_region->playlist());
820 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_frame, End, 0);
822 bool regions_touching = false;
824 if (region_left != 0 && (pre_trim_first_frame == region_left->last_frame() + 1)) {
825 regions_touching = true;
828 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
829 if (region_left != 0 && (region_left->last_frame() > _region->first_frame() || regions_touching)) {
830 region_left->trim_end (_region->first_frame() - 1, this);
834 region_changed (ARDOUR::bounds_change);
838 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
840 if (_region->locked()) {
844 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
845 double const speed = rtv.track()->speed ();
847 framepos_t const pre_trim_last_frame = _region->last_frame();
849 _region->trim_end ((framepos_t) (new_bound * speed), this);
852 // Get the next region on the right of this region and shrink/expand it.
853 boost::shared_ptr<Playlist> playlist (_region->playlist());
854 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_frame, Start, 1);
856 bool regions_touching = false;
858 if (region_right != 0 && (pre_trim_last_frame == region_right->first_frame() - 1)) {
859 regions_touching = true;
862 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
863 if (region_right != 0 && (region_right->first_frame() < _region->last_frame() || regions_touching)) {
864 region_right->trim_front (_region->last_frame() + 1, this);
867 region_changed (ARDOUR::bounds_change);
870 region_changed (PropertyChange (ARDOUR::Properties::length));
876 RegionView::thaw_after_trim ()
878 if (_region->locked()) {
882 _region->resume_property_changes ();
887 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
889 if (_region->locked()) {
893 framepos_t new_bound;
895 RouteTimeAxisView& rtv = dynamic_cast<RouteTimeAxisView&> (trackview);
896 double const speed = rtv.track()->speed ();
898 if (left_direction) {
899 if (swap_direction) {
900 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
902 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
905 if (swap_direction) {
906 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
908 new_bound = (framepos_t) (_region->position() / speed) + frame_delta;
912 _region->trim_start ((framepos_t) (new_bound * speed), this);
913 region_changed (PropertyChange (ARDOUR::Properties::start));