Hide warnings triggered by Ubuntu 20.04's gcc.
[dcpomatic.git] / src / wx / custom_scale_dialog.cc
1 /*
2     Copyright (C) 2020 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 "custom_scale_dialog.h"
23 #include "wx_util.h"
24 #include "lib/util.h"
25 #include <dcp/raw_convert.h>
26 #include "lib/warnings.h"
27 DCPOMATIC_DISABLE_WARNINGS
28 #include <wx/wx.h>
29 #include <wx/propgrid/property.h>
30 #include <wx/propgrid/props.h>
31 DCPOMATIC_ENABLE_WARNINGS
32
33
34 using boost::optional;
35 using namespace dcp;
36
37
38 CustomScaleDialog::CustomScaleDialog (wxWindow* parent, dcp::Size initial, dcp::Size film_container, optional<float> custom_ratio, optional<dcp::Size> custom_size)
39         : TableDialog (parent, _("Custom scale"), 3, 1, true)
40         , _film_container (film_container)
41 {
42         _ratio_to_fit = new wxRadioButton (this, wxID_ANY, _("Set ratio and fit to DCP container"));
43         add (_ratio_to_fit);
44         wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
45         _ratio = new wxTextCtrl (this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, 0, wxNumericPropertyValidator(wxNumericPropertyValidator::Float));
46         s->Add (_ratio, 1, wxRIGHT, 4);
47         add_label_to_sizer (s, this, wxT(":1"), false);
48         add (s);
49         _size_from_ratio = new wxStaticText (this, wxID_ANY, wxT(""));
50         add (_size_from_ratio, 1, wxALIGN_CENTER_VERTICAL);
51
52 #ifdef __WXGTK3__
53         int const spin_width = 118;
54 #else
55         int const spin_width = 64;
56 #endif
57
58         _size = new wxRadioButton (this, wxID_ANY, _("Set size"));
59         add (_size);
60         s = new wxBoxSizer (wxHORIZONTAL);
61         _width = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(spin_width, -1), wxSP_ARROW_KEYS, 1, film_container.width);
62         s->Add (_width, 1, wxRIGHT, 4);
63         add_label_to_sizer (s, this, wxT("x"), false);
64         _height = new wxSpinCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(spin_width, -1), wxSP_ARROW_KEYS, 1, film_container.height);
65         s->Add (_height, 1, wxRIGHT, 4);
66         add (s);
67         _ratio_from_size = new wxStaticText (this, wxID_ANY, wxT(""));
68         add (_ratio_from_size, 1, wxALIGN_CENTER_VERTICAL);
69
70         if (custom_ratio) {
71                 _ratio_to_fit->SetValue (true);
72                 _size->SetValue (false);
73                 _ratio->SetValue (wxString::Format("%.2f", *custom_ratio));
74                 _width->SetValue (initial.width);
75                 _height->SetValue (initial.height);
76         } else if (custom_size) {
77                 _ratio_to_fit->SetValue (false);
78                 _size->SetValue (true);
79                 _ratio->SetValue (wxString::Format("%.2f", initial.ratio()));
80                 _width->SetValue (custom_size->width);
81                 _height->SetValue (custom_size->height);
82         } else {
83                 _ratio_to_fit->SetValue (true);
84                 _size->SetValue (false);
85                 _ratio->SetValue (wxString::Format("%.2f", initial.ratio()));
86                 _width->SetValue (initial.width);
87                 _height->SetValue (initial.height);
88         }
89
90         setup_sensitivity ();
91         update_size_from_ratio ();
92         update_ratio_from_size ();
93
94         layout ();
95
96         _ratio_to_fit->Bind (wxEVT_RADIOBUTTON, boost::bind(&CustomScaleDialog::setup_sensitivity, this));
97         _ratio->Bind (wxEVT_TEXT, boost::bind(&CustomScaleDialog::update_size_from_ratio, this));
98         _size->Bind (wxEVT_RADIOBUTTON, boost::bind(&CustomScaleDialog::setup_sensitivity, this));
99         _width->Bind (wxEVT_TEXT, boost::bind(&CustomScaleDialog::update_ratio_from_size, this));
100         _height->Bind (wxEVT_TEXT, boost::bind(&CustomScaleDialog::update_ratio_from_size, this));
101 }
102
103
104 void
105 CustomScaleDialog::update_size_from_ratio ()
106 {
107         dcp::Size const s = fit_ratio_within (raw_convert<float>(wx_to_std(_ratio->GetValue())), _film_container);
108         _size_from_ratio->SetLabelMarkup (wxString::Format("<i>%dx%d</i>", s.width, s.height));
109 }
110
111
112 void
113 CustomScaleDialog::update_ratio_from_size ()
114 {
115         float const ratio = _height->GetValue() > 0 ? (float(_width->GetValue()) / _height->GetValue()) : 2;
116         _ratio_from_size->SetLabelMarkup (wxString::Format("<i>%.2f:1</i>", ratio));
117 }
118
119
120 void
121 CustomScaleDialog::setup_sensitivity ()
122 {
123         _ratio->Enable (_ratio_to_fit->GetValue());
124         _size_from_ratio->Enable (_ratio_to_fit->GetValue());
125         _width->Enable (_size->GetValue());
126         _height->Enable (_size->GetValue());
127         _ratio_from_size->Enable (_size->GetValue());
128 }
129
130
131 optional<float>
132 CustomScaleDialog::custom_ratio () const
133 {
134         if (!_ratio_to_fit->GetValue()) {
135                 return optional<float>();
136         }
137
138         return raw_convert<float>(wx_to_std(_ratio->GetValue()));
139 }
140
141
142 optional<dcp::Size>
143 CustomScaleDialog::custom_size () const
144 {
145         if (!_size->GetValue()) {
146                 return optional<dcp::Size>();
147         }
148
149         return dcp::Size (_width->GetValue(), _height->GetValue());
150 }
151