1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin, Santiago Palacios
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 - Replace use of wxINVERT with wxOverlay
16 - Make Begin/EndBatch() the same as the generic Freeze/Thaw()
17 - Review the column reordering code, it's a mess.
18 - Implement row reordering after dealing with the columns.
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
34 #include "wx/dcclient.h"
35 #include "wx/settings.h"
37 #include "wx/textctrl.h"
38 #include "wx/checkbox.h"
39 #include "wx/combobox.h"
40 #include "wx/valtext.h"
43 #include "wx/listbox.h"
46 #include "wx/textfile.h"
47 #include "wx/spinctrl.h"
48 #include "wx/tokenzr.h"
49 #include "wx/renderer.h"
50 #include "wx/headerctrl.h"
51 #include "wx/hashset.h"
53 #include "wx/generic/gridsel.h"
54 #include "wx/generic/gridctrl.h"
55 #include "wx/generic/grideditors.h"
56 #include "wx/generic/private/grid.h"
58 const char wxGridNameStr
[] = "grid";
60 #if defined(__WXMOTIF__)
61 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_MOTIF(identifier) identifier
66 #if defined(__WXGTK__)
67 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
69 #define WXUNUSED_GTK(identifier) identifier
72 // Required for wxIs... functions
75 WX_DECLARE_HASH_SET_WITH_DECL(int, wxIntegerHash
, wxIntegerEqual
,
76 wxGridFixedIndicesSet
, class WXDLLIMPEXP_ADV
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
86 //#define DEBUG_ATTR_CACHE
87 #ifdef DEBUG_ATTR_CACHE
88 static size_t gs_nAttrCacheHits
= 0;
89 static size_t gs_nAttrCacheMisses
= 0;
92 // this struct simply combines together the default header renderers
94 // as the renderers ctors are trivial, there is no problem with making them
96 struct DefaultHeaderRenderers
98 wxGridColumnHeaderRendererDefault colRenderer
;
99 wxGridRowHeaderRendererDefault rowRenderer
;
100 wxGridCornerHeaderRendererDefault cornerRenderer
;
101 } gs_defaultHeaderRenderers
;
103 } // anonymous namespace
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
110 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
116 const size_t GRID_SCROLL_LINE_X
= 15;
117 const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
119 // the size of hash tables used a bit everywhere (the max number of elements
120 // in these hash tables is the number of rows/columns)
121 const int GRID_HASH_SIZE
= 100;
123 // the minimal distance in pixels the mouse needs to move to start a drag
125 const int DRAG_SENSITIVITY
= 3;
127 } // anonymous namespace
129 #include "wx/arrimpl.cpp"
131 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
132 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_CLICK
, wxGridEvent
);
139 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_CLICK
, wxGridEvent
);
140 wxDEFINE_EVENT( wxEVT_GRID_CELL_LEFT_DCLICK
, wxGridEvent
);
141 wxDEFINE_EVENT( wxEVT_GRID_CELL_RIGHT_DCLICK
, wxGridEvent
);
142 wxDEFINE_EVENT( wxEVT_GRID_CELL_BEGIN_DRAG
, wxGridEvent
);
143 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_CLICK
, wxGridEvent
);
144 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_CLICK
, wxGridEvent
);
145 wxDEFINE_EVENT( wxEVT_GRID_LABEL_LEFT_DCLICK
, wxGridEvent
);
146 wxDEFINE_EVENT( wxEVT_GRID_LABEL_RIGHT_DCLICK
, wxGridEvent
);
147 wxDEFINE_EVENT( wxEVT_GRID_ROW_SIZE
, wxGridSizeEvent
);
148 wxDEFINE_EVENT( wxEVT_GRID_COL_SIZE
, wxGridSizeEvent
);
149 wxDEFINE_EVENT( wxEVT_GRID_COL_MOVE
, wxGridEvent
);
150 wxDEFINE_EVENT( wxEVT_GRID_COL_SORT
, wxGridEvent
);
151 wxDEFINE_EVENT( wxEVT_GRID_RANGE_SELECT
, wxGridRangeSelectEvent
);
152 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGING
, wxGridEvent
);
153 wxDEFINE_EVENT( wxEVT_GRID_CELL_CHANGED
, wxGridEvent
);
154 wxDEFINE_EVENT( wxEVT_GRID_SELECT_CELL
, wxGridEvent
);
155 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_SHOWN
, wxGridEvent
);
156 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_HIDDEN
, wxGridEvent
);
157 wxDEFINE_EVENT( wxEVT_GRID_EDITOR_CREATED
, wxGridEditorCreatedEvent
);
159 // ============================================================================
161 // ============================================================================
163 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
165 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
166 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
167 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
168 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
171 BEGIN_EVENT_TABLE(wxGridHeaderCtrl
, wxHeaderCtrl
)
172 EVT_HEADER_CLICK(wxID_ANY
, wxGridHeaderCtrl::OnClick
)
174 EVT_HEADER_BEGIN_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnBeginResize
)
175 EVT_HEADER_RESIZING(wxID_ANY
, wxGridHeaderCtrl::OnResizing
)
176 EVT_HEADER_END_RESIZE(wxID_ANY
, wxGridHeaderCtrl::OnEndResize
)
178 EVT_HEADER_BEGIN_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnBeginReorder
)
179 EVT_HEADER_END_REORDER(wxID_ANY
, wxGridHeaderCtrl::OnEndReorder
)
182 wxGridOperations
& wxGridRowOperations::Dual() const
184 static wxGridColumnOperations s_colOper
;
189 wxGridOperations
& wxGridColumnOperations::Dual() const
191 static wxGridRowOperations s_rowOper
;
196 // ----------------------------------------------------------------------------
197 // wxGridCellWorker is an (almost) empty common base class for
198 // wxGridCellRenderer and wxGridCellEditor managing ref counting
199 // ----------------------------------------------------------------------------
201 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
206 wxGridCellWorker::~wxGridCellWorker()
210 // ----------------------------------------------------------------------------
211 // wxGridHeaderLabelsRenderer and related classes
212 // ----------------------------------------------------------------------------
214 void wxGridHeaderLabelsRenderer::DrawLabel(const wxGrid
& grid
,
216 const wxString
& value
,
220 int textOrientation
) const
222 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
223 dc
.SetTextForeground(grid
.GetLabelTextColour());
224 dc
.SetFont(grid
.GetLabelFont());
225 grid
.DrawTextRectangle(dc
, value
, rect
, horizAlign
, vertAlign
, textOrientation
);
229 void wxGridRowHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
233 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
234 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
235 rect
.GetRight(), rect
.GetBottom());
236 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
237 rect
.GetLeft(), rect
.GetBottom());
238 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
239 rect
.GetRight() + 1, rect
.GetBottom());
241 dc
.SetPen(*wxWHITE_PEN
);
242 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
243 rect
.GetLeft() + 1, rect
.GetBottom());
244 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop(),
245 rect
.GetRight(), rect
.GetTop());
250 void wxGridColumnHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
254 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
255 dc
.DrawLine(rect
.GetRight(), rect
.GetTop(),
256 rect
.GetRight(), rect
.GetBottom());
257 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
258 rect
.GetRight(), rect
.GetTop());
259 dc
.DrawLine(rect
.GetLeft(), rect
.GetBottom(),
260 rect
.GetRight() + 1, rect
.GetBottom());
262 dc
.SetPen(*wxWHITE_PEN
);
263 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
264 rect
.GetLeft(), rect
.GetBottom());
265 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop() + 1,
266 rect
.GetRight(), rect
.GetTop() + 1);
271 void wxGridCornerHeaderRendererDefault::DrawBorder(const wxGrid
& WXUNUSED(grid
),
275 dc
.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
)));
276 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
277 rect
.GetRight() - 1, rect
.GetTop());
278 dc
.DrawLine(rect
.GetRight() - 1, rect
.GetBottom() - 1,
279 rect
.GetLeft(), rect
.GetBottom() - 1);
280 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
281 rect
.GetRight(), rect
.GetTop());
282 dc
.DrawLine(rect
.GetLeft(), rect
.GetTop(),
283 rect
.GetLeft(), rect
.GetBottom());
285 dc
.SetPen(*wxWHITE_PEN
);
286 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
287 rect
.GetRight() - 1, rect
.GetTop() + 1);
288 dc
.DrawLine(rect
.GetLeft() + 1, rect
.GetTop() + 1,
289 rect
.GetLeft() + 1, rect
.GetBottom() - 1);
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
300 m_isReadOnly
= Unset
;
305 m_attrkind
= wxGridCellAttr::Cell
;
307 m_sizeRows
= m_sizeCols
= 1;
308 m_overflow
= UnsetOverflow
;
310 SetDefAttr(attrDefault
);
313 wxGridCellAttr
*wxGridCellAttr::Clone() const
315 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
317 if ( HasTextColour() )
318 attr
->SetTextColour(GetTextColour());
319 if ( HasBackgroundColour() )
320 attr
->SetBackgroundColour(GetBackgroundColour());
322 attr
->SetFont(GetFont());
323 if ( HasAlignment() )
324 attr
->SetAlignment(m_hAlign
, m_vAlign
);
326 attr
->SetSize( m_sizeRows
, m_sizeCols
);
330 attr
->SetRenderer(m_renderer
);
331 m_renderer
->IncRef();
335 attr
->SetEditor(m_editor
);
342 attr
->SetOverflow( m_overflow
== Overflow
);
343 attr
->SetKind( m_attrkind
);
348 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
350 if ( !HasTextColour() && mergefrom
->HasTextColour() )
351 SetTextColour(mergefrom
->GetTextColour());
352 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
353 SetBackgroundColour(mergefrom
->GetBackgroundColour());
354 if ( !HasFont() && mergefrom
->HasFont() )
355 SetFont(mergefrom
->GetFont());
356 if ( !HasAlignment() && mergefrom
->HasAlignment() )
359 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
360 SetAlignment(hAlign
, vAlign
);
362 if ( !HasSize() && mergefrom
->HasSize() )
363 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
365 // Directly access member functions as GetRender/Editor don't just return
366 // m_renderer/m_editor
368 // Maybe add support for merge of Render and Editor?
369 if (!HasRenderer() && mergefrom
->HasRenderer() )
371 m_renderer
= mergefrom
->m_renderer
;
372 m_renderer
->IncRef();
374 if ( !HasEditor() && mergefrom
->HasEditor() )
376 m_editor
= mergefrom
->m_editor
;
379 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
380 SetReadOnly(mergefrom
->IsReadOnly());
382 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
383 SetOverflow(mergefrom
->GetOverflow());
385 SetDefAttr(mergefrom
->m_defGridAttr
);
388 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
390 // The size of a cell is normally 1,1
392 // If this cell is larger (2,2) then this is the top left cell
393 // the other cells that will be covered (lower right cells) must be
394 // set to negative or zero values such that
395 // row + num_rows of the covered cell points to the larger cell (this cell)
396 // same goes for the col + num_cols.
398 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
400 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
401 !((num_rows
<= 0) && (num_cols
> 0)) ||
402 !((num_rows
== 0) && (num_cols
== 0))),
403 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
405 m_sizeRows
= num_rows
;
406 m_sizeCols
= num_cols
;
409 const wxColour
& wxGridCellAttr::GetTextColour() const
415 else if (m_defGridAttr
&& m_defGridAttr
!= this)
417 return m_defGridAttr
->GetTextColour();
421 wxFAIL_MSG(wxT("Missing default cell attribute"));
426 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
428 if (HasBackgroundColour())
432 else if (m_defGridAttr
&& m_defGridAttr
!= this)
434 return m_defGridAttr
->GetBackgroundColour();
438 wxFAIL_MSG(wxT("Missing default cell attribute"));
443 const wxFont
& wxGridCellAttr::GetFont() const
449 else if (m_defGridAttr
&& m_defGridAttr
!= this)
451 return m_defGridAttr
->GetFont();
455 wxFAIL_MSG(wxT("Missing default cell attribute"));
460 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
469 else if (m_defGridAttr
&& m_defGridAttr
!= this)
471 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
475 wxFAIL_MSG(wxT("Missing default cell attribute"));
479 void wxGridCellAttr::GetNonDefaultAlignment(int *hAlign
, int *vAlign
) const
481 if ( hAlign
&& m_hAlign
!= wxALIGN_INVALID
)
484 if ( vAlign
&& m_vAlign
!= wxALIGN_INVALID
)
488 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
491 *num_rows
= m_sizeRows
;
493 *num_cols
= m_sizeCols
;
496 // GetRenderer and GetEditor use a slightly different decision path about
497 // which attribute to use. If a non-default attr object has one then it is
498 // used, otherwise the default editor or renderer is fetched from the grid and
499 // used. It should be the default for the data type of the cell. If it is
500 // NULL (because the table has a type that the grid does not have in its
501 // registry), then the grid's default editor or renderer is used.
503 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(const wxGrid
* grid
, int row
, int col
) const
505 wxGridCellRenderer
*renderer
= NULL
;
507 if ( m_renderer
&& this != m_defGridAttr
)
509 // use the cells renderer if it has one
510 renderer
= m_renderer
;
513 else // no non-default cell renderer
515 // get default renderer for the data type
518 // GetDefaultRendererForCell() will do IncRef() for us
519 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
522 if ( renderer
== NULL
)
524 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
526 // if we still don't have one then use the grid default
527 // (no need for IncRef() here neither)
528 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
530 else // default grid attr
532 // use m_renderer which we had decided not to use initially
533 renderer
= m_renderer
;
540 // we're supposed to always find something
541 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
546 // same as above, except for s/renderer/editor/g
547 wxGridCellEditor
* wxGridCellAttr::GetEditor(const wxGrid
* grid
, int row
, int col
) const
549 wxGridCellEditor
*editor
= NULL
;
551 if ( m_editor
&& this != m_defGridAttr
)
553 // use the cells editor if it has one
557 else // no non default cell editor
559 // get default editor for the data type
562 // GetDefaultEditorForCell() will do IncRef() for us
563 editor
= grid
->GetDefaultEditorForCell(row
, col
);
566 if ( editor
== NULL
)
568 if ( (m_defGridAttr
!= NULL
) && (m_defGridAttr
!= this) )
570 // if we still don't have one then use the grid default
571 // (no need for IncRef() here neither)
572 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
574 else // default grid attr
576 // use m_editor which we had decided not to use initially
584 // we're supposed to always find something
585 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
590 // ----------------------------------------------------------------------------
591 // wxGridCellAttrData
592 // ----------------------------------------------------------------------------
594 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
596 // Note: contrary to wxGridRowOrColAttrData::SetAttr, we must not
597 // touch attribute's reference counting explicitly, since this
598 // is managed by class wxGridCellWithAttr
599 int n
= FindIndex(row
, col
);
600 if ( n
== wxNOT_FOUND
)
605 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
607 //else: nothing to do
609 else // we already have an attribute for this cell
613 // change the attribute
614 m_attrs
[(size_t)n
].ChangeAttr(attr
);
618 // remove this attribute
619 m_attrs
.RemoveAt((size_t)n
);
624 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
626 wxGridCellAttr
*attr
= NULL
;
628 int n
= FindIndex(row
, col
);
629 if ( n
!= wxNOT_FOUND
)
631 attr
= m_attrs
[(size_t)n
].attr
;
638 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
640 size_t count
= m_attrs
.GetCount();
641 for ( size_t n
= 0; n
< count
; n
++ )
643 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
644 wxCoord row
= coords
.GetRow();
645 if ((size_t)row
>= pos
)
649 // If rows inserted, include row counter where necessary
650 coords
.SetRow(row
+ numRows
);
652 else if (numRows
< 0)
654 // If rows deleted ...
655 if ((size_t)row
>= pos
- numRows
)
657 // ...either decrement row counter (if row still exists)...
658 coords
.SetRow(row
+ numRows
);
662 // ...or remove the attribute
672 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
674 size_t count
= m_attrs
.GetCount();
675 for ( size_t n
= 0; n
< count
; n
++ )
677 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
678 wxCoord col
= coords
.GetCol();
679 if ( (size_t)col
>= pos
)
683 // If rows inserted, include row counter where necessary
684 coords
.SetCol(col
+ numCols
);
686 else if (numCols
< 0)
688 // If rows deleted ...
689 if ((size_t)col
>= pos
- numCols
)
691 // ...either decrement row counter (if row still exists)...
692 coords
.SetCol(col
+ numCols
);
696 // ...or remove the attribute
706 int wxGridCellAttrData::FindIndex(int row
, int col
) const
708 size_t count
= m_attrs
.GetCount();
709 for ( size_t n
= 0; n
< count
; n
++ )
711 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
712 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
721 // ----------------------------------------------------------------------------
722 // wxGridRowOrColAttrData
723 // ----------------------------------------------------------------------------
725 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
727 size_t count
= m_attrs
.GetCount();
728 for ( size_t n
= 0; n
< count
; n
++ )
730 m_attrs
[n
]->DecRef();
734 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
736 wxGridCellAttr
*attr
= NULL
;
738 int n
= m_rowsOrCols
.Index(rowOrCol
);
739 if ( n
!= wxNOT_FOUND
)
741 attr
= m_attrs
[(size_t)n
];
748 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
750 int i
= m_rowsOrCols
.Index(rowOrCol
);
751 if ( i
== wxNOT_FOUND
)
755 // store the new attribute, taking its ownership
756 m_rowsOrCols
.Add(rowOrCol
);
761 else // we have an attribute for this row or column
763 size_t n
= (size_t)i
;
765 // notice that this code works correctly even when the old attribute is
766 // the same as the new one: as we own of it, we must call DecRef() on
767 // it in any case and this won't result in destruction of the new
768 // attribute if it's the same as old one because it must have ref count
769 // of at least 2 to be passed to us while we keep a reference to it too
770 m_attrs
[n
]->DecRef();
774 // replace the attribute with the new one
777 else // remove the attribute
779 m_rowsOrCols
.RemoveAt(n
);
785 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
787 size_t count
= m_attrs
.GetCount();
788 for ( size_t n
= 0; n
< count
; n
++ )
790 int & rowOrCol
= m_rowsOrCols
[n
];
791 if ( (size_t)rowOrCol
>= pos
)
793 if ( numRowsOrCols
> 0 )
795 // If rows inserted, include row counter where necessary
796 rowOrCol
+= numRowsOrCols
;
798 else if ( numRowsOrCols
< 0)
800 // If rows deleted, either decrement row counter (if row still exists)
801 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
802 rowOrCol
+= numRowsOrCols
;
805 m_rowsOrCols
.RemoveAt(n
);
806 m_attrs
[n
]->DecRef();
816 // ----------------------------------------------------------------------------
817 // wxGridCellAttrProvider
818 // ----------------------------------------------------------------------------
820 wxGridCellAttrProvider::wxGridCellAttrProvider()
825 wxGridCellAttrProvider::~wxGridCellAttrProvider()
830 void wxGridCellAttrProvider::InitData()
832 m_data
= new wxGridCellAttrProviderData
;
835 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
836 wxGridCellAttr::wxAttrKind kind
) const
838 wxGridCellAttr
*attr
= NULL
;
843 case (wxGridCellAttr::Any
):
844 // Get cached merge attributes.
845 // Currently not used as no cache implemented as not mutable
846 // attr = m_data->m_mergeAttr.GetAttr(row, col);
849 // Basically implement old version.
850 // Also check merge cache, so we don't have to re-merge every time..
851 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
852 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
853 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
855 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
857 // Two or more are non NULL
858 attr
= new wxGridCellAttr
;
859 attr
->SetKind(wxGridCellAttr::Merged
);
861 // Order is important..
864 attr
->MergeWith(attrcell
);
869 attr
->MergeWith(attrcol
);
874 attr
->MergeWith(attrrow
);
878 // store merge attr if cache implemented
880 //m_data->m_mergeAttr.SetAttr(attr, row, col);
884 // one or none is non null return it or null.
903 case (wxGridCellAttr::Cell
):
904 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
907 case (wxGridCellAttr::Col
):
908 attr
= m_data
->m_colAttrs
.GetAttr(col
);
911 case (wxGridCellAttr::Row
):
912 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
917 // (wxGridCellAttr::Default):
918 // (wxGridCellAttr::Merged):
926 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
932 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
935 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
940 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
943 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
948 m_data
->m_colAttrs
.SetAttr(attr
, col
);
951 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
955 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
957 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
961 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
965 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
967 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
971 const wxGridColumnHeaderRenderer
&
972 wxGridCellAttrProvider::GetColumnHeaderRenderer(int WXUNUSED(col
))
974 return gs_defaultHeaderRenderers
.colRenderer
;
977 const wxGridRowHeaderRenderer
&
978 wxGridCellAttrProvider::GetRowHeaderRenderer(int WXUNUSED(row
))
980 return gs_defaultHeaderRenderers
.rowRenderer
;
983 const wxGridCornerHeaderRenderer
& wxGridCellAttrProvider::GetCornerRenderer()
985 return gs_defaultHeaderRenderers
.cornerRenderer
;
988 // ----------------------------------------------------------------------------
990 // ----------------------------------------------------------------------------
992 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
994 wxGridTableBase::wxGridTableBase()
997 m_attrProvider
= NULL
;
1000 wxGridTableBase::~wxGridTableBase()
1002 delete m_attrProvider
;
1005 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
1007 delete m_attrProvider
;
1008 m_attrProvider
= attrProvider
;
1011 bool wxGridTableBase::CanHaveAttributes()
1013 if ( ! GetAttrProvider() )
1015 // use the default attr provider by default
1016 SetAttrProvider(new wxGridCellAttrProvider
);
1022 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
1024 if ( m_attrProvider
)
1025 return m_attrProvider
->GetAttr(row
, col
, kind
);
1030 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
1032 if ( m_attrProvider
)
1035 attr
->SetKind(wxGridCellAttr::Cell
);
1036 m_attrProvider
->SetAttr(attr
, row
, col
);
1040 // as we take ownership of the pointer and don't store it, we must
1046 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1048 if ( m_attrProvider
)
1050 attr
->SetKind(wxGridCellAttr::Row
);
1051 m_attrProvider
->SetRowAttr(attr
, row
);
1055 // as we take ownership of the pointer and don't store it, we must
1061 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
1063 if ( m_attrProvider
)
1065 attr
->SetKind(wxGridCellAttr::Col
);
1066 m_attrProvider
->SetColAttr(attr
, col
);
1070 // as we take ownership of the pointer and don't store it, we must
1076 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
1077 size_t WXUNUSED(numRows
) )
1079 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
1084 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
1086 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
1091 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
1092 size_t WXUNUSED(numRows
) )
1094 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
1099 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
1100 size_t WXUNUSED(numCols
) )
1102 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
1107 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
1109 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
1114 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
1115 size_t WXUNUSED(numCols
) )
1117 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
1122 wxString
wxGridTableBase::GetRowLabelValue( int row
)
1126 // RD: Starting the rows at zero confuses users,
1127 // no matter how much it makes sense to us geeks.
1133 wxString
wxGridTableBase::GetColLabelValue( int col
)
1135 // default col labels are:
1136 // cols 0 to 25 : A-Z
1137 // cols 26 to 675 : AA-ZZ
1142 for ( n
= 1; ; n
++ )
1144 s
+= (wxChar
) (wxT('A') + (wxChar
)(col
% 26));
1150 // reverse the string...
1152 for ( i
= 0; i
< n
; i
++ )
1160 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
1162 return wxGRID_VALUE_STRING
;
1165 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
1166 const wxString
& typeName
)
1168 return typeName
== wxGRID_VALUE_STRING
;
1171 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
1173 return CanGetValueAs(row
, col
, typeName
);
1176 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
1181 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
1186 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
1191 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
1192 long WXUNUSED(value
) )
1196 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
1197 double WXUNUSED(value
) )
1201 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
1202 bool WXUNUSED(value
) )
1206 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1207 const wxString
& WXUNUSED(typeName
) )
1212 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
1213 const wxString
& WXUNUSED(typeName
),
1214 void* WXUNUSED(value
) )
1218 //////////////////////////////////////////////////////////////////////
1220 // Message class for the grid table to send requests and notifications
1224 wxGridTableMessage::wxGridTableMessage()
1232 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
1233 int commandInt1
, int commandInt2
)
1237 m_comInt1
= commandInt1
;
1238 m_comInt2
= commandInt2
;
1241 //////////////////////////////////////////////////////////////////////
1243 // A basic grid table for string data. An object of this class will
1244 // created by wxGrid if you don't specify an alternative table class.
1247 WX_DEFINE_OBJARRAY(wxGridStringArray
)
1249 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
1251 wxGridStringTable::wxGridStringTable()
1257 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
1260 m_numCols
= numCols
;
1262 m_data
.Alloc( numRows
);
1265 sa
.Alloc( numCols
);
1266 sa
.Add( wxEmptyString
, numCols
);
1268 m_data
.Add( sa
, numRows
);
1271 wxString
wxGridStringTable::GetValue( int row
, int col
)
1273 wxCHECK_MSG( (row
>= 0 && row
< GetNumberRows()) &&
1274 (col
>= 0 && col
< GetNumberCols()),
1276 wxT("invalid row or column index in wxGridStringTable") );
1278 return m_data
[row
][col
];
1281 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
1283 wxCHECK_RET( (row
>= 0 && row
< GetNumberRows()) &&
1284 (col
>= 0 && col
< GetNumberCols()),
1285 wxT("invalid row or column index in wxGridStringTable") );
1287 m_data
[row
][col
] = value
;
1290 void wxGridStringTable::Clear()
1293 int numRows
, numCols
;
1295 numRows
= m_data
.GetCount();
1298 numCols
= m_data
[0].GetCount();
1300 for ( row
= 0; row
< numRows
; row
++ )
1302 for ( col
= 0; col
< numCols
; col
++ )
1304 m_data
[row
][col
] = wxEmptyString
;
1310 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
1312 size_t curNumRows
= m_data
.GetCount();
1313 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1314 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1316 if ( pos
>= curNumRows
)
1318 return AppendRows( numRows
);
1322 sa
.Alloc( curNumCols
);
1323 sa
.Add( wxEmptyString
, curNumCols
);
1324 m_data
.Insert( sa
, pos
, numRows
);
1328 wxGridTableMessage
msg( this,
1329 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
1333 GetView()->ProcessTableMessage( msg
);
1339 bool wxGridStringTable::AppendRows( size_t numRows
)
1341 size_t curNumRows
= m_data
.GetCount();
1342 size_t curNumCols
= ( curNumRows
> 0
1343 ? m_data
[0].GetCount()
1344 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1347 if ( curNumCols
> 0 )
1349 sa
.Alloc( curNumCols
);
1350 sa
.Add( wxEmptyString
, curNumCols
);
1353 m_data
.Add( sa
, numRows
);
1357 wxGridTableMessage
msg( this,
1358 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
1361 GetView()->ProcessTableMessage( msg
);
1367 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
1369 size_t curNumRows
= m_data
.GetCount();
1371 if ( pos
>= curNumRows
)
1373 wxFAIL_MSG( wxString::Format
1375 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
1377 (unsigned long)numRows
,
1378 (unsigned long)curNumRows
1384 if ( numRows
> curNumRows
- pos
)
1386 numRows
= curNumRows
- pos
;
1389 if ( numRows
>= curNumRows
)
1395 m_data
.RemoveAt( pos
, numRows
);
1400 wxGridTableMessage
msg( this,
1401 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
1405 GetView()->ProcessTableMessage( msg
);
1411 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
1415 size_t curNumRows
= m_data
.GetCount();
1416 size_t curNumCols
= ( curNumRows
> 0
1417 ? m_data
[0].GetCount()
1418 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1420 if ( pos
>= curNumCols
)
1422 return AppendCols( numCols
);
1425 if ( !m_colLabels
.IsEmpty() )
1427 m_colLabels
.Insert( wxEmptyString
, pos
, numCols
);
1430 for ( i
= pos
; i
< pos
+ numCols
; i
++ )
1431 m_colLabels
[i
] = wxGridTableBase::GetColLabelValue( i
);
1434 for ( row
= 0; row
< curNumRows
; row
++ )
1436 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
1438 m_data
[row
].Insert( wxEmptyString
, col
);
1442 m_numCols
+= numCols
;
1446 wxGridTableMessage
msg( this,
1447 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
1451 GetView()->ProcessTableMessage( msg
);
1457 bool wxGridStringTable::AppendCols( size_t numCols
)
1461 size_t curNumRows
= m_data
.GetCount();
1463 for ( row
= 0; row
< curNumRows
; row
++ )
1465 m_data
[row
].Add( wxEmptyString
, numCols
);
1468 m_numCols
+= numCols
;
1472 wxGridTableMessage
msg( this,
1473 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
1476 GetView()->ProcessTableMessage( msg
);
1482 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
1486 size_t curNumRows
= m_data
.GetCount();
1487 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
1488 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
1490 if ( pos
>= curNumCols
)
1492 wxFAIL_MSG( wxString::Format
1494 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
1496 (unsigned long)numCols
,
1497 (unsigned long)curNumCols
1504 colID
= GetView()->GetColAt( pos
);
1508 if ( numCols
> curNumCols
- colID
)
1510 numCols
= curNumCols
- colID
;
1513 if ( !m_colLabels
.IsEmpty() )
1515 // m_colLabels stores just as many elements as it needs, e.g. if only
1516 // the label of the first column had been set it would have only one
1517 // element and not numCols, so account for it
1518 int nToRm
= m_colLabels
.size() - colID
;
1520 m_colLabels
.RemoveAt( colID
, nToRm
);
1523 if ( numCols
>= curNumCols
)
1525 for ( row
= 0; row
< curNumRows
; row
++ )
1527 m_data
[row
].Clear();
1532 else // something will be left
1534 for ( row
= 0; row
< curNumRows
; row
++ )
1536 m_data
[row
].RemoveAt( colID
, numCols
);
1539 m_numCols
-= numCols
;
1544 wxGridTableMessage
msg( this,
1545 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
1549 GetView()->ProcessTableMessage( msg
);
1555 wxString
wxGridStringTable::GetRowLabelValue( int row
)
1557 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1559 // using default label
1561 return wxGridTableBase::GetRowLabelValue( row
);
1565 return m_rowLabels
[row
];
1569 wxString
wxGridStringTable::GetColLabelValue( int col
)
1571 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1573 // using default label
1575 return wxGridTableBase::GetColLabelValue( col
);
1579 return m_colLabels
[col
];
1583 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
1585 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
1587 int n
= m_rowLabels
.GetCount();
1590 for ( i
= n
; i
<= row
; i
++ )
1592 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
1596 m_rowLabels
[row
] = value
;
1599 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
1601 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
1603 int n
= m_colLabels
.GetCount();
1606 for ( i
= n
; i
<= col
; i
++ )
1608 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
1612 m_colLabels
[col
] = value
;
1616 //////////////////////////////////////////////////////////////////////
1617 //////////////////////////////////////////////////////////////////////
1619 BEGIN_EVENT_TABLE(wxGridSubwindow
, wxWindow
)
1620 EVT_MOUSE_CAPTURE_LOST(wxGridSubwindow::OnMouseCaptureLost
)
1623 void wxGridSubwindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
& WXUNUSED(event
))
1625 m_owner
->CancelMouseCapture();
1628 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxGridSubwindow
)
1629 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
1630 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
1631 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
1634 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1638 // NO - don't do this because it will set both the x and y origin
1639 // coords to match the parent scrolled window and we just want to
1640 // set the y coord - MB
1642 // m_owner->PrepareDC( dc );
1645 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1646 wxPoint pt
= dc
.GetDeviceOrigin();
1647 dc
.SetDeviceOrigin( pt
.x
, pt
.y
-y
);
1649 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
1650 m_owner
->DrawRowLabels( dc
, rows
);
1653 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1655 m_owner
->ProcessRowLabelMouseEvent( event
);
1658 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1660 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1664 //////////////////////////////////////////////////////////////////////
1666 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxGridSubwindow
)
1667 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
1668 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
1669 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
1672 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1676 // NO - don't do this because it will set both the x and y origin
1677 // coords to match the parent scrolled window and we just want to
1678 // set the x coord - MB
1680 // m_owner->PrepareDC( dc );
1683 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
1684 wxPoint pt
= dc
.GetDeviceOrigin();
1685 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1686 dc
.SetDeviceOrigin( pt
.x
+x
, pt
.y
);
1688 dc
.SetDeviceOrigin( pt
.x
-x
, pt
.y
);
1690 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
1691 m_owner
->DrawColLabels( dc
, cols
);
1694 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1696 m_owner
->ProcessColLabelMouseEvent( event
);
1699 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1701 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1705 //////////////////////////////////////////////////////////////////////
1707 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxGridSubwindow
)
1708 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
1709 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
1710 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
1713 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
1717 m_owner
->DrawCornerLabel(dc
);
1720 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
1722 m_owner
->ProcessCornerLabelMouseEvent( event
);
1725 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
1727 if (!m_owner
->GetEventHandler()->ProcessEvent(event
))
1731 //////////////////////////////////////////////////////////////////////
1733 BEGIN_EVENT_TABLE( wxGridWindow
, wxGridSubwindow
)
1734 EVT_PAINT( wxGridWindow::OnPaint
)
1735 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
1736 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
1737 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
1738 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
1739 EVT_CHAR( wxGridWindow::OnChar
)
1740 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
1741 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
1742 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
1745 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1747 wxPaintDC
dc( this );
1748 m_owner
->PrepareDC( dc
);
1749 wxRegion reg
= GetUpdateRegion();
1750 wxGridCellCoordsArray dirtyCells
= m_owner
->CalcCellsExposed( reg
);
1751 m_owner
->DrawGridCellArea( dc
, dirtyCells
);
1753 m_owner
->DrawGridSpace( dc
);
1755 m_owner
->DrawAllGridLines( dc
, reg
);
1757 m_owner
->DrawHighlight( dc
, dirtyCells
);
1760 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1762 wxWindow::ScrollWindow( dx
, dy
, rect
);
1763 m_owner
->GetGridRowLabelWindow()->ScrollWindow( 0, dy
, rect
);
1764 m_owner
->GetGridColLabelWindow()->ScrollWindow( dx
, 0, rect
);
1767 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
1769 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
1772 m_owner
->ProcessGridCellMouseEvent( event
);
1775 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
1777 if (!m_owner
->GetEventHandler()->ProcessEvent( event
))
1781 // This seems to be required for wxMotif/wxGTK otherwise the mouse
1782 // cursor must be in the cell edit control to get key events
1784 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
1786 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1790 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
1792 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1796 void wxGridWindow::OnChar( wxKeyEvent
& event
)
1798 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1802 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
1806 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
1808 // and if we have any selection, it has to be repainted, because it
1809 // uses different colour when the grid is not focused:
1810 if ( m_owner
->IsSelection() )
1816 // NB: Note that this code is in "else" branch only because the other
1817 // branch refreshes everything and so there's no point in calling
1818 // Refresh() again, *not* because it should only be done if
1819 // !IsSelection(). If the above code is ever optimized to refresh
1820 // only selected area, this needs to be moved out of the "else"
1821 // branch so that it's always executed.
1823 // current cell cursor {dis,re}appears on focus change:
1824 const wxGridCellCoords
cursorCoords(m_owner
->GetGridCursorRow(),
1825 m_owner
->GetGridCursorCol());
1826 const wxRect cursor
=
1827 m_owner
->BlockToDeviceRect(cursorCoords
, cursorCoords
);
1828 Refresh(true, &cursor
);
1831 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
1835 #define internalXToCol(x) XToCol(x, true)
1836 #define internalYToRow(y) YToRow(y, true)
1838 /////////////////////////////////////////////////////////////////////
1840 #if wxUSE_EXTENDED_RTTI
1841 WX_DEFINE_FLAGS( wxGridStyle
)
1843 wxBEGIN_FLAGS( wxGridStyle
)
1844 // new style border flags, we put them first to
1845 // use them for streaming out
1846 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
1847 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
1848 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
1849 wxFLAGS_MEMBER(wxBORDER_RAISED
)
1850 wxFLAGS_MEMBER(wxBORDER_STATIC
)
1851 wxFLAGS_MEMBER(wxBORDER_NONE
)
1853 // old style border flags
1854 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
1855 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
1856 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
1857 wxFLAGS_MEMBER(wxRAISED_BORDER
)
1858 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
1859 wxFLAGS_MEMBER(wxBORDER
)
1861 // standard window styles
1862 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
1863 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
1864 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
1865 wxFLAGS_MEMBER(wxWANTS_CHARS
)
1866 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
1867 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
1868 wxFLAGS_MEMBER(wxVSCROLL
)
1869 wxFLAGS_MEMBER(wxHSCROLL
)
1871 wxEND_FLAGS( wxGridStyle
)
1873 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
1875 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
1876 wxHIDE_PROPERTY( Children
)
1877 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
1878 wxEND_PROPERTIES_TABLE()
1880 wxBEGIN_HANDLERS_TABLE(wxGrid
)
1881 wxEND_HANDLERS_TABLE()
1883 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
1886 TODO : Expose more information of a list's layout, etc. via appropriate objects (e.g., NotebookPageInfo)
1889 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
1892 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
1893 EVT_PAINT( wxGrid::OnPaint
)
1894 EVT_SIZE( wxGrid::OnSize
)
1895 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
1896 EVT_KEY_UP( wxGrid::OnKeyUp
)
1897 EVT_CHAR ( wxGrid::OnChar
)
1898 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
1901 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
1902 const wxPoint
& pos
, const wxSize
& size
,
1903 long style
, const wxString
& name
)
1905 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
1906 style
| wxWANTS_CHARS
, name
))
1909 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1910 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
1913 SetInitialSize(size
);
1922 m_winCapture
->ReleaseMouse();
1924 // Ensure that the editor control is destroyed before the grid is,
1925 // otherwise we crash later when the editor tries to do something with the
1926 // half destroyed grid
1927 HideCellEditControl();
1929 // Must do this or ~wxScrollHelper will pop the wrong event handler
1930 SetTargetWindow(this);
1932 wxSafeDecRef(m_defaultCellAttr
);
1934 #ifdef DEBUG_ATTR_CACHE
1935 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
1936 wxPrintf(wxT("wxGrid attribute cache statistics: "
1937 "total: %u, hits: %u (%u%%)\n"),
1938 total
, gs_nAttrCacheHits
,
1939 total
? (gs_nAttrCacheHits
*100) / total
: 0);
1942 // if we own the table, just delete it, otherwise at least don't leave it
1943 // with dangling view pointer
1946 else if ( m_table
&& m_table
->GetView() == this )
1947 m_table
->SetView(NULL
);
1949 delete m_typeRegistry
;
1952 delete m_setFixedRows
;
1953 delete m_setFixedCols
;
1957 // ----- internal init and update functions
1960 // NOTE: If using the default visual attributes works everywhere then this can
1961 // be removed as well as the #else cases below.
1962 #define _USE_VISATTR 0
1964 void wxGrid::Create()
1966 // create the type registry
1967 m_typeRegistry
= new wxGridTypeRegistry
;
1969 m_cellEditCtrlEnabled
= false;
1971 m_defaultCellAttr
= new wxGridCellAttr();
1973 // Set default cell attributes
1974 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
1975 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
1976 m_defaultCellAttr
->SetFont(GetFont());
1977 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
1978 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
1979 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
1982 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
1983 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
1985 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
1986 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
1989 m_defaultCellAttr
->SetTextColour(
1990 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1991 m_defaultCellAttr
->SetBackgroundColour(
1992 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1997 m_currentCellCoords
= wxGridNoCellCoords
;
1999 // subwindow components that make up the wxGrid
2000 m_rowLabelWin
= new wxGridRowLabelWindow(this);
2001 CreateColumnWindow();
2002 m_cornerLabelWin
= new wxGridCornerLabelWindow(this);
2003 m_gridWin
= new wxGridWindow( this );
2005 SetTargetWindow( m_gridWin
);
2008 wxColour gfg
= gva
.colFg
;
2009 wxColour gbg
= gva
.colBg
;
2010 wxColour lfg
= lva
.colFg
;
2011 wxColour lbg
= lva
.colBg
;
2013 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2014 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
2015 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
2016 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
2019 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
2020 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
2021 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
2022 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
2023 m_colWindow
->SetOwnForegroundColour(lfg
);
2024 m_colWindow
->SetOwnBackgroundColour(lbg
);
2026 m_gridWin
->SetOwnForegroundColour(gfg
);
2027 m_gridWin
->SetOwnBackgroundColour(gbg
);
2029 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
2030 m_labelTextColour
= m_rowLabelWin
->GetForegroundColour();
2032 // now that we have the grid window, use its font to compute the default
2034 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
2035 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
2036 m_defaultRowHeight
+= 8;
2038 m_defaultRowHeight
+= 4;
2043 void wxGrid::CreateColumnWindow()
2045 if ( m_useNativeHeader
)
2047 m_colWindow
= new wxGridHeaderCtrl(this);
2048 m_colLabelHeight
= m_colWindow
->GetBestSize().y
;
2050 else // draw labels ourselves
2052 m_colWindow
= new wxGridColLabelWindow(this);
2053 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2057 bool wxGrid::CreateGrid( int numRows
, int numCols
,
2058 wxGridSelectionModes selmode
)
2060 wxCHECK_MSG( !m_created
,
2062 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
2064 return SetTable(new wxGridStringTable(numRows
, numCols
), true, selmode
);
2067 void wxGrid::SetSelectionMode(wxGridSelectionModes selmode
)
2069 wxCHECK_RET( m_created
,
2070 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
2072 m_selection
->SetSelectionMode( selmode
);
2075 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
2077 wxCHECK_MSG( m_created
, wxGridSelectCells
,
2078 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
2080 return m_selection
->GetSelectionMode();
2084 wxGrid::SetTable(wxGridTableBase
*table
,
2086 wxGrid::wxGridSelectionModes selmode
)
2088 bool checkSelection
= false;
2091 // stop all processing
2096 m_table
->SetView(0);
2102 wxDELETE(m_selection
);
2107 checkSelection
= true;
2109 // kill row and column size arrays
2110 m_colWidths
.Empty();
2111 m_colRights
.Empty();
2112 m_rowHeights
.Empty();
2113 m_rowBottoms
.Empty();
2118 m_numRows
= table
->GetNumberRows();
2119 m_numCols
= table
->GetNumberCols();
2121 if ( m_useNativeHeader
)
2122 GetGridColHeader()->SetColumnCount(m_numCols
);
2125 m_table
->SetView( this );
2126 m_ownTable
= takeOwnership
;
2127 m_selection
= new wxGridSelection( this, selmode
);
2130 // If the newly set table is smaller than the
2131 // original one current cell and selection regions
2132 // might be invalid,
2133 m_selectedBlockCorner
= wxGridNoCellCoords
;
2134 m_currentCellCoords
=
2135 wxGridCellCoords(wxMin(m_numRows
, m_currentCellCoords
.GetRow()),
2136 wxMin(m_numCols
, m_currentCellCoords
.GetCol()));
2137 if (m_selectedBlockTopLeft
.GetRow() >= m_numRows
||
2138 m_selectedBlockTopLeft
.GetCol() >= m_numCols
)
2140 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
2141 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
2144 m_selectedBlockBottomRight
=
2145 wxGridCellCoords(wxMin(m_numRows
,
2146 m_selectedBlockBottomRight
.GetRow()),
2148 m_selectedBlockBottomRight
.GetCol()));
2162 m_cornerLabelWin
= NULL
;
2163 m_rowLabelWin
= NULL
;
2171 m_defaultCellAttr
= NULL
;
2172 m_typeRegistry
= NULL
;
2173 m_winCapture
= NULL
;
2175 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
2176 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
2179 m_setFixedCols
= NULL
;
2182 m_attrCache
.row
= -1;
2183 m_attrCache
.col
= -1;
2184 m_attrCache
.attr
= NULL
;
2186 m_labelFont
= GetFont();
2187 m_labelFont
.SetWeight( wxBOLD
);
2189 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
2190 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
2192 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
2193 m_colLabelVertAlign
= wxALIGN_CENTRE
;
2194 m_colLabelTextOrientation
= wxHORIZONTAL
;
2196 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
2197 m_defaultRowHeight
= 0; // this will be initialized after creation
2199 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
2200 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
2202 m_gridLineColour
= wxColour( 192,192,192 );
2203 m_gridLinesEnabled
= true;
2204 m_gridLinesClipHorz
=
2205 m_gridLinesClipVert
= true;
2206 m_cellHighlightColour
= *wxBLACK
;
2207 m_cellHighlightPenWidth
= 2;
2208 m_cellHighlightROPenWidth
= 1;
2210 m_canDragColMove
= false;
2212 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
2213 m_winCapture
= NULL
;
2214 m_canDragRowSize
= true;
2215 m_canDragColSize
= true;
2216 m_canDragGridSize
= true;
2217 m_canDragCell
= false;
2219 m_dragRowOrCol
= -1;
2220 m_isDragging
= false;
2221 m_startDragPos
= wxDefaultPosition
;
2223 m_sortCol
= wxNOT_FOUND
;
2224 m_sortIsAscending
= true;
2227 m_nativeColumnLabels
= false;
2229 m_waitForSlowClick
= false;
2231 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
2232 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
2234 m_currentCellCoords
= wxGridNoCellCoords
;
2236 m_selectedBlockTopLeft
=
2237 m_selectedBlockBottomRight
=
2238 m_selectedBlockCorner
= wxGridNoCellCoords
;
2240 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
2241 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
2243 m_editable
= true; // default for whole grid
2245 m_inOnKeyDown
= false;
2251 // we can't call SetScrollRate() as the window isn't created yet but OTOH
2252 // we don't need to call it neither as the scroll position is (0, 0) right
2253 // now anyhow, so just set the parameters directly
2254 m_xScrollPixelsPerLine
= GRID_SCROLL_LINE_X
;
2255 m_yScrollPixelsPerLine
= GRID_SCROLL_LINE_Y
;
2258 // ----------------------------------------------------------------------------
2259 // the idea is to call these functions only when necessary because they create
2260 // quite big arrays which eat memory mostly unnecessary - in particular, if
2261 // default widths/heights are used for all rows/columns, we may not use these
2264 // with some extra code, it should be possible to only store the widths/heights
2265 // different from default ones (resulting in space savings for huge grids) but
2266 // this is not done currently
2267 // ----------------------------------------------------------------------------
2269 void wxGrid::InitRowHeights()
2271 m_rowHeights
.Empty();
2272 m_rowBottoms
.Empty();
2274 m_rowHeights
.Alloc( m_numRows
);
2275 m_rowBottoms
.Alloc( m_numRows
);
2277 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
2280 for ( int i
= 0; i
< m_numRows
; i
++ )
2282 rowBottom
+= m_defaultRowHeight
;
2283 m_rowBottoms
.Add( rowBottom
);
2287 void wxGrid::InitColWidths()
2289 m_colWidths
.Empty();
2290 m_colRights
.Empty();
2292 m_colWidths
.Alloc( m_numCols
);
2293 m_colRights
.Alloc( m_numCols
);
2295 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
2297 for ( int i
= 0; i
< m_numCols
; i
++ )
2299 int colRight
= ( GetColPos( i
) + 1 ) * m_defaultColWidth
;
2300 m_colRights
.Add( colRight
);
2304 int wxGrid::GetColWidth(int col
) const
2306 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
2309 int wxGrid::GetColLeft(int col
) const
2311 return m_colRights
.IsEmpty() ? GetColPos( col
) * m_defaultColWidth
2312 : m_colRights
[col
] - m_colWidths
[col
];
2315 int wxGrid::GetColRight(int col
) const
2317 return m_colRights
.IsEmpty() ? (GetColPos( col
) + 1) * m_defaultColWidth
2321 int wxGrid::GetRowHeight(int row
) const
2323 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
2326 int wxGrid::GetRowTop(int row
) const
2328 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
2329 : m_rowBottoms
[row
] - m_rowHeights
[row
];
2332 int wxGrid::GetRowBottom(int row
) const
2334 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
2335 : m_rowBottoms
[row
];
2338 void wxGrid::CalcDimensions()
2340 // compute the size of the scrollable area
2341 int w
= m_numCols
> 0 ? GetColRight(GetColAt(m_numCols
- 1)) : 0;
2342 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
2347 // take into account editor if shown
2348 if ( IsCellEditControlShown() )
2351 int r
= m_currentCellCoords
.GetRow();
2352 int c
= m_currentCellCoords
.GetCol();
2353 int x
= GetColLeft(c
);
2354 int y
= GetRowTop(r
);
2356 // how big is the editor
2357 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
2358 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
2359 editor
->GetControl()->GetSize(&w2
, &h2
);
2370 // preserve (more or less) the previous position
2372 GetViewStart( &x
, &y
);
2374 // ensure the position is valid for the new scroll ranges
2376 x
= wxMax( w
- 1, 0 );
2378 y
= wxMax( h
- 1, 0 );
2380 // update the virtual size and refresh the scrollbars to reflect it
2381 m_gridWin
->SetVirtualSize(w
, h
);
2385 // if our OnSize() hadn't been called (it would if we have scrollbars), we
2386 // still must reposition the children
2390 wxSize
wxGrid::GetSizeAvailableForScrollTarget(const wxSize
& size
)
2392 wxSize
sizeGridWin(size
);
2393 sizeGridWin
.x
-= m_rowLabelWidth
;
2394 sizeGridWin
.y
-= m_colLabelHeight
;
2399 void wxGrid::CalcWindowSizes()
2401 // escape if the window is has not been fully created yet
2403 if ( m_cornerLabelWin
== NULL
)
2407 GetClientSize( &cw
, &ch
);
2409 // the grid may be too small to have enough space for the labels yet, don't
2410 // size the windows to negative sizes in this case
2411 int gw
= cw
- m_rowLabelWidth
;
2412 int gh
= ch
- m_colLabelHeight
;
2418 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
2419 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
2421 if ( m_colWindow
&& m_colWindow
->IsShown() )
2422 m_colWindow
->SetSize( m_rowLabelWidth
, 0, gw
, m_colLabelHeight
);
2424 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
2425 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, gh
);
2427 if ( m_gridWin
&& m_gridWin
->IsShown() )
2428 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, gw
, gh
);
2431 // this is called when the grid table sends a message
2432 // to indicate that it has been redimensioned
2434 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
2437 bool result
= false;
2439 // Clear the attribute cache as the attribute might refer to a different
2440 // cell than stored in the cache after adding/removing rows/columns.
2443 // By the same reasoning, the editor should be dismissed if columns are
2444 // added or removed. And for consistency, it should IMHO always be
2445 // removed, not only if the cell "underneath" it actually changes.
2446 // For now, I intentionally do not save the editor's content as the
2447 // cell it might want to save that stuff to might no longer exist.
2448 HideCellEditControl();
2450 switch ( msg
.GetId() )
2452 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
2454 size_t pos
= msg
.GetCommandInt();
2455 int numRows
= msg
.GetCommandInt2();
2457 m_numRows
+= numRows
;
2459 if ( !m_rowHeights
.IsEmpty() )
2461 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
2462 m_rowBottoms
.Insert( 0, pos
, numRows
);
2466 bottom
= m_rowBottoms
[pos
- 1];
2468 for ( i
= pos
; i
< m_numRows
; i
++ )
2470 bottom
+= m_rowHeights
[i
];
2471 m_rowBottoms
[i
] = bottom
;
2475 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2477 // if we have just inserted cols into an empty grid the current
2478 // cell will be undefined...
2480 SetCurrentCell( 0, 0 );
2484 m_selection
->UpdateRows( pos
, numRows
);
2485 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2487 attrProvider
->UpdateAttrRows( pos
, numRows
);
2489 if ( !GetBatchCount() )
2492 m_rowLabelWin
->Refresh();
2498 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
2500 int numRows
= msg
.GetCommandInt();
2501 int oldNumRows
= m_numRows
;
2502 m_numRows
+= numRows
;
2504 if ( !m_rowHeights
.IsEmpty() )
2506 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
2507 m_rowBottoms
.Add( 0, numRows
);
2510 if ( oldNumRows
> 0 )
2511 bottom
= m_rowBottoms
[oldNumRows
- 1];
2513 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
2515 bottom
+= m_rowHeights
[i
];
2516 m_rowBottoms
[i
] = bottom
;
2520 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2522 // if we have just inserted cols into an empty grid the current
2523 // cell will be undefined...
2525 SetCurrentCell( 0, 0 );
2528 if ( !GetBatchCount() )
2531 m_rowLabelWin
->Refresh();
2537 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
2539 size_t pos
= msg
.GetCommandInt();
2540 int numRows
= msg
.GetCommandInt2();
2541 m_numRows
-= numRows
;
2543 if ( !m_rowHeights
.IsEmpty() )
2545 m_rowHeights
.RemoveAt( pos
, numRows
);
2546 m_rowBottoms
.RemoveAt( pos
, numRows
);
2549 for ( i
= 0; i
< m_numRows
; i
++ )
2551 h
+= m_rowHeights
[i
];
2552 m_rowBottoms
[i
] = h
;
2558 m_currentCellCoords
= wxGridNoCellCoords
;
2562 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
2563 m_currentCellCoords
.Set( 0, 0 );
2567 m_selection
->UpdateRows( pos
, -((int)numRows
) );
2568 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2571 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
2573 // ifdef'd out following patch from Paul Gammans
2575 // No need to touch column attributes, unless we
2576 // removed _all_ rows, in this case, we remove
2577 // all column attributes.
2578 // I hate to do this here, but the
2579 // needed data is not available inside UpdateAttrRows.
2580 if ( !GetNumberRows() )
2581 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
2585 if ( !GetBatchCount() )
2588 m_rowLabelWin
->Refresh();
2594 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
2596 size_t pos
= msg
.GetCommandInt();
2597 int numCols
= msg
.GetCommandInt2();
2598 m_numCols
+= numCols
;
2600 if ( m_useNativeHeader
)
2601 GetGridColHeader()->SetColumnCount(m_numCols
);
2603 if ( !m_colAt
.IsEmpty() )
2605 //Shift the column IDs
2607 for ( i
= 0; i
< m_numCols
- numCols
; i
++ )
2609 if ( m_colAt
[i
] >= (int)pos
)
2610 m_colAt
[i
] += numCols
;
2613 m_colAt
.Insert( pos
, pos
, numCols
);
2615 //Set the new columns' positions
2616 for ( i
= pos
+ 1; i
< (int)pos
+ numCols
; i
++ )
2622 if ( !m_colWidths
.IsEmpty() )
2624 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
2625 m_colRights
.Insert( 0, pos
, numCols
);
2629 right
= m_colRights
[GetColAt( pos
- 1 )];
2632 for ( colPos
= pos
; colPos
< m_numCols
; colPos
++ )
2634 i
= GetColAt( colPos
);
2636 right
+= m_colWidths
[i
];
2637 m_colRights
[i
] = right
;
2641 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2643 // if we have just inserted cols into an empty grid the current
2644 // cell will be undefined...
2646 SetCurrentCell( 0, 0 );
2650 m_selection
->UpdateCols( pos
, numCols
);
2651 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2653 attrProvider
->UpdateAttrCols( pos
, numCols
);
2654 if ( !GetBatchCount() )
2657 m_colWindow
->Refresh();
2663 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
2665 int numCols
= msg
.GetCommandInt();
2666 int oldNumCols
= m_numCols
;
2667 m_numCols
+= numCols
;
2668 if ( m_useNativeHeader
)
2669 GetGridColHeader()->SetColumnCount(m_numCols
);
2671 if ( !m_colAt
.IsEmpty() )
2673 m_colAt
.Add( 0, numCols
);
2675 //Set the new columns' positions
2677 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
2683 if ( !m_colWidths
.IsEmpty() )
2685 m_colWidths
.Add( m_defaultColWidth
, numCols
);
2686 m_colRights
.Add( 0, numCols
);
2689 if ( oldNumCols
> 0 )
2690 right
= m_colRights
[GetColAt( oldNumCols
- 1 )];
2693 for ( colPos
= oldNumCols
; colPos
< m_numCols
; colPos
++ )
2695 i
= GetColAt( colPos
);
2697 right
+= m_colWidths
[i
];
2698 m_colRights
[i
] = right
;
2702 if ( m_currentCellCoords
== wxGridNoCellCoords
)
2704 // if we have just inserted cols into an empty grid the current
2705 // cell will be undefined...
2707 SetCurrentCell( 0, 0 );
2709 if ( !GetBatchCount() )
2712 m_colWindow
->Refresh();
2718 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
2720 size_t pos
= msg
.GetCommandInt();
2721 int numCols
= msg
.GetCommandInt2();
2722 m_numCols
-= numCols
;
2723 if ( m_useNativeHeader
)
2724 GetGridColHeader()->SetColumnCount(m_numCols
);
2726 if ( !m_colAt
.IsEmpty() )
2728 int colID
= GetColAt( pos
);
2730 m_colAt
.RemoveAt( pos
, numCols
);
2732 //Shift the column IDs
2734 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2736 if ( m_colAt
[colPos
] > colID
)
2737 m_colAt
[colPos
] -= numCols
;
2741 if ( !m_colWidths
.IsEmpty() )
2743 m_colWidths
.RemoveAt( pos
, numCols
);
2744 m_colRights
.RemoveAt( pos
, numCols
);
2748 for ( colPos
= 0; colPos
< m_numCols
; colPos
++ )
2750 i
= GetColAt( colPos
);
2752 w
+= m_colWidths
[i
];
2759 m_currentCellCoords
= wxGridNoCellCoords
;
2763 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
2764 m_currentCellCoords
.Set( 0, 0 );
2768 m_selection
->UpdateCols( pos
, -((int)numCols
) );
2769 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
2772 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
2774 // ifdef'd out following patch from Paul Gammans
2776 // No need to touch row attributes, unless we
2777 // removed _all_ columns, in this case, we remove
2778 // all row attributes.
2779 // I hate to do this here, but the
2780 // needed data is not available inside UpdateAttrCols.
2781 if ( !GetNumberCols() )
2782 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
2786 if ( !GetBatchCount() )
2789 m_colWindow
->Refresh();
2796 if (result
&& !GetBatchCount() )
2797 m_gridWin
->Refresh();
2802 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
) const
2804 wxRegionIterator
iter( reg
);
2807 wxArrayInt rowlabels
;
2814 // TODO: remove this when we can...
2815 // There is a bug in wxMotif that gives garbage update
2816 // rectangles if you jump-scroll a long way by clicking the
2817 // scrollbar with middle button. This is a work-around
2819 #if defined(__WXMOTIF__)
2821 m_gridWin
->GetClientSize( &cw
, &ch
);
2822 if ( r
.GetTop() > ch
)
2824 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2827 // logical bounds of update region
2830 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
2831 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
2833 // find the row labels within these bounds
2836 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2838 if ( GetRowBottom(row
) < top
)
2841 if ( GetRowTop(row
) > bottom
)
2844 rowlabels
.Add( row
);
2853 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
) const
2855 wxRegionIterator
iter( reg
);
2858 wxArrayInt colLabels
;
2865 // TODO: remove this when we can...
2866 // There is a bug in wxMotif that gives garbage update
2867 // rectangles if you jump-scroll a long way by clicking the
2868 // scrollbar with middle button. This is a work-around
2870 #if defined(__WXMOTIF__)
2872 m_gridWin
->GetClientSize( &cw
, &ch
);
2873 if ( r
.GetLeft() > cw
)
2875 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2878 // logical bounds of update region
2881 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
2882 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
2884 // find the cells within these bounds
2888 for ( colPos
= GetColPos( internalXToCol(left
) ); colPos
< m_numCols
; colPos
++ )
2890 col
= GetColAt( colPos
);
2892 if ( GetColRight(col
) < left
)
2895 if ( GetColLeft(col
) > right
)
2898 colLabels
.Add( col
);
2907 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
) const
2909 wxRegionIterator
iter( reg
);
2912 wxGridCellCoordsArray cellsExposed
;
2914 int left
, top
, right
, bottom
;
2919 // TODO: remove this when we can...
2920 // There is a bug in wxMotif that gives garbage update
2921 // rectangles if you jump-scroll a long way by clicking the
2922 // scrollbar with middle button. This is a work-around
2924 #if defined(__WXMOTIF__)
2926 m_gridWin
->GetClientSize( &cw
, &ch
);
2927 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
2928 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
2929 r
.SetRight( wxMin( r
.GetRight(), cw
) );
2930 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
2933 // logical bounds of update region
2935 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
2936 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
2938 // find the cells within these bounds
2940 for ( int row
= internalYToRow(top
); row
< m_numRows
; row
++ )
2942 if ( GetRowBottom(row
) <= top
)
2945 if ( GetRowTop(row
) > bottom
)
2948 // add all dirty cells in this row: notice that the columns which
2949 // are dirty don't depend on the row so we compute them only once
2950 // for the first dirty row and then reuse for all the next ones
2953 // do determine the dirty columns
2954 for ( int pos
= XToPos(left
); pos
<= XToPos(right
); pos
++ )
2955 cols
.push_back(GetColAt(pos
));
2957 // if there are no dirty columns at all, nothing to do
2962 const size_t count
= cols
.size();
2963 for ( size_t n
= 0; n
< count
; n
++ )
2964 cellsExposed
.Add(wxGridCellCoords(row
, cols
[n
]));
2970 return cellsExposed
;
2974 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
2977 wxPoint
pos( event
.GetPosition() );
2978 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
2980 if ( event
.Dragging() )
2984 m_isDragging
= true;
2985 m_rowLabelWin
->CaptureMouse();
2988 if ( event
.LeftIsDown() )
2990 switch ( m_cursorMode
)
2992 case WXGRID_CURSOR_RESIZE_ROW
:
2994 int cw
, ch
, left
, dummy
;
2995 m_gridWin
->GetClientSize( &cw
, &ch
);
2996 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
2998 wxClientDC
dc( m_gridWin
);
3001 GetRowTop(m_dragRowOrCol
) +
3002 GetRowMinimalHeight(m_dragRowOrCol
) );
3003 dc
.SetLogicalFunction(wxINVERT
);
3004 if ( m_dragLastPos
>= 0 )
3006 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3008 dc
.DrawLine( left
, y
, left
+cw
, y
);
3013 case WXGRID_CURSOR_SELECT_ROW
:
3015 if ( (row
= YToRow( y
)) >= 0 )
3018 m_selection
->SelectRow(row
, event
);
3023 // default label to suppress warnings about "enumeration value
3024 // 'xxx' not handled in switch
3032 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3037 if (m_rowLabelWin
->HasCapture())
3038 m_rowLabelWin
->ReleaseMouse();
3039 m_isDragging
= false;
3042 // ------------ Entering or leaving the window
3044 if ( event
.Entering() || event
.Leaving() )
3046 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3049 // ------------ Left button pressed
3051 else if ( event
.LeftDown() )
3053 row
= YToEdgeOfRow(y
);
3054 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3056 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3058 else // not a request to start resizing
3062 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3064 if ( !event
.ShiftDown() && !event
.CmdDown() )
3068 if ( event
.ShiftDown() )
3070 m_selection
->SelectBlock
3072 m_currentCellCoords
.GetRow(), 0,
3073 row
, GetNumberCols() - 1,
3079 m_selection
->SelectRow(row
, event
);
3083 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3088 // ------------ Left double click
3090 else if (event
.LeftDClick() )
3092 row
= YToEdgeOfRow(y
);
3093 if ( row
!= wxNOT_FOUND
&& CanDragRowSize(row
) )
3095 // adjust row height depending on label text
3097 // TODO: generate RESIZING event, see #10754
3098 AutoSizeRowLabelSize( row
);
3100 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, row
, -1, event
);
3102 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3105 else // not on row separator or it's not resizeable
3109 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
3111 // no default action at the moment
3116 // ------------ Left button released
3118 else if ( event
.LeftUp() )
3120 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3121 DoEndDragResizeRow(event
);
3123 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3127 // ------------ Right button down
3129 else if ( event
.RightDown() )
3133 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3135 // no default action at the moment
3139 // ------------ Right double click
3141 else if ( event
.RightDClick() )
3145 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3147 // no default action at the moment
3151 // ------------ No buttons down and mouse moving
3153 else if ( event
.Moving() )
3155 m_dragRowOrCol
= YToEdgeOfRow( y
);
3156 if ( m_dragRowOrCol
!= wxNOT_FOUND
)
3158 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3160 if ( CanDragRowSize(m_dragRowOrCol
) )
3161 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
3164 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3166 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
3171 void wxGrid::UpdateColumnSortingIndicator(int col
)
3173 wxCHECK_RET( col
!= wxNOT_FOUND
, "invalid column index" );
3175 if ( m_useNativeHeader
)
3176 GetGridColHeader()->UpdateColumn(col
);
3177 else if ( m_nativeColumnLabels
)
3178 m_colWindow
->Refresh();
3179 //else: sorting indicator display not yet implemented in grid version
3182 void wxGrid::SetSortingColumn(int col
, bool ascending
)
3184 if ( col
== m_sortCol
)
3186 // we are already using this column for sorting (or not sorting at all)
3187 // but we might still change the sorting order, check for it
3188 if ( m_sortCol
!= wxNOT_FOUND
&& ascending
!= m_sortIsAscending
)
3190 m_sortIsAscending
= ascending
;
3192 UpdateColumnSortingIndicator(m_sortCol
);
3195 else // we're changing the column used for sorting
3197 const int sortColOld
= m_sortCol
;
3199 // change it before updating the column as we want GetSortingColumn()
3200 // to return the correct new value
3203 if ( sortColOld
!= wxNOT_FOUND
)
3204 UpdateColumnSortingIndicator(sortColOld
);
3206 if ( m_sortCol
!= wxNOT_FOUND
)
3208 m_sortIsAscending
= ascending
;
3209 UpdateColumnSortingIndicator(m_sortCol
);
3214 void wxGrid::DoColHeaderClick(int col
)
3216 // we consider that the grid was resorted if this event is processed and
3218 if ( SendEvent(wxEVT_GRID_COL_SORT
, -1, col
) == 1 )
3220 SetSortingColumn(col
, IsSortingBy(col
) ? !m_sortIsAscending
: true);
3225 void wxGrid::DoStartResizeCol(int col
)
3227 m_dragRowOrCol
= col
;
3229 DoUpdateResizeColWidth(GetColWidth(m_dragRowOrCol
));
3232 void wxGrid::DoUpdateResizeCol(int x
)
3234 int cw
, ch
, dummy
, top
;
3235 m_gridWin
->GetClientSize( &cw
, &ch
);
3236 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3238 wxClientDC
dc( m_gridWin
);
3241 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) + GetColMinimalWidth(m_dragRowOrCol
));
3242 dc
.SetLogicalFunction(wxINVERT
);
3243 if ( m_dragLastPos
>= 0 )
3245 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ ch
);
3247 dc
.DrawLine( x
, top
, x
, top
+ ch
);
3251 void wxGrid::DoUpdateResizeColWidth(int w
)
3253 DoUpdateResizeCol(GetColLeft(m_dragRowOrCol
) + w
);
3256 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3259 wxPoint
pos( event
.GetPosition() );
3260 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3262 int col
= XToCol(x
);
3263 if ( event
.Dragging() )
3267 m_isDragging
= true;
3268 GetColLabelWindow()->CaptureMouse();
3270 if ( m_cursorMode
== WXGRID_CURSOR_MOVE_COL
&& col
!= -1 )
3271 DoStartMoveCol(col
);
3274 if ( event
.LeftIsDown() )
3276 switch ( m_cursorMode
)
3278 case WXGRID_CURSOR_RESIZE_COL
:
3279 DoUpdateResizeCol(x
);
3282 case WXGRID_CURSOR_SELECT_COL
:
3287 m_selection
->SelectCol(col
, event
);
3292 case WXGRID_CURSOR_MOVE_COL
:
3294 int posNew
= XToPos(x
);
3295 int colNew
= GetColAt(posNew
);
3297 // determine the position of the drop marker
3299 if ( x
>= GetColLeft(colNew
) + (GetColWidth(colNew
) / 2) )
3300 markerX
= GetColRight(colNew
);
3302 markerX
= GetColLeft(colNew
);
3304 if ( markerX
!= m_dragLastPos
)
3306 wxClientDC
dc( GetColLabelWindow() );
3310 GetColLabelWindow()->GetClientSize( &cw
, &ch
);
3314 //Clean up the last indicator
3315 if ( m_dragLastPos
>= 0 )
3317 wxPen
pen( GetColLabelWindow()->GetBackgroundColour(), 2 );
3319 dc
.DrawLine( m_dragLastPos
+ 1, 0, m_dragLastPos
+ 1, ch
);
3320 dc
.SetPen(wxNullPen
);
3322 if ( XToCol( m_dragLastPos
) != -1 )
3323 DrawColLabel( dc
, XToCol( m_dragLastPos
) );
3326 const wxColour
*color
;
3327 //Moving to the same place? Don't draw a marker
3328 if ( colNew
== m_dragRowOrCol
)
3329 color
= wxLIGHT_GREY
;
3334 wxPen
pen( *color
, 2 );
3337 dc
.DrawLine( markerX
, 0, markerX
, ch
);
3339 dc
.SetPen(wxNullPen
);
3341 m_dragLastPos
= markerX
- 1;
3346 // default label to suppress warnings about "enumeration value
3347 // 'xxx' not handled in switch
3355 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
3360 if (GetColLabelWindow()->HasCapture())
3361 GetColLabelWindow()->ReleaseMouse();
3362 m_isDragging
= false;
3365 // ------------ Entering or leaving the window
3367 if ( event
.Entering() || event
.Leaving() )
3369 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3372 // ------------ Left button pressed
3374 else if ( event
.LeftDown() )
3376 int col
= XToEdgeOfCol(x
);
3377 if ( col
!= wxNOT_FOUND
&& CanDragColSize(col
) )
3379 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow());
3381 else // not a request to start resizing
3385 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3387 if ( m_canDragColMove
)
3389 //Show button as pressed
3390 wxClientDC
dc( GetColLabelWindow() );
3391 int colLeft
= GetColLeft( col
);
3392 int colRight
= GetColRight( col
) - 1;
3393 dc
.SetPen( wxPen( GetColLabelWindow()->GetBackgroundColour(), 1 ) );
3394 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
3395 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
3397 ChangeCursorMode(WXGRID_CURSOR_MOVE_COL
, GetColLabelWindow());
3401 if ( !event
.ShiftDown() && !event
.CmdDown() )
3405 if ( event
.ShiftDown() )
3407 m_selection
->SelectBlock
3409 0, m_currentCellCoords
.GetCol(),
3410 GetNumberRows() - 1, col
,
3416 m_selection
->SelectCol(col
, event
);
3420 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, GetColLabelWindow());
3426 // ------------ Left double click
3428 if ( event
.LeftDClick() )
3430 const int colEdge
= XToEdgeOfCol(x
);
3431 if ( colEdge
== -1 )
3434 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
3436 // no default action at the moment
3441 // adjust column width depending on label text
3443 // TODO: generate RESIZING event, see #10754
3444 AutoSizeColLabelSize( colEdge
);
3446 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, colEdge
, event
);
3448 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3453 // ------------ Left button released
3455 else if ( event
.LeftUp() )
3457 switch ( m_cursorMode
)
3459 case WXGRID_CURSOR_RESIZE_COL
:
3460 DoEndDragResizeCol(event
);
3463 case WXGRID_CURSOR_MOVE_COL
:
3464 if ( m_dragLastPos
== -1 || col
== m_dragRowOrCol
)
3466 // the column didn't actually move anywhere
3468 DoColHeaderClick(col
);
3469 m_colWindow
->Refresh(); // "unpress" the column
3473 // get the position of the column we're over
3474 int pos
= XToPos(x
);
3476 // we may need to adjust the drop position but don't bother
3477 // checking for it if we can't anyhow
3480 // also find the index of the column we're over: notice
3481 // that the existing "col" variable may be invalid but
3482 // we need a valid one here
3483 const int colValid
= GetColAt(pos
);
3485 // if we're on the "near" (usually left but right in
3486 // RTL case) part of the column, the actual position we
3487 // should be placed in is actually the one before it
3489 const int middle
= GetColLeft(colValid
) +
3490 GetColWidth(colValid
)/2;
3491 if ( GetLayoutDirection() == wxLayout_LeftToRight
)
3492 onNearPart
= (x
<= middle
);
3493 else // wxLayout_RightToLeft
3494 onNearPart
= (x
> middle
);
3504 case WXGRID_CURSOR_SELECT_COL
:
3505 case WXGRID_CURSOR_SELECT_CELL
:
3506 case WXGRID_CURSOR_RESIZE_ROW
:
3507 case WXGRID_CURSOR_SELECT_ROW
:
3509 DoColHeaderClick(col
);
3513 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow());
3517 // ------------ Right button down
3519 else if ( event
.RightDown() )
3522 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
3524 // no default action at the moment
3528 // ------------ Right double click
3530 else if ( event
.RightDClick() )
3533 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
3535 // no default action at the moment
3539 // ------------ No buttons down and mouse moving
3541 else if ( event
.Moving() )
3543 m_dragRowOrCol
= XToEdgeOfCol( x
);
3544 if ( m_dragRowOrCol
>= 0 )
3546 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3548 if ( CanDragColSize(m_dragRowOrCol
) )
3549 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, GetColLabelWindow(), false);
3552 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3554 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, GetColLabelWindow(), false);
3559 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
3561 if ( event
.LeftDown() )
3563 // indicate corner label by having both row and
3566 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
3571 else if ( event
.LeftDClick() )
3573 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
3575 else if ( event
.RightDown() )
3577 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
3579 // no default action at the moment
3582 else if ( event
.RightDClick() )
3584 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
3586 // no default action at the moment
3591 void wxGrid::CancelMouseCapture()
3593 // cancel operation currently in progress, whatever it is
3596 m_isDragging
= false;
3597 m_startDragPos
= wxDefaultPosition
;
3599 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3600 m_winCapture
->SetCursor( *wxSTANDARD_CURSOR
);
3601 m_winCapture
= NULL
;
3603 // remove traces of whatever we drew on screen
3608 void wxGrid::ChangeCursorMode(CursorMode mode
,
3613 static const wxChar
*const cursorModes
[] =
3623 wxLogTrace(wxT("grid"),
3624 wxT("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
3625 win
== m_colWindow
? wxT("colLabelWin")
3626 : win
? wxT("rowLabelWin")
3628 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
3629 #endif // wxUSE_LOG_TRACE
3631 if ( mode
== m_cursorMode
&&
3632 win
== m_winCapture
&&
3633 captureMouse
== (m_winCapture
!= NULL
))
3638 // by default use the grid itself
3644 m_winCapture
->ReleaseMouse();
3645 m_winCapture
= NULL
;
3648 m_cursorMode
= mode
;
3650 switch ( m_cursorMode
)
3652 case WXGRID_CURSOR_RESIZE_ROW
:
3653 win
->SetCursor( m_rowResizeCursor
);
3656 case WXGRID_CURSOR_RESIZE_COL
:
3657 win
->SetCursor( m_colResizeCursor
);
3660 case WXGRID_CURSOR_MOVE_COL
:
3661 win
->SetCursor( wxCursor(wxCURSOR_HAND
) );
3665 win
->SetCursor( *wxSTANDARD_CURSOR
);
3669 // we need to capture mouse when resizing
3670 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
3671 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
3673 if ( captureMouse
&& resize
)
3675 win
->CaptureMouse();
3680 // ----------------------------------------------------------------------------
3681 // grid mouse event processing
3682 // ----------------------------------------------------------------------------
3685 wxGrid::DoGridCellDrag(wxMouseEvent
& event
,
3686 const wxGridCellCoords
& coords
,
3689 if ( coords
== wxGridNoCellCoords
)
3690 return; // we're outside any valid cell
3692 // Hide the edit control, so it won't interfere with drag-shrinking.
3693 if ( IsCellEditControlShown() )
3695 HideCellEditControl();
3696 SaveEditControlValue();
3699 switch ( event
.GetModifiers() )
3702 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3703 m_selectedBlockCorner
= coords
;
3704 UpdateBlockBeingSelected(m_selectedBlockCorner
, coords
);
3708 if ( CanDragCell() )
3712 if ( m_selectedBlockCorner
== wxGridNoCellCoords
)
3713 m_selectedBlockCorner
= coords
;
3715 SendEvent(wxEVT_GRID_CELL_BEGIN_DRAG
, coords
, event
);
3720 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
3724 // we don't handle the other key modifiers
3729 void wxGrid::DoGridLineDrag(wxMouseEvent
& event
, const wxGridOperations
& oper
)
3731 wxClientDC
dc(m_gridWin
);
3733 dc
.SetLogicalFunction(wxINVERT
);
3735 const wxRect
rectWin(CalcUnscrolledPosition(wxPoint(0, 0)),
3736 m_gridWin
->GetClientSize());
3738 // erase the previously drawn line, if any
3739 if ( m_dragLastPos
>= 0 )
3740 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3742 // we need the vertical position for rows and horizontal for columns here
3743 m_dragLastPos
= oper
.Dual().Select(CalcUnscrolledPosition(event
.GetPosition()));
3745 // don't allow resizing beneath the minimal size
3746 const int posMin
= oper
.GetLineStartPos(this, m_dragRowOrCol
) +
3747 oper
.GetMinimalLineSize(this, m_dragRowOrCol
);
3748 if ( m_dragLastPos
< posMin
)
3749 m_dragLastPos
= posMin
;
3751 // and draw it at the new position
3752 oper
.DrawParallelLineInRect(dc
, rectWin
, m_dragLastPos
);
3755 void wxGrid::DoGridDragEvent(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3757 if ( !m_isDragging
)
3759 // Don't start doing anything until the mouse has been dragged far
3761 const wxPoint
& pt
= event
.GetPosition();
3762 if ( m_startDragPos
== wxDefaultPosition
)
3764 m_startDragPos
= pt
;
3768 if ( abs(m_startDragPos
.x
- pt
.x
) <= DRAG_SENSITIVITY
&&
3769 abs(m_startDragPos
.y
- pt
.y
) <= DRAG_SENSITIVITY
)
3773 const bool isFirstDrag
= !m_isDragging
;
3774 m_isDragging
= true;
3776 switch ( m_cursorMode
)
3778 case WXGRID_CURSOR_SELECT_CELL
:
3779 DoGridCellDrag(event
, coords
, isFirstDrag
);
3782 case WXGRID_CURSOR_RESIZE_ROW
:
3783 DoGridLineDrag(event
, wxGridRowOperations());
3786 case WXGRID_CURSOR_RESIZE_COL
:
3787 DoGridLineDrag(event
, wxGridColumnOperations());
3796 wxASSERT_MSG( !m_winCapture
, "shouldn't capture the mouse twice" );
3798 m_winCapture
= m_gridWin
;
3799 m_winCapture
->CaptureMouse();
3804 wxGrid::DoGridCellLeftDown(wxMouseEvent
& event
,
3805 const wxGridCellCoords
& coords
,
3808 if ( SendEvent(wxEVT_GRID_CELL_LEFT_CLICK
, coords
, event
) )
3810 // event handled by user code, no need to do anything here
3814 if ( !event
.CmdDown() )
3817 if ( event
.ShiftDown() )
3821 m_selection
->SelectBlock(m_currentCellCoords
, coords
, event
);
3822 m_selectedBlockCorner
= coords
;
3825 else if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3827 DisableCellEditControl();
3828 MakeCellVisible( coords
);
3830 if ( event
.CmdDown() )
3834 m_selection
->ToggleCellSelection(coords
, event
);
3837 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3838 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3839 m_selectedBlockCorner
= coords
;
3845 // In row or column selection mode just clicking on the cell
3846 // should select the row or column containing it: this is more
3847 // convenient for the kinds of controls that use such selection
3848 // mode and is compatible with 2.8 behaviour (see #12062).
3849 switch ( m_selection
->GetSelectionMode() )
3851 case wxGridSelectCells
:
3852 case wxGridSelectRowsOrColumns
:
3853 // nothing to do in these cases
3856 case wxGridSelectRows
:
3857 m_selection
->SelectRow(coords
.GetRow());
3860 case wxGridSelectColumns
:
3861 m_selection
->SelectCol(coords
.GetCol());
3866 m_waitForSlowClick
= m_currentCellCoords
== coords
&&
3867 coords
!= wxGridNoCellCoords
;
3868 SetCurrentCell( coords
);
3874 wxGrid::DoGridCellLeftDClick(wxMouseEvent
& event
,
3875 const wxGridCellCoords
& coords
,
3878 if ( XToEdgeOfCol(pos
.x
) < 0 && YToEdgeOfRow(pos
.y
) < 0 )
3880 if ( !SendEvent(wxEVT_GRID_CELL_LEFT_DCLICK
, coords
, event
) )
3882 // we want double click to select a cell and start editing
3883 // (i.e. to behave in same way as sequence of two slow clicks):
3884 m_waitForSlowClick
= true;
3890 wxGrid::DoGridCellLeftUp(wxMouseEvent
& event
, const wxGridCellCoords
& coords
)
3892 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3896 m_winCapture
->ReleaseMouse();
3897 m_winCapture
= NULL
;
3900 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
3903 EnableCellEditControl();
3905 wxGridCellAttr
*attr
= GetCellAttr(coords
);
3906 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
3907 editor
->StartingClick();
3911 m_waitForSlowClick
= false;
3913 else if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
3914 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
3918 m_selection
->SelectBlock( m_selectedBlockTopLeft
,
3919 m_selectedBlockBottomRight
,
3923 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
3924 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
3926 // Show the edit control, if it has been hidden for
3928 ShowCellEditControl();
3931 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3933 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3934 DoEndDragResizeRow(event
);
3936 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
3938 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3939 DoEndDragResizeCol(event
);
3946 wxGrid::DoGridMouseMoveEvent(wxMouseEvent
& WXUNUSED(event
),
3947 const wxGridCellCoords
& coords
,
3950 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
3952 // out of grid cell area
3953 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3957 int dragRow
= YToEdgeOfRow( pos
.y
);
3958 int dragCol
= XToEdgeOfCol( pos
.x
);
3960 // Dragging on the corner of a cell to resize in both
3961 // directions is not implemented yet...
3963 if ( dragRow
>= 0 && dragCol
>= 0 )
3965 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3969 if ( dragRow
>= 0 && CanDragGridSize() && CanDragRowSize(dragRow
) )
3971 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3973 m_dragRowOrCol
= dragRow
;
3974 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, NULL
, false);
3977 // When using the native header window we can only resize the columns by
3978 // dragging the dividers in it because we can't make it enter into the
3979 // column resizing mode programmatically
3980 else if ( dragCol
>= 0 && !m_useNativeHeader
&&
3981 CanDragGridSize() && CanDragColSize(dragCol
) )
3983 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3985 m_dragRowOrCol
= dragCol
;
3986 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, NULL
, false);
3989 else // Neither on a row or col edge
3991 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3993 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
3998 void wxGrid::ProcessGridCellMouseEvent(wxMouseEvent
& event
)
4000 if ( event
.Entering() || event
.Leaving() )
4002 // we don't care about these events but we must not reset m_isDragging
4003 // if they happen so return before anything else is done
4008 const wxPoint pos
= CalcUnscrolledPosition(event
.GetPosition());
4010 // coordinates of the cell under mouse
4011 wxGridCellCoords coords
= XYToCell(pos
);
4013 int cell_rows
, cell_cols
;
4014 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4015 if ( (cell_rows
< 0) || (cell_cols
< 0) )
4017 coords
.SetRow(coords
.GetRow() + cell_rows
);
4018 coords
.SetCol(coords
.GetCol() + cell_cols
);
4021 if ( event
.Dragging() )
4023 if ( event
.LeftIsDown() )
4024 DoGridDragEvent(event
, coords
);
4030 m_isDragging
= false;
4031 m_startDragPos
= wxDefaultPosition
;
4033 // deal with various button presses
4034 if ( event
.IsButton() )
4036 if ( coords
!= wxGridNoCellCoords
)
4038 DisableCellEditControl();
4040 if ( event
.LeftDown() )
4041 DoGridCellLeftDown(event
, coords
, pos
);
4042 else if ( event
.LeftDClick() )
4043 DoGridCellLeftDClick(event
, coords
, pos
);
4044 else if ( event
.RightDown() )
4045 SendEvent(wxEVT_GRID_CELL_RIGHT_CLICK
, coords
, event
);
4046 else if ( event
.RightDClick() )
4047 SendEvent(wxEVT_GRID_CELL_RIGHT_DCLICK
, coords
, event
);
4050 // this one should be called even if we're not over any cell
4051 if ( event
.LeftUp() )
4053 DoGridCellLeftUp(event
, coords
);
4056 else if ( event
.Moving() )
4058 DoGridMouseMoveEvent(event
, coords
, pos
);
4060 else // unknown mouse event?
4066 // this function returns true only if the size really changed
4067 bool wxGrid::DoEndDragResizeLine(const wxGridOperations
& oper
)
4069 if ( m_dragLastPos
== -1 )
4072 const wxGridOperations
& doper
= oper
.Dual();
4074 const wxSize size
= m_gridWin
->GetClientSize();
4076 const wxPoint ptOrigin
= CalcUnscrolledPosition(wxPoint(0, 0));
4078 // erase the last line we drew
4079 wxClientDC
dc(m_gridWin
);
4081 dc
.SetLogicalFunction(wxINVERT
);
4083 const int posLineStart
= oper
.Select(ptOrigin
);
4084 const int posLineEnd
= oper
.Select(ptOrigin
) + oper
.Select(size
);
4086 oper
.DrawParallelLine(dc
, posLineStart
, posLineEnd
, m_dragLastPos
);
4088 // temporarily hide the edit control before resizing
4089 HideCellEditControl();
4090 SaveEditControlValue();
4092 // do resize the line
4093 const int lineStart
= oper
.GetLineStartPos(this, m_dragRowOrCol
);
4094 const int lineSizeOld
= oper
.GetLineSize(this, m_dragRowOrCol
);
4095 oper
.SetLineSize(this, m_dragRowOrCol
,
4096 wxMax(m_dragLastPos
- lineStart
,
4097 oper
.GetMinimalLineSize(this, m_dragRowOrCol
)));
4099 sizeChanged
= oper
.GetLineSize(this, m_dragRowOrCol
) != lineSizeOld
;
4103 // refresh now if we're not frozen
4104 if ( !GetBatchCount() )
4106 // we need to refresh everything beyond the resized line in the header
4109 // get the position from which to refresh in the other direction
4110 wxRect
rect(CellToRect(oper
.MakeCoords(m_dragRowOrCol
, 0)));
4111 rect
.SetPosition(CalcScrolledPosition(rect
.GetPosition()));
4113 // we only need the ordinate (for rows) or abscissa (for columns) here,
4114 // and need to cover the entire window in the other direction
4115 oper
.Select(rect
) = 0;
4117 wxRect
rectHeader(rect
.GetPosition(),
4120 oper
.GetHeaderWindowSize(this),
4121 doper
.Select(size
) - doper
.Select(rect
)
4124 oper
.GetHeaderWindow(this)->Refresh(true, &rectHeader
);
4127 // also refresh the grid window: extend the rectangle
4130 oper
.SelectSize(rect
) = oper
.Select(size
);
4132 int subtractLines
= 0;
4133 const int lineStart
= oper
.PosToLine(this, posLineStart
);
4134 if ( lineStart
>= 0 )
4136 // ensure that if we have a multi-cell block we redraw all of
4137 // it by increasing the refresh area to cover it entirely if a
4138 // part of it is affected
4139 const int lineEnd
= oper
.PosToLine(this, posLineEnd
, true);
4140 for ( int line
= lineStart
; line
< lineEnd
; line
++ )
4142 int cellLines
= oper
.Select(
4143 GetCellSize(oper
.MakeCoords(m_dragRowOrCol
, line
)));
4144 if ( cellLines
< subtractLines
)
4145 subtractLines
= cellLines
;
4150 oper
.GetLineStartPos(this, m_dragRowOrCol
+ subtractLines
);
4151 startPos
= doper
.CalcScrolledPosition(this, startPos
);
4153 doper
.Select(rect
) = startPos
;
4154 doper
.SelectSize(rect
) = doper
.Select(size
) - startPos
;
4156 m_gridWin
->Refresh(false, &rect
);
4160 // show the edit control back again
4161 ShowCellEditControl();
4166 void wxGrid::DoEndDragResizeRow(const wxMouseEvent
& event
)
4168 // TODO: generate RESIZING event, see #10754
4170 if ( DoEndDragResizeLine(wxGridRowOperations()) )
4171 SendGridSizeEvent(wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4174 void wxGrid::DoEndDragResizeCol(const wxMouseEvent
& event
)
4176 // TODO: generate RESIZING event, see #10754
4178 if ( DoEndDragResizeLine(wxGridColumnOperations()) )
4179 SendGridSizeEvent(wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4182 void wxGrid::DoStartMoveCol(int col
)
4184 m_dragRowOrCol
= col
;
4187 void wxGrid::DoEndMoveCol(int pos
)
4189 wxASSERT_MSG( m_dragRowOrCol
!= -1, "no matching DoStartMoveCol?" );
4191 if ( SendEvent(wxEVT_GRID_COL_MOVE
, -1, m_dragRowOrCol
) != -1 )
4192 SetColPos(m_dragRowOrCol
, pos
);
4193 //else: vetoed by user
4195 m_dragRowOrCol
= -1;
4198 void wxGrid::RefreshAfterColPosChange()
4200 // recalculate the column rights as the column positions have changed,
4201 // unless we calculate them dynamically because all columns widths are the
4202 // same and it's easy to do
4203 if ( !m_colWidths
.empty() )
4206 for ( int colPos
= 0; colPos
< m_numCols
; colPos
++ )
4208 int colID
= GetColAt( colPos
);
4210 colRight
+= m_colWidths
[colID
];
4211 m_colRights
[colID
] = colRight
;
4215 // and make the changes visible
4216 if ( m_useNativeHeader
)
4218 if ( m_colAt
.empty() )
4219 GetGridColHeader()->ResetColumnsOrder();
4221 GetGridColHeader()->SetColumnsOrder(m_colAt
);
4225 m_colWindow
->Refresh();
4227 m_gridWin
->Refresh();
4230 void wxGrid::SetColumnsOrder(const wxArrayInt
& order
)
4234 RefreshAfterColPosChange();
4237 void wxGrid::SetColPos(int idx
, int pos
)
4239 // we're going to need m_colAt now, initialize it if needed
4240 if ( m_colAt
.empty() )
4242 m_colAt
.reserve(m_numCols
);
4243 for ( int i
= 0; i
< m_numCols
; i
++ )
4244 m_colAt
.push_back(i
);
4247 wxHeaderCtrl::MoveColumnInOrderArray(m_colAt
, idx
, pos
);
4249 RefreshAfterColPosChange();
4252 void wxGrid::ResetColPos()
4256 RefreshAfterColPosChange();
4259 void wxGrid::EnableDragColMove( bool enable
)
4261 if ( m_canDragColMove
== enable
)
4264 if ( m_useNativeHeader
)
4266 // update all columns to make them [not] reorderable
4267 GetGridColHeader()->SetColumnCount(m_numCols
);
4270 m_canDragColMove
= enable
;
4272 // we use to call ResetColPos() from here if !enable but this doesn't seem
4273 // right as it would mean there would be no way to "freeze" the current
4274 // columns order by disabling moving them after putting them in the desired
4275 // order, whereas now you can always call ResetColPos() manually if needed
4280 // ------ interaction with data model
4282 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4284 switch ( msg
.GetId() )
4286 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4287 return GetModelValues();
4289 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4290 return SetModelValues();
4292 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4293 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4294 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4295 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4296 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4297 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4298 return Redimension( msg
);
4305 // The behaviour of this function depends on the grid table class
4306 // Clear() function. For the default wxGridStringTable class the
4307 // behaviour is to replace all cell contents with wxEmptyString but
4308 // not to change the number of rows or cols.
4310 void wxGrid::ClearGrid()
4314 if (IsCellEditControlEnabled())
4315 DisableCellEditControl();
4318 if (!GetBatchCount())
4319 m_gridWin
->Refresh();
4324 wxGrid::DoModifyLines(bool (wxGridTableBase::*funcModify
)(size_t, size_t),
4325 int pos
, int num
, bool WXUNUSED(updateLabels
) )
4327 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4332 if ( IsCellEditControlEnabled() )
4333 DisableCellEditControl();
4335 return (m_table
->*funcModify
)(pos
, num
);
4337 // the table will have sent the results of the insert row
4338 // operation to this view object as a grid table message
4342 wxGrid::DoAppendLines(bool (wxGridTableBase::*funcAppend
)(size_t),
4343 int num
, bool WXUNUSED(updateLabels
))
4345 wxCHECK_MSG( m_created
, false, "must finish creating the grid first" );
4350 return (m_table
->*funcAppend
)(num
);
4353 // ----------------------------------------------------------------------------
4354 // event generation helpers
4355 // ----------------------------------------------------------------------------
4358 wxGrid::SendGridSizeEvent(wxEventType type
,
4360 const wxMouseEvent
& mouseEv
)
4362 int rowOrCol
= row
== -1 ? col
: row
;
4364 wxGridSizeEvent
gridEvt( GetId(),
4368 mouseEv
.GetX() + GetRowLabelSize(),
4369 mouseEv
.GetY() + GetColLabelSize(),
4372 GetEventHandler()->ProcessEvent(gridEvt
);
4375 // Generate a grid event based on a mouse event and return:
4376 // -1 if the event was vetoed
4377 // +1 if the event was processed (but not vetoed)
4378 // 0 if the event wasn't handled
4380 wxGrid::SendEvent(const wxEventType type
,
4382 const wxMouseEvent
& mouseEv
)
4384 bool claimed
, vetoed
;
4386 if ( type
== wxEVT_GRID_RANGE_SELECT
)
4388 // Right now, it should _never_ end up here!
4389 wxGridRangeSelectEvent
gridEvt( GetId(),
4392 m_selectedBlockTopLeft
,
4393 m_selectedBlockBottomRight
,
4397 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4398 vetoed
= !gridEvt
.IsAllowed();
4400 else if ( type
== wxEVT_GRID_LABEL_LEFT_CLICK
||
4401 type
== wxEVT_GRID_LABEL_LEFT_DCLICK
||
4402 type
== wxEVT_GRID_LABEL_RIGHT_CLICK
||
4403 type
== wxEVT_GRID_LABEL_RIGHT_DCLICK
)
4405 wxPoint pos
= mouseEv
.GetPosition();
4407 if ( mouseEv
.GetEventObject() == GetGridRowLabelWindow() )
4408 pos
.y
+= GetColLabelSize();
4409 if ( mouseEv
.GetEventObject() == GetGridColLabelWindow() )
4410 pos
.x
+= GetRowLabelSize();
4412 wxGridEvent
gridEvt( GetId(),
4420 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4421 vetoed
= !gridEvt
.IsAllowed();
4425 wxGridEvent
gridEvt( GetId(),
4429 mouseEv
.GetX() + GetRowLabelSize(),
4430 mouseEv
.GetY() + GetColLabelSize(),
4433 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4434 vetoed
= !gridEvt
.IsAllowed();
4437 // A Veto'd event may not be `claimed' so test this first
4441 return claimed
? 1 : 0;
4444 // Generate a grid event of specified type, return value same as above
4447 wxGrid::SendEvent(const wxEventType type
, int row
, int col
, const wxString
& s
)
4449 wxGridEvent
gridEvt( GetId(), type
, this, row
, col
);
4450 gridEvt
.SetString(s
);
4452 const bool claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
4454 // A Veto'd event may not be `claimed' so test this first
4455 if ( !gridEvt
.IsAllowed() )
4458 return claimed
? 1 : 0;
4461 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4463 // needed to prevent zillions of paint events on MSW
4467 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
4469 // Don't do anything if between Begin/EndBatch...
4470 // EndBatch() will do all this on the last nested one anyway.
4471 if ( m_created
&& !GetBatchCount() )
4473 // Refresh to get correct scrolled position:
4474 wxScrolledWindow::Refresh(eraseb
, rect
);
4478 int rect_x
, rect_y
, rectWidth
, rectHeight
;
4479 int width_label
, width_cell
, height_label
, height_cell
;
4482 // Copy rectangle can get scroll offsets..
4483 rect_x
= rect
->GetX();
4484 rect_y
= rect
->GetY();
4485 rectWidth
= rect
->GetWidth();
4486 rectHeight
= rect
->GetHeight();
4488 width_label
= m_rowLabelWidth
- rect_x
;
4489 if (width_label
> rectWidth
)
4490 width_label
= rectWidth
;
4492 height_label
= m_colLabelHeight
- rect_y
;
4493 if (height_label
> rectHeight
)
4494 height_label
= rectHeight
;
4496 if (rect_x
> m_rowLabelWidth
)
4498 x
= rect_x
- m_rowLabelWidth
;
4499 width_cell
= rectWidth
;
4504 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
4507 if (rect_y
> m_colLabelHeight
)
4509 y
= rect_y
- m_colLabelHeight
;
4510 height_cell
= rectHeight
;
4515 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
4518 // Paint corner label part intersecting rect.
4519 if ( width_label
> 0 && height_label
> 0 )
4521 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
4522 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
4525 // Paint col labels part intersecting rect.
4526 if ( width_cell
> 0 && height_label
> 0 )
4528 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
4529 m_colWindow
->Refresh(eraseb
, &anotherrect
);
4532 // Paint row labels part intersecting rect.
4533 if ( width_label
> 0 && height_cell
> 0 )
4535 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
4536 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
4539 // Paint cell area part intersecting rect.
4540 if ( width_cell
> 0 && height_cell
> 0 )
4542 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
4543 m_gridWin
->Refresh(eraseb
, &anotherrect
);
4548 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
4549 m_colWindow
->Refresh(eraseb
, NULL
);
4550 m_rowLabelWin
->Refresh(eraseb
, NULL
);
4551 m_gridWin
->Refresh(eraseb
, NULL
);
4556 void wxGrid::OnSize(wxSizeEvent
& WXUNUSED(event
))
4558 if (m_targetWindow
!= this) // check whether initialisation has been done
4560 // reposition our children windows
4565 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4567 if ( m_inOnKeyDown
)
4569 // shouldn't be here - we are going round in circles...
4571 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4574 m_inOnKeyDown
= true;
4576 // propagate the event up and see if it gets processed
4577 wxWindow
*parent
= GetParent();
4578 wxKeyEvent
keyEvt( event
);
4579 keyEvt
.SetEventObject( parent
);
4581 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4583 if (GetLayoutDirection() == wxLayout_RightToLeft
)
4585 if (event
.GetKeyCode() == WXK_RIGHT
)
4586 event
.m_keyCode
= WXK_LEFT
;
4587 else if (event
.GetKeyCode() == WXK_LEFT
)
4588 event
.m_keyCode
= WXK_RIGHT
;
4591 // try local handlers
4592 switch ( event
.GetKeyCode() )
4595 if ( event
.ControlDown() )
4596 MoveCursorUpBlock( event
.ShiftDown() );
4598 MoveCursorUp( event
.ShiftDown() );
4602 if ( event
.ControlDown() )
4603 MoveCursorDownBlock( event
.ShiftDown() );
4605 MoveCursorDown( event
.ShiftDown() );
4609 if ( event
.ControlDown() )
4610 MoveCursorLeftBlock( event
.ShiftDown() );
4612 MoveCursorLeft( event
.ShiftDown() );
4616 if ( event
.ControlDown() )
4617 MoveCursorRightBlock( event
.ShiftDown() );
4619 MoveCursorRight( event
.ShiftDown() );
4623 case WXK_NUMPAD_ENTER
:
4624 if ( event
.ControlDown() )
4626 event
.Skip(); // to let the edit control have the return
4630 if ( GetGridCursorRow() < GetNumberRows()-1 )
4632 MoveCursorDown( event
.ShiftDown() );
4636 // at the bottom of a column
4637 DisableCellEditControl();
4647 if (event
.ShiftDown())
4649 if ( GetGridCursorCol() > 0 )
4651 MoveCursorLeft( false );
4656 DisableCellEditControl();
4661 if ( GetGridCursorCol() < GetNumberCols() - 1 )
4663 MoveCursorRight( false );
4668 DisableCellEditControl();
4674 GoToCell(event
.ControlDown() ? 0
4675 : m_currentCellCoords
.GetRow(),
4680 GoToCell(event
.ControlDown() ? m_numRows
- 1
4681 : m_currentCellCoords
.GetRow(),
4694 // Ctrl-Space selects the current column, Shift-Space -- the
4695 // current row and Ctrl-Shift-Space -- everything
4696 switch ( m_selection
? event
.GetModifiers() : wxMOD_NONE
)
4699 m_selection
->SelectCol(m_currentCellCoords
.GetCol());
4703 m_selection
->SelectRow(m_currentCellCoords
.GetRow());
4706 case wxMOD_CONTROL
| wxMOD_SHIFT
:
4707 m_selection
->SelectBlock(0, 0,
4708 m_numRows
- 1, m_numCols
- 1);
4712 if ( !IsEditable() )
4714 MoveCursorRight(false);
4717 //else: fall through
4730 m_inOnKeyDown
= false;
4733 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
4735 // try local handlers
4737 if ( event
.GetKeyCode() == WXK_SHIFT
)
4739 if ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
4740 m_selectedBlockBottomRight
!= wxGridNoCellCoords
)
4744 m_selection
->SelectBlock(
4745 m_selectedBlockTopLeft
,
4746 m_selectedBlockBottomRight
,
4751 m_selectedBlockTopLeft
= wxGridNoCellCoords
;
4752 m_selectedBlockBottomRight
= wxGridNoCellCoords
;
4753 m_selectedBlockCorner
= wxGridNoCellCoords
;
4757 void wxGrid::OnChar( wxKeyEvent
& event
)
4759 // is it possible to edit the current cell at all?
4760 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
4762 // yes, now check whether the cells editor accepts the key
4763 int row
= m_currentCellCoords
.GetRow();
4764 int col
= m_currentCellCoords
.GetCol();
4765 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
4766 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
4768 // <F2> is special and will always start editing, for
4769 // other keys - ask the editor itself
4770 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
4771 || editor
->IsAcceptedKey(event
) )
4773 // ensure cell is visble
4774 MakeCellVisible(row
, col
);
4775 EnableCellEditControl();
4777 // a problem can arise if the cell is not completely
4778 // visible (even after calling MakeCellVisible the
4779 // control is not created and calling StartingKey will
4781 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
4782 editor
->StartingKey(event
);
4798 void wxGrid::OnEraseBackground(wxEraseEvent
&)
4802 bool wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
4804 if ( SendEvent(wxEVT_GRID_SELECT_CELL
, coords
) == -1 )
4806 // the event has been vetoed - do nothing
4810 #if !defined(__WXMAC__)
4811 wxClientDC
dc( m_gridWin
);
4815 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
4817 DisableCellEditControl();
4819 if ( IsVisible( m_currentCellCoords
, false ) )
4822 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
4823 if ( !m_gridLinesEnabled
)
4831 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
4833 // Otherwise refresh redraws the highlight!
4834 m_currentCellCoords
= coords
;
4836 #if defined(__WXMAC__)
4837 m_gridWin
->Refresh(true /*, & r */);
4839 DrawGridCellArea( dc
, cells
);
4840 DrawAllGridLines( dc
, r
);
4845 m_currentCellCoords
= coords
;
4847 wxGridCellAttr
*attr
= GetCellAttr( coords
);
4848 #if !defined(__WXMAC__)
4849 DrawCellHighlight( dc
, attr
);
4857 wxGrid::UpdateBlockBeingSelected(int topRow
, int leftCol
,
4858 int bottomRow
, int rightCol
)
4862 switch ( m_selection
->GetSelectionMode() )
4865 wxFAIL_MSG( "unknown selection mode" );
4868 case wxGridSelectCells
:
4869 // arbitrary blocks selection allowed so just use the cell
4870 // coordinates as is
4873 case wxGridSelectRows
:
4874 // only full rows selection allowd, ensure that we do select
4877 rightCol
= GetNumberCols() - 1;
4880 case wxGridSelectColumns
:
4881 // same as above but for columns
4883 bottomRow
= GetNumberRows() - 1;
4886 case wxGridSelectRowsOrColumns
:
4887 // in this mode we can select only full rows or full columns so
4888 // it doesn't make sense to select blocks at all (and we can't
4889 // extend the block because there is no preferred direction, we
4890 // could only extend it to cover the entire grid but this is
4896 m_selectedBlockCorner
= wxGridCellCoords(bottomRow
, rightCol
);
4897 MakeCellVisible(m_selectedBlockCorner
);
4899 EnsureFirstLessThanSecond(topRow
, bottomRow
);
4900 EnsureFirstLessThanSecond(leftCol
, rightCol
);
4902 wxGridCellCoords updateTopLeft
= wxGridCellCoords(topRow
, leftCol
),
4903 updateBottomRight
= wxGridCellCoords(bottomRow
, rightCol
);
4905 // First the case that we selected a completely new area
4906 if ( m_selectedBlockTopLeft
== wxGridNoCellCoords
||
4907 m_selectedBlockBottomRight
== wxGridNoCellCoords
)
4910 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
4911 wxGridCellCoords ( bottomRow
, rightCol
) );
4912 m_gridWin
->Refresh( false, &rect
);
4915 // Now handle changing an existing selection area.
4916 else if ( m_selectedBlockTopLeft
!= updateTopLeft
||
4917 m_selectedBlockBottomRight
!= updateBottomRight
)
4919 // Compute two optimal update rectangles:
4920 // Either one rectangle is a real subset of the
4921 // other, or they are (almost) disjoint!
4923 bool need_refresh
[4];
4927 need_refresh
[3] = false;
4930 // Store intermediate values
4931 wxCoord oldLeft
= m_selectedBlockTopLeft
.GetCol();
4932 wxCoord oldTop
= m_selectedBlockTopLeft
.GetRow();
4933 wxCoord oldRight
= m_selectedBlockBottomRight
.GetCol();
4934 wxCoord oldBottom
= m_selectedBlockBottomRight
.GetRow();
4936 // Determine the outer/inner coordinates.
4937 EnsureFirstLessThanSecond(oldLeft
, leftCol
);
4938 EnsureFirstLessThanSecond(oldTop
, topRow
);
4939 EnsureFirstLessThanSecond(rightCol
, oldRight
);
4940 EnsureFirstLessThanSecond(bottomRow
, oldBottom
);
4942 // Now, either the stuff marked old is the outer
4943 // rectangle or we don't have a situation where one
4944 // is contained in the other.
4946 if ( oldLeft
< leftCol
)
4948 // Refresh the newly selected or deselected
4949 // area to the left of the old or new selection.
4950 need_refresh
[0] = true;
4951 rect
[0] = BlockToDeviceRect(
4952 wxGridCellCoords( oldTop
, oldLeft
),
4953 wxGridCellCoords( oldBottom
, leftCol
- 1 ) );
4956 if ( oldTop
< topRow
)
4958 // Refresh the newly selected or deselected
4959 // area above the old or new selection.
4960 need_refresh
[1] = true;
4961 rect
[1] = BlockToDeviceRect(
4962 wxGridCellCoords( oldTop
, leftCol
),
4963 wxGridCellCoords( topRow
- 1, rightCol
) );
4966 if ( oldRight
> rightCol
)
4968 // Refresh the newly selected or deselected
4969 // area to the right of the old or new selection.
4970 need_refresh
[2] = true;
4971 rect
[2] = BlockToDeviceRect(
4972 wxGridCellCoords( oldTop
, rightCol
+ 1 ),
4973 wxGridCellCoords( oldBottom
, oldRight
) );
4976 if ( oldBottom
> bottomRow
)
4978 // Refresh the newly selected or deselected
4979 // area below the old or new selection.
4980 need_refresh
[3] = true;
4981 rect
[3] = BlockToDeviceRect(
4982 wxGridCellCoords( bottomRow
+ 1, leftCol
),
4983 wxGridCellCoords( oldBottom
, rightCol
) );
4986 // various Refresh() calls
4987 for (i
= 0; i
< 4; i
++ )
4988 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
4989 m_gridWin
->Refresh( false, &(rect
[i
]) );
4993 m_selectedBlockTopLeft
= updateTopLeft
;
4994 m_selectedBlockBottomRight
= updateBottomRight
;
4998 // ------ functions to get/send data (see also public functions)
5001 bool wxGrid::GetModelValues()
5003 // Hide the editor, so it won't hide a changed value.
5004 HideCellEditControl();
5008 // all we need to do is repaint the grid
5010 m_gridWin
->Refresh();
5017 bool wxGrid::SetModelValues()
5021 // Disable the editor, so it won't hide a changed value.
5022 // Do we also want to save the current value of the editor first?
5024 DisableCellEditControl();
5028 for ( row
= 0; row
< m_numRows
; row
++ )
5030 for ( col
= 0; col
< m_numCols
; col
++ )
5032 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5042 // Note - this function only draws cells that are in the list of
5043 // exposed cells (usually set from the update region by
5044 // CalcExposedCells)
5046 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5048 if ( !m_numRows
|| !m_numCols
)
5051 int i
, numCells
= cells
.GetCount();
5052 int row
, col
, cell_rows
, cell_cols
;
5053 wxGridCellCoordsArray redrawCells
;
5055 for ( i
= numCells
- 1; i
>= 0; i
-- )
5057 row
= cells
[i
].GetRow();
5058 col
= cells
[i
].GetCol();
5059 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5061 // If this cell is part of a multicell block, find owner for repaint
5062 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5064 wxGridCellCoords
cell( row
+ cell_rows
, col
+ cell_cols
);
5065 bool marked
= false;
5066 for ( int j
= 0; j
< numCells
; j
++ )
5068 if ( cell
== cells
[j
] )
5077 int count
= redrawCells
.GetCount();
5078 for (int j
= 0; j
< count
; j
++)
5080 if ( cell
== redrawCells
[j
] )
5088 redrawCells
.Add( cell
);
5091 // don't bother drawing this cell
5095 // If this cell is empty, find cell to left that might want to overflow
5096 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
5098 for ( int l
= 0; l
< cell_rows
; l
++ )
5100 // find a cell in this row to leave already marked for repaint
5102 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
5103 if ((redrawCells
[k
].GetCol() < left
) &&
5104 (redrawCells
[k
].GetRow() == row
))
5106 left
= redrawCells
[k
].GetCol();
5110 left
= 0; // oh well
5112 for (int j
= col
- 1; j
>= left
; j
--)
5114 if (!m_table
->IsEmptyCell(row
+ l
, j
))
5116 if (GetCellOverflow(row
+ l
, j
))
5118 wxGridCellCoords
cell(row
+ l
, j
);
5119 bool marked
= false;
5121 for (int k
= 0; k
< numCells
; k
++)
5123 if ( cell
== cells
[k
] )
5132 int count
= redrawCells
.GetCount();
5133 for (int k
= 0; k
< count
; k
++)
5135 if ( cell
== redrawCells
[k
] )
5142 redrawCells
.Add( cell
);
5151 DrawCell( dc
, cells
[i
] );
5154 numCells
= redrawCells
.GetCount();
5156 for ( i
= numCells
- 1; i
>= 0; i
-- )
5158 DrawCell( dc
, redrawCells
[i
] );
5162 void wxGrid::DrawGridSpace( wxDC
& dc
)
5165 m_gridWin
->GetClientSize( &cw
, &ch
);
5168 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5170 int rightCol
= m_numCols
> 0 ? GetColRight(GetColAt( m_numCols
- 1 )) : 0;
5171 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
5173 if ( right
> rightCol
|| bottom
> bottomRow
)
5176 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5178 dc
.SetBrush(GetDefaultCellBackgroundColour());
5179 dc
.SetPen( *wxTRANSPARENT_PEN
);
5181 if ( right
> rightCol
)
5183 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5186 if ( bottom
> bottomRow
)
5188 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5193 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5195 int row
= coords
.GetRow();
5196 int col
= coords
.GetCol();
5198 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5201 // we draw the cell border ourselves
5202 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5204 bool isCurrent
= coords
== m_currentCellCoords
;
5206 wxRect rect
= CellToRect( row
, col
);
5208 // if the editor is shown, we should use it and not the renderer
5209 // Note: However, only if it is really _shown_, i.e. not hidden!
5210 if ( isCurrent
&& IsCellEditControlShown() )
5212 // NB: this "#if..." is temporary and fixes a problem where the
5213 // edit control is erased by this code after being rendered.
5214 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
5215 // implicitly, causing this out-of order render.
5216 #if !defined(__WXMAC__)
5217 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5218 editor
->PaintBackground(rect
, attr
);
5224 // but all the rest is drawn by the cell renderer and hence may be customized
5225 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5226 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5233 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5235 // don't show highlight when the grid doesn't have focus
5239 int row
= m_currentCellCoords
.GetRow();
5240 int col
= m_currentCellCoords
.GetCol();
5242 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5245 wxRect rect
= CellToRect(row
, col
);
5247 // hmmm... what could we do here to show that the cell is disabled?
5248 // for now, I just draw a thinner border than for the other ones, but
5249 // it doesn't look really good
5251 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
5255 // The center of the drawn line is where the position/width/height of
5256 // the rectangle is actually at (on wxMSW at least), so the
5257 // size of the rectangle is reduced to compensate for the thickness of
5258 // the line. If this is too strange on non-wxMSW platforms then
5259 // please #ifdef this appropriately.
5260 rect
.x
+= penWidth
/ 2;
5261 rect
.y
+= penWidth
/ 2;
5262 rect
.width
-= penWidth
- 1;
5263 rect
.height
-= penWidth
- 1;
5265 // Now draw the rectangle
5266 // use the cellHighlightColour if the cell is inside a selection, this
5267 // will ensure the cell is always visible.
5268 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
5269 : m_cellHighlightColour
,
5271 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5272 dc
.DrawRectangle(rect
);
5276 wxPen
wxGrid::GetDefaultGridLinePen()
5278 return wxPen(GetGridLineColour());
5281 wxPen
wxGrid::GetRowGridLinePen(int WXUNUSED(row
))
5283 return GetDefaultGridLinePen();
5286 wxPen
wxGrid::GetColGridLinePen(int WXUNUSED(col
))
5288 return GetDefaultGridLinePen();
5291 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5293 int row
= coords
.GetRow();
5294 int col
= coords
.GetCol();
5295 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5299 wxRect rect
= CellToRect( row
, col
);
5301 // right hand border
5302 dc
.SetPen( GetColGridLinePen(col
) );
5303 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
5304 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
5307 dc
.SetPen( GetRowGridLinePen(row
) );
5308 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
5309 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
5312 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
5314 // This if block was previously in wxGrid::OnPaint but that doesn't
5315 // seem to get called under wxGTK - MB
5317 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5318 m_numRows
&& m_numCols
)
5320 m_currentCellCoords
.Set(0, 0);
5323 if ( IsCellEditControlShown() )
5325 // don't show highlight when the edit control is shown
5329 // if the active cell was repainted, repaint its highlight too because it
5330 // might have been damaged by the grid lines
5331 size_t count
= cells
.GetCount();
5332 for ( size_t n
= 0; n
< count
; n
++ )
5334 wxGridCellCoords cell
= cells
[n
];
5336 // If we are using attributes, then we may have just exposed another
5337 // cell in a partially-visible merged cluster of cells. If the "anchor"
5338 // (upper left) cell of this merged cluster is the cell indicated by
5339 // m_currentCellCoords, then we need to refresh the cell highlight even
5340 // though the "anchor" itself is not part of our update segment.
5341 if ( CanHaveAttributes() )
5345 GetCellSize(cell
.GetRow(), cell
.GetCol(), &rows
, &cols
);
5348 cell
.SetRow(cell
.GetRow() + rows
);
5351 cell
.SetCol(cell
.GetCol() + cols
);
5354 if ( cell
== m_currentCellCoords
)
5356 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5357 DrawCellHighlight(dc
, attr
);
5365 // This is used to redraw all grid lines e.g. when the grid line colour
5368 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
5370 if ( !m_gridLinesEnabled
)
5373 int top
, bottom
, left
, right
;
5376 m_gridWin
->GetClientSize(&cw
, &ch
);
5377 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5378 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5380 // avoid drawing grid lines past the last row and col
5381 if ( m_gridLinesClipHorz
)
5386 const int lastColRight
= GetColRight(GetColAt(m_numCols
- 1));
5387 if ( right
> lastColRight
)
5388 right
= lastColRight
;
5391 if ( m_gridLinesClipVert
)
5396 const int lastRowBottom
= GetRowBottom(m_numRows
- 1);
5397 if ( bottom
> lastRowBottom
)
5398 bottom
= lastRowBottom
;
5401 // no gridlines inside multicells, clip them out
5402 int leftCol
= GetColPos( internalXToCol(left
) );
5403 int topRow
= internalYToRow(top
);
5404 int rightCol
= GetColPos( internalXToCol(right
) );
5405 int bottomRow
= internalYToRow(bottom
);
5407 wxRegion
clippedcells(0, 0, cw
, ch
);
5409 int cell_rows
, cell_cols
;
5412 for ( int j
= topRow
; j
<= bottomRow
; j
++ )
5414 for ( int colPos
= leftCol
; colPos
<= rightCol
; colPos
++ )
5416 int i
= GetColAt( colPos
);
5418 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
5419 if ((cell_rows
> 1) || (cell_cols
> 1))
5421 rect
= CellToRect(j
,i
);
5422 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5423 clippedcells
.Subtract(rect
);
5425 else if ((cell_rows
< 0) || (cell_cols
< 0))
5427 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
5428 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5429 clippedcells
.Subtract(rect
);
5434 dc
.SetDeviceClippingRegion( clippedcells
);
5437 // horizontal grid lines
5438 for ( int i
= internalYToRow(top
); i
< m_numRows
; i
++ )
5440 int bot
= GetRowBottom(i
) - 1;
5447 dc
.SetPen( GetRowGridLinePen(i
) );
5448 dc
.DrawLine( left
, bot
, right
, bot
);
5452 // vertical grid lines
5453 for ( int colPos
= leftCol
; colPos
< m_numCols
; colPos
++ )
5455 int i
= GetColAt( colPos
);
5457 int colRight
= GetColRight(i
);
5459 if (GetLayoutDirection() != wxLayout_RightToLeft
)
5463 if ( colRight
> right
)
5466 if ( colRight
>= left
)
5468 dc
.SetPen( GetColGridLinePen(i
) );
5469 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5473 dc
.DestroyClippingRegion();
5476 void wxGrid::DrawRowLabels( wxDC
& dc
, const wxArrayInt
& rows
)
5481 const size_t numLabels
= rows
.GetCount();
5482 for ( size_t i
= 0; i
< numLabels
; i
++ )
5484 DrawRowLabel( dc
, rows
[i
] );
5488 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5490 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
5493 wxGridCellAttrProvider
* const
5494 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5496 // notice that an explicit static_cast is needed to avoid a compilation
5497 // error with VC7.1 which, for some reason, tries to instantiate (abstract)
5498 // wxGridRowHeaderRenderer class without it
5499 const wxGridRowHeaderRenderer
&
5500 rend
= attrProvider
? attrProvider
->GetRowHeaderRenderer(row
)
5501 : static_cast<const wxGridRowHeaderRenderer
&>
5502 (gs_defaultHeaderRenderers
.rowRenderer
);
5504 wxRect
rect(0, GetRowTop(row
), m_rowLabelWidth
, GetRowHeight(row
));
5505 rend
.DrawBorder(*this, dc
, rect
);
5508 GetRowLabelAlignment(&hAlign
, &vAlign
);
5510 rend
.DrawLabel(*this, dc
, GetRowLabelValue(row
),
5511 rect
, hAlign
, vAlign
, wxHORIZONTAL
);
5514 void wxGrid::UseNativeColHeader(bool native
)
5516 if ( native
== m_useNativeHeader
)
5520 m_useNativeHeader
= native
;
5522 CreateColumnWindow();
5524 if ( m_useNativeHeader
)
5525 GetGridColHeader()->SetColumnCount(m_numCols
);
5529 void wxGrid::SetUseNativeColLabels( bool native
)
5531 wxASSERT_MSG( !m_useNativeHeader
,
5532 "doesn't make sense when using native header" );
5534 m_nativeColumnLabels
= native
;
5537 int height
= wxRendererNative::Get().GetHeaderButtonHeight( this );
5538 SetColLabelSize( height
);
5541 GetColLabelWindow()->Refresh();
5542 m_cornerLabelWin
->Refresh();
5545 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
5550 const size_t numLabels
= cols
.GetCount();
5551 for ( size_t i
= 0; i
< numLabels
; i
++ )
5553 DrawColLabel( dc
, cols
[i
] );
5557 void wxGrid::DrawCornerLabel(wxDC
& dc
)
5559 wxRect
rect(wxSize(m_rowLabelWidth
, m_colLabelHeight
));
5561 if ( m_nativeColumnLabels
)
5565 wxRendererNative::Get().DrawHeaderButton(m_cornerLabelWin
, dc
, rect
, 0);
5572 wxGridCellAttrProvider
* const
5573 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5574 const wxGridCornerHeaderRenderer
&
5575 rend
= attrProvider
? attrProvider
->GetCornerRenderer()
5576 : static_cast<wxGridCornerHeaderRenderer
&>
5577 (gs_defaultHeaderRenderers
.cornerRenderer
);
5579 rend
.DrawBorder(*this, dc
, rect
);
5583 void wxGrid::DrawColLabel(wxDC
& dc
, int col
)
5585 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
5588 int colLeft
= GetColLeft(col
);
5590 wxRect
rect(colLeft
, 0, GetColWidth(col
), m_colLabelHeight
);
5591 wxGridCellAttrProvider
* const
5592 attrProvider
= m_table
? m_table
->GetAttrProvider() : NULL
;
5593 const wxGridColumnHeaderRenderer
&
5594 rend
= attrProvider
? attrProvider
->GetColumnHeaderRenderer(col
)
5595 : static_cast<wxGridColumnHeaderRenderer
&>
5596 (gs_defaultHeaderRenderers
.colRenderer
);
5598 if ( m_nativeColumnLabels
)
5600 wxRendererNative::Get().DrawHeaderButton
5602 GetColLabelWindow(),
5607 ? IsSortOrderAscending()
5608 ? wxHDR_SORT_ICON_UP
5609 : wxHDR_SORT_ICON_DOWN
5610 : wxHDR_SORT_ICON_NONE
5616 // It is reported that we need to erase the background to avoid display
5617 // artefacts, see #12055.
5618 wxDCBrushChanger
setBrush(dc
, m_colWindow
->GetBackgroundColour());
5619 dc
.DrawRectangle(rect
);
5621 rend
.DrawBorder(*this, dc
, rect
);
5625 GetColLabelAlignment(&hAlign
, &vAlign
);
5626 const int orient
= GetColLabelTextOrientation();
5628 rend
.DrawLabel(*this, dc
, GetColLabelValue(col
), rect
, hAlign
, vAlign
, orient
);
5631 // TODO: these 2 functions should be replaced with wxDC::DrawLabel() to which
5632 // we just have to add textOrientation support
5633 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5634 const wxString
& value
,
5638 int textOrientation
) const
5640 wxArrayString lines
;
5642 StringToLines( value
, lines
);
5644 DrawTextRectangle(dc
, lines
, rect
, horizAlign
, vertAlign
, textOrientation
);
5647 void wxGrid::DrawTextRectangle(wxDC
& dc
,
5648 const wxArrayString
& lines
,
5652 int textOrientation
) const
5654 if ( lines
.empty() )
5657 wxDCClipper
clip(dc
, rect
);
5662 if ( textOrientation
== wxHORIZONTAL
)
5663 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5665 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
5669 switch ( vertAlign
)
5671 case wxALIGN_BOTTOM
:
5672 if ( textOrientation
== wxHORIZONTAL
)
5673 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5675 x
= rect
.x
+ rect
.width
- textWidth
;
5678 case wxALIGN_CENTRE
:
5679 if ( textOrientation
== wxHORIZONTAL
)
5680 y
= rect
.y
+ ((rect
.height
- textHeight
) / 2);
5682 x
= rect
.x
+ ((rect
.width
- textWidth
) / 2);
5687 if ( textOrientation
== wxHORIZONTAL
)
5694 // Align each line of a multi-line label
5695 size_t nLines
= lines
.GetCount();
5696 for ( size_t l
= 0; l
< nLines
; l
++ )
5698 const wxString
& line
= lines
[l
];
5702 *(textOrientation
== wxHORIZONTAL
? &y
: &x
) += dc
.GetCharHeight();
5706 wxCoord lineWidth
= 0,
5708 dc
.GetTextExtent(line
, &lineWidth
, &lineHeight
);
5710 switch ( horizAlign
)
5713 if ( textOrientation
== wxHORIZONTAL
)
5714 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
5716 y
= rect
.y
+ lineWidth
+ 1;
5719 case wxALIGN_CENTRE
:
5720 if ( textOrientation
== wxHORIZONTAL
)
5721 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
5723 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
5728 if ( textOrientation
== wxHORIZONTAL
)
5731 y
= rect
.y
+ rect
.height
- 1;
5735 if ( textOrientation
== wxHORIZONTAL
)
5737 dc
.DrawText( line
, x
, y
);
5742 dc
.DrawRotatedText( line
, x
, y
, 90.0 );
5748 // Split multi-line text up into an array of strings.
5749 // Any existing contents of the string array are preserved.
5751 // TODO: refactor wxTextFile::Read() and reuse the same code from here
5752 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
) const
5756 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5757 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5759 while ( startPos
< (int)tVal
.length() )
5761 pos
= tVal
.Mid(startPos
).Find( eol
);
5766 else if ( pos
== 0 )
5768 lines
.Add( wxEmptyString
);
5772 lines
.Add( tVal
.Mid(startPos
, pos
) );
5775 startPos
+= pos
+ 1;
5778 if ( startPos
< (int)tVal
.length() )
5780 lines
.Add( tVal
.Mid( startPos
) );
5784 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
5785 const wxArrayString
& lines
,
5786 long *width
, long *height
) const
5790 wxCoord lineW
= 0, lineH
= 0;
5793 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5795 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5796 w
= wxMax( w
, lineW
);
5805 // ------ Batch processing.
5807 void wxGrid::EndBatch()
5809 if ( m_batchCount
> 0 )
5812 if ( !m_batchCount
)
5815 m_rowLabelWin
->Refresh();
5816 m_colWindow
->Refresh();
5817 m_cornerLabelWin
->Refresh();
5818 m_gridWin
->Refresh();
5823 // Use this, rather than wxWindow::Refresh(), to force an immediate
5824 // repainting of the grid. Has no effect if you are already inside a
5825 // BeginBatch / EndBatch block.
5827 void wxGrid::ForceRefresh()
5833 bool wxGrid::Enable(bool enable
)
5835 if ( !wxScrolledWindow::Enable(enable
) )
5838 // redraw in the new state
5839 m_gridWin
->Refresh();
5845 // ------ Edit control functions
5848 void wxGrid::EnableEditing( bool edit
)
5850 if ( edit
!= m_editable
)
5853 EnableCellEditControl(edit
);
5858 void wxGrid::EnableCellEditControl( bool enable
)
5863 if ( enable
!= m_cellEditCtrlEnabled
)
5867 if ( SendEvent(wxEVT_GRID_EDITOR_SHOWN
) == -1 )
5870 // this should be checked by the caller!
5871 wxASSERT_MSG( CanEnableCellControl(), wxT("can't enable editing for this cell!") );
5873 // do it before ShowCellEditControl()
5874 m_cellEditCtrlEnabled
= enable
;
5876 ShowCellEditControl();
5880 SendEvent(wxEVT_GRID_EDITOR_HIDDEN
);
5882 HideCellEditControl();
5883 SaveEditControlValue();
5885 // do it after HideCellEditControl()
5886 m_cellEditCtrlEnabled
= enable
;
5891 bool wxGrid::IsCurrentCellReadOnly() const
5894 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5895 bool readonly
= attr
->IsReadOnly();
5901 bool wxGrid::CanEnableCellControl() const
5903 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
5904 !IsCurrentCellReadOnly();
5907 bool wxGrid::IsCellEditControlEnabled() const
5909 // the cell edit control might be disable for all cells or just for the
5910 // current one if it's read only
5911 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
5914 bool wxGrid::IsCellEditControlShown() const
5916 bool isShown
= false;
5918 if ( m_cellEditCtrlEnabled
)
5920 int row
= m_currentCellCoords
.GetRow();
5921 int col
= m_currentCellCoords
.GetCol();
5922 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5923 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
5928 if ( editor
->IsCreated() )
5930 isShown
= editor
->GetControl()->IsShown();
5940 void wxGrid::ShowCellEditControl()
5942 if ( IsCellEditControlEnabled() )
5944 if ( !IsVisible( m_currentCellCoords
, false ) )
5946 m_cellEditCtrlEnabled
= false;
5951 wxRect rect
= CellToRect( m_currentCellCoords
);
5952 int row
= m_currentCellCoords
.GetRow();
5953 int col
= m_currentCellCoords
.GetCol();
5955 // if this is part of a multicell, find owner (topleft)
5956 int cell_rows
, cell_cols
;
5957 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
5958 if ( cell_rows
<= 0 || cell_cols
<= 0 )
5962 m_currentCellCoords
.SetRow( row
);
5963 m_currentCellCoords
.SetCol( col
);
5966 // erase the highlight and the cell contents because the editor
5967 // might not cover the entire cell
5968 wxClientDC
dc( m_gridWin
);
5970 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5971 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour()));
5972 dc
.SetPen(*wxTRANSPARENT_PEN
);
5973 dc
.DrawRectangle(rect
);
5975 // convert to scrolled coords
5976 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5982 // cell is shifted by one pixel
5983 // However, don't allow x or y to become negative
5984 // since the SetSize() method interprets that as
5991 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5992 if ( !editor
->IsCreated() )
5994 editor
->Create(m_gridWin
, wxID_ANY
,
5995 new wxGridCellEditorEvtHandler(this, editor
));
5997 wxGridEditorCreatedEvent
evt(GetId(),
5998 wxEVT_GRID_EDITOR_CREATED
,
6002 editor
->GetControl());
6003 GetEventHandler()->ProcessEvent(evt
);
6006 // resize editor to overflow into righthand cells if allowed
6007 int maxWidth
= rect
.width
;
6008 wxString value
= GetCellValue(row
, col
);
6009 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
6012 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
6013 if (maxWidth
< rect
.width
)
6014 maxWidth
= rect
.width
;
6017 int client_right
= m_gridWin
->GetClientSize().GetWidth();
6018 if (rect
.x
+ maxWidth
> client_right
)
6019 maxWidth
= client_right
- rect
.x
;
6021 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
6023 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6024 // may have changed earlier
6025 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
6028 GetCellSize( row
, i
, &c_rows
, &c_cols
);
6030 // looks weird going over a multicell
6031 if (m_table
->IsEmptyCell( row
, i
) &&
6032 (rect
.width
< maxWidth
) && (c_rows
== 1))
6034 rect
.width
+= GetColWidth( i
);
6040 if (rect
.GetRight() > client_right
)
6041 rect
.SetRight( client_right
- 1 );
6044 editor
->SetCellAttr( attr
);
6045 editor
->SetSize( rect
);
6047 editor
->GetControl()->Move(
6048 editor
->GetControl()->GetPosition().x
+ nXMove
,
6049 editor
->GetControl()->GetPosition().y
);
6050 editor
->Show( true, attr
);
6052 // recalc dimensions in case we need to
6053 // expand the scrolled window to account for editor
6056 editor
->BeginEdit(row
, col
, this);
6057 editor
->SetCellAttr(NULL
);
6065 void wxGrid::HideCellEditControl()
6067 if ( IsCellEditControlEnabled() )
6069 int row
= m_currentCellCoords
.GetRow();
6070 int col
= m_currentCellCoords
.GetCol();
6072 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6073 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6074 const bool editorHadFocus
= editor
->GetControl()->HasFocus();
6075 editor
->Show( false );
6079 // return the focus to the grid itself if the editor had it
6081 // note that we must not do this unconditionally to avoid stealing
6082 // focus from the window which just received it if we are hiding the
6083 // editor precisely because we lost focus
6084 if ( editorHadFocus
)
6085 m_gridWin
->SetFocus();
6087 // refresh whole row to the right
6088 wxRect
rect( CellToRect(row
, col
) );
6089 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6090 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
6093 // ensure that the pixels under the focus ring get refreshed as well
6094 rect
.Inflate(10, 10);
6097 m_gridWin
->Refresh( false, &rect
);
6101 void wxGrid::SaveEditControlValue()
6103 if ( IsCellEditControlEnabled() )
6105 int row
= m_currentCellCoords
.GetRow();
6106 int col
= m_currentCellCoords
.GetCol();
6108 wxString oldval
= GetCellValue(row
, col
);
6110 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6111 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6114 bool changed
= editor
->EndEdit(row
, col
, this, oldval
, &newval
);
6116 if ( changed
&& SendEvent(wxEVT_GRID_CELL_CHANGING
, newval
) != -1 )
6118 editor
->ApplyEdit(row
, col
, this);
6120 // for compatibility reasons dating back to wx 2.8 when this event
6121 // was called wxEVT_GRID_CELL_CHANGE and wxEVT_GRID_CELL_CHANGING
6122 // didn't exist we allow vetoing this one too
6123 if ( SendEvent(wxEVT_GRID_CELL_CHANGED
, oldval
) == -1 )
6125 // Event has been vetoed, set the data back.
6126 SetCellValue(row
, col
, oldval
);
6136 // ------ Grid location functions
6137 // Note that all of these functions work with the logical coordinates of
6138 // grid cells and labels so you will need to convert from device
6139 // coordinates for mouse events etc.
6142 wxGridCellCoords
wxGrid::XYToCell(int x
, int y
) const
6144 int row
= YToRow(y
);
6145 int col
= XToCol(x
);
6147 return row
== -1 || col
== -1 ? wxGridNoCellCoords
6148 : wxGridCellCoords(row
, col
);
6151 // compute row or column from some (unscrolled) coordinate value, using either
6152 // m_defaultRowHeight/m_defaultColWidth or binary search on array of
6153 // m_rowBottoms/m_colRights to do it quickly (linear search shouldn't be used
6155 int wxGrid::PosToLinePos(int coord
,
6157 const wxGridOperations
& oper
) const
6159 const int numLines
= oper
.GetNumberOfLines(this);
6162 return clipToMinMax
&& numLines
> 0 ? 0 : wxNOT_FOUND
;
6164 const int defaultLineSize
= oper
.GetDefaultLineSize(this);
6165 wxCHECK_MSG( defaultLineSize
, -1, "can't have 0 default line size" );
6167 int maxPos
= coord
/ defaultLineSize
,
6170 // check for the simplest case: if we have no explicit line sizes
6171 // configured, then we already know the line this position falls in
6172 const wxArrayInt
& lineEnds
= oper
.GetLineEnds(this);
6173 if ( lineEnds
.empty() )
6175 if ( maxPos
< numLines
)
6178 return clipToMinMax
? numLines
- 1 : -1;
6182 // adjust maxPos before starting the binary search
6183 if ( maxPos
>= numLines
)
6185 maxPos
= numLines
- 1;
6189 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
)])
6192 const int minDist
= oper
.GetMinimalAcceptableLineSize(this);
6194 maxPos
= coord
/ minDist
;
6196 maxPos
= numLines
- 1;
6199 if ( maxPos
>= numLines
)
6200 maxPos
= numLines
- 1;
6203 // check if the position is beyond the last column
6204 const int lineAtMaxPos
= oper
.GetLineAt(this, maxPos
);
6205 if ( coord
>= lineEnds
[lineAtMaxPos
] )
6206 return clipToMinMax
? maxPos
: -1;
6208 // or before the first one
6209 const int lineAt0
= oper
.GetLineAt(this, 0);
6210 if ( coord
< lineEnds
[lineAt0
] )
6214 // finally do perform the binary search
6215 while ( minPos
< maxPos
)
6217 wxCHECK_MSG( lineEnds
[oper
.GetLineAt(this, minPos
)] <= coord
&&
6218 coord
< lineEnds
[oper
.GetLineAt(this, maxPos
)],
6220 "wxGrid: internal error in PosToLinePos()" );
6222 if ( coord
>= lineEnds
[oper
.GetLineAt(this, maxPos
- 1)] )
6227 const int median
= minPos
+ (maxPos
- minPos
+ 1) / 2;
6228 if ( coord
< lineEnds
[oper
.GetLineAt(this, median
)] )
6238 wxGrid::PosToLine(int coord
,
6240 const wxGridOperations
& oper
) const
6242 int pos
= PosToLinePos(coord
, clipToMinMax
, oper
);
6244 return pos
== wxNOT_FOUND
? wxNOT_FOUND
: oper
.GetLineAt(this, pos
);
6247 int wxGrid::YToRow(int y
, bool clipToMinMax
) const
6249 return PosToLine(y
, clipToMinMax
, wxGridRowOperations());
6252 int wxGrid::XToCol(int x
, bool clipToMinMax
) const
6254 return PosToLine(x
, clipToMinMax
, wxGridColumnOperations());
6257 int wxGrid::XToPos(int x
) const
6259 return PosToLinePos(x
, true /* clip */, wxGridColumnOperations());
6262 // return the row number such that the y coord is near the edge of, or -1 if
6263 // not near an edge.
6265 // notice that position can only possibly be near an edge if the row/column is
6266 // large enough to still allow for an "inner" area that is _not_ near the edge
6267 // (i.e., if the height/width is smaller than WXGRID_LABEL_EDGE_ZONE, pos will
6268 // _never_ be considered to be near the edge).
6269 int wxGrid::PosToEdgeOfLine(int pos
, const wxGridOperations
& oper
) const
6271 const int line
= oper
.PosToLine(this, pos
, true);
6273 if ( oper
.GetLineSize(this, line
) > WXGRID_LABEL_EDGE_ZONE
)
6275 // We know that we are in this line, test whether we are close enough
6276 // to start or end border, respectively.
6277 if ( abs(oper
.GetLineEndPos(this, line
) - pos
) < WXGRID_LABEL_EDGE_ZONE
)
6279 else if ( line
> 0 &&
6280 pos
- oper
.GetLineStartPos(this,
6281 line
) < WXGRID_LABEL_EDGE_ZONE
)
6288 int wxGrid::YToEdgeOfRow(int y
) const
6290 return PosToEdgeOfLine(y
, wxGridRowOperations());
6293 int wxGrid::XToEdgeOfCol(int x
) const
6295 return PosToEdgeOfLine(x
, wxGridColumnOperations());
6298 wxRect
wxGrid::CellToRect( int row
, int col
) const
6300 wxRect
rect( -1, -1, -1, -1 );
6302 if ( row
>= 0 && row
< m_numRows
&&
6303 col
>= 0 && col
< m_numCols
)
6305 int i
, cell_rows
, cell_cols
;
6306 rect
.width
= rect
.height
= 0;
6307 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6308 // if negative then find multicell owner
6313 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6315 rect
.x
= GetColLeft(col
);
6316 rect
.y
= GetRowTop(row
);
6317 for (i
=col
; i
< col
+ cell_cols
; i
++)
6318 rect
.width
+= GetColWidth(i
);
6319 for (i
=row
; i
< row
+ cell_rows
; i
++)
6320 rect
.height
+= GetRowHeight(i
);
6322 // if grid lines are enabled, then the area of the cell is a bit smaller
6323 if (m_gridLinesEnabled
)
6333 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
) const
6335 // get the cell rectangle in logical coords
6337 wxRect
r( CellToRect( row
, col
) );
6339 // convert to device coords
6341 int left
, top
, right
, bottom
;
6342 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6343 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6345 // check against the client area of the grid window
6347 m_gridWin
->GetClientSize( &cw
, &ch
);
6349 if ( wholeCellVisible
)
6351 // is the cell wholly visible ?
6352 return ( left
>= 0 && right
<= cw
&&
6353 top
>= 0 && bottom
<= ch
);
6357 // is the cell partly visible ?
6359 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6360 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6364 // make the specified cell location visible by doing a minimal amount
6367 void wxGrid::MakeCellVisible( int row
, int col
)
6370 int xpos
= -1, ypos
= -1;
6372 if ( row
>= 0 && row
< m_numRows
&&
6373 col
>= 0 && col
< m_numCols
)
6375 // get the cell rectangle in logical coords
6376 wxRect
r( CellToRect( row
, col
) );
6378 // convert to device coords
6379 int left
, top
, right
, bottom
;
6380 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6381 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6384 m_gridWin
->GetClientSize( &cw
, &ch
);
6390 else if ( bottom
> ch
)
6392 int h
= r
.GetHeight();
6394 for ( i
= row
- 1; i
>= 0; i
-- )
6396 int rowHeight
= GetRowHeight(i
);
6397 if ( h
+ rowHeight
> ch
)
6404 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6405 // have rounding errors (this is important, because if we do,
6406 // we might not scroll at all and some cells won't be redrawn)
6408 // Sometimes GRID_SCROLL_LINE / 2 is not enough,
6409 // so just add a full scroll unit...
6410 ypos
+= m_yScrollPixelsPerLine
;
6413 // special handling for wide cells - show always left part of the cell!
6414 // Otherwise, e.g. when stepping from row to row, it would jump between
6415 // left and right part of the cell on every step!
6417 if ( left
< 0 || (right
- left
) >= cw
)
6421 else if ( right
> cw
)
6423 // position the view so that the cell is on the right
6425 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
6426 xpos
= x0
+ (right
- cw
);
6428 // see comment for ypos above
6429 xpos
+= m_xScrollPixelsPerLine
;
6432 if ( xpos
!= -1 || ypos
!= -1 )
6435 xpos
/= m_xScrollPixelsPerLine
;
6437 ypos
/= m_yScrollPixelsPerLine
;
6438 Scroll( xpos
, ypos
);
6445 // ------ Grid cursor movement functions
6449 wxGrid::DoMoveCursor(bool expandSelection
,
6450 const wxGridDirectionOperations
& diroper
)
6452 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6455 if ( expandSelection
)
6457 wxGridCellCoords coords
= m_selectedBlockCorner
;
6458 if ( coords
== wxGridNoCellCoords
)
6459 coords
= m_currentCellCoords
;
6461 if ( diroper
.IsAtBoundary(coords
) )
6464 diroper
.Advance(coords
);
6466 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6468 else // don't expand selection
6472 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6475 wxGridCellCoords coords
= m_currentCellCoords
;
6476 diroper
.Advance(coords
);
6484 bool wxGrid::MoveCursorUp(bool expandSelection
)
6486 return DoMoveCursor(expandSelection
,
6487 wxGridBackwardOperations(this, wxGridRowOperations()));
6490 bool wxGrid::MoveCursorDown(bool expandSelection
)
6492 return DoMoveCursor(expandSelection
,
6493 wxGridForwardOperations(this, wxGridRowOperations()));
6496 bool wxGrid::MoveCursorLeft(bool expandSelection
)
6498 return DoMoveCursor(expandSelection
,
6499 wxGridBackwardOperations(this, wxGridColumnOperations()));
6502 bool wxGrid::MoveCursorRight(bool expandSelection
)
6504 return DoMoveCursor(expandSelection
,
6505 wxGridForwardOperations(this, wxGridColumnOperations()));
6508 bool wxGrid::DoMoveCursorByPage(const wxGridDirectionOperations
& diroper
)
6510 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6513 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6516 const int oldRow
= m_currentCellCoords
.GetRow();
6517 int newRow
= diroper
.MoveByPixelDistance(oldRow
, m_gridWin
->GetClientSize().y
);
6518 if ( newRow
== oldRow
)
6520 wxGridCellCoords
coords(m_currentCellCoords
);
6521 diroper
.Advance(coords
);
6522 newRow
= coords
.GetRow();
6525 GoToCell(newRow
, m_currentCellCoords
.GetCol());
6530 bool wxGrid::MovePageUp()
6532 return DoMoveCursorByPage(
6533 wxGridBackwardOperations(this, wxGridRowOperations()));
6536 bool wxGrid::MovePageDown()
6538 return DoMoveCursorByPage(
6539 wxGridForwardOperations(this, wxGridRowOperations()));
6542 // helper of DoMoveCursorByBlock(): advance the cell coordinates using diroper
6543 // until we find a non-empty cell or reach the grid end
6545 wxGrid::AdvanceToNextNonEmpty(wxGridCellCoords
& coords
,
6546 const wxGridDirectionOperations
& diroper
)
6548 while ( !diroper
.IsAtBoundary(coords
) )
6550 diroper
.Advance(coords
);
6551 if ( !m_table
->IsEmpty(coords
) )
6557 wxGrid::DoMoveCursorByBlock(bool expandSelection
,
6558 const wxGridDirectionOperations
& diroper
)
6560 if ( !m_table
|| m_currentCellCoords
== wxGridNoCellCoords
)
6563 if ( diroper
.IsAtBoundary(m_currentCellCoords
) )
6566 wxGridCellCoords
coords(m_currentCellCoords
);
6567 if ( m_table
->IsEmpty(coords
) )
6569 // we are in an empty cell: find the next block of non-empty cells
6570 AdvanceToNextNonEmpty(coords
, diroper
);
6572 else // current cell is not empty
6574 diroper
.Advance(coords
);
6575 if ( m_table
->IsEmpty(coords
) )
6577 // we started at the end of a block, find the next one
6578 AdvanceToNextNonEmpty(coords
, diroper
);
6580 else // we're in a middle of a block
6582 // go to the end of it, i.e. find the last cell before the next
6584 while ( !diroper
.IsAtBoundary(coords
) )
6586 wxGridCellCoords
coordsNext(coords
);
6587 diroper
.Advance(coordsNext
);
6588 if ( m_table
->IsEmpty(coordsNext
) )
6591 coords
= coordsNext
;
6596 if ( expandSelection
)
6598 UpdateBlockBeingSelected(m_currentCellCoords
, coords
);
6609 bool wxGrid::MoveCursorUpBlock(bool expandSelection
)
6611 return DoMoveCursorByBlock(
6613 wxGridBackwardOperations(this, wxGridRowOperations())
6617 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6619 return DoMoveCursorByBlock(
6621 wxGridForwardOperations(this, wxGridRowOperations())
6625 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6627 return DoMoveCursorByBlock(
6629 wxGridBackwardOperations(this, wxGridColumnOperations())
6633 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6635 return DoMoveCursorByBlock(
6637 wxGridForwardOperations(this, wxGridColumnOperations())
6642 // ------ Label values and formatting
6645 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
) const
6648 *horiz
= m_rowLabelHorizAlign
;
6650 *vert
= m_rowLabelVertAlign
;
6653 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
) const
6656 *horiz
= m_colLabelHorizAlign
;
6658 *vert
= m_colLabelVertAlign
;
6661 int wxGrid::GetColLabelTextOrientation() const
6663 return m_colLabelTextOrientation
;
6666 wxString
wxGrid::GetRowLabelValue( int row
) const
6670 return m_table
->GetRowLabelValue( row
);
6680 wxString
wxGrid::GetColLabelValue( int col
) const
6684 return m_table
->GetColLabelValue( col
);
6694 void wxGrid::SetRowLabelSize( int width
)
6696 wxASSERT( width
>= 0 || width
== wxGRID_AUTOSIZE
);
6698 if ( width
== wxGRID_AUTOSIZE
)
6700 width
= CalcColOrRowLabelAreaMinSize(wxGRID_ROW
);
6703 if ( width
!= m_rowLabelWidth
)
6707 m_rowLabelWin
->Show( false );
6708 m_cornerLabelWin
->Show( false );
6710 else if ( m_rowLabelWidth
== 0 )
6712 m_rowLabelWin
->Show( true );
6713 if ( m_colLabelHeight
> 0 )
6714 m_cornerLabelWin
->Show( true );
6717 m_rowLabelWidth
= width
;
6719 wxScrolledWindow::Refresh( true );
6723 void wxGrid::SetColLabelSize( int height
)
6725 wxASSERT( height
>=0 || height
== wxGRID_AUTOSIZE
);
6727 if ( height
== wxGRID_AUTOSIZE
)
6729 height
= CalcColOrRowLabelAreaMinSize(wxGRID_COLUMN
);
6732 if ( height
!= m_colLabelHeight
)
6736 m_colWindow
->Show( false );
6737 m_cornerLabelWin
->Show( false );
6739 else if ( m_colLabelHeight
== 0 )
6741 m_colWindow
->Show( true );
6742 if ( m_rowLabelWidth
> 0 )
6743 m_cornerLabelWin
->Show( true );
6746 m_colLabelHeight
= height
;
6748 wxScrolledWindow::Refresh( true );
6752 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6754 if ( m_labelBackgroundColour
!= colour
)
6756 m_labelBackgroundColour
= colour
;
6757 m_rowLabelWin
->SetBackgroundColour( colour
);
6758 m_colWindow
->SetBackgroundColour( colour
);
6759 m_cornerLabelWin
->SetBackgroundColour( colour
);
6761 if ( !GetBatchCount() )
6763 m_rowLabelWin
->Refresh();
6764 m_colWindow
->Refresh();
6765 m_cornerLabelWin
->Refresh();
6770 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6772 if ( m_labelTextColour
!= colour
)
6774 m_labelTextColour
= colour
;
6775 if ( !GetBatchCount() )
6777 m_rowLabelWin
->Refresh();
6778 m_colWindow
->Refresh();
6783 void wxGrid::SetLabelFont( const wxFont
& font
)
6786 if ( !GetBatchCount() )
6788 m_rowLabelWin
->Refresh();
6789 m_colWindow
->Refresh();
6793 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6795 // allow old (incorrect) defs to be used
6798 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6799 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6800 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6805 case wxTOP
: vert
= wxALIGN_TOP
; break;
6806 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6807 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6810 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6812 m_rowLabelHorizAlign
= horiz
;
6815 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6817 m_rowLabelVertAlign
= vert
;
6820 if ( !GetBatchCount() )
6822 m_rowLabelWin
->Refresh();
6826 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6828 // allow old (incorrect) defs to be used
6831 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
6832 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
6833 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
6838 case wxTOP
: vert
= wxALIGN_TOP
; break;
6839 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
6840 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
6843 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
6845 m_colLabelHorizAlign
= horiz
;
6848 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
6850 m_colLabelVertAlign
= vert
;
6853 if ( !GetBatchCount() )
6855 m_colWindow
->Refresh();
6859 // Note: under MSW, the default column label font must be changed because it
6860 // does not support vertical printing
6862 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
6863 // pGrid->SetLabelFont(font);
6864 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
6866 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
6868 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
6869 m_colLabelTextOrientation
= textOrientation
;
6871 if ( !GetBatchCount() )
6872 m_colWindow
->Refresh();
6875 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6879 m_table
->SetRowLabelValue( row
, s
);
6880 if ( !GetBatchCount() )
6882 wxRect rect
= CellToRect( row
, 0 );
6883 if ( rect
.height
> 0 )
6885 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6887 rect
.width
= m_rowLabelWidth
;
6888 m_rowLabelWin
->Refresh( true, &rect
);
6894 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6898 m_table
->SetColLabelValue( col
, s
);
6899 if ( !GetBatchCount() )
6901 if ( m_useNativeHeader
)
6903 GetGridColHeader()->UpdateColumn(col
);
6907 wxRect rect
= CellToRect( 0, col
);
6908 if ( rect
.width
> 0 )
6910 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6912 rect
.height
= m_colLabelHeight
;
6913 GetColLabelWindow()->Refresh( true, &rect
);
6920 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6922 if ( m_gridLineColour
!= colour
)
6924 m_gridLineColour
= colour
;
6926 if ( GridLinesEnabled() )
6931 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
6933 if ( m_cellHighlightColour
!= colour
)
6935 m_cellHighlightColour
= colour
;
6937 wxClientDC
dc( m_gridWin
);
6939 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6940 DrawCellHighlight(dc
, attr
);
6945 void wxGrid::SetCellHighlightPenWidth(int width
)
6947 if (m_cellHighlightPenWidth
!= width
)
6949 m_cellHighlightPenWidth
= width
;
6951 // Just redrawing the cell highlight is not enough since that won't
6952 // make any visible change if the the thickness is getting smaller.
6953 int row
= m_currentCellCoords
.GetRow();
6954 int col
= m_currentCellCoords
.GetCol();
6955 if ( row
== -1 || col
== -1 || GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6958 wxRect rect
= CellToRect(row
, col
);
6959 m_gridWin
->Refresh(true, &rect
);
6963 void wxGrid::SetCellHighlightROPenWidth(int width
)
6965 if (m_cellHighlightROPenWidth
!= width
)
6967 m_cellHighlightROPenWidth
= width
;
6969 // Just redrawing the cell highlight is not enough since that won't
6970 // make any visible change if the the thickness is getting smaller.
6971 int row
= m_currentCellCoords
.GetRow();
6972 int col
= m_currentCellCoords
.GetCol();
6973 if ( row
== -1 || col
== -1 ||
6974 GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6977 wxRect rect
= CellToRect(row
, col
);
6978 m_gridWin
->Refresh(true, &rect
);
6982 void wxGrid::RedrawGridLines()
6984 // the lines will be redrawn when the window is thawn
6985 if ( GetBatchCount() )
6988 if ( GridLinesEnabled() )
6990 wxClientDC
dc( m_gridWin
);
6992 DrawAllGridLines( dc
, wxRegion() );
6994 else // remove the grid lines
6996 m_gridWin
->Refresh();
7000 void wxGrid::EnableGridLines( bool enable
)
7002 if ( enable
!= m_gridLinesEnabled
)
7004 m_gridLinesEnabled
= enable
;
7010 void wxGrid::DoClipGridLines(bool& var
, bool clip
)
7016 if ( GridLinesEnabled() )
7021 int wxGrid::GetDefaultRowSize() const
7023 return m_defaultRowHeight
;
7026 int wxGrid::GetRowSize( int row
) const
7028 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, wxT("invalid row index") );
7030 return GetRowHeight(row
);
7033 int wxGrid::GetDefaultColSize() const
7035 return m_defaultColWidth
;
7038 int wxGrid::GetColSize( int col
) const
7040 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, wxT("invalid column index") );
7042 return GetColWidth(col
);
7045 // ============================================================================
7046 // access to the grid attributes: each of them has a default value in the grid
7047 // itself and may be overidden on a per-cell basis
7048 // ============================================================================
7050 // ----------------------------------------------------------------------------
7051 // setting default attributes
7052 // ----------------------------------------------------------------------------
7054 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7056 m_defaultCellAttr
->SetBackgroundColour(col
);
7058 m_gridWin
->SetBackgroundColour(col
);
7062 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7064 m_defaultCellAttr
->SetTextColour(col
);
7067 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7069 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7072 void wxGrid::SetDefaultCellOverflow( bool allow
)
7074 m_defaultCellAttr
->SetOverflow(allow
);
7077 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7079 m_defaultCellAttr
->SetFont(font
);
7082 // For editors and renderers the type registry takes precedence over the
7083 // default attr, so we need to register the new editor/renderer for the string
7084 // data type in order to make setting a default editor/renderer appear to
7087 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7089 RegisterDataType(wxGRID_VALUE_STRING
,
7091 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
7094 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7096 RegisterDataType(wxGRID_VALUE_STRING
,
7097 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
7101 // ----------------------------------------------------------------------------
7102 // access to the default attributes
7103 // ----------------------------------------------------------------------------
7105 wxColour
wxGrid::GetDefaultCellBackgroundColour() const
7107 return m_defaultCellAttr
->GetBackgroundColour();
7110 wxColour
wxGrid::GetDefaultCellTextColour() const
7112 return m_defaultCellAttr
->GetTextColour();
7115 wxFont
wxGrid::GetDefaultCellFont() const
7117 return m_defaultCellAttr
->GetFont();
7120 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
) const
7122 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7125 bool wxGrid::GetDefaultCellOverflow() const
7127 return m_defaultCellAttr
->GetOverflow();
7130 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7132 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7135 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7137 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
7140 // ----------------------------------------------------------------------------
7141 // access to cell attributes
7142 // ----------------------------------------------------------------------------
7144 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
) const
7146 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7147 wxColour colour
= attr
->GetBackgroundColour();
7153 wxColour
wxGrid::GetCellTextColour( int row
, int col
) const
7155 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7156 wxColour colour
= attr
->GetTextColour();
7162 wxFont
wxGrid::GetCellFont( int row
, int col
) const
7164 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7165 wxFont font
= attr
->GetFont();
7171 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
) const
7173 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7174 attr
->GetAlignment(horiz
, vert
);
7178 bool wxGrid::GetCellOverflow( int row
, int col
) const
7180 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7181 bool allow
= attr
->GetOverflow();
7188 wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
) const
7190 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7191 attr
->GetSize( num_rows
, num_cols
);
7194 if ( *num_rows
== 1 && *num_cols
== 1 )
7195 return CellSpan_None
; // just a normal cell
7197 if ( *num_rows
< 0 || *num_cols
< 0 )
7198 return CellSpan_Inside
; // covered by a multi-span cell
7200 // this cell spans multiple cells to its right/bottom
7201 return CellSpan_Main
;
7204 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
) const
7206 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7207 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7213 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
) const
7215 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7216 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7222 bool wxGrid::IsReadOnly(int row
, int col
) const
7224 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7225 bool isReadOnly
= attr
->IsReadOnly();
7231 // ----------------------------------------------------------------------------
7232 // attribute support: cache, automatic provider creation, ...
7233 // ----------------------------------------------------------------------------
7235 bool wxGrid::CanHaveAttributes() const
7242 return m_table
->CanHaveAttributes();
7245 void wxGrid::ClearAttrCache()
7247 if ( m_attrCache
.row
!= -1 )
7249 wxGridCellAttr
*oldAttr
= m_attrCache
.attr
;
7250 m_attrCache
.attr
= NULL
;
7251 m_attrCache
.row
= -1;
7252 // wxSafeDecRec(...) might cause event processing that accesses
7253 // the cached attribute, if one exists (e.g. by deleting the
7254 // editor stored within the attribute). Therefore it is important
7255 // to invalidate the cache before calling wxSafeDecRef!
7256 wxSafeDecRef(oldAttr
);
7260 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7264 wxGrid
*self
= (wxGrid
*)this; // const_cast
7266 self
->ClearAttrCache();
7267 self
->m_attrCache
.row
= row
;
7268 self
->m_attrCache
.col
= col
;
7269 self
->m_attrCache
.attr
= attr
;
7274 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7276 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7278 *attr
= m_attrCache
.attr
;
7279 wxSafeIncRef(m_attrCache
.attr
);
7281 #ifdef DEBUG_ATTR_CACHE
7282 gs_nAttrCacheHits
++;
7289 #ifdef DEBUG_ATTR_CACHE
7290 gs_nAttrCacheMisses
++;
7297 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7299 wxGridCellAttr
*attr
= NULL
;
7300 // Additional test to avoid looking at the cache e.g. for
7301 // wxNoCellCoords, as this will confuse memory management.
7304 if ( !LookupAttr(row
, col
, &attr
) )
7306 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
7308 CacheAttr(row
, col
, attr
);
7314 attr
->SetDefAttr(m_defaultCellAttr
);
7318 attr
= m_defaultCellAttr
;
7325 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7327 wxGridCellAttr
*attr
= NULL
;
7328 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
7330 wxCHECK_MSG( canHave
, attr
, wxT("Cell attributes not allowed"));
7331 wxCHECK_MSG( m_table
, attr
, wxT("must have a table") );
7333 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7336 attr
= new wxGridCellAttr(m_defaultCellAttr
);
7338 // artificially inc the ref count to match DecRef() in caller
7340 m_table
->SetAttr(attr
, row
, col
);
7346 // ----------------------------------------------------------------------------
7347 // setting column attributes (wrappers around SetColAttr)
7348 // ----------------------------------------------------------------------------
7350 void wxGrid::SetColFormatBool(int col
)
7352 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7355 void wxGrid::SetColFormatNumber(int col
)
7357 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7360 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7362 wxString typeName
= wxGRID_VALUE_FLOAT
;
7363 if ( (width
!= -1) || (precision
!= -1) )
7365 typeName
<< wxT(':') << width
<< wxT(',') << precision
;
7368 SetColFormatCustom(col
, typeName
);
7371 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7373 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
7375 attr
= new wxGridCellAttr
;
7376 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7377 attr
->SetRenderer(renderer
);
7378 wxGridCellEditor
*editor
= GetDefaultEditorForType(typeName
);
7379 attr
->SetEditor(editor
);
7381 SetColAttr(col
, attr
);
7385 // ----------------------------------------------------------------------------
7386 // setting cell attributes: this is forwarded to the table
7387 // ----------------------------------------------------------------------------
7389 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
7391 if ( CanHaveAttributes() )
7393 m_table
->SetAttr(attr
, row
, col
);
7402 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7404 if ( CanHaveAttributes() )
7406 m_table
->SetRowAttr(attr
, row
);
7415 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7417 if ( CanHaveAttributes() )
7419 m_table
->SetColAttr(attr
, col
);
7428 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7430 if ( CanHaveAttributes() )
7432 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7433 attr
->SetBackgroundColour(colour
);
7438 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7440 if ( CanHaveAttributes() )
7442 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7443 attr
->SetTextColour(colour
);
7448 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7450 if ( CanHaveAttributes() )
7452 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7453 attr
->SetFont(font
);
7458 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7460 if ( CanHaveAttributes() )
7462 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7463 attr
->SetAlignment(horiz
, vert
);
7468 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
7470 if ( CanHaveAttributes() )
7472 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7473 attr
->SetOverflow(allow
);
7478 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
7480 if ( CanHaveAttributes() )
7482 int cell_rows
, cell_cols
;
7484 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7485 attr
->GetSize(&cell_rows
, &cell_cols
);
7486 attr
->SetSize(num_rows
, num_cols
);
7489 // Cannot set the size of a cell to 0 or negative values
7490 // While it is perfectly legal to do that, this function cannot
7491 // handle all the possibilies, do it by hand by getting the CellAttr.
7492 // You can only set the size of a cell to 1,1 or greater with this fn
7493 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
7494 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
7495 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
7496 wxT("wxGrid::SetCellSize setting cell size to < 1"));
7498 // if this was already a multicell then "turn off" the other cells first
7499 if ((cell_rows
> 1) || (cell_cols
> 1))
7502 for (j
=row
; j
< row
+ cell_rows
; j
++)
7504 for (i
=col
; i
< col
+ cell_cols
; i
++)
7506 if ((i
!= col
) || (j
!= row
))
7508 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7509 attr_stub
->SetSize( 1, 1 );
7510 attr_stub
->DecRef();
7516 // mark the cells that will be covered by this cell to
7517 // negative or zero values to point back at this cell
7518 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
7521 for (j
=row
; j
< row
+ num_rows
; j
++)
7523 for (i
=col
; i
< col
+ num_cols
; i
++)
7525 if ((i
!= col
) || (j
!= row
))
7527 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
7528 attr_stub
->SetSize( row
- j
, col
- i
);
7529 attr_stub
->DecRef();
7537 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7539 if ( CanHaveAttributes() )
7541 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7542 attr
->SetRenderer(renderer
);
7547 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7549 if ( CanHaveAttributes() )
7551 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7552 attr
->SetEditor(editor
);
7557 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7559 if ( CanHaveAttributes() )
7561 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7562 attr
->SetReadOnly(isReadOnly
);
7567 // ----------------------------------------------------------------------------
7568 // Data type registration
7569 // ----------------------------------------------------------------------------
7571 void wxGrid::RegisterDataType(const wxString
& typeName
,
7572 wxGridCellRenderer
* renderer
,
7573 wxGridCellEditor
* editor
)
7575 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7579 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7581 wxString typeName
= m_table
->GetTypeName(row
, col
);
7582 return GetDefaultEditorForType(typeName
);
7585 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7587 wxString typeName
= m_table
->GetTypeName(row
, col
);
7588 return GetDefaultRendererForType(typeName
);
7591 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7593 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7594 if ( index
== wxNOT_FOUND
)
7596 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7601 return m_typeRegistry
->GetEditor(index
);
7604 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7606 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7607 if ( index
== wxNOT_FOUND
)
7609 wxFAIL_MSG(wxString::Format(wxT("Unknown data type name [%s]"), typeName
.c_str()));
7614 return m_typeRegistry
->GetRenderer(index
);
7617 // ----------------------------------------------------------------------------
7619 // ----------------------------------------------------------------------------
7621 void wxGrid::DoDisableLineResize(int line
, wxGridFixedIndicesSet
*& setFixed
)
7625 setFixed
= new wxGridFixedIndicesSet
;
7628 setFixed
->insert(line
);
7632 wxGrid::DoCanResizeLine(int line
, const wxGridFixedIndicesSet
*setFixed
) const
7634 return !setFixed
|| !setFixed
->count(line
);
7637 void wxGrid::EnableDragRowSize( bool enable
)
7639 m_canDragRowSize
= enable
;
7642 void wxGrid::EnableDragColSize( bool enable
)
7644 m_canDragColSize
= enable
;
7647 void wxGrid::EnableDragGridSize( bool enable
)
7649 m_canDragGridSize
= enable
;
7652 void wxGrid::EnableDragCell( bool enable
)
7654 m_canDragCell
= enable
;
7657 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7659 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
7661 if ( resizeExistingRows
)
7663 // since we are resizing all rows to the default row size,
7664 // we can simply clear the row heights and row bottoms
7665 // arrays (which also allows us to take advantage of
7666 // some speed optimisations)
7667 m_rowHeights
.Empty();
7668 m_rowBottoms
.Empty();
7669 if ( !GetBatchCount() )
7674 void wxGrid::SetRowSize( int row
, int height
)
7676 wxCHECK_RET( row
>= 0 && row
< m_numRows
, wxT("invalid row index") );
7678 // if < 0 then calculate new height from label
7682 wxArrayString lines
;
7683 wxClientDC
dc(m_rowLabelWin
);
7684 dc
.SetFont(GetLabelFont());
7685 StringToLines(GetRowLabelValue( row
), lines
);
7686 GetTextBoxSize( dc
, lines
, &w
, &h
);
7687 //check that it is not less than the minimal height
7688 height
= wxMax(h
, GetRowMinimalAcceptableHeight());
7691 // See comment in SetColSize
7692 if ( height
< GetRowMinimalAcceptableHeight())
7695 if ( m_rowHeights
.IsEmpty() )
7697 // need to really create the array
7701 int h
= wxMax( 0, height
);
7702 int diff
= h
- m_rowHeights
[row
];
7704 m_rowHeights
[row
] = h
;
7705 for ( int i
= row
; i
< m_numRows
; i
++ )
7707 m_rowBottoms
[i
] += diff
;
7710 if ( !GetBatchCount() )
7714 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7716 // we dont allow zero default column width
7717 m_defaultColWidth
= wxMax( wxMax( width
, m_minAcceptableColWidth
), 1 );
7719 if ( resizeExistingCols
)
7721 // since we are resizing all columns to the default column size,
7722 // we can simply clear the col widths and col rights
7723 // arrays (which also allows us to take advantage of
7724 // some speed optimisations)
7725 m_colWidths
.Empty();
7726 m_colRights
.Empty();
7727 if ( !GetBatchCount() )
7732 void wxGrid::SetColSize( int col
, int width
)
7734 wxCHECK_RET( col
>= 0 && col
< m_numCols
, wxT("invalid column index") );
7736 // if < 0 then calculate new width from label
7740 wxArrayString lines
;
7741 wxClientDC
dc(m_colWindow
);
7742 dc
.SetFont(GetLabelFont());
7743 StringToLines(GetColLabelValue(col
), lines
);
7744 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
7745 GetTextBoxSize( dc
, lines
, &w
, &h
);
7747 GetTextBoxSize( dc
, lines
, &h
, &w
);
7749 //check that it is not less than the minimal width
7750 width
= wxMax(width
, GetColMinimalAcceptableWidth());
7753 // we intentionally don't test whether the width is less than
7754 // GetColMinimalWidth() here but we do compare it with
7755 // GetColMinimalAcceptableWidth() as otherwise things currently break (see
7756 // #651) -- and we also always allow the width of 0 as it has the special
7757 // sense of hiding the column
7758 if ( width
> 0 && width
< GetColMinimalAcceptableWidth() )
7761 if ( m_colWidths
.IsEmpty() )
7763 // need to really create the array
7767 const int diff
= width
- m_colWidths
[col
];
7768 m_colWidths
[col
] = width
;
7769 if ( m_useNativeHeader
)
7770 GetGridColHeader()->UpdateColumn(col
);
7771 //else: will be refreshed when the header is redrawn
7773 for ( int colPos
= GetColPos(col
); colPos
< m_numCols
; colPos
++ )
7775 m_colRights
[GetColAt(colPos
)] += diff
;
7778 if ( !GetBatchCount() )
7785 void wxGrid::SetColMinimalWidth( int col
, int width
)
7787 if (width
> GetColMinimalAcceptableWidth())
7789 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7790 m_colMinWidths
[key
] = width
;
7794 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7796 if (width
> GetRowMinimalAcceptableHeight())
7798 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7799 m_rowMinHeights
[key
] = width
;
7803 int wxGrid::GetColMinimalWidth(int col
) const
7805 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
7806 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
7808 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
7811 int wxGrid::GetRowMinimalHeight(int row
) const
7813 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
7814 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
7816 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
7819 void wxGrid::SetColMinimalAcceptableWidth( int width
)
7821 // We do allow a width of 0 since this gives us
7822 // an easy way to temporarily hiding columns.
7824 m_minAcceptableColWidth
= width
;
7827 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
7829 // We do allow a height of 0 since this gives us
7830 // an easy way to temporarily hiding rows.
7832 m_minAcceptableRowHeight
= height
;
7835 int wxGrid::GetColMinimalAcceptableWidth() const
7837 return m_minAcceptableColWidth
;
7840 int wxGrid::GetRowMinimalAcceptableHeight() const
7842 return m_minAcceptableRowHeight
;
7845 // ----------------------------------------------------------------------------
7847 // ----------------------------------------------------------------------------
7850 wxGrid::AutoSizeColOrRow(int colOrRow
, bool setAsMin
, wxGridDirection direction
)
7852 const bool column
= direction
== wxGRID_COLUMN
;
7854 wxClientDC
dc(m_gridWin
);
7856 // cancel editing of cell
7857 HideCellEditControl();
7858 SaveEditControlValue();
7860 // initialize both of them just to avoid compiler warnings even if only
7861 // really needs to be initialized here
7875 wxCoord extent
, extentMax
= 0;
7876 int max
= column
? m_numRows
: m_numCols
;
7877 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7884 // we need to account for the cells spanning multiple columns/rows:
7885 // while they may need a lot of space, they don't need all of it in
7887 int numRows
, numCols
;
7888 const CellSpan span
= GetCellSize(row
, col
, &numRows
, &numCols
);
7889 if ( span
== CellSpan_Inside
)
7891 // we need to get the size of the main cell, not of a cell hidden
7896 // get the size of the main cell too
7897 GetCellSize(row
, col
, &numRows
, &numCols
);
7900 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7901 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7904 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7905 extent
= column
? size
.x
: size
.y
;
7907 if ( span
!= CellSpan_None
)
7909 // we spread the size of a spanning cell over all the cells it
7910 // covers evenly -- this is probably not ideal but we can't
7911 // really do much better here
7913 // notice that numCols and numRows are never 0 as they
7914 // correspond to the size of the main cell of the span and not
7915 // of the cell inside it
7916 extent
/= column
? numCols
: numRows
;
7919 if ( extent
> extentMax
)
7928 // now also compare with the column label extent
7930 dc
.SetFont( GetLabelFont() );
7934 dc
.GetMultiLineTextExtent( GetColLabelValue(col
), &w
, &h
);
7935 if ( GetColLabelTextOrientation() == wxVERTICAL
)
7939 dc
.GetMultiLineTextExtent( GetRowLabelValue(row
), &w
, &h
);
7941 extent
= column
? w
: h
;
7942 if ( extent
> extentMax
)
7947 // empty column - give default extent (notice that if extentMax is less
7948 // than default extent but != 0, it's OK)
7949 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7954 // leave some space around text
7962 // Ensure automatic width is not less than minimal width. See the
7963 // comment in SetColSize() for explanation of why this isn't done
7966 extentMax
= wxMax(extentMax
, GetColMinimalWidth(col
));
7968 SetColSize( col
, extentMax
);
7969 if ( !GetBatchCount() )
7971 if ( m_useNativeHeader
)
7973 GetGridColHeader()->UpdateColumn(col
);
7978 m_gridWin
->GetClientSize( &cw
, &ch
);
7979 wxRect
rect ( CellToRect( 0, col
) );
7981 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
7982 rect
.width
= cw
- rect
.x
;
7983 rect
.height
= m_colLabelHeight
;
7984 GetColLabelWindow()->Refresh( true, &rect
);
7990 // Ensure automatic width is not less than minimal height. See the
7991 // comment in SetColSize() for explanation of why this isn't done
7994 extentMax
= wxMax(extentMax
, GetRowMinimalHeight(row
));
7996 SetRowSize(row
, extentMax
);
7997 if ( !GetBatchCount() )
8000 m_gridWin
->GetClientSize( &cw
, &ch
);
8001 wxRect
rect( CellToRect( row
, 0 ) );
8003 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8004 rect
.width
= m_rowLabelWidth
;
8005 rect
.height
= ch
- rect
.y
;
8006 m_rowLabelWin
->Refresh( true, &rect
);
8013 SetColMinimalWidth(col
, extentMax
);
8015 SetRowMinimalHeight(row
, extentMax
);
8019 wxCoord
wxGrid::CalcColOrRowLabelAreaMinSize(wxGridDirection direction
)
8021 // calculate size for the rows or columns?
8022 const bool calcRows
= direction
== wxGRID_ROW
;
8024 wxClientDC
dc(calcRows
? GetGridRowLabelWindow()
8025 : GetGridColLabelWindow());
8026 dc
.SetFont(GetLabelFont());
8028 // which dimension should we take into account for calculations?
8030 // for columns, the text can be only horizontal so it's easy but for rows
8031 // we also have to take into account the text orientation
8033 useWidth
= calcRows
|| (GetColLabelTextOrientation() == wxVERTICAL
);
8035 wxArrayString lines
;
8036 wxCoord extentMax
= 0;
8038 const int numRowsOrCols
= calcRows
? m_numRows
: m_numCols
;
8039 for ( int rowOrCol
= 0; rowOrCol
< numRowsOrCols
; rowOrCol
++ )
8043 wxString label
= calcRows
? GetRowLabelValue(rowOrCol
)
8044 : GetColLabelValue(rowOrCol
);
8045 StringToLines(label
, lines
);
8048 GetTextBoxSize(dc
, lines
, &w
, &h
);
8050 const wxCoord extent
= useWidth
? w
: h
;
8051 if ( extent
> extentMax
)
8057 // empty column - give default extent (notice that if extentMax is less
8058 // than default extent but != 0, it's OK)
8059 extentMax
= calcRows
? GetDefaultRowLabelSize()
8060 : GetDefaultColLabelSize();
8063 // leave some space around text (taken from AutoSizeColOrRow)
8072 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8074 int width
= m_rowLabelWidth
;
8076 wxGridUpdateLocker locker
;
8078 locker
.Create(this);
8080 for ( int col
= 0; col
< m_numCols
; col
++ )
8083 AutoSizeColumn(col
, setAsMin
);
8085 width
+= GetColWidth(col
);
8091 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8093 int height
= m_colLabelHeight
;
8095 wxGridUpdateLocker locker
;
8097 locker
.Create(this);
8099 for ( int row
= 0; row
< m_numRows
; row
++ )
8102 AutoSizeRow(row
, setAsMin
);
8104 height
+= GetRowHeight(row
);
8110 void wxGrid::AutoSize()
8112 wxGridUpdateLocker
locker(this);
8114 wxSize
size(SetOrCalcColumnSizes(false) - m_rowLabelWidth
+ m_extraWidth
,
8115 SetOrCalcRowSizes(false) - m_colLabelHeight
+ m_extraHeight
);
8117 // we know that we're not going to have scrollbars so disable them now to
8118 // avoid trouble in SetClientSize() which can otherwise set the correct
8119 // client size but also leave space for (not needed any more) scrollbars
8120 SetScrollbars(m_xScrollPixelsPerLine
, m_yScrollPixelsPerLine
,
8123 SetClientSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
);
8126 void wxGrid::AutoSizeRowLabelSize( int row
)
8128 // Hide the edit control, so it
8129 // won't interfere with drag-shrinking.
8130 if ( IsCellEditControlShown() )
8132 HideCellEditControl();
8133 SaveEditControlValue();
8136 // autosize row height depending on label text
8137 SetRowSize(row
, -1);
8141 void wxGrid::AutoSizeColLabelSize( int col
)
8143 // Hide the edit control, so it
8144 // won't interfere with drag-shrinking.
8145 if ( IsCellEditControlShown() )
8147 HideCellEditControl();
8148 SaveEditControlValue();
8151 // autosize column width depending on label text
8152 SetColSize(col
, -1);
8156 wxSize
wxGrid::DoGetBestSize() const
8158 wxGrid
*self
= (wxGrid
*)this; // const_cast
8160 // we do the same as in AutoSize() here with the exception that we don't
8161 // change the column/row sizes, only calculate them
8162 wxSize
size(self
->SetOrCalcColumnSizes(true) - m_rowLabelWidth
+ m_extraWidth
,
8163 self
->SetOrCalcRowSizes(true) - m_colLabelHeight
+ m_extraHeight
);
8165 // NOTE: This size should be cached, but first we need to add calls to
8166 // InvalidateBestSize everywhere that could change the results of this
8168 // CacheBestSize(size);
8170 return wxSize(size
.x
+ m_rowLabelWidth
, size
.y
+ m_colLabelHeight
)
8171 + GetWindowBorderSize();
8179 wxPen
& wxGrid::GetDividerPen() const
8184 // ----------------------------------------------------------------------------
8185 // cell value accessor functions
8186 // ----------------------------------------------------------------------------
8188 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8192 m_table
->SetValue( row
, col
, s
);
8193 if ( !GetBatchCount() )
8196 wxRect
rect( CellToRect( row
, col
) );
8198 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
8199 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8200 m_gridWin
->Refresh( false, &rect
);
8203 if ( m_currentCellCoords
.GetRow() == row
&&
8204 m_currentCellCoords
.GetCol() == col
&&
8205 IsCellEditControlShown())
8206 // Note: If we are using IsCellEditControlEnabled,
8207 // this interacts badly with calling SetCellValue from
8208 // an EVT_GRID_CELL_CHANGE handler.
8210 HideCellEditControl();
8211 ShowCellEditControl(); // will reread data from table
8216 // ----------------------------------------------------------------------------
8217 // block, row and column selection
8218 // ----------------------------------------------------------------------------
8220 void wxGrid::SelectRow( int row
, bool addToSelected
)
8225 if ( !addToSelected
)
8228 m_selection
->SelectRow(row
);
8231 void wxGrid::SelectCol( int col
, bool addToSelected
)
8236 if ( !addToSelected
)
8239 m_selection
->SelectCol(col
);
8242 void wxGrid::SelectBlock(int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8248 if ( !addToSelected
)
8251 m_selection
->SelectBlock(topRow
, leftCol
, bottomRow
, rightCol
);
8254 void wxGrid::SelectAll()
8256 if ( m_numRows
> 0 && m_numCols
> 0 )
8259 m_selection
->SelectBlock( 0, 0, m_numRows
- 1, m_numCols
- 1 );
8263 // ----------------------------------------------------------------------------
8264 // cell, row and col deselection
8265 // ----------------------------------------------------------------------------
8267 void wxGrid::DeselectLine(int line
, const wxGridOperations
& oper
)
8272 const wxGridSelectionModes mode
= m_selection
->GetSelectionMode();
8273 if ( mode
== oper
.GetSelectionMode() ||
8274 mode
== wxGrid::wxGridSelectRowsOrColumns
)
8276 const wxGridCellCoords
c(oper
.MakeCoords(line
, 0));
8277 if ( m_selection
->IsInSelection(c
) )
8278 m_selection
->ToggleCellSelection(c
);
8280 else if ( mode
!= oper
.Dual().GetSelectionMode() )
8282 const int nOther
= oper
.Dual().GetNumberOfLines(this);
8283 for ( int i
= 0; i
< nOther
; i
++ )
8285 const wxGridCellCoords
c(oper
.MakeCoords(line
, i
));
8286 if ( m_selection
->IsInSelection(c
) )
8287 m_selection
->ToggleCellSelection(c
);
8290 //else: can only select orthogonal lines so no lines in this direction
8291 // could have been selected anyhow
8294 void wxGrid::DeselectRow(int row
)
8296 DeselectLine(row
, wxGridRowOperations());
8299 void wxGrid::DeselectCol(int col
)
8301 DeselectLine(col
, wxGridColumnOperations());
8304 void wxGrid::DeselectCell( int row
, int col
)
8306 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8307 m_selection
->ToggleCellSelection(row
, col
);
8310 bool wxGrid::IsSelection() const
8312 return ( m_selection
&& (m_selection
->IsSelection() ||
8313 ( m_selectedBlockTopLeft
!= wxGridNoCellCoords
&&
8314 m_selectedBlockBottomRight
!= wxGridNoCellCoords
) ) );
8317 bool wxGrid::IsInSelection( int row
, int col
) const
8319 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8320 ( row
>= m_selectedBlockTopLeft
.GetRow() &&
8321 col
>= m_selectedBlockTopLeft
.GetCol() &&
8322 row
<= m_selectedBlockBottomRight
.GetRow() &&
8323 col
<= m_selectedBlockBottomRight
.GetCol() )) );
8326 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
8330 wxGridCellCoordsArray a
;
8334 return m_selection
->m_cellSelection
;
8337 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
8341 wxGridCellCoordsArray a
;
8345 return m_selection
->m_blockSelectionTopLeft
;
8348 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
8352 wxGridCellCoordsArray a
;
8356 return m_selection
->m_blockSelectionBottomRight
;
8359 wxArrayInt
wxGrid::GetSelectedRows() const
8367 return m_selection
->m_rowSelection
;
8370 wxArrayInt
wxGrid::GetSelectedCols() const
8378 return m_selection
->m_colSelection
;
8381 void wxGrid::ClearSelection()
8383 wxRect r1
= BlockToDeviceRect(m_selectedBlockTopLeft
,
8384 m_selectedBlockBottomRight
);
8385 wxRect r2
= BlockToDeviceRect(m_currentCellCoords
,
8386 m_selectedBlockCorner
);
8388 m_selectedBlockTopLeft
=
8389 m_selectedBlockBottomRight
=
8390 m_selectedBlockCorner
= wxGridNoCellCoords
;
8392 if ( !r1
.IsEmpty() )
8393 RefreshRect(r1
, false);
8394 if ( !r2
.IsEmpty() )
8395 RefreshRect(r2
, false);
8398 m_selection
->ClearSelection();
8401 // This function returns the rectangle that encloses the given block
8402 // in device coords clipped to the client size of the grid window.
8404 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
& topLeft
,
8405 const wxGridCellCoords
& bottomRight
) const
8408 wxRect tempCellRect
= CellToRect(topLeft
);
8409 if ( tempCellRect
!= wxGridNoCellRect
)
8411 resultRect
= tempCellRect
;
8415 resultRect
= wxRect(0, 0, 0, 0);
8418 tempCellRect
= CellToRect(bottomRight
);
8419 if ( tempCellRect
!= wxGridNoCellRect
)
8421 resultRect
+= tempCellRect
;
8425 // If both inputs were "wxGridNoCellRect," then there's nothing to do.
8426 return wxGridNoCellRect
;
8429 // Ensure that left/right and top/bottom pairs are in order.
8430 int left
= resultRect
.GetLeft();
8431 int top
= resultRect
.GetTop();
8432 int right
= resultRect
.GetRight();
8433 int bottom
= resultRect
.GetBottom();
8435 int leftCol
= topLeft
.GetCol();
8436 int topRow
= topLeft
.GetRow();
8437 int rightCol
= bottomRight
.GetCol();
8438 int bottomRow
= bottomRight
.GetRow();
8462 // The following loop is ONLY necessary to detect and handle merged cells.
8464 m_gridWin
->GetClientSize( &cw
, &ch
);
8466 // Get the origin coordinates: notice that they will be negative if the
8467 // grid is scrolled downwards/to the right.
8468 int gridOriginX
= 0;
8469 int gridOriginY
= 0;
8470 CalcScrolledPosition(gridOriginX
, gridOriginY
, &gridOriginX
, &gridOriginY
);
8472 int onScreenLeftmostCol
= internalXToCol(-gridOriginX
);
8473 int onScreenUppermostRow
= internalYToRow(-gridOriginY
);
8475 int onScreenRightmostCol
= internalXToCol(-gridOriginX
+ cw
);
8476 int onScreenBottommostRow
= internalYToRow(-gridOriginY
+ ch
);
8478 // Bound our loop so that we only examine the portion of the selected block
8479 // that is shown on screen. Therefore, we compare the Top-Left block values
8480 // to the Top-Left screen values, and the Bottom-Right block values to the
8481 // Bottom-Right screen values, choosing appropriately.
8482 const int visibleTopRow
= wxMax(topRow
, onScreenUppermostRow
);
8483 const int visibleBottomRow
= wxMin(bottomRow
, onScreenBottommostRow
);
8484 const int visibleLeftCol
= wxMax(leftCol
, onScreenLeftmostCol
);
8485 const int visibleRightCol
= wxMin(rightCol
, onScreenRightmostCol
);
8487 for ( int j
= visibleTopRow
; j
<= visibleBottomRow
; j
++ )
8489 for ( int i
= visibleLeftCol
; i
<= visibleRightCol
; i
++ )
8491 if ( (j
== visibleTopRow
) || (j
== visibleBottomRow
) ||
8492 (i
== visibleLeftCol
) || (i
== visibleRightCol
) )
8494 tempCellRect
= CellToRect( j
, i
);
8496 if (tempCellRect
.x
< left
)
8497 left
= tempCellRect
.x
;
8498 if (tempCellRect
.y
< top
)
8499 top
= tempCellRect
.y
;
8500 if (tempCellRect
.x
+ tempCellRect
.width
> right
)
8501 right
= tempCellRect
.x
+ tempCellRect
.width
;
8502 if (tempCellRect
.y
+ tempCellRect
.height
> bottom
)
8503 bottom
= tempCellRect
.y
+ tempCellRect
.height
;
8507 i
= visibleRightCol
; // jump over inner cells.
8512 // Convert to scrolled coords
8513 CalcScrolledPosition( left
, top
, &left
, &top
);
8514 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
8516 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8517 return wxRect(0,0,0,0);
8519 resultRect
.SetLeft( wxMax(0, left
) );
8520 resultRect
.SetTop( wxMax(0, top
) );
8521 resultRect
.SetRight( wxMin(cw
, right
) );
8522 resultRect
.SetBottom( wxMin(ch
, bottom
) );
8527 void wxGrid::DoSetSizes(const wxGridSizesInfo
& sizeInfo
,
8528 const wxGridOperations
& oper
)
8531 oper
.SetDefaultLineSize(this, sizeInfo
.m_sizeDefault
, true);
8532 const int numLines
= oper
.GetNumberOfLines(this);
8533 for ( int i
= 0; i
< numLines
; i
++ )
8535 int size
= sizeInfo
.GetSize(i
);
8536 if ( size
!= sizeInfo
.m_sizeDefault
)
8537 oper
.SetLineSize(this, i
, size
);
8542 void wxGrid::SetColSizes(const wxGridSizesInfo
& sizeInfo
)
8544 DoSetSizes(sizeInfo
, wxGridColumnOperations());
8547 void wxGrid::SetRowSizes(const wxGridSizesInfo
& sizeInfo
)
8549 DoSetSizes(sizeInfo
, wxGridRowOperations());
8552 wxGridSizesInfo::wxGridSizesInfo(int defSize
, const wxArrayInt
& allSizes
)
8554 m_sizeDefault
= defSize
;
8555 for ( size_t i
= 0; i
< allSizes
.size(); i
++ )
8557 if ( allSizes
[i
] != defSize
)
8558 m_customSizes
[i
] = allSizes
[i
];
8562 int wxGridSizesInfo::GetSize(unsigned pos
) const
8564 wxUnsignedToIntHashMap::const_iterator it
= m_customSizes
.find(pos
);
8566 return it
== m_customSizes
.end() ? m_sizeDefault
: it
->second
;
8569 // ----------------------------------------------------------------------------
8571 // ----------------------------------------------------------------------------
8573 #if wxUSE_DRAG_AND_DROP
8575 // this allow setting drop target directly on wxGrid
8576 void wxGrid::SetDropTarget(wxDropTarget
*dropTarget
)
8578 GetGridWindow()->SetDropTarget(dropTarget
);
8581 #endif // wxUSE_DRAG_AND_DROP
8583 // ----------------------------------------------------------------------------
8584 // grid event classes
8585 // ----------------------------------------------------------------------------
8587 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
8589 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8590 int row
, int col
, int x
, int y
, bool sel
,
8591 bool control
, bool shift
, bool alt
, bool meta
)
8592 : wxNotifyEvent( type
, id
),
8593 wxKeyboardState(control
, shift
, alt
, meta
)
8595 Init(row
, col
, x
, y
, sel
);
8597 SetEventObject(obj
);
8600 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
8602 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8603 int rowOrCol
, int x
, int y
,
8604 bool control
, bool shift
, bool alt
, bool meta
)
8605 : wxNotifyEvent( type
, id
),
8606 wxKeyboardState(control
, shift
, alt
, meta
)
8608 Init(rowOrCol
, x
, y
);
8610 SetEventObject(obj
);
8614 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
8616 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8617 const wxGridCellCoords
& topLeft
,
8618 const wxGridCellCoords
& bottomRight
,
8619 bool sel
, bool control
,
8620 bool shift
, bool alt
, bool meta
)
8621 : wxNotifyEvent( type
, id
),
8622 wxKeyboardState(control
, shift
, alt
, meta
)
8624 Init(topLeft
, bottomRight
, sel
);
8626 SetEventObject(obj
);
8630 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
8632 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
8633 wxObject
* obj
, int row
,
8634 int col
, wxControl
* ctrl
)
8635 : wxCommandEvent(type
, id
)
8637 SetEventObject(obj
);
8644 // ----------------------------------------------------------------------------
8645 // wxGridTypeRegistry
8646 // ----------------------------------------------------------------------------
8648 wxGridTypeRegistry::~wxGridTypeRegistry()
8650 size_t count
= m_typeinfo
.GetCount();
8651 for ( size_t i
= 0; i
< count
; i
++ )
8652 delete m_typeinfo
[i
];
8655 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
8656 wxGridCellRenderer
* renderer
,
8657 wxGridCellEditor
* editor
)
8659 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
8661 // is it already registered?
8662 int loc
= FindRegisteredDataType(typeName
);
8663 if ( loc
!= wxNOT_FOUND
)
8665 delete m_typeinfo
[loc
];
8666 m_typeinfo
[loc
] = info
;
8670 m_typeinfo
.Add(info
);
8674 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
8676 size_t count
= m_typeinfo
.GetCount();
8677 for ( size_t i
= 0; i
< count
; i
++ )
8679 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
8688 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
8690 int index
= FindRegisteredDataType(typeName
);
8691 if ( index
== wxNOT_FOUND
)
8693 // check whether this is one of the standard ones, in which case
8694 // register it "on the fly"
8696 if ( typeName
== wxGRID_VALUE_STRING
)
8698 RegisterDataType(wxGRID_VALUE_STRING
,
8699 new wxGridCellStringRenderer
,
8700 new wxGridCellTextEditor
);
8703 #endif // wxUSE_TEXTCTRL
8705 if ( typeName
== wxGRID_VALUE_BOOL
)
8707 RegisterDataType(wxGRID_VALUE_BOOL
,
8708 new wxGridCellBoolRenderer
,
8709 new wxGridCellBoolEditor
);
8712 #endif // wxUSE_CHECKBOX
8714 if ( typeName
== wxGRID_VALUE_NUMBER
)
8716 RegisterDataType(wxGRID_VALUE_NUMBER
,
8717 new wxGridCellNumberRenderer
,
8718 new wxGridCellNumberEditor
);
8720 else if ( typeName
== wxGRID_VALUE_FLOAT
)
8722 RegisterDataType(wxGRID_VALUE_FLOAT
,
8723 new wxGridCellFloatRenderer
,
8724 new wxGridCellFloatEditor
);
8727 #endif // wxUSE_TEXTCTRL
8729 if ( typeName
== wxGRID_VALUE_CHOICE
)
8731 RegisterDataType(wxGRID_VALUE_CHOICE
,
8732 new wxGridCellStringRenderer
,
8733 new wxGridCellChoiceEditor
);
8736 #endif // wxUSE_COMBOBOX
8741 // we get here only if just added the entry for this type, so return
8743 index
= m_typeinfo
.GetCount() - 1;
8749 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
8751 int index
= FindDataType(typeName
);
8752 if ( index
== wxNOT_FOUND
)
8754 // the first part of the typename is the "real" type, anything after ':'
8755 // are the parameters for the renderer
8756 index
= FindDataType(typeName
.BeforeFirst(wxT(':')));
8757 if ( index
== wxNOT_FOUND
)
8762 wxGridCellRenderer
*renderer
= GetRenderer(index
);
8763 wxGridCellRenderer
*rendererOld
= renderer
;
8764 renderer
= renderer
->Clone();
8765 rendererOld
->DecRef();
8767 wxGridCellEditor
*editor
= GetEditor(index
);
8768 wxGridCellEditor
*editorOld
= editor
;
8769 editor
= editor
->Clone();
8770 editorOld
->DecRef();
8772 // do it even if there are no parameters to reset them to defaults
8773 wxString params
= typeName
.AfterFirst(wxT(':'));
8774 renderer
->SetParameters(params
);
8775 editor
->SetParameters(params
);
8777 // register the new typename
8778 RegisterDataType(typeName
, renderer
, editor
);
8780 // we just registered it, it's the last one
8781 index
= m_typeinfo
.GetCount() - 1;
8787 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
8789 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
8796 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
8798 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
8805 #endif // wxUSE_GRID