2 Copyright (C) 2021-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "film_viewer.h"
25 #include "markers_panel.h"
28 #include <dcp/warnings.h>
29 LIBDCP_DISABLE_WARNINGS
30 #include <wx/graphics.h>
31 #include <wx/tipwin.h>
32 LIBDCP_ENABLE_WARNINGS
33 #include <boost/bind/bind.hpp>
34 #include <boost/version.hpp>
37 using std::shared_ptr;
39 #if BOOST_VERSION >= 106100
40 using namespace boost::placeholders;
45 ID_move_marker_to_current_position = DCPOMATIC_MARKERS_PANEL_MENU,
48 /* Leave some space after this one as we use an ID for each marker type
49 * starting with ID_add_base.
55 static constexpr auto line_to_label_gap = 2;
58 MarkersPanel::MarkersPanel(wxWindow* parent, FilmViewer& viewer)
59 : wxPanel (parent, wxID_ANY, wxDefaultPosition, wxSize(-1, 16))
62 Bind (wxEVT_PAINT, boost::bind(&MarkersPanel::paint, this));
63 Bind (wxEVT_MOTION, boost::bind(&MarkersPanel::mouse_moved, this, _1));
65 Bind (wxEVT_LEFT_DOWN, boost::bind(&MarkersPanel::mouse_left_down, this));
66 Bind (wxEVT_RIGHT_DOWN, boost::bind(&MarkersPanel::mouse_right_down, this, _1));
68 Bind (wxEVT_MENU, boost::bind(&MarkersPanel::move_marker_to_current_position, this), ID_move_marker_to_current_position);
69 Bind (wxEVT_MENU, boost::bind(&MarkersPanel::remove_marker, this), ID_remove_marker);
70 Bind(wxEVT_MENU, boost::bind(&MarkersPanel::add_marker, this, _1), ID_add_base, ID_add_base + all_editable_markers().size() + uneditable_markers);
75 MarkersPanel::set_film (weak_ptr<Film> weak_film)
78 auto film = weak_film.lock ();
80 film->Change.connect (boost::bind(&MarkersPanel::film_changed, this, _1, _2));
81 update_from_film (film);
87 MarkersPanel::film_changed(ChangeType type, FilmProperty property)
89 if (type != ChangeType::DONE) {
93 auto film = _film.lock();
98 if (property == FilmProperty::MARKERS || property == FilmProperty::CONTENT || property == FilmProperty::CONTENT_ORDER || property == FilmProperty::VIDEO_FRAME_RATE) {
99 update_from_film (film);
105 MarkersPanel::update_from_film (shared_ptr<Film> film)
108 for (auto const& marker: film->markers()) {
109 _markers[marker.first] = Marker(
111 marker.first == dcp::Marker::FFOC ||
112 marker.first == dcp::Marker::FFTC ||
113 marker.first == dcp::Marker::FFOI ||
114 marker.first == dcp::Marker::FFEC ||
115 marker.first == dcp::Marker::FFMC
124 MarkersPanel::position (dcpomatic::DCPTime time, int width) const
126 #ifdef DCPOMATIC_LINUX
127 /* Number of pixels between the left/right bounding box edge of a wxSlider
128 * and the start of the "track".
130 auto constexpr end_gap = 12;
132 auto constexpr end_gap = 0;
134 auto film = _film.lock ();
139 return end_gap + time.get() * (width - end_gap * 2) / film->length().get();
144 MarkersPanel::mouse_moved (wxMouseEvent& ev)
148 auto film = _film.lock ();
153 auto const panel_width = GetSize().GetWidth();
154 #if !defined(DCPOMATIC_LINUX)
155 auto const panel_height = GetSize().GetHeight();
156 auto const factor = GetContentScaleFactor();
159 auto const x = ev.GetPosition().x;
160 for (auto const& marker: _markers) {
161 auto const pos = position(marker.second.time, panel_width);
162 auto const width = marker.second.width ? marker.second.width : 4;
163 auto const x1 = marker.second.line_before_label ? pos : pos - width - line_to_label_gap;
164 auto const x2 = marker.second.line_before_label ? pos + width + line_to_label_gap : pos;
165 if (x1 <= x && x < x2) {
166 _over = marker.first;
167 /* Tooltips flicker really badly on Wayland for some reason, so only do this on Windows/macOS for now */
168 #if !defined(DCPOMATIC_LINUX)
170 auto mouse = ClientToScreen (ev.GetPosition());
171 auto rect = wxRect(mouse.x, mouse.y, width * factor, panel_height * factor);
172 auto hmsf = marker.second.time.split(film->video_frame_rate());
173 char timecode_buffer[64];
174 snprintf (timecode_buffer, sizeof(timecode_buffer), " %02d:%02d:%02d:%02d", hmsf.h, hmsf.m, hmsf.s, hmsf.f);
175 auto tip_text = dcp::marker_to_string(marker.first) + std::string(timecode_buffer);
176 _tip = new wxTipWindow (this, std_to_wx(tip_text), 100, &_tip, &rect);
185 MarkersPanel::paint ()
189 std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
194 gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
195 gc->SetPen (wxPen(wxColour(200, 0, 0)));
196 gc->SetFont (gc->CreateFont(*wxSMALL_FONT, wxColour(200, 0, 0)));
198 auto const panel_width = GetSize().GetWidth();
199 auto const panel_height = GetSize().GetHeight();
201 for (auto& marker: _markers) {
202 auto label = std_to_wx(dcp::marker_to_string(marker.first));
203 if (marker.second.width == 0) {
204 /* We don't know the width of this marker label yet, so calculate it now */
205 wxDouble width, height, descent, external_leading;
206 gc->GetTextExtent (label, &width, &height, &descent, &external_leading);
207 marker.second.width = width;
209 auto line = gc->CreatePath ();
210 auto const pos = position(marker.second.time, panel_width);
211 line.MoveToPoint (pos, 0);
212 line.AddLineToPoint (pos, panel_height);
213 gc->StrokePath (line);
217 if (GetLayoutDirection() == wxLayout_RightToLeft) {
218 auto matrix = dc.GetTransformMatrix();
219 matrix.Translate(0, 0);
220 matrix.Mirror(wxHORIZONTAL);
221 dc.SetTransformMatrix(matrix);
222 label_x = marker.second.line_before_label ? (pos + line_to_label_gap + marker.second.width) : (pos - line_to_label_gap);
225 label_x = marker.second.line_before_label ? (pos + line_to_label_gap) : (pos - line_to_label_gap - marker.second.width);
228 gc->DrawText(label, label_x, 0);
230 dc.ResetTransformMatrix();
236 MarkersPanel::mouse_left_down ()
239 _viewer.seek(_markers[*_over].time, true);
245 MarkersPanel::mouse_right_down (wxMouseEvent& ev)
249 menu.Append (ID_move_marker_to_current_position, wxString::Format(_("Move %s marker to current position"), wx_to_std(dcp::marker_to_string(*_over))));
250 menu.Append (ID_remove_marker, wxString::Format(_("Remove %s marker"), wx_to_std(dcp::marker_to_string(*_over))));
253 auto add_marker = new wxMenu ();
254 for (auto const& marker: all_editable_markers()) {
255 add_marker->Append (static_cast<int>(ID_add_base) + static_cast<int>(marker.second), marker.first);
257 menu.Append (ID_add_marker, _("Add or move marker to current position"), add_marker);
259 _menu_marker = _over;
260 PopupMenu (&menu, ev.GetPosition());
265 MarkersPanel::move_marker_to_current_position ()
267 auto film = _film.lock ();
268 if (!film || !_menu_marker) {
272 film->set_marker(*_menu_marker, _viewer.position());
277 MarkersPanel::remove_marker ()
279 auto film = _film.lock ();
280 if (!film || !_menu_marker) {
284 film->unset_marker(*_menu_marker);
289 MarkersPanel::add_marker (wxCommandEvent& ev)
291 auto film = _film.lock ();
296 auto marker = static_cast<dcp::Marker>(ev.GetId() - ID_add_base);
297 film->set_marker(marker, _viewer.position());