Fix previous when content is not at time 0.
[dcpomatic.git] / src / wx / timeline_content_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_content_view.h"
21 #include "timeline.h"
22 #include "wx_util.h"
23 #include "lib/content.h"
24 #include <wx/graphics.h>
25 #include <boost/foreach.hpp>
26
27 using boost::shared_ptr;
28
29 TimelineContentView::TimelineContentView (Timeline& tl, shared_ptr<Content> c)
30         : TimelineView (tl)
31         , _content (c)
32         , _selected (false)
33 {
34         _content_connection = c->Changed.connect (bind (&TimelineContentView::content_changed, this, _2, _3));
35 }
36
37 dcpomatic::Rect<int>
38 TimelineContentView::bbox () const
39 {
40         DCPOMATIC_ASSERT (_track);
41
42         shared_ptr<const Film> film = _timeline.film ();
43         shared_ptr<const Content> content = _content.lock ();
44         if (!film || !content) {
45                 return dcpomatic::Rect<int> ();
46         }
47
48         return dcpomatic::Rect<int> (
49                 time_x (content->position ()),
50                 y_pos (_track.get()),
51                 content->length_after_trim().seconds() * _timeline.pixels_per_second().get_value_or(0),
52                 _timeline.track_height()
53                 );
54 }
55
56 void
57 TimelineContentView::set_selected (bool s)
58 {
59         _selected = s;
60         force_redraw ();
61 }
62
63 bool
64 TimelineContentView::selected () const
65 {
66         return _selected;
67 }
68
69 shared_ptr<Content>
70 TimelineContentView::content () const
71 {
72         return _content.lock ();
73 }
74
75 void
76 TimelineContentView::set_track (int t)
77 {
78         _track = t;
79 }
80
81 void
82 TimelineContentView::unset_track ()
83 {
84         _track = boost::optional<int> ();
85 }
86
87 boost::optional<int>
88 TimelineContentView::track () const
89 {
90         return _track;
91 }
92
93 void
94 TimelineContentView::do_paint (wxGraphicsContext* gc)
95 {
96         DCPOMATIC_ASSERT (_track);
97
98         shared_ptr<const Film> film = _timeline.film ();
99         shared_ptr<const Content> cont = content ();
100         if (!film || !cont) {
101                 return;
102         }
103
104         DCPTime const position = cont->position ();
105         DCPTime const len = cont->length_after_trim ();
106
107         wxColour selected (background_colour().Red() / 2, background_colour().Green() / 2, background_colour().Blue() / 2);
108
109         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 4, wxPENSTYLE_SOLID));
110         if (_selected) {
111                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (selected, wxBRUSHSTYLE_SOLID));
112         } else {
113                 gc->SetBrush (*wxTheBrushList->FindOrCreateBrush (background_colour(), wxBRUSHSTYLE_SOLID));
114         }
115
116         /* Outline */
117         wxGraphicsPath path = gc->CreatePath ();
118         path.MoveToPoint    (time_x (position) + 1,           y_pos (_track.get()) + 4);
119         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get()) + 4);
120         path.AddLineToPoint (time_x (position + len) - 1,     y_pos (_track.get() + 1) - 4);
121         path.AddLineToPoint (time_x (position) + 1,           y_pos (_track.get() + 1) - 4);
122         path.AddLineToPoint (time_x (position) + 1,           y_pos (_track.get()) + 4);
123         gc->StrokePath (path);
124         gc->FillPath (path);
125
126         /* Reel split points */
127         gc->SetPen (*wxThePenList->FindOrCreatePen (foreground_colour(), 1, wxPENSTYLE_DOT));
128         BOOST_FOREACH (DCPTime i, cont->reel_split_points ()) {
129                 path = gc->CreatePath ();
130                 path.MoveToPoint (time_x (i), y_pos (_track.get()) + 4);
131                 path.AddLineToPoint (time_x (i), y_pos (_track.get() + 1) - 4);
132                 gc->StrokePath (path);
133         }
134
135         /* Label text */
136         wxString name = std_to_wx (cont->summary());
137         wxDouble name_width;
138         wxDouble name_height;
139         wxDouble name_descent;
140         wxDouble name_leading;
141         gc->SetFont (gc->CreateFont (*wxNORMAL_FONT, foreground_colour ()));
142         gc->GetTextExtent (name, &name_width, &name_height, &name_descent, &name_leading);
143         gc->Clip (wxRegion (time_x (position), y_pos (_track.get()), len.seconds() * _timeline.pixels_per_second().get_value_or(0), _timeline.track_height()));
144         gc->DrawText (name, time_x (position) + 12, y_pos (_track.get() + 1) - name_height - 4);
145         gc->ResetClip ();
146 }
147
148 int
149 TimelineContentView::y_pos (int t) const
150 {
151         return _timeline.tracks_position().y + t * _timeline.track_height();
152 }
153
154 void
155 TimelineContentView::content_changed (int p, bool frequent)
156 {
157         ensure_ui_thread ();
158
159         if (p == ContentProperty::POSITION || p == ContentProperty::LENGTH) {
160                 force_redraw ();
161         }
162
163         if (!frequent) {
164                 _timeline.setup_pixels_per_second ();
165                 _timeline.Refresh ();
166         }
167 }