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