Fix scrolling of labels; prevent zooming out too far.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "film_editor.h"
22 #include "timeline.h"
23 #include "timeline_time_axis_view.h"
24 #include "timeline_reels_view.h"
25 #include "timeline_labels_view.h"
26 #include "timeline_video_content_view.h"
27 #include "timeline_audio_content_view.h"
28 #include "timeline_subtitle_content_view.h"
29 #include "timeline_atmos_content_view.h"
30 #include "content_panel.h"
31 #include "wx_util.h"
32 #include "lib/film.h"
33 #include "lib/playlist.h"
34 #include "lib/image_content.h"
35 #include "lib/timer.h"
36 #include "lib/audio_content.h"
37 #include "lib/subtitle_content.h"
38 #include "lib/video_content.h"
39 #include "lib/atmos_mxf_content.h"
40 #include <wx/graphics.h>
41 #include <boost/weak_ptr.hpp>
42 #include <boost/foreach.hpp>
43 #include <list>
44 #include <iostream>
45
46 using std::list;
47 using std::cout;
48 using std::min;
49 using std::max;
50 using boost::shared_ptr;
51 using boost::weak_ptr;
52 using boost::dynamic_pointer_cast;
53 using boost::bind;
54 using boost::optional;
55
56 /* 3 hours in 640 pixels */
57 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
58 int const Timeline::_minimum_track_height = 16;
59
60 Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
61         : wxPanel (parent, wxID_ANY)
62         , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
63         , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
64         , _content_panel (cp)
65         , _film (film)
66         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
67         , _reels_view (new TimelineReelsView (*this, 32))
68         , _labels_view (new TimelineLabelsView (*this))
69         , _tracks (0)
70         , _left_down (false)
71         , _down_view_position (0)
72         , _first_move (false)
73         , _menu (this)
74         , _snap (true)
75         , _tool (SELECT)
76         , _x_scroll_rate (16)
77         , _y_scroll_rate (16)
78         , _track_height (48)
79 {
80 #ifndef __WXOSX__
81         _labels_canvas->SetDoubleBuffered (true);
82         _main_canvas->SetDoubleBuffered (true);
83 #endif
84
85         wxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
86         sizer->Add (_labels_canvas, 0, wxEXPAND);
87         _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
88         sizer->Add (_main_canvas, 1, wxEXPAND);
89         SetSizer (sizer);
90
91         _labels_canvas->Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint_labels, this));
92         _main_canvas->Bind   (wxEVT_PAINT,      boost::bind (&Timeline::paint_main,   this));
93         _main_canvas->Bind   (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,    this, _1));
94         _main_canvas->Bind   (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,      this, _1));
95         _main_canvas->Bind   (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,   this, _1));
96         _main_canvas->Bind   (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved,  this, _1));
97         _main_canvas->Bind   (wxEVT_SIZE,       boost::bind (&Timeline::resized,      this));
98         _main_canvas->Bind   (wxEVT_SCROLLWIN_THUMBTRACK,  boost::bind (&Timeline::scrolled,     this, _1));
99
100         film_changed (Film::CONTENT);
101
102         SetMinSize (wxSize (640, 4 * track_height() + 96));
103
104         _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
105         _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
106
107         _pixels_per_second = max (_minimum_pixels_per_second, static_cast<double>(640) / film->length().seconds ());
108
109         setup_scrollbars ();
110         _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
111 }
112
113 void
114 Timeline::paint_labels ()
115 {
116         wxPaintDC dc (_labels_canvas);
117
118         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
119         if (!gc) {
120                 return;
121         }
122
123         int vsx, vsy;
124         _labels_canvas->GetViewStart (&vsx, &vsy);
125         gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
126
127         _labels_view->paint (gc, list<dcpomatic::Rect<int> >());
128
129         delete gc;
130 }
131
132 void
133 Timeline::paint_main ()
134 {
135         wxPaintDC dc (_main_canvas);
136         _main_canvas->DoPrepareDC (dc);
137
138         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
139         if (!gc) {
140                 return;
141         }
142
143         int vsx, vsy;
144         _main_canvas->GetViewStart (&vsx, &vsy);
145         gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
146
147         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
148
149         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
150
151                 shared_ptr<TimelineContentView> ic = dynamic_pointer_cast<TimelineContentView> (i);
152
153                 /* Find areas of overlap with other content views, so that we can plot them */
154                 list<dcpomatic::Rect<int> > overlaps;
155                 BOOST_FOREACH (shared_ptr<TimelineView> j, _views) {
156                         shared_ptr<TimelineContentView> jc = dynamic_pointer_cast<TimelineContentView> (j);
157                         /* No overlap with non-content views, views no different tracks, audio views or non-active views */
158                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
159                                 continue;
160                         }
161
162                         optional<dcpomatic::Rect<int> > r = j->bbox().intersection (i->bbox());
163                         if (r) {
164                                 overlaps.push_back (r.get ());
165                         }
166                 }
167
168                 i->paint (gc, overlaps);
169         }
170
171         if (_zoom_point) {
172                 /* Translate back as _down_point and _zoom_point do not take scroll into account */
173                 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
174                 gc->SetPen (*wxBLACK_PEN);
175                 gc->SetBrush (*wxTRANSPARENT_BRUSH);
176                 gc->DrawRectangle (
177                         min (_down_point.x, _zoom_point->x),
178                         min (_down_point.y, _zoom_point->y),
179                         fabs (_down_point.x - _zoom_point->x),
180                         fabs (_down_point.y - _zoom_point->y)
181                         );
182         }
183
184         delete gc;
185 }
186
187 void
188 Timeline::film_changed (Film::Property p)
189 {
190         if (p == Film::CONTENT || p == Film::REEL_TYPE || p == Film::REEL_LENGTH) {
191                 ensure_ui_thread ();
192                 recreate_views ();
193         } else if (p == Film::CONTENT_ORDER) {
194                 Refresh ();
195         }
196 }
197
198 void
199 Timeline::recreate_views ()
200 {
201         shared_ptr<const Film> film = _film.lock ();
202         if (!film) {
203                 return;
204         }
205
206         _views.clear ();
207         _views.push_back (_time_axis_view);
208         _views.push_back (_reels_view);
209
210         BOOST_FOREACH (shared_ptr<Content> i, film->content ()) {
211                 if (i->video) {
212                         _views.push_back (shared_ptr<TimelineView> (new TimelineVideoContentView (*this, i)));
213                 }
214
215                 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
216                         _views.push_back (shared_ptr<TimelineView> (new TimelineAudioContentView (*this, i)));
217                 }
218
219                 if (i->subtitle) {
220                         _views.push_back (shared_ptr<TimelineView> (new TimelineSubtitleContentView (*this, i)));
221                 }
222
223                 if (dynamic_pointer_cast<AtmosMXFContent> (i)) {
224                         _views.push_back (shared_ptr<TimelineView> (new TimelineAtmosContentView (*this, i)));
225                 }
226         }
227
228         assign_tracks ();
229         setup_scrollbars ();
230         Refresh ();
231 }
232
233 void
234 Timeline::film_content_changed (int property, bool frequent)
235 {
236         ensure_ui_thread ();
237
238         if (property == AudioContentProperty::STREAMS) {
239                 recreate_views ();
240         } else if (!frequent) {
241                 setup_scrollbars ();
242                 Refresh ();
243         }
244 }
245
246 template <class T>
247 int
248 place (TimelineViewList& views, int& tracks)
249 {
250         int const base = tracks;
251
252         BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
253                 if (!dynamic_pointer_cast<T>(i)) {
254                         continue;
255                 }
256
257                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
258
259                 int t = base;
260
261                 shared_ptr<Content> content = cv->content();
262                 DCPTimePeriod const content_period (content->position(), content->end());
263
264                 while (true) {
265                         TimelineViewList::iterator j = views.begin();
266                         while (j != views.end()) {
267                                 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
268                                 if (!test) {
269                                         ++j;
270                                         continue;
271                                 }
272
273                                 shared_ptr<Content> test_content = test->content();
274                                 if (
275                                         test->track() && test->track().get() == t &&
276                                         content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
277                                         /* we have an overlap on track `t' */
278                                         ++t;
279                                         break;
280                                 }
281
282                                 ++j;
283                         }
284
285                         if (j == views.end ()) {
286                                 /* no overlap on `t' */
287                                 break;
288                         }
289                 }
290
291                 cv->set_track (t);
292                 tracks = max (tracks, t + 1);
293         }
294
295         return tracks - base;
296 }
297
298 void
299 Timeline::assign_tracks ()
300 {
301         /* Tracks are:
302            Video (mono or left-eye)
303            Video (right-eye)
304            Subtitle 1
305            Subtitle 2
306            Subtitle N
307            Atmos
308            Audio 1
309            Audio 2
310            Audio N
311         */
312
313         _tracks = 0;
314
315         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
316                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
317                 if (c) {
318                         c->unset_track ();
319                 }
320         }
321
322         /* Video */
323
324         bool have_3d = false;
325         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
326                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
327                 if (!cv) {
328                         continue;
329                 }
330
331                 /* Video on tracks 0 and maybe 1 (left and right eye) */
332                 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
333                         cv->set_track (1);
334                         _tracks = max (_tracks, 2);
335                         have_3d = true;
336                 } else {
337                         cv->set_track (0);
338                 }
339         }
340
341         _tracks = max (_tracks, 1);
342
343         /* Subtitle */
344
345         int const subtitle_tracks = place<TimelineSubtitleContentView> (_views, _tracks);
346
347         /* Atmos */
348
349         bool have_atmos = false;
350         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
351                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
352                 if (!cv) {
353                         continue;
354                 }
355                 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
356                         cv->set_track (_tracks - 1);
357                         have_atmos = true;
358                 }
359         }
360
361         if (have_atmos) {
362                 ++_tracks;
363         }
364
365         /* Audio */
366
367         place<TimelineAudioContentView> (_views, _tracks);
368
369         _labels_view->set_3d (have_3d);
370         _labels_view->set_subtitle_tracks (subtitle_tracks);
371         _labels_view->set_atmos (have_atmos);
372
373         _time_axis_view->set_y (tracks());
374         _reels_view->set_y (8);
375 }
376
377 int
378 Timeline::tracks () const
379 {
380         return _tracks;
381 }
382
383 void
384 Timeline::setup_scrollbars ()
385 {
386         shared_ptr<const Film> film = _film.lock ();
387         if (!film || !_pixels_per_second) {
388                 return;
389         }
390         _labels_canvas->SetVirtualSize (_labels_view->bbox().width, tracks() * track_height() + 96);
391         _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
392         _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * track_height() + 96);
393         _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
394 }
395
396 shared_ptr<TimelineView>
397 Timeline::event_to_view (wxMouseEvent& ev)
398 {
399         /* Search backwards through views so that we find the uppermost one first */
400         TimelineViewList::reverse_iterator i = _views.rbegin();
401         Position<int> const p (ev.GetX(), ev.GetY());
402         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
403                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
404                 ++i;
405         }
406
407         if (i == _views.rend ()) {
408                 return shared_ptr<TimelineView> ();
409         }
410
411         return *i;
412 }
413
414 void
415 Timeline::left_down (wxMouseEvent& ev)
416 {
417         _left_down = true;
418         _down_point = ev.GetPosition ();
419
420         switch (_tool) {
421         case SELECT:
422                 left_down_select (ev);
423                 break;
424         case ZOOM:
425                 /* Nothing to do */
426                 break;
427         }
428 }
429
430 void
431 Timeline::left_down_select (wxMouseEvent& ev)
432 {
433         shared_ptr<TimelineView> view = event_to_view (ev);
434         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
435
436         _down_view.reset ();
437
438         if (content_view) {
439                 _down_view = content_view;
440                 _down_view_position = content_view->content()->position ();
441         }
442
443         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
444                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
445                 if (!cv) {
446                         continue;
447                 }
448
449                 if (!ev.ShiftDown ()) {
450                         cv->set_selected (view == *i);
451                 }
452         }
453
454         if (content_view && ev.ShiftDown ()) {
455                 content_view->set_selected (!content_view->selected ());
456         }
457
458         _first_move = false;
459
460         if (_down_view) {
461                 /* Pre-compute the points that we might snap to */
462                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
463                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
464                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
465                                 continue;
466                         }
467
468                         _start_snaps.push_back (cv->content()->position());
469                         _end_snaps.push_back (cv->content()->position());
470                         _start_snaps.push_back (cv->content()->end());
471                         _end_snaps.push_back (cv->content()->end());
472
473                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
474                                 _start_snaps.push_back (i);
475                         }
476                 }
477
478                 /* Tell everyone that things might change frequently during the drag */
479                 _down_view->content()->set_change_signals_frequent (true);
480         }
481 }
482
483 void
484 Timeline::left_up (wxMouseEvent& ev)
485 {
486         _left_down = false;
487
488         switch (_tool) {
489         case SELECT:
490                 left_up_select (ev);
491                 break;
492         case ZOOM:
493                 left_up_zoom (ev);
494                 break;
495         }
496 }
497
498 void
499 Timeline::left_up_select (wxMouseEvent& ev)
500 {
501         if (_down_view) {
502                 _down_view->content()->set_change_signals_frequent (false);
503         }
504
505         _content_panel->set_selection (selected_content ());
506         set_position_from_event (ev);
507
508         /* Clear up up the stuff we don't do during drag */
509         assign_tracks ();
510         setup_scrollbars ();
511         Refresh ();
512
513         _start_snaps.clear ();
514         _end_snaps.clear ();
515 }
516
517 void
518 Timeline::left_up_zoom (wxMouseEvent& ev)
519 {
520         _zoom_point = ev.GetPosition ();
521
522         int vsx, vsy;
523         _main_canvas->GetViewStart (&vsx, &vsy);
524
525         wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
526         wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
527
528         if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
529                 /* Very small zoom rectangle: we assume it wasn't intentional */
530                 return;
531         }
532
533         DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
534         DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
535         _pixels_per_second = max (_minimum_pixels_per_second, double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
536
537         double const tracks_top = double(top_left.y) / _track_height;
538         double const tracks_bottom = double(bottom_right.y) / _track_height;
539         _track_height = max(_minimum_track_height, int(lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top))));
540
541         setup_scrollbars ();
542         _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _track_height / _y_scroll_rate);
543         _labels_canvas->Scroll (0, tracks_top * _track_height / _y_scroll_rate);
544
545         _zoom_point = optional<wxPoint> ();
546         Refresh ();
547 }
548
549 void
550 Timeline::mouse_moved (wxMouseEvent& ev)
551 {
552         switch (_tool) {
553         case SELECT:
554                 mouse_moved_select (ev);
555                 break;
556         case ZOOM:
557                 mouse_moved_zoom (ev);
558                 break;
559         }
560 }
561
562 void
563 Timeline::mouse_moved_select (wxMouseEvent& ev)
564 {
565         if (!_left_down) {
566                 return;
567         }
568
569         set_position_from_event (ev);
570 }
571
572 void
573 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
574 {
575         if (!_left_down) {
576                 return;
577         }
578
579         _zoom_point = ev.GetPosition ();
580         Refresh ();
581 }
582
583 void
584 Timeline::right_down (wxMouseEvent& ev)
585 {
586         switch (_tool) {
587         case SELECT:
588                 right_down_select (ev);
589                 break;
590         case ZOOM:
591                 /* Zoom out */
592                 _pixels_per_second = max (_minimum_pixels_per_second, *_pixels_per_second / 2);
593                 _track_height = max (_minimum_track_height, _track_height / 2);
594                 setup_scrollbars ();
595                 Refresh ();
596                 break;
597         }
598 }
599
600 void
601 Timeline::right_down_select (wxMouseEvent& ev)
602 {
603         shared_ptr<TimelineView> view = event_to_view (ev);
604         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
605         if (!cv) {
606                 return;
607         }
608
609         if (!cv->selected ()) {
610                 clear_selection ();
611                 cv->set_selected (true);
612         }
613
614         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
615 }
616
617 void
618 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
619 {
620         DCPTime const d = a - b;
621         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
622                 nearest_distance = d;
623         }
624 }
625
626 void
627 Timeline::set_position_from_event (wxMouseEvent& ev)
628 {
629         if (!_pixels_per_second) {
630                 return;
631         }
632
633         double const pps = _pixels_per_second.get ();
634
635         wxPoint const p = ev.GetPosition();
636
637         if (!_first_move) {
638                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
639                    before the drag is considered to have started.
640                 */
641                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
642                 if (dist < 8) {
643                         return;
644                 }
645                 _first_move = true;
646         }
647
648         if (!_down_view) {
649                 return;
650         }
651
652         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
653
654         if (_snap) {
655
656                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
657                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
658                    positive is right).
659                 */
660                 optional<DCPTime> nearest_distance;
661
662                 /* Find the nearest snap point */
663
664                 BOOST_FOREACH (DCPTime i, _start_snaps) {
665                         maybe_snap (i, new_position, nearest_distance);
666                 }
667
668                 BOOST_FOREACH (DCPTime i, _end_snaps) {
669                         maybe_snap (i, new_end, nearest_distance);
670                 }
671
672                 if (nearest_distance) {
673                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
674                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
675                                 new_position += nearest_distance.get ();
676                         }
677                 }
678         }
679
680         if (new_position < DCPTime ()) {
681                 new_position = DCPTime ();
682         }
683
684         _down_view->content()->set_position (new_position);
685
686         shared_ptr<Film> film = _film.lock ();
687         DCPOMATIC_ASSERT (film);
688         film->set_sequence (false);
689 }
690
691 void
692 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
693 {
694         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
695 }
696
697 shared_ptr<const Film>
698 Timeline::film () const
699 {
700         return _film.lock ();
701 }
702
703 void
704 Timeline::resized ()
705 {
706         setup_scrollbars ();
707 }
708
709 void
710 Timeline::clear_selection ()
711 {
712         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
713                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
714                 if (cv) {
715                         cv->set_selected (false);
716                 }
717         }
718 }
719
720 TimelineContentViewList
721 Timeline::selected_views () const
722 {
723         TimelineContentViewList sel;
724
725         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
726                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
727                 if (cv && cv->selected()) {
728                         sel.push_back (cv);
729                 }
730         }
731
732         return sel;
733 }
734
735 ContentList
736 Timeline::selected_content () const
737 {
738         ContentList sel;
739         TimelineContentViewList views = selected_views ();
740
741         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
742                 sel.push_back ((*i)->content ());
743         }
744
745         return sel;
746 }
747
748 void
749 Timeline::set_selection (ContentList selection)
750 {
751         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
752                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
753                 if (cv) {
754                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
755                 }
756         }
757 }
758
759 int
760 Timeline::tracks_y_offset () const
761 {
762         return _reels_view->bbox().height + 4;
763 }
764
765 int
766 Timeline::width () const
767 {
768         return _main_canvas->GetVirtualSize().GetWidth();
769 }
770
771 void
772 Timeline::scrolled (wxScrollWinEvent& ev)
773 {
774         if (ev.GetOrientation() == wxVERTICAL) {
775                 _labels_canvas->Scroll (0, ev.GetPosition ());
776         }
777         ev.Skip ();
778 }