fixed (rare but fatal) bug in wxWindowDisabler
[wxWidgets.git] / src / generic / grid.cpp
index 5b26c552c29460a2b0e6611fdfc817273806b50e..592c3908543c5e606d5e0935e6a19f9d6c55a846 100644 (file)
 #include "wx/grid.h"
 #include "wx/generic/gridsel.h"
 
+#if defined(__WXMOTIF__)
+    #define WXUNUSED_MOTIF(identifier)  WXUNUSED(identifier)
+#else
+    #define WXUNUSED_MOTIF(identifier)  identifier
+#endif
+
+#if defined(__WXGTK__)
+    #define WXUNUSED_GTK(identifier)    WXUNUSED(identifier)
+#else
+    #define WXUNUSED_GTK(identifier)    identifier
+#endif
+
+// Required for wxIs... functions
+#include <ctype.h>
+
 // ----------------------------------------------------------------------------
 // array classes
 // ----------------------------------------------------------------------------
@@ -283,6 +298,7 @@ WX_DEFINE_ARRAY(wxGridDataTypeInfo*, wxGridDataTypeInfoArray);
 class WXDLLEXPORT wxGridTypeRegistry
 {
 public:
+  wxGridTypeRegistry() {}
     ~wxGridTypeRegistry();
 
     void RegisterDataType(const wxString& typeName,
@@ -584,7 +600,7 @@ void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
     if ( !event.AltDown() && !event.MetaDown() && !event.ControlDown() )
     {
         // insert the key in the control
-        long keycode = event.KeyCode();
+        int keycode = (int)event.KeyCode();
         if ( isprint(keycode) )
         {
             // FIXME this is not going to work for non letters...
@@ -603,11 +619,12 @@ void wxGridCellTextEditor::StartingKey(wxKeyEvent& event)
     event.Skip();
 }
 
-void wxGridCellTextEditor::HandleReturn(wxKeyEvent& event)
+void wxGridCellTextEditor::HandleReturn( wxKeyEvent&
+                                         WXUNUSED_GTK(WXUNUSED_MOTIF(event)) )
 {
 #if defined(__WXMOTIF__) || defined(__WXGTK__)
     // wxMotif needs a little extra help...
-    int pos = Text()->GetInsertionPoint();
+    long pos = Text()->GetInsertionPoint();
     wxString s( Text()->GetValue() );
     s = s.Left(pos) + "\n" + s.Mid(pos);
     Text()->SetValue(s);
@@ -696,7 +713,7 @@ void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid)
 
     if ( HasRange() )
     {
-        Spin()->SetValue(m_valueOld);
+        Spin()->SetValue((int)m_valueOld);
     }
     else
     {
@@ -735,7 +752,7 @@ void wxGridCellNumberEditor::Reset()
 {
     if ( HasRange() )
     {
-        Spin()->SetValue(m_valueOld);
+        Spin()->SetValue((int)m_valueOld);
     }
     else
     {
@@ -747,7 +764,7 @@ void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event)
 {
     if ( !HasRange() )
     {
-        long keycode = event.KeyCode();
+        int keycode = (int) event.KeyCode();
         if ( isdigit(keycode) || keycode == '+' || keycode == '-' )
         {
             wxGridCellTextEditor::StartingKey(event);
@@ -851,7 +868,7 @@ void wxGridCellFloatEditor::Reset()
 
 void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
 {
-    long keycode = event.KeyCode();
+    int keycode = (int)event.KeyCode();
     if ( isdigit(keycode) ||
          keycode == '+' || keycode == '-' || keycode == '.' )
     {
@@ -1170,7 +1187,7 @@ void wxGridCellRenderer::Draw(wxGrid& grid,
                               wxGridCellAttr& attr,
                               wxDC& dc,
                               const wxRect& rect,
-                              int row, int col,
+                              int WXUNUSED(row), int WXUNUSED(col),
                               bool isSelected)
 {
     dc.SetBackgroundMode( wxSOLID );
@@ -2246,7 +2263,8 @@ void wxGridTableBase::UpdateAttrCols( size_t pos, int numCols )
     }
 }
 
-bool wxGridTableBase::InsertRows( size_t pos, size_t numRows )
+bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos),
+                                  size_t WXUNUSED(numRows) )
 {
     wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
                     "but your derived table class does not override this function") );
@@ -2254,7 +2272,7 @@ bool wxGridTableBase::InsertRows( size_t pos, size_t numRows )
     return FALSE;
 }
 
-bool wxGridTableBase::AppendRows( size_t numRows )
+bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) )
 {
     wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
                     "but your derived table class does not override this function"));
@@ -2262,7 +2280,8 @@ bool wxGridTableBase::AppendRows( size_t numRows )
     return FALSE;
 }
 
-bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows )
+bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos),
+                                  size_t WXUNUSED(numRows) )
 {
     wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
                     "but your derived table class does not override this function"));
