1 ///////////////////////////////////////////////////////////////////////////
2 // Name: 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
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
430 wxGridCellEditor::~wxGridCellEditor()
435 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
436 wxWindowID
WXUNUSED(id
),
437 wxEvtHandler
* evtHandler
)
440 m_control
->PushEventHandler(evtHandler
);
443 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
444 wxGridCellAttr
*attr
)
446 // erase the background because we might not fill the cell
447 wxClientDC
dc(m_control
->GetParent());
448 dc
.SetPen(*wxTRANSPARENT_PEN
);
449 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
450 dc
.DrawRectangle(rectCell
);
452 // redraw the control we just painted over
453 m_control
->Refresh();
456 void wxGridCellEditor::Destroy()
460 m_control
->PopEventHandler(TRUE
/* delete it*/);
462 m_control
->Destroy();
467 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
469 wxASSERT_MSG(m_control
,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control
->Show(show
);
475 // set the colours/fonts if we have any
478 m_colFgOld
= m_control
->GetForegroundColour();
479 m_control
->SetForegroundColour(attr
->GetTextColour());
481 m_colBgOld
= m_control
->GetBackgroundColour();
482 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
484 m_fontOld
= m_control
->GetFont();
485 m_control
->SetFont(attr
->GetFont());
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
493 // restore the standard colours fonts
494 if ( m_colFgOld
.Ok() )
496 m_control
->SetForegroundColour(m_colFgOld
);
497 m_colFgOld
= wxNullColour
;
500 if ( m_colBgOld
.Ok() )
502 m_control
->SetBackgroundColour(m_colBgOld
);
503 m_colBgOld
= wxNullColour
;
506 if ( m_fontOld
.Ok() )
508 m_control
->SetFont(m_fontOld
);
509 m_fontOld
= wxNullFont
;
514 void wxGridCellEditor::SetSize(const wxRect
& rect
)
516 wxASSERT_MSG(m_control
,
517 wxT("The wxGridCellEditor must be Created first!"));
518 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
521 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
526 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
529 return !(event
.ControlDown() || event
.AltDown());
532 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
537 void wxGridCellEditor::StartingClick()
543 // ----------------------------------------------------------------------------
544 // wxGridCellTextEditor
545 // ----------------------------------------------------------------------------
547 wxGridCellTextEditor::wxGridCellTextEditor()
552 void wxGridCellTextEditor::Create(wxWindow
* parent
,
554 wxEvtHandler
* evtHandler
)
556 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
557 wxDefaultPosition
, wxDefaultSize
558 #if defined(__WXMSW__)
559 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
560 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
564 // TODO: use m_maxChars
566 wxGridCellEditor::Create(parent
, id
, evtHandler
);
569 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
570 wxGridCellAttr
* WXUNUSED(attr
))
572 // as we fill the entire client area, don't do anything here to minimize
576 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
578 wxRect
rect(rectOrig
);
580 // Make the edit control large enough to allow for internal
583 // TODO: remove this if the text ctrl sizing is improved esp. for
586 #if defined(__WXGTK__)
595 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
597 // MB: treat MSW separately here otherwise the caret doesn't show
598 // when the editor is in the first row.
599 #if defined(__WXMSW__)
602 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
605 #if defined(__WXMOTIF__)
609 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
610 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
611 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
612 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
615 wxGridCellEditor::SetSize(rect
);
618 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
625 DoBeginEdit(m_startValue
);
628 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
630 Text()->SetValue(startValue
);
631 Text()->SetInsertionPointEnd();
632 Text()->SetSelection(-1,-1);
636 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
639 wxASSERT_MSG(m_control
,
640 wxT("The wxGridCellEditor must be Created first!"));
642 bool changed
= FALSE
;
643 wxString value
= Text()->GetValue();
644 if (value
!= m_startValue
)
648 grid
->GetTable()->SetValue(row
, col
, value
);
650 m_startValue
= wxEmptyString
;
651 Text()->SetValue(m_startValue
);
657 void wxGridCellTextEditor::Reset()
659 wxASSERT_MSG(m_control
,
660 wxT("The wxGridCellEditor must be Created first!"));
662 DoReset(m_startValue
);
665 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
667 Text()->SetValue(startValue
);
668 Text()->SetInsertionPointEnd();
671 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
673 if ( wxGridCellEditor::IsAcceptedKey(event
) )
675 int keycode
= event
.GetKeyCode();
689 case WXK_NUMPAD_MULTIPLY
:
693 case WXK_NUMPAD_SUBTRACT
:
695 case WXK_NUMPAD_DECIMAL
:
697 case WXK_NUMPAD_DIVIDE
:
701 // accept 8 bit chars too if isprint() agrees
702 if ( (keycode
< 255) && (isprint(keycode
)) )
710 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
712 if ( !Text()->EmulateKeyPress(event
) )
718 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
719 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
721 #if defined(__WXMOTIF__) || defined(__WXGTK__)
722 // wxMotif needs a little extra help...
723 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
724 wxString
s( Text()->GetValue() );
725 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
727 Text()->SetInsertionPoint( pos
);
729 // the other ports can handle a Return key press
735 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
745 if ( !params
.ToLong(&tmp
) )
747 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
751 m_maxChars
= (size_t)tmp
;
756 // ----------------------------------------------------------------------------
757 // wxGridCellNumberEditor
758 // ----------------------------------------------------------------------------
760 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
766 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
768 wxEvtHandler
* evtHandler
)
772 // create a spin ctrl
773 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
774 wxDefaultPosition
, wxDefaultSize
,
778 wxGridCellEditor::Create(parent
, id
, evtHandler
);
782 // just a text control
783 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
786 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
787 #endif // wxUSE_VALIDATORS
791 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
793 // first get the value
794 wxGridTableBase
*table
= grid
->GetTable();
795 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
797 m_valueOld
= table
->GetValueAsLong(row
, col
);
802 wxString sValue
= table
->GetValue(row
, col
);
803 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
805 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
812 Spin()->SetValue((int)m_valueOld
);
817 DoBeginEdit(GetString());
821 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
830 value
= Spin()->GetValue();
831 changed
= value
!= m_valueOld
;
833 text
= wxString::Format(wxT("%ld"), value
);
837 text
= Text()->GetValue();
838 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
843 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
844 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
846 grid
->GetTable()->SetValue(row
, col
, text
);
852 void wxGridCellNumberEditor::Reset()
856 Spin()->SetValue((int)m_valueOld
);
860 DoReset(GetString());
864 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
866 if ( wxGridCellEditor::IsAcceptedKey(event
) )
868 int keycode
= event
.GetKeyCode();
884 case WXK_NUMPAD_SUBTRACT
:
890 if ( (keycode
< 128) && isdigit(keycode
) )
898 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
902 int keycode
= (int) event
.KeyCode();
903 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
904 || keycode
== WXK_NUMPAD0
905 || keycode
== WXK_NUMPAD1
906 || keycode
== WXK_NUMPAD2
907 || keycode
== WXK_NUMPAD3
908 || keycode
== WXK_NUMPAD4
909 || keycode
== WXK_NUMPAD5
910 || keycode
== WXK_NUMPAD6
911 || keycode
== WXK_NUMPAD7
912 || keycode
== WXK_NUMPAD8
913 || keycode
== WXK_NUMPAD9
914 || keycode
== WXK_ADD
915 || keycode
== WXK_NUMPAD_ADD
916 || keycode
== WXK_SUBTRACT
917 || keycode
== WXK_NUMPAD_SUBTRACT
)
919 wxGridCellTextEditor::StartingKey(event
);
929 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
940 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
944 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
948 // skip the error message below
953 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
957 // ----------------------------------------------------------------------------
958 // wxGridCellFloatEditor
959 // ----------------------------------------------------------------------------
961 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
964 m_precision
= precision
;
967 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
969 wxEvtHandler
* evtHandler
)
971 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
974 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
975 #endif // wxUSE_VALIDATORS
978 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
980 // first get the value
981 wxGridTableBase
*table
= grid
->GetTable();
982 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
984 m_valueOld
= table
->GetValueAsDouble(row
, col
);
989 wxString sValue
= table
->GetValue(row
, col
);
990 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
992 wxFAIL_MSG( _T("this cell doesn't have float value") );
997 DoBeginEdit(GetString());
1000 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1004 wxString
text(Text()->GetValue());
1006 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1008 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1009 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1011 grid
->GetTable()->SetValue(row
, col
, text
);
1021 void wxGridCellFloatEditor::Reset()
1023 DoReset(GetString());
1026 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1028 int keycode
= (int)event
.KeyCode();
1029 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1030 || keycode
== WXK_NUMPAD0
1031 || keycode
== WXK_NUMPAD1
1032 || keycode
== WXK_NUMPAD2
1033 || keycode
== WXK_NUMPAD3
1034 || keycode
== WXK_NUMPAD4
1035 || keycode
== WXK_NUMPAD5
1036 || keycode
== WXK_NUMPAD6
1037 || keycode
== WXK_NUMPAD7
1038 || keycode
== WXK_NUMPAD8
1039 || keycode
== WXK_NUMPAD9
1040 || keycode
== WXK_ADD
1041 || keycode
== WXK_NUMPAD_ADD
1042 || keycode
== WXK_SUBTRACT
1043 || keycode
== WXK_NUMPAD_SUBTRACT
)
1045 wxGridCellTextEditor::StartingKey(event
);
1047 // skip Skip() below
1054 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1065 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1069 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1071 m_precision
= (int)tmp
;
1073 // skip the error message below
1078 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1082 wxString
wxGridCellFloatEditor::GetString() const
1085 if ( m_width
== -1 )
1087 // default width/precision
1090 else if ( m_precision
== -1 )
1092 // default precision
1093 fmt
.Printf(_T("%%%d.f"), m_width
);
1097 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1100 return wxString::Format(fmt
, m_valueOld
);
1103 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1105 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1107 int keycode
= event
.GetKeyCode();
1121 case WXK_NUMPAD_ADD
:
1123 case WXK_NUMPAD_SUBTRACT
:
1125 case WXK_NUMPAD_DECIMAL
:
1129 // additionally accept 'e' as in '1e+6'
1130 if ( (keycode
< 128) &&
1131 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1139 #endif // wxUSE_TEXTCTRL
1143 // ----------------------------------------------------------------------------
1144 // wxGridCellBoolEditor
1145 // ----------------------------------------------------------------------------
1147 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1149 wxEvtHandler
* evtHandler
)
1151 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1152 wxDefaultPosition
, wxDefaultSize
,
1155 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1158 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1160 bool resize
= FALSE
;
1161 wxSize size
= m_control
->GetSize();
1162 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1164 // check if the checkbox is not too big/small for this cell
1165 wxSize sizeBest
= m_control
->GetBestSize();
1166 if ( !(size
== sizeBest
) )
1168 // reset to default size if it had been made smaller
1174 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1176 // leave 1 pixel margin
1177 size
.x
= size
.y
= minSize
- 2;
1184 m_control
->SetSize(size
);
1187 // position it in the centre of the rectangle (TODO: support alignment?)
1189 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1190 // the checkbox without label still has some space to the right in wxGTK,
1191 // so shift it to the right
1193 #elif defined(__WXMSW__)
1194 // here too, but in other way
1199 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1202 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1204 m_control
->Show(show
);
1208 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1209 CBox()->SetBackgroundColour(colBg
);
1213 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1215 wxASSERT_MSG(m_control
,
1216 wxT("The wxGridCellEditor must be Created first!"));
1218 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1219 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1222 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1223 m_startValue
= !( !cellval
|| (cellval
== "0") );
1225 CBox()->SetValue(m_startValue
);
1229 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1232 wxASSERT_MSG(m_control
,
1233 wxT("The wxGridCellEditor must be Created first!"));
1235 bool changed
= FALSE
;
1236 bool value
= CBox()->GetValue();
1237 if ( value
!= m_startValue
)
1242 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1243 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1245 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1251 void wxGridCellBoolEditor::Reset()
1253 wxASSERT_MSG(m_control
,
1254 wxT("The wxGridCellEditor must be Created first!"));
1256 CBox()->SetValue(m_startValue
);
1259 void wxGridCellBoolEditor::StartingClick()
1261 CBox()->SetValue(!CBox()->GetValue());
1264 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1266 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1268 int keycode
= event
.GetKeyCode();
1272 case WXK_NUMPAD_MULTIPLY
:
1274 case WXK_NUMPAD_ADD
:
1276 case WXK_NUMPAD_SUBTRACT
:
1287 #endif // wxUSE_CHECKBOX
1291 // ----------------------------------------------------------------------------
1292 // wxGridCellChoiceEditor
1293 // ----------------------------------------------------------------------------
1295 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1296 const wxString choices
[],
1298 : m_allowOthers(allowOthers
)
1302 m_choices
.Alloc(count
);
1303 for ( size_t n
= 0; n
< count
; n
++ )
1305 m_choices
.Add(choices
[n
]);
1310 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1312 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1313 editor
->m_allowOthers
= m_allowOthers
;
1314 editor
->m_choices
= m_choices
;
1319 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1321 wxEvtHandler
* evtHandler
)
1323 size_t count
= m_choices
.GetCount();
1324 wxString
*choices
= new wxString
[count
];
1325 for ( size_t n
= 0; n
< count
; n
++ )
1327 choices
[n
] = m_choices
[n
];
1330 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1331 wxDefaultPosition
, wxDefaultSize
,
1333 m_allowOthers
? 0 : wxCB_READONLY
);
1337 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1340 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1341 wxGridCellAttr
* attr
)
1343 // as we fill the entire client area, don't do anything here to minimize
1346 // TODO: It doesn't actually fill the client area since the height of a
1347 // combo always defaults to the standard... Until someone has time to
1348 // figure out the right rectangle to paint, just do it the normal way...
1349 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1352 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1354 wxASSERT_MSG(m_control
,
1355 wxT("The wxGridCellEditor must be Created first!"));
1357 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1359 Combo()->SetValue(m_startValue
);
1360 size_t count
= m_choices
.GetCount();
1361 for (size_t i
=0; i
<count
; i
++)
1363 if (m_startValue
== m_choices
[i
])
1365 Combo()->SetSelection(i
);
1369 Combo()->SetInsertionPointEnd();
1370 Combo()->SetFocus();
1373 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1376 wxString value
= Combo()->GetValue();
1377 bool changed
= value
!= m_startValue
;
1380 grid
->GetTable()->SetValue(row
, col
, value
);
1382 m_startValue
= wxEmptyString
;
1383 Combo()->SetValue(m_startValue
);
1388 void wxGridCellChoiceEditor::Reset()
1390 Combo()->SetValue(m_startValue
);
1391 Combo()->SetInsertionPointEnd();
1394 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1404 wxStringTokenizer
tk(params
, _T(','));
1405 while ( tk
.HasMoreTokens() )
1407 m_choices
.Add(tk
.GetNextToken());
1411 #endif // wxUSE_COMBOBOX
1413 // ----------------------------------------------------------------------------
1414 // wxGridCellEditorEvtHandler
1415 // ----------------------------------------------------------------------------
1417 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1419 switch ( event
.KeyCode() )
1423 m_grid
->DisableCellEditControl();
1427 m_grid
->GetEventHandler()->ProcessEvent( event
);
1431 case WXK_NUMPAD_ENTER
:
1432 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1433 m_editor
->HandleReturn(event
);
1442 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1444 switch ( event
.KeyCode() )
1449 case WXK_NUMPAD_ENTER
:
1457 // ----------------------------------------------------------------------------
1458 // wxGridCellWorker is an (almost) empty common base class for
1459 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1460 // ----------------------------------------------------------------------------
1462 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1467 wxGridCellWorker::~wxGridCellWorker()
1471 // ============================================================================
1473 // ============================================================================
1475 // ----------------------------------------------------------------------------
1476 // wxGridCellRenderer
1477 // ----------------------------------------------------------------------------
1479 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1480 wxGridCellAttr
& attr
,
1483 int WXUNUSED(row
), int WXUNUSED(col
),
1486 dc
.SetBackgroundMode( wxSOLID
);
1490 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1494 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1497 dc
.SetPen( *wxTRANSPARENT_PEN
);
1498 dc
.DrawRectangle(rect
);
1501 // ----------------------------------------------------------------------------
1502 // wxGridCellStringRenderer
1503 // ----------------------------------------------------------------------------
1505 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1506 wxGridCellAttr
& attr
,
1510 dc
.SetBackgroundMode( wxTRANSPARENT
);
1512 // TODO some special colours for attr.IsReadOnly() case?
1516 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1517 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1521 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1522 dc
.SetTextForeground( attr
.GetTextColour() );
1525 dc
.SetFont( attr
.GetFont() );
1528 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1530 const wxString
& text
)
1532 wxCoord x
= 0, y
= 0, max_x
= 0;
1533 dc
.SetFont(attr
.GetFont());
1534 wxStringTokenizer
tk(text
, _T('\n'));
1535 while ( tk
.HasMoreTokens() )
1537 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1538 max_x
= wxMax(max_x
, x
);
1541 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1543 return wxSize(max_x
, y
);
1546 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1547 wxGridCellAttr
& attr
,
1551 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1554 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1555 wxGridCellAttr
& attr
,
1557 const wxRect
& rectCell
,
1561 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1563 // now we only have to draw the text
1564 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1567 attr
.GetAlignment(&hAlign
, &vAlign
);
1569 wxRect rect
= rectCell
;
1572 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1573 rect
, hAlign
, vAlign
);
1576 // ----------------------------------------------------------------------------
1577 // wxGridCellNumberRenderer
1578 // ----------------------------------------------------------------------------
1580 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1582 wxGridTableBase
*table
= grid
.GetTable();
1584 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1586 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1590 text
= table
->GetValue(row
, col
);
1596 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1597 wxGridCellAttr
& attr
,
1599 const wxRect
& rectCell
,
1603 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1605 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1607 // draw the text right aligned by default
1609 attr
.GetAlignment(&hAlign
, &vAlign
);
1610 hAlign
= wxALIGN_RIGHT
;
1612 wxRect rect
= rectCell
;
1615 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1618 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1619 wxGridCellAttr
& attr
,
1623 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1626 // ----------------------------------------------------------------------------
1627 // wxGridCellFloatRenderer
1628 // ----------------------------------------------------------------------------
1630 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1633 SetPrecision(precision
);
1636 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1638 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1639 renderer
->m_width
= m_width
;
1640 renderer
->m_precision
= m_precision
;
1641 renderer
->m_format
= m_format
;
1646 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1648 wxGridTableBase
*table
= grid
.GetTable();
1653 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1655 val
= table
->GetValueAsDouble(row
, col
);
1660 text
= table
->GetValue(row
, col
);
1661 hasDouble
= text
.ToDouble(&val
);
1668 if ( m_width
== -1 )
1670 if ( m_precision
== -1 )
1672 // default width/precision
1673 m_format
= _T("%f");
1677 m_format
.Printf(_T("%%.%df"), m_precision
);
1680 else if ( m_precision
== -1 )
1682 // default precision
1683 m_format
.Printf(_T("%%%d.f"), m_width
);
1687 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1691 text
.Printf(m_format
, val
);
1694 //else: text already contains the string
1699 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1700 wxGridCellAttr
& attr
,
1702 const wxRect
& rectCell
,
1706 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1708 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1710 // draw the text right aligned by default
1712 attr
.GetAlignment(&hAlign
, &vAlign
);
1713 hAlign
= wxALIGN_RIGHT
;
1715 wxRect rect
= rectCell
;
1718 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1721 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1722 wxGridCellAttr
& attr
,
1726 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1729 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1733 // reset to defaults
1739 wxString tmp
= params
.BeforeFirst(_T(','));
1743 if ( tmp
.ToLong(&width
) )
1745 SetWidth((int)width
);
1749 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1753 tmp
= params
.AfterFirst(_T(','));
1757 if ( tmp
.ToLong(&precision
) )
1759 SetPrecision((int)precision
);
1763 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1771 // ----------------------------------------------------------------------------
1772 // wxGridCellBoolRenderer
1773 // ----------------------------------------------------------------------------
1775 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1777 // FIXME these checkbox size calculations are really ugly...
1779 // between checkmark and box
1780 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1782 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1783 wxGridCellAttr
& WXUNUSED(attr
),
1788 // compute it only once (no locks for MT safeness in GUI thread...)
1789 if ( !ms_sizeCheckMark
.x
)
1791 // get checkbox size
1792 wxCoord checkSize
= 0;
1793 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1794 wxSize size
= checkbox
->GetBestSize();
1795 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1797 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1798 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1799 checkSize
-= size
.y
/ 2;
1804 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1807 return ms_sizeCheckMark
;
1810 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1811 wxGridCellAttr
& attr
,
1817 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1819 // draw a check mark in the centre (ignoring alignment - TODO)
1820 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1822 // don't draw outside the cell
1823 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1824 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1826 // and even leave (at least) 1 pixel margin
1827 size
.x
= size
.y
= minSize
- 2;
1830 // draw a border around checkmark
1832 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1833 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1834 rectBorder
.width
= size
.x
;
1835 rectBorder
.height
= size
.y
;
1838 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1839 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1842 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1843 value
= !( !cellval
|| (cellval
== "0") );
1848 wxRect rectMark
= rectBorder
;
1850 // MSW DrawCheckMark() is weird (and should probably be changed...)
1851 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1855 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1858 dc
.SetTextForeground(attr
.GetTextColour());
1859 dc
.DrawCheckMark(rectMark
);
1862 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1863 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1864 dc
.DrawRectangle(rectBorder
);
1867 // ----------------------------------------------------------------------------
1869 // ----------------------------------------------------------------------------
1871 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1875 m_isReadOnly
= Unset
;
1880 m_attrkind
= wxGridCellAttr::Cell
;
1882 SetDefAttr(attrDefault
);
1885 wxGridCellAttr
*wxGridCellAttr::Clone() const
1887 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1889 if ( HasTextColour() )
1890 attr
->SetTextColour(GetTextColour());
1891 if ( HasBackgroundColour() )
1892 attr
->SetBackgroundColour(GetBackgroundColour());
1894 attr
->SetFont(GetFont());
1895 if ( HasAlignment() )
1896 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1900 attr
->SetRenderer(m_renderer
);
1901 m_renderer
->IncRef();
1905 attr
->SetEditor(m_editor
);
1910 attr
->SetReadOnly();
1912 attr
->SetKind( m_attrkind
);
1917 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1919 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1920 SetTextColour(mergefrom
->GetTextColour());
1921 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1922 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1923 if ( !HasFont() && mergefrom
->HasFont() )
1924 SetFont(mergefrom
->GetFont());
1925 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1927 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1928 SetAlignment(hAlign
, vAlign
);
1931 // Directly access member functions as GetRender/Editor don't just return
1932 // m_renderer/m_editor
1934 // Maybe add support for merge of Render and Editor?
1935 if (!HasRenderer() && mergefrom
->HasRenderer() )
1937 m_renderer
= mergefrom
->m_renderer
;
1938 m_renderer
->IncRef();
1940 if ( !HasEditor() && mergefrom
->HasEditor() )
1942 m_editor
= mergefrom
->m_editor
;
1945 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1946 SetReadOnly(mergefrom
->IsReadOnly());
1948 SetDefAttr(mergefrom
->m_defGridAttr
);
1951 const wxColour
& wxGridCellAttr::GetTextColour() const
1953 if (HasTextColour())
1957 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1959 return m_defGridAttr
->GetTextColour();
1963 wxFAIL_MSG(wxT("Missing default cell attribute"));
1964 return wxNullColour
;
1969 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1971 if (HasBackgroundColour())
1973 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1974 return m_defGridAttr
->GetBackgroundColour();
1977 wxFAIL_MSG(wxT("Missing default cell attribute"));
1978 return wxNullColour
;
1983 const wxFont
& wxGridCellAttr::GetFont() const
1987 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1988 return m_defGridAttr
->GetFont();
1991 wxFAIL_MSG(wxT("Missing default cell attribute"));
1997 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2001 if ( hAlign
) *hAlign
= m_hAlign
;
2002 if ( vAlign
) *vAlign
= m_vAlign
;
2004 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2005 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2008 wxFAIL_MSG(wxT("Missing default cell attribute"));
2013 // GetRenderer and GetEditor use a slightly different decision path about
2014 // which attribute to use. If a non-default attr object has one then it is
2015 // used, otherwise the default editor or renderer is fetched from the grid and
2016 // used. It should be the default for the data type of the cell. If it is
2017 // NULL (because the table has a type that the grid does not have in its
2018 // registry,) then the grid's default editor or renderer is used.
2020 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2022 wxGridCellRenderer
*renderer
;
2024 if ( m_renderer
&& this != m_defGridAttr
)
2026 // use the cells renderer if it has one
2027 renderer
= m_renderer
;
2030 else // no non default cell renderer
2032 // get default renderer for the data type
2035 // GetDefaultRendererForCell() will do IncRef() for us
2036 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2045 if (m_defGridAttr
&& this != m_defGridAttr
)
2047 // if we still don't have one then use the grid default
2048 // (no need for IncRef() here neither)
2049 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2051 else // default grid attr
2053 // use m_renderer which we had decided not to use initially
2054 renderer
= m_renderer
;
2061 // we're supposed to always find something
2062 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2067 // same as above, except for s/renderer/editor/g
2068 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2070 wxGridCellEditor
*editor
;
2072 if ( m_editor
&& this != m_defGridAttr
)
2074 // use the cells editor if it has one
2078 else // no non default cell editor
2080 // get default editor for the data type
2083 // GetDefaultEditorForCell() will do IncRef() for us
2084 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2093 if ( m_defGridAttr
&& this != m_defGridAttr
)
2095 // if we still don't have one then use the grid default
2096 // (no need for IncRef() here neither)
2097 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2099 else // default grid attr
2101 // use m_editor which we had decided not to use initially
2109 // we're supposed to always find something
2110 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2115 // ----------------------------------------------------------------------------
2116 // wxGridCellAttrData
2117 // ----------------------------------------------------------------------------
2119 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2121 int n
= FindIndex(row
, col
);
2122 if ( n
== wxNOT_FOUND
)
2124 // add the attribute
2125 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2129 // free the old attribute
2130 m_attrs
[(size_t)n
].attr
->DecRef();
2134 // change the attribute
2135 m_attrs
[(size_t)n
].attr
= attr
;
2139 // remove this attribute
2140 m_attrs
.RemoveAt((size_t)n
);
2145 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2147 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2149 int n
= FindIndex(row
, col
);
2150 if ( n
!= wxNOT_FOUND
)
2152 attr
= m_attrs
[(size_t)n
].attr
;
2159 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2161 size_t count
= m_attrs
.GetCount();
2162 for ( size_t n
= 0; n
< count
; n
++ )
2164 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2165 wxCoord row
= coords
.GetRow();
2166 if ((size_t)row
>= pos
)
2170 // If rows inserted, include row counter where necessary
2171 coords
.SetRow(row
+ numRows
);
2173 else if (numRows
< 0)
2175 // If rows deleted ...
2176 if ((size_t)row
>= pos
- numRows
)
2178 // ...either decrement row counter (if row still exists)...
2179 coords
.SetRow(row
+ numRows
);
2183 // ...or remove the attribute
2184 m_attrs
.RemoveAt((size_t)n
);
2192 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2194 size_t count
= m_attrs
.GetCount();
2195 for ( size_t n
= 0; n
< count
; n
++ )
2197 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2198 wxCoord col
= coords
.GetCol();
2199 if ( (size_t)col
>= pos
)
2203 // If rows inserted, include row counter where necessary
2204 coords
.SetCol(col
+ numCols
);
2206 else if (numCols
< 0)
2208 // If rows deleted ...
2209 if ((size_t)col
>= pos
- numCols
)
2211 // ...either decrement row counter (if row still exists)...
2212 coords
.SetCol(col
+ numCols
);
2216 // ...or remove the attribute
2217 m_attrs
.RemoveAt((size_t)n
);
2225 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2227 size_t count
= m_attrs
.GetCount();
2228 for ( size_t n
= 0; n
< count
; n
++ )
2230 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2231 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2240 // ----------------------------------------------------------------------------
2241 // wxGridRowOrColAttrData
2242 // ----------------------------------------------------------------------------
2244 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2246 size_t count
= m_attrs
.Count();
2247 for ( size_t n
= 0; n
< count
; n
++ )
2249 m_attrs
[n
]->DecRef();
2253 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2255 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2257 int n
= m_rowsOrCols
.Index(rowOrCol
);
2258 if ( n
!= wxNOT_FOUND
)
2260 attr
= m_attrs
[(size_t)n
];
2267 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2269 int i
= m_rowsOrCols
.Index(rowOrCol
);
2270 if ( i
== wxNOT_FOUND
)
2272 // add the attribute
2273 m_rowsOrCols
.Add(rowOrCol
);
2278 size_t n
= (size_t)i
;
2281 // change the attribute
2282 m_attrs
[n
]->DecRef();
2287 // remove this attribute
2288 m_attrs
[n
]->DecRef();
2289 m_rowsOrCols
.RemoveAt(n
);
2290 m_attrs
.RemoveAt(n
);
2295 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2297 size_t count
= m_attrs
.GetCount();
2298 for ( size_t n
= 0; n
< count
; n
++ )
2300 int & rowOrCol
= m_rowsOrCols
[n
];
2301 if ( (size_t)rowOrCol
>= pos
)
2303 if ( numRowsOrCols
> 0 )
2305 // If rows inserted, include row counter where necessary
2306 rowOrCol
+= numRowsOrCols
;
2308 else if ( numRowsOrCols
< 0)
2310 // If rows deleted, either decrement row counter (if row still exists)
2311 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2312 rowOrCol
+= numRowsOrCols
;
2315 m_rowsOrCols
.RemoveAt((size_t)n
);
2316 m_attrs
.RemoveAt((size_t)n
);
2324 // ----------------------------------------------------------------------------
2325 // wxGridCellAttrProvider
2326 // ----------------------------------------------------------------------------
2328 wxGridCellAttrProvider::wxGridCellAttrProvider()
2330 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2333 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2338 void wxGridCellAttrProvider::InitData()
2340 m_data
= new wxGridCellAttrProviderData
;
2343 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2344 wxGridCellAttr::wxAttrKind kind
) const
2346 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2351 case (wxGridCellAttr::Any
):
2352 //Get cached merge attributes.
2353 // Currenlty not used as no cache implemented as not mutiable
2354 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2357 //Basicaly implement old version.
2358 //Also check merge cache, so we don't have to re-merge every time..
2359 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2360 *attrrow
= (wxGridCellAttr
*)NULL
,
2361 *attrcol
= (wxGridCellAttr
*)NULL
;
2363 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2364 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2365 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2367 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2368 // Two or move are non NULL
2369 attr
= new wxGridCellAttr
;
2370 attr
->SetKind(wxGridCellAttr::Merged
);
2374 attr
->MergeWith(attrcell
);
2378 attr
->MergeWith(attrcol
);
2382 attr
->MergeWith(attrrow
);
2385 //store merge attr if cache implemented
2387 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2391 // one or none is non null return it or null.
2392 if(attrrow
) attr
= attrrow
;
2393 if(attrcol
) attr
= attrcol
;
2394 if(attrcell
) attr
= attrcell
;
2398 case (wxGridCellAttr::Cell
):
2399 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2401 case (wxGridCellAttr::Col
):
2402 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2404 case (wxGridCellAttr::Row
):
2405 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2409 // (wxGridCellAttr::Default):
2410 // (wxGridCellAttr::Merged):
2417 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2423 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2426 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2431 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2434 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2439 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2442 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2446 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2448 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2452 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2456 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2458 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2462 // ----------------------------------------------------------------------------
2463 // wxGridTypeRegistry
2464 // ----------------------------------------------------------------------------
2466 wxGridTypeRegistry::~wxGridTypeRegistry()
2468 size_t count
= m_typeinfo
.Count();
2469 for ( size_t i
= 0; i
< count
; i
++ )
2470 delete m_typeinfo
[i
];
2474 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2475 wxGridCellRenderer
* renderer
,
2476 wxGridCellEditor
* editor
)
2478 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2480 // is it already registered?
2481 int loc
= FindRegisteredDataType(typeName
);
2482 if ( loc
!= wxNOT_FOUND
)
2484 delete m_typeinfo
[loc
];
2485 m_typeinfo
[loc
] = info
;
2489 m_typeinfo
.Add(info
);
2493 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2495 size_t count
= m_typeinfo
.GetCount();
2496 for ( size_t i
= 0; i
< count
; i
++ )
2498 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2507 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2509 int index
= FindRegisteredDataType(typeName
);
2510 if ( index
== wxNOT_FOUND
)
2512 // check whether this is one of the standard ones, in which case
2513 // register it "on the fly"
2515 if ( typeName
== wxGRID_VALUE_STRING
)
2517 RegisterDataType(wxGRID_VALUE_STRING
,
2518 new wxGridCellStringRenderer
,
2519 new wxGridCellTextEditor
);
2521 #endif // wxUSE_TEXTCTRL
2523 if ( typeName
== wxGRID_VALUE_BOOL
)
2525 RegisterDataType(wxGRID_VALUE_BOOL
,
2526 new wxGridCellBoolRenderer
,
2527 new wxGridCellBoolEditor
);
2529 #endif // wxUSE_CHECKBOX
2531 if ( typeName
== wxGRID_VALUE_NUMBER
)
2533 RegisterDataType(wxGRID_VALUE_NUMBER
,
2534 new wxGridCellNumberRenderer
,
2535 new wxGridCellNumberEditor
);
2537 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2539 RegisterDataType(wxGRID_VALUE_FLOAT
,
2540 new wxGridCellFloatRenderer
,
2541 new wxGridCellFloatEditor
);
2543 #endif // wxUSE_TEXTCTRL
2545 if ( typeName
== wxGRID_VALUE_CHOICE
)
2547 RegisterDataType(wxGRID_VALUE_CHOICE
,
2548 new wxGridCellStringRenderer
,
2549 new wxGridCellChoiceEditor
);
2551 #endif // wxUSE_COMBOBOX
2556 // we get here only if just added the entry for this type, so return
2558 index
= m_typeinfo
.GetCount() - 1;
2564 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2566 int index
= FindDataType(typeName
);
2567 if ( index
== wxNOT_FOUND
)
2569 // the first part of the typename is the "real" type, anything after ':'
2570 // are the parameters for the renderer
2571 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2572 if ( index
== wxNOT_FOUND
)
2577 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2578 wxGridCellRenderer
*rendererOld
= renderer
;
2579 renderer
= renderer
->Clone();
2580 rendererOld
->DecRef();
2582 wxGridCellEditor
*editor
= GetEditor(index
);
2583 wxGridCellEditor
*editorOld
= editor
;
2584 editor
= editor
->Clone();
2585 editorOld
->DecRef();
2587 // do it even if there are no parameters to reset them to defaults
2588 wxString params
= typeName
.AfterFirst(_T(':'));
2589 renderer
->SetParameters(params
);
2590 editor
->SetParameters(params
);
2592 // register the new typename
2593 RegisterDataType(typeName
, renderer
, editor
);
2595 // we just registered it, it's the last one
2596 index
= m_typeinfo
.GetCount() - 1;
2602 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2604 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2610 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2612 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2618 // ----------------------------------------------------------------------------
2620 // ----------------------------------------------------------------------------
2622 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2625 wxGridTableBase::wxGridTableBase()
2627 m_view
= (wxGrid
*) NULL
;
2628 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2631 wxGridTableBase::~wxGridTableBase()
2633 delete m_attrProvider
;
2636 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2638 delete m_attrProvider
;
2639 m_attrProvider
= attrProvider
;
2642 bool wxGridTableBase::CanHaveAttributes()
2644 if ( ! GetAttrProvider() )
2646 // use the default attr provider by default
2647 SetAttrProvider(new wxGridCellAttrProvider
);
2652 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2654 if ( m_attrProvider
)
2655 return m_attrProvider
->GetAttr(row
, col
, kind
);
2657 return (wxGridCellAttr
*)NULL
;
2660 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2662 if ( m_attrProvider
)
2664 attr
->SetKind(wxGridCellAttr::Cell
);
2665 m_attrProvider
->SetAttr(attr
, row
, col
);
2669 // as we take ownership of the pointer and don't store it, we must
2675 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2677 if ( m_attrProvider
)
2679 attr
->SetKind(wxGridCellAttr::Row
);
2680 m_attrProvider
->SetRowAttr(attr
, row
);
2684 // as we take ownership of the pointer and don't store it, we must
2690 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2692 if ( m_attrProvider
)
2694 attr
->SetKind(wxGridCellAttr::Col
);
2695 m_attrProvider
->SetColAttr(attr
, col
);
2699 // as we take ownership of the pointer and don't store it, we must
2705 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2706 size_t WXUNUSED(numRows
) )
2708 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2713 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2715 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2720 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2721 size_t WXUNUSED(numRows
) )
2723 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2728 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2729 size_t WXUNUSED(numCols
) )
2731 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2736 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2738 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2743 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2744 size_t WXUNUSED(numCols
) )
2746 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2752 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2755 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2756 // how much it makes sense to us geeks.
2760 wxString
wxGridTableBase::GetColLabelValue( int col
)
2762 // default col labels are:
2763 // cols 0 to 25 : A-Z
2764 // cols 26 to 675 : AA-ZZ
2769 for ( n
= 1; ; n
++ )
2771 s
+= (_T('A') + (wxChar
)( col%26
));
2773 if ( col
< 0 ) break;
2776 // reverse the string...
2778 for ( i
= 0; i
< n
; i
++ )
2787 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2789 return wxGRID_VALUE_STRING
;
2792 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2793 const wxString
& typeName
)
2795 return typeName
== wxGRID_VALUE_STRING
;
2798 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2800 return CanGetValueAs(row
, col
, typeName
);
2803 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2808 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2813 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2818 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2819 long WXUNUSED(value
) )
2823 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2824 double WXUNUSED(value
) )
2828 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2829 bool WXUNUSED(value
) )
2834 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2835 const wxString
& WXUNUSED(typeName
) )
2840 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2841 const wxString
& WXUNUSED(typeName
),
2842 void* WXUNUSED(value
) )
2846 //////////////////////////////////////////////////////////////////////
2848 // Message class for the grid table to send requests and notifications
2852 wxGridTableMessage::wxGridTableMessage()
2854 m_table
= (wxGridTableBase
*) NULL
;
2860 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2861 int commandInt1
, int commandInt2
)
2865 m_comInt1
= commandInt1
;
2866 m_comInt2
= commandInt2
;
2871 //////////////////////////////////////////////////////////////////////
2873 // A basic grid table for string data. An object of this class will
2874 // created by wxGrid if you don't specify an alternative table class.
2877 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2879 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2881 wxGridStringTable::wxGridStringTable()
2886 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2891 m_data
.Alloc( numRows
);
2894 sa
.Alloc( numCols
);
2895 for ( col
= 0; col
< numCols
; col
++ )
2897 sa
.Add( wxEmptyString
);
2900 for ( row
= 0; row
< numRows
; row
++ )
2906 wxGridStringTable::~wxGridStringTable()
2910 int wxGridStringTable::GetNumberRows()
2912 return m_data
.GetCount();
2915 int wxGridStringTable::GetNumberCols()
2917 if ( m_data
.GetCount() > 0 )
2918 return m_data
[0].GetCount();
2923 wxString
wxGridStringTable::GetValue( int row
, int col
)
2925 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2926 _T("invalid row or column index in wxGridStringTable") );
2928 return m_data
[row
][col
];
2931 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2933 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2934 _T("invalid row or column index in wxGridStringTable") );
2936 m_data
[row
][col
] = value
;
2939 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2941 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2942 _T("invalid row or column index in wxGridStringTable") );
2944 return (m_data
[row
][col
] == wxEmptyString
);
2947 void wxGridStringTable::Clear()
2950 int numRows
, numCols
;
2952 numRows
= m_data
.GetCount();
2955 numCols
= m_data
[0].GetCount();
2957 for ( row
= 0; row
< numRows
; row
++ )
2959 for ( col
= 0; col
< numCols
; col
++ )
2961 m_data
[row
][col
] = wxEmptyString
;
2968 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2972 size_t curNumRows
= m_data
.GetCount();
2973 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2974 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2976 if ( pos
>= curNumRows
)
2978 return AppendRows( numRows
);
2982 sa
.Alloc( curNumCols
);
2983 for ( col
= 0; col
< curNumCols
; col
++ )
2985 sa
.Add( wxEmptyString
);
2988 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2990 m_data
.Insert( sa
, row
);
2994 wxGridTableMessage
msg( this,
2995 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2999 GetView()->ProcessTableMessage( msg
);
3005 bool wxGridStringTable::AppendRows( size_t numRows
)
3009 size_t curNumRows
= m_data
.GetCount();
3010 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3011 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3014 if ( curNumCols
> 0 )
3016 sa
.Alloc( curNumCols
);
3017 for ( col
= 0; col
< curNumCols
; col
++ )
3019 sa
.Add( wxEmptyString
);
3023 for ( row
= 0; row
< numRows
; row
++ )
3030 wxGridTableMessage
msg( this,
3031 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3034 GetView()->ProcessTableMessage( msg
);
3040 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3044 size_t curNumRows
= m_data
.GetCount();
3046 if ( pos
>= curNumRows
)
3049 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3050 pos
, numRows
, curNumRows
);
3051 wxFAIL_MSG( errmsg
);
3055 if ( numRows
> curNumRows
- pos
)
3057 numRows
= curNumRows
- pos
;
3060 if ( numRows
>= curNumRows
)
3062 m_data
.Empty(); // don't release memory just yet
3066 for ( n
= 0; n
< numRows
; n
++ )
3068 m_data
.RemoveAt( pos
);
3073 wxGridTableMessage
msg( this,
3074 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3078 GetView()->ProcessTableMessage( msg
);
3084 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3088 size_t curNumRows
= m_data
.GetCount();
3089 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3090 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3092 if ( pos
>= curNumCols
)
3094 return AppendCols( numCols
);
3097 for ( row
= 0; row
< curNumRows
; row
++ )
3099 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3101 m_data
[row
].Insert( wxEmptyString
, col
);
3106 wxGridTableMessage
msg( this,
3107 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3111 GetView()->ProcessTableMessage( msg
);
3117 bool wxGridStringTable::AppendCols( size_t numCols
)
3121 size_t curNumRows
= m_data
.GetCount();
3125 // TODO: something better than this ?
3127 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3132 for ( row
= 0; row
< curNumRows
; row
++ )
3134 for ( n
= 0; n
< numCols
; n
++ )
3136 m_data
[row
].Add( wxEmptyString
);
3142 wxGridTableMessage
msg( this,
3143 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3146 GetView()->ProcessTableMessage( msg
);
3152 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3156 size_t curNumRows
= m_data
.GetCount();
3157 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3158 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3160 if ( pos
>= curNumCols
)
3163 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3164 pos
, numCols
, curNumCols
);
3165 wxFAIL_MSG( errmsg
);
3169 if ( numCols
> curNumCols
- pos
)
3171 numCols
= curNumCols
- pos
;
3174 for ( row
= 0; row
< curNumRows
; row
++ )
3176 if ( numCols
>= curNumCols
)
3178 m_data
[row
].Clear();
3182 for ( n
= 0; n
< numCols
; n
++ )
3184 m_data
[row
].RemoveAt( pos
);
3190 wxGridTableMessage
msg( this,
3191 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3195 GetView()->ProcessTableMessage( msg
);
3201 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3203 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3205 // using default label
3207 return wxGridTableBase::GetRowLabelValue( row
);
3211 return m_rowLabels
[ row
];
3215 wxString
wxGridStringTable::GetColLabelValue( int col
)
3217 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3219 // using default label
3221 return wxGridTableBase::GetColLabelValue( col
);
3225 return m_colLabels
[ col
];
3229 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3231 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3233 int n
= m_rowLabels
.GetCount();
3235 for ( i
= n
; i
<= row
; i
++ )
3237 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3241 m_rowLabels
[row
] = value
;
3244 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3246 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3248 int n
= m_colLabels
.GetCount();
3250 for ( i
= n
; i
<= col
; i
++ )
3252 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3256 m_colLabels
[col
] = value
;
3261 //////////////////////////////////////////////////////////////////////
3262 //////////////////////////////////////////////////////////////////////
3264 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3266 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3267 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3268 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3269 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3270 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3271 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3274 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3276 const wxPoint
&pos
, const wxSize
&size
)
3277 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3282 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3286 // NO - don't do this because it will set both the x and y origin
3287 // coords to match the parent scrolled window and we just want to
3288 // set the y coord - MB
3290 // m_owner->PrepareDC( dc );
3293 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3294 dc
.SetDeviceOrigin( 0, -y
);
3296 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3297 m_owner
->DrawRowLabels( dc
, rows
);
3301 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3303 m_owner
->ProcessRowLabelMouseEvent( event
);
3307 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3309 m_owner
->GetEventHandler()->ProcessEvent(event
);
3313 // This seems to be required for wxMotif otherwise the mouse
3314 // cursor must be in the cell edit control to get key events
3316 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3318 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3321 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3323 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3328 //////////////////////////////////////////////////////////////////////
3330 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3332 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3333 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3334 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3335 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3336 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3337 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3340 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3342 const wxPoint
&pos
, const wxSize
&size
)
3343 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3348 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3352 // NO - don't do this because it will set both the x and y origin
3353 // coords to match the parent scrolled window and we just want to
3354 // set the x coord - MB
3356 // m_owner->PrepareDC( dc );
3359 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3360 dc
.SetDeviceOrigin( -x
, 0 );
3362 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3363 m_owner
->DrawColLabels( dc
, cols
);
3367 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3369 m_owner
->ProcessColLabelMouseEvent( event
);
3372 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3374 m_owner
->GetEventHandler()->ProcessEvent(event
);
3378 // This seems to be required for wxMotif otherwise the mouse
3379 // cursor must be in the cell edit control to get key events
3381 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3383 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3386 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3388 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3393 //////////////////////////////////////////////////////////////////////
3395 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3397 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3398 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3399 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3400 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3401 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3402 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3405 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3407 const wxPoint
&pos
, const wxSize
&size
)
3408 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3413 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3417 int client_height
= 0;
3418 int client_width
= 0;
3419 GetClientSize( &client_width
, &client_height
);
3421 dc
.SetPen( *wxBLACK_PEN
);
3422 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3423 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3425 dc
.SetPen( *wxWHITE_PEN
);
3426 dc
.DrawLine( 0, 0, client_width
, 0 );
3427 dc
.DrawLine( 0, 0, 0, client_height
);
3431 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3433 m_owner
->ProcessCornerLabelMouseEvent( event
);
3437 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3439 m_owner
->GetEventHandler()->ProcessEvent(event
);
3442 // This seems to be required for wxMotif otherwise the mouse
3443 // cursor must be in the cell edit control to get key events
3445 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3447 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3450 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3452 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3457 //////////////////////////////////////////////////////////////////////
3459 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3461 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3462 EVT_PAINT( wxGridWindow::OnPaint
)
3463 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3464 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3465 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3466 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3467 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3470 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3471 wxGridRowLabelWindow
*rowLblWin
,
3472 wxGridColLabelWindow
*colLblWin
,
3473 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3474 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3477 m_rowLabelWin
= rowLblWin
;
3478 m_colLabelWin
= colLblWin
;
3479 SetBackgroundColour(_T("WHITE"));
3483 wxGridWindow::~wxGridWindow()
3488 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3490 wxPaintDC
dc( this );
3491 m_owner
->PrepareDC( dc
);
3492 wxRegion reg
= GetUpdateRegion();
3493 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3494 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3495 #if WXGRID_DRAW_LINES
3496 m_owner
->DrawAllGridLines( dc
, reg
);
3498 m_owner
->DrawGridSpace( dc
);
3499 m_owner
->DrawHighlight( dc
, DirtyCells
);
3503 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3505 wxWindow::ScrollWindow( dx
, dy
, rect
);
3506 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3507 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3511 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3513 m_owner
->ProcessGridCellMouseEvent( event
);
3516 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3518 m_owner
->GetEventHandler()->ProcessEvent(event
);
3521 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3522 // cursor must be in the cell edit control to get key events
3524 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3526 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3529 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3531 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3534 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3539 //////////////////////////////////////////////////////////////////////
3541 // Internal Helper function for computing row or column from some
3542 // (unscrolled) coordinate value, using either
3543 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3544 // of m_rowBottoms/m_ColRights to speed up the search!
3546 // Internal helper macros for simpler use of that function
3548 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3549 const wxArrayInt
& BorderArray
, bool maxOnOverflow
);
3551 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3552 WXGRID_MIN_COL_WIDTH, \
3554 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3555 WXGRID_MIN_ROW_HEIGHT, \
3557 /////////////////////////////////////////////////////////////////////
3559 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3561 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3562 EVT_PAINT( wxGrid::OnPaint
)
3563 EVT_SIZE( wxGrid::OnSize
)
3564 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3565 EVT_KEY_UP( wxGrid::OnKeyUp
)
3566 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3569 wxGrid::wxGrid( wxWindow
*parent
,
3574 const wxString
& name
)
3575 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3576 m_colMinWidths(GRID_HASH_SIZE
),
3577 m_rowMinHeights(GRID_HASH_SIZE
)
3585 // Must do this or ~wxScrollHelper will pop the wrong event handler
3586 SetTargetWindow(this);
3588 wxSafeDecRef(m_defaultCellAttr
);
3590 #ifdef DEBUG_ATTR_CACHE
3591 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3592 wxPrintf(_T("wxGrid attribute cache statistics: "
3593 "total: %u, hits: %u (%u%%)\n"),
3594 total
, gs_nAttrCacheHits
,
3595 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3601 delete m_typeRegistry
;
3607 // ----- internal init and update functions
3610 void wxGrid::Create()
3612 m_created
= FALSE
; // set to TRUE by CreateGrid
3614 m_table
= (wxGridTableBase
*) NULL
;
3617 m_cellEditCtrlEnabled
= FALSE
;
3619 m_defaultCellAttr
= new wxGridCellAttr();
3621 // Set default cell attributes
3622 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3623 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3624 m_defaultCellAttr
->SetFont(GetFont());
3625 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3626 m_defaultCellAttr
->SetTextColour(
3627 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3628 m_defaultCellAttr
->SetBackgroundColour(
3629 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3630 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3631 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3636 m_currentCellCoords
= wxGridNoCellCoords
;
3638 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3639 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3641 // create the type registry
3642 m_typeRegistry
= new wxGridTypeRegistry
;
3645 // subwindow components that make up the wxGrid
3646 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3651 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3656 m_colLabelWin
= new wxGridColLabelWindow( this,
3661 m_gridWin
= new wxGridWindow( this,
3668 SetTargetWindow( m_gridWin
);
3674 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3675 wxGrid::wxGridSelectionModes selmode
)
3677 wxCHECK_MSG( !m_created
,
3679 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3681 m_numRows
= numRows
;
3682 m_numCols
= numCols
;
3684 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3685 m_table
->SetView( this );
3687 m_selection
= new wxGridSelection( this, selmode
);
3696 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3698 wxCHECK_RET( m_created
,
3699 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3701 m_selection
->SetSelectionMode( selmode
);
3704 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3705 wxGrid::wxGridSelectionModes selmode
)
3709 // RD: Actually, this should probably be allowed. I think it would be
3710 // nice to be able to switch multiple Tables in and out of a single
3711 // View at runtime. Is there anything in the implementation that
3712 // would prevent this?
3714 // At least, you now have to cope with m_selection
3715 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3720 m_numRows
= table
->GetNumberRows();
3721 m_numCols
= table
->GetNumberCols();
3724 m_table
->SetView( this );
3727 m_selection
= new wxGridSelection( this, selmode
);
3740 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3741 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3743 if ( m_rowLabelWin
)
3745 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3749 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3752 m_labelTextColour
= wxColour( _T("BLACK") );
3755 m_attrCache
.row
= -1;
3757 // TODO: something better than this ?
3759 m_labelFont
= this->GetFont();
3760 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3762 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3763 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3765 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3766 m_colLabelVertAlign
= wxALIGN_TOP
;
3768 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3769 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3771 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3772 m_defaultRowHeight
+= 8;
3774 m_defaultRowHeight
+= 4;
3777 m_gridLineColour
= wxColour( 128, 128, 255 );
3778 m_gridLinesEnabled
= TRUE
;
3779 m_cellHighlightColour
= m_gridLineColour
;
3780 m_cellHighlightPenWidth
= 2;
3781 m_cellHighlightROPenWidth
= 1;
3783 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3784 m_winCapture
= (wxWindow
*)NULL
;
3785 m_canDragRowSize
= TRUE
;
3786 m_canDragColSize
= TRUE
;
3787 m_canDragGridSize
= TRUE
;
3789 m_dragRowOrCol
= -1;
3790 m_isDragging
= FALSE
;
3791 m_startDragPos
= wxDefaultPosition
;
3793 m_waitForSlowClick
= FALSE
;
3795 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3796 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3798 m_currentCellCoords
= wxGridNoCellCoords
;
3800 m_selectingTopLeft
= wxGridNoCellCoords
;
3801 m_selectingBottomRight
= wxGridNoCellCoords
;
3802 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3803 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3805 m_editable
= TRUE
; // default for whole grid
3807 m_inOnKeyDown
= FALSE
;
3814 // ----------------------------------------------------------------------------
3815 // the idea is to call these functions only when necessary because they create
3816 // quite big arrays which eat memory mostly unnecessary - in particular, if
3817 // default widths/heights are used for all rows/columns, we may not use these
3820 // with some extra code, it should be possible to only store the
3821 // widths/heights different from default ones but this will be done later...
3822 // ----------------------------------------------------------------------------
3824 void wxGrid::InitRowHeights()
3826 m_rowHeights
.Empty();
3827 m_rowBottoms
.Empty();
3829 m_rowHeights
.Alloc( m_numRows
);
3830 m_rowBottoms
.Alloc( m_numRows
);
3833 for ( int i
= 0; i
< m_numRows
; i
++ )
3835 m_rowHeights
.Add( m_defaultRowHeight
);
3836 rowBottom
+= m_defaultRowHeight
;
3837 m_rowBottoms
.Add( rowBottom
);
3841 void wxGrid::InitColWidths()
3843 m_colWidths
.Empty();
3844 m_colRights
.Empty();
3846 m_colWidths
.Alloc( m_numCols
);
3847 m_colRights
.Alloc( m_numCols
);
3849 for ( int i
= 0; i
< m_numCols
; i
++ )
3851 m_colWidths
.Add( m_defaultColWidth
);
3852 colRight
+= m_defaultColWidth
;
3853 m_colRights
.Add( colRight
);
3857 int wxGrid::GetColWidth(int col
) const
3859 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3862 int wxGrid::GetColLeft(int col
) const
3864 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3865 : m_colRights
[col
] - m_colWidths
[col
];
3868 int wxGrid::GetColRight(int col
) const
3870 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3874 int wxGrid::GetRowHeight(int row
) const
3876 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3879 int wxGrid::GetRowTop(int row
) const
3881 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3882 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3885 int wxGrid::GetRowBottom(int row
) const
3887 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3888 : m_rowBottoms
[row
];
3891 void wxGrid::CalcDimensions()
3894 GetClientSize( &cw
, &ch
);
3896 if ( m_rowLabelWin
->IsShown() )
3897 cw
-= m_rowLabelWidth
;
3898 if ( m_colLabelWin
->IsShown() )
3899 ch
-= m_colLabelHeight
;
3902 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3903 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3905 // preserve (more or less) the previous position
3907 GetViewStart( &x
, &y
);
3909 // maybe we don't need scrollbars at all?
3911 // also adjust the position to be valid for the new scroll rangs
3932 // do set scrollbar parameters
3933 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3934 GetScrollX(w
), GetScrollY(h
), x
, y
,
3935 GetBatchCount() != 0);
3937 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3938 // still must reposition the children
3943 void wxGrid::CalcWindowSizes()
3946 GetClientSize( &cw
, &ch
);
3948 if ( m_cornerLabelWin
->IsShown() )
3949 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3951 if ( m_colLabelWin
->IsShown() )
3952 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3954 if ( m_rowLabelWin
->IsShown() )
3955 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3957 if ( m_gridWin
->IsShown() )
3958 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3962 // this is called when the grid table sends a message to say that it
3963 // has been redimensioned
3965 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3968 bool result
= FALSE
;
3971 // if we were using the default widths/heights so far, we must change them
3973 if ( m_colWidths
.IsEmpty() )
3978 if ( m_rowHeights
.IsEmpty() )
3984 switch ( msg
.GetId() )
3986 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3988 size_t pos
= msg
.GetCommandInt();
3989 int numRows
= msg
.GetCommandInt2();
3991 m_numRows
+= numRows
;
3993 if ( !m_rowHeights
.IsEmpty() )
3995 for ( i
= 0; i
< numRows
; i
++ )
3997 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3998 m_rowBottoms
.Insert( 0, pos
);
4002 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4004 for ( i
= pos
; i
< m_numRows
; i
++ )
4006 bottom
+= m_rowHeights
[i
];
4007 m_rowBottoms
[i
] = bottom
;
4010 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4012 // if we have just inserted cols into an empty grid the current
4013 // cell will be undefined...
4015 SetCurrentCell( 0, 0 );
4019 m_selection
->UpdateRows( pos
, numRows
);
4020 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4022 attrProvider
->UpdateAttrRows( pos
, numRows
);
4024 if ( !GetBatchCount() )
4027 m_rowLabelWin
->Refresh();
4033 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4035 int numRows
= msg
.GetCommandInt();
4036 int oldNumRows
= m_numRows
;
4037 m_numRows
+= numRows
;
4039 if ( !m_rowHeights
.IsEmpty() )
4041 for ( i
= 0; i
< numRows
; i
++ )
4043 m_rowHeights
.Add( m_defaultRowHeight
);
4044 m_rowBottoms
.Add( 0 );
4048 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4050 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4052 bottom
+= m_rowHeights
[i
];
4053 m_rowBottoms
[i
] = bottom
;
4056 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4058 // if we have just inserted cols into an empty grid the current
4059 // cell will be undefined...
4061 SetCurrentCell( 0, 0 );
4063 if ( !GetBatchCount() )
4066 m_rowLabelWin
->Refresh();
4072 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4074 size_t pos
= msg
.GetCommandInt();
4075 int numRows
= msg
.GetCommandInt2();
4076 m_numRows
-= numRows
;
4078 if ( !m_rowHeights
.IsEmpty() )
4080 for ( i
= 0; i
< numRows
; i
++ )
4082 m_rowHeights
.RemoveAt( pos
);
4083 m_rowBottoms
.RemoveAt( pos
);
4087 for ( i
= 0; i
< m_numRows
; i
++ )
4089 h
+= m_rowHeights
[i
];
4090 m_rowBottoms
[i
] = h
;
4095 m_currentCellCoords
= wxGridNoCellCoords
;
4099 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4100 m_currentCellCoords
.Set( 0, 0 );
4104 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4105 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4107 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4108 // ifdef'd out following patch from Paul Gammans
4110 // No need to touch column attributes, unless we
4111 // removed _all_ rows, in this case, we remove
4112 // all column attributes.
4113 // I hate to do this here, but the
4114 // needed data is not available inside UpdateAttrRows.
4115 if ( !GetNumberRows() )
4116 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4119 if ( !GetBatchCount() )
4122 m_rowLabelWin
->Refresh();
4128 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4130 size_t pos
= msg
.GetCommandInt();
4131 int numCols
= msg
.GetCommandInt2();
4132 m_numCols
+= numCols
;
4134 if ( !m_colWidths
.IsEmpty() )
4136 for ( i
= 0; i
< numCols
; i
++ )
4138 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4139 m_colRights
.Insert( 0, pos
);
4143 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4145 for ( i
= pos
; i
< m_numCols
; i
++ )
4147 right
+= m_colWidths
[i
];
4148 m_colRights
[i
] = right
;
4151 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4153 // if we have just inserted cols into an empty grid the current
4154 // cell will be undefined...
4156 SetCurrentCell( 0, 0 );
4160 m_selection
->UpdateCols( pos
, numCols
);
4161 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4163 attrProvider
->UpdateAttrCols( pos
, numCols
);
4164 if ( !GetBatchCount() )
4167 m_colLabelWin
->Refresh();
4174 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4176 int numCols
= msg
.GetCommandInt();
4177 int oldNumCols
= m_numCols
;
4178 m_numCols
+= numCols
;
4179 if ( !m_colWidths
.IsEmpty() )
4181 for ( i
= 0; i
< numCols
; i
++ )
4183 m_colWidths
.Add( m_defaultColWidth
);
4184 m_colRights
.Add( 0 );
4188 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4190 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4192 right
+= m_colWidths
[i
];
4193 m_colRights
[i
] = right
;
4196 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4198 // if we have just inserted cols into an empty grid the current
4199 // cell will be undefined...
4201 SetCurrentCell( 0, 0 );
4203 if ( !GetBatchCount() )
4206 m_colLabelWin
->Refresh();
4212 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4214 size_t pos
= msg
.GetCommandInt();
4215 int numCols
= msg
.GetCommandInt2();
4216 m_numCols
-= numCols
;
4218 if ( !m_colWidths
.IsEmpty() )
4220 for ( i
= 0; i
< numCols
; i
++ )
4222 m_colWidths
.RemoveAt( pos
);
4223 m_colRights
.RemoveAt( pos
);
4227 for ( i
= 0; i
< m_numCols
; i
++ )
4229 w
+= m_colWidths
[i
];
4235 m_currentCellCoords
= wxGridNoCellCoords
;
4239 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4240 m_currentCellCoords
.Set( 0, 0 );
4244 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4245 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4247 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4248 // ifdef'd out following patch from Paul Gammans
4250 // No need to touch row attributes, unless we
4251 // removed _all_ columns, in this case, we remove
4252 // all row attributes.
4253 // I hate to do this here, but the
4254 // needed data is not available inside UpdateAttrCols.
4255 if ( !GetNumberCols() )
4256 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4259 if ( !GetBatchCount() )
4262 m_colLabelWin
->Refresh();
4269 if (result
&& !GetBatchCount() )
4270 m_gridWin
->Refresh();
4275 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4277 wxRegionIterator
iter( reg
);
4280 wxArrayInt rowlabels
;
4287 // TODO: remove this when we can...
4288 // There is a bug in wxMotif that gives garbage update
4289 // rectangles if you jump-scroll a long way by clicking the
4290 // scrollbar with middle button. This is a work-around
4292 #if defined(__WXMOTIF__)
4294 m_gridWin
->GetClientSize( &cw
, &ch
);
4295 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4296 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4299 // logical bounds of update region
4302 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4303 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4305 // find the row labels within these bounds
4308 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4310 if ( GetRowBottom(row
) < top
)
4313 if ( GetRowTop(row
) > bottom
)
4316 rowlabels
.Add( row
);
4326 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4328 wxRegionIterator
iter( reg
);
4331 wxArrayInt colLabels
;
4338 // TODO: remove this when we can...
4339 // There is a bug in wxMotif that gives garbage update
4340 // rectangles if you jump-scroll a long way by clicking the
4341 // scrollbar with middle button. This is a work-around
4343 #if defined(__WXMOTIF__)
4345 m_gridWin
->GetClientSize( &cw
, &ch
);
4346 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4347 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4350 // logical bounds of update region
4353 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4354 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4356 // find the cells within these bounds
4359 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4361 if ( GetColRight(col
) < left
)
4364 if ( GetColLeft(col
) > right
)
4367 colLabels
.Add( col
);
4376 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4378 wxRegionIterator
iter( reg
);
4381 wxGridCellCoordsArray cellsExposed
;
4383 int left
, top
, right
, bottom
;
4388 // TODO: remove this when we can...
4389 // There is a bug in wxMotif that gives garbage update
4390 // rectangles if you jump-scroll a long way by clicking the
4391 // scrollbar with middle button. This is a work-around
4393 #if defined(__WXMOTIF__)
4395 m_gridWin
->GetClientSize( &cw
, &ch
);
4396 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4397 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4398 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4399 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4402 // logical bounds of update region
4404 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4405 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4407 // find the cells within these bounds
4410 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4412 if ( GetRowBottom(row
) <= top
)
4415 if ( GetRowTop(row
) > bottom
)
4418 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4420 if ( GetColRight(col
) <= left
)
4423 if ( GetColLeft(col
) > right
)
4426 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4433 return cellsExposed
;
4437 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4440 wxPoint
pos( event
.GetPosition() );
4441 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4443 if ( event
.Dragging() )
4447 m_isDragging
= TRUE
;
4448 m_rowLabelWin
->CaptureMouse();
4451 if ( event
.LeftIsDown() )
4453 switch( m_cursorMode
)
4455 case WXGRID_CURSOR_RESIZE_ROW
:
4457 int cw
, ch
, left
, dummy
;
4458 m_gridWin
->GetClientSize( &cw
, &ch
);
4459 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4461 wxClientDC
dc( m_gridWin
);
4464 GetRowTop(m_dragRowOrCol
) +
4465 GetRowMinimalHeight(m_dragRowOrCol
) );
4466 dc
.SetLogicalFunction(wxINVERT
);
4467 if ( m_dragLastPos
>= 0 )
4469 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4471 dc
.DrawLine( left
, y
, left
+cw
, y
);
4476 case WXGRID_CURSOR_SELECT_ROW
:
4477 if ( (row
= YToRow( y
)) >= 0 )
4481 m_selection
->SelectRow( row
,
4482 event
.ControlDown(),
4489 // default label to suppress warnings about "enumeration value
4490 // 'xxx' not handled in switch
4498 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4503 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4504 m_isDragging
= FALSE
;
4507 // ------------ Entering or leaving the window
4509 if ( event
.Entering() || event
.Leaving() )
4511 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4515 // ------------ Left button pressed
4517 else if ( event
.LeftDown() )
4519 // don't send a label click event for a hit on the
4520 // edge of the row label - this is probably the user
4521 // wanting to resize the row
4523 if ( YToEdgeOfRow(y
) < 0 )
4527 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4529 if ( !event
.ShiftDown() && !event
.ControlDown() )
4531 else if ( m_selection
)
4533 if ( event
.ShiftDown() )
4535 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4538 GetNumberCols() - 1,
4539 event
.ControlDown(),
4546 m_selection
->SelectRow( row
,
4547 event
.ControlDown(),
4554 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4559 // starting to drag-resize a row
4561 if ( CanDragRowSize() )
4562 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4567 // ------------ Left double click
4569 else if (event
.LeftDClick() )
4571 if ( YToEdgeOfRow(y
) < 0 )
4574 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4579 // ------------ Left button released
4581 else if ( event
.LeftUp() )
4583 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4585 DoEndDragResizeRow();
4587 // Note: we are ending the event *after* doing
4588 // default processing in this case
4590 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4593 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4598 // ------------ Right button down
4600 else if ( event
.RightDown() )
4603 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4605 // no default action at the moment
4610 // ------------ Right double click
4612 else if ( event
.RightDClick() )
4615 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4617 // no default action at the moment
4622 // ------------ No buttons down and mouse moving
4624 else if ( event
.Moving() )
4626 m_dragRowOrCol
= YToEdgeOfRow( y
);
4627 if ( m_dragRowOrCol
>= 0 )
4629 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4631 // don't capture the mouse yet
4632 if ( CanDragRowSize() )
4633 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4636 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4638 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4644 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4647 wxPoint
pos( event
.GetPosition() );
4648 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4650 if ( event
.Dragging() )
4654 m_isDragging
= TRUE
;
4655 m_colLabelWin
->CaptureMouse();
4658 if ( event
.LeftIsDown() )
4660 switch( m_cursorMode
)
4662 case WXGRID_CURSOR_RESIZE_COL
:
4664 int cw
, ch
, dummy
, top
;
4665 m_gridWin
->GetClientSize( &cw
, &ch
);
4666 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4668 wxClientDC
dc( m_gridWin
);
4671 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4672 GetColMinimalWidth(m_dragRowOrCol
));
4673 dc
.SetLogicalFunction(wxINVERT
);
4674 if ( m_dragLastPos
>= 0 )
4676 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4678 dc
.DrawLine( x
, top
, x
, top
+ch
);
4683 case WXGRID_CURSOR_SELECT_COL
:
4684 if ( (col
= XToCol( x
)) >= 0 )
4688 m_selection
->SelectCol( col
,
4689 event
.ControlDown(),
4696 // default label to suppress warnings about "enumeration value
4697 // 'xxx' not handled in switch
4705 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4710 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4711 m_isDragging
= FALSE
;
4714 // ------------ Entering or leaving the window
4716 if ( event
.Entering() || event
.Leaving() )
4718 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4722 // ------------ Left button pressed
4724 else if ( event
.LeftDown() )
4726 // don't send a label click event for a hit on the
4727 // edge of the col label - this is probably the user
4728 // wanting to resize the col
4730 if ( XToEdgeOfCol(x
) < 0 )
4734 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4736 if ( !event
.ShiftDown() && !event
.ControlDown() )
4740 if ( event
.ShiftDown() )
4742 m_selection
->SelectBlock( 0,
4743 m_currentCellCoords
.GetCol(),
4744 GetNumberRows() - 1, col
,
4745 event
.ControlDown(),
4752 m_selection
->SelectCol( col
,
4753 event
.ControlDown(),
4760 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4765 // starting to drag-resize a col
4767 if ( CanDragColSize() )
4768 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4773 // ------------ Left double click
4775 if ( event
.LeftDClick() )
4777 if ( XToEdgeOfCol(x
) < 0 )
4780 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4785 // ------------ Left button released
4787 else if ( event
.LeftUp() )
4789 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4791 DoEndDragResizeCol();
4793 // Note: we are ending the event *after* doing
4794 // default processing in this case
4796 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4799 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4804 // ------------ Right button down
4806 else if ( event
.RightDown() )
4809 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4811 // no default action at the moment
4816 // ------------ Right double click
4818 else if ( event
.RightDClick() )
4821 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4823 // no default action at the moment
4828 // ------------ No buttons down and mouse moving
4830 else if ( event
.Moving() )
4832 m_dragRowOrCol
= XToEdgeOfCol( x
);
4833 if ( m_dragRowOrCol
>= 0 )
4835 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4837 // don't capture the cursor yet
4838 if ( CanDragColSize() )
4839 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4842 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4844 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4850 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4852 if ( event
.LeftDown() )
4854 // indicate corner label by having both row and
4857 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4863 else if ( event
.LeftDClick() )
4865 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4868 else if ( event
.RightDown() )
4870 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4872 // no default action at the moment
4876 else if ( event
.RightDClick() )
4878 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4880 // no default action at the moment
4885 void wxGrid::ChangeCursorMode(CursorMode mode
,
4890 static const wxChar
*cursorModes
[] =
4899 wxLogTrace(_T("grid"),
4900 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4901 win
== m_colLabelWin
? _T("colLabelWin")
4902 : win
? _T("rowLabelWin")
4904 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4905 #endif // __WXDEBUG__
4907 if ( mode
== m_cursorMode
&&
4908 win
== m_winCapture
&&
4909 captureMouse
== (m_winCapture
!= NULL
))
4914 // by default use the grid itself
4920 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4921 m_winCapture
= (wxWindow
*)NULL
;
4924 m_cursorMode
= mode
;
4926 switch ( m_cursorMode
)
4928 case WXGRID_CURSOR_RESIZE_ROW
:
4929 win
->SetCursor( m_rowResizeCursor
);
4932 case WXGRID_CURSOR_RESIZE_COL
:
4933 win
->SetCursor( m_colResizeCursor
);
4937 win
->SetCursor( *wxSTANDARD_CURSOR
);
4940 // we need to capture mouse when resizing
4941 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4942 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4944 if ( captureMouse
&& resize
)
4946 win
->CaptureMouse();
4951 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4954 wxPoint
pos( event
.GetPosition() );
4955 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4957 wxGridCellCoords coords
;
4958 XYToCell( x
, y
, coords
);
4960 if ( event
.Dragging() )
4962 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4964 // Don't start doing anything until the mouse has been drug at
4965 // least 3 pixels in any direction...
4968 if (m_startDragPos
== wxDefaultPosition
)
4970 m_startDragPos
= pos
;
4973 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4977 m_isDragging
= TRUE
;
4978 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4980 // Hide the edit control, so it
4981 // won't interfer with drag-shrinking.
4982 if ( IsCellEditControlShown() )
4984 HideCellEditControl();
4985 SaveEditControlValue();
4988 // Have we captured the mouse yet?
4991 m_winCapture
= m_gridWin
;
4992 m_winCapture
->CaptureMouse();
4995 if ( coords
!= wxGridNoCellCoords
)
4997 if ( event
.ControlDown() )
4999 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5000 m_selectingKeyboard
= coords
;
5001 HighlightBlock ( m_selectingKeyboard
, coords
);
5005 if ( !IsSelection() )
5007 HighlightBlock( coords
, coords
);
5011 HighlightBlock( m_currentCellCoords
, coords
);
5015 if (! IsVisible(coords
))
5017 MakeCellVisible(coords
);
5018 // TODO: need to introduce a delay or something here. The
5019 // scrolling is way to fast, at least on MSW - also on GTK.
5023 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5025 int cw
, ch
, left
, dummy
;
5026 m_gridWin
->GetClientSize( &cw
, &ch
);
5027 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5029 wxClientDC
dc( m_gridWin
);
5031 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5032 GetRowMinimalHeight(m_dragRowOrCol
) );
5033 dc
.SetLogicalFunction(wxINVERT
);
5034 if ( m_dragLastPos
>= 0 )
5036 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5038 dc
.DrawLine( left
, y
, left
+cw
, y
);
5041 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5043 int cw
, ch
, dummy
, top
;
5044 m_gridWin
->GetClientSize( &cw
, &ch
);
5045 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5047 wxClientDC
dc( m_gridWin
);
5049 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5050 GetColMinimalWidth(m_dragRowOrCol
) );
5051 dc
.SetLogicalFunction(wxINVERT
);
5052 if ( m_dragLastPos
>= 0 )
5054 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5056 dc
.DrawLine( x
, top
, x
, top
+ch
);
5063 m_isDragging
= FALSE
;
5064 m_startDragPos
= wxDefaultPosition
;
5066 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5067 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5070 if ( event
.Entering() || event
.Leaving() )
5072 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5073 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5078 // ------------ Left button pressed
5080 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5082 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5087 if ( !event
.ControlDown() )
5089 if ( event
.ShiftDown() )
5093 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5094 m_currentCellCoords
.GetCol(),
5097 event
.ControlDown(),
5103 else if ( XToEdgeOfCol(x
) < 0 &&
5104 YToEdgeOfRow(y
) < 0 )
5106 DisableCellEditControl();
5107 MakeCellVisible( coords
);
5109 // if this is the second click on this cell then start
5111 if ( m_waitForSlowClick
&&
5112 (coords
== m_currentCellCoords
) &&
5113 CanEnableCellControl())
5115 EnableCellEditControl();
5117 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5118 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5121 editor
->StartingClick();
5125 m_waitForSlowClick
= FALSE
;
5129 if ( event
.ControlDown() )
5133 m_selection
->ToggleCellSelection( coords
.GetRow(),
5135 event
.ControlDown(),
5140 m_selectingTopLeft
= wxGridNoCellCoords
;
5141 m_selectingBottomRight
= wxGridNoCellCoords
;
5142 m_selectingKeyboard
= coords
;
5146 SetCurrentCell( coords
);
5149 if ( m_selection
->GetSelectionMode() !=
5150 wxGrid::wxGridSelectCells
)
5152 HighlightBlock( coords
, coords
);
5156 m_waitForSlowClick
= TRUE
;
5163 // ------------ Left double click
5165 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5167 DisableCellEditControl();
5169 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5171 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5179 // ------------ Left button released
5181 else if ( event
.LeftUp() )
5183 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5185 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5186 m_selectingBottomRight
!= wxGridNoCellCoords
)
5190 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5191 m_winCapture
= NULL
;
5196 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5197 m_selectingTopLeft
.GetCol(),
5198 m_selectingBottomRight
.GetRow(),
5199 m_selectingBottomRight
.GetCol(),
5200 event
.ControlDown(),
5206 m_selectingTopLeft
= wxGridNoCellCoords
;
5207 m_selectingBottomRight
= wxGridNoCellCoords
;
5210 // Show the edit control, if it has been hidden for
5212 ShowCellEditControl();
5214 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5216 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5217 DoEndDragResizeRow();
5219 // Note: we are ending the event *after* doing
5220 // default processing in this case
5222 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5224 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5226 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5227 DoEndDragResizeCol();
5229 // Note: we are ending the event *after* doing
5230 // default processing in this case
5232 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5239 // ------------ Right button down
5241 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5243 DisableCellEditControl();
5244 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5249 // no default action at the moment
5254 // ------------ Right double click
5256 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5258 DisableCellEditControl();
5259 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5264 // no default action at the moment
5268 // ------------ Moving and no button action
5270 else if ( event
.Moving() && !event
.IsButton() )
5272 int dragRow
= YToEdgeOfRow( y
);
5273 int dragCol
= XToEdgeOfCol( x
);
5275 // Dragging on the corner of a cell to resize in both
5276 // directions is not implemented yet...
5278 if ( dragRow
>= 0 && dragCol
>= 0 )
5280 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5286 m_dragRowOrCol
= dragRow
;
5288 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5290 if ( CanDragRowSize() && CanDragGridSize() )
5291 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5296 m_dragRowOrCol
= dragCol
;
5304 m_dragRowOrCol
= dragCol
;
5306 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5308 if ( CanDragColSize() && CanDragGridSize() )
5309 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5315 // Neither on a row or col edge
5317 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5319 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5325 void wxGrid::DoEndDragResizeRow()
5327 if ( m_dragLastPos
>= 0 )
5329 // erase the last line and resize the row
5331 int cw
, ch
, left
, dummy
;
5332 m_gridWin
->GetClientSize( &cw
, &ch
);
5333 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5335 wxClientDC
dc( m_gridWin
);
5337 dc
.SetLogicalFunction( wxINVERT
);
5338 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5339 HideCellEditControl();
5340 SaveEditControlValue();
5342 int rowTop
= GetRowTop(m_dragRowOrCol
);
5343 SetRowSize( m_dragRowOrCol
,
5344 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5346 if ( !GetBatchCount() )
5348 // Only needed to get the correct rect.y:
5349 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5351 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5352 rect
.width
= m_rowLabelWidth
;
5353 rect
.height
= ch
- rect
.y
;
5354 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5356 m_gridWin
->Refresh( FALSE
, &rect
);
5359 ShowCellEditControl();
5364 void wxGrid::DoEndDragResizeCol()
5366 if ( m_dragLastPos
>= 0 )
5368 // erase the last line and resize the col
5370 int cw
, ch
, dummy
, top
;
5371 m_gridWin
->GetClientSize( &cw
, &ch
);
5372 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5374 wxClientDC
dc( m_gridWin
);
5376 dc
.SetLogicalFunction( wxINVERT
);
5377 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5378 HideCellEditControl();
5379 SaveEditControlValue();
5381 int colLeft
= GetColLeft(m_dragRowOrCol
);
5382 SetColSize( m_dragRowOrCol
,
5383 wxMax( m_dragLastPos
- colLeft
,
5384 GetColMinimalWidth(m_dragRowOrCol
) ) );
5386 if ( !GetBatchCount() )
5388 // Only needed to get the correct rect.x:
5389 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5391 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5392 rect
.width
= cw
- rect
.x
;
5393 rect
.height
= m_colLabelHeight
;
5394 m_colLabelWin
->Refresh( TRUE
, &rect
);
5396 m_gridWin
->Refresh( FALSE
, &rect
);
5399 ShowCellEditControl();
5406 // ------ interaction with data model
5408 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5410 switch ( msg
.GetId() )
5412 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5413 return GetModelValues();
5415 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5416 return SetModelValues();
5418 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5419 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5420 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5421 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5422 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5423 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5424 return Redimension( msg
);
5433 // The behaviour of this function depends on the grid table class
5434 // Clear() function. For the default wxGridStringTable class the
5435 // behavious is to replace all cell contents with wxEmptyString but
5436 // not to change the number of rows or cols.
5438 void wxGrid::ClearGrid()
5442 if (IsCellEditControlEnabled())
5443 DisableCellEditControl();
5446 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5451 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5453 // TODO: something with updateLabels flag
5457 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5463 if (IsCellEditControlEnabled())
5464 DisableCellEditControl();
5466 return m_table
->InsertRows( pos
, numRows
);
5468 // the table will have sent the results of the insert row
5469 // operation to this view object as a grid table message
5475 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5477 // TODO: something with updateLabels flag
5481 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5485 return ( m_table
&& m_table
->AppendRows( numRows
) );
5486 // the table will have sent the results of the append row
5487 // operation to this view object as a grid table message
5491 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5493 // TODO: something with updateLabels flag
5497 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5503 if (IsCellEditControlEnabled())
5504 DisableCellEditControl();
5506 return (m_table
->DeleteRows( pos
, numRows
));
5507 // the table will have sent the results of the delete row
5508 // operation to this view object as a grid table message
5514 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5516 // TODO: something with updateLabels flag
5520 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5526 if (IsCellEditControlEnabled())
5527 DisableCellEditControl();
5529 return m_table
->InsertCols( pos
, numCols
);
5530 // the table will have sent the results of the insert col
5531 // operation to this view object as a grid table message
5537 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5539 // TODO: something with updateLabels flag
5543 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5547 return ( m_table
&& m_table
->AppendCols( numCols
) );
5548 // the table will have sent the results of the append col
5549 // operation to this view object as a grid table message
5553 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5555 // TODO: something with updateLabels flag
5559 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5565 if (IsCellEditControlEnabled())
5566 DisableCellEditControl();
5568 return ( m_table
->DeleteCols( pos
, numCols
) );
5569 // the table will have sent the results of the delete col
5570 // operation to this view object as a grid table message
5578 // ----- event handlers
5581 // Generate a grid event based on a mouse event and
5582 // return the result of ProcessEvent()
5584 int wxGrid::SendEvent( const wxEventType type
,
5586 wxMouseEvent
& mouseEv
)
5591 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5593 int rowOrCol
= (row
== -1 ? col
: row
);
5595 wxGridSizeEvent
gridEvt( GetId(),
5599 mouseEv
.GetX() + GetRowLabelSize(),
5600 mouseEv
.GetY() + GetColLabelSize(),
5601 mouseEv
.ControlDown(),
5602 mouseEv
.ShiftDown(),
5604 mouseEv
.MetaDown() );
5606 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5607 vetoed
= !gridEvt
.IsAllowed();
5609 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5611 // Right now, it should _never_ end up here!
5612 wxGridRangeSelectEvent
gridEvt( GetId(),
5616 m_selectingBottomRight
,
5618 mouseEv
.ControlDown(),
5619 mouseEv
.ShiftDown(),
5621 mouseEv
.MetaDown() );
5623 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5624 vetoed
= !gridEvt
.IsAllowed();
5628 wxGridEvent
gridEvt( GetId(),
5632 mouseEv
.GetX() + GetRowLabelSize(),
5633 mouseEv
.GetY() + GetColLabelSize(),
5635 mouseEv
.ControlDown(),
5636 mouseEv
.ShiftDown(),
5638 mouseEv
.MetaDown() );
5639 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5640 vetoed
= !gridEvt
.IsAllowed();
5643 // A Veto'd event may not be `claimed' so test this first
5644 if (vetoed
) return -1;
5645 return claimed
? 1 : 0;
5649 // Generate a grid event of specified type and return the result
5650 // of ProcessEvent().
5652 int wxGrid::SendEvent( const wxEventType type
,
5658 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5660 int rowOrCol
= (row
== -1 ? col
: row
);
5662 wxGridSizeEvent
gridEvt( GetId(),
5667 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5668 vetoed
= !gridEvt
.IsAllowed();
5672 wxGridEvent
gridEvt( GetId(),
5677 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5678 vetoed
= !gridEvt
.IsAllowed();
5681 // A Veto'd event may not be `claimed' so test this first
5682 if (vetoed
) return -1;
5683 return claimed
? 1 : 0;
5687 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5689 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5693 void wxGrid::OnSize( wxSizeEvent
& event
)
5695 // position the child windows
5698 // don't call CalcDimensions() from here, the base class handles the size
5704 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5706 if ( m_inOnKeyDown
)
5708 // shouldn't be here - we are going round in circles...
5710 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5713 m_inOnKeyDown
= TRUE
;
5715 // propagate the event up and see if it gets processed
5717 wxWindow
*parent
= GetParent();
5718 wxKeyEvent
keyEvt( event
);
5719 keyEvt
.SetEventObject( parent
);
5721 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5724 // try local handlers
5726 switch ( event
.KeyCode() )
5729 if ( event
.ControlDown() )
5731 MoveCursorUpBlock( event
.ShiftDown() );
5735 MoveCursorUp( event
.ShiftDown() );
5740 if ( event
.ControlDown() )
5742 MoveCursorDownBlock( event
.ShiftDown() );
5746 MoveCursorDown( event
.ShiftDown() );
5751 if ( event
.ControlDown() )
5753 MoveCursorLeftBlock( event
.ShiftDown() );
5757 MoveCursorLeft( event
.ShiftDown() );
5762 if ( event
.ControlDown() )
5764 MoveCursorRightBlock( event
.ShiftDown() );
5768 MoveCursorRight( event
.ShiftDown() );
5773 case WXK_NUMPAD_ENTER
:
5774 if ( event
.ControlDown() )
5776 event
.Skip(); // to let the edit control have the return
5780 if ( GetGridCursorRow() < GetNumberRows()-1 )
5782 MoveCursorDown( event
.ShiftDown() );
5786 // at the bottom of a column
5787 HideCellEditControl();
5788 SaveEditControlValue();
5798 if (event
.ShiftDown())
5800 if ( GetGridCursorCol() > 0 )
5802 MoveCursorLeft( FALSE
);
5807 HideCellEditControl();
5808 SaveEditControlValue();
5813 if ( GetGridCursorCol() < GetNumberCols()-1 )
5815 MoveCursorRight( FALSE
);
5820 HideCellEditControl();
5821 SaveEditControlValue();
5827 if ( event
.ControlDown() )
5829 MakeCellVisible( 0, 0 );
5830 SetCurrentCell( 0, 0 );
5839 if ( event
.ControlDown() )
5841 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5842 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5859 if ( event
.ControlDown() )
5863 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5864 m_currentCellCoords
.GetCol(),
5865 event
.ControlDown(),
5872 if ( !IsEditable() )
5874 MoveCursorRight( FALSE
);
5877 // Otherwise fall through to default
5880 // is it possible to edit the current cell at all?
5881 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5883 // yes, now check whether the cells editor accepts the key
5884 int row
= m_currentCellCoords
.GetRow();
5885 int col
= m_currentCellCoords
.GetCol();
5886 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5887 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5889 // <F2> is special and will always start editing, for
5890 // other keys - ask the editor itself
5891 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5892 || editor
->IsAcceptedKey(event
) )
5894 EnableCellEditControl();
5896 // the editor could be not shown for a variety of
5897 // reasons (i.e. blocked by the app or whatever), so
5898 // check if it really was created
5899 if ( m_cellEditCtrlEnabled
)
5901 editor
->StartingKey(event
);
5914 // let others process char events with modifiers or all
5915 // char events for readonly cells
5922 m_inOnKeyDown
= FALSE
;
5925 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5927 // try local handlers
5929 if ( event
.KeyCode() == WXK_SHIFT
)
5931 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5932 m_selectingBottomRight
!= wxGridNoCellCoords
)
5936 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5937 m_selectingTopLeft
.GetCol(),
5938 m_selectingBottomRight
.GetRow(),
5939 m_selectingBottomRight
.GetCol(),
5940 event
.ControlDown(),
5947 m_selectingTopLeft
= wxGridNoCellCoords
;
5948 m_selectingBottomRight
= wxGridNoCellCoords
;
5949 m_selectingKeyboard
= wxGridNoCellCoords
;
5953 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5957 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5959 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5961 // the event has been intercepted - do nothing
5965 wxClientDC
dc(m_gridWin
);
5968 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5970 HideCellEditControl();
5971 DisableCellEditControl();
5973 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5976 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5977 if ( !m_gridLinesEnabled
)
5985 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5987 // Otherwise refresh redraws the highlight!
5988 m_currentCellCoords
= coords
;
5990 DrawGridCellArea(dc
,cells
);
5991 DrawAllGridLines( dc
, r
);
5995 m_currentCellCoords
= coords
;
5997 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5998 DrawCellHighlight(dc
, attr
);
6003 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6006 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6010 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6013 rightCol
= GetNumberCols() - 1;
6015 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6018 bottomRow
= GetNumberRows() - 1;
6022 if ( topRow
> bottomRow
)
6029 if ( leftCol
> rightCol
)
6036 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6037 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6039 if ( m_selectingTopLeft
!= updateTopLeft
||
6040 m_selectingBottomRight
!= updateBottomRight
)
6042 // Compute two optimal update rectangles:
6043 // Either one rectangle is a real subset of the
6044 // other, or they are (almost) disjoint!
6046 bool need_refresh
[4];
6050 need_refresh
[3] = FALSE
;
6053 // Store intermediate values
6054 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6055 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6056 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6057 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6059 // Determine the outer/inner coordinates.
6060 if (oldLeft
> leftCol
)
6066 if (oldTop
> topRow
)
6072 if (oldRight
< rightCol
)
6075 oldRight
= rightCol
;
6078 if (oldBottom
< bottomRow
)
6081 oldBottom
= bottomRow
;
6085 // Now, either the stuff marked old is the outer
6086 // rectangle or we don't have a situation where one
6087 // is contained in the other.
6089 if ( oldLeft
< leftCol
)
6091 need_refresh
[0] = TRUE
;
6092 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6094 wxGridCellCoords ( oldBottom
,
6098 if ( oldTop
< topRow
)
6100 need_refresh
[1] = TRUE
;
6101 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6103 wxGridCellCoords ( topRow
- 1,
6107 if ( oldRight
> rightCol
)
6109 need_refresh
[2] = TRUE
;
6110 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6112 wxGridCellCoords ( oldBottom
,
6116 if ( oldBottom
> bottomRow
)
6118 need_refresh
[3] = TRUE
;
6119 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6121 wxGridCellCoords ( oldBottom
,
6127 m_selectingTopLeft
= updateTopLeft
;
6128 m_selectingBottomRight
= updateBottomRight
;
6130 // various Refresh() calls
6131 for (i
= 0; i
< 4; i
++ )
6132 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6133 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6136 // never generate an event as it will be generated from
6137 // wxGridSelection::SelectBlock!
6138 // (old comment from when this was the body of SelectBlock)
6142 // ------ functions to get/send data (see also public functions)
6145 bool wxGrid::GetModelValues()
6149 // all we need to do is repaint the grid
6151 m_gridWin
->Refresh();
6159 bool wxGrid::SetModelValues()
6165 for ( row
= 0; row
< m_numRows
; row
++ )
6167 for ( col
= 0; col
< m_numCols
; col
++ )
6169 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6181 // Note - this function only draws cells that are in the list of
6182 // exposed cells (usually set from the update region by
6183 // CalcExposedCells)
6185 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6187 if ( !m_numRows
|| !m_numCols
) return;
6190 size_t numCells
= cells
.GetCount();
6192 for ( i
= 0; i
< numCells
; i
++ )
6194 DrawCell( dc
, cells
[i
] );
6199 void wxGrid::DrawGridSpace( wxDC
& dc
)
6202 m_gridWin
->GetClientSize( &cw
, &ch
);
6205 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6207 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6208 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6210 if ( right
> rightCol
|| bottom
> bottomRow
)
6213 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6215 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6216 dc
.SetPen( *wxTRANSPARENT_PEN
);
6218 if ( right
> rightCol
)
6220 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6223 if ( bottom
> bottomRow
)
6225 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6231 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6233 int row
= coords
.GetRow();
6234 int col
= coords
.GetCol();
6236 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6239 // we draw the cell border ourselves
6240 #if !WXGRID_DRAW_LINES
6241 if ( m_gridLinesEnabled
)
6242 DrawCellBorder( dc
, coords
);
6245 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6247 bool isCurrent
= coords
== m_currentCellCoords
;
6249 wxRect rect
= CellToRect( row
, col
);
6251 // if the editor is shown, we should use it and not the renderer
6252 // Note: However, only if it is really _shown_, i.e. not hidden!
6253 if ( isCurrent
&& IsCellEditControlShown() )
6255 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6256 editor
->PaintBackground(rect
, attr
);
6261 // but all the rest is drawn by the cell renderer and hence may be
6263 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6264 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6271 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6273 int row
= m_currentCellCoords
.GetRow();
6274 int col
= m_currentCellCoords
.GetCol();
6276 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6279 wxRect rect
= CellToRect(row
, col
);
6281 // hmmm... what could we do here to show that the cell is disabled?
6282 // for now, I just draw a thinner border than for the other ones, but
6283 // it doesn't look really good
6285 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6289 // The center of th drawn line is where the position/width/height of
6290 // the rectangle is actually at, (on wxMSW atr least,) so we will
6291 // reduce the size of the rectangle to compensate for the thickness of
6292 // the line. If this is too strange on non wxMSW platforms then
6293 // please #ifdef this appropriately.
6294 rect
.x
+= penWidth
/2;
6295 rect
.y
+= penWidth
/2;
6296 rect
.width
-= penWidth
-1;
6297 rect
.height
-= penWidth
-1;
6300 // Now draw the rectangle
6301 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6302 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6303 dc
.DrawRectangle(rect
);
6307 // VZ: my experiments with 3d borders...
6309 // how to properly set colours for arbitrary bg?
6310 wxCoord x1
= rect
.x
,
6312 x2
= rect
.x
+ rect
.width
-1,
6313 y2
= rect
.y
+ rect
.height
-1;
6315 dc
.SetPen(*wxWHITE_PEN
);
6316 dc
.DrawLine(x1
, y1
, x2
, y1
);
6317 dc
.DrawLine(x1
, y1
, x1
, y2
);
6319 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6320 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6322 dc
.SetPen(*wxBLACK_PEN
);
6323 dc
.DrawLine(x1
, y2
, x2
, y2
);
6324 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6329 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6331 int row
= coords
.GetRow();
6332 int col
= coords
.GetCol();
6333 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6336 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6338 // right hand border
6340 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6341 GetColRight(col
), GetRowBottom(row
) );
6345 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6346 GetColRight(col
), GetRowBottom(row
) );
6349 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6351 // This if block was previously in wxGrid::OnPaint but that doesn't
6352 // seem to get called under wxGTK - MB
6354 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6355 m_numRows
&& m_numCols
)
6357 m_currentCellCoords
.Set(0, 0);
6360 if ( IsCellEditControlShown() )
6362 // don't show highlight when the edit control is shown
6366 // if the active cell was repainted, repaint its highlight too because it
6367 // might have been damaged by the grid lines
6368 size_t count
= cells
.GetCount();
6369 for ( size_t n
= 0; n
< count
; n
++ )
6371 if ( cells
[n
] == m_currentCellCoords
)
6373 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6374 DrawCellHighlight(dc
, attr
);
6382 // TODO: remove this ???
6383 // This is used to redraw all grid lines e.g. when the grid line colour
6386 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6388 if ( !m_gridLinesEnabled
||
6390 !m_numCols
) return;
6392 int top
, bottom
, left
, right
;
6394 #if 0 //#ifndef __WXGTK__
6398 m_gridWin
->GetClientSize(&cw
, &ch
);
6400 // virtual coords of visible area
6402 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6403 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6408 reg
.GetBox(x
, y
, w
, h
);
6409 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6410 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6414 m_gridWin
->GetClientSize(&cw
, &ch
);
6415 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6416 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6419 // avoid drawing grid lines past the last row and col
6421 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6422 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6424 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6426 // horizontal grid lines
6429 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6431 int bot
= GetRowBottom(i
) - 1;
6440 dc
.DrawLine( left
, bot
, right
, bot
);
6445 // vertical grid lines
6447 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6449 int colRight
= GetColRight(i
) - 1;
6450 if ( colRight
> right
)
6455 if ( colRight
>= left
)
6457 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6463 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6465 if ( !m_numRows
) return;
6468 size_t numLabels
= rows
.GetCount();
6470 for ( i
= 0; i
< numLabels
; i
++ )
6472 DrawRowLabel( dc
, rows
[i
] );
6477 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6479 if ( GetRowHeight(row
) <= 0 )
6482 int rowTop
= GetRowTop(row
),
6483 rowBottom
= GetRowBottom(row
) - 1;
6485 dc
.SetPen( *wxBLACK_PEN
);
6486 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6487 m_rowLabelWidth
-1, rowBottom
);
6489 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6491 dc
.SetPen( *wxWHITE_PEN
);
6492 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6493 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6495 dc
.SetBackgroundMode( wxTRANSPARENT
);
6496 dc
.SetTextForeground( GetLabelTextColour() );
6497 dc
.SetFont( GetLabelFont() );
6500 GetRowLabelAlignment( &hAlign
, &vAlign
);
6504 rect
.SetY( GetRowTop(row
) + 2 );
6505 rect
.SetWidth( m_rowLabelWidth
- 4 );
6506 rect
.SetHeight( GetRowHeight(row
) - 4 );
6507 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6511 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6513 if ( !m_numCols
) return;
6516 size_t numLabels
= cols
.GetCount();
6518 for ( i
= 0; i
< numLabels
; i
++ )
6520 DrawColLabel( dc
, cols
[i
] );
6525 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6527 if ( GetColWidth(col
) <= 0 )
6530 int colLeft
= GetColLeft(col
),
6531 colRight
= GetColRight(col
) - 1;
6533 dc
.SetPen( *wxBLACK_PEN
);
6534 dc
.DrawLine( colRight
, 0,
6535 colRight
, m_colLabelHeight
-1 );
6537 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6538 colRight
, m_colLabelHeight
-1 );
6540 dc
.SetPen( *wxWHITE_PEN
);
6541 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6542 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6544 dc
.SetBackgroundMode( wxTRANSPARENT
);
6545 dc
.SetTextForeground( GetLabelTextColour() );
6546 dc
.SetFont( GetLabelFont() );
6548 dc
.SetBackgroundMode( wxTRANSPARENT
);
6549 dc
.SetTextForeground( GetLabelTextColour() );
6550 dc
.SetFont( GetLabelFont() );
6553 GetColLabelAlignment( &hAlign
, &vAlign
);
6556 rect
.SetX( colLeft
+ 2 );
6558 rect
.SetWidth( GetColWidth(col
) - 4 );
6559 rect
.SetHeight( m_colLabelHeight
- 4 );
6560 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6563 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6564 const wxString
& value
,
6569 wxArrayString lines
;
6571 dc
.SetClippingRegion( rect
);
6572 StringToLines( value
, lines
);
6575 //Forward to new API.
6576 DrawTextRectangle( dc
,
6584 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6585 const wxArrayString
& lines
,
6590 long textWidth
, textHeight
;
6591 long lineWidth
, lineHeight
;
6593 if ( lines
.GetCount() )
6595 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6596 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6599 switch ( horizAlign
)
6602 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6605 case wxALIGN_CENTRE
:
6606 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6615 switch ( vertAlign
)
6617 case wxALIGN_BOTTOM
:
6618 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6621 case wxALIGN_CENTRE
:
6622 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6631 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6633 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6638 dc
.DestroyClippingRegion();
6642 // Split multi line text up into an array of strings. Any existing
6643 // contents of the string array are preserved.
6645 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6649 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6650 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6652 while ( startPos
< (int)tVal
.Length() )
6654 pos
= tVal
.Mid(startPos
).Find( eol
);
6659 else if ( pos
== 0 )
6661 lines
.Add( wxEmptyString
);
6665 lines
.Add( value
.Mid(startPos
, pos
) );
6669 if ( startPos
< (int)value
.Length() )
6671 lines
.Add( value
.Mid( startPos
) );
6676 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6677 const wxArrayString
& lines
,
6678 long *width
, long *height
)
6685 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6687 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6688 w
= wxMax( w
, lineW
);
6697 // ------ Batch processing.
6699 void wxGrid::EndBatch()
6701 if ( m_batchCount
> 0 )
6704 if ( !m_batchCount
)
6707 m_rowLabelWin
->Refresh();
6708 m_colLabelWin
->Refresh();
6709 m_cornerLabelWin
->Refresh();
6710 m_gridWin
->Refresh();
6715 // Use this, rather than wxWindow::Refresh(), to force an immediate
6716 // repainting of the grid. Has no effect if you are already inside a
6717 // BeginBatch / EndBatch block.
6719 void wxGrid::ForceRefresh()
6727 // ------ Edit control functions
6731 void wxGrid::EnableEditing( bool edit
)
6733 // TODO: improve this ?
6735 if ( edit
!= m_editable
)
6737 if(!edit
) EnableCellEditControl(edit
);
6743 void wxGrid::EnableCellEditControl( bool enable
)
6748 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6749 SetCurrentCell( 0, 0 );
6751 if ( enable
!= m_cellEditCtrlEnabled
)
6755 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6758 // this should be checked by the caller!
6759 wxASSERT_MSG( CanEnableCellControl(),
6760 _T("can't enable editing for this cell!") );
6762 // do it before ShowCellEditControl()
6763 m_cellEditCtrlEnabled
= enable
;
6765 ShowCellEditControl();
6769 //FIXME:add veto support
6770 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6772 HideCellEditControl();
6773 SaveEditControlValue();
6775 // do it after HideCellEditControl()
6776 m_cellEditCtrlEnabled
= enable
;
6781 bool wxGrid::IsCurrentCellReadOnly() const
6784 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6785 bool readonly
= attr
->IsReadOnly();
6791 bool wxGrid::CanEnableCellControl() const
6793 return m_editable
&& !IsCurrentCellReadOnly();
6796 bool wxGrid::IsCellEditControlEnabled() const
6798 // the cell edit control might be disable for all cells or just for the
6799 // current one if it's read only
6800 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6803 bool wxGrid::IsCellEditControlShown() const
6805 bool isShown
= FALSE
;
6807 if ( m_cellEditCtrlEnabled
)
6809 int row
= m_currentCellCoords
.GetRow();
6810 int col
= m_currentCellCoords
.GetCol();
6811 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6812 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6817 if ( editor
->IsCreated() )
6819 isShown
= editor
->GetControl()->IsShown();
6829 void wxGrid::ShowCellEditControl()
6831 if ( IsCellEditControlEnabled() )
6833 if ( !IsVisible( m_currentCellCoords
) )
6835 m_cellEditCtrlEnabled
= FALSE
;
6840 wxRect rect
= CellToRect( m_currentCellCoords
);
6841 int row
= m_currentCellCoords
.GetRow();
6842 int col
= m_currentCellCoords
.GetCol();
6844 // convert to scrolled coords
6846 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6848 // done in PaintBackground()
6850 // erase the highlight and the cell contents because the editor
6851 // might not cover the entire cell
6852 wxClientDC
dc( m_gridWin
);
6854 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6855 dc
.SetPen(*wxTRANSPARENT_PEN
);
6856 dc
.DrawRectangle(rect
);
6859 // cell is shifted by one pixel
6860 // However, don't allow x or y to become negative
6861 // since the SetSize() method interprets that as
6868 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6869 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6870 if ( !editor
->IsCreated() )
6872 editor
->Create(m_gridWin
, -1,
6873 new wxGridCellEditorEvtHandler(this, editor
));
6875 wxGridEditorCreatedEvent
evt(GetId(),
6876 wxEVT_GRID_EDITOR_CREATED
,
6880 editor
->GetControl());
6881 GetEventHandler()->ProcessEvent(evt
);
6884 editor
->Show( TRUE
, attr
);
6886 editor
->SetSize( rect
);
6888 editor
->BeginEdit(row
, col
, this);
6897 void wxGrid::HideCellEditControl()
6899 if ( IsCellEditControlEnabled() )
6901 int row
= m_currentCellCoords
.GetRow();
6902 int col
= m_currentCellCoords
.GetCol();
6904 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6905 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6906 editor
->Show( FALSE
);
6909 m_gridWin
->SetFocus();
6910 wxRect
rect( CellToRect( row
, col
) );
6911 m_gridWin
->Refresh( FALSE
, &rect
);
6916 void wxGrid::SaveEditControlValue()
6918 if ( IsCellEditControlEnabled() )
6920 int row
= m_currentCellCoords
.GetRow();
6921 int col
= m_currentCellCoords
.GetCol();
6923 wxString oldval
= GetCellValue(row
,col
);
6925 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6926 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6927 bool changed
= editor
->EndEdit(row
, col
, this);
6934 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6935 m_currentCellCoords
.GetRow(),
6936 m_currentCellCoords
.GetCol() ) < 0 ) {
6938 // Event has been vetoed, set the data back.
6939 SetCellValue(row
,col
,oldval
);
6947 // ------ Grid location functions
6948 // Note that all of these functions work with the logical coordinates of
6949 // grid cells and labels so you will need to convert from device
6950 // coordinates for mouse events etc.
6953 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6955 int row
= YToRow(y
);
6956 int col
= XToCol(x
);
6958 if ( row
== -1 || col
== -1 )
6960 coords
= wxGridNoCellCoords
;
6964 coords
.Set( row
, col
);
6969 // Internal Helper function for computing row or column from some
6970 // (unscrolled) coordinate value, using either
6971 // m_defaultRowHeight/m_defaultColWidth or binary search on array
6972 // of m_rowBottoms/m_ColRights to speed up the search!
6974 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
6975 const wxArrayInt
& BorderArray
, bool maxOnOverflow
)
6979 size_t i_max
= coord
/ defaultDist
,
6981 if (BorderArray
.IsEmpty())
6986 if ( i_max
>= BorderArray
.GetCount())
6987 i_max
= BorderArray
.GetCount() - 1;
6990 if ( coord
>= BorderArray
[i_max
])
6993 i_max
= coord
/ minDist
;
6995 if ( i_max
>= BorderArray
.GetCount())
6996 i_max
= BorderArray
.GetCount() - 1;
6998 if ( coord
>= BorderArray
[i_max
])
6999 return maxOnOverflow
? (int)i_max
: -1;
7000 if ( coord
< BorderArray
[0] )
7003 while ( i_max
- i_min
> 0 )
7005 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7006 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7007 if (coord
>= BorderArray
[ i_max
- 1])
7011 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7012 if (coord
< BorderArray
[median
])
7020 int wxGrid::YToRow( int y
)
7022 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7023 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, FALSE
);
7027 int wxGrid::XToCol( int x
)
7029 return CoordToRowOrCol(x
, m_defaultColWidth
,
7030 WXGRID_MIN_COL_WIDTH
, m_colRights
, FALSE
);
7034 // return the row number that that the y coord is near the edge of, or
7035 // -1 if not near an edge
7037 int wxGrid::YToEdgeOfRow( int y
)
7040 i
= internalYToRow(y
);
7042 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7044 // We know that we are in row i, test whether we are
7045 // close enough to lower or upper border, respectively.
7046 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7048 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7056 // return the col number that that the x coord is near the edge of, or
7057 // -1 if not near an edge
7059 int wxGrid::XToEdgeOfCol( int x
)
7062 i
= internalXToCol(x
);
7064 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7066 // We know that we are in column i, test whether we are
7067 // close enough to right or left border, respectively.
7068 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7070 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7078 wxRect
wxGrid::CellToRect( int row
, int col
)
7080 wxRect
rect( -1, -1, -1, -1 );
7082 if ( row
>= 0 && row
< m_numRows
&&
7083 col
>= 0 && col
< m_numCols
)
7085 rect
.x
= GetColLeft(col
);
7086 rect
.y
= GetRowTop(row
);
7087 rect
.width
= GetColWidth(col
);
7088 rect
.height
= GetRowHeight(row
);
7091 // if grid lines are enabled, then the area of the cell is a bit smaller
7092 if (m_gridLinesEnabled
) {
7100 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7102 // get the cell rectangle in logical coords
7104 wxRect
r( CellToRect( row
, col
) );
7106 // convert to device coords
7108 int left
, top
, right
, bottom
;
7109 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7110 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7112 // check against the client area of the grid window
7115 m_gridWin
->GetClientSize( &cw
, &ch
);
7117 if ( wholeCellVisible
)
7119 // is the cell wholly visible ?
7121 return ( left
>= 0 && right
<= cw
&&
7122 top
>= 0 && bottom
<= ch
);
7126 // is the cell partly visible ?
7128 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7129 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7134 // make the specified cell location visible by doing a minimal amount
7137 void wxGrid::MakeCellVisible( int row
, int col
)
7141 int xpos
= -1, ypos
= -1;
7143 if ( row
>= 0 && row
< m_numRows
&&
7144 col
>= 0 && col
< m_numCols
)
7146 // get the cell rectangle in logical coords
7148 wxRect
r( CellToRect( row
, col
) );
7150 // convert to device coords
7152 int left
, top
, right
, bottom
;
7153 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7154 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7157 m_gridWin
->GetClientSize( &cw
, &ch
);
7163 else if ( bottom
> ch
)
7165 int h
= r
.GetHeight();
7167 for ( i
= row
-1; i
>= 0; i
-- )
7169 int rowHeight
= GetRowHeight(i
);
7170 if ( h
+ rowHeight
> ch
)
7177 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7178 // have rounding errors (this is important, because if we do, we
7179 // might not scroll at all and some cells won't be redrawn)
7181 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7183 ypos
+= GRID_SCROLL_LINE_Y
;
7190 else if ( right
> cw
)
7192 int w
= r
.GetWidth();
7194 for ( i
= col
-1; i
>= 0; i
-- )
7196 int colWidth
= GetColWidth(i
);
7197 if ( w
+ colWidth
> cw
)
7204 // see comment for ypos above
7205 xpos
+= GRID_SCROLL_LINE_X
;
7208 if ( xpos
!= -1 || ypos
!= -1 )
7211 xpos
/= GRID_SCROLL_LINE_X
;
7213 ypos
/= GRID_SCROLL_LINE_Y
;
7214 Scroll( xpos
, ypos
);
7222 // ------ Grid cursor movement functions
7225 bool wxGrid::MoveCursorUp( bool expandSelection
)
7227 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7228 m_currentCellCoords
.GetRow() >= 0 )
7230 if ( expandSelection
)
7232 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7233 m_selectingKeyboard
= m_currentCellCoords
;
7234 if ( m_selectingKeyboard
.GetRow() > 0 )
7236 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7237 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7238 m_selectingKeyboard
.GetCol() );
7239 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7242 else if ( m_currentCellCoords
.GetRow() > 0 )
7245 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7246 m_currentCellCoords
.GetCol() );
7247 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7248 m_currentCellCoords
.GetCol() );
7259 bool wxGrid::MoveCursorDown( bool expandSelection
)
7261 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7262 m_currentCellCoords
.GetRow() < m_numRows
)
7264 if ( expandSelection
)
7266 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7267 m_selectingKeyboard
= m_currentCellCoords
;
7268 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7270 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7271 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7272 m_selectingKeyboard
.GetCol() );
7273 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7276 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7279 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7280 m_currentCellCoords
.GetCol() );
7281 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7282 m_currentCellCoords
.GetCol() );
7293 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7295 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7296 m_currentCellCoords
.GetCol() >= 0 )
7298 if ( expandSelection
)
7300 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7301 m_selectingKeyboard
= m_currentCellCoords
;
7302 if ( m_selectingKeyboard
.GetCol() > 0 )
7304 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7305 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7306 m_selectingKeyboard
.GetCol() );
7307 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7310 else if ( m_currentCellCoords
.GetCol() > 0 )
7313 MakeCellVisible( m_currentCellCoords
.GetRow(),
7314 m_currentCellCoords
.GetCol() - 1 );
7315 SetCurrentCell( m_currentCellCoords
.GetRow(),
7316 m_currentCellCoords
.GetCol() - 1 );
7327 bool wxGrid::MoveCursorRight( bool expandSelection
)
7329 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7330 m_currentCellCoords
.GetCol() < m_numCols
)
7332 if ( expandSelection
)
7334 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7335 m_selectingKeyboard
= m_currentCellCoords
;
7336 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7338 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7339 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7340 m_selectingKeyboard
.GetCol() );
7341 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7344 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7347 MakeCellVisible( m_currentCellCoords
.GetRow(),
7348 m_currentCellCoords
.GetCol() + 1 );
7349 SetCurrentCell( m_currentCellCoords
.GetRow(),
7350 m_currentCellCoords
.GetCol() + 1 );
7361 bool wxGrid::MovePageUp()
7363 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7365 int row
= m_currentCellCoords
.GetRow();
7369 m_gridWin
->GetClientSize( &cw
, &ch
);
7371 int y
= GetRowTop(row
);
7372 int newRow
= YToRow( y
- ch
+ 1 );
7377 else if ( newRow
== row
)
7382 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7383 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7391 bool wxGrid::MovePageDown()
7393 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7395 int row
= m_currentCellCoords
.GetRow();
7396 if ( row
< m_numRows
)
7399 m_gridWin
->GetClientSize( &cw
, &ch
);
7401 int y
= GetRowTop(row
);
7402 int newRow
= YToRow( y
+ ch
);
7405 newRow
= m_numRows
- 1;
7407 else if ( newRow
== row
)
7412 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7413 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7421 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7424 m_currentCellCoords
!= wxGridNoCellCoords
&&
7425 m_currentCellCoords
.GetRow() > 0 )
7427 int row
= m_currentCellCoords
.GetRow();
7428 int col
= m_currentCellCoords
.GetCol();
7430 if ( m_table
->IsEmptyCell(row
, col
) )
7432 // starting in an empty cell: find the next block of
7438 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7441 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7443 // starting at the top of a block: find the next block
7449 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7454 // starting within a block: find the top of the block
7459 if ( m_table
->IsEmptyCell(row
, col
) )
7467 MakeCellVisible( row
, col
);
7468 if ( expandSelection
)
7470 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7471 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7476 SetCurrentCell( row
, col
);
7484 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7487 m_currentCellCoords
!= wxGridNoCellCoords
&&
7488 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7490 int row
= m_currentCellCoords
.GetRow();
7491 int col
= m_currentCellCoords
.GetCol();
7493 if ( m_table
->IsEmptyCell(row
, col
) )
7495 // starting in an empty cell: find the next block of
7498 while ( row
< m_numRows
-1 )
7501 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7504 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7506 // starting at the bottom of a block: find the next block
7509 while ( row
< m_numRows
-1 )
7512 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7517 // starting within a block: find the bottom of the block
7519 while ( row
< m_numRows
-1 )
7522 if ( m_table
->IsEmptyCell(row
, col
) )
7530 MakeCellVisible( row
, col
);
7531 if ( expandSelection
)
7533 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7534 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7539 SetCurrentCell( row
, col
);
7548 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7551 m_currentCellCoords
!= wxGridNoCellCoords
&&
7552 m_currentCellCoords
.GetCol() > 0 )
7554 int row
= m_currentCellCoords
.GetRow();
7555 int col
= m_currentCellCoords
.GetCol();
7557 if ( m_table
->IsEmptyCell(row
, col
) )
7559 // starting in an empty cell: find the next block of
7565 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7568 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7570 // starting at the left of a block: find the next block
7576 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7581 // starting within a block: find the left of the block
7586 if ( m_table
->IsEmptyCell(row
, col
) )
7594 MakeCellVisible( row
, col
);
7595 if ( expandSelection
)
7597 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7598 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7603 SetCurrentCell( row
, col
);
7612 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7615 m_currentCellCoords
!= wxGridNoCellCoords
&&
7616 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7618 int row
= m_currentCellCoords
.GetRow();
7619 int col
= m_currentCellCoords
.GetCol();
7621 if ( m_table
->IsEmptyCell(row
, col
) )
7623 // starting in an empty cell: find the next block of
7626 while ( col
< m_numCols
-1 )
7629 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7632 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7634 // starting at the right of a block: find the next block
7637 while ( col
< m_numCols
-1 )
7640 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7645 // starting within a block: find the right of the block
7647 while ( col
< m_numCols
-1 )
7650 if ( m_table
->IsEmptyCell(row
, col
) )
7658 MakeCellVisible( row
, col
);
7659 if ( expandSelection
)
7661 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7662 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7667 SetCurrentCell( row
, col
);
7679 // ------ Label values and formatting
7682 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7684 *horiz
= m_rowLabelHorizAlign
;
7685 *vert
= m_rowLabelVertAlign
;
7688 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7690 *horiz
= m_colLabelHorizAlign
;
7691 *vert
= m_colLabelVertAlign
;
7694 wxString
wxGrid::GetRowLabelValue( int row
)
7698 return m_table
->GetRowLabelValue( row
);
7708 wxString
wxGrid::GetColLabelValue( int col
)
7712 return m_table
->GetColLabelValue( col
);
7723 void wxGrid::SetRowLabelSize( int width
)
7725 width
= wxMax( width
, 0 );
7726 if ( width
!= m_rowLabelWidth
)
7730 m_rowLabelWin
->Show( FALSE
);
7731 m_cornerLabelWin
->Show( FALSE
);
7733 else if ( m_rowLabelWidth
== 0 )
7735 m_rowLabelWin
->Show( TRUE
);
7736 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7739 m_rowLabelWidth
= width
;
7746 void wxGrid::SetColLabelSize( int height
)
7748 height
= wxMax( height
, 0 );
7749 if ( height
!= m_colLabelHeight
)
7753 m_colLabelWin
->Show( FALSE
);
7754 m_cornerLabelWin
->Show( FALSE
);
7756 else if ( m_colLabelHeight
== 0 )
7758 m_colLabelWin
->Show( TRUE
);
7759 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7762 m_colLabelHeight
= height
;
7769 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7771 if ( m_labelBackgroundColour
!= colour
)
7773 m_labelBackgroundColour
= colour
;
7774 m_rowLabelWin
->SetBackgroundColour( colour
);
7775 m_colLabelWin
->SetBackgroundColour( colour
);
7776 m_cornerLabelWin
->SetBackgroundColour( colour
);
7778 if ( !GetBatchCount() )
7780 m_rowLabelWin
->Refresh();
7781 m_colLabelWin
->Refresh();
7782 m_cornerLabelWin
->Refresh();
7787 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7789 if ( m_labelTextColour
!= colour
)
7791 m_labelTextColour
= colour
;
7792 if ( !GetBatchCount() )
7794 m_rowLabelWin
->Refresh();
7795 m_colLabelWin
->Refresh();
7800 void wxGrid::SetLabelFont( const wxFont
& font
)
7803 if ( !GetBatchCount() )
7805 m_rowLabelWin
->Refresh();
7806 m_colLabelWin
->Refresh();
7810 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7812 // allow old (incorrect) defs to be used
7815 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7816 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7817 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7822 case wxTOP
: vert
= wxALIGN_TOP
; break;
7823 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7824 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7827 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7829 m_rowLabelHorizAlign
= horiz
;
7832 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7834 m_rowLabelVertAlign
= vert
;
7837 if ( !GetBatchCount() )
7839 m_rowLabelWin
->Refresh();
7843 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7845 // allow old (incorrect) defs to be used
7848 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7849 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7850 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7855 case wxTOP
: vert
= wxALIGN_TOP
; break;
7856 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7857 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7860 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7862 m_colLabelHorizAlign
= horiz
;
7865 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7867 m_colLabelVertAlign
= vert
;
7870 if ( !GetBatchCount() )
7872 m_colLabelWin
->Refresh();
7876 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7880 m_table
->SetRowLabelValue( row
, s
);
7881 if ( !GetBatchCount() )
7883 wxRect rect
= CellToRect( row
, 0);
7884 if ( rect
.height
> 0 )
7886 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7888 rect
.width
= m_rowLabelWidth
;
7889 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7895 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7899 m_table
->SetColLabelValue( col
, s
);
7900 if ( !GetBatchCount() )
7902 wxRect rect
= CellToRect( 0, col
);
7903 if ( rect
.width
> 0 )
7905 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7907 rect
.height
= m_colLabelHeight
;
7908 m_colLabelWin
->Refresh( TRUE
, &rect
);
7914 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7916 if ( m_gridLineColour
!= colour
)
7918 m_gridLineColour
= colour
;
7920 wxClientDC
dc( m_gridWin
);
7922 DrawAllGridLines( dc
, wxRegion() );
7927 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7929 if ( m_cellHighlightColour
!= colour
)
7931 m_cellHighlightColour
= colour
;
7933 wxClientDC
dc( m_gridWin
);
7935 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7936 DrawCellHighlight(dc
, attr
);
7941 void wxGrid::SetCellHighlightPenWidth(int width
)
7943 if (m_cellHighlightPenWidth
!= width
) {
7944 m_cellHighlightPenWidth
= width
;
7946 // Just redrawing the cell highlight is not enough since that won't
7947 // make any visible change if the the thickness is getting smaller.
7948 int row
= m_currentCellCoords
.GetRow();
7949 int col
= m_currentCellCoords
.GetCol();
7950 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7952 wxRect rect
= CellToRect(row
, col
);
7953 m_gridWin
->Refresh(TRUE
, &rect
);
7957 void wxGrid::SetCellHighlightROPenWidth(int width
)
7959 if (m_cellHighlightROPenWidth
!= width
) {
7960 m_cellHighlightROPenWidth
= width
;
7962 // Just redrawing the cell highlight is not enough since that won't
7963 // make any visible change if the the thickness is getting smaller.
7964 int row
= m_currentCellCoords
.GetRow();
7965 int col
= m_currentCellCoords
.GetCol();
7966 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7968 wxRect rect
= CellToRect(row
, col
);
7969 m_gridWin
->Refresh(TRUE
, &rect
);
7973 void wxGrid::EnableGridLines( bool enable
)
7975 if ( enable
!= m_gridLinesEnabled
)
7977 m_gridLinesEnabled
= enable
;
7979 if ( !GetBatchCount() )
7983 wxClientDC
dc( m_gridWin
);
7985 DrawAllGridLines( dc
, wxRegion() );
7989 m_gridWin
->Refresh();
7996 int wxGrid::GetDefaultRowSize()
7998 return m_defaultRowHeight
;
8001 int wxGrid::GetRowSize( int row
)
8003 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8005 return GetRowHeight(row
);
8008 int wxGrid::GetDefaultColSize()
8010 return m_defaultColWidth
;
8013 int wxGrid::GetColSize( int col
)
8015 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8017 return GetColWidth(col
);
8020 // ============================================================================
8021 // access to the grid attributes: each of them has a default value in the grid
8022 // itself and may be overidden on a per-cell basis
8023 // ============================================================================
8025 // ----------------------------------------------------------------------------
8026 // setting default attributes
8027 // ----------------------------------------------------------------------------
8029 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8031 m_defaultCellAttr
->SetBackgroundColour(col
);
8033 m_gridWin
->SetBackgroundColour(col
);
8037 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8039 m_defaultCellAttr
->SetTextColour(col
);
8042 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8044 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8047 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8049 m_defaultCellAttr
->SetFont(font
);
8052 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8054 m_defaultCellAttr
->SetRenderer(renderer
);
8057 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8059 m_defaultCellAttr
->SetEditor(editor
);
8062 // ----------------------------------------------------------------------------
8063 // access to the default attrbiutes
8064 // ----------------------------------------------------------------------------
8066 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8068 return m_defaultCellAttr
->GetBackgroundColour();
8071 wxColour
wxGrid::GetDefaultCellTextColour()
8073 return m_defaultCellAttr
->GetTextColour();
8076 wxFont
wxGrid::GetDefaultCellFont()
8078 return m_defaultCellAttr
->GetFont();
8081 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8083 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8086 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8088 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8091 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8093 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8096 // ----------------------------------------------------------------------------
8097 // access to cell attributes
8098 // ----------------------------------------------------------------------------
8100 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8102 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8103 wxColour colour
= attr
->GetBackgroundColour();
8108 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8110 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8111 wxColour colour
= attr
->GetTextColour();
8116 wxFont
wxGrid::GetCellFont( int row
, int col
)
8118 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8119 wxFont font
= attr
->GetFont();
8124 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8126 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8127 attr
->GetAlignment(horiz
, vert
);
8131 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8133 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8134 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8140 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8142 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8143 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8149 bool wxGrid::IsReadOnly(int row
, int col
) const
8151 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8152 bool isReadOnly
= attr
->IsReadOnly();
8157 // ----------------------------------------------------------------------------
8158 // attribute support: cache, automatic provider creation, ...
8159 // ----------------------------------------------------------------------------
8161 bool wxGrid::CanHaveAttributes()
8168 return m_table
->CanHaveAttributes();
8171 void wxGrid::ClearAttrCache()
8173 if ( m_attrCache
.row
!= -1 )
8175 wxSafeDecRef(m_attrCache
.attr
);
8176 m_attrCache
.attr
= NULL
;
8177 m_attrCache
.row
= -1;
8181 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8183 wxGrid
*self
= (wxGrid
*)this; // const_cast
8185 self
->ClearAttrCache();
8186 self
->m_attrCache
.row
= row
;
8187 self
->m_attrCache
.col
= col
;
8188 self
->m_attrCache
.attr
= attr
;
8192 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8194 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8196 *attr
= m_attrCache
.attr
;
8197 wxSafeIncRef(m_attrCache
.attr
);
8199 #ifdef DEBUG_ATTR_CACHE
8200 gs_nAttrCacheHits
++;
8207 #ifdef DEBUG_ATTR_CACHE
8208 gs_nAttrCacheMisses
++;
8214 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8216 wxGridCellAttr
*attr
;
8217 if ( !LookupAttr(row
, col
, &attr
) )
8219 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8220 CacheAttr(row
, col
, attr
);
8224 attr
->SetDefAttr(m_defaultCellAttr
);
8228 attr
= m_defaultCellAttr
;
8235 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8237 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8239 wxCHECK_MSG( m_table
, attr
,
8240 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8242 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8245 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8247 // artificially inc the ref count to match DecRef() in caller
8249 m_table
->SetAttr(attr
, row
, col
);
8255 // ----------------------------------------------------------------------------
8256 // setting column attributes (wrappers around SetColAttr)
8257 // ----------------------------------------------------------------------------
8259 void wxGrid::SetColFormatBool(int col
)
8261 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8264 void wxGrid::SetColFormatNumber(int col
)
8266 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8269 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8271 wxString typeName
= wxGRID_VALUE_FLOAT
;
8272 if ( (width
!= -1) || (precision
!= -1) )
8274 typeName
<< _T(':') << width
<< _T(',') << precision
;
8277 SetColFormatCustom(col
, typeName
);
8280 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8282 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8284 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8286 attr
= new wxGridCellAttr
;
8287 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8288 attr
->SetRenderer(renderer
);
8290 SetColAttr(col
, attr
);
8294 // ----------------------------------------------------------------------------
8295 // setting cell attributes: this is forwarded to the table
8296 // ----------------------------------------------------------------------------
8298 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8300 if ( CanHaveAttributes() )
8302 m_table
->SetRowAttr(attr
, row
);
8311 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8313 if ( CanHaveAttributes() )
8315 m_table
->SetColAttr(attr
, col
);
8324 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8326 if ( CanHaveAttributes() )
8328 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8329 attr
->SetBackgroundColour(colour
);
8334 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8336 if ( CanHaveAttributes() )
8338 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8339 attr
->SetTextColour(colour
);
8344 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8346 if ( CanHaveAttributes() )
8348 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8349 attr
->SetFont(font
);
8354 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8356 if ( CanHaveAttributes() )
8358 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8359 attr
->SetAlignment(horiz
, vert
);
8364 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8366 if ( CanHaveAttributes() )
8368 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8369 attr
->SetRenderer(renderer
);
8374 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8376 if ( CanHaveAttributes() )
8378 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8379 attr
->SetEditor(editor
);
8384 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8386 if ( CanHaveAttributes() )
8388 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8389 attr
->SetReadOnly(isReadOnly
);
8394 // ----------------------------------------------------------------------------
8395 // Data type registration
8396 // ----------------------------------------------------------------------------
8398 void wxGrid::RegisterDataType(const wxString
& typeName
,
8399 wxGridCellRenderer
* renderer
,
8400 wxGridCellEditor
* editor
)
8402 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8406 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8408 wxString typeName
= m_table
->GetTypeName(row
, col
);
8409 return GetDefaultEditorForType(typeName
);
8412 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8414 wxString typeName
= m_table
->GetTypeName(row
, col
);
8415 return GetDefaultRendererForType(typeName
);
8419 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8421 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8422 if ( index
== wxNOT_FOUND
)
8424 wxFAIL_MSG(wxT("Unknown data type name"));
8429 return m_typeRegistry
->GetEditor(index
);
8433 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8435 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8436 if ( index
== wxNOT_FOUND
)
8438 wxFAIL_MSG(wxT("Unknown data type name"));
8443 return m_typeRegistry
->GetRenderer(index
);
8447 // ----------------------------------------------------------------------------
8449 // ----------------------------------------------------------------------------
8451 void wxGrid::EnableDragRowSize( bool enable
)
8453 m_canDragRowSize
= enable
;
8457 void wxGrid::EnableDragColSize( bool enable
)
8459 m_canDragColSize
= enable
;
8462 void wxGrid::EnableDragGridSize( bool enable
)
8464 m_canDragGridSize
= enable
;
8468 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8470 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8472 if ( resizeExistingRows
)
8474 // since we are resizing all rows to the default row size,
8475 // we can simply clear the row heights and row bottoms
8476 // arrays (which also allows us to take advantage of
8477 // some speed optimisations)
8478 m_rowHeights
.Empty();
8479 m_rowBottoms
.Empty();
8480 if ( !GetBatchCount() )
8485 void wxGrid::SetRowSize( int row
, int height
)
8487 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8489 if ( m_rowHeights
.IsEmpty() )
8491 // need to really create the array
8495 int h
= wxMax( 0, height
);
8496 int diff
= h
- m_rowHeights
[row
];
8498 m_rowHeights
[row
] = h
;
8500 for ( i
= row
; i
< m_numRows
; i
++ )
8502 m_rowBottoms
[i
] += diff
;
8504 if ( !GetBatchCount() )
8508 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8510 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8512 if ( resizeExistingCols
)
8514 // since we are resizing all columns to the default column size,
8515 // we can simply clear the col widths and col rights
8516 // arrays (which also allows us to take advantage of
8517 // some speed optimisations)
8518 m_colWidths
.Empty();
8519 m_colRights
.Empty();
8520 if ( !GetBatchCount() )
8525 void wxGrid::SetColSize( int col
, int width
)
8527 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8529 // should we check that it's bigger than GetColMinimalWidth(col) here?
8531 if ( m_colWidths
.IsEmpty() )
8533 // need to really create the array
8537 int w
= wxMax( 0, width
);
8538 int diff
= w
- m_colWidths
[col
];
8539 m_colWidths
[col
] = w
;
8542 for ( i
= col
; i
< m_numCols
; i
++ )
8544 m_colRights
[i
] += diff
;
8546 if ( !GetBatchCount() )
8551 void wxGrid::SetColMinimalWidth( int col
, int width
)
8553 m_colMinWidths
.Put(col
, width
);
8556 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8558 m_rowMinHeights
.Put(row
, width
);
8561 int wxGrid::GetColMinimalWidth(int col
) const
8563 long value
= m_colMinWidths
.Get(col
);
8564 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8567 int wxGrid::GetRowMinimalHeight(int row
) const
8569 long value
= m_rowMinHeights
.Get(row
);
8570 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8573 // ----------------------------------------------------------------------------
8575 // ----------------------------------------------------------------------------
8577 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8579 wxClientDC
dc(m_gridWin
);
8581 // init both of them to avoid compiler warnings, even if weo nly need one
8589 wxCoord extent
, extentMax
= 0;
8590 int max
= column
? m_numRows
: m_numCols
;
8591 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8598 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8599 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8602 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8603 extent
= column
? size
.x
: size
.y
;
8604 if ( extent
> extentMax
)
8615 // now also compare with the column label extent
8617 dc
.SetFont( GetLabelFont() );
8620 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8622 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8624 extent
= column
? w
: h
;
8625 if ( extent
> extentMax
)
8632 // empty column - give default extent (notice that if extentMax is less
8633 // than default extent but != 0, it's ok)
8634 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8640 // leave some space around text
8651 SetColSize(col
, extentMax
);
8652 if ( !GetBatchCount() )
8655 m_gridWin
->GetClientSize( &cw
, &ch
);
8656 wxRect
rect ( CellToRect( 0, col
) );
8658 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8659 rect
.width
= cw
- rect
.x
;
8660 rect
.height
= m_colLabelHeight
;
8661 m_colLabelWin
->Refresh( TRUE
, &rect
);
8666 SetRowSize(row
, extentMax
);
8667 if ( !GetBatchCount() )
8670 m_gridWin
->GetClientSize( &cw
, &ch
);
8671 wxRect
rect ( CellToRect( row
, 0 ) );
8673 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8674 rect
.width
= m_rowLabelWidth
;
8675 rect
.height
= ch
- rect
.y
;
8676 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8682 SetColMinimalWidth(col
, extentMax
);
8684 SetRowMinimalHeight(row
, extentMax
);
8688 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8690 int width
= m_rowLabelWidth
;
8695 for ( int col
= 0; col
< m_numCols
; col
++ )
8699 AutoSizeColumn(col
, setAsMin
);
8702 width
+= GetColWidth(col
);
8711 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8713 int height
= m_colLabelHeight
;
8718 for ( int row
= 0; row
< m_numRows
; row
++ )
8722 AutoSizeRow(row
, setAsMin
);
8725 height
+= GetRowHeight(row
);
8734 void wxGrid::AutoSize()
8738 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8740 // round up the size to a multiple of scroll step - this ensures that we
8741 // won't get the scrollbars if we're sized exactly to this width
8742 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
8743 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
8745 // distribute the extra space between teh columns/rows to avoid having
8746 // extra white space
8747 wxCoord diff
= sizeFit
.x
- size
.x
;
8750 // try to resize the columns uniformly
8751 wxCoord diffPerCol
= diff
/ m_numCols
;
8754 for ( int col
= 0; col
< m_numCols
; col
++ )
8756 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
8760 // add remaining amount to the last columns
8761 diff
-= diffPerCol
* m_numCols
;
8764 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
8766 SetColSize(col
, GetColWidth(col
) + 1);
8772 diff
= sizeFit
.y
- size
.y
;
8775 // try to resize the columns uniformly
8776 wxCoord diffPerRow
= diff
/ m_numRows
;
8779 for ( int row
= 0; row
< m_numRows
; row
++ )
8781 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
8785 // add remaining amount to the last rows
8786 diff
-= diffPerRow
* m_numRows
;
8789 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
8791 SetRowSize(row
, GetRowHeight(row
) + 1);
8798 SetClientSize(sizeFit
);
8801 wxSize
wxGrid::DoGetBestSize() const
8803 // don't set sizes, only calculate them
8804 wxGrid
*self
= (wxGrid
*)this; // const_cast
8806 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8807 self
->SetOrCalcRowSizes(TRUE
));
8816 wxPen
& wxGrid::GetDividerPen() const
8821 // ----------------------------------------------------------------------------
8822 // cell value accessor functions
8823 // ----------------------------------------------------------------------------
8825 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8829 m_table
->SetValue( row
, col
, s
);
8830 if ( !GetBatchCount() )
8832 wxClientDC
dc( m_gridWin
);
8834 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8837 if ( m_currentCellCoords
.GetRow() == row
&&
8838 m_currentCellCoords
.GetCol() == col
&&
8839 IsCellEditControlShown())
8840 // Note: If we are using IsCellEditControlEnabled,
8841 // this interacts badly with calling SetCellValue from
8842 // an EVT_GRID_CELL_CHANGE handler.
8844 HideCellEditControl();
8845 ShowCellEditControl(); // will reread data from table
8852 // ------ Block, row and col selection
8855 void wxGrid::SelectRow( int row
, bool addToSelected
)
8857 if ( IsSelection() && !addToSelected
)
8861 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8865 void wxGrid::SelectCol( int col
, bool addToSelected
)
8867 if ( IsSelection() && !addToSelected
)
8871 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8875 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8876 bool addToSelected
)
8878 if ( IsSelection() && !addToSelected
)
8882 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8883 FALSE
, addToSelected
);
8887 void wxGrid::SelectAll()
8889 if ( m_numRows
> 0 && m_numCols
> 0 )
8892 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8897 // ------ Cell, row and col deselection
8900 void wxGrid::DeselectRow( int row
)
8905 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8907 if ( m_selection
->IsInSelection(row
, 0 ) )
8908 m_selection
->ToggleCellSelection( row
, 0);
8912 int nCols
= GetNumberCols();
8913 for ( int i
= 0; i
< nCols
; i
++ )
8915 if ( m_selection
->IsInSelection(row
, i
) )
8916 m_selection
->ToggleCellSelection( row
, i
);
8921 void wxGrid::DeselectCol( int col
)
8926 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8928 if ( m_selection
->IsInSelection(0, col
) )
8929 m_selection
->ToggleCellSelection( 0, col
);
8933 int nRows
= GetNumberRows();
8934 for ( int i
= 0; i
< nRows
; i
++ )
8936 if ( m_selection
->IsInSelection(i
, col
) )
8937 m_selection
->ToggleCellSelection(i
, col
);
8942 void wxGrid::DeselectCell( int row
, int col
)
8944 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8945 m_selection
->ToggleCellSelection(row
, col
);
8948 bool wxGrid::IsSelection()
8950 return ( m_selection
&& (m_selection
->IsSelection() ||
8951 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8952 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
8955 bool wxGrid::IsInSelection( int row
, int col
)
8957 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8958 ( row
>= m_selectingTopLeft
.GetRow() &&
8959 col
>= m_selectingTopLeft
.GetCol() &&
8960 row
<= m_selectingBottomRight
.GetRow() &&
8961 col
<= m_selectingBottomRight
.GetCol() )) );
8964 void wxGrid::ClearSelection()
8966 m_selectingTopLeft
= wxGridNoCellCoords
;
8967 m_selectingBottomRight
= wxGridNoCellCoords
;
8969 m_selection
->ClearSelection();
8973 // This function returns the rectangle that encloses the given block
8974 // in device coords clipped to the client size of the grid window.
8976 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8977 const wxGridCellCoords
&bottomRight
)
8979 wxRect
rect( wxGridNoCellRect
);
8982 cellRect
= CellToRect( topLeft
);
8983 if ( cellRect
!= wxGridNoCellRect
)
8989 rect
= wxRect( 0, 0, 0, 0 );
8992 cellRect
= CellToRect( bottomRight
);
8993 if ( cellRect
!= wxGridNoCellRect
)
8999 return wxGridNoCellRect
;
9002 // convert to scrolled coords
9004 int left
, top
, right
, bottom
;
9005 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
9006 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
9009 m_gridWin
->GetClientSize( &cw
, &ch
);
9011 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9012 return wxRect( 0, 0, 0, 0);
9014 rect
.SetLeft( wxMax(0, left
) );
9015 rect
.SetTop( wxMax(0, top
) );
9016 rect
.SetRight( wxMin(cw
, right
) );
9017 rect
.SetBottom( wxMin(ch
, bottom
) );
9025 // ------ Grid event classes
9028 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9030 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9031 int row
, int col
, int x
, int y
, bool sel
,
9032 bool control
, bool shift
, bool alt
, bool meta
)
9033 : wxNotifyEvent( type
, id
)
9040 m_control
= control
;
9045 SetEventObject(obj
);
9049 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9051 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9052 int rowOrCol
, int x
, int y
,
9053 bool control
, bool shift
, bool alt
, bool meta
)
9054 : wxNotifyEvent( type
, id
)
9056 m_rowOrCol
= rowOrCol
;
9059 m_control
= control
;
9064 SetEventObject(obj
);
9068 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9070 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9071 const wxGridCellCoords
& topLeft
,
9072 const wxGridCellCoords
& bottomRight
,
9073 bool sel
, bool control
,
9074 bool shift
, bool alt
, bool meta
)
9075 : wxNotifyEvent( type
, id
)
9077 m_topLeft
= topLeft
;
9078 m_bottomRight
= bottomRight
;
9080 m_control
= control
;
9085 SetEventObject(obj
);
9089 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9091 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9092 wxObject
* obj
, int row
,
9093 int col
, wxControl
* ctrl
)
9094 : wxCommandEvent(type
, id
)
9096 SetEventObject(obj
);
9103 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9105 #endif // wxUSE_GRID