Use ScopeGuard more.
[dcpomatic.git] / src / wx / wx_util.cc
index e94f3e512acf03322be4f09032a09d0147cceb1b..1f2afb4f548693ac9be99ae399b01cb881dbfe43 100644 (file)
 
 
 #include "file_picker_ctrl.h"
+#include "language_tag_widget.h"
 #include "password_entry.h"
+#include "region_subtag_widget.h"
 #include "static_text.h"
 #include "wx_util.h"
 #include "lib/config.h"
 #include "lib/cross.h"
 #include "lib/job.h"
 #include "lib/job_manager.h"
+#include "lib/scope_guard.h"
 #include "lib/util.h"
 #include "lib/version.h"
 #include <dcp/locale_convert.h>
@@ -159,13 +162,13 @@ void
 error_dialog (wxWindow* parent, wxString m, optional<wxString> e)
 {
        auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_ERROR);
+       ScopeGuard sg = [d]() { d->Destroy(); };
        if (e) {
                wxString em = *e;
                em[0] = wxToupper (em[0]);
                d->SetExtendedMessage (em);
        }
        d->ShowModal ();
-       d->Destroy ();
 }
 
 
@@ -177,8 +180,8 @@ void
 message_dialog (wxWindow* parent, wxString m)
 {
        auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxOK | wxICON_INFORMATION);
+       ScopeGuard sg = [d]() { d->Destroy(); };
        d->ShowModal ();
-       d->Destroy ();
 }
 
 
@@ -187,9 +190,8 @@ bool
 confirm_dialog (wxWindow* parent, wxString m)
 {
        auto d = new wxMessageDialog (parent, m, _("DCP-o-matic"), wxYES_NO | wxICON_QUESTION);
-       int const r = d->ShowModal ();
-       d->Destroy ();
-       return r == wxID_YES;
+       ScopeGuard sg = [d]() { d->Destroy(); };
+       return d->ShowModal() == wxID_YES;
 }
 
 
@@ -304,7 +306,7 @@ checked_set (wxChoice* widget, vector<pair<string, string>> items)
                current.push_back (
                        make_pair(
                                wx_to_std(widget->GetString(i)),
-                               string_client_data(widget->GetClientObject(i))
+                               widget->GetClientData() ? string_client_data(widget->GetClientObject(i)) : ""
                                )
                        );
        }
@@ -383,6 +385,33 @@ checked_set (wxRadioButton* widget, bool value)
 }
 
 
+void
+checked_set(LanguageTagWidget* widget, dcp::LanguageTag value)
+{
+       if (widget->get() != value) {
+               widget->set(value);
+       }
+}
+
+
+void
+checked_set(LanguageTagWidget* widget, optional<dcp::LanguageTag> value)
+{
+       if (widget->get() != value) {
+               widget->set(value);
+       }
+}
+
+
+void
+checked_set(RegionSubtagWidget* widget, optional<dcp::LanguageTag::RegionSubtag> value)
+{
+       if (widget->get() != value) {
+               widget->set(value);
+       }
+}
+
+
 void
 dcpomatic_setup_i18n ()
 {
@@ -651,6 +680,13 @@ bitmap_path (string name)
 }
 
 
+wxString
+icon_path(string name)
+{
+       return gui_is_dark() ? bitmap_path(String::compose("%1_white.png", name)) : bitmap_path(String::compose("%1_black.png", name));
+}
+
+
 wxSize
 small_button_size (wxWindow* parent, wxString text)
 {
@@ -700,3 +736,28 @@ search_ctrl_height ()
 #endif
 }
 
+
+void
+report_config_load_failure(wxWindow* parent, Config::LoadFailure what)
+{
+       switch (what) {
+       case Config::LoadFailure::CONFIG:
+               message_dialog(parent, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
+               break;
+       case Config::LoadFailure::CINEMAS:
+               message_dialog(
+                       parent,
+                       _(wxString::Format("The cinemas list for creating KDMs (cinemas.xml) failed to load.  Please check the numbered backup files in %s",
+                                          std_to_wx(Config::instance()->cinemas_file().parent_path().string())))
+                       );
+               break;
+       case Config::LoadFailure::DKDM_RECIPIENTS:
+               message_dialog(
+                       parent,
+                       _(wxString::Format("The recipients list for creating DKDMs (dkdm_recipients.xml) failed to load.  Please check the numbered backup files in %s",
+                                          std_to_wx(Config::instance()->dkdm_recipients_file().parent_path().string())))
+                       );
+               break;
+       }
+}
+