Fix TimelineTimeAxisView::bbox() to be correct.
[dcpomatic.git] / src / wx / timeline_time_axis_view.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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.
10
11     DCP-o-matic 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.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "timeline.h"
23 #include "timeline_time_axis_view.h"
24 #include "wx_util.h"
25 #include <dcp/warnings.h>
26 LIBDCP_DISABLE_WARNINGS
27 #include <wx/graphics.h>
28 #include <wx/wx.h>
29 LIBDCP_ENABLE_WARNINGS
30
31
32 using std::cout;
33 using std::list;
34 using namespace dcpomatic;
35
36
37 TimelineTimeAxisView::TimelineTimeAxisView (Timeline& tl, int y)
38         : TimelineView (tl)
39         , _y (y)
40 {
41
42 }
43
44
45 dcpomatic::Rect<int>
46 TimelineTimeAxisView::bbox () const
47 {
48         return dcpomatic::Rect<int>(0, y_pos(_y), _timeline.width(), 48);
49 }
50
51
52 /** @param y y position in tracks (not pixels) */
53 void
54 TimelineTimeAxisView::set_y (int y)
55 {
56         _y = y;
57         force_redraw ();
58 }
59
60
61 void
62 TimelineTimeAxisView::do_paint (wxGraphicsContext* gc, list<dcpomatic::Rect<int> >)
63 {
64         if (!_timeline.pixels_per_second()) {
65                 return;
66         }
67
68         double const pps = _timeline.pixels_per_second().get ();
69
70         wxColour const colour = gui_is_dark() ? *wxWHITE : *wxBLACK;
71         gc->SetPen(*wxThePenList->FindOrCreatePen(colour, 1, wxPENSTYLE_SOLID));
72
73         double const mark_interval = calculate_mark_interval (rint (128 / pps));
74
75         int y = _y * _timeline.pixels_per_track() + 32;
76
77         wxGraphicsPath path = gc->CreatePath ();
78         path.MoveToPoint (0, y);
79         path.AddLineToPoint (_timeline.width(), y);
80         gc->StrokePath (path);
81
82         gc->SetFont(gc->CreateFont(*wxNORMAL_FONT, colour));
83
84         /* Time in seconds */
85         DCPTime t;
86         while ((t.seconds() * pps) < _timeline.width()) {
87                 wxGraphicsPath path = gc->CreatePath ();
88                 path.MoveToPoint (time_x (t), y - 4);
89                 path.AddLineToPoint (time_x (t), y + 4);
90                 gc->StrokePath (path);
91
92                 double tc = t.seconds ();
93                 int const h = tc / 3600;
94                 tc -= h * 3600;
95                 int const m = tc / 60;
96                 tc -= m * 60;
97                 int const s = tc;
98
99                 wxString str = wxString::Format (wxT ("%02d:%02d:%02d"), h, m, s);
100                 wxDouble str_width;
101                 wxDouble str_height;
102                 wxDouble str_descent;
103                 wxDouble str_leading;
104                 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
105
106                 int const tx = t.seconds() * pps;
107                 if ((tx + str_width) < _timeline.width()) {
108                         gc->DrawText (str, time_x (t), y + 16);
109                 }
110
111                 t += DCPTime::from_seconds (mark_interval);
112         }
113 }