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