1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
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"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
50 #include "wx/tokenzr.h"
53 #include "wx/generic/gridsel.h"
55 #if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
58 #define WXUNUSED_MOTIF(identifier) identifier
61 #if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64 #define WXUNUSED_GTK(identifier) identifier
67 // Required for wxIs... functions
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
76 struct wxGridCellWithAttr
78 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
79 : coords(row
, col
), attr(attr_
)
88 wxGridCellCoords coords
;
92 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
94 #include "wx/arrimpl.cpp"
96 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
97 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
106 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
107 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
108 const wxPoint
&pos
, const wxSize
&size
);
113 void OnPaint( wxPaintEvent
& event
);
114 void OnMouseEvent( wxMouseEvent
& event
);
115 void OnKeyDown( wxKeyEvent
& event
);
116 void OnKeyUp( wxKeyEvent
& );
118 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
119 DECLARE_EVENT_TABLE()
123 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
126 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnPaint( wxPaintEvent
&event
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnKeyUp( wxKeyEvent
& );
138 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
139 DECLARE_EVENT_TABLE()
143 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
146 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
147 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
);
153 void OnMouseEvent( wxMouseEvent
& event
);
154 void OnKeyDown( wxKeyEvent
& event
);
155 void OnKeyUp( wxKeyEvent
& );
156 void OnPaint( wxPaintEvent
& event
);
158 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
159 DECLARE_EVENT_TABLE()
162 class WXDLLEXPORT wxGridWindow
: public wxPanel
167 m_owner
= (wxGrid
*)NULL
;
168 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
169 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
172 wxGridWindow( wxGrid
*parent
,
173 wxGridRowLabelWindow
*rowLblWin
,
174 wxGridColLabelWindow
*colLblWin
,
175 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
178 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
182 wxGridRowLabelWindow
*m_rowLabelWin
;
183 wxGridColLabelWindow
*m_colLabelWin
;
185 void OnPaint( wxPaintEvent
&event
);
186 void OnMouseEvent( wxMouseEvent
& event
);
187 void OnKeyDown( wxKeyEvent
& );
188 void OnKeyUp( wxKeyEvent
& );
189 void OnEraseBackground( wxEraseEvent
& );
192 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
193 DECLARE_EVENT_TABLE()
198 class wxGridCellEditorEvtHandler
: public wxEvtHandler
201 wxGridCellEditorEvtHandler()
202 : m_grid(0), m_editor(0)
204 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
205 : m_grid(grid
), m_editor(editor
)
208 void OnKeyDown(wxKeyEvent
& event
);
209 void OnChar(wxKeyEvent
& event
);
213 wxGridCellEditor
* m_editor
;
214 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
215 DECLARE_EVENT_TABLE()
219 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
220 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
221 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
222 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
227 // ----------------------------------------------------------------------------
228 // the internal data representation used by wxGridCellAttrProvider
229 // ----------------------------------------------------------------------------
231 // this class stores attributes set for cells
232 class WXDLLEXPORT wxGridCellAttrData
235 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
236 wxGridCellAttr
*GetAttr(int row
, int col
) const;
237 void UpdateAttrRows( size_t pos
, int numRows
);
238 void UpdateAttrCols( size_t pos
, int numCols
);
241 // searches for the attr for given cell, returns wxNOT_FOUND if not found
242 int FindIndex(int row
, int col
) const;
244 wxGridCellWithAttrArray m_attrs
;
247 // this class stores attributes set for rows or columns
248 class WXDLLEXPORT wxGridRowOrColAttrData
251 // empty ctor to suppress warnings
252 wxGridRowOrColAttrData() { }
253 ~wxGridRowOrColAttrData();
255 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
256 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
257 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
260 wxArrayInt m_rowsOrCols
;
261 wxArrayAttrs m_attrs
;
264 // NB: this is just a wrapper around 3 objects: one which stores cell
265 // attributes, and 2 others for row/col ones
266 class WXDLLEXPORT wxGridCellAttrProviderData
269 wxGridCellAttrData m_cellAttrs
;
270 wxGridRowOrColAttrData m_rowAttrs
,
275 // ----------------------------------------------------------------------------
276 // data structures used for the data type registry
277 // ----------------------------------------------------------------------------
279 struct wxGridDataTypeInfo
281 wxGridDataTypeInfo(const wxString
& typeName
,
282 wxGridCellRenderer
* renderer
,
283 wxGridCellEditor
* editor
)
284 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
287 ~wxGridDataTypeInfo()
289 wxSafeDecRef(m_renderer
);
290 wxSafeDecRef(m_editor
);
294 wxGridCellRenderer
* m_renderer
;
295 wxGridCellEditor
* m_editor
;
299 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
302 class WXDLLEXPORT wxGridTypeRegistry
305 wxGridTypeRegistry() {}
306 ~wxGridTypeRegistry();
308 void RegisterDataType(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
);
312 // find one of already registered data types
313 int FindRegisteredDataType(const wxString
& typeName
);
315 // try to FindRegisteredDataType(), if this fails and typeName is one of
316 // standard typenames, register it and return its index
317 int FindDataType(const wxString
& typeName
);
319 // try to FindDataType(), if it fails see if it is not one of already
320 // registered data types with some params in which case clone the
321 // registered data type and set params for it
322 int FindOrCloneDataType(const wxString
& typeName
);
324 wxGridCellRenderer
* GetRenderer(int index
);
325 wxGridCellEditor
* GetEditor(int index
);
328 wxGridDataTypeInfoArray m_typeinfo
;
331 // ----------------------------------------------------------------------------
332 // conditional compilation
333 // ----------------------------------------------------------------------------
335 #ifndef WXGRID_DRAW_LINES
336 #define WXGRID_DRAW_LINES 1
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 //#define DEBUG_ATTR_CACHE
344 #ifdef DEBUG_ATTR_CACHE
345 static size_t gs_nAttrCacheHits
= 0;
346 static size_t gs_nAttrCacheMisses
= 0;
347 #endif // DEBUG_ATTR_CACHE
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
354 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
357 // TODO: fixed so far - make configurable later (and also different for x/y)
358 static const size_t GRID_SCROLL_LINE
= 10;
360 // the size of hash tables used a bit everywhere (the max number of elements
361 // in these hash tables is the number of rows/columns)
362 static const int GRID_HASH_SIZE
= 100;
364 // ============================================================================
366 // ============================================================================
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 wxGridCellEditor::wxGridCellEditor()
378 wxGridCellEditor::~wxGridCellEditor()
383 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
384 wxWindowID
WXUNUSED(id
),
385 wxEvtHandler
* evtHandler
)
388 m_control
->PushEventHandler(evtHandler
);
391 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
392 wxGridCellAttr
*attr
)
394 // erase the background because we might not fill the cell
395 wxClientDC
dc(m_control
->GetParent());
396 dc
.SetPen(*wxTRANSPARENT_PEN
);
397 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
398 dc
.DrawRectangle(rectCell
);
400 // redraw the control we just painted over
401 m_control
->Refresh();
404 void wxGridCellEditor::Destroy()
408 m_control
->PopEventHandler(TRUE
/* delete it*/);
410 m_control
->Destroy();
415 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
417 wxASSERT_MSG(m_control
,
418 wxT("The wxGridCellEditor must be Created first!"));
419 m_control
->Show(show
);
423 // set the colours/fonts if we have any
426 m_colFgOld
= m_control
->GetForegroundColour();
427 m_control
->SetForegroundColour(attr
->GetTextColour());
429 m_colBgOld
= m_control
->GetBackgroundColour();
430 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
432 m_fontOld
= m_control
->GetFont();
433 m_control
->SetFont(attr
->GetFont());
435 // can't do anything more in the base class version, the other
436 // attributes may only be used by the derived classes
441 // restore the standard colours fonts
442 if ( m_colFgOld
.Ok() )
444 m_control
->SetForegroundColour(m_colFgOld
);
445 m_colFgOld
= wxNullColour
;
448 if ( m_colBgOld
.Ok() )
450 m_control
->SetBackgroundColour(m_colBgOld
);
451 m_colBgOld
= wxNullColour
;
454 if ( m_fontOld
.Ok() )
456 m_control
->SetFont(m_fontOld
);
457 m_fontOld
= wxNullFont
;
462 void wxGridCellEditor::SetSize(const wxRect
& rect
)
464 wxASSERT_MSG(m_control
,
465 wxT("The wxGridCellEditor must be Created first!"));
466 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
469 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
474 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
476 // accept the simple key presses, not anything with Ctrl/Alt/Meta
477 return !event
.HasModifiers();
480 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
485 void wxGridCellEditor::StartingClick()
489 // ----------------------------------------------------------------------------
490 // wxGridCellTextEditor
491 // ----------------------------------------------------------------------------
493 wxGridCellTextEditor::wxGridCellTextEditor()
498 void wxGridCellTextEditor::Create(wxWindow
* parent
,
500 wxEvtHandler
* evtHandler
)
502 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
503 wxDefaultPosition
, wxDefaultSize
504 #if defined(__WXMSW__)
505 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
509 // TODO: use m_maxChars
511 wxGridCellEditor::Create(parent
, id
, evtHandler
);
514 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
515 wxGridCellAttr
* WXUNUSED(attr
))
517 // as we fill the entire client area, don't do anything here to minimize
521 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
523 wxRect
rect(rectOrig
);
525 // Make the edit control large enough to allow for internal
528 // TODO: remove this if the text ctrl sizing is improved esp. for
531 #if defined(__WXGTK__)
540 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
541 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
542 #if defined(__WXMOTIF__)
546 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
547 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
548 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
549 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
552 wxGridCellEditor::SetSize(rect
);
555 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
557 wxASSERT_MSG(m_control
,
558 wxT("The wxGridCellEditor must be Created first!"));
560 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
562 DoBeginEdit(m_startValue
);
565 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
567 Text()->SetValue(startValue
);
568 Text()->SetInsertionPointEnd();
572 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
575 wxASSERT_MSG(m_control
,
576 wxT("The wxGridCellEditor must be Created first!"));
578 bool changed
= FALSE
;
579 wxString value
= Text()->GetValue();
580 if (value
!= m_startValue
)
584 grid
->GetTable()->SetValue(row
, col
, value
);
586 m_startValue
= wxEmptyString
;
587 Text()->SetValue(m_startValue
);
593 void wxGridCellTextEditor::Reset()
595 wxASSERT_MSG(m_control
,
596 wxT("The wxGridCellEditor must be Created first!"));
598 DoReset(m_startValue
);
601 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
603 Text()->SetValue(startValue
);
604 Text()->SetInsertionPointEnd();
607 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
609 if ( wxGridCellEditor::IsAcceptedKey(event
) )
611 int keycode
= event
.GetKeyCode();
625 case WXK_NUMPAD_MULTIPLY
:
629 case WXK_NUMPAD_SUBTRACT
:
631 case WXK_NUMPAD_DECIMAL
:
633 case WXK_NUMPAD_DIVIDE
:
637 // accept 8 bit chars too if isprint() agrees
638 if ( (keycode
< 255) && (isprint(keycode
)) )
646 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
648 // we don't check for !HasModifiers() because IsAcceptedKey() did it
650 // insert the key in the control
652 int keycode
= event
.GetKeyCode();
665 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
669 case WXK_NUMPAD_MULTIPLY
:
679 case WXK_NUMPAD_SUBTRACT
:
684 case WXK_NUMPAD_DECIMAL
:
689 case WXK_NUMPAD_DIVIDE
:
694 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
696 // FIXME this is not going to work for non letters...
697 if ( !event
.ShiftDown() )
699 keycode
= tolower(keycode
);
702 ch
= (wxChar
)keycode
;
712 Text()->AppendText(ch
);
720 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
721 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
723 #if defined(__WXMOTIF__) || defined(__WXGTK__)
724 // wxMotif needs a little extra help...
725 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
726 wxString
s( Text()->GetValue() );
727 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
729 Text()->SetInsertionPoint( pos
);
731 // the other ports can handle a Return key press
737 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
747 if ( !params
.ToLong(&tmp
) )
749 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
753 m_maxChars
= (size_t)tmp
;
758 // ----------------------------------------------------------------------------
759 // wxGridCellNumberEditor
760 // ----------------------------------------------------------------------------
762 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
768 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
770 wxEvtHandler
* evtHandler
)
774 // create a spin ctrl
775 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
776 wxDefaultPosition
, wxDefaultSize
,
780 wxGridCellEditor::Create(parent
, id
, evtHandler
);
784 // just a text control
785 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
788 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
789 #endif // wxUSE_VALIDATORS
793 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
795 // first get the value
796 wxGridTableBase
*table
= grid
->GetTable();
797 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
799 m_valueOld
= table
->GetValueAsLong(row
, col
);
803 wxString sValue
= table
->GetValue(row
, col
);
804 if (! sValue
.ToLong(&m_valueOld
))
806 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
813 Spin()->SetValue((int)m_valueOld
);
818 DoBeginEdit(GetString());
822 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
830 value
= Spin()->GetValue();
831 changed
= value
!= m_valueOld
;
835 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
840 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
841 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
843 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
849 void wxGridCellNumberEditor::Reset()
853 Spin()->SetValue((int)m_valueOld
);
857 DoReset(GetString());
861 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
863 if ( wxGridCellEditor::IsAcceptedKey(event
) )
865 int keycode
= event
.GetKeyCode();
881 case WXK_NUMPAD_SUBTRACT
:
887 if ( (keycode
< 128) && isdigit(keycode
) )
895 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
899 int keycode
= (int) event
.KeyCode();
900 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
902 wxGridCellTextEditor::StartingKey(event
);
912 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
923 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
927 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
931 // skip the error message below
936 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
940 // ----------------------------------------------------------------------------
941 // wxGridCellFloatEditor
942 // ----------------------------------------------------------------------------
944 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
947 m_precision
= precision
;
950 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
952 wxEvtHandler
* evtHandler
)
954 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
957 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
958 #endif // wxUSE_VALIDATORS
961 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
963 // first get the value
964 wxGridTableBase
*table
= grid
->GetTable();
965 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
967 m_valueOld
= table
->GetValueAsDouble(row
, col
);
971 wxString sValue
= table
->GetValue(row
, col
);
972 if (! sValue
.ToDouble(&m_valueOld
))
974 wxFAIL_MSG( _T("this cell doesn't have float value") );
979 DoBeginEdit(GetString());
982 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
986 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
988 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
989 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
991 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1001 void wxGridCellFloatEditor::Reset()
1003 DoReset(GetString());
1006 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1008 int keycode
= (int)event
.KeyCode();
1009 if ( isdigit(keycode
) ||
1010 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1012 wxGridCellTextEditor::StartingKey(event
);
1014 // skip Skip() below
1021 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1032 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1036 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1038 m_precision
= (int)tmp
;
1040 // skip the error message below
1045 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1049 wxString
wxGridCellFloatEditor::GetString() const
1052 if ( m_width
== -1 )
1054 // default width/precision
1057 else if ( m_precision
== -1 )
1059 // default precision
1060 fmt
.Printf(_T("%%%d.g"), m_width
);
1064 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1067 return wxString::Format(fmt
, m_valueOld
);
1070 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1072 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1074 int keycode
= event
.GetKeyCode();
1088 case WXK_NUMPAD_ADD
:
1090 case WXK_NUMPAD_SUBTRACT
:
1092 case WXK_NUMPAD_DECIMAL
:
1096 // additionally accept 'e' as in '1e+6'
1097 if ( (keycode
< 128) &&
1098 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1106 // ----------------------------------------------------------------------------
1107 // wxGridCellBoolEditor
1108 // ----------------------------------------------------------------------------
1110 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1112 wxEvtHandler
* evtHandler
)
1114 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1115 wxDefaultPosition
, wxDefaultSize
,
1118 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1121 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1123 bool resize
= FALSE
;
1124 wxSize size
= m_control
->GetSize();
1125 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1127 // check if the checkbox is not too big/small for this cell
1128 wxSize sizeBest
= m_control
->GetBestSize();
1129 if ( !(size
== sizeBest
) )
1131 // reset to default size if it had been made smaller
1137 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1139 // leave 1 pixel margin
1140 size
.x
= size
.y
= minSize
- 2;
1147 m_control
->SetSize(size
);
1150 // position it in the centre of the rectangle (TODO: support alignment?)
1152 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1153 // the checkbox without label still has some space to the right in wxGTK,
1154 // so shift it to the right
1156 #elif defined(__WXMSW__)
1157 // here too, but in other way
1162 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1165 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1167 m_control
->Show(show
);
1171 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1172 CBox()->SetBackgroundColour(colBg
);
1176 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1178 wxASSERT_MSG(m_control
,
1179 wxT("The wxGridCellEditor must be Created first!"));
1181 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1182 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1184 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
1185 CBox()->SetValue(m_startValue
);
1189 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1192 wxASSERT_MSG(m_control
,
1193 wxT("The wxGridCellEditor must be Created first!"));
1195 bool changed
= FALSE
;
1196 bool value
= CBox()->GetValue();
1197 if ( value
!= m_startValue
)
1202 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1203 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1205 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1211 void wxGridCellBoolEditor::Reset()
1213 wxASSERT_MSG(m_control
,
1214 wxT("The wxGridCellEditor must be Created first!"));
1216 CBox()->SetValue(m_startValue
);
1219 void wxGridCellBoolEditor::StartingClick()
1221 CBox()->SetValue(!CBox()->GetValue());
1224 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1226 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1228 int keycode
= event
.GetKeyCode();
1232 case WXK_NUMPAD_MULTIPLY
:
1234 case WXK_NUMPAD_ADD
:
1236 case WXK_NUMPAD_SUBTRACT
:
1247 // ----------------------------------------------------------------------------
1248 // wxGridCellChoiceEditor
1249 // ----------------------------------------------------------------------------
1251 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1252 const wxString choices
[],
1254 : m_allowOthers(allowOthers
)
1258 m_choices
.Alloc(count
);
1259 for ( size_t n
= 0; n
< count
; n
++ )
1261 m_choices
.Add(choices
[n
]);
1266 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1268 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1269 editor
->m_allowOthers
= m_allowOthers
;
1270 editor
->m_choices
= m_choices
;
1275 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1277 wxEvtHandler
* evtHandler
)
1279 size_t count
= m_choices
.GetCount();
1280 wxString
*choices
= new wxString
[count
];
1281 for ( size_t n
= 0; n
< count
; n
++ )
1283 choices
[n
] = m_choices
[n
];
1286 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1287 wxDefaultPosition
, wxDefaultSize
,
1289 m_allowOthers
? 0 : wxCB_READONLY
);
1293 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1296 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1297 wxGridCellAttr
* attr
)
1299 // as we fill the entire client area, don't do anything here to minimize
1302 // TODO: It doesn't actually fill the client area since the height of a
1303 // combo always defaults to the standard... Until someone has time to
1304 // figure out the right rectangle to paint, just do it the normal way...
1305 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1308 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1310 wxASSERT_MSG(m_control
,
1311 wxT("The wxGridCellEditor must be Created first!"));
1313 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1315 Combo()->SetValue(m_startValue
);
1316 size_t count
= m_choices
.GetCount();
1317 for (size_t i
=0; i
<count
; i
++)
1319 if (m_startValue
== m_choices
[i
])
1321 Combo()->SetSelection(i
);
1325 Combo()->SetInsertionPointEnd();
1326 Combo()->SetFocus();
1329 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1332 wxString value
= Combo()->GetValue();
1333 bool changed
= value
!= m_startValue
;
1336 grid
->GetTable()->SetValue(row
, col
, value
);
1338 m_startValue
= wxEmptyString
;
1339 Combo()->SetValue(m_startValue
);
1344 void wxGridCellChoiceEditor::Reset()
1346 Combo()->SetValue(m_startValue
);
1347 Combo()->SetInsertionPointEnd();
1350 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1360 wxStringTokenizer
tk(params
, _T(','));
1361 while ( tk
.HasMoreTokens() )
1363 m_choices
.Add(tk
.GetNextToken());
1367 // ----------------------------------------------------------------------------
1368 // wxGridCellEditorEvtHandler
1369 // ----------------------------------------------------------------------------
1371 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1373 switch ( event
.KeyCode() )
1377 m_grid
->DisableCellEditControl();
1381 event
.Skip( m_grid
->ProcessEvent( event
) );
1385 if (!m_grid
->ProcessEvent(event
))
1386 m_editor
->HandleReturn(event
);
1395 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1397 switch ( event
.KeyCode() )
1409 // ----------------------------------------------------------------------------
1410 // wxGridCellWorker is an (almost) empty common base class for
1411 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1412 // ----------------------------------------------------------------------------
1414 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1419 wxGridCellWorker::~wxGridCellWorker()
1423 // ============================================================================
1425 // ============================================================================
1427 // ----------------------------------------------------------------------------
1428 // wxGridCellRenderer
1429 // ----------------------------------------------------------------------------
1431 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1432 wxGridCellAttr
& attr
,
1435 int WXUNUSED(row
), int WXUNUSED(col
),
1438 dc
.SetBackgroundMode( wxSOLID
);
1442 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1446 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1449 dc
.SetPen( *wxTRANSPARENT_PEN
);
1450 dc
.DrawRectangle(rect
);
1453 // ----------------------------------------------------------------------------
1454 // wxGridCellStringRenderer
1455 // ----------------------------------------------------------------------------
1457 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1458 wxGridCellAttr
& attr
,
1462 dc
.SetBackgroundMode( wxTRANSPARENT
);
1464 // TODO some special colours for attr.IsReadOnly() case?
1468 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1469 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1473 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1474 dc
.SetTextForeground( attr
.GetTextColour() );
1477 dc
.SetFont( attr
.GetFont() );
1480 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1482 const wxString
& text
)
1484 wxCoord x
= 0, y
= 0, max_x
= 0;
1485 dc
.SetFont(attr
.GetFont());
1486 wxStringTokenizer
tk(text
, _T('\n'));
1487 while ( tk
.HasMoreTokens() )
1489 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1490 max_x
= wxMax(max_x
, x
);
1493 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1495 return wxSize(max_x
, y
);
1498 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1499 wxGridCellAttr
& attr
,
1503 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1506 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1507 wxGridCellAttr
& attr
,
1509 const wxRect
& rectCell
,
1513 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1515 // now we only have to draw the text
1516 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1519 attr
.GetAlignment(&hAlign
, &vAlign
);
1521 wxRect rect
= rectCell
;
1524 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1525 rect
, hAlign
, vAlign
);
1528 // ----------------------------------------------------------------------------
1529 // wxGridCellNumberRenderer
1530 // ----------------------------------------------------------------------------
1532 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1534 wxGridTableBase
*table
= grid
.GetTable();
1536 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1538 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1542 text
= table
->GetValue(row
, col
);
1548 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1549 wxGridCellAttr
& attr
,
1551 const wxRect
& rectCell
,
1555 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1557 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1559 // draw the text right aligned by default
1561 attr
.GetAlignment(&hAlign
, &vAlign
);
1564 wxRect rect
= rectCell
;
1567 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1570 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1571 wxGridCellAttr
& attr
,
1575 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1578 // ----------------------------------------------------------------------------
1579 // wxGridCellFloatRenderer
1580 // ----------------------------------------------------------------------------
1582 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1585 SetPrecision(precision
);
1588 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1590 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1591 renderer
->m_width
= m_width
;
1592 renderer
->m_precision
= m_precision
;
1593 renderer
->m_format
= m_format
;
1598 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1600 wxGridTableBase
*table
= grid
.GetTable();
1605 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1607 val
= table
->GetValueAsDouble(row
, col
);
1612 text
= table
->GetValue(row
, col
);
1613 hasDouble
= text
.ToDouble(&val
);
1620 if ( m_width
== -1 )
1622 // default width/precision
1623 m_format
= _T("%f");
1625 else if ( m_precision
== -1 )
1627 // default precision
1628 m_format
.Printf(_T("%%%d.f"), m_width
);
1632 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1636 text
.Printf(m_format
, val
);
1638 //else: text already contains the string
1643 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1644 wxGridCellAttr
& attr
,
1646 const wxRect
& rectCell
,
1650 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1652 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1654 // draw the text right aligned by default
1656 attr
.GetAlignment(&hAlign
, &vAlign
);
1659 wxRect rect
= rectCell
;
1662 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1665 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1666 wxGridCellAttr
& attr
,
1670 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1673 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1679 // reset to defaults
1685 wxString tmp
= params
.BeforeFirst(_T(','));
1689 if ( !tmp
.ToLong(&width
) )
1695 SetWidth((int)width
);
1697 tmp
= params
.AfterFirst(_T(','));
1701 if ( !tmp
.ToLong(&precision
) )
1707 SetPrecision((int)precision
);
1715 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1720 // ----------------------------------------------------------------------------
1721 // wxGridCellBoolRenderer
1722 // ----------------------------------------------------------------------------
1724 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1726 // FIXME these checkbox size calculations are really ugly...
1728 // between checkmark and box
1729 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1731 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1732 wxGridCellAttr
& WXUNUSED(attr
),
1737 // compute it only once (no locks for MT safeness in GUI thread...)
1738 if ( !ms_sizeCheckMark
.x
)
1740 // get checkbox size
1741 wxCoord checkSize
= 0;
1742 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1743 wxSize size
= checkbox
->GetBestSize();
1744 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1746 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1747 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1748 checkSize
-= size
.y
/ 2;
1753 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1756 return ms_sizeCheckMark
;
1759 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1760 wxGridCellAttr
& attr
,
1766 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1768 // draw a check mark in the centre (ignoring alignment - TODO)
1769 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1771 // don't draw outside the cell
1772 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1773 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1775 // and even leave (at least) 1 pixel margin
1776 size
.x
= size
.y
= minSize
- 2;
1779 // draw a border around checkmark
1781 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1782 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1783 rectBorder
.width
= size
.x
;
1784 rectBorder
.height
= size
.y
;
1787 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1788 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1790 value
= !!grid
.GetTable()->GetValue(row
, col
);
1794 wxRect rectMark
= rectBorder
;
1796 // MSW DrawCheckMark() is weird (and should probably be changed...)
1797 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1801 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1804 dc
.SetTextForeground(attr
.GetTextColour());
1805 dc
.DrawCheckMark(rectMark
);
1808 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1809 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1810 dc
.DrawRectangle(rectBorder
);
1813 // ----------------------------------------------------------------------------
1815 // ----------------------------------------------------------------------------
1817 wxGridCellAttr
*wxGridCellAttr::Clone() const
1819 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1820 if ( HasTextColour() )
1821 attr
->SetTextColour(GetTextColour());
1822 if ( HasBackgroundColour() )
1823 attr
->SetBackgroundColour(GetBackgroundColour());
1825 attr
->SetFont(GetFont());
1826 if ( HasAlignment() )
1827 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1831 attr
->SetRenderer(m_renderer
);
1832 m_renderer
->IncRef();
1836 attr
->SetEditor(m_editor
);
1841 attr
->SetReadOnly();
1843 attr
->SetDefAttr(m_defGridAttr
);
1848 const wxColour
& wxGridCellAttr::GetTextColour() const
1850 if (HasTextColour())
1854 else if (m_defGridAttr
!= this)
1856 return m_defGridAttr
->GetTextColour();
1860 wxFAIL_MSG(wxT("Missing default cell attribute"));
1861 return wxNullColour
;
1866 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1868 if (HasBackgroundColour())
1870 else if (m_defGridAttr
!= this)
1871 return m_defGridAttr
->GetBackgroundColour();
1874 wxFAIL_MSG(wxT("Missing default cell attribute"));
1875 return wxNullColour
;
1880 const wxFont
& wxGridCellAttr::GetFont() const
1884 else if (m_defGridAttr
!= this)
1885 return m_defGridAttr
->GetFont();
1888 wxFAIL_MSG(wxT("Missing default cell attribute"));
1894 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1898 if ( hAlign
) *hAlign
= m_hAlign
;
1899 if ( vAlign
) *vAlign
= m_vAlign
;
1901 else if (m_defGridAttr
!= this)
1902 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1905 wxFAIL_MSG(wxT("Missing default cell attribute"));
1910 // GetRenderer and GetEditor use a slightly different decision path about
1911 // which attribute to use. If a non-default attr object has one then it is
1912 // used, otherwise the default editor or renderer is fetched from the grid and
1913 // used. It should be the default for the data type of the cell. If it is
1914 // NULL (because the table has a type that the grid does not have in its
1915 // registry,) then the grid's default editor or renderer is used.
1917 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1919 wxGridCellRenderer
* renderer
= NULL
;
1921 if ( m_defGridAttr
!= this || grid
== NULL
)
1923 renderer
= m_renderer
; // use local attribute
1928 if ( !renderer
&& grid
) // get renderer for the data type
1930 // GetDefaultRendererForCell() will do IncRef() for us
1931 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1936 // if we still don't have one then use the grid default
1937 // (no need for IncRef() here neither)
1938 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1943 wxFAIL_MSG(wxT("Missing default cell attribute"));
1949 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1951 wxGridCellEditor
* editor
= NULL
;
1953 if ( m_defGridAttr
!= this || grid
== NULL
)
1955 editor
= m_editor
; // use local attribute
1960 if ( !editor
&& grid
) // get renderer for the data type
1961 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1964 // if we still don't have one then use the grid default
1965 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1969 wxFAIL_MSG(wxT("Missing default cell attribute"));
1975 // ----------------------------------------------------------------------------
1976 // wxGridCellAttrData
1977 // ----------------------------------------------------------------------------
1979 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1981 int n
= FindIndex(row
, col
);
1982 if ( n
== wxNOT_FOUND
)
1984 // add the attribute
1985 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1991 // change the attribute
1992 m_attrs
[(size_t)n
].attr
= attr
;
1996 // remove this attribute
1997 m_attrs
.RemoveAt((size_t)n
);
2002 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2004 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2006 int n
= FindIndex(row
, col
);
2007 if ( n
!= wxNOT_FOUND
)
2009 attr
= m_attrs
[(size_t)n
].attr
;
2016 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2018 size_t count
= m_attrs
.GetCount();
2019 for ( size_t n
= 0; n
< count
; n
++ )
2021 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2022 wxCoord row
= coords
.GetRow();
2023 if ((size_t)row
>= pos
)
2027 // If rows inserted, include row counter where necessary
2028 coords
.SetRow(row
+ numRows
);
2030 else if (numRows
< 0)
2032 // If rows deleted ...
2033 if ((size_t)row
>= pos
- numRows
)
2035 // ...either decrement row counter (if row still exists)...
2036 coords
.SetRow(row
+ numRows
);
2040 // ...or remove the attribute
2041 m_attrs
.RemoveAt((size_t)n
);
2049 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2051 size_t count
= m_attrs
.GetCount();
2052 for ( size_t n
= 0; n
< count
; n
++ )
2054 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2055 wxCoord col
= coords
.GetCol();
2056 if ( (size_t)col
>= pos
)
2060 // If rows inserted, include row counter where necessary
2061 coords
.SetCol(col
+ numCols
);
2063 else if (numCols
< 0)
2065 // If rows deleted ...
2066 if ((size_t)col
>= pos
- numCols
)
2068 // ...either decrement row counter (if row still exists)...
2069 coords
.SetCol(col
+ numCols
);
2073 // ...or remove the attribute
2074 m_attrs
.RemoveAt((size_t)n
);
2082 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2084 size_t count
= m_attrs
.GetCount();
2085 for ( size_t n
= 0; n
< count
; n
++ )
2087 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2088 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2097 // ----------------------------------------------------------------------------
2098 // wxGridRowOrColAttrData
2099 // ----------------------------------------------------------------------------
2101 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2103 size_t count
= m_attrs
.Count();
2104 for ( size_t n
= 0; n
< count
; n
++ )
2106 m_attrs
[n
]->DecRef();
2110 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2112 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2114 int n
= m_rowsOrCols
.Index(rowOrCol
);
2115 if ( n
!= wxNOT_FOUND
)
2117 attr
= m_attrs
[(size_t)n
];
2124 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2126 int i
= m_rowsOrCols
.Index(rowOrCol
);
2127 if ( i
== wxNOT_FOUND
)
2129 // add the attribute
2130 m_rowsOrCols
.Add(rowOrCol
);
2135 size_t n
= (size_t)i
;
2138 // change the attribute
2139 m_attrs
[n
]->DecRef();
2144 // remove this attribute
2145 m_attrs
[n
]->DecRef();
2146 m_rowsOrCols
.RemoveAt(n
);
2147 m_attrs
.RemoveAt(n
);
2152 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2154 size_t count
= m_attrs
.GetCount();
2155 for ( size_t n
= 0; n
< count
; n
++ )
2157 int & rowOrCol
= m_rowsOrCols
[n
];
2158 if ( (size_t)rowOrCol
>= pos
)
2160 if ( numRowsOrCols
> 0 )
2162 // If rows inserted, include row counter where necessary
2163 rowOrCol
+= numRowsOrCols
;
2165 else if ( numRowsOrCols
< 0)
2167 // If rows deleted, either decrement row counter (if row still exists)
2168 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2169 rowOrCol
+= numRowsOrCols
;
2172 m_rowsOrCols
.RemoveAt((size_t)n
);
2173 m_attrs
.RemoveAt((size_t)n
);
2181 // ----------------------------------------------------------------------------
2182 // wxGridCellAttrProvider
2183 // ----------------------------------------------------------------------------
2185 wxGridCellAttrProvider::wxGridCellAttrProvider()
2187 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2190 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2195 void wxGridCellAttrProvider::InitData()
2197 m_data
= new wxGridCellAttrProviderData
;
2200 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2202 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2205 // first look for the attribute of this specific cell
2206 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2210 // then look for the col attr (col attributes are more common than
2211 // the row ones, hence they have priority)
2212 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2217 // finally try the row attributes
2218 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2225 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2231 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2234 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2239 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2242 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2247 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2250 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2254 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2256 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2260 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2264 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2266 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2270 // ----------------------------------------------------------------------------
2271 // wxGridTypeRegistry
2272 // ----------------------------------------------------------------------------
2274 wxGridTypeRegistry::~wxGridTypeRegistry()
2276 size_t count
= m_typeinfo
.Count();
2277 for ( size_t i
= 0; i
< count
; i
++ )
2278 delete m_typeinfo
[i
];
2282 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2283 wxGridCellRenderer
* renderer
,
2284 wxGridCellEditor
* editor
)
2286 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2288 // is it already registered?
2289 int loc
= FindRegisteredDataType(typeName
);
2290 if ( loc
!= wxNOT_FOUND
)
2292 delete m_typeinfo
[loc
];
2293 m_typeinfo
[loc
] = info
;
2297 m_typeinfo
.Add(info
);
2301 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2303 size_t count
= m_typeinfo
.GetCount();
2304 for ( size_t i
= 0; i
< count
; i
++ )
2306 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2315 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2317 int index
= FindRegisteredDataType(typeName
);
2318 if ( index
== wxNOT_FOUND
)
2320 // check whether this is one of the standard ones, in which case
2321 // register it "on the fly"
2322 if ( typeName
== wxGRID_VALUE_STRING
)
2324 RegisterDataType(wxGRID_VALUE_STRING
,
2325 new wxGridCellStringRenderer
,
2326 new wxGridCellTextEditor
);
2328 else if ( typeName
== wxGRID_VALUE_BOOL
)
2330 RegisterDataType(wxGRID_VALUE_BOOL
,
2331 new wxGridCellBoolRenderer
,
2332 new wxGridCellBoolEditor
);
2334 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2336 RegisterDataType(wxGRID_VALUE_NUMBER
,
2337 new wxGridCellNumberRenderer
,
2338 new wxGridCellNumberEditor
);
2340 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2342 RegisterDataType(wxGRID_VALUE_FLOAT
,
2343 new wxGridCellFloatRenderer
,
2344 new wxGridCellFloatEditor
);
2346 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2348 RegisterDataType(wxGRID_VALUE_CHOICE
,
2349 new wxGridCellStringRenderer
,
2350 new wxGridCellChoiceEditor
);
2357 // we get here only if just added the entry for this type, so return
2359 index
= m_typeinfo
.GetCount() - 1;
2365 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2367 int index
= FindDataType(typeName
);
2368 if ( index
== wxNOT_FOUND
)
2370 // the first part of the typename is the "real" type, anything after ':'
2371 // are the parameters for the renderer
2372 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2373 if ( index
== wxNOT_FOUND
)
2378 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2379 wxGridCellRenderer
*rendererOld
= renderer
;
2380 renderer
= renderer
->Clone();
2381 rendererOld
->DecRef();
2383 wxGridCellEditor
*editor
= GetEditor(index
);
2384 wxGridCellEditor
*editorOld
= editor
;
2385 editor
= editor
->Clone();
2386 editorOld
->DecRef();
2388 // do it even if there are no parameters to reset them to defaults
2389 wxString params
= typeName
.AfterFirst(_T(':'));
2390 renderer
->SetParameters(params
);
2391 editor
->SetParameters(params
);
2393 // register the new typename
2394 RegisterDataType(typeName
, renderer
, editor
);
2396 // we just registered it, it's the last one
2397 index
= m_typeinfo
.GetCount() - 1;
2403 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2405 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2410 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2412 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2417 // ----------------------------------------------------------------------------
2419 // ----------------------------------------------------------------------------
2421 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2424 wxGridTableBase::wxGridTableBase()
2426 m_view
= (wxGrid
*) NULL
;
2427 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2430 wxGridTableBase::~wxGridTableBase()
2432 delete m_attrProvider
;
2435 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2437 delete m_attrProvider
;
2438 m_attrProvider
= attrProvider
;
2441 bool wxGridTableBase::CanHaveAttributes()
2443 if ( ! GetAttrProvider() )
2445 // use the default attr provider by default
2446 SetAttrProvider(new wxGridCellAttrProvider
);
2451 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2453 if ( m_attrProvider
)
2454 return m_attrProvider
->GetAttr(row
, col
);
2456 return (wxGridCellAttr
*)NULL
;
2459 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2461 if ( m_attrProvider
)
2463 m_attrProvider
->SetAttr(attr
, row
, col
);
2467 // as we take ownership of the pointer and don't store it, we must
2473 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2475 if ( m_attrProvider
)
2477 m_attrProvider
->SetRowAttr(attr
, row
);
2481 // as we take ownership of the pointer and don't store it, we must
2487 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2489 if ( m_attrProvider
)
2491 m_attrProvider
->SetColAttr(attr
, col
);
2495 // as we take ownership of the pointer and don't store it, we must
2501 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2502 size_t WXUNUSED(numRows
) )
2504 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2509 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2511 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2516 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2517 size_t WXUNUSED(numRows
) )
2519 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2524 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2525 size_t WXUNUSED(numCols
) )
2527 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2532 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2534 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2539 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2540 size_t WXUNUSED(numCols
) )
2542 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2548 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2551 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2552 // how much it makes sense to us geeks.
2556 wxString
wxGridTableBase::GetColLabelValue( int col
)
2558 // default col labels are:
2559 // cols 0 to 25 : A-Z
2560 // cols 26 to 675 : AA-ZZ
2565 for ( n
= 1; ; n
++ )
2567 s
+= (_T('A') + (wxChar
)( col%26
));
2569 if ( col
< 0 ) break;
2572 // reverse the string...
2574 for ( i
= 0; i
< n
; i
++ )
2583 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2585 return wxGRID_VALUE_STRING
;
2588 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2589 const wxString
& typeName
)
2591 return typeName
== wxGRID_VALUE_STRING
;
2594 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2596 return CanGetValueAs(row
, col
, typeName
);
2599 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2604 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2609 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2614 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2615 long WXUNUSED(value
) )
2619 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2620 double WXUNUSED(value
) )
2624 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2625 bool WXUNUSED(value
) )
2630 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2631 const wxString
& WXUNUSED(typeName
) )
2636 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2637 const wxString
& WXUNUSED(typeName
),
2638 void* WXUNUSED(value
) )
2642 //////////////////////////////////////////////////////////////////////
2644 // Message class for the grid table to send requests and notifications
2648 wxGridTableMessage::wxGridTableMessage()
2650 m_table
= (wxGridTableBase
*) NULL
;
2656 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2657 int commandInt1
, int commandInt2
)
2661 m_comInt1
= commandInt1
;
2662 m_comInt2
= commandInt2
;
2667 //////////////////////////////////////////////////////////////////////
2669 // A basic grid table for string data. An object of this class will
2670 // created by wxGrid if you don't specify an alternative table class.
2673 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2675 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2677 wxGridStringTable::wxGridStringTable()
2682 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2687 m_data
.Alloc( numRows
);
2690 sa
.Alloc( numCols
);
2691 for ( col
= 0; col
< numCols
; col
++ )
2693 sa
.Add( wxEmptyString
);
2696 for ( row
= 0; row
< numRows
; row
++ )
2702 wxGridStringTable::~wxGridStringTable()
2706 int wxGridStringTable::GetNumberRows()
2708 return m_data
.GetCount();
2711 int wxGridStringTable::GetNumberCols()
2713 if ( m_data
.GetCount() > 0 )
2714 return m_data
[0].GetCount();
2719 wxString
wxGridStringTable::GetValue( int row
, int col
)
2721 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2722 _T("invalid row or column index in wxGridStringTable") );
2724 return m_data
[row
][col
];
2727 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2729 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2730 _T("invalid row or column index in wxGridStringTable") );
2732 m_data
[row
][col
] = value
;
2735 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2737 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2738 _T("invalid row or column index in wxGridStringTable") );
2740 return (m_data
[row
][col
] == wxEmptyString
);
2743 void wxGridStringTable::Clear()
2746 int numRows
, numCols
;
2748 numRows
= m_data
.GetCount();
2751 numCols
= m_data
[0].GetCount();
2753 for ( row
= 0; row
< numRows
; row
++ )
2755 for ( col
= 0; col
< numCols
; col
++ )
2757 m_data
[row
][col
] = wxEmptyString
;
2764 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2768 size_t curNumRows
= m_data
.GetCount();
2769 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2770 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2772 if ( pos
>= curNumRows
)
2774 return AppendRows( numRows
);
2778 sa
.Alloc( curNumCols
);
2779 for ( col
= 0; col
< curNumCols
; col
++ )
2781 sa
.Add( wxEmptyString
);
2784 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2786 m_data
.Insert( sa
, row
);
2790 wxGridTableMessage
msg( this,
2791 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2795 GetView()->ProcessTableMessage( msg
);
2801 bool wxGridStringTable::AppendRows( size_t numRows
)
2805 size_t curNumRows
= m_data
.GetCount();
2806 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2807 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2810 if ( curNumCols
> 0 )
2812 sa
.Alloc( curNumCols
);
2813 for ( col
= 0; col
< curNumCols
; col
++ )
2815 sa
.Add( wxEmptyString
);
2819 for ( row
= 0; row
< numRows
; row
++ )
2826 wxGridTableMessage
msg( this,
2827 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2830 GetView()->ProcessTableMessage( msg
);
2836 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2840 size_t curNumRows
= m_data
.GetCount();
2842 if ( pos
>= curNumRows
)
2845 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2846 pos
, numRows
, curNumRows
);
2847 wxFAIL_MSG( errmsg
);
2851 if ( numRows
> curNumRows
- pos
)
2853 numRows
= curNumRows
- pos
;
2856 if ( numRows
>= curNumRows
)
2858 m_data
.Empty(); // don't release memory just yet
2862 for ( n
= 0; n
< numRows
; n
++ )
2864 m_data
.Remove( pos
);
2869 wxGridTableMessage
msg( this,
2870 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2874 GetView()->ProcessTableMessage( msg
);
2880 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2884 size_t curNumRows
= m_data
.GetCount();
2885 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2886 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2888 if ( pos
>= curNumCols
)
2890 return AppendCols( numCols
);
2893 for ( row
= 0; row
< curNumRows
; row
++ )
2895 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2897 m_data
[row
].Insert( wxEmptyString
, col
);
2902 wxGridTableMessage
msg( this,
2903 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2907 GetView()->ProcessTableMessage( msg
);
2913 bool wxGridStringTable::AppendCols( size_t numCols
)
2917 size_t curNumRows
= m_data
.GetCount();
2921 // TODO: something better than this ?
2923 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2928 for ( row
= 0; row
< curNumRows
; row
++ )
2930 for ( n
= 0; n
< numCols
; n
++ )
2932 m_data
[row
].Add( wxEmptyString
);
2938 wxGridTableMessage
msg( this,
2939 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2942 GetView()->ProcessTableMessage( msg
);
2948 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2952 size_t curNumRows
= m_data
.GetCount();
2953 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2954 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2956 if ( pos
>= curNumCols
)
2959 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
2960 pos
, numCols
, curNumCols
);
2961 wxFAIL_MSG( errmsg
);
2965 if ( numCols
> curNumCols
- pos
)
2967 numCols
= curNumCols
- pos
;
2970 for ( row
= 0; row
< curNumRows
; row
++ )
2972 if ( numCols
>= curNumCols
)
2974 m_data
[row
].Clear();
2978 for ( n
= 0; n
< numCols
; n
++ )
2980 m_data
[row
].Remove( pos
);
2986 wxGridTableMessage
msg( this,
2987 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2991 GetView()->ProcessTableMessage( msg
);
2997 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2999 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3001 // using default label
3003 return wxGridTableBase::GetRowLabelValue( row
);
3007 return m_rowLabels
[ row
];
3011 wxString
wxGridStringTable::GetColLabelValue( int col
)
3013 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3015 // using default label
3017 return wxGridTableBase::GetColLabelValue( col
);
3021 return m_colLabels
[ col
];
3025 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3027 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3029 int n
= m_rowLabels
.GetCount();
3031 for ( i
= n
; i
<= row
; i
++ )
3033 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3037 m_rowLabels
[row
] = value
;
3040 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3042 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3044 int n
= m_colLabels
.GetCount();
3046 for ( i
= n
; i
<= col
; i
++ )
3048 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3052 m_colLabels
[col
] = value
;
3057 //////////////////////////////////////////////////////////////////////
3058 //////////////////////////////////////////////////////////////////////
3060 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3062 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3063 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3064 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3065 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3066 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3069 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3071 const wxPoint
&pos
, const wxSize
&size
)
3072 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3077 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3081 // NO - don't do this because it will set both the x and y origin
3082 // coords to match the parent scrolled window and we just want to
3083 // set the y coord - MB
3085 // m_owner->PrepareDC( dc );
3088 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3089 dc
.SetDeviceOrigin( 0, -y
);
3091 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3092 m_owner
->DrawRowLabels( dc
);
3096 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3098 m_owner
->ProcessRowLabelMouseEvent( event
);
3102 // This seems to be required for wxMotif otherwise the mouse
3103 // cursor must be in the cell edit control to get key events
3105 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3107 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3110 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3112 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3117 //////////////////////////////////////////////////////////////////////
3119 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3121 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3122 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3123 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3124 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3125 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3128 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3130 const wxPoint
&pos
, const wxSize
&size
)
3131 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3136 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3140 // NO - don't do this because it will set both the x and y origin
3141 // coords to match the parent scrolled window and we just want to
3142 // set the x coord - MB
3144 // m_owner->PrepareDC( dc );
3147 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3148 dc
.SetDeviceOrigin( -x
, 0 );
3150 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3151 m_owner
->DrawColLabels( dc
);
3155 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3157 m_owner
->ProcessColLabelMouseEvent( event
);
3161 // This seems to be required for wxMotif otherwise the mouse
3162 // cursor must be in the cell edit control to get key events
3164 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3166 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3169 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3171 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3176 //////////////////////////////////////////////////////////////////////
3178 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3180 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3181 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3182 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3183 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3184 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3187 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3189 const wxPoint
&pos
, const wxSize
&size
)
3190 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3195 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3199 int client_height
= 0;
3200 int client_width
= 0;
3201 GetClientSize( &client_width
, &client_height
);
3203 dc
.SetPen( *wxBLACK_PEN
);
3204 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3205 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3207 dc
.SetPen( *wxWHITE_PEN
);
3208 dc
.DrawLine( 0, 0, client_width
, 0 );
3209 dc
.DrawLine( 0, 0, 0, client_height
);
3213 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3215 m_owner
->ProcessCornerLabelMouseEvent( event
);
3219 // This seems to be required for wxMotif otherwise the mouse
3220 // cursor must be in the cell edit control to get key events
3222 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3224 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3227 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3229 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3234 //////////////////////////////////////////////////////////////////////
3236 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3238 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3239 EVT_PAINT( wxGridWindow::OnPaint
)
3240 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3241 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3242 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3243 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3246 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3247 wxGridRowLabelWindow
*rowLblWin
,
3248 wxGridColLabelWindow
*colLblWin
,
3249 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3250 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3253 m_rowLabelWin
= rowLblWin
;
3254 m_colLabelWin
= colLblWin
;
3255 SetBackgroundColour( "WHITE" );
3259 wxGridWindow::~wxGridWindow()
3264 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3266 wxPaintDC
dc( this );
3267 m_owner
->PrepareDC( dc
);
3268 wxRegion reg
= GetUpdateRegion();
3269 m_owner
->CalcCellsExposed( reg
);
3270 m_owner
->DrawGridCellArea( dc
);
3271 #if WXGRID_DRAW_LINES
3272 m_owner
->DrawAllGridLines( dc
, reg
);
3274 m_owner
->DrawGridSpace( dc
);
3275 m_owner
->DrawHighlight( dc
);
3279 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3281 wxPanel::ScrollWindow( dx
, dy
, rect
);
3282 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3283 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3287 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3289 m_owner
->ProcessGridCellMouseEvent( event
);
3293 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3294 // cursor must be in the cell edit control to get key events
3296 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3298 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3301 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3303 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3306 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3311 //////////////////////////////////////////////////////////////////////
3314 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3316 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3317 EVT_PAINT( wxGrid::OnPaint
)
3318 EVT_SIZE( wxGrid::OnSize
)
3319 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3320 EVT_KEY_UP( wxGrid::OnKeyUp
)
3321 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3324 wxGrid::wxGrid( wxWindow
*parent
,
3329 const wxString
& name
)
3330 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3331 m_colMinWidths(GRID_HASH_SIZE
),
3332 m_rowMinHeights(GRID_HASH_SIZE
)
3341 wxSafeDecRef(m_defaultCellAttr
);
3343 #ifdef DEBUG_ATTR_CACHE
3344 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3345 wxPrintf(_T("wxGrid attribute cache statistics: "
3346 "total: %u, hits: %u (%u%%)\n"),
3347 total
, gs_nAttrCacheHits
,
3348 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3354 delete m_typeRegistry
;
3360 // ----- internal init and update functions
3363 void wxGrid::Create()
3365 m_created
= FALSE
; // set to TRUE by CreateGrid
3367 m_table
= (wxGridTableBase
*) NULL
;
3370 m_cellEditCtrlEnabled
= FALSE
;
3372 m_defaultCellAttr
= new wxGridCellAttr
;
3373 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3375 // Set default cell attributes
3376 m_defaultCellAttr
->SetFont(GetFont());
3377 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
3378 m_defaultCellAttr
->SetTextColour(
3379 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3380 m_defaultCellAttr
->SetBackgroundColour(
3381 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3382 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3383 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3388 m_currentCellCoords
= wxGridNoCellCoords
;
3390 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3391 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3393 // create the type registry
3394 m_typeRegistry
= new wxGridTypeRegistry
;
3396 // subwindow components that make up the wxGrid
3397 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3402 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3407 m_colLabelWin
= new wxGridColLabelWindow( this,
3412 m_gridWin
= new wxGridWindow( this,
3419 SetTargetWindow( m_gridWin
);
3423 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3424 wxGrid::wxGridSelectionModes selmode
)
3426 wxCHECK_MSG( !m_created
,
3428 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3430 m_numRows
= numRows
;
3431 m_numCols
= numCols
;
3433 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3434 m_table
->SetView( this );
3436 m_selection
= new wxGridSelection( this, selmode
);
3443 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3447 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3450 m_selection
->SetSelectionMode( selmode
);
3453 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3454 wxGrid::wxGridSelectionModes selmode
)
3458 // RD: Actually, this should probably be allowed. I think it would be
3459 // nice to be able to switch multiple Tables in and out of a single
3460 // View at runtime. Is there anything in the implmentation that would
3463 // At least, you now have to cope with m_selection
3464 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3469 m_numRows
= table
->GetNumberRows();
3470 m_numCols
= table
->GetNumberCols();
3473 m_table
->SetView( this );
3476 m_selection
= new wxGridSelection( this, selmode
);
3487 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3488 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3490 if ( m_rowLabelWin
)
3492 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3496 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3499 m_labelTextColour
= wxColour( _T("BLACK") );
3502 m_attrCache
.row
= -1;
3504 // TODO: something better than this ?
3506 m_labelFont
= this->GetFont();
3507 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3509 m_rowLabelHorizAlign
= wxLEFT
;
3510 m_rowLabelVertAlign
= wxCENTRE
;
3512 m_colLabelHorizAlign
= wxCENTRE
;
3513 m_colLabelVertAlign
= wxTOP
;
3515 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3516 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3518 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3519 m_defaultRowHeight
+= 8;
3521 m_defaultRowHeight
+= 4;
3524 m_gridLineColour
= wxColour( 128, 128, 255 );
3525 m_gridLinesEnabled
= TRUE
;
3526 m_cellHighlightColour
= m_gridLineColour
;
3528 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3529 m_winCapture
= (wxWindow
*)NULL
;
3530 m_canDragRowSize
= TRUE
;
3531 m_canDragColSize
= TRUE
;
3532 m_canDragGridSize
= TRUE
;
3534 m_dragRowOrCol
= -1;
3535 m_isDragging
= FALSE
;
3536 m_startDragPos
= wxDefaultPosition
;
3538 m_waitForSlowClick
= FALSE
;
3540 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3541 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3543 m_currentCellCoords
= wxGridNoCellCoords
;
3545 m_selectingTopLeft
= wxGridNoCellCoords
;
3546 m_selectingBottomRight
= wxGridNoCellCoords
;
3547 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3548 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3550 m_editable
= TRUE
; // default for whole grid
3552 m_inOnKeyDown
= FALSE
;
3561 // ----------------------------------------------------------------------------
3562 // the idea is to call these functions only when necessary because they create
3563 // quite big arrays which eat memory mostly unnecessary - in particular, if
3564 // default widths/heights are used for all rows/columns, we may not use these
3567 // with some extra code, it should be possible to only store the
3568 // widths/heights different from default ones but this will be done later...
3569 // ----------------------------------------------------------------------------
3571 void wxGrid::InitRowHeights()
3573 m_rowHeights
.Empty();
3574 m_rowBottoms
.Empty();
3576 m_rowHeights
.Alloc( m_numRows
);
3577 m_rowBottoms
.Alloc( m_numRows
);
3580 for ( int i
= 0; i
< m_numRows
; i
++ )
3582 m_rowHeights
.Add( m_defaultRowHeight
);
3583 rowBottom
+= m_defaultRowHeight
;
3584 m_rowBottoms
.Add( rowBottom
);
3588 void wxGrid::InitColWidths()
3590 m_colWidths
.Empty();
3591 m_colRights
.Empty();
3593 m_colWidths
.Alloc( m_numCols
);
3594 m_colRights
.Alloc( m_numCols
);
3596 for ( int i
= 0; i
< m_numCols
; i
++ )
3598 m_colWidths
.Add( m_defaultColWidth
);
3599 colRight
+= m_defaultColWidth
;
3600 m_colRights
.Add( colRight
);
3604 int wxGrid::GetColWidth(int col
) const
3606 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3609 int wxGrid::GetColLeft(int col
) const
3611 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3612 : m_colRights
[col
] - m_colWidths
[col
];
3615 int wxGrid::GetColRight(int col
) const
3617 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3621 int wxGrid::GetRowHeight(int row
) const
3623 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3626 int wxGrid::GetRowTop(int row
) const
3628 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3629 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3632 int wxGrid::GetRowBottom(int row
) const
3634 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3635 : m_rowBottoms
[row
];
3638 void wxGrid::CalcDimensions()
3641 GetClientSize( &cw
, &ch
);
3643 if ( m_numRows
> 0 || m_numCols
> 0 )
3645 int right
= m_numCols
> 0 ? GetColRight( m_numCols
-1 ) + m_extraWidth
: 0;
3646 int bottom
= m_numRows
> 0 ? GetRowBottom( m_numRows
-1 ) + m_extraHeight
: 0;
3648 // TODO: restore the scroll position that we had before sizing
3651 GetViewStart( &x
, &y
);
3652 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3653 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3659 void wxGrid::CalcWindowSizes()
3662 GetClientSize( &cw
, &ch
);
3664 if ( m_cornerLabelWin
->IsShown() )
3665 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3667 if ( m_colLabelWin
->IsShown() )
3668 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3670 if ( m_rowLabelWin
->IsShown() )
3671 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3673 if ( m_gridWin
->IsShown() )
3674 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3678 // this is called when the grid table sends a message to say that it
3679 // has been redimensioned
3681 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3684 bool result
= FALSE
;
3687 // if we were using the default widths/heights so far, we must change them
3689 if ( m_colWidths
.IsEmpty() )
3694 if ( m_rowHeights
.IsEmpty() )
3700 switch ( msg
.GetId() )
3702 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3704 size_t pos
= msg
.GetCommandInt();
3705 int numRows
= msg
.GetCommandInt2();
3707 m_numRows
+= numRows
;
3709 if ( !m_rowHeights
.IsEmpty() )
3711 for ( i
= 0; i
< numRows
; i
++ )
3713 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3714 m_rowBottoms
.Insert( 0, pos
);
3718 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3720 for ( i
= pos
; i
< m_numRows
; i
++ )
3722 bottom
+= m_rowHeights
[i
];
3723 m_rowBottoms
[i
] = bottom
;
3726 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3728 // if we have just inserted cols into an empty grid the current
3729 // cell will be undefined...
3731 SetCurrentCell( 0, 0 );
3733 m_selection
->UpdateRows( pos
, numRows
);
3734 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3736 attrProvider
->UpdateAttrRows( pos
, numRows
);
3738 if ( !GetBatchCount() )
3741 m_rowLabelWin
->Refresh();
3747 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3749 int numRows
= msg
.GetCommandInt();
3750 int oldNumRows
= m_numRows
;
3751 m_numRows
+= numRows
;
3753 if ( !m_rowHeights
.IsEmpty() )
3755 for ( i
= 0; i
< numRows
; i
++ )
3757 m_rowHeights
.Add( m_defaultRowHeight
);
3758 m_rowBottoms
.Add( 0 );
3762 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3764 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3766 bottom
+= m_rowHeights
[i
];
3767 m_rowBottoms
[i
] = bottom
;
3770 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3772 // if we have just inserted cols into an empty grid the current
3773 // cell will be undefined...
3775 SetCurrentCell( 0, 0 );
3777 if ( !GetBatchCount() )
3780 m_rowLabelWin
->Refresh();
3786 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3788 size_t pos
= msg
.GetCommandInt();
3789 int numRows
= msg
.GetCommandInt2();
3790 m_numRows
-= numRows
;
3792 if ( !m_rowHeights
.IsEmpty() )
3794 for ( i
= 0; i
< numRows
; i
++ )
3796 m_rowHeights
.Remove( pos
);
3797 m_rowBottoms
.Remove( pos
);
3801 for ( i
= 0; i
< m_numRows
; i
++ )
3803 h
+= m_rowHeights
[i
];
3804 m_rowBottoms
[i
] = h
;
3809 m_currentCellCoords
= wxGridNoCellCoords
;
3813 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3814 m_currentCellCoords
.Set( 0, 0 );
3816 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3817 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3819 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3820 // ifdef'd out following patch from Paul Gammans
3822 // removed _all_ rows, in this case, we remove
3823 // all column attributes.
3824 // I hate to do this here, but the
3825 // needed data is not available inside UpdateAttrRows.
3826 if ( !GetNumberRows() )
3827 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3830 if ( !GetBatchCount() )
3833 m_rowLabelWin
->Refresh();
3839 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3841 size_t pos
= msg
.GetCommandInt();
3842 int numCols
= msg
.GetCommandInt2();
3843 m_numCols
+= numCols
;
3845 if ( !m_colWidths
.IsEmpty() )
3847 for ( i
= 0; i
< numCols
; i
++ )
3849 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3850 m_colRights
.Insert( 0, pos
);
3854 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3856 for ( i
= pos
; i
< m_numCols
; i
++ )
3858 right
+= m_colWidths
[i
];
3859 m_colRights
[i
] = right
;
3862 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3864 // if we have just inserted cols into an empty grid the current
3865 // cell will be undefined...
3867 SetCurrentCell( 0, 0 );
3869 m_selection
->UpdateCols( pos
, numCols
);
3870 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3872 attrProvider
->UpdateAttrCols( pos
, numCols
);
3873 if ( !GetBatchCount() )
3876 m_colLabelWin
->Refresh();
3883 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3885 int numCols
= msg
.GetCommandInt();
3886 int oldNumCols
= m_numCols
;
3887 m_numCols
+= numCols
;
3888 if ( !m_colWidths
.IsEmpty() )
3890 for ( i
= 0; i
< numCols
; i
++ )
3892 m_colWidths
.Add( m_defaultColWidth
);
3893 m_colRights
.Add( 0 );
3897 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3899 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3901 right
+= m_colWidths
[i
];
3902 m_colRights
[i
] = right
;
3905 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3907 // if we have just inserted cols into an empty grid the current
3908 // cell will be undefined...
3910 SetCurrentCell( 0, 0 );
3912 if ( !GetBatchCount() )
3915 m_colLabelWin
->Refresh();
3921 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3923 size_t pos
= msg
.GetCommandInt();
3924 int numCols
= msg
.GetCommandInt2();
3925 m_numCols
-= numCols
;
3927 if ( !m_colWidths
.IsEmpty() )
3929 for ( i
= 0; i
< numCols
; i
++ )
3931 m_colWidths
.Remove( pos
);
3932 m_colRights
.Remove( pos
);
3936 for ( i
= 0; i
< m_numCols
; i
++ )
3938 w
+= m_colWidths
[i
];
3944 m_currentCellCoords
= wxGridNoCellCoords
;
3948 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3949 m_currentCellCoords
.Set( 0, 0 );
3951 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3952 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3954 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3955 // ifdef'd out following patch from Paul Gammans
3957 // No need to touch row attributes, unless we
3958 // removed _all_ columns, in this case, we remove
3959 // all row attributes.
3960 // I hate to do this here, but the
3961 // needed data is not available inside UpdateAttrCols.
3962 if ( !GetNumberCols() )
3963 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
3966 if ( !GetBatchCount() )
3969 m_colLabelWin
->Refresh();
3974 // There is no path to this code !!!!!!
3980 if (result
&& !GetBatchCount() )
3981 m_gridWin
->Refresh();
3986 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
3988 wxRegionIterator
iter( reg
);
3991 m_rowLabelsExposed
.Empty();
3998 // TODO: remove this when we can...
3999 // There is a bug in wxMotif that gives garbage update
4000 // rectangles if you jump-scroll a long way by clicking the
4001 // scrollbar with middle button. This is a work-around
4003 #if defined(__WXMOTIF__)
4005 m_gridWin
->GetClientSize( &cw
, &ch
);
4006 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4007 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4010 // logical bounds of update region
4013 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4014 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4016 // find the row labels within these bounds
4019 for ( row
= 0; row
< m_numRows
; row
++ )
4021 if ( GetRowBottom(row
) < top
)
4024 if ( GetRowTop(row
) > bottom
)
4027 m_rowLabelsExposed
.Add( row
);
4035 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4037 wxRegionIterator
iter( reg
);
4040 m_colLabelsExposed
.Empty();
4047 // TODO: remove this when we can...
4048 // There is a bug in wxMotif that gives garbage update
4049 // rectangles if you jump-scroll a long way by clicking the
4050 // scrollbar with middle button. This is a work-around
4052 #if defined(__WXMOTIF__)
4054 m_gridWin
->GetClientSize( &cw
, &ch
);
4055 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4056 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4059 // logical bounds of update region
4062 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4063 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4065 // find the cells within these bounds
4068 for ( col
= 0; col
< m_numCols
; col
++ )
4070 if ( GetColRight(col
) < left
)
4073 if ( GetColLeft(col
) > right
)
4076 m_colLabelsExposed
.Add( col
);
4084 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4086 wxRegionIterator
iter( reg
);
4089 m_cellsExposed
.Empty();
4090 m_rowsExposed
.Empty();
4091 m_colsExposed
.Empty();
4093 int left
, top
, right
, bottom
;
4098 // TODO: remove this when we can...
4099 // There is a bug in wxMotif that gives garbage update
4100 // rectangles if you jump-scroll a long way by clicking the
4101 // scrollbar with middle button. This is a work-around
4103 #if defined(__WXMOTIF__)
4105 m_gridWin
->GetClientSize( &cw
, &ch
);
4106 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4107 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4108 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4109 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4112 // logical bounds of update region
4114 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4115 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4117 // find the cells within these bounds
4120 for ( row
= 0; row
< m_numRows
; row
++ )
4122 if ( GetRowBottom(row
) <= top
)
4125 if ( GetRowTop(row
) > bottom
)
4128 m_rowsExposed
.Add( row
);
4130 for ( col
= 0; col
< m_numCols
; col
++ )
4132 if ( GetColRight(col
) <= left
)
4135 if ( GetColLeft(col
) > right
)
4138 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4139 m_colsExposed
.Add( col
);
4140 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4149 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4152 wxPoint
pos( event
.GetPosition() );
4153 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4155 if ( event
.Dragging() )
4157 m_isDragging
= TRUE
;
4159 if ( event
.LeftIsDown() )
4161 switch( m_cursorMode
)
4163 case WXGRID_CURSOR_RESIZE_ROW
:
4165 int cw
, ch
, left
, dummy
;
4166 m_gridWin
->GetClientSize( &cw
, &ch
);
4167 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4169 wxClientDC
dc( m_gridWin
);
4172 GetRowTop(m_dragRowOrCol
) +
4173 GetRowMinimalHeight(m_dragRowOrCol
) );
4174 dc
.SetLogicalFunction(wxINVERT
);
4175 if ( m_dragLastPos
>= 0 )
4177 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4179 dc
.DrawLine( left
, y
, left
+cw
, y
);
4184 case WXGRID_CURSOR_SELECT_ROW
:
4185 if ( (row
= YToRow( y
)) >= 0 )
4187 m_selection
->SelectRow( row
,
4188 event
.ControlDown(),
4194 // default label to suppress warnings about "enumeration value
4195 // 'xxx' not handled in switch
4203 m_isDragging
= FALSE
;
4206 // ------------ Entering or leaving the window
4208 if ( event
.Entering() || event
.Leaving() )
4210 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4214 // ------------ Left button pressed
4216 else if ( event
.LeftDown() )
4218 // don't send a label click event for a hit on the
4219 // edge of the row label - this is probably the user
4220 // wanting to resize the row
4222 if ( YToEdgeOfRow(y
) < 0 )
4226 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4228 if ( !event
.ShiftDown() && !event
.ControlDown() )
4230 if ( event
.ShiftDown() )
4231 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4234 GetNumberCols() - 1,
4235 event
.ControlDown(),
4240 m_selection
->SelectRow( row
,
4241 event
.ControlDown(),
4245 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4250 // starting to drag-resize a row
4252 if ( CanDragRowSize() )
4253 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4258 // ------------ Left double click
4260 else if (event
.LeftDClick() )
4262 if ( YToEdgeOfRow(y
) < 0 )
4265 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4270 // ------------ Left button released
4272 else if ( event
.LeftUp() )
4274 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4276 DoEndDragResizeRow();
4278 // Note: we are ending the event *after* doing
4279 // default processing in this case
4281 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4284 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4289 // ------------ Right button down
4291 else if ( event
.RightDown() )
4294 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4296 // no default action at the moment
4301 // ------------ Right double click
4303 else if ( event
.RightDClick() )
4306 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4308 // no default action at the moment
4313 // ------------ No buttons down and mouse moving
4315 else if ( event
.Moving() )
4317 m_dragRowOrCol
= YToEdgeOfRow( y
);
4318 if ( m_dragRowOrCol
>= 0 )
4320 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4322 // don't capture the mouse yet
4323 if ( CanDragRowSize() )
4324 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4327 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4329 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4335 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4338 wxPoint
pos( event
.GetPosition() );
4339 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4341 if ( event
.Dragging() )
4343 m_isDragging
= TRUE
;
4345 if ( event
.LeftIsDown() )
4347 switch( m_cursorMode
)
4349 case WXGRID_CURSOR_RESIZE_COL
:
4351 int cw
, ch
, dummy
, top
;
4352 m_gridWin
->GetClientSize( &cw
, &ch
);
4353 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4355 wxClientDC
dc( m_gridWin
);
4358 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4359 GetColMinimalWidth(m_dragRowOrCol
));
4360 dc
.SetLogicalFunction(wxINVERT
);
4361 if ( m_dragLastPos
>= 0 )
4363 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4365 dc
.DrawLine( x
, top
, x
, top
+ch
);
4370 case WXGRID_CURSOR_SELECT_COL
:
4371 if ( (col
= XToCol( x
)) >= 0 )
4373 m_selection
->SelectCol( col
,
4374 event
.ControlDown(),
4380 // default label to suppress warnings about "enumeration value
4381 // 'xxx' not handled in switch
4389 m_isDragging
= FALSE
;
4392 // ------------ Entering or leaving the window
4394 if ( event
.Entering() || event
.Leaving() )
4396 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4400 // ------------ Left button pressed
4402 else if ( event
.LeftDown() )
4404 // don't send a label click event for a hit on the
4405 // edge of the col label - this is probably the user
4406 // wanting to resize the col
4408 if ( XToEdgeOfCol(x
) < 0 )
4412 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4414 if ( !event
.ShiftDown() && !event
.ControlDown() )
4416 if ( event
.ShiftDown() )
4417 m_selection
->SelectBlock( 0,
4418 m_currentCellCoords
.GetCol(),
4419 GetNumberRows() - 1, col
,
4420 event
.ControlDown(),
4425 m_selection
->SelectCol( col
,
4426 event
.ControlDown(),
4430 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4435 // starting to drag-resize a col
4437 if ( CanDragColSize() )
4438 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4443 // ------------ Left double click
4445 if ( event
.LeftDClick() )
4447 if ( XToEdgeOfCol(x
) < 0 )
4450 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4455 // ------------ Left button released
4457 else if ( event
.LeftUp() )
4459 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4461 DoEndDragResizeCol();
4463 // Note: we are ending the event *after* doing
4464 // default processing in this case
4466 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4469 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4474 // ------------ Right button down
4476 else if ( event
.RightDown() )
4479 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4481 // no default action at the moment
4486 // ------------ Right double click
4488 else if ( event
.RightDClick() )
4491 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4493 // no default action at the moment
4498 // ------------ No buttons down and mouse moving
4500 else if ( event
.Moving() )
4502 m_dragRowOrCol
= XToEdgeOfCol( x
);
4503 if ( m_dragRowOrCol
>= 0 )
4505 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4507 // don't capture the cursor yet
4508 if ( CanDragColSize() )
4509 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4512 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4514 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4520 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4522 if ( event
.LeftDown() )
4524 // indicate corner label by having both row and
4527 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4533 else if ( event
.LeftDClick() )
4535 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4538 else if ( event
.RightDown() )
4540 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4542 // no default action at the moment
4546 else if ( event
.RightDClick() )
4548 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4550 // no default action at the moment
4555 void wxGrid::ChangeCursorMode(CursorMode mode
,
4560 static const wxChar
*cursorModes
[] =
4569 wxLogTrace(_T("grid"),
4570 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4571 win
== m_colLabelWin
? _T("colLabelWin")
4572 : win
? _T("rowLabelWin")
4574 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4575 #endif // __WXDEBUG__
4577 if ( mode
== m_cursorMode
)
4582 // by default use the grid itself
4588 m_winCapture
->ReleaseMouse();
4589 m_winCapture
= (wxWindow
*)NULL
;
4592 m_cursorMode
= mode
;
4594 switch ( m_cursorMode
)
4596 case WXGRID_CURSOR_RESIZE_ROW
:
4597 win
->SetCursor( m_rowResizeCursor
);
4600 case WXGRID_CURSOR_RESIZE_COL
:
4601 win
->SetCursor( m_colResizeCursor
);
4605 win
->SetCursor( *wxSTANDARD_CURSOR
);
4608 // we need to capture mouse when resizing
4609 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4610 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4612 if ( captureMouse
&& resize
)
4614 win
->CaptureMouse();
4619 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4622 wxPoint
pos( event
.GetPosition() );
4623 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4625 wxGridCellCoords coords
;
4626 XYToCell( x
, y
, coords
);
4628 if ( event
.Dragging() )
4630 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4632 // Don't start doing anything until the mouse has been drug at
4633 // least 3 pixels in any direction...
4636 if (m_startDragPos
== wxDefaultPosition
)
4638 m_startDragPos
= pos
;
4641 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4645 m_isDragging
= TRUE
;
4646 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4648 // Hide the edit control, so it
4649 // won't interfer with drag-shrinking.
4650 if ( IsCellEditControlShown() )
4652 HideCellEditControl();
4653 SaveEditControlValue();
4656 // Have we captured the mouse yet?
4659 m_winCapture
= m_gridWin
;
4660 m_winCapture
->CaptureMouse();
4663 if ( coords
!= wxGridNoCellCoords
)
4665 if ( event
.ControlDown() )
4667 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4668 m_selectingKeyboard
= coords
;
4669 SelectBlock ( m_selectingKeyboard
, coords
);
4673 if ( !IsSelection() )
4675 SelectBlock( coords
, coords
);
4679 SelectBlock( m_currentCellCoords
, coords
);
4683 if (! IsVisible(coords
))
4685 MakeCellVisible(coords
);
4686 // TODO: need to introduce a delay or something here. The
4687 // scrolling is way to fast, at least on MSW - also on GTK.
4691 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4693 int cw
, ch
, left
, dummy
;
4694 m_gridWin
->GetClientSize( &cw
, &ch
);
4695 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4697 wxClientDC
dc( m_gridWin
);
4699 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4700 GetRowMinimalHeight(m_dragRowOrCol
) );
4701 dc
.SetLogicalFunction(wxINVERT
);
4702 if ( m_dragLastPos
>= 0 )
4704 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4706 dc
.DrawLine( left
, y
, left
+cw
, y
);
4709 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4711 int cw
, ch
, dummy
, top
;
4712 m_gridWin
->GetClientSize( &cw
, &ch
);
4713 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4715 wxClientDC
dc( m_gridWin
);
4717 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4718 GetColMinimalWidth(m_dragRowOrCol
) );
4719 dc
.SetLogicalFunction(wxINVERT
);
4720 if ( m_dragLastPos
>= 0 )
4722 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4724 dc
.DrawLine( x
, top
, x
, top
+ch
);
4731 m_isDragging
= FALSE
;
4732 m_startDragPos
= wxDefaultPosition
;
4734 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4735 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4738 if ( event
.Entering() || event
.Leaving() )
4740 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4741 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4746 // ------------ Left button pressed
4748 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4750 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4755 if ( !event
.ControlDown() )
4757 if ( event
.ShiftDown() )
4759 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4760 m_currentCellCoords
.GetCol(),
4763 event
.ControlDown(),
4768 else if ( XToEdgeOfCol(x
) < 0 &&
4769 YToEdgeOfRow(y
) < 0 )
4771 DisableCellEditControl();
4772 MakeCellVisible( coords
);
4774 // if this is the second click on this cell then start
4776 if ( m_waitForSlowClick
&&
4777 (coords
== m_currentCellCoords
) &&
4778 CanEnableCellControl())
4780 EnableCellEditControl();
4782 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4783 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4786 editor
->StartingClick();
4790 m_waitForSlowClick
= FALSE
;
4794 if ( event
.ControlDown() )
4796 m_selection
->ToggleCellSelection( coords
.GetRow(),
4798 event
.ControlDown(),
4802 m_selectingTopLeft
= wxGridNoCellCoords
;
4803 m_selectingBottomRight
= wxGridNoCellCoords
;
4804 m_selectingKeyboard
= coords
;
4808 SetCurrentCell( coords
);
4809 if ( m_selection
->GetSelectionMode()
4810 != wxGrid::wxGridSelectCells
)
4811 SelectBlock( coords
, coords
);
4813 m_waitForSlowClick
= TRUE
;
4820 // ------------ Left double click
4822 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4824 DisableCellEditControl();
4826 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4828 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4836 // ------------ Left button released
4838 else if ( event
.LeftUp() )
4840 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4842 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4843 m_selectingBottomRight
!= wxGridNoCellCoords
)
4847 m_winCapture
->ReleaseMouse();
4848 m_winCapture
= NULL
;
4850 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4851 m_selectingTopLeft
.GetCol(),
4852 m_selectingBottomRight
.GetRow(),
4853 m_selectingBottomRight
.GetCol(),
4854 event
.ControlDown(),
4858 m_selectingTopLeft
= wxGridNoCellCoords
;
4859 m_selectingBottomRight
= wxGridNoCellCoords
;
4862 // Show the edit control, if it has been hidden for
4864 ShowCellEditControl();
4866 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4868 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4869 DoEndDragResizeRow();
4871 // Note: we are ending the event *after* doing
4872 // default processing in this case
4874 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4876 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4878 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4879 DoEndDragResizeCol();
4881 // Note: we are ending the event *after* doing
4882 // default processing in this case
4884 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4891 // ------------ Right button down
4893 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4895 DisableCellEditControl();
4896 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4901 // no default action at the moment
4906 // ------------ Right double click
4908 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4910 DisableCellEditControl();
4911 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4916 // no default action at the moment
4920 // ------------ Moving and no button action
4922 else if ( event
.Moving() && !event
.IsButton() )
4924 int dragRow
= YToEdgeOfRow( y
);
4925 int dragCol
= XToEdgeOfCol( x
);
4927 // Dragging on the corner of a cell to resize in both
4928 // directions is not implemented yet...
4930 if ( dragRow
>= 0 && dragCol
>= 0 )
4932 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4938 m_dragRowOrCol
= dragRow
;
4940 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4942 if ( CanDragRowSize() && CanDragGridSize() )
4943 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4948 m_dragRowOrCol
= dragCol
;
4956 m_dragRowOrCol
= dragCol
;
4958 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4960 if ( CanDragColSize() && CanDragGridSize() )
4961 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4967 // Neither on a row or col edge
4969 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4971 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4977 void wxGrid::DoEndDragResizeRow()
4979 if ( m_dragLastPos
>= 0 )
4981 // erase the last line and resize the row
4983 int cw
, ch
, left
, dummy
;
4984 m_gridWin
->GetClientSize( &cw
, &ch
);
4985 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4987 wxClientDC
dc( m_gridWin
);
4989 dc
.SetLogicalFunction( wxINVERT
);
4990 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4991 HideCellEditControl();
4992 SaveEditControlValue();
4994 int rowTop
= GetRowTop(m_dragRowOrCol
);
4995 SetRowSize( m_dragRowOrCol
,
4996 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4998 if ( !GetBatchCount() )
5000 // Only needed to get the correct rect.y:
5001 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5003 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5004 rect
.width
= m_rowLabelWidth
;
5005 rect
.height
= ch
- rect
.y
;
5006 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5008 m_gridWin
->Refresh( FALSE
, &rect
);
5011 ShowCellEditControl();
5016 void wxGrid::DoEndDragResizeCol()
5018 if ( m_dragLastPos
>= 0 )
5020 // erase the last line and resize the col
5022 int cw
, ch
, dummy
, top
;
5023 m_gridWin
->GetClientSize( &cw
, &ch
);
5024 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5026 wxClientDC
dc( m_gridWin
);
5028 dc
.SetLogicalFunction( wxINVERT
);
5029 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5030 HideCellEditControl();
5031 SaveEditControlValue();
5033 int colLeft
= GetColLeft(m_dragRowOrCol
);
5034 SetColSize( m_dragRowOrCol
,
5035 wxMax( m_dragLastPos
- colLeft
,
5036 GetColMinimalWidth(m_dragRowOrCol
) ) );
5038 if ( !GetBatchCount() )
5040 // Only needed to get the correct rect.x:
5041 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5043 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5044 rect
.width
= cw
- rect
.x
;
5045 rect
.height
= m_colLabelHeight
;
5046 m_colLabelWin
->Refresh( TRUE
, &rect
);
5048 m_gridWin
->Refresh( FALSE
, &rect
);
5051 ShowCellEditControl();
5058 // ------ interaction with data model
5060 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5062 switch ( msg
.GetId() )
5064 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5065 return GetModelValues();
5067 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5068 return SetModelValues();
5070 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5071 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5072 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5073 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5074 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5075 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5076 return Redimension( msg
);
5085 // The behaviour of this function depends on the grid table class
5086 // Clear() function. For the default wxGridStringTable class the
5087 // behavious is to replace all cell contents with wxEmptyString but
5088 // not to change the number of rows or cols.
5090 void wxGrid::ClearGrid()
5094 if (IsCellEditControlEnabled())
5095 DisableCellEditControl();
5098 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5103 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5105 // TODO: something with updateLabels flag
5109 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5115 if (IsCellEditControlEnabled())
5116 DisableCellEditControl();
5118 return m_table
->InsertRows( pos
, numRows
);
5120 // the table will have sent the results of the insert row
5121 // operation to this view object as a grid table message
5127 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5129 // TODO: something with updateLabels flag
5133 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5137 return ( m_table
&& m_table
->AppendRows( numRows
) );
5138 // the table will have sent the results of the append row
5139 // operation to this view object as a grid table message
5143 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5145 // TODO: something with updateLabels flag
5149 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5155 if (IsCellEditControlEnabled())
5156 DisableCellEditControl();
5158 return (m_table
->DeleteRows( pos
, numRows
));
5159 // the table will have sent the results of the delete row
5160 // operation to this view object as a grid table message
5166 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5168 // TODO: something with updateLabels flag
5172 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5178 if (IsCellEditControlEnabled())
5179 DisableCellEditControl();
5181 return m_table
->InsertCols( pos
, numCols
);
5182 // the table will have sent the results of the insert col
5183 // operation to this view object as a grid table message
5189 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5191 // TODO: something with updateLabels flag
5195 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5199 return ( m_table
&& m_table
->AppendCols( numCols
) );
5200 // the table will have sent the results of the append col
5201 // operation to this view object as a grid table message
5205 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5207 // TODO: something with updateLabels flag
5211 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5217 if (IsCellEditControlEnabled())
5218 DisableCellEditControl();
5220 return ( m_table
->DeleteCols( pos
, numCols
) );
5221 // the table will have sent the results of the delete col
5222 // operation to this view object as a grid table message
5230 // ----- event handlers
5233 // Generate a grid event based on a mouse event and
5234 // return the result of ProcessEvent()
5236 bool wxGrid::SendEvent( const wxEventType type
,
5238 wxMouseEvent
& mouseEv
)
5240 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5242 int rowOrCol
= (row
== -1 ? col
: row
);
5244 wxGridSizeEvent
gridEvt( GetId(),
5248 mouseEv
.GetX() + GetColLabelSize(),
5249 mouseEv
.GetY() + GetRowLabelSize(),
5250 mouseEv
.ControlDown(),
5251 mouseEv
.ShiftDown(),
5253 mouseEv
.MetaDown() );
5255 return GetEventHandler()->ProcessEvent(gridEvt
);
5257 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5259 // Right now, it should _never_ end up here!
5260 wxGridRangeSelectEvent
gridEvt( GetId(),
5264 m_selectingBottomRight
,
5266 mouseEv
.ControlDown(),
5267 mouseEv
.ShiftDown(),
5269 mouseEv
.MetaDown() );
5271 return GetEventHandler()->ProcessEvent(gridEvt
);
5275 wxGridEvent
gridEvt( GetId(),
5279 mouseEv
.GetX() + GetColLabelSize(),
5280 mouseEv
.GetY() + GetRowLabelSize(),
5282 mouseEv
.ControlDown(),
5283 mouseEv
.ShiftDown(),
5285 mouseEv
.MetaDown() );
5287 return GetEventHandler()->ProcessEvent(gridEvt
);
5292 // Generate a grid event of specified type and return the result
5293 // of ProcessEvent().
5295 bool wxGrid::SendEvent( const wxEventType type
,
5298 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5300 int rowOrCol
= (row
== -1 ? col
: row
);
5302 wxGridSizeEvent
gridEvt( GetId(),
5307 return GetEventHandler()->ProcessEvent(gridEvt
);
5311 wxGridEvent
gridEvt( GetId(),
5316 return GetEventHandler()->ProcessEvent(gridEvt
);
5321 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5323 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5327 // This is just here to make sure that CalcDimensions gets called when
5328 // the grid view is resized... then the size event is skipped to allow
5329 // the box sizers to handle everything
5331 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5338 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5340 if ( m_inOnKeyDown
)
5342 // shouldn't be here - we are going round in circles...
5344 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5347 m_inOnKeyDown
= TRUE
;
5349 // propagate the event up and see if it gets processed
5351 wxWindow
*parent
= GetParent();
5352 wxKeyEvent
keyEvt( event
);
5353 keyEvt
.SetEventObject( parent
);
5355 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5358 // try local handlers
5360 switch ( event
.KeyCode() )
5363 if ( event
.ControlDown() )
5365 MoveCursorUpBlock( event
.ShiftDown() );
5369 MoveCursorUp( event
.ShiftDown() );
5374 if ( event
.ControlDown() )
5376 MoveCursorDownBlock( event
.ShiftDown() );
5380 MoveCursorDown( event
.ShiftDown() );
5385 if ( event
.ControlDown() )
5387 MoveCursorLeftBlock( event
.ShiftDown() );
5391 MoveCursorLeft( event
.ShiftDown() );
5396 if ( event
.ControlDown() )
5398 MoveCursorRightBlock( event
.ShiftDown() );
5402 MoveCursorRight( event
.ShiftDown() );
5407 if ( event
.ControlDown() )
5409 event
.Skip(); // to let the edit control have the return
5413 if ( GetGridCursorRow() < GetNumberRows()-1 )
5415 MoveCursorDown( event
.ShiftDown() );
5419 // at the bottom of a column
5420 HideCellEditControl();
5421 SaveEditControlValue();
5431 if (event
.ShiftDown())
5433 if ( GetGridCursorCol() > 0 )
5435 MoveCursorLeft( FALSE
);
5440 HideCellEditControl();
5441 SaveEditControlValue();
5446 if ( GetGridCursorCol() < GetNumberCols()-1 )
5448 MoveCursorRight( FALSE
);
5453 HideCellEditControl();
5454 SaveEditControlValue();
5460 if ( event
.ControlDown() )
5462 MakeCellVisible( 0, 0 );
5463 SetCurrentCell( 0, 0 );
5472 if ( event
.ControlDown() )
5474 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5475 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5492 if ( event
.ControlDown() )
5494 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5495 m_currentCellCoords
.GetCol(),
5496 event
.ControlDown(),
5502 if ( !IsEditable() )
5504 MoveCursorRight( FALSE
);
5507 // Otherwise fall through to default
5510 // is it possible to edit the current cell at all?
5511 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5513 // yes, now check whether the cells editor accepts the key
5514 int row
= m_currentCellCoords
.GetRow();
5515 int col
= m_currentCellCoords
.GetCol();
5516 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5517 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5519 // <F2> is special and will always start editing, for
5520 // other keys - ask the editor itself
5521 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5522 || editor
->IsAcceptedKey(event
) )
5524 EnableCellEditControl();
5525 editor
->StartingKey(event
);
5537 // let others process char events with modifiers or all
5538 // char events for readonly cells
5545 m_inOnKeyDown
= FALSE
;
5548 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5550 // try local handlers
5552 if ( event
.KeyCode() == WXK_SHIFT
)
5554 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5555 m_selectingBottomRight
!= wxGridNoCellCoords
)
5556 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5557 m_selectingTopLeft
.GetCol(),
5558 m_selectingBottomRight
.GetRow(),
5559 m_selectingBottomRight
.GetCol(),
5560 event
.ControlDown(),
5564 m_selectingTopLeft
= wxGridNoCellCoords
;
5565 m_selectingBottomRight
= wxGridNoCellCoords
;
5566 m_selectingKeyboard
= wxGridNoCellCoords
;
5570 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5574 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5576 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5578 // the event has been intercepted - do nothing
5582 wxClientDC
dc(m_gridWin
);
5585 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5587 HideCellEditControl();
5588 DisableCellEditControl();
5590 if ( IsVisible( m_currentCellCoords
) )
5593 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5594 if ( !m_gridLinesEnabled
)
5602 CalcCellsExposed( r
);
5604 // Otherwise refresh redraws the highlight!
5605 m_currentCellCoords
= coords
;
5607 DrawGridCellArea(dc
);
5608 DrawAllGridLines( dc
, r
);
5612 m_currentCellCoords
= coords
;
5614 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5615 DrawCellHighlight(dc
, attr
);
5621 // ------ functions to get/send data (see also public functions)
5624 bool wxGrid::GetModelValues()
5628 // all we need to do is repaint the grid
5630 m_gridWin
->Refresh();
5638 bool wxGrid::SetModelValues()
5644 for ( row
= 0; row
< m_numRows
; row
++ )
5646 for ( col
= 0; col
< m_numCols
; col
++ )
5648 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5660 // Note - this function only draws cells that are in the list of
5661 // exposed cells (usually set from the update region by
5662 // CalcExposedCells)
5664 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5666 if ( !m_numRows
|| !m_numCols
) return;
5669 size_t numCells
= m_cellsExposed
.GetCount();
5671 for ( i
= 0; i
< numCells
; i
++ )
5673 DrawCell( dc
, m_cellsExposed
[i
] );
5678 void wxGrid::DrawGridSpace( wxDC
& dc
)
5681 m_gridWin
->GetClientSize( &cw
, &ch
);
5684 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5686 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5687 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5689 if ( right
> rightCol
|| bottom
> bottomRow
)
5692 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5694 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5695 dc
.SetPen( *wxTRANSPARENT_PEN
);
5697 if ( right
> rightCol
)
5699 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5702 if ( bottom
> bottomRow
)
5704 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5710 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5712 int row
= coords
.GetRow();
5713 int col
= coords
.GetCol();
5715 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5718 // we draw the cell border ourselves
5719 #if !WXGRID_DRAW_LINES
5720 if ( m_gridLinesEnabled
)
5721 DrawCellBorder( dc
, coords
);
5724 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5726 bool isCurrent
= coords
== m_currentCellCoords
;
5728 wxRect rect
= CellToRect( row
, col
);
5730 // if the editor is shown, we should use it and not the renderer
5731 // Note: However, only if it is really _shown_, i.e. not hidden!
5732 if ( isCurrent
&& IsCellEditControlShown() )
5734 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5735 editor
->PaintBackground(rect
, attr
);
5740 // but all the rest is drawn by the cell renderer and hence may be
5742 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5743 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5750 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5752 int row
= m_currentCellCoords
.GetRow();
5753 int col
= m_currentCellCoords
.GetCol();
5755 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5758 wxRect rect
= CellToRect(row
, col
);
5760 // hmmm... what could we do here to show that the cell is disabled?
5761 // for now, I just draw a thinner border than for the other ones, but
5762 // it doesn't look really good
5763 dc
.SetPen(wxPen(m_cellHighlightColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5764 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5766 dc
.DrawRectangle(rect
);
5769 // VZ: my experiments with 3d borders...
5771 // how to properly set colours for arbitrary bg?
5772 wxCoord x1
= rect
.x
,
5774 x2
= rect
.x
+ rect
.width
-1,
5775 y2
= rect
.y
+ rect
.height
-1;
5777 dc
.SetPen(*wxWHITE_PEN
);
5778 dc
.DrawLine(x1
, y1
, x2
, y1
);
5779 dc
.DrawLine(x1
, y1
, x1
, y2
);
5781 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5782 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5784 dc
.SetPen(*wxBLACK_PEN
);
5785 dc
.DrawLine(x1
, y2
, x2
, y2
);
5786 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5791 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5793 int row
= coords
.GetRow();
5794 int col
= coords
.GetCol();
5795 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5798 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5800 // right hand border
5802 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5803 GetColRight(col
), GetRowBottom(row
) );
5807 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5808 GetColRight(col
), GetRowBottom(row
) );
5811 void wxGrid::DrawHighlight(wxDC
& dc
)
5813 // This if block was previously in wxGrid::OnPaint but that doesn't
5814 // seem to get called under wxGTK - MB
5816 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5817 m_numRows
&& m_numCols
)
5819 m_currentCellCoords
.Set(0, 0);
5822 if ( IsCellEditControlShown() )
5824 // don't show highlight when the edit control is shown
5828 // if the active cell was repainted, repaint its highlight too because it
5829 // might have been damaged by the grid lines
5830 size_t count
= m_cellsExposed
.GetCount();
5831 for ( size_t n
= 0; n
< count
; n
++ )
5833 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5835 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5836 DrawCellHighlight(dc
, attr
);
5844 // TODO: remove this ???
5845 // This is used to redraw all grid lines e.g. when the grid line colour
5848 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED_GTK(reg
) )
5850 if ( !m_gridLinesEnabled
||
5852 !m_numCols
) return;
5854 int top
, bottom
, left
, right
;
5856 #if 0 //#ifndef __WXGTK__
5860 m_gridWin
->GetClientSize(&cw
, &ch
);
5862 // virtual coords of visible area
5864 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5865 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5870 reg
.GetBox(x
, y
, w
, h
);
5871 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5872 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5876 m_gridWin
->GetClientSize(&cw
, &ch
);
5877 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5878 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5881 // avoid drawing grid lines past the last row and col
5883 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5884 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5886 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5888 // horizontal grid lines
5891 for ( i
= 0; i
< m_numRows
; i
++ )
5893 int bot
= GetRowBottom(i
) - 1;
5902 dc
.DrawLine( left
, bot
, right
, bot
);
5907 // vertical grid lines
5909 for ( i
= 0; i
< m_numCols
; i
++ )
5911 int colRight
= GetColRight(i
) - 1;
5912 if ( colRight
> right
)
5917 if ( colRight
>= left
)
5919 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5925 void wxGrid::DrawRowLabels( wxDC
& dc
)
5927 if ( !m_numRows
) return;
5930 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5932 for ( i
= 0; i
< numLabels
; i
++ )
5934 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5939 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5941 if ( GetRowHeight(row
) <= 0 )
5944 int rowTop
= GetRowTop(row
),
5945 rowBottom
= GetRowBottom(row
) - 1;
5947 dc
.SetPen( *wxBLACK_PEN
);
5948 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5949 m_rowLabelWidth
-1, rowBottom
);
5951 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5953 dc
.SetPen( *wxWHITE_PEN
);
5954 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5955 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5957 dc
.SetBackgroundMode( wxTRANSPARENT
);
5958 dc
.SetTextForeground( GetLabelTextColour() );
5959 dc
.SetFont( GetLabelFont() );
5962 GetRowLabelAlignment( &hAlign
, &vAlign
);
5966 rect
.SetY( GetRowTop(row
) + 2 );
5967 rect
.SetWidth( m_rowLabelWidth
- 4 );
5968 rect
.SetHeight( GetRowHeight(row
) - 4 );
5969 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5973 void wxGrid::DrawColLabels( wxDC
& dc
)
5975 if ( !m_numCols
) return;
5978 size_t numLabels
= m_colLabelsExposed
.GetCount();
5980 for ( i
= 0; i
< numLabels
; i
++ )
5982 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5987 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5989 if ( GetColWidth(col
) <= 0 )
5992 int colLeft
= GetColLeft(col
),
5993 colRight
= GetColRight(col
) - 1;
5995 dc
.SetPen( *wxBLACK_PEN
);
5996 dc
.DrawLine( colRight
, 0,
5997 colRight
, m_colLabelHeight
-1 );
5999 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6000 colRight
, m_colLabelHeight
-1 );
6002 dc
.SetPen( *wxWHITE_PEN
);
6003 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6004 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6006 dc
.SetBackgroundMode( wxTRANSPARENT
);
6007 dc
.SetTextForeground( GetLabelTextColour() );
6008 dc
.SetFont( GetLabelFont() );
6010 dc
.SetBackgroundMode( wxTRANSPARENT
);
6011 dc
.SetTextForeground( GetLabelTextColour() );
6012 dc
.SetFont( GetLabelFont() );
6015 GetColLabelAlignment( &hAlign
, &vAlign
);
6018 rect
.SetX( colLeft
+ 2 );
6020 rect
.SetWidth( GetColWidth(col
) - 4 );
6021 rect
.SetHeight( m_colLabelHeight
- 4 );
6022 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6026 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6027 const wxString
& value
,
6032 long textWidth
, textHeight
;
6033 long lineWidth
, lineHeight
;
6034 wxArrayString lines
;
6036 dc
.SetClippingRegion( rect
);
6037 StringToLines( value
, lines
);
6038 if ( lines
.GetCount() )
6040 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6041 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6044 switch ( horizAlign
)
6047 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6051 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6060 switch ( vertAlign
)
6063 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6067 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6076 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6078 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6083 dc
.DestroyClippingRegion();
6087 // Split multi line text up into an array of strings. Any existing
6088 // contents of the string array are preserved.
6090 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6094 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6095 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6097 while ( startPos
< (int)tVal
.Length() )
6099 pos
= tVal
.Mid(startPos
).Find( eol
);
6104 else if ( pos
== 0 )
6106 lines
.Add( wxEmptyString
);
6110 lines
.Add( value
.Mid(startPos
, pos
) );
6114 if ( startPos
< (int)value
.Length() )
6116 lines
.Add( value
.Mid( startPos
) );
6121 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6122 wxArrayString
& lines
,
6123 long *width
, long *height
)
6130 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6132 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6133 w
= wxMax( w
, lineW
);
6142 // ------ Batch processing.
6144 void wxGrid::EndBatch()
6146 if ( m_batchCount
> 0 )
6149 if ( !m_batchCount
)
6152 m_rowLabelWin
->Refresh();
6153 m_colLabelWin
->Refresh();
6154 m_cornerLabelWin
->Refresh();
6155 m_gridWin
->Refresh();
6160 // Use this, rather than wxWindow::Refresh(), to force an immediate
6161 // repainting of the grid. Has no effect if you are already inside a
6162 // BeginBatch / EndBatch block.
6164 void wxGrid::ForceRefresh()
6172 // ------ Edit control functions
6176 void wxGrid::EnableEditing( bool edit
)
6178 // TODO: improve this ?
6180 if ( edit
!= m_editable
)
6182 if(!edit
) EnableCellEditControl(edit
);
6188 void wxGrid::EnableCellEditControl( bool enable
)
6193 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6194 SetCurrentCell( 0, 0 );
6196 if ( enable
!= m_cellEditCtrlEnabled
)
6198 // TODO allow the app to Veto() this event?
6199 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6203 // this should be checked by the caller!
6204 wxASSERT_MSG( CanEnableCellControl(),
6205 _T("can't enable editing for this cell!") );
6207 // do it before ShowCellEditControl()
6208 m_cellEditCtrlEnabled
= enable
;
6210 ShowCellEditControl();
6214 HideCellEditControl();
6215 SaveEditControlValue();
6217 // do it after HideCellEditControl()
6218 m_cellEditCtrlEnabled
= enable
;
6223 bool wxGrid::IsCurrentCellReadOnly() const
6226 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6227 bool readonly
= attr
->IsReadOnly();
6233 bool wxGrid::CanEnableCellControl() const
6235 return m_editable
&& !IsCurrentCellReadOnly();
6238 bool wxGrid::IsCellEditControlEnabled() const
6240 // the cell edit control might be disable for all cells or just for the
6241 // current one if it's read only
6242 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6245 bool wxGrid::IsCellEditControlShown() const
6247 bool isShown
= FALSE
;
6249 if ( m_cellEditCtrlEnabled
)
6251 int row
= m_currentCellCoords
.GetRow();
6252 int col
= m_currentCellCoords
.GetCol();
6253 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6254 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6259 if ( editor
->IsCreated() )
6261 isShown
= editor
->GetControl()->IsShown();
6271 void wxGrid::ShowCellEditControl()
6273 if ( IsCellEditControlEnabled() )
6275 if ( !IsVisible( m_currentCellCoords
) )
6277 m_cellEditCtrlEnabled
= false;
6282 wxRect rect
= CellToRect( m_currentCellCoords
);
6283 int row
= m_currentCellCoords
.GetRow();
6284 int col
= m_currentCellCoords
.GetCol();
6286 // convert to scrolled coords
6288 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6290 // done in PaintBackground()
6292 // erase the highlight and the cell contents because the editor
6293 // might not cover the entire cell
6294 wxClientDC
dc( m_gridWin
);
6296 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6297 dc
.SetPen(*wxTRANSPARENT_PEN
);
6298 dc
.DrawRectangle(rect
);
6301 // cell is shifted by one pixel
6305 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6306 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6307 if ( !editor
->IsCreated() )
6309 editor
->Create(m_gridWin
, -1,
6310 new wxGridCellEditorEvtHandler(this, editor
));
6313 editor
->Show( TRUE
, attr
);
6315 editor
->SetSize( rect
);
6317 editor
->BeginEdit(row
, col
, this);
6326 void wxGrid::HideCellEditControl()
6328 if ( IsCellEditControlEnabled() )
6330 int row
= m_currentCellCoords
.GetRow();
6331 int col
= m_currentCellCoords
.GetCol();
6333 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6334 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6335 editor
->Show( FALSE
);
6338 m_gridWin
->SetFocus();
6339 wxRect
rect( CellToRect( row
, col
) );
6340 m_gridWin
->Refresh( FALSE
, &rect
);
6345 void wxGrid::SaveEditControlValue()
6347 if ( IsCellEditControlEnabled() )
6349 int row
= m_currentCellCoords
.GetRow();
6350 int col
= m_currentCellCoords
.GetCol();
6352 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6353 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6354 bool changed
= editor
->EndEdit(row
, col
, this);
6361 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6362 m_currentCellCoords
.GetRow(),
6363 m_currentCellCoords
.GetCol() );
6370 // ------ Grid location functions
6371 // Note that all of these functions work with the logical coordinates of
6372 // grid cells and labels so you will need to convert from device
6373 // coordinates for mouse events etc.
6376 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6378 int row
= YToRow(y
);
6379 int col
= XToCol(x
);
6381 if ( row
== -1 || col
== -1 )
6383 coords
= wxGridNoCellCoords
;
6387 coords
.Set( row
, col
);
6392 int wxGrid::YToRow( int y
)
6396 for ( i
= 0; i
< m_numRows
; i
++ )
6398 if ( y
< GetRowBottom(i
) )
6406 int wxGrid::XToCol( int x
)
6410 for ( i
= 0; i
< m_numCols
; i
++ )
6412 if ( x
< GetColRight(i
) )
6420 // return the row number that that the y coord is near the edge of, or
6421 // -1 if not near an edge
6423 int wxGrid::YToEdgeOfRow( int y
)
6427 for ( i
= 0; i
< m_numRows
; i
++ )
6429 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6431 d
= abs( y
- GetRowBottom(i
) );
6432 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6441 // return the col number that that the x coord is near the edge of, or
6442 // -1 if not near an edge
6444 int wxGrid::XToEdgeOfCol( int x
)
6448 for ( i
= 0; i
< m_numCols
; i
++ )
6450 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6452 d
= abs( x
- GetColRight(i
) );
6453 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6462 wxRect
wxGrid::CellToRect( int row
, int col
)
6464 wxRect
rect( -1, -1, -1, -1 );
6466 if ( row
>= 0 && row
< m_numRows
&&
6467 col
>= 0 && col
< m_numCols
)
6469 rect
.x
= GetColLeft(col
);
6470 rect
.y
= GetRowTop(row
);
6471 rect
.width
= GetColWidth(col
);
6472 rect
.height
= GetRowHeight(row
);
6475 // if grid lines are enabled, then the area of the cell is a bit smaller
6476 if (m_gridLinesEnabled
) {
6484 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6486 // get the cell rectangle in logical coords
6488 wxRect
r( CellToRect( row
, col
) );
6490 // convert to device coords
6492 int left
, top
, right
, bottom
;
6493 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6494 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6496 // check against the client area of the grid window
6499 m_gridWin
->GetClientSize( &cw
, &ch
);
6501 if ( wholeCellVisible
)
6503 // is the cell wholly visible ?
6505 return ( left
>= 0 && right
<= cw
&&
6506 top
>= 0 && bottom
<= ch
);
6510 // is the cell partly visible ?
6512 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6513 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6518 // make the specified cell location visible by doing a minimal amount
6521 void wxGrid::MakeCellVisible( int row
, int col
)
6524 int xpos
= -1, ypos
= -1;
6526 if ( row
>= 0 && row
< m_numRows
&&
6527 col
>= 0 && col
< m_numCols
)
6529 // get the cell rectangle in logical coords
6531 wxRect
r( CellToRect( row
, col
) );
6533 // convert to device coords
6535 int left
, top
, right
, bottom
;
6536 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6537 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6540 m_gridWin
->GetClientSize( &cw
, &ch
);
6546 else if ( bottom
> ch
)
6548 int h
= r
.GetHeight();
6550 for ( i
= row
-1; i
>= 0; i
-- )
6552 int rowHeight
= GetRowHeight(i
);
6553 if ( h
+ rowHeight
> ch
)
6560 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6561 // have rounding errors (this is important, because if we do, we
6562 // might not scroll at all and some cells won't be redrawn)
6563 ypos
+= GRID_SCROLL_LINE
/ 2;
6570 else if ( right
> cw
)
6572 int w
= r
.GetWidth();
6574 for ( i
= col
-1; i
>= 0; i
-- )
6576 int colWidth
= GetColWidth(i
);
6577 if ( w
+ colWidth
> cw
)
6584 // see comment for ypos above
6585 xpos
+= GRID_SCROLL_LINE
/ 2;
6588 if ( xpos
!= -1 || ypos
!= -1 )
6590 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6591 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6592 Scroll( xpos
, ypos
);
6600 // ------ Grid cursor movement functions
6603 bool wxGrid::MoveCursorUp( bool expandSelection
)
6605 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6606 m_currentCellCoords
.GetRow() >= 0 )
6608 if ( expandSelection
)
6610 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6611 m_selectingKeyboard
= m_currentCellCoords
;
6612 if ( m_selectingKeyboard
.GetRow() > 0 )
6614 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6615 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6616 m_selectingKeyboard
.GetCol() );
6617 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6620 else if ( m_currentCellCoords
.GetRow() > 0 )
6623 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6624 m_currentCellCoords
.GetCol() );
6625 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6626 m_currentCellCoords
.GetCol() );
6637 bool wxGrid::MoveCursorDown( bool expandSelection
)
6639 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6640 m_currentCellCoords
.GetRow() < m_numRows
)
6642 if ( expandSelection
)
6644 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6645 m_selectingKeyboard
= m_currentCellCoords
;
6646 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6648 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6649 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6650 m_selectingKeyboard
.GetCol() );
6651 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6654 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6657 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6658 m_currentCellCoords
.GetCol() );
6659 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6660 m_currentCellCoords
.GetCol() );
6671 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6673 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6674 m_currentCellCoords
.GetCol() >= 0 )
6676 if ( expandSelection
)
6678 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6679 m_selectingKeyboard
= m_currentCellCoords
;
6680 if ( m_selectingKeyboard
.GetCol() > 0 )
6682 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6683 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6684 m_selectingKeyboard
.GetCol() );
6685 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6688 else if ( m_currentCellCoords
.GetCol() > 0 )
6691 MakeCellVisible( m_currentCellCoords
.GetRow(),
6692 m_currentCellCoords
.GetCol() - 1 );
6693 SetCurrentCell( m_currentCellCoords
.GetRow(),
6694 m_currentCellCoords
.GetCol() - 1 );
6705 bool wxGrid::MoveCursorRight( bool expandSelection
)
6707 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6708 m_currentCellCoords
.GetCol() < m_numCols
)
6710 if ( expandSelection
)
6712 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6713 m_selectingKeyboard
= m_currentCellCoords
;
6714 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6716 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6717 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6718 m_selectingKeyboard
.GetCol() );
6719 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6722 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6725 MakeCellVisible( m_currentCellCoords
.GetRow(),
6726 m_currentCellCoords
.GetCol() + 1 );
6727 SetCurrentCell( m_currentCellCoords
.GetRow(),
6728 m_currentCellCoords
.GetCol() + 1 );
6739 bool wxGrid::MovePageUp()
6741 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6743 int row
= m_currentCellCoords
.GetRow();
6747 m_gridWin
->GetClientSize( &cw
, &ch
);
6749 int y
= GetRowTop(row
);
6750 int newRow
= YToRow( y
- ch
+ 1 );
6755 else if ( newRow
== row
)
6760 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6761 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6769 bool wxGrid::MovePageDown()
6771 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6773 int row
= m_currentCellCoords
.GetRow();
6774 if ( row
< m_numRows
)
6777 m_gridWin
->GetClientSize( &cw
, &ch
);
6779 int y
= GetRowTop(row
);
6780 int newRow
= YToRow( y
+ ch
);
6783 newRow
= m_numRows
- 1;
6785 else if ( newRow
== row
)
6790 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6791 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6799 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6802 m_currentCellCoords
!= wxGridNoCellCoords
&&
6803 m_currentCellCoords
.GetRow() > 0 )
6805 int row
= m_currentCellCoords
.GetRow();
6806 int col
= m_currentCellCoords
.GetCol();
6808 if ( m_table
->IsEmptyCell(row
, col
) )
6810 // starting in an empty cell: find the next block of
6816 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6819 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6821 // starting at the top of a block: find the next block
6827 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6832 // starting within a block: find the top of the block
6837 if ( m_table
->IsEmptyCell(row
, col
) )
6845 MakeCellVisible( row
, col
);
6846 if ( expandSelection
)
6848 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6849 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6854 SetCurrentCell( row
, col
);
6862 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
6865 m_currentCellCoords
!= wxGridNoCellCoords
&&
6866 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6868 int row
= m_currentCellCoords
.GetRow();
6869 int col
= m_currentCellCoords
.GetCol();
6871 if ( m_table
->IsEmptyCell(row
, col
) )
6873 // starting in an empty cell: find the next block of
6876 while ( row
< m_numRows
-1 )
6879 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6882 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6884 // starting at the bottom of a block: find the next block
6887 while ( row
< m_numRows
-1 )
6890 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6895 // starting within a block: find the bottom of the block
6897 while ( row
< m_numRows
-1 )
6900 if ( m_table
->IsEmptyCell(row
, col
) )
6908 MakeCellVisible( row
, col
);
6909 if ( expandSelection
)
6911 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6912 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6917 SetCurrentCell( row
, col
);
6926 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
6929 m_currentCellCoords
!= wxGridNoCellCoords
&&
6930 m_currentCellCoords
.GetCol() > 0 )
6932 int row
= m_currentCellCoords
.GetRow();
6933 int col
= m_currentCellCoords
.GetCol();
6935 if ( m_table
->IsEmptyCell(row
, col
) )
6937 // starting in an empty cell: find the next block of
6943 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6946 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6948 // starting at the left of a block: find the next block
6954 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6959 // starting within a block: find the left of the block
6964 if ( m_table
->IsEmptyCell(row
, col
) )
6972 MakeCellVisible( row
, col
);
6973 if ( expandSelection
)
6975 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6976 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
6981 SetCurrentCell( row
, col
);
6990 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
6993 m_currentCellCoords
!= wxGridNoCellCoords
&&
6994 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6996 int row
= m_currentCellCoords
.GetRow();
6997 int col
= m_currentCellCoords
.GetCol();
6999 if ( m_table
->IsEmptyCell(row
, col
) )
7001 // starting in an empty cell: find the next block of
7004 while ( col
< m_numCols
-1 )
7007 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7010 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7012 // starting at the right of a block: find the next block
7015 while ( col
< m_numCols
-1 )
7018 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7023 // starting within a block: find the right of the block
7025 while ( col
< m_numCols
-1 )
7028 if ( m_table
->IsEmptyCell(row
, col
) )
7036 MakeCellVisible( row
, col
);
7037 if ( expandSelection
)
7039 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7040 SelectBlock( m_currentCellCoords
, m_selectingKeyboard
);
7045 SetCurrentCell( row
, col
);
7057 // ------ Label values and formatting
7060 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7062 *horiz
= m_rowLabelHorizAlign
;
7063 *vert
= m_rowLabelVertAlign
;
7066 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7068 *horiz
= m_colLabelHorizAlign
;
7069 *vert
= m_colLabelVertAlign
;
7072 wxString
wxGrid::GetRowLabelValue( int row
)
7076 return m_table
->GetRowLabelValue( row
);
7086 wxString
wxGrid::GetColLabelValue( int col
)
7090 return m_table
->GetColLabelValue( col
);
7101 void wxGrid::SetRowLabelSize( int width
)
7103 width
= wxMax( width
, 0 );
7104 if ( width
!= m_rowLabelWidth
)
7108 m_rowLabelWin
->Show( FALSE
);
7109 m_cornerLabelWin
->Show( FALSE
);
7111 else if ( m_rowLabelWidth
== 0 )
7113 m_rowLabelWin
->Show( TRUE
);
7114 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7117 m_rowLabelWidth
= width
;
7124 void wxGrid::SetColLabelSize( int height
)
7126 height
= wxMax( height
, 0 );
7127 if ( height
!= m_colLabelHeight
)
7131 m_colLabelWin
->Show( FALSE
);
7132 m_cornerLabelWin
->Show( FALSE
);
7134 else if ( m_colLabelHeight
== 0 )
7136 m_colLabelWin
->Show( TRUE
);
7137 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7140 m_colLabelHeight
= height
;
7147 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7149 if ( m_labelBackgroundColour
!= colour
)
7151 m_labelBackgroundColour
= colour
;
7152 m_rowLabelWin
->SetBackgroundColour( colour
);
7153 m_colLabelWin
->SetBackgroundColour( colour
);
7154 m_cornerLabelWin
->SetBackgroundColour( colour
);
7156 if ( !GetBatchCount() )
7158 m_rowLabelWin
->Refresh();
7159 m_colLabelWin
->Refresh();
7160 m_cornerLabelWin
->Refresh();
7165 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7167 if ( m_labelTextColour
!= colour
)
7169 m_labelTextColour
= colour
;
7170 if ( !GetBatchCount() )
7172 m_rowLabelWin
->Refresh();
7173 m_colLabelWin
->Refresh();
7178 void wxGrid::SetLabelFont( const wxFont
& font
)
7181 if ( !GetBatchCount() )
7183 m_rowLabelWin
->Refresh();
7184 m_colLabelWin
->Refresh();
7188 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7190 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
7192 m_rowLabelHorizAlign
= horiz
;
7195 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
7197 m_rowLabelVertAlign
= vert
;
7200 if ( !GetBatchCount() )
7202 m_rowLabelWin
->Refresh();
7206 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7208 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
7210 m_colLabelHorizAlign
= horiz
;
7213 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
7215 m_colLabelVertAlign
= vert
;
7218 if ( !GetBatchCount() )
7220 m_colLabelWin
->Refresh();
7224 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7228 m_table
->SetRowLabelValue( row
, s
);
7229 if ( !GetBatchCount() )
7231 wxRect rect
= CellToRect( row
, 0);
7232 if ( rect
.height
> 0 )
7234 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7236 rect
.width
= m_rowLabelWidth
;
7237 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7243 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7247 m_table
->SetColLabelValue( col
, s
);
7248 if ( !GetBatchCount() )
7250 wxRect rect
= CellToRect( 0, col
);
7251 if ( rect
.width
> 0 )
7253 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7255 rect
.height
= m_colLabelHeight
;
7256 m_colLabelWin
->Refresh( TRUE
, &rect
);
7262 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7264 if ( m_gridLineColour
!= colour
)
7266 m_gridLineColour
= colour
;
7268 wxClientDC
dc( m_gridWin
);
7270 DrawAllGridLines( dc
, wxRegion() );
7275 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7277 if ( m_cellHighlightColour
!= colour
)
7279 m_cellHighlightColour
= colour
;
7281 wxClientDC
dc( m_gridWin
);
7283 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7284 DrawCellHighlight(dc
, attr
);
7289 void wxGrid::EnableGridLines( bool enable
)
7291 if ( enable
!= m_gridLinesEnabled
)
7293 m_gridLinesEnabled
= enable
;
7295 if ( !GetBatchCount() )
7299 wxClientDC
dc( m_gridWin
);
7301 DrawAllGridLines( dc
, wxRegion() );
7305 m_gridWin
->Refresh();
7312 int wxGrid::GetDefaultRowSize()
7314 return m_defaultRowHeight
;
7317 int wxGrid::GetRowSize( int row
)
7319 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7321 return GetRowHeight(row
);
7324 int wxGrid::GetDefaultColSize()
7326 return m_defaultColWidth
;
7329 int wxGrid::GetColSize( int col
)
7331 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7333 return GetColWidth(col
);
7336 // ============================================================================
7337 // access to the grid attributes: each of them has a default value in the grid
7338 // itself and may be overidden on a per-cell basis
7339 // ============================================================================
7341 // ----------------------------------------------------------------------------
7342 // setting default attributes
7343 // ----------------------------------------------------------------------------
7345 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7347 m_defaultCellAttr
->SetBackgroundColour(col
);
7349 m_gridWin
->SetBackgroundColour(col
);
7353 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7355 m_defaultCellAttr
->SetTextColour(col
);
7358 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7360 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7363 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7365 m_defaultCellAttr
->SetFont(font
);
7368 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7370 m_defaultCellAttr
->SetRenderer(renderer
);
7373 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7375 m_defaultCellAttr
->SetEditor(editor
);
7378 // ----------------------------------------------------------------------------
7379 // access to the default attrbiutes
7380 // ----------------------------------------------------------------------------
7382 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7384 return m_defaultCellAttr
->GetBackgroundColour();
7387 wxColour
wxGrid::GetDefaultCellTextColour()
7389 return m_defaultCellAttr
->GetTextColour();
7392 wxFont
wxGrid::GetDefaultCellFont()
7394 return m_defaultCellAttr
->GetFont();
7397 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7399 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7402 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7404 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7407 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7409 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7412 // ----------------------------------------------------------------------------
7413 // access to cell attributes
7414 // ----------------------------------------------------------------------------
7416 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7418 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7419 wxColour colour
= attr
->GetBackgroundColour();
7424 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7426 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7427 wxColour colour
= attr
->GetTextColour();
7432 wxFont
wxGrid::GetCellFont( int row
, int col
)
7434 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7435 wxFont font
= attr
->GetFont();
7440 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7442 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7443 attr
->GetAlignment(horiz
, vert
);
7447 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7449 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7450 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7456 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7458 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7459 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7465 bool wxGrid::IsReadOnly(int row
, int col
) const
7467 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7468 bool isReadOnly
= attr
->IsReadOnly();
7473 // ----------------------------------------------------------------------------
7474 // attribute support: cache, automatic provider creation, ...
7475 // ----------------------------------------------------------------------------
7477 bool wxGrid::CanHaveAttributes()
7484 return m_table
->CanHaveAttributes();
7487 void wxGrid::ClearAttrCache()
7489 if ( m_attrCache
.row
!= -1 )
7491 wxSafeDecRef(m_attrCache
.attr
);
7492 m_attrCache
.row
= -1;
7496 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7498 wxGrid
*self
= (wxGrid
*)this; // const_cast
7500 self
->ClearAttrCache();
7501 self
->m_attrCache
.row
= row
;
7502 self
->m_attrCache
.col
= col
;
7503 self
->m_attrCache
.attr
= attr
;
7507 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7509 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7511 *attr
= m_attrCache
.attr
;
7512 wxSafeIncRef(m_attrCache
.attr
);
7514 #ifdef DEBUG_ATTR_CACHE
7515 gs_nAttrCacheHits
++;
7522 #ifdef DEBUG_ATTR_CACHE
7523 gs_nAttrCacheMisses
++;
7529 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7531 wxGridCellAttr
*attr
;
7532 if ( !LookupAttr(row
, col
, &attr
) )
7534 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7535 CacheAttr(row
, col
, attr
);
7539 attr
->SetDefAttr(m_defaultCellAttr
);
7543 attr
= m_defaultCellAttr
;
7550 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7552 wxGridCellAttr
*attr
;
7553 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7555 wxASSERT_MSG( m_table
,
7556 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7558 attr
= m_table
->GetAttr(row
, col
);
7561 attr
= new wxGridCellAttr
;
7563 // artificially inc the ref count to match DecRef() in caller
7566 m_table
->SetAttr(attr
, row
, col
);
7569 CacheAttr(row
, col
, attr
);
7571 attr
->SetDefAttr(m_defaultCellAttr
);
7575 // ----------------------------------------------------------------------------
7576 // setting column attributes (wrappers around SetColAttr)
7577 // ----------------------------------------------------------------------------
7579 void wxGrid::SetColFormatBool(int col
)
7581 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7584 void wxGrid::SetColFormatNumber(int col
)
7586 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7589 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7591 wxString typeName
= wxGRID_VALUE_FLOAT
;
7592 if ( (width
!= -1) || (precision
!= -1) )
7594 typeName
<< _T(':') << width
<< _T(',') << precision
;
7597 SetColFormatCustom(col
, typeName
);
7600 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7602 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7603 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7604 attr
->SetRenderer(renderer
);
7606 SetColAttr(col
, attr
);
7609 // ----------------------------------------------------------------------------
7610 // setting cell attributes: this is forwarded to the table
7611 // ----------------------------------------------------------------------------
7613 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7615 if ( CanHaveAttributes() )
7617 m_table
->SetRowAttr(attr
, row
);
7625 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7627 if ( CanHaveAttributes() )
7629 m_table
->SetColAttr(attr
, col
);
7637 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7639 if ( CanHaveAttributes() )
7641 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7642 attr
->SetBackgroundColour(colour
);
7647 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7649 if ( CanHaveAttributes() )
7651 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7652 attr
->SetTextColour(colour
);
7657 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7659 if ( CanHaveAttributes() )
7661 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7662 attr
->SetFont(font
);
7667 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7669 if ( CanHaveAttributes() )
7671 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7672 attr
->SetAlignment(horiz
, vert
);
7677 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7679 if ( CanHaveAttributes() )
7681 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7682 attr
->SetRenderer(renderer
);
7687 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7689 if ( CanHaveAttributes() )
7691 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7692 attr
->SetEditor(editor
);
7697 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7699 if ( CanHaveAttributes() )
7701 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7702 attr
->SetReadOnly(isReadOnly
);
7707 // ----------------------------------------------------------------------------
7708 // Data type registration
7709 // ----------------------------------------------------------------------------
7711 void wxGrid::RegisterDataType(const wxString
& typeName
,
7712 wxGridCellRenderer
* renderer
,
7713 wxGridCellEditor
* editor
)
7715 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7719 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7721 wxString typeName
= m_table
->GetTypeName(row
, col
);
7722 return GetDefaultEditorForType(typeName
);
7725 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7727 wxString typeName
= m_table
->GetTypeName(row
, col
);
7728 return GetDefaultRendererForType(typeName
);
7732 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7734 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7735 if ( index
== wxNOT_FOUND
)
7737 wxFAIL_MSG(wxT("Unknown data type name"));
7742 return m_typeRegistry
->GetEditor(index
);
7746 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7748 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7749 if ( index
== wxNOT_FOUND
)
7751 wxFAIL_MSG(wxT("Unknown data type name"));
7756 return m_typeRegistry
->GetRenderer(index
);
7760 // ----------------------------------------------------------------------------
7762 // ----------------------------------------------------------------------------
7764 void wxGrid::EnableDragRowSize( bool enable
)
7766 m_canDragRowSize
= enable
;
7770 void wxGrid::EnableDragColSize( bool enable
)
7772 m_canDragColSize
= enable
;
7775 void wxGrid::EnableDragGridSize( bool enable
)
7777 m_canDragGridSize
= enable
;
7781 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7783 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7785 if ( resizeExistingRows
)
7793 void wxGrid::SetRowSize( int row
, int height
)
7795 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7797 if ( m_rowHeights
.IsEmpty() )
7799 // need to really create the array
7803 int h
= wxMax( 0, height
);
7804 int diff
= h
- m_rowHeights
[row
];
7806 m_rowHeights
[row
] = h
;
7808 for ( i
= row
; i
< m_numRows
; i
++ )
7810 m_rowBottoms
[i
] += diff
;
7815 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7817 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7819 if ( resizeExistingCols
)
7827 void wxGrid::SetColSize( int col
, int width
)
7829 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7831 // should we check that it's bigger than GetColMinimalWidth(col) here?
7833 if ( m_colWidths
.IsEmpty() )
7835 // need to really create the array
7839 int w
= wxMax( 0, width
);
7840 int diff
= w
- m_colWidths
[col
];
7841 m_colWidths
[col
] = w
;
7844 for ( i
= col
; i
< m_numCols
; i
++ )
7846 m_colRights
[i
] += diff
;
7852 void wxGrid::SetColMinimalWidth( int col
, int width
)
7854 m_colMinWidths
.Put(col
, width
);
7857 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7859 m_rowMinHeights
.Put(row
, width
);
7862 int wxGrid::GetColMinimalWidth(int col
) const
7864 long value
= m_colMinWidths
.Get(col
);
7865 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
7868 int wxGrid::GetRowMinimalHeight(int row
) const
7870 long value
= m_rowMinHeights
.Get(row
);
7871 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
7874 // ----------------------------------------------------------------------------
7876 // ----------------------------------------------------------------------------
7878 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
7880 wxClientDC
dc(m_gridWin
);
7882 // init both of them to avoid compiler warnings, even if weo nly need one
7890 wxCoord extent
, extentMax
= 0;
7891 int max
= column
? m_numRows
: m_numCols
;
7892 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7899 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7900 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7903 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7904 extent
= column
? size
.x
: size
.y
;
7905 if ( extent
> extentMax
)
7916 // now also compare with the column label extent
7918 dc
.SetFont( GetLabelFont() );
7921 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
7923 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
7925 extent
= column
? w
: h
;
7926 if ( extent
> extentMax
)
7933 // empty column - give default extent (notice that if extentMax is less
7934 // than default extent but != 0, it's ok)
7935 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7941 // leave some space around text
7951 SetColSize(col
, extentMax
);
7953 SetRowSize(row
, extentMax
);
7958 SetColMinimalWidth(col
, extentMax
);
7960 SetRowMinimalHeight(row
, extentMax
);
7964 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7966 int width
= m_rowLabelWidth
;
7968 for ( int col
= 0; col
< m_numCols
; col
++ )
7972 AutoSizeColumn(col
, setAsMin
);
7975 width
+= GetColWidth(col
);
7981 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7983 int height
= m_colLabelHeight
;
7985 for ( int row
= 0; row
< m_numRows
; row
++ )
7989 AutoSizeRow(row
, setAsMin
);
7992 height
+= GetRowHeight(row
);
7998 void wxGrid::AutoSize()
8001 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8004 wxSize
wxGrid::DoGetBestSize() const
8006 // don't set sizes, only calculate them
8007 wxGrid
*self
= (wxGrid
*)this; // const_cast
8009 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8010 self
->SetOrCalcRowSizes(TRUE
));
8019 wxPen
& wxGrid::GetDividerPen() const
8024 // ----------------------------------------------------------------------------
8025 // cell value accessor functions
8026 // ----------------------------------------------------------------------------
8028 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8032 m_table
->SetValue( row
, col
, s
);
8033 if ( !GetBatchCount() )
8035 wxClientDC
dc( m_gridWin
);
8037 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8040 if ( m_currentCellCoords
.GetRow() == row
&&
8041 m_currentCellCoords
.GetCol() == col
&&
8042 IsCellEditControlShown())
8043 // Note: If we are using IsCellEditControlEnabled,
8044 // this interacts badly with calling SetCellValue from
8045 // an EVT_GRID_CELL_CHANGE handler.
8047 HideCellEditControl();
8048 ShowCellEditControl(); // will reread data from table
8055 // ------ Block, row and col selection
8058 void wxGrid::SelectRow( int row
, bool addToSelected
)
8060 if ( IsSelection() && !addToSelected
)
8063 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8067 void wxGrid::SelectCol( int col
, bool addToSelected
)
8069 if ( IsSelection() && !addToSelected
)
8072 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8076 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
8079 wxGridCellCoords updateTopLeft
, updateBottomRight
;
8081 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8084 rightCol
= GetNumberCols() - 1;
8086 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8089 bottomRow
= GetNumberRows() - 1;
8091 if ( topRow
> bottomRow
)
8098 if ( leftCol
> rightCol
)
8105 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
8106 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
8108 if ( m_selectingTopLeft
!= updateTopLeft
||
8109 m_selectingBottomRight
!= updateBottomRight
)
8111 // Compute two optimal update rectangles:
8112 // Either one rectangle is a real subset of the
8113 // other, or they are (almost) disjoint!
8115 bool need_refresh
[4];
8119 need_refresh
[3] = FALSE
;
8122 // Store intermediate values
8123 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
8124 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
8125 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
8126 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
8128 // Determine the outer/inner coordinates.
8129 if (oldLeft
> leftCol
)
8135 if (oldTop
> topRow
)
8141 if (oldRight
< rightCol
)
8144 oldRight
= rightCol
;
8147 if (oldBottom
< bottomRow
)
8150 oldBottom
= bottomRow
;
8154 // Now, either the stuff marked old is the outer
8155 // rectangle or we don't have a situation where one
8156 // is contained in the other.
8158 if ( oldLeft
< leftCol
)
8160 need_refresh
[0] = TRUE
;
8161 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
8163 wxGridCellCoords ( oldBottom
,
8167 if ( oldTop
< topRow
)
8169 need_refresh
[1] = TRUE
;
8170 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
8172 wxGridCellCoords ( topRow
- 1,
8176 if ( oldRight
> rightCol
)
8178 need_refresh
[2] = TRUE
;
8179 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
8181 wxGridCellCoords ( oldBottom
,
8185 if ( oldBottom
> bottomRow
)
8187 need_refresh
[3] = TRUE
;
8188 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
8190 wxGridCellCoords ( oldBottom
,
8196 m_selectingTopLeft
= updateTopLeft
;
8197 m_selectingBottomRight
= updateBottomRight
;
8199 // various Refresh() calls
8200 for (i
= 0; i
< 4; i
++ )
8201 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
8202 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
8205 // never generate an event as it will be generated from
8206 // wxGridSelection::SelectBlock!
8209 void wxGrid::SelectAll()
8211 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8214 bool wxGrid::IsSelection()
8216 return ( m_selection
->IsSelection() ||
8217 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8218 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8221 bool wxGrid::IsInSelection( int row
, int col
)
8223 return ( m_selection
->IsInSelection( row
, col
) ||
8224 ( row
>= m_selectingTopLeft
.GetRow() &&
8225 col
>= m_selectingTopLeft
.GetCol() &&
8226 row
<= m_selectingBottomRight
.GetRow() &&
8227 col
<= m_selectingBottomRight
.GetCol() ) );
8230 void wxGrid::ClearSelection()
8232 m_selectingTopLeft
= wxGridNoCellCoords
;
8233 m_selectingBottomRight
= wxGridNoCellCoords
;
8234 m_selection
->ClearSelection();
8238 // This function returns the rectangle that encloses the given block
8239 // in device coords clipped to the client size of the grid window.
8241 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8242 const wxGridCellCoords
&bottomRight
)
8244 wxRect
rect( wxGridNoCellRect
);
8247 cellRect
= CellToRect( topLeft
);
8248 if ( cellRect
!= wxGridNoCellRect
)
8254 rect
= wxRect( 0, 0, 0, 0 );
8257 cellRect
= CellToRect( bottomRight
);
8258 if ( cellRect
!= wxGridNoCellRect
)
8264 return wxGridNoCellRect
;
8267 // convert to scrolled coords
8269 int left
, top
, right
, bottom
;
8270 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8271 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8274 m_gridWin
->GetClientSize( &cw
, &ch
);
8276 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8277 return wxRect( 0, 0, 0, 0);
8279 rect
.SetLeft( wxMax(0, left
) );
8280 rect
.SetTop( wxMax(0, top
) );
8281 rect
.SetRight( wxMin(cw
, right
) );
8282 rect
.SetBottom( wxMin(ch
, bottom
) );
8290 // ------ Grid event classes
8293 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8295 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8296 int row
, int col
, int x
, int y
, bool sel
,
8297 bool control
, bool shift
, bool alt
, bool meta
)
8298 : wxNotifyEvent( type
, id
)
8305 m_control
= control
;
8310 SetEventObject(obj
);
8314 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8316 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8317 int rowOrCol
, int x
, int y
,
8318 bool control
, bool shift
, bool alt
, bool meta
)
8319 : wxNotifyEvent( type
, id
)
8321 m_rowOrCol
= rowOrCol
;
8324 m_control
= control
;
8329 SetEventObject(obj
);
8333 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8335 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8336 const wxGridCellCoords
& topLeft
,
8337 const wxGridCellCoords
& bottomRight
,
8338 bool sel
, bool control
,
8339 bool shift
, bool alt
, bool meta
)
8340 : wxNotifyEvent( type
, id
)
8342 m_topLeft
= topLeft
;
8343 m_bottomRight
= bottomRight
;
8345 m_control
= control
;
8350 SetEventObject(obj
);
8354 #endif // ifndef wxUSE_NEW_GRID