1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
58 const char wxGridNameStr
[] = "grid";
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_MOTIF(identifier) identifier
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
69 #define WXUNUSED_GTK(identifier) identifier
72 // Required for wxIs... functions
75 WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, wxIntegerHash
, wxIntegerEqual
,
76 wxGridFixedIndicesSet
, class WXDLLIMPEXP_ADV
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
86 //#define DEBUG_ATTR_CACHE
87 #ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits
= 0;
89 static size_t gs_nAttrCacheMisses
= 0;
92 // this struct simply combines together the default header renderers
94 // as the renderers ctors are trivial, there is no problem with making them
96 struct DefaultHeaderRenderers
98 wxGridColumnHeaderRendererDefault colRenderer
;
99 wxGridRowHeaderRendererDefault rowRenderer
;
100 wxGridCornerHeaderRendererDefault cornerRenderer
;
101 } gs_defaultHeaderRenderers
;
103 } // anonymous namespace
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
110 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
116 const size_t GRID_SCROLL_LINE_X
= 15;
117 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
119 // the size of hash tables used a bit everywhere (the max number of elements
120 // in these hash tables is the number of rows/columns)
121 const int GRID_HASH_SIZE
= 100;
123 // the minimal distance in pixels the mouse needs to move to start a drag
125 const int DRAG_SENSITIVITY
= 3;
127 } // anonymous namespace
129 #include "wx/arrimpl.cpp"
131 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
132 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK
, wxGridEvent
);
139 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK
, wxGridEvent
);
140 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK
, wxGridEvent
);
141 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK
, wxGridEvent
);
142 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG
, wxGridEvent
);
143 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK
, wxGridEvent
);
144 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK
, wxGridEvent
);
145 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK
, wxGridEvent
);
146 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK
, wxGridEvent
);
147 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE
, wxGridSizeEvent
);
148 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE
, wxGridSizeEvent
);
149 wxDEFINE_EVENT( wxEVT_GRID_COL_AUTO_SIZE
, wxGridSizeEvent
);
150 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE
, wxGridEvent
);
151 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
152 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
154 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
155 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
158 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
159 wxDEFINE_EVENT( wxEVT_GRID_TABBING
, wxGridEvent
);
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
168 // ensure that first is less or equal to second, swapping the values if
170 void EnsureFirstLessThanSecond(int& first
, int& second
)
172 if ( first
> second
)
173 wxSwap(first
, second
);
176 } // anonymous namespace
178 // ============================================================================
180 // ============================================================================
182 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
184 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
185 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
186 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
187 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
190 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
191 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
192 EVT_HEADER_DCLICK(wxID_ANY
, wxGridHeaderCtrl::OnDoubleClick
)
193 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnRightClick
)
195 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
196 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
197 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
199 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
200 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
203 wxGridOperations
& wxGridRowOperations::Dual() const
205 static wxGridColumnOperations s_colOper
;
210 wxGridOperations
& wxGridColumnOperations::Dual() const
212 static wxGridRowOperations s_rowOper
;
217 // ----------------------------------------------------------------------------
218 // wxGridCellWorker is an (almost) empty common base class for
219 // wxGridCellRenderer and wxGridCellEditor managing ref counting
220 // ----------------------------------------------------------------------------
222 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
227 wxGridCellWorker::~wxGridCellWorker()
231 // ----------------------------------------------------------------------------
232 // wxGridHeaderLabelsRenderer and related classes
233 // ----------------------------------------------------------------------------
235 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid
& grid
,
237 const wxString
& value
,
241 int textOrientation
) const
243 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
244 dc
.SetTextForeground(grid
.GetLabelTextColour());
245 dc
.SetFont(grid
.GetLabelFont());
246 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
250 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
254 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
255 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
256 rect
.GetRight(), rect
.GetBottom());
257 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
258 rect
.GetLeft(), rect
.GetBottom());
259 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
260 rect
.GetRight() + 1, rect
.GetBottom());
262 dc
.SetPen(*wxWHITE_PEN
);
263 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
264 rect
.GetLeft() + 1, rect
.GetBottom());
265 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
266 rect
.GetRight(), rect
.GetTop());
271 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
275 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
276 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
277 rect
.GetRight(), rect
.GetBottom());
278 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
279 rect
.GetRight(), rect
.GetTop());
280 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
281 rect
.GetRight() + 1, rect
.GetBottom());
283 dc
.SetPen(*wxWHITE_PEN
);
284 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
285 rect
.GetLeft(), rect
.GetBottom());
286 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
287 rect
.GetRight(), rect
.GetTop() + 1);
292 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
296 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
297 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
298 rect
.GetRight() - 1, rect
.GetTop());
299 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
300 rect
.GetLeft(), rect
.GetBottom() - 1);
301 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
302 rect
.GetRight(), rect
.GetTop());
303 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
304 rect
.GetLeft(), rect
.GetBottom());
306 dc
.SetPen(*wxWHITE_PEN
);
307 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
308 rect
.GetRight() - 1, rect
.GetTop() + 1);
309 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
310 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
321 m_isReadOnly
= Unset
;
326 m_attrkind
= wxGridCellAttr::Cell
;
328 m_sizeRows
= m_sizeCols
= 1;
329 m_overflow
= UnsetOverflow
;
331 SetDefAttr(attrDefault
);
334 wxGridCellAttr
*wxGridCellAttr::Clone() const
336 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
338 if ( HasTextColour() )
339 attr
->SetTextColour(GetTextColour());
340 if ( HasBackgroundColour() )
341 attr
->SetBackgroundColour(GetBackgroundColour());
343 attr
->SetFont(GetFont());
344 if ( HasAlignment() )
345 attr
->SetAlignment(m_hAlign
, m_vAlign
);
347 attr
->SetSize( m_sizeRows
, m_sizeCols
);
351 attr
->SetRenderer(m_renderer
);
352 m_renderer
->IncRef();
356 attr
->SetEditor(m_editor
);
363 attr
->SetOverflow( m_overflow
== Overflow
);
364 attr
->SetKind( m_attrkind
);
369 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
371 if ( !HasTextColour() && mergefrom
->HasTextColour() )
372 SetTextColour(mergefrom
->GetTextColour());
373 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
374 SetBackgroundColour(mergefrom
->GetBackgroundColour());
375 if ( !HasFont() && mergefrom
->HasFont() )
376 SetFont(mergefrom
->GetFont());
377 if ( !HasAlignment() && mergefrom
->HasAlignment() )
380 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
381 SetAlignment(hAlign
, vAlign
);
383 if ( !HasSize() && mergefrom
->HasSize() )
384 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
386 // Directly access member functions as GetRender/Editor don't just return
387 // m_renderer/m_editor
389 // Maybe add support for merge of Render and Editor?
390 if (!HasRenderer() && mergefrom
->HasRenderer() )
392 m_renderer
= mergefrom
->m_renderer
;
393 m_renderer
->IncRef();
395 if ( !HasEditor() && mergefrom
->HasEditor() )
397 m_editor
= mergefrom
->m_editor
;
400 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
401 SetReadOnly(mergefrom
->IsReadOnly());
403 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
404 SetOverflow(mergefrom
->GetOverflow());
406 SetDefAttr(mergefrom
->m_defGridAttr
);
409 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
411 // The size of a cell is normally 1,1
413 // If this cell is larger (2,2) then this is the top left cell
414 // the other cells that will be covered (lower right cells) must be
415 // set to negative or zero values such that
416 // row + num_rows of the covered cell points to the larger cell (this cell)
417 // same goes for the col + num_cols.
419 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
421 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
422 !((num_rows
<= 0) && (num_cols
> 0)) ||
423 !((num_rows
== 0) && (num_cols
== 0))),
424 wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values"));
426 m_sizeRows
= num_rows
;
427 m_sizeCols
= num_cols
;
430 const wxColour
& wxGridCellAttr::GetTextColour() const
436 else if (m_defGridAttr
&& m_defGridAttr
!= this)
438 return m_defGridAttr
->GetTextColour();
442 wxFAIL_MSG(wxT("Missing default cell attribute"));
447 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
449 if (HasBackgroundColour())
453 else if (m_defGridAttr
&& m_defGridAttr
!= this)
455 return m_defGridAttr
->GetBackgroundColour();
459 wxFAIL_MSG(wxT("Missing default cell attribute"));
464 const wxFont
& wxGridCellAttr::GetFont() const
470 else if (m_defGridAttr
&& m_defGridAttr
!= this)
472 return m_defGridAttr
->GetFont();
476 wxFAIL_MSG(wxT("Missing default cell attribute"));
481 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
490 else if (m_defGridAttr
&& m_defGridAttr
!= this)
492 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
496 wxFAIL_MSG(wxT("Missing default cell attribute"));
500 void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
502 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
505 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
509 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
512 *num_rows
= m_sizeRows
;
514 *num_cols
= m_sizeCols
;
517 // GetRenderer and GetEditor use a slightly different decision path about
518 // which attribute to use. If a non-default attr object has one then it is
519 // used, otherwise the default editor or renderer is fetched from the grid and
520 // used. It should be the default for the data type of the cell. If it is
521 // NULL (because the table has a type that the grid does not have in its
522 // registry), then the grid's default editor or renderer is used.
524 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
526 wxGridCellRenderer
*renderer
= NULL
;
528 if ( m_renderer
&& this != m_defGridAttr
)
530 // use the cells renderer if it has one
531 renderer
= m_renderer
;
534 else // no non-default cell renderer
536 // get default renderer for the data type
539 // GetDefaultRendererForCell() will do IncRef() for us
540 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
543 if ( renderer
== NULL
)
545 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
547 // if we still don't have one then use the grid default
548 // (no need for IncRef() here neither)
549 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
551 else // default grid attr
553 // use m_renderer which we had decided not to use initially
554 renderer
= m_renderer
;
561 // we're supposed to always find something
562 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
567 // same as above, except for s/renderer/editor/g
568 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
570 wxGridCellEditor
*editor
= NULL
;
572 if ( m_editor
&& this != m_defGridAttr
)
574 // use the cells editor if it has one
578 else // no non default cell editor
580 // get default editor for the data type
583 // GetDefaultEditorForCell() will do IncRef() for us
584 editor
= grid
->GetDefaultEditorForCell(row
, col
);
587 if ( editor
== NULL
)
589 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
591 // if we still don't have one then use the grid default
592 // (no need for IncRef() here neither)
593 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
595 else // default grid attr
597 // use m_editor which we had decided not to use initially
605 // we're supposed to always find something
606 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
611 // ----------------------------------------------------------------------------
612 // wxGridCellAttrData
613 // ----------------------------------------------------------------------------
615 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
617 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
618 // touch attribute's reference counting explicitly, since this
619 // is managed by class wxGridCellWithAttr
620 int n
= FindIndex(row
, col
);
621 if ( n
== wxNOT_FOUND
)
626 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
628 //else: nothing to do
630 else // we already have an attribute for this cell
634 // change the attribute
635 m_attrs
[(size_t)n
].ChangeAttr(attr
);
639 // remove this attribute
640 m_attrs
.RemoveAt((size_t)n
);
645 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
647 wxGridCellAttr
*attr
= NULL
;
649 int n
= FindIndex(row
, col
);
650 if ( n
!= wxNOT_FOUND
)
652 attr
= m_attrs
[(size_t)n
].attr
;
659 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
661 size_t count
= m_attrs
.GetCount();
662 for ( size_t n
= 0; n
< count
; n
++ )
664 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
665 wxCoord row
= coords
.GetRow();
666 if ((size_t)row
>= pos
)
670 // If rows inserted, include row counter where necessary
671 coords
.SetRow(row
+ numRows
);
673 else if (numRows
< 0)
675 // If rows deleted ...
676 if ((size_t)row
>= pos
- numRows
)
678 // ...either decrement row counter (if row still exists)...
679 coords
.SetRow(row
+ numRows
);
683 // ...or remove the attribute
693 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
695 size_t count
= m_attrs
.GetCount();
696 for ( size_t n
= 0; n
< count
; n
++ )
698 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
699 wxCoord col
= coords
.GetCol();
700 if ( (size_t)col
>= pos
)
704 // If rows inserted, include row counter where necessary
705 coords
.SetCol(col
+ numCols
);
707 else if (numCols
< 0)
709 // If rows deleted ...
710 if ((size_t)col
>= pos
- numCols
)
712 // ...either decrement row counter (if row still exists)...
713 coords
.SetCol(col
+ numCols
);
717 // ...or remove the attribute
727 int wxGridCellAttrData::FindIndex(int row
, int col
) const
729 size_t count
= m_attrs
.GetCount();
730 for ( size_t n
= 0; n
< count
; n
++ )
732 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
733 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
742 // ----------------------------------------------------------------------------
743 // wxGridRowOrColAttrData
744 // ----------------------------------------------------------------------------
746 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
748 size_t count
= m_attrs
.GetCount();
749 for ( size_t n
= 0; n
< count
; n
++ )
751 m_attrs
[n
]->DecRef();
755 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
757 wxGridCellAttr
*attr
= NULL
;
759 int n
= m_rowsOrCols
.Index(rowOrCol
);
760 if ( n
!= wxNOT_FOUND
)
762 attr
= m_attrs
[(size_t)n
];
769 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
771 int i
= m_rowsOrCols
.Index(rowOrCol
);
772 if ( i
== wxNOT_FOUND
)
776 // store the new attribute, taking its ownership
777 m_rowsOrCols
.Add(rowOrCol
);
782 else // we have an attribute for this row or column
784 size_t n
= (size_t)i
;
786 // notice that this code works correctly even when the old attribute is
787 // the same as the new one: as we own of it, we must call DecRef() on
788 // it in any case and this won't result in destruction of the new
789 // attribute if it's the same as old one because it must have ref count
790 // of at least 2 to be passed to us while we keep a reference to it too
791 m_attrs
[n
]->DecRef();
795 // replace the attribute with the new one
798 else // remove the attribute
800 m_rowsOrCols
.RemoveAt(n
);
806 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
808 size_t count
= m_attrs
.GetCount();
809 for ( size_t n
= 0; n
< count
; n
++ )
811 int & rowOrCol
= m_rowsOrCols
[n
];
812 if ( (size_t)rowOrCol
>= pos
)
814 if ( numRowsOrCols
> 0 )
816 // If rows inserted, include row counter where necessary
817 rowOrCol
+= numRowsOrCols
;
819 else if ( numRowsOrCols
< 0)
821 // If rows deleted, either decrement row counter (if row still exists)
822 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
823 rowOrCol
+= numRowsOrCols
;
826 m_rowsOrCols
.RemoveAt(n
);
827 m_attrs
[n
]->DecRef();
837 // ----------------------------------------------------------------------------
838 // wxGridCellAttrProvider
839 // ----------------------------------------------------------------------------
841 wxGridCellAttrProvider::wxGridCellAttrProvider()
846 wxGridCellAttrProvider::~wxGridCellAttrProvider()
851 void wxGridCellAttrProvider::InitData()
853 m_data
= new wxGridCellAttrProviderData
;
856 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
857 wxGridCellAttr::wxAttrKind kind
) const
859 wxGridCellAttr
*attr
= NULL
;
864 case (wxGridCellAttr::Any
):
865 // Get cached merge attributes.
866 // Currently not used as no cache implemented as not mutable
867 // attr = m_data->m_mergeAttr.GetAttr(row, col);
870 // Basically implement old version.
871 // Also check merge cache, so we don't have to re-merge every time..
872 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
873 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
874 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
876 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
878 // Two or more are non NULL
879 attr
= new wxGridCellAttr
;
880 attr
->SetKind(wxGridCellAttr::Merged
);
882 // Order is important..
885 attr
->MergeWith(attrcell
);
890 attr
->MergeWith(attrcol
);
895 attr
->MergeWith(attrrow
);
899 // store merge attr if cache implemented
901 //m_data->m_mergeAttr.SetAttr(attr, row, col);
905 // one or none is non null return it or null.
924 case (wxGridCellAttr::Cell
):
925 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
928 case (wxGridCellAttr::Col
):
929 attr
= m_data
->m_colAttrs
.GetAttr(col
);
932 case (wxGridCellAttr::Row
):
933 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
938 // (wxGridCellAttr::Default):
939 // (wxGridCellAttr::Merged):
947 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
953 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
956 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
961 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
964 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
969 m_data
->m_colAttrs
.SetAttr(attr
, col
);
972 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
976 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
978 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
982 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
986 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
988 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
992 const wxGridColumnHeaderRenderer
&
993 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
995 return gs_defaultHeaderRenderers
.colRenderer
;
998 const wxGridRowHeaderRenderer
&
999 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
1001 return gs_defaultHeaderRenderers
.rowRenderer
;
1004 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
1006 return gs_defaultHeaderRenderers
.cornerRenderer
;
1009 // ----------------------------------------------------------------------------
1011 // ----------------------------------------------------------------------------
1013 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1015 wxGridTableBase::wxGridTableBase()
1018 m_attrProvider
= NULL
;
1021 wxGridTableBase::~wxGridTableBase()
1023 delete m_attrProvider
;
1026 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1028 delete m_attrProvider
;
1029 m_attrProvider
= attrProvider
;
1032 bool wxGridTableBase::CanHaveAttributes()
1034 if ( ! GetAttrProvider() )
1036 // use the default attr provider by default
1037 SetAttrProvider(new wxGridCellAttrProvider
);
1043 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1045 if ( m_attrProvider
)
1046 return m_attrProvider
->GetAttr(row
, col
, kind
);
1051 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1053 if ( m_attrProvider
)
1056 attr
->SetKind(wxGridCellAttr::Cell
);
1057 m_attrProvider
->SetAttr(attr
, row
, col
);
1061 // as we take ownership of the pointer and don't store it, we must
1067 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1069 if ( m_attrProvider
)
1071 attr
->SetKind(wxGridCellAttr::Row
);
1072 m_attrProvider
->SetRowAttr(attr
, row
);
1076 // as we take ownership of the pointer and don't store it, we must
1082 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1084 if ( m_attrProvider
)
1086 attr
->SetKind(wxGridCellAttr::Col
);
1087 m_attrProvider
->SetColAttr(attr
, col
);
1091 // as we take ownership of the pointer and don't store it, we must
1097 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1098 size_t WXUNUSED(numRows
) )
1100 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1105 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1107 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1112 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1113 size_t WXUNUSED(numRows
) )
1115 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1120 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1121 size_t WXUNUSED(numCols
) )
1123 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1128 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1130 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1135 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1136 size_t WXUNUSED(numCols
) )
1138 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1143 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1147 // RD: Starting the rows at zero confuses users,
1148 // no matter how much it makes sense to us geeks.
1154 wxString
wxGridTableBase::GetColLabelValue( int col
)
1156 // default col labels are:
1157 // cols 0 to 25 : A-Z
1158 // cols 26 to 675 : AA-ZZ
1163 for ( n
= 1; ; n
++ )
1165 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1171 // reverse the string...
1173 for ( i
= 0; i
< n
; i
++ )
1181 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1183 return wxGRID_VALUE_STRING
;
1186 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1187 const wxString
& typeName
)
1189 return typeName
== wxGRID_VALUE_STRING
;
1192 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1194 return CanGetValueAs(row
, col
, typeName
);
1197 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1202 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1207 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1212 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1213 long WXUNUSED(value
) )
1217 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1218 double WXUNUSED(value
) )
1222 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1223 bool WXUNUSED(value
) )
1227 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1228 const wxString
& WXUNUSED(typeName
) )
1233 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1234 const wxString
& WXUNUSED(typeName
),
1235 void* WXUNUSED(value
) )
1239 //////////////////////////////////////////////////////////////////////
1241 // Message class for the grid table to send requests and notifications
1245 wxGridTableMessage::wxGridTableMessage()
1253 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1254 int commandInt1
, int commandInt2
)
1258 m_comInt1
= commandInt1
;
1259 m_comInt2
= commandInt2
;
1262 //////////////////////////////////////////////////////////////////////
1264 // A basic grid table for string data. An object of this class will
1265 // created by wxGrid if you don't specify an alternative table class.
1268 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1270 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1272 wxGridStringTable::wxGridStringTable()
1278 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1281 m_numCols
= numCols
;
1283 m_data
.Alloc( numRows
);
1286 sa
.Alloc( numCols
);
1287 sa
.Add( wxEmptyString
, numCols
);
1289 m_data
.Add( sa
, numRows
);
1292 wxString
wxGridStringTable::GetValue( int row
, int col
)
1294 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1295 (col
>= 0 && col
< GetNumberCols()),
1297 wxT("invalid row or column index in wxGridStringTable") );
1299 return m_data
[row
][col
];
1302 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1304 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1305 (col
>= 0 && col
< GetNumberCols()),
1306 wxT("invalid row or column index in wxGridStringTable") );
1308 m_data
[row
][col
] = value
;
1311 void wxGridStringTable::Clear()
1314 int numRows
, numCols
;
1316 numRows
= m_data
.GetCount();
1319 numCols
= m_data
[0].GetCount();
1321 for ( row
= 0; row
< numRows
; row
++ )
1323 for ( col
= 0; col
< numCols
; col
++ )
1325 m_data
[row
][col
] = wxEmptyString
;
1331 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1333 size_t curNumRows
= m_data
.GetCount();
1334 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1335 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1337 if ( pos
>= curNumRows
)
1339 return AppendRows( numRows
);
1343 sa
.Alloc( curNumCols
);
1344 sa
.Add( wxEmptyString
, curNumCols
);
1345 m_data
.Insert( sa
, pos
, numRows
);
1349 wxGridTableMessage
msg( this,
1350 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1354 GetView()->ProcessTableMessage( msg
);
1360 bool wxGridStringTable::AppendRows( size_t numRows
)
1362 size_t curNumRows
= m_data
.GetCount();
1363 size_t curNumCols
= ( curNumRows
> 0
1364 ? m_data
[0].GetCount()
1365 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1368 if ( curNumCols
> 0 )
1370 sa
.Alloc( curNumCols
);
1371 sa
.Add( wxEmptyString
, curNumCols
);
1374 m_data
.Add( sa
, numRows
);
1378 wxGridTableMessage
msg( this,
1379 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1382 GetView()->ProcessTableMessage( msg
);
1388 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1390 size_t curNumRows
= m_data
.GetCount();
1392 if ( pos
>= curNumRows
)
1394 wxFAIL_MSG( wxString::Format
1396 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1398 (unsigned long)numRows
,
1399 (unsigned long)curNumRows
1405 if ( numRows
> curNumRows
- pos
)
1407 numRows
= curNumRows
- pos
;
1410 if ( numRows
>= curNumRows
)
1416 m_data
.RemoveAt( pos
, numRows
);
1421 wxGridTableMessage
msg( this,
1422 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1426 GetView()->ProcessTableMessage( msg
);
1432 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1436 size_t curNumRows
= m_data
.GetCount();
1437 size_t curNumCols
= ( curNumRows
> 0
1438 ? m_data
[0].GetCount()
1439 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1441 if ( pos
>= curNumCols
)
1443 return AppendCols( numCols
);
1446 if ( !m_colLabels
.IsEmpty() )
1448 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1451 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1452 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1455 for ( row
= 0; row
< curNumRows
; row
++ )
1457 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1459 m_data
[row
].Insert( wxEmptyString
, col
);
1463 m_numCols
+= numCols
;
1467 wxGridTableMessage
msg( this,
1468 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1472 GetView()->ProcessTableMessage( msg
);
1478 bool wxGridStringTable::AppendCols( size_t numCols
)
1482 size_t curNumRows
= m_data
.GetCount();
1484 for ( row
= 0; row
< curNumRows
; row
++ )
1486 m_data
[row
].Add( wxEmptyString
, numCols
);
1489 m_numCols
+= numCols
;
1493 wxGridTableMessage
msg( this,
1494 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1497 GetView()->ProcessTableMessage( msg
);
1503 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1507 size_t curNumRows
= m_data
.GetCount();
1508 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1509 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1511 if ( pos
>= curNumCols
)
1513 wxFAIL_MSG( wxString::Format
1515 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1517 (unsigned long)numCols
,
1518 (unsigned long)curNumCols
1525 colID
= GetView()->GetColAt( pos
);
1529 if ( numCols
> curNumCols
- colID
)
1531 numCols
= curNumCols
- colID
;
1534 if ( !m_colLabels
.IsEmpty() )
1536 // m_colLabels stores just as many elements as it needs, e.g. if only
1537 // the label of the first column had been set it would have only one
1538 // element and not numCols, so account for it
1539 int numRemaining
= m_colLabels
.size() - colID
;
1540 if (numRemaining
> 0)
1541 m_colLabels
.RemoveAt( colID
, wxMin(numCols
, numRemaining
) );
1544 if ( numCols
>= curNumCols
)
1546 for ( row
= 0; row
< curNumRows
; row
++ )
1548 m_data
[row
].Clear();
1553 else // something will be left
1555 for ( row
= 0; row
< curNumRows
; row
++ )
1557 m_data
[row
].RemoveAt( colID
, numCols
);
1560 m_numCols
-= numCols
;
1565 wxGridTableMessage
msg( this,
1566 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1570 GetView()->ProcessTableMessage( msg
);
1576 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1578 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1580 // using default label
1582 return wxGridTableBase::GetRowLabelValue( row
);
1586 return m_rowLabels
[row
];
1590 wxString
wxGridStringTable::GetColLabelValue( int col
)
1592 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1594 // using default label
1596 return wxGridTableBase::GetColLabelValue( col
);
1600 return m_colLabels
[col
];
1604 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1606 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1608 int n
= m_rowLabels
.GetCount();
1611 for ( i
= n
; i
<= row
; i
++ )
1613 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1617 m_rowLabels
[row
] = value
;
1620 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1622 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1624 int n
= m_colLabels
.GetCount();
1627 for ( i
= n
; i
<= col
; i
++ )
1629 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1633 m_colLabels
[col
] = value
;
1637 //////////////////////////////////////////////////////////////////////
1638 //////////////////////////////////////////////////////////////////////
1640 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1641 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1644 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1646 m_owner
->CancelMouseCapture();
1649 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1650 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1651 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1652 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1655 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1659 // NO - don't do this because it will set both the x and y origin
1660 // coords to match the parent scrolled window and we just want to
1661 // set the y coord - MB
1663 // m_owner->PrepareDC( dc );
1666 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1667 wxPoint pt
= dc
.GetDeviceOrigin();
1668 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1670 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1671 m_owner
->DrawRowLabels( dc
, rows
);
1674 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1676 m_owner
->ProcessRowLabelMouseEvent( event
);
1679 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1681 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1685 //////////////////////////////////////////////////////////////////////
1687 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1688 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1689 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1690 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1693 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1697 // NO - don't do this because it will set both the x and y origin
1698 // coords to match the parent scrolled window and we just want to
1699 // set the x coord - MB
1701 // m_owner->PrepareDC( dc );
1704 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1705 wxPoint pt
= dc
.GetDeviceOrigin();
1706 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1707 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1709 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1711 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1712 m_owner
->DrawColLabels( dc
, cols
);
1715 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1717 m_owner
->ProcessColLabelMouseEvent( event
);
1720 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1722 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1726 //////////////////////////////////////////////////////////////////////
1728 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1729 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1730 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1731 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1734 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1738 m_owner
->DrawCornerLabel(dc
);
1741 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1743 m_owner
->ProcessCornerLabelMouseEvent( event
);
1746 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1748 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1752 //////////////////////////////////////////////////////////////////////
1754 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1755 EVT_PAINT( wxGridWindow::OnPaint
)
1756 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1757 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1758 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1759 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1760 EVT_CHAR( wxGridWindow::OnChar
)
1761 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1762 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1763 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1766 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1768 wxPaintDC
dc( this );
1769 m_owner
->PrepareDC( dc
);
1770 wxRegion reg
= GetUpdateRegion();
1771 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1772 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1774 m_owner
->DrawGridSpace( dc
);
1776 m_owner
->DrawAllGridLines( dc
, reg
);
1778 m_owner
->DrawHighlight( dc
, dirtyCells
);
1781 void wxGrid::Render( wxDC
& dc
,
1782 const wxPoint
& position
,
1784 const wxGridCellCoords
& topLeft
,
1785 const wxGridCellCoords
& bottomRight
,
1788 wxCHECK_RET( bottomRight
.GetCol() < GetNumberCols(),
1789 "Invalid right column" );
1790 wxCHECK_RET( bottomRight
.GetRow() < GetNumberRows(),
1791 "Invalid bottom row" );
1793 // store user settings and reset later
1795 // remove grid selection, don't paint selection colour
1796 // unless we have wxGRID_DRAW_SELECTION
1797 // block selections are the only ones catered for here
1798 wxGridCellCoordsArray selectedCells
;
1799 bool hasSelection
= IsSelection();
1800 if ( hasSelection
&& !( style
& wxGRID_DRAW_SELECTION
) )
1802 selectedCells
= GetSelectionBlockTopLeft();
1803 // non block selections may not have a bottom right
1804 if ( GetSelectionBlockBottomRight().size() )
1805 selectedCells
.Add( GetSelectionBlockBottomRight()[ 0 ] );
1810 // store user device origin
1811 wxCoord userOriginX
, userOriginY
;
1812 dc
.GetDeviceOrigin( &userOriginX
, &userOriginY
);
1815 double scaleUserX
, scaleUserY
;
1816 dc
.GetUserScale( &scaleUserX
, &scaleUserY
);
1818 // set defaults if necessary
1819 wxGridCellCoords
leftTop( topLeft
), rightBottom( bottomRight
);
1820 if ( leftTop
.GetCol() < 0 )
1822 if ( leftTop
.GetRow() < 0 )
1824 if ( rightBottom
.GetCol() < 0 )
1825 rightBottom
.SetCol(GetNumberCols() - 1);
1826 if ( rightBottom
.GetRow() < 0 )
1827 rightBottom
.SetRow(GetNumberRows() - 1);
1829 // get grid offset, size and cell parameters
1830 wxPoint pointOffSet
;
1832 wxGridCellCoordsArray renderCells
;
1833 wxArrayInt arrayCols
;
1834 wxArrayInt arrayRows
;
1836 GetRenderSizes( leftTop
, rightBottom
,
1837 pointOffSet
, sizeGrid
,
1839 arrayCols
, arrayRows
);
1841 // add headers/labels to dimensions
1842 if ( style
& wxGRID_DRAW_ROWS_HEADER
)
1843 sizeGrid
.x
+= GetRowLabelSize();
1844 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1845 sizeGrid
.y
+= GetColLabelSize();
1847 // get render start position in logical units
1848 wxPoint positionRender
= GetRenderPosition( dc
, position
);
1850 wxCoord originX
= dc
.LogicalToDeviceX( positionRender
.x
);
1851 wxCoord originY
= dc
.LogicalToDeviceY( positionRender
.y
);
1853 dc
.SetDeviceOrigin( originX
, originY
);
1855 SetRenderScale( dc
, positionRender
, size
, sizeGrid
);
1857 // draw row headers at specified origin
1858 if ( GetRowLabelSize() > 0 && ( style
& wxGRID_DRAW_ROWS_HEADER
) )
1860 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1862 DrawCornerLabel( dc
); // do only if both col and row labels drawn
1863 originY
+= dc
.LogicalToDeviceYRel( GetColLabelSize() );
1866 originY
-= dc
.LogicalToDeviceYRel( pointOffSet
.y
);
1867 dc
.SetDeviceOrigin( originX
, originY
);
1869 DrawRowLabels( dc
, arrayRows
);
1871 // reset for columns
1872 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1873 originY
-= dc
.LogicalToDeviceYRel( GetColLabelSize() );
1875 originY
+= dc
.LogicalToDeviceYRel( pointOffSet
.y
);
1876 // X offset so we don't overwrite row labels
1877 originX
+= dc
.LogicalToDeviceXRel( GetRowLabelSize() );
1880 // subtract col offset where startcol > 0
1881 originX
-= dc
.LogicalToDeviceXRel( pointOffSet
.x
);
1882 // no y offset for col labels, they are at the Y origin
1884 // draw column labels
1885 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1887 dc
.SetDeviceOrigin( originX
, originY
);
1888 DrawColLabels( dc
, arrayCols
);
1889 // don't overwrite the labels, increment originY
1890 originY
+= dc
.LogicalToDeviceYRel( GetColLabelSize() );
1893 // set device origin to draw grid cells and lines
1894 originY
-= dc
.LogicalToDeviceYRel( pointOffSet
.y
);
1895 dc
.SetDeviceOrigin( originX
, originY
);
1897 // draw cell area background
1898 dc
.SetBrush( GetDefaultCellBackgroundColour() );
1899 dc
.SetPen( *wxTRANSPARENT_PEN
);
1900 // subtract headers from grid area dimensions
1901 wxSize
sizeCells( sizeGrid
);
1902 if ( style
& wxGRID_DRAW_ROWS_HEADER
)
1903 sizeCells
.x
-= GetRowLabelSize();
1904 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1905 sizeCells
.y
-= GetColLabelSize();
1907 dc
.DrawRectangle( pointOffSet
, sizeCells
);
1910 DrawGridCellArea( dc
, renderCells
);
1913 if ( style
& wxGRID_DRAW_CELL_LINES
)
1915 wxRegion
regionClip( pointOffSet
.x
, pointOffSet
.y
,
1916 sizeCells
.x
, sizeCells
.y
);
1918 DrawRangeGridLines(dc
, regionClip
, renderCells
[0], renderCells
.Last());
1921 // draw render rectangle bounding lines
1922 DoRenderBox( dc
, style
,
1923 pointOffSet
, sizeCells
,
1924 leftTop
, rightBottom
);
1926 // restore user setings
1927 dc
.SetDeviceOrigin( userOriginX
, userOriginY
);
1928 dc
.SetUserScale( scaleUserX
, scaleUserY
);
1930 if ( selectedCells
.size() && !( style
& wxGRID_DRAW_SELECTION
) )
1932 SelectBlock( selectedCells
[ 0 ].GetRow(),
1933 selectedCells
[ 0 ].GetCol(),
1934 selectedCells
[ selectedCells
.size() -1 ].GetRow(),
1935 selectedCells
[ selectedCells
.size() -1 ].GetCol() );
1940 wxGrid::SetRenderScale(wxDC
& dc
,
1941 const wxPoint
& pos
, const wxSize
& size
,
1942 const wxSize
& sizeGrid
)
1944 double scaleX
, scaleY
;
1947 if ( size
.GetWidth() != wxDefaultSize
.GetWidth() ) // size.x was specified
1948 sizeTemp
.SetWidth( size
.GetWidth() );
1950 sizeTemp
.SetWidth( dc
.DeviceToLogicalXRel( dc
.GetSize().GetWidth() )
1953 if ( size
.GetHeight() != wxDefaultSize
.GetHeight() ) // size.y was specified
1954 sizeTemp
.SetHeight( size
.GetHeight() );
1956 sizeTemp
.SetHeight( dc
.DeviceToLogicalYRel( dc
.GetSize().GetHeight() )
1959 scaleX
= (double)( (double) sizeTemp
.GetWidth() / (double) sizeGrid
.GetWidth() );
1960 scaleY
= (double)( (double) sizeTemp
.GetHeight() / (double) sizeGrid
.GetHeight() );
1962 dc
.SetUserScale( wxMin( scaleX
, scaleY
), wxMin( scaleX
, scaleY
) );
1965 // get grid rendered size, origin offset and fill cell arrays
1966 void wxGrid::GetRenderSizes( const wxGridCellCoords
& topLeft
,
1967 const wxGridCellCoords
& bottomRight
,
1968 wxPoint
& pointOffSet
, wxSize
& sizeGrid
,
1969 wxGridCellCoordsArray
& renderCells
,
1970 wxArrayInt
& arrayCols
, wxArrayInt
& arrayRows
)
1974 sizeGrid
.SetWidth( 0 );
1975 sizeGrid
.SetHeight( 0 );
1979 wxGridSizesInfo sizeinfo
= GetColSizes();
1980 for ( col
= 0; col
<= bottomRight
.GetCol(); col
++ )
1982 if ( col
< topLeft
.GetCol() )
1984 pointOffSet
.x
+= sizeinfo
.GetSize( col
);
1988 for ( row
= topLeft
.GetRow(); row
<= bottomRight
.GetRow(); row
++ )
1990 renderCells
.Add( wxGridCellCoords( row
, col
));
1991 arrayRows
.Add( row
); // column labels rendered in DrawColLabels
1993 arrayCols
.Add( col
); // row labels rendered in DrawRowLabels
1994 sizeGrid
.x
+= sizeinfo
.GetSize( col
);
1998 sizeinfo
= GetRowSizes();
1999 for ( row
= 0; row
<= bottomRight
.GetRow(); row
++ )
2001 if ( row
< topLeft
.GetRow() )
2002 pointOffSet
.y
+= sizeinfo
.GetSize( row
);
2004 sizeGrid
.y
+= sizeinfo
.GetSize( row
);
2008 // get render start position
2009 // if position not specified use dc draw extents MaxX and MaxY
2010 wxPoint
wxGrid::GetRenderPosition( wxDC
& dc
, const wxPoint
& position
)
2012 wxPoint
positionRender( position
);
2014 if ( !positionRender
.IsFullySpecified() )
2016 if ( positionRender
.x
== wxDefaultPosition
.x
)
2017 positionRender
.x
= dc
.MaxX();
2019 if ( positionRender
.y
== wxDefaultPosition
.y
)
2020 positionRender
.y
= dc
.MaxY();
2023 return positionRender
;
2026 // draw render rectangle bounding lines
2027 // useful where there is multi cell row or col clipping and no cell border
2028 void wxGrid::DoRenderBox( wxDC
& dc
, const int& style
,
2029 const wxPoint
& pointOffSet
,
2030 const wxSize
& sizeCells
,
2031 const wxGridCellCoords
& topLeft
,
2032 const wxGridCellCoords
& bottomRight
)
2034 if ( !( style
& wxGRID_DRAW_BOX_RECT
) )
2037 int bottom
= pointOffSet
.y
+ sizeCells
.GetY(),
2038 right
= pointOffSet
.x
+ sizeCells
.GetX() - 1;
2040 // horiz top line if we are not drawing column header/labels
2041 if ( !( style
& wxGRID_DRAW_COLS_HEADER
) )
2043 int left
= pointOffSet
.x
;
2044 left
+= ( style
& wxGRID_DRAW_COLS_HEADER
)
2045 ? - GetRowLabelSize() : 0;
2046 dc
.SetPen( GetRowGridLinePen( topLeft
.GetRow() ) );
2053 // horiz bottom line
2054 dc
.SetPen( GetRowGridLinePen( bottomRight
.GetRow() ) );
2055 dc
.DrawLine( pointOffSet
.x
, bottom
- 1, right
, bottom
- 1 );
2057 // left vertical line if we are not drawing row header/labels
2058 if ( !( style
& wxGRID_DRAW_ROWS_HEADER
) )
2060 int top
= pointOffSet
.y
;
2061 top
+= ( style
& wxGRID_DRAW_COLS_HEADER
)
2062 ? - GetColLabelSize() : 0;
2063 dc
.SetPen( GetColGridLinePen( topLeft
.GetCol() ) );
2064 dc
.DrawLine( pointOffSet
.x
-1,
2070 // right vertical line
2071 dc
.SetPen( GetColGridLinePen( bottomRight
.GetCol() ) );
2072 dc
.DrawLine( right
, pointOffSet
.y
, right
, bottom
- 1 );
2075 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2077 wxWindow::ScrollWindow( dx
, dy
, rect
);
2078 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
2079 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
2082 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2084 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
2087 m_owner
->ProcessGridCellMouseEvent( event
);
2090 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
2092 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
2096 // This seems to be required for wxMotif/wxGTK otherwise the mouse
2097 // cursor must be in the cell edit control to get key events
2099 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2101 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2105 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
2107 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2111 void wxGridWindow::OnChar( wxKeyEvent
& event
)
2113 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2117 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
2121 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
2123 // and if we have any selection, it has to be repainted, because it
2124 // uses different colour when the grid is not focused:
2125 if ( m_owner
->IsSelection() )
2131 // NB: Note that this code is in "else" branch only because the other
2132 // branch refreshes everything and so there's no point in calling
2133 // Refresh() again, *not* because it should only be done if
2134 // !IsSelection(). If the above code is ever optimized to refresh
2135 // only selected area, this needs to be moved out of the "else"
2136 // branch so that it's always executed.
2138 // current cell cursor {dis,re}appears on focus change:
2139 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
2140 m_owner
->GetGridCursorCol());
2141 const wxRect cursor
=
2142 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
2143 Refresh(true, &cursor
);
2146 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2150 #define internalXToCol(x) XToCol(x, true)
2151 #define internalYToRow(y) YToRow(y, true)
2153 /////////////////////////////////////////////////////////////////////
2155 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2156 EVT_PAINT( wxGrid::OnPaint
)
2157 EVT_SIZE( wxGrid::OnSize
)
2158 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2159 EVT_KEY_UP( wxGrid::OnKeyUp
)
2160 EVT_CHAR ( wxGrid::OnChar
)
2161 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2162 EVT_COMMAND(wxID_ANY
, wxEVT_GRID_HIDE_EDITOR
, wxGrid::OnHideEditor
)
2165 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
2166 const wxPoint
& pos
, const wxSize
& size
,
2167 long style
, const wxString
& name
)
2169 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
2170 style
| wxWANTS_CHARS
, name
))
2173 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
2174 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
2177 SetInitialSize(size
);
2186 m_winCapture
->ReleaseMouse();
2188 // Ensure that the editor control is destroyed before the grid is,
2189 // otherwise we crash later when the editor tries to do something with the
2190 // half destroyed grid
2191 HideCellEditControl();
2193 // Must do this or ~wxScrollHelper will pop the wrong event handler
2194 SetTargetWindow(this);
2196 wxSafeDecRef(m_defaultCellAttr
);
2198 #ifdef DEBUG_ATTR_CACHE
2199 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2200 wxPrintf(wxT("wxGrid attribute cache statistics: "
2201 "total: %u, hits: %u (%u%%)\n"),
2202 total
, gs_nAttrCacheHits
,
2203 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2206 // if we own the table, just delete it, otherwise at least don't leave it
2207 // with dangling view pointer
2210 else if ( m_table
&& m_table
->GetView() == this )
2211 m_table
->SetView(NULL
);
2213 delete m_typeRegistry
;
2216 delete m_setFixedRows
;
2217 delete m_setFixedCols
;
2221 // ----- internal init and update functions
2224 // NOTE: If using the default visual attributes works everywhere then this can
2225 // be removed as well as the #else cases below.
2226 #define _USE_VISATTR 0
2228 void wxGrid::Create()
2230 // create the type registry
2231 m_typeRegistry
= new wxGridTypeRegistry
;
2233 m_cellEditCtrlEnabled
= false;
2235 m_defaultCellAttr
= new wxGridCellAttr();
2237 // Set default cell attributes
2238 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2239 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
2240 m_defaultCellAttr
->SetFont(GetFont());
2241 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
2242 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2243 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2246 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
2247 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
2249 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
2250 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
2253 m_defaultCellAttr
->SetTextColour(
2254 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
2255 m_defaultCellAttr
->SetBackgroundColour(
2256 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
2261 m_currentCellCoords
= wxGridNoCellCoords
;
2263 // subwindow components that make up the wxGrid
2264 m_rowLabelWin
= new wxGridRowLabelWindow(this);
2265 CreateColumnWindow();
2266 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
2267 m_gridWin
= new wxGridWindow( this );
2269 SetTargetWindow( m_gridWin
);
2272 wxColour gfg
= gva
.colFg
;
2273 wxColour gbg
= gva
.colBg
;
2274 wxColour lfg
= lva
.colFg
;
2275 wxColour lbg
= lva
.colBg
;
2277 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2278 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
2279 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2280 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
2283 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
2284 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
2285 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
2286 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
2287 m_colWindow
->SetOwnForegroundColour(lfg
);
2288 m_colWindow
->SetOwnBackgroundColour(lbg
);
2290 m_gridWin
->SetOwnForegroundColour(gfg
);
2291 m_gridWin
->SetOwnBackgroundColour(gbg
);
2293 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2294 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
2296 // now that we have the grid window, use its font to compute the default
2298 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2299 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2300 m_defaultRowHeight
+= 8;
2302 m_defaultRowHeight
+= 4;
2307 void wxGrid::CreateColumnWindow()
2309 if ( m_useNativeHeader
)
2311 m_colWindow
= new wxGridHeaderCtrl(this);
2312 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2314 else // draw labels ourselves
2316 m_colWindow
= new wxGridColLabelWindow(this);
2317 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2321 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2322 wxGridSelectionModes selmode
)
2324 wxCHECK_MSG( !m_created
,
2326 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2328 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2331 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2333 wxCHECK_RET( m_created
,
2334 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2336 m_selection
->SetSelectionMode( selmode
);
2339 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2341 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2342 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2344 return m_selection
->GetSelectionMode();
2348 wxGrid::SetTable(wxGridTableBase
*table
,
2350 wxGrid::wxGridSelectionModes selmode
)
2352 bool checkSelection
= false;
2355 // stop all processing
2360 m_table
->SetView(0);
2366 wxDELETE(m_selection
);
2371 checkSelection
= true;
2373 // kill row and column size arrays
2374 m_colWidths
.Empty();
2375 m_colRights
.Empty();
2376 m_rowHeights
.Empty();
2377 m_rowBottoms
.Empty();
2382 m_numRows
= table
->GetNumberRows();
2383 m_numCols
= table
->GetNumberCols();
2385 if ( m_useNativeHeader
)
2386 GetGridColHeader()->SetColumnCount(m_numCols
);
2389 m_table
->SetView( this );
2390 m_ownTable
= takeOwnership
;
2391 m_selection
= new wxGridSelection( this, selmode
);
2394 // If the newly set table is smaller than the
2395 // original one current cell and selection regions
2396 // might be invalid,
2397 m_selectedBlockCorner
= wxGridNoCellCoords
;
2398 m_currentCellCoords
=
2399 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2400 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2401 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2402 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2404 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2405 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2408 m_selectedBlockBottomRight
=
2409 wxGridCellCoords(wxMin(m_numRows
,
2410 m_selectedBlockBottomRight
.GetRow()),
2412 m_selectedBlockBottomRight
.GetCol()));
2419 InvalidateBestSize();
2428 m_cornerLabelWin
= NULL
;
2429 m_rowLabelWin
= NULL
;
2437 m_defaultCellAttr
= NULL
;
2438 m_typeRegistry
= NULL
;
2439 m_winCapture
= NULL
;
2441 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2442 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2445 m_setFixedCols
= NULL
;
2448 m_attrCache
.row
= -1;
2449 m_attrCache
.col
= -1;
2450 m_attrCache
.attr
= NULL
;
2452 m_labelFont
= GetFont();
2453 m_labelFont
.SetWeight( wxBOLD
);
2455 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2456 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2458 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2459 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2460 m_colLabelTextOrientation
= wxHORIZONTAL
;
2462 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2463 m_defaultRowHeight
= 0; // this will be initialized after creation
2465 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2466 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2468 m_gridLineColour
= wxColour( 192,192,192 );
2469 m_gridLinesEnabled
= true;
2470 m_gridLinesClipHorz
=
2471 m_gridLinesClipVert
= true;
2472 m_cellHighlightColour
= *wxBLACK
;
2473 m_cellHighlightPenWidth
= 2;
2474 m_cellHighlightROPenWidth
= 1;
2476 m_canDragColMove
= false;
2478 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2479 m_winCapture
= NULL
;
2480 m_canDragRowSize
= true;
2481 m_canDragColSize
= true;
2482 m_canDragGridSize
= true;
2483 m_canDragCell
= false;
2485 m_dragRowOrCol
= -1;
2486 m_isDragging
= false;
2487 m_startDragPos
= wxDefaultPosition
;
2489 m_sortCol
= wxNOT_FOUND
;
2490 m_sortIsAscending
= true;
2493 m_nativeColumnLabels
= false;
2495 m_waitForSlowClick
= false;
2497 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2498 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2500 m_currentCellCoords
= wxGridNoCellCoords
;
2502 m_selectedBlockTopLeft
=
2503 m_selectedBlockBottomRight
=
2504 m_selectedBlockCorner
= wxGridNoCellCoords
;
2506 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2507 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2509 m_editable
= true; // default for whole grid
2511 m_inOnKeyDown
= false;
2517 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2518 // we don't need to call it neither as the scroll position is (0, 0) right
2519 // now anyhow, so just set the parameters directly
2520 m_xScrollPixelsPerLine
= GRID_SCROLL_LINE_X
;
2521 m_yScrollPixelsPerLine
= GRID_SCROLL_LINE_Y
;
2523 m_tabBehaviour
= Tab_Stop
;
2526 // ----------------------------------------------------------------------------
2527 // the idea is to call these functions only when necessary because they create
2528 // quite big arrays which eat memory mostly unnecessary - in particular, if
2529 // default widths/heights are used for all rows/columns, we may not use these
2532 // with some extra code, it should be possible to only store the widths/heights
2533 // different from default ones (resulting in space savings for huge grids) but
2534 // this is not done currently
2535 // ----------------------------------------------------------------------------
2537 void wxGrid::InitRowHeights()
2539 m_rowHeights
.Empty();
2540 m_rowBottoms
.Empty();
2542 m_rowHeights
.Alloc( m_numRows
);
2543 m_rowBottoms
.Alloc( m_numRows
);
2545 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2548 for ( int i
= 0; i
< m_numRows
; i
++ )
2550 rowBottom
+= m_defaultRowHeight
;
2551 m_rowBottoms
.Add( rowBottom
);
2555 void wxGrid::InitColWidths()
2557 m_colWidths
.Empty();
2558 m_colRights
.Empty();
2560 m_colWidths
.Alloc( m_numCols
);
2561 m_colRights
.Alloc( m_numCols
);
2563 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2565 for ( int i
= 0; i
< m_numCols
; i
++ )
2567 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2568 m_colRights
.Add( colRight
);
2572 int wxGrid::GetColWidth(int col
) const
2574 if ( m_colWidths
.IsEmpty() )
2575 return m_defaultColWidth
;
2577 // a negative width indicates a hidden column
2578 return m_colWidths
[col
] > 0 ? m_colWidths
[col
] : 0;
2581 int wxGrid::GetColLeft(int col
) const
2583 if ( m_colRights
.IsEmpty() )
2584 return GetColPos( col
) * m_defaultColWidth
;
2586 return m_colRights
[col
] - GetColWidth(col
);
2589 int wxGrid::GetColRight(int col
) const
2591 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2595 int wxGrid::GetRowHeight(int row
) const
2597 // no custom heights / hidden rows
2598 if ( m_rowHeights
.IsEmpty() )
2599 return m_defaultRowHeight
;
2601 // a negative height indicates a hidden row
2602 return m_rowHeights
[row
] > 0 ? m_rowHeights
[row
] : 0;
2605 int wxGrid::GetRowTop(int row
) const
2607 if ( m_rowBottoms
.IsEmpty() )
2608 return row
* m_defaultRowHeight
;
2610 return m_rowBottoms
[row
] - GetRowHeight(row
);
2613 int wxGrid::GetRowBottom(int row
) const
2615 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2616 : m_rowBottoms
[row
];
2619 void wxGrid::CalcDimensions()
2621 // compute the size of the scrollable area
2622 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2623 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2628 // take into account editor if shown
2629 if ( IsCellEditControlShown() )
2632 int r
= m_currentCellCoords
.GetRow();
2633 int c
= m_currentCellCoords
.GetCol();
2634 int x
= GetColLeft(c
);
2635 int y
= GetRowTop(r
);
2637 // how big is the editor
2638 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2639 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2640 editor
->GetControl()->GetSize(&w2
, &h2
);
2651 // preserve (more or less) the previous position
2653 GetViewStart( &x
, &y
);
2655 // ensure the position is valid for the new scroll ranges
2657 x
= wxMax( w
- 1, 0 );
2659 y
= wxMax( h
- 1, 0 );
2661 // update the virtual size and refresh the scrollbars to reflect it
2662 m_gridWin
->SetVirtualSize(w
, h
);
2666 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2667 // still must reposition the children
2671 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2673 wxSize
sizeGridWin(size
);
2674 sizeGridWin
.x
-= m_rowLabelWidth
;
2675 sizeGridWin
.y
-= m_colLabelHeight
;
2680 void wxGrid::CalcWindowSizes()
2682 // escape if the window is has not been fully created yet
2684 if ( m_cornerLabelWin
== NULL
)
2688 GetClientSize( &cw
, &ch
);
2690 // the grid may be too small to have enough space for the labels yet, don't
2691 // size the windows to negative sizes in this case
2692 int gw
= cw
- m_rowLabelWidth
;
2693 int gh
= ch
- m_colLabelHeight
;
2699 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2700 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2702 if ( m_colWindow
&& m_colWindow
->IsShown() )
2703 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2705 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2706 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2708 if ( m_gridWin
&& m_gridWin
->IsShown() )
2709 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2712 // this is called when the grid table sends a message
2713 // to indicate that it has been redimensioned
2715 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2718 bool result
= false;
2720 // Clear the attribute cache as the attribute might refer to a different
2721 // cell than stored in the cache after adding/removing rows/columns.
2724 // By the same reasoning, the editor should be dismissed if columns are
2725 // added or removed. And for consistency, it should IMHO always be
2726 // removed, not only if the cell "underneath" it actually changes.
2727 // For now, I intentionally do not save the editor's content as the
2728 // cell it might want to save that stuff to might no longer exist.
2729 HideCellEditControl();
2731 switch ( msg
.GetId() )
2733 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2735 size_t pos
= msg
.GetCommandInt();
2736 int numRows
= msg
.GetCommandInt2();
2738 m_numRows
+= numRows
;
2740 if ( !m_rowHeights
.IsEmpty() )
2742 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2743 m_rowBottoms
.Insert( 0, pos
, numRows
);
2747 bottom
= m_rowBottoms
[pos
- 1];
2749 for ( i
= pos
; i
< m_numRows
; i
++ )
2751 bottom
+= m_rowHeights
[i
];
2752 m_rowBottoms
[i
] = bottom
;
2756 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2758 // if we have just inserted cols into an empty grid the current
2759 // cell will be undefined...
2761 SetCurrentCell( 0, 0 );
2765 m_selection
->UpdateRows( pos
, numRows
);
2766 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2768 attrProvider
->UpdateAttrRows( pos
, numRows
);
2770 if ( !GetBatchCount() )
2773 m_rowLabelWin
->Refresh();
2779 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2781 int numRows
= msg
.GetCommandInt();
2782 int oldNumRows
= m_numRows
;
2783 m_numRows
+= numRows
;
2785 if ( !m_rowHeights
.IsEmpty() )
2787 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2788 m_rowBottoms
.Add( 0, numRows
);
2791 if ( oldNumRows
> 0 )
2792 bottom
= m_rowBottoms
[oldNumRows
- 1];
2794 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2796 bottom
+= m_rowHeights
[i
];
2797 m_rowBottoms
[i
] = bottom
;
2801 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2803 // if we have just inserted cols into an empty grid the current
2804 // cell will be undefined...
2806 SetCurrentCell( 0, 0 );
2809 if ( !GetBatchCount() )
2812 m_rowLabelWin
->Refresh();
2818 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2820 size_t pos
= msg
.GetCommandInt();
2821 int numRows
= msg
.GetCommandInt2();
2822 m_numRows
-= numRows
;
2824 if ( !m_rowHeights
.IsEmpty() )
2826 m_rowHeights
.RemoveAt( pos
, numRows
);
2827 m_rowBottoms
.RemoveAt( pos
, numRows
);
2830 for ( i
= 0; i
< m_numRows
; i
++ )
2832 h
+= m_rowHeights
[i
];
2833 m_rowBottoms
[i
] = h
;
2839 m_currentCellCoords
= wxGridNoCellCoords
;
2843 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2844 m_currentCellCoords
.Set( 0, 0 );
2848 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2849 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2852 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2854 // ifdef'd out following patch from Paul Gammans
2856 // No need to touch column attributes, unless we
2857 // removed _all_ rows, in this case, we remove
2858 // all column attributes.
2859 // I hate to do this here, but the
2860 // needed data is not available inside UpdateAttrRows.
2861 if ( !GetNumberRows() )
2862 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2866 if ( !GetBatchCount() )
2869 m_rowLabelWin
->Refresh();
2875 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2877 size_t pos
= msg
.GetCommandInt();
2878 int numCols
= msg
.GetCommandInt2();
2879 m_numCols
+= numCols
;
2881 if ( m_useNativeHeader
)
2882 GetGridColHeader()->SetColumnCount(m_numCols
);
2884 if ( !m_colAt
.IsEmpty() )
2886 //Shift the column IDs
2887 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2889 if ( m_colAt
[i
] >= (int)pos
)
2890 m_colAt
[i
] += numCols
;
2893 m_colAt
.Insert( pos
, pos
, numCols
);
2895 //Set the new columns' positions
2896 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2902 if ( !m_colWidths
.IsEmpty() )
2904 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2905 m_colRights
.Insert( 0, pos
, numCols
);
2909 right
= m_colRights
[GetColAt( pos
- 1 )];
2912 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2914 i
= GetColAt( colPos
);
2916 right
+= m_colWidths
[i
];
2917 m_colRights
[i
] = right
;
2921 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2923 // if we have just inserted cols into an empty grid the current
2924 // cell will be undefined...
2926 SetCurrentCell( 0, 0 );
2930 m_selection
->UpdateCols( pos
, numCols
);
2931 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2933 attrProvider
->UpdateAttrCols( pos
, numCols
);
2934 if ( !GetBatchCount() )
2937 m_colWindow
->Refresh();
2943 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2945 int numCols
= msg
.GetCommandInt();
2946 int oldNumCols
= m_numCols
;
2947 m_numCols
+= numCols
;
2949 if ( !m_colAt
.IsEmpty() )
2951 m_colAt
.Add( 0, numCols
);
2953 //Set the new columns' positions
2954 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2960 if ( !m_colWidths
.IsEmpty() )
2962 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2963 m_colRights
.Add( 0, numCols
);
2966 if ( oldNumCols
> 0 )
2967 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2970 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2972 i
= GetColAt( colPos
);
2974 right
+= m_colWidths
[i
];
2975 m_colRights
[i
] = right
;
2979 // Notice that this must be called after updating m_colWidths above
2980 // as the native grid control will check whether the new columns
2981 // are shown which results in accessing m_colWidths array.
2982 if ( m_useNativeHeader
)
2983 GetGridColHeader()->SetColumnCount(m_numCols
);
2985 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2987 // if we have just inserted cols into an empty grid the current
2988 // cell will be undefined...
2990 SetCurrentCell( 0, 0 );
2992 if ( !GetBatchCount() )
2995 m_colWindow
->Refresh();
3001 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3003 size_t pos
= msg
.GetCommandInt();
3004 int numCols
= msg
.GetCommandInt2();
3005 m_numCols
-= numCols
;
3006 if ( m_useNativeHeader
)
3007 GetGridColHeader()->SetColumnCount(m_numCols
);
3009 if ( !m_colAt
.IsEmpty() )
3011 int colID
= GetColAt( pos
);
3013 m_colAt
.RemoveAt( pos
, numCols
);
3015 //Shift the column IDs
3017 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
3019 if ( m_colAt
[colPos
] > colID
)
3020 m_colAt
[colPos
] -= numCols
;
3024 if ( !m_colWidths
.IsEmpty() )
3026 m_colWidths
.RemoveAt( pos
, numCols
);
3027 m_colRights
.RemoveAt( pos
, numCols
);
3031 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
3033 i
= GetColAt( colPos
);
3035 w
+= m_colWidths
[i
];
3042 m_currentCellCoords
= wxGridNoCellCoords
;
3046 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3047 m_currentCellCoords
.Set( 0, 0 );
3051 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3052 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3055 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3057 // ifdef'd out following patch from Paul Gammans
3059 // No need to touch row attributes, unless we
3060 // removed _all_ columns, in this case, we remove
3061 // all row attributes.
3062 // I hate to do this here, but the
3063 // needed data is not available inside UpdateAttrCols.
3064 if ( !GetNumberCols() )
3065 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
3069 if ( !GetBatchCount() )
3072 m_colWindow
->Refresh();
3079 InvalidateBestSize();
3081 if (result
&& !GetBatchCount() )
3082 m_gridWin
->Refresh();
3087 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
3089 wxRegionIterator
iter( reg
);
3092 wxArrayInt rowlabels
;
3099 // TODO: remove this when we can...
3100 // There is a bug in wxMotif that gives garbage update
3101 // rectangles if you jump-scroll a long way by clicking the
3102 // scrollbar with middle button. This is a work-around
3104 #if defined(__WXMOTIF__)
3106 m_gridWin
->GetClientSize( &cw
, &ch
);
3107 if ( r
.GetTop() > ch
)
3109 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3112 // logical bounds of update region
3115 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3116 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3118 // find the row labels within these bounds
3121 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
3123 if ( GetRowBottom(row
) < top
)
3126 if ( GetRowTop(row
) > bottom
)
3129 rowlabels
.Add( row
);
3138 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
3140 wxRegionIterator
iter( reg
);
3143 wxArrayInt colLabels
;
3150 // TODO: remove this when we can...
3151 // There is a bug in wxMotif that gives garbage update
3152 // rectangles if you jump-scroll a long way by clicking the
3153 // scrollbar with middle button. This is a work-around
3155 #if defined(__WXMOTIF__)
3157 m_gridWin
->GetClientSize( &cw
, &ch
);
3158 if ( r
.GetLeft() > cw
)
3160 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3163 // logical bounds of update region
3166 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3167 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3169 // find the cells within these bounds
3173 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
3175 col
= GetColAt( colPos
);
3177 if ( GetColRight(col
) < left
)
3180 if ( GetColLeft(col
) > right
)
3183 colLabels
.Add( col
);
3192 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
3196 wxGridCellCoordsArray cellsExposed
;
3198 int left
, top
, right
, bottom
;
3199 for ( wxRegionIterator
iter(reg
); iter
; ++iter
)
3203 // Skip 0-height cells, they're invisible anyhow, don't waste time
3204 // getting their rectangles and so on.
3205 if ( !r
.GetHeight() )
3208 // TODO: remove this when we can...
3209 // There is a bug in wxMotif that gives garbage update
3210 // rectangles if you jump-scroll a long way by clicking the
3211 // scrollbar with middle button. This is a work-around
3213 #if defined(__WXMOTIF__)
3215 m_gridWin
->GetClientSize( &cw
, &ch
);
3216 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3217 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3218 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3219 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3222 // logical bounds of update region
3224 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3225 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3227 // find the cells within these bounds
3229 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
3231 if ( GetRowBottom(row
) <= top
)
3234 if ( GetRowTop(row
) > bottom
)
3237 // add all dirty cells in this row: notice that the columns which
3238 // are dirty don't depend on the row so we compute them only once
3239 // for the first dirty row and then reuse for all the next ones
3242 // do determine the dirty columns
3243 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
3244 cols
.push_back(GetColAt(pos
));
3246 // if there are no dirty columns at all, nothing to do
3251 const size_t count
= cols
.size();
3252 for ( size_t n
= 0; n
< count
; n
++ )
3253 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
3257 return cellsExposed
;
3261 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3264 wxPoint
pos( event
.GetPosition() );
3265 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3267 if ( event
.Dragging() )
3271 m_isDragging
= true;
3272 m_rowLabelWin
->CaptureMouse();
3275 if ( event
.LeftIsDown() )
3277 switch ( m_cursorMode
)
3279 case WXGRID_CURSOR_RESIZE_ROW
:
3281 int cw
, ch
, left
, dummy
;
3282 m_gridWin
->GetClientSize( &cw
, &ch
);
3283 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3285 wxClientDC
dc( m_gridWin
);
3288 GetRowTop(m_dragRowOrCol
) +
3289 GetRowMinimalHeight(m_dragRowOrCol
) );
3290 dc
.SetLogicalFunction(wxINVERT
);
3291 if ( m_dragLastPos
>= 0 )
3293 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3295 dc
.DrawLine( left
, y
, left
+cw
, y
);
3300 case WXGRID_CURSOR_SELECT_ROW
:
3302 if ( (row
= YToRow( y
)) >= 0 )
3305 m_selection
->SelectRow(row
, event
);
3310 // default label to suppress warnings about "enumeration value
3311 // 'xxx' not handled in switch
3319 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3324 if (m_rowLabelWin
->HasCapture())
3325 m_rowLabelWin
->ReleaseMouse();
3326 m_isDragging
= false;
3329 // ------------ Entering or leaving the window
3331 if ( event
.Entering() || event
.Leaving() )
3333 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3336 // ------------ Left button pressed
3338 else if ( event
.LeftDown() )
3340 row
= YToEdgeOfRow(y
);
3341 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3343 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3345 else // not a request to start resizing
3349 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3351 if ( !event
.ShiftDown() && !event
.CmdDown() )
3355 if ( event
.ShiftDown() )
3357 m_selection
->SelectBlock
3359 m_currentCellCoords
.GetRow(), 0,
3360 row
, GetNumberCols() - 1,
3366 m_selection
->SelectRow(row
, event
);
3370 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3375 // ------------ Left double click
3377 else if (event
.LeftDClick() )
3379 row
= YToEdgeOfRow(y
);
3380 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3382 // adjust row height depending on label text
3384 // TODO: generate RESIZING event, see #10754
3385 AutoSizeRowLabelSize( row
);
3387 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3389 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3392 else // not on row separator or it's not resizable
3396 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3398 // no default action at the moment
3403 // ------------ Left button released
3405 else if ( event
.LeftUp() )
3407 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3408 DoEndDragResizeRow(event
);
3410 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3414 // ------------ Right button down
3416 else if ( event
.RightDown() )
3420 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3422 // no default action at the moment
3426 // ------------ Right double click
3428 else if ( event
.RightDClick() )
3432 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3434 // no default action at the moment
3438 // ------------ No buttons down and mouse moving
3440 else if ( event
.Moving() )
3442 m_dragRowOrCol
= YToEdgeOfRow( y
);
3443 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3445 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3447 if ( CanDragRowSize(m_dragRowOrCol
) )
3448 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3451 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3453 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3458 void wxGrid::UpdateColumnSortingIndicator(int col
)
3460 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3462 if ( m_useNativeHeader
)
3463 GetGridColHeader()->UpdateColumn(col
);
3464 else if ( m_nativeColumnLabels
)
3465 m_colWindow
->Refresh();
3466 //else: sorting indicator display not yet implemented in grid version
3469 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3471 if ( col
== m_sortCol
)
3473 // we are already using this column for sorting (or not sorting at all)
3474 // but we might still change the sorting order, check for it
3475 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3477 m_sortIsAscending
= ascending
;
3479 UpdateColumnSortingIndicator(m_sortCol
);
3482 else // we're changing the column used for sorting
3484 const int sortColOld
= m_sortCol
;
3486 // change it before updating the column as we want GetSortingColumn()
3487 // to return the correct new value
3490 if ( sortColOld
!= wxNOT_FOUND
)
3491 UpdateColumnSortingIndicator(sortColOld
);
3493 if ( m_sortCol
!= wxNOT_FOUND
)
3495 m_sortIsAscending
= ascending
;
3496 UpdateColumnSortingIndicator(m_sortCol
);
3501 void wxGrid::DoColHeaderClick(int col
)
3503 // we consider that the grid was resorted if this event is processed and
3505 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3507 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3512 void wxGrid::DoStartResizeCol(int col
)
3514 m_dragRowOrCol
= col
;
3516 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3519 void wxGrid::DoUpdateResizeCol(int x
)
3521 int cw
, ch
, dummy
, top
;
3522 m_gridWin
->GetClientSize( &cw
, &ch
);
3523 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3525 wxClientDC
dc( m_gridWin
);
3528 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3529 dc
.SetLogicalFunction(wxINVERT
);
3530 if ( m_dragLastPos
>= 0 )
3532 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3534 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3538 void wxGrid::DoUpdateResizeColWidth(int w
)
3540 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3543 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3546 CalcUnscrolledPosition( event
.GetPosition().x
, 0, &x
, NULL
);
3548 int col
= XToCol(x
);
3549 if ( event
.Dragging() )
3553 m_isDragging
= true;
3554 GetColLabelWindow()->CaptureMouse();
3556 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3557 DoStartMoveCol(col
);
3560 if ( event
.LeftIsDown() )
3562 switch ( m_cursorMode
)
3564 case WXGRID_CURSOR_RESIZE_COL
:
3565 DoUpdateResizeCol(x
);
3568 case WXGRID_CURSOR_SELECT_COL
:
3573 m_selection
->SelectCol(col
, event
);
3578 case WXGRID_CURSOR_MOVE_COL
:
3580 int posNew
= XToPos(x
);
3581 int colNew
= GetColAt(posNew
);
3583 // determine the position of the drop marker
3585 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3586 markerX
= GetColRight(colNew
);
3588 markerX
= GetColLeft(colNew
);
3590 if ( markerX
!= m_dragLastPos
)
3592 wxClientDC
dc( GetColLabelWindow() );
3596 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3600 //Clean up the last indicator
3601 if ( m_dragLastPos
>= 0 )
3603 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3605 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3606 dc
.SetPen(wxNullPen
);
3608 if ( XToCol( m_dragLastPos
) != -1 )
3609 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3612 const wxColour
*color
;
3613 //Moving to the same place? Don't draw a marker
3614 if ( colNew
== m_dragRowOrCol
)
3615 color
= wxLIGHT_GREY
;
3620 wxPen
pen( *color
, 2 );
3623 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3625 dc
.SetPen(wxNullPen
);
3627 m_dragLastPos
= markerX
- 1;
3632 // default label to suppress warnings about "enumeration value
3633 // 'xxx' not handled in switch
3641 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3646 if (GetColLabelWindow()->HasCapture())
3647 GetColLabelWindow()->ReleaseMouse();
3648 m_isDragging
= false;
3651 // ------------ Entering or leaving the window
3653 if ( event
.Entering() || event
.Leaving() )
3655 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3658 // ------------ Left button pressed
3660 else if ( event
.LeftDown() )
3662 int colEdge
= XToEdgeOfCol(x
);
3663 if ( colEdge
!= wxNOT_FOUND
&& CanDragColSize(colEdge
) )
3665 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3667 else // not a request to start resizing
3670 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3672 if ( m_canDragColMove
)
3674 //Show button as pressed
3675 wxClientDC
dc( GetColLabelWindow() );
3676 int colLeft
= GetColLeft( col
);
3677 int colRight
= GetColRight( col
) - 1;
3678 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3679 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3680 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3682 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3686 if ( !event
.ShiftDown() && !event
.CmdDown() )
3690 if ( event
.ShiftDown() )
3692 m_selection
->SelectBlock
3694 0, m_currentCellCoords
.GetCol(),
3695 GetNumberRows() - 1, col
,
3701 m_selection
->SelectCol(col
, event
);
3705 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3711 // ------------ Left double click
3713 if ( event
.LeftDClick() )
3715 const int colEdge
= XToEdgeOfCol(x
);
3716 if ( colEdge
== -1 )
3719 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3721 // no default action at the moment
3726 // adjust column width depending on label text
3728 // TODO: generate RESIZING event, see #10754
3729 if ( !SendGridSizeEvent(wxEVT_GRID_COL_AUTO_SIZE
, -1, colEdge
, event
) )
3730 AutoSizeColLabelSize( colEdge
);
3732 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3734 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3739 // ------------ Left button released
3741 else if ( event
.LeftUp() )
3743 switch ( m_cursorMode
)
3745 case WXGRID_CURSOR_RESIZE_COL
:
3746 DoEndDragResizeCol(event
);
3749 case WXGRID_CURSOR_MOVE_COL
:
3750 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3752 // the column didn't actually move anywhere
3754 DoColHeaderClick(col
);
3755 m_colWindow
->Refresh(); // "unpress" the column
3759 // get the position of the column we're over
3760 int pos
= XToPos(x
);
3762 // we may need to adjust the drop position but don't bother
3763 // checking for it if we can't anyhow
3766 // also find the index of the column we're over: notice
3767 // that the existing "col" variable may be invalid but
3768 // we need a valid one here
3769 const int colValid
= GetColAt(pos
);
3771 // if we're on the "near" (usually left but right in
3772 // RTL case) part of the column, the actual position we
3773 // should be placed in is actually the one before it
3775 const int middle
= GetColLeft(colValid
) +
3776 GetColWidth(colValid
)/2;
3777 if ( GetLayoutDirection() == wxLayout_LeftToRight
)
3778 onNearPart
= (x
<= middle
);
3779 else // wxLayout_RightToLeft
3780 onNearPart
= (x
> middle
);
3790 case WXGRID_CURSOR_SELECT_COL
:
3791 case WXGRID_CURSOR_SELECT_CELL
:
3792 case WXGRID_CURSOR_RESIZE_ROW
:
3793 case WXGRID_CURSOR_SELECT_ROW
:
3795 DoColHeaderClick(col
);
3799 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3803 // ------------ Right button down
3805 else if ( event
.RightDown() )
3808 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3810 // no default action at the moment
3814 // ------------ Right double click
3816 else if ( event
.RightDClick() )
3819 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3821 // no default action at the moment
3825 // ------------ No buttons down and mouse moving
3827 else if ( event
.Moving() )
3829 m_dragRowOrCol
= XToEdgeOfCol( x
);
3830 if ( m_dragRowOrCol
>= 0 )
3832 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3834 if ( CanDragColSize(m_dragRowOrCol
) )
3835 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3838 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3840 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3845 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3847 if ( event
.LeftDown() )
3849 // indicate corner label by having both row and
3852 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3857 else if ( event
.LeftDClick() )
3859 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3861 else if ( event
.RightDown() )
3863 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3865 // no default action at the moment
3868 else if ( event
.RightDClick() )
3870 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3872 // no default action at the moment
3877 void wxGrid::CancelMouseCapture()
3879 // cancel operation currently in progress, whatever it is
3882 m_isDragging
= false;
3883 m_startDragPos
= wxDefaultPosition
;
3885 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3886 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3887 m_winCapture
= NULL
;
3889 // remove traces of whatever we drew on screen
3894 void wxGrid::ChangeCursorMode(CursorMode mode
,
3899 static const wxChar
*const cursorModes
[] =
3909 wxLogTrace(wxT("grid"),
3910 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3911 win
== m_colWindow
? wxT("colLabelWin")
3912 : win
? wxT("rowLabelWin")
3914 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3915 #endif // wxUSE_LOG_TRACE
3917 if ( mode
== m_cursorMode
&&
3918 win
== m_winCapture
&&
3919 captureMouse
== (m_winCapture
!= NULL
))
3924 // by default use the grid itself
3930 m_winCapture
->ReleaseMouse();
3931 m_winCapture
= NULL
;
3934 m_cursorMode
= mode
;
3936 switch ( m_cursorMode
)
3938 case WXGRID_CURSOR_RESIZE_ROW
:
3939 win
->SetCursor( m_rowResizeCursor
);
3942 case WXGRID_CURSOR_RESIZE_COL
:
3943 win
->SetCursor( m_colResizeCursor
);
3946 case WXGRID_CURSOR_MOVE_COL
:
3947 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3951 win
->SetCursor( *wxSTANDARD_CURSOR
);
3955 // we need to capture mouse when resizing
3956 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3957 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3959 if ( captureMouse
&& resize
)
3961 win
->CaptureMouse();
3966 // ----------------------------------------------------------------------------
3967 // grid mouse event processing
3968 // ----------------------------------------------------------------------------
3971 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3972 const wxGridCellCoords
& coords
,
3975 bool performDefault
= true ;
3977 if ( coords
== wxGridNoCellCoords
)
3978 return performDefault
; // we're outside any valid cell
3980 // Hide the edit control, so it won't interfere with drag-shrinking.
3981 if ( IsCellEditControlShown() )
3983 HideCellEditControl();
3984 SaveEditControlValue();
3987 switch ( event
.GetModifiers() )
3990 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3991 m_selectedBlockCorner
= coords
;
3992 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3996 if ( CanDragCell() )
4000 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
4001 m_selectedBlockCorner
= coords
;
4003 // if event is handled by user code, no further processing
4004 if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
) != 0 )
4005 performDefault
= false;
4007 return performDefault
;
4011 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
4015 // we don't handle the other key modifiers
4019 return performDefault
;
4022 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
4024 wxClientDC
dc(m_gridWin
);
4026 dc
.SetLogicalFunction(wxINVERT
);
4028 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
4029 m_gridWin
->GetClientSize());
4031 // erase the previously drawn line, if any
4032 if ( m_dragLastPos
>= 0 )
4033 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
4035 // we need the vertical position for rows and horizontal for columns here
4036 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
4038 // don't allow resizing beneath the minimal size
4039 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
4040 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
4041 if ( m_dragLastPos
< posMin
)
4042 m_dragLastPos
= posMin
;
4044 // and draw it at the new position
4045 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
4048 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
4050 if ( !m_isDragging
)
4052 // Don't start doing anything until the mouse has been dragged far
4054 const wxPoint
& pt
= event
.GetPosition();
4055 if ( m_startDragPos
== wxDefaultPosition
)
4057 m_startDragPos
= pt
;
4061 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
4062 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
4066 const bool isFirstDrag
= !m_isDragging
;
4067 m_isDragging
= true;
4069 switch ( m_cursorMode
)
4071 case WXGRID_CURSOR_SELECT_CELL
:
4072 // no further handling if handled by user
4073 if ( DoGridCellDrag(event
, coords
, isFirstDrag
) == false )
4077 case WXGRID_CURSOR_RESIZE_ROW
:
4078 DoGridLineDrag(event
, wxGridRowOperations());
4081 case WXGRID_CURSOR_RESIZE_COL
:
4082 DoGridLineDrag(event
, wxGridColumnOperations());
4091 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
4093 m_winCapture
= m_gridWin
;
4094 m_winCapture
->CaptureMouse();
4099 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
4100 const wxGridCellCoords
& coords
,
4103 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
4105 // event handled by user code, no need to do anything here
4109 if ( !event
.CmdDown() )
4112 if ( event
.ShiftDown() )
4116 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
4117 m_selectedBlockCorner
= coords
;
4120 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
4122 DisableCellEditControl();
4123 MakeCellVisible( coords
);
4125 if ( event
.CmdDown() )
4129 m_selection
->ToggleCellSelection(coords
, event
);
4132 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4133 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4134 m_selectedBlockCorner
= coords
;
4140 // In row or column selection mode just clicking on the cell
4141 // should select the row or column containing it: this is more
4142 // convenient for the kinds of controls that use such selection
4143 // mode and is compatible with 2.8 behaviour (see #12062).
4144 switch ( m_selection
->GetSelectionMode() )
4146 case wxGridSelectCells
:
4147 case wxGridSelectRowsOrColumns
:
4148 // nothing to do in these cases
4151 case wxGridSelectRows
:
4152 m_selection
->SelectRow(coords
.GetRow());
4155 case wxGridSelectColumns
:
4156 m_selection
->SelectCol(coords
.GetCol());
4161 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
4162 coords
!= wxGridNoCellCoords
;
4163 SetCurrentCell( coords
);
4169 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
4170 const wxGridCellCoords
& coords
,
4173 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
4175 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
4177 // we want double click to select a cell and start editing
4178 // (i.e. to behave in same way as sequence of two slow clicks):
4179 m_waitForSlowClick
= true;
4185 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
4187 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4191 m_winCapture
->ReleaseMouse();
4192 m_winCapture
= NULL
;
4195 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
4198 EnableCellEditControl();
4200 wxGridCellAttr
*attr
= GetCellAttr(coords
);
4201 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
4202 editor
->StartingClick();
4206 m_waitForSlowClick
= false;
4208 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4209 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4213 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
4214 m_selectedBlockBottomRight
,
4218 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4219 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4221 // Show the edit control, if it has been hidden for
4223 ShowCellEditControl();
4226 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4228 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4229 DoEndDragResizeRow(event
);
4231 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4233 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4234 DoEndDragResizeCol(event
);
4241 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
4242 const wxGridCellCoords
& coords
,
4245 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
4247 // out of grid cell area
4248 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4252 int dragRow
= YToEdgeOfRow( pos
.y
);
4253 int dragCol
= XToEdgeOfCol( pos
.x
);
4255 // Dragging on the corner of a cell to resize in both
4256 // directions is not implemented yet...
4258 if ( dragRow
>= 0 && dragCol
>= 0 )
4260 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4264 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
4266 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4268 m_dragRowOrCol
= dragRow
;
4269 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
4272 // When using the native header window we can only resize the columns by
4273 // dragging the dividers in it because we can't make it enter into the
4274 // column resizing mode programmatically
4275 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
4276 CanDragGridSize() && CanDragColSize(dragCol
) )
4278 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4280 m_dragRowOrCol
= dragCol
;
4281 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
4284 else // Neither on a row or col edge
4286 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4288 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4293 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
4295 if ( event
.Entering() || event
.Leaving() )
4297 // we don't care about these events but we must not reset m_isDragging
4298 // if they happen so return before anything else is done
4303 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
4305 // coordinates of the cell under mouse
4306 wxGridCellCoords coords
= XYToCell(pos
);
4308 int cell_rows
, cell_cols
;
4309 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4310 if ( (cell_rows
< 0) || (cell_cols
< 0) )
4312 coords
.SetRow(coords
.GetRow() + cell_rows
);
4313 coords
.SetCol(coords
.GetCol() + cell_cols
);
4316 if ( event
.Dragging() )
4318 if ( event
.LeftIsDown() )
4319 DoGridDragEvent(event
, coords
);
4325 m_isDragging
= false;
4326 m_startDragPos
= wxDefaultPosition
;
4328 // deal with various button presses
4329 if ( event
.IsButton() )
4331 if ( coords
!= wxGridNoCellCoords
)
4333 DisableCellEditControl();
4335 if ( event
.LeftDown() )
4336 DoGridCellLeftDown(event
, coords
, pos
);
4337 else if ( event
.LeftDClick() )
4338 DoGridCellLeftDClick(event
, coords
, pos
);
4339 else if ( event
.RightDown() )
4340 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
4341 else if ( event
.RightDClick() )
4342 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
4345 // this one should be called even if we're not over any cell
4346 if ( event
.LeftUp() )
4348 DoGridCellLeftUp(event
, coords
);
4351 else if ( event
.Moving() )
4353 DoGridMouseMoveEvent(event
, coords
, pos
);
4355 else // unknown mouse event?
4361 // this function returns true only if the size really changed
4362 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4364 if ( m_dragLastPos
== -1 )
4367 const wxGridOperations
& doper
= oper
.Dual();
4369 const wxSize size
= m_gridWin
->GetClientSize();
4371 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4373 // erase the last line we drew
4374 wxClientDC
dc(m_gridWin
);
4376 dc
.SetLogicalFunction(wxINVERT
);
4378 const int posLineStart
= oper
.Select(ptOrigin
);
4379 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4381 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4383 // temporarily hide the edit control before resizing
4384 HideCellEditControl();
4385 SaveEditControlValue();
4387 // do resize the line
4388 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4389 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4390 oper
.SetLineSize(this, m_dragRowOrCol
,
4391 wxMax(m_dragLastPos
- lineStart
,
4392 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4394 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4398 // refresh now if we're not frozen
4399 if ( !GetBatchCount() )
4401 // we need to refresh everything beyond the resized line in the header
4404 // get the position from which to refresh in the other direction
4405 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4406 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4408 // we only need the ordinate (for rows) or abscissa (for columns) here,
4409 // and need to cover the entire window in the other direction
4410 oper
.Select(rect
) = 0;
4412 wxRect
rectHeader(rect
.GetPosition(),
4415 oper
.GetHeaderWindowSize(this),
4416 doper
.Select(size
) - doper
.Select(rect
)
4419 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4422 // also refresh the grid window: extend the rectangle
4425 oper
.SelectSize(rect
) = oper
.Select(size
);
4427 int subtractLines
= 0;
4428 int line
= doper
.PosToLine(this, posLineStart
);
4431 // ensure that if we have a multi-cell block we redraw all of
4432 // it by increasing the refresh area to cover it entirely if a
4433 // part of it is affected
4434 const int lineEnd
= doper
.PosToLine(this, posLineEnd
, true);
4435 for ( ; line
< lineEnd
; line
++ )
4437 int cellLines
= oper
.Select(
4438 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4439 if ( cellLines
< subtractLines
)
4440 subtractLines
= cellLines
;
4445 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4446 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4448 doper
.Select(rect
) = startPos
;
4449 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4451 m_gridWin
->Refresh(false, &rect
);
4455 // show the edit control back again
4456 ShowCellEditControl();
4461 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4463 // TODO: generate RESIZING event, see #10754
4465 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4466 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4469 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4471 // TODO: generate RESIZING event, see #10754
4473 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4474 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4477 void wxGrid::DoStartMoveCol(int col
)
4479 m_dragRowOrCol
= col
;
4482 void wxGrid::DoEndMoveCol(int pos
)
4484 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4486 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4487 SetColPos(m_dragRowOrCol
, pos
);
4488 //else: vetoed by user
4490 m_dragRowOrCol
= -1;
4493 void wxGrid::RefreshAfterColPosChange()
4495 // recalculate the column rights as the column positions have changed,
4496 // unless we calculate them dynamically because all columns widths are the
4497 // same and it's easy to do
4498 if ( !m_colWidths
.empty() )
4501 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4503 int colID
= GetColAt( colPos
);
4505 colRight
+= m_colWidths
[colID
];
4506 m_colRights
[colID
] = colRight
;
4510 // and make the changes visible
4511 if ( m_useNativeHeader
)
4513 if ( m_colAt
.empty() )
4514 GetGridColHeader()->ResetColumnsOrder();
4516 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4520 m_colWindow
->Refresh();
4522 m_gridWin
->Refresh();
4525 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4529 RefreshAfterColPosChange();
4532 void wxGrid::SetColPos(int idx
, int pos
)
4534 // we're going to need m_colAt now, initialize it if needed
4535 if ( m_colAt
.empty() )
4537 m_colAt
.reserve(m_numCols
);
4538 for ( int i
= 0; i
< m_numCols
; i
++ )
4539 m_colAt
.push_back(i
);
4542 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4544 RefreshAfterColPosChange();
4547 void wxGrid::ResetColPos()
4551 RefreshAfterColPosChange();
4554 void wxGrid::EnableDragColMove( bool enable
)
4556 if ( m_canDragColMove
== enable
)
4559 if ( m_useNativeHeader
)
4561 // update all columns to make them [not] reorderable
4562 GetGridColHeader()->SetColumnCount(m_numCols
);
4565 m_canDragColMove
= enable
;
4567 // we use to call ResetColPos() from here if !enable but this doesn't seem
4568 // right as it would mean there would be no way to "freeze" the current
4569 // columns order by disabling moving them after putting them in the desired
4570 // order, whereas now you can always call ResetColPos() manually if needed
4575 // ------ interaction with data model
4577 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4579 switch ( msg
.GetId() )
4581 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4582 return GetModelValues();
4584 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4585 return SetModelValues();
4587 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4588 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4589 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4590 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4591 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4592 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4593 return Redimension( msg
);
4600 // The behaviour of this function depends on the grid table class
4601 // Clear() function. For the default wxGridStringTable class the
4602 // behaviour is to replace all cell contents with wxEmptyString but
4603 // not to change the number of rows or cols.
4605 void wxGrid::ClearGrid()
4609 if (IsCellEditControlEnabled())
4610 DisableCellEditControl();
4613 if (!GetBatchCount())
4614 m_gridWin
->Refresh();
4619 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4620 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4622 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4627 if ( IsCellEditControlEnabled() )
4628 DisableCellEditControl();
4630 return (m_table
->*funcModify
)(pos
, num
);
4632 // the table will have sent the results of the insert row
4633 // operation to this view object as a grid table message
4637 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4638 int num
, bool WXUNUSED(updateLabels
))
4640 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4645 return (m_table
->*funcAppend
)(num
);
4648 // ----------------------------------------------------------------------------
4649 // event generation helpers
4650 // ----------------------------------------------------------------------------
4653 wxGrid::SendGridSizeEvent(wxEventType type
,
4655 const wxMouseEvent
& mouseEv
)
4657 int rowOrCol
= row
== -1 ? col
: row
;
4659 wxGridSizeEvent
gridEvt( GetId(),
4663 mouseEv
.GetX() + GetRowLabelSize(),
4664 mouseEv
.GetY() + GetColLabelSize(),
4667 return GetEventHandler()->ProcessEvent(gridEvt
);
4670 // Generate a grid event based on a mouse event and return:
4671 // -1 if the event was vetoed
4672 // +1 if the event was processed (but not vetoed)
4673 // 0 if the event wasn't handled
4675 wxGrid::SendEvent(const wxEventType type
,
4677 const wxMouseEvent
& mouseEv
)
4679 bool claimed
, vetoed
;
4681 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4683 // Right now, it should _never_ end up here!
4684 wxGridRangeSelectEvent
gridEvt( GetId(),
4687 m_selectedBlockTopLeft
,
4688 m_selectedBlockBottomRight
,
4692 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4693 vetoed
= !gridEvt
.IsAllowed();
4695 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4696 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4697 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4698 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4700 wxPoint pos
= mouseEv
.GetPosition();
4702 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4703 pos
.y
+= GetColLabelSize();
4704 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4705 pos
.x
+= GetRowLabelSize();
4707 wxGridEvent
gridEvt( GetId(),
4715 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4716 vetoed
= !gridEvt
.IsAllowed();
4720 wxGridEvent
gridEvt( GetId(),
4724 mouseEv
.GetX() + GetRowLabelSize(),
4725 mouseEv
.GetY() + GetColLabelSize(),
4729 if ( type
== wxEVT_GRID_CELL_BEGIN_DRAG
)
4731 // by default the dragging is not supported, the user code must
4732 // explicitly allow the event for it to take place
4736 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4737 vetoed
= !gridEvt
.IsAllowed();
4740 // A Veto'd event may not be `claimed' so test this first
4744 return claimed
? 1 : 0;
4747 // Generate a grid event of specified type, return value same as above
4750 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4752 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4753 gridEvt
.SetString(s
);
4755 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4757 // A Veto'd event may not be `claimed' so test this first
4758 if ( !gridEvt
.IsAllowed() )
4761 return claimed
? 1 : 0;
4764 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4766 // needed to prevent zillions of paint events on MSW
4770 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4772 // Don't do anything if between Begin/EndBatch...
4773 // EndBatch() will do all this on the last nested one anyway.
4774 if ( m_created
&& !GetBatchCount() )
4776 // Refresh to get correct scrolled position:
4777 wxScrolledWindow::Refresh(eraseb
, rect
);
4781 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4782 int width_label
, width_cell
, height_label
, height_cell
;
4785 // Copy rectangle can get scroll offsets..
4786 rect_x
= rect
->GetX();
4787 rect_y
= rect
->GetY();
4788 rectWidth
= rect
->GetWidth();
4789 rectHeight
= rect
->GetHeight();
4791 width_label
= m_rowLabelWidth
- rect_x
;
4792 if (width_label
> rectWidth
)
4793 width_label
= rectWidth
;
4795 height_label
= m_colLabelHeight
- rect_y
;
4796 if (height_label
> rectHeight
)
4797 height_label
= rectHeight
;
4799 if (rect_x
> m_rowLabelWidth
)
4801 x
= rect_x
- m_rowLabelWidth
;
4802 width_cell
= rectWidth
;
4807 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4810 if (rect_y
> m_colLabelHeight
)
4812 y
= rect_y
- m_colLabelHeight
;
4813 height_cell
= rectHeight
;
4818 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4821 // Paint corner label part intersecting rect.
4822 if ( width_label
> 0 && height_label
> 0 )
4824 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4825 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4828 // Paint col labels part intersecting rect.
4829 if ( width_cell
> 0 && height_label
> 0 )
4831 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4832 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4835 // Paint row labels part intersecting rect.
4836 if ( width_label
> 0 && height_cell
> 0 )
4838 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4839 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4842 // Paint cell area part intersecting rect.
4843 if ( width_cell
> 0 && height_cell
> 0 )
4845 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4846 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4851 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4852 m_colWindow
->Refresh(eraseb
, NULL
);
4853 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4854 m_gridWin
->Refresh(eraseb
, NULL
);
4859 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4861 if (m_targetWindow
!= this) // check whether initialisation has been done
4863 // reposition our children windows
4868 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4870 if ( m_inOnKeyDown
)
4872 // shouldn't be here - we are going round in circles...
4874 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4877 m_inOnKeyDown
= true;
4879 // propagate the event up and see if it gets processed
4880 wxWindow
*parent
= GetParent();
4881 wxKeyEvent
keyEvt( event
);
4882 keyEvt
.SetEventObject( parent
);
4884 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4886 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4888 if (event
.GetKeyCode() == WXK_RIGHT
)
4889 event
.m_keyCode
= WXK_LEFT
;
4890 else if (event
.GetKeyCode() == WXK_LEFT
)
4891 event
.m_keyCode
= WXK_RIGHT
;
4894 // try local handlers
4895 switch ( event
.GetKeyCode() )
4898 if ( event
.ControlDown() )
4899 MoveCursorUpBlock( event
.ShiftDown() );
4901 MoveCursorUp( event
.ShiftDown() );
4905 if ( event
.ControlDown() )
4906 MoveCursorDownBlock( event
.ShiftDown() );
4908 MoveCursorDown( event
.ShiftDown() );
4912 if ( event
.ControlDown() )
4913 MoveCursorLeftBlock( event
.ShiftDown() );
4915 MoveCursorLeft( event
.ShiftDown() );
4919 if ( event
.ControlDown() )
4920 MoveCursorRightBlock( event
.ShiftDown() );
4922 MoveCursorRight( event
.ShiftDown() );
4926 case WXK_NUMPAD_ENTER
:
4927 if ( event
.ControlDown() )
4929 event
.Skip(); // to let the edit control have the return
4933 if ( GetGridCursorRow() < GetNumberRows()-1 )
4935 MoveCursorDown( event
.ShiftDown() );
4939 // at the bottom of a column
4940 DisableCellEditControl();
4951 // send an event to the grid's parents for custom handling
4952 wxGridEvent
gridEvt(GetId(), wxEVT_GRID_TABBING
, this,
4953 GetGridCursorRow(), GetGridCursorCol(),
4954 -1, -1, false, event
);
4955 if ( ProcessWindowEvent(gridEvt
) )
4957 // the event has been handled so no need for more processing
4961 DoGridProcessTab( event
);
4965 GoToCell(event
.ControlDown() ? 0
4966 : m_currentCellCoords
.GetRow(),
4971 GoToCell(event
.ControlDown() ? m_numRows
- 1
4972 : m_currentCellCoords
.GetRow(),
4985 // Ctrl-Space selects the current column, Shift-Space -- the
4986 // current row and Ctrl-Shift-Space -- everything
4987 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4990 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4994 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4997 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4998 m_selection
->SelectBlock(0, 0,
4999 m_numRows
- 1, m_numCols
- 1);
5003 if ( !IsEditable() )
5005 MoveCursorRight(false);
5008 //else: fall through
5021 m_inOnKeyDown
= false;
5024 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5026 // try local handlers
5028 if ( event
.GetKeyCode() == WXK_SHIFT
)
5030 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
5031 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
5035 m_selection
->SelectBlock(
5036 m_selectedBlockTopLeft
,
5037 m_selectedBlockBottomRight
,
5042 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
5043 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
5044 m_selectedBlockCorner
= wxGridNoCellCoords
;
5048 void wxGrid::OnChar( wxKeyEvent
& event
)
5050 // is it possible to edit the current cell at all?
5051 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5053 // yes, now check whether the cells editor accepts the key
5054 int row
= m_currentCellCoords
.GetRow();
5055 int col
= m_currentCellCoords
.GetCol();
5056 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5057 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5059 // <F2> is special and will always start editing, for
5060 // other keys - ask the editor itself
5061 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
5062 || editor
->IsAcceptedKey(event
) )
5064 // ensure cell is visble
5065 MakeCellVisible(row
, col
);
5066 EnableCellEditControl();
5068 // a problem can arise if the cell is not completely
5069 // visible (even after calling MakeCellVisible the
5070 // control is not created and calling StartingKey will
5072 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
5073 editor
->StartingKey(event
);
5089 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5093 void wxGrid::DoGridProcessTab(wxKeyboardState
& kbdState
)
5095 const bool isForwardTab
= !kbdState
.ShiftDown();
5097 // TAB processing only changes when we are at the borders of the grid, so
5098 // let's first handle the common behaviour when we are not at the border.
5101 if ( GetGridCursorCol() < GetNumberCols() - 1 )
5103 MoveCursorRight( false );
5109 if ( GetGridCursorCol() )
5111 MoveCursorLeft( false );
5117 // We only get here if the cursor is at the border of the grid, apply the
5118 // configured behaviour.
5119 switch ( m_tabBehaviour
)
5122 // Nothing special to do, we remain at the current cell.
5126 // Go to the beginning of the next or the end of the previous row.
5129 if ( GetGridCursorRow() < GetNumberRows() - 1 )
5131 GoToCell( GetGridCursorRow() + 1, 0 );
5137 if ( GetGridCursorRow() > 0 )
5139 GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 );
5146 if ( Navigate( isForwardTab
? wxNavigationKeyEvent::IsForward
5147 : wxNavigationKeyEvent::IsBackward
) )
5152 // If we remain in this cell, stop editing it if we were doing so.
5153 DisableCellEditControl();
5156 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5158 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
5160 // the event has been vetoed - do nothing
5164 #if !defined(__WXMAC__)
5165 wxClientDC
dc( m_gridWin
);
5169 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5171 DisableCellEditControl();
5173 if ( IsVisible( m_currentCellCoords
, false ) )
5176 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
5177 if ( !m_gridLinesEnabled
)
5185 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5187 // Otherwise refresh redraws the highlight!
5188 m_currentCellCoords
= coords
;
5190 #if defined(__WXMAC__)
5191 m_gridWin
->Refresh(true /*, & r */);
5193 DrawGridCellArea( dc
, cells
);
5194 DrawAllGridLines( dc
, r
);
5199 m_currentCellCoords
= coords
;
5201 wxGridCellAttr
*attr
= GetCellAttr( coords
);
5202 #if !defined(__WXMAC__)
5203 DrawCellHighlight( dc
, attr
);
5211 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
5212 int bottomRow
, int rightCol
)
5214 MakeCellVisible(m_selectedBlockCorner
);
5215 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
5219 switch ( m_selection
->GetSelectionMode() )
5222 wxFAIL_MSG( "unknown selection mode" );
5225 case wxGridSelectCells
:
5226 // arbitrary blocks selection allowed so just use the cell
5227 // coordinates as is
5230 case wxGridSelectRows
:
5231 // only full rows selection allowd, ensure that we do select
5234 rightCol
= GetNumberCols() - 1;
5237 case wxGridSelectColumns
:
5238 // same as above but for columns
5240 bottomRow
= GetNumberRows() - 1;
5243 case wxGridSelectRowsOrColumns
:
5244 // in this mode we can select only full rows or full columns so
5245 // it doesn't make sense to select blocks at all (and we can't
5246 // extend the block because there is no preferred direction, we
5247 // could only extend it to cover the entire grid but this is
5253 EnsureFirstLessThanSecond(topRow
, bottomRow
);
5254 EnsureFirstLessThanSecond(leftCol
, rightCol
);
5256 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
5257 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
5259 // First the case that we selected a completely new area
5260 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
5261 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
5264 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
5265 wxGridCellCoords ( bottomRow
, rightCol
) );
5266 m_gridWin
->Refresh( false, &rect
);
5269 // Now handle changing an existing selection area.
5270 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
5271 m_selectedBlockBottomRight
!= updateBottomRight
)
5273 // Compute two optimal update rectangles:
5274 // Either one rectangle is a real subset of the
5275 // other, or they are (almost) disjoint!
5277 bool need_refresh
[4];
5281 need_refresh
[3] = false;
5284 // Store intermediate values
5285 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
5286 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
5287 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
5288 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
5290 // Determine the outer/inner coordinates.
5291 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
5292 EnsureFirstLessThanSecond(oldTop
, topRow
);
5293 EnsureFirstLessThanSecond(rightCol
, oldRight
);
5294 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
5296 // Now, either the stuff marked old is the outer
5297 // rectangle or we don't have a situation where one
5298 // is contained in the other.
5300 if ( oldLeft
< leftCol
)
5302 // Refresh the newly selected or deselected
5303 // area to the left of the old or new selection.
5304 need_refresh
[0] = true;
5305 rect
[0] = BlockToDeviceRect(
5306 wxGridCellCoords( oldTop
, oldLeft
),
5307 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
5310 if ( oldTop
< topRow
)
5312 // Refresh the newly selected or deselected
5313 // area above the old or new selection.
5314 need_refresh
[1] = true;
5315 rect
[1] = BlockToDeviceRect(
5316 wxGridCellCoords( oldTop
, leftCol
),
5317 wxGridCellCoords( topRow
- 1, rightCol
) );
5320 if ( oldRight
> rightCol
)
5322 // Refresh the newly selected or deselected
5323 // area to the right of the old or new selection.
5324 need_refresh
[2] = true;
5325 rect
[2] = BlockToDeviceRect(
5326 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
5327 wxGridCellCoords( oldBottom
, oldRight
) );
5330 if ( oldBottom
> bottomRow
)
5332 // Refresh the newly selected or deselected
5333 // area below the old or new selection.
5334 need_refresh
[3] = true;
5335 rect
[3] = BlockToDeviceRect(
5336 wxGridCellCoords( bottomRow
+ 1, leftCol
),
5337 wxGridCellCoords( oldBottom
, rightCol
) );
5340 // various Refresh() calls
5341 for (i
= 0; i
< 4; i
++ )
5342 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5343 m_gridWin
->Refresh( false, &(rect
[i
]) );
5347 m_selectedBlockTopLeft
= updateTopLeft
;
5348 m_selectedBlockBottomRight
= updateBottomRight
;
5352 // ------ functions to get/send data (see also public functions)
5355 bool wxGrid::GetModelValues()
5357 // Hide the editor, so it won't hide a changed value.
5358 HideCellEditControl();
5362 // all we need to do is repaint the grid
5364 m_gridWin
->Refresh();
5371 bool wxGrid::SetModelValues()
5375 // Disable the editor, so it won't hide a changed value.
5376 // Do we also want to save the current value of the editor first?
5378 DisableCellEditControl();
5382 for ( row
= 0; row
< m_numRows
; row
++ )
5384 for ( col
= 0; col
< m_numCols
; col
++ )
5386 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5396 // Note - this function only draws cells that are in the list of
5397 // exposed cells (usually set from the update region by
5398 // CalcExposedCells)
5400 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5402 if ( !m_numRows
|| !m_numCols
)
5405 int i
, numCells
= cells
.GetCount();
5406 int row
, col
, cell_rows
, cell_cols
;
5407 wxGridCellCoordsArray redrawCells
;
5409 for ( i
= numCells
- 1; i
>= 0; i
-- )
5411 row
= cells
[i
].GetRow();
5412 col
= cells
[i
].GetCol();
5413 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5415 // If this cell is part of a multicell block, find owner for repaint
5416 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5418 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5419 bool marked
= false;
5420 for ( int j
= 0; j
< numCells
; j
++ )
5422 if ( cell
== cells
[j
] )
5431 int count
= redrawCells
.GetCount();
5432 for (int j
= 0; j
< count
; j
++)
5434 if ( cell
== redrawCells
[j
] )
5442 redrawCells
.Add( cell
);
5445 // don't bother drawing this cell
5449 // If this cell is empty, find cell to left that might want to overflow
5450 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5452 for ( int l
= 0; l
< cell_rows
; l
++ )
5454 // find a cell in this row to leave already marked for repaint
5456 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5457 if ((redrawCells
[k
].GetCol() < left
) &&
5458 (redrawCells
[k
].GetRow() == row
))
5460 left
= redrawCells
[k
].GetCol();
5464 left
= 0; // oh well
5466 for (int j
= col
- 1; j
>= left
; j
--)
5468 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5470 if (GetCellOverflow(row
+ l
, j
))
5472 wxGridCellCoords
cell(row
+ l
, j
);
5473 bool marked
= false;
5475 for (int k
= 0; k
< numCells
; k
++)
5477 if ( cell
== cells
[k
] )
5486 int count
= redrawCells
.GetCount();
5487 for (int k
= 0; k
< count
; k
++)
5489 if ( cell
== redrawCells
[k
] )
5496 redrawCells
.Add( cell
);
5505 DrawCell( dc
, cells
[i
] );
5508 numCells
= redrawCells
.GetCount();
5510 for ( i
= numCells
- 1; i
>= 0; i
-- )
5512 DrawCell( dc
, redrawCells
[i
] );
5516 void wxGrid::DrawGridSpace( wxDC
& dc
)
5519 m_gridWin
->GetClientSize( &cw
, &ch
);
5522 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5524 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5525 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5527 if ( right
> rightCol
|| bottom
> bottomRow
)
5530 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5532 dc
.SetBrush(GetDefaultCellBackgroundColour());
5533 dc
.SetPen( *wxTRANSPARENT_PEN
);
5535 if ( right
> rightCol
)
5537 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5540 if ( bottom
> bottomRow
)
5542 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5547 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5549 int row
= coords
.GetRow();
5550 int col
= coords
.GetCol();
5552 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5555 // we draw the cell border ourselves
5556 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5558 bool isCurrent
= coords
== m_currentCellCoords
;
5560 wxRect rect
= CellToRect( row
, col
);
5562 // if the editor is shown, we should use it and not the renderer
5563 // Note: However, only if it is really _shown_, i.e. not hidden!
5564 if ( isCurrent
&& IsCellEditControlShown() )
5566 // NB: this "#if..." is temporary and fixes a problem where the
5567 // edit control is erased by this code after being rendered.
5568 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5569 // implicitly, causing this out-of order render.
5570 #if !defined(__WXMAC__)
5571 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5572 editor
->PaintBackground(dc
, rect
, *attr
);
5578 // but all the rest is drawn by the cell renderer and hence may be customized
5579 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5580 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5587 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5589 // don't show highlight when the grid doesn't have focus
5593 int row
= m_currentCellCoords
.GetRow();
5594 int col
= m_currentCellCoords
.GetCol();
5596 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5599 wxRect rect
= CellToRect(row
, col
);
5601 // hmmm... what could we do here to show that the cell is disabled?
5602 // for now, I just draw a thinner border than for the other ones, but
5603 // it doesn't look really good
5605 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5609 // The center of the drawn line is where the position/width/height of
5610 // the rectangle is actually at (on wxMSW at least), so the
5611 // size of the rectangle is reduced to compensate for the thickness of
5612 // the line. If this is too strange on non-wxMSW platforms then
5613 // please #ifdef this appropriately.
5614 rect
.x
+= penWidth
/ 2;
5615 rect
.y
+= penWidth
/ 2;
5616 rect
.width
-= penWidth
- 1;
5617 rect
.height
-= penWidth
- 1;
5619 // Now draw the rectangle
5620 // use the cellHighlightColour if the cell is inside a selection, this
5621 // will ensure the cell is always visible.
5622 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5623 : m_cellHighlightColour
,
5625 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5626 dc
.DrawRectangle(rect
);
5630 wxPen
wxGrid::GetDefaultGridLinePen()
5632 return wxPen(GetGridLineColour());
5635 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5637 return GetDefaultGridLinePen();
5640 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5642 return GetDefaultGridLinePen();
5645 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5647 int row
= coords
.GetRow();
5648 int col
= coords
.GetCol();
5649 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5653 wxRect rect
= CellToRect( row
, col
);
5655 // right hand border
5656 dc
.SetPen( GetColGridLinePen(col
) );
5657 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5658 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5661 dc
.SetPen( GetRowGridLinePen(row
) );
5662 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5663 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5666 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5668 // This if block was previously in wxGrid::OnPaint but that doesn't
5669 // seem to get called under wxGTK - MB
5671 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5672 m_numRows
&& m_numCols
)
5674 m_currentCellCoords
.Set(0, 0);
5677 if ( IsCellEditControlShown() )
5679 // don't show highlight when the edit control is shown
5683 // if the active cell was repainted, repaint its highlight too because it
5684 // might have been damaged by the grid lines
5685 size_t count
= cells
.GetCount();
5686 for ( size_t n
= 0; n
< count
; n
++ )
5688 wxGridCellCoords cell
= cells
[n
];
5690 // If we are using attributes, then we may have just exposed another
5691 // cell in a partially-visible merged cluster of cells. If the "anchor"
5692 // (upper left) cell of this merged cluster is the cell indicated by
5693 // m_currentCellCoords, then we need to refresh the cell highlight even
5694 // though the "anchor" itself is not part of our update segment.
5695 if ( CanHaveAttributes() )
5699 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5702 cell
.SetRow(cell
.GetRow() + rows
);
5705 cell
.SetCol(cell
.GetCol() + cols
);
5708 if ( cell
== m_currentCellCoords
)
5710 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5711 DrawCellHighlight(dc
, attr
);
5719 // Used by wxGrid::Render() to draw the grid lines only for the cells in the
5722 wxGrid::DrawRangeGridLines(wxDC
& dc
,
5723 const wxRegion
& reg
,
5724 const wxGridCellCoords
& topLeft
,
5725 const wxGridCellCoords
& bottomRight
)
5727 if ( !m_gridLinesEnabled
)
5730 int top
, left
, width
, height
;
5731 reg
.GetBox( left
, top
, width
, height
);
5733 // create a clipping region
5734 wxRegion
clippedcells( dc
.LogicalToDeviceX( left
),
5735 dc
.LogicalToDeviceY( top
),
5736 dc
.LogicalToDeviceXRel( width
),
5737 dc
.LogicalToDeviceYRel( height
) );
5739 // subtract multi cell span area from clipping region for lines
5741 for ( int row
= topLeft
.GetRow(); row
<= bottomRight
.GetRow(); row
++ )
5743 for ( int col
= topLeft
.GetCol(); col
<= bottomRight
.GetCol(); col
++ )
5745 int cell_rows
, cell_cols
;
5746 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5747 if ( cell_rows
> 1 || cell_cols
> 1 ) // multi cell
5749 rect
= CellToRect( row
, col
);
5750 // cater for scaling
5751 // device origin already set in ::Render() for x, y
5752 rect
.x
= dc
.LogicalToDeviceX( rect
.x
);
5753 rect
.y
= dc
.LogicalToDeviceY( rect
.y
);
5754 rect
.width
= dc
.LogicalToDeviceXRel( rect
.width
);
5755 rect
.height
= dc
.LogicalToDeviceYRel( rect
.height
) - 1;
5756 clippedcells
.Subtract( rect
);
5758 else if ( cell_rows
< 0 || cell_cols
< 0 ) // part of multicell
5760 rect
= CellToRect( row
+ cell_rows
, col
+ cell_cols
);
5761 rect
.x
= dc
.LogicalToDeviceX( rect
.x
);
5762 rect
.y
= dc
.LogicalToDeviceY( rect
.y
);
5763 rect
.width
= dc
.LogicalToDeviceXRel( rect
.width
);
5764 rect
.height
= dc
.LogicalToDeviceYRel( rect
.height
) - 1;
5765 clippedcells
.Subtract( rect
);
5770 dc
.SetDeviceClippingRegion( clippedcells
);
5773 top
, left
, top
+ height
, left
+ width
,
5774 topLeft
.GetRow(), topLeft
.GetCol(),
5775 bottomRight
.GetRow(), bottomRight
.GetCol());
5777 dc
.DestroyClippingRegion();
5780 // This is used to redraw all grid lines e.g. when the grid line colour
5783 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5785 if ( !m_gridLinesEnabled
)
5788 int top
, bottom
, left
, right
;
5791 m_gridWin
->GetClientSize(&cw
, &ch
);
5792 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5793 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5795 // avoid drawing grid lines past the last row and col
5796 if ( m_gridLinesClipHorz
)
5801 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5802 if ( right
> lastColRight
)
5803 right
= lastColRight
;
5806 if ( m_gridLinesClipVert
)
5811 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5812 if ( bottom
> lastRowBottom
)
5813 bottom
= lastRowBottom
;
5816 // no gridlines inside multicells, clip them out
5817 int leftCol
= GetColPos( internalXToCol(left
) );
5818 int topRow
= internalYToRow(top
);
5819 int rightCol
= GetColPos( internalXToCol(right
) );
5820 int bottomRow
= internalYToRow(bottom
);
5822 wxRegion
clippedcells(0, 0, cw
, ch
);
5824 int cell_rows
, cell_cols
;
5827 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5829 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5831 int i
= GetColAt( colPos
);
5833 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5834 if ((cell_rows
> 1) || (cell_cols
> 1))
5836 rect
= CellToRect(j
,i
);
5837 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5838 clippedcells
.Subtract(rect
);
5840 else if ((cell_rows
< 0) || (cell_cols
< 0))
5842 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5843 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5844 clippedcells
.Subtract(rect
);
5849 dc
.SetDeviceClippingRegion( clippedcells
);
5852 top
, left
, bottom
, right
,
5853 topRow
, leftCol
, m_numRows
, m_numCols
);
5855 dc
.DestroyClippingRegion();
5859 wxGrid::DoDrawGridLines(wxDC
& dc
,
5861 int bottom
, int right
,
5862 int topRow
, int leftCol
,
5863 int bottomRow
, int rightCol
)
5865 // horizontal grid lines
5866 for ( int i
= topRow
; i
< bottomRow
; i
++ )
5868 int bot
= GetRowBottom(i
) - 1;
5875 dc
.SetPen( GetRowGridLinePen(i
) );
5876 dc
.DrawLine( left
, bot
, right
, bot
);
5880 // vertical grid lines
5881 for ( int colPos
= leftCol
; colPos
< rightCol
; colPos
++ )
5883 int i
= GetColAt( colPos
);
5885 int colRight
= GetColRight(i
);
5887 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5891 if ( colRight
> right
)
5894 if ( colRight
>= left
)
5896 dc
.SetPen( GetColGridLinePen(i
) );
5897 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5902 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5907 const size_t numLabels
= rows
.GetCount();
5908 for ( size_t i
= 0; i
< numLabels
; i
++ )
5910 DrawRowLabel( dc
, rows
[i
] );
5914 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5916 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5919 wxGridCellAttrProvider
* const
5920 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5922 // notice that an explicit static_cast is needed to avoid a compilation
5923 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5924 // wxGridRowHeaderRenderer class without it
5925 const wxGridRowHeaderRenderer
&
5926 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5927 : static_cast<const wxGridRowHeaderRenderer
&>
5928 (gs_defaultHeaderRenderers
.rowRenderer
);
5930 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5931 rend
.DrawBorder(*this, dc
, rect
);
5934 GetRowLabelAlignment(&hAlign
, &vAlign
);
5936 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5937 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5940 void wxGrid::UseNativeColHeader(bool native
)
5942 if ( native
== m_useNativeHeader
)
5946 m_useNativeHeader
= native
;
5948 CreateColumnWindow();
5950 if ( m_useNativeHeader
)
5951 GetGridColHeader()->SetColumnCount(m_numCols
);
5955 void wxGrid::SetUseNativeColLabels( bool native
)
5957 wxASSERT_MSG( !m_useNativeHeader
,
5958 "doesn't make sense when using native header" );
5960 m_nativeColumnLabels
= native
;
5963 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5964 SetColLabelSize( height
);
5967 GetColLabelWindow()->Refresh();
5968 m_cornerLabelWin
->Refresh();
5971 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5976 const size_t numLabels
= cols
.GetCount();
5977 for ( size_t i
= 0; i
< numLabels
; i
++ )
5979 DrawColLabel( dc
, cols
[i
] );
5983 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5985 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5987 if ( m_nativeColumnLabels
)
5991 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5998 wxGridCellAttrProvider
* const
5999 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
6000 const wxGridCornerHeaderRenderer
&
6001 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
6002 : static_cast<wxGridCornerHeaderRenderer
&>
6003 (gs_defaultHeaderRenderers
.cornerRenderer
);
6005 rend
.DrawBorder(*this, dc
, rect
);
6009 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
6011 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
6014 int colLeft
= GetColLeft(col
);
6016 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
6017 wxGridCellAttrProvider
* const
6018 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
6019 const wxGridColumnHeaderRenderer
&
6020 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
6021 : static_cast<wxGridColumnHeaderRenderer
&>
6022 (gs_defaultHeaderRenderers
.colRenderer
);
6024 if ( m_nativeColumnLabels
)
6026 wxRendererNative::Get().DrawHeaderButton
6028 GetColLabelWindow(),
6033 ? IsSortOrderAscending()
6034 ? wxHDR_SORT_ICON_UP
6035 : wxHDR_SORT_ICON_DOWN
6036 : wxHDR_SORT_ICON_NONE
6042 // It is reported that we need to erase the background to avoid display
6043 // artefacts, see #12055.
6044 wxDCBrushChanger
setBrush(dc
, m_colWindow
->GetBackgroundColour());
6045 dc
.DrawRectangle(rect
);
6047 rend
.DrawBorder(*this, dc
, rect
);
6051 GetColLabelAlignment(&hAlign
, &vAlign
);
6052 const int orient
= GetColLabelTextOrientation();
6054 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
6057 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
6058 // we just have to add textOrientation support
6059 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6060 const wxString
& value
,
6064 int textOrientation
) const
6066 wxArrayString lines
;
6068 StringToLines( value
, lines
);
6070 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
6073 void wxGrid::DrawTextRectangle(wxDC
& dc
,
6074 const wxArrayString
& lines
,
6078 int textOrientation
) const
6080 if ( lines
.empty() )
6083 wxDCClipper
clip(dc
, rect
);
6088 if ( textOrientation
== wxHORIZONTAL
)
6089 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6091 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
6095 switch ( vertAlign
)
6097 case wxALIGN_BOTTOM
:
6098 if ( textOrientation
== wxHORIZONTAL
)
6099 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6101 x
= rect
.x
+ rect
.width
- textWidth
;
6104 case wxALIGN_CENTRE
:
6105 if ( textOrientation
== wxHORIZONTAL
)
6106 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
6108 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
6113 if ( textOrientation
== wxHORIZONTAL
)
6120 // Align each line of a multi-line label
6121 size_t nLines
= lines
.GetCount();
6122 for ( size_t l
= 0; l
< nLines
; l
++ )
6124 const wxString
& line
= lines
[l
];
6128 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
6132 wxCoord lineWidth
= 0,
6134 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
6136 switch ( horizAlign
)
6139 if ( textOrientation
== wxHORIZONTAL
)
6140 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
6142 y
= rect
.y
+ lineWidth
+ 1;
6145 case wxALIGN_CENTRE
:
6146 if ( textOrientation
== wxHORIZONTAL
)
6147 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
6149 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
6154 if ( textOrientation
== wxHORIZONTAL
)
6157 y
= rect
.y
+ rect
.height
- 1;
6161 if ( textOrientation
== wxHORIZONTAL
)
6163 dc
.DrawText( line
, x
, y
);
6168 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
6174 // Split multi-line text up into an array of strings.
6175 // Any existing contents of the string array are preserved.
6177 // TODO: refactor wxTextFile::Read() and reuse the same code from here
6178 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
6182 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6183 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6185 while ( startPos
< (int)tVal
.length() )
6187 pos
= tVal
.Mid(startPos
).Find( eol
);
6192 else if ( pos
== 0 )
6194 lines
.Add( wxEmptyString
);
6198 lines
.Add( tVal
.Mid(startPos
, pos
) );
6201 startPos
+= pos
+ 1;
6204 if ( startPos
< (int)tVal
.length() )
6206 lines
.Add( tVal
.Mid( startPos
) );
6210 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
6211 const wxArrayString
& lines
,
6212 long *width
, long *height
) const
6216 wxCoord lineW
= 0, lineH
= 0;
6219 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6221 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6222 w
= wxMax( w
, lineW
);
6231 // ------ Batch processing.
6233 void wxGrid::EndBatch()
6235 if ( m_batchCount
> 0 )
6238 if ( !m_batchCount
)
6241 m_rowLabelWin
->Refresh();
6242 m_colWindow
->Refresh();
6243 m_cornerLabelWin
->Refresh();
6244 m_gridWin
->Refresh();
6249 // Use this, rather than wxWindow::Refresh(), to force an immediate
6250 // repainting of the grid. Has no effect if you are already inside a
6251 // BeginBatch / EndBatch block.
6253 void wxGrid::ForceRefresh()
6259 bool wxGrid::Enable(bool enable
)
6261 if ( !wxScrolledWindow::Enable(enable
) )
6264 // redraw in the new state
6265 m_gridWin
->Refresh();
6271 // ------ Edit control functions
6274 void wxGrid::EnableEditing( bool edit
)
6276 if ( edit
!= m_editable
)
6279 EnableCellEditControl(edit
);
6284 void wxGrid::EnableCellEditControl( bool enable
)
6289 if ( enable
!= m_cellEditCtrlEnabled
)
6293 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
6296 // this should be checked by the caller!
6297 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
6299 // do it before ShowCellEditControl()
6300 m_cellEditCtrlEnabled
= enable
;
6302 ShowCellEditControl();
6306 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
6308 HideCellEditControl();
6309 SaveEditControlValue();
6311 // do it after HideCellEditControl()
6312 m_cellEditCtrlEnabled
= enable
;
6317 bool wxGrid::IsCurrentCellReadOnly() const
6320 attr
= const_cast<wxGrid
*>(this)->GetCellAttr(m_currentCellCoords
);
6321 bool readonly
= attr
->IsReadOnly();
6327 bool wxGrid::CanEnableCellControl() const
6329 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
6330 !IsCurrentCellReadOnly();
6333 bool wxGrid::IsCellEditControlEnabled() const
6335 // the cell edit control might be disable for all cells or just for the
6336 // current one if it's read only
6337 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
6340 bool wxGrid::IsCellEditControlShown() const
6342 bool isShown
= false;
6344 if ( m_cellEditCtrlEnabled
)
6346 int row
= m_currentCellCoords
.GetRow();
6347 int col
= m_currentCellCoords
.GetCol();
6348 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6349 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6354 if ( editor
->IsCreated() )
6356 isShown
= editor
->GetControl()->IsShown();
6366 void wxGrid::ShowCellEditControl()
6368 if ( IsCellEditControlEnabled() )
6370 if ( !IsVisible( m_currentCellCoords
, false ) )
6372 m_cellEditCtrlEnabled
= false;
6377 wxRect rect
= CellToRect( m_currentCellCoords
);
6378 int row
= m_currentCellCoords
.GetRow();
6379 int col
= m_currentCellCoords
.GetCol();
6381 // if this is part of a multicell, find owner (topleft)
6382 int cell_rows
, cell_cols
;
6383 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6384 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6388 m_currentCellCoords
.SetRow( row
);
6389 m_currentCellCoords
.SetCol( col
);
6392 // erase the highlight and the cell contents because the editor
6393 // might not cover the entire cell
6394 wxClientDC
dc( m_gridWin
);
6396 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6397 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
6398 dc
.SetPen(*wxTRANSPARENT_PEN
);
6399 dc
.DrawRectangle(rect
);
6401 // convert to scrolled coords
6402 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6408 // cell is shifted by one pixel
6409 // However, don't allow x or y to become negative
6410 // since the SetSize() method interprets that as
6417 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6418 if ( !editor
->IsCreated() )
6420 editor
->Create(m_gridWin
, wxID_ANY
,
6421 new wxGridCellEditorEvtHandler(this, editor
));
6423 wxGridEditorCreatedEvent
evt(GetId(),
6424 wxEVT_GRID_EDITOR_CREATED
,
6428 editor
->GetControl());
6429 GetEventHandler()->ProcessEvent(evt
);
6432 // resize editor to overflow into righthand cells if allowed
6433 int maxWidth
= rect
.width
;
6434 wxString value
= GetCellValue(row
, col
);
6435 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
6438 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
6439 if (maxWidth
< rect
.width
)
6440 maxWidth
= rect
.width
;
6443 int client_right
= m_gridWin
->GetClientSize().GetWidth();
6444 if (rect
.x
+ maxWidth
> client_right
)
6445 maxWidth
= client_right
- rect
.x
;
6447 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
6449 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6450 // may have changed earlier
6451 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
6454 GetCellSize( row
, i
, &c_rows
, &c_cols
);
6456 // looks weird going over a multicell
6457 if (m_table
->IsEmptyCell( row
, i
) &&
6458 (rect
.width
< maxWidth
) && (c_rows
== 1))
6460 rect
.width
+= GetColWidth( i
);
6466 if (rect
.GetRight() > client_right
)
6467 rect
.SetRight( client_right
- 1 );
6470 editor
->SetCellAttr( attr
);
6471 editor
->SetSize( rect
);
6473 editor
->GetControl()->Move(
6474 editor
->GetControl()->GetPosition().x
+ nXMove
,
6475 editor
->GetControl()->GetPosition().y
);
6476 editor
->Show( true, attr
);
6478 // recalc dimensions in case we need to
6479 // expand the scrolled window to account for editor
6482 editor
->BeginEdit(row
, col
, this);
6483 editor
->SetCellAttr(NULL
);
6491 void wxGrid::HideCellEditControl()
6493 if ( IsCellEditControlEnabled() )
6495 int row
= m_currentCellCoords
.GetRow();
6496 int col
= m_currentCellCoords
.GetCol();
6498 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6499 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6500 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6501 editor
->Show( false );
6505 // return the focus to the grid itself if the editor had it
6507 // note that we must not do this unconditionally to avoid stealing
6508 // focus from the window which just received it if we are hiding the
6509 // editor precisely because we lost focus
6510 if ( editorHadFocus
)
6511 m_gridWin
->SetFocus();
6513 // refresh whole row to the right
6514 wxRect
rect( CellToRect(row
, col
) );
6515 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6516 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6519 // ensure that the pixels under the focus ring get refreshed as well
6520 rect
.Inflate(10, 10);
6523 m_gridWin
->Refresh( false, &rect
);
6527 void wxGrid::SaveEditControlValue()
6529 if ( IsCellEditControlEnabled() )
6531 int row
= m_currentCellCoords
.GetRow();
6532 int col
= m_currentCellCoords
.GetCol();
6534 wxString oldval
= GetCellValue(row
, col
);
6536 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6537 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6540 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6542 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6544 editor
->ApplyEdit(row
, col
, this);
6546 // for compatibility reasons dating back to wx 2.8 when this event
6547 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6548 // didn't exist we allow vetoing this one too
6549 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6551 // Event has been vetoed, set the data back.
6552 SetCellValue(row
, col
, oldval
);
6561 void wxGrid::OnHideEditor(wxCommandEvent
& WXUNUSED(event
))
6563 DisableCellEditControl();
6567 // ------ Grid location functions
6568 // Note that all of these functions work with the logical coordinates of
6569 // grid cells and labels so you will need to convert from device
6570 // coordinates for mouse events etc.
6573 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6575 int row
= YToRow(y
);
6576 int col
= XToCol(x
);
6578 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6579 : wxGridCellCoords(row
, col
);
6582 // compute row or column from some (unscrolled) coordinate value, using either
6583 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6584 // m_rowBottoms/m_colRights to do it quickly in O(log n) time.
6585 // NOTE: This may not work correctly for reordered columns.
6586 int wxGrid::PosToLinePos(int coord
,
6588 const wxGridOperations
& oper
) const
6590 const int numLines
= oper
.GetNumberOfLines(this);
6593 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6595 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6596 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6598 int maxPos
= coord
/ defaultLineSize
,
6601 // check for the simplest case: if we have no explicit line sizes
6602 // configured, then we already know the line this position falls in
6603 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6604 if ( lineEnds
.empty() )
6606 if ( maxPos
< numLines
)
6609 return clipToMinMax
? numLines
- 1 : -1;
6613 // binary search is quite efficient and we can't really make any assumptions
6614 // on where to start here since row and columns could be of size 0 if they are
6615 // hidden. While this could be made more efficient, some profiling will be
6616 // necessary to determine if it really is a performance bottleneck
6617 maxPos
= numLines
- 1;
6619 // check if the position is beyond the last column
6620 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6621 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6622 return clipToMinMax
? maxPos
: -1;
6624 // or before the first one
6625 const int lineAt0
= oper
.GetLineAt(this, 0);
6626 if ( coord
< lineEnds
[lineAt0
] )
6630 // finally do perform the binary search
6631 while ( minPos
< maxPos
)
6633 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6634 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6636 "wxGrid: internal error in PosToLinePos()" );
6638 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6643 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6644 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6654 wxGrid::PosToLine(int coord
,
6656 const wxGridOperations
& oper
) const
6658 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6660 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6663 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6665 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6668 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6670 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6673 int wxGrid::XToPos(int x
) const
6675 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6678 // return the row/col number such that the pos is near the edge of, or -1 if
6679 // not near an edge.
6681 // notice that position can only possibly be near an edge if the row/column is
6682 // large enough to still allow for an "inner" area that is _not_ near the edge
6683 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6684 // _never_ be considered to be near the edge).
6685 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6687 // Get the bottom or rightmost line that could match.
6688 int line
= oper
.PosToLine(this, pos
, true);
6690 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6692 // We know that we are in this line, test whether we are close enough
6693 // to start or end border, respectively.
6694 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6696 else if ( line
> 0 &&
6697 pos
- oper
.GetLineStartPos(this,
6698 line
) < WXGRID_LABEL_EDGE_ZONE
)
6700 // We need to find the previous visible line, so skip all the
6701 // hidden (of size 0) ones.
6704 line
= oper
.GetLineBefore(this, line
);
6706 while ( line
>= 0 && oper
.GetLineSize(this, line
) == 0 );
6708 // It can possibly be -1 here.
6716 int wxGrid::YToEdgeOfRow(int y
) const
6718 return PosToEdgeOfLine(y
, wxGridRowOperations());
6721 int wxGrid::XToEdgeOfCol(int x
) const
6723 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6726 wxRect
wxGrid::CellToRect( int row
, int col
) const
6728 wxRect
rect( -1, -1, -1, -1 );
6730 if ( row
>= 0 && row
< m_numRows
&&
6731 col
>= 0 && col
< m_numCols
)
6733 int i
, cell_rows
, cell_cols
;
6734 rect
.width
= rect
.height
= 0;
6735 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6736 // if negative then find multicell owner
6741 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6743 rect
.x
= GetColLeft(col
);
6744 rect
.y
= GetRowTop(row
);
6745 for (i
=col
; i
< col
+ cell_cols
; i
++)
6746 rect
.width
+= GetColWidth(i
);
6747 for (i
=row
; i
< row
+ cell_rows
; i
++)
6748 rect
.height
+= GetRowHeight(i
);
6750 // if grid lines are enabled, then the area of the cell is a bit smaller
6751 if (m_gridLinesEnabled
)
6761 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6763 // get the cell rectangle in logical coords
6765 wxRect
r( CellToRect( row
, col
) );
6767 // convert to device coords
6769 int left
, top
, right
, bottom
;
6770 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6771 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6773 // check against the client area of the grid window
6775 m_gridWin
->GetClientSize( &cw
, &ch
);
6777 if ( wholeCellVisible
)
6779 // is the cell wholly visible ?
6780 return ( left
>= 0 && right
<= cw
&&
6781 top
>= 0 && bottom
<= ch
);
6785 // is the cell partly visible ?
6787 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6788 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6792 // make the specified cell location visible by doing a minimal amount
6795 void wxGrid::MakeCellVisible( int row
, int col
)
6798 int xpos
= -1, ypos
= -1;
6800 if ( row
>= 0 && row
< m_numRows
&&
6801 col
>= 0 && col
< m_numCols
)
6803 // get the cell rectangle in logical coords
6804 wxRect
r( CellToRect( row
, col
) );
6806 // convert to device coords
6807 int left
, top
, right
, bottom
;
6808 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6809 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6812 m_gridWin
->GetClientSize( &cw
, &ch
);
6818 else if ( bottom
> ch
)
6820 int h
= r
.GetHeight();
6822 for ( i
= row
- 1; i
>= 0; i
-- )
6824 int rowHeight
= GetRowHeight(i
);
6825 if ( h
+ rowHeight
> ch
)
6832 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6833 // have rounding errors (this is important, because if we do,
6834 // we might not scroll at all and some cells won't be redrawn)
6836 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6837 // so just add a full scroll unit...
6838 ypos
+= m_yScrollPixelsPerLine
;
6841 // special handling for wide cells - show always left part of the cell!
6842 // Otherwise, e.g. when stepping from row to row, it would jump between
6843 // left and right part of the cell on every step!
6845 if ( left
< 0 || (right
- left
) >= cw
)
6849 else if ( right
> cw
)
6851 // position the view so that the cell is on the right
6853 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6854 xpos
= x0
+ (right
- cw
);
6856 // see comment for ypos above
6857 xpos
+= m_xScrollPixelsPerLine
;
6860 if ( xpos
!= -1 || ypos
!= -1 )
6863 xpos
/= m_xScrollPixelsPerLine
;
6865 ypos
/= m_yScrollPixelsPerLine
;
6866 Scroll( xpos
, ypos
);
6873 // ------ Grid cursor movement functions
6877 wxGrid::DoMoveCursor(bool expandSelection
,
6878 const wxGridDirectionOperations
& diroper
)
6880 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6883 if ( expandSelection
)
6885 wxGridCellCoords coords
= m_selectedBlockCorner
;
6886 if ( coords
== wxGridNoCellCoords
)
6887 coords
= m_currentCellCoords
;
6889 if ( diroper
.IsAtBoundary(coords
) )
6892 diroper
.Advance(coords
);
6894 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6896 else // don't expand selection
6900 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6903 wxGridCellCoords coords
= m_currentCellCoords
;
6904 diroper
.Advance(coords
);
6912 bool wxGrid::MoveCursorUp(bool expandSelection
)
6914 return DoMoveCursor(expandSelection
,
6915 wxGridBackwardOperations(this, wxGridRowOperations()));
6918 bool wxGrid::MoveCursorDown(bool expandSelection
)
6920 return DoMoveCursor(expandSelection
,
6921 wxGridForwardOperations(this, wxGridRowOperations()));
6924 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6926 return DoMoveCursor(expandSelection
,
6927 wxGridBackwardOperations(this, wxGridColumnOperations()));
6930 bool wxGrid::MoveCursorRight(bool expandSelection
)
6932 return DoMoveCursor(expandSelection
,
6933 wxGridForwardOperations(this, wxGridColumnOperations()));
6936 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6938 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6941 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6944 const int oldRow
= m_currentCellCoords
.GetRow();
6945 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6946 if ( newRow
== oldRow
)
6948 wxGridCellCoords
coords(m_currentCellCoords
);
6949 diroper
.Advance(coords
);
6950 newRow
= coords
.GetRow();
6953 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6958 bool wxGrid::MovePageUp()
6960 return DoMoveCursorByPage(
6961 wxGridBackwardOperations(this, wxGridRowOperations()));
6964 bool wxGrid::MovePageDown()
6966 return DoMoveCursorByPage(
6967 wxGridForwardOperations(this, wxGridRowOperations()));
6970 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6971 // until we find a non-empty cell or reach the grid end
6973 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6974 const wxGridDirectionOperations
& diroper
)
6976 while ( !diroper
.IsAtBoundary(coords
) )
6978 diroper
.Advance(coords
);
6979 if ( !m_table
->IsEmpty(coords
) )
6985 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6986 const wxGridDirectionOperations
& diroper
)
6988 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6991 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6994 wxGridCellCoords
coords(m_currentCellCoords
);
6995 if ( m_table
->IsEmpty(coords
) )
6997 // we are in an empty cell: find the next block of non-empty cells
6998 AdvanceToNextNonEmpty(coords
, diroper
);
7000 else // current cell is not empty
7002 diroper
.Advance(coords
);
7003 if ( m_table
->IsEmpty(coords
) )
7005 // we started at the end of a block, find the next one
7006 AdvanceToNextNonEmpty(coords
, diroper
);
7008 else // we're in a middle of a block
7010 // go to the end of it, i.e. find the last cell before the next
7012 while ( !diroper
.IsAtBoundary(coords
) )
7014 wxGridCellCoords
coordsNext(coords
);
7015 diroper
.Advance(coordsNext
);
7016 if ( m_table
->IsEmpty(coordsNext
) )
7019 coords
= coordsNext
;
7024 if ( expandSelection
)
7026 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
7037 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
7039 return DoMoveCursorByBlock(
7041 wxGridBackwardOperations(this, wxGridRowOperations())
7045 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7047 return DoMoveCursorByBlock(
7049 wxGridForwardOperations(this, wxGridRowOperations())
7053 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7055 return DoMoveCursorByBlock(
7057 wxGridBackwardOperations(this, wxGridColumnOperations())
7061 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7063 return DoMoveCursorByBlock(
7065 wxGridForwardOperations(this, wxGridColumnOperations())
7070 // ------ Label values and formatting
7073 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
7076 *horiz
= m_rowLabelHorizAlign
;
7078 *vert
= m_rowLabelVertAlign
;
7081 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
7084 *horiz
= m_colLabelHorizAlign
;
7086 *vert
= m_colLabelVertAlign
;
7089 int wxGrid::GetColLabelTextOrientation() const
7091 return m_colLabelTextOrientation
;
7094 wxString
wxGrid::GetRowLabelValue( int row
) const
7098 return m_table
->GetRowLabelValue( row
);
7108 wxString
wxGrid::GetColLabelValue( int col
) const
7112 return m_table
->GetColLabelValue( col
);
7122 void wxGrid::SetRowLabelSize( int width
)
7124 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
7126 if ( width
== wxGRID_AUTOSIZE
)
7128 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
7131 if ( width
!= m_rowLabelWidth
)
7135 m_rowLabelWin
->Show( false );
7136 m_cornerLabelWin
->Show( false );
7138 else if ( m_rowLabelWidth
== 0 )
7140 m_rowLabelWin
->Show( true );
7141 if ( m_colLabelHeight
> 0 )
7142 m_cornerLabelWin
->Show( true );
7145 m_rowLabelWidth
= width
;
7146 InvalidateBestSize();
7148 wxScrolledWindow::Refresh( true );
7152 void wxGrid::SetColLabelSize( int height
)
7154 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
7156 if ( height
== wxGRID_AUTOSIZE
)
7158 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
7161 if ( height
!= m_colLabelHeight
)
7165 m_colWindow
->Show( false );
7166 m_cornerLabelWin
->Show( false );
7168 else if ( m_colLabelHeight
== 0 )
7170 m_colWindow
->Show( true );
7171 if ( m_rowLabelWidth
> 0 )
7172 m_cornerLabelWin
->Show( true );
7175 m_colLabelHeight
= height
;
7176 InvalidateBestSize();
7178 wxScrolledWindow::Refresh( true );
7182 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7184 if ( m_labelBackgroundColour
!= colour
)
7186 m_labelBackgroundColour
= colour
;
7187 m_rowLabelWin
->SetBackgroundColour( colour
);
7188 m_colWindow
->SetBackgroundColour( colour
);
7189 m_cornerLabelWin
->SetBackgroundColour( colour
);
7191 if ( !GetBatchCount() )
7193 m_rowLabelWin
->Refresh();
7194 m_colWindow
->Refresh();
7195 m_cornerLabelWin
->Refresh();
7200 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7202 if ( m_labelTextColour
!= colour
)
7204 m_labelTextColour
= colour
;
7205 if ( !GetBatchCount() )
7207 m_rowLabelWin
->Refresh();
7208 m_colWindow
->Refresh();
7213 void wxGrid::SetLabelFont( const wxFont
& font
)
7216 if ( !GetBatchCount() )
7218 m_rowLabelWin
->Refresh();
7219 m_colWindow
->Refresh();
7223 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7225 // allow old (incorrect) defs to be used
7228 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7229 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7230 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7235 case wxTOP
: vert
= wxALIGN_TOP
; break;
7236 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7237 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7240 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7242 m_rowLabelHorizAlign
= horiz
;
7245 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7247 m_rowLabelVertAlign
= vert
;
7250 if ( !GetBatchCount() )
7252 m_rowLabelWin
->Refresh();
7256 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7258 // allow old (incorrect) defs to be used
7261 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7262 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7263 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7268 case wxTOP
: vert
= wxALIGN_TOP
; break;
7269 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7270 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7273 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7275 m_colLabelHorizAlign
= horiz
;
7278 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7280 m_colLabelVertAlign
= vert
;
7283 if ( !GetBatchCount() )
7285 m_colWindow
->Refresh();
7289 // Note: under MSW, the default column label font must be changed because it
7290 // does not support vertical printing
7292 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
7293 // pGrid->SetLabelFont(font);
7294 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
7296 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
7298 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
7299 m_colLabelTextOrientation
= textOrientation
;
7301 if ( !GetBatchCount() )
7302 m_colWindow
->Refresh();
7305 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7309 m_table
->SetRowLabelValue( row
, s
);
7310 if ( !GetBatchCount() )
7312 wxRect rect
= CellToRect( row
, 0 );
7313 if ( rect
.height
> 0 )
7315 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7317 rect
.width
= m_rowLabelWidth
;
7318 m_rowLabelWin
->Refresh( true, &rect
);
7324 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7328 m_table
->SetColLabelValue( col
, s
);
7329 if ( !GetBatchCount() )
7331 if ( m_useNativeHeader
)
7333 GetGridColHeader()->UpdateColumn(col
);
7337 wxRect rect
= CellToRect( 0, col
);
7338 if ( rect
.width
> 0 )
7340 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7342 rect
.height
= m_colLabelHeight
;
7343 GetColLabelWindow()->Refresh( true, &rect
);
7350 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7352 if ( m_gridLineColour
!= colour
)
7354 m_gridLineColour
= colour
;
7356 if ( GridLinesEnabled() )
7361 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7363 if ( m_cellHighlightColour
!= colour
)
7365 m_cellHighlightColour
= colour
;
7367 wxClientDC
dc( m_gridWin
);
7369 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7370 DrawCellHighlight(dc
, attr
);
7375 void wxGrid::SetCellHighlightPenWidth(int width
)
7377 if (m_cellHighlightPenWidth
!= width
)
7379 m_cellHighlightPenWidth
= width
;
7381 // Just redrawing the cell highlight is not enough since that won't
7382 // make any visible change if the thickness is getting smaller.
7383 int row
= m_currentCellCoords
.GetRow();
7384 int col
= m_currentCellCoords
.GetCol();
7385 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7388 wxRect rect
= CellToRect(row
, col
);
7389 m_gridWin
->Refresh(true, &rect
);
7393 void wxGrid::SetCellHighlightROPenWidth(int width
)
7395 if (m_cellHighlightROPenWidth
!= width
)
7397 m_cellHighlightROPenWidth
= width
;
7399 // Just redrawing the cell highlight is not enough since that won't
7400 // make any visible change if the thickness is getting smaller.
7401 int row
= m_currentCellCoords
.GetRow();
7402 int col
= m_currentCellCoords
.GetCol();
7403 if ( row
== -1 || col
== -1 ||
7404 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7407 wxRect rect
= CellToRect(row
, col
);
7408 m_gridWin
->Refresh(true, &rect
);
7412 void wxGrid::RedrawGridLines()
7414 // the lines will be redrawn when the window is thawn
7415 if ( GetBatchCount() )
7418 if ( GridLinesEnabled() )
7420 wxClientDC
dc( m_gridWin
);
7422 DrawAllGridLines( dc
, wxRegion() );
7424 else // remove the grid lines
7426 m_gridWin
->Refresh();
7430 void wxGrid::EnableGridLines( bool enable
)
7432 if ( enable
!= m_gridLinesEnabled
)
7434 m_gridLinesEnabled
= enable
;
7440 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
7446 if ( GridLinesEnabled() )
7451 int wxGrid::GetDefaultRowSize() const
7453 return m_defaultRowHeight
;
7456 int wxGrid::GetRowSize( int row
) const
7458 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
7460 return GetRowHeight(row
);
7463 int wxGrid::GetDefaultColSize() const
7465 return m_defaultColWidth
;
7468 int wxGrid::GetColSize( int col
) const
7470 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
7472 return GetColWidth(col
);
7475 // ============================================================================
7476 // access to the grid attributes: each of them has a default value in the grid
7477 // itself and may be overidden on a per-cell basis
7478 // ============================================================================
7480 // ----------------------------------------------------------------------------
7481 // setting default attributes
7482 // ----------------------------------------------------------------------------
7484 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7486 m_defaultCellAttr
->SetBackgroundColour(col
);
7488 m_gridWin
->SetBackgroundColour(col
);
7492 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7494 m_defaultCellAttr
->SetTextColour(col
);
7497 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7499 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7502 void wxGrid::SetDefaultCellOverflow( bool allow
)
7504 m_defaultCellAttr
->SetOverflow(allow
);
7507 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7509 m_defaultCellAttr
->SetFont(font
);
7512 // For editors and renderers the type registry takes precedence over the
7513 // default attr, so we need to register the new editor/renderer for the string
7514 // data type in order to make setting a default editor/renderer appear to
7517 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7519 RegisterDataType(wxGRID_VALUE_STRING
,
7521 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7524 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7526 RegisterDataType(wxGRID_VALUE_STRING
,
7527 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7531 // ----------------------------------------------------------------------------
7532 // access to the default attributes
7533 // ----------------------------------------------------------------------------
7535 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7537 return m_defaultCellAttr
->GetBackgroundColour();
7540 wxColour
wxGrid::GetDefaultCellTextColour() const
7542 return m_defaultCellAttr
->GetTextColour();
7545 wxFont
wxGrid::GetDefaultCellFont() const
7547 return m_defaultCellAttr
->GetFont();
7550 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7552 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7555 bool wxGrid::GetDefaultCellOverflow() const
7557 return m_defaultCellAttr
->GetOverflow();
7560 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7562 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7565 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7567 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7570 // ----------------------------------------------------------------------------
7571 // access to cell attributes
7572 // ----------------------------------------------------------------------------
7574 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7576 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7577 wxColour colour
= attr
->GetBackgroundColour();
7583 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7585 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7586 wxColour colour
= attr
->GetTextColour();
7592 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7594 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7595 wxFont font
= attr
->GetFont();
7601 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7603 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7604 attr
->GetAlignment(horiz
, vert
);
7608 bool wxGrid::GetCellOverflow( int row
, int col
) const
7610 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7611 bool allow
= attr
->GetOverflow();
7618 wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7620 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7621 attr
->GetSize( num_rows
, num_cols
);
7624 if ( *num_rows
== 1 && *num_cols
== 1 )
7625 return CellSpan_None
; // just a normal cell
7627 if ( *num_rows
< 0 || *num_cols
< 0 )
7628 return CellSpan_Inside
; // covered by a multi-span cell
7630 // this cell spans multiple cells to its right/bottom
7631 return CellSpan_Main
;
7634 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7636 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7637 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7643 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7645 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7646 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7652 bool wxGrid::IsReadOnly(int row
, int col
) const
7654 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7655 bool isReadOnly
= attr
->IsReadOnly();
7661 // ----------------------------------------------------------------------------
7662 // attribute support: cache, automatic provider creation, ...
7663 // ----------------------------------------------------------------------------
7665 bool wxGrid::CanHaveAttributes() const
7672 return m_table
->CanHaveAttributes();
7675 void wxGrid::ClearAttrCache()
7677 if ( m_attrCache
.row
!= -1 )
7679 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7680 m_attrCache
.attr
= NULL
;
7681 m_attrCache
.row
= -1;
7682 // wxSafeDecRec(...) might cause event processing that accesses
7683 // the cached attribute, if one exists (e.g. by deleting the
7684 // editor stored within the attribute). Therefore it is important
7685 // to invalidate the cache before calling wxSafeDecRef!
7686 wxSafeDecRef(oldAttr
);
7690 void wxGrid::RefreshAttr(int row
, int col
)
7692 if ( m_attrCache
.row
== row
&& m_attrCache
.col
== col
)
7697 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7701 wxGrid
* const self
= const_cast<wxGrid
*>(this);
7703 self
->ClearAttrCache();
7704 self
->m_attrCache
.row
= row
;
7705 self
->m_attrCache
.col
= col
;
7706 self
->m_attrCache
.attr
= attr
;
7711 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7713 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7715 *attr
= m_attrCache
.attr
;
7716 wxSafeIncRef(m_attrCache
.attr
);
7718 #ifdef DEBUG_ATTR_CACHE
7719 gs_nAttrCacheHits
++;
7726 #ifdef DEBUG_ATTR_CACHE
7727 gs_nAttrCacheMisses
++;
7734 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7736 wxGridCellAttr
*attr
= NULL
;
7737 // Additional test to avoid looking at the cache e.g. for
7738 // wxNoCellCoords, as this will confuse memory management.
7741 if ( !LookupAttr(row
, col
, &attr
) )
7743 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7745 CacheAttr(row
, col
, attr
);
7751 attr
->SetDefAttr(m_defaultCellAttr
);
7755 attr
= m_defaultCellAttr
;
7762 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7764 wxGridCellAttr
*attr
= NULL
;
7765 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7767 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7768 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7770 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7773 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7775 // artificially inc the ref count to match DecRef() in caller
7777 m_table
->SetAttr(attr
, row
, col
);
7783 // ----------------------------------------------------------------------------
7784 // setting column attributes (wrappers around SetColAttr)
7785 // ----------------------------------------------------------------------------
7787 void wxGrid::SetColFormatBool(int col
)
7789 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7792 void wxGrid::SetColFormatNumber(int col
)
7794 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7797 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7799 wxString typeName
= wxGRID_VALUE_FLOAT
;
7800 if ( (width
!= -1) || (precision
!= -1) )
7802 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7805 SetColFormatCustom(col
, typeName
);
7808 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7810 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7812 attr
= new wxGridCellAttr
;
7813 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7814 attr
->SetRenderer(renderer
);
7815 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7816 attr
->SetEditor(editor
);
7818 SetColAttr(col
, attr
);
7822 // ----------------------------------------------------------------------------
7823 // setting cell attributes: this is forwarded to the table
7824 // ----------------------------------------------------------------------------
7826 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7828 if ( CanHaveAttributes() )
7830 m_table
->SetAttr(attr
, row
, col
);
7839 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7841 if ( CanHaveAttributes() )
7843 m_table
->SetRowAttr(attr
, row
);
7852 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7854 if ( CanHaveAttributes() )
7856 m_table
->SetColAttr(attr
, col
);
7865 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7867 if ( CanHaveAttributes() )
7869 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7870 attr
->SetBackgroundColour(colour
);
7875 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7877 if ( CanHaveAttributes() )
7879 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7880 attr
->SetTextColour(colour
);
7885 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7887 if ( CanHaveAttributes() )
7889 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7890 attr
->SetFont(font
);
7895 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7897 if ( CanHaveAttributes() )
7899 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7900 attr
->SetAlignment(horiz
, vert
);
7905 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7907 if ( CanHaveAttributes() )
7909 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7910 attr
->SetOverflow(allow
);
7915 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7917 if ( CanHaveAttributes() )
7919 int cell_rows
, cell_cols
;
7921 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7922 attr
->GetSize(&cell_rows
, &cell_cols
);
7923 attr
->SetSize(num_rows
, num_cols
);
7926 // Cannot set the size of a cell to 0 or negative values
7927 // While it is perfectly legal to do that, this function cannot
7928 // handle all the possibilies, do it by hand by getting the CellAttr.
7929 // You can only set the size of a cell to 1,1 or greater with this fn
7930 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7931 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7932 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7933 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7935 // if this was already a multicell then "turn off" the other cells first
7936 if ((cell_rows
> 1) || (cell_cols
> 1))
7939 for (j
=row
; j
< row
+ cell_rows
; j
++)
7941 for (i
=col
; i
< col
+ cell_cols
; i
++)
7943 if ((i
!= col
) || (j
!= row
))
7945 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7946 attr_stub
->SetSize( 1, 1 );
7947 attr_stub
->DecRef();
7953 // mark the cells that will be covered by this cell to
7954 // negative or zero values to point back at this cell
7955 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7958 for (j
=row
; j
< row
+ num_rows
; j
++)
7960 for (i
=col
; i
< col
+ num_cols
; i
++)
7962 if ((i
!= col
) || (j
!= row
))
7964 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7965 attr_stub
->SetSize( row
- j
, col
- i
);
7966 attr_stub
->DecRef();
7974 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7976 if ( CanHaveAttributes() )
7978 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7979 attr
->SetRenderer(renderer
);
7984 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7986 if ( CanHaveAttributes() )
7988 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7989 attr
->SetEditor(editor
);
7994 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7996 if ( CanHaveAttributes() )
7998 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7999 attr
->SetReadOnly(isReadOnly
);
8004 // ----------------------------------------------------------------------------
8005 // Data type registration
8006 // ----------------------------------------------------------------------------
8008 void wxGrid::RegisterDataType(const wxString
& typeName
,
8009 wxGridCellRenderer
* renderer
,
8010 wxGridCellEditor
* editor
)
8012 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8016 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8018 wxString typeName
= m_table
->GetTypeName(row
, col
);
8019 return GetDefaultEditorForType(typeName
);
8022 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8024 wxString typeName
= m_table
->GetTypeName(row
, col
);
8025 return GetDefaultRendererForType(typeName
);
8028 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8030 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8031 if ( index
== wxNOT_FOUND
)
8033 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
8038 return m_typeRegistry
->GetEditor(index
);
8041 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8043 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8044 if ( index
== wxNOT_FOUND
)
8046 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
8051 return m_typeRegistry
->GetRenderer(index
);
8054 // ----------------------------------------------------------------------------
8056 // ----------------------------------------------------------------------------
8058 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
8062 setFixed
= new wxGridFixedIndicesSet
;
8065 setFixed
->insert(line
);
8069 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
8071 return !setFixed
|| !setFixed
->count(line
);
8074 void wxGrid::EnableDragRowSize( bool enable
)
8076 m_canDragRowSize
= enable
;
8079 void wxGrid::EnableDragColSize( bool enable
)
8081 m_canDragColSize
= enable
;
8084 void wxGrid::EnableDragGridSize( bool enable
)
8086 m_canDragGridSize
= enable
;
8089 void wxGrid::EnableDragCell( bool enable
)
8091 m_canDragCell
= enable
;
8094 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8096 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
8098 if ( resizeExistingRows
)
8100 // since we are resizing all rows to the default row size,
8101 // we can simply clear the row heights and row bottoms
8102 // arrays (which also allows us to take advantage of
8103 // some speed optimisations)
8104 m_rowHeights
.Empty();
8105 m_rowBottoms
.Empty();
8106 if ( !GetBatchCount() )
8114 // This is a common part of SetRowSize() and SetColSize() which takes care of
8115 // updating the height/width of a row/column depending on its current value and
8118 // Returns the difference between the new and the old size.
8119 int UpdateRowOrColSize(int& sizeCurrent
, int sizeNew
)
8121 // On input here sizeCurrent can be negative if it's currently hidden (the
8122 // real size is its absolute value then). And sizeNew can be 0 to indicate
8123 // that the row/column should be hidden or -1 to indicate that it should be
8128 // We're showing back a previously hidden row/column.
8129 wxASSERT_MSG( sizeNew
== -1, wxS("New size must be positive or -1.") );
8131 // If it's already visible, simply do nothing.
8132 if ( sizeCurrent
>= 0 )
8135 // Otherwise show it by restoring its old size.
8136 sizeCurrent
= -sizeCurrent
;
8138 // This is positive which is correct.
8141 else if ( sizeNew
== 0 )
8143 // We're hiding a row/column.
8145 // If it's already hidden, simply do nothing.
8146 if ( sizeCurrent
<= 0 )
8149 // Otherwise hide it and also remember the shown size to be able to
8150 // restore it later.
8151 sizeCurrent
= -sizeCurrent
;
8153 // This is negative which is correct.
8156 else // We're just changing the row/column size.
8158 // Here it could have been hidden or not previously.
8159 const int sizeOld
= sizeCurrent
< 0 ? 0 : sizeCurrent
;
8161 sizeCurrent
= sizeNew
;
8163 return sizeCurrent
- sizeOld
;
8167 } // anonymous namespace
8169 void wxGrid::SetRowSize( int row
, int height
)
8171 // See comment in SetColSize
8172 if ( height
> 0 && height
< GetRowMinimalAcceptableHeight())
8175 // The value of -1 is special and means to fit the height to the row label.
8176 // As with the columns, ignore attempts to auto-size the hidden rows.
8177 if ( height
== -1 && GetRowHeight(row
) != 0 )
8180 wxArrayString lines
;
8181 wxClientDC
dc(m_rowLabelWin
);
8182 dc
.SetFont(GetLabelFont());
8183 StringToLines(GetRowLabelValue( row
), lines
);
8184 GetTextBoxSize( dc
, lines
, &w
, &h
);
8185 //check that it is not less than the minimal height
8186 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
8189 DoSetRowSize(row
, height
);
8192 void wxGrid::DoSetRowSize( int row
, int height
)
8194 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
8196 if ( m_rowHeights
.IsEmpty() )
8198 // need to really create the array
8202 const int diff
= UpdateRowOrColSize(m_rowHeights
[row
], height
);
8206 for ( int i
= row
; i
< m_numRows
; i
++ )
8208 m_rowBottoms
[i
] += diff
;
8211 InvalidateBestSize();
8213 if ( !GetBatchCount() )
8220 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8222 // we dont allow zero default column width
8223 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
8225 if ( resizeExistingCols
)
8227 // since we are resizing all columns to the default column size,
8228 // we can simply clear the col widths and col rights
8229 // arrays (which also allows us to take advantage of
8230 // some speed optimisations)
8231 m_colWidths
.Empty();
8232 m_colRights
.Empty();
8233 if ( !GetBatchCount() )
8238 void wxGrid::SetColSize( int col
, int width
)
8240 // we intentionally don't test whether the width is less than
8241 // GetColMinimalWidth() here but we do compare it with
8242 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
8243 // #651) -- and we also always allow the width of 0 as it has the special
8244 // sense of hiding the column
8245 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
8248 // The value of -1 is special and means to fit the width to the column label.
8250 // Notice that we currently don't support auto-sizing hidden columns (we
8251 // could, but it's not clear whether this is really needed and it would
8252 // make the code more complex), and for them passing -1 simply means to
8253 // show the column back using its old size.
8254 if ( width
== -1 && GetColWidth(col
) != 0 )
8257 wxArrayString lines
;
8258 wxClientDC
dc(m_colWindow
);
8259 dc
.SetFont(GetLabelFont());
8260 StringToLines(GetColLabelValue(col
), lines
);
8261 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
8262 GetTextBoxSize( dc
, lines
, &w
, &h
);
8264 GetTextBoxSize( dc
, lines
, &h
, &w
);
8266 //check that it is not less than the minimal width
8267 width
= wxMax(width
, GetColMinimalAcceptableWidth());
8270 DoSetColSize(col
, width
);
8273 void wxGrid::DoSetColSize( int col
, int width
)
8275 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
8277 if ( m_colWidths
.IsEmpty() )
8279 // need to really create the array
8283 const int diff
= UpdateRowOrColSize(m_colWidths
[col
], width
);
8287 if ( m_useNativeHeader
)
8288 GetGridColHeader()->UpdateColumn(col
);
8289 //else: will be refreshed when the header is redrawn
8291 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
8293 m_colRights
[GetColAt(colPos
)] += diff
;
8296 InvalidateBestSize();
8298 if ( !GetBatchCount() )
8305 void wxGrid::SetColMinimalWidth( int col
, int width
)
8307 if (width
> GetColMinimalAcceptableWidth())
8309 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
8310 m_colMinWidths
[key
] = width
;
8314 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8316 if (width
> GetRowMinimalAcceptableHeight())
8318 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
8319 m_rowMinHeights
[key
] = width
;
8323 int wxGrid::GetColMinimalWidth(int col
) const
8325 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
8326 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
8328 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
8331 int wxGrid::GetRowMinimalHeight(int row
) const
8333 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
8334 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
8336 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
8339 void wxGrid::SetColMinimalAcceptableWidth( int width
)
8341 // We do allow a width of 0 since this gives us
8342 // an easy way to temporarily hiding columns.
8344 m_minAcceptableColWidth
= width
;
8347 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
8349 // We do allow a height of 0 since this gives us
8350 // an easy way to temporarily hiding rows.
8352 m_minAcceptableRowHeight
= height
;
8355 int wxGrid::GetColMinimalAcceptableWidth() const
8357 return m_minAcceptableColWidth
;
8360 int wxGrid::GetRowMinimalAcceptableHeight() const
8362 return m_minAcceptableRowHeight
;
8365 // ----------------------------------------------------------------------------
8367 // ----------------------------------------------------------------------------
8370 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
8372 const bool column
= direction
== wxGRID_COLUMN
;
8374 // We don't support auto-sizing hidden rows or columns, this doesn't seem
8375 // to make much sense.
8378 if ( GetColWidth(colOrRow
) == 0 )
8383 if ( GetRowHeight(colOrRow
) == 0 )
8387 wxClientDC
dc(m_gridWin
);
8389 // cancel editing of cell
8390 HideCellEditControl();
8391 SaveEditControlValue();
8393 // initialize both of them just to avoid compiler warnings even if only
8394 // really needs to be initialized here
8408 wxCoord extent
, extentMax
= 0;
8409 int max
= column
? m_numRows
: m_numCols
;
8410 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8423 // we need to account for the cells spanning multiple columns/rows:
8424 // while they may need a lot of space, they don't need all of it in
8426 int numRows
, numCols
;
8427 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
8428 if ( span
== CellSpan_Inside
)
8430 // we need to get the size of the main cell, not of a cell hidden
8435 // get the size of the main cell too
8436 GetCellSize(row
, col
, &numRows
, &numCols
);
8439 // get cell ( main cell if CellSpan_Inside ) renderer best size
8440 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8441 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
8444 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8445 extent
= column
? size
.x
: size
.y
;
8447 if ( span
!= CellSpan_None
)
8449 // we spread the size of a spanning cell over all the cells it
8450 // covers evenly -- this is probably not ideal but we can't
8451 // really do much better here
8453 // notice that numCols and numRows are never 0 as they
8454 // correspond to the size of the main cell of the span and not
8455 // of the cell inside it
8456 extent
/= column
? numCols
: numRows
;
8459 if ( extent
> extentMax
)
8468 // now also compare with the column label extent
8470 dc
.SetFont( GetLabelFont() );
8474 dc
.GetMultiLineTextExtent( GetColLabelValue(colOrRow
), &w
, &h
);
8475 if ( GetColLabelTextOrientation() == wxVERTICAL
)
8479 dc
.GetMultiLineTextExtent( GetRowLabelValue(colOrRow
), &w
, &h
);
8481 extent
= column
? w
: h
;
8482 if ( extent
> extentMax
)
8487 // empty column - give default extent (notice that if extentMax is less
8488 // than default extent but != 0, it's OK)
8489 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8494 // leave some space around text
8502 // Ensure automatic width is not less than minimal width. See the
8503 // comment in SetColSize() for explanation of why this isn't done
8506 extentMax
= wxMax(extentMax
, GetColMinimalWidth(colOrRow
));
8508 SetColSize( colOrRow
, extentMax
);
8509 if ( !GetBatchCount() )
8511 if ( m_useNativeHeader
)
8513 GetGridColHeader()->UpdateColumn(colOrRow
);
8518 m_gridWin
->GetClientSize( &cw
, &ch
);
8519 wxRect
rect ( CellToRect( 0, colOrRow
) );
8521 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8522 rect
.width
= cw
- rect
.x
;
8523 rect
.height
= m_colLabelHeight
;
8524 GetColLabelWindow()->Refresh( true, &rect
);
8530 // Ensure automatic width is not less than minimal height. See the
8531 // comment in SetColSize() for explanation of why this isn't done
8534 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(colOrRow
));
8536 SetRowSize(colOrRow
, extentMax
);
8537 if ( !GetBatchCount() )
8540 m_gridWin
->GetClientSize( &cw
, &ch
);
8541 wxRect
rect( CellToRect( colOrRow
, 0 ) );
8543 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8544 rect
.width
= m_rowLabelWidth
;
8545 rect
.height
= ch
- rect
.y
;
8546 m_rowLabelWin
->Refresh( true, &rect
);
8553 SetColMinimalWidth(colOrRow
, extentMax
);
8555 SetRowMinimalHeight(colOrRow
, extentMax
);
8559 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
8561 // calculate size for the rows or columns?
8562 const bool calcRows
= direction
== wxGRID_ROW
;
8564 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
8565 : GetGridColLabelWindow());
8566 dc
.SetFont(GetLabelFont());
8568 // which dimension should we take into account for calculations?
8570 // for columns, the text can be only horizontal so it's easy but for rows
8571 // we also have to take into account the text orientation
8573 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
8575 wxArrayString lines
;
8576 wxCoord extentMax
= 0;
8578 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
8579 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
8583 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
8584 : GetColLabelValue(rowOrCol
);
8585 StringToLines(label
, lines
);
8588 GetTextBoxSize(dc
, lines
, &w
, &h
);
8590 const wxCoord extent
= useWidth
? w
: h
;
8591 if ( extent
> extentMax
)
8597 // empty column - give default extent (notice that if extentMax is less
8598 // than default extent but != 0, it's OK)
8599 extentMax
= calcRows
? GetDefaultRowLabelSize()
8600 : GetDefaultColLabelSize();
8603 // leave some space around text (taken from AutoSizeColOrRow)
8612 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8614 int width
= m_rowLabelWidth
;
8616 wxGridUpdateLocker locker
;
8618 locker
.Create(this);
8620 for ( int col
= 0; col
< m_numCols
; col
++ )
8623 AutoSizeColumn(col
, setAsMin
);
8625 width
+= GetColWidth(col
);
8631 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8633 int height
= m_colLabelHeight
;
8635 wxGridUpdateLocker locker
;
8637 locker
.Create(this);
8639 for ( int row
= 0; row
< m_numRows
; row
++ )
8642 AutoSizeRow(row
, setAsMin
);
8644 height
+= GetRowHeight(row
);
8650 void wxGrid::AutoSize()
8652 wxGridUpdateLocker
locker(this);
8654 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8655 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8657 // we know that we're not going to have scrollbars so disable them now to
8658 // avoid trouble in SetClientSize() which can otherwise set the correct
8659 // client size but also leave space for (not needed any more) scrollbars
8660 SetScrollbars(m_xScrollPixelsPerLine
, m_yScrollPixelsPerLine
,
8663 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8666 void wxGrid::AutoSizeRowLabelSize( int row
)
8668 // Hide the edit control, so it
8669 // won't interfere with drag-shrinking.
8670 if ( IsCellEditControlShown() )
8672 HideCellEditControl();
8673 SaveEditControlValue();
8676 // autosize row height depending on label text
8677 SetRowSize(row
, -1);
8682 void wxGrid::AutoSizeColLabelSize( int col
)
8684 // Hide the edit control, so it
8685 // won't interfere with drag-shrinking.
8686 if ( IsCellEditControlShown() )
8688 HideCellEditControl();
8689 SaveEditControlValue();
8692 // autosize column width depending on label text
8693 SetColSize(col
, -1);
8698 wxSize
wxGrid::DoGetBestSize() const
8700 wxGrid
* const self
= const_cast<wxGrid
*>(this);
8702 // we do the same as in AutoSize() here with the exception that we don't
8703 // change the column/row sizes, only calculate them
8704 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8705 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8707 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8708 + GetWindowBorderSize();
8716 #if WXWIN_COMPATIBILITY_2_8
8717 wxPen
& wxGrid::GetDividerPen() const
8721 #endif // WXWIN_COMPATIBILITY_2_8
8723 // ----------------------------------------------------------------------------
8724 // cell value accessor functions
8725 // ----------------------------------------------------------------------------
8727 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8731 m_table
->SetValue( row
, col
, s
);
8732 if ( !GetBatchCount() )
8735 wxRect
rect( CellToRect( row
, col
) );
8737 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8738 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8739 m_gridWin
->Refresh( false, &rect
);
8742 if ( m_currentCellCoords
.GetRow() == row
&&
8743 m_currentCellCoords
.GetCol() == col
&&
8744 IsCellEditControlShown())
8745 // Note: If we are using IsCellEditControlEnabled,
8746 // this interacts badly with calling SetCellValue from
8747 // an EVT_GRID_CELL_CHANGE handler.
8749 HideCellEditControl();
8750 ShowCellEditControl(); // will reread data from table
8755 // ----------------------------------------------------------------------------
8756 // block, row and column selection
8757 // ----------------------------------------------------------------------------
8759 void wxGrid::SelectRow( int row
, bool addToSelected
)
8764 if ( !addToSelected
)
8767 m_selection
->SelectRow(row
);
8770 void wxGrid::SelectCol( int col
, bool addToSelected
)
8775 if ( !addToSelected
)
8778 m_selection
->SelectCol(col
);
8781 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8787 if ( !addToSelected
)
8790 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8793 void wxGrid::SelectAll()
8795 if ( m_numRows
> 0 && m_numCols
> 0 )
8798 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8802 // ----------------------------------------------------------------------------
8803 // cell, row and col deselection
8804 // ----------------------------------------------------------------------------
8806 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8811 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8812 if ( mode
== oper
.GetSelectionMode() ||
8813 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8815 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8816 if ( m_selection
->IsInSelection(c
) )
8817 m_selection
->ToggleCellSelection(c
);
8819 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8821 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8822 for ( int i
= 0; i
< nOther
; i
++ )
8824 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8825 if ( m_selection
->IsInSelection(c
) )
8826 m_selection
->ToggleCellSelection(c
);
8829 //else: can only select orthogonal lines so no lines in this direction
8830 // could have been selected anyhow
8833 void wxGrid::DeselectRow(int row
)
8835 DeselectLine(row
, wxGridRowOperations());
8838 void wxGrid::DeselectCol(int col
)
8840 DeselectLine(col
, wxGridColumnOperations());
8843 void wxGrid::DeselectCell( int row
, int col
)
8845 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8846 m_selection
->ToggleCellSelection(row
, col
);
8849 bool wxGrid::IsSelection() const
8851 return ( m_selection
&& (m_selection
->IsSelection() ||
8852 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8853 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8856 bool wxGrid::IsInSelection( int row
, int col
) const
8858 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8859 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8860 col
>= m_selectedBlockTopLeft
.GetCol() &&
8861 row
<= m_selectedBlockBottomRight
.GetRow() &&
8862 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8865 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8869 wxGridCellCoordsArray a
;
8873 return m_selection
->m_cellSelection
;
8876 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8880 wxGridCellCoordsArray a
;
8884 return m_selection
->m_blockSelectionTopLeft
;
8887 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8891 wxGridCellCoordsArray a
;
8895 return m_selection
->m_blockSelectionBottomRight
;
8898 wxArrayInt
wxGrid::GetSelectedRows() const
8906 return m_selection
->m_rowSelection
;
8909 wxArrayInt
wxGrid::GetSelectedCols() const
8917 return m_selection
->m_colSelection
;
8920 void wxGrid::ClearSelection()
8922 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8923 m_selectedBlockBottomRight
);
8924 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8925 m_selectedBlockCorner
);
8927 m_selectedBlockTopLeft
=
8928 m_selectedBlockBottomRight
=
8929 m_selectedBlockCorner
= wxGridNoCellCoords
;
8931 if ( !r1
.IsEmpty() )
8932 RefreshRect(r1
, false);
8933 if ( !r2
.IsEmpty() )
8934 RefreshRect(r2
, false);
8937 m_selection
->ClearSelection();
8940 // This function returns the rectangle that encloses the given block
8941 // in device coords clipped to the client size of the grid window.
8943 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8944 const wxGridCellCoords
& bottomRight
) const
8947 wxRect tempCellRect
= CellToRect(topLeft
);
8948 if ( tempCellRect
!= wxGridNoCellRect
)
8950 resultRect
= tempCellRect
;
8954 resultRect
= wxRect(0, 0, 0, 0);
8957 tempCellRect
= CellToRect(bottomRight
);
8958 if ( tempCellRect
!= wxGridNoCellRect
)
8960 resultRect
+= tempCellRect
;
8964 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8965 return wxGridNoCellRect
;
8968 // Ensure that left/right and top/bottom pairs are in order.
8969 int left
= resultRect
.GetLeft();
8970 int top
= resultRect
.GetTop();
8971 int right
= resultRect
.GetRight();
8972 int bottom
= resultRect
.GetBottom();
8974 int leftCol
= topLeft
.GetCol();
8975 int topRow
= topLeft
.GetRow();
8976 int rightCol
= bottomRight
.GetCol();
8977 int bottomRow
= bottomRight
.GetRow();
9001 // The following loop is ONLY necessary to detect and handle merged cells.
9003 m_gridWin
->GetClientSize( &cw
, &ch
);
9005 // Get the origin coordinates: notice that they will be negative if the
9006 // grid is scrolled downwards/to the right.
9007 int gridOriginX
= 0;
9008 int gridOriginY
= 0;
9009 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
9011 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
9012 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
9014 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
9015 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
9017 // Bound our loop so that we only examine the portion of the selected block
9018 // that is shown on screen. Therefore, we compare the Top-Left block values
9019 // to the Top-Left screen values, and the Bottom-Right block values to the
9020 // Bottom-Right screen values, choosing appropriately.
9021 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
9022 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
9023 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
9024 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
9026 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
9028 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
9030 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
9031 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
9033 tempCellRect
= CellToRect( j
, i
);
9035 if (tempCellRect
.x
< left
)
9036 left
= tempCellRect
.x
;
9037 if (tempCellRect
.y
< top
)
9038 top
= tempCellRect
.y
;
9039 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
9040 right
= tempCellRect
.x
+ tempCellRect
.width
;
9041 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
9042 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
9046 i
= visibleRightCol
; // jump over inner cells.
9051 // Convert to scrolled coords
9052 CalcScrolledPosition( left
, top
, &left
, &top
);
9053 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9055 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9056 return wxRect(0,0,0,0);
9058 resultRect
.SetLeft( wxMax(0, left
) );
9059 resultRect
.SetTop( wxMax(0, top
) );
9060 resultRect
.SetRight( wxMin(cw
, right
) );
9061 resultRect
.SetBottom( wxMin(ch
, bottom
) );
9066 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
9067 const wxGridOperations
& oper
)
9070 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
9071 const int numLines
= oper
.GetNumberOfLines(this);
9072 for ( int i
= 0; i
< numLines
; i
++ )
9074 int size
= sizeInfo
.GetSize(i
);
9075 if ( size
!= sizeInfo
.m_sizeDefault
)
9076 oper
.SetLineSize(this, i
, size
);
9081 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
9083 DoSetSizes(sizeInfo
, wxGridColumnOperations());
9086 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
9088 DoSetSizes(sizeInfo
, wxGridRowOperations());
9091 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
9093 m_sizeDefault
= defSize
;
9094 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
9096 if ( allSizes
[i
] != defSize
)
9097 m_customSizes
[i
] = allSizes
[i
];
9101 int wxGridSizesInfo::GetSize(unsigned pos
) const
9103 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
9105 // if it's not found return the default
9106 if ( it
== m_customSizes
.end() )
9107 return m_sizeDefault
;
9109 // otherwise return 0 if it's hidden, currently there is no way to get
9110 // its size before it had been hidden
9111 if ( it
->second
< 0 )
9117 // ----------------------------------------------------------------------------
9119 // ----------------------------------------------------------------------------
9121 #if wxUSE_DRAG_AND_DROP
9123 // this allow setting drop target directly on wxGrid
9124 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
9126 GetGridWindow()->SetDropTarget(dropTarget
);
9129 #endif // wxUSE_DRAG_AND_DROP
9131 // ----------------------------------------------------------------------------
9132 // grid event classes
9133 // ----------------------------------------------------------------------------
9135 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9137 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9138 int row
, int col
, int x
, int y
, bool sel
,
9139 bool control
, bool shift
, bool alt
, bool meta
)
9140 : wxNotifyEvent( type
, id
),
9141 wxKeyboardState(control
, shift
, alt
, meta
)
9143 Init(row
, col
, x
, y
, sel
);
9145 SetEventObject(obj
);
9148 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9150 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9151 int rowOrCol
, int x
, int y
,
9152 bool control
, bool shift
, bool alt
, bool meta
)
9153 : wxNotifyEvent( type
, id
),
9154 wxKeyboardState(control
, shift
, alt
, meta
)
9156 Init(rowOrCol
, x
, y
);
9158 SetEventObject(obj
);
9162 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9164 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9165 const wxGridCellCoords
& topLeft
,
9166 const wxGridCellCoords
& bottomRight
,
9167 bool sel
, bool control
,
9168 bool shift
, bool alt
, bool meta
)
9169 : wxNotifyEvent( type
, id
),
9170 wxKeyboardState(control
, shift
, alt
, meta
)
9172 Init(topLeft
, bottomRight
, sel
);
9174 SetEventObject(obj
);
9178 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9180 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9181 wxObject
* obj
, int row
,
9182 int col
, wxControl
* ctrl
)
9183 : wxCommandEvent(type
, id
)
9185 SetEventObject(obj
);
9192 // ----------------------------------------------------------------------------
9193 // wxGridTypeRegistry
9194 // ----------------------------------------------------------------------------
9196 wxGridTypeRegistry::~wxGridTypeRegistry()
9198 size_t count
= m_typeinfo
.GetCount();
9199 for ( size_t i
= 0; i
< count
; i
++ )
9200 delete m_typeinfo
[i
];
9203 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
9204 wxGridCellRenderer
* renderer
,
9205 wxGridCellEditor
* editor
)
9207 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
9209 // is it already registered?
9210 int loc
= FindRegisteredDataType(typeName
);
9211 if ( loc
!= wxNOT_FOUND
)
9213 delete m_typeinfo
[loc
];
9214 m_typeinfo
[loc
] = info
;
9218 m_typeinfo
.Add(info
);
9222 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
9224 size_t count
= m_typeinfo
.GetCount();
9225 for ( size_t i
= 0; i
< count
; i
++ )
9227 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
9236 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
9238 int index
= FindRegisteredDataType(typeName
);
9239 if ( index
== wxNOT_FOUND
)
9241 // check whether this is one of the standard ones, in which case
9242 // register it "on the fly"
9244 if ( typeName
== wxGRID_VALUE_STRING
)
9246 RegisterDataType(wxGRID_VALUE_STRING
,
9247 new wxGridCellStringRenderer
,
9248 new wxGridCellTextEditor
);
9251 #endif // wxUSE_TEXTCTRL
9253 if ( typeName
== wxGRID_VALUE_BOOL
)
9255 RegisterDataType(wxGRID_VALUE_BOOL
,
9256 new wxGridCellBoolRenderer
,
9257 new wxGridCellBoolEditor
);
9260 #endif // wxUSE_CHECKBOX
9262 if ( typeName
== wxGRID_VALUE_NUMBER
)
9264 RegisterDataType(wxGRID_VALUE_NUMBER
,
9265 new wxGridCellNumberRenderer
,
9266 new wxGridCellNumberEditor
);
9268 else if ( typeName
== wxGRID_VALUE_FLOAT
)
9270 RegisterDataType(wxGRID_VALUE_FLOAT
,
9271 new wxGridCellFloatRenderer
,
9272 new wxGridCellFloatEditor
);
9275 #endif // wxUSE_TEXTCTRL
9277 if ( typeName
== wxGRID_VALUE_CHOICE
)
9279 RegisterDataType(wxGRID_VALUE_CHOICE
,
9280 new wxGridCellStringRenderer
,
9281 new wxGridCellChoiceEditor
);
9284 #endif // wxUSE_COMBOBOX
9289 // we get here only if just added the entry for this type, so return
9291 index
= m_typeinfo
.GetCount() - 1;
9297 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
9299 int index
= FindDataType(typeName
);
9300 if ( index
== wxNOT_FOUND
)
9302 // the first part of the typename is the "real" type, anything after ':'
9303 // are the parameters for the renderer
9304 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
9305 if ( index
== wxNOT_FOUND
)
9310 wxGridCellRenderer
*renderer
= GetRenderer(index
);
9311 wxGridCellRenderer
*rendererOld
= renderer
;
9312 renderer
= renderer
->Clone();
9313 rendererOld
->DecRef();
9315 wxGridCellEditor
*editor
= GetEditor(index
);
9316 wxGridCellEditor
*editorOld
= editor
;
9317 editor
= editor
->Clone();
9318 editorOld
->DecRef();
9320 // do it even if there are no parameters to reset them to defaults
9321 wxString params
= typeName
.AfterFirst(wxT(':'));
9322 renderer
->SetParameters(params
);
9323 editor
->SetParameters(params
);
9325 // register the new typename
9326 RegisterDataType(typeName
, renderer
, editor
);
9328 // we just registered it, it's the last one
9329 index
= m_typeinfo
.GetCount() - 1;
9335 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
9337 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
9344 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
9346 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
9353 #endif // wxUSE_GRID