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