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