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