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