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