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 // ============================================================================
163 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
165 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
166 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
167 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
168 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
171 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
172 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
174 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
175 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
176 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
178 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
179 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
182 wxGridOperations
& wxGridRowOperations::Dual() const
184 static wxGridColumnOperations s_colOper
;
189 wxGridOperations
& wxGridColumnOperations::Dual() const
191 static wxGridRowOperations s_rowOper
;
196 // ----------------------------------------------------------------------------
197 // wxGridCellWorker is an (almost) empty common base class for
198 // wxGridCellRenderer and wxGridCellEditor managing ref counting
199 // ----------------------------------------------------------------------------
201 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
206 wxGridCellWorker::~wxGridCellWorker()
210 // ----------------------------------------------------------------------------
211 // wxGridHeaderLabelsRenderer and related classes
212 // ----------------------------------------------------------------------------
214 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid
& grid
,
216 const wxString
& value
,
220 int textOrientation
) const
222 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
223 dc
.SetTextForeground(grid
.GetLabelTextColour());
224 dc
.SetFont(grid
.GetLabelFont());
225 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
229 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
233 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
234 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
235 rect
.GetRight(), rect
.GetBottom());
236 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
237 rect
.GetLeft(), rect
.GetBottom());
238 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
239 rect
.GetRight() + 1, rect
.GetBottom());
241 dc
.SetPen(*wxWHITE_PEN
);
242 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
243 rect
.GetLeft() + 1, rect
.GetBottom());
244 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
245 rect
.GetRight(), rect
.GetTop());
250 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
254 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
255 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
256 rect
.GetRight(), rect
.GetBottom());
257 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
258 rect
.GetRight(), rect
.GetTop());
259 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
260 rect
.GetRight() + 1, rect
.GetBottom());
262 dc
.SetPen(*wxWHITE_PEN
);
263 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
264 rect
.GetLeft(), rect
.GetBottom());
265 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
266 rect
.GetRight(), rect
.GetTop() + 1);
271 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
275 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
276 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
277 rect
.GetRight() - 1, rect
.GetTop());
278 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
279 rect
.GetLeft(), rect
.GetBottom() - 1);
280 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
281 rect
.GetRight(), rect
.GetTop());
282 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
283 rect
.GetLeft(), rect
.GetBottom());
285 dc
.SetPen(*wxWHITE_PEN
);
286 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
287 rect
.GetRight() - 1, rect
.GetTop() + 1);
288 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
289 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
300 m_isReadOnly
= Unset
;
305 m_attrkind
= wxGridCellAttr::Cell
;
307 m_sizeRows
= m_sizeCols
= 1;
308 m_overflow
= UnsetOverflow
;
310 SetDefAttr(attrDefault
);
313 wxGridCellAttr
*wxGridCellAttr::Clone() const
315 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
317 if ( HasTextColour() )
318 attr
->SetTextColour(GetTextColour());
319 if ( HasBackgroundColour() )
320 attr
->SetBackgroundColour(GetBackgroundColour());
322 attr
->SetFont(GetFont());
323 if ( HasAlignment() )
324 attr
->SetAlignment(m_hAlign
, m_vAlign
);
326 attr
->SetSize( m_sizeRows
, m_sizeCols
);
330 attr
->SetRenderer(m_renderer
);
331 m_renderer
->IncRef();
335 attr
->SetEditor(m_editor
);
342 attr
->SetOverflow( m_overflow
== Overflow
);
343 attr
->SetKind( m_attrkind
);
348 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
350 if ( !HasTextColour() && mergefrom
->HasTextColour() )
351 SetTextColour(mergefrom
->GetTextColour());
352 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
353 SetBackgroundColour(mergefrom
->GetBackgroundColour());
354 if ( !HasFont() && mergefrom
->HasFont() )
355 SetFont(mergefrom
->GetFont());
356 if ( !HasAlignment() && mergefrom
->HasAlignment() )
359 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
360 SetAlignment(hAlign
, vAlign
);
362 if ( !HasSize() && mergefrom
->HasSize() )
363 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
365 // Directly access member functions as GetRender/Editor don't just return
366 // m_renderer/m_editor
368 // Maybe add support for merge of Render and Editor?
369 if (!HasRenderer() && mergefrom
->HasRenderer() )
371 m_renderer
= mergefrom
->m_renderer
;
372 m_renderer
->IncRef();
374 if ( !HasEditor() && mergefrom
->HasEditor() )
376 m_editor
= mergefrom
->m_editor
;
379 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
380 SetReadOnly(mergefrom
->IsReadOnly());
382 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
383 SetOverflow(mergefrom
->GetOverflow());
385 SetDefAttr(mergefrom
->m_defGridAttr
);
388 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
390 // The size of a cell is normally 1,1
392 // If this cell is larger (2,2) then this is the top left cell
393 // the other cells that will be covered (lower right cells) must be
394 // set to negative or zero values such that
395 // row + num_rows of the covered cell points to the larger cell (this cell)
396 // same goes for the col + num_cols.
398 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
400 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
401 !((num_rows
<= 0) && (num_cols
> 0)) ||
402 !((num_rows
== 0) && (num_cols
== 0))),
403 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
405 m_sizeRows
= num_rows
;
406 m_sizeCols
= num_cols
;
409 const wxColour
& wxGridCellAttr::GetTextColour() const
415 else if (m_defGridAttr
&& m_defGridAttr
!= this)
417 return m_defGridAttr
->GetTextColour();
421 wxFAIL_MSG(wxT("Missing default cell attribute"));
426 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
428 if (HasBackgroundColour())
432 else if (m_defGridAttr
&& m_defGridAttr
!= this)
434 return m_defGridAttr
->GetBackgroundColour();
438 wxFAIL_MSG(wxT("Missing default cell attribute"));
443 const wxFont
& wxGridCellAttr::GetFont() const
449 else if (m_defGridAttr
&& m_defGridAttr
!= this)
451 return m_defGridAttr
->GetFont();
455 wxFAIL_MSG(wxT("Missing default cell attribute"));
460 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
469 else if (m_defGridAttr
&& m_defGridAttr
!= this)
471 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
475 wxFAIL_MSG(wxT("Missing default cell attribute"));
479 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
482 *num_rows
= m_sizeRows
;
484 *num_cols
= m_sizeCols
;
487 // GetRenderer and GetEditor use a slightly different decision path about
488 // which attribute to use. If a non-default attr object has one then it is
489 // used, otherwise the default editor or renderer is fetched from the grid and
490 // used. It should be the default for the data type of the cell. If it is
491 // NULL (because the table has a type that the grid does not have in its
492 // registry), then the grid's default editor or renderer is used.
494 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
496 wxGridCellRenderer
*renderer
= NULL
;
498 if ( m_renderer
&& this != m_defGridAttr
)
500 // use the cells renderer if it has one
501 renderer
= m_renderer
;
504 else // no non-default cell renderer
506 // get default renderer for the data type
509 // GetDefaultRendererForCell() will do IncRef() for us
510 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
513 if ( renderer
== NULL
)
515 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
517 // if we still don't have one then use the grid default
518 // (no need for IncRef() here neither)
519 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
521 else // default grid attr
523 // use m_renderer which we had decided not to use initially
524 renderer
= m_renderer
;
531 // we're supposed to always find something
532 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
537 // same as above, except for s/renderer/editor/g
538 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
540 wxGridCellEditor
*editor
= NULL
;
542 if ( m_editor
&& this != m_defGridAttr
)
544 // use the cells editor if it has one
548 else // no non default cell editor
550 // get default editor for the data type
553 // GetDefaultEditorForCell() will do IncRef() for us
554 editor
= grid
->GetDefaultEditorForCell(row
, col
);
557 if ( editor
== NULL
)
559 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
561 // if we still don't have one then use the grid default
562 // (no need for IncRef() here neither)
563 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
565 else // default grid attr
567 // use m_editor which we had decided not to use initially
575 // we're supposed to always find something
576 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
581 // ----------------------------------------------------------------------------
582 // wxGridCellAttrData
583 // ----------------------------------------------------------------------------
585 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
587 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
588 // touch attribute's reference counting explicitly, since this
589 // is managed by class wxGridCellWithAttr
590 int n
= FindIndex(row
, col
);
591 if ( n
== wxNOT_FOUND
)
596 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
598 //else: nothing to do
600 else // we already have an attribute for this cell
604 // change the attribute
605 m_attrs
[(size_t)n
].ChangeAttr(attr
);
609 // remove this attribute
610 m_attrs
.RemoveAt((size_t)n
);
615 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
617 wxGridCellAttr
*attr
= NULL
;
619 int n
= FindIndex(row
, col
);
620 if ( n
!= wxNOT_FOUND
)
622 attr
= m_attrs
[(size_t)n
].attr
;
629 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
631 size_t count
= m_attrs
.GetCount();
632 for ( size_t n
= 0; n
< count
; n
++ )
634 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
635 wxCoord row
= coords
.GetRow();
636 if ((size_t)row
>= pos
)
640 // If rows inserted, include row counter where necessary
641 coords
.SetRow(row
+ numRows
);
643 else if (numRows
< 0)
645 // If rows deleted ...
646 if ((size_t)row
>= pos
- numRows
)
648 // ...either decrement row counter (if row still exists)...
649 coords
.SetRow(row
+ numRows
);
653 // ...or remove the attribute
663 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
665 size_t count
= m_attrs
.GetCount();
666 for ( size_t n
= 0; n
< count
; n
++ )
668 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
669 wxCoord col
= coords
.GetCol();
670 if ( (size_t)col
>= pos
)
674 // If rows inserted, include row counter where necessary
675 coords
.SetCol(col
+ numCols
);
677 else if (numCols
< 0)
679 // If rows deleted ...
680 if ((size_t)col
>= pos
- numCols
)
682 // ...either decrement row counter (if row still exists)...
683 coords
.SetCol(col
+ numCols
);
687 // ...or remove the attribute
697 int wxGridCellAttrData::FindIndex(int row
, int col
) const
699 size_t count
= m_attrs
.GetCount();
700 for ( size_t n
= 0; n
< count
; n
++ )
702 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
703 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
712 // ----------------------------------------------------------------------------
713 // wxGridRowOrColAttrData
714 // ----------------------------------------------------------------------------
716 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
718 size_t count
= m_attrs
.GetCount();
719 for ( size_t n
= 0; n
< count
; n
++ )
721 m_attrs
[n
]->DecRef();
725 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
727 wxGridCellAttr
*attr
= NULL
;
729 int n
= m_rowsOrCols
.Index(rowOrCol
);
730 if ( n
!= wxNOT_FOUND
)
732 attr
= m_attrs
[(size_t)n
];
739 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
741 int i
= m_rowsOrCols
.Index(rowOrCol
);
742 if ( i
== wxNOT_FOUND
)
746 // store the new attribute, taking its ownership
747 m_rowsOrCols
.Add(rowOrCol
);
752 else // we have an attribute for this row or column
754 size_t n
= (size_t)i
;
756 // notice that this code works correctly even when the old attribute is
757 // the same as the new one: as we own of it, we must call DecRef() on
758 // it in any case and this won't result in destruction of the new
759 // attribute if it's the same as old one because it must have ref count
760 // of at least 2 to be passed to us while we keep a reference to it too
761 m_attrs
[n
]->DecRef();
765 // replace the attribute with the new one
768 else // remove the attribute
770 m_rowsOrCols
.RemoveAt(n
);
776 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
778 size_t count
= m_attrs
.GetCount();
779 for ( size_t n
= 0; n
< count
; n
++ )
781 int & rowOrCol
= m_rowsOrCols
[n
];
782 if ( (size_t)rowOrCol
>= pos
)
784 if ( numRowsOrCols
> 0 )
786 // If rows inserted, include row counter where necessary
787 rowOrCol
+= numRowsOrCols
;
789 else if ( numRowsOrCols
< 0)
791 // If rows deleted, either decrement row counter (if row still exists)
792 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
793 rowOrCol
+= numRowsOrCols
;
796 m_rowsOrCols
.RemoveAt(n
);
797 m_attrs
[n
]->DecRef();
807 // ----------------------------------------------------------------------------
808 // wxGridCellAttrProvider
809 // ----------------------------------------------------------------------------
811 wxGridCellAttrProvider::wxGridCellAttrProvider()
816 wxGridCellAttrProvider::~wxGridCellAttrProvider()
821 void wxGridCellAttrProvider::InitData()
823 m_data
= new wxGridCellAttrProviderData
;
826 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
827 wxGridCellAttr::wxAttrKind kind
) const
829 wxGridCellAttr
*attr
= NULL
;
834 case (wxGridCellAttr::Any
):
835 // Get cached merge attributes.
836 // Currently not used as no cache implemented as not mutable
837 // attr = m_data->m_mergeAttr.GetAttr(row, col);
840 // Basically implement old version.
841 // Also check merge cache, so we don't have to re-merge every time..
842 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
843 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
844 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
846 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
848 // Two or more are non NULL
849 attr
= new wxGridCellAttr
;
850 attr
->SetKind(wxGridCellAttr::Merged
);
852 // Order is important..
855 attr
->MergeWith(attrcell
);
860 attr
->MergeWith(attrcol
);
865 attr
->MergeWith(attrrow
);
869 // store merge attr if cache implemented
871 //m_data->m_mergeAttr.SetAttr(attr, row, col);
875 // one or none is non null return it or null.
894 case (wxGridCellAttr::Cell
):
895 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
898 case (wxGridCellAttr::Col
):
899 attr
= m_data
->m_colAttrs
.GetAttr(col
);
902 case (wxGridCellAttr::Row
):
903 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
908 // (wxGridCellAttr::Default):
909 // (wxGridCellAttr::Merged):
917 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
923 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
926 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
931 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
934 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
939 m_data
->m_colAttrs
.SetAttr(attr
, col
);
942 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
946 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
948 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
952 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
956 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
958 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
962 const wxGridColumnHeaderRenderer
&
963 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
965 return gs_defaultHeaderRenderers
.colRenderer
;
968 const wxGridRowHeaderRenderer
&
969 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
971 return gs_defaultHeaderRenderers
.rowRenderer
;
974 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
976 return gs_defaultHeaderRenderers
.cornerRenderer
;
979 // ----------------------------------------------------------------------------
981 // ----------------------------------------------------------------------------
983 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
985 wxGridTableBase::wxGridTableBase()
988 m_attrProvider
= NULL
;
991 wxGridTableBase::~wxGridTableBase()
993 delete m_attrProvider
;
996 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
998 delete m_attrProvider
;
999 m_attrProvider
= attrProvider
;
1002 bool wxGridTableBase::CanHaveAttributes()
1004 if ( ! GetAttrProvider() )
1006 // use the default attr provider by default
1007 SetAttrProvider(new wxGridCellAttrProvider
);
1013 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1015 if ( m_attrProvider
)
1016 return m_attrProvider
->GetAttr(row
, col
, kind
);
1021 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1023 if ( m_attrProvider
)
1026 attr
->SetKind(wxGridCellAttr::Cell
);
1027 m_attrProvider
->SetAttr(attr
, row
, col
);
1031 // as we take ownership of the pointer and don't store it, we must
1037 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1039 if ( m_attrProvider
)
1041 attr
->SetKind(wxGridCellAttr::Row
);
1042 m_attrProvider
->SetRowAttr(attr
, row
);
1046 // as we take ownership of the pointer and don't store it, we must
1052 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1054 if ( m_attrProvider
)
1056 attr
->SetKind(wxGridCellAttr::Col
);
1057 m_attrProvider
->SetColAttr(attr
, col
);
1061 // as we take ownership of the pointer and don't store it, we must
1067 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1068 size_t WXUNUSED(numRows
) )
1070 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1075 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1077 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1082 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1083 size_t WXUNUSED(numRows
) )
1085 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1090 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1091 size_t WXUNUSED(numCols
) )
1093 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1098 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1100 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1105 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1106 size_t WXUNUSED(numCols
) )
1108 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1113 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1117 // RD: Starting the rows at zero confuses users,
1118 // no matter how much it makes sense to us geeks.
1124 wxString
wxGridTableBase::GetColLabelValue( int col
)
1126 // default col labels are:
1127 // cols 0 to 25 : A-Z
1128 // cols 26 to 675 : AA-ZZ
1133 for ( n
= 1; ; n
++ )
1135 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1141 // reverse the string...
1143 for ( i
= 0; i
< n
; i
++ )
1151 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1153 return wxGRID_VALUE_STRING
;
1156 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1157 const wxString
& typeName
)
1159 return typeName
== wxGRID_VALUE_STRING
;
1162 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1164 return CanGetValueAs(row
, col
, typeName
);
1167 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1172 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1177 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1182 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1183 long WXUNUSED(value
) )
1187 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1188 double WXUNUSED(value
) )
1192 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1193 bool WXUNUSED(value
) )
1197 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1198 const wxString
& WXUNUSED(typeName
) )
1203 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1204 const wxString
& WXUNUSED(typeName
),
1205 void* WXUNUSED(value
) )
1209 //////////////////////////////////////////////////////////////////////
1211 // Message class for the grid table to send requests and notifications
1215 wxGridTableMessage::wxGridTableMessage()
1223 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1224 int commandInt1
, int commandInt2
)
1228 m_comInt1
= commandInt1
;
1229 m_comInt2
= commandInt2
;
1232 //////////////////////////////////////////////////////////////////////
1234 // A basic grid table for string data. An object of this class will
1235 // created by wxGrid if you don't specify an alternative table class.
1238 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1240 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1242 wxGridStringTable::wxGridStringTable()
1248 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1251 m_numCols
= numCols
;
1253 m_data
.Alloc( numRows
);
1256 sa
.Alloc( numCols
);
1257 sa
.Add( wxEmptyString
, numCols
);
1259 m_data
.Add( sa
, numRows
);
1262 wxString
wxGridStringTable::GetValue( int row
, int col
)
1264 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1265 (col
>= 0 && col
< GetNumberCols()),
1267 wxT("invalid row or column index in wxGridStringTable") );
1269 return m_data
[row
][col
];
1272 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1274 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1275 (col
>= 0 && col
< GetNumberCols()),
1276 wxT("invalid row or column index in wxGridStringTable") );
1278 m_data
[row
][col
] = value
;
1281 void wxGridStringTable::Clear()
1284 int numRows
, numCols
;
1286 numRows
= m_data
.GetCount();
1289 numCols
= m_data
[0].GetCount();
1291 for ( row
= 0; row
< numRows
; row
++ )
1293 for ( col
= 0; col
< numCols
; col
++ )
1295 m_data
[row
][col
] = wxEmptyString
;
1301 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1303 size_t curNumRows
= m_data
.GetCount();
1304 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1305 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1307 if ( pos
>= curNumRows
)
1309 return AppendRows( numRows
);
1313 sa
.Alloc( curNumCols
);
1314 sa
.Add( wxEmptyString
, curNumCols
);
1315 m_data
.Insert( sa
, pos
, numRows
);
1319 wxGridTableMessage
msg( this,
1320 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1324 GetView()->ProcessTableMessage( msg
);
1330 bool wxGridStringTable::AppendRows( size_t numRows
)
1332 size_t curNumRows
= m_data
.GetCount();
1333 size_t curNumCols
= ( curNumRows
> 0
1334 ? m_data
[0].GetCount()
1335 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1338 if ( curNumCols
> 0 )
1340 sa
.Alloc( curNumCols
);
1341 sa
.Add( wxEmptyString
, curNumCols
);
1344 m_data
.Add( sa
, numRows
);
1348 wxGridTableMessage
msg( this,
1349 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1352 GetView()->ProcessTableMessage( msg
);
1358 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1360 size_t curNumRows
= m_data
.GetCount();
1362 if ( pos
>= curNumRows
)
1364 wxFAIL_MSG( wxString::Format
1366 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1368 (unsigned long)numRows
,
1369 (unsigned long)curNumRows
1375 if ( numRows
> curNumRows
- pos
)
1377 numRows
= curNumRows
- pos
;
1380 if ( numRows
>= curNumRows
)
1386 m_data
.RemoveAt( pos
, numRows
);
1391 wxGridTableMessage
msg( this,
1392 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1396 GetView()->ProcessTableMessage( msg
);
1402 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1406 size_t curNumRows
= m_data
.GetCount();
1407 size_t curNumCols
= ( curNumRows
> 0
1408 ? m_data
[0].GetCount()
1409 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1411 if ( pos
>= curNumCols
)
1413 return AppendCols( numCols
);
1416 if ( !m_colLabels
.IsEmpty() )
1418 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1421 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1422 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1425 for ( row
= 0; row
< curNumRows
; row
++ )
1427 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1429 m_data
[row
].Insert( wxEmptyString
, col
);
1433 m_numCols
+= numCols
;
1437 wxGridTableMessage
msg( this,
1438 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1442 GetView()->ProcessTableMessage( msg
);
1448 bool wxGridStringTable::AppendCols( size_t numCols
)
1452 size_t curNumRows
= m_data
.GetCount();
1454 for ( row
= 0; row
< curNumRows
; row
++ )
1456 m_data
[row
].Add( wxEmptyString
, numCols
);
1459 m_numCols
+= numCols
;
1463 wxGridTableMessage
msg( this,
1464 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1467 GetView()->ProcessTableMessage( msg
);
1473 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1477 size_t curNumRows
= m_data
.GetCount();
1478 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1479 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1481 if ( pos
>= curNumCols
)
1483 wxFAIL_MSG( wxString::Format
1485 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1487 (unsigned long)numCols
,
1488 (unsigned long)curNumCols
1495 colID
= GetView()->GetColAt( pos
);
1499 if ( numCols
> curNumCols
- colID
)
1501 numCols
= curNumCols
- colID
;
1504 if ( !m_colLabels
.IsEmpty() )
1506 // m_colLabels stores just as many elements as it needs, e.g. if only
1507 // the label of the first column had been set it would have only one
1508 // element and not numCols, so account for it
1509 int nToRm
= m_colLabels
.size() - colID
;
1511 m_colLabels
.RemoveAt( colID
, nToRm
);
1514 if ( numCols
>= curNumCols
)
1516 for ( row
= 0; row
< curNumRows
; row
++ )
1518 m_data
[row
].Clear();
1523 else // something will be left
1525 for ( row
= 0; row
< curNumRows
; row
++ )
1527 m_data
[row
].RemoveAt( colID
, numCols
);
1530 m_numCols
-= numCols
;
1535 wxGridTableMessage
msg( this,
1536 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1540 GetView()->ProcessTableMessage( msg
);
1546 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1548 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1550 // using default label
1552 return wxGridTableBase::GetRowLabelValue( row
);
1556 return m_rowLabels
[row
];
1560 wxString
wxGridStringTable::GetColLabelValue( int col
)
1562 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1564 // using default label
1566 return wxGridTableBase::GetColLabelValue( col
);
1570 return m_colLabels
[col
];
1574 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1576 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1578 int n
= m_rowLabels
.GetCount();
1581 for ( i
= n
; i
<= row
; i
++ )
1583 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1587 m_rowLabels
[row
] = value
;
1590 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1592 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1594 int n
= m_colLabels
.GetCount();
1597 for ( i
= n
; i
<= col
; i
++ )
1599 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1603 m_colLabels
[col
] = value
;
1607 //////////////////////////////////////////////////////////////////////
1608 //////////////////////////////////////////////////////////////////////
1610 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1611 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1614 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1616 m_owner
->CancelMouseCapture();
1619 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1620 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1621 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1622 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1625 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1629 // NO - don't do this because it will set both the x and y origin
1630 // coords to match the parent scrolled window and we just want to
1631 // set the y coord - MB
1633 // m_owner->PrepareDC( dc );
1636 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1637 wxPoint pt
= dc
.GetDeviceOrigin();
1638 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1640 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1641 m_owner
->DrawRowLabels( dc
, rows
);
1644 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1646 m_owner
->ProcessRowLabelMouseEvent( event
);
1649 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1651 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1655 //////////////////////////////////////////////////////////////////////
1657 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1658 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1659 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1660 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1663 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1667 // NO - don't do this because it will set both the x and y origin
1668 // coords to match the parent scrolled window and we just want to
1669 // set the x coord - MB
1671 // m_owner->PrepareDC( dc );
1674 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1675 wxPoint pt
= dc
.GetDeviceOrigin();
1676 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1677 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1679 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1681 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1682 m_owner
->DrawColLabels( dc
, cols
);
1685 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1687 m_owner
->ProcessColLabelMouseEvent( event
);
1690 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1692 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1696 //////////////////////////////////////////////////////////////////////
1698 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1699 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1700 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1701 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1704 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1708 m_owner
->DrawCornerLabel(dc
);
1711 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1713 m_owner
->ProcessCornerLabelMouseEvent( event
);
1716 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1718 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1722 //////////////////////////////////////////////////////////////////////
1724 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1725 EVT_PAINT( wxGridWindow::OnPaint
)
1726 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1727 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1728 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1729 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1730 EVT_CHAR( wxGridWindow::OnChar
)
1731 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1732 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1733 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1736 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1738 wxPaintDC
dc( this );
1739 m_owner
->PrepareDC( dc
);
1740 wxRegion reg
= GetUpdateRegion();
1741 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1742 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1744 m_owner
->DrawGridSpace( dc
);
1746 m_owner
->DrawAllGridLines( dc
, reg
);
1748 m_owner
->DrawHighlight( dc
, dirtyCells
);
1751 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1753 wxWindow::ScrollWindow( dx
, dy
, rect
);
1754 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1755 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1758 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1760 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1763 m_owner
->ProcessGridCellMouseEvent( event
);
1766 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1768 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1772 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1773 // cursor must be in the cell edit control to get key events
1775 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1777 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1781 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1783 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1787 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1789 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1793 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1797 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1799 // and if we have any selection, it has to be repainted, because it
1800 // uses different colour when the grid is not focused:
1801 if ( m_owner
->IsSelection() )
1807 // NB: Note that this code is in "else" branch only because the other
1808 // branch refreshes everything and so there's no point in calling
1809 // Refresh() again, *not* because it should only be done if
1810 // !IsSelection(). If the above code is ever optimized to refresh
1811 // only selected area, this needs to be moved out of the "else"
1812 // branch so that it's always executed.
1814 // current cell cursor {dis,re}appears on focus change:
1815 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1816 m_owner
->GetGridCursorCol());
1817 const wxRect cursor
=
1818 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1819 Refresh(true, &cursor
);
1822 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1826 #define internalXToCol(x) XToCol(x, true)
1827 #define internalYToRow(y) YToRow(y, true)
1829 /////////////////////////////////////////////////////////////////////
1831 #if wxUSE_EXTENDED_RTTI
1832 WX_DEFINE_FLAGS( wxGridStyle
)
1834 wxBEGIN_FLAGS( wxGridStyle
)
1835 // new style border flags, we put them first to
1836 // use them for streaming out
1837 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
1838 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
1839 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
1840 wxFLAGS_MEMBER(wxBORDER_RAISED
)
1841 wxFLAGS_MEMBER(wxBORDER_STATIC
)
1842 wxFLAGS_MEMBER(wxBORDER_NONE
)
1844 // old style border flags
1845 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
1846 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
1847 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
1848 wxFLAGS_MEMBER(wxRAISED_BORDER
)
1849 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
1850 wxFLAGS_MEMBER(wxBORDER
)
1852 // standard window styles
1853 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
1854 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
1855 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
1856 wxFLAGS_MEMBER(wxWANTS_CHARS
)
1857 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
1858 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
1859 wxFLAGS_MEMBER(wxVSCROLL
)
1860 wxFLAGS_MEMBER(wxHSCROLL
)
1862 wxEND_FLAGS( wxGridStyle
)
1864 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
1866 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
1867 wxHIDE_PROPERTY( Children
)
1868 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1869 wxEND_PROPERTIES_TABLE()
1871 wxBEGIN_HANDLERS_TABLE(wxGrid
)
1872 wxEND_HANDLERS_TABLE()
1874 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1877 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1880 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1883 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1884 EVT_PAINT( wxGrid::OnPaint
)
1885 EVT_SIZE( wxGrid::OnSize
)
1886 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1887 EVT_KEY_UP( wxGrid::OnKeyUp
)
1888 EVT_CHAR ( wxGrid::OnChar
)
1889 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1892 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1893 const wxPoint
& pos
, const wxSize
& size
,
1894 long style
, const wxString
& name
)
1896 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1897 style
| wxWANTS_CHARS
, name
))
1900 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1901 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1904 SetInitialSize(size
);
1905 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
1914 m_winCapture
->ReleaseMouse();
1916 // Ensure that the editor control is destroyed before the grid is,
1917 // otherwise we crash later when the editor tries to do something with the
1918 // half destroyed grid
1919 HideCellEditControl();
1921 // Must do this or ~wxScrollHelper will pop the wrong event handler
1922 SetTargetWindow(this);
1924 wxSafeDecRef(m_defaultCellAttr
);
1926 #ifdef DEBUG_ATTR_CACHE
1927 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1928 wxPrintf(wxT("wxGrid attribute cache statistics: "
1929 "total: %u, hits: %u (%u%%)\n"),
1930 total
, gs_nAttrCacheHits
,
1931 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1934 // if we own the table, just delete it, otherwise at least don't leave it
1935 // with dangling view pointer
1938 else if ( m_table
&& m_table
->GetView() == this )
1939 m_table
->SetView(NULL
);
1941 delete m_typeRegistry
;
1944 delete m_setFixedRows
;
1945 delete m_setFixedCols
;
1949 // ----- internal init and update functions
1952 // NOTE: If using the default visual attributes works everywhere then this can
1953 // be removed as well as the #else cases below.
1954 #define _USE_VISATTR 0
1956 void wxGrid::Create()
1958 // create the type registry
1959 m_typeRegistry
= new wxGridTypeRegistry
;
1961 m_cellEditCtrlEnabled
= false;
1963 m_defaultCellAttr
= new wxGridCellAttr();
1965 // Set default cell attributes
1966 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1967 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1968 m_defaultCellAttr
->SetFont(GetFont());
1969 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1970 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1971 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1974 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1975 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1977 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1978 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1981 m_defaultCellAttr
->SetTextColour(
1982 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1983 m_defaultCellAttr
->SetBackgroundColour(
1984 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1989 m_currentCellCoords
= wxGridNoCellCoords
;
1991 // subwindow components that make up the wxGrid
1992 m_rowLabelWin
= new wxGridRowLabelWindow(this);
1993 CreateColumnWindow();
1994 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
1995 m_gridWin
= new wxGridWindow( this );
1997 SetTargetWindow( m_gridWin
);
2000 wxColour gfg
= gva
.colFg
;
2001 wxColour gbg
= gva
.colBg
;
2002 wxColour lfg
= lva
.colFg
;
2003 wxColour lbg
= lva
.colBg
;
2005 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2006 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
2007 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2008 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
2011 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
2012 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
2013 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
2014 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
2015 m_colWindow
->SetOwnForegroundColour(lfg
);
2016 m_colWindow
->SetOwnBackgroundColour(lbg
);
2018 m_gridWin
->SetOwnForegroundColour(gfg
);
2019 m_gridWin
->SetOwnBackgroundColour(gbg
);
2021 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2022 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
2024 // now that we have the grid window, use its font to compute the default
2026 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2027 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2028 m_defaultRowHeight
+= 8;
2030 m_defaultRowHeight
+= 4;
2035 void wxGrid::CreateColumnWindow()
2037 if ( m_useNativeHeader
)
2039 m_colWindow
= new wxGridHeaderCtrl(this);
2040 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2042 else // draw labels ourselves
2044 m_colWindow
= new wxGridColLabelWindow(this);
2045 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2049 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2050 wxGridSelectionModes selmode
)
2052 wxCHECK_MSG( !m_created
,
2054 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2056 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2059 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2061 wxCHECK_RET( m_created
,
2062 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2064 m_selection
->SetSelectionMode( selmode
);
2067 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2069 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2070 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2072 return m_selection
->GetSelectionMode();
2076 wxGrid::SetTable(wxGridTableBase
*table
,
2078 wxGrid::wxGridSelectionModes selmode
)
2080 bool checkSelection
= false;
2083 // stop all processing
2088 m_table
->SetView(0);
2100 checkSelection
= true;
2102 // kill row and column size arrays
2103 m_colWidths
.Empty();
2104 m_colRights
.Empty();
2105 m_rowHeights
.Empty();
2106 m_rowBottoms
.Empty();
2111 m_numRows
= table
->GetNumberRows();
2112 m_numCols
= table
->GetNumberCols();
2114 if ( m_useNativeHeader
)
2115 GetGridColHeader()->SetColumnCount(m_numCols
);
2118 m_table
->SetView( this );
2119 m_ownTable
= takeOwnership
;
2120 m_selection
= new wxGridSelection( this, selmode
);
2123 // If the newly set table is smaller than the
2124 // original one current cell and selection regions
2125 // might be invalid,
2126 m_selectedBlockCorner
= wxGridNoCellCoords
;
2127 m_currentCellCoords
=
2128 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2129 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2130 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2131 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2133 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2134 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2137 m_selectedBlockBottomRight
=
2138 wxGridCellCoords(wxMin(m_numRows
,
2139 m_selectedBlockBottomRight
.GetRow()),
2141 m_selectedBlockBottomRight
.GetCol()));
2155 m_cornerLabelWin
= NULL
;
2156 m_rowLabelWin
= NULL
;
2164 m_defaultCellAttr
= NULL
;
2165 m_typeRegistry
= NULL
;
2166 m_winCapture
= NULL
;
2168 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2169 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2172 m_setFixedCols
= NULL
;
2175 m_attrCache
.row
= -1;
2176 m_attrCache
.col
= -1;
2177 m_attrCache
.attr
= NULL
;
2179 m_labelFont
= GetFont();
2180 m_labelFont
.SetWeight( wxBOLD
);
2182 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2183 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2185 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2186 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2187 m_colLabelTextOrientation
= wxHORIZONTAL
;
2189 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2190 m_defaultRowHeight
= 0; // this will be initialized after creation
2192 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2193 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2195 m_gridLineColour
= wxColour( 192,192,192 );
2196 m_gridLinesEnabled
= true;
2197 m_gridLinesClipHorz
=
2198 m_gridLinesClipVert
= true;
2199 m_cellHighlightColour
= *wxBLACK
;
2200 m_cellHighlightPenWidth
= 2;
2201 m_cellHighlightROPenWidth
= 1;
2203 m_canDragColMove
= false;
2205 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2206 m_winCapture
= NULL
;
2207 m_canDragRowSize
= true;
2208 m_canDragColSize
= true;
2209 m_canDragGridSize
= true;
2210 m_canDragCell
= false;
2212 m_dragRowOrCol
= -1;
2213 m_isDragging
= false;
2214 m_startDragPos
= wxDefaultPosition
;
2216 m_sortCol
= wxNOT_FOUND
;
2217 m_sortIsAscending
= true;
2220 m_nativeColumnLabels
= false;
2222 m_waitForSlowClick
= false;
2224 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2225 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2227 m_currentCellCoords
= wxGridNoCellCoords
;
2229 m_selectedBlockTopLeft
=
2230 m_selectedBlockBottomRight
=
2231 m_selectedBlockCorner
= wxGridNoCellCoords
;
2233 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2234 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2236 m_editable
= true; // default for whole grid
2238 m_inOnKeyDown
= false;
2244 m_scrollLineX
= GRID_SCROLL_LINE_X
;
2245 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
2248 // ----------------------------------------------------------------------------
2249 // the idea is to call these functions only when necessary because they create
2250 // quite big arrays which eat memory mostly unnecessary - in particular, if
2251 // default widths/heights are used for all rows/columns, we may not use these
2254 // with some extra code, it should be possible to only store the widths/heights
2255 // different from default ones (resulting in space savings for huge grids) but
2256 // this is not done currently
2257 // ----------------------------------------------------------------------------
2259 void wxGrid::InitRowHeights()
2261 m_rowHeights
.Empty();
2262 m_rowBottoms
.Empty();
2264 m_rowHeights
.Alloc( m_numRows
);
2265 m_rowBottoms
.Alloc( m_numRows
);
2267 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2270 for ( int i
= 0; i
< m_numRows
; i
++ )
2272 rowBottom
+= m_defaultRowHeight
;
2273 m_rowBottoms
.Add( rowBottom
);
2277 void wxGrid::InitColWidths()
2279 m_colWidths
.Empty();
2280 m_colRights
.Empty();
2282 m_colWidths
.Alloc( m_numCols
);
2283 m_colRights
.Alloc( m_numCols
);
2285 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2287 for ( int i
= 0; i
< m_numCols
; i
++ )
2289 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2290 m_colRights
.Add( colRight
);
2294 int wxGrid::GetColWidth(int col
) const
2296 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2299 int wxGrid::GetColLeft(int col
) const
2301 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2302 : m_colRights
[col
] - m_colWidths
[col
];
2305 int wxGrid::GetColRight(int col
) const
2307 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2311 int wxGrid::GetRowHeight(int row
) const
2313 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2316 int wxGrid::GetRowTop(int row
) const
2318 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2319 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2322 int wxGrid::GetRowBottom(int row
) const
2324 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2325 : m_rowBottoms
[row
];
2328 void wxGrid::CalcDimensions()
2330 // compute the size of the scrollable area
2331 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2332 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2337 // take into account editor if shown
2338 if ( IsCellEditControlShown() )
2341 int r
= m_currentCellCoords
.GetRow();
2342 int c
= m_currentCellCoords
.GetCol();
2343 int x
= GetColLeft(c
);
2344 int y
= GetRowTop(r
);
2346 // how big is the editor
2347 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2348 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2349 editor
->GetControl()->GetSize(&w2
, &h2
);
2360 // preserve (more or less) the previous position
2362 GetViewStart( &x
, &y
);
2364 // ensure the position is valid for the new scroll ranges
2366 x
= wxMax( w
- 1, 0 );
2368 y
= wxMax( h
- 1, 0 );
2370 // update the virtual size and refresh the scrollbars to reflect it
2371 m_gridWin
->SetVirtualSize(w
, h
);
2375 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2376 // still must reposition the children
2380 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2382 wxSize
sizeGridWin(size
);
2383 sizeGridWin
.x
-= m_rowLabelWidth
;
2384 sizeGridWin
.y
-= m_colLabelHeight
;
2389 void wxGrid::CalcWindowSizes()
2391 // escape if the window is has not been fully created yet
2393 if ( m_cornerLabelWin
== NULL
)
2397 GetClientSize( &cw
, &ch
);
2399 // the grid may be too small to have enough space for the labels yet, don't
2400 // size the windows to negative sizes in this case
2401 int gw
= cw
- m_rowLabelWidth
;
2402 int gh
= ch
- m_colLabelHeight
;
2408 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2409 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2411 if ( m_colWindow
&& m_colWindow
->IsShown() )
2412 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2414 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2415 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2417 if ( m_gridWin
&& m_gridWin
->IsShown() )
2418 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2421 // this is called when the grid table sends a message
2422 // to indicate that it has been redimensioned
2424 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2427 bool result
= false;
2429 // Clear the attribute cache as the attribute might refer to a different
2430 // cell than stored in the cache after adding/removing rows/columns.
2433 // By the same reasoning, the editor should be dismissed if columns are
2434 // added or removed. And for consistency, it should IMHO always be
2435 // removed, not only if the cell "underneath" it actually changes.
2436 // For now, I intentionally do not save the editor's content as the
2437 // cell it might want to save that stuff to might no longer exist.
2438 HideCellEditControl();
2440 switch ( msg
.GetId() )
2442 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2444 size_t pos
= msg
.GetCommandInt();
2445 int numRows
= msg
.GetCommandInt2();
2447 m_numRows
+= numRows
;
2449 if ( !m_rowHeights
.IsEmpty() )
2451 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2452 m_rowBottoms
.Insert( 0, pos
, numRows
);
2456 bottom
= m_rowBottoms
[pos
- 1];
2458 for ( i
= pos
; i
< m_numRows
; i
++ )
2460 bottom
+= m_rowHeights
[i
];
2461 m_rowBottoms
[i
] = bottom
;
2465 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2467 // if we have just inserted cols into an empty grid the current
2468 // cell will be undefined...
2470 SetCurrentCell( 0, 0 );
2474 m_selection
->UpdateRows( pos
, numRows
);
2475 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2477 attrProvider
->UpdateAttrRows( pos
, numRows
);
2479 if ( !GetBatchCount() )
2482 m_rowLabelWin
->Refresh();
2488 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2490 int numRows
= msg
.GetCommandInt();
2491 int oldNumRows
= m_numRows
;
2492 m_numRows
+= numRows
;
2494 if ( !m_rowHeights
.IsEmpty() )
2496 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2497 m_rowBottoms
.Add( 0, numRows
);
2500 if ( oldNumRows
> 0 )
2501 bottom
= m_rowBottoms
[oldNumRows
- 1];
2503 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2505 bottom
+= m_rowHeights
[i
];
2506 m_rowBottoms
[i
] = bottom
;
2510 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2512 // if we have just inserted cols into an empty grid the current
2513 // cell will be undefined...
2515 SetCurrentCell( 0, 0 );
2518 if ( !GetBatchCount() )
2521 m_rowLabelWin
->Refresh();
2527 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2529 size_t pos
= msg
.GetCommandInt();
2530 int numRows
= msg
.GetCommandInt2();
2531 m_numRows
-= numRows
;
2533 if ( !m_rowHeights
.IsEmpty() )
2535 m_rowHeights
.RemoveAt( pos
, numRows
);
2536 m_rowBottoms
.RemoveAt( pos
, numRows
);
2539 for ( i
= 0; i
< m_numRows
; i
++ )
2541 h
+= m_rowHeights
[i
];
2542 m_rowBottoms
[i
] = h
;
2548 m_currentCellCoords
= wxGridNoCellCoords
;
2552 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2553 m_currentCellCoords
.Set( 0, 0 );
2557 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2558 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2561 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2563 // ifdef'd out following patch from Paul Gammans
2565 // No need to touch column attributes, unless we
2566 // removed _all_ rows, in this case, we remove
2567 // all column attributes.
2568 // I hate to do this here, but the
2569 // needed data is not available inside UpdateAttrRows.
2570 if ( !GetNumberRows() )
2571 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2575 if ( !GetBatchCount() )
2578 m_rowLabelWin
->Refresh();
2584 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2586 size_t pos
= msg
.GetCommandInt();
2587 int numCols
= msg
.GetCommandInt2();
2588 m_numCols
+= numCols
;
2590 if ( m_useNativeHeader
)
2591 GetGridColHeader()->SetColumnCount(m_numCols
);
2593 if ( !m_colAt
.IsEmpty() )
2595 //Shift the column IDs
2597 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2599 if ( m_colAt
[i
] >= (int)pos
)
2600 m_colAt
[i
] += numCols
;
2603 m_colAt
.Insert( pos
, pos
, numCols
);
2605 //Set the new columns' positions
2606 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2612 if ( !m_colWidths
.IsEmpty() )
2614 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2615 m_colRights
.Insert( 0, pos
, numCols
);
2619 right
= m_colRights
[GetColAt( pos
- 1 )];
2622 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2624 i
= GetColAt( colPos
);
2626 right
+= m_colWidths
[i
];
2627 m_colRights
[i
] = right
;
2631 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2633 // if we have just inserted cols into an empty grid the current
2634 // cell will be undefined...
2636 SetCurrentCell( 0, 0 );
2640 m_selection
->UpdateCols( pos
, numCols
);
2641 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2643 attrProvider
->UpdateAttrCols( pos
, numCols
);
2644 if ( !GetBatchCount() )
2647 m_colWindow
->Refresh();
2653 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2655 int numCols
= msg
.GetCommandInt();
2656 int oldNumCols
= m_numCols
;
2657 m_numCols
+= numCols
;
2658 if ( m_useNativeHeader
)
2659 GetGridColHeader()->SetColumnCount(m_numCols
);
2661 if ( !m_colAt
.IsEmpty() )
2663 m_colAt
.Add( 0, numCols
);
2665 //Set the new columns' positions
2667 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2673 if ( !m_colWidths
.IsEmpty() )
2675 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2676 m_colRights
.Add( 0, numCols
);
2679 if ( oldNumCols
> 0 )
2680 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2683 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2685 i
= GetColAt( colPos
);
2687 right
+= m_colWidths
[i
];
2688 m_colRights
[i
] = right
;
2692 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2694 // if we have just inserted cols into an empty grid the current
2695 // cell will be undefined...
2697 SetCurrentCell( 0, 0 );
2699 if ( !GetBatchCount() )
2702 m_colWindow
->Refresh();
2708 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2710 size_t pos
= msg
.GetCommandInt();
2711 int numCols
= msg
.GetCommandInt2();
2712 m_numCols
-= numCols
;
2713 if ( m_useNativeHeader
)
2714 GetGridColHeader()->SetColumnCount(m_numCols
);
2716 if ( !m_colAt
.IsEmpty() )
2718 int colID
= GetColAt( pos
);
2720 m_colAt
.RemoveAt( pos
, numCols
);
2722 //Shift the column IDs
2724 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2726 if ( m_colAt
[colPos
] > colID
)
2727 m_colAt
[colPos
] -= numCols
;
2731 if ( !m_colWidths
.IsEmpty() )
2733 m_colWidths
.RemoveAt( pos
, numCols
);
2734 m_colRights
.RemoveAt( pos
, numCols
);
2738 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2740 i
= GetColAt( colPos
);
2742 w
+= m_colWidths
[i
];
2749 m_currentCellCoords
= wxGridNoCellCoords
;
2753 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2754 m_currentCellCoords
.Set( 0, 0 );
2758 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2759 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2762 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2764 // ifdef'd out following patch from Paul Gammans
2766 // No need to touch row attributes, unless we
2767 // removed _all_ columns, in this case, we remove
2768 // all row attributes.
2769 // I hate to do this here, but the
2770 // needed data is not available inside UpdateAttrCols.
2771 if ( !GetNumberCols() )
2772 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2776 if ( !GetBatchCount() )
2779 m_colWindow
->Refresh();
2786 if (result
&& !GetBatchCount() )
2787 m_gridWin
->Refresh();
2792 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2794 wxRegionIterator
iter( reg
);
2797 wxArrayInt rowlabels
;
2804 // TODO: remove this when we can...
2805 // There is a bug in wxMotif that gives garbage update
2806 // rectangles if you jump-scroll a long way by clicking the
2807 // scrollbar with middle button. This is a work-around
2809 #if defined(__WXMOTIF__)
2811 m_gridWin
->GetClientSize( &cw
, &ch
);
2812 if ( r
.GetTop() > ch
)
2814 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2817 // logical bounds of update region
2820 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2821 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2823 // find the row labels within these bounds
2826 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2828 if ( GetRowBottom(row
) < top
)
2831 if ( GetRowTop(row
) > bottom
)
2834 rowlabels
.Add( row
);
2843 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2845 wxRegionIterator
iter( reg
);
2848 wxArrayInt colLabels
;
2855 // TODO: remove this when we can...
2856 // There is a bug in wxMotif that gives garbage update
2857 // rectangles if you jump-scroll a long way by clicking the
2858 // scrollbar with middle button. This is a work-around
2860 #if defined(__WXMOTIF__)
2862 m_gridWin
->GetClientSize( &cw
, &ch
);
2863 if ( r
.GetLeft() > cw
)
2865 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2868 // logical bounds of update region
2871 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2872 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2874 // find the cells within these bounds
2878 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2880 col
= GetColAt( colPos
);
2882 if ( GetColRight(col
) < left
)
2885 if ( GetColLeft(col
) > right
)
2888 colLabels
.Add( col
);
2897 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2899 wxRegionIterator
iter( reg
);
2902 wxGridCellCoordsArray cellsExposed
;
2904 int left
, top
, right
, bottom
;
2909 // TODO: remove this when we can...
2910 // There is a bug in wxMotif that gives garbage update
2911 // rectangles if you jump-scroll a long way by clicking the
2912 // scrollbar with middle button. This is a work-around
2914 #if defined(__WXMOTIF__)
2916 m_gridWin
->GetClientSize( &cw
, &ch
);
2917 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2918 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2919 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2920 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2923 // logical bounds of update region
2925 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2926 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2928 // find the cells within these bounds
2930 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2932 if ( GetRowBottom(row
) <= top
)
2935 if ( GetRowTop(row
) > bottom
)
2938 // add all dirty cells in this row: notice that the columns which
2939 // are dirty don't depend on the row so we compute them only once
2940 // for the first dirty row and then reuse for all the next ones
2943 // do determine the dirty columns
2944 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2945 cols
.push_back(GetColAt(pos
));
2947 // if there are no dirty columns at all, nothing to do
2952 const size_t count
= cols
.size();
2953 for ( size_t n
= 0; n
< count
; n
++ )
2954 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2960 return cellsExposed
;
2964 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2967 wxPoint
pos( event
.GetPosition() );
2968 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2970 if ( event
.Dragging() )
2974 m_isDragging
= true;
2975 m_rowLabelWin
->CaptureMouse();
2978 if ( event
.LeftIsDown() )
2980 switch ( m_cursorMode
)
2982 case WXGRID_CURSOR_RESIZE_ROW
:
2984 int cw
, ch
, left
, dummy
;
2985 m_gridWin
->GetClientSize( &cw
, &ch
);
2986 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2988 wxClientDC
dc( m_gridWin
);
2991 GetRowTop(m_dragRowOrCol
) +
2992 GetRowMinimalHeight(m_dragRowOrCol
) );
2993 dc
.SetLogicalFunction(wxINVERT
);
2994 if ( m_dragLastPos
>= 0 )
2996 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2998 dc
.DrawLine( left
, y
, left
+cw
, y
);
3003 case WXGRID_CURSOR_SELECT_ROW
:
3005 if ( (row
= YToRow( y
)) >= 0 )
3008 m_selection
->SelectRow(row
, event
);
3013 // default label to suppress warnings about "enumeration value
3014 // 'xxx' not handled in switch
3022 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3027 if (m_rowLabelWin
->HasCapture())
3028 m_rowLabelWin
->ReleaseMouse();
3029 m_isDragging
= false;
3032 // ------------ Entering or leaving the window
3034 if ( event
.Entering() || event
.Leaving() )
3036 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3039 // ------------ Left button pressed
3041 else if ( event
.LeftDown() )
3043 row
= YToEdgeOfRow(y
);
3044 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3046 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3048 else // not a request to start resizing
3052 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3054 if ( !event
.ShiftDown() && !event
.CmdDown() )
3058 if ( event
.ShiftDown() )
3060 m_selection
->SelectBlock
3062 m_currentCellCoords
.GetRow(), 0,
3063 row
, GetNumberCols() - 1,
3069 m_selection
->SelectRow(row
, event
);
3073 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3078 // ------------ Left double click
3080 else if (event
.LeftDClick() )
3082 row
= YToEdgeOfRow(y
);
3083 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3085 // adjust row height depending on label text
3087 // TODO: generate RESIZING event, see #10754
3088 AutoSizeRowLabelSize( row
);
3090 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3095 else // not on row separator or it's not resizeable
3099 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3101 // no default action at the moment
3106 // ------------ Left button released
3108 else if ( event
.LeftUp() )
3110 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3111 DoEndDragResizeRow(event
);
3113 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3117 // ------------ Right button down
3119 else if ( event
.RightDown() )
3123 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3125 // no default action at the moment
3129 // ------------ Right double click
3131 else if ( event
.RightDClick() )
3135 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3137 // no default action at the moment
3141 // ------------ No buttons down and mouse moving
3143 else if ( event
.Moving() )
3145 m_dragRowOrCol
= YToEdgeOfRow( y
);
3146 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3148 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3150 if ( CanDragRowSize(m_dragRowOrCol
) )
3151 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3154 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3156 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3161 void wxGrid::UpdateColumnSortingIndicator(int col
)
3163 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3165 if ( m_useNativeHeader
)
3166 GetGridColHeader()->UpdateColumn(col
);
3167 else if ( m_nativeColumnLabels
)
3168 m_colWindow
->Refresh();
3169 //else: sorting indicator display not yet implemented in grid version
3172 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3174 if ( col
== m_sortCol
)
3176 // we are already using this column for sorting (or not sorting at all)
3177 // but we might still change the sorting order, check for it
3178 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3180 m_sortIsAscending
= ascending
;
3182 UpdateColumnSortingIndicator(m_sortCol
);
3185 else // we're changing the column used for sorting
3187 const int sortColOld
= m_sortCol
;
3189 // change it before updating the column as we want GetSortingColumn()
3190 // to return the correct new value
3193 if ( sortColOld
!= wxNOT_FOUND
)
3194 UpdateColumnSortingIndicator(sortColOld
);
3196 if ( m_sortCol
!= wxNOT_FOUND
)
3198 m_sortIsAscending
= ascending
;
3199 UpdateColumnSortingIndicator(m_sortCol
);
3204 void wxGrid::DoColHeaderClick(int col
)
3206 // we consider that the grid was resorted if this event is processed and
3208 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3210 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3215 void wxGrid::DoStartResizeCol(int col
)
3217 m_dragRowOrCol
= col
;
3219 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3222 void wxGrid::DoUpdateResizeCol(int x
)
3224 int cw
, ch
, dummy
, top
;
3225 m_gridWin
->GetClientSize( &cw
, &ch
);
3226 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3228 wxClientDC
dc( m_gridWin
);
3231 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3232 dc
.SetLogicalFunction(wxINVERT
);
3233 if ( m_dragLastPos
>= 0 )
3235 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3237 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3241 void wxGrid::DoUpdateResizeColWidth(int w
)
3243 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3246 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3249 wxPoint
pos( event
.GetPosition() );
3250 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3252 int col
= XToCol(x
);
3253 if ( event
.Dragging() )
3257 m_isDragging
= true;
3258 GetColLabelWindow()->CaptureMouse();
3260 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3261 DoStartMoveCol(col
);
3264 if ( event
.LeftIsDown() )
3266 switch ( m_cursorMode
)
3268 case WXGRID_CURSOR_RESIZE_COL
:
3269 DoUpdateResizeCol(x
);
3272 case WXGRID_CURSOR_SELECT_COL
:
3277 m_selection
->SelectCol(col
, event
);
3282 case WXGRID_CURSOR_MOVE_COL
:
3284 int posNew
= XToPos(x
);
3285 int colNew
= GetColAt(posNew
);
3287 // determine the position of the drop marker
3289 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3290 markerX
= GetColRight(colNew
);
3292 markerX
= GetColLeft(colNew
);
3294 if ( markerX
!= m_dragLastPos
)
3296 wxClientDC
dc( GetColLabelWindow() );
3300 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3304 //Clean up the last indicator
3305 if ( m_dragLastPos
>= 0 )
3307 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3309 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3310 dc
.SetPen(wxNullPen
);
3312 if ( XToCol( m_dragLastPos
) != -1 )
3313 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3316 const wxColour
*color
;
3317 //Moving to the same place? Don't draw a marker
3318 if ( colNew
== m_dragRowOrCol
)
3319 color
= wxLIGHT_GREY
;
3324 wxPen
pen( *color
, 2 );
3327 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3329 dc
.SetPen(wxNullPen
);
3331 m_dragLastPos
= markerX
- 1;
3336 // default label to suppress warnings about "enumeration value
3337 // 'xxx' not handled in switch
3345 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3350 if (GetColLabelWindow()->HasCapture())
3351 GetColLabelWindow()->ReleaseMouse();
3352 m_isDragging
= false;
3355 // ------------ Entering or leaving the window
3357 if ( event
.Entering() || event
.Leaving() )
3359 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3362 // ------------ Left button pressed
3364 else if ( event
.LeftDown() )
3366 int col
= XToEdgeOfCol(x
);
3367 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3369 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3371 else // not a request to start resizing
3375 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3377 if ( m_canDragColMove
)
3379 //Show button as pressed
3380 wxClientDC
dc( GetColLabelWindow() );
3381 int colLeft
= GetColLeft( col
);
3382 int colRight
= GetColRight( col
) - 1;
3383 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3384 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3385 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3387 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3391 if ( !event
.ShiftDown() && !event
.CmdDown() )
3395 if ( event
.ShiftDown() )
3397 m_selection
->SelectBlock
3399 0, m_currentCellCoords
.GetCol(),
3400 GetNumberRows() - 1, col
,
3406 m_selection
->SelectCol(col
, event
);
3410 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3416 // ------------ Left double click
3418 if ( event
.LeftDClick() )
3420 const int colEdge
= XToEdgeOfCol(x
);
3421 if ( colEdge
== -1 )
3424 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3426 // no default action at the moment
3431 // adjust column width depending on label text
3433 // TODO: generate RESIZING event, see #10754
3434 AutoSizeColLabelSize( colEdge
);
3436 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3438 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3443 // ------------ Left button released
3445 else if ( event
.LeftUp() )
3447 switch ( m_cursorMode
)
3449 case WXGRID_CURSOR_RESIZE_COL
:
3450 DoEndDragResizeCol(event
);
3453 case WXGRID_CURSOR_MOVE_COL
:
3454 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3456 // the column didn't actually move anywhere
3458 DoColHeaderClick(col
);
3459 m_colWindow
->Refresh(); // "unpress" the column
3463 DoEndMoveCol(XToPos(x
));
3467 case WXGRID_CURSOR_SELECT_COL
:
3468 case WXGRID_CURSOR_SELECT_CELL
:
3469 case WXGRID_CURSOR_RESIZE_ROW
:
3470 case WXGRID_CURSOR_SELECT_ROW
:
3472 DoColHeaderClick(col
);
3476 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3480 // ------------ Right button down
3482 else if ( event
.RightDown() )
3485 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3487 // no default action at the moment
3491 // ------------ Right double click
3493 else if ( event
.RightDClick() )
3496 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3498 // no default action at the moment
3502 // ------------ No buttons down and mouse moving
3504 else if ( event
.Moving() )
3506 m_dragRowOrCol
= XToEdgeOfCol( x
);
3507 if ( m_dragRowOrCol
>= 0 )
3509 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3511 if ( CanDragColSize(m_dragRowOrCol
) )
3512 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3515 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3517 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3522 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3524 if ( event
.LeftDown() )
3526 // indicate corner label by having both row and
3529 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3534 else if ( event
.LeftDClick() )
3536 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3538 else if ( event
.RightDown() )
3540 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3542 // no default action at the moment
3545 else if ( event
.RightDClick() )
3547 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3549 // no default action at the moment
3554 void wxGrid::CancelMouseCapture()
3556 // cancel operation currently in progress, whatever it is
3559 m_isDragging
= false;
3560 m_startDragPos
= wxDefaultPosition
;
3562 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3563 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3564 m_winCapture
= NULL
;
3566 // remove traces of whatever we drew on screen
3571 void wxGrid::ChangeCursorMode(CursorMode mode
,
3576 static const wxChar
*cursorModes
[] =
3586 wxLogTrace(wxT("grid"),
3587 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3588 win
== m_colWindow
? wxT("colLabelWin")
3589 : win
? wxT("rowLabelWin")
3591 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3592 #endif // wxUSE_LOG_TRACE
3594 if ( mode
== m_cursorMode
&&
3595 win
== m_winCapture
&&
3596 captureMouse
== (m_winCapture
!= NULL
))
3601 // by default use the grid itself
3607 m_winCapture
->ReleaseMouse();
3608 m_winCapture
= NULL
;
3611 m_cursorMode
= mode
;
3613 switch ( m_cursorMode
)
3615 case WXGRID_CURSOR_RESIZE_ROW
:
3616 win
->SetCursor( m_rowResizeCursor
);
3619 case WXGRID_CURSOR_RESIZE_COL
:
3620 win
->SetCursor( m_colResizeCursor
);
3623 case WXGRID_CURSOR_MOVE_COL
:
3624 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3628 win
->SetCursor( *wxSTANDARD_CURSOR
);
3632 // we need to capture mouse when resizing
3633 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3634 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3636 if ( captureMouse
&& resize
)
3638 win
->CaptureMouse();
3643 // ----------------------------------------------------------------------------
3644 // grid mouse event processing
3645 // ----------------------------------------------------------------------------
3648 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3649 const wxGridCellCoords
& coords
,
3652 if ( coords
== wxGridNoCellCoords
)
3653 return; // we're outside any valid cell
3655 // Hide the edit control, so it won't interfere with drag-shrinking.
3656 if ( IsCellEditControlShown() )
3658 HideCellEditControl();
3659 SaveEditControlValue();
3662 switch ( event
.GetModifiers() )
3665 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3666 m_selectedBlockCorner
= coords
;
3667 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3671 if ( CanDragCell() )
3675 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3676 m_selectedBlockCorner
= coords
;
3678 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3683 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3687 // we don't handle the other key modifiers
3692 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3694 wxClientDC
dc(m_gridWin
);
3696 dc
.SetLogicalFunction(wxINVERT
);
3698 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3699 m_gridWin
->GetClientSize());
3701 // erase the previously drawn line, if any
3702 if ( m_dragLastPos
>= 0 )
3703 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3705 // we need the vertical position for rows and horizontal for columns here
3706 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3708 // don't allow resizing beneath the minimal size
3709 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3710 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3711 if ( m_dragLastPos
< posMin
)
3712 m_dragLastPos
= posMin
;
3714 // and draw it at the new position
3715 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3718 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3720 if ( !m_isDragging
)
3722 // Don't start doing anything until the mouse has been dragged far
3724 const wxPoint
& pt
= event
.GetPosition();
3725 if ( m_startDragPos
== wxDefaultPosition
)
3727 m_startDragPos
= pt
;
3731 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3732 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3736 const bool isFirstDrag
= !m_isDragging
;
3737 m_isDragging
= true;
3739 switch ( m_cursorMode
)
3741 case WXGRID_CURSOR_SELECT_CELL
:
3742 DoGridCellDrag(event
, coords
, isFirstDrag
);
3745 case WXGRID_CURSOR_RESIZE_ROW
:
3746 DoGridLineDrag(event
, wxGridRowOperations());
3749 case WXGRID_CURSOR_RESIZE_COL
:
3750 DoGridLineDrag(event
, wxGridColumnOperations());
3759 m_winCapture
= m_gridWin
;
3760 m_winCapture
->CaptureMouse();
3765 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3766 const wxGridCellCoords
& coords
,
3769 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3771 // event handled by user code, no need to do anything here
3775 if ( !event
.CmdDown() )
3778 if ( event
.ShiftDown() )
3782 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3783 m_selectedBlockCorner
= coords
;
3786 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3788 DisableCellEditControl();
3789 MakeCellVisible( coords
);
3791 if ( event
.CmdDown() )
3795 m_selection
->ToggleCellSelection(coords
, event
);
3798 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3799 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3800 m_selectedBlockCorner
= coords
;
3804 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3805 coords
!= wxGridNoCellCoords
;
3806 SetCurrentCell( coords
);
3812 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3813 const wxGridCellCoords
& coords
,
3816 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3818 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3820 // we want double click to select a cell and start editing
3821 // (i.e. to behave in same way as sequence of two slow clicks):
3822 m_waitForSlowClick
= true;
3828 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3830 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3834 m_winCapture
->ReleaseMouse();
3835 m_winCapture
= NULL
;
3838 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3841 EnableCellEditControl();
3843 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3844 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3845 editor
->StartingClick();
3849 m_waitForSlowClick
= false;
3851 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3852 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3856 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3857 m_selectedBlockBottomRight
,
3861 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3862 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3864 // Show the edit control, if it has been hidden for
3866 ShowCellEditControl();
3869 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3871 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3872 DoEndDragResizeRow(event
);
3874 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3876 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3877 DoEndDragResizeCol(event
);
3884 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3885 const wxGridCellCoords
& coords
,
3888 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3890 // out of grid cell area
3891 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3895 int dragRow
= YToEdgeOfRow( pos
.y
);
3896 int dragCol
= XToEdgeOfCol( pos
.x
);
3898 // Dragging on the corner of a cell to resize in both
3899 // directions is not implemented yet...
3901 if ( dragRow
>= 0 && dragCol
>= 0 )
3903 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3907 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3909 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3911 m_dragRowOrCol
= dragRow
;
3912 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3915 // When using the native header window we can only resize the columns by
3916 // dragging the dividers in it because we can't make it enter into the
3917 // column resizing mode programmatically
3918 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3919 CanDragGridSize() && CanDragColSize(dragCol
) )
3921 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3923 m_dragRowOrCol
= dragCol
;
3924 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3927 else // Neither on a row or col edge
3929 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3931 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3936 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
3938 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
3940 // coordinates of the cell under mouse
3941 wxGridCellCoords coords
= XYToCell(pos
);
3943 int cell_rows
, cell_cols
;
3944 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
3945 if ( (cell_rows
< 0) || (cell_cols
< 0) )
3947 coords
.SetRow(coords
.GetRow() + cell_rows
);
3948 coords
.SetCol(coords
.GetCol() + cell_cols
);
3951 if ( event
.Dragging() )
3953 if ( event
.LeftIsDown() )
3954 DoGridDragEvent(event
, coords
);
3960 m_isDragging
= false;
3961 m_startDragPos
= wxDefaultPosition
;
3963 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3964 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3967 if ( event
.Entering() || event
.Leaving() )
3969 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3970 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3974 // deal with various button presses
3975 if ( event
.IsButton() )
3977 if ( coords
!= wxGridNoCellCoords
)
3979 DisableCellEditControl();
3981 if ( event
.LeftDown() )
3982 DoGridCellLeftDown(event
, coords
, pos
);
3983 else if ( event
.LeftDClick() )
3984 DoGridCellLeftDClick(event
, coords
, pos
);
3985 else if ( event
.RightDown() )
3986 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
3987 else if ( event
.RightDClick() )
3988 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
3991 // this one should be called even if we're not over any cell
3992 if ( event
.LeftUp() )
3994 DoGridCellLeftUp(event
, coords
);
3997 else if ( event
.Moving() )
3999 DoGridMouseMoveEvent(event
, coords
, pos
);
4001 else // unknown mouse event?
4007 // this function returns true only if the size really changed
4008 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4010 if ( m_dragLastPos
== -1 )
4013 const wxGridOperations
& doper
= oper
.Dual();
4015 const wxSize size
= m_gridWin
->GetClientSize();
4017 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4019 // erase the last line we drew
4020 wxClientDC
dc(m_gridWin
);
4022 dc
.SetLogicalFunction(wxINVERT
);
4024 const int posLineStart
= oper
.Select(ptOrigin
);
4025 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4027 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4029 // temporarily hide the edit control before resizing
4030 HideCellEditControl();
4031 SaveEditControlValue();
4033 // do resize the line
4034 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4035 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4036 oper
.SetLineSize(this, m_dragRowOrCol
,
4037 wxMax(m_dragLastPos
- lineStart
,
4038 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4040 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4044 // refresh now if we're not frozen
4045 if ( !GetBatchCount() )
4047 // we need to refresh everything beyond the resized line in the header
4050 // get the position from which to refresh in the other direction
4051 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4052 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4054 // we only need the ordinate (for rows) or abscissa (for columns) here,
4055 // and need to cover the entire window in the other direction
4056 oper
.Select(rect
) = 0;
4058 wxRect
rectHeader(rect
.GetPosition(),
4061 oper
.GetHeaderWindowSize(this),
4062 doper
.Select(size
) - doper
.Select(rect
)
4065 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4068 // also refresh the grid window: extend the rectangle
4071 oper
.SelectSize(rect
) = oper
.Select(size
);
4073 int subtractLines
= 0;
4074 const int lineStart
= oper
.PosToLine(this, posLineStart
);
4075 if ( lineStart
>= 0 )
4077 // ensure that if we have a multi-cell block we redraw all of
4078 // it by increasing the refresh area to cover it entirely if a
4079 // part of it is affected
4080 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
4081 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
4083 int cellLines
= oper
.Select(
4084 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4085 if ( cellLines
< subtractLines
)
4086 subtractLines
= cellLines
;
4091 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4092 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4094 doper
.Select(rect
) = startPos
;
4095 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4097 m_gridWin
->Refresh(false, &rect
);
4101 // show the edit control back again
4102 ShowCellEditControl();
4107 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4109 // TODO: generate RESIZING event, see #10754
4111 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4112 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4115 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4117 // TODO: generate RESIZING event, see #10754
4119 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4120 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4123 void wxGrid::DoStartMoveCol(int col
)
4125 m_dragRowOrCol
= col
;
4128 void wxGrid::DoEndMoveCol(int pos
)
4130 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4132 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4133 SetColPos(m_dragRowOrCol
, pos
);
4134 //else: vetoed by user
4136 m_dragRowOrCol
= -1;
4139 void wxGrid::RefreshAfterColPosChange()
4141 // recalculate the column rights as the column positions have changed,
4142 // unless we calculate them dynamically because all columns widths are the
4143 // same and it's easy to do
4144 if ( !m_colWidths
.empty() )
4147 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4149 int colID
= GetColAt( colPos
);
4151 colRight
+= m_colWidths
[colID
];
4152 m_colRights
[colID
] = colRight
;
4156 // and make the changes visible
4157 if ( m_useNativeHeader
)
4159 if ( m_colAt
.empty() )
4160 GetGridColHeader()->ResetColumnsOrder();
4162 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4166 m_colWindow
->Refresh();
4168 m_gridWin
->Refresh();
4171 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4175 RefreshAfterColPosChange();
4178 void wxGrid::SetColPos(int idx
, int pos
)
4180 // we're going to need m_colAt now, initialize it if needed
4181 if ( m_colAt
.empty() )
4183 m_colAt
.reserve(m_numCols
);
4184 for ( int i
= 0; i
< m_numCols
; i
++ )
4185 m_colAt
.push_back(i
);
4188 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4190 RefreshAfterColPosChange();
4193 void wxGrid::ResetColPos()
4197 RefreshAfterColPosChange();
4200 void wxGrid::EnableDragColMove( bool enable
)
4202 if ( m_canDragColMove
== enable
)
4205 if ( m_useNativeHeader
)
4207 // update all columns to make them [not] reorderable
4208 GetGridColHeader()->SetColumnCount(m_numCols
);
4211 m_canDragColMove
= enable
;
4213 // we use to call ResetColPos() from here if !enable but this doesn't seem
4214 // right as it would mean there would be no way to "freeze" the current
4215 // columns order by disabling moving them after putting them in the desired
4216 // order, whereas now you can always call ResetColPos() manually if needed
4221 // ------ interaction with data model
4223 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4225 switch ( msg
.GetId() )
4227 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4228 return GetModelValues();
4230 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4231 return SetModelValues();
4233 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4234 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4235 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4236 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4237 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4238 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4239 return Redimension( msg
);
4246 // The behaviour of this function depends on the grid table class
4247 // Clear() function. For the default wxGridStringTable class the
4248 // behaviour is to replace all cell contents with wxEmptyString but
4249 // not to change the number of rows or cols.
4251 void wxGrid::ClearGrid()
4255 if (IsCellEditControlEnabled())
4256 DisableCellEditControl();
4259 if (!GetBatchCount())
4260 m_gridWin
->Refresh();
4265 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4266 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4268 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4273 if ( IsCellEditControlEnabled() )
4274 DisableCellEditControl();
4276 return (m_table
->*funcModify
)(pos
, num
);
4278 // the table will have sent the results of the insert row
4279 // operation to this view object as a grid table message
4283 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4284 int num
, bool WXUNUSED(updateLabels
))
4286 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4291 return (m_table
->*funcAppend
)(num
);
4294 // ----------------------------------------------------------------------------
4295 // event generation helpers
4296 // ----------------------------------------------------------------------------
4299 wxGrid::SendGridSizeEvent(wxEventType type
,
4301 const wxMouseEvent
& mouseEv
)
4303 int rowOrCol
= row
== -1 ? col
: row
;
4305 wxGridSizeEvent
gridEvt( GetId(),
4309 mouseEv
.GetX() + GetRowLabelSize(),
4310 mouseEv
.GetY() + GetColLabelSize(),
4313 GetEventHandler()->ProcessEvent(gridEvt
);
4316 // Generate a grid event based on a mouse event and return:
4317 // -1 if the event was vetoed
4318 // +1 if the event was processed (but not vetoed)
4319 // 0 if the event wasn't handled
4321 wxGrid::SendEvent(const wxEventType type
,
4323 const wxMouseEvent
& mouseEv
)
4325 bool claimed
, vetoed
;
4327 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4329 // Right now, it should _never_ end up here!
4330 wxGridRangeSelectEvent
gridEvt( GetId(),
4333 m_selectedBlockTopLeft
,
4334 m_selectedBlockBottomRight
,
4338 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4339 vetoed
= !gridEvt
.IsAllowed();
4341 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4342 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4343 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4344 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4346 wxPoint pos
= mouseEv
.GetPosition();
4348 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4349 pos
.y
+= GetColLabelSize();
4350 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4351 pos
.x
+= GetRowLabelSize();
4353 wxGridEvent
gridEvt( GetId(),
4361 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4362 vetoed
= !gridEvt
.IsAllowed();
4366 wxGridEvent
gridEvt( GetId(),
4370 mouseEv
.GetX() + GetRowLabelSize(),
4371 mouseEv
.GetY() + GetColLabelSize(),
4374 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4375 vetoed
= !gridEvt
.IsAllowed();
4378 // A Veto'd event may not be `claimed' so test this first
4382 return claimed
? 1 : 0;
4385 // Generate a grid event of specified type, return value same as above
4388 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4390 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4391 gridEvt
.SetString(s
);
4393 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4395 // A Veto'd event may not be `claimed' so test this first
4396 if ( !gridEvt
.IsAllowed() )
4399 return claimed
? 1 : 0;
4402 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4404 // needed to prevent zillions of paint events on MSW
4408 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4410 // Don't do anything if between Begin/EndBatch...
4411 // EndBatch() will do all this on the last nested one anyway.
4412 if ( m_created
&& !GetBatchCount() )
4414 // Refresh to get correct scrolled position:
4415 wxScrolledWindow::Refresh(eraseb
, rect
);
4419 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4420 int width_label
, width_cell
, height_label
, height_cell
;
4423 // Copy rectangle can get scroll offsets..
4424 rect_x
= rect
->GetX();
4425 rect_y
= rect
->GetY();
4426 rectWidth
= rect
->GetWidth();
4427 rectHeight
= rect
->GetHeight();
4429 width_label
= m_rowLabelWidth
- rect_x
;
4430 if (width_label
> rectWidth
)
4431 width_label
= rectWidth
;
4433 height_label
= m_colLabelHeight
- rect_y
;
4434 if (height_label
> rectHeight
)
4435 height_label
= rectHeight
;
4437 if (rect_x
> m_rowLabelWidth
)
4439 x
= rect_x
- m_rowLabelWidth
;
4440 width_cell
= rectWidth
;
4445 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4448 if (rect_y
> m_colLabelHeight
)
4450 y
= rect_y
- m_colLabelHeight
;
4451 height_cell
= rectHeight
;
4456 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4459 // Paint corner label part intersecting rect.
4460 if ( width_label
> 0 && height_label
> 0 )
4462 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4463 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4466 // Paint col labels part intersecting rect.
4467 if ( width_cell
> 0 && height_label
> 0 )
4469 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4470 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4473 // Paint row labels part intersecting rect.
4474 if ( width_label
> 0 && height_cell
> 0 )
4476 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4477 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4480 // Paint cell area part intersecting rect.
4481 if ( width_cell
> 0 && height_cell
> 0 )
4483 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4484 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4489 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4490 m_colWindow
->Refresh(eraseb
, NULL
);
4491 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4492 m_gridWin
->Refresh(eraseb
, NULL
);
4497 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4499 if (m_targetWindow
!= this) // check whether initialisation has been done
4501 // reposition our children windows
4506 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4508 if ( m_inOnKeyDown
)
4510 // shouldn't be here - we are going round in circles...
4512 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4515 m_inOnKeyDown
= true;
4517 // propagate the event up and see if it gets processed
4518 wxWindow
*parent
= GetParent();
4519 wxKeyEvent
keyEvt( event
);
4520 keyEvt
.SetEventObject( parent
);
4522 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4524 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4526 if (event
.GetKeyCode() == WXK_RIGHT
)
4527 event
.m_keyCode
= WXK_LEFT
;
4528 else if (event
.GetKeyCode() == WXK_LEFT
)
4529 event
.m_keyCode
= WXK_RIGHT
;
4532 // try local handlers
4533 switch ( event
.GetKeyCode() )
4536 if ( event
.ControlDown() )
4537 MoveCursorUpBlock( event
.ShiftDown() );
4539 MoveCursorUp( event
.ShiftDown() );
4543 if ( event
.ControlDown() )
4544 MoveCursorDownBlock( event
.ShiftDown() );
4546 MoveCursorDown( event
.ShiftDown() );
4550 if ( event
.ControlDown() )
4551 MoveCursorLeftBlock( event
.ShiftDown() );
4553 MoveCursorLeft( event
.ShiftDown() );
4557 if ( event
.ControlDown() )
4558 MoveCursorRightBlock( event
.ShiftDown() );
4560 MoveCursorRight( event
.ShiftDown() );
4564 case WXK_NUMPAD_ENTER
:
4565 if ( event
.ControlDown() )
4567 event
.Skip(); // to let the edit control have the return
4571 if ( GetGridCursorRow() < GetNumberRows()-1 )
4573 MoveCursorDown( event
.ShiftDown() );
4577 // at the bottom of a column
4578 DisableCellEditControl();
4588 if (event
.ShiftDown())
4590 if ( GetGridCursorCol() > 0 )
4592 MoveCursorLeft( false );
4597 DisableCellEditControl();
4602 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4604 MoveCursorRight( false );
4609 DisableCellEditControl();
4615 if ( event
.ControlDown() )
4626 if ( event
.ControlDown() )
4628 GoToCell(m_numRows
- 1, m_numCols
- 1);
4645 // Ctrl-Space selects the current column, Shift-Space -- the
4646 // current row and Ctrl-Shift-Space -- everything
4647 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4650 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4654 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4657 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4658 m_selection
->SelectBlock(0, 0,
4659 m_numRows
- 1, m_numCols
- 1);
4663 if ( !IsEditable() )
4665 MoveCursorRight(false);
4668 //else: fall through
4681 m_inOnKeyDown
= false;
4684 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4686 // try local handlers
4688 if ( event
.GetKeyCode() == WXK_SHIFT
)
4690 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4691 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4695 m_selection
->SelectBlock(
4696 m_selectedBlockTopLeft
,
4697 m_selectedBlockBottomRight
,
4702 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4703 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4704 m_selectedBlockCorner
= wxGridNoCellCoords
;
4708 void wxGrid::OnChar( wxKeyEvent
& event
)
4710 // is it possible to edit the current cell at all?
4711 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4713 // yes, now check whether the cells editor accepts the key
4714 int row
= m_currentCellCoords
.GetRow();
4715 int col
= m_currentCellCoords
.GetCol();
4716 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4717 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4719 // <F2> is special and will always start editing, for
4720 // other keys - ask the editor itself
4721 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4722 || editor
->IsAcceptedKey(event
) )
4724 // ensure cell is visble
4725 MakeCellVisible(row
, col
);
4726 EnableCellEditControl();
4728 // a problem can arise if the cell is not completely
4729 // visible (even after calling MakeCellVisible the
4730 // control is not created and calling StartingKey will
4732 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4733 editor
->StartingKey(event
);
4749 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4753 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4755 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4757 // the event has been vetoed - do nothing
4761 #if !defined(__WXMAC__)
4762 wxClientDC
dc( m_gridWin
);
4766 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4768 DisableCellEditControl();
4770 if ( IsVisible( m_currentCellCoords
, false ) )
4773 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4774 if ( !m_gridLinesEnabled
)
4782 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4784 // Otherwise refresh redraws the highlight!
4785 m_currentCellCoords
= coords
;
4787 #if defined(__WXMAC__)
4788 m_gridWin
->Refresh(true /*, & r */);
4790 DrawGridCellArea( dc
, cells
);
4791 DrawAllGridLines( dc
, r
);
4796 m_currentCellCoords
= coords
;
4798 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4799 #if !defined(__WXMAC__)
4800 DrawCellHighlight( dc
, attr
);
4808 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4809 int bottomRow
, int rightCol
)
4813 switch ( m_selection
->GetSelectionMode() )
4816 wxFAIL_MSG( "unknown selection mode" );
4819 case wxGridSelectCells
:
4820 // arbitrary blocks selection allowed so just use the cell
4821 // coordinates as is
4824 case wxGridSelectRows
:
4825 // only full rows selection allowd, ensure that we do select
4828 rightCol
= GetNumberCols() - 1;
4831 case wxGridSelectColumns
:
4832 // same as above but for columns
4834 bottomRow
= GetNumberRows() - 1;
4837 case wxGridSelectRowsOrColumns
:
4838 // in this mode we can select only full rows or full columns so
4839 // it doesn't make sense to select blocks at all (and we can't
4840 // extend the block because there is no preferred direction, we
4841 // could only extend it to cover the entire grid but this is
4847 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4848 MakeCellVisible(m_selectedBlockCorner
);
4850 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4851 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4853 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4854 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4856 // First the case that we selected a completely new area
4857 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4858 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4861 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4862 wxGridCellCoords ( bottomRow
, rightCol
) );
4863 m_gridWin
->Refresh( false, &rect
);
4866 // Now handle changing an existing selection area.
4867 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4868 m_selectedBlockBottomRight
!= updateBottomRight
)
4870 // Compute two optimal update rectangles:
4871 // Either one rectangle is a real subset of the
4872 // other, or they are (almost) disjoint!
4874 bool need_refresh
[4];
4878 need_refresh
[3] = false;
4881 // Store intermediate values
4882 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4883 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4884 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4885 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4887 // Determine the outer/inner coordinates.
4888 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4889 EnsureFirstLessThanSecond(oldTop
, topRow
);
4890 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4891 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4893 // Now, either the stuff marked old is the outer
4894 // rectangle or we don't have a situation where one
4895 // is contained in the other.
4897 if ( oldLeft
< leftCol
)
4899 // Refresh the newly selected or deselected
4900 // area to the left of the old or new selection.
4901 need_refresh
[0] = true;
4902 rect
[0] = BlockToDeviceRect(
4903 wxGridCellCoords( oldTop
, oldLeft
),
4904 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4907 if ( oldTop
< topRow
)
4909 // Refresh the newly selected or deselected
4910 // area above the old or new selection.
4911 need_refresh
[1] = true;
4912 rect
[1] = BlockToDeviceRect(
4913 wxGridCellCoords( oldTop
, leftCol
),
4914 wxGridCellCoords( topRow
- 1, rightCol
) );
4917 if ( oldRight
> rightCol
)
4919 // Refresh the newly selected or deselected
4920 // area to the right of the old or new selection.
4921 need_refresh
[2] = true;
4922 rect
[2] = BlockToDeviceRect(
4923 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4924 wxGridCellCoords( oldBottom
, oldRight
) );
4927 if ( oldBottom
> bottomRow
)
4929 // Refresh the newly selected or deselected
4930 // area below the old or new selection.
4931 need_refresh
[3] = true;
4932 rect
[3] = BlockToDeviceRect(
4933 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4934 wxGridCellCoords( oldBottom
, rightCol
) );
4937 // various Refresh() calls
4938 for (i
= 0; i
< 4; i
++ )
4939 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4940 m_gridWin
->Refresh( false, &(rect
[i
]) );
4944 m_selectedBlockTopLeft
= updateTopLeft
;
4945 m_selectedBlockBottomRight
= updateBottomRight
;
4949 // ------ functions to get/send data (see also public functions)
4952 bool wxGrid::GetModelValues()
4954 // Hide the editor, so it won't hide a changed value.
4955 HideCellEditControl();
4959 // all we need to do is repaint the grid
4961 m_gridWin
->Refresh();
4968 bool wxGrid::SetModelValues()
4972 // Disable the editor, so it won't hide a changed value.
4973 // Do we also want to save the current value of the editor first?
4975 DisableCellEditControl();
4979 for ( row
= 0; row
< m_numRows
; row
++ )
4981 for ( col
= 0; col
< m_numCols
; col
++ )
4983 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4993 // Note - this function only draws cells that are in the list of
4994 // exposed cells (usually set from the update region by
4995 // CalcExposedCells)
4997 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
4999 if ( !m_numRows
|| !m_numCols
)
5002 int i
, numCells
= cells
.GetCount();
5003 int row
, col
, cell_rows
, cell_cols
;
5004 wxGridCellCoordsArray redrawCells
;
5006 for ( i
= numCells
- 1; i
>= 0; i
-- )
5008 row
= cells
[i
].GetRow();
5009 col
= cells
[i
].GetCol();
5010 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5012 // If this cell is part of a multicell block, find owner for repaint
5013 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5015 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5016 bool marked
= false;
5017 for ( int j
= 0; j
< numCells
; j
++ )
5019 if ( cell
== cells
[j
] )
5028 int count
= redrawCells
.GetCount();
5029 for (int j
= 0; j
< count
; j
++)
5031 if ( cell
== redrawCells
[j
] )
5039 redrawCells
.Add( cell
);
5042 // don't bother drawing this cell
5046 // If this cell is empty, find cell to left that might want to overflow
5047 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5049 for ( int l
= 0; l
< cell_rows
; l
++ )
5051 // find a cell in this row to leave already marked for repaint
5053 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5054 if ((redrawCells
[k
].GetCol() < left
) &&
5055 (redrawCells
[k
].GetRow() == row
))
5057 left
= redrawCells
[k
].GetCol();
5061 left
= 0; // oh well
5063 for (int j
= col
- 1; j
>= left
; j
--)
5065 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5067 if (GetCellOverflow(row
+ l
, j
))
5069 wxGridCellCoords
cell(row
+ l
, j
);
5070 bool marked
= false;
5072 for (int k
= 0; k
< numCells
; k
++)
5074 if ( cell
== cells
[k
] )
5083 int count
= redrawCells
.GetCount();
5084 for (int k
= 0; k
< count
; k
++)
5086 if ( cell
== redrawCells
[k
] )
5093 redrawCells
.Add( cell
);
5102 DrawCell( dc
, cells
[i
] );
5105 numCells
= redrawCells
.GetCount();
5107 for ( i
= numCells
- 1; i
>= 0; i
-- )
5109 DrawCell( dc
, redrawCells
[i
] );
5113 void wxGrid::DrawGridSpace( wxDC
& dc
)
5116 m_gridWin
->GetClientSize( &cw
, &ch
);
5119 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5121 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5122 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5124 if ( right
> rightCol
|| bottom
> bottomRow
)
5127 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5129 dc
.SetBrush(GetDefaultCellBackgroundColour());
5130 dc
.SetPen( *wxTRANSPARENT_PEN
);
5132 if ( right
> rightCol
)
5134 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5137 if ( bottom
> bottomRow
)
5139 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5144 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5146 int row
= coords
.GetRow();
5147 int col
= coords
.GetCol();
5149 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5152 // we draw the cell border ourselves
5153 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5155 bool isCurrent
= coords
== m_currentCellCoords
;
5157 wxRect rect
= CellToRect( row
, col
);
5159 // if the editor is shown, we should use it and not the renderer
5160 // Note: However, only if it is really _shown_, i.e. not hidden!
5161 if ( isCurrent
&& IsCellEditControlShown() )
5163 // NB: this "#if..." is temporary and fixes a problem where the
5164 // edit control is erased by this code after being rendered.
5165 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5166 // implicitly, causing this out-of order render.
5167 #if !defined(__WXMAC__)
5168 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5169 editor
->PaintBackground(rect
, attr
);
5175 // but all the rest is drawn by the cell renderer and hence may be customized
5176 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5177 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5184 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5186 // don't show highlight when the grid doesn't have focus
5190 int row
= m_currentCellCoords
.GetRow();
5191 int col
= m_currentCellCoords
.GetCol();
5193 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5196 wxRect rect
= CellToRect(row
, col
);
5198 // hmmm... what could we do here to show that the cell is disabled?
5199 // for now, I just draw a thinner border than for the other ones, but
5200 // it doesn't look really good
5202 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5206 // The center of the drawn line is where the position/width/height of
5207 // the rectangle is actually at (on wxMSW at least), so the
5208 // size of the rectangle is reduced to compensate for the thickness of
5209 // the line. If this is too strange on non-wxMSW platforms then
5210 // please #ifdef this appropriately.
5211 rect
.x
+= penWidth
/ 2;
5212 rect
.y
+= penWidth
/ 2;
5213 rect
.width
-= penWidth
- 1;
5214 rect
.height
-= penWidth
- 1;
5216 // Now draw the rectangle
5217 // use the cellHighlightColour if the cell is inside a selection, this
5218 // will ensure the cell is always visible.
5219 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5220 : m_cellHighlightColour
,
5222 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5223 dc
.DrawRectangle(rect
);
5227 wxPen
wxGrid::GetDefaultGridLinePen()
5229 return wxPen(GetGridLineColour());
5232 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5234 return GetDefaultGridLinePen();
5237 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5239 return GetDefaultGridLinePen();
5242 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5244 int row
= coords
.GetRow();
5245 int col
= coords
.GetCol();
5246 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5250 wxRect rect
= CellToRect( row
, col
);
5252 // right hand border
5253 dc
.SetPen( GetColGridLinePen(col
) );
5254 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5255 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5258 dc
.SetPen( GetRowGridLinePen(row
) );
5259 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5260 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5263 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5265 // This if block was previously in wxGrid::OnPaint but that doesn't
5266 // seem to get called under wxGTK - MB
5268 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5269 m_numRows
&& m_numCols
)
5271 m_currentCellCoords
.Set(0, 0);
5274 if ( IsCellEditControlShown() )
5276 // don't show highlight when the edit control is shown
5280 // if the active cell was repainted, repaint its highlight too because it
5281 // might have been damaged by the grid lines
5282 size_t count
= cells
.GetCount();
5283 for ( size_t n
= 0; n
< count
; n
++ )
5285 wxGridCellCoords cell
= cells
[n
];
5287 // If we are using attributes, then we may have just exposed another
5288 // cell in a partially-visible merged cluster of cells. If the "anchor"
5289 // (upper left) cell of this merged cluster is the cell indicated by
5290 // m_currentCellCoords, then we need to refresh the cell highlight even
5291 // though the "anchor" itself is not part of our update segment.
5292 if ( CanHaveAttributes() )
5296 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5299 cell
.SetRow(cell
.GetRow() + rows
);
5302 cell
.SetCol(cell
.GetCol() + cols
);
5305 if ( cell
== m_currentCellCoords
)
5307 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5308 DrawCellHighlight(dc
, attr
);
5316 // This is used to redraw all grid lines e.g. when the grid line colour
5319 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5321 if ( !m_gridLinesEnabled
)
5324 int top
, bottom
, left
, right
;
5327 m_gridWin
->GetClientSize(&cw
, &ch
);
5328 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5329 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5331 // avoid drawing grid lines past the last row and col
5332 if ( m_gridLinesClipHorz
)
5337 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5338 if ( right
> lastColRight
)
5339 right
= lastColRight
;
5342 if ( m_gridLinesClipVert
)
5347 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5348 if ( bottom
> lastRowBottom
)
5349 bottom
= lastRowBottom
;
5352 // no gridlines inside multicells, clip them out
5353 int leftCol
= GetColPos( internalXToCol(left
) );
5354 int topRow
= internalYToRow(top
);
5355 int rightCol
= GetColPos( internalXToCol(right
) );
5356 int bottomRow
= internalYToRow(bottom
);
5358 wxRegion
clippedcells(0, 0, cw
, ch
);
5360 int cell_rows
, cell_cols
;
5363 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5365 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5367 int i
= GetColAt( colPos
);
5369 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5370 if ((cell_rows
> 1) || (cell_cols
> 1))
5372 rect
= CellToRect(j
,i
);
5373 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5374 clippedcells
.Subtract(rect
);
5376 else if ((cell_rows
< 0) || (cell_cols
< 0))
5378 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5379 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5380 clippedcells
.Subtract(rect
);
5385 dc
.SetDeviceClippingRegion( clippedcells
);
5388 // horizontal grid lines
5389 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5391 int bot
= GetRowBottom(i
) - 1;
5398 dc
.SetPen( GetRowGridLinePen(i
) );
5399 dc
.DrawLine( left
, bot
, right
, bot
);
5403 // vertical grid lines
5404 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5406 int i
= GetColAt( colPos
);
5408 int colRight
= GetColRight(i
);
5410 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5414 if ( colRight
> right
)
5417 if ( colRight
>= left
)
5419 dc
.SetPen( GetColGridLinePen(i
) );
5420 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5424 dc
.DestroyClippingRegion();
5427 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5432 const size_t numLabels
= rows
.GetCount();
5433 for ( size_t i
= 0; i
< numLabels
; i
++ )
5435 DrawRowLabel( dc
, rows
[i
] );
5439 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5441 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5444 wxGridCellAttrProvider
* const
5445 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5446 const wxGridRowHeaderRenderer
&
5447 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5448 : gs_defaultHeaderRenderers
.rowRenderer
;
5449 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5450 rend
.DrawBorder(*this, dc
, rect
);
5453 GetRowLabelAlignment(&hAlign
, &vAlign
);
5455 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5456 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5459 void wxGrid::UseNativeColHeader(bool native
)
5461 if ( native
== m_useNativeHeader
)
5465 m_useNativeHeader
= native
;
5467 CreateColumnWindow();
5469 if ( m_useNativeHeader
)
5470 GetGridColHeader()->SetColumnCount(m_numCols
);
5474 void wxGrid::SetUseNativeColLabels( bool native
)
5476 wxASSERT_MSG( !m_useNativeHeader
,
5477 "doesn't make sense when using native header" );
5479 m_nativeColumnLabels
= native
;
5482 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5483 SetColLabelSize( height
);
5486 GetColLabelWindow()->Refresh();
5487 m_cornerLabelWin
->Refresh();
5490 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5495 const size_t numLabels
= cols
.GetCount();
5496 for ( size_t i
= 0; i
< numLabels
; i
++ )
5498 DrawColLabel( dc
, cols
[i
] );
5502 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5504 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5506 if ( m_nativeColumnLabels
)
5510 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5517 wxGridCellAttrProvider
* const
5518 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5519 const wxGridCornerHeaderRenderer
&
5520 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5521 : gs_defaultHeaderRenderers
.cornerRenderer
;
5523 rend
.DrawBorder(*this, dc
, rect
);
5527 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5529 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5532 int colLeft
= GetColLeft(col
);
5534 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5535 wxGridCellAttrProvider
* const
5536 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5537 const wxGridColumnHeaderRenderer
&
5538 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
5539 : gs_defaultHeaderRenderers
.colRenderer
;
5541 if ( m_nativeColumnLabels
)
5543 wxRendererNative::Get().DrawHeaderButton
5545 GetColLabelWindow(),
5550 ? IsSortOrderAscending()
5551 ? wxHDR_SORT_ICON_UP
5552 : wxHDR_SORT_ICON_DOWN
5553 : wxHDR_SORT_ICON_NONE
5559 rend
.DrawBorder(*this, dc
, rect
);
5563 GetColLabelAlignment(&hAlign
, &vAlign
);
5564 const int orient
= GetColLabelTextOrientation();
5566 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5569 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5570 // we just have to add textOrientation support
5571 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5572 const wxString
& value
,
5576 int textOrientation
) const
5578 wxArrayString lines
;
5580 StringToLines( value
, lines
);
5582 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5585 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5586 const wxArrayString
& lines
,
5590 int textOrientation
) const
5592 if ( lines
.empty() )
5595 wxDCClipper
clip(dc
, rect
);
5600 if ( textOrientation
== wxHORIZONTAL
)
5601 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5603 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5607 switch ( vertAlign
)
5609 case wxALIGN_BOTTOM
:
5610 if ( textOrientation
== wxHORIZONTAL
)
5611 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5613 x
= rect
.x
+ rect
.width
- textWidth
;
5616 case wxALIGN_CENTRE
:
5617 if ( textOrientation
== wxHORIZONTAL
)
5618 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5620 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5625 if ( textOrientation
== wxHORIZONTAL
)
5632 // Align each line of a multi-line label
5633 size_t nLines
= lines
.GetCount();
5634 for ( size_t l
= 0; l
< nLines
; l
++ )
5636 const wxString
& line
= lines
[l
];
5640 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5644 wxCoord lineWidth
= 0,
5646 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5648 switch ( horizAlign
)
5651 if ( textOrientation
== wxHORIZONTAL
)
5652 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5654 y
= rect
.y
+ lineWidth
+ 1;
5657 case wxALIGN_CENTRE
:
5658 if ( textOrientation
== wxHORIZONTAL
)
5659 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5661 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5666 if ( textOrientation
== wxHORIZONTAL
)
5669 y
= rect
.y
+ rect
.height
- 1;
5673 if ( textOrientation
== wxHORIZONTAL
)
5675 dc
.DrawText( line
, x
, y
);
5680 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5686 // Split multi-line text up into an array of strings.
5687 // Any existing contents of the string array are preserved.
5689 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5690 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5694 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5695 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5697 while ( startPos
< (int)tVal
.length() )
5699 pos
= tVal
.Mid(startPos
).Find( eol
);
5704 else if ( pos
== 0 )
5706 lines
.Add( wxEmptyString
);
5710 lines
.Add( tVal
.Mid(startPos
, pos
) );
5713 startPos
+= pos
+ 1;
5716 if ( startPos
< (int)tVal
.length() )
5718 lines
.Add( tVal
.Mid( startPos
) );
5722 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5723 const wxArrayString
& lines
,
5724 long *width
, long *height
) const
5728 wxCoord lineW
= 0, lineH
= 0;
5731 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5733 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5734 w
= wxMax( w
, lineW
);
5743 // ------ Batch processing.
5745 void wxGrid::EndBatch()
5747 if ( m_batchCount
> 0 )
5750 if ( !m_batchCount
)
5753 m_rowLabelWin
->Refresh();
5754 m_colWindow
->Refresh();
5755 m_cornerLabelWin
->Refresh();
5756 m_gridWin
->Refresh();
5761 // Use this, rather than wxWindow::Refresh(), to force an immediate
5762 // repainting of the grid. Has no effect if you are already inside a
5763 // BeginBatch / EndBatch block.
5765 void wxGrid::ForceRefresh()
5771 bool wxGrid::Enable(bool enable
)
5773 if ( !wxScrolledWindow::Enable(enable
) )
5776 // redraw in the new state
5777 m_gridWin
->Refresh();
5783 // ------ Edit control functions
5786 void wxGrid::EnableEditing( bool edit
)
5788 if ( edit
!= m_editable
)
5791 EnableCellEditControl(edit
);
5796 void wxGrid::EnableCellEditControl( bool enable
)
5801 if ( enable
!= m_cellEditCtrlEnabled
)
5805 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5808 // this should be checked by the caller!
5809 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5811 // do it before ShowCellEditControl()
5812 m_cellEditCtrlEnabled
= enable
;
5814 ShowCellEditControl();
5818 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5820 HideCellEditControl();
5821 SaveEditControlValue();
5823 // do it after HideCellEditControl()
5824 m_cellEditCtrlEnabled
= enable
;
5829 bool wxGrid::IsCurrentCellReadOnly() const
5832 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5833 bool readonly
= attr
->IsReadOnly();
5839 bool wxGrid::CanEnableCellControl() const
5841 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5842 !IsCurrentCellReadOnly();
5845 bool wxGrid::IsCellEditControlEnabled() const
5847 // the cell edit control might be disable for all cells or just for the
5848 // current one if it's read only
5849 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5852 bool wxGrid::IsCellEditControlShown() const
5854 bool isShown
= false;
5856 if ( m_cellEditCtrlEnabled
)
5858 int row
= m_currentCellCoords
.GetRow();
5859 int col
= m_currentCellCoords
.GetCol();
5860 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5861 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5866 if ( editor
->IsCreated() )
5868 isShown
= editor
->GetControl()->IsShown();
5878 void wxGrid::ShowCellEditControl()
5880 if ( IsCellEditControlEnabled() )
5882 if ( !IsVisible( m_currentCellCoords
, false ) )
5884 m_cellEditCtrlEnabled
= false;
5889 wxRect rect
= CellToRect( m_currentCellCoords
);
5890 int row
= m_currentCellCoords
.GetRow();
5891 int col
= m_currentCellCoords
.GetCol();
5893 // if this is part of a multicell, find owner (topleft)
5894 int cell_rows
, cell_cols
;
5895 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5896 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5900 m_currentCellCoords
.SetRow( row
);
5901 m_currentCellCoords
.SetCol( col
);
5904 // erase the highlight and the cell contents because the editor
5905 // might not cover the entire cell
5906 wxClientDC
dc( m_gridWin
);
5908 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5909 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5910 dc
.SetPen(*wxTRANSPARENT_PEN
);
5911 dc
.DrawRectangle(rect
);
5913 // convert to scrolled coords
5914 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5920 // cell is shifted by one pixel
5921 // However, don't allow x or y to become negative
5922 // since the SetSize() method interprets that as
5929 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5930 if ( !editor
->IsCreated() )
5932 editor
->Create(m_gridWin
, wxID_ANY
,
5933 new wxGridCellEditorEvtHandler(this, editor
));
5935 wxGridEditorCreatedEvent
evt(GetId(),
5936 wxEVT_GRID_EDITOR_CREATED
,
5940 editor
->GetControl());
5941 GetEventHandler()->ProcessEvent(evt
);
5944 // resize editor to overflow into righthand cells if allowed
5945 int maxWidth
= rect
.width
;
5946 wxString value
= GetCellValue(row
, col
);
5947 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
5950 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
5951 if (maxWidth
< rect
.width
)
5952 maxWidth
= rect
.width
;
5955 int client_right
= m_gridWin
->GetClientSize().GetWidth();
5956 if (rect
.x
+ maxWidth
> client_right
)
5957 maxWidth
= client_right
- rect
.x
;
5959 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
5961 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5962 // may have changed earlier
5963 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
5966 GetCellSize( row
, i
, &c_rows
, &c_cols
);
5968 // looks weird going over a multicell
5969 if (m_table
->IsEmptyCell( row
, i
) &&
5970 (rect
.width
< maxWidth
) && (c_rows
== 1))
5972 rect
.width
+= GetColWidth( i
);
5978 if (rect
.GetRight() > client_right
)
5979 rect
.SetRight( client_right
- 1 );
5982 editor
->SetCellAttr( attr
);
5983 editor
->SetSize( rect
);
5985 editor
->GetControl()->Move(
5986 editor
->GetControl()->GetPosition().x
+ nXMove
,
5987 editor
->GetControl()->GetPosition().y
);
5988 editor
->Show( true, attr
);
5990 // recalc dimensions in case we need to
5991 // expand the scrolled window to account for editor
5994 editor
->BeginEdit(row
, col
, this);
5995 editor
->SetCellAttr(NULL
);
6003 void wxGrid::HideCellEditControl()
6005 if ( IsCellEditControlEnabled() )
6007 int row
= m_currentCellCoords
.GetRow();
6008 int col
= m_currentCellCoords
.GetCol();
6010 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6011 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6012 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6013 editor
->Show( false );
6017 // return the focus to the grid itself if the editor had it
6019 // note that we must not do this unconditionally to avoid stealing
6020 // focus from the window which just received it if we are hiding the
6021 // editor precisely because we lost focus
6022 if ( editorHadFocus
)
6023 m_gridWin
->SetFocus();
6025 // refresh whole row to the right
6026 wxRect
rect( CellToRect(row
, col
) );
6027 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6028 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6031 // ensure that the pixels under the focus ring get refreshed as well
6032 rect
.Inflate(10, 10);
6035 m_gridWin
->Refresh( false, &rect
);
6039 void wxGrid::SaveEditControlValue()
6041 if ( IsCellEditControlEnabled() )
6043 int row
= m_currentCellCoords
.GetRow();
6044 int col
= m_currentCellCoords
.GetCol();
6046 wxString oldval
= GetCellValue(row
, col
);
6048 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6049 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6052 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6054 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6056 editor
->ApplyEdit(row
, col
, this);
6058 // for compatibility reasons dating back to wx 2.8 when this event
6059 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6060 // didn't exist we allow vetoing this one too
6061 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6063 // Event has been vetoed, set the data back.
6064 SetCellValue(row
, col
, oldval
);
6074 // ------ Grid location functions
6075 // Note that all of these functions work with the logical coordinates of
6076 // grid cells and labels so you will need to convert from device
6077 // coordinates for mouse events etc.
6080 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6082 int row
= YToRow(y
);
6083 int col
= XToCol(x
);
6085 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6086 : wxGridCellCoords(row
, col
);
6089 // compute row or column from some (unscrolled) coordinate value, using either
6090 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6091 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6093 int wxGrid::PosToLinePos(int coord
,
6095 const wxGridOperations
& oper
) const
6097 const int numLines
= oper
.GetNumberOfLines(this);
6100 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6102 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6103 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6105 int maxPos
= coord
/ defaultLineSize
,
6108 // check for the simplest case: if we have no explicit line sizes
6109 // configured, then we already know the line this position falls in
6110 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6111 if ( lineEnds
.empty() )
6113 if ( maxPos
< numLines
)
6116 return clipToMinMax
? numLines
- 1 : -1;
6120 // adjust maxPos before starting the binary search
6121 if ( maxPos
>= numLines
)
6123 maxPos
= numLines
- 1;
6127 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6130 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6132 maxPos
= coord
/ minDist
;
6134 maxPos
= numLines
- 1;
6137 if ( maxPos
>= numLines
)
6138 maxPos
= numLines
- 1;
6141 // check if the position is beyond the last column
6142 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6143 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6144 return clipToMinMax
? maxPos
: -1;
6146 // or before the first one
6147 const int lineAt0
= oper
.GetLineAt(this, 0);
6148 if ( coord
< lineEnds
[lineAt0
] )
6152 // finally do perform the binary search
6153 while ( minPos
< maxPos
)
6155 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6156 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6158 "wxGrid: internal error in PosToLinePos()" );
6160 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6165 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6166 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6176 wxGrid::PosToLine(int coord
,
6178 const wxGridOperations
& oper
) const
6180 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6182 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6185 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6187 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6190 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6192 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6195 int wxGrid::XToPos(int x
) const
6197 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6200 // return the row number such that the y coord is near the edge of, or -1 if
6201 // not near an edge.
6203 // notice that position can only possibly be near an edge if the row/column is
6204 // large enough to still allow for an "inner" area that is _not_ near the edge
6205 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6206 // _never_ be considered to be near the edge).
6207 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6209 const int line
= oper
.PosToLine(this, pos
, true);
6211 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6213 // We know that we are in this line, test whether we are close enough
6214 // to start or end border, respectively.
6215 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6217 else if ( line
> 0 &&
6218 pos
- oper
.GetLineStartPos(this,
6219 line
) < WXGRID_LABEL_EDGE_ZONE
)
6226 int wxGrid::YToEdgeOfRow(int y
) const
6228 return PosToEdgeOfLine(y
, wxGridRowOperations());
6231 int wxGrid::XToEdgeOfCol(int x
) const
6233 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6236 wxRect
wxGrid::CellToRect( int row
, int col
) const
6238 wxRect
rect( -1, -1, -1, -1 );
6240 if ( row
>= 0 && row
< m_numRows
&&
6241 col
>= 0 && col
< m_numCols
)
6243 int i
, cell_rows
, cell_cols
;
6244 rect
.width
= rect
.height
= 0;
6245 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6246 // if negative then find multicell owner
6251 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6253 rect
.x
= GetColLeft(col
);
6254 rect
.y
= GetRowTop(row
);
6255 for (i
=col
; i
< col
+ cell_cols
; i
++)
6256 rect
.width
+= GetColWidth(i
);
6257 for (i
=row
; i
< row
+ cell_rows
; i
++)
6258 rect
.height
+= GetRowHeight(i
);
6260 // if grid lines are enabled, then the area of the cell is a bit smaller
6261 if (m_gridLinesEnabled
)
6271 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6273 // get the cell rectangle in logical coords
6275 wxRect
r( CellToRect( row
, col
) );
6277 // convert to device coords
6279 int left
, top
, right
, bottom
;
6280 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6281 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6283 // check against the client area of the grid window
6285 m_gridWin
->GetClientSize( &cw
, &ch
);
6287 if ( wholeCellVisible
)
6289 // is the cell wholly visible ?
6290 return ( left
>= 0 && right
<= cw
&&
6291 top
>= 0 && bottom
<= ch
);
6295 // is the cell partly visible ?
6297 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6298 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6302 // make the specified cell location visible by doing a minimal amount
6305 void wxGrid::MakeCellVisible( int row
, int col
)
6308 int xpos
= -1, ypos
= -1;
6310 if ( row
>= 0 && row
< m_numRows
&&
6311 col
>= 0 && col
< m_numCols
)
6313 // get the cell rectangle in logical coords
6314 wxRect
r( CellToRect( row
, col
) );
6316 // convert to device coords
6317 int left
, top
, right
, bottom
;
6318 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6319 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6322 m_gridWin
->GetClientSize( &cw
, &ch
);
6328 else if ( bottom
> ch
)
6330 int h
= r
.GetHeight();
6332 for ( i
= row
- 1; i
>= 0; i
-- )
6334 int rowHeight
= GetRowHeight(i
);
6335 if ( h
+ rowHeight
> ch
)
6342 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6343 // have rounding errors (this is important, because if we do,
6344 // we might not scroll at all and some cells won't be redrawn)
6346 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6347 // so just add a full scroll unit...
6348 ypos
+= m_scrollLineY
;
6351 // special handling for wide cells - show always left part of the cell!
6352 // Otherwise, e.g. when stepping from row to row, it would jump between
6353 // left and right part of the cell on every step!
6355 if ( left
< 0 || (right
- left
) >= cw
)
6359 else if ( right
> cw
)
6361 // position the view so that the cell is on the right
6363 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6364 xpos
= x0
+ (right
- cw
);
6366 // see comment for ypos above
6367 xpos
+= m_scrollLineX
;
6370 if ( xpos
!= -1 || ypos
!= -1 )
6373 xpos
/= m_scrollLineX
;
6375 ypos
/= m_scrollLineY
;
6376 Scroll( xpos
, ypos
);
6383 // ------ Grid cursor movement functions
6387 wxGrid::DoMoveCursor(bool expandSelection
,
6388 const wxGridDirectionOperations
& diroper
)
6390 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6393 if ( expandSelection
)
6395 wxGridCellCoords coords
= m_selectedBlockCorner
;
6396 if ( coords
== wxGridNoCellCoords
)
6397 coords
= m_currentCellCoords
;
6399 if ( diroper
.IsAtBoundary(coords
) )
6402 diroper
.Advance(coords
);
6404 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6406 else // don't expand selection
6410 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6413 wxGridCellCoords coords
= m_currentCellCoords
;
6414 diroper
.Advance(coords
);
6422 bool wxGrid::MoveCursorUp(bool expandSelection
)
6424 return DoMoveCursor(expandSelection
,
6425 wxGridBackwardOperations(this, wxGridRowOperations()));
6428 bool wxGrid::MoveCursorDown(bool expandSelection
)
6430 return DoMoveCursor(expandSelection
,
6431 wxGridForwardOperations(this, wxGridRowOperations()));
6434 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6436 return DoMoveCursor(expandSelection
,
6437 wxGridBackwardOperations(this, wxGridColumnOperations()));
6440 bool wxGrid::MoveCursorRight(bool expandSelection
)
6442 return DoMoveCursor(expandSelection
,
6443 wxGridForwardOperations(this, wxGridColumnOperations()));
6446 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6448 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6451 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6454 const int oldRow
= m_currentCellCoords
.GetRow();
6455 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6456 if ( newRow
== oldRow
)
6458 wxGridCellCoords
coords(m_currentCellCoords
);
6459 diroper
.Advance(coords
);
6460 newRow
= coords
.GetRow();
6463 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6468 bool wxGrid::MovePageUp()
6470 return DoMoveCursorByPage(
6471 wxGridBackwardOperations(this, wxGridRowOperations()));
6474 bool wxGrid::MovePageDown()
6476 return DoMoveCursorByPage(
6477 wxGridForwardOperations(this, wxGridRowOperations()));
6480 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6481 // until we find a non-empty cell or reach the grid end
6483 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6484 const wxGridDirectionOperations
& diroper
)
6486 while ( !diroper
.IsAtBoundary(coords
) )
6488 diroper
.Advance(coords
);
6489 if ( !m_table
->IsEmpty(coords
) )
6495 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6496 const wxGridDirectionOperations
& diroper
)
6498 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6501 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6504 wxGridCellCoords
coords(m_currentCellCoords
);
6505 if ( m_table
->IsEmpty(coords
) )
6507 // we are in an empty cell: find the next block of non-empty cells
6508 AdvanceToNextNonEmpty(coords
, diroper
);
6510 else // current cell is not empty
6512 diroper
.Advance(coords
);
6513 if ( m_table
->IsEmpty(coords
) )
6515 // we started at the end of a block, find the next one
6516 AdvanceToNextNonEmpty(coords
, diroper
);
6518 else // we're in a middle of a block
6520 // go to the end of it, i.e. find the last cell before the next
6522 while ( !diroper
.IsAtBoundary(coords
) )
6524 wxGridCellCoords
coordsNext(coords
);
6525 diroper
.Advance(coordsNext
);
6526 if ( m_table
->IsEmpty(coordsNext
) )
6529 coords
= coordsNext
;
6534 if ( expandSelection
)
6536 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6547 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6549 return DoMoveCursorByBlock(
6551 wxGridBackwardOperations(this, wxGridRowOperations())
6555 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6557 return DoMoveCursorByBlock(
6559 wxGridForwardOperations(this, wxGridRowOperations())
6563 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6565 return DoMoveCursorByBlock(
6567 wxGridBackwardOperations(this, wxGridColumnOperations())
6571 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6573 return DoMoveCursorByBlock(
6575 wxGridForwardOperations(this, wxGridColumnOperations())
6580 // ------ Label values and formatting
6583 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6586 *horiz
= m_rowLabelHorizAlign
;
6588 *vert
= m_rowLabelVertAlign
;
6591 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6594 *horiz
= m_colLabelHorizAlign
;
6596 *vert
= m_colLabelVertAlign
;
6599 int wxGrid::GetColLabelTextOrientation() const
6601 return m_colLabelTextOrientation
;
6604 wxString
wxGrid::GetRowLabelValue( int row
) const
6608 return m_table
->GetRowLabelValue( row
);
6618 wxString
wxGrid::GetColLabelValue( int col
) const
6622 return m_table
->GetColLabelValue( col
);
6632 void wxGrid::SetRowLabelSize( int width
)
6634 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6636 if ( width
== wxGRID_AUTOSIZE
)
6638 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6641 if ( width
!= m_rowLabelWidth
)
6645 m_rowLabelWin
->Show( false );
6646 m_cornerLabelWin
->Show( false );
6648 else if ( m_rowLabelWidth
== 0 )
6650 m_rowLabelWin
->Show( true );
6651 if ( m_colLabelHeight
> 0 )
6652 m_cornerLabelWin
->Show( true );
6655 m_rowLabelWidth
= width
;
6657 wxScrolledWindow::Refresh( true );
6661 void wxGrid::SetColLabelSize( int height
)
6663 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6665 if ( height
== wxGRID_AUTOSIZE
)
6667 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6670 if ( height
!= m_colLabelHeight
)
6674 m_colWindow
->Show( false );
6675 m_cornerLabelWin
->Show( false );
6677 else if ( m_colLabelHeight
== 0 )
6679 m_colWindow
->Show( true );
6680 if ( m_rowLabelWidth
> 0 )
6681 m_cornerLabelWin
->Show( true );
6684 m_colLabelHeight
= height
;
6686 wxScrolledWindow::Refresh( true );
6690 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6692 if ( m_labelBackgroundColour
!= colour
)
6694 m_labelBackgroundColour
= colour
;
6695 m_rowLabelWin
->SetBackgroundColour( colour
);
6696 m_colWindow
->SetBackgroundColour( colour
);
6697 m_cornerLabelWin
->SetBackgroundColour( colour
);
6699 if ( !GetBatchCount() )
6701 m_rowLabelWin
->Refresh();
6702 m_colWindow
->Refresh();
6703 m_cornerLabelWin
->Refresh();
6708 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6710 if ( m_labelTextColour
!= colour
)
6712 m_labelTextColour
= colour
;
6713 if ( !GetBatchCount() )
6715 m_rowLabelWin
->Refresh();
6716 m_colWindow
->Refresh();
6721 void wxGrid::SetLabelFont( const wxFont
& font
)
6724 if ( !GetBatchCount() )
6726 m_rowLabelWin
->Refresh();
6727 m_colWindow
->Refresh();
6731 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6733 // allow old (incorrect) defs to be used
6736 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6737 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6738 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6743 case wxTOP
: vert
= wxALIGN_TOP
; break;
6744 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6745 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6748 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6750 m_rowLabelHorizAlign
= horiz
;
6753 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6755 m_rowLabelVertAlign
= vert
;
6758 if ( !GetBatchCount() )
6760 m_rowLabelWin
->Refresh();
6764 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6766 // allow old (incorrect) defs to be used
6769 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6770 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6771 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6776 case wxTOP
: vert
= wxALIGN_TOP
; break;
6777 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6778 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6781 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6783 m_colLabelHorizAlign
= horiz
;
6786 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6788 m_colLabelVertAlign
= vert
;
6791 if ( !GetBatchCount() )
6793 m_colWindow
->Refresh();
6797 // Note: under MSW, the default column label font must be changed because it
6798 // does not support vertical printing
6800 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6801 // pGrid->SetLabelFont(font);
6802 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6804 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6806 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6807 m_colLabelTextOrientation
= textOrientation
;
6809 if ( !GetBatchCount() )
6810 m_colWindow
->Refresh();
6813 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6817 m_table
->SetRowLabelValue( row
, s
);
6818 if ( !GetBatchCount() )
6820 wxRect rect
= CellToRect( row
, 0 );
6821 if ( rect
.height
> 0 )
6823 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6825 rect
.width
= m_rowLabelWidth
;
6826 m_rowLabelWin
->Refresh( true, &rect
);
6832 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6836 m_table
->SetColLabelValue( col
, s
);
6837 if ( !GetBatchCount() )
6839 if ( m_useNativeHeader
)
6841 GetGridColHeader()->UpdateColumn(col
);
6845 wxRect rect
= CellToRect( 0, col
);
6846 if ( rect
.width
> 0 )
6848 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6850 rect
.height
= m_colLabelHeight
;
6851 GetColLabelWindow()->Refresh( true, &rect
);
6858 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6860 if ( m_gridLineColour
!= colour
)
6862 m_gridLineColour
= colour
;
6864 if ( GridLinesEnabled() )
6869 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6871 if ( m_cellHighlightColour
!= colour
)
6873 m_cellHighlightColour
= colour
;
6875 wxClientDC
dc( m_gridWin
);
6877 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6878 DrawCellHighlight(dc
, attr
);
6883 void wxGrid::SetCellHighlightPenWidth(int width
)
6885 if (m_cellHighlightPenWidth
!= width
)
6887 m_cellHighlightPenWidth
= width
;
6889 // Just redrawing the cell highlight is not enough since that won't
6890 // make any visible change if the the thickness is getting smaller.
6891 int row
= m_currentCellCoords
.GetRow();
6892 int col
= m_currentCellCoords
.GetCol();
6893 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6896 wxRect rect
= CellToRect(row
, col
);
6897 m_gridWin
->Refresh(true, &rect
);
6901 void wxGrid::SetCellHighlightROPenWidth(int width
)
6903 if (m_cellHighlightROPenWidth
!= width
)
6905 m_cellHighlightROPenWidth
= width
;
6907 // Just redrawing the cell highlight is not enough since that won't
6908 // make any visible change if the the thickness is getting smaller.
6909 int row
= m_currentCellCoords
.GetRow();
6910 int col
= m_currentCellCoords
.GetCol();
6911 if ( row
== -1 || col
== -1 ||
6912 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6915 wxRect rect
= CellToRect(row
, col
);
6916 m_gridWin
->Refresh(true, &rect
);
6920 void wxGrid::RedrawGridLines()
6922 // the lines will be redrawn when the window is thawn
6923 if ( GetBatchCount() )
6926 if ( GridLinesEnabled() )
6928 wxClientDC
dc( m_gridWin
);
6930 DrawAllGridLines( dc
, wxRegion() );
6932 else // remove the grid lines
6934 m_gridWin
->Refresh();
6938 void wxGrid::EnableGridLines( bool enable
)
6940 if ( enable
!= m_gridLinesEnabled
)
6942 m_gridLinesEnabled
= enable
;
6948 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
6954 if ( GridLinesEnabled() )
6959 int wxGrid::GetDefaultRowSize() const
6961 return m_defaultRowHeight
;
6964 int wxGrid::GetRowSize( int row
) const
6966 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
6968 return GetRowHeight(row
);
6971 int wxGrid::GetDefaultColSize() const
6973 return m_defaultColWidth
;
6976 int wxGrid::GetColSize( int col
) const
6978 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
6980 return GetColWidth(col
);
6983 // ============================================================================
6984 // access to the grid attributes: each of them has a default value in the grid
6985 // itself and may be overidden on a per-cell basis
6986 // ============================================================================
6988 // ----------------------------------------------------------------------------
6989 // setting default attributes
6990 // ----------------------------------------------------------------------------
6992 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6994 m_defaultCellAttr
->SetBackgroundColour(col
);
6996 m_gridWin
->SetBackgroundColour(col
);
7000 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7002 m_defaultCellAttr
->SetTextColour(col
);
7005 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7007 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7010 void wxGrid::SetDefaultCellOverflow( bool allow
)
7012 m_defaultCellAttr
->SetOverflow(allow
);
7015 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7017 m_defaultCellAttr
->SetFont(font
);
7020 // For editors and renderers the type registry takes precedence over the
7021 // default attr, so we need to register the new editor/renderer for the string
7022 // data type in order to make setting a default editor/renderer appear to
7025 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7027 RegisterDataType(wxGRID_VALUE_STRING
,
7029 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7032 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7034 RegisterDataType(wxGRID_VALUE_STRING
,
7035 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7039 // ----------------------------------------------------------------------------
7040 // access to the default attributes
7041 // ----------------------------------------------------------------------------
7043 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7045 return m_defaultCellAttr
->GetBackgroundColour();
7048 wxColour
wxGrid::GetDefaultCellTextColour() const
7050 return m_defaultCellAttr
->GetTextColour();
7053 wxFont
wxGrid::GetDefaultCellFont() const
7055 return m_defaultCellAttr
->GetFont();
7058 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7060 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7063 bool wxGrid::GetDefaultCellOverflow() const
7065 return m_defaultCellAttr
->GetOverflow();
7068 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7070 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7073 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7075 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7078 // ----------------------------------------------------------------------------
7079 // access to cell attributes
7080 // ----------------------------------------------------------------------------
7082 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7084 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7085 wxColour colour
= attr
->GetBackgroundColour();
7091 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7093 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7094 wxColour colour
= attr
->GetTextColour();
7100 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7102 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7103 wxFont font
= attr
->GetFont();
7109 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7111 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7112 attr
->GetAlignment(horiz
, vert
);
7116 bool wxGrid::GetCellOverflow( int row
, int col
) const
7118 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7119 bool allow
= attr
->GetOverflow();
7125 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7127 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7128 attr
->GetSize( num_rows
, num_cols
);
7132 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7134 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7135 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7141 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7143 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7144 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7150 bool wxGrid::IsReadOnly(int row
, int col
) const
7152 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7153 bool isReadOnly
= attr
->IsReadOnly();
7159 // ----------------------------------------------------------------------------
7160 // attribute support: cache, automatic provider creation, ...
7161 // ----------------------------------------------------------------------------
7163 bool wxGrid::CanHaveAttributes() const
7170 return m_table
->CanHaveAttributes();
7173 void wxGrid::ClearAttrCache()
7175 if ( m_attrCache
.row
!= -1 )
7177 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7178 m_attrCache
.attr
= NULL
;
7179 m_attrCache
.row
= -1;
7180 // wxSafeDecRec(...) might cause event processing that accesses
7181 // the cached attribute, if one exists (e.g. by deleting the
7182 // editor stored within the attribute). Therefore it is important
7183 // to invalidate the cache before calling wxSafeDecRef!
7184 wxSafeDecRef(oldAttr
);
7188 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7192 wxGrid
*self
= (wxGrid
*)this; // const_cast
7194 self
->ClearAttrCache();
7195 self
->m_attrCache
.row
= row
;
7196 self
->m_attrCache
.col
= col
;
7197 self
->m_attrCache
.attr
= attr
;
7202 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7204 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7206 *attr
= m_attrCache
.attr
;
7207 wxSafeIncRef(m_attrCache
.attr
);
7209 #ifdef DEBUG_ATTR_CACHE
7210 gs_nAttrCacheHits
++;
7217 #ifdef DEBUG_ATTR_CACHE
7218 gs_nAttrCacheMisses
++;
7225 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7227 wxGridCellAttr
*attr
= NULL
;
7228 // Additional test to avoid looking at the cache e.g. for
7229 // wxNoCellCoords, as this will confuse memory management.
7232 if ( !LookupAttr(row
, col
, &attr
) )
7234 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7236 CacheAttr(row
, col
, attr
);
7242 attr
->SetDefAttr(m_defaultCellAttr
);
7246 attr
= m_defaultCellAttr
;
7253 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7255 wxGridCellAttr
*attr
= NULL
;
7256 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7258 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7259 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7261 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7264 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7266 // artificially inc the ref count to match DecRef() in caller
7268 m_table
->SetAttr(attr
, row
, col
);
7274 // ----------------------------------------------------------------------------
7275 // setting column attributes (wrappers around SetColAttr)
7276 // ----------------------------------------------------------------------------
7278 void wxGrid::SetColFormatBool(int col
)
7280 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7283 void wxGrid::SetColFormatNumber(int col
)
7285 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7288 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7290 wxString typeName
= wxGRID_VALUE_FLOAT
;
7291 if ( (width
!= -1) || (precision
!= -1) )
7293 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7296 SetColFormatCustom(col
, typeName
);
7299 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7301 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7303 attr
= new wxGridCellAttr
;
7304 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7305 attr
->SetRenderer(renderer
);
7306 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7307 attr
->SetEditor(editor
);
7309 SetColAttr(col
, attr
);
7313 // ----------------------------------------------------------------------------
7314 // setting cell attributes: this is forwarded to the table
7315 // ----------------------------------------------------------------------------
7317 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7319 if ( CanHaveAttributes() )
7321 m_table
->SetAttr(attr
, row
, col
);
7330 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7332 if ( CanHaveAttributes() )
7334 m_table
->SetRowAttr(attr
, row
);
7343 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7345 if ( CanHaveAttributes() )
7347 m_table
->SetColAttr(attr
, col
);
7356 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7358 if ( CanHaveAttributes() )
7360 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7361 attr
->SetBackgroundColour(colour
);
7366 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7368 if ( CanHaveAttributes() )
7370 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7371 attr
->SetTextColour(colour
);
7376 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7378 if ( CanHaveAttributes() )
7380 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7381 attr
->SetFont(font
);
7386 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7388 if ( CanHaveAttributes() )
7390 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7391 attr
->SetAlignment(horiz
, vert
);
7396 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7398 if ( CanHaveAttributes() )
7400 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7401 attr
->SetOverflow(allow
);
7406 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7408 if ( CanHaveAttributes() )
7410 int cell_rows
, cell_cols
;
7412 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7413 attr
->GetSize(&cell_rows
, &cell_cols
);
7414 attr
->SetSize(num_rows
, num_cols
);
7417 // Cannot set the size of a cell to 0 or negative values
7418 // While it is perfectly legal to do that, this function cannot
7419 // handle all the possibilies, do it by hand by getting the CellAttr.
7420 // You can only set the size of a cell to 1,1 or greater with this fn
7421 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7422 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7423 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7424 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7426 // if this was already a multicell then "turn off" the other cells first
7427 if ((cell_rows
> 1) || (cell_cols
> 1))
7430 for (j
=row
; j
< row
+ cell_rows
; j
++)
7432 for (i
=col
; i
< col
+ cell_cols
; i
++)
7434 if ((i
!= col
) || (j
!= row
))
7436 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7437 attr_stub
->SetSize( 1, 1 );
7438 attr_stub
->DecRef();
7444 // mark the cells that will be covered by this cell to
7445 // negative or zero values to point back at this cell
7446 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7449 for (j
=row
; j
< row
+ num_rows
; j
++)
7451 for (i
=col
; i
< col
+ num_cols
; i
++)
7453 if ((i
!= col
) || (j
!= row
))
7455 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7456 attr_stub
->SetSize( row
- j
, col
- i
);
7457 attr_stub
->DecRef();
7465 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7467 if ( CanHaveAttributes() )
7469 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7470 attr
->SetRenderer(renderer
);
7475 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7477 if ( CanHaveAttributes() )
7479 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7480 attr
->SetEditor(editor
);
7485 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7487 if ( CanHaveAttributes() )
7489 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7490 attr
->SetReadOnly(isReadOnly
);
7495 // ----------------------------------------------------------------------------
7496 // Data type registration
7497 // ----------------------------------------------------------------------------
7499 void wxGrid::RegisterDataType(const wxString
& typeName
,
7500 wxGridCellRenderer
* renderer
,
7501 wxGridCellEditor
* editor
)
7503 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7507 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7509 wxString typeName
= m_table
->GetTypeName(row
, col
);
7510 return GetDefaultEditorForType(typeName
);
7513 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7515 wxString typeName
= m_table
->GetTypeName(row
, col
);
7516 return GetDefaultRendererForType(typeName
);
7519 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7521 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7522 if ( index
== wxNOT_FOUND
)
7524 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7529 return m_typeRegistry
->GetEditor(index
);
7532 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7534 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7535 if ( index
== wxNOT_FOUND
)
7537 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7542 return m_typeRegistry
->GetRenderer(index
);
7545 // ----------------------------------------------------------------------------
7547 // ----------------------------------------------------------------------------
7549 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7553 setFixed
= new wxGridFixedIndicesSet
;
7556 setFixed
->insert(line
);
7560 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7562 return !setFixed
|| !setFixed
->count(line
);
7565 void wxGrid::EnableDragRowSize( bool enable
)
7567 m_canDragRowSize
= enable
;
7570 void wxGrid::EnableDragColSize( bool enable
)
7572 m_canDragColSize
= enable
;
7575 void wxGrid::EnableDragGridSize( bool enable
)
7577 m_canDragGridSize
= enable
;
7580 void wxGrid::EnableDragCell( bool enable
)
7582 m_canDragCell
= enable
;
7585 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7587 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7589 if ( resizeExistingRows
)
7591 // since we are resizing all rows to the default row size,
7592 // we can simply clear the row heights and row bottoms
7593 // arrays (which also allows us to take advantage of
7594 // some speed optimisations)
7595 m_rowHeights
.Empty();
7596 m_rowBottoms
.Empty();
7597 if ( !GetBatchCount() )
7602 void wxGrid::SetRowSize( int row
, int height
)
7604 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7606 // if < 0 then calculate new height from label
7610 wxArrayString lines
;
7611 wxClientDC
dc(m_rowLabelWin
);
7612 dc
.SetFont(GetLabelFont());
7613 StringToLines(GetRowLabelValue( row
), lines
);
7614 GetTextBoxSize( dc
, lines
, &w
, &h
);
7615 //check that it is not less than the minimal height
7616 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7619 // See comment in SetColSize
7620 if ( height
< GetRowMinimalAcceptableHeight())
7623 if ( m_rowHeights
.IsEmpty() )
7625 // need to really create the array
7629 int h
= wxMax( 0, height
);
7630 int diff
= h
- m_rowHeights
[row
];
7632 m_rowHeights
[row
] = h
;
7633 for ( int i
= row
; i
< m_numRows
; i
++ )
7635 m_rowBottoms
[i
] += diff
;
7638 if ( !GetBatchCount() )
7642 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7644 // we dont allow zero default column width
7645 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7647 if ( resizeExistingCols
)
7649 // since we are resizing all columns to the default column size,
7650 // we can simply clear the col widths and col rights
7651 // arrays (which also allows us to take advantage of
7652 // some speed optimisations)
7653 m_colWidths
.Empty();
7654 m_colRights
.Empty();
7655 if ( !GetBatchCount() )
7660 void wxGrid::SetColSize( int col
, int width
)
7662 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7664 // if < 0 then calculate new width from label
7668 wxArrayString lines
;
7669 wxClientDC
dc(m_colWindow
);
7670 dc
.SetFont(GetLabelFont());
7671 StringToLines(GetColLabelValue(col
), lines
);
7672 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7673 GetTextBoxSize( dc
, lines
, &w
, &h
);
7675 GetTextBoxSize( dc
, lines
, &h
, &w
);
7677 //check that it is not less than the minimal width
7678 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7681 // we intentionally don't test whether the width is less than
7682 // GetColMinimalWidth() here but we do compare it with
7683 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7684 // #651) -- and we also always allow the width of 0 as it has the special
7685 // sense of hiding the column
7686 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7689 if ( m_colWidths
.IsEmpty() )
7691 // need to really create the array
7695 const int diff
= width
- m_colWidths
[col
];
7696 m_colWidths
[col
] = width
;
7697 if ( m_useNativeHeader
)
7698 GetGridColHeader()->UpdateColumn(col
);
7699 //else: will be refreshed when the header is redrawn
7701 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7703 m_colRights
[GetColAt(colPos
)] += diff
;
7706 if ( !GetBatchCount() )
7713 void wxGrid::SetColMinimalWidth( int col
, int width
)
7715 if (width
> GetColMinimalAcceptableWidth())
7717 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7718 m_colMinWidths
[key
] = width
;
7722 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7724 if (width
> GetRowMinimalAcceptableHeight())
7726 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7727 m_rowMinHeights
[key
] = width
;
7731 int wxGrid::GetColMinimalWidth(int col
) const
7733 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7734 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7736 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7739 int wxGrid::GetRowMinimalHeight(int row
) const
7741 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7742 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7744 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7747 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7749 // We do allow a width of 0 since this gives us
7750 // an easy way to temporarily hiding columns.
7752 m_minAcceptableColWidth
= width
;
7755 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7757 // We do allow a height of 0 since this gives us
7758 // an easy way to temporarily hiding rows.
7760 m_minAcceptableRowHeight
= height
;
7763 int wxGrid::GetColMinimalAcceptableWidth() const
7765 return m_minAcceptableColWidth
;
7768 int wxGrid::GetRowMinimalAcceptableHeight() const
7770 return m_minAcceptableRowHeight
;
7773 // ----------------------------------------------------------------------------
7775 // ----------------------------------------------------------------------------
7778 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7780 const bool column
= direction
== wxGRID_COLUMN
;
7782 wxClientDC
dc(m_gridWin
);
7784 // cancel editing of cell
7785 HideCellEditControl();
7786 SaveEditControlValue();
7788 // init both of them to avoid compiler warnings, even if we only need one
7796 wxCoord extent
, extentMax
= 0;
7797 int max
= column
? m_numRows
: m_numCols
;
7798 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7805 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7806 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7809 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7810 extent
= column
? size
.x
: size
.y
;
7811 if ( extent
> extentMax
)
7820 // now also compare with the column label extent
7822 dc
.SetFont( GetLabelFont() );
7826 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7827 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7831 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7833 extent
= column
? w
: h
;
7834 if ( extent
> extentMax
)
7839 // empty column - give default extent (notice that if extentMax is less
7840 // than default extent but != 0, it's OK)
7841 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7846 // leave some space around text
7854 // Ensure automatic width is not less than minimal width. See the
7855 // comment in SetColSize() for explanation of why this isn't done
7858 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7860 SetColSize( col
, extentMax
);
7861 if ( !GetBatchCount() )
7863 if ( m_useNativeHeader
)
7865 GetGridColHeader()->UpdateColumn(col
);
7870 m_gridWin
->GetClientSize( &cw
, &ch
);
7871 wxRect
rect ( CellToRect( 0, col
) );
7873 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7874 rect
.width
= cw
- rect
.x
;
7875 rect
.height
= m_colLabelHeight
;
7876 GetColLabelWindow()->Refresh( true, &rect
);
7882 // Ensure automatic width is not less than minimal height. See the
7883 // comment in SetColSize() for explanation of why this isn't done
7886 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7888 SetRowSize(row
, extentMax
);
7889 if ( !GetBatchCount() )
7892 m_gridWin
->GetClientSize( &cw
, &ch
);
7893 wxRect
rect( CellToRect( row
, 0 ) );
7895 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
7896 rect
.width
= m_rowLabelWidth
;
7897 rect
.height
= ch
- rect
.y
;
7898 m_rowLabelWin
->Refresh( true, &rect
);
7905 SetColMinimalWidth(col
, extentMax
);
7907 SetRowMinimalHeight(row
, extentMax
);
7911 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
7913 // calculate size for the rows or columns?
7914 const bool calcRows
= direction
== wxGRID_ROW
;
7916 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
7917 : GetGridColLabelWindow());
7918 dc
.SetFont(GetLabelFont());
7920 // which dimension should we take into account for calculations?
7922 // for columns, the text can be only horizontal so it's easy but for rows
7923 // we also have to take into account the text orientation
7925 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
7927 wxArrayString lines
;
7928 wxCoord extentMax
= 0;
7930 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
7931 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
7935 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
7936 : GetColLabelValue(rowOrCol
);
7937 StringToLines(label
, lines
);
7940 GetTextBoxSize(dc
, lines
, &w
, &h
);
7942 const wxCoord extent
= useWidth
? w
: h
;
7943 if ( extent
> extentMax
)
7949 // empty column - give default extent (notice that if extentMax is less
7950 // than default extent but != 0, it's OK)
7951 extentMax
= calcRows
? GetDefaultRowLabelSize()
7952 : GetDefaultColLabelSize();
7955 // leave some space around text (taken from AutoSizeColOrRow)
7964 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7966 int width
= m_rowLabelWidth
;
7968 wxGridUpdateLocker locker
;
7970 locker
.Create(this);
7972 for ( int col
= 0; col
< m_numCols
; col
++ )
7975 AutoSizeColumn(col
, setAsMin
);
7977 width
+= GetColWidth(col
);
7983 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7985 int height
= m_colLabelHeight
;
7987 wxGridUpdateLocker locker
;
7989 locker
.Create(this);
7991 for ( int row
= 0; row
< m_numRows
; row
++ )
7994 AutoSizeRow(row
, setAsMin
);
7996 height
+= GetRowHeight(row
);
8002 void wxGrid::AutoSize()
8004 wxGridUpdateLocker
locker(this);
8006 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8007 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8009 // we know that we're not going to have scrollbars so disable them now to
8010 // avoid trouble in SetClientSize() which can otherwise set the correct
8011 // client size but also leave space for (not needed any more) scrollbars
8012 SetScrollbars(0, 0, 0, 0, 0, 0, true);
8014 // restore the scroll rate parameters overwritten by SetScrollbars()
8015 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
8017 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8020 void wxGrid::AutoSizeRowLabelSize( int row
)
8022 // Hide the edit control, so it
8023 // won't interfere with drag-shrinking.
8024 if ( IsCellEditControlShown() )
8026 HideCellEditControl();
8027 SaveEditControlValue();
8030 // autosize row height depending on label text
8031 SetRowSize(row
, -1);
8035 void wxGrid::AutoSizeColLabelSize( int col
)
8037 // Hide the edit control, so it
8038 // won't interfere with drag-shrinking.
8039 if ( IsCellEditControlShown() )
8041 HideCellEditControl();
8042 SaveEditControlValue();
8045 // autosize column width depending on label text
8046 SetColSize(col
, -1);
8050 wxSize
wxGrid::DoGetBestSize() const
8052 wxGrid
*self
= (wxGrid
*)this; // const_cast
8054 // we do the same as in AutoSize() here with the exception that we don't
8055 // change the column/row sizes, only calculate them
8056 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8057 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8059 // NOTE: This size should be cached, but first we need to add calls to
8060 // InvalidateBestSize everywhere that could change the results of this
8062 // CacheBestSize(size);
8064 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8065 + GetWindowBorderSize();
8073 wxPen
& wxGrid::GetDividerPen() const
8078 // ----------------------------------------------------------------------------
8079 // cell value accessor functions
8080 // ----------------------------------------------------------------------------
8082 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8086 m_table
->SetValue( row
, col
, s
);
8087 if ( !GetBatchCount() )
8090 wxRect
rect( CellToRect( row
, col
) );
8092 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8093 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8094 m_gridWin
->Refresh( false, &rect
);
8097 if ( m_currentCellCoords
.GetRow() == row
&&
8098 m_currentCellCoords
.GetCol() == col
&&
8099 IsCellEditControlShown())
8100 // Note: If we are using IsCellEditControlEnabled,
8101 // this interacts badly with calling SetCellValue from
8102 // an EVT_GRID_CELL_CHANGE handler.
8104 HideCellEditControl();
8105 ShowCellEditControl(); // will reread data from table
8110 // ----------------------------------------------------------------------------
8111 // block, row and column selection
8112 // ----------------------------------------------------------------------------
8114 void wxGrid::SelectRow( int row
, bool addToSelected
)
8119 if ( !addToSelected
)
8122 m_selection
->SelectRow(row
);
8125 void wxGrid::SelectCol( int col
, bool addToSelected
)
8130 if ( !addToSelected
)
8133 m_selection
->SelectCol(col
);
8136 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8142 if ( !addToSelected
)
8145 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8148 void wxGrid::SelectAll()
8150 if ( m_numRows
> 0 && m_numCols
> 0 )
8153 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8157 // ----------------------------------------------------------------------------
8158 // cell, row and col deselection
8159 // ----------------------------------------------------------------------------
8161 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8166 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8167 if ( mode
== oper
.GetSelectionMode() ||
8168 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8170 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8171 if ( m_selection
->IsInSelection(c
) )
8172 m_selection
->ToggleCellSelection(c
);
8174 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8176 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8177 for ( int i
= 0; i
< nOther
; i
++ )
8179 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8180 if ( m_selection
->IsInSelection(c
) )
8181 m_selection
->ToggleCellSelection(c
);
8184 //else: can only select orthogonal lines so no lines in this direction
8185 // could have been selected anyhow
8188 void wxGrid::DeselectRow(int row
)
8190 DeselectLine(row
, wxGridRowOperations());
8193 void wxGrid::DeselectCol(int col
)
8195 DeselectLine(col
, wxGridColumnOperations());
8198 void wxGrid::DeselectCell( int row
, int col
)
8200 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8201 m_selection
->ToggleCellSelection(row
, col
);
8204 bool wxGrid::IsSelection() const
8206 return ( m_selection
&& (m_selection
->IsSelection() ||
8207 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8208 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8211 bool wxGrid::IsInSelection( int row
, int col
) const
8213 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8214 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8215 col
>= m_selectedBlockTopLeft
.GetCol() &&
8216 row
<= m_selectedBlockBottomRight
.GetRow() &&
8217 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8220 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8224 wxGridCellCoordsArray a
;
8228 return m_selection
->m_cellSelection
;
8231 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8235 wxGridCellCoordsArray a
;
8239 return m_selection
->m_blockSelectionTopLeft
;
8242 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8246 wxGridCellCoordsArray a
;
8250 return m_selection
->m_blockSelectionBottomRight
;
8253 wxArrayInt
wxGrid::GetSelectedRows() const
8261 return m_selection
->m_rowSelection
;
8264 wxArrayInt
wxGrid::GetSelectedCols() const
8272 return m_selection
->m_colSelection
;
8275 void wxGrid::ClearSelection()
8277 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8278 m_selectedBlockBottomRight
);
8279 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8280 m_selectedBlockCorner
);
8282 m_selectedBlockTopLeft
=
8283 m_selectedBlockBottomRight
=
8284 m_selectedBlockCorner
= wxGridNoCellCoords
;
8286 if ( !r1
.IsEmpty() )
8287 RefreshRect(r1
, false);
8288 if ( !r2
.IsEmpty() )
8289 RefreshRect(r2
, false);
8292 m_selection
->ClearSelection();
8295 // This function returns the rectangle that encloses the given block
8296 // in device coords clipped to the client size of the grid window.
8298 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8299 const wxGridCellCoords
& bottomRight
) const
8302 wxRect tempCellRect
= CellToRect(topLeft
);
8303 if ( tempCellRect
!= wxGridNoCellRect
)
8305 resultRect
= tempCellRect
;
8309 resultRect
= wxRect(0, 0, 0, 0);
8312 tempCellRect
= CellToRect(bottomRight
);
8313 if ( tempCellRect
!= wxGridNoCellRect
)
8315 resultRect
+= tempCellRect
;
8319 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8320 return wxGridNoCellRect
;
8323 // Ensure that left/right and top/bottom pairs are in order.
8324 int left
= resultRect
.GetLeft();
8325 int top
= resultRect
.GetTop();
8326 int right
= resultRect
.GetRight();
8327 int bottom
= resultRect
.GetBottom();
8329 int leftCol
= topLeft
.GetCol();
8330 int topRow
= topLeft
.GetRow();
8331 int rightCol
= bottomRight
.GetCol();
8332 int bottomRow
= bottomRight
.GetRow();
8356 // The following loop is ONLY necessary to detect and handle merged cells.
8358 m_gridWin
->GetClientSize( &cw
, &ch
);
8360 // Get the origin coordinates: notice that they will be negative if the
8361 // grid is scrolled downwards/to the right.
8362 int gridOriginX
= 0;
8363 int gridOriginY
= 0;
8364 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8366 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8367 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8369 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8370 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8372 // Bound our loop so that we only examine the portion of the selected block
8373 // that is shown on screen. Therefore, we compare the Top-Left block values
8374 // to the Top-Left screen values, and the Bottom-Right block values to the
8375 // Bottom-Right screen values, choosing appropriately.
8376 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8377 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8378 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8379 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8381 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8383 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8385 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8386 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8388 tempCellRect
= CellToRect( j
, i
);
8390 if (tempCellRect
.x
< left
)
8391 left
= tempCellRect
.x
;
8392 if (tempCellRect
.y
< top
)
8393 top
= tempCellRect
.y
;
8394 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8395 right
= tempCellRect
.x
+ tempCellRect
.width
;
8396 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8397 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8401 i
= visibleRightCol
; // jump over inner cells.
8406 // Convert to scrolled coords
8407 CalcScrolledPosition( left
, top
, &left
, &top
);
8408 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8410 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8411 return wxRect(0,0,0,0);
8413 resultRect
.SetLeft( wxMax(0, left
) );
8414 resultRect
.SetTop( wxMax(0, top
) );
8415 resultRect
.SetRight( wxMin(cw
, right
) );
8416 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8421 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8422 const wxGridOperations
& oper
)
8425 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8426 const int numLines
= oper
.GetNumberOfLines(this);
8427 for ( int i
= 0; i
< numLines
; i
++ )
8429 int size
= sizeInfo
.GetSize(i
);
8430 if ( size
!= sizeInfo
.m_sizeDefault
)
8431 oper
.SetLineSize(this, i
, size
);
8436 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8438 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8441 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8443 DoSetSizes(sizeInfo
, wxGridRowOperations());
8446 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8448 m_sizeDefault
= defSize
;
8449 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8451 if ( allSizes
[i
] != defSize
)
8452 m_customSizes
[i
] = allSizes
[i
];
8456 int wxGridSizesInfo::GetSize(unsigned pos
) const
8458 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8460 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8463 // ----------------------------------------------------------------------------
8465 // ----------------------------------------------------------------------------
8467 #if wxUSE_DRAG_AND_DROP
8469 // this allow setting drop target directly on wxGrid
8470 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8472 GetGridWindow()->SetDropTarget(dropTarget
);
8475 #endif // wxUSE_DRAG_AND_DROP
8477 // ----------------------------------------------------------------------------
8478 // grid event classes
8479 // ----------------------------------------------------------------------------
8481 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8483 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8484 int row
, int col
, int x
, int y
, bool sel
,
8485 bool control
, bool shift
, bool alt
, bool meta
)
8486 : wxNotifyEvent( type
, id
),
8487 wxKeyboardState(control
, shift
, alt
, meta
)
8489 Init(row
, col
, x
, y
, sel
);
8491 SetEventObject(obj
);
8494 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8496 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8497 int rowOrCol
, int x
, int y
,
8498 bool control
, bool shift
, bool alt
, bool meta
)
8499 : wxNotifyEvent( type
, id
),
8500 wxKeyboardState(control
, shift
, alt
, meta
)
8502 Init(rowOrCol
, x
, y
);
8504 SetEventObject(obj
);
8508 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8510 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8511 const wxGridCellCoords
& topLeft
,
8512 const wxGridCellCoords
& bottomRight
,
8513 bool sel
, bool control
,
8514 bool shift
, bool alt
, bool meta
)
8515 : wxNotifyEvent( type
, id
),
8516 wxKeyboardState(control
, shift
, alt
, meta
)
8518 Init(topLeft
, bottomRight
, sel
);
8520 SetEventObject(obj
);
8524 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8526 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8527 wxObject
* obj
, int row
,
8528 int col
, wxControl
* ctrl
)
8529 : wxCommandEvent(type
, id
)
8531 SetEventObject(obj
);
8538 // ----------------------------------------------------------------------------
8539 // wxGridTypeRegistry
8540 // ----------------------------------------------------------------------------
8542 wxGridTypeRegistry::~wxGridTypeRegistry()
8544 size_t count
= m_typeinfo
.GetCount();
8545 for ( size_t i
= 0; i
< count
; i
++ )
8546 delete m_typeinfo
[i
];
8549 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8550 wxGridCellRenderer
* renderer
,
8551 wxGridCellEditor
* editor
)
8553 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8555 // is it already registered?
8556 int loc
= FindRegisteredDataType(typeName
);
8557 if ( loc
!= wxNOT_FOUND
)
8559 delete m_typeinfo
[loc
];
8560 m_typeinfo
[loc
] = info
;
8564 m_typeinfo
.Add(info
);
8568 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8570 size_t count
= m_typeinfo
.GetCount();
8571 for ( size_t i
= 0; i
< count
; i
++ )
8573 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8582 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8584 int index
= FindRegisteredDataType(typeName
);
8585 if ( index
== wxNOT_FOUND
)
8587 // check whether this is one of the standard ones, in which case
8588 // register it "on the fly"
8590 if ( typeName
== wxGRID_VALUE_STRING
)
8592 RegisterDataType(wxGRID_VALUE_STRING
,
8593 new wxGridCellStringRenderer
,
8594 new wxGridCellTextEditor
);
8597 #endif // wxUSE_TEXTCTRL
8599 if ( typeName
== wxGRID_VALUE_BOOL
)
8601 RegisterDataType(wxGRID_VALUE_BOOL
,
8602 new wxGridCellBoolRenderer
,
8603 new wxGridCellBoolEditor
);
8606 #endif // wxUSE_CHECKBOX
8608 if ( typeName
== wxGRID_VALUE_NUMBER
)
8610 RegisterDataType(wxGRID_VALUE_NUMBER
,
8611 new wxGridCellNumberRenderer
,
8612 new wxGridCellNumberEditor
);
8614 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8616 RegisterDataType(wxGRID_VALUE_FLOAT
,
8617 new wxGridCellFloatRenderer
,
8618 new wxGridCellFloatEditor
);
8621 #endif // wxUSE_TEXTCTRL
8623 if ( typeName
== wxGRID_VALUE_CHOICE
)
8625 RegisterDataType(wxGRID_VALUE_CHOICE
,
8626 new wxGridCellStringRenderer
,
8627 new wxGridCellChoiceEditor
);
8630 #endif // wxUSE_COMBOBOX
8635 // we get here only if just added the entry for this type, so return
8637 index
= m_typeinfo
.GetCount() - 1;
8643 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8645 int index
= FindDataType(typeName
);
8646 if ( index
== wxNOT_FOUND
)
8648 // the first part of the typename is the "real" type, anything after ':'
8649 // are the parameters for the renderer
8650 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8651 if ( index
== wxNOT_FOUND
)
8656 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8657 wxGridCellRenderer
*rendererOld
= renderer
;
8658 renderer
= renderer
->Clone();
8659 rendererOld
->DecRef();
8661 wxGridCellEditor
*editor
= GetEditor(index
);
8662 wxGridCellEditor
*editorOld
= editor
;
8663 editor
= editor
->Clone();
8664 editorOld
->DecRef();
8666 // do it even if there are no parameters to reset them to defaults
8667 wxString params
= typeName
.AfterFirst(wxT(':'));
8668 renderer
->SetParameters(params
);
8669 editor
->SetParameters(params
);
8671 // register the new typename
8672 RegisterDataType(typeName
, renderer
, editor
);
8674 // we just registered it, it's the last one
8675 index
= m_typeinfo
.GetCount() - 1;
8681 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8683 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8690 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8692 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8699 #endif // wxUSE_GRID