Merge remote-tracking branch 'origin/master' into 2.0
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/wx/wx_util.cc
21  *  @brief Some utility functions and classes.
22  */
23
24 #include <boost/thread.hpp>
25 #include <wx/filepicker.h>
26 #include <wx/spinctrl.h>
27 #include "lib/config.h"
28 #include "lib/util.h"
29 #include "wx_util.h"
30
31 using namespace std;
32 using namespace boost;
33
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.
41  */
42 wxStaticText *
43 #ifdef __WXOSX__
44 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
45 #else
46 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
47 #endif
48 {
49         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
50 #ifdef __WXOSX__
51         if (left) {
52                 flags |= wxALIGN_RIGHT;
53                 t += wxT (":");
54         }
55 #endif  
56         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
57         s->Add (m, prop, flags, 6);
58         return m;
59 }
60
61 wxStaticText *
62 #ifdef __WXOSX__
63 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
64 #else
65 add_label_to_grid_bag_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
66 #endif
67 {
68         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
69 #ifdef __WXOSX__
70         if (left) {
71                 flags |= wxALIGN_RIGHT;
72                 t += wxT (":");
73         }
74 #endif  
75         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
76         s->Add (m, pos, span, flags);
77         return m;
78 }
79
80 /** Pop up an error dialogue box.
81  *  @param parent Parent.
82  *  @param m Message.
83  */
84 void
85 error_dialog (wxWindow* parent, wxString m)
86 {
87         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK);
88         d->ShowModal ();
89         d->Destroy ();
90 }
91
92 bool
93 confirm_dialog (wxWindow* parent, wxString m)
94 {
95         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
96         int const r = d->ShowModal ();
97         d->Destroy ();
98         return r == wxID_YES;
99 }
100         
101
102 /** @param s wxWidgets string.
103  *  @return Corresponding STL string.
104  */
105 string
106 wx_to_std (wxString s)
107 {
108         return string (s.ToUTF8 ());
109 }
110
111 /** @param s STL string.
112  *  @return Corresponding wxWidgets string.
113  */
114 wxString
115 std_to_wx (string s)
116 {
117         return wxString (s.c_str(), wxConvUTF8);
118 }
119
120 int const ThreadedStaticText::_update_event_id = 10000;
121
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.
125  */
126 ThreadedStaticText::ThreadedStaticText (wxWindow* parent, wxString initial, boost::function<string ()> fn)
127         : wxStaticText (parent, wxID_ANY, initial)
128 {
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));
131 }
132
133 ThreadedStaticText::~ThreadedStaticText ()
134 {
135         _thread->interrupt ();
136         _thread->join ();
137         delete _thread;
138 }
139
140 /** Run our thread and post the result to the GUI thread via AddPendingEvent */
141 void
142 ThreadedStaticText::run (boost::function<string ()> fn)
143 try
144 {
145         wxCommandEvent ev (wxEVT_COMMAND_TEXT_UPDATED, _update_event_id);
146         ev.SetString (std_to_wx (fn ()));
147         GetEventHandler()->AddPendingEvent (ev);
148 }
149 catch (...)
150 {
151         /* Ignore exceptions; marginally better than the program quitting, but
152            only marginally.
153         */
154 }
155
156 /** Called in the GUI thread when our worker thread has finished */
157 void
158 ThreadedStaticText::thread_finished (wxCommandEvent& ev)
159 {
160         SetLabel (ev.GetString ());
161         Finished ();
162 }
163
164 string
165 string_client_data (wxClientData* o)
166 {
167         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
168 }
169
170 void
171 checked_set (wxFilePickerCtrl* widget, string value)
172 {
173         if (widget->GetPath() != std_to_wx (value)) {
174                 if (value.empty()) {
175                         /* Hack to make wxWidgets clear the control when we are passed
176                            an empty value.
177                         */
178                         value = " ";
179                 }
180                 widget->SetPath (std_to_wx (value));
181         }
182 }
183
184 void
185 checked_set (wxSpinCtrl* widget, int value)
186 {
187         if (widget->GetValue() != value) {
188                 widget->SetValue (value);
189         }
190 }
191
192 void
193 checked_set (wxSpinCtrlDouble* widget, double value)
194 {
195         /* XXX: completely arbitrary epsilon */
196         if (fabs (widget->GetValue() - value) > 1e-16) {
197                 widget->SetValue (value);
198         }
199 }
200
201 void
202 checked_set (wxChoice* widget, int value)
203 {
204         if (widget->GetSelection() != value) {
205                 widget->SetSelection (value);
206         }
207 }
208
209 void
210 checked_set (wxChoice* widget, string value)
211 {
212         wxClientData* o = 0;
213         if (widget->GetSelection() != -1) {
214                 o = widget->GetClientObject (widget->GetSelection ());
215         }
216         
217         if (!o || string_client_data(o) != value) {
218                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
219                         if (string_client_data (widget->GetClientObject (i)) == value) {
220                                 widget->SetSelection (i);
221                         }
222                 }
223         }
224 }
225
226 void
227 checked_set (wxTextCtrl* widget, string value)
228 {
229         if (widget->GetValue() != std_to_wx (value)) {
230                 widget->ChangeValue (std_to_wx (value));
231         }
232 }
233
234 void
235 checked_set (wxStaticText* widget, string value)
236 {
237         if (widget->GetLabel() != std_to_wx (value)) {
238                 widget->SetLabel (std_to_wx (value));
239         }
240 }
241
242 void
243 checked_set (wxCheckBox* widget, bool value)
244 {
245         if (widget->GetValue() != value) {
246                 widget->SetValue (value);
247         }
248 }
249
250 void
251 checked_set (wxRadioButton* widget, bool value)
252 {
253         if (widget->GetValue() != value) {
254                 widget->SetValue (value);
255         }
256 }
257
258 void
259 dcpomatic_setup_i18n ()
260 {
261         int language = wxLANGUAGE_DEFAULT;
262
263         boost::optional<string> config_lang = Config::instance()->language ();
264         if (config_lang && !config_lang->empty ()) {
265                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
266                 if (li) {
267                         language = li->Language;
268                 }
269         }
270
271         wxLocale* locale = 0;
272         if (wxLocale::IsAvailable (language)) {
273                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
274
275 #ifdef DCPOMATIC_WINDOWS
276                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
277 #endif          
278
279 #ifdef DCPOMATIC_POSIX
280                 locale->AddCatalogLookupPathPrefix (POSIX_LOCALE_PREFIX);
281 #endif
282
283                 locale->AddCatalog (wxT ("libdcpomatic-wx"));
284                 locale->AddCatalog (wxT ("dcpomatic"));
285                 
286                 if (!locale->IsOk()) {
287                         delete locale;
288                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
289                 }
290         }
291
292         if (locale) {
293                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
294         }
295 }
296
297 int
298 wx_get (wxSpinCtrl* w)
299 {
300         return w->GetValue ();
301 }
302
303 int
304 wx_get (wxChoice* w)
305 {
306         return w->GetSelection ();
307 }
308
309 double
310 wx_get (wxSpinCtrlDouble* w)
311 {
312         return w->GetValue ();
313 }
314
315 /** @param s String of the form Context|String
316  *  @return translation, or String if no translation is available.
317  */
318 wxString
319 context_translation (wxString s)
320 {
321         wxString t = wxGetTranslation (s);
322         if (t == s) {
323                 /* No translation; strip the context */
324                 int c = t.Find (wxT ("|"));
325                 if (c != wxNOT_FOUND) {
326                         t = t.Mid (c + 1);
327                 }
328         }
329
330         return t;
331 }