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;
542 // MB: treat MSW separately here otherwise the caret doesn't show
543 // when the editor is in the first row.
544 #if defined(__WXMSW__)
547 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
550 #if defined(__WXMOTIF__)
554 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
555 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
556 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
557 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
560 wxGridCellEditor::SetSize(rect
);
563 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
565 wxASSERT_MSG(m_control
,
566 wxT("The wxGridCellEditor must be Created first!"));
568 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
570 DoBeginEdit(m_startValue
);
573 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
575 Text()->SetValue(startValue
);
576 Text()->SetInsertionPointEnd();
580 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
583 wxASSERT_MSG(m_control
,
584 wxT("The wxGridCellEditor must be Created first!"));
586 bool changed
= FALSE
;
587 wxString value
= Text()->GetValue();
588 if (value
!= m_startValue
)
592 grid
->GetTable()->SetValue(row
, col
, value
);
594 m_startValue
= wxEmptyString
;
595 Text()->SetValue(m_startValue
);
601 void wxGridCellTextEditor::Reset()
603 wxASSERT_MSG(m_control
,
604 wxT("The wxGridCellEditor must be Created first!"));
606 DoReset(m_startValue
);
609 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
611 Text()->SetValue(startValue
);
612 Text()->SetInsertionPointEnd();
615 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
617 if ( wxGridCellEditor::IsAcceptedKey(event
) )
619 int keycode
= event
.GetKeyCode();
633 case WXK_NUMPAD_MULTIPLY
:
637 case WXK_NUMPAD_SUBTRACT
:
639 case WXK_NUMPAD_DECIMAL
:
641 case WXK_NUMPAD_DIVIDE
:
645 // accept 8 bit chars too if isprint() agrees
646 if ( (keycode
< 255) && (isprint(keycode
)) )
654 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
656 // we don't check for !HasModifiers() because IsAcceptedKey() did it
658 // insert the key in the control
660 int keycode
= event
.GetKeyCode();
673 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
677 case WXK_NUMPAD_MULTIPLY
:
687 case WXK_NUMPAD_SUBTRACT
:
692 case WXK_NUMPAD_DECIMAL
:
697 case WXK_NUMPAD_DIVIDE
:
702 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
704 // FIXME this is not going to work for non letters...
705 if ( !event
.ShiftDown() )
707 keycode
= tolower(keycode
);
710 ch
= (wxChar
)keycode
;
720 Text()->AppendText(ch
);
728 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
729 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
731 #if defined(__WXMOTIF__) || defined(__WXGTK__)
732 // wxMotif needs a little extra help...
733 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
734 wxString
s( Text()->GetValue() );
735 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
737 Text()->SetInsertionPoint( pos
);
739 // the other ports can handle a Return key press
745 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
755 if ( !params
.ToLong(&tmp
) )
757 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
761 m_maxChars
= (size_t)tmp
;
766 // ----------------------------------------------------------------------------
767 // wxGridCellNumberEditor
768 // ----------------------------------------------------------------------------
770 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
776 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
778 wxEvtHandler
* evtHandler
)
782 // create a spin ctrl
783 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
784 wxDefaultPosition
, wxDefaultSize
,
788 wxGridCellEditor::Create(parent
, id
, evtHandler
);
792 // just a text control
793 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
796 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
797 #endif // wxUSE_VALIDATORS
801 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
803 // first get the value
804 wxGridTableBase
*table
= grid
->GetTable();
805 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
807 m_valueOld
= table
->GetValueAsLong(row
, col
);
811 wxString sValue
= table
->GetValue(row
, col
);
812 if (! sValue
.ToLong(&m_valueOld
))
814 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
821 Spin()->SetValue((int)m_valueOld
);
826 DoBeginEdit(GetString());
830 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
838 value
= Spin()->GetValue();
839 changed
= value
!= m_valueOld
;
843 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
848 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
849 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
851 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
857 void wxGridCellNumberEditor::Reset()
861 Spin()->SetValue((int)m_valueOld
);
865 DoReset(GetString());
869 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
871 if ( wxGridCellEditor::IsAcceptedKey(event
) )
873 int keycode
= event
.GetKeyCode();
889 case WXK_NUMPAD_SUBTRACT
:
895 if ( (keycode
< 128) && isdigit(keycode
) )
903 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
907 int keycode
= (int) event
.KeyCode();
908 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
910 wxGridCellTextEditor::StartingKey(event
);
920 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
931 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
935 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
939 // skip the error message below
944 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
948 // ----------------------------------------------------------------------------
949 // wxGridCellFloatEditor
950 // ----------------------------------------------------------------------------
952 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
955 m_precision
= precision
;
958 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
960 wxEvtHandler
* evtHandler
)
962 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
965 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
966 #endif // wxUSE_VALIDATORS
969 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
971 // first get the value
972 wxGridTableBase
*table
= grid
->GetTable();
973 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
975 m_valueOld
= table
->GetValueAsDouble(row
, col
);
979 wxString sValue
= table
->GetValue(row
, col
);
980 if (! sValue
.ToDouble(&m_valueOld
))
982 wxFAIL_MSG( _T("this cell doesn't have float value") );
987 DoBeginEdit(GetString());
990 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
994 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
996 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
997 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
999 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1009 void wxGridCellFloatEditor::Reset()
1011 DoReset(GetString());
1014 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1016 int keycode
= (int)event
.KeyCode();
1017 if ( isdigit(keycode
) ||
1018 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1020 wxGridCellTextEditor::StartingKey(event
);
1022 // skip Skip() below
1029 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1040 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1044 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1046 m_precision
= (int)tmp
;
1048 // skip the error message below
1053 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1057 wxString
wxGridCellFloatEditor::GetString() const
1060 if ( m_width
== -1 )
1062 // default width/precision
1065 else if ( m_precision
== -1 )
1067 // default precision
1068 fmt
.Printf(_T("%%%d.g"), m_width
);
1072 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1075 return wxString::Format(fmt
, m_valueOld
);
1078 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1080 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1082 int keycode
= event
.GetKeyCode();
1096 case WXK_NUMPAD_ADD
:
1098 case WXK_NUMPAD_SUBTRACT
:
1100 case WXK_NUMPAD_DECIMAL
:
1104 // additionally accept 'e' as in '1e+6'
1105 if ( (keycode
< 128) &&
1106 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1114 // ----------------------------------------------------------------------------
1115 // wxGridCellBoolEditor
1116 // ----------------------------------------------------------------------------
1118 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1120 wxEvtHandler
* evtHandler
)
1122 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1123 wxDefaultPosition
, wxDefaultSize
,
1126 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1129 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1131 bool resize
= FALSE
;
1132 wxSize size
= m_control
->GetSize();
1133 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1135 // check if the checkbox is not too big/small for this cell
1136 wxSize sizeBest
= m_control
->GetBestSize();
1137 if ( !(size
== sizeBest
) )
1139 // reset to default size if it had been made smaller
1145 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1147 // leave 1 pixel margin
1148 size
.x
= size
.y
= minSize
- 2;
1155 m_control
->SetSize(size
);
1158 // position it in the centre of the rectangle (TODO: support alignment?)
1160 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1161 // the checkbox without label still has some space to the right in wxGTK,
1162 // so shift it to the right
1164 #elif defined(__WXMSW__)
1165 // here too, but in other way
1170 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1173 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1175 m_control
->Show(show
);
1179 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1180 CBox()->SetBackgroundColour(colBg
);
1184 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1186 wxASSERT_MSG(m_control
,
1187 wxT("The wxGridCellEditor must be Created first!"));
1189 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1190 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1192 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
1193 CBox()->SetValue(m_startValue
);
1197 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1200 wxASSERT_MSG(m_control
,
1201 wxT("The wxGridCellEditor must be Created first!"));
1203 bool changed
= FALSE
;
1204 bool value
= CBox()->GetValue();
1205 if ( value
!= m_startValue
)
1210 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1211 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1213 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1219 void wxGridCellBoolEditor::Reset()
1221 wxASSERT_MSG(m_control
,
1222 wxT("The wxGridCellEditor must be Created first!"));
1224 CBox()->SetValue(m_startValue
);
1227 void wxGridCellBoolEditor::StartingClick()
1229 CBox()->SetValue(!CBox()->GetValue());
1232 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1234 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1236 int keycode
= event
.GetKeyCode();
1240 case WXK_NUMPAD_MULTIPLY
:
1242 case WXK_NUMPAD_ADD
:
1244 case WXK_NUMPAD_SUBTRACT
:
1255 // ----------------------------------------------------------------------------
1256 // wxGridCellChoiceEditor
1257 // ----------------------------------------------------------------------------
1259 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1260 const wxString choices
[],
1262 : m_allowOthers(allowOthers
)
1266 m_choices
.Alloc(count
);
1267 for ( size_t n
= 0; n
< count
; n
++ )
1269 m_choices
.Add(choices
[n
]);
1274 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1276 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1277 editor
->m_allowOthers
= m_allowOthers
;
1278 editor
->m_choices
= m_choices
;
1283 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1285 wxEvtHandler
* evtHandler
)
1287 size_t count
= m_choices
.GetCount();
1288 wxString
*choices
= new wxString
[count
];
1289 for ( size_t n
= 0; n
< count
; n
++ )
1291 choices
[n
] = m_choices
[n
];
1294 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1295 wxDefaultPosition
, wxDefaultSize
,
1297 m_allowOthers
? 0 : wxCB_READONLY
);
1301 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1304 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1305 wxGridCellAttr
* attr
)
1307 // as we fill the entire client area, don't do anything here to minimize
1310 // TODO: It doesn't actually fill the client area since the height of a
1311 // combo always defaults to the standard... Until someone has time to
1312 // figure out the right rectangle to paint, just do it the normal way...
1313 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1316 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1318 wxASSERT_MSG(m_control
,
1319 wxT("The wxGridCellEditor must be Created first!"));
1321 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1323 Combo()->SetValue(m_startValue
);
1324 size_t count
= m_choices
.GetCount();
1325 for (size_t i
=0; i
<count
; i
++)
1327 if (m_startValue
== m_choices
[i
])
1329 Combo()->SetSelection(i
);
1333 Combo()->SetInsertionPointEnd();
1334 Combo()->SetFocus();
1337 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1340 wxString value
= Combo()->GetValue();
1341 bool changed
= value
!= m_startValue
;
1344 grid
->GetTable()->SetValue(row
, col
, value
);
1346 m_startValue
= wxEmptyString
;
1347 Combo()->SetValue(m_startValue
);
1352 void wxGridCellChoiceEditor::Reset()
1354 Combo()->SetValue(m_startValue
);
1355 Combo()->SetInsertionPointEnd();
1358 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1368 wxStringTokenizer
tk(params
, _T(','));
1369 while ( tk
.HasMoreTokens() )
1371 m_choices
.Add(tk
.GetNextToken());
1375 // ----------------------------------------------------------------------------
1376 // wxGridCellEditorEvtHandler
1377 // ----------------------------------------------------------------------------
1379 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1381 switch ( event
.KeyCode() )
1385 m_grid
->DisableCellEditControl();
1389 event
.Skip( m_grid
->ProcessEvent( event
) );
1393 if (!m_grid
->ProcessEvent(event
))
1394 m_editor
->HandleReturn(event
);
1403 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1405 switch ( event
.KeyCode() )
1417 // ----------------------------------------------------------------------------
1418 // wxGridCellWorker is an (almost) empty common base class for
1419 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1420 // ----------------------------------------------------------------------------
1422 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1427 wxGridCellWorker::~wxGridCellWorker()
1431 // ============================================================================
1433 // ============================================================================
1435 // ----------------------------------------------------------------------------
1436 // wxGridCellRenderer
1437 // ----------------------------------------------------------------------------
1439 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1440 wxGridCellAttr
& attr
,
1443 int WXUNUSED(row
), int WXUNUSED(col
),
1446 dc
.SetBackgroundMode( wxSOLID
);
1450 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1454 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1457 dc
.SetPen( *wxTRANSPARENT_PEN
);
1458 dc
.DrawRectangle(rect
);
1461 // ----------------------------------------------------------------------------
1462 // wxGridCellStringRenderer
1463 // ----------------------------------------------------------------------------
1465 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1466 wxGridCellAttr
& attr
,
1470 dc
.SetBackgroundMode( wxTRANSPARENT
);
1472 // TODO some special colours for attr.IsReadOnly() case?
1476 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1477 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1481 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1482 dc
.SetTextForeground( attr
.GetTextColour() );
1485 dc
.SetFont( attr
.GetFont() );
1488 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1490 const wxString
& text
)
1492 wxCoord x
= 0, y
= 0, max_x
= 0;
1493 dc
.SetFont(attr
.GetFont());
1494 wxStringTokenizer
tk(text
, _T('\n'));
1495 while ( tk
.HasMoreTokens() )
1497 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1498 max_x
= wxMax(max_x
, x
);
1501 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1503 return wxSize(max_x
, y
);
1506 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1507 wxGridCellAttr
& attr
,
1511 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1514 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1515 wxGridCellAttr
& attr
,
1517 const wxRect
& rectCell
,
1521 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1523 // now we only have to draw the text
1524 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1527 attr
.GetAlignment(&hAlign
, &vAlign
);
1529 wxRect rect
= rectCell
;
1532 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1533 rect
, hAlign
, vAlign
);
1536 // ----------------------------------------------------------------------------
1537 // wxGridCellNumberRenderer
1538 // ----------------------------------------------------------------------------
1540 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1542 wxGridTableBase
*table
= grid
.GetTable();
1544 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1546 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1550 text
= table
->GetValue(row
, col
);
1556 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1557 wxGridCellAttr
& attr
,
1559 const wxRect
& rectCell
,
1563 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1565 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1567 // draw the text right aligned by default
1569 attr
.GetAlignment(&hAlign
, &vAlign
);
1570 hAlign
= wxALIGN_RIGHT
;
1572 wxRect rect
= rectCell
;
1575 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1578 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1579 wxGridCellAttr
& attr
,
1583 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1586 // ----------------------------------------------------------------------------
1587 // wxGridCellFloatRenderer
1588 // ----------------------------------------------------------------------------
1590 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1593 SetPrecision(precision
);
1596 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1598 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1599 renderer
->m_width
= m_width
;
1600 renderer
->m_precision
= m_precision
;
1601 renderer
->m_format
= m_format
;
1606 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1608 wxGridTableBase
*table
= grid
.GetTable();
1613 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1615 val
= table
->GetValueAsDouble(row
, col
);
1620 text
= table
->GetValue(row
, col
);
1621 hasDouble
= text
.ToDouble(&val
);
1628 if ( m_width
== -1 )
1630 // default width/precision
1631 m_format
= _T("%f");
1633 else if ( m_precision
== -1 )
1635 // default precision
1636 m_format
.Printf(_T("%%%d.f"), m_width
);
1640 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1644 text
.Printf(m_format
, val
);
1646 //else: text already contains the string
1651 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1652 wxGridCellAttr
& attr
,
1654 const wxRect
& rectCell
,
1658 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1660 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1662 // draw the text right aligned by default
1664 attr
.GetAlignment(&hAlign
, &vAlign
);
1665 hAlign
= wxALIGN_RIGHT
;
1667 wxRect rect
= rectCell
;
1670 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1673 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1674 wxGridCellAttr
& attr
,
1678 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1681 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1687 // reset to defaults
1693 wxString tmp
= params
.BeforeFirst(_T(','));
1697 if ( !tmp
.ToLong(&width
) )
1703 SetWidth((int)width
);
1705 tmp
= params
.AfterFirst(_T(','));
1709 if ( !tmp
.ToLong(&precision
) )
1715 SetPrecision((int)precision
);
1723 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1728 // ----------------------------------------------------------------------------
1729 // wxGridCellBoolRenderer
1730 // ----------------------------------------------------------------------------
1732 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1734 // FIXME these checkbox size calculations are really ugly...
1736 // between checkmark and box
1737 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1739 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1740 wxGridCellAttr
& WXUNUSED(attr
),
1745 // compute it only once (no locks for MT safeness in GUI thread...)
1746 if ( !ms_sizeCheckMark
.x
)
1748 // get checkbox size
1749 wxCoord checkSize
= 0;
1750 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1751 wxSize size
= checkbox
->GetBestSize();
1752 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1754 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1755 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1756 checkSize
-= size
.y
/ 2;
1761 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1764 return ms_sizeCheckMark
;
1767 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1768 wxGridCellAttr
& attr
,
1774 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1776 // draw a check mark in the centre (ignoring alignment - TODO)
1777 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1779 // don't draw outside the cell
1780 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1781 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1783 // and even leave (at least) 1 pixel margin
1784 size
.x
= size
.y
= minSize
- 2;
1787 // draw a border around checkmark
1789 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1790 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1791 rectBorder
.width
= size
.x
;
1792 rectBorder
.height
= size
.y
;
1795 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1796 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1798 value
= !!grid
.GetTable()->GetValue(row
, col
);
1802 wxRect rectMark
= rectBorder
;
1804 // MSW DrawCheckMark() is weird (and should probably be changed...)
1805 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1809 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1812 dc
.SetTextForeground(attr
.GetTextColour());
1813 dc
.DrawCheckMark(rectMark
);
1816 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1817 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1818 dc
.DrawRectangle(rectBorder
);
1821 // ----------------------------------------------------------------------------
1823 // ----------------------------------------------------------------------------
1825 wxGridCellAttr
*wxGridCellAttr::Clone() const
1827 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1828 if ( HasTextColour() )
1829 attr
->SetTextColour(GetTextColour());
1830 if ( HasBackgroundColour() )
1831 attr
->SetBackgroundColour(GetBackgroundColour());
1833 attr
->SetFont(GetFont());
1834 if ( HasAlignment() )
1835 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1839 attr
->SetRenderer(m_renderer
);
1840 m_renderer
->IncRef();
1844 attr
->SetEditor(m_editor
);
1849 attr
->SetReadOnly();
1851 attr
->SetDefAttr(m_defGridAttr
);
1856 const wxColour
& wxGridCellAttr::GetTextColour() const
1858 if (HasTextColour())
1862 else if (m_defGridAttr
!= this)
1864 return m_defGridAttr
->GetTextColour();
1868 wxFAIL_MSG(wxT("Missing default cell attribute"));
1869 return wxNullColour
;
1874 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1876 if (HasBackgroundColour())
1878 else if (m_defGridAttr
!= this)
1879 return m_defGridAttr
->GetBackgroundColour();
1882 wxFAIL_MSG(wxT("Missing default cell attribute"));
1883 return wxNullColour
;
1888 const wxFont
& wxGridCellAttr::GetFont() const
1892 else if (m_defGridAttr
!= this)
1893 return m_defGridAttr
->GetFont();
1896 wxFAIL_MSG(wxT("Missing default cell attribute"));
1902 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1906 if ( hAlign
) *hAlign
= m_hAlign
;
1907 if ( vAlign
) *vAlign
= m_vAlign
;
1909 else if (m_defGridAttr
!= this)
1910 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1913 wxFAIL_MSG(wxT("Missing default cell attribute"));
1918 // GetRenderer and GetEditor use a slightly different decision path about
1919 // which attribute to use. If a non-default attr object has one then it is
1920 // used, otherwise the default editor or renderer is fetched from the grid and
1921 // used. It should be the default for the data type of the cell. If it is
1922 // NULL (because the table has a type that the grid does not have in its
1923 // registry,) then the grid's default editor or renderer is used.
1925 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1927 wxGridCellRenderer
* renderer
= NULL
;
1929 if ( m_defGridAttr
!= this || grid
== NULL
)
1931 renderer
= m_renderer
; // use local attribute
1936 if ( !renderer
&& grid
) // get renderer for the data type
1938 // GetDefaultRendererForCell() will do IncRef() for us
1939 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1944 // if we still don't have one then use the grid default
1945 // (no need for IncRef() here neither)
1946 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1951 wxFAIL_MSG(wxT("Missing default cell attribute"));
1957 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1959 wxGridCellEditor
* editor
= NULL
;
1961 if ( m_defGridAttr
!= this || grid
== NULL
)
1963 editor
= m_editor
; // use local attribute
1968 if ( !editor
&& grid
) // get renderer for the data type
1969 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1972 // if we still don't have one then use the grid default
1973 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1977 wxFAIL_MSG(wxT("Missing default cell attribute"));
1983 // ----------------------------------------------------------------------------
1984 // wxGridCellAttrData
1985 // ----------------------------------------------------------------------------
1987 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1989 int n
= FindIndex(row
, col
);
1990 if ( n
== wxNOT_FOUND
)
1992 // add the attribute
1993 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1999 // change the attribute
2000 m_attrs
[(size_t)n
].attr
= attr
;
2004 // remove this attribute
2005 m_attrs
.RemoveAt((size_t)n
);
2010 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2012 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2014 int n
= FindIndex(row
, col
);
2015 if ( n
!= wxNOT_FOUND
)
2017 attr
= m_attrs
[(size_t)n
].attr
;
2024 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2026 size_t count
= m_attrs
.GetCount();
2027 for ( size_t n
= 0; n
< count
; n
++ )
2029 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2030 wxCoord row
= coords
.GetRow();
2031 if ((size_t)row
>= pos
)
2035 // If rows inserted, include row counter where necessary
2036 coords
.SetRow(row
+ numRows
);
2038 else if (numRows
< 0)
2040 // If rows deleted ...
2041 if ((size_t)row
>= pos
- numRows
)
2043 // ...either decrement row counter (if row still exists)...
2044 coords
.SetRow(row
+ numRows
);
2048 // ...or remove the attribute
2049 m_attrs
.RemoveAt((size_t)n
);
2057 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2059 size_t count
= m_attrs
.GetCount();
2060 for ( size_t n
= 0; n
< count
; n
++ )
2062 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2063 wxCoord col
= coords
.GetCol();
2064 if ( (size_t)col
>= pos
)
2068 // If rows inserted, include row counter where necessary
2069 coords
.SetCol(col
+ numCols
);
2071 else if (numCols
< 0)
2073 // If rows deleted ...
2074 if ((size_t)col
>= pos
- numCols
)
2076 // ...either decrement row counter (if row still exists)...
2077 coords
.SetCol(col
+ numCols
);
2081 // ...or remove the attribute
2082 m_attrs
.RemoveAt((size_t)n
);
2090 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2092 size_t count
= m_attrs
.GetCount();
2093 for ( size_t n
= 0; n
< count
; n
++ )
2095 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2096 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2105 // ----------------------------------------------------------------------------
2106 // wxGridRowOrColAttrData
2107 // ----------------------------------------------------------------------------
2109 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2111 size_t count
= m_attrs
.Count();
2112 for ( size_t n
= 0; n
< count
; n
++ )
2114 m_attrs
[n
]->DecRef();
2118 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2120 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2122 int n
= m_rowsOrCols
.Index(rowOrCol
);
2123 if ( n
!= wxNOT_FOUND
)
2125 attr
= m_attrs
[(size_t)n
];
2132 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2134 int i
= m_rowsOrCols
.Index(rowOrCol
);
2135 if ( i
== wxNOT_FOUND
)
2137 // add the attribute
2138 m_rowsOrCols
.Add(rowOrCol
);
2143 size_t n
= (size_t)i
;
2146 // change the attribute
2147 m_attrs
[n
]->DecRef();
2152 // remove this attribute
2153 m_attrs
[n
]->DecRef();
2154 m_rowsOrCols
.RemoveAt(n
);
2155 m_attrs
.RemoveAt(n
);
2160 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2162 size_t count
= m_attrs
.GetCount();
2163 for ( size_t n
= 0; n
< count
; n
++ )
2165 int & rowOrCol
= m_rowsOrCols
[n
];
2166 if ( (size_t)rowOrCol
>= pos
)
2168 if ( numRowsOrCols
> 0 )
2170 // If rows inserted, include row counter where necessary
2171 rowOrCol
+= numRowsOrCols
;
2173 else if ( numRowsOrCols
< 0)
2175 // If rows deleted, either decrement row counter (if row still exists)
2176 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2177 rowOrCol
+= numRowsOrCols
;
2180 m_rowsOrCols
.RemoveAt((size_t)n
);
2181 m_attrs
.RemoveAt((size_t)n
);
2189 // ----------------------------------------------------------------------------
2190 // wxGridCellAttrProvider
2191 // ----------------------------------------------------------------------------
2193 wxGridCellAttrProvider::wxGridCellAttrProvider()
2195 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2198 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2203 void wxGridCellAttrProvider::InitData()
2205 m_data
= new wxGridCellAttrProviderData
;
2208 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2210 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2213 // first look for the attribute of this specific cell
2214 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2218 // then look for the col attr (col attributes are more common than
2219 // the row ones, hence they have priority)
2220 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2225 // finally try the row attributes
2226 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2233 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2239 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2242 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2247 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2250 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2255 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2258 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2262 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2264 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2268 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2272 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2274 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2278 // ----------------------------------------------------------------------------
2279 // wxGridTypeRegistry
2280 // ----------------------------------------------------------------------------
2282 wxGridTypeRegistry::~wxGridTypeRegistry()
2284 size_t count
= m_typeinfo
.Count();
2285 for ( size_t i
= 0; i
< count
; i
++ )
2286 delete m_typeinfo
[i
];
2290 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2291 wxGridCellRenderer
* renderer
,
2292 wxGridCellEditor
* editor
)
2294 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2296 // is it already registered?
2297 int loc
= FindRegisteredDataType(typeName
);
2298 if ( loc
!= wxNOT_FOUND
)
2300 delete m_typeinfo
[loc
];
2301 m_typeinfo
[loc
] = info
;
2305 m_typeinfo
.Add(info
);
2309 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2311 size_t count
= m_typeinfo
.GetCount();
2312 for ( size_t i
= 0; i
< count
; i
++ )
2314 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2323 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2325 int index
= FindRegisteredDataType(typeName
);
2326 if ( index
== wxNOT_FOUND
)
2328 // check whether this is one of the standard ones, in which case
2329 // register it "on the fly"
2330 if ( typeName
== wxGRID_VALUE_STRING
)
2332 RegisterDataType(wxGRID_VALUE_STRING
,
2333 new wxGridCellStringRenderer
,
2334 new wxGridCellTextEditor
);
2336 else if ( typeName
== wxGRID_VALUE_BOOL
)
2338 RegisterDataType(wxGRID_VALUE_BOOL
,
2339 new wxGridCellBoolRenderer
,
2340 new wxGridCellBoolEditor
);
2342 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2344 RegisterDataType(wxGRID_VALUE_NUMBER
,
2345 new wxGridCellNumberRenderer
,
2346 new wxGridCellNumberEditor
);
2348 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2350 RegisterDataType(wxGRID_VALUE_FLOAT
,
2351 new wxGridCellFloatRenderer
,
2352 new wxGridCellFloatEditor
);
2354 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2356 RegisterDataType(wxGRID_VALUE_CHOICE
,
2357 new wxGridCellStringRenderer
,
2358 new wxGridCellChoiceEditor
);
2365 // we get here only if just added the entry for this type, so return
2367 index
= m_typeinfo
.GetCount() - 1;
2373 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2375 int index
= FindDataType(typeName
);
2376 if ( index
== wxNOT_FOUND
)
2378 // the first part of the typename is the "real" type, anything after ':'
2379 // are the parameters for the renderer
2380 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2381 if ( index
== wxNOT_FOUND
)
2386 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2387 wxGridCellRenderer
*rendererOld
= renderer
;
2388 renderer
= renderer
->Clone();
2389 rendererOld
->DecRef();
2391 wxGridCellEditor
*editor
= GetEditor(index
);
2392 wxGridCellEditor
*editorOld
= editor
;
2393 editor
= editor
->Clone();
2394 editorOld
->DecRef();
2396 // do it even if there are no parameters to reset them to defaults
2397 wxString params
= typeName
.AfterFirst(_T(':'));
2398 renderer
->SetParameters(params
);
2399 editor
->SetParameters(params
);
2401 // register the new typename
2402 RegisterDataType(typeName
, renderer
, editor
);
2404 // we just registered it, it's the last one
2405 index
= m_typeinfo
.GetCount() - 1;
2411 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2413 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2418 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2420 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2425 // ----------------------------------------------------------------------------
2427 // ----------------------------------------------------------------------------
2429 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2432 wxGridTableBase::wxGridTableBase()
2434 m_view
= (wxGrid
*) NULL
;
2435 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2438 wxGridTableBase::~wxGridTableBase()
2440 delete m_attrProvider
;
2443 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2445 delete m_attrProvider
;
2446 m_attrProvider
= attrProvider
;
2449 bool wxGridTableBase::CanHaveAttributes()
2451 if ( ! GetAttrProvider() )
2453 // use the default attr provider by default
2454 SetAttrProvider(new wxGridCellAttrProvider
);
2459 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2461 if ( m_attrProvider
)
2462 return m_attrProvider
->GetAttr(row
, col
);
2464 return (wxGridCellAttr
*)NULL
;
2467 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2469 if ( m_attrProvider
)
2471 m_attrProvider
->SetAttr(attr
, row
, col
);
2475 // as we take ownership of the pointer and don't store it, we must
2481 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2483 if ( m_attrProvider
)
2485 m_attrProvider
->SetRowAttr(attr
, row
);
2489 // as we take ownership of the pointer and don't store it, we must
2495 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2497 if ( m_attrProvider
)
2499 m_attrProvider
->SetColAttr(attr
, col
);
2503 // as we take ownership of the pointer and don't store it, we must
2509 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2510 size_t WXUNUSED(numRows
) )
2512 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2517 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2519 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2524 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2525 size_t WXUNUSED(numRows
) )
2527 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2532 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2533 size_t WXUNUSED(numCols
) )
2535 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2540 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2542 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2547 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2548 size_t WXUNUSED(numCols
) )
2550 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2556 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2559 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2560 // how much it makes sense to us geeks.
2564 wxString
wxGridTableBase::GetColLabelValue( int col
)
2566 // default col labels are:
2567 // cols 0 to 25 : A-Z
2568 // cols 26 to 675 : AA-ZZ
2573 for ( n
= 1; ; n
++ )
2575 s
+= (_T('A') + (wxChar
)( col%26
));
2577 if ( col
< 0 ) break;
2580 // reverse the string...
2582 for ( i
= 0; i
< n
; i
++ )
2591 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2593 return wxGRID_VALUE_STRING
;
2596 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2597 const wxString
& typeName
)
2599 return typeName
== wxGRID_VALUE_STRING
;
2602 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2604 return CanGetValueAs(row
, col
, typeName
);
2607 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2612 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2617 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2622 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2623 long WXUNUSED(value
) )
2627 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2628 double WXUNUSED(value
) )
2632 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2633 bool WXUNUSED(value
) )
2638 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2639 const wxString
& WXUNUSED(typeName
) )
2644 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2645 const wxString
& WXUNUSED(typeName
),
2646 void* WXUNUSED(value
) )
2650 //////////////////////////////////////////////////////////////////////
2652 // Message class for the grid table to send requests and notifications
2656 wxGridTableMessage::wxGridTableMessage()
2658 m_table
= (wxGridTableBase
*) NULL
;
2664 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2665 int commandInt1
, int commandInt2
)
2669 m_comInt1
= commandInt1
;
2670 m_comInt2
= commandInt2
;
2675 //////////////////////////////////////////////////////////////////////
2677 // A basic grid table for string data. An object of this class will
2678 // created by wxGrid if you don't specify an alternative table class.
2681 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2683 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2685 wxGridStringTable::wxGridStringTable()
2690 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2695 m_data
.Alloc( numRows
);
2698 sa
.Alloc( numCols
);
2699 for ( col
= 0; col
< numCols
; col
++ )
2701 sa
.Add( wxEmptyString
);
2704 for ( row
= 0; row
< numRows
; row
++ )
2710 wxGridStringTable::~wxGridStringTable()
2714 int wxGridStringTable::GetNumberRows()
2716 return m_data
.GetCount();
2719 int wxGridStringTable::GetNumberCols()
2721 if ( m_data
.GetCount() > 0 )
2722 return m_data
[0].GetCount();
2727 wxString
wxGridStringTable::GetValue( int row
, int col
)
2729 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2730 _T("invalid row or column index in wxGridStringTable") );
2732 return m_data
[row
][col
];
2735 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2737 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2738 _T("invalid row or column index in wxGridStringTable") );
2740 m_data
[row
][col
] = value
;
2743 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2745 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2746 _T("invalid row or column index in wxGridStringTable") );
2748 return (m_data
[row
][col
] == wxEmptyString
);
2751 void wxGridStringTable::Clear()
2754 int numRows
, numCols
;
2756 numRows
= m_data
.GetCount();
2759 numCols
= m_data
[0].GetCount();
2761 for ( row
= 0; row
< numRows
; row
++ )
2763 for ( col
= 0; col
< numCols
; col
++ )
2765 m_data
[row
][col
] = wxEmptyString
;
2772 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2776 size_t curNumRows
= m_data
.GetCount();
2777 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2778 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2780 if ( pos
>= curNumRows
)
2782 return AppendRows( numRows
);
2786 sa
.Alloc( curNumCols
);
2787 for ( col
= 0; col
< curNumCols
; col
++ )
2789 sa
.Add( wxEmptyString
);
2792 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2794 m_data
.Insert( sa
, row
);
2798 wxGridTableMessage
msg( this,
2799 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2803 GetView()->ProcessTableMessage( msg
);
2809 bool wxGridStringTable::AppendRows( size_t numRows
)
2813 size_t curNumRows
= m_data
.GetCount();
2814 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2815 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2818 if ( curNumCols
> 0 )
2820 sa
.Alloc( curNumCols
);
2821 for ( col
= 0; col
< curNumCols
; col
++ )
2823 sa
.Add( wxEmptyString
);
2827 for ( row
= 0; row
< numRows
; row
++ )
2834 wxGridTableMessage
msg( this,
2835 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2838 GetView()->ProcessTableMessage( msg
);
2844 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2848 size_t curNumRows
= m_data
.GetCount();
2850 if ( pos
>= curNumRows
)
2853 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2854 pos
, numRows
, curNumRows
);
2855 wxFAIL_MSG( errmsg
);
2859 if ( numRows
> curNumRows
- pos
)
2861 numRows
= curNumRows
- pos
;
2864 if ( numRows
>= curNumRows
)
2866 m_data
.Empty(); // don't release memory just yet
2870 for ( n
= 0; n
< numRows
; n
++ )
2872 m_data
.Remove( pos
);
2877 wxGridTableMessage
msg( this,
2878 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2882 GetView()->ProcessTableMessage( msg
);
2888 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2892 size_t curNumRows
= m_data
.GetCount();
2893 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2894 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2896 if ( pos
>= curNumCols
)
2898 return AppendCols( numCols
);
2901 for ( row
= 0; row
< curNumRows
; row
++ )
2903 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2905 m_data
[row
].Insert( wxEmptyString
, col
);
2910 wxGridTableMessage
msg( this,
2911 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2915 GetView()->ProcessTableMessage( msg
);
2921 bool wxGridStringTable::AppendCols( size_t numCols
)
2925 size_t curNumRows
= m_data
.GetCount();
2929 // TODO: something better than this ?
2931 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2936 for ( row
= 0; row
< curNumRows
; row
++ )
2938 for ( n
= 0; n
< numCols
; n
++ )
2940 m_data
[row
].Add( wxEmptyString
);
2946 wxGridTableMessage
msg( this,
2947 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2950 GetView()->ProcessTableMessage( msg
);
2956 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2960 size_t curNumRows
= m_data
.GetCount();
2961 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2962 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2964 if ( pos
>= curNumCols
)
2967 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
2968 pos
, numCols
, curNumCols
);
2969 wxFAIL_MSG( errmsg
);
2973 if ( numCols
> curNumCols
- pos
)
2975 numCols
= curNumCols
- pos
;
2978 for ( row
= 0; row
< curNumRows
; row
++ )
2980 if ( numCols
>= curNumCols
)
2982 m_data
[row
].Clear();
2986 for ( n
= 0; n
< numCols
; n
++ )
2988 m_data
[row
].Remove( pos
);
2994 wxGridTableMessage
msg( this,
2995 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2999 GetView()->ProcessTableMessage( msg
);
3005 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3007 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3009 // using default label
3011 return wxGridTableBase::GetRowLabelValue( row
);
3015 return m_rowLabels
[ row
];
3019 wxString
wxGridStringTable::GetColLabelValue( int col
)
3021 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3023 // using default label
3025 return wxGridTableBase::GetColLabelValue( col
);
3029 return m_colLabels
[ col
];
3033 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3035 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3037 int n
= m_rowLabels
.GetCount();
3039 for ( i
= n
; i
<= row
; i
++ )
3041 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3045 m_rowLabels
[row
] = value
;
3048 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3050 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3052 int n
= m_colLabels
.GetCount();
3054 for ( i
= n
; i
<= col
; i
++ )
3056 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3060 m_colLabels
[col
] = value
;
3065 //////////////////////////////////////////////////////////////////////
3066 //////////////////////////////////////////////////////////////////////
3068 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3070 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3071 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3072 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3073 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3074 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3077 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3079 const wxPoint
&pos
, const wxSize
&size
)
3080 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3085 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3089 // NO - don't do this because it will set both the x and y origin
3090 // coords to match the parent scrolled window and we just want to
3091 // set the y coord - MB
3093 // m_owner->PrepareDC( dc );
3096 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3097 dc
.SetDeviceOrigin( 0, -y
);
3099 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3100 m_owner
->DrawRowLabels( dc
);
3104 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3106 m_owner
->ProcessRowLabelMouseEvent( event
);
3110 // This seems to be required for wxMotif otherwise the mouse
3111 // cursor must be in the cell edit control to get key events
3113 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3115 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3118 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3120 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3125 //////////////////////////////////////////////////////////////////////
3127 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3129 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3130 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3131 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3132 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3133 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3136 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3138 const wxPoint
&pos
, const wxSize
&size
)
3139 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3144 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3148 // NO - don't do this because it will set both the x and y origin
3149 // coords to match the parent scrolled window and we just want to
3150 // set the x coord - MB
3152 // m_owner->PrepareDC( dc );
3155 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3156 dc
.SetDeviceOrigin( -x
, 0 );
3158 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3159 m_owner
->DrawColLabels( dc
);
3163 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3165 m_owner
->ProcessColLabelMouseEvent( event
);
3169 // This seems to be required for wxMotif otherwise the mouse
3170 // cursor must be in the cell edit control to get key events
3172 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3174 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3177 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3179 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3184 //////////////////////////////////////////////////////////////////////
3186 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3188 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3189 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3190 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3191 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3192 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3195 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3197 const wxPoint
&pos
, const wxSize
&size
)
3198 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3203 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3207 int client_height
= 0;
3208 int client_width
= 0;
3209 GetClientSize( &client_width
, &client_height
);
3211 dc
.SetPen( *wxBLACK_PEN
);
3212 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3213 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3215 dc
.SetPen( *wxWHITE_PEN
);
3216 dc
.DrawLine( 0, 0, client_width
, 0 );
3217 dc
.DrawLine( 0, 0, 0, client_height
);
3221 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3223 m_owner
->ProcessCornerLabelMouseEvent( event
);
3227 // This seems to be required for wxMotif otherwise the mouse
3228 // cursor must be in the cell edit control to get key events
3230 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3232 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3235 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3237 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3242 //////////////////////////////////////////////////////////////////////
3244 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3246 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3247 EVT_PAINT( wxGridWindow::OnPaint
)
3248 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3249 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3250 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3251 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3254 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3255 wxGridRowLabelWindow
*rowLblWin
,
3256 wxGridColLabelWindow
*colLblWin
,
3257 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3258 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3261 m_rowLabelWin
= rowLblWin
;
3262 m_colLabelWin
= colLblWin
;
3263 SetBackgroundColour( "WHITE" );
3267 wxGridWindow::~wxGridWindow()
3272 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3274 wxPaintDC
dc( this );
3275 m_owner
->PrepareDC( dc
);
3276 wxRegion reg
= GetUpdateRegion();
3277 m_owner
->CalcCellsExposed( reg
);
3278 m_owner
->DrawGridCellArea( dc
);
3279 #if WXGRID_DRAW_LINES
3280 m_owner
->DrawAllGridLines( dc
, reg
);
3282 m_owner
->DrawGridSpace( dc
);
3283 m_owner
->DrawHighlight( dc
);
3287 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3289 wxPanel::ScrollWindow( dx
, dy
, rect
);
3290 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3291 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3295 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3297 m_owner
->ProcessGridCellMouseEvent( event
);
3301 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3302 // cursor must be in the cell edit control to get key events
3304 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3306 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3309 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3311 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3314 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3319 //////////////////////////////////////////////////////////////////////
3322 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3324 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3325 EVT_PAINT( wxGrid::OnPaint
)
3326 EVT_SIZE( wxGrid::OnSize
)
3327 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3328 EVT_KEY_UP( wxGrid::OnKeyUp
)
3329 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3332 wxGrid::wxGrid( wxWindow
*parent
,
3337 const wxString
& name
)
3338 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3339 m_colMinWidths(GRID_HASH_SIZE
),
3340 m_rowMinHeights(GRID_HASH_SIZE
)
3349 wxSafeDecRef(m_defaultCellAttr
);
3351 #ifdef DEBUG_ATTR_CACHE
3352 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3353 wxPrintf(_T("wxGrid attribute cache statistics: "
3354 "total: %u, hits: %u (%u%%)\n"),
3355 total
, gs_nAttrCacheHits
,
3356 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3362 delete m_typeRegistry
;
3368 // ----- internal init and update functions
3371 void wxGrid::Create()
3373 m_created
= FALSE
; // set to TRUE by CreateGrid
3375 m_table
= (wxGridTableBase
*) NULL
;
3378 m_cellEditCtrlEnabled
= FALSE
;
3380 m_defaultCellAttr
= new wxGridCellAttr
;
3381 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3383 // Set default cell attributes
3384 m_defaultCellAttr
->SetFont(GetFont());
3385 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3386 m_defaultCellAttr
->SetTextColour(
3387 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3388 m_defaultCellAttr
->SetBackgroundColour(
3389 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3390 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3391 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3396 m_currentCellCoords
= wxGridNoCellCoords
;
3398 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3399 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3401 // create the type registry
3402 m_typeRegistry
= new wxGridTypeRegistry
;
3404 // subwindow components that make up the wxGrid
3405 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3410 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3415 m_colLabelWin
= new wxGridColLabelWindow( this,
3420 m_gridWin
= new wxGridWindow( this,
3427 SetTargetWindow( m_gridWin
);
3431 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3432 wxGrid::wxGridSelectionModes selmode
)
3434 wxCHECK_MSG( !m_created
,
3436 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3438 m_numRows
= numRows
;
3439 m_numCols
= numCols
;
3441 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3442 m_table
->SetView( this );
3444 m_selection
= new wxGridSelection( this, selmode
);
3451 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3455 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3458 m_selection
->SetSelectionMode( selmode
);
3461 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3462 wxGrid::wxGridSelectionModes selmode
)
3466 // RD: Actually, this should probably be allowed. I think it would be
3467 // nice to be able to switch multiple Tables in and out of a single
3468 // View at runtime. Is there anything in the implmentation that would
3471 // At least, you now have to cope with m_selection
3472 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3477 m_numRows
= table
->GetNumberRows();
3478 m_numCols
= table
->GetNumberCols();
3481 m_table
->SetView( this );
3484 m_selection
= new wxGridSelection( this, selmode
);
3495 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3496 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3498 if ( m_rowLabelWin
)
3500 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3504 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3507 m_labelTextColour
= wxColour( _T("BLACK") );
3510 m_attrCache
.row
= -1;
3512 // TODO: something better than this ?
3514 m_labelFont
= this->GetFont();
3515 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3517 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3518 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3520 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3521 m_colLabelVertAlign
= wxALIGN_TOP
;
3523 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3524 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3526 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3527 m_defaultRowHeight
+= 8;
3529 m_defaultRowHeight
+= 4;
3532 m_gridLineColour
= wxColour( 128, 128, 255 );
3533 m_gridLinesEnabled
= TRUE
;
3534 m_cellHighlightColour
= m_gridLineColour
;
3536 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3537 m_winCapture
= (wxWindow
*)NULL
;
3538 m_canDragRowSize
= TRUE
;
3539 m_canDragColSize
= TRUE
;
3540 m_canDragGridSize
= TRUE
;
3542 m_dragRowOrCol
= -1;
3543 m_isDragging
= FALSE
;
3544 m_startDragPos
= wxDefaultPosition
;
3546 m_waitForSlowClick
= FALSE
;
3548 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3549 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3551 m_currentCellCoords
= wxGridNoCellCoords
;
3553 m_selectingTopLeft
= wxGridNoCellCoords
;
3554 m_selectingBottomRight
= wxGridNoCellCoords
;
3555 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3556 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3558 m_editable
= TRUE
; // default for whole grid
3560 m_inOnKeyDown
= FALSE
;
3569 // ----------------------------------------------------------------------------
3570 // the idea is to call these functions only when necessary because they create
3571 // quite big arrays which eat memory mostly unnecessary - in particular, if
3572 // default widths/heights are used for all rows/columns, we may not use these
3575 // with some extra code, it should be possible to only store the
3576 // widths/heights different from default ones but this will be done later...
3577 // ----------------------------------------------------------------------------
3579 void wxGrid::InitRowHeights()
3581 m_rowHeights
.Empty();
3582 m_rowBottoms
.Empty();
3584 m_rowHeights
.Alloc( m_numRows
);
3585 m_rowBottoms
.Alloc( m_numRows
);
3588 for ( int i
= 0; i
< m_numRows
; i
++ )
3590 m_rowHeights
.Add( m_defaultRowHeight
);
3591 rowBottom
+= m_defaultRowHeight
;
3592 m_rowBottoms
.Add( rowBottom
);
3596 void wxGrid::InitColWidths()
3598 m_colWidths
.Empty();
3599 m_colRights
.Empty();
3601 m_colWidths
.Alloc( m_numCols
);
3602 m_colRights
.Alloc( m_numCols
);
3604 for ( int i
= 0; i
< m_numCols
; i
++ )
3606 m_colWidths
.Add( m_defaultColWidth
);
3607 colRight
+= m_defaultColWidth
;
3608 m_colRights
.Add( colRight
);
3612 int wxGrid::GetColWidth(int col
) const
3614 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3617 int wxGrid::GetColLeft(int col
) const
3619 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3620 : m_colRights
[col
] - m_colWidths
[col
];
3623 int wxGrid::GetColRight(int col
) const
3625 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3629 int wxGrid::GetRowHeight(int row
) const
3631 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3634 int wxGrid::GetRowTop(int row
) const
3636 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3637 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3640 int wxGrid::GetRowBottom(int row
) const
3642 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3643 : m_rowBottoms
[row
];
3646 void wxGrid::CalcDimensions()
3649 GetClientSize( &cw
, &ch
);
3651 if ( m_numRows
> 0 || m_numCols
> 0 )
3653 int right
= m_numCols
> 0 ? GetColRight( m_numCols
-1 ) + m_extraWidth
: 0;
3654 int bottom
= m_numRows
> 0 ? GetRowBottom( m_numRows
-1 ) + m_extraHeight
: 0;
3656 // TODO: restore the scroll position that we had before sizing
3659 GetViewStart( &x
, &y
);
3660 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3661 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3667 void wxGrid::CalcWindowSizes()
3670 GetClientSize( &cw
, &ch
);
3672 if ( m_cornerLabelWin
->IsShown() )
3673 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3675 if ( m_colLabelWin
->IsShown() )
3676 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3678 if ( m_rowLabelWin
->IsShown() )
3679 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3681 if ( m_gridWin
->IsShown() )
3682 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3686 // this is called when the grid table sends a message to say that it
3687 // has been redimensioned
3689 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3692 bool result
= FALSE
;
3695 // if we were using the default widths/heights so far, we must change them
3697 if ( m_colWidths
.IsEmpty() )
3702 if ( m_rowHeights
.IsEmpty() )
3708 switch ( msg
.GetId() )
3710 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3712 size_t pos
= msg
.GetCommandInt();
3713 int numRows
= msg
.GetCommandInt2();
3715 m_numRows
+= numRows
;
3717 if ( !m_rowHeights
.IsEmpty() )
3719 for ( i
= 0; i
< numRows
; i
++ )
3721 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3722 m_rowBottoms
.Insert( 0, pos
);
3726 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3728 for ( i
= pos
; i
< m_numRows
; i
++ )
3730 bottom
+= m_rowHeights
[i
];
3731 m_rowBottoms
[i
] = bottom
;
3734 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3736 // if we have just inserted cols into an empty grid the current
3737 // cell will be undefined...
3739 SetCurrentCell( 0, 0 );
3741 m_selection
->UpdateRows( pos
, numRows
);
3742 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3744 attrProvider
->UpdateAttrRows( pos
, numRows
);
3746 if ( !GetBatchCount() )
3749 m_rowLabelWin
->Refresh();
3755 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3757 int numRows
= msg
.GetCommandInt();
3758 int oldNumRows
= m_numRows
;
3759 m_numRows
+= numRows
;
3761 if ( !m_rowHeights
.IsEmpty() )
3763 for ( i
= 0; i
< numRows
; i
++ )
3765 m_rowHeights
.Add( m_defaultRowHeight
);
3766 m_rowBottoms
.Add( 0 );
3770 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3772 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3774 bottom
+= m_rowHeights
[i
];
3775 m_rowBottoms
[i
] = bottom
;
3778 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3780 // if we have just inserted cols into an empty grid the current
3781 // cell will be undefined...
3783 SetCurrentCell( 0, 0 );
3785 if ( !GetBatchCount() )
3788 m_rowLabelWin
->Refresh();
3794 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3796 size_t pos
= msg
.GetCommandInt();
3797 int numRows
= msg
.GetCommandInt2();
3798 m_numRows
-= numRows
;
3800 if ( !m_rowHeights
.IsEmpty() )
3802 for ( i
= 0; i
< numRows
; i
++ )
3804 m_rowHeights
.Remove( pos
);
3805 m_rowBottoms
.Remove( pos
);
3809 for ( i
= 0; i
< m_numRows
; i
++ )
3811 h
+= m_rowHeights
[i
];
3812 m_rowBottoms
[i
] = h
;
3817 m_currentCellCoords
= wxGridNoCellCoords
;
3821 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3822 m_currentCellCoords
.Set( 0, 0 );
3824 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3825 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3827 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3828 // ifdef'd out following patch from Paul Gammans
3830 // removed _all_ rows, in this case, we remove
3831 // all column attributes.
3832 // I hate to do this here, but the
3833 // needed data is not available inside UpdateAttrRows.
3834 if ( !GetNumberRows() )
3835 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3838 if ( !GetBatchCount() )
3841 m_rowLabelWin
->Refresh();
3847 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3849 size_t pos
= msg
.GetCommandInt();
3850 int numCols
= msg
.GetCommandInt2();
3851 m_numCols
+= numCols
;
3853 if ( !m_colWidths
.IsEmpty() )
3855 for ( i
= 0; i
< numCols
; i
++ )
3857 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3858 m_colRights
.Insert( 0, pos
);
3862 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3864 for ( i
= pos
; i
< m_numCols
; i
++ )
3866 right
+= m_colWidths
[i
];
3867 m_colRights
[i
] = right
;
3870 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3872 // if we have just inserted cols into an empty grid the current
3873 // cell will be undefined...
3875 SetCurrentCell( 0, 0 );
3877 m_selection
->UpdateCols( pos
, numCols
);
3878 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3880 attrProvider
->UpdateAttrCols( pos
, numCols
);
3881 if ( !GetBatchCount() )
3884 m_colLabelWin
->Refresh();
3891 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3893 int numCols
= msg
.GetCommandInt();
3894 int oldNumCols
= m_numCols
;
3895 m_numCols
+= numCols
;
3896 if ( !m_colWidths
.IsEmpty() )
3898 for ( i
= 0; i
< numCols
; i
++ )
3900 m_colWidths
.Add( m_defaultColWidth
);
3901 m_colRights
.Add( 0 );
3905 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3907 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3909 right
+= m_colWidths
[i
];
3910 m_colRights
[i
] = right
;
3913 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3915 // if we have just inserted cols into an empty grid the current
3916 // cell will be undefined...
3918 SetCurrentCell( 0, 0 );
3920 if ( !GetBatchCount() )
3923 m_colLabelWin
->Refresh();
3929 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3931 size_t pos
= msg
.GetCommandInt();
3932 int numCols
= msg
.GetCommandInt2();
3933 m_numCols
-= numCols
;
3935 if ( !m_colWidths
.IsEmpty() )
3937 for ( i
= 0; i
< numCols
; i
++ )
3939 m_colWidths
.Remove( pos
);
3940 m_colRights
.Remove( pos
);
3944 for ( i
= 0; i
< m_numCols
; i
++ )
3946 w
+= m_colWidths
[i
];
3952 m_currentCellCoords
= wxGridNoCellCoords
;
3956 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3957 m_currentCellCoords
.Set( 0, 0 );
3959 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3960 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3962 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3963 // ifdef'd out following patch from Paul Gammans
3965 // No need to touch row attributes, unless we
3966 // removed _all_ columns, in this case, we remove
3967 // all row attributes.
3968 // I hate to do this here, but the
3969 // needed data is not available inside UpdateAttrCols.
3970 if ( !GetNumberCols() )
3971 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
3974 if ( !GetBatchCount() )
3977 m_colLabelWin
->Refresh();
3982 // There is no path to this code !!!!!!
3988 if (result
&& !GetBatchCount() )
3989 m_gridWin
->Refresh();
3994 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
3996 wxRegionIterator
iter( reg
);
3999 m_rowLabelsExposed
.Empty();
4006 // TODO: remove this when we can...
4007 // There is a bug in wxMotif that gives garbage update
4008 // rectangles if you jump-scroll a long way by clicking the
4009 // scrollbar with middle button. This is a work-around
4011 #if defined(__WXMOTIF__)
4013 m_gridWin
->GetClientSize( &cw
, &ch
);
4014 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4015 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4018 // logical bounds of update region
4021 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4022 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4024 // find the row labels within these bounds
4027 for ( row
= 0; row
< m_numRows
; row
++ )
4029 if ( GetRowBottom(row
) < top
)
4032 if ( GetRowTop(row
) > bottom
)
4035 m_rowLabelsExposed
.Add( row
);
4043 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4045 wxRegionIterator
iter( reg
);
4048 m_colLabelsExposed
.Empty();
4055 // TODO: remove this when we can...
4056 // There is a bug in wxMotif that gives garbage update
4057 // rectangles if you jump-scroll a long way by clicking the
4058 // scrollbar with middle button. This is a work-around
4060 #if defined(__WXMOTIF__)
4062 m_gridWin
->GetClientSize( &cw
, &ch
);
4063 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4064 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4067 // logical bounds of update region
4070 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4071 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4073 // find the cells within these bounds
4076 for ( col
= 0; col
< m_numCols
; col
++ )
4078 if ( GetColRight(col
) < left
)
4081 if ( GetColLeft(col
) > right
)
4084 m_colLabelsExposed
.Add( col
);
4092 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4094 wxRegionIterator
iter( reg
);
4097 m_cellsExposed
.Empty();
4098 m_rowsExposed
.Empty();
4099 m_colsExposed
.Empty();
4101 int left
, top
, right
, bottom
;
4106 // TODO: remove this when we can...
4107 // There is a bug in wxMotif that gives garbage update
4108 // rectangles if you jump-scroll a long way by clicking the
4109 // scrollbar with middle button. This is a work-around
4111 #if defined(__WXMOTIF__)
4113 m_gridWin
->GetClientSize( &cw
, &ch
);
4114 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4115 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4116 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4117 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4120 // logical bounds of update region
4122 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4123 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4125 // find the cells within these bounds
4128 for ( row
= 0; row
< m_numRows
; row
++ )
4130 if ( GetRowBottom(row
) <= top
)
4133 if ( GetRowTop(row
) > bottom
)
4136 m_rowsExposed
.Add( row
);
4138 for ( col
= 0; col
< m_numCols
; col
++ )
4140 if ( GetColRight(col
) <= left
)
4143 if ( GetColLeft(col
) > right
)
4146 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4147 m_colsExposed
.Add( col
);
4148 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4157 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4160 wxPoint
pos( event
.GetPosition() );
4161 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4163 if ( event
.Dragging() )
4165 m_isDragging
= TRUE
;
4167 if ( event
.LeftIsDown() )
4169 switch( m_cursorMode
)
4171 case WXGRID_CURSOR_RESIZE_ROW
:
4173 int cw
, ch
, left
, dummy
;
4174 m_gridWin
->GetClientSize( &cw
, &ch
);
4175 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4177 wxClientDC
dc( m_gridWin
);
4180 GetRowTop(m_dragRowOrCol
) +
4181 GetRowMinimalHeight(m_dragRowOrCol
) );
4182 dc
.SetLogicalFunction(wxINVERT
);
4183 if ( m_dragLastPos
>= 0 )
4185 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4187 dc
.DrawLine( left
, y
, left
+cw
, y
);
4192 case WXGRID_CURSOR_SELECT_ROW
:
4193 if ( (row
= YToRow( y
)) >= 0 )
4195 m_selection
->SelectRow( row
,
4196 event
.ControlDown(),
4202 // default label to suppress warnings about "enumeration value
4203 // 'xxx' not handled in switch
4211 m_isDragging
= FALSE
;
4214 // ------------ Entering or leaving the window
4216 if ( event
.Entering() || event
.Leaving() )
4218 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4222 // ------------ Left button pressed
4224 else if ( event
.LeftDown() )
4226 // don't send a label click event for a hit on the
4227 // edge of the row label - this is probably the user
4228 // wanting to resize the row
4230 if ( YToEdgeOfRow(y
) < 0 )
4234 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4236 if ( !event
.ShiftDown() && !event
.ControlDown() )
4238 if ( event
.ShiftDown() )
4239 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4242 GetNumberCols() - 1,
4243 event
.ControlDown(),
4248 m_selection
->SelectRow( row
,
4249 event
.ControlDown(),
4253 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4258 // starting to drag-resize a row
4260 if ( CanDragRowSize() )
4261 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4266 // ------------ Left double click
4268 else if (event
.LeftDClick() )
4270 if ( YToEdgeOfRow(y
) < 0 )
4273 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4278 // ------------ Left button released
4280 else if ( event
.LeftUp() )
4282 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4284 DoEndDragResizeRow();
4286 // Note: we are ending the event *after* doing
4287 // default processing in this case
4289 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4292 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4297 // ------------ Right button down
4299 else if ( event
.RightDown() )
4302 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4304 // no default action at the moment
4309 // ------------ Right double click
4311 else if ( event
.RightDClick() )
4314 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4316 // no default action at the moment
4321 // ------------ No buttons down and mouse moving
4323 else if ( event
.Moving() )
4325 m_dragRowOrCol
= YToEdgeOfRow( y
);
4326 if ( m_dragRowOrCol
>= 0 )
4328 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4330 // don't capture the mouse yet
4331 if ( CanDragRowSize() )
4332 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4335 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4337 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4343 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4346 wxPoint
pos( event
.GetPosition() );
4347 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4349 if ( event
.Dragging() )
4351 m_isDragging
= TRUE
;
4353 if ( event
.LeftIsDown() )
4355 switch( m_cursorMode
)
4357 case WXGRID_CURSOR_RESIZE_COL
:
4359 int cw
, ch
, dummy
, top
;
4360 m_gridWin
->GetClientSize( &cw
, &ch
);
4361 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4363 wxClientDC
dc( m_gridWin
);
4366 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4367 GetColMinimalWidth(m_dragRowOrCol
));
4368 dc
.SetLogicalFunction(wxINVERT
);
4369 if ( m_dragLastPos
>= 0 )
4371 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4373 dc
.DrawLine( x
, top
, x
, top
+ch
);
4378 case WXGRID_CURSOR_SELECT_COL
:
4379 if ( (col
= XToCol( x
)) >= 0 )
4381 m_selection
->SelectCol( col
,
4382 event
.ControlDown(),
4388 // default label to suppress warnings about "enumeration value
4389 // 'xxx' not handled in switch
4397 m_isDragging
= FALSE
;
4400 // ------------ Entering or leaving the window
4402 if ( event
.Entering() || event
.Leaving() )
4404 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4408 // ------------ Left button pressed
4410 else if ( event
.LeftDown() )
4412 // don't send a label click event for a hit on the
4413 // edge of the col label - this is probably the user
4414 // wanting to resize the col
4416 if ( XToEdgeOfCol(x
) < 0 )
4420 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4422 if ( !event
.ShiftDown() && !event
.ControlDown() )
4424 if ( event
.ShiftDown() )
4425 m_selection
->SelectBlock( 0,
4426 m_currentCellCoords
.GetCol(),
4427 GetNumberRows() - 1, col
,
4428 event
.ControlDown(),
4433 m_selection
->SelectCol( col
,
4434 event
.ControlDown(),
4438 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4443 // starting to drag-resize a col
4445 if ( CanDragColSize() )
4446 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4451 // ------------ Left double click
4453 if ( event
.LeftDClick() )
4455 if ( XToEdgeOfCol(x
) < 0 )
4458 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4463 // ------------ Left button released
4465 else if ( event
.LeftUp() )
4467 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4469 DoEndDragResizeCol();
4471 // Note: we are ending the event *after* doing
4472 // default processing in this case
4474 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4482 // ------------ Right button down
4484 else if ( event
.RightDown() )
4487 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4489 // no default action at the moment
4494 // ------------ Right double click
4496 else if ( event
.RightDClick() )
4499 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4501 // no default action at the moment
4506 // ------------ No buttons down and mouse moving
4508 else if ( event
.Moving() )
4510 m_dragRowOrCol
= XToEdgeOfCol( x
);
4511 if ( m_dragRowOrCol
>= 0 )
4513 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4515 // don't capture the cursor yet
4516 if ( CanDragColSize() )
4517 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4520 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4522 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4528 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4530 if ( event
.LeftDown() )
4532 // indicate corner label by having both row and
4535 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4541 else if ( event
.LeftDClick() )
4543 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4546 else if ( event
.RightDown() )
4548 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4550 // no default action at the moment
4554 else if ( event
.RightDClick() )
4556 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4558 // no default action at the moment
4563 void wxGrid::ChangeCursorMode(CursorMode mode
,
4568 static const wxChar
*cursorModes
[] =
4577 wxLogTrace(_T("grid"),
4578 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4579 win
== m_colLabelWin
? _T("colLabelWin")
4580 : win
? _T("rowLabelWin")
4582 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4583 #endif // __WXDEBUG__
4585 if ( mode
== m_cursorMode
)
4590 // by default use the grid itself
4596 m_winCapture
->ReleaseMouse();
4597 m_winCapture
= (wxWindow
*)NULL
;
4600 m_cursorMode
= mode
;
4602 switch ( m_cursorMode
)
4604 case WXGRID_CURSOR_RESIZE_ROW
:
4605 win
->SetCursor( m_rowResizeCursor
);
4608 case WXGRID_CURSOR_RESIZE_COL
:
4609 win
->SetCursor( m_colResizeCursor
);
4613 win
->SetCursor( *wxSTANDARD_CURSOR
);
4616 // we need to capture mouse when resizing
4617 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4618 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4620 if ( captureMouse
&& resize
)
4622 win
->CaptureMouse();
4627 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4630 wxPoint
pos( event
.GetPosition() );
4631 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4633 wxGridCellCoords coords
;
4634 XYToCell( x
, y
, coords
);
4636 if ( event
.Dragging() )
4638 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4640 // Don't start doing anything until the mouse has been drug at
4641 // least 3 pixels in any direction...
4644 if (m_startDragPos
== wxDefaultPosition
)
4646 m_startDragPos
= pos
;
4649 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4653 m_isDragging
= TRUE
;
4654 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4656 // Hide the edit control, so it
4657 // won't interfer with drag-shrinking.
4658 if ( IsCellEditControlShown() )
4660 HideCellEditControl();
4661 SaveEditControlValue();
4664 // Have we captured the mouse yet?
4667 m_winCapture
= m_gridWin
;
4668 m_winCapture
->CaptureMouse();
4671 if ( coords
!= wxGridNoCellCoords
)
4673 if ( event
.ControlDown() )
4675 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4676 m_selectingKeyboard
= coords
;
4677 HighlightBlock ( m_selectingKeyboard
, coords
);
4681 if ( !IsSelection() )
4683 HighlightBlock( coords
, coords
);
4687 HighlightBlock( m_currentCellCoords
, coords
);
4691 if (! IsVisible(coords
))
4693 MakeCellVisible(coords
);
4694 // TODO: need to introduce a delay or something here. The
4695 // scrolling is way to fast, at least on MSW - also on GTK.
4699 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4701 int cw
, ch
, left
, dummy
;
4702 m_gridWin
->GetClientSize( &cw
, &ch
);
4703 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4705 wxClientDC
dc( m_gridWin
);
4707 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4708 GetRowMinimalHeight(m_dragRowOrCol
) );
4709 dc
.SetLogicalFunction(wxINVERT
);
4710 if ( m_dragLastPos
>= 0 )
4712 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4714 dc
.DrawLine( left
, y
, left
+cw
, y
);
4717 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4719 int cw
, ch
, dummy
, top
;
4720 m_gridWin
->GetClientSize( &cw
, &ch
);
4721 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4723 wxClientDC
dc( m_gridWin
);
4725 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4726 GetColMinimalWidth(m_dragRowOrCol
) );
4727 dc
.SetLogicalFunction(wxINVERT
);
4728 if ( m_dragLastPos
>= 0 )
4730 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4732 dc
.DrawLine( x
, top
, x
, top
+ch
);
4739 m_isDragging
= FALSE
;
4740 m_startDragPos
= wxDefaultPosition
;
4742 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4743 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4746 if ( event
.Entering() || event
.Leaving() )
4748 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4749 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4754 // ------------ Left button pressed
4756 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4758 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4763 if ( !event
.ControlDown() )
4765 if ( event
.ShiftDown() )
4767 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4768 m_currentCellCoords
.GetCol(),
4771 event
.ControlDown(),
4776 else if ( XToEdgeOfCol(x
) < 0 &&
4777 YToEdgeOfRow(y
) < 0 )
4779 DisableCellEditControl();
4780 MakeCellVisible( coords
);
4782 // if this is the second click on this cell then start
4784 if ( m_waitForSlowClick
&&
4785 (coords
== m_currentCellCoords
) &&
4786 CanEnableCellControl())
4788 EnableCellEditControl();
4790 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4791 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4794 editor
->StartingClick();
4798 m_waitForSlowClick
= FALSE
;
4802 if ( event
.ControlDown() )
4804 m_selection
->ToggleCellSelection( coords
.GetRow(),
4806 event
.ControlDown(),
4810 m_selectingTopLeft
= wxGridNoCellCoords
;
4811 m_selectingBottomRight
= wxGridNoCellCoords
;
4812 m_selectingKeyboard
= coords
;
4816 SetCurrentCell( coords
);
4817 if ( m_selection
->GetSelectionMode()
4818 != wxGrid::wxGridSelectCells
)
4819 HighlightBlock( coords
, coords
);
4821 m_waitForSlowClick
= TRUE
;
4828 // ------------ Left double click
4830 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4832 DisableCellEditControl();
4834 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4836 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4844 // ------------ Left button released
4846 else if ( event
.LeftUp() )
4848 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4850 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4851 m_selectingBottomRight
!= wxGridNoCellCoords
)
4855 m_winCapture
->ReleaseMouse();
4856 m_winCapture
= NULL
;
4858 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4859 m_selectingTopLeft
.GetCol(),
4860 m_selectingBottomRight
.GetRow(),
4861 m_selectingBottomRight
.GetCol(),
4862 event
.ControlDown(),
4866 m_selectingTopLeft
= wxGridNoCellCoords
;
4867 m_selectingBottomRight
= wxGridNoCellCoords
;
4870 // Show the edit control, if it has been hidden for
4872 ShowCellEditControl();
4874 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4876 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4877 DoEndDragResizeRow();
4879 // Note: we are ending the event *after* doing
4880 // default processing in this case
4882 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4884 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4886 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4887 DoEndDragResizeCol();
4889 // Note: we are ending the event *after* doing
4890 // default processing in this case
4892 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4899 // ------------ Right button down
4901 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4903 DisableCellEditControl();
4904 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4909 // no default action at the moment
4914 // ------------ Right double click
4916 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4918 DisableCellEditControl();
4919 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4924 // no default action at the moment
4928 // ------------ Moving and no button action
4930 else if ( event
.Moving() && !event
.IsButton() )
4932 int dragRow
= YToEdgeOfRow( y
);
4933 int dragCol
= XToEdgeOfCol( x
);
4935 // Dragging on the corner of a cell to resize in both
4936 // directions is not implemented yet...
4938 if ( dragRow
>= 0 && dragCol
>= 0 )
4940 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4946 m_dragRowOrCol
= dragRow
;
4948 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4950 if ( CanDragRowSize() && CanDragGridSize() )
4951 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4956 m_dragRowOrCol
= dragCol
;
4964 m_dragRowOrCol
= dragCol
;
4966 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4968 if ( CanDragColSize() && CanDragGridSize() )
4969 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4975 // Neither on a row or col edge
4977 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4979 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4985 void wxGrid::DoEndDragResizeRow()
4987 if ( m_dragLastPos
>= 0 )
4989 // erase the last line and resize the row
4991 int cw
, ch
, left
, dummy
;
4992 m_gridWin
->GetClientSize( &cw
, &ch
);
4993 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4995 wxClientDC
dc( m_gridWin
);
4997 dc
.SetLogicalFunction( wxINVERT
);
4998 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4999 HideCellEditControl();
5000 SaveEditControlValue();
5002 int rowTop
= GetRowTop(m_dragRowOrCol
);
5003 SetRowSize( m_dragRowOrCol
,
5004 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5006 if ( !GetBatchCount() )
5008 // Only needed to get the correct rect.y:
5009 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5011 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5012 rect
.width
= m_rowLabelWidth
;
5013 rect
.height
= ch
- rect
.y
;
5014 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5016 m_gridWin
->Refresh( FALSE
, &rect
);
5019 ShowCellEditControl();
5024 void wxGrid::DoEndDragResizeCol()
5026 if ( m_dragLastPos
>= 0 )
5028 // erase the last line and resize the col
5030 int cw
, ch
, dummy
, top
;
5031 m_gridWin
->GetClientSize( &cw
, &ch
);
5032 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5034 wxClientDC
dc( m_gridWin
);
5036 dc
.SetLogicalFunction( wxINVERT
);
5037 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5038 HideCellEditControl();
5039 SaveEditControlValue();
5041 int colLeft
= GetColLeft(m_dragRowOrCol
);
5042 SetColSize( m_dragRowOrCol
,
5043 wxMax( m_dragLastPos
- colLeft
,
5044 GetColMinimalWidth(m_dragRowOrCol
) ) );
5046 if ( !GetBatchCount() )
5048 // Only needed to get the correct rect.x:
5049 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5051 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5052 rect
.width
= cw
- rect
.x
;
5053 rect
.height
= m_colLabelHeight
;
5054 m_colLabelWin
->Refresh( TRUE
, &rect
);
5056 m_gridWin
->Refresh( FALSE
, &rect
);
5059 ShowCellEditControl();
5066 // ------ interaction with data model
5068 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5070 switch ( msg
.GetId() )
5072 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5073 return GetModelValues();
5075 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5076 return SetModelValues();
5078 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5079 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5080 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5081 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5082 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5083 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5084 return Redimension( msg
);
5093 // The behaviour of this function depends on the grid table class
5094 // Clear() function. For the default wxGridStringTable class the
5095 // behavious is to replace all cell contents with wxEmptyString but
5096 // not to change the number of rows or cols.
5098 void wxGrid::ClearGrid()
5102 if (IsCellEditControlEnabled())
5103 DisableCellEditControl();
5106 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5111 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5113 // TODO: something with updateLabels flag
5117 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5123 if (IsCellEditControlEnabled())
5124 DisableCellEditControl();
5126 return m_table
->InsertRows( pos
, numRows
);
5128 // the table will have sent the results of the insert row
5129 // operation to this view object as a grid table message
5135 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5137 // TODO: something with updateLabels flag
5141 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5145 return ( m_table
&& m_table
->AppendRows( numRows
) );
5146 // the table will have sent the results of the append row
5147 // operation to this view object as a grid table message
5151 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5153 // TODO: something with updateLabels flag
5157 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5163 if (IsCellEditControlEnabled())
5164 DisableCellEditControl();
5166 return (m_table
->DeleteRows( pos
, numRows
));
5167 // the table will have sent the results of the delete row
5168 // operation to this view object as a grid table message
5174 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5176 // TODO: something with updateLabels flag
5180 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5186 if (IsCellEditControlEnabled())
5187 DisableCellEditControl();
5189 return m_table
->InsertCols( pos
, numCols
);
5190 // the table will have sent the results of the insert col
5191 // operation to this view object as a grid table message
5197 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5199 // TODO: something with updateLabels flag
5203 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5207 return ( m_table
&& m_table
->AppendCols( numCols
) );
5208 // the table will have sent the results of the append col
5209 // operation to this view object as a grid table message
5213 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5215 // TODO: something with updateLabels flag
5219 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5225 if (IsCellEditControlEnabled())
5226 DisableCellEditControl();
5228 return ( m_table
->DeleteCols( pos
, numCols
) );
5229 // the table will have sent the results of the delete col
5230 // operation to this view object as a grid table message
5238 // ----- event handlers
5241 // Generate a grid event based on a mouse event and
5242 // return the result of ProcessEvent()
5244 bool wxGrid::SendEvent( const wxEventType type
,
5246 wxMouseEvent
& mouseEv
)
5248 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5250 int rowOrCol
= (row
== -1 ? col
: row
);
5252 wxGridSizeEvent
gridEvt( GetId(),
5256 mouseEv
.GetX() + GetColLabelSize(),
5257 mouseEv
.GetY() + GetRowLabelSize(),
5258 mouseEv
.ControlDown(),
5259 mouseEv
.ShiftDown(),
5261 mouseEv
.MetaDown() );
5263 return GetEventHandler()->ProcessEvent(gridEvt
);
5265 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5267 // Right now, it should _never_ end up here!
5268 wxGridRangeSelectEvent
gridEvt( GetId(),
5272 m_selectingBottomRight
,
5274 mouseEv
.ControlDown(),
5275 mouseEv
.ShiftDown(),
5277 mouseEv
.MetaDown() );
5279 return GetEventHandler()->ProcessEvent(gridEvt
);
5283 wxGridEvent
gridEvt( GetId(),
5287 mouseEv
.GetX() + GetColLabelSize(),
5288 mouseEv
.GetY() + GetRowLabelSize(),
5290 mouseEv
.ControlDown(),
5291 mouseEv
.ShiftDown(),
5293 mouseEv
.MetaDown() );
5295 return GetEventHandler()->ProcessEvent(gridEvt
);
5300 // Generate a grid event of specified type and return the result
5301 // of ProcessEvent().
5303 bool wxGrid::SendEvent( const wxEventType type
,
5306 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5308 int rowOrCol
= (row
== -1 ? col
: row
);
5310 wxGridSizeEvent
gridEvt( GetId(),
5315 return GetEventHandler()->ProcessEvent(gridEvt
);
5319 wxGridEvent
gridEvt( GetId(),
5324 return GetEventHandler()->ProcessEvent(gridEvt
);
5329 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5331 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5335 // This is just here to make sure that CalcDimensions gets called when
5336 // the grid view is resized... then the size event is skipped to allow
5337 // the box sizers to handle everything
5339 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5346 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5348 if ( m_inOnKeyDown
)
5350 // shouldn't be here - we are going round in circles...
5352 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5355 m_inOnKeyDown
= TRUE
;
5357 // propagate the event up and see if it gets processed
5359 wxWindow
*parent
= GetParent();
5360 wxKeyEvent
keyEvt( event
);
5361 keyEvt
.SetEventObject( parent
);
5363 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5366 // try local handlers
5368 switch ( event
.KeyCode() )
5371 if ( event
.ControlDown() )
5373 MoveCursorUpBlock( event
.ShiftDown() );
5377 MoveCursorUp( event
.ShiftDown() );
5382 if ( event
.ControlDown() )
5384 MoveCursorDownBlock( event
.ShiftDown() );
5388 MoveCursorDown( event
.ShiftDown() );
5393 if ( event
.ControlDown() )
5395 MoveCursorLeftBlock( event
.ShiftDown() );
5399 MoveCursorLeft( event
.ShiftDown() );
5404 if ( event
.ControlDown() )
5406 MoveCursorRightBlock( event
.ShiftDown() );
5410 MoveCursorRight( event
.ShiftDown() );
5415 if ( event
.ControlDown() )
5417 event
.Skip(); // to let the edit control have the return
5421 if ( GetGridCursorRow() < GetNumberRows()-1 )
5423 MoveCursorDown( event
.ShiftDown() );
5427 // at the bottom of a column
5428 HideCellEditControl();
5429 SaveEditControlValue();
5439 if (event
.ShiftDown())
5441 if ( GetGridCursorCol() > 0 )
5443 MoveCursorLeft( FALSE
);
5448 HideCellEditControl();
5449 SaveEditControlValue();
5454 if ( GetGridCursorCol() < GetNumberCols()-1 )
5456 MoveCursorRight( FALSE
);
5461 HideCellEditControl();
5462 SaveEditControlValue();
5468 if ( event
.ControlDown() )
5470 MakeCellVisible( 0, 0 );
5471 SetCurrentCell( 0, 0 );
5480 if ( event
.ControlDown() )
5482 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5483 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5500 if ( event
.ControlDown() )
5502 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5503 m_currentCellCoords
.GetCol(),
5504 event
.ControlDown(),
5510 if ( !IsEditable() )
5512 MoveCursorRight( FALSE
);
5515 // Otherwise fall through to default
5518 // is it possible to edit the current cell at all?
5519 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5521 // yes, now check whether the cells editor accepts the key
5522 int row
= m_currentCellCoords
.GetRow();
5523 int col
= m_currentCellCoords
.GetCol();
5524 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5525 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5527 // <F2> is special and will always start editing, for
5528 // other keys - ask the editor itself
5529 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5530 || editor
->IsAcceptedKey(event
) )
5532 EnableCellEditControl();
5533 editor
->StartingKey(event
);
5545 // let others process char events with modifiers or all
5546 // char events for readonly cells
5553 m_inOnKeyDown
= FALSE
;
5556 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5558 // try local handlers
5560 if ( event
.KeyCode() == WXK_SHIFT
)
5562 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5563 m_selectingBottomRight
!= wxGridNoCellCoords
)
5564 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5565 m_selectingTopLeft
.GetCol(),
5566 m_selectingBottomRight
.GetRow(),
5567 m_selectingBottomRight
.GetCol(),
5568 event
.ControlDown(),
5572 m_selectingTopLeft
= wxGridNoCellCoords
;
5573 m_selectingBottomRight
= wxGridNoCellCoords
;
5574 m_selectingKeyboard
= wxGridNoCellCoords
;
5578 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5582 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5584 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5586 // the event has been intercepted - do nothing
5590 wxClientDC
dc(m_gridWin
);
5593 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5595 HideCellEditControl();
5596 DisableCellEditControl();
5598 if ( IsVisible( m_currentCellCoords
, FALSE
) ) // zzz
5601 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5602 if ( !m_gridLinesEnabled
)
5610 CalcCellsExposed( r
);
5612 // Otherwise refresh redraws the highlight!
5613 m_currentCellCoords
= coords
;
5615 DrawGridCellArea(dc
);
5616 DrawAllGridLines( dc
, r
);
5620 m_currentCellCoords
= coords
;
5622 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5623 DrawCellHighlight(dc
, attr
);
5628 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5631 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5633 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5636 rightCol
= GetNumberCols() - 1;
5638 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5641 bottomRow
= GetNumberRows() - 1;
5643 if ( topRow
> bottomRow
)
5650 if ( leftCol
> rightCol
)
5657 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5658 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5660 if ( m_selectingTopLeft
!= updateTopLeft
||
5661 m_selectingBottomRight
!= updateBottomRight
)
5663 // Compute two optimal update rectangles:
5664 // Either one rectangle is a real subset of the
5665 // other, or they are (almost) disjoint!
5667 bool need_refresh
[4];
5671 need_refresh
[3] = FALSE
;
5674 // Store intermediate values
5675 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5676 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5677 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5678 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5680 // Determine the outer/inner coordinates.
5681 if (oldLeft
> leftCol
)
5687 if (oldTop
> topRow
)
5693 if (oldRight
< rightCol
)
5696 oldRight
= rightCol
;
5699 if (oldBottom
< bottomRow
)
5702 oldBottom
= bottomRow
;
5706 // Now, either the stuff marked old is the outer
5707 // rectangle or we don't have a situation where one
5708 // is contained in the other.
5710 if ( oldLeft
< leftCol
)
5712 need_refresh
[0] = TRUE
;
5713 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5715 wxGridCellCoords ( oldBottom
,
5719 if ( oldTop
< topRow
)
5721 need_refresh
[1] = TRUE
;
5722 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5724 wxGridCellCoords ( topRow
- 1,
5728 if ( oldRight
> rightCol
)
5730 need_refresh
[2] = TRUE
;
5731 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5733 wxGridCellCoords ( oldBottom
,
5737 if ( oldBottom
> bottomRow
)
5739 need_refresh
[3] = TRUE
;
5740 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5742 wxGridCellCoords ( oldBottom
,
5748 m_selectingTopLeft
= updateTopLeft
;
5749 m_selectingBottomRight
= updateBottomRight
;
5751 // various Refresh() calls
5752 for (i
= 0; i
< 4; i
++ )
5753 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5754 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5757 // never generate an event as it will be generated from
5758 // wxGridSelection::SelectBlock!
5759 // (old comment from when this was the body of SelectBlock)
5764 // ------ functions to get/send data (see also public functions)
5767 bool wxGrid::GetModelValues()
5771 // all we need to do is repaint the grid
5773 m_gridWin
->Refresh();
5781 bool wxGrid::SetModelValues()
5787 for ( row
= 0; row
< m_numRows
; row
++ )
5789 for ( col
= 0; col
< m_numCols
; col
++ )
5791 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5803 // Note - this function only draws cells that are in the list of
5804 // exposed cells (usually set from the update region by
5805 // CalcExposedCells)
5807 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5809 if ( !m_numRows
|| !m_numCols
) return;
5812 size_t numCells
= m_cellsExposed
.GetCount();
5814 for ( i
= 0; i
< numCells
; i
++ )
5816 DrawCell( dc
, m_cellsExposed
[i
] );
5821 void wxGrid::DrawGridSpace( wxDC
& dc
)
5824 m_gridWin
->GetClientSize( &cw
, &ch
);
5827 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5829 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5830 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5832 if ( right
> rightCol
|| bottom
> bottomRow
)
5835 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5837 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5838 dc
.SetPen( *wxTRANSPARENT_PEN
);
5840 if ( right
> rightCol
)
5842 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5845 if ( bottom
> bottomRow
)
5847 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5853 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5855 int row
= coords
.GetRow();
5856 int col
= coords
.GetCol();
5858 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5861 // we draw the cell border ourselves
5862 #if !WXGRID_DRAW_LINES
5863 if ( m_gridLinesEnabled
)
5864 DrawCellBorder( dc
, coords
);
5867 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5869 bool isCurrent
= coords
== m_currentCellCoords
;
5871 wxRect rect
= CellToRect( row
, col
);
5873 // if the editor is shown, we should use it and not the renderer
5874 // Note: However, only if it is really _shown_, i.e. not hidden!
5875 if ( isCurrent
&& IsCellEditControlShown() )
5877 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5878 editor
->PaintBackground(rect
, attr
);
5883 // but all the rest is drawn by the cell renderer and hence may be
5885 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5886 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5893 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5895 int row
= m_currentCellCoords
.GetRow();
5896 int col
= m_currentCellCoords
.GetCol();
5898 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5901 wxRect rect
= CellToRect(row
, col
);
5903 // hmmm... what could we do here to show that the cell is disabled?
5904 // for now, I just draw a thinner border than for the other ones, but
5905 // it doesn't look really good
5906 dc
.SetPen(wxPen(m_cellHighlightColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5907 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5909 dc
.DrawRectangle(rect
);
5912 // VZ: my experiments with 3d borders...
5914 // how to properly set colours for arbitrary bg?
5915 wxCoord x1
= rect
.x
,
5917 x2
= rect
.x
+ rect
.width
-1,
5918 y2
= rect
.y
+ rect
.height
-1;
5920 dc
.SetPen(*wxWHITE_PEN
);
5921 dc
.DrawLine(x1
, y1
, x2
, y1
);
5922 dc
.DrawLine(x1
, y1
, x1
, y2
);
5924 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5925 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5927 dc
.SetPen(*wxBLACK_PEN
);
5928 dc
.DrawLine(x1
, y2
, x2
, y2
);
5929 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5934 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5936 int row
= coords
.GetRow();
5937 int col
= coords
.GetCol();
5938 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5941 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5943 // right hand border
5945 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5946 GetColRight(col
), GetRowBottom(row
) );
5950 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5951 GetColRight(col
), GetRowBottom(row
) );
5954 void wxGrid::DrawHighlight(wxDC
& dc
)
5956 // This if block was previously in wxGrid::OnPaint but that doesn't
5957 // seem to get called under wxGTK - MB
5959 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5960 m_numRows
&& m_numCols
)
5962 m_currentCellCoords
.Set(0, 0);
5965 if ( IsCellEditControlShown() )
5967 // don't show highlight when the edit control is shown
5971 // if the active cell was repainted, repaint its highlight too because it
5972 // might have been damaged by the grid lines
5973 size_t count
= m_cellsExposed
.GetCount();
5974 for ( size_t n
= 0; n
< count
; n
++ )
5976 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5978 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5979 DrawCellHighlight(dc
, attr
);
5987 // TODO: remove this ???
5988 // This is used to redraw all grid lines e.g. when the grid line colour
5991 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED_GTK(reg
) )
5993 if ( !m_gridLinesEnabled
||
5995 !m_numCols
) return;
5997 int top
, bottom
, left
, right
;
5999 #if 0 //#ifndef __WXGTK__
6003 m_gridWin
->GetClientSize(&cw
, &ch
);
6005 // virtual coords of visible area
6007 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6008 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6013 reg
.GetBox(x
, y
, w
, h
);
6014 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6015 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6019 m_gridWin
->GetClientSize(&cw
, &ch
);
6020 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6021 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6024 // avoid drawing grid lines past the last row and col
6026 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6027 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6029 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6031 // horizontal grid lines
6034 for ( i
= 0; i
< m_numRows
; i
++ )
6036 int bot
= GetRowBottom(i
) - 1;
6045 dc
.DrawLine( left
, bot
, right
, bot
);
6050 // vertical grid lines
6052 for ( i
= 0; i
< m_numCols
; i
++ )
6054 int colRight
= GetColRight(i
) - 1;
6055 if ( colRight
> right
)
6060 if ( colRight
>= left
)
6062 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6068 void wxGrid::DrawRowLabels( wxDC
& dc
)
6070 if ( !m_numRows
) return;
6073 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6075 for ( i
= 0; i
< numLabels
; i
++ )
6077 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6082 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6084 if ( GetRowHeight(row
) <= 0 )
6087 int rowTop
= GetRowTop(row
),
6088 rowBottom
= GetRowBottom(row
) - 1;
6090 dc
.SetPen( *wxBLACK_PEN
);
6091 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6092 m_rowLabelWidth
-1, rowBottom
);
6094 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6096 dc
.SetPen( *wxWHITE_PEN
);
6097 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6098 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6100 dc
.SetBackgroundMode( wxTRANSPARENT
);
6101 dc
.SetTextForeground( GetLabelTextColour() );
6102 dc
.SetFont( GetLabelFont() );
6105 GetRowLabelAlignment( &hAlign
, &vAlign
);
6109 rect
.SetY( GetRowTop(row
) + 2 );
6110 rect
.SetWidth( m_rowLabelWidth
- 4 );
6111 rect
.SetHeight( GetRowHeight(row
) - 4 );
6112 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6116 void wxGrid::DrawColLabels( wxDC
& dc
)
6118 if ( !m_numCols
) return;
6121 size_t numLabels
= m_colLabelsExposed
.GetCount();
6123 for ( i
= 0; i
< numLabels
; i
++ )
6125 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6130 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6132 if ( GetColWidth(col
) <= 0 )
6135 int colLeft
= GetColLeft(col
),
6136 colRight
= GetColRight(col
) - 1;
6138 dc
.SetPen( *wxBLACK_PEN
);
6139 dc
.DrawLine( colRight
, 0,
6140 colRight
, m_colLabelHeight
-1 );
6142 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6143 colRight
, m_colLabelHeight
-1 );
6145 dc
.SetPen( *wxWHITE_PEN
);
6146 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6147 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6149 dc
.SetBackgroundMode( wxTRANSPARENT
);
6150 dc
.SetTextForeground( GetLabelTextColour() );
6151 dc
.SetFont( GetLabelFont() );
6153 dc
.SetBackgroundMode( wxTRANSPARENT
);
6154 dc
.SetTextForeground( GetLabelTextColour() );
6155 dc
.SetFont( GetLabelFont() );
6158 GetColLabelAlignment( &hAlign
, &vAlign
);
6161 rect
.SetX( colLeft
+ 2 );
6163 rect
.SetWidth( GetColWidth(col
) - 4 );
6164 rect
.SetHeight( m_colLabelHeight
- 4 );
6165 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6169 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6170 const wxString
& value
,
6175 long textWidth
, textHeight
;
6176 long lineWidth
, lineHeight
;
6177 wxArrayString lines
;
6179 dc
.SetClippingRegion( rect
);
6180 StringToLines( value
, lines
);
6181 if ( lines
.GetCount() )
6183 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6184 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6187 switch ( horizAlign
)
6190 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6193 case wxALIGN_CENTRE
:
6194 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6203 switch ( vertAlign
)
6205 case wxALIGN_BOTTOM
:
6206 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6209 case wxALIGN_CENTRE
:
6210 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6219 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6221 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6226 dc
.DestroyClippingRegion();
6230 // Split multi line text up into an array of strings. Any existing
6231 // contents of the string array are preserved.
6233 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6237 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6238 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6240 while ( startPos
< (int)tVal
.Length() )
6242 pos
= tVal
.Mid(startPos
).Find( eol
);
6247 else if ( pos
== 0 )
6249 lines
.Add( wxEmptyString
);
6253 lines
.Add( value
.Mid(startPos
, pos
) );
6257 if ( startPos
< (int)value
.Length() )
6259 lines
.Add( value
.Mid( startPos
) );
6264 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6265 wxArrayString
& lines
,
6266 long *width
, long *height
)
6273 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6275 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6276 w
= wxMax( w
, lineW
);
6285 // ------ Batch processing.
6287 void wxGrid::EndBatch()
6289 if ( m_batchCount
> 0 )
6292 if ( !m_batchCount
)
6295 m_rowLabelWin
->Refresh();
6296 m_colLabelWin
->Refresh();
6297 m_cornerLabelWin
->Refresh();
6298 m_gridWin
->Refresh();
6303 // Use this, rather than wxWindow::Refresh(), to force an immediate
6304 // repainting of the grid. Has no effect if you are already inside a
6305 // BeginBatch / EndBatch block.
6307 void wxGrid::ForceRefresh()
6315 // ------ Edit control functions
6319 void wxGrid::EnableEditing( bool edit
)
6321 // TODO: improve this ?
6323 if ( edit
!= m_editable
)
6325 if(!edit
) EnableCellEditControl(edit
);
6331 void wxGrid::EnableCellEditControl( bool enable
)
6336 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6337 SetCurrentCell( 0, 0 );
6339 if ( enable
!= m_cellEditCtrlEnabled
)
6341 // TODO allow the app to Veto() this event?
6342 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6346 // this should be checked by the caller!
6347 wxASSERT_MSG( CanEnableCellControl(),
6348 _T("can't enable editing for this cell!") );
6350 // do it before ShowCellEditControl()
6351 m_cellEditCtrlEnabled
= enable
;
6353 ShowCellEditControl();
6357 HideCellEditControl();
6358 SaveEditControlValue();
6360 // do it after HideCellEditControl()
6361 m_cellEditCtrlEnabled
= enable
;
6366 bool wxGrid::IsCurrentCellReadOnly() const
6369 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6370 bool readonly
= attr
->IsReadOnly();
6376 bool wxGrid::CanEnableCellControl() const
6378 return m_editable
&& !IsCurrentCellReadOnly();
6381 bool wxGrid::IsCellEditControlEnabled() const
6383 // the cell edit control might be disable for all cells or just for the
6384 // current one if it's read only
6385 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6388 bool wxGrid::IsCellEditControlShown() const
6390 bool isShown
= FALSE
;
6392 if ( m_cellEditCtrlEnabled
)
6394 int row
= m_currentCellCoords
.GetRow();
6395 int col
= m_currentCellCoords
.GetCol();
6396 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6397 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6402 if ( editor
->IsCreated() )
6404 isShown
= editor
->GetControl()->IsShown();
6414 void wxGrid::ShowCellEditControl()
6416 if ( IsCellEditControlEnabled() )
6418 if ( !IsVisible( m_currentCellCoords
) )
6420 m_cellEditCtrlEnabled
= false;
6425 wxRect rect
= CellToRect( m_currentCellCoords
);
6426 int row
= m_currentCellCoords
.GetRow();
6427 int col
= m_currentCellCoords
.GetCol();
6429 // convert to scrolled coords
6431 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6433 // done in PaintBackground()
6435 // erase the highlight and the cell contents because the editor
6436 // might not cover the entire cell
6437 wxClientDC
dc( m_gridWin
);
6439 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6440 dc
.SetPen(*wxTRANSPARENT_PEN
);
6441 dc
.DrawRectangle(rect
);
6444 // cell is shifted by one pixel
6448 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6449 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6450 if ( !editor
->IsCreated() )
6452 editor
->Create(m_gridWin
, -1,
6453 new wxGridCellEditorEvtHandler(this, editor
));
6456 editor
->Show( TRUE
, attr
);
6458 editor
->SetSize( rect
);
6460 editor
->BeginEdit(row
, col
, this);
6469 void wxGrid::HideCellEditControl()
6471 if ( IsCellEditControlEnabled() )
6473 int row
= m_currentCellCoords
.GetRow();
6474 int col
= m_currentCellCoords
.GetCol();
6476 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6477 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6478 editor
->Show( FALSE
);
6481 m_gridWin
->SetFocus();
6482 wxRect
rect( CellToRect( row
, col
) );
6483 m_gridWin
->Refresh( FALSE
, &rect
);
6488 void wxGrid::SaveEditControlValue()
6490 if ( IsCellEditControlEnabled() )
6492 int row
= m_currentCellCoords
.GetRow();
6493 int col
= m_currentCellCoords
.GetCol();
6495 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6496 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6497 bool changed
= editor
->EndEdit(row
, col
, this);
6504 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6505 m_currentCellCoords
.GetRow(),
6506 m_currentCellCoords
.GetCol() );
6513 // ------ Grid location functions
6514 // Note that all of these functions work with the logical coordinates of
6515 // grid cells and labels so you will need to convert from device
6516 // coordinates for mouse events etc.
6519 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6521 int row
= YToRow(y
);
6522 int col
= XToCol(x
);
6524 if ( row
== -1 || col
== -1 )
6526 coords
= wxGridNoCellCoords
;
6530 coords
.Set( row
, col
);
6535 int wxGrid::YToRow( int y
)
6539 for ( i
= 0; i
< m_numRows
; i
++ )
6541 if ( y
< GetRowBottom(i
) )
6549 int wxGrid::XToCol( int x
)
6553 for ( i
= 0; i
< m_numCols
; i
++ )
6555 if ( x
< GetColRight(i
) )
6563 // return the row number that that the y coord is near the edge of, or
6564 // -1 if not near an edge
6566 int wxGrid::YToEdgeOfRow( int y
)
6570 for ( i
= 0; i
< m_numRows
; i
++ )
6572 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6574 d
= abs( y
- GetRowBottom(i
) );
6575 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6584 // return the col number that that the x coord is near the edge of, or
6585 // -1 if not near an edge
6587 int wxGrid::XToEdgeOfCol( int x
)
6591 for ( i
= 0; i
< m_numCols
; i
++ )
6593 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6595 d
= abs( x
- GetColRight(i
) );
6596 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6605 wxRect
wxGrid::CellToRect( int row
, int col
)
6607 wxRect
rect( -1, -1, -1, -1 );
6609 if ( row
>= 0 && row
< m_numRows
&&
6610 col
>= 0 && col
< m_numCols
)
6612 rect
.x
= GetColLeft(col
);
6613 rect
.y
= GetRowTop(row
);
6614 rect
.width
= GetColWidth(col
);
6615 rect
.height
= GetRowHeight(row
);
6618 // if grid lines are enabled, then the area of the cell is a bit smaller
6619 if (m_gridLinesEnabled
) {
6627 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6629 // get the cell rectangle in logical coords
6631 wxRect
r( CellToRect( row
, col
) );
6633 // convert to device coords
6635 int left
, top
, right
, bottom
;
6636 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6637 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6639 // check against the client area of the grid window
6642 m_gridWin
->GetClientSize( &cw
, &ch
);
6644 if ( wholeCellVisible
)
6646 // is the cell wholly visible ?
6648 return ( left
>= 0 && right
<= cw
&&
6649 top
>= 0 && bottom
<= ch
);
6653 // is the cell partly visible ?
6655 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6656 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6661 // make the specified cell location visible by doing a minimal amount
6664 void wxGrid::MakeCellVisible( int row
, int col
)
6667 int xpos
= -1, ypos
= -1;
6669 if ( row
>= 0 && row
< m_numRows
&&
6670 col
>= 0 && col
< m_numCols
)
6672 // get the cell rectangle in logical coords
6674 wxRect
r( CellToRect( row
, col
) );
6676 // convert to device coords
6678 int left
, top
, right
, bottom
;
6679 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6680 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6683 m_gridWin
->GetClientSize( &cw
, &ch
);
6689 else if ( bottom
> ch
)
6691 int h
= r
.GetHeight();
6693 for ( i
= row
-1; i
>= 0; i
-- )
6695 int rowHeight
= GetRowHeight(i
);
6696 if ( h
+ rowHeight
> ch
)
6703 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6704 // have rounding errors (this is important, because if we do, we
6705 // might not scroll at all and some cells won't be redrawn)
6706 ypos
+= GRID_SCROLL_LINE
/ 2;
6713 else if ( right
> cw
)
6715 int w
= r
.GetWidth();
6717 for ( i
= col
-1; i
>= 0; i
-- )
6719 int colWidth
= GetColWidth(i
);
6720 if ( w
+ colWidth
> cw
)
6727 // see comment for ypos above
6728 xpos
+= GRID_SCROLL_LINE
/ 2;
6731 if ( xpos
!= -1 || ypos
!= -1 )
6733 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6734 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6735 Scroll( xpos
, ypos
);
6743 // ------ Grid cursor movement functions
6746 bool wxGrid::MoveCursorUp( bool expandSelection
)
6748 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6749 m_currentCellCoords
.GetRow() >= 0 )
6751 if ( expandSelection
)
6753 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6754 m_selectingKeyboard
= m_currentCellCoords
;
6755 if ( m_selectingKeyboard
.GetRow() > 0 )
6757 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6758 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6759 m_selectingKeyboard
.GetCol() );
6760 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6763 else if ( m_currentCellCoords
.GetRow() > 0 )
6766 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6767 m_currentCellCoords
.GetCol() );
6768 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6769 m_currentCellCoords
.GetCol() );
6780 bool wxGrid::MoveCursorDown( bool expandSelection
)
6782 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6783 m_currentCellCoords
.GetRow() < m_numRows
)
6785 if ( expandSelection
)
6787 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6788 m_selectingKeyboard
= m_currentCellCoords
;
6789 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6791 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6792 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6793 m_selectingKeyboard
.GetCol() );
6794 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6797 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6800 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6801 m_currentCellCoords
.GetCol() );
6802 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6803 m_currentCellCoords
.GetCol() );
6814 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6816 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6817 m_currentCellCoords
.GetCol() >= 0 )
6819 if ( expandSelection
)
6821 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6822 m_selectingKeyboard
= m_currentCellCoords
;
6823 if ( m_selectingKeyboard
.GetCol() > 0 )
6825 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6826 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6827 m_selectingKeyboard
.GetCol() );
6828 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6831 else if ( m_currentCellCoords
.GetCol() > 0 )
6834 MakeCellVisible( m_currentCellCoords
.GetRow(),
6835 m_currentCellCoords
.GetCol() - 1 );
6836 SetCurrentCell( m_currentCellCoords
.GetRow(),
6837 m_currentCellCoords
.GetCol() - 1 );
6848 bool wxGrid::MoveCursorRight( bool expandSelection
)
6850 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6851 m_currentCellCoords
.GetCol() < m_numCols
)
6853 if ( expandSelection
)
6855 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6856 m_selectingKeyboard
= m_currentCellCoords
;
6857 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6859 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6860 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6861 m_selectingKeyboard
.GetCol() );
6862 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6865 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6868 MakeCellVisible( m_currentCellCoords
.GetRow(),
6869 m_currentCellCoords
.GetCol() + 1 );
6870 SetCurrentCell( m_currentCellCoords
.GetRow(),
6871 m_currentCellCoords
.GetCol() + 1 );
6882 bool wxGrid::MovePageUp()
6884 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6886 int row
= m_currentCellCoords
.GetRow();
6890 m_gridWin
->GetClientSize( &cw
, &ch
);
6892 int y
= GetRowTop(row
);
6893 int newRow
= YToRow( y
- ch
+ 1 );
6898 else if ( newRow
== row
)
6903 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6904 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6912 bool wxGrid::MovePageDown()
6914 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6916 int row
= m_currentCellCoords
.GetRow();
6917 if ( row
< m_numRows
)
6920 m_gridWin
->GetClientSize( &cw
, &ch
);
6922 int y
= GetRowTop(row
);
6923 int newRow
= YToRow( y
+ ch
);
6926 newRow
= m_numRows
- 1;
6928 else if ( newRow
== row
)
6933 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6934 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6942 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6945 m_currentCellCoords
!= wxGridNoCellCoords
&&
6946 m_currentCellCoords
.GetRow() > 0 )
6948 int row
= m_currentCellCoords
.GetRow();
6949 int col
= m_currentCellCoords
.GetCol();
6951 if ( m_table
->IsEmptyCell(row
, col
) )
6953 // starting in an empty cell: find the next block of
6959 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6962 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6964 // starting at the top of a block: find the next block
6970 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6975 // starting within a block: find the top of the block
6980 if ( m_table
->IsEmptyCell(row
, col
) )
6988 MakeCellVisible( row
, col
);
6989 if ( expandSelection
)
6991 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
6992 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6997 SetCurrentCell( row
, col
);
7005 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7008 m_currentCellCoords
!= wxGridNoCellCoords
&&
7009 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7011 int row
= m_currentCellCoords
.GetRow();
7012 int col
= m_currentCellCoords
.GetCol();
7014 if ( m_table
->IsEmptyCell(row
, col
) )
7016 // starting in an empty cell: find the next block of
7019 while ( row
< m_numRows
-1 )
7022 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7025 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7027 // starting at the bottom of a block: find the next block
7030 while ( row
< m_numRows
-1 )
7033 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7038 // starting within a block: find the bottom of the block
7040 while ( row
< m_numRows
-1 )
7043 if ( m_table
->IsEmptyCell(row
, col
) )
7051 MakeCellVisible( row
, col
);
7052 if ( expandSelection
)
7054 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7055 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7060 SetCurrentCell( row
, col
);
7069 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7072 m_currentCellCoords
!= wxGridNoCellCoords
&&
7073 m_currentCellCoords
.GetCol() > 0 )
7075 int row
= m_currentCellCoords
.GetRow();
7076 int col
= m_currentCellCoords
.GetCol();
7078 if ( m_table
->IsEmptyCell(row
, col
) )
7080 // starting in an empty cell: find the next block of
7086 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7089 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7091 // starting at the left of a block: find the next block
7097 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7102 // starting within a block: find the left of the block
7107 if ( m_table
->IsEmptyCell(row
, col
) )
7115 MakeCellVisible( row
, col
);
7116 if ( expandSelection
)
7118 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7119 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7124 SetCurrentCell( row
, col
);
7133 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7136 m_currentCellCoords
!= wxGridNoCellCoords
&&
7137 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7139 int row
= m_currentCellCoords
.GetRow();
7140 int col
= m_currentCellCoords
.GetCol();
7142 if ( m_table
->IsEmptyCell(row
, col
) )
7144 // starting in an empty cell: find the next block of
7147 while ( col
< m_numCols
-1 )
7150 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7153 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7155 // starting at the right of a block: find the next block
7158 while ( col
< m_numCols
-1 )
7161 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7166 // starting within a block: find the right of the block
7168 while ( col
< m_numCols
-1 )
7171 if ( m_table
->IsEmptyCell(row
, col
) )
7179 MakeCellVisible( row
, col
);
7180 if ( expandSelection
)
7182 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7183 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7188 SetCurrentCell( row
, col
);
7200 // ------ Label values and formatting
7203 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7205 *horiz
= m_rowLabelHorizAlign
;
7206 *vert
= m_rowLabelVertAlign
;
7209 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7211 *horiz
= m_colLabelHorizAlign
;
7212 *vert
= m_colLabelVertAlign
;
7215 wxString
wxGrid::GetRowLabelValue( int row
)
7219 return m_table
->GetRowLabelValue( row
);
7229 wxString
wxGrid::GetColLabelValue( int col
)
7233 return m_table
->GetColLabelValue( col
);
7244 void wxGrid::SetRowLabelSize( int width
)
7246 width
= wxMax( width
, 0 );
7247 if ( width
!= m_rowLabelWidth
)
7251 m_rowLabelWin
->Show( FALSE
);
7252 m_cornerLabelWin
->Show( FALSE
);
7254 else if ( m_rowLabelWidth
== 0 )
7256 m_rowLabelWin
->Show( TRUE
);
7257 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7260 m_rowLabelWidth
= width
;
7267 void wxGrid::SetColLabelSize( int height
)
7269 height
= wxMax( height
, 0 );
7270 if ( height
!= m_colLabelHeight
)
7274 m_colLabelWin
->Show( FALSE
);
7275 m_cornerLabelWin
->Show( FALSE
);
7277 else if ( m_colLabelHeight
== 0 )
7279 m_colLabelWin
->Show( TRUE
);
7280 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7283 m_colLabelHeight
= height
;
7290 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7292 if ( m_labelBackgroundColour
!= colour
)
7294 m_labelBackgroundColour
= colour
;
7295 m_rowLabelWin
->SetBackgroundColour( colour
);
7296 m_colLabelWin
->SetBackgroundColour( colour
);
7297 m_cornerLabelWin
->SetBackgroundColour( colour
);
7299 if ( !GetBatchCount() )
7301 m_rowLabelWin
->Refresh();
7302 m_colLabelWin
->Refresh();
7303 m_cornerLabelWin
->Refresh();
7308 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7310 if ( m_labelTextColour
!= colour
)
7312 m_labelTextColour
= colour
;
7313 if ( !GetBatchCount() )
7315 m_rowLabelWin
->Refresh();
7316 m_colLabelWin
->Refresh();
7321 void wxGrid::SetLabelFont( const wxFont
& font
)
7324 if ( !GetBatchCount() )
7326 m_rowLabelWin
->Refresh();
7327 m_colLabelWin
->Refresh();
7331 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7333 // allow old (incorrect) defs to be used
7336 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7337 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7338 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7343 case wxTOP
: vert
= wxALIGN_TOP
; break;
7344 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7345 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7348 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7350 m_rowLabelHorizAlign
= horiz
;
7353 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7355 m_rowLabelVertAlign
= vert
;
7358 if ( !GetBatchCount() )
7360 m_rowLabelWin
->Refresh();
7364 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7366 // allow old (incorrect) defs to be used
7369 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7370 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7371 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7376 case wxTOP
: vert
= wxALIGN_TOP
; break;
7377 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7378 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7381 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7383 m_colLabelHorizAlign
= horiz
;
7386 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7388 m_colLabelVertAlign
= vert
;
7391 if ( !GetBatchCount() )
7393 m_colLabelWin
->Refresh();
7397 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7401 m_table
->SetRowLabelValue( row
, s
);
7402 if ( !GetBatchCount() )
7404 wxRect rect
= CellToRect( row
, 0);
7405 if ( rect
.height
> 0 )
7407 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7409 rect
.width
= m_rowLabelWidth
;
7410 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7416 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7420 m_table
->SetColLabelValue( col
, s
);
7421 if ( !GetBatchCount() )
7423 wxRect rect
= CellToRect( 0, col
);
7424 if ( rect
.width
> 0 )
7426 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7428 rect
.height
= m_colLabelHeight
;
7429 m_colLabelWin
->Refresh( TRUE
, &rect
);
7435 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7437 if ( m_gridLineColour
!= colour
)
7439 m_gridLineColour
= colour
;
7441 wxClientDC
dc( m_gridWin
);
7443 DrawAllGridLines( dc
, wxRegion() );
7448 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7450 if ( m_cellHighlightColour
!= colour
)
7452 m_cellHighlightColour
= colour
;
7454 wxClientDC
dc( m_gridWin
);
7456 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7457 DrawCellHighlight(dc
, attr
);
7462 void wxGrid::EnableGridLines( bool enable
)
7464 if ( enable
!= m_gridLinesEnabled
)
7466 m_gridLinesEnabled
= enable
;
7468 if ( !GetBatchCount() )
7472 wxClientDC
dc( m_gridWin
);
7474 DrawAllGridLines( dc
, wxRegion() );
7478 m_gridWin
->Refresh();
7485 int wxGrid::GetDefaultRowSize()
7487 return m_defaultRowHeight
;
7490 int wxGrid::GetRowSize( int row
)
7492 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7494 return GetRowHeight(row
);
7497 int wxGrid::GetDefaultColSize()
7499 return m_defaultColWidth
;
7502 int wxGrid::GetColSize( int col
)
7504 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7506 return GetColWidth(col
);
7509 // ============================================================================
7510 // access to the grid attributes: each of them has a default value in the grid
7511 // itself and may be overidden on a per-cell basis
7512 // ============================================================================
7514 // ----------------------------------------------------------------------------
7515 // setting default attributes
7516 // ----------------------------------------------------------------------------
7518 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7520 m_defaultCellAttr
->SetBackgroundColour(col
);
7522 m_gridWin
->SetBackgroundColour(col
);
7526 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7528 m_defaultCellAttr
->SetTextColour(col
);
7531 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7533 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7536 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7538 m_defaultCellAttr
->SetFont(font
);
7541 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7543 m_defaultCellAttr
->SetRenderer(renderer
);
7546 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7548 m_defaultCellAttr
->SetEditor(editor
);
7551 // ----------------------------------------------------------------------------
7552 // access to the default attrbiutes
7553 // ----------------------------------------------------------------------------
7555 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7557 return m_defaultCellAttr
->GetBackgroundColour();
7560 wxColour
wxGrid::GetDefaultCellTextColour()
7562 return m_defaultCellAttr
->GetTextColour();
7565 wxFont
wxGrid::GetDefaultCellFont()
7567 return m_defaultCellAttr
->GetFont();
7570 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7572 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7575 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7577 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7580 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7582 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7585 // ----------------------------------------------------------------------------
7586 // access to cell attributes
7587 // ----------------------------------------------------------------------------
7589 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7591 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7592 wxColour colour
= attr
->GetBackgroundColour();
7597 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7599 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7600 wxColour colour
= attr
->GetTextColour();
7605 wxFont
wxGrid::GetCellFont( int row
, int col
)
7607 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7608 wxFont font
= attr
->GetFont();
7613 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7615 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7616 attr
->GetAlignment(horiz
, vert
);
7620 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7622 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7623 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7629 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7631 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7632 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7638 bool wxGrid::IsReadOnly(int row
, int col
) const
7640 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7641 bool isReadOnly
= attr
->IsReadOnly();
7646 // ----------------------------------------------------------------------------
7647 // attribute support: cache, automatic provider creation, ...
7648 // ----------------------------------------------------------------------------
7650 bool wxGrid::CanHaveAttributes()
7657 return m_table
->CanHaveAttributes();
7660 void wxGrid::ClearAttrCache()
7662 if ( m_attrCache
.row
!= -1 )
7664 wxSafeDecRef(m_attrCache
.attr
);
7665 m_attrCache
.row
= -1;
7669 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7671 wxGrid
*self
= (wxGrid
*)this; // const_cast
7673 self
->ClearAttrCache();
7674 self
->m_attrCache
.row
= row
;
7675 self
->m_attrCache
.col
= col
;
7676 self
->m_attrCache
.attr
= attr
;
7680 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7682 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7684 *attr
= m_attrCache
.attr
;
7685 wxSafeIncRef(m_attrCache
.attr
);
7687 #ifdef DEBUG_ATTR_CACHE
7688 gs_nAttrCacheHits
++;
7695 #ifdef DEBUG_ATTR_CACHE
7696 gs_nAttrCacheMisses
++;
7702 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7704 wxGridCellAttr
*attr
;
7705 if ( !LookupAttr(row
, col
, &attr
) )
7707 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7708 CacheAttr(row
, col
, attr
);
7712 attr
->SetDefAttr(m_defaultCellAttr
);
7716 attr
= m_defaultCellAttr
;
7723 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7725 wxGridCellAttr
*attr
;
7726 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7728 wxASSERT_MSG( m_table
,
7729 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7731 attr
= m_table
->GetAttr(row
, col
);
7734 attr
= new wxGridCellAttr
;
7736 // artificially inc the ref count to match DecRef() in caller
7739 m_table
->SetAttr(attr
, row
, col
);
7742 CacheAttr(row
, col
, attr
);
7744 attr
->SetDefAttr(m_defaultCellAttr
);
7748 // ----------------------------------------------------------------------------
7749 // setting column attributes (wrappers around SetColAttr)
7750 // ----------------------------------------------------------------------------
7752 void wxGrid::SetColFormatBool(int col
)
7754 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7757 void wxGrid::SetColFormatNumber(int col
)
7759 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7762 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7764 wxString typeName
= wxGRID_VALUE_FLOAT
;
7765 if ( (width
!= -1) || (precision
!= -1) )
7767 typeName
<< _T(':') << width
<< _T(',') << precision
;
7770 SetColFormatCustom(col
, typeName
);
7773 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7775 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7776 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7777 attr
->SetRenderer(renderer
);
7779 SetColAttr(col
, attr
);
7782 // ----------------------------------------------------------------------------
7783 // setting cell attributes: this is forwarded to the table
7784 // ----------------------------------------------------------------------------
7786 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7788 if ( CanHaveAttributes() )
7790 m_table
->SetRowAttr(attr
, row
);
7798 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7800 if ( CanHaveAttributes() )
7802 m_table
->SetColAttr(attr
, col
);
7810 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7812 if ( CanHaveAttributes() )
7814 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7815 attr
->SetBackgroundColour(colour
);
7820 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7822 if ( CanHaveAttributes() )
7824 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7825 attr
->SetTextColour(colour
);
7830 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7832 if ( CanHaveAttributes() )
7834 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7835 attr
->SetFont(font
);
7840 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7842 if ( CanHaveAttributes() )
7844 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7845 attr
->SetAlignment(horiz
, vert
);
7850 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7852 if ( CanHaveAttributes() )
7854 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7855 attr
->SetRenderer(renderer
);
7860 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7862 if ( CanHaveAttributes() )
7864 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7865 attr
->SetEditor(editor
);
7870 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7872 if ( CanHaveAttributes() )
7874 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7875 attr
->SetReadOnly(isReadOnly
);
7880 // ----------------------------------------------------------------------------
7881 // Data type registration
7882 // ----------------------------------------------------------------------------
7884 void wxGrid::RegisterDataType(const wxString
& typeName
,
7885 wxGridCellRenderer
* renderer
,
7886 wxGridCellEditor
* editor
)
7888 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7892 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7894 wxString typeName
= m_table
->GetTypeName(row
, col
);
7895 return GetDefaultEditorForType(typeName
);
7898 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7900 wxString typeName
= m_table
->GetTypeName(row
, col
);
7901 return GetDefaultRendererForType(typeName
);
7905 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7907 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7908 if ( index
== wxNOT_FOUND
)
7910 wxFAIL_MSG(wxT("Unknown data type name"));
7915 return m_typeRegistry
->GetEditor(index
);
7919 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7921 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7922 if ( index
== wxNOT_FOUND
)
7924 wxFAIL_MSG(wxT("Unknown data type name"));
7929 return m_typeRegistry
->GetRenderer(index
);
7933 // ----------------------------------------------------------------------------
7935 // ----------------------------------------------------------------------------
7937 void wxGrid::EnableDragRowSize( bool enable
)
7939 m_canDragRowSize
= enable
;
7943 void wxGrid::EnableDragColSize( bool enable
)
7945 m_canDragColSize
= enable
;
7948 void wxGrid::EnableDragGridSize( bool enable
)
7950 m_canDragGridSize
= enable
;
7954 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7956 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7958 if ( resizeExistingRows
)
7966 void wxGrid::SetRowSize( int row
, int height
)
7968 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7970 if ( m_rowHeights
.IsEmpty() )
7972 // need to really create the array
7976 int h
= wxMax( 0, height
);
7977 int diff
= h
- m_rowHeights
[row
];
7979 m_rowHeights
[row
] = h
;
7981 for ( i
= row
; i
< m_numRows
; i
++ )
7983 m_rowBottoms
[i
] += diff
;
7988 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7990 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7992 if ( resizeExistingCols
)
8000 void wxGrid::SetColSize( int col
, int width
)
8002 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8004 // should we check that it's bigger than GetColMinimalWidth(col) here?
8006 if ( m_colWidths
.IsEmpty() )
8008 // need to really create the array
8012 int w
= wxMax( 0, width
);
8013 int diff
= w
- m_colWidths
[col
];
8014 m_colWidths
[col
] = w
;
8017 for ( i
= col
; i
< m_numCols
; i
++ )
8019 m_colRights
[i
] += diff
;
8025 void wxGrid::SetColMinimalWidth( int col
, int width
)
8027 m_colMinWidths
.Put(col
, width
);
8030 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8032 m_rowMinHeights
.Put(row
, width
);
8035 int wxGrid::GetColMinimalWidth(int col
) const
8037 long value
= m_colMinWidths
.Get(col
);
8038 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8041 int wxGrid::GetRowMinimalHeight(int row
) const
8043 long value
= m_rowMinHeights
.Get(row
);
8044 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8047 // ----------------------------------------------------------------------------
8049 // ----------------------------------------------------------------------------
8051 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8053 wxClientDC
dc(m_gridWin
);
8055 // init both of them to avoid compiler warnings, even if weo nly need one
8063 wxCoord extent
, extentMax
= 0;
8064 int max
= column
? m_numRows
: m_numCols
;
8065 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8072 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8073 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8076 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8077 extent
= column
? size
.x
: size
.y
;
8078 if ( extent
> extentMax
)
8089 // now also compare with the column label extent
8091 dc
.SetFont( GetLabelFont() );
8094 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8096 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
8098 extent
= column
? w
: h
;
8099 if ( extent
> extentMax
)
8106 // empty column - give default extent (notice that if extentMax is less
8107 // than default extent but != 0, it's ok)
8108 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8114 // leave some space around text
8124 SetColSize(col
, extentMax
);
8126 SetRowSize(row
, extentMax
);
8131 SetColMinimalWidth(col
, extentMax
);
8133 SetRowMinimalHeight(row
, extentMax
);
8137 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8139 int width
= m_rowLabelWidth
;
8141 for ( int col
= 0; col
< m_numCols
; col
++ )
8145 AutoSizeColumn(col
, setAsMin
);
8148 width
+= GetColWidth(col
);
8154 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8156 int height
= m_colLabelHeight
;
8158 for ( int row
= 0; row
< m_numRows
; row
++ )
8162 AutoSizeRow(row
, setAsMin
);
8165 height
+= GetRowHeight(row
);
8171 void wxGrid::AutoSize()
8174 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8177 wxSize
wxGrid::DoGetBestSize() const
8179 // don't set sizes, only calculate them
8180 wxGrid
*self
= (wxGrid
*)this; // const_cast
8182 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8183 self
->SetOrCalcRowSizes(TRUE
));
8192 wxPen
& wxGrid::GetDividerPen() const
8197 // ----------------------------------------------------------------------------
8198 // cell value accessor functions
8199 // ----------------------------------------------------------------------------
8201 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8205 m_table
->SetValue( row
, col
, s
);
8206 if ( !GetBatchCount() )
8208 wxClientDC
dc( m_gridWin
);
8210 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8213 if ( m_currentCellCoords
.GetRow() == row
&&
8214 m_currentCellCoords
.GetCol() == col
&&
8215 IsCellEditControlShown())
8216 // Note: If we are using IsCellEditControlEnabled,
8217 // this interacts badly with calling SetCellValue from
8218 // an EVT_GRID_CELL_CHANGE handler.
8220 HideCellEditControl();
8221 ShowCellEditControl(); // will reread data from table
8228 // ------ Block, row and col selection
8231 void wxGrid::SelectRow( int row
, bool addToSelected
)
8233 if ( IsSelection() && !addToSelected
)
8236 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8240 void wxGrid::SelectCol( int col
, bool addToSelected
)
8242 if ( IsSelection() && !addToSelected
)
8245 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8249 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8250 bool addToSelected
)
8252 if ( IsSelection() && !addToSelected
)
8255 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8256 FALSE
, addToSelected
);
8260 void wxGrid::SelectAll()
8262 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8265 bool wxGrid::IsSelection()
8267 return ( m_selection
->IsSelection() ||
8268 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8269 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8272 bool wxGrid::IsInSelection( int row
, int col
)
8274 return ( m_selection
->IsInSelection( row
, col
) ||
8275 ( row
>= m_selectingTopLeft
.GetRow() &&
8276 col
>= m_selectingTopLeft
.GetCol() &&
8277 row
<= m_selectingBottomRight
.GetRow() &&
8278 col
<= m_selectingBottomRight
.GetCol() ) );
8281 void wxGrid::ClearSelection()
8283 m_selectingTopLeft
= wxGridNoCellCoords
;
8284 m_selectingBottomRight
= wxGridNoCellCoords
;
8285 m_selection
->ClearSelection();
8289 // This function returns the rectangle that encloses the given block
8290 // in device coords clipped to the client size of the grid window.
8292 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8293 const wxGridCellCoords
&bottomRight
)
8295 wxRect
rect( wxGridNoCellRect
);
8298 cellRect
= CellToRect( topLeft
);
8299 if ( cellRect
!= wxGridNoCellRect
)
8305 rect
= wxRect( 0, 0, 0, 0 );
8308 cellRect
= CellToRect( bottomRight
);
8309 if ( cellRect
!= wxGridNoCellRect
)
8315 return wxGridNoCellRect
;
8318 // convert to scrolled coords
8320 int left
, top
, right
, bottom
;
8321 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8322 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8325 m_gridWin
->GetClientSize( &cw
, &ch
);
8327 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8328 return wxRect( 0, 0, 0, 0);
8330 rect
.SetLeft( wxMax(0, left
) );
8331 rect
.SetTop( wxMax(0, top
) );
8332 rect
.SetRight( wxMin(cw
, right
) );
8333 rect
.SetBottom( wxMin(ch
, bottom
) );
8341 // ------ Grid event classes
8344 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8346 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8347 int row
, int col
, int x
, int y
, bool sel
,
8348 bool control
, bool shift
, bool alt
, bool meta
)
8349 : wxNotifyEvent( type
, id
)
8356 m_control
= control
;
8361 SetEventObject(obj
);
8365 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8367 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8368 int rowOrCol
, int x
, int y
,
8369 bool control
, bool shift
, bool alt
, bool meta
)
8370 : wxNotifyEvent( type
, id
)
8372 m_rowOrCol
= rowOrCol
;
8375 m_control
= control
;
8380 SetEventObject(obj
);
8384 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8386 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8387 const wxGridCellCoords
& topLeft
,
8388 const wxGridCellCoords
& bottomRight
,
8389 bool sel
, bool control
,
8390 bool shift
, bool alt
, bool meta
)
8391 : wxNotifyEvent( type
, id
)
8393 m_topLeft
= topLeft
;
8394 m_bottomRight
= bottomRight
;
8396 m_control
= control
;
8401 SetEventObject(obj
);
8405 #endif // ifndef wxUSE_NEW_GRID