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::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
481 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
484 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
488 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
491 *num_rows
= m_sizeRows
;
493 *num_cols
= m_sizeCols
;
496 // GetRenderer and GetEditor use a slightly different decision path about
497 // which attribute to use. If a non-default attr object has one then it is
498 // used, otherwise the default editor or renderer is fetched from the grid and
499 // used. It should be the default for the data type of the cell. If it is
500 // NULL (because the table has a type that the grid does not have in its
501 // registry), then the grid's default editor or renderer is used.
503 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
505 wxGridCellRenderer
*renderer
= NULL
;
507 if ( m_renderer
&& this != m_defGridAttr
)
509 // use the cells renderer if it has one
510 renderer
= m_renderer
;
513 else // no non-default cell renderer
515 // get default renderer for the data type
518 // GetDefaultRendererForCell() will do IncRef() for us
519 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
522 if ( renderer
== NULL
)
524 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
526 // if we still don't have one then use the grid default
527 // (no need for IncRef() here neither)
528 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
530 else // default grid attr
532 // use m_renderer which we had decided not to use initially
533 renderer
= m_renderer
;
540 // we're supposed to always find something
541 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
546 // same as above, except for s/renderer/editor/g
547 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
549 wxGridCellEditor
*editor
= NULL
;
551 if ( m_editor
&& this != m_defGridAttr
)
553 // use the cells editor if it has one
557 else // no non default cell editor
559 // get default editor for the data type
562 // GetDefaultEditorForCell() will do IncRef() for us
563 editor
= grid
->GetDefaultEditorForCell(row
, col
);
566 if ( editor
== NULL
)
568 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
570 // if we still don't have one then use the grid default
571 // (no need for IncRef() here neither)
572 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
574 else // default grid attr
576 // use m_editor which we had decided not to use initially
584 // we're supposed to always find something
585 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
590 // ----------------------------------------------------------------------------
591 // wxGridCellAttrData
592 // ----------------------------------------------------------------------------
594 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
596 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
597 // touch attribute's reference counting explicitly, since this
598 // is managed by class wxGridCellWithAttr
599 int n
= FindIndex(row
, col
);
600 if ( n
== wxNOT_FOUND
)
605 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
607 //else: nothing to do
609 else // we already have an attribute for this cell
613 // change the attribute
614 m_attrs
[(size_t)n
].ChangeAttr(attr
);
618 // remove this attribute
619 m_attrs
.RemoveAt((size_t)n
);
624 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
626 wxGridCellAttr
*attr
= NULL
;
628 int n
= FindIndex(row
, col
);
629 if ( n
!= wxNOT_FOUND
)
631 attr
= m_attrs
[(size_t)n
].attr
;
638 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
640 size_t count
= m_attrs
.GetCount();
641 for ( size_t n
= 0; n
< count
; n
++ )
643 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
644 wxCoord row
= coords
.GetRow();
645 if ((size_t)row
>= pos
)
649 // If rows inserted, include row counter where necessary
650 coords
.SetRow(row
+ numRows
);
652 else if (numRows
< 0)
654 // If rows deleted ...
655 if ((size_t)row
>= pos
- numRows
)
657 // ...either decrement row counter (if row still exists)...
658 coords
.SetRow(row
+ numRows
);
662 // ...or remove the attribute
672 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
674 size_t count
= m_attrs
.GetCount();
675 for ( size_t n
= 0; n
< count
; n
++ )
677 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
678 wxCoord col
= coords
.GetCol();
679 if ( (size_t)col
>= pos
)
683 // If rows inserted, include row counter where necessary
684 coords
.SetCol(col
+ numCols
);
686 else if (numCols
< 0)
688 // If rows deleted ...
689 if ((size_t)col
>= pos
- numCols
)
691 // ...either decrement row counter (if row still exists)...
692 coords
.SetCol(col
+ numCols
);
696 // ...or remove the attribute
706 int wxGridCellAttrData::FindIndex(int row
, int col
) const
708 size_t count
= m_attrs
.GetCount();
709 for ( size_t n
= 0; n
< count
; n
++ )
711 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
712 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
721 // ----------------------------------------------------------------------------
722 // wxGridRowOrColAttrData
723 // ----------------------------------------------------------------------------
725 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
727 size_t count
= m_attrs
.GetCount();
728 for ( size_t n
= 0; n
< count
; n
++ )
730 m_attrs
[n
]->DecRef();
734 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
736 wxGridCellAttr
*attr
= NULL
;
738 int n
= m_rowsOrCols
.Index(rowOrCol
);
739 if ( n
!= wxNOT_FOUND
)
741 attr
= m_attrs
[(size_t)n
];
748 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
750 int i
= m_rowsOrCols
.Index(rowOrCol
);
751 if ( i
== wxNOT_FOUND
)
755 // store the new attribute, taking its ownership
756 m_rowsOrCols
.Add(rowOrCol
);
761 else // we have an attribute for this row or column
763 size_t n
= (size_t)i
;
765 // notice that this code works correctly even when the old attribute is
766 // the same as the new one: as we own of it, we must call DecRef() on
767 // it in any case and this won't result in destruction of the new
768 // attribute if it's the same as old one because it must have ref count
769 // of at least 2 to be passed to us while we keep a reference to it too
770 m_attrs
[n
]->DecRef();
774 // replace the attribute with the new one
777 else // remove the attribute
779 m_rowsOrCols
.RemoveAt(n
);
785 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
787 size_t count
= m_attrs
.GetCount();
788 for ( size_t n
= 0; n
< count
; n
++ )
790 int & rowOrCol
= m_rowsOrCols
[n
];
791 if ( (size_t)rowOrCol
>= pos
)
793 if ( numRowsOrCols
> 0 )
795 // If rows inserted, include row counter where necessary
796 rowOrCol
+= numRowsOrCols
;
798 else if ( numRowsOrCols
< 0)
800 // If rows deleted, either decrement row counter (if row still exists)
801 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
802 rowOrCol
+= numRowsOrCols
;
805 m_rowsOrCols
.RemoveAt(n
);
806 m_attrs
[n
]->DecRef();
816 // ----------------------------------------------------------------------------
817 // wxGridCellAttrProvider
818 // ----------------------------------------------------------------------------
820 wxGridCellAttrProvider::wxGridCellAttrProvider()
825 wxGridCellAttrProvider::~wxGridCellAttrProvider()
830 void wxGridCellAttrProvider::InitData()
832 m_data
= new wxGridCellAttrProviderData
;
835 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
836 wxGridCellAttr::wxAttrKind kind
) const
838 wxGridCellAttr
*attr
= NULL
;
843 case (wxGridCellAttr::Any
):
844 // Get cached merge attributes.
845 // Currently not used as no cache implemented as not mutable
846 // attr = m_data->m_mergeAttr.GetAttr(row, col);
849 // Basically implement old version.
850 // Also check merge cache, so we don't have to re-merge every time..
851 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
852 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
853 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
855 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
857 // Two or more are non NULL
858 attr
= new wxGridCellAttr
;
859 attr
->SetKind(wxGridCellAttr::Merged
);
861 // Order is important..
864 attr
->MergeWith(attrcell
);
869 attr
->MergeWith(attrcol
);
874 attr
->MergeWith(attrrow
);
878 // store merge attr if cache implemented
880 //m_data->m_mergeAttr.SetAttr(attr, row, col);
884 // one or none is non null return it or null.
903 case (wxGridCellAttr::Cell
):
904 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
907 case (wxGridCellAttr::Col
):
908 attr
= m_data
->m_colAttrs
.GetAttr(col
);
911 case (wxGridCellAttr::Row
):
912 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
917 // (wxGridCellAttr::Default):
918 // (wxGridCellAttr::Merged):
926 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
932 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
935 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
940 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
943 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
948 m_data
->m_colAttrs
.SetAttr(attr
, col
);
951 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
955 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
957 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
961 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
965 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
967 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
971 const wxGridColumnHeaderRenderer
&
972 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
974 return gs_defaultHeaderRenderers
.colRenderer
;
977 const wxGridRowHeaderRenderer
&
978 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
980 return gs_defaultHeaderRenderers
.rowRenderer
;
983 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
985 return gs_defaultHeaderRenderers
.cornerRenderer
;
988 // ----------------------------------------------------------------------------
990 // ----------------------------------------------------------------------------
992 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
994 wxGridTableBase::wxGridTableBase()
997 m_attrProvider
= NULL
;
1000 wxGridTableBase::~wxGridTableBase()
1002 delete m_attrProvider
;
1005 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1007 delete m_attrProvider
;
1008 m_attrProvider
= attrProvider
;
1011 bool wxGridTableBase::CanHaveAttributes()
1013 if ( ! GetAttrProvider() )
1015 // use the default attr provider by default
1016 SetAttrProvider(new wxGridCellAttrProvider
);
1022 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1024 if ( m_attrProvider
)
1025 return m_attrProvider
->GetAttr(row
, col
, kind
);
1030 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1032 if ( m_attrProvider
)
1035 attr
->SetKind(wxGridCellAttr::Cell
);
1036 m_attrProvider
->SetAttr(attr
, row
, col
);
1040 // as we take ownership of the pointer and don't store it, we must
1046 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1048 if ( m_attrProvider
)
1050 attr
->SetKind(wxGridCellAttr::Row
);
1051 m_attrProvider
->SetRowAttr(attr
, row
);
1055 // as we take ownership of the pointer and don't store it, we must
1061 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1063 if ( m_attrProvider
)
1065 attr
->SetKind(wxGridCellAttr::Col
);
1066 m_attrProvider
->SetColAttr(attr
, col
);
1070 // as we take ownership of the pointer and don't store it, we must
1076 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1077 size_t WXUNUSED(numRows
) )
1079 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1084 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1086 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1091 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1092 size_t WXUNUSED(numRows
) )
1094 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1099 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1100 size_t WXUNUSED(numCols
) )
1102 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1107 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1109 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1114 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1115 size_t WXUNUSED(numCols
) )
1117 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1122 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1126 // RD: Starting the rows at zero confuses users,
1127 // no matter how much it makes sense to us geeks.
1133 wxString
wxGridTableBase::GetColLabelValue( int col
)
1135 // default col labels are:
1136 // cols 0 to 25 : A-Z
1137 // cols 26 to 675 : AA-ZZ
1142 for ( n
= 1; ; n
++ )
1144 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1150 // reverse the string...
1152 for ( i
= 0; i
< n
; i
++ )
1160 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1162 return wxGRID_VALUE_STRING
;
1165 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1166 const wxString
& typeName
)
1168 return typeName
== wxGRID_VALUE_STRING
;
1171 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1173 return CanGetValueAs(row
, col
, typeName
);
1176 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1181 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1186 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1191 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1192 long WXUNUSED(value
) )
1196 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1197 double WXUNUSED(value
) )
1201 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1202 bool WXUNUSED(value
) )
1206 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1207 const wxString
& WXUNUSED(typeName
) )
1212 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1213 const wxString
& WXUNUSED(typeName
),
1214 void* WXUNUSED(value
) )
1218 //////////////////////////////////////////////////////////////////////
1220 // Message class for the grid table to send requests and notifications
1224 wxGridTableMessage::wxGridTableMessage()
1232 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1233 int commandInt1
, int commandInt2
)
1237 m_comInt1
= commandInt1
;
1238 m_comInt2
= commandInt2
;
1241 //////////////////////////////////////////////////////////////////////
1243 // A basic grid table for string data. An object of this class will
1244 // created by wxGrid if you don't specify an alternative table class.
1247 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1249 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1251 wxGridStringTable::wxGridStringTable()
1257 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1260 m_numCols
= numCols
;
1262 m_data
.Alloc( numRows
);
1265 sa
.Alloc( numCols
);
1266 sa
.Add( wxEmptyString
, numCols
);
1268 m_data
.Add( sa
, numRows
);
1271 wxString
wxGridStringTable::GetValue( int row
, int col
)
1273 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1274 (col
>= 0 && col
< GetNumberCols()),
1276 wxT("invalid row or column index in wxGridStringTable") );
1278 return m_data
[row
][col
];
1281 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1283 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1284 (col
>= 0 && col
< GetNumberCols()),
1285 wxT("invalid row or column index in wxGridStringTable") );
1287 m_data
[row
][col
] = value
;
1290 void wxGridStringTable::Clear()
1293 int numRows
, numCols
;
1295 numRows
= m_data
.GetCount();
1298 numCols
= m_data
[0].GetCount();
1300 for ( row
= 0; row
< numRows
; row
++ )
1302 for ( col
= 0; col
< numCols
; col
++ )
1304 m_data
[row
][col
] = wxEmptyString
;
1310 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1312 size_t curNumRows
= m_data
.GetCount();
1313 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1314 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1316 if ( pos
>= curNumRows
)
1318 return AppendRows( numRows
);
1322 sa
.Alloc( curNumCols
);
1323 sa
.Add( wxEmptyString
, curNumCols
);
1324 m_data
.Insert( sa
, pos
, numRows
);
1328 wxGridTableMessage
msg( this,
1329 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1333 GetView()->ProcessTableMessage( msg
);
1339 bool wxGridStringTable::AppendRows( size_t numRows
)
1341 size_t curNumRows
= m_data
.GetCount();
1342 size_t curNumCols
= ( curNumRows
> 0
1343 ? m_data
[0].GetCount()
1344 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1347 if ( curNumCols
> 0 )
1349 sa
.Alloc( curNumCols
);
1350 sa
.Add( wxEmptyString
, curNumCols
);
1353 m_data
.Add( sa
, numRows
);
1357 wxGridTableMessage
msg( this,
1358 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1361 GetView()->ProcessTableMessage( msg
);
1367 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1369 size_t curNumRows
= m_data
.GetCount();
1371 if ( pos
>= curNumRows
)
1373 wxFAIL_MSG( wxString::Format
1375 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1377 (unsigned long)numRows
,
1378 (unsigned long)curNumRows
1384 if ( numRows
> curNumRows
- pos
)
1386 numRows
= curNumRows
- pos
;
1389 if ( numRows
>= curNumRows
)
1395 m_data
.RemoveAt( pos
, numRows
);
1400 wxGridTableMessage
msg( this,
1401 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1405 GetView()->ProcessTableMessage( msg
);
1411 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1415 size_t curNumRows
= m_data
.GetCount();
1416 size_t curNumCols
= ( curNumRows
> 0
1417 ? m_data
[0].GetCount()
1418 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1420 if ( pos
>= curNumCols
)
1422 return AppendCols( numCols
);
1425 if ( !m_colLabels
.IsEmpty() )
1427 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1430 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1431 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1434 for ( row
= 0; row
< curNumRows
; row
++ )
1436 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1438 m_data
[row
].Insert( wxEmptyString
, col
);
1442 m_numCols
+= numCols
;
1446 wxGridTableMessage
msg( this,
1447 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1451 GetView()->ProcessTableMessage( msg
);
1457 bool wxGridStringTable::AppendCols( size_t numCols
)
1461 size_t curNumRows
= m_data
.GetCount();
1463 for ( row
= 0; row
< curNumRows
; row
++ )
1465 m_data
[row
].Add( wxEmptyString
, numCols
);
1468 m_numCols
+= numCols
;
1472 wxGridTableMessage
msg( this,
1473 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1476 GetView()->ProcessTableMessage( msg
);
1482 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1486 size_t curNumRows
= m_data
.GetCount();
1487 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1488 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1490 if ( pos
>= curNumCols
)
1492 wxFAIL_MSG( wxString::Format
1494 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1496 (unsigned long)numCols
,
1497 (unsigned long)curNumCols
1504 colID
= GetView()->GetColAt( pos
);
1508 if ( numCols
> curNumCols
- colID
)
1510 numCols
= curNumCols
- colID
;
1513 if ( !m_colLabels
.IsEmpty() )
1515 // m_colLabels stores just as many elements as it needs, e.g. if only
1516 // the label of the first column had been set it would have only one
1517 // element and not numCols, so account for it
1518 int nToRm
= m_colLabels
.size() - colID
;
1520 m_colLabels
.RemoveAt( colID
, nToRm
);
1523 if ( numCols
>= curNumCols
)
1525 for ( row
= 0; row
< curNumRows
; row
++ )
1527 m_data
[row
].Clear();
1532 else // something will be left
1534 for ( row
= 0; row
< curNumRows
; row
++ )
1536 m_data
[row
].RemoveAt( colID
, numCols
);
1539 m_numCols
-= numCols
;
1544 wxGridTableMessage
msg( this,
1545 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1549 GetView()->ProcessTableMessage( msg
);
1555 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1557 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1559 // using default label
1561 return wxGridTableBase::GetRowLabelValue( row
);
1565 return m_rowLabels
[row
];
1569 wxString
wxGridStringTable::GetColLabelValue( int col
)
1571 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1573 // using default label
1575 return wxGridTableBase::GetColLabelValue( col
);
1579 return m_colLabels
[col
];
1583 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1585 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1587 int n
= m_rowLabels
.GetCount();
1590 for ( i
= n
; i
<= row
; i
++ )
1592 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1596 m_rowLabels
[row
] = value
;
1599 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1601 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1603 int n
= m_colLabels
.GetCount();
1606 for ( i
= n
; i
<= col
; i
++ )
1608 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1612 m_colLabels
[col
] = value
;
1616 //////////////////////////////////////////////////////////////////////
1617 //////////////////////////////////////////////////////////////////////
1619 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1620 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1623 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1625 m_owner
->CancelMouseCapture();
1628 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1629 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1630 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1631 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1634 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1638 // NO - don't do this because it will set both the x and y origin
1639 // coords to match the parent scrolled window and we just want to
1640 // set the y coord - MB
1642 // m_owner->PrepareDC( dc );
1645 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1646 wxPoint pt
= dc
.GetDeviceOrigin();
1647 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1649 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1650 m_owner
->DrawRowLabels( dc
, rows
);
1653 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1655 m_owner
->ProcessRowLabelMouseEvent( event
);
1658 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1660 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1664 //////////////////////////////////////////////////////////////////////
1666 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1667 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1668 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1669 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1672 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1676 // NO - don't do this because it will set both the x and y origin
1677 // coords to match the parent scrolled window and we just want to
1678 // set the x coord - MB
1680 // m_owner->PrepareDC( dc );
1683 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1684 wxPoint pt
= dc
.GetDeviceOrigin();
1685 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1686 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1688 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1690 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1691 m_owner
->DrawColLabels( dc
, cols
);
1694 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1696 m_owner
->ProcessColLabelMouseEvent( event
);
1699 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1701 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1705 //////////////////////////////////////////////////////////////////////
1707 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1708 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1709 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1710 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1713 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1717 m_owner
->DrawCornerLabel(dc
);
1720 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1722 m_owner
->ProcessCornerLabelMouseEvent( event
);
1725 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1727 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1731 //////////////////////////////////////////////////////////////////////
1733 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1734 EVT_PAINT( wxGridWindow::OnPaint
)
1735 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1736 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1737 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1738 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1739 EVT_CHAR( wxGridWindow::OnChar
)
1740 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1741 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1742 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1745 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1747 wxPaintDC
dc( this );
1748 m_owner
->PrepareDC( dc
);
1749 wxRegion reg
= GetUpdateRegion();
1750 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1751 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1753 m_owner
->DrawGridSpace( dc
);
1755 m_owner
->DrawAllGridLines( dc
, reg
);
1757 m_owner
->DrawHighlight( dc
, dirtyCells
);
1760 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1762 wxWindow::ScrollWindow( dx
, dy
, rect
);
1763 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1764 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1767 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1769 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1772 m_owner
->ProcessGridCellMouseEvent( event
);
1775 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1777 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1781 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1782 // cursor must be in the cell edit control to get key events
1784 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1786 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1790 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1792 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1796 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1798 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1802 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1806 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1808 // and if we have any selection, it has to be repainted, because it
1809 // uses different colour when the grid is not focused:
1810 if ( m_owner
->IsSelection() )
1816 // NB: Note that this code is in "else" branch only because the other
1817 // branch refreshes everything and so there's no point in calling
1818 // Refresh() again, *not* because it should only be done if
1819 // !IsSelection(). If the above code is ever optimized to refresh
1820 // only selected area, this needs to be moved out of the "else"
1821 // branch so that it's always executed.
1823 // current cell cursor {dis,re}appears on focus change:
1824 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1825 m_owner
->GetGridCursorCol());
1826 const wxRect cursor
=
1827 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1828 Refresh(true, &cursor
);
1831 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1835 #define internalXToCol(x) XToCol(x, true)
1836 #define internalYToRow(y) YToRow(y, true)
1838 /////////////////////////////////////////////////////////////////////
1840 #if wxUSE_EXTENDED_RTTI
1841 WX_DEFINE_FLAGS( wxGridStyle
)
1843 wxBEGIN_FLAGS( wxGridStyle
)
1844 // new style border flags, we put them first to
1845 // use them for streaming out
1846 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
1847 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
1848 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
1849 wxFLAGS_MEMBER(wxBORDER_RAISED
)
1850 wxFLAGS_MEMBER(wxBORDER_STATIC
)
1851 wxFLAGS_MEMBER(wxBORDER_NONE
)
1853 // old style border flags
1854 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
1855 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
1856 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
1857 wxFLAGS_MEMBER(wxRAISED_BORDER
)
1858 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
1859 wxFLAGS_MEMBER(wxBORDER
)
1861 // standard window styles
1862 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
1863 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
1864 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
1865 wxFLAGS_MEMBER(wxWANTS_CHARS
)
1866 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
1867 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
1868 wxFLAGS_MEMBER(wxVSCROLL
)
1869 wxFLAGS_MEMBER(wxHSCROLL
)
1871 wxEND_FLAGS( wxGridStyle
)
1873 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
1875 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
1876 wxHIDE_PROPERTY( Children
)
1877 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1878 wxEND_PROPERTIES_TABLE()
1880 wxBEGIN_HANDLERS_TABLE(wxGrid
)
1881 wxEND_HANDLERS_TABLE()
1883 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1886 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1889 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1892 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1893 EVT_PAINT( wxGrid::OnPaint
)
1894 EVT_SIZE( wxGrid::OnSize
)
1895 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1896 EVT_KEY_UP( wxGrid::OnKeyUp
)
1897 EVT_CHAR ( wxGrid::OnChar
)
1898 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1901 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1902 const wxPoint
& pos
, const wxSize
& size
,
1903 long style
, const wxString
& name
)
1905 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1906 style
| wxWANTS_CHARS
, name
))
1909 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1910 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1913 SetInitialSize(size
);
1914 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
1923 m_winCapture
->ReleaseMouse();
1925 // Ensure that the editor control is destroyed before the grid is,
1926 // otherwise we crash later when the editor tries to do something with the
1927 // half destroyed grid
1928 HideCellEditControl();
1930 // Must do this or ~wxScrollHelper will pop the wrong event handler
1931 SetTargetWindow(this);
1933 wxSafeDecRef(m_defaultCellAttr
);
1935 #ifdef DEBUG_ATTR_CACHE
1936 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1937 wxPrintf(wxT("wxGrid attribute cache statistics: "
1938 "total: %u, hits: %u (%u%%)\n"),
1939 total
, gs_nAttrCacheHits
,
1940 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1943 // if we own the table, just delete it, otherwise at least don't leave it
1944 // with dangling view pointer
1947 else if ( m_table
&& m_table
->GetView() == this )
1948 m_table
->SetView(NULL
);
1950 delete m_typeRegistry
;
1953 delete m_setFixedRows
;
1954 delete m_setFixedCols
;
1958 // ----- internal init and update functions
1961 // NOTE: If using the default visual attributes works everywhere then this can
1962 // be removed as well as the #else cases below.
1963 #define _USE_VISATTR 0
1965 void wxGrid::Create()
1967 // create the type registry
1968 m_typeRegistry
= new wxGridTypeRegistry
;
1970 m_cellEditCtrlEnabled
= false;
1972 m_defaultCellAttr
= new wxGridCellAttr();
1974 // Set default cell attributes
1975 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1976 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1977 m_defaultCellAttr
->SetFont(GetFont());
1978 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1979 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1980 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1983 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1984 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1986 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1987 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1990 m_defaultCellAttr
->SetTextColour(
1991 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1992 m_defaultCellAttr
->SetBackgroundColour(
1993 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1998 m_currentCellCoords
= wxGridNoCellCoords
;
2000 // subwindow components that make up the wxGrid
2001 m_rowLabelWin
= new wxGridRowLabelWindow(this);
2002 CreateColumnWindow();
2003 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
2004 m_gridWin
= new wxGridWindow( this );
2006 SetTargetWindow( m_gridWin
);
2009 wxColour gfg
= gva
.colFg
;
2010 wxColour gbg
= gva
.colBg
;
2011 wxColour lfg
= lva
.colFg
;
2012 wxColour lbg
= lva
.colBg
;
2014 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2015 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
2016 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2017 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
2020 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
2021 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
2022 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
2023 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
2024 m_colWindow
->SetOwnForegroundColour(lfg
);
2025 m_colWindow
->SetOwnBackgroundColour(lbg
);
2027 m_gridWin
->SetOwnForegroundColour(gfg
);
2028 m_gridWin
->SetOwnBackgroundColour(gbg
);
2030 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2031 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
2033 // now that we have the grid window, use its font to compute the default
2035 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2036 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2037 m_defaultRowHeight
+= 8;
2039 m_defaultRowHeight
+= 4;
2044 void wxGrid::CreateColumnWindow()
2046 if ( m_useNativeHeader
)
2048 m_colWindow
= new wxGridHeaderCtrl(this);
2049 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2051 else // draw labels ourselves
2053 m_colWindow
= new wxGridColLabelWindow(this);
2054 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2058 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2059 wxGridSelectionModes selmode
)
2061 wxCHECK_MSG( !m_created
,
2063 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2065 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2068 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2070 wxCHECK_RET( m_created
,
2071 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2073 m_selection
->SetSelectionMode( selmode
);
2076 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2078 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2079 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2081 return m_selection
->GetSelectionMode();
2085 wxGrid::SetTable(wxGridTableBase
*table
,
2087 wxGrid::wxGridSelectionModes selmode
)
2089 bool checkSelection
= false;
2092 // stop all processing
2097 m_table
->SetView(0);
2109 checkSelection
= true;
2111 // kill row and column size arrays
2112 m_colWidths
.Empty();
2113 m_colRights
.Empty();
2114 m_rowHeights
.Empty();
2115 m_rowBottoms
.Empty();
2120 m_numRows
= table
->GetNumberRows();
2121 m_numCols
= table
->GetNumberCols();
2123 if ( m_useNativeHeader
)
2124 GetGridColHeader()->SetColumnCount(m_numCols
);
2127 m_table
->SetView( this );
2128 m_ownTable
= takeOwnership
;
2129 m_selection
= new wxGridSelection( this, selmode
);
2132 // If the newly set table is smaller than the
2133 // original one current cell and selection regions
2134 // might be invalid,
2135 m_selectedBlockCorner
= wxGridNoCellCoords
;
2136 m_currentCellCoords
=
2137 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2138 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2139 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2140 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2142 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2143 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2146 m_selectedBlockBottomRight
=
2147 wxGridCellCoords(wxMin(m_numRows
,
2148 m_selectedBlockBottomRight
.GetRow()),
2150 m_selectedBlockBottomRight
.GetCol()));
2164 m_cornerLabelWin
= NULL
;
2165 m_rowLabelWin
= NULL
;
2173 m_defaultCellAttr
= NULL
;
2174 m_typeRegistry
= NULL
;
2175 m_winCapture
= NULL
;
2177 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2178 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2181 m_setFixedCols
= NULL
;
2184 m_attrCache
.row
= -1;
2185 m_attrCache
.col
= -1;
2186 m_attrCache
.attr
= NULL
;
2188 m_labelFont
= GetFont();
2189 m_labelFont
.SetWeight( wxBOLD
);
2191 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2192 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2194 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2195 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2196 m_colLabelTextOrientation
= wxHORIZONTAL
;
2198 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2199 m_defaultRowHeight
= 0; // this will be initialized after creation
2201 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2202 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2204 m_gridLineColour
= wxColour( 192,192,192 );
2205 m_gridLinesEnabled
= true;
2206 m_gridLinesClipHorz
=
2207 m_gridLinesClipVert
= true;
2208 m_cellHighlightColour
= *wxBLACK
;
2209 m_cellHighlightPenWidth
= 2;
2210 m_cellHighlightROPenWidth
= 1;
2212 m_canDragColMove
= false;
2214 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2215 m_winCapture
= NULL
;
2216 m_canDragRowSize
= true;
2217 m_canDragColSize
= true;
2218 m_canDragGridSize
= true;
2219 m_canDragCell
= false;
2221 m_dragRowOrCol
= -1;
2222 m_isDragging
= false;
2223 m_startDragPos
= wxDefaultPosition
;
2225 m_sortCol
= wxNOT_FOUND
;
2226 m_sortIsAscending
= true;
2229 m_nativeColumnLabels
= false;
2231 m_waitForSlowClick
= false;
2233 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2234 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2236 m_currentCellCoords
= wxGridNoCellCoords
;
2238 m_selectedBlockTopLeft
=
2239 m_selectedBlockBottomRight
=
2240 m_selectedBlockCorner
= wxGridNoCellCoords
;
2242 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2243 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2245 m_editable
= true; // default for whole grid
2247 m_inOnKeyDown
= false;
2253 m_scrollLineX
= GRID_SCROLL_LINE_X
;
2254 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
2257 // ----------------------------------------------------------------------------
2258 // the idea is to call these functions only when necessary because they create
2259 // quite big arrays which eat memory mostly unnecessary - in particular, if
2260 // default widths/heights are used for all rows/columns, we may not use these
2263 // with some extra code, it should be possible to only store the widths/heights
2264 // different from default ones (resulting in space savings for huge grids) but
2265 // this is not done currently
2266 // ----------------------------------------------------------------------------
2268 void wxGrid::InitRowHeights()
2270 m_rowHeights
.Empty();
2271 m_rowBottoms
.Empty();
2273 m_rowHeights
.Alloc( m_numRows
);
2274 m_rowBottoms
.Alloc( m_numRows
);
2276 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2279 for ( int i
= 0; i
< m_numRows
; i
++ )
2281 rowBottom
+= m_defaultRowHeight
;
2282 m_rowBottoms
.Add( rowBottom
);
2286 void wxGrid::InitColWidths()
2288 m_colWidths
.Empty();
2289 m_colRights
.Empty();
2291 m_colWidths
.Alloc( m_numCols
);
2292 m_colRights
.Alloc( m_numCols
);
2294 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2296 for ( int i
= 0; i
< m_numCols
; i
++ )
2298 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2299 m_colRights
.Add( colRight
);
2303 int wxGrid::GetColWidth(int col
) const
2305 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2308 int wxGrid::GetColLeft(int col
) const
2310 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2311 : m_colRights
[col
] - m_colWidths
[col
];
2314 int wxGrid::GetColRight(int col
) const
2316 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2320 int wxGrid::GetRowHeight(int row
) const
2322 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2325 int wxGrid::GetRowTop(int row
) const
2327 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2328 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2331 int wxGrid::GetRowBottom(int row
) const
2333 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2334 : m_rowBottoms
[row
];
2337 void wxGrid::CalcDimensions()
2339 // compute the size of the scrollable area
2340 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2341 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2346 // take into account editor if shown
2347 if ( IsCellEditControlShown() )
2350 int r
= m_currentCellCoords
.GetRow();
2351 int c
= m_currentCellCoords
.GetCol();
2352 int x
= GetColLeft(c
);
2353 int y
= GetRowTop(r
);
2355 // how big is the editor
2356 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2357 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2358 editor
->GetControl()->GetSize(&w2
, &h2
);
2369 // preserve (more or less) the previous position
2371 GetViewStart( &x
, &y
);
2373 // ensure the position is valid for the new scroll ranges
2375 x
= wxMax( w
- 1, 0 );
2377 y
= wxMax( h
- 1, 0 );
2379 // update the virtual size and refresh the scrollbars to reflect it
2380 m_gridWin
->SetVirtualSize(w
, h
);
2384 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2385 // still must reposition the children
2389 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2391 wxSize
sizeGridWin(size
);
2392 sizeGridWin
.x
-= m_rowLabelWidth
;
2393 sizeGridWin
.y
-= m_colLabelHeight
;
2398 void wxGrid::CalcWindowSizes()
2400 // escape if the window is has not been fully created yet
2402 if ( m_cornerLabelWin
== NULL
)
2406 GetClientSize( &cw
, &ch
);
2408 // the grid may be too small to have enough space for the labels yet, don't
2409 // size the windows to negative sizes in this case
2410 int gw
= cw
- m_rowLabelWidth
;
2411 int gh
= ch
- m_colLabelHeight
;
2417 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2418 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2420 if ( m_colWindow
&& m_colWindow
->IsShown() )
2421 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2423 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2424 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2426 if ( m_gridWin
&& m_gridWin
->IsShown() )
2427 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2430 // this is called when the grid table sends a message
2431 // to indicate that it has been redimensioned
2433 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2436 bool result
= false;
2438 // Clear the attribute cache as the attribute might refer to a different
2439 // cell than stored in the cache after adding/removing rows/columns.
2442 // By the same reasoning, the editor should be dismissed if columns are
2443 // added or removed. And for consistency, it should IMHO always be
2444 // removed, not only if the cell "underneath" it actually changes.
2445 // For now, I intentionally do not save the editor's content as the
2446 // cell it might want to save that stuff to might no longer exist.
2447 HideCellEditControl();
2449 switch ( msg
.GetId() )
2451 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2453 size_t pos
= msg
.GetCommandInt();
2454 int numRows
= msg
.GetCommandInt2();
2456 m_numRows
+= numRows
;
2458 if ( !m_rowHeights
.IsEmpty() )
2460 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2461 m_rowBottoms
.Insert( 0, pos
, numRows
);
2465 bottom
= m_rowBottoms
[pos
- 1];
2467 for ( i
= pos
; i
< m_numRows
; i
++ )
2469 bottom
+= m_rowHeights
[i
];
2470 m_rowBottoms
[i
] = bottom
;
2474 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2476 // if we have just inserted cols into an empty grid the current
2477 // cell will be undefined...
2479 SetCurrentCell( 0, 0 );
2483 m_selection
->UpdateRows( pos
, numRows
);
2484 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2486 attrProvider
->UpdateAttrRows( pos
, numRows
);
2488 if ( !GetBatchCount() )
2491 m_rowLabelWin
->Refresh();
2497 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2499 int numRows
= msg
.GetCommandInt();
2500 int oldNumRows
= m_numRows
;
2501 m_numRows
+= numRows
;
2503 if ( !m_rowHeights
.IsEmpty() )
2505 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2506 m_rowBottoms
.Add( 0, numRows
);
2509 if ( oldNumRows
> 0 )
2510 bottom
= m_rowBottoms
[oldNumRows
- 1];
2512 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2514 bottom
+= m_rowHeights
[i
];
2515 m_rowBottoms
[i
] = bottom
;
2519 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2521 // if we have just inserted cols into an empty grid the current
2522 // cell will be undefined...
2524 SetCurrentCell( 0, 0 );
2527 if ( !GetBatchCount() )
2530 m_rowLabelWin
->Refresh();
2536 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2538 size_t pos
= msg
.GetCommandInt();
2539 int numRows
= msg
.GetCommandInt2();
2540 m_numRows
-= numRows
;
2542 if ( !m_rowHeights
.IsEmpty() )
2544 m_rowHeights
.RemoveAt( pos
, numRows
);
2545 m_rowBottoms
.RemoveAt( pos
, numRows
);
2548 for ( i
= 0; i
< m_numRows
; i
++ )
2550 h
+= m_rowHeights
[i
];
2551 m_rowBottoms
[i
] = h
;
2557 m_currentCellCoords
= wxGridNoCellCoords
;
2561 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2562 m_currentCellCoords
.Set( 0, 0 );
2566 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2567 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2570 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2572 // ifdef'd out following patch from Paul Gammans
2574 // No need to touch column attributes, unless we
2575 // removed _all_ rows, in this case, we remove
2576 // all column attributes.
2577 // I hate to do this here, but the
2578 // needed data is not available inside UpdateAttrRows.
2579 if ( !GetNumberRows() )
2580 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2584 if ( !GetBatchCount() )
2587 m_rowLabelWin
->Refresh();
2593 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2595 size_t pos
= msg
.GetCommandInt();
2596 int numCols
= msg
.GetCommandInt2();
2597 m_numCols
+= numCols
;
2599 if ( m_useNativeHeader
)
2600 GetGridColHeader()->SetColumnCount(m_numCols
);
2602 if ( !m_colAt
.IsEmpty() )
2604 //Shift the column IDs
2606 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2608 if ( m_colAt
[i
] >= (int)pos
)
2609 m_colAt
[i
] += numCols
;
2612 m_colAt
.Insert( pos
, pos
, numCols
);
2614 //Set the new columns' positions
2615 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2621 if ( !m_colWidths
.IsEmpty() )
2623 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2624 m_colRights
.Insert( 0, pos
, numCols
);
2628 right
= m_colRights
[GetColAt( pos
- 1 )];
2631 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2633 i
= GetColAt( colPos
);
2635 right
+= m_colWidths
[i
];
2636 m_colRights
[i
] = right
;
2640 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2642 // if we have just inserted cols into an empty grid the current
2643 // cell will be undefined...
2645 SetCurrentCell( 0, 0 );
2649 m_selection
->UpdateCols( pos
, numCols
);
2650 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2652 attrProvider
->UpdateAttrCols( pos
, numCols
);
2653 if ( !GetBatchCount() )
2656 m_colWindow
->Refresh();
2662 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2664 int numCols
= msg
.GetCommandInt();
2665 int oldNumCols
= m_numCols
;
2666 m_numCols
+= numCols
;
2667 if ( m_useNativeHeader
)
2668 GetGridColHeader()->SetColumnCount(m_numCols
);
2670 if ( !m_colAt
.IsEmpty() )
2672 m_colAt
.Add( 0, numCols
);
2674 //Set the new columns' positions
2676 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2682 if ( !m_colWidths
.IsEmpty() )
2684 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2685 m_colRights
.Add( 0, numCols
);
2688 if ( oldNumCols
> 0 )
2689 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2692 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2694 i
= GetColAt( colPos
);
2696 right
+= m_colWidths
[i
];
2697 m_colRights
[i
] = right
;
2701 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2703 // if we have just inserted cols into an empty grid the current
2704 // cell will be undefined...
2706 SetCurrentCell( 0, 0 );
2708 if ( !GetBatchCount() )
2711 m_colWindow
->Refresh();
2717 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2719 size_t pos
= msg
.GetCommandInt();
2720 int numCols
= msg
.GetCommandInt2();
2721 m_numCols
-= numCols
;
2722 if ( m_useNativeHeader
)
2723 GetGridColHeader()->SetColumnCount(m_numCols
);
2725 if ( !m_colAt
.IsEmpty() )
2727 int colID
= GetColAt( pos
);
2729 m_colAt
.RemoveAt( pos
, numCols
);
2731 //Shift the column IDs
2733 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2735 if ( m_colAt
[colPos
] > colID
)
2736 m_colAt
[colPos
] -= numCols
;
2740 if ( !m_colWidths
.IsEmpty() )
2742 m_colWidths
.RemoveAt( pos
, numCols
);
2743 m_colRights
.RemoveAt( pos
, numCols
);
2747 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2749 i
= GetColAt( colPos
);
2751 w
+= m_colWidths
[i
];
2758 m_currentCellCoords
= wxGridNoCellCoords
;
2762 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2763 m_currentCellCoords
.Set( 0, 0 );
2767 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2768 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2771 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2773 // ifdef'd out following patch from Paul Gammans
2775 // No need to touch row attributes, unless we
2776 // removed _all_ columns, in this case, we remove
2777 // all row attributes.
2778 // I hate to do this here, but the
2779 // needed data is not available inside UpdateAttrCols.
2780 if ( !GetNumberCols() )
2781 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2785 if ( !GetBatchCount() )
2788 m_colWindow
->Refresh();
2795 if (result
&& !GetBatchCount() )
2796 m_gridWin
->Refresh();
2801 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2803 wxRegionIterator
iter( reg
);
2806 wxArrayInt rowlabels
;
2813 // TODO: remove this when we can...
2814 // There is a bug in wxMotif that gives garbage update
2815 // rectangles if you jump-scroll a long way by clicking the
2816 // scrollbar with middle button. This is a work-around
2818 #if defined(__WXMOTIF__)
2820 m_gridWin
->GetClientSize( &cw
, &ch
);
2821 if ( r
.GetTop() > ch
)
2823 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2826 // logical bounds of update region
2829 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2830 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2832 // find the row labels within these bounds
2835 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2837 if ( GetRowBottom(row
) < top
)
2840 if ( GetRowTop(row
) > bottom
)
2843 rowlabels
.Add( row
);
2852 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2854 wxRegionIterator
iter( reg
);
2857 wxArrayInt colLabels
;
2864 // TODO: remove this when we can...
2865 // There is a bug in wxMotif that gives garbage update
2866 // rectangles if you jump-scroll a long way by clicking the
2867 // scrollbar with middle button. This is a work-around
2869 #if defined(__WXMOTIF__)
2871 m_gridWin
->GetClientSize( &cw
, &ch
);
2872 if ( r
.GetLeft() > cw
)
2874 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2877 // logical bounds of update region
2880 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2881 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2883 // find the cells within these bounds
2887 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2889 col
= GetColAt( colPos
);
2891 if ( GetColRight(col
) < left
)
2894 if ( GetColLeft(col
) > right
)
2897 colLabels
.Add( col
);
2906 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2908 wxRegionIterator
iter( reg
);
2911 wxGridCellCoordsArray cellsExposed
;
2913 int left
, top
, right
, bottom
;
2918 // TODO: remove this when we can...
2919 // There is a bug in wxMotif that gives garbage update
2920 // rectangles if you jump-scroll a long way by clicking the
2921 // scrollbar with middle button. This is a work-around
2923 #if defined(__WXMOTIF__)
2925 m_gridWin
->GetClientSize( &cw
, &ch
);
2926 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2927 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2928 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2929 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2932 // logical bounds of update region
2934 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2935 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2937 // find the cells within these bounds
2939 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2941 if ( GetRowBottom(row
) <= top
)
2944 if ( GetRowTop(row
) > bottom
)
2947 // add all dirty cells in this row: notice that the columns which
2948 // are dirty don't depend on the row so we compute them only once
2949 // for the first dirty row and then reuse for all the next ones
2952 // do determine the dirty columns
2953 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2954 cols
.push_back(GetColAt(pos
));
2956 // if there are no dirty columns at all, nothing to do
2961 const size_t count
= cols
.size();
2962 for ( size_t n
= 0; n
< count
; n
++ )
2963 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2969 return cellsExposed
;
2973 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2976 wxPoint
pos( event
.GetPosition() );
2977 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2979 if ( event
.Dragging() )
2983 m_isDragging
= true;
2984 m_rowLabelWin
->CaptureMouse();
2987 if ( event
.LeftIsDown() )
2989 switch ( m_cursorMode
)
2991 case WXGRID_CURSOR_RESIZE_ROW
:
2993 int cw
, ch
, left
, dummy
;
2994 m_gridWin
->GetClientSize( &cw
, &ch
);
2995 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2997 wxClientDC
dc( m_gridWin
);
3000 GetRowTop(m_dragRowOrCol
) +
3001 GetRowMinimalHeight(m_dragRowOrCol
) );
3002 dc
.SetLogicalFunction(wxINVERT
);
3003 if ( m_dragLastPos
>= 0 )
3005 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3007 dc
.DrawLine( left
, y
, left
+cw
, y
);
3012 case WXGRID_CURSOR_SELECT_ROW
:
3014 if ( (row
= YToRow( y
)) >= 0 )
3017 m_selection
->SelectRow(row
, event
);
3022 // default label to suppress warnings about "enumeration value
3023 // 'xxx' not handled in switch
3031 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3036 if (m_rowLabelWin
->HasCapture())
3037 m_rowLabelWin
->ReleaseMouse();
3038 m_isDragging
= false;
3041 // ------------ Entering or leaving the window
3043 if ( event
.Entering() || event
.Leaving() )
3045 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3048 // ------------ Left button pressed
3050 else if ( event
.LeftDown() )
3052 row
= YToEdgeOfRow(y
);
3053 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3055 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3057 else // not a request to start resizing
3061 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3063 if ( !event
.ShiftDown() && !event
.CmdDown() )
3067 if ( event
.ShiftDown() )
3069 m_selection
->SelectBlock
3071 m_currentCellCoords
.GetRow(), 0,
3072 row
, GetNumberCols() - 1,
3078 m_selection
->SelectRow(row
, event
);
3082 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3087 // ------------ Left double click
3089 else if (event
.LeftDClick() )
3091 row
= YToEdgeOfRow(y
);
3092 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3094 // adjust row height depending on label text
3096 // TODO: generate RESIZING event, see #10754
3097 AutoSizeRowLabelSize( row
);
3099 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3101 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3104 else // not on row separator or it's not resizeable
3108 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3110 // no default action at the moment
3115 // ------------ Left button released
3117 else if ( event
.LeftUp() )
3119 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3120 DoEndDragResizeRow(event
);
3122 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3126 // ------------ Right button down
3128 else if ( event
.RightDown() )
3132 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3134 // no default action at the moment
3138 // ------------ Right double click
3140 else if ( event
.RightDClick() )
3144 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3146 // no default action at the moment
3150 // ------------ No buttons down and mouse moving
3152 else if ( event
.Moving() )
3154 m_dragRowOrCol
= YToEdgeOfRow( y
);
3155 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3157 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3159 if ( CanDragRowSize(m_dragRowOrCol
) )
3160 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3163 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3165 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3170 void wxGrid::UpdateColumnSortingIndicator(int col
)
3172 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3174 if ( m_useNativeHeader
)
3175 GetGridColHeader()->UpdateColumn(col
);
3176 else if ( m_nativeColumnLabels
)
3177 m_colWindow
->Refresh();
3178 //else: sorting indicator display not yet implemented in grid version
3181 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3183 if ( col
== m_sortCol
)
3185 // we are already using this column for sorting (or not sorting at all)
3186 // but we might still change the sorting order, check for it
3187 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3189 m_sortIsAscending
= ascending
;
3191 UpdateColumnSortingIndicator(m_sortCol
);
3194 else // we're changing the column used for sorting
3196 const int sortColOld
= m_sortCol
;
3198 // change it before updating the column as we want GetSortingColumn()
3199 // to return the correct new value
3202 if ( sortColOld
!= wxNOT_FOUND
)
3203 UpdateColumnSortingIndicator(sortColOld
);
3205 if ( m_sortCol
!= wxNOT_FOUND
)
3207 m_sortIsAscending
= ascending
;
3208 UpdateColumnSortingIndicator(m_sortCol
);
3213 void wxGrid::DoColHeaderClick(int col
)
3215 // we consider that the grid was resorted if this event is processed and
3217 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3219 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3224 void wxGrid::DoStartResizeCol(int col
)
3226 m_dragRowOrCol
= col
;
3228 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3231 void wxGrid::DoUpdateResizeCol(int x
)
3233 int cw
, ch
, dummy
, top
;
3234 m_gridWin
->GetClientSize( &cw
, &ch
);
3235 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3237 wxClientDC
dc( m_gridWin
);
3240 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3241 dc
.SetLogicalFunction(wxINVERT
);
3242 if ( m_dragLastPos
>= 0 )
3244 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3246 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3250 void wxGrid::DoUpdateResizeColWidth(int w
)
3252 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3255 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3258 wxPoint
pos( event
.GetPosition() );
3259 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3261 int col
= XToCol(x
);
3262 if ( event
.Dragging() )
3266 m_isDragging
= true;
3267 GetColLabelWindow()->CaptureMouse();
3269 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3270 DoStartMoveCol(col
);
3273 if ( event
.LeftIsDown() )
3275 switch ( m_cursorMode
)
3277 case WXGRID_CURSOR_RESIZE_COL
:
3278 DoUpdateResizeCol(x
);
3281 case WXGRID_CURSOR_SELECT_COL
:
3286 m_selection
->SelectCol(col
, event
);
3291 case WXGRID_CURSOR_MOVE_COL
:
3293 int posNew
= XToPos(x
);
3294 int colNew
= GetColAt(posNew
);
3296 // determine the position of the drop marker
3298 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3299 markerX
= GetColRight(colNew
);
3301 markerX
= GetColLeft(colNew
);
3303 if ( markerX
!= m_dragLastPos
)
3305 wxClientDC
dc( GetColLabelWindow() );
3309 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3313 //Clean up the last indicator
3314 if ( m_dragLastPos
>= 0 )
3316 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3318 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3319 dc
.SetPen(wxNullPen
);
3321 if ( XToCol( m_dragLastPos
) != -1 )
3322 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3325 const wxColour
*color
;
3326 //Moving to the same place? Don't draw a marker
3327 if ( colNew
== m_dragRowOrCol
)
3328 color
= wxLIGHT_GREY
;
3333 wxPen
pen( *color
, 2 );
3336 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3338 dc
.SetPen(wxNullPen
);
3340 m_dragLastPos
= markerX
- 1;
3345 // default label to suppress warnings about "enumeration value
3346 // 'xxx' not handled in switch
3354 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3359 if (GetColLabelWindow()->HasCapture())
3360 GetColLabelWindow()->ReleaseMouse();
3361 m_isDragging
= false;
3364 // ------------ Entering or leaving the window
3366 if ( event
.Entering() || event
.Leaving() )
3368 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3371 // ------------ Left button pressed
3373 else if ( event
.LeftDown() )
3375 int col
= XToEdgeOfCol(x
);
3376 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3378 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3380 else // not a request to start resizing
3384 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3386 if ( m_canDragColMove
)
3388 //Show button as pressed
3389 wxClientDC
dc( GetColLabelWindow() );
3390 int colLeft
= GetColLeft( col
);
3391 int colRight
= GetColRight( col
) - 1;
3392 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3393 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3394 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3396 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3400 if ( !event
.ShiftDown() && !event
.CmdDown() )
3404 if ( event
.ShiftDown() )
3406 m_selection
->SelectBlock
3408 0, m_currentCellCoords
.GetCol(),
3409 GetNumberRows() - 1, col
,
3415 m_selection
->SelectCol(col
, event
);
3419 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3425 // ------------ Left double click
3427 if ( event
.LeftDClick() )
3429 const int colEdge
= XToEdgeOfCol(x
);
3430 if ( colEdge
== -1 )
3433 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3435 // no default action at the moment
3440 // adjust column width depending on label text
3442 // TODO: generate RESIZING event, see #10754
3443 AutoSizeColLabelSize( colEdge
);
3445 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3447 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3452 // ------------ Left button released
3454 else if ( event
.LeftUp() )
3456 switch ( m_cursorMode
)
3458 case WXGRID_CURSOR_RESIZE_COL
:
3459 DoEndDragResizeCol(event
);
3462 case WXGRID_CURSOR_MOVE_COL
:
3463 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3465 // the column didn't actually move anywhere
3467 DoColHeaderClick(col
);
3468 m_colWindow
->Refresh(); // "unpress" the column
3472 DoEndMoveCol(XToPos(x
));
3476 case WXGRID_CURSOR_SELECT_COL
:
3477 case WXGRID_CURSOR_SELECT_CELL
:
3478 case WXGRID_CURSOR_RESIZE_ROW
:
3479 case WXGRID_CURSOR_SELECT_ROW
:
3481 DoColHeaderClick(col
);
3485 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3489 // ------------ Right button down
3491 else if ( event
.RightDown() )
3494 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3496 // no default action at the moment
3500 // ------------ Right double click
3502 else if ( event
.RightDClick() )
3505 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3507 // no default action at the moment
3511 // ------------ No buttons down and mouse moving
3513 else if ( event
.Moving() )
3515 m_dragRowOrCol
= XToEdgeOfCol( x
);
3516 if ( m_dragRowOrCol
>= 0 )
3518 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3520 if ( CanDragColSize(m_dragRowOrCol
) )
3521 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3524 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3526 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3531 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3533 if ( event
.LeftDown() )
3535 // indicate corner label by having both row and
3538 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3543 else if ( event
.LeftDClick() )
3545 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3547 else if ( event
.RightDown() )
3549 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3551 // no default action at the moment
3554 else if ( event
.RightDClick() )
3556 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3558 // no default action at the moment
3563 void wxGrid::CancelMouseCapture()
3565 // cancel operation currently in progress, whatever it is
3568 m_isDragging
= false;
3569 m_startDragPos
= wxDefaultPosition
;
3571 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3572 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3573 m_winCapture
= NULL
;
3575 // remove traces of whatever we drew on screen
3580 void wxGrid::ChangeCursorMode(CursorMode mode
,
3585 static const wxChar
*const cursorModes
[] =
3595 wxLogTrace(wxT("grid"),
3596 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3597 win
== m_colWindow
? wxT("colLabelWin")
3598 : win
? wxT("rowLabelWin")
3600 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3601 #endif // wxUSE_LOG_TRACE
3603 if ( mode
== m_cursorMode
&&
3604 win
== m_winCapture
&&
3605 captureMouse
== (m_winCapture
!= NULL
))
3610 // by default use the grid itself
3616 m_winCapture
->ReleaseMouse();
3617 m_winCapture
= NULL
;
3620 m_cursorMode
= mode
;
3622 switch ( m_cursorMode
)
3624 case WXGRID_CURSOR_RESIZE_ROW
:
3625 win
->SetCursor( m_rowResizeCursor
);
3628 case WXGRID_CURSOR_RESIZE_COL
:
3629 win
->SetCursor( m_colResizeCursor
);
3632 case WXGRID_CURSOR_MOVE_COL
:
3633 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3637 win
->SetCursor( *wxSTANDARD_CURSOR
);
3641 // we need to capture mouse when resizing
3642 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3643 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3645 if ( captureMouse
&& resize
)
3647 win
->CaptureMouse();
3652 // ----------------------------------------------------------------------------
3653 // grid mouse event processing
3654 // ----------------------------------------------------------------------------
3657 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3658 const wxGridCellCoords
& coords
,
3661 if ( coords
== wxGridNoCellCoords
)
3662 return; // we're outside any valid cell
3664 // Hide the edit control, so it won't interfere with drag-shrinking.
3665 if ( IsCellEditControlShown() )
3667 HideCellEditControl();
3668 SaveEditControlValue();
3671 switch ( event
.GetModifiers() )
3674 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3675 m_selectedBlockCorner
= coords
;
3676 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3680 if ( CanDragCell() )
3684 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3685 m_selectedBlockCorner
= coords
;
3687 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3692 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3696 // we don't handle the other key modifiers
3701 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3703 wxClientDC
dc(m_gridWin
);
3705 dc
.SetLogicalFunction(wxINVERT
);
3707 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3708 m_gridWin
->GetClientSize());
3710 // erase the previously drawn line, if any
3711 if ( m_dragLastPos
>= 0 )
3712 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3714 // we need the vertical position for rows and horizontal for columns here
3715 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3717 // don't allow resizing beneath the minimal size
3718 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3719 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3720 if ( m_dragLastPos
< posMin
)
3721 m_dragLastPos
= posMin
;
3723 // and draw it at the new position
3724 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3727 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3729 if ( !m_isDragging
)
3731 // Don't start doing anything until the mouse has been dragged far
3733 const wxPoint
& pt
= event
.GetPosition();
3734 if ( m_startDragPos
== wxDefaultPosition
)
3736 m_startDragPos
= pt
;
3740 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3741 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3745 const bool isFirstDrag
= !m_isDragging
;
3746 m_isDragging
= true;
3748 switch ( m_cursorMode
)
3750 case WXGRID_CURSOR_SELECT_CELL
:
3751 DoGridCellDrag(event
, coords
, isFirstDrag
);
3754 case WXGRID_CURSOR_RESIZE_ROW
:
3755 DoGridLineDrag(event
, wxGridRowOperations());
3758 case WXGRID_CURSOR_RESIZE_COL
:
3759 DoGridLineDrag(event
, wxGridColumnOperations());
3768 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
3770 m_winCapture
= m_gridWin
;
3771 m_winCapture
->CaptureMouse();
3776 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3777 const wxGridCellCoords
& coords
,
3780 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3782 // event handled by user code, no need to do anything here
3786 if ( !event
.CmdDown() )
3789 if ( event
.ShiftDown() )
3793 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3794 m_selectedBlockCorner
= coords
;
3797 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3799 DisableCellEditControl();
3800 MakeCellVisible( coords
);
3802 if ( event
.CmdDown() )
3806 m_selection
->ToggleCellSelection(coords
, event
);
3809 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3810 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3811 m_selectedBlockCorner
= coords
;
3815 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3816 coords
!= wxGridNoCellCoords
;
3817 SetCurrentCell( coords
);
3823 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3824 const wxGridCellCoords
& coords
,
3827 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3829 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3831 // we want double click to select a cell and start editing
3832 // (i.e. to behave in same way as sequence of two slow clicks):
3833 m_waitForSlowClick
= true;
3839 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3841 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3845 m_winCapture
->ReleaseMouse();
3846 m_winCapture
= NULL
;
3849 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3852 EnableCellEditControl();
3854 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3855 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3856 editor
->StartingClick();
3860 m_waitForSlowClick
= false;
3862 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3863 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3867 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3868 m_selectedBlockBottomRight
,
3872 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3873 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3875 // Show the edit control, if it has been hidden for
3877 ShowCellEditControl();
3880 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3882 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3883 DoEndDragResizeRow(event
);
3885 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3887 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3888 DoEndDragResizeCol(event
);
3895 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3896 const wxGridCellCoords
& coords
,
3899 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3901 // out of grid cell area
3902 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3906 int dragRow
= YToEdgeOfRow( pos
.y
);
3907 int dragCol
= XToEdgeOfCol( pos
.x
);
3909 // Dragging on the corner of a cell to resize in both
3910 // directions is not implemented yet...
3912 if ( dragRow
>= 0 && dragCol
>= 0 )
3914 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3918 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3920 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3922 m_dragRowOrCol
= dragRow
;
3923 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3926 // When using the native header window we can only resize the columns by
3927 // dragging the dividers in it because we can't make it enter into the
3928 // column resizing mode programmatically
3929 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3930 CanDragGridSize() && CanDragColSize(dragCol
) )
3932 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3934 m_dragRowOrCol
= dragCol
;
3935 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3938 else // Neither on a row or col edge
3940 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3942 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3947 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
3949 if ( event
.Entering() || event
.Leaving() )
3951 // we don't care about these events but we must not reset m_isDragging
3952 // if they happen so return before anything else is done
3957 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
3959 // coordinates of the cell under mouse
3960 wxGridCellCoords coords
= XYToCell(pos
);
3962 int cell_rows
, cell_cols
;
3963 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
3964 if ( (cell_rows
< 0) || (cell_cols
< 0) )
3966 coords
.SetRow(coords
.GetRow() + cell_rows
);
3967 coords
.SetCol(coords
.GetCol() + cell_cols
);
3970 if ( event
.Dragging() )
3972 if ( event
.LeftIsDown() )
3973 DoGridDragEvent(event
, coords
);
3979 m_isDragging
= false;
3980 m_startDragPos
= wxDefaultPosition
;
3982 // deal with various button presses
3983 if ( event
.IsButton() )
3985 if ( coords
!= wxGridNoCellCoords
)
3987 DisableCellEditControl();
3989 if ( event
.LeftDown() )
3990 DoGridCellLeftDown(event
, coords
, pos
);
3991 else if ( event
.LeftDClick() )
3992 DoGridCellLeftDClick(event
, coords
, pos
);
3993 else if ( event
.RightDown() )
3994 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
3995 else if ( event
.RightDClick() )
3996 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
3999 // this one should be called even if we're not over any cell
4000 if ( event
.LeftUp() )
4002 DoGridCellLeftUp(event
, coords
);
4005 else if ( event
.Moving() )
4007 DoGridMouseMoveEvent(event
, coords
, pos
);
4009 else // unknown mouse event?
4015 // this function returns true only if the size really changed
4016 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4018 if ( m_dragLastPos
== -1 )
4021 const wxGridOperations
& doper
= oper
.Dual();
4023 const wxSize size
= m_gridWin
->GetClientSize();
4025 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4027 // erase the last line we drew
4028 wxClientDC
dc(m_gridWin
);
4030 dc
.SetLogicalFunction(wxINVERT
);
4032 const int posLineStart
= oper
.Select(ptOrigin
);
4033 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4035 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4037 // temporarily hide the edit control before resizing
4038 HideCellEditControl();
4039 SaveEditControlValue();
4041 // do resize the line
4042 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4043 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4044 oper
.SetLineSize(this, m_dragRowOrCol
,
4045 wxMax(m_dragLastPos
- lineStart
,
4046 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4048 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4052 // refresh now if we're not frozen
4053 if ( !GetBatchCount() )
4055 // we need to refresh everything beyond the resized line in the header
4058 // get the position from which to refresh in the other direction
4059 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4060 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4062 // we only need the ordinate (for rows) or abscissa (for columns) here,
4063 // and need to cover the entire window in the other direction
4064 oper
.Select(rect
) = 0;
4066 wxRect
rectHeader(rect
.GetPosition(),
4069 oper
.GetHeaderWindowSize(this),
4070 doper
.Select(size
) - doper
.Select(rect
)
4073 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4076 // also refresh the grid window: extend the rectangle
4079 oper
.SelectSize(rect
) = oper
.Select(size
);
4081 int subtractLines
= 0;
4082 const int lineStart
= oper
.PosToLine(this, posLineStart
);
4083 if ( lineStart
>= 0 )
4085 // ensure that if we have a multi-cell block we redraw all of
4086 // it by increasing the refresh area to cover it entirely if a
4087 // part of it is affected
4088 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
4089 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
4091 int cellLines
= oper
.Select(
4092 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4093 if ( cellLines
< subtractLines
)
4094 subtractLines
= cellLines
;
4099 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4100 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4102 doper
.Select(rect
) = startPos
;
4103 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4105 m_gridWin
->Refresh(false, &rect
);
4109 // show the edit control back again
4110 ShowCellEditControl();
4115 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4117 // TODO: generate RESIZING event, see #10754
4119 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4120 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4123 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4125 // TODO: generate RESIZING event, see #10754
4127 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4128 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4131 void wxGrid::DoStartMoveCol(int col
)
4133 m_dragRowOrCol
= col
;
4136 void wxGrid::DoEndMoveCol(int pos
)
4138 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4140 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4141 SetColPos(m_dragRowOrCol
, pos
);
4142 //else: vetoed by user
4144 m_dragRowOrCol
= -1;
4147 void wxGrid::RefreshAfterColPosChange()
4149 // recalculate the column rights as the column positions have changed,
4150 // unless we calculate them dynamically because all columns widths are the
4151 // same and it's easy to do
4152 if ( !m_colWidths
.empty() )
4155 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4157 int colID
= GetColAt( colPos
);
4159 colRight
+= m_colWidths
[colID
];
4160 m_colRights
[colID
] = colRight
;
4164 // and make the changes visible
4165 if ( m_useNativeHeader
)
4167 if ( m_colAt
.empty() )
4168 GetGridColHeader()->ResetColumnsOrder();
4170 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4174 m_colWindow
->Refresh();
4176 m_gridWin
->Refresh();
4179 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4183 RefreshAfterColPosChange();
4186 void wxGrid::SetColPos(int idx
, int pos
)
4188 // we're going to need m_colAt now, initialize it if needed
4189 if ( m_colAt
.empty() )
4191 m_colAt
.reserve(m_numCols
);
4192 for ( int i
= 0; i
< m_numCols
; i
++ )
4193 m_colAt
.push_back(i
);
4196 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4198 RefreshAfterColPosChange();
4201 void wxGrid::ResetColPos()
4205 RefreshAfterColPosChange();
4208 void wxGrid::EnableDragColMove( bool enable
)
4210 if ( m_canDragColMove
== enable
)
4213 if ( m_useNativeHeader
)
4215 // update all columns to make them [not] reorderable
4216 GetGridColHeader()->SetColumnCount(m_numCols
);
4219 m_canDragColMove
= enable
;
4221 // we use to call ResetColPos() from here if !enable but this doesn't seem
4222 // right as it would mean there would be no way to "freeze" the current
4223 // columns order by disabling moving them after putting them in the desired
4224 // order, whereas now you can always call ResetColPos() manually if needed
4229 // ------ interaction with data model
4231 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4233 switch ( msg
.GetId() )
4235 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4236 return GetModelValues();
4238 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4239 return SetModelValues();
4241 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4242 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4243 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4244 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4245 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4246 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4247 return Redimension( msg
);
4254 // The behaviour of this function depends on the grid table class
4255 // Clear() function. For the default wxGridStringTable class the
4256 // behaviour is to replace all cell contents with wxEmptyString but
4257 // not to change the number of rows or cols.
4259 void wxGrid::ClearGrid()
4263 if (IsCellEditControlEnabled())
4264 DisableCellEditControl();
4267 if (!GetBatchCount())
4268 m_gridWin
->Refresh();
4273 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4274 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4276 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4281 if ( IsCellEditControlEnabled() )
4282 DisableCellEditControl();
4284 return (m_table
->*funcModify
)(pos
, num
);
4286 // the table will have sent the results of the insert row
4287 // operation to this view object as a grid table message
4291 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4292 int num
, bool WXUNUSED(updateLabels
))
4294 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4299 return (m_table
->*funcAppend
)(num
);
4302 // ----------------------------------------------------------------------------
4303 // event generation helpers
4304 // ----------------------------------------------------------------------------
4307 wxGrid::SendGridSizeEvent(wxEventType type
,
4309 const wxMouseEvent
& mouseEv
)
4311 int rowOrCol
= row
== -1 ? col
: row
;
4313 wxGridSizeEvent
gridEvt( GetId(),
4317 mouseEv
.GetX() + GetRowLabelSize(),
4318 mouseEv
.GetY() + GetColLabelSize(),
4321 GetEventHandler()->ProcessEvent(gridEvt
);
4324 // Generate a grid event based on a mouse event and return:
4325 // -1 if the event was vetoed
4326 // +1 if the event was processed (but not vetoed)
4327 // 0 if the event wasn't handled
4329 wxGrid::SendEvent(const wxEventType type
,
4331 const wxMouseEvent
& mouseEv
)
4333 bool claimed
, vetoed
;
4335 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4337 // Right now, it should _never_ end up here!
4338 wxGridRangeSelectEvent
gridEvt( GetId(),
4341 m_selectedBlockTopLeft
,
4342 m_selectedBlockBottomRight
,
4346 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4347 vetoed
= !gridEvt
.IsAllowed();
4349 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4350 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4351 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4352 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4354 wxPoint pos
= mouseEv
.GetPosition();
4356 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4357 pos
.y
+= GetColLabelSize();
4358 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4359 pos
.x
+= GetRowLabelSize();
4361 wxGridEvent
gridEvt( GetId(),
4369 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4370 vetoed
= !gridEvt
.IsAllowed();
4374 wxGridEvent
gridEvt( GetId(),
4378 mouseEv
.GetX() + GetRowLabelSize(),
4379 mouseEv
.GetY() + GetColLabelSize(),
4382 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4383 vetoed
= !gridEvt
.IsAllowed();
4386 // A Veto'd event may not be `claimed' so test this first
4390 return claimed
? 1 : 0;
4393 // Generate a grid event of specified type, return value same as above
4396 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4398 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4399 gridEvt
.SetString(s
);
4401 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4403 // A Veto'd event may not be `claimed' so test this first
4404 if ( !gridEvt
.IsAllowed() )
4407 return claimed
? 1 : 0;
4410 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4412 // needed to prevent zillions of paint events on MSW
4416 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4418 // Don't do anything if between Begin/EndBatch...
4419 // EndBatch() will do all this on the last nested one anyway.
4420 if ( m_created
&& !GetBatchCount() )
4422 // Refresh to get correct scrolled position:
4423 wxScrolledWindow::Refresh(eraseb
, rect
);
4427 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4428 int width_label
, width_cell
, height_label
, height_cell
;
4431 // Copy rectangle can get scroll offsets..
4432 rect_x
= rect
->GetX();
4433 rect_y
= rect
->GetY();
4434 rectWidth
= rect
->GetWidth();
4435 rectHeight
= rect
->GetHeight();
4437 width_label
= m_rowLabelWidth
- rect_x
;
4438 if (width_label
> rectWidth
)
4439 width_label
= rectWidth
;
4441 height_label
= m_colLabelHeight
- rect_y
;
4442 if (height_label
> rectHeight
)
4443 height_label
= rectHeight
;
4445 if (rect_x
> m_rowLabelWidth
)
4447 x
= rect_x
- m_rowLabelWidth
;
4448 width_cell
= rectWidth
;
4453 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4456 if (rect_y
> m_colLabelHeight
)
4458 y
= rect_y
- m_colLabelHeight
;
4459 height_cell
= rectHeight
;
4464 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4467 // Paint corner label part intersecting rect.
4468 if ( width_label
> 0 && height_label
> 0 )
4470 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4471 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4474 // Paint col labels part intersecting rect.
4475 if ( width_cell
> 0 && height_label
> 0 )
4477 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4478 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4481 // Paint row labels part intersecting rect.
4482 if ( width_label
> 0 && height_cell
> 0 )
4484 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4485 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4488 // Paint cell area part intersecting rect.
4489 if ( width_cell
> 0 && height_cell
> 0 )
4491 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4492 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4497 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4498 m_colWindow
->Refresh(eraseb
, NULL
);
4499 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4500 m_gridWin
->Refresh(eraseb
, NULL
);
4505 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4507 if (m_targetWindow
!= this) // check whether initialisation has been done
4509 // reposition our children windows
4514 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4516 if ( m_inOnKeyDown
)
4518 // shouldn't be here - we are going round in circles...
4520 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4523 m_inOnKeyDown
= true;
4525 // propagate the event up and see if it gets processed
4526 wxWindow
*parent
= GetParent();
4527 wxKeyEvent
keyEvt( event
);
4528 keyEvt
.SetEventObject( parent
);
4530 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4532 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4534 if (event
.GetKeyCode() == WXK_RIGHT
)
4535 event
.m_keyCode
= WXK_LEFT
;
4536 else if (event
.GetKeyCode() == WXK_LEFT
)
4537 event
.m_keyCode
= WXK_RIGHT
;
4540 // try local handlers
4541 switch ( event
.GetKeyCode() )
4544 if ( event
.ControlDown() )
4545 MoveCursorUpBlock( event
.ShiftDown() );
4547 MoveCursorUp( event
.ShiftDown() );
4551 if ( event
.ControlDown() )
4552 MoveCursorDownBlock( event
.ShiftDown() );
4554 MoveCursorDown( event
.ShiftDown() );
4558 if ( event
.ControlDown() )
4559 MoveCursorLeftBlock( event
.ShiftDown() );
4561 MoveCursorLeft( event
.ShiftDown() );
4565 if ( event
.ControlDown() )
4566 MoveCursorRightBlock( event
.ShiftDown() );
4568 MoveCursorRight( event
.ShiftDown() );
4572 case WXK_NUMPAD_ENTER
:
4573 if ( event
.ControlDown() )
4575 event
.Skip(); // to let the edit control have the return
4579 if ( GetGridCursorRow() < GetNumberRows()-1 )
4581 MoveCursorDown( event
.ShiftDown() );
4585 // at the bottom of a column
4586 DisableCellEditControl();
4596 if (event
.ShiftDown())
4598 if ( GetGridCursorCol() > 0 )
4600 MoveCursorLeft( false );
4605 DisableCellEditControl();
4610 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4612 MoveCursorRight( false );
4617 DisableCellEditControl();
4623 if ( event
.ControlDown() )
4634 if ( event
.ControlDown() )
4636 GoToCell(m_numRows
- 1, m_numCols
- 1);
4653 // Ctrl-Space selects the current column, Shift-Space -- the
4654 // current row and Ctrl-Shift-Space -- everything
4655 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4658 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4662 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4665 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4666 m_selection
->SelectBlock(0, 0,
4667 m_numRows
- 1, m_numCols
- 1);
4671 if ( !IsEditable() )
4673 MoveCursorRight(false);
4676 //else: fall through
4689 m_inOnKeyDown
= false;
4692 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4694 // try local handlers
4696 if ( event
.GetKeyCode() == WXK_SHIFT
)
4698 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4699 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4703 m_selection
->SelectBlock(
4704 m_selectedBlockTopLeft
,
4705 m_selectedBlockBottomRight
,
4710 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4711 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4712 m_selectedBlockCorner
= wxGridNoCellCoords
;
4716 void wxGrid::OnChar( wxKeyEvent
& event
)
4718 // is it possible to edit the current cell at all?
4719 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4721 // yes, now check whether the cells editor accepts the key
4722 int row
= m_currentCellCoords
.GetRow();
4723 int col
= m_currentCellCoords
.GetCol();
4724 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4725 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4727 // <F2> is special and will always start editing, for
4728 // other keys - ask the editor itself
4729 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4730 || editor
->IsAcceptedKey(event
) )
4732 // ensure cell is visble
4733 MakeCellVisible(row
, col
);
4734 EnableCellEditControl();
4736 // a problem can arise if the cell is not completely
4737 // visible (even after calling MakeCellVisible the
4738 // control is not created and calling StartingKey will
4740 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4741 editor
->StartingKey(event
);
4757 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4761 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4763 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4765 // the event has been vetoed - do nothing
4769 #if !defined(__WXMAC__)
4770 wxClientDC
dc( m_gridWin
);
4774 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4776 DisableCellEditControl();
4778 if ( IsVisible( m_currentCellCoords
, false ) )
4781 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4782 if ( !m_gridLinesEnabled
)
4790 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4792 // Otherwise refresh redraws the highlight!
4793 m_currentCellCoords
= coords
;
4795 #if defined(__WXMAC__)
4796 m_gridWin
->Refresh(true /*, & r */);
4798 DrawGridCellArea( dc
, cells
);
4799 DrawAllGridLines( dc
, r
);
4804 m_currentCellCoords
= coords
;
4806 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4807 #if !defined(__WXMAC__)
4808 DrawCellHighlight( dc
, attr
);
4816 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4817 int bottomRow
, int rightCol
)
4821 switch ( m_selection
->GetSelectionMode() )
4824 wxFAIL_MSG( "unknown selection mode" );
4827 case wxGridSelectCells
:
4828 // arbitrary blocks selection allowed so just use the cell
4829 // coordinates as is
4832 case wxGridSelectRows
:
4833 // only full rows selection allowd, ensure that we do select
4836 rightCol
= GetNumberCols() - 1;
4839 case wxGridSelectColumns
:
4840 // same as above but for columns
4842 bottomRow
= GetNumberRows() - 1;
4845 case wxGridSelectRowsOrColumns
:
4846 // in this mode we can select only full rows or full columns so
4847 // it doesn't make sense to select blocks at all (and we can't
4848 // extend the block because there is no preferred direction, we
4849 // could only extend it to cover the entire grid but this is
4855 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4856 MakeCellVisible(m_selectedBlockCorner
);
4858 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4859 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4861 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4862 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4864 // First the case that we selected a completely new area
4865 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4866 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4869 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4870 wxGridCellCoords ( bottomRow
, rightCol
) );
4871 m_gridWin
->Refresh( false, &rect
);
4874 // Now handle changing an existing selection area.
4875 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4876 m_selectedBlockBottomRight
!= updateBottomRight
)
4878 // Compute two optimal update rectangles:
4879 // Either one rectangle is a real subset of the
4880 // other, or they are (almost) disjoint!
4882 bool need_refresh
[4];
4886 need_refresh
[3] = false;
4889 // Store intermediate values
4890 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4891 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4892 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4893 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4895 // Determine the outer/inner coordinates.
4896 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4897 EnsureFirstLessThanSecond(oldTop
, topRow
);
4898 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4899 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4901 // Now, either the stuff marked old is the outer
4902 // rectangle or we don't have a situation where one
4903 // is contained in the other.
4905 if ( oldLeft
< leftCol
)
4907 // Refresh the newly selected or deselected
4908 // area to the left of the old or new selection.
4909 need_refresh
[0] = true;
4910 rect
[0] = BlockToDeviceRect(
4911 wxGridCellCoords( oldTop
, oldLeft
),
4912 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4915 if ( oldTop
< topRow
)
4917 // Refresh the newly selected or deselected
4918 // area above the old or new selection.
4919 need_refresh
[1] = true;
4920 rect
[1] = BlockToDeviceRect(
4921 wxGridCellCoords( oldTop
, leftCol
),
4922 wxGridCellCoords( topRow
- 1, rightCol
) );
4925 if ( oldRight
> rightCol
)
4927 // Refresh the newly selected or deselected
4928 // area to the right of the old or new selection.
4929 need_refresh
[2] = true;
4930 rect
[2] = BlockToDeviceRect(
4931 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4932 wxGridCellCoords( oldBottom
, oldRight
) );
4935 if ( oldBottom
> bottomRow
)
4937 // Refresh the newly selected or deselected
4938 // area below the old or new selection.
4939 need_refresh
[3] = true;
4940 rect
[3] = BlockToDeviceRect(
4941 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4942 wxGridCellCoords( oldBottom
, rightCol
) );
4945 // various Refresh() calls
4946 for (i
= 0; i
< 4; i
++ )
4947 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4948 m_gridWin
->Refresh( false, &(rect
[i
]) );
4952 m_selectedBlockTopLeft
= updateTopLeft
;
4953 m_selectedBlockBottomRight
= updateBottomRight
;
4957 // ------ functions to get/send data (see also public functions)
4960 bool wxGrid::GetModelValues()
4962 // Hide the editor, so it won't hide a changed value.
4963 HideCellEditControl();
4967 // all we need to do is repaint the grid
4969 m_gridWin
->Refresh();
4976 bool wxGrid::SetModelValues()
4980 // Disable the editor, so it won't hide a changed value.
4981 // Do we also want to save the current value of the editor first?
4983 DisableCellEditControl();
4987 for ( row
= 0; row
< m_numRows
; row
++ )
4989 for ( col
= 0; col
< m_numCols
; col
++ )
4991 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5001 // Note - this function only draws cells that are in the list of
5002 // exposed cells (usually set from the update region by
5003 // CalcExposedCells)
5005 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5007 if ( !m_numRows
|| !m_numCols
)
5010 int i
, numCells
= cells
.GetCount();
5011 int row
, col
, cell_rows
, cell_cols
;
5012 wxGridCellCoordsArray redrawCells
;
5014 for ( i
= numCells
- 1; i
>= 0; i
-- )
5016 row
= cells
[i
].GetRow();
5017 col
= cells
[i
].GetCol();
5018 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5020 // If this cell is part of a multicell block, find owner for repaint
5021 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5023 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5024 bool marked
= false;
5025 for ( int j
= 0; j
< numCells
; j
++ )
5027 if ( cell
== cells
[j
] )
5036 int count
= redrawCells
.GetCount();
5037 for (int j
= 0; j
< count
; j
++)
5039 if ( cell
== redrawCells
[j
] )
5047 redrawCells
.Add( cell
);
5050 // don't bother drawing this cell
5054 // If this cell is empty, find cell to left that might want to overflow
5055 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5057 for ( int l
= 0; l
< cell_rows
; l
++ )
5059 // find a cell in this row to leave already marked for repaint
5061 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5062 if ((redrawCells
[k
].GetCol() < left
) &&
5063 (redrawCells
[k
].GetRow() == row
))
5065 left
= redrawCells
[k
].GetCol();
5069 left
= 0; // oh well
5071 for (int j
= col
- 1; j
>= left
; j
--)
5073 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5075 if (GetCellOverflow(row
+ l
, j
))
5077 wxGridCellCoords
cell(row
+ l
, j
);
5078 bool marked
= false;
5080 for (int k
= 0; k
< numCells
; k
++)
5082 if ( cell
== cells
[k
] )
5091 int count
= redrawCells
.GetCount();
5092 for (int k
= 0; k
< count
; k
++)
5094 if ( cell
== redrawCells
[k
] )
5101 redrawCells
.Add( cell
);
5110 DrawCell( dc
, cells
[i
] );
5113 numCells
= redrawCells
.GetCount();
5115 for ( i
= numCells
- 1; i
>= 0; i
-- )
5117 DrawCell( dc
, redrawCells
[i
] );
5121 void wxGrid::DrawGridSpace( wxDC
& dc
)
5124 m_gridWin
->GetClientSize( &cw
, &ch
);
5127 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5129 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5130 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5132 if ( right
> rightCol
|| bottom
> bottomRow
)
5135 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5137 dc
.SetBrush(GetDefaultCellBackgroundColour());
5138 dc
.SetPen( *wxTRANSPARENT_PEN
);
5140 if ( right
> rightCol
)
5142 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5145 if ( bottom
> bottomRow
)
5147 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5152 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5154 int row
= coords
.GetRow();
5155 int col
= coords
.GetCol();
5157 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5160 // we draw the cell border ourselves
5161 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5163 bool isCurrent
= coords
== m_currentCellCoords
;
5165 wxRect rect
= CellToRect( row
, col
);
5167 // if the editor is shown, we should use it and not the renderer
5168 // Note: However, only if it is really _shown_, i.e. not hidden!
5169 if ( isCurrent
&& IsCellEditControlShown() )
5171 // NB: this "#if..." is temporary and fixes a problem where the
5172 // edit control is erased by this code after being rendered.
5173 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5174 // implicitly, causing this out-of order render.
5175 #if !defined(__WXMAC__)
5176 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5177 editor
->PaintBackground(rect
, attr
);
5183 // but all the rest is drawn by the cell renderer and hence may be customized
5184 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5185 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5192 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5194 // don't show highlight when the grid doesn't have focus
5198 int row
= m_currentCellCoords
.GetRow();
5199 int col
= m_currentCellCoords
.GetCol();
5201 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5204 wxRect rect
= CellToRect(row
, col
);
5206 // hmmm... what could we do here to show that the cell is disabled?
5207 // for now, I just draw a thinner border than for the other ones, but
5208 // it doesn't look really good
5210 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5214 // The center of the drawn line is where the position/width/height of
5215 // the rectangle is actually at (on wxMSW at least), so the
5216 // size of the rectangle is reduced to compensate for the thickness of
5217 // the line. If this is too strange on non-wxMSW platforms then
5218 // please #ifdef this appropriately.
5219 rect
.x
+= penWidth
/ 2;
5220 rect
.y
+= penWidth
/ 2;
5221 rect
.width
-= penWidth
- 1;
5222 rect
.height
-= penWidth
- 1;
5224 // Now draw the rectangle
5225 // use the cellHighlightColour if the cell is inside a selection, this
5226 // will ensure the cell is always visible.
5227 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5228 : m_cellHighlightColour
,
5230 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5231 dc
.DrawRectangle(rect
);
5235 wxPen
wxGrid::GetDefaultGridLinePen()
5237 return wxPen(GetGridLineColour());
5240 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5242 return GetDefaultGridLinePen();
5245 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5247 return GetDefaultGridLinePen();
5250 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5252 int row
= coords
.GetRow();
5253 int col
= coords
.GetCol();
5254 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5258 wxRect rect
= CellToRect( row
, col
);
5260 // right hand border
5261 dc
.SetPen( GetColGridLinePen(col
) );
5262 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5263 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5266 dc
.SetPen( GetRowGridLinePen(row
) );
5267 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5268 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5271 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5273 // This if block was previously in wxGrid::OnPaint but that doesn't
5274 // seem to get called under wxGTK - MB
5276 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5277 m_numRows
&& m_numCols
)
5279 m_currentCellCoords
.Set(0, 0);
5282 if ( IsCellEditControlShown() )
5284 // don't show highlight when the edit control is shown
5288 // if the active cell was repainted, repaint its highlight too because it
5289 // might have been damaged by the grid lines
5290 size_t count
= cells
.GetCount();
5291 for ( size_t n
= 0; n
< count
; n
++ )
5293 wxGridCellCoords cell
= cells
[n
];
5295 // If we are using attributes, then we may have just exposed another
5296 // cell in a partially-visible merged cluster of cells. If the "anchor"
5297 // (upper left) cell of this merged cluster is the cell indicated by
5298 // m_currentCellCoords, then we need to refresh the cell highlight even
5299 // though the "anchor" itself is not part of our update segment.
5300 if ( CanHaveAttributes() )
5304 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5307 cell
.SetRow(cell
.GetRow() + rows
);
5310 cell
.SetCol(cell
.GetCol() + cols
);
5313 if ( cell
== m_currentCellCoords
)
5315 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5316 DrawCellHighlight(dc
, attr
);
5324 // This is used to redraw all grid lines e.g. when the grid line colour
5327 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5329 if ( !m_gridLinesEnabled
)
5332 int top
, bottom
, left
, right
;
5335 m_gridWin
->GetClientSize(&cw
, &ch
);
5336 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5337 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5339 // avoid drawing grid lines past the last row and col
5340 if ( m_gridLinesClipHorz
)
5345 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5346 if ( right
> lastColRight
)
5347 right
= lastColRight
;
5350 if ( m_gridLinesClipVert
)
5355 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5356 if ( bottom
> lastRowBottom
)
5357 bottom
= lastRowBottom
;
5360 // no gridlines inside multicells, clip them out
5361 int leftCol
= GetColPos( internalXToCol(left
) );
5362 int topRow
= internalYToRow(top
);
5363 int rightCol
= GetColPos( internalXToCol(right
) );
5364 int bottomRow
= internalYToRow(bottom
);
5366 wxRegion
clippedcells(0, 0, cw
, ch
);
5368 int cell_rows
, cell_cols
;
5371 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5373 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5375 int i
= GetColAt( colPos
);
5377 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5378 if ((cell_rows
> 1) || (cell_cols
> 1))
5380 rect
= CellToRect(j
,i
);
5381 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5382 clippedcells
.Subtract(rect
);
5384 else if ((cell_rows
< 0) || (cell_cols
< 0))
5386 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5387 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5388 clippedcells
.Subtract(rect
);
5393 dc
.SetDeviceClippingRegion( clippedcells
);
5396 // horizontal grid lines
5397 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5399 int bot
= GetRowBottom(i
) - 1;
5406 dc
.SetPen( GetRowGridLinePen(i
) );
5407 dc
.DrawLine( left
, bot
, right
, bot
);
5411 // vertical grid lines
5412 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5414 int i
= GetColAt( colPos
);
5416 int colRight
= GetColRight(i
);
5418 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5422 if ( colRight
> right
)
5425 if ( colRight
>= left
)
5427 dc
.SetPen( GetColGridLinePen(i
) );
5428 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5432 dc
.DestroyClippingRegion();
5435 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5440 const size_t numLabels
= rows
.GetCount();
5441 for ( size_t i
= 0; i
< numLabels
; i
++ )
5443 DrawRowLabel( dc
, rows
[i
] );
5447 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5449 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5452 wxGridCellAttrProvider
* const
5453 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5455 // notice that an explicit static_cast is needed to avoid a compilation
5456 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5457 // wxGridRowHeaderRenderer class without it
5458 const wxGridRowHeaderRenderer
&
5459 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5460 : static_cast<const wxGridRowHeaderRenderer
&>
5461 (gs_defaultHeaderRenderers
.rowRenderer
);
5463 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5464 rend
.DrawBorder(*this, dc
, rect
);
5467 GetRowLabelAlignment(&hAlign
, &vAlign
);
5469 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5470 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5473 void wxGrid::UseNativeColHeader(bool native
)
5475 if ( native
== m_useNativeHeader
)
5479 m_useNativeHeader
= native
;
5481 CreateColumnWindow();
5483 if ( m_useNativeHeader
)
5484 GetGridColHeader()->SetColumnCount(m_numCols
);
5488 void wxGrid::SetUseNativeColLabels( bool native
)
5490 wxASSERT_MSG( !m_useNativeHeader
,
5491 "doesn't make sense when using native header" );
5493 m_nativeColumnLabels
= native
;
5496 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5497 SetColLabelSize( height
);
5500 GetColLabelWindow()->Refresh();
5501 m_cornerLabelWin
->Refresh();
5504 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5509 const size_t numLabels
= cols
.GetCount();
5510 for ( size_t i
= 0; i
< numLabels
; i
++ )
5512 DrawColLabel( dc
, cols
[i
] );
5516 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5518 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5520 if ( m_nativeColumnLabels
)
5524 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5531 wxGridCellAttrProvider
* const
5532 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5533 const wxGridCornerHeaderRenderer
&
5534 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5535 : static_cast<wxGridCornerHeaderRenderer
&>
5536 (gs_defaultHeaderRenderers
.cornerRenderer
);
5538 rend
.DrawBorder(*this, dc
, rect
);
5542 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5544 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5547 int colLeft
= GetColLeft(col
);
5549 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5550 wxGridCellAttrProvider
* const
5551 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5552 const wxGridColumnHeaderRenderer
&
5553 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
5554 : static_cast<wxGridColumnHeaderRenderer
&>
5555 (gs_defaultHeaderRenderers
.colRenderer
);
5557 if ( m_nativeColumnLabels
)
5559 wxRendererNative::Get().DrawHeaderButton
5561 GetColLabelWindow(),
5566 ? IsSortOrderAscending()
5567 ? wxHDR_SORT_ICON_UP
5568 : wxHDR_SORT_ICON_DOWN
5569 : wxHDR_SORT_ICON_NONE
5575 rend
.DrawBorder(*this, dc
, rect
);
5579 GetColLabelAlignment(&hAlign
, &vAlign
);
5580 const int orient
= GetColLabelTextOrientation();
5582 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5585 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5586 // we just have to add textOrientation support
5587 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5588 const wxString
& value
,
5592 int textOrientation
) const
5594 wxArrayString lines
;
5596 StringToLines( value
, lines
);
5598 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5601 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5602 const wxArrayString
& lines
,
5606 int textOrientation
) const
5608 if ( lines
.empty() )
5611 wxDCClipper
clip(dc
, rect
);
5616 if ( textOrientation
== wxHORIZONTAL
)
5617 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5619 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5623 switch ( vertAlign
)
5625 case wxALIGN_BOTTOM
:
5626 if ( textOrientation
== wxHORIZONTAL
)
5627 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5629 x
= rect
.x
+ rect
.width
- textWidth
;
5632 case wxALIGN_CENTRE
:
5633 if ( textOrientation
== wxHORIZONTAL
)
5634 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5636 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5641 if ( textOrientation
== wxHORIZONTAL
)
5648 // Align each line of a multi-line label
5649 size_t nLines
= lines
.GetCount();
5650 for ( size_t l
= 0; l
< nLines
; l
++ )
5652 const wxString
& line
= lines
[l
];
5656 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5660 wxCoord lineWidth
= 0,
5662 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5664 switch ( horizAlign
)
5667 if ( textOrientation
== wxHORIZONTAL
)
5668 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5670 y
= rect
.y
+ lineWidth
+ 1;
5673 case wxALIGN_CENTRE
:
5674 if ( textOrientation
== wxHORIZONTAL
)
5675 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5677 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5682 if ( textOrientation
== wxHORIZONTAL
)
5685 y
= rect
.y
+ rect
.height
- 1;
5689 if ( textOrientation
== wxHORIZONTAL
)
5691 dc
.DrawText( line
, x
, y
);
5696 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5702 // Split multi-line text up into an array of strings.
5703 // Any existing contents of the string array are preserved.
5705 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5706 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5710 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5711 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5713 while ( startPos
< (int)tVal
.length() )
5715 pos
= tVal
.Mid(startPos
).Find( eol
);
5720 else if ( pos
== 0 )
5722 lines
.Add( wxEmptyString
);
5726 lines
.Add( tVal
.Mid(startPos
, pos
) );
5729 startPos
+= pos
+ 1;
5732 if ( startPos
< (int)tVal
.length() )
5734 lines
.Add( tVal
.Mid( startPos
) );
5738 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5739 const wxArrayString
& lines
,
5740 long *width
, long *height
) const
5744 wxCoord lineW
= 0, lineH
= 0;
5747 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5749 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5750 w
= wxMax( w
, lineW
);
5759 // ------ Batch processing.
5761 void wxGrid::EndBatch()
5763 if ( m_batchCount
> 0 )
5766 if ( !m_batchCount
)
5769 m_rowLabelWin
->Refresh();
5770 m_colWindow
->Refresh();
5771 m_cornerLabelWin
->Refresh();
5772 m_gridWin
->Refresh();
5777 // Use this, rather than wxWindow::Refresh(), to force an immediate
5778 // repainting of the grid. Has no effect if you are already inside a
5779 // BeginBatch / EndBatch block.
5781 void wxGrid::ForceRefresh()
5787 bool wxGrid::Enable(bool enable
)
5789 if ( !wxScrolledWindow::Enable(enable
) )
5792 // redraw in the new state
5793 m_gridWin
->Refresh();
5799 // ------ Edit control functions
5802 void wxGrid::EnableEditing( bool edit
)
5804 if ( edit
!= m_editable
)
5807 EnableCellEditControl(edit
);
5812 void wxGrid::EnableCellEditControl( bool enable
)
5817 if ( enable
!= m_cellEditCtrlEnabled
)
5821 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5824 // this should be checked by the caller!
5825 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5827 // do it before ShowCellEditControl()
5828 m_cellEditCtrlEnabled
= enable
;
5830 ShowCellEditControl();
5834 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5836 HideCellEditControl();
5837 SaveEditControlValue();
5839 // do it after HideCellEditControl()
5840 m_cellEditCtrlEnabled
= enable
;
5845 bool wxGrid::IsCurrentCellReadOnly() const
5848 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5849 bool readonly
= attr
->IsReadOnly();
5855 bool wxGrid::CanEnableCellControl() const
5857 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5858 !IsCurrentCellReadOnly();
5861 bool wxGrid::IsCellEditControlEnabled() const
5863 // the cell edit control might be disable for all cells or just for the
5864 // current one if it's read only
5865 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5868 bool wxGrid::IsCellEditControlShown() const
5870 bool isShown
= false;
5872 if ( m_cellEditCtrlEnabled
)
5874 int row
= m_currentCellCoords
.GetRow();
5875 int col
= m_currentCellCoords
.GetCol();
5876 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5877 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5882 if ( editor
->IsCreated() )
5884 isShown
= editor
->GetControl()->IsShown();
5894 void wxGrid::ShowCellEditControl()
5896 if ( IsCellEditControlEnabled() )
5898 if ( !IsVisible( m_currentCellCoords
, false ) )
5900 m_cellEditCtrlEnabled
= false;
5905 wxRect rect
= CellToRect( m_currentCellCoords
);
5906 int row
= m_currentCellCoords
.GetRow();
5907 int col
= m_currentCellCoords
.GetCol();
5909 // if this is part of a multicell, find owner (topleft)
5910 int cell_rows
, cell_cols
;
5911 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5912 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5916 m_currentCellCoords
.SetRow( row
);
5917 m_currentCellCoords
.SetCol( col
);
5920 // erase the highlight and the cell contents because the editor
5921 // might not cover the entire cell
5922 wxClientDC
dc( m_gridWin
);
5924 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5925 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5926 dc
.SetPen(*wxTRANSPARENT_PEN
);
5927 dc
.DrawRectangle(rect
);
5929 // convert to scrolled coords
5930 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5936 // cell is shifted by one pixel
5937 // However, don't allow x or y to become negative
5938 // since the SetSize() method interprets that as
5945 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5946 if ( !editor
->IsCreated() )
5948 editor
->Create(m_gridWin
, wxID_ANY
,
5949 new wxGridCellEditorEvtHandler(this, editor
));
5951 wxGridEditorCreatedEvent
evt(GetId(),
5952 wxEVT_GRID_EDITOR_CREATED
,
5956 editor
->GetControl());
5957 GetEventHandler()->ProcessEvent(evt
);
5960 // resize editor to overflow into righthand cells if allowed
5961 int maxWidth
= rect
.width
;
5962 wxString value
= GetCellValue(row
, col
);
5963 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
5966 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
5967 if (maxWidth
< rect
.width
)
5968 maxWidth
= rect
.width
;
5971 int client_right
= m_gridWin
->GetClientSize().GetWidth();
5972 if (rect
.x
+ maxWidth
> client_right
)
5973 maxWidth
= client_right
- rect
.x
;
5975 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
5977 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5978 // may have changed earlier
5979 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
5982 GetCellSize( row
, i
, &c_rows
, &c_cols
);
5984 // looks weird going over a multicell
5985 if (m_table
->IsEmptyCell( row
, i
) &&
5986 (rect
.width
< maxWidth
) && (c_rows
== 1))
5988 rect
.width
+= GetColWidth( i
);
5994 if (rect
.GetRight() > client_right
)
5995 rect
.SetRight( client_right
- 1 );
5998 editor
->SetCellAttr( attr
);
5999 editor
->SetSize( rect
);
6001 editor
->GetControl()->Move(
6002 editor
->GetControl()->GetPosition().x
+ nXMove
,
6003 editor
->GetControl()->GetPosition().y
);
6004 editor
->Show( true, attr
);
6006 // recalc dimensions in case we need to
6007 // expand the scrolled window to account for editor
6010 editor
->BeginEdit(row
, col
, this);
6011 editor
->SetCellAttr(NULL
);
6019 void wxGrid::HideCellEditControl()
6021 if ( IsCellEditControlEnabled() )
6023 int row
= m_currentCellCoords
.GetRow();
6024 int col
= m_currentCellCoords
.GetCol();
6026 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6027 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6028 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6029 editor
->Show( false );
6033 // return the focus to the grid itself if the editor had it
6035 // note that we must not do this unconditionally to avoid stealing
6036 // focus from the window which just received it if we are hiding the
6037 // editor precisely because we lost focus
6038 if ( editorHadFocus
)
6039 m_gridWin
->SetFocus();
6041 // refresh whole row to the right
6042 wxRect
rect( CellToRect(row
, col
) );
6043 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6044 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6047 // ensure that the pixels under the focus ring get refreshed as well
6048 rect
.Inflate(10, 10);
6051 m_gridWin
->Refresh( false, &rect
);
6055 void wxGrid::SaveEditControlValue()
6057 if ( IsCellEditControlEnabled() )
6059 int row
= m_currentCellCoords
.GetRow();
6060 int col
= m_currentCellCoords
.GetCol();
6062 wxString oldval
= GetCellValue(row
, col
);
6064 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6065 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6068 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6070 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6072 editor
->ApplyEdit(row
, col
, this);
6074 // for compatibility reasons dating back to wx 2.8 when this event
6075 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6076 // didn't exist we allow vetoing this one too
6077 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6079 // Event has been vetoed, set the data back.
6080 SetCellValue(row
, col
, oldval
);
6090 // ------ Grid location functions
6091 // Note that all of these functions work with the logical coordinates of
6092 // grid cells and labels so you will need to convert from device
6093 // coordinates for mouse events etc.
6096 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6098 int row
= YToRow(y
);
6099 int col
= XToCol(x
);
6101 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6102 : wxGridCellCoords(row
, col
);
6105 // compute row or column from some (unscrolled) coordinate value, using either
6106 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6107 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6109 int wxGrid::PosToLinePos(int coord
,
6111 const wxGridOperations
& oper
) const
6113 const int numLines
= oper
.GetNumberOfLines(this);
6116 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6118 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6119 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6121 int maxPos
= coord
/ defaultLineSize
,
6124 // check for the simplest case: if we have no explicit line sizes
6125 // configured, then we already know the line this position falls in
6126 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6127 if ( lineEnds
.empty() )
6129 if ( maxPos
< numLines
)
6132 return clipToMinMax
? numLines
- 1 : -1;
6136 // adjust maxPos before starting the binary search
6137 if ( maxPos
>= numLines
)
6139 maxPos
= numLines
- 1;
6143 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6146 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6148 maxPos
= coord
/ minDist
;
6150 maxPos
= numLines
- 1;
6153 if ( maxPos
>= numLines
)
6154 maxPos
= numLines
- 1;
6157 // check if the position is beyond the last column
6158 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6159 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6160 return clipToMinMax
? maxPos
: -1;
6162 // or before the first one
6163 const int lineAt0
= oper
.GetLineAt(this, 0);
6164 if ( coord
< lineEnds
[lineAt0
] )
6168 // finally do perform the binary search
6169 while ( minPos
< maxPos
)
6171 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6172 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6174 "wxGrid: internal error in PosToLinePos()" );
6176 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6181 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6182 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6192 wxGrid::PosToLine(int coord
,
6194 const wxGridOperations
& oper
) const
6196 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6198 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6201 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6203 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6206 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6208 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6211 int wxGrid::XToPos(int x
) const
6213 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6216 // return the row number such that the y coord is near the edge of, or -1 if
6217 // not near an edge.
6219 // notice that position can only possibly be near an edge if the row/column is
6220 // large enough to still allow for an "inner" area that is _not_ near the edge
6221 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6222 // _never_ be considered to be near the edge).
6223 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6225 const int line
= oper
.PosToLine(this, pos
, true);
6227 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6229 // We know that we are in this line, test whether we are close enough
6230 // to start or end border, respectively.
6231 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6233 else if ( line
> 0 &&
6234 pos
- oper
.GetLineStartPos(this,
6235 line
) < WXGRID_LABEL_EDGE_ZONE
)
6242 int wxGrid::YToEdgeOfRow(int y
) const
6244 return PosToEdgeOfLine(y
, wxGridRowOperations());
6247 int wxGrid::XToEdgeOfCol(int x
) const
6249 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6252 wxRect
wxGrid::CellToRect( int row
, int col
) const
6254 wxRect
rect( -1, -1, -1, -1 );
6256 if ( row
>= 0 && row
< m_numRows
&&
6257 col
>= 0 && col
< m_numCols
)
6259 int i
, cell_rows
, cell_cols
;
6260 rect
.width
= rect
.height
= 0;
6261 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6262 // if negative then find multicell owner
6267 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6269 rect
.x
= GetColLeft(col
);
6270 rect
.y
= GetRowTop(row
);
6271 for (i
=col
; i
< col
+ cell_cols
; i
++)
6272 rect
.width
+= GetColWidth(i
);
6273 for (i
=row
; i
< row
+ cell_rows
; i
++)
6274 rect
.height
+= GetRowHeight(i
);
6276 // if grid lines are enabled, then the area of the cell is a bit smaller
6277 if (m_gridLinesEnabled
)
6287 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6289 // get the cell rectangle in logical coords
6291 wxRect
r( CellToRect( row
, col
) );
6293 // convert to device coords
6295 int left
, top
, right
, bottom
;
6296 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6297 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6299 // check against the client area of the grid window
6301 m_gridWin
->GetClientSize( &cw
, &ch
);
6303 if ( wholeCellVisible
)
6305 // is the cell wholly visible ?
6306 return ( left
>= 0 && right
<= cw
&&
6307 top
>= 0 && bottom
<= ch
);
6311 // is the cell partly visible ?
6313 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6314 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6318 // make the specified cell location visible by doing a minimal amount
6321 void wxGrid::MakeCellVisible( int row
, int col
)
6324 int xpos
= -1, ypos
= -1;
6326 if ( row
>= 0 && row
< m_numRows
&&
6327 col
>= 0 && col
< m_numCols
)
6329 // get the cell rectangle in logical coords
6330 wxRect
r( CellToRect( row
, col
) );
6332 // convert to device coords
6333 int left
, top
, right
, bottom
;
6334 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6335 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6338 m_gridWin
->GetClientSize( &cw
, &ch
);
6344 else if ( bottom
> ch
)
6346 int h
= r
.GetHeight();
6348 for ( i
= row
- 1; i
>= 0; i
-- )
6350 int rowHeight
= GetRowHeight(i
);
6351 if ( h
+ rowHeight
> ch
)
6358 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6359 // have rounding errors (this is important, because if we do,
6360 // we might not scroll at all and some cells won't be redrawn)
6362 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6363 // so just add a full scroll unit...
6364 ypos
+= m_scrollLineY
;
6367 // special handling for wide cells - show always left part of the cell!
6368 // Otherwise, e.g. when stepping from row to row, it would jump between
6369 // left and right part of the cell on every step!
6371 if ( left
< 0 || (right
- left
) >= cw
)
6375 else if ( right
> cw
)
6377 // position the view so that the cell is on the right
6379 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6380 xpos
= x0
+ (right
- cw
);
6382 // see comment for ypos above
6383 xpos
+= m_scrollLineX
;
6386 if ( xpos
!= -1 || ypos
!= -1 )
6389 xpos
/= m_scrollLineX
;
6391 ypos
/= m_scrollLineY
;
6392 Scroll( xpos
, ypos
);
6399 // ------ Grid cursor movement functions
6403 wxGrid::DoMoveCursor(bool expandSelection
,
6404 const wxGridDirectionOperations
& diroper
)
6406 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6409 if ( expandSelection
)
6411 wxGridCellCoords coords
= m_selectedBlockCorner
;
6412 if ( coords
== wxGridNoCellCoords
)
6413 coords
= m_currentCellCoords
;
6415 if ( diroper
.IsAtBoundary(coords
) )
6418 diroper
.Advance(coords
);
6420 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6422 else // don't expand selection
6426 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6429 wxGridCellCoords coords
= m_currentCellCoords
;
6430 diroper
.Advance(coords
);
6438 bool wxGrid::MoveCursorUp(bool expandSelection
)
6440 return DoMoveCursor(expandSelection
,
6441 wxGridBackwardOperations(this, wxGridRowOperations()));
6444 bool wxGrid::MoveCursorDown(bool expandSelection
)
6446 return DoMoveCursor(expandSelection
,
6447 wxGridForwardOperations(this, wxGridRowOperations()));
6450 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6452 return DoMoveCursor(expandSelection
,
6453 wxGridBackwardOperations(this, wxGridColumnOperations()));
6456 bool wxGrid::MoveCursorRight(bool expandSelection
)
6458 return DoMoveCursor(expandSelection
,
6459 wxGridForwardOperations(this, wxGridColumnOperations()));
6462 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6464 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6467 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6470 const int oldRow
= m_currentCellCoords
.GetRow();
6471 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6472 if ( newRow
== oldRow
)
6474 wxGridCellCoords
coords(m_currentCellCoords
);
6475 diroper
.Advance(coords
);
6476 newRow
= coords
.GetRow();
6479 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6484 bool wxGrid::MovePageUp()
6486 return DoMoveCursorByPage(
6487 wxGridBackwardOperations(this, wxGridRowOperations()));
6490 bool wxGrid::MovePageDown()
6492 return DoMoveCursorByPage(
6493 wxGridForwardOperations(this, wxGridRowOperations()));
6496 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6497 // until we find a non-empty cell or reach the grid end
6499 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6500 const wxGridDirectionOperations
& diroper
)
6502 while ( !diroper
.IsAtBoundary(coords
) )
6504 diroper
.Advance(coords
);
6505 if ( !m_table
->IsEmpty(coords
) )
6511 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6512 const wxGridDirectionOperations
& diroper
)
6514 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6517 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6520 wxGridCellCoords
coords(m_currentCellCoords
);
6521 if ( m_table
->IsEmpty(coords
) )
6523 // we are in an empty cell: find the next block of non-empty cells
6524 AdvanceToNextNonEmpty(coords
, diroper
);
6526 else // current cell is not empty
6528 diroper
.Advance(coords
);
6529 if ( m_table
->IsEmpty(coords
) )
6531 // we started at the end of a block, find the next one
6532 AdvanceToNextNonEmpty(coords
, diroper
);
6534 else // we're in a middle of a block
6536 // go to the end of it, i.e. find the last cell before the next
6538 while ( !diroper
.IsAtBoundary(coords
) )
6540 wxGridCellCoords
coordsNext(coords
);
6541 diroper
.Advance(coordsNext
);
6542 if ( m_table
->IsEmpty(coordsNext
) )
6545 coords
= coordsNext
;
6550 if ( expandSelection
)
6552 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6563 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6565 return DoMoveCursorByBlock(
6567 wxGridBackwardOperations(this, wxGridRowOperations())
6571 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6573 return DoMoveCursorByBlock(
6575 wxGridForwardOperations(this, wxGridRowOperations())
6579 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6581 return DoMoveCursorByBlock(
6583 wxGridBackwardOperations(this, wxGridColumnOperations())
6587 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6589 return DoMoveCursorByBlock(
6591 wxGridForwardOperations(this, wxGridColumnOperations())
6596 // ------ Label values and formatting
6599 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6602 *horiz
= m_rowLabelHorizAlign
;
6604 *vert
= m_rowLabelVertAlign
;
6607 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6610 *horiz
= m_colLabelHorizAlign
;
6612 *vert
= m_colLabelVertAlign
;
6615 int wxGrid::GetColLabelTextOrientation() const
6617 return m_colLabelTextOrientation
;
6620 wxString
wxGrid::GetRowLabelValue( int row
) const
6624 return m_table
->GetRowLabelValue( row
);
6634 wxString
wxGrid::GetColLabelValue( int col
) const
6638 return m_table
->GetColLabelValue( col
);
6648 void wxGrid::SetRowLabelSize( int width
)
6650 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6652 if ( width
== wxGRID_AUTOSIZE
)
6654 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6657 if ( width
!= m_rowLabelWidth
)
6661 m_rowLabelWin
->Show( false );
6662 m_cornerLabelWin
->Show( false );
6664 else if ( m_rowLabelWidth
== 0 )
6666 m_rowLabelWin
->Show( true );
6667 if ( m_colLabelHeight
> 0 )
6668 m_cornerLabelWin
->Show( true );
6671 m_rowLabelWidth
= width
;
6673 wxScrolledWindow::Refresh( true );
6677 void wxGrid::SetColLabelSize( int height
)
6679 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6681 if ( height
== wxGRID_AUTOSIZE
)
6683 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6686 if ( height
!= m_colLabelHeight
)
6690 m_colWindow
->Show( false );
6691 m_cornerLabelWin
->Show( false );
6693 else if ( m_colLabelHeight
== 0 )
6695 m_colWindow
->Show( true );
6696 if ( m_rowLabelWidth
> 0 )
6697 m_cornerLabelWin
->Show( true );
6700 m_colLabelHeight
= height
;
6702 wxScrolledWindow::Refresh( true );
6706 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6708 if ( m_labelBackgroundColour
!= colour
)
6710 m_labelBackgroundColour
= colour
;
6711 m_rowLabelWin
->SetBackgroundColour( colour
);
6712 m_colWindow
->SetBackgroundColour( colour
);
6713 m_cornerLabelWin
->SetBackgroundColour( colour
);
6715 if ( !GetBatchCount() )
6717 m_rowLabelWin
->Refresh();
6718 m_colWindow
->Refresh();
6719 m_cornerLabelWin
->Refresh();
6724 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6726 if ( m_labelTextColour
!= colour
)
6728 m_labelTextColour
= colour
;
6729 if ( !GetBatchCount() )
6731 m_rowLabelWin
->Refresh();
6732 m_colWindow
->Refresh();
6737 void wxGrid::SetLabelFont( const wxFont
& font
)
6740 if ( !GetBatchCount() )
6742 m_rowLabelWin
->Refresh();
6743 m_colWindow
->Refresh();
6747 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6749 // allow old (incorrect) defs to be used
6752 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6753 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6754 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6759 case wxTOP
: vert
= wxALIGN_TOP
; break;
6760 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6761 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6764 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6766 m_rowLabelHorizAlign
= horiz
;
6769 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6771 m_rowLabelVertAlign
= vert
;
6774 if ( !GetBatchCount() )
6776 m_rowLabelWin
->Refresh();
6780 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6782 // allow old (incorrect) defs to be used
6785 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6786 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6787 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6792 case wxTOP
: vert
= wxALIGN_TOP
; break;
6793 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6794 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6797 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6799 m_colLabelHorizAlign
= horiz
;
6802 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6804 m_colLabelVertAlign
= vert
;
6807 if ( !GetBatchCount() )
6809 m_colWindow
->Refresh();
6813 // Note: under MSW, the default column label font must be changed because it
6814 // does not support vertical printing
6816 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6817 // pGrid->SetLabelFont(font);
6818 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6820 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6822 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6823 m_colLabelTextOrientation
= textOrientation
;
6825 if ( !GetBatchCount() )
6826 m_colWindow
->Refresh();
6829 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6833 m_table
->SetRowLabelValue( row
, s
);
6834 if ( !GetBatchCount() )
6836 wxRect rect
= CellToRect( row
, 0 );
6837 if ( rect
.height
> 0 )
6839 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6841 rect
.width
= m_rowLabelWidth
;
6842 m_rowLabelWin
->Refresh( true, &rect
);
6848 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6852 m_table
->SetColLabelValue( col
, s
);
6853 if ( !GetBatchCount() )
6855 if ( m_useNativeHeader
)
6857 GetGridColHeader()->UpdateColumn(col
);
6861 wxRect rect
= CellToRect( 0, col
);
6862 if ( rect
.width
> 0 )
6864 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6866 rect
.height
= m_colLabelHeight
;
6867 GetColLabelWindow()->Refresh( true, &rect
);
6874 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6876 if ( m_gridLineColour
!= colour
)
6878 m_gridLineColour
= colour
;
6880 if ( GridLinesEnabled() )
6885 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6887 if ( m_cellHighlightColour
!= colour
)
6889 m_cellHighlightColour
= colour
;
6891 wxClientDC
dc( m_gridWin
);
6893 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6894 DrawCellHighlight(dc
, attr
);
6899 void wxGrid::SetCellHighlightPenWidth(int width
)
6901 if (m_cellHighlightPenWidth
!= width
)
6903 m_cellHighlightPenWidth
= width
;
6905 // Just redrawing the cell highlight is not enough since that won't
6906 // make any visible change if the the thickness is getting smaller.
6907 int row
= m_currentCellCoords
.GetRow();
6908 int col
= m_currentCellCoords
.GetCol();
6909 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6912 wxRect rect
= CellToRect(row
, col
);
6913 m_gridWin
->Refresh(true, &rect
);
6917 void wxGrid::SetCellHighlightROPenWidth(int width
)
6919 if (m_cellHighlightROPenWidth
!= width
)
6921 m_cellHighlightROPenWidth
= width
;
6923 // Just redrawing the cell highlight is not enough since that won't
6924 // make any visible change if the the thickness is getting smaller.
6925 int row
= m_currentCellCoords
.GetRow();
6926 int col
= m_currentCellCoords
.GetCol();
6927 if ( row
== -1 || col
== -1 ||
6928 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6931 wxRect rect
= CellToRect(row
, col
);
6932 m_gridWin
->Refresh(true, &rect
);
6936 void wxGrid::RedrawGridLines()
6938 // the lines will be redrawn when the window is thawn
6939 if ( GetBatchCount() )
6942 if ( GridLinesEnabled() )
6944 wxClientDC
dc( m_gridWin
);
6946 DrawAllGridLines( dc
, wxRegion() );
6948 else // remove the grid lines
6950 m_gridWin
->Refresh();
6954 void wxGrid::EnableGridLines( bool enable
)
6956 if ( enable
!= m_gridLinesEnabled
)
6958 m_gridLinesEnabled
= enable
;
6964 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
6970 if ( GridLinesEnabled() )
6975 int wxGrid::GetDefaultRowSize() const
6977 return m_defaultRowHeight
;
6980 int wxGrid::GetRowSize( int row
) const
6982 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
6984 return GetRowHeight(row
);
6987 int wxGrid::GetDefaultColSize() const
6989 return m_defaultColWidth
;
6992 int wxGrid::GetColSize( int col
) const
6994 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
6996 return GetColWidth(col
);
6999 // ============================================================================
7000 // access to the grid attributes: each of them has a default value in the grid
7001 // itself and may be overidden on a per-cell basis
7002 // ============================================================================
7004 // ----------------------------------------------------------------------------
7005 // setting default attributes
7006 // ----------------------------------------------------------------------------
7008 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7010 m_defaultCellAttr
->SetBackgroundColour(col
);
7012 m_gridWin
->SetBackgroundColour(col
);
7016 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7018 m_defaultCellAttr
->SetTextColour(col
);
7021 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7023 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7026 void wxGrid::SetDefaultCellOverflow( bool allow
)
7028 m_defaultCellAttr
->SetOverflow(allow
);
7031 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7033 m_defaultCellAttr
->SetFont(font
);
7036 // For editors and renderers the type registry takes precedence over the
7037 // default attr, so we need to register the new editor/renderer for the string
7038 // data type in order to make setting a default editor/renderer appear to
7041 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7043 RegisterDataType(wxGRID_VALUE_STRING
,
7045 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7048 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7050 RegisterDataType(wxGRID_VALUE_STRING
,
7051 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7055 // ----------------------------------------------------------------------------
7056 // access to the default attributes
7057 // ----------------------------------------------------------------------------
7059 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7061 return m_defaultCellAttr
->GetBackgroundColour();
7064 wxColour
wxGrid::GetDefaultCellTextColour() const
7066 return m_defaultCellAttr
->GetTextColour();
7069 wxFont
wxGrid::GetDefaultCellFont() const
7071 return m_defaultCellAttr
->GetFont();
7074 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7076 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7079 bool wxGrid::GetDefaultCellOverflow() const
7081 return m_defaultCellAttr
->GetOverflow();
7084 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7086 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7089 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7091 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7094 // ----------------------------------------------------------------------------
7095 // access to cell attributes
7096 // ----------------------------------------------------------------------------
7098 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7100 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7101 wxColour colour
= attr
->GetBackgroundColour();
7107 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7109 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7110 wxColour colour
= attr
->GetTextColour();
7116 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7118 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7119 wxFont font
= attr
->GetFont();
7125 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7127 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7128 attr
->GetAlignment(horiz
, vert
);
7132 bool wxGrid::GetCellOverflow( int row
, int col
) const
7134 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7135 bool allow
= attr
->GetOverflow();
7142 wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7144 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7145 attr
->GetSize( num_rows
, num_cols
);
7148 if ( *num_rows
== 1 && *num_cols
== 1 )
7149 return CellSpan_None
; // just a normal cell
7151 if ( *num_rows
< 0 || *num_cols
< 0 )
7152 return CellSpan_Inside
; // covered by a multi-span cell
7154 // this cell spans multiple cells to its right/bottom
7155 return CellSpan_Main
;
7158 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7160 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7161 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7167 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7169 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7170 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7176 bool wxGrid::IsReadOnly(int row
, int col
) const
7178 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7179 bool isReadOnly
= attr
->IsReadOnly();
7185 // ----------------------------------------------------------------------------
7186 // attribute support: cache, automatic provider creation, ...
7187 // ----------------------------------------------------------------------------
7189 bool wxGrid::CanHaveAttributes() const
7196 return m_table
->CanHaveAttributes();
7199 void wxGrid::ClearAttrCache()
7201 if ( m_attrCache
.row
!= -1 )
7203 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7204 m_attrCache
.attr
= NULL
;
7205 m_attrCache
.row
= -1;
7206 // wxSafeDecRec(...) might cause event processing that accesses
7207 // the cached attribute, if one exists (e.g. by deleting the
7208 // editor stored within the attribute). Therefore it is important
7209 // to invalidate the cache before calling wxSafeDecRef!
7210 wxSafeDecRef(oldAttr
);
7214 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7218 wxGrid
*self
= (wxGrid
*)this; // const_cast
7220 self
->ClearAttrCache();
7221 self
->m_attrCache
.row
= row
;
7222 self
->m_attrCache
.col
= col
;
7223 self
->m_attrCache
.attr
= attr
;
7228 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7230 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7232 *attr
= m_attrCache
.attr
;
7233 wxSafeIncRef(m_attrCache
.attr
);
7235 #ifdef DEBUG_ATTR_CACHE
7236 gs_nAttrCacheHits
++;
7243 #ifdef DEBUG_ATTR_CACHE
7244 gs_nAttrCacheMisses
++;
7251 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7253 wxGridCellAttr
*attr
= NULL
;
7254 // Additional test to avoid looking at the cache e.g. for
7255 // wxNoCellCoords, as this will confuse memory management.
7258 if ( !LookupAttr(row
, col
, &attr
) )
7260 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7262 CacheAttr(row
, col
, attr
);
7268 attr
->SetDefAttr(m_defaultCellAttr
);
7272 attr
= m_defaultCellAttr
;
7279 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7281 wxGridCellAttr
*attr
= NULL
;
7282 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7284 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7285 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7287 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7290 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7292 // artificially inc the ref count to match DecRef() in caller
7294 m_table
->SetAttr(attr
, row
, col
);
7300 // ----------------------------------------------------------------------------
7301 // setting column attributes (wrappers around SetColAttr)
7302 // ----------------------------------------------------------------------------
7304 void wxGrid::SetColFormatBool(int col
)
7306 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7309 void wxGrid::SetColFormatNumber(int col
)
7311 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7314 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7316 wxString typeName
= wxGRID_VALUE_FLOAT
;
7317 if ( (width
!= -1) || (precision
!= -1) )
7319 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7322 SetColFormatCustom(col
, typeName
);
7325 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7327 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7329 attr
= new wxGridCellAttr
;
7330 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7331 attr
->SetRenderer(renderer
);
7332 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7333 attr
->SetEditor(editor
);
7335 SetColAttr(col
, attr
);
7339 // ----------------------------------------------------------------------------
7340 // setting cell attributes: this is forwarded to the table
7341 // ----------------------------------------------------------------------------
7343 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7345 if ( CanHaveAttributes() )
7347 m_table
->SetAttr(attr
, row
, col
);
7356 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7358 if ( CanHaveAttributes() )
7360 m_table
->SetRowAttr(attr
, row
);
7369 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7371 if ( CanHaveAttributes() )
7373 m_table
->SetColAttr(attr
, col
);
7382 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7384 if ( CanHaveAttributes() )
7386 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7387 attr
->SetBackgroundColour(colour
);
7392 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7394 if ( CanHaveAttributes() )
7396 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7397 attr
->SetTextColour(colour
);
7402 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7404 if ( CanHaveAttributes() )
7406 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7407 attr
->SetFont(font
);
7412 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7414 if ( CanHaveAttributes() )
7416 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7417 attr
->SetAlignment(horiz
, vert
);
7422 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7424 if ( CanHaveAttributes() )
7426 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7427 attr
->SetOverflow(allow
);
7432 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7434 if ( CanHaveAttributes() )
7436 int cell_rows
, cell_cols
;
7438 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7439 attr
->GetSize(&cell_rows
, &cell_cols
);
7440 attr
->SetSize(num_rows
, num_cols
);
7443 // Cannot set the size of a cell to 0 or negative values
7444 // While it is perfectly legal to do that, this function cannot
7445 // handle all the possibilies, do it by hand by getting the CellAttr.
7446 // You can only set the size of a cell to 1,1 or greater with this fn
7447 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7448 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7449 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7450 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7452 // if this was already a multicell then "turn off" the other cells first
7453 if ((cell_rows
> 1) || (cell_cols
> 1))
7456 for (j
=row
; j
< row
+ cell_rows
; j
++)
7458 for (i
=col
; i
< col
+ cell_cols
; i
++)
7460 if ((i
!= col
) || (j
!= row
))
7462 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7463 attr_stub
->SetSize( 1, 1 );
7464 attr_stub
->DecRef();
7470 // mark the cells that will be covered by this cell to
7471 // negative or zero values to point back at this cell
7472 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7475 for (j
=row
; j
< row
+ num_rows
; j
++)
7477 for (i
=col
; i
< col
+ num_cols
; i
++)
7479 if ((i
!= col
) || (j
!= row
))
7481 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7482 attr_stub
->SetSize( row
- j
, col
- i
);
7483 attr_stub
->DecRef();
7491 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7493 if ( CanHaveAttributes() )
7495 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7496 attr
->SetRenderer(renderer
);
7501 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7503 if ( CanHaveAttributes() )
7505 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7506 attr
->SetEditor(editor
);
7511 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7513 if ( CanHaveAttributes() )
7515 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7516 attr
->SetReadOnly(isReadOnly
);
7521 // ----------------------------------------------------------------------------
7522 // Data type registration
7523 // ----------------------------------------------------------------------------
7525 void wxGrid::RegisterDataType(const wxString
& typeName
,
7526 wxGridCellRenderer
* renderer
,
7527 wxGridCellEditor
* editor
)
7529 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7533 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7535 wxString typeName
= m_table
->GetTypeName(row
, col
);
7536 return GetDefaultEditorForType(typeName
);
7539 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7541 wxString typeName
= m_table
->GetTypeName(row
, col
);
7542 return GetDefaultRendererForType(typeName
);
7545 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7547 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7548 if ( index
== wxNOT_FOUND
)
7550 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7555 return m_typeRegistry
->GetEditor(index
);
7558 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7560 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7561 if ( index
== wxNOT_FOUND
)
7563 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7568 return m_typeRegistry
->GetRenderer(index
);
7571 // ----------------------------------------------------------------------------
7573 // ----------------------------------------------------------------------------
7575 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7579 setFixed
= new wxGridFixedIndicesSet
;
7582 setFixed
->insert(line
);
7586 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7588 return !setFixed
|| !setFixed
->count(line
);
7591 void wxGrid::EnableDragRowSize( bool enable
)
7593 m_canDragRowSize
= enable
;
7596 void wxGrid::EnableDragColSize( bool enable
)
7598 m_canDragColSize
= enable
;
7601 void wxGrid::EnableDragGridSize( bool enable
)
7603 m_canDragGridSize
= enable
;
7606 void wxGrid::EnableDragCell( bool enable
)
7608 m_canDragCell
= enable
;
7611 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7613 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7615 if ( resizeExistingRows
)
7617 // since we are resizing all rows to the default row size,
7618 // we can simply clear the row heights and row bottoms
7619 // arrays (which also allows us to take advantage of
7620 // some speed optimisations)
7621 m_rowHeights
.Empty();
7622 m_rowBottoms
.Empty();
7623 if ( !GetBatchCount() )
7628 void wxGrid::SetRowSize( int row
, int height
)
7630 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7632 // if < 0 then calculate new height from label
7636 wxArrayString lines
;
7637 wxClientDC
dc(m_rowLabelWin
);
7638 dc
.SetFont(GetLabelFont());
7639 StringToLines(GetRowLabelValue( row
), lines
);
7640 GetTextBoxSize( dc
, lines
, &w
, &h
);
7641 //check that it is not less than the minimal height
7642 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7645 // See comment in SetColSize
7646 if ( height
< GetRowMinimalAcceptableHeight())
7649 if ( m_rowHeights
.IsEmpty() )
7651 // need to really create the array
7655 int h
= wxMax( 0, height
);
7656 int diff
= h
- m_rowHeights
[row
];
7658 m_rowHeights
[row
] = h
;
7659 for ( int i
= row
; i
< m_numRows
; i
++ )
7661 m_rowBottoms
[i
] += diff
;
7664 if ( !GetBatchCount() )
7668 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7670 // we dont allow zero default column width
7671 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7673 if ( resizeExistingCols
)
7675 // since we are resizing all columns to the default column size,
7676 // we can simply clear the col widths and col rights
7677 // arrays (which also allows us to take advantage of
7678 // some speed optimisations)
7679 m_colWidths
.Empty();
7680 m_colRights
.Empty();
7681 if ( !GetBatchCount() )
7686 void wxGrid::SetColSize( int col
, int width
)
7688 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7690 // if < 0 then calculate new width from label
7694 wxArrayString lines
;
7695 wxClientDC
dc(m_colWindow
);
7696 dc
.SetFont(GetLabelFont());
7697 StringToLines(GetColLabelValue(col
), lines
);
7698 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7699 GetTextBoxSize( dc
, lines
, &w
, &h
);
7701 GetTextBoxSize( dc
, lines
, &h
, &w
);
7703 //check that it is not less than the minimal width
7704 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7707 // we intentionally don't test whether the width is less than
7708 // GetColMinimalWidth() here but we do compare it with
7709 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7710 // #651) -- and we also always allow the width of 0 as it has the special
7711 // sense of hiding the column
7712 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7715 if ( m_colWidths
.IsEmpty() )
7717 // need to really create the array
7721 const int diff
= width
- m_colWidths
[col
];
7722 m_colWidths
[col
] = width
;
7723 if ( m_useNativeHeader
)
7724 GetGridColHeader()->UpdateColumn(col
);
7725 //else: will be refreshed when the header is redrawn
7727 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7729 m_colRights
[GetColAt(colPos
)] += diff
;
7732 if ( !GetBatchCount() )
7739 void wxGrid::SetColMinimalWidth( int col
, int width
)
7741 if (width
> GetColMinimalAcceptableWidth())
7743 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7744 m_colMinWidths
[key
] = width
;
7748 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7750 if (width
> GetRowMinimalAcceptableHeight())
7752 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7753 m_rowMinHeights
[key
] = width
;
7757 int wxGrid::GetColMinimalWidth(int col
) const
7759 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7760 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7762 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7765 int wxGrid::GetRowMinimalHeight(int row
) const
7767 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7768 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7770 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7773 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7775 // We do allow a width of 0 since this gives us
7776 // an easy way to temporarily hiding columns.
7778 m_minAcceptableColWidth
= width
;
7781 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7783 // We do allow a height of 0 since this gives us
7784 // an easy way to temporarily hiding rows.
7786 m_minAcceptableRowHeight
= height
;
7789 int wxGrid::GetColMinimalAcceptableWidth() const
7791 return m_minAcceptableColWidth
;
7794 int wxGrid::GetRowMinimalAcceptableHeight() const
7796 return m_minAcceptableRowHeight
;
7799 // ----------------------------------------------------------------------------
7801 // ----------------------------------------------------------------------------
7804 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7806 const bool column
= direction
== wxGRID_COLUMN
;
7808 wxClientDC
dc(m_gridWin
);
7810 // cancel editing of cell
7811 HideCellEditControl();
7812 SaveEditControlValue();
7814 // initialize both of them just to avoid compiler warnings
7818 wxCoord extent
, extentMax
= 0;
7819 int max
= column
? m_numRows
: m_numCols
;
7820 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7833 // we need to account for the cells spanning multiple columns/rows:
7834 // while they may need a lot of space, they don't need all of it in
7836 int numRows
, numCols
;
7837 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
7838 if ( span
== CellSpan_Inside
)
7840 // we need to get the size of the main cell, not of a cell hidden
7845 // get the size of the main cell too
7846 GetCellSize(row
, col
, &numRows
, &numCols
);
7849 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7850 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7853 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7854 extent
= column
? size
.x
: size
.y
;
7856 if ( span
!= CellSpan_None
)
7858 // we spread the size of a spanning cell over all the cells it
7859 // covers evenly -- this is probably not ideal but we can't
7860 // really do much better here
7862 // notice that numCols and numRows are never 0 as they
7863 // correspond to the size of the main cell of the span and not
7864 // of the cell inside it
7865 extent
/= column
? numCols
: numRows
;
7868 if ( extent
> extentMax
)
7877 // now also compare with the column label extent
7879 dc
.SetFont( GetLabelFont() );
7883 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7884 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7888 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7890 extent
= column
? w
: h
;
7891 if ( extent
> extentMax
)
7896 // empty column - give default extent (notice that if extentMax is less
7897 // than default extent but != 0, it's OK)
7898 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7903 // leave some space around text
7911 // Ensure automatic width is not less than minimal width. See the
7912 // comment in SetColSize() for explanation of why this isn't done
7915 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7917 SetColSize( col
, extentMax
);
7918 if ( !GetBatchCount() )
7920 if ( m_useNativeHeader
)
7922 GetGridColHeader()->UpdateColumn(col
);
7927 m_gridWin
->GetClientSize( &cw
, &ch
);
7928 wxRect
rect ( CellToRect( 0, col
) );
7930 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7931 rect
.width
= cw
- rect
.x
;
7932 rect
.height
= m_colLabelHeight
;
7933 GetColLabelWindow()->Refresh( true, &rect
);
7939 // Ensure automatic width is not less than minimal height. See the
7940 // comment in SetColSize() for explanation of why this isn't done
7943 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7945 SetRowSize(row
, extentMax
);
7946 if ( !GetBatchCount() )
7949 m_gridWin
->GetClientSize( &cw
, &ch
);
7950 wxRect
rect( CellToRect( row
, 0 ) );
7952 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
7953 rect
.width
= m_rowLabelWidth
;
7954 rect
.height
= ch
- rect
.y
;
7955 m_rowLabelWin
->Refresh( true, &rect
);
7962 SetColMinimalWidth(col
, extentMax
);
7964 SetRowMinimalHeight(row
, extentMax
);
7968 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
7970 // calculate size for the rows or columns?
7971 const bool calcRows
= direction
== wxGRID_ROW
;
7973 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
7974 : GetGridColLabelWindow());
7975 dc
.SetFont(GetLabelFont());
7977 // which dimension should we take into account for calculations?
7979 // for columns, the text can be only horizontal so it's easy but for rows
7980 // we also have to take into account the text orientation
7982 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
7984 wxArrayString lines
;
7985 wxCoord extentMax
= 0;
7987 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
7988 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
7992 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
7993 : GetColLabelValue(rowOrCol
);
7994 StringToLines(label
, lines
);
7997 GetTextBoxSize(dc
, lines
, &w
, &h
);
7999 const wxCoord extent
= useWidth
? w
: h
;
8000 if ( extent
> extentMax
)
8006 // empty column - give default extent (notice that if extentMax is less
8007 // than default extent but != 0, it's OK)
8008 extentMax
= calcRows
? GetDefaultRowLabelSize()
8009 : GetDefaultColLabelSize();
8012 // leave some space around text (taken from AutoSizeColOrRow)
8021 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8023 int width
= m_rowLabelWidth
;
8025 wxGridUpdateLocker locker
;
8027 locker
.Create(this);
8029 for ( int col
= 0; col
< m_numCols
; col
++ )
8032 AutoSizeColumn(col
, setAsMin
);
8034 width
+= GetColWidth(col
);
8040 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8042 int height
= m_colLabelHeight
;
8044 wxGridUpdateLocker locker
;
8046 locker
.Create(this);
8048 for ( int row
= 0; row
< m_numRows
; row
++ )
8051 AutoSizeRow(row
, setAsMin
);
8053 height
+= GetRowHeight(row
);
8059 void wxGrid::AutoSize()
8061 wxGridUpdateLocker
locker(this);
8063 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8064 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8066 // we know that we're not going to have scrollbars so disable them now to
8067 // avoid trouble in SetClientSize() which can otherwise set the correct
8068 // client size but also leave space for (not needed any more) scrollbars
8069 SetScrollbars(0, 0, 0, 0, 0, 0, true);
8071 // restore the scroll rate parameters overwritten by SetScrollbars()
8072 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
8074 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8077 void wxGrid::AutoSizeRowLabelSize( int row
)
8079 // Hide the edit control, so it
8080 // won't interfere with drag-shrinking.
8081 if ( IsCellEditControlShown() )
8083 HideCellEditControl();
8084 SaveEditControlValue();
8087 // autosize row height depending on label text
8088 SetRowSize(row
, -1);
8092 void wxGrid::AutoSizeColLabelSize( int col
)
8094 // Hide the edit control, so it
8095 // won't interfere with drag-shrinking.
8096 if ( IsCellEditControlShown() )
8098 HideCellEditControl();
8099 SaveEditControlValue();
8102 // autosize column width depending on label text
8103 SetColSize(col
, -1);
8107 wxSize
wxGrid::DoGetBestSize() const
8109 wxGrid
*self
= (wxGrid
*)this; // const_cast
8111 // we do the same as in AutoSize() here with the exception that we don't
8112 // change the column/row sizes, only calculate them
8113 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8114 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8116 // NOTE: This size should be cached, but first we need to add calls to
8117 // InvalidateBestSize everywhere that could change the results of this
8119 // CacheBestSize(size);
8121 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8122 + GetWindowBorderSize();
8130 wxPen
& wxGrid::GetDividerPen() const
8135 // ----------------------------------------------------------------------------
8136 // cell value accessor functions
8137 // ----------------------------------------------------------------------------
8139 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8143 m_table
->SetValue( row
, col
, s
);
8144 if ( !GetBatchCount() )
8147 wxRect
rect( CellToRect( row
, col
) );
8149 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8150 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8151 m_gridWin
->Refresh( false, &rect
);
8154 if ( m_currentCellCoords
.GetRow() == row
&&
8155 m_currentCellCoords
.GetCol() == col
&&
8156 IsCellEditControlShown())
8157 // Note: If we are using IsCellEditControlEnabled,
8158 // this interacts badly with calling SetCellValue from
8159 // an EVT_GRID_CELL_CHANGE handler.
8161 HideCellEditControl();
8162 ShowCellEditControl(); // will reread data from table
8167 // ----------------------------------------------------------------------------
8168 // block, row and column selection
8169 // ----------------------------------------------------------------------------
8171 void wxGrid::SelectRow( int row
, bool addToSelected
)
8176 if ( !addToSelected
)
8179 m_selection
->SelectRow(row
);
8182 void wxGrid::SelectCol( int col
, bool addToSelected
)
8187 if ( !addToSelected
)
8190 m_selection
->SelectCol(col
);
8193 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8199 if ( !addToSelected
)
8202 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8205 void wxGrid::SelectAll()
8207 if ( m_numRows
> 0 && m_numCols
> 0 )
8210 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8214 // ----------------------------------------------------------------------------
8215 // cell, row and col deselection
8216 // ----------------------------------------------------------------------------
8218 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8223 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8224 if ( mode
== oper
.GetSelectionMode() ||
8225 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8227 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8228 if ( m_selection
->IsInSelection(c
) )
8229 m_selection
->ToggleCellSelection(c
);
8231 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8233 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8234 for ( int i
= 0; i
< nOther
; i
++ )
8236 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8237 if ( m_selection
->IsInSelection(c
) )
8238 m_selection
->ToggleCellSelection(c
);
8241 //else: can only select orthogonal lines so no lines in this direction
8242 // could have been selected anyhow
8245 void wxGrid::DeselectRow(int row
)
8247 DeselectLine(row
, wxGridRowOperations());
8250 void wxGrid::DeselectCol(int col
)
8252 DeselectLine(col
, wxGridColumnOperations());
8255 void wxGrid::DeselectCell( int row
, int col
)
8257 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8258 m_selection
->ToggleCellSelection(row
, col
);
8261 bool wxGrid::IsSelection() const
8263 return ( m_selection
&& (m_selection
->IsSelection() ||
8264 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8265 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8268 bool wxGrid::IsInSelection( int row
, int col
) const
8270 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8271 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8272 col
>= m_selectedBlockTopLeft
.GetCol() &&
8273 row
<= m_selectedBlockBottomRight
.GetRow() &&
8274 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8277 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8281 wxGridCellCoordsArray a
;
8285 return m_selection
->m_cellSelection
;
8288 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8292 wxGridCellCoordsArray a
;
8296 return m_selection
->m_blockSelectionTopLeft
;
8299 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8303 wxGridCellCoordsArray a
;
8307 return m_selection
->m_blockSelectionBottomRight
;
8310 wxArrayInt
wxGrid::GetSelectedRows() const
8318 return m_selection
->m_rowSelection
;
8321 wxArrayInt
wxGrid::GetSelectedCols() const
8329 return m_selection
->m_colSelection
;
8332 void wxGrid::ClearSelection()
8334 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8335 m_selectedBlockBottomRight
);
8336 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8337 m_selectedBlockCorner
);
8339 m_selectedBlockTopLeft
=
8340 m_selectedBlockBottomRight
=
8341 m_selectedBlockCorner
= wxGridNoCellCoords
;
8343 if ( !r1
.IsEmpty() )
8344 RefreshRect(r1
, false);
8345 if ( !r2
.IsEmpty() )
8346 RefreshRect(r2
, false);
8349 m_selection
->ClearSelection();
8352 // This function returns the rectangle that encloses the given block
8353 // in device coords clipped to the client size of the grid window.
8355 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8356 const wxGridCellCoords
& bottomRight
) const
8359 wxRect tempCellRect
= CellToRect(topLeft
);
8360 if ( tempCellRect
!= wxGridNoCellRect
)
8362 resultRect
= tempCellRect
;
8366 resultRect
= wxRect(0, 0, 0, 0);
8369 tempCellRect
= CellToRect(bottomRight
);
8370 if ( tempCellRect
!= wxGridNoCellRect
)
8372 resultRect
+= tempCellRect
;
8376 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8377 return wxGridNoCellRect
;
8380 // Ensure that left/right and top/bottom pairs are in order.
8381 int left
= resultRect
.GetLeft();
8382 int top
= resultRect
.GetTop();
8383 int right
= resultRect
.GetRight();
8384 int bottom
= resultRect
.GetBottom();
8386 int leftCol
= topLeft
.GetCol();
8387 int topRow
= topLeft
.GetRow();
8388 int rightCol
= bottomRight
.GetCol();
8389 int bottomRow
= bottomRight
.GetRow();
8413 // The following loop is ONLY necessary to detect and handle merged cells.
8415 m_gridWin
->GetClientSize( &cw
, &ch
);
8417 // Get the origin coordinates: notice that they will be negative if the
8418 // grid is scrolled downwards/to the right.
8419 int gridOriginX
= 0;
8420 int gridOriginY
= 0;
8421 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8423 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8424 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8426 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8427 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8429 // Bound our loop so that we only examine the portion of the selected block
8430 // that is shown on screen. Therefore, we compare the Top-Left block values
8431 // to the Top-Left screen values, and the Bottom-Right block values to the
8432 // Bottom-Right screen values, choosing appropriately.
8433 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8434 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8435 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8436 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8438 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8440 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8442 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8443 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8445 tempCellRect
= CellToRect( j
, i
);
8447 if (tempCellRect
.x
< left
)
8448 left
= tempCellRect
.x
;
8449 if (tempCellRect
.y
< top
)
8450 top
= tempCellRect
.y
;
8451 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8452 right
= tempCellRect
.x
+ tempCellRect
.width
;
8453 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8454 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8458 i
= visibleRightCol
; // jump over inner cells.
8463 // Convert to scrolled coords
8464 CalcScrolledPosition( left
, top
, &left
, &top
);
8465 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8467 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8468 return wxRect(0,0,0,0);
8470 resultRect
.SetLeft( wxMax(0, left
) );
8471 resultRect
.SetTop( wxMax(0, top
) );
8472 resultRect
.SetRight( wxMin(cw
, right
) );
8473 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8478 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8479 const wxGridOperations
& oper
)
8482 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8483 const int numLines
= oper
.GetNumberOfLines(this);
8484 for ( int i
= 0; i
< numLines
; i
++ )
8486 int size
= sizeInfo
.GetSize(i
);
8487 if ( size
!= sizeInfo
.m_sizeDefault
)
8488 oper
.SetLineSize(this, i
, size
);
8493 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8495 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8498 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8500 DoSetSizes(sizeInfo
, wxGridRowOperations());
8503 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8505 m_sizeDefault
= defSize
;
8506 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8508 if ( allSizes
[i
] != defSize
)
8509 m_customSizes
[i
] = allSizes
[i
];
8513 int wxGridSizesInfo::GetSize(unsigned pos
) const
8515 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8517 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8520 // ----------------------------------------------------------------------------
8522 // ----------------------------------------------------------------------------
8524 #if wxUSE_DRAG_AND_DROP
8526 // this allow setting drop target directly on wxGrid
8527 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8529 GetGridWindow()->SetDropTarget(dropTarget
);
8532 #endif // wxUSE_DRAG_AND_DROP
8534 // ----------------------------------------------------------------------------
8535 // grid event classes
8536 // ----------------------------------------------------------------------------
8538 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8540 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8541 int row
, int col
, int x
, int y
, bool sel
,
8542 bool control
, bool shift
, bool alt
, bool meta
)
8543 : wxNotifyEvent( type
, id
),
8544 wxKeyboardState(control
, shift
, alt
, meta
)
8546 Init(row
, col
, x
, y
, sel
);
8548 SetEventObject(obj
);
8551 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8553 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8554 int rowOrCol
, int x
, int y
,
8555 bool control
, bool shift
, bool alt
, bool meta
)
8556 : wxNotifyEvent( type
, id
),
8557 wxKeyboardState(control
, shift
, alt
, meta
)
8559 Init(rowOrCol
, x
, y
);
8561 SetEventObject(obj
);
8565 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8567 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8568 const wxGridCellCoords
& topLeft
,
8569 const wxGridCellCoords
& bottomRight
,
8570 bool sel
, bool control
,
8571 bool shift
, bool alt
, bool meta
)
8572 : wxNotifyEvent( type
, id
),
8573 wxKeyboardState(control
, shift
, alt
, meta
)
8575 Init(topLeft
, bottomRight
, sel
);
8577 SetEventObject(obj
);
8581 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8583 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8584 wxObject
* obj
, int row
,
8585 int col
, wxControl
* ctrl
)
8586 : wxCommandEvent(type
, id
)
8588 SetEventObject(obj
);
8595 // ----------------------------------------------------------------------------
8596 // wxGridTypeRegistry
8597 // ----------------------------------------------------------------------------
8599 wxGridTypeRegistry::~wxGridTypeRegistry()
8601 size_t count
= m_typeinfo
.GetCount();
8602 for ( size_t i
= 0; i
< count
; i
++ )
8603 delete m_typeinfo
[i
];
8606 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8607 wxGridCellRenderer
* renderer
,
8608 wxGridCellEditor
* editor
)
8610 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8612 // is it already registered?
8613 int loc
= FindRegisteredDataType(typeName
);
8614 if ( loc
!= wxNOT_FOUND
)
8616 delete m_typeinfo
[loc
];
8617 m_typeinfo
[loc
] = info
;
8621 m_typeinfo
.Add(info
);
8625 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8627 size_t count
= m_typeinfo
.GetCount();
8628 for ( size_t i
= 0; i
< count
; i
++ )
8630 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8639 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8641 int index
= FindRegisteredDataType(typeName
);
8642 if ( index
== wxNOT_FOUND
)
8644 // check whether this is one of the standard ones, in which case
8645 // register it "on the fly"
8647 if ( typeName
== wxGRID_VALUE_STRING
)
8649 RegisterDataType(wxGRID_VALUE_STRING
,
8650 new wxGridCellStringRenderer
,
8651 new wxGridCellTextEditor
);
8654 #endif // wxUSE_TEXTCTRL
8656 if ( typeName
== wxGRID_VALUE_BOOL
)
8658 RegisterDataType(wxGRID_VALUE_BOOL
,
8659 new wxGridCellBoolRenderer
,
8660 new wxGridCellBoolEditor
);
8663 #endif // wxUSE_CHECKBOX
8665 if ( typeName
== wxGRID_VALUE_NUMBER
)
8667 RegisterDataType(wxGRID_VALUE_NUMBER
,
8668 new wxGridCellNumberRenderer
,
8669 new wxGridCellNumberEditor
);
8671 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8673 RegisterDataType(wxGRID_VALUE_FLOAT
,
8674 new wxGridCellFloatRenderer
,
8675 new wxGridCellFloatEditor
);
8678 #endif // wxUSE_TEXTCTRL
8680 if ( typeName
== wxGRID_VALUE_CHOICE
)
8682 RegisterDataType(wxGRID_VALUE_CHOICE
,
8683 new wxGridCellStringRenderer
,
8684 new wxGridCellChoiceEditor
);
8687 #endif // wxUSE_COMBOBOX
8692 // we get here only if just added the entry for this type, so return
8694 index
= m_typeinfo
.GetCount() - 1;
8700 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8702 int index
= FindDataType(typeName
);
8703 if ( index
== wxNOT_FOUND
)
8705 // the first part of the typename is the "real" type, anything after ':'
8706 // are the parameters for the renderer
8707 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8708 if ( index
== wxNOT_FOUND
)
8713 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8714 wxGridCellRenderer
*rendererOld
= renderer
;
8715 renderer
= renderer
->Clone();
8716 rendererOld
->DecRef();
8718 wxGridCellEditor
*editor
= GetEditor(index
);
8719 wxGridCellEditor
*editorOld
= editor
;
8720 editor
= editor
->Clone();
8721 editorOld
->DecRef();
8723 // do it even if there are no parameters to reset them to defaults
8724 wxString params
= typeName
.AfterFirst(wxT(':'));
8725 renderer
->SetParameters(params
);
8726 editor
->SetParameters(params
);
8728 // register the new typename
8729 RegisterDataType(typeName
, renderer
, editor
);
8731 // we just registered it, it's the last one
8732 index
= m_typeinfo
.GetCount() - 1;
8738 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8740 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8747 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8749 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8756 #endif // wxUSE_GRID