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()
1132 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1135 m_data
.Alloc( numRows
);
1138 sa
.Alloc( numCols
);
1139 sa
.Add( wxEmptyString
, numCols
);
1141 m_data
.Add( sa
, numRows
);
1144 wxGridStringTable::~wxGridStringTable()
1148 int wxGridStringTable::GetNumberRows()
1150 return m_data
.GetCount();
1153 int wxGridStringTable::GetNumberCols()
1155 if ( m_data
.GetCount() > 0 )
1156 return m_data
[0].GetCount();
1161 wxString
wxGridStringTable::GetValue( int row
, int col
)
1163 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
1165 _T("invalid row or column index in wxGridStringTable") );
1167 return m_data
[row
][col
];
1170 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1172 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
1173 _T("invalid row or column index in wxGridStringTable") );
1175 m_data
[row
][col
] = value
;
1178 void wxGridStringTable::Clear()
1181 int numRows
, numCols
;
1183 numRows
= m_data
.GetCount();
1186 numCols
= m_data
[0].GetCount();
1188 for ( row
= 0; row
< numRows
; row
++ )
1190 for ( col
= 0; col
< numCols
; col
++ )
1192 m_data
[row
][col
] = wxEmptyString
;
1198 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1200 size_t curNumRows
= m_data
.GetCount();
1201 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1202 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1204 if ( pos
>= curNumRows
)
1206 return AppendRows( numRows
);
1210 sa
.Alloc( curNumCols
);
1211 sa
.Add( wxEmptyString
, curNumCols
);
1212 m_data
.Insert( sa
, pos
, numRows
);
1216 wxGridTableMessage
msg( this,
1217 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1221 GetView()->ProcessTableMessage( msg
);
1227 bool wxGridStringTable::AppendRows( size_t numRows
)
1229 size_t curNumRows
= m_data
.GetCount();
1230 size_t curNumCols
= ( curNumRows
> 0
1231 ? m_data
[0].GetCount()
1232 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1235 if ( curNumCols
> 0 )
1237 sa
.Alloc( curNumCols
);
1238 sa
.Add( wxEmptyString
, curNumCols
);
1241 m_data
.Add( sa
, numRows
);
1245 wxGridTableMessage
msg( this,
1246 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1249 GetView()->ProcessTableMessage( msg
);
1255 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1257 size_t curNumRows
= m_data
.GetCount();
1259 if ( pos
>= curNumRows
)
1261 wxFAIL_MSG( wxString::Format
1263 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1265 (unsigned long)numRows
,
1266 (unsigned long)curNumRows
1272 if ( numRows
> curNumRows
- pos
)
1274 numRows
= curNumRows
- pos
;
1277 if ( numRows
>= curNumRows
)
1283 m_data
.RemoveAt( pos
, numRows
);
1288 wxGridTableMessage
msg( this,
1289 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1293 GetView()->ProcessTableMessage( msg
);
1299 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1303 size_t curNumRows
= m_data
.GetCount();
1304 size_t curNumCols
= ( curNumRows
> 0
1305 ? m_data
[0].GetCount()
1306 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1308 if ( pos
>= curNumCols
)
1310 return AppendCols( numCols
);
1313 if ( !m_colLabels
.IsEmpty() )
1315 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1318 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1319 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1322 for ( row
= 0; row
< curNumRows
; row
++ )
1324 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1326 m_data
[row
].Insert( wxEmptyString
, col
);
1332 wxGridTableMessage
msg( this,
1333 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1337 GetView()->ProcessTableMessage( msg
);
1343 bool wxGridStringTable::AppendCols( size_t numCols
)
1347 size_t curNumRows
= m_data
.GetCount();
1349 for ( row
= 0; row
< curNumRows
; row
++ )
1351 m_data
[row
].Add( wxEmptyString
, numCols
);
1356 wxGridTableMessage
msg( this,
1357 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1360 GetView()->ProcessTableMessage( msg
);
1366 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1370 size_t curNumRows
= m_data
.GetCount();
1371 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1372 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1374 if ( pos
>= curNumCols
)
1376 wxFAIL_MSG( wxString::Format
1378 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1380 (unsigned long)numCols
,
1381 (unsigned long)curNumCols
1388 colID
= GetView()->GetColAt( pos
);
1392 if ( numCols
> curNumCols
- colID
)
1394 numCols
= curNumCols
- colID
;
1397 if ( !m_colLabels
.IsEmpty() )
1399 // m_colLabels stores just as many elements as it needs, e.g. if only
1400 // the label of the first column had been set it would have only one
1401 // element and not numCols, so account for it
1402 int nToRm
= m_colLabels
.size() - colID
;
1404 m_colLabels
.RemoveAt( colID
, nToRm
);
1407 for ( row
= 0; row
< curNumRows
; row
++ )
1409 if ( numCols
>= curNumCols
)
1411 m_data
[row
].Clear();
1415 m_data
[row
].RemoveAt( colID
, numCols
);
1421 wxGridTableMessage
msg( this,
1422 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1426 GetView()->ProcessTableMessage( msg
);
1432 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1434 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1436 // using default label
1438 return wxGridTableBase::GetRowLabelValue( row
);
1442 return m_rowLabels
[row
];
1446 wxString
wxGridStringTable::GetColLabelValue( int col
)
1448 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1450 // using default label
1452 return wxGridTableBase::GetColLabelValue( col
);
1456 return m_colLabels
[col
];
1460 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1462 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1464 int n
= m_rowLabels
.GetCount();
1467 for ( i
= n
; i
<= row
; i
++ )
1469 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1473 m_rowLabels
[row
] = value
;
1476 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1478 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1480 int n
= m_colLabels
.GetCount();
1483 for ( i
= n
; i
<= col
; i
++ )
1485 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1489 m_colLabels
[col
] = value
;
1493 //////////////////////////////////////////////////////////////////////
1494 //////////////////////////////////////////////////////////////////////
1496 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1497 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1500 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1502 m_owner
->CancelMouseCapture();
1505 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1506 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1507 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1508 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1511 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1515 // NO - don't do this because it will set both the x and y origin
1516 // coords to match the parent scrolled window and we just want to
1517 // set the y coord - MB
1519 // m_owner->PrepareDC( dc );
1522 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1523 wxPoint pt
= dc
.GetDeviceOrigin();
1524 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1526 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1527 m_owner
->DrawRowLabels( dc
, rows
);
1530 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1532 m_owner
->ProcessRowLabelMouseEvent( event
);
1535 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1537 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1541 //////////////////////////////////////////////////////////////////////
1543 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1544 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1545 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1546 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1549 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1553 // NO - don't do this because it will set both the x and y origin
1554 // coords to match the parent scrolled window and we just want to
1555 // set the x coord - MB
1557 // m_owner->PrepareDC( dc );
1560 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1561 wxPoint pt
= dc
.GetDeviceOrigin();
1562 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1563 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1565 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1567 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1568 m_owner
->DrawColLabels( dc
, cols
);
1571 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1573 m_owner
->ProcessColLabelMouseEvent( event
);
1576 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1578 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1582 //////////////////////////////////////////////////////////////////////
1584 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1585 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1586 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1587 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1590 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1594 m_owner
->DrawCornerLabel(dc
);
1597 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1599 m_owner
->ProcessCornerLabelMouseEvent( event
);
1602 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1604 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1608 //////////////////////////////////////////////////////////////////////
1610 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1611 EVT_PAINT( wxGridWindow::OnPaint
)
1612 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1613 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1614 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1615 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1616 EVT_CHAR( wxGridWindow::OnChar
)
1617 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1618 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1619 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1622 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1624 wxPaintDC
dc( this );
1625 m_owner
->PrepareDC( dc
);
1626 wxRegion reg
= GetUpdateRegion();
1627 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1628 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1630 m_owner
->DrawGridSpace( dc
);
1632 m_owner
->DrawAllGridLines( dc
, reg
);
1634 m_owner
->DrawHighlight( dc
, dirtyCells
);
1637 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1639 wxWindow::ScrollWindow( dx
, dy
, rect
);
1640 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1641 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1644 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1646 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1649 m_owner
->ProcessGridCellMouseEvent( event
);
1652 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1654 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1658 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1659 // cursor must be in the cell edit control to get key events
1661 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1663 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1667 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1669 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1673 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1675 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1679 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1683 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1685 // and if we have any selection, it has to be repainted, because it
1686 // uses different colour when the grid is not focused:
1687 if ( m_owner
->IsSelection() )
1693 // NB: Note that this code is in "else" branch only because the other
1694 // branch refreshes everything and so there's no point in calling
1695 // Refresh() again, *not* because it should only be done if
1696 // !IsSelection(). If the above code is ever optimized to refresh
1697 // only selected area, this needs to be moved out of the "else"
1698 // branch so that it's always executed.
1700 // current cell cursor {dis,re}appears on focus change:
1701 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1702 m_owner
->GetGridCursorCol());
1703 const wxRect cursor
=
1704 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1705 Refresh(true, &cursor
);
1708 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1712 #define internalXToCol(x) XToCol(x, true)
1713 #define internalYToRow(y) YToRow(y, true)
1715 /////////////////////////////////////////////////////////////////////
1717 #if wxUSE_EXTENDED_RTTI
1718 WX_DEFINE_FLAGS( wxGridStyle
)
1720 wxBEGIN_FLAGS( wxGridStyle
)
1721 // new style border flags, we put them first to
1722 // use them for streaming out
1723 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
1724 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
1725 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
1726 wxFLAGS_MEMBER(wxBORDER_RAISED
)
1727 wxFLAGS_MEMBER(wxBORDER_STATIC
)
1728 wxFLAGS_MEMBER(wxBORDER_NONE
)
1730 // old style border flags
1731 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
1732 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
1733 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
1734 wxFLAGS_MEMBER(wxRAISED_BORDER
)
1735 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
1736 wxFLAGS_MEMBER(wxBORDER
)
1738 // standard window styles
1739 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
1740 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
1741 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
1742 wxFLAGS_MEMBER(wxWANTS_CHARS
)
1743 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
1744 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
1745 wxFLAGS_MEMBER(wxVSCROLL
)
1746 wxFLAGS_MEMBER(wxHSCROLL
)
1748 wxEND_FLAGS( wxGridStyle
)
1750 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
1752 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
1753 wxHIDE_PROPERTY( Children
)
1754 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1755 wxEND_PROPERTIES_TABLE()
1757 wxBEGIN_HANDLERS_TABLE(wxGrid
)
1758 wxEND_HANDLERS_TABLE()
1760 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1763 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1766 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1769 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1770 EVT_PAINT( wxGrid::OnPaint
)
1771 EVT_SIZE( wxGrid::OnSize
)
1772 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1773 EVT_KEY_UP( wxGrid::OnKeyUp
)
1774 EVT_CHAR ( wxGrid::OnChar
)
1775 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1778 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1779 const wxPoint
& pos
, const wxSize
& size
,
1780 long style
, const wxString
& name
)
1782 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1783 style
| wxWANTS_CHARS
, name
))
1786 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1787 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1790 SetInitialSize(size
);
1791 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
1800 m_winCapture
->ReleaseMouse();
1802 // Ensure that the editor control is destroyed before the grid is,
1803 // otherwise we crash later when the editor tries to do something with the
1804 // half destroyed grid
1805 HideCellEditControl();
1807 // Must do this or ~wxScrollHelper will pop the wrong event handler
1808 SetTargetWindow(this);
1810 wxSafeDecRef(m_defaultCellAttr
);
1812 #ifdef DEBUG_ATTR_CACHE
1813 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1814 wxPrintf(_T("wxGrid attribute cache statistics: "
1815 "total: %u, hits: %u (%u%%)\n"),
1816 total
, gs_nAttrCacheHits
,
1817 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1820 // if we own the table, just delete it, otherwise at least don't leave it
1821 // with dangling view pointer
1824 else if ( m_table
&& m_table
->GetView() == this )
1825 m_table
->SetView(NULL
);
1827 delete m_typeRegistry
;
1830 delete m_setFixedRows
;
1831 delete m_setFixedCols
;
1835 // ----- internal init and update functions
1838 // NOTE: If using the default visual attributes works everywhere then this can
1839 // be removed as well as the #else cases below.
1840 #define _USE_VISATTR 0
1842 void wxGrid::Create()
1844 // create the type registry
1845 m_typeRegistry
= new wxGridTypeRegistry
;
1847 m_cellEditCtrlEnabled
= false;
1849 m_defaultCellAttr
= new wxGridCellAttr();
1851 // Set default cell attributes
1852 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1853 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1854 m_defaultCellAttr
->SetFont(GetFont());
1855 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1856 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1857 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1860 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1861 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1863 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1864 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1867 m_defaultCellAttr
->SetTextColour(
1868 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1869 m_defaultCellAttr
->SetBackgroundColour(
1870 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1875 m_currentCellCoords
= wxGridNoCellCoords
;
1877 // subwindow components that make up the wxGrid
1878 m_rowLabelWin
= new wxGridRowLabelWindow(this);
1879 CreateColumnWindow();
1880 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
1881 m_gridWin
= new wxGridWindow( this );
1883 SetTargetWindow( m_gridWin
);
1886 wxColour gfg
= gva
.colFg
;
1887 wxColour gbg
= gva
.colBg
;
1888 wxColour lfg
= lva
.colFg
;
1889 wxColour lbg
= lva
.colBg
;
1891 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1892 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
1893 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
1894 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1897 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
1898 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
1899 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
1900 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
1901 m_colWindow
->SetOwnForegroundColour(lfg
);
1902 m_colWindow
->SetOwnBackgroundColour(lbg
);
1904 m_gridWin
->SetOwnForegroundColour(gfg
);
1905 m_gridWin
->SetOwnBackgroundColour(gbg
);
1907 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
1908 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
1910 // now that we have the grid window, use its font to compute the default
1912 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
1913 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
1914 m_defaultRowHeight
+= 8;
1916 m_defaultRowHeight
+= 4;
1921 void wxGrid::CreateColumnWindow()
1923 if ( m_useNativeHeader
)
1925 m_colWindow
= new wxGridHeaderCtrl(this);
1926 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
1928 else // draw labels ourselves
1930 m_colWindow
= new wxGridColLabelWindow(this);
1931 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
1935 bool wxGrid::CreateGrid( int numRows
, int numCols
,
1936 wxGridSelectionModes selmode
)
1938 wxCHECK_MSG( !m_created
,
1940 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
1942 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
1945 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
1947 wxCHECK_RET( m_created
,
1948 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
1950 m_selection
->SetSelectionMode( selmode
);
1953 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
1955 wxCHECK_MSG( m_created
, wxGridSelectCells
,
1956 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
1958 return m_selection
->GetSelectionMode();
1962 wxGrid::SetTable(wxGridTableBase
*table
,
1964 wxGrid::wxGridSelectionModes selmode
)
1966 bool checkSelection
= false;
1969 // stop all processing
1974 m_table
->SetView(0);
1986 checkSelection
= true;
1988 // kill row and column size arrays
1989 m_colWidths
.Empty();
1990 m_colRights
.Empty();
1991 m_rowHeights
.Empty();
1992 m_rowBottoms
.Empty();
1997 m_numRows
= table
->GetNumberRows();
1998 m_numCols
= table
->GetNumberCols();
2000 if ( m_useNativeHeader
)
2001 GetGridColHeader()->SetColumnCount(m_numCols
);
2004 m_table
->SetView( this );
2005 m_ownTable
= takeOwnership
;
2006 m_selection
= new wxGridSelection( this, selmode
);
2009 // If the newly set table is smaller than the
2010 // original one current cell and selection regions
2011 // might be invalid,
2012 m_selectedBlockCorner
= wxGridNoCellCoords
;
2013 m_currentCellCoords
=
2014 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2015 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2016 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2017 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2019 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2020 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2023 m_selectedBlockBottomRight
=
2024 wxGridCellCoords(wxMin(m_numRows
,
2025 m_selectedBlockBottomRight
.GetRow()),
2027 m_selectedBlockBottomRight
.GetCol()));
2041 m_cornerLabelWin
= NULL
;
2042 m_rowLabelWin
= NULL
;
2050 m_defaultCellAttr
= NULL
;
2051 m_typeRegistry
= NULL
;
2052 m_winCapture
= NULL
;
2054 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2055 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2058 m_setFixedCols
= NULL
;
2061 m_attrCache
.row
= -1;
2062 m_attrCache
.col
= -1;
2063 m_attrCache
.attr
= NULL
;
2065 m_labelFont
= GetFont();
2066 m_labelFont
.SetWeight( wxBOLD
);
2068 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2069 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2071 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2072 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2073 m_colLabelTextOrientation
= wxHORIZONTAL
;
2075 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2076 m_defaultRowHeight
= 0; // this will be initialized after creation
2078 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2079 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2081 m_gridLineColour
= wxColour( 192,192,192 );
2082 m_gridLinesEnabled
= true;
2083 m_gridLinesClipHorz
=
2084 m_gridLinesClipVert
= true;
2085 m_cellHighlightColour
= *wxBLACK
;
2086 m_cellHighlightPenWidth
= 2;
2087 m_cellHighlightROPenWidth
= 1;
2089 m_canDragColMove
= false;
2091 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2092 m_winCapture
= NULL
;
2093 m_canDragRowSize
= true;
2094 m_canDragColSize
= true;
2095 m_canDragGridSize
= true;
2096 m_canDragCell
= false;
2098 m_dragRowOrCol
= -1;
2099 m_isDragging
= false;
2100 m_startDragPos
= wxDefaultPosition
;
2102 m_sortCol
= wxNOT_FOUND
;
2103 m_sortIsAscending
= true;
2106 m_nativeColumnLabels
= false;
2108 m_waitForSlowClick
= false;
2110 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2111 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2113 m_currentCellCoords
= wxGridNoCellCoords
;
2115 m_selectedBlockTopLeft
=
2116 m_selectedBlockBottomRight
=
2117 m_selectedBlockCorner
= wxGridNoCellCoords
;
2119 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2120 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2122 m_editable
= true; // default for whole grid
2124 m_inOnKeyDown
= false;
2130 m_scrollLineX
= GRID_SCROLL_LINE_X
;
2131 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
2134 // ----------------------------------------------------------------------------
2135 // the idea is to call these functions only when necessary because they create
2136 // quite big arrays which eat memory mostly unnecessary - in particular, if
2137 // default widths/heights are used for all rows/columns, we may not use these
2140 // with some extra code, it should be possible to only store the widths/heights
2141 // different from default ones (resulting in space savings for huge grids) but
2142 // this is not done currently
2143 // ----------------------------------------------------------------------------
2145 void wxGrid::InitRowHeights()
2147 m_rowHeights
.Empty();
2148 m_rowBottoms
.Empty();
2150 m_rowHeights
.Alloc( m_numRows
);
2151 m_rowBottoms
.Alloc( m_numRows
);
2153 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2156 for ( int i
= 0; i
< m_numRows
; i
++ )
2158 rowBottom
+= m_defaultRowHeight
;
2159 m_rowBottoms
.Add( rowBottom
);
2163 void wxGrid::InitColWidths()
2165 m_colWidths
.Empty();
2166 m_colRights
.Empty();
2168 m_colWidths
.Alloc( m_numCols
);
2169 m_colRights
.Alloc( m_numCols
);
2171 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2173 for ( int i
= 0; i
< m_numCols
; i
++ )
2175 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2176 m_colRights
.Add( colRight
);
2180 int wxGrid::GetColWidth(int col
) const
2182 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2185 int wxGrid::GetColLeft(int col
) const
2187 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2188 : m_colRights
[col
] - m_colWidths
[col
];
2191 int wxGrid::GetColRight(int col
) const
2193 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2197 int wxGrid::GetRowHeight(int row
) const
2199 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2202 int wxGrid::GetRowTop(int row
) const
2204 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2205 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2208 int wxGrid::GetRowBottom(int row
) const
2210 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2211 : m_rowBottoms
[row
];
2214 void wxGrid::CalcDimensions()
2216 // compute the size of the scrollable area
2217 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2218 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2223 // take into account editor if shown
2224 if ( IsCellEditControlShown() )
2227 int r
= m_currentCellCoords
.GetRow();
2228 int c
= m_currentCellCoords
.GetCol();
2229 int x
= GetColLeft(c
);
2230 int y
= GetRowTop(r
);
2232 // how big is the editor
2233 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2234 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2235 editor
->GetControl()->GetSize(&w2
, &h2
);
2246 // preserve (more or less) the previous position
2248 GetViewStart( &x
, &y
);
2250 // ensure the position is valid for the new scroll ranges
2252 x
= wxMax( w
- 1, 0 );
2254 y
= wxMax( h
- 1, 0 );
2256 // update the virtual size and refresh the scrollbars to reflect it
2257 m_gridWin
->SetVirtualSize(w
, h
);
2261 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2262 // still must reposition the children
2266 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2268 wxSize
sizeGridWin(size
);
2269 sizeGridWin
.x
-= m_rowLabelWidth
;
2270 sizeGridWin
.y
-= m_colLabelHeight
;
2275 void wxGrid::CalcWindowSizes()
2277 // escape if the window is has not been fully created yet
2279 if ( m_cornerLabelWin
== NULL
)
2283 GetClientSize( &cw
, &ch
);
2285 // the grid may be too small to have enough space for the labels yet, don't
2286 // size the windows to negative sizes in this case
2287 int gw
= cw
- m_rowLabelWidth
;
2288 int gh
= ch
- m_colLabelHeight
;
2294 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2295 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2297 if ( m_colWindow
&& m_colWindow
->IsShown() )
2298 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2300 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2301 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2303 if ( m_gridWin
&& m_gridWin
->IsShown() )
2304 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2307 // this is called when the grid table sends a message
2308 // to indicate that it has been redimensioned
2310 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2313 bool result
= false;
2315 // Clear the attribute cache as the attribute might refer to a different
2316 // cell than stored in the cache after adding/removing rows/columns.
2319 // By the same reasoning, the editor should be dismissed if columns are
2320 // added or removed. And for consistency, it should IMHO always be
2321 // removed, not only if the cell "underneath" it actually changes.
2322 // For now, I intentionally do not save the editor's content as the
2323 // cell it might want to save that stuff to might no longer exist.
2324 HideCellEditControl();
2326 switch ( msg
.GetId() )
2328 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2330 size_t pos
= msg
.GetCommandInt();
2331 int numRows
= msg
.GetCommandInt2();
2333 m_numRows
+= numRows
;
2335 if ( !m_rowHeights
.IsEmpty() )
2337 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2338 m_rowBottoms
.Insert( 0, pos
, numRows
);
2342 bottom
= m_rowBottoms
[pos
- 1];
2344 for ( i
= pos
; i
< m_numRows
; i
++ )
2346 bottom
+= m_rowHeights
[i
];
2347 m_rowBottoms
[i
] = bottom
;
2351 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2353 // if we have just inserted cols into an empty grid the current
2354 // cell will be undefined...
2356 SetCurrentCell( 0, 0 );
2360 m_selection
->UpdateRows( pos
, numRows
);
2361 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2363 attrProvider
->UpdateAttrRows( pos
, numRows
);
2365 if ( !GetBatchCount() )
2368 m_rowLabelWin
->Refresh();
2374 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2376 int numRows
= msg
.GetCommandInt();
2377 int oldNumRows
= m_numRows
;
2378 m_numRows
+= numRows
;
2380 if ( !m_rowHeights
.IsEmpty() )
2382 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2383 m_rowBottoms
.Add( 0, numRows
);
2386 if ( oldNumRows
> 0 )
2387 bottom
= m_rowBottoms
[oldNumRows
- 1];
2389 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2391 bottom
+= m_rowHeights
[i
];
2392 m_rowBottoms
[i
] = bottom
;
2396 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2398 // if we have just inserted cols into an empty grid the current
2399 // cell will be undefined...
2401 SetCurrentCell( 0, 0 );
2404 if ( !GetBatchCount() )
2407 m_rowLabelWin
->Refresh();
2413 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2415 size_t pos
= msg
.GetCommandInt();
2416 int numRows
= msg
.GetCommandInt2();
2417 m_numRows
-= numRows
;
2419 if ( !m_rowHeights
.IsEmpty() )
2421 m_rowHeights
.RemoveAt( pos
, numRows
);
2422 m_rowBottoms
.RemoveAt( pos
, numRows
);
2425 for ( i
= 0; i
< m_numRows
; i
++ )
2427 h
+= m_rowHeights
[i
];
2428 m_rowBottoms
[i
] = h
;
2434 m_currentCellCoords
= wxGridNoCellCoords
;
2438 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2439 m_currentCellCoords
.Set( 0, 0 );
2443 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2444 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2447 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2449 // ifdef'd out following patch from Paul Gammans
2451 // No need to touch column attributes, unless we
2452 // removed _all_ rows, in this case, we remove
2453 // all column attributes.
2454 // I hate to do this here, but the
2455 // needed data is not available inside UpdateAttrRows.
2456 if ( !GetNumberRows() )
2457 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2461 if ( !GetBatchCount() )
2464 m_rowLabelWin
->Refresh();
2470 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2472 size_t pos
= msg
.GetCommandInt();
2473 int numCols
= msg
.GetCommandInt2();
2474 m_numCols
+= numCols
;
2476 if ( m_useNativeHeader
)
2477 GetGridColHeader()->SetColumnCount(m_numCols
);
2479 if ( !m_colAt
.IsEmpty() )
2481 //Shift the column IDs
2483 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2485 if ( m_colAt
[i
] >= (int)pos
)
2486 m_colAt
[i
] += numCols
;
2489 m_colAt
.Insert( pos
, pos
, numCols
);
2491 //Set the new columns' positions
2492 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2498 if ( !m_colWidths
.IsEmpty() )
2500 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2501 m_colRights
.Insert( 0, pos
, numCols
);
2505 right
= m_colRights
[GetColAt( pos
- 1 )];
2508 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2510 i
= GetColAt( colPos
);
2512 right
+= m_colWidths
[i
];
2513 m_colRights
[i
] = right
;
2517 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2519 // if we have just inserted cols into an empty grid the current
2520 // cell will be undefined...
2522 SetCurrentCell( 0, 0 );
2526 m_selection
->UpdateCols( pos
, numCols
);
2527 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2529 attrProvider
->UpdateAttrCols( pos
, numCols
);
2530 if ( !GetBatchCount() )
2533 m_colWindow
->Refresh();
2539 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2541 int numCols
= msg
.GetCommandInt();
2542 int oldNumCols
= m_numCols
;
2543 m_numCols
+= numCols
;
2544 if ( m_useNativeHeader
)
2545 GetGridColHeader()->SetColumnCount(m_numCols
);
2547 if ( !m_colAt
.IsEmpty() )
2549 m_colAt
.Add( 0, numCols
);
2551 //Set the new columns' positions
2553 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2559 if ( !m_colWidths
.IsEmpty() )
2561 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2562 m_colRights
.Add( 0, numCols
);
2565 if ( oldNumCols
> 0 )
2566 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2569 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2571 i
= GetColAt( colPos
);
2573 right
+= m_colWidths
[i
];
2574 m_colRights
[i
] = right
;
2578 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2580 // if we have just inserted cols into an empty grid the current
2581 // cell will be undefined...
2583 SetCurrentCell( 0, 0 );
2585 if ( !GetBatchCount() )
2588 m_colWindow
->Refresh();
2594 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2596 size_t pos
= msg
.GetCommandInt();
2597 int numCols
= msg
.GetCommandInt2();
2598 m_numCols
-= numCols
;
2599 if ( m_useNativeHeader
)
2600 GetGridColHeader()->SetColumnCount(m_numCols
);
2602 if ( !m_colAt
.IsEmpty() )
2604 int colID
= GetColAt( pos
);
2606 m_colAt
.RemoveAt( pos
, numCols
);
2608 //Shift the column IDs
2610 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2612 if ( m_colAt
[colPos
] > colID
)
2613 m_colAt
[colPos
] -= numCols
;
2617 if ( !m_colWidths
.IsEmpty() )
2619 m_colWidths
.RemoveAt( pos
, numCols
);
2620 m_colRights
.RemoveAt( pos
, numCols
);
2624 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2626 i
= GetColAt( colPos
);
2628 w
+= m_colWidths
[i
];
2635 m_currentCellCoords
= wxGridNoCellCoords
;
2639 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2640 m_currentCellCoords
.Set( 0, 0 );
2644 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2645 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2648 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2650 // ifdef'd out following patch from Paul Gammans
2652 // No need to touch row attributes, unless we
2653 // removed _all_ columns, in this case, we remove
2654 // all row attributes.
2655 // I hate to do this here, but the
2656 // needed data is not available inside UpdateAttrCols.
2657 if ( !GetNumberCols() )
2658 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2662 if ( !GetBatchCount() )
2665 m_colWindow
->Refresh();
2672 if (result
&& !GetBatchCount() )
2673 m_gridWin
->Refresh();
2678 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2680 wxRegionIterator
iter( reg
);
2683 wxArrayInt rowlabels
;
2690 // TODO: remove this when we can...
2691 // There is a bug in wxMotif that gives garbage update
2692 // rectangles if you jump-scroll a long way by clicking the
2693 // scrollbar with middle button. This is a work-around
2695 #if defined(__WXMOTIF__)
2697 m_gridWin
->GetClientSize( &cw
, &ch
);
2698 if ( r
.GetTop() > ch
)
2700 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2703 // logical bounds of update region
2706 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2707 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2709 // find the row labels within these bounds
2712 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2714 if ( GetRowBottom(row
) < top
)
2717 if ( GetRowTop(row
) > bottom
)
2720 rowlabels
.Add( row
);
2729 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2731 wxRegionIterator
iter( reg
);
2734 wxArrayInt colLabels
;
2741 // TODO: remove this when we can...
2742 // There is a bug in wxMotif that gives garbage update
2743 // rectangles if you jump-scroll a long way by clicking the
2744 // scrollbar with middle button. This is a work-around
2746 #if defined(__WXMOTIF__)
2748 m_gridWin
->GetClientSize( &cw
, &ch
);
2749 if ( r
.GetLeft() > cw
)
2751 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2754 // logical bounds of update region
2757 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2758 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2760 // find the cells within these bounds
2764 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2766 col
= GetColAt( colPos
);
2768 if ( GetColRight(col
) < left
)
2771 if ( GetColLeft(col
) > right
)
2774 colLabels
.Add( col
);
2783 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2785 wxRegionIterator
iter( reg
);
2788 wxGridCellCoordsArray cellsExposed
;
2790 int left
, top
, right
, bottom
;
2795 // TODO: remove this when we can...
2796 // There is a bug in wxMotif that gives garbage update
2797 // rectangles if you jump-scroll a long way by clicking the
2798 // scrollbar with middle button. This is a work-around
2800 #if defined(__WXMOTIF__)
2802 m_gridWin
->GetClientSize( &cw
, &ch
);
2803 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2804 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2805 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2806 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2809 // logical bounds of update region
2811 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2812 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2814 // find the cells within these bounds
2816 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2818 if ( GetRowBottom(row
) <= top
)
2821 if ( GetRowTop(row
) > bottom
)
2824 // add all dirty cells in this row: notice that the columns which
2825 // are dirty don't depend on the row so we compute them only once
2826 // for the first dirty row and then reuse for all the next ones
2829 // do determine the dirty columns
2830 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2831 cols
.push_back(GetColAt(pos
));
2833 // if there are no dirty columns at all, nothing to do
2838 const size_t count
= cols
.size();
2839 for ( size_t n
= 0; n
< count
; n
++ )
2840 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2846 return cellsExposed
;
2850 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2853 wxPoint
pos( event
.GetPosition() );
2854 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2856 if ( event
.Dragging() )
2860 m_isDragging
= true;
2861 m_rowLabelWin
->CaptureMouse();
2864 if ( event
.LeftIsDown() )
2866 switch ( m_cursorMode
)
2868 case WXGRID_CURSOR_RESIZE_ROW
:
2870 int cw
, ch
, left
, dummy
;
2871 m_gridWin
->GetClientSize( &cw
, &ch
);
2872 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2874 wxClientDC
dc( m_gridWin
);
2877 GetRowTop(m_dragRowOrCol
) +
2878 GetRowMinimalHeight(m_dragRowOrCol
) );
2879 dc
.SetLogicalFunction(wxINVERT
);
2880 if ( m_dragLastPos
>= 0 )
2882 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
2884 dc
.DrawLine( left
, y
, left
+cw
, y
);
2889 case WXGRID_CURSOR_SELECT_ROW
:
2891 if ( (row
= YToRow( y
)) >= 0 )
2894 m_selection
->SelectRow(row
, event
);
2899 // default label to suppress warnings about "enumeration value
2900 // 'xxx' not handled in switch
2908 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
2913 if (m_rowLabelWin
->HasCapture())
2914 m_rowLabelWin
->ReleaseMouse();
2915 m_isDragging
= false;
2918 // ------------ Entering or leaving the window
2920 if ( event
.Entering() || event
.Leaving() )
2922 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
2925 // ------------ Left button pressed
2927 else if ( event
.LeftDown() )
2929 row
= YToEdgeOfRow(y
);
2930 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
2932 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
2934 else // not a request to start resizing
2938 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
2940 if ( !event
.ShiftDown() && !event
.CmdDown() )
2944 if ( event
.ShiftDown() )
2946 m_selection
->SelectBlock
2948 m_currentCellCoords
.GetRow(), 0,
2949 row
, GetNumberCols() - 1,
2955 m_selection
->SelectRow(row
, event
);
2959 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
2964 // ------------ Left double click
2966 else if (event
.LeftDClick() )
2968 row
= YToEdgeOfRow(y
);
2969 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
2971 // adjust row height depending on label text
2973 // TODO: generate RESIZING event, see #10754
2974 AutoSizeRowLabelSize( row
);
2976 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
2978 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
2981 else // not on row separator or it's not resizeable
2985 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
2987 // no default action at the moment
2992 // ------------ Left button released
2994 else if ( event
.LeftUp() )
2996 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
2997 DoEndDragResizeRow(event
);
2999 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3003 // ------------ Right button down
3005 else if ( event
.RightDown() )
3009 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3011 // no default action at the moment
3015 // ------------ Right double click
3017 else if ( event
.RightDClick() )
3021 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3023 // no default action at the moment
3027 // ------------ No buttons down and mouse moving
3029 else if ( event
.Moving() )
3031 m_dragRowOrCol
= YToEdgeOfRow( y
);
3032 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3034 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3036 if ( CanDragRowSize(m_dragRowOrCol
) )
3037 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3040 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3042 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3047 void wxGrid::UpdateColumnSortingIndicator(int col
)
3049 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3051 if ( m_useNativeHeader
)
3052 GetGridColHeader()->UpdateColumn(col
);
3053 else if ( m_nativeColumnLabels
)
3054 m_colWindow
->Refresh();
3055 //else: sorting indicator display not yet implemented in grid version
3058 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3060 if ( col
== m_sortCol
)
3062 // we are already using this column for sorting (or not sorting at all)
3063 // but we might still change the sorting order, check for it
3064 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3066 m_sortIsAscending
= ascending
;
3068 UpdateColumnSortingIndicator(m_sortCol
);
3071 else // we're changing the column used for sorting
3073 const int sortColOld
= m_sortCol
;
3075 // change it before updating the column as we want GetSortingColumn()
3076 // to return the correct new value
3079 if ( sortColOld
!= wxNOT_FOUND
)
3080 UpdateColumnSortingIndicator(sortColOld
);
3082 if ( m_sortCol
!= wxNOT_FOUND
)
3084 m_sortIsAscending
= ascending
;
3085 UpdateColumnSortingIndicator(m_sortCol
);
3090 void wxGrid::DoColHeaderClick(int col
)
3092 // we consider that the grid was resorted if this event is processed and
3094 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3096 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3101 void wxGrid::DoStartResizeCol(int col
)
3103 m_dragRowOrCol
= col
;
3105 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3108 void wxGrid::DoUpdateResizeCol(int x
)
3110 int cw
, ch
, dummy
, top
;
3111 m_gridWin
->GetClientSize( &cw
, &ch
);
3112 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3114 wxClientDC
dc( m_gridWin
);
3117 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3118 dc
.SetLogicalFunction(wxINVERT
);
3119 if ( m_dragLastPos
>= 0 )
3121 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3123 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3127 void wxGrid::DoUpdateResizeColWidth(int w
)
3129 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3132 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3135 wxPoint
pos( event
.GetPosition() );
3136 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3138 int col
= XToCol(x
);
3139 if ( event
.Dragging() )
3143 m_isDragging
= true;
3144 GetColLabelWindow()->CaptureMouse();
3146 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3147 DoStartMoveCol(col
);
3150 if ( event
.LeftIsDown() )
3152 switch ( m_cursorMode
)
3154 case WXGRID_CURSOR_RESIZE_COL
:
3155 DoUpdateResizeCol(x
);
3158 case WXGRID_CURSOR_SELECT_COL
:
3163 m_selection
->SelectCol(col
, event
);
3168 case WXGRID_CURSOR_MOVE_COL
:
3170 int posNew
= XToPos(x
);
3171 int colNew
= GetColAt(posNew
);
3173 // determine the position of the drop marker
3175 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3176 markerX
= GetColRight(colNew
);
3178 markerX
= GetColLeft(colNew
);
3180 if ( markerX
!= m_dragLastPos
)
3182 wxClientDC
dc( GetColLabelWindow() );
3186 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3190 //Clean up the last indicator
3191 if ( m_dragLastPos
>= 0 )
3193 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3195 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3196 dc
.SetPen(wxNullPen
);
3198 if ( XToCol( m_dragLastPos
) != -1 )
3199 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3202 const wxColour
*color
;
3203 //Moving to the same place? Don't draw a marker
3204 if ( colNew
== m_dragRowOrCol
)
3205 color
= wxLIGHT_GREY
;
3210 wxPen
pen( *color
, 2 );
3213 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3215 dc
.SetPen(wxNullPen
);
3217 m_dragLastPos
= markerX
- 1;
3222 // default label to suppress warnings about "enumeration value
3223 // 'xxx' not handled in switch
3231 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3236 if (GetColLabelWindow()->HasCapture())
3237 GetColLabelWindow()->ReleaseMouse();
3238 m_isDragging
= false;
3241 // ------------ Entering or leaving the window
3243 if ( event
.Entering() || event
.Leaving() )
3245 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3248 // ------------ Left button pressed
3250 else if ( event
.LeftDown() )
3252 int col
= XToEdgeOfCol(x
);
3253 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3255 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3257 else // not a request to start resizing
3261 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3263 if ( m_canDragColMove
)
3265 //Show button as pressed
3266 wxClientDC
dc( GetColLabelWindow() );
3267 int colLeft
= GetColLeft( col
);
3268 int colRight
= GetColRight( col
) - 1;
3269 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3270 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3271 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3273 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3277 if ( !event
.ShiftDown() && !event
.CmdDown() )
3281 if ( event
.ShiftDown() )
3283 m_selection
->SelectBlock
3285 0, m_currentCellCoords
.GetCol(),
3286 GetNumberRows() - 1, col
,
3292 m_selection
->SelectCol(col
, event
);
3296 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3302 // ------------ Left double click
3304 if ( event
.LeftDClick() )
3306 const int colEdge
= XToEdgeOfCol(x
);
3307 if ( colEdge
== -1 )
3310 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3312 // no default action at the moment
3317 // adjust column width depending on label text
3319 // TODO: generate RESIZING event, see #10754
3320 AutoSizeColLabelSize( colEdge
);
3322 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3324 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3329 // ------------ Left button released
3331 else if ( event
.LeftUp() )
3333 switch ( m_cursorMode
)
3335 case WXGRID_CURSOR_RESIZE_COL
:
3336 DoEndDragResizeCol(event
);
3339 case WXGRID_CURSOR_MOVE_COL
:
3340 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3342 // the column didn't actually move anywhere
3344 DoColHeaderClick(col
);
3345 m_colWindow
->Refresh(); // "unpress" the column
3349 DoEndMoveCol(XToPos(x
));
3353 case WXGRID_CURSOR_SELECT_COL
:
3354 case WXGRID_CURSOR_SELECT_CELL
:
3355 case WXGRID_CURSOR_RESIZE_ROW
:
3356 case WXGRID_CURSOR_SELECT_ROW
:
3358 DoColHeaderClick(col
);
3362 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3366 // ------------ Right button down
3368 else if ( event
.RightDown() )
3371 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3373 // no default action at the moment
3377 // ------------ Right double click
3379 else if ( event
.RightDClick() )
3382 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3384 // no default action at the moment
3388 // ------------ No buttons down and mouse moving
3390 else if ( event
.Moving() )
3392 m_dragRowOrCol
= XToEdgeOfCol( x
);
3393 if ( m_dragRowOrCol
>= 0 )
3395 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3397 if ( CanDragColSize(m_dragRowOrCol
) )
3398 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3401 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3403 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3408 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3410 if ( event
.LeftDown() )
3412 // indicate corner label by having both row and
3415 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3420 else if ( event
.LeftDClick() )
3422 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3424 else if ( event
.RightDown() )
3426 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3428 // no default action at the moment
3431 else if ( event
.RightDClick() )
3433 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3435 // no default action at the moment
3440 void wxGrid::CancelMouseCapture()
3442 // cancel operation currently in progress, whatever it is
3445 m_isDragging
= false;
3446 m_startDragPos
= wxDefaultPosition
;
3448 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3449 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3450 m_winCapture
= NULL
;
3452 // remove traces of whatever we drew on screen
3457 void wxGrid::ChangeCursorMode(CursorMode mode
,
3462 static const wxChar
*cursorModes
[] =
3472 wxLogTrace(_T("grid"),
3473 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3474 win
== m_colWindow
? _T("colLabelWin")
3475 : win
? _T("rowLabelWin")
3477 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3478 #endif // wxUSE_LOG_TRACE
3480 if ( mode
== m_cursorMode
&&
3481 win
== m_winCapture
&&
3482 captureMouse
== (m_winCapture
!= NULL
))
3487 // by default use the grid itself
3493 m_winCapture
->ReleaseMouse();
3494 m_winCapture
= NULL
;
3497 m_cursorMode
= mode
;
3499 switch ( m_cursorMode
)
3501 case WXGRID_CURSOR_RESIZE_ROW
:
3502 win
->SetCursor( m_rowResizeCursor
);
3505 case WXGRID_CURSOR_RESIZE_COL
:
3506 win
->SetCursor( m_colResizeCursor
);
3509 case WXGRID_CURSOR_MOVE_COL
:
3510 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3514 win
->SetCursor( *wxSTANDARD_CURSOR
);
3518 // we need to capture mouse when resizing
3519 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3520 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3522 if ( captureMouse
&& resize
)
3524 win
->CaptureMouse();
3529 // ----------------------------------------------------------------------------
3530 // grid mouse event processing
3531 // ----------------------------------------------------------------------------
3534 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3535 const wxGridCellCoords
& coords
,
3538 if ( coords
== wxGridNoCellCoords
)
3539 return; // we're outside any valid cell
3541 // Hide the edit control, so it won't interfere with drag-shrinking.
3542 if ( IsCellEditControlShown() )
3544 HideCellEditControl();
3545 SaveEditControlValue();
3548 switch ( event
.GetModifiers() )
3551 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3552 m_selectedBlockCorner
= coords
;
3553 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3557 if ( CanDragCell() )
3561 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3562 m_selectedBlockCorner
= coords
;
3564 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3569 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3573 // we don't handle the other key modifiers
3578 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3580 wxClientDC
dc(m_gridWin
);
3582 dc
.SetLogicalFunction(wxINVERT
);
3584 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3585 m_gridWin
->GetClientSize());
3587 // erase the previously drawn line, if any
3588 if ( m_dragLastPos
>= 0 )
3589 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3591 // we need the vertical position for rows and horizontal for columns here
3592 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3594 // don't allow resizing beneath the minimal size
3595 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3596 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3597 if ( m_dragLastPos
< posMin
)
3598 m_dragLastPos
= posMin
;
3600 // and draw it at the new position
3601 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3604 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3606 if ( !m_isDragging
)
3608 // Don't start doing anything until the mouse has been dragged far
3610 const wxPoint
& pt
= event
.GetPosition();
3611 if ( m_startDragPos
== wxDefaultPosition
)
3613 m_startDragPos
= pt
;
3617 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3618 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3622 const bool isFirstDrag
= !m_isDragging
;
3623 m_isDragging
= true;
3625 switch ( m_cursorMode
)
3627 case WXGRID_CURSOR_SELECT_CELL
:
3628 DoGridCellDrag(event
, coords
, isFirstDrag
);
3631 case WXGRID_CURSOR_RESIZE_ROW
:
3632 DoGridLineDrag(event
, wxGridRowOperations());
3635 case WXGRID_CURSOR_RESIZE_COL
:
3636 DoGridLineDrag(event
, wxGridColumnOperations());
3645 m_winCapture
= m_gridWin
;
3646 m_winCapture
->CaptureMouse();
3651 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3652 const wxGridCellCoords
& coords
,
3655 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3657 // event handled by user code, no need to do anything here
3661 if ( !event
.CmdDown() )
3664 if ( event
.ShiftDown() )
3668 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3669 m_selectedBlockCorner
= coords
;
3672 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3674 DisableCellEditControl();
3675 MakeCellVisible( coords
);
3677 if ( event
.CmdDown() )
3681 m_selection
->ToggleCellSelection(coords
, event
);
3684 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3685 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3686 m_selectedBlockCorner
= coords
;
3690 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3691 coords
!= wxGridNoCellCoords
;
3692 SetCurrentCell( coords
);
3698 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3699 const wxGridCellCoords
& coords
,
3702 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3704 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3706 // we want double click to select a cell and start editing
3707 // (i.e. to behave in same way as sequence of two slow clicks):
3708 m_waitForSlowClick
= true;
3714 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3716 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3720 m_winCapture
->ReleaseMouse();
3721 m_winCapture
= NULL
;
3724 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3727 EnableCellEditControl();
3729 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3730 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3731 editor
->StartingClick();
3735 m_waitForSlowClick
= false;
3737 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3738 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3742 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3743 m_selectedBlockBottomRight
,
3747 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3748 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3750 // Show the edit control, if it has been hidden for
3752 ShowCellEditControl();
3755 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3757 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3758 DoEndDragResizeRow(event
);
3760 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3762 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3763 DoEndDragResizeCol(event
);
3770 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3771 const wxGridCellCoords
& coords
,
3774 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3776 // out of grid cell area
3777 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3781 int dragRow
= YToEdgeOfRow( pos
.y
);
3782 int dragCol
= XToEdgeOfCol( pos
.x
);
3784 // Dragging on the corner of a cell to resize in both
3785 // directions is not implemented yet...
3787 if ( dragRow
>= 0 && dragCol
>= 0 )
3789 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3793 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3795 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3797 m_dragRowOrCol
= dragRow
;
3798 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3801 // When using the native header window we can only resize the columns by
3802 // dragging the dividers in it because we can't make it enter into the
3803 // column resizing mode programmatically
3804 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3805 CanDragGridSize() && CanDragColSize(dragCol
) )
3807 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3809 m_dragRowOrCol
= dragCol
;
3810 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3813 else // Neither on a row or col edge
3815 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3817 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3822 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
3824 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
3826 // coordinates of the cell under mouse
3827 wxGridCellCoords coords
= XYToCell(pos
);
3829 int cell_rows
, cell_cols
;
3830 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
3831 if ( (cell_rows
< 0) || (cell_cols
< 0) )
3833 coords
.SetRow(coords
.GetRow() + cell_rows
);
3834 coords
.SetCol(coords
.GetCol() + cell_cols
);
3837 if ( event
.Dragging() )
3839 if ( event
.LeftIsDown() )
3840 DoGridDragEvent(event
, coords
);
3846 m_isDragging
= false;
3847 m_startDragPos
= wxDefaultPosition
;
3849 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
3850 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
3853 if ( event
.Entering() || event
.Leaving() )
3855 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3856 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
3860 // deal with various button presses
3861 if ( event
.IsButton() )
3863 if ( coords
!= wxGridNoCellCoords
)
3865 DisableCellEditControl();
3867 if ( event
.LeftDown() )
3868 DoGridCellLeftDown(event
, coords
, pos
);
3869 else if ( event
.LeftDClick() )
3870 DoGridCellLeftDClick(event
, coords
, pos
);
3871 else if ( event
.RightDown() )
3872 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
3873 else if ( event
.RightDClick() )
3874 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
3877 // this one should be called even if we're not over any cell
3878 if ( event
.LeftUp() )
3880 DoGridCellLeftUp(event
, coords
);
3883 else if ( event
.Moving() )
3885 DoGridMouseMoveEvent(event
, coords
, pos
);
3887 else // unknown mouse event?
3893 // this function returns true only if the size really changed
3894 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
3896 if ( m_dragLastPos
== -1 )
3899 const wxGridOperations
& doper
= oper
.Dual();
3901 const wxSize size
= m_gridWin
->GetClientSize();
3903 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
3905 // erase the last line we drew
3906 wxClientDC
dc(m_gridWin
);
3908 dc
.SetLogicalFunction(wxINVERT
);
3910 const int posLineStart
= oper
.Select(ptOrigin
);
3911 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
3913 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
3915 // temporarily hide the edit control before resizing
3916 HideCellEditControl();
3917 SaveEditControlValue();
3919 // do resize the line
3920 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
3921 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
3922 oper
.SetLineSize(this, m_dragRowOrCol
,
3923 wxMax(m_dragLastPos
- lineStart
,
3924 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
3926 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
3930 // refresh now if we're not frozen
3931 if ( !GetBatchCount() )
3933 // we need to refresh everything beyond the resized line in the header
3936 // get the position from which to refresh in the other direction
3937 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
3938 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
3940 // we only need the ordinate (for rows) or abscissa (for columns) here,
3941 // and need to cover the entire window in the other direction
3942 oper
.Select(rect
) = 0;
3944 wxRect
rectHeader(rect
.GetPosition(),
3947 oper
.GetHeaderWindowSize(this),
3948 doper
.Select(size
) - doper
.Select(rect
)
3951 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
3954 // also refresh the grid window: extend the rectangle
3957 oper
.SelectSize(rect
) = oper
.Select(size
);
3959 int subtractLines
= 0;
3960 const int lineStart
= oper
.PosToLine(this, posLineStart
);
3961 if ( lineStart
>= 0 )
3963 // ensure that if we have a multi-cell block we redraw all of
3964 // it by increasing the refresh area to cover it entirely if a
3965 // part of it is affected
3966 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
3967 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
3969 int cellLines
= oper
.Select(
3970 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
3971 if ( cellLines
< subtractLines
)
3972 subtractLines
= cellLines
;
3977 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
3978 startPos
= doper
.CalcScrolledPosition(this, startPos
);
3980 doper
.Select(rect
) = startPos
;
3981 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
3983 m_gridWin
->Refresh(false, &rect
);
3987 // show the edit control back again
3988 ShowCellEditControl();
3993 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
3995 // TODO: generate RESIZING event, see #10754
3997 if ( DoEndDragResizeLine(wxGridRowOperations()) )
3998 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4001 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4003 // TODO: generate RESIZING event, see #10754
4005 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4006 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4009 void wxGrid::DoStartMoveCol(int col
)
4011 m_dragRowOrCol
= col
;
4014 void wxGrid::DoEndMoveCol(int pos
)
4016 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4018 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4019 SetColPos(m_dragRowOrCol
, pos
);
4020 //else: vetoed by user
4022 m_dragRowOrCol
= -1;
4025 void wxGrid::RefreshAfterColPosChange()
4027 // recalculate the column rights as the column positions have changed,
4028 // unless we calculate them dynamically because all columns widths are the
4029 // same and it's easy to do
4030 if ( !m_colWidths
.empty() )
4033 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4035 int colID
= GetColAt( colPos
);
4037 colRight
+= m_colWidths
[colID
];
4038 m_colRights
[colID
] = colRight
;
4042 // and make the changes visible
4043 if ( m_useNativeHeader
)
4045 if ( m_colAt
.empty() )
4046 GetGridColHeader()->ResetColumnsOrder();
4048 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4052 m_colWindow
->Refresh();
4054 m_gridWin
->Refresh();
4057 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4061 RefreshAfterColPosChange();
4064 void wxGrid::SetColPos(int idx
, int pos
)
4066 // we're going to need m_colAt now, initialize it if needed
4067 if ( m_colAt
.empty() )
4069 m_colAt
.reserve(m_numCols
);
4070 for ( int i
= 0; i
< m_numCols
; i
++ )
4071 m_colAt
.push_back(i
);
4074 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4076 RefreshAfterColPosChange();
4079 void wxGrid::ResetColPos()
4083 RefreshAfterColPosChange();
4086 void wxGrid::EnableDragColMove( bool enable
)
4088 if ( m_canDragColMove
== enable
)
4091 if ( m_useNativeHeader
)
4093 // update all columns to make them [not] reorderable
4094 GetGridColHeader()->SetColumnCount(m_numCols
);
4097 m_canDragColMove
= enable
;
4099 // we use to call ResetColPos() from here if !enable but this doesn't seem
4100 // right as it would mean there would be no way to "freeze" the current
4101 // columns order by disabling moving them after putting them in the desired
4102 // order, whereas now you can always call ResetColPos() manually if needed
4107 // ------ interaction with data model
4109 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4111 switch ( msg
.GetId() )
4113 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4114 return GetModelValues();
4116 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4117 return SetModelValues();
4119 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4120 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4121 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4122 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4123 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4124 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4125 return Redimension( msg
);
4132 // The behaviour of this function depends on the grid table class
4133 // Clear() function. For the default wxGridStringTable class the
4134 // behaviour is to replace all cell contents with wxEmptyString but
4135 // not to change the number of rows or cols.
4137 void wxGrid::ClearGrid()
4141 if (IsCellEditControlEnabled())
4142 DisableCellEditControl();
4145 if (!GetBatchCount())
4146 m_gridWin
->Refresh();
4151 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4152 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4154 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4159 if ( IsCellEditControlEnabled() )
4160 DisableCellEditControl();
4162 return (m_table
->*funcModify
)(pos
, num
);
4164 // the table will have sent the results of the insert row
4165 // operation to this view object as a grid table message
4169 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4170 int num
, bool WXUNUSED(updateLabels
))
4172 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4177 return (m_table
->*funcAppend
)(num
);
4180 // ----------------------------------------------------------------------------
4181 // event generation helpers
4182 // ----------------------------------------------------------------------------
4185 wxGrid::SendGridSizeEvent(wxEventType type
,
4187 const wxMouseEvent
& mouseEv
)
4189 int rowOrCol
= row
== -1 ? col
: row
;
4191 wxGridSizeEvent
gridEvt( GetId(),
4195 mouseEv
.GetX() + GetRowLabelSize(),
4196 mouseEv
.GetY() + GetColLabelSize(),
4199 GetEventHandler()->ProcessEvent(gridEvt
);
4202 // Generate a grid event based on a mouse event and return:
4203 // -1 if the event was vetoed
4204 // +1 if the event was processed (but not vetoed)
4205 // 0 if the event wasn't handled
4207 wxGrid::SendEvent(wxEventType type
,
4209 const wxMouseEvent
& mouseEv
)
4211 bool claimed
, vetoed
;
4213 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4215 // Right now, it should _never_ end up here!
4216 wxGridRangeSelectEvent
gridEvt( GetId(),
4219 m_selectedBlockTopLeft
,
4220 m_selectedBlockBottomRight
,
4224 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4225 vetoed
= !gridEvt
.IsAllowed();
4227 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4228 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4229 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4230 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4232 wxPoint pos
= mouseEv
.GetPosition();
4234 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4235 pos
.y
+= GetColLabelSize();
4236 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4237 pos
.x
+= GetRowLabelSize();
4239 wxGridEvent
gridEvt( GetId(),
4247 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4248 vetoed
= !gridEvt
.IsAllowed();
4252 wxGridEvent
gridEvt( GetId(),
4256 mouseEv
.GetX() + GetRowLabelSize(),
4257 mouseEv
.GetY() + GetColLabelSize(),
4260 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4261 vetoed
= !gridEvt
.IsAllowed();
4264 // A Veto'd event may not be `claimed' so test this first
4268 return claimed
? 1 : 0;
4271 // Generate a grid event of specified type, return value same as above
4274 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4276 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4277 gridEvt
.SetString(s
);
4279 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4281 // A Veto'd event may not be `claimed' so test this first
4282 if ( !gridEvt
.IsAllowed() )
4285 return claimed
? 1 : 0;
4288 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4290 // needed to prevent zillions of paint events on MSW
4294 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4296 // Don't do anything if between Begin/EndBatch...
4297 // EndBatch() will do all this on the last nested one anyway.
4298 if ( m_created
&& !GetBatchCount() )
4300 // Refresh to get correct scrolled position:
4301 wxScrolledWindow::Refresh(eraseb
, rect
);
4305 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4306 int width_label
, width_cell
, height_label
, height_cell
;
4309 // Copy rectangle can get scroll offsets..
4310 rect_x
= rect
->GetX();
4311 rect_y
= rect
->GetY();
4312 rectWidth
= rect
->GetWidth();
4313 rectHeight
= rect
->GetHeight();
4315 width_label
= m_rowLabelWidth
- rect_x
;
4316 if (width_label
> rectWidth
)
4317 width_label
= rectWidth
;
4319 height_label
= m_colLabelHeight
- rect_y
;
4320 if (height_label
> rectHeight
)
4321 height_label
= rectHeight
;
4323 if (rect_x
> m_rowLabelWidth
)
4325 x
= rect_x
- m_rowLabelWidth
;
4326 width_cell
= rectWidth
;
4331 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4334 if (rect_y
> m_colLabelHeight
)
4336 y
= rect_y
- m_colLabelHeight
;
4337 height_cell
= rectHeight
;
4342 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4345 // Paint corner label part intersecting rect.
4346 if ( width_label
> 0 && height_label
> 0 )
4348 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4349 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4352 // Paint col labels part intersecting rect.
4353 if ( width_cell
> 0 && height_label
> 0 )
4355 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4356 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4359 // Paint row labels part intersecting rect.
4360 if ( width_label
> 0 && height_cell
> 0 )
4362 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4363 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4366 // Paint cell area part intersecting rect.
4367 if ( width_cell
> 0 && height_cell
> 0 )
4369 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4370 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4375 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4376 m_colWindow
->Refresh(eraseb
, NULL
);
4377 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4378 m_gridWin
->Refresh(eraseb
, NULL
);
4383 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4385 if (m_targetWindow
!= this) // check whether initialisation has been done
4387 // reposition our children windows
4392 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4394 if ( m_inOnKeyDown
)
4396 // shouldn't be here - we are going round in circles...
4398 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4401 m_inOnKeyDown
= true;
4403 // propagate the event up and see if it gets processed
4404 wxWindow
*parent
= GetParent();
4405 wxKeyEvent
keyEvt( event
);
4406 keyEvt
.SetEventObject( parent
);
4408 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4410 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4412 if (event
.GetKeyCode() == WXK_RIGHT
)
4413 event
.m_keyCode
= WXK_LEFT
;
4414 else if (event
.GetKeyCode() == WXK_LEFT
)
4415 event
.m_keyCode
= WXK_RIGHT
;
4418 // try local handlers
4419 switch ( event
.GetKeyCode() )
4422 if ( event
.ControlDown() )
4423 MoveCursorUpBlock( event
.ShiftDown() );
4425 MoveCursorUp( event
.ShiftDown() );
4429 if ( event
.ControlDown() )
4430 MoveCursorDownBlock( event
.ShiftDown() );
4432 MoveCursorDown( event
.ShiftDown() );
4436 if ( event
.ControlDown() )
4437 MoveCursorLeftBlock( event
.ShiftDown() );
4439 MoveCursorLeft( event
.ShiftDown() );
4443 if ( event
.ControlDown() )
4444 MoveCursorRightBlock( event
.ShiftDown() );
4446 MoveCursorRight( event
.ShiftDown() );
4450 case WXK_NUMPAD_ENTER
:
4451 if ( event
.ControlDown() )
4453 event
.Skip(); // to let the edit control have the return
4457 if ( GetGridCursorRow() < GetNumberRows()-1 )
4459 MoveCursorDown( event
.ShiftDown() );
4463 // at the bottom of a column
4464 DisableCellEditControl();
4474 if (event
.ShiftDown())
4476 if ( GetGridCursorCol() > 0 )
4478 MoveCursorLeft( false );
4483 DisableCellEditControl();
4488 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4490 MoveCursorRight( false );
4495 DisableCellEditControl();
4501 if ( event
.ControlDown() )
4512 if ( event
.ControlDown() )
4514 GoToCell(m_numRows
- 1, m_numCols
- 1);
4531 // Ctrl-Space selects the current column, Shift-Space -- the
4532 // current row and Ctrl-Shift-Space -- everything
4533 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4536 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4540 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4543 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4544 m_selection
->SelectBlock(0, 0,
4545 m_numRows
- 1, m_numCols
- 1);
4549 if ( !IsEditable() )
4551 MoveCursorRight(false);
4554 //else: fall through
4567 m_inOnKeyDown
= false;
4570 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4572 // try local handlers
4574 if ( event
.GetKeyCode() == WXK_SHIFT
)
4576 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4577 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4581 m_selection
->SelectBlock(
4582 m_selectedBlockTopLeft
,
4583 m_selectedBlockBottomRight
,
4588 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4589 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4590 m_selectedBlockCorner
= wxGridNoCellCoords
;
4594 void wxGrid::OnChar( wxKeyEvent
& event
)
4596 // is it possible to edit the current cell at all?
4597 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4599 // yes, now check whether the cells editor accepts the key
4600 int row
= m_currentCellCoords
.GetRow();
4601 int col
= m_currentCellCoords
.GetCol();
4602 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4603 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4605 // <F2> is special and will always start editing, for
4606 // other keys - ask the editor itself
4607 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4608 || editor
->IsAcceptedKey(event
) )
4610 // ensure cell is visble
4611 MakeCellVisible(row
, col
);
4612 EnableCellEditControl();
4614 // a problem can arise if the cell is not completely
4615 // visible (even after calling MakeCellVisible the
4616 // control is not created and calling StartingKey will
4618 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4619 editor
->StartingKey(event
);
4635 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4639 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4641 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4643 // the event has been vetoed - do nothing
4647 #if !defined(__WXMAC__)
4648 wxClientDC
dc( m_gridWin
);
4652 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4654 DisableCellEditControl();
4656 if ( IsVisible( m_currentCellCoords
, false ) )
4659 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4660 if ( !m_gridLinesEnabled
)
4668 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4670 // Otherwise refresh redraws the highlight!
4671 m_currentCellCoords
= coords
;
4673 #if defined(__WXMAC__)
4674 m_gridWin
->Refresh(true /*, & r */);
4676 DrawGridCellArea( dc
, cells
);
4677 DrawAllGridLines( dc
, r
);
4682 m_currentCellCoords
= coords
;
4684 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4685 #if !defined(__WXMAC__)
4686 DrawCellHighlight( dc
, attr
);
4694 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4695 int bottomRow
, int rightCol
)
4699 switch ( m_selection
->GetSelectionMode() )
4702 wxFAIL_MSG( "unknown selection mode" );
4705 case wxGridSelectCells
:
4706 // arbitrary blocks selection allowed so just use the cell
4707 // coordinates as is
4710 case wxGridSelectRows
:
4711 // only full rows selection allowd, ensure that we do select
4714 rightCol
= GetNumberCols() - 1;
4717 case wxGridSelectColumns
:
4718 // same as above but for columns
4720 bottomRow
= GetNumberRows() - 1;
4723 case wxGridSelectRowsOrColumns
:
4724 // in this mode we can select only full rows or full columns so
4725 // it doesn't make sense to select blocks at all (and we can't
4726 // extend the block because there is no preferred direction, we
4727 // could only extend it to cover the entire grid but this is
4733 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4734 MakeCellVisible(m_selectedBlockCorner
);
4736 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4737 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4739 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4740 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4742 // First the case that we selected a completely new area
4743 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4744 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4747 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4748 wxGridCellCoords ( bottomRow
, rightCol
) );
4749 m_gridWin
->Refresh( false, &rect
);
4752 // Now handle changing an existing selection area.
4753 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4754 m_selectedBlockBottomRight
!= updateBottomRight
)
4756 // Compute two optimal update rectangles:
4757 // Either one rectangle is a real subset of the
4758 // other, or they are (almost) disjoint!
4760 bool need_refresh
[4];
4764 need_refresh
[3] = false;
4767 // Store intermediate values
4768 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4769 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4770 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4771 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4773 // Determine the outer/inner coordinates.
4774 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4775 EnsureFirstLessThanSecond(oldTop
, topRow
);
4776 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4777 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4779 // Now, either the stuff marked old is the outer
4780 // rectangle or we don't have a situation where one
4781 // is contained in the other.
4783 if ( oldLeft
< leftCol
)
4785 // Refresh the newly selected or deselected
4786 // area to the left of the old or new selection.
4787 need_refresh
[0] = true;
4788 rect
[0] = BlockToDeviceRect(
4789 wxGridCellCoords( oldTop
, oldLeft
),
4790 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4793 if ( oldTop
< topRow
)
4795 // Refresh the newly selected or deselected
4796 // area above the old or new selection.
4797 need_refresh
[1] = true;
4798 rect
[1] = BlockToDeviceRect(
4799 wxGridCellCoords( oldTop
, leftCol
),
4800 wxGridCellCoords( topRow
- 1, rightCol
) );
4803 if ( oldRight
> rightCol
)
4805 // Refresh the newly selected or deselected
4806 // area to the right of the old or new selection.
4807 need_refresh
[2] = true;
4808 rect
[2] = BlockToDeviceRect(
4809 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4810 wxGridCellCoords( oldBottom
, oldRight
) );
4813 if ( oldBottom
> bottomRow
)
4815 // Refresh the newly selected or deselected
4816 // area below the old or new selection.
4817 need_refresh
[3] = true;
4818 rect
[3] = BlockToDeviceRect(
4819 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4820 wxGridCellCoords( oldBottom
, rightCol
) );
4823 // various Refresh() calls
4824 for (i
= 0; i
< 4; i
++ )
4825 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4826 m_gridWin
->Refresh( false, &(rect
[i
]) );
4830 m_selectedBlockTopLeft
= updateTopLeft
;
4831 m_selectedBlockBottomRight
= updateBottomRight
;
4835 // ------ functions to get/send data (see also public functions)
4838 bool wxGrid::GetModelValues()
4840 // Hide the editor, so it won't hide a changed value.
4841 HideCellEditControl();
4845 // all we need to do is repaint the grid
4847 m_gridWin
->Refresh();
4854 bool wxGrid::SetModelValues()
4858 // Disable the editor, so it won't hide a changed value.
4859 // Do we also want to save the current value of the editor first?
4861 DisableCellEditControl();
4865 for ( row
= 0; row
< m_numRows
; row
++ )
4867 for ( col
= 0; col
< m_numCols
; col
++ )
4869 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
4879 // Note - this function only draws cells that are in the list of
4880 // exposed cells (usually set from the update region by
4881 // CalcExposedCells)
4883 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
4885 if ( !m_numRows
|| !m_numCols
)
4888 int i
, numCells
= cells
.GetCount();
4889 int row
, col
, cell_rows
, cell_cols
;
4890 wxGridCellCoordsArray redrawCells
;
4892 for ( i
= numCells
- 1; i
>= 0; i
-- )
4894 row
= cells
[i
].GetRow();
4895 col
= cells
[i
].GetCol();
4896 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
4898 // If this cell is part of a multicell block, find owner for repaint
4899 if ( cell_rows
<= 0 || cell_cols
<= 0 )
4901 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
4902 bool marked
= false;
4903 for ( int j
= 0; j
< numCells
; j
++ )
4905 if ( cell
== cells
[j
] )
4914 int count
= redrawCells
.GetCount();
4915 for (int j
= 0; j
< count
; j
++)
4917 if ( cell
== redrawCells
[j
] )
4925 redrawCells
.Add( cell
);
4928 // don't bother drawing this cell
4932 // If this cell is empty, find cell to left that might want to overflow
4933 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
4935 for ( int l
= 0; l
< cell_rows
; l
++ )
4937 // find a cell in this row to leave already marked for repaint
4939 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
4940 if ((redrawCells
[k
].GetCol() < left
) &&
4941 (redrawCells
[k
].GetRow() == row
))
4943 left
= redrawCells
[k
].GetCol();
4947 left
= 0; // oh well
4949 for (int j
= col
- 1; j
>= left
; j
--)
4951 if (!m_table
->IsEmptyCell(row
+ l
, j
))
4953 if (GetCellOverflow(row
+ l
, j
))
4955 wxGridCellCoords
cell(row
+ l
, j
);
4956 bool marked
= false;
4958 for (int k
= 0; k
< numCells
; k
++)
4960 if ( cell
== cells
[k
] )
4969 int count
= redrawCells
.GetCount();
4970 for (int k
= 0; k
< count
; k
++)
4972 if ( cell
== redrawCells
[k
] )
4979 redrawCells
.Add( cell
);
4988 DrawCell( dc
, cells
[i
] );
4991 numCells
= redrawCells
.GetCount();
4993 for ( i
= numCells
- 1; i
>= 0; i
-- )
4995 DrawCell( dc
, redrawCells
[i
] );
4999 void wxGrid::DrawGridSpace( wxDC
& dc
)
5002 m_gridWin
->GetClientSize( &cw
, &ch
);
5005 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5007 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5008 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5010 if ( right
> rightCol
|| bottom
> bottomRow
)
5013 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5015 dc
.SetBrush(GetDefaultCellBackgroundColour());
5016 dc
.SetPen( *wxTRANSPARENT_PEN
);
5018 if ( right
> rightCol
)
5020 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5023 if ( bottom
> bottomRow
)
5025 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5030 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5032 int row
= coords
.GetRow();
5033 int col
= coords
.GetCol();
5035 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5038 // we draw the cell border ourselves
5039 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5041 bool isCurrent
= coords
== m_currentCellCoords
;
5043 wxRect rect
= CellToRect( row
, col
);
5045 // if the editor is shown, we should use it and not the renderer
5046 // Note: However, only if it is really _shown_, i.e. not hidden!
5047 if ( isCurrent
&& IsCellEditControlShown() )
5049 // NB: this "#if..." is temporary and fixes a problem where the
5050 // edit control is erased by this code after being rendered.
5051 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5052 // implicitly, causing this out-of order render.
5053 #if !defined(__WXMAC__)
5054 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5055 editor
->PaintBackground(rect
, attr
);
5061 // but all the rest is drawn by the cell renderer and hence may be customized
5062 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5063 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5070 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5072 // don't show highlight when the grid doesn't have focus
5076 int row
= m_currentCellCoords
.GetRow();
5077 int col
= m_currentCellCoords
.GetCol();
5079 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5082 wxRect rect
= CellToRect(row
, col
);
5084 // hmmm... what could we do here to show that the cell is disabled?
5085 // for now, I just draw a thinner border than for the other ones, but
5086 // it doesn't look really good
5088 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5092 // The center of the drawn line is where the position/width/height of
5093 // the rectangle is actually at (on wxMSW at least), so the
5094 // size of the rectangle is reduced to compensate for the thickness of
5095 // the line. If this is too strange on non-wxMSW platforms then
5096 // please #ifdef this appropriately.
5097 rect
.x
+= penWidth
/ 2;
5098 rect
.y
+= penWidth
/ 2;
5099 rect
.width
-= penWidth
- 1;
5100 rect
.height
-= penWidth
- 1;
5102 // Now draw the rectangle
5103 // use the cellHighlightColour if the cell is inside a selection, this
5104 // will ensure the cell is always visible.
5105 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5106 : m_cellHighlightColour
,
5108 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5109 dc
.DrawRectangle(rect
);
5113 wxPen
wxGrid::GetDefaultGridLinePen()
5115 return wxPen(GetGridLineColour());
5118 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5120 return GetDefaultGridLinePen();
5123 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5125 return GetDefaultGridLinePen();
5128 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5130 int row
= coords
.GetRow();
5131 int col
= coords
.GetCol();
5132 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5136 wxRect rect
= CellToRect( row
, col
);
5138 // right hand border
5139 dc
.SetPen( GetColGridLinePen(col
) );
5140 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5141 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5144 dc
.SetPen( GetRowGridLinePen(row
) );
5145 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5146 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5149 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5151 // This if block was previously in wxGrid::OnPaint but that doesn't
5152 // seem to get called under wxGTK - MB
5154 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5155 m_numRows
&& m_numCols
)
5157 m_currentCellCoords
.Set(0, 0);
5160 if ( IsCellEditControlShown() )
5162 // don't show highlight when the edit control is shown
5166 // if the active cell was repainted, repaint its highlight too because it
5167 // might have been damaged by the grid lines
5168 size_t count
= cells
.GetCount();
5169 for ( size_t n
= 0; n
< count
; n
++ )
5171 wxGridCellCoords cell
= cells
[n
];
5173 // If we are using attributes, then we may have just exposed another
5174 // cell in a partially-visible merged cluster of cells. If the "anchor"
5175 // (upper left) cell of this merged cluster is the cell indicated by
5176 // m_currentCellCoords, then we need to refresh the cell highlight even
5177 // though the "anchor" itself is not part of our update segment.
5178 if ( CanHaveAttributes() )
5182 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5185 cell
.SetRow(cell
.GetRow() + rows
);
5188 cell
.SetCol(cell
.GetCol() + cols
);
5191 if ( cell
== m_currentCellCoords
)
5193 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5194 DrawCellHighlight(dc
, attr
);
5202 // This is used to redraw all grid lines e.g. when the grid line colour
5205 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5207 if ( !m_gridLinesEnabled
)
5210 int top
, bottom
, left
, right
;
5213 m_gridWin
->GetClientSize(&cw
, &ch
);
5214 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5215 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5217 // avoid drawing grid lines past the last row and col
5218 if ( m_gridLinesClipHorz
)
5223 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5224 if ( right
> lastColRight
)
5225 right
= lastColRight
;
5228 if ( m_gridLinesClipVert
)
5233 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5234 if ( bottom
> lastRowBottom
)
5235 bottom
= lastRowBottom
;
5238 // no gridlines inside multicells, clip them out
5239 int leftCol
= GetColPos( internalXToCol(left
) );
5240 int topRow
= internalYToRow(top
);
5241 int rightCol
= GetColPos( internalXToCol(right
) );
5242 int bottomRow
= internalYToRow(bottom
);
5244 wxRegion
clippedcells(0, 0, cw
, ch
);
5246 int cell_rows
, cell_cols
;
5249 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5251 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5253 int i
= GetColAt( colPos
);
5255 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5256 if ((cell_rows
> 1) || (cell_cols
> 1))
5258 rect
= CellToRect(j
,i
);
5259 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5260 clippedcells
.Subtract(rect
);
5262 else if ((cell_rows
< 0) || (cell_cols
< 0))
5264 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5265 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5266 clippedcells
.Subtract(rect
);
5271 dc
.SetDeviceClippingRegion( clippedcells
);
5274 // horizontal grid lines
5275 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5277 int bot
= GetRowBottom(i
) - 1;
5284 dc
.SetPen( GetRowGridLinePen(i
) );
5285 dc
.DrawLine( left
, bot
, right
, bot
);
5289 // vertical grid lines
5290 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5292 int i
= GetColAt( colPos
);
5294 int colRight
= GetColRight(i
);
5296 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5300 if ( colRight
> right
)
5303 if ( colRight
>= left
)
5305 dc
.SetPen( GetColGridLinePen(i
) );
5306 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5310 dc
.DestroyClippingRegion();
5313 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5318 const size_t numLabels
= rows
.GetCount();
5319 for ( size_t i
= 0; i
< numLabels
; i
++ )
5321 DrawRowLabel( dc
, rows
[i
] );
5325 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5327 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5332 int rowTop
= GetRowTop(row
),
5333 rowBottom
= GetRowBottom(row
) - 1;
5335 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
5336 dc
.DrawLine( m_rowLabelWidth
- 1, rowTop
, m_rowLabelWidth
- 1, rowBottom
);
5337 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5338 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
5340 dc
.SetPen( *wxWHITE_PEN
);
5341 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
5342 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
- 1, rowTop
);
5344 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
5345 dc
.SetTextForeground( GetLabelTextColour() );
5346 dc
.SetFont( GetLabelFont() );
5349 GetRowLabelAlignment( &hAlign
, &vAlign
);
5352 rect
.SetY( GetRowTop(row
) + 2 );
5353 rect
.SetWidth( m_rowLabelWidth
- 4 );
5354 rect
.SetHeight( GetRowHeight(row
) - 4 );
5355 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5358 void wxGrid::UseNativeColHeader(bool native
)
5360 if ( native
== m_useNativeHeader
)
5364 m_useNativeHeader
= native
;
5366 CreateColumnWindow();
5368 if ( m_useNativeHeader
)
5369 GetGridColHeader()->SetColumnCount(m_numCols
);
5373 void wxGrid::SetUseNativeColLabels( bool native
)
5375 wxASSERT_MSG( !m_useNativeHeader
,
5376 "doesn't make sense when using native header" );
5378 m_nativeColumnLabels
= native
;
5381 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5382 SetColLabelSize( height
);
5385 GetColLabelWindow()->Refresh();
5386 m_cornerLabelWin
->Refresh();
5389 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5394 const size_t numLabels
= cols
.GetCount();
5395 for ( size_t i
= 0; i
< numLabels
; i
++ )
5397 DrawColLabel( dc
, cols
[i
] );
5401 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5403 if ( m_nativeColumnLabels
)
5405 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5408 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5412 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
5413 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
5414 m_rowLabelWidth
- 1, 0 );
5415 dc
.DrawLine( m_rowLabelWidth
- 1, m_colLabelHeight
- 1,
5416 0, m_colLabelHeight
- 1 );
5417 dc
.DrawLine( 0, 0, m_rowLabelWidth
, 0 );
5418 dc
.DrawLine( 0, 0, 0, m_colLabelHeight
);
5420 dc
.SetPen( *wxWHITE_PEN
);
5421 dc
.DrawLine( 1, 1, m_rowLabelWidth
- 1, 1 );
5422 dc
.DrawLine( 1, 1, 1, m_colLabelHeight
- 1 );
5426 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5428 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5431 int colLeft
= GetColLeft(col
);
5433 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5435 if ( m_nativeColumnLabels
)
5437 wxRendererNative::Get().DrawHeaderButton
5439 GetColLabelWindow(),
5444 ? IsSortOrderAscending()
5445 ? wxHDR_SORT_ICON_UP
5446 : wxHDR_SORT_ICON_DOWN
5447 : wxHDR_SORT_ICON_NONE
5452 int colRight
= GetColRight(col
) - 1;
5454 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
5455 dc
.DrawLine( colRight
, 0,
5456 colRight
, m_colLabelHeight
- 1 );
5457 dc
.DrawLine( colLeft
, 0,
5459 dc
.DrawLine( colLeft
, m_colLabelHeight
- 1,
5460 colRight
+ 1, m_colLabelHeight
- 1 );
5462 dc
.SetPen( *wxWHITE_PEN
);
5463 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
- 1 );
5464 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
5467 dc
.SetBackgroundMode( wxBRUSHSTYLE_TRANSPARENT
);
5468 dc
.SetTextForeground( GetLabelTextColour() );
5469 dc
.SetFont( GetLabelFont() );
5472 GetColLabelAlignment( &hAlign
, &vAlign
);
5473 const int orient
= GetColLabelTextOrientation();
5476 DrawTextRectangle(dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5479 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5480 // we just have to add textOrientation support
5481 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5482 const wxString
& value
,
5486 int textOrientation
)
5488 wxArrayString lines
;
5490 StringToLines( value
, lines
);
5492 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5495 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5496 const wxArrayString
& lines
,
5500 int textOrientation
)
5502 if ( lines
.empty() )
5505 wxDCClipper
clip(dc
, rect
);
5510 if ( textOrientation
== wxHORIZONTAL
)
5511 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5513 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5517 switch ( vertAlign
)
5519 case wxALIGN_BOTTOM
:
5520 if ( textOrientation
== wxHORIZONTAL
)
5521 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5523 x
= rect
.x
+ rect
.width
- textWidth
;
5526 case wxALIGN_CENTRE
:
5527 if ( textOrientation
== wxHORIZONTAL
)
5528 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5530 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5535 if ( textOrientation
== wxHORIZONTAL
)
5542 // Align each line of a multi-line label
5543 size_t nLines
= lines
.GetCount();
5544 for ( size_t l
= 0; l
< nLines
; l
++ )
5546 const wxString
& line
= lines
[l
];
5550 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5554 wxCoord lineWidth
= 0,
5556 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5558 switch ( horizAlign
)
5561 if ( textOrientation
== wxHORIZONTAL
)
5562 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5564 y
= rect
.y
+ lineWidth
+ 1;
5567 case wxALIGN_CENTRE
:
5568 if ( textOrientation
== wxHORIZONTAL
)
5569 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5571 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5576 if ( textOrientation
== wxHORIZONTAL
)
5579 y
= rect
.y
+ rect
.height
- 1;
5583 if ( textOrientation
== wxHORIZONTAL
)
5585 dc
.DrawText( line
, x
, y
);
5590 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5596 // Split multi-line text up into an array of strings.
5597 // Any existing contents of the string array are preserved.
5599 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5600 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5604 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5605 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5607 while ( startPos
< (int)tVal
.length() )
5609 pos
= tVal
.Mid(startPos
).Find( eol
);
5614 else if ( pos
== 0 )
5616 lines
.Add( wxEmptyString
);
5620 lines
.Add( tVal
.Mid(startPos
, pos
) );
5623 startPos
+= pos
+ 1;
5626 if ( startPos
< (int)tVal
.length() )
5628 lines
.Add( tVal
.Mid( startPos
) );
5632 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5633 const wxArrayString
& lines
,
5634 long *width
, long *height
) const
5638 wxCoord lineW
= 0, lineH
= 0;
5641 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5643 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5644 w
= wxMax( w
, lineW
);
5653 // ------ Batch processing.
5655 void wxGrid::EndBatch()
5657 if ( m_batchCount
> 0 )
5660 if ( !m_batchCount
)
5663 m_rowLabelWin
->Refresh();
5664 m_colWindow
->Refresh();
5665 m_cornerLabelWin
->Refresh();
5666 m_gridWin
->Refresh();
5671 // Use this, rather than wxWindow::Refresh(), to force an immediate
5672 // repainting of the grid. Has no effect if you are already inside a
5673 // BeginBatch / EndBatch block.
5675 void wxGrid::ForceRefresh()
5681 bool wxGrid::Enable(bool enable
)
5683 if ( !wxScrolledWindow::Enable(enable
) )
5686 // redraw in the new state
5687 m_gridWin
->Refresh();
5693 // ------ Edit control functions
5696 void wxGrid::EnableEditing( bool edit
)
5698 if ( edit
!= m_editable
)
5701 EnableCellEditControl(edit
);
5706 void wxGrid::EnableCellEditControl( bool enable
)
5711 if ( enable
!= m_cellEditCtrlEnabled
)
5715 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5718 // this should be checked by the caller!
5719 wxASSERT_MSG( CanEnableCellControl(), _T("can't enable editing for this cell!") );
5721 // do it before ShowCellEditControl()
5722 m_cellEditCtrlEnabled
= enable
;
5724 ShowCellEditControl();
5728 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5730 HideCellEditControl();
5731 SaveEditControlValue();
5733 // do it after HideCellEditControl()
5734 m_cellEditCtrlEnabled
= enable
;
5739 bool wxGrid::IsCurrentCellReadOnly() const
5742 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5743 bool readonly
= attr
->IsReadOnly();
5749 bool wxGrid::CanEnableCellControl() const
5751 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5752 !IsCurrentCellReadOnly();
5755 bool wxGrid::IsCellEditControlEnabled() const
5757 // the cell edit control might be disable for all cells or just for the
5758 // current one if it's read only
5759 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5762 bool wxGrid::IsCellEditControlShown() const
5764 bool isShown
= false;
5766 if ( m_cellEditCtrlEnabled
)
5768 int row
= m_currentCellCoords
.GetRow();
5769 int col
= m_currentCellCoords
.GetCol();
5770 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5771 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5776 if ( editor
->IsCreated() )
5778 isShown
= editor
->GetControl()->IsShown();
5788 void wxGrid::ShowCellEditControl()
5790 if ( IsCellEditControlEnabled() )
5792 if ( !IsVisible( m_currentCellCoords
, false ) )
5794 m_cellEditCtrlEnabled
= false;
5799 wxRect rect
= CellToRect( m_currentCellCoords
);
5800 int row
= m_currentCellCoords
.GetRow();
5801 int col
= m_currentCellCoords
.GetCol();
5803 // if this is part of a multicell, find owner (topleft)
5804 int cell_rows
, cell_cols
;
5805 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5806 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5810 m_currentCellCoords
.SetRow( row
);
5811 m_currentCellCoords
.SetCol( col
);
5814 // erase the highlight and the cell contents because the editor
5815 // might not cover the entire cell
5816 wxClientDC
dc( m_gridWin
);
5818 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5819 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5820 dc
.SetPen(*wxTRANSPARENT_PEN
);
5821 dc
.DrawRectangle(rect
);
5823 // convert to scrolled coords
5824 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5830 // cell is shifted by one pixel
5831 // However, don't allow x or y to become negative
5832 // since the SetSize() method interprets that as
5839 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5840 if ( !editor
->IsCreated() )
5842 editor
->Create(m_gridWin
, wxID_ANY
,
5843 new wxGridCellEditorEvtHandler(this, editor
));
5845 wxGridEditorCreatedEvent
evt(GetId(),
5846 wxEVT_GRID_EDITOR_CREATED
,
5850 editor
->GetControl());
5851 GetEventHandler()->ProcessEvent(evt
);
5854 // resize editor to overflow into righthand cells if allowed
5855 int maxWidth
= rect
.width
;
5856 wxString value
= GetCellValue(row
, col
);
5857 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
5860 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
5861 if (maxWidth
< rect
.width
)
5862 maxWidth
= rect
.width
;
5865 int client_right
= m_gridWin
->GetClientSize().GetWidth();
5866 if (rect
.x
+ maxWidth
> client_right
)
5867 maxWidth
= client_right
- rect
.x
;
5869 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
5871 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5872 // may have changed earlier
5873 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
5876 GetCellSize( row
, i
, &c_rows
, &c_cols
);
5878 // looks weird going over a multicell
5879 if (m_table
->IsEmptyCell( row
, i
) &&
5880 (rect
.width
< maxWidth
) && (c_rows
== 1))
5882 rect
.width
+= GetColWidth( i
);
5888 if (rect
.GetRight() > client_right
)
5889 rect
.SetRight( client_right
- 1 );
5892 editor
->SetCellAttr( attr
);
5893 editor
->SetSize( rect
);
5895 editor
->GetControl()->Move(
5896 editor
->GetControl()->GetPosition().x
+ nXMove
,
5897 editor
->GetControl()->GetPosition().y
);
5898 editor
->Show( true, attr
);
5900 // recalc dimensions in case we need to
5901 // expand the scrolled window to account for editor
5904 editor
->BeginEdit(row
, col
, this);
5905 editor
->SetCellAttr(NULL
);
5913 void wxGrid::HideCellEditControl()
5915 if ( IsCellEditControlEnabled() )
5917 int row
= m_currentCellCoords
.GetRow();
5918 int col
= m_currentCellCoords
.GetCol();
5920 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
5921 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5922 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
5923 editor
->Show( false );
5927 // return the focus to the grid itself if the editor had it
5929 // note that we must not do this unconditionally to avoid stealing
5930 // focus from the window which just received it if we are hiding the
5931 // editor precisely because we lost focus
5932 if ( editorHadFocus
)
5933 m_gridWin
->SetFocus();
5935 // refresh whole row to the right
5936 wxRect
rect( CellToRect(row
, col
) );
5937 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5938 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
5941 // ensure that the pixels under the focus ring get refreshed as well
5942 rect
.Inflate(10, 10);
5945 m_gridWin
->Refresh( false, &rect
);
5949 void wxGrid::SaveEditControlValue()
5951 if ( IsCellEditControlEnabled() )
5953 int row
= m_currentCellCoords
.GetRow();
5954 int col
= m_currentCellCoords
.GetCol();
5956 wxString oldval
= GetCellValue(row
, col
);
5958 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5959 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5962 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
5964 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
5966 editor
->ApplyEdit(row
, col
, this);
5968 // for compatibility reasons dating back to wx 2.8 when this event
5969 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
5970 // didn't exist we allow vetoing this one too
5971 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
5973 // Event has been vetoed, set the data back.
5974 SetCellValue(row
, col
, oldval
);
5984 // ------ Grid location functions
5985 // Note that all of these functions work with the logical coordinates of
5986 // grid cells and labels so you will need to convert from device
5987 // coordinates for mouse events etc.
5990 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
5992 int row
= YToRow(y
);
5993 int col
= XToCol(x
);
5995 return row
== -1 || col
== -1 ? wxGridNoCellCoords
5996 : wxGridCellCoords(row
, col
);
5999 // compute row or column from some (unscrolled) coordinate value, using either
6000 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6001 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6003 int wxGrid::PosToLinePos(int coord
,
6005 const wxGridOperations
& oper
) const
6007 const int numLines
= oper
.GetNumberOfLines(this);
6010 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6012 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6013 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6015 int maxPos
= coord
/ defaultLineSize
,
6018 // check for the simplest case: if we have no explicit line sizes
6019 // configured, then we already know the line this position falls in
6020 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6021 if ( lineEnds
.empty() )
6023 if ( maxPos
< numLines
)
6026 return clipToMinMax
? numLines
- 1 : -1;
6030 // adjust maxPos before starting the binary search
6031 if ( maxPos
>= numLines
)
6033 maxPos
= numLines
- 1;
6037 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6040 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6042 maxPos
= coord
/ minDist
;
6044 maxPos
= numLines
- 1;
6047 if ( maxPos
>= numLines
)
6048 maxPos
= numLines
- 1;
6051 // check if the position is beyond the last column
6052 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6053 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6054 return clipToMinMax
? maxPos
: -1;
6056 // or before the first one
6057 const int lineAt0
= oper
.GetLineAt(this, 0);
6058 if ( coord
< lineEnds
[lineAt0
] )
6062 // finally do perform the binary search
6063 while ( minPos
< maxPos
)
6065 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6066 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6068 "wxGrid: internal error in PosToLinePos()" );
6070 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6075 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6076 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6086 wxGrid::PosToLine(int coord
,
6088 const wxGridOperations
& oper
) const
6090 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6092 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6095 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6097 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6100 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6102 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6105 int wxGrid::XToPos(int x
) const
6107 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6110 // return the row number such that the y coord is near the edge of, or -1 if
6111 // not near an edge.
6113 // notice that position can only possibly be near an edge if the row/column is
6114 // large enough to still allow for an "inner" area that is _not_ near the edge
6115 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6116 // _never_ be considered to be near the edge).
6117 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6119 const int line
= oper
.PosToLine(this, pos
, true);
6121 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6123 // We know that we are in this line, test whether we are close enough
6124 // to start or end border, respectively.
6125 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6127 else if ( line
> 0 &&
6128 pos
- oper
.GetLineStartPos(this,
6129 line
) < WXGRID_LABEL_EDGE_ZONE
)
6136 int wxGrid::YToEdgeOfRow(int y
) const
6138 return PosToEdgeOfLine(y
, wxGridRowOperations());
6141 int wxGrid::XToEdgeOfCol(int x
) const
6143 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6146 wxRect
wxGrid::CellToRect( int row
, int col
) const
6148 wxRect
rect( -1, -1, -1, -1 );
6150 if ( row
>= 0 && row
< m_numRows
&&
6151 col
>= 0 && col
< m_numCols
)
6153 int i
, cell_rows
, cell_cols
;
6154 rect
.width
= rect
.height
= 0;
6155 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6156 // if negative then find multicell owner
6161 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6163 rect
.x
= GetColLeft(col
);
6164 rect
.y
= GetRowTop(row
);
6165 for (i
=col
; i
< col
+ cell_cols
; i
++)
6166 rect
.width
+= GetColWidth(i
);
6167 for (i
=row
; i
< row
+ cell_rows
; i
++)
6168 rect
.height
+= GetRowHeight(i
);
6170 // if grid lines are enabled, then the area of the cell is a bit smaller
6171 if (m_gridLinesEnabled
)
6181 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6183 // get the cell rectangle in logical coords
6185 wxRect
r( CellToRect( row
, col
) );
6187 // convert to device coords
6189 int left
, top
, right
, bottom
;
6190 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6191 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6193 // check against the client area of the grid window
6195 m_gridWin
->GetClientSize( &cw
, &ch
);
6197 if ( wholeCellVisible
)
6199 // is the cell wholly visible ?
6200 return ( left
>= 0 && right
<= cw
&&
6201 top
>= 0 && bottom
<= ch
);
6205 // is the cell partly visible ?
6207 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6208 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6212 // make the specified cell location visible by doing a minimal amount
6215 void wxGrid::MakeCellVisible( int row
, int col
)
6218 int xpos
= -1, ypos
= -1;
6220 if ( row
>= 0 && row
< m_numRows
&&
6221 col
>= 0 && col
< m_numCols
)
6223 // get the cell rectangle in logical coords
6224 wxRect
r( CellToRect( row
, col
) );
6226 // convert to device coords
6227 int left
, top
, right
, bottom
;
6228 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6229 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6232 m_gridWin
->GetClientSize( &cw
, &ch
);
6238 else if ( bottom
> ch
)
6240 int h
= r
.GetHeight();
6242 for ( i
= row
- 1; i
>= 0; i
-- )
6244 int rowHeight
= GetRowHeight(i
);
6245 if ( h
+ rowHeight
> ch
)
6252 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6253 // have rounding errors (this is important, because if we do,
6254 // we might not scroll at all and some cells won't be redrawn)
6256 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6257 // so just add a full scroll unit...
6258 ypos
+= m_scrollLineY
;
6261 // special handling for wide cells - show always left part of the cell!
6262 // Otherwise, e.g. when stepping from row to row, it would jump between
6263 // left and right part of the cell on every step!
6265 if ( left
< 0 || (right
- left
) >= cw
)
6269 else if ( right
> cw
)
6271 // position the view so that the cell is on the right
6273 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6274 xpos
= x0
+ (right
- cw
);
6276 // see comment for ypos above
6277 xpos
+= m_scrollLineX
;
6280 if ( xpos
!= -1 || ypos
!= -1 )
6283 xpos
/= m_scrollLineX
;
6285 ypos
/= m_scrollLineY
;
6286 Scroll( xpos
, ypos
);
6293 // ------ Grid cursor movement functions
6297 wxGrid::DoMoveCursor(bool expandSelection
,
6298 const wxGridDirectionOperations
& diroper
)
6300 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6303 if ( expandSelection
)
6305 wxGridCellCoords coords
= m_selectedBlockCorner
;
6306 if ( coords
== wxGridNoCellCoords
)
6307 coords
= m_currentCellCoords
;
6309 if ( diroper
.IsAtBoundary(coords
) )
6312 diroper
.Advance(coords
);
6314 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6316 else // don't expand selection
6320 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6323 wxGridCellCoords coords
= m_currentCellCoords
;
6324 diroper
.Advance(coords
);
6332 bool wxGrid::MoveCursorUp(bool expandSelection
)
6334 return DoMoveCursor(expandSelection
,
6335 wxGridBackwardOperations(this, wxGridRowOperations()));
6338 bool wxGrid::MoveCursorDown(bool expandSelection
)
6340 return DoMoveCursor(expandSelection
,
6341 wxGridForwardOperations(this, wxGridRowOperations()));
6344 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6346 return DoMoveCursor(expandSelection
,
6347 wxGridBackwardOperations(this, wxGridColumnOperations()));
6350 bool wxGrid::MoveCursorRight(bool expandSelection
)
6352 return DoMoveCursor(expandSelection
,
6353 wxGridForwardOperations(this, wxGridColumnOperations()));
6356 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6358 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6361 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6364 const int oldRow
= m_currentCellCoords
.GetRow();
6365 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6366 if ( newRow
== oldRow
)
6368 wxGridCellCoords
coords(m_currentCellCoords
);
6369 diroper
.Advance(coords
);
6370 newRow
= coords
.GetRow();
6373 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6378 bool wxGrid::MovePageUp()
6380 return DoMoveCursorByPage(
6381 wxGridBackwardOperations(this, wxGridRowOperations()));
6384 bool wxGrid::MovePageDown()
6386 return DoMoveCursorByPage(
6387 wxGridForwardOperations(this, wxGridRowOperations()));
6390 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6391 // until we find a non-empty cell or reach the grid end
6393 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6394 const wxGridDirectionOperations
& diroper
)
6396 while ( !diroper
.IsAtBoundary(coords
) )
6398 diroper
.Advance(coords
);
6399 if ( !m_table
->IsEmpty(coords
) )
6405 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6406 const wxGridDirectionOperations
& diroper
)
6408 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6411 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6414 wxGridCellCoords
coords(m_currentCellCoords
);
6415 if ( m_table
->IsEmpty(coords
) )
6417 // we are in an empty cell: find the next block of non-empty cells
6418 AdvanceToNextNonEmpty(coords
, diroper
);
6420 else // current cell is not empty
6422 diroper
.Advance(coords
);
6423 if ( m_table
->IsEmpty(coords
) )
6425 // we started at the end of a block, find the next one
6426 AdvanceToNextNonEmpty(coords
, diroper
);
6428 else // we're in a middle of a block
6430 // go to the end of it, i.e. find the last cell before the next
6432 while ( !diroper
.IsAtBoundary(coords
) )
6434 wxGridCellCoords
coordsNext(coords
);
6435 diroper
.Advance(coordsNext
);
6436 if ( m_table
->IsEmpty(coordsNext
) )
6439 coords
= coordsNext
;
6444 if ( expandSelection
)
6446 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6457 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6459 return DoMoveCursorByBlock(
6461 wxGridBackwardOperations(this, wxGridRowOperations())
6465 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6467 return DoMoveCursorByBlock(
6469 wxGridForwardOperations(this, wxGridRowOperations())
6473 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6475 return DoMoveCursorByBlock(
6477 wxGridBackwardOperations(this, wxGridColumnOperations())
6481 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6483 return DoMoveCursorByBlock(
6485 wxGridForwardOperations(this, wxGridColumnOperations())
6490 // ------ Label values and formatting
6493 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6496 *horiz
= m_rowLabelHorizAlign
;
6498 *vert
= m_rowLabelVertAlign
;
6501 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6504 *horiz
= m_colLabelHorizAlign
;
6506 *vert
= m_colLabelVertAlign
;
6509 int wxGrid::GetColLabelTextOrientation() const
6511 return m_colLabelTextOrientation
;
6514 wxString
wxGrid::GetRowLabelValue( int row
) const
6518 return m_table
->GetRowLabelValue( row
);
6528 wxString
wxGrid::GetColLabelValue( int col
) const
6532 return m_table
->GetColLabelValue( col
);
6542 void wxGrid::SetRowLabelSize( int width
)
6544 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6546 if ( width
== wxGRID_AUTOSIZE
)
6548 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6551 if ( width
!= m_rowLabelWidth
)
6555 m_rowLabelWin
->Show( false );
6556 m_cornerLabelWin
->Show( false );
6558 else if ( m_rowLabelWidth
== 0 )
6560 m_rowLabelWin
->Show( true );
6561 if ( m_colLabelHeight
> 0 )
6562 m_cornerLabelWin
->Show( true );
6565 m_rowLabelWidth
= width
;
6567 wxScrolledWindow::Refresh( true );
6571 void wxGrid::SetColLabelSize( int height
)
6573 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6575 if ( height
== wxGRID_AUTOSIZE
)
6577 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6580 if ( height
!= m_colLabelHeight
)
6584 m_colWindow
->Show( false );
6585 m_cornerLabelWin
->Show( false );
6587 else if ( m_colLabelHeight
== 0 )
6589 m_colWindow
->Show( true );
6590 if ( m_rowLabelWidth
> 0 )
6591 m_cornerLabelWin
->Show( true );
6594 m_colLabelHeight
= height
;
6596 wxScrolledWindow::Refresh( true );
6600 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6602 if ( m_labelBackgroundColour
!= colour
)
6604 m_labelBackgroundColour
= colour
;
6605 m_rowLabelWin
->SetBackgroundColour( colour
);
6606 m_colWindow
->SetBackgroundColour( colour
);
6607 m_cornerLabelWin
->SetBackgroundColour( colour
);
6609 if ( !GetBatchCount() )
6611 m_rowLabelWin
->Refresh();
6612 m_colWindow
->Refresh();
6613 m_cornerLabelWin
->Refresh();
6618 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6620 if ( m_labelTextColour
!= colour
)
6622 m_labelTextColour
= colour
;
6623 if ( !GetBatchCount() )
6625 m_rowLabelWin
->Refresh();
6626 m_colWindow
->Refresh();
6631 void wxGrid::SetLabelFont( const wxFont
& font
)
6634 if ( !GetBatchCount() )
6636 m_rowLabelWin
->Refresh();
6637 m_colWindow
->Refresh();
6641 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6643 // allow old (incorrect) defs to be used
6646 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6647 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6648 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6653 case wxTOP
: vert
= wxALIGN_TOP
; break;
6654 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6655 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6658 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6660 m_rowLabelHorizAlign
= horiz
;
6663 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6665 m_rowLabelVertAlign
= vert
;
6668 if ( !GetBatchCount() )
6670 m_rowLabelWin
->Refresh();
6674 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6676 // allow old (incorrect) defs to be used
6679 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6680 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6681 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6686 case wxTOP
: vert
= wxALIGN_TOP
; break;
6687 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6688 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6691 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6693 m_colLabelHorizAlign
= horiz
;
6696 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6698 m_colLabelVertAlign
= vert
;
6701 if ( !GetBatchCount() )
6703 m_colWindow
->Refresh();
6707 // Note: under MSW, the default column label font must be changed because it
6708 // does not support vertical printing
6710 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6711 // pGrid->SetLabelFont(font);
6712 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6714 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6716 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6717 m_colLabelTextOrientation
= textOrientation
;
6719 if ( !GetBatchCount() )
6720 m_colWindow
->Refresh();
6723 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6727 m_table
->SetRowLabelValue( row
, s
);
6728 if ( !GetBatchCount() )
6730 wxRect rect
= CellToRect( row
, 0 );
6731 if ( rect
.height
> 0 )
6733 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6735 rect
.width
= m_rowLabelWidth
;
6736 m_rowLabelWin
->Refresh( true, &rect
);
6742 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6746 m_table
->SetColLabelValue( col
, s
);
6747 if ( !GetBatchCount() )
6749 if ( m_useNativeHeader
)
6751 GetGridColHeader()->UpdateColumn(col
);
6755 wxRect rect
= CellToRect( 0, col
);
6756 if ( rect
.width
> 0 )
6758 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6760 rect
.height
= m_colLabelHeight
;
6761 GetColLabelWindow()->Refresh( true, &rect
);
6768 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6770 if ( m_gridLineColour
!= colour
)
6772 m_gridLineColour
= colour
;
6774 if ( GridLinesEnabled() )
6779 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6781 if ( m_cellHighlightColour
!= colour
)
6783 m_cellHighlightColour
= colour
;
6785 wxClientDC
dc( m_gridWin
);
6787 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6788 DrawCellHighlight(dc
, attr
);
6793 void wxGrid::SetCellHighlightPenWidth(int width
)
6795 if (m_cellHighlightPenWidth
!= width
)
6797 m_cellHighlightPenWidth
= width
;
6799 // Just redrawing the cell highlight is not enough since that won't
6800 // make any visible change if the the thickness is getting smaller.
6801 int row
= m_currentCellCoords
.GetRow();
6802 int col
= m_currentCellCoords
.GetCol();
6803 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6806 wxRect rect
= CellToRect(row
, col
);
6807 m_gridWin
->Refresh(true, &rect
);
6811 void wxGrid::SetCellHighlightROPenWidth(int width
)
6813 if (m_cellHighlightROPenWidth
!= width
)
6815 m_cellHighlightROPenWidth
= width
;
6817 // Just redrawing the cell highlight is not enough since that won't
6818 // make any visible change if the the thickness is getting smaller.
6819 int row
= m_currentCellCoords
.GetRow();
6820 int col
= m_currentCellCoords
.GetCol();
6821 if ( row
== -1 || col
== -1 ||
6822 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6825 wxRect rect
= CellToRect(row
, col
);
6826 m_gridWin
->Refresh(true, &rect
);
6830 void wxGrid::RedrawGridLines()
6832 // the lines will be redrawn when the window is thawn
6833 if ( GetBatchCount() )
6836 if ( GridLinesEnabled() )
6838 wxClientDC
dc( m_gridWin
);
6840 DrawAllGridLines( dc
, wxRegion() );
6842 else // remove the grid lines
6844 m_gridWin
->Refresh();
6848 void wxGrid::EnableGridLines( bool enable
)
6850 if ( enable
!= m_gridLinesEnabled
)
6852 m_gridLinesEnabled
= enable
;
6858 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
6864 if ( GridLinesEnabled() )
6869 int wxGrid::GetDefaultRowSize() const
6871 return m_defaultRowHeight
;
6874 int wxGrid::GetRowSize( int row
) const
6876 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6878 return GetRowHeight(row
);
6881 int wxGrid::GetDefaultColSize() const
6883 return m_defaultColWidth
;
6886 int wxGrid::GetColSize( int col
) const
6888 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6890 return GetColWidth(col
);
6893 // ============================================================================
6894 // access to the grid attributes: each of them has a default value in the grid
6895 // itself and may be overidden on a per-cell basis
6896 // ============================================================================
6898 // ----------------------------------------------------------------------------
6899 // setting default attributes
6900 // ----------------------------------------------------------------------------
6902 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6904 m_defaultCellAttr
->SetBackgroundColour(col
);
6906 m_gridWin
->SetBackgroundColour(col
);
6910 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6912 m_defaultCellAttr
->SetTextColour(col
);
6915 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6917 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6920 void wxGrid::SetDefaultCellOverflow( bool allow
)
6922 m_defaultCellAttr
->SetOverflow(allow
);
6925 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6927 m_defaultCellAttr
->SetFont(font
);
6930 // For editors and renderers the type registry takes precedence over the
6931 // default attr, so we need to register the new editor/renderer for the string
6932 // data type in order to make setting a default editor/renderer appear to
6935 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6937 RegisterDataType(wxGRID_VALUE_STRING
,
6939 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
6942 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6944 RegisterDataType(wxGRID_VALUE_STRING
,
6945 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
6949 // ----------------------------------------------------------------------------
6950 // access to the default attributes
6951 // ----------------------------------------------------------------------------
6953 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
6955 return m_defaultCellAttr
->GetBackgroundColour();
6958 wxColour
wxGrid::GetDefaultCellTextColour() const
6960 return m_defaultCellAttr
->GetTextColour();
6963 wxFont
wxGrid::GetDefaultCellFont() const
6965 return m_defaultCellAttr
->GetFont();
6968 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
6970 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6973 bool wxGrid::GetDefaultCellOverflow() const
6975 return m_defaultCellAttr
->GetOverflow();
6978 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6980 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
6983 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6985 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
6988 // ----------------------------------------------------------------------------
6989 // access to cell attributes
6990 // ----------------------------------------------------------------------------
6992 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
6994 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6995 wxColour colour
= attr
->GetBackgroundColour();
7001 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7003 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7004 wxColour colour
= attr
->GetTextColour();
7010 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7012 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7013 wxFont font
= attr
->GetFont();
7019 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7021 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7022 attr
->GetAlignment(horiz
, vert
);
7026 bool wxGrid::GetCellOverflow( int row
, int col
) const
7028 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7029 bool allow
= attr
->GetOverflow();
7035 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7037 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7038 attr
->GetSize( num_rows
, num_cols
);
7042 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7044 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7045 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7051 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7053 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7054 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7060 bool wxGrid::IsReadOnly(int row
, int col
) const
7062 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7063 bool isReadOnly
= attr
->IsReadOnly();
7069 // ----------------------------------------------------------------------------
7070 // attribute support: cache, automatic provider creation, ...
7071 // ----------------------------------------------------------------------------
7073 bool wxGrid::CanHaveAttributes() const
7080 return m_table
->CanHaveAttributes();
7083 void wxGrid::ClearAttrCache()
7085 if ( m_attrCache
.row
!= -1 )
7087 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7088 m_attrCache
.attr
= NULL
;
7089 m_attrCache
.row
= -1;
7090 // wxSafeDecRec(...) might cause event processing that accesses
7091 // the cached attribute, if one exists (e.g. by deleting the
7092 // editor stored within the attribute). Therefore it is important
7093 // to invalidate the cache before calling wxSafeDecRef!
7094 wxSafeDecRef(oldAttr
);
7098 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7102 wxGrid
*self
= (wxGrid
*)this; // const_cast
7104 self
->ClearAttrCache();
7105 self
->m_attrCache
.row
= row
;
7106 self
->m_attrCache
.col
= col
;
7107 self
->m_attrCache
.attr
= attr
;
7112 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7114 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7116 *attr
= m_attrCache
.attr
;
7117 wxSafeIncRef(m_attrCache
.attr
);
7119 #ifdef DEBUG_ATTR_CACHE
7120 gs_nAttrCacheHits
++;
7127 #ifdef DEBUG_ATTR_CACHE
7128 gs_nAttrCacheMisses
++;
7135 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7137 wxGridCellAttr
*attr
= NULL
;
7138 // Additional test to avoid looking at the cache e.g. for
7139 // wxNoCellCoords, as this will confuse memory management.
7142 if ( !LookupAttr(row
, col
, &attr
) )
7144 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7146 CacheAttr(row
, col
, attr
);
7152 attr
->SetDefAttr(m_defaultCellAttr
);
7156 attr
= m_defaultCellAttr
;
7163 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7165 wxGridCellAttr
*attr
= NULL
;
7166 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7168 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
7169 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
7171 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7174 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7176 // artificially inc the ref count to match DecRef() in caller
7178 m_table
->SetAttr(attr
, row
, col
);
7184 // ----------------------------------------------------------------------------
7185 // setting column attributes (wrappers around SetColAttr)
7186 // ----------------------------------------------------------------------------
7188 void wxGrid::SetColFormatBool(int col
)
7190 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7193 void wxGrid::SetColFormatNumber(int col
)
7195 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7198 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7200 wxString typeName
= wxGRID_VALUE_FLOAT
;
7201 if ( (width
!= -1) || (precision
!= -1) )
7203 typeName
<< _T(':') << width
<< _T(',') << precision
;
7206 SetColFormatCustom(col
, typeName
);
7209 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7211 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7213 attr
= new wxGridCellAttr
;
7214 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7215 attr
->SetRenderer(renderer
);
7216 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7217 attr
->SetEditor(editor
);
7219 SetColAttr(col
, attr
);
7223 // ----------------------------------------------------------------------------
7224 // setting cell attributes: this is forwarded to the table
7225 // ----------------------------------------------------------------------------
7227 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7229 if ( CanHaveAttributes() )
7231 m_table
->SetAttr(attr
, row
, col
);
7240 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7242 if ( CanHaveAttributes() )
7244 m_table
->SetRowAttr(attr
, row
);
7253 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7255 if ( CanHaveAttributes() )
7257 m_table
->SetColAttr(attr
, col
);
7266 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7268 if ( CanHaveAttributes() )
7270 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7271 attr
->SetBackgroundColour(colour
);
7276 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7278 if ( CanHaveAttributes() )
7280 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7281 attr
->SetTextColour(colour
);
7286 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7288 if ( CanHaveAttributes() )
7290 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7291 attr
->SetFont(font
);
7296 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7298 if ( CanHaveAttributes() )
7300 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7301 attr
->SetAlignment(horiz
, vert
);
7306 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7308 if ( CanHaveAttributes() )
7310 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7311 attr
->SetOverflow(allow
);
7316 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7318 if ( CanHaveAttributes() )
7320 int cell_rows
, cell_cols
;
7322 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7323 attr
->GetSize(&cell_rows
, &cell_cols
);
7324 attr
->SetSize(num_rows
, num_cols
);
7327 // Cannot set the size of a cell to 0 or negative values
7328 // While it is perfectly legal to do that, this function cannot
7329 // handle all the possibilies, do it by hand by getting the CellAttr.
7330 // You can only set the size of a cell to 1,1 or greater with this fn
7331 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7332 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7333 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7334 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7336 // if this was already a multicell then "turn off" the other cells first
7337 if ((cell_rows
> 1) || (cell_cols
> 1))
7340 for (j
=row
; j
< row
+ cell_rows
; j
++)
7342 for (i
=col
; i
< col
+ cell_cols
; i
++)
7344 if ((i
!= col
) || (j
!= row
))
7346 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7347 attr_stub
->SetSize( 1, 1 );
7348 attr_stub
->DecRef();
7354 // mark the cells that will be covered by this cell to
7355 // negative or zero values to point back at this cell
7356 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7359 for (j
=row
; j
< row
+ num_rows
; j
++)
7361 for (i
=col
; i
< col
+ num_cols
; i
++)
7363 if ((i
!= col
) || (j
!= row
))
7365 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7366 attr_stub
->SetSize( row
- j
, col
- i
);
7367 attr_stub
->DecRef();
7375 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7377 if ( CanHaveAttributes() )
7379 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7380 attr
->SetRenderer(renderer
);
7385 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7387 if ( CanHaveAttributes() )
7389 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7390 attr
->SetEditor(editor
);
7395 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7397 if ( CanHaveAttributes() )
7399 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7400 attr
->SetReadOnly(isReadOnly
);
7405 // ----------------------------------------------------------------------------
7406 // Data type registration
7407 // ----------------------------------------------------------------------------
7409 void wxGrid::RegisterDataType(const wxString
& typeName
,
7410 wxGridCellRenderer
* renderer
,
7411 wxGridCellEditor
* editor
)
7413 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7417 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7419 wxString typeName
= m_table
->GetTypeName(row
, col
);
7420 return GetDefaultEditorForType(typeName
);
7423 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7425 wxString typeName
= m_table
->GetTypeName(row
, col
);
7426 return GetDefaultRendererForType(typeName
);
7429 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7431 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7432 if ( index
== wxNOT_FOUND
)
7434 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7439 return m_typeRegistry
->GetEditor(index
);
7442 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7444 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7445 if ( index
== wxNOT_FOUND
)
7447 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7452 return m_typeRegistry
->GetRenderer(index
);
7455 // ----------------------------------------------------------------------------
7457 // ----------------------------------------------------------------------------
7459 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7463 setFixed
= new wxGridFixedIndicesSet
;
7466 setFixed
->insert(line
);
7470 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7472 return !setFixed
|| !setFixed
->count(line
);
7475 void wxGrid::EnableDragRowSize( bool enable
)
7477 m_canDragRowSize
= enable
;
7480 void wxGrid::EnableDragColSize( bool enable
)
7482 m_canDragColSize
= enable
;
7485 void wxGrid::EnableDragGridSize( bool enable
)
7487 m_canDragGridSize
= enable
;
7490 void wxGrid::EnableDragCell( bool enable
)
7492 m_canDragCell
= enable
;
7495 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7497 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7499 if ( resizeExistingRows
)
7501 // since we are resizing all rows to the default row size,
7502 // we can simply clear the row heights and row bottoms
7503 // arrays (which also allows us to take advantage of
7504 // some speed optimisations)
7505 m_rowHeights
.Empty();
7506 m_rowBottoms
.Empty();
7507 if ( !GetBatchCount() )
7512 void wxGrid::SetRowSize( int row
, int height
)
7514 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7516 // if < 0 then calculate new height from label
7520 wxArrayString lines
;
7521 wxClientDC
dc(m_rowLabelWin
);
7522 dc
.SetFont(GetLabelFont());
7523 StringToLines(GetRowLabelValue( row
), lines
);
7524 GetTextBoxSize( dc
, lines
, &w
, &h
);
7525 //check that it is not less than the minimal height
7526 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7529 // See comment in SetColSize
7530 if ( height
< GetRowMinimalAcceptableHeight())
7533 if ( m_rowHeights
.IsEmpty() )
7535 // need to really create the array
7539 int h
= wxMax( 0, height
);
7540 int diff
= h
- m_rowHeights
[row
];
7542 m_rowHeights
[row
] = h
;
7543 for ( int i
= row
; i
< m_numRows
; i
++ )
7545 m_rowBottoms
[i
] += diff
;
7548 if ( !GetBatchCount() )
7552 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7554 // we dont allow zero default column width
7555 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7557 if ( resizeExistingCols
)
7559 // since we are resizing all columns to the default column size,
7560 // we can simply clear the col widths and col rights
7561 // arrays (which also allows us to take advantage of
7562 // some speed optimisations)
7563 m_colWidths
.Empty();
7564 m_colRights
.Empty();
7565 if ( !GetBatchCount() )
7570 void wxGrid::SetColSize( int col
, int width
)
7572 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7574 // if < 0 then calculate new width from label
7578 wxArrayString lines
;
7579 wxClientDC
dc(m_colWindow
);
7580 dc
.SetFont(GetLabelFont());
7581 StringToLines(GetColLabelValue(col
), lines
);
7582 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7583 GetTextBoxSize( dc
, lines
, &w
, &h
);
7585 GetTextBoxSize( dc
, lines
, &h
, &w
);
7587 //check that it is not less than the minimal width
7588 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7591 // we intentionally don't test whether the width is less than
7592 // GetColMinimalWidth() here but we do compare it with
7593 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7594 // #651) -- and we also always allow the width of 0 as it has the special
7595 // sense of hiding the column
7596 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7599 if ( m_colWidths
.IsEmpty() )
7601 // need to really create the array
7605 const int diff
= width
- m_colWidths
[col
];
7606 m_colWidths
[col
] = width
;
7607 if ( m_useNativeHeader
)
7608 GetGridColHeader()->UpdateColumn(col
);
7609 //else: will be refreshed when the header is redrawn
7611 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7613 m_colRights
[GetColAt(colPos
)] += diff
;
7616 if ( !GetBatchCount() )
7623 void wxGrid::SetColMinimalWidth( int col
, int width
)
7625 if (width
> GetColMinimalAcceptableWidth())
7627 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7628 m_colMinWidths
[key
] = width
;
7632 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7634 if (width
> GetRowMinimalAcceptableHeight())
7636 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7637 m_rowMinHeights
[key
] = width
;
7641 int wxGrid::GetColMinimalWidth(int col
) const
7643 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7644 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7646 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7649 int wxGrid::GetRowMinimalHeight(int row
) const
7651 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7652 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7654 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7657 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7659 // We do allow a width of 0 since this gives us
7660 // an easy way to temporarily hiding columns.
7662 m_minAcceptableColWidth
= width
;
7665 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7667 // We do allow a height of 0 since this gives us
7668 // an easy way to temporarily hiding rows.
7670 m_minAcceptableRowHeight
= height
;
7673 int wxGrid::GetColMinimalAcceptableWidth() const
7675 return m_minAcceptableColWidth
;
7678 int wxGrid::GetRowMinimalAcceptableHeight() const
7680 return m_minAcceptableRowHeight
;
7683 // ----------------------------------------------------------------------------
7685 // ----------------------------------------------------------------------------
7688 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7690 const bool column
= direction
== wxGRID_COLUMN
;
7692 wxClientDC
dc(m_gridWin
);
7694 // cancel editing of cell
7695 HideCellEditControl();
7696 SaveEditControlValue();
7698 // init both of them to avoid compiler warnings, even if we only need one
7706 wxCoord extent
, extentMax
= 0;
7707 int max
= column
? m_numRows
: m_numCols
;
7708 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7715 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7716 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7719 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7720 extent
= column
? size
.x
: size
.y
;
7721 if ( extent
> extentMax
)
7730 // now also compare with the column label extent
7732 dc
.SetFont( GetLabelFont() );
7736 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7737 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7741 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7743 extent
= column
? w
: h
;
7744 if ( extent
> extentMax
)
7749 // empty column - give default extent (notice that if extentMax is less
7750 // than default extent but != 0, it's OK)
7751 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7756 // leave some space around text
7764 // Ensure automatic width is not less than minimal width. See the
7765 // comment in SetColSize() for explanation of why this isn't done
7768 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7770 SetColSize( col
, extentMax
);
7771 if ( !GetBatchCount() )
7773 if ( m_useNativeHeader
)
7775 GetGridColHeader()->UpdateColumn(col
);
7780 m_gridWin
->GetClientSize( &cw
, &ch
);
7781 wxRect
rect ( CellToRect( 0, col
) );
7783 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7784 rect
.width
= cw
- rect
.x
;
7785 rect
.height
= m_colLabelHeight
;
7786 GetColLabelWindow()->Refresh( true, &rect
);
7792 // Ensure automatic width is not less than minimal height. See the
7793 // comment in SetColSize() for explanation of why this isn't done
7796 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7798 SetRowSize(row
, extentMax
);
7799 if ( !GetBatchCount() )
7802 m_gridWin
->GetClientSize( &cw
, &ch
);
7803 wxRect
rect( CellToRect( row
, 0 ) );
7805 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
7806 rect
.width
= m_rowLabelWidth
;
7807 rect
.height
= ch
- rect
.y
;
7808 m_rowLabelWin
->Refresh( true, &rect
);
7815 SetColMinimalWidth(col
, extentMax
);
7817 SetRowMinimalHeight(row
, extentMax
);
7821 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
7823 // calculate size for the rows or columns?
7824 const bool calcRows
= direction
== wxGRID_ROW
;
7826 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
7827 : GetGridColLabelWindow());
7828 dc
.SetFont(GetLabelFont());
7830 // which dimension should we take into account for calculations?
7832 // for columns, the text can be only horizontal so it's easy but for rows
7833 // we also have to take into account the text orientation
7835 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
7837 wxArrayString lines
;
7838 wxCoord extentMax
= 0;
7840 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
7841 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
7845 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
7846 : GetColLabelValue(rowOrCol
);
7847 StringToLines(label
, lines
);
7850 GetTextBoxSize(dc
, lines
, &w
, &h
);
7852 const wxCoord extent
= useWidth
? w
: h
;
7853 if ( extent
> extentMax
)
7859 // empty column - give default extent (notice that if extentMax is less
7860 // than default extent but != 0, it's OK)
7861 extentMax
= calcRows
? GetDefaultRowLabelSize()
7862 : GetDefaultColLabelSize();
7865 // leave some space around text (taken from AutoSizeColOrRow)
7874 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7876 int width
= m_rowLabelWidth
;
7878 wxGridUpdateLocker locker
;
7880 locker
.Create(this);
7882 for ( int col
= 0; col
< m_numCols
; col
++ )
7885 AutoSizeColumn(col
, setAsMin
);
7887 width
+= GetColWidth(col
);
7893 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7895 int height
= m_colLabelHeight
;
7897 wxGridUpdateLocker locker
;
7899 locker
.Create(this);
7901 for ( int row
= 0; row
< m_numRows
; row
++ )
7904 AutoSizeRow(row
, setAsMin
);
7906 height
+= GetRowHeight(row
);
7912 void wxGrid::AutoSize()
7914 wxGridUpdateLocker
locker(this);
7916 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
7917 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
7919 // we know that we're not going to have scrollbars so disable them now to
7920 // avoid trouble in SetClientSize() which can otherwise set the correct
7921 // client size but also leave space for (not needed any more) scrollbars
7922 SetScrollbars(0, 0, 0, 0, 0, 0, true);
7924 // restore the scroll rate parameters overwritten by SetScrollbars()
7925 SetScrollRate(m_scrollLineX
, m_scrollLineY
);
7927 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
7930 void wxGrid::AutoSizeRowLabelSize( int row
)
7932 // Hide the edit control, so it
7933 // won't interfere with drag-shrinking.
7934 if ( IsCellEditControlShown() )
7936 HideCellEditControl();
7937 SaveEditControlValue();
7940 // autosize row height depending on label text
7941 SetRowSize(row
, -1);
7945 void wxGrid::AutoSizeColLabelSize( int col
)
7947 // Hide the edit control, so it
7948 // won't interfere with drag-shrinking.
7949 if ( IsCellEditControlShown() )
7951 HideCellEditControl();
7952 SaveEditControlValue();
7955 // autosize column width depending on label text
7956 SetColSize(col
, -1);
7960 wxSize
wxGrid::DoGetBestSize() const
7962 wxGrid
*self
= (wxGrid
*)this; // const_cast
7964 // we do the same as in AutoSize() here with the exception that we don't
7965 // change the column/row sizes, only calculate them
7966 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
7967 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
7969 // NOTE: This size should be cached, but first we need to add calls to
7970 // InvalidateBestSize everywhere that could change the results of this
7972 // CacheBestSize(size);
7974 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
7975 + GetWindowBorderSize();
7983 wxPen
& wxGrid::GetDividerPen() const
7988 // ----------------------------------------------------------------------------
7989 // cell value accessor functions
7990 // ----------------------------------------------------------------------------
7992 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7996 m_table
->SetValue( row
, col
, s
);
7997 if ( !GetBatchCount() )
8000 wxRect
rect( CellToRect( row
, col
) );
8002 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8003 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8004 m_gridWin
->Refresh( false, &rect
);
8007 if ( m_currentCellCoords
.GetRow() == row
&&
8008 m_currentCellCoords
.GetCol() == col
&&
8009 IsCellEditControlShown())
8010 // Note: If we are using IsCellEditControlEnabled,
8011 // this interacts badly with calling SetCellValue from
8012 // an EVT_GRID_CELL_CHANGE handler.
8014 HideCellEditControl();
8015 ShowCellEditControl(); // will reread data from table
8020 // ----------------------------------------------------------------------------
8021 // block, row and column selection
8022 // ----------------------------------------------------------------------------
8024 void wxGrid::SelectRow( int row
, bool addToSelected
)
8029 if ( !addToSelected
)
8032 m_selection
->SelectRow(row
);
8035 void wxGrid::SelectCol( int col
, bool addToSelected
)
8040 if ( !addToSelected
)
8043 m_selection
->SelectCol(col
);
8046 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8052 if ( !addToSelected
)
8055 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8058 void wxGrid::SelectAll()
8060 if ( m_numRows
> 0 && m_numCols
> 0 )
8063 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8067 // ----------------------------------------------------------------------------
8068 // cell, row and col deselection
8069 // ----------------------------------------------------------------------------
8071 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8076 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8077 if ( mode
== oper
.GetSelectionMode() ||
8078 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8080 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8081 if ( m_selection
->IsInSelection(c
) )
8082 m_selection
->ToggleCellSelection(c
);
8084 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8086 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8087 for ( int i
= 0; i
< nOther
; i
++ )
8089 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8090 if ( m_selection
->IsInSelection(c
) )
8091 m_selection
->ToggleCellSelection(c
);
8094 //else: can only select orthogonal lines so no lines in this direction
8095 // could have been selected anyhow
8098 void wxGrid::DeselectRow(int row
)
8100 DeselectLine(row
, wxGridRowOperations());
8103 void wxGrid::DeselectCol(int col
)
8105 DeselectLine(col
, wxGridColumnOperations());
8108 void wxGrid::DeselectCell( int row
, int col
)
8110 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8111 m_selection
->ToggleCellSelection(row
, col
);
8114 bool wxGrid::IsSelection() const
8116 return ( m_selection
&& (m_selection
->IsSelection() ||
8117 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8118 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8121 bool wxGrid::IsInSelection( int row
, int col
) const
8123 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8124 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8125 col
>= m_selectedBlockTopLeft
.GetCol() &&
8126 row
<= m_selectedBlockBottomRight
.GetRow() &&
8127 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8130 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8134 wxGridCellCoordsArray a
;
8138 return m_selection
->m_cellSelection
;
8141 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8145 wxGridCellCoordsArray a
;
8149 return m_selection
->m_blockSelectionTopLeft
;
8152 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8156 wxGridCellCoordsArray a
;
8160 return m_selection
->m_blockSelectionBottomRight
;
8163 wxArrayInt
wxGrid::GetSelectedRows() const
8171 return m_selection
->m_rowSelection
;
8174 wxArrayInt
wxGrid::GetSelectedCols() const
8182 return m_selection
->m_colSelection
;
8185 void wxGrid::ClearSelection()
8187 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8188 m_selectedBlockBottomRight
);
8189 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8190 m_selectedBlockCorner
);
8192 m_selectedBlockTopLeft
=
8193 m_selectedBlockBottomRight
=
8194 m_selectedBlockCorner
= wxGridNoCellCoords
;
8196 if ( !r1
.IsEmpty() )
8197 RefreshRect(r1
, false);
8198 if ( !r2
.IsEmpty() )
8199 RefreshRect(r2
, false);
8202 m_selection
->ClearSelection();
8205 // This function returns the rectangle that encloses the given block
8206 // in device coords clipped to the client size of the grid window.
8208 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8209 const wxGridCellCoords
& bottomRight
) const
8212 wxRect tempCellRect
= CellToRect(topLeft
);
8213 if ( tempCellRect
!= wxGridNoCellRect
)
8215 resultRect
= tempCellRect
;
8219 resultRect
= wxRect(0, 0, 0, 0);
8222 tempCellRect
= CellToRect(bottomRight
);
8223 if ( tempCellRect
!= wxGridNoCellRect
)
8225 resultRect
+= tempCellRect
;
8229 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8230 return wxGridNoCellRect
;
8233 // Ensure that left/right and top/bottom pairs are in order.
8234 int left
= resultRect
.GetLeft();
8235 int top
= resultRect
.GetTop();
8236 int right
= resultRect
.GetRight();
8237 int bottom
= resultRect
.GetBottom();
8239 int leftCol
= topLeft
.GetCol();
8240 int topRow
= topLeft
.GetRow();
8241 int rightCol
= bottomRight
.GetCol();
8242 int bottomRow
= bottomRight
.GetRow();
8266 // The following loop is ONLY necessary to detect and handle merged cells.
8268 m_gridWin
->GetClientSize( &cw
, &ch
);
8270 // Get the origin coordinates: notice that they will be negative if the
8271 // grid is scrolled downwards/to the right.
8272 int gridOriginX
= 0;
8273 int gridOriginY
= 0;
8274 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8276 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8277 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8279 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8280 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8282 // Bound our loop so that we only examine the portion of the selected block
8283 // that is shown on screen. Therefore, we compare the Top-Left block values
8284 // to the Top-Left screen values, and the Bottom-Right block values to the
8285 // Bottom-Right screen values, choosing appropriately.
8286 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8287 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8288 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8289 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8291 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8293 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8295 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8296 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8298 tempCellRect
= CellToRect( j
, i
);
8300 if (tempCellRect
.x
< left
)
8301 left
= tempCellRect
.x
;
8302 if (tempCellRect
.y
< top
)
8303 top
= tempCellRect
.y
;
8304 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8305 right
= tempCellRect
.x
+ tempCellRect
.width
;
8306 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8307 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8311 i
= visibleRightCol
; // jump over inner cells.
8316 // Convert to scrolled coords
8317 CalcScrolledPosition( left
, top
, &left
, &top
);
8318 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8320 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8321 return wxRect(0,0,0,0);
8323 resultRect
.SetLeft( wxMax(0, left
) );
8324 resultRect
.SetTop( wxMax(0, top
) );
8325 resultRect
.SetRight( wxMin(cw
, right
) );
8326 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8331 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8332 const wxGridOperations
& oper
)
8335 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8336 const int numLines
= oper
.GetNumberOfLines(this);
8337 for ( int i
= 0; i
< numLines
; i
++ )
8339 int size
= sizeInfo
.GetSize(i
);
8340 if ( size
!= sizeInfo
.m_sizeDefault
)
8341 oper
.SetLineSize(this, i
, size
);
8346 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8348 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8351 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8353 DoSetSizes(sizeInfo
, wxGridRowOperations());
8356 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8358 m_sizeDefault
= defSize
;
8359 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8361 if ( allSizes
[i
] != defSize
)
8362 m_customSizes
[i
] = allSizes
[i
];
8366 int wxGridSizesInfo::GetSize(unsigned pos
) const
8368 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8370 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8373 // ----------------------------------------------------------------------------
8375 // ----------------------------------------------------------------------------
8377 #if wxUSE_DRAG_AND_DROP
8379 // this allow setting drop target directly on wxGrid
8380 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8382 GetGridWindow()->SetDropTarget(dropTarget
);
8385 #endif // wxUSE_DRAG_AND_DROP
8387 // ----------------------------------------------------------------------------
8388 // grid event classes
8389 // ----------------------------------------------------------------------------
8391 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8393 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8394 int row
, int col
, int x
, int y
, bool sel
,
8395 bool control
, bool shift
, bool alt
, bool meta
)
8396 : wxNotifyEvent( type
, id
),
8397 wxKeyboardState(control
, shift
, alt
, meta
)
8399 Init(row
, col
, x
, y
, sel
);
8401 SetEventObject(obj
);
8404 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8406 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8407 int rowOrCol
, int x
, int y
,
8408 bool control
, bool shift
, bool alt
, bool meta
)
8409 : wxNotifyEvent( type
, id
),
8410 wxKeyboardState(control
, shift
, alt
, meta
)
8412 Init(rowOrCol
, x
, y
);
8414 SetEventObject(obj
);
8418 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8420 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8421 const wxGridCellCoords
& topLeft
,
8422 const wxGridCellCoords
& bottomRight
,
8423 bool sel
, bool control
,
8424 bool shift
, bool alt
, bool meta
)
8425 : wxNotifyEvent( type
, id
),
8426 wxKeyboardState(control
, shift
, alt
, meta
)
8428 Init(topLeft
, bottomRight
, sel
);
8430 SetEventObject(obj
);
8434 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8436 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8437 wxObject
* obj
, int row
,
8438 int col
, wxControl
* ctrl
)
8439 : wxCommandEvent(type
, id
)
8441 SetEventObject(obj
);
8448 // ----------------------------------------------------------------------------
8449 // wxGridTypeRegistry
8450 // ----------------------------------------------------------------------------
8452 wxGridTypeRegistry::~wxGridTypeRegistry()
8454 size_t count
= m_typeinfo
.GetCount();
8455 for ( size_t i
= 0; i
< count
; i
++ )
8456 delete m_typeinfo
[i
];
8459 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8460 wxGridCellRenderer
* renderer
,
8461 wxGridCellEditor
* editor
)
8463 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8465 // is it already registered?
8466 int loc
= FindRegisteredDataType(typeName
);
8467 if ( loc
!= wxNOT_FOUND
)
8469 delete m_typeinfo
[loc
];
8470 m_typeinfo
[loc
] = info
;
8474 m_typeinfo
.Add(info
);
8478 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8480 size_t count
= m_typeinfo
.GetCount();
8481 for ( size_t i
= 0; i
< count
; i
++ )
8483 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8492 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8494 int index
= FindRegisteredDataType(typeName
);
8495 if ( index
== wxNOT_FOUND
)
8497 // check whether this is one of the standard ones, in which case
8498 // register it "on the fly"
8500 if ( typeName
== wxGRID_VALUE_STRING
)
8502 RegisterDataType(wxGRID_VALUE_STRING
,
8503 new wxGridCellStringRenderer
,
8504 new wxGridCellTextEditor
);
8507 #endif // wxUSE_TEXTCTRL
8509 if ( typeName
== wxGRID_VALUE_BOOL
)
8511 RegisterDataType(wxGRID_VALUE_BOOL
,
8512 new wxGridCellBoolRenderer
,
8513 new wxGridCellBoolEditor
);
8516 #endif // wxUSE_CHECKBOX
8518 if ( typeName
== wxGRID_VALUE_NUMBER
)
8520 RegisterDataType(wxGRID_VALUE_NUMBER
,
8521 new wxGridCellNumberRenderer
,
8522 new wxGridCellNumberEditor
);
8524 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8526 RegisterDataType(wxGRID_VALUE_FLOAT
,
8527 new wxGridCellFloatRenderer
,
8528 new wxGridCellFloatEditor
);
8531 #endif // wxUSE_TEXTCTRL
8533 if ( typeName
== wxGRID_VALUE_CHOICE
)
8535 RegisterDataType(wxGRID_VALUE_CHOICE
,
8536 new wxGridCellStringRenderer
,
8537 new wxGridCellChoiceEditor
);
8540 #endif // wxUSE_COMBOBOX
8545 // we get here only if just added the entry for this type, so return
8547 index
= m_typeinfo
.GetCount() - 1;
8553 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8555 int index
= FindDataType(typeName
);
8556 if ( index
== wxNOT_FOUND
)
8558 // the first part of the typename is the "real" type, anything after ':'
8559 // are the parameters for the renderer
8560 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
8561 if ( index
== wxNOT_FOUND
)
8566 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8567 wxGridCellRenderer
*rendererOld
= renderer
;
8568 renderer
= renderer
->Clone();
8569 rendererOld
->DecRef();
8571 wxGridCellEditor
*editor
= GetEditor(index
);
8572 wxGridCellEditor
*editorOld
= editor
;
8573 editor
= editor
->Clone();
8574 editorOld
->DecRef();
8576 // do it even if there are no parameters to reset them to defaults
8577 wxString params
= typeName
.AfterFirst(_T(':'));
8578 renderer
->SetParameters(params
);
8579 editor
->SetParameters(params
);
8581 // register the new typename
8582 RegisterDataType(typeName
, renderer
, editor
);
8584 // we just registered it, it's the last one
8585 index
= m_typeinfo
.GetCount() - 1;
8591 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8593 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8600 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8602 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8609 #endif // wxUSE_GRID