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 "streamview.h"
32 #include "global_signals.h"
33 #include "region_view.h"
34 #include "route_time_axis.h"
35 #include "canvas-waveview.h"
36 #include "canvas-simplerect.h"
37 #include "region_selection.h"
38 #include "selection.h"
39 #include "public_editor.h"
40 #include "ardour_ui.h"
41 #include "rgb_macros.h"
42 #include "gui_thread.h"
46 using namespace ARDOUR;
48 using namespace Editing;
50 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* background_group, ArdourCanvas::Group* canvas_group)
52 , owns_background_group (background_group == 0)
53 , owns_canvas_group (canvas_group == 0)
54 , _background_group (background_group ? background_group : new ArdourCanvas::Group (*_trackview.canvas_background()))
55 , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Group(*_trackview.canvas_display()))
56 , _samples_per_unit (_trackview.editor().get_current_zoom ())
59 , stream_base_color(0xFFFFFFFF)
61 , _layer_display (Overlaid)
63 , last_rec_data_frame(0)
65 /* set_position() will position the group */
67 canvas_rect = new ArdourCanvas::SimpleRect (*_background_group);
68 canvas_rect->property_x1() = 0.0;
69 canvas_rect->property_y1() = 0.0;
70 canvas_rect->property_x2() = Gtkmm2ext::physical_screen_width (_trackview.editor().get_window());
71 canvas_rect->property_y2() = (double) tv.current_height();
72 canvas_rect->raise(1); // raise above tempo lines
74 canvas_rect->property_outline_what() = (guint32) (0x2|0x8); // outline RHS and bottom
75 canvas_rect->property_outline_color_rgba() = RGBA_TO_UINT (0, 0, 0, 255);
77 canvas_rect->signal_event().connect (sigc::bind (
78 sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
79 canvas_rect, &_trackview));
81 if (_trackview.is_track()) {
82 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
83 _trackview.track()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
85 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
86 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
87 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
90 ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
93 StreamView::~StreamView ()
99 if (owns_background_group) {
100 delete _background_group;
103 if (owns_canvas_group) {
104 delete _canvas_group;
109 StreamView::attach ()
111 if (_trackview.is_track()) {
112 display_track (_trackview.track ());
117 StreamView::set_position (gdouble x, gdouble y)
119 _canvas_group->property_x() = x;
120 _canvas_group->property_y() = y;
125 StreamView::set_height (double h)
127 /* limit the values to something sane-ish */
128 if (h < 10.0 || h > 1000.0) {
132 if (canvas_rect->property_y2() == h) {
137 canvas_rect->property_y2() = height;
138 update_contents_height ();
144 StreamView::set_samples_per_unit (gdouble spp)
146 RegionViewList::iterator i;
152 _samples_per_unit = spp;
154 for (i = region_views.begin(); i != region_views.end(); ++i) {
155 (*i)->set_samples_per_unit (spp);
158 for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
159 RecBoxInfo &recbox = (*xi);
161 gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
162 gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
164 recbox.rectangle->property_x1() = xstart;
165 recbox.rectangle->property_x2() = xend;
168 update_coverage_frames ();
174 StreamView::add_region_view (boost::weak_ptr<Region> wr)
176 boost::shared_ptr<Region> r (wr.lock());
181 add_region_view_internal (r, true);
183 if (_layer_display == Stacked || _layer_display == Expanded) {
184 update_contents_height ();
189 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
191 ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
193 boost::shared_ptr<Region> r (weak_r.lock());
199 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
200 if (((*i)->region()) == r) {
202 region_views.erase (i);
208 RegionViewRemoved (); /* EMIT SIGNAL */
212 StreamView::undisplay_track ()
214 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
215 RegionViewList::iterator next = i;
221 region_views.clear();
225 StreamView::display_track (boost::shared_ptr<Track> tr)
227 playlist_switched_connection.disconnect();
228 playlist_switched (tr);
229 tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
233 StreamView::layer_regions()
235 // In one traversal of the region view list:
236 // - Build a list of region views sorted by layer
237 // - Remove invalid views from the actual region view list
239 list<RegionView*>::iterator i, tmp;
240 for (i = region_views.begin(); i != region_views.end(); ) {
244 if (!(*i)->is_valid()) {
246 region_views.erase (i);
250 (*i)->enable_display(true);
253 if (copy.size() == 0) {
254 copy.push_front((*i));
259 RegionViewList::iterator k = copy.begin();
260 RegionViewList::iterator l = copy.end();
263 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
264 copy.push_front((*i));
267 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
268 copy.push_back((*i));
273 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
274 if ((*j)->region()->layer() >= (*i)->region()->layer()) {
275 copy.insert(j, (*i));
283 // Fix canvas layering by raising each to the top in the sorted order.
284 for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
285 (*i)->get_canvas_group()->raise_to_top ();
290 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
292 boost::shared_ptr<Track> tr (wtr.lock());
298 /* update layers count and the y positions and heights of our regions */
299 if (tr->playlist()) {
300 _layers = tr->playlist()->top_layer() + 1;
303 if (_layer_display == Stacked) {
304 update_contents_height ();
305 update_coverage_frames ();
307 /* layering has probably been modified. reflect this in the canvas. */
313 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
315 boost::shared_ptr<Track> tr (wtr.lock());
321 /* disconnect from old playlist */
323 playlist_connections.drop_connections ();
330 /* update layers count and the y positions and heights of our regions */
331 _layers = tr->playlist()->top_layer() + 1;
332 update_contents_height ();
333 update_coverage_frames ();
337 tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
338 tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
339 tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
340 tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frames, this), gui_context());
345 StreamView::diskstream_changed ()
347 boost::shared_ptr<Track> t;
349 if ((t = _trackview.track()) != 0) {
350 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
352 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
357 StreamView::apply_color (Gdk::Color color, ColorTarget target)
359 list<RegionView *>::iterator i;
363 region_color = color;
364 for (i = region_views.begin(); i != region_views.end(); ++i) {
365 (*i)->set_color (region_color);
369 case StreamBaseColor:
370 stream_base_color = RGBA_TO_UINT (
371 color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
372 canvas_rect->property_fill_color_rgba() = stream_base_color;
378 StreamView::region_layered (RegionView* rv)
380 /* don't ever leave it at the bottom, since then it doesn't
381 get events - the parent group does instead ...
383 rv->get_canvas_group()->raise (rv->region()->layer());
387 StreamView::rec_enable_changed ()
393 StreamView::sess_rec_enable_changed ()
399 StreamView::transport_changed()
405 StreamView::transport_looped()
407 // to force a new rec region
409 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
413 StreamView::update_rec_box ()
415 if (rec_active && rec_rects.size() > 0) {
416 /* only update the last box */
417 RecBoxInfo & rect = rec_rects.back();
418 framepos_t const at = _trackview.track()->current_capture_end ();
422 switch (_trackview.track()->mode()) {
426 rect.length = at - rect.start;
427 xstart = _trackview.editor().frame_to_pixel (rect.start);
428 xend = _trackview.editor().frame_to_pixel (at);
433 xstart = _trackview.editor().frame_to_pixel (_trackview.track()->current_capture_start());
434 xend = _trackview.editor().frame_to_pixel (at);
438 rect.rectangle->property_x1() = xstart;
439 rect.rectangle->property_x2() = xend;
444 StreamView::find_view (boost::shared_ptr<const Region> region)
446 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
448 if ((*i)->region() == region) {
456 StreamView::num_selected_regionviews () const
460 for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
461 if ((*i)->get_selected()) {
469 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
471 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
477 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
479 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
480 if ((*i)->get_selected()) {
487 StreamView::set_selected_regionviews (RegionSelection& regions)
491 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
495 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
502 (*i)->set_selected (selected);
507 /** Get selectable things within a given range.
508 * @param start Start time in session frames.
509 * @param end End time in session frames.
510 * @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
511 * @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
512 * @param result Filled in with selectable things.
516 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results)
518 layer_t min_layer = 0;
519 layer_t max_layer = 0;
521 if (_layer_display == Stacked) {
522 double const c = child_height ();
524 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
531 int const ma = _layers - ((top - _trackview.y_position()) / c);
532 if (ma > (int) _layers) {
533 max_layer = _layers - 1;
540 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
542 bool layer_ok = true;
544 if (_layer_display == Stacked) {
545 layer_t const l = (*i)->region()->layer ();
546 layer_ok = (min_layer <= l && l <= max_layer);
549 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
550 results.push_back (*i);
556 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
558 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
559 if (!sel.regions.contains (*i)) {
560 results.push_back (*i);
565 /** @return height of a child region view, depending on stacked / overlaid mode */
567 StreamView::child_height () const
569 switch (_layer_display) {
573 return height / _layers;
575 return height / (_layers * 2 + 1);
583 StreamView::update_contents_height ()
585 const double h = child_height ();
587 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
588 switch (_layer_display) {
593 (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
596 (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
600 (*i)->set_height (h);
603 for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
604 switch (_layer_display) {
606 i->rectangle->property_y2() = height;
610 /* In stacked displays, the recregion is always at the top */
611 i->rectangle->property_y1() = 0;
612 i->rectangle->property_y2() = h;
617 ContentsHeightChanged (); /* EMIT SIGNAL */
621 StreamView::set_layer_display (LayerDisplay d)
625 if (_layer_display == Overlaid) {
629 update_contents_height ();
630 update_coverage_frames ();
634 StreamView::update_coverage_frames ()
636 for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
637 (*i)->update_coverage_frames (_layer_display);
642 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
644 if (_new_rec_layer_time < to) {
645 /* The region being recorded has overlapped the start of a top-layered region, so
646 `fake' a new visual layer for the recording. This is only a visual thing for now,
647 as the proper layering will get sorted out when the recorded region is added to
651 /* Stop this happening again */
652 _new_rec_layer_time = max_framepos;
654 /* Make space in the view for the new layer */
657 /* Set the temporary region to the correct layer so that it gets drawn correctly */
658 region->set_layer (_layers - 1);
660 /* and reset the view */
661 update_contents_height ();
666 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
668 /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
669 recorded region in. To work out where this needs to happen, find the start of the next
670 top-layered region after the start of the region we are recording and make a note of it.
672 if (_layer_display == Stacked) {
673 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
675 _new_rec_layer_time = max_framepos;
680 StreamView::enter_internal_edit_mode ()
682 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
688 StreamView::leave_internal_edit_mode ()
690 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {