Merge remote-tracking branch 'remotes/origin/exportvis' into windows+cc
[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 "global_signals.h"
38 #include "region_view.h"
39 #include "route_time_axis.h"
40 #include "region_selection.h"
41 #include "selection.h"
42 #include "public_editor.h"
43 #include "ardour_ui.h"
44 #include "rgb_macros.h"
45 #include "gui_thread.h"
46 #include "utils.h"
47
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace Editing;
52
53 StreamView::StreamView (RouteTimeAxisView& tv)
54         : _trackview (tv)
55         , _canvas_group (new ArdourCanvas::Group (_trackview.canvas_display()))
56         , _samples_per_pixel (_trackview.editor().get_current_zoom ())
57         , rec_updating(false)
58         , rec_active(false)
59         , stream_base_color(0xFFFFFFFF)
60         , _layers (1)
61         , _layer_display (Overlaid)
62         , height(tv.height)
63         , last_rec_data_frame(0)
64 {
65         CANVAS_DEBUG_NAME (_canvas_group, string_compose ("SV canvas group %1", _trackview.name()));
66         
67         /* set_position() will position the group */
68
69         canvas_rect = new ArdourCanvas::Rectangle (_canvas_group);
70         CANVAS_DEBUG_NAME (canvas_rect, string_compose ("SV canvas rectangle %1", _trackview.name()));
71         canvas_rect->set (ArdourCanvas::Rect (0, 0, ArdourCanvas::COORD_MAX, tv.current_height ()));
72         canvas_rect->raise(1); // raise above tempo lines
73
74         canvas_rect->set_outline_what (ArdourCanvas::Rectangle::What (ArdourCanvas::Rectangle::TOP | ArdourCanvas::Rectangle::BOTTOM));
75         canvas_rect->set_outline_color (RGBA_TO_UINT (0, 0, 0, 255));
76         canvas_rect->set_fill_color (RGBA_TO_UINT (1.0, 0, 0, 255));
77         canvas_rect->set_fill (true);
78
79         canvas_rect->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
102 void
103 StreamView::attach ()
104 {
105         if (_trackview.is_track()) {
106                 display_track (_trackview.track ());
107         }
108 }
109
110 int
111 StreamView::set_position (gdouble x, gdouble y)
112 {
113         _canvas_group->set_position (ArdourCanvas::Duple (x, y));
114         return 0;
115 }
116
117 int
118 StreamView::set_height (double h)
119 {
120         /* limit the values to something sane-ish */
121         if (h < 10.0 || h > 1000.0) {
122                 return -1;
123         }
124
125         if (canvas_rect->y1() == h) {
126                 return 0;
127         }
128
129         height = h;
130         canvas_rect->set_y1 (height);
131         update_contents_height ();
132
133         return 0;
134 }
135
136 int
137 StreamView::set_samples_per_pixel (double fpp)
138 {
139         RegionViewList::iterator i;
140
141         if (fpp < 1.0) {
142                 return -1;
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_frames ();
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                 update_coverage_frames ();
299         } else {
300                 /* layering has probably been modified. reflect this in the canvas. */
301                 layer_regions();
302         }
303 }
304
305 void
306 StreamView::playlist_switched (boost::weak_ptr<Track> wtr)
307 {
308         boost::shared_ptr<Track> tr (wtr.lock());
309
310         if (!tr) {
311                 return;
312         }
313
314         /* disconnect from old playlist */
315
316         playlist_connections.drop_connections ();
317         undisplay_track ();
318
319         /* draw it */
320
321         redisplay_track ();
322
323         /* update layers count and the y positions and heights of our regions */
324         _layers = tr->playlist()->top_layer() + 1;
325         update_contents_height ();
326         update_coverage_frames ();
327
328         /* catch changes */
329
330         tr->playlist()->LayeringChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::playlist_layered, this, boost::weak_ptr<Track> (tr)), gui_context());
331         tr->playlist()->RegionAdded.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::add_region_view, this, _1), gui_context());
332         tr->playlist()->RegionRemoved.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::remove_region_view, this, _1), gui_context());
333         tr->playlist()->ContentsChanged.connect (playlist_connections, invalidator (*this), boost::bind (&StreamView::update_coverage_frames, this), gui_context());
334 }
335
336
337 void
338 StreamView::diskstream_changed ()
339 {
340         boost::shared_ptr<Track> t;
341
342         if ((t = _trackview.track()) != 0) {
343                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::display_track, this, t));
344         } else {
345                 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::undisplay_track, this));
346         }
347 }
348
349 void
350 StreamView::apply_color (Gdk::Color color, ColorTarget target)
351 {
352         list<RegionView *>::iterator i;
353
354         switch (target) {
355         case RegionColor:
356                 region_color = color;
357                 for (i = region_views.begin(); i != region_views.end(); ++i) {
358                         (*i)->set_color (region_color);
359                 }
360                 break;
361
362         case StreamBaseColor:
363                 stream_base_color = RGBA_TO_UINT (color.get_red_p(), color.get_green_p(), color.get_blue_p(), 255);
364                 canvas_rect->set_fill_color (stream_base_color);
365                 break;
366         }
367 }
368
369 void
370 StreamView::region_layered (RegionView* rv)
371 {
372         /* don't ever leave it at the bottom, since then it doesn't
373            get events - the  parent group does instead ...
374         */
375         rv->get_canvas_group()->raise (rv->region()->layer());
376 }
377
378 void
379 StreamView::rec_enable_changed ()
380 {
381         setup_rec_box ();
382 }
383
384 void
385 StreamView::sess_rec_enable_changed ()
386 {
387         setup_rec_box ();
388 }
389
390 void
391 StreamView::transport_changed()
392 {
393         setup_rec_box ();
394 }
395
396 void
397 StreamView::transport_looped()
398 {
399         // to force a new rec region
400         rec_active = false;
401         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&StreamView::setup_rec_box, this));
402 }
403
404 void
405 StreamView::update_rec_box ()
406 {
407         if (rec_active && rec_rects.size() > 0) {
408                 /* only update the last box */
409                 RecBoxInfo & rect = rec_rects.back();
410                 framepos_t const at = _trackview.track()->current_capture_end ();
411                 double xstart;
412                 double xend;
413
414                 switch (_trackview.track()->mode()) {
415
416                 case NonLayered:
417                 case Normal:
418                         rect.length = at - rect.start;
419                         xstart = _trackview.editor().sample_to_pixel (rect.start);
420                         xend = _trackview.editor().sample_to_pixel (at);
421                         break;
422
423                 case Destructive:
424                         rect.length = 2;
425                         xstart = _trackview.editor().sample_to_pixel (_trackview.track()->current_capture_start());
426                         xend = _trackview.editor().sample_to_pixel (at);
427                         break;
428                 }
429
430                 rect.rectangle->set_x0 (xstart);
431                 rect.rectangle->set_x1 (xend);
432         }
433 }
434
435 RegionView*
436 StreamView::find_view (boost::shared_ptr<const Region> region)
437 {
438         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
439
440                 if ((*i)->region() == region) {
441                         return *i;
442                 }
443         }
444         return 0;
445 }
446
447 uint32_t
448 StreamView::num_selected_regionviews () const
449 {
450         uint32_t cnt = 0;
451
452         for (list<RegionView*>::const_iterator i = region_views.begin(); i != region_views.end(); ++i) {
453                 if ((*i)->get_selected()) {
454                         ++cnt;
455                 }
456         }
457         return cnt;
458 }
459
460 void
461 StreamView::foreach_regionview (sigc::slot<void,RegionView*> slot)
462 {
463         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
464                 slot (*i);
465         }
466 }
467
468 void
469 StreamView::foreach_selected_regionview (sigc::slot<void,RegionView*> slot)
470 {
471         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
472                 if ((*i)->get_selected()) {
473                         slot (*i);
474                 }
475         }
476 }
477
478 void
479 StreamView::set_selected_regionviews (RegionSelection& regions)
480 {
481         bool selected;
482
483         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
484
485                 selected = false;
486
487                 for (RegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
488                         if (*i == *ii) {
489                                 selected = true;
490                                 break;
491                         }
492                 }
493
494                 (*i)->set_selected (selected);
495         }
496 }
497
498
499 /** Get selectable things within a given range.
500  *  @param start Start time in session frames.
501  *  @param end End time in session frames.
502  *  @param top Top y range, in trackview coordinates (ie 0 is the top of the track view)
503  *  @param bot Bottom y range, in trackview coordinates (ie 0 is the top of the track view)
504  *  @param result Filled in with selectable things.
505  */
506
507 void
508 StreamView::get_selectables (framepos_t start, framepos_t end, double top, double bottom, list<Selectable*>& results)
509 {
510         layer_t min_layer = 0;
511         layer_t max_layer = 0;
512
513         if (_layer_display == Stacked) {
514                 double const c = child_height ();
515
516                 int const mi = _layers - ((bottom - _trackview.y_position()) / c);
517                 if (mi < 0) {
518                         min_layer = 0;
519                 } else {
520                         min_layer = mi;
521                 }
522
523                 int const ma = _layers - ((top - _trackview.y_position()) / c);
524                 if (ma > (int) _layers) {
525                         max_layer = _layers - 1;
526                 } else {
527                         max_layer = ma;
528                 }
529
530         }
531
532         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
533
534                 bool layer_ok = true;
535
536                 if (_layer_display == Stacked) {
537                         layer_t const l = (*i)->region()->layer ();
538                         layer_ok = (min_layer <= l && l <= max_layer);
539                 }
540
541                 if ((*i)->region()->coverage (start, end) != Evoral::OverlapNone && layer_ok) {
542                         results.push_back (*i);
543                 }
544         }
545 }
546
547 void
548 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
549 {
550         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
551                 if (!sel.regions.contains (*i)) {
552                         results.push_back (*i);
553                 }
554         }
555 }
556
557 /** @return height of a child region view, depending on stacked / overlaid mode */
558 double
559 StreamView::child_height () const
560 {
561         switch (_layer_display) {
562         case Overlaid:
563                 return height;
564         case Stacked:
565                 return height / _layers;
566         case Expanded:
567                 return height / (_layers * 2 + 1);
568         }
569         
570         /* NOTREACHED */
571         return height;
572 }
573
574 void
575 StreamView::update_contents_height ()
576 {
577         const double h = child_height ();
578
579         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
580                 switch (_layer_display) {
581                 case Overlaid:
582                         (*i)->set_y (0);
583                         break;
584                 case Stacked:
585                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * h);
586                         break;
587                 case Expanded:
588                         (*i)->set_y (height - ((*i)->region()->layer() + 1) * 2 * h);
589                         break;
590                 }
591
592                 (*i)->set_height (h);
593         }
594
595         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
596                 switch (_layer_display) {
597                 case Overlaid:
598                         i->rectangle->set_y1 (height);
599                         break;
600                 case Stacked:
601                 case Expanded:
602                         /* In stacked displays, the recregion is always at the top */
603                         i->rectangle->set_y0 (0);
604                         i->rectangle->set_y1 (h);
605                         break;
606                 }
607         }
608
609         ContentsHeightChanged (); /* EMIT SIGNAL */
610 }
611
612 void
613 StreamView::set_layer_display (LayerDisplay d)
614 {
615         _layer_display = d;
616
617         if (_layer_display == Overlaid) {
618                 layer_regions ();
619         }
620         
621         update_contents_height ();
622         update_coverage_frames ();
623 }
624
625 void
626 StreamView::update_coverage_frames ()
627 {
628         for (RegionViewList::iterator i = region_views.begin (); i != region_views.end (); ++i) {
629                 (*i)->update_coverage_frames (_layer_display);
630         }
631 }
632
633 void
634 StreamView::check_record_layers (boost::shared_ptr<Region> region, framepos_t to)
635 {
636         if (_new_rec_layer_time < to) {
637                 /* The region being recorded has overlapped the start of a top-layered region, so
638                    `fake' a new visual layer for the recording.  This is only a visual thing for now,
639                    as the proper layering will get sorted out when the recorded region is added to
640                    its playlist.
641                 */
642
643                 /* Stop this happening again */
644                 _new_rec_layer_time = max_framepos;
645
646                 /* Make space in the view for the new layer */
647                 ++_layers;
648
649                 /* Set the temporary region to the correct layer so that it gets drawn correctly */
650                 region->set_layer (_layers - 1);
651
652                 /* and reset the view */
653                 update_contents_height ();
654         }
655 }
656
657 void
658 StreamView::setup_new_rec_layer_time (boost::shared_ptr<Region> region)
659 {
660         /* If we are in Stacked mode, we may need to (visually) create a new layer to put the
661            recorded region in.  To work out where this needs to happen, find the start of the next
662            top-layered region after the start of the region we are recording and make a note of it.
663         */
664         if (_layer_display == Stacked) {
665                 _new_rec_layer_time = _trackview.track()->playlist()->find_next_top_layer_position (region->start());
666         } else {
667                 _new_rec_layer_time = max_framepos;
668         }
669 }
670
671 void
672 StreamView::enter_internal_edit_mode ()
673 {
674         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
675                 (*i)->hide_rect ();
676         }
677 }
678
679 void
680 StreamView::leave_internal_edit_mode ()
681 {
682         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
683                 (*i)->show_rect ();
684         }
685 }