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