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