@@ -2270,7 +2289,8 @@ bool wxGridTableBase::DeleteRows( size_t pos, size_t numRows )
     return FALSE;
 }
 
-bool wxGridTableBase::InsertCols( size_t pos, size_t numCols )
+bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos),
+                                  size_t WXUNUSED(numCols) )
 {
     wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
                   "but your derived table class does not override this function"));
@@ -2278,7 +2298,7 @@ bool wxGridTableBase::InsertCols( size_t pos, size_t numCols )
     return FALSE;
 }
 
-bool wxGridTableBase::AppendCols( size_t numCols )
+bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) )
 {
     wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
                    "but your derived table class does not override this function"));
@@ -2286,7 +2306,8 @@ bool wxGridTableBase::AppendCols( size_t numCols )
     return FALSE;
 }
 
-bool wxGridTableBase::DeleteCols( size_t pos, size_t numCols )
+bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos),
+                                  size_t WXUNUSED(numCols) )
 {
     wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
                     "but your derived table class does not override this function"));
@@ -2453,12 +2474,12 @@ wxGridStringTable::~wxGridStringTable()
 {
 }
 
-long wxGridStringTable::GetNumberRows()
+int wxGridStringTable::GetNumberRows()
 {
     return m_data.GetCount();
 }
 
-long wxGridStringTable::GetNumberCols()
+int wxGridStringTable::GetNumberCols()
 {
     if ( m_data.GetCount() > 0 )
         return m_data[0].GetCount();
@@ -2824,7 +2845,7 @@ wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent,
     m_owner = parent;
 }
 
-void wxGridRowLabelWindow::OnPaint( wxPaintEvent &event )
+void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
 {
     wxPaintDC dc(this);
 
@@ -2877,7 +2898,7 @@ wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent,
     m_owner = parent;
 }
 
-void wxGridColLabelWindow::OnPaint( wxPaintEvent &event )
+void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) )
 {
     wxPaintDC dc(this);
 
@@ -3031,7 +3052,7 @@ void wxGridWindow::OnKeyDown( wxKeyEvent& event )
 }
 
 
-void wxGridWindow::OnEraseBackground(wxEraseEvent& event)
+void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
 {
 }
 
@@ -3190,7 +3211,7 @@ bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
         // View at runtime.  Is there anything in the implmentation that would
         // prevent this?
 
-        // At least, you now have to copy with m_selection
+        // At least, you now have to cope with m_selection
         wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
         return FALSE;
     }
@@ -3800,10 +3821,13 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
                 break;
 
                 case WXGRID_CURSOR_SELECT_ROW:
-                    if ( (row = YToRow( y )) >= 0  &&
-                         !IsInSelection( row, 0 ) )
+                    if ( (row = YToRow( y )) >= 0 )
                     {
-                        SelectRow( row, TRUE );
+                        m_selection->SelectRow( row,
+                                                event.ControlDown(),
+                                                event.ShiftDown(),
+                                                event.AltDown(),
+                                                event.MetaDown() );
                     }
 
                 // default label to suppress warnings about "enumeration value
@@ -3840,7 +3864,23 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
             if ( row >= 0  &&
                  !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, row, -1, event ) )
             {
-                SelectRow( row, event.ShiftDown() );
+                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);
             }
         }
@@ -3967,10 +4007,13 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
                 break;
 
                 case WXGRID_CURSOR_SELECT_COL:
-                    if ( (col = XToCol( x )) >= 0  &&
-                         !IsInSelection( 0, col ) )
+                    if ( (col = XToCol( x )) >= 0 )
                     {
-                        SelectCol( col, TRUE );
+                        m_selection->SelectCol( col,
+                                                event.ControlDown(),
+                                                event.ShiftDown(),
+                                                event.AltDown(),
+                                                event.MetaDown() );
                     }
 
                 // default label to suppress warnings about "enumeration value
@@ -4007,7 +4050,22 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
             if ( col >= 0  &&
                  !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK, -1, col, event ) )
             {
-                SelectCol( col, event.ShiftDown() );
+                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);
             }
         }
@@ -4243,20 +4301,29 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
 
             if ( coords != wxGridNoCellCoords )
             {
-                if ( !IsSelection() )
+                if ( event.ControlDown() )
                 {
-                    SelectBlock( coords, coords );
+                    if ( m_selectingKeyboard == wxGridNoCellCoords)
+                        m_selectingKeyboard = coords;
+                    SelectBlock ( m_selectingKeyboard, coords );
                 }
                 else
                 {
-                    SelectBlock( m_currentCellCoords, coords );
+                    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.
+                    // scrolling is way to fast, at least on MSW - also on GTK.
                 }
             }
         }
