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