From 86f5e64bb18f748bd5d574fd86bce84b119bef86 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 16 Apr 2012 12:12:19 +0000 Subject: [PATCH] Added wxAcceleratorEntry::ToRawString(). This function can be used to obtain language/locale-independent representation of an accelerator. This is particularly useful for storing it in configuration files. Closes #14228. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71203 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/accel.h | 9 ++++++++- interface/wx/accel.h | 14 ++++++++++++++ src/common/accelcmn.cpp | 31 ++++++++++++++++++++++--------- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 4060433d9f..ddfc91898c 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -503,6 +503,7 @@ All (GUI): - Enable/disable "Window" menu items in AUI MDI correctly (wsu). - Added wxTimePickerCtrl::Get/SetTime(). - Fix WXK_MENU handling in wxStyledTextCtrl under wxGTK (cantabile). +- Added wxAcceleratorEntry::ToRawString() (Armel Asselin). GTK: diff --git a/include/wx/accel.h b/include/wx/accel.h index ca2d8dfb0e..562b257d92 100644 --- a/include/wx/accel.h +++ b/include/wx/accel.h @@ -118,7 +118,12 @@ public: // returns a wxString for the this accelerator. // this function formats it using the - format // where maybe a hyphen-separated list of "shift|alt|ctrl" - wxString ToString() const; + wxString ToString() const { return AsPossiblyLocalizedString(true); } + + // same as above but without translating, useful if the string is meant to + // be stored in a file or otherwise stored, instead of being shown to the + // user + wxString ToRawString() const { return AsPossiblyLocalizedString(false); } // returns true if the given string correctly initialized this object // (i.e. if IsOk() returns true after this call) @@ -126,6 +131,8 @@ public: private: + wxString AsPossiblyLocalizedString(bool localized) const; + // common part of Create() and FromString() static bool ParseAccel(const wxString& str, int *flags, int *keycode); diff --git a/interface/wx/accel.h b/interface/wx/accel.h index 580d30033d..bdc72a32ea 100644 --- a/interface/wx/accel.h +++ b/interface/wx/accel.h @@ -114,6 +114,20 @@ public: */ wxString ToString() const; + /** + Returns a textual representation of this accelerator which is + appropriate for saving in configuration files. + + Unlike the string returned by ToString(), this one is never translated + so, while it's not suitable for showing to the user, it can be used to + uniquely identify the accelerator independently of the user language. + + The returned string can still be parsed by FromString(). + + @since 2.9.4 + */ + wxString ToRawString() const; + /** Parses the given string and sets the accelerator accordingly. diff --git a/src/common/accelcmn.cpp b/src/common/accelcmn.cpp index 1ed6c5947d..2b3165ec37 100644 --- a/src/common/accelcmn.cpp +++ b/src/common/accelcmn.cpp @@ -302,30 +302,43 @@ bool wxAcceleratorEntry::FromString(const wxString& str) return ParseAccel(str, &m_flags, &m_keyCode); } -wxString wxAcceleratorEntry::ToString() const +namespace +{ + +wxString PossiblyLocalize(const wxString& str, bool localize) +{ + return localize ? wxGetTranslation(str) : str; +} + +} + +wxString wxAcceleratorEntry::AsPossiblyLocalizedString(bool localized) const { wxString text; int flags = GetFlags(); if ( flags & wxACCEL_ALT ) - text += _("Alt+"); + text += PossiblyLocalize(wxTRANSLATE("Alt+"), localized); if ( flags & wxACCEL_CTRL ) - text += _("Ctrl+"); + text += PossiblyLocalize(wxTRANSLATE("Ctrl+"), localized); if ( flags & wxACCEL_SHIFT ) - text += _("Shift+"); + text += PossiblyLocalize(wxTRANSLATE("Shift+"), localized); #if defined(__WXMAC__) || defined(__WXCOCOA__) if ( flags & wxACCEL_RAW_CTRL ) - text += _("RawCtrl+"); + text += PossiblyLocalize(wxTRANSLATE("RawCtrl+"), localized); #endif const int code = GetKeyCode(); if ( code >= WXK_F1 && code <= WXK_F12 ) - text << _("F") << code - WXK_F1 + 1; + text << PossiblyLocalize(wxTRANSLATE("F"), localized) + << code - WXK_F1 + 1; else if ( code >= WXK_NUMPAD0 && code <= WXK_NUMPAD9 ) - text << _("KP_") << code - WXK_NUMPAD0; + text << PossiblyLocalize(wxTRANSLATE("KP_"), localized) + << code - WXK_NUMPAD0; else if ( code >= WXK_SPECIAL1 && code <= WXK_SPECIAL20 ) - text << _("SPECIAL") << code - WXK_SPECIAL1 + 1; + text << PossiblyLocalize(wxTRANSLATE("SPECIAL"), localized) + << code - WXK_SPECIAL1 + 1; else // check the named keys { size_t n; @@ -334,7 +347,7 @@ wxString wxAcceleratorEntry::ToString() const const wxKeyName& kn = wxKeyNames[n]; if ( code == kn.code ) { - text << wxGetTranslation(kn.name); + text << PossiblyLocalize(kn.name, localized); break; } } -- 2.45.2