@@ -4326,7 +4393,11 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
             m_selection->SelectBlock( m_currentCellCoords.GetRow(),
                                       m_currentCellCoords.GetCol(),
                                       coords.GetRow(),
-                                      coords.GetCol() );
+                                      coords.GetCol(),
+                                      event.ControlDown(),
+                                      event.ShiftDown(),
+                                      event.AltDown(),
+                                      event.MetaDown() );
         }
         else if ( XToEdgeOfCol(x) < 0  &&
                   YToEdgeOfRow(y) < 0 )
@@ -4359,11 +4430,20 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
                 }
                 else
                 {
-                    m_selection->ToggleCellSelection( coords.GetRow(),
-                                                      coords.GetCol() );
-                    m_selectingTopLeft = wxGridNoCellCoords;
-                    m_selectingBottomRight = wxGridNoCellCoords;
-                    SetCurrentCell( coords );
+                    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;
                 }
             }
@@ -4405,7 +4485,10 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
                                           m_selectingTopLeft.GetCol(),
                                           m_selectingBottomRight.GetRow(),
                                           m_selectingBottomRight.GetCol(),
-                                          & event );
+                                          event.ControlDown(),
+                                          event.ShiftDown(),
+                                          event.AltDown(),
+                                          event.MetaDown() );
                 m_selectingTopLeft = wxGridNoCellCoords;
                 m_selectingBottomRight = wxGridNoCellCoords;
             }
