added ChangeCursorMode() method, rewrote the col/row resizing code to capture/release mouse properly

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5963 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-02-11 17:43:42 +00:00
parent c3b177ae63
commit e2b42eebeb
2 changed files with 137 additions and 60 deletions

View File

@@ -949,14 +949,28 @@ protected:
bool m_inOnKeyDown; bool m_inOnKeyDown;
int m_batchCount; int m_batchCount;
int m_cursorMode; enum CursorMode
enum { WXGRID_CURSOR_SELECT_CELL, {
WXGRID_CURSOR_RESIZE_ROW, WXGRID_CURSOR_SELECT_CELL,
WXGRID_CURSOR_RESIZE_COL, WXGRID_CURSOR_RESIZE_ROW,
WXGRID_CURSOR_SELECT_ROW, WXGRID_CURSOR_RESIZE_COL,
WXGRID_CURSOR_SELECT_COL WXGRID_CURSOR_SELECT_ROW,
WXGRID_CURSOR_SELECT_COL
}; };
// this method not only sets m_cursorMode but also sets the correct cursor
// for the given mode and, if captureMouse is not FALSE releases the mouse
// if it was captured and captures it if it must be captured
//
// for this to work, you should always use it and not set m_cursorMode
// directly!
void ChangeCursorMode(CursorMode mode,
wxWindow *win = (wxWindow *)NULL,
bool captureMouse = TRUE);
wxWindow *m_winCapture; // the window which captured the mouse
CursorMode m_cursorMode;
int m_dragLastPos; int m_dragLastPos;
int m_dragRowOrCol; int m_dragRowOrCol;
bool m_isDragging; bool m_isDragging;

View File

