2 * Copyright (C) 2005-2019 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006-2007 Jesse Chappell <jesse@essej.net>
5 * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
6 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
7 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
8 * Copyright (C) 2014-2015 Tim Mayberry <mojofunk@gmail.com>
9 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
10 * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <gtkmm2ext/gtk_ui.h>
32 #include <gtkmm2ext/utils.h>
34 #include "ardour/playlist.h"
35 #include "ardour/region.h"
36 #include "ardour/track.h"
37 #include "ardour/session.h"
39 #include "pbd/compose.h"
41 #include "canvas/rectangle.h"
42 #include "canvas/debug.h"
44 #include "streamview.h"
45 #include "region_view.h"
46 #include "route_time_axis.h"
47 #include "region_selection.h"
48 #include "selection.h"
49 #include "public_editor.h"
51 #include "rgb_macros.h"
52 #include "gui_thread.h"
53 #include "ui_config.h"
59 using namespace ARDOUR;
60 using namespace ARDOUR_UI_UTILS;
62 using namespace Editing;
64 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Container* canvas_group)
66 , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Container (_trackview.canvas_display()))
67 , _samples_per_pixel (_trackview.editor().get_current_zoom ())
70 , stream_base_color(0xFFFFFFFF)
72 , _layer_display (Overlaid)
74 , last_rec_data_sample(0)
76 CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
78 /* set_position() will position the group */
80 canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
81 CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
82 canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
83 canvas_rect->set_outline (false);
84 canvas_rect->set_fill (true);
85 canvas_rect->Event.connect (sigc::bind (sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
87 if (_trackview.is_track()) {
88 _trackview.track()->rec_enable_control()->Changed.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
90 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
91 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
92 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
95 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
98 StreamView::~StreamView ()
106 StreamView::attach ()
108 if (_trackview.is_track()) {
109 display_track (_trackview.track ());
114 StreamView::set_position (gdouble x, gdouble y)
116 _canvas_group->set_position (ArdourCanvas::Duple (x, y));
121 StreamView::set_height (double h)
123 /* limit the values to something sane-ish */
125 if (h < 10.0 || h > 1000.0) {
134 canvas_rect->set_y1 (height);
135 update_contents_height ();
141 StreamView::set_samples_per_pixel (double fpp)
143 RegionViewList::iterator i;
149 if (fpp == _samples_per_pixel) {
153 _samples_per_pixel = fpp;
155 for (i = region_views.begin(); i != region_views.end(); ++i) {
156 (*i)->set_samples_per_pixel (fpp);
159 for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
160 RecBoxInfo &recbox = (*xi);
162 ArdourCanvas::Coord const xstart = _trackview.editor().sample_to_pixel (recbox.start);
163 ArdourCanvas::Coord const xend = _trackview.editor().sample_to_pixel (recbox.start + recbox.length);
165 recbox.rectangle->set_x0 (xstart);
166 recbox.rectangle->set_x1 (xend);
169 update_coverage_frame ();
175 StreamView::add_region_view (boost::weak_ptr<Region> wr)
177 boost::shared_ptr<Region> r (wr.lock());
182 add_region_view_internal (r, true);
184 if (_layer_display == Stacked || _layer_display == Expanded) {
185 update_contents_height ();
190 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
192 ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
194 boost::shared_ptr<Region> r (weak_r.lock());
200 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
201 if (((*i)->region()) == r) {
203 region_views.erase (i);
209 RegionViewRemoved (); /* EMIT SIGNAL */
213 StreamView::undisplay_track ()
215 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
216 RegionViewList::iterator next = i;
222 region_views.clear();
226 StreamView::display_track (boost::shared_ptr<Track> tr)
228 playlist_switched_connection.disconnect();
229 playlist_switched (tr);
230 tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
234 StreamView::layer_regions()
236 // In one traversal of the region view list:
237 // - Build a list of region views sorted by layer
238 // - Remove invalid views from the actual region view list
240 list<RegionView*>::iterator i, tmp;
241 for (i = region_views.begin(); i != region_views.end(); ) {
245 if (!(*i)->is_valid()) {
247 region_views.erase (i);
251 (*i)->enable_display(true);
254 if (copy.size() == 0) {
255 copy.push_front((*i));
260 RegionViewList::iterator k = copy.begin();
261 RegionViewList::iterator l = copy.end();
264 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
265 copy.push_front((*i));
268 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
269 copy.push_back((*i));
274 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
275 if ((*j)->region()->layer() >= (*i)->region()->layer()) {
276 copy.insert(j, (*i));
284 // Fix canvas layering by raising each to the top in the sorted order.
285 for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
286 (*i)->get_canvas_group()->raise_to_top ();
291 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
293 boost::shared_ptr<Track> tr (wtr.lock());
299 /* update layers count and the y positions and heights of our regions */
300 if (tr->playlist()) {
301 _layers = tr->playlist()->top_layer() + 1;
304 if (_layer_display == Stacked) {
305 update_contents_height ();
306 /* tricky. playlist_changed() does this as well, and its really inefficient. */
307 update_coverage_frame ();
309 /* layering has probably been modified. reflect this in the canvas. */
315 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
317 boost::shared_ptr<Track> tr (wtr.lock());
323 /* disconnect from old playlist */
325 playlist_connections.drop_connections ();
326 //undisplay_track ();
329 tr->playlist()->freeze();
331 tr->playlist()->thaw();
332 /* update layers count and the y positions and heights of our regions */
333 _layers = tr->playlist()->top_layer() + 1;
334 update_contents_height ();
335 update_coverage_frame ();
339 tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
340 tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
341 tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
342 tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frame, this), gui_context());
347 StreamView::diskstream_changed ()
349 boost::shared_ptr<Track> t;
351 if ((t = _trackview.track()) != 0) {
352 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
354 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
359 StreamView::apply_color (Gdk::Color const& c, ColorTarget target)
361 return apply_color (gdk_color_to_rgba (c), target);
365 StreamView::apply_color (uint32_t color, ColorTarget target)
367 list<RegionView *>::iterator i;
371 region_color = color;
372 for (i = region_views.begin(); i != region_views.end(); ++i) {
373 (*i)->set_color (region_color);
377 case StreamBaseColor:
378 stream_base_color = color;
379 canvas_rect->set_fill_color (stream_base_color);
385 StreamView::region_layered (RegionView* rv)
387 /* don't ever leave it at the bottom, since then it doesn't
388 get events - the parent group does instead ...
390 rv->get_canvas_group()->raise (rv->region()->layer());
394 StreamView::rec_enable_changed ()
400 StreamView::sess_rec_enable_changed ()
406 StreamView::transport_changed()
412 StreamView::transport_looped()
414 // to force a new rec region
416 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
420 StreamView::create_rec_box(samplepos_t sample_pos, double width)
422 const double xstart = _trackview.editor().sample_to_pixel(sample_pos);
423 const double xend = xstart + width;
424 const uint32_t fill_color = UIConfiguration::instance().color_mod("recording rect", "recording_rect");
426 ArdourCanvas::Rectangle* rec_rect = new ArdourCanvas::Rectangle(_canvas_group);
427 rec_rect->set_x0(xstart);
429 rec_rect->set_x1(xend);
430 rec_rect->set_y1(child_height ());
431 rec_rect->set_outline_what(ArdourCanvas::Rectangle::What(0));
432 rec_rect->set_outline_color(UIConfiguration::instance().color("recording rect"));
433 rec_rect->set_fill_color(fill_color);
434 rec_rect->lower_to_bottom();
437 recbox.rectangle = rec_rect;
440 if (rec_rects.empty()) {
441 recbox.start = _trackview.session()->record_location ();
443 recbox.start = _trackview.session()->transport_sample ();
446 rec_rects.push_back (recbox);
448 screen_update_connection.disconnect();
449 screen_update_connection = Timers::rapid_connect (sigc::mem_fun(*this, &StreamView::update_rec_box));
456 StreamView::update_rec_box ()
458 if (rec_active && rec_rects.size() > 0) {
459 /* only update the last box */
460 RecBoxInfo & rect = rec_rects.back();
461 samplepos_t const at = _trackview.track()->current_capture_end ();
465 switch (_trackview.track()->mode()) {
469 rect.length = at - rect.start;
470 xstart = _trackview.editor().sample_to_pixel (rect.start);
471 xend = _trackview.editor().sample_to_pixel (at);
476 xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
477 xend = _trackview.editor().sample_to_pixel (at);
481 fatal << string_compose (_("programming error: %1"), "illegal track mode") << endmsg;
482 abort(); /*NOTREACHED*/
486 rect.rectangle->set_x0 (xstart);
487 rect.rectangle->set_x1 (xend);
492 StreamView::find_view (boost::shared_ptr<const Region> region)
494 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
496 if ((*i)->region() == region) {
504 StreamView::num_selected_regionviews () const
508 for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
509 if ((*i)->selected()) {
517 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
519 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
525 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
527 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
528 if ((*i)->selected()) {
535 StreamView::set_selected_regionviews (RegionSelection& regions)
539 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
543 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
550 (*i)->set_selected (selected);
554 /** Get selectable things within a given range.
555 * @param start Start time in session samples.
556 * @param end End time in session samples.
557 * @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
558 * @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
559 * @param result Filled in with selectable things.
562 StreamView::get_selectables (samplepos_t start, samplepos_t end, double top, double bottom, list<Selectable*>& results, bool within)
564 if (_trackview.editor().internal_editing()) {
565 return; // Don't select regions with an internal tool
568 layer_t min_layer = 0;
569 layer_t max_layer = 0;
571 if (_layer_display == Stacked) {
572 double const c = child_height ();
574 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
581 int const ma = _layers - ((top - _trackview.y_position()) / c);
582 if (ma > (int) _layers) {
583 max_layer = _layers - 1;
590 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
592 bool layer_ok = true;
594 if (_layer_display == Stacked) {
595 layer_t const l = (*i)->region()->layer ();
596 layer_ok = (min_layer <= l && l <= max_layer);
600 if ((*i)->region()->coverage (start, end) == Evoral::OverlapExternal && layer_ok) {
601 results.push_back (*i);
604 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
605 results.push_back (*i);
613 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
615 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
616 if (!sel.regions.contains (*i)) {
617 results.push_back (*i);
622 /** @return height of a child region view, depending on stacked / overlaid mode */
624 StreamView::child_height () const
626 switch (_layer_display) {
630 return height / _layers;
632 return height / (_layers * 2 + 1);
635 abort(); /* NOTREACHED */
640 StreamView::update_contents_height ()
642 const double h = child_height ();
644 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
645 switch (_layer_display) {
650 (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
653 (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
657 (*i)->set_height (h);
660 for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
661 switch (_layer_display) {
663 i->rectangle->set_y1 (height);
667 /* In stacked displays, the recregion is always at the top */
668 i->rectangle->set_y0 (0);
669 i->rectangle->set_y1 (h);
674 ContentsHeightChanged (); /* EMIT SIGNAL */
678 StreamView::set_layer_display (LayerDisplay d)
682 if (_layer_display == Overlaid) {
686 update_contents_height ();
687 update_coverage_frame ();
691 StreamView::update_coverage_frame ()
693 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
694 (*i)->update_coverage_frame (_layer_display);
699 StreamView::check_record_layers (boost::shared_ptr<Region> region, samplepos_t to)
701 if (_new_rec_layer_time < to) {
702 /* The region being recorded has overlapped the start of a top-layered region, so
703 `fake' a new visual layer for the recording. This is only a visual thing for now,
704 as the proper layering will get sorted out when the recorded region is added to
708 /* Stop this happening again */
709 _new_rec_layer_time = max_samplepos;
711 /* Make space in the view for the new layer */
714 /* Set the temporary region to the correct layer so that it gets drawn correctly */
715 region->set_layer (_layers - 1);
717 /* and reset the view */
718 update_contents_height ();
723 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
725 /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
726 recorded region in. To work out where this needs to happen, find the start of the next
727 top-layered region after the start of the region we are recording and make a note of it.
729 if (_layer_display == Stacked) {
730 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
732 _new_rec_layer_time = max_samplepos;
737 StreamView::parameter_changed (string const & what)
739 if (what == "show-region-name") {
740 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
741 (*i)->update_visibility ();