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