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
;
227 /* remove old silent frames */
228 drop_silent_frames ();
230 if (silences
.empty()) {
234 uint32_t const color
= ARDOUR_UI::config()->canvasvar_Silence
.get();
236 for (AudioIntervalResult::const_iterator i
= silences
.begin(); i
!= silences
.end(); ++i
) {
238 ArdourCanvas::SimpleRect
* cr
= new ArdourCanvas::SimpleRect (*group
);
239 _silent_frames
.push_back (cr
);
241 /* coordinates for the rect are relative to the regionview origin */
243 cr
->property_x1() = trackview
.editor().frame_to_pixel (i
->first
- _region
->start());
244 cr
->property_x2() = trackview
.editor().frame_to_pixel (i
->second
- _region
->start());
245 cr
->property_y1() = 1;
246 cr
->property_y2() = _height
- 2;
247 cr
->property_outline_pixels() = 0;
248 cr
->property_fill_color_rgba () = color
;
250 shortest
= min (shortest
, i
->second
- i
->first
);
253 /* Find shortest audible segment */
254 framecnt_t shortest_audible
= max_framecnt
;
256 framecnt_t s
= _region
->start();
257 for (AudioIntervalResult::const_iterator i
= silences
.begin(); i
!= silences
.end(); ++i
) {
258 framecnt_t
const dur
= i
->first
- s
;
260 shortest_audible
= min (shortest_audible
, dur
);
266 framecnt_t
const dur
= _region
->start() + _region
->length() - 1 - s
;
268 shortest_audible
= min (shortest_audible
, dur
);
271 _silence_text
= new ArdourCanvas::NoEventText (*group
);
272 _silence_text
->property_font_desc() = get_font_for_style (N_("SilenceText"));
273 _silence_text
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_SilenceText
.get();
274 _silence_text
->property_anchor() = ANCHOR_NW
;
276 /* both positions are relative to the region start offset in source */
278 _silence_text
->property_x() = trackview
.editor().frame_to_pixel (silences
.front().first
- _region
->start()) + 10.0;
279 _silence_text
->property_y() = 20.0;
281 double ms
= (float) shortest
/_region
->session().frame_rate();
283 /* ms are now in seconds */
288 sunits
= _("minutes");
290 } else if (ms
< 1.0) {
297 string text
= string_compose (ngettext ("%1 silent segment", "%1 silent segments", silences
.size()), silences
.size())
299 + string_compose (_("shortest = %1 %2"), ms
, sunits
);
301 if (shortest_audible
!= max_framepos
) {
302 /* ms are now in seconds */
303 double ma
= (float) shortest_audible
/ _region
->session().frame_rate();
307 aunits
= _("minutes");
309 } else if (ma
< 1.0) {
316 text
+= string_compose (_("\n (shortest audible segment = %1 %2)"), ma
, aunits
);
319 _silence_text
->property_text() = text
.c_str ();
323 RegionView::hide_silent_frames ()
325 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _silent_frames
.begin (); i
!= _silent_frames
.end (); ++i
) {
328 _silence_text
->hide();
332 RegionView::drop_silent_frames ()
334 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _silent_frames
.begin (); i
!= _silent_frames
.end (); ++i
) {
337 _silent_frames
.clear ();
339 delete _silence_text
;
344 RegionView::_lock_toggle (ArdourCanvas::Item
*, GdkEvent
* ev
, void* arg
)
347 case GDK_BUTTON_RELEASE
:
348 static_cast<RegionView
*>(arg
)->lock_toggle ();
358 RegionView::lock_toggle ()
360 _region
->set_locked (!_region
->locked());
364 RegionView::region_changed (const PropertyChange
& what_changed
)
366 ENSURE_GUI_THREAD (*this, &RegionView::region_changed
, what_changed
);
368 if (what_changed
.contains (ARDOUR::bounds_change
)) {
369 region_resized (what_changed
);
370 region_sync_changed ();
372 if (what_changed
.contains (ARDOUR::Properties::muted
)) {
375 if (what_changed
.contains (ARDOUR::Properties::opaque
)) {
378 if (what_changed
.contains (ARDOUR::Properties::name
)) {
381 if (what_changed
.contains (ARDOUR::Properties::sync_position
)) {
382 region_sync_changed ();
384 if (what_changed
.contains (ARDOUR::Properties::locked
)) {
390 RegionView::region_locked ()
392 /* name will show locked status */
397 RegionView::region_resized (const PropertyChange
& what_changed
)
401 if (what_changed
.contains (ARDOUR::Properties::position
)) {
402 set_position (_region
->position(), 0);
403 _time_converter
.set_origin_b (_region
->position());
406 PropertyChange s_and_l
;
407 s_and_l
.add (ARDOUR::Properties::start
);
408 s_and_l
.add (ARDOUR::Properties::length
);
410 if (what_changed
.contains (s_and_l
)) {
412 set_duration (_region
->length(), 0);
414 unit_length
= _region
->length() / samples_per_unit
;
416 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
418 (*i
)->set_duration (unit_length
);
425 RegionView::reset_width_dependent_items (double pixel_width
)
427 TimeAxisViewItem::reset_width_dependent_items (pixel_width
);
428 _pixel_width
= pixel_width
;
432 RegionView::region_muted ()
439 RegionView::region_opacity ()
445 RegionView::raise_to_top ()
447 _region
->raise_to_top ();
451 RegionView::lower_to_bottom ()
453 _region
->lower_to_bottom ();
457 RegionView::set_position (framepos_t pos
, void* /*src*/, double* ignored
)
462 if (!(ret
= TimeAxisViewItem::set_position (pos
, this, &delta
))) {
471 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
472 (*i
)->group
->move (delta
, 0.0);
480 RegionView::set_samples_per_unit (gdouble spu
)
482 TimeAxisViewItem::set_samples_per_unit (spu
);
484 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
485 (*i
)->set_samples_per_unit (spu
);
486 (*i
)->set_duration (_region
->length() / samples_per_unit
);
489 region_sync_changed ();
493 RegionView::set_duration (framecnt_t frames
, void *src
)
495 if (!TimeAxisViewItem::set_duration (frames
, src
)) {
499 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
500 (*i
)->set_duration (_region
->length() / samples_per_unit
);
507 RegionView::set_colors ()
509 TimeAxisViewItem::set_colors ();
512 sync_mark
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
513 sync_line
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
518 RegionView::set_frame_color ()
520 if (_region
->opaque()) {
526 TimeAxisViewItem::set_frame_color ();
530 RegionView::fake_set_opaque (bool yn
)
542 RegionView::show_region_editor ()
545 editor
= new RegionEditor (trackview
.session(), region());
553 RegionView::hide_region_editor()
561 RegionView::make_name () const
565 // XXX nice to have some good icons for this
567 if (_region
->locked()) {
569 str
+= _region
->name();
571 } else if (_region
->position_locked()) {
573 str
+= _region
->name();
576 str
= _region
->name();
579 if (_region
->muted()) {
580 str
= string ("!") + str
;
587 RegionView::region_renamed ()
589 std::string str
= make_name ();
591 set_item_name (str
, this);
593 reset_width_dependent_items (_pixel_width
);
597 RegionView::region_sync_changed ()
600 framecnt_t sync_offset
;
602 sync_offset
= _region
->sync_offset (sync_dir
);
604 if (sync_offset
== 0) {
605 /* no need for a sync mark */
615 /* points set below */
617 sync_mark
= new ArdourCanvas::Polygon (*group
);
618 sync_mark
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
620 sync_line
= new ArdourCanvas::Line (*group
);
621 sync_line
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
622 sync_line
->property_width_pixels() = 1;
625 /* this has to handle both a genuine change of position, a change of samples_per_unit,
626 and a change in the bounds of the _region->
629 if (sync_offset
== 0) {
631 /* no sync mark - its the start of the region */
638 if ((sync_dir
< 0) || ((sync_dir
> 0) && (sync_offset
> _region
->length()))) {
640 /* no sync mark - its out of the bounds of the region */
651 //points = sync_mark->property_points().get_value();
653 double offset
= sync_offset
/ samples_per_unit
;
654 points
.push_back (Gnome::Art::Point (offset
- ((sync_mark_width
-1)/2), 1));
655 points
.push_back (Gnome::Art::Point (offset
+ ((sync_mark_width
-1)/2), 1));
656 points
.push_back (Gnome::Art::Point (offset
, sync_mark_width
- 1));
657 points
.push_back (Gnome::Art::Point (offset
- ((sync_mark_width
-1)/2), 1));
658 sync_mark
->property_points().set_value (points
);
662 points
.push_back (Gnome::Art::Point (offset
, 0));
663 points
.push_back (Gnome::Art::Point (offset
, trackview
.current_height() - NAME_HIGHLIGHT_SIZE
));
665 sync_line
->property_points().set_value (points
);
672 RegionView::move (double x_delta
, double y_delta
)
674 if (!_region
->can_move() || (x_delta
== 0 && y_delta
== 0)) {
678 get_canvas_group()->move (x_delta
, y_delta
);
680 /* note: ghosts never leave their tracks so y_delta for them is always zero */
682 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
683 (*i
)->group
->move (x_delta
, 0.0);
688 RegionView::remove_ghost_in (TimeAxisView
& tv
)
690 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
691 if (&(*i
)->trackview
== &tv
) {
699 RegionView::remove_ghost (GhostRegion
* ghost
)
705 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
714 RegionView::get_fill_color ()
720 RegionView::set_height (double h
)
722 TimeAxisViewItem::set_height(h
);
727 framecnt_t sync_offset
;
728 sync_offset
= _region
->sync_offset (sync_dir
);
729 double offset
= sync_offset
/ samples_per_unit
;
731 points
.push_back (Gnome::Art::Point (offset
, 0));
732 points
.push_back (Gnome::Art::Point (offset
, h
- NAME_HIGHLIGHT_SIZE
));
733 sync_line
->property_points().set_value (points
);
736 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _coverage_frames
.begin(); i
!= _coverage_frames
.end(); ++i
) {
737 (*i
)->property_y2() = h
+ 1;
740 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _silent_frames
.begin(); i
!= _silent_frames
.end(); ++i
) {
741 (*i
)->property_y2() = h
+ 1;
746 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
747 * which uses them. */
749 RegionView::update_coverage_frames (LayerDisplay d
)
751 /* remove old coverage frames */
752 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _coverage_frames
.begin (); i
!= _coverage_frames
.end (); ++i
) {
756 _coverage_frames
.clear ();
759 /* don't do coverage frames unless we're in stacked mode */
763 boost::shared_ptr
<Playlist
> pl (_region
->playlist ());
768 framepos_t
const position
= _region
->first_frame ();
769 framepos_t t
= position
;
770 framepos_t
const end
= _region
->last_frame ();
772 ArdourCanvas::SimpleRect
* cr
= 0;
775 uint32_t const color
= frame
->property_fill_color_rgba ();
776 uint32_t const base_alpha
= UINT_RGBA_A (color
);
782 /* is this region is on top at time t? */
783 bool const new_me
= (pl
->top_unmuted_region_at (t
) == _region
);
785 /* finish off any old rect, if required */
786 if (cr
&& me
!= new_me
) {
787 cr
->property_x2() = trackview
.editor().frame_to_pixel (t
- position
);
790 /* start off any new rect, if required */
791 if (cr
== 0 || me
!= new_me
) {
792 cr
= new ArdourCanvas::SimpleRect (*group
);
793 _coverage_frames
.push_back (cr
);
794 cr
->property_x1() = trackview
.editor().frame_to_pixel (t
- position
);
795 cr
->property_y1() = 1;
796 cr
->property_y2() = _height
+ 1;
797 cr
->property_outline_pixels() = 0;
798 /* areas that will be played get a lower alpha */
799 uint32_t alpha
= base_alpha
;
803 cr
->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color
, alpha
);
806 t
= pl
->find_next_region_boundary (t
, 1);
811 /* finish off the last rectangle */
812 cr
->property_x2() = trackview
.editor().frame_to_pixel (end
- position
);
815 if (frame_handle_start
) {
816 frame_handle_start
->raise_to_top ();
819 if (frame_handle_end
) {
820 frame_handle_end
->raise_to_top ();
823 if (name_highlight
) {
824 name_highlight
->raise_to_top ();
828 name_pixbuf
->raise_to_top ();
833 RegionView::trim_front (framepos_t new_bound
, bool no_overlap
)
835 if (_region
->locked()) {
839 RouteTimeAxisView
& rtv
= dynamic_cast<RouteTimeAxisView
&> (trackview
);
840 double const speed
= rtv
.track()->speed ();
842 framepos_t
const pre_trim_first_frame
= _region
->first_frame();
844 _region
->trim_front ((framepos_t
) (new_bound
* speed
));
847 // Get the next region on the left of this region and shrink/expand it.
848 boost::shared_ptr
<Playlist
> playlist (_region
->playlist());
849 boost::shared_ptr
<Region
> region_left
= playlist
->find_next_region (pre_trim_first_frame
, End
, 0);
851 bool regions_touching
= false;
853 if (region_left
!= 0 && (pre_trim_first_frame
== region_left
->last_frame() + 1)) {
854 regions_touching
= true;
857 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
858 if (region_left
!= 0 && (region_left
->last_frame() > _region
->first_frame() || regions_touching
)) {
859 region_left
->trim_end (_region
->first_frame() - 1);
863 region_changed (ARDOUR::bounds_change
);
867 RegionView::trim_end (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_last_frame
= _region
->last_frame();
878 _region
->trim_end ((framepos_t
) (new_bound
* speed
));
881 // Get the next region on the right of this region and shrink/expand it.
882 boost::shared_ptr
<Playlist
> playlist (_region
->playlist());
883 boost::shared_ptr
<Region
> region_right
= playlist
->find_next_region (pre_trim_last_frame
, Start
, 1);
885 bool regions_touching
= false;
887 if (region_right
!= 0 && (pre_trim_last_frame
== region_right
->first_frame() - 1)) {
888 regions_touching
= true;
891 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
892 if (region_right
!= 0 && (region_right
->first_frame() < _region
->last_frame() || regions_touching
)) {
893 region_right
->trim_front (_region
->last_frame() + 1);
896 region_changed (ARDOUR::bounds_change
);
899 region_changed (PropertyChange (ARDOUR::Properties::length
));
905 RegionView::thaw_after_trim ()
907 if (_region
->locked()) {
911 _region
->resume_property_changes ();
916 RegionView::trim_contents (framepos_t frame_delta
, bool left_direction
, bool swap_direction
)
918 if (_region
->locked()) {
922 framepos_t new_bound
;
924 RouteTimeAxisView
& rtv
= dynamic_cast<RouteTimeAxisView
&> (trackview
);
925 double const speed
= rtv
.track()->speed ();
927 if (left_direction
) {
928 if (swap_direction
) {
929 new_bound
= (framepos_t
) (_region
->position() / speed
) + frame_delta
;
931 new_bound
= (framepos_t
) (_region
->position() / speed
) - frame_delta
;
934 if (swap_direction
) {
935 new_bound
= (framepos_t
) (_region
->position() / speed
) - frame_delta
;
937 new_bound
= (framepos_t
) (_region
->position() / speed
) + frame_delta
;
941 _region
->trim_start ((framepos_t
) (new_bound
* speed
));
942 region_changed (PropertyChange (ARDOUR::Properties::start
));