1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
4 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
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.
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.
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.
23 #include <wx/graphics.h>
24 #include <boost/weak_ptr.hpp>
33 using boost::shared_ptr;
34 using boost::weak_ptr;
35 using boost::dynamic_pointer_cast;
47 void paint (wxGraphicsContext* g)
49 _last_paint_bbox = bbox ();
55 _timeline.force_redraw (_last_paint_bbox);
56 _timeline.force_redraw (bbox ());
59 virtual Rect bbox () const = 0;
62 virtual void do_paint (wxGraphicsContext *) = 0;
64 int time_x (Time t) const
66 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
72 Rect _last_paint_bbox;
75 class ContentView : public View
78 ContentView (Timeline& tl, shared_ptr<Content> c, int t)
84 c->Changed.connect (bind (&ContentView::content_changed, this, _2));
89 shared_ptr<const Film> film = _timeline.film ();
90 shared_ptr<const Content> content = _content.lock ();
91 if (!film || !content) {
96 time_x (content->start ()) - 8,
98 content->length () * _timeline.pixels_per_time_unit() + 16,
99 _timeline.track_height() + 16
103 void set_selected (bool s) {
108 bool selected () const {
112 weak_ptr<Content> content () const {
116 virtual wxString type () const = 0;
117 virtual wxColour colour () const = 0;
121 void do_paint (wxGraphicsContext* gc)
123 shared_ptr<const Film> film = _timeline.film ();
124 shared_ptr<const Content> content = _content.lock ();
125 if (!film || !content) {
129 Time const start = content->start ();
130 Time const len = content->length ();
132 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
134 gc->SetPen (*wxBLACK_PEN);
136 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
137 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
139 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
141 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
144 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
146 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
148 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
152 wxGraphicsPath path = gc->CreatePath ();
153 path.MoveToPoint (time_x (start), y_pos (_track));
154 path.AddLineToPoint (time_x (start + len), y_pos (_track));
155 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1));
156 path.AddLineToPoint (time_x (start), y_pos (_track + 1));
157 path.AddLineToPoint (time_x (start), y_pos (_track));
158 gc->StrokePath (path);
161 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
163 wxDouble name_height;
164 wxDouble name_descent;
165 wxDouble name_leading;
166 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
168 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
169 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
173 int y_pos (int t) const
175 return _timeline.tracks_position().y + t * _timeline.track_height();
178 void content_changed (int p)
180 if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
185 boost::weak_ptr<Content> _content;
190 class AudioContentView : public ContentView
193 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
194 : ContentView (tl, c, t)
198 wxString type () const
203 wxColour colour () const
205 return wxColour (149, 121, 232, 255);
209 class VideoContentView : public ContentView
212 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
213 : ContentView (tl, c, t)
218 wxString type () const
223 wxColour colour () const
225 return wxColour (242, 92, 120, 255);
229 class TimeAxisView : public View
232 TimeAxisView (Timeline& tl, int y)
237 void do_paint (wxGraphicsContext* gc)
239 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
240 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
242 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
245 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
246 if (mark_interval > 5) {
247 mark_interval -= mark_interval % 5;
249 if (mark_interval > 10) {
250 mark_interval -= mark_interval % 10;
252 if (mark_interval > 60) {
253 mark_interval -= mark_interval % 60;
255 if (mark_interval > 3600) {
256 mark_interval -= mark_interval % 3600;
259 if (mark_interval < 1) {
263 wxGraphicsPath path = gc->CreatePath ();
264 path.MoveToPoint (_timeline.x_offset(), _y);
265 path.AddLineToPoint (_timeline.width(), _y);
266 gc->StrokePath (path);
269 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
270 wxGraphicsPath path = gc->CreatePath ();
271 path.MoveToPoint (time_x (t), _y - 4);
272 path.AddLineToPoint (time_x (t), _y + 4);
273 gc->StrokePath (path);
275 int tc = t / TIME_HZ;
276 int const h = tc / 3600;
278 int const m = tc / 60;
282 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
285 wxDouble str_descent;
286 wxDouble str_leading;
287 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
289 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
290 if ((tx + str_width) < _timeline.width()) {
291 gc->DrawText (str, time_x (t), _y + 16);
294 t += mark_interval * TIME_HZ;
300 return Rect (0, _y - 4, _timeline.width(), 24);
307 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
308 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
310 , _pixels_per_time_unit (0)
312 , _down_view_start (0)
313 , _first_move (false)
315 SetDoubleBuffered (true);
317 setup_pixels_per_time_unit ();
319 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
320 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
321 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
322 Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
323 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
325 SetMinSize (wxSize (640, tracks() * track_height() + 96));
329 film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
333 Timeline::paint (wxPaintEvent &)
337 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
342 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
344 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
352 Timeline::playlist_changed ()
354 shared_ptr<const Film> fl = _film.lock ();
361 Playlist::ContentList content = fl->playlist()->content ();
363 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
364 if (dynamic_pointer_cast<VideoContent> (*i)) {
365 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
367 if (dynamic_pointer_cast<AudioContent> (*i)) {
368 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
372 _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
378 Timeline::tracks () const
385 Timeline::setup_pixels_per_time_unit ()
387 shared_ptr<const Film> film = _film.lock ();
392 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
396 Timeline::left_down (wxMouseEvent& ev)
398 list<shared_ptr<View> >::iterator i = _views.begin();
399 Position const p (ev.GetX(), ev.GetY());
400 while (i != _views.end() && !(*i)->bbox().contains (p)) {
406 if (i != _views.end ()) {
407 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*i);
410 shared_ptr<Content> c = cv->content().lock();
412 _down_view_start = c->start ();
416 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
417 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
419 cv->set_selected (i == j);
424 _down_point = ev.GetPosition ();
429 Timeline::left_up (wxMouseEvent &)
435 Timeline::mouse_moved (wxMouseEvent& ev)
441 wxPoint const p = ev.GetPosition();
444 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
451 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
453 shared_ptr<Content> c = _down_view->content().lock();
455 c->set_start (_down_view_start + time_diff);
461 Timeline::force_redraw (Rect const & r)
463 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
466 shared_ptr<const Film>
467 Timeline::film () const
469 return _film.lock ();
473 Timeline::resized (wxSizeEvent &)
475 setup_pixels_per_time_unit ();