8c90c3d8c1aa33768a70a93449f507c4f3efd079
[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 "timeline.h"
25 #include "wx_util.h"
26 #include "playlist.h"
27
28 using std::list;
29 using std::cout;
30 using std::max;
31 using boost::shared_ptr;
32 using boost::weak_ptr;
33 using boost::dynamic_pointer_cast;
34 using boost::bind;
35
36 class View
37 {
38 public:
39         View (Timeline& t)
40                 : _timeline (t)
41         {
42
43         }
44                 
45         void paint (wxGraphicsContext* g)
46         {
47                 _last_paint_bbox = bbox ();
48                 do_paint (g);
49         }
50         
51         void force_redraw ()
52         {
53                 _timeline.force_redraw (_last_paint_bbox);
54                 _timeline.force_redraw (bbox ());
55         }
56
57         virtual Rect bbox () const = 0;
58
59 protected:
60         virtual void do_paint (wxGraphicsContext *) = 0;
61         
62         int time_x (Time t) const
63         {
64                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
65         }
66         
67         Timeline& _timeline;
68
69 private:
70         Rect _last_paint_bbox;
71 };
72
73 class ContentView : public View
74 {
75 public:
76         ContentView (Timeline& tl, shared_ptr<Content> c, int t)
77                 : View (tl)
78                 , _content (c)
79                 , _track (t)
80                 , _selected (false)
81         {
82                 _content_connection = c->Changed.connect (bind (&ContentView::content_changed, this, _2));
83         }
84
85         Rect bbox () const
86         {
87                 shared_ptr<const Film> film = _timeline.film ();
88                 shared_ptr<const Content> content = _content.lock ();
89                 if (!film || !content) {
90                         return Rect ();
91                 }
92                 
93                 return Rect (
94                         time_x (content->start ()) - 8,
95                         y_pos (_track) - 8,
96                         content->length () * _timeline.pixels_per_time_unit() + 16,
97                         _timeline.track_height() + 16
98                         );
99         }
100
101         void set_selected (bool s) {
102                 _selected = s;
103                 force_redraw ();
104         }
105         
106         bool selected () const {
107                 return _selected;
108         }
109
110         weak_ptr<Content> content () const {
111                 return _content;
112         }
113
114         virtual wxString type () const = 0;
115         virtual wxColour colour () const = 0;
116         
117 private:
118
119         void do_paint (wxGraphicsContext* gc)
120         {
121                 shared_ptr<const Film> film = _timeline.film ();
122                 shared_ptr<const Content> content = _content.lock ();
123                 if (!film || !content) {
124                         return;
125                 }
126
127                 Time const start = content->start ();
128                 Time const len = content->length ();
129
130                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
131
132                 gc->SetPen (*wxBLACK_PEN);
133                 
134 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
135                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
136                 if (_selected) {
137                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
138                 } else {
139                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
140                 }
141 #else                   
142                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
143                 if (_selected) {
144                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
145                 } else {
146                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
147                 }
148 #endif
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 (content->file().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)
177         {
178                 if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
179                         force_redraw ();
180                 }
181         }
182
183         boost::weak_ptr<Content> _content;
184         int _track;
185         bool _selected;
186
187         boost::signals2::scoped_connection _content_connection;
188 };
189
190 class AudioContentView : public ContentView
191 {
192 public:
193         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
194                 : ContentView (tl, c, t)
195         {}
196         
197 private:
198         wxString type () const
199         {
200                 return _("audio");
201         }
202
203         wxColour colour () const
204         {
205                 return wxColour (149, 121, 232, 255);
206         }
207 };
208
209 class VideoContentView : public ContentView
210 {
211 public:
212         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
213                 : ContentView (tl, c, t)
214         {}
215
216 private:        
217
218         wxString type () const
219         {
220                 return _("video");
221         }
222
223         wxColour colour () const
224         {
225                 return wxColour (242, 92, 120, 255);
226         }
227 };
228
229 class TimeAxisView : public View
230 {
231 public:
232         TimeAxisView (Timeline& tl, int y)
233                 : View (tl)
234                 , _y (y)
235         {}
236
237         void do_paint (wxGraphicsContext* gc)
238         {
239 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
240                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
241 #else               
242                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
243 #endif              
244                 
245                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
246                 if (mark_interval > 5) {
247                         mark_interval -= mark_interval % 5;
248                 }
249                 if (mark_interval > 10) {
250                         mark_interval -= mark_interval % 10;
251                 }
252                 if (mark_interval > 60) {
253                         mark_interval -= mark_interval % 60;
254                 }
255                 if (mark_interval > 3600) {
256                         mark_interval -= mark_interval % 3600;
257                 }
258                 
259                 if (mark_interval < 1) {
260                         mark_interval = 1;
261                 }
262
263                 wxGraphicsPath path = gc->CreatePath ();
264                 path.MoveToPoint (_timeline.x_offset(), _y);
265                 path.AddLineToPoint (_timeline.width(), _y);
266                 gc->StrokePath (path);
267
268                 Time t = 0;
269                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
270                         wxGraphicsPath path = gc->CreatePath ();
271                         path.MoveToPoint (time_x (t), _y - 4);
272                         path.AddLineToPoint (time_x (t), _y + 4);
273                         gc->StrokePath (path);
274
275                         int tc = t / TIME_HZ;
276                         int const h = tc / 3600;
277                         tc -= h * 3600;
278                         int const m = tc / 60;
279                         tc -= m * 60;
280                         int const s = tc;
281                         
282                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
283                         wxDouble str_width;
284                         wxDouble str_height;
285                         wxDouble str_descent;
286                         wxDouble str_leading;
287                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
288                         
289                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
290                         if ((tx + str_width) < _timeline.width()) {
291                                 gc->DrawText (str, time_x (t), _y + 16);
292                         }
293                         
294                         t += mark_interval * TIME_HZ;
295                 }
296         }
297
298         Rect bbox () const
299         {
300                 return Rect (0, _y - 4, _timeline.width(), 24);
301         }
302
303 private:
304         int _y;
305 };
306
307 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
308         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
309         , _film (film)
310         , _pixels_per_time_unit (0)
311         , _left_down (false)
312         , _down_view_start (0)
313         , _first_move (false)
314 {
315         SetDoubleBuffered (true);
316
317         setup_pixels_per_time_unit ();
318         
319         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
320         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
321         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
322         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
323         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
324
325         SetMinSize (wxSize (640, tracks() * track_height() + 96));
326
327         playlist_changed ();
328
329         _playlist_connection = film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
330 }
331
332 void
333 Timeline::paint (wxPaintEvent &)
334 {
335         wxPaintDC dc (this);
336
337         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
338         if (!gc) {
339                 return;
340         }
341
342         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
343
344         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
345                 (*i)->paint (gc);
346         }
347
348         delete gc;
349 }
350
351 void
352 Timeline::playlist_changed ()
353 {
354         shared_ptr<const Film> fl = _film.lock ();
355         if (!fl) {
356                 return;
357         }
358
359         _views.clear ();
360
361         Playlist::ContentList content = fl->playlist()->content ();
362
363         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
364                 if (dynamic_pointer_cast<VideoContent> (*i)) {
365                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
366                 }
367                 if (dynamic_pointer_cast<AudioContent> (*i)) {
368                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
369                 }
370         }
371
372         _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
373                 
374         Refresh ();
375 }
376
377 int
378 Timeline::tracks () const
379 {
380         /* XXX */
381         return 2;
382 }
383
384 void
385 Timeline::setup_pixels_per_time_unit ()
386 {
387         shared_ptr<const Film> film = _film.lock ();
388         if (!film) {
389                 return;
390         }
391
392         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
393 }
394
395 void
396 Timeline::left_down (wxMouseEvent& ev)
397 {
398         list<shared_ptr<View> >::iterator i = _views.begin();
399         Position const p (ev.GetX(), ev.GetY());
400         while (i != _views.end() && !(*i)->bbox().contains (p)) {
401                 ++i;
402         }
403
404         _down_view.reset ();
405
406         if (i != _views.end ()) {
407                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
408                 if (cv) {
409                         _down_view = cv;
410                         shared_ptr<Content> c = cv->content().lock();
411                         assert (c);
412                         _down_view_start = c->start ();
413                 }
414         }
415
416         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
417                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
418                 if (cv) {
419                         cv->set_selected (i == j);
420                 }
421         }
422
423         _left_down = true;
424         _down_point = ev.GetPosition ();
425         _first_move = false;
426 }
427
428 void
429 Timeline::left_up (wxMouseEvent &)
430 {
431         _left_down = false;
432 }
433
434 void
435 Timeline::mouse_moved (wxMouseEvent& ev)
436 {
437         if (!_left_down) {
438                 return;
439         }
440
441         wxPoint const p = ev.GetPosition();
442
443         if (!_first_move) {
444                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
445                 if (dist < 8) {
446                         return;
447                 }
448                 _first_move = true;
449         }
450
451         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
452         if (_down_view) {
453                 shared_ptr<Content> c = _down_view->content().lock();
454                 if (c) {
455                         c->set_start (max (static_cast<Time> (0), _down_view_start + time_diff));
456                 }
457         }
458 }
459
460 void
461 Timeline::force_redraw (Rect const & r)
462 {
463         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
464 }
465
466 shared_ptr<const Film>
467 Timeline::film () const
468 {
469         return _film.lock ();
470 }
471
472 void
473 Timeline::resized (wxSizeEvent &)
474 {
475         setup_pixels_per_time_unit ();
476 }