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.
25 #include <gtkmm2ext/gtk_ui.h>
27 #include "ardour/playlist.h"
28 #include "ardour/profile.h"
29 #include "ardour/session.h"
31 #include "gtkmm2ext/colors.h"
33 #include "canvas/polygon.h"
34 #include "canvas/debug.h"
35 #include "canvas/pixbuf.h"
36 #include "canvas/text.h"
37 #include "canvas/line.h"
39 #include "streamview.h"
40 #include "region_view.h"
41 #include "automation_region_view.h"
42 #include "route_time_axis.h"
43 #include "public_editor.h"
44 #include "region_editor.h"
45 #include "ghostregion.h"
46 #include "route_time_axis.h"
47 #include "ui_config.h"
49 #include "rgb_macros.h"
50 #include "gui_thread.h"
55 using namespace ARDOUR;
56 using namespace ARDOUR_UI_UTILS;
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::Container* parent,
68 boost::shared_ptr<ARDOUR::Region> r,
72 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), false, automation,
73 (automation ? TimeAxisViewItem::ShowFrame :
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)
90 RegionView::RegionView (const RegionView& other)
91 : sigc::trackable(other)
92 , TimeAxisViewItem (other)
95 /* derived concrete type will call init () */
97 _region = other._region;
98 current_visible_sync_position = other.current_visible_sync_position;
100 _pixel_width = other._pixel_width;
103 RegionView::RegionView (const RegionView& other, boost::shared_ptr<Region> other_region)
104 : sigc::trackable(other)
105 , TimeAxisViewItem (other)
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;
120 RegionView::RegionView (ArdourCanvas::Container* parent,
122 boost::shared_ptr<ARDOUR::Region> r,
124 uint32_t basic_color,
126 TimeAxisViewItem::Visibility visibility)
127 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
132 , current_visible_sync_position(0.0)
134 , _enable_display(false)
136 , in_destructor(false)
137 , wait_for_data(false)
143 RegionView::init (bool wfd)
147 in_destructor = false;
154 if (name_highlight) {
155 name_highlight->set_data ("regionview", this);
156 name_highlight->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event), name_highlight, this));
159 if (sample_handle_start) {
160 sample_handle_start->set_data ("regionview", this);
161 sample_handle_start->set_data ("isleft", (void*) 1);
162 sample_handle_start->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_sample_handle_event), sample_handle_start, this));
163 sample_handle_start->raise_to_top();
166 if (sample_handle_end) {
167 sample_handle_end->set_data ("regionview", this);
168 sample_handle_end->set_data ("isleft", (void*) 0);
169 sample_handle_end->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_sample_handle_event), sample_handle_end, this));
170 sample_handle_end->raise_to_top();
174 name_text->set_data ("regionview", this);
175 name_text->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event), name_text, this));
179 _enable_display = true;
182 /* derived class calls set_height () including RegionView::set_height() in ::init() */
183 //set_height (trackview.current_height());
185 _region->PropertyChanged.connect (*this, invalidator (*this), boost::bind (&RegionView::region_changed, this, _1), gui_context());
187 /* derived class calls set_colors () including RegionView::set_colors() in ::init() */
189 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &RegionView::color_handler));
191 /* XXX sync mark drag? */
194 RegionView::~RegionView ()
196 in_destructor = true;
198 for (vector<GhostRegion*>::iterator g = ghosts.begin(); g != ghosts.end(); ++g) {
202 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_samples.begin (); i != _coverage_samples.end (); ++i) {
206 drop_silent_samples ();
212 RegionView::canvas_group_event (GdkEvent* event)
214 if (!in_destructor) {
215 return trackview.editor().canvas_region_view_event (event, group, this);
221 RegionView::set_silent_samples (const AudioIntervalResult& silences, double /*threshold*/)
223 samplecnt_t shortest = max_samplecnt;
225 /* remove old silent samples */
226 drop_silent_samples ();
228 if (silences.empty()) {
232 uint32_t const color = UIConfiguration::instance().color_mod ("silence", "silence");
234 for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
236 ArdourCanvas::Rectangle* cr = new ArdourCanvas::Rectangle (group);
237 cr->set_ignore_events (true);
238 _silent_samples.push_back (cr);
240 /* coordinates for the rect are relative to the regionview origin */
242 cr->set_x0 (trackview.editor().sample_to_pixel (i->first - _region->start()));
243 cr->set_x1 (trackview.editor().sample_to_pixel (i->second - _region->start()));
245 cr->set_y1 (_height - 2);
246 cr->set_outline (false);
247 cr->set_fill_color (color);
249 shortest = min (shortest, i->second - i->first);
252 /* Find shortest audible segment */
253 samplecnt_t shortest_audible = max_samplecnt;
255 samplecnt_t s = _region->start();
256 for (AudioIntervalResult::const_iterator i = silences.begin(); i != silences.end(); ++i) {
257 samplecnt_t const dur = i->first - s;
259 shortest_audible = min (shortest_audible, dur);
265 samplecnt_t const dur = _region->start() + _region->length() - 1 - s;
267 shortest_audible = min (shortest_audible, dur);
270 _silence_text = new ArdourCanvas::Text (group);
271 _silence_text->set_ignore_events (true);
272 _silence_text->set_font_description (get_font_for_style (N_("SilenceText")));
273 _silence_text->set_color (UIConfiguration::instance().color ("silence text"));
275 /* both positions are relative to the region start offset in source */
277 _silence_text->set_x_position (trackview.editor().sample_to_pixel (silences.front().first - _region->start()) + 10.0);
278 _silence_text->set_y_position (20.0);
280 double ms = (float) shortest/_region->session().sample_rate();
282 /* ms are now in seconds */
287 sunits = _("minutes");
289 } else if (ms < 1.0) {
296 string text = string_compose (ngettext ("%1 silent segment", "%1 silent segments", silences.size()), silences.size())
298 + string_compose (_("shortest = %1 %2"), ms, sunits);
300 if (shortest_audible != max_samplepos) {
301 /* ms are now in seconds */
302 double ma = (float) shortest_audible / _region->session().sample_rate();
306 aunits = _("minutes");
308 } else if (ma < 1.0) {
315 text += string_compose (_("\n (shortest audible segment = %1 %2)"), ma, aunits);
318 _silence_text->set (text);
322 RegionView::hide_silent_samples ()
324 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_samples.begin (); i != _silent_samples.end (); ++i) {
327 _silence_text->hide();
331 RegionView::drop_silent_samples ()
333 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_samples.begin (); i != _silent_samples.end (); ++i) {
336 _silent_samples.clear ();
338 delete _silence_text;
343 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
346 case GDK_BUTTON_RELEASE:
347 static_cast<RegionView*>(arg)->lock_toggle ();
357 RegionView::lock_toggle ()
359 _region->set_locked (!_region->locked());
363 RegionView::region_changed (const PropertyChange& what_changed)
365 ENSURE_GUI_THREAD (*this, &RegionView::region_changed, what_changed);
367 if (what_changed.contains (ARDOUR::bounds_change)) {
368 region_resized (what_changed);
369 region_sync_changed ();
371 if (what_changed.contains (ARDOUR::Properties::muted)) {
374 if (what_changed.contains (ARDOUR::Properties::opaque)) {
377 if (what_changed.contains (ARDOUR::Properties::name)) {
380 if (what_changed.contains (ARDOUR::Properties::position_lock_style)) {
383 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
384 region_sync_changed ();
386 if (what_changed.contains (ARDOUR::Properties::locked)) {
392 RegionView::region_locked ()
394 /* name will show locked status */
399 RegionView::region_resized (const PropertyChange& what_changed)
403 if (what_changed.contains (ARDOUR::Properties::position)) {
404 set_position (_region->position(), 0);
407 PropertyChange s_and_l;
408 s_and_l.add (ARDOUR::Properties::start);
409 s_and_l.add (ARDOUR::Properties::length);
411 if (what_changed.contains (s_and_l)) {
413 set_duration (_region->length(), 0);
415 unit_length = _region->length() / samples_per_pixel;
417 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
419 (*i)->set_duration (unit_length);
426 RegionView::reset_width_dependent_items (double pixel_width)
428 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
429 _pixel_width = pixel_width;
433 RegionView::region_muted ()
440 RegionView::region_opacity ()
446 RegionView::raise_to_top ()
448 _region->raise_to_top ();
452 RegionView::lower_to_bottom ()
454 _region->lower_to_bottom ();
458 RegionView::set_position (samplepos_t pos, void* /*src*/, double* ignored)
463 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
472 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
473 (*i)->group->move (ArdourCanvas::Duple (delta, 0.0));
481 RegionView::set_samples_per_pixel (double fpp)
483 TimeAxisViewItem::set_samples_per_pixel (fpp);
485 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
486 (*i)->set_samples_per_pixel (fpp);
487 (*i)->set_duration (_region->length() / fpp);
490 region_sync_changed ();
494 RegionView::set_duration (samplecnt_t samples, void *src)
496 if (!TimeAxisViewItem::set_duration (samples, src)) {
500 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
501 (*i)->set_duration (_region->length() / samples_per_pixel);
508 RegionView::set_colors ()
510 TimeAxisViewItem::set_colors ();
511 set_sync_mark_color ();
515 RegionView::set_sync_mark_color ()
518 Gtkmm2ext::Color c = UIConfiguration::instance().color ("sync mark");
519 sync_mark->set_fill_color (c);
520 sync_mark->set_outline_color (c);
521 sync_line->set_outline_color (c);
526 RegionView::get_fill_color () const
528 Gtkmm2ext::Color f = TimeAxisViewItem::get_fill_color();
531 if (_region->opaque() && (!ARDOUR::Profile->get_mixbus() || (!_dragging && !_region->muted ()))) {
532 modname = "opaque region base";
534 modname = "transparent region base";
537 return Gtkmm2ext::HSV(f).mod (UIConfiguration::instance().modifier (modname)).color ();
541 RegionView::show_region_editor ()
544 editor = new RegionEditor (trackview.session(), region());
552 RegionView::hide_region_editor()
560 RegionView::make_name () const
564 // XXX nice to have some good icons for this
565 if (_region->position_lock_style() == MusicTime) {
566 str += "\u266B"; // BEAMED EIGHTH NOTES
569 if (_region->locked()) {
570 str += "\u2629"; // CROSS OF JERUSALEM
571 str += _region->name();
572 } else if (_region->position_locked()) {
573 str += "\u21B9"; // LEFTWARDS ARROW TO BAR OVER RIGHTWARDS ARROW TO BAR
574 str += _region->name();
575 } else if (_region->video_locked()) {
577 str += _region->name();
580 str += _region->name();
583 if (_region->muted()) {
584 str = string ("!") + str;
591 RegionView::region_renamed ()
593 std::string str = make_name ();
595 set_item_name (str, this);
600 RegionView::region_sync_changed ()
603 samplecnt_t sync_offset;
605 sync_offset = _region->sync_offset (sync_dir);
607 if (sync_offset == 0) {
608 /* no need for a sync mark */
618 /* points set below */
620 sync_mark = new ArdourCanvas::Polygon (group);
621 CANVAS_DEBUG_NAME (sync_mark, string_compose ("sync mark for %1", get_item_name()));
622 sync_line = new ArdourCanvas::Line (group);
623 CANVAS_DEBUG_NAME (sync_line, string_compose ("sync mark for %1", get_item_name()));
625 set_sync_mark_color ();
628 /* this has to handle both a genuine change of position, a change of samples_per_pixel
629 and a change in the bounds of the _region->
632 if (sync_offset == 0) {
634 /* no sync mark - its the start of the region */
641 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
643 /* no sync mark - its out of the bounds of the region */
654 //points = sync_mark->property_points().get_value();
656 double offset = sync_offset / samples_per_pixel;
657 points.push_back (ArdourCanvas::Duple (offset - ((sync_mark_width-1)/2), 1));
658 points.push_back (ArdourCanvas::Duple (offset + ((sync_mark_width-1)/2), 1));
659 points.push_back (ArdourCanvas::Duple (offset, sync_mark_width - 1));
660 points.push_back (ArdourCanvas::Duple (offset - ((sync_mark_width-1)/2), 1));
661 sync_mark->set (points);
664 sync_line->set (ArdourCanvas::Duple (offset, 0), ArdourCanvas::Duple (offset, trackview.current_height() - NAME_HIGHLIGHT_SIZE));
671 RegionView::move (double x_delta, double y_delta)
673 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
677 /* items will not prevent Item::move() moving
678 * them to a negative x-axis coordinate, which
679 * is legal, but we don't want that here.
682 ArdourCanvas::Item *item = get_canvas_group ();
684 if (item->position().x + x_delta < 0) {
685 x_delta = -item->position().x; /* move it to zero */
688 item->move (ArdourCanvas::Duple (x_delta, y_delta));
690 /* note: ghosts never leave their tracks so y_delta for them is always zero */
692 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
693 (*i)->group->move (ArdourCanvas::Duple (x_delta, 0.0));
698 RegionView::remove_ghost_in (TimeAxisView& tv)
700 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
701 if (&(*i)->trackview == &tv) {
709 RegionView::remove_ghost (GhostRegion* ghost)
715 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
724 RegionView::set_height (double h)
726 TimeAxisViewItem::set_height(h);
731 samplecnt_t sync_offset;
732 sync_offset = _region->sync_offset (sync_dir);
733 double offset = sync_offset / samples_per_pixel;
736 ArdourCanvas::Duple (offset, 0),
737 ArdourCanvas::Duple (offset, h - NAME_HIGHLIGHT_SIZE)
741 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_samples.begin(); i != _coverage_samples.end(); ++i) {
742 (*i)->set_y1 (h + 1);
745 for (list<ArdourCanvas::Rectangle*>::iterator i = _silent_samples.begin(); i != _silent_samples.end(); ++i) {
746 (*i)->set_y1 (h + 1);
751 /** Remove old coverage samples and make new ones, if we're in a LayerDisplay mode
752 * which uses them. */
754 RegionView::update_coverage_samples (LayerDisplay d)
756 /* remove old coverage samples */
757 for (list<ArdourCanvas::Rectangle*>::iterator i = _coverage_samples.begin (); i != _coverage_samples.end (); ++i) {
761 _coverage_samples.clear ();
764 /* don't do coverage samples unless we're in stacked mode */
768 boost::shared_ptr<Playlist> pl (_region->playlist ());
773 samplepos_t const position = _region->first_sample ();
774 samplepos_t t = position;
775 samplepos_t const end = _region->last_sample ();
777 ArdourCanvas::Rectangle* cr = 0;
780 /* the color that will be used to show parts of regions that will not be heard */
781 uint32_t const non_playing_color = UIConfiguration::instance().color_mod ("covered region", "covered region base");
788 /* is this region is on top at time t? */
789 bool const new_me = (pl->top_unmuted_region_at (t) == _region);
790 /* finish off any old rect, if required */
791 if (cr && me != new_me) {
792 cr->set_x1 (trackview.editor().sample_to_pixel (t - position));
795 /* start off any new rect, if required */
796 if (cr == 0 || me != new_me) {
797 cr = new ArdourCanvas::Rectangle (group);
798 _coverage_samples.push_back (cr);
799 cr->set_x0 (trackview.editor().sample_to_pixel (t - position));
801 cr->set_y1 (_height + 1);
802 cr->set_outline (false);
803 cr->set_ignore_events (true);
805 cr->set_fill_color (UINT_RGBA_CHANGE_A (non_playing_color, 0));
807 cr->set_fill_color (non_playing_color);
810 t = pl->find_next_region_boundary (t, 1);
817 t = pl->find_next_region_boundary (t, 1);
820 /* finish off the last rectangle */
821 cr->set_x1 (trackview.editor().sample_to_pixel (end - position));
824 if (sample_handle_start) {
825 sample_handle_start->raise_to_top ();
828 if (sample_handle_end) {
829 sample_handle_end->raise_to_top ();
832 if (name_highlight) {
833 name_highlight->raise_to_top ();
837 name_text->raise_to_top ();
842 RegionView::trim_front (samplepos_t new_bound, bool no_overlap, const int32_t sub_num)
844 if (_region->locked()) {
848 samplepos_t const pre_trim_first_sample = _region->first_sample();
850 if (_region->position() == new_bound) {
854 _region->trim_front (new_bound, sub_num);
857 // Get the next region on the left of this region and shrink/expand it.
858 boost::shared_ptr<Playlist> playlist (_region->playlist());
859 boost::shared_ptr<Region> region_left = playlist->find_next_region (pre_trim_first_sample, End, 0);
861 bool regions_touching = false;
863 if (region_left != 0 && (pre_trim_first_sample == region_left->last_sample() + 1)) {
864 regions_touching = true;
867 // Only trim region on the left if the first sample has gone beyond the left region's last sample.
868 if (region_left != 0 && (region_left->last_sample() > _region->first_sample() || regions_touching)) {
869 region_left->trim_end (_region->first_sample() - 1);
873 region_changed (ARDOUR::bounds_change);
875 return (pre_trim_first_sample != _region->first_sample()); //return true if we actually changed something
879 RegionView::trim_end (samplepos_t new_bound, bool no_overlap, const int32_t sub_num)
881 if (_region->locked()) {
885 samplepos_t const pre_trim_last_sample = _region->last_sample();
887 _region->trim_end (new_bound, sub_num);
890 // Get the next region on the right of this region and shrink/expand it.
891 boost::shared_ptr<Playlist> playlist (_region->playlist());
892 boost::shared_ptr<Region> region_right = playlist->find_next_region (pre_trim_last_sample, Start, 1);
894 bool regions_touching = false;
896 if (region_right != 0 && (pre_trim_last_sample == region_right->first_sample() - 1)) {
897 regions_touching = true;
900 // Only trim region on the right if the last sample has gone beyond the right region's first sample.
901 if (region_right != 0 && (region_right->first_sample() < _region->last_sample() || regions_touching)) {
902 region_right->trim_front (_region->last_sample() + 1, sub_num);
905 region_changed (ARDOUR::bounds_change);
908 region_changed (PropertyChange (ARDOUR::Properties::length));
911 return (pre_trim_last_sample != _region->last_sample()); //return true if we actually changed something
916 RegionView::thaw_after_trim ()
918 if (_region->locked()) {
922 _region->resume_property_changes ();
927 RegionView::move_contents (sampleoffset_t distance)
929 if (_region->locked()) {
932 _region->move_start (distance);
933 region_changed (PropertyChange (ARDOUR::Properties::start));
936 /** Snap a sample offset within our region using the current snap settings.
937 * @param x Frame offset from this region's position.
938 * @param ensure_snap whether to ignore snap_mode (in the case of SnapOff) and magnetic snap.
939 * Used when inverting snap mode logic with key modifiers, or snap distance calculation.
940 * @return Snapped sample offset from this region's position.
943 RegionView::snap_sample_to_sample (sampleoffset_t x, bool ensure_snap) const
945 PublicEditor& editor = trackview.editor();
946 /* x is region relative, convert it to global absolute samples */
947 samplepos_t const session_sample = x + _region->position();
949 /* try a snap in either direction */
950 MusicSample sample (session_sample, 0);
951 editor.snap_to (sample, RoundNearest, SnapToAny_Visual, ensure_snap);
953 /* if we went off the beginning of the region, snap forwards */
954 if (sample.sample < _region->position ()) {
955 sample.sample = session_sample;
956 editor.snap_to (sample, RoundUpAlways, SnapToAny_Visual, ensure_snap);
959 /* back to region relative, keeping the relevant divisor */
960 return MusicSample (sample.sample - _region->position(), sample.division);