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