handle multiple imports of the same file better (via better source naming); make...
[ardour2.git] / gtk2_ardour / region_view.cc
blob755281a119e11fe31fcc7bb9ca14b3557280a8ac
1 /*
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.
20 #include <cmath>
21 #include <cassert>
22 #include <algorithm>
24 #include <gtkmm.h>
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"
42 #include "waveview.h"
43 #include "public_editor.h"
44 #include "region_editor.h"
45 #include "ghostregion.h"
46 #include "route_time_axis.h"
47 #include "utils.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
51 #include "i18n.h"
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
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,
64 TimeAxisView& tv,
65 boost::shared_ptr<ARDOUR::Region> r,
66 double spu,
67 Gdk::Color const & basic_color,
68 bool automation)
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))
72 , _region (r)
73 , sync_mark(0)
74 , sync_line(0)
75 , editor(0)
76 , current_visible_sync_position(0.0)
77 , valid(false)
78 , _enable_display(false)
79 , _pixel_width(1.0)
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;
96 valid = false;
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;
115 valid = false;
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,
122 TimeAxisView& tv,
123 boost::shared_ptr<ARDOUR::Region> r,
124 double spu,
125 Gdk::Color const & basic_color,
126 bool recording,
127 TimeAxisViewItem::Visibility visibility)
128 : TimeAxisViewItem (r->name(), *parent, tv, spu, basic_color, r->position(), r->length(), recording, false, visibility)
129 , _region (r)
130 , sync_mark(0)
131 , sync_line(0)
132 , editor(0)
133 , current_visible_sync_position(0.0)
134 , valid(false)
135 , _enable_display(false)
136 , _pixel_width(1.0)
137 , in_destructor(false)
138 , wait_for_data(false)
139 , _time_converter(r->session().tempo_map(), r->position())
143 void
144 RegionView::init (Gdk::Color const & basic_color, bool wfd)
146 editor = 0;
147 valid = true;
148 in_destructor = false;
149 wait_for_data = wfd;
150 sync_mark = 0;
151 sync_line = 0;
152 sync_mark = 0;
153 sync_line = 0;
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();
176 if (name_pixbuf) {
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));
181 if (wfd) {
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));
191 set_colors ();
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) {
203 delete *g;
206 for (list<ArdourCanvas::SimpleRect*>::iterator i = _coverage_frames.begin (); i != _coverage_frames.end (); ++i) {
207 delete *i;
210 delete editor;
213 gint
214 RegionView::_lock_toggle (ArdourCanvas::Item*, GdkEvent* ev, void* arg)
216 switch (ev->type) {
217 case GDK_BUTTON_RELEASE:
218 static_cast<RegionView*>(arg)->lock_toggle ();
219 return TRUE;
220 break;
221 default:
222 break;
224 return FALSE;
227 void
228 RegionView::lock_toggle ()
230 _region->set_locked (!_region->locked());
233 void
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)) {
243 region_muted ();
245 if (what_changed.contains (ARDOUR::Properties::opaque)) {
246 region_opacity ();
248 if (what_changed.contains (ARDOUR::Properties::name)) {
249 region_renamed ();
251 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
252 region_sync_changed ();
254 if (what_changed.contains (ARDOUR::Properties::locked)) {
255 region_locked ();
259 void
260 RegionView::region_locked ()
262 /* name will show locked status */
263 region_renamed ();
266 void
267 RegionView::region_resized (const PropertyChange& what_changed)
269 double unit_length;
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);
294 void
295 RegionView::reset_width_dependent_items (double pixel_width)
297 TimeAxisViewItem::reset_width_dependent_items (pixel_width);
298 _pixel_width = pixel_width;
301 void
302 RegionView::region_muted ()
304 set_frame_color ();
305 region_renamed ();
308 void
309 RegionView::region_opacity ()
311 set_frame_color ();
314 void
315 RegionView::raise_to_top ()
317 _region->raise_to_top ();
320 void
321 RegionView::lower_to_bottom ()
323 _region->lower_to_bottom ();
326 bool
327 RegionView::set_position (framepos_t pos, void* /*src*/, double* ignored)
329 double delta;
330 bool ret;
332 if (!(ret = TimeAxisViewItem::set_position (pos, this, &delta))) {
333 return false;
336 if (ignored) {
337 *ignored = delta;
340 if (delta) {
341 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
342 (*i)->group->move (delta, 0.0);
346 return ret;
349 void
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 ();
362 bool
363 RegionView::set_duration (framecnt_t frames, void *src)
365 if (!TimeAxisViewItem::set_duration (frames, src)) {
366 return false;
369 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
370 (*i)->set_duration (_region->length() / samples_per_unit);
373 return true;
376 void
377 RegionView::set_colors ()
379 TimeAxisViewItem::set_colors ();
381 if (sync_mark) {
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
387 void
388 RegionView::set_frame_color ()
390 if (_region->opaque()) {
391 fill_opacity = 130;
392 } else {
393 fill_opacity = 60;
396 TimeAxisViewItem::set_frame_color ();
399 void
400 RegionView::fake_set_opaque (bool yn)
402 if (yn) {
403 fill_opacity = 130;
404 } else {
405 fill_opacity = 60;
408 set_frame_color ();
411 void
412 RegionView::show_region_editor ()
414 if (editor == 0) {
415 editor = new RegionEditor (trackview.session(), region());
418 editor->present ();
419 editor->show_all();
422 void
423 RegionView::hide_region_editor()
425 if (editor) {
426 editor->hide_all ();
430 std::string
431 RegionView::make_name () const
433 std::string str;
435 // XXX nice to have some good icons for this
437 if (_region->locked()) {
438 str += '>';
439 str += _region->name();
440 str += '<';
441 } else if (_region->position_locked()) {
442 str += '{';
443 str += _region->name();
444 str += '}';
445 } else {
446 str = _region->name();
449 if (_region->muted()) {
450 str = string ("!") + str;
453 return str;
456 void
457 RegionView::region_renamed ()
459 std::string str = make_name ();
461 set_item_name (str, this);
462 set_name_text (str);
463 reset_width_dependent_items (_pixel_width);
466 void
467 RegionView::region_sync_changed ()
469 int sync_dir;
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 */
476 if (sync_mark) {
477 sync_mark->hide();
478 sync_line->hide ();
480 return;
483 if (!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 */
503 sync_mark->hide();
504 sync_line->hide ();
506 } else {
508 if ((sync_dir < 0) || ((sync_dir > 0) && (sync_offset > _region->length()))) {
510 /* no sync mark - its out of the bounds of the region */
512 sync_mark->hide();
513 sync_line->hide ();
515 } else {
517 /* lets do it */
519 Points points;
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);
529 sync_mark->show ();
531 points.clear ();
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);
536 sync_line->show ();
541 void
542 RegionView::move (double x_delta, double y_delta)
544 if (!_region->can_move() || (x_delta == 0 && y_delta == 0)) {
545 return;
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);
557 void
558 RegionView::remove_ghost_in (TimeAxisView& tv)
560 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
561 if (&(*i)->trackview == &tv) {
562 delete *i;
563 break;
568 void
569 RegionView::remove_ghost (GhostRegion* ghost)
571 if (in_destructor) {
572 return;
575 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
576 if (*i == ghost) {
577 ghosts.erase (i);
578 break;
583 uint32_t
584 RegionView::get_fill_color ()
586 return fill_color;
589 void
590 RegionView::set_height (double h)
592 TimeAxisViewItem::set_height(h);
594 if (sync_line) {
595 Points points;
596 int sync_dir;
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. */
613 void
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) {
618 delete *i;
621 _coverage_frames.clear ();
623 if (d != Stacked) {
624 /* don't do coverage frames unless we're in stacked mode */
625 return;
628 boost::shared_ptr<Playlist> pl (_region->playlist ());
629 if (!pl) {
630 return;
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;
638 bool me = false;
640 uint32_t const color = frame->property_fill_color_rgba ();
641 uint32_t const base_alpha = UINT_RGBA_A (color);
643 while (t < end) {
645 t++;
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;
665 if (new_me) {
666 alpha /= 2;
668 cr->property_fill_color_rgba () = UINT_RGBA_CHANGE_A (color, alpha);
671 t = pl->find_next_region_boundary (t, 1);
672 me = new_me;
675 if (cr) {
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 ();
686 void
687 RegionView::trim_start (framepos_t new_bound, bool no_overlap)
689 if (_region->locked()) {
690 return;
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);
700 if (no_overlap) {
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);
720 void
721 RegionView::trim_end (framepos_t new_bound, bool no_overlap)
723 if (_region->locked()) {
724 return;
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);
734 if (no_overlap) {
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);
752 } else {
753 region_changed (PropertyChange (ARDOUR::Properties::length));
758 void
759 RegionView::thaw_after_trim ()
761 if (_region->locked()) {
762 return;
765 _region->resume_property_changes ();
769 void
770 RegionView::trim_contents (framepos_t frame_delta, bool left_direction, bool swap_direction)
772 if (_region->locked()) {
773 return;
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;
784 } else {
785 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
787 } else {
788 if (swap_direction) {
789 new_bound = (framepos_t) (_region->position() / speed) - frame_delta;
790 } else {
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));