From 291c0c2915a10102333c9d114f5f4f3fe3a2b341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=CC=81clav=20Slavi=CC=81k?= Date: Sat, 16 Sep 2023 19:01:28 +0200 Subject: [PATCH] Properties: reset plural form expr on language change Previously, changing the language without adjusting plural forms would result in incorrect plural form expression being used. Fix this by handling the common case better: if the user changes catalog language, reset plural forms to use the default expression. This is almost always what the user wants when manipulating the language. --- src/propertiesdlg.cpp | 41 ++++++++++++++++------------------------- src/propertiesdlg.h | 5 ++--- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/propertiesdlg.cpp b/src/propertiesdlg.cpp index 3b2b00308b..7d733ea1f2 100644 --- a/src/propertiesdlg.cpp +++ b/src/propertiesdlg.cpp @@ -756,7 +756,6 @@ PropertiesDialog::PropertiesDialog(wxWindow *parent, CatalogPtr cat, bool fileEx m_language->Bind(wxEVT_COMBOBOX, &PropertiesDialog::OnLanguageChanged, this); m_pluralFormsDefault->Bind(wxEVT_RADIOBUTTON, &PropertiesDialog::OnPluralFormsDefault, this); - m_pluralFormsCustom->Bind(wxEVT_RADIOBUTTON, &PropertiesDialog::OnPluralFormsCustom, this); m_pluralFormsExpr->Bind( wxEVT_UPDATE_UI, [=](wxUpdateUIEvent& e){ e.Enable(m_pluralFormsCustom->GetValue()); }); @@ -875,7 +874,7 @@ void PropertiesDialog::TransferTo(const CatalogPtr& cat) if (m_hasLang) { m_language->SetLang(cat->Header().Lang); - OnLanguageValueChanged(m_language->GetValue()); + OnLanguageValueChanged(); PluralFormsExpr pf_def(cat->Header().Lang.DefaultPluralFormsExpr()); PluralFormsExpr pf_cat(cat->Header().GetHeader("Plural-Forms").ToStdString()); @@ -969,34 +968,34 @@ void PropertiesDialog::DisableSourcesControls() void PropertiesDialog::OnLanguageChanged(wxCommandEvent& event) { m_validatedLang = -1; - OnLanguageValueChanged(event.GetString()); + OnLanguageValueChanged(); event.Skip(); } -void PropertiesDialog::OnLanguageValueChanged(const wxString& langstr) +void PropertiesDialog::OnLanguageValueChanged() { - Language lang = Language::TryParse(langstr.ToStdWstring()); - auto pluralForm = lang.DefaultPluralFormsExpr(); - if (!pluralForm) + Language lang = m_language->GetLang(); + if (lang == m_currentLanguageValue) + return; + m_currentLanguageValue = lang; + + auto pluralExpr = lang.DefaultPluralFormsExpr(); + auto validPlurals = lang.IsValid() && pluralExpr; + m_pluralFormsDefault->Enable(validPlurals); + if (validPlurals) { - m_pluralFormsDefault->Disable(); - m_pluralFormsCustom->SetValue(true); + m_pluralFormsDefault->SetValue(true); + m_pluralFormsExpr->SetValue(bidi::mark_direction(pluralExpr.str(), TextDirection::LTR)); } else { - m_pluralFormsDefault->Enable(); - if (m_pluralFormsExpr->GetValue().empty() || - PluralFormsExpr(m_pluralFormsExpr->GetValue().ToStdString()) == pluralForm) - { - m_pluralFormsDefault->SetValue(true); - } + m_pluralFormsCustom->SetValue(true); + m_pluralFormsExpr->Clear(); } } void PropertiesDialog::OnPluralFormsDefault(wxCommandEvent& event) { - m_rememberedPluralForm = m_pluralFormsExpr->GetValue(); - Language lang = m_language->GetLang(); if (lang.IsValid()) { @@ -1008,14 +1007,6 @@ void PropertiesDialog::OnPluralFormsDefault(wxCommandEvent& event) event.Skip(); } -void PropertiesDialog::OnPluralFormsCustom(wxCommandEvent& event) -{ - if (!m_rememberedPluralForm.empty()) - m_pluralFormsExpr->SetValue(m_rememberedPluralForm); - - event.Skip(); -} - void PropertiesDialog::OnGettextSettings(wxCommandEvent&) { wxWindowPtr dlg(new GettextSettingsDialog(this)); diff --git a/src/propertiesdlg.h b/src/propertiesdlg.h index b0839a969d..d1b9471883 100644 --- a/src/propertiesdlg.h +++ b/src/propertiesdlg.h @@ -59,9 +59,8 @@ class PropertiesDialog : public wxDialog void DisableSourcesControls(); void OnLanguageChanged(wxCommandEvent& event); - void OnLanguageValueChanged(const wxString& langstr); + void OnLanguageValueChanged(); void OnPluralFormsDefault(wxCommandEvent& event); - void OnPluralFormsCustom(wxCommandEvent& event); void OnGettextSettings(wxCommandEvent& event); struct PathsData; @@ -83,7 +82,7 @@ class PropertiesDialog : public wxDialog PathsList *m_paths, *m_excludedPaths; wxEditableListBox *m_keywords; wxCheckBox *m_defaultKeywords; - wxString m_rememberedPluralForm; + Language m_currentLanguageValue; std::shared_ptr m_gettextSettings; bool m_hasLang; int m_validatedPlural, m_validatedLang;