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