From 8d2c70414cc0c6b93147d0c0cc963f217fcb2023 Mon Sep 17 00:00:00 2001 From: Jaakko Salli Date: Sat, 12 Jun 2010 09:30:57 +0000 Subject: [PATCH] Added wxPropertyGrid::DedicateKey(), which prevents specific key presses from being eaten by editor controls. This is useful for customizing keyboard navigation. Also added utility function wxPGFindInVector<>(), which is used in the new code, and also in some other places. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64562 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/propgrid/propgrid.h | 26 ++++++++++++++++++++++++++ include/wx/propgrid/propgriddefs.h | 13 +++++++++++++ interface/wx/propgrid/propgrid.h | 19 +++++++++++++++++++ samples/propgrid/propgrid.cpp | 28 +++++++++++++++++++++++++++- samples/propgrid/propgrid.h | 2 ++ src/propgrid/property.cpp | 7 +------ src/propgrid/propgrid.cpp | 15 ++++++++++++--- src/propgrid/propgridpagestate.cpp | 9 ++------- 9 files changed, 103 insertions(+), 17 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index ae2a06e6ee..fee187ad4d 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -494,6 +494,7 @@ All (GUI): dismissed immediately as text control grabbed the focus). - wxPropertyGrid: added wxPG_EX_MULTIPLE_SELECTION. - wxPropertyGrid: added functions for editing property labels. +- wxPropertyGrid: Added wxPropertyGrid::DedicateKey(). - wxPropertyGridManager: added wxPG_NO_INTERNAL_BORDER, wxPG_EX_NO_TOOLBAR_DIVIDER and wxPG_EX_TOOLBAR_SEPARATOR styles for finer control over borders. Borders around property grid are now native for diff --git a/include/wx/propgrid/propgrid.h b/include/wx/propgrid/propgrid.h index 9d783e2be0..973b9e2113 100644 --- a/include/wx/propgrid/propgrid.h +++ b/include/wx/propgrid/propgrid.h @@ -709,12 +709,34 @@ public: /** Adds given key combination to trigger given action. + Here is a sample code to make Enter key press move focus to + the next property. + + @code + propGrid->AddActionTrigger(wxPG_ACTION_NEXT_PROPERTY, + WXK_RETURN); + propGrid->DedicateKey(WXK_RETURN); + @endcode + @param action Which action to trigger. See @link pgactions List of list of wxPropertyGrid actions@endlink. */ void AddActionTrigger( int action, int keycode, int modifiers = 0 ); + /** + Dedicates a specific keycode to wxPropertyGrid. This means that such + key presses will not be redirected to editor controls. + + Using this function allows, for example, navigation between + properties using arrow keys even when the focus is in the editor + control. + */ + void DedicateKey( int keycode ) + { + m_dedicatedKeys.push_back(keycode); + } + /** This static function enables or disables automatic use of wxGetTranslation for following strings: wxEnumProperty list labels, @@ -1814,6 +1836,10 @@ protected: wxArrayPGProperty m_deletedProperties; wxArrayPGProperty m_removedProperties; + /** List of key codes that will not be handed over to editor controls. */ + // FIXME: Make this a hash set once there is template-based wxHashSet. + wxVector m_dedicatedKeys; + // // Temporary values // diff --git a/include/wx/propgrid/propgriddefs.h b/include/wx/propgrid/propgriddefs.h index a9f7778fb2..57a042eb35 100644 --- a/include/wx/propgrid/propgriddefs.h +++ b/include/wx/propgrid/propgriddefs.h @@ -340,6 +340,19 @@ WX_DECLARE_HASH_MAP_WITH_DECL(wxInt32, wxPGHashMapI2I, class WXDLLIMPEXP_PROPGRID); +// Utility to find if specific item is in a vector. Returns index to +// the item, or wxNOT_FOUND if not present. +template +int wxPGFindInVector( CONTAINER vector, const T& item ) +{ + for ( unsigned int i=0; iAddActionTrigger(wxPG_ACTION_NEXT_PROPERTY, + WXK_RETURN); + propGrid->DedicateKey(WXK_RETURN); + @endcode + @param action Which action to trigger. See @ref propgrid_keyboard_actions. @param keycode @@ -546,6 +555,16 @@ public: long style = wxPG_DEFAULT_STYLE, const wxChar* name = wxPropertyGridNameStr ); + /** + Dedicates a specific keycode to wxPropertyGrid. This means that such + key presses will not be redirected to editor controls. + + Using this function allows, for example, navigation between + properties using arrow keys even when the focus is in the editor + control. + */ + void DedicateKey( int keycode ); + /** Enables or disables (shows/hides) categories according to parameter enable. diff --git a/samples/propgrid/propgrid.cpp b/samples/propgrid/propgrid.cpp index 9bca75f01b..83002fb868 100644 --- a/samples/propgrid/propgrid.cpp +++ b/samples/propgrid/propgrid.cpp @@ -691,7 +691,8 @@ enum ID_RUNMINIMAL, ID_ENABLELABELEDITING, ID_VETOCOLDRAG, - ID_SHOWHEADER + ID_SHOWHEADER, + ID_ONEXTENDEDKEYNAV }; // ----------------------------------------------------------------------- @@ -756,6 +757,7 @@ BEGIN_EVENT_TABLE(FormMain, wxFrame) EVT_MENU( ID_ITERATE2, FormMain::OnIterate2Click ) EVT_MENU( ID_ITERATE3, FormMain::OnIterate3Click ) EVT_MENU( ID_ITERATE4, FormMain::OnIterate4Click ) + EVT_MENU( ID_ONEXTENDEDKEYNAV, FormMain::OnExtendedKeyNav ) EVT_MENU( ID_SETBGCOLOUR, FormMain::OnSetBackgroundColour ) EVT_MENU( ID_SETBGCOLOURRECUR, FormMain::OnSetBackgroundColour ) EVT_MENU( ID_CLEARMODIF, FormMain::OnClearModifyStatusClick ) @@ -2308,6 +2310,11 @@ FormMain::FormMain(const wxString& title, const wxPoint& pos, const wxSize& size menuTools2->Append(ID_ITERATE3, wxT("Reverse Iterate Over Properties") ); menuTools2->Append(ID_ITERATE4, wxT("Iterate Over Categories") ); menuTools2->AppendSeparator(); + menuTools2->Append(ID_ONEXTENDEDKEYNAV, "Extend Keyboard Navigation", + "This will set Enter to navigate to next property, " + "and allows arrow keys to navigate even when in " + "editor control."); + menuTools2->AppendSeparator(); menuTools2->Append(ID_SETPROPERTYVALUE, wxT("Set Property Value") ); menuTools2->Append(ID_CLEARMODIF, wxT("Clear Modified Status"), wxT("Clears wxPG_MODIFIED flag from all properties.") ); menuTools2->AppendSeparator(); @@ -2684,6 +2691,25 @@ void FormMain::OnIterate4Click( wxCommandEvent& WXUNUSED(event) ) // ----------------------------------------------------------------------- +void FormMain::OnExtendedKeyNav( wxCommandEvent& WXUNUSED(event) ) +{ + // Use AddActionTrigger() and DedicateKey() to set up Enter, + // Up, and Down keys for navigating between properties. + wxPropertyGrid* propGrid = m_pPropGridManager->GetGrid(); + + propGrid->AddActionTrigger(wxPG_ACTION_NEXT_PROPERTY, + WXK_RETURN); + propGrid->DedicateKey(WXK_RETURN); + + // Up and Down keys are alredy associated with navigation, + // but we must also prevent them from being eaten by + // editor controls. + propGrid->DedicateKey(WXK_UP); + propGrid->DedicateKey(WXK_DOWN); +} + +// ----------------------------------------------------------------------- + void FormMain::OnFitColumnsClick( wxCommandEvent& WXUNUSED(event) ) { wxPropertyGridPage* page = m_pPropGridManager->GetCurrentPage(); diff --git a/samples/propgrid/propgrid.h b/samples/propgrid/propgrid.h index fb051d3978..5a73ace546 100644 --- a/samples/propgrid/propgrid.h +++ b/samples/propgrid/propgrid.h @@ -226,6 +226,8 @@ public: void OnIterate3Click( wxCommandEvent& event ); void OnIterate4Click( wxCommandEvent& event ); + void OnExtendedKeyNav( wxCommandEvent& event ); + void OnPropertyGridChange( wxPropertyGridEvent& event ); void OnPropertyGridChanging( wxPropertyGridEvent& event ); void OnPropertyGridSelect( wxPropertyGridEvent& event ); diff --git a/src/propgrid/property.cpp b/src/propgrid/property.cpp index 93067a10f6..04d2504498 100644 --- a/src/propgrid/property.cpp +++ b/src/propgrid/property.cpp @@ -692,12 +692,7 @@ wxPropertyGrid* wxPGProperty::GetGrid() const int wxPGProperty::Index( const wxPGProperty* p ) const { - for ( unsigned int i = 0; i