ef0c96ef2589a5baeb849301dc1104ceabfe3c19
[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         _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
44 }
45
46 void
47 SimpleVideoView::paint ()
48 {
49         wxPaintDC dc (_panel);
50
51 #ifdef DCPOMATIC_VARIANT_SWAROOP
52         if (viewer->background_image()) {
53                 dc.Clear ();
54                 maybe_draw_background_image (dc);
55                 return;
56         }
57 #endif
58
59         dcp::Size const out_size = _viewer->out_size ();
60         wxSize const panel_size = _panel->GetSize ();
61
62         if (!out_size.width || !out_size.height || !_image || out_size != _image->size()) {
63                 dc.Clear ();
64         } else {
65
66                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
67                 wxBitmap frame_bitmap (frame);
68                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
69
70 #ifdef DCPOMATIC_VARIANT_SWAROOP
71                 XXX
72                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
73                 int64_t n = _video_position.get() / period.get();
74                 DCPTime from(n * period.get());
75                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
76                 if (from <= _video_position && _video_position <= to) {
77                         if (!_in_watermark) {
78                                 _in_watermark = true;
79                                 _watermark_x = rand() % _panel_size.width;
80                                 _watermark_y = rand() % _panel_size.height;
81                         }
82                         dc.SetTextForeground(*wxWHITE);
83                         string wm = Config::instance()->player_watermark_theatre();
84                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
85                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
86                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
87                 } else {
88                         _in_watermark = false;
89                 }
90 #endif
91         }
92
93         if (out_size.width < panel_size.GetWidth()) {
94                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
95                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
96                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
97                 dc.SetPen (p);
98                 dc.SetBrush (b);
99                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
100         }
101
102         if (out_size.height < panel_size.GetHeight()) {
103                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
104                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
105                 dc.SetPen (p);
106                 dc.SetBrush (b);
107                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
108                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
109                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
110         }
111
112         if (_viewer->outline_content()) {
113                 Position<int> inter_position = _viewer->inter_position ();
114                 dcp::Size inter_size = _viewer->inter_size ();
115                 wxPen p (wxColour (255, 0, 0), 2);
116                 dc.SetPen (p);
117                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
118                 dc.DrawRectangle (inter_position.x, inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, inter_size.width, inter_size.height);
119         }
120 }
121
122 void
123 SimpleVideoView::update ()
124 {
125         _panel->Refresh ();
126         _panel->Update ();
127 }