Re-work scaling following excellent insights by Carsten Kurz, described
[dcpomatic.git] / src / wx / wx_util.cc
1 /*
2     Copyright (C) 2012-2019 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 "static_text.h"
28 #include "password_entry.h"
29 #include "lib/config.h"
30 #include "lib/job_manager.h"
31 #include "lib/util.h"
32 #include "lib/cross.h"
33 #include "lib/job.h"
34 #include <dcp/locale_convert.h>
35 #include <wx/spinctrl.h>
36 #include <wx/splash.h>
37 #include <wx/progdlg.h>
38 #include <wx/filepicker.h>
39 #include <boost/thread.hpp>
40
41 using std::string;
42 using std::vector;
43 using std::pair;
44 using boost::shared_ptr;
45 using boost::optional;
46 using dcp::locale_convert;
47 using namespace dcpomatic;
48
49 wxStaticText *
50 #ifdef __WXOSX__
51 create_label (wxWindow* p, wxString t, bool left)
52 #else
53 create_label (wxWindow* p, wxString t, bool)
54 #endif
55 {
56 #ifdef __WXOSX__
57         if (left) {
58                 t += wxT (":");
59         }
60 #endif
61         return new StaticText (p, t);
62 }
63
64 /** Add a wxStaticText to a wxSizer, aligning it at vertical centre.
65  *  @param s Sizer to add to.
66  *  @param p Parent window for the wxStaticText.
67  *  @param t Text for the wxStaticText.
68  *  @param left true if this label is a `left label'; ie the sort
69  *  of label which should be right-aligned on OS X.
70  *  @param prop Proportion to pass when calling Add() on the wxSizer.
71  */
72 wxStaticText *
73 add_label_to_sizer (wxSizer* s, wxWindow* p, wxString t, bool left, int prop, int flags)
74 {
75 #ifdef __WXOSX__
76         if (left) {
77                 flags |= wxALIGN_RIGHT;
78         }
79 #endif
80         wxStaticText* m = create_label (p, t, left);
81         s->Add (m, prop, flags, 6);
82         return m;
83 }
84
85 wxStaticText *
86 #ifdef __WXOSX__
87 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool left, int prop, int flags)
88 #else
89 add_label_to_sizer (wxSizer* s, wxStaticText* t, bool, int prop, int flags)
90 #endif
91 {
92 #ifdef __WXOSX__
93         if (left) {
94                 flags |= wxALIGN_RIGHT;
95         }
96 #endif
97         s->Add (t, prop, flags, 6);
98         return t;
99 }
100
101 wxStaticText *
102 add_label_to_sizer (wxGridBagSizer* s, wxWindow* p, wxString t, bool left, wxGBPosition pos, wxGBSpan span)
103 {
104         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
105 #ifdef __WXOSX__
106         if (left) {
107                 flags |= wxALIGN_RIGHT;
108         }
109 #endif
110         wxStaticText* m = create_label (p, t, left);
111         s->Add (m, pos, span, flags);
112         return m;
113 }
114
115 wxStaticText *
116 #ifdef __WXOSX__
117 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool left, wxGBPosition pos, wxGBSpan span)
118 #else
119 add_label_to_sizer (wxGridBagSizer* s, wxStaticText* t, bool, wxGBPosition pos, wxGBSpan span)
120 #endif
121 {
122         int flags = wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT;
123 #ifdef __WXOSX__
124         if (left) {
125                 flags |= wxALIGN_RIGHT;
126         }
127 #endif
128         s->Add (t, pos, span, flags);
129         return t;
130 }
131
132 /** Pop up an error dialogue box.
133  *  @param parent Parent.
134  *  @param m Message.
135  *  @param e Extended message.
136  */
137 void
138 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
139 {
140         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
141         if (e) {
142                 wxString em = *e;
143                 em[0] = wxToupper (em[0]);
144                 d->SetExtendedMessage (em);
145         }
146         d->ShowModal ();
147         d->Destroy ();
148 }
149
150 /** Pop up an error dialogue box.
151  *  @param parent Parent.
152  *  @param m Message.
153  */
154 void
155 message_dialog (wxWindow* parent, wxString m)
156 {
157         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
158         d->ShowModal ();
159         d->Destroy ();
160 }
161
162 /** @return true if the user answered "yes" */
163 bool
164 confirm_dialog (wxWindow* parent, wxString m)
165 {
166         wxMessageDialog* d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
167         int const r = d->ShowModal ();
168         d->Destroy ();
169         return r == wxID_YES;
170 }
171
172
173 /** @param s wxWidgets string.
174  *  @return Corresponding STL string.
175  */
176 string
177 wx_to_std (wxString s)
178 {
179         return string (s.ToUTF8 ());
180 }
181
182 /** @param s STL string.
183  *  @return Corresponding wxWidgets string.
184  */
185 wxString
186 std_to_wx (string s)
187 {
188         return wxString (s.c_str(), wxConvUTF8);
189 }
190
191 string
192 string_client_data (wxClientData* o)
193 {
194         return wx_to_std (dynamic_cast<wxStringClientData*>(o)->GetData());
195 }
196
197 void
198 checked_set (FilePickerCtrl* widget, boost::filesystem::path value)
199 {
200         if (widget->GetPath() != std_to_wx (value.string())) {
201                 if (value.empty()) {
202                         /* Hack to make wxWidgets clear the control when we are passed
203                            an empty value.
204                         */
205                         value = " ";
206                 }
207                 widget->SetPath (std_to_wx (value.string()));
208         }
209 }
210
211 void
212 checked_set (wxDirPickerCtrl* widget, boost::filesystem::path value)
213 {
214         if (widget->GetPath() != std_to_wx (value.string())) {
215                 if (value.empty()) {
216                         /* Hack to make wxWidgets clear the control when we are passed
217                            an empty value.
218                         */
219                         value = " ";
220                 }
221                 widget->SetPath (std_to_wx (value.string()));
222         }
223 }
224
225 void
226 checked_set (wxSpinCtrl* widget, int value)
227 {
228         if (widget->GetValue() != value) {
229                 widget->SetValue (value);
230         }
231 }
232
233 void
234 checked_set (wxSpinCtrlDouble* widget, double value)
235 {
236         /* XXX: completely arbitrary epsilon */
237         if (fabs (widget->GetValue() - value) > 1e-16) {
238                 widget->SetValue (value);
239         }
240 }
241
242 void
243 checked_set (wxChoice* widget, int value)
244 {
245         if (widget->GetSelection() != value) {
246                 widget->SetSelection (value);
247         }
248 }
249
250 void
251 checked_set (wxChoice* widget, string value)
252 {
253         wxClientData* o = 0;
254         if (widget->GetSelection() != -1) {
255                 o = widget->GetClientObject (widget->GetSelection ());
256         }
257
258         if (!o || string_client_data(o) != value) {
259                 for (unsigned int i = 0; i < widget->GetCount(); ++i) {
260                         if (string_client_data (widget->GetClientObject (i)) == value) {
261                                 widget->SetSelection (i);
262                         }
263                 }
264         }
265 }
266
267 void
268 checked_set (wxChoice* widget, vector<pair<string, string> > items)
269 {
270        vector<pair<string, string> > current;
271        for (unsigned int i = 0; i < widget->GetCount(); ++i) {
272                current.push_back (
273                        make_pair (
274                                wx_to_std (widget->GetString (i)),
275                                string_client_data (widget->GetClientObject (i))
276                                )
277                        );
278        }
279
280        if (current == items) {
281                return;
282        }
283
284        widget->Clear ();
285        for (vector<pair<string, string> >::const_iterator i = items.begin(); i != items.end(); ++i) {
286                widget->Append (std_to_wx (i->first), new wxStringClientData (std_to_wx (i->second)));
287        }
288 }
289
290 void
291 checked_set (wxTextCtrl* widget, string value)
292 {
293         if (widget->GetValue() != std_to_wx (value)) {
294                 widget->ChangeValue (std_to_wx (value));
295         }
296 }
297
298 void
299 checked_set (PasswordEntry* entry, string value)
300 {
301         if (entry->get() != value) {
302                 entry->set(value);
303         }
304 }
305
306 void
307 checked_set (wxTextCtrl* widget, wxString value)
308 {
309         if (widget->GetValue() != value) {
310                 widget->ChangeValue (value);
311         }
312 }
313
314 void
315 checked_set (wxStaticText* widget, string value)
316 {
317         if (widget->GetLabel() != std_to_wx (value)) {
318                 widget->SetLabel (std_to_wx (value));
319         }
320 }
321
322 void
323 checked_set (wxStaticText* widget, wxString value)
324 {
325         if (widget->GetLabel() != value) {
326                 widget->SetLabel (value);
327         }
328 }
329
330 void
331 checked_set (wxCheckBox* widget, bool value)
332 {
333         if (widget->GetValue() != value) {
334                 widget->SetValue (value);
335         }
336 }
337
338 void
339 checked_set (wxRadioButton* widget, bool value)
340 {
341         if (widget->GetValue() != value) {
342                 widget->SetValue (value);
343         }
344 }
345
346 void
347 dcpomatic_setup_i18n ()
348 {
349         int language = wxLANGUAGE_DEFAULT;
350
351         boost::optional<string> config_lang = Config::instance()->language ();
352         if (config_lang && !config_lang->empty ()) {
353                 wxLanguageInfo const * li = wxLocale::FindLanguageInfo (std_to_wx (config_lang.get ()));
354                 if (li) {
355                         language = li->Language;
356                 }
357         }
358
359         wxLocale* locale = 0;
360         if (wxLocale::IsAvailable (language)) {
361                 locale = new wxLocale (language, wxLOCALE_LOAD_DEFAULT);
362
363 #ifdef DCPOMATIC_WINDOWS
364                 locale->AddCatalogLookupPathPrefix (std_to_wx (mo_path().string()));
365 #endif
366
367 #ifdef DCPOMATIC_LINUX
368                 locale->AddCatalogLookupPathPrefix (LINUX_LOCALE_PREFIX);
369
370                 /* We have to include the wxWidgets .mo in our distribution,
371                    so we rename it to avoid clashes with any other installation
372                    of wxWidgets.
373                 */
374                 locale->AddCatalog (wxT ("dcpomatic2-wxstd"));
375
376                 /* Fedora 29 (at least) installs wxstd3.mo instead of wxstd.mo */
377                 locale->AddCatalog (wxT ("wxstd3"));
378 #endif
379
380                 locale->AddCatalog (wxT ("libdcpomatic2-wx"));
381                 locale->AddCatalog (wxT ("dcpomatic2"));
382
383                 if (!locale->IsOk()) {
384                         delete locale;
385                         locale = new wxLocale (wxLANGUAGE_ENGLISH);
386                 }
387         }
388
389         if (locale) {
390                 dcpomatic_setup_gettext_i18n (wx_to_std (locale->GetCanonicalName ()));
391         }
392 }
393
394 int
395 wx_get (wxSpinCtrl* w)
396 {
397         return w->GetValue ();
398 }
399
400 int
401 wx_get (wxChoice* w)
402 {
403         return w->GetSelection ();
404 }
405
406 double
407 wx_get (wxSpinCtrlDouble* w)
408 {
409         return w->GetValue ();
410 }
411
412 /** @param s String of the form Context|String
413  *  @return translation, or String if no translation is available.
414  */
415 wxString
416 context_translation (wxString s)
417 {
418         wxString t = wxGetTranslation (s);
419         if (t == s) {
420                 /* No translation; strip the context */
421                 int c = t.Find (wxT ("|"));
422                 if (c != wxNOT_FOUND) {
423                         t = t.Mid (c + 1);
424                 }
425         }
426
427         return t;
428 }
429
430 wxString
431 time_to_timecode (DCPTime t, double fps)
432 {
433         double w = t.seconds ();
434         int const h = (w / 3600);
435         w -= h * 3600;
436         int const m = (w / 60);
437         w -= m * 60;
438         int const s = floor (w);
439         w -= s;
440         int const f = lrint (w * fps);
441         return wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f);
442 }
443
444 void
445 setup_audio_channels_choice (wxChoice* choice, int minimum)
446 {
447         vector<pair<string, string> > items;
448         for (int i = minimum; i <= 16; i += 2) {
449                 if (i == 2) {
450                         items.push_back (make_pair (wx_to_std (_("2 - stereo")), locale_convert<string> (i)));
451                 } else if (i == 4) {
452                         items.push_back (make_pair (wx_to_std (_("4 - L/C/R/Lfe")), locale_convert<string> (i)));
453                 } else if (i == 6) {
454                         items.push_back (make_pair (wx_to_std (_("6 - 5.1")), locale_convert<string> (i)));
455                 } else if (i == 8) {
456                         items.push_back (make_pair (wx_to_std (_("8 - 5.1/HI/VI")), locale_convert<string> (i)));
457                 } else if (i == 12) {
458                         items.push_back (make_pair (wx_to_std (_("12 - 7.1/HI/VI")), locale_convert<string> (i)));
459                 } else {
460                         items.push_back (make_pair (locale_convert<string> (i), locale_convert<string> (i)));
461                 }
462         }
463
464         checked_set (choice, items);
465 }
466
467 wxSplashScreen *
468 maybe_show_splash ()
469 {
470         wxSplashScreen* splash = 0;
471         try {
472                 wxBitmap bitmap;
473                 boost::filesystem::path p = shared_path () / "splash.png";
474                 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
475                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
476                         wxYield ();
477                 }
478         } catch (boost::filesystem::filesystem_error& e) {
479                 /* Maybe we couldn't find the splash image; never mind */
480         }
481
482         return splash;
483 }
484
485 double
486 calculate_mark_interval (double mark_interval)
487 {
488         if (mark_interval > 5) {
489                 mark_interval -= lrint (mark_interval) % 5;
490         }
491         if (mark_interval > 10) {
492                 mark_interval -= lrint (mark_interval) % 10;
493         }
494         if (mark_interval > 60) {
495                 mark_interval -= lrint (mark_interval) % 60;
496         }
497         if (mark_interval > 3600) {
498                 mark_interval -= lrint (mark_interval) % 3600;
499         }
500
501         if (mark_interval < 1) {
502                 mark_interval = 1;
503         }
504
505         return mark_interval;
506 }
507
508
509 /** @return false if the task was cancelled */
510 bool
511 display_progress (wxString title, wxString task)
512 {
513         JobManager* jm = JobManager::instance ();
514
515         wxProgressDialog progress (title, task, 100, 0, wxPD_CAN_ABORT);
516
517         bool ok = true;
518
519         while (jm->work_to_do()) {
520                 dcpomatic_sleep_seconds (1);
521                 if (!progress.Pulse()) {
522                         /* user pressed cancel */
523                         BOOST_FOREACH (shared_ptr<Job> i, jm->get()) {
524                                 i->cancel();
525                         }
526                         ok = false;
527                         break;
528                 }
529         }
530
531         return ok;
532 }
533
534
535 int
536 get_offsets (vector<Offset>& offsets)
537 {
538         offsets.push_back (Offset(_("UTC-11"),  -11,  0));
539         offsets.push_back (Offset(_("UTC-10"),  -10,  0));
540         offsets.push_back (Offset(_("UTC-9"),    -9,  0));
541         offsets.push_back (Offset(_("UTC-8"),    -8,  0));
542         offsets.push_back (Offset(_("UTC-7"),    -7,  0));
543         offsets.push_back (Offset(_("UTC-6"),    -6,  0));
544         offsets.push_back (Offset(_("UTC-5"),    -5,  0));
545         offsets.push_back (Offset(_("UTC-4:30"), -4, 30));
546         offsets.push_back (Offset(_("UTC-4"),    -4,  0));
547         offsets.push_back (Offset(_("UTC-3:30"), -3, 30));
548         offsets.push_back (Offset(_("UTC-3"),    -3,  0));
549         offsets.push_back (Offset(_("UTC-2"),    -2,  0));
550         offsets.push_back (Offset(_("UTC-1"),    -1,  0));
551         int utc = offsets.size();
552         offsets.push_back (Offset(_("UTC")  ,     0,  0));
553         offsets.push_back (Offset(_("UTC+1"),     1,  0));
554         offsets.push_back (Offset(_("UTC+2"),     2,  0));
555         offsets.push_back (Offset(_("UTC+3"),     3,  0));
556         offsets.push_back (Offset(_("UTC+4"),     4,  0));
557         offsets.push_back (Offset(_("UTC+5"),     5,  0));
558         offsets.push_back (Offset(_("UTC+5:30"),  5, 30));
559         offsets.push_back (Offset(_("UTC+6"),     6,  0));
560         offsets.push_back (Offset(_("UTC+7"),     7,  0));
561         offsets.push_back (Offset(_("UTC+8"),     8,  0));
562         offsets.push_back (Offset(_("UTC+9"),     9,  0));
563         offsets.push_back (Offset(_("UTC+9:30"),  9, 30));
564         offsets.push_back (Offset(_("UTC+10"),   10,  0));
565         offsets.push_back (Offset(_("UTC+11"),   11,  0));
566         offsets.push_back (Offset(_("UTC+12"),   12,  0));
567
568         return utc;
569 }
570
571
572 wxString
573 bitmap_path (string name)
574 {
575         boost::filesystem::path base;
576
577 #ifdef DCPOMATIC_DEBUG
578         /* Hack to allow OS X to find icons when running from the source tree */
579         char* path = getenv ("DCPOMATIC_GRAPHICS");
580         if (path) {
581                 base = path;
582         } else {
583                 base = shared_path();
584         }
585 #else
586         base = shared_path();
587 #endif
588
589         boost::filesystem::path p = base / String::compose("%1.png", name);
590         return std_to_wx (p.string());
591 }
592