Various more hacks.
[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 "film.h"
25 #include "timeline.h"
26 #include "wx_util.h"
27 #include "playlist.h"
28
29 using std::list;
30 using std::cout;
31 using std::max;
32 using boost::shared_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         virtual void paint (wxGraphicsContext *) = 0;
46         virtual Rect bbox () const = 0;
47
48 protected:
49         int time_x (Time t) const
50         {
51                 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
52         }
53         
54         Timeline& _timeline;
55 };
56
57 class ContentView : public View
58 {
59 public:
60         ContentView (Timeline& tl, shared_ptr<const Content> c, int t)
61                 : View (tl)
62                 , _content (c)
63                 , _track (t)
64                 , _selected (false)
65         {
66
67         }
68
69         void paint (wxGraphicsContext* gc)
70         {
71                 shared_ptr<const Film> film = _timeline.film ();
72                 shared_ptr<const Content> content = _content.lock ();
73                 if (!film || !content) {
74                         return;
75                 }
76
77                 Time const start = content->start ();
78                 Time const len = content->length (film);
79
80                 gc->SetPen (*wxBLACK_PEN);
81                 
82 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
83                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
84                 if (_selected) {
85                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (200, 200, 200), wxBRUSHSTYLE_SOLID));
86                 } else {
87                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
88                 }
89 #else                   
90                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
91                 if (_selected) {
92                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (200, 200, 200), wxSOLID));
93                 } else {
94                         gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
95                 }
96 #endif
97                 
98                 wxGraphicsPath path = gc->CreatePath ();
99                 path.MoveToPoint    (time_x (start),       y_pos (_track));
100                 path.AddLineToPoint (time_x (start + len), y_pos (_track));
101                 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1));
102                 path.AddLineToPoint (time_x (start),       y_pos (_track + 1));
103                 path.AddLineToPoint (time_x (start),       y_pos (_track));
104                 gc->StrokePath (path);
105                 gc->FillPath (path);
106
107                 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
108                 wxDouble name_width;
109                 wxDouble name_height;
110                 wxDouble name_descent;
111                 wxDouble name_leading;
112                 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
113                 
114                 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
115                 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
116                 gc->ResetClip ();
117         }
118
119         Rect bbox () const
120         {
121                 shared_ptr<const Film> film = _timeline.film ();
122                 shared_ptr<const Content> content = _content.lock ();
123                 if (!film || !content) {
124                         return Rect ();
125                 }
126                 
127                 return Rect (
128                         time_x (content->start ()),
129                         y_pos (_track),
130                         content->length (film) * _timeline.pixels_per_time_unit(),
131                         _timeline.track_height()
132                         );
133         }
134
135         void set_selected (bool s) {
136                 _selected = s;
137                 _timeline.force_redraw (bbox ());
138         }
139         
140         bool selected () const {
141                 return _selected;
142         }
143
144         virtual wxString type () const = 0;
145         virtual wxColour colour () const = 0;
146         
147 private:
148         
149         int y_pos (int t) const
150         {
151                 return _timeline.tracks_position().y + t * _timeline.track_height();
152         }
153
154         boost::weak_ptr<const Content> _content;
155         int _track;
156         bool _selected;
157 };
158
159 class AudioContentView : public ContentView
160 {
161 public:
162         AudioContentView (Timeline& tl, shared_ptr<const Content> c, int t)
163                 : ContentView (tl, c, t)
164         {}
165         
166 private:
167         wxString type () const
168         {
169                 return _("audio");
170         }
171
172         wxColour colour () const
173         {
174                 return wxColour (149, 121, 232, 255);
175         }
176 };
177
178 class VideoContentView : public ContentView
179 {
180 public:
181         VideoContentView (Timeline& tl, shared_ptr<const Content> c, int t)
182                 : ContentView (tl, c, t)
183         {}
184
185 private:        
186
187         wxString type () const
188         {
189                 return _("video");
190         }
191
192         wxColour colour () const
193         {
194                 return wxColour (242, 92, 120, 255);
195         }
196 };
197
198 class TimeAxisView : public View
199 {
200 public:
201         TimeAxisView (Timeline& tl, int y)
202                 : View (tl)
203                 , _y (y)
204         {}
205
206         void paint (wxGraphicsContext* gc)
207         {
208 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
209                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
210 #else               
211                 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
212 #endif              
213                 
214                 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
215                 if (mark_interval > 5) {
216                         mark_interval -= mark_interval % 5;
217                 }
218                 if (mark_interval > 10) {
219                         mark_interval -= mark_interval % 10;
220                 }
221                 if (mark_interval > 60) {
222                         mark_interval -= mark_interval % 60;
223                 }
224                 if (mark_interval > 3600) {
225                         mark_interval -= mark_interval % 3600;
226                 }
227                 
228                 if (mark_interval < 1) {
229                         mark_interval = 1;
230                 }
231
232                 wxGraphicsPath path = gc->CreatePath ();
233                 path.MoveToPoint (_timeline.x_offset(), _y);
234                 path.AddLineToPoint (_timeline.width(), _y);
235                 gc->StrokePath (path);
236
237                 Time t = 0;
238                 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
239                         wxGraphicsPath path = gc->CreatePath ();
240                         path.MoveToPoint (time_x (t), _y - 4);
241                         path.AddLineToPoint (time_x (t), _y + 4);
242                         gc->StrokePath (path);
243
244                         int tc = t / TIME_HZ;
245                         int const h = tc / 3600;
246                         tc -= h * 3600;
247                         int const m = tc / 60;
248                         tc -= m * 60;
249                         int const s = tc;
250                         
251                         wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
252                         wxDouble str_width;
253                         wxDouble str_height;
254                         wxDouble str_descent;
255                         wxDouble str_leading;
256                         gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
257                         
258                         int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
259                         if ((tx + str_width) < _timeline.width()) {
260                                 gc->DrawText (str, time_x (t), _y + 16);
261                         }
262                         
263                         t += mark_interval * TIME_HZ;
264                 }
265         }
266
267         Rect bbox () const
268         {
269                 return Rect (0, _y - 4, _timeline.width(), 24);
270         }
271
272 private:
273         int _y;
274 };
275
276 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
277         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
278         , _film (film)
279         , _pixels_per_time_unit (0)
280 {
281         SetDoubleBuffered (true);
282
283         setup_pixels_per_time_unit ();
284         
285         Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
286         Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
287         Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
288
289         SetMinSize (wxSize (640, tracks() * track_height() + 96));
290
291         playlist_changed ();
292
293         film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
294         film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_changed, this));
295 }
296
297 void
298 Timeline::paint (wxPaintEvent &)
299 {
300         wxPaintDC dc (this);
301
302         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
303         if (!gc) {
304                 return;
305         }
306
307         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
308
309         for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
310                 (*i)->paint (gc);
311         }
312
313         delete gc;
314 }
315
316 void
317 Timeline::playlist_changed ()
318 {
319         shared_ptr<const Film> fl = _film.lock ();
320         if (!fl) {
321                 return;
322         }
323
324         _views.clear ();
325
326         Playlist::ContentList content = fl->playlist()->content ();
327
328         for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
329                 if (dynamic_pointer_cast<VideoContent> (*i)) {
330                         _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
331                 }
332                 if (dynamic_pointer_cast<AudioContent> (*i)) {
333                         _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
334                 }
335         }
336
337         _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
338                 
339         Refresh ();
340 }
341
342 int
343 Timeline::tracks () const
344 {
345         /* XXX */
346         return 2;
347 }
348
349 void
350 Timeline::setup_pixels_per_time_unit ()
351 {
352         shared_ptr<const Film> film = _film.lock ();
353         if (!film) {
354                 return;
355         }
356
357         _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
358 }
359
360 void
361 Timeline::left_down (wxMouseEvent& ev)
362 {
363         list<shared_ptr<View> >::iterator i = _views.begin();
364         Position const p (ev.GetX(), ev.GetY());
365         while (i != _views.end() && !(*i)->bbox().contains (p)) {
366                 ++i;
367         }
368
369         for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
370                 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
371                 if (cv) {
372                         cv->set_selected (i == j);
373                 }
374         }
375 }
376
377 void
378 Timeline::force_redraw (Rect const & r)
379 {
380         RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
381 }
382
383 shared_ptr<const Film>
384 Timeline::film () const
385 {
386         return _film.lock ();
387 }
388
389 void
390 Timeline::resized (wxSizeEvent &)
391 {
392         setup_pixels_per_time_unit ();
393 }