Restore update of coverage frames on playlist contents changed, which is needed for...
[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         , 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         canvas_rect->property_outline_color_rgba() = RGBA_TO_UINT (0, 0, 0, 255);
77
78         canvas_rect->signal_event().connect (sigc::bind (
79                         sigc::mem_fun (_trackview.editor(), &PublicEditor::canvas_stream_view_event),
80                         canvas_rect, &_trackview));
81
82         if (_trackview.is_track()) {
83                 _trackview.track()->DiskstreamChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::diskstream_changed, this), gui_context());
84                 _trackview.track()->RecordEnableChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::rec_enable_changed, this), gui_context());
85
86                 _trackview.session()->TransportStateChange.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_changed, this), gui_context());
87                 _trackview.session()->TransportLooped.connect (*this, invalidator (*this), boost::bind (&StreamView::transport_looped, this), gui_context());
88                 _trackview.session()->RecordStateChanged.connect (*this, invalidator (*this), boost::bind (&StreamView::sess_rec_enable_changed, this), gui_context());
89         }
90
91         ColorsChanged.connect (sigc::mem_fun (*this, &StreamView::color_handler));
92 }
93
94 StreamView::~StreamView ()
95 {
96         undisplay_track ();
97
98         delete canvas_rect;
99
100         if (owns_background_group) {
101                 delete _background_group;
102         }
103
104         if (owns_canvas_group) {
105                 delete _canvas_group;
106         }
107 }
108
109 void
110 StreamView::attach ()
111 {
112         if (_trackview.is_track()) {
113                 display_track (_trackview.track ());
114         }
115 }
116
117 int
118 StreamView::set_position (gdouble x, gdouble y)
119 {
120         _canvas_group->property_x() = x;
121         _canvas_group->property_y() = y;
122         return 0;
123 }
124
125 int
126 StreamView::set_height (double h)
127 {
128         /* limit the values to something sane-ish */
129         if (h < 10.0 || h > 1000.0) {
130                 return -1;
131         }
132
133         if (canvas_rect->property_y2() == h) {
134                 return 0;
135         }
136
137         height = h;
138         canvas_rect->property_y2() = height;
139         update_contents_height ();
140
141         return 0;
142 }
143
144 int
145 StreamView::set_samples_per_unit (gdouble spp)
146 {
147         RegionViewList::iterator i;
148
149         if (spp < 1.0) {
150                 return -1;
151         }
152
153         _samples_per_unit = spp;
154
155         for (i = region_views.begin(); i != region_views.end(); ++i) {
156                 (*i)->set_samples_per_unit (spp);
157         }
158
159         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
160                 RecBoxInfo &recbox = (*xi);
161
162                 gdouble xstart = _trackview.editor().frame_to_pixel (recbox.start);
163                 gdouble xend = _trackview.editor().frame_to_pixel (recbox.start + recbox.length);
164
165                 recbox.rectangle->property_x1() = xstart;
166                 recbox.rectangle->property_x2() = xend;
167         }
168
169         update_coverage_frames ();
170
171         return 0;
172 }
173
174 void
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
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
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
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
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
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                 update_coverage_frames ();
307         } else {
308                 /* layering has probably been modified. reflect this in the canvas. */
309                 layer_regions();
310         }
311 }
312
313 void
314 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
315 {
316         boost::shared_ptr<Track> tr (wtr.lock());
317
318         if (!tr) {
319                 return;
320         }
321
322         /* disconnect from old playlist */
323
324         playlist_connections.drop_connections ();
325         undisplay_track ();
326
327         /* draw it */
328
329         redisplay_track ();
330
331         /* update layers count and the y positions and heights of our regions */
332         _layers = tr->playlist()->top_layer() + 1;
333         update_contents_height ();
334         update_coverage_frames ();
335
336         /* catch changes */
337
338         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
339         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::add_region_view, this, _1), gui_context());
340         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::remove_region_view, this, _1), gui_context());
341         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), ui_bind (&StreamView::update_coverage_frames, this), gui_context());
342 }
343
344
345 void
346 StreamView::diskstream_changed ()
347 {
348         boost::shared_ptr<Track> t;
349
350         if ((t = _trackview.track()) != 0) {
351                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
352         } else {
353                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
354         }
355 }
356
357 void
358 StreamView::apply_color (Gdk::Color color, ColorTarget target)
359 {
360         list<RegionView *>::iterator i;
361
362         switch (target) {
363         case RegionColor:
364                 region_color = color;
365                 for (i = region_views.begin(); i != region_views.end(); ++i) {
366                         (*i)->set_color (region_color);
367                 }
368                 break;
369
370         case StreamBaseColor:
371                 stream_base_color = RGBA_TO_UINT (
372                         color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
373                 canvas_rect->property_fill_color_rgba() = stream_base_color;
374                 break;
375         }
376 }
377
378 void
379 StreamView::region_layered (RegionView* rv)
380 {
381         /* don't ever leave it at the bottom, since then it doesn't
382            get events - the  parent group does instead ...
383         */
384         rv->get_canvas_group()->raise (rv->region()->layer());
385 }
386
387 void
388 StreamView::rec_enable_changed ()
389 {
390         setup_rec_box ();
391 }
392
393 void
394 StreamView::sess_rec_enable_changed ()
395 {
396         setup_rec_box ();
397 }
398
399 void
400 StreamView::transport_changed()
401 {
402         setup_rec_box ();
403 }
404
405 void
406 StreamView::transport_looped()
407 {
408         // to force a new rec region
409         rec_active = false;
410         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
411 }
412
413 void
414 StreamView::update_rec_box ()
415 {
416         if (rec_active && rec_rects.size() > 0) {
417                 /* only update the last box */
418                 RecBoxInfo & rect = rec_rects.back();
419                 framepos_t const at = _trackview.track()->current_capture_end ();
420                 double xstart;
421                 double xend;
422
423                 switch (_trackview.track()->mode()) {
424
425                 case NonLayered:
426                 case Normal:
427                         rect.length = at - rect.start;
428                         xstart = _trackview.editor().frame_to_pixel (rect.start);
429                         xend = _trackview.editor().frame_to_pixel (at);
430                         break;
431
432                 case Destructive:
433                         rect.length = 2;
434                         xstart = _trackview.editor().frame_to_pixel (_trackview.track()->current_capture_start());
435                         xend = _trackview.editor().frame_to_pixel (at);
436                         break;
437                 }
438
439                 rect.rectangle->property_x1() = xstart;
440                 rect.rectangle->property_x2() = xend;
441         }
442 }
443
444 RegionView*
445 StreamView::find_view (boost::shared_ptr<const Region> region)
446 {
447         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
448
449                 if ((*i)->region() == region) {
450                         return *i;
451                 }
452         }
453         return 0;
454 }
455
456 uint32_t
457 StreamView::num_selected_regionviews () const
458 {
459         uint32_t cnt = 0;
460
461         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
462                 if ((*i)->get_selected()) {
463                         ++cnt;
464                 }
465         }
466         return cnt;
467 }
468
469 void
470 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
471 {
472         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
473                 slot (*i);
474         }
475 }
476
477 void
478 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
479 {
480         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
481                 if ((*i)->get_selected()) {
482                         slot (*i);
483                 }
484         }
485 }
486
487 void
488 StreamView::set_selected_regionviews (RegionSelection& regions)
489 {
490         bool selected;
491
492         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
493
494                 selected = false;
495
496                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
497                         if (*i == *ii) {
498                                 selected = true;
499                                 break;
500                         }
501                 }
502
503                 (*i)->set_selected (selected);
504         }
505 }
506
507
508 /** Get selectable things within a given range.
509  *  @param start Start time in session frames.
510  *  @param end End time in session frames.
511  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
512  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
513  *  @param result Filled in with selectable things.
514  */
515
516 void
517 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results)
518 {
519         layer_t min_layer = 0;
520         layer_t max_layer = 0;
521
522         if (_layer_display == Stacked) {
523                 double const c = child_height ();
524
525                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
526                 if (mi < 0) {
527                         min_layer = 0;
528                 } else {
529                         min_layer = mi;
530                 }
531
532                 int const ma = _layers - ((top - _trackview.y_position()) / c);
533                 if (ma > (int) _layers) {
534                         max_layer = _layers - 1;
535                 } else {
536                         max_layer = ma;
537                 }
538
539         }
540
541         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
542
543                 bool layer_ok = true;
544
545                 if (_layer_display == Stacked) {
546                         layer_t const l = (*i)->region()->layer ();
547                         layer_ok = (min_layer <= l && l <= max_layer);
548                 }
549
550                 if ((*i)->region()->coverage (start, end) != OverlapNone && layer_ok) {
551                         results.push_back (*i);
552                 }
553         }
554 }
555
556 void
557 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
558 {
559         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
560                 if (!sel.regions.contains (*i)) {
561                         results.push_back (*i);
562                 }
563         }
564 }
565
566 /** @return height of a child region view, depending on stacked / overlaid mode */
567 double
568 StreamView::child_height () const
569 {
570         switch (_layer_display) {
571         case Overlaid:
572                 return height;
573         case Stacked:
574                 return height / _layers;
575         case Expanded:
576                 return height / (_layers * 2 + 1);
577         }
578         
579         /* NOTREACHED */
580         return height;
581 }
582
583 void
584 StreamView::update_contents_height ()
585 {
586         const double h = child_height ();
587
588         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
589                 switch (_layer_display) {
590                 case Overlaid:
591                         (*i)->set_y (0);
592                         break;
593                 case Stacked:
594                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
595                         break;
596                 case Expanded:
597                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
598                         break;
599                 }
600
601                 (*i)->set_height (h);
602         }
603
604         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
605                 switch (_layer_display) {
606                 case Overlaid:
607                         i->rectangle->property_y2() = height;
608                         break;
609                 case Stacked:
610                 case Expanded:
611                         /* In stacked displays, the recregion is always at the top */
612                         i->rectangle->property_y1() = 0;
613                         i->rectangle->property_y2() = h;
614                         break;
615                 }
616         }
617 }
618
619 void
620 StreamView::set_layer_display (LayerDisplay d)
621 {
622         _layer_display = d;
623
624         if (_layer_display == Overlaid) {
625                 layer_regions ();
626         }
627         
628         update_contents_height ();
629         update_coverage_frames ();
630 }
631
632 void
633 StreamView::update_coverage_frames ()
634 {
635         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
636                 (*i)->update_coverage_frames (_layer_display);
637         }
638 }
639
640 void
641 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
642 {
643         if (_new_rec_layer_time < to) {
644                 /* The region being recorded has overlapped the start of a top-layered region, so
645                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
646                    as the proper layering will get sorted out when the recorded region is added to
647                    its playlist.
648                 */
649
650                 /* Stop this happening again */
651                 _new_rec_layer_time = max_framepos;
652
653                 /* Make space in the view for the new layer */
654                 ++_layers;
655
656                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
657                 region->set_layer (_layers - 1);
658
659                 /* and reset the view */
660                 update_contents_height ();
661         }
662 }
663
664 void
665 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
666 {
667         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
668            recorded region in.  To work out where this needs to happen, find the start of the next
669            top-layered region after the start of the region we are recording and make a note of it.
670         */
671         if (_layer_display == Stacked) {
672                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
673         } else {
674                 _new_rec_layer_time = max_framepos;
675         }
676 }
677
678 void
679 StreamView::enter_internal_edit_mode ()
680 {
681         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
682                 (*i)->hide_rect ();
683         }
684 }
685
686 void
687 StreamView::leave_internal_edit_mode ()
688 {
689         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
690                 (*i)->show_rect ();
691         }
692 }