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