]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxTextCtrl::EmulateKeyPress
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 7 Apr 2002 22:29:04 +0000 (22:29 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 7 Apr 2002 22:29:04 +0000 (22:29 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15005 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/text.tex
include/wx/msw/textctrl.h
include/wx/textctrl.h
src/common/textcmn.cpp
src/generic/grid.cpp
src/msw/textctrl.cpp

index 225e93ccebd62f22934fab062374c2dd46663204..b01523546d0b1712bd9394c4a055c14972bb05cf 100644 (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:
 
index cc05b73006d6474d2eb62abd1eb24b28acaf9fa2..b91a240ec3374e47d5fbf1e0282626c39377a89f 100644 (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}
index 66114fa0c77d286fed2ef14b9d9e7e31b818f17a..c5e03295f3b52f537e4ab90dbfa8df53babd7f16 100644 (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)
index 0905218aaecde11c6881bb1a5429097069da8504..141cbbe8470368ecc521917b3ef9882d0988de35 100644 (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
index 11c063dfdd95f8ccbc4a0ce2b85e769ec1d9d66d..bf4de031f140d7e9302f0c262e06453c4d8ef941 100644 (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
 // ----------------------------------------------------------------------------
index b6afb0c214aab8b69b1c46d0e6910a1694913d85..101e6cf372d6589a314e8231ba9c0eb3e98de8c8 100644 (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();
     }
index 43ca7b27ef2755eede5f3e286b6e707dd3d33d46..cee29ac19eceb291fcea5fe7516f5ba6970a4640 100644 (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
 // ----------------------------------------------------------------------------