2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/wx/wx_util.cc
22 * @brief Some utility functions and classes.
26 #include "file_picker_ctrl.h"
27 #include "lib/config.h"
29 #include <dcp/locale_convert.h>
30 #include <wx/spinctrl.h>
31 #include <boost/thread.hpp>
34 using namespace boost;
35 using dcp::locale_convert;
37 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
38 * @param s Sizer to add to.
39 * @param p Parent window for the wxStaticText.
40 * @param t Text for the wxStaticText.
41 * @param left true if this label is a `left label'; ie the sort
42 * of label which should be right-aligned on OS X.
43 * @param prop Proportion to pass when calling Add() on the wxSizer.
47 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
49 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
52 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
55 flags |= wxALIGN_RIGHT;
59 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
60 s->Add (m, prop, flags, 6);
66 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
68 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
71 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
74 flags |= wxALIGN_RIGHT;
78 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
79 s->Add (m, pos, span, flags);
83 /** Pop up an error dialogue box.
84 * @param parent Parent.
88 error_dialog (wxWindow* parent, wxString m)
90 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
95 /** Pop up an error dialogue box.
96 * @param parent Parent.
100 message_dialog (wxWindow* parent, wxString m)
102 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
108 confirm_dialog (wxWindow* parent, wxString m)
110 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
111 int const r = d->ShowModal ();
113 return r == wxID_YES;
117 /** @param s wxWidgets string.
118 * @return Corresponding STL string.
121 wx_to_std (wxString s)
123 return string (s.ToUTF8 ());
126 /** @param s STL string.
127 * @return Corresponding wxWidgets string.
132 return wxString (s.c_str(), wxConvUTF8);
136 string_client_data (wxClientData* o)
138 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
142 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
144 if (widget->GetPath() != std_to_wx (value.string())) {
146 /* Hack to make wxWidgets clear the control when we are passed
151 widget->SetPath (std_to_wx (value.string()));
156 checked_set (wxSpinCtrl* widget, int value)
158 if (widget->GetValue() != value) {
159 widget->SetValue (value);
164 checked_set (wxSpinCtrlDouble* widget, double value)
166 /* XXX: completely arbitrary epsilon */
167 if (fabs (widget->GetValue() - value) > 1e-16) {
168 widget->SetValue (value);
173 checked_set (wxChoice* widget, int value)
175 if (widget->GetSelection() != value) {
176 widget->SetSelection (value);
181 checked_set (wxChoice* widget, string value)
184 if (widget->GetSelection() != -1) {
185 o = widget->GetClientObject (widget->GetSelection ());
188 if (!o || string_client_data(o) != value) {
189 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
190 if (string_client_data (widget->GetClientObject (i)) == value) {
191 widget->SetSelection (i);
198 checked_set (wxChoice* widget, vector<pair<string, string> > items)
200 vector<pair<string, string> > current;
201 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
204 wx_to_std (widget->GetString (i)),
205 string_client_data (widget->GetClientObject (i))
210 if (current == items) {
215 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
216 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
221 checked_set (wxTextCtrl* widget, string value)
223 if (widget->GetValue() != std_to_wx (value)) {
224 widget->ChangeValue (std_to_wx (value));
229 checked_set (wxTextCtrl* widget, wxString value)
231 if (widget->GetValue() != value) {
232 widget->ChangeValue (value);
237 checked_set (wxStaticText* widget, string value)
239 if (widget->GetLabel() != std_to_wx (value)) {
240 widget->SetLabel (std_to_wx (value));
245 checked_set (wxStaticText* widget, wxString value)
247 if (widget->GetLabel() != value) {
248 widget->SetLabel (value);
253 checked_set (wxCheckBox* widget, bool value)
255 if (widget->GetValue() != value) {
256 widget->SetValue (value);
261 checked_set (wxRadioButton* widget, bool value)
263 if (widget->GetValue() != value) {
264 widget->SetValue (value);
269 dcpomatic_setup_i18n ()
271 int language = wxLANGUAGE_DEFAULT;
273 boost::optional<string> config_lang = Config::instance()->language ();
274 if (config_lang && !config_lang->empty ()) {
275 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
277 language = li->Language;
281 wxLocale* locale = 0;
282 if (wxLocale::IsAvailable (language)) {
283 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
285 #ifdef DCPOMATIC_WINDOWS
286 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
289 #ifdef DCPOMATIC_LINUX
290 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
292 /* We have to include the wxWidgets .mo in our distribution,
293 so we rename it to avoid clashes with any other installation
296 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
299 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
300 locale->AddCatalog (wxT ("dcpomatic2"));
302 if (!locale->IsOk()) {
304 locale = new wxLocale (wxLANGUAGE_ENGLISH);
309 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
314 wx_get (wxSpinCtrl* w)
316 return w->GetValue ();
322 return w->GetSelection ();
326 wx_get (wxSpinCtrlDouble* w)
328 return w->GetValue ();
331 /** @param s String of the form Context|String
332 * @return translation, or String if no translation is available.
335 context_translation (wxString s)
337 wxString t = wxGetTranslation (s);
339 /* No translation; strip the context */
340 int c = t.Find (wxT ("|"));
341 if (c != wxNOT_FOUND) {
350 time_to_timecode (DCPTime t, double fps)
352 double w = t.seconds ();
353 int const h = (w / 3600);
355 int const m = (w / 60);
357 int const s = floor (w);
359 int const f = lrint (w * fps);
360 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
364 setup_audio_channels_choice (wxChoice* choice, int minimum)
366 vector<pair<string, string> > items;
367 for (int i = minimum; i <= 16; i += 2) {
369 items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
371 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
373 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
375 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
376 } else if (i == 12) {
377 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
379 items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
383 checked_set (choice, items);