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 "streamview.h"
37 #include "region_view.h"
38 #include "automation_region_view.h"
39 #include "route_time_axis.h"
40 #include "simplerect.h"
41 #include "simpleline.h"
43 #include "public_editor.h"
44 #include "region_editor.h"
45 #include "ghostregion.h"
46 #include "route_time_axis.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
54 using namespace ARDOUR
;
56 using namespace Editing
;
57 using namespace ArdourCanvas
;
59 static const int32_t sync_mark_width
= 9;
61 PBD::Signal1
<void,RegionView
*> RegionView::RegionViewGoingAway
;
63 RegionView::RegionView (ArdourCanvas::Group
* parent
,
65 boost::shared_ptr
<ARDOUR::Region
> r
,
67 Gdk::Color
const & basic_color
,
69 : TimeAxisViewItem (r
->name(), *parent
, tv
, spu
, basic_color
, r
->position(), r
->length(), false, automation
,
70 TimeAxisViewItem::Visibility (TimeAxisViewItem::ShowNameText
|
71 TimeAxisViewItem::ShowNameHighlight
| TimeAxisViewItem::ShowFrame
))
76 , current_visible_sync_position(0.0)
78 , _enable_display(false)
80 , in_destructor(false)
81 , wait_for_data(false)
82 , _time_converter(r
->session().tempo_map(), r
->position())
84 GhostRegion::CatchDeletion
.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost
, this, _1
), gui_context());
87 RegionView::RegionView (const RegionView
& other
)
88 : sigc::trackable(other
)
89 , TimeAxisViewItem (other
)
90 , _time_converter(other
._time_converter
)
92 /* derived concrete type will call init () */
94 _region
= other
._region
;
95 current_visible_sync_position
= other
.current_visible_sync_position
;
97 _pixel_width
= other
._pixel_width
;
99 GhostRegion::CatchDeletion
.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost
, this, _1
), gui_context());
102 RegionView::RegionView (const RegionView
& other
, boost::shared_ptr
<Region
> other_region
)
103 : sigc::trackable(other
)
104 , TimeAxisViewItem (other
)
105 , _time_converter(other
._time_converter
)
107 /* this is a pseudo-copy constructor used when dragging regions
108 around on the canvas.
111 /* derived concrete type will call init () */
113 _region
= other_region
;
114 current_visible_sync_position
= other
.current_visible_sync_position
;
116 _pixel_width
= other
._pixel_width
;
118 GhostRegion::CatchDeletion
.connect (*this, invalidator (*this), ui_bind (&RegionView::remove_ghost
, this, _1
), gui_context());
121 RegionView::RegionView (ArdourCanvas::Group
* parent
,
123 boost::shared_ptr
<ARDOUR::Region
> r
,
125 Gdk::Color
const & basic_color
,
127 TimeAxisViewItem::Visibility visibility
)
128 : TimeAxisViewItem (r
->name(), *parent
, tv
, spu
, basic_color
, r
->position(), r
->length(), recording
, false, visibility
)
133 , current_visible_sync_position(0.0)
135 , _enable_display(false)
137 , in_destructor(false)
138 , wait_for_data(false)
139 , _time_converter(r
->session().tempo_map(), r
->position())
144 RegionView::init (Gdk::Color
const & basic_color
, bool wfd
)
148 in_destructor
= false;
155 compute_colors (basic_color
);
157 if (name_highlight
) {
158 name_highlight
->set_data ("regionview", this);
159 name_highlight
->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_highlight_event
), name_highlight
, this));
161 if (frame_handle_start
) {
162 frame_handle_start
->set_data ("regionview", this);
163 frame_handle_start
->set_data ("isleft", (void*) 1);
164 frame_handle_start
->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event
), frame_handle_start
, this));
165 frame_handle_start
->raise_to_top();
168 if (frame_handle_end
) {
169 frame_handle_end
->set_data ("regionview", this);
170 frame_handle_end
->set_data ("isleft", (void*) 0);
171 frame_handle_end
->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_frame_handle_event
), frame_handle_end
, this));
172 frame_handle_end
->raise_to_top();
177 name_pixbuf
->set_data ("regionview", this);
178 name_pixbuf
->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_name_event
), name_pixbuf
, this));
182 _enable_display
= true;
185 set_height (trackview
.current_height());
187 _region
->PropertyChanged
.connect (*this, invalidator (*this), ui_bind (&RegionView::region_changed
, this, _1
), gui_context());
189 group
->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_region_view_event
), group
, this));
193 ColorsChanged
.connect (sigc::mem_fun (*this, &RegionView::color_handler
));
195 /* XXX sync mark drag? */
198 RegionView::~RegionView ()
200 in_destructor
= true;
202 for (vector
<GhostRegion
*>::iterator g
= ghosts
.begin(); g
!= ghosts
.end(); ++g
) {
206 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _coverage_frames
.begin (); i
!= _coverage_frames
.end (); ++i
) {
214 RegionView::_lock_toggle (ArdourCanvas::Item
*, GdkEvent
* ev
, void* arg
)
217 case GDK_BUTTON_RELEASE
:
218 static_cast<RegionView
*>(arg
)->lock_toggle ();
228 RegionView::lock_toggle ()
230 _region
->set_locked (!_region
->locked());
234 RegionView::region_changed (const PropertyChange
& what_changed
)
236 ENSURE_GUI_THREAD (*this, &RegionView::region_changed
, what_changed
);
238 if (what_changed
.contains (ARDOUR::bounds_change
)) {
239 region_resized (what_changed
);
240 region_sync_changed ();
242 if (what_changed
.contains (ARDOUR::Properties::muted
)) {
245 if (what_changed
.contains (ARDOUR::Properties::opaque
)) {
248 if (what_changed
.contains (ARDOUR::Properties::name
)) {
251 if (what_changed
.contains (ARDOUR::Properties::sync_position
)) {
252 region_sync_changed ();
254 if (what_changed
.contains (ARDOUR::Properties::locked
)) {
260 RegionView::region_locked ()
262 /* name will show locked status */
267 RegionView::region_resized (const PropertyChange
& what_changed
)
271 if (what_changed
.contains (ARDOUR::Properties::position
)) {
272 set_position (_region
->position(), 0);
273 _time_converter
.set_origin_b (_region
->position());
276 PropertyChange s_and_l
;
277 s_and_l
.add (ARDOUR::Properties::start
);
278 s_and_l
.add (ARDOUR::Properties::length
);
280 if (what_changed
.contains (s_and_l
)) {
282 set_duration (_region
->length(), 0);
284 unit_length
= _region
->length() / samples_per_unit
;
286 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
288 (*i
)->set_duration (unit_length
);
295 RegionView::reset_width_dependent_items (double pixel_width
)
297 TimeAxisViewItem::reset_width_dependent_items (pixel_width
);
298 _pixel_width
= pixel_width
;
302 RegionView::region_muted ()
309 RegionView::region_opacity ()
315 RegionView::raise_to_top ()
317 _region
->raise_to_top ();
321 RegionView::lower_to_bottom ()
323 _region
->lower_to_bottom ();
327 RegionView::set_position (framepos_t pos
, void* /*src*/, double* ignored
)
332 if (!(ret
= TimeAxisViewItem::set_position (pos
, this, &delta
))) {
341 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
342 (*i
)->group
->move (delta
, 0.0);
350 RegionView::set_samples_per_unit (gdouble spu
)
352 TimeAxisViewItem::set_samples_per_unit (spu
);
354 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
355 (*i
)->set_samples_per_unit (spu
);
356 (*i
)->set_duration (_region
->length() / samples_per_unit
);
359 region_sync_changed ();
363 RegionView::set_duration (framecnt_t frames
, void *src
)
365 if (!TimeAxisViewItem::set_duration (frames
, src
)) {
369 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
370 (*i
)->set_duration (_region
->length() / samples_per_unit
);
377 RegionView::set_colors ()
379 TimeAxisViewItem::set_colors ();
382 sync_mark
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
383 sync_line
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
388 RegionView::set_frame_color ()
390 if (_region
->opaque()) {
396 TimeAxisViewItem::set_frame_color ();
400 RegionView::fake_set_opaque (bool yn
)
412 RegionView::show_region_editor ()
415 editor
= new RegionEditor (trackview
.session(), region());
423 RegionView::hide_region_editor()
431 RegionView::make_name () const
435 // XXX nice to have some good icons for this
437 if (_region
->locked()) {
439 str
+= _region
->name();
441 } else if (_region
->position_locked()) {
443 str
+= _region
->name();
446 str
= _region
->name();
449 if (_region
->muted()) {
450 str
= string ("!") + str
;
457 RegionView::region_renamed ()
459 std::string str
= make_name ();
461 set_item_name (str
, this);
463 reset_width_dependent_items (_pixel_width
);
467 RegionView::region_sync_changed ()
470 nframes_t sync_offset
;
472 sync_offset
= _region
->sync_offset (sync_dir
);
474 if (sync_offset
== 0) {
475 /* no need for a sync mark */
485 /* points set below */
487 sync_mark
= new ArdourCanvas::Polygon (*group
);
488 sync_mark
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
490 sync_line
= new ArdourCanvas::Line (*group
);
491 sync_line
->property_fill_color_rgba() = RGBA_TO_UINT(0,255,0,255); // fill_color; // FIXME make a themeable colour
492 sync_line
->property_width_pixels() = 1;
495 /* this has to handle both a genuine change of position, a change of samples_per_unit,
496 and a change in the bounds of the _region->
499 if (sync_offset
== 0) {
501 /* no sync mark - its the start of the region */
508 if ((sync_dir
< 0) || ((sync_dir
> 0) && (sync_offset
> _region
->length()))) {
510 /* no sync mark - its out of the bounds of the region */
521 //points = sync_mark->property_points().get_value();
523 double offset
= sync_offset
/ samples_per_unit
;
524 points
.push_back (Gnome::Art::Point (offset
- ((sync_mark_width
-1)/2), 1));
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));
527 points
.push_back (Gnome::Art::Point (offset
- ((sync_mark_width
-1)/2), 1));
528 sync_mark
->property_points().set_value (points
);
532 points
.push_back (Gnome::Art::Point (offset
, 0));
533 points
.push_back (Gnome::Art::Point (offset
, trackview
.current_height() - NAME_HIGHLIGHT_SIZE
));
535 sync_line
->property_points().set_value (points
);
542 RegionView::move (double x_delta
, double y_delta
)
544 if (!_region
->can_move() || (x_delta
== 0 && y_delta
== 0)) {
548 get_canvas_group()->move (x_delta
, y_delta
);
550 /* note: ghosts never leave their tracks so y_delta for them is always zero */
552 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
553 (*i
)->group
->move (x_delta
, 0.0);
558 RegionView::remove_ghost_in (TimeAxisView
& tv
)
560 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
561 if (&(*i
)->trackview
== &tv
) {
569 RegionView::remove_ghost (GhostRegion
* ghost
)
575 for (vector
<GhostRegion
*>::iterator i
= ghosts
.begin(); i
!= ghosts
.end(); ++i
) {
584 RegionView::get_fill_color ()
590 RegionView::set_height (double h
)
592 TimeAxisViewItem::set_height(h
);
597 nframes_t sync_offset
;
598 sync_offset
= _region
->sync_offset (sync_dir
);
599 double offset
= sync_offset
/ samples_per_unit
;
601 points
.push_back (Gnome::Art::Point (offset
, 0));
602 points
.push_back (Gnome::Art::Point (offset
, h
- NAME_HIGHLIGHT_SIZE
));
603 sync_line
->property_points().set_value (points
);
606 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _coverage_frames
.begin(); i
!= _coverage_frames
.end(); ++i
) {
607 (*i
)->property_y2() = h
+ 1;
611 /** Remove old coverage frames and make new ones, if we're in a LayerDisplay mode
612 * which uses them. */
614 RegionView::update_coverage_frames (LayerDisplay d
)
616 /* remove old coverage frames */
617 for (list
<ArdourCanvas::SimpleRect
*>::iterator i
= _coverage_frames
.begin (); i
!= _coverage_frames
.end (); ++i
) {
621 _coverage_frames
.clear ();
624 /* don't do coverage frames unless we're in stacked mode */
628 boost::shared_ptr
<Playlist
> pl (_region
->playlist ());
633 nframes_t
const position
= _region
->first_frame ();
634 nframes_t t
= position
;
635 nframes_t
const end
= _region
->last_frame ();
637 ArdourCanvas::SimpleRect
* cr
= 0;
640 uint32_t const color
= frame
->property_fill_color_rgba ();
641 uint32_t const base_alpha
= UINT_RGBA_A (color
);
647 /* is this region is on top at time t? */
648 bool const new_me
= (pl
->top_unmuted_region_at (t
) == _region
);
650 /* finish off any old rect, if required */
651 if (cr
&& me
!= new_me
) {
652 cr
->property_x2() = trackview
.editor().frame_to_pixel (t
- position
);
655 /* start off any new rect, if required */
656 if (cr
== 0 || me
!= new_me
) {
657 cr
= new ArdourCanvas::SimpleRect (*group
);
658 _coverage_frames
.push_back (cr
);
659 cr
->property_x1() = trackview
.editor().frame_to_pixel (t
- position
);
660 cr
->property_y1() = 1;
661 cr
->property_y2() = _height
+ 1;
662 cr
->property_outline_pixels() = 0;
663 /* areas that will be played get a lower alpha */
664 uint32_t alpha
= base_alpha
;
668 cr
->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color
, alpha
);
671 t
= pl
->find_next_region_boundary (t
, 1);
676 /* finish off the last rectangle */
677 cr
->property_x2() = trackview
.editor().frame_to_pixel (end
- position
);
680 if (frame_handle_start
) {
681 frame_handle_start
->raise_to_top ();
682 frame_handle_end
->raise_to_top ();
687 RegionView::trim_start (framepos_t new_bound
, bool no_overlap
)
689 if (_region
->locked()) {
693 RouteTimeAxisView
& rtv
= dynamic_cast<RouteTimeAxisView
&> (trackview
);
694 double const speed
= rtv
.track()->speed ();
696 framepos_t
const pre_trim_first_frame
= _region
->first_frame();
698 _region
->trim_front ((framepos_t
) (new_bound
* speed
), this);
701 // Get the next region on the left of this region and shrink/expand it.
702 boost::shared_ptr
<Playlist
> playlist (_region
->playlist());
703 boost::shared_ptr
<Region
> region_left
= playlist
->find_next_region (pre_trim_first_frame
, End
, 0);
705 bool regions_touching
= false;
707 if (region_left
!= 0 && (pre_trim_first_frame
== region_left
->last_frame() + 1)) {
708 regions_touching
= true;
711 // Only trim region on the left if the first frame has gone beyond the left region's last frame.
712 if (region_left
!= 0 && (region_left
->last_frame() > _region
->first_frame() || regions_touching
)) {
713 region_left
->trim_end (_region
->first_frame() - 1, this);
717 region_changed (ARDOUR::bounds_change
);
721 RegionView::trim_end (framepos_t new_bound
, bool no_overlap
)
723 if (_region
->locked()) {
727 RouteTimeAxisView
& rtv
= dynamic_cast<RouteTimeAxisView
&> (trackview
);
728 double const speed
= rtv
.track()->speed ();
730 framepos_t
const pre_trim_last_frame
= _region
->last_frame();
732 _region
->trim_end ((framepos_t
) (new_bound
* speed
), this);
735 // Get the next region on the right of this region and shrink/expand it.
736 boost::shared_ptr
<Playlist
> playlist (_region
->playlist());
737 boost::shared_ptr
<Region
> region_right
= playlist
->find_next_region (pre_trim_last_frame
, Start
, 1);
739 bool regions_touching
= false;
741 if (region_right
!= 0 && (pre_trim_last_frame
== region_right
->first_frame() - 1)) {
742 regions_touching
= true;
745 // Only trim region on the right if the last frame has gone beyond the right region's first frame.
746 if (region_right
!= 0 && (region_right
->first_frame() < _region
->last_frame() || regions_touching
)) {
747 region_right
->trim_front (_region
->last_frame() + 1, this);
750 region_changed (ARDOUR::bounds_change
);
753 region_changed (PropertyChange (ARDOUR::Properties::length
));
759 RegionView::thaw_after_trim ()
761 if (_region
->locked()) {
765 _region
->resume_property_changes ();
770 RegionView::trim_contents (framepos_t frame_delta
, bool left_direction
, bool swap_direction
)
772 if (_region
->locked()) {
776 framepos_t new_bound
;
778 RouteTimeAxisView
& rtv
= dynamic_cast<RouteTimeAxisView
&> (trackview
);
779 double const speed
= rtv
.track()->speed ();
781 if (left_direction
) {
782 if (swap_direction
) {
783 new_bound
= (framepos_t
) (_region
->position() / speed
) + frame_delta
;
785 new_bound
= (framepos_t
) (_region
->position() / speed
) - frame_delta
;
788 if (swap_direction
) {
789 new_bound
= (framepos_t
) (_region
->position() / speed
) - frame_delta
;
791 new_bound
= (framepos_t
) (_region
->position() / speed
) + frame_delta
;
795 _region
->trim_start ((framepos_t
) (new_bound
* speed
), this);
796 region_changed (PropertyChange (ARDOUR::Properties::start
));