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