Merge branch 'prefs' of ssh://carlh.dyndns.org/home/carl/git/dcpomatic into prefs
[dcpomatic.git] / src / wx / timeline.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <list>
21 #include <wx/graphics.h>
22 #include <boost/weak_ptr.hpp>
23 #include "lib/film.h"
24 #include "lib/playlist.h"
25 #include "film_editor.h"
26 #include "timeline.h"
27 #include "wx_util.h"
28
29 using std::list;
30 using std::cout;
31 using std::max;
32 using boost::shared_ptr;
33 using boost::weak_ptr;
34 using boost::dynamic_pointer_cast;
35 using boost::bind;
36 using boost::optional;
37
38 /** Parent class for components of the timeline (e.g. a piece of content or an axis) */
39 class View : public boost::noncopyable
40 {
41 public:
42         View (Timeline& t)
43                 : _timeline (t)
44         {
45
46         }
47
48         virtual ~View () {}
49                 
50         void paint (wxGraphicsContext* g)
51         {
52                 _last_paint_bbox = bbox ();
53                 do_paint (g);
54         }
55         
56         void force_redraw ()
57         {
58                 _timeline.force_redraw (_last_paint_bbox);
59                 _timeline.force_redraw (bbox ());
60         }
61
62         virtual dcpomatic::Rect<int> bbox () const = 0;
63
64 protected:
65         virtual void do_paint (wxGraphicsContext *) = 0;
66         
67         int time_x (Time t) const
68         {
69                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
70         }
71         
72         Timeline& _timeline;
73
74 private:
75         dcpomatic::Rect<int> _last_paint_bbox;
76 };
77
78
79 /** Parent class for views of pieces of content */
80 class ContentView : public View
81 {
82 public:
83         ContentView (Timeline& tl, shared_ptr<Content> c)
84                 : View (tl)
85                 , _content (c)
86                 , _track (0)
87                 , _selected (false)
88         {
89                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
90         }
91
92         dcpomatic::Rect<int> bbox () const
93         {
94                 shared_ptr<const Film> film = _timeline.film ();
95                 shared_ptr<const Content> content = _content.lock ();
96                 if (!film || !content) {
97                         return dcpomatic::Rect<int> ();
98                 }
99                 
100                 return dcpomatic::Rect<int> (
101                         time_x (content->position ()) - 8,
102                         y_pos (_track) - 8,
103                         content->length_after_trim () * _timeline.pixels_per_time_unit() + 16,
104                         _timeline.track_height() + 16
105                         );
106         }
107
108         void set_selected (bool s) {
109                 _selected = s;
110                 force_redraw ();
111         }
112         
113         bool selected () const {
114                 return _selected;
115         }
116
117         shared_ptr<Content> content () const {
118                 return _content.lock ();
119         }
120
121         void set_track (int t) {
122                 _track = t;
123         }
124
125         int track () const {
126                 return _track;
127         }
128
129         virtual wxString type () const = 0;
130         virtual wxColour colour () const = 0;
131         
132 private:
133
134         void do_paint (wxGraphicsContext* gc)
135         {
136                 shared_ptr<const Film> film = _timeline.film ();
137                 shared_ptr<const Content> cont = content ();
138                 if (!film || !cont) {
139                         return;
140                 }
141
142                 Time const position = cont->position ();
143                 Time const len = cont->length_after_trim ();
144
145                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
146
147                 gc->SetPen (*wxBLACK_PEN);
148                 
149                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
150                 if (_selected) {
151                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
152                 } else {
153                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
154                 }
155
156                 wxGraphicsPath path = gc->CreatePath ();
157                 path.MoveToPoint    (time_x (position),       y_pos (_track) + 4);
158                 path.AddLineToPoint (time_x (position + len), y_pos (_track) + 4);
159                 path.AddLineToPoint (time_x (position + len), y_pos (_track + 1) - 4);
160                 path.AddLineToPoint (time_x (position),       y_pos (_track + 1) - 4);
161                 path.AddLineToPoint (time_x (position),       y_pos (_track) + 4);
162                 gc->StrokePath (path);
163                 gc->FillPath (path);
164
165                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path_summary()).data(), type().data());
166                 wxDouble name_width;
167                 wxDouble name_height;
168                 wxDouble name_descent;
169                 wxDouble name_leading;
170                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
171                 
172                 gc->Clip (wxRegion (time_x (position), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
173                 gc->DrawText (name, time_x (position) + 12, y_pos (_track + 1) - name_height - 4);
174                 gc->ResetClip ();
175         }
176
177         int y_pos (int t) const
178         {
179                 return _timeline.tracks_position().y + t * _timeline.track_height();
180         }
181
182         void content_changed (int p, bool frequent)
183         {
184                 ensure_ui_thread ();
185                 
186                 if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
187                         force_redraw ();
188                 }
189
190                 if (!frequent) {
191                         _timeline.setup_pixels_per_time_unit ();
192                         _timeline.Refresh ();
193                 }
194         }
195
196         boost::weak_ptr<Content> _content;
197         int _track;
198         bool _selected;
199
200         boost::signals2::scoped_connection _content_connection;
201 };
202
203 class AudioContentView : public ContentView
204 {
205 public:
206         AudioContentView (Timeline& tl, shared_ptr<Content> c)
207                 : ContentView (tl, c)
208         {}
209         
210 private:
211         wxString type () const
212         {
213                 return _("audio");
214         }
215
216         wxColour colour () const
217         {
218                 return wxColour (149, 121, 232, 255);
219         }
220 };
221
222 class VideoContentView : public ContentView
223 {
224 public:
225         VideoContentView (Timeline& tl, shared_ptr<Content> c)
226                 : ContentView (tl, c)
227         {}
228
229 private:        
230
231         wxString type () const
232         {
233                 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
234                         return _("video");
235                 } else {
236                         return _("still");
237                 }
238         }
239
240         wxColour colour () const
241         {
242                 return wxColour (242, 92, 120, 255);
243         }
244 };
245
246 class TimeAxisView : public View
247 {
248 public:
249         TimeAxisView (Timeline& tl, int y)
250                 : View (tl)
251                 , _y (y)
252         {}
253         
254         dcpomatic::Rect<int> bbox () const
255         {
256                 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
257         }
258
259         void set_y (int y)
260         {
261                 _y = y;
262                 force_redraw ();
263         }
264
265 private:        
266
267         void do_paint (wxGraphicsContext* gc)
268         {
269                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
270                 
271                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
272                 if (mark_interval > 5) {
273                         mark_interval -= mark_interval % 5;
274                 }
275                 if (mark_interval > 10) {
276                         mark_interval -= mark_interval % 10;
277                 }
278                 if (mark_interval > 60) {
279                         mark_interval -= mark_interval % 60;
280                 }
281                 if (mark_interval > 3600) {
282                         mark_interval -= mark_interval % 3600;
283                 }
284                 
285                 if (mark_interval < 1) {
286                         mark_interval = 1;
287                 }
288
289                 wxGraphicsPath path = gc->CreatePath ();
290                 path.MoveToPoint (_timeline.x_offset(), _y);
291                 path.AddLineToPoint (_timeline.width(), _y);
292                 gc->StrokePath (path);
293
294                 Time t = 0;
295                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
296                         wxGraphicsPath path = gc->CreatePath ();
297                         path.MoveToPoint (time_x (t), _y - 4);
298                         path.AddLineToPoint (time_x (t), _y + 4);
299                         gc->StrokePath (path);
300
301                         int tc = t / TIME_HZ;
302                         int const h = tc / 3600;
303                         tc -= h * 3600;
304                         int const m = tc / 60;
305                         tc -= m * 60;
306                         int const s = tc;
307                         
308                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
309                         wxDouble str_width;
310                         wxDouble str_height;
311                         wxDouble str_descent;
312                         wxDouble str_leading;
313                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
314                         
315                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
316                         if ((tx + str_width) < _timeline.width()) {
317                                 gc->DrawText (str, time_x (t), _y + 16);
318                         }
319                         
320                         t += mark_interval * TIME_HZ;
321                 }
322         }
323
324 private:
325         int _y;
326 };
327
328
329 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
330         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
331         , _film_editor (ed)
332         , _film (film)
333         , _time_axis_view (new TimeAxisView (*this, 32))
334         , _tracks (0)
335         , _pixels_per_time_unit (0)
336         , _left_down (false)
337         , _down_view_position (0)
338         , _first_move (false)
339         , _menu (this)
340         , _snap (true)
341 {
342 #ifndef __WXOSX__
343         SetDoubleBuffered (true);
344 #endif  
345
346         Bind (wxEVT_PAINT,      boost::bind (&Timeline::paint,       this));
347         Bind (wxEVT_LEFT_DOWN,  boost::bind (&Timeline::left_down,   this, _1));
348         Bind (wxEVT_LEFT_UP,    boost::bind (&Timeline::left_up,     this, _1));
349         Bind (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,  this, _1));
350         Bind (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved, this, _1));
351         Bind (wxEVT_SIZE,       boost::bind (&Timeline::resized,     this));
352
353         playlist_changed ();
354
355         SetMinSize (wxSize (640, tracks() * track_height() + 96));
356
357         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
358 }
359
360 void
361 Timeline::paint ()
362 {
363         wxPaintDC dc (this);
364
365         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
366         if (!gc) {
367                 return;
368         }
369
370         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
371
372         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
373                 (*i)->paint (gc);
374         }
375
376         delete gc;
377 }
378
379 void
380 Timeline::playlist_changed ()
381 {
382         ensure_ui_thread ();
383         
384         shared_ptr<const Film> fl = _film.lock ();
385         if (!fl) {
386                 return;
387         }
388
389         _views.clear ();
390         _views.push_back (_time_axis_view);
391
392         ContentList content = fl->playlist()->content ();
393
394         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
395                 if (dynamic_pointer_cast<VideoContent> (*i)) {
396                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
397                 }
398                 if (dynamic_pointer_cast<AudioContent> (*i)) {
399                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
400                 }
401         }
402
403         assign_tracks ();
404         setup_pixels_per_time_unit ();
405         Refresh ();
406 }
407
408 void
409 Timeline::assign_tracks ()
410 {
411         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
412                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
413                 if (cv) {
414                         cv->set_track (0);
415                         _tracks = 1;
416                 }
417         }
418
419         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
420                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
421                 if (!acv) {
422                         continue;
423                 }
424         
425                 shared_ptr<Content> acv_content = acv->content();
426
427                 int t = 1;
428                 while (1) {
429                         ViewList::iterator j = _views.begin();
430                         while (j != _views.end()) {
431                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
432                                 if (!test) {
433                                         ++j;
434                                         continue;
435                                 }
436                                 
437                                 shared_ptr<Content> test_content = test->content();
438                                         
439                                 if (test && test->track() == t) {
440                                         bool const no_overlap =
441                                                 (acv_content->position() < test_content->position() && acv_content->end() < test_content->position()) ||
442                                                 (acv_content->position() > test_content->end()      && acv_content->end() > test_content->end());
443                                         
444                                         if (!no_overlap) {
445                                                 /* we have an overlap on track `t' */
446                                                 ++t;
447                                                 break;
448                                         }
449                                 }
450                                 
451                                 ++j;
452                         }
453
454                         if (j == _views.end ()) {
455                                 /* no overlap on `t' */
456                                 break;
457                         }
458                 }
459
460                 acv->set_track (t);
461                 _tracks = max (_tracks, t + 1);
462         }
463
464         _time_axis_view->set_y (tracks() * track_height() + 32);
465 }
466
467 int
468 Timeline::tracks () const
469 {
470         return _tracks;
471 }
472
473 void
474 Timeline::setup_pixels_per_time_unit ()
475 {
476         shared_ptr<const Film> film = _film.lock ();
477         if (!film || film->length() == 0) {
478                 return;
479         }
480
481         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
482 }
483
484 shared_ptr<View>
485 Timeline::event_to_view (wxMouseEvent& ev)
486 {
487         ViewList::iterator i = _views.begin();
488         Position<int> const p (ev.GetX(), ev.GetY());
489         while (i != _views.end() && !(*i)->bbox().contains (p)) {
490                 ++i;
491         }
492
493         if (i == _views.end ()) {
494                 return shared_ptr<View> ();
495         }
496
497         return *i;
498 }
499
500 void
501 Timeline::left_down (wxMouseEvent& ev)
502 {
503         shared_ptr<View> view = event_to_view (ev);
504         shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
505
506         _down_view.reset ();
507
508         if (content_view) {
509                 _down_view = content_view;
510                 _down_view_position = content_view->content()->position ();
511         }
512
513         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
514                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
515                 if (!cv) {
516                         continue;
517                 }
518                 
519                 if (!ev.ShiftDown ()) {
520                         cv->set_selected (view == *i);
521                 }
522                 
523                 if (view == *i) {
524                         _film_editor->set_selection (cv->content ());
525                 }
526         }
527
528         if (content_view && ev.ShiftDown ()) {
529                 content_view->set_selected (!content_view->selected ());
530         }
531
532         _left_down = true;
533         _down_point = ev.GetPosition ();
534         _first_move = false;
535
536         if (_down_view) {
537                 _down_view->content()->set_change_signals_frequent (true);
538         }
539 }
540
541 void
542 Timeline::left_up (wxMouseEvent& ev)
543 {
544         _left_down = false;
545
546         if (_down_view) {
547                 _down_view->content()->set_change_signals_frequent (false);
548         }
549
550         set_position_from_event (ev);
551 }
552
553 void
554 Timeline::mouse_moved (wxMouseEvent& ev)
555 {
556         if (!_left_down) {
557                 return;
558         }
559
560         set_position_from_event (ev);
561 }
562
563 void
564 Timeline::right_down (wxMouseEvent& ev)
565 {
566         shared_ptr<View> view = event_to_view (ev);
567         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
568         if (!cv) {
569                 return;
570         }
571
572         if (!cv->selected ()) {
573                 clear_selection ();
574                 cv->set_selected (true);
575         }
576
577         _menu.popup (_film, selected_content (), ev.GetPosition ());
578 }
579
580 void
581 Timeline::set_position_from_event (wxMouseEvent& ev)
582 {
583         wxPoint const p = ev.GetPosition();
584
585         if (!_first_move) {
586                 /* We haven't moved yet; in that case, we must move the mouse some reasonable distance
587                    before the drag is considered to have started.
588                 */
589                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
590                 if (dist < 8) {
591                         return;
592                 }
593                 _first_move = true;
594         }
595
596         if (!_down_view) {
597                 return;
598         }
599         
600         Time new_position = _down_view_position + (p.x - _down_point.x) / _pixels_per_time_unit;
601         
602         if (_snap) {
603                 
604                 bool first = true;
605                 Time nearest_distance = TIME_MAX;
606                 Time nearest_new_position = TIME_MAX;
607                 
608                 /* Find the nearest content edge; this is inefficient */
609                 for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
610                         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
611                         if (!cv || cv == _down_view) {
612                                 continue;
613                         }
614                         
615                         {
616                                 /* Snap starts to ends */
617                                 Time const d = abs (cv->content()->end() - new_position);
618                                 if (first || d < nearest_distance) {
619                                         nearest_distance = d;
620                                         nearest_new_position = cv->content()->end();
621                                 }
622                         }
623                         
624                         {
625                                 /* Snap ends to starts */
626                                 Time const d = abs (cv->content()->position() - (new_position + _down_view->content()->length_after_trim()));
627                                 if (d < nearest_distance) {
628                                         nearest_distance = d;
629                                         nearest_new_position = cv->content()->position() - _down_view->content()->length_after_trim ();
630                                 }
631                         }
632                         
633                         first = false;
634                 }
635                 
636                 if (!first) {
637                         /* Snap if it's close; `close' means within a proportion of the time on the timeline */
638                         if (nearest_distance < (width() / pixels_per_time_unit()) / 32) {
639                                 new_position = nearest_new_position;
640                         }
641                 }
642         }
643         
644         if (new_position < 0) {
645                 new_position = 0;
646         }
647         
648         _down_view->content()->set_position (new_position);
649         
650         shared_ptr<Film> film = _film.lock ();
651         assert (film);
652         film->set_sequence_video (false);
653 }
654
655 void
656 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
657 {
658         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
659 }
660
661 shared_ptr<const Film>
662 Timeline::film () const
663 {
664         return _film.lock ();
665 }
666
667 void
668 Timeline::resized ()
669 {
670         setup_pixels_per_time_unit ();
671 }
672
673 void
674 Timeline::clear_selection ()
675 {
676         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
677                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
678                 if (cv) {
679                         cv->set_selected (false);
680                 }
681         }
682 }
683
684 Timeline::ContentViewList
685 Timeline::selected_views () const
686 {
687         ContentViewList sel;
688         
689         for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
690                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
691                 if (cv && cv->selected()) {
692                         sel.push_back (cv);
693                 }
694         }
695
696         return sel;
697 }
698
699 ContentList
700 Timeline::selected_content () const
701 {
702         ContentList sel;
703         ContentViewList views = selected_views ();
704         
705         for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
706                 sel.push_back ((*i)->content ());
707         }
708
709         return sel;
710 }