Fix build.
[dcpomatic.git] / src / wx / markers_panel.cc
1 /*
2     Copyright (C) 2021-2022 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 "film_viewer.h"
23 #include "markers.h"
24 #include "markers_panel.h"
25 #include "wx_util.h"
26 #include "lib/film.h"
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <wx/graphics.h>
30 #include <wx/tipwin.h>
31 LIBDCP_ENABLE_WARNINGS
32 #include <boost/bind/bind.hpp>
33 #include <boost/version.hpp>
34
35
36 using std::shared_ptr;
37 using std::weak_ptr;
38 #if BOOST_VERSION >= 106100
39 using namespace boost::placeholders;
40 #endif
41
42
43 enum {
44         ID_move_marker_to_current_position,
45         ID_remove_marker,
46         ID_add_marker,
47         /* Leave some space after this one as we use an ID for each marker type
48          * starting with ID_add_base.
49          */
50         ID_add_base,
51 };
52
53
54 static constexpr auto line_to_label_gap = 2;
55
56
57 MarkersPanel::MarkersPanel(wxWindow* parent, FilmViewer& viewer)
58         : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxSize(-1, 16))
59         , _viewer (viewer)
60 {
61         Bind (wxEVT_PAINT, boost::bind(&MarkersPanel::paint, this));
62         Bind (wxEVT_MOTION, boost::bind(&MarkersPanel::mouse_moved, this, _1));
63
64         Bind (wxEVT_LEFT_DOWN, boost::bind(&MarkersPanel::mouse_left_down, this));
65         Bind (wxEVT_RIGHT_DOWN, boost::bind(&MarkersPanel::mouse_right_down, this, _1));
66
67         Bind (wxEVT_MENU, boost::bind(&MarkersPanel::move_marker_to_current_position, this), ID_move_marker_to_current_position);
68         Bind (wxEVT_MENU, boost::bind(&MarkersPanel::remove_marker, this), ID_remove_marker);
69         Bind (wxEVT_MENU, boost::bind(&MarkersPanel::add_marker, this, _1), ID_add_base, ID_add_base + all_editable_markers().size());
70 }
71
72
73 void
74 MarkersPanel::set_film (weak_ptr<Film> weak_film)
75 {
76         _film = weak_film;
77         auto film = weak_film.lock ();
78         if (film) {
79                 film->Change.connect (boost::bind(&MarkersPanel::film_changed, this, _1, _2));
80                 update_from_film (film);
81         }
82 }
83
84
85 void
86 MarkersPanel::film_changed(ChangeType type, FilmProperty property)
87 {
88         if (type != ChangeType::DONE) {
89                 return;
90         }
91
92         auto film = _film.lock();
93         if (!film) {
94                 return;
95         }
96
97         if (property == FilmProperty::MARKERS || property == FilmProperty::CONTENT || property == FilmProperty::CONTENT_ORDER || property == FilmProperty::VIDEO_FRAME_RATE) {
98                 update_from_film (film);
99         }
100 }
101
102
103 void
104 MarkersPanel::update_from_film (shared_ptr<Film> film)
105 {
106         _markers.clear ();
107         for (auto const& marker: film->markers()) {
108                 _markers[marker.first] = Marker(
109                         marker.second,
110                         marker.first == dcp::Marker::FFOC ||
111                         marker.first == dcp::Marker::FFTC ||
112                         marker.first == dcp::Marker::FFOI ||
113                         marker.first == dcp::Marker::FFEC ||
114                         marker.first == dcp::Marker::FFMC
115                         );
116
117         }
118         Refresh ();
119 }
120
121
122 int
123 MarkersPanel::position (dcpomatic::DCPTime time, int width) const
124 {
125 #ifdef DCPOMATIC_LINUX
126         /* Number of pixels between the left/right bounding box edge of a wxSlider
127          * and the start of the "track".
128          */
129         auto constexpr end_gap = 12;
130 #else
131         auto constexpr end_gap = 0;
132 #endif
133         auto film = _film.lock ();
134         if (!film) {
135                 return 0;
136         }
137
138         return end_gap + time.get() * (width - end_gap * 2) / film->length().get();
139 }
140
141
142 void
143 MarkersPanel::mouse_moved (wxMouseEvent& ev)
144 {
145         _over = boost::none;
146
147         auto film = _film.lock ();
148         if (!film) {
149                 return;
150         }
151
152         auto const panel_width = GetSize().GetWidth();
153 #if !defined(DCPOMATIC_LINUX)
154         auto const panel_height = GetSize().GetHeight();
155         auto const factor = GetContentScaleFactor();
156 #endif
157
158         auto const x = ev.GetPosition().x;
159         for (auto const& marker: _markers) {
160                 auto const pos = position(marker.second.time, panel_width);
161                 auto const width = marker.second.width ? marker.second.width : 4;
162                 auto const x1 = marker.second.line_before_label ? pos : pos - width - line_to_label_gap;
163                 auto const x2 = marker.second.line_before_label ? pos + width + line_to_label_gap : pos;
164                 if (x1 <= x && x < x2) {
165                         _over = marker.first;
166 /* Tooltips flicker really badly on Wayland for some reason, so only do this on Windows/macOS for now */
167 #if !defined(DCPOMATIC_LINUX)
168                         if (!_tip) {
169                                 auto mouse = ClientToScreen (ev.GetPosition());
170                                 auto rect = wxRect(mouse.x, mouse.y, width * factor, panel_height * factor);
171                                 auto hmsf = marker.second.time.split(film->video_frame_rate());
172                                 char timecode_buffer[64];
173                                 snprintf (timecode_buffer, sizeof(timecode_buffer), " %02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
174                                 auto tip_text = dcp::marker_to_string(marker.first) + std::string(timecode_buffer);
175                                 _tip = new wxTipWindow (this, std_to_wx(tip_text), 100, &_tip, &rect);
176                         }
177 #endif
178                 }
179         }
180 }
181
182
183 void
184 MarkersPanel::paint ()
185 {
186         wxPaintDC dc (this);
187
188         std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
189         if (!gc) {
190                 return;
191         }
192
193         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
194         gc->SetPen (wxPen(wxColour(200, 0, 0)));
195         gc->SetFont (gc->CreateFont(*wxSMALL_FONT, wxColour(200, 0, 0)));
196
197         auto const panel_width = GetSize().GetWidth();
198         auto const panel_height = GetSize().GetHeight();
199
200         for (auto& marker: _markers) {
201                 auto label = std_to_wx(dcp::marker_to_string(marker.first));
202                 if (marker.second.width == 0) {
203                         /* We don't know the width of this marker label yet, so calculate it now */
204                         wxDouble width, height, descent, external_leading;
205                         gc->GetTextExtent (label, &width, &height, &descent, &external_leading);
206                         marker.second.width = width;
207                 }
208                 auto line = gc->CreatePath ();
209                 auto const pos = position(marker.second.time, panel_width);
210                 line.MoveToPoint (pos, 0);
211                 line.AddLineToPoint (pos, panel_height);
212                 gc->StrokePath (line);
213                 if (marker.second.line_before_label) {
214                         gc->DrawText (label, pos + line_to_label_gap, 0);
215                 } else {
216                         gc->DrawText (label, pos - line_to_label_gap - marker.second.width, 0);
217                 }
218         }
219 }
220
221
222 void
223 MarkersPanel::mouse_left_down ()
224 {
225         if (_over) {
226                 _viewer.seek(_markers[*_over].time, true);
227         }
228 }
229
230
231 void
232 MarkersPanel::mouse_right_down (wxMouseEvent& ev)
233 {
234         wxMenu menu;
235         if (_over) {
236                 menu.Append (ID_move_marker_to_current_position, wxString::Format(_("Move %s marker to current position"), wx_to_std(dcp::marker_to_string(*_over))));
237                 menu.Append (ID_remove_marker, wxString::Format(_("Remove %s marker"), wx_to_std(dcp::marker_to_string(*_over))));
238         }
239
240         auto add_marker = new wxMenu ();
241         for (auto const& marker: all_editable_markers()) {
242                 add_marker->Append (static_cast<int>(ID_add_base) + static_cast<int>(marker.second), marker.first);
243         }
244         menu.Append (ID_add_marker, _("Add or move marker to current position"), add_marker);
245
246         _menu_marker = _over;
247         PopupMenu (&menu, ev.GetPosition());
248 }
249
250
251 void
252 MarkersPanel::move_marker_to_current_position ()
253 {
254         auto film = _film.lock ();
255         if (!film || !_menu_marker) {
256                 return;
257         }
258
259         film->set_marker(*_menu_marker, _viewer.position());
260 }
261
262
263 void
264 MarkersPanel::remove_marker ()
265 {
266         auto film = _film.lock ();
267         if (!film || !_menu_marker) {
268                 return;
269         }
270
271         film->unset_marker(*_menu_marker);
272 }
273
274
275 void
276 MarkersPanel::add_marker (wxCommandEvent& ev)
277 {
278         auto film = _film.lock ();
279         if (!film) {
280                 return;
281         }
282
283         auto marker = static_cast<dcp::Marker>(ev.GetId() - ID_add_base);
284         film->set_marker(marker, _viewer.position());
285 }
286