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, Time s, int t)
70 void paint (wxGraphicsContext* gc)
72 shared_ptr<const Film> film = _timeline.film ();
73 shared_ptr<const Content> content = _content.lock ();
74 if (!film || !content) {
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) {
127 return Rect (time_x (_start), y_pos (_track), content->length (film) * _timeline.pixels_per_time_unit(), _timeline.track_height());
130 void set_selected (bool s) {
132 _timeline.force_redraw (bbox ());
135 bool selected () const {
139 virtual wxString type () const = 0;
140 virtual wxColour colour () const = 0;
144 int y_pos (int t) const
146 return _timeline.tracks_position().y + t * _timeline.track_height();
149 boost::weak_ptr<const Content> _content;
155 class AudioContentView : public ContentView
158 AudioContentView (Timeline& tl, shared_ptr<const Content> c, Time s, int t)
159 : ContentView (tl, c, s, t)
163 wxString type () const
168 wxColour colour () const
170 return wxColour (149, 121, 232, 255);
174 class VideoContentView : public ContentView
177 VideoContentView (Timeline& tl, shared_ptr<const Content> c, Time s, int t)
178 : ContentView (tl, c, s, t)
183 wxString type () const
188 wxColour colour () const
190 return wxColour (242, 92, 120, 255);
194 class TimeAxisView : public View
197 TimeAxisView (Timeline& tl, int y)
202 void paint (wxGraphicsContext* gc)
204 #if wxMAJOR_VERSION == 2 && wxMINOR_VERSION >= 9
205 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
207 gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxSOLID));
210 int mark_interval = rint (128 / (TIME_HZ * _timeline.pixels_per_time_unit ()));
211 if (mark_interval > 5) {
212 mark_interval -= mark_interval % 5;
214 if (mark_interval > 10) {
215 mark_interval -= mark_interval % 10;
217 if (mark_interval > 60) {
218 mark_interval -= mark_interval % 60;
220 if (mark_interval > 3600) {
221 mark_interval -= mark_interval % 3600;
224 if (mark_interval < 1) {
228 wxGraphicsPath path = gc->CreatePath ();
229 path.MoveToPoint (_timeline.x_offset(), _y);
230 path.AddLineToPoint (_timeline.width(), _y);
231 gc->StrokePath (path);
234 while ((t * _timeline.pixels_per_time_unit()) < _timeline.width()) {
235 wxGraphicsPath path = gc->CreatePath ();
236 path.MoveToPoint (time_x (t), _y - 4);
237 path.AddLineToPoint (time_x (t), _y + 4);
238 gc->StrokePath (path);
240 int tc = t / TIME_HZ;
241 int const h = tc / 3600;
243 int const m = tc / 60;
247 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
250 wxDouble str_descent;
251 wxDouble str_leading;
252 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
254 int const tx = _timeline.x_offset() + t * _timeline.pixels_per_time_unit();
255 if ((tx + str_width) < _timeline.width()) {
256 gc->DrawText (str, time_x (t), _y + 16);
259 t += mark_interval * TIME_HZ;
265 return Rect (0, _y - 4, _timeline.width(), 24);
272 Timeline::Timeline (wxWindow* parent, shared_ptr<const Film> film)
273 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
275 , _pixels_per_time_unit (0)
277 SetDoubleBuffered (true);
279 setup_pixels_per_time_unit ();
281 Connect (wxID_ANY, wxEVT_PAINT, wxPaintEventHandler (Timeline::paint), 0, this);
282 Connect (wxID_ANY, wxEVT_LEFT_DOWN, wxMouseEventHandler (Timeline::left_down), 0, this);
283 Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Timeline::resized), 0, this);
285 SetMinSize (wxSize (640, tracks() * track_height() + 96));
289 film->playlist()->Changed.connect (bind (&Timeline::playlist_changed, this));
290 film->playlist()->ContentChanged.connect (bind (&Timeline::playlist_changed, this));
294 Timeline::paint (wxPaintEvent &)
298 wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
303 gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
305 for (list<shared_ptr<View> >::iterator i = _views.begin(); i != _views.end(); ++i) {
313 Timeline::playlist_changed ()
315 shared_ptr<const Film> fl = _film.lock ();
322 Playlist::RegionList regions = fl->playlist()->regions ();
324 for (Playlist::RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
325 if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
326 _views.push_back (shared_ptr<View> (new VideoContentView (*this, (*i)->content, (*i)->time, 0)));
328 _views.push_back (shared_ptr<View> (new AudioContentView (*this, (*i)->content, (*i)->time, 1)));
332 _views.push_back (shared_ptr<View> (new TimeAxisView (*this, tracks() * track_height() + 32)));
338 Timeline::tracks () const
345 Timeline::setup_pixels_per_time_unit ()
347 shared_ptr<const Film> film = _film.lock ();
352 _pixels_per_time_unit = static_cast<double>(width() - x_offset() * 2) / film->length();
356 Timeline::left_down (wxMouseEvent& ev)
358 list<shared_ptr<View> >::iterator i = _views.begin();
359 Position const p (ev.GetX(), ev.GetY());
360 while (i != _views.end() && !(*i)->bbox().contains (p)) {
364 for (list<shared_ptr<View> >::iterator j = _views.begin(); j != _views.end(); ++j) {
365 shared_ptr<ContentView> cv = dynamic_pointer_cast<ContentView> (*j);
367 cv->set_selected (i == j);
373 Timeline::force_redraw (Rect const & r)
375 RefreshRect (wxRect (r.x, r.y, r.width, r.height), false);
378 shared_ptr<const Film>
379 Timeline::film () const
381 return _film.lock ();
385 Timeline::resized (wxSizeEvent &)
387 setup_pixels_per_time_unit ();