1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
58 const char wxGridNameStr
[] = "grid";
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_MOTIF(identifier) identifier
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
69 #define WXUNUSED_GTK(identifier) identifier
72 // Required for wxIs... functions
75 WX_DECLARE_HASH_SET_WITH_DECL_PTR(int, wxIntegerHash
, wxIntegerEqual
,
76 wxGridFixedIndicesSet
, class WXDLLIMPEXP_ADV
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
86 //#define DEBUG_ATTR_CACHE
87 #ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits
= 0;
89 static size_t gs_nAttrCacheMisses
= 0;
92 // this struct simply combines together the default header renderers
94 // as the renderers ctors are trivial, there is no problem with making them
96 struct DefaultHeaderRenderers
98 wxGridColumnHeaderRendererDefault colRenderer
;
99 wxGridRowHeaderRendererDefault rowRenderer
;
100 wxGridCornerHeaderRendererDefault cornerRenderer
;
101 } gs_defaultHeaderRenderers
;
103 } // anonymous namespace
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
110 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
116 const size_t GRID_SCROLL_LINE_X
= 15;
117 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
119 // the size of hash tables used a bit everywhere (the max number of elements
120 // in these hash tables is the number of rows/columns)
121 const int GRID_HASH_SIZE
= 100;
123 // the minimal distance in pixels the mouse needs to move to start a drag
125 const int DRAG_SENSITIVITY
= 3;
127 } // anonymous namespace
129 #include "wx/arrimpl.cpp"
131 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
132 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK
, wxGridEvent
);
139 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK
, wxGridEvent
);
140 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK
, wxGridEvent
);
141 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK
, wxGridEvent
);
142 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG
, wxGridEvent
);
143 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK
, wxGridEvent
);
144 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK
, wxGridEvent
);
145 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK
, wxGridEvent
);
146 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK
, wxGridEvent
);
147 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE
, wxGridSizeEvent
);
148 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE
, wxGridSizeEvent
);
149 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE
, wxGridEvent
);
150 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
151 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
152 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
154 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
155 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
158 wxDEFINE_EVENT( wxEVT_GRID_TABBING
, wxGridEvent
);
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
167 // ensure that first is less or equal to second, swapping the values if
169 void EnsureFirstLessThanSecond(int& first
, int& second
)
171 if ( first
> second
)
172 wxSwap(first
, second
);
175 } // anonymous namespace
177 // ============================================================================
179 // ============================================================================
181 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
183 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
184 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
185 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
186 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
189 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
190 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
191 EVT_HEADER_DCLICK(wxID_ANY
, wxGridHeaderCtrl::OnDoubleClick
)
192 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnRightClick
)
194 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
195 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
196 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
198 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
199 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
202 wxGridOperations
& wxGridRowOperations::Dual() const
204 static wxGridColumnOperations s_colOper
;
209 wxGridOperations
& wxGridColumnOperations::Dual() const
211 static wxGridRowOperations s_rowOper
;
216 // ----------------------------------------------------------------------------
217 // wxGridCellWorker is an (almost) empty common base class for
218 // wxGridCellRenderer and wxGridCellEditor managing ref counting
219 // ----------------------------------------------------------------------------
221 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
226 wxGridCellWorker::~wxGridCellWorker()
230 // ----------------------------------------------------------------------------
231 // wxGridHeaderLabelsRenderer and related classes
232 // ----------------------------------------------------------------------------
234 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid
& grid
,
236 const wxString
& value
,
240 int textOrientation
) const
242 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
243 dc
.SetTextForeground(grid
.GetLabelTextColour());
244 dc
.SetFont(grid
.GetLabelFont());
245 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
249 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
253 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
254 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
255 rect
.GetRight(), rect
.GetBottom());
256 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
257 rect
.GetLeft(), rect
.GetBottom());
258 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
259 rect
.GetRight() + 1, rect
.GetBottom());
261 dc
.SetPen(*wxWHITE_PEN
);
262 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
263 rect
.GetLeft() + 1, rect
.GetBottom());
264 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
265 rect
.GetRight(), rect
.GetTop());
270 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
274 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
275 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
276 rect
.GetRight(), rect
.GetBottom());
277 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
278 rect
.GetRight(), rect
.GetTop());
279 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
280 rect
.GetRight() + 1, rect
.GetBottom());
282 dc
.SetPen(*wxWHITE_PEN
);
283 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
284 rect
.GetLeft(), rect
.GetBottom());
285 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
286 rect
.GetRight(), rect
.GetTop() + 1);
291 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
295 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
296 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
297 rect
.GetRight() - 1, rect
.GetTop());
298 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
299 rect
.GetLeft(), rect
.GetBottom() - 1);
300 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
301 rect
.GetRight(), rect
.GetTop());
302 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
303 rect
.GetLeft(), rect
.GetBottom());
305 dc
.SetPen(*wxWHITE_PEN
);
306 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
307 rect
.GetRight() - 1, rect
.GetTop() + 1);
308 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
309 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
320 m_isReadOnly
= Unset
;
325 m_attrkind
= wxGridCellAttr::Cell
;
327 m_sizeRows
= m_sizeCols
= 1;
328 m_overflow
= UnsetOverflow
;
330 SetDefAttr(attrDefault
);
333 wxGridCellAttr
*wxGridCellAttr::Clone() const
335 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
337 if ( HasTextColour() )
338 attr
->SetTextColour(GetTextColour());
339 if ( HasBackgroundColour() )
340 attr
->SetBackgroundColour(GetBackgroundColour());
342 attr
->SetFont(GetFont());
343 if ( HasAlignment() )
344 attr
->SetAlignment(m_hAlign
, m_vAlign
);
346 attr
->SetSize( m_sizeRows
, m_sizeCols
);
350 attr
->SetRenderer(m_renderer
);
351 m_renderer
->IncRef();
355 attr
->SetEditor(m_editor
);
362 attr
->SetOverflow( m_overflow
== Overflow
);
363 attr
->SetKind( m_attrkind
);
368 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
370 if ( !HasTextColour() && mergefrom
->HasTextColour() )
371 SetTextColour(mergefrom
->GetTextColour());
372 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
373 SetBackgroundColour(mergefrom
->GetBackgroundColour());
374 if ( !HasFont() && mergefrom
->HasFont() )
375 SetFont(mergefrom
->GetFont());
376 if ( !HasAlignment() && mergefrom
->HasAlignment() )
379 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
380 SetAlignment(hAlign
, vAlign
);
382 if ( !HasSize() && mergefrom
->HasSize() )
383 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
385 // Directly access member functions as GetRender/Editor don't just return
386 // m_renderer/m_editor
388 // Maybe add support for merge of Render and Editor?
389 if (!HasRenderer() && mergefrom
->HasRenderer() )
391 m_renderer
= mergefrom
->m_renderer
;
392 m_renderer
->IncRef();
394 if ( !HasEditor() && mergefrom
->HasEditor() )
396 m_editor
= mergefrom
->m_editor
;
399 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
400 SetReadOnly(mergefrom
->IsReadOnly());
402 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
403 SetOverflow(mergefrom
->GetOverflow());
405 SetDefAttr(mergefrom
->m_defGridAttr
);
408 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
410 // The size of a cell is normally 1,1
412 // If this cell is larger (2,2) then this is the top left cell
413 // the other cells that will be covered (lower right cells) must be
414 // set to negative or zero values such that
415 // row + num_rows of the covered cell points to the larger cell (this cell)
416 // same goes for the col + num_cols.
418 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
420 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
421 !((num_rows
<= 0) && (num_cols
> 0)) ||
422 !((num_rows
== 0) && (num_cols
== 0))),
423 wxT("wxGridCellAttr::SetSize only takes two positive values or negative/zero values"));
425 m_sizeRows
= num_rows
;
426 m_sizeCols
= num_cols
;
429 const wxColour
& wxGridCellAttr::GetTextColour() const
435 else if (m_defGridAttr
&& m_defGridAttr
!= this)
437 return m_defGridAttr
->GetTextColour();
441 wxFAIL_MSG(wxT("Missing default cell attribute"));
446 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
448 if (HasBackgroundColour())
452 else if (m_defGridAttr
&& m_defGridAttr
!= this)
454 return m_defGridAttr
->GetBackgroundColour();
458 wxFAIL_MSG(wxT("Missing default cell attribute"));
463 const wxFont
& wxGridCellAttr::GetFont() const
469 else if (m_defGridAttr
&& m_defGridAttr
!= this)
471 return m_defGridAttr
->GetFont();
475 wxFAIL_MSG(wxT("Missing default cell attribute"));
480 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
489 else if (m_defGridAttr
&& m_defGridAttr
!= this)
491 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
495 wxFAIL_MSG(wxT("Missing default cell attribute"));
499 void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
501 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
504 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
508 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
511 *num_rows
= m_sizeRows
;
513 *num_cols
= m_sizeCols
;
516 // GetRenderer and GetEditor use a slightly different decision path about
517 // which attribute to use. If a non-default attr object has one then it is
518 // used, otherwise the default editor or renderer is fetched from the grid and
519 // used. It should be the default for the data type of the cell. If it is
520 // NULL (because the table has a type that the grid does not have in its
521 // registry), then the grid's default editor or renderer is used.
523 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
525 wxGridCellRenderer
*renderer
= NULL
;
527 if ( m_renderer
&& this != m_defGridAttr
)
529 // use the cells renderer if it has one
530 renderer
= m_renderer
;
533 else // no non-default cell renderer
535 // get default renderer for the data type
538 // GetDefaultRendererForCell() will do IncRef() for us
539 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
542 if ( renderer
== NULL
)
544 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
546 // if we still don't have one then use the grid default
547 // (no need for IncRef() here neither)
548 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
550 else // default grid attr
552 // use m_renderer which we had decided not to use initially
553 renderer
= m_renderer
;
560 // we're supposed to always find something
561 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
566 // same as above, except for s/renderer/editor/g
567 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
569 wxGridCellEditor
*editor
= NULL
;
571 if ( m_editor
&& this != m_defGridAttr
)
573 // use the cells editor if it has one
577 else // no non default cell editor
579 // get default editor for the data type
582 // GetDefaultEditorForCell() will do IncRef() for us
583 editor
= grid
->GetDefaultEditorForCell(row
, col
);
586 if ( editor
== NULL
)
588 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
590 // if we still don't have one then use the grid default
591 // (no need for IncRef() here neither)
592 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
594 else // default grid attr
596 // use m_editor which we had decided not to use initially
604 // we're supposed to always find something
605 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
610 // ----------------------------------------------------------------------------
611 // wxGridCellAttrData
612 // ----------------------------------------------------------------------------
614 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
616 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
617 // touch attribute's reference counting explicitly, since this
618 // is managed by class wxGridCellWithAttr
619 int n
= FindIndex(row
, col
);
620 if ( n
== wxNOT_FOUND
)
625 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
627 //else: nothing to do
629 else // we already have an attribute for this cell
633 // change the attribute
634 m_attrs
[(size_t)n
].ChangeAttr(attr
);
638 // remove this attribute
639 m_attrs
.RemoveAt((size_t)n
);
644 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
646 wxGridCellAttr
*attr
= NULL
;
648 int n
= FindIndex(row
, col
);
649 if ( n
!= wxNOT_FOUND
)
651 attr
= m_attrs
[(size_t)n
].attr
;
658 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
660 size_t count
= m_attrs
.GetCount();
661 for ( size_t n
= 0; n
< count
; n
++ )
663 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
664 wxCoord row
= coords
.GetRow();
665 if ((size_t)row
>= pos
)
669 // If rows inserted, include row counter where necessary
670 coords
.SetRow(row
+ numRows
);
672 else if (numRows
< 0)
674 // If rows deleted ...
675 if ((size_t)row
>= pos
- numRows
)
677 // ...either decrement row counter (if row still exists)...
678 coords
.SetRow(row
+ numRows
);
682 // ...or remove the attribute
692 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
694 size_t count
= m_attrs
.GetCount();
695 for ( size_t n
= 0; n
< count
; n
++ )
697 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
698 wxCoord col
= coords
.GetCol();
699 if ( (size_t)col
>= pos
)
703 // If rows inserted, include row counter where necessary
704 coords
.SetCol(col
+ numCols
);
706 else if (numCols
< 0)
708 // If rows deleted ...
709 if ((size_t)col
>= pos
- numCols
)
711 // ...either decrement row counter (if row still exists)...
712 coords
.SetCol(col
+ numCols
);
716 // ...or remove the attribute
726 int wxGridCellAttrData::FindIndex(int row
, int col
) const
728 size_t count
= m_attrs
.GetCount();
729 for ( size_t n
= 0; n
< count
; n
++ )
731 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
732 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
741 // ----------------------------------------------------------------------------
742 // wxGridRowOrColAttrData
743 // ----------------------------------------------------------------------------
745 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
747 size_t count
= m_attrs
.GetCount();
748 for ( size_t n
= 0; n
< count
; n
++ )
750 m_attrs
[n
]->DecRef();
754 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
756 wxGridCellAttr
*attr
= NULL
;
758 int n
= m_rowsOrCols
.Index(rowOrCol
);
759 if ( n
!= wxNOT_FOUND
)
761 attr
= m_attrs
[(size_t)n
];
768 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
770 int i
= m_rowsOrCols
.Index(rowOrCol
);
771 if ( i
== wxNOT_FOUND
)
775 // store the new attribute, taking its ownership
776 m_rowsOrCols
.Add(rowOrCol
);
781 else // we have an attribute for this row or column
783 size_t n
= (size_t)i
;
785 // notice that this code works correctly even when the old attribute is
786 // the same as the new one: as we own of it, we must call DecRef() on
787 // it in any case and this won't result in destruction of the new
788 // attribute if it's the same as old one because it must have ref count
789 // of at least 2 to be passed to us while we keep a reference to it too
790 m_attrs
[n
]->DecRef();
794 // replace the attribute with the new one
797 else // remove the attribute
799 m_rowsOrCols
.RemoveAt(n
);
805 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
807 size_t count
= m_attrs
.GetCount();
808 for ( size_t n
= 0; n
< count
; n
++ )
810 int & rowOrCol
= m_rowsOrCols
[n
];
811 if ( (size_t)rowOrCol
>= pos
)
813 if ( numRowsOrCols
> 0 )
815 // If rows inserted, include row counter where necessary
816 rowOrCol
+= numRowsOrCols
;
818 else if ( numRowsOrCols
< 0)
820 // If rows deleted, either decrement row counter (if row still exists)
821 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
822 rowOrCol
+= numRowsOrCols
;
825 m_rowsOrCols
.RemoveAt(n
);
826 m_attrs
[n
]->DecRef();
836 // ----------------------------------------------------------------------------
837 // wxGridCellAttrProvider
838 // ----------------------------------------------------------------------------
840 wxGridCellAttrProvider::wxGridCellAttrProvider()
845 wxGridCellAttrProvider::~wxGridCellAttrProvider()
850 void wxGridCellAttrProvider::InitData()
852 m_data
= new wxGridCellAttrProviderData
;
855 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
856 wxGridCellAttr::wxAttrKind kind
) const
858 wxGridCellAttr
*attr
= NULL
;
863 case (wxGridCellAttr::Any
):
864 // Get cached merge attributes.
865 // Currently not used as no cache implemented as not mutable
866 // attr = m_data->m_mergeAttr.GetAttr(row, col);
869 // Basically implement old version.
870 // Also check merge cache, so we don't have to re-merge every time..
871 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
872 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
873 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
875 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
877 // Two or more are non NULL
878 attr
= new wxGridCellAttr
;
879 attr
->SetKind(wxGridCellAttr::Merged
);
881 // Order is important..
884 attr
->MergeWith(attrcell
);
889 attr
->MergeWith(attrcol
);
894 attr
->MergeWith(attrrow
);
898 // store merge attr if cache implemented
900 //m_data->m_mergeAttr.SetAttr(attr, row, col);
904 // one or none is non null return it or null.
923 case (wxGridCellAttr::Cell
):
924 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
927 case (wxGridCellAttr::Col
):
928 attr
= m_data
->m_colAttrs
.GetAttr(col
);
931 case (wxGridCellAttr::Row
):
932 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
937 // (wxGridCellAttr::Default):
938 // (wxGridCellAttr::Merged):
946 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
952 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
955 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
960 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
963 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
968 m_data
->m_colAttrs
.SetAttr(attr
, col
);
971 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
975 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
977 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
981 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
985 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
987 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
991 const wxGridColumnHeaderRenderer
&
992 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
994 return gs_defaultHeaderRenderers
.colRenderer
;
997 const wxGridRowHeaderRenderer
&
998 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
1000 return gs_defaultHeaderRenderers
.rowRenderer
;
1003 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
1005 return gs_defaultHeaderRenderers
.cornerRenderer
;
1008 // ----------------------------------------------------------------------------
1010 // ----------------------------------------------------------------------------
1012 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
1014 wxGridTableBase::wxGridTableBase()
1017 m_attrProvider
= NULL
;
1020 wxGridTableBase::~wxGridTableBase()
1022 delete m_attrProvider
;
1025 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1027 delete m_attrProvider
;
1028 m_attrProvider
= attrProvider
;
1031 bool wxGridTableBase::CanHaveAttributes()
1033 if ( ! GetAttrProvider() )
1035 // use the default attr provider by default
1036 SetAttrProvider(new wxGridCellAttrProvider
);
1042 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1044 if ( m_attrProvider
)
1045 return m_attrProvider
->GetAttr(row
, col
, kind
);
1050 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1052 if ( m_attrProvider
)
1055 attr
->SetKind(wxGridCellAttr::Cell
);
1056 m_attrProvider
->SetAttr(attr
, row
, col
);
1060 // as we take ownership of the pointer and don't store it, we must
1066 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1068 if ( m_attrProvider
)
1070 attr
->SetKind(wxGridCellAttr::Row
);
1071 m_attrProvider
->SetRowAttr(attr
, row
);
1075 // as we take ownership of the pointer and don't store it, we must
1081 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1083 if ( m_attrProvider
)
1085 attr
->SetKind(wxGridCellAttr::Col
);
1086 m_attrProvider
->SetColAttr(attr
, col
);
1090 // as we take ownership of the pointer and don't store it, we must
1096 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1097 size_t WXUNUSED(numRows
) )
1099 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1104 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1106 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1111 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1112 size_t WXUNUSED(numRows
) )
1114 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1119 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1120 size_t WXUNUSED(numCols
) )
1122 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1127 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1129 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1134 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1135 size_t WXUNUSED(numCols
) )
1137 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1142 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1146 // RD: Starting the rows at zero confuses users,
1147 // no matter how much it makes sense to us geeks.
1153 wxString
wxGridTableBase::GetColLabelValue( int col
)
1155 // default col labels are:
1156 // cols 0 to 25 : A-Z
1157 // cols 26 to 675 : AA-ZZ
1162 for ( n
= 1; ; n
++ )
1164 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1170 // reverse the string...
1172 for ( i
= 0; i
< n
; i
++ )
1180 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1182 return wxGRID_VALUE_STRING
;
1185 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1186 const wxString
& typeName
)
1188 return typeName
== wxGRID_VALUE_STRING
;
1191 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1193 return CanGetValueAs(row
, col
, typeName
);
1196 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1201 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1206 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1211 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1212 long WXUNUSED(value
) )
1216 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1217 double WXUNUSED(value
) )
1221 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1222 bool WXUNUSED(value
) )
1226 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1227 const wxString
& WXUNUSED(typeName
) )
1232 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1233 const wxString
& WXUNUSED(typeName
),
1234 void* WXUNUSED(value
) )
1238 //////////////////////////////////////////////////////////////////////
1240 // Message class for the grid table to send requests and notifications
1244 wxGridTableMessage::wxGridTableMessage()
1252 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1253 int commandInt1
, int commandInt2
)
1257 m_comInt1
= commandInt1
;
1258 m_comInt2
= commandInt2
;
1261 //////////////////////////////////////////////////////////////////////
1263 // A basic grid table for string data. An object of this class will
1264 // created by wxGrid if you don't specify an alternative table class.
1267 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1269 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1271 wxGridStringTable::wxGridStringTable()
1277 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1280 m_numCols
= numCols
;
1282 m_data
.Alloc( numRows
);
1285 sa
.Alloc( numCols
);
1286 sa
.Add( wxEmptyString
, numCols
);
1288 m_data
.Add( sa
, numRows
);
1291 wxString
wxGridStringTable::GetValue( int row
, int col
)
1293 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1294 (col
>= 0 && col
< GetNumberCols()),
1296 wxT("invalid row or column index in wxGridStringTable") );
1298 return m_data
[row
][col
];
1301 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1303 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1304 (col
>= 0 && col
< GetNumberCols()),
1305 wxT("invalid row or column index in wxGridStringTable") );
1307 m_data
[row
][col
] = value
;
1310 void wxGridStringTable::Clear()
1313 int numRows
, numCols
;
1315 numRows
= m_data
.GetCount();
1318 numCols
= m_data
[0].GetCount();
1320 for ( row
= 0; row
< numRows
; row
++ )
1322 for ( col
= 0; col
< numCols
; col
++ )
1324 m_data
[row
][col
] = wxEmptyString
;
1330 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1332 size_t curNumRows
= m_data
.GetCount();
1333 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1334 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1336 if ( pos
>= curNumRows
)
1338 return AppendRows( numRows
);
1342 sa
.Alloc( curNumCols
);
1343 sa
.Add( wxEmptyString
, curNumCols
);
1344 m_data
.Insert( sa
, pos
, numRows
);
1348 wxGridTableMessage
msg( this,
1349 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1353 GetView()->ProcessTableMessage( msg
);
1359 bool wxGridStringTable::AppendRows( size_t numRows
)
1361 size_t curNumRows
= m_data
.GetCount();
1362 size_t curNumCols
= ( curNumRows
> 0
1363 ? m_data
[0].GetCount()
1364 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1367 if ( curNumCols
> 0 )
1369 sa
.Alloc( curNumCols
);
1370 sa
.Add( wxEmptyString
, curNumCols
);
1373 m_data
.Add( sa
, numRows
);
1377 wxGridTableMessage
msg( this,
1378 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1381 GetView()->ProcessTableMessage( msg
);
1387 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1389 size_t curNumRows
= m_data
.GetCount();
1391 if ( pos
>= curNumRows
)
1393 wxFAIL_MSG( wxString::Format
1395 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1397 (unsigned long)numRows
,
1398 (unsigned long)curNumRows
1404 if ( numRows
> curNumRows
- pos
)
1406 numRows
= curNumRows
- pos
;
1409 if ( numRows
>= curNumRows
)
1415 m_data
.RemoveAt( pos
, numRows
);
1420 wxGridTableMessage
msg( this,
1421 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1425 GetView()->ProcessTableMessage( msg
);
1431 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1435 size_t curNumRows
= m_data
.GetCount();
1436 size_t curNumCols
= ( curNumRows
> 0
1437 ? m_data
[0].GetCount()
1438 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1440 if ( pos
>= curNumCols
)
1442 return AppendCols( numCols
);
1445 if ( !m_colLabels
.IsEmpty() )
1447 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1450 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1451 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1454 for ( row
= 0; row
< curNumRows
; row
++ )
1456 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1458 m_data
[row
].Insert( wxEmptyString
, col
);
1462 m_numCols
+= numCols
;
1466 wxGridTableMessage
msg( this,
1467 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1471 GetView()->ProcessTableMessage( msg
);
1477 bool wxGridStringTable::AppendCols( size_t numCols
)
1481 size_t curNumRows
= m_data
.GetCount();
1483 for ( row
= 0; row
< curNumRows
; row
++ )
1485 m_data
[row
].Add( wxEmptyString
, numCols
);
1488 m_numCols
+= numCols
;
1492 wxGridTableMessage
msg( this,
1493 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1496 GetView()->ProcessTableMessage( msg
);
1502 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1506 size_t curNumRows
= m_data
.GetCount();
1507 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1508 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1510 if ( pos
>= curNumCols
)
1512 wxFAIL_MSG( wxString::Format
1514 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1516 (unsigned long)numCols
,
1517 (unsigned long)curNumCols
1524 colID
= GetView()->GetColAt( pos
);
1528 if ( numCols
> curNumCols
- colID
)
1530 numCols
= curNumCols
- colID
;
1533 if ( !m_colLabels
.IsEmpty() )
1535 // m_colLabels stores just as many elements as it needs, e.g. if only
1536 // the label of the first column had been set it would have only one
1537 // element and not numCols, so account for it
1538 int numRemaining
= m_colLabels
.size() - colID
;
1539 if (numRemaining
> 0)
1540 m_colLabels
.RemoveAt( colID
, wxMin(numCols
, numRemaining
) );
1543 if ( numCols
>= curNumCols
)
1545 for ( row
= 0; row
< curNumRows
; row
++ )
1547 m_data
[row
].Clear();
1552 else // something will be left
1554 for ( row
= 0; row
< curNumRows
; row
++ )
1556 m_data
[row
].RemoveAt( colID
, numCols
);
1559 m_numCols
-= numCols
;
1564 wxGridTableMessage
msg( this,
1565 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1569 GetView()->ProcessTableMessage( msg
);
1575 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1577 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1579 // using default label
1581 return wxGridTableBase::GetRowLabelValue( row
);
1585 return m_rowLabels
[row
];
1589 wxString
wxGridStringTable::GetColLabelValue( int col
)
1591 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1593 // using default label
1595 return wxGridTableBase::GetColLabelValue( col
);
1599 return m_colLabels
[col
];
1603 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1605 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1607 int n
= m_rowLabels
.GetCount();
1610 for ( i
= n
; i
<= row
; i
++ )
1612 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1616 m_rowLabels
[row
] = value
;
1619 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1621 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1623 int n
= m_colLabels
.GetCount();
1626 for ( i
= n
; i
<= col
; i
++ )
1628 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1632 m_colLabels
[col
] = value
;
1636 //////////////////////////////////////////////////////////////////////
1637 //////////////////////////////////////////////////////////////////////
1639 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1640 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1643 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1645 m_owner
->CancelMouseCapture();
1648 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1649 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1650 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1651 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1654 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1658 // NO - don't do this because it will set both the x and y origin
1659 // coords to match the parent scrolled window and we just want to
1660 // set the y coord - MB
1662 // m_owner->PrepareDC( dc );
1665 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1666 wxPoint pt
= dc
.GetDeviceOrigin();
1667 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1669 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1670 m_owner
->DrawRowLabels( dc
, rows
);
1673 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1675 m_owner
->ProcessRowLabelMouseEvent( event
);
1678 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1680 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1684 //////////////////////////////////////////////////////////////////////
1686 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1687 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1688 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1689 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1692 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1696 // NO - don't do this because it will set both the x and y origin
1697 // coords to match the parent scrolled window and we just want to
1698 // set the x coord - MB
1700 // m_owner->PrepareDC( dc );
1703 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1704 wxPoint pt
= dc
.GetDeviceOrigin();
1705 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1706 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1708 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1710 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1711 m_owner
->DrawColLabels( dc
, cols
);
1714 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1716 m_owner
->ProcessColLabelMouseEvent( event
);
1719 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1721 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1725 //////////////////////////////////////////////////////////////////////
1727 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1728 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1729 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1730 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1733 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1737 m_owner
->DrawCornerLabel(dc
);
1740 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1742 m_owner
->ProcessCornerLabelMouseEvent( event
);
1745 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1747 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1751 //////////////////////////////////////////////////////////////////////
1753 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1754 EVT_PAINT( wxGridWindow::OnPaint
)
1755 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1756 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1757 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1758 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1759 EVT_CHAR( wxGridWindow::OnChar
)
1760 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1761 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1762 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1765 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1767 wxPaintDC
dc( this );
1768 m_owner
->PrepareDC( dc
);
1769 wxRegion reg
= GetUpdateRegion();
1770 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1771 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1773 m_owner
->DrawGridSpace( dc
);
1775 m_owner
->DrawAllGridLines( dc
, reg
);
1777 m_owner
->DrawHighlight( dc
, dirtyCells
);
1780 void wxGrid::Render( wxDC
& dc
,
1781 const wxPoint
& position
,
1783 const wxGridCellCoords
& topLeft
,
1784 const wxGridCellCoords
& bottomRight
,
1787 wxCHECK_RET( bottomRight
.GetCol() < GetNumberCols(),
1788 "Invalid right column" );
1789 wxCHECK_RET( bottomRight
.GetRow() < GetNumberRows(),
1790 "Invalid bottom row" );
1792 // store user settings and reset later
1794 // remove grid selection, don't paint selection colour
1795 // unless we have wxGRID_DRAW_SELECTION
1796 // block selections are the only ones catered for here
1797 wxGridCellCoordsArray selectedCells
;
1798 bool hasSelection
= IsSelection();
1799 if ( hasSelection
&& !( style
& wxGRID_DRAW_SELECTION
) )
1801 selectedCells
= GetSelectionBlockTopLeft();
1802 // non block selections may not have a bottom right
1803 if ( GetSelectionBlockBottomRight().size() )
1804 selectedCells
.Add( GetSelectionBlockBottomRight()[ 0 ] );
1809 // store user device origin
1810 wxCoord userOriginX
, userOriginY
;
1811 dc
.GetDeviceOrigin( &userOriginX
, &userOriginY
);
1814 double scaleUserX
, scaleUserY
;
1815 dc
.GetUserScale( &scaleUserX
, &scaleUserY
);
1817 // set defaults if necessary
1818 wxGridCellCoords
leftTop( topLeft
), rightBottom( bottomRight
);
1819 if ( leftTop
.GetCol() < 0 )
1821 if ( leftTop
.GetRow() < 0 )
1823 if ( rightBottom
.GetCol() < 0 )
1824 rightBottom
.SetCol(GetNumberCols() - 1);
1825 if ( rightBottom
.GetRow() < 0 )
1826 rightBottom
.SetRow(GetNumberRows() - 1);
1828 // get grid offset, size and cell parameters
1829 wxPoint pointOffSet
;
1831 wxGridCellCoordsArray renderCells
;
1832 wxArrayInt arrayCols
;
1833 wxArrayInt arrayRows
;
1835 GetRenderSizes( leftTop
, rightBottom
,
1836 pointOffSet
, sizeGrid
,
1838 arrayCols
, arrayRows
);
1840 // add headers/labels to dimensions
1841 if ( style
& wxGRID_DRAW_ROWS_HEADER
)
1842 sizeGrid
.x
+= GetRowLabelSize();
1843 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1844 sizeGrid
.y
+= GetColLabelSize();
1846 // get render start position in logical units
1847 wxPoint positionRender
= GetRenderPosition( dc
, position
);
1849 wxCoord originX
= dc
.LogicalToDeviceX( positionRender
.x
);
1850 wxCoord originY
= dc
.LogicalToDeviceY( positionRender
.y
);
1852 dc
.SetDeviceOrigin( originX
, originY
);
1854 SetRenderScale( dc
, positionRender
, size
, sizeGrid
);
1856 // draw row headers at specified origin
1857 if ( GetRowLabelSize() > 0 && ( style
& wxGRID_DRAW_ROWS_HEADER
) )
1859 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1861 DrawCornerLabel( dc
); // do only if both col and row labels drawn
1862 originY
+= dc
.LogicalToDeviceYRel( GetColLabelSize() );
1865 originY
-= dc
.LogicalToDeviceYRel( pointOffSet
.y
);
1866 dc
.SetDeviceOrigin( originX
, originY
);
1868 DrawRowLabels( dc
, arrayRows
);
1870 // reset for columns
1871 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1872 originY
-= dc
.LogicalToDeviceYRel( GetColLabelSize() );
1874 originY
+= dc
.LogicalToDeviceYRel( pointOffSet
.y
);
1875 // X offset so we don't overwrite row labels
1876 originX
+= dc
.LogicalToDeviceXRel( GetRowLabelSize() );
1879 // subtract col offset where startcol > 0
1880 originX
-= dc
.LogicalToDeviceXRel( pointOffSet
.x
);
1881 // no y offset for col labels, they are at the Y origin
1883 // draw column labels
1884 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1886 dc
.SetDeviceOrigin( originX
, originY
);
1887 DrawColLabels( dc
, arrayCols
);
1888 // don't overwrite the labels, increment originY
1889 originY
+= dc
.LogicalToDeviceYRel( GetColLabelSize() );
1892 // set device origin to draw grid cells and lines
1893 originY
-= dc
.LogicalToDeviceYRel( pointOffSet
.y
);
1894 dc
.SetDeviceOrigin( originX
, originY
);
1896 // draw cell area background
1897 dc
.SetBrush( GetDefaultCellBackgroundColour() );
1898 dc
.SetPen( *wxTRANSPARENT_PEN
);
1899 // subtract headers from grid area dimensions
1900 wxSize
sizeCells( sizeGrid
);
1901 if ( style
& wxGRID_DRAW_ROWS_HEADER
)
1902 sizeCells
.x
-= GetRowLabelSize();
1903 if ( style
& wxGRID_DRAW_COLS_HEADER
)
1904 sizeCells
.y
-= GetColLabelSize();
1906 dc
.DrawRectangle( pointOffSet
, sizeCells
);
1909 DrawGridCellArea( dc
, renderCells
);
1912 if ( style
& wxGRID_DRAW_CELL_LINES
)
1914 wxRegion
regionClip( pointOffSet
.x
, pointOffSet
.y
,
1915 sizeCells
.x
, sizeCells
.y
);
1917 DrawRangeGridLines(dc
, regionClip
, renderCells
[0], renderCells
.Last());
1920 // draw render rectangle bounding lines
1921 DoRenderBox( dc
, style
,
1922 pointOffSet
, sizeCells
,
1923 leftTop
, rightBottom
);
1925 // restore user setings
1926 dc
.SetDeviceOrigin( userOriginX
, userOriginY
);
1927 dc
.SetUserScale( scaleUserX
, scaleUserY
);
1929 if ( selectedCells
.size() && !( style
& wxGRID_DRAW_SELECTION
) )
1931 SelectBlock( selectedCells
[ 0 ].GetRow(),
1932 selectedCells
[ 0 ].GetCol(),
1933 selectedCells
[ selectedCells
.size() -1 ].GetRow(),
1934 selectedCells
[ selectedCells
.size() -1 ].GetCol() );
1939 wxGrid::SetRenderScale(wxDC
& dc
,
1940 const wxPoint
& pos
, const wxSize
& size
,
1941 const wxSize
& sizeGrid
)
1943 double scaleX
, scaleY
;
1946 if ( size
.GetWidth() != wxDefaultSize
.GetWidth() ) // size.x was specified
1947 sizeTemp
.SetWidth( size
.GetWidth() );
1949 sizeTemp
.SetWidth( dc
.DeviceToLogicalXRel( dc
.GetSize().GetWidth() )
1952 if ( size
.GetHeight() != wxDefaultSize
.GetHeight() ) // size.y was specified
1953 sizeTemp
.SetHeight( size
.GetHeight() );
1955 sizeTemp
.SetHeight( dc
.DeviceToLogicalYRel( dc
.GetSize().GetHeight() )
1958 scaleX
= (double)( (double) sizeTemp
.GetWidth() / (double) sizeGrid
.GetWidth() );
1959 scaleY
= (double)( (double) sizeTemp
.GetHeight() / (double) sizeGrid
.GetHeight() );
1961 dc
.SetUserScale( wxMin( scaleX
, scaleY
), wxMin( scaleX
, scaleY
) );
1964 // get grid rendered size, origin offset and fill cell arrays
1965 void wxGrid::GetRenderSizes( const wxGridCellCoords
& topLeft
,
1966 const wxGridCellCoords
& bottomRight
,
1967 wxPoint
& pointOffSet
, wxSize
& sizeGrid
,
1968 wxGridCellCoordsArray
& renderCells
,
1969 wxArrayInt
& arrayCols
, wxArrayInt
& arrayRows
)
1973 sizeGrid
.SetWidth( 0 );
1974 sizeGrid
.SetHeight( 0 );
1978 wxGridSizesInfo sizeinfo
= GetColSizes();
1979 for ( col
= 0; col
<= bottomRight
.GetCol(); col
++ )
1981 if ( col
< topLeft
.GetCol() )
1983 pointOffSet
.x
+= sizeinfo
.GetSize( col
);
1987 for ( row
= topLeft
.GetRow(); row
<= bottomRight
.GetRow(); row
++ )
1989 renderCells
.Add( wxGridCellCoords( row
, col
));
1990 arrayRows
.Add( row
); // column labels rendered in DrawColLabels
1992 arrayCols
.Add( col
); // row labels rendered in DrawRowLabels
1993 sizeGrid
.x
+= sizeinfo
.GetSize( col
);
1997 sizeinfo
= GetRowSizes();
1998 for ( row
= 0; row
<= bottomRight
.GetRow(); row
++ )
2000 if ( row
< topLeft
.GetRow() )
2001 pointOffSet
.y
+= sizeinfo
.GetSize( row
);
2003 sizeGrid
.y
+= sizeinfo
.GetSize( row
);
2007 // get render start position
2008 // if position not specified use dc draw extents MaxX and MaxY
2009 wxPoint
wxGrid::GetRenderPosition( wxDC
& dc
, const wxPoint
& position
)
2011 wxPoint
positionRender( position
);
2013 if ( !positionRender
.IsFullySpecified() )
2015 if ( positionRender
.x
== wxDefaultPosition
.x
)
2016 positionRender
.x
= dc
.MaxX();
2018 if ( positionRender
.y
== wxDefaultPosition
.y
)
2019 positionRender
.y
= dc
.MaxY();
2022 return positionRender
;
2025 // draw render rectangle bounding lines
2026 // useful where there is multi cell row or col clipping and no cell border
2027 void wxGrid::DoRenderBox( wxDC
& dc
, const int& style
,
2028 const wxPoint
& pointOffSet
,
2029 const wxSize
& sizeCells
,
2030 const wxGridCellCoords
& topLeft
,
2031 const wxGridCellCoords
& bottomRight
)
2033 if ( !( style
& wxGRID_DRAW_BOX_RECT
) )
2036 int bottom
= pointOffSet
.y
+ sizeCells
.GetY(),
2037 right
= pointOffSet
.x
+ sizeCells
.GetX() - 1;
2039 // horiz top line if we are not drawing column header/labels
2040 if ( !( style
& wxGRID_DRAW_COLS_HEADER
) )
2042 int left
= pointOffSet
.x
;
2043 left
+= ( style
& wxGRID_DRAW_COLS_HEADER
)
2044 ? - GetRowLabelSize() : 0;
2045 dc
.SetPen( GetRowGridLinePen( topLeft
.GetRow() ) );
2052 // horiz bottom line
2053 dc
.SetPen( GetRowGridLinePen( bottomRight
.GetRow() ) );
2054 dc
.DrawLine( pointOffSet
.x
, bottom
- 1, right
, bottom
- 1 );
2056 // left vertical line if we are not drawing row header/labels
2057 if ( !( style
& wxGRID_DRAW_ROWS_HEADER
) )
2059 int top
= pointOffSet
.y
;
2060 top
+= ( style
& wxGRID_DRAW_COLS_HEADER
)
2061 ? - GetColLabelSize() : 0;
2062 dc
.SetPen( GetColGridLinePen( topLeft
.GetCol() ) );
2063 dc
.DrawLine( pointOffSet
.x
-1,
2069 // right vertical line
2070 dc
.SetPen( GetColGridLinePen( bottomRight
.GetCol() ) );
2071 dc
.DrawLine( right
, pointOffSet
.y
, right
, bottom
- 1 );
2074 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2076 wxWindow::ScrollWindow( dx
, dy
, rect
);
2077 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
2078 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
2081 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
2083 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
2086 m_owner
->ProcessGridCellMouseEvent( event
);
2089 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
2091 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
2095 // This seems to be required for wxMotif/wxGTK otherwise the mouse
2096 // cursor must be in the cell edit control to get key events
2098 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
2100 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2104 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
2106 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2110 void wxGridWindow::OnChar( wxKeyEvent
& event
)
2112 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2116 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
2120 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
2122 // and if we have any selection, it has to be repainted, because it
2123 // uses different colour when the grid is not focused:
2124 if ( m_owner
->IsSelection() )
2130 // NB: Note that this code is in "else" branch only because the other
2131 // branch refreshes everything and so there's no point in calling
2132 // Refresh() again, *not* because it should only be done if
2133 // !IsSelection(). If the above code is ever optimized to refresh
2134 // only selected area, this needs to be moved out of the "else"
2135 // branch so that it's always executed.
2137 // current cell cursor {dis,re}appears on focus change:
2138 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
2139 m_owner
->GetGridCursorCol());
2140 const wxRect cursor
=
2141 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
2142 Refresh(true, &cursor
);
2145 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
2149 #define internalXToCol(x) XToCol(x, true)
2150 #define internalYToRow(y) YToRow(y, true)
2152 /////////////////////////////////////////////////////////////////////
2154 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
2155 EVT_PAINT( wxGrid::OnPaint
)
2156 EVT_SIZE( wxGrid::OnSize
)
2157 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
2158 EVT_KEY_UP( wxGrid::OnKeyUp
)
2159 EVT_CHAR ( wxGrid::OnChar
)
2160 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
2163 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
2164 const wxPoint
& pos
, const wxSize
& size
,
2165 long style
, const wxString
& name
)
2167 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
2168 style
| wxWANTS_CHARS
, name
))
2171 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
2172 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
2175 SetInitialSize(size
);
2184 m_winCapture
->ReleaseMouse();
2186 // Ensure that the editor control is destroyed before the grid is,
2187 // otherwise we crash later when the editor tries to do something with the
2188 // half destroyed grid
2189 HideCellEditControl();
2191 // Must do this or ~wxScrollHelper will pop the wrong event handler
2192 SetTargetWindow(this);
2194 wxSafeDecRef(m_defaultCellAttr
);
2196 #ifdef DEBUG_ATTR_CACHE
2197 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
2198 wxPrintf(wxT("wxGrid attribute cache statistics: "
2199 "total: %u, hits: %u (%u%%)\n"),
2200 total
, gs_nAttrCacheHits
,
2201 total
? (gs_nAttrCacheHits
*100) / total
: 0);
2204 // if we own the table, just delete it, otherwise at least don't leave it
2205 // with dangling view pointer
2208 else if ( m_table
&& m_table
->GetView() == this )
2209 m_table
->SetView(NULL
);
2211 delete m_typeRegistry
;
2214 delete m_setFixedRows
;
2215 delete m_setFixedCols
;
2219 // ----- internal init and update functions
2222 // NOTE: If using the default visual attributes works everywhere then this can
2223 // be removed as well as the #else cases below.
2224 #define _USE_VISATTR 0
2226 void wxGrid::Create()
2228 // create the type registry
2229 m_typeRegistry
= new wxGridTypeRegistry
;
2231 m_cellEditCtrlEnabled
= false;
2233 m_defaultCellAttr
= new wxGridCellAttr();
2235 // Set default cell attributes
2236 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
2237 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
2238 m_defaultCellAttr
->SetFont(GetFont());
2239 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
2240 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
2241 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
2244 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
2245 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
2247 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
2248 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
2251 m_defaultCellAttr
->SetTextColour(
2252 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
2253 m_defaultCellAttr
->SetBackgroundColour(
2254 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
2259 m_currentCellCoords
= wxGridNoCellCoords
;
2261 // subwindow components that make up the wxGrid
2262 m_rowLabelWin
= new wxGridRowLabelWindow(this);
2263 CreateColumnWindow();
2264 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
2265 m_gridWin
= new wxGridWindow( this );
2267 SetTargetWindow( m_gridWin
);
2270 wxColour gfg
= gva
.colFg
;
2271 wxColour gbg
= gva
.colBg
;
2272 wxColour lfg
= lva
.colFg
;
2273 wxColour lbg
= lva
.colBg
;
2275 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2276 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
2277 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2278 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
2281 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
2282 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
2283 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
2284 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
2285 m_colWindow
->SetOwnForegroundColour(lfg
);
2286 m_colWindow
->SetOwnBackgroundColour(lbg
);
2288 m_gridWin
->SetOwnForegroundColour(gfg
);
2289 m_gridWin
->SetOwnBackgroundColour(gbg
);
2291 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2292 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
2294 // now that we have the grid window, use its font to compute the default
2296 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2297 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2298 m_defaultRowHeight
+= 8;
2300 m_defaultRowHeight
+= 4;
2305 void wxGrid::CreateColumnWindow()
2307 if ( m_useNativeHeader
)
2309 m_colWindow
= new wxGridHeaderCtrl(this);
2310 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2312 else // draw labels ourselves
2314 m_colWindow
= new wxGridColLabelWindow(this);
2315 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2319 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2320 wxGridSelectionModes selmode
)
2322 wxCHECK_MSG( !m_created
,
2324 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2326 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2329 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2331 wxCHECK_RET( m_created
,
2332 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2334 m_selection
->SetSelectionMode( selmode
);
2337 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2339 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2340 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2342 return m_selection
->GetSelectionMode();
2346 wxGrid::SetTable(wxGridTableBase
*table
,
2348 wxGrid::wxGridSelectionModes selmode
)
2350 bool checkSelection
= false;
2353 // stop all processing
2358 m_table
->SetView(0);
2364 wxDELETE(m_selection
);
2369 checkSelection
= true;
2371 // kill row and column size arrays
2372 m_colWidths
.Empty();
2373 m_colRights
.Empty();
2374 m_rowHeights
.Empty();
2375 m_rowBottoms
.Empty();
2380 m_numRows
= table
->GetNumberRows();
2381 m_numCols
= table
->GetNumberCols();
2383 if ( m_useNativeHeader
)
2384 GetGridColHeader()->SetColumnCount(m_numCols
);
2387 m_table
->SetView( this );
2388 m_ownTable
= takeOwnership
;
2389 m_selection
= new wxGridSelection( this, selmode
);
2392 // If the newly set table is smaller than the
2393 // original one current cell and selection regions
2394 // might be invalid,
2395 m_selectedBlockCorner
= wxGridNoCellCoords
;
2396 m_currentCellCoords
=
2397 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2398 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2399 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2400 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2402 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2403 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2406 m_selectedBlockBottomRight
=
2407 wxGridCellCoords(wxMin(m_numRows
,
2408 m_selectedBlockBottomRight
.GetRow()),
2410 m_selectedBlockBottomRight
.GetCol()));
2424 m_cornerLabelWin
= NULL
;
2425 m_rowLabelWin
= NULL
;
2433 m_defaultCellAttr
= NULL
;
2434 m_typeRegistry
= NULL
;
2435 m_winCapture
= NULL
;
2437 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2438 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2441 m_setFixedCols
= NULL
;
2444 m_attrCache
.row
= -1;
2445 m_attrCache
.col
= -1;
2446 m_attrCache
.attr
= NULL
;
2448 m_labelFont
= GetFont();
2449 m_labelFont
.SetWeight( wxBOLD
);
2451 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2452 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2454 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2455 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2456 m_colLabelTextOrientation
= wxHORIZONTAL
;
2458 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2459 m_defaultRowHeight
= 0; // this will be initialized after creation
2461 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2462 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2464 m_gridLineColour
= wxColour( 192,192,192 );
2465 m_gridLinesEnabled
= true;
2466 m_gridLinesClipHorz
=
2467 m_gridLinesClipVert
= true;
2468 m_cellHighlightColour
= *wxBLACK
;
2469 m_cellHighlightPenWidth
= 2;
2470 m_cellHighlightROPenWidth
= 1;
2472 m_canDragColMove
= false;
2474 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2475 m_winCapture
= NULL
;
2476 m_canDragRowSize
= true;
2477 m_canDragColSize
= true;
2478 m_canDragGridSize
= true;
2479 m_canDragCell
= false;
2481 m_dragRowOrCol
= -1;
2482 m_isDragging
= false;
2483 m_startDragPos
= wxDefaultPosition
;
2485 m_sortCol
= wxNOT_FOUND
;
2486 m_sortIsAscending
= true;
2489 m_nativeColumnLabels
= false;
2491 m_waitForSlowClick
= false;
2493 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2494 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2496 m_currentCellCoords
= wxGridNoCellCoords
;
2498 m_selectedBlockTopLeft
=
2499 m_selectedBlockBottomRight
=
2500 m_selectedBlockCorner
= wxGridNoCellCoords
;
2502 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2503 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2505 m_editable
= true; // default for whole grid
2507 m_inOnKeyDown
= false;
2513 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2514 // we don't need to call it neither as the scroll position is (0, 0) right
2515 // now anyhow, so just set the parameters directly
2516 m_xScrollPixelsPerLine
= GRID_SCROLL_LINE_X
;
2517 m_yScrollPixelsPerLine
= GRID_SCROLL_LINE_Y
;
2519 m_tabBehaviour
= Tab_Stop
;
2522 // ----------------------------------------------------------------------------
2523 // the idea is to call these functions only when necessary because they create
2524 // quite big arrays which eat memory mostly unnecessary - in particular, if
2525 // default widths/heights are used for all rows/columns, we may not use these
2528 // with some extra code, it should be possible to only store the widths/heights
2529 // different from default ones (resulting in space savings for huge grids) but
2530 // this is not done currently
2531 // ----------------------------------------------------------------------------
2533 void wxGrid::InitRowHeights()
2535 m_rowHeights
.Empty();
2536 m_rowBottoms
.Empty();
2538 m_rowHeights
.Alloc( m_numRows
);
2539 m_rowBottoms
.Alloc( m_numRows
);
2541 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2544 for ( int i
= 0; i
< m_numRows
; i
++ )
2546 rowBottom
+= m_defaultRowHeight
;
2547 m_rowBottoms
.Add( rowBottom
);
2551 void wxGrid::InitColWidths()
2553 m_colWidths
.Empty();
2554 m_colRights
.Empty();
2556 m_colWidths
.Alloc( m_numCols
);
2557 m_colRights
.Alloc( m_numCols
);
2559 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2561 for ( int i
= 0; i
< m_numCols
; i
++ )
2563 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2564 m_colRights
.Add( colRight
);
2568 int wxGrid::GetColWidth(int col
) const
2570 if ( m_colWidths
.IsEmpty() )
2571 return m_defaultColWidth
;
2573 // a negative width indicates a hidden column
2574 return m_colWidths
[col
] > 0 ? m_colWidths
[col
] : 0;
2577 int wxGrid::GetColLeft(int col
) const
2579 if ( m_colRights
.IsEmpty() )
2580 return GetColPos( col
) * m_defaultColWidth
;
2582 return m_colRights
[col
] - GetColWidth(col
);
2585 int wxGrid::GetColRight(int col
) const
2587 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2591 int wxGrid::GetRowHeight(int row
) const
2593 // no custom heights / hidden rows
2594 if ( m_rowHeights
.IsEmpty() )
2595 return m_defaultRowHeight
;
2597 // a negative height indicates a hidden row
2598 return m_rowHeights
[row
] > 0 ? m_rowHeights
[row
] : 0;
2601 int wxGrid::GetRowTop(int row
) const
2603 if ( m_rowBottoms
.IsEmpty() )
2604 return row
* m_defaultRowHeight
;
2606 return m_rowBottoms
[row
] - GetRowHeight(row
);
2609 int wxGrid::GetRowBottom(int row
) const
2611 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2612 : m_rowBottoms
[row
];
2615 void wxGrid::CalcDimensions()
2617 // compute the size of the scrollable area
2618 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2619 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2624 // take into account editor if shown
2625 if ( IsCellEditControlShown() )
2628 int r
= m_currentCellCoords
.GetRow();
2629 int c
= m_currentCellCoords
.GetCol();
2630 int x
= GetColLeft(c
);
2631 int y
= GetRowTop(r
);
2633 // how big is the editor
2634 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2635 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2636 editor
->GetControl()->GetSize(&w2
, &h2
);
2647 // preserve (more or less) the previous position
2649 GetViewStart( &x
, &y
);
2651 // ensure the position is valid for the new scroll ranges
2653 x
= wxMax( w
- 1, 0 );
2655 y
= wxMax( h
- 1, 0 );
2657 // update the virtual size and refresh the scrollbars to reflect it
2658 m_gridWin
->SetVirtualSize(w
, h
);
2662 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2663 // still must reposition the children
2667 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2669 wxSize
sizeGridWin(size
);
2670 sizeGridWin
.x
-= m_rowLabelWidth
;
2671 sizeGridWin
.y
-= m_colLabelHeight
;
2676 void wxGrid::CalcWindowSizes()
2678 // escape if the window is has not been fully created yet
2680 if ( m_cornerLabelWin
== NULL
)
2684 GetClientSize( &cw
, &ch
);
2686 // the grid may be too small to have enough space for the labels yet, don't
2687 // size the windows to negative sizes in this case
2688 int gw
= cw
- m_rowLabelWidth
;
2689 int gh
= ch
- m_colLabelHeight
;
2695 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2696 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2698 if ( m_colWindow
&& m_colWindow
->IsShown() )
2699 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2701 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2702 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2704 if ( m_gridWin
&& m_gridWin
->IsShown() )
2705 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2708 // this is called when the grid table sends a message
2709 // to indicate that it has been redimensioned
2711 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2714 bool result
= false;
2716 // Clear the attribute cache as the attribute might refer to a different
2717 // cell than stored in the cache after adding/removing rows/columns.
2720 // By the same reasoning, the editor should be dismissed if columns are
2721 // added or removed. And for consistency, it should IMHO always be
2722 // removed, not only if the cell "underneath" it actually changes.
2723 // For now, I intentionally do not save the editor's content as the
2724 // cell it might want to save that stuff to might no longer exist.
2725 HideCellEditControl();
2727 switch ( msg
.GetId() )
2729 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2731 size_t pos
= msg
.GetCommandInt();
2732 int numRows
= msg
.GetCommandInt2();
2734 m_numRows
+= numRows
;
2736 if ( !m_rowHeights
.IsEmpty() )
2738 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2739 m_rowBottoms
.Insert( 0, pos
, numRows
);
2743 bottom
= m_rowBottoms
[pos
- 1];
2745 for ( i
= pos
; i
< m_numRows
; i
++ )
2747 bottom
+= m_rowHeights
[i
];
2748 m_rowBottoms
[i
] = bottom
;
2752 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2754 // if we have just inserted cols into an empty grid the current
2755 // cell will be undefined...
2757 SetCurrentCell( 0, 0 );
2761 m_selection
->UpdateRows( pos
, numRows
);
2762 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2764 attrProvider
->UpdateAttrRows( pos
, numRows
);
2766 if ( !GetBatchCount() )
2769 m_rowLabelWin
->Refresh();
2775 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2777 int numRows
= msg
.GetCommandInt();
2778 int oldNumRows
= m_numRows
;
2779 m_numRows
+= numRows
;
2781 if ( !m_rowHeights
.IsEmpty() )
2783 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2784 m_rowBottoms
.Add( 0, numRows
);
2787 if ( oldNumRows
> 0 )
2788 bottom
= m_rowBottoms
[oldNumRows
- 1];
2790 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2792 bottom
+= m_rowHeights
[i
];
2793 m_rowBottoms
[i
] = bottom
;
2797 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2799 // if we have just inserted cols into an empty grid the current
2800 // cell will be undefined...
2802 SetCurrentCell( 0, 0 );
2805 if ( !GetBatchCount() )
2808 m_rowLabelWin
->Refresh();
2814 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2816 size_t pos
= msg
.GetCommandInt();
2817 int numRows
= msg
.GetCommandInt2();
2818 m_numRows
-= numRows
;
2820 if ( !m_rowHeights
.IsEmpty() )
2822 m_rowHeights
.RemoveAt( pos
, numRows
);
2823 m_rowBottoms
.RemoveAt( pos
, numRows
);
2826 for ( i
= 0; i
< m_numRows
; i
++ )
2828 h
+= m_rowHeights
[i
];
2829 m_rowBottoms
[i
] = h
;
2835 m_currentCellCoords
= wxGridNoCellCoords
;
2839 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2840 m_currentCellCoords
.Set( 0, 0 );
2844 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2845 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2848 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2850 // ifdef'd out following patch from Paul Gammans
2852 // No need to touch column attributes, unless we
2853 // removed _all_ rows, in this case, we remove
2854 // all column attributes.
2855 // I hate to do this here, but the
2856 // needed data is not available inside UpdateAttrRows.
2857 if ( !GetNumberRows() )
2858 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2862 if ( !GetBatchCount() )
2865 m_rowLabelWin
->Refresh();
2871 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2873 size_t pos
= msg
.GetCommandInt();
2874 int numCols
= msg
.GetCommandInt2();
2875 m_numCols
+= numCols
;
2877 if ( m_useNativeHeader
)
2878 GetGridColHeader()->SetColumnCount(m_numCols
);
2880 if ( !m_colAt
.IsEmpty() )
2882 //Shift the column IDs
2883 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2885 if ( m_colAt
[i
] >= (int)pos
)
2886 m_colAt
[i
] += numCols
;
2889 m_colAt
.Insert( pos
, pos
, numCols
);
2891 //Set the new columns' positions
2892 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2898 if ( !m_colWidths
.IsEmpty() )
2900 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2901 m_colRights
.Insert( 0, pos
, numCols
);
2905 right
= m_colRights
[GetColAt( pos
- 1 )];
2908 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2910 i
= GetColAt( colPos
);
2912 right
+= m_colWidths
[i
];
2913 m_colRights
[i
] = right
;
2917 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2919 // if we have just inserted cols into an empty grid the current
2920 // cell will be undefined...
2922 SetCurrentCell( 0, 0 );
2926 m_selection
->UpdateCols( pos
, numCols
);
2927 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2929 attrProvider
->UpdateAttrCols( pos
, numCols
);
2930 if ( !GetBatchCount() )
2933 m_colWindow
->Refresh();
2939 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2941 int numCols
= msg
.GetCommandInt();
2942 int oldNumCols
= m_numCols
;
2943 m_numCols
+= numCols
;
2944 if ( m_useNativeHeader
)
2945 GetGridColHeader()->SetColumnCount(m_numCols
);
2947 if ( !m_colAt
.IsEmpty() )
2949 m_colAt
.Add( 0, numCols
);
2951 //Set the new columns' positions
2952 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2958 if ( !m_colWidths
.IsEmpty() )
2960 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2961 m_colRights
.Add( 0, numCols
);
2964 if ( oldNumCols
> 0 )
2965 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2968 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2970 i
= GetColAt( colPos
);
2972 right
+= m_colWidths
[i
];
2973 m_colRights
[i
] = right
;
2977 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2979 // if we have just inserted cols into an empty grid the current
2980 // cell will be undefined...
2982 SetCurrentCell( 0, 0 );
2984 if ( !GetBatchCount() )
2987 m_colWindow
->Refresh();
2993 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2995 size_t pos
= msg
.GetCommandInt();
2996 int numCols
= msg
.GetCommandInt2();
2997 m_numCols
-= numCols
;
2998 if ( m_useNativeHeader
)
2999 GetGridColHeader()->SetColumnCount(m_numCols
);
3001 if ( !m_colAt
.IsEmpty() )
3003 int colID
= GetColAt( pos
);
3005 m_colAt
.RemoveAt( pos
, numCols
);
3007 //Shift the column IDs
3009 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
3011 if ( m_colAt
[colPos
] > colID
)
3012 m_colAt
[colPos
] -= numCols
;
3016 if ( !m_colWidths
.IsEmpty() )
3018 m_colWidths
.RemoveAt( pos
, numCols
);
3019 m_colRights
.RemoveAt( pos
, numCols
);
3023 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
3025 i
= GetColAt( colPos
);
3027 w
+= m_colWidths
[i
];
3034 m_currentCellCoords
= wxGridNoCellCoords
;
3038 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3039 m_currentCellCoords
.Set( 0, 0 );
3043 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3044 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3047 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3049 // ifdef'd out following patch from Paul Gammans
3051 // No need to touch row attributes, unless we
3052 // removed _all_ columns, in this case, we remove
3053 // all row attributes.
3054 // I hate to do this here, but the
3055 // needed data is not available inside UpdateAttrCols.
3056 if ( !GetNumberCols() )
3057 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
3061 if ( !GetBatchCount() )
3064 m_colWindow
->Refresh();
3071 InvalidateBestSize();
3073 if (result
&& !GetBatchCount() )
3074 m_gridWin
->Refresh();
3079 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
3081 wxRegionIterator
iter( reg
);
3084 wxArrayInt rowlabels
;
3091 // TODO: remove this when we can...
3092 // There is a bug in wxMotif that gives garbage update
3093 // rectangles if you jump-scroll a long way by clicking the
3094 // scrollbar with middle button. This is a work-around
3096 #if defined(__WXMOTIF__)
3098 m_gridWin
->GetClientSize( &cw
, &ch
);
3099 if ( r
.GetTop() > ch
)
3101 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3104 // logical bounds of update region
3107 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3108 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3110 // find the row labels within these bounds
3113 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
3115 if ( GetRowBottom(row
) < top
)
3118 if ( GetRowTop(row
) > bottom
)
3121 rowlabels
.Add( row
);
3130 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
3132 wxRegionIterator
iter( reg
);
3135 wxArrayInt colLabels
;
3142 // TODO: remove this when we can...
3143 // There is a bug in wxMotif that gives garbage update
3144 // rectangles if you jump-scroll a long way by clicking the
3145 // scrollbar with middle button. This is a work-around
3147 #if defined(__WXMOTIF__)
3149 m_gridWin
->GetClientSize( &cw
, &ch
);
3150 if ( r
.GetLeft() > cw
)
3152 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3155 // logical bounds of update region
3158 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3159 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3161 // find the cells within these bounds
3165 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
3167 col
= GetColAt( colPos
);
3169 if ( GetColRight(col
) < left
)
3172 if ( GetColLeft(col
) > right
)
3175 colLabels
.Add( col
);
3184 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
3188 wxGridCellCoordsArray cellsExposed
;
3190 int left
, top
, right
, bottom
;
3191 for ( wxRegionIterator
iter(reg
); iter
; ++iter
)
3195 // Skip 0-height cells, they're invisible anyhow, don't waste time
3196 // getting their rectangles and so on.
3197 if ( !r
.GetHeight() )
3200 // TODO: remove this when we can...
3201 // There is a bug in wxMotif that gives garbage update
3202 // rectangles if you jump-scroll a long way by clicking the
3203 // scrollbar with middle button. This is a work-around
3205 #if defined(__WXMOTIF__)
3207 m_gridWin
->GetClientSize( &cw
, &ch
);
3208 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3209 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3210 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3211 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3214 // logical bounds of update region
3216 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3217 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3219 // find the cells within these bounds
3221 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
3223 if ( GetRowBottom(row
) <= top
)
3226 if ( GetRowTop(row
) > bottom
)
3229 // add all dirty cells in this row: notice that the columns which
3230 // are dirty don't depend on the row so we compute them only once
3231 // for the first dirty row and then reuse for all the next ones
3234 // do determine the dirty columns
3235 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
3236 cols
.push_back(GetColAt(pos
));
3238 // if there are no dirty columns at all, nothing to do
3243 const size_t count
= cols
.size();
3244 for ( size_t n
= 0; n
< count
; n
++ )
3245 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
3249 return cellsExposed
;
3253 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3256 wxPoint
pos( event
.GetPosition() );
3257 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3259 if ( event
.Dragging() )
3263 m_isDragging
= true;
3264 m_rowLabelWin
->CaptureMouse();
3267 if ( event
.LeftIsDown() )
3269 switch ( m_cursorMode
)
3271 case WXGRID_CURSOR_RESIZE_ROW
:
3273 int cw
, ch
, left
, dummy
;
3274 m_gridWin
->GetClientSize( &cw
, &ch
);
3275 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3277 wxClientDC
dc( m_gridWin
);
3280 GetRowTop(m_dragRowOrCol
) +
3281 GetRowMinimalHeight(m_dragRowOrCol
) );
3282 dc
.SetLogicalFunction(wxINVERT
);
3283 if ( m_dragLastPos
>= 0 )
3285 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3287 dc
.DrawLine( left
, y
, left
+cw
, y
);
3292 case WXGRID_CURSOR_SELECT_ROW
:
3294 if ( (row
= YToRow( y
)) >= 0 )
3297 m_selection
->SelectRow(row
, event
);
3302 // default label to suppress warnings about "enumeration value
3303 // 'xxx' not handled in switch
3311 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3316 if (m_rowLabelWin
->HasCapture())
3317 m_rowLabelWin
->ReleaseMouse();
3318 m_isDragging
= false;
3321 // ------------ Entering or leaving the window
3323 if ( event
.Entering() || event
.Leaving() )
3325 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3328 // ------------ Left button pressed
3330 else if ( event
.LeftDown() )
3332 row
= YToEdgeOfRow(y
);
3333 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3335 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3337 else // not a request to start resizing
3341 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3343 if ( !event
.ShiftDown() && !event
.CmdDown() )
3347 if ( event
.ShiftDown() )
3349 m_selection
->SelectBlock
3351 m_currentCellCoords
.GetRow(), 0,
3352 row
, GetNumberCols() - 1,
3358 m_selection
->SelectRow(row
, event
);
3362 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3367 // ------------ Left double click
3369 else if (event
.LeftDClick() )
3371 row
= YToEdgeOfRow(y
);
3372 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3374 // adjust row height depending on label text
3376 // TODO: generate RESIZING event, see #10754
3377 AutoSizeRowLabelSize( row
);
3379 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3381 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3384 else // not on row separator or it's not resizable
3388 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3390 // no default action at the moment
3395 // ------------ Left button released
3397 else if ( event
.LeftUp() )
3399 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3400 DoEndDragResizeRow(event
);
3402 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3406 // ------------ Right button down
3408 else if ( event
.RightDown() )
3412 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3414 // no default action at the moment
3418 // ------------ Right double click
3420 else if ( event
.RightDClick() )
3424 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3426 // no default action at the moment
3430 // ------------ No buttons down and mouse moving
3432 else if ( event
.Moving() )
3434 m_dragRowOrCol
= YToEdgeOfRow( y
);
3435 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3437 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3439 if ( CanDragRowSize(m_dragRowOrCol
) )
3440 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3443 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3445 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3450 void wxGrid::UpdateColumnSortingIndicator(int col
)
3452 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3454 if ( m_useNativeHeader
)
3455 GetGridColHeader()->UpdateColumn(col
);
3456 else if ( m_nativeColumnLabels
)
3457 m_colWindow
->Refresh();
3458 //else: sorting indicator display not yet implemented in grid version
3461 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3463 if ( col
== m_sortCol
)
3465 // we are already using this column for sorting (or not sorting at all)
3466 // but we might still change the sorting order, check for it
3467 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3469 m_sortIsAscending
= ascending
;
3471 UpdateColumnSortingIndicator(m_sortCol
);
3474 else // we're changing the column used for sorting
3476 const int sortColOld
= m_sortCol
;
3478 // change it before updating the column as we want GetSortingColumn()
3479 // to return the correct new value
3482 if ( sortColOld
!= wxNOT_FOUND
)
3483 UpdateColumnSortingIndicator(sortColOld
);
3485 if ( m_sortCol
!= wxNOT_FOUND
)
3487 m_sortIsAscending
= ascending
;
3488 UpdateColumnSortingIndicator(m_sortCol
);
3493 void wxGrid::DoColHeaderClick(int col
)
3495 // we consider that the grid was resorted if this event is processed and
3497 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3499 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3504 void wxGrid::DoStartResizeCol(int col
)
3506 m_dragRowOrCol
= col
;
3508 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3511 void wxGrid::DoUpdateResizeCol(int x
)
3513 int cw
, ch
, dummy
, top
;
3514 m_gridWin
->GetClientSize( &cw
, &ch
);
3515 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3517 wxClientDC
dc( m_gridWin
);
3520 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3521 dc
.SetLogicalFunction(wxINVERT
);
3522 if ( m_dragLastPos
>= 0 )
3524 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3526 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3530 void wxGrid::DoUpdateResizeColWidth(int w
)
3532 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3535 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3538 CalcUnscrolledPosition( event
.GetPosition().x
, 0, &x
, NULL
);
3540 int col
= XToCol(x
);
3541 if ( event
.Dragging() )
3545 m_isDragging
= true;
3546 GetColLabelWindow()->CaptureMouse();
3548 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3549 DoStartMoveCol(col
);
3552 if ( event
.LeftIsDown() )
3554 switch ( m_cursorMode
)
3556 case WXGRID_CURSOR_RESIZE_COL
:
3557 DoUpdateResizeCol(x
);
3560 case WXGRID_CURSOR_SELECT_COL
:
3565 m_selection
->SelectCol(col
, event
);
3570 case WXGRID_CURSOR_MOVE_COL
:
3572 int posNew
= XToPos(x
);
3573 int colNew
= GetColAt(posNew
);
3575 // determine the position of the drop marker
3577 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3578 markerX
= GetColRight(colNew
);
3580 markerX
= GetColLeft(colNew
);
3582 if ( markerX
!= m_dragLastPos
)
3584 wxClientDC
dc( GetColLabelWindow() );
3588 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3592 //Clean up the last indicator
3593 if ( m_dragLastPos
>= 0 )
3595 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3597 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3598 dc
.SetPen(wxNullPen
);
3600 if ( XToCol( m_dragLastPos
) != -1 )
3601 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3604 const wxColour
*color
;
3605 //Moving to the same place? Don't draw a marker
3606 if ( colNew
== m_dragRowOrCol
)
3607 color
= wxLIGHT_GREY
;
3612 wxPen
pen( *color
, 2 );
3615 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3617 dc
.SetPen(wxNullPen
);
3619 m_dragLastPos
= markerX
- 1;
3624 // default label to suppress warnings about "enumeration value
3625 // 'xxx' not handled in switch
3633 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3638 if (GetColLabelWindow()->HasCapture())
3639 GetColLabelWindow()->ReleaseMouse();
3640 m_isDragging
= false;
3643 // ------------ Entering or leaving the window
3645 if ( event
.Entering() || event
.Leaving() )
3647 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3650 // ------------ Left button pressed
3652 else if ( event
.LeftDown() )
3654 int colEdge
= XToEdgeOfCol(x
);
3655 if ( colEdge
!= wxNOT_FOUND
&& CanDragColSize(colEdge
) )
3657 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3659 else // not a request to start resizing
3662 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3664 if ( m_canDragColMove
)
3666 //Show button as pressed
3667 wxClientDC
dc( GetColLabelWindow() );
3668 int colLeft
= GetColLeft( col
);
3669 int colRight
= GetColRight( col
) - 1;
3670 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3671 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3672 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3674 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3678 if ( !event
.ShiftDown() && !event
.CmdDown() )
3682 if ( event
.ShiftDown() )
3684 m_selection
->SelectBlock
3686 0, m_currentCellCoords
.GetCol(),
3687 GetNumberRows() - 1, col
,
3693 m_selection
->SelectCol(col
, event
);
3697 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3703 // ------------ Left double click
3705 if ( event
.LeftDClick() )
3707 const int colEdge
= XToEdgeOfCol(x
);
3708 if ( colEdge
== -1 )
3711 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3713 // no default action at the moment
3718 // adjust column width depending on label text
3720 // TODO: generate RESIZING event, see #10754
3721 AutoSizeColLabelSize( colEdge
);
3723 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3725 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3730 // ------------ Left button released
3732 else if ( event
.LeftUp() )
3734 switch ( m_cursorMode
)
3736 case WXGRID_CURSOR_RESIZE_COL
:
3737 DoEndDragResizeCol(event
);
3740 case WXGRID_CURSOR_MOVE_COL
:
3741 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3743 // the column didn't actually move anywhere
3745 DoColHeaderClick(col
);
3746 m_colWindow
->Refresh(); // "unpress" the column
3750 // get the position of the column we're over
3751 int pos
= XToPos(x
);
3753 // we may need to adjust the drop position but don't bother
3754 // checking for it if we can't anyhow
3757 // also find the index of the column we're over: notice
3758 // that the existing "col" variable may be invalid but
3759 // we need a valid one here
3760 const int colValid
= GetColAt(pos
);
3762 // if we're on the "near" (usually left but right in
3763 // RTL case) part of the column, the actual position we
3764 // should be placed in is actually the one before it
3766 const int middle
= GetColLeft(colValid
) +
3767 GetColWidth(colValid
)/2;
3768 if ( GetLayoutDirection() == wxLayout_LeftToRight
)
3769 onNearPart
= (x
<= middle
);
3770 else // wxLayout_RightToLeft
3771 onNearPart
= (x
> middle
);
3781 case WXGRID_CURSOR_SELECT_COL
:
3782 case WXGRID_CURSOR_SELECT_CELL
:
3783 case WXGRID_CURSOR_RESIZE_ROW
:
3784 case WXGRID_CURSOR_SELECT_ROW
:
3786 DoColHeaderClick(col
);
3790 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3794 // ------------ Right button down
3796 else if ( event
.RightDown() )
3799 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3801 // no default action at the moment
3805 // ------------ Right double click
3807 else if ( event
.RightDClick() )
3810 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3812 // no default action at the moment
3816 // ------------ No buttons down and mouse moving
3818 else if ( event
.Moving() )
3820 m_dragRowOrCol
= XToEdgeOfCol( x
);
3821 if ( m_dragRowOrCol
>= 0 )
3823 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3825 if ( CanDragColSize(m_dragRowOrCol
) )
3826 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3829 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3831 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3836 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3838 if ( event
.LeftDown() )
3840 // indicate corner label by having both row and
3843 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3848 else if ( event
.LeftDClick() )
3850 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3852 else if ( event
.RightDown() )
3854 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3856 // no default action at the moment
3859 else if ( event
.RightDClick() )
3861 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3863 // no default action at the moment
3868 void wxGrid::CancelMouseCapture()
3870 // cancel operation currently in progress, whatever it is
3873 m_isDragging
= false;
3874 m_startDragPos
= wxDefaultPosition
;
3876 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3877 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3878 m_winCapture
= NULL
;
3880 // remove traces of whatever we drew on screen
3885 void wxGrid::ChangeCursorMode(CursorMode mode
,
3890 static const wxChar
*const cursorModes
[] =
3900 wxLogTrace(wxT("grid"),
3901 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3902 win
== m_colWindow
? wxT("colLabelWin")
3903 : win
? wxT("rowLabelWin")
3905 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3906 #endif // wxUSE_LOG_TRACE
3908 if ( mode
== m_cursorMode
&&
3909 win
== m_winCapture
&&
3910 captureMouse
== (m_winCapture
!= NULL
))
3915 // by default use the grid itself
3921 m_winCapture
->ReleaseMouse();
3922 m_winCapture
= NULL
;
3925 m_cursorMode
= mode
;
3927 switch ( m_cursorMode
)
3929 case WXGRID_CURSOR_RESIZE_ROW
:
3930 win
->SetCursor( m_rowResizeCursor
);
3933 case WXGRID_CURSOR_RESIZE_COL
:
3934 win
->SetCursor( m_colResizeCursor
);
3937 case WXGRID_CURSOR_MOVE_COL
:
3938 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3942 win
->SetCursor( *wxSTANDARD_CURSOR
);
3946 // we need to capture mouse when resizing
3947 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3948 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3950 if ( captureMouse
&& resize
)
3952 win
->CaptureMouse();
3957 // ----------------------------------------------------------------------------
3958 // grid mouse event processing
3959 // ----------------------------------------------------------------------------
3962 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3963 const wxGridCellCoords
& coords
,
3966 bool performDefault
= true ;
3968 if ( coords
== wxGridNoCellCoords
)
3969 return performDefault
; // we're outside any valid cell
3971 // Hide the edit control, so it won't interfere with drag-shrinking.
3972 if ( IsCellEditControlShown() )
3974 HideCellEditControl();
3975 SaveEditControlValue();
3978 switch ( event
.GetModifiers() )
3981 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3982 m_selectedBlockCorner
= coords
;
3983 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3987 if ( CanDragCell() )
3991 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3992 m_selectedBlockCorner
= coords
;
3994 // if event is handled by user code, no further processing
3995 if ( SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
) != 0 )
3996 performDefault
= false;
3998 return performDefault
;
4002 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
4006 // we don't handle the other key modifiers
4010 return performDefault
;
4013 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
4015 wxClientDC
dc(m_gridWin
);
4017 dc
.SetLogicalFunction(wxINVERT
);
4019 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
4020 m_gridWin
->GetClientSize());
4022 // erase the previously drawn line, if any
4023 if ( m_dragLastPos
>= 0 )
4024 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
4026 // we need the vertical position for rows and horizontal for columns here
4027 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
4029 // don't allow resizing beneath the minimal size
4030 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
4031 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
4032 if ( m_dragLastPos
< posMin
)
4033 m_dragLastPos
= posMin
;
4035 // and draw it at the new position
4036 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
4039 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
4041 if ( !m_isDragging
)
4043 // Don't start doing anything until the mouse has been dragged far
4045 const wxPoint
& pt
= event
.GetPosition();
4046 if ( m_startDragPos
== wxDefaultPosition
)
4048 m_startDragPos
= pt
;
4052 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
4053 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
4057 const bool isFirstDrag
= !m_isDragging
;
4058 m_isDragging
= true;
4060 switch ( m_cursorMode
)
4062 case WXGRID_CURSOR_SELECT_CELL
:
4063 // no further handling if handled by user
4064 if ( DoGridCellDrag(event
, coords
, isFirstDrag
) == false )
4068 case WXGRID_CURSOR_RESIZE_ROW
:
4069 DoGridLineDrag(event
, wxGridRowOperations());
4072 case WXGRID_CURSOR_RESIZE_COL
:
4073 DoGridLineDrag(event
, wxGridColumnOperations());
4082 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
4084 m_winCapture
= m_gridWin
;
4085 m_winCapture
->CaptureMouse();
4090 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
4091 const wxGridCellCoords
& coords
,
4094 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
4096 // event handled by user code, no need to do anything here
4100 if ( !event
.CmdDown() )
4103 if ( event
.ShiftDown() )
4107 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
4108 m_selectedBlockCorner
= coords
;
4111 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
4113 DisableCellEditControl();
4114 MakeCellVisible( coords
);
4116 if ( event
.CmdDown() )
4120 m_selection
->ToggleCellSelection(coords
, event
);
4123 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4124 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4125 m_selectedBlockCorner
= coords
;
4131 // In row or column selection mode just clicking on the cell
4132 // should select the row or column containing it: this is more
4133 // convenient for the kinds of controls that use such selection
4134 // mode and is compatible with 2.8 behaviour (see #12062).
4135 switch ( m_selection
->GetSelectionMode() )
4137 case wxGridSelectCells
:
4138 case wxGridSelectRowsOrColumns
:
4139 // nothing to do in these cases
4142 case wxGridSelectRows
:
4143 m_selection
->SelectRow(coords
.GetRow());
4146 case wxGridSelectColumns
:
4147 m_selection
->SelectCol(coords
.GetCol());
4152 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
4153 coords
!= wxGridNoCellCoords
;
4154 SetCurrentCell( coords
);
4160 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
4161 const wxGridCellCoords
& coords
,
4164 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
4166 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
4168 // we want double click to select a cell and start editing
4169 // (i.e. to behave in same way as sequence of two slow clicks):
4170 m_waitForSlowClick
= true;
4176 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
4178 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4182 m_winCapture
->ReleaseMouse();
4183 m_winCapture
= NULL
;
4186 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
4189 EnableCellEditControl();
4191 wxGridCellAttr
*attr
= GetCellAttr(coords
);
4192 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
4193 editor
->StartingClick();
4197 m_waitForSlowClick
= false;
4199 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4200 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4204 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
4205 m_selectedBlockBottomRight
,
4209 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4210 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4212 // Show the edit control, if it has been hidden for
4214 ShowCellEditControl();
4217 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4219 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4220 DoEndDragResizeRow(event
);
4222 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4224 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4225 DoEndDragResizeCol(event
);
4232 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
4233 const wxGridCellCoords
& coords
,
4236 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
4238 // out of grid cell area
4239 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4243 int dragRow
= YToEdgeOfRow( pos
.y
);
4244 int dragCol
= XToEdgeOfCol( pos
.x
);
4246 // Dragging on the corner of a cell to resize in both
4247 // directions is not implemented yet...
4249 if ( dragRow
>= 0 && dragCol
>= 0 )
4251 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4255 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
4257 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4259 m_dragRowOrCol
= dragRow
;
4260 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
4263 // When using the native header window we can only resize the columns by
4264 // dragging the dividers in it because we can't make it enter into the
4265 // column resizing mode programmatically
4266 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
4267 CanDragGridSize() && CanDragColSize(dragCol
) )
4269 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4271 m_dragRowOrCol
= dragCol
;
4272 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
4275 else // Neither on a row or col edge
4277 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4279 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4284 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
4286 if ( event
.Entering() || event
.Leaving() )
4288 // we don't care about these events but we must not reset m_isDragging
4289 // if they happen so return before anything else is done
4294 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
4296 // coordinates of the cell under mouse
4297 wxGridCellCoords coords
= XYToCell(pos
);
4299 int cell_rows
, cell_cols
;
4300 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4301 if ( (cell_rows
< 0) || (cell_cols
< 0) )
4303 coords
.SetRow(coords
.GetRow() + cell_rows
);
4304 coords
.SetCol(coords
.GetCol() + cell_cols
);
4307 if ( event
.Dragging() )
4309 if ( event
.LeftIsDown() )
4310 DoGridDragEvent(event
, coords
);
4316 m_isDragging
= false;
4317 m_startDragPos
= wxDefaultPosition
;
4319 // deal with various button presses
4320 if ( event
.IsButton() )
4322 if ( coords
!= wxGridNoCellCoords
)
4324 DisableCellEditControl();
4326 if ( event
.LeftDown() )
4327 DoGridCellLeftDown(event
, coords
, pos
);
4328 else if ( event
.LeftDClick() )
4329 DoGridCellLeftDClick(event
, coords
, pos
);
4330 else if ( event
.RightDown() )
4331 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
4332 else if ( event
.RightDClick() )
4333 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
4336 // this one should be called even if we're not over any cell
4337 if ( event
.LeftUp() )
4339 DoGridCellLeftUp(event
, coords
);
4342 else if ( event
.Moving() )
4344 DoGridMouseMoveEvent(event
, coords
, pos
);
4346 else // unknown mouse event?
4352 // this function returns true only if the size really changed
4353 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4355 if ( m_dragLastPos
== -1 )
4358 const wxGridOperations
& doper
= oper
.Dual();
4360 const wxSize size
= m_gridWin
->GetClientSize();
4362 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4364 // erase the last line we drew
4365 wxClientDC
dc(m_gridWin
);
4367 dc
.SetLogicalFunction(wxINVERT
);
4369 const int posLineStart
= oper
.Select(ptOrigin
);
4370 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4372 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4374 // temporarily hide the edit control before resizing
4375 HideCellEditControl();
4376 SaveEditControlValue();
4378 // do resize the line
4379 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4380 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4381 oper
.SetLineSize(this, m_dragRowOrCol
,
4382 wxMax(m_dragLastPos
- lineStart
,
4383 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4385 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4389 // refresh now if we're not frozen
4390 if ( !GetBatchCount() )
4392 // we need to refresh everything beyond the resized line in the header
4395 // get the position from which to refresh in the other direction
4396 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4397 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4399 // we only need the ordinate (for rows) or abscissa (for columns) here,
4400 // and need to cover the entire window in the other direction
4401 oper
.Select(rect
) = 0;
4403 wxRect
rectHeader(rect
.GetPosition(),
4406 oper
.GetHeaderWindowSize(this),
4407 doper
.Select(size
) - doper
.Select(rect
)
4410 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4413 // also refresh the grid window: extend the rectangle
4416 oper
.SelectSize(rect
) = oper
.Select(size
);
4418 int subtractLines
= 0;
4419 int line
= doper
.PosToLine(this, posLineStart
);
4422 // ensure that if we have a multi-cell block we redraw all of
4423 // it by increasing the refresh area to cover it entirely if a
4424 // part of it is affected
4425 const int lineEnd
= doper
.PosToLine(this, posLineEnd
, true);
4426 for ( ; line
< lineEnd
; line
++ )
4428 int cellLines
= oper
.Select(
4429 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4430 if ( cellLines
< subtractLines
)
4431 subtractLines
= cellLines
;
4436 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4437 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4439 doper
.Select(rect
) = startPos
;
4440 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4442 m_gridWin
->Refresh(false, &rect
);
4446 // show the edit control back again
4447 ShowCellEditControl();
4452 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4454 // TODO: generate RESIZING event, see #10754
4456 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4457 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4460 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4462 // TODO: generate RESIZING event, see #10754
4464 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4465 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4468 void wxGrid::DoStartMoveCol(int col
)
4470 m_dragRowOrCol
= col
;
4473 void wxGrid::DoEndMoveCol(int pos
)
4475 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4477 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4478 SetColPos(m_dragRowOrCol
, pos
);
4479 //else: vetoed by user
4481 m_dragRowOrCol
= -1;
4484 void wxGrid::RefreshAfterColPosChange()
4486 // recalculate the column rights as the column positions have changed,
4487 // unless we calculate them dynamically because all columns widths are the
4488 // same and it's easy to do
4489 if ( !m_colWidths
.empty() )
4492 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4494 int colID
= GetColAt( colPos
);
4496 colRight
+= m_colWidths
[colID
];
4497 m_colRights
[colID
] = colRight
;
4501 // and make the changes visible
4502 if ( m_useNativeHeader
)
4504 if ( m_colAt
.empty() )
4505 GetGridColHeader()->ResetColumnsOrder();
4507 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4511 m_colWindow
->Refresh();
4513 m_gridWin
->Refresh();
4516 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4520 RefreshAfterColPosChange();
4523 void wxGrid::SetColPos(int idx
, int pos
)
4525 // we're going to need m_colAt now, initialize it if needed
4526 if ( m_colAt
.empty() )
4528 m_colAt
.reserve(m_numCols
);
4529 for ( int i
= 0; i
< m_numCols
; i
++ )
4530 m_colAt
.push_back(i
);
4533 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4535 RefreshAfterColPosChange();
4538 void wxGrid::ResetColPos()
4542 RefreshAfterColPosChange();
4545 void wxGrid::EnableDragColMove( bool enable
)
4547 if ( m_canDragColMove
== enable
)
4550 if ( m_useNativeHeader
)
4552 // update all columns to make them [not] reorderable
4553 GetGridColHeader()->SetColumnCount(m_numCols
);
4556 m_canDragColMove
= enable
;
4558 // we use to call ResetColPos() from here if !enable but this doesn't seem
4559 // right as it would mean there would be no way to "freeze" the current
4560 // columns order by disabling moving them after putting them in the desired
4561 // order, whereas now you can always call ResetColPos() manually if needed
4566 // ------ interaction with data model
4568 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4570 switch ( msg
.GetId() )
4572 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4573 return GetModelValues();
4575 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4576 return SetModelValues();
4578 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4579 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4580 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4581 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4582 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4583 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4584 return Redimension( msg
);
4591 // The behaviour of this function depends on the grid table class
4592 // Clear() function. For the default wxGridStringTable class the
4593 // behaviour is to replace all cell contents with wxEmptyString but
4594 // not to change the number of rows or cols.
4596 void wxGrid::ClearGrid()
4600 if (IsCellEditControlEnabled())
4601 DisableCellEditControl();
4604 if (!GetBatchCount())
4605 m_gridWin
->Refresh();
4610 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4611 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4613 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4618 if ( IsCellEditControlEnabled() )
4619 DisableCellEditControl();
4621 return (m_table
->*funcModify
)(pos
, num
);
4623 // the table will have sent the results of the insert row
4624 // operation to this view object as a grid table message
4628 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4629 int num
, bool WXUNUSED(updateLabels
))
4631 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4636 return (m_table
->*funcAppend
)(num
);
4639 // ----------------------------------------------------------------------------
4640 // event generation helpers
4641 // ----------------------------------------------------------------------------
4644 wxGrid::SendGridSizeEvent(wxEventType type
,
4646 const wxMouseEvent
& mouseEv
)
4648 int rowOrCol
= row
== -1 ? col
: row
;
4650 wxGridSizeEvent
gridEvt( GetId(),
4654 mouseEv
.GetX() + GetRowLabelSize(),
4655 mouseEv
.GetY() + GetColLabelSize(),
4658 GetEventHandler()->ProcessEvent(gridEvt
);
4661 // Generate a grid event based on a mouse event and return:
4662 // -1 if the event was vetoed
4663 // +1 if the event was processed (but not vetoed)
4664 // 0 if the event wasn't handled
4666 wxGrid::SendEvent(const wxEventType type
,
4668 const wxMouseEvent
& mouseEv
)
4670 bool claimed
, vetoed
;
4672 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4674 // Right now, it should _never_ end up here!
4675 wxGridRangeSelectEvent
gridEvt( GetId(),
4678 m_selectedBlockTopLeft
,
4679 m_selectedBlockBottomRight
,
4683 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4684 vetoed
= !gridEvt
.IsAllowed();
4686 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4687 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4688 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4689 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4691 wxPoint pos
= mouseEv
.GetPosition();
4693 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4694 pos
.y
+= GetColLabelSize();
4695 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4696 pos
.x
+= GetRowLabelSize();
4698 wxGridEvent
gridEvt( GetId(),
4706 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4707 vetoed
= !gridEvt
.IsAllowed();
4711 wxGridEvent
gridEvt( GetId(),
4715 mouseEv
.GetX() + GetRowLabelSize(),
4716 mouseEv
.GetY() + GetColLabelSize(),
4720 if ( type
== wxEVT_GRID_CELL_BEGIN_DRAG
)
4722 // by default the dragging is not supported, the user code must
4723 // explicitly allow the event for it to take place
4727 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4728 vetoed
= !gridEvt
.IsAllowed();
4731 // A Veto'd event may not be `claimed' so test this first
4735 return claimed
? 1 : 0;
4738 // Generate a grid event of specified type, return value same as above
4741 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4743 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4744 gridEvt
.SetString(s
);
4746 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4748 // A Veto'd event may not be `claimed' so test this first
4749 if ( !gridEvt
.IsAllowed() )
4752 return claimed
? 1 : 0;
4755 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4757 // needed to prevent zillions of paint events on MSW
4761 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4763 // Don't do anything if between Begin/EndBatch...
4764 // EndBatch() will do all this on the last nested one anyway.
4765 if ( m_created
&& !GetBatchCount() )
4767 // Refresh to get correct scrolled position:
4768 wxScrolledWindow::Refresh(eraseb
, rect
);
4772 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4773 int width_label
, width_cell
, height_label
, height_cell
;
4776 // Copy rectangle can get scroll offsets..
4777 rect_x
= rect
->GetX();
4778 rect_y
= rect
->GetY();
4779 rectWidth
= rect
->GetWidth();
4780 rectHeight
= rect
->GetHeight();
4782 width_label
= m_rowLabelWidth
- rect_x
;
4783 if (width_label
> rectWidth
)
4784 width_label
= rectWidth
;
4786 height_label
= m_colLabelHeight
- rect_y
;
4787 if (height_label
> rectHeight
)
4788 height_label
= rectHeight
;
4790 if (rect_x
> m_rowLabelWidth
)
4792 x
= rect_x
- m_rowLabelWidth
;
4793 width_cell
= rectWidth
;
4798 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4801 if (rect_y
> m_colLabelHeight
)
4803 y
= rect_y
- m_colLabelHeight
;
4804 height_cell
= rectHeight
;
4809 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4812 // Paint corner label part intersecting rect.
4813 if ( width_label
> 0 && height_label
> 0 )
4815 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4816 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4819 // Paint col labels part intersecting rect.
4820 if ( width_cell
> 0 && height_label
> 0 )
4822 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4823 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4826 // Paint row labels part intersecting rect.
4827 if ( width_label
> 0 && height_cell
> 0 )
4829 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4830 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4833 // Paint cell area part intersecting rect.
4834 if ( width_cell
> 0 && height_cell
> 0 )
4836 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4837 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4842 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4843 m_colWindow
->Refresh(eraseb
, NULL
);
4844 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4845 m_gridWin
->Refresh(eraseb
, NULL
);
4850 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4852 if (m_targetWindow
!= this) // check whether initialisation has been done
4854 // reposition our children windows
4859 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4861 if ( m_inOnKeyDown
)
4863 // shouldn't be here - we are going round in circles...
4865 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4868 m_inOnKeyDown
= true;
4870 // propagate the event up and see if it gets processed
4871 wxWindow
*parent
= GetParent();
4872 wxKeyEvent
keyEvt( event
);
4873 keyEvt
.SetEventObject( parent
);
4875 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4877 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4879 if (event
.GetKeyCode() == WXK_RIGHT
)
4880 event
.m_keyCode
= WXK_LEFT
;
4881 else if (event
.GetKeyCode() == WXK_LEFT
)
4882 event
.m_keyCode
= WXK_RIGHT
;
4885 // try local handlers
4886 switch ( event
.GetKeyCode() )
4889 if ( event
.ControlDown() )
4890 MoveCursorUpBlock( event
.ShiftDown() );
4892 MoveCursorUp( event
.ShiftDown() );
4896 if ( event
.ControlDown() )
4897 MoveCursorDownBlock( event
.ShiftDown() );
4899 MoveCursorDown( event
.ShiftDown() );
4903 if ( event
.ControlDown() )
4904 MoveCursorLeftBlock( event
.ShiftDown() );
4906 MoveCursorLeft( event
.ShiftDown() );
4910 if ( event
.ControlDown() )
4911 MoveCursorRightBlock( event
.ShiftDown() );
4913 MoveCursorRight( event
.ShiftDown() );
4917 case WXK_NUMPAD_ENTER
:
4918 if ( event
.ControlDown() )
4920 event
.Skip(); // to let the edit control have the return
4924 if ( GetGridCursorRow() < GetNumberRows()-1 )
4926 MoveCursorDown( event
.ShiftDown() );
4930 // at the bottom of a column
4931 DisableCellEditControl();
4942 // send an event to the grid's parents for custom handling
4943 wxGridEvent
gridEvt(GetId(), wxEVT_GRID_TABBING
, this,
4944 GetGridCursorRow(), GetGridCursorCol(),
4945 -1, -1, false, event
);
4946 if ( ProcessWindowEvent(gridEvt
) )
4948 // the event has been handled so no need for more processing
4952 DoGridProcessTab( event
);
4956 GoToCell(event
.ControlDown() ? 0
4957 : m_currentCellCoords
.GetRow(),
4962 GoToCell(event
.ControlDown() ? m_numRows
- 1
4963 : m_currentCellCoords
.GetRow(),
4976 // Ctrl-Space selects the current column, Shift-Space -- the
4977 // current row and Ctrl-Shift-Space -- everything
4978 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4981 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4985 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4988 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4989 m_selection
->SelectBlock(0, 0,
4990 m_numRows
- 1, m_numCols
- 1);
4994 if ( !IsEditable() )
4996 MoveCursorRight(false);
4999 //else: fall through
5012 m_inOnKeyDown
= false;
5015 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5017 // try local handlers
5019 if ( event
.GetKeyCode() == WXK_SHIFT
)
5021 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
5022 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
5026 m_selection
->SelectBlock(
5027 m_selectedBlockTopLeft
,
5028 m_selectedBlockBottomRight
,
5033 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
5034 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
5035 m_selectedBlockCorner
= wxGridNoCellCoords
;
5039 void wxGrid::OnChar( wxKeyEvent
& event
)
5041 // is it possible to edit the current cell at all?
5042 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5044 // yes, now check whether the cells editor accepts the key
5045 int row
= m_currentCellCoords
.GetRow();
5046 int col
= m_currentCellCoords
.GetCol();
5047 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5048 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5050 // <F2> is special and will always start editing, for
5051 // other keys - ask the editor itself
5052 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
5053 || editor
->IsAcceptedKey(event
) )
5055 // ensure cell is visble
5056 MakeCellVisible(row
, col
);
5057 EnableCellEditControl();
5059 // a problem can arise if the cell is not completely
5060 // visible (even after calling MakeCellVisible the
5061 // control is not created and calling StartingKey will
5063 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
5064 editor
->StartingKey(event
);
5080 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5084 void wxGrid::DoGridProcessTab(wxKeyboardState
& kbdState
)
5086 const bool isForwardTab
= !kbdState
.ShiftDown();
5088 // TAB processing only changes when we are at the borders of the grid, so
5089 // let's first handle the common behaviour when we are not at the border.
5092 if ( GetGridCursorCol() < GetNumberCols() - 1 )
5094 MoveCursorRight( false );
5100 if ( GetGridCursorCol() )
5102 MoveCursorLeft( false );
5108 // We only get here if the cursor is at the border of the grid, apply the
5109 // configured behaviour.
5110 switch ( m_tabBehaviour
)
5113 // Nothing special to do, we remain at the current cell.
5117 // Go to the beginning of the next or the end of the previous row.
5120 if ( GetGridCursorRow() < GetNumberRows() - 1 )
5122 GoToCell( GetGridCursorRow() + 1, 0 );
5128 if ( GetGridCursorRow() > 0 )
5130 GoToCell( GetGridCursorRow() - 1, GetNumberCols() - 1 );
5137 if ( Navigate( isForwardTab
? wxNavigationKeyEvent::IsForward
5138 : wxNavigationKeyEvent::IsBackward
) )
5143 // If we remain in this cell, stop editing it if we were doing so.
5144 DisableCellEditControl();
5147 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5149 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
5151 // the event has been vetoed - do nothing
5155 #if !defined(__WXMAC__)
5156 wxClientDC
dc( m_gridWin
);
5160 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5162 DisableCellEditControl();
5164 if ( IsVisible( m_currentCellCoords
, false ) )
5167 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
5168 if ( !m_gridLinesEnabled
)
5176 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5178 // Otherwise refresh redraws the highlight!
5179 m_currentCellCoords
= coords
;
5181 #if defined(__WXMAC__)
5182 m_gridWin
->Refresh(true /*, & r */);
5184 DrawGridCellArea( dc
, cells
);
5185 DrawAllGridLines( dc
, r
);
5190 m_currentCellCoords
= coords
;
5192 wxGridCellAttr
*attr
= GetCellAttr( coords
);
5193 #if !defined(__WXMAC__)
5194 DrawCellHighlight( dc
, attr
);
5202 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
5203 int bottomRow
, int rightCol
)
5205 MakeCellVisible(m_selectedBlockCorner
);
5206 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
5210 switch ( m_selection
->GetSelectionMode() )
5213 wxFAIL_MSG( "unknown selection mode" );
5216 case wxGridSelectCells
:
5217 // arbitrary blocks selection allowed so just use the cell
5218 // coordinates as is
5221 case wxGridSelectRows
:
5222 // only full rows selection allowd, ensure that we do select
5225 rightCol
= GetNumberCols() - 1;
5228 case wxGridSelectColumns
:
5229 // same as above but for columns
5231 bottomRow
= GetNumberRows() - 1;
5234 case wxGridSelectRowsOrColumns
:
5235 // in this mode we can select only full rows or full columns so
5236 // it doesn't make sense to select blocks at all (and we can't
5237 // extend the block because there is no preferred direction, we
5238 // could only extend it to cover the entire grid but this is
5244 EnsureFirstLessThanSecond(topRow
, bottomRow
);
5245 EnsureFirstLessThanSecond(leftCol
, rightCol
);
5247 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
5248 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
5250 // First the case that we selected a completely new area
5251 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
5252 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
5255 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
5256 wxGridCellCoords ( bottomRow
, rightCol
) );
5257 m_gridWin
->Refresh( false, &rect
);
5260 // Now handle changing an existing selection area.
5261 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
5262 m_selectedBlockBottomRight
!= updateBottomRight
)
5264 // Compute two optimal update rectangles:
5265 // Either one rectangle is a real subset of the
5266 // other, or they are (almost) disjoint!
5268 bool need_refresh
[4];
5272 need_refresh
[3] = false;
5275 // Store intermediate values
5276 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
5277 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
5278 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
5279 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
5281 // Determine the outer/inner coordinates.
5282 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
5283 EnsureFirstLessThanSecond(oldTop
, topRow
);
5284 EnsureFirstLessThanSecond(rightCol
, oldRight
);
5285 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
5287 // Now, either the stuff marked old is the outer
5288 // rectangle or we don't have a situation where one
5289 // is contained in the other.
5291 if ( oldLeft
< leftCol
)
5293 // Refresh the newly selected or deselected
5294 // area to the left of the old or new selection.
5295 need_refresh
[0] = true;
5296 rect
[0] = BlockToDeviceRect(
5297 wxGridCellCoords( oldTop
, oldLeft
),
5298 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
5301 if ( oldTop
< topRow
)
5303 // Refresh the newly selected or deselected
5304 // area above the old or new selection.
5305 need_refresh
[1] = true;
5306 rect
[1] = BlockToDeviceRect(
5307 wxGridCellCoords( oldTop
, leftCol
),
5308 wxGridCellCoords( topRow
- 1, rightCol
) );
5311 if ( oldRight
> rightCol
)
5313 // Refresh the newly selected or deselected
5314 // area to the right of the old or new selection.
5315 need_refresh
[2] = true;
5316 rect
[2] = BlockToDeviceRect(
5317 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
5318 wxGridCellCoords( oldBottom
, oldRight
) );
5321 if ( oldBottom
> bottomRow
)
5323 // Refresh the newly selected or deselected
5324 // area below the old or new selection.
5325 need_refresh
[3] = true;
5326 rect
[3] = BlockToDeviceRect(
5327 wxGridCellCoords( bottomRow
+ 1, leftCol
),
5328 wxGridCellCoords( oldBottom
, rightCol
) );
5331 // various Refresh() calls
5332 for (i
= 0; i
< 4; i
++ )
5333 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5334 m_gridWin
->Refresh( false, &(rect
[i
]) );
5338 m_selectedBlockTopLeft
= updateTopLeft
;
5339 m_selectedBlockBottomRight
= updateBottomRight
;
5343 // ------ functions to get/send data (see also public functions)
5346 bool wxGrid::GetModelValues()
5348 // Hide the editor, so it won't hide a changed value.
5349 HideCellEditControl();
5353 // all we need to do is repaint the grid
5355 m_gridWin
->Refresh();
5362 bool wxGrid::SetModelValues()
5366 // Disable the editor, so it won't hide a changed value.
5367 // Do we also want to save the current value of the editor first?
5369 DisableCellEditControl();
5373 for ( row
= 0; row
< m_numRows
; row
++ )
5375 for ( col
= 0; col
< m_numCols
; col
++ )
5377 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5387 // Note - this function only draws cells that are in the list of
5388 // exposed cells (usually set from the update region by
5389 // CalcExposedCells)
5391 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5393 if ( !m_numRows
|| !m_numCols
)
5396 int i
, numCells
= cells
.GetCount();
5397 int row
, col
, cell_rows
, cell_cols
;
5398 wxGridCellCoordsArray redrawCells
;
5400 for ( i
= numCells
- 1; i
>= 0; i
-- )
5402 row
= cells
[i
].GetRow();
5403 col
= cells
[i
].GetCol();
5404 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5406 // If this cell is part of a multicell block, find owner for repaint
5407 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5409 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5410 bool marked
= false;
5411 for ( int j
= 0; j
< numCells
; j
++ )
5413 if ( cell
== cells
[j
] )
5422 int count
= redrawCells
.GetCount();
5423 for (int j
= 0; j
< count
; j
++)
5425 if ( cell
== redrawCells
[j
] )
5433 redrawCells
.Add( cell
);
5436 // don't bother drawing this cell
5440 // If this cell is empty, find cell to left that might want to overflow
5441 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5443 for ( int l
= 0; l
< cell_rows
; l
++ )
5445 // find a cell in this row to leave already marked for repaint
5447 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5448 if ((redrawCells
[k
].GetCol() < left
) &&
5449 (redrawCells
[k
].GetRow() == row
))
5451 left
= redrawCells
[k
].GetCol();
5455 left
= 0; // oh well
5457 for (int j
= col
- 1; j
>= left
; j
--)
5459 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5461 if (GetCellOverflow(row
+ l
, j
))
5463 wxGridCellCoords
cell(row
+ l
, j
);
5464 bool marked
= false;
5466 for (int k
= 0; k
< numCells
; k
++)
5468 if ( cell
== cells
[k
] )
5477 int count
= redrawCells
.GetCount();
5478 for (int k
= 0; k
< count
; k
++)
5480 if ( cell
== redrawCells
[k
] )
5487 redrawCells
.Add( cell
);
5496 DrawCell( dc
, cells
[i
] );
5499 numCells
= redrawCells
.GetCount();
5501 for ( i
= numCells
- 1; i
>= 0; i
-- )
5503 DrawCell( dc
, redrawCells
[i
] );
5507 void wxGrid::DrawGridSpace( wxDC
& dc
)
5510 m_gridWin
->GetClientSize( &cw
, &ch
);
5513 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5515 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5516 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5518 if ( right
> rightCol
|| bottom
> bottomRow
)
5521 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5523 dc
.SetBrush(GetDefaultCellBackgroundColour());
5524 dc
.SetPen( *wxTRANSPARENT_PEN
);
5526 if ( right
> rightCol
)
5528 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5531 if ( bottom
> bottomRow
)
5533 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5538 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5540 int row
= coords
.GetRow();
5541 int col
= coords
.GetCol();
5543 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5546 // we draw the cell border ourselves
5547 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5549 bool isCurrent
= coords
== m_currentCellCoords
;
5551 wxRect rect
= CellToRect( row
, col
);
5553 // if the editor is shown, we should use it and not the renderer
5554 // Note: However, only if it is really _shown_, i.e. not hidden!
5555 if ( isCurrent
&& IsCellEditControlShown() )
5557 // NB: this "#if..." is temporary and fixes a problem where the
5558 // edit control is erased by this code after being rendered.
5559 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5560 // implicitly, causing this out-of order render.
5561 #if !defined(__WXMAC__)
5562 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5563 editor
->PaintBackground(dc
, rect
, *attr
);
5569 // but all the rest is drawn by the cell renderer and hence may be customized
5570 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5571 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5578 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5580 // don't show highlight when the grid doesn't have focus
5584 int row
= m_currentCellCoords
.GetRow();
5585 int col
= m_currentCellCoords
.GetCol();
5587 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5590 wxRect rect
= CellToRect(row
, col
);
5592 // hmmm... what could we do here to show that the cell is disabled?
5593 // for now, I just draw a thinner border than for the other ones, but
5594 // it doesn't look really good
5596 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5600 // The center of the drawn line is where the position/width/height of
5601 // the rectangle is actually at (on wxMSW at least), so the
5602 // size of the rectangle is reduced to compensate for the thickness of
5603 // the line. If this is too strange on non-wxMSW platforms then
5604 // please #ifdef this appropriately.
5605 rect
.x
+= penWidth
/ 2;
5606 rect
.y
+= penWidth
/ 2;
5607 rect
.width
-= penWidth
- 1;
5608 rect
.height
-= penWidth
- 1;
5610 // Now draw the rectangle
5611 // use the cellHighlightColour if the cell is inside a selection, this
5612 // will ensure the cell is always visible.
5613 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5614 : m_cellHighlightColour
,
5616 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5617 dc
.DrawRectangle(rect
);
5621 wxPen
wxGrid::GetDefaultGridLinePen()
5623 return wxPen(GetGridLineColour());
5626 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5628 return GetDefaultGridLinePen();
5631 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5633 return GetDefaultGridLinePen();
5636 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5638 int row
= coords
.GetRow();
5639 int col
= coords
.GetCol();
5640 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5644 wxRect rect
= CellToRect( row
, col
);
5646 // right hand border
5647 dc
.SetPen( GetColGridLinePen(col
) );
5648 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5649 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5652 dc
.SetPen( GetRowGridLinePen(row
) );
5653 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5654 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5657 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5659 // This if block was previously in wxGrid::OnPaint but that doesn't
5660 // seem to get called under wxGTK - MB
5662 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5663 m_numRows
&& m_numCols
)
5665 m_currentCellCoords
.Set(0, 0);
5668 if ( IsCellEditControlShown() )
5670 // don't show highlight when the edit control is shown
5674 // if the active cell was repainted, repaint its highlight too because it
5675 // might have been damaged by the grid lines
5676 size_t count
= cells
.GetCount();
5677 for ( size_t n
= 0; n
< count
; n
++ )
5679 wxGridCellCoords cell
= cells
[n
];
5681 // If we are using attributes, then we may have just exposed another
5682 // cell in a partially-visible merged cluster of cells. If the "anchor"
5683 // (upper left) cell of this merged cluster is the cell indicated by
5684 // m_currentCellCoords, then we need to refresh the cell highlight even
5685 // though the "anchor" itself is not part of our update segment.
5686 if ( CanHaveAttributes() )
5690 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5693 cell
.SetRow(cell
.GetRow() + rows
);
5696 cell
.SetCol(cell
.GetCol() + cols
);
5699 if ( cell
== m_currentCellCoords
)
5701 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5702 DrawCellHighlight(dc
, attr
);
5710 // Used by wxGrid::Render() to draw the grid lines only for the cells in the
5713 wxGrid::DrawRangeGridLines(wxDC
& dc
,
5714 const wxRegion
& reg
,
5715 const wxGridCellCoords
& topLeft
,
5716 const wxGridCellCoords
& bottomRight
)
5718 if ( !m_gridLinesEnabled
)
5721 int top
, left
, width
, height
;
5722 reg
.GetBox( left
, top
, width
, height
);
5724 // create a clipping region
5725 wxRegion
clippedcells( dc
.LogicalToDeviceX( left
),
5726 dc
.LogicalToDeviceY( top
),
5727 dc
.LogicalToDeviceXRel( width
),
5728 dc
.LogicalToDeviceYRel( height
) );
5730 // subtract multi cell span area from clipping region for lines
5732 for ( int row
= topLeft
.GetRow(); row
<= bottomRight
.GetRow(); row
++ )
5734 for ( int col
= topLeft
.GetCol(); col
<= bottomRight
.GetCol(); col
++ )
5736 int cell_rows
, cell_cols
;
5737 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5738 if ( cell_rows
> 1 || cell_cols
> 1 ) // multi cell
5740 rect
= CellToRect( row
, col
);
5741 // cater for scaling
5742 // device origin already set in ::Render() for x, y
5743 rect
.x
= dc
.LogicalToDeviceX( rect
.x
);
5744 rect
.y
= dc
.LogicalToDeviceY( rect
.y
);
5745 rect
.width
= dc
.LogicalToDeviceXRel( rect
.width
);
5746 rect
.height
= dc
.LogicalToDeviceYRel( rect
.height
) - 1;
5747 clippedcells
.Subtract( rect
);
5749 else if ( cell_rows
< 0 || cell_cols
< 0 ) // part of multicell
5751 rect
= CellToRect( row
+ cell_rows
, col
+ cell_cols
);
5752 rect
.x
= dc
.LogicalToDeviceX( rect
.x
);
5753 rect
.y
= dc
.LogicalToDeviceY( rect
.y
);
5754 rect
.width
= dc
.LogicalToDeviceXRel( rect
.width
);
5755 rect
.height
= dc
.LogicalToDeviceYRel( rect
.height
) - 1;
5756 clippedcells
.Subtract( rect
);
5761 dc
.SetDeviceClippingRegion( clippedcells
);
5764 top
, left
, top
+ height
, left
+ width
,
5765 topLeft
.GetRow(), topLeft
.GetCol(),
5766 bottomRight
.GetRow(), bottomRight
.GetCol());
5768 dc
.DestroyClippingRegion();
5771 // This is used to redraw all grid lines e.g. when the grid line colour
5774 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5776 if ( !m_gridLinesEnabled
)
5779 int top
, bottom
, left
, right
;
5782 m_gridWin
->GetClientSize(&cw
, &ch
);
5783 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5784 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5786 // avoid drawing grid lines past the last row and col
5787 if ( m_gridLinesClipHorz
)
5792 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5793 if ( right
> lastColRight
)
5794 right
= lastColRight
;
5797 if ( m_gridLinesClipVert
)
5802 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5803 if ( bottom
> lastRowBottom
)
5804 bottom
= lastRowBottom
;
5807 // no gridlines inside multicells, clip them out
5808 int leftCol
= GetColPos( internalXToCol(left
) );
5809 int topRow
= internalYToRow(top
);
5810 int rightCol
= GetColPos( internalXToCol(right
) );
5811 int bottomRow
= internalYToRow(bottom
);
5813 wxRegion
clippedcells(0, 0, cw
, ch
);
5815 int cell_rows
, cell_cols
;
5818 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5820 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5822 int i
= GetColAt( colPos
);
5824 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5825 if ((cell_rows
> 1) || (cell_cols
> 1))
5827 rect
= CellToRect(j
,i
);
5828 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5829 clippedcells
.Subtract(rect
);
5831 else if ((cell_rows
< 0) || (cell_cols
< 0))
5833 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5834 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5835 clippedcells
.Subtract(rect
);
5840 dc
.SetDeviceClippingRegion( clippedcells
);
5843 top
, left
, bottom
, right
,
5844 topRow
, leftCol
, m_numRows
, m_numCols
);
5846 dc
.DestroyClippingRegion();
5850 wxGrid::DoDrawGridLines(wxDC
& dc
,
5852 int bottom
, int right
,
5853 int topRow
, int leftCol
,
5854 int bottomRow
, int rightCol
)
5856 // horizontal grid lines
5857 for ( int i
= topRow
; i
< bottomRow
; i
++ )
5859 int bot
= GetRowBottom(i
) - 1;
5866 dc
.SetPen( GetRowGridLinePen(i
) );
5867 dc
.DrawLine( left
, bot
, right
, bot
);
5871 // vertical grid lines
5872 for ( int colPos
= leftCol
; colPos
< rightCol
; colPos
++ )
5874 int i
= GetColAt( colPos
);
5876 int colRight
= GetColRight(i
);
5878 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5882 if ( colRight
> right
)
5885 if ( colRight
>= left
)
5887 dc
.SetPen( GetColGridLinePen(i
) );
5888 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5893 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5898 const size_t numLabels
= rows
.GetCount();
5899 for ( size_t i
= 0; i
< numLabels
; i
++ )
5901 DrawRowLabel( dc
, rows
[i
] );
5905 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5907 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5910 wxGridCellAttrProvider
* const
5911 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5913 // notice that an explicit static_cast is needed to avoid a compilation
5914 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5915 // wxGridRowHeaderRenderer class without it
5916 const wxGridRowHeaderRenderer
&
5917 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5918 : static_cast<const wxGridRowHeaderRenderer
&>
5919 (gs_defaultHeaderRenderers
.rowRenderer
);
5921 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5922 rend
.DrawBorder(*this, dc
, rect
);
5925 GetRowLabelAlignment(&hAlign
, &vAlign
);
5927 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5928 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5931 void wxGrid::UseNativeColHeader(bool native
)
5933 if ( native
== m_useNativeHeader
)
5937 m_useNativeHeader
= native
;
5939 CreateColumnWindow();
5941 if ( m_useNativeHeader
)
5942 GetGridColHeader()->SetColumnCount(m_numCols
);
5946 void wxGrid::SetUseNativeColLabels( bool native
)
5948 wxASSERT_MSG( !m_useNativeHeader
,
5949 "doesn't make sense when using native header" );
5951 m_nativeColumnLabels
= native
;
5954 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5955 SetColLabelSize( height
);
5958 GetColLabelWindow()->Refresh();
5959 m_cornerLabelWin
->Refresh();
5962 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5967 const size_t numLabels
= cols
.GetCount();
5968 for ( size_t i
= 0; i
< numLabels
; i
++ )
5970 DrawColLabel( dc
, cols
[i
] );
5974 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5976 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5978 if ( m_nativeColumnLabels
)
5982 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5989 wxGridCellAttrProvider
* const
5990 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5991 const wxGridCornerHeaderRenderer
&
5992 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5993 : static_cast<wxGridCornerHeaderRenderer
&>
5994 (gs_defaultHeaderRenderers
.cornerRenderer
);
5996 rend
.DrawBorder(*this, dc
, rect
);
6000 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
6002 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
6005 int colLeft
= GetColLeft(col
);
6007 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
6008 wxGridCellAttrProvider
* const
6009 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
6010 const wxGridColumnHeaderRenderer
&
6011 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
6012 : static_cast<wxGridColumnHeaderRenderer
&>
6013 (gs_defaultHeaderRenderers
.colRenderer
);
6015 if ( m_nativeColumnLabels
)
6017 wxRendererNative::Get().DrawHeaderButton
6019 GetColLabelWindow(),
6024 ? IsSortOrderAscending()
6025 ? wxHDR_SORT_ICON_UP
6026 : wxHDR_SORT_ICON_DOWN
6027 : wxHDR_SORT_ICON_NONE
6033 // It is reported that we need to erase the background to avoid display
6034 // artefacts, see #12055.
6035 wxDCBrushChanger
setBrush(dc
, m_colWindow
->GetBackgroundColour());
6036 dc
.DrawRectangle(rect
);
6038 rend
.DrawBorder(*this, dc
, rect
);
6042 GetColLabelAlignment(&hAlign
, &vAlign
);
6043 const int orient
= GetColLabelTextOrientation();
6045 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
6048 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
6049 // we just have to add textOrientation support
6050 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6051 const wxString
& value
,
6055 int textOrientation
) const
6057 wxArrayString lines
;
6059 StringToLines( value
, lines
);
6061 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
6064 void wxGrid::DrawTextRectangle(wxDC
& dc
,
6065 const wxArrayString
& lines
,
6069 int textOrientation
) const
6071 if ( lines
.empty() )
6074 wxDCClipper
clip(dc
, rect
);
6079 if ( textOrientation
== wxHORIZONTAL
)
6080 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6082 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
6086 switch ( vertAlign
)
6088 case wxALIGN_BOTTOM
:
6089 if ( textOrientation
== wxHORIZONTAL
)
6090 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6092 x
= rect
.x
+ rect
.width
- textWidth
;
6095 case wxALIGN_CENTRE
:
6096 if ( textOrientation
== wxHORIZONTAL
)
6097 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
6099 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
6104 if ( textOrientation
== wxHORIZONTAL
)
6111 // Align each line of a multi-line label
6112 size_t nLines
= lines
.GetCount();
6113 for ( size_t l
= 0; l
< nLines
; l
++ )
6115 const wxString
& line
= lines
[l
];
6119 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
6123 wxCoord lineWidth
= 0,
6125 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
6127 switch ( horizAlign
)
6130 if ( textOrientation
== wxHORIZONTAL
)
6131 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
6133 y
= rect
.y
+ lineWidth
+ 1;
6136 case wxALIGN_CENTRE
:
6137 if ( textOrientation
== wxHORIZONTAL
)
6138 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
6140 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
6145 if ( textOrientation
== wxHORIZONTAL
)
6148 y
= rect
.y
+ rect
.height
- 1;
6152 if ( textOrientation
== wxHORIZONTAL
)
6154 dc
.DrawText( line
, x
, y
);
6159 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
6165 // Split multi-line text up into an array of strings.
6166 // Any existing contents of the string array are preserved.
6168 // TODO: refactor wxTextFile::Read() and reuse the same code from here
6169 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
6173 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6174 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6176 while ( startPos
< (int)tVal
.length() )
6178 pos
= tVal
.Mid(startPos
).Find( eol
);
6183 else if ( pos
== 0 )
6185 lines
.Add( wxEmptyString
);
6189 lines
.Add( tVal
.Mid(startPos
, pos
) );
6192 startPos
+= pos
+ 1;
6195 if ( startPos
< (int)tVal
.length() )
6197 lines
.Add( tVal
.Mid( startPos
) );
6201 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
6202 const wxArrayString
& lines
,
6203 long *width
, long *height
) const
6207 wxCoord lineW
= 0, lineH
= 0;
6210 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6212 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6213 w
= wxMax( w
, lineW
);
6222 // ------ Batch processing.
6224 void wxGrid::EndBatch()
6226 if ( m_batchCount
> 0 )
6229 if ( !m_batchCount
)
6232 m_rowLabelWin
->Refresh();
6233 m_colWindow
->Refresh();
6234 m_cornerLabelWin
->Refresh();
6235 m_gridWin
->Refresh();
6240 // Use this, rather than wxWindow::Refresh(), to force an immediate
6241 // repainting of the grid. Has no effect if you are already inside a
6242 // BeginBatch / EndBatch block.
6244 void wxGrid::ForceRefresh()
6250 bool wxGrid::Enable(bool enable
)
6252 if ( !wxScrolledWindow::Enable(enable
) )
6255 // redraw in the new state
6256 m_gridWin
->Refresh();
6262 // ------ Edit control functions
6265 void wxGrid::EnableEditing( bool edit
)
6267 if ( edit
!= m_editable
)
6270 EnableCellEditControl(edit
);
6275 void wxGrid::EnableCellEditControl( bool enable
)
6280 if ( enable
!= m_cellEditCtrlEnabled
)
6284 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
6287 // this should be checked by the caller!
6288 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
6290 // do it before ShowCellEditControl()
6291 m_cellEditCtrlEnabled
= enable
;
6293 ShowCellEditControl();
6297 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
6299 HideCellEditControl();
6300 SaveEditControlValue();
6302 // do it after HideCellEditControl()
6303 m_cellEditCtrlEnabled
= enable
;
6308 bool wxGrid::IsCurrentCellReadOnly() const
6311 attr
= const_cast<wxGrid
*>(this)->GetCellAttr(m_currentCellCoords
);
6312 bool readonly
= attr
->IsReadOnly();
6318 bool wxGrid::CanEnableCellControl() const
6320 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
6321 !IsCurrentCellReadOnly();
6324 bool wxGrid::IsCellEditControlEnabled() const
6326 // the cell edit control might be disable for all cells or just for the
6327 // current one if it's read only
6328 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
6331 bool wxGrid::IsCellEditControlShown() const
6333 bool isShown
= false;
6335 if ( m_cellEditCtrlEnabled
)
6337 int row
= m_currentCellCoords
.GetRow();
6338 int col
= m_currentCellCoords
.GetCol();
6339 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6340 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6345 if ( editor
->IsCreated() )
6347 isShown
= editor
->GetControl()->IsShown();
6357 void wxGrid::ShowCellEditControl()
6359 if ( IsCellEditControlEnabled() )
6361 if ( !IsVisible( m_currentCellCoords
, false ) )
6363 m_cellEditCtrlEnabled
= false;
6368 wxRect rect
= CellToRect( m_currentCellCoords
);
6369 int row
= m_currentCellCoords
.GetRow();
6370 int col
= m_currentCellCoords
.GetCol();
6372 // if this is part of a multicell, find owner (topleft)
6373 int cell_rows
, cell_cols
;
6374 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6375 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6379 m_currentCellCoords
.SetRow( row
);
6380 m_currentCellCoords
.SetCol( col
);
6383 // erase the highlight and the cell contents because the editor
6384 // might not cover the entire cell
6385 wxClientDC
dc( m_gridWin
);
6387 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6388 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
6389 dc
.SetPen(*wxTRANSPARENT_PEN
);
6390 dc
.DrawRectangle(rect
);
6392 // convert to scrolled coords
6393 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6399 // cell is shifted by one pixel
6400 // However, don't allow x or y to become negative
6401 // since the SetSize() method interprets that as
6408 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6409 if ( !editor
->IsCreated() )
6411 editor
->Create(m_gridWin
, wxID_ANY
,
6412 new wxGridCellEditorEvtHandler(this, editor
));
6414 wxGridEditorCreatedEvent
evt(GetId(),
6415 wxEVT_GRID_EDITOR_CREATED
,
6419 editor
->GetControl());
6420 GetEventHandler()->ProcessEvent(evt
);
6423 // resize editor to overflow into righthand cells if allowed
6424 int maxWidth
= rect
.width
;
6425 wxString value
= GetCellValue(row
, col
);
6426 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
6429 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
6430 if (maxWidth
< rect
.width
)
6431 maxWidth
= rect
.width
;
6434 int client_right
= m_gridWin
->GetClientSize().GetWidth();
6435 if (rect
.x
+ maxWidth
> client_right
)
6436 maxWidth
= client_right
- rect
.x
;
6438 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
6440 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6441 // may have changed earlier
6442 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
6445 GetCellSize( row
, i
, &c_rows
, &c_cols
);
6447 // looks weird going over a multicell
6448 if (m_table
->IsEmptyCell( row
, i
) &&
6449 (rect
.width
< maxWidth
) && (c_rows
== 1))
6451 rect
.width
+= GetColWidth( i
);
6457 if (rect
.GetRight() > client_right
)
6458 rect
.SetRight( client_right
- 1 );
6461 editor
->SetCellAttr( attr
);
6462 editor
->SetSize( rect
);
6464 editor
->GetControl()->Move(
6465 editor
->GetControl()->GetPosition().x
+ nXMove
,
6466 editor
->GetControl()->GetPosition().y
);
6467 editor
->Show( true, attr
);
6469 // recalc dimensions in case we need to
6470 // expand the scrolled window to account for editor
6473 editor
->BeginEdit(row
, col
, this);
6474 editor
->SetCellAttr(NULL
);
6482 void wxGrid::HideCellEditControl()
6484 if ( IsCellEditControlEnabled() )
6486 int row
= m_currentCellCoords
.GetRow();
6487 int col
= m_currentCellCoords
.GetCol();
6489 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6490 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6491 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6492 editor
->Show( false );
6496 // return the focus to the grid itself if the editor had it
6498 // note that we must not do this unconditionally to avoid stealing
6499 // focus from the window which just received it if we are hiding the
6500 // editor precisely because we lost focus
6501 if ( editorHadFocus
)
6502 m_gridWin
->SetFocus();
6504 // refresh whole row to the right
6505 wxRect
rect( CellToRect(row
, col
) );
6506 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6507 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6510 // ensure that the pixels under the focus ring get refreshed as well
6511 rect
.Inflate(10, 10);
6514 m_gridWin
->Refresh( false, &rect
);
6518 void wxGrid::SaveEditControlValue()
6520 if ( IsCellEditControlEnabled() )
6522 int row
= m_currentCellCoords
.GetRow();
6523 int col
= m_currentCellCoords
.GetCol();
6525 wxString oldval
= GetCellValue(row
, col
);
6527 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6528 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6531 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6533 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6535 editor
->ApplyEdit(row
, col
, this);
6537 // for compatibility reasons dating back to wx 2.8 when this event
6538 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6539 // didn't exist we allow vetoing this one too
6540 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6542 // Event has been vetoed, set the data back.
6543 SetCellValue(row
, col
, oldval
);
6553 // ------ Grid location functions
6554 // Note that all of these functions work with the logical coordinates of
6555 // grid cells and labels so you will need to convert from device
6556 // coordinates for mouse events etc.
6559 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6561 int row
= YToRow(y
);
6562 int col
= XToCol(x
);
6564 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6565 : wxGridCellCoords(row
, col
);
6568 // compute row or column from some (unscrolled) coordinate value, using either
6569 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6570 // m_rowBottoms/m_colRights to do it quickly in O(log n) time.
6571 // NOTE: This may not work correctly for reordered columns.
6572 int wxGrid::PosToLinePos(int coord
,
6574 const wxGridOperations
& oper
) const
6576 const int numLines
= oper
.GetNumberOfLines(this);
6579 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6581 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6582 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6584 int maxPos
= coord
/ defaultLineSize
,
6587 // check for the simplest case: if we have no explicit line sizes
6588 // configured, then we already know the line this position falls in
6589 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6590 if ( lineEnds
.empty() )
6592 if ( maxPos
< numLines
)
6595 return clipToMinMax
? numLines
- 1 : -1;
6599 // binary search is quite efficient and we can't really make any assumptions
6600 // on where to start here since row and columns could be of size 0 if they are
6601 // hidden. While this could be made more efficient, some profiling will be
6602 // necessary to determine if it really is a performance bottleneck
6603 maxPos
= numLines
- 1;
6605 // check if the position is beyond the last column
6606 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6607 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6608 return clipToMinMax
? maxPos
: -1;
6610 // or before the first one
6611 const int lineAt0
= oper
.GetLineAt(this, 0);
6612 if ( coord
< lineEnds
[lineAt0
] )
6616 // finally do perform the binary search
6617 while ( minPos
< maxPos
)
6619 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6620 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6622 "wxGrid: internal error in PosToLinePos()" );
6624 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6629 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6630 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6640 wxGrid::PosToLine(int coord
,
6642 const wxGridOperations
& oper
) const
6644 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6646 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6649 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6651 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6654 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6656 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6659 int wxGrid::XToPos(int x
) const
6661 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6664 // return the row number such that the y coord is near the edge of, or -1 if
6665 // not near an edge.
6667 // notice that position can only possibly be near an edge if the row/column is
6668 // large enough to still allow for an "inner" area that is _not_ near the edge
6669 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6670 // _never_ be considered to be near the edge).
6671 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6673 const int line
= oper
.PosToLine(this, pos
, true);
6675 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6677 // We know that we are in this line, test whether we are close enough
6678 // to start or end border, respectively.
6679 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6681 else if ( line
> 0 &&
6682 pos
- oper
.GetLineStartPos(this,
6683 line
) < WXGRID_LABEL_EDGE_ZONE
)
6685 return oper
.GetLineBefore(this, line
);
6692 int wxGrid::YToEdgeOfRow(int y
) const
6694 return PosToEdgeOfLine(y
, wxGridRowOperations());
6697 int wxGrid::XToEdgeOfCol(int x
) const
6699 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6702 wxRect
wxGrid::CellToRect( int row
, int col
) const
6704 wxRect
rect( -1, -1, -1, -1 );
6706 if ( row
>= 0 && row
< m_numRows
&&
6707 col
>= 0 && col
< m_numCols
)
6709 int i
, cell_rows
, cell_cols
;
6710 rect
.width
= rect
.height
= 0;
6711 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6712 // if negative then find multicell owner
6717 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6719 rect
.x
= GetColLeft(col
);
6720 rect
.y
= GetRowTop(row
);
6721 for (i
=col
; i
< col
+ cell_cols
; i
++)
6722 rect
.width
+= GetColWidth(i
);
6723 for (i
=row
; i
< row
+ cell_rows
; i
++)
6724 rect
.height
+= GetRowHeight(i
);
6726 // if grid lines are enabled, then the area of the cell is a bit smaller
6727 if (m_gridLinesEnabled
)
6737 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6739 // get the cell rectangle in logical coords
6741 wxRect
r( CellToRect( row
, col
) );
6743 // convert to device coords
6745 int left
, top
, right
, bottom
;
6746 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6747 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6749 // check against the client area of the grid window
6751 m_gridWin
->GetClientSize( &cw
, &ch
);
6753 if ( wholeCellVisible
)
6755 // is the cell wholly visible ?
6756 return ( left
>= 0 && right
<= cw
&&
6757 top
>= 0 && bottom
<= ch
);
6761 // is the cell partly visible ?
6763 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6764 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6768 // make the specified cell location visible by doing a minimal amount
6771 void wxGrid::MakeCellVisible( int row
, int col
)
6774 int xpos
= -1, ypos
= -1;
6776 if ( row
>= 0 && row
< m_numRows
&&
6777 col
>= 0 && col
< m_numCols
)
6779 // get the cell rectangle in logical coords
6780 wxRect
r( CellToRect( row
, col
) );
6782 // convert to device coords
6783 int left
, top
, right
, bottom
;
6784 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6785 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6788 m_gridWin
->GetClientSize( &cw
, &ch
);
6794 else if ( bottom
> ch
)
6796 int h
= r
.GetHeight();
6798 for ( i
= row
- 1; i
>= 0; i
-- )
6800 int rowHeight
= GetRowHeight(i
);
6801 if ( h
+ rowHeight
> ch
)
6808 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6809 // have rounding errors (this is important, because if we do,
6810 // we might not scroll at all and some cells won't be redrawn)
6812 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6813 // so just add a full scroll unit...
6814 ypos
+= m_yScrollPixelsPerLine
;
6817 // special handling for wide cells - show always left part of the cell!
6818 // Otherwise, e.g. when stepping from row to row, it would jump between
6819 // left and right part of the cell on every step!
6821 if ( left
< 0 || (right
- left
) >= cw
)
6825 else if ( right
> cw
)
6827 // position the view so that the cell is on the right
6829 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6830 xpos
= x0
+ (right
- cw
);
6832 // see comment for ypos above
6833 xpos
+= m_xScrollPixelsPerLine
;
6836 if ( xpos
!= -1 || ypos
!= -1 )
6839 xpos
/= m_xScrollPixelsPerLine
;
6841 ypos
/= m_yScrollPixelsPerLine
;
6842 Scroll( xpos
, ypos
);
6849 // ------ Grid cursor movement functions
6853 wxGrid::DoMoveCursor(bool expandSelection
,
6854 const wxGridDirectionOperations
& diroper
)
6856 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6859 if ( expandSelection
)
6861 wxGridCellCoords coords
= m_selectedBlockCorner
;
6862 if ( coords
== wxGridNoCellCoords
)
6863 coords
= m_currentCellCoords
;
6865 if ( diroper
.IsAtBoundary(coords
) )
6868 diroper
.Advance(coords
);
6870 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6872 else // don't expand selection
6876 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6879 wxGridCellCoords coords
= m_currentCellCoords
;
6880 diroper
.Advance(coords
);
6888 bool wxGrid::MoveCursorUp(bool expandSelection
)
6890 return DoMoveCursor(expandSelection
,
6891 wxGridBackwardOperations(this, wxGridRowOperations()));
6894 bool wxGrid::MoveCursorDown(bool expandSelection
)
6896 return DoMoveCursor(expandSelection
,
6897 wxGridForwardOperations(this, wxGridRowOperations()));
6900 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6902 return DoMoveCursor(expandSelection
,
6903 wxGridBackwardOperations(this, wxGridColumnOperations()));
6906 bool wxGrid::MoveCursorRight(bool expandSelection
)
6908 return DoMoveCursor(expandSelection
,
6909 wxGridForwardOperations(this, wxGridColumnOperations()));
6912 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6914 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6917 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6920 const int oldRow
= m_currentCellCoords
.GetRow();
6921 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6922 if ( newRow
== oldRow
)
6924 wxGridCellCoords
coords(m_currentCellCoords
);
6925 diroper
.Advance(coords
);
6926 newRow
= coords
.GetRow();
6929 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6934 bool wxGrid::MovePageUp()
6936 return DoMoveCursorByPage(
6937 wxGridBackwardOperations(this, wxGridRowOperations()));
6940 bool wxGrid::MovePageDown()
6942 return DoMoveCursorByPage(
6943 wxGridForwardOperations(this, wxGridRowOperations()));
6946 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6947 // until we find a non-empty cell or reach the grid end
6949 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6950 const wxGridDirectionOperations
& diroper
)
6952 while ( !diroper
.IsAtBoundary(coords
) )
6954 diroper
.Advance(coords
);
6955 if ( !m_table
->IsEmpty(coords
) )
6961 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6962 const wxGridDirectionOperations
& diroper
)
6964 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6967 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6970 wxGridCellCoords
coords(m_currentCellCoords
);
6971 if ( m_table
->IsEmpty(coords
) )
6973 // we are in an empty cell: find the next block of non-empty cells
6974 AdvanceToNextNonEmpty(coords
, diroper
);
6976 else // current cell is not empty
6978 diroper
.Advance(coords
);
6979 if ( m_table
->IsEmpty(coords
) )
6981 // we started at the end of a block, find the next one
6982 AdvanceToNextNonEmpty(coords
, diroper
);
6984 else // we're in a middle of a block
6986 // go to the end of it, i.e. find the last cell before the next
6988 while ( !diroper
.IsAtBoundary(coords
) )
6990 wxGridCellCoords
coordsNext(coords
);
6991 diroper
.Advance(coordsNext
);
6992 if ( m_table
->IsEmpty(coordsNext
) )
6995 coords
= coordsNext
;
7000 if ( expandSelection
)
7002 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
7013 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
7015 return DoMoveCursorByBlock(
7017 wxGridBackwardOperations(this, wxGridRowOperations())
7021 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7023 return DoMoveCursorByBlock(
7025 wxGridForwardOperations(this, wxGridRowOperations())
7029 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7031 return DoMoveCursorByBlock(
7033 wxGridBackwardOperations(this, wxGridColumnOperations())
7037 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7039 return DoMoveCursorByBlock(
7041 wxGridForwardOperations(this, wxGridColumnOperations())
7046 // ------ Label values and formatting
7049 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
7052 *horiz
= m_rowLabelHorizAlign
;
7054 *vert
= m_rowLabelVertAlign
;
7057 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
7060 *horiz
= m_colLabelHorizAlign
;
7062 *vert
= m_colLabelVertAlign
;
7065 int wxGrid::GetColLabelTextOrientation() const
7067 return m_colLabelTextOrientation
;
7070 wxString
wxGrid::GetRowLabelValue( int row
) const
7074 return m_table
->GetRowLabelValue( row
);
7084 wxString
wxGrid::GetColLabelValue( int col
) const
7088 return m_table
->GetColLabelValue( col
);
7098 void wxGrid::SetRowLabelSize( int width
)
7100 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
7102 if ( width
== wxGRID_AUTOSIZE
)
7104 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
7107 if ( width
!= m_rowLabelWidth
)
7111 m_rowLabelWin
->Show( false );
7112 m_cornerLabelWin
->Show( false );
7114 else if ( m_rowLabelWidth
== 0 )
7116 m_rowLabelWin
->Show( true );
7117 if ( m_colLabelHeight
> 0 )
7118 m_cornerLabelWin
->Show( true );
7121 m_rowLabelWidth
= width
;
7122 InvalidateBestSize();
7124 wxScrolledWindow::Refresh( true );
7128 void wxGrid::SetColLabelSize( int height
)
7130 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
7132 if ( height
== wxGRID_AUTOSIZE
)
7134 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
7137 if ( height
!= m_colLabelHeight
)
7141 m_colWindow
->Show( false );
7142 m_cornerLabelWin
->Show( false );
7144 else if ( m_colLabelHeight
== 0 )
7146 m_colWindow
->Show( true );
7147 if ( m_rowLabelWidth
> 0 )
7148 m_cornerLabelWin
->Show( true );
7151 m_colLabelHeight
= height
;
7152 InvalidateBestSize();
7154 wxScrolledWindow::Refresh( true );
7158 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7160 if ( m_labelBackgroundColour
!= colour
)
7162 m_labelBackgroundColour
= colour
;
7163 m_rowLabelWin
->SetBackgroundColour( colour
);
7164 m_colWindow
->SetBackgroundColour( colour
);
7165 m_cornerLabelWin
->SetBackgroundColour( colour
);
7167 if ( !GetBatchCount() )
7169 m_rowLabelWin
->Refresh();
7170 m_colWindow
->Refresh();
7171 m_cornerLabelWin
->Refresh();
7176 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7178 if ( m_labelTextColour
!= colour
)
7180 m_labelTextColour
= colour
;
7181 if ( !GetBatchCount() )
7183 m_rowLabelWin
->Refresh();
7184 m_colWindow
->Refresh();
7189 void wxGrid::SetLabelFont( const wxFont
& font
)
7192 if ( !GetBatchCount() )
7194 m_rowLabelWin
->Refresh();
7195 m_colWindow
->Refresh();
7199 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7201 // allow old (incorrect) defs to be used
7204 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7205 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7206 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7211 case wxTOP
: vert
= wxALIGN_TOP
; break;
7212 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7213 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7216 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7218 m_rowLabelHorizAlign
= horiz
;
7221 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7223 m_rowLabelVertAlign
= vert
;
7226 if ( !GetBatchCount() )
7228 m_rowLabelWin
->Refresh();
7232 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7234 // allow old (incorrect) defs to be used
7237 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7238 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7239 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7244 case wxTOP
: vert
= wxALIGN_TOP
; break;
7245 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7246 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7249 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7251 m_colLabelHorizAlign
= horiz
;
7254 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7256 m_colLabelVertAlign
= vert
;
7259 if ( !GetBatchCount() )
7261 m_colWindow
->Refresh();
7265 // Note: under MSW, the default column label font must be changed because it
7266 // does not support vertical printing
7268 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
7269 // pGrid->SetLabelFont(font);
7270 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
7272 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
7274 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
7275 m_colLabelTextOrientation
= textOrientation
;
7277 if ( !GetBatchCount() )
7278 m_colWindow
->Refresh();
7281 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7285 m_table
->SetRowLabelValue( row
, s
);
7286 if ( !GetBatchCount() )
7288 wxRect rect
= CellToRect( row
, 0 );
7289 if ( rect
.height
> 0 )
7291 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7293 rect
.width
= m_rowLabelWidth
;
7294 m_rowLabelWin
->Refresh( true, &rect
);
7300 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7304 m_table
->SetColLabelValue( col
, s
);
7305 if ( !GetBatchCount() )
7307 if ( m_useNativeHeader
)
7309 GetGridColHeader()->UpdateColumn(col
);
7313 wxRect rect
= CellToRect( 0, col
);
7314 if ( rect
.width
> 0 )
7316 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7318 rect
.height
= m_colLabelHeight
;
7319 GetColLabelWindow()->Refresh( true, &rect
);
7326 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7328 if ( m_gridLineColour
!= colour
)
7330 m_gridLineColour
= colour
;
7332 if ( GridLinesEnabled() )
7337 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7339 if ( m_cellHighlightColour
!= colour
)
7341 m_cellHighlightColour
= colour
;
7343 wxClientDC
dc( m_gridWin
);
7345 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7346 DrawCellHighlight(dc
, attr
);
7351 void wxGrid::SetCellHighlightPenWidth(int width
)
7353 if (m_cellHighlightPenWidth
!= width
)
7355 m_cellHighlightPenWidth
= width
;
7357 // Just redrawing the cell highlight is not enough since that won't
7358 // make any visible change if the thickness is getting smaller.
7359 int row
= m_currentCellCoords
.GetRow();
7360 int col
= m_currentCellCoords
.GetCol();
7361 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7364 wxRect rect
= CellToRect(row
, col
);
7365 m_gridWin
->Refresh(true, &rect
);
7369 void wxGrid::SetCellHighlightROPenWidth(int width
)
7371 if (m_cellHighlightROPenWidth
!= width
)
7373 m_cellHighlightROPenWidth
= width
;
7375 // Just redrawing the cell highlight is not enough since that won't
7376 // make any visible change if the thickness is getting smaller.
7377 int row
= m_currentCellCoords
.GetRow();
7378 int col
= m_currentCellCoords
.GetCol();
7379 if ( row
== -1 || col
== -1 ||
7380 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7383 wxRect rect
= CellToRect(row
, col
);
7384 m_gridWin
->Refresh(true, &rect
);
7388 void wxGrid::RedrawGridLines()
7390 // the lines will be redrawn when the window is thawn
7391 if ( GetBatchCount() )
7394 if ( GridLinesEnabled() )
7396 wxClientDC
dc( m_gridWin
);
7398 DrawAllGridLines( dc
, wxRegion() );
7400 else // remove the grid lines
7402 m_gridWin
->Refresh();
7406 void wxGrid::EnableGridLines( bool enable
)
7408 if ( enable
!= m_gridLinesEnabled
)
7410 m_gridLinesEnabled
= enable
;
7416 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
7422 if ( GridLinesEnabled() )
7427 int wxGrid::GetDefaultRowSize() const
7429 return m_defaultRowHeight
;
7432 int wxGrid::GetRowSize( int row
) const
7434 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
7436 return GetRowHeight(row
);
7439 int wxGrid::GetDefaultColSize() const
7441 return m_defaultColWidth
;
7444 int wxGrid::GetColSize( int col
) const
7446 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
7448 return GetColWidth(col
);
7451 // ============================================================================
7452 // access to the grid attributes: each of them has a default value in the grid
7453 // itself and may be overidden on a per-cell basis
7454 // ============================================================================
7456 // ----------------------------------------------------------------------------
7457 // setting default attributes
7458 // ----------------------------------------------------------------------------
7460 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7462 m_defaultCellAttr
->SetBackgroundColour(col
);
7464 m_gridWin
->SetBackgroundColour(col
);
7468 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7470 m_defaultCellAttr
->SetTextColour(col
);
7473 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7475 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7478 void wxGrid::SetDefaultCellOverflow( bool allow
)
7480 m_defaultCellAttr
->SetOverflow(allow
);
7483 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7485 m_defaultCellAttr
->SetFont(font
);
7488 // For editors and renderers the type registry takes precedence over the
7489 // default attr, so we need to register the new editor/renderer for the string
7490 // data type in order to make setting a default editor/renderer appear to
7493 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7495 RegisterDataType(wxGRID_VALUE_STRING
,
7497 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7500 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7502 RegisterDataType(wxGRID_VALUE_STRING
,
7503 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7507 // ----------------------------------------------------------------------------
7508 // access to the default attributes
7509 // ----------------------------------------------------------------------------
7511 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7513 return m_defaultCellAttr
->GetBackgroundColour();
7516 wxColour
wxGrid::GetDefaultCellTextColour() const
7518 return m_defaultCellAttr
->GetTextColour();
7521 wxFont
wxGrid::GetDefaultCellFont() const
7523 return m_defaultCellAttr
->GetFont();
7526 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7528 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7531 bool wxGrid::GetDefaultCellOverflow() const
7533 return m_defaultCellAttr
->GetOverflow();
7536 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7538 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7541 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7543 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7546 // ----------------------------------------------------------------------------
7547 // access to cell attributes
7548 // ----------------------------------------------------------------------------
7550 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7552 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7553 wxColour colour
= attr
->GetBackgroundColour();
7559 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7561 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7562 wxColour colour
= attr
->GetTextColour();
7568 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7570 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7571 wxFont font
= attr
->GetFont();
7577 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7579 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7580 attr
->GetAlignment(horiz
, vert
);
7584 bool wxGrid::GetCellOverflow( int row
, int col
) const
7586 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7587 bool allow
= attr
->GetOverflow();
7594 wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7596 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7597 attr
->GetSize( num_rows
, num_cols
);
7600 if ( *num_rows
== 1 && *num_cols
== 1 )
7601 return CellSpan_None
; // just a normal cell
7603 if ( *num_rows
< 0 || *num_cols
< 0 )
7604 return CellSpan_Inside
; // covered by a multi-span cell
7606 // this cell spans multiple cells to its right/bottom
7607 return CellSpan_Main
;
7610 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7612 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7613 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7619 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7621 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7622 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7628 bool wxGrid::IsReadOnly(int row
, int col
) const
7630 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7631 bool isReadOnly
= attr
->IsReadOnly();
7637 // ----------------------------------------------------------------------------
7638 // attribute support: cache, automatic provider creation, ...
7639 // ----------------------------------------------------------------------------
7641 bool wxGrid::CanHaveAttributes() const
7648 return m_table
->CanHaveAttributes();
7651 void wxGrid::ClearAttrCache()
7653 if ( m_attrCache
.row
!= -1 )
7655 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7656 m_attrCache
.attr
= NULL
;
7657 m_attrCache
.row
= -1;
7658 // wxSafeDecRec(...) might cause event processing that accesses
7659 // the cached attribute, if one exists (e.g. by deleting the
7660 // editor stored within the attribute). Therefore it is important
7661 // to invalidate the cache before calling wxSafeDecRef!
7662 wxSafeDecRef(oldAttr
);
7666 void wxGrid::RefreshAttr(int row
, int col
)
7668 if ( m_attrCache
.row
== row
&& m_attrCache
.col
== col
)
7673 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7677 wxGrid
* const self
= const_cast<wxGrid
*>(this);
7679 self
->ClearAttrCache();
7680 self
->m_attrCache
.row
= row
;
7681 self
->m_attrCache
.col
= col
;
7682 self
->m_attrCache
.attr
= attr
;
7687 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7689 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7691 *attr
= m_attrCache
.attr
;
7692 wxSafeIncRef(m_attrCache
.attr
);
7694 #ifdef DEBUG_ATTR_CACHE
7695 gs_nAttrCacheHits
++;
7702 #ifdef DEBUG_ATTR_CACHE
7703 gs_nAttrCacheMisses
++;
7710 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7712 wxGridCellAttr
*attr
= NULL
;
7713 // Additional test to avoid looking at the cache e.g. for
7714 // wxNoCellCoords, as this will confuse memory management.
7717 if ( !LookupAttr(row
, col
, &attr
) )
7719 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7721 CacheAttr(row
, col
, attr
);
7727 attr
->SetDefAttr(m_defaultCellAttr
);
7731 attr
= m_defaultCellAttr
;
7738 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7740 wxGridCellAttr
*attr
= NULL
;
7741 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7743 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7744 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7746 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7749 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7751 // artificially inc the ref count to match DecRef() in caller
7753 m_table
->SetAttr(attr
, row
, col
);
7759 // ----------------------------------------------------------------------------
7760 // setting column attributes (wrappers around SetColAttr)
7761 // ----------------------------------------------------------------------------
7763 void wxGrid::SetColFormatBool(int col
)
7765 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7768 void wxGrid::SetColFormatNumber(int col
)
7770 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7773 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7775 wxString typeName
= wxGRID_VALUE_FLOAT
;
7776 if ( (width
!= -1) || (precision
!= -1) )
7778 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7781 SetColFormatCustom(col
, typeName
);
7784 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7786 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7788 attr
= new wxGridCellAttr
;
7789 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7790 attr
->SetRenderer(renderer
);
7791 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7792 attr
->SetEditor(editor
);
7794 SetColAttr(col
, attr
);
7798 // ----------------------------------------------------------------------------
7799 // setting cell attributes: this is forwarded to the table
7800 // ----------------------------------------------------------------------------
7802 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7804 if ( CanHaveAttributes() )
7806 m_table
->SetAttr(attr
, row
, col
);
7815 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7817 if ( CanHaveAttributes() )
7819 m_table
->SetRowAttr(attr
, row
);
7828 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7830 if ( CanHaveAttributes() )
7832 m_table
->SetColAttr(attr
, col
);
7841 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7843 if ( CanHaveAttributes() )
7845 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7846 attr
->SetBackgroundColour(colour
);
7851 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7853 if ( CanHaveAttributes() )
7855 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7856 attr
->SetTextColour(colour
);
7861 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7863 if ( CanHaveAttributes() )
7865 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7866 attr
->SetFont(font
);
7871 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7873 if ( CanHaveAttributes() )
7875 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7876 attr
->SetAlignment(horiz
, vert
);
7881 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7883 if ( CanHaveAttributes() )
7885 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7886 attr
->SetOverflow(allow
);
7891 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7893 if ( CanHaveAttributes() )
7895 int cell_rows
, cell_cols
;
7897 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7898 attr
->GetSize(&cell_rows
, &cell_cols
);
7899 attr
->SetSize(num_rows
, num_cols
);
7902 // Cannot set the size of a cell to 0 or negative values
7903 // While it is perfectly legal to do that, this function cannot
7904 // handle all the possibilies, do it by hand by getting the CellAttr.
7905 // You can only set the size of a cell to 1,1 or greater with this fn
7906 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7907 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7908 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7909 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7911 // if this was already a multicell then "turn off" the other cells first
7912 if ((cell_rows
> 1) || (cell_cols
> 1))
7915 for (j
=row
; j
< row
+ cell_rows
; j
++)
7917 for (i
=col
; i
< col
+ cell_cols
; i
++)
7919 if ((i
!= col
) || (j
!= row
))
7921 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7922 attr_stub
->SetSize( 1, 1 );
7923 attr_stub
->DecRef();
7929 // mark the cells that will be covered by this cell to
7930 // negative or zero values to point back at this cell
7931 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7934 for (j
=row
; j
< row
+ num_rows
; j
++)
7936 for (i
=col
; i
< col
+ num_cols
; i
++)
7938 if ((i
!= col
) || (j
!= row
))
7940 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7941 attr_stub
->SetSize( row
- j
, col
- i
);
7942 attr_stub
->DecRef();
7950 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7952 if ( CanHaveAttributes() )
7954 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7955 attr
->SetRenderer(renderer
);
7960 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7962 if ( CanHaveAttributes() )
7964 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7965 attr
->SetEditor(editor
);
7970 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7972 if ( CanHaveAttributes() )
7974 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7975 attr
->SetReadOnly(isReadOnly
);
7980 // ----------------------------------------------------------------------------
7981 // Data type registration
7982 // ----------------------------------------------------------------------------
7984 void wxGrid::RegisterDataType(const wxString
& typeName
,
7985 wxGridCellRenderer
* renderer
,
7986 wxGridCellEditor
* editor
)
7988 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7992 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7994 wxString typeName
= m_table
->GetTypeName(row
, col
);
7995 return GetDefaultEditorForType(typeName
);
7998 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8000 wxString typeName
= m_table
->GetTypeName(row
, col
);
8001 return GetDefaultRendererForType(typeName
);
8004 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8006 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8007 if ( index
== wxNOT_FOUND
)
8009 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
8014 return m_typeRegistry
->GetEditor(index
);
8017 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8019 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8020 if ( index
== wxNOT_FOUND
)
8022 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
8027 return m_typeRegistry
->GetRenderer(index
);
8030 // ----------------------------------------------------------------------------
8032 // ----------------------------------------------------------------------------
8034 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
8038 setFixed
= new wxGridFixedIndicesSet
;
8041 setFixed
->insert(line
);
8045 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
8047 return !setFixed
|| !setFixed
->count(line
);
8050 void wxGrid::EnableDragRowSize( bool enable
)
8052 m_canDragRowSize
= enable
;
8055 void wxGrid::EnableDragColSize( bool enable
)
8057 m_canDragColSize
= enable
;
8060 void wxGrid::EnableDragGridSize( bool enable
)
8062 m_canDragGridSize
= enable
;
8065 void wxGrid::EnableDragCell( bool enable
)
8067 m_canDragCell
= enable
;
8070 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8072 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
8074 if ( resizeExistingRows
)
8076 // since we are resizing all rows to the default row size,
8077 // we can simply clear the row heights and row bottoms
8078 // arrays (which also allows us to take advantage of
8079 // some speed optimisations)
8080 m_rowHeights
.Empty();
8081 m_rowBottoms
.Empty();
8082 if ( !GetBatchCount() )
8090 // This is a common part of SetRowSize() and SetColSize() which takes care of
8091 // updating the height/width of a row/column depending on its current value and
8094 // Returns the difference between the new and the old size.
8095 int UpdateRowOrColSize(int& sizeCurrent
, int sizeNew
)
8097 // On input here sizeCurrent can be negative if it's currently hidden (the
8098 // real size is its absolute value then). And sizeNew can be 0 to indicate
8099 // that the row/column should be hidden or -1 to indicate that it should be
8104 // We're showing back a previously hidden row/column.
8105 wxASSERT_MSG( sizeNew
== -1, wxS("New size must be positive or -1.") );
8107 wxASSERT_MSG( sizeCurrent
< 0, wxS("May only show back if hidden.") );
8109 sizeCurrent
= -sizeCurrent
;
8111 // This is positive which is correct.
8114 else if ( sizeNew
== 0 )
8116 // We're hiding a row/column.
8117 wxASSERT_MSG( sizeCurrent
> 0, wxS("Can't hide if already hidden.") );
8119 sizeCurrent
= -sizeCurrent
;
8121 // This is negative which is correct.
8124 else // We're just changing the row/column size.
8126 // Here it could have been hidden or not previously.
8127 const int sizeOld
= sizeCurrent
< 0 ? 0 : sizeCurrent
;
8129 sizeCurrent
= sizeNew
;
8131 return sizeCurrent
- sizeOld
;
8135 } // anonymous namespace
8137 void wxGrid::SetRowSize( int row
, int height
)
8139 // See comment in SetColSize
8140 if ( height
> 0 && height
< GetRowMinimalAcceptableHeight())
8143 // The value of -1 is special and means to fit the height to the row label.
8146 // As with the columns, ignore attempts to auto-size the hidden rows.
8147 if ( GetRowHeight(row
) == 0 )
8151 wxArrayString lines
;
8152 wxClientDC
dc(m_rowLabelWin
);
8153 dc
.SetFont(GetLabelFont());
8154 StringToLines(GetRowLabelValue( row
), lines
);
8155 GetTextBoxSize( dc
, lines
, &w
, &h
);
8156 //check that it is not less than the minimal height
8157 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
8160 DoSetRowSize(row
, height
);
8163 void wxGrid::DoSetRowSize( int row
, int height
)
8165 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
8167 if ( m_rowHeights
.IsEmpty() )
8169 // need to really create the array
8173 const int diff
= UpdateRowOrColSize(m_rowHeights
[row
], height
);
8177 for ( int i
= row
; i
< m_numRows
; i
++ )
8179 m_rowBottoms
[i
] += diff
;
8182 InvalidateBestSize();
8184 if ( !GetBatchCount() )
8191 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8193 // we dont allow zero default column width
8194 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
8196 if ( resizeExistingCols
)
8198 // since we are resizing all columns to the default column size,
8199 // we can simply clear the col widths and col rights
8200 // arrays (which also allows us to take advantage of
8201 // some speed optimisations)
8202 m_colWidths
.Empty();
8203 m_colRights
.Empty();
8204 if ( !GetBatchCount() )
8209 void wxGrid::SetColSize( int col
, int width
)
8211 // we intentionally don't test whether the width is less than
8212 // GetColMinimalWidth() here but we do compare it with
8213 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
8214 // #651) -- and we also always allow the width of 0 as it has the special
8215 // sense of hiding the column
8216 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
8219 // The value of -1 is special and means to fit the width to the column label.
8222 // We currently don't support auto-sizing hidden columns. We could, but
8223 // it's not clear whether this is really needed and it would make the
8224 // code more complex.
8225 if ( GetColWidth(col
) == 0 )
8229 wxArrayString lines
;
8230 wxClientDC
dc(m_colWindow
);
8231 dc
.SetFont(GetLabelFont());
8232 StringToLines(GetColLabelValue(col
), lines
);
8233 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
8234 GetTextBoxSize( dc
, lines
, &w
, &h
);
8236 GetTextBoxSize( dc
, lines
, &h
, &w
);
8238 //check that it is not less than the minimal width
8239 width
= wxMax(width
, GetColMinimalAcceptableWidth());
8242 DoSetColSize(col
, width
);
8245 void wxGrid::DoSetColSize( int col
, int width
)
8247 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
8249 if ( m_colWidths
.IsEmpty() )
8251 // need to really create the array
8255 const int diff
= UpdateRowOrColSize(m_colWidths
[col
], width
);
8259 if ( m_useNativeHeader
)
8260 GetGridColHeader()->UpdateColumn(col
);
8261 //else: will be refreshed when the header is redrawn
8263 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
8265 m_colRights
[GetColAt(colPos
)] += diff
;
8268 InvalidateBestSize();
8270 if ( !GetBatchCount() )
8277 void wxGrid::SetColMinimalWidth( int col
, int width
)
8279 if (width
> GetColMinimalAcceptableWidth())
8281 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
8282 m_colMinWidths
[key
] = width
;
8286 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8288 if (width
> GetRowMinimalAcceptableHeight())
8290 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
8291 m_rowMinHeights
[key
] = width
;
8295 int wxGrid::GetColMinimalWidth(int col
) const
8297 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
8298 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
8300 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
8303 int wxGrid::GetRowMinimalHeight(int row
) const
8305 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
8306 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
8308 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
8311 void wxGrid::SetColMinimalAcceptableWidth( int width
)
8313 // We do allow a width of 0 since this gives us
8314 // an easy way to temporarily hiding columns.
8316 m_minAcceptableColWidth
= width
;
8319 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
8321 // We do allow a height of 0 since this gives us
8322 // an easy way to temporarily hiding rows.
8324 m_minAcceptableRowHeight
= height
;
8327 int wxGrid::GetColMinimalAcceptableWidth() const
8329 return m_minAcceptableColWidth
;
8332 int wxGrid::GetRowMinimalAcceptableHeight() const
8334 return m_minAcceptableRowHeight
;
8337 // ----------------------------------------------------------------------------
8339 // ----------------------------------------------------------------------------
8342 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
8344 const bool column
= direction
== wxGRID_COLUMN
;
8346 // We don't support auto-sizing hidden rows or columns, this doesn't seem
8347 // to make much sense.
8350 if ( GetColWidth(colOrRow
) == 0 )
8355 if ( GetRowHeight(colOrRow
) == 0 )
8359 wxClientDC
dc(m_gridWin
);
8361 // cancel editing of cell
8362 HideCellEditControl();
8363 SaveEditControlValue();
8365 // initialize both of them just to avoid compiler warnings even if only
8366 // really needs to be initialized here
8380 wxCoord extent
, extentMax
= 0;
8381 int max
= column
? m_numRows
: m_numCols
;
8382 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8395 // we need to account for the cells spanning multiple columns/rows:
8396 // while they may need a lot of space, they don't need all of it in
8398 int numRows
, numCols
;
8399 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
8400 if ( span
== CellSpan_Inside
)
8402 // we need to get the size of the main cell, not of a cell hidden
8407 // get the size of the main cell too
8408 GetCellSize(row
, col
, &numRows
, &numCols
);
8411 // get cell ( main cell if CellSpan_Inside ) renderer best size
8412 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8413 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
8416 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8417 extent
= column
? size
.x
: size
.y
;
8419 if ( span
!= CellSpan_None
)
8421 // we spread the size of a spanning cell over all the cells it
8422 // covers evenly -- this is probably not ideal but we can't
8423 // really do much better here
8425 // notice that numCols and numRows are never 0 as they
8426 // correspond to the size of the main cell of the span and not
8427 // of the cell inside it
8428 extent
/= column
? numCols
: numRows
;
8431 if ( extent
> extentMax
)
8440 // now also compare with the column label extent
8442 dc
.SetFont( GetLabelFont() );
8446 dc
.GetMultiLineTextExtent( GetColLabelValue(colOrRow
), &w
, &h
);
8447 if ( GetColLabelTextOrientation() == wxVERTICAL
)
8451 dc
.GetMultiLineTextExtent( GetRowLabelValue(colOrRow
), &w
, &h
);
8453 extent
= column
? w
: h
;
8454 if ( extent
> extentMax
)
8459 // empty column - give default extent (notice that if extentMax is less
8460 // than default extent but != 0, it's OK)
8461 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8466 // leave some space around text
8474 // Ensure automatic width is not less than minimal width. See the
8475 // comment in SetColSize() for explanation of why this isn't done
8478 extentMax
= wxMax(extentMax
, GetColMinimalWidth(colOrRow
));
8480 SetColSize( colOrRow
, extentMax
);
8481 if ( !GetBatchCount() )
8483 if ( m_useNativeHeader
)
8485 GetGridColHeader()->UpdateColumn(colOrRow
);
8490 m_gridWin
->GetClientSize( &cw
, &ch
);
8491 wxRect
rect ( CellToRect( 0, colOrRow
) );
8493 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8494 rect
.width
= cw
- rect
.x
;
8495 rect
.height
= m_colLabelHeight
;
8496 GetColLabelWindow()->Refresh( true, &rect
);
8502 // Ensure automatic width is not less than minimal height. See the
8503 // comment in SetColSize() for explanation of why this isn't done
8506 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(colOrRow
));
8508 SetRowSize(colOrRow
, extentMax
);
8509 if ( !GetBatchCount() )
8512 m_gridWin
->GetClientSize( &cw
, &ch
);
8513 wxRect
rect( CellToRect( colOrRow
, 0 ) );
8515 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8516 rect
.width
= m_rowLabelWidth
;
8517 rect
.height
= ch
- rect
.y
;
8518 m_rowLabelWin
->Refresh( true, &rect
);
8525 SetColMinimalWidth(colOrRow
, extentMax
);
8527 SetRowMinimalHeight(colOrRow
, extentMax
);
8531 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
8533 // calculate size for the rows or columns?
8534 const bool calcRows
= direction
== wxGRID_ROW
;
8536 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
8537 : GetGridColLabelWindow());
8538 dc
.SetFont(GetLabelFont());
8540 // which dimension should we take into account for calculations?
8542 // for columns, the text can be only horizontal so it's easy but for rows
8543 // we also have to take into account the text orientation
8545 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
8547 wxArrayString lines
;
8548 wxCoord extentMax
= 0;
8550 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
8551 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
8555 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
8556 : GetColLabelValue(rowOrCol
);
8557 StringToLines(label
, lines
);
8560 GetTextBoxSize(dc
, lines
, &w
, &h
);
8562 const wxCoord extent
= useWidth
? w
: h
;
8563 if ( extent
> extentMax
)
8569 // empty column - give default extent (notice that if extentMax is less
8570 // than default extent but != 0, it's OK)
8571 extentMax
= calcRows
? GetDefaultRowLabelSize()
8572 : GetDefaultColLabelSize();
8575 // leave some space around text (taken from AutoSizeColOrRow)
8584 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8586 int width
= m_rowLabelWidth
;
8588 wxGridUpdateLocker locker
;
8590 locker
.Create(this);
8592 for ( int col
= 0; col
< m_numCols
; col
++ )
8595 AutoSizeColumn(col
, setAsMin
);
8597 width
+= GetColWidth(col
);
8603 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8605 int height
= m_colLabelHeight
;
8607 wxGridUpdateLocker locker
;
8609 locker
.Create(this);
8611 for ( int row
= 0; row
< m_numRows
; row
++ )
8614 AutoSizeRow(row
, setAsMin
);
8616 height
+= GetRowHeight(row
);
8622 void wxGrid::AutoSize()
8624 wxGridUpdateLocker
locker(this);
8626 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8627 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8629 // we know that we're not going to have scrollbars so disable them now to
8630 // avoid trouble in SetClientSize() which can otherwise set the correct
8631 // client size but also leave space for (not needed any more) scrollbars
8632 SetScrollbars(m_xScrollPixelsPerLine
, m_yScrollPixelsPerLine
,
8635 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8638 void wxGrid::AutoSizeRowLabelSize( int row
)
8640 // Hide the edit control, so it
8641 // won't interfere with drag-shrinking.
8642 if ( IsCellEditControlShown() )
8644 HideCellEditControl();
8645 SaveEditControlValue();
8648 // autosize row height depending on label text
8649 SetRowSize(row
, -1);
8654 void wxGrid::AutoSizeColLabelSize( int col
)
8656 // Hide the edit control, so it
8657 // won't interfere with drag-shrinking.
8658 if ( IsCellEditControlShown() )
8660 HideCellEditControl();
8661 SaveEditControlValue();
8664 // autosize column width depending on label text
8665 SetColSize(col
, -1);
8670 wxSize
wxGrid::DoGetBestSize() const
8672 wxGrid
* const self
= const_cast<wxGrid
*>(this);
8674 // we do the same as in AutoSize() here with the exception that we don't
8675 // change the column/row sizes, only calculate them
8676 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8677 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8679 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8680 + GetWindowBorderSize();
8688 #if WXWIN_COMPATIBILITY_2_8
8689 wxPen
& wxGrid::GetDividerPen() const
8693 #endif // WXWIN_COMPATIBILITY_2_8
8695 // ----------------------------------------------------------------------------
8696 // cell value accessor functions
8697 // ----------------------------------------------------------------------------
8699 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8703 m_table
->SetValue( row
, col
, s
);
8704 if ( !GetBatchCount() )
8707 wxRect
rect( CellToRect( row
, col
) );
8709 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8710 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8711 m_gridWin
->Refresh( false, &rect
);
8714 if ( m_currentCellCoords
.GetRow() == row
&&
8715 m_currentCellCoords
.GetCol() == col
&&
8716 IsCellEditControlShown())
8717 // Note: If we are using IsCellEditControlEnabled,
8718 // this interacts badly with calling SetCellValue from
8719 // an EVT_GRID_CELL_CHANGE handler.
8721 HideCellEditControl();
8722 ShowCellEditControl(); // will reread data from table
8727 // ----------------------------------------------------------------------------
8728 // block, row and column selection
8729 // ----------------------------------------------------------------------------
8731 void wxGrid::SelectRow( int row
, bool addToSelected
)
8736 if ( !addToSelected
)
8739 m_selection
->SelectRow(row
);
8742 void wxGrid::SelectCol( int col
, bool addToSelected
)
8747 if ( !addToSelected
)
8750 m_selection
->SelectCol(col
);
8753 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8759 if ( !addToSelected
)
8762 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8765 void wxGrid::SelectAll()
8767 if ( m_numRows
> 0 && m_numCols
> 0 )
8770 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8774 // ----------------------------------------------------------------------------
8775 // cell, row and col deselection
8776 // ----------------------------------------------------------------------------
8778 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8783 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8784 if ( mode
== oper
.GetSelectionMode() ||
8785 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8787 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8788 if ( m_selection
->IsInSelection(c
) )
8789 m_selection
->ToggleCellSelection(c
);
8791 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8793 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8794 for ( int i
= 0; i
< nOther
; i
++ )
8796 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8797 if ( m_selection
->IsInSelection(c
) )
8798 m_selection
->ToggleCellSelection(c
);
8801 //else: can only select orthogonal lines so no lines in this direction
8802 // could have been selected anyhow
8805 void wxGrid::DeselectRow(int row
)
8807 DeselectLine(row
, wxGridRowOperations());
8810 void wxGrid::DeselectCol(int col
)
8812 DeselectLine(col
, wxGridColumnOperations());
8815 void wxGrid::DeselectCell( int row
, int col
)
8817 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8818 m_selection
->ToggleCellSelection(row
, col
);
8821 bool wxGrid::IsSelection() const
8823 return ( m_selection
&& (m_selection
->IsSelection() ||
8824 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8825 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8828 bool wxGrid::IsInSelection( int row
, int col
) const
8830 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8831 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8832 col
>= m_selectedBlockTopLeft
.GetCol() &&
8833 row
<= m_selectedBlockBottomRight
.GetRow() &&
8834 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8837 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8841 wxGridCellCoordsArray a
;
8845 return m_selection
->m_cellSelection
;
8848 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8852 wxGridCellCoordsArray a
;
8856 return m_selection
->m_blockSelectionTopLeft
;
8859 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8863 wxGridCellCoordsArray a
;
8867 return m_selection
->m_blockSelectionBottomRight
;
8870 wxArrayInt
wxGrid::GetSelectedRows() const
8878 return m_selection
->m_rowSelection
;
8881 wxArrayInt
wxGrid::GetSelectedCols() const
8889 return m_selection
->m_colSelection
;
8892 void wxGrid::ClearSelection()
8894 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8895 m_selectedBlockBottomRight
);
8896 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8897 m_selectedBlockCorner
);
8899 m_selectedBlockTopLeft
=
8900 m_selectedBlockBottomRight
=
8901 m_selectedBlockCorner
= wxGridNoCellCoords
;
8903 if ( !r1
.IsEmpty() )
8904 RefreshRect(r1
, false);
8905 if ( !r2
.IsEmpty() )
8906 RefreshRect(r2
, false);
8909 m_selection
->ClearSelection();
8912 // This function returns the rectangle that encloses the given block
8913 // in device coords clipped to the client size of the grid window.
8915 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8916 const wxGridCellCoords
& bottomRight
) const
8919 wxRect tempCellRect
= CellToRect(topLeft
);
8920 if ( tempCellRect
!= wxGridNoCellRect
)
8922 resultRect
= tempCellRect
;
8926 resultRect
= wxRect(0, 0, 0, 0);
8929 tempCellRect
= CellToRect(bottomRight
);
8930 if ( tempCellRect
!= wxGridNoCellRect
)
8932 resultRect
+= tempCellRect
;
8936 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8937 return wxGridNoCellRect
;
8940 // Ensure that left/right and top/bottom pairs are in order.
8941 int left
= resultRect
.GetLeft();
8942 int top
= resultRect
.GetTop();
8943 int right
= resultRect
.GetRight();
8944 int bottom
= resultRect
.GetBottom();
8946 int leftCol
= topLeft
.GetCol();
8947 int topRow
= topLeft
.GetRow();
8948 int rightCol
= bottomRight
.GetCol();
8949 int bottomRow
= bottomRight
.GetRow();
8973 // The following loop is ONLY necessary to detect and handle merged cells.
8975 m_gridWin
->GetClientSize( &cw
, &ch
);
8977 // Get the origin coordinates: notice that they will be negative if the
8978 // grid is scrolled downwards/to the right.
8979 int gridOriginX
= 0;
8980 int gridOriginY
= 0;
8981 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8983 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8984 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8986 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8987 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8989 // Bound our loop so that we only examine the portion of the selected block
8990 // that is shown on screen. Therefore, we compare the Top-Left block values
8991 // to the Top-Left screen values, and the Bottom-Right block values to the
8992 // Bottom-Right screen values, choosing appropriately.
8993 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8994 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8995 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8996 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8998 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
9000 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
9002 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
9003 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
9005 tempCellRect
= CellToRect( j
, i
);
9007 if (tempCellRect
.x
< left
)
9008 left
= tempCellRect
.x
;
9009 if (tempCellRect
.y
< top
)
9010 top
= tempCellRect
.y
;
9011 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
9012 right
= tempCellRect
.x
+ tempCellRect
.width
;
9013 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
9014 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
9018 i
= visibleRightCol
; // jump over inner cells.
9023 // Convert to scrolled coords
9024 CalcScrolledPosition( left
, top
, &left
, &top
);
9025 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9027 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9028 return wxRect(0,0,0,0);
9030 resultRect
.SetLeft( wxMax(0, left
) );
9031 resultRect
.SetTop( wxMax(0, top
) );
9032 resultRect
.SetRight( wxMin(cw
, right
) );
9033 resultRect
.SetBottom( wxMin(ch
, bottom
) );
9038 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
9039 const wxGridOperations
& oper
)
9042 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
9043 const int numLines
= oper
.GetNumberOfLines(this);
9044 for ( int i
= 0; i
< numLines
; i
++ )
9046 int size
= sizeInfo
.GetSize(i
);
9047 if ( size
!= sizeInfo
.m_sizeDefault
)
9048 oper
.SetLineSize(this, i
, size
);
9053 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
9055 DoSetSizes(sizeInfo
, wxGridColumnOperations());
9058 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
9060 DoSetSizes(sizeInfo
, wxGridRowOperations());
9063 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
9065 m_sizeDefault
= defSize
;
9066 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
9068 if ( allSizes
[i
] != defSize
)
9069 m_customSizes
[i
] = allSizes
[i
];
9073 int wxGridSizesInfo::GetSize(unsigned pos
) const
9075 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
9077 // if it's not found return the default
9078 if ( it
== m_customSizes
.end() )
9079 return m_sizeDefault
;
9081 // otherwise return 0 if it's hidden, currently there is no way to get
9082 // its size before it had been hidden
9083 if ( it
->second
< 0 )
9089 // ----------------------------------------------------------------------------
9091 // ----------------------------------------------------------------------------
9093 #if wxUSE_DRAG_AND_DROP
9095 // this allow setting drop target directly on wxGrid
9096 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
9098 GetGridWindow()->SetDropTarget(dropTarget
);
9101 #endif // wxUSE_DRAG_AND_DROP
9103 // ----------------------------------------------------------------------------
9104 // grid event classes
9105 // ----------------------------------------------------------------------------
9107 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9109 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9110 int row
, int col
, int x
, int y
, bool sel
,
9111 bool control
, bool shift
, bool alt
, bool meta
)
9112 : wxNotifyEvent( type
, id
),
9113 wxKeyboardState(control
, shift
, alt
, meta
)
9115 Init(row
, col
, x
, y
, sel
);
9117 SetEventObject(obj
);
9120 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9122 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9123 int rowOrCol
, int x
, int y
,
9124 bool control
, bool shift
, bool alt
, bool meta
)
9125 : wxNotifyEvent( type
, id
),
9126 wxKeyboardState(control
, shift
, alt
, meta
)
9128 Init(rowOrCol
, x
, y
);
9130 SetEventObject(obj
);
9134 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9136 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9137 const wxGridCellCoords
& topLeft
,
9138 const wxGridCellCoords
& bottomRight
,
9139 bool sel
, bool control
,
9140 bool shift
, bool alt
, bool meta
)
9141 : wxNotifyEvent( type
, id
),
9142 wxKeyboardState(control
, shift
, alt
, meta
)
9144 Init(topLeft
, bottomRight
, sel
);
9146 SetEventObject(obj
);
9150 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9152 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9153 wxObject
* obj
, int row
,
9154 int col
, wxControl
* ctrl
)
9155 : wxCommandEvent(type
, id
)
9157 SetEventObject(obj
);
9164 // ----------------------------------------------------------------------------
9165 // wxGridTypeRegistry
9166 // ----------------------------------------------------------------------------
9168 wxGridTypeRegistry::~wxGridTypeRegistry()
9170 size_t count
= m_typeinfo
.GetCount();
9171 for ( size_t i
= 0; i
< count
; i
++ )
9172 delete m_typeinfo
[i
];
9175 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
9176 wxGridCellRenderer
* renderer
,
9177 wxGridCellEditor
* editor
)
9179 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
9181 // is it already registered?
9182 int loc
= FindRegisteredDataType(typeName
);
9183 if ( loc
!= wxNOT_FOUND
)
9185 delete m_typeinfo
[loc
];
9186 m_typeinfo
[loc
] = info
;
9190 m_typeinfo
.Add(info
);
9194 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
9196 size_t count
= m_typeinfo
.GetCount();
9197 for ( size_t i
= 0; i
< count
; i
++ )
9199 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
9208 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
9210 int index
= FindRegisteredDataType(typeName
);
9211 if ( index
== wxNOT_FOUND
)
9213 // check whether this is one of the standard ones, in which case
9214 // register it "on the fly"
9216 if ( typeName
== wxGRID_VALUE_STRING
)
9218 RegisterDataType(wxGRID_VALUE_STRING
,
9219 new wxGridCellStringRenderer
,
9220 new wxGridCellTextEditor
);
9223 #endif // wxUSE_TEXTCTRL
9225 if ( typeName
== wxGRID_VALUE_BOOL
)
9227 RegisterDataType(wxGRID_VALUE_BOOL
,
9228 new wxGridCellBoolRenderer
,
9229 new wxGridCellBoolEditor
);
9232 #endif // wxUSE_CHECKBOX
9234 if ( typeName
== wxGRID_VALUE_NUMBER
)
9236 RegisterDataType(wxGRID_VALUE_NUMBER
,
9237 new wxGridCellNumberRenderer
,
9238 new wxGridCellNumberEditor
);
9240 else if ( typeName
== wxGRID_VALUE_FLOAT
)
9242 RegisterDataType(wxGRID_VALUE_FLOAT
,
9243 new wxGridCellFloatRenderer
,
9244 new wxGridCellFloatEditor
);
9247 #endif // wxUSE_TEXTCTRL
9249 if ( typeName
== wxGRID_VALUE_CHOICE
)
9251 RegisterDataType(wxGRID_VALUE_CHOICE
,
9252 new wxGridCellStringRenderer
,
9253 new wxGridCellChoiceEditor
);
9256 #endif // wxUSE_COMBOBOX
9261 // we get here only if just added the entry for this type, so return
9263 index
= m_typeinfo
.GetCount() - 1;
9269 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
9271 int index
= FindDataType(typeName
);
9272 if ( index
== wxNOT_FOUND
)
9274 // the first part of the typename is the "real" type, anything after ':'
9275 // are the parameters for the renderer
9276 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
9277 if ( index
== wxNOT_FOUND
)
9282 wxGridCellRenderer
*renderer
= GetRenderer(index
);
9283 wxGridCellRenderer
*rendererOld
= renderer
;
9284 renderer
= renderer
->Clone();
9285 rendererOld
->DecRef();
9287 wxGridCellEditor
*editor
= GetEditor(index
);
9288 wxGridCellEditor
*editorOld
= editor
;
9289 editor
= editor
->Clone();
9290 editorOld
->DecRef();
9292 // do it even if there are no parameters to reset them to defaults
9293 wxString params
= typeName
.AfterFirst(wxT(':'));
9294 renderer
->SetParameters(params
);
9295 editor
->SetParameters(params
);
9297 // register the new typename
9298 RegisterDataType(typeName
, renderer
, editor
);
9300 // we just registered it, it's the last one
9301 index
= m_typeinfo
.GetCount() - 1;
9307 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
9309 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
9316 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
9318 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
9325 #endif // wxUSE_GRID