Make the timeline look better when there is no content in the film.
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013-2021 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 "content_panel.h"
22 #include "film_editor.h"
23 #include "film_viewer.h"
24 #include "timeline.h"
25 #include "timeline_atmos_content_view.h"
26 #include "timeline_audio_content_view.h"
27 #include "timeline_labels_view.h"
28 #include "timeline_reels_view.h"
29 #include "timeline_text_content_view.h"
30 #include "timeline_time_axis_view.h"
31 #include "timeline_video_content_view.h"
32 #include "wx_util.h"
33 #include "lib/atmos_mxf_content.h"
34 #include "lib/audio_content.h"
35 #include "lib/film.h"
36 #include "lib/image_content.h"
37 #include "lib/playlist.h"
38 #include "lib/scope_guard.h"
39 #include "lib/text_content.h"
40 #include "lib/timer.h"
41 #include "lib/video_content.h"
42 #include <dcp/warnings.h>
43 LIBDCP_DISABLE_WARNINGS
44 #include <wx/graphics.h>
45 LIBDCP_ENABLE_WARNINGS
46 #include <iterator>
47 #include <list>
48
49
50 using std::abs;
51 using std::dynamic_pointer_cast;
52 using std::list;
53 using std::make_shared;
54 using std::max;
55 using std::min;
56 using std::shared_ptr;
57 using std::weak_ptr;
58 using boost::bind;
59 using boost::optional;
60 using namespace dcpomatic;
61 #if BOOST_VERSION >= 106100
62 using namespace boost::placeholders;
63 #endif
64
65
66 /* 3 hours in 640 pixels */
67 double const Timeline::_minimum_pixels_per_second = 640.0 / (60 * 60 * 3);
68 int const Timeline::_minimum_pixels_per_track = 16;
69
70
71 Timeline::Timeline(wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film, FilmViewer& viewer)
72         : wxPanel (parent, wxID_ANY)
73         , _labels_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
74         , _main_canvas (new wxScrolledCanvas (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE))
75         , _content_panel (cp)
76         , _film (film)
77         , _viewer (viewer)
78         , _time_axis_view (new TimelineTimeAxisView (*this, 64))
79         , _reels_view (new TimelineReelsView (*this, 32))
80         , _labels_view (new TimelineLabelsView (*this))
81         , _tracks (0)
82         , _left_down (false)
83         , _down_view_position (0)
84         , _first_move (false)
85         , _menu (this, viewer)
86         , _snap (true)
87         , _tool (SELECT)
88         , _x_scroll_rate (16)
89         , _y_scroll_rate (16)
90         , _pixels_per_track (48)
91         , _first_resize (true)
92         , _timer (this)
93 {
94 #ifndef __WXOSX__
95         _labels_canvas->SetDoubleBuffered (true);
96         _main_canvas->SetDoubleBuffered (true);
97 #endif
98
99         auto sizer = new wxBoxSizer (wxHORIZONTAL);
100         sizer->Add (_labels_canvas, 0, wxEXPAND);
101         _labels_canvas->SetMinSize (wxSize (_labels_view->bbox().width, -1));
102         sizer->Add (_main_canvas, 1, wxEXPAND);
103         SetSizer (sizer);
104
105         _labels_canvas->Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint_labels, this));
106         _main_canvas->Bind   (wxEVT_PAINT,      boost::bind (&Timeline::paint_main,   this));
107         _main_canvas->Bind   (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,    this, _1));
108         _main_canvas->Bind   (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,      this, _1));
109         _main_canvas->Bind   (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,   this, _1));
110         _main_canvas->Bind   (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved,  this, _1));
111         _main_canvas->Bind   (wxEVT_SIZE,       boost::bind (&Timeline::resized,      this));
112         _main_canvas->Bind   (wxEVT_SCROLLWIN_TOP,        boost::bind (&Timeline::scrolled,     this, _1));
113         _main_canvas->Bind   (wxEVT_SCROLLWIN_BOTTOM,     boost::bind (&Timeline::scrolled,     this, _1));
114         _main_canvas->Bind   (wxEVT_SCROLLWIN_LINEUP,     boost::bind (&Timeline::scrolled,     this, _1));
115         _main_canvas->Bind   (wxEVT_SCROLLWIN_LINEDOWN,   boost::bind (&Timeline::scrolled,     this, _1));
116         _main_canvas->Bind   (wxEVT_SCROLLWIN_PAGEUP,     boost::bind (&Timeline::scrolled,     this, _1));
117         _main_canvas->Bind   (wxEVT_SCROLLWIN_PAGEDOWN,   boost::bind (&Timeline::scrolled,     this, _1));
118         _main_canvas->Bind   (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled,     this, _1));
119
120         film_change (ChangeType::DONE, Film::Property::CONTENT);
121
122         SetMinSize (wxSize (640, 4 * pixels_per_track() + 96));
123
124         _film_changed_connection = film->Change.connect (bind (&Timeline::film_change, this, _1, _2));
125         _film_content_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
126
127         Bind (wxEVT_TIMER, boost::bind(&Timeline::update_playhead, this));
128         _timer.Start (200, wxTIMER_CONTINUOUS);
129
130         setup_scrollbars ();
131         _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
132 }
133
134
135 void
136 Timeline::update_playhead ()
137 {
138         Refresh ();
139 }
140
141
142 void
143 Timeline::set_pixels_per_second (double pps)
144 {
145         _pixels_per_second = max (_minimum_pixels_per_second, pps);
146 }
147
148
149 void
150 Timeline::paint_labels ()
151 {
152         wxPaintDC dc (_labels_canvas);
153
154         auto film = _film.lock();
155         if (film->content().empty()) {
156                 return;
157         }
158
159         auto gc = wxGraphicsContext::Create (dc);
160         if (!gc) {
161                 return;
162         }
163
164         ScopeGuard sg = [gc]() { delete gc; };
165
166         int vsx, vsy;
167         _labels_canvas->GetViewStart (&vsx, &vsy);
168         gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate + tracks_y_offset());
169
170         _labels_view->paint (gc, {});
171 }
172
173
174 void
175 Timeline::paint_main ()
176 {
177         wxPaintDC dc (_main_canvas);
178
179         auto film = _film.lock();
180         if (film->content().empty()) {
181                 return;
182         }
183
184         _main_canvas->DoPrepareDC (dc);
185
186         auto gc = wxGraphicsContext::Create (dc);
187         if (!gc) {
188                 return;
189         }
190
191         ScopeGuard sg = [gc]() { delete gc; };
192
193         int vsx, vsy;
194         _main_canvas->GetViewStart (&vsx, &vsy);
195         gc->Translate (-vsx * _x_scroll_rate, -vsy * _y_scroll_rate);
196
197         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
198
199         for (auto i: _views) {
200
201                 auto ic = dynamic_pointer_cast<TimelineContentView> (i);
202
203                 /* Find areas of overlap with other content views, so that we can plot them */
204                 list<dcpomatic::Rect<int>> overlaps;
205                 for (auto j: _views) {
206                         auto jc = dynamic_pointer_cast<TimelineContentView> (j);
207                         /* No overlap with non-content views, views on different tracks, audio views or non-active views */
208                         if (!ic || !jc || i == j || ic->track() != jc->track() || ic->track().get_value_or(2) >= 2 || !ic->active() || !jc->active()) {
209                                 continue;
210                         }
211
212                         auto r = j->bbox().intersection(i->bbox());
213                         if (r) {
214                                 overlaps.push_back (r.get ());
215                         }
216                 }
217
218                 i->paint (gc, overlaps);
219         }
220
221         if (_zoom_point) {
222                 /* Translate back as _down_point and _zoom_point do not take scroll into account */
223                 gc->Translate (vsx * _x_scroll_rate, vsy * _y_scroll_rate);
224                 gc->SetPen(gui_is_dark() ? *wxWHITE_PEN : *wxBLACK_PEN);
225                 gc->SetBrush (*wxTRANSPARENT_BRUSH);
226                 gc->DrawRectangle (
227                         min (_down_point.x, _zoom_point->x),
228                         min (_down_point.y, _zoom_point->y),
229                         abs (_down_point.x - _zoom_point->x),
230                         abs (_down_point.y - _zoom_point->y)
231                         );
232         }
233
234         /* Playhead */
235
236         gc->SetPen (*wxRED_PEN);
237         auto path = gc->CreatePath ();
238         double const ph = _viewer.position().seconds() * pixels_per_second().get_value_or(0);
239         path.MoveToPoint (ph, 0);
240         path.AddLineToPoint (ph, pixels_per_track() * _tracks + 32);
241         gc->StrokePath (path);
242 }
243
244
245 void
246 Timeline::film_change (ChangeType type, Film::Property p)
247 {
248         if (type != ChangeType::DONE) {
249                 return;
250         }
251
252         if (p == Film::Property::CONTENT || p == Film::Property::REEL_TYPE || p == Film::Property::REEL_LENGTH) {
253                 ensure_ui_thread ();
254                 recreate_views ();
255         } else if (p == Film::Property::CONTENT_ORDER) {
256                 Refresh ();
257         }
258 }
259
260
261 void
262 Timeline::recreate_views ()
263 {
264         auto film = _film.lock ();
265         if (!film) {
266                 return;
267         }
268
269         _views.clear ();
270         _views.push_back (_time_axis_view);
271         _views.push_back (_reels_view);
272
273         for (auto i: film->content ()) {
274                 if (i->video) {
275                         _views.push_back (make_shared<TimelineVideoContentView>(*this, i));
276                 }
277
278                 if (i->audio && !i->audio->mapping().mapped_output_channels().empty ()) {
279                         _views.push_back (make_shared<TimelineAudioContentView>(*this, i));
280                 }
281
282                 for (auto j: i->text) {
283                         _views.push_back (make_shared<TimelineTextContentView>(*this, i, j));
284                 }
285
286                 if (i->atmos) {
287                         _views.push_back (make_shared<TimelineAtmosContentView>(*this, i));
288                 }
289         }
290
291         assign_tracks ();
292         setup_scrollbars ();
293         Refresh ();
294 }
295
296
297 void
298 Timeline::film_content_change (ChangeType type, int property, bool frequent)
299 {
300         if (type != ChangeType::DONE) {
301                 return;
302         }
303
304         ensure_ui_thread ();
305
306         if (property == AudioContentProperty::STREAMS || property == VideoContentProperty::FRAME_TYPE) {
307                 recreate_views ();
308         } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
309                 _reels_view->force_redraw ();
310         } else if (!frequent) {
311                 setup_scrollbars ();
312                 Refresh ();
313         }
314 }
315
316
317 template <class T>
318 int
319 place (shared_ptr<const Film> film, TimelineViewList& views, int& tracks)
320 {
321         int const base = tracks;
322
323         for (auto i: views) {
324                 if (!dynamic_pointer_cast<T>(i)) {
325                         continue;
326                 }
327
328                 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
329
330                 int t = base;
331
332                 auto content = cv->content();
333                 DCPTimePeriod const content_period (content->position(), content->end(film));
334
335                 while (true) {
336                         auto j = views.begin();
337                         while (j != views.end()) {
338                                 auto test = dynamic_pointer_cast<T> (*j);
339                                 if (!test) {
340                                         ++j;
341                                         continue;
342                                 }
343
344                                 auto test_content = test->content();
345                                 if (
346                                         test->track() && test->track().get() == t &&
347                                         content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end(film)))) {
348                                         /* we have an overlap on track `t' */
349                                         ++t;
350                                         break;
351                                 }
352
353                                 ++j;
354                         }
355
356                         if (j == views.end ()) {
357                                 /* no overlap on `t' */
358                                 break;
359                         }
360                 }
361
362                 cv->set_track (t);
363                 tracks = max (tracks, t + 1);
364         }
365
366         return tracks - base;
367 }
368
369
370 /** Compare the mapped output channels of two TimelineViews, so we can into
371  *  order of first mapped DCP channel.
372  */
373 struct AudioMappingComparator {
374         bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
375                 int la = -1;
376                 auto cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
377                 if (cva) {
378                         auto oc = cva->content()->audio->mapping().mapped_output_channels();
379                         la = *min_element(boost::begin(oc), boost::end(oc));
380                 }
381                 int lb = -1;
382                 auto cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
383                 if (cvb) {
384                         auto oc = cvb->content()->audio->mapping().mapped_output_channels();
385                         lb = *min_element(boost::begin(oc), boost::end(oc));
386                 }
387                 return la < lb;
388         }
389 };
390
391
392 void
393 Timeline::assign_tracks ()
394 {
395         /* Tracks are:
396            Video 1
397            Video 2
398            Video N
399            Text 1
400            Text 2
401            Text N
402            Atmos
403            Audio 1
404            Audio 2
405            Audio N
406         */
407
408         auto film = _film.lock ();
409         DCPOMATIC_ASSERT (film);
410
411         _tracks = 0;
412
413         for (auto i: _views) {
414                 auto c = dynamic_pointer_cast<TimelineContentView>(i);
415                 if (c) {
416                         c->unset_track ();
417                 }
418         }
419
420         int const video_tracks = place<TimelineVideoContentView> (film, _views, _tracks);
421         int const text_tracks = place<TimelineTextContentView> (film, _views, _tracks);
422
423         /* Atmos */
424
425         bool have_atmos = false;
426         for (auto i: _views) {
427                 auto cv = dynamic_pointer_cast<TimelineAtmosContentView>(i);
428                 if (cv) {
429                         cv->set_track (_tracks);
430                         have_atmos = true;
431                 }
432         }
433
434         if (have_atmos) {
435                 ++_tracks;
436         }
437
438         /* Audio.  We're sorting the views so that we get the audio views in order of increasing
439            DCP channel index.
440         */
441
442         auto views = _views;
443         sort(views.begin(), views.end(), AudioMappingComparator());
444         int const audio_tracks = place<TimelineAudioContentView> (film, views, _tracks);
445
446         _labels_view->set_video_tracks (video_tracks);
447         _labels_view->set_audio_tracks (audio_tracks);
448         _labels_view->set_text_tracks (text_tracks);
449         _labels_view->set_atmos (have_atmos);
450
451         _time_axis_view->set_y (tracks());
452         _reels_view->set_y (8);
453 }
454
455
456 int
457 Timeline::tracks () const
458 {
459         return _tracks;
460 }
461
462
463 void
464 Timeline::setup_scrollbars ()
465 {
466         auto film = _film.lock ();
467         if (!film || !_pixels_per_second) {
468                 return;
469         }
470
471         int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
472
473         _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
474         _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
475         _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
476         _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
477 }
478
479
480 shared_ptr<TimelineView>
481 Timeline::event_to_view (wxMouseEvent& ev)
482 {
483         /* Search backwards through views so that we find the uppermost one first */
484         auto i = _views.rbegin();
485
486         int vsx, vsy;
487         _main_canvas->GetViewStart (&vsx, &vsy);
488         Position<int> const p (ev.GetX() + vsx * _x_scroll_rate, ev.GetY() + vsy * _y_scroll_rate);
489
490         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
491                 ++i;
492         }
493
494         if (i == _views.rend ()) {
495                 return {};
496         }
497
498         return *i;
499 }
500
501
502 void
503 Timeline::left_down (wxMouseEvent& ev)
504 {
505         _left_down = true;
506         _down_point = ev.GetPosition ();
507
508         switch (_tool) {
509         case SELECT:
510                 left_down_select (ev);
511                 break;
512         case ZOOM:
513         case ZOOM_ALL:
514         case SNAP:
515         case SEQUENCE:
516                 /* Nothing to do */
517                 break;
518         }
519 }
520
521
522 void
523 Timeline::left_down_select (wxMouseEvent& ev)
524 {
525         auto view = event_to_view (ev);
526         auto content_view = dynamic_pointer_cast<TimelineContentView>(view);
527
528         _down_view.reset ();
529
530         if (content_view) {
531                 _down_view = content_view;
532                 _down_view_position = content_view->content()->position ();
533         }
534
535         if (dynamic_pointer_cast<TimelineTimeAxisView>(view)) {
536                 int vsx, vsy;
537                 _main_canvas->GetViewStart(&vsx, &vsy);
538                 _viewer.seek(DCPTime::from_seconds((ev.GetPosition().x + vsx * _x_scroll_rate) / _pixels_per_second.get_value_or(1)), true);
539         }
540
541         for (auto i: _views) {
542                 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
543                 if (!cv) {
544                         continue;
545                 }
546
547                 if (!ev.ShiftDown ()) {
548                         cv->set_selected (view == i);
549                 }
550         }
551
552         if (content_view && ev.ShiftDown ()) {
553                 content_view->set_selected (!content_view->selected ());
554         }
555
556         _first_move = false;
557
558         if (_down_view) {
559                 /* Pre-compute the points that we might snap to */
560                 for (auto i: _views) {
561                         auto cv = dynamic_pointer_cast<TimelineContentView>(i);
562                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
563                                 continue;
564                         }
565
566                         auto film = _film.lock ();
567                         DCPOMATIC_ASSERT (film);
568
569                         _start_snaps.push_back (cv->content()->position());
570                         _end_snaps.push_back (cv->content()->position());
571                         _start_snaps.push_back (cv->content()->end(film));
572                         _end_snaps.push_back (cv->content()->end(film));
573
574                         for (auto i: cv->content()->reel_split_points(film)) {
575                                 _start_snaps.push_back (i);
576                         }
577                 }
578
579                 /* Tell everyone that things might change frequently during the drag */
580                 _down_view->content()->set_change_signals_frequent (true);
581         }
582 }
583
584
585 void
586 Timeline::left_up (wxMouseEvent& ev)
587 {
588         _left_down = false;
589
590         switch (_tool) {
591         case SELECT:
592                 left_up_select (ev);
593                 break;
594         case ZOOM:
595                 left_up_zoom (ev);
596                 break;
597         case ZOOM_ALL:
598         case SNAP:
599         case SEQUENCE:
600                 break;
601         }
602 }
603
604
605 void
606 Timeline::left_up_select (wxMouseEvent& ev)
607 {
608         if (_down_view) {
609                 _down_view->content()->set_change_signals_frequent (false);
610         }
611
612         _content_panel->set_selection (selected_content ());
613         /* Since we may have just set change signals back to `not-frequent', we have to
614            make sure this position change is signalled, even if the position value has
615            not changed since the last time it was set (with frequent=true).  This is
616            a bit of a hack.
617         */
618         set_position_from_event (ev, true);
619
620         /* Clear up up the stuff we don't do during drag */
621         assign_tracks ();
622         setup_scrollbars ();
623         Refresh ();
624
625         _start_snaps.clear ();
626         _end_snaps.clear ();
627 }
628
629
630 void
631 Timeline::left_up_zoom (wxMouseEvent& ev)
632 {
633         _zoom_point = ev.GetPosition ();
634
635         int vsx, vsy;
636         _main_canvas->GetViewStart (&vsx, &vsy);
637
638         wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
639         wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
640
641         if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
642                 /* Very small zoom rectangle: we assume it wasn't intentional */
643                 _zoom_point = optional<wxPoint> ();
644                 Refresh ();
645                 return;
646         }
647
648         auto const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
649         auto const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
650         set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
651
652         double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
653         double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
654         set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
655
656         setup_scrollbars ();
657         int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
658         _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
659         _labels_canvas->Scroll (0, y);
660
661         _zoom_point = optional<wxPoint> ();
662         Refresh ();
663 }
664
665
666 void
667 Timeline::set_pixels_per_track (int h)
668 {
669         _pixels_per_track = max(_minimum_pixels_per_track, h);
670 }
671
672
673 void
674 Timeline::mouse_moved (wxMouseEvent& ev)
675 {
676         switch (_tool) {
677         case SELECT:
678                 mouse_moved_select (ev);
679                 break;
680         case ZOOM:
681                 mouse_moved_zoom (ev);
682                 break;
683         case ZOOM_ALL:
684         case SNAP:
685         case SEQUENCE:
686                 break;
687         }
688 }
689
690
691 void
692 Timeline::mouse_moved_select (wxMouseEvent& ev)
693 {
694         if (!_left_down) {
695                 return;
696         }
697
698         set_position_from_event (ev);
699 }
700
701
702 void
703 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
704 {
705         if (!_left_down) {
706                 return;
707         }
708
709         _zoom_point = ev.GetPosition ();
710         Refresh ();
711 }
712
713
714 void
715 Timeline::right_down (wxMouseEvent& ev)
716 {
717         switch (_tool) {
718         case SELECT:
719                 right_down_select (ev);
720                 break;
721         case ZOOM:
722                 /* Zoom out */
723                 set_pixels_per_second (*_pixels_per_second / 2);
724                 set_pixels_per_track (_pixels_per_track / 2);
725                 setup_scrollbars ();
726                 Refresh ();
727                 break;
728         case ZOOM_ALL:
729         case SNAP:
730         case SEQUENCE:
731                 break;
732         }
733 }
734
735
736 void
737 Timeline::right_down_select (wxMouseEvent& ev)
738 {
739         auto view = event_to_view (ev);
740         auto cv = dynamic_pointer_cast<TimelineContentView> (view);
741         if (!cv) {
742                 return;
743         }
744
745         if (!cv->selected ()) {
746                 clear_selection ();
747                 cv->set_selected (true);
748         }
749
750         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
751 }
752
753
754 void
755 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
756 {
757         auto const d = a - b;
758         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
759                 nearest_distance = d;
760         }
761 }
762
763
764 void
765 Timeline::set_position_from_event (wxMouseEvent& ev, bool force_emit)
766 {
767         if (!_pixels_per_second) {
768                 return;
769         }
770
771         double const pps = _pixels_per_second.get ();
772
773         auto const p = ev.GetPosition();
774
775         if (!_first_move) {
776                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
777                    before the drag is considered to have started.
778                 */
779                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
780                 if (dist < 8) {
781                         return;
782                 }
783                 _first_move = true;
784         }
785
786         if (!_down_view) {
787                 return;
788         }
789
790         auto new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
791
792         auto film = _film.lock ();
793         DCPOMATIC_ASSERT (film);
794
795         if (_snap) {
796                 auto const new_end = new_position + _down_view->content()->length_after_trim(film);
797                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
798                    positive is right).
799                 */
800                 optional<DCPTime> nearest_distance;
801
802                 /* Find the nearest snap point */
803
804                 for (auto i: _start_snaps) {
805                         maybe_snap (i, new_position, nearest_distance);
806                 }
807
808                 for (auto i: _end_snaps) {
809                         maybe_snap (i, new_end, nearest_distance);
810                 }
811
812                 if (nearest_distance) {
813                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
814                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
815                                 new_position += nearest_distance.get ();
816                         }
817                 }
818         }
819
820         if (new_position < DCPTime ()) {
821                 new_position = DCPTime ();
822         }
823
824         _down_view->content()->set_position (film, new_position, force_emit);
825
826         film->set_sequence (false);
827 }
828
829
830 void
831 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
832 {
833         _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
834 }
835
836
837 shared_ptr<const Film>
838 Timeline::film () const
839 {
840         return _film.lock ();
841 }
842
843
844 void
845 Timeline::resized ()
846 {
847         if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
848                 zoom_all ();
849                 _first_resize = false;
850         }
851         setup_scrollbars ();
852 }
853
854
855 void
856 Timeline::clear_selection ()
857 {
858         for (auto i: _views) {
859                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView>(i);
860                 if (cv) {
861                         cv->set_selected (false);
862                 }
863         }
864 }
865
866
867 TimelineContentViewList
868 Timeline::selected_views () const
869 {
870         TimelineContentViewList sel;
871
872         for (auto i: _views) {
873                 auto cv = dynamic_pointer_cast<TimelineContentView>(i);
874                 if (cv && cv->selected()) {
875                         sel.push_back (cv);
876                 }
877         }
878
879         return sel;
880 }
881
882
883 ContentList
884 Timeline::selected_content () const
885 {
886         ContentList sel;
887
888         for (auto i: selected_views()) {
889                 sel.push_back(i->content());
890         }
891
892         return sel;
893 }
894
895
896 void
897 Timeline::set_selection (ContentList selection)
898 {
899         for (auto i: _views) {
900                 auto cv = dynamic_pointer_cast<TimelineContentView> (i);
901                 if (cv) {
902                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
903                 }
904         }
905 }
906
907
908 int
909 Timeline::tracks_y_offset () const
910 {
911         return _reels_view->bbox().height + 4;
912 }
913
914
915 int
916 Timeline::width () const
917 {
918         return _main_canvas->GetVirtualSize().GetWidth();
919 }
920
921
922 void
923 Timeline::scrolled (wxScrollWinEvent& ev)
924 {
925         if (ev.GetOrientation() == wxVERTICAL) {
926                 int x, y;
927                 _main_canvas->GetViewStart (&x, &y);
928                 _labels_canvas->Scroll (0, y);
929         }
930         ev.Skip ();
931 }
932
933
934 void
935 Timeline::tool_clicked (Tool t)
936 {
937         switch (t) {
938         case ZOOM:
939         case SELECT:
940                 _tool = t;
941                 break;
942         case ZOOM_ALL:
943                 zoom_all ();
944                 break;
945         case SNAP:
946         case SEQUENCE:
947                 break;
948         }
949 }
950
951
952 void
953 Timeline::zoom_all ()
954 {
955         auto film = _film.lock ();
956         DCPOMATIC_ASSERT (film);
957         set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
958         set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
959         setup_scrollbars ();
960         _main_canvas->Scroll (0, 0);
961         _labels_canvas->Scroll (0, 0);
962         Refresh ();
963 }
964
965
966 void
967 Timeline::keypress(wxKeyEvent const& event)
968 {
969         if (event.GetKeyCode() == WXK_DELETE) {
970                 auto film = _film.lock();
971                 film->remove_content(selected_content());
972         }
973 }
974