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 // ----------------------------------------------------------------------------
21 #pragma implementation "gridsel.h"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include "wx/wxprec.h"
33 #if defined(wxUSE_NEW_GRID) && (wxUSE_NEW_GRID)
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,
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
,
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 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
263 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
264 if ( !m_grid
->GetBatchCount() )
265 ((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 ),
274 ControlDown
, ShiftDown
,
277 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
280 void wxGridSelection::SelectCol( int col
,
281 bool ControlDown
, bool ShiftDown
,
282 bool AltDown
, bool MetaDown
)
284 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
288 // Remove single cells contained in newly selected block.
289 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
291 count
= m_cellSelection
.GetCount();
292 for ( n
= 0; n
< count
; n
++ )
294 wxGridCellCoords
& coords
= m_cellSelection
[n
];
295 if ( BlockContainsCell( 0, col
, m_grid
->GetNumberRows() - 1, col
,
296 coords
.GetRow(), coords
.GetCol() ) )
298 m_cellSelection
.RemoveAt(n
);
304 // Simplify list of selected blocks (if possible)
305 count
= m_blockSelectionTopLeft
.GetCount();
307 for ( n
= 0; n
< count
; n
++ )
309 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
310 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
312 // Remove block if it is a subset of the column
313 if ( coords1
.GetCol() == col
&& col
== coords2
.GetCol() )
315 m_blockSelectionTopLeft
.RemoveAt(n
);
316 m_blockSelectionBottomRight
.RemoveAt(n
);
319 else if ( coords1
.GetRow() == 0 &&
320 coords2
.GetRow() == m_grid
->GetNumberRows() - 1 )
322 // silently return, if row is contained in block
323 if ( coords1
.GetCol() <= col
&& col
<= coords2
.GetCol() )
325 // expand block, if it touched col
326 else if ( coords1
.GetCol() == col
+ 1)
331 else if ( coords2
.GetCol() == col
- 1)
339 // Unless we successfully handled the column,
340 // Check whether col is already selected.
343 count
= m_colSelection
.GetCount();
344 for ( n
= 0; n
< count
; n
++ )
346 if ( col
== m_colSelection
[n
] )
350 // Add col to selection
351 m_colSelection
.Add(col
);
355 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
356 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
357 if ( !m_grid
->GetBatchCount() )
358 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
361 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
362 wxEVT_GRID_RANGE_SELECT
,
364 wxGridCellCoords( 0, col
),
365 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
367 ControlDown
, ShiftDown
,
370 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
373 void wxGridSelection::SelectBlock( int topRow
, int leftCol
,
374 int bottomRow
, int rightCol
,
375 bool ControlDown
, bool ShiftDown
,
376 bool AltDown
, bool MetaDown
,
379 // Fix the coordinates of the block if needed.
380 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
383 rightCol
= m_grid
->GetNumberCols() - 1;
385 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
388 bottomRow
= m_grid
->GetNumberRows() - 1;
390 if ( topRow
> bottomRow
)
397 if ( leftCol
> rightCol
)
404 // Handle single cell selection in SelectCell.
405 if ( topRow
== bottomRow
&& leftCol
== rightCol
)
406 SelectCell( topRow
, leftCol
, ControlDown
, ShiftDown
,
407 AltDown
, MetaDown
, sendEvent
);
410 // Remove single cells contained in newly selected block.
411 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
413 count
= m_cellSelection
.GetCount();
414 for ( n
= 0; n
< count
; n
++ )
416 wxGridCellCoords
& coords
= m_cellSelection
[n
];
417 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
418 coords
.GetRow(), coords
.GetCol() ) )
420 m_cellSelection
.RemoveAt(n
);
426 // If a block containing the selection is already selected, return,
427 // if a block contained in the selection is found, remove it.
429 count
= m_blockSelectionTopLeft
.GetCount();
430 for ( n
= 0; n
< count
; n
++ )
432 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
433 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
434 switch ( BlockContain( coords1
.GetRow(), coords1
.GetCol(),
435 coords2
.GetRow(), coords2
.GetCol(),
436 topRow
, leftCol
, bottomRow
, rightCol
) )
441 m_blockSelectionTopLeft
.RemoveAt(n
);
442 m_blockSelectionBottomRight
.RemoveAt(n
);
449 // If a row containing the selection is already selected, return,
450 // if a row contained in newly selected block is found, remove it.
451 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
453 count
= m_rowSelection
.GetCount();
454 for ( n
= 0; n
< count
; n
++ )
456 switch ( BlockContain( m_rowSelection
[n
], 0,
457 m_rowSelection
[n
], m_grid
->GetNumberCols()-1,
458 topRow
, leftCol
, bottomRow
, rightCol
) )
463 m_rowSelection
.RemoveAt(n
);
470 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
472 count
= m_colSelection
.GetCount();
473 for ( n
= 0; n
< count
; n
++ )
475 switch ( BlockContain( 0, m_colSelection
[n
],
476 m_grid
->GetNumberRows()-1, m_colSelection
[n
],
477 topRow
, leftCol
, bottomRow
, rightCol
) )
482 m_colSelection
.RemoveAt(n
);
489 m_blockSelectionTopLeft
.Add( wxGridCellCoords( topRow
, leftCol
) );
490 m_blockSelectionBottomRight
.Add( wxGridCellCoords( bottomRow
, rightCol
) );
493 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( topRow
, leftCol
),
494 wxGridCellCoords( bottomRow
, rightCol
) );
495 if ( !m_grid
->GetBatchCount() )
496 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
498 // Send Event, if not disabled.
501 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
502 wxEVT_GRID_RANGE_SELECT
,
504 wxGridCellCoords( topRow
, leftCol
),
505 wxGridCellCoords( bottomRow
, rightCol
),
507 ControlDown
, ShiftDown
,
509 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
513 void wxGridSelection::SelectCell( int row
, int col
,
514 bool ControlDown
, bool ShiftDown
,
515 bool AltDown
, bool MetaDown
,
518 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
520 SelectBlock(row
, 0, row
, m_grid
->GetNumberCols() - 1,
521 ControlDown
, ShiftDown
, AltDown
, MetaDown
);
524 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
526 SelectBlock(0, col
, m_grid
->GetNumberRows() - 1, col
,
527 ControlDown
, ShiftDown
, AltDown
, MetaDown
);
530 else if ( IsInSelection ( row
, col
) )
532 m_cellSelection
.Add( wxGridCellCoords( row
, col
) );
535 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, col
),
536 wxGridCellCoords( row
, col
) );
537 if ( !m_grid
->GetBatchCount() )
538 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
543 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
544 wxEVT_GRID_RANGE_SELECT
,
546 wxGridCellCoords( row
, col
),
547 wxGridCellCoords( row
, col
),
549 ControlDown
, ShiftDown
,
551 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
555 void wxGridSelection::ToggleCellSelection( int row
, int col
,
556 bool ControlDown
, bool ShiftDown
,
557 bool AltDown
, bool MetaDown
)
559 // if the cell is not selected, select it
560 if ( !IsInSelection ( row
, col
) )
562 SelectCell( row
, col
, ControlDown
, ShiftDown
,
567 // otherwise deselect it. This can be simple or more or
568 // less difficult, depending on how the cell is selected.
571 // The simplest case: The cell is contained in m_cellSelection
572 // Then it can't be contained in rows/cols/block (since those
573 // would remove the cell from m_cellSelection on creation), so
574 // we just have to remove it from m_cellSelection.
576 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
578 count
= m_cellSelection
.GetCount();
579 for ( n
= 0; n
< count
; n
++ )
581 wxGridCellCoords
& coords
= m_cellSelection
[n
];
582 if ( row
== coords
.GetRow() && col
== coords
.GetCol() )
585 r
= m_grid
->BlockToDeviceRect( m_cellSelection
[n
],
586 m_cellSelection
[n
] );
587 m_cellSelection
.RemoveAt(n
);
589 if ( !m_grid
->GetBatchCount() )
590 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
593 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
594 wxEVT_GRID_RANGE_SELECT
,
596 wxGridCellCoords( row
, col
),
597 wxGridCellCoords( row
, col
),
599 ControlDown
, ShiftDown
,
601 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
607 // The most difficult case: The cell is member of one or even several
608 // blocks. Split each such block in up to 4 new parts, that don't
609 // contain the cell to be selected, like this:
610 // |---------------------------|
614 // |---------------------------|
615 // | part 3 |x| part 4 |
616 // |---------------------------|
620 // |---------------------------|
621 // (The x marks the newly deselected cell).
622 // Note: in row selection mode, we only need part1 and part2;
623 // in column selection mode, we only need part 3 and part4,
624 // which are expanded to whole columns automatically!
626 count
= m_blockSelectionTopLeft
.GetCount();
627 for ( n
= 0; n
< count
; n
++ )
629 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
630 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
631 int topRow
= coords1
.GetRow();
632 int leftCol
= coords1
.GetCol();
633 int bottomRow
= coords2
.GetRow();
634 int rightCol
= coords2
.GetCol();
635 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
639 m_blockSelectionTopLeft
.RemoveAt(n
);
640 m_blockSelectionBottomRight
.RemoveAt(n
);
642 // add up to 4 smaller blocks and set update region
643 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
646 SelectBlock( topRow
, leftCol
,
647 row
- 1, rightCol
, 0, FALSE
);
648 if ( bottomRow
> row
)
649 SelectBlock( row
+ 1, leftCol
,
650 bottomRow
, rightCol
, 0, FALSE
);
652 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
655 SelectBlock( row
, leftCol
, row
, col
- 1, 0, FALSE
);
656 if ( rightCol
> col
)
657 SelectBlock( row
, col
+ 1, row
, rightCol
, 0, FALSE
);
662 // remove a cell from a row, adding up to two new blocks
663 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
665 count
= m_rowSelection
.GetCount();
666 for ( n
= 0; n
< count
; n
++ )
668 if ( m_rowSelection
[n
] == row
)
670 m_rowSelection
.RemoveAt(n
);
672 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
675 SelectBlock( row
, 0, row
, col
- 1, 0, FALSE
);
676 if ( col
< m_grid
->GetNumberCols() - 1 )
677 SelectBlock( row
, col
+ 1,
678 row
, m_grid
->GetNumberCols() - 1,
685 // remove a cell from a column, adding up to two new blocks
686 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
688 count
= m_colSelection
.GetCount();
689 for ( n
= 0; n
< count
; n
++ )
691 if ( m_colSelection
[n
] == col
)
693 m_colSelection
.RemoveAt(n
);
695 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
698 SelectBlock( 0, col
, row
- 1, col
, 0, FALSE
);
699 if ( row
< m_grid
->GetNumberRows() - 1 )
700 SelectBlock( row
+ 1, col
,
701 m_grid
->GetNumberRows() - 1, col
,
708 // Refresh the screen and send the event; according to m_selectionMode,
709 // we need to either update only the cell, or the whole row/column.
711 switch (m_selectionMode
)
713 case wxGrid::wxGridSelectCells
:
715 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, col
),
716 wxGridCellCoords( row
, col
) );
717 if ( !m_grid
->GetBatchCount() )
718 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
719 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
720 wxEVT_GRID_RANGE_SELECT
,
722 wxGridCellCoords( row
, col
),
723 wxGridCellCoords( row
, col
),
725 ControlDown
, ShiftDown
,
727 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
730 case wxGrid::wxGridSelectRows
:
732 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
733 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
734 if ( !m_grid
->GetBatchCount() )
735 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
736 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
737 wxEVT_GRID_RANGE_SELECT
,
739 wxGridCellCoords( row
, 0 ),
740 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
742 ControlDown
, ShiftDown
,
744 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
747 case wxGrid::wxGridSelectColumns
:
749 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
750 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
751 if ( !m_grid
->GetBatchCount() )
752 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
753 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
754 wxEVT_GRID_RANGE_SELECT
,
756 wxGridCellCoords( 0, col
),
757 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
759 ControlDown
, ShiftDown
,
761 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
767 void wxGridSelection::ClearSelection()
771 // deselect all invidiual cells and update the screen
772 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
775 while( ( n
= m_cellSelection
.GetCount() ) > 0)
779 r
= m_grid
->BlockToDeviceRect( m_cellSelection
[n
],
780 m_cellSelection
[n
] );
781 m_cellSelection
.RemoveAt(n
);
782 if ( !m_grid
->GetBatchCount() )
783 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
787 // deselect all blocks and update the screen
788 while( ( n
= m_blockSelectionTopLeft
.GetCount() ) > 0)
792 r
= m_grid
->BlockToDeviceRect( m_blockSelectionTopLeft
[n
],
793 m_blockSelectionBottomRight
[n
] );
794 m_blockSelectionTopLeft
.RemoveAt(n
);
795 m_blockSelectionBottomRight
.RemoveAt(n
);
796 if ( !m_grid
->GetBatchCount() )
797 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
800 // deselect all rows and update the screen
801 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
803 while( ( n
= m_rowSelection
.GetCount() ) > 0)
806 int & row
= m_rowSelection
[n
];
808 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
809 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
810 m_rowSelection
.RemoveAt(n
);
811 if ( !m_grid
->GetBatchCount() )
812 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
816 // deselect all columns and update the screen
817 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
819 while( ( n
= m_colSelection
.GetCount() ) > 0)
822 int & col
= m_colSelection
[n
];
824 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
825 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
826 m_colSelection
.RemoveAt(n
);
827 if ( !m_grid
->GetBatchCount() )
828 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
832 // One deselection event, indicating deselection of _all_ cells.
833 // (No finer grained events for each of the smaller regions
834 // deselected above!)
835 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
836 wxEVT_GRID_RANGE_SELECT
,
838 wxGridCellCoords( 0, 0 ),
839 wxGridCellCoords( m_grid
->GetNumberRows() - 1,
840 m_grid
->GetNumberCols() - 1 ),
843 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
847 void wxGridSelection::UpdateRows( size_t pos
, int numRows
)
849 size_t count
= m_cellSelection
.GetCount();
851 for ( n
= 0; n
< count
; n
++ )
853 wxGridCellCoords
& coords
= m_cellSelection
[n
];
854 wxCoord row
= coords
.GetRow();
855 if ((size_t)row
>= pos
)
859 // If rows inserted, increase row counter where necessary
860 coords
.SetRow(row
+ numRows
);
862 else if (numRows
< 0)
864 // If rows deleted ...
865 if ((size_t)row
>= pos
- numRows
)
867 // ...either decrement row counter (if row still exists)...
868 coords
.SetRow(row
+ numRows
);
872 // ...or remove the attribute
873 m_cellSelection
.RemoveAt(n
);
880 count
= m_blockSelectionTopLeft
.GetCount();
881 for ( n
= 0; n
< count
; n
++ )
883 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
884 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
885 wxCoord row1
= coords1
.GetRow();
886 wxCoord row2
= coords2
.GetRow();
887 if ((size_t)row2
>= pos
)
891 // If rows inserted, increase row counter where necessary
892 coords2
.SetRow(row2
+ numRows
);
893 if ( (size_t)row1
>= pos
)
894 coords1
.SetRow(row1
+ numRows
);
896 else if (numRows
< 0)
898 // If rows deleted ...
899 if ((size_t)row2
>= pos
- numRows
)
901 // ...either decrement row counter (if row still exists)...
902 coords2
.SetRow(row2
+ numRows
);
903 if ( (size_t) row1
>= pos
)
904 coords1
.SetRow( wxMax(row1
+ numRows
, (int) pos
) );
909 if ( (size_t) row1
>= pos
)
911 // ...or remove the attribute
912 m_blockSelectionTopLeft
.RemoveAt(n
);
913 m_blockSelectionBottomRight
.RemoveAt(n
);
923 count
= m_rowSelection
.GetCount();
924 for ( n
= 0; n
< count
; n
++ )
926 int & rowOrCol
= m_rowSelection
[n
];
927 if ( (size_t)rowOrCol
>= pos
)
931 // If rows inserted, include row counter where necessary
934 else if ( numRows
< 0)
936 // If rows deleted, either decrement row counter (if row still exists)
937 if ((size_t)rowOrCol
>= pos
- numRows
)
941 m_rowSelection
.RemoveAt(n
);
947 // No need to touch selected columns, unless we removed _all_
948 // rows, in this case, we remove all columns from the selection.
949 if ( !m_grid
->GetNumberRows() )
950 m_colSelection
.Clear();
953 void wxGridSelection::UpdateCols( size_t pos
, int numCols
)
955 size_t count
= m_cellSelection
.GetCount();
957 for ( n
= 0; n
< count
; n
++ )
959 wxGridCellCoords
& coords
= m_cellSelection
[n
];
960 wxCoord col
= coords
.GetCol();
961 if ((size_t)col
>= pos
)
965 // If rows inserted, increase row counter where necessary
966 coords
.SetCol(col
+ numCols
);
968 else if (numCols
< 0)
970 // If rows deleted ...
971 if ((size_t)col
>= pos
- numCols
)
973 // ...either decrement row counter (if row still exists)...
974 coords
.SetCol(col
+ numCols
);
978 // ...or remove the attribute
979 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 col1
= coords1
.GetCol();
992 wxCoord col2
= coords2
.GetCol();
993 if ((size_t)col2
>= pos
)
997 // If rows inserted, increase row counter where necessary
998 coords2
.SetCol(col2
+ numCols
);
999 if ( (size_t)col1
>= pos
)
1000 coords1
.SetCol(col1
+ numCols
);
1002 else if (numCols
< 0)
1004 // If cols deleted ...
1005 if ((size_t)col2
>= pos
- numCols
)
1007 // ...either decrement col counter (if col still exists)...
1008 coords2
.SetCol(col2
+ numCols
);
1009 if ( (size_t) col1
>= pos
)
1010 coords1
.SetCol( wxMax(col1
+ numCols
, (int) pos
) );
1015 if ( (size_t) col1
>= pos
)
1017 // ...or remove the attribute
1018 m_blockSelectionTopLeft
.RemoveAt(n
);
1019 m_blockSelectionBottomRight
.RemoveAt(n
);
1023 coords2
.SetCol(pos
);
1029 count
= m_colSelection
.GetCount();
1030 for ( n
= 0; n
< count
; n
++ )
1032 int & rowOrCol
= m_colSelection
[n
];
1033 if ( (size_t)rowOrCol
>= pos
)
1037 // If cols inserted, include col counter where necessary
1038 rowOrCol
+= numCols
;
1040 else if ( numCols
< 0)
1042 // If cols deleted, either decrement col counter (if col still exists)
1043 if ((size_t)rowOrCol
>= pos
- numCols
)
1044 rowOrCol
+= numCols
;
1047 m_colSelection
.RemoveAt(n
);
1054 // No need to touch selected rows, unless we removed _all_
1055 // columns, in this case, we remove all rows from the selection.
1056 if ( !m_grid
->GetNumberCols() )
1057 m_rowSelection
.Clear();
1060 int wxGridSelection::BlockContain( int topRow1
, int leftCol1
,
1061 int bottomRow1
, int rightCol1
,
1062 int topRow2
, int leftCol2
,
1063 int bottomRow2
, int rightCol2
)
1064 // returns 1, if Block1 contains Block2,
1065 // -1, if Block2 contains Block1,
1068 if ( topRow1
<= topRow2
&& bottomRow2
<= bottomRow1
&&
1069 leftCol1
<= leftCol2
&& rightCol2
<= rightCol1
)
1071 else if ( topRow2
<= topRow1
&& bottomRow1
<= bottomRow2
&&
1072 leftCol2
<= leftCol1
&& rightCol1
<= rightCol2
)