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
This commit is contained in:
Vadim Zeitlin 2012-04-16 12:12:19 +00:00
parent 6f3320bf3d
commit 86f5e64bb1
4 changed files with 45 additions and 10 deletions

View File

@ -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:

View File

@ -118,7 +118,12 @@ public:
// returns a wxString for the this accelerator.
// this function formats it using the <flags>-<keycode> format
// where <flags> 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);

View File

@ -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.

View File

@ -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;
}
}