Generate key events in generic wxDataViewCtrl implementation.

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
This commit is contained in:
Vadim Zeitlin
2010-07-14 11:12:03 +00:00
parent e0ff229573
commit 63ced01b22
2 changed files with 38 additions and 6 deletions

View File

@@ -71,8 +71,8 @@ public:
unsigned int nPanel, unsigned int nPanel,
unsigned long style = 0); unsigned long style = 0);
public: // event handlers private:
// event handlers
void OnStyleChange(wxCommandEvent& event); void OnStyleChange(wxCommandEvent& event);
void OnSetBackgroundColour(wxCommandEvent& event); void OnSetBackgroundColour(wxCommandEvent& event);
void OnSetForegroundColour(wxCommandEvent& event); void OnSetForegroundColour(wxCommandEvent& event);
@@ -127,7 +127,12 @@ public: // event handlers
void OnDropPossible( wxDataViewEvent &event ); void OnDropPossible( wxDataViewEvent &event );
void OnDrop( wxDataViewEvent &event ); void OnDrop( wxDataViewEvent &event );
private: void OnDataViewChar(wxKeyEvent& event);
// helper used by both OnDeleteSelected() and OnDataViewChar()
void DeleteSelectedItems();
wxNotebook* m_notebook; wxNotebook* m_notebook;
// the controls stored in the various tabs of the main 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] = m_ctrl[0] =
new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition, new wxDataViewCtrl( parent, ID_MUSIC_CTRL, wxDefaultPosition,
wxDefaultSize, style ); wxDefaultSize, style );
m_ctrl[0]->Connect(wxEVT_CHAR,
wxKeyEventHandler(MyFrame::OnDataViewChar),
NULL, this);
m_music_model = new MyMusicTreeModel; m_music_model = new MyMusicTreeModel;
m_ctrl[0]->AssociateModel( m_music_model.get() ); 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 ); m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 );
} }
void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) ) void MyFrame::DeleteSelectedItems()
{ {
wxDataViewItemArray items; wxDataViewItemArray items;
int len = m_ctrl[0]->GetSelections( items ); int len = m_ctrl[0]->GetSelections( items );
@@ -860,6 +868,11 @@ void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
m_music_model->Delete( items[i] ); m_music_model->Delete( items[i] );
} }
void MyFrame::OnDeleteSelected( wxCommandEvent& WXUNUSED(event) )
{
DeleteSelectedItems();
}
void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) ) void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) )
{ {
m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) ); m_ctrl[0]->DeleteColumn( m_ctrl[0]->GetColumn( 2 ) );
@@ -1074,6 +1087,13 @@ void MyFrame::OnRightClick( wxMouseEvent &event )
event.GetX(), event.GetY() ); 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 // MyFrame - event handlers for the second page

View File

@@ -3281,7 +3281,15 @@ void wxDataViewMainWindow::DestroyTree()
void wxDataViewMainWindow::OnChar( wxKeyEvent &event ) 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; return;
// no item -> nothing to do // no item -> nothing to do
@@ -3299,7 +3307,6 @@ void wxDataViewMainWindow::OnChar( wxKeyEvent &event )
{ {
case WXK_RETURN: case WXK_RETURN:
{ {
wxWindow *parent = GetParent();
wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED,
parent->GetId()); parent->GetId());
le.SetItem( GetItemByRow(m_currentRow) ); le.SetItem( GetItemByRow(m_currentRow) );
@@ -3861,6 +3868,11 @@ bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
m_clientArea = new wxDataViewMainWindow( this, wxID_ANY ); 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)) if (HasFlag(wxDV_NO_HEADER))
m_headerArea = NULL; m_headerArea = NULL;
else else