1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/gridsel.cpp
3 // Purpose: wxGridSelection
8 // Copyright: (c) Stefan Neis (Stefan.Neis@t-online.de)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 // ============================================================================
21 // ============================================================================
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
29 #include "wx/generic/gridsel.h"
32 // Some explanation for the members of the class:
33 // m_cellSelection stores individual selected cells
34 // -- this is only used if m_selectionMode == wxGridSelectCells
35 // m_blockSelectionTopLeft and m_blockSelectionBottomRight
36 // store the upper left and lower right corner of selected Blocks
37 // m_rowSelection and m_colSelection store individual selected
38 // rows and columns; maybe those are superfluous and should be
41 wxGridSelection::wxGridSelection( wxGrid
* grid
,
42 wxGrid::wxGridSelectionModes sel
)
45 m_selectionMode
= sel
;
48 bool wxGridSelection::IsSelection()
50 return ( m_cellSelection
.GetCount() || m_blockSelectionTopLeft
.GetCount() ||
51 m_rowSelection
.GetCount() || m_colSelection
.GetCount() );
54 bool wxGridSelection::IsInSelection( int row
, int col
)
58 // First check whether the given cell is individually selected
59 // (if m_selectionMode is wxGridSelectCells).
60 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
62 count
= m_cellSelection
.GetCount();
63 for ( size_t n
= 0; n
< count
; n
++ )
65 wxGridCellCoords
& coords
= m_cellSelection
[n
];
66 if ( row
== coords
.GetRow() && col
== coords
.GetCol() )
71 // Now check whether the given cell is
72 // contained in one of the selected blocks.
73 count
= m_blockSelectionTopLeft
.GetCount();
74 for ( size_t n
= 0; n
< count
; n
++ )
76 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
77 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
78 if ( BlockContainsCell(coords1
.GetRow(), coords1
.GetCol(),
79 coords2
.GetRow(), coords2
.GetCol(),
84 // Now check whether the given cell is
85 // contained in one of the selected rows
86 // (unless we are in column selection mode).
87 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
89 count
= m_rowSelection
.GetCount();
90 for ( size_t n
= 0; n
< count
; n
++ )
92 if ( row
== m_rowSelection
[n
] )
97 // Now check whether the given cell is
98 // contained in one of the selected columns
99 // (unless we are in row selection mode).
100 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
102 count
= m_colSelection
.GetCount();
103 for ( size_t n
= 0; n
< count
; n
++ )
105 if ( col
== m_colSelection
[n
] )
113 // Change the selection mode
114 void wxGridSelection::SetSelectionMode( wxGrid::wxGridSelectionModes selmode
)
116 // if selection mode is unchanged return immediately
117 if (selmode
== m_selectionMode
)
120 if ( m_selectionMode
!= wxGrid::wxGridSelectCells
)
122 // if changing form row to column selection
123 // or vice versa, clear the selection.
124 if ( selmode
!= wxGrid::wxGridSelectCells
)
127 m_selectionMode
= selmode
;
131 // if changing from cell selection to something else,
132 // promote selected cells/blocks to whole rows/columns.
134 while ( ( n
= m_cellSelection
.GetCount() ) > 0 )
137 wxGridCellCoords
& coords
= m_cellSelection
[n
];
138 int row
= coords
.GetRow();
139 int col
= coords
.GetCol();
140 m_cellSelection
.RemoveAt(n
);
141 if (selmode
== wxGrid::wxGridSelectRows
)
143 else // selmode == wxGridSelectColumns)
147 // Note that m_blockSelectionTopLeft's size may be changing!
148 for (n
= 0; n
< m_blockSelectionTopLeft
.GetCount(); n
++)
150 wxGridCellCoords
& coords
= m_blockSelectionTopLeft
[n
];
151 int topRow
= coords
.GetRow();
152 int leftCol
= coords
.GetCol();
153 coords
= m_blockSelectionBottomRight
[n
];
154 int bottomRow
= coords
.GetRow();
155 int rightCol
= coords
.GetCol();
157 if (selmode
== wxGrid::wxGridSelectRows
)
159 if (leftCol
!= 0 || rightCol
!= m_grid
->GetNumberCols() - 1 )
161 m_blockSelectionTopLeft
.RemoveAt(n
);
162 m_blockSelectionBottomRight
.RemoveAt(n
);
163 SelectBlock( topRow
, 0,
164 bottomRow
, m_grid
->GetNumberCols() - 1,
165 false, false, false, false, false );
168 else // selmode == wxGridSelectColumns)
170 if (topRow
!= 0 || bottomRow
!= m_grid
->GetNumberRows() - 1 )
172 m_blockSelectionTopLeft
.RemoveAt(n
);
173 m_blockSelectionBottomRight
.RemoveAt(n
);
174 SelectBlock( 0, leftCol
,
175 m_grid
->GetNumberRows() - 1, rightCol
,
176 false, false, false, false, false );
181 m_selectionMode
= selmode
;
185 void wxGridSelection::SelectRow( int row
,
186 bool ControlDown
, bool ShiftDown
,
187 bool AltDown
, bool MetaDown
)
189 if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
194 // Remove single cells contained in newly selected block.
195 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
197 count
= m_cellSelection
.GetCount();
198 for ( n
= 0; n
< count
; n
++ )
200 wxGridCellCoords
& coords
= m_cellSelection
[n
];
201 if ( BlockContainsCell( row
, 0, row
, m_grid
->GetNumberCols() - 1,
202 coords
.GetRow(), coords
.GetCol() ) )
204 m_cellSelection
.RemoveAt(n
);
211 // Simplify list of selected blocks (if possible)
212 count
= m_blockSelectionTopLeft
.GetCount();
215 for ( n
= 0; n
< count
; n
++ )
217 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
218 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
220 // Remove block if it is a subset of the row
221 if ( coords1
.GetRow() == row
&& row
== coords2
.GetRow() )
223 m_blockSelectionTopLeft
.RemoveAt(n
);
224 m_blockSelectionBottomRight
.RemoveAt(n
);
228 else if ( coords1
.GetCol() == 0 &&
229 coords2
.GetCol() == m_grid
->GetNumberCols() - 1 )
231 // silently return, if row is contained in block
232 if ( coords1
.GetRow() <= row
&& row
<= coords2
.GetRow() )
234 // expand block, if it touched row
235 else if ( coords1
.GetRow() == row
+ 1)
240 else if ( coords2
.GetRow() == row
- 1)
248 // Unless we successfully handled the row,
249 // check whether row is already selected.
252 count
= m_rowSelection
.GetCount();
253 for ( n
= 0; n
< count
; n
++ )
255 if ( row
== m_rowSelection
[n
] )
259 // Add row to selection
260 m_rowSelection
.Add(row
);
264 if ( !m_grid
->GetBatchCount() )
266 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
267 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
268 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
272 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
273 wxEVT_GRID_RANGE_SELECT
,
275 wxGridCellCoords( row
, 0 ),
276 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
278 ControlDown
, ShiftDown
,
281 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
284 void wxGridSelection::SelectCol( int col
,
285 bool ControlDown
, bool ShiftDown
,
286 bool AltDown
, bool MetaDown
)
288 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
292 // Remove single cells contained in newly selected block.
293 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
295 count
= m_cellSelection
.GetCount();
296 for ( n
= 0; n
< count
; n
++ )
298 wxGridCellCoords
& coords
= m_cellSelection
[n
];
299 if ( BlockContainsCell( 0, col
, m_grid
->GetNumberRows() - 1, col
,
300 coords
.GetRow(), coords
.GetCol() ) )
302 m_cellSelection
.RemoveAt(n
);
309 // Simplify list of selected blocks (if possible)
310 count
= m_blockSelectionTopLeft
.GetCount();
312 for ( n
= 0; n
< count
; n
++ )
314 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
315 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
317 // Remove block if it is a subset of the column
318 if ( coords1
.GetCol() == col
&& col
== coords2
.GetCol() )
320 m_blockSelectionTopLeft
.RemoveAt(n
);
321 m_blockSelectionBottomRight
.RemoveAt(n
);
325 else if ( coords1
.GetRow() == 0 &&
326 coords2
.GetRow() == m_grid
->GetNumberRows() - 1 )
328 // silently return, if row is contained in block
329 if ( coords1
.GetCol() <= col
&& col
<= coords2
.GetCol() )
331 // expand block, if it touched col
332 else if ( coords1
.GetCol() == col
+ 1)
337 else if ( coords2
.GetCol() == col
- 1)
345 // Unless we successfully handled the column,
346 // Check whether col is already selected.
349 count
= m_colSelection
.GetCount();
350 for ( n
= 0; n
< count
; n
++ )
352 if ( col
== m_colSelection
[n
] )
356 // Add col to selection
357 m_colSelection
.Add(col
);
361 if ( !m_grid
->GetBatchCount() )
363 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
364 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
365 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
369 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
370 wxEVT_GRID_RANGE_SELECT
,
372 wxGridCellCoords( 0, col
),
373 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
375 ControlDown
, ShiftDown
,
378 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
381 void wxGridSelection::SelectBlock( int topRow
, int leftCol
,
382 int bottomRow
, int rightCol
,
383 bool ControlDown
, bool ShiftDown
,
384 bool AltDown
, bool MetaDown
,
387 // Fix the coordinates of the block if needed.
388 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
391 rightCol
= m_grid
->GetNumberCols() - 1;
393 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
396 bottomRow
= m_grid
->GetNumberRows() - 1;
399 if ( topRow
> bottomRow
)
406 if ( leftCol
> rightCol
)
413 // Handle single cell selection in SelectCell.
414 // (MB: added check for selection mode here to prevent
415 // crashes if, for example, we are select rows and the
416 // grid only has 1 col)
417 if ( m_selectionMode
== wxGrid::wxGridSelectCells
&&
418 topRow
== bottomRow
&& leftCol
== rightCol
)
420 SelectCell( topRow
, leftCol
, ControlDown
, ShiftDown
,
421 AltDown
, MetaDown
, sendEvent
);
426 // Remove single cells contained in newly selected block.
427 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
429 count
= m_cellSelection
.GetCount();
430 for ( n
= 0; n
< count
; n
++ )
432 wxGridCellCoords
& coords
= m_cellSelection
[n
];
433 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
434 coords
.GetRow(), coords
.GetCol() ) )
436 m_cellSelection
.RemoveAt(n
);
443 // If a block containing the selection is already selected, return,
444 // if a block contained in the selection is found, remove it.
446 count
= m_blockSelectionTopLeft
.GetCount();
447 for ( n
= 0; n
< count
; n
++ )
449 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
450 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
452 switch ( BlockContain( coords1
.GetRow(), coords1
.GetCol(),
453 coords2
.GetRow(), coords2
.GetCol(),
454 topRow
, leftCol
, bottomRow
, rightCol
) )
460 m_blockSelectionTopLeft
.RemoveAt(n
);
461 m_blockSelectionBottomRight
.RemoveAt(n
);
471 // If a row containing the selection is already selected, return,
472 // if a row contained in newly selected block is found, remove it.
473 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
475 count
= m_rowSelection
.GetCount();
476 for ( n
= 0; n
< count
; n
++ )
478 switch ( BlockContain( m_rowSelection
[n
], 0,
479 m_rowSelection
[n
], m_grid
->GetNumberCols() - 1,
480 topRow
, leftCol
, bottomRow
, rightCol
) )
486 m_rowSelection
.RemoveAt(n
);
497 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
499 count
= m_colSelection
.GetCount();
500 for ( n
= 0; n
< count
; n
++ )
502 switch ( BlockContain( 0, m_colSelection
[n
],
503 m_grid
->GetNumberRows() - 1, m_colSelection
[n
],
504 topRow
, leftCol
, bottomRow
, rightCol
) )
510 m_colSelection
.RemoveAt(n
);
521 m_blockSelectionTopLeft
.Add( wxGridCellCoords( topRow
, leftCol
) );
522 m_blockSelectionBottomRight
.Add( wxGridCellCoords( bottomRow
, rightCol
) );
525 if ( !m_grid
->GetBatchCount() )
527 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( topRow
, leftCol
),
528 wxGridCellCoords( bottomRow
, rightCol
) );
529 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
532 // Send Event, if not disabled.
535 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
536 wxEVT_GRID_RANGE_SELECT
,
538 wxGridCellCoords( topRow
, leftCol
),
539 wxGridCellCoords( bottomRow
, rightCol
),
541 ControlDown
, ShiftDown
,
543 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
547 void wxGridSelection::SelectCell( int row
, int col
,
548 bool ControlDown
, bool ShiftDown
,
549 bool AltDown
, bool MetaDown
,
552 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
554 SelectBlock(row
, 0, row
, m_grid
->GetNumberCols() - 1,
555 ControlDown
, ShiftDown
, AltDown
, MetaDown
, sendEvent
);
559 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
561 SelectBlock(0, col
, m_grid
->GetNumberRows() - 1, col
,
562 ControlDown
, ShiftDown
, AltDown
, MetaDown
, sendEvent
);
566 else if ( IsInSelection ( row
, col
) )
569 m_cellSelection
.Add( wxGridCellCoords( row
, col
) );
572 if ( !m_grid
->GetBatchCount() )
574 wxRect r
= m_grid
->BlockToDeviceRect(
575 wxGridCellCoords( row
, col
),
576 wxGridCellCoords( row
, col
) );
577 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
583 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
584 wxEVT_GRID_RANGE_SELECT
,
586 wxGridCellCoords( row
, col
),
587 wxGridCellCoords( row
, col
),
589 ControlDown
, ShiftDown
,
591 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
595 void wxGridSelection::ToggleCellSelection( int row
, int col
,
596 bool ControlDown
, bool ShiftDown
,
597 bool AltDown
, bool MetaDown
)
599 // if the cell is not selected, select it
600 if ( !IsInSelection ( row
, col
) )
602 SelectCell( row
, col
, ControlDown
, ShiftDown
, AltDown
, MetaDown
);
607 // otherwise deselect it. This can be simple or more or
608 // less difficult, depending on how the cell is selected.
611 // The simplest case: The cell is contained in m_cellSelection
612 // Then it can't be contained in rows/cols/block (since those
613 // would remove the cell from m_cellSelection on creation), so
614 // we just have to remove it from m_cellSelection.
616 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
618 count
= m_cellSelection
.GetCount();
619 for ( n
= 0; n
< count
; n
++ )
621 const wxGridCellCoords
& sel
= m_cellSelection
[n
];
622 if ( row
== sel
.GetRow() && col
== sel
.GetCol() )
624 wxGridCellCoords coords
= m_cellSelection
[n
];
625 m_cellSelection
.RemoveAt(n
);
626 if ( !m_grid
->GetBatchCount() )
628 wxRect r
= m_grid
->BlockToDeviceRect( coords
, coords
);
629 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
633 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
634 wxEVT_GRID_RANGE_SELECT
,
636 wxGridCellCoords( row
, col
),
637 wxGridCellCoords( row
, col
),
639 ControlDown
, ShiftDown
,
641 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
648 // The most difficult case: The cell is member of one or even several
649 // blocks. Split each such block in up to 4 new parts, that don't
650 // contain the cell to be selected, like this:
651 // |---------------------------|
655 // |---------------------------|
656 // | part 3 |x| part 4 |
657 // |---------------------------|
661 // |---------------------------|
662 // (The x marks the newly deselected cell).
663 // Note: in row selection mode, we only need part1 and part2;
664 // in column selection mode, we only need part 3 and part4,
665 // which are expanded to whole columns automatically!
667 count
= m_blockSelectionTopLeft
.GetCount();
668 for ( n
= 0; n
< count
; n
++ )
670 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
671 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
672 int topRow
= coords1
.GetRow();
673 int leftCol
= coords1
.GetCol();
674 int bottomRow
= coords2
.GetRow();
675 int rightCol
= coords2
.GetCol();
677 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
, row
, col
) )
680 m_blockSelectionTopLeft
.RemoveAt(n
);
681 m_blockSelectionBottomRight
.RemoveAt(n
);
685 // add up to 4 smaller blocks and set update region
686 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
689 SelectBlock( topRow
, leftCol
, row
- 1, rightCol
,
690 false, false, false, false, false );
691 if ( bottomRow
> row
)
692 SelectBlock( row
+ 1, leftCol
, bottomRow
, rightCol
,
693 false, false, false, false, false );
696 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
699 SelectBlock( row
, leftCol
, row
, col
- 1,
700 false, false, false, false, false );
701 if ( rightCol
> col
)
702 SelectBlock( row
, col
+ 1, row
, rightCol
,
703 false, false, false, false, false );
708 // remove a cell from a row, adding up to two new blocks
709 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
711 count
= m_rowSelection
.GetCount();
712 for ( n
= 0; n
< count
; n
++ )
714 if ( m_rowSelection
[n
] == row
)
716 m_rowSelection
.RemoveAt(n
);
720 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
723 SelectBlock( row
, 0, row
, col
- 1,
724 false, false, false, false, false );
725 if ( col
< m_grid
->GetNumberCols() - 1 )
726 SelectBlock( row
, col
+ 1,
727 row
, m_grid
->GetNumberCols() - 1,
728 false, false, false, false, false );
734 // remove a cell from a column, adding up to two new blocks
735 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
737 count
= m_colSelection
.GetCount();
738 for ( n
= 0; n
< count
; n
++ )
740 if ( m_colSelection
[n
] == col
)
742 m_colSelection
.RemoveAt(n
);
746 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
749 SelectBlock( 0, col
, row
- 1, col
,
750 false, false, false, false, false );
751 if ( row
< m_grid
->GetNumberRows() - 1 )
752 SelectBlock( row
+ 1, col
,
753 m_grid
->GetNumberRows() - 1, col
,
754 false, false, false, false, false );
760 // Refresh the screen and send the event; according to m_selectionMode,
761 // we need to either update only the cell, or the whole row/column.
763 switch (m_selectionMode
)
765 case wxGrid::wxGridSelectCells
:
767 if ( !m_grid
->GetBatchCount() )
769 r
= m_grid
->BlockToDeviceRect(
770 wxGridCellCoords( row
, col
),
771 wxGridCellCoords( row
, col
) );
772 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
775 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
776 wxEVT_GRID_RANGE_SELECT
,
778 wxGridCellCoords( row
, col
),
779 wxGridCellCoords( row
, col
),
781 ControlDown
, ShiftDown
,
783 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
787 case wxGrid::wxGridSelectRows
:
789 if ( !m_grid
->GetBatchCount() )
791 r
= m_grid
->BlockToDeviceRect(
792 wxGridCellCoords( row
, 0 ),
793 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
794 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
797 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
798 wxEVT_GRID_RANGE_SELECT
,
800 wxGridCellCoords( row
, 0 ),
801 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
803 ControlDown
, ShiftDown
,
805 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
809 case wxGrid::wxGridSelectColumns
:
811 if ( !m_grid
->GetBatchCount() )
813 r
= m_grid
->BlockToDeviceRect(
814 wxGridCellCoords( 0, col
),
815 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
816 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
819 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
820 wxEVT_GRID_RANGE_SELECT
,
822 wxGridCellCoords( 0, col
),
823 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
825 ControlDown
, ShiftDown
,
827 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
836 void wxGridSelection::ClearSelection()
840 wxGridCellCoords coords1
, coords2
;
842 // deselect all individual cells and update the screen
843 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
845 while ( ( n
= m_cellSelection
.GetCount() ) > 0)
848 coords1
= m_cellSelection
[n
];
849 m_cellSelection
.RemoveAt(n
);
850 if ( !m_grid
->GetBatchCount() )
852 r
= m_grid
->BlockToDeviceRect( coords1
, coords1
);
853 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
856 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
862 // deselect all blocks and update the screen
863 while ( ( n
= m_blockSelectionTopLeft
.GetCount() ) > 0)
866 coords1
= m_blockSelectionTopLeft
[n
];
867 coords2
= m_blockSelectionBottomRight
[n
];
868 m_blockSelectionTopLeft
.RemoveAt(n
);
869 m_blockSelectionBottomRight
.RemoveAt(n
);
870 if ( !m_grid
->GetBatchCount() )
872 r
= m_grid
->BlockToDeviceRect( coords1
, coords2
);
873 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
876 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
881 // deselect all rows and update the screen
882 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
884 while ( ( n
= m_rowSelection
.GetCount() ) > 0)
887 int row
= m_rowSelection
[n
];
888 m_rowSelection
.RemoveAt(n
);
889 if ( !m_grid
->GetBatchCount() )
891 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
892 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
893 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
896 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
902 // deselect all columns and update the screen
903 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
905 while ( ( n
= m_colSelection
.GetCount() ) > 0)
908 int col
= m_colSelection
[n
];
909 m_colSelection
.RemoveAt(n
);
910 if ( !m_grid
->GetBatchCount() )
912 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
913 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
914 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
917 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
923 // One deselection event, indicating deselection of _all_ cells.
924 // (No finer grained events for each of the smaller regions
925 // deselected above!)
926 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
927 wxEVT_GRID_RANGE_SELECT
,
929 wxGridCellCoords( 0, 0 ),
931 m_grid
->GetNumberRows() - 1,
932 m_grid
->GetNumberCols() - 1 ),
935 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
939 void wxGridSelection::UpdateRows( size_t pos
, int numRows
)
941 size_t count
= m_cellSelection
.GetCount();
943 for ( n
= 0; n
< count
; n
++ )
945 wxGridCellCoords
& coords
= m_cellSelection
[n
];
946 wxCoord row
= coords
.GetRow();
947 if ((size_t)row
>= pos
)
951 // If rows inserted, increase row counter where necessary
952 coords
.SetRow(row
+ numRows
);
954 else if (numRows
< 0)
956 // If rows deleted ...
957 if ((size_t)row
>= pos
- numRows
)
959 // ...either decrement row counter (if row still exists)...
960 coords
.SetRow(row
+ numRows
);
964 // ...or remove the attribute
965 m_cellSelection
.RemoveAt(n
);
973 count
= m_blockSelectionTopLeft
.GetCount();
974 for ( n
= 0; n
< count
; n
++ )
976 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
977 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
978 wxCoord row1
= coords1
.GetRow();
979 wxCoord row2
= coords2
.GetRow();
981 if ((size_t)row2
>= pos
)
985 // If rows inserted, increase row counter where necessary
986 coords2
.SetRow( row2
+ numRows
);
987 if ((size_t)row1
>= pos
)
988 coords1
.SetRow( row1
+ numRows
);
990 else if (numRows
< 0)
992 // If rows deleted ...
993 if ((size_t)row2
>= pos
- numRows
)
995 // ...either decrement row counter (if row still exists)...
996 coords2
.SetRow( row2
+ numRows
);
997 if ((size_t)row1
>= pos
)
998 coords1
.SetRow( wxMax(row1
+ numRows
, (int)pos
) );
1003 if ((size_t)row1
>= pos
)
1005 // ...or remove the attribute
1006 m_blockSelectionTopLeft
.RemoveAt(n
);
1007 m_blockSelectionBottomRight
.RemoveAt(n
);
1012 coords2
.SetRow( pos
);
1018 count
= m_rowSelection
.GetCount();
1019 for ( n
= 0; n
< count
; n
++ )
1021 int rowOrCol_
= m_rowSelection
[n
];
1023 if ((size_t) rowOrCol_
>= pos
)
1027 m_rowSelection
[n
] += numRows
;
1029 else if ( numRows
< 0 )
1031 if ((size_t)rowOrCol_
>= (pos
- numRows
))
1032 m_rowSelection
[n
] += numRows
;
1035 m_rowSelection
.RemoveAt( n
);
1042 // No need to touch selected columns, unless we removed _all_
1043 // rows, in this case, we remove all columns from the selection.
1045 if ( !m_grid
->GetNumberRows() )
1046 m_colSelection
.Clear();
1050 void wxGridSelection::UpdateCols( size_t pos
, int numCols
)
1052 size_t count
= m_cellSelection
.GetCount();
1055 for ( n
= 0; n
< count
; n
++ )
1057 wxGridCellCoords
& coords
= m_cellSelection
[n
];
1058 wxCoord col
= coords
.GetCol();
1059 if ((size_t)col
>= pos
)
1063 // If rows inserted, increase row counter where necessary
1064 coords
.SetCol(col
+ numCols
);
1066 else if (numCols
< 0)
1068 // If rows deleted ...
1069 if ((size_t)col
>= pos
- numCols
)
1071 // ...either decrement row counter (if row still exists)...
1072 coords
.SetCol(col
+ numCols
);
1076 // ...or remove the attribute
1077 m_cellSelection
.RemoveAt(n
);
1085 count
= m_blockSelectionTopLeft
.GetCount();
1086 for ( n
= 0; n
< count
; n
++ )
1088 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
1089 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
1090 wxCoord col1
= coords1
.GetCol();
1091 wxCoord col2
= coords2
.GetCol();
1093 if ((size_t)col2
>= pos
)
1097 // If rows inserted, increase row counter where necessary
1098 coords2
.SetCol(col2
+ numCols
);
1099 if ((size_t)col1
>= pos
)
1100 coords1
.SetCol(col1
+ numCols
);
1102 else if (numCols
< 0)
1104 // If cols deleted ...
1105 if ((size_t)col2
>= pos
- numCols
)
1107 // ...either decrement col counter (if col still exists)...
1108 coords2
.SetCol(col2
+ numCols
);
1109 if ( (size_t) col1
>= pos
)
1110 coords1
.SetCol( wxMax(col1
+ numCols
, (int)pos
) );
1115 if ((size_t)col1
>= pos
)
1117 // ...or remove the attribute
1118 m_blockSelectionTopLeft
.RemoveAt(n
);
1119 m_blockSelectionBottomRight
.RemoveAt(n
);
1124 coords2
.SetCol(pos
);
1130 count
= m_colSelection
.GetCount();
1131 for ( n
= 0; n
< count
; n
++ )
1133 int rowOrCol
= m_colSelection
[n
];
1135 if ((size_t)rowOrCol
>= pos
)
1138 m_colSelection
[n
] += numCols
;
1139 else if ( numCols
< 0 )
1141 if ((size_t)rowOrCol
>= (pos
- numCols
))
1142 m_colSelection
[n
] += numCols
;
1145 m_colSelection
.RemoveAt( n
);
1153 // No need to touch selected rows, unless we removed _all_
1154 // columns, in this case, we remove all rows from the selection.
1155 if ( !m_grid
->GetNumberCols() )
1156 m_rowSelection
.Clear();
1159 int wxGridSelection::BlockContain( int topRow1
, int leftCol1
,
1160 int bottomRow1
, int rightCol1
,
1161 int topRow2
, int leftCol2
,
1162 int bottomRow2
, int rightCol2
)
1163 // returns 1, if Block1 contains Block2,
1164 // -1, if Block2 contains Block1,
1167 if ( topRow1
<= topRow2
&& bottomRow2
<= bottomRow1
&&
1168 leftCol1
<= leftCol2
&& rightCol2
<= rightCol1
)
1170 else if ( topRow2
<= topRow1
&& bottomRow1
<= bottomRow2
&&
1171 leftCol2
<= leftCol1
&& rightCol1
<= rightCol2
)