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.
23 #include <gtkmm2ext/gtk_ui.h>
24 #include <gtkmm2ext/utils.h>
26 #include "ardour/playlist.h"
27 #include "ardour/region.h"
28 #include "ardour/track.h"
29 #include "ardour/session.h"
31 #include "pbd/compose.h"
33 #include "canvas/rectangle.h"
34 #include "canvas/debug.h"
36 #include "streamview.h"
37 #include "global_signals.h"
38 #include "region_view.h"
39 #include "route_time_axis.h"
40 #include "region_selection.h"
41 #include "selection.h"
42 #include "public_editor.h"
43 #include "ardour_ui.h"
45 #include "rgb_macros.h"
46 #include "gui_thread.h"
52 using namespace ARDOUR;
53 using namespace ARDOUR_UI_UTILS;
55 using namespace Editing;
57 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Container* canvas_group)
59 , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Container (_trackview.canvas_display()))
60 , _samples_per_pixel (_trackview.editor().get_current_zoom ())
63 , stream_base_color(0xFFFFFFFF)
65 , _layer_display (Overlaid)
67 , last_rec_data_frame(0)
69 CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
71 /* set_position() will position the group */
73 canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
74 CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
75 canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
76 canvas_rect->set_outline (false);
77 canvas_rect->set_fill (true);
78 canvas_rect->Event.connect (sigc::bind (sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
80 if (_trackview.is_track()) {
81 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
82 _trackview.track()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
84 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
85 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
86 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
89 ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
92 StreamView::~StreamView ()
100 StreamView::attach ()
102 if (_trackview.is_track()) {
103 display_track (_trackview.track ());
108 StreamView::set_position (gdouble x, gdouble y)
110 _canvas_group->set_position (ArdourCanvas::Duple (x, y));
115 StreamView::set_height (double h)
117 /* limit the values to something sane-ish */
119 if (h < 10.0 || h > 1000.0) {
123 if (canvas_rect->y1() == h) {
128 canvas_rect->set_y1 (height);
129 update_contents_height ();
135 StreamView::set_samples_per_pixel (double fpp)
137 RegionViewList::iterator i;
143 _samples_per_pixel = fpp;
145 for (i = region_views.begin(); i != region_views.end(); ++i) {
146 (*i)->set_samples_per_pixel (fpp);
149 for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
150 RecBoxInfo &recbox = (*xi);
152 ArdourCanvas::Coord const xstart = _trackview.editor().sample_to_pixel (recbox.start);
153 ArdourCanvas::Coord const xend = _trackview.editor().sample_to_pixel (recbox.start + recbox.length);
155 recbox.rectangle->set_x0 (xstart);
156 recbox.rectangle->set_x1 (xend);
159 update_coverage_frames ();
165 StreamView::add_region_view (boost::weak_ptr<Region> wr)
167 boost::shared_ptr<Region> r (wr.lock());
172 add_region_view_internal (r, true);
174 if (_layer_display == Stacked || _layer_display == Expanded) {
175 update_contents_height ();
180 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
182 ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
184 boost::shared_ptr<Region> r (weak_r.lock());
190 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
191 if (((*i)->region()) == r) {
193 region_views.erase (i);
199 RegionViewRemoved (); /* EMIT SIGNAL */
203 StreamView::undisplay_track ()
205 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
206 RegionViewList::iterator next = i;
212 region_views.clear();
216 StreamView::display_track (boost::shared_ptr<Track> tr)
218 playlist_switched_connection.disconnect();
219 playlist_switched (tr);
220 tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
224 StreamView::layer_regions()
226 // In one traversal of the region view list:
227 // - Build a list of region views sorted by layer
228 // - Remove invalid views from the actual region view list
230 list<RegionView*>::iterator i, tmp;
231 for (i = region_views.begin(); i != region_views.end(); ) {
235 if (!(*i)->is_valid()) {
237 region_views.erase (i);
241 (*i)->enable_display(true);
244 if (copy.size() == 0) {
245 copy.push_front((*i));
250 RegionViewList::iterator k = copy.begin();
251 RegionViewList::iterator l = copy.end();
254 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
255 copy.push_front((*i));
258 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
259 copy.push_back((*i));
264 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
265 if ((*j)->region()->layer() >= (*i)->region()->layer()) {
266 copy.insert(j, (*i));
274 // Fix canvas layering by raising each to the top in the sorted order.
275 for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
276 (*i)->get_canvas_group()->raise_to_top ();
281 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
283 boost::shared_ptr<Track> tr (wtr.lock());
289 /* update layers count and the y positions and heights of our regions */
290 if (tr->playlist()) {
291 _layers = tr->playlist()->top_layer() + 1;
294 if (_layer_display == Stacked) {
295 update_contents_height ();
296 update_coverage_frames ();
298 /* layering has probably been modified. reflect this in the canvas. */
304 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
306 boost::shared_ptr<Track> tr (wtr.lock());
312 /* disconnect from old playlist */
314 playlist_connections.drop_connections ();
321 /* update layers count and the y positions and heights of our regions */
322 _layers = tr->playlist()->top_layer() + 1;
323 update_contents_height ();
324 update_coverage_frames ();
328 tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
329 tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
330 tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
331 tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frames, this), gui_context());
336 StreamView::diskstream_changed ()
338 boost::shared_ptr<Track> t;
340 if ((t = _trackview.track()) != 0) {
341 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
343 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
348 StreamView::apply_color (Gdk::Color const& c, ColorTarget target)
350 return apply_color (gdk_color_to_rgba (c), target);
354 StreamView::apply_color (uint32_t color, ColorTarget target)
356 list<RegionView *>::iterator i;
360 region_color = color;
361 for (i = region_views.begin(); i != region_views.end(); ++i) {
362 (*i)->set_color (region_color);
366 case StreamBaseColor:
367 stream_base_color = color;
368 canvas_rect->set_fill_color (stream_base_color);
374 StreamView::region_layered (RegionView* rv)
376 /* don't ever leave it at the bottom, since then it doesn't
377 get events - the parent group does instead ...
379 rv->get_canvas_group()->raise (rv->region()->layer());
383 StreamView::rec_enable_changed ()
389 StreamView::sess_rec_enable_changed ()
395 StreamView::transport_changed()
401 StreamView::transport_looped()
403 // to force a new rec region
405 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
409 StreamView::create_rec_box(framepos_t frame_pos, double width)
411 const double xstart = _trackview.editor().sample_to_pixel(frame_pos);
412 const double xend = xstart + width;
413 const uint32_t fill_color = ARDOUR_UI::config()->color_mod("recording rect", "recording_rect");
415 ArdourCanvas::Rectangle* rec_rect = new ArdourCanvas::Rectangle(_canvas_group);
416 rec_rect->set_x0(xstart);
418 rec_rect->set_x1(xend);
419 rec_rect->set_y1(child_height ());
420 rec_rect->set_outline_what(ArdourCanvas::Rectangle::What(0));
421 rec_rect->set_outline_color(ARDOUR_UI::config()->color("recording rect"));
422 rec_rect->set_fill_color(fill_color);
423 rec_rect->lower_to_bottom();
426 recbox.rectangle = rec_rect;
429 if (rec_rects.empty()) {
430 recbox.start = _trackview.session()->record_location ();
432 recbox.start = _trackview.session()->transport_frame ();
435 rec_rects.push_back (recbox);
437 screen_update_connection.disconnect();
438 screen_update_connection = Timers::rapid_connect (sigc::mem_fun(*this, &StreamView::update_rec_box));
445 StreamView::update_rec_box ()
447 if (rec_active && rec_rects.size() > 0) {
448 /* only update the last box */
449 RecBoxInfo & rect = rec_rects.back();
450 framepos_t const at = _trackview.track()->current_capture_end ();
454 switch (_trackview.track()->mode()) {
458 rect.length = at - rect.start;
459 xstart = _trackview.editor().sample_to_pixel (rect.start);
460 xend = _trackview.editor().sample_to_pixel (at);
465 xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
466 xend = _trackview.editor().sample_to_pixel (at);
470 fatal << string_compose (_("programming error: %1"), "illegal track mode") << endmsg;
471 abort(); /*NOTREACHED*/
475 rect.rectangle->set_x0 (xstart);
476 rect.rectangle->set_x1 (xend);
481 StreamView::find_view (boost::shared_ptr<const Region> region)
483 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
485 if ((*i)->region() == region) {
493 StreamView::num_selected_regionviews () const
497 for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
498 if ((*i)->get_selected()) {
506 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
508 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
514 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
516 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
517 if ((*i)->get_selected()) {
524 StreamView::set_selected_regionviews (RegionSelection& regions)
528 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
532 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
539 (*i)->set_selected (selected);
543 /** Get selectable things within a given range.
544 * @param start Start time in session frames.
545 * @param end End time in session frames.
546 * @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
547 * @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
548 * @param result Filled in with selectable things.
551 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results, bool within)
553 if (_trackview.editor().internal_editing()) {
554 return; // Don't select regions with an internal tool
557 layer_t min_layer = 0;
558 layer_t max_layer = 0;
560 if (_layer_display == Stacked) {
561 double const c = child_height ();
563 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
570 int const ma = _layers - ((top - _trackview.y_position()) / c);
571 if (ma > (int) _layers) {
572 max_layer = _layers - 1;
579 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
581 bool layer_ok = true;
583 if (_layer_display == Stacked) {
584 layer_t const l = (*i)->region()->layer ();
585 layer_ok = (min_layer <= l && l <= max_layer);
589 if ((*i)->region()->coverage (start, end) == Evoral::OverlapExternal && layer_ok) {
590 results.push_back (*i);
593 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
594 results.push_back (*i);
602 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
604 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
605 if (!sel.regions.contains (*i)) {
606 results.push_back (*i);
611 /** @return height of a child region view, depending on stacked / overlaid mode */
613 StreamView::child_height () const
615 switch (_layer_display) {
619 return height / _layers;
621 return height / (_layers * 2 + 1);
624 abort(); /* NOTREACHED */
629 StreamView::update_contents_height ()
631 const double h = child_height ();
633 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
634 switch (_layer_display) {
639 (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
642 (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
646 (*i)->set_height (h);
649 for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
650 switch (_layer_display) {
652 i->rectangle->set_y1 (height);
656 /* In stacked displays, the recregion is always at the top */
657 i->rectangle->set_y0 (0);
658 i->rectangle->set_y1 (h);
663 ContentsHeightChanged (); /* EMIT SIGNAL */
667 StreamView::set_layer_display (LayerDisplay d)
671 if (_layer_display == Overlaid) {
675 update_contents_height ();
676 update_coverage_frames ();
680 StreamView::update_coverage_frames ()
682 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
683 (*i)->update_coverage_frames (_layer_display);
688 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
690 if (_new_rec_layer_time < to) {
691 /* The region being recorded has overlapped the start of a top-layered region, so
692 `fake' a new visual layer for the recording. This is only a visual thing for now,
693 as the proper layering will get sorted out when the recorded region is added to
697 /* Stop this happening again */
698 _new_rec_layer_time = max_framepos;
700 /* Make space in the view for the new layer */
703 /* Set the temporary region to the correct layer so that it gets drawn correctly */
704 region->set_layer (_layers - 1);
706 /* and reset the view */
707 update_contents_height ();
712 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
714 /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
715 recorded region in. To work out where this needs to happen, find the start of the next
716 top-layered region after the start of the region we are recording and make a note of it.
718 if (_layer_display == Stacked) {
719 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
721 _new_rec_layer_time = max_framepos;