]> git.saurik.com Git - wxWidgets.git/commitdiff
Generate key events in generic wxDataViewCtrl implementation.
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 14 Jul 2010 11:12:03 +0000 (11:12 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 14 Jul 2010 11:12:03 +0000 (11:12 +0000)
Forward wxEVT_CHAR events from wxDataViewMainWindow to the parent window so
that they could be processed at wxDataViewCtrl level.

Call DisableKeyboardScrolling() to ensure that cursor movement keys are not
always eaten by the parent window but can be used for the navigation in the
control if they're not processed by user.

Add a test keyboard handler to the dataview sample to check that handling keys
in wxDataViewCtrl does work.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64957 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/dataview/dataview.cpp
src/generic/datavgen.cpp

index 6383ede75fcf8f38b4277fe7c9ac4b56814f7038..e443e2c25d335421e5a23801d8e548894e1b28bc 100644 (file)
@@ -71,8 +71,8 @@ public:
                            unsigned int nPanel,
                            unsigned long style = 0);
 
-public:     // event handlers
-
+private:
+    // event handlers
     void OnStyleChange(wxCommandEvent& event);
     void OnSetBackgroundColour(wxCommandEvent& event);
     void OnSetForegroundColour(wxCommandEvent& event);
@@ -127,7 +127,12 @@ public:     // event handlers
     void OnDropPossible( wxDataViewEvent &event );
     void OnDrop( wxDataViewEvent &event );
 
-private:
+    void OnDataViewChar(wxKeyEvent& event);
+
+    // helper used by both OnDeleteSelected() and OnDataViewChar()
+    void DeleteSelectedItems();
+
+
     wxNotebook* m_notebook;
 
     // the controls stored in the various tabs of the main notebook:
@@ -510,6 +515,9 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
             m_ctrl[0] =
                 new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
                                     wxDefaultSize, style );
+            m_ctrl[0]->Connect(wxEVT_CHAR,
+                               wxKeyEventHandler(MyFrame::OnDataViewChar),
+                               NULL, this);
 
             m_music_model = new MyMusicTreeModel;
             m_ctrl[0]->AssociateModel( m_music_model.get() );
@@ -851,7 +859,7 @@ void MyFrame::OnAddMozart( wxCommandEvent& WXUNUSED(event) )
     m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
 }
 
-void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
+void MyFrame::DeleteSelectedItems()
 {
     wxDataViewItemArray items;
     int len = m_ctrl[0]->GetSelections( items );
@@ -860,6 +868,11 @@ void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
             m_music_model->Delete( items[i] );
 }
 
+void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
+{
+    DeleteSelectedItems();
+}
+
 void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
 {
     m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) );
@@ -1074,6 +1087,13 @@ void MyFrame::OnRightClick( wxMouseEvent &event )
                  event.GetX(), event.GetY() );
 }
 
+void MyFrame::OnDataViewChar(wxKeyEvent& event)
+{
+    if ( event.GetKeyCode() == WXK_DELETE )
+        DeleteSelectedItems();
+    else
+        event.Skip();
+}
 
 // ----------------------------------------------------------------------------
 // MyFrame - event handlers for the second page
index dc1bf273c0ce7db91c752818a83a07684c618076..dd7813621d9f095ad2afcc6792ee110a06fa9d55 100644 (file)
@@ -3281,7 +3281,15 @@ void wxDataViewMainWindow::DestroyTree()
 
 void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
 {
-    if ( GetParent()->HandleAsNavigationKey(event) )
+    wxWindow * const parent = GetParent();
+
+    // propagate the char event upwards
+    wxKeyEvent eventForParent(event);
+    eventForParent.SetEventObject(parent);
+    if ( parent->ProcessWindowEvent(eventForParent) )
+        return;
+
+    if ( parent->HandleAsNavigationKey(event) )
         return;
 
     // no item -> nothing to do
@@ -3299,7 +3307,6 @@ void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
     {
         case WXK_RETURN:
             {
-                wxWindow *parent = GetParent();
                 wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,
                                    parent->GetId());
                 le.SetItem( GetItemByRow(m_currentRow) );
@@ -3861,6 +3868,11 @@ bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
 
     m_clientArea = new wxDataViewMainWindow( this, wxID_ANY );
 
+    // We use the cursor keys for moving the selection, not scrolling, so call
+    // this method to ensure wxScrollHelperEvtHandler doesn't catch all
+    // keyboard events forwarded to us from wxListMainWindow.
+    DisableKeyboardScrolling();
+
     if (HasFlag(wxDV_NO_HEADER))
         m_headerArea = NULL;
     else