2 Copyright (C) 2012 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);
120 int const ThreadedStaticText::_update_event_id = 10000;
122 /** @param parent Parent for the wxStaticText.
123 * @param initial Initial text for the wxStaticText while the computation is being run.
124 * @param fn Function which works out what the wxStaticText content should be and returns it.
126 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, wxString initial, function<string ()> fn)
127 : wxStaticText (parent, wxID_ANY, initial)
129 Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ThreadedStaticText::thread_finished, this, _1), _update_event_id);
130 _thread = new thread (bind (&ThreadedStaticText::run, this, fn));
133 ThreadedStaticText::~ThreadedStaticText ()
135 _thread->interrupt ();
140 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
142 ThreadedStaticText::run (function<string ()> fn)
145 wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
146 ev.SetString (std_to_wx (fn ()));
147 GetEventHandler()->AddPendingEvent (ev);
151 /* Ignore exceptions; marginally better than the program quitting, but
156 /** Called in the GUI thread when our worker thread has finished */
158 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
160 SetLabel (ev.GetString ());
165 string_client_data (wxClientData* o)
167 return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
171 checked_set (wxFilePickerCtrl* widget, string value)
173 if (widget->GetPath() != std_to_wx (value)) {
175 /* Hack to make wxWidgets clear the control when we are passed
180 widget->SetPath (std_to_wx (value));
185 checked_set (wxSpinCtrl* widget, int value)
187 if (widget->GetValue() != value) {
188 widget->SetValue (value);
193 checked_set (wxChoice* widget, int value)
195 if (widget->GetSelection() != value) {
196 widget->SetSelection (value);
201 checked_set (wxChoice* widget, string value)
204 if (widget->GetSelection() != -1) {
205 o = widget->GetClientObject (widget->GetSelection ());
208 if (!o || string_client_data(o) != value) {
209 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
210 if (string_client_data (widget->GetClientObject (i)) == value) {
211 widget->SetSelection (i);
218 checked_set (wxTextCtrl* widget, string value)
220 if (widget->GetValue() != std_to_wx (value)) {
221 widget->ChangeValue (std_to_wx (value));
226 checked_set (wxStaticText* widget, string value)
228 if (widget->GetLabel() != std_to_wx (value)) {
229 widget->SetLabel (std_to_wx (value));
234 checked_set (wxCheckBox* widget, bool value)
236 if (widget->GetValue() != value) {
237 widget->SetValue (value);
242 checked_set (wxRadioButton* widget, bool value)
244 if (widget->GetValue() != value) {
245 widget->SetValue (value);
250 dcpomatic_setup_i18n ()
252 int language = wxLANGUAGE_DEFAULT;
254 boost::optional<string> config_lang = Config::instance()->language ();
255 if (config_lang && !config_lang->empty ()) {
256 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
258 language = li->Language;
262 wxLocale* locale = 0;
263 if (wxLocale::IsAvailable (language)) {
264 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
266 #ifdef DCPOMATIC_WINDOWS
267 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
270 #ifdef DCPOMATIC_POSIX
271 locale->AddCatalogLookupPathPrefix (POSIX_LOCALE_PREFIX);
274 locale->AddCatalog (wxT ("libdcpomatic-wx"));
275 locale->AddCatalog (wxT ("dcpomatic"));
277 if (!locale->IsOk()) {
279 locale = new wxLocale (wxLANGUAGE_ENGLISH);
284 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
289 wx_get (wxSpinCtrl* w)
291 return w->GetValue ();
297 return w->GetSelection ();
303 while (wxTheApp->Pending ()) {
304 wxTheApp->Dispatch ();