1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
58 const char wxGridNameStr
[] = "grid";
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_MOTIF(identifier) identifier
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
69 #define WXUNUSED_GTK(identifier) identifier
72 // Required for wxIs... functions
75 WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, ::wxIntegerHash
, ::wxIntegerEqual
,
76 wxGridFixedIndicesSet
, class WXDLLIMPEXP_ADV
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
86 //#define DEBUG_ATTR_CACHE
87 #ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits
= 0;
89 static size_t gs_nAttrCacheMisses
= 0;
92 // this struct simply combines together the default header renderers
94 // as the renderers ctors are trivial, there is no problem with making them
96 struct DefaultHeaderRenderers
98 wxGridColumnHeaderRendererDefault colRenderer
;
99 wxGridRowHeaderRendererDefault rowRenderer
;
100 wxGridCornerHeaderRendererDefault cornerRenderer
;
101 } gs_defaultHeaderRenderers
;
103 } // anonymous namespace
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
110 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
116 const size_t GRID_SCROLL_LINE_X
= 15;
117 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
119 // the size of hash tables used a bit everywhere (the max number of elements
120 // in these hash tables is the number of rows/columns)
121 const int GRID_HASH_SIZE
= 100;
123 // the minimal distance in pixels the mouse needs to move to start a drag
125 const int DRAG_SENSITIVITY
= 3;
127 } // anonymous namespace
129 #include "wx/arrimpl.cpp"
131 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
132 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK
, wxGridEvent
);
139 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK
, wxGridEvent
);
140 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK
, wxGridEvent
);
141 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK
, wxGridEvent
);
142 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG
, wxGridEvent
);
143 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK
, wxGridEvent
);
144 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK
, wxGridEvent
);
145 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK
, wxGridEvent
);
146 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK
, wxGridEvent
);
147 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE
, wxGridSizeEvent
);
148 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE
, wxGridSizeEvent
);
149 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE
, wxGridEvent
);
150 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
151 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
152 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
154 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
155 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
166 // ensure that first is less or equal to second, swapping the values if
168 void EnsureFirstLessThanSecond(int& first
, int& second
)
170 if ( first
> second
)
171 wxSwap(first
, second
);
174 } // anonymous namespace
176 // ============================================================================
178 // ============================================================================
180 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
182 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
183 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler
::OnKillFocus
)
184 EVT_KEY_DOWN( wxGridCellEditorEvtHandler
::OnKeyDown
)
185 EVT_CHAR( wxGridCellEditorEvtHandler
::OnChar
)
188 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
189 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl
::OnClick
)
190 EVT_HEADER_DCLICK(wxID_ANY
, wxGridHeaderCtrl
::OnDoubleClick
)
191 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxGridHeaderCtrl
::OnRightClick
)
193 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl
::OnBeginResize
)
194 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl
::OnResizing
)
195 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl
::OnEndResize
)
197 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl
::OnBeginReorder
)
198 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl
::OnEndReorder
)
201 wxGridOperations
& wxGridRowOperations
::Dual() const
203 static wxGridColumnOperations s_colOper
;
208 wxGridOperations
& wxGridColumnOperations
::Dual() const
210 static wxGridRowOperations s_rowOper
;
215 // ----------------------------------------------------------------------------
216 // wxGridCellWorker is an (almost) empty common base class for
217 // wxGridCellRenderer and wxGridCellEditor managing ref counting
218 // ----------------------------------------------------------------------------
220 void wxGridCellWorker
::SetParameters(const wxString
& WXUNUSED(params
))
225 wxGridCellWorker
::~wxGridCellWorker()
229 // ----------------------------------------------------------------------------
230 // wxGridHeaderLabelsRenderer and related classes
231 // ----------------------------------------------------------------------------
233 void wxGridHeaderLabelsRenderer
::DrawLabel(const wxGrid
& grid
,
235 const wxString
& value
,
239 int textOrientation
) const
241 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
242 dc
.SetTextForeground(grid
.GetLabelTextColour());
243 dc
.SetFont(grid
.GetLabelFont());
244 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
248 void wxGridRowHeaderRendererDefault
::DrawBorder(const wxGrid
& WXUNUSED(grid
),
252 dc
.SetPen(wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
)));
253 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
254 rect
.GetRight(), rect
.GetBottom());
255 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
256 rect
.GetLeft(), rect
.GetBottom());
257 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
258 rect
.GetRight() + 1, rect
.GetBottom());
260 dc
.SetPen(*wxWHITE_PEN
);
261 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
262 rect
.GetLeft() + 1, rect
.GetBottom());
263 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
264 rect
.GetRight(), rect
.GetTop());
269 void wxGridColumnHeaderRendererDefault
::DrawBorder(const wxGrid
& WXUNUSED(grid
),
273 dc
.SetPen(wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
)));
274 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
275 rect
.GetRight(), rect
.GetBottom());
276 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
277 rect
.GetRight(), rect
.GetTop());
278 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
279 rect
.GetRight() + 1, rect
.GetBottom());
281 dc
.SetPen(*wxWHITE_PEN
);
282 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
283 rect
.GetLeft(), rect
.GetBottom());
284 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
285 rect
.GetRight(), rect
.GetTop() + 1);
290 void wxGridCornerHeaderRendererDefault
::DrawBorder(const wxGrid
& WXUNUSED(grid
),
294 dc
.SetPen(wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
)));
295 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
296 rect
.GetRight() - 1, rect
.GetTop());
297 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
298 rect
.GetLeft(), rect
.GetBottom() - 1);
299 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
300 rect
.GetRight(), rect
.GetTop());
301 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
302 rect
.GetLeft(), rect
.GetBottom());
304 dc
.SetPen(*wxWHITE_PEN
);
305 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
306 rect
.GetRight() - 1, rect
.GetTop() + 1);
307 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
308 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
317 void wxGridCellAttr
::Init(wxGridCellAttr
*attrDefault
)
319 m_isReadOnly
= Unset
;
324 m_attrkind
= wxGridCellAttr
::Cell
;
326 m_sizeRows
= m_sizeCols
= 1;
327 m_overflow
= UnsetOverflow
;
329 SetDefAttr(attrDefault
);
332 wxGridCellAttr
*wxGridCellAttr
::Clone() const
334 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
336 if ( HasTextColour() )
337 attr
->SetTextColour(GetTextColour());
338 if ( HasBackgroundColour() )
339 attr
->SetBackgroundColour(GetBackgroundColour());
341 attr
->SetFont(GetFont());
342 if ( HasAlignment() )
343 attr
->SetAlignment(m_hAlign
, m_vAlign
);
345 attr
->SetSize( m_sizeRows
, m_sizeCols
);
349 attr
->SetRenderer(m_renderer
);
350 m_renderer
->IncRef();
354 attr
->SetEditor(m_editor
);
361 attr
->SetOverflow( m_overflow
== Overflow
);
362 attr
->SetKind( m_attrkind
);
367 void wxGridCellAttr
::MergeWith(wxGridCellAttr
*mergefrom
)
369 if ( !HasTextColour() && mergefrom
->HasTextColour() )
370 SetTextColour(mergefrom
->GetTextColour());
371 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
372 SetBackgroundColour(mergefrom
->GetBackgroundColour());
373 if ( !HasFont() && mergefrom
->HasFont() )
374 SetFont(mergefrom
->GetFont());
375 if ( !HasAlignment() && mergefrom
->HasAlignment() )
378 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
379 SetAlignment(hAlign
, vAlign
);
381 if ( !HasSize() && mergefrom
->HasSize() )
382 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
384 // Directly access member functions as GetRender/Editor don't just return
385 // m_renderer/m_editor
387 // Maybe add support for merge of Render and Editor?
388 if (!HasRenderer() && mergefrom
->HasRenderer() )
390 m_renderer
= mergefrom
->m_renderer
;
391 m_renderer
->IncRef();
393 if ( !HasEditor() && mergefrom
->HasEditor() )
395 m_editor
= mergefrom
->m_editor
;
398 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
399 SetReadOnly(mergefrom
->IsReadOnly());
401 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
402 SetOverflow(mergefrom
->GetOverflow());
404 SetDefAttr(mergefrom
->m_defGridAttr
);
407 void wxGridCellAttr
::SetSize(int num_rows
, int num_cols
)
409 // The size of a cell is normally 1,1
411 // If this cell is larger (2,2) then this is the top left cell
412 // the other cells that will be covered (lower right cells) must be
413 // set to negative or zero values such that
414 // row + num_rows of the covered cell points to the larger cell (this cell)
415 // same goes for the col + num_cols.
417 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
419 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
420 !((num_rows
<= 0) && (num_cols
> 0)) ||
421 !((num_rows
== 0) && (num_cols
== 0))),
422 wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values"));
424 m_sizeRows
= num_rows
;
425 m_sizeCols
= num_cols
;
428 const wxColour
& wxGridCellAttr
::GetTextColour() const
434 else if (m_defGridAttr
&& m_defGridAttr
!= this)
436 return m_defGridAttr
->GetTextColour();
440 wxFAIL_MSG(wxT("Missing default cell attribute"));
445 const wxColour
& wxGridCellAttr
::GetBackgroundColour() const
447 if (HasBackgroundColour())
451 else if (m_defGridAttr
&& m_defGridAttr
!= this)
453 return m_defGridAttr
->GetBackgroundColour();
457 wxFAIL_MSG(wxT("Missing default cell attribute"));
462 const wxFont
& wxGridCellAttr
::GetFont() const
468 else if (m_defGridAttr
&& m_defGridAttr
!= this)
470 return m_defGridAttr
->GetFont();
474 wxFAIL_MSG(wxT("Missing default cell attribute"));
479 void wxGridCellAttr
::GetAlignment(int *hAlign
, int *vAlign
) const
488 else if (m_defGridAttr
&& m_defGridAttr
!= this)
490 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
494 wxFAIL_MSG(wxT("Missing default cell attribute"));
498 void wxGridCellAttr
::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
500 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
503 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
507 void wxGridCellAttr
::GetSize( int *num_rows
, int *num_cols
) const
510 *num_rows
= m_sizeRows
;
512 *num_cols
= m_sizeCols
;
515 // GetRenderer and GetEditor use a slightly different decision path about
516 // which attribute to use. If a non-default attr object has one then it is
517 // used, otherwise the default editor or renderer is fetched from the grid and
518 // used. It should be the default for the data type of the cell. If it is
519 // NULL (because the table has a type that the grid does not have in its
520 // registry), then the grid's default editor or renderer is used.
522 wxGridCellRenderer
* wxGridCellAttr
::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
524 wxGridCellRenderer
*renderer
= NULL
;
526 if ( m_renderer
&& this != m_defGridAttr
)
528 // use the cells renderer if it has one
529 renderer
= m_renderer
;
532 else // no non-default cell renderer
534 // get default renderer for the data type
537 // GetDefaultRendererForCell() will do IncRef() for us
538 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
541 if ( renderer
== NULL
)
543 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
545 // if we still don't have one then use the grid default
546 // (no need for IncRef() here neither)
547 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
549 else // default grid attr
551 // use m_renderer which we had decided not to use initially
552 renderer
= m_renderer
;
559 // we're supposed to always find something
560 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
565 // same as above, except for s/renderer/editor/g
566 wxGridCellEditor
* wxGridCellAttr
::GetEditor(const wxGrid
* grid
, int row
, int col
) const
568 wxGridCellEditor
*editor
= NULL
;
570 if ( m_editor
&& this != m_defGridAttr
)
572 // use the cells editor if it has one
576 else // no non default cell editor
578 // get default editor for the data type
581 // GetDefaultEditorForCell() will do IncRef() for us
582 editor
= grid
->GetDefaultEditorForCell(row
, col
);
585 if ( editor
== NULL
)
587 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
589 // if we still don't have one then use the grid default
590 // (no need for IncRef() here neither)
591 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
593 else // default grid attr
595 // use m_editor which we had decided not to use initially
603 // we're supposed to always find something
604 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
609 // ----------------------------------------------------------------------------
610 // wxGridCellAttrData
611 // ----------------------------------------------------------------------------
613 void wxGridCellAttrData
::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
615 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
616 // touch attribute's reference counting explicitly, since this
617 // is managed by class wxGridCellWithAttr
618 int n
= FindIndex(row
, col
);
619 if ( n
== wxNOT_FOUND
)
624 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
626 //else: nothing to do
628 else // we already have an attribute for this cell
632 // change the attribute
633 m_attrs
[(size_t)n
].ChangeAttr(attr
);
637 // remove this attribute
638 m_attrs
.RemoveAt((size_t)n
);
643 wxGridCellAttr
*wxGridCellAttrData
::GetAttr(int row
, int col
) const
645 wxGridCellAttr
*attr
= NULL
;
647 int n
= FindIndex(row
, col
);
648 if ( n
!= wxNOT_FOUND
)
650 attr
= m_attrs
[(size_t)n
].attr
;
657 void wxGridCellAttrData
::UpdateAttrRows( size_t pos
, int numRows
)
659 size_t count
= m_attrs
.GetCount();
660 for ( size_t n
= 0; n
< count
; n
++ )
662 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
663 wxCoord row
= coords
.GetRow();
664 if ((size_t)row
>= pos
)
668 // If rows inserted, include row counter where necessary
669 coords
.SetRow(row
+ numRows
);
671 else if (numRows
< 0)
673 // If rows deleted ...
674 if ((size_t)row
>= pos
- numRows
)
676 // ...either decrement row counter (if row still exists)...
677 coords
.SetRow(row
+ numRows
);
681 // ...or remove the attribute
691 void wxGridCellAttrData
::UpdateAttrCols( size_t pos
, int numCols
)
693 size_t count
= m_attrs
.GetCount();
694 for ( size_t n
= 0; n
< count
; n
++ )
696 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
697 wxCoord col
= coords
.GetCol();
698 if ( (size_t)col
>= pos
)
702 // If rows inserted, include row counter where necessary
703 coords
.SetCol(col
+ numCols
);
705 else if (numCols
< 0)
707 // If rows deleted ...
708 if ((size_t)col
>= pos
- numCols
)
710 // ...either decrement row counter (if row still exists)...
711 coords
.SetCol(col
+ numCols
);
715 // ...or remove the attribute
725 int wxGridCellAttrData
::FindIndex(int row
, int col
) const
727 size_t count
= m_attrs
.GetCount();
728 for ( size_t n
= 0; n
< count
; n
++ )
730 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
731 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
740 // ----------------------------------------------------------------------------
741 // wxGridRowOrColAttrData
742 // ----------------------------------------------------------------------------
744 wxGridRowOrColAttrData
::~wxGridRowOrColAttrData()
746 size_t count
= m_attrs
.GetCount();
747 for ( size_t n
= 0; n
< count
; n
++ )
749 m_attrs
[n
]->DecRef();
753 wxGridCellAttr
*wxGridRowOrColAttrData
::GetAttr(int rowOrCol
) const
755 wxGridCellAttr
*attr
= NULL
;
757 int n
= m_rowsOrCols
.Index(rowOrCol
);
758 if ( n
!= wxNOT_FOUND
)
760 attr
= m_attrs
[(size_t)n
];
767 void wxGridRowOrColAttrData
::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
769 int i
= m_rowsOrCols
.Index(rowOrCol
);
770 if ( i
== wxNOT_FOUND
)
774 // store the new attribute, taking its ownership
775 m_rowsOrCols
.Add(rowOrCol
);
780 else // we have an attribute for this row or column
782 size_t n
= (size_t)i
;
784 // notice that this code works correctly even when the old attribute is
785 // the same as the new one: as we own of it, we must call DecRef() on
786 // it in any case and this won't result in destruction of the new
787 // attribute if it's the same as old one because it must have ref count
788 // of at least 2 to be passed to us while we keep a reference to it too
789 m_attrs
[n
]->DecRef();
793 // replace the attribute with the new one
796 else // remove the attribute
798 m_rowsOrCols
.RemoveAt(n
);
804 void wxGridRowOrColAttrData
::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
806 size_t count
= m_attrs
.GetCount();
807 for ( size_t n
= 0; n
< count
; n
++ )
809 int & rowOrCol
= m_rowsOrCols
[n
];
810 if ( (size_t)rowOrCol
>= pos
)
812 if ( numRowsOrCols
> 0 )
814 // If rows inserted, include row counter where necessary
815 rowOrCol
+= numRowsOrCols
;
817 else if ( numRowsOrCols
< 0)
819 // If rows deleted, either decrement row counter (if row still exists)
820 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
821 rowOrCol
+= numRowsOrCols
;
824 m_rowsOrCols
.RemoveAt(n
);
825 m_attrs
[n
]->DecRef();
835 // ----------------------------------------------------------------------------
836 // wxGridCellAttrProvider
837 // ----------------------------------------------------------------------------
839 wxGridCellAttrProvider
::wxGridCellAttrProvider()
844 wxGridCellAttrProvider
::~wxGridCellAttrProvider()
849 void wxGridCellAttrProvider
::InitData()
851 m_data
= new wxGridCellAttrProviderData
;
854 wxGridCellAttr
*wxGridCellAttrProvider
::GetAttr(int row
, int col
,
855 wxGridCellAttr
::wxAttrKind kind
) const
857 wxGridCellAttr
*attr
= NULL
;
862 case (wxGridCellAttr
::Any
):
863 // Get cached merge attributes.
864 // Currently not used as no cache implemented as not mutable
865 // attr = m_data->m_mergeAttr.GetAttr(row, col);
868 // Basically implement old version.
869 // Also check merge cache, so we don't have to re-merge every time..
870 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
871 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
872 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
874 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
876 // Two or more are non NULL
877 attr
= new wxGridCellAttr
;
878 attr
->SetKind(wxGridCellAttr
::Merged
);
880 // Order is important..
883 attr
->MergeWith(attrcell
);
888 attr
->MergeWith(attrcol
);
893 attr
->MergeWith(attrrow
);
897 // store merge attr if cache implemented
899 //m_data->m_mergeAttr.SetAttr(attr, row, col);
903 // one or none is non null return it or null.
922 case (wxGridCellAttr
::Cell
):
923 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
926 case (wxGridCellAttr
::Col
):
927 attr
= m_data
->m_colAttrs
.GetAttr(col
);
930 case (wxGridCellAttr
::Row
):
931 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
936 // (wxGridCellAttr::Default):
937 // (wxGridCellAttr::Merged):
945 void wxGridCellAttrProvider
::SetAttr(wxGridCellAttr
*attr
,
951 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
954 void wxGridCellAttrProvider
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
959 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
962 void wxGridCellAttrProvider
::SetColAttr(wxGridCellAttr
*attr
, int col
)
967 m_data
->m_colAttrs
.SetAttr(attr
, col
);
970 void wxGridCellAttrProvider
::UpdateAttrRows( size_t pos
, int numRows
)
974 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
976 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
980 void wxGridCellAttrProvider
::UpdateAttrCols( size_t pos
, int numCols
)
984 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
986 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
990 const wxGridColumnHeaderRenderer
&
991 wxGridCellAttrProvider
::GetColumnHeaderRenderer(int WXUNUSED(col
))
993 return gs_defaultHeaderRenderers
.colRenderer
;
996 const wxGridRowHeaderRenderer
&
997 wxGridCellAttrProvider
::GetRowHeaderRenderer(int WXUNUSED(row
))
999 return gs_defaultHeaderRenderers
.rowRenderer
;
1002 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider
::GetCornerRenderer()
1004 return gs_defaultHeaderRenderers
.cornerRenderer
;
1007 // ----------------------------------------------------------------------------
1009 // ----------------------------------------------------------------------------
1011 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1013 wxGridTableBase
::wxGridTableBase()
1016 m_attrProvider
= NULL
;
1019 wxGridTableBase
::~wxGridTableBase()
1021 delete m_attrProvider
;
1024 void wxGridTableBase
::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1026 delete m_attrProvider
;
1027 m_attrProvider
= attrProvider
;
1030 bool wxGridTableBase
::CanHaveAttributes()
1032 if ( ! GetAttrProvider() )
1034 // use the default attr provider by default
1035 SetAttrProvider(new wxGridCellAttrProvider
);
1041 wxGridCellAttr
*wxGridTableBase
::GetAttr(int row
, int col
, wxGridCellAttr
::wxAttrKind kind
)
1043 if ( m_attrProvider
)
1044 return m_attrProvider
->GetAttr(row
, col
, kind
);
1049 void wxGridTableBase
::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1051 if ( m_attrProvider
)
1054 attr
->SetKind(wxGridCellAttr
::Cell
);
1055 m_attrProvider
->SetAttr(attr
, row
, col
);
1059 // as we take ownership of the pointer and don't store it, we must
1065 void wxGridTableBase
::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1067 if ( m_attrProvider
)
1069 attr
->SetKind(wxGridCellAttr
::Row
);
1070 m_attrProvider
->SetRowAttr(attr
, row
);
1074 // as we take ownership of the pointer and don't store it, we must
1080 void wxGridTableBase
::SetColAttr(wxGridCellAttr
*attr
, int col
)
1082 if ( m_attrProvider
)
1084 attr
->SetKind(wxGridCellAttr
::Col
);
1085 m_attrProvider
->SetColAttr(attr
, col
);
1089 // as we take ownership of the pointer and don't store it, we must
1095 bool wxGridTableBase
::InsertRows( size_t WXUNUSED(pos
),
1096 size_t WXUNUSED(numRows
) )
1098 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1103 bool wxGridTableBase
::AppendRows( size_t WXUNUSED(numRows
) )
1105 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1110 bool wxGridTableBase
::DeleteRows( size_t WXUNUSED(pos
),
1111 size_t WXUNUSED(numRows
) )
1113 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1118 bool wxGridTableBase
::InsertCols( size_t WXUNUSED(pos
),
1119 size_t WXUNUSED(numCols
) )
1121 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1126 bool wxGridTableBase
::AppendCols( size_t WXUNUSED(numCols
) )
1128 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1133 bool wxGridTableBase
::DeleteCols( size_t WXUNUSED(pos
),
1134 size_t WXUNUSED(numCols
) )
1136 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1141 wxString wxGridTableBase
::GetRowLabelValue( int row
)
1145 // RD: Starting the rows at zero confuses users,
1146 // no matter how much it makes sense to us geeks.
1152 wxString wxGridTableBase
::GetColLabelValue( int col
)
1154 // default col labels are:
1155 // cols 0 to 25 : A-Z
1156 // cols 26 to 675 : AA-ZZ
1161 for ( n
= 1; ; n
++ )
1163 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1169 // reverse the string...
1171 for ( i
= 0; i
< n
; i
++ )
1179 wxString wxGridTableBase
::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1181 return wxGRID_VALUE_STRING
;
1184 bool wxGridTableBase
::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1185 const wxString
& typeName
)
1187 return typeName
== wxGRID_VALUE_STRING
;
1190 bool wxGridTableBase
::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1192 return CanGetValueAs(row
, col
, typeName
);
1195 long wxGridTableBase
::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1200 double wxGridTableBase
::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1205 bool wxGridTableBase
::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1210 void wxGridTableBase
::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1211 long WXUNUSED(value
) )
1215 void wxGridTableBase
::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1216 double WXUNUSED(value
) )
1220 void wxGridTableBase
::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1221 bool WXUNUSED(value
) )
1225 void* wxGridTableBase
::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1226 const wxString
& WXUNUSED(typeName
) )
1231 void wxGridTableBase
::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1232 const wxString
& WXUNUSED(typeName
),
1233 void* WXUNUSED(value
) )
1237 //////////////////////////////////////////////////////////////////////
1239 // Message class for the grid table to send requests and notifications
1243 wxGridTableMessage
::wxGridTableMessage()
1251 wxGridTableMessage
::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1252 int commandInt1
, int commandInt2
)
1256 m_comInt1
= commandInt1
;
1257 m_comInt2
= commandInt2
;
1260 //////////////////////////////////////////////////////////////////////
1262 // A basic grid table for string data. An object of this class will
1263 // created by wxGrid if you don't specify an alternative table class.
1266 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1268 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1270 wxGridStringTable
::wxGridStringTable()
1276 wxGridStringTable
::wxGridStringTable( int numRows
, int numCols
)
1279 m_numCols
= numCols
;
1281 m_data
.Alloc( numRows
);
1284 sa
.Alloc( numCols
);
1285 sa
.Add( wxEmptyString
, numCols
);
1287 m_data
.Add( sa
, numRows
);
1290 wxString wxGridStringTable
::GetValue( int row
, int col
)
1292 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1293 (col
>= 0 && col
< GetNumberCols()),
1295 wxT("invalid row or column index in wxGridStringTable") );
1297 return m_data
[row
][col
];
1300 void wxGridStringTable
::SetValue( int row
, int col
, const wxString
& value
)
1302 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1303 (col
>= 0 && col
< GetNumberCols()),
1304 wxT("invalid row or column index in wxGridStringTable") );
1306 m_data
[row
][col
] = value
;
1309 void wxGridStringTable
::Clear()
1312 int numRows
, numCols
;
1314 numRows
= m_data
.GetCount();
1317 numCols
= m_data
[0].GetCount();
1319 for ( row
= 0; row
< numRows
; row
++ )
1321 for ( col
= 0; col
< numCols
; col
++ )
1323 m_data
[row
][col
] = wxEmptyString
;
1329 bool wxGridStringTable
::InsertRows( size_t pos
, size_t numRows
)
1331 size_t curNumRows
= m_data
.GetCount();
1332 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1333 ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
1335 if ( pos
>= curNumRows
)
1337 return AppendRows( numRows
);
1341 sa
.Alloc( curNumCols
);
1342 sa
.Add( wxEmptyString
, curNumCols
);
1343 m_data
.Insert( sa
, pos
, numRows
);
1347 wxGridTableMessage
msg( this,
1348 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1352 GetView()->ProcessTableMessage( msg
);
1358 bool wxGridStringTable
::AppendRows( size_t numRows
)
1360 size_t curNumRows
= m_data
.GetCount();
1361 size_t curNumCols
= ( curNumRows
> 0
1362 ? m_data
[0].GetCount()
1363 : ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
1366 if ( curNumCols
> 0 )
1368 sa
.Alloc( curNumCols
);
1369 sa
.Add( wxEmptyString
, curNumCols
);
1372 m_data
.Add( sa
, numRows
);
1376 wxGridTableMessage
msg( this,
1377 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1380 GetView()->ProcessTableMessage( msg
);
1386 bool wxGridStringTable
::DeleteRows( size_t pos
, size_t numRows
)
1388 size_t curNumRows
= m_data
.GetCount();
1390 if ( pos
>= curNumRows
)
1392 wxFAIL_MSG( wxString
::Format
1394 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1396 (unsigned long)numRows
,
1397 (unsigned long)curNumRows
1403 if ( numRows
> curNumRows
- pos
)
1405 numRows
= curNumRows
- pos
;
1408 if ( numRows
>= curNumRows
)
1414 m_data
.RemoveAt( pos
, numRows
);
1419 wxGridTableMessage
msg( this,
1420 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1424 GetView()->ProcessTableMessage( msg
);
1430 bool wxGridStringTable
::InsertCols( size_t pos
, size_t numCols
)
1434 size_t curNumRows
= m_data
.GetCount();
1435 size_t curNumCols
= ( curNumRows
> 0
1436 ? m_data
[0].GetCount()
1437 : ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
1439 if ( pos
>= curNumCols
)
1441 return AppendCols( numCols
);
1444 if ( !m_colLabels
.IsEmpty() )
1446 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1449 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1450 m_colLabels
[i
] = wxGridTableBase
::GetColLabelValue( i
);
1453 for ( row
= 0; row
< curNumRows
; row
++ )
1455 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1457 m_data
[row
].Insert( wxEmptyString
, col
);
1461 m_numCols
+= numCols
;
1465 wxGridTableMessage
msg( this,
1466 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1470 GetView()->ProcessTableMessage( msg
);
1476 bool wxGridStringTable
::AppendCols( size_t numCols
)
1480 size_t curNumRows
= m_data
.GetCount();
1482 for ( row
= 0; row
< curNumRows
; row
++ )
1484 m_data
[row
].Add( wxEmptyString
, numCols
);
1487 m_numCols
+= numCols
;
1491 wxGridTableMessage
msg( this,
1492 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1495 GetView()->ProcessTableMessage( msg
);
1501 bool wxGridStringTable
::DeleteCols( size_t pos
, size_t numCols
)
1505 size_t curNumRows
= m_data
.GetCount();
1506 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1507 ( GetView() ?
GetView()->GetNumberCols() : 0 ) );
1509 if ( pos
>= curNumCols
)
1511 wxFAIL_MSG( wxString
::Format
1513 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1515 (unsigned long)numCols
,
1516 (unsigned long)curNumCols
1523 colID
= GetView()->GetColAt( pos
);
1527 if ( numCols
> curNumCols
- colID
)
1529 numCols
= curNumCols
- colID
;
1532 if ( !m_colLabels
.IsEmpty() )
1534 // m_colLabels stores just as many elements as it needs, e.g. if only
1535 // the label of the first column had been set it would have only one
1536 // element and not numCols, so account for it
1537 int numRemaining
= m_colLabels
.size() - colID
;
1538 if (numRemaining
> 0)
1539 m_colLabels
.RemoveAt( colID
, wxMin(numCols
, numRemaining
) );
1542 if ( numCols
>= curNumCols
)
1544 for ( row
= 0; row
< curNumRows
; row
++ )
1546 m_data
[row
].Clear();
1551 else // something will be left
1553 for ( row
= 0; row
< curNumRows
; row
++ )
1555 m_data
[row
].RemoveAt( colID
, numCols
);
1558 m_numCols
-= numCols
;
1563 wxGridTableMessage
msg( this,
1564 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1568 GetView()->ProcessTableMessage( msg
);
1574 wxString wxGridStringTable
::GetRowLabelValue( int row
)
1576 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1578 // using default label
1580 return wxGridTableBase
::GetRowLabelValue( row
);
1584 return m_rowLabels
[row
];
1588 wxString wxGridStringTable
::GetColLabelValue( int col
)
1590 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1592 // using default label
1594 return wxGridTableBase
::GetColLabelValue( col
);
1598 return m_colLabels
[col
];
1602 void wxGridStringTable
::SetRowLabelValue( int row
, const wxString
& value
)
1604 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1606 int n
= m_rowLabels
.GetCount();
1609 for ( i
= n
; i
<= row
; i
++ )
1611 m_rowLabels
.Add( wxGridTableBase
::GetRowLabelValue(i
) );
1615 m_rowLabels
[row
] = value
;
1618 void wxGridStringTable
::SetColLabelValue( int col
, const wxString
& value
)
1620 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1622 int n
= m_colLabels
.GetCount();
1625 for ( i
= n
; i
<= col
; i
++ )
1627 m_colLabels
.Add( wxGridTableBase
::GetColLabelValue(i
) );
1631 m_colLabels
[col
] = value
;
1635 //////////////////////////////////////////////////////////////////////
1636 //////////////////////////////////////////////////////////////////////
1638 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1639 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow
::OnMouseCaptureLost
)
1642 void wxGridSubwindow
::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1644 m_owner
->CancelMouseCapture();
1647 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1648 EVT_PAINT( wxGridRowLabelWindow
::OnPaint
)
1649 EVT_MOUSEWHEEL( wxGridRowLabelWindow
::OnMouseWheel
)
1650 EVT_MOUSE_EVENTS( wxGridRowLabelWindow
::OnMouseEvent
)
1653 void wxGridRowLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1657 // NO - don't do this because it will set both the x and y origin
1658 // coords to match the parent scrolled window and we just want to
1659 // set the y coord - MB
1661 // m_owner->PrepareDC( dc );
1664 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1665 wxPoint pt
= dc
.GetDeviceOrigin();
1666 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1668 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1669 m_owner
->DrawRowLabels( dc
, rows
);
1672 void wxGridRowLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1674 m_owner
->ProcessRowLabelMouseEvent( event
);
1677 void wxGridRowLabelWindow
::OnMouseWheel( wxMouseEvent
& event
)
1679 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1683 //////////////////////////////////////////////////////////////////////
1685 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1686 EVT_PAINT( wxGridColLabelWindow
::OnPaint
)
1687 EVT_MOUSEWHEEL( wxGridColLabelWindow
::OnMouseWheel
)
1688 EVT_MOUSE_EVENTS( wxGridColLabelWindow
::OnMouseEvent
)
1691 void wxGridColLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1695 // NO - don't do this because it will set both the x and y origin
1696 // coords to match the parent scrolled window and we just want to
1697 // set the x coord - MB
1699 // m_owner->PrepareDC( dc );
1702 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1703 wxPoint pt
= dc
.GetDeviceOrigin();
1704 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1705 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1707 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1709 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1710 m_owner
->DrawColLabels( dc
, cols
);
1713 void wxGridColLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1715 m_owner
->ProcessColLabelMouseEvent( event
);
1718 void wxGridColLabelWindow
::OnMouseWheel( wxMouseEvent
& event
)
1720 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1724 //////////////////////////////////////////////////////////////////////
1726 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1727 EVT_MOUSEWHEEL( wxGridCornerLabelWindow
::OnMouseWheel
)
1728 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow
::OnMouseEvent
)
1729 EVT_PAINT( wxGridCornerLabelWindow
::OnPaint
)
1732 void wxGridCornerLabelWindow
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1736 m_owner
->DrawCornerLabel(dc
);
1739 void wxGridCornerLabelWindow
::OnMouseEvent( wxMouseEvent
& event
)
1741 m_owner
->ProcessCornerLabelMouseEvent( event
);
1744 void wxGridCornerLabelWindow
::OnMouseWheel( wxMouseEvent
& event
)
1746 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1750 //////////////////////////////////////////////////////////////////////
1752 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1753 EVT_PAINT( wxGridWindow
::OnPaint
)
1754 EVT_MOUSEWHEEL( wxGridWindow
::OnMouseWheel
)
1755 EVT_MOUSE_EVENTS( wxGridWindow
::OnMouseEvent
)
1756 EVT_KEY_DOWN( wxGridWindow
::OnKeyDown
)
1757 EVT_KEY_UP( wxGridWindow
::OnKeyUp
)
1758 EVT_CHAR( wxGridWindow
::OnChar
)
1759 EVT_SET_FOCUS( wxGridWindow
::OnFocus
)
1760 EVT_KILL_FOCUS( wxGridWindow
::OnFocus
)
1761 EVT_ERASE_BACKGROUND( wxGridWindow
::OnEraseBackground
)
1764 void wxGridWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1766 wxPaintDC
dc( this );
1767 m_owner
->PrepareDC( dc
);
1768 wxRegion reg
= GetUpdateRegion();
1769 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1770 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1772 m_owner
->DrawGridSpace( dc
);
1774 m_owner
->DrawAllGridLines( dc
, reg
);
1776 m_owner
->DrawHighlight( dc
, dirtyCells
);
1779 void wxGridWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1781 wxWindow
::ScrollWindow( dx
, dy
, rect
);
1782 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1783 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1786 void wxGridWindow
::OnMouseEvent( wxMouseEvent
& event
)
1788 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1791 m_owner
->ProcessGridCellMouseEvent( event
);
1794 void wxGridWindow
::OnMouseWheel( wxMouseEvent
& event
)
1796 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1800 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1801 // cursor must be in the cell edit control to get key events
1803 void wxGridWindow
::OnKeyDown( wxKeyEvent
& event
)
1805 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1809 void wxGridWindow
::OnKeyUp( wxKeyEvent
& event
)
1811 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1815 void wxGridWindow
::OnChar( wxKeyEvent
& event
)
1817 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1821 void wxGridWindow
::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1825 void wxGridWindow
::OnFocus(wxFocusEvent
& event
)
1827 // and if we have any selection, it has to be repainted, because it
1828 // uses different colour when the grid is not focused:
1829 if ( m_owner
->IsSelection() )
1835 // NB: Note that this code is in "else" branch only because the other
1836 // branch refreshes everything and so there's no point in calling
1837 // Refresh() again, *not* because it should only be done if
1838 // !IsSelection(). If the above code is ever optimized to refresh
1839 // only selected area, this needs to be moved out of the "else"
1840 // branch so that it's always executed.
1842 // current cell cursor {dis,re}appears on focus change:
1843 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1844 m_owner
->GetGridCursorCol());
1845 const wxRect cursor
=
1846 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1847 Refresh(true, &cursor
);
1850 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1854 #define internalXToCol(x) XToCol(x, true)
1855 #define internalYToRow(y) YToRow(y, true)
1857 /////////////////////////////////////////////////////////////////////
1859 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1860 EVT_PAINT( wxGrid
::OnPaint
)
1861 EVT_SIZE( wxGrid
::OnSize
)
1862 EVT_KEY_DOWN( wxGrid
::OnKeyDown
)
1863 EVT_KEY_UP( wxGrid
::OnKeyUp
)
1864 EVT_CHAR ( wxGrid
::OnChar
)
1865 EVT_ERASE_BACKGROUND( wxGrid
::OnEraseBackground
)
1868 bool wxGrid
::Create(wxWindow
*parent
, wxWindowID id
,
1869 const wxPoint
& pos
, const wxSize
& size
,
1870 long style
, const wxString
& name
)
1872 if (!wxScrolledWindow
::Create(parent
, id
, pos
, size
,
1873 style
| wxWANTS_CHARS
, name
))
1876 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1877 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1880 SetInitialSize(size
);
1889 m_winCapture
->ReleaseMouse();
1891 // Ensure that the editor control is destroyed before the grid is,
1892 // otherwise we crash later when the editor tries to do something with the
1893 // half destroyed grid
1894 HideCellEditControl();
1896 // Must do this or ~wxScrollHelper will pop the wrong event handler
1897 SetTargetWindow(this);
1899 wxSafeDecRef(m_defaultCellAttr
);
1901 #ifdef DEBUG_ATTR_CACHE
1902 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1903 wxPrintf(wxT("wxGrid attribute cache statistics: "
1904 "total: %u, hits: %u (%u%%)\n"),
1905 total
, gs_nAttrCacheHits
,
1906 total ?
(gs_nAttrCacheHits
*100) / total
: 0);
1909 // if we own the table, just delete it, otherwise at least don't leave it
1910 // with dangling view pointer
1913 else if ( m_table
&& m_table
->GetView() == this )
1914 m_table
->SetView(NULL
);
1916 delete m_typeRegistry
;
1919 delete m_setFixedRows
;
1920 delete m_setFixedCols
;
1924 // ----- internal init and update functions
1927 // NOTE: If using the default visual attributes works everywhere then this can
1928 // be removed as well as the #else cases below.
1929 #define _USE_VISATTR 0
1931 void wxGrid
::Create()
1933 // create the type registry
1934 m_typeRegistry
= new wxGridTypeRegistry
;
1936 m_cellEditCtrlEnabled
= false;
1938 m_defaultCellAttr
= new wxGridCellAttr();
1940 // Set default cell attributes
1941 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1942 m_defaultCellAttr
->SetKind(wxGridCellAttr
::Default
);
1943 m_defaultCellAttr
->SetFont(GetFont());
1944 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1945 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1946 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1949 wxVisualAttributes gva
= wxListBox
::GetClassDefaultAttributes();
1950 wxVisualAttributes lva
= wxPanel
::GetClassDefaultAttributes();
1952 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1953 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1956 m_defaultCellAttr
->SetTextColour(
1957 wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1958 m_defaultCellAttr
->SetBackgroundColour(
1959 wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOW
));
1964 m_currentCellCoords
= wxGridNoCellCoords
;
1966 // subwindow components that make up the wxGrid
1967 m_rowLabelWin
= new wxGridRowLabelWindow(this);
1968 CreateColumnWindow();
1969 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
1970 m_gridWin
= new wxGridWindow( this );
1972 SetTargetWindow( m_gridWin
);
1975 wxColour gfg
= gva
.colFg
;
1976 wxColour gbg
= gva
.colBg
;
1977 wxColour lfg
= lva
.colFg
;
1978 wxColour lbg
= lva
.colBg
;
1980 wxColour gfg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1981 wxColour gbg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOW
);
1982 wxColour lfg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1983 wxColour lbg
= wxSystemSettings
::GetColour( wxSYS_COLOUR_BTNFACE
);
1986 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
1987 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
1988 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
1989 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
1990 m_colWindow
->SetOwnForegroundColour(lfg
);
1991 m_colWindow
->SetOwnBackgroundColour(lbg
);
1993 m_gridWin
->SetOwnForegroundColour(gfg
);
1994 m_gridWin
->SetOwnBackgroundColour(gbg
);
1996 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1997 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
1999 // now that we have the grid window, use its font to compute the default
2001 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2002 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2003 m_defaultRowHeight
+= 8;
2005 m_defaultRowHeight
+= 4;
2010 void wxGrid
::CreateColumnWindow()
2012 if ( m_useNativeHeader
)
2014 m_colWindow
= new wxGridHeaderCtrl(this);
2015 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2017 else // draw labels ourselves
2019 m_colWindow
= new wxGridColLabelWindow(this);
2020 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2024 bool wxGrid
::CreateGrid( int numRows
, int numCols
,
2025 wxGridSelectionModes selmode
)
2027 wxCHECK_MSG( !m_created
,
2029 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2031 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2034 void wxGrid
::SetSelectionMode(wxGridSelectionModes selmode
)
2036 wxCHECK_RET( m_created
,
2037 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2039 m_selection
->SetSelectionMode( selmode
);
2042 wxGrid
::wxGridSelectionModes wxGrid
::GetSelectionMode() const
2044 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2045 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2047 return m_selection
->GetSelectionMode();
2051 wxGrid
::SetTable(wxGridTableBase
*table
,
2053 wxGrid
::wxGridSelectionModes selmode
)
2055 bool checkSelection
= false;
2058 // stop all processing
2063 m_table
->SetView(0);
2069 wxDELETE(m_selection
);
2074 checkSelection
= true;
2076 // kill row and column size arrays
2077 m_colWidths
.Empty();
2078 m_colRights
.Empty();
2079 m_rowHeights
.Empty();
2080 m_rowBottoms
.Empty();
2085 m_numRows
= table
->GetNumberRows();
2086 m_numCols
= table
->GetNumberCols();
2088 if ( m_useNativeHeader
)
2089 GetGridColHeader()->SetColumnCount(m_numCols
);
2092 m_table
->SetView( this );
2093 m_ownTable
= takeOwnership
;
2094 m_selection
= new wxGridSelection( this, selmode
);
2097 // If the newly set table is smaller than the
2098 // original one current cell and selection regions
2099 // might be invalid,
2100 m_selectedBlockCorner
= wxGridNoCellCoords
;
2101 m_currentCellCoords
=
2102 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2103 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2104 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2105 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2107 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2108 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2111 m_selectedBlockBottomRight
=
2112 wxGridCellCoords(wxMin(m_numRows
,
2113 m_selectedBlockBottomRight
.GetRow()),
2115 m_selectedBlockBottomRight
.GetCol()));
2129 m_cornerLabelWin
= NULL
;
2130 m_rowLabelWin
= NULL
;
2138 m_defaultCellAttr
= NULL
;
2139 m_typeRegistry
= NULL
;
2140 m_winCapture
= NULL
;
2142 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2143 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2146 m_setFixedCols
= NULL
;
2149 m_attrCache
.row
= -1;
2150 m_attrCache
.col
= -1;
2151 m_attrCache
.attr
= NULL
;
2153 m_labelFont
= GetFont();
2154 m_labelFont
.SetWeight( wxBOLD
);
2156 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2157 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2159 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2160 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2161 m_colLabelTextOrientation
= wxHORIZONTAL
;
2163 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2164 m_defaultRowHeight
= 0; // this will be initialized after creation
2166 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2167 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2169 m_gridLineColour
= wxColour( 192,192,192 );
2170 m_gridLinesEnabled
= true;
2171 m_gridLinesClipHorz
=
2172 m_gridLinesClipVert
= true;
2173 m_cellHighlightColour
= *wxBLACK
;
2174 m_cellHighlightPenWidth
= 2;
2175 m_cellHighlightROPenWidth
= 1;
2177 m_canDragColMove
= false;
2179 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2180 m_winCapture
= NULL
;
2181 m_canDragRowSize
= true;
2182 m_canDragColSize
= true;
2183 m_canDragGridSize
= true;
2184 m_canDragCell
= false;
2186 m_dragRowOrCol
= -1;
2187 m_isDragging
= false;
2188 m_startDragPos
= wxDefaultPosition
;
2190 m_sortCol
= wxNOT_FOUND
;
2191 m_sortIsAscending
= true;
2194 m_nativeColumnLabels
= false;
2196 m_waitForSlowClick
= false;
2198 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2199 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2201 m_currentCellCoords
= wxGridNoCellCoords
;
2203 m_selectedBlockTopLeft
=
2204 m_selectedBlockBottomRight
=
2205 m_selectedBlockCorner
= wxGridNoCellCoords
;
2207 m_selectionBackground
= wxSystemSettings
::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2208 m_selectionForeground
= wxSystemSettings
::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2210 m_editable
= true; // default for whole grid
2212 m_inOnKeyDown
= false;
2218 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2219 // we don't need to call it neither as the scroll position is (0, 0) right
2220 // now anyhow, so just set the parameters directly
2221 m_xScrollPixelsPerLine
= GRID_SCROLL_LINE_X
;
2222 m_yScrollPixelsPerLine
= GRID_SCROLL_LINE_Y
;
2225 // ----------------------------------------------------------------------------
2226 // the idea is to call these functions only when necessary because they create
2227 // quite big arrays which eat memory mostly unnecessary - in particular, if
2228 // default widths/heights are used for all rows/columns, we may not use these
2231 // with some extra code, it should be possible to only store the widths/heights
2232 // different from default ones (resulting in space savings for huge grids) but
2233 // this is not done currently
2234 // ----------------------------------------------------------------------------
2236 void wxGrid
::InitRowHeights()
2238 m_rowHeights
.Empty();
2239 m_rowBottoms
.Empty();
2241 m_rowHeights
.Alloc( m_numRows
);
2242 m_rowBottoms
.Alloc( m_numRows
);
2244 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2247 for ( int i
= 0; i
< m_numRows
; i
++ )
2249 rowBottom
+= m_defaultRowHeight
;
2250 m_rowBottoms
.Add( rowBottom
);
2254 void wxGrid
::InitColWidths()
2256 m_colWidths
.Empty();
2257 m_colRights
.Empty();
2259 m_colWidths
.Alloc( m_numCols
);
2260 m_colRights
.Alloc( m_numCols
);
2262 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2264 for ( int i
= 0; i
< m_numCols
; i
++ )
2266 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2267 m_colRights
.Add( colRight
);
2271 int wxGrid
::GetColWidth(int col
) const
2273 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2276 int wxGrid
::GetColLeft(int col
) const
2278 return m_colRights
.IsEmpty() ?
GetColPos( col
) * m_defaultColWidth
2279 : m_colRights
[col
] - m_colWidths
[col
];
2282 int wxGrid
::GetColRight(int col
) const
2284 return m_colRights
.IsEmpty() ?
(GetColPos( col
) + 1) * m_defaultColWidth
2288 int wxGrid
::GetRowHeight(int row
) const
2290 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2293 int wxGrid
::GetRowTop(int row
) const
2295 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2296 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2299 int wxGrid
::GetRowBottom(int row
) const
2301 return m_rowBottoms
.IsEmpty() ?
(row
+ 1) * m_defaultRowHeight
2302 : m_rowBottoms
[row
];
2305 void wxGrid
::CalcDimensions()
2307 // compute the size of the scrollable area
2308 int w
= m_numCols
> 0 ?
GetColRight(GetColAt(m_numCols
- 1)) : 0;
2309 int h
= m_numRows
> 0 ?
GetRowBottom(m_numRows
- 1) : 0;
2314 // take into account editor if shown
2315 if ( IsCellEditControlShown() )
2318 int r
= m_currentCellCoords
.GetRow();
2319 int c
= m_currentCellCoords
.GetCol();
2320 int x
= GetColLeft(c
);
2321 int y
= GetRowTop(r
);
2323 // how big is the editor
2324 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2325 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2326 editor
->GetControl()->GetSize(&w2
, &h2
);
2337 // preserve (more or less) the previous position
2339 GetViewStart( &x
, &y
);
2341 // ensure the position is valid for the new scroll ranges
2343 x
= wxMax( w
- 1, 0 );
2345 y
= wxMax( h
- 1, 0 );
2347 // update the virtual size and refresh the scrollbars to reflect it
2348 m_gridWin
->SetVirtualSize(w
, h
);
2352 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2353 // still must reposition the children
2357 wxSize wxGrid
::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2359 wxSize
sizeGridWin(size
);
2360 sizeGridWin
.x
-= m_rowLabelWidth
;
2361 sizeGridWin
.y
-= m_colLabelHeight
;
2366 void wxGrid
::CalcWindowSizes()
2368 // escape if the window is has not been fully created yet
2370 if ( m_cornerLabelWin
== NULL
)
2374 GetClientSize( &cw
, &ch
);
2376 // the grid may be too small to have enough space for the labels yet, don't
2377 // size the windows to negative sizes in this case
2378 int gw
= cw
- m_rowLabelWidth
;
2379 int gh
= ch
- m_colLabelHeight
;
2385 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2386 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2388 if ( m_colWindow
&& m_colWindow
->IsShown() )
2389 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2391 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2392 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2394 if ( m_gridWin
&& m_gridWin
->IsShown() )
2395 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2398 // this is called when the grid table sends a message
2399 // to indicate that it has been redimensioned
2401 bool wxGrid
::Redimension( wxGridTableMessage
& msg
)
2404 bool result
= false;
2406 // Clear the attribute cache as the attribute might refer to a different
2407 // cell than stored in the cache after adding/removing rows/columns.
2410 // By the same reasoning, the editor should be dismissed if columns are
2411 // added or removed. And for consistency, it should IMHO always be
2412 // removed, not only if the cell "underneath" it actually changes.
2413 // For now, I intentionally do not save the editor's content as the
2414 // cell it might want to save that stuff to might no longer exist.
2415 HideCellEditControl();
2417 switch ( msg
.GetId() )
2419 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2421 size_t pos
= msg
.GetCommandInt();
2422 int numRows
= msg
.GetCommandInt2();
2424 m_numRows
+= numRows
;
2426 if ( !m_rowHeights
.IsEmpty() )
2428 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2429 m_rowBottoms
.Insert( 0, pos
, numRows
);
2433 bottom
= m_rowBottoms
[pos
- 1];
2435 for ( i
= pos
; i
< m_numRows
; i
++ )
2437 bottom
+= m_rowHeights
[i
];
2438 m_rowBottoms
[i
] = bottom
;
2442 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2444 // if we have just inserted cols into an empty grid the current
2445 // cell will be undefined...
2447 SetCurrentCell( 0, 0 );
2451 m_selection
->UpdateRows( pos
, numRows
);
2452 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2454 attrProvider
->UpdateAttrRows( pos
, numRows
);
2456 if ( !GetBatchCount() )
2459 m_rowLabelWin
->Refresh();
2465 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2467 int numRows
= msg
.GetCommandInt();
2468 int oldNumRows
= m_numRows
;
2469 m_numRows
+= numRows
;
2471 if ( !m_rowHeights
.IsEmpty() )
2473 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2474 m_rowBottoms
.Add( 0, numRows
);
2477 if ( oldNumRows
> 0 )
2478 bottom
= m_rowBottoms
[oldNumRows
- 1];
2480 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2482 bottom
+= m_rowHeights
[i
];
2483 m_rowBottoms
[i
] = bottom
;
2487 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2489 // if we have just inserted cols into an empty grid the current
2490 // cell will be undefined...
2492 SetCurrentCell( 0, 0 );
2495 if ( !GetBatchCount() )
2498 m_rowLabelWin
->Refresh();
2504 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2506 size_t pos
= msg
.GetCommandInt();
2507 int numRows
= msg
.GetCommandInt2();
2508 m_numRows
-= numRows
;
2510 if ( !m_rowHeights
.IsEmpty() )
2512 m_rowHeights
.RemoveAt( pos
, numRows
);
2513 m_rowBottoms
.RemoveAt( pos
, numRows
);
2516 for ( i
= 0; i
< m_numRows
; i
++ )
2518 h
+= m_rowHeights
[i
];
2519 m_rowBottoms
[i
] = h
;
2525 m_currentCellCoords
= wxGridNoCellCoords
;
2529 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2530 m_currentCellCoords
.Set( 0, 0 );
2534 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2535 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2538 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2540 // ifdef'd out following patch from Paul Gammans
2542 // No need to touch column attributes, unless we
2543 // removed _all_ rows, in this case, we remove
2544 // all column attributes.
2545 // I hate to do this here, but the
2546 // needed data is not available inside UpdateAttrRows.
2547 if ( !GetNumberRows() )
2548 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2552 if ( !GetBatchCount() )
2555 m_rowLabelWin
->Refresh();
2561 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2563 size_t pos
= msg
.GetCommandInt();
2564 int numCols
= msg
.GetCommandInt2();
2565 m_numCols
+= numCols
;
2567 if ( m_useNativeHeader
)
2568 GetGridColHeader()->SetColumnCount(m_numCols
);
2570 if ( !m_colAt
.IsEmpty() )
2572 //Shift the column IDs
2574 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2576 if ( m_colAt
[i
] >= (int)pos
)
2577 m_colAt
[i
] += numCols
;
2580 m_colAt
.Insert( pos
, pos
, numCols
);
2582 //Set the new columns' positions
2583 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2589 if ( !m_colWidths
.IsEmpty() )
2591 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2592 m_colRights
.Insert( 0, pos
, numCols
);
2596 right
= m_colRights
[GetColAt( pos
- 1 )];
2599 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2601 i
= GetColAt( colPos
);
2603 right
+= m_colWidths
[i
];
2604 m_colRights
[i
] = right
;
2608 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2610 // if we have just inserted cols into an empty grid the current
2611 // cell will be undefined...
2613 SetCurrentCell( 0, 0 );
2617 m_selection
->UpdateCols( pos
, numCols
);
2618 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2620 attrProvider
->UpdateAttrCols( pos
, numCols
);
2621 if ( !GetBatchCount() )
2624 m_colWindow
->Refresh();
2630 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2632 int numCols
= msg
.GetCommandInt();
2633 int oldNumCols
= m_numCols
;
2634 m_numCols
+= numCols
;
2635 if ( m_useNativeHeader
)
2636 GetGridColHeader()->SetColumnCount(m_numCols
);
2638 if ( !m_colAt
.IsEmpty() )
2640 m_colAt
.Add( 0, numCols
);
2642 //Set the new columns' positions
2644 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2650 if ( !m_colWidths
.IsEmpty() )
2652 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2653 m_colRights
.Add( 0, numCols
);
2656 if ( oldNumCols
> 0 )
2657 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2660 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2662 i
= GetColAt( colPos
);
2664 right
+= m_colWidths
[i
];
2665 m_colRights
[i
] = right
;
2669 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2671 // if we have just inserted cols into an empty grid the current
2672 // cell will be undefined...
2674 SetCurrentCell( 0, 0 );
2676 if ( !GetBatchCount() )
2679 m_colWindow
->Refresh();
2685 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2687 size_t pos
= msg
.GetCommandInt();
2688 int numCols
= msg
.GetCommandInt2();
2689 m_numCols
-= numCols
;
2690 if ( m_useNativeHeader
)
2691 GetGridColHeader()->SetColumnCount(m_numCols
);
2693 if ( !m_colAt
.IsEmpty() )
2695 int colID
= GetColAt( pos
);
2697 m_colAt
.RemoveAt( pos
, numCols
);
2699 //Shift the column IDs
2701 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2703 if ( m_colAt
[colPos
] > colID
)
2704 m_colAt
[colPos
] -= numCols
;
2708 if ( !m_colWidths
.IsEmpty() )
2710 m_colWidths
.RemoveAt( pos
, numCols
);
2711 m_colRights
.RemoveAt( pos
, numCols
);
2715 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2717 i
= GetColAt( colPos
);
2719 w
+= m_colWidths
[i
];
2726 m_currentCellCoords
= wxGridNoCellCoords
;
2730 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2731 m_currentCellCoords
.Set( 0, 0 );
2735 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2736 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2739 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2741 // ifdef'd out following patch from Paul Gammans
2743 // No need to touch row attributes, unless we
2744 // removed _all_ columns, in this case, we remove
2745 // all row attributes.
2746 // I hate to do this here, but the
2747 // needed data is not available inside UpdateAttrCols.
2748 if ( !GetNumberCols() )
2749 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2753 if ( !GetBatchCount() )
2756 m_colWindow
->Refresh();
2763 if (result
&& !GetBatchCount() )
2764 m_gridWin
->Refresh();
2769 wxArrayInt wxGrid
::CalcRowLabelsExposed( const wxRegion
& reg
) const
2771 wxRegionIterator
iter( reg
);
2774 wxArrayInt rowlabels
;
2781 // TODO: remove this when we can...
2782 // There is a bug in wxMotif that gives garbage update
2783 // rectangles if you jump-scroll a long way by clicking the
2784 // scrollbar with middle button. This is a work-around
2786 #if defined(__WXMOTIF__)
2788 m_gridWin
->GetClientSize( &cw
, &ch
);
2789 if ( r
.GetTop() > ch
)
2791 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2794 // logical bounds of update region
2797 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2798 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2800 // find the row labels within these bounds
2803 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2805 if ( GetRowBottom(row
) < top
)
2808 if ( GetRowTop(row
) > bottom
)
2811 rowlabels
.Add( row
);
2820 wxArrayInt wxGrid
::CalcColLabelsExposed( const wxRegion
& reg
) const
2822 wxRegionIterator
iter( reg
);
2825 wxArrayInt colLabels
;
2832 // TODO: remove this when we can...
2833 // There is a bug in wxMotif that gives garbage update
2834 // rectangles if you jump-scroll a long way by clicking the
2835 // scrollbar with middle button. This is a work-around
2837 #if defined(__WXMOTIF__)
2839 m_gridWin
->GetClientSize( &cw
, &ch
);
2840 if ( r
.GetLeft() > cw
)
2842 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2845 // logical bounds of update region
2848 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2849 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2851 // find the cells within these bounds
2855 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2857 col
= GetColAt( colPos
);
2859 if ( GetColRight(col
) < left
)
2862 if ( GetColLeft(col
) > right
)
2865 colLabels
.Add( col
);
2874 wxGridCellCoordsArray wxGrid
::CalcCellsExposed( const wxRegion
& reg
) const
2876 wxRegionIterator
iter( reg
);
2879 wxGridCellCoordsArray cellsExposed
;
2881 int left
, top
, right
, bottom
;
2886 // TODO: remove this when we can...
2887 // There is a bug in wxMotif that gives garbage update
2888 // rectangles if you jump-scroll a long way by clicking the
2889 // scrollbar with middle button. This is a work-around
2891 #if defined(__WXMOTIF__)
2893 m_gridWin
->GetClientSize( &cw
, &ch
);
2894 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2895 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2896 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2897 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2900 // logical bounds of update region
2902 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2903 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2905 // find the cells within these bounds
2907 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2909 if ( GetRowBottom(row
) <= top
)
2912 if ( GetRowTop(row
) > bottom
)
2915 // add all dirty cells in this row: notice that the columns which
2916 // are dirty don't depend on the row so we compute them only once
2917 // for the first dirty row and then reuse for all the next ones
2920 // do determine the dirty columns
2921 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2922 cols
.push_back(GetColAt(pos
));
2924 // if there are no dirty columns at all, nothing to do
2929 const size_t count
= cols
.size();
2930 for ( size_t n
= 0; n
< count
; n
++ )
2931 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2937 return cellsExposed
;
2941 void wxGrid
::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2944 wxPoint
pos( event
.GetPosition() );
2945 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2947 if ( event
.Dragging() )
2951 m_isDragging
= true;
2952 m_rowLabelWin
->CaptureMouse();
2955 if ( event
.LeftIsDown() )
2957 switch ( m_cursorMode
)
2959 case WXGRID_CURSOR_RESIZE_ROW
:
2961 int cw
, ch
, left
, dummy
;
2962 m_gridWin
->GetClientSize( &cw
, &ch
);
2963 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2965 wxClientDC
dc( m_gridWin
);
2968 GetRowTop(m_dragRowOrCol
) +
2969 GetRowMinimalHeight(m_dragRowOrCol
) );
2970 dc
.SetLogicalFunction(wxINVERT
);
2971 if ( m_dragLastPos
>= 0 )
2973 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2975 dc
.DrawLine( left
, y
, left
+cw
, y
);
2980 case WXGRID_CURSOR_SELECT_ROW
:
2982 if ( (row
= YToRow( y
)) >= 0 )
2985 m_selection
->SelectRow(row
, event
);
2990 // default label to suppress warnings about "enumeration value
2991 // 'xxx' not handled in switch
2999 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3004 if (m_rowLabelWin
->HasCapture())
3005 m_rowLabelWin
->ReleaseMouse();
3006 m_isDragging
= false;
3009 // ------------ Entering or leaving the window
3011 if ( event
.Entering() || event
.Leaving() )
3013 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3016 // ------------ Left button pressed
3018 else if ( event
.LeftDown() )
3020 row
= YToEdgeOfRow(y
);
3021 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3023 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3025 else // not a request to start resizing
3029 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3031 if ( !event
.ShiftDown() && !event
.CmdDown() )
3035 if ( event
.ShiftDown() )
3037 m_selection
->SelectBlock
3039 m_currentCellCoords
.GetRow(), 0,
3040 row
, GetNumberCols() - 1,
3046 m_selection
->SelectRow(row
, event
);
3050 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3055 // ------------ Left double click
3057 else if (event
.LeftDClick() )
3059 row
= YToEdgeOfRow(y
);
3060 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3062 // adjust row height depending on label text
3064 // TODO: generate RESIZING event, see #10754
3065 AutoSizeRowLabelSize( row
);
3067 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3069 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3072 else // not on row separator or it's not resizable
3076 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3078 // no default action at the moment
3083 // ------------ Left button released
3085 else if ( event
.LeftUp() )
3087 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3088 DoEndDragResizeRow(event
);
3090 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3094 // ------------ Right button down
3096 else if ( event
.RightDown() )
3100 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3102 // no default action at the moment
3106 // ------------ Right double click
3108 else if ( event
.RightDClick() )
3112 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3114 // no default action at the moment
3118 // ------------ No buttons down and mouse moving
3120 else if ( event
.Moving() )
3122 m_dragRowOrCol
= YToEdgeOfRow( y
);
3123 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3125 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3127 if ( CanDragRowSize(m_dragRowOrCol
) )
3128 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3131 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3133 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3138 void wxGrid
::UpdateColumnSortingIndicator(int col
)
3140 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3142 if ( m_useNativeHeader
)
3143 GetGridColHeader()->UpdateColumn(col
);
3144 else if ( m_nativeColumnLabels
)
3145 m_colWindow
->Refresh();
3146 //else: sorting indicator display not yet implemented in grid version
3149 void wxGrid
::SetSortingColumn(int col
, bool ascending
)
3151 if ( col
== m_sortCol
)
3153 // we are already using this column for sorting (or not sorting at all)
3154 // but we might still change the sorting order, check for it
3155 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3157 m_sortIsAscending
= ascending
;
3159 UpdateColumnSortingIndicator(m_sortCol
);
3162 else // we're changing the column used for sorting
3164 const int sortColOld
= m_sortCol
;
3166 // change it before updating the column as we want GetSortingColumn()
3167 // to return the correct new value
3170 if ( sortColOld
!= wxNOT_FOUND
)
3171 UpdateColumnSortingIndicator(sortColOld
);
3173 if ( m_sortCol
!= wxNOT_FOUND
)
3175 m_sortIsAscending
= ascending
;
3176 UpdateColumnSortingIndicator(m_sortCol
);
3181 void wxGrid
::DoColHeaderClick(int col
)
3183 // we consider that the grid was resorted if this event is processed and
3185 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3187 SetSortingColumn(col
, IsSortingBy(col
) ?
!m_sortIsAscending
: true);
3192 void wxGrid
::DoStartResizeCol(int col
)
3194 m_dragRowOrCol
= col
;
3196 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3199 void wxGrid
::DoUpdateResizeCol(int x
)
3201 int cw
, ch
, dummy
, top
;
3202 m_gridWin
->GetClientSize( &cw
, &ch
);
3203 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3205 wxClientDC
dc( m_gridWin
);
3208 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3209 dc
.SetLogicalFunction(wxINVERT
);
3210 if ( m_dragLastPos
>= 0 )
3212 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3214 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3218 void wxGrid
::DoUpdateResizeColWidth(int w
)
3220 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3223 void wxGrid
::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3226 wxPoint
pos( event
.GetPosition() );
3227 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3229 int col
= XToCol(x
);
3230 if ( event
.Dragging() )
3234 m_isDragging
= true;
3235 GetColLabelWindow()->CaptureMouse();
3237 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3238 DoStartMoveCol(col
);
3241 if ( event
.LeftIsDown() )
3243 switch ( m_cursorMode
)
3245 case WXGRID_CURSOR_RESIZE_COL
:
3246 DoUpdateResizeCol(x
);
3249 case WXGRID_CURSOR_SELECT_COL
:
3254 m_selection
->SelectCol(col
, event
);
3259 case WXGRID_CURSOR_MOVE_COL
:
3261 int posNew
= XToPos(x
);
3262 int colNew
= GetColAt(posNew
);
3264 // determine the position of the drop marker
3266 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3267 markerX
= GetColRight(colNew
);
3269 markerX
= GetColLeft(colNew
);
3271 if ( markerX
!= m_dragLastPos
)
3273 wxClientDC
dc( GetColLabelWindow() );
3277 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3281 //Clean up the last indicator
3282 if ( m_dragLastPos
>= 0 )
3284 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3286 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3287 dc
.SetPen(wxNullPen
);
3289 if ( XToCol( m_dragLastPos
) != -1 )
3290 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3293 const wxColour
*color
;
3294 //Moving to the same place? Don't draw a marker
3295 if ( colNew
== m_dragRowOrCol
)
3296 color
= wxLIGHT_GREY
;
3301 wxPen
pen( *color
, 2 );
3304 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3306 dc
.SetPen(wxNullPen
);
3308 m_dragLastPos
= markerX
- 1;
3313 // default label to suppress warnings about "enumeration value
3314 // 'xxx' not handled in switch
3322 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3327 if (GetColLabelWindow()->HasCapture())
3328 GetColLabelWindow()->ReleaseMouse();
3329 m_isDragging
= false;
3332 // ------------ Entering or leaving the window
3334 if ( event
.Entering() || event
.Leaving() )
3336 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3339 // ------------ Left button pressed
3341 else if ( event
.LeftDown() )
3343 int col
= XToEdgeOfCol(x
);
3344 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3346 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3348 else // not a request to start resizing
3352 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3354 if ( m_canDragColMove
)
3356 //Show button as pressed
3357 wxClientDC
dc( GetColLabelWindow() );
3358 int colLeft
= GetColLeft( col
);
3359 int colRight
= GetColRight( col
) - 1;
3360 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3361 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3362 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3364 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3368 if ( !event
.ShiftDown() && !event
.CmdDown() )
3372 if ( event
.ShiftDown() )
3374 m_selection
->SelectBlock
3376 0, m_currentCellCoords
.GetCol(),
3377 GetNumberRows() - 1, col
,
3383 m_selection
->SelectCol(col
, event
);
3387 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3393 // ------------ Left double click
3395 if ( event
.LeftDClick() )
3397 const int colEdge
= XToEdgeOfCol(x
);
3398 if ( colEdge
== -1 )
3401 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3403 // no default action at the moment
3408 // adjust column width depending on label text
3410 // TODO: generate RESIZING event, see #10754
3411 AutoSizeColLabelSize( colEdge
);
3413 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3415 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3420 // ------------ Left button released
3422 else if ( event
.LeftUp() )
3424 switch ( m_cursorMode
)
3426 case WXGRID_CURSOR_RESIZE_COL
:
3427 DoEndDragResizeCol(event
);
3430 case WXGRID_CURSOR_MOVE_COL
:
3431 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3433 // the column didn't actually move anywhere
3435 DoColHeaderClick(col
);
3436 m_colWindow
->Refresh(); // "unpress" the column
3440 // get the position of the column we're over
3441 int pos
= XToPos(x
);
3443 // we may need to adjust the drop position but don't bother
3444 // checking for it if we can't anyhow
3447 // also find the index of the column we're over: notice
3448 // that the existing "col" variable may be invalid but
3449 // we need a valid one here
3450 const int colValid
= GetColAt(pos
);
3452 // if we're on the "near" (usually left but right in
3453 // RTL case) part of the column, the actual position we
3454 // should be placed in is actually the one before it
3456 const int middle
= GetColLeft(colValid
) +
3457 GetColWidth(colValid
)/2;
3458 if ( GetLayoutDirection() == wxLayout_LeftToRight
)
3459 onNearPart
= (x
<= middle
);
3460 else // wxLayout_RightToLeft
3461 onNearPart
= (x
> middle
);
3471 case WXGRID_CURSOR_SELECT_COL
:
3472 case WXGRID_CURSOR_SELECT_CELL
:
3473 case WXGRID_CURSOR_RESIZE_ROW
:
3474 case WXGRID_CURSOR_SELECT_ROW
:
3476 DoColHeaderClick(col
);
3480 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3484 // ------------ Right button down
3486 else if ( event
.RightDown() )
3489 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3491 // no default action at the moment
3495 // ------------ Right double click
3497 else if ( event
.RightDClick() )
3500 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3502 // no default action at the moment
3506 // ------------ No buttons down and mouse moving
3508 else if ( event
.Moving() )
3510 m_dragRowOrCol
= XToEdgeOfCol( x
);
3511 if ( m_dragRowOrCol
>= 0 )
3513 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3515 if ( CanDragColSize(m_dragRowOrCol
) )
3516 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3519 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3521 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3526 void wxGrid
::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3528 if ( event
.LeftDown() )
3530 // indicate corner label by having both row and
3533 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3538 else if ( event
.LeftDClick() )
3540 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3542 else if ( event
.RightDown() )
3544 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3546 // no default action at the moment
3549 else if ( event
.RightDClick() )
3551 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3553 // no default action at the moment
3558 void wxGrid
::CancelMouseCapture()
3560 // cancel operation currently in progress, whatever it is
3563 m_isDragging
= false;
3564 m_startDragPos
= wxDefaultPosition
;
3566 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3567 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3568 m_winCapture
= NULL
;
3570 // remove traces of whatever we drew on screen
3575 void wxGrid
::ChangeCursorMode(CursorMode mode
,
3580 static const wxChar
*const cursorModes
[] =
3590 wxLogTrace(wxT("grid"),
3591 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3592 win
== m_colWindow ?
wxT("colLabelWin")
3593 : win ?
wxT("rowLabelWin")
3595 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3596 #endif // wxUSE_LOG_TRACE
3598 if ( mode
== m_cursorMode
&&
3599 win
== m_winCapture
&&
3600 captureMouse
== (m_winCapture
!= NULL
))
3605 // by default use the grid itself
3611 m_winCapture
->ReleaseMouse();
3612 m_winCapture
= NULL
;
3615 m_cursorMode
= mode
;
3617 switch ( m_cursorMode
)
3619 case WXGRID_CURSOR_RESIZE_ROW
:
3620 win
->SetCursor( m_rowResizeCursor
);
3623 case WXGRID_CURSOR_RESIZE_COL
:
3624 win
->SetCursor( m_colResizeCursor
);
3627 case WXGRID_CURSOR_MOVE_COL
:
3628 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3632 win
->SetCursor( *wxSTANDARD_CURSOR
);
3636 // we need to capture mouse when resizing
3637 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3638 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3640 if ( captureMouse
&& resize
)
3642 win
->CaptureMouse();
3647 // ----------------------------------------------------------------------------
3648 // grid mouse event processing
3649 // ----------------------------------------------------------------------------
3652 wxGrid
::DoGridCellDrag(wxMouseEvent
& event
,
3653 const wxGridCellCoords
& coords
,
3656 bool performDefault
= true ;
3658 if ( coords
== wxGridNoCellCoords
)
3659 return performDefault
; // we're outside any valid cell
3661 // Hide the edit control, so it won't interfere with drag-shrinking.
3662 if ( IsCellEditControlShown() )
3664 HideCellEditControl();
3665 SaveEditControlValue();
3668 switch ( event
.GetModifiers() )
3671 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3672 m_selectedBlockCorner
= coords
;
3673 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3677 if ( CanDragCell() )
3681 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3682 m_selectedBlockCorner
= coords
;
3684 // if event is handled by user code, no further processing
3685 if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
) != 0 )
3686 performDefault
= false;
3688 return performDefault
;
3692 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3696 // we don't handle the other key modifiers
3700 return performDefault
;
3703 void wxGrid
::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3705 wxClientDC
dc(m_gridWin
);
3707 dc
.SetLogicalFunction(wxINVERT
);
3709 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3710 m_gridWin
->GetClientSize());
3712 // erase the previously drawn line, if any
3713 if ( m_dragLastPos
>= 0 )
3714 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3716 // we need the vertical position for rows and horizontal for columns here
3717 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3719 // don't allow resizing beneath the minimal size
3720 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3721 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3722 if ( m_dragLastPos
< posMin
)
3723 m_dragLastPos
= posMin
;
3725 // and draw it at the new position
3726 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3729 void wxGrid
::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3731 if ( !m_isDragging
)
3733 // Don't start doing anything until the mouse has been dragged far
3735 const wxPoint
& pt
= event
.GetPosition();
3736 if ( m_startDragPos
== wxDefaultPosition
)
3738 m_startDragPos
= pt
;
3742 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3743 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3747 const bool isFirstDrag
= !m_isDragging
;
3748 m_isDragging
= true;
3750 switch ( m_cursorMode
)
3752 case WXGRID_CURSOR_SELECT_CELL
:
3753 // no further handling if handled by user
3754 if ( DoGridCellDrag(event
, coords
, isFirstDrag
) == false )
3758 case WXGRID_CURSOR_RESIZE_ROW
:
3759 DoGridLineDrag(event
, wxGridRowOperations());
3762 case WXGRID_CURSOR_RESIZE_COL
:
3763 DoGridLineDrag(event
, wxGridColumnOperations());
3772 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
3774 m_winCapture
= m_gridWin
;
3775 m_winCapture
->CaptureMouse();
3780 wxGrid
::DoGridCellLeftDown(wxMouseEvent
& event
,
3781 const wxGridCellCoords
& coords
,
3784 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3786 // event handled by user code, no need to do anything here
3790 if ( !event
.CmdDown() )
3793 if ( event
.ShiftDown() )
3797 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3798 m_selectedBlockCorner
= coords
;
3801 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3803 DisableCellEditControl();
3804 MakeCellVisible( coords
);
3806 if ( event
.CmdDown() )
3810 m_selection
->ToggleCellSelection(coords
, event
);
3813 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3814 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3815 m_selectedBlockCorner
= coords
;
3821 // In row or column selection mode just clicking on the cell
3822 // should select the row or column containing it: this is more
3823 // convenient for the kinds of controls that use such selection
3824 // mode and is compatible with 2.8 behaviour (see #12062).
3825 switch ( m_selection
->GetSelectionMode() )
3827 case wxGridSelectCells
:
3828 case wxGridSelectRowsOrColumns
:
3829 // nothing to do in these cases
3832 case wxGridSelectRows
:
3833 m_selection
->SelectRow(coords
.GetRow());
3836 case wxGridSelectColumns
:
3837 m_selection
->SelectCol(coords
.GetCol());
3842 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3843 coords
!= wxGridNoCellCoords
;
3844 SetCurrentCell( coords
);
3850 wxGrid
::DoGridCellLeftDClick(wxMouseEvent
& event
,
3851 const wxGridCellCoords
& coords
,
3854 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3856 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3858 // we want double click to select a cell and start editing
3859 // (i.e. to behave in same way as sequence of two slow clicks):
3860 m_waitForSlowClick
= true;
3866 wxGrid
::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3868 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3872 m_winCapture
->ReleaseMouse();
3873 m_winCapture
= NULL
;
3876 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3879 EnableCellEditControl();
3881 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3882 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3883 editor
->StartingClick();
3887 m_waitForSlowClick
= false;
3889 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3890 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3894 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3895 m_selectedBlockBottomRight
,
3899 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3900 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3902 // Show the edit control, if it has been hidden for
3904 ShowCellEditControl();
3907 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3909 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3910 DoEndDragResizeRow(event
);
3912 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3914 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3915 DoEndDragResizeCol(event
);
3922 wxGrid
::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3923 const wxGridCellCoords
& coords
,
3926 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3928 // out of grid cell area
3929 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3933 int dragRow
= YToEdgeOfRow( pos
.y
);
3934 int dragCol
= XToEdgeOfCol( pos
.x
);
3936 // Dragging on the corner of a cell to resize in both
3937 // directions is not implemented yet...
3939 if ( dragRow
>= 0 && dragCol
>= 0 )
3941 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3945 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3947 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3949 m_dragRowOrCol
= dragRow
;
3950 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3953 // When using the native header window we can only resize the columns by
3954 // dragging the dividers in it because we can't make it enter into the
3955 // column resizing mode programmatically
3956 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3957 CanDragGridSize() && CanDragColSize(dragCol
) )
3959 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3961 m_dragRowOrCol
= dragCol
;
3962 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3965 else // Neither on a row or col edge
3967 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3969 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3974 void wxGrid
::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
3976 if ( event
.Entering() || event
.Leaving() )
3978 // we don't care about these events but we must not reset m_isDragging
3979 // if they happen so return before anything else is done
3984 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
3986 // coordinates of the cell under mouse
3987 wxGridCellCoords coords
= XYToCell(pos
);
3989 int cell_rows
, cell_cols
;
3990 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
3991 if ( (cell_rows
< 0) || (cell_cols
< 0) )
3993 coords
.SetRow(coords
.GetRow() + cell_rows
);
3994 coords
.SetCol(coords
.GetCol() + cell_cols
);
3997 if ( event
.Dragging() )
3999 if ( event
.LeftIsDown() )
4000 DoGridDragEvent(event
, coords
);
4006 m_isDragging
= false;
4007 m_startDragPos
= wxDefaultPosition
;
4009 // deal with various button presses
4010 if ( event
.IsButton() )
4012 if ( coords
!= wxGridNoCellCoords
)
4014 DisableCellEditControl();
4016 if ( event
.LeftDown() )
4017 DoGridCellLeftDown(event
, coords
, pos
);
4018 else if ( event
.LeftDClick() )
4019 DoGridCellLeftDClick(event
, coords
, pos
);
4020 else if ( event
.RightDown() )
4021 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
4022 else if ( event
.RightDClick() )
4023 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
4026 // this one should be called even if we're not over any cell
4027 if ( event
.LeftUp() )
4029 DoGridCellLeftUp(event
, coords
);
4032 else if ( event
.Moving() )
4034 DoGridMouseMoveEvent(event
, coords
, pos
);
4036 else // unknown mouse event?
4042 // this function returns true only if the size really changed
4043 bool wxGrid
::DoEndDragResizeLine(const wxGridOperations
& oper
)
4045 if ( m_dragLastPos
== -1 )
4048 const wxGridOperations
& doper
= oper
.Dual();
4050 const wxSize size
= m_gridWin
->GetClientSize();
4052 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4054 // erase the last line we drew
4055 wxClientDC
dc(m_gridWin
);
4057 dc
.SetLogicalFunction(wxINVERT
);
4059 const int posLineStart
= oper
.Select(ptOrigin
);
4060 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4062 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4064 // temporarily hide the edit control before resizing
4065 HideCellEditControl();
4066 SaveEditControlValue();
4068 // do resize the line
4069 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4070 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4071 oper
.SetLineSize(this, m_dragRowOrCol
,
4072 wxMax(m_dragLastPos
- lineStart
,
4073 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4075 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4079 // refresh now if we're not frozen
4080 if ( !GetBatchCount() )
4082 // we need to refresh everything beyond the resized line in the header
4085 // get the position from which to refresh in the other direction
4086 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4087 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4089 // we only need the ordinate (for rows) or abscissa (for columns) here,
4090 // and need to cover the entire window in the other direction
4091 oper
.Select(rect
) = 0;
4093 wxRect
rectHeader(rect
.GetPosition(),
4096 oper
.GetHeaderWindowSize(this),
4097 doper
.Select(size
) - doper
.Select(rect
)
4100 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4103 // also refresh the grid window: extend the rectangle
4106 oper
.SelectSize(rect
) = oper
.Select(size
);
4108 int subtractLines
= 0;
4109 const int lineStart
= doper
.PosToLine(this, posLineStart
);
4110 if ( lineStart
>= 0 )
4112 // ensure that if we have a multi-cell block we redraw all of
4113 // it by increasing the refresh area to cover it entirely if a
4114 // part of it is affected
4115 const int lineEnd
= doper
.PosToLine(this, posLineEnd
, true);
4116 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
4118 int cellLines
= oper
.Select(
4119 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4120 if ( cellLines
< subtractLines
)
4121 subtractLines
= cellLines
;
4126 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4127 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4129 doper
.Select(rect
) = startPos
;
4130 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4132 m_gridWin
->Refresh(false, &rect
);
4136 // show the edit control back again
4137 ShowCellEditControl();
4142 void wxGrid
::DoEndDragResizeRow(const wxMouseEvent
& event
)
4144 // TODO: generate RESIZING event, see #10754
4146 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4147 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4150 void wxGrid
::DoEndDragResizeCol(const wxMouseEvent
& event
)
4152 // TODO: generate RESIZING event, see #10754
4154 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4155 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4158 void wxGrid
::DoStartMoveCol(int col
)
4160 m_dragRowOrCol
= col
;
4163 void wxGrid
::DoEndMoveCol(int pos
)
4165 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4167 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4168 SetColPos(m_dragRowOrCol
, pos
);
4169 //else: vetoed by user
4171 m_dragRowOrCol
= -1;
4174 void wxGrid
::RefreshAfterColPosChange()
4176 // recalculate the column rights as the column positions have changed,
4177 // unless we calculate them dynamically because all columns widths are the
4178 // same and it's easy to do
4179 if ( !m_colWidths
.empty() )
4182 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4184 int colID
= GetColAt( colPos
);
4186 colRight
+= m_colWidths
[colID
];
4187 m_colRights
[colID
] = colRight
;
4191 // and make the changes visible
4192 if ( m_useNativeHeader
)
4194 if ( m_colAt
.empty() )
4195 GetGridColHeader()->ResetColumnsOrder();
4197 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4201 m_colWindow
->Refresh();
4203 m_gridWin
->Refresh();
4206 void wxGrid
::SetColumnsOrder(const wxArrayInt
& order
)
4210 RefreshAfterColPosChange();
4213 void wxGrid
::SetColPos(int idx
, int pos
)
4215 // we're going to need m_colAt now, initialize it if needed
4216 if ( m_colAt
.empty() )
4218 m_colAt
.reserve(m_numCols
);
4219 for ( int i
= 0; i
< m_numCols
; i
++ )
4220 m_colAt
.push_back(i
);
4223 wxHeaderCtrl
::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4225 RefreshAfterColPosChange();
4228 void wxGrid
::ResetColPos()
4232 RefreshAfterColPosChange();
4235 void wxGrid
::EnableDragColMove( bool enable
)
4237 if ( m_canDragColMove
== enable
)
4240 if ( m_useNativeHeader
)
4242 // update all columns to make them [not] reorderable
4243 GetGridColHeader()->SetColumnCount(m_numCols
);
4246 m_canDragColMove
= enable
;
4248 // we use to call ResetColPos() from here if !enable but this doesn't seem
4249 // right as it would mean there would be no way to "freeze" the current
4250 // columns order by disabling moving them after putting them in the desired
4251 // order, whereas now you can always call ResetColPos() manually if needed
4256 // ------ interaction with data model
4258 bool wxGrid
::ProcessTableMessage( wxGridTableMessage
& msg
)
4260 switch ( msg
.GetId() )
4262 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4263 return GetModelValues();
4265 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4266 return SetModelValues();
4268 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4269 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4270 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4271 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4272 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4273 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4274 return Redimension( msg
);
4281 // The behaviour of this function depends on the grid table class
4282 // Clear() function. For the default wxGridStringTable class the
4283 // behaviour is to replace all cell contents with wxEmptyString but
4284 // not to change the number of rows or cols.
4286 void wxGrid
::ClearGrid()
4290 if (IsCellEditControlEnabled())
4291 DisableCellEditControl();
4294 if (!GetBatchCount())
4295 m_gridWin
->Refresh();
4300 wxGrid
::DoModifyLines(bool (wxGridTableBase
::*funcModify
)(size_t, size_t),
4301 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4303 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4308 if ( IsCellEditControlEnabled() )
4309 DisableCellEditControl();
4311 return (m_table
->*funcModify
)(pos
, num
);
4313 // the table will have sent the results of the insert row
4314 // operation to this view object as a grid table message
4318 wxGrid
::DoAppendLines(bool (wxGridTableBase
::*funcAppend
)(size_t),
4319 int num
, bool WXUNUSED(updateLabels
))
4321 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4326 return (m_table
->*funcAppend
)(num
);
4329 // ----------------------------------------------------------------------------
4330 // event generation helpers
4331 // ----------------------------------------------------------------------------
4334 wxGrid
::SendGridSizeEvent(wxEventType type
,
4336 const wxMouseEvent
& mouseEv
)
4338 int rowOrCol
= row
== -1 ? col
: row
;
4340 wxGridSizeEvent
gridEvt( GetId(),
4344 mouseEv
.GetX() + GetRowLabelSize(),
4345 mouseEv
.GetY() + GetColLabelSize(),
4348 GetEventHandler()->ProcessEvent(gridEvt
);
4351 // Generate a grid event based on a mouse event and return:
4352 // -1 if the event was vetoed
4353 // +1 if the event was processed (but not vetoed)
4354 // 0 if the event wasn't handled
4356 wxGrid
::SendEvent(const wxEventType type
,
4358 const wxMouseEvent
& mouseEv
)
4360 bool claimed
, vetoed
;
4362 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4364 // Right now, it should _never_ end up here!
4365 wxGridRangeSelectEvent
gridEvt( GetId(),
4368 m_selectedBlockTopLeft
,
4369 m_selectedBlockBottomRight
,
4373 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4374 vetoed
= !gridEvt
.IsAllowed();
4376 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4377 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4378 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4379 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4381 wxPoint pos
= mouseEv
.GetPosition();
4383 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4384 pos
.y
+= GetColLabelSize();
4385 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4386 pos
.x
+= GetRowLabelSize();
4388 wxGridEvent
gridEvt( GetId(),
4396 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4397 vetoed
= !gridEvt
.IsAllowed();
4401 wxGridEvent
gridEvt( GetId(),
4405 mouseEv
.GetX() + GetRowLabelSize(),
4406 mouseEv
.GetY() + GetColLabelSize(),
4410 if ( type
== wxEVT_GRID_CELL_BEGIN_DRAG
)
4412 // by default the dragging is not supported, the user code must
4413 // explicitly allow the event for it to take place
4417 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4418 vetoed
= !gridEvt
.IsAllowed();
4421 // A Veto'd event may not be `claimed' so test this first
4425 return claimed ?
1 : 0;
4428 // Generate a grid event of specified type, return value same as above
4431 wxGrid
::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4433 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4434 gridEvt
.SetString(s
);
4436 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4438 // A Veto'd event may not be `claimed' so test this first
4439 if ( !gridEvt
.IsAllowed() )
4442 return claimed ?
1 : 0;
4445 void wxGrid
::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4447 // needed to prevent zillions of paint events on MSW
4451 void wxGrid
::Refresh(bool eraseb
, const wxRect
* rect
)
4453 // Don't do anything if between Begin/EndBatch...
4454 // EndBatch() will do all this on the last nested one anyway.
4455 if ( m_created
&& !GetBatchCount() )
4457 // Refresh to get correct scrolled position:
4458 wxScrolledWindow
::Refresh(eraseb
, rect
);
4462 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4463 int width_label
, width_cell
, height_label
, height_cell
;
4466 // Copy rectangle can get scroll offsets..
4467 rect_x
= rect
->GetX();
4468 rect_y
= rect
->GetY();
4469 rectWidth
= rect
->GetWidth();
4470 rectHeight
= rect
->GetHeight();
4472 width_label
= m_rowLabelWidth
- rect_x
;
4473 if (width_label
> rectWidth
)
4474 width_label
= rectWidth
;
4476 height_label
= m_colLabelHeight
- rect_y
;
4477 if (height_label
> rectHeight
)
4478 height_label
= rectHeight
;
4480 if (rect_x
> m_rowLabelWidth
)
4482 x
= rect_x
- m_rowLabelWidth
;
4483 width_cell
= rectWidth
;
4488 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4491 if (rect_y
> m_colLabelHeight
)
4493 y
= rect_y
- m_colLabelHeight
;
4494 height_cell
= rectHeight
;
4499 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4502 // Paint corner label part intersecting rect.
4503 if ( width_label
> 0 && height_label
> 0 )
4505 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4506 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4509 // Paint col labels part intersecting rect.
4510 if ( width_cell
> 0 && height_label
> 0 )
4512 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4513 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4516 // Paint row labels part intersecting rect.
4517 if ( width_label
> 0 && height_cell
> 0 )
4519 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4520 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4523 // Paint cell area part intersecting rect.
4524 if ( width_cell
> 0 && height_cell
> 0 )
4526 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4527 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4532 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4533 m_colWindow
->Refresh(eraseb
, NULL
);
4534 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4535 m_gridWin
->Refresh(eraseb
, NULL
);
4540 void wxGrid
::OnSize(wxSizeEvent
& WXUNUSED(event
))
4542 if (m_targetWindow
!= this) // check whether initialisation has been done
4544 // reposition our children windows
4549 void wxGrid
::OnKeyDown( wxKeyEvent
& event
)
4551 if ( m_inOnKeyDown
)
4553 // shouldn't be here - we are going round in circles...
4555 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4558 m_inOnKeyDown
= true;
4560 // propagate the event up and see if it gets processed
4561 wxWindow
*parent
= GetParent();
4562 wxKeyEvent
keyEvt( event
);
4563 keyEvt
.SetEventObject( parent
);
4565 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4567 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4569 if (event
.GetKeyCode() == WXK_RIGHT
)
4570 event
.m_keyCode
= WXK_LEFT
;
4571 else if (event
.GetKeyCode() == WXK_LEFT
)
4572 event
.m_keyCode
= WXK_RIGHT
;
4575 // try local handlers
4576 switch ( event
.GetKeyCode() )
4579 if ( event
.ControlDown() )
4580 MoveCursorUpBlock( event
.ShiftDown() );
4582 MoveCursorUp( event
.ShiftDown() );
4586 if ( event
.ControlDown() )
4587 MoveCursorDownBlock( event
.ShiftDown() );
4589 MoveCursorDown( event
.ShiftDown() );
4593 if ( event
.ControlDown() )
4594 MoveCursorLeftBlock( event
.ShiftDown() );
4596 MoveCursorLeft( event
.ShiftDown() );
4600 if ( event
.ControlDown() )
4601 MoveCursorRightBlock( event
.ShiftDown() );
4603 MoveCursorRight( event
.ShiftDown() );
4607 case WXK_NUMPAD_ENTER
:
4608 if ( event
.ControlDown() )
4610 event
.Skip(); // to let the edit control have the return
4614 if ( GetGridCursorRow() < GetNumberRows()-1 )
4616 MoveCursorDown( event
.ShiftDown() );
4620 // at the bottom of a column
4621 DisableCellEditControl();
4631 if (event
.ShiftDown())
4633 if ( GetGridCursorCol() > 0 )
4635 MoveCursorLeft( false );
4640 DisableCellEditControl();
4645 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4647 MoveCursorRight( false );
4652 DisableCellEditControl();
4658 GoToCell(event
.ControlDown() ?
0
4659 : m_currentCellCoords
.GetRow(),
4664 GoToCell(event
.ControlDown() ? m_numRows
- 1
4665 : m_currentCellCoords
.GetRow(),
4678 // Ctrl-Space selects the current column, Shift-Space -- the
4679 // current row and Ctrl-Shift-Space -- everything
4680 switch ( m_selection ? event
.GetModifiers() : wxMOD_NONE
)
4683 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4687 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4690 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4691 m_selection
->SelectBlock(0, 0,
4692 m_numRows
- 1, m_numCols
- 1);
4696 if ( !IsEditable() )
4698 MoveCursorRight(false);
4701 //else: fall through
4714 m_inOnKeyDown
= false;
4717 void wxGrid
::OnKeyUp( wxKeyEvent
& event
)
4719 // try local handlers
4721 if ( event
.GetKeyCode() == WXK_SHIFT
)
4723 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4724 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4728 m_selection
->SelectBlock(
4729 m_selectedBlockTopLeft
,
4730 m_selectedBlockBottomRight
,
4735 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4736 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4737 m_selectedBlockCorner
= wxGridNoCellCoords
;
4741 void wxGrid
::OnChar( wxKeyEvent
& event
)
4743 // is it possible to edit the current cell at all?
4744 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4746 // yes, now check whether the cells editor accepts the key
4747 int row
= m_currentCellCoords
.GetRow();
4748 int col
= m_currentCellCoords
.GetCol();
4749 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4750 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4752 // <F2> is special and will always start editing, for
4753 // other keys - ask the editor itself
4754 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4755 || editor
->IsAcceptedKey(event
) )
4757 // ensure cell is visble
4758 MakeCellVisible(row
, col
);
4759 EnableCellEditControl();
4761 // a problem can arise if the cell is not completely
4762 // visible (even after calling MakeCellVisible the
4763 // control is not created and calling StartingKey will
4765 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4766 editor
->StartingKey(event
);
4782 void wxGrid
::OnEraseBackground(wxEraseEvent
&)
4786 bool wxGrid
::SetCurrentCell( const wxGridCellCoords
& coords
)
4788 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4790 // the event has been vetoed - do nothing
4794 #if !defined(__WXMAC__)
4795 wxClientDC
dc( m_gridWin
);
4799 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4801 DisableCellEditControl();
4803 if ( IsVisible( m_currentCellCoords
, false ) )
4806 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4807 if ( !m_gridLinesEnabled
)
4815 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4817 // Otherwise refresh redraws the highlight!
4818 m_currentCellCoords
= coords
;
4820 #if defined(__WXMAC__)
4821 m_gridWin
->Refresh(true /*, & r */);
4823 DrawGridCellArea( dc
, cells
);
4824 DrawAllGridLines( dc
, r
);
4829 m_currentCellCoords
= coords
;
4831 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4832 #if !defined(__WXMAC__)
4833 DrawCellHighlight( dc
, attr
);
4841 wxGrid
::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4842 int bottomRow
, int rightCol
)
4844 MakeCellVisible(m_selectedBlockCorner
);
4845 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4849 switch ( m_selection
->GetSelectionMode() )
4852 wxFAIL_MSG( "unknown selection mode" );
4855 case wxGridSelectCells
:
4856 // arbitrary blocks selection allowed so just use the cell
4857 // coordinates as is
4860 case wxGridSelectRows
:
4861 // only full rows selection allowd, ensure that we do select
4864 rightCol
= GetNumberCols() - 1;
4867 case wxGridSelectColumns
:
4868 // same as above but for columns
4870 bottomRow
= GetNumberRows() - 1;
4873 case wxGridSelectRowsOrColumns
:
4874 // in this mode we can select only full rows or full columns so
4875 // it doesn't make sense to select blocks at all (and we can't
4876 // extend the block because there is no preferred direction, we
4877 // could only extend it to cover the entire grid but this is
4883 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4884 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4886 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4887 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4889 // First the case that we selected a completely new area
4890 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4891 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4894 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4895 wxGridCellCoords ( bottomRow
, rightCol
) );
4896 m_gridWin
->Refresh( false, &rect
);
4899 // Now handle changing an existing selection area.
4900 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4901 m_selectedBlockBottomRight
!= updateBottomRight
)
4903 // Compute two optimal update rectangles:
4904 // Either one rectangle is a real subset of the
4905 // other, or they are (almost) disjoint!
4907 bool need_refresh
[4];
4911 need_refresh
[3] = false;
4914 // Store intermediate values
4915 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4916 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4917 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4918 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4920 // Determine the outer/inner coordinates.
4921 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4922 EnsureFirstLessThanSecond(oldTop
, topRow
);
4923 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4924 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4926 // Now, either the stuff marked old is the outer
4927 // rectangle or we don't have a situation where one
4928 // is contained in the other.
4930 if ( oldLeft
< leftCol
)
4932 // Refresh the newly selected or deselected
4933 // area to the left of the old or new selection.
4934 need_refresh
[0] = true;
4935 rect
[0] = BlockToDeviceRect(
4936 wxGridCellCoords( oldTop
, oldLeft
),
4937 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4940 if ( oldTop
< topRow
)
4942 // Refresh the newly selected or deselected
4943 // area above the old or new selection.
4944 need_refresh
[1] = true;
4945 rect
[1] = BlockToDeviceRect(
4946 wxGridCellCoords( oldTop
, leftCol
),
4947 wxGridCellCoords( topRow
- 1, rightCol
) );
4950 if ( oldRight
> rightCol
)
4952 // Refresh the newly selected or deselected
4953 // area to the right of the old or new selection.
4954 need_refresh
[2] = true;
4955 rect
[2] = BlockToDeviceRect(
4956 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4957 wxGridCellCoords( oldBottom
, oldRight
) );
4960 if ( oldBottom
> bottomRow
)
4962 // Refresh the newly selected or deselected
4963 // area below the old or new selection.
4964 need_refresh
[3] = true;
4965 rect
[3] = BlockToDeviceRect(
4966 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4967 wxGridCellCoords( oldBottom
, rightCol
) );
4970 // various Refresh() calls
4971 for (i
= 0; i
< 4; i
++ )
4972 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4973 m_gridWin
->Refresh( false, &(rect
[i
]) );
4977 m_selectedBlockTopLeft
= updateTopLeft
;
4978 m_selectedBlockBottomRight
= updateBottomRight
;
4982 // ------ functions to get/send data (see also public functions)
4985 bool wxGrid
::GetModelValues()
4987 // Hide the editor, so it won't hide a changed value.
4988 HideCellEditControl();
4992 // all we need to do is repaint the grid
4994 m_gridWin
->Refresh();
5001 bool wxGrid
::SetModelValues()
5005 // Disable the editor, so it won't hide a changed value.
5006 // Do we also want to save the current value of the editor first?
5008 DisableCellEditControl();
5012 for ( row
= 0; row
< m_numRows
; row
++ )
5014 for ( col
= 0; col
< m_numCols
; col
++ )
5016 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5026 // Note - this function only draws cells that are in the list of
5027 // exposed cells (usually set from the update region by
5028 // CalcExposedCells)
5030 void wxGrid
::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5032 if ( !m_numRows
|| !m_numCols
)
5035 int i
, numCells
= cells
.GetCount();
5036 int row
, col
, cell_rows
, cell_cols
;
5037 wxGridCellCoordsArray redrawCells
;
5039 for ( i
= numCells
- 1; i
>= 0; i
-- )
5041 row
= cells
[i
].GetRow();
5042 col
= cells
[i
].GetCol();
5043 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5045 // If this cell is part of a multicell block, find owner for repaint
5046 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5048 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5049 bool marked
= false;
5050 for ( int j
= 0; j
< numCells
; j
++ )
5052 if ( cell
== cells
[j
] )
5061 int count
= redrawCells
.GetCount();
5062 for (int j
= 0; j
< count
; j
++)
5064 if ( cell
== redrawCells
[j
] )
5072 redrawCells
.Add( cell
);
5075 // don't bother drawing this cell
5079 // If this cell is empty, find cell to left that might want to overflow
5080 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5082 for ( int l
= 0; l
< cell_rows
; l
++ )
5084 // find a cell in this row to leave already marked for repaint
5086 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5087 if ((redrawCells
[k
].GetCol() < left
) &&
5088 (redrawCells
[k
].GetRow() == row
))
5090 left
= redrawCells
[k
].GetCol();
5094 left
= 0; // oh well
5096 for (int j
= col
- 1; j
>= left
; j
--)
5098 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5100 if (GetCellOverflow(row
+ l
, j
))
5102 wxGridCellCoords
cell(row
+ l
, j
);
5103 bool marked
= false;
5105 for (int k
= 0; k
< numCells
; k
++)
5107 if ( cell
== cells
[k
] )
5116 int count
= redrawCells
.GetCount();
5117 for (int k
= 0; k
< count
; k
++)
5119 if ( cell
== redrawCells
[k
] )
5126 redrawCells
.Add( cell
);
5135 DrawCell( dc
, cells
[i
] );
5138 numCells
= redrawCells
.GetCount();
5140 for ( i
= numCells
- 1; i
>= 0; i
-- )
5142 DrawCell( dc
, redrawCells
[i
] );
5146 void wxGrid
::DrawGridSpace( wxDC
& dc
)
5149 m_gridWin
->GetClientSize( &cw
, &ch
);
5152 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5154 int rightCol
= m_numCols
> 0 ?
GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5155 int bottomRow
= m_numRows
> 0 ?
GetRowBottom(m_numRows
- 1) : 0;
5157 if ( right
> rightCol
|| bottom
> bottomRow
)
5160 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5162 dc
.SetBrush(GetDefaultCellBackgroundColour());
5163 dc
.SetPen( *wxTRANSPARENT_PEN
);
5165 if ( right
> rightCol
)
5167 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5170 if ( bottom
> bottomRow
)
5172 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5177 void wxGrid
::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5179 int row
= coords
.GetRow();
5180 int col
= coords
.GetCol();
5182 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5185 // we draw the cell border ourselves
5186 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5188 bool isCurrent
= coords
== m_currentCellCoords
;
5190 wxRect rect
= CellToRect( row
, col
);
5192 // if the editor is shown, we should use it and not the renderer
5193 // Note: However, only if it is really _shown_, i.e. not hidden!
5194 if ( isCurrent
&& IsCellEditControlShown() )
5196 // NB: this "#if..." is temporary and fixes a problem where the
5197 // edit control is erased by this code after being rendered.
5198 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5199 // implicitly, causing this out-of order render.
5200 #if !defined(__WXMAC__)
5201 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5202 editor
->PaintBackground(rect
, attr
);
5208 // but all the rest is drawn by the cell renderer and hence may be customized
5209 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5210 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5217 void wxGrid
::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5219 // don't show highlight when the grid doesn't have focus
5223 int row
= m_currentCellCoords
.GetRow();
5224 int col
= m_currentCellCoords
.GetCol();
5226 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5229 wxRect rect
= CellToRect(row
, col
);
5231 // hmmm... what could we do here to show that the cell is disabled?
5232 // for now, I just draw a thinner border than for the other ones, but
5233 // it doesn't look really good
5235 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5239 // The center of the drawn line is where the position/width/height of
5240 // the rectangle is actually at (on wxMSW at least), so the
5241 // size of the rectangle is reduced to compensate for the thickness of
5242 // the line. If this is too strange on non-wxMSW platforms then
5243 // please #ifdef this appropriately.
5244 rect
.x
+= penWidth
/ 2;
5245 rect
.y
+= penWidth
/ 2;
5246 rect
.width
-= penWidth
- 1;
5247 rect
.height
-= penWidth
- 1;
5249 // Now draw the rectangle
5250 // use the cellHighlightColour if the cell is inside a selection, this
5251 // will ensure the cell is always visible.
5252 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5253 : m_cellHighlightColour
,
5255 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5256 dc
.DrawRectangle(rect
);
5260 wxPen wxGrid
::GetDefaultGridLinePen()
5262 return wxPen(GetGridLineColour());
5265 wxPen wxGrid
::GetRowGridLinePen(int WXUNUSED(row
))
5267 return GetDefaultGridLinePen();
5270 wxPen wxGrid
::GetColGridLinePen(int WXUNUSED(col
))
5272 return GetDefaultGridLinePen();
5275 void wxGrid
::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5277 int row
= coords
.GetRow();
5278 int col
= coords
.GetCol();
5279 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5283 wxRect rect
= CellToRect( row
, col
);
5285 // right hand border
5286 dc
.SetPen( GetColGridLinePen(col
) );
5287 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5288 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5291 dc
.SetPen( GetRowGridLinePen(row
) );
5292 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5293 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5296 void wxGrid
::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5298 // This if block was previously in wxGrid::OnPaint but that doesn't
5299 // seem to get called under wxGTK - MB
5301 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5302 m_numRows
&& m_numCols
)
5304 m_currentCellCoords
.Set(0, 0);
5307 if ( IsCellEditControlShown() )
5309 // don't show highlight when the edit control is shown
5313 // if the active cell was repainted, repaint its highlight too because it
5314 // might have been damaged by the grid lines
5315 size_t count
= cells
.GetCount();
5316 for ( size_t n
= 0; n
< count
; n
++ )
5318 wxGridCellCoords cell
= cells
[n
];
5320 // If we are using attributes, then we may have just exposed another
5321 // cell in a partially-visible merged cluster of cells. If the "anchor"
5322 // (upper left) cell of this merged cluster is the cell indicated by
5323 // m_currentCellCoords, then we need to refresh the cell highlight even
5324 // though the "anchor" itself is not part of our update segment.
5325 if ( CanHaveAttributes() )
5329 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5332 cell
.SetRow(cell
.GetRow() + rows
);
5335 cell
.SetCol(cell
.GetCol() + cols
);
5338 if ( cell
== m_currentCellCoords
)
5340 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5341 DrawCellHighlight(dc
, attr
);
5349 // Used by wxGrid::Render() to draw the grid lines only for the cells in the
5352 wxGrid
::DrawRangeGridLines(wxDC
& dc
,
5353 const wxRegion
& reg
,
5354 const wxGridCellCoords
& topLeft
,
5355 const wxGridCellCoords
& bottomRight
)
5357 if ( !m_gridLinesEnabled
)
5360 int top
, left
, width
, height
;
5361 reg
.GetBox( left
, top
, width
, height
);
5363 // create a clipping region
5364 wxRegion
clippedcells( dc
.LogicalToDeviceX( left
),
5365 dc
.LogicalToDeviceY( top
),
5366 dc
.LogicalToDeviceXRel( width
),
5367 dc
.LogicalToDeviceYRel( height
) );
5369 // subtract multi cell span area from clipping region for lines
5371 for ( int row
= topLeft
.GetRow(); row
<= bottomRight
.GetRow(); row
++ )
5373 for ( int col
= topLeft
.GetCol(); col
<= bottomRight
.GetCol(); col
++ )
5375 int cell_rows
, cell_cols
;
5376 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5377 if ( cell_rows
> 1 || cell_cols
> 1 ) // multi cell
5379 rect
= CellToRect( row
, col
);
5380 // cater for scaling
5381 // device origin already set in ::Render() for x, y
5382 rect
.x
= dc
.LogicalToDeviceX( rect
.x
);
5383 rect
.y
= dc
.LogicalToDeviceY( rect
.y
);
5384 rect
.width
= dc
.LogicalToDeviceXRel( rect
.width
);
5385 rect
.height
= dc
.LogicalToDeviceYRel( rect
.height
) - 1;
5386 clippedcells
.Subtract( rect
);
5388 else if ( cell_rows
< 0 || cell_cols
< 0 ) // part of multicell
5390 rect
= CellToRect( row
+ cell_rows
, col
+ cell_cols
);
5391 rect
.x
= dc
.LogicalToDeviceX( rect
.x
);
5392 rect
.y
= dc
.LogicalToDeviceY( rect
.y
);
5393 rect
.width
= dc
.LogicalToDeviceXRel( rect
.width
);
5394 rect
.height
= dc
.LogicalToDeviceYRel( rect
.height
) - 1;
5395 clippedcells
.Subtract( rect
);
5400 dc
.SetDeviceClippingRegion( clippedcells
);
5403 top
, left
, top
+ height
, left
+ width
,
5404 topLeft
.GetRow(), topLeft
.GetCol(),
5405 bottomRight
.GetRow(), bottomRight
.GetCol());
5407 dc
.DestroyClippingRegion();
5410 // This is used to redraw all grid lines e.g. when the grid line colour
5413 void wxGrid
::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5415 if ( !m_gridLinesEnabled
)
5418 int top
, bottom
, left
, right
;
5421 m_gridWin
->GetClientSize(&cw
, &ch
);
5422 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5423 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5425 // avoid drawing grid lines past the last row and col
5426 if ( m_gridLinesClipHorz
)
5431 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5432 if ( right
> lastColRight
)
5433 right
= lastColRight
;
5436 if ( m_gridLinesClipVert
)
5441 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5442 if ( bottom
> lastRowBottom
)
5443 bottom
= lastRowBottom
;
5446 // no gridlines inside multicells, clip them out
5447 int leftCol
= GetColPos( internalXToCol(left
) );
5448 int topRow
= internalYToRow(top
);
5449 int rightCol
= GetColPos( internalXToCol(right
) );
5450 int bottomRow
= internalYToRow(bottom
);
5452 wxRegion
clippedcells(0, 0, cw
, ch
);
5454 int cell_rows
, cell_cols
;
5457 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5459 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5461 int i
= GetColAt( colPos
);
5463 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5464 if ((cell_rows
> 1) || (cell_cols
> 1))
5466 rect
= CellToRect(j
,i
);
5467 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5468 clippedcells
.Subtract(rect
);
5470 else if ((cell_rows
< 0) || (cell_cols
< 0))
5472 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5473 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5474 clippedcells
.Subtract(rect
);
5479 dc
.SetDeviceClippingRegion( clippedcells
);
5482 top
, left
, bottom
, right
,
5483 topRow
, leftCol
, m_numRows
, m_numCols
);
5485 dc
.DestroyClippingRegion();
5489 wxGrid
::DoDrawGridLines(wxDC
& dc
,
5491 int bottom
, int right
,
5492 int topRow
, int leftCol
,
5493 int bottomRow
, int rightCol
)
5495 // horizontal grid lines
5496 for ( int i
= topRow
; i
< bottomRow
; i
++ )
5498 int bot
= GetRowBottom(i
) - 1;
5505 dc
.SetPen( GetRowGridLinePen(i
) );
5506 dc
.DrawLine( left
, bot
, right
, bot
);
5510 // vertical grid lines
5511 for ( int colPos
= leftCol
; colPos
< rightCol
; colPos
++ )
5513 int i
= GetColAt( colPos
);
5515 int colRight
= GetColRight(i
);
5517 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5521 if ( colRight
> right
)
5524 if ( colRight
>= left
)
5526 dc
.SetPen( GetColGridLinePen(i
) );
5527 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5532 void wxGrid
::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5537 const size_t numLabels
= rows
.GetCount();
5538 for ( size_t i
= 0; i
< numLabels
; i
++ )
5540 DrawRowLabel( dc
, rows
[i
] );
5544 void wxGrid
::DrawRowLabel( wxDC
& dc
, int row
)
5546 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5549 wxGridCellAttrProvider
* const
5550 attrProvider
= m_table ? m_table
->GetAttrProvider() : NULL
;
5552 // notice that an explicit static_cast is needed to avoid a compilation
5553 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5554 // wxGridRowHeaderRenderer class without it
5555 const wxGridRowHeaderRenderer
&
5556 rend
= attrProvider ? attrProvider
->GetRowHeaderRenderer(row
)
5557 : static_cast<const wxGridRowHeaderRenderer
&>
5558 (gs_defaultHeaderRenderers
.rowRenderer
);
5560 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5561 rend
.DrawBorder(*this, dc
, rect
);
5564 GetRowLabelAlignment(&hAlign
, &vAlign
);
5566 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5567 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5570 void wxGrid
::UseNativeColHeader(bool native
)
5572 if ( native
== m_useNativeHeader
)
5576 m_useNativeHeader
= native
;
5578 CreateColumnWindow();
5580 if ( m_useNativeHeader
)
5581 GetGridColHeader()->SetColumnCount(m_numCols
);
5585 void wxGrid
::SetUseNativeColLabels( bool native
)
5587 wxASSERT_MSG( !m_useNativeHeader
,
5588 "doesn't make sense when using native header" );
5590 m_nativeColumnLabels
= native
;
5593 int height
= wxRendererNative
::Get().GetHeaderButtonHeight( this );
5594 SetColLabelSize( height
);
5597 GetColLabelWindow()->Refresh();
5598 m_cornerLabelWin
->Refresh();
5601 void wxGrid
::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5606 const size_t numLabels
= cols
.GetCount();
5607 for ( size_t i
= 0; i
< numLabels
; i
++ )
5609 DrawColLabel( dc
, cols
[i
] );
5613 void wxGrid
::DrawCornerLabel(wxDC
& dc
)
5615 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5617 if ( m_nativeColumnLabels
)
5621 wxRendererNative
::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5628 wxGridCellAttrProvider
* const
5629 attrProvider
= m_table ? m_table
->GetAttrProvider() : NULL
;
5630 const wxGridCornerHeaderRenderer
&
5631 rend
= attrProvider ? attrProvider
->GetCornerRenderer()
5632 : static_cast<wxGridCornerHeaderRenderer
&>
5633 (gs_defaultHeaderRenderers
.cornerRenderer
);
5635 rend
.DrawBorder(*this, dc
, rect
);
5639 void wxGrid
::DrawColLabel(wxDC
& dc
, int col
)
5641 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5644 int colLeft
= GetColLeft(col
);
5646 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5647 wxGridCellAttrProvider
* const
5648 attrProvider
= m_table ? m_table
->GetAttrProvider() : NULL
;
5649 const wxGridColumnHeaderRenderer
&
5650 rend
= attrProvider ? attrProvider
->GetColumnHeaderRenderer(col
)
5651 : static_cast<wxGridColumnHeaderRenderer
&>
5652 (gs_defaultHeaderRenderers
.colRenderer
);
5654 if ( m_nativeColumnLabels
)
5656 wxRendererNative
::Get().DrawHeaderButton
5658 GetColLabelWindow(),
5663 ?
IsSortOrderAscending()
5664 ? wxHDR_SORT_ICON_UP
5665 : wxHDR_SORT_ICON_DOWN
5666 : wxHDR_SORT_ICON_NONE
5672 // It is reported that we need to erase the background to avoid display
5673 // artefacts, see #12055.
5674 wxDCBrushChanger
setBrush(dc
, m_colWindow
->GetBackgroundColour());
5675 dc
.DrawRectangle(rect
);
5677 rend
.DrawBorder(*this, dc
, rect
);
5681 GetColLabelAlignment(&hAlign
, &vAlign
);
5682 const int orient
= GetColLabelTextOrientation();
5684 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5687 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5688 // we just have to add textOrientation support
5689 void wxGrid
::DrawTextRectangle( wxDC
& dc
,
5690 const wxString
& value
,
5694 int textOrientation
) const
5696 wxArrayString lines
;
5698 StringToLines( value
, lines
);
5700 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5703 void wxGrid
::DrawTextRectangle(wxDC
& dc
,
5704 const wxArrayString
& lines
,
5708 int textOrientation
) const
5710 if ( lines
.empty() )
5713 wxDCClipper
clip(dc
, rect
);
5718 if ( textOrientation
== wxHORIZONTAL
)
5719 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5721 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5725 switch ( vertAlign
)
5727 case wxALIGN_BOTTOM
:
5728 if ( textOrientation
== wxHORIZONTAL
)
5729 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5731 x
= rect
.x
+ rect
.width
- textWidth
;
5734 case wxALIGN_CENTRE
:
5735 if ( textOrientation
== wxHORIZONTAL
)
5736 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5738 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5743 if ( textOrientation
== wxHORIZONTAL
)
5750 // Align each line of a multi-line label
5751 size_t nLines
= lines
.GetCount();
5752 for ( size_t l
= 0; l
< nLines
; l
++ )
5754 const wxString
& line
= lines
[l
];
5758 *(textOrientation
== wxHORIZONTAL ?
&y
: &x
) += dc
.GetCharHeight();
5762 wxCoord lineWidth
= 0,
5764 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5766 switch ( horizAlign
)
5769 if ( textOrientation
== wxHORIZONTAL
)
5770 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5772 y
= rect
.y
+ lineWidth
+ 1;
5775 case wxALIGN_CENTRE
:
5776 if ( textOrientation
== wxHORIZONTAL
)
5777 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5779 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5784 if ( textOrientation
== wxHORIZONTAL
)
5787 y
= rect
.y
+ rect
.height
- 1;
5791 if ( textOrientation
== wxHORIZONTAL
)
5793 dc
.DrawText( line
, x
, y
);
5798 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5804 // Split multi-line text up into an array of strings.
5805 // Any existing contents of the string array are preserved.
5807 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5808 void wxGrid
::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5812 wxString eol
= wxTextFile
::GetEOL( wxTextFileType_Unix
);
5813 wxString tVal
= wxTextFile
::Translate( value
, wxTextFileType_Unix
);
5815 while ( startPos
< (int)tVal
.length() )
5817 pos
= tVal
.Mid(startPos
).Find( eol
);
5822 else if ( pos
== 0 )
5824 lines
.Add( wxEmptyString
);
5828 lines
.Add( tVal
.Mid(startPos
, pos
) );
5831 startPos
+= pos
+ 1;
5834 if ( startPos
< (int)tVal
.length() )
5836 lines
.Add( tVal
.Mid( startPos
) );
5840 void wxGrid
::GetTextBoxSize( const wxDC
& dc
,
5841 const wxArrayString
& lines
,
5842 long *width
, long *height
) const
5846 wxCoord lineW
= 0, lineH
= 0;
5849 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5851 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5852 w
= wxMax( w
, lineW
);
5861 // ------ Batch processing.
5863 void wxGrid
::EndBatch()
5865 if ( m_batchCount
> 0 )
5868 if ( !m_batchCount
)
5871 m_rowLabelWin
->Refresh();
5872 m_colWindow
->Refresh();
5873 m_cornerLabelWin
->Refresh();
5874 m_gridWin
->Refresh();
5879 // Use this, rather than wxWindow::Refresh(), to force an immediate
5880 // repainting of the grid. Has no effect if you are already inside a
5881 // BeginBatch / EndBatch block.
5883 void wxGrid
::ForceRefresh()
5889 bool wxGrid
::Enable(bool enable
)
5891 if ( !wxScrolledWindow
::Enable(enable
) )
5894 // redraw in the new state
5895 m_gridWin
->Refresh();
5901 // ------ Edit control functions
5904 void wxGrid
::EnableEditing( bool edit
)
5906 if ( edit
!= m_editable
)
5909 EnableCellEditControl(edit
);
5914 void wxGrid
::EnableCellEditControl( bool enable
)
5919 if ( enable
!= m_cellEditCtrlEnabled
)
5923 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5926 // this should be checked by the caller!
5927 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5929 // do it before ShowCellEditControl()
5930 m_cellEditCtrlEnabled
= enable
;
5932 ShowCellEditControl();
5936 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5938 HideCellEditControl();
5939 SaveEditControlValue();
5941 // do it after HideCellEditControl()
5942 m_cellEditCtrlEnabled
= enable
;
5947 bool wxGrid
::IsCurrentCellReadOnly() const
5950 attr
= const_cast<wxGrid
*>(this)->GetCellAttr(m_currentCellCoords
);
5951 bool readonly
= attr
->IsReadOnly();
5957 bool wxGrid
::CanEnableCellControl() const
5959 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5960 !IsCurrentCellReadOnly();
5963 bool wxGrid
::IsCellEditControlEnabled() const
5965 // the cell edit control might be disable for all cells or just for the
5966 // current one if it's read only
5967 return m_cellEditCtrlEnabled ?
!IsCurrentCellReadOnly() : false;
5970 bool wxGrid
::IsCellEditControlShown() const
5972 bool isShown
= false;
5974 if ( m_cellEditCtrlEnabled
)
5976 int row
= m_currentCellCoords
.GetRow();
5977 int col
= m_currentCellCoords
.GetCol();
5978 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5979 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5984 if ( editor
->IsCreated() )
5986 isShown
= editor
->GetControl()->IsShown();
5996 void wxGrid
::ShowCellEditControl()
5998 if ( IsCellEditControlEnabled() )
6000 if ( !IsVisible( m_currentCellCoords
, false ) )
6002 m_cellEditCtrlEnabled
= false;
6007 wxRect rect
= CellToRect( m_currentCellCoords
);
6008 int row
= m_currentCellCoords
.GetRow();
6009 int col
= m_currentCellCoords
.GetCol();
6011 // if this is part of a multicell, find owner (topleft)
6012 int cell_rows
, cell_cols
;
6013 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6014 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6018 m_currentCellCoords
.SetRow( row
);
6019 m_currentCellCoords
.SetCol( col
);
6022 // erase the highlight and the cell contents because the editor
6023 // might not cover the entire cell
6024 wxClientDC
dc( m_gridWin
);
6026 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6027 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
6028 dc
.SetPen(*wxTRANSPARENT_PEN
);
6029 dc
.DrawRectangle(rect
);
6031 // convert to scrolled coords
6032 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6038 // cell is shifted by one pixel
6039 // However, don't allow x or y to become negative
6040 // since the SetSize() method interprets that as
6047 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6048 if ( !editor
->IsCreated() )
6050 editor
->Create(m_gridWin
, wxID_ANY
,
6051 new wxGridCellEditorEvtHandler(this, editor
));
6053 wxGridEditorCreatedEvent
evt(GetId(),
6054 wxEVT_GRID_EDITOR_CREATED
,
6058 editor
->GetControl());
6059 GetEventHandler()->ProcessEvent(evt
);
6062 // resize editor to overflow into righthand cells if allowed
6063 int maxWidth
= rect
.width
;
6064 wxString value
= GetCellValue(row
, col
);
6065 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
6068 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
6069 if (maxWidth
< rect
.width
)
6070 maxWidth
= rect
.width
;
6073 int client_right
= m_gridWin
->GetClientSize().GetWidth();
6074 if (rect
.x
+ maxWidth
> client_right
)
6075 maxWidth
= client_right
- rect
.x
;
6077 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
6079 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6080 // may have changed earlier
6081 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
6084 GetCellSize( row
, i
, &c_rows
, &c_cols
);
6086 // looks weird going over a multicell
6087 if (m_table
->IsEmptyCell( row
, i
) &&
6088 (rect
.width
< maxWidth
) && (c_rows
== 1))
6090 rect
.width
+= GetColWidth( i
);
6096 if (rect
.GetRight() > client_right
)
6097 rect
.SetRight( client_right
- 1 );
6100 editor
->SetCellAttr( attr
);
6101 editor
->SetSize( rect
);
6103 editor
->GetControl()->Move(
6104 editor
->GetControl()->GetPosition().x
+ nXMove
,
6105 editor
->GetControl()->GetPosition().y
);
6106 editor
->Show( true, attr
);
6108 // recalc dimensions in case we need to
6109 // expand the scrolled window to account for editor
6112 editor
->BeginEdit(row
, col
, this);
6113 editor
->SetCellAttr(NULL
);
6121 void wxGrid
::HideCellEditControl()
6123 if ( IsCellEditControlEnabled() )
6125 int row
= m_currentCellCoords
.GetRow();
6126 int col
= m_currentCellCoords
.GetCol();
6128 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6129 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6130 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6131 editor
->Show( false );
6135 // return the focus to the grid itself if the editor had it
6137 // note that we must not do this unconditionally to avoid stealing
6138 // focus from the window which just received it if we are hiding the
6139 // editor precisely because we lost focus
6140 if ( editorHadFocus
)
6141 m_gridWin
->SetFocus();
6143 // refresh whole row to the right
6144 wxRect
rect( CellToRect(row
, col
) );
6145 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6146 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6149 // ensure that the pixels under the focus ring get refreshed as well
6150 rect
.Inflate(10, 10);
6153 m_gridWin
->Refresh( false, &rect
);
6157 void wxGrid
::SaveEditControlValue()
6159 if ( IsCellEditControlEnabled() )
6161 int row
= m_currentCellCoords
.GetRow();
6162 int col
= m_currentCellCoords
.GetCol();
6164 wxString oldval
= GetCellValue(row
, col
);
6166 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6167 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6170 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6172 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6174 editor
->ApplyEdit(row
, col
, this);
6176 // for compatibility reasons dating back to wx 2.8 when this event
6177 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6178 // didn't exist we allow vetoing this one too
6179 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6181 // Event has been vetoed, set the data back.
6182 SetCellValue(row
, col
, oldval
);
6192 // ------ Grid location functions
6193 // Note that all of these functions work with the logical coordinates of
6194 // grid cells and labels so you will need to convert from device
6195 // coordinates for mouse events etc.
6198 wxGridCellCoords wxGrid
::XYToCell(int x
, int y
) const
6200 int row
= YToRow(y
);
6201 int col
= XToCol(x
);
6203 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6204 : wxGridCellCoords(row
, col
);
6207 // compute row or column from some (unscrolled) coordinate value, using either
6208 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6209 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6211 int wxGrid
::PosToLinePos(int coord
,
6213 const wxGridOperations
& oper
) const
6215 const int numLines
= oper
.GetNumberOfLines(this);
6218 return clipToMinMax
&& numLines
> 0 ?
0 : wxNOT_FOUND
;
6220 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6221 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6223 int maxPos
= coord
/ defaultLineSize
,
6226 // check for the simplest case: if we have no explicit line sizes
6227 // configured, then we already know the line this position falls in
6228 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6229 if ( lineEnds
.empty() )
6231 if ( maxPos
< numLines
)
6234 return clipToMinMax ? numLines
- 1 : -1;
6238 // adjust maxPos before starting the binary search
6239 if ( maxPos
>= numLines
)
6241 maxPos
= numLines
- 1;
6245 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6248 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6250 maxPos
= coord
/ minDist
;
6252 maxPos
= numLines
- 1;
6255 if ( maxPos
>= numLines
)
6256 maxPos
= numLines
- 1;
6259 // check if the position is beyond the last column
6260 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6261 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6262 return clipToMinMax ? maxPos
: -1;
6264 // or before the first one
6265 const int lineAt0
= oper
.GetLineAt(this, 0);
6266 if ( coord
< lineEnds
[lineAt0
] )
6270 // finally do perform the binary search
6271 while ( minPos
< maxPos
)
6273 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6274 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6276 "wxGrid: internal error in PosToLinePos()" );
6278 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6283 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6284 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6294 wxGrid
::PosToLine(int coord
,
6296 const wxGridOperations
& oper
) const
6298 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6300 return pos
== wxNOT_FOUND ? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6303 int wxGrid
::YToRow(int y
, bool clipToMinMax
) const
6305 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6308 int wxGrid
::XToCol(int x
, bool clipToMinMax
) const
6310 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6313 int wxGrid
::XToPos(int x
) const
6315 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6318 // return the row number such that the y coord is near the edge of, or -1 if
6319 // not near an edge.
6321 // notice that position can only possibly be near an edge if the row/column is
6322 // large enough to still allow for an "inner" area that is _not_ near the edge
6323 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6324 // _never_ be considered to be near the edge).
6325 int wxGrid
::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6327 const int line
= oper
.PosToLine(this, pos
, true);
6329 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6331 // We know that we are in this line, test whether we are close enough
6332 // to start or end border, respectively.
6333 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6335 else if ( line
> 0 &&
6336 pos
- oper
.GetLineStartPos(this,
6337 line
) < WXGRID_LABEL_EDGE_ZONE
)
6339 return oper
.GetLineBefore(this, line
);
6346 int wxGrid
::YToEdgeOfRow(int y
) const
6348 return PosToEdgeOfLine(y
, wxGridRowOperations());
6351 int wxGrid
::XToEdgeOfCol(int x
) const
6353 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6356 wxRect wxGrid
::CellToRect( int row
, int col
) const
6358 wxRect
rect( -1, -1, -1, -1 );
6360 if ( row
>= 0 && row
< m_numRows
&&
6361 col
>= 0 && col
< m_numCols
)
6363 int i
, cell_rows
, cell_cols
;
6364 rect
.width
= rect
.height
= 0;
6365 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6366 // if negative then find multicell owner
6371 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6373 rect
.x
= GetColLeft(col
);
6374 rect
.y
= GetRowTop(row
);
6375 for (i
=col
; i
< col
+ cell_cols
; i
++)
6376 rect
.width
+= GetColWidth(i
);
6377 for (i
=row
; i
< row
+ cell_rows
; i
++)
6378 rect
.height
+= GetRowHeight(i
);
6380 // if grid lines are enabled, then the area of the cell is a bit smaller
6381 if (m_gridLinesEnabled
)
6391 bool wxGrid
::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6393 // get the cell rectangle in logical coords
6395 wxRect
r( CellToRect( row
, col
) );
6397 // convert to device coords
6399 int left
, top
, right
, bottom
;
6400 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6401 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6403 // check against the client area of the grid window
6405 m_gridWin
->GetClientSize( &cw
, &ch
);
6407 if ( wholeCellVisible
)
6409 // is the cell wholly visible ?
6410 return ( left
>= 0 && right
<= cw
&&
6411 top
>= 0 && bottom
<= ch
);
6415 // is the cell partly visible ?
6417 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6418 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6422 // make the specified cell location visible by doing a minimal amount
6425 void wxGrid
::MakeCellVisible( int row
, int col
)
6428 int xpos
= -1, ypos
= -1;
6430 if ( row
>= 0 && row
< m_numRows
&&
6431 col
>= 0 && col
< m_numCols
)
6433 // get the cell rectangle in logical coords
6434 wxRect
r( CellToRect( row
, col
) );
6436 // convert to device coords
6437 int left
, top
, right
, bottom
;
6438 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6439 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6442 m_gridWin
->GetClientSize( &cw
, &ch
);
6448 else if ( bottom
> ch
)
6450 int h
= r
.GetHeight();
6452 for ( i
= row
- 1; i
>= 0; i
-- )
6454 int rowHeight
= GetRowHeight(i
);
6455 if ( h
+ rowHeight
> ch
)
6462 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6463 // have rounding errors (this is important, because if we do,
6464 // we might not scroll at all and some cells won't be redrawn)
6466 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6467 // so just add a full scroll unit...
6468 ypos
+= m_yScrollPixelsPerLine
;
6471 // special handling for wide cells - show always left part of the cell!
6472 // Otherwise, e.g. when stepping from row to row, it would jump between
6473 // left and right part of the cell on every step!
6475 if ( left
< 0 || (right
- left
) >= cw
)
6479 else if ( right
> cw
)
6481 // position the view so that the cell is on the right
6483 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6484 xpos
= x0
+ (right
- cw
);
6486 // see comment for ypos above
6487 xpos
+= m_xScrollPixelsPerLine
;
6490 if ( xpos
!= -1 || ypos
!= -1 )
6493 xpos
/= m_xScrollPixelsPerLine
;
6495 ypos
/= m_yScrollPixelsPerLine
;
6496 Scroll( xpos
, ypos
);
6503 // ------ Grid cursor movement functions
6507 wxGrid
::DoMoveCursor(bool expandSelection
,
6508 const wxGridDirectionOperations
& diroper
)
6510 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6513 if ( expandSelection
)
6515 wxGridCellCoords coords
= m_selectedBlockCorner
;
6516 if ( coords
== wxGridNoCellCoords
)
6517 coords
= m_currentCellCoords
;
6519 if ( diroper
.IsAtBoundary(coords
) )
6522 diroper
.Advance(coords
);
6524 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6526 else // don't expand selection
6530 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6533 wxGridCellCoords coords
= m_currentCellCoords
;
6534 diroper
.Advance(coords
);
6542 bool wxGrid
::MoveCursorUp(bool expandSelection
)
6544 return DoMoveCursor(expandSelection
,
6545 wxGridBackwardOperations(this, wxGridRowOperations()));
6548 bool wxGrid
::MoveCursorDown(bool expandSelection
)
6550 return DoMoveCursor(expandSelection
,
6551 wxGridForwardOperations(this, wxGridRowOperations()));
6554 bool wxGrid
::MoveCursorLeft(bool expandSelection
)
6556 return DoMoveCursor(expandSelection
,
6557 wxGridBackwardOperations(this, wxGridColumnOperations()));
6560 bool wxGrid
::MoveCursorRight(bool expandSelection
)
6562 return DoMoveCursor(expandSelection
,
6563 wxGridForwardOperations(this, wxGridColumnOperations()));
6566 bool wxGrid
::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6568 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6571 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6574 const int oldRow
= m_currentCellCoords
.GetRow();
6575 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6576 if ( newRow
== oldRow
)
6578 wxGridCellCoords
coords(m_currentCellCoords
);
6579 diroper
.Advance(coords
);
6580 newRow
= coords
.GetRow();
6583 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6588 bool wxGrid
::MovePageUp()
6590 return DoMoveCursorByPage(
6591 wxGridBackwardOperations(this, wxGridRowOperations()));
6594 bool wxGrid
::MovePageDown()
6596 return DoMoveCursorByPage(
6597 wxGridForwardOperations(this, wxGridRowOperations()));
6600 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6601 // until we find a non-empty cell or reach the grid end
6603 wxGrid
::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6604 const wxGridDirectionOperations
& diroper
)
6606 while ( !diroper
.IsAtBoundary(coords
) )
6608 diroper
.Advance(coords
);
6609 if ( !m_table
->IsEmpty(coords
) )
6615 wxGrid
::DoMoveCursorByBlock(bool expandSelection
,
6616 const wxGridDirectionOperations
& diroper
)
6618 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6621 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6624 wxGridCellCoords
coords(m_currentCellCoords
);
6625 if ( m_table
->IsEmpty(coords
) )
6627 // we are in an empty cell: find the next block of non-empty cells
6628 AdvanceToNextNonEmpty(coords
, diroper
);
6630 else // current cell is not empty
6632 diroper
.Advance(coords
);
6633 if ( m_table
->IsEmpty(coords
) )
6635 // we started at the end of a block, find the next one
6636 AdvanceToNextNonEmpty(coords
, diroper
);
6638 else // we're in a middle of a block
6640 // go to the end of it, i.e. find the last cell before the next
6642 while ( !diroper
.IsAtBoundary(coords
) )
6644 wxGridCellCoords
coordsNext(coords
);
6645 diroper
.Advance(coordsNext
);
6646 if ( m_table
->IsEmpty(coordsNext
) )
6649 coords
= coordsNext
;
6654 if ( expandSelection
)
6656 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6667 bool wxGrid
::MoveCursorUpBlock(bool expandSelection
)
6669 return DoMoveCursorByBlock(
6671 wxGridBackwardOperations(this, wxGridRowOperations())
6675 bool wxGrid
::MoveCursorDownBlock( bool expandSelection
)
6677 return DoMoveCursorByBlock(
6679 wxGridForwardOperations(this, wxGridRowOperations())
6683 bool wxGrid
::MoveCursorLeftBlock( bool expandSelection
)
6685 return DoMoveCursorByBlock(
6687 wxGridBackwardOperations(this, wxGridColumnOperations())
6691 bool wxGrid
::MoveCursorRightBlock( bool expandSelection
)
6693 return DoMoveCursorByBlock(
6695 wxGridForwardOperations(this, wxGridColumnOperations())
6700 // ------ Label values and formatting
6703 void wxGrid
::GetRowLabelAlignment( int *horiz
, int *vert
) const
6706 *horiz
= m_rowLabelHorizAlign
;
6708 *vert
= m_rowLabelVertAlign
;
6711 void wxGrid
::GetColLabelAlignment( int *horiz
, int *vert
) const
6714 *horiz
= m_colLabelHorizAlign
;
6716 *vert
= m_colLabelVertAlign
;
6719 int wxGrid
::GetColLabelTextOrientation() const
6721 return m_colLabelTextOrientation
;
6724 wxString wxGrid
::GetRowLabelValue( int row
) const
6728 return m_table
->GetRowLabelValue( row
);
6738 wxString wxGrid
::GetColLabelValue( int col
) const
6742 return m_table
->GetColLabelValue( col
);
6752 void wxGrid
::SetRowLabelSize( int width
)
6754 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6756 if ( width
== wxGRID_AUTOSIZE
)
6758 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6761 if ( width
!= m_rowLabelWidth
)
6765 m_rowLabelWin
->Show( false );
6766 m_cornerLabelWin
->Show( false );
6768 else if ( m_rowLabelWidth
== 0 )
6770 m_rowLabelWin
->Show( true );
6771 if ( m_colLabelHeight
> 0 )
6772 m_cornerLabelWin
->Show( true );
6775 m_rowLabelWidth
= width
;
6777 wxScrolledWindow
::Refresh( true );
6781 void wxGrid
::SetColLabelSize( int height
)
6783 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6785 if ( height
== wxGRID_AUTOSIZE
)
6787 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6790 if ( height
!= m_colLabelHeight
)
6794 m_colWindow
->Show( false );
6795 m_cornerLabelWin
->Show( false );
6797 else if ( m_colLabelHeight
== 0 )
6799 m_colWindow
->Show( true );
6800 if ( m_rowLabelWidth
> 0 )
6801 m_cornerLabelWin
->Show( true );
6804 m_colLabelHeight
= height
;
6806 wxScrolledWindow
::Refresh( true );
6810 void wxGrid
::SetLabelBackgroundColour( const wxColour
& colour
)
6812 if ( m_labelBackgroundColour
!= colour
)
6814 m_labelBackgroundColour
= colour
;
6815 m_rowLabelWin
->SetBackgroundColour( colour
);
6816 m_colWindow
->SetBackgroundColour( colour
);
6817 m_cornerLabelWin
->SetBackgroundColour( colour
);
6819 if ( !GetBatchCount() )
6821 m_rowLabelWin
->Refresh();
6822 m_colWindow
->Refresh();
6823 m_cornerLabelWin
->Refresh();
6828 void wxGrid
::SetLabelTextColour( const wxColour
& colour
)
6830 if ( m_labelTextColour
!= colour
)
6832 m_labelTextColour
= colour
;
6833 if ( !GetBatchCount() )
6835 m_rowLabelWin
->Refresh();
6836 m_colWindow
->Refresh();
6841 void wxGrid
::SetLabelFont( const wxFont
& font
)
6844 if ( !GetBatchCount() )
6846 m_rowLabelWin
->Refresh();
6847 m_colWindow
->Refresh();
6851 void wxGrid
::SetRowLabelAlignment( int horiz
, int vert
)
6853 // allow old (incorrect) defs to be used
6856 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6857 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6858 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6863 case wxTOP
: vert
= wxALIGN_TOP
; break;
6864 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6865 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6868 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6870 m_rowLabelHorizAlign
= horiz
;
6873 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6875 m_rowLabelVertAlign
= vert
;
6878 if ( !GetBatchCount() )
6880 m_rowLabelWin
->Refresh();
6884 void wxGrid
::SetColLabelAlignment( int horiz
, int vert
)
6886 // allow old (incorrect) defs to be used
6889 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6890 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6891 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6896 case wxTOP
: vert
= wxALIGN_TOP
; break;
6897 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6898 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6901 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6903 m_colLabelHorizAlign
= horiz
;
6906 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6908 m_colLabelVertAlign
= vert
;
6911 if ( !GetBatchCount() )
6913 m_colWindow
->Refresh();
6917 // Note: under MSW, the default column label font must be changed because it
6918 // does not support vertical printing
6920 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6921 // pGrid->SetLabelFont(font);
6922 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6924 void wxGrid
::SetColLabelTextOrientation( int textOrientation
)
6926 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6927 m_colLabelTextOrientation
= textOrientation
;
6929 if ( !GetBatchCount() )
6930 m_colWindow
->Refresh();
6933 void wxGrid
::SetRowLabelValue( int row
, const wxString
& s
)
6937 m_table
->SetRowLabelValue( row
, s
);
6938 if ( !GetBatchCount() )
6940 wxRect rect
= CellToRect( row
, 0 );
6941 if ( rect
.height
> 0 )
6943 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6945 rect
.width
= m_rowLabelWidth
;
6946 m_rowLabelWin
->Refresh( true, &rect
);
6952 void wxGrid
::SetColLabelValue( int col
, const wxString
& s
)
6956 m_table
->SetColLabelValue( col
, s
);
6957 if ( !GetBatchCount() )
6959 if ( m_useNativeHeader
)
6961 GetGridColHeader()->UpdateColumn(col
);
6965 wxRect rect
= CellToRect( 0, col
);
6966 if ( rect
.width
> 0 )
6968 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6970 rect
.height
= m_colLabelHeight
;
6971 GetColLabelWindow()->Refresh( true, &rect
);
6978 void wxGrid
::SetGridLineColour( const wxColour
& colour
)
6980 if ( m_gridLineColour
!= colour
)
6982 m_gridLineColour
= colour
;
6984 if ( GridLinesEnabled() )
6989 void wxGrid
::SetCellHighlightColour( const wxColour
& colour
)
6991 if ( m_cellHighlightColour
!= colour
)
6993 m_cellHighlightColour
= colour
;
6995 wxClientDC
dc( m_gridWin
);
6997 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6998 DrawCellHighlight(dc
, attr
);
7003 void wxGrid
::SetCellHighlightPenWidth(int width
)
7005 if (m_cellHighlightPenWidth
!= width
)
7007 m_cellHighlightPenWidth
= width
;
7009 // Just redrawing the cell highlight is not enough since that won't
7010 // make any visible change if the thickness is getting smaller.
7011 int row
= m_currentCellCoords
.GetRow();
7012 int col
= m_currentCellCoords
.GetCol();
7013 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7016 wxRect rect
= CellToRect(row
, col
);
7017 m_gridWin
->Refresh(true, &rect
);
7021 void wxGrid
::SetCellHighlightROPenWidth(int width
)
7023 if (m_cellHighlightROPenWidth
!= width
)
7025 m_cellHighlightROPenWidth
= width
;
7027 // Just redrawing the cell highlight is not enough since that won't
7028 // make any visible change if the thickness is getting smaller.
7029 int row
= m_currentCellCoords
.GetRow();
7030 int col
= m_currentCellCoords
.GetCol();
7031 if ( row
== -1 || col
== -1 ||
7032 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7035 wxRect rect
= CellToRect(row
, col
);
7036 m_gridWin
->Refresh(true, &rect
);
7040 void wxGrid
::RedrawGridLines()
7042 // the lines will be redrawn when the window is thawn
7043 if ( GetBatchCount() )
7046 if ( GridLinesEnabled() )
7048 wxClientDC
dc( m_gridWin
);
7050 DrawAllGridLines( dc
, wxRegion() );
7052 else // remove the grid lines
7054 m_gridWin
->Refresh();
7058 void wxGrid
::EnableGridLines( bool enable
)
7060 if ( enable
!= m_gridLinesEnabled
)
7062 m_gridLinesEnabled
= enable
;
7068 void wxGrid
::DoClipGridLines(bool& var
, bool clip
)
7074 if ( GridLinesEnabled() )
7079 int wxGrid
::GetDefaultRowSize() const
7081 return m_defaultRowHeight
;
7084 int wxGrid
::GetRowSize( int row
) const
7086 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
7088 return GetRowHeight(row
);
7091 int wxGrid
::GetDefaultColSize() const
7093 return m_defaultColWidth
;
7096 int wxGrid
::GetColSize( int col
) const
7098 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
7100 return GetColWidth(col
);
7103 // ============================================================================
7104 // access to the grid attributes: each of them has a default value in the grid
7105 // itself and may be overidden on a per-cell basis
7106 // ============================================================================
7108 // ----------------------------------------------------------------------------
7109 // setting default attributes
7110 // ----------------------------------------------------------------------------
7112 void wxGrid
::SetDefaultCellBackgroundColour( const wxColour
& col
)
7114 m_defaultCellAttr
->SetBackgroundColour(col
);
7116 m_gridWin
->SetBackgroundColour(col
);
7120 void wxGrid
::SetDefaultCellTextColour( const wxColour
& col
)
7122 m_defaultCellAttr
->SetTextColour(col
);
7125 void wxGrid
::SetDefaultCellAlignment( int horiz
, int vert
)
7127 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7130 void wxGrid
::SetDefaultCellOverflow( bool allow
)
7132 m_defaultCellAttr
->SetOverflow(allow
);
7135 void wxGrid
::SetDefaultCellFont( const wxFont
& font
)
7137 m_defaultCellAttr
->SetFont(font
);
7140 // For editors and renderers the type registry takes precedence over the
7141 // default attr, so we need to register the new editor/renderer for the string
7142 // data type in order to make setting a default editor/renderer appear to
7145 void wxGrid
::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7147 RegisterDataType(wxGRID_VALUE_STRING
,
7149 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7152 void wxGrid
::SetDefaultEditor(wxGridCellEditor
*editor
)
7154 RegisterDataType(wxGRID_VALUE_STRING
,
7155 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7159 // ----------------------------------------------------------------------------
7160 // access to the default attributes
7161 // ----------------------------------------------------------------------------
7163 wxColour wxGrid
::GetDefaultCellBackgroundColour() const
7165 return m_defaultCellAttr
->GetBackgroundColour();
7168 wxColour wxGrid
::GetDefaultCellTextColour() const
7170 return m_defaultCellAttr
->GetTextColour();
7173 wxFont wxGrid
::GetDefaultCellFont() const
7175 return m_defaultCellAttr
->GetFont();
7178 void wxGrid
::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7180 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7183 bool wxGrid
::GetDefaultCellOverflow() const
7185 return m_defaultCellAttr
->GetOverflow();
7188 wxGridCellRenderer
*wxGrid
::GetDefaultRenderer() const
7190 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7193 wxGridCellEditor
*wxGrid
::GetDefaultEditor() const
7195 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7198 // ----------------------------------------------------------------------------
7199 // access to cell attributes
7200 // ----------------------------------------------------------------------------
7202 wxColour wxGrid
::GetCellBackgroundColour(int row
, int col
) const
7204 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7205 wxColour colour
= attr
->GetBackgroundColour();
7211 wxColour wxGrid
::GetCellTextColour( int row
, int col
) const
7213 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7214 wxColour colour
= attr
->GetTextColour();
7220 wxFont wxGrid
::GetCellFont( int row
, int col
) const
7222 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7223 wxFont font
= attr
->GetFont();
7229 void wxGrid
::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7231 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7232 attr
->GetAlignment(horiz
, vert
);
7236 bool wxGrid
::GetCellOverflow( int row
, int col
) const
7238 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7239 bool allow
= attr
->GetOverflow();
7246 wxGrid
::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7248 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7249 attr
->GetSize( num_rows
, num_cols
);
7252 if ( *num_rows
== 1 && *num_cols
== 1 )
7253 return CellSpan_None
; // just a normal cell
7255 if ( *num_rows
< 0 || *num_cols
< 0 )
7256 return CellSpan_Inside
; // covered by a multi-span cell
7258 // this cell spans multiple cells to its right/bottom
7259 return CellSpan_Main
;
7262 wxGridCellRenderer
* wxGrid
::GetCellRenderer(int row
, int col
) const
7264 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7265 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7271 wxGridCellEditor
* wxGrid
::GetCellEditor(int row
, int col
) const
7273 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7274 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7280 bool wxGrid
::IsReadOnly(int row
, int col
) const
7282 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7283 bool isReadOnly
= attr
->IsReadOnly();
7289 // ----------------------------------------------------------------------------
7290 // attribute support: cache, automatic provider creation, ...
7291 // ----------------------------------------------------------------------------
7293 bool wxGrid
::CanHaveAttributes() const
7300 return m_table
->CanHaveAttributes();
7303 void wxGrid
::ClearAttrCache()
7305 if ( m_attrCache
.row
!= -1 )
7307 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7308 m_attrCache
.attr
= NULL
;
7309 m_attrCache
.row
= -1;
7310 // wxSafeDecRec(...) might cause event processing that accesses
7311 // the cached attribute, if one exists (e.g. by deleting the
7312 // editor stored within the attribute). Therefore it is important
7313 // to invalidate the cache before calling wxSafeDecRef!
7314 wxSafeDecRef(oldAttr
);
7318 void wxGrid
::RefreshAttr(int row
, int col
)
7320 if ( m_attrCache
.row
== row
&& m_attrCache
.col
== col
)
7325 void wxGrid
::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7329 wxGrid
* const self
= const_cast<wxGrid
*>(this);
7331 self
->ClearAttrCache();
7332 self
->m_attrCache
.row
= row
;
7333 self
->m_attrCache
.col
= col
;
7334 self
->m_attrCache
.attr
= attr
;
7339 bool wxGrid
::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7341 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7343 *attr
= m_attrCache
.attr
;
7344 wxSafeIncRef(m_attrCache
.attr
);
7346 #ifdef DEBUG_ATTR_CACHE
7347 gs_nAttrCacheHits
++;
7354 #ifdef DEBUG_ATTR_CACHE
7355 gs_nAttrCacheMisses
++;
7362 wxGridCellAttr
*wxGrid
::GetCellAttr(int row
, int col
) const
7364 wxGridCellAttr
*attr
= NULL
;
7365 // Additional test to avoid looking at the cache e.g. for
7366 // wxNoCellCoords, as this will confuse memory management.
7369 if ( !LookupAttr(row
, col
, &attr
) )
7371 attr
= m_table ? m_table
->GetAttr(row
, col
, wxGridCellAttr
::Any
)
7373 CacheAttr(row
, col
, attr
);
7379 attr
->SetDefAttr(m_defaultCellAttr
);
7383 attr
= m_defaultCellAttr
;
7390 wxGridCellAttr
*wxGrid
::GetOrCreateCellAttr(int row
, int col
) const
7392 wxGridCellAttr
*attr
= NULL
;
7393 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7395 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7396 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7398 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr
::Cell
);
7401 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7403 // artificially inc the ref count to match DecRef() in caller
7405 m_table
->SetAttr(attr
, row
, col
);
7411 // ----------------------------------------------------------------------------
7412 // setting column attributes (wrappers around SetColAttr)
7413 // ----------------------------------------------------------------------------
7415 void wxGrid
::SetColFormatBool(int col
)
7417 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7420 void wxGrid
::SetColFormatNumber(int col
)
7422 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7425 void wxGrid
::SetColFormatFloat(int col
, int width
, int precision
)
7427 wxString typeName
= wxGRID_VALUE_FLOAT
;
7428 if ( (width
!= -1) || (precision
!= -1) )
7430 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7433 SetColFormatCustom(col
, typeName
);
7436 void wxGrid
::SetColFormatCustom(int col
, const wxString
& typeName
)
7438 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr
::Col
);
7440 attr
= new wxGridCellAttr
;
7441 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7442 attr
->SetRenderer(renderer
);
7443 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7444 attr
->SetEditor(editor
);
7446 SetColAttr(col
, attr
);
7450 // ----------------------------------------------------------------------------
7451 // setting cell attributes: this is forwarded to the table
7452 // ----------------------------------------------------------------------------
7454 void wxGrid
::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7456 if ( CanHaveAttributes() )
7458 m_table
->SetAttr(attr
, row
, col
);
7467 void wxGrid
::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7469 if ( CanHaveAttributes() )
7471 m_table
->SetRowAttr(attr
, row
);
7480 void wxGrid
::SetColAttr(int col
, wxGridCellAttr
*attr
)
7482 if ( CanHaveAttributes() )
7484 m_table
->SetColAttr(attr
, col
);
7493 void wxGrid
::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7495 if ( CanHaveAttributes() )
7497 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7498 attr
->SetBackgroundColour(colour
);
7503 void wxGrid
::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7505 if ( CanHaveAttributes() )
7507 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7508 attr
->SetTextColour(colour
);
7513 void wxGrid
::SetCellFont( int row
, int col
, const wxFont
& font
)
7515 if ( CanHaveAttributes() )
7517 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7518 attr
->SetFont(font
);
7523 void wxGrid
::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7525 if ( CanHaveAttributes() )
7527 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7528 attr
->SetAlignment(horiz
, vert
);
7533 void wxGrid
::SetCellOverflow( int row
, int col
, bool allow
)
7535 if ( CanHaveAttributes() )
7537 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7538 attr
->SetOverflow(allow
);
7543 void wxGrid
::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7545 if ( CanHaveAttributes() )
7547 int cell_rows
, cell_cols
;
7549 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7550 attr
->GetSize(&cell_rows
, &cell_cols
);
7551 attr
->SetSize(num_rows
, num_cols
);
7554 // Cannot set the size of a cell to 0 or negative values
7555 // While it is perfectly legal to do that, this function cannot
7556 // handle all the possibilies, do it by hand by getting the CellAttr.
7557 // You can only set the size of a cell to 1,1 or greater with this fn
7558 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7559 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7560 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7561 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7563 // if this was already a multicell then "turn off" the other cells first
7564 if ((cell_rows
> 1) || (cell_cols
> 1))
7567 for (j
=row
; j
< row
+ cell_rows
; j
++)
7569 for (i
=col
; i
< col
+ cell_cols
; i
++)
7571 if ((i
!= col
) || (j
!= row
))
7573 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7574 attr_stub
->SetSize( 1, 1 );
7575 attr_stub
->DecRef();
7581 // mark the cells that will be covered by this cell to
7582 // negative or zero values to point back at this cell
7583 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7586 for (j
=row
; j
< row
+ num_rows
; j
++)
7588 for (i
=col
; i
< col
+ num_cols
; i
++)
7590 if ((i
!= col
) || (j
!= row
))
7592 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7593 attr_stub
->SetSize( row
- j
, col
- i
);
7594 attr_stub
->DecRef();
7602 void wxGrid
::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7604 if ( CanHaveAttributes() )
7606 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7607 attr
->SetRenderer(renderer
);
7612 void wxGrid
::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7614 if ( CanHaveAttributes() )
7616 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7617 attr
->SetEditor(editor
);
7622 void wxGrid
::SetReadOnly(int row
, int col
, bool isReadOnly
)
7624 if ( CanHaveAttributes() )
7626 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7627 attr
->SetReadOnly(isReadOnly
);
7632 // ----------------------------------------------------------------------------
7633 // Data type registration
7634 // ----------------------------------------------------------------------------
7636 void wxGrid
::RegisterDataType(const wxString
& typeName
,
7637 wxGridCellRenderer
* renderer
,
7638 wxGridCellEditor
* editor
)
7640 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7644 wxGridCellEditor
* wxGrid
::GetDefaultEditorForCell(int row
, int col
) const
7646 wxString typeName
= m_table
->GetTypeName(row
, col
);
7647 return GetDefaultEditorForType(typeName
);
7650 wxGridCellRenderer
* wxGrid
::GetDefaultRendererForCell(int row
, int col
) const
7652 wxString typeName
= m_table
->GetTypeName(row
, col
);
7653 return GetDefaultRendererForType(typeName
);
7656 wxGridCellEditor
* wxGrid
::GetDefaultEditorForType(const wxString
& typeName
) const
7658 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7659 if ( index
== wxNOT_FOUND
)
7661 wxFAIL_MSG(wxString
::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7666 return m_typeRegistry
->GetEditor(index
);
7669 wxGridCellRenderer
* wxGrid
::GetDefaultRendererForType(const wxString
& typeName
) const
7671 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7672 if ( index
== wxNOT_FOUND
)
7674 wxFAIL_MSG(wxString
::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7679 return m_typeRegistry
->GetRenderer(index
);
7682 // ----------------------------------------------------------------------------
7684 // ----------------------------------------------------------------------------
7686 void wxGrid
::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7690 setFixed
= new wxGridFixedIndicesSet
;
7693 setFixed
->insert(line
);
7697 wxGrid
::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7699 return !setFixed
|| !setFixed
->count(line
);
7702 void wxGrid
::EnableDragRowSize( bool enable
)
7704 m_canDragRowSize
= enable
;
7707 void wxGrid
::EnableDragColSize( bool enable
)
7709 m_canDragColSize
= enable
;
7712 void wxGrid
::EnableDragGridSize( bool enable
)
7714 m_canDragGridSize
= enable
;
7717 void wxGrid
::EnableDragCell( bool enable
)
7719 m_canDragCell
= enable
;
7722 void wxGrid
::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7724 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7726 if ( resizeExistingRows
)
7728 // since we are resizing all rows to the default row size,
7729 // we can simply clear the row heights and row bottoms
7730 // arrays (which also allows us to take advantage of
7731 // some speed optimisations)
7732 m_rowHeights
.Empty();
7733 m_rowBottoms
.Empty();
7734 if ( !GetBatchCount() )
7739 void wxGrid
::SetRowSize( int row
, int height
)
7741 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7743 // if < 0 then calculate new height from label
7747 wxArrayString lines
;
7748 wxClientDC
dc(m_rowLabelWin
);
7749 dc
.SetFont(GetLabelFont());
7750 StringToLines(GetRowLabelValue( row
), lines
);
7751 GetTextBoxSize( dc
, lines
, &w
, &h
);
7752 //check that it is not less than the minimal height
7753 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7756 // See comment in SetColSize
7757 if ( height
> 0 && height
< GetRowMinimalAcceptableHeight())
7760 if ( m_rowHeights
.IsEmpty() )
7762 // need to really create the array
7766 int h
= wxMax( 0, height
);
7767 int diff
= h
- m_rowHeights
[row
];
7769 m_rowHeights
[row
] = h
;
7770 for ( int i
= row
; i
< m_numRows
; i
++ )
7772 m_rowBottoms
[i
] += diff
;
7775 if ( !GetBatchCount() )
7782 void wxGrid
::SetDefaultColSize( int width
, bool resizeExistingCols
)
7784 // we dont allow zero default column width
7785 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7787 if ( resizeExistingCols
)
7789 // since we are resizing all columns to the default column size,
7790 // we can simply clear the col widths and col rights
7791 // arrays (which also allows us to take advantage of
7792 // some speed optimisations)
7793 m_colWidths
.Empty();
7794 m_colRights
.Empty();
7795 if ( !GetBatchCount() )
7800 void wxGrid
::SetColSize( int col
, int width
)
7802 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7804 // if < 0 then calculate new width from label
7808 wxArrayString lines
;
7809 wxClientDC
dc(m_colWindow
);
7810 dc
.SetFont(GetLabelFont());
7811 StringToLines(GetColLabelValue(col
), lines
);
7812 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7813 GetTextBoxSize( dc
, lines
, &w
, &h
);
7815 GetTextBoxSize( dc
, lines
, &h
, &w
);
7817 //check that it is not less than the minimal width
7818 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7821 // we intentionally don't test whether the width is less than
7822 // GetColMinimalWidth() here but we do compare it with
7823 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7824 // #651) -- and we also always allow the width of 0 as it has the special
7825 // sense of hiding the column
7826 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7829 if ( m_colWidths
.IsEmpty() )
7831 // need to really create the array
7835 const int diff
= width
- m_colWidths
[col
];
7836 m_colWidths
[col
] = width
;
7837 if ( m_useNativeHeader
)
7838 GetGridColHeader()->UpdateColumn(col
);
7839 //else: will be refreshed when the header is redrawn
7841 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7843 m_colRights
[GetColAt(colPos
)] += diff
;
7846 if ( !GetBatchCount() )
7853 void wxGrid
::SetColMinimalWidth( int col
, int width
)
7855 if (width
> GetColMinimalAcceptableWidth())
7857 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)col
;
7858 m_colMinWidths
[key
] = width
;
7862 void wxGrid
::SetRowMinimalHeight( int row
, int width
)
7864 if (width
> GetRowMinimalAcceptableHeight())
7866 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)row
;
7867 m_rowMinHeights
[key
] = width
;
7871 int wxGrid
::GetColMinimalWidth(int col
) const
7873 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)col
;
7874 wxLongToLongHashMap
::const_iterator it
= m_colMinWidths
.find(key
);
7876 return it
!= m_colMinWidths
.end() ?
(int)it
->second
: m_minAcceptableColWidth
;
7879 int wxGrid
::GetRowMinimalHeight(int row
) const
7881 wxLongToLongHashMap
::key_type key
= (wxLongToLongHashMap
::key_type
)row
;
7882 wxLongToLongHashMap
::const_iterator it
= m_rowMinHeights
.find(key
);
7884 return it
!= m_rowMinHeights
.end() ?
(int)it
->second
: m_minAcceptableRowHeight
;
7887 void wxGrid
::SetColMinimalAcceptableWidth( int width
)
7889 // We do allow a width of 0 since this gives us
7890 // an easy way to temporarily hiding columns.
7892 m_minAcceptableColWidth
= width
;
7895 void wxGrid
::SetRowMinimalAcceptableHeight( int height
)
7897 // We do allow a height of 0 since this gives us
7898 // an easy way to temporarily hiding rows.
7900 m_minAcceptableRowHeight
= height
;
7903 int wxGrid
::GetColMinimalAcceptableWidth() const
7905 return m_minAcceptableColWidth
;
7908 int wxGrid
::GetRowMinimalAcceptableHeight() const
7910 return m_minAcceptableRowHeight
;
7913 // ----------------------------------------------------------------------------
7915 // ----------------------------------------------------------------------------
7918 wxGrid
::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7920 const bool column
= direction
== wxGRID_COLUMN
;
7922 wxClientDC
dc(m_gridWin
);
7924 // cancel editing of cell
7925 HideCellEditControl();
7926 SaveEditControlValue();
7928 // initialize both of them just to avoid compiler warnings even if only
7929 // really needs to be initialized here
7943 wxCoord extent
, extentMax
= 0;
7944 int max
= column ? m_numRows
: m_numCols
;
7945 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7952 // we need to account for the cells spanning multiple columns/rows:
7953 // while they may need a lot of space, they don't need all of it in
7955 int numRows
, numCols
;
7956 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
7957 if ( span
== CellSpan_Inside
)
7959 // we need to get the size of the main cell, not of a cell hidden
7964 // get the size of the main cell too
7965 GetCellSize(row
, col
, &numRows
, &numCols
);
7968 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7969 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7972 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7973 extent
= column ? size
.x
: size
.y
;
7975 if ( span
!= CellSpan_None
)
7977 // we spread the size of a spanning cell over all the cells it
7978 // covers evenly -- this is probably not ideal but we can't
7979 // really do much better here
7981 // notice that numCols and numRows are never 0 as they
7982 // correspond to the size of the main cell of the span and not
7983 // of the cell inside it
7984 extent
/= column ? numCols
: numRows
;
7987 if ( extent
> extentMax
)
7996 // now also compare with the column label extent
7998 dc
.SetFont( GetLabelFont() );
8002 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
8003 if ( GetColLabelTextOrientation() == wxVERTICAL
)
8007 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
8009 extent
= column ? w
: h
;
8010 if ( extent
> extentMax
)
8015 // empty column - give default extent (notice that if extentMax is less
8016 // than default extent but != 0, it's OK)
8017 extentMax
= column ? m_defaultColWidth
: m_defaultRowHeight
;
8022 // leave some space around text
8030 // Ensure automatic width is not less than minimal width. See the
8031 // comment in SetColSize() for explanation of why this isn't done
8034 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
8036 SetColSize( col
, extentMax
);
8037 if ( !GetBatchCount() )
8039 if ( m_useNativeHeader
)
8041 GetGridColHeader()->UpdateColumn(col
);
8046 m_gridWin
->GetClientSize( &cw
, &ch
);
8047 wxRect
rect ( CellToRect( 0, col
) );
8049 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8050 rect
.width
= cw
- rect
.x
;
8051 rect
.height
= m_colLabelHeight
;
8052 GetColLabelWindow()->Refresh( true, &rect
);
8058 // Ensure automatic width is not less than minimal height. See the
8059 // comment in SetColSize() for explanation of why this isn't done
8062 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
8064 SetRowSize(row
, extentMax
);
8065 if ( !GetBatchCount() )
8068 m_gridWin
->GetClientSize( &cw
, &ch
);
8069 wxRect
rect( CellToRect( row
, 0 ) );
8071 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8072 rect
.width
= m_rowLabelWidth
;
8073 rect
.height
= ch
- rect
.y
;
8074 m_rowLabelWin
->Refresh( true, &rect
);
8081 SetColMinimalWidth(col
, extentMax
);
8083 SetRowMinimalHeight(row
, extentMax
);
8087 wxCoord wxGrid
::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
8089 // calculate size for the rows or columns?
8090 const bool calcRows
= direction
== wxGRID_ROW
;
8092 wxClientDC
dc(calcRows ?
GetGridRowLabelWindow()
8093 : GetGridColLabelWindow());
8094 dc
.SetFont(GetLabelFont());
8096 // which dimension should we take into account for calculations?
8098 // for columns, the text can be only horizontal so it's easy but for rows
8099 // we also have to take into account the text orientation
8101 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
8103 wxArrayString lines
;
8104 wxCoord extentMax
= 0;
8106 const int numRowsOrCols
= calcRows ? m_numRows
: m_numCols
;
8107 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
8111 wxString label
= calcRows ?
GetRowLabelValue(rowOrCol
)
8112 : GetColLabelValue(rowOrCol
);
8113 StringToLines(label
, lines
);
8116 GetTextBoxSize(dc
, lines
, &w
, &h
);
8118 const wxCoord extent
= useWidth ? w
: h
;
8119 if ( extent
> extentMax
)
8125 // empty column - give default extent (notice that if extentMax is less
8126 // than default extent but != 0, it's OK)
8127 extentMax
= calcRows ?
GetDefaultRowLabelSize()
8128 : GetDefaultColLabelSize();
8131 // leave some space around text (taken from AutoSizeColOrRow)
8140 int wxGrid
::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8142 int width
= m_rowLabelWidth
;
8144 wxGridUpdateLocker locker
;
8146 locker
.Create(this);
8148 for ( int col
= 0; col
< m_numCols
; col
++ )
8151 AutoSizeColumn(col
, setAsMin
);
8153 width
+= GetColWidth(col
);
8159 int wxGrid
::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8161 int height
= m_colLabelHeight
;
8163 wxGridUpdateLocker locker
;
8165 locker
.Create(this);
8167 for ( int row
= 0; row
< m_numRows
; row
++ )
8170 AutoSizeRow(row
, setAsMin
);
8172 height
+= GetRowHeight(row
);
8178 void wxGrid
::AutoSize()
8180 wxGridUpdateLocker
locker(this);
8182 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8183 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8185 // we know that we're not going to have scrollbars so disable them now to
8186 // avoid trouble in SetClientSize() which can otherwise set the correct
8187 // client size but also leave space for (not needed any more) scrollbars
8188 SetScrollbars(m_xScrollPixelsPerLine
, m_yScrollPixelsPerLine
,
8191 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8194 void wxGrid
::AutoSizeRowLabelSize( int row
)
8196 // Hide the edit control, so it
8197 // won't interfere with drag-shrinking.
8198 if ( IsCellEditControlShown() )
8200 HideCellEditControl();
8201 SaveEditControlValue();
8204 // autosize row height depending on label text
8205 SetRowSize(row
, -1);
8209 void wxGrid
::AutoSizeColLabelSize( int col
)
8211 // Hide the edit control, so it
8212 // won't interfere with drag-shrinking.
8213 if ( IsCellEditControlShown() )
8215 HideCellEditControl();
8216 SaveEditControlValue();
8219 // autosize column width depending on label text
8220 SetColSize(col
, -1);
8224 wxSize wxGrid
::DoGetBestSize() const
8226 wxGrid
* const self
= const_cast<wxGrid
*>(this);
8228 // we do the same as in AutoSize() here with the exception that we don't
8229 // change the column/row sizes, only calculate them
8230 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8231 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8233 // NOTE: This size should be cached, but first we need to add calls to
8234 // InvalidateBestSize everywhere that could change the results of this
8236 // CacheBestSize(size);
8238 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8239 + GetWindowBorderSize();
8247 #if WXWIN_COMPATIBILITY_2_8
8248 wxPen
& wxGrid
::GetDividerPen() const
8252 #endif // WXWIN_COMPATIBILITY_2_8
8254 // ----------------------------------------------------------------------------
8255 // cell value accessor functions
8256 // ----------------------------------------------------------------------------
8258 void wxGrid
::SetCellValue( int row
, int col
, const wxString
& s
)
8262 m_table
->SetValue( row
, col
, s
);
8263 if ( !GetBatchCount() )
8266 wxRect
rect( CellToRect( row
, col
) );
8268 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8269 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8270 m_gridWin
->Refresh( false, &rect
);
8273 if ( m_currentCellCoords
.GetRow() == row
&&
8274 m_currentCellCoords
.GetCol() == col
&&
8275 IsCellEditControlShown())
8276 // Note: If we are using IsCellEditControlEnabled,
8277 // this interacts badly with calling SetCellValue from
8278 // an EVT_GRID_CELL_CHANGE handler.
8280 HideCellEditControl();
8281 ShowCellEditControl(); // will reread data from table
8286 // ----------------------------------------------------------------------------
8287 // block, row and column selection
8288 // ----------------------------------------------------------------------------
8290 void wxGrid
::SelectRow( int row
, bool addToSelected
)
8295 if ( !addToSelected
)
8298 m_selection
->SelectRow(row
);
8301 void wxGrid
::SelectCol( int col
, bool addToSelected
)
8306 if ( !addToSelected
)
8309 m_selection
->SelectCol(col
);
8312 void wxGrid
::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8318 if ( !addToSelected
)
8321 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8324 void wxGrid
::SelectAll()
8326 if ( m_numRows
> 0 && m_numCols
> 0 )
8329 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8333 // ----------------------------------------------------------------------------
8334 // cell, row and col deselection
8335 // ----------------------------------------------------------------------------
8337 void wxGrid
::DeselectLine(int line
, const wxGridOperations
& oper
)
8342 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8343 if ( mode
== oper
.GetSelectionMode() ||
8344 mode
== wxGrid
::wxGridSelectRowsOrColumns
)
8346 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8347 if ( m_selection
->IsInSelection(c
) )
8348 m_selection
->ToggleCellSelection(c
);
8350 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8352 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8353 for ( int i
= 0; i
< nOther
; i
++ )
8355 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8356 if ( m_selection
->IsInSelection(c
) )
8357 m_selection
->ToggleCellSelection(c
);
8360 //else: can only select orthogonal lines so no lines in this direction
8361 // could have been selected anyhow
8364 void wxGrid
::DeselectRow(int row
)
8366 DeselectLine(row
, wxGridRowOperations());
8369 void wxGrid
::DeselectCol(int col
)
8371 DeselectLine(col
, wxGridColumnOperations());
8374 void wxGrid
::DeselectCell( int row
, int col
)
8376 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8377 m_selection
->ToggleCellSelection(row
, col
);
8380 bool wxGrid
::IsSelection() const
8382 return ( m_selection
&& (m_selection
->IsSelection() ||
8383 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8384 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8387 bool wxGrid
::IsInSelection( int row
, int col
) const
8389 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8390 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8391 col
>= m_selectedBlockTopLeft
.GetCol() &&
8392 row
<= m_selectedBlockBottomRight
.GetRow() &&
8393 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8396 wxGridCellCoordsArray wxGrid
::GetSelectedCells() const
8400 wxGridCellCoordsArray a
;
8404 return m_selection
->m_cellSelection
;
8407 wxGridCellCoordsArray wxGrid
::GetSelectionBlockTopLeft() const
8411 wxGridCellCoordsArray a
;
8415 return m_selection
->m_blockSelectionTopLeft
;
8418 wxGridCellCoordsArray wxGrid
::GetSelectionBlockBottomRight() const
8422 wxGridCellCoordsArray a
;
8426 return m_selection
->m_blockSelectionBottomRight
;
8429 wxArrayInt wxGrid
::GetSelectedRows() const
8437 return m_selection
->m_rowSelection
;
8440 wxArrayInt wxGrid
::GetSelectedCols() const
8448 return m_selection
->m_colSelection
;
8451 void wxGrid
::ClearSelection()
8453 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8454 m_selectedBlockBottomRight
);
8455 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8456 m_selectedBlockCorner
);
8458 m_selectedBlockTopLeft
=
8459 m_selectedBlockBottomRight
=
8460 m_selectedBlockCorner
= wxGridNoCellCoords
;
8462 if ( !r1
.IsEmpty() )
8463 RefreshRect(r1
, false);
8464 if ( !r2
.IsEmpty() )
8465 RefreshRect(r2
, false);
8468 m_selection
->ClearSelection();
8471 // This function returns the rectangle that encloses the given block
8472 // in device coords clipped to the client size of the grid window.
8474 wxRect wxGrid
::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8475 const wxGridCellCoords
& bottomRight
) const
8478 wxRect tempCellRect
= CellToRect(topLeft
);
8479 if ( tempCellRect
!= wxGridNoCellRect
)
8481 resultRect
= tempCellRect
;
8485 resultRect
= wxRect(0, 0, 0, 0);
8488 tempCellRect
= CellToRect(bottomRight
);
8489 if ( tempCellRect
!= wxGridNoCellRect
)
8491 resultRect
+= tempCellRect
;
8495 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8496 return wxGridNoCellRect
;
8499 // Ensure that left/right and top/bottom pairs are in order.
8500 int left
= resultRect
.GetLeft();
8501 int top
= resultRect
.GetTop();
8502 int right
= resultRect
.GetRight();
8503 int bottom
= resultRect
.GetBottom();
8505 int leftCol
= topLeft
.GetCol();
8506 int topRow
= topLeft
.GetRow();
8507 int rightCol
= bottomRight
.GetCol();
8508 int bottomRow
= bottomRight
.GetRow();
8532 // The following loop is ONLY necessary to detect and handle merged cells.
8534 m_gridWin
->GetClientSize( &cw
, &ch
);
8536 // Get the origin coordinates: notice that they will be negative if the
8537 // grid is scrolled downwards/to the right.
8538 int gridOriginX
= 0;
8539 int gridOriginY
= 0;
8540 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8542 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8543 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8545 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8546 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8548 // Bound our loop so that we only examine the portion of the selected block
8549 // that is shown on screen. Therefore, we compare the Top-Left block values
8550 // to the Top-Left screen values, and the Bottom-Right block values to the
8551 // Bottom-Right screen values, choosing appropriately.
8552 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8553 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8554 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8555 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8557 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8559 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8561 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8562 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8564 tempCellRect
= CellToRect( j
, i
);
8566 if (tempCellRect
.x
< left
)
8567 left
= tempCellRect
.x
;
8568 if (tempCellRect
.y
< top
)
8569 top
= tempCellRect
.y
;
8570 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8571 right
= tempCellRect
.x
+ tempCellRect
.width
;
8572 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8573 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8577 i
= visibleRightCol
; // jump over inner cells.
8582 // Convert to scrolled coords
8583 CalcScrolledPosition( left
, top
, &left
, &top
);
8584 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8586 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8587 return wxRect(0,0,0,0);
8589 resultRect
.SetLeft( wxMax(0, left
) );
8590 resultRect
.SetTop( wxMax(0, top
) );
8591 resultRect
.SetRight( wxMin(cw
, right
) );
8592 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8597 void wxGrid
::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8598 const wxGridOperations
& oper
)
8601 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8602 const int numLines
= oper
.GetNumberOfLines(this);
8603 for ( int i
= 0; i
< numLines
; i
++ )
8605 int size
= sizeInfo
.GetSize(i
);
8606 if ( size
!= sizeInfo
.m_sizeDefault
)
8607 oper
.SetLineSize(this, i
, size
);
8612 void wxGrid
::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8614 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8617 void wxGrid
::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8619 DoSetSizes(sizeInfo
, wxGridRowOperations());
8622 wxGridSizesInfo
::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8624 m_sizeDefault
= defSize
;
8625 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8627 if ( allSizes
[i
] != defSize
)
8628 m_customSizes
[i
] = allSizes
[i
];
8632 int wxGridSizesInfo
::GetSize(unsigned pos
) const
8634 wxUnsignedToIntHashMap
::const_iterator it
= m_customSizes
.find(pos
);
8636 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8639 // ----------------------------------------------------------------------------
8641 // ----------------------------------------------------------------------------
8643 #if wxUSE_DRAG_AND_DROP
8645 // this allow setting drop target directly on wxGrid
8646 void wxGrid
::SetDropTarget(wxDropTarget
*dropTarget
)
8648 GetGridWindow()->SetDropTarget(dropTarget
);
8651 #endif // wxUSE_DRAG_AND_DROP
8653 // ----------------------------------------------------------------------------
8654 // grid event classes
8655 // ----------------------------------------------------------------------------
8657 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8659 wxGridEvent
::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8660 int row
, int col
, int x
, int y
, bool sel
,
8661 bool control
, bool shift
, bool alt
, bool meta
)
8662 : wxNotifyEvent( type
, id
),
8663 wxKeyboardState(control
, shift
, alt
, meta
)
8665 Init(row
, col
, x
, y
, sel
);
8667 SetEventObject(obj
);
8670 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8672 wxGridSizeEvent
::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8673 int rowOrCol
, int x
, int y
,
8674 bool control
, bool shift
, bool alt
, bool meta
)
8675 : wxNotifyEvent( type
, id
),
8676 wxKeyboardState(control
, shift
, alt
, meta
)
8678 Init(rowOrCol
, x
, y
);
8680 SetEventObject(obj
);
8684 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8686 wxGridRangeSelectEvent
::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8687 const wxGridCellCoords
& topLeft
,
8688 const wxGridCellCoords
& bottomRight
,
8689 bool sel
, bool control
,
8690 bool shift
, bool alt
, bool meta
)
8691 : wxNotifyEvent( type
, id
),
8692 wxKeyboardState(control
, shift
, alt
, meta
)
8694 Init(topLeft
, bottomRight
, sel
);
8696 SetEventObject(obj
);
8700 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8702 wxGridEditorCreatedEvent
::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8703 wxObject
* obj
, int row
,
8704 int col
, wxControl
* ctrl
)
8705 : wxCommandEvent(type
, id
)
8707 SetEventObject(obj
);
8714 // ----------------------------------------------------------------------------
8715 // wxGridTypeRegistry
8716 // ----------------------------------------------------------------------------
8718 wxGridTypeRegistry
::~wxGridTypeRegistry()
8720 size_t count
= m_typeinfo
.GetCount();
8721 for ( size_t i
= 0; i
< count
; i
++ )
8722 delete m_typeinfo
[i
];
8725 void wxGridTypeRegistry
::RegisterDataType(const wxString
& typeName
,
8726 wxGridCellRenderer
* renderer
,
8727 wxGridCellEditor
* editor
)
8729 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8731 // is it already registered?
8732 int loc
= FindRegisteredDataType(typeName
);
8733 if ( loc
!= wxNOT_FOUND
)
8735 delete m_typeinfo
[loc
];
8736 m_typeinfo
[loc
] = info
;
8740 m_typeinfo
.Add(info
);
8744 int wxGridTypeRegistry
::FindRegisteredDataType(const wxString
& typeName
)
8746 size_t count
= m_typeinfo
.GetCount();
8747 for ( size_t i
= 0; i
< count
; i
++ )
8749 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8758 int wxGridTypeRegistry
::FindDataType(const wxString
& typeName
)
8760 int index
= FindRegisteredDataType(typeName
);
8761 if ( index
== wxNOT_FOUND
)
8763 // check whether this is one of the standard ones, in which case
8764 // register it "on the fly"
8766 if ( typeName
== wxGRID_VALUE_STRING
)
8768 RegisterDataType(wxGRID_VALUE_STRING
,
8769 new wxGridCellStringRenderer
,
8770 new wxGridCellTextEditor
);
8773 #endif // wxUSE_TEXTCTRL
8775 if ( typeName
== wxGRID_VALUE_BOOL
)
8777 RegisterDataType(wxGRID_VALUE_BOOL
,
8778 new wxGridCellBoolRenderer
,
8779 new wxGridCellBoolEditor
);
8782 #endif // wxUSE_CHECKBOX
8784 if ( typeName
== wxGRID_VALUE_NUMBER
)
8786 RegisterDataType(wxGRID_VALUE_NUMBER
,
8787 new wxGridCellNumberRenderer
,
8788 new wxGridCellNumberEditor
);
8790 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8792 RegisterDataType(wxGRID_VALUE_FLOAT
,
8793 new wxGridCellFloatRenderer
,
8794 new wxGridCellFloatEditor
);
8797 #endif // wxUSE_TEXTCTRL
8799 if ( typeName
== wxGRID_VALUE_CHOICE
)
8801 RegisterDataType(wxGRID_VALUE_CHOICE
,
8802 new wxGridCellStringRenderer
,
8803 new wxGridCellChoiceEditor
);
8806 #endif // wxUSE_COMBOBOX
8811 // we get here only if just added the entry for this type, so return
8813 index
= m_typeinfo
.GetCount() - 1;
8819 int wxGridTypeRegistry
::FindOrCloneDataType(const wxString
& typeName
)
8821 int index
= FindDataType(typeName
);
8822 if ( index
== wxNOT_FOUND
)
8824 // the first part of the typename is the "real" type, anything after ':'
8825 // are the parameters for the renderer
8826 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8827 if ( index
== wxNOT_FOUND
)
8832 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8833 wxGridCellRenderer
*rendererOld
= renderer
;
8834 renderer
= renderer
->Clone();
8835 rendererOld
->DecRef();
8837 wxGridCellEditor
*editor
= GetEditor(index
);
8838 wxGridCellEditor
*editorOld
= editor
;
8839 editor
= editor
->Clone();
8840 editorOld
->DecRef();
8842 // do it even if there are no parameters to reset them to defaults
8843 wxString params
= typeName
.AfterFirst(wxT(':'));
8844 renderer
->SetParameters(params
);
8845 editor
->SetParameters(params
);
8847 // register the new typename
8848 RegisterDataType(typeName
, renderer
, editor
);
8850 // we just registered it, it's the last one
8851 index
= m_typeinfo
.GetCount() - 1;
8857 wxGridCellRenderer
* wxGridTypeRegistry
::GetRenderer(int index
)
8859 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8866 wxGridCellEditor
* wxGridTypeRegistry
::GetEditor(int index
)
8868 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8875 #endif // wxUSE_GRID