added wxTextCtrl::EmulateKeyPress

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15005 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-04-07 22:29:04 +00:00
parent 15bee36fdf
commit 94af7d45ef
7 changed files with 121 additions and 67 deletions

View File

@@ -143,6 +143,7 @@ All (GUI):
- added wxImage::FloodFill and implemented wxWindowDC::DoFloodFill method
for GTK+, Mac, MGL, X11, Motif ports (Chris Elliott)
- added (platform-dependent) scan code to wxKeyEvent (Bryce Denney)
- added wxTextCtrl::EmulateKeyPress()
wxMSW:

View File

@@ -395,6 +395,20 @@ Copies the selected text to the clipboard and removes the selection.
Resets the internal `modified' flag as if the current edits had been saved.
\membersection{wxTextCtrl::EmulateKeyPress}
\func{bool}{EmulateKeyPress}{\param{const wxKeyEvent\& }{event}}
This functions inserts into the control the character which would have been
inserted if the given key event had occured in the text control. The
{\it event} object should be the same as the one passed to {\tt EVT\_KEY\_DOWN}
handler previously by wxWindows.
\wxheading{Return value}
{\tt TRUE} if the event resulted in a change to the control, {\tt FALSE}
otherwise.
\membersection{wxTextCtrl::GetDefaultStyle}\label{wxtextctrlgetdefaultstyle}
\constfunc{const wxTextAttr\& }{GetDefaultStyle}{\void}

View File

@@ -82,6 +82,10 @@ public:
virtual void WriteText(const wxString& text);
virtual void AppendText(const wxString& text);
#ifdef __WIN32__
virtual bool EmulateKeyPress(const wxKeyEvent& event);
#endif // __WIN32__
#if wxUSE_RICHEDIT
// apply text attribute to the range of text (only works with richedit
// controls)

View File

@@ -203,6 +203,10 @@ public:
virtual void WriteText(const wxString& text) = 0;
virtual void AppendText(const wxString& text) = 0;
// insert the character which would have resulted from this key event,
// return TRUE if anything has been inserted
virtual bool EmulateKeyPress(const wxKeyEvent& event);
// text control under some platforms supports the text styles: these
// methods allow to apply the given text style to the given selection or to
// set/get the style which will be used for all appended text

View File

@@ -277,6 +277,84 @@ bool wxTextCtrlBase::CanPaste() const
return IsEditable();
}
// ----------------------------------------------------------------------------
// emulating key presses
// ----------------------------------------------------------------------------
bool wxTextCtrlBase::EmulateKeyPress(const wxKeyEvent& event)
{
// the generic version is unused in wxMSW
#ifndef __WIN32__
wxChar ch;
int keycode = event.GetKeyCode();
switch ( keycode )
{
case WXK_NUMPAD0:
case WXK_NUMPAD1:
case WXK_NUMPAD2:
case WXK_NUMPAD3:
case WXK_NUMPAD4:
case WXK_NUMPAD5:
case WXK_NUMPAD6:
case WXK_NUMPAD7:
case WXK_NUMPAD8:
case WXK_NUMPAD9:
ch = _T('0') + keycode - WXK_NUMPAD0;
break;
case WXK_MULTIPLY:
case WXK_NUMPAD_MULTIPLY:
ch = _T('*');
break;
case WXK_ADD:
case WXK_NUMPAD_ADD:
ch = _T('+');
break;
case WXK_SUBTRACT:
case WXK_NUMPAD_SUBTRACT:
ch = _T('-');
break;
case WXK_DECIMAL:
case WXK_NUMPAD_DECIMAL:
ch = _T('.');
break;
case WXK_DIVIDE:
case WXK_NUMPAD_DIVIDE:
ch = _T('/');
break;
default:
if ( keycode < 256 && keycode >= 0 && isprint(keycode) )
{
// FIXME this is not going to work for non letters...
if ( !event.ShiftDown() )
{
keycode = tolower(keycode);
}
ch = (wxChar)keycode;
}
else
{
ch = _T('\0');
}
}
if ( ch )
{
WriteText(ch);
return TRUE;
}
#endif // !__WIN32__
return FALSE;
}
// ----------------------------------------------------------------------------
// selection and ranges
// ----------------------------------------------------------------------------

View File

@@ -708,73 +708,7 @@ bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event)
void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
{
// we don't check for !HasModifiers() because IsAcceptedKey() did it
// insert the key in the control
wxChar ch;
int keycode = event.GetKeyCode();
switch ( keycode )
{
case WXK_NUMPAD0:
case WXK_NUMPAD1:
case WXK_NUMPAD2:
case WXK_NUMPAD3:
case WXK_NUMPAD4:
case WXK_NUMPAD5:
case WXK_NUMPAD6:
case WXK_NUMPAD7:
case WXK_NUMPAD8:
case WXK_NUMPAD9:
ch = _T('0') + keycode - WXK_NUMPAD0;
break;
case WXK_MULTIPLY:
case WXK_NUMPAD_MULTIPLY:
ch = _T('*');
break;
case WXK_ADD:
case WXK_NUMPAD_ADD:
ch = _T('+');
break;
case WXK_SUBTRACT:
case WXK_NUMPAD_SUBTRACT:
ch = _T('-');
break;
case WXK_DECIMAL:
case WXK_NUMPAD_DECIMAL:
ch = _T('.');
break;
case WXK_DIVIDE:
case WXK_NUMPAD_DIVIDE:
ch = _T('/');
break;
default:
if ( keycode < 256 && keycode >= 0 && isprint(keycode) )
{
// FIXME this is not going to work for non letters...
if ( !event.ShiftDown() )
{
keycode = tolower(keycode);
}
ch = (wxChar)keycode;
}
else
{
ch = _T('\0');
}
}
if ( ch )
{
Text()->AppendText(ch);
}
else
if ( !Text()->EmulateKeyPress(event) )
{
event.Skip();
}

View File

@@ -692,6 +692,25 @@ void wxTextCtrl::Clear()
::SetWindowText(GetHwnd(), wxT(""));
}
#ifdef __WIN32__
bool wxTextCtrl::EmulateKeyPress(const wxKeyEvent& event)
{
SetFocus();
size_t lenOld = GetValue().length();
wxUint32 code = event.GetRawKeyCode();
::keybd_event(code, 0, 0 /* key press */, NULL);
::keybd_event(code, 0, KEYEVENTF_KEYUP, NULL);
// assume that any alphanumeric key changes the total number of characters
// in the control - this should work in 99% of cases
return GetValue().length() != lenOld;
}
#endif // __WIN32__
// ----------------------------------------------------------------------------
// Clipboard operations
// ----------------------------------------------------------------------------