Merge branch '1.0' of ssh://carlh.dnsalias.org/home/carl/git/dvdomatic into 1.0
[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
37 class View : public boost::noncopyable
38 {
39 public:
40         View (Timeline& t)
41                 : _timeline (t)
42         {
43
44         }
45                 
46         void paint (wxGraphicsContext* g)
47         {
48                 _last_paint_bbox = bbox ();
49                 do_paint (g);
50         }
51         
52         void force_redraw ()
53         {
54                 _timeline.force_redraw (_last_paint_bbox);
55                 _timeline.force_redraw (bbox ());
56         }
57
58         virtual dcpomatic::Rect<int> bbox () const = 0;
59
60 protected:
61         virtual void do_paint (wxGraphicsContext *) = 0;
62         
63         int time_x (Time t) const
64         {
65                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
66         }
67         
68         Timeline& _timeline;
69
70 private:
71         dcpomatic::Rect<int> _last_paint_bbox;
72 };
73
74 class ContentView : public View
75 {
76 public:
77         ContentView (Timeline& tl, shared_ptr<Content> c)
78                 : View (tl)
79                 , _content (c)
80                 , _track (0)
81                 , _selected (false)
82         {
83                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2, _3));
84         }
85
86         dcpomatic::Rect<int> bbox () const
87         {
88                 shared_ptr<const Film> film = _timeline.film ();
89                 shared_ptr<const Content> content = _content.lock ();
90                 if (!film || !content) {
91                         return dcpomatic::Rect<int> ();
92                 }
93                 
94                 return dcpomatic::Rect<int> (
95                         time_x (content->start ()) - 8,
96                         y_pos (_track) - 8,
97                         content->length () * _timeline.pixels_per_time_unit() + 16,
98                         _timeline.track_height() + 16
99                         );
100         }
101
102         void set_selected (bool s) {
103                 _selected = s;
104                 force_redraw ();
105         }
106         
107         bool selected () const {
108                 return _selected;
109         }
110
111         shared_ptr<Content> content () const {
112                 return _content.lock ();
113         }
114
115         void set_track (int t) {
116                 _track = t;
117         }
118
119         int track () const {
120                 return _track;
121         }
122
123         virtual wxString type () const = 0;
124         virtual wxColour colour () const = 0;
125         
126 private:
127
128         void do_paint (wxGraphicsContext* gc)
129         {
130                 shared_ptr<const Film> film = _timeline.film ();
131                 shared_ptr<const Content> cont = content ();
132                 if (!film || !cont) {
133                         return;
134                 }
135
136                 Time const start = cont->start ();
137                 Time const len = cont->length ();
138
139                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
140
141                 gc->SetPen (*wxBLACK_PEN);
142                 
143                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
144                 if (_selected) {
145                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
146                 } else {
147                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
148                 }
149
150                 wxGraphicsPath path = gc->CreatePath ();
151                 path.MoveToPoint    (time_x (start),       y_pos (_track) + 4);
152                 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
153                 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
154                 path.AddLineToPoint (time_x (start),       y_pos (_track + 1) - 4);
155                 path.AddLineToPoint (time_x (start),       y_pos (_track) + 4);
156                 gc->StrokePath (path);
157                 gc->FillPath (path);
158
159                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (cont->path().filename().string()).data(), type().data());
160                 wxDouble name_width;
161                 wxDouble name_height;
162                 wxDouble name_descent;
163                 wxDouble name_leading;
164                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
165                 
166                 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
167                 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
168                 gc->ResetClip ();
169         }
170
171         int y_pos (int t) const
172         {
173                 return _timeline.tracks_position().y + t * _timeline.track_height();
174         }
175
176         void content_changed (int p, bool frequent)
177         {
178                 ensure_ui_thread ();
179                 
180                 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
181                         force_redraw ();
182                 }
183
184                 if (!frequent) {
185                         _timeline.setup_pixels_per_time_unit ();
186                         _timeline.Refresh ();
187                 }
188         }
189
190         boost::weak_ptr<Content> _content;
191         int _track;
192         bool _selected;
193
194         boost::signals2::scoped_connection _content_connection;
195 };
196
197 class AudioContentView : public ContentView
198 {
199 public:
200         AudioContentView (Timeline& tl, shared_ptr<Content> c)
201                 : ContentView (tl, c)
202         {}
203         
204 private:
205         wxString type () const
206         {
207                 return _("audio");
208         }
209
210         wxColour colour () const
211         {
212                 return wxColour (149, 121, 232, 255);
213         }
214 };
215
216 class VideoContentView : public ContentView
217 {
218 public:
219         VideoContentView (Timeline& tl, shared_ptr<Content> c)
220                 : ContentView (tl, c)
221         {}
222
223 private:        
224
225         wxString type () const
226         {
227                 if (dynamic_pointer_cast<FFmpegContent> (content ())) {
228                         return _("video");
229                 } else {
230                         return _("still");
231                 }
232         }
233
234         wxColour colour () const
235         {
236                 return wxColour (242, 92, 120, 255);
237         }
238 };
239
240 class TimeAxisView : public View
241 {
242 public:
243         TimeAxisView (Timeline& tl, int y)
244                 : View (tl)
245                 , _y (y)
246         {}
247         
248         dcpomatic::Rect<int> bbox () const
249         {
250                 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
251         }
252
253         void set_y (int y)
254         {
255                 _y = y;
256                 force_redraw ();
257         }
258
259 private:        
260
261         void do_paint (wxGraphicsContext* gc)
262         {
263                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
264                 
265                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
266                 if (mark_interval > 5) {
267                         mark_interval -= mark_interval % 5;
268                 }
269                 if (mark_interval > 10) {
270                         mark_interval -= mark_interval % 10;
271                 }
272                 if (mark_interval > 60) {
273                         mark_interval -= mark_interval % 60;
274                 }
275                 if (mark_interval > 3600) {
276                         mark_interval -= mark_interval % 3600;
277                 }
278                 
279                 if (mark_interval < 1) {
280                         mark_interval = 1;
281                 }
282
283                 wxGraphicsPath path = gc->CreatePath ();
284                 path.MoveToPoint (_timeline.x_offset(), _y);
285                 path.AddLineToPoint (_timeline.width(), _y);
286                 gc->StrokePath (path);
287
288                 Time t = 0;
289                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
290                         wxGraphicsPath path = gc->CreatePath ();
291                         path.MoveToPoint (time_x (t), _y - 4);
292                         path.AddLineToPoint (time_x (t), _y + 4);
293                         gc->StrokePath (path);
294
295                         int tc = t / TIME_HZ;
296                         int const h = tc / 3600;
297                         tc -= h * 3600;
298                         int const m = tc / 60;
299                         tc -= m * 60;
300                         int const s = tc;
301                         
302                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
303                         wxDouble str_width;
304                         wxDouble str_height;
305                         wxDouble str_descent;
306                         wxDouble str_leading;
307                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
308                         
309                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
310                         if ((tx + str_width) < _timeline.width()) {
311                                 gc->DrawText (str, time_x (t), _y + 16);
312                         }
313                         
314                         t += mark_interval * TIME_HZ;
315                 }
316         }
317
318 private:
319         int _y;
320 };
321
322 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
323         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
324         , _film_editor (ed)
325         , _film (film)
326         , _time_axis_view (new TimeAxisView (*this, 32))
327         , _tracks (0)
328         , _pixels_per_time_unit (0)
329         , _left_down (false)
330         , _down_view_start (0)
331         , _first_move (false)
332         , _menu (film, this)
333 {
334 #ifndef __WXOSX__
335         SetDoubleBuffered (true);
336 #endif  
337
338         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
339         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
340         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
341         Connect (wxID_ANY, wxEVT_RIGHT_DOWN, wxMouseEventHandler (Timeline::right_down), 0, this);
342         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
343         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
344
345         playlist_changed ();
346
347         SetMinSize (wxSize (640, tracks() * track_height() + 96));
348
349         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
350 }
351
352 void
353 Timeline::paint (wxPaintEvent &)
354 {
355         wxPaintDC dc (this);
356
357         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
358         if (!gc) {
359                 return;
360         }
361
362         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
363
364         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
365                 (*i)->paint (gc);
366         }
367
368         delete gc;
369 }
370
371 void
372 Timeline::playlist_changed ()
373 {
374         ensure_ui_thread ();
375         
376         shared_ptr<const Film> fl = _film.lock ();
377         if (!fl) {
378                 return;
379         }
380
381         _views.clear ();
382         _views.push_back (_time_axis_view);
383
384         ContentList content = fl->playlist()->content ();
385
386         for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
387                 if (dynamic_pointer_cast<VideoContent> (*i)) {
388                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i)));
389                 }
390                 if (dynamic_pointer_cast<AudioContent> (*i)) {
391                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i)));
392                 }
393         }
394
395         assign_tracks ();
396         setup_pixels_per_time_unit ();
397         Refresh ();
398 }
399
400 void
401 Timeline::assign_tracks ()
402 {
403         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
404                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
405                 if (cv) {
406                         cv->set_track (0);
407                         _tracks = 1;
408                 }
409         }
410
411         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
412                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
413                 if (!acv) {
414                         continue;
415                 }
416         
417                 shared_ptr<Content> acv_content = acv->content();
418
419                 int t = 1;
420                 while (1) {
421                         ViewList::iterator j = _views.begin();
422                         while (j != _views.end()) {
423                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
424                                 if (!test) {
425                                         ++j;
426                                         continue;
427                                 }
428                                 
429                                 shared_ptr<Content> test_content = test->content();
430                                         
431                                 if (test && test->track() == t) {
432                                         bool const no_overlap =
433                                                 (acv_content->start() < test_content->start() && acv_content->end() < test_content->start()) ||
434                                                 (acv_content->start() > test_content->end()   && acv_content->end() > test_content->end());
435                                         
436                                         if (!no_overlap) {
437                                                 /* we have an overlap on track `t' */
438                                                 ++t;
439                                                 break;
440                                         }
441                                 }
442                                 
443                                 ++j;
444                         }
445
446                         if (j == _views.end ()) {
447                                 /* no overlap on `t' */
448                                 break;
449                         }
450                 }
451
452                 acv->set_track (t);
453                 _tracks = max (_tracks, t + 1);
454         }
455
456         _time_axis_view->set_y (tracks() * track_height() + 32);
457 }
458
459 int
460 Timeline::tracks () const
461 {
462         return _tracks;
463 }
464
465 void
466 Timeline::setup_pixels_per_time_unit ()
467 {
468         shared_ptr<const Film> film = _film.lock ();
469         if (!film) {
470                 return;
471         }
472
473         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length ();
474 }
475
476 shared_ptr<View>
477 Timeline::event_to_view (wxMouseEvent& ev)
478 {
479         ViewList::iterator i = _views.begin();
480         Position<int> const p (ev.GetX(), ev.GetY());
481         while (i != _views.end() && !(*i)->bbox().contains (p)) {
482                 ++i;
483         }
484
485         if (i == _views.end ()) {
486                 return shared_ptr<View> ();
487         }
488
489         return *i;
490 }
491
492 void
493 Timeline::left_down (wxMouseEvent& ev)
494 {
495         shared_ptr<View> view = event_to_view (ev);
496         shared_ptr<ContentView> content_view = dynamic_pointer_cast<ContentView> (view);
497
498         _down_view.reset ();
499
500         if (content_view) {
501                 _down_view = content_view;
502                 _down_view_start = content_view->content()->start ();
503         }
504
505         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
506                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
507                 if (!cv) {
508                         continue;
509                 }
510                 
511                 if (!ev.ShiftDown ()) {
512                         cv->set_selected (view == *i);
513                 }
514                 
515                 if (view == *i) {
516                         _film_editor->set_selection (cv->content ());
517                 }
518         }
519
520         if (content_view && ev.ShiftDown ()) {
521                 content_view->set_selected (!content_view->selected ());
522         }
523
524         _left_down = true;
525         _down_point = ev.GetPosition ();
526         _first_move = false;
527
528         if (_down_view) {
529                 _down_view->content()->set_change_signals_frequent (true);
530         }
531 }
532
533 void
534 Timeline::left_up (wxMouseEvent& ev)
535 {
536         _left_down = false;
537
538         if (_down_view) {
539                 _down_view->content()->set_change_signals_frequent (false);
540         }
541
542         set_start_from_event (ev);
543 }
544
545 void
546 Timeline::mouse_moved (wxMouseEvent& ev)
547 {
548         if (!_left_down) {
549                 return;
550         }
551
552         set_start_from_event (ev);
553 }
554
555 void
556 Timeline::right_down (wxMouseEvent& ev)
557 {
558         shared_ptr<View> view = event_to_view (ev);
559         shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (view);
560         if (!cv) {
561                 return;
562         }
563
564         if (!cv->selected ()) {
565                 clear_selection ();
566                 cv->set_selected (true);
567         }
568
569         _menu.popup (selected_content (), ev.GetPosition ());
570 }
571
572 void
573 Timeline::set_start_from_event (wxMouseEvent& ev)
574 {
575         wxPoint const p = ev.GetPosition();
576
577         if (!_first_move) {
578                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
579                 if (dist < 8) {
580                         return;
581                 }
582                 _first_move = true;
583         }
584
585         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
586         if (_down_view) {
587                 _down_view->content()->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
588
589                 shared_ptr<Film> film = _film.lock ();
590                 assert (film);
591                 film->set_sequence_video (false);
592         }
593 }
594
595 void
596 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
597 {
598         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
599 }
600
601 shared_ptr<const Film>
602 Timeline::film () const
603 {
604         return _film.lock ();
605 }
606
607 void
608 Timeline::resized (wxSizeEvent &)
609 {
610         setup_pixels_per_time_unit ();
611 }
612
613 void
614 Timeline::clear_selection ()
615 {
616         for (ViewList::iterator i = _views.begin(); i != _views.end(); ++i) {
617                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
618                 if (cv) {
619                         cv->set_selected (false);
620                 }
621         }
622 }
623
624 Timeline::ContentViewList
625 Timeline::selected_views () const
626 {
627         ContentViewList sel;
628         
629         for (ViewList::const_iterator i = _views.begin(); i != _views.end(); ++i) {
630                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
631                 if (cv && cv->selected()) {
632                         sel.push_back (cv);
633                 }
634         }
635
636         return sel;
637 }
638
639 ContentList
640 Timeline::selected_content () const
641 {
642         ContentList sel;
643         ContentViewList views = selected_views ();
644         
645         for (ContentViewList::const_iterator i = views.begin(); i != views.end(); ++i) {
646                 sel.push_back ((*i)->content ());
647         }
648
649         return sel;
650 }