66af8f19f58b75b3b30666211977a7f000ac8f36
[dcpomatic.git] / src / wx / simple_video_view.cc
1 /*
2     Copyright (C) 2019 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 #include "simple_video_view.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "lib/image.h"
25 #include <dcp/util.h>
26 #include <wx/wx.h>
27 #include <boost/bind.hpp>
28
29 using std::max;
30 using std::string;
31 using boost::optional;
32 using namespace dcpomatic;
33
34 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
35         : VideoView (viewer)
36 {
37         _panel = new wxPanel (parent);
38
39 #ifndef __WXOSX__
40         _panel->SetDoubleBuffered (true);
41 #endif
42
43         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
44         _panel->SetBackgroundColour (*wxBLACK);
45
46         _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
47         _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
48 }
49
50 void
51 SimpleVideoView::paint ()
52 {
53         _viewer->state_timer().set("paint-panel");
54         wxPaintDC dc (_panel);
55
56         dcp::Size const out_size = _viewer->out_size ();
57         wxSize const panel_size = _panel->GetSize ();
58
59 #ifdef DCPOMATIC_VARIANT_SWAROOP
60         if (_viewer->background_image()) {
61                 dc.Clear ();
62                 optional<boost::filesystem::path> bg = Config::instance()->player_background_image();
63                 if (bg) {
64                         wxImage image (std_to_wx(bg->string()));
65                         wxBitmap bitmap (image);
66                         dc.DrawBitmap (bitmap, max(0, (panel_size.GetWidth() - image.GetSize().GetWidth()) / 2), max(0, (panel_size.GetHeight() - image.GetSize().GetHeight()) / 2));
67                 }
68                 return;
69         }
70 #endif
71
72         if (!out_size.width || !out_size.height || !_image || out_size != _image->size()) {
73                 dc.Clear ();
74         } else {
75
76                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
77                 wxBitmap frame_bitmap (frame);
78                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
79
80 #ifdef DCPOMATIC_VARIANT_SWAROOP
81                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
82                 int64_t n = _viewer->video_position().get() / period.get();
83                 DCPTime from(n * period.get());
84                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
85                 if (from <= _viewer->video_position() && _viewer->video_position() <= to) {
86                         if (!_in_watermark) {
87                                 _in_watermark = true;
88                                 _watermark_x = rand() % panel_size.GetWidth();
89                                 _watermark_y = rand() % panel_size.GetHeight();
90                         }
91                         dc.SetTextForeground(*wxWHITE);
92                         string wm = Config::instance()->player_watermark_theatre();
93                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
94                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
95                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
96                 } else {
97                         _in_watermark = false;
98                 }
99 #endif
100         }
101
102         if (out_size.width < panel_size.GetWidth()) {
103                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
104                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
105                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
106                 dc.SetPen (p);
107                 dc.SetBrush (b);
108                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
109         }
110
111         if (out_size.height < panel_size.GetHeight()) {
112                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
113                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
114                 dc.SetPen (p);
115                 dc.SetBrush (b);
116                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
117                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
118                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
119         }
120
121         if (_viewer->outline_content()) {
122                 Position<int> inter_position = _viewer->inter_position ();
123                 dcp::Size inter_size = _viewer->inter_size ();
124                 wxPen p (wxColour (255, 0, 0), 2);
125                 dc.SetPen (p);
126                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
127                 dc.DrawRectangle (inter_position.x, inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, inter_size.width, inter_size.height);
128         }
129         _viewer->state_timer().unset();
130 }
131
132 void
133 SimpleVideoView::update ()
134 {
135         _panel->Refresh ();
136         _panel->Update ();
137 }