2 Copyright (C) 2012-2015 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 <wx/spinctrl.h>
30 #include <boost/thread.hpp>
33 using namespace boost;
35 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
36 * @param s Sizer to add to.
37 * @param p Parent window for the wxStaticText.
38 * @param t Text for the wxStaticText.
39 * @param left true if this label is a `left label'; ie the sort
40 * of label which should be right-aligned on OS X.
41 * @param prop Proportion to pass when calling Add() on the wxSizer.
45 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
47 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
50 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
53 flags |= wxALIGN_RIGHT;
57 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
58 s->Add (m, prop, flags, 6);
64 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
66 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
69 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
72 flags |= wxALIGN_RIGHT;
76 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
77 s->Add (m, pos, span, flags);
81 /** Pop up an error dialogue box.
82 * @param parent Parent.
86 error_dialog (wxWindow* parent, wxString m)
88 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
93 /** Pop up an error dialogue box.
94 * @param parent Parent.
98 message_dialog (wxWindow* parent, wxString m)
100 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
106 confirm_dialog (wxWindow* parent, wxString m)
108 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
109 int const r = d->ShowModal ();
111 return r == wxID_YES;
115 /** @param s wxWidgets string.
116 * @return Corresponding STL string.
119 wx_to_std (wxString s)
121 return string (s.ToUTF8 ());
124 /** @param s STL string.
125 * @return Corresponding wxWidgets string.
130 return wxString (s.c_str(), wxConvUTF8);
134 string_client_data (wxClientData* o)
136 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
140 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
142 if (widget->GetPath() != std_to_wx (value.string())) {
144 /* Hack to make wxWidgets clear the control when we are passed
149 widget->SetPath (std_to_wx (value.string()));
154 checked_set (wxSpinCtrl* widget, int value)
156 if (widget->GetValue() != value) {
157 widget->SetValue (value);
162 checked_set (wxSpinCtrlDouble* widget, double value)
164 /* XXX: completely arbitrary epsilon */
165 if (fabs (widget->GetValue() - value) > 1e-16) {
166 widget->SetValue (value);
171 checked_set (wxChoice* widget, int value)
173 if (widget->GetSelection() != value) {
174 widget->SetSelection (value);
179 checked_set (wxChoice* widget, string value)
182 if (widget->GetSelection() != -1) {
183 o = widget->GetClientObject (widget->GetSelection ());
186 if (!o || string_client_data(o) != value) {
187 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
188 if (string_client_data (widget->GetClientObject (i)) == value) {
189 widget->SetSelection (i);
196 checked_set (wxChoice* widget, vector<pair<string, string> > items)
198 vector<pair<string, string> > current;
199 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
202 wx_to_std (widget->GetString (i)),
203 string_client_data (widget->GetClientObject (i))
208 if (current == items) {
213 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
214 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
219 checked_set (wxTextCtrl* widget, string value)
221 if (widget->GetValue() != std_to_wx (value)) {
222 widget->ChangeValue (std_to_wx (value));
227 checked_set (wxTextCtrl* widget, wxString value)
229 if (widget->GetValue() != value) {
230 widget->ChangeValue (value);
235 checked_set (wxStaticText* widget, string value)
237 if (widget->GetLabel() != std_to_wx (value)) {
238 widget->SetLabel (std_to_wx (value));
243 checked_set (wxStaticText* widget, wxString value)
245 if (widget->GetLabel() != value) {
246 widget->SetLabel (value);
251 checked_set (wxCheckBox* widget, bool value)
253 if (widget->GetValue() != value) {
254 widget->SetValue (value);
259 checked_set (wxRadioButton* widget, bool value)
261 if (widget->GetValue() != value) {
262 widget->SetValue (value);
267 dcpomatic_setup_i18n ()
269 int language = wxLANGUAGE_DEFAULT;
271 boost::optional<string> config_lang = Config::instance()->language ();
272 if (config_lang && !config_lang->empty ()) {
273 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
275 language = li->Language;
279 wxLocale* locale = 0;
280 if (wxLocale::IsAvailable (language)) {
281 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
283 #ifdef DCPOMATIC_WINDOWS
284 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
287 #ifdef DCPOMATIC_LINUX
288 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
290 /* We have to include the wxWidgets .mo in our distribution,
291 so we rename it to avoid clashes with any other installation
294 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
297 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
298 locale->AddCatalog (wxT ("dcpomatic2"));
300 if (!locale->IsOk()) {
302 locale = new wxLocale (wxLANGUAGE_ENGLISH);
307 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
312 wx_get (wxSpinCtrl* w)
314 return w->GetValue ();
320 return w->GetSelection ();
324 wx_get (wxSpinCtrlDouble* w)
326 return w->GetValue ();
329 /** @param s String of the form Context|String
330 * @return translation, or String if no translation is available.
333 context_translation (wxString s)
335 wxString t = wxGetTranslation (s);
337 /* No translation; strip the context */
338 int c = t.Find (wxT ("|"));
339 if (c != wxNOT_FOUND) {
348 time_to_timecode (DCPTime t, double fps)
350 double w = t.seconds ();
351 int const h = (w / 3600);
353 int const m = (w / 60);
355 int const s = floor (w);
357 int const f = lrint (w * fps);
358 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);