Don't show reel labels if there is no space.
[dcpomatic.git] / src / wx / timeline_reels_view.cc
1 /*
2     Copyright (C) 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_reels_view.h"
21 #include "timeline.h"
22 #include <wx/wx.h>
23 #include <wx/graphics.h>
24 #include <boost/foreach.hpp>
25
26 using std::min;
27
28 TimelineReelsView::TimelineReelsView (Timeline& tl, int y)
29         : TimelineView (tl)
30         , _y (y)
31 {
32
33 }
34
35 dcpomatic::Rect<int>
36 TimelineReelsView::bbox () const
37 {
38         return dcpomatic::Rect<int> (0, _y - 4, _timeline.width(), 24);
39 }
40
41 void
42 TimelineReelsView::set_y (int y)
43 {
44         _y = y;
45         force_redraw ();
46 }
47
48 void
49 TimelineReelsView::do_paint (wxGraphicsContext* gc)
50 {
51         if (!_timeline.pixels_per_second()) {
52                 return;
53         }
54
55         double const pps = _timeline.pixels_per_second().get ();
56
57         gc->SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 255), 1, wxPENSTYLE_SOLID));
58
59         wxGraphicsPath path = gc->CreatePath ();
60         path.MoveToPoint (time_x (DCPTime (0)), _y);
61         path.AddLineToPoint (time_x (_timeline.film()->length()), _y);
62         gc->StrokePath (path);
63
64         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, wxColour (0, 0, 255)));
65
66         int reel = 1;
67         BOOST_FOREACH (DCPTimePeriod i, _timeline.film()->reels()) {
68                 int const size = min (8.0, i.duration().seconds() * pps / 2);
69
70                 wxGraphicsPath path = gc->CreatePath ();
71                 path.MoveToPoint (time_x (i.from) + size, _y + size / 2);
72                 path.AddLineToPoint (time_x (i.from), _y);
73                 path.AddLineToPoint (time_x (i.from) + size, _y - size / 2);
74                 gc->StrokePath (path);
75
76                 path = gc->CreatePath ();
77                 path.MoveToPoint (time_x (i.to) - size, _y + size / 2);
78                 path.AddLineToPoint (time_x (i.to), _y);
79                 path.AddLineToPoint (time_x (i.to) - size, _y - size / 2);
80                 gc->StrokePath (path);
81
82                 wxString str = wxString::Format (_("Reel %d"), reel++);
83                 wxDouble str_width;
84                 wxDouble str_height;
85                 wxDouble str_descent;
86                 wxDouble str_leading;
87                 gc->GetTextExtent (str, &str_width, &str_height, &str_descent, &str_leading);
88
89                 int const available_width = time_x (DCPTime (i.to.get() - i.from.get()));
90
91                 if (available_width > str_width) {
92                         gc->DrawText (str, time_x (DCPTime (i.from.get())) + (available_width - str_width) / 2, _y + 4);
93                 }
94         }
95 }