1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
58 const char wxGridNameStr
[] = "grid";
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_MOTIF(identifier) identifier
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
69 #define WXUNUSED_GTK(identifier) identifier
72 // Required for wxIs... functions
75 WX_DECLARE_HASH_SET_WITH_DECL(int, wxIntegerHash
, wxIntegerEqual
,
76 wxGridFixedIndicesSet
, class WXDLLIMPEXP_ADV
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 //#define DEBUG_ATTR_CACHE
84 #ifdef DEBUG_ATTR_CACHE
85 static size_t gs_nAttrCacheHits
= 0;
86 static size_t gs_nAttrCacheMisses
= 0;
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
94 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
100 const size_t GRID_SCROLL_LINE_X
= 15;
101 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
103 // the size of hash tables used a bit everywhere (the max number of elements
104 // in these hash tables is the number of rows/columns)
105 const int GRID_HASH_SIZE
= 100;
107 // the minimal distance in pixels the mouse needs to move to start a drag
109 const int DRAG_SENSITIVITY
= 3;
111 } // anonymous namespace
113 #include "wx/arrimpl.cpp"
115 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
116 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK
, wxGridEvent
);
123 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK
, wxGridEvent
);
124 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK
, wxGridEvent
);
125 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK
, wxGridEvent
);
126 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG
, wxGridEvent
);
127 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK
, wxGridEvent
);
128 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK
, wxGridEvent
);
129 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK
, wxGridEvent
);
130 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK
, wxGridEvent
);
131 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE
, wxGridSizeEvent
);
132 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE
, wxGridSizeEvent
);
133 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE
, wxGridEvent
);
134 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
135 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
136 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
137 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
138 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
139 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
140 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
141 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
143 // ============================================================================
145 // ============================================================================
147 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
149 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
150 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
151 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
152 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
155 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
156 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
158 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
159 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
160 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
162 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
163 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
166 wxGridOperations
& wxGridRowOperations::Dual() const
168 static wxGridColumnOperations s_colOper
;
173 wxGridOperations
& wxGridColumnOperations::Dual() const
175 static wxGridRowOperations s_rowOper
;
180 // ----------------------------------------------------------------------------
181 // wxGridCellWorker is an (almost) empty common base class for
182 // wxGridCellRenderer and wxGridCellEditor managing ref counting
183 // ----------------------------------------------------------------------------
185 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
190 wxGridCellWorker::~wxGridCellWorker()
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
202 m_isReadOnly
= Unset
;
207 m_attrkind
= wxGridCellAttr::Cell
;
209 m_sizeRows
= m_sizeCols
= 1;
210 m_overflow
= UnsetOverflow
;
212 SetDefAttr(attrDefault
);
215 wxGridCellAttr
*wxGridCellAttr::Clone() const
217 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
219 if ( HasTextColour() )
220 attr
->SetTextColour(GetTextColour());
221 if ( HasBackgroundColour() )
222 attr
->SetBackgroundColour(GetBackgroundColour());
224 attr
->SetFont(GetFont());
225 if ( HasAlignment() )
226 attr
->SetAlignment(m_hAlign
, m_vAlign
);
228 attr
->SetSize( m_sizeRows
, m_sizeCols
);
232 attr
->SetRenderer(m_renderer
);
233 m_renderer
->IncRef();
237 attr
->SetEditor(m_editor
);
244 attr
->SetOverflow( m_overflow
== Overflow
);
245 attr
->SetKind( m_attrkind
);
250 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
252 if ( !HasTextColour() && mergefrom
->HasTextColour() )
253 SetTextColour(mergefrom
->GetTextColour());
254 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
255 SetBackgroundColour(mergefrom
->GetBackgroundColour());
256 if ( !HasFont() && mergefrom
->HasFont() )
257 SetFont(mergefrom
->GetFont());
258 if ( !HasAlignment() && mergefrom
->HasAlignment() )
261 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
262 SetAlignment(hAlign
, vAlign
);
264 if ( !HasSize() && mergefrom
->HasSize() )
265 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
267 // Directly access member functions as GetRender/Editor don't just return
268 // m_renderer/m_editor
270 // Maybe add support for merge of Render and Editor?
271 if (!HasRenderer() && mergefrom
->HasRenderer() )
273 m_renderer
= mergefrom
->m_renderer
;
274 m_renderer
->IncRef();
276 if ( !HasEditor() && mergefrom
->HasEditor() )
278 m_editor
= mergefrom
->m_editor
;
281 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
282 SetReadOnly(mergefrom
->IsReadOnly());
284 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
285 SetOverflow(mergefrom
->GetOverflow());
287 SetDefAttr(mergefrom
->m_defGridAttr
);
290 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
292 // The size of a cell is normally 1,1
294 // If this cell is larger (2,2) then this is the top left cell
295 // the other cells that will be covered (lower right cells) must be
296 // set to negative or zero values such that
297 // row + num_rows of the covered cell points to the larger cell (this cell)
298 // same goes for the col + num_cols.
300 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
302 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
303 !((num_rows
<= 0) && (num_cols
> 0)) ||
304 !((num_rows
== 0) && (num_cols
== 0))),
305 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
307 m_sizeRows
= num_rows
;
308 m_sizeCols
= num_cols
;
311 const wxColour
& wxGridCellAttr::GetTextColour() const
317 else if (m_defGridAttr
&& m_defGridAttr
!= this)
319 return m_defGridAttr
->GetTextColour();
323 wxFAIL_MSG(wxT("Missing default cell attribute"));
328 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
330 if (HasBackgroundColour())
334 else if (m_defGridAttr
&& m_defGridAttr
!= this)
336 return m_defGridAttr
->GetBackgroundColour();
340 wxFAIL_MSG(wxT("Missing default cell attribute"));
345 const wxFont
& wxGridCellAttr::GetFont() const
351 else if (m_defGridAttr
&& m_defGridAttr
!= this)
353 return m_defGridAttr
->GetFont();
357 wxFAIL_MSG(wxT("Missing default cell attribute"));
362 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
371 else if (m_defGridAttr
&& m_defGridAttr
!= this)
373 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
377 wxFAIL_MSG(wxT("Missing default cell attribute"));
381 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
384 *num_rows
= m_sizeRows
;
386 *num_cols
= m_sizeCols
;
389 // GetRenderer and GetEditor use a slightly different decision path about
390 // which attribute to use. If a non-default attr object has one then it is
391 // used, otherwise the default editor or renderer is fetched from the grid and
392 // used. It should be the default for the data type of the cell. If it is
393 // NULL (because the table has a type that the grid does not have in its
394 // registry), then the grid's default editor or renderer is used.
396 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
398 wxGridCellRenderer
*renderer
= NULL
;
400 if ( m_renderer
&& this != m_defGridAttr
)
402 // use the cells renderer if it has one
403 renderer
= m_renderer
;
406 else // no non-default cell renderer
408 // get default renderer for the data type
411 // GetDefaultRendererForCell() will do IncRef() for us
412 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
415 if ( renderer
== NULL
)
417 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
419 // if we still don't have one then use the grid default
420 // (no need for IncRef() here neither)
421 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
423 else // default grid attr
425 // use m_renderer which we had decided not to use initially
426 renderer
= m_renderer
;
433 // we're supposed to always find something
434 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
439 // same as above, except for s/renderer/editor/g
440 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
442 wxGridCellEditor
*editor
= NULL
;
444 if ( m_editor
&& this != m_defGridAttr
)
446 // use the cells editor if it has one
450 else // no non default cell editor
452 // get default editor for the data type
455 // GetDefaultEditorForCell() will do IncRef() for us
456 editor
= grid
->GetDefaultEditorForCell(row
, col
);
459 if ( editor
== NULL
)
461 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
463 // if we still don't have one then use the grid default
464 // (no need for IncRef() here neither)
465 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
467 else // default grid attr
469 // use m_editor which we had decided not to use initially
477 // we're supposed to always find something
478 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
483 // ----------------------------------------------------------------------------
484 // wxGridCellAttrData
485 // ----------------------------------------------------------------------------
487 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
489 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
490 // touch attribute's reference counting explicitly, since this
491 // is managed by class wxGridCellWithAttr
492 int n
= FindIndex(row
, col
);
493 if ( n
== wxNOT_FOUND
)
498 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
500 //else: nothing to do
502 else // we already have an attribute for this cell
506 // change the attribute
507 m_attrs
[(size_t)n
].ChangeAttr(attr
);
511 // remove this attribute
512 m_attrs
.RemoveAt((size_t)n
);
517 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
519 wxGridCellAttr
*attr
= NULL
;
521 int n
= FindIndex(row
, col
);
522 if ( n
!= wxNOT_FOUND
)
524 attr
= m_attrs
[(size_t)n
].attr
;
531 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
533 size_t count
= m_attrs
.GetCount();
534 for ( size_t n
= 0; n
< count
; n
++ )
536 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
537 wxCoord row
= coords
.GetRow();
538 if ((size_t)row
>= pos
)
542 // If rows inserted, include row counter where necessary
543 coords
.SetRow(row
+ numRows
);
545 else if (numRows
< 0)
547 // If rows deleted ...
548 if ((size_t)row
>= pos
- numRows
)
550 // ...either decrement row counter (if row still exists)...
551 coords
.SetRow(row
+ numRows
);
555 // ...or remove the attribute
565 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
567 size_t count
= m_attrs
.GetCount();
568 for ( size_t n
= 0; n
< count
; n
++ )
570 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
571 wxCoord col
= coords
.GetCol();
572 if ( (size_t)col
>= pos
)
576 // If rows inserted, include row counter where necessary
577 coords
.SetCol(col
+ numCols
);
579 else if (numCols
< 0)
581 // If rows deleted ...
582 if ((size_t)col
>= pos
- numCols
)
584 // ...either decrement row counter (if row still exists)...
585 coords
.SetCol(col
+ numCols
);
589 // ...or remove the attribute
599 int wxGridCellAttrData::FindIndex(int row
, int col
) const
601 size_t count
= m_attrs
.GetCount();
602 for ( size_t n
= 0; n
< count
; n
++ )
604 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
605 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
614 // ----------------------------------------------------------------------------
615 // wxGridRowOrColAttrData
616 // ----------------------------------------------------------------------------
618 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
620 size_t count
= m_attrs
.GetCount();
621 for ( size_t n
= 0; n
< count
; n
++ )
623 m_attrs
[n
]->DecRef();
627 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
629 wxGridCellAttr
*attr
= NULL
;
631 int n
= m_rowsOrCols
.Index(rowOrCol
);
632 if ( n
!= wxNOT_FOUND
)
634 attr
= m_attrs
[(size_t)n
];
641 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
643 int i
= m_rowsOrCols
.Index(rowOrCol
);
644 if ( i
== wxNOT_FOUND
)
648 // add the attribute - no need to do anything to reference count
649 // since we take ownership of the attribute.
650 m_rowsOrCols
.Add(rowOrCol
);
657 size_t n
= (size_t)i
;
658 if ( m_attrs
[n
] == attr
)
663 // change the attribute, handling reference count manually,
664 // taking ownership of the new attribute.
665 m_attrs
[n
]->DecRef();
670 // remove this attribute, handling reference count manually
671 m_attrs
[n
]->DecRef();
672 m_rowsOrCols
.RemoveAt(n
);
678 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
680 size_t count
= m_attrs
.GetCount();
681 for ( size_t n
= 0; n
< count
; n
++ )
683 int & rowOrCol
= m_rowsOrCols
[n
];
684 if ( (size_t)rowOrCol
>= pos
)
686 if ( numRowsOrCols
> 0 )
688 // If rows inserted, include row counter where necessary
689 rowOrCol
+= numRowsOrCols
;
691 else if ( numRowsOrCols
< 0)
693 // If rows deleted, either decrement row counter (if row still exists)
694 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
695 rowOrCol
+= numRowsOrCols
;
698 m_rowsOrCols
.RemoveAt(n
);
699 m_attrs
[n
]->DecRef();
709 // ----------------------------------------------------------------------------
710 // wxGridCellAttrProvider
711 // ----------------------------------------------------------------------------
713 wxGridCellAttrProvider::wxGridCellAttrProvider()
718 wxGridCellAttrProvider::~wxGridCellAttrProvider()
723 void wxGridCellAttrProvider::InitData()
725 m_data
= new wxGridCellAttrProviderData
;
728 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
729 wxGridCellAttr::wxAttrKind kind
) const
731 wxGridCellAttr
*attr
= NULL
;
736 case (wxGridCellAttr::Any
):
737 // Get cached merge attributes.
738 // Currently not used as no cache implemented as not mutable
739 // attr = m_data->m_mergeAttr.GetAttr(row, col);
742 // Basically implement old version.
743 // Also check merge cache, so we don't have to re-merge every time..
744 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
745 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
746 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
748 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
750 // Two or more are non NULL
751 attr
= new wxGridCellAttr
;
752 attr
->SetKind(wxGridCellAttr::Merged
);
754 // Order is important..
757 attr
->MergeWith(attrcell
);
762 attr
->MergeWith(attrcol
);
767 attr
->MergeWith(attrrow
);
771 // store merge attr if cache implemented
773 //m_data->m_mergeAttr.SetAttr(attr, row, col);
777 // one or none is non null return it or null.
796 case (wxGridCellAttr::Cell
):
797 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
800 case (wxGridCellAttr::Col
):
801 attr
= m_data
->m_colAttrs
.GetAttr(col
);
804 case (wxGridCellAttr::Row
):
805 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
810 // (wxGridCellAttr::Default):
811 // (wxGridCellAttr::Merged):
819 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
825 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
828 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
833 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
836 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
841 m_data
->m_colAttrs
.SetAttr(attr
, col
);
844 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
848 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
850 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
854 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
858 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
860 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
864 // ----------------------------------------------------------------------------
866 // ----------------------------------------------------------------------------
868 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
870 wxGridTableBase::wxGridTableBase()
873 m_attrProvider
= NULL
;
876 wxGridTableBase::~wxGridTableBase()
878 delete m_attrProvider
;
881 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
883 delete m_attrProvider
;
884 m_attrProvider
= attrProvider
;
887 bool wxGridTableBase::CanHaveAttributes()
889 if ( ! GetAttrProvider() )
891 // use the default attr provider by default
892 SetAttrProvider(new wxGridCellAttrProvider
);
898 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
900 if ( m_attrProvider
)
901 return m_attrProvider
->GetAttr(row
, col
, kind
);
906 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
908 if ( m_attrProvider
)
911 attr
->SetKind(wxGridCellAttr::Cell
);
912 m_attrProvider
->SetAttr(attr
, row
, col
);
916 // as we take ownership of the pointer and don't store it, we must
922 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
924 if ( m_attrProvider
)
926 attr
->SetKind(wxGridCellAttr::Row
);
927 m_attrProvider
->SetRowAttr(attr
, row
);
931 // as we take ownership of the pointer and don't store it, we must
937 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
939 if ( m_attrProvider
)
941 attr
->SetKind(wxGridCellAttr::Col
);
942 m_attrProvider
->SetColAttr(attr
, col
);
946 // as we take ownership of the pointer and don't store it, we must
952 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
953 size_t WXUNUSED(numRows
) )
955 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
960 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
962 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
967 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
968 size_t WXUNUSED(numRows
) )
970 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
975 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
976 size_t WXUNUSED(numCols
) )
978 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
983 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
985 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
990 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
991 size_t WXUNUSED(numCols
) )
993 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
998 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1002 // RD: Starting the rows at zero confuses users,
1003 // no matter how much it makes sense to us geeks.
1009 wxString
wxGridTableBase::GetColLabelValue( int col
)
1011 // default col labels are:
1012 // cols 0 to 25 : A-Z
1013 // cols 26 to 675 : AA-ZZ
1018 for ( n
= 1; ; n
++ )
1020 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
1026 // reverse the string...
1028 for ( i
= 0; i
< n
; i
++ )
1036 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1038 return wxGRID_VALUE_STRING
;
1041 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1042 const wxString
& typeName
)
1044 return typeName
== wxGRID_VALUE_STRING
;
1047 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1049 return CanGetValueAs(row
, col
, typeName
);
1052 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1057 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1062 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1067 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1068 long WXUNUSED(value
) )
1072 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1073 double WXUNUSED(value
) )
1077 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1078 bool WXUNUSED(value
) )
1082 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1083 const wxString
& WXUNUSED(typeName
) )
1088 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1089 const wxString
& WXUNUSED(typeName
),
1090 void* WXUNUSED(value
) )
1094 //////////////////////////////////////////////////////////////////////
1096 // Message class for the grid table to send requests and notifications
1100 wxGridTableMessage::wxGridTableMessage()
1108 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1109 int commandInt1
, int commandInt2
)
1113 m_comInt1
= commandInt1
;
1114 m_comInt2
= commandInt2
;
1117 //////////////////////////////////////////////////////////////////////
1119 // A basic grid table for string data. An object of this class will
1120 // created by wxGrid if you don't specify an alternative table class.
1123 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1125 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1127 wxGridStringTable::wxGridStringTable()
1133 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1136 m_numCols
= numCols
;
1138 m_data
.Alloc( numRows
);
1141 sa
.Alloc( numCols
);
1142 sa
.Add( wxEmptyString
, numCols
);
1144 m_data
.Add( sa
, numRows
);
1147 wxString
wxGridStringTable::GetValue( int row
, int col
)
1149 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
1151 _T("invalid row or column index in wxGridStringTable") );
1153 return m_data
[row
][col
];
1156 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1158 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
1159 _T("invalid row or column index in wxGridStringTable") );
1161 m_data
[row
][col
] = value
;
1164 void wxGridStringTable::Clear()
1167 int numRows
, numCols
;
1169 numRows
= m_data
.GetCount();
1172 numCols
= m_data
[0].GetCount();
1174 for ( row
= 0; row
< numRows
; row
++ )
1176 for ( col
= 0; col
< numCols
; col
++ )
1178 m_data
[row
][col
] = wxEmptyString
;
1184 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1186 size_t curNumRows
= m_data
.GetCount();
1187 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1188 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1190 if ( pos
>= curNumRows
)
1192 return AppendRows( numRows
);
1196 sa
.Alloc( curNumCols
);
1197 sa
.Add( wxEmptyString
, curNumCols
);
1198 m_data
.Insert( sa
, pos
, numRows
);
1202 wxGridTableMessage
msg( this,
1203 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1207 GetView()->ProcessTableMessage( msg
);
1213 bool wxGridStringTable::AppendRows( size_t numRows
)
1215 size_t curNumRows
= m_data
.GetCount();
1216 size_t curNumCols
= ( curNumRows
> 0
1217 ? m_data
[0].GetCount()
1218 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1221 if ( curNumCols
> 0 )
1223 sa
.Alloc( curNumCols
);
1224 sa
.Add( wxEmptyString
, curNumCols
);
1227 m_data
.Add( sa
, numRows
);
1231 wxGridTableMessage
msg( this,
1232 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1235 GetView()->ProcessTableMessage( msg
);
1241 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1243 size_t curNumRows
= m_data
.GetCount();
1245 if ( pos
>= curNumRows
)
1247 wxFAIL_MSG( wxString::Format
1249 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1251 (unsigned long)numRows
,
1252 (unsigned long)curNumRows
1258 if ( numRows
> curNumRows
- pos
)
1260 numRows
= curNumRows
- pos
;
1263 if ( numRows
>= curNumRows
)
1269 m_data
.RemoveAt( pos
, numRows
);
1274 wxGridTableMessage
msg( this,
1275 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1279 GetView()->ProcessTableMessage( msg
);
1285 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1289 size_t curNumRows
= m_data
.GetCount();
1290 size_t curNumCols
= ( curNumRows
> 0
1291 ? m_data
[0].GetCount()
1292 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1294 if ( pos
>= curNumCols
)
1296 return AppendCols( numCols
);
1299 if ( !m_colLabels
.IsEmpty() )
1301 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1304 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1305 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1308 for ( row
= 0; row
< curNumRows
; row
++ )
1310 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1312 m_data
[row
].Insert( wxEmptyString
, col
);
1316 m_numCols
+= numCols
;
1320 wxGridTableMessage
msg( this,
1321 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1325 GetView()->ProcessTableMessage( msg
);
1331 bool wxGridStringTable::AppendCols( size_t numCols
)
1335 size_t curNumRows
= m_data
.GetCount();
1337 for ( row
= 0; row
< curNumRows
; row
++ )
1339 m_data
[row
].Add( wxEmptyString
, numCols
);
1342 m_numCols
+= numCols
;
1346 wxGridTableMessage
msg( this,
1347 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1350 GetView()->ProcessTableMessage( msg
);
1356 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1360 size_t curNumRows
= m_data
.GetCount();
1361 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1362 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1364 if ( pos
>= curNumCols
)
1366 wxFAIL_MSG( wxString::Format
1368 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1370 (unsigned long)numCols
,
1371 (unsigned long)curNumCols
1378 colID
= GetView()->GetColAt( pos
);
1382 if ( numCols
> curNumCols
- colID
)
1384 numCols
= curNumCols
- colID
;
1387 if ( !m_colLabels
.IsEmpty() )
1389 // m_colLabels stores just as many elements as it needs, e.g. if only
1390 // the label of the first column had been set it would have only one
1391 // element and not numCols, so account for it
1392 int nToRm
= m_colLabels
.size() - colID
;
1394 m_colLabels
.RemoveAt( colID
, nToRm
);
1397 if ( numCols
>= curNumCols
)
1399 for ( row
= 0; row
< curNumRows
; row
++ )
1401 m_data
[row
].Clear();
1406 else // something will be left
1408 for ( row
= 0; row
< curNumRows
; row
++ )
1410 m_data
[row
].RemoveAt( colID
, numCols
);
1413 m_numCols
-= numCols
;
1418 wxGridTableMessage
msg( this,
1419 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1423 GetView()->ProcessTableMessage( msg
);
1429 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1431 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1433 // using default label
1435 return wxGridTableBase::GetRowLabelValue( row
);
1439 return m_rowLabels
[row
];
1443 wxString
wxGridStringTable::GetColLabelValue( int col
)
1445 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1447 // using default label
1449 return wxGridTableBase::GetColLabelValue( col
);
1453 return m_colLabels
[col
];
1457 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1459 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1461 int n
= m_rowLabels
.GetCount();
1464 for ( i
= n
; i
<= row
; i
++ )
1466 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1470 m_rowLabels
[row
] = value
;
1473 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1475 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1477 int n
= m_colLabels
.GetCount();
1480 for ( i
= n
; i
<= col
; i
++ )
1482 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1486 m_colLabels
[col
] = value
;
1490 //////////////////////////////////////////////////////////////////////
1491 //////////////////////////////////////////////////////////////////////
1493 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1494 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1497 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1499 m_owner
->CancelMouseCapture();
1502 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1503 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1504 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1505 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1508 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1512 // NO - don't do this because it will set both the x and y origin
1513 // coords to match the parent scrolled window and we just want to
1514 // set the y coord - MB
1516 // m_owner->PrepareDC( dc );
1519 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1520 wxPoint pt
= dc
.GetDeviceOrigin();
1521 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1523 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1524 m_owner
->DrawRowLabels( dc
, rows
);
1527 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1529 m_owner
->ProcessRowLabelMouseEvent( event
);
1532 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1534 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1538 //////////////////////////////////////////////////////////////////////
1540 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1541 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1542 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1543 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1546 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1550 // NO - don't do this because it will set both the x and y origin
1551 // coords to match the parent scrolled window and we just want to
1552 // set the x coord - MB
1554 // m_owner->PrepareDC( dc );
1557 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1558 wxPoint pt
= dc
.GetDeviceOrigin();
1559 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1560 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1562 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1564 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1565 m_owner
->DrawColLabels( dc
, cols
);
1568 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1570 m_owner
->ProcessColLabelMouseEvent( event
);
1573 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1575 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1579 //////////////////////////////////////////////////////////////////////
1581 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1582 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1583 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1584 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1587 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1591 m_owner
->DrawCornerLabel(dc
);
1594 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1596 m_owner
->ProcessCornerLabelMouseEvent( event
);
1599 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1601 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1605 //////////////////////////////////////////////////////////////////////
1607 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1608 EVT_PAINT( wxGridWindow::OnPaint
)
1609 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1610 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1611 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1612 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1613 EVT_CHAR( wxGridWindow::OnChar
)
1614 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1615 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1616 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1619 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1621 wxPaintDC
dc( this );
1622 m_owner
->PrepareDC( dc
);
1623 wxRegion reg
= GetUpdateRegion();
1624 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1625 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1627 m_owner
->DrawGridSpace( dc
);
1629 m_owner
->DrawAllGridLines( dc
, reg
);
1631 m_owner
->DrawHighlight( dc
, dirtyCells
);
1634 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1636 wxWindow::ScrollWindow( dx
, dy
, rect
);
1637 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1638 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1641 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1643 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1646 m_owner
->ProcessGridCellMouseEvent( event
);
1649 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1651 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1655 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1656 // cursor must be in the cell edit control to get key events
1658 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1660 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1664 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1666 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1670 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1672 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1676 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1680 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1682 // and if we have any selection, it has to be repainted, because it
1683 // uses different colour when the grid is not focused:
1684 if ( m_owner
->IsSelection() )
1690 // NB: Note that this code is in "else" branch only because the other
1691 // branch refreshes everything and so there's no point in calling
1692 // Refresh() again, *not* because it should only be done if
1693 // !IsSelection(). If the above code is ever optimized to refresh
1694 // only selected area, this needs to be moved out of the "else"
1695 // branch so that it's always executed.
1697 // current cell cursor {dis,re}appears on focus change:
1698 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1699 m_owner
->GetGridCursorCol());
1700 const wxRect cursor
=
1701 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1702 Refresh(true, &cursor
);
1705 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1709 #define internalXToCol(x) XToCol(x, true)
1710 #define internalYToRow(y) YToRow(y, true)
1712 /////////////////////////////////////////////////////////////////////
1714 #if wxUSE_EXTENDED_RTTI
1715 WX_DEFINE_FLAGS( wxGridStyle
)
1717 wxBEGIN_FLAGS( wxGridStyle
)
1718 // new style border flags, we put them first to
1719 // use them for streaming out
1720 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
1721 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
1722 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
1723 wxFLAGS_MEMBER(wxBORDER_RAISED
)
1724 wxFLAGS_MEMBER(wxBORDER_STATIC
)
1725 wxFLAGS_MEMBER(wxBORDER_NONE
)
1727 // old style border flags
1728 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
1729 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
1730 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
1731 wxFLAGS_MEMBER(wxRAISED_BORDER
)
1732 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
1733 wxFLAGS_MEMBER(wxBORDER
)
1735 // standard window styles
1736 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
1737 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
1738 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
1739 wxFLAGS_MEMBER(wxWANTS_CHARS
)
1740 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
1741 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
1742 wxFLAGS_MEMBER(wxVSCROLL
)
1743 wxFLAGS_MEMBER(wxHSCROLL
)
1745 wxEND_FLAGS( wxGridStyle
)
1747 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
1749 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
1750 wxHIDE_PROPERTY( Children
)
1751 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1752 wxEND_PROPERTIES_TABLE()
1754 wxBEGIN_HANDLERS_TABLE(wxGrid
)
1755 wxEND_HANDLERS_TABLE()
1757 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1760 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1763 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1766 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1767 EVT_PAINT( wxGrid::OnPaint
)
1768 EVT_SIZE( wxGrid::OnSize
)
1769 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1770 EVT_KEY_UP( wxGrid::OnKeyUp
)
1771 EVT_CHAR ( wxGrid::OnChar
)
1772 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1775 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1776 const wxPoint
& pos
, const wxSize
& size
,
1777 long style
, const wxString
& name
)
1779 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1780 style
| wxWANTS_CHARS
, name
))
1783 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1784 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1787 SetInitialSize(size
);
1788 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
1797 m_winCapture
->ReleaseMouse();
1799 // Ensure that the editor control is destroyed before the grid is,
1800 // otherwise we crash later when the editor tries to do something with the
1801 // half destroyed grid
1802 HideCellEditControl();
1804 // Must do this or ~wxScrollHelper will pop the wrong event handler
1805 SetTargetWindow(this);
1807 wxSafeDecRef(m_defaultCellAttr
);
1809 #ifdef DEBUG_ATTR_CACHE
1810 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1811 wxPrintf(_T("wxGrid attribute cache statistics: "
1812 "total: %u, hits: %u (%u%%)\n"),
1813 total
, gs_nAttrCacheHits
,
1814 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1817 // if we own the table, just delete it, otherwise at least don't leave it
1818 // with dangling view pointer
1821 else if ( m_table
&& m_table
->GetView() == this )
1822 m_table
->SetView(NULL
);
1824 delete m_typeRegistry
;
1827 delete m_setFixedRows
;
1828 delete m_setFixedCols
;
1832 // ----- internal init and update functions
1835 // NOTE: If using the default visual attributes works everywhere then this can
1836 // be removed as well as the #else cases below.
1837 #define _USE_VISATTR 0
1839 void wxGrid::Create()
1841 // create the type registry
1842 m_typeRegistry
= new wxGridTypeRegistry
;
1844 m_cellEditCtrlEnabled
= false;
1846 m_defaultCellAttr
= new wxGridCellAttr();
1848 // Set default cell attributes
1849 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1850 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1851 m_defaultCellAttr
->SetFont(GetFont());
1852 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1853 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1854 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1857 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1858 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1860 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1861 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1864 m_defaultCellAttr
->SetTextColour(
1865 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1866 m_defaultCellAttr
->SetBackgroundColour(
1867 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1872 m_currentCellCoords
= wxGridNoCellCoords
;
1874 // subwindow components that make up the wxGrid
1875 m_rowLabelWin
= new wxGridRowLabelWindow(this);
1876 CreateColumnWindow();
1877 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
1878 m_gridWin
= new wxGridWindow( this );
1880 SetTargetWindow( m_gridWin
);
1883 wxColour gfg
= gva
.colFg
;
1884 wxColour gbg
= gva
.colBg
;
1885 wxColour lfg
= lva
.colFg
;
1886 wxColour lbg
= lva
.colBg
;
1888 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1889 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
1890 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1891 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1894 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
1895 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
1896 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
1897 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
1898 m_colWindow
->SetOwnForegroundColour(lfg
);
1899 m_colWindow
->SetOwnBackgroundColour(lbg
);
1901 m_gridWin
->SetOwnForegroundColour(gfg
);
1902 m_gridWin
->SetOwnBackgroundColour(gbg
);
1904 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1905 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
1907 // now that we have the grid window, use its font to compute the default
1909 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1910 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1911 m_defaultRowHeight
+= 8;
1913 m_defaultRowHeight
+= 4;
1918 void wxGrid::CreateColumnWindow()
1920 if ( m_useNativeHeader
)
1922 m_colWindow
= new wxGridHeaderCtrl(this);
1923 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
1925 else // draw labels ourselves
1927 m_colWindow
= new wxGridColLabelWindow(this);
1928 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1932 bool wxGrid::CreateGrid( int numRows
, int numCols
,
1933 wxGridSelectionModes selmode
)
1935 wxCHECK_MSG( !m_created
,
1937 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1939 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
1942 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
1944 wxCHECK_RET( m_created
,
1945 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
1947 m_selection
->SetSelectionMode( selmode
);
1950 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
1952 wxCHECK_MSG( m_created
, wxGridSelectCells
,
1953 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
1955 return m_selection
->GetSelectionMode();
1959 wxGrid::SetTable(wxGridTableBase
*table
,
1961 wxGrid::wxGridSelectionModes selmode
)
1963 bool checkSelection
= false;
1966 // stop all processing
1971 m_table
->SetView(0);
1983 checkSelection
= true;
1985 // kill row and column size arrays
1986 m_colWidths
.Empty();
1987 m_colRights
.Empty();
1988 m_rowHeights
.Empty();
1989 m_rowBottoms
.Empty();
1994 m_numRows
= table
->GetNumberRows();
1995 m_numCols
= table
->GetNumberCols();
1997 if ( m_useNativeHeader
)
1998 GetGridColHeader()->SetColumnCount(m_numCols
);
2001 m_table
->SetView( this );
2002 m_ownTable
= takeOwnership
;
2003 m_selection
= new wxGridSelection( this, selmode
);
2006 // If the newly set table is smaller than the
2007 // original one current cell and selection regions
2008 // might be invalid,
2009 m_selectedBlockCorner
= wxGridNoCellCoords
;
2010 m_currentCellCoords
=
2011 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2012 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2013 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2014 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2016 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2017 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2020 m_selectedBlockBottomRight
=
2021 wxGridCellCoords(wxMin(m_numRows
,
2022 m_selectedBlockBottomRight
.GetRow()),
2024 m_selectedBlockBottomRight
.GetCol()));
2038 m_cornerLabelWin
= NULL
;
2039 m_rowLabelWin
= NULL
;
2047 m_defaultCellAttr
= NULL
;
2048 m_typeRegistry
= NULL
;
2049 m_winCapture
= NULL
;
2051 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2052 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2055 m_setFixedCols
= NULL
;
2058 m_attrCache
.row
= -1;
2059 m_attrCache
.col
= -1;
2060 m_attrCache
.attr
= NULL
;
2062 m_labelFont
= GetFont();
2063 m_labelFont
.SetWeight( wxBOLD
);
2065 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2066 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2068 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2069 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2070 m_colLabelTextOrientation
= wxHORIZONTAL
;
2072 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2073 m_defaultRowHeight
= 0; // this will be initialized after creation
2075 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2076 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2078 m_gridLineColour
= wxColour( 192,192,192 );
2079 m_gridLinesEnabled
= true;
2080 m_gridLinesClipHorz
=
2081 m_gridLinesClipVert
= true;
2082 m_cellHighlightColour
= *wxBLACK
;
2083 m_cellHighlightPenWidth
= 2;
2084 m_cellHighlightROPenWidth
= 1;
2086 m_canDragColMove
= false;
2088 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2089 m_winCapture
= NULL
;
2090 m_canDragRowSize
= true;
2091 m_canDragColSize
= true;
2092 m_canDragGridSize
= true;
2093 m_canDragCell
= false;
2095 m_dragRowOrCol
= -1;
2096 m_isDragging
= false;
2097 m_startDragPos
= wxDefaultPosition
;
2099 m_sortCol
= wxNOT_FOUND
;
2100 m_sortIsAscending
= true;
2103 m_nativeColumnLabels
= false;
2105 m_waitForSlowClick
= false;
2107 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2108 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2110 m_currentCellCoords
= wxGridNoCellCoords
;
2112 m_selectedBlockTopLeft
=
2113 m_selectedBlockBottomRight
=
2114 m_selectedBlockCorner
= wxGridNoCellCoords
;
2116 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2117 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2119 m_editable
= true; // default for whole grid
2121 m_inOnKeyDown
= false;
2127 m_scrollLineX
= GRID_SCROLL_LINE_X
;
2128 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
2131 // ----------------------------------------------------------------------------
2132 // the idea is to call these functions only when necessary because they create
2133 // quite big arrays which eat memory mostly unnecessary - in particular, if
2134 // default widths/heights are used for all rows/columns, we may not use these
2137 // with some extra code, it should be possible to only store the widths/heights
2138 // different from default ones (resulting in space savings for huge grids) but
2139 // this is not done currently
2140 // ----------------------------------------------------------------------------
2142 void wxGrid::InitRowHeights()
2144 m_rowHeights
.Empty();
2145 m_rowBottoms
.Empty();
2147 m_rowHeights
.Alloc( m_numRows
);
2148 m_rowBottoms
.Alloc( m_numRows
);
2150 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2153 for ( int i
= 0; i
< m_numRows
; i
++ )
2155 rowBottom
+= m_defaultRowHeight
;
2156 m_rowBottoms
.Add( rowBottom
);
2160 void wxGrid::InitColWidths()
2162 m_colWidths
.Empty();
2163 m_colRights
.Empty();
2165 m_colWidths
.Alloc( m_numCols
);
2166 m_colRights
.Alloc( m_numCols
);
2168 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2170 for ( int i
= 0; i
< m_numCols
; i
++ )
2172 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2173 m_colRights
.Add( colRight
);
2177 int wxGrid::GetColWidth(int col
) const
2179 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2182 int wxGrid::GetColLeft(int col
) const
2184 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2185 : m_colRights
[col
] - m_colWidths
[col
];
2188 int wxGrid::GetColRight(int col
) const
2190 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2194 int wxGrid::GetRowHeight(int row
) const
2196 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2199 int wxGrid::GetRowTop(int row
) const
2201 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2202 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2205 int wxGrid::GetRowBottom(int row
) const
2207 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2208 : m_rowBottoms
[row
];
2211 void wxGrid::CalcDimensions()
2213 // compute the size of the scrollable area
2214 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2215 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2220 // take into account editor if shown
2221 if ( IsCellEditControlShown() )
2224 int r
= m_currentCellCoords
.GetRow();
2225 int c
= m_currentCellCoords
.GetCol();
2226 int x
= GetColLeft(c
);
2227 int y
= GetRowTop(r
);
2229 // how big is the editor
2230 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2231 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2232 editor
->GetControl()->GetSize(&w2
, &h2
);
2243 // preserve (more or less) the previous position
2245 GetViewStart( &x
, &y
);
2247 // ensure the position is valid for the new scroll ranges
2249 x
= wxMax( w
- 1, 0 );
2251 y
= wxMax( h
- 1, 0 );
2253 // update the virtual size and refresh the scrollbars to reflect it
2254 m_gridWin
->SetVirtualSize(w
, h
);
2258 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2259 // still must reposition the children
2263 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2265 wxSize
sizeGridWin(size
);
2266 sizeGridWin
.x
-= m_rowLabelWidth
;
2267 sizeGridWin
.y
-= m_colLabelHeight
;
2272 void wxGrid::CalcWindowSizes()
2274 // escape if the window is has not been fully created yet
2276 if ( m_cornerLabelWin
== NULL
)
2280 GetClientSize( &cw
, &ch
);
2282 // the grid may be too small to have enough space for the labels yet, don't
2283 // size the windows to negative sizes in this case
2284 int gw
= cw
- m_rowLabelWidth
;
2285 int gh
= ch
- m_colLabelHeight
;
2291 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2292 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2294 if ( m_colWindow
&& m_colWindow
->IsShown() )
2295 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2297 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2298 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2300 if ( m_gridWin
&& m_gridWin
->IsShown() )
2301 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2304 // this is called when the grid table sends a message
2305 // to indicate that it has been redimensioned
2307 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2310 bool result
= false;
2312 // Clear the attribute cache as the attribute might refer to a different
2313 // cell than stored in the cache after adding/removing rows/columns.
2316 // By the same reasoning, the editor should be dismissed if columns are
2317 // added or removed. And for consistency, it should IMHO always be
2318 // removed, not only if the cell "underneath" it actually changes.
2319 // For now, I intentionally do not save the editor's content as the
2320 // cell it might want to save that stuff to might no longer exist.
2321 HideCellEditControl();
2323 switch ( msg
.GetId() )
2325 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2327 size_t pos
= msg
.GetCommandInt();
2328 int numRows
= msg
.GetCommandInt2();
2330 m_numRows
+= numRows
;
2332 if ( !m_rowHeights
.IsEmpty() )
2334 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2335 m_rowBottoms
.Insert( 0, pos
, numRows
);
2339 bottom
= m_rowBottoms
[pos
- 1];
2341 for ( i
= pos
; i
< m_numRows
; i
++ )
2343 bottom
+= m_rowHeights
[i
];
2344 m_rowBottoms
[i
] = bottom
;
2348 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2350 // if we have just inserted cols into an empty grid the current
2351 // cell will be undefined...
2353 SetCurrentCell( 0, 0 );
2357 m_selection
->UpdateRows( pos
, numRows
);
2358 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2360 attrProvider
->UpdateAttrRows( pos
, numRows
);
2362 if ( !GetBatchCount() )
2365 m_rowLabelWin
->Refresh();
2371 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2373 int numRows
= msg
.GetCommandInt();
2374 int oldNumRows
= m_numRows
;
2375 m_numRows
+= numRows
;
2377 if ( !m_rowHeights
.IsEmpty() )
2379 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2380 m_rowBottoms
.Add( 0, numRows
);
2383 if ( oldNumRows
> 0 )
2384 bottom
= m_rowBottoms
[oldNumRows
- 1];
2386 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2388 bottom
+= m_rowHeights
[i
];
2389 m_rowBottoms
[i
] = bottom
;
2393 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2395 // if we have just inserted cols into an empty grid the current
2396 // cell will be undefined...
2398 SetCurrentCell( 0, 0 );
2401 if ( !GetBatchCount() )
2404 m_rowLabelWin
->Refresh();
2410 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2412 size_t pos
= msg
.GetCommandInt();
2413 int numRows
= msg
.GetCommandInt2();
2414 m_numRows
-= numRows
;
2416 if ( !m_rowHeights
.IsEmpty() )
2418 m_rowHeights
.RemoveAt( pos
, numRows
);
2419 m_rowBottoms
.RemoveAt( pos
, numRows
);
2422 for ( i
= 0; i
< m_numRows
; i
++ )
2424 h
+= m_rowHeights
[i
];
2425 m_rowBottoms
[i
] = h
;
2431 m_currentCellCoords
= wxGridNoCellCoords
;
2435 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2436 m_currentCellCoords
.Set( 0, 0 );
2440 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2441 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2444 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2446 // ifdef'd out following patch from Paul Gammans
2448 // No need to touch column attributes, unless we
2449 // removed _all_ rows, in this case, we remove
2450 // all column attributes.
2451 // I hate to do this here, but the
2452 // needed data is not available inside UpdateAttrRows.
2453 if ( !GetNumberRows() )
2454 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2458 if ( !GetBatchCount() )
2461 m_rowLabelWin
->Refresh();
2467 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2469 size_t pos
= msg
.GetCommandInt();
2470 int numCols
= msg
.GetCommandInt2();
2471 m_numCols
+= numCols
;
2473 if ( m_useNativeHeader
)
2474 GetGridColHeader()->SetColumnCount(m_numCols
);
2476 if ( !m_colAt
.IsEmpty() )
2478 //Shift the column IDs
2480 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2482 if ( m_colAt
[i
] >= (int)pos
)
2483 m_colAt
[i
] += numCols
;
2486 m_colAt
.Insert( pos
, pos
, numCols
);
2488 //Set the new columns' positions
2489 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2495 if ( !m_colWidths
.IsEmpty() )
2497 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2498 m_colRights
.Insert( 0, pos
, numCols
);
2502 right
= m_colRights
[GetColAt( pos
- 1 )];
2505 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2507 i
= GetColAt( colPos
);
2509 right
+= m_colWidths
[i
];
2510 m_colRights
[i
] = right
;
2514 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2516 // if we have just inserted cols into an empty grid the current
2517 // cell will be undefined...
2519 SetCurrentCell( 0, 0 );
2523 m_selection
->UpdateCols( pos
, numCols
);
2524 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2526 attrProvider
->UpdateAttrCols( pos
, numCols
);
2527 if ( !GetBatchCount() )
2530 m_colWindow
->Refresh();
2536 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2538 int numCols
= msg
.GetCommandInt();
2539 int oldNumCols
= m_numCols
;
2540 m_numCols
+= numCols
;
2541 if ( m_useNativeHeader
)
2542 GetGridColHeader()->SetColumnCount(m_numCols
);
2544 if ( !m_colAt
.IsEmpty() )
2546 m_colAt
.Add( 0, numCols
);
2548 //Set the new columns' positions
2550 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2556 if ( !m_colWidths
.IsEmpty() )
2558 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2559 m_colRights
.Add( 0, numCols
);
2562 if ( oldNumCols
> 0 )
2563 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2566 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2568 i
= GetColAt( colPos
);
2570 right
+= m_colWidths
[i
];
2571 m_colRights
[i
] = right
;
2575 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2577 // if we have just inserted cols into an empty grid the current
2578 // cell will be undefined...
2580 SetCurrentCell( 0, 0 );
2582 if ( !GetBatchCount() )
2585 m_colWindow
->Refresh();
2591 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2593 size_t pos
= msg
.GetCommandInt();
2594 int numCols
= msg
.GetCommandInt2();
2595 m_numCols
-= numCols
;
2596 if ( m_useNativeHeader
)
2597 GetGridColHeader()->SetColumnCount(m_numCols
);
2599 if ( !m_colAt
.IsEmpty() )
2601 int colID
= GetColAt( pos
);
2603 m_colAt
.RemoveAt( pos
, numCols
);
2605 //Shift the column IDs
2607 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2609 if ( m_colAt
[colPos
] > colID
)
2610 m_colAt
[colPos
] -= numCols
;
2614 if ( !m_colWidths
.IsEmpty() )
2616 m_colWidths
.RemoveAt( pos
, numCols
);
2617 m_colRights
.RemoveAt( pos
, numCols
);
2621 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2623 i
= GetColAt( colPos
);
2625 w
+= m_colWidths
[i
];
2632 m_currentCellCoords
= wxGridNoCellCoords
;
2636 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2637 m_currentCellCoords
.Set( 0, 0 );
2641 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2642 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2645 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2647 // ifdef'd out following patch from Paul Gammans
2649 // No need to touch row attributes, unless we
2650 // removed _all_ columns, in this case, we remove
2651 // all row attributes.
2652 // I hate to do this here, but the
2653 // needed data is not available inside UpdateAttrCols.
2654 if ( !GetNumberCols() )
2655 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2659 if ( !GetBatchCount() )
2662 m_colWindow
->Refresh();
2669 if (result
&& !GetBatchCount() )
2670 m_gridWin
->Refresh();
2675 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2677 wxRegionIterator
iter( reg
);
2680 wxArrayInt rowlabels
;
2687 // TODO: remove this when we can...
2688 // There is a bug in wxMotif that gives garbage update
2689 // rectangles if you jump-scroll a long way by clicking the
2690 // scrollbar with middle button. This is a work-around
2692 #if defined(__WXMOTIF__)
2694 m_gridWin
->GetClientSize( &cw
, &ch
);
2695 if ( r
.GetTop() > ch
)
2697 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2700 // logical bounds of update region
2703 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2704 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2706 // find the row labels within these bounds
2709 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2711 if ( GetRowBottom(row
) < top
)
2714 if ( GetRowTop(row
) > bottom
)
2717 rowlabels
.Add( row
);
2726 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2728 wxRegionIterator
iter( reg
);
2731 wxArrayInt colLabels
;
2738 // TODO: remove this when we can...
2739 // There is a bug in wxMotif that gives garbage update
2740 // rectangles if you jump-scroll a long way by clicking the
2741 // scrollbar with middle button. This is a work-around
2743 #if defined(__WXMOTIF__)
2745 m_gridWin
->GetClientSize( &cw
, &ch
);
2746 if ( r
.GetLeft() > cw
)
2748 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2751 // logical bounds of update region
2754 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2755 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2757 // find the cells within these bounds
2761 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2763 col
= GetColAt( colPos
);
2765 if ( GetColRight(col
) < left
)
2768 if ( GetColLeft(col
) > right
)
2771 colLabels
.Add( col
);
2780 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2782 wxRegionIterator
iter( reg
);
2785 wxGridCellCoordsArray cellsExposed
;
2787 int left
, top
, right
, bottom
;
2792 // TODO: remove this when we can...
2793 // There is a bug in wxMotif that gives garbage update
2794 // rectangles if you jump-scroll a long way by clicking the
2795 // scrollbar with middle button. This is a work-around
2797 #if defined(__WXMOTIF__)
2799 m_gridWin
->GetClientSize( &cw
, &ch
);
2800 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2801 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2802 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2803 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2806 // logical bounds of update region
2808 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2809 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2811 // find the cells within these bounds
2813 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2815 if ( GetRowBottom(row
) <= top
)
2818 if ( GetRowTop(row
) > bottom
)
2821 // add all dirty cells in this row: notice that the columns which
2822 // are dirty don't depend on the row so we compute them only once
2823 // for the first dirty row and then reuse for all the next ones
2826 // do determine the dirty columns
2827 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2828 cols
.push_back(GetColAt(pos
));
2830 // if there are no dirty columns at all, nothing to do
2835 const size_t count
= cols
.size();
2836 for ( size_t n
= 0; n
< count
; n
++ )
2837 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2843 return cellsExposed
;
2847 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2850 wxPoint
pos( event
.GetPosition() );
2851 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2853 if ( event
.Dragging() )
2857 m_isDragging
= true;
2858 m_rowLabelWin
->CaptureMouse();
2861 if ( event
.LeftIsDown() )
2863 switch ( m_cursorMode
)
2865 case WXGRID_CURSOR_RESIZE_ROW
:
2867 int cw
, ch
, left
, dummy
;
2868 m_gridWin
->GetClientSize( &cw
, &ch
);
2869 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2871 wxClientDC
dc( m_gridWin
);
2874 GetRowTop(m_dragRowOrCol
) +
2875 GetRowMinimalHeight(m_dragRowOrCol
) );
2876 dc
.SetLogicalFunction(wxINVERT
);
2877 if ( m_dragLastPos
>= 0 )
2879 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2881 dc
.DrawLine( left
, y
, left
+cw
, y
);
2886 case WXGRID_CURSOR_SELECT_ROW
:
2888 if ( (row
= YToRow( y
)) >= 0 )
2891 m_selection
->SelectRow(row
, event
);
2896 // default label to suppress warnings about "enumeration value
2897 // 'xxx' not handled in switch
2905 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
2910 if (m_rowLabelWin
->HasCapture())
2911 m_rowLabelWin
->ReleaseMouse();
2912 m_isDragging
= false;
2915 // ------------ Entering or leaving the window
2917 if ( event
.Entering() || event
.Leaving() )
2919 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2922 // ------------ Left button pressed
2924 else if ( event
.LeftDown() )
2926 row
= YToEdgeOfRow(y
);
2927 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
2929 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2931 else // not a request to start resizing
2935 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2937 if ( !event
.ShiftDown() && !event
.CmdDown() )
2941 if ( event
.ShiftDown() )
2943 m_selection
->SelectBlock
2945 m_currentCellCoords
.GetRow(), 0,
2946 row
, GetNumberCols() - 1,
2952 m_selection
->SelectRow(row
, event
);
2956 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2961 // ------------ Left double click
2963 else if (event
.LeftDClick() )
2965 row
= YToEdgeOfRow(y
);
2966 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
2968 // adjust row height depending on label text
2970 // TODO: generate RESIZING event, see #10754
2971 AutoSizeRowLabelSize( row
);
2973 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
2975 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
2978 else // not on row separator or it's not resizeable
2982 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
2984 // no default action at the moment
2989 // ------------ Left button released
2991 else if ( event
.LeftUp() )
2993 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2994 DoEndDragResizeRow(event
);
2996 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3000 // ------------ Right button down
3002 else if ( event
.RightDown() )
3006 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3008 // no default action at the moment
3012 // ------------ Right double click
3014 else if ( event
.RightDClick() )
3018 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3020 // no default action at the moment
3024 // ------------ No buttons down and mouse moving
3026 else if ( event
.Moving() )
3028 m_dragRowOrCol
= YToEdgeOfRow( y
);
3029 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3031 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3033 if ( CanDragRowSize(m_dragRowOrCol
) )
3034 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3037 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3039 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3044 void wxGrid::UpdateColumnSortingIndicator(int col
)
3046 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3048 if ( m_useNativeHeader
)
3049 GetGridColHeader()->UpdateColumn(col
);
3050 else if ( m_nativeColumnLabels
)
3051 m_colWindow
->Refresh();
3052 //else: sorting indicator display not yet implemented in grid version
3055 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3057 if ( col
== m_sortCol
)
3059 // we are already using this column for sorting (or not sorting at all)
3060 // but we might still change the sorting order, check for it
3061 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3063 m_sortIsAscending
= ascending
;
3065 UpdateColumnSortingIndicator(m_sortCol
);
3068 else // we're changing the column used for sorting
3070 const int sortColOld
= m_sortCol
;
3072 // change it before updating the column as we want GetSortingColumn()
3073 // to return the correct new value
3076 if ( sortColOld
!= wxNOT_FOUND
)
3077 UpdateColumnSortingIndicator(sortColOld
);
3079 if ( m_sortCol
!= wxNOT_FOUND
)
3081 m_sortIsAscending
= ascending
;
3082 UpdateColumnSortingIndicator(m_sortCol
);
3087 void wxGrid::DoColHeaderClick(int col
)
3089 // we consider that the grid was resorted if this event is processed and
3091 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3093 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3098 void wxGrid::DoStartResizeCol(int col
)
3100 m_dragRowOrCol
= col
;
3102 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3105 void wxGrid::DoUpdateResizeCol(int x
)
3107 int cw
, ch
, dummy
, top
;
3108 m_gridWin
->GetClientSize( &cw
, &ch
);
3109 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3111 wxClientDC
dc( m_gridWin
);
3114 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3115 dc
.SetLogicalFunction(wxINVERT
);
3116 if ( m_dragLastPos
>= 0 )
3118 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3120 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3124 void wxGrid::DoUpdateResizeColWidth(int w
)
3126 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3129 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3132 wxPoint
pos( event
.GetPosition() );
3133 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3135 int col
= XToCol(x
);
3136 if ( event
.Dragging() )
3140 m_isDragging
= true;
3141 GetColLabelWindow()->CaptureMouse();
3143 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3144 DoStartMoveCol(col
);
3147 if ( event
.LeftIsDown() )
3149 switch ( m_cursorMode
)
3151 case WXGRID_CURSOR_RESIZE_COL
:
3152 DoUpdateResizeCol(x
);
3155 case WXGRID_CURSOR_SELECT_COL
:
3160 m_selection
->SelectCol(col
, event
);
3165 case WXGRID_CURSOR_MOVE_COL
:
3167 int posNew
= XToPos(x
);
3168 int colNew
= GetColAt(posNew
);
3170 // determine the position of the drop marker
3172 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3173 markerX
= GetColRight(colNew
);
3175 markerX
= GetColLeft(colNew
);
3177 if ( markerX
!= m_dragLastPos
)
3179 wxClientDC
dc( GetColLabelWindow() );
3183 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3187 //Clean up the last indicator
3188 if ( m_dragLastPos
>= 0 )
3190 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3192 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3193 dc
.SetPen(wxNullPen
);
3195 if ( XToCol( m_dragLastPos
) != -1 )
3196 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3199 const wxColour
*color
;
3200 //Moving to the same place? Don't draw a marker
3201 if ( colNew
== m_dragRowOrCol
)
3202 color
= wxLIGHT_GREY
;
3207 wxPen
pen( *color
, 2 );
3210 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3212 dc
.SetPen(wxNullPen
);
3214 m_dragLastPos
= markerX
- 1;
3219 // default label to suppress warnings about "enumeration value
3220 // 'xxx' not handled in switch
3228 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3233 if (GetColLabelWindow()->HasCapture())
3234 GetColLabelWindow()->ReleaseMouse();
3235 m_isDragging
= false;
3238 // ------------ Entering or leaving the window
3240 if ( event
.Entering() || event
.Leaving() )
3242 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3245 // ------------ Left button pressed
3247 else if ( event
.LeftDown() )
3249 int col
= XToEdgeOfCol(x
);
3250 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3252 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3254 else // not a request to start resizing
3258 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3260 if ( m_canDragColMove
)
3262 //Show button as pressed
3263 wxClientDC
dc( GetColLabelWindow() );
3264 int colLeft
= GetColLeft( col
);
3265 int colRight
= GetColRight( col
) - 1;
3266 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3267 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3268 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3270 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3274 if ( !event
.ShiftDown() && !event
.CmdDown() )
3278 if ( event
.ShiftDown() )
3280 m_selection
->SelectBlock
3282 0, m_currentCellCoords
.GetCol(),
3283 GetNumberRows() - 1, col
,
3289 m_selection
->SelectCol(col
, event
);
3293 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3299 // ------------ Left double click
3301 if ( event
.LeftDClick() )
3303 const int colEdge
= XToEdgeOfCol(x
);
3304 if ( colEdge
== -1 )
3307 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3309 // no default action at the moment
3314 // adjust column width depending on label text
3316 // TODO: generate RESIZING event, see #10754
3317 AutoSizeColLabelSize( colEdge
);
3319 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3321 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3326 // ------------ Left button released
3328 else if ( event
.LeftUp() )
3330 switch ( m_cursorMode
)
3332 case WXGRID_CURSOR_RESIZE_COL
:
3333 DoEndDragResizeCol(event
);
3336 case WXGRID_CURSOR_MOVE_COL
:
3337 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3339 // the column didn't actually move anywhere
3341 DoColHeaderClick(col
);
3342 m_colWindow
->Refresh(); // "unpress" the column
3346 DoEndMoveCol(XToPos(x
));
3350 case WXGRID_CURSOR_SELECT_COL
:
3351 case WXGRID_CURSOR_SELECT_CELL
:
3352 case WXGRID_CURSOR_RESIZE_ROW
:
3353 case WXGRID_CURSOR_SELECT_ROW
:
3355 DoColHeaderClick(col
);
3359 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3363 // ------------ Right button down
3365 else if ( event
.RightDown() )
3368 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3370 // no default action at the moment
3374 // ------------ Right double click
3376 else if ( event
.RightDClick() )
3379 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3381 // no default action at the moment
3385 // ------------ No buttons down and mouse moving
3387 else if ( event
.Moving() )
3389 m_dragRowOrCol
= XToEdgeOfCol( x
);
3390 if ( m_dragRowOrCol
>= 0 )
3392 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3394 if ( CanDragColSize(m_dragRowOrCol
) )
3395 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3398 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3400 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3405 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3407 if ( event
.LeftDown() )
3409 // indicate corner label by having both row and
3412 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3417 else if ( event
.LeftDClick() )
3419 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3421 else if ( event
.RightDown() )
3423 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3425 // no default action at the moment
3428 else if ( event
.RightDClick() )
3430 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3432 // no default action at the moment
3437 void wxGrid::CancelMouseCapture()
3439 // cancel operation currently in progress, whatever it is
3442 m_isDragging
= false;
3443 m_startDragPos
= wxDefaultPosition
;
3445 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3446 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3447 m_winCapture
= NULL
;
3449 // remove traces of whatever we drew on screen
3454 void wxGrid::ChangeCursorMode(CursorMode mode
,
3459 static const wxChar
*cursorModes
[] =
3469 wxLogTrace(_T("grid"),
3470 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3471 win
== m_colWindow
? _T("colLabelWin")
3472 : win
? _T("rowLabelWin")
3474 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3475 #endif // wxUSE_LOG_TRACE
3477 if ( mode
== m_cursorMode
&&
3478 win
== m_winCapture
&&
3479 captureMouse
== (m_winCapture
!= NULL
))
3484 // by default use the grid itself
3490 m_winCapture
->ReleaseMouse();
3491 m_winCapture
= NULL
;
3494 m_cursorMode
= mode
;
3496 switch ( m_cursorMode
)
3498 case WXGRID_CURSOR_RESIZE_ROW
:
3499 win
->SetCursor( m_rowResizeCursor
);
3502 case WXGRID_CURSOR_RESIZE_COL
:
3503 win
->SetCursor( m_colResizeCursor
);
3506 case WXGRID_CURSOR_MOVE_COL
:
3507 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3511 win
->SetCursor( *wxSTANDARD_CURSOR
);
3515 // we need to capture mouse when resizing
3516 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3517 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3519 if ( captureMouse
&& resize
)
3521 win
->CaptureMouse();
3526 // ----------------------------------------------------------------------------
3527 // grid mouse event processing
3528 // ----------------------------------------------------------------------------
3531 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3532 const wxGridCellCoords
& coords
,
3535 if ( coords
== wxGridNoCellCoords
)
3536 return; // we're outside any valid cell
3538 // Hide the edit control, so it won't interfere with drag-shrinking.
3539 if ( IsCellEditControlShown() )
3541 HideCellEditControl();
3542 SaveEditControlValue();
3545 switch ( event
.GetModifiers() )
3548 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3549 m_selectedBlockCorner
= coords
;
3550 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3554 if ( CanDragCell() )
3558 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3559 m_selectedBlockCorner
= coords
;
3561 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3566 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3570 // we don't handle the other key modifiers
3575 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3577 wxClientDC
dc(m_gridWin
);
3579 dc
.SetLogicalFunction(wxINVERT
);
3581 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3582 m_gridWin
->GetClientSize());
3584 // erase the previously drawn line, if any
3585 if ( m_dragLastPos
>= 0 )
3586 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3588 // we need the vertical position for rows and horizontal for columns here
3589 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3591 // don't allow resizing beneath the minimal size
3592 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3593 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3594 if ( m_dragLastPos
< posMin
)
3595 m_dragLastPos
= posMin
;
3597 // and draw it at the new position
3598 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3601 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3603 if ( !m_isDragging
)
3605 // Don't start doing anything until the mouse has been dragged far
3607 const wxPoint
& pt
= event
.GetPosition();
3608 if ( m_startDragPos
== wxDefaultPosition
)
3610 m_startDragPos
= pt
;
3614 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3615 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3619 const bool isFirstDrag
= !m_isDragging
;
3620 m_isDragging
= true;
3622 switch ( m_cursorMode
)
3624 case WXGRID_CURSOR_SELECT_CELL
:
3625 DoGridCellDrag(event
, coords
, isFirstDrag
);
3628 case WXGRID_CURSOR_RESIZE_ROW
:
3629 DoGridLineDrag(event
, wxGridRowOperations());
3632 case WXGRID_CURSOR_RESIZE_COL
:
3633 DoGridLineDrag(event
, wxGridColumnOperations());
3642 m_winCapture
= m_gridWin
;
3643 m_winCapture
->CaptureMouse();
3648 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3649 const wxGridCellCoords
& coords
,
3652 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3654 // event handled by user code, no need to do anything here
3658 if ( !event
.CmdDown() )
3661 if ( event
.ShiftDown() )
3665 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3666 m_selectedBlockCorner
= coords
;
3669 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3671 DisableCellEditControl();
3672 MakeCellVisible( coords
);
3674 if ( event
.CmdDown() )
3678 m_selection
->ToggleCellSelection(coords
, event
);
3681 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3682 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3683 m_selectedBlockCorner
= coords
;
3687 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3688 coords
!= wxGridNoCellCoords
;
3689 SetCurrentCell( coords
);
3695 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3696 const wxGridCellCoords
& coords
,
3699 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3701 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3703 // we want double click to select a cell and start editing
3704 // (i.e. to behave in same way as sequence of two slow clicks):
3705 m_waitForSlowClick
= true;
3711 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3713 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3717 m_winCapture
->ReleaseMouse();
3718 m_winCapture
= NULL
;
3721 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3724 EnableCellEditControl();
3726 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3727 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3728 editor
->StartingClick();
3732 m_waitForSlowClick
= false;
3734 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3735 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3739 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3740 m_selectedBlockBottomRight
,
3744 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3745 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3747 // Show the edit control, if it has been hidden for
3749 ShowCellEditControl();
3752 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3754 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3755 DoEndDragResizeRow(event
);
3757 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3759 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3760 DoEndDragResizeCol(event
);
3767 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3768 const wxGridCellCoords
& coords
,
3771 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3773 // out of grid cell area
3774 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3778 int dragRow
= YToEdgeOfRow( pos
.y
);
3779 int dragCol
= XToEdgeOfCol( pos
.x
);
3781 // Dragging on the corner of a cell to resize in both
3782 // directions is not implemented yet...
3784 if ( dragRow
>= 0 && dragCol
>= 0 )
3786 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3790 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3792 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3794 m_dragRowOrCol
= dragRow
;
3795 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3798 // When using the native header window we can only resize the columns by
3799 // dragging the dividers in it because we can't make it enter into the
3800 // column resizing mode programmatically
3801 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3802 CanDragGridSize() && CanDragColSize(dragCol
) )
3804 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3806 m_dragRowOrCol
= dragCol
;
3807 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3810 else // Neither on a row or col edge
3812 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3814 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3819 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
3821 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
3823 // coordinates of the cell under mouse
3824 wxGridCellCoords coords
= XYToCell(pos
);
3826 int cell_rows
, cell_cols
;
3827 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
3828 if ( (cell_rows
< 0) || (cell_cols
< 0) )
3830 coords
.SetRow(coords
.GetRow() + cell_rows
);
3831 coords
.SetCol(coords
.GetCol() + cell_cols
);
3834 if ( event
.Dragging() )
3836 if ( event
.LeftIsDown() )
3837 DoGridDragEvent(event
, coords
);
3843 m_isDragging
= false;
3844 m_startDragPos
= wxDefaultPosition
;
3846 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3847 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3850 if ( event
.Entering() || event
.Leaving() )
3852 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3853 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3857 // deal with various button presses
3858 if ( event
.IsButton() )
3860 if ( coords
!= wxGridNoCellCoords
)
3862 DisableCellEditControl();
3864 if ( event
.LeftDown() )
3865 DoGridCellLeftDown(event
, coords
, pos
);
3866 else if ( event
.LeftDClick() )
3867 DoGridCellLeftDClick(event
, coords
, pos
);
3868 else if ( event
.RightDown() )
3869 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
3870 else if ( event
.RightDClick() )
3871 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
3874 // this one should be called even if we're not over any cell
3875 if ( event
.LeftUp() )
3877 DoGridCellLeftUp(event
, coords
);
3880 else if ( event
.Moving() )
3882 DoGridMouseMoveEvent(event
, coords
, pos
);
3884 else // unknown mouse event?
3890 // this function returns true only if the size really changed
3891 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
3893 if ( m_dragLastPos
== -1 )
3896 const wxGridOperations
& doper
= oper
.Dual();
3898 const wxSize size
= m_gridWin
->GetClientSize();
3900 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
3902 // erase the last line we drew
3903 wxClientDC
dc(m_gridWin
);
3905 dc
.SetLogicalFunction(wxINVERT
);
3907 const int posLineStart
= oper
.Select(ptOrigin
);
3908 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
3910 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
3912 // temporarily hide the edit control before resizing
3913 HideCellEditControl();
3914 SaveEditControlValue();
3916 // do resize the line
3917 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
3918 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
3919 oper
.SetLineSize(this, m_dragRowOrCol
,
3920 wxMax(m_dragLastPos
- lineStart
,
3921 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
3923 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
3927 // refresh now if we're not frozen
3928 if ( !GetBatchCount() )
3930 // we need to refresh everything beyond the resized line in the header
3933 // get the position from which to refresh in the other direction
3934 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
3935 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
3937 // we only need the ordinate (for rows) or abscissa (for columns) here,
3938 // and need to cover the entire window in the other direction
3939 oper
.Select(rect
) = 0;
3941 wxRect
rectHeader(rect
.GetPosition(),
3944 oper
.GetHeaderWindowSize(this),
3945 doper
.Select(size
) - doper
.Select(rect
)
3948 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
3951 // also refresh the grid window: extend the rectangle
3954 oper
.SelectSize(rect
) = oper
.Select(size
);
3956 int subtractLines
= 0;
3957 const int lineStart
= oper
.PosToLine(this, posLineStart
);
3958 if ( lineStart
>= 0 )
3960 // ensure that if we have a multi-cell block we redraw all of
3961 // it by increasing the refresh area to cover it entirely if a
3962 // part of it is affected
3963 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
3964 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
3966 int cellLines
= oper
.Select(
3967 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
3968 if ( cellLines
< subtractLines
)
3969 subtractLines
= cellLines
;
3974 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
3975 startPos
= doper
.CalcScrolledPosition(this, startPos
);
3977 doper
.Select(rect
) = startPos
;
3978 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
3980 m_gridWin
->Refresh(false, &rect
);
3984 // show the edit control back again
3985 ShowCellEditControl();
3990 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
3992 // TODO: generate RESIZING event, see #10754
3994 if ( DoEndDragResizeLine(wxGridRowOperations()) )
3995 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3998 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4000 // TODO: generate RESIZING event, see #10754
4002 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4003 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4006 void wxGrid::DoStartMoveCol(int col
)
4008 m_dragRowOrCol
= col
;
4011 void wxGrid::DoEndMoveCol(int pos
)
4013 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4015 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4016 SetColPos(m_dragRowOrCol
, pos
);
4017 //else: vetoed by user
4019 m_dragRowOrCol
= -1;
4022 void wxGrid::RefreshAfterColPosChange()
4024 // recalculate the column rights as the column positions have changed,
4025 // unless we calculate them dynamically because all columns widths are the
4026 // same and it's easy to do
4027 if ( !m_colWidths
.empty() )
4030 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4032 int colID
= GetColAt( colPos
);
4034 colRight
+= m_colWidths
[colID
];
4035 m_colRights
[colID
] = colRight
;
4039 // and make the changes visible
4040 if ( m_useNativeHeader
)
4042 if ( m_colAt
.empty() )
4043 GetGridColHeader()->ResetColumnsOrder();
4045 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4049 m_colWindow
->Refresh();
4051 m_gridWin
->Refresh();
4054 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4058 RefreshAfterColPosChange();
4061 void wxGrid::SetColPos(int idx
, int pos
)
4063 // we're going to need m_colAt now, initialize it if needed
4064 if ( m_colAt
.empty() )
4066 m_colAt
.reserve(m_numCols
);
4067 for ( int i
= 0; i
< m_numCols
; i
++ )
4068 m_colAt
.push_back(i
);
4071 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4073 RefreshAfterColPosChange();
4076 void wxGrid::ResetColPos()
4080 RefreshAfterColPosChange();
4083 void wxGrid::EnableDragColMove( bool enable
)
4085 if ( m_canDragColMove
== enable
)
4088 if ( m_useNativeHeader
)
4090 // update all columns to make them [not] reorderable
4091 GetGridColHeader()->SetColumnCount(m_numCols
);
4094 m_canDragColMove
= enable
;
4096 // we use to call ResetColPos() from here if !enable but this doesn't seem
4097 // right as it would mean there would be no way to "freeze" the current
4098 // columns order by disabling moving them after putting them in the desired
4099 // order, whereas now you can always call ResetColPos() manually if needed
4104 // ------ interaction with data model
4106 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4108 switch ( msg
.GetId() )
4110 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4111 return GetModelValues();
4113 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4114 return SetModelValues();
4116 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4117 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4118 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4119 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4120 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4121 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4122 return Redimension( msg
);
4129 // The behaviour of this function depends on the grid table class
4130 // Clear() function. For the default wxGridStringTable class the
4131 // behaviour is to replace all cell contents with wxEmptyString but
4132 // not to change the number of rows or cols.
4134 void wxGrid::ClearGrid()
4138 if (IsCellEditControlEnabled())
4139 DisableCellEditControl();
4142 if (!GetBatchCount())
4143 m_gridWin
->Refresh();
4148 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4149 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4151 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4156 if ( IsCellEditControlEnabled() )
4157 DisableCellEditControl();
4159 return (m_table
->*funcModify
)(pos
, num
);
4161 // the table will have sent the results of the insert row
4162 // operation to this view object as a grid table message
4166 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4167 int num
, bool WXUNUSED(updateLabels
))
4169 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4174 return (m_table
->*funcAppend
)(num
);
4177 // ----------------------------------------------------------------------------
4178 // event generation helpers
4179 // ----------------------------------------------------------------------------
4182 wxGrid::SendGridSizeEvent(wxEventType type
,
4184 const wxMouseEvent
& mouseEv
)
4186 int rowOrCol
= row
== -1 ? col
: row
;
4188 wxGridSizeEvent
gridEvt( GetId(),
4192 mouseEv
.GetX() + GetRowLabelSize(),
4193 mouseEv
.GetY() + GetColLabelSize(),
4196 GetEventHandler()->ProcessEvent(gridEvt
);
4199 // Generate a grid event based on a mouse event and return:
4200 // -1 if the event was vetoed
4201 // +1 if the event was processed (but not vetoed)
4202 // 0 if the event wasn't handled
4204 wxGrid::SendEvent(const wxEventType type
,
4206 const wxMouseEvent
& mouseEv
)
4208 bool claimed
, vetoed
;
4210 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4212 // Right now, it should _never_ end up here!
4213 wxGridRangeSelectEvent
gridEvt( GetId(),
4216 m_selectedBlockTopLeft
,
4217 m_selectedBlockBottomRight
,
4221 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4222 vetoed
= !gridEvt
.IsAllowed();
4224 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4225 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4226 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4227 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4229 wxPoint pos
= mouseEv
.GetPosition();
4231 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4232 pos
.y
+= GetColLabelSize();
4233 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4234 pos
.x
+= GetRowLabelSize();
4236 wxGridEvent
gridEvt( GetId(),
4244 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4245 vetoed
= !gridEvt
.IsAllowed();
4249 wxGridEvent
gridEvt( GetId(),
4253 mouseEv
.GetX() + GetRowLabelSize(),
4254 mouseEv
.GetY() + GetColLabelSize(),
4257 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4258 vetoed
= !gridEvt
.IsAllowed();
4261 // A Veto'd event may not be `claimed' so test this first
4265 return claimed
? 1 : 0;
4268 // Generate a grid event of specified type, return value same as above
4271 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4273 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4274 gridEvt
.SetString(s
);
4276 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4278 // A Veto'd event may not be `claimed' so test this first
4279 if ( !gridEvt
.IsAllowed() )
4282 return claimed
? 1 : 0;
4285 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4287 // needed to prevent zillions of paint events on MSW
4291 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4293 // Don't do anything if between Begin/EndBatch...
4294 // EndBatch() will do all this on the last nested one anyway.
4295 if ( m_created
&& !GetBatchCount() )
4297 // Refresh to get correct scrolled position:
4298 wxScrolledWindow::Refresh(eraseb
, rect
);
4302 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4303 int width_label
, width_cell
, height_label
, height_cell
;
4306 // Copy rectangle can get scroll offsets..
4307 rect_x
= rect
->GetX();
4308 rect_y
= rect
->GetY();
4309 rectWidth
= rect
->GetWidth();
4310 rectHeight
= rect
->GetHeight();
4312 width_label
= m_rowLabelWidth
- rect_x
;
4313 if (width_label
> rectWidth
)
4314 width_label
= rectWidth
;
4316 height_label
= m_colLabelHeight
- rect_y
;
4317 if (height_label
> rectHeight
)
4318 height_label
= rectHeight
;
4320 if (rect_x
> m_rowLabelWidth
)
4322 x
= rect_x
- m_rowLabelWidth
;
4323 width_cell
= rectWidth
;
4328 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4331 if (rect_y
> m_colLabelHeight
)
4333 y
= rect_y
- m_colLabelHeight
;
4334 height_cell
= rectHeight
;
4339 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4342 // Paint corner label part intersecting rect.
4343 if ( width_label
> 0 && height_label
> 0 )
4345 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4346 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4349 // Paint col labels part intersecting rect.
4350 if ( width_cell
> 0 && height_label
> 0 )
4352 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4353 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4356 // Paint row labels part intersecting rect.
4357 if ( width_label
> 0 && height_cell
> 0 )
4359 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4360 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4363 // Paint cell area part intersecting rect.
4364 if ( width_cell
> 0 && height_cell
> 0 )
4366 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4367 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4372 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4373 m_colWindow
->Refresh(eraseb
, NULL
);
4374 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4375 m_gridWin
->Refresh(eraseb
, NULL
);
4380 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4382 if (m_targetWindow
!= this) // check whether initialisation has been done
4384 // reposition our children windows
4389 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4391 if ( m_inOnKeyDown
)
4393 // shouldn't be here - we are going round in circles...
4395 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4398 m_inOnKeyDown
= true;
4400 // propagate the event up and see if it gets processed
4401 wxWindow
*parent
= GetParent();
4402 wxKeyEvent
keyEvt( event
);
4403 keyEvt
.SetEventObject( parent
);
4405 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4407 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4409 if (event
.GetKeyCode() == WXK_RIGHT
)
4410 event
.m_keyCode
= WXK_LEFT
;
4411 else if (event
.GetKeyCode() == WXK_LEFT
)
4412 event
.m_keyCode
= WXK_RIGHT
;
4415 // try local handlers
4416 switch ( event
.GetKeyCode() )
4419 if ( event
.ControlDown() )
4420 MoveCursorUpBlock( event
.ShiftDown() );
4422 MoveCursorUp( event
.ShiftDown() );
4426 if ( event
.ControlDown() )
4427 MoveCursorDownBlock( event
.ShiftDown() );
4429 MoveCursorDown( event
.ShiftDown() );
4433 if ( event
.ControlDown() )
4434 MoveCursorLeftBlock( event
.ShiftDown() );
4436 MoveCursorLeft( event
.ShiftDown() );
4440 if ( event
.ControlDown() )
4441 MoveCursorRightBlock( event
.ShiftDown() );
4443 MoveCursorRight( event
.ShiftDown() );
4447 case WXK_NUMPAD_ENTER
:
4448 if ( event
.ControlDown() )
4450 event
.Skip(); // to let the edit control have the return
4454 if ( GetGridCursorRow() < GetNumberRows()-1 )
4456 MoveCursorDown( event
.ShiftDown() );
4460 // at the bottom of a column
4461 DisableCellEditControl();
4471 if (event
.ShiftDown())
4473 if ( GetGridCursorCol() > 0 )
4475 MoveCursorLeft( false );
4480 DisableCellEditControl();
4485 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4487 MoveCursorRight( false );
4492 DisableCellEditControl();
4498 if ( event
.ControlDown() )
4509 if ( event
.ControlDown() )
4511 GoToCell(m_numRows
- 1, m_numCols
- 1);
4528 // Ctrl-Space selects the current column, Shift-Space -- the
4529 // current row and Ctrl-Shift-Space -- everything
4530 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4533 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4537 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4540 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4541 m_selection
->SelectBlock(0, 0,
4542 m_numRows
- 1, m_numCols
- 1);
4546 if ( !IsEditable() )
4548 MoveCursorRight(false);
4551 //else: fall through
4564 m_inOnKeyDown
= false;
4567 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4569 // try local handlers
4571 if ( event
.GetKeyCode() == WXK_SHIFT
)
4573 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4574 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4578 m_selection
->SelectBlock(
4579 m_selectedBlockTopLeft
,
4580 m_selectedBlockBottomRight
,
4585 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4586 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4587 m_selectedBlockCorner
= wxGridNoCellCoords
;
4591 void wxGrid::OnChar( wxKeyEvent
& event
)
4593 // is it possible to edit the current cell at all?
4594 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4596 // yes, now check whether the cells editor accepts the key
4597 int row
= m_currentCellCoords
.GetRow();
4598 int col
= m_currentCellCoords
.GetCol();
4599 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4600 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4602 // <F2> is special and will always start editing, for
4603 // other keys - ask the editor itself
4604 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4605 || editor
->IsAcceptedKey(event
) )
4607 // ensure cell is visble
4608 MakeCellVisible(row
, col
);
4609 EnableCellEditControl();
4611 // a problem can arise if the cell is not completely
4612 // visible (even after calling MakeCellVisible the
4613 // control is not created and calling StartingKey will
4615 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4616 editor
->StartingKey(event
);
4632 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4636 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4638 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4640 // the event has been vetoed - do nothing
4644 #if !defined(__WXMAC__)
4645 wxClientDC
dc( m_gridWin
);
4649 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4651 DisableCellEditControl();
4653 if ( IsVisible( m_currentCellCoords
, false ) )
4656 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4657 if ( !m_gridLinesEnabled
)
4665 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4667 // Otherwise refresh redraws the highlight!
4668 m_currentCellCoords
= coords
;
4670 #if defined(__WXMAC__)
4671 m_gridWin
->Refresh(true /*, & r */);
4673 DrawGridCellArea( dc
, cells
);
4674 DrawAllGridLines( dc
, r
);
4679 m_currentCellCoords
= coords
;
4681 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4682 #if !defined(__WXMAC__)
4683 DrawCellHighlight( dc
, attr
);
4691 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4692 int bottomRow
, int rightCol
)
4696 switch ( m_selection
->GetSelectionMode() )
4699 wxFAIL_MSG( "unknown selection mode" );
4702 case wxGridSelectCells
:
4703 // arbitrary blocks selection allowed so just use the cell
4704 // coordinates as is
4707 case wxGridSelectRows
:
4708 // only full rows selection allowd, ensure that we do select
4711 rightCol
= GetNumberCols() - 1;
4714 case wxGridSelectColumns
:
4715 // same as above but for columns
4717 bottomRow
= GetNumberRows() - 1;
4720 case wxGridSelectRowsOrColumns
:
4721 // in this mode we can select only full rows or full columns so
4722 // it doesn't make sense to select blocks at all (and we can't
4723 // extend the block because there is no preferred direction, we
4724 // could only extend it to cover the entire grid but this is
4730 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4731 MakeCellVisible(m_selectedBlockCorner
);
4733 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4734 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4736 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4737 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4739 // First the case that we selected a completely new area
4740 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4741 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4744 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4745 wxGridCellCoords ( bottomRow
, rightCol
) );
4746 m_gridWin
->Refresh( false, &rect
);
4749 // Now handle changing an existing selection area.
4750 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4751 m_selectedBlockBottomRight
!= updateBottomRight
)
4753 // Compute two optimal update rectangles:
4754 // Either one rectangle is a real subset of the
4755 // other, or they are (almost) disjoint!
4757 bool need_refresh
[4];
4761 need_refresh
[3] = false;
4764 // Store intermediate values
4765 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4766 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4767 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4768 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4770 // Determine the outer/inner coordinates.
4771 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4772 EnsureFirstLessThanSecond(oldTop
, topRow
);
4773 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4774 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4776 // Now, either the stuff marked old is the outer
4777 // rectangle or we don't have a situation where one
4778 // is contained in the other.
4780 if ( oldLeft
< leftCol
)
4782 // Refresh the newly selected or deselected
4783 // area to the left of the old or new selection.
4784 need_refresh
[0] = true;
4785 rect
[0] = BlockToDeviceRect(
4786 wxGridCellCoords( oldTop
, oldLeft
),
4787 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4790 if ( oldTop
< topRow
)
4792 // Refresh the newly selected or deselected
4793 // area above the old or new selection.
4794 need_refresh
[1] = true;
4795 rect
[1] = BlockToDeviceRect(
4796 wxGridCellCoords( oldTop
, leftCol
),
4797 wxGridCellCoords( topRow
- 1, rightCol
) );
4800 if ( oldRight
> rightCol
)
4802 // Refresh the newly selected or deselected
4803 // area to the right of the old or new selection.
4804 need_refresh
[2] = true;
4805 rect
[2] = BlockToDeviceRect(
4806 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4807 wxGridCellCoords( oldBottom
, oldRight
) );
4810 if ( oldBottom
> bottomRow
)
4812 // Refresh the newly selected or deselected
4813 // area below the old or new selection.
4814 need_refresh
[3] = true;
4815 rect
[3] = BlockToDeviceRect(
4816 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4817 wxGridCellCoords( oldBottom
, rightCol
) );
4820 // various Refresh() calls
4821 for (i
= 0; i
< 4; i
++ )
4822 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4823 m_gridWin
->Refresh( false, &(rect
[i
]) );
4827 m_selectedBlockTopLeft
= updateTopLeft
;
4828 m_selectedBlockBottomRight
= updateBottomRight
;
4832 // ------ functions to get/send data (see also public functions)
4835 bool wxGrid::GetModelValues()
4837 // Hide the editor, so it won't hide a changed value.
4838 HideCellEditControl();
4842 // all we need to do is repaint the grid
4844 m_gridWin
->Refresh();
4851 bool wxGrid::SetModelValues()
4855 // Disable the editor, so it won't hide a changed value.
4856 // Do we also want to save the current value of the editor first?
4858 DisableCellEditControl();
4862 for ( row
= 0; row
< m_numRows
; row
++ )
4864 for ( col
= 0; col
< m_numCols
; col
++ )
4866 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4876 // Note - this function only draws cells that are in the list of
4877 // exposed cells (usually set from the update region by
4878 // CalcExposedCells)
4880 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
4882 if ( !m_numRows
|| !m_numCols
)
4885 int i
, numCells
= cells
.GetCount();
4886 int row
, col
, cell_rows
, cell_cols
;
4887 wxGridCellCoordsArray redrawCells
;
4889 for ( i
= numCells
- 1; i
>= 0; i
-- )
4891 row
= cells
[i
].GetRow();
4892 col
= cells
[i
].GetCol();
4893 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
4895 // If this cell is part of a multicell block, find owner for repaint
4896 if ( cell_rows
<= 0 || cell_cols
<= 0 )
4898 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
4899 bool marked
= false;
4900 for ( int j
= 0; j
< numCells
; j
++ )
4902 if ( cell
== cells
[j
] )
4911 int count
= redrawCells
.GetCount();
4912 for (int j
= 0; j
< count
; j
++)
4914 if ( cell
== redrawCells
[j
] )
4922 redrawCells
.Add( cell
);
4925 // don't bother drawing this cell
4929 // If this cell is empty, find cell to left that might want to overflow
4930 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
4932 for ( int l
= 0; l
< cell_rows
; l
++ )
4934 // find a cell in this row to leave already marked for repaint
4936 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
4937 if ((redrawCells
[k
].GetCol() < left
) &&
4938 (redrawCells
[k
].GetRow() == row
))
4940 left
= redrawCells
[k
].GetCol();
4944 left
= 0; // oh well
4946 for (int j
= col
- 1; j
>= left
; j
--)
4948 if (!m_table
->IsEmptyCell(row
+ l
, j
))
4950 if (GetCellOverflow(row
+ l
, j
))
4952 wxGridCellCoords
cell(row
+ l
, j
);
4953 bool marked
= false;
4955 for (int k
= 0; k
< numCells
; k
++)
4957 if ( cell
== cells
[k
] )
4966 int count
= redrawCells
.GetCount();
4967 for (int k
= 0; k
< count
; k
++)
4969 if ( cell
== redrawCells
[k
] )
4976 redrawCells
.Add( cell
);
4985 DrawCell( dc
, cells
[i
] );
4988 numCells
= redrawCells
.GetCount();
4990 for ( i
= numCells
- 1; i
>= 0; i
-- )
4992 DrawCell( dc
, redrawCells
[i
] );
4996 void wxGrid::DrawGridSpace( wxDC
& dc
)
4999 m_gridWin
->GetClientSize( &cw
, &ch
);
5002 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5004 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5005 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5007 if ( right
> rightCol
|| bottom
> bottomRow
)
5010 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5012 dc
.SetBrush(GetDefaultCellBackgroundColour());
5013 dc
.SetPen( *wxTRANSPARENT_PEN
);
5015 if ( right
> rightCol
)
5017 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5020 if ( bottom
> bottomRow
)
5022 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5027 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5029 int row
= coords
.GetRow();
5030 int col
= coords
.GetCol();
5032 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5035 // we draw the cell border ourselves
5036 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5038 bool isCurrent
= coords
== m_currentCellCoords
;
5040 wxRect rect
= CellToRect( row
, col
);
5042 // if the editor is shown, we should use it and not the renderer
5043 // Note: However, only if it is really _shown_, i.e. not hidden!
5044 if ( isCurrent
&& IsCellEditControlShown() )
5046 // NB: this "#if..." is temporary and fixes a problem where the
5047 // edit control is erased by this code after being rendered.
5048 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5049 // implicitly, causing this out-of order render.
5050 #if !defined(__WXMAC__)
5051 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5052 editor
->PaintBackground(rect
, attr
);
5058 // but all the rest is drawn by the cell renderer and hence may be customized
5059 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5060 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5067 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5069 // don't show highlight when the grid doesn't have focus
5073 int row
= m_currentCellCoords
.GetRow();
5074 int col
= m_currentCellCoords
.GetCol();
5076 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5079 wxRect rect
= CellToRect(row
, col
);
5081 // hmmm... what could we do here to show that the cell is disabled?
5082 // for now, I just draw a thinner border than for the other ones, but
5083 // it doesn't look really good
5085 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5089 // The center of the drawn line is where the position/width/height of
5090 // the rectangle is actually at (on wxMSW at least), so the
5091 // size of the rectangle is reduced to compensate for the thickness of
5092 // the line. If this is too strange on non-wxMSW platforms then
5093 // please #ifdef this appropriately.
5094 rect
.x
+= penWidth
/ 2;
5095 rect
.y
+= penWidth
/ 2;
5096 rect
.width
-= penWidth
- 1;
5097 rect
.height
-= penWidth
- 1;
5099 // Now draw the rectangle
5100 // use the cellHighlightColour if the cell is inside a selection, this
5101 // will ensure the cell is always visible.
5102 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5103 : m_cellHighlightColour
,
5105 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5106 dc
.DrawRectangle(rect
);
5110 wxPen
wxGrid::GetDefaultGridLinePen()
5112 return wxPen(GetGridLineColour());
5115 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5117 return GetDefaultGridLinePen();
5120 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5122 return GetDefaultGridLinePen();
5125 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5127 int row
= coords
.GetRow();
5128 int col
= coords
.GetCol();
5129 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5133 wxRect rect
= CellToRect( row
, col
);
5135 // right hand border
5136 dc
.SetPen( GetColGridLinePen(col
) );
5137 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5138 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5141 dc
.SetPen( GetRowGridLinePen(row
) );
5142 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5143 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5146 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5148 // This if block was previously in wxGrid::OnPaint but that doesn't
5149 // seem to get called under wxGTK - MB
5151 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5152 m_numRows
&& m_numCols
)
5154 m_currentCellCoords
.Set(0, 0);
5157 if ( IsCellEditControlShown() )
5159 // don't show highlight when the edit control is shown
5163 // if the active cell was repainted, repaint its highlight too because it
5164 // might have been damaged by the grid lines
5165 size_t count
= cells
.GetCount();
5166 for ( size_t n
= 0; n
< count
; n
++ )
5168 wxGridCellCoords cell
= cells
[n
];
5170 // If we are using attributes, then we may have just exposed another
5171 // cell in a partially-visible merged cluster of cells. If the "anchor"
5172 // (upper left) cell of this merged cluster is the cell indicated by
5173 // m_currentCellCoords, then we need to refresh the cell highlight even
5174 // though the "anchor" itself is not part of our update segment.
5175 if ( CanHaveAttributes() )
5179 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5182 cell
.SetRow(cell
.GetRow() + rows
);
5185 cell
.SetCol(cell
.GetCol() + cols
);
5188 if ( cell
== m_currentCellCoords
)
5190 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5191 DrawCellHighlight(dc
, attr
);
5199 // This is used to redraw all grid lines e.g. when the grid line colour
5202 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5204 if ( !m_gridLinesEnabled
)
5207 int top
, bottom
, left
, right
;
5210 m_gridWin
->GetClientSize(&cw
, &ch
);
5211 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5212 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5214 // avoid drawing grid lines past the last row and col
5215 if ( m_gridLinesClipHorz
)
5220 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5221 if ( right
> lastColRight
)
5222 right
= lastColRight
;
5225 if ( m_gridLinesClipVert
)
5230 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5231 if ( bottom
> lastRowBottom
)
5232 bottom
= lastRowBottom
;
5235 // no gridlines inside multicells, clip them out
5236 int leftCol
= GetColPos( internalXToCol(left
) );
5237 int topRow
= internalYToRow(top
);
5238 int rightCol
= GetColPos( internalXToCol(right
) );
5239 int bottomRow
= internalYToRow(bottom
);
5241 wxRegion
clippedcells(0, 0, cw
, ch
);
5243 int cell_rows
, cell_cols
;
5246 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5248 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5250 int i
= GetColAt( colPos
);
5252 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5253 if ((cell_rows
> 1) || (cell_cols
> 1))
5255 rect
= CellToRect(j
,i
);
5256 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5257 clippedcells
.Subtract(rect
);
5259 else if ((cell_rows
< 0) || (cell_cols
< 0))
5261 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5262 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5263 clippedcells
.Subtract(rect
);
5268 dc
.SetDeviceClippingRegion( clippedcells
);
5271 // horizontal grid lines
5272 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5274 int bot
= GetRowBottom(i
) - 1;
5281 dc
.SetPen( GetRowGridLinePen(i
) );
5282 dc
.DrawLine( left
, bot
, right
, bot
);
5286 // vertical grid lines
5287 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5289 int i
= GetColAt( colPos
);
5291 int colRight
= GetColRight(i
);
5293 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5297 if ( colRight
> right
)
5300 if ( colRight
>= left
)
5302 dc
.SetPen( GetColGridLinePen(i
) );
5303 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5307 dc
.DestroyClippingRegion();
5310 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5315 const size_t numLabels
= rows
.GetCount();
5316 for ( size_t i
= 0; i
< numLabels
; i
++ )
5318 DrawRowLabel( dc
, rows
[i
] );
5322 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5324 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5329 int rowTop
= GetRowTop(row
),
5330 rowBottom
= GetRowBottom(row
) - 1;
5332 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
5333 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
5334 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5335 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
5337 dc
.SetPen( *wxWHITE_PEN
);
5338 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
5339 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
5341 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
5342 dc
.SetTextForeground( GetLabelTextColour() );
5343 dc
.SetFont( GetLabelFont() );
5346 GetRowLabelAlignment( &hAlign
, &vAlign
);
5349 rect
.SetY( GetRowTop(row
) + 2 );
5350 rect
.SetWidth( m_rowLabelWidth
- 4 );
5351 rect
.SetHeight( GetRowHeight(row
) - 4 );
5352 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5355 void wxGrid::UseNativeColHeader(bool native
)
5357 if ( native
== m_useNativeHeader
)
5361 m_useNativeHeader
= native
;
5363 CreateColumnWindow();
5365 if ( m_useNativeHeader
)
5366 GetGridColHeader()->SetColumnCount(m_numCols
);
5370 void wxGrid::SetUseNativeColLabels( bool native
)
5372 wxASSERT_MSG( !m_useNativeHeader
,
5373 "doesn't make sense when using native header" );
5375 m_nativeColumnLabels
= native
;
5378 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5379 SetColLabelSize( height
);
5382 GetColLabelWindow()->Refresh();
5383 m_cornerLabelWin
->Refresh();
5386 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5391 const size_t numLabels
= cols
.GetCount();
5392 for ( size_t i
= 0; i
< numLabels
; i
++ )
5394 DrawColLabel( dc
, cols
[i
] );
5398 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5400 if ( m_nativeColumnLabels
)
5402 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5405 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5409 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
5410 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
5411 m_rowLabelWidth
- 1, 0 );
5412 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
5413 0, m_colLabelHeight
- 1 );
5414 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
5415 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
5417 dc
.SetPen( *wxWHITE_PEN
);
5418 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
5419 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
5423 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5425 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5428 int colLeft
= GetColLeft(col
);
5430 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5432 if ( m_nativeColumnLabels
)
5434 wxRendererNative::Get().DrawHeaderButton
5436 GetColLabelWindow(),
5441 ? IsSortOrderAscending()
5442 ? wxHDR_SORT_ICON_UP
5443 : wxHDR_SORT_ICON_DOWN
5444 : wxHDR_SORT_ICON_NONE
5449 int colRight
= GetColRight(col
) - 1;
5451 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
5452 dc
.DrawLine( colRight
, 0,
5453 colRight
, m_colLabelHeight
- 1 );
5454 dc
.DrawLine( colLeft
, 0,
5456 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
5457 colRight
+ 1, m_colLabelHeight
- 1 );
5459 dc
.SetPen( *wxWHITE_PEN
);
5460 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
5461 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5464 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
5465 dc
.SetTextForeground( GetLabelTextColour() );
5466 dc
.SetFont( GetLabelFont() );
5469 GetColLabelAlignment( &hAlign
, &vAlign
);
5470 const int orient
= GetColLabelTextOrientation();
5473 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5476 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5477 // we just have to add textOrientation support
5478 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5479 const wxString
& value
,
5483 int textOrientation
)
5485 wxArrayString lines
;
5487 StringToLines( value
, lines
);
5489 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5492 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5493 const wxArrayString
& lines
,
5497 int textOrientation
)
5499 if ( lines
.empty() )
5502 wxDCClipper
clip(dc
, rect
);
5507 if ( textOrientation
== wxHORIZONTAL
)
5508 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5510 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5514 switch ( vertAlign
)
5516 case wxALIGN_BOTTOM
:
5517 if ( textOrientation
== wxHORIZONTAL
)
5518 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5520 x
= rect
.x
+ rect
.width
- textWidth
;
5523 case wxALIGN_CENTRE
:
5524 if ( textOrientation
== wxHORIZONTAL
)
5525 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5527 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5532 if ( textOrientation
== wxHORIZONTAL
)
5539 // Align each line of a multi-line label
5540 size_t nLines
= lines
.GetCount();
5541 for ( size_t l
= 0; l
< nLines
; l
++ )
5543 const wxString
& line
= lines
[l
];
5547 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5551 wxCoord lineWidth
= 0,
5553 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5555 switch ( horizAlign
)
5558 if ( textOrientation
== wxHORIZONTAL
)
5559 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5561 y
= rect
.y
+ lineWidth
+ 1;
5564 case wxALIGN_CENTRE
:
5565 if ( textOrientation
== wxHORIZONTAL
)
5566 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5568 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5573 if ( textOrientation
== wxHORIZONTAL
)
5576 y
= rect
.y
+ rect
.height
- 1;
5580 if ( textOrientation
== wxHORIZONTAL
)
5582 dc
.DrawText( line
, x
, y
);
5587 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5593 // Split multi-line text up into an array of strings.
5594 // Any existing contents of the string array are preserved.
5596 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5597 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5601 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5602 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5604 while ( startPos
< (int)tVal
.length() )
5606 pos
= tVal
.Mid(startPos
).Find( eol
);
5611 else if ( pos
== 0 )
5613 lines
.Add( wxEmptyString
);
5617 lines
.Add( tVal
.Mid(startPos
, pos
) );
5620 startPos
+= pos
+ 1;
5623 if ( startPos
< (int)tVal
.length() )
5625 lines
.Add( tVal
.Mid( startPos
) );
5629 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5630 const wxArrayString
& lines
,
5631 long *width
, long *height
) const
5635 wxCoord lineW
= 0, lineH
= 0;
5638 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5640 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5641 w
= wxMax( w
, lineW
);
5650 // ------ Batch processing.
5652 void wxGrid::EndBatch()
5654 if ( m_batchCount
> 0 )
5657 if ( !m_batchCount
)
5660 m_rowLabelWin
->Refresh();
5661 m_colWindow
->Refresh();
5662 m_cornerLabelWin
->Refresh();
5663 m_gridWin
->Refresh();
5668 // Use this, rather than wxWindow::Refresh(), to force an immediate
5669 // repainting of the grid. Has no effect if you are already inside a
5670 // BeginBatch / EndBatch block.
5672 void wxGrid::ForceRefresh()
5678 bool wxGrid::Enable(bool enable
)
5680 if ( !wxScrolledWindow::Enable(enable
) )
5683 // redraw in the new state
5684 m_gridWin
->Refresh();
5690 // ------ Edit control functions
5693 void wxGrid::EnableEditing( bool edit
)
5695 if ( edit
!= m_editable
)
5698 EnableCellEditControl(edit
);
5703 void wxGrid::EnableCellEditControl( bool enable
)
5708 if ( enable
!= m_cellEditCtrlEnabled
)
5712 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5715 // this should be checked by the caller!
5716 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
5718 // do it before ShowCellEditControl()
5719 m_cellEditCtrlEnabled
= enable
;
5721 ShowCellEditControl();
5725 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5727 HideCellEditControl();
5728 SaveEditControlValue();
5730 // do it after HideCellEditControl()
5731 m_cellEditCtrlEnabled
= enable
;
5736 bool wxGrid::IsCurrentCellReadOnly() const
5739 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5740 bool readonly
= attr
->IsReadOnly();
5746 bool wxGrid::CanEnableCellControl() const
5748 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5749 !IsCurrentCellReadOnly();
5752 bool wxGrid::IsCellEditControlEnabled() const
5754 // the cell edit control might be disable for all cells or just for the
5755 // current one if it's read only
5756 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5759 bool wxGrid::IsCellEditControlShown() const
5761 bool isShown
= false;
5763 if ( m_cellEditCtrlEnabled
)
5765 int row
= m_currentCellCoords
.GetRow();
5766 int col
= m_currentCellCoords
.GetCol();
5767 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5768 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5773 if ( editor
->IsCreated() )
5775 isShown
= editor
->GetControl()->IsShown();
5785 void wxGrid::ShowCellEditControl()
5787 if ( IsCellEditControlEnabled() )
5789 if ( !IsVisible( m_currentCellCoords
, false ) )
5791 m_cellEditCtrlEnabled
= false;
5796 wxRect rect
= CellToRect( m_currentCellCoords
);
5797 int row
= m_currentCellCoords
.GetRow();
5798 int col
= m_currentCellCoords
.GetCol();
5800 // if this is part of a multicell, find owner (topleft)
5801 int cell_rows
, cell_cols
;
5802 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5803 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5807 m_currentCellCoords
.SetRow( row
);
5808 m_currentCellCoords
.SetCol( col
);
5811 // erase the highlight and the cell contents because the editor
5812 // might not cover the entire cell
5813 wxClientDC
dc( m_gridWin
);
5815 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5816 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5817 dc
.SetPen(*wxTRANSPARENT_PEN
);
5818 dc
.DrawRectangle(rect
);
5820 // convert to scrolled coords
5821 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5827 // cell is shifted by one pixel
5828 // However, don't allow x or y to become negative
5829 // since the SetSize() method interprets that as
5836 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5837 if ( !editor
->IsCreated() )
5839 editor
->Create(m_gridWin
, wxID_ANY
,
5840 new wxGridCellEditorEvtHandler(this, editor
));
5842 wxGridEditorCreatedEvent
evt(GetId(),
5843 wxEVT_GRID_EDITOR_CREATED
,
5847 editor
->GetControl());
5848 GetEventHandler()->ProcessEvent(evt
);
5851 // resize editor to overflow into righthand cells if allowed
5852 int maxWidth
= rect
.width
;
5853 wxString value
= GetCellValue(row
, col
);
5854 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
5857 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
5858 if (maxWidth
< rect
.width
)
5859 maxWidth
= rect
.width
;
5862 int client_right
= m_gridWin
->GetClientSize().GetWidth();
5863 if (rect
.x
+ maxWidth
> client_right
)
5864 maxWidth
= client_right
- rect
.x
;
5866 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
5868 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5869 // may have changed earlier
5870 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
5873 GetCellSize( row
, i
, &c_rows
, &c_cols
);
5875 // looks weird going over a multicell
5876 if (m_table
->IsEmptyCell( row
, i
) &&
5877 (rect
.width
< maxWidth
) && (c_rows
== 1))
5879 rect
.width
+= GetColWidth( i
);
5885 if (rect
.GetRight() > client_right
)
5886 rect
.SetRight( client_right
- 1 );
5889 editor
->SetCellAttr( attr
);
5890 editor
->SetSize( rect
);
5892 editor
->GetControl()->Move(
5893 editor
->GetControl()->GetPosition().x
+ nXMove
,
5894 editor
->GetControl()->GetPosition().y
);
5895 editor
->Show( true, attr
);
5897 // recalc dimensions in case we need to
5898 // expand the scrolled window to account for editor
5901 editor
->BeginEdit(row
, col
, this);
5902 editor
->SetCellAttr(NULL
);
5910 void wxGrid::HideCellEditControl()
5912 if ( IsCellEditControlEnabled() )
5914 int row
= m_currentCellCoords
.GetRow();
5915 int col
= m_currentCellCoords
.GetCol();
5917 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5918 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5919 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
5920 editor
->Show( false );
5924 // return the focus to the grid itself if the editor had it
5926 // note that we must not do this unconditionally to avoid stealing
5927 // focus from the window which just received it if we are hiding the
5928 // editor precisely because we lost focus
5929 if ( editorHadFocus
)
5930 m_gridWin
->SetFocus();
5932 // refresh whole row to the right
5933 wxRect
rect( CellToRect(row
, col
) );
5934 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5935 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
5938 // ensure that the pixels under the focus ring get refreshed as well
5939 rect
.Inflate(10, 10);
5942 m_gridWin
->Refresh( false, &rect
);
5946 void wxGrid::SaveEditControlValue()
5948 if ( IsCellEditControlEnabled() )
5950 int row
= m_currentCellCoords
.GetRow();
5951 int col
= m_currentCellCoords
.GetCol();
5953 wxString oldval
= GetCellValue(row
, col
);
5955 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5956 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5959 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
5961 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
5963 editor
->ApplyEdit(row
, col
, this);
5965 // for compatibility reasons dating back to wx 2.8 when this event
5966 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
5967 // didn't exist we allow vetoing this one too
5968 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
5970 // Event has been vetoed, set the data back.
5971 SetCellValue(row
, col
, oldval
);
5981 // ------ Grid location functions
5982 // Note that all of these functions work with the logical coordinates of
5983 // grid cells and labels so you will need to convert from device
5984 // coordinates for mouse events etc.
5987 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
5989 int row
= YToRow(y
);
5990 int col
= XToCol(x
);
5992 return row
== -1 || col
== -1 ? wxGridNoCellCoords
5993 : wxGridCellCoords(row
, col
);
5996 // compute row or column from some (unscrolled) coordinate value, using either
5997 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
5998 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6000 int wxGrid::PosToLinePos(int coord
,
6002 const wxGridOperations
& oper
) const
6004 const int numLines
= oper
.GetNumberOfLines(this);
6007 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6009 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6010 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6012 int maxPos
= coord
/ defaultLineSize
,
6015 // check for the simplest case: if we have no explicit line sizes
6016 // configured, then we already know the line this position falls in
6017 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6018 if ( lineEnds
.empty() )
6020 if ( maxPos
< numLines
)
6023 return clipToMinMax
? numLines
- 1 : -1;
6027 // adjust maxPos before starting the binary search
6028 if ( maxPos
>= numLines
)
6030 maxPos
= numLines
- 1;
6034 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6037 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6039 maxPos
= coord
/ minDist
;
6041 maxPos
= numLines
- 1;
6044 if ( maxPos
>= numLines
)
6045 maxPos
= numLines
- 1;
6048 // check if the position is beyond the last column
6049 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6050 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6051 return clipToMinMax
? maxPos
: -1;
6053 // or before the first one
6054 const int lineAt0
= oper
.GetLineAt(this, 0);
6055 if ( coord
< lineEnds
[lineAt0
] )
6059 // finally do perform the binary search
6060 while ( minPos
< maxPos
)
6062 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6063 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6065 "wxGrid: internal error in PosToLinePos()" );
6067 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6072 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6073 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6083 wxGrid::PosToLine(int coord
,
6085 const wxGridOperations
& oper
) const
6087 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6089 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6092 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6094 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6097 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6099 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6102 int wxGrid::XToPos(int x
) const
6104 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6107 // return the row number such that the y coord is near the edge of, or -1 if
6108 // not near an edge.
6110 // notice that position can only possibly be near an edge if the row/column is
6111 // large enough to still allow for an "inner" area that is _not_ near the edge
6112 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6113 // _never_ be considered to be near the edge).
6114 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6116 const int line
= oper
.PosToLine(this, pos
, true);
6118 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6120 // We know that we are in this line, test whether we are close enough
6121 // to start or end border, respectively.
6122 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6124 else if ( line
> 0 &&
6125 pos
- oper
.GetLineStartPos(this,
6126 line
) < WXGRID_LABEL_EDGE_ZONE
)
6133 int wxGrid::YToEdgeOfRow(int y
) const
6135 return PosToEdgeOfLine(y
, wxGridRowOperations());
6138 int wxGrid::XToEdgeOfCol(int x
) const
6140 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6143 wxRect
wxGrid::CellToRect( int row
, int col
) const
6145 wxRect
rect( -1, -1, -1, -1 );
6147 if ( row
>= 0 && row
< m_numRows
&&
6148 col
>= 0 && col
< m_numCols
)
6150 int i
, cell_rows
, cell_cols
;
6151 rect
.width
= rect
.height
= 0;
6152 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6153 // if negative then find multicell owner
6158 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6160 rect
.x
= GetColLeft(col
);
6161 rect
.y
= GetRowTop(row
);
6162 for (i
=col
; i
< col
+ cell_cols
; i
++)
6163 rect
.width
+= GetColWidth(i
);
6164 for (i
=row
; i
< row
+ cell_rows
; i
++)
6165 rect
.height
+= GetRowHeight(i
);
6167 // if grid lines are enabled, then the area of the cell is a bit smaller
6168 if (m_gridLinesEnabled
)
6178 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6180 // get the cell rectangle in logical coords
6182 wxRect
r( CellToRect( row
, col
) );
6184 // convert to device coords
6186 int left
, top
, right
, bottom
;
6187 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6188 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6190 // check against the client area of the grid window
6192 m_gridWin
->GetClientSize( &cw
, &ch
);
6194 if ( wholeCellVisible
)
6196 // is the cell wholly visible ?
6197 return ( left
>= 0 && right
<= cw
&&
6198 top
>= 0 && bottom
<= ch
);
6202 // is the cell partly visible ?
6204 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6205 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6209 // make the specified cell location visible by doing a minimal amount
6212 void wxGrid::MakeCellVisible( int row
, int col
)
6215 int xpos
= -1, ypos
= -1;
6217 if ( row
>= 0 && row
< m_numRows
&&
6218 col
>= 0 && col
< m_numCols
)
6220 // get the cell rectangle in logical coords
6221 wxRect
r( CellToRect( row
, col
) );
6223 // convert to device coords
6224 int left
, top
, right
, bottom
;
6225 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6226 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6229 m_gridWin
->GetClientSize( &cw
, &ch
);
6235 else if ( bottom
> ch
)
6237 int h
= r
.GetHeight();
6239 for ( i
= row
- 1; i
>= 0; i
-- )
6241 int rowHeight
= GetRowHeight(i
);
6242 if ( h
+ rowHeight
> ch
)
6249 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6250 // have rounding errors (this is important, because if we do,
6251 // we might not scroll at all and some cells won't be redrawn)
6253 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6254 // so just add a full scroll unit...
6255 ypos
+= m_scrollLineY
;
6258 // special handling for wide cells - show always left part of the cell!
6259 // Otherwise, e.g. when stepping from row to row, it would jump between
6260 // left and right part of the cell on every step!
6262 if ( left
< 0 || (right
- left
) >= cw
)
6266 else if ( right
> cw
)
6268 // position the view so that the cell is on the right
6270 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6271 xpos
= x0
+ (right
- cw
);
6273 // see comment for ypos above
6274 xpos
+= m_scrollLineX
;
6277 if ( xpos
!= -1 || ypos
!= -1 )
6280 xpos
/= m_scrollLineX
;
6282 ypos
/= m_scrollLineY
;
6283 Scroll( xpos
, ypos
);
6290 // ------ Grid cursor movement functions
6294 wxGrid::DoMoveCursor(bool expandSelection
,
6295 const wxGridDirectionOperations
& diroper
)
6297 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6300 if ( expandSelection
)
6302 wxGridCellCoords coords
= m_selectedBlockCorner
;
6303 if ( coords
== wxGridNoCellCoords
)
6304 coords
= m_currentCellCoords
;
6306 if ( diroper
.IsAtBoundary(coords
) )
6309 diroper
.Advance(coords
);
6311 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6313 else // don't expand selection
6317 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6320 wxGridCellCoords coords
= m_currentCellCoords
;
6321 diroper
.Advance(coords
);
6329 bool wxGrid::MoveCursorUp(bool expandSelection
)
6331 return DoMoveCursor(expandSelection
,
6332 wxGridBackwardOperations(this, wxGridRowOperations()));
6335 bool wxGrid::MoveCursorDown(bool expandSelection
)
6337 return DoMoveCursor(expandSelection
,
6338 wxGridForwardOperations(this, wxGridRowOperations()));
6341 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6343 return DoMoveCursor(expandSelection
,
6344 wxGridBackwardOperations(this, wxGridColumnOperations()));
6347 bool wxGrid::MoveCursorRight(bool expandSelection
)
6349 return DoMoveCursor(expandSelection
,
6350 wxGridForwardOperations(this, wxGridColumnOperations()));
6353 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6355 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6358 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6361 const int oldRow
= m_currentCellCoords
.GetRow();
6362 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6363 if ( newRow
== oldRow
)
6365 wxGridCellCoords
coords(m_currentCellCoords
);
6366 diroper
.Advance(coords
);
6367 newRow
= coords
.GetRow();
6370 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6375 bool wxGrid::MovePageUp()
6377 return DoMoveCursorByPage(
6378 wxGridBackwardOperations(this, wxGridRowOperations()));
6381 bool wxGrid::MovePageDown()
6383 return DoMoveCursorByPage(
6384 wxGridForwardOperations(this, wxGridRowOperations()));
6387 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6388 // until we find a non-empty cell or reach the grid end
6390 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6391 const wxGridDirectionOperations
& diroper
)
6393 while ( !diroper
.IsAtBoundary(coords
) )
6395 diroper
.Advance(coords
);
6396 if ( !m_table
->IsEmpty(coords
) )
6402 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6403 const wxGridDirectionOperations
& diroper
)
6405 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6408 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6411 wxGridCellCoords
coords(m_currentCellCoords
);
6412 if ( m_table
->IsEmpty(coords
) )
6414 // we are in an empty cell: find the next block of non-empty cells
6415 AdvanceToNextNonEmpty(coords
, diroper
);
6417 else // current cell is not empty
6419 diroper
.Advance(coords
);
6420 if ( m_table
->IsEmpty(coords
) )
6422 // we started at the end of a block, find the next one
6423 AdvanceToNextNonEmpty(coords
, diroper
);
6425 else // we're in a middle of a block
6427 // go to the end of it, i.e. find the last cell before the next
6429 while ( !diroper
.IsAtBoundary(coords
) )
6431 wxGridCellCoords
coordsNext(coords
);
6432 diroper
.Advance(coordsNext
);
6433 if ( m_table
->IsEmpty(coordsNext
) )
6436 coords
= coordsNext
;
6441 if ( expandSelection
)
6443 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6454 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6456 return DoMoveCursorByBlock(
6458 wxGridBackwardOperations(this, wxGridRowOperations())
6462 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6464 return DoMoveCursorByBlock(
6466 wxGridForwardOperations(this, wxGridRowOperations())
6470 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6472 return DoMoveCursorByBlock(
6474 wxGridBackwardOperations(this, wxGridColumnOperations())
6478 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6480 return DoMoveCursorByBlock(
6482 wxGridForwardOperations(this, wxGridColumnOperations())
6487 // ------ Label values and formatting
6490 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6493 *horiz
= m_rowLabelHorizAlign
;
6495 *vert
= m_rowLabelVertAlign
;
6498 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6501 *horiz
= m_colLabelHorizAlign
;
6503 *vert
= m_colLabelVertAlign
;
6506 int wxGrid::GetColLabelTextOrientation() const
6508 return m_colLabelTextOrientation
;
6511 wxString
wxGrid::GetRowLabelValue( int row
) const
6515 return m_table
->GetRowLabelValue( row
);
6525 wxString
wxGrid::GetColLabelValue( int col
) const
6529 return m_table
->GetColLabelValue( col
);
6539 void wxGrid::SetRowLabelSize( int width
)
6541 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6543 if ( width
== wxGRID_AUTOSIZE
)
6545 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6548 if ( width
!= m_rowLabelWidth
)
6552 m_rowLabelWin
->Show( false );
6553 m_cornerLabelWin
->Show( false );
6555 else if ( m_rowLabelWidth
== 0 )
6557 m_rowLabelWin
->Show( true );
6558 if ( m_colLabelHeight
> 0 )
6559 m_cornerLabelWin
->Show( true );
6562 m_rowLabelWidth
= width
;
6564 wxScrolledWindow::Refresh( true );
6568 void wxGrid::SetColLabelSize( int height
)
6570 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6572 if ( height
== wxGRID_AUTOSIZE
)
6574 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6577 if ( height
!= m_colLabelHeight
)
6581 m_colWindow
->Show( false );
6582 m_cornerLabelWin
->Show( false );
6584 else if ( m_colLabelHeight
== 0 )
6586 m_colWindow
->Show( true );
6587 if ( m_rowLabelWidth
> 0 )
6588 m_cornerLabelWin
->Show( true );
6591 m_colLabelHeight
= height
;
6593 wxScrolledWindow::Refresh( true );
6597 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6599 if ( m_labelBackgroundColour
!= colour
)
6601 m_labelBackgroundColour
= colour
;
6602 m_rowLabelWin
->SetBackgroundColour( colour
);
6603 m_colWindow
->SetBackgroundColour( colour
);
6604 m_cornerLabelWin
->SetBackgroundColour( colour
);
6606 if ( !GetBatchCount() )
6608 m_rowLabelWin
->Refresh();
6609 m_colWindow
->Refresh();
6610 m_cornerLabelWin
->Refresh();
6615 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6617 if ( m_labelTextColour
!= colour
)
6619 m_labelTextColour
= colour
;
6620 if ( !GetBatchCount() )
6622 m_rowLabelWin
->Refresh();
6623 m_colWindow
->Refresh();
6628 void wxGrid::SetLabelFont( const wxFont
& font
)
6631 if ( !GetBatchCount() )
6633 m_rowLabelWin
->Refresh();
6634 m_colWindow
->Refresh();
6638 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6640 // allow old (incorrect) defs to be used
6643 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6644 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6645 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6650 case wxTOP
: vert
= wxALIGN_TOP
; break;
6651 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6652 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6655 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6657 m_rowLabelHorizAlign
= horiz
;
6660 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6662 m_rowLabelVertAlign
= vert
;
6665 if ( !GetBatchCount() )
6667 m_rowLabelWin
->Refresh();
6671 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6673 // allow old (incorrect) defs to be used
6676 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6677 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6678 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6683 case wxTOP
: vert
= wxALIGN_TOP
; break;
6684 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6685 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6688 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6690 m_colLabelHorizAlign
= horiz
;
6693 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6695 m_colLabelVertAlign
= vert
;
6698 if ( !GetBatchCount() )
6700 m_colWindow
->Refresh();
6704 // Note: under MSW, the default column label font must be changed because it
6705 // does not support vertical printing
6707 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6708 // pGrid->SetLabelFont(font);
6709 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6711 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6713 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6714 m_colLabelTextOrientation
= textOrientation
;
6716 if ( !GetBatchCount() )
6717 m_colWindow
->Refresh();
6720 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6724 m_table
->SetRowLabelValue( row
, s
);
6725 if ( !GetBatchCount() )
6727 wxRect rect
= CellToRect( row
, 0 );
6728 if ( rect
.height
> 0 )
6730 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6732 rect
.width
= m_rowLabelWidth
;
6733 m_rowLabelWin
->Refresh( true, &rect
);
6739 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6743 m_table
->SetColLabelValue( col
, s
);
6744 if ( !GetBatchCount() )
6746 if ( m_useNativeHeader
)
6748 GetGridColHeader()->UpdateColumn(col
);
6752 wxRect rect
= CellToRect( 0, col
);
6753 if ( rect
.width
> 0 )
6755 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6757 rect
.height
= m_colLabelHeight
;
6758 GetColLabelWindow()->Refresh( true, &rect
);
6765 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6767 if ( m_gridLineColour
!= colour
)
6769 m_gridLineColour
= colour
;
6771 if ( GridLinesEnabled() )
6776 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6778 if ( m_cellHighlightColour
!= colour
)
6780 m_cellHighlightColour
= colour
;
6782 wxClientDC
dc( m_gridWin
);
6784 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6785 DrawCellHighlight(dc
, attr
);
6790 void wxGrid::SetCellHighlightPenWidth(int width
)
6792 if (m_cellHighlightPenWidth
!= width
)
6794 m_cellHighlightPenWidth
= width
;
6796 // Just redrawing the cell highlight is not enough since that won't
6797 // make any visible change if the the thickness is getting smaller.
6798 int row
= m_currentCellCoords
.GetRow();
6799 int col
= m_currentCellCoords
.GetCol();
6800 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6803 wxRect rect
= CellToRect(row
, col
);
6804 m_gridWin
->Refresh(true, &rect
);
6808 void wxGrid::SetCellHighlightROPenWidth(int width
)
6810 if (m_cellHighlightROPenWidth
!= width
)
6812 m_cellHighlightROPenWidth
= width
;
6814 // Just redrawing the cell highlight is not enough since that won't
6815 // make any visible change if the the thickness is getting smaller.
6816 int row
= m_currentCellCoords
.GetRow();
6817 int col
= m_currentCellCoords
.GetCol();
6818 if ( row
== -1 || col
== -1 ||
6819 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6822 wxRect rect
= CellToRect(row
, col
);
6823 m_gridWin
->Refresh(true, &rect
);
6827 void wxGrid::RedrawGridLines()
6829 // the lines will be redrawn when the window is thawn
6830 if ( GetBatchCount() )
6833 if ( GridLinesEnabled() )
6835 wxClientDC
dc( m_gridWin
);
6837 DrawAllGridLines( dc
, wxRegion() );
6839 else // remove the grid lines
6841 m_gridWin
->Refresh();
6845 void wxGrid::EnableGridLines( bool enable
)
6847 if ( enable
!= m_gridLinesEnabled
)
6849 m_gridLinesEnabled
= enable
;
6855 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
6861 if ( GridLinesEnabled() )
6866 int wxGrid::GetDefaultRowSize() const
6868 return m_defaultRowHeight
;
6871 int wxGrid::GetRowSize( int row
) const
6873 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6875 return GetRowHeight(row
);
6878 int wxGrid::GetDefaultColSize() const
6880 return m_defaultColWidth
;
6883 int wxGrid::GetColSize( int col
) const
6885 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6887 return GetColWidth(col
);
6890 // ============================================================================
6891 // access to the grid attributes: each of them has a default value in the grid
6892 // itself and may be overidden on a per-cell basis
6893 // ============================================================================
6895 // ----------------------------------------------------------------------------
6896 // setting default attributes
6897 // ----------------------------------------------------------------------------
6899 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6901 m_defaultCellAttr
->SetBackgroundColour(col
);
6903 m_gridWin
->SetBackgroundColour(col
);
6907 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6909 m_defaultCellAttr
->SetTextColour(col
);
6912 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6914 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6917 void wxGrid::SetDefaultCellOverflow( bool allow
)
6919 m_defaultCellAttr
->SetOverflow(allow
);
6922 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6924 m_defaultCellAttr
->SetFont(font
);
6927 // For editors and renderers the type registry takes precedence over the
6928 // default attr, so we need to register the new editor/renderer for the string
6929 // data type in order to make setting a default editor/renderer appear to
6932 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6934 RegisterDataType(wxGRID_VALUE_STRING
,
6936 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
6939 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6941 RegisterDataType(wxGRID_VALUE_STRING
,
6942 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
6946 // ----------------------------------------------------------------------------
6947 // access to the default attributes
6948 // ----------------------------------------------------------------------------
6950 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
6952 return m_defaultCellAttr
->GetBackgroundColour();
6955 wxColour
wxGrid::GetDefaultCellTextColour() const
6957 return m_defaultCellAttr
->GetTextColour();
6960 wxFont
wxGrid::GetDefaultCellFont() const
6962 return m_defaultCellAttr
->GetFont();
6965 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
6967 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6970 bool wxGrid::GetDefaultCellOverflow() const
6972 return m_defaultCellAttr
->GetOverflow();
6975 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6977 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
6980 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6982 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
6985 // ----------------------------------------------------------------------------
6986 // access to cell attributes
6987 // ----------------------------------------------------------------------------
6989 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
6991 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6992 wxColour colour
= attr
->GetBackgroundColour();
6998 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7000 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7001 wxColour colour
= attr
->GetTextColour();
7007 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7009 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7010 wxFont font
= attr
->GetFont();
7016 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7018 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7019 attr
->GetAlignment(horiz
, vert
);
7023 bool wxGrid::GetCellOverflow( int row
, int col
) const
7025 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7026 bool allow
= attr
->GetOverflow();
7032 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7034 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7035 attr
->GetSize( num_rows
, num_cols
);
7039 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7041 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7042 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7048 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7050 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7051 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7057 bool wxGrid::IsReadOnly(int row
, int col
) const
7059 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7060 bool isReadOnly
= attr
->IsReadOnly();
7066 // ----------------------------------------------------------------------------
7067 // attribute support: cache, automatic provider creation, ...
7068 // ----------------------------------------------------------------------------
7070 bool wxGrid::CanHaveAttributes() const
7077 return m_table
->CanHaveAttributes();
7080 void wxGrid::ClearAttrCache()
7082 if ( m_attrCache
.row
!= -1 )
7084 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7085 m_attrCache
.attr
= NULL
;
7086 m_attrCache
.row
= -1;
7087 // wxSafeDecRec(...) might cause event processing that accesses
7088 // the cached attribute, if one exists (e.g. by deleting the
7089 // editor stored within the attribute). Therefore it is important
7090 // to invalidate the cache before calling wxSafeDecRef!
7091 wxSafeDecRef(oldAttr
);
7095 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7099 wxGrid
*self
= (wxGrid
*)this; // const_cast
7101 self
->ClearAttrCache();
7102 self
->m_attrCache
.row
= row
;
7103 self
->m_attrCache
.col
= col
;
7104 self
->m_attrCache
.attr
= attr
;
7109 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7111 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7113 *attr
= m_attrCache
.attr
;
7114 wxSafeIncRef(m_attrCache
.attr
);
7116 #ifdef DEBUG_ATTR_CACHE
7117 gs_nAttrCacheHits
++;
7124 #ifdef DEBUG_ATTR_CACHE
7125 gs_nAttrCacheMisses
++;
7132 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7134 wxGridCellAttr
*attr
= NULL
;
7135 // Additional test to avoid looking at the cache e.g. for
7136 // wxNoCellCoords, as this will confuse memory management.
7139 if ( !LookupAttr(row
, col
, &attr
) )
7141 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7143 CacheAttr(row
, col
, attr
);
7149 attr
->SetDefAttr(m_defaultCellAttr
);
7153 attr
= m_defaultCellAttr
;
7160 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7162 wxGridCellAttr
*attr
= NULL
;
7163 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7165 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
7166 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
7168 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7171 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7173 // artificially inc the ref count to match DecRef() in caller
7175 m_table
->SetAttr(attr
, row
, col
);
7181 // ----------------------------------------------------------------------------
7182 // setting column attributes (wrappers around SetColAttr)
7183 // ----------------------------------------------------------------------------
7185 void wxGrid::SetColFormatBool(int col
)
7187 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7190 void wxGrid::SetColFormatNumber(int col
)
7192 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7195 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7197 wxString typeName
= wxGRID_VALUE_FLOAT
;
7198 if ( (width
!= -1) || (precision
!= -1) )
7200 typeName
<< _T(':') << width
<< _T(',') << precision
;
7203 SetColFormatCustom(col
, typeName
);
7206 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7208 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7210 attr
= new wxGridCellAttr
;
7211 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7212 attr
->SetRenderer(renderer
);
7213 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7214 attr
->SetEditor(editor
);
7216 SetColAttr(col
, attr
);
7220 // ----------------------------------------------------------------------------
7221 // setting cell attributes: this is forwarded to the table
7222 // ----------------------------------------------------------------------------
7224 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7226 if ( CanHaveAttributes() )
7228 m_table
->SetAttr(attr
, row
, col
);
7237 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7239 if ( CanHaveAttributes() )
7241 m_table
->SetRowAttr(attr
, row
);
7250 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7252 if ( CanHaveAttributes() )
7254 m_table
->SetColAttr(attr
, col
);
7263 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7265 if ( CanHaveAttributes() )
7267 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7268 attr
->SetBackgroundColour(colour
);
7273 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7275 if ( CanHaveAttributes() )
7277 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7278 attr
->SetTextColour(colour
);
7283 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7285 if ( CanHaveAttributes() )
7287 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7288 attr
->SetFont(font
);
7293 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7295 if ( CanHaveAttributes() )
7297 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7298 attr
->SetAlignment(horiz
, vert
);
7303 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7305 if ( CanHaveAttributes() )
7307 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7308 attr
->SetOverflow(allow
);
7313 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7315 if ( CanHaveAttributes() )
7317 int cell_rows
, cell_cols
;
7319 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7320 attr
->GetSize(&cell_rows
, &cell_cols
);
7321 attr
->SetSize(num_rows
, num_cols
);
7324 // Cannot set the size of a cell to 0 or negative values
7325 // While it is perfectly legal to do that, this function cannot
7326 // handle all the possibilies, do it by hand by getting the CellAttr.
7327 // You can only set the size of a cell to 1,1 or greater with this fn
7328 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7329 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7330 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7331 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7333 // if this was already a multicell then "turn off" the other cells first
7334 if ((cell_rows
> 1) || (cell_cols
> 1))
7337 for (j
=row
; j
< row
+ cell_rows
; j
++)
7339 for (i
=col
; i
< col
+ cell_cols
; i
++)
7341 if ((i
!= col
) || (j
!= row
))
7343 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7344 attr_stub
->SetSize( 1, 1 );
7345 attr_stub
->DecRef();
7351 // mark the cells that will be covered by this cell to
7352 // negative or zero values to point back at this cell
7353 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7356 for (j
=row
; j
< row
+ num_rows
; j
++)
7358 for (i
=col
; i
< col
+ num_cols
; i
++)
7360 if ((i
!= col
) || (j
!= row
))
7362 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7363 attr_stub
->SetSize( row
- j
, col
- i
);
7364 attr_stub
->DecRef();
7372 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7374 if ( CanHaveAttributes() )
7376 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7377 attr
->SetRenderer(renderer
);
7382 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7384 if ( CanHaveAttributes() )
7386 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7387 attr
->SetEditor(editor
);
7392 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7394 if ( CanHaveAttributes() )
7396 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7397 attr
->SetReadOnly(isReadOnly
);
7402 // ----------------------------------------------------------------------------
7403 // Data type registration
7404 // ----------------------------------------------------------------------------
7406 void wxGrid::RegisterDataType(const wxString
& typeName
,
7407 wxGridCellRenderer
* renderer
,
7408 wxGridCellEditor
* editor
)
7410 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7414 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7416 wxString typeName
= m_table
->GetTypeName(row
, col
);
7417 return GetDefaultEditorForType(typeName
);
7420 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7422 wxString typeName
= m_table
->GetTypeName(row
, col
);
7423 return GetDefaultRendererForType(typeName
);
7426 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7428 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7429 if ( index
== wxNOT_FOUND
)
7431 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7436 return m_typeRegistry
->GetEditor(index
);
7439 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7441 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7442 if ( index
== wxNOT_FOUND
)
7444 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7449 return m_typeRegistry
->GetRenderer(index
);
7452 // ----------------------------------------------------------------------------
7454 // ----------------------------------------------------------------------------
7456 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7460 setFixed
= new wxGridFixedIndicesSet
;
7463 setFixed
->insert(line
);
7467 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7469 return !setFixed
|| !setFixed
->count(line
);
7472 void wxGrid::EnableDragRowSize( bool enable
)
7474 m_canDragRowSize
= enable
;
7477 void wxGrid::EnableDragColSize( bool enable
)
7479 m_canDragColSize
= enable
;
7482 void wxGrid::EnableDragGridSize( bool enable
)
7484 m_canDragGridSize
= enable
;
7487 void wxGrid::EnableDragCell( bool enable
)
7489 m_canDragCell
= enable
;
7492 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7494 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7496 if ( resizeExistingRows
)
7498 // since we are resizing all rows to the default row size,
7499 // we can simply clear the row heights and row bottoms
7500 // arrays (which also allows us to take advantage of
7501 // some speed optimisations)
7502 m_rowHeights
.Empty();
7503 m_rowBottoms
.Empty();
7504 if ( !GetBatchCount() )
7509 void wxGrid::SetRowSize( int row
, int height
)
7511 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7513 // if < 0 then calculate new height from label
7517 wxArrayString lines
;
7518 wxClientDC
dc(m_rowLabelWin
);
7519 dc
.SetFont(GetLabelFont());
7520 StringToLines(GetRowLabelValue( row
), lines
);
7521 GetTextBoxSize( dc
, lines
, &w
, &h
);
7522 //check that it is not less than the minimal height
7523 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7526 // See comment in SetColSize
7527 if ( height
< GetRowMinimalAcceptableHeight())
7530 if ( m_rowHeights
.IsEmpty() )
7532 // need to really create the array
7536 int h
= wxMax( 0, height
);
7537 int diff
= h
- m_rowHeights
[row
];
7539 m_rowHeights
[row
] = h
;
7540 for ( int i
= row
; i
< m_numRows
; i
++ )
7542 m_rowBottoms
[i
] += diff
;
7545 if ( !GetBatchCount() )
7549 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7551 // we dont allow zero default column width
7552 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7554 if ( resizeExistingCols
)
7556 // since we are resizing all columns to the default column size,
7557 // we can simply clear the col widths and col rights
7558 // arrays (which also allows us to take advantage of
7559 // some speed optimisations)
7560 m_colWidths
.Empty();
7561 m_colRights
.Empty();
7562 if ( !GetBatchCount() )
7567 void wxGrid::SetColSize( int col
, int width
)
7569 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7571 // if < 0 then calculate new width from label
7575 wxArrayString lines
;
7576 wxClientDC
dc(m_colWindow
);
7577 dc
.SetFont(GetLabelFont());
7578 StringToLines(GetColLabelValue(col
), lines
);
7579 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7580 GetTextBoxSize( dc
, lines
, &w
, &h
);
7582 GetTextBoxSize( dc
, lines
, &h
, &w
);
7584 //check that it is not less than the minimal width
7585 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7588 // we intentionally don't test whether the width is less than
7589 // GetColMinimalWidth() here but we do compare it with
7590 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7591 // #651) -- and we also always allow the width of 0 as it has the special
7592 // sense of hiding the column
7593 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7596 if ( m_colWidths
.IsEmpty() )
7598 // need to really create the array
7602 const int diff
= width
- m_colWidths
[col
];
7603 m_colWidths
[col
] = width
;
7604 if ( m_useNativeHeader
)
7605 GetGridColHeader()->UpdateColumn(col
);
7606 //else: will be refreshed when the header is redrawn
7608 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7610 m_colRights
[GetColAt(colPos
)] += diff
;
7613 if ( !GetBatchCount() )
7620 void wxGrid::SetColMinimalWidth( int col
, int width
)
7622 if (width
> GetColMinimalAcceptableWidth())
7624 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7625 m_colMinWidths
[key
] = width
;
7629 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7631 if (width
> GetRowMinimalAcceptableHeight())
7633 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7634 m_rowMinHeights
[key
] = width
;
7638 int wxGrid::GetColMinimalWidth(int col
) const
7640 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7641 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7643 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7646 int wxGrid::GetRowMinimalHeight(int row
) const
7648 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7649 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7651 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7654 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7656 // We do allow a width of 0 since this gives us
7657 // an easy way to temporarily hiding columns.
7659 m_minAcceptableColWidth
= width
;
7662 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7664 // We do allow a height of 0 since this gives us
7665 // an easy way to temporarily hiding rows.
7667 m_minAcceptableRowHeight
= height
;
7670 int wxGrid::GetColMinimalAcceptableWidth() const
7672 return m_minAcceptableColWidth
;
7675 int wxGrid::GetRowMinimalAcceptableHeight() const
7677 return m_minAcceptableRowHeight
;
7680 // ----------------------------------------------------------------------------
7682 // ----------------------------------------------------------------------------
7685 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7687 const bool column
= direction
== wxGRID_COLUMN
;
7689 wxClientDC
dc(m_gridWin
);
7691 // cancel editing of cell
7692 HideCellEditControl();
7693 SaveEditControlValue();
7695 // init both of them to avoid compiler warnings, even if we only need one
7703 wxCoord extent
, extentMax
= 0;
7704 int max
= column
? m_numRows
: m_numCols
;
7705 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7712 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7713 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7716 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7717 extent
= column
? size
.x
: size
.y
;
7718 if ( extent
> extentMax
)
7727 // now also compare with the column label extent
7729 dc
.SetFont( GetLabelFont() );
7733 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7734 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7738 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7740 extent
= column
? w
: h
;
7741 if ( extent
> extentMax
)
7746 // empty column - give default extent (notice that if extentMax is less
7747 // than default extent but != 0, it's OK)
7748 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7753 // leave some space around text
7761 // Ensure automatic width is not less than minimal width. See the
7762 // comment in SetColSize() for explanation of why this isn't done
7765 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7767 SetColSize( col
, extentMax
);
7768 if ( !GetBatchCount() )
7770 if ( m_useNativeHeader
)
7772 GetGridColHeader()->UpdateColumn(col
);
7777 m_gridWin
->GetClientSize( &cw
, &ch
);
7778 wxRect
rect ( CellToRect( 0, col
) );
7780 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7781 rect
.width
= cw
- rect
.x
;
7782 rect
.height
= m_colLabelHeight
;
7783 GetColLabelWindow()->Refresh( true, &rect
);
7789 // Ensure automatic width is not less than minimal height. See the
7790 // comment in SetColSize() for explanation of why this isn't done
7793 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7795 SetRowSize(row
, extentMax
);
7796 if ( !GetBatchCount() )
7799 m_gridWin
->GetClientSize( &cw
, &ch
);
7800 wxRect
rect( CellToRect( row
, 0 ) );
7802 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
7803 rect
.width
= m_rowLabelWidth
;
7804 rect
.height
= ch
- rect
.y
;
7805 m_rowLabelWin
->Refresh( true, &rect
);
7812 SetColMinimalWidth(col
, extentMax
);
7814 SetRowMinimalHeight(row
, extentMax
);
7818 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
7820 // calculate size for the rows or columns?
7821 const bool calcRows
= direction
== wxGRID_ROW
;
7823 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
7824 : GetGridColLabelWindow());
7825 dc
.SetFont(GetLabelFont());
7827 // which dimension should we take into account for calculations?
7829 // for columns, the text can be only horizontal so it's easy but for rows
7830 // we also have to take into account the text orientation
7832 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
7834 wxArrayString lines
;
7835 wxCoord extentMax
= 0;
7837 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
7838 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
7842 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
7843 : GetColLabelValue(rowOrCol
);
7844 StringToLines(label
, lines
);
7847 GetTextBoxSize(dc
, lines
, &w
, &h
);
7849 const wxCoord extent
= useWidth
? w
: h
;
7850 if ( extent
> extentMax
)
7856 // empty column - give default extent (notice that if extentMax is less
7857 // than default extent but != 0, it's OK)
7858 extentMax
= calcRows
? GetDefaultRowLabelSize()
7859 : GetDefaultColLabelSize();
7862 // leave some space around text (taken from AutoSizeColOrRow)
7871 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7873 int width
= m_rowLabelWidth
;
7875 wxGridUpdateLocker locker
;
7877 locker
.Create(this);
7879 for ( int col
= 0; col
< m_numCols
; col
++ )
7882 AutoSizeColumn(col
, setAsMin
);
7884 width
+= GetColWidth(col
);
7890 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7892 int height
= m_colLabelHeight
;
7894 wxGridUpdateLocker locker
;
7896 locker
.Create(this);
7898 for ( int row
= 0; row
< m_numRows
; row
++ )
7901 AutoSizeRow(row
, setAsMin
);
7903 height
+= GetRowHeight(row
);
7909 void wxGrid::AutoSize()
7911 wxGridUpdateLocker
locker(this);
7913 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
7914 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
7916 // we know that we're not going to have scrollbars so disable them now to
7917 // avoid trouble in SetClientSize() which can otherwise set the correct
7918 // client size but also leave space for (not needed any more) scrollbars
7919 SetScrollbars(0, 0, 0, 0, 0, 0, true);
7921 // restore the scroll rate parameters overwritten by SetScrollbars()
7922 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
7924 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
7927 void wxGrid::AutoSizeRowLabelSize( int row
)
7929 // Hide the edit control, so it
7930 // won't interfere with drag-shrinking.
7931 if ( IsCellEditControlShown() )
7933 HideCellEditControl();
7934 SaveEditControlValue();
7937 // autosize row height depending on label text
7938 SetRowSize(row
, -1);
7942 void wxGrid::AutoSizeColLabelSize( int col
)
7944 // Hide the edit control, so it
7945 // won't interfere with drag-shrinking.
7946 if ( IsCellEditControlShown() )
7948 HideCellEditControl();
7949 SaveEditControlValue();
7952 // autosize column width depending on label text
7953 SetColSize(col
, -1);
7957 wxSize
wxGrid::DoGetBestSize() const
7959 wxGrid
*self
= (wxGrid
*)this; // const_cast
7961 // we do the same as in AutoSize() here with the exception that we don't
7962 // change the column/row sizes, only calculate them
7963 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
7964 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
7966 // NOTE: This size should be cached, but first we need to add calls to
7967 // InvalidateBestSize everywhere that could change the results of this
7969 // CacheBestSize(size);
7971 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
7972 + GetWindowBorderSize();
7980 wxPen
& wxGrid::GetDividerPen() const
7985 // ----------------------------------------------------------------------------
7986 // cell value accessor functions
7987 // ----------------------------------------------------------------------------
7989 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7993 m_table
->SetValue( row
, col
, s
);
7994 if ( !GetBatchCount() )
7997 wxRect
rect( CellToRect( row
, col
) );
7999 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8000 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8001 m_gridWin
->Refresh( false, &rect
);
8004 if ( m_currentCellCoords
.GetRow() == row
&&
8005 m_currentCellCoords
.GetCol() == col
&&
8006 IsCellEditControlShown())
8007 // Note: If we are using IsCellEditControlEnabled,
8008 // this interacts badly with calling SetCellValue from
8009 // an EVT_GRID_CELL_CHANGE handler.
8011 HideCellEditControl();
8012 ShowCellEditControl(); // will reread data from table
8017 // ----------------------------------------------------------------------------
8018 // block, row and column selection
8019 // ----------------------------------------------------------------------------
8021 void wxGrid::SelectRow( int row
, bool addToSelected
)
8026 if ( !addToSelected
)
8029 m_selection
->SelectRow(row
);
8032 void wxGrid::SelectCol( int col
, bool addToSelected
)
8037 if ( !addToSelected
)
8040 m_selection
->SelectCol(col
);
8043 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8049 if ( !addToSelected
)
8052 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8055 void wxGrid::SelectAll()
8057 if ( m_numRows
> 0 && m_numCols
> 0 )
8060 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8064 // ----------------------------------------------------------------------------
8065 // cell, row and col deselection
8066 // ----------------------------------------------------------------------------
8068 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8073 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8074 if ( mode
== oper
.GetSelectionMode() ||
8075 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8077 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8078 if ( m_selection
->IsInSelection(c
) )
8079 m_selection
->ToggleCellSelection(c
);
8081 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8083 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8084 for ( int i
= 0; i
< nOther
; i
++ )
8086 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8087 if ( m_selection
->IsInSelection(c
) )
8088 m_selection
->ToggleCellSelection(c
);
8091 //else: can only select orthogonal lines so no lines in this direction
8092 // could have been selected anyhow
8095 void wxGrid::DeselectRow(int row
)
8097 DeselectLine(row
, wxGridRowOperations());
8100 void wxGrid::DeselectCol(int col
)
8102 DeselectLine(col
, wxGridColumnOperations());
8105 void wxGrid::DeselectCell( int row
, int col
)
8107 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8108 m_selection
->ToggleCellSelection(row
, col
);
8111 bool wxGrid::IsSelection() const
8113 return ( m_selection
&& (m_selection
->IsSelection() ||
8114 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8115 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8118 bool wxGrid::IsInSelection( int row
, int col
) const
8120 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8121 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8122 col
>= m_selectedBlockTopLeft
.GetCol() &&
8123 row
<= m_selectedBlockBottomRight
.GetRow() &&
8124 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8127 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8131 wxGridCellCoordsArray a
;
8135 return m_selection
->m_cellSelection
;
8138 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8142 wxGridCellCoordsArray a
;
8146 return m_selection
->m_blockSelectionTopLeft
;
8149 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8153 wxGridCellCoordsArray a
;
8157 return m_selection
->m_blockSelectionBottomRight
;
8160 wxArrayInt
wxGrid::GetSelectedRows() const
8168 return m_selection
->m_rowSelection
;
8171 wxArrayInt
wxGrid::GetSelectedCols() const
8179 return m_selection
->m_colSelection
;
8182 void wxGrid::ClearSelection()
8184 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8185 m_selectedBlockBottomRight
);
8186 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8187 m_selectedBlockCorner
);
8189 m_selectedBlockTopLeft
=
8190 m_selectedBlockBottomRight
=
8191 m_selectedBlockCorner
= wxGridNoCellCoords
;
8193 if ( !r1
.IsEmpty() )
8194 RefreshRect(r1
, false);
8195 if ( !r2
.IsEmpty() )
8196 RefreshRect(r2
, false);
8199 m_selection
->ClearSelection();
8202 // This function returns the rectangle that encloses the given block
8203 // in device coords clipped to the client size of the grid window.
8205 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8206 const wxGridCellCoords
& bottomRight
) const
8209 wxRect tempCellRect
= CellToRect(topLeft
);
8210 if ( tempCellRect
!= wxGridNoCellRect
)
8212 resultRect
= tempCellRect
;
8216 resultRect
= wxRect(0, 0, 0, 0);
8219 tempCellRect
= CellToRect(bottomRight
);
8220 if ( tempCellRect
!= wxGridNoCellRect
)
8222 resultRect
+= tempCellRect
;
8226 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8227 return wxGridNoCellRect
;
8230 // Ensure that left/right and top/bottom pairs are in order.
8231 int left
= resultRect
.GetLeft();
8232 int top
= resultRect
.GetTop();
8233 int right
= resultRect
.GetRight();
8234 int bottom
= resultRect
.GetBottom();
8236 int leftCol
= topLeft
.GetCol();
8237 int topRow
= topLeft
.GetRow();
8238 int rightCol
= bottomRight
.GetCol();
8239 int bottomRow
= bottomRight
.GetRow();
8263 // The following loop is ONLY necessary to detect and handle merged cells.
8265 m_gridWin
->GetClientSize( &cw
, &ch
);
8267 // Get the origin coordinates: notice that they will be negative if the
8268 // grid is scrolled downwards/to the right.
8269 int gridOriginX
= 0;
8270 int gridOriginY
= 0;
8271 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8273 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8274 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8276 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8277 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8279 // Bound our loop so that we only examine the portion of the selected block
8280 // that is shown on screen. Therefore, we compare the Top-Left block values
8281 // to the Top-Left screen values, and the Bottom-Right block values to the
8282 // Bottom-Right screen values, choosing appropriately.
8283 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8284 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8285 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8286 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8288 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8290 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8292 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8293 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8295 tempCellRect
= CellToRect( j
, i
);
8297 if (tempCellRect
.x
< left
)
8298 left
= tempCellRect
.x
;
8299 if (tempCellRect
.y
< top
)
8300 top
= tempCellRect
.y
;
8301 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8302 right
= tempCellRect
.x
+ tempCellRect
.width
;
8303 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8304 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8308 i
= visibleRightCol
; // jump over inner cells.
8313 // Convert to scrolled coords
8314 CalcScrolledPosition( left
, top
, &left
, &top
);
8315 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8317 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8318 return wxRect(0,0,0,0);
8320 resultRect
.SetLeft( wxMax(0, left
) );
8321 resultRect
.SetTop( wxMax(0, top
) );
8322 resultRect
.SetRight( wxMin(cw
, right
) );
8323 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8328 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8329 const wxGridOperations
& oper
)
8332 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8333 const int numLines
= oper
.GetNumberOfLines(this);
8334 for ( int i
= 0; i
< numLines
; i
++ )
8336 int size
= sizeInfo
.GetSize(i
);
8337 if ( size
!= sizeInfo
.m_sizeDefault
)
8338 oper
.SetLineSize(this, i
, size
);
8343 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8345 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8348 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8350 DoSetSizes(sizeInfo
, wxGridRowOperations());
8353 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8355 m_sizeDefault
= defSize
;
8356 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8358 if ( allSizes
[i
] != defSize
)
8359 m_customSizes
[i
] = allSizes
[i
];
8363 int wxGridSizesInfo::GetSize(unsigned pos
) const
8365 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8367 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8370 // ----------------------------------------------------------------------------
8372 // ----------------------------------------------------------------------------
8374 #if wxUSE_DRAG_AND_DROP
8376 // this allow setting drop target directly on wxGrid
8377 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8379 GetGridWindow()->SetDropTarget(dropTarget
);
8382 #endif // wxUSE_DRAG_AND_DROP
8384 // ----------------------------------------------------------------------------
8385 // grid event classes
8386 // ----------------------------------------------------------------------------
8388 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8390 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8391 int row
, int col
, int x
, int y
, bool sel
,
8392 bool control
, bool shift
, bool alt
, bool meta
)
8393 : wxNotifyEvent( type
, id
),
8394 wxKeyboardState(control
, shift
, alt
, meta
)
8396 Init(row
, col
, x
, y
, sel
);
8398 SetEventObject(obj
);
8401 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8403 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8404 int rowOrCol
, int x
, int y
,
8405 bool control
, bool shift
, bool alt
, bool meta
)
8406 : wxNotifyEvent( type
, id
),
8407 wxKeyboardState(control
, shift
, alt
, meta
)
8409 Init(rowOrCol
, x
, y
);
8411 SetEventObject(obj
);
8415 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8417 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8418 const wxGridCellCoords
& topLeft
,
8419 const wxGridCellCoords
& bottomRight
,
8420 bool sel
, bool control
,
8421 bool shift
, bool alt
, bool meta
)
8422 : wxNotifyEvent( type
, id
),
8423 wxKeyboardState(control
, shift
, alt
, meta
)
8425 Init(topLeft
, bottomRight
, sel
);
8427 SetEventObject(obj
);
8431 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8433 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8434 wxObject
* obj
, int row
,
8435 int col
, wxControl
* ctrl
)
8436 : wxCommandEvent(type
, id
)
8438 SetEventObject(obj
);
8445 // ----------------------------------------------------------------------------
8446 // wxGridTypeRegistry
8447 // ----------------------------------------------------------------------------
8449 wxGridTypeRegistry::~wxGridTypeRegistry()
8451 size_t count
= m_typeinfo
.GetCount();
8452 for ( size_t i
= 0; i
< count
; i
++ )
8453 delete m_typeinfo
[i
];
8456 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8457 wxGridCellRenderer
* renderer
,
8458 wxGridCellEditor
* editor
)
8460 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8462 // is it already registered?
8463 int loc
= FindRegisteredDataType(typeName
);
8464 if ( loc
!= wxNOT_FOUND
)
8466 delete m_typeinfo
[loc
];
8467 m_typeinfo
[loc
] = info
;
8471 m_typeinfo
.Add(info
);
8475 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8477 size_t count
= m_typeinfo
.GetCount();
8478 for ( size_t i
= 0; i
< count
; i
++ )
8480 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8489 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8491 int index
= FindRegisteredDataType(typeName
);
8492 if ( index
== wxNOT_FOUND
)
8494 // check whether this is one of the standard ones, in which case
8495 // register it "on the fly"
8497 if ( typeName
== wxGRID_VALUE_STRING
)
8499 RegisterDataType(wxGRID_VALUE_STRING
,
8500 new wxGridCellStringRenderer
,
8501 new wxGridCellTextEditor
);
8504 #endif // wxUSE_TEXTCTRL
8506 if ( typeName
== wxGRID_VALUE_BOOL
)
8508 RegisterDataType(wxGRID_VALUE_BOOL
,
8509 new wxGridCellBoolRenderer
,
8510 new wxGridCellBoolEditor
);
8513 #endif // wxUSE_CHECKBOX
8515 if ( typeName
== wxGRID_VALUE_NUMBER
)
8517 RegisterDataType(wxGRID_VALUE_NUMBER
,
8518 new wxGridCellNumberRenderer
,
8519 new wxGridCellNumberEditor
);
8521 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8523 RegisterDataType(wxGRID_VALUE_FLOAT
,
8524 new wxGridCellFloatRenderer
,
8525 new wxGridCellFloatEditor
);
8528 #endif // wxUSE_TEXTCTRL
8530 if ( typeName
== wxGRID_VALUE_CHOICE
)
8532 RegisterDataType(wxGRID_VALUE_CHOICE
,
8533 new wxGridCellStringRenderer
,
8534 new wxGridCellChoiceEditor
);
8537 #endif // wxUSE_COMBOBOX
8542 // we get here only if just added the entry for this type, so return
8544 index
= m_typeinfo
.GetCount() - 1;
8550 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8552 int index
= FindDataType(typeName
);
8553 if ( index
== wxNOT_FOUND
)
8555 // the first part of the typename is the "real" type, anything after ':'
8556 // are the parameters for the renderer
8557 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
8558 if ( index
== wxNOT_FOUND
)
8563 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8564 wxGridCellRenderer
*rendererOld
= renderer
;
8565 renderer
= renderer
->Clone();
8566 rendererOld
->DecRef();
8568 wxGridCellEditor
*editor
= GetEditor(index
);
8569 wxGridCellEditor
*editorOld
= editor
;
8570 editor
= editor
->Clone();
8571 editorOld
->DecRef();
8573 // do it even if there are no parameters to reset them to defaults
8574 wxString params
= typeName
.AfterFirst(_T(':'));
8575 renderer
->SetParameters(params
);
8576 editor
->SetParameters(params
);
8578 // register the new typename
8579 RegisterDataType(typeName
, renderer
, editor
);
8581 // we just registered it, it's the last one
8582 index
= m_typeinfo
.GetCount() - 1;
8588 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8590 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8597 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8599 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8606 #endif // wxUSE_GRID