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 virtual void paint (wxGraphicsContext *) = 0;
48 virtual Rect bbox () const = 0;
52 _timeline.force_redraw (bbox ());
56 int time_x (Time t) const
58 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
64 class ContentView : public View
67 ContentView (Timeline& tl, shared_ptr<Content> c, int t)
76 void paint (wxGraphicsContext* gc)
78 shared_ptr<const Film> film = _timeline.film ();
79 shared_ptr<const Content> content = _content.lock ();
80 if (!film || !content) {
84 Time const start = content->start ();
85 Time const len = content->length ();
87 wxColour selected (colour().Red() / 2, colour().Green() / 2, colour().Blue() / 2);
89 gc->SetPen (*wxBLACK_PEN);
91 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
92 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
94 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
96 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
99 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
101 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxSOLID));
103 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
107 wxGraphicsPath path = gc->CreatePath ();
108 path.MoveToPoint (time_x (start), y_pos (_track));
109 path.AddLineToPoint (time_x (start + len), y_pos (_track));
110 path.AddLineToPoint (time_x (start + len), y_pos (_track + 1));
111 path.AddLineToPoint (time_x (start), y_pos (_track + 1));
112 path.AddLineToPoint (time_x (start), y_pos (_track));
113 gc->StrokePath (path);
116 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
118 wxDouble name_height;
119 wxDouble name_descent;
120 wxDouble name_leading;
121 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
123 gc->Clip (wxRegion (time_x (start), y_pos (_track), len * _timeline.pixels_per_time_unit(), _timeline.track_height()));
124 gc->DrawText (name, time_x (start) + 12, y_pos (_track + 1) - name_height - 4);
130 shared_ptr<const Film> film = _timeline.film ();
131 shared_ptr<const Content> content = _content.lock ();
132 if (!film || !content) {
137 time_x (content->start ()),
139 content->length () * _timeline.pixels_per_time_unit(),
140 _timeline.track_height()
144 void set_selected (bool s) {
149 bool selected () const {
153 weak_ptr<Content> content () const {
157 virtual wxString type () const = 0;
158 virtual wxColour colour () const = 0;
162 int y_pos (int t) const
164 return _timeline.tracks_position().y + t * _timeline.track_height();
167 boost::weak_ptr<Content> _content;
172 class AudioContentView : public ContentView
175 AudioContentView (Timeline& tl, shared_ptr<Content> c, int t)
176 : ContentView (tl, c, t)
180 wxString type () const
185 wxColour colour () const
187 return wxColour (149, 121, 232, 255);
191 class VideoContentView : public ContentView
194 VideoContentView (Timeline& tl, shared_ptr<Content> c, int t)
195 : ContentView (tl, c, t)
200 wxString type () const
205 wxColour colour () const
207 return wxColour (242, 92, 120, 255);
211 class TimeAxisView : public View
214 TimeAxisView (Timeline& tl, int y)
219 void paint (wxGraphicsContext* gc)
221 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
222 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
224 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
227 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
228 if (mark_interval > 5) {
229 mark_interval -= mark_interval % 5;
231 if (mark_interval > 10) {
232 mark_interval -= mark_interval % 10;
234 if (mark_interval > 60) {
235 mark_interval -= mark_interval % 60;
237 if (mark_interval > 3600) {
238 mark_interval -= mark_interval % 3600;
241 if (mark_interval < 1) {
245 wxGraphicsPath path = gc->CreatePath ();
246 path.MoveToPoint (_timeline.x_offset(), _y);
247 path.AddLineToPoint (_timeline.width(), _y);
248 gc->StrokePath (path);
251 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
252 wxGraphicsPath path = gc->CreatePath ();
253 path.MoveToPoint (time_x (t), _y - 4);
254 path.AddLineToPoint (time_x (t), _y + 4);
255 gc->StrokePath (path);
257 int tc = t / TIME_HZ;
258 int const h = tc / 3600;
260 int const m = tc / 60;
264 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
267 wxDouble str_descent;
268 wxDouble str_leading;
269 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
271 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
272 if ((tx + str_width) < _timeline.width()) {
273 gc->DrawText (str, time_x (t), _y + 16);
276 t += mark_interval * TIME_HZ;
282 return Rect (0, _y - 4, _timeline.width(), 24);
289 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
290 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
292 , _pixels_per_time_unit (0)
294 , _down_view_start (0)
295 , _first_move (false)
297 SetDoubleBuffered (true);
299 setup_pixels_per_time_unit ();
301 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
302 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
303 Connect (wxID_ANY, wxEVT_LEFT_UP, wxMouseEventHandler (Timeline::left_up), 0, this);
304 Connect (wxID_ANY, wxEVT_MOTION, wxMouseEventHandler (Timeline::mouse_moved), 0, this);
305 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
307 SetMinSize (wxSize (640, tracks() * track_height() + 96));
311 film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
312 film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_changed, this));
316 Timeline::paint (wxPaintEvent &)
320 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
325 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
327 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
335 Timeline::playlist_changed ()
337 shared_ptr<const Film> fl = _film.lock ();
344 Playlist::ContentList content = fl->playlist()->content ();
346 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
347 if (dynamic_pointer_cast<VideoContent> (*i)) {
348 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
350 if (dynamic_pointer_cast<AudioContent> (*i)) {
351 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
355 _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
361 Timeline::tracks () const
368 Timeline::setup_pixels_per_time_unit ()
370 shared_ptr<const Film> film = _film.lock ();
375 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
379 Timeline::left_down (wxMouseEvent& ev)
381 list<shared_ptr<View> >::iterator i = _views.begin();
382 Position const p (ev.GetX(), ev.GetY());
383 while (i != _views.end() && !(*i)->bbox().contains (p)) {
389 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
390 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
393 shared_ptr<Content> c = cv->content().lock();
395 _down_view_start = c->start ();
396 cv->set_selected (i == j);
401 _down_point = ev.GetPosition ();
406 Timeline::left_up (wxMouseEvent &)
412 Timeline::mouse_moved (wxMouseEvent& ev)
418 wxPoint const p = ev.GetPosition();
421 int const dist = sqrt (pow (p.x - _down_point.x, 2) + pow (p.y - _down_point.y, 2));
428 Time const time_diff = (p.x - _down_point.x) / _pixels_per_time_unit;
430 shared_ptr<Content> c = _down_view->content().lock();
432 _down_view->force_redraw ();
433 c->set_start (_down_view_start + time_diff);
434 _down_view->force_redraw ();
440 Timeline::force_redraw (Rect const & r)
442 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
445 shared_ptr<const Film>
446 Timeline::film () const
448 return _film.lock ();
452 Timeline::resized (wxSizeEvent &)
454 setup_pixels_per_time_unit ();