Fix time axis view position.
[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 Rect 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         Rect _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         Rect 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 Rect ();
92                 }
93                 
94                 return Rect (
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         Rect bbox () const
247         {
248                 return Rect (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         SetDoubleBuffered (true);
336
337         setup_pixels_per_time_unit ();
338         
339         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
340         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
341         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 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         _views.push_back (_time_axis_view);
352 }
353
354 void
355 Timeline::paint (wxPaintEvent &)
356 {
357         wxPaintDC dc (this);
358
359         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
360         if (!gc) {
361                 return;
362         }
363
364         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
365
366         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
367                 (*i)->paint (gc);
368         }
369
370         delete gc;
371 }
372
373 void
374 Timeline::playlist_changed ()
375 {
376         shared_ptr<const Film> fl = _film.lock ();
377         if (!fl) {
378                 return;
379         }
380
381         _views.clear ();
382
383         Playlist::ContentList content = fl->playlist()->content ();
384
385         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
386                 if (dynamic_pointer_cast<VideoContent> (*i)) {
387                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
388                 }
389                 if (dynamic_pointer_cast<AudioContent> (*i)) {
390                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 0)));
391                 }
392         }
393
394         assign_tracks ();
395         Refresh ();
396 }
397
398 void
399 Timeline::assign_tracks ()
400 {
401         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
402                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
403                 if (cv) {
404                         cv->set_track (0);
405                         _tracks = 1;
406                 }
407         }
408
409         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
410                 shared_ptr<AudioContentView> acv = dynamic_pointer_cast<AudioContentView> (*i);
411                 if (!acv) {
412                         continue;
413                 }
414         
415                 shared_ptr<Content> acv_content = acv->content().lock ();
416                 assert (acv_content);
417                 
418                 int t = 1;
419                 while (1) {
420                         list<shared_ptr<View> >::iterator j = _views.begin();
421                         while (j != _views.end()) {
422                                 shared_ptr<AudioContentView> test = dynamic_pointer_cast<AudioContentView> (*j);
423                                 if (!test) {
424                                         ++j;
425                                         continue;
426                                 }
427                                 
428                                 shared_ptr<Content> test_content = test->content().lock ();
429                                 assert (test_content);
430                                         
431                                 if (test && test->track() == t) {
432                                         if ((acv_content->start() <= test_content->start() && test_content->start() <= acv_content->end()) ||
433                                             (acv_content->start() <= test_content->end()   && test_content->end()   <= acv_content->end())) {
434                                                 /* we have an overlap on track `t' */
435                                                 ++t;
436                                                 break;
437                                         }
438                                 }
439                                 
440                                 ++j;
441                         }
442
443                         if (j == _views.end ()) {
444                                 /* no overlap on `t' */
445                                 break;
446                         }
447                 }
448
449                 acv->set_track (t);
450                 _tracks = max (_tracks, t + 1);
451         }
452
453         _time_axis_view->set_y (tracks() * track_height() + 32);
454 }
455
456 int
457 Timeline::tracks () const
458 {
459         return _tracks;
460 }
461
462 void
463 Timeline::setup_pixels_per_time_unit ()
464 {
465         shared_ptr<const Film> film = _film.lock ();
466         if (!film) {
467                 return;
468         }
469
470         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
471 }
472
473 void
474 Timeline::left_down (wxMouseEvent& ev)
475 {
476         list<shared_ptr<View> >::iterator i = _views.begin();
477         Position const p (ev.GetX(), ev.GetY());
478         while (i != _views.end() && !(*i)->bbox().contains (p)) {
479                 ++i;
480         }
481
482         _down_view.reset ();
483
484         if (i != _views.end ()) {
485                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
486                 if (cv) {
487                         _down_view = cv;
488                         shared_ptr<Content> c = cv->content().lock();
489                         assert (c);
490                         _down_view_start = c->start ();
491                 }
492         }
493
494         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
495                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
496                 if (cv) {
497                         cv->set_selected (i == j);
498                         if (i == j) {
499                                 _film_editor->set_selection (cv->content ());
500                         }
501                 }
502         }
503
504         _left_down = true;
505         _down_point = ev.GetPosition ();
506         _first_move = false;
507 }
508
509 void
510 Timeline::left_up (wxMouseEvent &)
511 {
512         _left_down = false;
513 }
514
515 void
516 Timeline::mouse_moved (wxMouseEvent& ev)
517 {
518         if (!_left_down) {
519                 return;
520         }
521
522         wxPoint const p = ev.GetPosition();
523
524         if (!_first_move) {
525                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
526                 if (dist < 8) {
527                         return;
528                 }
529                 _first_move = true;
530         }
531
532         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
533         if (_down_view) {
534                 shared_ptr<Content> c = _down_view->content().lock();
535                 if (c) {
536                         c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
537
538                         shared_ptr<Film> film = _film.lock ();
539                         assert (film);
540                         film->set_sequence_video (false);
541                 }
542         }
543 }
544
545 void
546 Timeline::force_redraw (Rect const & r)
547 {
548         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
549 }
550
551 shared_ptr<const Film>
552 Timeline::film () const
553 {
554         return _film.lock ();
555 }
556
557 void
558 Timeline::resized (wxSizeEvent &)
559 {
560         setup_pixels_per_time_unit ();
561 }