+ return TRUE;
+
+ case wxGRIDTABLE_NOTIFY_COLS_DELETED:
+ {
+ size_t pos = msg.GetCommandInt();
+ int numCols = msg.GetCommandInt2();
+ for ( i = 0; i < numCols; i++ )
+ {
+ m_colWidths.Remove( pos );
+ m_colRights.Remove( pos );
+ }
+ m_numCols -= numCols;
+
+ if ( !m_numCols )
+ {
+#if 0 // leave the row alone here so that AppendCols will work subsequently
+ m_numRows = 0;
+ m_rowHeights.Clear();
+ m_rowBottoms.Clear();
+#endif
+ m_currentCellCoords = wxGridNoCellCoords;
+ }
+ else
+ {
+ if ( m_currentCellCoords.GetCol() >= m_numCols )
+ m_currentCellCoords.Set( 0, 0 );
+
+ int w = 0;
+ for ( i = 0; i < m_numCols; i++ )
+ {
+ w += m_colWidths[i];
+ m_colRights[i] = w;
+ }
+ }
+ CalcDimensions();
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+void wxGrid::CalcRowLabelsExposed( wxRegion& reg )
+{
+ wxRegionIterator iter( reg );
+ wxRect r;
+
+ m_rowLabelsExposed.Empty();
+
+ int top, bottom;
+ while ( iter )
+ {
+ r = iter.GetRect();
+
+ // TODO: remove this when we can...
+ // There is a bug in wxMotif that gives garbage update
+ // rectangles if you jump-scroll a long way by clicking the
+ // scrollbar with middle button. This is a work-around
+ //
+#if defined(__WXMOTIF__)
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+ if ( r.GetTop() > ch ) r.SetTop( 0 );
+ r.SetBottom( wxMin( r.GetBottom(), ch ) );
+#endif
+
+ // logical bounds of update region
+ //
+ int dummy;
+ CalcUnscrolledPosition( 0, r.GetTop(), &dummy, &top );
+ CalcUnscrolledPosition( 0, r.GetBottom(), &dummy, &bottom );
+
+ // find the row labels within these bounds
+ //
+ int row;
+ for ( row = 0; row < m_numRows; row++ )
+ {
+ if ( GetRowBottom(row) < top )
+ continue;
+
+ if ( GetRowTop(row) > bottom )
+ break;
+
+ m_rowLabelsExposed.Add( row );
+ }
+
+ iter++ ;
+ }
+}
+
+
+void wxGrid::CalcColLabelsExposed( wxRegion& reg )
+{
+ wxRegionIterator iter( reg );
+ wxRect r;
+
+ m_colLabelsExposed.Empty();
+
+ int left, right;
+ while ( iter )
+ {
+ r = iter.GetRect();
+
+ // TODO: remove this when we can...
+ // There is a bug in wxMotif that gives garbage update
+ // rectangles if you jump-scroll a long way by clicking the
+ // scrollbar with middle button. This is a work-around
+ //
+#if defined(__WXMOTIF__)
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+ if ( r.GetLeft() > cw ) r.SetLeft( 0 );
+ r.SetRight( wxMin( r.GetRight(), cw ) );
+#endif
+
+ // logical bounds of update region
+ //
+ int dummy;
+ CalcUnscrolledPosition( r.GetLeft(), 0, &left, &dummy );
+ CalcUnscrolledPosition( r.GetRight(), 0, &right, &dummy );
+
+ // find the cells within these bounds
+ //
+ int col;
+ for ( col = 0; col < m_numCols; col++ )
+ {
+ if ( GetColRight(col) < left )
+ continue;
+
+ if ( GetColLeft(col) > right )
+ break;
+
+ m_colLabelsExposed.Add( col );
+ }
+
+ iter++ ;
+ }
+}
+
+
+void wxGrid::CalcCellsExposed( wxRegion& reg )
+{
+ wxRegionIterator iter( reg );
+ wxRect r;
+
+ m_cellsExposed.Empty();
+ m_rowsExposed.Empty();
+ m_colsExposed.Empty();
+
+ int left, top, right, bottom;
+ while ( iter )
+ {
+ r = iter.GetRect();
+
+ // TODO: remove this when we can...
+ // There is a bug in wxMotif that gives garbage update
+ // rectangles if you jump-scroll a long way by clicking the
+ // scrollbar with middle button. This is a work-around
+ //
+#if defined(__WXMOTIF__)
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+ if ( r.GetTop() > ch ) r.SetTop( 0 );
+ if ( r.GetLeft() > cw ) r.SetLeft( 0 );
+ r.SetRight( wxMin( r.GetRight(), cw ) );
+ r.SetBottom( wxMin( r.GetBottom(), ch ) );
+#endif
+
+ // logical bounds of update region
+ //
+ CalcUnscrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
+ CalcUnscrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
+
+ // find the cells within these bounds
+ //
+ int row, col;
+ for ( row = 0; row < m_numRows; row++ )
+ {
+ if ( GetRowBottom(row) <= top )
+ continue;
+
+ if ( GetRowTop(row) > bottom )
+ break;
+
+ m_rowsExposed.Add( row );
+
+ for ( col = 0; col < m_numCols; col++ )
+ {
+ if ( GetColRight(col) <= left )
+ continue;
+
+ if ( GetColLeft(col) > right )
+ break;
+
+ if ( m_colsExposed.Index( col ) == wxNOT_FOUND )
+ m_colsExposed.Add( col );
+ m_cellsExposed.Add( wxGridCellCoords( row, col ) );
+ }
+ }
+
+ iter++;
+ }
+}
+
+
+void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
+{
+ int x, y, row;
+ wxPoint pos( event.GetPosition() );
+ CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
+
+ if ( event.Dragging() )
+ {
+ m_isDragging = TRUE;
+
+ if ( event.LeftIsDown() )
+ {
+ switch( m_cursorMode )
+ {
+ case WXGRID_CURSOR_RESIZE_ROW:
+ {
+ int cw, ch, left, dummy;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &left, &dummy );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ y = wxMax( y,
+ GetRowTop(m_dragRowOrCol) +
+ GetRowMinimalHeight(m_dragRowOrCol) );
+ dc.SetLogicalFunction(wxINVERT);
+ if ( m_dragLastPos >= 0 )
+ {
+ dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
+ }
+ dc.DrawLine( left, y, left+cw, y );
+ m_dragLastPos = y;
+ }
+ break;
+
+ case WXGRID_CURSOR_SELECT_ROW:
+ if ( (row = YToRow( y )) >= 0 )
+ {
+ m_selection->SelectRow( row,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ }
+
+ // default label to suppress warnings about "enumeration value
+ // 'xxx' not handled in switch
+ default:
+ break;
+ }
+ }
+ return;
+ }
+
+ m_isDragging = FALSE;
+
+
+ // ------------ Entering or leaving the window
+ //
+ if ( event.Entering() || event.Leaving() )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
+ }
+
+
+ // ------------ Left button pressed
+ //
+ else if ( event.LeftDown() )
+ {
+ // don't send a label click event for a hit on the
+ // edge of the row label - this is probably the user
+ // wanting to resize the row
+ //
+ if ( YToEdgeOfRow(y) < 0 )
+ {
+ row = YToRow(y);
+ if ( row >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
+ {
+ if ( !event.ShiftDown() && !event.ControlDown() )
+ ClearSelection();
+ if ( event.ShiftDown() )
+ m_selection->SelectBlock( m_currentCellCoords.GetRow(),
+ 0,
+ row,
+ GetNumberCols() - 1,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ else
+ m_selection->SelectRow( row,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
+ }
+ }
+ else
+ {
+ // starting to drag-resize a row
+ //
+ if ( CanDragRowSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin);
+ }
+ }
+
+
+ // ------------ Left double click
+ //
+ else if (event.LeftDClick() )
+ {
+ if ( YToEdgeOfRow(y) < 0 )
+ {
+ row = YToRow(y);
+ SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, row, -1, event );
+ }
+ }
+
+
+ // ------------ Left button released
+ //
+ else if ( event.LeftUp() )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
+ {
+ DoEndDragResizeRow();
+
+ // Note: we are ending the event *after* doing
+ // default processing in this case
+ //
+ SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
+ }
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin);
+ m_dragLastPos = -1;
+ }
+
+
+ // ------------ Right button down
+ //
+ else if ( event.RightDown() )
+ {
+ row = YToRow(y);
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, row, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+
+ // ------------ Right double click
+ //
+ else if ( event.RightDClick() )
+ {
+ row = YToRow(y);
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, row, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+
+ // ------------ No buttons down and mouse moving
+ //
+ else if ( event.Moving() )
+ {
+ m_dragRowOrCol = YToEdgeOfRow( y );
+ if ( m_dragRowOrCol >= 0 )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ // don't capture the mouse yet
+ if ( CanDragRowSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW, m_rowLabelWin, FALSE);
+ }
+ }
+ else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_rowLabelWin, FALSE);
+ }
+ }
+}
+
+
+void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
+{
+ int x, y, col;
+ wxPoint pos( event.GetPosition() );
+ CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
+
+ if ( event.Dragging() )
+ {
+ m_isDragging = TRUE;
+
+ if ( event.LeftIsDown() )
+ {
+ switch( m_cursorMode )
+ {
+ case WXGRID_CURSOR_RESIZE_COL:
+ {
+ int cw, ch, dummy, top;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &dummy, &top );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+
+ x = wxMax( x, GetColLeft(m_dragRowOrCol) +
+ GetColMinimalWidth(m_dragRowOrCol));
+ dc.SetLogicalFunction(wxINVERT);
+ if ( m_dragLastPos >= 0 )
+ {
+ dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
+ }
+ dc.DrawLine( x, top, x, top+ch );
+ m_dragLastPos = x;
+ }
+ break;
+
+ case WXGRID_CURSOR_SELECT_COL:
+ if ( (col = XToCol( x )) >= 0 )
+ {
+ m_selection->SelectCol( col,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ }
+
+ // default label to suppress warnings about "enumeration value
+ // 'xxx' not handled in switch
+ default:
+ break;
+ }
+ }
+ return;
+ }
+
+ m_isDragging = FALSE;
+
+
+ // ------------ Entering or leaving the window
+ //
+ if ( event.Entering() || event.Leaving() )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
+ }
+
+
+ // ------------ Left button pressed
+ //
+ else if ( event.LeftDown() )
+ {
+ // don't send a label click event for a hit on the
+ // edge of the col label - this is probably the user
+ // wanting to resize the col
+ //
+ if ( XToEdgeOfCol(x) < 0 )
+ {
+ col = XToCol(x);
+ if ( col >= 0 &&
+ !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
+ {
+ if ( !event.ShiftDown() && !event.ControlDown() )
+ ClearSelection();
+ if ( event.ShiftDown() )
+ m_selection->SelectBlock( 0,
+ m_currentCellCoords.GetCol(),
+ GetNumberRows() - 1, col,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ else
+ m_selection->SelectCol( col,
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
+ }
+ }
+ else
+ {
+ // starting to drag-resize a col
+ //
+ if ( CanDragColSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin);
+ }
+ }
+
+
+ // ------------ Left double click
+ //
+ if ( event.LeftDClick() )
+ {
+ if ( XToEdgeOfCol(x) < 0 )
+ {
+ col = XToCol(x);
+ SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, col, event );
+ }
+ }
+
+
+ // ------------ Left button released
+ //
+ else if ( event.LeftUp() )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
+ {
+ DoEndDragResizeCol();
+
+ // Note: we are ending the event *after* doing
+ // default processing in this case
+ //
+ SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
+ }
+
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin);
+ m_dragLastPos = -1;
+ }
+
+
+ // ------------ Right button down
+ //
+ else if ( event.RightDown() )
+ {
+ col = XToCol(x);
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, col, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+
+ // ------------ Right double click
+ //
+ else if ( event.RightDClick() )
+ {
+ col = XToCol(x);
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, col, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+
+ // ------------ No buttons down and mouse moving
+ //
+ else if ( event.Moving() )
+ {
+ m_dragRowOrCol = XToEdgeOfCol( x );
+ if ( m_dragRowOrCol >= 0 )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ // don't capture the cursor yet
+ if ( CanDragColSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL, m_colLabelWin, FALSE);
+ }
+ }
+ else if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL, m_colLabelWin, FALSE);
+ }
+ }
+}
+
+
+void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent& event )
+{
+ if ( event.LeftDown() )
+ {
+ // indicate corner label by having both row and
+ // col args == -1
+ //
+ if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, -1, event ) )
+ {
+ SelectAll();
+ }
+ }
+
+ else if ( event.LeftDClick() )
+ {
+ SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK, -1, -1, event );
+ }
+
+ else if ( event.RightDown() )
+ {
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK, -1, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+ else if ( event.RightDClick() )
+ {
+ if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK, -1, -1, event ) )
+ {
+ // no default action at the moment
+ }
+ }
+}
+
+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")
+ };
+
+ wxLogTrace(_T("grid"),
+ _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 )
+{
+ int x, y;
+ wxPoint pos( event.GetPosition() );
+ CalcUnscrolledPosition( pos.x, pos.y, &x, &y );
+
+ wxGridCellCoords coords;
+ XYToCell( x, y, coords );
+
+ if ( event.Dragging() )
+ {
+ //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
+
+ // Don't start doing anything until the mouse has been drug at
+ // least 3 pixels in any direction...
+ if (! m_isDragging)
+ {
+ if (m_startDragPos == wxDefaultPosition)
+ {
+ m_startDragPos = pos;
+ return;
+ }
+ if (abs(m_startDragPos.x - pos.x) < 4 && abs(m_startDragPos.y - pos.y) < 4)
+ return;
+ }
+
+ m_isDragging = TRUE;
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ // Hide the edit control, so it
+ // won't interfer with drag-shrinking.
+ if ( IsCellEditControlEnabled() )
+ {
+ HideCellEditControl();
+ SaveEditControlValue();
+ }
+
+ // Have we captured the mouse yet?
+ if (! m_winCapture)
+ {
+ m_winCapture = m_gridWin;
+ m_winCapture->CaptureMouse();
+ }
+
+ if ( coords != wxGridNoCellCoords )
+ {
+ if ( event.ControlDown() )
+ {
+ if ( m_selectingKeyboard == wxGridNoCellCoords)
+ m_selectingKeyboard = coords;
+ SelectBlock ( m_selectingKeyboard, coords );
+ }
+ else
+ {
+ if ( !IsSelection() )
+ {
+ SelectBlock( coords, coords );
+ }
+ else
+ {
+ SelectBlock( m_currentCellCoords, coords );
+ }
+ }
+
+ if (! IsVisible(coords))
+ {
+ MakeCellVisible(coords);
+ // TODO: need to introduce a delay or something here. The
+ // scrolling is way to fast, at least on MSW - also on GTK.
+ }
+ }
+ }
+ else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
+ {
+ int cw, ch, left, dummy;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &left, &dummy );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ y = wxMax( y, GetRowTop(m_dragRowOrCol) +
+ GetRowMinimalHeight(m_dragRowOrCol) );
+ dc.SetLogicalFunction(wxINVERT);
+ if ( m_dragLastPos >= 0 )
+ {
+ dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
+ }
+ dc.DrawLine( left, y, left+cw, y );
+ m_dragLastPos = y;
+ }
+ else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
+ {
+ int cw, ch, dummy, top;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &dummy, &top );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ x = wxMax( x, GetColLeft(m_dragRowOrCol) +
+ GetColMinimalWidth(m_dragRowOrCol) );
+ dc.SetLogicalFunction(wxINVERT);
+ if ( m_dragLastPos >= 0 )
+ {
+ dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
+ }
+ dc.DrawLine( x, top, x, top+ch );
+ m_dragLastPos = x;
+ }
+
+ return;
+ }
+
+ m_isDragging = FALSE;
+ m_startDragPos = wxDefaultPosition;
+
+ // 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() )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ m_gridWin->SetCursor( *wxSTANDARD_CURSOR );
+ }
+ else
+#endif // 0
+
+ // ------------ Left button pressed
+ //
+ if ( event.LeftDown() && coords != wxGridNoCellCoords )
+ {
+ if ( !event.ShiftDown() && !event.ControlDown() )
+ ClearSelection();
+ if ( event.ShiftDown() )
+ {
+ m_selection->SelectBlock( m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol(),
+ coords.GetRow(),
+ coords.GetCol(),
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ }
+ else if ( XToEdgeOfCol(x) < 0 &&
+ YToEdgeOfRow(y) < 0 )
+ {
+ if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK,
+ coords.GetRow(),
+ coords.GetCol(),
+ event ) )
+ {
+ DisableCellEditControl();
+ MakeCellVisible( coords );
+
+ // if this is the second click on this cell then start
+ // the edit control
+ if ( m_waitForSlowClick &&
+ (coords == m_currentCellCoords) &&
+ CanEnableCellControl())
+ {
+ EnableCellEditControl();
+
+ wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
+ wxGridCellEditor *editor = attr->GetEditor(this,
+ coords.GetRow(),
+ coords.GetCol());
+ editor->StartingClick();
+ editor->DecRef();
+ attr->DecRef();
+
+ m_waitForSlowClick = FALSE;
+ }
+ else
+ {
+ if ( event.ControlDown() )
+ {
+ m_selection->ToggleCellSelection( coords.GetRow(),
+ coords.GetCol(),
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ m_selectingTopLeft = wxGridNoCellCoords;
+ m_selectingBottomRight = wxGridNoCellCoords;
+ m_selectingKeyboard = coords;
+ }
+ else
+ SetCurrentCell( coords );
+ m_waitForSlowClick = TRUE;
+ }
+ }
+ }
+ }
+
+
+ // ------------ Left double click
+ //
+ else if ( event.LeftDClick() && coords != wxGridNoCellCoords )
+ {
+ DisableCellEditControl();
+
+ if ( XToEdgeOfCol(x) < 0 && YToEdgeOfRow(y) < 0 )
+ {
+ SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK,
+ coords.GetRow(),
+ coords.GetCol(),
+ event );
+ }
+ }
+
+
+ // ------------ Left button released
+ //
+ else if ( event.LeftUp() )
+ {
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ if ( m_selectingTopLeft != wxGridNoCellCoords &&
+ m_selectingBottomRight != wxGridNoCellCoords )
+ {
+ if (m_winCapture)
+ {
+ m_winCapture->ReleaseMouse();
+ m_winCapture = NULL;
+ }
+ m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
+ m_selectingTopLeft.GetCol(),
+ m_selectingBottomRight.GetRow(),
+ m_selectingBottomRight.GetCol(),
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ m_selectingTopLeft = wxGridNoCellCoords;
+ m_selectingBottomRight = wxGridNoCellCoords;
+ }
+
+ // Show the edit control, if it has been hidden for
+ // drag-shrinking.
+ ShowCellEditControl();
+ }
+ else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_ROW )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ DoEndDragResizeRow();
+
+ // Note: we are ending the event *after* doing
+ // default processing in this case
+ //
+ SendEvent( wxEVT_GRID_ROW_SIZE, m_dragRowOrCol, -1, event );
+ }
+ else if ( m_cursorMode == WXGRID_CURSOR_RESIZE_COL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ DoEndDragResizeCol();
+
+ // Note: we are ending the event *after* doing
+ // default processing in this case
+ //
+ SendEvent( wxEVT_GRID_COL_SIZE, -1, m_dragRowOrCol, event );
+ }
+
+ m_dragLastPos = -1;
+ }
+
+
+ // ------------ Right button down
+ //
+ else if ( event.RightDown() && coords != wxGridNoCellCoords )
+ {
+ DisableCellEditControl();
+ if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK,
+ coords.GetRow(),
+ coords.GetCol(),
+ event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+
+ // ------------ Right double click
+ //
+ else if ( event.RightDClick() && coords != wxGridNoCellCoords )
+ {
+ DisableCellEditControl();
+ if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK,
+ coords.GetRow(),
+ coords.GetCol(),
+ event ) )
+ {
+ // no default action at the moment
+ }
+ }
+
+ // ------------ Moving and no button action
+ //
+ else if ( event.Moving() && !event.IsButton() )
+ {
+ int dragRow = YToEdgeOfRow( y );
+ int dragCol = XToEdgeOfCol( x );
+
+ // Dragging on the corner of a cell to resize in both
+ // directions is not implemented yet...
+ //
+ if ( dragRow >= 0 && dragCol >= 0 )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ return;
+ }
+
+ if ( dragRow >= 0 )
+ {
+ m_dragRowOrCol = dragRow;
+
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ if ( CanDragRowSize() && CanDragGridSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW);
+ }
+
+ if ( dragCol >= 0 )
+ {
+ m_dragRowOrCol = dragCol;
+ }
+
+ return;
+ }
+
+ if ( dragCol >= 0 )
+ {
+ m_dragRowOrCol = dragCol;
+
+ if ( m_cursorMode == WXGRID_CURSOR_SELECT_CELL )
+ {
+ if ( CanDragColSize() && CanDragGridSize() )
+ ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL);
+ }
+
+ return;
+ }
+
+ // Neither on a row or col edge
+ //
+ if ( m_cursorMode != WXGRID_CURSOR_SELECT_CELL )
+ {
+ ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL);
+ }
+ }
+}
+
+
+void wxGrid::DoEndDragResizeRow()
+{
+ if ( m_dragLastPos >= 0 )
+ {
+ // erase the last line and resize the row
+ //
+ int cw, ch, left, dummy;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &left, &dummy );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ dc.SetLogicalFunction( wxINVERT );
+ dc.DrawLine( left, m_dragLastPos, left+cw, m_dragLastPos );
+ HideCellEditControl();
+ SaveEditControlValue();
+
+ int rowTop = GetRowTop(m_dragRowOrCol);
+ SetRowSize( m_dragRowOrCol,
+ wxMax( m_dragLastPos - rowTop, WXGRID_MIN_ROW_HEIGHT ) );
+
+ if ( !GetBatchCount() )
+ {
+ // Only needed to get the correct rect.y:
+ wxRect rect ( CellToRect( m_dragRowOrCol, 0 ) );
+ rect.x = 0;
+ CalcScrolledPosition(0, rect.y, &dummy, &rect.y);
+ rect.width = m_rowLabelWidth;
+ rect.height = ch - rect.y;
+ m_rowLabelWin->Refresh( TRUE, &rect );
+ rect.width = cw;
+ m_gridWin->Refresh( FALSE, &rect );
+ }
+
+ ShowCellEditControl();
+ }
+}
+
+
+void wxGrid::DoEndDragResizeCol()
+{
+ if ( m_dragLastPos >= 0 )
+ {
+ // erase the last line and resize the col
+ //
+ int cw, ch, dummy, top;
+ m_gridWin->GetClientSize( &cw, &ch );
+ CalcUnscrolledPosition( 0, 0, &dummy, &top );
+
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ dc.SetLogicalFunction( wxINVERT );
+ dc.DrawLine( m_dragLastPos, top, m_dragLastPos, top+ch );
+ HideCellEditControl();
+ SaveEditControlValue();
+
+ int colLeft = GetColLeft(m_dragRowOrCol);
+ SetColSize( m_dragRowOrCol,
+ wxMax( m_dragLastPos - colLeft,
+ GetColMinimalWidth(m_dragRowOrCol) ) );
+
+ if ( !GetBatchCount() )
+ {
+ // Only needed to get the correct rect.x:
+ wxRect rect ( CellToRect( 0, m_dragRowOrCol ) );
+ rect.y = 0;
+ CalcScrolledPosition(rect.x, 0, &rect.x, &dummy);
+ rect.width = cw - rect.x;
+ rect.height = m_colLabelHeight;
+ m_colLabelWin->Refresh( TRUE, &rect );
+ rect.height = ch;
+ m_gridWin->Refresh( FALSE, &rect );
+ }
+
+ ShowCellEditControl();
+ }
+}
+
+
+
+//
+// ------ interaction with data model
+//
+bool wxGrid::ProcessTableMessage( wxGridTableMessage& msg )
+{
+ switch ( msg.GetId() )
+ {
+ case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES:
+ return GetModelValues();
+
+ case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES:
+ return SetModelValues();
+
+ case wxGRIDTABLE_NOTIFY_ROWS_INSERTED:
+ case wxGRIDTABLE_NOTIFY_ROWS_APPENDED:
+ case wxGRIDTABLE_NOTIFY_ROWS_DELETED:
+ case wxGRIDTABLE_NOTIFY_COLS_INSERTED:
+ case wxGRIDTABLE_NOTIFY_COLS_APPENDED:
+ case wxGRIDTABLE_NOTIFY_COLS_DELETED:
+ return Redimension( msg );
+
+ default:
+ return FALSE;
+ }
+}
+
+
+
+// The behaviour of this function depends on the grid table class
+// Clear() function. For the default wxGridStringTable class the
+// behavious is to replace all cell contents with wxEmptyString but
+// not to change the number of rows or cols.
+//
+void wxGrid::ClearGrid()
+{
+ if ( m_table )
+ {
+ if (IsCellEditControlEnabled())
+ DisableCellEditControl();
+
+ m_table->Clear();
+ if ( !GetBatchCount() ) m_gridWin->Refresh();
+ }
+}
+
+
+bool wxGrid::InsertRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
+{
+ // TODO: something with updateLabels flag
+
+ if ( !m_created )
+ {
+ wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
+ return FALSE;
+ }
+
+ if ( m_table )
+ {
+ if (IsCellEditControlEnabled())
+ DisableCellEditControl();
+
+ bool ok = m_table->InsertRows( pos, numRows );
+
+ // the table will have sent the results of the insert row
+ // operation to this view object as a grid table message
+ //
+ if ( ok )
+ {
+ if ( m_numCols == 0 )
+ {
+ m_table->AppendCols( WXGRID_DEFAULT_NUMBER_COLS );
+ //
+ // TODO: perhaps instead of appending the default number of cols
+ // we should remember what the last non-zero number of cols was ?
+ //
+ }
+
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ m_selection->UpdateRows( pos, numRows );
+ if ( !GetBatchCount() ) Refresh();
+ }
+
+ return ok;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+
+bool wxGrid::AppendRows( int numRows, bool WXUNUSED(updateLabels) )
+{
+ // TODO: something with updateLabels flag
+
+ if ( !m_created )
+ {
+ wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
+ return FALSE;
+ }
+
+ if ( m_table && m_table->AppendRows( numRows ) )
+ {
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ // the table will have sent the results of the append row
+ // operation to this view object as a grid table message
+ //
+ if ( !GetBatchCount() ) Refresh();
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+
+bool wxGrid::DeleteRows( int pos, int numRows, bool WXUNUSED(updateLabels) )
+{
+ // TODO: something with updateLabels flag
+
+ if ( !m_created )
+ {
+ wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
+ return FALSE;
+ }
+
+ if ( m_table )
+ {
+ if (IsCellEditControlEnabled())
+ DisableCellEditControl();
+
+ if (m_table->DeleteRows( pos, numRows ))
+ {
+
+ // the table will have sent the results of the delete row
+ // operation to this view object as a grid table message
+ //
+ m_selection->UpdateRows( pos, -((int)numRows) );
+ if ( !GetBatchCount() ) Refresh();
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+
+bool wxGrid::InsertCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
+{
+ // TODO: something with updateLabels flag
+
+ if ( !m_created )
+ {
+ wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
+ return FALSE;
+ }
+
+ if ( m_table )
+ {
+ if (IsCellEditControlEnabled())
+ DisableCellEditControl();
+
+ bool ok = m_table->InsertCols( pos, numCols );
+
+ // the table will have sent the results of the insert col
+ // operation to this view object as a grid table message
+ //
+ if ( ok )
+ {
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ m_selection->UpdateCols( pos, numCols );
+ if ( !GetBatchCount() ) Refresh();
+ }
+
+ return ok;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+
+bool wxGrid::AppendCols( int numCols, bool WXUNUSED(updateLabels) )
+{
+ // TODO: something with updateLabels flag
+
+ if ( !m_created )
+ {
+ wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
+ return FALSE;
+ }
+
+ if ( m_table && m_table->AppendCols( numCols ) )
+ {
+ // the table will have sent the results of the append col
+ // operation to this view object as a grid table message
+ //
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ {
+ // if we have just inserted cols into an empty grid the current
+ // cell will be undefined...
+ //
+ SetCurrentCell( 0, 0 );
+ }
+
+ if ( !GetBatchCount() ) Refresh();
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+
+bool wxGrid::DeleteCols( int pos, int numCols, bool WXUNUSED(updateLabels) )
+{
+ // TODO: something with updateLabels flag
+
+ if ( !m_created )
+ {
+ wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
+ return FALSE;
+ }
+
+ if ( m_table )
+ {
+ if (IsCellEditControlEnabled())
+ DisableCellEditControl();
+
+ if ( m_table->DeleteCols( pos, numCols ) )
+ {
+ // the table will have sent the results of the delete col
+ // operation to this view object as a grid table message
+ //
+ m_selection->UpdateCols( pos, -((int)numCols) );
+ if ( !GetBatchCount() ) Refresh();
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+
+
+//
+// ----- event handlers
+//
+
+// Generate a grid event based on a mouse event and
+// return the result of ProcessEvent()
+//
+bool wxGrid::SendEvent( const wxEventType type,
+ int row, int col,
+ wxMouseEvent& mouseEv )
+{
+ if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
+ {
+ int rowOrCol = (row == -1 ? col : row);
+
+ wxGridSizeEvent gridEvt( GetId(),
+ type,
+ this,
+ rowOrCol,
+ mouseEv.GetX(), mouseEv.GetY(),
+ mouseEv.ControlDown(),
+ mouseEv.ShiftDown(),
+ mouseEv.AltDown(),
+ mouseEv.MetaDown() );
+
+ return GetEventHandler()->ProcessEvent(gridEvt);
+ }
+ 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(),
+ mouseEv.MetaDown() );
+
+ return GetEventHandler()->ProcessEvent(gridEvt);
+ }
+ else
+ {
+ wxGridEvent gridEvt( GetId(),
+ type,
+ this,
+ row, col,
+ mouseEv.GetX(), mouseEv.GetY(),
+ FALSE,
+ mouseEv.ControlDown(),
+ mouseEv.ShiftDown(),
+ mouseEv.AltDown(),
+ mouseEv.MetaDown() );
+
+ return GetEventHandler()->ProcessEvent(gridEvt);
+ }
+}
+
+
+// Generate a grid event of specified type and return the result
+// of ProcessEvent().
+//
+bool wxGrid::SendEvent( const wxEventType type,
+ int row, int col )
+{
+ if ( type == wxEVT_GRID_ROW_SIZE || type == wxEVT_GRID_COL_SIZE )
+ {
+ int rowOrCol = (row == -1 ? col : row);
+
+ wxGridSizeEvent gridEvt( GetId(),
+ type,
+ this,
+ rowOrCol );
+
+ return GetEventHandler()->ProcessEvent(gridEvt);
+ }
+ else
+ {
+ wxGridEvent gridEvt( GetId(),
+ type,
+ this,
+ row, col );
+
+ return GetEventHandler()->ProcessEvent(gridEvt);
+ }
+}
+
+
+void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
+{
+ wxPaintDC dc( this );
+}
+
+
+// This is just here to make sure that CalcDimensions gets called when
+// the grid view is resized... then the size event is skipped to allow
+// the box sizers to handle everything
+//
+void wxGrid::OnSize( wxSizeEvent& WXUNUSED(event) )
+{
+ CalcWindowSizes();
+ CalcDimensions();
+}
+
+
+void wxGrid::OnKeyDown( wxKeyEvent& event )
+{
+ if ( m_inOnKeyDown )
+ {
+ // shouldn't be here - we are going round in circles...
+ //
+ wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
+ }
+
+ m_inOnKeyDown = TRUE;
+
+ // propagate the event up and see if it gets processed
+ //
+ wxWindow *parent = GetParent();
+ wxKeyEvent keyEvt( event );
+ keyEvt.SetEventObject( parent );
+
+ if ( !parent->GetEventHandler()->ProcessEvent( keyEvt ) )
+ {
+
+ // try local handlers
+ //
+ if ( !event.ShiftDown() &&
+ m_selectingKeyboard != wxGridNoCellCoords )
+ {
+ if ( m_selectingTopLeft != wxGridNoCellCoords &&
+ m_selectingBottomRight != wxGridNoCellCoords )
+ m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
+ m_selectingTopLeft.GetCol(),
+ m_selectingBottomRight.GetRow(),
+ m_selectingBottomRight.GetCol(),
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ m_selectingTopLeft = wxGridNoCellCoords;
+ m_selectingBottomRight = wxGridNoCellCoords;
+ m_selectingKeyboard = wxGridNoCellCoords;
+ }
+
+ switch ( event.KeyCode() )
+ {
+ case WXK_UP:
+ if ( event.ControlDown() )
+ {
+ MoveCursorUpBlock( event.ShiftDown() );
+ }
+ else
+ {
+ MoveCursorUp( event.ShiftDown() );
+ }
+ break;
+
+ case WXK_DOWN:
+ if ( event.ControlDown() )
+ {
+ MoveCursorDownBlock( event.ShiftDown() );
+ }
+ else
+ {
+ MoveCursorDown( event.ShiftDown() );
+ }
+ break;
+
+ case WXK_LEFT:
+ if ( event.ControlDown() )
+ {
+ MoveCursorLeftBlock( event.ShiftDown() );
+ }
+ else
+ {
+ MoveCursorLeft( event.ShiftDown() );
+ }
+ break;
+
+ case WXK_RIGHT:
+ if ( event.ControlDown() )
+ {
+ MoveCursorRightBlock( event.ShiftDown() );
+ }
+ else
+ {
+ MoveCursorRight( event.ShiftDown() );
+ }
+ break;
+
+ case WXK_RETURN:
+ if ( event.ControlDown() )
+ {
+ event.Skip(); // to let the edit control have the return
+ }
+ else
+ {
+ MoveCursorDown( event.ShiftDown() );
+ }
+ break;
+
+ case WXK_ESCAPE:
+ ClearSelection();
+ break;
+
+ case WXK_TAB:
+ if (event.ShiftDown())
+ MoveCursorLeft( FALSE );
+ else
+ MoveCursorRight( FALSE );
+ break;
+
+ case WXK_HOME:
+ if ( event.ControlDown() )
+ {
+ MakeCellVisible( 0, 0 );
+ SetCurrentCell( 0, 0 );
+ }
+ else
+ {
+ event.Skip();
+ }
+ break;
+
+ case WXK_END:
+ if ( event.ControlDown() )
+ {
+ MakeCellVisible( m_numRows-1, m_numCols-1 );
+ SetCurrentCell( m_numRows-1, m_numCols-1 );
+ }
+ else
+ {
+ event.Skip();
+ }
+ break;
+
+ case WXK_PRIOR:
+ MovePageUp();
+ break;
+
+ case WXK_NEXT:
+ MovePageDown();
+ break;
+
+ case WXK_SPACE:
+ if ( event.ControlDown() )
+ {
+ m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol(),
+ event.ControlDown(),
+ event.ShiftDown(),
+ event.AltDown(),
+ event.MetaDown() );
+ break;
+ }
+ if ( !IsEditable() )
+ {
+ MoveCursorRight( FALSE );
+ break;
+ }
+ // Otherwise fall through to default
+
+ 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((int)event.KeyCode()) &&
+ (event.KeyCode() < 256 && event.KeyCode() >= 0)) ||
+ event.KeyCode() == WXK_F2) &&
+ !IsCellEditControlEnabled() &&
+ CanEnableCellControl() )
+ {
+ EnableCellEditControl();
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellEditor *editor = attr->GetEditor(this, row, col);
+ editor->StartingKey(event);
+ editor->DecRef();
+ attr->DecRef();
+ }
+ else
+ {
+ // let others process char events with modifiers or all
+ // char events for readonly cells
+ event.Skip();
+ }
+ break;
+ }
+ }
+
+ m_inOnKeyDown = FALSE;
+}
+
+void wxGrid::OnEraseBackground(wxEraseEvent&)
+{
+}
+
+void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
+{
+ if ( m_currentCellCoords != wxGridNoCellCoords )
+ {
+ HideCellEditControl();
+ DisableCellEditControl();
+
+ // Clear the old current cell highlight
+ wxRect r = BlockToDeviceRect(m_currentCellCoords, m_currentCellCoords);
+
+ // Otherwise refresh redraws the highlight!
+ m_currentCellCoords = coords;
+
+ m_gridWin->Refresh( FALSE, &r );
+ }
+
+ m_currentCellCoords = coords;
+
+ wxClientDC dc(m_gridWin);
+ PrepareDC(dc);
+
+ wxGridCellAttr* attr = GetCellAttr(coords);
+ DrawCellHighlight(dc, attr);
+ attr->DecRef();
+
+#if 0
+ // SN: For my extended selection code, automatic
+ // deselection is definitely not a good idea.
+ if ( IsSelection() )
+ {
+ wxRect r( SelectionToDeviceRect() );
+ ClearSelection();
+ if ( !GetBatchCount() ) m_gridWin->Refresh( FALSE, &r );
+ }
+#endif
+}
+
+
+//
+// ------ functions to get/send data (see also public functions)
+//
+
+bool wxGrid::GetModelValues()
+{
+ if ( m_table )
+ {
+ // all we need to do is repaint the grid
+ //
+ m_gridWin->Refresh();
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+bool wxGrid::SetModelValues()
+{
+ int row, col;
+
+ if ( m_table )
+ {
+ for ( row = 0; row < m_numRows; row++ )
+ {
+ for ( col = 0; col < m_numCols; col++ )
+ {
+ m_table->SetValue( row, col, GetCellValue(row, col) );
+ }
+ }
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+
+// Note - this function only draws cells that are in the list of
+// exposed cells (usually set from the update region by
+// CalcExposedCells)
+//
+void wxGrid::DrawGridCellArea( wxDC& dc )
+{
+ if ( !m_numRows || !m_numCols ) return;
+
+ size_t i;
+ size_t numCells = m_cellsExposed.GetCount();
+
+ for ( i = 0; i < numCells; i++ )
+ {
+ DrawCell( dc, m_cellsExposed[i] );
+ }
+}
+
+
+void wxGrid::DrawGridSpace( wxDC& dc )
+{
+ if ( m_numRows && m_numCols )
+ {
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+
+ int right, bottom;
+ CalcUnscrolledPosition( cw, ch, &right, &bottom );
+
+ if ( right > GetColRight(m_numCols-1) ||
+ bottom > GetRowBottom(m_numRows-1) )
+ {
+ int left, top;
+ CalcUnscrolledPosition( 0, 0, &left, &top );
+
+ dc.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID) );
+ dc.SetPen( *wxTRANSPARENT_PEN );
+
+ if ( right > GetColRight(m_numCols-1) )
+ dc.DrawRectangle( GetColRight(m_numCols-1), top,
+ right - GetColRight(m_numCols-1), ch );
+
+ if ( bottom > GetRowBottom(m_numRows-1) )
+ dc.DrawRectangle( left, GetRowBottom(m_numRows-1),
+ cw, bottom - GetRowBottom(m_numRows-1) );
+ }
+ }
+}
+
+
+void wxGrid::DrawCell( wxDC& dc, const wxGridCellCoords& coords )
+{
+ int row = coords.GetRow();
+ int col = coords.GetCol();
+
+ if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
+ return;
+
+ // we draw the cell border ourselves
+#if !WXGRID_DRAW_LINES
+ if ( m_gridLinesEnabled )
+ DrawCellBorder( dc, coords );
+#endif
+
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+
+ bool isCurrent = coords == m_currentCellCoords;
+
+ wxRect rect;
+ rect.x = GetColLeft(col);
+ rect.y = GetRowTop(row);
+ rect.width = GetColWidth(col) - 1;
+ rect.height = GetRowHeight(row) - 1;
+
+ // if the editor is shown, we should use it and not the renderer
+ if ( isCurrent && IsCellEditControlEnabled() )
+ {
+ wxGridCellEditor *editor = attr->GetEditor(this, row, col);
+ editor->PaintBackground(rect, attr);
+ editor->DecRef();
+ }
+ else
+ {
+ // but all the rest is drawn by the cell renderer and hence may be
+ // customized
+ wxGridCellRenderer *renderer = attr->GetRenderer(this, row, col);
+ renderer->Draw(*this, *attr, dc, rect, row, col, IsInSelection(coords));
+ renderer->DecRef();
+ }
+
+ attr->DecRef();
+}
+
+void wxGrid::DrawCellHighlight( wxDC& dc, const wxGridCellAttr *attr )
+{
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
+ return;
+
+ wxRect rect;
+ rect.x = GetColLeft(col);
+ rect.y = GetRowTop(row);
+ rect.width = GetColWidth(col) - 1;
+ rect.height = GetRowHeight(row) - 1;
+
+ // hmmm... what could we do here to show that the cell is disabled?
+ // for now, I just draw a thinner border than for the other ones, but
+ // it doesn't look really good
+ dc.SetPen(wxPen(m_gridLineColour, attr->IsReadOnly() ? 1 : 3, wxSOLID));
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+
+ dc.DrawRectangle(rect);
+
+#if 0
+ // VZ: my experiments with 3d borders...
+
+ // how to properly set colours for arbitrary bg?
+ wxCoord x1 = rect.x,
+ y1 = rect.y,
+ x2 = rect.x + rect.width -1,
+ y2 = rect.y + rect.height -1;
+
+ dc.SetPen(*wxWHITE_PEN);
+ dc.DrawLine(x1, y1, x2, y1);
+ dc.DrawLine(x1, y1, x1, y2);
+
+ dc.DrawLine(x1 + 1, y2 - 1, x2 - 1, y2 - 1);
+ dc.DrawLine(x2 - 1, y1 + 1, x2 - 1, y2 );
+
+ dc.SetPen(*wxBLACK_PEN);
+ dc.DrawLine(x1, y2, x2, y2);
+ dc.DrawLine(x2, y1, x2, y2+1);
+#endif // 0
+}
+
+
+void wxGrid::DrawCellBorder( wxDC& dc, const wxGridCellCoords& coords )
+{
+ int row = coords.GetRow();
+ int col = coords.GetCol();
+ if ( GetColWidth(col) <= 0 || GetRowHeight(row) <= 0 )
+ return;
+
+ dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
+
+ // right hand border
+ //
+ dc.DrawLine( GetColRight(col), GetRowTop(row),
+ GetColRight(col), GetRowBottom(row) );
+
+ // bottom border
+ //
+ dc.DrawLine( GetColLeft(col), GetRowBottom(row),
+ GetColRight(col), GetRowBottom(row) );
+}
+
+void wxGrid::DrawHighlight(wxDC& dc)
+{
+ // This if block was previously in wxGrid::OnPaint but that doesn't
+ // seem to get called under wxGTK - MB
+ //
+ if ( m_currentCellCoords == wxGridNoCellCoords &&
+ m_numRows && m_numCols )
+ {
+ m_currentCellCoords.Set(0, 0);
+ }
+
+ if ( IsCellEditControlEnabled() )
+ {
+ // don't show highlight when the edit control is shown
+ return;
+ }
+
+ // if the active cell was repainted, repaint its highlight too because it
+ // might have been damaged by the grid lines
+ size_t count = m_cellsExposed.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ if ( m_cellsExposed[n] == m_currentCellCoords )
+ {
+ wxGridCellAttr* attr = GetCellAttr(m_currentCellCoords);
+ DrawCellHighlight(dc, attr);
+ attr->DecRef();
+
+ break;
+ }
+ }
+}
+
+// TODO: remove this ???
+// This is used to redraw all grid lines e.g. when the grid line colour
+// has been changed
+//
+void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED_GTK(reg) )
+{
+ if ( !m_gridLinesEnabled ||
+ !m_numRows ||
+ !m_numCols ) return;
+
+ int top, bottom, left, right;
+
+#ifndef __WXGTK__
+ if (reg.IsEmpty())
+ {
+ int cw, ch;
+ m_gridWin->GetClientSize(&cw, &ch);
+
+ // virtual coords of visible area
+ //
+ CalcUnscrolledPosition( 0, 0, &left, &top );
+ CalcUnscrolledPosition( cw, ch, &right, &bottom );
+ }
+ else
+ {
+ wxCoord x, y, w, h;
+ reg.GetBox(x, y, w, h);
+ CalcUnscrolledPosition( x, y, &left, &top );
+ CalcUnscrolledPosition( x + w, y + h, &right, &bottom );
+ }
+#else
+ int cw, ch;
+ m_gridWin->GetClientSize(&cw, &ch);
+ CalcUnscrolledPosition( 0, 0, &left, &top );
+ CalcUnscrolledPosition( cw, ch, &right, &bottom );
+#endif
+
+ // avoid drawing grid lines past the last row and col
+ //
+ right = wxMin( right, GetColRight(m_numCols - 1) );
+ bottom = wxMin( bottom, GetRowBottom(m_numRows - 1) );
+
+ dc.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID) );
+
+ // horizontal grid lines
+ //
+ int i;
+ for ( i = 0; i < m_numRows; i++ )
+ {
+ int bot = GetRowBottom(i) - 1;
+
+ if ( bot > bottom )
+ {
+ break;
+ }
+
+ if ( bot >= top )
+ {
+ dc.DrawLine( left, bot, right, bot );
+ }
+ }
+
+
+ // vertical grid lines
+ //
+ for ( i = 0; i < m_numCols; i++ )
+ {
+ int colRight = GetColRight(i) - 1;
+ if ( colRight > right )
+ {
+ break;
+ }
+
+ if ( colRight >= left )
+ {
+ dc.DrawLine( colRight, top, colRight, bottom );
+ }
+ }
+}
+
+
+void wxGrid::DrawRowLabels( wxDC& dc )
+{
+ if ( !m_numRows || !m_numCols ) return;
+
+ size_t i;
+ size_t numLabels = m_rowLabelsExposed.GetCount();
+
+ for ( i = 0; i < numLabels; i++ )
+ {
+ DrawRowLabel( dc, m_rowLabelsExposed[i] );
+ }
+}
+
+
+void wxGrid::DrawRowLabel( wxDC& dc, int row )
+{
+ if ( GetRowHeight(row) <= 0 )
+ return;
+
+ int rowTop = GetRowTop(row),
+ rowBottom = GetRowBottom(row) - 1;
+
+ dc.SetPen( *wxBLACK_PEN );
+ dc.DrawLine( m_rowLabelWidth-1, rowTop,
+ m_rowLabelWidth-1, rowBottom );
+
+ dc.DrawLine( 0, rowBottom, m_rowLabelWidth-1, rowBottom );
+
+ dc.SetPen( *wxWHITE_PEN );
+ dc.DrawLine( 0, rowTop, 0, rowBottom );
+ dc.DrawLine( 0, rowTop, m_rowLabelWidth-1, rowTop );
+
+ dc.SetBackgroundMode( wxTRANSPARENT );
+ dc.SetTextForeground( GetLabelTextColour() );
+ dc.SetFont( GetLabelFont() );
+
+ int hAlign, vAlign;
+ GetRowLabelAlignment( &hAlign, &vAlign );
+
+ wxRect rect;
+ rect.SetX( 2 );
+ rect.SetY( GetRowTop(row) + 2 );
+ rect.SetWidth( m_rowLabelWidth - 4 );
+ rect.SetHeight( GetRowHeight(row) - 4 );
+ DrawTextRectangle( dc, GetRowLabelValue( row ), rect, hAlign, vAlign );
+}
+
+
+void wxGrid::DrawColLabels( wxDC& dc )
+{
+ if ( !m_numRows || !m_numCols ) return;
+
+ size_t i;
+ size_t numLabels = m_colLabelsExposed.GetCount();
+
+ for ( i = 0; i < numLabels; i++ )
+ {
+ DrawColLabel( dc, m_colLabelsExposed[i] );
+ }
+}
+
+
+void wxGrid::DrawColLabel( wxDC& dc, int col )
+{
+ if ( GetColWidth(col) <= 0 )
+ return;
+
+ int colLeft = GetColLeft(col),
+ colRight = GetColRight(col) - 1;
+
+ dc.SetPen( *wxBLACK_PEN );
+ dc.DrawLine( colRight, 0,
+ colRight, m_colLabelHeight-1 );
+
+ dc.DrawLine( colLeft, m_colLabelHeight-1,
+ colRight, m_colLabelHeight-1 );
+
+ dc.SetPen( *wxWHITE_PEN );
+ dc.DrawLine( colLeft, 0, colLeft, m_colLabelHeight-1 );
+ dc.DrawLine( colLeft, 0, colRight, 0 );
+
+ dc.SetBackgroundMode( wxTRANSPARENT );
+ dc.SetTextForeground( GetLabelTextColour() );
+ dc.SetFont( GetLabelFont() );
+
+ dc.SetBackgroundMode( wxTRANSPARENT );
+ dc.SetTextForeground( GetLabelTextColour() );
+ dc.SetFont( GetLabelFont() );
+
+ int hAlign, vAlign;
+ GetColLabelAlignment( &hAlign, &vAlign );
+
+ wxRect rect;
+ rect.SetX( colLeft + 2 );
+ rect.SetY( 2 );
+ rect.SetWidth( GetColWidth(col) - 4 );
+ rect.SetHeight( m_colLabelHeight - 4 );
+ DrawTextRectangle( dc, GetColLabelValue( col ), rect, hAlign, vAlign );
+}
+
+
+void wxGrid::DrawTextRectangle( wxDC& dc,
+ const wxString& value,
+ const wxRect& rect,
+ int horizAlign,
+ int vertAlign )
+{
+ long textWidth, textHeight;
+ long lineWidth, lineHeight;
+ wxArrayString lines;
+
+ dc.SetClippingRegion( rect );
+ StringToLines( value, lines );
+ if ( lines.GetCount() )
+ {
+ GetTextBoxSize( dc, lines, &textWidth, &textHeight );
+ dc.GetTextExtent( lines[0], &lineWidth, &lineHeight );
+
+ float x, y;
+ switch ( horizAlign )
+ {
+ case wxRIGHT:
+ x = rect.x + (rect.width - textWidth - 1);
+ break;
+
+ case wxCENTRE:
+ x = rect.x + ((rect.width - textWidth)/2);
+ break;
+
+ case wxLEFT:
+ default:
+ x = rect.x + 1;
+ break;
+ }
+
+ switch ( vertAlign )
+ {
+ case wxBOTTOM:
+ y = rect.y + (rect.height - textHeight - 1);
+ break;
+
+ case wxCENTRE:
+ y = rect.y + ((rect.height - textHeight)/2);
+ break;
+
+ case wxTOP:
+ default:
+ y = rect.y + 1;
+ break;
+ }
+
+ for ( size_t i = 0; i < lines.GetCount(); i++ )
+ {
+ dc.DrawText( lines[i], (int)x, (int)y );
+ y += lineHeight;
+ }
+ }
+
+ dc.DestroyClippingRegion();
+}
+
+
+// Split multi line text up into an array of strings. Any existing
+// contents of the string array are preserved.
+//
+void wxGrid::StringToLines( const wxString& value, wxArrayString& lines )
+{
+ int startPos = 0;
+ int pos;
+ wxString eol = wxTextFile::GetEOL( wxTextFileType_Unix );
+ wxString tVal = wxTextFile::Translate( value, wxTextFileType_Unix );
+
+ while ( startPos < (int)tVal.Length() )
+ {
+ pos = tVal.Mid(startPos).Find( eol );
+ if ( pos < 0 )
+ {
+ break;
+ }
+ else if ( pos == 0 )
+ {
+ lines.Add( wxEmptyString );
+ }
+ else
+ {
+ lines.Add( value.Mid(startPos, pos) );
+ }
+ startPos += pos+1;
+ }
+ if ( startPos < (int)value.Length() )
+ {
+ lines.Add( value.Mid( startPos ) );
+ }
+}
+
+
+void wxGrid::GetTextBoxSize( wxDC& dc,
+ wxArrayString& lines,
+ long *width, long *height )
+{
+ long w = 0;
+ long h = 0;
+ long lineW, lineH;
+
+ size_t i;
+ for ( i = 0; i < lines.GetCount(); i++ )
+ {
+ dc.GetTextExtent( lines[i], &lineW, &lineH );
+ w = wxMax( w, lineW );
+ h += lineH;
+ }
+
+ *width = w;
+ *height = h;
+}
+
+
+//
+// ------ Edit control functions
+//
+
+
+void wxGrid::EnableEditing( bool edit )
+{
+ // TODO: improve this ?
+ //
+ if ( edit != m_editable )
+ {
+ m_editable = edit;
+
+ // FIXME IMHO this won't disable the edit control if edit == FALSE
+ // because of the check in the beginning of
+ // EnableCellEditControl() just below (VZ)
+ EnableCellEditControl(m_editable);
+ }
+}
+
+
+void wxGrid::EnableCellEditControl( bool enable )
+{
+ if (! m_editable)
+ return;
+
+ if ( m_currentCellCoords == wxGridNoCellCoords )
+ SetCurrentCell( 0, 0 );
+
+ if ( enable != m_cellEditCtrlEnabled )
+ {
+ // TODO allow the app to Veto() this event?
+ SendEvent(enable ? wxEVT_GRID_EDITOR_SHOWN : wxEVT_GRID_EDITOR_HIDDEN);
+
+ if ( enable )
+ {
+ // this should be checked by the caller!
+ wxASSERT_MSG( CanEnableCellControl(),
+ _T("can't enable editing for this cell!") );
+
+ // do it before ShowCellEditControl()
+ m_cellEditCtrlEnabled = enable;
+
+ ShowCellEditControl();
+ }
+ else
+ {
+ HideCellEditControl();
+ SaveEditControlValue();
+
+ // do it after HideCellEditControl()
+ m_cellEditCtrlEnabled = enable;
+ }
+ }
+}
+
+bool wxGrid::IsCurrentCellReadOnly() const
+{
+ // const_cast
+ wxGridCellAttr* attr = ((wxGrid *)this)->GetCellAttr(m_currentCellCoords);
+ bool readonly = attr->IsReadOnly();
+ attr->DecRef();
+
+ return readonly;
+}
+
+bool wxGrid::CanEnableCellControl() const
+{
+ return m_editable && !IsCurrentCellReadOnly();
+}
+
+bool wxGrid::IsCellEditControlEnabled() const
+{
+ // the cell edit control might be disable for all cells or just for the
+ // current one if it's read only
+ return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
+}
+
+void wxGrid::ShowCellEditControl()
+{
+ if ( IsCellEditControlEnabled() )
+ {
+ if ( !IsVisible( m_currentCellCoords ) )
+ {
+ return;
+ }
+ else
+ {
+ wxRect rect = CellToRect( m_currentCellCoords );
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ // convert to scrolled coords
+ //
+ CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
+
+ // done in PaintBackground()
+#if 0
+ // erase the highlight and the cell contents because the editor
+ // might not cover the entire cell
+ wxClientDC dc( m_gridWin );
+ PrepareDC( dc );
+ dc.SetBrush(*wxLIGHT_GREY_BRUSH); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
+ dc.SetPen(*wxTRANSPARENT_PEN);
+ dc.DrawRectangle(rect);
+#endif // 0
+
+ // cell is shifted by one pixel
+ rect.x--;
+ rect.y--;
+
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellEditor* editor = attr->GetEditor(this, row, col);
+ if ( !editor->IsCreated() )
+ {
+ editor->Create(m_gridWin, -1,
+ new wxGridCellEditorEvtHandler(this, editor));
+ }
+
+ editor->Show( TRUE, attr );
+
+ editor->SetSize( rect );
+
+ editor->BeginEdit(row, col, this);
+
+ editor->DecRef();
+ attr->DecRef();
+ }
+ }
+}
+
+
+void wxGrid::HideCellEditControl()
+{
+ if ( IsCellEditControlEnabled() )
+ {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellEditor *editor = attr->GetEditor(this, row, col);
+ editor->Show( FALSE );
+ editor->DecRef();
+ attr->DecRef();
+ m_gridWin->SetFocus();
+ }
+}
+
+
+void wxGrid::SaveEditControlValue()
+{
+ if ( IsCellEditControlEnabled() )
+ {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellEditor* editor = attr->GetEditor(this, row, col);
+ bool changed = editor->EndEdit(row, col, this);
+
+ editor->DecRef();
+ attr->DecRef();
+
+ if (changed)
+ {
+ SendEvent( wxEVT_GRID_CELL_CHANGE,
+ m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol() );
+ }
+ }
+}
+
+
+//
+// ------ Grid location functions
+// Note that all of these functions work with the logical coordinates of
+// grid cells and labels so you will need to convert from device
+// coordinates for mouse events etc.
+//
+
+void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
+{
+ int row = YToRow(y);
+ int col = XToCol(x);
+
+ if ( row == -1 || col == -1 )
+ {
+ coords = wxGridNoCellCoords;
+ }
+ else
+ {
+ coords.Set( row, col );
+ }
+}
+
+
+int wxGrid::YToRow( int y )
+{
+ int i;
+
+ for ( i = 0; i < m_numRows; i++ )
+ {
+ if ( y < GetRowBottom(i) )
+ return i;
+ }
+
+ return -1;
+}
+
+
+int wxGrid::XToCol( int x )
+{
+ int i;
+
+ for ( i = 0; i < m_numCols; i++ )
+ {
+ if ( x < GetColRight(i) )
+ return i;
+ }
+
+ return -1;
+}
+
+
+// return the row number that that the y coord is near the edge of, or
+// -1 if not near an edge
+//
+int wxGrid::YToEdgeOfRow( int y )
+{
+ int i, d;
+
+ for ( i = 0; i < m_numRows; i++ )
+ {
+ if ( GetRowHeight(i) > WXGRID_LABEL_EDGE_ZONE )
+ {
+ d = abs( y - GetRowBottom(i) );
+ if ( d < WXGRID_LABEL_EDGE_ZONE )
+ return i;
+ }
+ }
+
+ return -1;
+}
+
+
+// return the col number that that the x coord is near the edge of, or
+// -1 if not near an edge
+//
+int wxGrid::XToEdgeOfCol( int x )
+{
+ int i, d;
+
+ for ( i = 0; i < m_numCols; i++ )
+ {
+ if ( GetColWidth(i) > WXGRID_LABEL_EDGE_ZONE )
+ {
+ d = abs( x - GetColRight(i) );
+ if ( d < WXGRID_LABEL_EDGE_ZONE )
+ return i;
+ }
+ }
+
+ return -1;
+}
+
+
+wxRect wxGrid::CellToRect( int row, int col )
+{
+ wxRect rect( -1, -1, -1, -1 );
+
+ if ( row >= 0 && row < m_numRows &&
+ col >= 0 && col < m_numCols )
+ {
+ rect.x = GetColLeft(col);
+ rect.y = GetRowTop(row);
+ rect.width = GetColWidth(col);
+ rect.height = GetRowHeight(row);
+ }
+
+ return rect;
+}
+
+
+bool wxGrid::IsVisible( int row, int col, bool wholeCellVisible )
+{
+ // get the cell rectangle in logical coords
+ //
+ wxRect r( CellToRect( row, col ) );
+
+ // convert to device coords
+ //
+ int left, top, right, bottom;
+ CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
+ CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
+
+ // check against the client area of the grid window
+ //
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+
+ if ( wholeCellVisible )
+ {
+ // is the cell wholly visible ?
+ //
+ return ( left >= 0 && right <= cw &&
+ top >= 0 && bottom <= ch );
+ }
+ else
+ {
+ // is the cell partly visible ?
+ //
+ return ( ((left >=0 && left < cw) || (right > 0 && right <= cw)) &&
+ ((top >=0 && top < ch) || (bottom > 0 && bottom <= ch)) );
+ }
+}
+
+
+// make the specified cell location visible by doing a minimal amount
+// of scrolling
+//
+void wxGrid::MakeCellVisible( int row, int col )
+{
+ int i;
+ int xpos = -1, ypos = -1;
+
+ if ( row >= 0 && row < m_numRows &&
+ col >= 0 && col < m_numCols )
+ {
+ // get the cell rectangle in logical coords
+ //
+ wxRect r( CellToRect( row, col ) );
+
+ // convert to device coords
+ //
+ int left, top, right, bottom;
+ CalcScrolledPosition( r.GetLeft(), r.GetTop(), &left, &top );
+ CalcScrolledPosition( r.GetRight(), r.GetBottom(), &right, &bottom );
+
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+
+ if ( top < 0 )
+ {
+ ypos = r.GetTop();
+ }
+ else if ( bottom > ch )
+ {
+ int h = r.GetHeight();
+ ypos = r.GetTop();
+ for ( i = row-1; i >= 0; i-- )
+ {
+ int rowHeight = GetRowHeight(i);
+ if ( h + rowHeight > ch )
+ break;
+
+ h += rowHeight;
+ ypos -= rowHeight;
+ }
+
+ // we divide it later by GRID_SCROLL_LINE, make sure that we don't
+ // have rounding errors (this is important, because if we do, we
+ // might not scroll at all and some cells won't be redrawn)
+ ypos += GRID_SCROLL_LINE / 2;
+ }
+
+ if ( left < 0 )
+ {
+ xpos = r.GetLeft();
+ }
+ else if ( right > cw )
+ {
+ int w = r.GetWidth();
+ xpos = r.GetLeft();
+ for ( i = col-1; i >= 0; i-- )
+ {
+ int colWidth = GetColWidth(i);
+ if ( w + colWidth > cw )
+ break;
+
+ w += colWidth;
+ xpos -= colWidth;
+ }
+
+ // see comment for ypos above
+ xpos += GRID_SCROLL_LINE / 2;
+ }
+
+ if ( xpos != -1 || ypos != -1 )
+ {
+ if ( xpos != -1 ) xpos /= GRID_SCROLL_LINE;
+ if ( ypos != -1 ) ypos /= GRID_SCROLL_LINE;
+ Scroll( xpos, ypos );
+ AdjustScrollbars();
+ }
+ }
+}
+
+
+//
+// ------ Grid cursor movement functions
+//
+
+bool wxGrid::MoveCursorUp( bool expandSelection )
+{
+ if ( m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetRow() > 0 )
+ {
+ if ( expandSelection )
+ {
+ if ( m_selectingKeyboard == wxGridNoCellCoords )
+ m_selectingKeyboard = m_currentCellCoords;
+ m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() - 1 );
+ MakeCellVisible( m_selectingKeyboard.GetRow(),
+ m_selectingKeyboard.GetCol() );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ MakeCellVisible( m_currentCellCoords.GetRow() - 1,
+ m_currentCellCoords.GetCol() );
+ SetCurrentCell( m_currentCellCoords.GetRow() - 1,
+ m_currentCellCoords.GetCol() );
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+bool wxGrid::MoveCursorDown( bool expandSelection )
+{
+ if ( m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetRow() < m_numRows-1 )
+ {
+ if ( expandSelection )
+ {
+ if ( m_selectingKeyboard == wxGridNoCellCoords )
+ m_selectingKeyboard = m_currentCellCoords;
+ m_selectingKeyboard.SetRow( m_selectingKeyboard.GetRow() + 1 );
+ MakeCellVisible( m_selectingKeyboard.GetRow(),
+ m_selectingKeyboard.GetCol() );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ MakeCellVisible( m_currentCellCoords.GetRow() + 1,
+ m_currentCellCoords.GetCol() );
+ SetCurrentCell( m_currentCellCoords.GetRow() + 1,
+ m_currentCellCoords.GetCol() );
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+bool wxGrid::MoveCursorLeft( bool expandSelection )
+{
+ if ( m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetCol() > 0 )
+ {
+ if ( expandSelection )
+ {
+ if ( m_selectingKeyboard == wxGridNoCellCoords )
+ m_selectingKeyboard = m_currentCellCoords;
+ m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() - 1 );
+ MakeCellVisible( m_selectingKeyboard.GetRow(),
+ m_selectingKeyboard.GetCol() );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ MakeCellVisible( m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol() - 1 );
+ SetCurrentCell( m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol() - 1 );
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+bool wxGrid::MoveCursorRight( bool expandSelection )
+{
+ if ( m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetCol() < m_numCols - 1 )
+ {
+ if ( expandSelection )
+ {
+ if ( m_selectingKeyboard == wxGridNoCellCoords )
+ m_selectingKeyboard = m_currentCellCoords;
+ m_selectingKeyboard.SetCol( m_selectingKeyboard.GetCol() + 1 );
+ MakeCellVisible( m_selectingKeyboard.GetRow(),
+ m_selectingKeyboard.GetCol() );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ MakeCellVisible( m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol() + 1 );
+ SetCurrentCell( m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol() + 1 );
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+bool wxGrid::MovePageUp()
+{
+ if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
+
+ int row = m_currentCellCoords.GetRow();
+ if ( row > 0 )
+ {
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+
+ int y = GetRowTop(row);
+ int newRow = YToRow( y - ch + 1 );
+ if ( newRow == -1 )
+ {
+ newRow = 0;
+ }
+ else if ( newRow == row )
+ {
+ newRow = row - 1;
+ }
+
+ MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
+ SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+bool wxGrid::MovePageDown()
+{
+ if ( m_currentCellCoords == wxGridNoCellCoords ) return FALSE;
+
+ int row = m_currentCellCoords.GetRow();
+ if ( row < m_numRows )
+ {
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+
+ int y = GetRowTop(row);
+ int newRow = YToRow( y + ch );
+ if ( newRow == -1 )
+ {
+ newRow = m_numRows - 1;
+ }
+ else if ( newRow == row )
+ {
+ newRow = row + 1;
+ }
+
+ MakeCellVisible( newRow, m_currentCellCoords.GetCol() );
+ SetCurrentCell( newRow, m_currentCellCoords.GetCol() );
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+bool wxGrid::MoveCursorUpBlock( bool expandSelection )
+{
+ if ( m_table &&
+ m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetRow() > 0 )
+ {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ // starting in an empty cell: find the next block of
+ // non-empty cells
+ //
+ while ( row > 0 )
+ {
+ row-- ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else if ( m_table->IsEmptyCell(row-1, col) )
+ {
+ // starting at the top of a block: find the next block
+ //
+ row--;
+ while ( row > 0 )
+ {
+ row-- ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else
+ {
+ // starting within a block: find the top of the block
+ //
+ while ( row > 0 )
+ {
+ row-- ;
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ row++ ;
+ break;
+ }
+ }
+ }
+
+ MakeCellVisible( row, col );
+ if ( expandSelection )
+ {
+ m_selectingKeyboard = wxGridCellCoords( row, col );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ SetCurrentCell( row, col );
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+bool wxGrid::MoveCursorDownBlock( bool expandSelection )
+{
+ if ( m_table &&
+ m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetRow() < m_numRows-1 )
+ {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ // starting in an empty cell: find the next block of
+ // non-empty cells
+ //
+ while ( row < m_numRows-1 )
+ {
+ row++ ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else if ( m_table->IsEmptyCell(row+1, col) )
+ {
+ // starting at the bottom of a block: find the next block
+ //
+ row++;
+ while ( row < m_numRows-1 )
+ {
+ row++ ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else
+ {
+ // starting within a block: find the bottom of the block
+ //
+ while ( row < m_numRows-1 )
+ {
+ row++ ;
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ row-- ;
+ break;
+ }
+ }
+ }
+
+ MakeCellVisible( row, col );
+ if ( expandSelection )
+ {
+ m_selectingKeyboard = wxGridCellCoords( row, col );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ SetCurrentCell( row, col );
+ }
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
+{
+ if ( m_table &&
+ m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetCol() > 0 )
+ {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ // starting in an empty cell: find the next block of
+ // non-empty cells
+ //
+ while ( col > 0 )
+ {
+ col-- ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else if ( m_table->IsEmptyCell(row, col-1) )
+ {
+ // starting at the left of a block: find the next block
+ //
+ col--;
+ while ( col > 0 )
+ {
+ col-- ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else
+ {
+ // starting within a block: find the left of the block
+ //
+ while ( col > 0 )
+ {
+ col-- ;
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ col++ ;
+ break;
+ }
+ }
+ }
+
+ MakeCellVisible( row, col );
+ if ( expandSelection )
+ {
+ m_selectingKeyboard = wxGridCellCoords( row, col );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ SetCurrentCell( row, col );
+ }
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+bool wxGrid::MoveCursorRightBlock( bool expandSelection )
+{
+ if ( m_table &&
+ m_currentCellCoords != wxGridNoCellCoords &&
+ m_currentCellCoords.GetCol() < m_numCols-1 )
+ {
+ int row = m_currentCellCoords.GetRow();
+ int col = m_currentCellCoords.GetCol();
+
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ // starting in an empty cell: find the next block of
+ // non-empty cells
+ //
+ while ( col < m_numCols-1 )
+ {
+ col++ ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else if ( m_table->IsEmptyCell(row, col+1) )
+ {
+ // starting at the right of a block: find the next block
+ //
+ col++;
+ while ( col < m_numCols-1 )
+ {
+ col++ ;
+ if ( !(m_table->IsEmptyCell(row, col)) ) break;
+ }
+ }
+ else
+ {
+ // starting within a block: find the right of the block
+ //
+ while ( col < m_numCols-1 )
+ {
+ col++ ;
+ if ( m_table->IsEmptyCell(row, col) )
+ {
+ col-- ;
+ break;
+ }
+ }
+ }
+
+ MakeCellVisible( row, col );
+ if ( expandSelection )
+ {
+ m_selectingKeyboard = wxGridCellCoords( row, col );
+ SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+ }
+ else
+ {
+ ClearSelection();
+ SetCurrentCell( row, col );
+ }
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+
+
+//
+// ------ Label values and formatting
+//
+
+void wxGrid::GetRowLabelAlignment( int *horiz, int *vert )
+{
+ *horiz = m_rowLabelHorizAlign;
+ *vert = m_rowLabelVertAlign;
+}
+
+void wxGrid::GetColLabelAlignment( int *horiz, int *vert )
+{
+ *horiz = m_colLabelHorizAlign;
+ *vert = m_colLabelVertAlign;
+}