1 /*
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>
11 *
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.
16 *
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.
21 *
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.
25 */
26
27 #include <cmath>
28
29 #include <gtkmm.h>
30
31 #include <gtkmm2ext/gtk_ui.h>
32 #include <gtkmm2ext/utils.h>
33
34 #include "ardour/playlist.h"
35 #include "ardour/region.h"
36 #include "ardour/track.h"
37 #include "ardour/session.h"
38
39 #include "pbd/compose.h"
40
41 #include "canvas/rectangle.h"
42 #include "canvas/debug.h"
43
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"
50 #include "timers.h"
51 #include "rgb_macros.h"
52 #include "gui_thread.h"
53 #include "ui_config.h"
54 #include "utils.h"
55
56 #include "pbd/i18n.h"
57
58 using namespace std;
59 using namespace ARDOUR;
60 using namespace ARDOUR_UI_UTILS;
61 using namespace PBD;
62 using namespace Editing;
63
StreamView(RouteTimeAxisView & tv,ArdourCanvas::Container * canvas_group)64 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Container* canvas_group)
65 : _trackview (tv)
66 , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Container (_trackview.canvas_display()))
67 , _samples_per_pixel (_trackview.editor().get_current_zoom ())
68 , rec_updating(false)
69 , rec_active(false)
70 , stream_base_color(0xFFFFFFFF)
71 , _layers (1)
72 , _layer_display (Overlaid)
73 , height (tv.height)
74 , last_rec_data_sample(0)
75 {
76 CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
77
78 /* set_position() will position the group */
79
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));
86
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());
89
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());
93 }
94
95 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
96 }
97
~StreamView()98 StreamView::~StreamView ()
99 {
100 undisplay_track ();
101
102 delete canvas_rect;
103 }
104
105 void
attach()106 StreamView::attach ()
107 {
108 if (_trackview.is_track()) {
109 display_track (_trackview.track ());
110 }
111 }
112
113 int
set_position(gdouble x,gdouble y)114 StreamView::set_position (gdouble x, gdouble y)
115 {
116 _canvas_group->set_position (ArdourCanvas::Duple (x, y));
117 return 0;
118 }
119
120 int
set_height(double h)121 StreamView::set_height (double h)
122 {
123 /* limit the values to something sane-ish */
124
125 if (h < 10.0 || h > 1000.0) {
126 return -1;
127 }
128
129 if (height == h) {
130 return 0;
131 }
132
133 height = h;
134 canvas_rect->set_y1 (height);
135 update_contents_height ();
136
137 return 0;
138 }
139
140 int
set_samples_per_pixel(double fpp)141 StreamView::set_samples_per_pixel (double fpp)
142 {
143 RegionViewList::iterator i;
144
145 if (fpp < 1.0) {
146 return -1;
147 }
148
149 if (fpp == _samples_per_pixel) {
150 return 0;
151 }
152
153 _samples_per_pixel = fpp;
154
155 for (i = region_views.begin(); i != region_views.end(); ++i) {
156 (*i)->set_samples_per_pixel (fpp);
157 }
158
159 for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
160 RecBoxInfo &recbox = (*xi);
161
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);
164
165 recbox.rectangle->set_x0 (xstart);
166 recbox.rectangle->set_x1 (xend);
167 }
168
169 update_coverage_frame ();
170
171 return 0;
172 }
173
174 void
add_region_view(boost::weak_ptr<Region> wr)175 StreamView::add_region_view (boost::weak_ptr<Region> wr)
176 {
177 boost::shared_ptr<Region> r (wr.lock());
178 if (!r) {
179 return;
180 }
181
182 add_region_view_internal (r, true);
183
184 if (_layer_display == Stacked || _layer_display == Expanded) {
185 update_contents_height ();
186 }
187 }
188
189 void
remove_region_view(boost::weak_ptr<Region> weak_r)190 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
191 {
192 ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
193
194 boost::shared_ptr<Region> r (weak_r.lock());
195
196 if (!r) {
197 return;
198 }
199
200 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
201 if (((*i)->region()) == r) {
202 RegionView* rv = *i;
203 region_views.erase (i);
204 delete rv;
205 break;
206 }
207 }
208
209 RegionViewRemoved (); /* EMIT SIGNAL */
210 }
211
212 void
undisplay_track()213 StreamView::undisplay_track ()
214 {
215 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
216 RegionViewList::iterator next = i;
217 ++next;
218 delete *i;
219 i = next;
220 }
221
222 region_views.clear();
223 }
224
225 void
display_track(boost::shared_ptr<Track> tr)226 StreamView::display_track (boost::shared_ptr<Track> tr)
227 {
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());
231 }
232
233 void
layer_regions()234 StreamView::layer_regions()
235 {
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
239 RegionViewList copy;
240 list<RegionView*>::iterator i, tmp;
241 for (i = region_views.begin(); i != region_views.end(); ) {
242 tmp = i;
243 tmp++;
244
245 if (!(*i)->is_valid()) {
246 delete *i;
247 region_views.erase (i);
248 i = tmp;
249 continue;
250 } else {
251 (*i)->enable_display(true);
252 }
253
254 if (copy.size() == 0) {
255 copy.push_front((*i));
256 i = tmp;
257 continue;
258 }
259
260 RegionViewList::iterator k = copy.begin();
261 RegionViewList::iterator l = copy.end();
262 l--;
263
264 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
265 copy.push_front((*i));
266 i = tmp;
267 continue;
268 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
269 copy.push_back((*i));
270 i = tmp;
271 continue;
272 }
273
274 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
275 if ((*j)->region()->layer() >= (*i)->region()->layer()) {
276 copy.insert(j, (*i));
277 break;
278 }
279 }
280
281 i = tmp;
282 }
283
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 ();
287 }
288 }
289
290 void
playlist_layered(boost::weak_ptr<Track> wtr)291 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
292 {
293 boost::shared_ptr<Track> tr (wtr.lock());
294
295 if (!tr) {
296 return;
297 }
298
299 /* update layers count and the y positions and heights of our regions */
300 if (tr->playlist()) {
301 _layers = tr->playlist()->top_layer() + 1;
302 }
303
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 ();
308 } else {
309 /* layering has probably been modified. reflect this in the canvas. */
310 layer_regions();
311 }
312 }
313
314 void
playlist_switched(boost::weak_ptr<Track> wtr)315 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
316 {
317 boost::shared_ptr<Track> tr (wtr.lock());
318
319 if (!tr) {
320 return;
321 }
322
323 /* disconnect from old playlist */
324
325 playlist_connections.drop_connections ();
326 //undisplay_track ();
327
328 /* draw it */
329 tr->playlist()->freeze();
330 redisplay_track ();
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 ();
336
337 /* catch changes */
338
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());
343 }
344
345
346 void
apply_color(Gdk::Color const & c,ColorTarget target)347 StreamView::apply_color (Gdk::Color const& c, ColorTarget target)
348 {
349 return apply_color (gdk_color_to_rgba (c), target);
350 }
351
352 void
apply_color(uint32_t color,ColorTarget target)353 StreamView::apply_color (uint32_t color, ColorTarget target)
354 {
355 list<RegionView *>::iterator i;
356
357 switch (target) {
358 case RegionColor:
359 region_color = color;
360 for (i = region_views.begin(); i != region_views.end(); ++i) {
361 (*i)->set_color (region_color);
362 }
363 break;
364
365 case StreamBaseColor:
366 stream_base_color = color;
367 canvas_rect->set_fill_color (stream_base_color);
368 break;
369 }
370 }
371
372 void
region_layered(RegionView * rv)373 StreamView::region_layered (RegionView* rv)
374 {
375 /* don't ever leave it at the bottom, since then it doesn't
376 get events - the parent group does instead ...
377 */
378 rv->get_canvas_group()->raise (rv->region()->layer());
379 }
380
381 void
rec_enable_changed()382 StreamView::rec_enable_changed ()
383 {
384 setup_rec_box ();
385 }
386
387 void
sess_rec_enable_changed()388 StreamView::sess_rec_enable_changed ()
389 {
390 setup_rec_box ();
391 }
392
393 void
transport_changed()394 StreamView::transport_changed()
395 {
396 setup_rec_box ();
397 }
398
399 void
transport_looped()400 StreamView::transport_looped()
401 {
402 // to force a new rec region
403 rec_active = false;
404 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
405 }
406
407 void
create_rec_box(samplepos_t sample_pos,double width)408 StreamView::create_rec_box(samplepos_t sample_pos, double width)
409 {
410 const double xstart = _trackview.editor().sample_to_pixel(sample_pos);
411 const double xend = xstart + width;
412 const uint32_t fill_color = UIConfiguration::instance().color_mod("recording rect", "recording_rect");
413
414 ArdourCanvas::Rectangle* rec_rect = new ArdourCanvas::Rectangle(_canvas_group);
415 rec_rect->set_x0(xstart);
416 rec_rect->set_y0(0);
417 rec_rect->set_x1(xend);
418 rec_rect->set_y1(child_height ());
419 rec_rect->set_outline_what(ArdourCanvas::Rectangle::What(0));
420 rec_rect->set_outline_color(UIConfiguration::instance().color("recording rect"));
421 rec_rect->set_fill_color(fill_color);
422 rec_rect->lower_to_bottom();
423
424 RecBoxInfo recbox;
425 recbox.rectangle = rec_rect;
426 recbox.length = 0;
427
428 if (rec_rects.empty()) {
429 recbox.start = _trackview.session()->record_location ();
430 } else {
431 recbox.start = _trackview.session()->transport_sample ();
432 }
433
434 rec_rects.push_back (recbox);
435
436 screen_update_connection.disconnect();
437 screen_update_connection = Timers::rapid_connect (sigc::mem_fun(*this, &StreamView::update_rec_box));
438
439 rec_updating = true;
440 rec_active = true;
441 }
442
443 void
update_rec_box()444 StreamView::update_rec_box ()
445 {
446 if (rec_active && rec_rects.size() > 0) {
447 /* only update the last box */
448 RecBoxInfo & rect = rec_rects.back();
449 samplepos_t const at = _trackview.track()->current_capture_end ();
450 double xstart;
451 double xend;
452
453 switch (_trackview.track()->mode()) {
454
455 case NonLayered:
456 case Normal:
457 rect.length = at - rect.start;
458 xstart = _trackview.editor().sample_to_pixel (rect.start);
459 xend = _trackview.editor().sample_to_pixel (at);
460 break;
461
462 default:
463 fatal << string_compose (_("programming error: %1"), "illegal track mode") << endmsg;
464 abort(); /*NOTREACHED*/
465 return;
466 }
467
468 rect.rectangle->set_x0 (xstart);
469 rect.rectangle->set_x1 (xend);
470 }
471 }
472
473 RegionView*
find_view(boost::shared_ptr<const Region> region)474 StreamView::find_view (boost::shared_ptr<const Region> region)
475 {
476 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
477
478 if ((*i)->region() == region) {
479 return *i;
480 }
481 }
482 return 0;
483 }
484
485 uint32_t
num_selected_regionviews() const486 StreamView::num_selected_regionviews () const
487 {
488 uint32_t cnt = 0;
489
490 for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
491 if ((*i)->selected()) {
492 ++cnt;
493 }
494 }
495 return cnt;
496 }
497
498 void
foreach_regionview(sigc::slot<void,RegionView * > slot)499 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
500 {
501 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
502 slot (*i);
503 }
504 }
505
506 void
foreach_selected_regionview(sigc::slot<void,RegionView * > slot)507 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
508 {
509 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
510 if ((*i)->selected()) {
511 slot (*i);
512 }
513 }
514 }
515
516 void
set_selected_regionviews(RegionSelection & regions)517 StreamView::set_selected_regionviews (RegionSelection& regions)
518 {
519 bool selected;
520
521 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
522
523 selected = false;
524
525 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
526 if (*i == *ii) {
527 selected = true;
528 break;
529 }
530 }
531
532 (*i)->set_selected (selected);
533 }
534 }
535
536 /** Get selectable things within a given range.
537 * @param start Start time in session samples.
538 * @param end End time in session samples.
539 * @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
540 * @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
541 * @param result Filled in with selectable things.
542 */
543 void
get_selectables(samplepos_t start,samplepos_t end,double top,double bottom,list<Selectable * > & results,bool within)544 StreamView::get_selectables (samplepos_t start, samplepos_t end, double top, double bottom, list<Selectable*>& results, bool within)
545 {
546 if (_trackview.editor().internal_editing()) {
547 return; // Don't select regions with an internal tool
548 }
549
550 layer_t min_layer = 0;
551 layer_t max_layer = 0;
552
553 if (_layer_display == Stacked) {
554 double const c = child_height ();
555
556 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
557 if (mi < 0) {
558 min_layer = 0;
559 } else {
560 min_layer = mi;
561 }
562
563 int const ma = _layers - ((top - _trackview.y_position()) / c);
564 if (ma > (int) _layers) {
565 max_layer = _layers - 1;
566 } else {
567 max_layer = ma;
568 }
569
570 }
571
572 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
573
574 bool layer_ok = true;
575
576 if (_layer_display == Stacked) {
577 layer_t const l = (*i)->region()->layer ();
578 layer_ok = (min_layer <= l && l <= max_layer);
579 }
580
581 if (within) {
582 if ((*i)->region()->coverage (start, end) == Evoral::OverlapExternal && layer_ok) {
583 results.push_back (*i);
584 }
585 } else {
586 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
587 results.push_back (*i);
588 }
589 }
590
591 }
592 }
593
594 void
get_inverted_selectables(Selection & sel,list<Selectable * > & results)595 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
596 {
597 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
598 if (!sel.regions.contains (*i)) {
599 results.push_back (*i);
600 }
601 }
602 }
603
604 /** @return height of a child region view, depending on stacked / overlaid mode */
605 double
child_height() const606 StreamView::child_height () const
607 {
608 switch (_layer_display) {
609 case Overlaid:
610 return height;
611 case Stacked:
612 return height / _layers;
613 case Expanded:
614 return height / (_layers * 2 + 1);
615 }
616
617 abort(); /* NOTREACHED */
618 return height;
619 }
620
621 void
update_contents_height()622 StreamView::update_contents_height ()
623 {
624 const double h = child_height ();
625
626 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
627 switch (_layer_display) {
628 case Overlaid:
629 (*i)->set_y (0);
630 break;
631 case Stacked:
632 (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
633 break;
634 case Expanded:
635 (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
636 break;
637 }
638
639 (*i)->set_height (h);
640 }
641
642 for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
643 switch (_layer_display) {
644 case Overlaid:
645 i->rectangle->set_y1 (height);
646 break;
647 case Stacked:
648 case Expanded:
649 /* In stacked displays, the recregion is always at the top */
650 i->rectangle->set_y0 (0);
651 i->rectangle->set_y1 (h);
652 break;
653 }
654 }
655
656 ContentsHeightChanged (); /* EMIT SIGNAL */
657 }
658
659 void
set_layer_display(LayerDisplay d)660 StreamView::set_layer_display (LayerDisplay d)
661 {
662 _layer_display = d;
663
664 if (_layer_display == Overlaid) {
665 layer_regions ();
666 }
667
668 update_contents_height ();
669 update_coverage_frame ();
670 }
671
672 void
update_coverage_frame()673 StreamView::update_coverage_frame ()
674 {
675 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
676 (*i)->update_coverage_frame (_layer_display);
677 }
678 }
679
680 void
check_record_layers(boost::shared_ptr<Region> region,samplepos_t to)681 StreamView::check_record_layers (boost::shared_ptr<Region> region, samplepos_t to)
682 {
683 if (_new_rec_layer_time < to) {
684 /* The region being recorded has overlapped the start of a top-layered region, so
685 `fake' a new visual layer for the recording. This is only a visual thing for now,
686 as the proper layering will get sorted out when the recorded region is added to
687 its playlist.
688 */
689
690 /* Stop this happening again */
691 _new_rec_layer_time = max_samplepos;
692
693 /* Make space in the view for the new layer */
694 ++_layers;
695
696 /* Set the temporary region to the correct layer so that it gets drawn correctly */
697 region->set_layer (_layers - 1);
698
699 /* and reset the view */
700 update_contents_height ();
701 }
702 }
703
704 void
setup_new_rec_layer_time(boost::shared_ptr<Region> region)705 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
706 {
707 /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
708 recorded region in. To work out where this needs to happen, find the start of the next
709 top-layered region after the start of the region we are recording and make a note of it.
710 */
711 if (_layer_display == Stacked) {
712 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->position());
713 } else {
714 _new_rec_layer_time = max_samplepos;
715 }
716 }
717
718 void
parameter_changed(string const & what)719 StreamView::parameter_changed (string const & what)
720 {
721 if (what == "show-region-name") {
722 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
723 (*i)->update_visibility ();
724 }
725 }
726 }
727