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