2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <wx/graphics.h>
29 using boost::shared_ptr;
30 using boost::dynamic_pointer_cast;
42 virtual void paint (wxGraphicsContext *) = 0;
43 virtual Rect bbox () const = 0;
46 int time_x (Time t) const
48 return _timeline.tracks_position().x + t * _timeline.pixels_per_second();
54 class ContentView : public View
57 ContentView (Timeline& tl, boost::shared_ptr<const Content> c, Time s, int t)
67 void paint (wxGraphicsContext* gc)
69 shared_ptr<const Content> content = _content.lock ();
74 Time const len = content->temporal_length ();
76 gc->SetPen (*wxBLACK_PEN);
78 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
79 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
81 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (200, 200, 200), wxBRUSHSTYLE_SOLID));
83 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
86 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
88 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (200, 200, 200), wxSOLID));
90 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
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);
103 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
105 wxDouble name_height;
106 wxDouble name_descent;
107 wxDouble name_leading;
108 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
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);
117 shared_ptr<const Content> content = _content.lock ();
122 return Rect (time_x (_start), y_pos (_track), content->temporal_length() * _timeline.pixels_per_second(), _timeline.track_height());
125 void set_selected (bool s) {
127 _timeline.force_redraw (bbox ());
130 bool selected () const {
134 virtual wxString type () const = 0;
135 virtual wxColour colour () const = 0;
139 int y_pos (int t) const
141 return _timeline.tracks_position().y + t * _timeline.track_height();
144 boost::weak_ptr<const Content> _content;
150 class AudioContentView : public ContentView
153 AudioContentView (Timeline& tl, boost::shared_ptr<const Content> c, Time s, int t)
154 : ContentView (tl, c, s, t)
158 wxString type () const
163 wxColour colour () const
165 return wxColour (149, 121, 232, 255);
169 class VideoContentView : public ContentView
172 VideoContentView (Timeline& tl, boost::shared_ptr<const Content> c, Time s, int t)
173 : ContentView (tl, c, s, t)
178 wxString type () const
183 wxColour colour () const
185 return wxColour (242, 92, 120, 255);
189 class TimeAxisView : public View
192 TimeAxisView (Timeline& tl, int y)
197 void paint (wxGraphicsContext* gc)
199 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
200 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
202 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
205 int mark_interval = rint (128 / _timeline.pixels_per_second ());
206 if (mark_interval > 5) {
207 mark_interval -= mark_interval % 5;
209 if (mark_interval > 10) {
210 mark_interval -= mark_interval % 10;
212 if (mark_interval > 60) {
213 mark_interval -= mark_interval % 60;
215 if (mark_interval > 3600) {
216 mark_interval -= mark_interval % 3600;
219 if (mark_interval < 1) {
223 wxGraphicsPath path = gc->CreatePath ();
224 path.MoveToPoint (_timeline.x_offset(), _y);
225 path.AddLineToPoint (_timeline.width(), _y);
226 gc->StrokePath (path);
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);
236 int const h = tc / 3600;
238 int const m = tc / 60;
242 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
245 wxDouble str_descent;
246 wxDouble str_leading;
247 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
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);
259 return Rect (0, _y - 4, _timeline.width(), 24);
266 Timeline::Timeline (wxWindow* parent, shared_ptr<Playlist> pl)
269 , _pixels_per_second (0)
271 SetDoubleBuffered (true);
273 setup_pixels_per_second ();
275 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
276 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
278 SetMinSize (wxSize (640, tracks() * track_height() + 96));
282 pl->Changed.connect (bind (&Timeline::playlist_changed, this));
283 pl->ContentChanged.connect (bind (&Timeline::playlist_changed, this));
287 Timeline::paint (wxPaintEvent &)
291 shared_ptr<Playlist> pl = _playlist.lock ();
296 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
301 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
304 _pixels_per_second = (width() - x_offset() * 2) / (pl->content_length() / pl->video_frame_rate());
306 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
314 Timeline::playlist_changed ()
316 shared_ptr<Playlist> pl = _playlist.lock ();
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 ();
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) {
339 time += (*i)->temporal_length ();
343 _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
349 Timeline::tracks () const
351 shared_ptr<Playlist> pl = _playlist.lock ();
356 if (pl->audio_from() == Playlist::AUDIO_FFMPEG) {
360 return 1 + max (size_t (1), pl->audio().size());
364 Timeline::setup_pixels_per_second ()
366 shared_ptr<Playlist> pl = _playlist.lock ();
372 _pixels_per_second = (width() - x_offset() * 2) / (pl->content_length() / pl->video_frame_rate());
376 Timeline::left_down (wxMouseEvent& ev)
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)) {
384 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
385 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
387 cv->set_selected (i == j);
393 Timeline::force_redraw (Rect const & r)
395 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);