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
, bool addToSelected
)
189 if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
193 // Remove single cells contained in newly selected block.
194 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
196 count
= m_cellSelection
.GetCount();
197 for ( n
= 0; n
< count
; n
++ )
199 wxGridCellCoords
& coords
= m_cellSelection
[n
];
200 if ( BlockContainsCell( row
, 0, row
, m_grid
->GetNumberCols() - 1,
201 coords
.GetRow(), coords
.GetCol() ) )
203 m_cellSelection
.RemoveAt(n
);
209 // Simplify list of selected blocks (if possible)
210 count
= m_blockSelectionTopLeft
.GetCount();
212 for ( n
= 0; n
< count
; n
++ )
214 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
215 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
217 // Remove block if it is a subset of the row
218 if ( coords1
.GetRow() == row
&& row
== coords2
.GetRow() )
220 m_blockSelectionTopLeft
.RemoveAt(n
);
221 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 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
261 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
262 if ( !m_grid
->GetBatchCount() )
263 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
266 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
267 wxEVT_GRID_RANGE_SELECT
,
269 wxGridCellCoords( row
, 0 ),
270 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
272 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
275 void wxGridSelection::SelectCol( int col
, bool addToSelected
)
277 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
281 // Remove single cells contained in newly selected block.
282 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
284 count
= m_cellSelection
.GetCount();
285 for ( n
= 0; n
< count
; n
++ )
287 wxGridCellCoords
& coords
= m_cellSelection
[n
];
288 if ( BlockContainsCell( 0, col
, m_grid
->GetNumberRows() - 1, col
,
289 coords
.GetRow(), coords
.GetCol() ) )
291 m_cellSelection
.RemoveAt(n
);
297 // Simplify list of selected blocks (if possible)
298 count
= m_blockSelectionTopLeft
.GetCount();
300 for ( n
= 0; n
< count
; n
++ )
302 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
303 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
305 // Remove block if it is a subset of the column
306 if ( coords1
.GetCol() == col
&& col
== coords2
.GetCol() )
308 m_blockSelectionTopLeft
.RemoveAt(n
);
309 m_blockSelectionBottomRight
.RemoveAt(n
);
312 else if ( coords1
.GetRow() == 0 &&
313 coords2
.GetRow() == m_grid
->GetNumberRows() - 1 )
315 // silently return, if row is contained in block
316 if ( coords1
.GetCol() <= col
&& col
<= coords2
.GetCol() )
318 // expand block, if it touched col
319 else if ( coords1
.GetCol() == col
+ 1)
324 else if ( coords2
.GetCol() == col
- 1)
332 // Unless we successfully handled the column,
333 // Check whether col is already selected.
336 count
= m_colSelection
.GetCount();
337 for ( n
= 0; n
< count
; n
++ )
339 if ( col
== m_colSelection
[n
] )
343 // Add col to selection
344 m_colSelection
.Add(col
);
348 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
349 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
350 if ( !m_grid
->GetBatchCount() )
351 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
354 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
355 wxEVT_GRID_RANGE_SELECT
,
357 wxGridCellCoords( 0, col
),
358 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
360 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
363 void wxGridSelection::SelectBlock( int topRow
, int leftCol
,
364 int bottomRow
, int rightCol
,
365 wxMouseEvent
* mouseEv
, bool sendEvent
)
367 // Fix the coordinates of the block if needed.
368 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
371 rightCol
= m_grid
->GetNumberCols() - 1;
373 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
376 bottomRow
= m_grid
->GetNumberRows() - 1;
378 if ( topRow
> bottomRow
)
385 if ( leftCol
> rightCol
)
392 // Handle single cell selection in SelectCell.
393 if ( topRow
== bottomRow
&& leftCol
== rightCol
)
394 SelectCell( topRow
, leftCol
, sendEvent
);
397 // Remove single cells contained in newly selected block.
398 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
400 count
= m_cellSelection
.GetCount();
401 for ( n
= 0; n
< count
; n
++ )
403 wxGridCellCoords
& coords
= m_cellSelection
[n
];
404 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
405 coords
.GetRow(), coords
.GetCol() ) )
407 m_cellSelection
.RemoveAt(n
);
413 // If a block containing the selection is already selected, return,
414 // if a block contained in the selection is found, remove it.
416 count
= m_blockSelectionTopLeft
.GetCount();
417 for ( n
= 0; n
< count
; n
++ )
419 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
420 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
421 switch ( BlockContain( coords1
.GetRow(), coords1
.GetCol(),
422 coords2
.GetRow(), coords2
.GetCol(),
423 topRow
, leftCol
, bottomRow
, rightCol
) )
428 m_blockSelectionTopLeft
.RemoveAt(n
);
429 m_blockSelectionBottomRight
.RemoveAt(n
);
436 // If a row containing the selection is already selected, return,
437 // if a row contained in newly selected block is found, remove it.
438 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
440 count
= m_rowSelection
.GetCount();
441 for ( n
= 0; n
< count
; n
++ )
443 switch ( BlockContain( m_rowSelection
[n
], 0,
444 m_rowSelection
[n
], m_grid
->GetNumberCols()-1,
445 topRow
, leftCol
, bottomRow
, rightCol
) )
450 m_rowSelection
.RemoveAt(n
);
457 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
459 count
= m_colSelection
.GetCount();
460 for ( n
= 0; n
< count
; n
++ )
462 switch ( BlockContain( 0, m_colSelection
[n
],
463 m_grid
->GetNumberRows()-1, m_colSelection
[n
],
464 topRow
, leftCol
, bottomRow
, rightCol
) )
469 m_colSelection
.RemoveAt(n
);
476 m_blockSelectionTopLeft
.Add( wxGridCellCoords( topRow
, leftCol
) );
477 m_blockSelectionBottomRight
.Add( wxGridCellCoords( bottomRow
, rightCol
) );
480 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( topRow
, leftCol
),
481 wxGridCellCoords( bottomRow
, rightCol
) );
482 if ( !m_grid
->GetBatchCount() )
483 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
485 // Send Event, if not disabled.
490 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
491 wxEVT_GRID_RANGE_SELECT
,
493 wxGridCellCoords( topRow
, leftCol
),
494 wxGridCellCoords( bottomRow
, rightCol
) );
495 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
499 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
500 wxEVT_GRID_RANGE_SELECT
,
502 wxGridCellCoords( topRow
, leftCol
),
503 wxGridCellCoords( bottomRow
, rightCol
),
505 mouseEv
->ControlDown(),
506 mouseEv
->ShiftDown(),
508 mouseEv
->MetaDown() );
509 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
514 void wxGridSelection::SelectCell( int row
, int col
, bool sendEvent
)
516 if ( m_selectionMode
== wxGrid::wxGridSelectRows
)
518 SelectBlock(row
, 0, row
, m_grid
->GetNumberCols() - 1 );
521 else if ( m_selectionMode
== wxGrid::wxGridSelectColumns
)
523 SelectBlock(0, col
, m_grid
->GetNumberRows() - 1, col
);
526 else if ( IsInSelection ( row
, col
) )
528 m_cellSelection
.Add( wxGridCellCoords( row
, col
) );
531 wxRect r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, col
),
532 wxGridCellCoords( row
, col
) );
533 if ( !m_grid
->GetBatchCount() )
534 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
537 wxGridEvent
gridEvt( m_grid
->GetId(),
538 wxEVT_GRID_SELECT_CELL
,
541 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
544 void wxGridSelection::ToggleCellSelection( int row
, int col
)
546 // if the cell is not selected, select it
547 if ( !IsInSelection ( row
, col
) )
549 SelectCell( row
, col
);
553 // otherwise deselect it. This can be simple or more or
554 // less difficult, depending on how the cell is selected.
557 // The simplest case: The cell is contained in m_cellSelection
558 // Then it can't be contained in rows/cols/block (since those
559 // would remove the cell from m_cellSelection on creation), so
560 // we just have to remove it from m_cellSelection.
562 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
564 count
= m_cellSelection
.GetCount();
565 for ( n
= 0; n
< count
; n
++ )
567 wxGridCellCoords
& coords
= m_cellSelection
[n
];
568 if ( row
== coords
.GetRow() && col
== coords
.GetCol() )
571 r
= m_grid
->BlockToDeviceRect( m_cellSelection
[n
],
572 m_cellSelection
[n
] );
573 m_cellSelection
.RemoveAt(n
);
575 if ( !m_grid
->GetBatchCount() )
576 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
580 wxGridEvent
gridEvt( m_grid
->GetId(),
581 wxEVT_GRID_SELECT_CELL
,
583 row
, col
, -1, -1, FALSE
);
584 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
589 // The most difficult case: The cell is member of one or even several
590 // blocks. Split each such block in up to 4 new parts, that don't
591 // contain the cell to be selected, like this:
592 // |---------------------------|
596 // |---------------------------|
597 // | part 3 |x| part 4 |
598 // |---------------------------|
602 // |---------------------------|
603 // (The x marks the newly deselected cell).
604 // Note: in row selection mode, we only need part1 and part2;
605 // in column selection mode, we only need part 3 and part4,
606 // which are expanded to whole columns automatically!
608 count
= m_blockSelectionTopLeft
.GetCount();
609 for ( n
= 0; n
< count
; n
++ )
611 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
612 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
613 int topRow
= coords1
.GetRow();
614 int leftCol
= coords1
.GetCol();
615 int bottomRow
= coords2
.GetRow();
616 int rightCol
= coords2
.GetCol();
617 if ( BlockContainsCell( topRow
, leftCol
, bottomRow
, rightCol
,
621 m_blockSelectionTopLeft
.RemoveAt(n
);
622 m_blockSelectionBottomRight
.RemoveAt(n
);
624 // add up to 4 smaller blocks and set update region
625 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
628 SelectBlock( topRow
, leftCol
,
629 row
- 1, rightCol
, 0, FALSE
);
630 if ( bottomRow
> row
)
631 SelectBlock( row
+ 1, leftCol
,
632 bottomRow
, rightCol
, 0, FALSE
);
634 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
637 SelectBlock( row
, leftCol
, row
, col
- 1, 0, FALSE
);
638 if ( rightCol
> col
)
639 SelectBlock( row
, col
+ 1, row
, rightCol
, 0, FALSE
);
644 // remove a cell from a row, adding up to two new blocks
645 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
647 count
= m_rowSelection
.GetCount();
648 for ( n
= 0; n
< count
; n
++ )
650 if ( m_rowSelection
[n
] == row
)
652 m_rowSelection
.RemoveAt(n
);
654 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
657 SelectBlock( row
, 0, row
, col
- 1, 0, FALSE
);
658 if ( col
< m_grid
->GetNumberCols() - 1 )
659 SelectBlock( row
, col
+ 1,
660 row
, m_grid
->GetNumberCols() - 1,
667 // remove a cell from a column, adding up to two new blocks
668 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
670 count
= m_colSelection
.GetCount();
671 for ( n
= 0; n
< count
; n
++ )
673 if ( m_colSelection
[n
] == col
)
675 m_colSelection
.RemoveAt(n
);
677 if (m_selectionMode
== wxGrid::wxGridSelectCells
)
680 SelectBlock( 0, col
, row
- 1, col
, 0, FALSE
);
681 if ( row
< m_grid
->GetNumberRows() - 1 )
682 SelectBlock( row
+ 1, col
,
683 m_grid
->GetNumberRows() - 1, col
,
690 // Refresh the screen and send the event; according to m_selectionMode,
691 // we need to either update only the cell, or the whole row/column.
693 switch (m_selectionMode
)
695 case wxGrid::wxGridSelectCells
:
697 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, col
),
698 wxGridCellCoords( row
, col
) );
699 if ( !m_grid
->GetBatchCount() )
700 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
701 wxGridEvent
gridEvt( m_grid
->GetId(),
702 wxEVT_GRID_SELECT_CELL
,
704 row
, col
, -1, -1, FALSE
);
705 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
708 case wxGrid::wxGridSelectRows
:
710 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
711 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
712 if ( !m_grid
->GetBatchCount() )
713 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
714 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
715 wxEVT_GRID_RANGE_SELECT
,
717 wxGridCellCoords( row
, 0 ),
718 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ),
720 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
723 case wxGrid::wxGridSelectColumns
:
725 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
726 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
727 if ( !m_grid
->GetBatchCount() )
728 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
729 wxGridRangeSelectEvent
gridEvt( m_grid
->GetId(),
730 wxEVT_GRID_RANGE_SELECT
,
732 wxGridCellCoords( 0, col
),
733 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
),
735 m_grid
->GetEventHandler()->ProcessEvent(gridEvt
);
741 void wxGridSelection::ClearSelection()
743 // Should this send deselection events?
746 // deselect all invidiual cells and update the screen
747 if ( m_selectionMode
== wxGrid::wxGridSelectCells
)
750 while( ( n
= m_cellSelection
.GetCount() ) > 0)
754 r
= m_grid
->BlockToDeviceRect( m_cellSelection
[n
],
755 m_cellSelection
[n
] );
756 m_cellSelection
.RemoveAt(n
);
757 if ( !m_grid
->GetBatchCount() )
758 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
762 // deselect all blocks and update the screen
763 while( ( n
= m_blockSelectionTopLeft
.GetCount() ) > 0)
767 r
= m_grid
->BlockToDeviceRect( m_blockSelectionTopLeft
[n
],
768 m_blockSelectionBottomRight
[n
] );
769 m_blockSelectionTopLeft
.RemoveAt(n
);
770 m_blockSelectionBottomRight
.RemoveAt(n
);
771 if ( !m_grid
->GetBatchCount() )
772 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
775 // deselect all rows and update the screen
776 if ( m_selectionMode
!= wxGrid::wxGridSelectColumns
)
778 while( ( n
= m_rowSelection
.GetCount() ) > 0)
781 int & row
= m_rowSelection
[n
];
783 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( row
, 0 ),
784 wxGridCellCoords( row
, m_grid
->GetNumberCols() - 1 ) );
785 m_rowSelection
.RemoveAt(n
);
786 if ( !m_grid
->GetBatchCount() )
787 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
791 // deselect all columns and update the screen
792 if ( m_selectionMode
!= wxGrid::wxGridSelectRows
)
794 while( ( n
= m_colSelection
.GetCount() ) > 0)
797 int & col
= m_colSelection
[n
];
799 r
= m_grid
->BlockToDeviceRect( wxGridCellCoords( 0, col
),
800 wxGridCellCoords( m_grid
->GetNumberRows() - 1, col
) );
801 m_colSelection
.RemoveAt(n
);
802 if ( !m_grid
->GetBatchCount() )
803 ((wxWindow
*)m_grid
->m_gridWin
)->Refresh( FALSE
, &r
);
809 void wxGridSelection::UpdateRows( size_t pos
, int numRows
)
811 size_t count
= m_cellSelection
.GetCount();
813 for ( n
= 0; n
< count
; n
++ )
815 wxGridCellCoords
& coords
= m_cellSelection
[n
];
816 wxCoord row
= coords
.GetRow();
817 if ((size_t)row
>= pos
)
821 // If rows inserted, increase row counter where necessary
822 coords
.SetRow(row
+ numRows
);
824 else if (numRows
< 0)
826 // If rows deleted ...
827 if ((size_t)row
>= pos
- numRows
)
829 // ...either decrement row counter (if row still exists)...
830 coords
.SetRow(row
+ numRows
);
834 // ...or remove the attribute
835 m_cellSelection
.RemoveAt(n
);
842 count
= m_blockSelectionTopLeft
.GetCount();
843 for ( n
= 0; n
< count
; n
++ )
845 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
846 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
847 wxCoord row1
= coords1
.GetRow();
848 wxCoord row2
= coords2
.GetRow();
849 if ((size_t)row2
>= pos
)
853 // If rows inserted, increase row counter where necessary
854 coords2
.SetRow(row2
+ numRows
);
855 if ( (size_t)row1
>= pos
)
856 coords1
.SetRow(row1
+ numRows
);
858 else if (numRows
< 0)
860 // If rows deleted ...
861 if ((size_t)row2
>= pos
- numRows
)
863 // ...either decrement row counter (if row still exists)...
864 coords2
.SetRow(row2
+ numRows
);
865 if ( (size_t) row1
>= pos
)
866 coords1
.SetRow( wxMax(row1
+ numRows
, (int) pos
) );
871 if ( (size_t) row1
>= pos
)
873 // ...or remove the attribute
874 m_blockSelectionTopLeft
.RemoveAt(n
);
875 m_blockSelectionBottomRight
.RemoveAt(n
);
885 count
= m_rowSelection
.GetCount();
886 for ( n
= 0; n
< count
; n
++ )
888 int & rowOrCol
= m_rowSelection
[n
];
889 if ( (size_t)rowOrCol
>= pos
)
893 // If rows inserted, include row counter where necessary
896 else if ( numRows
< 0)
898 // If rows deleted, either decrement row counter (if row still exists)
899 if ((size_t)rowOrCol
>= pos
- numRows
)
903 m_rowSelection
.RemoveAt(n
);
911 void wxGridSelection::UpdateCols( size_t pos
, int numCols
)
913 size_t count
= m_cellSelection
.GetCount();
915 for ( n
= 0; n
< count
; n
++ )
917 wxGridCellCoords
& coords
= m_cellSelection
[n
];
918 wxCoord col
= coords
.GetCol();
919 if ((size_t)col
>= pos
)
923 // If rows inserted, increase row counter where necessary
924 coords
.SetCol(col
+ numCols
);
926 else if (numCols
< 0)
928 // If rows deleted ...
929 if ((size_t)col
>= pos
- numCols
)
931 // ...either decrement row counter (if row still exists)...
932 coords
.SetCol(col
+ numCols
);
936 // ...or remove the attribute
937 m_cellSelection
.RemoveAt(n
);
944 count
= m_blockSelectionTopLeft
.GetCount();
945 for ( n
= 0; n
< count
; n
++ )
947 wxGridCellCoords
& coords1
= m_blockSelectionTopLeft
[n
];
948 wxGridCellCoords
& coords2
= m_blockSelectionBottomRight
[n
];
949 wxCoord col1
= coords1
.GetCol();
950 wxCoord col2
= coords2
.GetCol();
951 if ((size_t)col2
>= pos
)
955 // If rows inserted, increase row counter where necessary
956 coords2
.SetCol(col2
+ numCols
);
957 if ( (size_t)col1
>= pos
)
958 coords1
.SetCol(col1
+ numCols
);
960 else if (numCols
< 0)
962 // If cols deleted ...
963 if ((size_t)col2
>= pos
- numCols
)
965 // ...either decrement col counter (if col still exists)...
966 coords2
.SetCol(col2
+ numCols
);
967 if ( (size_t) col1
>= pos
)
968 coords1
.SetCol( wxMax(col1
+ numCols
, (int) pos
) );
973 if ( (size_t) col1
>= pos
)
975 // ...or remove the attribute
976 m_blockSelectionTopLeft
.RemoveAt(n
);
977 m_blockSelectionBottomRight
.RemoveAt(n
);
987 count
= m_colSelection
.GetCount();
988 for ( n
= 0; n
< count
; n
++ )
990 int & rowOrCol
= m_colSelection
[n
];
991 if ( (size_t)rowOrCol
>= pos
)
995 // If cols inserted, include col counter where necessary
998 else if ( numCols
< 0)
1000 // If cols deleted, either decrement col counter (if col still exists)
1001 if ((size_t)rowOrCol
>= pos
- numCols
)
1002 rowOrCol
+= numCols
;
1005 m_colSelection
.RemoveAt(n
);
1013 int wxGridSelection::BlockContain( int topRow1
, int leftCol1
,
1014 int bottomRow1
, int rightCol1
,
1015 int topRow2
, int leftCol2
,
1016 int bottomRow2
, int rightCol2
)
1017 // returns 1, if Block1 contains Block2,
1018 // -1, if Block2 contains Block1,
1021 if ( topRow1
<= topRow2
&& bottomRow2
<= bottomRow1
&&
1022 leftCol1
<= leftCol2
&& rightCol2
<= rightCol1
)
1024 else if ( topRow2
<= topRow1
&& bottomRow1
<= bottomRow2
&&
1025 leftCol2
<= leftCol1
&& rightCol1
<= rightCol2
)