@@ -4921,8 +5004,8 @@ bool wxGrid::SendEvent( const wxEventType type,
                              type,
                              this,
                              row, col,
-                             TRUE,
                              mouseEv.GetX(), mouseEv.GetY(),
+                             FALSE,
                              mouseEv.ControlDown(),
                              mouseEv.ShiftDown(),
                              mouseEv.AltDown(),
@@ -4972,7 +5055,7 @@ void wxGrid::OnPaint( wxPaintEvent& WXUNUSED(event) )
 // the grid view is resized... then the size event is skipped to allow
 // the box sizers to handle everything
 //
-void wxGrid::OnSize( wxSizeEvent& event )
+void wxGrid::OnSize( wxSizeEvent& WXUNUSED(event) )
 {
     CalcWindowSizes();
     CalcDimensions();
@@ -5001,6 +5084,24 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
 
         // 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:
@@ -5059,7 +5160,7 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
                 break;
 
             case WXK_ESCAPE:
-                m_selection->ClearSelection();
+                ClearSelection();
                 break;
 
             case WXK_TAB:
@@ -5101,20 +5202,23 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
                 MovePageDown();
                 break;
 
-#if 0
             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
-#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
@@ -5125,7 +5229,7 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
                 if ( !(event.AltDown() ||
                        event.MetaDown() ||
                        event.ControlDown()) &&
-                     ((isalnum(event.KeyCode()) &&
+                     ((isalnum((int)event.KeyCode()) &&
                        (event.KeyCode() < 256 && event.KeyCode() >= 0)) ||
                       event.KeyCode() == WXK_F2) &&
                      !IsCellEditControlEnabled() &&
@@ -5153,7 +5257,6 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
     m_inOnKeyDown = FALSE;
 }
 
-
 void wxGrid::OnEraseBackground(wxEraseEvent&)
 {
 }
@@ -5430,7 +5533,7 @@ void wxGrid::DrawHighlight(wxDC& dc)
 // 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 & reg )
+void wxGrid::DrawAllGridLines( wxDC& dc, const wxRegion & WXUNUSED_GTK(reg) )
 {
     if ( !m_gridLinesEnabled ||
          !m_numRows ||
@@ -5660,7 +5763,7 @@ void wxGrid::DrawTextRectangle( wxDC& dc,
 
         for ( size_t i = 0;  i < lines.GetCount();  i++ )
         {
-            dc.DrawText( lines[i], (long)x, (long)y );
+            dc.DrawText( lines[i], (int)x, (int)y );
             y += lineHeight;
         }
     }
@@ -6132,18 +6235,22 @@ bool wxGrid::MoveCursorUp( bool expandSelection )
          m_currentCellCoords.GetRow() > 0 )
     {
         if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
-        
-        MakeCellVisible( m_currentCellCoords.GetRow() - 1,
-                        m_currentCellCoords.GetCol() );
-
-        SetCurrentCell( m_currentCellCoords.GetRow() - 1,
-                        m_currentCellCoords.GetCol() );
-
-        if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
+        {
+            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;
     }
 
@@ -6157,18 +6264,22 @@ bool wxGrid::MoveCursorDown( bool expandSelection )
          m_currentCellCoords.GetRow() < m_numRows-1 )
     {
         if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
-
-        MakeCellVisible( m_currentCellCoords.GetRow() + 1,
-                        m_currentCellCoords.GetCol() );
-
-        SetCurrentCell( m_currentCellCoords.GetRow() + 1,
-                        m_currentCellCoords.GetCol() );
-
-        if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
+        {
+            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;
     }
 
@@ -6182,18 +6293,22 @@ bool wxGrid::MoveCursorLeft( bool expandSelection )
          m_currentCellCoords.GetCol() > 0 )
     {
         if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
-
-        MakeCellVisible( m_currentCellCoords.GetRow(),
-                        m_currentCellCoords.GetCol() - 1 );
-
-        SetCurrentCell( m_currentCellCoords.GetRow(),
-                        m_currentCellCoords.GetCol() - 1 );
-
-        if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
+        {
+            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;
     }
 
@@ -6207,18 +6322,22 @@ bool wxGrid::MoveCursorRight( bool expandSelection )
          m_currentCellCoords.GetCol() < m_numCols - 1 )
     {
         if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
-
-        MakeCellVisible( m_currentCellCoords.GetRow(),
-                        m_currentCellCoords.GetCol() + 1 );
-
-        SetCurrentCell( m_currentCellCoords.GetRow(),
-                        m_currentCellCoords.GetCol() + 1 );
-
-        if ( expandSelection )
-            m_selection->SelectCell( m_currentCellCoords.GetRow(),
-                                     m_currentCellCoords.GetCol() );
+        {
+            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;
     }
 
@@ -6334,11 +6453,15 @@ bool wxGrid::MoveCursorUpBlock( bool expandSelection )
 
         MakeCellVisible( row, col );
         if ( expandSelection )
-            m_selection->SelectBlock( m_currentCellCoords.GetRow(),
-                                      m_currentCellCoords.GetCol(),
-                                      row, col );
-        SetCurrentCell( row, col );
-
+        {
+            m_selectingKeyboard = wxGridCellCoords( row, col );
+            SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+        }
+        else
+        {
+            ClearSelection();
+            SetCurrentCell( row, col );
+        }
         return TRUE;
     }
 
@@ -6393,10 +6516,15 @@ bool wxGrid::MoveCursorDownBlock( bool expandSelection )
 
         MakeCellVisible( row, col );
         if ( expandSelection )
-            m_selection->SelectBlock( m_currentCellCoords.GetRow(),
-                                      m_currentCellCoords.GetCol(),
-                                      row, col );
-        SetCurrentCell( row, col );
+        {
+            m_selectingKeyboard = wxGridCellCoords( row, col );
+            SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+        }
+        else
+        {
+            ClearSelection();
+            SetCurrentCell( row, col );
+        }
 
         return TRUE;
     }
@@ -6452,10 +6580,15 @@ bool wxGrid::MoveCursorLeftBlock( bool expandSelection )
 
         MakeCellVisible( row, col );
         if ( expandSelection )
-            m_selection->SelectBlock( m_currentCellCoords.GetRow(),
-                                      m_currentCellCoords.GetCol(),
-                                      row, col );
-        SetCurrentCell( row, col );
+        {
+            m_selectingKeyboard = wxGridCellCoords( row, col );
+            SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+        }
+        else
+        {
+            ClearSelection();
+            SetCurrentCell( row, col );
+        }
 
         return TRUE;
     }
@@ -6511,10 +6644,15 @@ bool wxGrid::MoveCursorRightBlock( bool expandSelection )
 
         MakeCellVisible( row, col );
         if ( expandSelection )
-            m_selection->SelectBlock( m_currentCellCoords.GetRow(),
-                                      m_currentCellCoords.GetCol(),
-                                      row, col );
-        SetCurrentCell( row, col );
+        {
+            m_selectingKeyboard = wxGridCellCoords( row, col );
+            SelectBlock( m_currentCellCoords, m_selectingKeyboard );
+        }
+        else
+        {
+            ClearSelection();
+            SetCurrentCell( row, col );
+        }
 
         return TRUE;
     }
@@ -7502,7 +7640,7 @@ void wxGrid::SetCellValue( int row, int col, const wxString& s )
 void wxGrid::SelectRow( int row, bool addToSelected )
 {
     if ( IsSelection() && !addToSelected )
-        m_selection->ClearSelection();
+        ClearSelection();
 
     m_selection->SelectRow( row );
 }
@@ -7511,7 +7649,7 @@ void wxGrid::SelectRow( int row, bool addToSelected )
 void wxGrid::SelectCol( int col, bool addToSelected )
 {
     if ( IsSelection() && !addToSelected )
-        m_selection->ClearSelection();
+        ClearSelection();
 
     m_selection->SelectCol( col );
 }