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(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_MOVE
, wxGridEvent
);
150 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
151 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
152 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
154 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
155 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
166 // ensure that first is less or equal to second, swapping the values if
168 void EnsureFirstLessThanSecond(int& first
, int& second
)
170 if ( first
> second
)
171 wxSwap(first
, second
);
174 } // anonymous namespace
176 // ============================================================================
178 // ============================================================================
180 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
182 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
183 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
184 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
185 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
188 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
189 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
190 EVT_HEADER_DCLICK(wxID_ANY
, wxGridHeaderCtrl::OnDoubleClick
)
191 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnRightClick
)
193 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
194 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
195 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
197 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
198 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
201 wxGridOperations
& wxGridRowOperations::Dual() const
203 static wxGridColumnOperations s_colOper
;
208 wxGridOperations
& wxGridColumnOperations::Dual() const
210 static wxGridRowOperations s_rowOper
;
215 // ----------------------------------------------------------------------------
216 // wxGridCellWorker is an (almost) empty common base class for
217 // wxGridCellRenderer and wxGridCellEditor managing ref counting
218 // ----------------------------------------------------------------------------
220 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
225 wxGridCellWorker::~wxGridCellWorker()
229 // ----------------------------------------------------------------------------
230 // wxGridHeaderLabelsRenderer and related classes
231 // ----------------------------------------------------------------------------
233 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid
& grid
,
235 const wxString
& value
,
239 int textOrientation
) const
241 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
242 dc
.SetTextForeground(grid
.GetLabelTextColour());
243 dc
.SetFont(grid
.GetLabelFont());
244 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
248 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
252 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
253 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
254 rect
.GetRight(), rect
.GetBottom());
255 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
256 rect
.GetLeft(), rect
.GetBottom());
257 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
258 rect
.GetRight() + 1, rect
.GetBottom());
260 dc
.SetPen(*wxWHITE_PEN
);
261 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
262 rect
.GetLeft() + 1, rect
.GetBottom());
263 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
264 rect
.GetRight(), rect
.GetTop());
269 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
273 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
274 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
275 rect
.GetRight(), rect
.GetBottom());
276 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
277 rect
.GetRight(), rect
.GetTop());
278 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
279 rect
.GetRight() + 1, rect
.GetBottom());
281 dc
.SetPen(*wxWHITE_PEN
);
282 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
283 rect
.GetLeft(), rect
.GetBottom());
284 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
285 rect
.GetRight(), rect
.GetTop() + 1);
290 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
294 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
295 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
296 rect
.GetRight() - 1, rect
.GetTop());
297 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
298 rect
.GetLeft(), rect
.GetBottom() - 1);
299 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
300 rect
.GetRight(), rect
.GetTop());
301 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
302 rect
.GetLeft(), rect
.GetBottom());
304 dc
.SetPen(*wxWHITE_PEN
);
305 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
306 rect
.GetRight() - 1, rect
.GetTop() + 1);
307 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
308 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
319 m_isReadOnly
= Unset
;
324 m_attrkind
= wxGridCellAttr::Cell
;
326 m_sizeRows
= m_sizeCols
= 1;
327 m_overflow
= UnsetOverflow
;
329 SetDefAttr(attrDefault
);
332 wxGridCellAttr
*wxGridCellAttr::Clone() const
334 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
336 if ( HasTextColour() )
337 attr
->SetTextColour(GetTextColour());
338 if ( HasBackgroundColour() )
339 attr
->SetBackgroundColour(GetBackgroundColour());
341 attr
->SetFont(GetFont());
342 if ( HasAlignment() )
343 attr
->SetAlignment(m_hAlign
, m_vAlign
);
345 attr
->SetSize( m_sizeRows
, m_sizeCols
);
349 attr
->SetRenderer(m_renderer
);
350 m_renderer
->IncRef();
354 attr
->SetEditor(m_editor
);
361 attr
->SetOverflow( m_overflow
== Overflow
);
362 attr
->SetKind( m_attrkind
);
367 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
369 if ( !HasTextColour() && mergefrom
->HasTextColour() )
370 SetTextColour(mergefrom
->GetTextColour());
371 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
372 SetBackgroundColour(mergefrom
->GetBackgroundColour());
373 if ( !HasFont() && mergefrom
->HasFont() )
374 SetFont(mergefrom
->GetFont());
375 if ( !HasAlignment() && mergefrom
->HasAlignment() )
378 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
379 SetAlignment(hAlign
, vAlign
);
381 if ( !HasSize() && mergefrom
->HasSize() )
382 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
384 // Directly access member functions as GetRender/Editor don't just return
385 // m_renderer/m_editor
387 // Maybe add support for merge of Render and Editor?
388 if (!HasRenderer() && mergefrom
->HasRenderer() )
390 m_renderer
= mergefrom
->m_renderer
;
391 m_renderer
->IncRef();
393 if ( !HasEditor() && mergefrom
->HasEditor() )
395 m_editor
= mergefrom
->m_editor
;
398 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
399 SetReadOnly(mergefrom
->IsReadOnly());
401 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
402 SetOverflow(mergefrom
->GetOverflow());
404 SetDefAttr(mergefrom
->m_defGridAttr
);
407 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
409 // The size of a cell is normally 1,1
411 // If this cell is larger (2,2) then this is the top left cell
412 // the other cells that will be covered (lower right cells) must be
413 // set to negative or zero values such that
414 // row + num_rows of the covered cell points to the larger cell (this cell)
415 // same goes for the col + num_cols.
417 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
419 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
420 !((num_rows
<= 0) && (num_cols
> 0)) ||
421 !((num_rows
== 0) && (num_cols
== 0))),
422 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
424 m_sizeRows
= num_rows
;
425 m_sizeCols
= num_cols
;
428 const wxColour
& wxGridCellAttr::GetTextColour() const
434 else if (m_defGridAttr
&& m_defGridAttr
!= this)
436 return m_defGridAttr
->GetTextColour();
440 wxFAIL_MSG(wxT("Missing default cell attribute"));
445 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
447 if (HasBackgroundColour())
451 else if (m_defGridAttr
&& m_defGridAttr
!= this)
453 return m_defGridAttr
->GetBackgroundColour();
457 wxFAIL_MSG(wxT("Missing default cell attribute"));
462 const wxFont
& wxGridCellAttr::GetFont() const
468 else if (m_defGridAttr
&& m_defGridAttr
!= this)
470 return m_defGridAttr
->GetFont();
474 wxFAIL_MSG(wxT("Missing default cell attribute"));
479 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
488 else if (m_defGridAttr
&& m_defGridAttr
!= this)
490 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
494 wxFAIL_MSG(wxT("Missing default cell attribute"));
498 void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
500 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
503 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
507 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
510 *num_rows
= m_sizeRows
;
512 *num_cols
= m_sizeCols
;
515 // GetRenderer and GetEditor use a slightly different decision path about
516 // which attribute to use. If a non-default attr object has one then it is
517 // used, otherwise the default editor or renderer is fetched from the grid and
518 // used. It should be the default for the data type of the cell. If it is
519 // NULL (because the table has a type that the grid does not have in its
520 // registry), then the grid's default editor or renderer is used.
522 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
524 wxGridCellRenderer
*renderer
= NULL
;
526 if ( m_renderer
&& this != m_defGridAttr
)
528 // use the cells renderer if it has one
529 renderer
= m_renderer
;
532 else // no non-default cell renderer
534 // get default renderer for the data type
537 // GetDefaultRendererForCell() will do IncRef() for us
538 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
541 if ( renderer
== NULL
)
543 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
545 // if we still don't have one then use the grid default
546 // (no need for IncRef() here neither)
547 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
549 else // default grid attr
551 // use m_renderer which we had decided not to use initially
552 renderer
= m_renderer
;
559 // we're supposed to always find something
560 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
565 // same as above, except for s/renderer/editor/g
566 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
568 wxGridCellEditor
*editor
= NULL
;
570 if ( m_editor
&& this != m_defGridAttr
)
572 // use the cells editor if it has one
576 else // no non default cell editor
578 // get default editor for the data type
581 // GetDefaultEditorForCell() will do IncRef() for us
582 editor
= grid
->GetDefaultEditorForCell(row
, col
);
585 if ( editor
== NULL
)
587 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
589 // if we still don't have one then use the grid default
590 // (no need for IncRef() here neither)
591 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
593 else // default grid attr
595 // use m_editor which we had decided not to use initially
603 // we're supposed to always find something
604 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
609 // ----------------------------------------------------------------------------
610 // wxGridCellAttrData
611 // ----------------------------------------------------------------------------
613 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
615 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
616 // touch attribute's reference counting explicitly, since this
617 // is managed by class wxGridCellWithAttr
618 int n
= FindIndex(row
, col
);
619 if ( n
== wxNOT_FOUND
)
624 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
626 //else: nothing to do
628 else // we already have an attribute for this cell
632 // change the attribute
633 m_attrs
[(size_t)n
].ChangeAttr(attr
);
637 // remove this attribute
638 m_attrs
.RemoveAt((size_t)n
);
643 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
645 wxGridCellAttr
*attr
= NULL
;
647 int n
= FindIndex(row
, col
);
648 if ( n
!= wxNOT_FOUND
)
650 attr
= m_attrs
[(size_t)n
].attr
;
657 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
659 size_t count
= m_attrs
.GetCount();
660 for ( size_t n
= 0; n
< count
; n
++ )
662 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
663 wxCoord row
= coords
.GetRow();
664 if ((size_t)row
>= pos
)
668 // If rows inserted, include row counter where necessary
669 coords
.SetRow(row
+ numRows
);
671 else if (numRows
< 0)
673 // If rows deleted ...
674 if ((size_t)row
>= pos
- numRows
)
676 // ...either decrement row counter (if row still exists)...
677 coords
.SetRow(row
+ numRows
);
681 // ...or remove the attribute
691 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
693 size_t count
= m_attrs
.GetCount();
694 for ( size_t n
= 0; n
< count
; n
++ )
696 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
697 wxCoord col
= coords
.GetCol();
698 if ( (size_t)col
>= pos
)
702 // If rows inserted, include row counter where necessary
703 coords
.SetCol(col
+ numCols
);
705 else if (numCols
< 0)
707 // If rows deleted ...
708 if ((size_t)col
>= pos
- numCols
)
710 // ...either decrement row counter (if row still exists)...
711 coords
.SetCol(col
+ numCols
);
715 // ...or remove the attribute
725 int wxGridCellAttrData::FindIndex(int row
, int col
) const
727 size_t count
= m_attrs
.GetCount();
728 for ( size_t n
= 0; n
< count
; n
++ )
730 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
731 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
740 // ----------------------------------------------------------------------------
741 // wxGridRowOrColAttrData
742 // ----------------------------------------------------------------------------
744 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
746 size_t count
= m_attrs
.GetCount();
747 for ( size_t n
= 0; n
< count
; n
++ )
749 m_attrs
[n
]->DecRef();
753 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
755 wxGridCellAttr
*attr
= NULL
;
757 int n
= m_rowsOrCols
.Index(rowOrCol
);
758 if ( n
!= wxNOT_FOUND
)
760 attr
= m_attrs
[(size_t)n
];
767 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
769 int i
= m_rowsOrCols
.Index(rowOrCol
);
770 if ( i
== wxNOT_FOUND
)
774 // store the new attribute, taking its ownership
775 m_rowsOrCols
.Add(rowOrCol
);
780 else // we have an attribute for this row or column
782 size_t n
= (size_t)i
;
784 // notice that this code works correctly even when the old attribute is
785 // the same as the new one: as we own of it, we must call DecRef() on
786 // it in any case and this won't result in destruction of the new
787 // attribute if it's the same as old one because it must have ref count
788 // of at least 2 to be passed to us while we keep a reference to it too
789 m_attrs
[n
]->DecRef();
793 // replace the attribute with the new one
796 else // remove the attribute
798 m_rowsOrCols
.RemoveAt(n
);
804 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
806 size_t count
= m_attrs
.GetCount();
807 for ( size_t n
= 0; n
< count
; n
++ )
809 int & rowOrCol
= m_rowsOrCols
[n
];
810 if ( (size_t)rowOrCol
>= pos
)
812 if ( numRowsOrCols
> 0 )
814 // If rows inserted, include row counter where necessary
815 rowOrCol
+= numRowsOrCols
;
817 else if ( numRowsOrCols
< 0)
819 // If rows deleted, either decrement row counter (if row still exists)
820 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
821 rowOrCol
+= numRowsOrCols
;
824 m_rowsOrCols
.RemoveAt(n
);
825 m_attrs
[n
]->DecRef();
835 // ----------------------------------------------------------------------------
836 // wxGridCellAttrProvider
837 // ----------------------------------------------------------------------------
839 wxGridCellAttrProvider::wxGridCellAttrProvider()
844 wxGridCellAttrProvider::~wxGridCellAttrProvider()
849 void wxGridCellAttrProvider::InitData()
851 m_data
= new wxGridCellAttrProviderData
;
854 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
855 wxGridCellAttr::wxAttrKind kind
) const
857 wxGridCellAttr
*attr
= NULL
;
862 case (wxGridCellAttr::Any
):
863 // Get cached merge attributes.
864 // Currently not used as no cache implemented as not mutable
865 // attr = m_data->m_mergeAttr.GetAttr(row, col);
868 // Basically implement old version.
869 // Also check merge cache, so we don't have to re-merge every time..
870 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
871 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
872 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
874 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
876 // Two or more are non NULL
877 attr
= new wxGridCellAttr
;
878 attr
->SetKind(wxGridCellAttr::Merged
);
880 // Order is important..
883 attr
->MergeWith(attrcell
);
888 attr
->MergeWith(attrcol
);
893 attr
->MergeWith(attrrow
);
897 // store merge attr if cache implemented
899 //m_data->m_mergeAttr.SetAttr(attr, row, col);
903 // one or none is non null return it or null.
922 case (wxGridCellAttr::Cell
):
923 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
926 case (wxGridCellAttr::Col
):
927 attr
= m_data
->m_colAttrs
.GetAttr(col
);
930 case (wxGridCellAttr::Row
):
931 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
936 // (wxGridCellAttr::Default):
937 // (wxGridCellAttr::Merged):
945 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
951 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
954 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
959 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
962 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
967 m_data
->m_colAttrs
.SetAttr(attr
, col
);
970 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
974 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
976 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
980 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
984 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
986 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
990 const wxGridColumnHeaderRenderer
&
991 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
993 return gs_defaultHeaderRenderers
.colRenderer
;
996 const wxGridRowHeaderRenderer
&
997 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
999 return gs_defaultHeaderRenderers
.rowRenderer
;
1002 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
1004 return gs_defaultHeaderRenderers
.cornerRenderer
;
1007 // ----------------------------------------------------------------------------
1009 // ----------------------------------------------------------------------------
1011 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1013 wxGridTableBase::wxGridTableBase()
1016 m_attrProvider
= NULL
;
1019 wxGridTableBase::~wxGridTableBase()
1021 delete m_attrProvider
;
1024 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1026 delete m_attrProvider
;
1027 m_attrProvider
= attrProvider
;
1030 bool wxGridTableBase::CanHaveAttributes()
1032 if ( ! GetAttrProvider() )
1034 // use the default attr provider by default
1035 SetAttrProvider(new wxGridCellAttrProvider
);
1041 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1043 if ( m_attrProvider
)
1044 return m_attrProvider
->GetAttr(row
, col
, kind
);
1049 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1051 if ( m_attrProvider
)
1054 attr
->SetKind(wxGridCellAttr::Cell
);
1055 m_attrProvider
->SetAttr(attr
, row
, col
);
1059 // as we take ownership of the pointer and don't store it, we must
1065 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1067 if ( m_attrProvider
)
1069 attr
->SetKind(wxGridCellAttr::Row
);
1070 m_attrProvider
->SetRowAttr(attr
, row
);
1074 // as we take ownership of the pointer and don't store it, we must
1080 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1082 if ( m_attrProvider
)
1084 attr
->SetKind(wxGridCellAttr::Col
);
1085 m_attrProvider
->SetColAttr(attr
, col
);
1089 // as we take ownership of the pointer and don't store it, we must
1095 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1096 size_t WXUNUSED(numRows
) )
1098 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1103 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1105 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1110 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1111 size_t WXUNUSED(numRows
) )
1113 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1118 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1119 size_t WXUNUSED(numCols
) )
1121 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1126 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1128 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1133 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1134 size_t WXUNUSED(numCols
) )
1136 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1141 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1145 // RD: Starting the rows at zero confuses users,
1146 // no matter how much it makes sense to us geeks.
1152 wxString
wxGridTableBase::GetColLabelValue( int col
)
1154 // default col labels are:
1155 // cols 0 to 25 : A-Z
1156 // cols 26 to 675 : AA-ZZ
1161 for ( n
= 1; ; n
++ )
1163 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1169 // reverse the string...
1171 for ( i
= 0; i
< n
; i
++ )
1179 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1181 return wxGRID_VALUE_STRING
;
1184 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1185 const wxString
& typeName
)
1187 return typeName
== wxGRID_VALUE_STRING
;
1190 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1192 return CanGetValueAs(row
, col
, typeName
);
1195 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1200 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1205 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1210 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1211 long WXUNUSED(value
) )
1215 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1216 double WXUNUSED(value
) )
1220 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1221 bool WXUNUSED(value
) )
1225 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1226 const wxString
& WXUNUSED(typeName
) )
1231 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1232 const wxString
& WXUNUSED(typeName
),
1233 void* WXUNUSED(value
) )
1237 //////////////////////////////////////////////////////////////////////
1239 // Message class for the grid table to send requests and notifications
1243 wxGridTableMessage::wxGridTableMessage()
1251 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1252 int commandInt1
, int commandInt2
)
1256 m_comInt1
= commandInt1
;
1257 m_comInt2
= commandInt2
;
1260 //////////////////////////////////////////////////////////////////////
1262 // A basic grid table for string data. An object of this class will
1263 // created by wxGrid if you don't specify an alternative table class.
1266 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1268 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1270 wxGridStringTable::wxGridStringTable()
1276 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1279 m_numCols
= numCols
;
1281 m_data
.Alloc( numRows
);
1284 sa
.Alloc( numCols
);
1285 sa
.Add( wxEmptyString
, numCols
);
1287 m_data
.Add( sa
, numRows
);
1290 wxString
wxGridStringTable::GetValue( int row
, int col
)
1292 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1293 (col
>= 0 && col
< GetNumberCols()),
1295 wxT("invalid row or column index in wxGridStringTable") );
1297 return m_data
[row
][col
];
1300 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1302 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1303 (col
>= 0 && col
< GetNumberCols()),
1304 wxT("invalid row or column index in wxGridStringTable") );
1306 m_data
[row
][col
] = value
;
1309 void wxGridStringTable::Clear()
1312 int numRows
, numCols
;
1314 numRows
= m_data
.GetCount();
1317 numCols
= m_data
[0].GetCount();
1319 for ( row
= 0; row
< numRows
; row
++ )
1321 for ( col
= 0; col
< numCols
; col
++ )
1323 m_data
[row
][col
] = wxEmptyString
;
1329 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1331 size_t curNumRows
= m_data
.GetCount();
1332 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1333 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1335 if ( pos
>= curNumRows
)
1337 return AppendRows( numRows
);
1341 sa
.Alloc( curNumCols
);
1342 sa
.Add( wxEmptyString
, curNumCols
);
1343 m_data
.Insert( sa
, pos
, numRows
);
1347 wxGridTableMessage
msg( this,
1348 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1352 GetView()->ProcessTableMessage( msg
);
1358 bool wxGridStringTable::AppendRows( size_t numRows
)
1360 size_t curNumRows
= m_data
.GetCount();
1361 size_t curNumCols
= ( curNumRows
> 0
1362 ? m_data
[0].GetCount()
1363 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1366 if ( curNumCols
> 0 )
1368 sa
.Alloc( curNumCols
);
1369 sa
.Add( wxEmptyString
, curNumCols
);
1372 m_data
.Add( sa
, numRows
);
1376 wxGridTableMessage
msg( this,
1377 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1380 GetView()->ProcessTableMessage( msg
);
1386 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1388 size_t curNumRows
= m_data
.GetCount();
1390 if ( pos
>= curNumRows
)
1392 wxFAIL_MSG( wxString::Format
1394 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1396 (unsigned long)numRows
,
1397 (unsigned long)curNumRows
1403 if ( numRows
> curNumRows
- pos
)
1405 numRows
= curNumRows
- pos
;
1408 if ( numRows
>= curNumRows
)
1414 m_data
.RemoveAt( pos
, numRows
);
1419 wxGridTableMessage
msg( this,
1420 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1424 GetView()->ProcessTableMessage( msg
);
1430 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1434 size_t curNumRows
= m_data
.GetCount();
1435 size_t curNumCols
= ( curNumRows
> 0
1436 ? m_data
[0].GetCount()
1437 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1439 if ( pos
>= curNumCols
)
1441 return AppendCols( numCols
);
1444 if ( !m_colLabels
.IsEmpty() )
1446 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1449 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1450 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1453 for ( row
= 0; row
< curNumRows
; row
++ )
1455 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1457 m_data
[row
].Insert( wxEmptyString
, col
);
1461 m_numCols
+= numCols
;
1465 wxGridTableMessage
msg( this,
1466 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1470 GetView()->ProcessTableMessage( msg
);
1476 bool wxGridStringTable::AppendCols( size_t numCols
)
1480 size_t curNumRows
= m_data
.GetCount();
1482 for ( row
= 0; row
< curNumRows
; row
++ )
1484 m_data
[row
].Add( wxEmptyString
, numCols
);
1487 m_numCols
+= numCols
;
1491 wxGridTableMessage
msg( this,
1492 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1495 GetView()->ProcessTableMessage( msg
);
1501 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1505 size_t curNumRows
= m_data
.GetCount();
1506 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1507 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1509 if ( pos
>= curNumCols
)
1511 wxFAIL_MSG( wxString::Format
1513 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1515 (unsigned long)numCols
,
1516 (unsigned long)curNumCols
1523 colID
= GetView()->GetColAt( pos
);
1527 if ( numCols
> curNumCols
- colID
)
1529 numCols
= curNumCols
- colID
;
1532 if ( !m_colLabels
.IsEmpty() )
1534 // m_colLabels stores just as many elements as it needs, e.g. if only
1535 // the label of the first column had been set it would have only one
1536 // element and not numCols, so account for it
1537 int numRemaining
= m_colLabels
.size() - colID
;
1538 m_colLabels
.RemoveAt( colID
, wxMin(numCols
, numRemaining
) );
1541 if ( numCols
>= curNumCols
)
1543 for ( row
= 0; row
< curNumRows
; row
++ )
1545 m_data
[row
].Clear();
1550 else // something will be left
1552 for ( row
= 0; row
< curNumRows
; row
++ )
1554 m_data
[row
].RemoveAt( colID
, numCols
);
1557 m_numCols
-= numCols
;
1562 wxGridTableMessage
msg( this,
1563 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1567 GetView()->ProcessTableMessage( msg
);
1573 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1575 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1577 // using default label
1579 return wxGridTableBase::GetRowLabelValue( row
);
1583 return m_rowLabels
[row
];
1587 wxString
wxGridStringTable::GetColLabelValue( int col
)
1589 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1591 // using default label
1593 return wxGridTableBase::GetColLabelValue( col
);
1597 return m_colLabels
[col
];
1601 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1603 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1605 int n
= m_rowLabels
.GetCount();
1608 for ( i
= n
; i
<= row
; i
++ )
1610 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1614 m_rowLabels
[row
] = value
;
1617 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1619 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1621 int n
= m_colLabels
.GetCount();
1624 for ( i
= n
; i
<= col
; i
++ )
1626 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1630 m_colLabels
[col
] = value
;
1634 //////////////////////////////////////////////////////////////////////
1635 //////////////////////////////////////////////////////////////////////
1637 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1638 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1641 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1643 m_owner
->CancelMouseCapture();
1646 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1647 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1648 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1649 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1652 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1656 // NO - don't do this because it will set both the x and y origin
1657 // coords to match the parent scrolled window and we just want to
1658 // set the y coord - MB
1660 // m_owner->PrepareDC( dc );
1663 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1664 wxPoint pt
= dc
.GetDeviceOrigin();
1665 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1667 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1668 m_owner
->DrawRowLabels( dc
, rows
);
1671 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1673 m_owner
->ProcessRowLabelMouseEvent( event
);
1676 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1678 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1682 //////////////////////////////////////////////////////////////////////
1684 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1685 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1686 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1687 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1690 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1694 // NO - don't do this because it will set both the x and y origin
1695 // coords to match the parent scrolled window and we just want to
1696 // set the x coord - MB
1698 // m_owner->PrepareDC( dc );
1701 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1702 wxPoint pt
= dc
.GetDeviceOrigin();
1703 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1704 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1706 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1708 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1709 m_owner
->DrawColLabels( dc
, cols
);
1712 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1714 m_owner
->ProcessColLabelMouseEvent( event
);
1717 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1719 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1723 //////////////////////////////////////////////////////////////////////
1725 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1726 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1727 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1728 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1731 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1735 m_owner
->DrawCornerLabel(dc
);
1738 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1740 m_owner
->ProcessCornerLabelMouseEvent( event
);
1743 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1745 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1749 //////////////////////////////////////////////////////////////////////
1751 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1752 EVT_PAINT( wxGridWindow::OnPaint
)
1753 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1754 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1755 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1756 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1757 EVT_CHAR( wxGridWindow::OnChar
)
1758 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1759 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1760 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1763 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1765 wxPaintDC
dc( this );
1766 m_owner
->PrepareDC( dc
);
1767 wxRegion reg
= GetUpdateRegion();
1768 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1769 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1771 m_owner
->DrawGridSpace( dc
);
1773 m_owner
->DrawAllGridLines( dc
, reg
);
1775 m_owner
->DrawHighlight( dc
, dirtyCells
);
1778 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1780 wxWindow::ScrollWindow( dx
, dy
, rect
);
1781 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1782 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1785 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1787 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1790 m_owner
->ProcessGridCellMouseEvent( event
);
1793 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1795 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1799 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1800 // cursor must be in the cell edit control to get key events
1802 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1804 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1808 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1810 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1814 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1816 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1820 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1824 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1826 // and if we have any selection, it has to be repainted, because it
1827 // uses different colour when the grid is not focused:
1828 if ( m_owner
->IsSelection() )
1834 // NB: Note that this code is in "else" branch only because the other
1835 // branch refreshes everything and so there's no point in calling
1836 // Refresh() again, *not* because it should only be done if
1837 // !IsSelection(). If the above code is ever optimized to refresh
1838 // only selected area, this needs to be moved out of the "else"
1839 // branch so that it's always executed.
1841 // current cell cursor {dis,re}appears on focus change:
1842 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1843 m_owner
->GetGridCursorCol());
1844 const wxRect cursor
=
1845 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1846 Refresh(true, &cursor
);
1849 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1853 #define internalXToCol(x) XToCol(x, true)
1854 #define internalYToRow(y) YToRow(y, true)
1856 /////////////////////////////////////////////////////////////////////
1858 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1859 EVT_PAINT( wxGrid::OnPaint
)
1860 EVT_SIZE( wxGrid::OnSize
)
1861 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1862 EVT_KEY_UP( wxGrid::OnKeyUp
)
1863 EVT_CHAR ( wxGrid::OnChar
)
1864 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1867 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1868 const wxPoint
& pos
, const wxSize
& size
,
1869 long style
, const wxString
& name
)
1871 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1872 style
| wxWANTS_CHARS
, name
))
1875 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1876 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1879 SetInitialSize(size
);
1888 m_winCapture
->ReleaseMouse();
1890 // Ensure that the editor control is destroyed before the grid is,
1891 // otherwise we crash later when the editor tries to do something with the
1892 // half destroyed grid
1893 HideCellEditControl();
1895 // Must do this or ~wxScrollHelper will pop the wrong event handler
1896 SetTargetWindow(this);
1898 wxSafeDecRef(m_defaultCellAttr
);
1900 #ifdef DEBUG_ATTR_CACHE
1901 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1902 wxPrintf(wxT("wxGrid attribute cache statistics: "
1903 "total: %u, hits: %u (%u%%)\n"),
1904 total
, gs_nAttrCacheHits
,
1905 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1908 // if we own the table, just delete it, otherwise at least don't leave it
1909 // with dangling view pointer
1912 else if ( m_table
&& m_table
->GetView() == this )
1913 m_table
->SetView(NULL
);
1915 delete m_typeRegistry
;
1918 delete m_setFixedRows
;
1919 delete m_setFixedCols
;
1923 // ----- internal init and update functions
1926 // NOTE: If using the default visual attributes works everywhere then this can
1927 // be removed as well as the #else cases below.
1928 #define _USE_VISATTR 0
1930 void wxGrid::Create()
1932 // create the type registry
1933 m_typeRegistry
= new wxGridTypeRegistry
;
1935 m_cellEditCtrlEnabled
= false;
1937 m_defaultCellAttr
= new wxGridCellAttr();
1939 // Set default cell attributes
1940 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1941 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1942 m_defaultCellAttr
->SetFont(GetFont());
1943 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1944 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1945 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1948 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1949 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1951 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1952 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1955 m_defaultCellAttr
->SetTextColour(
1956 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1957 m_defaultCellAttr
->SetBackgroundColour(
1958 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1963 m_currentCellCoords
= wxGridNoCellCoords
;
1965 // subwindow components that make up the wxGrid
1966 m_rowLabelWin
= new wxGridRowLabelWindow(this);
1967 CreateColumnWindow();
1968 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
1969 m_gridWin
= new wxGridWindow( this );
1971 SetTargetWindow( m_gridWin
);
1974 wxColour gfg
= gva
.colFg
;
1975 wxColour gbg
= gva
.colBg
;
1976 wxColour lfg
= lva
.colFg
;
1977 wxColour lbg
= lva
.colBg
;
1979 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1980 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
1981 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1982 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1985 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
1986 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
1987 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
1988 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
1989 m_colWindow
->SetOwnForegroundColour(lfg
);
1990 m_colWindow
->SetOwnBackgroundColour(lbg
);
1992 m_gridWin
->SetOwnForegroundColour(gfg
);
1993 m_gridWin
->SetOwnBackgroundColour(gbg
);
1995 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1996 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
1998 // now that we have the grid window, use its font to compute the default
2000 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2001 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2002 m_defaultRowHeight
+= 8;
2004 m_defaultRowHeight
+= 4;
2009 void wxGrid::CreateColumnWindow()
2011 if ( m_useNativeHeader
)
2013 m_colWindow
= new wxGridHeaderCtrl(this);
2014 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2016 else // draw labels ourselves
2018 m_colWindow
= new wxGridColLabelWindow(this);
2019 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2023 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2024 wxGridSelectionModes selmode
)
2026 wxCHECK_MSG( !m_created
,
2028 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2030 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2033 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2035 wxCHECK_RET( m_created
,
2036 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2038 m_selection
->SetSelectionMode( selmode
);
2041 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2043 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2044 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2046 return m_selection
->GetSelectionMode();
2050 wxGrid::SetTable(wxGridTableBase
*table
,
2052 wxGrid::wxGridSelectionModes selmode
)
2054 bool checkSelection
= false;
2057 // stop all processing
2062 m_table
->SetView(0);
2068 wxDELETE(m_selection
);
2073 checkSelection
= true;
2075 // kill row and column size arrays
2076 m_colWidths
.Empty();
2077 m_colRights
.Empty();
2078 m_rowHeights
.Empty();
2079 m_rowBottoms
.Empty();
2084 m_numRows
= table
->GetNumberRows();
2085 m_numCols
= table
->GetNumberCols();
2087 if ( m_useNativeHeader
)
2088 GetGridColHeader()->SetColumnCount(m_numCols
);
2091 m_table
->SetView( this );
2092 m_ownTable
= takeOwnership
;
2093 m_selection
= new wxGridSelection( this, selmode
);
2096 // If the newly set table is smaller than the
2097 // original one current cell and selection regions
2098 // might be invalid,
2099 m_selectedBlockCorner
= wxGridNoCellCoords
;
2100 m_currentCellCoords
=
2101 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2102 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2103 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2104 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2106 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2107 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2110 m_selectedBlockBottomRight
=
2111 wxGridCellCoords(wxMin(m_numRows
,
2112 m_selectedBlockBottomRight
.GetRow()),
2114 m_selectedBlockBottomRight
.GetCol()));
2128 m_cornerLabelWin
= NULL
;
2129 m_rowLabelWin
= NULL
;
2137 m_defaultCellAttr
= NULL
;
2138 m_typeRegistry
= NULL
;
2139 m_winCapture
= NULL
;
2141 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2142 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2145 m_setFixedCols
= NULL
;
2148 m_attrCache
.row
= -1;
2149 m_attrCache
.col
= -1;
2150 m_attrCache
.attr
= NULL
;
2152 m_labelFont
= GetFont();
2153 m_labelFont
.SetWeight( wxBOLD
);
2155 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2156 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2158 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2159 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2160 m_colLabelTextOrientation
= wxHORIZONTAL
;
2162 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2163 m_defaultRowHeight
= 0; // this will be initialized after creation
2165 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2166 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2168 m_gridLineColour
= wxColour( 192,192,192 );
2169 m_gridLinesEnabled
= true;
2170 m_gridLinesClipHorz
=
2171 m_gridLinesClipVert
= true;
2172 m_cellHighlightColour
= *wxBLACK
;
2173 m_cellHighlightPenWidth
= 2;
2174 m_cellHighlightROPenWidth
= 1;
2176 m_canDragColMove
= false;
2178 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2179 m_winCapture
= NULL
;
2180 m_canDragRowSize
= true;
2181 m_canDragColSize
= true;
2182 m_canDragGridSize
= true;
2183 m_canDragCell
= false;
2185 m_dragRowOrCol
= -1;
2186 m_isDragging
= false;
2187 m_startDragPos
= wxDefaultPosition
;
2189 m_sortCol
= wxNOT_FOUND
;
2190 m_sortIsAscending
= true;
2193 m_nativeColumnLabels
= false;
2195 m_waitForSlowClick
= false;
2197 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2198 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2200 m_currentCellCoords
= wxGridNoCellCoords
;
2202 m_selectedBlockTopLeft
=
2203 m_selectedBlockBottomRight
=
2204 m_selectedBlockCorner
= wxGridNoCellCoords
;
2206 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2207 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2209 m_editable
= true; // default for whole grid
2211 m_inOnKeyDown
= false;
2217 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2218 // we don't need to call it neither as the scroll position is (0, 0) right
2219 // now anyhow, so just set the parameters directly
2220 m_xScrollPixelsPerLine
= GRID_SCROLL_LINE_X
;
2221 m_yScrollPixelsPerLine
= GRID_SCROLL_LINE_Y
;
2224 // ----------------------------------------------------------------------------
2225 // the idea is to call these functions only when necessary because they create
2226 // quite big arrays which eat memory mostly unnecessary - in particular, if
2227 // default widths/heights are used for all rows/columns, we may not use these
2230 // with some extra code, it should be possible to only store the widths/heights
2231 // different from default ones (resulting in space savings for huge grids) but
2232 // this is not done currently
2233 // ----------------------------------------------------------------------------
2235 void wxGrid::InitRowHeights()
2237 m_rowHeights
.Empty();
2238 m_rowBottoms
.Empty();
2240 m_rowHeights
.Alloc( m_numRows
);
2241 m_rowBottoms
.Alloc( m_numRows
);
2243 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2246 for ( int i
= 0; i
< m_numRows
; i
++ )
2248 rowBottom
+= m_defaultRowHeight
;
2249 m_rowBottoms
.Add( rowBottom
);
2253 void wxGrid::InitColWidths()
2255 m_colWidths
.Empty();
2256 m_colRights
.Empty();
2258 m_colWidths
.Alloc( m_numCols
);
2259 m_colRights
.Alloc( m_numCols
);
2261 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2263 for ( int i
= 0; i
< m_numCols
; i
++ )
2265 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2266 m_colRights
.Add( colRight
);
2270 int wxGrid::GetColWidth(int col
) const
2272 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2275 int wxGrid::GetColLeft(int col
) const
2277 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2278 : m_colRights
[col
] - m_colWidths
[col
];
2281 int wxGrid::GetColRight(int col
) const
2283 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2287 int wxGrid::GetRowHeight(int row
) const
2289 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2292 int wxGrid::GetRowTop(int row
) const
2294 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2295 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2298 int wxGrid::GetRowBottom(int row
) const
2300 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2301 : m_rowBottoms
[row
];
2304 void wxGrid::CalcDimensions()
2306 // compute the size of the scrollable area
2307 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2308 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2313 // take into account editor if shown
2314 if ( IsCellEditControlShown() )
2317 int r
= m_currentCellCoords
.GetRow();
2318 int c
= m_currentCellCoords
.GetCol();
2319 int x
= GetColLeft(c
);
2320 int y
= GetRowTop(r
);
2322 // how big is the editor
2323 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2324 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2325 editor
->GetControl()->GetSize(&w2
, &h2
);
2336 // preserve (more or less) the previous position
2338 GetViewStart( &x
, &y
);
2340 // ensure the position is valid for the new scroll ranges
2342 x
= wxMax( w
- 1, 0 );
2344 y
= wxMax( h
- 1, 0 );
2346 // update the virtual size and refresh the scrollbars to reflect it
2347 m_gridWin
->SetVirtualSize(w
, h
);
2351 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2352 // still must reposition the children
2356 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2358 wxSize
sizeGridWin(size
);
2359 sizeGridWin
.x
-= m_rowLabelWidth
;
2360 sizeGridWin
.y
-= m_colLabelHeight
;
2365 void wxGrid::CalcWindowSizes()
2367 // escape if the window is has not been fully created yet
2369 if ( m_cornerLabelWin
== NULL
)
2373 GetClientSize( &cw
, &ch
);
2375 // the grid may be too small to have enough space for the labels yet, don't
2376 // size the windows to negative sizes in this case
2377 int gw
= cw
- m_rowLabelWidth
;
2378 int gh
= ch
- m_colLabelHeight
;
2384 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2385 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2387 if ( m_colWindow
&& m_colWindow
->IsShown() )
2388 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2390 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2391 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2393 if ( m_gridWin
&& m_gridWin
->IsShown() )
2394 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2397 // this is called when the grid table sends a message
2398 // to indicate that it has been redimensioned
2400 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2403 bool result
= false;
2405 // Clear the attribute cache as the attribute might refer to a different
2406 // cell than stored in the cache after adding/removing rows/columns.
2409 // By the same reasoning, the editor should be dismissed if columns are
2410 // added or removed. And for consistency, it should IMHO always be
2411 // removed, not only if the cell "underneath" it actually changes.
2412 // For now, I intentionally do not save the editor's content as the
2413 // cell it might want to save that stuff to might no longer exist.
2414 HideCellEditControl();
2416 switch ( msg
.GetId() )
2418 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2420 size_t pos
= msg
.GetCommandInt();
2421 int numRows
= msg
.GetCommandInt2();
2423 m_numRows
+= numRows
;
2425 if ( !m_rowHeights
.IsEmpty() )
2427 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2428 m_rowBottoms
.Insert( 0, pos
, numRows
);
2432 bottom
= m_rowBottoms
[pos
- 1];
2434 for ( i
= pos
; i
< m_numRows
; i
++ )
2436 bottom
+= m_rowHeights
[i
];
2437 m_rowBottoms
[i
] = bottom
;
2441 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2443 // if we have just inserted cols into an empty grid the current
2444 // cell will be undefined...
2446 SetCurrentCell( 0, 0 );
2450 m_selection
->UpdateRows( pos
, numRows
);
2451 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2453 attrProvider
->UpdateAttrRows( pos
, numRows
);
2455 if ( !GetBatchCount() )
2458 m_rowLabelWin
->Refresh();
2464 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2466 int numRows
= msg
.GetCommandInt();
2467 int oldNumRows
= m_numRows
;
2468 m_numRows
+= numRows
;
2470 if ( !m_rowHeights
.IsEmpty() )
2472 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2473 m_rowBottoms
.Add( 0, numRows
);
2476 if ( oldNumRows
> 0 )
2477 bottom
= m_rowBottoms
[oldNumRows
- 1];
2479 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2481 bottom
+= m_rowHeights
[i
];
2482 m_rowBottoms
[i
] = bottom
;
2486 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2488 // if we have just inserted cols into an empty grid the current
2489 // cell will be undefined...
2491 SetCurrentCell( 0, 0 );
2494 if ( !GetBatchCount() )
2497 m_rowLabelWin
->Refresh();
2503 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2505 size_t pos
= msg
.GetCommandInt();
2506 int numRows
= msg
.GetCommandInt2();
2507 m_numRows
-= numRows
;
2509 if ( !m_rowHeights
.IsEmpty() )
2511 m_rowHeights
.RemoveAt( pos
, numRows
);
2512 m_rowBottoms
.RemoveAt( pos
, numRows
);
2515 for ( i
= 0; i
< m_numRows
; i
++ )
2517 h
+= m_rowHeights
[i
];
2518 m_rowBottoms
[i
] = h
;
2524 m_currentCellCoords
= wxGridNoCellCoords
;
2528 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2529 m_currentCellCoords
.Set( 0, 0 );
2533 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2534 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2537 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2539 // ifdef'd out following patch from Paul Gammans
2541 // No need to touch column attributes, unless we
2542 // removed _all_ rows, in this case, we remove
2543 // all column attributes.
2544 // I hate to do this here, but the
2545 // needed data is not available inside UpdateAttrRows.
2546 if ( !GetNumberRows() )
2547 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2551 if ( !GetBatchCount() )
2554 m_rowLabelWin
->Refresh();
2560 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2562 size_t pos
= msg
.GetCommandInt();
2563 int numCols
= msg
.GetCommandInt2();
2564 m_numCols
+= numCols
;
2566 if ( m_useNativeHeader
)
2567 GetGridColHeader()->SetColumnCount(m_numCols
);
2569 if ( !m_colAt
.IsEmpty() )
2571 //Shift the column IDs
2573 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2575 if ( m_colAt
[i
] >= (int)pos
)
2576 m_colAt
[i
] += numCols
;
2579 m_colAt
.Insert( pos
, pos
, numCols
);
2581 //Set the new columns' positions
2582 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2588 if ( !m_colWidths
.IsEmpty() )
2590 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2591 m_colRights
.Insert( 0, pos
, numCols
);
2595 right
= m_colRights
[GetColAt( pos
- 1 )];
2598 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2600 i
= GetColAt( colPos
);
2602 right
+= m_colWidths
[i
];
2603 m_colRights
[i
] = right
;
2607 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2609 // if we have just inserted cols into an empty grid the current
2610 // cell will be undefined...
2612 SetCurrentCell( 0, 0 );
2616 m_selection
->UpdateCols( pos
, numCols
);
2617 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2619 attrProvider
->UpdateAttrCols( pos
, numCols
);
2620 if ( !GetBatchCount() )
2623 m_colWindow
->Refresh();
2629 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2631 int numCols
= msg
.GetCommandInt();
2632 int oldNumCols
= m_numCols
;
2633 m_numCols
+= numCols
;
2634 if ( m_useNativeHeader
)
2635 GetGridColHeader()->SetColumnCount(m_numCols
);
2637 if ( !m_colAt
.IsEmpty() )
2639 m_colAt
.Add( 0, numCols
);
2641 //Set the new columns' positions
2643 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2649 if ( !m_colWidths
.IsEmpty() )
2651 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2652 m_colRights
.Add( 0, numCols
);
2655 if ( oldNumCols
> 0 )
2656 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2659 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2661 i
= GetColAt( colPos
);
2663 right
+= m_colWidths
[i
];
2664 m_colRights
[i
] = right
;
2668 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2670 // if we have just inserted cols into an empty grid the current
2671 // cell will be undefined...
2673 SetCurrentCell( 0, 0 );
2675 if ( !GetBatchCount() )
2678 m_colWindow
->Refresh();
2684 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2686 size_t pos
= msg
.GetCommandInt();
2687 int numCols
= msg
.GetCommandInt2();
2688 m_numCols
-= numCols
;
2689 if ( m_useNativeHeader
)
2690 GetGridColHeader()->SetColumnCount(m_numCols
);
2692 if ( !m_colAt
.IsEmpty() )
2694 int colID
= GetColAt( pos
);
2696 m_colAt
.RemoveAt( pos
, numCols
);
2698 //Shift the column IDs
2700 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2702 if ( m_colAt
[colPos
] > colID
)
2703 m_colAt
[colPos
] -= numCols
;
2707 if ( !m_colWidths
.IsEmpty() )
2709 m_colWidths
.RemoveAt( pos
, numCols
);
2710 m_colRights
.RemoveAt( pos
, numCols
);
2714 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2716 i
= GetColAt( colPos
);
2718 w
+= m_colWidths
[i
];
2725 m_currentCellCoords
= wxGridNoCellCoords
;
2729 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2730 m_currentCellCoords
.Set( 0, 0 );
2734 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2735 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2738 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2740 // ifdef'd out following patch from Paul Gammans
2742 // No need to touch row attributes, unless we
2743 // removed _all_ columns, in this case, we remove
2744 // all row attributes.
2745 // I hate to do this here, but the
2746 // needed data is not available inside UpdateAttrCols.
2747 if ( !GetNumberCols() )
2748 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2752 if ( !GetBatchCount() )
2755 m_colWindow
->Refresh();
2762 if (result
&& !GetBatchCount() )
2763 m_gridWin
->Refresh();
2768 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2770 wxRegionIterator
iter( reg
);
2773 wxArrayInt rowlabels
;
2780 // TODO: remove this when we can...
2781 // There is a bug in wxMotif that gives garbage update
2782 // rectangles if you jump-scroll a long way by clicking the
2783 // scrollbar with middle button. This is a work-around
2785 #if defined(__WXMOTIF__)
2787 m_gridWin
->GetClientSize( &cw
, &ch
);
2788 if ( r
.GetTop() > ch
)
2790 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2793 // logical bounds of update region
2796 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2797 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2799 // find the row labels within these bounds
2802 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2804 if ( GetRowBottom(row
) < top
)
2807 if ( GetRowTop(row
) > bottom
)
2810 rowlabels
.Add( row
);
2819 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2821 wxRegionIterator
iter( reg
);
2824 wxArrayInt colLabels
;
2831 // TODO: remove this when we can...
2832 // There is a bug in wxMotif that gives garbage update
2833 // rectangles if you jump-scroll a long way by clicking the
2834 // scrollbar with middle button. This is a work-around
2836 #if defined(__WXMOTIF__)
2838 m_gridWin
->GetClientSize( &cw
, &ch
);
2839 if ( r
.GetLeft() > cw
)
2841 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2844 // logical bounds of update region
2847 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2848 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2850 // find the cells within these bounds
2854 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2856 col
= GetColAt( colPos
);
2858 if ( GetColRight(col
) < left
)
2861 if ( GetColLeft(col
) > right
)
2864 colLabels
.Add( col
);
2873 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2875 wxRegionIterator
iter( reg
);
2878 wxGridCellCoordsArray cellsExposed
;
2880 int left
, top
, right
, bottom
;
2885 // TODO: remove this when we can...
2886 // There is a bug in wxMotif that gives garbage update
2887 // rectangles if you jump-scroll a long way by clicking the
2888 // scrollbar with middle button. This is a work-around
2890 #if defined(__WXMOTIF__)
2892 m_gridWin
->GetClientSize( &cw
, &ch
);
2893 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2894 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2895 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2896 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2899 // logical bounds of update region
2901 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2902 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2904 // find the cells within these bounds
2906 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2908 if ( GetRowBottom(row
) <= top
)
2911 if ( GetRowTop(row
) > bottom
)
2914 // add all dirty cells in this row: notice that the columns which
2915 // are dirty don't depend on the row so we compute them only once
2916 // for the first dirty row and then reuse for all the next ones
2919 // do determine the dirty columns
2920 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2921 cols
.push_back(GetColAt(pos
));
2923 // if there are no dirty columns at all, nothing to do
2928 const size_t count
= cols
.size();
2929 for ( size_t n
= 0; n
< count
; n
++ )
2930 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2936 return cellsExposed
;
2940 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2943 wxPoint
pos( event
.GetPosition() );
2944 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2946 if ( event
.Dragging() )
2950 m_isDragging
= true;
2951 m_rowLabelWin
->CaptureMouse();
2954 if ( event
.LeftIsDown() )
2956 switch ( m_cursorMode
)
2958 case WXGRID_CURSOR_RESIZE_ROW
:
2960 int cw
, ch
, left
, dummy
;
2961 m_gridWin
->GetClientSize( &cw
, &ch
);
2962 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2964 wxClientDC
dc( m_gridWin
);
2967 GetRowTop(m_dragRowOrCol
) +
2968 GetRowMinimalHeight(m_dragRowOrCol
) );
2969 dc
.SetLogicalFunction(wxINVERT
);
2970 if ( m_dragLastPos
>= 0 )
2972 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2974 dc
.DrawLine( left
, y
, left
+cw
, y
);
2979 case WXGRID_CURSOR_SELECT_ROW
:
2981 if ( (row
= YToRow( y
)) >= 0 )
2984 m_selection
->SelectRow(row
, event
);
2989 // default label to suppress warnings about "enumeration value
2990 // 'xxx' not handled in switch
2998 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3003 if (m_rowLabelWin
->HasCapture())
3004 m_rowLabelWin
->ReleaseMouse();
3005 m_isDragging
= false;
3008 // ------------ Entering or leaving the window
3010 if ( event
.Entering() || event
.Leaving() )
3012 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3015 // ------------ Left button pressed
3017 else if ( event
.LeftDown() )
3019 row
= YToEdgeOfRow(y
);
3020 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3022 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3024 else // not a request to start resizing
3028 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3030 if ( !event
.ShiftDown() && !event
.CmdDown() )
3034 if ( event
.ShiftDown() )
3036 m_selection
->SelectBlock
3038 m_currentCellCoords
.GetRow(), 0,
3039 row
, GetNumberCols() - 1,
3045 m_selection
->SelectRow(row
, event
);
3049 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3054 // ------------ Left double click
3056 else if (event
.LeftDClick() )
3058 row
= YToEdgeOfRow(y
);
3059 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3061 // adjust row height depending on label text
3063 // TODO: generate RESIZING event, see #10754
3064 AutoSizeRowLabelSize( row
);
3066 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3068 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3071 else // not on row separator or it's not resizable
3075 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3077 // no default action at the moment
3082 // ------------ Left button released
3084 else if ( event
.LeftUp() )
3086 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3087 DoEndDragResizeRow(event
);
3089 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3093 // ------------ Right button down
3095 else if ( event
.RightDown() )
3099 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3101 // no default action at the moment
3105 // ------------ Right double click
3107 else if ( event
.RightDClick() )
3111 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3113 // no default action at the moment
3117 // ------------ No buttons down and mouse moving
3119 else if ( event
.Moving() )
3121 m_dragRowOrCol
= YToEdgeOfRow( y
);
3122 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3124 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3126 if ( CanDragRowSize(m_dragRowOrCol
) )
3127 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3130 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3132 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3137 void wxGrid::UpdateColumnSortingIndicator(int col
)
3139 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3141 if ( m_useNativeHeader
)
3142 GetGridColHeader()->UpdateColumn(col
);
3143 else if ( m_nativeColumnLabels
)
3144 m_colWindow
->Refresh();
3145 //else: sorting indicator display not yet implemented in grid version
3148 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3150 if ( col
== m_sortCol
)
3152 // we are already using this column for sorting (or not sorting at all)
3153 // but we might still change the sorting order, check for it
3154 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3156 m_sortIsAscending
= ascending
;
3158 UpdateColumnSortingIndicator(m_sortCol
);
3161 else // we're changing the column used for sorting
3163 const int sortColOld
= m_sortCol
;
3165 // change it before updating the column as we want GetSortingColumn()
3166 // to return the correct new value
3169 if ( sortColOld
!= wxNOT_FOUND
)
3170 UpdateColumnSortingIndicator(sortColOld
);
3172 if ( m_sortCol
!= wxNOT_FOUND
)
3174 m_sortIsAscending
= ascending
;
3175 UpdateColumnSortingIndicator(m_sortCol
);
3180 void wxGrid::DoColHeaderClick(int col
)
3182 // we consider that the grid was resorted if this event is processed and
3184 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3186 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3191 void wxGrid::DoStartResizeCol(int col
)
3193 m_dragRowOrCol
= col
;
3195 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3198 void wxGrid::DoUpdateResizeCol(int x
)
3200 int cw
, ch
, dummy
, top
;
3201 m_gridWin
->GetClientSize( &cw
, &ch
);
3202 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3204 wxClientDC
dc( m_gridWin
);
3207 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3208 dc
.SetLogicalFunction(wxINVERT
);
3209 if ( m_dragLastPos
>= 0 )
3211 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3213 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3217 void wxGrid::DoUpdateResizeColWidth(int w
)
3219 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3222 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3225 wxPoint
pos( event
.GetPosition() );
3226 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3228 int col
= XToCol(x
);
3229 if ( event
.Dragging() )
3233 m_isDragging
= true;
3234 GetColLabelWindow()->CaptureMouse();
3236 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3237 DoStartMoveCol(col
);
3240 if ( event
.LeftIsDown() )
3242 switch ( m_cursorMode
)
3244 case WXGRID_CURSOR_RESIZE_COL
:
3245 DoUpdateResizeCol(x
);
3248 case WXGRID_CURSOR_SELECT_COL
:
3253 m_selection
->SelectCol(col
, event
);
3258 case WXGRID_CURSOR_MOVE_COL
:
3260 int posNew
= XToPos(x
);
3261 int colNew
= GetColAt(posNew
);
3263 // determine the position of the drop marker
3265 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3266 markerX
= GetColRight(colNew
);
3268 markerX
= GetColLeft(colNew
);
3270 if ( markerX
!= m_dragLastPos
)
3272 wxClientDC
dc( GetColLabelWindow() );
3276 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3280 //Clean up the last indicator
3281 if ( m_dragLastPos
>= 0 )
3283 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3285 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3286 dc
.SetPen(wxNullPen
);
3288 if ( XToCol( m_dragLastPos
) != -1 )
3289 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3292 const wxColour
*color
;
3293 //Moving to the same place? Don't draw a marker
3294 if ( colNew
== m_dragRowOrCol
)
3295 color
= wxLIGHT_GREY
;
3300 wxPen
pen( *color
, 2 );
3303 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3305 dc
.SetPen(wxNullPen
);
3307 m_dragLastPos
= markerX
- 1;
3312 // default label to suppress warnings about "enumeration value
3313 // 'xxx' not handled in switch
3321 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3326 if (GetColLabelWindow()->HasCapture())
3327 GetColLabelWindow()->ReleaseMouse();
3328 m_isDragging
= false;
3331 // ------------ Entering or leaving the window
3333 if ( event
.Entering() || event
.Leaving() )
3335 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3338 // ------------ Left button pressed
3340 else if ( event
.LeftDown() )
3342 int col
= XToEdgeOfCol(x
);
3343 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3345 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3347 else // not a request to start resizing
3351 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3353 if ( m_canDragColMove
)
3355 //Show button as pressed
3356 wxClientDC
dc( GetColLabelWindow() );
3357 int colLeft
= GetColLeft( col
);
3358 int colRight
= GetColRight( col
) - 1;
3359 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3360 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3361 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3363 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3367 if ( !event
.ShiftDown() && !event
.CmdDown() )
3371 if ( event
.ShiftDown() )
3373 m_selection
->SelectBlock
3375 0, m_currentCellCoords
.GetCol(),
3376 GetNumberRows() - 1, col
,
3382 m_selection
->SelectCol(col
, event
);
3386 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3392 // ------------ Left double click
3394 if ( event
.LeftDClick() )
3396 const int colEdge
= XToEdgeOfCol(x
);
3397 if ( colEdge
== -1 )
3400 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3402 // no default action at the moment
3407 // adjust column width depending on label text
3409 // TODO: generate RESIZING event, see #10754
3410 AutoSizeColLabelSize( colEdge
);
3412 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3414 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3419 // ------------ Left button released
3421 else if ( event
.LeftUp() )
3423 switch ( m_cursorMode
)
3425 case WXGRID_CURSOR_RESIZE_COL
:
3426 DoEndDragResizeCol(event
);
3429 case WXGRID_CURSOR_MOVE_COL
:
3430 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3432 // the column didn't actually move anywhere
3434 DoColHeaderClick(col
);
3435 m_colWindow
->Refresh(); // "unpress" the column
3439 // get the position of the column we're over
3440 int pos
= XToPos(x
);
3442 // we may need to adjust the drop position but don't bother
3443 // checking for it if we can't anyhow
3446 // also find the index of the column we're over: notice
3447 // that the existing "col" variable may be invalid but
3448 // we need a valid one here
3449 const int colValid
= GetColAt(pos
);
3451 // if we're on the "near" (usually left but right in
3452 // RTL case) part of the column, the actual position we
3453 // should be placed in is actually the one before it
3455 const int middle
= GetColLeft(colValid
) +
3456 GetColWidth(colValid
)/2;
3457 if ( GetLayoutDirection() == wxLayout_LeftToRight
)
3458 onNearPart
= (x
<= middle
);
3459 else // wxLayout_RightToLeft
3460 onNearPart
= (x
> middle
);
3470 case WXGRID_CURSOR_SELECT_COL
:
3471 case WXGRID_CURSOR_SELECT_CELL
:
3472 case WXGRID_CURSOR_RESIZE_ROW
:
3473 case WXGRID_CURSOR_SELECT_ROW
:
3475 DoColHeaderClick(col
);
3479 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3483 // ------------ Right button down
3485 else if ( event
.RightDown() )
3488 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3490 // no default action at the moment
3494 // ------------ Right double click
3496 else if ( event
.RightDClick() )
3499 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3501 // no default action at the moment
3505 // ------------ No buttons down and mouse moving
3507 else if ( event
.Moving() )
3509 m_dragRowOrCol
= XToEdgeOfCol( x
);
3510 if ( m_dragRowOrCol
>= 0 )
3512 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3514 if ( CanDragColSize(m_dragRowOrCol
) )
3515 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3518 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3520 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3525 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3527 if ( event
.LeftDown() )
3529 // indicate corner label by having both row and
3532 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3537 else if ( event
.LeftDClick() )
3539 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3541 else if ( event
.RightDown() )
3543 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3545 // no default action at the moment
3548 else if ( event
.RightDClick() )
3550 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3552 // no default action at the moment
3557 void wxGrid::CancelMouseCapture()
3559 // cancel operation currently in progress, whatever it is
3562 m_isDragging
= false;
3563 m_startDragPos
= wxDefaultPosition
;
3565 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3566 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3567 m_winCapture
= NULL
;
3569 // remove traces of whatever we drew on screen
3574 void wxGrid::ChangeCursorMode(CursorMode mode
,
3579 static const wxChar
*const cursorModes
[] =
3589 wxLogTrace(wxT("grid"),
3590 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3591 win
== m_colWindow
? wxT("colLabelWin")
3592 : win
? wxT("rowLabelWin")
3594 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3595 #endif // wxUSE_LOG_TRACE
3597 if ( mode
== m_cursorMode
&&
3598 win
== m_winCapture
&&
3599 captureMouse
== (m_winCapture
!= NULL
))
3604 // by default use the grid itself
3610 m_winCapture
->ReleaseMouse();
3611 m_winCapture
= NULL
;
3614 m_cursorMode
= mode
;
3616 switch ( m_cursorMode
)
3618 case WXGRID_CURSOR_RESIZE_ROW
:
3619 win
->SetCursor( m_rowResizeCursor
);
3622 case WXGRID_CURSOR_RESIZE_COL
:
3623 win
->SetCursor( m_colResizeCursor
);
3626 case WXGRID_CURSOR_MOVE_COL
:
3627 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3631 win
->SetCursor( *wxSTANDARD_CURSOR
);
3635 // we need to capture mouse when resizing
3636 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3637 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3639 if ( captureMouse
&& resize
)
3641 win
->CaptureMouse();
3646 // ----------------------------------------------------------------------------
3647 // grid mouse event processing
3648 // ----------------------------------------------------------------------------
3651 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3652 const wxGridCellCoords
& coords
,
3655 if ( coords
== wxGridNoCellCoords
)
3656 return; // we're outside any valid cell
3658 // Hide the edit control, so it won't interfere with drag-shrinking.
3659 if ( IsCellEditControlShown() )
3661 HideCellEditControl();
3662 SaveEditControlValue();
3665 switch ( event
.GetModifiers() )
3668 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3669 m_selectedBlockCorner
= coords
;
3670 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3674 if ( CanDragCell() )
3678 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3679 m_selectedBlockCorner
= coords
;
3681 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3686 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3690 // we don't handle the other key modifiers
3695 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3697 wxClientDC
dc(m_gridWin
);
3699 dc
.SetLogicalFunction(wxINVERT
);
3701 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3702 m_gridWin
->GetClientSize());
3704 // erase the previously drawn line, if any
3705 if ( m_dragLastPos
>= 0 )
3706 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3708 // we need the vertical position for rows and horizontal for columns here
3709 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3711 // don't allow resizing beneath the minimal size
3712 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3713 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3714 if ( m_dragLastPos
< posMin
)
3715 m_dragLastPos
= posMin
;
3717 // and draw it at the new position
3718 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3721 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3723 if ( !m_isDragging
)
3725 // Don't start doing anything until the mouse has been dragged far
3727 const wxPoint
& pt
= event
.GetPosition();
3728 if ( m_startDragPos
== wxDefaultPosition
)
3730 m_startDragPos
= pt
;
3734 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3735 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3739 const bool isFirstDrag
= !m_isDragging
;
3740 m_isDragging
= true;
3742 switch ( m_cursorMode
)
3744 case WXGRID_CURSOR_SELECT_CELL
:
3745 DoGridCellDrag(event
, coords
, isFirstDrag
);
3748 case WXGRID_CURSOR_RESIZE_ROW
:
3749 DoGridLineDrag(event
, wxGridRowOperations());
3752 case WXGRID_CURSOR_RESIZE_COL
:
3753 DoGridLineDrag(event
, wxGridColumnOperations());
3762 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
3764 m_winCapture
= m_gridWin
;
3765 m_winCapture
->CaptureMouse();
3770 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3771 const wxGridCellCoords
& coords
,
3774 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3776 // event handled by user code, no need to do anything here
3780 if ( !event
.CmdDown() )
3783 if ( event
.ShiftDown() )
3787 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3788 m_selectedBlockCorner
= coords
;
3791 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3793 DisableCellEditControl();
3794 MakeCellVisible( coords
);
3796 if ( event
.CmdDown() )
3800 m_selection
->ToggleCellSelection(coords
, event
);
3803 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3804 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3805 m_selectedBlockCorner
= coords
;
3811 // In row or column selection mode just clicking on the cell
3812 // should select the row or column containing it: this is more
3813 // convenient for the kinds of controls that use such selection
3814 // mode and is compatible with 2.8 behaviour (see #12062).
3815 switch ( m_selection
->GetSelectionMode() )
3817 case wxGridSelectCells
:
3818 case wxGridSelectRowsOrColumns
:
3819 // nothing to do in these cases
3822 case wxGridSelectRows
:
3823 m_selection
->SelectRow(coords
.GetRow());
3826 case wxGridSelectColumns
:
3827 m_selection
->SelectCol(coords
.GetCol());
3832 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3833 coords
!= wxGridNoCellCoords
;
3834 SetCurrentCell( coords
);
3840 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3841 const wxGridCellCoords
& coords
,
3844 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3846 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3848 // we want double click to select a cell and start editing
3849 // (i.e. to behave in same way as sequence of two slow clicks):
3850 m_waitForSlowClick
= true;
3856 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3858 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3862 m_winCapture
->ReleaseMouse();
3863 m_winCapture
= NULL
;
3866 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3869 EnableCellEditControl();
3871 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3872 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3873 editor
->StartingClick();
3877 m_waitForSlowClick
= false;
3879 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3880 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3884 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3885 m_selectedBlockBottomRight
,
3889 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3890 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3892 // Show the edit control, if it has been hidden for
3894 ShowCellEditControl();
3897 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3899 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3900 DoEndDragResizeRow(event
);
3902 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3904 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3905 DoEndDragResizeCol(event
);
3912 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3913 const wxGridCellCoords
& coords
,
3916 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3918 // out of grid cell area
3919 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3923 int dragRow
= YToEdgeOfRow( pos
.y
);
3924 int dragCol
= XToEdgeOfCol( pos
.x
);
3926 // Dragging on the corner of a cell to resize in both
3927 // directions is not implemented yet...
3929 if ( dragRow
>= 0 && dragCol
>= 0 )
3931 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3935 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3937 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3939 m_dragRowOrCol
= dragRow
;
3940 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3943 // When using the native header window we can only resize the columns by
3944 // dragging the dividers in it because we can't make it enter into the
3945 // column resizing mode programmatically
3946 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3947 CanDragGridSize() && CanDragColSize(dragCol
) )
3949 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3951 m_dragRowOrCol
= dragCol
;
3952 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3955 else // Neither on a row or col edge
3957 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3959 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3964 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
3966 if ( event
.Entering() || event
.Leaving() )
3968 // we don't care about these events but we must not reset m_isDragging
3969 // if they happen so return before anything else is done
3974 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
3976 // coordinates of the cell under mouse
3977 wxGridCellCoords coords
= XYToCell(pos
);
3979 int cell_rows
, cell_cols
;
3980 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
3981 if ( (cell_rows
< 0) || (cell_cols
< 0) )
3983 coords
.SetRow(coords
.GetRow() + cell_rows
);
3984 coords
.SetCol(coords
.GetCol() + cell_cols
);
3987 if ( event
.Dragging() )
3989 if ( event
.LeftIsDown() )
3990 DoGridDragEvent(event
, coords
);
3996 m_isDragging
= false;
3997 m_startDragPos
= wxDefaultPosition
;
3999 // deal with various button presses
4000 if ( event
.IsButton() )
4002 if ( coords
!= wxGridNoCellCoords
)
4004 DisableCellEditControl();
4006 if ( event
.LeftDown() )
4007 DoGridCellLeftDown(event
, coords
, pos
);
4008 else if ( event
.LeftDClick() )
4009 DoGridCellLeftDClick(event
, coords
, pos
);
4010 else if ( event
.RightDown() )
4011 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
4012 else if ( event
.RightDClick() )
4013 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
4016 // this one should be called even if we're not over any cell
4017 if ( event
.LeftUp() )
4019 DoGridCellLeftUp(event
, coords
);
4022 else if ( event
.Moving() )
4024 DoGridMouseMoveEvent(event
, coords
, pos
);
4026 else // unknown mouse event?
4032 // this function returns true only if the size really changed
4033 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4035 if ( m_dragLastPos
== -1 )
4038 const wxGridOperations
& doper
= oper
.Dual();
4040 const wxSize size
= m_gridWin
->GetClientSize();
4042 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4044 // erase the last line we drew
4045 wxClientDC
dc(m_gridWin
);
4047 dc
.SetLogicalFunction(wxINVERT
);
4049 const int posLineStart
= oper
.Select(ptOrigin
);
4050 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4052 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4054 // temporarily hide the edit control before resizing
4055 HideCellEditControl();
4056 SaveEditControlValue();
4058 // do resize the line
4059 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4060 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4061 oper
.SetLineSize(this, m_dragRowOrCol
,
4062 wxMax(m_dragLastPos
- lineStart
,
4063 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4065 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4069 // refresh now if we're not frozen
4070 if ( !GetBatchCount() )
4072 // we need to refresh everything beyond the resized line in the header
4075 // get the position from which to refresh in the other direction
4076 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4077 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4079 // we only need the ordinate (for rows) or abscissa (for columns) here,
4080 // and need to cover the entire window in the other direction
4081 oper
.Select(rect
) = 0;
4083 wxRect
rectHeader(rect
.GetPosition(),
4086 oper
.GetHeaderWindowSize(this),
4087 doper
.Select(size
) - doper
.Select(rect
)
4090 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4093 // also refresh the grid window: extend the rectangle
4096 oper
.SelectSize(rect
) = oper
.Select(size
);
4098 int subtractLines
= 0;
4099 const int lineStart
= doper
.PosToLine(this, posLineStart
);
4100 if ( lineStart
>= 0 )
4102 // ensure that if we have a multi-cell block we redraw all of
4103 // it by increasing the refresh area to cover it entirely if a
4104 // part of it is affected
4105 const int lineEnd
= doper
.PosToLine(this, posLineEnd
, true);
4106 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
4108 int cellLines
= oper
.Select(
4109 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4110 if ( cellLines
< subtractLines
)
4111 subtractLines
= cellLines
;
4116 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4117 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4119 doper
.Select(rect
) = startPos
;
4120 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4122 m_gridWin
->Refresh(false, &rect
);
4126 // show the edit control back again
4127 ShowCellEditControl();
4132 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4134 // TODO: generate RESIZING event, see #10754
4136 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4137 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4140 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4142 // TODO: generate RESIZING event, see #10754
4144 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4145 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4148 void wxGrid::DoStartMoveCol(int col
)
4150 m_dragRowOrCol
= col
;
4153 void wxGrid::DoEndMoveCol(int pos
)
4155 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4157 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4158 SetColPos(m_dragRowOrCol
, pos
);
4159 //else: vetoed by user
4161 m_dragRowOrCol
= -1;
4164 void wxGrid::RefreshAfterColPosChange()
4166 // recalculate the column rights as the column positions have changed,
4167 // unless we calculate them dynamically because all columns widths are the
4168 // same and it's easy to do
4169 if ( !m_colWidths
.empty() )
4172 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4174 int colID
= GetColAt( colPos
);
4176 colRight
+= m_colWidths
[colID
];
4177 m_colRights
[colID
] = colRight
;
4181 // and make the changes visible
4182 if ( m_useNativeHeader
)
4184 if ( m_colAt
.empty() )
4185 GetGridColHeader()->ResetColumnsOrder();
4187 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4191 m_colWindow
->Refresh();
4193 m_gridWin
->Refresh();
4196 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4200 RefreshAfterColPosChange();
4203 void wxGrid::SetColPos(int idx
, int pos
)
4205 // we're going to need m_colAt now, initialize it if needed
4206 if ( m_colAt
.empty() )
4208 m_colAt
.reserve(m_numCols
);
4209 for ( int i
= 0; i
< m_numCols
; i
++ )
4210 m_colAt
.push_back(i
);
4213 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4215 RefreshAfterColPosChange();
4218 void wxGrid::ResetColPos()
4222 RefreshAfterColPosChange();
4225 void wxGrid::EnableDragColMove( bool enable
)
4227 if ( m_canDragColMove
== enable
)
4230 if ( m_useNativeHeader
)
4232 // update all columns to make them [not] reorderable
4233 GetGridColHeader()->SetColumnCount(m_numCols
);
4236 m_canDragColMove
= enable
;
4238 // we use to call ResetColPos() from here if !enable but this doesn't seem
4239 // right as it would mean there would be no way to "freeze" the current
4240 // columns order by disabling moving them after putting them in the desired
4241 // order, whereas now you can always call ResetColPos() manually if needed
4246 // ------ interaction with data model
4248 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4250 switch ( msg
.GetId() )
4252 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4253 return GetModelValues();
4255 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4256 return SetModelValues();
4258 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4259 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4260 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4261 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4262 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4263 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4264 return Redimension( msg
);
4271 // The behaviour of this function depends on the grid table class
4272 // Clear() function. For the default wxGridStringTable class the
4273 // behaviour is to replace all cell contents with wxEmptyString but
4274 // not to change the number of rows or cols.
4276 void wxGrid::ClearGrid()
4280 if (IsCellEditControlEnabled())
4281 DisableCellEditControl();
4284 if (!GetBatchCount())
4285 m_gridWin
->Refresh();
4290 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4291 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4293 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4298 if ( IsCellEditControlEnabled() )
4299 DisableCellEditControl();
4301 return (m_table
->*funcModify
)(pos
, num
);
4303 // the table will have sent the results of the insert row
4304 // operation to this view object as a grid table message
4308 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4309 int num
, bool WXUNUSED(updateLabels
))
4311 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4316 return (m_table
->*funcAppend
)(num
);
4319 // ----------------------------------------------------------------------------
4320 // event generation helpers
4321 // ----------------------------------------------------------------------------
4324 wxGrid::SendGridSizeEvent(wxEventType type
,
4326 const wxMouseEvent
& mouseEv
)
4328 int rowOrCol
= row
== -1 ? col
: row
;
4330 wxGridSizeEvent
gridEvt( GetId(),
4334 mouseEv
.GetX() + GetRowLabelSize(),
4335 mouseEv
.GetY() + GetColLabelSize(),
4338 GetEventHandler()->ProcessEvent(gridEvt
);
4341 // Generate a grid event based on a mouse event and return:
4342 // -1 if the event was vetoed
4343 // +1 if the event was processed (but not vetoed)
4344 // 0 if the event wasn't handled
4346 wxGrid::SendEvent(const wxEventType type
,
4348 const wxMouseEvent
& mouseEv
)
4350 bool claimed
, vetoed
;
4352 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4354 // Right now, it should _never_ end up here!
4355 wxGridRangeSelectEvent
gridEvt( GetId(),
4358 m_selectedBlockTopLeft
,
4359 m_selectedBlockBottomRight
,
4363 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4364 vetoed
= !gridEvt
.IsAllowed();
4366 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4367 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4368 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4369 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4371 wxPoint pos
= mouseEv
.GetPosition();
4373 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4374 pos
.y
+= GetColLabelSize();
4375 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4376 pos
.x
+= GetRowLabelSize();
4378 wxGridEvent
gridEvt( GetId(),
4386 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4387 vetoed
= !gridEvt
.IsAllowed();
4391 wxGridEvent
gridEvt( GetId(),
4395 mouseEv
.GetX() + GetRowLabelSize(),
4396 mouseEv
.GetY() + GetColLabelSize(),
4399 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4400 vetoed
= !gridEvt
.IsAllowed();
4403 // A Veto'd event may not be `claimed' so test this first
4407 return claimed
? 1 : 0;
4410 // Generate a grid event of specified type, return value same as above
4413 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4415 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4416 gridEvt
.SetString(s
);
4418 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4420 // A Veto'd event may not be `claimed' so test this first
4421 if ( !gridEvt
.IsAllowed() )
4424 return claimed
? 1 : 0;
4427 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4429 // needed to prevent zillions of paint events on MSW
4433 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4435 // Don't do anything if between Begin/EndBatch...
4436 // EndBatch() will do all this on the last nested one anyway.
4437 if ( m_created
&& !GetBatchCount() )
4439 // Refresh to get correct scrolled position:
4440 wxScrolledWindow::Refresh(eraseb
, rect
);
4444 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4445 int width_label
, width_cell
, height_label
, height_cell
;
4448 // Copy rectangle can get scroll offsets..
4449 rect_x
= rect
->GetX();
4450 rect_y
= rect
->GetY();
4451 rectWidth
= rect
->GetWidth();
4452 rectHeight
= rect
->GetHeight();
4454 width_label
= m_rowLabelWidth
- rect_x
;
4455 if (width_label
> rectWidth
)
4456 width_label
= rectWidth
;
4458 height_label
= m_colLabelHeight
- rect_y
;
4459 if (height_label
> rectHeight
)
4460 height_label
= rectHeight
;
4462 if (rect_x
> m_rowLabelWidth
)
4464 x
= rect_x
- m_rowLabelWidth
;
4465 width_cell
= rectWidth
;
4470 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4473 if (rect_y
> m_colLabelHeight
)
4475 y
= rect_y
- m_colLabelHeight
;
4476 height_cell
= rectHeight
;
4481 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4484 // Paint corner label part intersecting rect.
4485 if ( width_label
> 0 && height_label
> 0 )
4487 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4488 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4491 // Paint col labels part intersecting rect.
4492 if ( width_cell
> 0 && height_label
> 0 )
4494 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4495 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4498 // Paint row labels part intersecting rect.
4499 if ( width_label
> 0 && height_cell
> 0 )
4501 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4502 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4505 // Paint cell area part intersecting rect.
4506 if ( width_cell
> 0 && height_cell
> 0 )
4508 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4509 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4514 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4515 m_colWindow
->Refresh(eraseb
, NULL
);
4516 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4517 m_gridWin
->Refresh(eraseb
, NULL
);
4522 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4524 if (m_targetWindow
!= this) // check whether initialisation has been done
4526 // reposition our children windows
4531 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4533 if ( m_inOnKeyDown
)
4535 // shouldn't be here - we are going round in circles...
4537 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4540 m_inOnKeyDown
= true;
4542 // propagate the event up and see if it gets processed
4543 wxWindow
*parent
= GetParent();
4544 wxKeyEvent
keyEvt( event
);
4545 keyEvt
.SetEventObject( parent
);
4547 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4549 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4551 if (event
.GetKeyCode() == WXK_RIGHT
)
4552 event
.m_keyCode
= WXK_LEFT
;
4553 else if (event
.GetKeyCode() == WXK_LEFT
)
4554 event
.m_keyCode
= WXK_RIGHT
;
4557 // try local handlers
4558 switch ( event
.GetKeyCode() )
4561 if ( event
.ControlDown() )
4562 MoveCursorUpBlock( event
.ShiftDown() );
4564 MoveCursorUp( event
.ShiftDown() );
4568 if ( event
.ControlDown() )
4569 MoveCursorDownBlock( event
.ShiftDown() );
4571 MoveCursorDown( event
.ShiftDown() );
4575 if ( event
.ControlDown() )
4576 MoveCursorLeftBlock( event
.ShiftDown() );
4578 MoveCursorLeft( event
.ShiftDown() );
4582 if ( event
.ControlDown() )
4583 MoveCursorRightBlock( event
.ShiftDown() );
4585 MoveCursorRight( event
.ShiftDown() );
4589 case WXK_NUMPAD_ENTER
:
4590 if ( event
.ControlDown() )
4592 event
.Skip(); // to let the edit control have the return
4596 if ( GetGridCursorRow() < GetNumberRows()-1 )
4598 MoveCursorDown( event
.ShiftDown() );
4602 // at the bottom of a column
4603 DisableCellEditControl();
4613 if (event
.ShiftDown())
4615 if ( GetGridCursorCol() > 0 )
4617 MoveCursorLeft( false );
4622 DisableCellEditControl();
4627 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4629 MoveCursorRight( false );
4634 DisableCellEditControl();
4640 GoToCell(event
.ControlDown() ? 0
4641 : m_currentCellCoords
.GetRow(),
4646 GoToCell(event
.ControlDown() ? m_numRows
- 1
4647 : m_currentCellCoords
.GetRow(),
4660 // Ctrl-Space selects the current column, Shift-Space -- the
4661 // current row and Ctrl-Shift-Space -- everything
4662 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4665 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4669 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4672 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4673 m_selection
->SelectBlock(0, 0,
4674 m_numRows
- 1, m_numCols
- 1);
4678 if ( !IsEditable() )
4680 MoveCursorRight(false);
4683 //else: fall through
4696 m_inOnKeyDown
= false;
4699 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4701 // try local handlers
4703 if ( event
.GetKeyCode() == WXK_SHIFT
)
4705 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4706 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4710 m_selection
->SelectBlock(
4711 m_selectedBlockTopLeft
,
4712 m_selectedBlockBottomRight
,
4717 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4718 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4719 m_selectedBlockCorner
= wxGridNoCellCoords
;
4723 void wxGrid::OnChar( wxKeyEvent
& event
)
4725 // is it possible to edit the current cell at all?
4726 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4728 // yes, now check whether the cells editor accepts the key
4729 int row
= m_currentCellCoords
.GetRow();
4730 int col
= m_currentCellCoords
.GetCol();
4731 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4732 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4734 // <F2> is special and will always start editing, for
4735 // other keys - ask the editor itself
4736 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4737 || editor
->IsAcceptedKey(event
) )
4739 // ensure cell is visble
4740 MakeCellVisible(row
, col
);
4741 EnableCellEditControl();
4743 // a problem can arise if the cell is not completely
4744 // visible (even after calling MakeCellVisible the
4745 // control is not created and calling StartingKey will
4747 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4748 editor
->StartingKey(event
);
4764 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4768 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4770 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4772 // the event has been vetoed - do nothing
4776 #if !defined(__WXMAC__)
4777 wxClientDC
dc( m_gridWin
);
4781 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4783 DisableCellEditControl();
4785 if ( IsVisible( m_currentCellCoords
, false ) )
4788 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4789 if ( !m_gridLinesEnabled
)
4797 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4799 // Otherwise refresh redraws the highlight!
4800 m_currentCellCoords
= coords
;
4802 #if defined(__WXMAC__)
4803 m_gridWin
->Refresh(true /*, & r */);
4805 DrawGridCellArea( dc
, cells
);
4806 DrawAllGridLines( dc
, r
);
4811 m_currentCellCoords
= coords
;
4813 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4814 #if !defined(__WXMAC__)
4815 DrawCellHighlight( dc
, attr
);
4823 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4824 int bottomRow
, int rightCol
)
4826 MakeCellVisible(m_selectedBlockCorner
);
4827 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4831 switch ( m_selection
->GetSelectionMode() )
4834 wxFAIL_MSG( "unknown selection mode" );
4837 case wxGridSelectCells
:
4838 // arbitrary blocks selection allowed so just use the cell
4839 // coordinates as is
4842 case wxGridSelectRows
:
4843 // only full rows selection allowd, ensure that we do select
4846 rightCol
= GetNumberCols() - 1;
4849 case wxGridSelectColumns
:
4850 // same as above but for columns
4852 bottomRow
= GetNumberRows() - 1;
4855 case wxGridSelectRowsOrColumns
:
4856 // in this mode we can select only full rows or full columns so
4857 // it doesn't make sense to select blocks at all (and we can't
4858 // extend the block because there is no preferred direction, we
4859 // could only extend it to cover the entire grid but this is
4865 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4866 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4868 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4869 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4871 // First the case that we selected a completely new area
4872 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4873 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4876 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4877 wxGridCellCoords ( bottomRow
, rightCol
) );
4878 m_gridWin
->Refresh( false, &rect
);
4881 // Now handle changing an existing selection area.
4882 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4883 m_selectedBlockBottomRight
!= updateBottomRight
)
4885 // Compute two optimal update rectangles:
4886 // Either one rectangle is a real subset of the
4887 // other, or they are (almost) disjoint!
4889 bool need_refresh
[4];
4893 need_refresh
[3] = false;
4896 // Store intermediate values
4897 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4898 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4899 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4900 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4902 // Determine the outer/inner coordinates.
4903 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4904 EnsureFirstLessThanSecond(oldTop
, topRow
);
4905 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4906 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4908 // Now, either the stuff marked old is the outer
4909 // rectangle or we don't have a situation where one
4910 // is contained in the other.
4912 if ( oldLeft
< leftCol
)
4914 // Refresh the newly selected or deselected
4915 // area to the left of the old or new selection.
4916 need_refresh
[0] = true;
4917 rect
[0] = BlockToDeviceRect(
4918 wxGridCellCoords( oldTop
, oldLeft
),
4919 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4922 if ( oldTop
< topRow
)
4924 // Refresh the newly selected or deselected
4925 // area above the old or new selection.
4926 need_refresh
[1] = true;
4927 rect
[1] = BlockToDeviceRect(
4928 wxGridCellCoords( oldTop
, leftCol
),
4929 wxGridCellCoords( topRow
- 1, rightCol
) );
4932 if ( oldRight
> rightCol
)
4934 // Refresh the newly selected or deselected
4935 // area to the right of the old or new selection.
4936 need_refresh
[2] = true;
4937 rect
[2] = BlockToDeviceRect(
4938 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4939 wxGridCellCoords( oldBottom
, oldRight
) );
4942 if ( oldBottom
> bottomRow
)
4944 // Refresh the newly selected or deselected
4945 // area below the old or new selection.
4946 need_refresh
[3] = true;
4947 rect
[3] = BlockToDeviceRect(
4948 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4949 wxGridCellCoords( oldBottom
, rightCol
) );
4952 // various Refresh() calls
4953 for (i
= 0; i
< 4; i
++ )
4954 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4955 m_gridWin
->Refresh( false, &(rect
[i
]) );
4959 m_selectedBlockTopLeft
= updateTopLeft
;
4960 m_selectedBlockBottomRight
= updateBottomRight
;
4964 // ------ functions to get/send data (see also public functions)
4967 bool wxGrid::GetModelValues()
4969 // Hide the editor, so it won't hide a changed value.
4970 HideCellEditControl();
4974 // all we need to do is repaint the grid
4976 m_gridWin
->Refresh();
4983 bool wxGrid::SetModelValues()
4987 // Disable the editor, so it won't hide a changed value.
4988 // Do we also want to save the current value of the editor first?
4990 DisableCellEditControl();
4994 for ( row
= 0; row
< m_numRows
; row
++ )
4996 for ( col
= 0; col
< m_numCols
; col
++ )
4998 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5008 // Note - this function only draws cells that are in the list of
5009 // exposed cells (usually set from the update region by
5010 // CalcExposedCells)
5012 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5014 if ( !m_numRows
|| !m_numCols
)
5017 int i
, numCells
= cells
.GetCount();
5018 int row
, col
, cell_rows
, cell_cols
;
5019 wxGridCellCoordsArray redrawCells
;
5021 for ( i
= numCells
- 1; i
>= 0; i
-- )
5023 row
= cells
[i
].GetRow();
5024 col
= cells
[i
].GetCol();
5025 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5027 // If this cell is part of a multicell block, find owner for repaint
5028 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5030 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5031 bool marked
= false;
5032 for ( int j
= 0; j
< numCells
; j
++ )
5034 if ( cell
== cells
[j
] )
5043 int count
= redrawCells
.GetCount();
5044 for (int j
= 0; j
< count
; j
++)
5046 if ( cell
== redrawCells
[j
] )
5054 redrawCells
.Add( cell
);
5057 // don't bother drawing this cell
5061 // If this cell is empty, find cell to left that might want to overflow
5062 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5064 for ( int l
= 0; l
< cell_rows
; l
++ )
5066 // find a cell in this row to leave already marked for repaint
5068 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5069 if ((redrawCells
[k
].GetCol() < left
) &&
5070 (redrawCells
[k
].GetRow() == row
))
5072 left
= redrawCells
[k
].GetCol();
5076 left
= 0; // oh well
5078 for (int j
= col
- 1; j
>= left
; j
--)
5080 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5082 if (GetCellOverflow(row
+ l
, j
))
5084 wxGridCellCoords
cell(row
+ l
, j
);
5085 bool marked
= false;
5087 for (int k
= 0; k
< numCells
; k
++)
5089 if ( cell
== cells
[k
] )
5098 int count
= redrawCells
.GetCount();
5099 for (int k
= 0; k
< count
; k
++)
5101 if ( cell
== redrawCells
[k
] )
5108 redrawCells
.Add( cell
);
5117 DrawCell( dc
, cells
[i
] );
5120 numCells
= redrawCells
.GetCount();
5122 for ( i
= numCells
- 1; i
>= 0; i
-- )
5124 DrawCell( dc
, redrawCells
[i
] );
5128 void wxGrid::DrawGridSpace( wxDC
& dc
)
5131 m_gridWin
->GetClientSize( &cw
, &ch
);
5134 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5136 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5137 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5139 if ( right
> rightCol
|| bottom
> bottomRow
)
5142 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5144 dc
.SetBrush(GetDefaultCellBackgroundColour());
5145 dc
.SetPen( *wxTRANSPARENT_PEN
);
5147 if ( right
> rightCol
)
5149 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5152 if ( bottom
> bottomRow
)
5154 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5159 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5161 int row
= coords
.GetRow();
5162 int col
= coords
.GetCol();
5164 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5167 // we draw the cell border ourselves
5168 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5170 bool isCurrent
= coords
== m_currentCellCoords
;
5172 wxRect rect
= CellToRect( row
, col
);
5174 // if the editor is shown, we should use it and not the renderer
5175 // Note: However, only if it is really _shown_, i.e. not hidden!
5176 if ( isCurrent
&& IsCellEditControlShown() )
5178 // NB: this "#if..." is temporary and fixes a problem where the
5179 // edit control is erased by this code after being rendered.
5180 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5181 // implicitly, causing this out-of order render.
5182 #if !defined(__WXMAC__)
5183 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5184 editor
->PaintBackground(rect
, attr
);
5190 // but all the rest is drawn by the cell renderer and hence may be customized
5191 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5192 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5199 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5201 // don't show highlight when the grid doesn't have focus
5205 int row
= m_currentCellCoords
.GetRow();
5206 int col
= m_currentCellCoords
.GetCol();
5208 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5211 wxRect rect
= CellToRect(row
, col
);
5213 // hmmm... what could we do here to show that the cell is disabled?
5214 // for now, I just draw a thinner border than for the other ones, but
5215 // it doesn't look really good
5217 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5221 // The center of the drawn line is where the position/width/height of
5222 // the rectangle is actually at (on wxMSW at least), so the
5223 // size of the rectangle is reduced to compensate for the thickness of
5224 // the line. If this is too strange on non-wxMSW platforms then
5225 // please #ifdef this appropriately.
5226 rect
.x
+= penWidth
/ 2;
5227 rect
.y
+= penWidth
/ 2;
5228 rect
.width
-= penWidth
- 1;
5229 rect
.height
-= penWidth
- 1;
5231 // Now draw the rectangle
5232 // use the cellHighlightColour if the cell is inside a selection, this
5233 // will ensure the cell is always visible.
5234 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5235 : m_cellHighlightColour
,
5237 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5238 dc
.DrawRectangle(rect
);
5242 wxPen
wxGrid::GetDefaultGridLinePen()
5244 return wxPen(GetGridLineColour());
5247 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5249 return GetDefaultGridLinePen();
5252 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5254 return GetDefaultGridLinePen();
5257 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5259 int row
= coords
.GetRow();
5260 int col
= coords
.GetCol();
5261 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5265 wxRect rect
= CellToRect( row
, col
);
5267 // right hand border
5268 dc
.SetPen( GetColGridLinePen(col
) );
5269 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5270 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5273 dc
.SetPen( GetRowGridLinePen(row
) );
5274 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5275 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5278 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5280 // This if block was previously in wxGrid::OnPaint but that doesn't
5281 // seem to get called under wxGTK - MB
5283 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5284 m_numRows
&& m_numCols
)
5286 m_currentCellCoords
.Set(0, 0);
5289 if ( IsCellEditControlShown() )
5291 // don't show highlight when the edit control is shown
5295 // if the active cell was repainted, repaint its highlight too because it
5296 // might have been damaged by the grid lines
5297 size_t count
= cells
.GetCount();
5298 for ( size_t n
= 0; n
< count
; n
++ )
5300 wxGridCellCoords cell
= cells
[n
];
5302 // If we are using attributes, then we may have just exposed another
5303 // cell in a partially-visible merged cluster of cells. If the "anchor"
5304 // (upper left) cell of this merged cluster is the cell indicated by
5305 // m_currentCellCoords, then we need to refresh the cell highlight even
5306 // though the "anchor" itself is not part of our update segment.
5307 if ( CanHaveAttributes() )
5311 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5314 cell
.SetRow(cell
.GetRow() + rows
);
5317 cell
.SetCol(cell
.GetCol() + cols
);
5320 if ( cell
== m_currentCellCoords
)
5322 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5323 DrawCellHighlight(dc
, attr
);
5331 // This is used to redraw all grid lines e.g. when the grid line colour
5334 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5336 if ( !m_gridLinesEnabled
)
5339 int top
, bottom
, left
, right
;
5342 m_gridWin
->GetClientSize(&cw
, &ch
);
5343 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5344 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5346 // avoid drawing grid lines past the last row and col
5347 if ( m_gridLinesClipHorz
)
5352 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5353 if ( right
> lastColRight
)
5354 right
= lastColRight
;
5357 if ( m_gridLinesClipVert
)
5362 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5363 if ( bottom
> lastRowBottom
)
5364 bottom
= lastRowBottom
;
5367 // no gridlines inside multicells, clip them out
5368 int leftCol
= GetColPos( internalXToCol(left
) );
5369 int topRow
= internalYToRow(top
);
5370 int rightCol
= GetColPos( internalXToCol(right
) );
5371 int bottomRow
= internalYToRow(bottom
);
5373 wxRegion
clippedcells(0, 0, cw
, ch
);
5375 int cell_rows
, cell_cols
;
5378 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5380 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5382 int i
= GetColAt( colPos
);
5384 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5385 if ((cell_rows
> 1) || (cell_cols
> 1))
5387 rect
= CellToRect(j
,i
);
5388 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5389 clippedcells
.Subtract(rect
);
5391 else if ((cell_rows
< 0) || (cell_cols
< 0))
5393 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5394 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5395 clippedcells
.Subtract(rect
);
5400 dc
.SetDeviceClippingRegion( clippedcells
);
5403 // horizontal grid lines
5404 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5406 int bot
= GetRowBottom(i
) - 1;
5413 dc
.SetPen( GetRowGridLinePen(i
) );
5414 dc
.DrawLine( left
, bot
, right
, bot
);
5418 // vertical grid lines
5419 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5421 int i
= GetColAt( colPos
);
5423 int colRight
= GetColRight(i
);
5425 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5429 if ( colRight
> right
)
5432 if ( colRight
>= left
)
5434 dc
.SetPen( GetColGridLinePen(i
) );
5435 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5439 dc
.DestroyClippingRegion();
5442 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5447 const size_t numLabels
= rows
.GetCount();
5448 for ( size_t i
= 0; i
< numLabels
; i
++ )
5450 DrawRowLabel( dc
, rows
[i
] );
5454 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5456 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5459 wxGridCellAttrProvider
* const
5460 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5462 // notice that an explicit static_cast is needed to avoid a compilation
5463 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5464 // wxGridRowHeaderRenderer class without it
5465 const wxGridRowHeaderRenderer
&
5466 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5467 : static_cast<const wxGridRowHeaderRenderer
&>
5468 (gs_defaultHeaderRenderers
.rowRenderer
);
5470 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5471 rend
.DrawBorder(*this, dc
, rect
);
5474 GetRowLabelAlignment(&hAlign
, &vAlign
);
5476 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5477 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5480 void wxGrid::UseNativeColHeader(bool native
)
5482 if ( native
== m_useNativeHeader
)
5486 m_useNativeHeader
= native
;
5488 CreateColumnWindow();
5490 if ( m_useNativeHeader
)
5491 GetGridColHeader()->SetColumnCount(m_numCols
);
5495 void wxGrid::SetUseNativeColLabels( bool native
)
5497 wxASSERT_MSG( !m_useNativeHeader
,
5498 "doesn't make sense when using native header" );
5500 m_nativeColumnLabels
= native
;
5503 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5504 SetColLabelSize( height
);
5507 GetColLabelWindow()->Refresh();
5508 m_cornerLabelWin
->Refresh();
5511 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5516 const size_t numLabels
= cols
.GetCount();
5517 for ( size_t i
= 0; i
< numLabels
; i
++ )
5519 DrawColLabel( dc
, cols
[i
] );
5523 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5525 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5527 if ( m_nativeColumnLabels
)
5531 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5538 wxGridCellAttrProvider
* const
5539 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5540 const wxGridCornerHeaderRenderer
&
5541 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5542 : static_cast<wxGridCornerHeaderRenderer
&>
5543 (gs_defaultHeaderRenderers
.cornerRenderer
);
5545 rend
.DrawBorder(*this, dc
, rect
);
5549 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5551 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5554 int colLeft
= GetColLeft(col
);
5556 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5557 wxGridCellAttrProvider
* const
5558 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5559 const wxGridColumnHeaderRenderer
&
5560 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
5561 : static_cast<wxGridColumnHeaderRenderer
&>
5562 (gs_defaultHeaderRenderers
.colRenderer
);
5564 if ( m_nativeColumnLabels
)
5566 wxRendererNative::Get().DrawHeaderButton
5568 GetColLabelWindow(),
5573 ? IsSortOrderAscending()
5574 ? wxHDR_SORT_ICON_UP
5575 : wxHDR_SORT_ICON_DOWN
5576 : wxHDR_SORT_ICON_NONE
5582 // It is reported that we need to erase the background to avoid display
5583 // artefacts, see #12055.
5584 wxDCBrushChanger
setBrush(dc
, m_colWindow
->GetBackgroundColour());
5585 dc
.DrawRectangle(rect
);
5587 rend
.DrawBorder(*this, dc
, rect
);
5591 GetColLabelAlignment(&hAlign
, &vAlign
);
5592 const int orient
= GetColLabelTextOrientation();
5594 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5597 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5598 // we just have to add textOrientation support
5599 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5600 const wxString
& value
,
5604 int textOrientation
) const
5606 wxArrayString lines
;
5608 StringToLines( value
, lines
);
5610 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5613 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5614 const wxArrayString
& lines
,
5618 int textOrientation
) const
5620 if ( lines
.empty() )
5623 wxDCClipper
clip(dc
, rect
);
5628 if ( textOrientation
== wxHORIZONTAL
)
5629 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5631 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5635 switch ( vertAlign
)
5637 case wxALIGN_BOTTOM
:
5638 if ( textOrientation
== wxHORIZONTAL
)
5639 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5641 x
= rect
.x
+ rect
.width
- textWidth
;
5644 case wxALIGN_CENTRE
:
5645 if ( textOrientation
== wxHORIZONTAL
)
5646 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5648 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5653 if ( textOrientation
== wxHORIZONTAL
)
5660 // Align each line of a multi-line label
5661 size_t nLines
= lines
.GetCount();
5662 for ( size_t l
= 0; l
< nLines
; l
++ )
5664 const wxString
& line
= lines
[l
];
5668 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5672 wxCoord lineWidth
= 0,
5674 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5676 switch ( horizAlign
)
5679 if ( textOrientation
== wxHORIZONTAL
)
5680 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5682 y
= rect
.y
+ lineWidth
+ 1;
5685 case wxALIGN_CENTRE
:
5686 if ( textOrientation
== wxHORIZONTAL
)
5687 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5689 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5694 if ( textOrientation
== wxHORIZONTAL
)
5697 y
= rect
.y
+ rect
.height
- 1;
5701 if ( textOrientation
== wxHORIZONTAL
)
5703 dc
.DrawText( line
, x
, y
);
5708 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5714 // Split multi-line text up into an array of strings.
5715 // Any existing contents of the string array are preserved.
5717 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5718 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5722 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5723 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5725 while ( startPos
< (int)tVal
.length() )
5727 pos
= tVal
.Mid(startPos
).Find( eol
);
5732 else if ( pos
== 0 )
5734 lines
.Add( wxEmptyString
);
5738 lines
.Add( tVal
.Mid(startPos
, pos
) );
5741 startPos
+= pos
+ 1;
5744 if ( startPos
< (int)tVal
.length() )
5746 lines
.Add( tVal
.Mid( startPos
) );
5750 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5751 const wxArrayString
& lines
,
5752 long *width
, long *height
) const
5756 wxCoord lineW
= 0, lineH
= 0;
5759 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5761 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5762 w
= wxMax( w
, lineW
);
5771 // ------ Batch processing.
5773 void wxGrid::EndBatch()
5775 if ( m_batchCount
> 0 )
5778 if ( !m_batchCount
)
5781 m_rowLabelWin
->Refresh();
5782 m_colWindow
->Refresh();
5783 m_cornerLabelWin
->Refresh();
5784 m_gridWin
->Refresh();
5789 // Use this, rather than wxWindow::Refresh(), to force an immediate
5790 // repainting of the grid. Has no effect if you are already inside a
5791 // BeginBatch / EndBatch block.
5793 void wxGrid::ForceRefresh()
5799 bool wxGrid::Enable(bool enable
)
5801 if ( !wxScrolledWindow::Enable(enable
) )
5804 // redraw in the new state
5805 m_gridWin
->Refresh();
5811 // ------ Edit control functions
5814 void wxGrid::EnableEditing( bool edit
)
5816 if ( edit
!= m_editable
)
5819 EnableCellEditControl(edit
);
5824 void wxGrid::EnableCellEditControl( bool enable
)
5829 if ( enable
!= m_cellEditCtrlEnabled
)
5833 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5836 // this should be checked by the caller!
5837 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5839 // do it before ShowCellEditControl()
5840 m_cellEditCtrlEnabled
= enable
;
5842 ShowCellEditControl();
5846 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5848 HideCellEditControl();
5849 SaveEditControlValue();
5851 // do it after HideCellEditControl()
5852 m_cellEditCtrlEnabled
= enable
;
5857 bool wxGrid::IsCurrentCellReadOnly() const
5860 attr
= const_cast<wxGrid
*>(this)->GetCellAttr(m_currentCellCoords
);
5861 bool readonly
= attr
->IsReadOnly();
5867 bool wxGrid::CanEnableCellControl() const
5869 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5870 !IsCurrentCellReadOnly();
5873 bool wxGrid::IsCellEditControlEnabled() const
5875 // the cell edit control might be disable for all cells or just for the
5876 // current one if it's read only
5877 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5880 bool wxGrid::IsCellEditControlShown() const
5882 bool isShown
= false;
5884 if ( m_cellEditCtrlEnabled
)
5886 int row
= m_currentCellCoords
.GetRow();
5887 int col
= m_currentCellCoords
.GetCol();
5888 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5889 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5894 if ( editor
->IsCreated() )
5896 isShown
= editor
->GetControl()->IsShown();
5906 void wxGrid::ShowCellEditControl()
5908 if ( IsCellEditControlEnabled() )
5910 if ( !IsVisible( m_currentCellCoords
, false ) )
5912 m_cellEditCtrlEnabled
= false;
5917 wxRect rect
= CellToRect( m_currentCellCoords
);
5918 int row
= m_currentCellCoords
.GetRow();
5919 int col
= m_currentCellCoords
.GetCol();
5921 // if this is part of a multicell, find owner (topleft)
5922 int cell_rows
, cell_cols
;
5923 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5924 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5928 m_currentCellCoords
.SetRow( row
);
5929 m_currentCellCoords
.SetCol( col
);
5932 // erase the highlight and the cell contents because the editor
5933 // might not cover the entire cell
5934 wxClientDC
dc( m_gridWin
);
5936 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5937 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5938 dc
.SetPen(*wxTRANSPARENT_PEN
);
5939 dc
.DrawRectangle(rect
);
5941 // convert to scrolled coords
5942 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5948 // cell is shifted by one pixel
5949 // However, don't allow x or y to become negative
5950 // since the SetSize() method interprets that as
5957 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5958 if ( !editor
->IsCreated() )
5960 editor
->Create(m_gridWin
, wxID_ANY
,
5961 new wxGridCellEditorEvtHandler(this, editor
));
5963 wxGridEditorCreatedEvent
evt(GetId(),
5964 wxEVT_GRID_EDITOR_CREATED
,
5968 editor
->GetControl());
5969 GetEventHandler()->ProcessEvent(evt
);
5972 // resize editor to overflow into righthand cells if allowed
5973 int maxWidth
= rect
.width
;
5974 wxString value
= GetCellValue(row
, col
);
5975 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
5978 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
5979 if (maxWidth
< rect
.width
)
5980 maxWidth
= rect
.width
;
5983 int client_right
= m_gridWin
->GetClientSize().GetWidth();
5984 if (rect
.x
+ maxWidth
> client_right
)
5985 maxWidth
= client_right
- rect
.x
;
5987 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
5989 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5990 // may have changed earlier
5991 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
5994 GetCellSize( row
, i
, &c_rows
, &c_cols
);
5996 // looks weird going over a multicell
5997 if (m_table
->IsEmptyCell( row
, i
) &&
5998 (rect
.width
< maxWidth
) && (c_rows
== 1))
6000 rect
.width
+= GetColWidth( i
);
6006 if (rect
.GetRight() > client_right
)
6007 rect
.SetRight( client_right
- 1 );
6010 editor
->SetCellAttr( attr
);
6011 editor
->SetSize( rect
);
6013 editor
->GetControl()->Move(
6014 editor
->GetControl()->GetPosition().x
+ nXMove
,
6015 editor
->GetControl()->GetPosition().y
);
6016 editor
->Show( true, attr
);
6018 // recalc dimensions in case we need to
6019 // expand the scrolled window to account for editor
6022 editor
->BeginEdit(row
, col
, this);
6023 editor
->SetCellAttr(NULL
);
6031 void wxGrid::HideCellEditControl()
6033 if ( IsCellEditControlEnabled() )
6035 int row
= m_currentCellCoords
.GetRow();
6036 int col
= m_currentCellCoords
.GetCol();
6038 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6039 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6040 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6041 editor
->Show( false );
6045 // return the focus to the grid itself if the editor had it
6047 // note that we must not do this unconditionally to avoid stealing
6048 // focus from the window which just received it if we are hiding the
6049 // editor precisely because we lost focus
6050 if ( editorHadFocus
)
6051 m_gridWin
->SetFocus();
6053 // refresh whole row to the right
6054 wxRect
rect( CellToRect(row
, col
) );
6055 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6056 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6059 // ensure that the pixels under the focus ring get refreshed as well
6060 rect
.Inflate(10, 10);
6063 m_gridWin
->Refresh( false, &rect
);
6067 void wxGrid::SaveEditControlValue()
6069 if ( IsCellEditControlEnabled() )
6071 int row
= m_currentCellCoords
.GetRow();
6072 int col
= m_currentCellCoords
.GetCol();
6074 wxString oldval
= GetCellValue(row
, col
);
6076 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6077 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6080 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6082 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6084 editor
->ApplyEdit(row
, col
, this);
6086 // for compatibility reasons dating back to wx 2.8 when this event
6087 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6088 // didn't exist we allow vetoing this one too
6089 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6091 // Event has been vetoed, set the data back.
6092 SetCellValue(row
, col
, oldval
);
6102 // ------ Grid location functions
6103 // Note that all of these functions work with the logical coordinates of
6104 // grid cells and labels so you will need to convert from device
6105 // coordinates for mouse events etc.
6108 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6110 int row
= YToRow(y
);
6111 int col
= XToCol(x
);
6113 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6114 : wxGridCellCoords(row
, col
);
6117 // compute row or column from some (unscrolled) coordinate value, using either
6118 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6119 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6121 int wxGrid::PosToLinePos(int coord
,
6123 const wxGridOperations
& oper
) const
6125 const int numLines
= oper
.GetNumberOfLines(this);
6128 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6130 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6131 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6133 int maxPos
= coord
/ defaultLineSize
,
6136 // check for the simplest case: if we have no explicit line sizes
6137 // configured, then we already know the line this position falls in
6138 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6139 if ( lineEnds
.empty() )
6141 if ( maxPos
< numLines
)
6144 return clipToMinMax
? numLines
- 1 : -1;
6148 // adjust maxPos before starting the binary search
6149 if ( maxPos
>= numLines
)
6151 maxPos
= numLines
- 1;
6155 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6158 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6160 maxPos
= coord
/ minDist
;
6162 maxPos
= numLines
- 1;
6165 if ( maxPos
>= numLines
)
6166 maxPos
= numLines
- 1;
6169 // check if the position is beyond the last column
6170 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6171 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6172 return clipToMinMax
? maxPos
: -1;
6174 // or before the first one
6175 const int lineAt0
= oper
.GetLineAt(this, 0);
6176 if ( coord
< lineEnds
[lineAt0
] )
6180 // finally do perform the binary search
6181 while ( minPos
< maxPos
)
6183 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6184 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6186 "wxGrid: internal error in PosToLinePos()" );
6188 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6193 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6194 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6204 wxGrid::PosToLine(int coord
,
6206 const wxGridOperations
& oper
) const
6208 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6210 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6213 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6215 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6218 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6220 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6223 int wxGrid::XToPos(int x
) const
6225 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6228 // return the row number such that the y coord is near the edge of, or -1 if
6229 // not near an edge.
6231 // notice that position can only possibly be near an edge if the row/column is
6232 // large enough to still allow for an "inner" area that is _not_ near the edge
6233 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6234 // _never_ be considered to be near the edge).
6235 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6237 const int line
= oper
.PosToLine(this, pos
, true);
6239 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6241 // We know that we are in this line, test whether we are close enough
6242 // to start or end border, respectively.
6243 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6245 else if ( line
> 0 &&
6246 pos
- oper
.GetLineStartPos(this,
6247 line
) < WXGRID_LABEL_EDGE_ZONE
)
6249 return oper
.GetLineBefore(this, line
);
6256 int wxGrid::YToEdgeOfRow(int y
) const
6258 return PosToEdgeOfLine(y
, wxGridRowOperations());
6261 int wxGrid::XToEdgeOfCol(int x
) const
6263 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6266 wxRect
wxGrid::CellToRect( int row
, int col
) const
6268 wxRect
rect( -1, -1, -1, -1 );
6270 if ( row
>= 0 && row
< m_numRows
&&
6271 col
>= 0 && col
< m_numCols
)
6273 int i
, cell_rows
, cell_cols
;
6274 rect
.width
= rect
.height
= 0;
6275 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6276 // if negative then find multicell owner
6281 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6283 rect
.x
= GetColLeft(col
);
6284 rect
.y
= GetRowTop(row
);
6285 for (i
=col
; i
< col
+ cell_cols
; i
++)
6286 rect
.width
+= GetColWidth(i
);
6287 for (i
=row
; i
< row
+ cell_rows
; i
++)
6288 rect
.height
+= GetRowHeight(i
);
6290 // if grid lines are enabled, then the area of the cell is a bit smaller
6291 if (m_gridLinesEnabled
)
6301 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6303 // get the cell rectangle in logical coords
6305 wxRect
r( CellToRect( row
, col
) );
6307 // convert to device coords
6309 int left
, top
, right
, bottom
;
6310 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6311 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6313 // check against the client area of the grid window
6315 m_gridWin
->GetClientSize( &cw
, &ch
);
6317 if ( wholeCellVisible
)
6319 // is the cell wholly visible ?
6320 return ( left
>= 0 && right
<= cw
&&
6321 top
>= 0 && bottom
<= ch
);
6325 // is the cell partly visible ?
6327 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6328 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6332 // make the specified cell location visible by doing a minimal amount
6335 void wxGrid::MakeCellVisible( int row
, int col
)
6338 int xpos
= -1, ypos
= -1;
6340 if ( row
>= 0 && row
< m_numRows
&&
6341 col
>= 0 && col
< m_numCols
)
6343 // get the cell rectangle in logical coords
6344 wxRect
r( CellToRect( row
, col
) );
6346 // convert to device coords
6347 int left
, top
, right
, bottom
;
6348 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6349 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6352 m_gridWin
->GetClientSize( &cw
, &ch
);
6358 else if ( bottom
> ch
)
6360 int h
= r
.GetHeight();
6362 for ( i
= row
- 1; i
>= 0; i
-- )
6364 int rowHeight
= GetRowHeight(i
);
6365 if ( h
+ rowHeight
> ch
)
6372 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6373 // have rounding errors (this is important, because if we do,
6374 // we might not scroll at all and some cells won't be redrawn)
6376 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6377 // so just add a full scroll unit...
6378 ypos
+= m_yScrollPixelsPerLine
;
6381 // special handling for wide cells - show always left part of the cell!
6382 // Otherwise, e.g. when stepping from row to row, it would jump between
6383 // left and right part of the cell on every step!
6385 if ( left
< 0 || (right
- left
) >= cw
)
6389 else if ( right
> cw
)
6391 // position the view so that the cell is on the right
6393 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6394 xpos
= x0
+ (right
- cw
);
6396 // see comment for ypos above
6397 xpos
+= m_xScrollPixelsPerLine
;
6400 if ( xpos
!= -1 || ypos
!= -1 )
6403 xpos
/= m_xScrollPixelsPerLine
;
6405 ypos
/= m_yScrollPixelsPerLine
;
6406 Scroll( xpos
, ypos
);
6413 // ------ Grid cursor movement functions
6417 wxGrid::DoMoveCursor(bool expandSelection
,
6418 const wxGridDirectionOperations
& diroper
)
6420 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6423 if ( expandSelection
)
6425 wxGridCellCoords coords
= m_selectedBlockCorner
;
6426 if ( coords
== wxGridNoCellCoords
)
6427 coords
= m_currentCellCoords
;
6429 if ( diroper
.IsAtBoundary(coords
) )
6432 diroper
.Advance(coords
);
6434 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6436 else // don't expand selection
6440 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6443 wxGridCellCoords coords
= m_currentCellCoords
;
6444 diroper
.Advance(coords
);
6452 bool wxGrid::MoveCursorUp(bool expandSelection
)
6454 return DoMoveCursor(expandSelection
,
6455 wxGridBackwardOperations(this, wxGridRowOperations()));
6458 bool wxGrid::MoveCursorDown(bool expandSelection
)
6460 return DoMoveCursor(expandSelection
,
6461 wxGridForwardOperations(this, wxGridRowOperations()));
6464 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6466 return DoMoveCursor(expandSelection
,
6467 wxGridBackwardOperations(this, wxGridColumnOperations()));
6470 bool wxGrid::MoveCursorRight(bool expandSelection
)
6472 return DoMoveCursor(expandSelection
,
6473 wxGridForwardOperations(this, wxGridColumnOperations()));
6476 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6478 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6481 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6484 const int oldRow
= m_currentCellCoords
.GetRow();
6485 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6486 if ( newRow
== oldRow
)
6488 wxGridCellCoords
coords(m_currentCellCoords
);
6489 diroper
.Advance(coords
);
6490 newRow
= coords
.GetRow();
6493 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6498 bool wxGrid::MovePageUp()
6500 return DoMoveCursorByPage(
6501 wxGridBackwardOperations(this, wxGridRowOperations()));
6504 bool wxGrid::MovePageDown()
6506 return DoMoveCursorByPage(
6507 wxGridForwardOperations(this, wxGridRowOperations()));
6510 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6511 // until we find a non-empty cell or reach the grid end
6513 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6514 const wxGridDirectionOperations
& diroper
)
6516 while ( !diroper
.IsAtBoundary(coords
) )
6518 diroper
.Advance(coords
);
6519 if ( !m_table
->IsEmpty(coords
) )
6525 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6526 const wxGridDirectionOperations
& diroper
)
6528 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6531 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6534 wxGridCellCoords
coords(m_currentCellCoords
);
6535 if ( m_table
->IsEmpty(coords
) )
6537 // we are in an empty cell: find the next block of non-empty cells
6538 AdvanceToNextNonEmpty(coords
, diroper
);
6540 else // current cell is not empty
6542 diroper
.Advance(coords
);
6543 if ( m_table
->IsEmpty(coords
) )
6545 // we started at the end of a block, find the next one
6546 AdvanceToNextNonEmpty(coords
, diroper
);
6548 else // we're in a middle of a block
6550 // go to the end of it, i.e. find the last cell before the next
6552 while ( !diroper
.IsAtBoundary(coords
) )
6554 wxGridCellCoords
coordsNext(coords
);
6555 diroper
.Advance(coordsNext
);
6556 if ( m_table
->IsEmpty(coordsNext
) )
6559 coords
= coordsNext
;
6564 if ( expandSelection
)
6566 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6577 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6579 return DoMoveCursorByBlock(
6581 wxGridBackwardOperations(this, wxGridRowOperations())
6585 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6587 return DoMoveCursorByBlock(
6589 wxGridForwardOperations(this, wxGridRowOperations())
6593 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6595 return DoMoveCursorByBlock(
6597 wxGridBackwardOperations(this, wxGridColumnOperations())
6601 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6603 return DoMoveCursorByBlock(
6605 wxGridForwardOperations(this, wxGridColumnOperations())
6610 // ------ Label values and formatting
6613 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6616 *horiz
= m_rowLabelHorizAlign
;
6618 *vert
= m_rowLabelVertAlign
;
6621 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6624 *horiz
= m_colLabelHorizAlign
;
6626 *vert
= m_colLabelVertAlign
;
6629 int wxGrid::GetColLabelTextOrientation() const
6631 return m_colLabelTextOrientation
;
6634 wxString
wxGrid::GetRowLabelValue( int row
) const
6638 return m_table
->GetRowLabelValue( row
);
6648 wxString
wxGrid::GetColLabelValue( int col
) const
6652 return m_table
->GetColLabelValue( col
);
6662 void wxGrid::SetRowLabelSize( int width
)
6664 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6666 if ( width
== wxGRID_AUTOSIZE
)
6668 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6671 if ( width
!= m_rowLabelWidth
)
6675 m_rowLabelWin
->Show( false );
6676 m_cornerLabelWin
->Show( false );
6678 else if ( m_rowLabelWidth
== 0 )
6680 m_rowLabelWin
->Show( true );
6681 if ( m_colLabelHeight
> 0 )
6682 m_cornerLabelWin
->Show( true );
6685 m_rowLabelWidth
= width
;
6687 wxScrolledWindow::Refresh( true );
6691 void wxGrid::SetColLabelSize( int height
)
6693 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6695 if ( height
== wxGRID_AUTOSIZE
)
6697 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6700 if ( height
!= m_colLabelHeight
)
6704 m_colWindow
->Show( false );
6705 m_cornerLabelWin
->Show( false );
6707 else if ( m_colLabelHeight
== 0 )
6709 m_colWindow
->Show( true );
6710 if ( m_rowLabelWidth
> 0 )
6711 m_cornerLabelWin
->Show( true );
6714 m_colLabelHeight
= height
;
6716 wxScrolledWindow::Refresh( true );
6720 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6722 if ( m_labelBackgroundColour
!= colour
)
6724 m_labelBackgroundColour
= colour
;
6725 m_rowLabelWin
->SetBackgroundColour( colour
);
6726 m_colWindow
->SetBackgroundColour( colour
);
6727 m_cornerLabelWin
->SetBackgroundColour( colour
);
6729 if ( !GetBatchCount() )
6731 m_rowLabelWin
->Refresh();
6732 m_colWindow
->Refresh();
6733 m_cornerLabelWin
->Refresh();
6738 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6740 if ( m_labelTextColour
!= colour
)
6742 m_labelTextColour
= colour
;
6743 if ( !GetBatchCount() )
6745 m_rowLabelWin
->Refresh();
6746 m_colWindow
->Refresh();
6751 void wxGrid::SetLabelFont( const wxFont
& font
)
6754 if ( !GetBatchCount() )
6756 m_rowLabelWin
->Refresh();
6757 m_colWindow
->Refresh();
6761 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6763 // allow old (incorrect) defs to be used
6766 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6767 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6768 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6773 case wxTOP
: vert
= wxALIGN_TOP
; break;
6774 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6775 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6778 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6780 m_rowLabelHorizAlign
= horiz
;
6783 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6785 m_rowLabelVertAlign
= vert
;
6788 if ( !GetBatchCount() )
6790 m_rowLabelWin
->Refresh();
6794 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6796 // allow old (incorrect) defs to be used
6799 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6800 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6801 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6806 case wxTOP
: vert
= wxALIGN_TOP
; break;
6807 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6808 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6811 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6813 m_colLabelHorizAlign
= horiz
;
6816 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6818 m_colLabelVertAlign
= vert
;
6821 if ( !GetBatchCount() )
6823 m_colWindow
->Refresh();
6827 // Note: under MSW, the default column label font must be changed because it
6828 // does not support vertical printing
6830 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6831 // pGrid->SetLabelFont(font);
6832 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6834 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6836 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6837 m_colLabelTextOrientation
= textOrientation
;
6839 if ( !GetBatchCount() )
6840 m_colWindow
->Refresh();
6843 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6847 m_table
->SetRowLabelValue( row
, s
);
6848 if ( !GetBatchCount() )
6850 wxRect rect
= CellToRect( row
, 0 );
6851 if ( rect
.height
> 0 )
6853 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6855 rect
.width
= m_rowLabelWidth
;
6856 m_rowLabelWin
->Refresh( true, &rect
);
6862 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6866 m_table
->SetColLabelValue( col
, s
);
6867 if ( !GetBatchCount() )
6869 if ( m_useNativeHeader
)
6871 GetGridColHeader()->UpdateColumn(col
);
6875 wxRect rect
= CellToRect( 0, col
);
6876 if ( rect
.width
> 0 )
6878 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6880 rect
.height
= m_colLabelHeight
;
6881 GetColLabelWindow()->Refresh( true, &rect
);
6888 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6890 if ( m_gridLineColour
!= colour
)
6892 m_gridLineColour
= colour
;
6894 if ( GridLinesEnabled() )
6899 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6901 if ( m_cellHighlightColour
!= colour
)
6903 m_cellHighlightColour
= colour
;
6905 wxClientDC
dc( m_gridWin
);
6907 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6908 DrawCellHighlight(dc
, attr
);
6913 void wxGrid::SetCellHighlightPenWidth(int width
)
6915 if (m_cellHighlightPenWidth
!= width
)
6917 m_cellHighlightPenWidth
= width
;
6919 // Just redrawing the cell highlight is not enough since that won't
6920 // make any visible change if the thickness is getting smaller.
6921 int row
= m_currentCellCoords
.GetRow();
6922 int col
= m_currentCellCoords
.GetCol();
6923 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6926 wxRect rect
= CellToRect(row
, col
);
6927 m_gridWin
->Refresh(true, &rect
);
6931 void wxGrid::SetCellHighlightROPenWidth(int width
)
6933 if (m_cellHighlightROPenWidth
!= width
)
6935 m_cellHighlightROPenWidth
= width
;
6937 // Just redrawing the cell highlight is not enough since that won't
6938 // make any visible change if the thickness is getting smaller.
6939 int row
= m_currentCellCoords
.GetRow();
6940 int col
= m_currentCellCoords
.GetCol();
6941 if ( row
== -1 || col
== -1 ||
6942 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6945 wxRect rect
= CellToRect(row
, col
);
6946 m_gridWin
->Refresh(true, &rect
);
6950 void wxGrid::RedrawGridLines()
6952 // the lines will be redrawn when the window is thawn
6953 if ( GetBatchCount() )
6956 if ( GridLinesEnabled() )
6958 wxClientDC
dc( m_gridWin
);
6960 DrawAllGridLines( dc
, wxRegion() );
6962 else // remove the grid lines
6964 m_gridWin
->Refresh();
6968 void wxGrid::EnableGridLines( bool enable
)
6970 if ( enable
!= m_gridLinesEnabled
)
6972 m_gridLinesEnabled
= enable
;
6978 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
6984 if ( GridLinesEnabled() )
6989 int wxGrid::GetDefaultRowSize() const
6991 return m_defaultRowHeight
;
6994 int wxGrid::GetRowSize( int row
) const
6996 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
6998 return GetRowHeight(row
);
7001 int wxGrid::GetDefaultColSize() const
7003 return m_defaultColWidth
;
7006 int wxGrid::GetColSize( int col
) const
7008 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
7010 return GetColWidth(col
);
7013 // ============================================================================
7014 // access to the grid attributes: each of them has a default value in the grid
7015 // itself and may be overidden on a per-cell basis
7016 // ============================================================================
7018 // ----------------------------------------------------------------------------
7019 // setting default attributes
7020 // ----------------------------------------------------------------------------
7022 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7024 m_defaultCellAttr
->SetBackgroundColour(col
);
7026 m_gridWin
->SetBackgroundColour(col
);
7030 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7032 m_defaultCellAttr
->SetTextColour(col
);
7035 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7037 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7040 void wxGrid::SetDefaultCellOverflow( bool allow
)
7042 m_defaultCellAttr
->SetOverflow(allow
);
7045 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7047 m_defaultCellAttr
->SetFont(font
);
7050 // For editors and renderers the type registry takes precedence over the
7051 // default attr, so we need to register the new editor/renderer for the string
7052 // data type in order to make setting a default editor/renderer appear to
7055 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7057 RegisterDataType(wxGRID_VALUE_STRING
,
7059 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7062 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7064 RegisterDataType(wxGRID_VALUE_STRING
,
7065 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7069 // ----------------------------------------------------------------------------
7070 // access to the default attributes
7071 // ----------------------------------------------------------------------------
7073 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7075 return m_defaultCellAttr
->GetBackgroundColour();
7078 wxColour
wxGrid::GetDefaultCellTextColour() const
7080 return m_defaultCellAttr
->GetTextColour();
7083 wxFont
wxGrid::GetDefaultCellFont() const
7085 return m_defaultCellAttr
->GetFont();
7088 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7090 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7093 bool wxGrid::GetDefaultCellOverflow() const
7095 return m_defaultCellAttr
->GetOverflow();
7098 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7100 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7103 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7105 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7108 // ----------------------------------------------------------------------------
7109 // access to cell attributes
7110 // ----------------------------------------------------------------------------
7112 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7114 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7115 wxColour colour
= attr
->GetBackgroundColour();
7121 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7123 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7124 wxColour colour
= attr
->GetTextColour();
7130 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7132 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7133 wxFont font
= attr
->GetFont();
7139 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7141 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7142 attr
->GetAlignment(horiz
, vert
);
7146 bool wxGrid::GetCellOverflow( int row
, int col
) const
7148 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7149 bool allow
= attr
->GetOverflow();
7156 wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7158 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7159 attr
->GetSize( num_rows
, num_cols
);
7162 if ( *num_rows
== 1 && *num_cols
== 1 )
7163 return CellSpan_None
; // just a normal cell
7165 if ( *num_rows
< 0 || *num_cols
< 0 )
7166 return CellSpan_Inside
; // covered by a multi-span cell
7168 // this cell spans multiple cells to its right/bottom
7169 return CellSpan_Main
;
7172 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7174 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7175 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7181 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7183 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7184 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7190 bool wxGrid::IsReadOnly(int row
, int col
) const
7192 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7193 bool isReadOnly
= attr
->IsReadOnly();
7199 // ----------------------------------------------------------------------------
7200 // attribute support: cache, automatic provider creation, ...
7201 // ----------------------------------------------------------------------------
7203 bool wxGrid::CanHaveAttributes() const
7210 return m_table
->CanHaveAttributes();
7213 void wxGrid::ClearAttrCache()
7215 if ( m_attrCache
.row
!= -1 )
7217 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7218 m_attrCache
.attr
= NULL
;
7219 m_attrCache
.row
= -1;
7220 // wxSafeDecRec(...) might cause event processing that accesses
7221 // the cached attribute, if one exists (e.g. by deleting the
7222 // editor stored within the attribute). Therefore it is important
7223 // to invalidate the cache before calling wxSafeDecRef!
7224 wxSafeDecRef(oldAttr
);
7228 void wxGrid::RefreshAttr(int row
, int col
)
7230 if ( m_attrCache
.row
== row
&& m_attrCache
.col
== col
)
7235 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7239 wxGrid
* const self
= const_cast<wxGrid
*>(this);
7241 self
->ClearAttrCache();
7242 self
->m_attrCache
.row
= row
;
7243 self
->m_attrCache
.col
= col
;
7244 self
->m_attrCache
.attr
= attr
;
7249 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7251 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7253 *attr
= m_attrCache
.attr
;
7254 wxSafeIncRef(m_attrCache
.attr
);
7256 #ifdef DEBUG_ATTR_CACHE
7257 gs_nAttrCacheHits
++;
7264 #ifdef DEBUG_ATTR_CACHE
7265 gs_nAttrCacheMisses
++;
7272 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7274 wxGridCellAttr
*attr
= NULL
;
7275 // Additional test to avoid looking at the cache e.g. for
7276 // wxNoCellCoords, as this will confuse memory management.
7279 if ( !LookupAttr(row
, col
, &attr
) )
7281 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7283 CacheAttr(row
, col
, attr
);
7289 attr
->SetDefAttr(m_defaultCellAttr
);
7293 attr
= m_defaultCellAttr
;
7300 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7302 wxGridCellAttr
*attr
= NULL
;
7303 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7305 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7306 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7308 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7311 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7313 // artificially inc the ref count to match DecRef() in caller
7315 m_table
->SetAttr(attr
, row
, col
);
7321 // ----------------------------------------------------------------------------
7322 // setting column attributes (wrappers around SetColAttr)
7323 // ----------------------------------------------------------------------------
7325 void wxGrid::SetColFormatBool(int col
)
7327 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7330 void wxGrid::SetColFormatNumber(int col
)
7332 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7335 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7337 wxString typeName
= wxGRID_VALUE_FLOAT
;
7338 if ( (width
!= -1) || (precision
!= -1) )
7340 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7343 SetColFormatCustom(col
, typeName
);
7346 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7348 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7350 attr
= new wxGridCellAttr
;
7351 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7352 attr
->SetRenderer(renderer
);
7353 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7354 attr
->SetEditor(editor
);
7356 SetColAttr(col
, attr
);
7360 // ----------------------------------------------------------------------------
7361 // setting cell attributes: this is forwarded to the table
7362 // ----------------------------------------------------------------------------
7364 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7366 if ( CanHaveAttributes() )
7368 m_table
->SetAttr(attr
, row
, col
);
7377 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7379 if ( CanHaveAttributes() )
7381 m_table
->SetRowAttr(attr
, row
);
7390 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7392 if ( CanHaveAttributes() )
7394 m_table
->SetColAttr(attr
, col
);
7403 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7405 if ( CanHaveAttributes() )
7407 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7408 attr
->SetBackgroundColour(colour
);
7413 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7415 if ( CanHaveAttributes() )
7417 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7418 attr
->SetTextColour(colour
);
7423 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7425 if ( CanHaveAttributes() )
7427 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7428 attr
->SetFont(font
);
7433 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7435 if ( CanHaveAttributes() )
7437 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7438 attr
->SetAlignment(horiz
, vert
);
7443 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7445 if ( CanHaveAttributes() )
7447 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7448 attr
->SetOverflow(allow
);
7453 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7455 if ( CanHaveAttributes() )
7457 int cell_rows
, cell_cols
;
7459 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7460 attr
->GetSize(&cell_rows
, &cell_cols
);
7461 attr
->SetSize(num_rows
, num_cols
);
7464 // Cannot set the size of a cell to 0 or negative values
7465 // While it is perfectly legal to do that, this function cannot
7466 // handle all the possibilies, do it by hand by getting the CellAttr.
7467 // You can only set the size of a cell to 1,1 or greater with this fn
7468 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7469 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7470 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7471 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7473 // if this was already a multicell then "turn off" the other cells first
7474 if ((cell_rows
> 1) || (cell_cols
> 1))
7477 for (j
=row
; j
< row
+ cell_rows
; j
++)
7479 for (i
=col
; i
< col
+ cell_cols
; i
++)
7481 if ((i
!= col
) || (j
!= row
))
7483 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7484 attr_stub
->SetSize( 1, 1 );
7485 attr_stub
->DecRef();
7491 // mark the cells that will be covered by this cell to
7492 // negative or zero values to point back at this cell
7493 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7496 for (j
=row
; j
< row
+ num_rows
; j
++)
7498 for (i
=col
; i
< col
+ num_cols
; i
++)
7500 if ((i
!= col
) || (j
!= row
))
7502 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7503 attr_stub
->SetSize( row
- j
, col
- i
);
7504 attr_stub
->DecRef();
7512 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7514 if ( CanHaveAttributes() )
7516 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7517 attr
->SetRenderer(renderer
);
7522 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7524 if ( CanHaveAttributes() )
7526 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7527 attr
->SetEditor(editor
);
7532 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7534 if ( CanHaveAttributes() )
7536 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7537 attr
->SetReadOnly(isReadOnly
);
7542 // ----------------------------------------------------------------------------
7543 // Data type registration
7544 // ----------------------------------------------------------------------------
7546 void wxGrid::RegisterDataType(const wxString
& typeName
,
7547 wxGridCellRenderer
* renderer
,
7548 wxGridCellEditor
* editor
)
7550 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7554 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7556 wxString typeName
= m_table
->GetTypeName(row
, col
);
7557 return GetDefaultEditorForType(typeName
);
7560 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7562 wxString typeName
= m_table
->GetTypeName(row
, col
);
7563 return GetDefaultRendererForType(typeName
);
7566 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7568 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7569 if ( index
== wxNOT_FOUND
)
7571 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7576 return m_typeRegistry
->GetEditor(index
);
7579 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7581 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7582 if ( index
== wxNOT_FOUND
)
7584 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7589 return m_typeRegistry
->GetRenderer(index
);
7592 // ----------------------------------------------------------------------------
7594 // ----------------------------------------------------------------------------
7596 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7600 setFixed
= new wxGridFixedIndicesSet
;
7603 setFixed
->insert(line
);
7607 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7609 return !setFixed
|| !setFixed
->count(line
);
7612 void wxGrid::EnableDragRowSize( bool enable
)
7614 m_canDragRowSize
= enable
;
7617 void wxGrid::EnableDragColSize( bool enable
)
7619 m_canDragColSize
= enable
;
7622 void wxGrid::EnableDragGridSize( bool enable
)
7624 m_canDragGridSize
= enable
;
7627 void wxGrid::EnableDragCell( bool enable
)
7629 m_canDragCell
= enable
;
7632 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7634 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7636 if ( resizeExistingRows
)
7638 // since we are resizing all rows to the default row size,
7639 // we can simply clear the row heights and row bottoms
7640 // arrays (which also allows us to take advantage of
7641 // some speed optimisations)
7642 m_rowHeights
.Empty();
7643 m_rowBottoms
.Empty();
7644 if ( !GetBatchCount() )
7649 void wxGrid::SetRowSize( int row
, int height
)
7651 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7653 // if < 0 then calculate new height from label
7657 wxArrayString lines
;
7658 wxClientDC
dc(m_rowLabelWin
);
7659 dc
.SetFont(GetLabelFont());
7660 StringToLines(GetRowLabelValue( row
), lines
);
7661 GetTextBoxSize( dc
, lines
, &w
, &h
);
7662 //check that it is not less than the minimal height
7663 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7666 // See comment in SetColSize
7667 if ( height
< GetRowMinimalAcceptableHeight())
7670 if ( m_rowHeights
.IsEmpty() )
7672 // need to really create the array
7676 int h
= wxMax( 0, height
);
7677 int diff
= h
- m_rowHeights
[row
];
7679 m_rowHeights
[row
] = h
;
7680 for ( int i
= row
; i
< m_numRows
; i
++ )
7682 m_rowBottoms
[i
] += diff
;
7685 if ( !GetBatchCount() )
7689 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7691 // we dont allow zero default column width
7692 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7694 if ( resizeExistingCols
)
7696 // since we are resizing all columns to the default column size,
7697 // we can simply clear the col widths and col rights
7698 // arrays (which also allows us to take advantage of
7699 // some speed optimisations)
7700 m_colWidths
.Empty();
7701 m_colRights
.Empty();
7702 if ( !GetBatchCount() )
7707 void wxGrid::SetColSize( int col
, int width
)
7709 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7711 // if < 0 then calculate new width from label
7715 wxArrayString lines
;
7716 wxClientDC
dc(m_colWindow
);
7717 dc
.SetFont(GetLabelFont());
7718 StringToLines(GetColLabelValue(col
), lines
);
7719 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7720 GetTextBoxSize( dc
, lines
, &w
, &h
);
7722 GetTextBoxSize( dc
, lines
, &h
, &w
);
7724 //check that it is not less than the minimal width
7725 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7728 // we intentionally don't test whether the width is less than
7729 // GetColMinimalWidth() here but we do compare it with
7730 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7731 // #651) -- and we also always allow the width of 0 as it has the special
7732 // sense of hiding the column
7733 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7736 if ( m_colWidths
.IsEmpty() )
7738 // need to really create the array
7742 const int diff
= width
- m_colWidths
[col
];
7743 m_colWidths
[col
] = width
;
7744 if ( m_useNativeHeader
)
7745 GetGridColHeader()->UpdateColumn(col
);
7746 //else: will be refreshed when the header is redrawn
7748 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7750 m_colRights
[GetColAt(colPos
)] += diff
;
7753 if ( !GetBatchCount() )
7760 void wxGrid::SetColMinimalWidth( int col
, int width
)
7762 if (width
> GetColMinimalAcceptableWidth())
7764 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7765 m_colMinWidths
[key
] = width
;
7769 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7771 if (width
> GetRowMinimalAcceptableHeight())
7773 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7774 m_rowMinHeights
[key
] = width
;
7778 int wxGrid::GetColMinimalWidth(int col
) const
7780 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7781 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7783 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7786 int wxGrid::GetRowMinimalHeight(int row
) const
7788 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7789 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7791 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7794 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7796 // We do allow a width of 0 since this gives us
7797 // an easy way to temporarily hiding columns.
7799 m_minAcceptableColWidth
= width
;
7802 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7804 // We do allow a height of 0 since this gives us
7805 // an easy way to temporarily hiding rows.
7807 m_minAcceptableRowHeight
= height
;
7810 int wxGrid::GetColMinimalAcceptableWidth() const
7812 return m_minAcceptableColWidth
;
7815 int wxGrid::GetRowMinimalAcceptableHeight() const
7817 return m_minAcceptableRowHeight
;
7820 // ----------------------------------------------------------------------------
7822 // ----------------------------------------------------------------------------
7825 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7827 const bool column
= direction
== wxGRID_COLUMN
;
7829 wxClientDC
dc(m_gridWin
);
7831 // cancel editing of cell
7832 HideCellEditControl();
7833 SaveEditControlValue();
7835 // initialize both of them just to avoid compiler warnings even if only
7836 // really needs to be initialized here
7850 wxCoord extent
, extentMax
= 0;
7851 int max
= column
? m_numRows
: m_numCols
;
7852 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7859 // we need to account for the cells spanning multiple columns/rows:
7860 // while they may need a lot of space, they don't need all of it in
7862 int numRows
, numCols
;
7863 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
7864 if ( span
== CellSpan_Inside
)
7866 // we need to get the size of the main cell, not of a cell hidden
7871 // get the size of the main cell too
7872 GetCellSize(row
, col
, &numRows
, &numCols
);
7875 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7876 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7879 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7880 extent
= column
? size
.x
: size
.y
;
7882 if ( span
!= CellSpan_None
)
7884 // we spread the size of a spanning cell over all the cells it
7885 // covers evenly -- this is probably not ideal but we can't
7886 // really do much better here
7888 // notice that numCols and numRows are never 0 as they
7889 // correspond to the size of the main cell of the span and not
7890 // of the cell inside it
7891 extent
/= column
? numCols
: numRows
;
7894 if ( extent
> extentMax
)
7903 // now also compare with the column label extent
7905 dc
.SetFont( GetLabelFont() );
7909 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7910 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7914 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7916 extent
= column
? w
: h
;
7917 if ( extent
> extentMax
)
7922 // empty column - give default extent (notice that if extentMax is less
7923 // than default extent but != 0, it's OK)
7924 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7929 // leave some space around text
7937 // Ensure automatic width is not less than minimal width. See the
7938 // comment in SetColSize() for explanation of why this isn't done
7941 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7943 SetColSize( col
, extentMax
);
7944 if ( !GetBatchCount() )
7946 if ( m_useNativeHeader
)
7948 GetGridColHeader()->UpdateColumn(col
);
7953 m_gridWin
->GetClientSize( &cw
, &ch
);
7954 wxRect
rect ( CellToRect( 0, col
) );
7956 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7957 rect
.width
= cw
- rect
.x
;
7958 rect
.height
= m_colLabelHeight
;
7959 GetColLabelWindow()->Refresh( true, &rect
);
7965 // Ensure automatic width is not less than minimal height. See the
7966 // comment in SetColSize() for explanation of why this isn't done
7969 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7971 SetRowSize(row
, extentMax
);
7972 if ( !GetBatchCount() )
7975 m_gridWin
->GetClientSize( &cw
, &ch
);
7976 wxRect
rect( CellToRect( row
, 0 ) );
7978 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
7979 rect
.width
= m_rowLabelWidth
;
7980 rect
.height
= ch
- rect
.y
;
7981 m_rowLabelWin
->Refresh( true, &rect
);
7988 SetColMinimalWidth(col
, extentMax
);
7990 SetRowMinimalHeight(row
, extentMax
);
7994 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
7996 // calculate size for the rows or columns?
7997 const bool calcRows
= direction
== wxGRID_ROW
;
7999 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
8000 : GetGridColLabelWindow());
8001 dc
.SetFont(GetLabelFont());
8003 // which dimension should we take into account for calculations?
8005 // for columns, the text can be only horizontal so it's easy but for rows
8006 // we also have to take into account the text orientation
8008 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
8010 wxArrayString lines
;
8011 wxCoord extentMax
= 0;
8013 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
8014 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
8018 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
8019 : GetColLabelValue(rowOrCol
);
8020 StringToLines(label
, lines
);
8023 GetTextBoxSize(dc
, lines
, &w
, &h
);
8025 const wxCoord extent
= useWidth
? w
: h
;
8026 if ( extent
> extentMax
)
8032 // empty column - give default extent (notice that if extentMax is less
8033 // than default extent but != 0, it's OK)
8034 extentMax
= calcRows
? GetDefaultRowLabelSize()
8035 : GetDefaultColLabelSize();
8038 // leave some space around text (taken from AutoSizeColOrRow)
8047 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8049 int width
= m_rowLabelWidth
;
8051 wxGridUpdateLocker locker
;
8053 locker
.Create(this);
8055 for ( int col
= 0; col
< m_numCols
; col
++ )
8058 AutoSizeColumn(col
, setAsMin
);
8060 width
+= GetColWidth(col
);
8066 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8068 int height
= m_colLabelHeight
;
8070 wxGridUpdateLocker locker
;
8072 locker
.Create(this);
8074 for ( int row
= 0; row
< m_numRows
; row
++ )
8077 AutoSizeRow(row
, setAsMin
);
8079 height
+= GetRowHeight(row
);
8085 void wxGrid::AutoSize()
8087 wxGridUpdateLocker
locker(this);
8089 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8090 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8092 // we know that we're not going to have scrollbars so disable them now to
8093 // avoid trouble in SetClientSize() which can otherwise set the correct
8094 // client size but also leave space for (not needed any more) scrollbars
8095 SetScrollbars(m_xScrollPixelsPerLine
, m_yScrollPixelsPerLine
,
8098 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8101 void wxGrid::AutoSizeRowLabelSize( int row
)
8103 // Hide the edit control, so it
8104 // won't interfere with drag-shrinking.
8105 if ( IsCellEditControlShown() )
8107 HideCellEditControl();
8108 SaveEditControlValue();
8111 // autosize row height depending on label text
8112 SetRowSize(row
, -1);
8116 void wxGrid::AutoSizeColLabelSize( int col
)
8118 // Hide the edit control, so it
8119 // won't interfere with drag-shrinking.
8120 if ( IsCellEditControlShown() )
8122 HideCellEditControl();
8123 SaveEditControlValue();
8126 // autosize column width depending on label text
8127 SetColSize(col
, -1);
8131 wxSize
wxGrid::DoGetBestSize() const
8133 wxGrid
* const self
= const_cast<wxGrid
*>(this);
8135 // we do the same as in AutoSize() here with the exception that we don't
8136 // change the column/row sizes, only calculate them
8137 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8138 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8140 // NOTE: This size should be cached, but first we need to add calls to
8141 // InvalidateBestSize everywhere that could change the results of this
8143 // CacheBestSize(size);
8145 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8146 + GetWindowBorderSize();
8154 wxPen
& wxGrid::GetDividerPen() const
8159 // ----------------------------------------------------------------------------
8160 // cell value accessor functions
8161 // ----------------------------------------------------------------------------
8163 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8167 m_table
->SetValue( row
, col
, s
);
8168 if ( !GetBatchCount() )
8171 wxRect
rect( CellToRect( row
, col
) );
8173 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8174 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8175 m_gridWin
->Refresh( false, &rect
);
8178 if ( m_currentCellCoords
.GetRow() == row
&&
8179 m_currentCellCoords
.GetCol() == col
&&
8180 IsCellEditControlShown())
8181 // Note: If we are using IsCellEditControlEnabled,
8182 // this interacts badly with calling SetCellValue from
8183 // an EVT_GRID_CELL_CHANGE handler.
8185 HideCellEditControl();
8186 ShowCellEditControl(); // will reread data from table
8191 // ----------------------------------------------------------------------------
8192 // block, row and column selection
8193 // ----------------------------------------------------------------------------
8195 void wxGrid::SelectRow( int row
, bool addToSelected
)
8200 if ( !addToSelected
)
8203 m_selection
->SelectRow(row
);
8206 void wxGrid::SelectCol( int col
, bool addToSelected
)
8211 if ( !addToSelected
)
8214 m_selection
->SelectCol(col
);
8217 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8223 if ( !addToSelected
)
8226 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8229 void wxGrid::SelectAll()
8231 if ( m_numRows
> 0 && m_numCols
> 0 )
8234 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8238 // ----------------------------------------------------------------------------
8239 // cell, row and col deselection
8240 // ----------------------------------------------------------------------------
8242 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8247 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8248 if ( mode
== oper
.GetSelectionMode() ||
8249 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8251 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8252 if ( m_selection
->IsInSelection(c
) )
8253 m_selection
->ToggleCellSelection(c
);
8255 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8257 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8258 for ( int i
= 0; i
< nOther
; i
++ )
8260 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8261 if ( m_selection
->IsInSelection(c
) )
8262 m_selection
->ToggleCellSelection(c
);
8265 //else: can only select orthogonal lines so no lines in this direction
8266 // could have been selected anyhow
8269 void wxGrid::DeselectRow(int row
)
8271 DeselectLine(row
, wxGridRowOperations());
8274 void wxGrid::DeselectCol(int col
)
8276 DeselectLine(col
, wxGridColumnOperations());
8279 void wxGrid::DeselectCell( int row
, int col
)
8281 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8282 m_selection
->ToggleCellSelection(row
, col
);
8285 bool wxGrid::IsSelection() const
8287 return ( m_selection
&& (m_selection
->IsSelection() ||
8288 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8289 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8292 bool wxGrid::IsInSelection( int row
, int col
) const
8294 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8295 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8296 col
>= m_selectedBlockTopLeft
.GetCol() &&
8297 row
<= m_selectedBlockBottomRight
.GetRow() &&
8298 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8301 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8305 wxGridCellCoordsArray a
;
8309 return m_selection
->m_cellSelection
;
8312 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8316 wxGridCellCoordsArray a
;
8320 return m_selection
->m_blockSelectionTopLeft
;
8323 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8327 wxGridCellCoordsArray a
;
8331 return m_selection
->m_blockSelectionBottomRight
;
8334 wxArrayInt
wxGrid::GetSelectedRows() const
8342 return m_selection
->m_rowSelection
;
8345 wxArrayInt
wxGrid::GetSelectedCols() const
8353 return m_selection
->m_colSelection
;
8356 void wxGrid::ClearSelection()
8358 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8359 m_selectedBlockBottomRight
);
8360 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8361 m_selectedBlockCorner
);
8363 m_selectedBlockTopLeft
=
8364 m_selectedBlockBottomRight
=
8365 m_selectedBlockCorner
= wxGridNoCellCoords
;
8367 if ( !r1
.IsEmpty() )
8368 RefreshRect(r1
, false);
8369 if ( !r2
.IsEmpty() )
8370 RefreshRect(r2
, false);
8373 m_selection
->ClearSelection();
8376 // This function returns the rectangle that encloses the given block
8377 // in device coords clipped to the client size of the grid window.
8379 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8380 const wxGridCellCoords
& bottomRight
) const
8383 wxRect tempCellRect
= CellToRect(topLeft
);
8384 if ( tempCellRect
!= wxGridNoCellRect
)
8386 resultRect
= tempCellRect
;
8390 resultRect
= wxRect(0, 0, 0, 0);
8393 tempCellRect
= CellToRect(bottomRight
);
8394 if ( tempCellRect
!= wxGridNoCellRect
)
8396 resultRect
+= tempCellRect
;
8400 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8401 return wxGridNoCellRect
;
8404 // Ensure that left/right and top/bottom pairs are in order.
8405 int left
= resultRect
.GetLeft();
8406 int top
= resultRect
.GetTop();
8407 int right
= resultRect
.GetRight();
8408 int bottom
= resultRect
.GetBottom();
8410 int leftCol
= topLeft
.GetCol();
8411 int topRow
= topLeft
.GetRow();
8412 int rightCol
= bottomRight
.GetCol();
8413 int bottomRow
= bottomRight
.GetRow();
8437 // The following loop is ONLY necessary to detect and handle merged cells.
8439 m_gridWin
->GetClientSize( &cw
, &ch
);
8441 // Get the origin coordinates: notice that they will be negative if the
8442 // grid is scrolled downwards/to the right.
8443 int gridOriginX
= 0;
8444 int gridOriginY
= 0;
8445 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8447 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8448 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8450 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8451 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8453 // Bound our loop so that we only examine the portion of the selected block
8454 // that is shown on screen. Therefore, we compare the Top-Left block values
8455 // to the Top-Left screen values, and the Bottom-Right block values to the
8456 // Bottom-Right screen values, choosing appropriately.
8457 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8458 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8459 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8460 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8462 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8464 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8466 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8467 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8469 tempCellRect
= CellToRect( j
, i
);
8471 if (tempCellRect
.x
< left
)
8472 left
= tempCellRect
.x
;
8473 if (tempCellRect
.y
< top
)
8474 top
= tempCellRect
.y
;
8475 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8476 right
= tempCellRect
.x
+ tempCellRect
.width
;
8477 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8478 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8482 i
= visibleRightCol
; // jump over inner cells.
8487 // Convert to scrolled coords
8488 CalcScrolledPosition( left
, top
, &left
, &top
);
8489 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8491 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8492 return wxRect(0,0,0,0);
8494 resultRect
.SetLeft( wxMax(0, left
) );
8495 resultRect
.SetTop( wxMax(0, top
) );
8496 resultRect
.SetRight( wxMin(cw
, right
) );
8497 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8502 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8503 const wxGridOperations
& oper
)
8506 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8507 const int numLines
= oper
.GetNumberOfLines(this);
8508 for ( int i
= 0; i
< numLines
; i
++ )
8510 int size
= sizeInfo
.GetSize(i
);
8511 if ( size
!= sizeInfo
.m_sizeDefault
)
8512 oper
.SetLineSize(this, i
, size
);
8517 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8519 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8522 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8524 DoSetSizes(sizeInfo
, wxGridRowOperations());
8527 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8529 m_sizeDefault
= defSize
;
8530 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8532 if ( allSizes
[i
] != defSize
)
8533 m_customSizes
[i
] = allSizes
[i
];
8537 int wxGridSizesInfo::GetSize(unsigned pos
) const
8539 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8541 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8544 // ----------------------------------------------------------------------------
8546 // ----------------------------------------------------------------------------
8548 #if wxUSE_DRAG_AND_DROP
8550 // this allow setting drop target directly on wxGrid
8551 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8553 GetGridWindow()->SetDropTarget(dropTarget
);
8556 #endif // wxUSE_DRAG_AND_DROP
8558 // ----------------------------------------------------------------------------
8559 // grid event classes
8560 // ----------------------------------------------------------------------------
8562 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8564 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8565 int row
, int col
, int x
, int y
, bool sel
,
8566 bool control
, bool shift
, bool alt
, bool meta
)
8567 : wxNotifyEvent( type
, id
),
8568 wxKeyboardState(control
, shift
, alt
, meta
)
8570 Init(row
, col
, x
, y
, sel
);
8572 SetEventObject(obj
);
8575 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8577 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8578 int rowOrCol
, int x
, int y
,
8579 bool control
, bool shift
, bool alt
, bool meta
)
8580 : wxNotifyEvent( type
, id
),
8581 wxKeyboardState(control
, shift
, alt
, meta
)
8583 Init(rowOrCol
, x
, y
);
8585 SetEventObject(obj
);
8589 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8591 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8592 const wxGridCellCoords
& topLeft
,
8593 const wxGridCellCoords
& bottomRight
,
8594 bool sel
, bool control
,
8595 bool shift
, bool alt
, bool meta
)
8596 : wxNotifyEvent( type
, id
),
8597 wxKeyboardState(control
, shift
, alt
, meta
)
8599 Init(topLeft
, bottomRight
, sel
);
8601 SetEventObject(obj
);
8605 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8607 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8608 wxObject
* obj
, int row
,
8609 int col
, wxControl
* ctrl
)
8610 : wxCommandEvent(type
, id
)
8612 SetEventObject(obj
);
8619 // ----------------------------------------------------------------------------
8620 // wxGridTypeRegistry
8621 // ----------------------------------------------------------------------------
8623 wxGridTypeRegistry::~wxGridTypeRegistry()
8625 size_t count
= m_typeinfo
.GetCount();
8626 for ( size_t i
= 0; i
< count
; i
++ )
8627 delete m_typeinfo
[i
];
8630 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8631 wxGridCellRenderer
* renderer
,
8632 wxGridCellEditor
* editor
)
8634 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8636 // is it already registered?
8637 int loc
= FindRegisteredDataType(typeName
);
8638 if ( loc
!= wxNOT_FOUND
)
8640 delete m_typeinfo
[loc
];
8641 m_typeinfo
[loc
] = info
;
8645 m_typeinfo
.Add(info
);
8649 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8651 size_t count
= m_typeinfo
.GetCount();
8652 for ( size_t i
= 0; i
< count
; i
++ )
8654 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8663 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8665 int index
= FindRegisteredDataType(typeName
);
8666 if ( index
== wxNOT_FOUND
)
8668 // check whether this is one of the standard ones, in which case
8669 // register it "on the fly"
8671 if ( typeName
== wxGRID_VALUE_STRING
)
8673 RegisterDataType(wxGRID_VALUE_STRING
,
8674 new wxGridCellStringRenderer
,
8675 new wxGridCellTextEditor
);
8678 #endif // wxUSE_TEXTCTRL
8680 if ( typeName
== wxGRID_VALUE_BOOL
)
8682 RegisterDataType(wxGRID_VALUE_BOOL
,
8683 new wxGridCellBoolRenderer
,
8684 new wxGridCellBoolEditor
);
8687 #endif // wxUSE_CHECKBOX
8689 if ( typeName
== wxGRID_VALUE_NUMBER
)
8691 RegisterDataType(wxGRID_VALUE_NUMBER
,
8692 new wxGridCellNumberRenderer
,
8693 new wxGridCellNumberEditor
);
8695 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8697 RegisterDataType(wxGRID_VALUE_FLOAT
,
8698 new wxGridCellFloatRenderer
,
8699 new wxGridCellFloatEditor
);
8702 #endif // wxUSE_TEXTCTRL
8704 if ( typeName
== wxGRID_VALUE_CHOICE
)
8706 RegisterDataType(wxGRID_VALUE_CHOICE
,
8707 new wxGridCellStringRenderer
,
8708 new wxGridCellChoiceEditor
);
8711 #endif // wxUSE_COMBOBOX
8716 // we get here only if just added the entry for this type, so return
8718 index
= m_typeinfo
.GetCount() - 1;
8724 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8726 int index
= FindDataType(typeName
);
8727 if ( index
== wxNOT_FOUND
)
8729 // the first part of the typename is the "real" type, anything after ':'
8730 // are the parameters for the renderer
8731 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8732 if ( index
== wxNOT_FOUND
)
8737 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8738 wxGridCellRenderer
*rendererOld
= renderer
;
8739 renderer
= renderer
->Clone();
8740 rendererOld
->DecRef();
8742 wxGridCellEditor
*editor
= GetEditor(index
);
8743 wxGridCellEditor
*editorOld
= editor
;
8744 editor
= editor
->Clone();
8745 editorOld
->DecRef();
8747 // do it even if there are no parameters to reset them to defaults
8748 wxString params
= typeName
.AfterFirst(wxT(':'));
8749 renderer
->SetParameters(params
);
8750 editor
->SetParameters(params
);
8752 // register the new typename
8753 RegisterDataType(typeName
, renderer
, editor
);
8755 // we just registered it, it's the last one
8756 index
= m_typeinfo
.GetCount() - 1;
8762 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8764 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8771 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8773 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8780 #endif // wxUSE_GRID