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