Replace incorrect uses of raw_convert with a new locale_convert.
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file src/wx/wx_util.cc
22  *  @brief Some utility functions and classes.
23  */
24
25 #include "wx_util.h"
26 #include "file_picker_ctrl.h"
27 #include "lib/config.h"
28 #include "lib/util.h"
29 #include "lib/locale_convert.h"
30 #include <wx/spinctrl.h>
31 #include <boost/thread.hpp>
32
33 using namespace std;
34 using namespace boost;
35
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.
43  */
44 wxStaticText *
45 #ifdef __WXOSX__
46 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop)
47 #else
48 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool, int prop)
49 #endif
50 {
51         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
52 #ifdef __WXOSX__
53         if (left) {
54                 flags |= wxALIGN_RIGHT;
55                 t += wxT (":");
56         }
57 #endif
58         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
59         s->Add (m, prop, flags, 6);
60         return m;
61 }
62
63 wxStaticText *
64 #ifdef __WXOSX__
65 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
66 #else
67 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool, wxGBPosition pos, wxGBSpan span)
68 #endif
69 {
70         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
71 #ifdef __WXOSX__
72         if (left) {
73                 flags |= wxALIGN_RIGHT;
74                 t += wxT (":");
75         }
76 #endif
77         wxStaticText* m = new wxStaticText (p, wxID_ANY, t);
78         s->Add (m, pos, span, flags);
79         return m;
80 }
81
82 /** Pop up an error dialogue box.
83  *  @param parent Parent.
84  *  @param m Message.
85  */
86 void
87 error_dialog (wxWindow* parent, wxString m)
88 {
89         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
90         d->ShowModal ();
91         d->Destroy ();
92 }
93
94 /** Pop up an error dialogue box.
95  *  @param parent Parent.
96  *  @param m Message.
97  */
98 void
99 message_dialog (wxWindow* parent, wxString m)
100 {
101         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
102         d->ShowModal ();
103         d->Destroy ();
104 }
105
106 bool
107 confirm_dialog (wxWindow* parent, wxString m)
108 {
109         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
110         int const r = d->ShowModal ();
111         d->Destroy ();
112         return r == wxID_YES;
113 }
114
115
116 /** @param s wxWidgets string.
117  *  @return Corresponding STL string.
118  */
119 string
120 wx_to_std (wxString s)
121 {
122         return string (s.ToUTF8 ());
123 }
124
125 /** @param s STL string.
126  *  @return Corresponding wxWidgets string.
127  */
128 wxString
129 std_to_wx (string s)
130 {
131         return wxString (s.c_str(), wxConvUTF8);
132 }
133
134 string
135 string_client_data (wxClientData* o)
136 {
137         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
138 }
139
140 void
141 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
142 {
143         if (widget->GetPath() != std_to_wx (value.string())) {
144                 if (value.empty()) {
145                         /* Hack to make wxWidgets clear the control when we are passed
146                            an empty value.
147                         */
148                         value = " ";
149                 }
150                 widget->SetPath (std_to_wx (value.string()));
151         }
152 }
153
154 void
155 checked_set (wxSpinCtrl* widget, int value)
156 {
157         if (widget->GetValue() != value) {
158                 widget->SetValue (value);
159         }
160 }
161
162 void
163 checked_set (wxSpinCtrlDouble* widget, double value)
164 {
165         /* XXX: completely arbitrary epsilon */
166         if (fabs (widget->GetValue() - value) > 1e-16) {
167                 widget->SetValue (value);
168         }
169 }
170
171 void
172 checked_set (wxChoice* widget, int value)
173 {
174         if (widget->GetSelection() != value) {
175                 widget->SetSelection (value);
176         }
177 }
178
179 void
180 checked_set (wxChoice* widget, string value)
181 {
182         wxClientData* o = 0;
183         if (widget->GetSelection() != -1) {
184                 o = widget->GetClientObject (widget->GetSelection ());
185         }
186
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);
191                         }
192                 }
193         }
194 }
195
196 void
197 checked_set (wxChoice* widget, vector<pair<string, string> > items)
198 {
199        vector<pair<string, string> > current;
200        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
201                current.push_back (
202                        make_pair (
203                                wx_to_std (widget->GetString (i)),
204                                string_client_data (widget->GetClientObject (i))
205                                )
206                        );
207        }
208
209        if (current == items) {
210                return;
211        }
212
213        widget->Clear ();
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)));
216        }
217 }
218
219 void
220 checked_set (wxTextCtrl* widget, string value)
221 {
222         if (widget->GetValue() != std_to_wx (value)) {
223                 widget->ChangeValue (std_to_wx (value));
224         }
225 }
226
227 void
228 checked_set (wxTextCtrl* widget, wxString value)
229 {
230         if (widget->GetValue() != value) {
231                 widget->ChangeValue (value);
232         }
233 }
234
235 void
236 checked_set (wxStaticText* widget, string value)
237 {
238         if (widget->GetLabel() != std_to_wx (value)) {
239                 widget->SetLabel (std_to_wx (value));
240         }
241 }
242
243 void
244 checked_set (wxStaticText* widget, wxString value)
245 {
246         if (widget->GetLabel() != value) {
247                 widget->SetLabel (value);
248         }
249 }
250
251 void
252 checked_set (wxCheckBox* widget, bool value)
253 {
254         if (widget->GetValue() != value) {
255                 widget->SetValue (value);
256         }
257 }
258
259 void
260 checked_set (wxRadioButton* widget, bool value)
261 {
262         if (widget->GetValue() != value) {
263                 widget->SetValue (value);
264         }
265 }
266
267 void
268 dcpomatic_setup_i18n ()
269 {
270         int language = wxLANGUAGE_DEFAULT;
271
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 ()));
275                 if (li) {
276                         language = li->Language;
277                 }
278         }
279
280         wxLocale* locale = 0;
281         if (wxLocale::IsAvailable (language)) {
282                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
283
284 #ifdef DCPOMATIC_WINDOWS
285                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
286 #endif
287
288 #ifdef DCPOMATIC_LINUX
289                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
290
291                 /* We have to include the wxWidgets .mo in our distribution,
292                    so we rename it to avoid clashes with any other installation
293                    of wxWidgets.
294                 */
295                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
296 #endif
297
298                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
299                 locale->AddCatalog (wxT ("dcpomatic2"));
300
301                 if (!locale->IsOk()) {
302                         delete locale;
303                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
304                 }
305         }
306
307         if (locale) {
308                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
309         }
310 }
311
312 int
313 wx_get (wxSpinCtrl* w)
314 {
315         return w->GetValue ();
316 }
317
318 int
319 wx_get (wxChoice* w)
320 {
321         return w->GetSelection ();
322 }
323
324 double
325 wx_get (wxSpinCtrlDouble* w)
326 {
327         return w->GetValue ();
328 }
329
330 /** @param s String of the form Context|String
331  *  @return translation, or String if no translation is available.
332  */
333 wxString
334 context_translation (wxString s)
335 {
336         wxString t = wxGetTranslation (s);
337         if (t == s) {
338                 /* No translation; strip the context */
339                 int c = t.Find (wxT ("|"));
340                 if (c != wxNOT_FOUND) {
341                         t = t.Mid (c + 1);
342                 }
343         }
344
345         return t;
346 }
347
348 wxString
349 time_to_timecode (DCPTime t, double fps)
350 {
351         double w = t.seconds ();
352         int const h = (w / 3600);
353         w -= h * 3600;
354         int const m = (w / 60);
355         w -= m * 60;
356         int const s = floor (w);
357         w -= s;
358         int const f = lrint (w * fps);
359         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
360 }
361
362 void
363 setup_audio_channels_choice (wxChoice* choice, int minimum)
364 {
365         vector<pair<string, string> > items;
366         for (int i = minimum; i <= 16; i += 2) {
367                 if (i == 2) {
368                         items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
369                 } else if (i == 4) {
370                         items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
371                 } else if (i == 6) {
372                         items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
373                 } else if (i == 8) {
374                         items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
375                 } else if (i == 12) {
376                         items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
377                 } else {
378                         items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
379                 }
380         }
381
382         checked_set (choice, items);
383 }