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