1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
58 const char wxGridNameStr
[] = "grid";
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_MOTIF(identifier) identifier
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
69 #define WXUNUSED_GTK(identifier) identifier
72 // Required for wxIs... functions
75 WX_DECLARE_HASH_SET_WITH_DECL(int, wxIntegerHash
, wxIntegerEqual
,
76 wxGridFixedIndicesSet
, class WXDLLIMPEXP_ADV
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
86 //#define DEBUG_ATTR_CACHE
87 #ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits
= 0;
89 static size_t gs_nAttrCacheMisses
= 0;
92 // this struct simply combines together the default header renderers
94 // as the renderers ctors are trivial, there is no problem with making them
96 struct DefaultHeaderRenderers
98 wxGridColumnHeaderRendererDefault colRenderer
;
99 wxGridRowHeaderRendererDefault rowRenderer
;
100 wxGridCornerHeaderRendererDefault cornerRenderer
;
101 } gs_defaultHeaderRenderers
;
103 } // anonymous namespace
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
110 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
116 const size_t GRID_SCROLL_LINE_X
= 15;
117 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
119 // the size of hash tables used a bit everywhere (the max number of elements
120 // in these hash tables is the number of rows/columns)
121 const int GRID_HASH_SIZE
= 100;
123 // the minimal distance in pixels the mouse needs to move to start a drag
125 const int DRAG_SENSITIVITY
= 3;
127 } // anonymous namespace
129 #include "wx/arrimpl.cpp"
131 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
132 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK
, wxGridEvent
);
139 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK
, wxGridEvent
);
140 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK
, wxGridEvent
);
141 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK
, wxGridEvent
);
142 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG
, wxGridEvent
);
143 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK
, wxGridEvent
);
144 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK
, wxGridEvent
);
145 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK
, wxGridEvent
);
146 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK
, wxGridEvent
);
147 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE
, wxGridSizeEvent
);
148 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE
, wxGridSizeEvent
);
149 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE
, wxGridEvent
);
150 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
151 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
152 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
154 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
155 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
159 // ============================================================================
161 // ============================================================================
163 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
165 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
166 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
167 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
168 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
171 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
172 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
173 EVT_HEADER_DCLICK(wxID_ANY
, wxGridHeaderCtrl::OnDoubleClick
)
174 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnRightClick
)
176 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
177 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
178 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
180 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
181 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
184 wxGridOperations
& wxGridRowOperations::Dual() const
186 static wxGridColumnOperations s_colOper
;
191 wxGridOperations
& wxGridColumnOperations::Dual() const
193 static wxGridRowOperations s_rowOper
;
198 // ----------------------------------------------------------------------------
199 // wxGridCellWorker is an (almost) empty common base class for
200 // wxGridCellRenderer and wxGridCellEditor managing ref counting
201 // ----------------------------------------------------------------------------
203 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
208 wxGridCellWorker::~wxGridCellWorker()
212 // ----------------------------------------------------------------------------
213 // wxGridHeaderLabelsRenderer and related classes
214 // ----------------------------------------------------------------------------
216 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid
& grid
,
218 const wxString
& value
,
222 int textOrientation
) const
224 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
225 dc
.SetTextForeground(grid
.GetLabelTextColour());
226 dc
.SetFont(grid
.GetLabelFont());
227 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
231 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
235 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
236 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
237 rect
.GetRight(), rect
.GetBottom());
238 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
239 rect
.GetLeft(), rect
.GetBottom());
240 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
241 rect
.GetRight() + 1, rect
.GetBottom());
243 dc
.SetPen(*wxWHITE_PEN
);
244 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
245 rect
.GetLeft() + 1, rect
.GetBottom());
246 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
247 rect
.GetRight(), rect
.GetTop());
252 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
256 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
257 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
258 rect
.GetRight(), rect
.GetBottom());
259 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
260 rect
.GetRight(), rect
.GetTop());
261 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
262 rect
.GetRight() + 1, rect
.GetBottom());
264 dc
.SetPen(*wxWHITE_PEN
);
265 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
266 rect
.GetLeft(), rect
.GetBottom());
267 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
268 rect
.GetRight(), rect
.GetTop() + 1);
273 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
277 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
278 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
279 rect
.GetRight() - 1, rect
.GetTop());
280 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
281 rect
.GetLeft(), rect
.GetBottom() - 1);
282 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
283 rect
.GetRight(), rect
.GetTop());
284 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
285 rect
.GetLeft(), rect
.GetBottom());
287 dc
.SetPen(*wxWHITE_PEN
);
288 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
289 rect
.GetRight() - 1, rect
.GetTop() + 1);
290 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
291 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
302 m_isReadOnly
= Unset
;
307 m_attrkind
= wxGridCellAttr::Cell
;
309 m_sizeRows
= m_sizeCols
= 1;
310 m_overflow
= UnsetOverflow
;
312 SetDefAttr(attrDefault
);
315 wxGridCellAttr
*wxGridCellAttr::Clone() const
317 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
319 if ( HasTextColour() )
320 attr
->SetTextColour(GetTextColour());
321 if ( HasBackgroundColour() )
322 attr
->SetBackgroundColour(GetBackgroundColour());
324 attr
->SetFont(GetFont());
325 if ( HasAlignment() )
326 attr
->SetAlignment(m_hAlign
, m_vAlign
);
328 attr
->SetSize( m_sizeRows
, m_sizeCols
);
332 attr
->SetRenderer(m_renderer
);
333 m_renderer
->IncRef();
337 attr
->SetEditor(m_editor
);
344 attr
->SetOverflow( m_overflow
== Overflow
);
345 attr
->SetKind( m_attrkind
);
350 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
352 if ( !HasTextColour() && mergefrom
->HasTextColour() )
353 SetTextColour(mergefrom
->GetTextColour());
354 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
355 SetBackgroundColour(mergefrom
->GetBackgroundColour());
356 if ( !HasFont() && mergefrom
->HasFont() )
357 SetFont(mergefrom
->GetFont());
358 if ( !HasAlignment() && mergefrom
->HasAlignment() )
361 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
362 SetAlignment(hAlign
, vAlign
);
364 if ( !HasSize() && mergefrom
->HasSize() )
365 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
367 // Directly access member functions as GetRender/Editor don't just return
368 // m_renderer/m_editor
370 // Maybe add support for merge of Render and Editor?
371 if (!HasRenderer() && mergefrom
->HasRenderer() )
373 m_renderer
= mergefrom
->m_renderer
;
374 m_renderer
->IncRef();
376 if ( !HasEditor() && mergefrom
->HasEditor() )
378 m_editor
= mergefrom
->m_editor
;
381 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
382 SetReadOnly(mergefrom
->IsReadOnly());
384 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
385 SetOverflow(mergefrom
->GetOverflow());
387 SetDefAttr(mergefrom
->m_defGridAttr
);
390 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
392 // The size of a cell is normally 1,1
394 // If this cell is larger (2,2) then this is the top left cell
395 // the other cells that will be covered (lower right cells) must be
396 // set to negative or zero values such that
397 // row + num_rows of the covered cell points to the larger cell (this cell)
398 // same goes for the col + num_cols.
400 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
402 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
403 !((num_rows
<= 0) && (num_cols
> 0)) ||
404 !((num_rows
== 0) && (num_cols
== 0))),
405 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
407 m_sizeRows
= num_rows
;
408 m_sizeCols
= num_cols
;
411 const wxColour
& wxGridCellAttr::GetTextColour() const
417 else if (m_defGridAttr
&& m_defGridAttr
!= this)
419 return m_defGridAttr
->GetTextColour();
423 wxFAIL_MSG(wxT("Missing default cell attribute"));
428 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
430 if (HasBackgroundColour())
434 else if (m_defGridAttr
&& m_defGridAttr
!= this)
436 return m_defGridAttr
->GetBackgroundColour();
440 wxFAIL_MSG(wxT("Missing default cell attribute"));
445 const wxFont
& wxGridCellAttr::GetFont() const
451 else if (m_defGridAttr
&& m_defGridAttr
!= this)
453 return m_defGridAttr
->GetFont();
457 wxFAIL_MSG(wxT("Missing default cell attribute"));
462 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
471 else if (m_defGridAttr
&& m_defGridAttr
!= this)
473 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
477 wxFAIL_MSG(wxT("Missing default cell attribute"));
481 void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
483 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
486 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
490 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
493 *num_rows
= m_sizeRows
;
495 *num_cols
= m_sizeCols
;
498 // GetRenderer and GetEditor use a slightly different decision path about
499 // which attribute to use. If a non-default attr object has one then it is
500 // used, otherwise the default editor or renderer is fetched from the grid and
501 // used. It should be the default for the data type of the cell. If it is
502 // NULL (because the table has a type that the grid does not have in its
503 // registry), then the grid's default editor or renderer is used.
505 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
507 wxGridCellRenderer
*renderer
= NULL
;
509 if ( m_renderer
&& this != m_defGridAttr
)
511 // use the cells renderer if it has one
512 renderer
= m_renderer
;
515 else // no non-default cell renderer
517 // get default renderer for the data type
520 // GetDefaultRendererForCell() will do IncRef() for us
521 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
524 if ( renderer
== NULL
)
526 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
528 // if we still don't have one then use the grid default
529 // (no need for IncRef() here neither)
530 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
532 else // default grid attr
534 // use m_renderer which we had decided not to use initially
535 renderer
= m_renderer
;
542 // we're supposed to always find something
543 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
548 // same as above, except for s/renderer/editor/g
549 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
551 wxGridCellEditor
*editor
= NULL
;
553 if ( m_editor
&& this != m_defGridAttr
)
555 // use the cells editor if it has one
559 else // no non default cell editor
561 // get default editor for the data type
564 // GetDefaultEditorForCell() will do IncRef() for us
565 editor
= grid
->GetDefaultEditorForCell(row
, col
);
568 if ( editor
== NULL
)
570 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
572 // if we still don't have one then use the grid default
573 // (no need for IncRef() here neither)
574 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
576 else // default grid attr
578 // use m_editor which we had decided not to use initially
586 // we're supposed to always find something
587 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
592 // ----------------------------------------------------------------------------
593 // wxGridCellAttrData
594 // ----------------------------------------------------------------------------
596 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
598 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
599 // touch attribute's reference counting explicitly, since this
600 // is managed by class wxGridCellWithAttr
601 int n
= FindIndex(row
, col
);
602 if ( n
== wxNOT_FOUND
)
607 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
609 //else: nothing to do
611 else // we already have an attribute for this cell
615 // change the attribute
616 m_attrs
[(size_t)n
].ChangeAttr(attr
);
620 // remove this attribute
621 m_attrs
.RemoveAt((size_t)n
);
626 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
628 wxGridCellAttr
*attr
= NULL
;
630 int n
= FindIndex(row
, col
);
631 if ( n
!= wxNOT_FOUND
)
633 attr
= m_attrs
[(size_t)n
].attr
;
640 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
642 size_t count
= m_attrs
.GetCount();
643 for ( size_t n
= 0; n
< count
; n
++ )
645 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
646 wxCoord row
= coords
.GetRow();
647 if ((size_t)row
>= pos
)
651 // If rows inserted, include row counter where necessary
652 coords
.SetRow(row
+ numRows
);
654 else if (numRows
< 0)
656 // If rows deleted ...
657 if ((size_t)row
>= pos
- numRows
)
659 // ...either decrement row counter (if row still exists)...
660 coords
.SetRow(row
+ numRows
);
664 // ...or remove the attribute
674 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
676 size_t count
= m_attrs
.GetCount();
677 for ( size_t n
= 0; n
< count
; n
++ )
679 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
680 wxCoord col
= coords
.GetCol();
681 if ( (size_t)col
>= pos
)
685 // If rows inserted, include row counter where necessary
686 coords
.SetCol(col
+ numCols
);
688 else if (numCols
< 0)
690 // If rows deleted ...
691 if ((size_t)col
>= pos
- numCols
)
693 // ...either decrement row counter (if row still exists)...
694 coords
.SetCol(col
+ numCols
);
698 // ...or remove the attribute
708 int wxGridCellAttrData::FindIndex(int row
, int col
) const
710 size_t count
= m_attrs
.GetCount();
711 for ( size_t n
= 0; n
< count
; n
++ )
713 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
714 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
723 // ----------------------------------------------------------------------------
724 // wxGridRowOrColAttrData
725 // ----------------------------------------------------------------------------
727 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
729 size_t count
= m_attrs
.GetCount();
730 for ( size_t n
= 0; n
< count
; n
++ )
732 m_attrs
[n
]->DecRef();
736 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
738 wxGridCellAttr
*attr
= NULL
;
740 int n
= m_rowsOrCols
.Index(rowOrCol
);
741 if ( n
!= wxNOT_FOUND
)
743 attr
= m_attrs
[(size_t)n
];
750 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
752 int i
= m_rowsOrCols
.Index(rowOrCol
);
753 if ( i
== wxNOT_FOUND
)
757 // store the new attribute, taking its ownership
758 m_rowsOrCols
.Add(rowOrCol
);
763 else // we have an attribute for this row or column
765 size_t n
= (size_t)i
;
767 // notice that this code works correctly even when the old attribute is
768 // the same as the new one: as we own of it, we must call DecRef() on
769 // it in any case and this won't result in destruction of the new
770 // attribute if it's the same as old one because it must have ref count
771 // of at least 2 to be passed to us while we keep a reference to it too
772 m_attrs
[n
]->DecRef();
776 // replace the attribute with the new one
779 else // remove the attribute
781 m_rowsOrCols
.RemoveAt(n
);
787 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
789 size_t count
= m_attrs
.GetCount();
790 for ( size_t n
= 0; n
< count
; n
++ )
792 int & rowOrCol
= m_rowsOrCols
[n
];
793 if ( (size_t)rowOrCol
>= pos
)
795 if ( numRowsOrCols
> 0 )
797 // If rows inserted, include row counter where necessary
798 rowOrCol
+= numRowsOrCols
;
800 else if ( numRowsOrCols
< 0)
802 // If rows deleted, either decrement row counter (if row still exists)
803 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
804 rowOrCol
+= numRowsOrCols
;
807 m_rowsOrCols
.RemoveAt(n
);
808 m_attrs
[n
]->DecRef();
818 // ----------------------------------------------------------------------------
819 // wxGridCellAttrProvider
820 // ----------------------------------------------------------------------------
822 wxGridCellAttrProvider::wxGridCellAttrProvider()
827 wxGridCellAttrProvider::~wxGridCellAttrProvider()
832 void wxGridCellAttrProvider::InitData()
834 m_data
= new wxGridCellAttrProviderData
;
837 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
838 wxGridCellAttr::wxAttrKind kind
) const
840 wxGridCellAttr
*attr
= NULL
;
845 case (wxGridCellAttr::Any
):
846 // Get cached merge attributes.
847 // Currently not used as no cache implemented as not mutable
848 // attr = m_data->m_mergeAttr.GetAttr(row, col);
851 // Basically implement old version.
852 // Also check merge cache, so we don't have to re-merge every time..
853 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
854 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
855 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
857 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
859 // Two or more are non NULL
860 attr
= new wxGridCellAttr
;
861 attr
->SetKind(wxGridCellAttr::Merged
);
863 // Order is important..
866 attr
->MergeWith(attrcell
);
871 attr
->MergeWith(attrcol
);
876 attr
->MergeWith(attrrow
);
880 // store merge attr if cache implemented
882 //m_data->m_mergeAttr.SetAttr(attr, row, col);
886 // one or none is non null return it or null.
905 case (wxGridCellAttr::Cell
):
906 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
909 case (wxGridCellAttr::Col
):
910 attr
= m_data
->m_colAttrs
.GetAttr(col
);
913 case (wxGridCellAttr::Row
):
914 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
919 // (wxGridCellAttr::Default):
920 // (wxGridCellAttr::Merged):
928 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
934 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
937 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
942 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
945 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
950 m_data
->m_colAttrs
.SetAttr(attr
, col
);
953 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
957 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
959 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
963 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
967 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
969 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
973 const wxGridColumnHeaderRenderer
&
974 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
976 return gs_defaultHeaderRenderers
.colRenderer
;
979 const wxGridRowHeaderRenderer
&
980 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
982 return gs_defaultHeaderRenderers
.rowRenderer
;
985 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
987 return gs_defaultHeaderRenderers
.cornerRenderer
;
990 // ----------------------------------------------------------------------------
992 // ----------------------------------------------------------------------------
994 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
996 wxGridTableBase::wxGridTableBase()
999 m_attrProvider
= NULL
;
1002 wxGridTableBase::~wxGridTableBase()
1004 delete m_attrProvider
;
1007 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1009 delete m_attrProvider
;
1010 m_attrProvider
= attrProvider
;
1013 bool wxGridTableBase::CanHaveAttributes()
1015 if ( ! GetAttrProvider() )
1017 // use the default attr provider by default
1018 SetAttrProvider(new wxGridCellAttrProvider
);
1024 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1026 if ( m_attrProvider
)
1027 return m_attrProvider
->GetAttr(row
, col
, kind
);
1032 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1034 if ( m_attrProvider
)
1037 attr
->SetKind(wxGridCellAttr::Cell
);
1038 m_attrProvider
->SetAttr(attr
, row
, col
);
1042 // as we take ownership of the pointer and don't store it, we must
1048 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1050 if ( m_attrProvider
)
1052 attr
->SetKind(wxGridCellAttr::Row
);
1053 m_attrProvider
->SetRowAttr(attr
, row
);
1057 // as we take ownership of the pointer and don't store it, we must
1063 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1065 if ( m_attrProvider
)
1067 attr
->SetKind(wxGridCellAttr::Col
);
1068 m_attrProvider
->SetColAttr(attr
, col
);
1072 // as we take ownership of the pointer and don't store it, we must
1078 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1079 size_t WXUNUSED(numRows
) )
1081 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1086 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1088 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1093 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1094 size_t WXUNUSED(numRows
) )
1096 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1101 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1102 size_t WXUNUSED(numCols
) )
1104 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1109 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1111 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1116 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1117 size_t WXUNUSED(numCols
) )
1119 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1124 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1128 // RD: Starting the rows at zero confuses users,
1129 // no matter how much it makes sense to us geeks.
1135 wxString
wxGridTableBase::GetColLabelValue( int col
)
1137 // default col labels are:
1138 // cols 0 to 25 : A-Z
1139 // cols 26 to 675 : AA-ZZ
1144 for ( n
= 1; ; n
++ )
1146 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1152 // reverse the string...
1154 for ( i
= 0; i
< n
; i
++ )
1162 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1164 return wxGRID_VALUE_STRING
;
1167 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1168 const wxString
& typeName
)
1170 return typeName
== wxGRID_VALUE_STRING
;
1173 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1175 return CanGetValueAs(row
, col
, typeName
);
1178 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1183 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1188 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1193 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1194 long WXUNUSED(value
) )
1198 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1199 double WXUNUSED(value
) )
1203 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1204 bool WXUNUSED(value
) )
1208 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1209 const wxString
& WXUNUSED(typeName
) )
1214 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1215 const wxString
& WXUNUSED(typeName
),
1216 void* WXUNUSED(value
) )
1220 //////////////////////////////////////////////////////////////////////
1222 // Message class for the grid table to send requests and notifications
1226 wxGridTableMessage::wxGridTableMessage()
1234 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1235 int commandInt1
, int commandInt2
)
1239 m_comInt1
= commandInt1
;
1240 m_comInt2
= commandInt2
;
1243 //////////////////////////////////////////////////////////////////////
1245 // A basic grid table for string data. An object of this class will
1246 // created by wxGrid if you don't specify an alternative table class.
1249 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1251 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1253 wxGridStringTable::wxGridStringTable()
1259 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1262 m_numCols
= numCols
;
1264 m_data
.Alloc( numRows
);
1267 sa
.Alloc( numCols
);
1268 sa
.Add( wxEmptyString
, numCols
);
1270 m_data
.Add( sa
, numRows
);
1273 wxString
wxGridStringTable::GetValue( int row
, int col
)
1275 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1276 (col
>= 0 && col
< GetNumberCols()),
1278 wxT("invalid row or column index in wxGridStringTable") );
1280 return m_data
[row
][col
];
1283 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1285 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1286 (col
>= 0 && col
< GetNumberCols()),
1287 wxT("invalid row or column index in wxGridStringTable") );
1289 m_data
[row
][col
] = value
;
1292 void wxGridStringTable::Clear()
1295 int numRows
, numCols
;
1297 numRows
= m_data
.GetCount();
1300 numCols
= m_data
[0].GetCount();
1302 for ( row
= 0; row
< numRows
; row
++ )
1304 for ( col
= 0; col
< numCols
; col
++ )
1306 m_data
[row
][col
] = wxEmptyString
;
1312 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1314 size_t curNumRows
= m_data
.GetCount();
1315 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1316 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1318 if ( pos
>= curNumRows
)
1320 return AppendRows( numRows
);
1324 sa
.Alloc( curNumCols
);
1325 sa
.Add( wxEmptyString
, curNumCols
);
1326 m_data
.Insert( sa
, pos
, numRows
);
1330 wxGridTableMessage
msg( this,
1331 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1335 GetView()->ProcessTableMessage( msg
);
1341 bool wxGridStringTable::AppendRows( size_t numRows
)
1343 size_t curNumRows
= m_data
.GetCount();
1344 size_t curNumCols
= ( curNumRows
> 0
1345 ? m_data
[0].GetCount()
1346 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1349 if ( curNumCols
> 0 )
1351 sa
.Alloc( curNumCols
);
1352 sa
.Add( wxEmptyString
, curNumCols
);
1355 m_data
.Add( sa
, numRows
);
1359 wxGridTableMessage
msg( this,
1360 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1363 GetView()->ProcessTableMessage( msg
);
1369 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1371 size_t curNumRows
= m_data
.GetCount();
1373 if ( pos
>= curNumRows
)
1375 wxFAIL_MSG( wxString::Format
1377 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1379 (unsigned long)numRows
,
1380 (unsigned long)curNumRows
1386 if ( numRows
> curNumRows
- pos
)
1388 numRows
= curNumRows
- pos
;
1391 if ( numRows
>= curNumRows
)
1397 m_data
.RemoveAt( pos
, numRows
);
1402 wxGridTableMessage
msg( this,
1403 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1407 GetView()->ProcessTableMessage( msg
);
1413 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1417 size_t curNumRows
= m_data
.GetCount();
1418 size_t curNumCols
= ( curNumRows
> 0
1419 ? m_data
[0].GetCount()
1420 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1422 if ( pos
>= curNumCols
)
1424 return AppendCols( numCols
);
1427 if ( !m_colLabels
.IsEmpty() )
1429 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1432 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1433 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1436 for ( row
= 0; row
< curNumRows
; row
++ )
1438 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1440 m_data
[row
].Insert( wxEmptyString
, col
);
1444 m_numCols
+= numCols
;
1448 wxGridTableMessage
msg( this,
1449 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1453 GetView()->ProcessTableMessage( msg
);
1459 bool wxGridStringTable::AppendCols( size_t numCols
)
1463 size_t curNumRows
= m_data
.GetCount();
1465 for ( row
= 0; row
< curNumRows
; row
++ )
1467 m_data
[row
].Add( wxEmptyString
, numCols
);
1470 m_numCols
+= numCols
;
1474 wxGridTableMessage
msg( this,
1475 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1478 GetView()->ProcessTableMessage( msg
);
1484 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1488 size_t curNumRows
= m_data
.GetCount();
1489 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1490 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1492 if ( pos
>= curNumCols
)
1494 wxFAIL_MSG( wxString::Format
1496 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1498 (unsigned long)numCols
,
1499 (unsigned long)curNumCols
1506 colID
= GetView()->GetColAt( pos
);
1510 if ( numCols
> curNumCols
- colID
)
1512 numCols
= curNumCols
- colID
;
1515 if ( !m_colLabels
.IsEmpty() )
1517 // m_colLabels stores just as many elements as it needs, e.g. if only
1518 // the label of the first column had been set it would have only one
1519 // element and not numCols, so account for it
1520 int nToRm
= m_colLabels
.size() - colID
;
1522 m_colLabels
.RemoveAt( colID
, nToRm
);
1525 if ( numCols
>= curNumCols
)
1527 for ( row
= 0; row
< curNumRows
; row
++ )
1529 m_data
[row
].Clear();
1534 else // something will be left
1536 for ( row
= 0; row
< curNumRows
; row
++ )
1538 m_data
[row
].RemoveAt( colID
, numCols
);
1541 m_numCols
-= numCols
;
1546 wxGridTableMessage
msg( this,
1547 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1551 GetView()->ProcessTableMessage( msg
);
1557 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1559 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1561 // using default label
1563 return wxGridTableBase::GetRowLabelValue( row
);
1567 return m_rowLabels
[row
];
1571 wxString
wxGridStringTable::GetColLabelValue( int col
)
1573 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1575 // using default label
1577 return wxGridTableBase::GetColLabelValue( col
);
1581 return m_colLabels
[col
];
1585 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1587 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1589 int n
= m_rowLabels
.GetCount();
1592 for ( i
= n
; i
<= row
; i
++ )
1594 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1598 m_rowLabels
[row
] = value
;
1601 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1603 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1605 int n
= m_colLabels
.GetCount();
1608 for ( i
= n
; i
<= col
; i
++ )
1610 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1614 m_colLabels
[col
] = value
;
1618 //////////////////////////////////////////////////////////////////////
1619 //////////////////////////////////////////////////////////////////////
1621 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1622 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1625 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1627 m_owner
->CancelMouseCapture();
1630 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1631 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1632 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1633 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1636 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1640 // NO - don't do this because it will set both the x and y origin
1641 // coords to match the parent scrolled window and we just want to
1642 // set the y coord - MB
1644 // m_owner->PrepareDC( dc );
1647 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1648 wxPoint pt
= dc
.GetDeviceOrigin();
1649 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1651 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1652 m_owner
->DrawRowLabels( dc
, rows
);
1655 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1657 m_owner
->ProcessRowLabelMouseEvent( event
);
1660 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1662 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1666 //////////////////////////////////////////////////////////////////////
1668 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1669 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1670 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1671 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1674 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1678 // NO - don't do this because it will set both the x and y origin
1679 // coords to match the parent scrolled window and we just want to
1680 // set the x coord - MB
1682 // m_owner->PrepareDC( dc );
1685 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1686 wxPoint pt
= dc
.GetDeviceOrigin();
1687 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1688 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1690 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1692 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1693 m_owner
->DrawColLabels( dc
, cols
);
1696 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1698 m_owner
->ProcessColLabelMouseEvent( event
);
1701 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1703 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1707 //////////////////////////////////////////////////////////////////////
1709 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1710 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1711 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1712 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1715 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1719 m_owner
->DrawCornerLabel(dc
);
1722 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1724 m_owner
->ProcessCornerLabelMouseEvent( event
);
1727 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1729 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1733 //////////////////////////////////////////////////////////////////////
1735 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1736 EVT_PAINT( wxGridWindow::OnPaint
)
1737 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1738 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1739 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1740 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1741 EVT_CHAR( wxGridWindow::OnChar
)
1742 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1743 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1744 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1747 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1749 wxPaintDC
dc( this );
1750 m_owner
->PrepareDC( dc
);
1751 wxRegion reg
= GetUpdateRegion();
1752 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1753 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1755 m_owner
->DrawGridSpace( dc
);
1757 m_owner
->DrawAllGridLines( dc
, reg
);
1759 m_owner
->DrawHighlight( dc
, dirtyCells
);
1762 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1764 wxWindow::ScrollWindow( dx
, dy
, rect
);
1765 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1766 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1769 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1771 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1774 m_owner
->ProcessGridCellMouseEvent( event
);
1777 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1779 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1783 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1784 // cursor must be in the cell edit control to get key events
1786 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1788 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1792 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1794 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1798 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1800 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1804 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1808 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1810 // and if we have any selection, it has to be repainted, because it
1811 // uses different colour when the grid is not focused:
1812 if ( m_owner
->IsSelection() )
1818 // NB: Note that this code is in "else" branch only because the other
1819 // branch refreshes everything and so there's no point in calling
1820 // Refresh() again, *not* because it should only be done if
1821 // !IsSelection(). If the above code is ever optimized to refresh
1822 // only selected area, this needs to be moved out of the "else"
1823 // branch so that it's always executed.
1825 // current cell cursor {dis,re}appears on focus change:
1826 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1827 m_owner
->GetGridCursorCol());
1828 const wxRect cursor
=
1829 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1830 Refresh(true, &cursor
);
1833 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1837 #define internalXToCol(x) XToCol(x, true)
1838 #define internalYToRow(y) YToRow(y, true)
1840 /////////////////////////////////////////////////////////////////////
1842 #if wxUSE_EXTENDED_RTTI
1843 WX_DEFINE_FLAGS( wxGridStyle
)
1845 wxBEGIN_FLAGS( wxGridStyle
)
1846 // new style border flags, we put them first to
1847 // use them for streaming out
1848 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
1849 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
1850 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
1851 wxFLAGS_MEMBER(wxBORDER_RAISED
)
1852 wxFLAGS_MEMBER(wxBORDER_STATIC
)
1853 wxFLAGS_MEMBER(wxBORDER_NONE
)
1855 // old style border flags
1856 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
1857 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
1858 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
1859 wxFLAGS_MEMBER(wxRAISED_BORDER
)
1860 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
1861 wxFLAGS_MEMBER(wxBORDER
)
1863 // standard window styles
1864 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
1865 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
1866 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
1867 wxFLAGS_MEMBER(wxWANTS_CHARS
)
1868 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
1869 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
1870 wxFLAGS_MEMBER(wxVSCROLL
)
1871 wxFLAGS_MEMBER(wxHSCROLL
)
1873 wxEND_FLAGS( wxGridStyle
)
1875 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
1877 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
1878 wxHIDE_PROPERTY( Children
)
1879 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1880 wxEND_PROPERTIES_TABLE()
1882 wxBEGIN_HANDLERS_TABLE(wxGrid
)
1883 wxEND_HANDLERS_TABLE()
1885 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1888 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1891 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1894 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1895 EVT_PAINT( wxGrid::OnPaint
)
1896 EVT_SIZE( wxGrid::OnSize
)
1897 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1898 EVT_KEY_UP( wxGrid::OnKeyUp
)
1899 EVT_CHAR ( wxGrid::OnChar
)
1900 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1903 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1904 const wxPoint
& pos
, const wxSize
& size
,
1905 long style
, const wxString
& name
)
1907 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1908 style
| wxWANTS_CHARS
, name
))
1911 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1912 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1915 SetInitialSize(size
);
1924 m_winCapture
->ReleaseMouse();
1926 // Ensure that the editor control is destroyed before the grid is,
1927 // otherwise we crash later when the editor tries to do something with the
1928 // half destroyed grid
1929 HideCellEditControl();
1931 // Must do this or ~wxScrollHelper will pop the wrong event handler
1932 SetTargetWindow(this);
1934 wxSafeDecRef(m_defaultCellAttr
);
1936 #ifdef DEBUG_ATTR_CACHE
1937 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1938 wxPrintf(wxT("wxGrid attribute cache statistics: "
1939 "total: %u, hits: %u (%u%%)\n"),
1940 total
, gs_nAttrCacheHits
,
1941 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1944 // if we own the table, just delete it, otherwise at least don't leave it
1945 // with dangling view pointer
1948 else if ( m_table
&& m_table
->GetView() == this )
1949 m_table
->SetView(NULL
);
1951 delete m_typeRegistry
;
1954 delete m_setFixedRows
;
1955 delete m_setFixedCols
;
1959 // ----- internal init and update functions
1962 // NOTE: If using the default visual attributes works everywhere then this can
1963 // be removed as well as the #else cases below.
1964 #define _USE_VISATTR 0
1966 void wxGrid::Create()
1968 // create the type registry
1969 m_typeRegistry
= new wxGridTypeRegistry
;
1971 m_cellEditCtrlEnabled
= false;
1973 m_defaultCellAttr
= new wxGridCellAttr();
1975 // Set default cell attributes
1976 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1977 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1978 m_defaultCellAttr
->SetFont(GetFont());
1979 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1980 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1981 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1984 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1985 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1987 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1988 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1991 m_defaultCellAttr
->SetTextColour(
1992 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1993 m_defaultCellAttr
->SetBackgroundColour(
1994 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1999 m_currentCellCoords
= wxGridNoCellCoords
;
2001 // subwindow components that make up the wxGrid
2002 m_rowLabelWin
= new wxGridRowLabelWindow(this);
2003 CreateColumnWindow();
2004 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
2005 m_gridWin
= new wxGridWindow( this );
2007 SetTargetWindow( m_gridWin
);
2010 wxColour gfg
= gva
.colFg
;
2011 wxColour gbg
= gva
.colBg
;
2012 wxColour lfg
= lva
.colFg
;
2013 wxColour lbg
= lva
.colBg
;
2015 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2016 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
2017 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2018 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
2021 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
2022 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
2023 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
2024 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
2025 m_colWindow
->SetOwnForegroundColour(lfg
);
2026 m_colWindow
->SetOwnBackgroundColour(lbg
);
2028 m_gridWin
->SetOwnForegroundColour(gfg
);
2029 m_gridWin
->SetOwnBackgroundColour(gbg
);
2031 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2032 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
2034 // now that we have the grid window, use its font to compute the default
2036 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2037 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2038 m_defaultRowHeight
+= 8;
2040 m_defaultRowHeight
+= 4;
2045 void wxGrid::CreateColumnWindow()
2047 if ( m_useNativeHeader
)
2049 m_colWindow
= new wxGridHeaderCtrl(this);
2050 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2052 else // draw labels ourselves
2054 m_colWindow
= new wxGridColLabelWindow(this);
2055 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2059 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2060 wxGridSelectionModes selmode
)
2062 wxCHECK_MSG( !m_created
,
2064 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2066 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2069 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2071 wxCHECK_RET( m_created
,
2072 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2074 m_selection
->SetSelectionMode( selmode
);
2077 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2079 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2080 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2082 return m_selection
->GetSelectionMode();
2086 wxGrid::SetTable(wxGridTableBase
*table
,
2088 wxGrid::wxGridSelectionModes selmode
)
2090 bool checkSelection
= false;
2093 // stop all processing
2098 m_table
->SetView(0);
2104 wxDELETE(m_selection
);
2109 checkSelection
= true;
2111 // kill row and column size arrays
2112 m_colWidths
.Empty();
2113 m_colRights
.Empty();
2114 m_rowHeights
.Empty();
2115 m_rowBottoms
.Empty();
2120 m_numRows
= table
->GetNumberRows();
2121 m_numCols
= table
->GetNumberCols();
2123 if ( m_useNativeHeader
)
2124 GetGridColHeader()->SetColumnCount(m_numCols
);
2127 m_table
->SetView( this );
2128 m_ownTable
= takeOwnership
;
2129 m_selection
= new wxGridSelection( this, selmode
);
2132 // If the newly set table is smaller than the
2133 // original one current cell and selection regions
2134 // might be invalid,
2135 m_selectedBlockCorner
= wxGridNoCellCoords
;
2136 m_currentCellCoords
=
2137 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2138 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2139 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2140 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2142 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2143 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2146 m_selectedBlockBottomRight
=
2147 wxGridCellCoords(wxMin(m_numRows
,
2148 m_selectedBlockBottomRight
.GetRow()),
2150 m_selectedBlockBottomRight
.GetCol()));
2164 m_cornerLabelWin
= NULL
;
2165 m_rowLabelWin
= NULL
;
2173 m_defaultCellAttr
= NULL
;
2174 m_typeRegistry
= NULL
;
2175 m_winCapture
= NULL
;
2177 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2178 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2181 m_setFixedCols
= NULL
;
2184 m_attrCache
.row
= -1;
2185 m_attrCache
.col
= -1;
2186 m_attrCache
.attr
= NULL
;
2188 m_labelFont
= GetFont();
2189 m_labelFont
.SetWeight( wxBOLD
);
2191 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2192 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2194 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2195 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2196 m_colLabelTextOrientation
= wxHORIZONTAL
;
2198 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2199 m_defaultRowHeight
= 0; // this will be initialized after creation
2201 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2202 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2204 m_gridLineColour
= wxColour( 192,192,192 );
2205 m_gridLinesEnabled
= true;
2206 m_gridLinesClipHorz
=
2207 m_gridLinesClipVert
= true;
2208 m_cellHighlightColour
= *wxBLACK
;
2209 m_cellHighlightPenWidth
= 2;
2210 m_cellHighlightROPenWidth
= 1;
2212 m_canDragColMove
= false;
2214 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2215 m_winCapture
= NULL
;
2216 m_canDragRowSize
= true;
2217 m_canDragColSize
= true;
2218 m_canDragGridSize
= true;
2219 m_canDragCell
= false;
2221 m_dragRowOrCol
= -1;
2222 m_isDragging
= false;
2223 m_startDragPos
= wxDefaultPosition
;
2225 m_sortCol
= wxNOT_FOUND
;
2226 m_sortIsAscending
= true;
2229 m_nativeColumnLabels
= false;
2231 m_waitForSlowClick
= false;
2233 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2234 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2236 m_currentCellCoords
= wxGridNoCellCoords
;
2238 m_selectedBlockTopLeft
=
2239 m_selectedBlockBottomRight
=
2240 m_selectedBlockCorner
= wxGridNoCellCoords
;
2242 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2243 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2245 m_editable
= true; // default for whole grid
2247 m_inOnKeyDown
= false;
2253 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2254 // we don't need to call it neither as the scroll position is (0, 0) right
2255 // now anyhow, so just set the parameters directly
2256 m_xScrollPixelsPerLine
= GRID_SCROLL_LINE_X
;
2257 m_yScrollPixelsPerLine
= GRID_SCROLL_LINE_Y
;
2260 // ----------------------------------------------------------------------------
2261 // the idea is to call these functions only when necessary because they create
2262 // quite big arrays which eat memory mostly unnecessary - in particular, if
2263 // default widths/heights are used for all rows/columns, we may not use these
2266 // with some extra code, it should be possible to only store the widths/heights
2267 // different from default ones (resulting in space savings for huge grids) but
2268 // this is not done currently
2269 // ----------------------------------------------------------------------------
2271 void wxGrid::InitRowHeights()
2273 m_rowHeights
.Empty();
2274 m_rowBottoms
.Empty();
2276 m_rowHeights
.Alloc( m_numRows
);
2277 m_rowBottoms
.Alloc( m_numRows
);
2279 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2282 for ( int i
= 0; i
< m_numRows
; i
++ )
2284 rowBottom
+= m_defaultRowHeight
;
2285 m_rowBottoms
.Add( rowBottom
);
2289 void wxGrid::InitColWidths()
2291 m_colWidths
.Empty();
2292 m_colRights
.Empty();
2294 m_colWidths
.Alloc( m_numCols
);
2295 m_colRights
.Alloc( m_numCols
);
2297 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2299 for ( int i
= 0; i
< m_numCols
; i
++ )
2301 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2302 m_colRights
.Add( colRight
);
2306 int wxGrid::GetColWidth(int col
) const
2308 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2311 int wxGrid::GetColLeft(int col
) const
2313 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2314 : m_colRights
[col
] - m_colWidths
[col
];
2317 int wxGrid::GetColRight(int col
) const
2319 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2323 int wxGrid::GetRowHeight(int row
) const
2325 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2328 int wxGrid::GetRowTop(int row
) const
2330 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2331 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2334 int wxGrid::GetRowBottom(int row
) const
2336 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2337 : m_rowBottoms
[row
];
2340 void wxGrid::CalcDimensions()
2342 // compute the size of the scrollable area
2343 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2344 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2349 // take into account editor if shown
2350 if ( IsCellEditControlShown() )
2353 int r
= m_currentCellCoords
.GetRow();
2354 int c
= m_currentCellCoords
.GetCol();
2355 int x
= GetColLeft(c
);
2356 int y
= GetRowTop(r
);
2358 // how big is the editor
2359 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2360 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2361 editor
->GetControl()->GetSize(&w2
, &h2
);
2372 // preserve (more or less) the previous position
2374 GetViewStart( &x
, &y
);
2376 // ensure the position is valid for the new scroll ranges
2378 x
= wxMax( w
- 1, 0 );
2380 y
= wxMax( h
- 1, 0 );
2382 // update the virtual size and refresh the scrollbars to reflect it
2383 m_gridWin
->SetVirtualSize(w
, h
);
2387 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2388 // still must reposition the children
2392 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2394 wxSize
sizeGridWin(size
);
2395 sizeGridWin
.x
-= m_rowLabelWidth
;
2396 sizeGridWin
.y
-= m_colLabelHeight
;
2401 void wxGrid::CalcWindowSizes()
2403 // escape if the window is has not been fully created yet
2405 if ( m_cornerLabelWin
== NULL
)
2409 GetClientSize( &cw
, &ch
);
2411 // the grid may be too small to have enough space for the labels yet, don't
2412 // size the windows to negative sizes in this case
2413 int gw
= cw
- m_rowLabelWidth
;
2414 int gh
= ch
- m_colLabelHeight
;
2420 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2421 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2423 if ( m_colWindow
&& m_colWindow
->IsShown() )
2424 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2426 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2427 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2429 if ( m_gridWin
&& m_gridWin
->IsShown() )
2430 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2433 // this is called when the grid table sends a message
2434 // to indicate that it has been redimensioned
2436 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2439 bool result
= false;
2441 // Clear the attribute cache as the attribute might refer to a different
2442 // cell than stored in the cache after adding/removing rows/columns.
2445 // By the same reasoning, the editor should be dismissed if columns are
2446 // added or removed. And for consistency, it should IMHO always be
2447 // removed, not only if the cell "underneath" it actually changes.
2448 // For now, I intentionally do not save the editor's content as the
2449 // cell it might want to save that stuff to might no longer exist.
2450 HideCellEditControl();
2452 switch ( msg
.GetId() )
2454 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2456 size_t pos
= msg
.GetCommandInt();
2457 int numRows
= msg
.GetCommandInt2();
2459 m_numRows
+= numRows
;
2461 if ( !m_rowHeights
.IsEmpty() )
2463 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2464 m_rowBottoms
.Insert( 0, pos
, numRows
);
2468 bottom
= m_rowBottoms
[pos
- 1];
2470 for ( i
= pos
; i
< m_numRows
; i
++ )
2472 bottom
+= m_rowHeights
[i
];
2473 m_rowBottoms
[i
] = bottom
;
2477 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2479 // if we have just inserted cols into an empty grid the current
2480 // cell will be undefined...
2482 SetCurrentCell( 0, 0 );
2486 m_selection
->UpdateRows( pos
, numRows
);
2487 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2489 attrProvider
->UpdateAttrRows( pos
, numRows
);
2491 if ( !GetBatchCount() )
2494 m_rowLabelWin
->Refresh();
2500 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2502 int numRows
= msg
.GetCommandInt();
2503 int oldNumRows
= m_numRows
;
2504 m_numRows
+= numRows
;
2506 if ( !m_rowHeights
.IsEmpty() )
2508 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2509 m_rowBottoms
.Add( 0, numRows
);
2512 if ( oldNumRows
> 0 )
2513 bottom
= m_rowBottoms
[oldNumRows
- 1];
2515 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2517 bottom
+= m_rowHeights
[i
];
2518 m_rowBottoms
[i
] = bottom
;
2522 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2524 // if we have just inserted cols into an empty grid the current
2525 // cell will be undefined...
2527 SetCurrentCell( 0, 0 );
2530 if ( !GetBatchCount() )
2533 m_rowLabelWin
->Refresh();
2539 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2541 size_t pos
= msg
.GetCommandInt();
2542 int numRows
= msg
.GetCommandInt2();
2543 m_numRows
-= numRows
;
2545 if ( !m_rowHeights
.IsEmpty() )
2547 m_rowHeights
.RemoveAt( pos
, numRows
);
2548 m_rowBottoms
.RemoveAt( pos
, numRows
);
2551 for ( i
= 0; i
< m_numRows
; i
++ )
2553 h
+= m_rowHeights
[i
];
2554 m_rowBottoms
[i
] = h
;
2560 m_currentCellCoords
= wxGridNoCellCoords
;
2564 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2565 m_currentCellCoords
.Set( 0, 0 );
2569 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2570 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2573 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2575 // ifdef'd out following patch from Paul Gammans
2577 // No need to touch column attributes, unless we
2578 // removed _all_ rows, in this case, we remove
2579 // all column attributes.
2580 // I hate to do this here, but the
2581 // needed data is not available inside UpdateAttrRows.
2582 if ( !GetNumberRows() )
2583 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2587 if ( !GetBatchCount() )
2590 m_rowLabelWin
->Refresh();
2596 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2598 size_t pos
= msg
.GetCommandInt();
2599 int numCols
= msg
.GetCommandInt2();
2600 m_numCols
+= numCols
;
2602 if ( m_useNativeHeader
)
2603 GetGridColHeader()->SetColumnCount(m_numCols
);
2605 if ( !m_colAt
.IsEmpty() )
2607 //Shift the column IDs
2609 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2611 if ( m_colAt
[i
] >= (int)pos
)
2612 m_colAt
[i
] += numCols
;
2615 m_colAt
.Insert( pos
, pos
, numCols
);
2617 //Set the new columns' positions
2618 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2624 if ( !m_colWidths
.IsEmpty() )
2626 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2627 m_colRights
.Insert( 0, pos
, numCols
);
2631 right
= m_colRights
[GetColAt( pos
- 1 )];
2634 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2636 i
= GetColAt( colPos
);
2638 right
+= m_colWidths
[i
];
2639 m_colRights
[i
] = right
;
2643 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2645 // if we have just inserted cols into an empty grid the current
2646 // cell will be undefined...
2648 SetCurrentCell( 0, 0 );
2652 m_selection
->UpdateCols( pos
, numCols
);
2653 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2655 attrProvider
->UpdateAttrCols( pos
, numCols
);
2656 if ( !GetBatchCount() )
2659 m_colWindow
->Refresh();
2665 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2667 int numCols
= msg
.GetCommandInt();
2668 int oldNumCols
= m_numCols
;
2669 m_numCols
+= numCols
;
2670 if ( m_useNativeHeader
)
2671 GetGridColHeader()->SetColumnCount(m_numCols
);
2673 if ( !m_colAt
.IsEmpty() )
2675 m_colAt
.Add( 0, numCols
);
2677 //Set the new columns' positions
2679 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2685 if ( !m_colWidths
.IsEmpty() )
2687 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2688 m_colRights
.Add( 0, numCols
);
2691 if ( oldNumCols
> 0 )
2692 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2695 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2697 i
= GetColAt( colPos
);
2699 right
+= m_colWidths
[i
];
2700 m_colRights
[i
] = right
;
2704 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2706 // if we have just inserted cols into an empty grid the current
2707 // cell will be undefined...
2709 SetCurrentCell( 0, 0 );
2711 if ( !GetBatchCount() )
2714 m_colWindow
->Refresh();
2720 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2722 size_t pos
= msg
.GetCommandInt();
2723 int numCols
= msg
.GetCommandInt2();
2724 m_numCols
-= numCols
;
2725 if ( m_useNativeHeader
)
2726 GetGridColHeader()->SetColumnCount(m_numCols
);
2728 if ( !m_colAt
.IsEmpty() )
2730 int colID
= GetColAt( pos
);
2732 m_colAt
.RemoveAt( pos
, numCols
);
2734 //Shift the column IDs
2736 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2738 if ( m_colAt
[colPos
] > colID
)
2739 m_colAt
[colPos
] -= numCols
;
2743 if ( !m_colWidths
.IsEmpty() )
2745 m_colWidths
.RemoveAt( pos
, numCols
);
2746 m_colRights
.RemoveAt( pos
, numCols
);
2750 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2752 i
= GetColAt( colPos
);
2754 w
+= m_colWidths
[i
];
2761 m_currentCellCoords
= wxGridNoCellCoords
;
2765 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2766 m_currentCellCoords
.Set( 0, 0 );
2770 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2771 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2774 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2776 // ifdef'd out following patch from Paul Gammans
2778 // No need to touch row attributes, unless we
2779 // removed _all_ columns, in this case, we remove
2780 // all row attributes.
2781 // I hate to do this here, but the
2782 // needed data is not available inside UpdateAttrCols.
2783 if ( !GetNumberCols() )
2784 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2788 if ( !GetBatchCount() )
2791 m_colWindow
->Refresh();
2798 if (result
&& !GetBatchCount() )
2799 m_gridWin
->Refresh();
2804 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2806 wxRegionIterator
iter( reg
);
2809 wxArrayInt rowlabels
;
2816 // TODO: remove this when we can...
2817 // There is a bug in wxMotif that gives garbage update
2818 // rectangles if you jump-scroll a long way by clicking the
2819 // scrollbar with middle button. This is a work-around
2821 #if defined(__WXMOTIF__)
2823 m_gridWin
->GetClientSize( &cw
, &ch
);
2824 if ( r
.GetTop() > ch
)
2826 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2829 // logical bounds of update region
2832 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2833 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2835 // find the row labels within these bounds
2838 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2840 if ( GetRowBottom(row
) < top
)
2843 if ( GetRowTop(row
) > bottom
)
2846 rowlabels
.Add( row
);
2855 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2857 wxRegionIterator
iter( reg
);
2860 wxArrayInt colLabels
;
2867 // TODO: remove this when we can...
2868 // There is a bug in wxMotif that gives garbage update
2869 // rectangles if you jump-scroll a long way by clicking the
2870 // scrollbar with middle button. This is a work-around
2872 #if defined(__WXMOTIF__)
2874 m_gridWin
->GetClientSize( &cw
, &ch
);
2875 if ( r
.GetLeft() > cw
)
2877 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2880 // logical bounds of update region
2883 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2884 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2886 // find the cells within these bounds
2890 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2892 col
= GetColAt( colPos
);
2894 if ( GetColRight(col
) < left
)
2897 if ( GetColLeft(col
) > right
)
2900 colLabels
.Add( col
);
2909 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2911 wxRegionIterator
iter( reg
);
2914 wxGridCellCoordsArray cellsExposed
;
2916 int left
, top
, right
, bottom
;
2921 // TODO: remove this when we can...
2922 // There is a bug in wxMotif that gives garbage update
2923 // rectangles if you jump-scroll a long way by clicking the
2924 // scrollbar with middle button. This is a work-around
2926 #if defined(__WXMOTIF__)
2928 m_gridWin
->GetClientSize( &cw
, &ch
);
2929 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2930 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2931 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2932 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2935 // logical bounds of update region
2937 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2938 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2940 // find the cells within these bounds
2942 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2944 if ( GetRowBottom(row
) <= top
)
2947 if ( GetRowTop(row
) > bottom
)
2950 // add all dirty cells in this row: notice that the columns which
2951 // are dirty don't depend on the row so we compute them only once
2952 // for the first dirty row and then reuse for all the next ones
2955 // do determine the dirty columns
2956 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2957 cols
.push_back(GetColAt(pos
));
2959 // if there are no dirty columns at all, nothing to do
2964 const size_t count
= cols
.size();
2965 for ( size_t n
= 0; n
< count
; n
++ )
2966 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2972 return cellsExposed
;
2976 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2979 wxPoint
pos( event
.GetPosition() );
2980 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2982 if ( event
.Dragging() )
2986 m_isDragging
= true;
2987 m_rowLabelWin
->CaptureMouse();
2990 if ( event
.LeftIsDown() )
2992 switch ( m_cursorMode
)
2994 case WXGRID_CURSOR_RESIZE_ROW
:
2996 int cw
, ch
, left
, dummy
;
2997 m_gridWin
->GetClientSize( &cw
, &ch
);
2998 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3000 wxClientDC
dc( m_gridWin
);
3003 GetRowTop(m_dragRowOrCol
) +
3004 GetRowMinimalHeight(m_dragRowOrCol
) );
3005 dc
.SetLogicalFunction(wxINVERT
);
3006 if ( m_dragLastPos
>= 0 )
3008 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3010 dc
.DrawLine( left
, y
, left
+cw
, y
);
3015 case WXGRID_CURSOR_SELECT_ROW
:
3017 if ( (row
= YToRow( y
)) >= 0 )
3020 m_selection
->SelectRow(row
, event
);
3025 // default label to suppress warnings about "enumeration value
3026 // 'xxx' not handled in switch
3034 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3039 if (m_rowLabelWin
->HasCapture())
3040 m_rowLabelWin
->ReleaseMouse();
3041 m_isDragging
= false;
3044 // ------------ Entering or leaving the window
3046 if ( event
.Entering() || event
.Leaving() )
3048 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3051 // ------------ Left button pressed
3053 else if ( event
.LeftDown() )
3055 row
= YToEdgeOfRow(y
);
3056 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3058 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3060 else // not a request to start resizing
3064 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3066 if ( !event
.ShiftDown() && !event
.CmdDown() )
3070 if ( event
.ShiftDown() )
3072 m_selection
->SelectBlock
3074 m_currentCellCoords
.GetRow(), 0,
3075 row
, GetNumberCols() - 1,
3081 m_selection
->SelectRow(row
, event
);
3085 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3090 // ------------ Left double click
3092 else if (event
.LeftDClick() )
3094 row
= YToEdgeOfRow(y
);
3095 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3097 // adjust row height depending on label text
3099 // TODO: generate RESIZING event, see #10754
3100 AutoSizeRowLabelSize( row
);
3102 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3104 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3107 else // not on row separator or it's not resizeable
3111 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3113 // no default action at the moment
3118 // ------------ Left button released
3120 else if ( event
.LeftUp() )
3122 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3123 DoEndDragResizeRow(event
);
3125 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3129 // ------------ Right button down
3131 else if ( event
.RightDown() )
3135 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3137 // no default action at the moment
3141 // ------------ Right double click
3143 else if ( event
.RightDClick() )
3147 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3149 // no default action at the moment
3153 // ------------ No buttons down and mouse moving
3155 else if ( event
.Moving() )
3157 m_dragRowOrCol
= YToEdgeOfRow( y
);
3158 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3160 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3162 if ( CanDragRowSize(m_dragRowOrCol
) )
3163 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3166 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3168 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3173 void wxGrid::UpdateColumnSortingIndicator(int col
)
3175 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3177 if ( m_useNativeHeader
)
3178 GetGridColHeader()->UpdateColumn(col
);
3179 else if ( m_nativeColumnLabels
)
3180 m_colWindow
->Refresh();
3181 //else: sorting indicator display not yet implemented in grid version
3184 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3186 if ( col
== m_sortCol
)
3188 // we are already using this column for sorting (or not sorting at all)
3189 // but we might still change the sorting order, check for it
3190 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3192 m_sortIsAscending
= ascending
;
3194 UpdateColumnSortingIndicator(m_sortCol
);
3197 else // we're changing the column used for sorting
3199 const int sortColOld
= m_sortCol
;
3201 // change it before updating the column as we want GetSortingColumn()
3202 // to return the correct new value
3205 if ( sortColOld
!= wxNOT_FOUND
)
3206 UpdateColumnSortingIndicator(sortColOld
);
3208 if ( m_sortCol
!= wxNOT_FOUND
)
3210 m_sortIsAscending
= ascending
;
3211 UpdateColumnSortingIndicator(m_sortCol
);
3216 void wxGrid::DoColHeaderClick(int col
)
3218 // we consider that the grid was resorted if this event is processed and
3220 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3222 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3227 void wxGrid::DoStartResizeCol(int col
)
3229 m_dragRowOrCol
= col
;
3231 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3234 void wxGrid::DoUpdateResizeCol(int x
)
3236 int cw
, ch
, dummy
, top
;
3237 m_gridWin
->GetClientSize( &cw
, &ch
);
3238 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3240 wxClientDC
dc( m_gridWin
);
3243 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3244 dc
.SetLogicalFunction(wxINVERT
);
3245 if ( m_dragLastPos
>= 0 )
3247 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3249 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3253 void wxGrid::DoUpdateResizeColWidth(int w
)
3255 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3258 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3261 wxPoint
pos( event
.GetPosition() );
3262 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3264 int col
= XToCol(x
);
3265 if ( event
.Dragging() )
3269 m_isDragging
= true;
3270 GetColLabelWindow()->CaptureMouse();
3272 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3273 DoStartMoveCol(col
);
3276 if ( event
.LeftIsDown() )
3278 switch ( m_cursorMode
)
3280 case WXGRID_CURSOR_RESIZE_COL
:
3281 DoUpdateResizeCol(x
);
3284 case WXGRID_CURSOR_SELECT_COL
:
3289 m_selection
->SelectCol(col
, event
);
3294 case WXGRID_CURSOR_MOVE_COL
:
3296 int posNew
= XToPos(x
);
3297 int colNew
= GetColAt(posNew
);
3299 // determine the position of the drop marker
3301 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3302 markerX
= GetColRight(colNew
);
3304 markerX
= GetColLeft(colNew
);
3306 if ( markerX
!= m_dragLastPos
)
3308 wxClientDC
dc( GetColLabelWindow() );
3312 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3316 //Clean up the last indicator
3317 if ( m_dragLastPos
>= 0 )
3319 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3321 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3322 dc
.SetPen(wxNullPen
);
3324 if ( XToCol( m_dragLastPos
) != -1 )
3325 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3328 const wxColour
*color
;
3329 //Moving to the same place? Don't draw a marker
3330 if ( colNew
== m_dragRowOrCol
)
3331 color
= wxLIGHT_GREY
;
3336 wxPen
pen( *color
, 2 );
3339 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3341 dc
.SetPen(wxNullPen
);
3343 m_dragLastPos
= markerX
- 1;
3348 // default label to suppress warnings about "enumeration value
3349 // 'xxx' not handled in switch
3357 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3362 if (GetColLabelWindow()->HasCapture())
3363 GetColLabelWindow()->ReleaseMouse();
3364 m_isDragging
= false;
3367 // ------------ Entering or leaving the window
3369 if ( event
.Entering() || event
.Leaving() )
3371 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3374 // ------------ Left button pressed
3376 else if ( event
.LeftDown() )
3378 int col
= XToEdgeOfCol(x
);
3379 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3381 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3383 else // not a request to start resizing
3387 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3389 if ( m_canDragColMove
)
3391 //Show button as pressed
3392 wxClientDC
dc( GetColLabelWindow() );
3393 int colLeft
= GetColLeft( col
);
3394 int colRight
= GetColRight( col
) - 1;
3395 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3396 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3397 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3399 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3403 if ( !event
.ShiftDown() && !event
.CmdDown() )
3407 if ( event
.ShiftDown() )
3409 m_selection
->SelectBlock
3411 0, m_currentCellCoords
.GetCol(),
3412 GetNumberRows() - 1, col
,
3418 m_selection
->SelectCol(col
, event
);
3422 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3428 // ------------ Left double click
3430 if ( event
.LeftDClick() )
3432 const int colEdge
= XToEdgeOfCol(x
);
3433 if ( colEdge
== -1 )
3436 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3438 // no default action at the moment
3443 // adjust column width depending on label text
3445 // TODO: generate RESIZING event, see #10754
3446 AutoSizeColLabelSize( colEdge
);
3448 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3450 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3455 // ------------ Left button released
3457 else if ( event
.LeftUp() )
3459 switch ( m_cursorMode
)
3461 case WXGRID_CURSOR_RESIZE_COL
:
3462 DoEndDragResizeCol(event
);
3465 case WXGRID_CURSOR_MOVE_COL
:
3466 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3468 // the column didn't actually move anywhere
3470 DoColHeaderClick(col
);
3471 m_colWindow
->Refresh(); // "unpress" the column
3475 // get the position of the column we're over
3476 int pos
= XToPos(x
);
3478 // we may need to adjust the drop position but don't bother
3479 // checking for it if we can't anyhow
3482 // also find the index of the column we're over: notice
3483 // that the existing "col" variable may be invalid but
3484 // we need a valid one here
3485 const int colValid
= GetColAt(pos
);
3487 // if we're on the "near" (usually left but right in
3488 // RTL case) part of the column, the actual position we
3489 // should be placed in is actually the one before it
3491 const int middle
= GetColLeft(colValid
) +
3492 GetColWidth(colValid
)/2;
3493 if ( GetLayoutDirection() == wxLayout_LeftToRight
)
3494 onNearPart
= (x
<= middle
);
3495 else // wxLayout_RightToLeft
3496 onNearPart
= (x
> middle
);
3506 case WXGRID_CURSOR_SELECT_COL
:
3507 case WXGRID_CURSOR_SELECT_CELL
:
3508 case WXGRID_CURSOR_RESIZE_ROW
:
3509 case WXGRID_CURSOR_SELECT_ROW
:
3511 DoColHeaderClick(col
);
3515 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3519 // ------------ Right button down
3521 else if ( event
.RightDown() )
3524 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3526 // no default action at the moment
3530 // ------------ Right double click
3532 else if ( event
.RightDClick() )
3535 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3537 // no default action at the moment
3541 // ------------ No buttons down and mouse moving
3543 else if ( event
.Moving() )
3545 m_dragRowOrCol
= XToEdgeOfCol( x
);
3546 if ( m_dragRowOrCol
>= 0 )
3548 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3550 if ( CanDragColSize(m_dragRowOrCol
) )
3551 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3554 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3556 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3561 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3563 if ( event
.LeftDown() )
3565 // indicate corner label by having both row and
3568 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3573 else if ( event
.LeftDClick() )
3575 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3577 else if ( event
.RightDown() )
3579 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3581 // no default action at the moment
3584 else if ( event
.RightDClick() )
3586 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3588 // no default action at the moment
3593 void wxGrid::CancelMouseCapture()
3595 // cancel operation currently in progress, whatever it is
3598 m_isDragging
= false;
3599 m_startDragPos
= wxDefaultPosition
;
3601 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3602 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3603 m_winCapture
= NULL
;
3605 // remove traces of whatever we drew on screen
3610 void wxGrid::ChangeCursorMode(CursorMode mode
,
3615 static const wxChar
*const cursorModes
[] =
3625 wxLogTrace(wxT("grid"),
3626 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3627 win
== m_colWindow
? wxT("colLabelWin")
3628 : win
? wxT("rowLabelWin")
3630 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3631 #endif // wxUSE_LOG_TRACE
3633 if ( mode
== m_cursorMode
&&
3634 win
== m_winCapture
&&
3635 captureMouse
== (m_winCapture
!= NULL
))
3640 // by default use the grid itself
3646 m_winCapture
->ReleaseMouse();
3647 m_winCapture
= NULL
;
3650 m_cursorMode
= mode
;
3652 switch ( m_cursorMode
)
3654 case WXGRID_CURSOR_RESIZE_ROW
:
3655 win
->SetCursor( m_rowResizeCursor
);
3658 case WXGRID_CURSOR_RESIZE_COL
:
3659 win
->SetCursor( m_colResizeCursor
);
3662 case WXGRID_CURSOR_MOVE_COL
:
3663 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3667 win
->SetCursor( *wxSTANDARD_CURSOR
);
3671 // we need to capture mouse when resizing
3672 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3673 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3675 if ( captureMouse
&& resize
)
3677 win
->CaptureMouse();
3682 // ----------------------------------------------------------------------------
3683 // grid mouse event processing
3684 // ----------------------------------------------------------------------------
3687 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3688 const wxGridCellCoords
& coords
,
3691 if ( coords
== wxGridNoCellCoords
)
3692 return; // we're outside any valid cell
3694 // Hide the edit control, so it won't interfere with drag-shrinking.
3695 if ( IsCellEditControlShown() )
3697 HideCellEditControl();
3698 SaveEditControlValue();
3701 switch ( event
.GetModifiers() )
3704 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3705 m_selectedBlockCorner
= coords
;
3706 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3710 if ( CanDragCell() )
3714 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3715 m_selectedBlockCorner
= coords
;
3717 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3722 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3726 // we don't handle the other key modifiers
3731 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3733 wxClientDC
dc(m_gridWin
);
3735 dc
.SetLogicalFunction(wxINVERT
);
3737 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3738 m_gridWin
->GetClientSize());
3740 // erase the previously drawn line, if any
3741 if ( m_dragLastPos
>= 0 )
3742 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3744 // we need the vertical position for rows and horizontal for columns here
3745 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3747 // don't allow resizing beneath the minimal size
3748 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3749 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3750 if ( m_dragLastPos
< posMin
)
3751 m_dragLastPos
= posMin
;
3753 // and draw it at the new position
3754 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3757 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3759 if ( !m_isDragging
)
3761 // Don't start doing anything until the mouse has been dragged far
3763 const wxPoint
& pt
= event
.GetPosition();
3764 if ( m_startDragPos
== wxDefaultPosition
)
3766 m_startDragPos
= pt
;
3770 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3771 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3775 const bool isFirstDrag
= !m_isDragging
;
3776 m_isDragging
= true;
3778 switch ( m_cursorMode
)
3780 case WXGRID_CURSOR_SELECT_CELL
:
3781 DoGridCellDrag(event
, coords
, isFirstDrag
);
3784 case WXGRID_CURSOR_RESIZE_ROW
:
3785 DoGridLineDrag(event
, wxGridRowOperations());
3788 case WXGRID_CURSOR_RESIZE_COL
:
3789 DoGridLineDrag(event
, wxGridColumnOperations());
3798 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
3800 m_winCapture
= m_gridWin
;
3801 m_winCapture
->CaptureMouse();
3806 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3807 const wxGridCellCoords
& coords
,
3810 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3812 // event handled by user code, no need to do anything here
3816 if ( !event
.CmdDown() )
3819 if ( event
.ShiftDown() )
3823 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3824 m_selectedBlockCorner
= coords
;
3827 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3829 DisableCellEditControl();
3830 MakeCellVisible( coords
);
3832 if ( event
.CmdDown() )
3836 m_selection
->ToggleCellSelection(coords
, event
);
3839 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3840 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3841 m_selectedBlockCorner
= coords
;
3847 // In row or column selection mode just clicking on the cell
3848 // should select the row or column containing it: this is more
3849 // convenient for the kinds of controls that use such selection
3850 // mode and is compatible with 2.8 behaviour (see #12062).
3851 switch ( m_selection
->GetSelectionMode() )
3853 case wxGridSelectCells
:
3854 case wxGridSelectRowsOrColumns
:
3855 // nothing to do in these cases
3858 case wxGridSelectRows
:
3859 m_selection
->SelectRow(coords
.GetRow());
3862 case wxGridSelectColumns
:
3863 m_selection
->SelectCol(coords
.GetCol());
3868 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3869 coords
!= wxGridNoCellCoords
;
3870 SetCurrentCell( coords
);
3876 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3877 const wxGridCellCoords
& coords
,
3880 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3882 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3884 // we want double click to select a cell and start editing
3885 // (i.e. to behave in same way as sequence of two slow clicks):
3886 m_waitForSlowClick
= true;
3892 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3894 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3898 m_winCapture
->ReleaseMouse();
3899 m_winCapture
= NULL
;
3902 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3905 EnableCellEditControl();
3907 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3908 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3909 editor
->StartingClick();
3913 m_waitForSlowClick
= false;
3915 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3916 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3920 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3921 m_selectedBlockBottomRight
,
3925 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3926 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3928 // Show the edit control, if it has been hidden for
3930 ShowCellEditControl();
3933 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3935 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3936 DoEndDragResizeRow(event
);
3938 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3940 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3941 DoEndDragResizeCol(event
);
3948 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3949 const wxGridCellCoords
& coords
,
3952 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3954 // out of grid cell area
3955 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3959 int dragRow
= YToEdgeOfRow( pos
.y
);
3960 int dragCol
= XToEdgeOfCol( pos
.x
);
3962 // Dragging on the corner of a cell to resize in both
3963 // directions is not implemented yet...
3965 if ( dragRow
>= 0 && dragCol
>= 0 )
3967 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3971 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3973 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3975 m_dragRowOrCol
= dragRow
;
3976 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3979 // When using the native header window we can only resize the columns by
3980 // dragging the dividers in it because we can't make it enter into the
3981 // column resizing mode programmatically
3982 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3983 CanDragGridSize() && CanDragColSize(dragCol
) )
3985 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3987 m_dragRowOrCol
= dragCol
;
3988 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3991 else // Neither on a row or col edge
3993 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3995 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4000 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
4002 if ( event
.Entering() || event
.Leaving() )
4004 // we don't care about these events but we must not reset m_isDragging
4005 // if they happen so return before anything else is done
4010 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
4012 // coordinates of the cell under mouse
4013 wxGridCellCoords coords
= XYToCell(pos
);
4015 int cell_rows
, cell_cols
;
4016 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4017 if ( (cell_rows
< 0) || (cell_cols
< 0) )
4019 coords
.SetRow(coords
.GetRow() + cell_rows
);
4020 coords
.SetCol(coords
.GetCol() + cell_cols
);
4023 if ( event
.Dragging() )
4025 if ( event
.LeftIsDown() )
4026 DoGridDragEvent(event
, coords
);
4032 m_isDragging
= false;
4033 m_startDragPos
= wxDefaultPosition
;
4035 // deal with various button presses
4036 if ( event
.IsButton() )
4038 if ( coords
!= wxGridNoCellCoords
)
4040 DisableCellEditControl();
4042 if ( event
.LeftDown() )
4043 DoGridCellLeftDown(event
, coords
, pos
);
4044 else if ( event
.LeftDClick() )
4045 DoGridCellLeftDClick(event
, coords
, pos
);
4046 else if ( event
.RightDown() )
4047 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
4048 else if ( event
.RightDClick() )
4049 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
4052 // this one should be called even if we're not over any cell
4053 if ( event
.LeftUp() )
4055 DoGridCellLeftUp(event
, coords
);
4058 else if ( event
.Moving() )
4060 DoGridMouseMoveEvent(event
, coords
, pos
);
4062 else // unknown mouse event?
4068 // this function returns true only if the size really changed
4069 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4071 if ( m_dragLastPos
== -1 )
4074 const wxGridOperations
& doper
= oper
.Dual();
4076 const wxSize size
= m_gridWin
->GetClientSize();
4078 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4080 // erase the last line we drew
4081 wxClientDC
dc(m_gridWin
);
4083 dc
.SetLogicalFunction(wxINVERT
);
4085 const int posLineStart
= oper
.Select(ptOrigin
);
4086 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4088 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4090 // temporarily hide the edit control before resizing
4091 HideCellEditControl();
4092 SaveEditControlValue();
4094 // do resize the line
4095 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4096 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4097 oper
.SetLineSize(this, m_dragRowOrCol
,
4098 wxMax(m_dragLastPos
- lineStart
,
4099 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4101 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4105 // refresh now if we're not frozen
4106 if ( !GetBatchCount() )
4108 // we need to refresh everything beyond the resized line in the header
4111 // get the position from which to refresh in the other direction
4112 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4113 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4115 // we only need the ordinate (for rows) or abscissa (for columns) here,
4116 // and need to cover the entire window in the other direction
4117 oper
.Select(rect
) = 0;
4119 wxRect
rectHeader(rect
.GetPosition(),
4122 oper
.GetHeaderWindowSize(this),
4123 doper
.Select(size
) - doper
.Select(rect
)
4126 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4129 // also refresh the grid window: extend the rectangle
4132 oper
.SelectSize(rect
) = oper
.Select(size
);
4134 int subtractLines
= 0;
4135 const int lineStart
= oper
.PosToLine(this, posLineStart
);
4136 if ( lineStart
>= 0 )
4138 // ensure that if we have a multi-cell block we redraw all of
4139 // it by increasing the refresh area to cover it entirely if a
4140 // part of it is affected
4141 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
4142 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
4144 int cellLines
= oper
.Select(
4145 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4146 if ( cellLines
< subtractLines
)
4147 subtractLines
= cellLines
;
4152 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4153 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4155 doper
.Select(rect
) = startPos
;
4156 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4158 m_gridWin
->Refresh(false, &rect
);
4162 // show the edit control back again
4163 ShowCellEditControl();
4168 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4170 // TODO: generate RESIZING event, see #10754
4172 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4173 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4176 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4178 // TODO: generate RESIZING event, see #10754
4180 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4181 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4184 void wxGrid::DoStartMoveCol(int col
)
4186 m_dragRowOrCol
= col
;
4189 void wxGrid::DoEndMoveCol(int pos
)
4191 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4193 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4194 SetColPos(m_dragRowOrCol
, pos
);
4195 //else: vetoed by user
4197 m_dragRowOrCol
= -1;
4200 void wxGrid::RefreshAfterColPosChange()
4202 // recalculate the column rights as the column positions have changed,
4203 // unless we calculate them dynamically because all columns widths are the
4204 // same and it's easy to do
4205 if ( !m_colWidths
.empty() )
4208 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4210 int colID
= GetColAt( colPos
);
4212 colRight
+= m_colWidths
[colID
];
4213 m_colRights
[colID
] = colRight
;
4217 // and make the changes visible
4218 if ( m_useNativeHeader
)
4220 if ( m_colAt
.empty() )
4221 GetGridColHeader()->ResetColumnsOrder();
4223 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4227 m_colWindow
->Refresh();
4229 m_gridWin
->Refresh();
4232 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4236 RefreshAfterColPosChange();
4239 void wxGrid::SetColPos(int idx
, int pos
)
4241 // we're going to need m_colAt now, initialize it if needed
4242 if ( m_colAt
.empty() )
4244 m_colAt
.reserve(m_numCols
);
4245 for ( int i
= 0; i
< m_numCols
; i
++ )
4246 m_colAt
.push_back(i
);
4249 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4251 RefreshAfterColPosChange();
4254 void wxGrid::ResetColPos()
4258 RefreshAfterColPosChange();
4261 void wxGrid::EnableDragColMove( bool enable
)
4263 if ( m_canDragColMove
== enable
)
4266 if ( m_useNativeHeader
)
4268 // update all columns to make them [not] reorderable
4269 GetGridColHeader()->SetColumnCount(m_numCols
);
4272 m_canDragColMove
= enable
;
4274 // we use to call ResetColPos() from here if !enable but this doesn't seem
4275 // right as it would mean there would be no way to "freeze" the current
4276 // columns order by disabling moving them after putting them in the desired
4277 // order, whereas now you can always call ResetColPos() manually if needed
4282 // ------ interaction with data model
4284 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4286 switch ( msg
.GetId() )
4288 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4289 return GetModelValues();
4291 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4292 return SetModelValues();
4294 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4295 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4296 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4297 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4298 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4299 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4300 return Redimension( msg
);
4307 // The behaviour of this function depends on the grid table class
4308 // Clear() function. For the default wxGridStringTable class the
4309 // behaviour is to replace all cell contents with wxEmptyString but
4310 // not to change the number of rows or cols.
4312 void wxGrid::ClearGrid()
4316 if (IsCellEditControlEnabled())
4317 DisableCellEditControl();
4320 if (!GetBatchCount())
4321 m_gridWin
->Refresh();
4326 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4327 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4329 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4334 if ( IsCellEditControlEnabled() )
4335 DisableCellEditControl();
4337 return (m_table
->*funcModify
)(pos
, num
);
4339 // the table will have sent the results of the insert row
4340 // operation to this view object as a grid table message
4344 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4345 int num
, bool WXUNUSED(updateLabels
))
4347 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4352 return (m_table
->*funcAppend
)(num
);
4355 // ----------------------------------------------------------------------------
4356 // event generation helpers
4357 // ----------------------------------------------------------------------------
4360 wxGrid::SendGridSizeEvent(wxEventType type
,
4362 const wxMouseEvent
& mouseEv
)
4364 int rowOrCol
= row
== -1 ? col
: row
;
4366 wxGridSizeEvent
gridEvt( GetId(),
4370 mouseEv
.GetX() + GetRowLabelSize(),
4371 mouseEv
.GetY() + GetColLabelSize(),
4374 GetEventHandler()->ProcessEvent(gridEvt
);
4377 // Generate a grid event based on a mouse event and return:
4378 // -1 if the event was vetoed
4379 // +1 if the event was processed (but not vetoed)
4380 // 0 if the event wasn't handled
4382 wxGrid::SendEvent(const wxEventType type
,
4384 const wxMouseEvent
& mouseEv
)
4386 bool claimed
, vetoed
;
4388 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4390 // Right now, it should _never_ end up here!
4391 wxGridRangeSelectEvent
gridEvt( GetId(),
4394 m_selectedBlockTopLeft
,
4395 m_selectedBlockBottomRight
,
4399 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4400 vetoed
= !gridEvt
.IsAllowed();
4402 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4403 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4404 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4405 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4407 wxPoint pos
= mouseEv
.GetPosition();
4409 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4410 pos
.y
+= GetColLabelSize();
4411 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4412 pos
.x
+= GetRowLabelSize();
4414 wxGridEvent
gridEvt( GetId(),
4422 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4423 vetoed
= !gridEvt
.IsAllowed();
4427 wxGridEvent
gridEvt( GetId(),
4431 mouseEv
.GetX() + GetRowLabelSize(),
4432 mouseEv
.GetY() + GetColLabelSize(),
4435 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4436 vetoed
= !gridEvt
.IsAllowed();
4439 // A Veto'd event may not be `claimed' so test this first
4443 return claimed
? 1 : 0;
4446 // Generate a grid event of specified type, return value same as above
4449 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4451 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4452 gridEvt
.SetString(s
);
4454 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4456 // A Veto'd event may not be `claimed' so test this first
4457 if ( !gridEvt
.IsAllowed() )
4460 return claimed
? 1 : 0;
4463 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4465 // needed to prevent zillions of paint events on MSW
4469 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4471 // Don't do anything if between Begin/EndBatch...
4472 // EndBatch() will do all this on the last nested one anyway.
4473 if ( m_created
&& !GetBatchCount() )
4475 // Refresh to get correct scrolled position:
4476 wxScrolledWindow::Refresh(eraseb
, rect
);
4480 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4481 int width_label
, width_cell
, height_label
, height_cell
;
4484 // Copy rectangle can get scroll offsets..
4485 rect_x
= rect
->GetX();
4486 rect_y
= rect
->GetY();
4487 rectWidth
= rect
->GetWidth();
4488 rectHeight
= rect
->GetHeight();
4490 width_label
= m_rowLabelWidth
- rect_x
;
4491 if (width_label
> rectWidth
)
4492 width_label
= rectWidth
;
4494 height_label
= m_colLabelHeight
- rect_y
;
4495 if (height_label
> rectHeight
)
4496 height_label
= rectHeight
;
4498 if (rect_x
> m_rowLabelWidth
)
4500 x
= rect_x
- m_rowLabelWidth
;
4501 width_cell
= rectWidth
;
4506 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4509 if (rect_y
> m_colLabelHeight
)
4511 y
= rect_y
- m_colLabelHeight
;
4512 height_cell
= rectHeight
;
4517 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4520 // Paint corner label part intersecting rect.
4521 if ( width_label
> 0 && height_label
> 0 )
4523 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4524 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4527 // Paint col labels part intersecting rect.
4528 if ( width_cell
> 0 && height_label
> 0 )
4530 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4531 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4534 // Paint row labels part intersecting rect.
4535 if ( width_label
> 0 && height_cell
> 0 )
4537 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4538 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4541 // Paint cell area part intersecting rect.
4542 if ( width_cell
> 0 && height_cell
> 0 )
4544 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4545 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4550 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4551 m_colWindow
->Refresh(eraseb
, NULL
);
4552 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4553 m_gridWin
->Refresh(eraseb
, NULL
);
4558 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4560 if (m_targetWindow
!= this) // check whether initialisation has been done
4562 // reposition our children windows
4567 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4569 if ( m_inOnKeyDown
)
4571 // shouldn't be here - we are going round in circles...
4573 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4576 m_inOnKeyDown
= true;
4578 // propagate the event up and see if it gets processed
4579 wxWindow
*parent
= GetParent();
4580 wxKeyEvent
keyEvt( event
);
4581 keyEvt
.SetEventObject( parent
);
4583 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4585 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4587 if (event
.GetKeyCode() == WXK_RIGHT
)
4588 event
.m_keyCode
= WXK_LEFT
;
4589 else if (event
.GetKeyCode() == WXK_LEFT
)
4590 event
.m_keyCode
= WXK_RIGHT
;
4593 // try local handlers
4594 switch ( event
.GetKeyCode() )
4597 if ( event
.ControlDown() )
4598 MoveCursorUpBlock( event
.ShiftDown() );
4600 MoveCursorUp( event
.ShiftDown() );
4604 if ( event
.ControlDown() )
4605 MoveCursorDownBlock( event
.ShiftDown() );
4607 MoveCursorDown( event
.ShiftDown() );
4611 if ( event
.ControlDown() )
4612 MoveCursorLeftBlock( event
.ShiftDown() );
4614 MoveCursorLeft( event
.ShiftDown() );
4618 if ( event
.ControlDown() )
4619 MoveCursorRightBlock( event
.ShiftDown() );
4621 MoveCursorRight( event
.ShiftDown() );
4625 case WXK_NUMPAD_ENTER
:
4626 if ( event
.ControlDown() )
4628 event
.Skip(); // to let the edit control have the return
4632 if ( GetGridCursorRow() < GetNumberRows()-1 )
4634 MoveCursorDown( event
.ShiftDown() );
4638 // at the bottom of a column
4639 DisableCellEditControl();
4649 if (event
.ShiftDown())
4651 if ( GetGridCursorCol() > 0 )
4653 MoveCursorLeft( false );
4658 DisableCellEditControl();
4663 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4665 MoveCursorRight( false );
4670 DisableCellEditControl();
4676 GoToCell(event
.ControlDown() ? 0
4677 : m_currentCellCoords
.GetRow(),
4682 GoToCell(event
.ControlDown() ? m_numRows
- 1
4683 : m_currentCellCoords
.GetRow(),
4696 // Ctrl-Space selects the current column, Shift-Space -- the
4697 // current row and Ctrl-Shift-Space -- everything
4698 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4701 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4705 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4708 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4709 m_selection
->SelectBlock(0, 0,
4710 m_numRows
- 1, m_numCols
- 1);
4714 if ( !IsEditable() )
4716 MoveCursorRight(false);
4719 //else: fall through
4732 m_inOnKeyDown
= false;
4735 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4737 // try local handlers
4739 if ( event
.GetKeyCode() == WXK_SHIFT
)
4741 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4742 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4746 m_selection
->SelectBlock(
4747 m_selectedBlockTopLeft
,
4748 m_selectedBlockBottomRight
,
4753 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4754 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4755 m_selectedBlockCorner
= wxGridNoCellCoords
;
4759 void wxGrid::OnChar( wxKeyEvent
& event
)
4761 // is it possible to edit the current cell at all?
4762 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4764 // yes, now check whether the cells editor accepts the key
4765 int row
= m_currentCellCoords
.GetRow();
4766 int col
= m_currentCellCoords
.GetCol();
4767 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4768 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4770 // <F2> is special and will always start editing, for
4771 // other keys - ask the editor itself
4772 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4773 || editor
->IsAcceptedKey(event
) )
4775 // ensure cell is visble
4776 MakeCellVisible(row
, col
);
4777 EnableCellEditControl();
4779 // a problem can arise if the cell is not completely
4780 // visible (even after calling MakeCellVisible the
4781 // control is not created and calling StartingKey will
4783 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4784 editor
->StartingKey(event
);
4800 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4804 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4806 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4808 // the event has been vetoed - do nothing
4812 #if !defined(__WXMAC__)
4813 wxClientDC
dc( m_gridWin
);
4817 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4819 DisableCellEditControl();
4821 if ( IsVisible( m_currentCellCoords
, false ) )
4824 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4825 if ( !m_gridLinesEnabled
)
4833 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4835 // Otherwise refresh redraws the highlight!
4836 m_currentCellCoords
= coords
;
4838 #if defined(__WXMAC__)
4839 m_gridWin
->Refresh(true /*, & r */);
4841 DrawGridCellArea( dc
, cells
);
4842 DrawAllGridLines( dc
, r
);
4847 m_currentCellCoords
= coords
;
4849 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4850 #if !defined(__WXMAC__)
4851 DrawCellHighlight( dc
, attr
);
4859 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4860 int bottomRow
, int rightCol
)
4864 switch ( m_selection
->GetSelectionMode() )
4867 wxFAIL_MSG( "unknown selection mode" );
4870 case wxGridSelectCells
:
4871 // arbitrary blocks selection allowed so just use the cell
4872 // coordinates as is
4875 case wxGridSelectRows
:
4876 // only full rows selection allowd, ensure that we do select
4879 rightCol
= GetNumberCols() - 1;
4882 case wxGridSelectColumns
:
4883 // same as above but for columns
4885 bottomRow
= GetNumberRows() - 1;
4888 case wxGridSelectRowsOrColumns
:
4889 // in this mode we can select only full rows or full columns so
4890 // it doesn't make sense to select blocks at all (and we can't
4891 // extend the block because there is no preferred direction, we
4892 // could only extend it to cover the entire grid but this is
4898 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4899 MakeCellVisible(m_selectedBlockCorner
);
4901 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4902 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4904 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4905 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4907 // First the case that we selected a completely new area
4908 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4909 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4912 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4913 wxGridCellCoords ( bottomRow
, rightCol
) );
4914 m_gridWin
->Refresh( false, &rect
);
4917 // Now handle changing an existing selection area.
4918 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4919 m_selectedBlockBottomRight
!= updateBottomRight
)
4921 // Compute two optimal update rectangles:
4922 // Either one rectangle is a real subset of the
4923 // other, or they are (almost) disjoint!
4925 bool need_refresh
[4];
4929 need_refresh
[3] = false;
4932 // Store intermediate values
4933 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4934 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4935 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4936 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4938 // Determine the outer/inner coordinates.
4939 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4940 EnsureFirstLessThanSecond(oldTop
, topRow
);
4941 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4942 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4944 // Now, either the stuff marked old is the outer
4945 // rectangle or we don't have a situation where one
4946 // is contained in the other.
4948 if ( oldLeft
< leftCol
)
4950 // Refresh the newly selected or deselected
4951 // area to the left of the old or new selection.
4952 need_refresh
[0] = true;
4953 rect
[0] = BlockToDeviceRect(
4954 wxGridCellCoords( oldTop
, oldLeft
),
4955 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4958 if ( oldTop
< topRow
)
4960 // Refresh the newly selected or deselected
4961 // area above the old or new selection.
4962 need_refresh
[1] = true;
4963 rect
[1] = BlockToDeviceRect(
4964 wxGridCellCoords( oldTop
, leftCol
),
4965 wxGridCellCoords( topRow
- 1, rightCol
) );
4968 if ( oldRight
> rightCol
)
4970 // Refresh the newly selected or deselected
4971 // area to the right of the old or new selection.
4972 need_refresh
[2] = true;
4973 rect
[2] = BlockToDeviceRect(
4974 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4975 wxGridCellCoords( oldBottom
, oldRight
) );
4978 if ( oldBottom
> bottomRow
)
4980 // Refresh the newly selected or deselected
4981 // area below the old or new selection.
4982 need_refresh
[3] = true;
4983 rect
[3] = BlockToDeviceRect(
4984 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4985 wxGridCellCoords( oldBottom
, rightCol
) );
4988 // various Refresh() calls
4989 for (i
= 0; i
< 4; i
++ )
4990 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4991 m_gridWin
->Refresh( false, &(rect
[i
]) );
4995 m_selectedBlockTopLeft
= updateTopLeft
;
4996 m_selectedBlockBottomRight
= updateBottomRight
;
5000 // ------ functions to get/send data (see also public functions)
5003 bool wxGrid::GetModelValues()
5005 // Hide the editor, so it won't hide a changed value.
5006 HideCellEditControl();
5010 // all we need to do is repaint the grid
5012 m_gridWin
->Refresh();
5019 bool wxGrid::SetModelValues()
5023 // Disable the editor, so it won't hide a changed value.
5024 // Do we also want to save the current value of the editor first?
5026 DisableCellEditControl();
5030 for ( row
= 0; row
< m_numRows
; row
++ )
5032 for ( col
= 0; col
< m_numCols
; col
++ )
5034 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5044 // Note - this function only draws cells that are in the list of
5045 // exposed cells (usually set from the update region by
5046 // CalcExposedCells)
5048 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5050 if ( !m_numRows
|| !m_numCols
)
5053 int i
, numCells
= cells
.GetCount();
5054 int row
, col
, cell_rows
, cell_cols
;
5055 wxGridCellCoordsArray redrawCells
;
5057 for ( i
= numCells
- 1; i
>= 0; i
-- )
5059 row
= cells
[i
].GetRow();
5060 col
= cells
[i
].GetCol();
5061 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5063 // If this cell is part of a multicell block, find owner for repaint
5064 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5066 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5067 bool marked
= false;
5068 for ( int j
= 0; j
< numCells
; j
++ )
5070 if ( cell
== cells
[j
] )
5079 int count
= redrawCells
.GetCount();
5080 for (int j
= 0; j
< count
; j
++)
5082 if ( cell
== redrawCells
[j
] )
5090 redrawCells
.Add( cell
);
5093 // don't bother drawing this cell
5097 // If this cell is empty, find cell to left that might want to overflow
5098 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5100 for ( int l
= 0; l
< cell_rows
; l
++ )
5102 // find a cell in this row to leave already marked for repaint
5104 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5105 if ((redrawCells
[k
].GetCol() < left
) &&
5106 (redrawCells
[k
].GetRow() == row
))
5108 left
= redrawCells
[k
].GetCol();
5112 left
= 0; // oh well
5114 for (int j
= col
- 1; j
>= left
; j
--)
5116 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5118 if (GetCellOverflow(row
+ l
, j
))
5120 wxGridCellCoords
cell(row
+ l
, j
);
5121 bool marked
= false;
5123 for (int k
= 0; k
< numCells
; k
++)
5125 if ( cell
== cells
[k
] )
5134 int count
= redrawCells
.GetCount();
5135 for (int k
= 0; k
< count
; k
++)
5137 if ( cell
== redrawCells
[k
] )
5144 redrawCells
.Add( cell
);
5153 DrawCell( dc
, cells
[i
] );
5156 numCells
= redrawCells
.GetCount();
5158 for ( i
= numCells
- 1; i
>= 0; i
-- )
5160 DrawCell( dc
, redrawCells
[i
] );
5164 void wxGrid::DrawGridSpace( wxDC
& dc
)
5167 m_gridWin
->GetClientSize( &cw
, &ch
);
5170 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5172 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5173 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5175 if ( right
> rightCol
|| bottom
> bottomRow
)
5178 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5180 dc
.SetBrush(GetDefaultCellBackgroundColour());
5181 dc
.SetPen( *wxTRANSPARENT_PEN
);
5183 if ( right
> rightCol
)
5185 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5188 if ( bottom
> bottomRow
)
5190 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5195 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5197 int row
= coords
.GetRow();
5198 int col
= coords
.GetCol();
5200 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5203 // we draw the cell border ourselves
5204 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5206 bool isCurrent
= coords
== m_currentCellCoords
;
5208 wxRect rect
= CellToRect( row
, col
);
5210 // if the editor is shown, we should use it and not the renderer
5211 // Note: However, only if it is really _shown_, i.e. not hidden!
5212 if ( isCurrent
&& IsCellEditControlShown() )
5214 // NB: this "#if..." is temporary and fixes a problem where the
5215 // edit control is erased by this code after being rendered.
5216 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5217 // implicitly, causing this out-of order render.
5218 #if !defined(__WXMAC__)
5219 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5220 editor
->PaintBackground(rect
, attr
);
5226 // but all the rest is drawn by the cell renderer and hence may be customized
5227 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5228 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5235 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5237 // don't show highlight when the grid doesn't have focus
5241 int row
= m_currentCellCoords
.GetRow();
5242 int col
= m_currentCellCoords
.GetCol();
5244 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5247 wxRect rect
= CellToRect(row
, col
);
5249 // hmmm... what could we do here to show that the cell is disabled?
5250 // for now, I just draw a thinner border than for the other ones, but
5251 // it doesn't look really good
5253 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5257 // The center of the drawn line is where the position/width/height of
5258 // the rectangle is actually at (on wxMSW at least), so the
5259 // size of the rectangle is reduced to compensate for the thickness of
5260 // the line. If this is too strange on non-wxMSW platforms then
5261 // please #ifdef this appropriately.
5262 rect
.x
+= penWidth
/ 2;
5263 rect
.y
+= penWidth
/ 2;
5264 rect
.width
-= penWidth
- 1;
5265 rect
.height
-= penWidth
- 1;
5267 // Now draw the rectangle
5268 // use the cellHighlightColour if the cell is inside a selection, this
5269 // will ensure the cell is always visible.
5270 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5271 : m_cellHighlightColour
,
5273 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5274 dc
.DrawRectangle(rect
);
5278 wxPen
wxGrid::GetDefaultGridLinePen()
5280 return wxPen(GetGridLineColour());
5283 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5285 return GetDefaultGridLinePen();
5288 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5290 return GetDefaultGridLinePen();
5293 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5295 int row
= coords
.GetRow();
5296 int col
= coords
.GetCol();
5297 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5301 wxRect rect
= CellToRect( row
, col
);
5303 // right hand border
5304 dc
.SetPen( GetColGridLinePen(col
) );
5305 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5306 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5309 dc
.SetPen( GetRowGridLinePen(row
) );
5310 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5311 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5314 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5316 // This if block was previously in wxGrid::OnPaint but that doesn't
5317 // seem to get called under wxGTK - MB
5319 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5320 m_numRows
&& m_numCols
)
5322 m_currentCellCoords
.Set(0, 0);
5325 if ( IsCellEditControlShown() )
5327 // don't show highlight when the edit control is shown
5331 // if the active cell was repainted, repaint its highlight too because it
5332 // might have been damaged by the grid lines
5333 size_t count
= cells
.GetCount();
5334 for ( size_t n
= 0; n
< count
; n
++ )
5336 wxGridCellCoords cell
= cells
[n
];
5338 // If we are using attributes, then we may have just exposed another
5339 // cell in a partially-visible merged cluster of cells. If the "anchor"
5340 // (upper left) cell of this merged cluster is the cell indicated by
5341 // m_currentCellCoords, then we need to refresh the cell highlight even
5342 // though the "anchor" itself is not part of our update segment.
5343 if ( CanHaveAttributes() )
5347 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5350 cell
.SetRow(cell
.GetRow() + rows
);
5353 cell
.SetCol(cell
.GetCol() + cols
);
5356 if ( cell
== m_currentCellCoords
)
5358 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5359 DrawCellHighlight(dc
, attr
);
5367 // This is used to redraw all grid lines e.g. when the grid line colour
5370 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5372 if ( !m_gridLinesEnabled
)
5375 int top
, bottom
, left
, right
;
5378 m_gridWin
->GetClientSize(&cw
, &ch
);
5379 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5380 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5382 // avoid drawing grid lines past the last row and col
5383 if ( m_gridLinesClipHorz
)
5388 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5389 if ( right
> lastColRight
)
5390 right
= lastColRight
;
5393 if ( m_gridLinesClipVert
)
5398 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5399 if ( bottom
> lastRowBottom
)
5400 bottom
= lastRowBottom
;
5403 // no gridlines inside multicells, clip them out
5404 int leftCol
= GetColPos( internalXToCol(left
) );
5405 int topRow
= internalYToRow(top
);
5406 int rightCol
= GetColPos( internalXToCol(right
) );
5407 int bottomRow
= internalYToRow(bottom
);
5409 wxRegion
clippedcells(0, 0, cw
, ch
);
5411 int cell_rows
, cell_cols
;
5414 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5416 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5418 int i
= GetColAt( colPos
);
5420 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5421 if ((cell_rows
> 1) || (cell_cols
> 1))
5423 rect
= CellToRect(j
,i
);
5424 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5425 clippedcells
.Subtract(rect
);
5427 else if ((cell_rows
< 0) || (cell_cols
< 0))
5429 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5430 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5431 clippedcells
.Subtract(rect
);
5436 dc
.SetDeviceClippingRegion( clippedcells
);
5439 // horizontal grid lines
5440 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5442 int bot
= GetRowBottom(i
) - 1;
5449 dc
.SetPen( GetRowGridLinePen(i
) );
5450 dc
.DrawLine( left
, bot
, right
, bot
);
5454 // vertical grid lines
5455 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5457 int i
= GetColAt( colPos
);
5459 int colRight
= GetColRight(i
);
5461 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5465 if ( colRight
> right
)
5468 if ( colRight
>= left
)
5470 dc
.SetPen( GetColGridLinePen(i
) );
5471 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5475 dc
.DestroyClippingRegion();
5478 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5483 const size_t numLabels
= rows
.GetCount();
5484 for ( size_t i
= 0; i
< numLabels
; i
++ )
5486 DrawRowLabel( dc
, rows
[i
] );
5490 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5492 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5495 wxGridCellAttrProvider
* const
5496 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5498 // notice that an explicit static_cast is needed to avoid a compilation
5499 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5500 // wxGridRowHeaderRenderer class without it
5501 const wxGridRowHeaderRenderer
&
5502 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5503 : static_cast<const wxGridRowHeaderRenderer
&>
5504 (gs_defaultHeaderRenderers
.rowRenderer
);
5506 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5507 rend
.DrawBorder(*this, dc
, rect
);
5510 GetRowLabelAlignment(&hAlign
, &vAlign
);
5512 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5513 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5516 void wxGrid::UseNativeColHeader(bool native
)
5518 if ( native
== m_useNativeHeader
)
5522 m_useNativeHeader
= native
;
5524 CreateColumnWindow();
5526 if ( m_useNativeHeader
)
5527 GetGridColHeader()->SetColumnCount(m_numCols
);
5531 void wxGrid::SetUseNativeColLabels( bool native
)
5533 wxASSERT_MSG( !m_useNativeHeader
,
5534 "doesn't make sense when using native header" );
5536 m_nativeColumnLabels
= native
;
5539 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5540 SetColLabelSize( height
);
5543 GetColLabelWindow()->Refresh();
5544 m_cornerLabelWin
->Refresh();
5547 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5552 const size_t numLabels
= cols
.GetCount();
5553 for ( size_t i
= 0; i
< numLabels
; i
++ )
5555 DrawColLabel( dc
, cols
[i
] );
5559 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5561 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5563 if ( m_nativeColumnLabels
)
5567 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5574 wxGridCellAttrProvider
* const
5575 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5576 const wxGridCornerHeaderRenderer
&
5577 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5578 : static_cast<wxGridCornerHeaderRenderer
&>
5579 (gs_defaultHeaderRenderers
.cornerRenderer
);
5581 rend
.DrawBorder(*this, dc
, rect
);
5585 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5587 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5590 int colLeft
= GetColLeft(col
);
5592 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5593 wxGridCellAttrProvider
* const
5594 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5595 const wxGridColumnHeaderRenderer
&
5596 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
5597 : static_cast<wxGridColumnHeaderRenderer
&>
5598 (gs_defaultHeaderRenderers
.colRenderer
);
5600 if ( m_nativeColumnLabels
)
5602 wxRendererNative::Get().DrawHeaderButton
5604 GetColLabelWindow(),
5609 ? IsSortOrderAscending()
5610 ? wxHDR_SORT_ICON_UP
5611 : wxHDR_SORT_ICON_DOWN
5612 : wxHDR_SORT_ICON_NONE
5618 // It is reported that we need to erase the background to avoid display
5619 // artefacts, see #12055.
5620 wxDCBrushChanger
setBrush(dc
, m_colWindow
->GetBackgroundColour());
5621 dc
.DrawRectangle(rect
);
5623 rend
.DrawBorder(*this, dc
, rect
);
5627 GetColLabelAlignment(&hAlign
, &vAlign
);
5628 const int orient
= GetColLabelTextOrientation();
5630 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5633 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5634 // we just have to add textOrientation support
5635 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5636 const wxString
& value
,
5640 int textOrientation
) const
5642 wxArrayString lines
;
5644 StringToLines( value
, lines
);
5646 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5649 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5650 const wxArrayString
& lines
,
5654 int textOrientation
) const
5656 if ( lines
.empty() )
5659 wxDCClipper
clip(dc
, rect
);
5664 if ( textOrientation
== wxHORIZONTAL
)
5665 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5667 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5671 switch ( vertAlign
)
5673 case wxALIGN_BOTTOM
:
5674 if ( textOrientation
== wxHORIZONTAL
)
5675 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5677 x
= rect
.x
+ rect
.width
- textWidth
;
5680 case wxALIGN_CENTRE
:
5681 if ( textOrientation
== wxHORIZONTAL
)
5682 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5684 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5689 if ( textOrientation
== wxHORIZONTAL
)
5696 // Align each line of a multi-line label
5697 size_t nLines
= lines
.GetCount();
5698 for ( size_t l
= 0; l
< nLines
; l
++ )
5700 const wxString
& line
= lines
[l
];
5704 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5708 wxCoord lineWidth
= 0,
5710 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5712 switch ( horizAlign
)
5715 if ( textOrientation
== wxHORIZONTAL
)
5716 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5718 y
= rect
.y
+ lineWidth
+ 1;
5721 case wxALIGN_CENTRE
:
5722 if ( textOrientation
== wxHORIZONTAL
)
5723 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5725 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5730 if ( textOrientation
== wxHORIZONTAL
)
5733 y
= rect
.y
+ rect
.height
- 1;
5737 if ( textOrientation
== wxHORIZONTAL
)
5739 dc
.DrawText( line
, x
, y
);
5744 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5750 // Split multi-line text up into an array of strings.
5751 // Any existing contents of the string array are preserved.
5753 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5754 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5758 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5759 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5761 while ( startPos
< (int)tVal
.length() )
5763 pos
= tVal
.Mid(startPos
).Find( eol
);
5768 else if ( pos
== 0 )
5770 lines
.Add( wxEmptyString
);
5774 lines
.Add( tVal
.Mid(startPos
, pos
) );
5777 startPos
+= pos
+ 1;
5780 if ( startPos
< (int)tVal
.length() )
5782 lines
.Add( tVal
.Mid( startPos
) );
5786 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5787 const wxArrayString
& lines
,
5788 long *width
, long *height
) const
5792 wxCoord lineW
= 0, lineH
= 0;
5795 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5797 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5798 w
= wxMax( w
, lineW
);
5807 // ------ Batch processing.
5809 void wxGrid::EndBatch()
5811 if ( m_batchCount
> 0 )
5814 if ( !m_batchCount
)
5817 m_rowLabelWin
->Refresh();
5818 m_colWindow
->Refresh();
5819 m_cornerLabelWin
->Refresh();
5820 m_gridWin
->Refresh();
5825 // Use this, rather than wxWindow::Refresh(), to force an immediate
5826 // repainting of the grid. Has no effect if you are already inside a
5827 // BeginBatch / EndBatch block.
5829 void wxGrid::ForceRefresh()
5835 bool wxGrid::Enable(bool enable
)
5837 if ( !wxScrolledWindow::Enable(enable
) )
5840 // redraw in the new state
5841 m_gridWin
->Refresh();
5847 // ------ Edit control functions
5850 void wxGrid::EnableEditing( bool edit
)
5852 if ( edit
!= m_editable
)
5855 EnableCellEditControl(edit
);
5860 void wxGrid::EnableCellEditControl( bool enable
)
5865 if ( enable
!= m_cellEditCtrlEnabled
)
5869 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5872 // this should be checked by the caller!
5873 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5875 // do it before ShowCellEditControl()
5876 m_cellEditCtrlEnabled
= enable
;
5878 ShowCellEditControl();
5882 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5884 HideCellEditControl();
5885 SaveEditControlValue();
5887 // do it after HideCellEditControl()
5888 m_cellEditCtrlEnabled
= enable
;
5893 bool wxGrid::IsCurrentCellReadOnly() const
5896 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5897 bool readonly
= attr
->IsReadOnly();
5903 bool wxGrid::CanEnableCellControl() const
5905 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5906 !IsCurrentCellReadOnly();
5909 bool wxGrid::IsCellEditControlEnabled() const
5911 // the cell edit control might be disable for all cells or just for the
5912 // current one if it's read only
5913 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5916 bool wxGrid::IsCellEditControlShown() const
5918 bool isShown
= false;
5920 if ( m_cellEditCtrlEnabled
)
5922 int row
= m_currentCellCoords
.GetRow();
5923 int col
= m_currentCellCoords
.GetCol();
5924 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5925 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5930 if ( editor
->IsCreated() )
5932 isShown
= editor
->GetControl()->IsShown();
5942 void wxGrid::ShowCellEditControl()
5944 if ( IsCellEditControlEnabled() )
5946 if ( !IsVisible( m_currentCellCoords
, false ) )
5948 m_cellEditCtrlEnabled
= false;
5953 wxRect rect
= CellToRect( m_currentCellCoords
);
5954 int row
= m_currentCellCoords
.GetRow();
5955 int col
= m_currentCellCoords
.GetCol();
5957 // if this is part of a multicell, find owner (topleft)
5958 int cell_rows
, cell_cols
;
5959 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5960 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5964 m_currentCellCoords
.SetRow( row
);
5965 m_currentCellCoords
.SetCol( col
);
5968 // erase the highlight and the cell contents because the editor
5969 // might not cover the entire cell
5970 wxClientDC
dc( m_gridWin
);
5972 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5973 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5974 dc
.SetPen(*wxTRANSPARENT_PEN
);
5975 dc
.DrawRectangle(rect
);
5977 // convert to scrolled coords
5978 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5984 // cell is shifted by one pixel
5985 // However, don't allow x or y to become negative
5986 // since the SetSize() method interprets that as
5993 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5994 if ( !editor
->IsCreated() )
5996 editor
->Create(m_gridWin
, wxID_ANY
,
5997 new wxGridCellEditorEvtHandler(this, editor
));
5999 wxGridEditorCreatedEvent
evt(GetId(),
6000 wxEVT_GRID_EDITOR_CREATED
,
6004 editor
->GetControl());
6005 GetEventHandler()->ProcessEvent(evt
);
6008 // resize editor to overflow into righthand cells if allowed
6009 int maxWidth
= rect
.width
;
6010 wxString value
= GetCellValue(row
, col
);
6011 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
6014 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
6015 if (maxWidth
< rect
.width
)
6016 maxWidth
= rect
.width
;
6019 int client_right
= m_gridWin
->GetClientSize().GetWidth();
6020 if (rect
.x
+ maxWidth
> client_right
)
6021 maxWidth
= client_right
- rect
.x
;
6023 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
6025 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6026 // may have changed earlier
6027 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
6030 GetCellSize( row
, i
, &c_rows
, &c_cols
);
6032 // looks weird going over a multicell
6033 if (m_table
->IsEmptyCell( row
, i
) &&
6034 (rect
.width
< maxWidth
) && (c_rows
== 1))
6036 rect
.width
+= GetColWidth( i
);
6042 if (rect
.GetRight() > client_right
)
6043 rect
.SetRight( client_right
- 1 );
6046 editor
->SetCellAttr( attr
);
6047 editor
->SetSize( rect
);
6049 editor
->GetControl()->Move(
6050 editor
->GetControl()->GetPosition().x
+ nXMove
,
6051 editor
->GetControl()->GetPosition().y
);
6052 editor
->Show( true, attr
);
6054 // recalc dimensions in case we need to
6055 // expand the scrolled window to account for editor
6058 editor
->BeginEdit(row
, col
, this);
6059 editor
->SetCellAttr(NULL
);
6067 void wxGrid::HideCellEditControl()
6069 if ( IsCellEditControlEnabled() )
6071 int row
= m_currentCellCoords
.GetRow();
6072 int col
= m_currentCellCoords
.GetCol();
6074 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6075 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6076 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6077 editor
->Show( false );
6081 // return the focus to the grid itself if the editor had it
6083 // note that we must not do this unconditionally to avoid stealing
6084 // focus from the window which just received it if we are hiding the
6085 // editor precisely because we lost focus
6086 if ( editorHadFocus
)
6087 m_gridWin
->SetFocus();
6089 // refresh whole row to the right
6090 wxRect
rect( CellToRect(row
, col
) );
6091 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6092 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6095 // ensure that the pixels under the focus ring get refreshed as well
6096 rect
.Inflate(10, 10);
6099 m_gridWin
->Refresh( false, &rect
);
6103 void wxGrid::SaveEditControlValue()
6105 if ( IsCellEditControlEnabled() )
6107 int row
= m_currentCellCoords
.GetRow();
6108 int col
= m_currentCellCoords
.GetCol();
6110 wxString oldval
= GetCellValue(row
, col
);
6112 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6113 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6116 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6118 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6120 editor
->ApplyEdit(row
, col
, this);
6122 // for compatibility reasons dating back to wx 2.8 when this event
6123 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6124 // didn't exist we allow vetoing this one too
6125 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6127 // Event has been vetoed, set the data back.
6128 SetCellValue(row
, col
, oldval
);
6138 // ------ Grid location functions
6139 // Note that all of these functions work with the logical coordinates of
6140 // grid cells and labels so you will need to convert from device
6141 // coordinates for mouse events etc.
6144 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6146 int row
= YToRow(y
);
6147 int col
= XToCol(x
);
6149 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6150 : wxGridCellCoords(row
, col
);
6153 // compute row or column from some (unscrolled) coordinate value, using either
6154 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6155 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6157 int wxGrid::PosToLinePos(int coord
,
6159 const wxGridOperations
& oper
) const
6161 const int numLines
= oper
.GetNumberOfLines(this);
6164 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6166 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6167 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6169 int maxPos
= coord
/ defaultLineSize
,
6172 // check for the simplest case: if we have no explicit line sizes
6173 // configured, then we already know the line this position falls in
6174 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6175 if ( lineEnds
.empty() )
6177 if ( maxPos
< numLines
)
6180 return clipToMinMax
? numLines
- 1 : -1;
6184 // adjust maxPos before starting the binary search
6185 if ( maxPos
>= numLines
)
6187 maxPos
= numLines
- 1;
6191 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6194 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6196 maxPos
= coord
/ minDist
;
6198 maxPos
= numLines
- 1;
6201 if ( maxPos
>= numLines
)
6202 maxPos
= numLines
- 1;
6205 // check if the position is beyond the last column
6206 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6207 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6208 return clipToMinMax
? maxPos
: -1;
6210 // or before the first one
6211 const int lineAt0
= oper
.GetLineAt(this, 0);
6212 if ( coord
< lineEnds
[lineAt0
] )
6216 // finally do perform the binary search
6217 while ( minPos
< maxPos
)
6219 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6220 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6222 "wxGrid: internal error in PosToLinePos()" );
6224 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6229 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6230 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6240 wxGrid::PosToLine(int coord
,
6242 const wxGridOperations
& oper
) const
6244 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6246 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6249 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6251 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6254 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6256 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6259 int wxGrid::XToPos(int x
) const
6261 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6264 // return the row number such that the y coord is near the edge of, or -1 if
6265 // not near an edge.
6267 // notice that position can only possibly be near an edge if the row/column is
6268 // large enough to still allow for an "inner" area that is _not_ near the edge
6269 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6270 // _never_ be considered to be near the edge).
6271 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6273 const int line
= oper
.PosToLine(this, pos
, true);
6275 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6277 // We know that we are in this line, test whether we are close enough
6278 // to start or end border, respectively.
6279 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6281 else if ( line
> 0 &&
6282 pos
- oper
.GetLineStartPos(this,
6283 line
) < WXGRID_LABEL_EDGE_ZONE
)
6290 int wxGrid::YToEdgeOfRow(int y
) const
6292 return PosToEdgeOfLine(y
, wxGridRowOperations());
6295 int wxGrid::XToEdgeOfCol(int x
) const
6297 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6300 wxRect
wxGrid::CellToRect( int row
, int col
) const
6302 wxRect
rect( -1, -1, -1, -1 );
6304 if ( row
>= 0 && row
< m_numRows
&&
6305 col
>= 0 && col
< m_numCols
)
6307 int i
, cell_rows
, cell_cols
;
6308 rect
.width
= rect
.height
= 0;
6309 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6310 // if negative then find multicell owner
6315 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6317 rect
.x
= GetColLeft(col
);
6318 rect
.y
= GetRowTop(row
);
6319 for (i
=col
; i
< col
+ cell_cols
; i
++)
6320 rect
.width
+= GetColWidth(i
);
6321 for (i
=row
; i
< row
+ cell_rows
; i
++)
6322 rect
.height
+= GetRowHeight(i
);
6324 // if grid lines are enabled, then the area of the cell is a bit smaller
6325 if (m_gridLinesEnabled
)
6335 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6337 // get the cell rectangle in logical coords
6339 wxRect
r( CellToRect( row
, col
) );
6341 // convert to device coords
6343 int left
, top
, right
, bottom
;
6344 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6345 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6347 // check against the client area of the grid window
6349 m_gridWin
->GetClientSize( &cw
, &ch
);
6351 if ( wholeCellVisible
)
6353 // is the cell wholly visible ?
6354 return ( left
>= 0 && right
<= cw
&&
6355 top
>= 0 && bottom
<= ch
);
6359 // is the cell partly visible ?
6361 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6362 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6366 // make the specified cell location visible by doing a minimal amount
6369 void wxGrid::MakeCellVisible( int row
, int col
)
6372 int xpos
= -1, ypos
= -1;
6374 if ( row
>= 0 && row
< m_numRows
&&
6375 col
>= 0 && col
< m_numCols
)
6377 // get the cell rectangle in logical coords
6378 wxRect
r( CellToRect( row
, col
) );
6380 // convert to device coords
6381 int left
, top
, right
, bottom
;
6382 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6383 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6386 m_gridWin
->GetClientSize( &cw
, &ch
);
6392 else if ( bottom
> ch
)
6394 int h
= r
.GetHeight();
6396 for ( i
= row
- 1; i
>= 0; i
-- )
6398 int rowHeight
= GetRowHeight(i
);
6399 if ( h
+ rowHeight
> ch
)
6406 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6407 // have rounding errors (this is important, because if we do,
6408 // we might not scroll at all and some cells won't be redrawn)
6410 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6411 // so just add a full scroll unit...
6412 ypos
+= m_yScrollPixelsPerLine
;
6415 // special handling for wide cells - show always left part of the cell!
6416 // Otherwise, e.g. when stepping from row to row, it would jump between
6417 // left and right part of the cell on every step!
6419 if ( left
< 0 || (right
- left
) >= cw
)
6423 else if ( right
> cw
)
6425 // position the view so that the cell is on the right
6427 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6428 xpos
= x0
+ (right
- cw
);
6430 // see comment for ypos above
6431 xpos
+= m_xScrollPixelsPerLine
;
6434 if ( xpos
!= -1 || ypos
!= -1 )
6437 xpos
/= m_xScrollPixelsPerLine
;
6439 ypos
/= m_yScrollPixelsPerLine
;
6440 Scroll( xpos
, ypos
);
6447 // ------ Grid cursor movement functions
6451 wxGrid::DoMoveCursor(bool expandSelection
,
6452 const wxGridDirectionOperations
& diroper
)
6454 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6457 if ( expandSelection
)
6459 wxGridCellCoords coords
= m_selectedBlockCorner
;
6460 if ( coords
== wxGridNoCellCoords
)
6461 coords
= m_currentCellCoords
;
6463 if ( diroper
.IsAtBoundary(coords
) )
6466 diroper
.Advance(coords
);
6468 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6470 else // don't expand selection
6474 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6477 wxGridCellCoords coords
= m_currentCellCoords
;
6478 diroper
.Advance(coords
);
6486 bool wxGrid::MoveCursorUp(bool expandSelection
)
6488 return DoMoveCursor(expandSelection
,
6489 wxGridBackwardOperations(this, wxGridRowOperations()));
6492 bool wxGrid::MoveCursorDown(bool expandSelection
)
6494 return DoMoveCursor(expandSelection
,
6495 wxGridForwardOperations(this, wxGridRowOperations()));
6498 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6500 return DoMoveCursor(expandSelection
,
6501 wxGridBackwardOperations(this, wxGridColumnOperations()));
6504 bool wxGrid::MoveCursorRight(bool expandSelection
)
6506 return DoMoveCursor(expandSelection
,
6507 wxGridForwardOperations(this, wxGridColumnOperations()));
6510 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6512 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6515 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6518 const int oldRow
= m_currentCellCoords
.GetRow();
6519 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6520 if ( newRow
== oldRow
)
6522 wxGridCellCoords
coords(m_currentCellCoords
);
6523 diroper
.Advance(coords
);
6524 newRow
= coords
.GetRow();
6527 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6532 bool wxGrid::MovePageUp()
6534 return DoMoveCursorByPage(
6535 wxGridBackwardOperations(this, wxGridRowOperations()));
6538 bool wxGrid::MovePageDown()
6540 return DoMoveCursorByPage(
6541 wxGridForwardOperations(this, wxGridRowOperations()));
6544 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6545 // until we find a non-empty cell or reach the grid end
6547 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6548 const wxGridDirectionOperations
& diroper
)
6550 while ( !diroper
.IsAtBoundary(coords
) )
6552 diroper
.Advance(coords
);
6553 if ( !m_table
->IsEmpty(coords
) )
6559 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6560 const wxGridDirectionOperations
& diroper
)
6562 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6565 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6568 wxGridCellCoords
coords(m_currentCellCoords
);
6569 if ( m_table
->IsEmpty(coords
) )
6571 // we are in an empty cell: find the next block of non-empty cells
6572 AdvanceToNextNonEmpty(coords
, diroper
);
6574 else // current cell is not empty
6576 diroper
.Advance(coords
);
6577 if ( m_table
->IsEmpty(coords
) )
6579 // we started at the end of a block, find the next one
6580 AdvanceToNextNonEmpty(coords
, diroper
);
6582 else // we're in a middle of a block
6584 // go to the end of it, i.e. find the last cell before the next
6586 while ( !diroper
.IsAtBoundary(coords
) )
6588 wxGridCellCoords
coordsNext(coords
);
6589 diroper
.Advance(coordsNext
);
6590 if ( m_table
->IsEmpty(coordsNext
) )
6593 coords
= coordsNext
;
6598 if ( expandSelection
)
6600 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6611 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6613 return DoMoveCursorByBlock(
6615 wxGridBackwardOperations(this, wxGridRowOperations())
6619 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6621 return DoMoveCursorByBlock(
6623 wxGridForwardOperations(this, wxGridRowOperations())
6627 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6629 return DoMoveCursorByBlock(
6631 wxGridBackwardOperations(this, wxGridColumnOperations())
6635 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6637 return DoMoveCursorByBlock(
6639 wxGridForwardOperations(this, wxGridColumnOperations())
6644 // ------ Label values and formatting
6647 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6650 *horiz
= m_rowLabelHorizAlign
;
6652 *vert
= m_rowLabelVertAlign
;
6655 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6658 *horiz
= m_colLabelHorizAlign
;
6660 *vert
= m_colLabelVertAlign
;
6663 int wxGrid::GetColLabelTextOrientation() const
6665 return m_colLabelTextOrientation
;
6668 wxString
wxGrid::GetRowLabelValue( int row
) const
6672 return m_table
->GetRowLabelValue( row
);
6682 wxString
wxGrid::GetColLabelValue( int col
) const
6686 return m_table
->GetColLabelValue( col
);
6696 void wxGrid::SetRowLabelSize( int width
)
6698 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6700 if ( width
== wxGRID_AUTOSIZE
)
6702 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6705 if ( width
!= m_rowLabelWidth
)
6709 m_rowLabelWin
->Show( false );
6710 m_cornerLabelWin
->Show( false );
6712 else if ( m_rowLabelWidth
== 0 )
6714 m_rowLabelWin
->Show( true );
6715 if ( m_colLabelHeight
> 0 )
6716 m_cornerLabelWin
->Show( true );
6719 m_rowLabelWidth
= width
;
6721 wxScrolledWindow::Refresh( true );
6725 void wxGrid::SetColLabelSize( int height
)
6727 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6729 if ( height
== wxGRID_AUTOSIZE
)
6731 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6734 if ( height
!= m_colLabelHeight
)
6738 m_colWindow
->Show( false );
6739 m_cornerLabelWin
->Show( false );
6741 else if ( m_colLabelHeight
== 0 )
6743 m_colWindow
->Show( true );
6744 if ( m_rowLabelWidth
> 0 )
6745 m_cornerLabelWin
->Show( true );
6748 m_colLabelHeight
= height
;
6750 wxScrolledWindow::Refresh( true );
6754 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6756 if ( m_labelBackgroundColour
!= colour
)
6758 m_labelBackgroundColour
= colour
;
6759 m_rowLabelWin
->SetBackgroundColour( colour
);
6760 m_colWindow
->SetBackgroundColour( colour
);
6761 m_cornerLabelWin
->SetBackgroundColour( colour
);
6763 if ( !GetBatchCount() )
6765 m_rowLabelWin
->Refresh();
6766 m_colWindow
->Refresh();
6767 m_cornerLabelWin
->Refresh();
6772 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6774 if ( m_labelTextColour
!= colour
)
6776 m_labelTextColour
= colour
;
6777 if ( !GetBatchCount() )
6779 m_rowLabelWin
->Refresh();
6780 m_colWindow
->Refresh();
6785 void wxGrid::SetLabelFont( const wxFont
& font
)
6788 if ( !GetBatchCount() )
6790 m_rowLabelWin
->Refresh();
6791 m_colWindow
->Refresh();
6795 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6797 // allow old (incorrect) defs to be used
6800 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6801 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6802 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6807 case wxTOP
: vert
= wxALIGN_TOP
; break;
6808 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6809 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6812 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6814 m_rowLabelHorizAlign
= horiz
;
6817 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6819 m_rowLabelVertAlign
= vert
;
6822 if ( !GetBatchCount() )
6824 m_rowLabelWin
->Refresh();
6828 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6830 // allow old (incorrect) defs to be used
6833 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6834 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6835 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6840 case wxTOP
: vert
= wxALIGN_TOP
; break;
6841 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6842 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6845 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6847 m_colLabelHorizAlign
= horiz
;
6850 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6852 m_colLabelVertAlign
= vert
;
6855 if ( !GetBatchCount() )
6857 m_colWindow
->Refresh();
6861 // Note: under MSW, the default column label font must be changed because it
6862 // does not support vertical printing
6864 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6865 // pGrid->SetLabelFont(font);
6866 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6868 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6870 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6871 m_colLabelTextOrientation
= textOrientation
;
6873 if ( !GetBatchCount() )
6874 m_colWindow
->Refresh();
6877 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6881 m_table
->SetRowLabelValue( row
, s
);
6882 if ( !GetBatchCount() )
6884 wxRect rect
= CellToRect( row
, 0 );
6885 if ( rect
.height
> 0 )
6887 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6889 rect
.width
= m_rowLabelWidth
;
6890 m_rowLabelWin
->Refresh( true, &rect
);
6896 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6900 m_table
->SetColLabelValue( col
, s
);
6901 if ( !GetBatchCount() )
6903 if ( m_useNativeHeader
)
6905 GetGridColHeader()->UpdateColumn(col
);
6909 wxRect rect
= CellToRect( 0, col
);
6910 if ( rect
.width
> 0 )
6912 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6914 rect
.height
= m_colLabelHeight
;
6915 GetColLabelWindow()->Refresh( true, &rect
);
6922 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6924 if ( m_gridLineColour
!= colour
)
6926 m_gridLineColour
= colour
;
6928 if ( GridLinesEnabled() )
6933 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6935 if ( m_cellHighlightColour
!= colour
)
6937 m_cellHighlightColour
= colour
;
6939 wxClientDC
dc( m_gridWin
);
6941 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6942 DrawCellHighlight(dc
, attr
);
6947 void wxGrid::SetCellHighlightPenWidth(int width
)
6949 if (m_cellHighlightPenWidth
!= width
)
6951 m_cellHighlightPenWidth
= width
;
6953 // Just redrawing the cell highlight is not enough since that won't
6954 // make any visible change if the the thickness is getting smaller.
6955 int row
= m_currentCellCoords
.GetRow();
6956 int col
= m_currentCellCoords
.GetCol();
6957 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6960 wxRect rect
= CellToRect(row
, col
);
6961 m_gridWin
->Refresh(true, &rect
);
6965 void wxGrid::SetCellHighlightROPenWidth(int width
)
6967 if (m_cellHighlightROPenWidth
!= width
)
6969 m_cellHighlightROPenWidth
= width
;
6971 // Just redrawing the cell highlight is not enough since that won't
6972 // make any visible change if the the thickness is getting smaller.
6973 int row
= m_currentCellCoords
.GetRow();
6974 int col
= m_currentCellCoords
.GetCol();
6975 if ( row
== -1 || col
== -1 ||
6976 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6979 wxRect rect
= CellToRect(row
, col
);
6980 m_gridWin
->Refresh(true, &rect
);
6984 void wxGrid::RedrawGridLines()
6986 // the lines will be redrawn when the window is thawn
6987 if ( GetBatchCount() )
6990 if ( GridLinesEnabled() )
6992 wxClientDC
dc( m_gridWin
);
6994 DrawAllGridLines( dc
, wxRegion() );
6996 else // remove the grid lines
6998 m_gridWin
->Refresh();
7002 void wxGrid::EnableGridLines( bool enable
)
7004 if ( enable
!= m_gridLinesEnabled
)
7006 m_gridLinesEnabled
= enable
;
7012 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
7018 if ( GridLinesEnabled() )
7023 int wxGrid::GetDefaultRowSize() const
7025 return m_defaultRowHeight
;
7028 int wxGrid::GetRowSize( int row
) const
7030 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
7032 return GetRowHeight(row
);
7035 int wxGrid::GetDefaultColSize() const
7037 return m_defaultColWidth
;
7040 int wxGrid::GetColSize( int col
) const
7042 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
7044 return GetColWidth(col
);
7047 // ============================================================================
7048 // access to the grid attributes: each of them has a default value in the grid
7049 // itself and may be overidden on a per-cell basis
7050 // ============================================================================
7052 // ----------------------------------------------------------------------------
7053 // setting default attributes
7054 // ----------------------------------------------------------------------------
7056 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7058 m_defaultCellAttr
->SetBackgroundColour(col
);
7060 m_gridWin
->SetBackgroundColour(col
);
7064 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7066 m_defaultCellAttr
->SetTextColour(col
);
7069 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7071 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7074 void wxGrid::SetDefaultCellOverflow( bool allow
)
7076 m_defaultCellAttr
->SetOverflow(allow
);
7079 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7081 m_defaultCellAttr
->SetFont(font
);
7084 // For editors and renderers the type registry takes precedence over the
7085 // default attr, so we need to register the new editor/renderer for the string
7086 // data type in order to make setting a default editor/renderer appear to
7089 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7091 RegisterDataType(wxGRID_VALUE_STRING
,
7093 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7096 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7098 RegisterDataType(wxGRID_VALUE_STRING
,
7099 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7103 // ----------------------------------------------------------------------------
7104 // access to the default attributes
7105 // ----------------------------------------------------------------------------
7107 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7109 return m_defaultCellAttr
->GetBackgroundColour();
7112 wxColour
wxGrid::GetDefaultCellTextColour() const
7114 return m_defaultCellAttr
->GetTextColour();
7117 wxFont
wxGrid::GetDefaultCellFont() const
7119 return m_defaultCellAttr
->GetFont();
7122 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7124 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7127 bool wxGrid::GetDefaultCellOverflow() const
7129 return m_defaultCellAttr
->GetOverflow();
7132 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7134 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7137 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7139 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7142 // ----------------------------------------------------------------------------
7143 // access to cell attributes
7144 // ----------------------------------------------------------------------------
7146 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7148 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7149 wxColour colour
= attr
->GetBackgroundColour();
7155 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7157 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7158 wxColour colour
= attr
->GetTextColour();
7164 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7166 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7167 wxFont font
= attr
->GetFont();
7173 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7175 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7176 attr
->GetAlignment(horiz
, vert
);
7180 bool wxGrid::GetCellOverflow( int row
, int col
) const
7182 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7183 bool allow
= attr
->GetOverflow();
7190 wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7192 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7193 attr
->GetSize( num_rows
, num_cols
);
7196 if ( *num_rows
== 1 && *num_cols
== 1 )
7197 return CellSpan_None
; // just a normal cell
7199 if ( *num_rows
< 0 || *num_cols
< 0 )
7200 return CellSpan_Inside
; // covered by a multi-span cell
7202 // this cell spans multiple cells to its right/bottom
7203 return CellSpan_Main
;
7206 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7208 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7209 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7215 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7217 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7218 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7224 bool wxGrid::IsReadOnly(int row
, int col
) const
7226 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7227 bool isReadOnly
= attr
->IsReadOnly();
7233 // ----------------------------------------------------------------------------
7234 // attribute support: cache, automatic provider creation, ...
7235 // ----------------------------------------------------------------------------
7237 bool wxGrid::CanHaveAttributes() const
7244 return m_table
->CanHaveAttributes();
7247 void wxGrid::ClearAttrCache()
7249 if ( m_attrCache
.row
!= -1 )
7251 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7252 m_attrCache
.attr
= NULL
;
7253 m_attrCache
.row
= -1;
7254 // wxSafeDecRec(...) might cause event processing that accesses
7255 // the cached attribute, if one exists (e.g. by deleting the
7256 // editor stored within the attribute). Therefore it is important
7257 // to invalidate the cache before calling wxSafeDecRef!
7258 wxSafeDecRef(oldAttr
);
7262 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7266 wxGrid
*self
= (wxGrid
*)this; // const_cast
7268 self
->ClearAttrCache();
7269 self
->m_attrCache
.row
= row
;
7270 self
->m_attrCache
.col
= col
;
7271 self
->m_attrCache
.attr
= attr
;
7276 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7278 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7280 *attr
= m_attrCache
.attr
;
7281 wxSafeIncRef(m_attrCache
.attr
);
7283 #ifdef DEBUG_ATTR_CACHE
7284 gs_nAttrCacheHits
++;
7291 #ifdef DEBUG_ATTR_CACHE
7292 gs_nAttrCacheMisses
++;
7299 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7301 wxGridCellAttr
*attr
= NULL
;
7302 // Additional test to avoid looking at the cache e.g. for
7303 // wxNoCellCoords, as this will confuse memory management.
7306 if ( !LookupAttr(row
, col
, &attr
) )
7308 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7310 CacheAttr(row
, col
, attr
);
7316 attr
->SetDefAttr(m_defaultCellAttr
);
7320 attr
= m_defaultCellAttr
;
7327 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7329 wxGridCellAttr
*attr
= NULL
;
7330 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7332 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7333 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7335 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7338 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7340 // artificially inc the ref count to match DecRef() in caller
7342 m_table
->SetAttr(attr
, row
, col
);
7348 // ----------------------------------------------------------------------------
7349 // setting column attributes (wrappers around SetColAttr)
7350 // ----------------------------------------------------------------------------
7352 void wxGrid::SetColFormatBool(int col
)
7354 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7357 void wxGrid::SetColFormatNumber(int col
)
7359 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7362 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7364 wxString typeName
= wxGRID_VALUE_FLOAT
;
7365 if ( (width
!= -1) || (precision
!= -1) )
7367 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7370 SetColFormatCustom(col
, typeName
);
7373 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7375 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7377 attr
= new wxGridCellAttr
;
7378 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7379 attr
->SetRenderer(renderer
);
7380 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7381 attr
->SetEditor(editor
);
7383 SetColAttr(col
, attr
);
7387 // ----------------------------------------------------------------------------
7388 // setting cell attributes: this is forwarded to the table
7389 // ----------------------------------------------------------------------------
7391 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7393 if ( CanHaveAttributes() )
7395 m_table
->SetAttr(attr
, row
, col
);
7404 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7406 if ( CanHaveAttributes() )
7408 m_table
->SetRowAttr(attr
, row
);
7417 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7419 if ( CanHaveAttributes() )
7421 m_table
->SetColAttr(attr
, col
);
7430 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7432 if ( CanHaveAttributes() )
7434 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7435 attr
->SetBackgroundColour(colour
);
7440 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7442 if ( CanHaveAttributes() )
7444 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7445 attr
->SetTextColour(colour
);
7450 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7452 if ( CanHaveAttributes() )
7454 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7455 attr
->SetFont(font
);
7460 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7462 if ( CanHaveAttributes() )
7464 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7465 attr
->SetAlignment(horiz
, vert
);
7470 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7472 if ( CanHaveAttributes() )
7474 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7475 attr
->SetOverflow(allow
);
7480 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7482 if ( CanHaveAttributes() )
7484 int cell_rows
, cell_cols
;
7486 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7487 attr
->GetSize(&cell_rows
, &cell_cols
);
7488 attr
->SetSize(num_rows
, num_cols
);
7491 // Cannot set the size of a cell to 0 or negative values
7492 // While it is perfectly legal to do that, this function cannot
7493 // handle all the possibilies, do it by hand by getting the CellAttr.
7494 // You can only set the size of a cell to 1,1 or greater with this fn
7495 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7496 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7497 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7498 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7500 // if this was already a multicell then "turn off" the other cells first
7501 if ((cell_rows
> 1) || (cell_cols
> 1))
7504 for (j
=row
; j
< row
+ cell_rows
; j
++)
7506 for (i
=col
; i
< col
+ cell_cols
; i
++)
7508 if ((i
!= col
) || (j
!= row
))
7510 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7511 attr_stub
->SetSize( 1, 1 );
7512 attr_stub
->DecRef();
7518 // mark the cells that will be covered by this cell to
7519 // negative or zero values to point back at this cell
7520 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7523 for (j
=row
; j
< row
+ num_rows
; j
++)
7525 for (i
=col
; i
< col
+ num_cols
; i
++)
7527 if ((i
!= col
) || (j
!= row
))
7529 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7530 attr_stub
->SetSize( row
- j
, col
- i
);
7531 attr_stub
->DecRef();
7539 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7541 if ( CanHaveAttributes() )
7543 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7544 attr
->SetRenderer(renderer
);
7549 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7551 if ( CanHaveAttributes() )
7553 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7554 attr
->SetEditor(editor
);
7559 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7561 if ( CanHaveAttributes() )
7563 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7564 attr
->SetReadOnly(isReadOnly
);
7569 // ----------------------------------------------------------------------------
7570 // Data type registration
7571 // ----------------------------------------------------------------------------
7573 void wxGrid::RegisterDataType(const wxString
& typeName
,
7574 wxGridCellRenderer
* renderer
,
7575 wxGridCellEditor
* editor
)
7577 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7581 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7583 wxString typeName
= m_table
->GetTypeName(row
, col
);
7584 return GetDefaultEditorForType(typeName
);
7587 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7589 wxString typeName
= m_table
->GetTypeName(row
, col
);
7590 return GetDefaultRendererForType(typeName
);
7593 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7595 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7596 if ( index
== wxNOT_FOUND
)
7598 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7603 return m_typeRegistry
->GetEditor(index
);
7606 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7608 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7609 if ( index
== wxNOT_FOUND
)
7611 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7616 return m_typeRegistry
->GetRenderer(index
);
7619 // ----------------------------------------------------------------------------
7621 // ----------------------------------------------------------------------------
7623 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7627 setFixed
= new wxGridFixedIndicesSet
;
7630 setFixed
->insert(line
);
7634 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7636 return !setFixed
|| !setFixed
->count(line
);
7639 void wxGrid::EnableDragRowSize( bool enable
)
7641 m_canDragRowSize
= enable
;
7644 void wxGrid::EnableDragColSize( bool enable
)
7646 m_canDragColSize
= enable
;
7649 void wxGrid::EnableDragGridSize( bool enable
)
7651 m_canDragGridSize
= enable
;
7654 void wxGrid::EnableDragCell( bool enable
)
7656 m_canDragCell
= enable
;
7659 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7661 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7663 if ( resizeExistingRows
)
7665 // since we are resizing all rows to the default row size,
7666 // we can simply clear the row heights and row bottoms
7667 // arrays (which also allows us to take advantage of
7668 // some speed optimisations)
7669 m_rowHeights
.Empty();
7670 m_rowBottoms
.Empty();
7671 if ( !GetBatchCount() )
7676 void wxGrid::SetRowSize( int row
, int height
)
7678 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7680 // if < 0 then calculate new height from label
7684 wxArrayString lines
;
7685 wxClientDC
dc(m_rowLabelWin
);
7686 dc
.SetFont(GetLabelFont());
7687 StringToLines(GetRowLabelValue( row
), lines
);
7688 GetTextBoxSize( dc
, lines
, &w
, &h
);
7689 //check that it is not less than the minimal height
7690 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7693 // See comment in SetColSize
7694 if ( height
< GetRowMinimalAcceptableHeight())
7697 if ( m_rowHeights
.IsEmpty() )
7699 // need to really create the array
7703 int h
= wxMax( 0, height
);
7704 int diff
= h
- m_rowHeights
[row
];
7706 m_rowHeights
[row
] = h
;
7707 for ( int i
= row
; i
< m_numRows
; i
++ )
7709 m_rowBottoms
[i
] += diff
;
7712 if ( !GetBatchCount() )
7716 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7718 // we dont allow zero default column width
7719 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7721 if ( resizeExistingCols
)
7723 // since we are resizing all columns to the default column size,
7724 // we can simply clear the col widths and col rights
7725 // arrays (which also allows us to take advantage of
7726 // some speed optimisations)
7727 m_colWidths
.Empty();
7728 m_colRights
.Empty();
7729 if ( !GetBatchCount() )
7734 void wxGrid::SetColSize( int col
, int width
)
7736 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7738 // if < 0 then calculate new width from label
7742 wxArrayString lines
;
7743 wxClientDC
dc(m_colWindow
);
7744 dc
.SetFont(GetLabelFont());
7745 StringToLines(GetColLabelValue(col
), lines
);
7746 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7747 GetTextBoxSize( dc
, lines
, &w
, &h
);
7749 GetTextBoxSize( dc
, lines
, &h
, &w
);
7751 //check that it is not less than the minimal width
7752 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7755 // we intentionally don't test whether the width is less than
7756 // GetColMinimalWidth() here but we do compare it with
7757 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7758 // #651) -- and we also always allow the width of 0 as it has the special
7759 // sense of hiding the column
7760 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7763 if ( m_colWidths
.IsEmpty() )
7765 // need to really create the array
7769 const int diff
= width
- m_colWidths
[col
];
7770 m_colWidths
[col
] = width
;
7771 if ( m_useNativeHeader
)
7772 GetGridColHeader()->UpdateColumn(col
);
7773 //else: will be refreshed when the header is redrawn
7775 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7777 m_colRights
[GetColAt(colPos
)] += diff
;
7780 if ( !GetBatchCount() )
7787 void wxGrid::SetColMinimalWidth( int col
, int width
)
7789 if (width
> GetColMinimalAcceptableWidth())
7791 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7792 m_colMinWidths
[key
] = width
;
7796 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7798 if (width
> GetRowMinimalAcceptableHeight())
7800 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7801 m_rowMinHeights
[key
] = width
;
7805 int wxGrid::GetColMinimalWidth(int col
) const
7807 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7808 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7810 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7813 int wxGrid::GetRowMinimalHeight(int row
) const
7815 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7816 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7818 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7821 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7823 // We do allow a width of 0 since this gives us
7824 // an easy way to temporarily hiding columns.
7826 m_minAcceptableColWidth
= width
;
7829 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7831 // We do allow a height of 0 since this gives us
7832 // an easy way to temporarily hiding rows.
7834 m_minAcceptableRowHeight
= height
;
7837 int wxGrid::GetColMinimalAcceptableWidth() const
7839 return m_minAcceptableColWidth
;
7842 int wxGrid::GetRowMinimalAcceptableHeight() const
7844 return m_minAcceptableRowHeight
;
7847 // ----------------------------------------------------------------------------
7849 // ----------------------------------------------------------------------------
7852 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7854 const bool column
= direction
== wxGRID_COLUMN
;
7856 wxClientDC
dc(m_gridWin
);
7858 // cancel editing of cell
7859 HideCellEditControl();
7860 SaveEditControlValue();
7862 // initialize both of them just to avoid compiler warnings even if only
7863 // really needs to be initialized here
7877 wxCoord extent
, extentMax
= 0;
7878 int max
= column
? m_numRows
: m_numCols
;
7879 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7886 // we need to account for the cells spanning multiple columns/rows:
7887 // while they may need a lot of space, they don't need all of it in
7889 int numRows
, numCols
;
7890 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
7891 if ( span
== CellSpan_Inside
)
7893 // we need to get the size of the main cell, not of a cell hidden
7898 // get the size of the main cell too
7899 GetCellSize(row
, col
, &numRows
, &numCols
);
7902 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7903 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7906 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7907 extent
= column
? size
.x
: size
.y
;
7909 if ( span
!= CellSpan_None
)
7911 // we spread the size of a spanning cell over all the cells it
7912 // covers evenly -- this is probably not ideal but we can't
7913 // really do much better here
7915 // notice that numCols and numRows are never 0 as they
7916 // correspond to the size of the main cell of the span and not
7917 // of the cell inside it
7918 extent
/= column
? numCols
: numRows
;
7921 if ( extent
> extentMax
)
7930 // now also compare with the column label extent
7932 dc
.SetFont( GetLabelFont() );
7936 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7937 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7941 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7943 extent
= column
? w
: h
;
7944 if ( extent
> extentMax
)
7949 // empty column - give default extent (notice that if extentMax is less
7950 // than default extent but != 0, it's OK)
7951 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7956 // leave some space around text
7964 // Ensure automatic width is not less than minimal width. See the
7965 // comment in SetColSize() for explanation of why this isn't done
7968 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7970 SetColSize( col
, extentMax
);
7971 if ( !GetBatchCount() )
7973 if ( m_useNativeHeader
)
7975 GetGridColHeader()->UpdateColumn(col
);
7980 m_gridWin
->GetClientSize( &cw
, &ch
);
7981 wxRect
rect ( CellToRect( 0, col
) );
7983 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7984 rect
.width
= cw
- rect
.x
;
7985 rect
.height
= m_colLabelHeight
;
7986 GetColLabelWindow()->Refresh( true, &rect
);
7992 // Ensure automatic width is not less than minimal height. See the
7993 // comment in SetColSize() for explanation of why this isn't done
7996 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7998 SetRowSize(row
, extentMax
);
7999 if ( !GetBatchCount() )
8002 m_gridWin
->GetClientSize( &cw
, &ch
);
8003 wxRect
rect( CellToRect( row
, 0 ) );
8005 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8006 rect
.width
= m_rowLabelWidth
;
8007 rect
.height
= ch
- rect
.y
;
8008 m_rowLabelWin
->Refresh( true, &rect
);
8015 SetColMinimalWidth(col
, extentMax
);
8017 SetRowMinimalHeight(row
, extentMax
);
8021 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
8023 // calculate size for the rows or columns?
8024 const bool calcRows
= direction
== wxGRID_ROW
;
8026 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
8027 : GetGridColLabelWindow());
8028 dc
.SetFont(GetLabelFont());
8030 // which dimension should we take into account for calculations?
8032 // for columns, the text can be only horizontal so it's easy but for rows
8033 // we also have to take into account the text orientation
8035 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
8037 wxArrayString lines
;
8038 wxCoord extentMax
= 0;
8040 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
8041 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
8045 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
8046 : GetColLabelValue(rowOrCol
);
8047 StringToLines(label
, lines
);
8050 GetTextBoxSize(dc
, lines
, &w
, &h
);
8052 const wxCoord extent
= useWidth
? w
: h
;
8053 if ( extent
> extentMax
)
8059 // empty column - give default extent (notice that if extentMax is less
8060 // than default extent but != 0, it's OK)
8061 extentMax
= calcRows
? GetDefaultRowLabelSize()
8062 : GetDefaultColLabelSize();
8065 // leave some space around text (taken from AutoSizeColOrRow)
8074 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8076 int width
= m_rowLabelWidth
;
8078 wxGridUpdateLocker locker
;
8080 locker
.Create(this);
8082 for ( int col
= 0; col
< m_numCols
; col
++ )
8085 AutoSizeColumn(col
, setAsMin
);
8087 width
+= GetColWidth(col
);
8093 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8095 int height
= m_colLabelHeight
;
8097 wxGridUpdateLocker locker
;
8099 locker
.Create(this);
8101 for ( int row
= 0; row
< m_numRows
; row
++ )
8104 AutoSizeRow(row
, setAsMin
);
8106 height
+= GetRowHeight(row
);
8112 void wxGrid::AutoSize()
8114 wxGridUpdateLocker
locker(this);
8116 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8117 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8119 // we know that we're not going to have scrollbars so disable them now to
8120 // avoid trouble in SetClientSize() which can otherwise set the correct
8121 // client size but also leave space for (not needed any more) scrollbars
8122 SetScrollbars(m_xScrollPixelsPerLine
, m_yScrollPixelsPerLine
,
8125 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8128 void wxGrid::AutoSizeRowLabelSize( int row
)
8130 // Hide the edit control, so it
8131 // won't interfere with drag-shrinking.
8132 if ( IsCellEditControlShown() )
8134 HideCellEditControl();
8135 SaveEditControlValue();
8138 // autosize row height depending on label text
8139 SetRowSize(row
, -1);
8143 void wxGrid::AutoSizeColLabelSize( int col
)
8145 // Hide the edit control, so it
8146 // won't interfere with drag-shrinking.
8147 if ( IsCellEditControlShown() )
8149 HideCellEditControl();
8150 SaveEditControlValue();
8153 // autosize column width depending on label text
8154 SetColSize(col
, -1);
8158 wxSize
wxGrid::DoGetBestSize() const
8160 wxGrid
*self
= (wxGrid
*)this; // const_cast
8162 // we do the same as in AutoSize() here with the exception that we don't
8163 // change the column/row sizes, only calculate them
8164 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8165 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8167 // NOTE: This size should be cached, but first we need to add calls to
8168 // InvalidateBestSize everywhere that could change the results of this
8170 // CacheBestSize(size);
8172 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8173 + GetWindowBorderSize();
8181 wxPen
& wxGrid::GetDividerPen() const
8186 // ----------------------------------------------------------------------------
8187 // cell value accessor functions
8188 // ----------------------------------------------------------------------------
8190 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8194 m_table
->SetValue( row
, col
, s
);
8195 if ( !GetBatchCount() )
8198 wxRect
rect( CellToRect( row
, col
) );
8200 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8201 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8202 m_gridWin
->Refresh( false, &rect
);
8205 if ( m_currentCellCoords
.GetRow() == row
&&
8206 m_currentCellCoords
.GetCol() == col
&&
8207 IsCellEditControlShown())
8208 // Note: If we are using IsCellEditControlEnabled,
8209 // this interacts badly with calling SetCellValue from
8210 // an EVT_GRID_CELL_CHANGE handler.
8212 HideCellEditControl();
8213 ShowCellEditControl(); // will reread data from table
8218 // ----------------------------------------------------------------------------
8219 // block, row and column selection
8220 // ----------------------------------------------------------------------------
8222 void wxGrid::SelectRow( int row
, bool addToSelected
)
8227 if ( !addToSelected
)
8230 m_selection
->SelectRow(row
);
8233 void wxGrid::SelectCol( int col
, bool addToSelected
)
8238 if ( !addToSelected
)
8241 m_selection
->SelectCol(col
);
8244 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8250 if ( !addToSelected
)
8253 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8256 void wxGrid::SelectAll()
8258 if ( m_numRows
> 0 && m_numCols
> 0 )
8261 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8265 // ----------------------------------------------------------------------------
8266 // cell, row and col deselection
8267 // ----------------------------------------------------------------------------
8269 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8274 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8275 if ( mode
== oper
.GetSelectionMode() ||
8276 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8278 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8279 if ( m_selection
->IsInSelection(c
) )
8280 m_selection
->ToggleCellSelection(c
);
8282 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8284 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8285 for ( int i
= 0; i
< nOther
; i
++ )
8287 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8288 if ( m_selection
->IsInSelection(c
) )
8289 m_selection
->ToggleCellSelection(c
);
8292 //else: can only select orthogonal lines so no lines in this direction
8293 // could have been selected anyhow
8296 void wxGrid::DeselectRow(int row
)
8298 DeselectLine(row
, wxGridRowOperations());
8301 void wxGrid::DeselectCol(int col
)
8303 DeselectLine(col
, wxGridColumnOperations());
8306 void wxGrid::DeselectCell( int row
, int col
)
8308 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8309 m_selection
->ToggleCellSelection(row
, col
);
8312 bool wxGrid::IsSelection() const
8314 return ( m_selection
&& (m_selection
->IsSelection() ||
8315 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8316 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8319 bool wxGrid::IsInSelection( int row
, int col
) const
8321 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8322 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8323 col
>= m_selectedBlockTopLeft
.GetCol() &&
8324 row
<= m_selectedBlockBottomRight
.GetRow() &&
8325 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8328 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8332 wxGridCellCoordsArray a
;
8336 return m_selection
->m_cellSelection
;
8339 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8343 wxGridCellCoordsArray a
;
8347 return m_selection
->m_blockSelectionTopLeft
;
8350 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8354 wxGridCellCoordsArray a
;
8358 return m_selection
->m_blockSelectionBottomRight
;
8361 wxArrayInt
wxGrid::GetSelectedRows() const
8369 return m_selection
->m_rowSelection
;
8372 wxArrayInt
wxGrid::GetSelectedCols() const
8380 return m_selection
->m_colSelection
;
8383 void wxGrid::ClearSelection()
8385 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8386 m_selectedBlockBottomRight
);
8387 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8388 m_selectedBlockCorner
);
8390 m_selectedBlockTopLeft
=
8391 m_selectedBlockBottomRight
=
8392 m_selectedBlockCorner
= wxGridNoCellCoords
;
8394 if ( !r1
.IsEmpty() )
8395 RefreshRect(r1
, false);
8396 if ( !r2
.IsEmpty() )
8397 RefreshRect(r2
, false);
8400 m_selection
->ClearSelection();
8403 // This function returns the rectangle that encloses the given block
8404 // in device coords clipped to the client size of the grid window.
8406 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8407 const wxGridCellCoords
& bottomRight
) const
8410 wxRect tempCellRect
= CellToRect(topLeft
);
8411 if ( tempCellRect
!= wxGridNoCellRect
)
8413 resultRect
= tempCellRect
;
8417 resultRect
= wxRect(0, 0, 0, 0);
8420 tempCellRect
= CellToRect(bottomRight
);
8421 if ( tempCellRect
!= wxGridNoCellRect
)
8423 resultRect
+= tempCellRect
;
8427 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8428 return wxGridNoCellRect
;
8431 // Ensure that left/right and top/bottom pairs are in order.
8432 int left
= resultRect
.GetLeft();
8433 int top
= resultRect
.GetTop();
8434 int right
= resultRect
.GetRight();
8435 int bottom
= resultRect
.GetBottom();
8437 int leftCol
= topLeft
.GetCol();
8438 int topRow
= topLeft
.GetRow();
8439 int rightCol
= bottomRight
.GetCol();
8440 int bottomRow
= bottomRight
.GetRow();
8464 // The following loop is ONLY necessary to detect and handle merged cells.
8466 m_gridWin
->GetClientSize( &cw
, &ch
);
8468 // Get the origin coordinates: notice that they will be negative if the
8469 // grid is scrolled downwards/to the right.
8470 int gridOriginX
= 0;
8471 int gridOriginY
= 0;
8472 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8474 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8475 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8477 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8478 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8480 // Bound our loop so that we only examine the portion of the selected block
8481 // that is shown on screen. Therefore, we compare the Top-Left block values
8482 // to the Top-Left screen values, and the Bottom-Right block values to the
8483 // Bottom-Right screen values, choosing appropriately.
8484 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8485 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8486 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8487 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8489 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8491 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8493 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8494 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8496 tempCellRect
= CellToRect( j
, i
);
8498 if (tempCellRect
.x
< left
)
8499 left
= tempCellRect
.x
;
8500 if (tempCellRect
.y
< top
)
8501 top
= tempCellRect
.y
;
8502 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8503 right
= tempCellRect
.x
+ tempCellRect
.width
;
8504 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8505 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8509 i
= visibleRightCol
; // jump over inner cells.
8514 // Convert to scrolled coords
8515 CalcScrolledPosition( left
, top
, &left
, &top
);
8516 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8518 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8519 return wxRect(0,0,0,0);
8521 resultRect
.SetLeft( wxMax(0, left
) );
8522 resultRect
.SetTop( wxMax(0, top
) );
8523 resultRect
.SetRight( wxMin(cw
, right
) );
8524 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8529 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8530 const wxGridOperations
& oper
)
8533 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8534 const int numLines
= oper
.GetNumberOfLines(this);
8535 for ( int i
= 0; i
< numLines
; i
++ )
8537 int size
= sizeInfo
.GetSize(i
);
8538 if ( size
!= sizeInfo
.m_sizeDefault
)
8539 oper
.SetLineSize(this, i
, size
);
8544 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8546 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8549 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8551 DoSetSizes(sizeInfo
, wxGridRowOperations());
8554 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8556 m_sizeDefault
= defSize
;
8557 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8559 if ( allSizes
[i
] != defSize
)
8560 m_customSizes
[i
] = allSizes
[i
];
8564 int wxGridSizesInfo::GetSize(unsigned pos
) const
8566 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8568 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8571 // ----------------------------------------------------------------------------
8573 // ----------------------------------------------------------------------------
8575 #if wxUSE_DRAG_AND_DROP
8577 // this allow setting drop target directly on wxGrid
8578 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8580 GetGridWindow()->SetDropTarget(dropTarget
);
8583 #endif // wxUSE_DRAG_AND_DROP
8585 // ----------------------------------------------------------------------------
8586 // grid event classes
8587 // ----------------------------------------------------------------------------
8589 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8591 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8592 int row
, int col
, int x
, int y
, bool sel
,
8593 bool control
, bool shift
, bool alt
, bool meta
)
8594 : wxNotifyEvent( type
, id
),
8595 wxKeyboardState(control
, shift
, alt
, meta
)
8597 Init(row
, col
, x
, y
, sel
);
8599 SetEventObject(obj
);
8602 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8604 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8605 int rowOrCol
, int x
, int y
,
8606 bool control
, bool shift
, bool alt
, bool meta
)
8607 : wxNotifyEvent( type
, id
),
8608 wxKeyboardState(control
, shift
, alt
, meta
)
8610 Init(rowOrCol
, x
, y
);
8612 SetEventObject(obj
);
8616 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8618 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8619 const wxGridCellCoords
& topLeft
,
8620 const wxGridCellCoords
& bottomRight
,
8621 bool sel
, bool control
,
8622 bool shift
, bool alt
, bool meta
)
8623 : wxNotifyEvent( type
, id
),
8624 wxKeyboardState(control
, shift
, alt
, meta
)
8626 Init(topLeft
, bottomRight
, sel
);
8628 SetEventObject(obj
);
8632 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8634 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8635 wxObject
* obj
, int row
,
8636 int col
, wxControl
* ctrl
)
8637 : wxCommandEvent(type
, id
)
8639 SetEventObject(obj
);
8646 // ----------------------------------------------------------------------------
8647 // wxGridTypeRegistry
8648 // ----------------------------------------------------------------------------
8650 wxGridTypeRegistry::~wxGridTypeRegistry()
8652 size_t count
= m_typeinfo
.GetCount();
8653 for ( size_t i
= 0; i
< count
; i
++ )
8654 delete m_typeinfo
[i
];
8657 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8658 wxGridCellRenderer
* renderer
,
8659 wxGridCellEditor
* editor
)
8661 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8663 // is it already registered?
8664 int loc
= FindRegisteredDataType(typeName
);
8665 if ( loc
!= wxNOT_FOUND
)
8667 delete m_typeinfo
[loc
];
8668 m_typeinfo
[loc
] = info
;
8672 m_typeinfo
.Add(info
);
8676 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8678 size_t count
= m_typeinfo
.GetCount();
8679 for ( size_t i
= 0; i
< count
; i
++ )
8681 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8690 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8692 int index
= FindRegisteredDataType(typeName
);
8693 if ( index
== wxNOT_FOUND
)
8695 // check whether this is one of the standard ones, in which case
8696 // register it "on the fly"
8698 if ( typeName
== wxGRID_VALUE_STRING
)
8700 RegisterDataType(wxGRID_VALUE_STRING
,
8701 new wxGridCellStringRenderer
,
8702 new wxGridCellTextEditor
);
8705 #endif // wxUSE_TEXTCTRL
8707 if ( typeName
== wxGRID_VALUE_BOOL
)
8709 RegisterDataType(wxGRID_VALUE_BOOL
,
8710 new wxGridCellBoolRenderer
,
8711 new wxGridCellBoolEditor
);
8714 #endif // wxUSE_CHECKBOX
8716 if ( typeName
== wxGRID_VALUE_NUMBER
)
8718 RegisterDataType(wxGRID_VALUE_NUMBER
,
8719 new wxGridCellNumberRenderer
,
8720 new wxGridCellNumberEditor
);
8722 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8724 RegisterDataType(wxGRID_VALUE_FLOAT
,
8725 new wxGridCellFloatRenderer
,
8726 new wxGridCellFloatEditor
);
8729 #endif // wxUSE_TEXTCTRL
8731 if ( typeName
== wxGRID_VALUE_CHOICE
)
8733 RegisterDataType(wxGRID_VALUE_CHOICE
,
8734 new wxGridCellStringRenderer
,
8735 new wxGridCellChoiceEditor
);
8738 #endif // wxUSE_COMBOBOX
8743 // we get here only if just added the entry for this type, so return
8745 index
= m_typeinfo
.GetCount() - 1;
8751 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8753 int index
= FindDataType(typeName
);
8754 if ( index
== wxNOT_FOUND
)
8756 // the first part of the typename is the "real" type, anything after ':'
8757 // are the parameters for the renderer
8758 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8759 if ( index
== wxNOT_FOUND
)
8764 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8765 wxGridCellRenderer
*rendererOld
= renderer
;
8766 renderer
= renderer
->Clone();
8767 rendererOld
->DecRef();
8769 wxGridCellEditor
*editor
= GetEditor(index
);
8770 wxGridCellEditor
*editorOld
= editor
;
8771 editor
= editor
->Clone();
8772 editorOld
->DecRef();
8774 // do it even if there are no parameters to reset them to defaults
8775 wxString params
= typeName
.AfterFirst(wxT(':'));
8776 renderer
->SetParameters(params
);
8777 editor
->SetParameters(params
);
8779 // register the new typename
8780 RegisterDataType(typeName
, renderer
, editor
);
8782 // we just registered it, it's the last one
8783 index
= m_typeinfo
.GetCount() - 1;
8789 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8791 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8798 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8800 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8807 #endif // wxUSE_GRID