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
;
5447 // notice that an explicit static_cast is needed to avoid a compilation
5448 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5449 // wxGridRowHeaderRenderer class without it
5450 const wxGridRowHeaderRenderer
&
5451 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5452 : static_cast<const wxGridRowHeaderRenderer
&>
5453 (gs_defaultHeaderRenderers
.rowRenderer
);
5455 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5456 rend
.DrawBorder(*this, dc
, rect
);
5459 GetRowLabelAlignment(&hAlign
, &vAlign
);
5461 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5462 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5465 void wxGrid::UseNativeColHeader(bool native
)
5467 if ( native
== m_useNativeHeader
)
5471 m_useNativeHeader
= native
;
5473 CreateColumnWindow();
5475 if ( m_useNativeHeader
)
5476 GetGridColHeader()->SetColumnCount(m_numCols
);
5480 void wxGrid::SetUseNativeColLabels( bool native
)
5482 wxASSERT_MSG( !m_useNativeHeader
,
5483 "doesn't make sense when using native header" );
5485 m_nativeColumnLabels
= native
;
5488 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5489 SetColLabelSize( height
);
5492 GetColLabelWindow()->Refresh();
5493 m_cornerLabelWin
->Refresh();
5496 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5501 const size_t numLabels
= cols
.GetCount();
5502 for ( size_t i
= 0; i
< numLabels
; i
++ )
5504 DrawColLabel( dc
, cols
[i
] );
5508 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5510 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5512 if ( m_nativeColumnLabels
)
5516 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5523 wxGridCellAttrProvider
* const
5524 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5525 const wxGridCornerHeaderRenderer
&
5526 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5527 : static_cast<wxGridCornerHeaderRenderer
&>
5528 (gs_defaultHeaderRenderers
.cornerRenderer
);
5530 rend
.DrawBorder(*this, dc
, rect
);
5534 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5536 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5539 int colLeft
= GetColLeft(col
);
5541 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5542 wxGridCellAttrProvider
* const
5543 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5544 const wxGridColumnHeaderRenderer
&
5545 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
5546 : static_cast<wxGridColumnHeaderRenderer
&>
5547 (gs_defaultHeaderRenderers
.colRenderer
);
5549 if ( m_nativeColumnLabels
)
5551 wxRendererNative::Get().DrawHeaderButton
5553 GetColLabelWindow(),
5558 ? IsSortOrderAscending()
5559 ? wxHDR_SORT_ICON_UP
5560 : wxHDR_SORT_ICON_DOWN
5561 : wxHDR_SORT_ICON_NONE
5567 rend
.DrawBorder(*this, dc
, rect
);
5571 GetColLabelAlignment(&hAlign
, &vAlign
);
5572 const int orient
= GetColLabelTextOrientation();
5574 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5577 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5578 // we just have to add textOrientation support
5579 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5580 const wxString
& value
,
5584 int textOrientation
) const
5586 wxArrayString lines
;
5588 StringToLines( value
, lines
);
5590 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5593 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5594 const wxArrayString
& lines
,
5598 int textOrientation
) const
5600 if ( lines
.empty() )
5603 wxDCClipper
clip(dc
, rect
);
5608 if ( textOrientation
== wxHORIZONTAL
)
5609 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5611 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5615 switch ( vertAlign
)
5617 case wxALIGN_BOTTOM
:
5618 if ( textOrientation
== wxHORIZONTAL
)
5619 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5621 x
= rect
.x
+ rect
.width
- textWidth
;
5624 case wxALIGN_CENTRE
:
5625 if ( textOrientation
== wxHORIZONTAL
)
5626 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5628 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5633 if ( textOrientation
== wxHORIZONTAL
)
5640 // Align each line of a multi-line label
5641 size_t nLines
= lines
.GetCount();
5642 for ( size_t l
= 0; l
< nLines
; l
++ )
5644 const wxString
& line
= lines
[l
];
5648 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5652 wxCoord lineWidth
= 0,
5654 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5656 switch ( horizAlign
)
5659 if ( textOrientation
== wxHORIZONTAL
)
5660 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5662 y
= rect
.y
+ lineWidth
+ 1;
5665 case wxALIGN_CENTRE
:
5666 if ( textOrientation
== wxHORIZONTAL
)
5667 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5669 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5674 if ( textOrientation
== wxHORIZONTAL
)
5677 y
= rect
.y
+ rect
.height
- 1;
5681 if ( textOrientation
== wxHORIZONTAL
)
5683 dc
.DrawText( line
, x
, y
);
5688 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5694 // Split multi-line text up into an array of strings.
5695 // Any existing contents of the string array are preserved.
5697 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5698 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5702 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5703 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5705 while ( startPos
< (int)tVal
.length() )
5707 pos
= tVal
.Mid(startPos
).Find( eol
);
5712 else if ( pos
== 0 )
5714 lines
.Add( wxEmptyString
);
5718 lines
.Add( tVal
.Mid(startPos
, pos
) );
5721 startPos
+= pos
+ 1;
5724 if ( startPos
< (int)tVal
.length() )
5726 lines
.Add( tVal
.Mid( startPos
) );
5730 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5731 const wxArrayString
& lines
,
5732 long *width
, long *height
) const
5736 wxCoord lineW
= 0, lineH
= 0;
5739 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5741 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5742 w
= wxMax( w
, lineW
);
5751 // ------ Batch processing.
5753 void wxGrid::EndBatch()
5755 if ( m_batchCount
> 0 )
5758 if ( !m_batchCount
)
5761 m_rowLabelWin
->Refresh();
5762 m_colWindow
->Refresh();
5763 m_cornerLabelWin
->Refresh();
5764 m_gridWin
->Refresh();
5769 // Use this, rather than wxWindow::Refresh(), to force an immediate
5770 // repainting of the grid. Has no effect if you are already inside a
5771 // BeginBatch / EndBatch block.
5773 void wxGrid::ForceRefresh()
5779 bool wxGrid::Enable(bool enable
)
5781 if ( !wxScrolledWindow::Enable(enable
) )
5784 // redraw in the new state
5785 m_gridWin
->Refresh();
5791 // ------ Edit control functions
5794 void wxGrid::EnableEditing( bool edit
)
5796 if ( edit
!= m_editable
)
5799 EnableCellEditControl(edit
);
5804 void wxGrid::EnableCellEditControl( bool enable
)
5809 if ( enable
!= m_cellEditCtrlEnabled
)
5813 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5816 // this should be checked by the caller!
5817 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5819 // do it before ShowCellEditControl()
5820 m_cellEditCtrlEnabled
= enable
;
5822 ShowCellEditControl();
5826 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5828 HideCellEditControl();
5829 SaveEditControlValue();
5831 // do it after HideCellEditControl()
5832 m_cellEditCtrlEnabled
= enable
;
5837 bool wxGrid::IsCurrentCellReadOnly() const
5840 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5841 bool readonly
= attr
->IsReadOnly();
5847 bool wxGrid::CanEnableCellControl() const
5849 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5850 !IsCurrentCellReadOnly();
5853 bool wxGrid::IsCellEditControlEnabled() const
5855 // the cell edit control might be disable for all cells or just for the
5856 // current one if it's read only
5857 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5860 bool wxGrid::IsCellEditControlShown() const
5862 bool isShown
= false;
5864 if ( m_cellEditCtrlEnabled
)
5866 int row
= m_currentCellCoords
.GetRow();
5867 int col
= m_currentCellCoords
.GetCol();
5868 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5869 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5874 if ( editor
->IsCreated() )
5876 isShown
= editor
->GetControl()->IsShown();
5886 void wxGrid::ShowCellEditControl()
5888 if ( IsCellEditControlEnabled() )
5890 if ( !IsVisible( m_currentCellCoords
, false ) )
5892 m_cellEditCtrlEnabled
= false;
5897 wxRect rect
= CellToRect( m_currentCellCoords
);
5898 int row
= m_currentCellCoords
.GetRow();
5899 int col
= m_currentCellCoords
.GetCol();
5901 // if this is part of a multicell, find owner (topleft)
5902 int cell_rows
, cell_cols
;
5903 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5904 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5908 m_currentCellCoords
.SetRow( row
);
5909 m_currentCellCoords
.SetCol( col
);
5912 // erase the highlight and the cell contents because the editor
5913 // might not cover the entire cell
5914 wxClientDC
dc( m_gridWin
);
5916 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5917 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5918 dc
.SetPen(*wxTRANSPARENT_PEN
);
5919 dc
.DrawRectangle(rect
);
5921 // convert to scrolled coords
5922 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5928 // cell is shifted by one pixel
5929 // However, don't allow x or y to become negative
5930 // since the SetSize() method interprets that as
5937 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5938 if ( !editor
->IsCreated() )
5940 editor
->Create(m_gridWin
, wxID_ANY
,
5941 new wxGridCellEditorEvtHandler(this, editor
));
5943 wxGridEditorCreatedEvent
evt(GetId(),
5944 wxEVT_GRID_EDITOR_CREATED
,
5948 editor
->GetControl());
5949 GetEventHandler()->ProcessEvent(evt
);
5952 // resize editor to overflow into righthand cells if allowed
5953 int maxWidth
= rect
.width
;
5954 wxString value
= GetCellValue(row
, col
);
5955 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
5958 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
5959 if (maxWidth
< rect
.width
)
5960 maxWidth
= rect
.width
;
5963 int client_right
= m_gridWin
->GetClientSize().GetWidth();
5964 if (rect
.x
+ maxWidth
> client_right
)
5965 maxWidth
= client_right
- rect
.x
;
5967 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
5969 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5970 // may have changed earlier
5971 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
5974 GetCellSize( row
, i
, &c_rows
, &c_cols
);
5976 // looks weird going over a multicell
5977 if (m_table
->IsEmptyCell( row
, i
) &&
5978 (rect
.width
< maxWidth
) && (c_rows
== 1))
5980 rect
.width
+= GetColWidth( i
);
5986 if (rect
.GetRight() > client_right
)
5987 rect
.SetRight( client_right
- 1 );
5990 editor
->SetCellAttr( attr
);
5991 editor
->SetSize( rect
);
5993 editor
->GetControl()->Move(
5994 editor
->GetControl()->GetPosition().x
+ nXMove
,
5995 editor
->GetControl()->GetPosition().y
);
5996 editor
->Show( true, attr
);
5998 // recalc dimensions in case we need to
5999 // expand the scrolled window to account for editor
6002 editor
->BeginEdit(row
, col
, this);
6003 editor
->SetCellAttr(NULL
);
6011 void wxGrid::HideCellEditControl()
6013 if ( IsCellEditControlEnabled() )
6015 int row
= m_currentCellCoords
.GetRow();
6016 int col
= m_currentCellCoords
.GetCol();
6018 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6019 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6020 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6021 editor
->Show( false );
6025 // return the focus to the grid itself if the editor had it
6027 // note that we must not do this unconditionally to avoid stealing
6028 // focus from the window which just received it if we are hiding the
6029 // editor precisely because we lost focus
6030 if ( editorHadFocus
)
6031 m_gridWin
->SetFocus();
6033 // refresh whole row to the right
6034 wxRect
rect( CellToRect(row
, col
) );
6035 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6036 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6039 // ensure that the pixels under the focus ring get refreshed as well
6040 rect
.Inflate(10, 10);
6043 m_gridWin
->Refresh( false, &rect
);
6047 void wxGrid::SaveEditControlValue()
6049 if ( IsCellEditControlEnabled() )
6051 int row
= m_currentCellCoords
.GetRow();
6052 int col
= m_currentCellCoords
.GetCol();
6054 wxString oldval
= GetCellValue(row
, col
);
6056 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6057 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6060 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6062 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6064 editor
->ApplyEdit(row
, col
, this);
6066 // for compatibility reasons dating back to wx 2.8 when this event
6067 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6068 // didn't exist we allow vetoing this one too
6069 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6071 // Event has been vetoed, set the data back.
6072 SetCellValue(row
, col
, oldval
);
6082 // ------ Grid location functions
6083 // Note that all of these functions work with the logical coordinates of
6084 // grid cells and labels so you will need to convert from device
6085 // coordinates for mouse events etc.
6088 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6090 int row
= YToRow(y
);
6091 int col
= XToCol(x
);
6093 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6094 : wxGridCellCoords(row
, col
);
6097 // compute row or column from some (unscrolled) coordinate value, using either
6098 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6099 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6101 int wxGrid::PosToLinePos(int coord
,
6103 const wxGridOperations
& oper
) const
6105 const int numLines
= oper
.GetNumberOfLines(this);
6108 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6110 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6111 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6113 int maxPos
= coord
/ defaultLineSize
,
6116 // check for the simplest case: if we have no explicit line sizes
6117 // configured, then we already know the line this position falls in
6118 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6119 if ( lineEnds
.empty() )
6121 if ( maxPos
< numLines
)
6124 return clipToMinMax
? numLines
- 1 : -1;
6128 // adjust maxPos before starting the binary search
6129 if ( maxPos
>= numLines
)
6131 maxPos
= numLines
- 1;
6135 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6138 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6140 maxPos
= coord
/ minDist
;
6142 maxPos
= numLines
- 1;
6145 if ( maxPos
>= numLines
)
6146 maxPos
= numLines
- 1;
6149 // check if the position is beyond the last column
6150 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6151 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6152 return clipToMinMax
? maxPos
: -1;
6154 // or before the first one
6155 const int lineAt0
= oper
.GetLineAt(this, 0);
6156 if ( coord
< lineEnds
[lineAt0
] )
6160 // finally do perform the binary search
6161 while ( minPos
< maxPos
)
6163 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6164 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6166 "wxGrid: internal error in PosToLinePos()" );
6168 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6173 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6174 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6184 wxGrid::PosToLine(int coord
,
6186 const wxGridOperations
& oper
) const
6188 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6190 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6193 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6195 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6198 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6200 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6203 int wxGrid::XToPos(int x
) const
6205 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6208 // return the row number such that the y coord is near the edge of, or -1 if
6209 // not near an edge.
6211 // notice that position can only possibly be near an edge if the row/column is
6212 // large enough to still allow for an "inner" area that is _not_ near the edge
6213 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6214 // _never_ be considered to be near the edge).
6215 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6217 const int line
= oper
.PosToLine(this, pos
, true);
6219 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6221 // We know that we are in this line, test whether we are close enough
6222 // to start or end border, respectively.
6223 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6225 else if ( line
> 0 &&
6226 pos
- oper
.GetLineStartPos(this,
6227 line
) < WXGRID_LABEL_EDGE_ZONE
)
6234 int wxGrid::YToEdgeOfRow(int y
) const
6236 return PosToEdgeOfLine(y
, wxGridRowOperations());
6239 int wxGrid::XToEdgeOfCol(int x
) const
6241 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6244 wxRect
wxGrid::CellToRect( int row
, int col
) const
6246 wxRect
rect( -1, -1, -1, -1 );
6248 if ( row
>= 0 && row
< m_numRows
&&
6249 col
>= 0 && col
< m_numCols
)
6251 int i
, cell_rows
, cell_cols
;
6252 rect
.width
= rect
.height
= 0;
6253 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6254 // if negative then find multicell owner
6259 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6261 rect
.x
= GetColLeft(col
);
6262 rect
.y
= GetRowTop(row
);
6263 for (i
=col
; i
< col
+ cell_cols
; i
++)
6264 rect
.width
+= GetColWidth(i
);
6265 for (i
=row
; i
< row
+ cell_rows
; i
++)
6266 rect
.height
+= GetRowHeight(i
);
6268 // if grid lines are enabled, then the area of the cell is a bit smaller
6269 if (m_gridLinesEnabled
)
6279 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6281 // get the cell rectangle in logical coords
6283 wxRect
r( CellToRect( row
, col
) );
6285 // convert to device coords
6287 int left
, top
, right
, bottom
;
6288 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6289 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6291 // check against the client area of the grid window
6293 m_gridWin
->GetClientSize( &cw
, &ch
);
6295 if ( wholeCellVisible
)
6297 // is the cell wholly visible ?
6298 return ( left
>= 0 && right
<= cw
&&
6299 top
>= 0 && bottom
<= ch
);
6303 // is the cell partly visible ?
6305 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6306 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6310 // make the specified cell location visible by doing a minimal amount
6313 void wxGrid::MakeCellVisible( int row
, int col
)
6316 int xpos
= -1, ypos
= -1;
6318 if ( row
>= 0 && row
< m_numRows
&&
6319 col
>= 0 && col
< m_numCols
)
6321 // get the cell rectangle in logical coords
6322 wxRect
r( CellToRect( row
, col
) );
6324 // convert to device coords
6325 int left
, top
, right
, bottom
;
6326 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6327 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6330 m_gridWin
->GetClientSize( &cw
, &ch
);
6336 else if ( bottom
> ch
)
6338 int h
= r
.GetHeight();
6340 for ( i
= row
- 1; i
>= 0; i
-- )
6342 int rowHeight
= GetRowHeight(i
);
6343 if ( h
+ rowHeight
> ch
)
6350 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6351 // have rounding errors (this is important, because if we do,
6352 // we might not scroll at all and some cells won't be redrawn)
6354 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6355 // so just add a full scroll unit...
6356 ypos
+= m_scrollLineY
;
6359 // special handling for wide cells - show always left part of the cell!
6360 // Otherwise, e.g. when stepping from row to row, it would jump between
6361 // left and right part of the cell on every step!
6363 if ( left
< 0 || (right
- left
) >= cw
)
6367 else if ( right
> cw
)
6369 // position the view so that the cell is on the right
6371 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6372 xpos
= x0
+ (right
- cw
);
6374 // see comment for ypos above
6375 xpos
+= m_scrollLineX
;
6378 if ( xpos
!= -1 || ypos
!= -1 )
6381 xpos
/= m_scrollLineX
;
6383 ypos
/= m_scrollLineY
;
6384 Scroll( xpos
, ypos
);
6391 // ------ Grid cursor movement functions
6395 wxGrid::DoMoveCursor(bool expandSelection
,
6396 const wxGridDirectionOperations
& diroper
)
6398 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6401 if ( expandSelection
)
6403 wxGridCellCoords coords
= m_selectedBlockCorner
;
6404 if ( coords
== wxGridNoCellCoords
)
6405 coords
= m_currentCellCoords
;
6407 if ( diroper
.IsAtBoundary(coords
) )
6410 diroper
.Advance(coords
);
6412 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6414 else // don't expand selection
6418 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6421 wxGridCellCoords coords
= m_currentCellCoords
;
6422 diroper
.Advance(coords
);
6430 bool wxGrid::MoveCursorUp(bool expandSelection
)
6432 return DoMoveCursor(expandSelection
,
6433 wxGridBackwardOperations(this, wxGridRowOperations()));
6436 bool wxGrid::MoveCursorDown(bool expandSelection
)
6438 return DoMoveCursor(expandSelection
,
6439 wxGridForwardOperations(this, wxGridRowOperations()));
6442 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6444 return DoMoveCursor(expandSelection
,
6445 wxGridBackwardOperations(this, wxGridColumnOperations()));
6448 bool wxGrid::MoveCursorRight(bool expandSelection
)
6450 return DoMoveCursor(expandSelection
,
6451 wxGridForwardOperations(this, wxGridColumnOperations()));
6454 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6456 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6459 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6462 const int oldRow
= m_currentCellCoords
.GetRow();
6463 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6464 if ( newRow
== oldRow
)
6466 wxGridCellCoords
coords(m_currentCellCoords
);
6467 diroper
.Advance(coords
);
6468 newRow
= coords
.GetRow();
6471 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6476 bool wxGrid::MovePageUp()
6478 return DoMoveCursorByPage(
6479 wxGridBackwardOperations(this, wxGridRowOperations()));
6482 bool wxGrid::MovePageDown()
6484 return DoMoveCursorByPage(
6485 wxGridForwardOperations(this, wxGridRowOperations()));
6488 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6489 // until we find a non-empty cell or reach the grid end
6491 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6492 const wxGridDirectionOperations
& diroper
)
6494 while ( !diroper
.IsAtBoundary(coords
) )
6496 diroper
.Advance(coords
);
6497 if ( !m_table
->IsEmpty(coords
) )
6503 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6504 const wxGridDirectionOperations
& diroper
)
6506 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6509 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6512 wxGridCellCoords
coords(m_currentCellCoords
);
6513 if ( m_table
->IsEmpty(coords
) )
6515 // we are in an empty cell: find the next block of non-empty cells
6516 AdvanceToNextNonEmpty(coords
, diroper
);
6518 else // current cell is not empty
6520 diroper
.Advance(coords
);
6521 if ( m_table
->IsEmpty(coords
) )
6523 // we started at the end of a block, find the next one
6524 AdvanceToNextNonEmpty(coords
, diroper
);
6526 else // we're in a middle of a block
6528 // go to the end of it, i.e. find the last cell before the next
6530 while ( !diroper
.IsAtBoundary(coords
) )
6532 wxGridCellCoords
coordsNext(coords
);
6533 diroper
.Advance(coordsNext
);
6534 if ( m_table
->IsEmpty(coordsNext
) )
6537 coords
= coordsNext
;
6542 if ( expandSelection
)
6544 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6555 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6557 return DoMoveCursorByBlock(
6559 wxGridBackwardOperations(this, wxGridRowOperations())
6563 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6565 return DoMoveCursorByBlock(
6567 wxGridForwardOperations(this, wxGridRowOperations())
6571 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6573 return DoMoveCursorByBlock(
6575 wxGridBackwardOperations(this, wxGridColumnOperations())
6579 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6581 return DoMoveCursorByBlock(
6583 wxGridForwardOperations(this, wxGridColumnOperations())
6588 // ------ Label values and formatting
6591 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6594 *horiz
= m_rowLabelHorizAlign
;
6596 *vert
= m_rowLabelVertAlign
;
6599 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6602 *horiz
= m_colLabelHorizAlign
;
6604 *vert
= m_colLabelVertAlign
;
6607 int wxGrid::GetColLabelTextOrientation() const
6609 return m_colLabelTextOrientation
;
6612 wxString
wxGrid::GetRowLabelValue( int row
) const
6616 return m_table
->GetRowLabelValue( row
);
6626 wxString
wxGrid::GetColLabelValue( int col
) const
6630 return m_table
->GetColLabelValue( col
);
6640 void wxGrid::SetRowLabelSize( int width
)
6642 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6644 if ( width
== wxGRID_AUTOSIZE
)
6646 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6649 if ( width
!= m_rowLabelWidth
)
6653 m_rowLabelWin
->Show( false );
6654 m_cornerLabelWin
->Show( false );
6656 else if ( m_rowLabelWidth
== 0 )
6658 m_rowLabelWin
->Show( true );
6659 if ( m_colLabelHeight
> 0 )
6660 m_cornerLabelWin
->Show( true );
6663 m_rowLabelWidth
= width
;
6665 wxScrolledWindow::Refresh( true );
6669 void wxGrid::SetColLabelSize( int height
)
6671 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6673 if ( height
== wxGRID_AUTOSIZE
)
6675 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6678 if ( height
!= m_colLabelHeight
)
6682 m_colWindow
->Show( false );
6683 m_cornerLabelWin
->Show( false );
6685 else if ( m_colLabelHeight
== 0 )
6687 m_colWindow
->Show( true );
6688 if ( m_rowLabelWidth
> 0 )
6689 m_cornerLabelWin
->Show( true );
6692 m_colLabelHeight
= height
;
6694 wxScrolledWindow::Refresh( true );
6698 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6700 if ( m_labelBackgroundColour
!= colour
)
6702 m_labelBackgroundColour
= colour
;
6703 m_rowLabelWin
->SetBackgroundColour( colour
);
6704 m_colWindow
->SetBackgroundColour( colour
);
6705 m_cornerLabelWin
->SetBackgroundColour( colour
);
6707 if ( !GetBatchCount() )
6709 m_rowLabelWin
->Refresh();
6710 m_colWindow
->Refresh();
6711 m_cornerLabelWin
->Refresh();
6716 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6718 if ( m_labelTextColour
!= colour
)
6720 m_labelTextColour
= colour
;
6721 if ( !GetBatchCount() )
6723 m_rowLabelWin
->Refresh();
6724 m_colWindow
->Refresh();
6729 void wxGrid::SetLabelFont( const wxFont
& font
)
6732 if ( !GetBatchCount() )
6734 m_rowLabelWin
->Refresh();
6735 m_colWindow
->Refresh();
6739 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6741 // allow old (incorrect) defs to be used
6744 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6745 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6746 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6751 case wxTOP
: vert
= wxALIGN_TOP
; break;
6752 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6753 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6756 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6758 m_rowLabelHorizAlign
= horiz
;
6761 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6763 m_rowLabelVertAlign
= vert
;
6766 if ( !GetBatchCount() )
6768 m_rowLabelWin
->Refresh();
6772 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6774 // allow old (incorrect) defs to be used
6777 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6778 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6779 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6784 case wxTOP
: vert
= wxALIGN_TOP
; break;
6785 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6786 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6789 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6791 m_colLabelHorizAlign
= horiz
;
6794 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6796 m_colLabelVertAlign
= vert
;
6799 if ( !GetBatchCount() )
6801 m_colWindow
->Refresh();
6805 // Note: under MSW, the default column label font must be changed because it
6806 // does not support vertical printing
6808 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6809 // pGrid->SetLabelFont(font);
6810 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6812 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6814 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6815 m_colLabelTextOrientation
= textOrientation
;
6817 if ( !GetBatchCount() )
6818 m_colWindow
->Refresh();
6821 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6825 m_table
->SetRowLabelValue( row
, s
);
6826 if ( !GetBatchCount() )
6828 wxRect rect
= CellToRect( row
, 0 );
6829 if ( rect
.height
> 0 )
6831 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6833 rect
.width
= m_rowLabelWidth
;
6834 m_rowLabelWin
->Refresh( true, &rect
);
6840 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6844 m_table
->SetColLabelValue( col
, s
);
6845 if ( !GetBatchCount() )
6847 if ( m_useNativeHeader
)
6849 GetGridColHeader()->UpdateColumn(col
);
6853 wxRect rect
= CellToRect( 0, col
);
6854 if ( rect
.width
> 0 )
6856 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6858 rect
.height
= m_colLabelHeight
;
6859 GetColLabelWindow()->Refresh( true, &rect
);
6866 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6868 if ( m_gridLineColour
!= colour
)
6870 m_gridLineColour
= colour
;
6872 if ( GridLinesEnabled() )
6877 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6879 if ( m_cellHighlightColour
!= colour
)
6881 m_cellHighlightColour
= colour
;
6883 wxClientDC
dc( m_gridWin
);
6885 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6886 DrawCellHighlight(dc
, attr
);
6891 void wxGrid::SetCellHighlightPenWidth(int width
)
6893 if (m_cellHighlightPenWidth
!= width
)
6895 m_cellHighlightPenWidth
= width
;
6897 // Just redrawing the cell highlight is not enough since that won't
6898 // make any visible change if the the thickness is getting smaller.
6899 int row
= m_currentCellCoords
.GetRow();
6900 int col
= m_currentCellCoords
.GetCol();
6901 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6904 wxRect rect
= CellToRect(row
, col
);
6905 m_gridWin
->Refresh(true, &rect
);
6909 void wxGrid::SetCellHighlightROPenWidth(int width
)
6911 if (m_cellHighlightROPenWidth
!= width
)
6913 m_cellHighlightROPenWidth
= width
;
6915 // Just redrawing the cell highlight is not enough since that won't
6916 // make any visible change if the the thickness is getting smaller.
6917 int row
= m_currentCellCoords
.GetRow();
6918 int col
= m_currentCellCoords
.GetCol();
6919 if ( row
== -1 || col
== -1 ||
6920 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6923 wxRect rect
= CellToRect(row
, col
);
6924 m_gridWin
->Refresh(true, &rect
);
6928 void wxGrid::RedrawGridLines()
6930 // the lines will be redrawn when the window is thawn
6931 if ( GetBatchCount() )
6934 if ( GridLinesEnabled() )
6936 wxClientDC
dc( m_gridWin
);
6938 DrawAllGridLines( dc
, wxRegion() );
6940 else // remove the grid lines
6942 m_gridWin
->Refresh();
6946 void wxGrid::EnableGridLines( bool enable
)
6948 if ( enable
!= m_gridLinesEnabled
)
6950 m_gridLinesEnabled
= enable
;
6956 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
6962 if ( GridLinesEnabled() )
6967 int wxGrid::GetDefaultRowSize() const
6969 return m_defaultRowHeight
;
6972 int wxGrid::GetRowSize( int row
) const
6974 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
6976 return GetRowHeight(row
);
6979 int wxGrid::GetDefaultColSize() const
6981 return m_defaultColWidth
;
6984 int wxGrid::GetColSize( int col
) const
6986 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
6988 return GetColWidth(col
);
6991 // ============================================================================
6992 // access to the grid attributes: each of them has a default value in the grid
6993 // itself and may be overidden on a per-cell basis
6994 // ============================================================================
6996 // ----------------------------------------------------------------------------
6997 // setting default attributes
6998 // ----------------------------------------------------------------------------
7000 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7002 m_defaultCellAttr
->SetBackgroundColour(col
);
7004 m_gridWin
->SetBackgroundColour(col
);
7008 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7010 m_defaultCellAttr
->SetTextColour(col
);
7013 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7015 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7018 void wxGrid::SetDefaultCellOverflow( bool allow
)
7020 m_defaultCellAttr
->SetOverflow(allow
);
7023 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7025 m_defaultCellAttr
->SetFont(font
);
7028 // For editors and renderers the type registry takes precedence over the
7029 // default attr, so we need to register the new editor/renderer for the string
7030 // data type in order to make setting a default editor/renderer appear to
7033 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7035 RegisterDataType(wxGRID_VALUE_STRING
,
7037 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7040 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7042 RegisterDataType(wxGRID_VALUE_STRING
,
7043 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7047 // ----------------------------------------------------------------------------
7048 // access to the default attributes
7049 // ----------------------------------------------------------------------------
7051 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7053 return m_defaultCellAttr
->GetBackgroundColour();
7056 wxColour
wxGrid::GetDefaultCellTextColour() const
7058 return m_defaultCellAttr
->GetTextColour();
7061 wxFont
wxGrid::GetDefaultCellFont() const
7063 return m_defaultCellAttr
->GetFont();
7066 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7068 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7071 bool wxGrid::GetDefaultCellOverflow() const
7073 return m_defaultCellAttr
->GetOverflow();
7076 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7078 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7081 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7083 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7086 // ----------------------------------------------------------------------------
7087 // access to cell attributes
7088 // ----------------------------------------------------------------------------
7090 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7092 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7093 wxColour colour
= attr
->GetBackgroundColour();
7099 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7101 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7102 wxColour colour
= attr
->GetTextColour();
7108 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7110 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7111 wxFont font
= attr
->GetFont();
7117 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7119 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7120 attr
->GetAlignment(horiz
, vert
);
7124 bool wxGrid::GetCellOverflow( int row
, int col
) const
7126 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7127 bool allow
= attr
->GetOverflow();
7133 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7135 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7136 attr
->GetSize( num_rows
, num_cols
);
7140 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7142 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7143 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7149 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7151 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7152 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7158 bool wxGrid::IsReadOnly(int row
, int col
) const
7160 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7161 bool isReadOnly
= attr
->IsReadOnly();
7167 // ----------------------------------------------------------------------------
7168 // attribute support: cache, automatic provider creation, ...
7169 // ----------------------------------------------------------------------------
7171 bool wxGrid::CanHaveAttributes() const
7178 return m_table
->CanHaveAttributes();
7181 void wxGrid::ClearAttrCache()
7183 if ( m_attrCache
.row
!= -1 )
7185 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7186 m_attrCache
.attr
= NULL
;
7187 m_attrCache
.row
= -1;
7188 // wxSafeDecRec(...) might cause event processing that accesses
7189 // the cached attribute, if one exists (e.g. by deleting the
7190 // editor stored within the attribute). Therefore it is important
7191 // to invalidate the cache before calling wxSafeDecRef!
7192 wxSafeDecRef(oldAttr
);
7196 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7200 wxGrid
*self
= (wxGrid
*)this; // const_cast
7202 self
->ClearAttrCache();
7203 self
->m_attrCache
.row
= row
;
7204 self
->m_attrCache
.col
= col
;
7205 self
->m_attrCache
.attr
= attr
;
7210 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7212 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7214 *attr
= m_attrCache
.attr
;
7215 wxSafeIncRef(m_attrCache
.attr
);
7217 #ifdef DEBUG_ATTR_CACHE
7218 gs_nAttrCacheHits
++;
7225 #ifdef DEBUG_ATTR_CACHE
7226 gs_nAttrCacheMisses
++;
7233 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7235 wxGridCellAttr
*attr
= NULL
;
7236 // Additional test to avoid looking at the cache e.g. for
7237 // wxNoCellCoords, as this will confuse memory management.
7240 if ( !LookupAttr(row
, col
, &attr
) )
7242 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7244 CacheAttr(row
, col
, attr
);
7250 attr
->SetDefAttr(m_defaultCellAttr
);
7254 attr
= m_defaultCellAttr
;
7261 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7263 wxGridCellAttr
*attr
= NULL
;
7264 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7266 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7267 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7269 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7272 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7274 // artificially inc the ref count to match DecRef() in caller
7276 m_table
->SetAttr(attr
, row
, col
);
7282 // ----------------------------------------------------------------------------
7283 // setting column attributes (wrappers around SetColAttr)
7284 // ----------------------------------------------------------------------------
7286 void wxGrid::SetColFormatBool(int col
)
7288 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7291 void wxGrid::SetColFormatNumber(int col
)
7293 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7296 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7298 wxString typeName
= wxGRID_VALUE_FLOAT
;
7299 if ( (width
!= -1) || (precision
!= -1) )
7301 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7304 SetColFormatCustom(col
, typeName
);
7307 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7309 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7311 attr
= new wxGridCellAttr
;
7312 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7313 attr
->SetRenderer(renderer
);
7314 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7315 attr
->SetEditor(editor
);
7317 SetColAttr(col
, attr
);
7321 // ----------------------------------------------------------------------------
7322 // setting cell attributes: this is forwarded to the table
7323 // ----------------------------------------------------------------------------
7325 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7327 if ( CanHaveAttributes() )
7329 m_table
->SetAttr(attr
, row
, col
);
7338 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7340 if ( CanHaveAttributes() )
7342 m_table
->SetRowAttr(attr
, row
);
7351 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7353 if ( CanHaveAttributes() )
7355 m_table
->SetColAttr(attr
, col
);
7364 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7366 if ( CanHaveAttributes() )
7368 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7369 attr
->SetBackgroundColour(colour
);
7374 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7376 if ( CanHaveAttributes() )
7378 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7379 attr
->SetTextColour(colour
);
7384 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7386 if ( CanHaveAttributes() )
7388 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7389 attr
->SetFont(font
);
7394 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7396 if ( CanHaveAttributes() )
7398 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7399 attr
->SetAlignment(horiz
, vert
);
7404 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7406 if ( CanHaveAttributes() )
7408 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7409 attr
->SetOverflow(allow
);
7414 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7416 if ( CanHaveAttributes() )
7418 int cell_rows
, cell_cols
;
7420 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7421 attr
->GetSize(&cell_rows
, &cell_cols
);
7422 attr
->SetSize(num_rows
, num_cols
);
7425 // Cannot set the size of a cell to 0 or negative values
7426 // While it is perfectly legal to do that, this function cannot
7427 // handle all the possibilies, do it by hand by getting the CellAttr.
7428 // You can only set the size of a cell to 1,1 or greater with this fn
7429 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7430 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7431 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7432 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7434 // if this was already a multicell then "turn off" the other cells first
7435 if ((cell_rows
> 1) || (cell_cols
> 1))
7438 for (j
=row
; j
< row
+ cell_rows
; j
++)
7440 for (i
=col
; i
< col
+ cell_cols
; i
++)
7442 if ((i
!= col
) || (j
!= row
))
7444 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7445 attr_stub
->SetSize( 1, 1 );
7446 attr_stub
->DecRef();
7452 // mark the cells that will be covered by this cell to
7453 // negative or zero values to point back at this cell
7454 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7457 for (j
=row
; j
< row
+ num_rows
; j
++)
7459 for (i
=col
; i
< col
+ num_cols
; i
++)
7461 if ((i
!= col
) || (j
!= row
))
7463 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7464 attr_stub
->SetSize( row
- j
, col
- i
);
7465 attr_stub
->DecRef();
7473 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7475 if ( CanHaveAttributes() )
7477 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7478 attr
->SetRenderer(renderer
);
7483 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7485 if ( CanHaveAttributes() )
7487 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7488 attr
->SetEditor(editor
);
7493 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7495 if ( CanHaveAttributes() )
7497 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7498 attr
->SetReadOnly(isReadOnly
);
7503 // ----------------------------------------------------------------------------
7504 // Data type registration
7505 // ----------------------------------------------------------------------------
7507 void wxGrid::RegisterDataType(const wxString
& typeName
,
7508 wxGridCellRenderer
* renderer
,
7509 wxGridCellEditor
* editor
)
7511 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7515 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7517 wxString typeName
= m_table
->GetTypeName(row
, col
);
7518 return GetDefaultEditorForType(typeName
);
7521 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7523 wxString typeName
= m_table
->GetTypeName(row
, col
);
7524 return GetDefaultRendererForType(typeName
);
7527 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7529 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7530 if ( index
== wxNOT_FOUND
)
7532 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7537 return m_typeRegistry
->GetEditor(index
);
7540 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7542 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7543 if ( index
== wxNOT_FOUND
)
7545 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7550 return m_typeRegistry
->GetRenderer(index
);
7553 // ----------------------------------------------------------------------------
7555 // ----------------------------------------------------------------------------
7557 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7561 setFixed
= new wxGridFixedIndicesSet
;
7564 setFixed
->insert(line
);
7568 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7570 return !setFixed
|| !setFixed
->count(line
);
7573 void wxGrid::EnableDragRowSize( bool enable
)
7575 m_canDragRowSize
= enable
;
7578 void wxGrid::EnableDragColSize( bool enable
)
7580 m_canDragColSize
= enable
;
7583 void wxGrid::EnableDragGridSize( bool enable
)
7585 m_canDragGridSize
= enable
;
7588 void wxGrid::EnableDragCell( bool enable
)
7590 m_canDragCell
= enable
;
7593 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7595 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7597 if ( resizeExistingRows
)
7599 // since we are resizing all rows to the default row size,
7600 // we can simply clear the row heights and row bottoms
7601 // arrays (which also allows us to take advantage of
7602 // some speed optimisations)
7603 m_rowHeights
.Empty();
7604 m_rowBottoms
.Empty();
7605 if ( !GetBatchCount() )
7610 void wxGrid::SetRowSize( int row
, int height
)
7612 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7614 // if < 0 then calculate new height from label
7618 wxArrayString lines
;
7619 wxClientDC
dc(m_rowLabelWin
);
7620 dc
.SetFont(GetLabelFont());
7621 StringToLines(GetRowLabelValue( row
), lines
);
7622 GetTextBoxSize( dc
, lines
, &w
, &h
);
7623 //check that it is not less than the minimal height
7624 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7627 // See comment in SetColSize
7628 if ( height
< GetRowMinimalAcceptableHeight())
7631 if ( m_rowHeights
.IsEmpty() )
7633 // need to really create the array
7637 int h
= wxMax( 0, height
);
7638 int diff
= h
- m_rowHeights
[row
];
7640 m_rowHeights
[row
] = h
;
7641 for ( int i
= row
; i
< m_numRows
; i
++ )
7643 m_rowBottoms
[i
] += diff
;
7646 if ( !GetBatchCount() )
7650 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7652 // we dont allow zero default column width
7653 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7655 if ( resizeExistingCols
)
7657 // since we are resizing all columns to the default column size,
7658 // we can simply clear the col widths and col rights
7659 // arrays (which also allows us to take advantage of
7660 // some speed optimisations)
7661 m_colWidths
.Empty();
7662 m_colRights
.Empty();
7663 if ( !GetBatchCount() )
7668 void wxGrid::SetColSize( int col
, int width
)
7670 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7672 // if < 0 then calculate new width from label
7676 wxArrayString lines
;
7677 wxClientDC
dc(m_colWindow
);
7678 dc
.SetFont(GetLabelFont());
7679 StringToLines(GetColLabelValue(col
), lines
);
7680 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7681 GetTextBoxSize( dc
, lines
, &w
, &h
);
7683 GetTextBoxSize( dc
, lines
, &h
, &w
);
7685 //check that it is not less than the minimal width
7686 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7689 // we intentionally don't test whether the width is less than
7690 // GetColMinimalWidth() here but we do compare it with
7691 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7692 // #651) -- and we also always allow the width of 0 as it has the special
7693 // sense of hiding the column
7694 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7697 if ( m_colWidths
.IsEmpty() )
7699 // need to really create the array
7703 const int diff
= width
- m_colWidths
[col
];
7704 m_colWidths
[col
] = width
;
7705 if ( m_useNativeHeader
)
7706 GetGridColHeader()->UpdateColumn(col
);
7707 //else: will be refreshed when the header is redrawn
7709 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7711 m_colRights
[GetColAt(colPos
)] += diff
;
7714 if ( !GetBatchCount() )
7721 void wxGrid::SetColMinimalWidth( int col
, int width
)
7723 if (width
> GetColMinimalAcceptableWidth())
7725 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7726 m_colMinWidths
[key
] = width
;
7730 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7732 if (width
> GetRowMinimalAcceptableHeight())
7734 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7735 m_rowMinHeights
[key
] = width
;
7739 int wxGrid::GetColMinimalWidth(int col
) const
7741 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7742 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7744 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7747 int wxGrid::GetRowMinimalHeight(int row
) const
7749 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7750 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7752 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7755 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7757 // We do allow a width of 0 since this gives us
7758 // an easy way to temporarily hiding columns.
7760 m_minAcceptableColWidth
= width
;
7763 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7765 // We do allow a height of 0 since this gives us
7766 // an easy way to temporarily hiding rows.
7768 m_minAcceptableRowHeight
= height
;
7771 int wxGrid::GetColMinimalAcceptableWidth() const
7773 return m_minAcceptableColWidth
;
7776 int wxGrid::GetRowMinimalAcceptableHeight() const
7778 return m_minAcceptableRowHeight
;
7781 // ----------------------------------------------------------------------------
7783 // ----------------------------------------------------------------------------
7786 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7788 const bool column
= direction
== wxGRID_COLUMN
;
7790 wxClientDC
dc(m_gridWin
);
7792 // cancel editing of cell
7793 HideCellEditControl();
7794 SaveEditControlValue();
7796 // init both of them to avoid compiler warnings, even if we only need one
7804 wxCoord extent
, extentMax
= 0;
7805 int max
= column
? m_numRows
: m_numCols
;
7806 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7813 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7814 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7817 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7818 extent
= column
? size
.x
: size
.y
;
7819 if ( extent
> extentMax
)
7828 // now also compare with the column label extent
7830 dc
.SetFont( GetLabelFont() );
7834 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7835 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7839 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7841 extent
= column
? w
: h
;
7842 if ( extent
> extentMax
)
7847 // empty column - give default extent (notice that if extentMax is less
7848 // than default extent but != 0, it's OK)
7849 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7854 // leave some space around text
7862 // Ensure automatic width is not less than minimal width. See the
7863 // comment in SetColSize() for explanation of why this isn't done
7866 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7868 SetColSize( col
, extentMax
);
7869 if ( !GetBatchCount() )
7871 if ( m_useNativeHeader
)
7873 GetGridColHeader()->UpdateColumn(col
);
7878 m_gridWin
->GetClientSize( &cw
, &ch
);
7879 wxRect
rect ( CellToRect( 0, col
) );
7881 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7882 rect
.width
= cw
- rect
.x
;
7883 rect
.height
= m_colLabelHeight
;
7884 GetColLabelWindow()->Refresh( true, &rect
);
7890 // Ensure automatic width is not less than minimal height. See the
7891 // comment in SetColSize() for explanation of why this isn't done
7894 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7896 SetRowSize(row
, extentMax
);
7897 if ( !GetBatchCount() )
7900 m_gridWin
->GetClientSize( &cw
, &ch
);
7901 wxRect
rect( CellToRect( row
, 0 ) );
7903 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
7904 rect
.width
= m_rowLabelWidth
;
7905 rect
.height
= ch
- rect
.y
;
7906 m_rowLabelWin
->Refresh( true, &rect
);
7913 SetColMinimalWidth(col
, extentMax
);
7915 SetRowMinimalHeight(row
, extentMax
);
7919 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
7921 // calculate size for the rows or columns?
7922 const bool calcRows
= direction
== wxGRID_ROW
;
7924 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
7925 : GetGridColLabelWindow());
7926 dc
.SetFont(GetLabelFont());
7928 // which dimension should we take into account for calculations?
7930 // for columns, the text can be only horizontal so it's easy but for rows
7931 // we also have to take into account the text orientation
7933 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
7935 wxArrayString lines
;
7936 wxCoord extentMax
= 0;
7938 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
7939 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
7943 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
7944 : GetColLabelValue(rowOrCol
);
7945 StringToLines(label
, lines
);
7948 GetTextBoxSize(dc
, lines
, &w
, &h
);
7950 const wxCoord extent
= useWidth
? w
: h
;
7951 if ( extent
> extentMax
)
7957 // empty column - give default extent (notice that if extentMax is less
7958 // than default extent but != 0, it's OK)
7959 extentMax
= calcRows
? GetDefaultRowLabelSize()
7960 : GetDefaultColLabelSize();
7963 // leave some space around text (taken from AutoSizeColOrRow)
7972 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7974 int width
= m_rowLabelWidth
;
7976 wxGridUpdateLocker locker
;
7978 locker
.Create(this);
7980 for ( int col
= 0; col
< m_numCols
; col
++ )
7983 AutoSizeColumn(col
, setAsMin
);
7985 width
+= GetColWidth(col
);
7991 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7993 int height
= m_colLabelHeight
;
7995 wxGridUpdateLocker locker
;
7997 locker
.Create(this);
7999 for ( int row
= 0; row
< m_numRows
; row
++ )
8002 AutoSizeRow(row
, setAsMin
);
8004 height
+= GetRowHeight(row
);
8010 void wxGrid::AutoSize()
8012 wxGridUpdateLocker
locker(this);
8014 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8015 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8017 // we know that we're not going to have scrollbars so disable them now to
8018 // avoid trouble in SetClientSize() which can otherwise set the correct
8019 // client size but also leave space for (not needed any more) scrollbars
8020 SetScrollbars(0, 0, 0, 0, 0, 0, true);
8022 // restore the scroll rate parameters overwritten by SetScrollbars()
8023 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
8025 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8028 void wxGrid::AutoSizeRowLabelSize( int row
)
8030 // Hide the edit control, so it
8031 // won't interfere with drag-shrinking.
8032 if ( IsCellEditControlShown() )
8034 HideCellEditControl();
8035 SaveEditControlValue();
8038 // autosize row height depending on label text
8039 SetRowSize(row
, -1);
8043 void wxGrid::AutoSizeColLabelSize( int col
)
8045 // Hide the edit control, so it
8046 // won't interfere with drag-shrinking.
8047 if ( IsCellEditControlShown() )
8049 HideCellEditControl();
8050 SaveEditControlValue();
8053 // autosize column width depending on label text
8054 SetColSize(col
, -1);
8058 wxSize
wxGrid::DoGetBestSize() const
8060 wxGrid
*self
= (wxGrid
*)this; // const_cast
8062 // we do the same as in AutoSize() here with the exception that we don't
8063 // change the column/row sizes, only calculate them
8064 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8065 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8067 // NOTE: This size should be cached, but first we need to add calls to
8068 // InvalidateBestSize everywhere that could change the results of this
8070 // CacheBestSize(size);
8072 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8073 + GetWindowBorderSize();
8081 wxPen
& wxGrid::GetDividerPen() const
8086 // ----------------------------------------------------------------------------
8087 // cell value accessor functions
8088 // ----------------------------------------------------------------------------
8090 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8094 m_table
->SetValue( row
, col
, s
);
8095 if ( !GetBatchCount() )
8098 wxRect
rect( CellToRect( row
, col
) );
8100 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8101 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8102 m_gridWin
->Refresh( false, &rect
);
8105 if ( m_currentCellCoords
.GetRow() == row
&&
8106 m_currentCellCoords
.GetCol() == col
&&
8107 IsCellEditControlShown())
8108 // Note: If we are using IsCellEditControlEnabled,
8109 // this interacts badly with calling SetCellValue from
8110 // an EVT_GRID_CELL_CHANGE handler.
8112 HideCellEditControl();
8113 ShowCellEditControl(); // will reread data from table
8118 // ----------------------------------------------------------------------------
8119 // block, row and column selection
8120 // ----------------------------------------------------------------------------
8122 void wxGrid::SelectRow( int row
, bool addToSelected
)
8127 if ( !addToSelected
)
8130 m_selection
->SelectRow(row
);
8133 void wxGrid::SelectCol( int col
, bool addToSelected
)
8138 if ( !addToSelected
)
8141 m_selection
->SelectCol(col
);
8144 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8150 if ( !addToSelected
)
8153 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8156 void wxGrid::SelectAll()
8158 if ( m_numRows
> 0 && m_numCols
> 0 )
8161 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8165 // ----------------------------------------------------------------------------
8166 // cell, row and col deselection
8167 // ----------------------------------------------------------------------------
8169 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8174 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8175 if ( mode
== oper
.GetSelectionMode() ||
8176 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8178 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8179 if ( m_selection
->IsInSelection(c
) )
8180 m_selection
->ToggleCellSelection(c
);
8182 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8184 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8185 for ( int i
= 0; i
< nOther
; i
++ )
8187 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8188 if ( m_selection
->IsInSelection(c
) )
8189 m_selection
->ToggleCellSelection(c
);
8192 //else: can only select orthogonal lines so no lines in this direction
8193 // could have been selected anyhow
8196 void wxGrid::DeselectRow(int row
)
8198 DeselectLine(row
, wxGridRowOperations());
8201 void wxGrid::DeselectCol(int col
)
8203 DeselectLine(col
, wxGridColumnOperations());
8206 void wxGrid::DeselectCell( int row
, int col
)
8208 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8209 m_selection
->ToggleCellSelection(row
, col
);
8212 bool wxGrid::IsSelection() const
8214 return ( m_selection
&& (m_selection
->IsSelection() ||
8215 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8216 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8219 bool wxGrid::IsInSelection( int row
, int col
) const
8221 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8222 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8223 col
>= m_selectedBlockTopLeft
.GetCol() &&
8224 row
<= m_selectedBlockBottomRight
.GetRow() &&
8225 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8228 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8232 wxGridCellCoordsArray a
;
8236 return m_selection
->m_cellSelection
;
8239 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8243 wxGridCellCoordsArray a
;
8247 return m_selection
->m_blockSelectionTopLeft
;
8250 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8254 wxGridCellCoordsArray a
;
8258 return m_selection
->m_blockSelectionBottomRight
;
8261 wxArrayInt
wxGrid::GetSelectedRows() const
8269 return m_selection
->m_rowSelection
;
8272 wxArrayInt
wxGrid::GetSelectedCols() const
8280 return m_selection
->m_colSelection
;
8283 void wxGrid::ClearSelection()
8285 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8286 m_selectedBlockBottomRight
);
8287 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8288 m_selectedBlockCorner
);
8290 m_selectedBlockTopLeft
=
8291 m_selectedBlockBottomRight
=
8292 m_selectedBlockCorner
= wxGridNoCellCoords
;
8294 if ( !r1
.IsEmpty() )
8295 RefreshRect(r1
, false);
8296 if ( !r2
.IsEmpty() )
8297 RefreshRect(r2
, false);
8300 m_selection
->ClearSelection();
8303 // This function returns the rectangle that encloses the given block
8304 // in device coords clipped to the client size of the grid window.
8306 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8307 const wxGridCellCoords
& bottomRight
) const
8310 wxRect tempCellRect
= CellToRect(topLeft
);
8311 if ( tempCellRect
!= wxGridNoCellRect
)
8313 resultRect
= tempCellRect
;
8317 resultRect
= wxRect(0, 0, 0, 0);
8320 tempCellRect
= CellToRect(bottomRight
);
8321 if ( tempCellRect
!= wxGridNoCellRect
)
8323 resultRect
+= tempCellRect
;
8327 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8328 return wxGridNoCellRect
;
8331 // Ensure that left/right and top/bottom pairs are in order.
8332 int left
= resultRect
.GetLeft();
8333 int top
= resultRect
.GetTop();
8334 int right
= resultRect
.GetRight();
8335 int bottom
= resultRect
.GetBottom();
8337 int leftCol
= topLeft
.GetCol();
8338 int topRow
= topLeft
.GetRow();
8339 int rightCol
= bottomRight
.GetCol();
8340 int bottomRow
= bottomRight
.GetRow();
8364 // The following loop is ONLY necessary to detect and handle merged cells.
8366 m_gridWin
->GetClientSize( &cw
, &ch
);
8368 // Get the origin coordinates: notice that they will be negative if the
8369 // grid is scrolled downwards/to the right.
8370 int gridOriginX
= 0;
8371 int gridOriginY
= 0;
8372 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8374 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8375 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8377 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8378 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8380 // Bound our loop so that we only examine the portion of the selected block
8381 // that is shown on screen. Therefore, we compare the Top-Left block values
8382 // to the Top-Left screen values, and the Bottom-Right block values to the
8383 // Bottom-Right screen values, choosing appropriately.
8384 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8385 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8386 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8387 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8389 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8391 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8393 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8394 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8396 tempCellRect
= CellToRect( j
, i
);
8398 if (tempCellRect
.x
< left
)
8399 left
= tempCellRect
.x
;
8400 if (tempCellRect
.y
< top
)
8401 top
= tempCellRect
.y
;
8402 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8403 right
= tempCellRect
.x
+ tempCellRect
.width
;
8404 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8405 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8409 i
= visibleRightCol
; // jump over inner cells.
8414 // Convert to scrolled coords
8415 CalcScrolledPosition( left
, top
, &left
, &top
);
8416 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8418 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8419 return wxRect(0,0,0,0);
8421 resultRect
.SetLeft( wxMax(0, left
) );
8422 resultRect
.SetTop( wxMax(0, top
) );
8423 resultRect
.SetRight( wxMin(cw
, right
) );
8424 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8429 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8430 const wxGridOperations
& oper
)
8433 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8434 const int numLines
= oper
.GetNumberOfLines(this);
8435 for ( int i
= 0; i
< numLines
; i
++ )
8437 int size
= sizeInfo
.GetSize(i
);
8438 if ( size
!= sizeInfo
.m_sizeDefault
)
8439 oper
.SetLineSize(this, i
, size
);
8444 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8446 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8449 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8451 DoSetSizes(sizeInfo
, wxGridRowOperations());
8454 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8456 m_sizeDefault
= defSize
;
8457 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8459 if ( allSizes
[i
] != defSize
)
8460 m_customSizes
[i
] = allSizes
[i
];
8464 int wxGridSizesInfo::GetSize(unsigned pos
) const
8466 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8468 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8471 // ----------------------------------------------------------------------------
8473 // ----------------------------------------------------------------------------
8475 #if wxUSE_DRAG_AND_DROP
8477 // this allow setting drop target directly on wxGrid
8478 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8480 GetGridWindow()->SetDropTarget(dropTarget
);
8483 #endif // wxUSE_DRAG_AND_DROP
8485 // ----------------------------------------------------------------------------
8486 // grid event classes
8487 // ----------------------------------------------------------------------------
8489 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8491 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8492 int row
, int col
, int x
, int y
, bool sel
,
8493 bool control
, bool shift
, bool alt
, bool meta
)
8494 : wxNotifyEvent( type
, id
),
8495 wxKeyboardState(control
, shift
, alt
, meta
)
8497 Init(row
, col
, x
, y
, sel
);
8499 SetEventObject(obj
);
8502 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8504 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8505 int rowOrCol
, int x
, int y
,
8506 bool control
, bool shift
, bool alt
, bool meta
)
8507 : wxNotifyEvent( type
, id
),
8508 wxKeyboardState(control
, shift
, alt
, meta
)
8510 Init(rowOrCol
, x
, y
);
8512 SetEventObject(obj
);
8516 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8518 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8519 const wxGridCellCoords
& topLeft
,
8520 const wxGridCellCoords
& bottomRight
,
8521 bool sel
, bool control
,
8522 bool shift
, bool alt
, bool meta
)
8523 : wxNotifyEvent( type
, id
),
8524 wxKeyboardState(control
, shift
, alt
, meta
)
8526 Init(topLeft
, bottomRight
, sel
);
8528 SetEventObject(obj
);
8532 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8534 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8535 wxObject
* obj
, int row
,
8536 int col
, wxControl
* ctrl
)
8537 : wxCommandEvent(type
, id
)
8539 SetEventObject(obj
);
8546 // ----------------------------------------------------------------------------
8547 // wxGridTypeRegistry
8548 // ----------------------------------------------------------------------------
8550 wxGridTypeRegistry::~wxGridTypeRegistry()
8552 size_t count
= m_typeinfo
.GetCount();
8553 for ( size_t i
= 0; i
< count
; i
++ )
8554 delete m_typeinfo
[i
];
8557 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8558 wxGridCellRenderer
* renderer
,
8559 wxGridCellEditor
* editor
)
8561 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8563 // is it already registered?
8564 int loc
= FindRegisteredDataType(typeName
);
8565 if ( loc
!= wxNOT_FOUND
)
8567 delete m_typeinfo
[loc
];
8568 m_typeinfo
[loc
] = info
;
8572 m_typeinfo
.Add(info
);
8576 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8578 size_t count
= m_typeinfo
.GetCount();
8579 for ( size_t i
= 0; i
< count
; i
++ )
8581 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8590 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8592 int index
= FindRegisteredDataType(typeName
);
8593 if ( index
== wxNOT_FOUND
)
8595 // check whether this is one of the standard ones, in which case
8596 // register it "on the fly"
8598 if ( typeName
== wxGRID_VALUE_STRING
)
8600 RegisterDataType(wxGRID_VALUE_STRING
,
8601 new wxGridCellStringRenderer
,
8602 new wxGridCellTextEditor
);
8605 #endif // wxUSE_TEXTCTRL
8607 if ( typeName
== wxGRID_VALUE_BOOL
)
8609 RegisterDataType(wxGRID_VALUE_BOOL
,
8610 new wxGridCellBoolRenderer
,
8611 new wxGridCellBoolEditor
);
8614 #endif // wxUSE_CHECKBOX
8616 if ( typeName
== wxGRID_VALUE_NUMBER
)
8618 RegisterDataType(wxGRID_VALUE_NUMBER
,
8619 new wxGridCellNumberRenderer
,
8620 new wxGridCellNumberEditor
);
8622 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8624 RegisterDataType(wxGRID_VALUE_FLOAT
,
8625 new wxGridCellFloatRenderer
,
8626 new wxGridCellFloatEditor
);
8629 #endif // wxUSE_TEXTCTRL
8631 if ( typeName
== wxGRID_VALUE_CHOICE
)
8633 RegisterDataType(wxGRID_VALUE_CHOICE
,
8634 new wxGridCellStringRenderer
,
8635 new wxGridCellChoiceEditor
);
8638 #endif // wxUSE_COMBOBOX
8643 // we get here only if just added the entry for this type, so return
8645 index
= m_typeinfo
.GetCount() - 1;
8651 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8653 int index
= FindDataType(typeName
);
8654 if ( index
== wxNOT_FOUND
)
8656 // the first part of the typename is the "real" type, anything after ':'
8657 // are the parameters for the renderer
8658 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8659 if ( index
== wxNOT_FOUND
)
8664 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8665 wxGridCellRenderer
*rendererOld
= renderer
;
8666 renderer
= renderer
->Clone();
8667 rendererOld
->DecRef();
8669 wxGridCellEditor
*editor
= GetEditor(index
);
8670 wxGridCellEditor
*editorOld
= editor
;
8671 editor
= editor
->Clone();
8672 editorOld
->DecRef();
8674 // do it even if there are no parameters to reset them to defaults
8675 wxString params
= typeName
.AfterFirst(wxT(':'));
8676 renderer
->SetParameters(params
);
8677 editor
->SetParameters(params
);
8679 // register the new typename
8680 RegisterDataType(typeName
, renderer
, editor
);
8682 // we just registered it, it's the last one
8683 index
= m_typeinfo
.GetCount() - 1;
8689 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8691 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8698 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8700 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8707 #endif // wxUSE_GRID