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