ce37ec659e3f7561ea85be7fe94d3077d6080de0
[dcpomatic.git] / src / wx / timeline_time_axis_view.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "timeline_time_axis_view.h"
21 #include "timeline.h"
22 #include <wx/wx.h>
23 #include <wx/graphics.h>
24
25 using std::list;
26
27 TimelineTimeAxisView::TimelineTimeAxisView (Timeline& tl, int y)
28         : TimelineView (tl)
29         , _y (y)
30 {
31
32 }
33
34 dcpomatic::Rect<int>
35 TimelineTimeAxisView::bbox () const
36 {
37         return dcpomatic::Rect<int> (_timeline.tracks_position().x, _y - 4, _timeline.width(), 24);
38 }
39
40 void
41 TimelineTimeAxisView::set_y (int y)
42 {
43         _y = y;
44         force_redraw ();
45 }
46
47 void
48 TimelineTimeAxisView::do_paint (wxGraphicsContext* gc, list<dcpomatic::Rect<int> >)
49 {
50         if (!_timeline.pixels_per_second()) {
51                 return;
52         }
53
54         double const pps = _timeline.pixels_per_second().get ();
55
56         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
57
58         double mark_interval = rint (128 / pps);
59         if (mark_interval > 5) {
60                 mark_interval -= lrint (mark_interval) % 5;
61         }
62         if (mark_interval > 10) {
63                 mark_interval -= lrint (mark_interval) % 10;
64         }
65         if (mark_interval > 60) {
66                 mark_interval -= lrint (mark_interval) % 60;
67         }
68         if (mark_interval > 3600) {
69                 mark_interval -= lrint (mark_interval) % 3600;
70         }
71
72         if (mark_interval < 1) {
73                 mark_interval = 1;
74         }
75
76         wxGraphicsPath path = gc->CreatePath ();
77         path.MoveToPoint (_timeline.tracks_position().x, _y);
78         path.AddLineToPoint (_timeline.width(), _y);
79         gc->StrokePath (path);
80
81         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT));
82
83         /* Time in seconds */
84         DCPTime t;
85         while ((t.seconds() * pps) < _timeline.width()) {
86                 wxGraphicsPath path = gc->CreatePath ();
87                 path.MoveToPoint (time_x (t), _y - 4);
88                 path.AddLineToPoint (time_x (t), _y + 4);
89                 gc->StrokePath (path);
90
91                 double tc = t.seconds ();
92                 int const h = tc / 3600;
93                 tc -= h * 3600;
94                 int const m = tc / 60;
95                 tc -= m * 60;
96                 int const s = tc;
97
98                 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
99                 wxDouble str_width;
100                 wxDouble str_height;
101                 wxDouble str_descent;
102                 wxDouble str_leading;
103                 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
104
105                 int const tx = _timeline.tracks_position().x + t.seconds() * pps;
106                 if ((tx + str_width) < _timeline.width()) {
107                         gc->DrawText (str, time_x (t), _y + 16);
108                 }
109
110                 t += DCPTime::from_seconds (mark_interval);
111         }
112 }