From 7a34307e2463bca44f128e28b70e1df4f626e8f8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 23 Aug 2009 00:32:17 +0000 Subject: [PATCH] Added wxKeyEvent::IsKeyInCategory() method. This allows to test whether a given key belongs to the category of e.g. arrow keys or navigation keys in a more concise and more readable manner. Closes #10268. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61736 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/event.h | 30 ++++++++++++++++++++++++++ interface/wx/event.h | 40 +++++++++++++++++++++++++++++++++++ src/common/event.cpp | 39 ++++++++++++++++++++++++++++++++++ src/osx/textctrl_osx.cpp | 6 +++--- src/richtext/richtextctrl.cpp | 18 +--------------- 6 files changed, 114 insertions(+), 20 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 494d413e22..35b0d00c50 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -394,6 +394,7 @@ All (GUI): - Added long version field to wxAboutDialogInfo (Jeff Tupper). - Added wxWindow::CanScroll() behaving like the old HasScrollbar() and made HasScrollbar() really check for the scrollbar existence. +- Added wxKeyEvent::IsKeyInCategory() (Jeff Tupper). GTK: diff --git a/include/wx/event.h b/include/wx/event.h index 21936ccaa1..06cd01f28b 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -1504,6 +1504,33 @@ private: wxEVT_HOTKEY */ +// key categories: the bit flags for IsKeyInCategory() function +// +// the enum values used may change in future version of wx +// use the named constants only, or bitwise combinations thereof +enum wxKeyCategoryFlags +{ + // arrow keys, on and off numeric keypads + WXK_CATEGORY_ARROW = 1, + + // page up and page down keys, on and off numeric keypads + WXK_CATEGORY_PAGING = 2, + + // home and end keys, on and off numeric keypads + WXK_CATEGORY_JUMP = 4, + + // tab key + WXK_CATEGORY_TAB = 8, + + // backspace and delete keys, on and off numeric keypads + WXK_CATEGORY_CUT = 16, + + // all keys usually used for navigation + WXK_CATEGORY_NAVIGATION = WXK_CATEGORY_ARROW | + WXK_CATEGORY_PAGING | + WXK_CATEGORY_JUMP +}; + class WXDLLIMPEXP_CORE wxKeyEvent : public wxEvent, public wxKeyboardState { @@ -1514,6 +1541,9 @@ public: // get the key code: an ASCII7 char or an element of wxKeyCode enum int GetKeyCode() const { return (int)m_keyCode; } + // returns true iff this event's key code is of a certain type + bool IsKeyInCategory(int category) const; + #if wxUSE_UNICODE // get the Unicode character corresponding to this key wxChar GetUnicodeKey() const { return m_uniChar; } diff --git a/interface/wx/event.h b/interface/wx/event.h index 3b81128945..b934e0e9b0 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -1083,6 +1083,36 @@ protected: }; +/** + Flags for categories of keys. + + These values are used by wxKeyEvent::IsKeyInCategory(). They may be + combined via the bitwise operators |, &, and ~. + + @since 2.9.1 +*/ +enum wxKeyCategoryFlags +{ + /// arrow keys, on and off numeric keypads + WXK_CATEGORY_ARROW, + + /// page up and page down keys, on and off numeric keypads + WXK_CATEGORY_PAGING, + + /// home and end keys, on and off numeric keypads + WXK_CATEGORY_JUMP, + + /// tab key + WXK_CATEGORY_TAB, + + /// backspace and delete keys, on and off numeric keypads + WXK_CATEGORY_CUT, + + /// union of WXK_CATEGORY_ARROW, WXK_CATEGORY_PAGING, and WXK_CATEGORY_JUMP categories + WXK_CATEGORY_NAVIGATION +}; + + /** @class wxKeyEvent @@ -1177,6 +1207,16 @@ public: */ int GetKeyCode() const; + /** + Returns true if the key is in the given key category. + + @param category + A bitwise combination of named ::wxKeyCategoryFlags constants. + + @since 2.9.1 + */ + bool IsKeyInCategory(int category) const; + //@{ /** Obtains the position (in client coordinates) at which the key was pressed. diff --git a/src/common/event.cpp b/src/common/event.cpp index 34ec269af1..658f7523af 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -752,6 +752,45 @@ wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt) #endif } +bool wxKeyEvent::IsKeyInCategory(int category) const +{ + switch ( GetKeyCode() ) + { + case WXK_LEFT: + case WXK_RIGHT: + case WXK_UP: + case WXK_DOWN: + case WXK_NUMPAD_LEFT: + case WXK_NUMPAD_RIGHT: + case WXK_NUMPAD_UP: + case WXK_NUMPAD_DOWN: + return (category & WXK_CATEGORY_ARROW) != 0; + + case WXK_PAGEDOWN: + case WXK_END: + case WXK_NUMPAD_PAGEUP: + case WXK_NUMPAD_PAGEDOWN: + return (category & WXK_CATEGORY_PAGING) != 0; + + case WXK_HOME: + case WXK_PAGEUP: + case WXK_NUMPAD_HOME: + case WXK_NUMPAD_END: + return (category & WXK_CATEGORY_JUMP) != 0; + + case WXK_TAB: + return (category & WXK_CATEGORY_TAB) != 0; + + case WXK_BACK: + case WXK_DELETE: + case WXK_NUMPAD_DELETE: + return (category & WXK_CATEGORY_CUT) != 0; + + default: + return false; + } +} + // ---------------------------------------------------------------------------- // wxWindowCreateEvent // ---------------------------------------------------------------------------- diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index 29546c39cb..8fd25ac960 100644 --- a/src/osx/textctrl_osx.cpp +++ b/src/osx/textctrl_osx.cpp @@ -473,7 +473,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) return ; } - if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB && + if ( !IsEditable() && !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB) && !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) ) // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END ) @@ -486,8 +486,8 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) // allow navigation and deletion GetSelection( &from, &to ); if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength && - key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB && key != WXK_UP && key != WXK_DOWN && - key != WXK_BACK && key != WXK_DELETE && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) && + !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB | WXK_CATEGORY_CUT) && + !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) && from == to ) { // eat it, we don't want to add more than allowed # of characters diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp index ed5f8a08a7..c46eafc11a 100644 --- a/src/richtext/richtextctrl.cpp +++ b/src/richtext/richtextctrl.cpp @@ -692,23 +692,7 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) if (event.GetEventType() == wxEVT_KEY_DOWN) { - if (event.GetKeyCode() == WXK_LEFT || - event.GetKeyCode() == WXK_RIGHT || - event.GetKeyCode() == WXK_UP || - event.GetKeyCode() == WXK_DOWN || - event.GetKeyCode() == WXK_HOME || - event.GetKeyCode() == WXK_PAGEUP || - event.GetKeyCode() == WXK_PAGEDOWN || - event.GetKeyCode() == WXK_END || - - event.GetKeyCode() == WXK_NUMPAD_LEFT || - event.GetKeyCode() == WXK_NUMPAD_RIGHT || - event.GetKeyCode() == WXK_NUMPAD_UP || - event.GetKeyCode() == WXK_NUMPAD_DOWN || - event.GetKeyCode() == WXK_NUMPAD_HOME || - event.GetKeyCode() == WXK_NUMPAD_PAGEUP || - event.GetKeyCode() == WXK_NUMPAD_PAGEDOWN || - event.GetKeyCode() == WXK_NUMPAD_END) + if (event.IsKeyInCategory(WXK_CATEGORY_NAVIGATION)) { KeyboardNavigate(event.GetKeyCode(), flags); return; -- 2.45.2