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>
32 using boost::shared_ptr;
33 using boost::dynamic_pointer_cast;
45 virtual void paint (wxGraphicsContext *) = 0;
46 virtual Rect bbox () const = 0;
49 int time_x (Time t) const
51 return _timeline.tracks_position().x + t * _timeline.pixels_per_time_unit();
57 class ContentView : public View
60 ContentView (Timeline& tl, shared_ptr<const Content> c, int t)
69 void paint (wxGraphicsContext* gc)
71 shared_ptr<const Film> film = _timeline.film ();
72 shared_ptr<const Content> content = _content.lock ();
73 if (!film || !content) {
77 Time const start = content->time ();
78 Time const len = content->length (film);
80 gc->SetPen (*wxBLACK_PEN);
82 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
83 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxPENSTYLE_SOLID));
85 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (200, 200, 200), wxBRUSHSTYLE_SOLID));
87 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxBRUSHSTYLE_SOLID));
90 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 4, wxSOLID));
92 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (200, 200, 200), wxSOLID));
94 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (colour(), wxSOLID));
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);
107 wxString name = wxString::Format (wxT ("%s [%s]"), std_to_wx (content->file().filename().string()).data(), type().data());
109 wxDouble name_height;
110 wxDouble name_descent;
111 wxDouble name_leading;
112 gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
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);
121 shared_ptr<const Film> film = _timeline.film ();
122 shared_ptr<const Content> content = _content.lock ();
123 if (!film || !content) {
128 time_x (content->time ()),
130 content->length (film) * _timeline.pixels_per_time_unit(),
131 _timeline.track_height()
135 void set_selected (bool s) {
137 _timeline.force_redraw (bbox ());
140 bool selected () const {
144 virtual wxString type () const = 0;
145 virtual wxColour colour () const = 0;
149 int y_pos (int t) const
151 return _timeline.tracks_position().y + t * _timeline.track_height();
154 boost::weak_ptr<const Content> _content;
159 class AudioContentView : public ContentView
162 AudioContentView (Timeline& tl, shared_ptr<const Content> c, int t)
163 : ContentView (tl, c, t)
167 wxString type () const
172 wxColour colour () const
174 return wxColour (149, 121, 232, 255);
178 class VideoContentView : public ContentView
181 VideoContentView (Timeline& tl, shared_ptr<const Content> c, int t)
182 : ContentView (tl, c, t)
187 wxString type () const
192 wxColour colour () const
194 return wxColour (242, 92, 120, 255);
198 class TimeAxisView : public View
201 TimeAxisView (Timeline& tl, int y)
206 void paint (wxGraphicsContext* gc)
208 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
209 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
211 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
214 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
215 if (mark_interval > 5) {
216 mark_interval -= mark_interval % 5;
218 if (mark_interval > 10) {
219 mark_interval -= mark_interval % 10;
221 if (mark_interval > 60) {
222 mark_interval -= mark_interval % 60;
224 if (mark_interval > 3600) {
225 mark_interval -= mark_interval % 3600;
228 if (mark_interval < 1) {
232 wxGraphicsPath path = gc->CreatePath ();
233 path.MoveToPoint (_timeline.x_offset(), _y);
234 path.AddLineToPoint (_timeline.width(), _y);
235 gc->StrokePath (path);
238 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
239 wxGraphicsPath path = gc->CreatePath ();
240 path.MoveToPoint (time_x (t), _y - 4);
241 path.AddLineToPoint (time_x (t), _y + 4);
242 gc->StrokePath (path);
244 int tc = t / TIME_HZ;
245 int const h = tc / 3600;
247 int const m = tc / 60;
251 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
254 wxDouble str_descent;
255 wxDouble str_leading;
256 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
258 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
259 if ((tx + str_width) < _timeline.width()) {
260 gc->DrawText (str, time_x (t), _y + 16);
263 t += mark_interval * TIME_HZ;
269 return Rect (0, _y - 4, _timeline.width(), 24);
276 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
277 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
279 , _pixels_per_time_unit (0)
281 SetDoubleBuffered (true);
283 setup_pixels_per_time_unit ();
285 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
286 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
287 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
289 SetMinSize (wxSize (640, tracks() * track_height() + 96));
293 film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
294 film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_changed, this));
298 Timeline::paint (wxPaintEvent &)
302 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
307 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
309 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
317 Timeline::playlist_changed ()
319 shared_ptr<const Film> fl = _film.lock ();
326 Playlist::ContentList content = fl->playlist()->content ();
328 for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
329 if (dynamic_pointer_cast<VideoContent> (*i)) {
330 _views.push_back (shared_ptr<View> (new VideoContentView (*this, *i, 0)));
332 _views.push_back (shared_ptr<View> (new AudioContentView (*this, *i, 1)));
336 _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
342 Timeline::tracks () const
349 Timeline::setup_pixels_per_time_unit ()
351 shared_ptr<const Film> film = _film.lock ();
356 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
360 Timeline::left_down (wxMouseEvent& ev)
362 list<shared_ptr<View> >::iterator i = _views.begin();
363 Position const p (ev.GetX(), ev.GetY());
364 while (i != _views.end() && !(*i)->bbox().contains (p)) {
368 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
369 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
371 cv->set_selected (i == j);
377 Timeline::force_redraw (Rect const & r)
379 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
382 shared_ptr<const Film>
383 Timeline::film () const
385 return _film.lock ();
389 Timeline::resized (wxSizeEvent &)
391 setup_pixels_per_time_unit ();