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