1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/gridsel.cpp
3 // Purpose: wxGridSelection
8 // Copyright: (c) Stefan Neis (Stefan.Neis@t-online.de)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "gridsel.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/generic/gridsel.h"
37 // Some explanation for the members of the class:
38 // m_cellSelection stores individual selected cells
39 // -- this is only used if m_selectionMode == wxGridSelectCells
40 // m_blockSelectionTopLeft and m_blockSelectionBottomRight
41 // store the upper left and lower right corner of selected Blocks
42 // m_rowSelection and m_colSelection store individual selected
43 // rows and columns; maybe those are superfluous and should be
46 wxGridSelection::wxGridSelection( wxGrid
* grid
,
47 wxGrid::wxGridSelectionModes sel
)
50 m_selectionMode
= sel
;
53 bool wxGridSelection::IsSelection()
55 return ( m_cellSelection
.GetCount() || m_blockSelectionTopLeft
.GetCount() ||
56 m_rowSelection
.GetCount() || m_colSelection
.GetCount() );
59 bool wxGridSelection::IsInSelection ( int row
, int col
)
63 // First check whether the given cell is individually selected
64 // (if m_selectionMode is wxGridSelectCells).
65 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
67 count
= m_cellSelection
.GetCount();
68 for ( size_t n
= 0; n
< count
; n
++ )
70 wxGridCellCoords
& coords
= m_cellSelection
[n
];
71 if ( row
== coords
.GetRow() && col
== coords
.GetCol() )
76 // Now check whether the given cell is
77 // contained in one of the selected blocks.
78 count
= m_blockSelectionTopLeft
.GetCount();
79 for ( size_t n
= 0; n
< count
; n
++ )
81 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
82 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
83 if ( BlockContainsCell(coords1
.GetRow(), coords1
.GetCol(),
84 coords2
.GetRow(), coords2
.GetCol(),
89 // Now check whether the given cell is
90 // contained in one of the selected rows
91 // (unless we are in column selection mode).
92 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
94 size_t count
= m_rowSelection
.GetCount();
95 for ( size_t n
= 0; n
< count
; n
++ )
97 if ( row
== m_rowSelection
[n
] )
102 // Now check whether the given cell is
103 // contained in one of the selected columns
104 // (unless we are in row selection mode).
105 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
107 size_t count
= m_colSelection
.GetCount();
108 for ( size_t n
= 0; n
< count
; n
++ )
110 if ( col
== m_colSelection
[n
] )
117 // Change the selection mode
118 void wxGridSelection::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
120 // if selection mode is unchanged return immediately
121 if (selmode
== m_selectionMode
)
124 if ( m_selectionMode
!= wxGrid::wxGridSelectCells
)
126 // if changing form row to column selection
127 // or vice versa, clear the selection.
128 if ( selmode
!= wxGrid::wxGridSelectCells
)
131 m_selectionMode
= selmode
;
135 // if changing from cell selection to something else,
136 // promote selected cells/blocks to whole rows/columns.
138 while ( ( n
= m_cellSelection
.GetCount() ) > 0 )
141 wxGridCellCoords
& coords
= m_cellSelection
[n
];
142 int row
= coords
.GetRow();
143 int col
= coords
.GetCol();
144 m_cellSelection
.RemoveAt(n
);
145 if (selmode
== wxGrid::wxGridSelectRows
)
147 else // selmode == wxGridSelectColumns)
151 for (n
= 0; n
< m_blockSelectionTopLeft
.GetCount(); n
++)
152 // Note that m_blockSelectionTopLeft's size may be changing!
154 wxGridCellCoords
& coords
= m_blockSelectionTopLeft
[n
];
155 int topRow
= coords
.GetRow();
156 int leftCol
= coords
.GetCol();
157 coords
= m_blockSelectionBottomRight
[n
];
158 int bottomRow
= coords
.GetRow();
159 int rightCol
= coords
.GetCol();
160 if (selmode
== wxGrid::wxGridSelectRows
)
162 if (leftCol
!= 0 || rightCol
!= m_grid
->GetNumberCols() - 1 )
164 m_blockSelectionTopLeft
.RemoveAt(n
);
165 m_blockSelectionBottomRight
.RemoveAt(n
);
166 SelectBlock( topRow
, 0,
167 bottomRow
, m_grid
->GetNumberCols() - 1,
168 false, false, false, false, false );
171 else // selmode == wxGridSelectColumns)
173 if (topRow
!= 0 || bottomRow
!= m_grid
->GetNumberRows() - 1 )
175 m_blockSelectionTopLeft
.RemoveAt(n
);
176 m_blockSelectionBottomRight
.RemoveAt(n
);
177 SelectBlock( 0, leftCol
,
178 m_grid
->GetNumberRows() - 1, rightCol
,
179 false, false, false, false, false );
183 m_selectionMode
= selmode
;
187 void wxGridSelection::SelectRow( int row
,
188 bool ControlDown
, bool ShiftDown
,
189 bool AltDown
, bool MetaDown
)
191 if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
195 // Remove single cells contained in newly selected block.
196 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
198 count
= m_cellSelection
.GetCount();
199 for ( n
= 0; n
< count
; n
++ )
201 wxGridCellCoords
& coords
= m_cellSelection
[n
];
202 if ( BlockContainsCell( row
, 0, row
, m_grid
->GetNumberCols() - 1,
203 coords
.GetRow(), coords
.GetCol() ) )
205 m_cellSelection
.RemoveAt(n
);
211 // Simplify list of selected blocks (if possible)
212 count
= m_blockSelectionTopLeft
.GetCount();
214 for ( n
= 0; n
< count
; n
++ )
216 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
217 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
219 // Remove block if it is a subset of the row
220 if ( coords1
.GetRow() == row
&& row
== coords2
.GetRow() )
222 m_blockSelectionTopLeft
.RemoveAt(n
);
223 m_blockSelectionBottomRight
.RemoveAt(n
);
226 else if ( coords1
.GetCol() == 0 &&
227 coords2
.GetCol() == m_grid
->GetNumberCols() - 1 )
229 // silently return, if row is contained in block
230 if ( coords1
.GetRow() <= row
&& row
<= coords2
.GetRow() )
232 // expand block, if it touched row
233 else if ( coords1
.GetRow() == row
+ 1)
238 else if ( coords2
.GetRow() == row
- 1)
246 // Unless we successfully handled the row,
247 // check whether row is already selected.
250 count
= m_rowSelection
.GetCount();
251 for ( n
= 0; n
< count
; n
++ )
253 if ( row
== m_rowSelection
[n
] )
257 // Add row to selection
258 m_rowSelection
.Add(row
);
262 if ( !m_grid
->GetBatchCount() )
264 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
265 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
266 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
270 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
271 wxEVT_GRID_RANGE_SELECT
,
273 wxGridCellCoords( row
, 0 ),
274 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
276 ControlDown
, ShiftDown
,
279 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
282 void wxGridSelection::SelectCol( int col
,
283 bool ControlDown
, bool ShiftDown
,
284 bool AltDown
, bool MetaDown
)
286 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
290 // Remove single cells contained in newly selected block.
291 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
293 count
= m_cellSelection
.GetCount();
294 for ( n
= 0; n
< count
; n
++ )
296 wxGridCellCoords
& coords
= m_cellSelection
[n
];
297 if ( BlockContainsCell( 0, col
, m_grid
->GetNumberRows() - 1, col
,
298 coords
.GetRow(), coords
.GetCol() ) )
300 m_cellSelection
.RemoveAt(n
);
306 // Simplify list of selected blocks (if possible)
307 count
= m_blockSelectionTopLeft
.GetCount();
309 for ( n
= 0; n
< count
; n
++ )
311 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
312 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
314 // Remove block if it is a subset of the column
315 if ( coords1
.GetCol() == col
&& col
== coords2
.GetCol() )
317 m_blockSelectionTopLeft
.RemoveAt(n
);
318 m_blockSelectionBottomRight
.RemoveAt(n
);
321 else if ( coords1
.GetRow() == 0 &&
322 coords2
.GetRow() == m_grid
->GetNumberRows() - 1 )
324 // silently return, if row is contained in block
325 if ( coords1
.GetCol() <= col
&& col
<= coords2
.GetCol() )
327 // expand block, if it touched col
328 else if ( coords1
.GetCol() == col
+ 1)
333 else if ( coords2
.GetCol() == col
- 1)
341 // Unless we successfully handled the column,
342 // Check whether col is already selected.
345 count
= m_colSelection
.GetCount();
346 for ( n
= 0; n
< count
; n
++ )
348 if ( col
== m_colSelection
[n
] )
352 // Add col to selection
353 m_colSelection
.Add(col
);
357 if ( !m_grid
->GetBatchCount() )
359 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
360 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
361 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
365 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
366 wxEVT_GRID_RANGE_SELECT
,
368 wxGridCellCoords( 0, col
),
369 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
371 ControlDown
, ShiftDown
,
374 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
377 void wxGridSelection::SelectBlock( int topRow
, int leftCol
,
378 int bottomRow
, int rightCol
,
379 bool ControlDown
, bool ShiftDown
,
380 bool AltDown
, bool MetaDown
,
383 // Fix the coordinates of the block if needed.
384 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
387 rightCol
= m_grid
->GetNumberCols() - 1;
389 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
392 bottomRow
= m_grid
->GetNumberRows() - 1;
394 if ( topRow
> bottomRow
)
401 if ( leftCol
> rightCol
)
408 // Handle single cell selection in SelectCell.
409 // (MB: added check for selection mode here to prevent
410 // crashes if, for example, we are select rows and the
411 // grid only has 1 col)
412 if ( m_selectionMode
== wxGrid::wxGridSelectCells
&&
413 topRow
== bottomRow
&& leftCol
== rightCol
)
414 SelectCell( topRow
, leftCol
, ControlDown
, ShiftDown
,
415 AltDown
, MetaDown
, sendEvent
);
418 // Remove single cells contained in newly selected block.
419 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
421 count
= m_cellSelection
.GetCount();
422 for ( n
= 0; n
< count
; n
++ )
424 wxGridCellCoords
& coords
= m_cellSelection
[n
];
425 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
426 coords
.GetRow(), coords
.GetCol() ) )
428 m_cellSelection
.RemoveAt(n
);
434 // If a block containing the selection is already selected, return,
435 // if a block contained in the selection is found, remove it.
437 count
= m_blockSelectionTopLeft
.GetCount();
438 for ( n
= 0; n
< count
; n
++ )
440 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
441 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
442 switch ( BlockContain( coords1
.GetRow(), coords1
.GetCol(),
443 coords2
.GetRow(), coords2
.GetCol(),
444 topRow
, leftCol
, bottomRow
, rightCol
) )
450 m_blockSelectionTopLeft
.RemoveAt(n
);
451 m_blockSelectionBottomRight
.RemoveAt(n
);
460 // If a row containing the selection is already selected, return,
461 // if a row contained in newly selected block is found, remove it.
462 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
464 count
= m_rowSelection
.GetCount();
465 for ( n
= 0; n
< count
; n
++ )
467 switch ( BlockContain( m_rowSelection
[n
], 0,
468 m_rowSelection
[n
], m_grid
->GetNumberCols()-1,
469 topRow
, leftCol
, bottomRow
, rightCol
) )
475 m_rowSelection
.RemoveAt(n
);
484 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
486 count
= m_colSelection
.GetCount();
487 for ( n
= 0; n
< count
; n
++ )
489 switch ( BlockContain( 0, m_colSelection
[n
],
490 m_grid
->GetNumberRows()-1, m_colSelection
[n
],
491 topRow
, leftCol
, bottomRow
, rightCol
) )
497 m_colSelection
.RemoveAt(n
);
506 m_blockSelectionTopLeft
.Add( wxGridCellCoords( topRow
, leftCol
) );
507 m_blockSelectionBottomRight
.Add( wxGridCellCoords( bottomRow
, rightCol
) );
510 if ( !m_grid
->GetBatchCount() )
512 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( topRow
, leftCol
),
513 wxGridCellCoords( bottomRow
, rightCol
) );
514 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
517 // Send Event, if not disabled.
520 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
521 wxEVT_GRID_RANGE_SELECT
,
523 wxGridCellCoords( topRow
, leftCol
),
524 wxGridCellCoords( bottomRow
, rightCol
),
526 ControlDown
, ShiftDown
,
528 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
532 void wxGridSelection::SelectCell( int row
, int col
,
533 bool ControlDown
, bool ShiftDown
,
534 bool AltDown
, bool MetaDown
,
537 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
539 SelectBlock(row
, 0, row
, m_grid
->GetNumberCols() - 1,
540 ControlDown
, ShiftDown
, AltDown
, MetaDown
, sendEvent
);
543 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
545 SelectBlock(0, col
, m_grid
->GetNumberRows() - 1, col
,
546 ControlDown
, ShiftDown
, AltDown
, MetaDown
, sendEvent
);
549 else if ( IsInSelection ( row
, col
) )
551 m_cellSelection
.Add( wxGridCellCoords( row
, col
) );
554 if ( !m_grid
->GetBatchCount() )
556 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, col
),
557 wxGridCellCoords( row
, col
) );
558 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
564 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
565 wxEVT_GRID_RANGE_SELECT
,
567 wxGridCellCoords( row
, col
),
568 wxGridCellCoords( row
, col
),
570 ControlDown
, ShiftDown
,
572 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
576 void wxGridSelection::ToggleCellSelection( int row
, int col
,
577 bool ControlDown
, bool ShiftDown
,
578 bool AltDown
, bool MetaDown
)
580 // if the cell is not selected, select it
581 if ( !IsInSelection ( row
, col
) )
583 SelectCell( row
, col
, ControlDown
, ShiftDown
,
588 // otherwise deselect it. This can be simple or more or
589 // less difficult, depending on how the cell is selected.
592 // The simplest case: The cell is contained in m_cellSelection
593 // Then it can't be contained in rows/cols/block (since those
594 // would remove the cell from m_cellSelection on creation), so
595 // we just have to remove it from m_cellSelection.
597 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
599 count
= m_cellSelection
.GetCount();
600 for ( n
= 0; n
< count
; n
++ )
602 wxGridCellCoords
& coords
= m_cellSelection
[n
];
603 if ( row
== coords
.GetRow() && col
== coords
.GetCol() )
605 wxGridCellCoords coords
= m_cellSelection
[n
];
606 m_cellSelection
.RemoveAt(n
);
607 if ( !m_grid
->GetBatchCount() )
609 wxRect r
= m_grid
->BlockToDeviceRect( coords
, coords
);
610 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
614 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
615 wxEVT_GRID_RANGE_SELECT
,
617 wxGridCellCoords( row
, col
),
618 wxGridCellCoords( row
, col
),
620 ControlDown
, ShiftDown
,
622 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
628 // The most difficult case: The cell is member of one or even several
629 // blocks. Split each such block in up to 4 new parts, that don't
630 // contain the cell to be selected, like this:
631 // |---------------------------|
635 // |---------------------------|
636 // | part 3 |x| part 4 |
637 // |---------------------------|
641 // |---------------------------|
642 // (The x marks the newly deselected cell).
643 // Note: in row selection mode, we only need part1 and part2;
644 // in column selection mode, we only need part 3 and part4,
645 // which are expanded to whole columns automatically!
647 count
= m_blockSelectionTopLeft
.GetCount();
648 for ( n
= 0; n
< count
; n
++ )
650 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
651 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
652 int topRow
= coords1
.GetRow();
653 int leftCol
= coords1
.GetCol();
654 int bottomRow
= coords2
.GetRow();
655 int rightCol
= coords2
.GetCol();
656 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
660 m_blockSelectionTopLeft
.RemoveAt(n
);
661 m_blockSelectionBottomRight
.RemoveAt(n
);
663 // add up to 4 smaller blocks and set update region
664 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
667 SelectBlock( topRow
, leftCol
, row
- 1, rightCol
,
668 false, false, false, false, false );
669 if ( bottomRow
> row
)
670 SelectBlock( row
+ 1, leftCol
, bottomRow
, rightCol
,
671 false, false, false, false, false );
673 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
676 SelectBlock( row
, leftCol
, row
, col
- 1,
677 false, false, false, false, false );
678 if ( rightCol
> col
)
679 SelectBlock( row
, col
+ 1, row
, rightCol
,
680 false, false, false, false, false );
685 // remove a cell from a row, adding up to two new blocks
686 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
688 count
= m_rowSelection
.GetCount();
689 for ( n
= 0; n
< count
; n
++ )
691 if ( m_rowSelection
[n
] == row
)
693 m_rowSelection
.RemoveAt(n
);
695 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
698 SelectBlock( row
, 0, row
, col
- 1,
699 false, false, false, false, false );
700 if ( col
< m_grid
->GetNumberCols() - 1 )
701 SelectBlock( row
, col
+ 1,
702 row
, m_grid
->GetNumberCols() - 1,
703 false, false, false, false, false );
709 // remove a cell from a column, adding up to two new blocks
710 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
712 count
= m_colSelection
.GetCount();
713 for ( n
= 0; n
< count
; n
++ )
715 if ( m_colSelection
[n
] == col
)
717 m_colSelection
.RemoveAt(n
);
719 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
722 SelectBlock( 0, col
, row
- 1, col
,
723 false, false, false, false, false );
724 if ( row
< m_grid
->GetNumberRows() - 1 )
725 SelectBlock( row
+ 1, col
,
726 m_grid
->GetNumberRows() - 1, col
,
727 false, false, false, false, false );
733 // Refresh the screen and send the event; according to m_selectionMode,
734 // we need to either update only the cell, or the whole row/column.
736 switch (m_selectionMode
)
738 case wxGrid::wxGridSelectCells
:
740 if ( !m_grid
->GetBatchCount() )
742 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, col
),
743 wxGridCellCoords( row
, col
) );
744 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
747 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
748 wxEVT_GRID_RANGE_SELECT
,
750 wxGridCellCoords( row
, col
),
751 wxGridCellCoords( row
, col
),
753 ControlDown
, ShiftDown
,
755 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
758 case wxGrid::wxGridSelectRows
:
760 if ( !m_grid
->GetBatchCount() )
762 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
763 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
764 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
767 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
768 wxEVT_GRID_RANGE_SELECT
,
770 wxGridCellCoords( row
, 0 ),
771 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
773 ControlDown
, ShiftDown
,
775 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
778 case wxGrid::wxGridSelectColumns
:
780 if ( !m_grid
->GetBatchCount() )
782 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
783 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
784 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
787 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
788 wxEVT_GRID_RANGE_SELECT
,
790 wxGridCellCoords( 0, col
),
791 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
793 ControlDown
, ShiftDown
,
795 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
801 void wxGridSelection::ClearSelection()
805 wxGridCellCoords coords1
, coords2
;
807 // deselect all invidiual cells and update the screen
808 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
810 while( ( n
= m_cellSelection
.GetCount() ) > 0)
813 coords1
= m_cellSelection
[n
];
814 m_cellSelection
.RemoveAt(n
);
815 if ( !m_grid
->GetBatchCount() )
817 r
= m_grid
->BlockToDeviceRect( coords1
, coords1
);
818 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
820 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
826 // deselect all blocks and update the screen
827 while( ( n
= m_blockSelectionTopLeft
.GetCount() ) > 0)
830 coords1
= m_blockSelectionTopLeft
[n
];
831 coords2
= m_blockSelectionBottomRight
[n
];
832 m_blockSelectionTopLeft
.RemoveAt(n
);
833 m_blockSelectionBottomRight
.RemoveAt(n
);
834 if ( !m_grid
->GetBatchCount() )
836 r
= m_grid
->BlockToDeviceRect( coords1
, coords2
);
837 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
839 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
844 // deselect all rows and update the screen
845 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
847 while( ( n
= m_rowSelection
.GetCount() ) > 0)
850 int row
= m_rowSelection
[n
];
851 m_rowSelection
.RemoveAt(n
);
852 if ( !m_grid
->GetBatchCount() )
854 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
855 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
856 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
858 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
864 // deselect all columns and update the screen
865 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
867 while( ( n
= m_colSelection
.GetCount() ) > 0)
870 int col
= m_colSelection
[n
];
871 m_colSelection
.RemoveAt(n
);
872 if ( !m_grid
->GetBatchCount() )
874 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
875 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
876 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
878 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
884 // One deselection event, indicating deselection of _all_ cells.
885 // (No finer grained events for each of the smaller regions
886 // deselected above!)
887 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
888 wxEVT_GRID_RANGE_SELECT
,
890 wxGridCellCoords( 0, 0 ),
891 wxGridCellCoords( m_grid
->GetNumberRows() - 1,
892 m_grid
->GetNumberCols() - 1 ),
895 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
899 void wxGridSelection::UpdateRows( size_t pos
, int numRows
)
901 size_t count
= m_cellSelection
.GetCount();
903 for ( n
= 0; n
< count
; n
++ )
905 wxGridCellCoords
& coords
= m_cellSelection
[n
];
906 wxCoord row
= coords
.GetRow();
907 if ((size_t)row
>= pos
)
911 // If rows inserted, increase row counter where necessary
912 coords
.SetRow(row
+ numRows
);
914 else if (numRows
< 0)
916 // If rows deleted ...
917 if ((size_t)row
>= pos
- numRows
)
919 // ...either decrement row counter (if row still exists)...
920 coords
.SetRow(row
+ numRows
);
924 // ...or remove the attribute
925 m_cellSelection
.RemoveAt(n
);
932 count
= m_blockSelectionTopLeft
.GetCount();
933 for ( n
= 0; n
< count
; n
++ )
935 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
936 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
937 wxCoord row1
= coords1
.GetRow();
938 wxCoord row2
= coords2
.GetRow();
939 if ((size_t)row2
>= pos
)
943 // If rows inserted, increase row counter where necessary
944 coords2
.SetRow(row2
+ numRows
);
945 if ( (size_t)row1
>= pos
)
946 coords1
.SetRow(row1
+ numRows
);
948 else if (numRows
< 0)
950 // If rows deleted ...
951 if ((size_t)row2
>= pos
- numRows
)
953 // ...either decrement row counter (if row still exists)...
954 coords2
.SetRow(row2
+ numRows
);
955 if ( (size_t) row1
>= pos
)
956 coords1
.SetRow( wxMax(row1
+ numRows
, (int) pos
) );
961 if ( (size_t) row1
>= pos
)
963 // ...or remove the attribute
964 m_blockSelectionTopLeft
.RemoveAt(n
);
965 m_blockSelectionBottomRight
.RemoveAt(n
);
975 count
= m_rowSelection
.GetCount();
976 for ( n
= 0; n
< count
; n
++ )
978 int rowOrCol_
= m_rowSelection
[ n
];
980 if ( ( size_t ) rowOrCol_
>= pos
)
984 m_rowSelection
[ n
] += numRows
;
986 else if ( numRows
< 0 )
988 if ( ( size_t ) rowOrCol_
>= ( pos
- numRows
) )
989 m_rowSelection
[ n
] += numRows
;
992 m_rowSelection
.RemoveAt ( n
);
999 // No need to touch selected columns, unless we removed _all_
1000 // rows, in this case, we remove all columns from the selection.
1002 if ( !m_grid
->GetNumberRows() )
1003 m_colSelection
.Clear();
1007 void wxGridSelection::UpdateCols( size_t pos
, int numCols
)
1009 size_t count
= m_cellSelection
.GetCount();
1011 for ( n
= 0; n
< count
; n
++ )
1013 wxGridCellCoords
& coords
= m_cellSelection
[n
];
1014 wxCoord col
= coords
.GetCol();
1015 if ((size_t)col
>= pos
)
1019 // If rows inserted, increase row counter where necessary
1020 coords
.SetCol(col
+ numCols
);
1022 else if (numCols
< 0)
1024 // If rows deleted ...
1025 if ((size_t)col
>= pos
- numCols
)
1027 // ...either decrement row counter (if row still exists)...
1028 coords
.SetCol(col
+ numCols
);
1032 // ...or remove the attribute
1033 m_cellSelection
.RemoveAt(n
);
1040 count
= m_blockSelectionTopLeft
.GetCount();
1041 for ( n
= 0; n
< count
; n
++ )
1043 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
1044 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
1045 wxCoord col1
= coords1
.GetCol();
1046 wxCoord col2
= coords2
.GetCol();
1047 if ((size_t)col2
>= pos
)
1051 // If rows inserted, increase row counter where necessary
1052 coords2
.SetCol(col2
+ numCols
);
1053 if ( (size_t)col1
>= pos
)
1054 coords1
.SetCol(col1
+ numCols
);
1056 else if (numCols
< 0)
1058 // If cols deleted ...
1059 if ((size_t)col2
>= pos
- numCols
)
1061 // ...either decrement col counter (if col still exists)...
1062 coords2
.SetCol(col2
+ numCols
);
1063 if ( (size_t) col1
>= pos
)
1064 coords1
.SetCol( wxMax(col1
+ numCols
, (int) pos
) );
1069 if ( (size_t) col1
>= pos
)
1071 // ...or remove the attribute
1072 m_blockSelectionTopLeft
.RemoveAt(n
);
1073 m_blockSelectionBottomRight
.RemoveAt(n
);
1077 coords2
.SetCol(pos
);
1083 count
= m_colSelection
.GetCount();
1084 for ( n
= 0; n
< count
; n
++ )
1087 int rowOrCol
= m_colSelection
[ n
];
1088 if ( ( size_t ) rowOrCol
>= pos
)
1091 m_colSelection
[ n
] += numCols
;
1092 else if ( numCols
< 0 )
1094 if ( ( size_t ) rowOrCol
>= ( pos
-numCols
) )
1095 m_colSelection
[ n
] += numCols
;
1098 m_colSelection
.RemoveAt ( n
);
1107 // No need to touch selected rows, unless we removed _all_
1108 // columns, in this case, we remove all rows from the selection.
1109 if ( !m_grid
->GetNumberCols() )
1110 m_rowSelection
.Clear();
1114 int wxGridSelection::BlockContain( int topRow1
, int leftCol1
,
1115 int bottomRow1
, int rightCol1
,
1116 int topRow2
, int leftCol2
,
1117 int bottomRow2
, int rightCol2
)
1118 // returns 1, if Block1 contains Block2,
1119 // -1, if Block2 contains Block1,
1122 if ( topRow1
<= topRow2
&& bottomRow2
<= bottomRow1
&&
1123 leftCol1
<= leftCol2
&& rightCol2
<= rightCol1
)
1125 else if ( topRow2
<= topRow1
&& bottomRow1
<= bottomRow2
&&
1126 leftCol2
<= leftCol1
&& rightCol1
<= rightCol2
)