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