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