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