Capitalise first letter of extended errors for appearance sake.
[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                 wxString em = *e;
134                 em[0] = wxToupper (em[0]);
135                 d->SetExtendedMessage (em);
136         }
137         d->ShowModal ();
138         d->Destroy ();
139 }
140
141 /** Pop up an error dialogue box.
142  *  @param parent Parent.
143  *  @param m Message.
144  */
145 void
146 message_dialog (wxWindow* parent, wxString m)
147 {
148         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
149         d->ShowModal ();
150         d->Destroy ();
151 }
152
153 bool
154 confirm_dialog (wxWindow* parent, wxString m)
155 {
156         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
157         int const r = d->ShowModal ();
158         d->Destroy ();
159         return r == wxID_YES;
160 }
161
162
163 /** @param s wxWidgets string.
164  *  @return Corresponding STL string.
165  */
166 string
167 wx_to_std (wxString s)
168 {
169         return string (s.ToUTF8 ());
170 }
171
172 /** @param s STL string.
173  *  @return Corresponding wxWidgets string.
174  */
175 wxString
176 std_to_wx (string s)
177 {
178         return wxString (s.c_str(), wxConvUTF8);
179 }
180
181 string
182 string_client_data (wxClientData* o)
183 {
184         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
185 }
186
187 void
188 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
189 {
190         if (widget->GetPath() != std_to_wx (value.string())) {
191                 if (value.empty()) {
192                         /* Hack to make wxWidgets clear the control when we are passed
193                            an empty value.
194                         */
195                         value = " ";
196                 }
197                 widget->SetPath (std_to_wx (value.string()));
198         }
199 }
200
201 void
202 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
203 {
204         if (widget->GetPath() != std_to_wx (value.string())) {
205                 if (value.empty()) {
206                         /* Hack to make wxWidgets clear the control when we are passed
207                            an empty value.
208                         */
209                         value = " ";
210                 }
211                 widget->SetPath (std_to_wx (value.string()));
212         }
213 }
214
215 void
216 checked_set (wxSpinCtrl* widget, int value)
217 {
218         if (widget->GetValue() != value) {
219                 widget->SetValue (value);
220         }
221 }
222
223 void
224 checked_set (wxSpinCtrlDouble* widget, double value)
225 {
226         /* XXX: completely arbitrary epsilon */
227         if (fabs (widget->GetValue() - value) > 1e-16) {
228                 widget->SetValue (value);
229         }
230 }
231
232 void
233 checked_set (wxChoice* widget, int value)
234 {
235         if (widget->GetSelection() != value) {
236                 widget->SetSelection (value);
237         }
238 }
239
240 void
241 checked_set (wxChoice* widget, string value)
242 {
243         wxClientData* o = 0;
244         if (widget->GetSelection() != -1) {
245                 o = widget->GetClientObject (widget->GetSelection ());
246         }
247
248         if (!o || string_client_data(o) != value) {
249                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
250                         if (string_client_data (widget->GetClientObject (i)) == value) {
251                                 widget->SetSelection (i);
252                         }
253                 }
254         }
255 }
256
257 void
258 checked_set (wxChoice* widget, vector<pair<string, string> > items)
259 {
260        vector<pair<string, string> > current;
261        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
262                current.push_back (
263                        make_pair (
264                                wx_to_std (widget->GetString (i)),
265                                string_client_data (widget->GetClientObject (i))
266                                )
267                        );
268        }
269
270        if (current == items) {
271                return;
272        }
273
274        widget->Clear ();
275        for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
276                widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
277        }
278 }
279
280 void
281 checked_set (wxTextCtrl* widget, string value)
282 {
283         if (widget->GetValue() != std_to_wx (value)) {
284                 widget->ChangeValue (std_to_wx (value));
285         }
286 }
287
288 void
289 checked_set (wxTextCtrl* widget, wxString value)
290 {
291         if (widget->GetValue() != value) {
292                 widget->ChangeValue (value);
293         }
294 }
295
296 void
297 checked_set (wxStaticText* widget, string value)
298 {
299         if (widget->GetLabel() != std_to_wx (value)) {
300                 widget->SetLabel (std_to_wx (value));
301         }
302 }
303
304 void
305 checked_set (wxStaticText* widget, wxString value)
306 {
307         if (widget->GetLabel() != value) {
308                 widget->SetLabel (value);
309         }
310 }
311
312 void
313 checked_set (wxCheckBox* widget, bool value)
314 {
315         if (widget->GetValue() != value) {
316                 widget->SetValue (value);
317         }
318 }
319
320 void
321 checked_set (wxRadioButton* widget, bool value)
322 {
323         if (widget->GetValue() != value) {
324                 widget->SetValue (value);
325         }
326 }
327
328 void
329 dcpomatic_setup_i18n ()
330 {
331         int language = wxLANGUAGE_DEFAULT;
332
333         boost::optional<string> config_lang = Config::instance()->language ();
334         if (config_lang && !config_lang->empty ()) {
335                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
336                 if (li) {
337                         language = li->Language;
338                 }
339         }
340
341         wxLocale* locale = 0;
342         if (wxLocale::IsAvailable (language)) {
343                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
344
345 #ifdef DCPOMATIC_WINDOWS
346                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
347 #endif
348
349 #ifdef DCPOMATIC_LINUX
350                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
351
352                 /* We have to include the wxWidgets .mo in our distribution,
353                    so we rename it to avoid clashes with any other installation
354                    of wxWidgets.
355                 */
356                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
357 #endif
358
359                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
360                 locale->AddCatalog (wxT ("dcpomatic2"));
361
362                 if (!locale->IsOk()) {
363                         delete locale;
364                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
365                 }
366         }
367
368         if (locale) {
369                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
370         }
371 }
372
373 int
374 wx_get (wxSpinCtrl* w)
375 {
376         return w->GetValue ();
377 }
378
379 int
380 wx_get (wxChoice* w)
381 {
382         return w->GetSelection ();
383 }
384
385 double
386 wx_get (wxSpinCtrlDouble* w)
387 {
388         return w->GetValue ();
389 }
390
391 /** @param s String of the form Context|String
392  *  @return translation, or String if no translation is available.
393  */
394 wxString
395 context_translation (wxString s)
396 {
397         wxString t = wxGetTranslation (s);
398         if (t == s) {
399                 /* No translation; strip the context */
400                 int c = t.Find (wxT ("|"));
401                 if (c != wxNOT_FOUND) {
402                         t = t.Mid (c + 1);
403                 }
404         }
405
406         return t;
407 }
408
409 wxString
410 time_to_timecode (DCPTime t, double fps)
411 {
412         double w = t.seconds ();
413         int const h = (w / 3600);
414         w -= h * 3600;
415         int const m = (w / 60);
416         w -= m * 60;
417         int const s = floor (w);
418         w -= s;
419         int const f = lrint (w * fps);
420         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
421 }
422
423 void
424 setup_audio_channels_choice (wxChoice* choice, int minimum)
425 {
426         vector<pair<string, string> > items;
427         for (int i = minimum; i <= 16; i += 2) {
428                 if (i == 2) {
429                         items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
430                 } else if (i == 4) {
431                         items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
432                 } else if (i == 6) {
433                         items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
434                 } else if (i == 8) {
435                         items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
436                 } else if (i == 12) {
437                         items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
438                 } else {
439                         items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
440                 }
441         }
442
443         checked_set (choice, items);
444 }
445
446 wxSplashScreen *
447 maybe_show_splash ()
448 {
449         wxSplashScreen* splash = 0;
450         try {
451                 if (!Config::have_existing ("config.xml")) {
452                         wxBitmap bitmap;
453                         boost::filesystem::path p = shared_path () / "splash.png";
454                         if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
455                                 splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
456                                 wxYield ();
457                         }
458                 }
459         } catch (boost::filesystem::filesystem_error& e) {
460                 /* Maybe we couldn't find the splash image; never mind */
461         }
462
463         return splash;
464 }
465
466 boost::filesystem::path
467 path_from_file_dialog (wxFileDialog* dialog, string extension)
468 {
469         return boost::filesystem::path(wx_to_std(dialog->GetPath())).replace_extension(extension);
470 }
471
472 double
473 calculate_mark_interval (double mark_interval)
474 {
475         if (mark_interval > 5) {
476                 mark_interval -= lrint (mark_interval) % 5;
477         }
478         if (mark_interval > 10) {
479                 mark_interval -= lrint (mark_interval) % 10;
480         }
481         if (mark_interval > 60) {
482                 mark_interval -= lrint (mark_interval) % 60;
483         }
484         if (mark_interval > 3600) {
485                 mark_interval -= lrint (mark_interval) % 3600;
486         }
487
488         if (mark_interval < 1) {
489                 mark_interval = 1;
490         }
491
492         return mark_interval;
493 }