Replace May/Done/NotDone signal sets with one signal and extend
[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_change_connection = film->ContentChange.connect (bind (&Timeline::film_content_change, this, _1, _3, _4));
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_change (ChangeType type, int property, bool frequent)
247 {
248         if (type != CHANGE_TYPE_DONE) {
249                 return;
250         }
251
252         ensure_ui_thread ();
253
254         if (property == AudioContentProperty::STREAMS) {
255                 recreate_views ();
256         } else if (property == ContentProperty::POSITION || property == ContentProperty::LENGTH) {
257                 _reels_view->force_redraw ();
258         } else if (!frequent) {
259                 setup_scrollbars ();
260                 Refresh ();
261         }
262 }
263
264 template <class T>
265 int
266 place (TimelineViewList& views, int& tracks)
267 {
268         int const base = tracks;
269
270         BOOST_FOREACH (shared_ptr<TimelineView> i, views) {
271                 if (!dynamic_pointer_cast<T>(i)) {
272                         continue;
273                 }
274
275                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (i);
276
277                 int t = base;
278
279                 shared_ptr<Content> content = cv->content();
280                 DCPTimePeriod const content_period (content->position(), content->end());
281
282                 while (true) {
283                         TimelineViewList::iterator j = views.begin();
284                         while (j != views.end()) {
285                                 shared_ptr<T> test = dynamic_pointer_cast<T> (*j);
286                                 if (!test) {
287                                         ++j;
288                                         continue;
289                                 }
290
291                                 shared_ptr<Content> test_content = test->content();
292                                 if (
293                                         test->track() && test->track().get() == t &&
294                                         content_period.overlap(DCPTimePeriod(test_content->position(), test_content->end()))) {
295                                         /* we have an overlap on track `t' */
296                                         ++t;
297                                         break;
298                                 }
299
300                                 ++j;
301                         }
302
303                         if (j == views.end ()) {
304                                 /* no overlap on `t' */
305                                 break;
306                         }
307                 }
308
309                 cv->set_track (t);
310                 tracks = max (tracks, t + 1);
311         }
312
313         return tracks - base;
314 }
315
316 /** Compare the mapped output channels of two TimelineViews, so we can into
317  *  order of first mapped DCP channel.
318  */
319 struct AudioMappingComparator {
320         bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
321                 int la = -1;
322                 shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
323                 if (cva) {
324                         list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
325                         la = *min_element(boost::begin(oc), boost::end(oc));
326                 }
327                 int lb = -1;
328                 shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
329                 if (cvb) {
330                         list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
331                         lb = *min_element(boost::begin(oc), boost::end(oc));
332                 }
333                 return la < lb;
334         }
335 };
336
337 void
338 Timeline::assign_tracks ()
339 {
340         /* Tracks are:
341            Video (mono or left-eye)
342            Video (right-eye)
343            Text 1
344            Text 2
345            Text N
346            Atmos
347            Audio 1
348            Audio 2
349            Audio N
350         */
351
352         _tracks = 0;
353
354         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
355                 shared_ptr<TimelineContentView> c = dynamic_pointer_cast<TimelineContentView> (*i);
356                 if (c) {
357                         c->unset_track ();
358                 }
359         }
360
361         /* Video */
362
363         bool have_3d = false;
364         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
365                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
366                 if (!cv) {
367                         continue;
368                 }
369
370                 /* Video on tracks 0 and maybe 1 (left and right eye) */
371                 if (cv->content()->video->frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
372                         cv->set_track (1);
373                         _tracks = max (_tracks, 2);
374                         have_3d = true;
375                 } else {
376                         cv->set_track (0);
377                 }
378         }
379
380         _tracks = max (_tracks, 1);
381
382         /* Texts */
383
384         int const text_tracks = place<TimelineTextContentView> (_views, _tracks);
385
386         /* Atmos */
387
388         bool have_atmos = false;
389         BOOST_FOREACH (shared_ptr<TimelineView> i, _views) {
390                 shared_ptr<TimelineVideoContentView> cv = dynamic_pointer_cast<TimelineVideoContentView> (i);
391                 if (!cv) {
392                         continue;
393                 }
394                 if (dynamic_pointer_cast<TimelineAtmosContentView> (i)) {
395                         cv->set_track (_tracks - 1);
396                         have_atmos = true;
397                 }
398         }
399
400         if (have_atmos) {
401                 ++_tracks;
402         }
403
404         /* Audio.  We're sorting the views so that we get the audio views in order of increasing
405            DCP channel index.
406         */
407
408         TimelineViewList views = _views;
409         sort(views.begin(), views.end(), AudioMappingComparator());
410         int const audio_tracks = place<TimelineAudioContentView> (views, _tracks);
411
412         _labels_view->set_3d (have_3d);
413         _labels_view->set_audio_tracks (audio_tracks);
414         _labels_view->set_text_tracks (text_tracks);
415         _labels_view->set_atmos (have_atmos);
416
417         _time_axis_view->set_y (tracks());
418         _reels_view->set_y (8);
419 }
420
421 int
422 Timeline::tracks () const
423 {
424         return _tracks;
425 }
426
427 void
428 Timeline::setup_scrollbars ()
429 {
430         shared_ptr<const Film> film = _film.lock ();
431         if (!film || !_pixels_per_second) {
432                 return;
433         }
434
435         int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
436
437         _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
438         _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
439         _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
440         _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
441 }
442
443 shared_ptr<TimelineView>
444 Timeline::event_to_view (wxMouseEvent& ev)
445 {
446         /* Search backwards through views so that we find the uppermost one first */
447         TimelineViewList::reverse_iterator i = _views.rbegin();
448         Position<int> const p (ev.GetX(), ev.GetY());
449         while (i != _views.rend() && !(*i)->bbox().contains (p)) {
450                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
451                 ++i;
452         }
453
454         if (i == _views.rend ()) {
455                 return shared_ptr<TimelineView> ();
456         }
457
458         return *i;
459 }
460
461 void
462 Timeline::left_down (wxMouseEvent& ev)
463 {
464         _left_down = true;
465         _down_point = ev.GetPosition ();
466
467         switch (_tool) {
468         case SELECT:
469                 left_down_select (ev);
470                 break;
471         case ZOOM:
472         case ZOOM_ALL:
473         case SNAP:
474         case SEQUENCE:
475                 /* Nothing to do */
476                 break;
477         }
478 }
479
480 void
481 Timeline::left_down_select (wxMouseEvent& ev)
482 {
483         shared_ptr<TimelineView> view = event_to_view (ev);
484         shared_ptr<TimelineContentView> content_view = dynamic_pointer_cast<TimelineContentView> (view);
485
486         _down_view.reset ();
487
488         if (content_view) {
489                 _down_view = content_view;
490                 _down_view_position = content_view->content()->position ();
491         }
492
493         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
494                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
495                 if (!cv) {
496                         continue;
497                 }
498
499                 if (!ev.ShiftDown ()) {
500                         cv->set_selected (view == *i);
501                 }
502         }
503
504         if (content_view && ev.ShiftDown ()) {
505                 content_view->set_selected (!content_view->selected ());
506         }
507
508         _first_move = false;
509
510         if (_down_view) {
511                 /* Pre-compute the points that we might snap to */
512                 for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
513                         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
514                         if (!cv || cv == _down_view || cv->content() == _down_view->content()) {
515                                 continue;
516                         }
517
518                         _start_snaps.push_back (cv->content()->position());
519                         _end_snaps.push_back (cv->content()->position());
520                         _start_snaps.push_back (cv->content()->end());
521                         _end_snaps.push_back (cv->content()->end());
522
523                         BOOST_FOREACH (DCPTime i, cv->content()->reel_split_points()) {
524                                 _start_snaps.push_back (i);
525                         }
526                 }
527
528                 /* Tell everyone that things might change frequently during the drag */
529                 _down_view->content()->set_change_signals_frequent (true);
530         }
531 }
532
533 void
534 Timeline::left_up (wxMouseEvent& ev)
535 {
536         _left_down = false;
537
538         switch (_tool) {
539         case SELECT:
540                 left_up_select (ev);
541                 break;
542         case ZOOM:
543                 left_up_zoom (ev);
544                 break;
545         case ZOOM_ALL:
546         case SNAP:
547         case SEQUENCE:
548                 break;
549         }
550 }
551
552 void
553 Timeline::left_up_select (wxMouseEvent& ev)
554 {
555         if (_down_view) {
556                 _down_view->content()->set_change_signals_frequent (false);
557         }
558
559         _content_panel->set_selection (selected_content ());
560         set_position_from_event (ev);
561
562         /* Clear up up the stuff we don't do during drag */
563         assign_tracks ();
564         setup_scrollbars ();
565         Refresh ();
566
567         _start_snaps.clear ();
568         _end_snaps.clear ();
569 }
570
571 void
572 Timeline::left_up_zoom (wxMouseEvent& ev)
573 {
574         _zoom_point = ev.GetPosition ();
575
576         int vsx, vsy;
577         _main_canvas->GetViewStart (&vsx, &vsy);
578
579         wxPoint top_left(min(_down_point.x, _zoom_point->x), min(_down_point.y, _zoom_point->y));
580         wxPoint bottom_right(max(_down_point.x, _zoom_point->x), max(_down_point.y, _zoom_point->y));
581
582         if ((bottom_right.x - top_left.x) < 8 || (bottom_right.y - top_left.y) < 8) {
583                 /* Very small zoom rectangle: we assume it wasn't intentional */
584                 return;
585         }
586
587         DCPTime const time_left = DCPTime::from_seconds((top_left.x + vsx) / *_pixels_per_second);
588         DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
589         set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
590
591         double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
592         double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
593         set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
594
595         setup_scrollbars ();
596         int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
597         _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
598         _labels_canvas->Scroll (0, y);
599
600         _zoom_point = optional<wxPoint> ();
601         Refresh ();
602 }
603
604 void
605 Timeline::set_pixels_per_track (int h)
606 {
607         _pixels_per_track = max(_minimum_pixels_per_track, h);
608 }
609
610 void
611 Timeline::mouse_moved (wxMouseEvent& ev)
612 {
613         switch (_tool) {
614         case SELECT:
615                 mouse_moved_select (ev);
616                 break;
617         case ZOOM:
618                 mouse_moved_zoom (ev);
619                 break;
620         case ZOOM_ALL:
621         case SNAP:
622         case SEQUENCE:
623                 break;
624         }
625 }
626
627 void
628 Timeline::mouse_moved_select (wxMouseEvent& ev)
629 {
630         if (!_left_down) {
631                 return;
632         }
633
634         set_position_from_event (ev);
635 }
636
637 void
638 Timeline::mouse_moved_zoom (wxMouseEvent& ev)
639 {
640         if (!_left_down) {
641                 return;
642         }
643
644         _zoom_point = ev.GetPosition ();
645         Refresh ();
646 }
647
648 void
649 Timeline::right_down (wxMouseEvent& ev)
650 {
651         switch (_tool) {
652         case SELECT:
653                 right_down_select (ev);
654                 break;
655         case ZOOM:
656                 /* Zoom out */
657                 set_pixels_per_second (*_pixels_per_second / 2);
658                 set_pixels_per_track (_pixels_per_track / 2);
659                 setup_scrollbars ();
660                 Refresh ();
661                 break;
662         case ZOOM_ALL:
663         case SNAP:
664         case SEQUENCE:
665                 break;
666         }
667 }
668
669 void
670 Timeline::right_down_select (wxMouseEvent& ev)
671 {
672         shared_ptr<TimelineView> view = event_to_view (ev);
673         shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (view);
674         if (!cv) {
675                 return;
676         }
677
678         if (!cv->selected ()) {
679                 clear_selection ();
680                 cv->set_selected (true);
681         }
682
683         _menu.popup (_film, selected_content (), selected_views (), ev.GetPosition ());
684 }
685
686 void
687 Timeline::maybe_snap (DCPTime a, DCPTime b, optional<DCPTime>& nearest_distance) const
688 {
689         DCPTime const d = a - b;
690         if (!nearest_distance || d.abs() < nearest_distance.get().abs()) {
691                 nearest_distance = d;
692         }
693 }
694
695 void
696 Timeline::set_position_from_event (wxMouseEvent& ev)
697 {
698         if (!_pixels_per_second) {
699                 return;
700         }
701
702         double const pps = _pixels_per_second.get ();
703
704         wxPoint const p = ev.GetPosition();
705
706         if (!_first_move) {
707                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
708                    before the drag is considered to have started.
709                 */
710                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
711                 if (dist < 8) {
712                         return;
713                 }
714                 _first_move = true;
715         }
716
717         if (!_down_view) {
718                 return;
719         }
720
721         DCPTime new_position = _down_view_position + DCPTime::from_seconds ((p.x - _down_point.x) / pps);
722
723         if (_snap) {
724
725                 DCPTime const new_end = new_position + _down_view->content()->length_after_trim();
726                 /* Signed `distance' to nearest thing (i.e. negative is left on the timeline,
727                    positive is right).
728                 */
729                 optional<DCPTime> nearest_distance;
730
731                 /* Find the nearest snap point */
732
733                 BOOST_FOREACH (DCPTime i, _start_snaps) {
734                         maybe_snap (i, new_position, nearest_distance);
735                 }
736
737                 BOOST_FOREACH (DCPTime i, _end_snaps) {
738                         maybe_snap (i, new_end, nearest_distance);
739                 }
740
741                 if (nearest_distance) {
742                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
743                         if (nearest_distance.get().abs() < DCPTime::from_seconds ((width() / pps) / 64)) {
744                                 new_position += nearest_distance.get ();
745                         }
746                 }
747         }
748
749         if (new_position < DCPTime ()) {
750                 new_position = DCPTime ();
751         }
752
753         _down_view->content()->set_position (new_position);
754
755         shared_ptr<Film> film = _film.lock ();
756         DCPOMATIC_ASSERT (film);
757         film->set_sequence (false);
758 }
759
760 void
761 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
762 {
763         _main_canvas->RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
764 }
765
766 shared_ptr<const Film>
767 Timeline::film () const
768 {
769         return _film.lock ();
770 }
771
772 void
773 Timeline::resized ()
774 {
775         if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
776                 zoom_all ();
777                 _first_resize = false;
778         }
779         setup_scrollbars ();
780 }
781
782 void
783 Timeline::clear_selection ()
784 {
785         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
786                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
787                 if (cv) {
788                         cv->set_selected (false);
789                 }
790         }
791 }
792
793 TimelineContentViewList
794 Timeline::selected_views () const
795 {
796         TimelineContentViewList sel;
797
798         for (TimelineViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
799                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
800                 if (cv && cv->selected()) {
801                         sel.push_back (cv);
802                 }
803         }
804
805         return sel;
806 }
807
808 ContentList
809 Timeline::selected_content () const
810 {
811         ContentList sel;
812         TimelineContentViewList views = selected_views ();
813
814         for (TimelineContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
815                 sel.push_back ((*i)->content ());
816         }
817
818         return sel;
819 }
820
821 void
822 Timeline::set_selection (ContentList selection)
823 {
824         for (TimelineViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
825                 shared_ptr<TimelineContentView> cv = dynamic_pointer_cast<TimelineContentView> (*i);
826                 if (cv) {
827                         cv->set_selected (find (selection.begin(), selection.end(), cv->content ()) != selection.end ());
828                 }
829         }
830 }
831
832 int
833 Timeline::tracks_y_offset () const
834 {
835         return _reels_view->bbox().height + 4;
836 }
837
838 int
839 Timeline::width () const
840 {
841         return _main_canvas->GetVirtualSize().GetWidth();
842 }
843
844 void
845 Timeline::scrolled (wxScrollWinEvent& ev)
846 {
847         if (ev.GetOrientation() == wxVERTICAL) {
848                 int x, y;
849                 _main_canvas->GetViewStart (&x, &y);
850                 _labels_canvas->Scroll (0, y);
851         }
852         ev.Skip ();
853 }
854
855 void
856 Timeline::tool_clicked (Tool t)
857 {
858         switch (t) {
859         case ZOOM:
860         case SELECT:
861                 _tool = t;
862                 break;
863         case ZOOM_ALL:
864                 zoom_all ();
865                 break;
866         case SNAP:
867         case SEQUENCE:
868                 break;
869         }
870 }
871
872 void
873 Timeline::zoom_all ()
874 {
875         shared_ptr<Film> film = _film.lock ();
876         DCPOMATIC_ASSERT (film);
877         set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
878         set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
879         setup_scrollbars ();
880         _main_canvas->Scroll (0, 0);
881         _labels_canvas->Scroll (0, 0);
882         Refresh ();
883 }