@@ -1323,6 +1323,7 @@ void wxGrid::Init()
m_gridLinesEnabled = TRUE; m_gridLinesEnabled = TRUE;
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; m_cursorMode = WXGRID_CURSOR_SELECT_CELL;
m_winCapture = (wxWindow *)NULL;
m_dragLastPos = -1; m_dragLastPos = -1;
m_dragRowOrCol = -1; m_dragRowOrCol = -1;
m_isDragging = FALSE; m_isDragging = FALSE;
@@ -1773,14 +1774,16 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
break; break;
case WXGRID_CURSOR_SELECT_ROW: case WXGRID_CURSOR_SELECT_ROW:
{
if ( (row = YToRow( y )) >= 0 && if ( (row = YToRow( y )) >= 0 &&
!IsInSelection( row, 0 ) ) !IsInSelection( row, 0 ) )
{ {
SelectRow( row, TRUE ); SelectRow( row, TRUE );
} }
}
break; // default label to suppress warnings about "enumeration value
// 'xxx' not handled in switch
default:
break;
} }
} }
return; return;
@@ -1793,11 +1796,10 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
// //
if ( event.Entering() || event.Leaving() ) if ( event.Entering() || event.Leaving() )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
m_rowLabelWin->SetCursor( *wxSTANDARD_CURSOR );
} }
// ------------ Left button pressed // ------------ Left button pressed
// //
else if ( event.LeftDown() ) else if ( event.LeftDown() )
@@ -1813,14 +1815,14 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
!SendEvent( EVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) ) !SendEvent( EVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
{ {
SelectRow( row, event.ShiftDown() ); SelectRow( row, event.ShiftDown() );
m_cursorMode = WXGRID_CURSOR_SELECT_ROW; ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
} }
} }
else else
{ {
// starting to drag-resize a row // starting to drag-resize a row
// //
m_rowLabelWin->CaptureMouse(); ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
} }
} }
@@ -1843,7 +1845,6 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
{ {
if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
{ {
m_rowLabelWin->ReleaseMouse();
DoEndDragResizeRow(); DoEndDragResizeRow();
// Note: we are ending the event *after* doing // Note: we are ending the event *after* doing
@@ -1852,8 +1853,8 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
SendEvent( EVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event ); SendEvent( EVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
} }
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
m_dragLastPos = -1; m_dragLastPos = -1;
} }
@@ -1890,14 +1891,13 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
{ {
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_RESIZE_ROW; // don't capture the mouse yet
m_rowLabelWin->SetCursor( m_rowResizeCursor ); ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
} }
} }
else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
m_rowLabelWin->SetCursor( *wxSTANDARD_CURSOR );
} }
} }
} }
@@ -1936,14 +1936,16 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
break; break;
case WXGRID_CURSOR_SELECT_COL: case WXGRID_CURSOR_SELECT_COL:
{
if ( (col = XToCol( x )) >= 0 && if ( (col = XToCol( x )) >= 0 &&
!IsInSelection( 0, col ) ) !IsInSelection( 0, col ) )
{ {
SelectCol( col, TRUE ); SelectCol( col, TRUE );
} }
}
break; // default label to suppress warnings about "enumeration value
// 'xxx' not handled in switch
default:
break;
} }
} }
return; return;
@@ -1956,11 +1958,10 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
// //
if ( event.Entering() || event.Leaving() ) if ( event.Entering() || event.Leaving() )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
m_colLabelWin->SetCursor( *wxSTANDARD_CURSOR );
} }
// ------------ Left button pressed // ------------ Left button pressed
// //
else if ( event.LeftDown() ) else if ( event.LeftDown() )
@@ -1976,14 +1977,14 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
!SendEvent( EVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) ) !SendEvent( EVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
{ {
SelectCol( col, event.ShiftDown() ); SelectCol( col, event.ShiftDown() );
m_cursorMode = WXGRID_CURSOR_SELECT_COL; ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
} }
} }
else else
{ {
// starting to drag-resize a col // starting to drag-resize a col
// //
m_colLabelWin->CaptureMouse(); ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
} }
} }
@@ -2006,16 +2007,15 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
{ {
if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
{ {
m_colLabelWin->ReleaseMouse();
DoEndDragResizeCol(); DoEndDragResizeCol();
// Note: we are ending the event *after* doing // Note: we are ending the event *after* doing
// default processing in this case // default processing in this case
// //
SendEvent( EVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); SendEvent( EVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
} }
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
m_dragLastPos = -1; m_dragLastPos = -1;
} }
@@ -2053,14 +2053,13 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
{ {
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_RESIZE_COL; // don't capture the cursor yet
m_colLabelWin->SetCursor( m_colResizeCursor ); ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
} }
} }
else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
m_colLabelWin->SetCursor( *wxSTANDARD_CURSOR );
} }
} }
} }
@@ -2101,6 +2100,68 @@ void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
} }
} }
void wxGrid::ChangeCursorMode(CursorMode mode,
wxWindow *win,
bool captureMouse)
{
#ifdef __WXDEBUG__
static const wxChar *cursorModes[] =
{
_T("SELECT_CELL"),
_T("RESIZE_ROW"),
_T("RESIZE_COL"),
_T("SELECT_ROW"),
_T("SELECT_COL")
};
wxLogDebug(_T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
win == m_colLabelWin ? _T("colLabelWin")
: win ? _T("rowLabelWin")
: _T("gridWin"),
cursorModes[m_cursorMode], cursorModes[mode]);
#endif // __WXDEBUG__
if ( mode == m_cursorMode )
return;
if ( !win )
{
// by default use the grid itself
win = m_gridWin;
}
if ( m_winCapture )
{
m_winCapture->ReleaseMouse();
m_winCapture = (wxWindow *)NULL;
}
m_cursorMode = mode;
switch ( m_cursorMode )
{
case WXGRID_CURSOR_RESIZE_ROW:
win->SetCursor( m_rowResizeCursor );
break;
case WXGRID_CURSOR_RESIZE_COL:
win->SetCursor( m_colResizeCursor );
break;
default:
win->SetCursor( *wxSTANDARD_CURSOR );
}
// we need to capture mouse when resizing
bool resize = m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ||
m_cursorMode == WXGRID_CURSOR_RESIZE_COL;
if ( captureMouse && resize )
{
win->CaptureMouse();
m_winCapture = win;
}
}
void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event ) void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
{ {
@@ -2153,7 +2214,7 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
int cw, ch, dummy, top; int cw, ch, dummy, top;
m_gridWin->GetClientSize( &cw, &ch ); m_gridWin->GetClientSize( &cw, &ch );
CalcUnscrolledPosition( 0, 0, &dummy, &top ); CalcUnscrolledPosition( 0, 0, &dummy, &top );
wxClientDC dc( m_gridWin ); wxClientDC dc( m_gridWin );
PrepareDC( dc ); PrepareDC( dc );
dc.SetLogicalFunction(wxINVERT); dc.SetLogicalFunction(wxINVERT);
@@ -2164,7 +2225,7 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
dc.DrawLine( x, top, x, top+ch ); dc.DrawLine( x, top, x, top+ch );
m_dragLastPos = x; m_dragLastPos = x;
} }
return; return;
} }
@@ -2172,15 +2233,21 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
if ( coords != wxGridNoCellCoords ) if ( coords != wxGridNoCellCoords )
{ {
// VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
// immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
// wxGTK
#if 0
if ( event.Entering() || event.Leaving() ) if ( event.Entering() || event.Leaving() )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
m_gridWin->SetCursor( *wxSTANDARD_CURSOR ); m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
} }
else
#endif // 0
// ------------ Left button pressed // ------------ Left button pressed
// //
else if ( event.LeftDown() ) if ( event.LeftDown() )
{ {
if ( event.ShiftDown() ) if ( event.ShiftDown() )
{ {
@@ -2233,7 +2300,7 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
} }
else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW ) else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
{ {
m_gridWin->ReleaseMouse(); ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
DoEndDragResizeRow(); DoEndDragResizeRow();
// Note: we are ending the event *after* doing // Note: we are ending the event *after* doing
@@ -2243,15 +2310,15 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
} }
else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL ) else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
{ {
m_gridWin->ReleaseMouse(); ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
DoEndDragResizeCol(); DoEndDragResizeCol();
// Note: we are ending the event *after* doing // Note: we are ending the event *after* doing
// default processing in this case // default processing in this case
// //
SendEvent( EVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event ); SendEvent( EVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
} }
m_dragLastPos = -1; m_dragLastPos = -1;
} }
@@ -2295,8 +2362,7 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
// //
if ( dragRow >= 0 && dragCol >= 0 ) if ( dragRow >= 0 && dragCol >= 0 )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
return; return;
} }
@@ -2306,32 +2372,29 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_RESIZE_ROW; ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
m_gridWin->SetCursor( m_rowResizeCursor );
} }
return; return;
} }
if ( dragCol >= 0 ) if ( dragCol >= 0 )
{ {
m_dragRowOrCol = dragCol; m_dragRowOrCol = dragCol;
if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL ) if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_RESIZE_COL; ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
m_gridWin->SetCursor( m_colResizeCursor );
} }
return; return;
} }
// Neither on a row or col edge // Neither on a row or col edge
// //
if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL ) if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
{ {
m_cursorMode = WXGRID_CURSOR_SELECT_CELL; ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
} }
} }
} }
@@ -2357,7 +2420,7 @@ void wxGrid::DoEndDragResizeRow()
int rowTop = m_rowBottoms[m_dragRowOrCol] - m_rowHeights[m_dragRowOrCol]; int rowTop = m_rowBottoms[m_dragRowOrCol] - m_rowHeights[m_dragRowOrCol];
SetRowSize( m_dragRowOrCol, SetRowSize( m_dragRowOrCol,
wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) ); wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
if ( !GetBatchCount() ) if ( !GetBatchCount() )
{ {
// Only needed to get the correct rect.y: // Only needed to get the correct rect.y:
@@ -2408,7 +2471,7 @@ void wxGrid::DoEndDragResizeCol()
rect.height = ch; rect.height = ch;
m_gridWin->Refresh( FALSE, &rect ); m_gridWin->Refresh( FALSE, &rect );
} }
ShowCellEditControl(); ShowCellEditControl();
} }
} }
@@ -3369,7 +3432,7 @@ void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
int pos; int pos;
wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix ); wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix ); wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
while ( startPos < (int)tVal.Length() ) while ( startPos < (int)tVal.Length() )
{ {
pos = tVal.Mid(startPos).Find( eol ); pos = tVal.Mid(startPos).Find( eol );