From 5c8fc7c1debd3d2a58c015132f2a3d9e3e26cd38 Mon Sep 17 00:00:00 2001 From: Stefan Neis Date: Sat, 4 Mar 2000 23:09:37 +0000 Subject: [PATCH] Improved handling of selection/deselection events. Improved keyboard navigation. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6445 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/grid.h | 30 ++++--- include/wx/generic/gridsel.h | 6 +- samples/newgrid/griddemo.cpp | 30 ++++--- src/generic/grid.cpp | 155 ++++++++++++++++++-------------- src/generic/gridsel.cpp | 168 ++++++++++++++++++++++++++++------- 5 files changed, 262 insertions(+), 127 deletions(-) diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index cf4d7155f2..40fd20a7d2 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -998,8 +998,8 @@ public: wxGridTableBase * GetTable() const { return m_table; } bool SetTable( wxGridTableBase *table, bool takeOwnership=FALSE, - wxGrid::wxGridSelectionModes selmode = - wxGrid::wxGridSelectCells ); + wxGrid::wxGridSelectionModes selmode = + wxGrid::wxGridSelectCells ); void ClearGrid(); bool InsertRows( int pos = 0, int numRows = 1, bool updateLabels=TRUE ); @@ -1105,16 +1105,16 @@ public: void SetGridCursor( int row, int col ) { SetCurrentCell( wxGridCellCoords(row, col) ); } - bool MoveCursorUp(); - bool MoveCursorDown(); - bool MoveCursorLeft(); - bool MoveCursorRight(); + bool MoveCursorUp( bool expandSelection ); + bool MoveCursorDown( bool expandSelection ); + bool MoveCursorLeft( bool expandSelection ); + bool MoveCursorRight( bool expandSelection ); bool MovePageDown(); bool MovePageUp(); - bool MoveCursorUpBlock(); - bool MoveCursorDownBlock(); - bool MoveCursorLeftBlock(); - bool MoveCursorRightBlock(); + bool MoveCursorUpBlock( bool expandSelection ); + bool MoveCursorDownBlock( bool expandSelection ); + bool MoveCursorLeftBlock( bool expandSelection ); + bool MoveCursorRightBlock( bool expandSelection ); // ------ label and gridline formatting @@ -1732,17 +1732,18 @@ class WXDLLEXPORT wxGridEvent : public wxNotifyEvent public: wxGridEvent() : wxNotifyEvent(), m_row(-1), m_col(-1), m_x(-1), m_y(-1), - m_control(0), m_meta(0), m_shift(0), m_alt(0) + m_selecting(0), m_control(0), m_meta(0), m_shift(0), m_alt(0) { } wxGridEvent(int id, wxEventType type, wxObject* obj, - int row=-1, int col=-1, int x=-1, int y=-1, + int row=-1, int col=-1, int x=-1, int y=-1, bool sel = TRUE, bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE); virtual int GetRow() { return m_row; } virtual int GetCol() { return m_col; } wxPoint GetPosition() { return wxPoint( m_x, m_y ); } + bool Selecting() { return m_selecting; } bool ControlDown() { return m_control; } bool MetaDown() { return m_meta; } bool ShiftDown() { return m_shift; } @@ -1753,6 +1754,7 @@ protected: int m_col; int m_x; int m_y; + bool m_selecting; bool m_control; bool m_meta; bool m_shift; @@ -1802,6 +1804,7 @@ public: { m_topLeft = wxGridNoCellCoords; m_bottomRight = wxGridNoCellCoords; + m_selecting = FALSE; m_control = FALSE; m_meta = FALSE; m_shift = FALSE; @@ -1811,6 +1814,7 @@ public: wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, const wxGridCellCoords& topLeft, const wxGridCellCoords& bottomRight, + bool sel = TRUE, bool control=FALSE, bool shift=FALSE, bool alt=FALSE, bool meta=FALSE); @@ -1820,6 +1824,7 @@ public: int GetBottomRow() { return m_bottomRight.GetRow(); } int GetLeftCol() { return m_topLeft.GetCol(); } int GetRightCol() { return m_bottomRight.GetCol(); } + bool Selecting() { return m_selecting; } bool ControlDown() { return m_control; } bool MetaDown() { return m_meta; } bool ShiftDown() { return m_shift; } @@ -1828,6 +1833,7 @@ public: protected: wxGridCellCoords m_topLeft; wxGridCellCoords m_bottomRight; + bool m_selecting; bool m_control; bool m_meta; bool m_shift; diff --git a/include/wx/generic/gridsel.h b/include/wx/generic/gridsel.h index 08598b3409..295e1fa506 100644 --- a/include/wx/generic/gridsel.h +++ b/include/wx/generic/gridsel.h @@ -31,8 +31,10 @@ public: void SetSelectionMode(wxGrid::wxGridSelectionModes selmode); void SelectRow( int row, bool addToSelected = FALSE ); void SelectCol( int col, bool addToSelected = FALSE ); - void SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol ); - void SelectCell( int row, int col); + void SelectBlock( int topRow, int leftCol, + int bottomRow, int rightCol, + wxMouseEvent* event = 0, bool sendEvent = TRUE ); + void SelectCell( int row, int col, bool sendEvent = TRUE ); void ToggleCellSelection( int row, int col); void ClearSelection(); diff --git a/samples/newgrid/griddemo.cpp b/samples/newgrid/griddemo.cpp index 35c0713d5c..4b3fc2ed9f 100644 --- a/samples/newgrid/griddemo.cpp +++ b/samples/newgrid/griddemo.cpp @@ -165,8 +165,8 @@ GridFrame::GridFrame() wxMenu *selectionMenu = new wxMenu; editMenu->Append( ID_CHANGESEL, "Change &selection mode", - selectionMenu, - "Change selection mode" ); + selectionMenu, + "Change selection mode" ); selectionMenu->Append( ID_SELCELLS, "Select &Cells" ); selectionMenu->Append( ID_SELROWS, "Select &Rows" ); @@ -498,8 +498,8 @@ void GridFrame::DeleteSelectedRows( wxCommandEvent& WXUNUSED(ev) ) if ( grid->IsSelection() ) { for ( int n = 0; n < grid->GetNumberRows(); n++ ) - if ( grid->IsInSelection( n , 0 ) ) - grid->DeleteRows( n, 1 ); + if ( grid->IsInSelection( n , 0 ) ) + grid->DeleteRows( n, 1 ); } } @@ -509,8 +509,8 @@ void GridFrame::DeleteSelectedCols( wxCommandEvent& WXUNUSED(ev) ) if ( grid->IsSelection() ) { for ( int n = 0; n < grid->GetNumberCols(); n++ ) - if ( grid->IsInSelection( 0 , n ) ) - grid->DeleteCols( n, 1 ); + if ( grid->IsInSelection( 0 , n ) ) + grid->DeleteCols( n, 1 ); } } @@ -617,7 +617,11 @@ void GridFrame::OnColSize( wxGridSizeEvent& ev ) void GridFrame::OnSelectCell( wxGridEvent& ev ) { logBuf = ""; - logBuf << "Selected cell at row " << ev.GetRow() + if ( ev.Selecting() ) + logBuf << "Selected "; + else + logBuf << "Deselected "; + logBuf << "cell at row " << ev.GetRow() << " col " << ev.GetCol(); wxLogMessage( "%s", logBuf.c_str() ); @@ -629,10 +633,14 @@ void GridFrame::OnSelectCell( wxGridEvent& ev ) void GridFrame::OnRangeSelected( wxGridRangeSelectEvent& ev ) { logBuf = ""; - logBuf << "Selected cells from row " << ev.GetTopRow() - << " col " << ev.GetLeftCol() - << " to row " << ev.GetBottomRow() - << " col " << ev.GetRightCol(); + if ( ev.Selecting() ) + logBuf << "Selected "; + else + logBuf << "Deselected "; + logBuf << "cells from row " << ev.GetTopRow() + << " col " << ev.GetLeftCol() + << " to row " << ev.GetBottomRow() + << " col " << ev.GetRightCol(); wxLogMessage( "%s", logBuf.c_str() ); diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 992e3925da..7481b5c2a7 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -4324,7 +4324,10 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) ClearSelection(); if ( event.ShiftDown() ) { - SelectBlock( m_currentCellCoords, coords ); + m_selection->SelectBlock( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol(), + coords.GetRow(), + coords.GetCol() ); } else if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 ) @@ -4394,16 +4397,18 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) if ( m_selectingTopLeft != wxGridNoCellCoords && m_selectingBottomRight != wxGridNoCellCoords ) { - m_selection->SelectBlock( m_selectingTopLeft.GetRow(), - m_selectingTopLeft.GetCol(), - m_selectingBottomRight.GetRow(), - m_selectingBottomRight.GetCol() ); if (m_winCapture) { m_winCapture->ReleaseMouse(); m_winCapture = NULL; } - SendEvent( wxEVT_GRID_RANGE_SELECT, -1, -1, event ); + m_selection->SelectBlock( m_selectingTopLeft.GetRow(), + m_selectingTopLeft.GetCol(), + m_selectingBottomRight.GetRow(), + m_selectingBottomRight.GetCol(), + & event ); + m_selectingTopLeft = wxGridNoCellCoords; + m_selectingBottomRight = wxGridNoCellCoords; } // Show the edit control, if it has been hidden for @@ -4897,11 +4902,13 @@ bool wxGrid::SendEvent( const wxEventType type, } else if ( type == wxEVT_GRID_RANGE_SELECT ) { + // Right now, it should _never_ end up here! wxGridRangeSelectEvent gridEvt( GetId(), type, this, m_selectingTopLeft, m_selectingBottomRight, + TRUE, mouseEv.ControlDown(), mouseEv.ShiftDown(), mouseEv.AltDown(), @@ -4915,6 +4922,7 @@ bool wxGrid::SendEvent( const wxEventType type, type, this, row, col, + false, mouseEv.GetX(), mouseEv.GetY(), mouseEv.ControlDown(), mouseEv.ShiftDown(), @@ -5001,11 +5009,6 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) ) { - // TODO: Should also support Shift-cursor keys for - // extending the selection. Maybe add a flag to - // MoveCursorXXX() and MoveCursorXXXBlock() and - // just send event.ShiftDown(). - // try local handlers // switch ( event.KeyCode() ) @@ -5013,44 +5016,44 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) case WXK_UP: if ( event.ControlDown() ) { - MoveCursorUpBlock(); + MoveCursorUpBlock( event.ShiftDown() ); } else { - MoveCursorUp(); + MoveCursorUp( event.ShiftDown() ); } break; case WXK_DOWN: if ( event.ControlDown() ) { - MoveCursorDownBlock(); + MoveCursorDownBlock( event.ShiftDown() ); } else { - MoveCursorDown(); + MoveCursorDown( event.ShiftDown() ); } break; case WXK_LEFT: if ( event.ControlDown() ) { - MoveCursorLeftBlock(); + MoveCursorLeftBlock( event.ShiftDown() ); } else { - MoveCursorLeft(); + MoveCursorLeft( event.ShiftDown() ); } break; case WXK_RIGHT: if ( event.ControlDown() ) { - MoveCursorRightBlock(); + MoveCursorRightBlock( event.ShiftDown() ); } else { - MoveCursorRight(); + MoveCursorRight( event.ShiftDown() ); } break; @@ -5061,15 +5064,19 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) } else { - MoveCursorDown(); + MoveCursorDown( event.ShiftDown() ); } break; + case WXK_ESCAPE: + m_selection->ClearSelection(); + break; + case WXK_TAB: if (event.ShiftDown()) - MoveCursorLeft(); + MoveCursorLeft( FALSE ); else - MoveCursorRight(); + MoveCursorRight( FALSE ); break; case WXK_HOME: @@ -5104,21 +5111,33 @@ void wxGrid::OnKeyDown( wxKeyEvent& event ) MovePageDown(); break; +#if 0 case WXK_SPACE: if ( !IsEditable() ) { - MoveCursorRight(); + MoveCursorRight( FALSE ); break; } // Otherwise fall through to default +#else + case WXK_SPACE: + m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol() ); + break; +#endif default: // alphanumeric keys or F2 (special key just for this) enable // the cell edit control + // On just Shift/Control I get values for event.KeyCode() + // that are outside the range where isalnum's behaviour is + // well defined, so do an additional sanity check. if ( !(event.AltDown() || event.MetaDown() || event.ControlDown()) && - (isalnum(event.KeyCode()) || event.KeyCode() == WXK_F2) && + ((isalnum(event.KeyCode()) && + (event.KeyCode() < 256 && event.KeyCode() >= 0)) || + event.KeyCode() == WXK_F2) && !IsCellEditControlEnabled() && CanEnableCellControl() ) { @@ -5151,12 +5170,6 @@ void wxGrid::OnEraseBackground(wxEraseEvent&) void wxGrid::SetCurrentCell( const wxGridCellCoords& coords ) { - if ( SendEvent( wxEVT_GRID_SELECT_CELL, coords.GetRow(), coords.GetCol() ) ) - { - // the event has been intercepted - do nothing - return; - } - if ( m_displayed && m_currentCellCoords != wxGridNoCellCoords ) { @@ -6117,7 +6130,7 @@ void wxGrid::MakeCellVisible( int row, int col ) // ------ Grid cursor movement functions // -bool wxGrid::MoveCursorUp() +bool wxGrid::MoveCursorUp( bool expandSelection ) { if ( m_currentCellCoords != wxGridNoCellCoords && m_currentCellCoords.GetRow() > 0 ) @@ -6128,6 +6141,9 @@ bool wxGrid::MoveCursorUp() SetCurrentCell( m_currentCellCoords.GetRow() - 1, m_currentCellCoords.GetCol() ); + if ( expandSelection ) + m_selection->SelectCell( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol() ); return TRUE; } @@ -6135,7 +6151,7 @@ bool wxGrid::MoveCursorUp() } -bool wxGrid::MoveCursorDown() +bool wxGrid::MoveCursorDown( bool expandSelection ) { // TODO: allow for scrolling // @@ -6148,6 +6164,9 @@ bool wxGrid::MoveCursorDown() SetCurrentCell( m_currentCellCoords.GetRow() + 1, m_currentCellCoords.GetCol() ); + if ( expandSelection ) + m_selection->SelectCell( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol() ); return TRUE; } @@ -6155,7 +6174,7 @@ bool wxGrid::MoveCursorDown() } -bool wxGrid::MoveCursorLeft() +bool wxGrid::MoveCursorLeft( bool expandSelection ) { if ( m_currentCellCoords != wxGridNoCellCoords && m_currentCellCoords.GetCol() > 0 ) @@ -6166,6 +6185,9 @@ bool wxGrid::MoveCursorLeft() SetCurrentCell( m_currentCellCoords.GetRow(), m_currentCellCoords.GetCol() - 1 ); + if ( expandSelection ) + m_selection->SelectCell( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol() ); return TRUE; } @@ -6173,7 +6195,7 @@ bool wxGrid::MoveCursorLeft() } -bool wxGrid::MoveCursorRight() +bool wxGrid::MoveCursorRight( bool expandSelection ) { if ( m_currentCellCoords != wxGridNoCellCoords && m_currentCellCoords.GetCol() < m_numCols - 1 ) @@ -6184,6 +6206,9 @@ bool wxGrid::MoveCursorRight() SetCurrentCell( m_currentCellCoords.GetRow(), m_currentCellCoords.GetCol() + 1 ); + if ( expandSelection ) + m_selection->SelectCell( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol() ); return TRUE; } @@ -6251,7 +6276,7 @@ bool wxGrid::MovePageDown() return FALSE; } -bool wxGrid::MoveCursorUpBlock() +bool wxGrid::MoveCursorUpBlock( bool expandSelection ) { if ( m_table && m_currentCellCoords != wxGridNoCellCoords && @@ -6298,6 +6323,10 @@ bool wxGrid::MoveCursorUpBlock() } MakeCellVisible( row, col ); + if ( expandSelection ) + m_selection->SelectBlock( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol(), + row, col ); SetCurrentCell( row, col ); return TRUE; @@ -6306,7 +6335,7 @@ bool wxGrid::MoveCursorUpBlock() return FALSE; } -bool wxGrid::MoveCursorDownBlock() +bool wxGrid::MoveCursorDownBlock( bool expandSelection ) { if ( m_table && m_currentCellCoords != wxGridNoCellCoords && @@ -6353,6 +6382,10 @@ bool wxGrid::MoveCursorDownBlock() } MakeCellVisible( row, col ); + if ( expandSelection ) + m_selection->SelectBlock( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol(), + row, col ); SetCurrentCell( row, col ); return TRUE; @@ -6361,7 +6394,7 @@ bool wxGrid::MoveCursorDownBlock() return FALSE; } -bool wxGrid::MoveCursorLeftBlock() +bool wxGrid::MoveCursorLeftBlock( bool expandSelection ) { if ( m_table && m_currentCellCoords != wxGridNoCellCoords && @@ -6408,6 +6441,10 @@ bool wxGrid::MoveCursorLeftBlock() } MakeCellVisible( row, col ); + if ( expandSelection ) + m_selection->SelectBlock( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol(), + row, col ); SetCurrentCell( row, col ); return TRUE; @@ -6416,7 +6453,7 @@ bool wxGrid::MoveCursorLeftBlock() return FALSE; } -bool wxGrid::MoveCursorRightBlock() +bool wxGrid::MoveCursorRightBlock( bool expandSelection ) { if ( m_table && m_currentCellCoords != wxGridNoCellCoords && @@ -6463,6 +6500,10 @@ bool wxGrid::MoveCursorRightBlock() } MakeCellVisible( row, col ); + if ( expandSelection ) + m_selection->SelectBlock( m_currentCellCoords.GetRow(), + m_currentCellCoords.GetCol(), + row, col ); SetCurrentCell( row, col ); return TRUE; @@ -7454,14 +7495,6 @@ void wxGrid::SelectRow( int row, bool addToSelected ) m_selection->ClearSelection(); m_selection->SelectRow( row ); - - wxGridRangeSelectEvent gridEvt( GetId(), - wxEVT_GRID_RANGE_SELECT, - this, - wxGridCellCoords( row, 0 ), - wxGridCellCoords( row, m_numCols - 1 ) ); - - GetEventHandler()->ProcessEvent(gridEvt); } @@ -7471,14 +7504,6 @@ void wxGrid::SelectCol( int col, bool addToSelected ) m_selection->ClearSelection(); m_selection->SelectCol( col ); - - wxGridRangeSelectEvent gridEvt( GetId(), - wxEVT_GRID_RANGE_SELECT, - this, - wxGridCellCoords( 0, col ), - wxGridCellCoords( m_numRows - 1, col ) ); - - GetEventHandler()->ProcessEvent(gridEvt); } @@ -7601,19 +7626,8 @@ void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol ) m_gridWin->Refresh( FALSE, &(rect[i]) ); } - // only generate an event if the block is not being selected by - // dragging the mouse (in which case the event will be generated in - // the mouse event handler) - if ( !m_isDragging ) - { - wxGridRangeSelectEvent gridEvt( GetId(), - wxEVT_GRID_RANGE_SELECT, - this, - m_selectingTopLeft, - m_selectingBottomRight ); - - GetEventHandler()->ProcessEvent(gridEvt); - } + // never generate an event as it will be generated from + // wxGridSelection::SelectBlock! } void wxGrid::SelectAll() @@ -7700,7 +7714,7 @@ wxRect wxGrid::BlockToDeviceRect( const wxGridCellCoords &topLeft, IMPLEMENT_DYNAMIC_CLASS( wxGridEvent, wxEvent ) wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, - int row, int col, int x, int y, + int row, int col, int x, int y, bool sel, bool control, bool shift, bool alt, bool meta ) : wxNotifyEvent( type, id ) { @@ -7708,6 +7722,7 @@ wxGridEvent::wxGridEvent( int id, wxEventType type, wxObject* obj, m_col = col; m_x = x; m_y = y; + m_selecting = sel; m_control = control; m_shift = shift; m_alt = alt; @@ -7741,11 +7756,13 @@ IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent, wxEvent ) wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id, wxEventType type, wxObject* obj, const wxGridCellCoords& topLeft, const wxGridCellCoords& bottomRight, - bool control, bool shift, bool alt, bool meta ) + bool sel, bool control, + bool shift, bool alt, bool meta ) : wxNotifyEvent( type, id ) { m_topLeft = topLeft; m_bottomRight = bottomRight; + m_selecting = sel; m_control = control; m_shift = shift; m_alt = alt; diff --git a/src/generic/gridsel.cpp b/src/generic/gridsel.cpp index 69fabe032d..99d9082014 100644 --- a/src/generic/gridsel.cpp +++ b/src/generic/gridsel.cpp @@ -164,7 +164,8 @@ void wxGridSelection::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) m_blockSelectionTopLeft.RemoveAt(n); m_blockSelectionBottomRight.RemoveAt(n); SelectBlock( topRow, 0, - bottomRow, m_grid->GetNumberCols() - 1 ); + bottomRow, m_grid->GetNumberCols() - 1, + 0, FALSE ); } } else // selmode == wxGridSelectColumns) @@ -174,7 +175,8 @@ void wxGridSelection::SetSelectionMode(wxGrid::wxGridSelectionModes selmode) m_blockSelectionTopLeft.RemoveAt(n); m_blockSelectionBottomRight.RemoveAt(n); SelectBlock( 0, leftCol, - m_grid->GetNumberRows() - 1, rightCol ); + m_grid->GetNumberRows() - 1, rightCol, + 0, FALSE ); } } } @@ -260,8 +262,14 @@ void wxGridSelection::SelectRow( int row, bool addToSelected ) if ( !m_grid->GetBatchCount() ) ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); - // Possibly send event here? This would imply that no event is sent, - // if the row already was part of the selection. + // Send Event + wxGridRangeSelectEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_RANGE_SELECT, + m_grid, + wxGridCellCoords( row, 0 ), + wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ) ); + + m_grid->GetEventHandler()->ProcessEvent(gridEvt); } void wxGridSelection::SelectCol( int col, bool addToSelected ) @@ -342,13 +350,21 @@ void wxGridSelection::SelectCol( int col, bool addToSelected ) if ( !m_grid->GetBatchCount() ) ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); - // Possibly send event here? This would imply that no event is sent, - // if the row already was part of the selection. + // Send Event + wxGridRangeSelectEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_RANGE_SELECT, + m_grid, + wxGridCellCoords( 0, col ), + wxGridCellCoords( m_grid->GetNumberRows() - 1, col ) ); + + m_grid->GetEventHandler()->ProcessEvent(gridEvt); } -void wxGridSelection::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol ) +void wxGridSelection::SelectBlock( int topRow, int leftCol, + int bottomRow, int rightCol, + wxMouseEvent* mouseEv, bool sendEvent ) { - // Fix the coordinates of the block if potentially needed + // Fix the coordinates of the block if needed. if ( m_selectionMode == wxGrid::wxGridSelectRows ) { leftCol = 0; @@ -359,10 +375,23 @@ void wxGridSelection::SelectBlock( int topRow, int leftCol, int bottomRow, int r topRow = 0; bottomRow = m_grid->GetNumberRows() - 1; } + if ( topRow > bottomRow ) + { + int temp = topRow; + topRow = bottomRow; + bottomRow = temp; + } + + if ( leftCol > rightCol ) + { + int temp = leftCol; + leftCol = rightCol; + rightCol = temp; + } // Handle single cell selection in SelectCell. if ( topRow == bottomRow && leftCol == rightCol ) - SelectCell( topRow, leftCol ); + SelectCell( topRow, leftCol, sendEvent ); size_t count, n; // Remove single cells contained in newly selected block. @@ -453,10 +482,36 @@ void wxGridSelection::SelectBlock( int topRow, int leftCol, int bottomRow, int r if ( !m_grid->GetBatchCount() ) ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); - // Possibly send event? + // Send Event, if not disabled. + if ( sendEvent ) + { + if ( mouseEv == 0) + { + wxGridRangeSelectEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_RANGE_SELECT, + m_grid, + wxGridCellCoords( topRow, leftCol ), + wxGridCellCoords( bottomRow, rightCol ) ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); + } + else + { + wxGridRangeSelectEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_RANGE_SELECT, + m_grid, + wxGridCellCoords( topRow, leftCol ), + wxGridCellCoords( bottomRow, rightCol ), + TRUE, + mouseEv->ControlDown(), + mouseEv->ShiftDown(), + mouseEv->AltDown(), + mouseEv->MetaDown() ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); + } + } } -void wxGridSelection::SelectCell( int row, int col) +void wxGridSelection::SelectCell( int row, int col, bool sendEvent ) { if ( m_selectionMode == wxGrid::wxGridSelectRows ) { @@ -478,7 +533,12 @@ void wxGridSelection::SelectCell( int row, int col) if ( !m_grid->GetBatchCount() ) ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); - // Possibly send event? + // Send event + wxGridEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_SELECT_CELL, + m_grid, + row, col ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); } void wxGridSelection::ToggleCellSelection( int row, int col) @@ -515,6 +575,13 @@ void wxGridSelection::ToggleCellSelection( int row, int col) if ( !m_grid->GetBatchCount() ) ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); return; + + // Send event + wxGridEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_SELECT_CELL, + m_grid, + row, col, -1, -1, FALSE ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); } } } @@ -558,16 +625,18 @@ void wxGridSelection::ToggleCellSelection( int row, int col) if ( m_selectionMode != wxGrid::wxGridSelectColumns ) { if ( topRow < row ) - SelectBlock( topRow, leftCol, row - 1, rightCol ); + SelectBlock( topRow, leftCol, + row - 1, rightCol, 0, FALSE ); if ( bottomRow > row ) - SelectBlock( row + 1, leftCol, bottomRow, rightCol ); + SelectBlock( row + 1, leftCol, + bottomRow, rightCol, 0, FALSE ); } if ( m_selectionMode != wxGrid::wxGridSelectRows ) { if ( leftCol < col ) - SelectBlock( row, leftCol, row, col - 1 ); + SelectBlock( row, leftCol, row, col - 1, 0, FALSE ); if ( rightCol > col ) - SelectBlock( row, col + 1, row, rightCol ); + SelectBlock( row, col + 1, row, rightCol, 0, FALSE ); } } } @@ -585,9 +654,11 @@ void wxGridSelection::ToggleCellSelection( int row, int col) if (m_selectionMode == wxGrid::wxGridSelectCells) { if ( col > 0 ) - SelectBlock( row, 0, row, col - 1 ); + SelectBlock( row, 0, row, col - 1, 0, FALSE ); if ( col < m_grid->GetNumberCols() - 1 ) - SelectBlock( row, col + 1, row, m_grid->GetNumberCols() - 1 ); + SelectBlock( row, col + 1, + row, m_grid->GetNumberCols() - 1, + 0, FALSE ); } } } @@ -606,34 +677,65 @@ void wxGridSelection::ToggleCellSelection( int row, int col) if (m_selectionMode == wxGrid::wxGridSelectCells) { if ( row > 0 ) - SelectBlock( 0, col, row - 1, col ); + SelectBlock( 0, col, row - 1, col, 0, FALSE ); if ( row < m_grid->GetNumberRows() - 1 ) - SelectBlock( row + 1, col, m_grid->GetNumberRows() - 1, col ); + SelectBlock( row + 1, col, + m_grid->GetNumberRows() - 1, col, + 0, FALSE ); } } } } - // Refresh the screen; according to m_selectionMode, we - // need to either update only the cell, or the whole row/column. + // Refresh the screen and send the event; according to m_selectionMode, + // we need to either update only the cell, or the whole row/column. wxRect r; switch (m_selectionMode) { case wxGrid::wxGridSelectCells: - r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, col ), - wxGridCellCoords( row, col ) ); - break; + { + r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, col ), + wxGridCellCoords( row, col ) ); + if ( !m_grid->GetBatchCount() ) + ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); + wxGridEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_SELECT_CELL, + m_grid, + row, col, -1, -1, FALSE ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); + break; + } case wxGrid::wxGridSelectRows: - r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, 0 ), - wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ) ); - break; + { + r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, 0 ), + wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ) ); + if ( !m_grid->GetBatchCount() ) + ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); + wxGridRangeSelectEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_RANGE_SELECT, + m_grid, + wxGridCellCoords( row, 0 ), + wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ), + FALSE ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); + break; + } case wxGrid::wxGridSelectColumns: - r = m_grid->BlockToDeviceRect( wxGridCellCoords( 0, col ), - wxGridCellCoords( m_grid->GetNumberRows() - 1, col ) ); - break; + { + r = m_grid->BlockToDeviceRect( wxGridCellCoords( 0, col ), + wxGridCellCoords( m_grid->GetNumberRows() - 1, col ) ); + if ( !m_grid->GetBatchCount() ) + ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); + wxGridRangeSelectEvent gridEvt( m_grid->GetId(), + wxEVT_GRID_RANGE_SELECT, + m_grid, + wxGridCellCoords( 0, col ), + wxGridCellCoords( m_grid->GetNumberRows() - 1, col ), + FALSE ); + m_grid->GetEventHandler()->ProcessEvent(gridEvt); + break; + } } - if ( !m_grid->GetBatchCount() ) - ((wxWindow *)m_grid->m_gridWin)->Refresh( FALSE, &r ); } void wxGridSelection::ClearSelection() -- 2.47.2