2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/wx/wx_util.cc
21 * @brief Some utility functions and classes.
24 #include <boost/thread.hpp>
25 #include <wx/filepicker.h>
26 #include <wx/spinctrl.h>
27 #include "lib/config.h"
32 using namespace boost;
34 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
35 * @param s Sizer to add to.
36 * @param p Parent window for the wxStaticText.
37 * @param t Text for the wxStaticText.
38 * @param left true if this label is a `left label'; ie the sort
39 * of label which should be right-aligned on OS X.
40 * @param prop Proportion to pass when calling Add() on the wxSizer.
44 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
46 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
49 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
52 flags |= wxALIGN_RIGHT;
56 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
57 s->Add (m, prop, flags, 6);
63 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
65 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
68 int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
71 flags |= wxALIGN_RIGHT;
75 wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
76 s->Add (m, pos, span, flags);
80 /** Pop up an error dialogue box.
81 * @param parent Parent.
85 error_dialog (wxWindow* parent, wxString m)
87 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK);
93 confirm_dialog (wxWindow* parent, wxString m)
95 wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
96 int const r = d->ShowModal ();
102 /** @param s wxWidgets string.
103 * @return Corresponding STL string.
106 wx_to_std (wxString s)
108 return string (s.ToUTF8 ());
111 /** @param s STL string.
112 * @return Corresponding wxWidgets string.
117 return wxString (s.c_str(), wxConvUTF8);
121 string_client_data (wxClientData* o)
123 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
127 checked_set (wxFilePickerCtrl* widget, string value)
129 if (widget->GetPath() != std_to_wx (value)) {
131 /* Hack to make wxWidgets clear the control when we are passed
136 widget->SetPath (std_to_wx (value));
141 checked_set (wxSpinCtrl* widget, int value)
143 if (widget->GetValue() != value) {
144 widget->SetValue (value);
149 checked_set (wxSpinCtrlDouble* widget, double value)
151 /* XXX: completely arbitrary epsilon */
152 if (fabs (widget->GetValue() - value) > 1e-16) {
153 widget->SetValue (value);
158 checked_set (wxChoice* widget, int value)
160 if (widget->GetSelection() != value) {
161 widget->SetSelection (value);
166 checked_set (wxChoice* widget, string value)
169 if (widget->GetSelection() != -1) {
170 o = widget->GetClientObject (widget->GetSelection ());
173 if (!o || string_client_data(o) != value) {
174 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
175 if (string_client_data (widget->GetClientObject (i)) == value) {
176 widget->SetSelection (i);
183 checked_set (wxChoice* widget, vector<pair<string, string> > items)
185 vector<pair<string, string> > current;
186 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
189 wx_to_std (widget->GetString (i)),
190 string_client_data (widget->GetClientObject (i))
195 if (current == items) {
200 for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
201 widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
206 checked_set (wxTextCtrl* widget, string value)
208 if (widget->GetValue() != std_to_wx (value)) {
209 widget->ChangeValue (std_to_wx (value));
214 checked_set (wxTextCtrl* widget, wxString value)
216 if (widget->GetValue() != value) {
217 widget->ChangeValue (value);
222 checked_set (wxStaticText* widget, string value)
224 if (widget->GetLabel() != std_to_wx (value)) {
225 widget->SetLabel (std_to_wx (value));
230 checked_set (wxStaticText* widget, wxString value)
232 if (widget->GetLabel() != value) {
233 widget->SetLabel (value);
238 checked_set (wxCheckBox* widget, bool value)
240 if (widget->GetValue() != value) {
241 widget->SetValue (value);
246 checked_set (wxRadioButton* widget, bool value)
248 if (widget->GetValue() != value) {
249 widget->SetValue (value);
254 dcpomatic_setup_i18n ()
256 int language = wxLANGUAGE_DEFAULT;
258 boost::optional<string> config_lang = Config::instance()->language ();
259 if (config_lang && !config_lang->empty ()) {
260 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
262 language = li->Language;
266 wxLocale* locale = 0;
267 if (wxLocale::IsAvailable (language)) {
268 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
270 #ifdef DCPOMATIC_WINDOWS
271 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
274 #ifdef DCPOMATIC_LINUX
275 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
277 /* We have to include the wxWidgets .mo in our distribution,
278 so we rename it to avoid clashes with any other installation
281 locale->AddCatalog (wxT ("dcpomatic-wxstd"));
284 locale->AddCatalog (wxT ("libdcpomatic-wx"));
285 locale->AddCatalog (wxT ("dcpomatic"));
287 if (!locale->IsOk()) {
289 locale = new wxLocale (wxLANGUAGE_ENGLISH);
294 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
299 wx_get (wxSpinCtrl* w)
301 return w->GetValue ();
307 return w->GetSelection ();
311 wx_get (wxSpinCtrlDouble* w)
313 return w->GetValue ();
316 /** @param s String of the form Context|String
317 * @return translation, or String if no translation is available.
320 context_translation (wxString s)
322 wxString t = wxGetTranslation (s);
324 /* No translation; strip the context */
325 int c = t.Find (wxT ("|"));
326 if (c != wxNOT_FOUND) {
335 time_to_timecode (DCPTime t, float fps)
337 double w = t.seconds ();
338 int const h = (w / 3600);
340 int const m = (w / 60);
342 int const s = floor (w);
344 int const f = rint (w * fps);
345 return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);