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