Merge branch '1.0' of /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 "film.h"
24 #include "film_editor.h"
25 #include "timeline.h"
26 #include "wx_util.h"
27 #include "lib/playlist.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
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, int t)
78                 : View (tl)
79                 , _content (c)
80                 , _track (t)
81                 , _selected (false)
82         {
83                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2));
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         weak_ptr<Content> content () const {
112                 return _content;
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> content = _content.lock ();
132                 if (!film || !content) {
133                         return;
134                 }
135
136                 Time const start = content->start ();
137                 Time const len = content->length ();
138
139                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
140
141                 gc->SetPen (*wxBLACK_PEN);
142                 
143 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
144                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
145                 if (_selected) {
146                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
147                 } else {
148                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
149                 }
150 #else                   
151                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
152                 if (_selected) {
153                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
154                 } else {
155                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
156                 }
157 #endif
158                 
159                 wxGraphicsPath path = gc->CreatePath ();
160                 path.MoveToPoint    (time_x (start),       y_pos (_track) + 4);
161                 path.AddLineToPoint (time_x (start + len), y_pos (_track) + 4);
162                 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1) - 4);
163                 path.AddLineToPoint (time_x (start),       y_pos (_track + 1) - 4);
164                 path.AddLineToPoint (time_x (start),       y_pos (_track) + 4);
165                 gc->StrokePath (path);
166                 gc->FillPath (path);
167
168                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
169                 wxDouble name_width;
170                 wxDouble name_height;
171                 wxDouble name_descent;
172                 wxDouble name_leading;
173                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
174                 
175                 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
176                 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
177                 gc->ResetClip ();
178         }
179
180         int y_pos (int t) const
181         {
182                 return _timeline.tracks_position().y + t * _timeline.track_height();
183         }
184
185         void content_changed (int p)
186         {
187                 if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
188                         force_redraw ();
189                 }
190         }
191
192         boost::weak_ptr<Content> _content;
193         int _track;
194         bool _selected;
195
196         boost::signals2::scoped_connection _content_connection;
197 };
198
199 class AudioContentView : public ContentView
200 {
201 public:
202         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
203                 : ContentView (tl, c, t)
204         {}
205         
206 private:
207         wxString type () const
208         {
209                 return _("audio");
210         }
211
212         wxColour colour () const
213         {
214                 return wxColour (149, 121, 232, 255);
215         }
216 };
217
218 class VideoContentView : public ContentView
219 {
220 public:
221         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
222                 : ContentView (tl, c, t)
223         {}
224
225 private:        
226
227         wxString type () const
228         {
229                 return _("video");
230         }
231
232         wxColour colour () const
233         {
234                 return wxColour (242, 92, 120, 255);
235         }
236 };
237
238 class TimeAxisView : public View
239 {
240 public:
241         TimeAxisView (Timeline& tl, int y)
242                 : View (tl)
243                 , _y (y)
244         {}
245         
246         dcpomatic::Rect<int> bbox () const
247         {
248                 return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
249         }
250
251         void set_y (int y)
252         {
253                 _y = y;
254                 force_redraw ();
255         }
256
257 private:        
258
259         void do_paint (wxGraphicsContext* gc)
260         {
261 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
262                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
263 #else               
264                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
265 #endif              
266                 
267                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
268                 if (mark_interval > 5) {
269                         mark_interval -= mark_interval % 5;
270                 }
271                 if (mark_interval > 10) {
272                         mark_interval -= mark_interval % 10;
273                 }
274                 if (mark_interval > 60) {
275                         mark_interval -= mark_interval % 60;
276                 }
277                 if (mark_interval > 3600) {
278                         mark_interval -= mark_interval % 3600;
279                 }
280                 
281                 if (mark_interval < 1) {
282                         mark_interval = 1;
283                 }
284
285                 wxGraphicsPath path = gc->CreatePath ();
286                 path.MoveToPoint (_timeline.x_offset(), _y);
287                 path.AddLineToPoint (_timeline.width(), _y);
288                 gc->StrokePath (path);
289
290                 Time t = 0;
291                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
292                         wxGraphicsPath path = gc->CreatePath ();
293                         path.MoveToPoint (time_x (t), _y - 4);
294                         path.AddLineToPoint (time_x (t), _y + 4);
295                         gc->StrokePath (path);
296
297                         int tc = t / TIME_HZ;
298                         int const h = tc / 3600;
299                         tc -= h * 3600;
300                         int const m = tc / 60;
301                         tc -= m * 60;
302                         int const s = tc;
303                         
304                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
305                         wxDouble str_width;
306                         wxDouble str_height;
307                         wxDouble str_descent;
308                         wxDouble str_leading;
309                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
310                         
311                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
312                         if ((tx + str_width) < _timeline.width()) {
313                                 gc->DrawText (str, time_x (t), _y + 16);
314                         }
315                         
316                         t += mark_interval * TIME_HZ;
317                 }
318         }
319
320 private:
321         int _y;
322 };
323
324 Timeline::Timeline (wxWindow* parent, FilmEditor* ed, shared_ptr<Film> film)
325         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
326         , _film_editor (ed)
327         , _film (film)
328         , _time_axis_view (new TimeAxisView (*this, 32))
329         , _tracks (0)
330         , _pixels_per_time_unit (0)
331         , _left_down (false)
332         , _down_view_start (0)
333         , _first_move (false)
334 {
335 #ifndef __WXOSX__
336         SetDoubleBuffered (true);
337 #endif  
338
339         setup_pixels_per_time_unit ();
340         
341         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
342         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
343         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
344         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
345         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
346
347         playlist_changed ();
348
349         SetMinSize (wxSize (640, tracks() * track_height() + 96));
350
351         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
352
353         _views.push_back (_time_axis_view);
354 }
355
356 void
357 Timeline::paint (wxPaintEvent &)
358 {
359         wxPaintDC dc (this);
360
361         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
362         if (!gc) {
363                 return;
364         }
365
366         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
367
368         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
369                 (*i)->paint (gc);
370         }
371
372         delete gc;
373 }
374
375 void
376 Timeline::playlist_changed ()
377 {
378         shared_ptr<const Film> fl = _film.lock ();
379         if (!fl) {
380                 return;
381         }
382
383         _views.clear ();
384
385         Playlist::ContentList content = fl->playlist()->content ();
386
387         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
388                 if (dynamic_pointer_cast<VideoContent> (*i)) {
389                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
390                 }
391                 if (dynamic_pointer_cast<AudioContent> (*i)) {
392                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
393                 }
394         }
395
396         assign_tracks ();
397         Refresh ();
398 }
399
400 void
401 Timeline::assign_tracks ()
402 {
403         for (list<shared_ptr<View> >::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 (list<shared_ptr<View> >::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().lock ();
418                 assert (acv_content);
419                 
420                 int t = 1;
421                 while (1) {
422                         list<shared_ptr<View> >::iterator j = _views.begin();
423                         while (j != _views.end()) {
424                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
425                                 if (!test) {
426                                         ++j;
427                                         continue;
428                                 }
429                                 
430                                 shared_ptr<Content> test_content = test->content().lock ();
431                                 assert (test_content);
432                                         
433                                 if (test && test->track() == t) {
434                                         if ((acv_content->start() <= test_content->start() && test_content->start() <= acv_content->end()) ||
435                                             (acv_content->start() <= test_content->end()   && test_content->end()   <= acv_content->end())) {
436                                                 /* we have an overlap on track `t' */
437                                                 ++t;
438                                                 break;
439                                         }
440                                 }
441                                 
442                                 ++j;
443                         }
444
445                         if (j == _views.end ()) {
446                                 /* no overlap on `t' */
447                                 break;
448                         }
449                 }
450
451                 acv->set_track (t);
452                 _tracks = max (_tracks, t + 1);
453         }
454
455         _time_axis_view->set_y (tracks() * track_height() + 32);
456 }
457
458 int
459 Timeline::tracks () const
460 {
461         return _tracks;
462 }
463
464 void
465 Timeline::setup_pixels_per_time_unit ()
466 {
467         shared_ptr<const Film> film = _film.lock ();
468         if (!film) {
469                 return;
470         }
471
472         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
473 }
474
475 void
476 Timeline::left_down (wxMouseEvent& ev)
477 {
478         list<shared_ptr<View> >::iterator i = _views.begin();
479         Position<int> const p (ev.GetX(), ev.GetY());
480         while (i != _views.end() && !(*i)->bbox().contains (p)) {
481                 ++i;
482         }
483
484         _down_view.reset ();
485
486         if (i != _views.end ()) {
487                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
488                 if (cv) {
489                         _down_view = cv;
490                         shared_ptr<Content> c = cv->content().lock();
491                         assert (c);
492                         _down_view_start = c->start ();
493                 }
494         }
495
496         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
497                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
498                 if (cv) {
499                         cv->set_selected (i == j);
500                         if (i == j) {
501                                 _film_editor->set_selection (cv->content ());
502                         }
503                 }
504         }
505
506         _left_down = true;
507         _down_point = ev.GetPosition ();
508         _first_move = false;
509 }
510
511 void
512 Timeline::left_up (wxMouseEvent &)
513 {
514         _left_down = false;
515 }
516
517 void
518 Timeline::mouse_moved (wxMouseEvent& ev)
519 {
520         if (!_left_down) {
521                 return;
522         }
523
524         wxPoint const p = ev.GetPosition();
525
526         if (!_first_move) {
527                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
528                 if (dist < 8) {
529                         return;
530                 }
531                 _first_move = true;
532         }
533
534         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
535         if (_down_view) {
536                 shared_ptr<Content> c = _down_view->content().lock();
537                 if (c) {
538                         c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
539
540                         shared_ptr<Film> film = _film.lock ();
541                         assert (film);
542                         film->set_sequence_video (false);
543                 }
544         }
545 }
546
547 void
548 Timeline::force_redraw (dcpomatic::Rect<int> const & r)
549 {
550         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
551 }
552
553 shared_ptr<const Film>
554 Timeline::film () const
555 {
556         return _film.lock ();
557 }
558
559 void
560 Timeline::resized (wxSizeEvent &)
561 {
562         setup_pixels_per_time_unit ();
563 }