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