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/raw_convert.h>
30 #include <wx/spinctrl.h>
31 #include <boost/thread.hpp>
34 using namespace boost;
36 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
37 * @param s Sizer to add to.
38 * @param p Parent window for the wxStaticText.
39 * @param t Text for the wxStaticText.
40 * @param left true if this label is a `left label'; ie the sort
41 * of label which should be right-aligned on OS X.
42 * @param prop Proportion to pass when calling Add() on the wxSizer.
46 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
48 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
51 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
54 flags |= wxALIGN_RIGHT;
58 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
59 s->Add (m, prop, flags, 6);
65 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
67 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
70 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
73 flags |= wxALIGN_RIGHT;
77 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
78 s->Add (m, pos, span, flags);
82 /** Pop up an error dialogue box.
83 * @param parent Parent.
87 error_dialog (wxWindow* parent, wxString m)
89 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
94 /** Pop up an error dialogue box.
95 * @param parent Parent.
99 message_dialog (wxWindow* parent, wxString m)
101 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
107 confirm_dialog (wxWindow* parent, wxString m)
109 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
110 int const r = d->ShowModal ();
112 return r == wxID_YES;
116 /** @param s wxWidgets string.
117 * @return Corresponding STL string.
120 wx_to_std (wxString s)
122 return string (s.ToUTF8 ());
125 /** @param s STL string.
126 * @return Corresponding wxWidgets string.
131 return wxString (s.c_str(), wxConvUTF8);
135 string_client_data (wxClientData* o)
137 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
141 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
143 if (widget->GetPath() != std_to_wx (value.string())) {
145 /* Hack to make wxWidgets clear the control when we are passed
150 widget->SetPath (std_to_wx (value.string()));
155 checked_set (wxSpinCtrl* widget, int value)
157 if (widget->GetValue() != value) {
158 widget->SetValue (value);
163 checked_set (wxSpinCtrlDouble* widget, double value)
165 /* XXX: completely arbitrary epsilon */
166 if (fabs (widget->GetValue() - value) > 1e-16) {
167 widget->SetValue (value);
172 checked_set (wxChoice* widget, int value)
174 if (widget->GetSelection() != value) {
175 widget->SetSelection (value);
180 checked_set (wxChoice* widget, string value)
183 if (widget->GetSelection() != -1) {
184 o = widget->GetClientObject (widget->GetSelection ());
187 if (!o || string_client_data(o) != value) {
188 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
189 if (string_client_data (widget->GetClientObject (i)) == value) {
190 widget->SetSelection (i);
197 checked_set (wxChoice* widget, vector<pair<string, string> > items)
199 vector<pair<string, string> > current;
200 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
203 wx_to_std (widget->GetString (i)),
204 string_client_data (widget->GetClientObject (i))
209 if (current == items) {
214 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
215 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
220 checked_set (wxTextCtrl* widget, string value)
222 if (widget->GetValue() != std_to_wx (value)) {
223 widget->ChangeValue (std_to_wx (value));
228 checked_set (wxTextCtrl* widget, wxString value)
230 if (widget->GetValue() != value) {
231 widget->ChangeValue (value);
236 checked_set (wxStaticText* widget, string value)
238 if (widget->GetLabel() != std_to_wx (value)) {
239 widget->SetLabel (std_to_wx (value));
244 checked_set (wxStaticText* widget, wxString value)
246 if (widget->GetLabel() != value) {
247 widget->SetLabel (value);
252 checked_set (wxCheckBox* widget, bool value)
254 if (widget->GetValue() != value) {
255 widget->SetValue (value);
260 checked_set (wxRadioButton* widget, bool value)
262 if (widget->GetValue() != value) {
263 widget->SetValue (value);
268 dcpomatic_setup_i18n ()
270 int language = wxLANGUAGE_DEFAULT;
272 boost::optional<string> config_lang = Config::instance()->language ();
273 if (config_lang && !config_lang->empty ()) {
274 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
276 language = li->Language;
280 wxLocale* locale = 0;
281 if (wxLocale::IsAvailable (language)) {
282 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
284 #ifdef DCPOMATIC_WINDOWS
285 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
288 #ifdef DCPOMATIC_LINUX
289 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
291 /* We have to include the wxWidgets .mo in our distribution,
292 so we rename it to avoid clashes with any other installation
295 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
298 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
299 locale->AddCatalog (wxT ("dcpomatic2"));
301 if (!locale->IsOk()) {
303 locale = new wxLocale (wxLANGUAGE_ENGLISH);
308 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
313 wx_get (wxSpinCtrl* w)
315 return w->GetValue ();
321 return w->GetSelection ();
325 wx_get (wxSpinCtrlDouble* w)
327 return w->GetValue ();
330 /** @param s String of the form Context|String
331 * @return translation, or String if no translation is available.
334 context_translation (wxString s)
336 wxString t = wxGetTranslation (s);
338 /* No translation; strip the context */
339 int c = t.Find (wxT ("|"));
340 if (c != wxNOT_FOUND) {
349 time_to_timecode (DCPTime t, double fps)
351 double w = t.seconds ();
352 int const h = (w / 3600);
354 int const m = (w / 60);
356 int const s = floor (w);
358 int const f = lrint (w * fps);
359 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
363 setup_audio_channels_choice (wxChoice* choice, int minimum)
365 vector<pair<string, string> > items;
366 for (int i = minimum; i <= 16; i += 2) {
368 items.push_back (make_pair (wx_to_std (_("2 - stereo")), dcp::raw_convert<string> (i)));
370 items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), dcp::raw_convert<string> (i)));
372 items.push_back (make_pair (wx_to_std (_("6 - 5.1")), dcp::raw_convert<string> (i)));
374 items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), dcp::raw_convert<string> (i)));
375 } else if (i == 12) {
376 items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), dcp::raw_convert<string> (i)));
378 items.push_back (make_pair (dcp::raw_convert<string> (i), dcp::raw_convert<string> (i)));
382 checked_set (choice, items);