Add wxTextEntry::ForceUpper()

Allow automatically converting lower-case letters entered into wxTextCtrl to
upper-case equivalents. Provide generic fallback and implement the method
natively for all the major platforms.

Also update the text sample to show it in action.
This commit is contained in:
Vadim Zeitlin
2015-11-25 03:40:40 +01:00
parent 99d9a81e28
commit 69b66e9e2e
14 changed files with 238 additions and 18 deletions

View File

@@ -310,6 +310,66 @@ bool wxTextEntryBase::CanPaste() const
return false;
}
// ----------------------------------------------------------------------------
// input restrictions
// ----------------------------------------------------------------------------
#ifndef wxHAS_NATIVE_TEXT_FORCEUPPER
namespace
{
// Poor man's lambda: helper for binding ConvertToUpperCase() to the event
struct ForceUpperFunctor
{
explicit ForceUpperFunctor(wxTextEntryBase* entry)
: m_entry(entry)
{
}
void operator()(wxCommandEvent& event)
{
event.Skip();
m_entry->ConvertToUpperCase();
}
wxTextEntryBase* const m_entry;
};
} // anonymous namespace
#endif // !wxHAS_NATIVE_TEXT_FORCEUPPER
void wxTextEntryBase::ConvertToUpperCase()
{
const wxString& valueOld = GetValue();
const wxString& valueNew = valueOld.Upper();
if ( valueNew != valueOld )
{
long from, to;
GetSelection(&from, &to);
ChangeValue(valueNew);
SetSelection(from, to);
}
}
void wxTextEntryBase::ForceUpper()
{
// Do nothing if this method is never called because a native override is
// provided: this is just a tiny size-saving optimization, nothing else.
#ifndef wxHAS_NATIVE_TEXT_FORCEUPPER
wxWindow* const win = GetEditableWindow();
wxCHECK_RET( win, wxS("can't be called before creating the window") );
// Convert the current control contents to upper case
ConvertToUpperCase();
// And ensure that any text entered in the future is converted too
win->Bind(wxEVT_TEXT, ForceUpperFunctor(this));
#endif // !wxHAS_NATIVE_TEXT_FORCEUPPER
}
// ----------------------------------------------------------------------------
// hints support
// ----------------------------------------------------------------------------