Fix underflow in layer_t with rubber band selection on multiple tracks with stacked...
[ardour.git] / gtk2_ardour / streamview.cc
1 /*
2     Copyright (C) 2001, 2006 Paul Davis
3
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.
8
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.
13
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.
17 */
18
19 #include <cmath>
20
21 #include <gtkmm.h>
22
23 #include <gtkmm2ext/gtk_ui.h>
24 #include <gtkmm2ext/utils.h>
25
26 #include "ardour/playlist.h"
27 #include "ardour/region.h"
28 #include "ardour/source.h"
29 #include "ardour/track.h"
30 #include "ardour/session.h"
31
32 #include "streamview.h"
33 #include "global_signals.h"
34 #include "region_view.h"
35 #include "route_time_axis.h"
36 #include "canvas-waveview.h"
37 #include "canvas-simplerect.h"
38 #include "region_selection.h"
39 #include "selection.h"
40 #include "public_editor.h"
41 #include "ardour_ui.h"
42 #include "rgb_macros.h"
43 #include "gui_thread.h"
44 #include "utils.h"
45
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace PBD;
49 using namespace Editing;
50
51 StreamView::StreamView (RouteTimeAxisView& tv, ArdourCanvas::Group* background_group, ArdourCanvas::Group* canvas_group)
52         : _trackview (tv)
53         , owns_background_group (background_group == 0)
54         , owns_canvas_group (canvas_group == 0)
55         , _background_group (background_group ? background_group : new ArdourCanvas::Group (*_trackview.canvas_background()))
56         , _canvas_group (canvas_group ? canvas_group : new ArdourCanvas::Group(*_trackview.canvas_display()))
57         , _samples_per_unit (_trackview.editor().get_current_zoom ())
58         , rec_updating(false)
59         , rec_active(false)
60         , region_color(_trackview.color())
61         , stream_base_color(0xFFFFFFFF)
62         , _layers (1)
63         , _layer_display (Overlaid)
64         , height(tv.height)
65         , last_rec_data_frame(0)
66 {
67         /* set_position() will position the group */
68
69         canvas_rect = new ArdourCanvas::SimpleRect (*_background_group);
70         canvas_rect->property_x1() = 0.0;
71         canvas_rect->property_y1() = 0.0;
72         canvas_rect->property_x2() = Gtkmm2ext::physical_screen_width (_trackview.editor().get_window());
73         canvas_rect->property_y2() = (double) tv.current_height();
74         canvas_rect->raise(1); // raise above tempo lines
75
76         canvas_rect->property_outline_what() = (guint32) (0x2|0x8);  // outline RHS and bottom
77         canvas_rect->property_outline_color_rgba() = RGBA_TO_UINT (0, 0, 0, 255);
78
79         canvas_rect->signal_event().connect (sigc::bind (
80                         sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
81                         canvas_rect, &_trackview));
82
83         if (_trackview.is_track()) {
84                 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
85                 _trackview.track()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
86
87                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
88                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
89                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
90         }
91
92         ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
93 }
94
95 StreamView::~StreamView ()
96 {
97         undisplay_track ();
98
99         delete canvas_rect;
100
101         if (owns_background_group) {
102                 delete _background_group;
103         }
104
105         if (owns_canvas_group) {
106                 delete _canvas_group;
107         }
108 }
109
110 void
111 StreamView::attach ()
112 {
113         if (_trackview.is_track()) {
114                 display_track (_trackview.track ());
115         }
116 }
117
118 int
119 StreamView::set_position (gdouble x, gdouble y)
120 {
121         _canvas_group->property_x() = x;
122         _canvas_group->property_y() = y;
123         return 0;
124 }
125
126 int
127 StreamView::set_height (double h)
128 {
129         /* limit the values to something sane-ish */
130         if (h < 10.0 || h > 1000.0) {
131                 return -1;
132         }
133
134         if (canvas_rect->property_y2() == h) {
135                 return 0;
136         }
137
138         height = h;
139         canvas_rect->property_y2() = height;
140         update_contents_height ();
141
142         return 0;
143 }
144
145 int
146 StreamView::set_samples_per_unit (gdouble spp)
147 {
148         RegionViewList::iterator i;
149
150         if (spp < 1.0) {
151                 return -1;
152         }
153
154         _samples_per_unit = spp;
155
156         for (i = region_views.begin(); i != region_views.end(); ++i) {
157                 (*i)->set_samples_per_unit (spp);
158         }
159
160         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
161                 RecBoxInfo &recbox = (*xi);
162
163                 gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
164                 gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
165
166                 recbox.rectangle->property_x1() = xstart;
167                 recbox.rectangle->property_x2() = xend;
168         }
169
170         update_coverage_frames ();
171
172         return 0;
173 }
174
175 void
176 StreamView::add_region_view (boost::weak_ptr<Region> wr)
177 {
178         boost::shared_ptr<Region> r (wr.lock());
179         if (!r) {
180                 return;
181         }
182
183         add_region_view_internal (r, true);
184
185         if (_layer_display == Stacked) {
186                 update_contents_height ();
187         }
188 }
189
190 void
191 StreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
192 {
193         ENSURE_GUI_THREAD (*this, &StreamView::remove_region_view, weak_r)
194
195         boost::shared_ptr<Region> r (weak_r.lock());
196
197         if (!r) {
198                 return;
199         }
200
201         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
202                 if (((*i)->region()) == r) {
203                         RegionView* rv = *i;
204                         region_views.erase (i);
205                         delete rv;
206                         break;
207                 }
208         }
209
210         RegionViewRemoved (); /* EMIT SIGNAL */
211 }
212
213 void
214 StreamView::undisplay_track ()
215 {
216         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end() ; ) {
217                 RegionViewList::iterator next = i;
218                 ++next;
219                 delete *i;
220                 i = next;
221         }
222
223         region_views.clear();
224 }
225
226 void
227 StreamView::display_track (boost::shared_ptr<Track> tr)
228 {
229         playlist_switched_connection.disconnect();
230         playlist_switched (tr);
231         tr->PlaylistChanged.connect (playlist_switched_connection, invalidator (*this), boost::bind (&StreamView::playlist_switched, this, boost::weak_ptr<Track> (tr)), gui_context());
232 }
233
234 void
235 StreamView::layer_regions()
236 {
237         // In one traversal of the region view list:
238         // - Build a list of region views sorted by layer
239         // - Remove invalid views from the actual region view list
240         RegionViewList copy;
241         list<RegionView*>::iterator i, tmp;
242         for (i = region_views.begin(); i != region_views.end(); ) {
243                 tmp = i;
244                 tmp++;
245
246                 if (!(*i)->is_valid()) {
247                         delete *i;
248                         region_views.erase (i);
249                         i = tmp;
250                         continue;
251                 } else {
252                         (*i)->enable_display(true);
253                 }
254
255                 if (copy.size() == 0) {
256                         copy.push_front((*i));
257                         i = tmp;
258                         continue;
259                 }
260
261                 RegionViewList::iterator k = copy.begin();
262                 RegionViewList::iterator l = copy.end();
263                 l--;
264
265                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
266                         copy.push_front((*i));
267                         i = tmp;
268                         continue;
269                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
270                         copy.push_back((*i));
271                         i = tmp;
272                         continue;
273                 }
274
275                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
276                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
277                                 copy.insert(j, (*i));
278                                 break;
279                         }
280                 }
281
282                 i = tmp;
283         }
284
285         // Fix canvas layering by raising each to the top in the sorted order.
286         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
287                 (*i)->get_canvas_group()->raise_to_top ();
288         }
289 }
290
291 void
292 StreamView::playlist_layered (boost::weak_ptr<Track> wtr)
293 {
294         boost::shared_ptr<Track> tr (wtr.lock());
295
296         if (!tr) {
297                 return;
298         }
299
300         /* update layers count and the y positions and heights of our regions */
301         if (tr->playlist()) {
302                 _layers = tr->playlist()->top_layer() + 1;
303         }
304
305         if (_layer_display == Stacked) {
306                 update_contents_height ();
307                 update_coverage_frames ();
308         } else {
309                 /* layering has probably been modified. reflect this in the canvas. */
310                 layer_regions();
311         }
312 }
313
314 void
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         /* update layers count and the y positions and heights of our regions */
329         _layers = tr->playlist()->top_layer() + 1;
330         update_contents_height ();
331         update_coverage_frames ();
332
333         tr->playlist()->set_explicit_relayering (_layer_display == Stacked);
334
335         /* draw it */
336
337         redisplay_track ();
338
339         /* catch changes */
340
341         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
342         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::add_region_view, this, _1), gui_context());
343         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::remove_region_view, this, _1), gui_context());
344         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::update_coverage_frames, this), gui_context());
345 }
346
347 void
348 StreamView::diskstream_changed ()
349 {
350         boost::shared_ptr<Track> t;
351
352         if ((t = _trackview.track()) != 0) {
353                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
354         } else {
355                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
356         }
357 }
358
359 void
360 StreamView::apply_color (Gdk::Color color, ColorTarget target)
361 {
362         list<RegionView *>::iterator i;
363
364         switch (target) {
365         case RegionColor:
366                 region_color = color;
367                 for (i = region_views.begin(); i != region_views.end(); ++i) {
368                         (*i)->set_color (region_color);
369                 }
370                 break;
371
372         case StreamBaseColor:
373                 stream_base_color = RGBA_TO_UINT (
374                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
375                 canvas_rect->property_fill_color_rgba() = stream_base_color;
376                 break;
377         }
378 }
379
380 void
381 StreamView::region_layered (RegionView* rv)
382 {
383         /* don't ever leave it at the bottom, since then it doesn't
384            get events - the  parent group does instead ...
385         */
386         rv->get_canvas_group()->raise (rv->region()->layer());
387 }
388
389 void
390 StreamView::rec_enable_changed ()
391 {
392         setup_rec_box ();
393 }
394
395 void
396 StreamView::sess_rec_enable_changed ()
397 {
398         setup_rec_box ();
399 }
400
401 void
402 StreamView::transport_changed()
403 {
404         setup_rec_box ();
405 }
406
407 void
408 StreamView::transport_looped()
409 {
410         // to force a new rec region
411         rec_active = false;
412         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
413 }
414
415 void
416 StreamView::update_rec_box ()
417 {
418         if (rec_active && rec_rects.size() > 0) {
419                 /* only update the last box */
420                 RecBoxInfo & rect = rec_rects.back();
421                 framepos_t const at = _trackview.track()->current_capture_end ();
422                 double xstart;
423                 double xend;
424
425                 switch (_trackview.track()->mode()) {
426
427                 case NonLayered:
428                 case Normal:
429                         rect.length = at - rect.start;
430                         xstart = _trackview.editor().frame_to_pixel (rect.start);
431                         xend = _trackview.editor().frame_to_pixel (at);
432                         break;
433
434                 case Destructive:
435                         rect.length = 2;
436                         xstart = _trackview.editor().frame_to_pixel (_trackview.track()->current_capture_start());
437                         xend = _trackview.editor().frame_to_pixel (at);
438                         break;
439                 }
440
441                 rect.rectangle->property_x1() = xstart;
442                 rect.rectangle->property_x2() = xend;
443         }
444 }
445
446 RegionView*
447 StreamView::find_view (boost::shared_ptr<const Region> region)
448 {
449         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
450
451                 if ((*i)->region() == region) {
452                         return *i;
453                 }
454         }
455         return 0;
456 }
457
458 uint32_t
459 StreamView::num_selected_regionviews () const
460 {
461         uint32_t cnt = 0;
462
463         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
464                 if ((*i)->get_selected()) {
465                         ++cnt;
466                 }
467         }
468         return cnt;
469 }
470
471 void
472 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
473 {
474         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
475                 slot (*i);
476         }
477 }
478
479 void
480 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
481 {
482         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
483                 if ((*i)->get_selected()) {
484                         slot (*i);
485                 }
486         }
487 }
488
489 void
490 StreamView::set_selected_regionviews (RegionSelection& regions)
491 {
492         bool selected;
493
494         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
495
496                 selected = false;
497
498                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
499                         if (*i == *ii) {
500                                 selected = true;
501                                 break;
502                         }
503                 }
504
505                 (*i)->set_selected (selected);
506         }
507 }
508
509
510 /** Get selectable things within a given range.
511  *  @param start Start time in session frames.
512  *  @param end End time in session frames.
513  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
514  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
515  *  @param result Filled in with selectable things.
516  */
517
518 void
519 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results)
520 {
521         layer_t min_layer = 0;
522         layer_t max_layer = 0;
523
524         if (_layer_display == Stacked) {
525                 double const c = child_height ();
526
527                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
528                 if (mi < 0) {
529                         min_layer = 0;
530                 } else {
531                         min_layer = mi;
532                 }
533
534                 int const ma = _layers - ((top - _trackview.y_position()) / c);
535                 if (ma > _layers) {
536                         max_layer = _layers - 1;
537                 } else {
538                         max_layer = ma;
539                 }
540
541         }
542
543         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
544
545                 bool layer_ok = true;
546
547                 if (_layer_display == Stacked) {
548                         layer_t const l = (*i)->region()->layer ();
549                         layer_ok = (min_layer <= l && l <= max_layer);
550                 }
551
552                 if ((*i)->region()->coverage (start, end) != OverlapNone && layer_ok) {
553                         results.push_back (*i);
554                 }
555         }
556 }
557
558 void
559 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
560 {
561         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
562                 if (!sel.regions.contains (*i)) {
563                         results.push_back (*i);
564                 }
565         }
566 }
567
568 /** @return height of a child region view, depending on stacked / overlaid mode */
569 double
570 StreamView::child_height () const
571 {
572         if (_layer_display == Stacked) {
573                 return height / _layers;
574         }
575
576         return height;
577 }
578
579 void
580 StreamView::update_contents_height ()
581 {
582         const double h = child_height ();
583
584         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
585                 switch (_layer_display) {
586                 case Overlaid:
587                         (*i)->set_y (0);
588                         break;
589                 case Stacked:
590                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
591                         break;
592                 }
593
594                 (*i)->set_height (h);
595         }
596
597         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
598                 switch (_layer_display) {
599                 case Overlaid:
600                         i->rectangle->property_y2() = height;
601                         break;
602                 case Stacked:
603                         /* In stacked displays, the recregion is always at the top */
604                         i->rectangle->property_y1() = 0;
605                         i->rectangle->property_y2() = h;
606                         break;
607                 }
608         }
609 }
610
611 void
612 StreamView::set_layer_display (LayerDisplay d)
613 {
614         _layer_display = d;
615         update_contents_height ();
616         update_coverage_frames ();
617         _trackview.track()->playlist()->set_explicit_relayering (_layer_display == Stacked);
618 }
619
620 void
621 StreamView::update_coverage_frames ()
622 {
623         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
624                 (*i)->update_coverage_frames (_layer_display);
625         }
626 }
627
628 void
629 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
630 {
631         if (_new_rec_layer_time < to) {
632                 /* The region being recorded has overlapped the start of a top-layered region, so
633                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
634                    as the proper layering will get sorted out when the recorded region is added to
635                    its playlist.
636                 */
637
638                 /* Stop this happening again */
639                 _new_rec_layer_time = max_framepos;
640
641                 /* Make space in the view for the new layer */
642                 ++_layers;
643
644                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
645                 region->set_layer (_layers - 1);
646
647                 /* and reset the view */
648                 update_contents_height ();
649         }
650 }
651
652 void
653 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
654 {
655         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
656            recorded region in.  To work out where this needs to happen, find the start of the next
657            top-layered region after the start of the region we are recording and make a note of it.
658         */
659         if (_layer_display == Stacked) {
660                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
661         } else {
662                 _new_rec_layer_time = max_framepos;
663         }
664 }
665
666 void
667 StreamView::enter_internal_edit_mode ()
668 {
669         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
670                 (*i)->hide_rect ();
671         }
672 }
673
674 void
675 StreamView::leave_internal_edit_mode ()
676 {
677         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
678                 (*i)->show_rect ();
679         }
680 }