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 SelectBlockNoEvent( topRow
, 0,
164 bottomRow
, m_grid
->GetNumberCols() - 1);
167 else // selmode == wxGridSelectColumns)
169 if (topRow
!= 0 || bottomRow
!= m_grid
->GetNumberRows() - 1 )
171 m_blockSelectionTopLeft
.RemoveAt(n
);
172 m_blockSelectionBottomRight
.RemoveAt(n
);
173 SelectBlockNoEvent(0, leftCol
,
174 m_grid
->GetNumberRows() - 1, rightCol
);
179 m_selectionMode
= selmode
;
183 void wxGridSelection::SelectRow(int row
, const wxKeyboardState
& kbd
)
185 if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
190 // Remove single cells contained in newly selected block.
191 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
193 count
= m_cellSelection
.GetCount();
194 for ( n
= 0; n
< count
; n
++ )
196 wxGridCellCoords
& coords
= m_cellSelection
[n
];
197 if ( BlockContainsCell( row
, 0, row
, m_grid
->GetNumberCols() - 1,
198 coords
.GetRow(), coords
.GetCol() ) )
200 m_cellSelection
.RemoveAt(n
);
207 // Simplify list of selected blocks (if possible)
208 count
= m_blockSelectionTopLeft
.GetCount();
211 for ( n
= 0; n
< count
; n
++ )
213 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
214 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
216 // Remove block if it is a subset of the row
217 if ( coords1
.GetRow() == row
&& row
== coords2
.GetRow() )
219 m_blockSelectionTopLeft
.RemoveAt(n
);
220 m_blockSelectionBottomRight
.RemoveAt(n
);
224 else if ( coords1
.GetCol() == 0 &&
225 coords2
.GetCol() == m_grid
->GetNumberCols() - 1 )
227 // silently return, if row is contained in block
228 if ( coords1
.GetRow() <= row
&& row
<= coords2
.GetRow() )
230 // expand block, if it touched row
231 else if ( coords1
.GetRow() == row
+ 1)
236 else if ( coords2
.GetRow() == row
- 1)
244 // Unless we successfully handled the row,
245 // check whether row is already selected.
248 count
= m_rowSelection
.GetCount();
249 for ( n
= 0; n
< count
; n
++ )
251 if ( row
== m_rowSelection
[n
] )
255 // Add row to selection
256 m_rowSelection
.Add(row
);
260 if ( !m_grid
->GetBatchCount() )
262 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
263 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
264 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
268 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
269 wxEVT_GRID_RANGE_SELECT
,
271 wxGridCellCoords( row
, 0 ),
272 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
276 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
279 void wxGridSelection::SelectCol(int col
, const wxKeyboardState
& kbd
)
281 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
285 // Remove single cells contained in newly selected block.
286 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
288 count
= m_cellSelection
.GetCount();
289 for ( n
= 0; n
< count
; n
++ )
291 wxGridCellCoords
& coords
= m_cellSelection
[n
];
292 if ( BlockContainsCell( 0, col
, m_grid
->GetNumberRows() - 1, col
,
293 coords
.GetRow(), coords
.GetCol() ) )
295 m_cellSelection
.RemoveAt(n
);
302 // Simplify list of selected blocks (if possible)
303 count
= m_blockSelectionTopLeft
.GetCount();
305 for ( n
= 0; n
< count
; n
++ )
307 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
308 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
310 // Remove block if it is a subset of the column
311 if ( coords1
.GetCol() == col
&& col
== coords2
.GetCol() )
313 m_blockSelectionTopLeft
.RemoveAt(n
);
314 m_blockSelectionBottomRight
.RemoveAt(n
);
318 else if ( coords1
.GetRow() == 0 &&
319 coords2
.GetRow() == m_grid
->GetNumberRows() - 1 )
321 // silently return, if row is contained in block
322 if ( coords1
.GetCol() <= col
&& col
<= coords2
.GetCol() )
324 // expand block, if it touched col
325 else if ( coords1
.GetCol() == col
+ 1)
330 else if ( coords2
.GetCol() == col
- 1)
338 // Unless we successfully handled the column,
339 // Check whether col is already selected.
342 count
= m_colSelection
.GetCount();
343 for ( n
= 0; n
< count
; n
++ )
345 if ( col
== m_colSelection
[n
] )
349 // Add col to selection
350 m_colSelection
.Add(col
);
354 if ( !m_grid
->GetBatchCount() )
356 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
357 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
358 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
362 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
363 wxEVT_GRID_RANGE_SELECT
,
365 wxGridCellCoords( 0, col
),
366 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
370 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
373 void wxGridSelection::SelectBlock( int topRow
, int leftCol
,
374 int bottomRow
, int rightCol
,
375 const wxKeyboardState
& kbd
,
378 // Fix the coordinates of the block if needed.
379 switch ( m_selectionMode
)
382 wxFAIL_MSG( "unknown selection mode" );
385 case wxGrid::wxGridSelectCells
:
386 // nothing to do -- in this mode arbitrary blocks can be selected
389 case wxGrid::wxGridSelectRows
:
391 rightCol
= m_grid
->GetNumberCols() - 1;
394 case wxGrid::wxGridSelectColumns
:
396 bottomRow
= m_grid
->GetNumberRows() - 1;
399 case wxGrid::wxGridSelectRowsOrColumns
:
400 // block selection doesn't make sense for this mode, we could only
401 // select the entire grid but this wouldn't be useful
405 if ( topRow
> bottomRow
)
412 if ( leftCol
> rightCol
)
419 // Handle single cell selection in SelectCell.
420 // (MB: added check for selection mode here to prevent
421 // crashes if, for example, we are select rows and the
422 // grid only has 1 col)
423 if ( m_selectionMode
== wxGrid::wxGridSelectCells
&&
424 topRow
== bottomRow
&& leftCol
== rightCol
)
426 SelectCell( topRow
, leftCol
, kbd
, sendEvent
);
431 // Remove single cells contained in newly selected block.
432 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
434 count
= m_cellSelection
.GetCount();
435 for ( n
= 0; n
< count
; n
++ )
437 wxGridCellCoords
& coords
= m_cellSelection
[n
];
438 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
439 coords
.GetRow(), coords
.GetCol() ) )
441 m_cellSelection
.RemoveAt(n
);
448 // If a block containing the selection is already selected, return,
449 // if a block contained in the selection is found, remove it.
451 count
= m_blockSelectionTopLeft
.GetCount();
452 for ( n
= 0; n
< count
; n
++ )
454 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
455 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
457 switch ( BlockContain( coords1
.GetRow(), coords1
.GetCol(),
458 coords2
.GetRow(), coords2
.GetCol(),
459 topRow
, leftCol
, bottomRow
, rightCol
) )
465 m_blockSelectionTopLeft
.RemoveAt(n
);
466 m_blockSelectionBottomRight
.RemoveAt(n
);
476 // If a row containing the selection is already selected, return,
477 // if a row contained in newly selected block is found, remove it.
478 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
480 count
= m_rowSelection
.GetCount();
481 for ( n
= 0; n
< count
; n
++ )
483 switch ( BlockContain( m_rowSelection
[n
], 0,
484 m_rowSelection
[n
], m_grid
->GetNumberCols() - 1,
485 topRow
, leftCol
, bottomRow
, rightCol
) )
491 m_rowSelection
.RemoveAt(n
);
502 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
504 count
= m_colSelection
.GetCount();
505 for ( n
= 0; n
< count
; n
++ )
507 switch ( BlockContain( 0, m_colSelection
[n
],
508 m_grid
->GetNumberRows() - 1, m_colSelection
[n
],
509 topRow
, leftCol
, bottomRow
, rightCol
) )
515 m_colSelection
.RemoveAt(n
);
526 m_blockSelectionTopLeft
.Add( wxGridCellCoords( topRow
, leftCol
) );
527 m_blockSelectionBottomRight
.Add( wxGridCellCoords( bottomRow
, rightCol
) );
530 if ( !m_grid
->GetBatchCount() )
532 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( topRow
, leftCol
),
533 wxGridCellCoords( bottomRow
, rightCol
) );
534 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
537 // Send Event, if not disabled.
540 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
541 wxEVT_GRID_RANGE_SELECT
,
543 wxGridCellCoords( topRow
, leftCol
),
544 wxGridCellCoords( bottomRow
, rightCol
),
547 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
551 void wxGridSelection::SelectCell( int row
, int col
,
552 const wxKeyboardState
& kbd
,
555 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
557 SelectBlock(row
, 0, row
, m_grid
->GetNumberCols() - 1, kbd
, sendEvent
);
561 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
563 SelectBlock(0, col
, m_grid
->GetNumberRows() - 1, col
, kbd
, sendEvent
);
567 else if ( IsInSelection ( row
, col
) )
570 m_cellSelection
.Add( wxGridCellCoords( row
, col
) );
573 if ( !m_grid
->GetBatchCount() )
575 wxRect r
= m_grid
->BlockToDeviceRect(
576 wxGridCellCoords( row
, col
),
577 wxGridCellCoords( row
, col
) );
578 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
584 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
585 wxEVT_GRID_RANGE_SELECT
,
587 wxGridCellCoords( row
, col
),
588 wxGridCellCoords( row
, col
),
591 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
596 wxGridSelection::ToggleCellSelection(int row
, int col
,
597 const wxKeyboardState
& kbd
)
599 // if the cell is not selected, select it
600 if ( !IsInSelection ( row
, col
) )
602 SelectCell(row
, col
, kbd
);
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
),
640 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
647 // The most difficult case: The cell is member of one or even several
648 // blocks. Split each such block in up to 4 new parts, that don't
649 // contain the cell to be selected, like this:
650 // |---------------------------|
654 // |---------------------------|
655 // | part 3 |x| part 4 |
656 // |---------------------------|
660 // |---------------------------|
661 // (The x marks the newly deselected cell).
662 // Note: in row selection mode, we only need part1 and part2;
663 // in column selection mode, we only need part 3 and part4,
664 // which are expanded to whole columns automatically!
666 count
= m_blockSelectionTopLeft
.GetCount();
667 for ( n
= 0; n
< count
; n
++ )
669 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
670 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
671 int topRow
= coords1
.GetRow();
672 int leftCol
= coords1
.GetCol();
673 int bottomRow
= coords2
.GetRow();
674 int rightCol
= coords2
.GetCol();
676 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
, row
, col
) )
679 m_blockSelectionTopLeft
.RemoveAt(n
);
680 m_blockSelectionBottomRight
.RemoveAt(n
);
684 // add up to 4 smaller blocks and set update region
685 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
688 SelectBlockNoEvent(topRow
, leftCol
, row
- 1, rightCol
);
689 if ( bottomRow
> row
)
690 SelectBlockNoEvent(row
+ 1, leftCol
, bottomRow
, rightCol
);
693 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
696 SelectBlockNoEvent(row
, leftCol
, row
, col
- 1);
697 if ( rightCol
> col
)
698 SelectBlockNoEvent(row
, col
+ 1, row
, rightCol
);
703 bool rowSelectionWasChanged
= false;
704 // remove a cell from a row, adding up to two new blocks
705 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
707 count
= m_rowSelection
.GetCount();
708 for ( n
= 0; n
< count
; n
++ )
710 if ( m_rowSelection
[n
] == row
)
712 m_rowSelection
.RemoveAt(n
);
716 rowSelectionWasChanged
= true;
718 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
721 SelectBlockNoEvent(row
, 0, row
, col
- 1);
722 if ( col
< m_grid
->GetNumberCols() - 1 )
723 SelectBlockNoEvent( row
, col
+ 1,
724 row
, m_grid
->GetNumberCols() - 1);
730 bool colSelectionWasChanged
= false;
731 // remove a cell from a column, adding up to two new blocks
732 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
734 count
= m_colSelection
.GetCount();
735 for ( n
= 0; n
< count
; n
++ )
737 if ( m_colSelection
[n
] == col
)
739 m_colSelection
.RemoveAt(n
);
743 colSelectionWasChanged
= true;
745 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
748 SelectBlockNoEvent(0, col
, row
- 1, col
);
749 if ( row
< m_grid
->GetNumberRows() - 1 )
750 SelectBlockNoEvent(row
+ 1, col
,
751 m_grid
->GetNumberRows() - 1, col
);
757 // Refresh the screen and send the event; according to m_selectionMode,
758 // we need to either update only the cell, or the whole row/column.
760 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
762 if ( !m_grid
->GetBatchCount() )
764 r
= m_grid
->BlockToDeviceRect(
765 wxGridCellCoords( row
, col
),
766 wxGridCellCoords( row
, col
) );
767 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
770 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
771 wxEVT_GRID_RANGE_SELECT
,
773 wxGridCellCoords( row
, col
),
774 wxGridCellCoords( row
, col
),
777 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
779 else // rows/columns selection mode
781 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
&&
782 rowSelectionWasChanged
)
784 int numCols
= m_grid
->GetNumberCols();
785 for ( int colFrom
= 0, colTo
= 0; colTo
<= numCols
; ++colTo
)
787 if ( m_colSelection
.Index(colTo
) >= 0 || colTo
== numCols
)
789 if ( colFrom
< colTo
)
791 if ( !m_grid
->GetBatchCount() )
793 r
= m_grid
->BlockToDeviceRect(
794 wxGridCellCoords( row
, colFrom
),
795 wxGridCellCoords( row
, colTo
-1 ) );
796 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
799 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
800 wxEVT_GRID_RANGE_SELECT
,
802 wxGridCellCoords( row
, colFrom
),
803 wxGridCellCoords( row
, colTo
- 1 ),
806 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
814 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
&&
815 colSelectionWasChanged
)
817 int numRows
= m_grid
->GetNumberRows();
818 for ( int rowFrom
= 0, rowTo
= 0; rowTo
<= numRows
; ++rowTo
)
820 if ( m_rowSelection
.Index(rowTo
) >= 0 || rowTo
== numRows
)
824 if ( !m_grid
->GetBatchCount() )
826 r
= m_grid
->BlockToDeviceRect(
827 wxGridCellCoords( rowFrom
, col
),
828 wxGridCellCoords( rowTo
- 1, col
) );
829 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
832 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
833 wxEVT_GRID_RANGE_SELECT
,
835 wxGridCellCoords( rowFrom
, col
),
836 wxGridCellCoords( rowTo
- 1, col
),
839 m_grid
->GetEventHandler()->ProcessEvent( gridEvt
);
849 void wxGridSelection::ClearSelection()
853 wxGridCellCoords coords1
, coords2
;
855 // deselect all individual cells and update the screen
856 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
858 while ( ( n
= m_cellSelection
.GetCount() ) > 0)
861 coords1
= m_cellSelection
[n
];
862 m_cellSelection
.RemoveAt(n
);
863 if ( !m_grid
->GetBatchCount() )
865 r
= m_grid
->BlockToDeviceRect( coords1
, coords1
);
866 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
869 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
875 // deselect all blocks and update the screen
876 while ( ( n
= m_blockSelectionTopLeft
.GetCount() ) > 0)
879 coords1
= m_blockSelectionTopLeft
[n
];
880 coords2
= m_blockSelectionBottomRight
[n
];
881 m_blockSelectionTopLeft
.RemoveAt(n
);
882 m_blockSelectionBottomRight
.RemoveAt(n
);
883 if ( !m_grid
->GetBatchCount() )
885 r
= m_grid
->BlockToDeviceRect( coords1
, coords2
);
886 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
889 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
894 // deselect all rows and update the screen
895 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
897 while ( ( n
= m_rowSelection
.GetCount() ) > 0)
900 int row
= m_rowSelection
[n
];
901 m_rowSelection
.RemoveAt(n
);
902 if ( !m_grid
->GetBatchCount() )
904 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
905 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
906 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
909 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
915 // deselect all columns and update the screen
916 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
918 while ( ( n
= m_colSelection
.GetCount() ) > 0)
921 int col
= m_colSelection
[n
];
922 m_colSelection
.RemoveAt(n
);
923 if ( !m_grid
->GetBatchCount() )
925 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
926 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
927 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( false, &r
);
930 ((wxWindow
*)m_grid
->m_gridWin
)->Update();
936 // One deselection event, indicating deselection of _all_ cells.
937 // (No finer grained events for each of the smaller regions
938 // deselected above!)
939 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
940 wxEVT_GRID_RANGE_SELECT
,
942 wxGridCellCoords( 0, 0 ),
944 m_grid
->GetNumberRows() - 1,
945 m_grid
->GetNumberCols() - 1 ),
948 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
952 void wxGridSelection::UpdateRows( size_t pos
, int numRows
)
954 size_t count
= m_cellSelection
.GetCount();
956 for ( n
= 0; n
< count
; n
++ )
958 wxGridCellCoords
& coords
= m_cellSelection
[n
];
959 wxCoord row
= coords
.GetRow();
960 if ((size_t)row
>= pos
)
964 // If rows inserted, increase row counter where necessary
965 coords
.SetRow(row
+ numRows
);
967 else if (numRows
< 0)
969 // If rows deleted ...
970 if ((size_t)row
>= pos
- numRows
)
972 // ...either decrement row counter (if row still exists)...
973 coords
.SetRow(row
+ numRows
);
977 // ...or remove the attribute
978 m_cellSelection
.RemoveAt(n
);
986 count
= m_blockSelectionTopLeft
.GetCount();
987 for ( n
= 0; n
< count
; n
++ )
989 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
990 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
991 wxCoord row1
= coords1
.GetRow();
992 wxCoord row2
= coords2
.GetRow();
994 if ((size_t)row2
>= pos
)
998 // If rows inserted, increase row counter where necessary
999 coords2
.SetRow( row2
+ numRows
);
1000 if ((size_t)row1
>= pos
)
1001 coords1
.SetRow( row1
+ numRows
);
1003 else if (numRows
< 0)
1005 // If rows deleted ...
1006 if ((size_t)row2
>= pos
- numRows
)
1008 // ...either decrement row counter (if row still exists)...
1009 coords2
.SetRow( row2
+ numRows
);
1010 if ((size_t)row1
>= pos
)
1011 coords1
.SetRow( wxMax(row1
+ numRows
, (int)pos
) );
1016 if ((size_t)row1
>= pos
)
1018 // ...or remove the attribute
1019 m_blockSelectionTopLeft
.RemoveAt(n
);
1020 m_blockSelectionBottomRight
.RemoveAt(n
);
1025 coords2
.SetRow( pos
);
1031 count
= m_rowSelection
.GetCount();
1032 for ( n
= 0; n
< count
; n
++ )
1034 int rowOrCol_
= m_rowSelection
[n
];
1036 if ((size_t) rowOrCol_
>= pos
)
1040 m_rowSelection
[n
] += numRows
;
1042 else if ( numRows
< 0 )
1044 if ((size_t)rowOrCol_
>= (pos
- numRows
))
1045 m_rowSelection
[n
] += numRows
;
1048 m_rowSelection
.RemoveAt( n
);
1055 // No need to touch selected columns, unless we removed _all_
1056 // rows, in this case, we remove all columns from the selection.
1058 if ( !m_grid
->GetNumberRows() )
1059 m_colSelection
.Clear();
1063 void wxGridSelection::UpdateCols( size_t pos
, int numCols
)
1065 size_t count
= m_cellSelection
.GetCount();
1068 for ( n
= 0; n
< count
; n
++ )
1070 wxGridCellCoords
& coords
= m_cellSelection
[n
];
1071 wxCoord col
= coords
.GetCol();
1072 if ((size_t)col
>= pos
)
1076 // If rows inserted, increase row counter where necessary
1077 coords
.SetCol(col
+ numCols
);
1079 else if (numCols
< 0)
1081 // If rows deleted ...
1082 if ((size_t)col
>= pos
- numCols
)
1084 // ...either decrement row counter (if row still exists)...
1085 coords
.SetCol(col
+ numCols
);
1089 // ...or remove the attribute
1090 m_cellSelection
.RemoveAt(n
);
1098 count
= m_blockSelectionTopLeft
.GetCount();
1099 for ( n
= 0; n
< count
; n
++ )
1101 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
1102 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
1103 wxCoord col1
= coords1
.GetCol();
1104 wxCoord col2
= coords2
.GetCol();
1106 if ((size_t)col2
>= pos
)
1110 // If rows inserted, increase row counter where necessary
1111 coords2
.SetCol(col2
+ numCols
);
1112 if ((size_t)col1
>= pos
)
1113 coords1
.SetCol(col1
+ numCols
);
1115 else if (numCols
< 0)
1117 // If cols deleted ...
1118 if ((size_t)col2
>= pos
- numCols
)
1120 // ...either decrement col counter (if col still exists)...
1121 coords2
.SetCol(col2
+ numCols
);
1122 if ( (size_t) col1
>= pos
)
1123 coords1
.SetCol( wxMax(col1
+ numCols
, (int)pos
) );
1128 if ((size_t)col1
>= pos
)
1130 // ...or remove the attribute
1131 m_blockSelectionTopLeft
.RemoveAt(n
);
1132 m_blockSelectionBottomRight
.RemoveAt(n
);
1137 coords2
.SetCol(pos
);
1143 count
= m_colSelection
.GetCount();
1144 for ( n
= 0; n
< count
; n
++ )
1146 int rowOrCol
= m_colSelection
[n
];
1148 if ((size_t)rowOrCol
>= pos
)
1151 m_colSelection
[n
] += numCols
;
1152 else if ( numCols
< 0 )
1154 if ((size_t)rowOrCol
>= (pos
- numCols
))
1155 m_colSelection
[n
] += numCols
;
1158 m_colSelection
.RemoveAt( n
);
1166 // No need to touch selected rows, unless we removed _all_
1167 // columns, in this case, we remove all rows from the selection.
1168 if ( !m_grid
->GetNumberCols() )
1169 m_rowSelection
.Clear();
1172 int wxGridSelection::BlockContain( int topRow1
, int leftCol1
,
1173 int bottomRow1
, int rightCol1
,
1174 int topRow2
, int leftCol2
,
1175 int bottomRow2
, int rightCol2
)
1176 // returns 1, if Block1 contains Block2,
1177 // -1, if Block2 contains Block1,
1180 if ( topRow1
<= topRow2
&& bottomRow2
<= bottomRow1
&&
1181 leftCol1
<= leftCol2
&& rightCol2
<= rightCol1
)
1183 else if ( topRow2
<= topRow1
&& bottomRow1
<= bottomRow2
&&
1184 leftCol2
<= leftCol1
&& rightCol1
<= rightCol2
)