Allow drag of content; fix up some problems with timings.
[dcpomatic.git] / src / wx / timeline.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <list>
23 #include <wx/graphics.h>
24 #include <boost/weak_ptr.hpp>
25 #include "film.h"
26 #include "timeline.h"
27 #include "wx_util.h"
28 #include "playlist.h"
29
30 using std::list;
31 using std::cout;
32 using std::max;
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
36 using boost::bind;
37
38 class View
39 {
40 public:
41         View (Timeline& t)
42                 : _timeline (t)
43         {
44
45         }
46                 
47         virtual void paint (wxGraphicsContext *) = 0;
48         virtual Rect bbox () const = 0;
49
50         void force_redraw ()
51         {
52                 _timeline.force_redraw (bbox ());
53         }
54
55 protected:
56         int time_x (Time t) const
57         {
58                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
59         }
60         
61         Timeline& _timeline;
62 };
63
64 class ContentView : public View
65 {
66 public:
67         ContentView (Timeline& tl, shared_ptr<Content> c, int t)
68                 : View (tl)
69                 , _content (c)
70                 , _track (t)
71                 , _selected (false)
72         {
73
74         }
75
76         void paint (wxGraphicsContext* gc)
77         {
78                 shared_ptr<const Film> film = _timeline.film ();
79                 shared_ptr<const Content> content = _content.lock ();
80                 if (!film || !content) {
81                         return;
82                 }
83
84                 Time const start = content->start ();
85                 Time const len = content->length ();
86
87                 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
88
89                 gc->SetPen (*wxBLACK_PEN);
90                 
91 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
92                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
93                 if (_selected) {
94                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
95                 } else {
96                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
97                 }
98 #else                   
99                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
100                 if (_selected) {
101                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
102                 } else {
103                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
104                 }
105 #endif
106                 
107                 wxGraphicsPath path = gc->CreatePath ();
108                 path.MoveToPoint    (time_x (start),       y_pos (_track));
109                 path.AddLineToPoint (time_x (start + len), y_pos (_track));
110                 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1));
111                 path.AddLineToPoint (time_x (start),       y_pos (_track + 1));
112                 path.AddLineToPoint (time_x (start),       y_pos (_track));
113                 gc->StrokePath (path);
114                 gc->FillPath (path);
115
116                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
117                 wxDouble name_width;
118                 wxDouble name_height;
119                 wxDouble name_descent;
120                 wxDouble name_leading;
121                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
122                 
123                 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
124                 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
125                 gc->ResetClip ();
126         }
127
128         Rect bbox () const
129         {
130                 shared_ptr<const Film> film = _timeline.film ();
131                 shared_ptr<const Content> content = _content.lock ();
132                 if (!film || !content) {
133                         return Rect ();
134                 }
135                 
136                 return Rect (
137                         time_x (content->start ()),
138                         y_pos (_track),
139                         content->length () * _timeline.pixels_per_time_unit(),
140                         _timeline.track_height()
141                         );
142         }
143
144         void set_selected (bool s) {
145                 _selected = s;
146                 force_redraw ();
147         }
148         
149         bool selected () const {
150                 return _selected;
151         }
152
153         weak_ptr<Content> content () const {
154                 return _content;
155         }
156
157         virtual wxString type () const = 0;
158         virtual wxColour colour () const = 0;
159         
160 private:
161         
162         int y_pos (int t) const
163         {
164                 return _timeline.tracks_position().y + t * _timeline.track_height();
165         }
166
167         boost::weak_ptr<Content> _content;
168         int _track;
169         bool _selected;
170 };
171
172 class AudioContentView : public ContentView
173 {
174 public:
175         AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
176                 : ContentView (tl, c, t)
177         {}
178         
179 private:
180         wxString type () const
181         {
182                 return _("audio");
183         }
184
185         wxColour colour () const
186         {
187                 return wxColour (149, 121, 232, 255);
188         }
189 };
190
191 class VideoContentView : public ContentView
192 {
193 public:
194         VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
195                 : ContentView (tl, c, t)
196         {}
197
198 private:        
199
200         wxString type () const
201         {
202                 return _("video");
203         }
204
205         wxColour colour () const
206         {
207                 return wxColour (242, 92, 120, 255);
208         }
209 };
210
211 class TimeAxisView : public View
212 {
213 public:
214         TimeAxisView (Timeline& tl, int y)
215                 : View (tl)
216                 , _y (y)
217         {}
218
219         void paint (wxGraphicsContext* gc)
220         {
221 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
222                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
223 #else               
224                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
225 #endif              
226                 
227                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
228                 if (mark_interval > 5) {
229                         mark_interval -= mark_interval % 5;
230                 }
231                 if (mark_interval > 10) {
232                         mark_interval -= mark_interval % 10;
233                 }
234                 if (mark_interval > 60) {
235                         mark_interval -= mark_interval % 60;
236                 }
237                 if (mark_interval > 3600) {
238                         mark_interval -= mark_interval % 3600;
239                 }
240                 
241                 if (mark_interval < 1) {
242                         mark_interval = 1;
243                 }
244
245                 wxGraphicsPath path = gc->CreatePath ();
246                 path.MoveToPoint (_timeline.x_offset(), _y);
247                 path.AddLineToPoint (_timeline.width(), _y);
248                 gc->StrokePath (path);
249
250                 Time t = 0;
251                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
252                         wxGraphicsPath path = gc->CreatePath ();
253                         path.MoveToPoint (time_x (t), _y - 4);
254                         path.AddLineToPoint (time_x (t), _y + 4);
255                         gc->StrokePath (path);
256
257                         int tc = t / TIME_HZ;
258                         int const h = tc / 3600;
259                         tc -= h * 3600;
260                         int const m = tc / 60;
261                         tc -= m * 60;
262                         int const s = tc;
263                         
264                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
265                         wxDouble str_width;
266                         wxDouble str_height;
267                         wxDouble str_descent;
268                         wxDouble str_leading;
269                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
270                         
271                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
272                         if ((tx + str_width) < _timeline.width()) {
273                                 gc->DrawText (str, time_x (t), _y + 16);
274                         }
275                         
276                         t += mark_interval * TIME_HZ;
277                 }
278         }
279
280         Rect bbox () const
281         {
282                 return Rect (0, _y - 4, _timeline.width(), 24);
283         }
284
285 private:
286         int _y;
287 };
288
289 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
290         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
291         , _film (film)
292         , _pixels_per_time_unit (0)
293         , _left_down (false)
294         , _down_view_start (0)
295         , _first_move (false)
296 {
297         SetDoubleBuffered (true);
298
299         setup_pixels_per_time_unit ();
300         
301         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
302         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
303         Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
304         Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
305         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
306
307         SetMinSize (wxSize (640, tracks() * track_height() + 96));
308
309         playlist_changed ();
310
311         film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
312         film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_changed, this));
313 }
314
315 void
316 Timeline::paint (wxPaintEvent &)
317 {
318         wxPaintDC dc (this);
319
320         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
321         if (!gc) {
322                 return;
323         }
324
325         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
326
327         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
328                 (*i)->paint (gc);
329         }
330
331         delete gc;
332 }
333
334 void
335 Timeline::playlist_changed ()
336 {
337         shared_ptr<const Film> fl = _film.lock ();
338         if (!fl) {
339                 return;
340         }
341
342         _views.clear ();
343
344         Playlist::ContentList content = fl->playlist()->content ();
345
346         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
347                 if (dynamic_pointer_cast<VideoContent> (*i)) {
348                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
349                 }
350                 if (dynamic_pointer_cast<AudioContent> (*i)) {
351                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
352                 }
353         }
354
355         _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
356                 
357         Refresh ();
358 }
359
360 int
361 Timeline::tracks () const
362 {
363         /* XXX */
364         return 2;
365 }
366
367 void
368 Timeline::setup_pixels_per_time_unit ()
369 {
370         shared_ptr<const Film> film = _film.lock ();
371         if (!film) {
372                 return;
373         }
374
375         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
376 }
377
378 void
379 Timeline::left_down (wxMouseEvent& ev)
380 {
381         list<shared_ptr<View> >::iterator i = _views.begin();
382         Position const p (ev.GetX(), ev.GetY());
383         while (i != _views.end() && !(*i)->bbox().contains (p)) {
384                 ++i;
385         }
386
387         _down_view.reset ();
388
389         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
390                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
391                 if (cv) {
392                         _down_view = cv;
393                         shared_ptr<Content> c = cv->content().lock();
394                         assert (c);
395                         _down_view_start = c->start ();
396                         cv->set_selected (i == j);
397                 }
398         }
399
400         _left_down = true;
401         _down_point = ev.GetPosition ();
402         _first_move = false;
403 }
404
405 void
406 Timeline::left_up (wxMouseEvent &)
407 {
408         _left_down = false;
409 }
410
411 void
412 Timeline::mouse_moved (wxMouseEvent& ev)
413 {
414         if (!_left_down) {
415                 return;
416         }
417
418         wxPoint const p = ev.GetPosition();
419
420         if (!_first_move) {
421                 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
422                 if (dist < 8) {
423                         return;
424                 }
425                 _first_move = true;
426         }
427
428         Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
429         if (_down_view) {
430                 shared_ptr<Content> c = _down_view->content().lock();
431                 if (c) {
432                         _down_view->force_redraw ();
433                         c->set_start (_down_view_start + time_diff);
434                         _down_view->force_redraw ();
435                 }
436         }
437 }
438
439 void
440 Timeline::force_redraw (Rect const & r)
441 {
442         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
443 }
444
445 shared_ptr<const Film>
446 Timeline::film () const
447 {
448         return _film.lock ();
449 }
450
451 void
452 Timeline::resized (wxSizeEvent &)
453 {
454         setup_pixels_per_time_unit ();
455 }