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_PROCESS_TAB
| wxTE_MULTILINE
|
506 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
510 // TODO: use m_maxChars
512 wxGridCellEditor::Create(parent
, id
, evtHandler
);
515 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
516 wxGridCellAttr
* WXUNUSED(attr
))
518 // as we fill the entire client area, don't do anything here to minimize
522 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
524 wxRect
rect(rectOrig
);
526 // Make the edit control large enough to allow for internal
529 // TODO: remove this if the text ctrl sizing is improved esp. for
532 #if defined(__WXGTK__)
541 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
543 // MB: treat MSW separately here otherwise the caret doesn't show
544 // when the editor is in the first row.
545 #if defined(__WXMSW__)
548 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
551 #if defined(__WXMOTIF__)
555 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
556 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
557 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
558 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
561 wxGridCellEditor::SetSize(rect
);
564 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
566 wxASSERT_MSG(m_control
,
567 wxT("The wxGridCellEditor must be Created first!"));
569 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
571 DoBeginEdit(m_startValue
);
574 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
576 Text()->SetValue(startValue
);
577 Text()->SetInsertionPointEnd();
581 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
584 wxASSERT_MSG(m_control
,
585 wxT("The wxGridCellEditor must be Created first!"));
587 bool changed
= FALSE
;
588 wxString value
= Text()->GetValue();
589 if (value
!= m_startValue
)
593 grid
->GetTable()->SetValue(row
, col
, value
);
595 m_startValue
= wxEmptyString
;
596 Text()->SetValue(m_startValue
);
602 void wxGridCellTextEditor::Reset()
604 wxASSERT_MSG(m_control
,
605 wxT("The wxGridCellEditor must be Created first!"));
607 DoReset(m_startValue
);
610 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
612 Text()->SetValue(startValue
);
613 Text()->SetInsertionPointEnd();
616 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
618 if ( wxGridCellEditor::IsAcceptedKey(event
) )
620 int keycode
= event
.GetKeyCode();
634 case WXK_NUMPAD_MULTIPLY
:
638 case WXK_NUMPAD_SUBTRACT
:
640 case WXK_NUMPAD_DECIMAL
:
642 case WXK_NUMPAD_DIVIDE
:
646 // accept 8 bit chars too if isprint() agrees
647 if ( (keycode
< 255) && (isprint(keycode
)) )
655 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
657 // we don't check for !HasModifiers() because IsAcceptedKey() did it
659 // insert the key in the control
661 int keycode
= event
.GetKeyCode();
674 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
678 case WXK_NUMPAD_MULTIPLY
:
688 case WXK_NUMPAD_SUBTRACT
:
693 case WXK_NUMPAD_DECIMAL
:
698 case WXK_NUMPAD_DIVIDE
:
703 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
705 // FIXME this is not going to work for non letters...
706 if ( !event
.ShiftDown() )
708 keycode
= tolower(keycode
);
711 ch
= (wxChar
)keycode
;
721 Text()->AppendText(ch
);
729 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
730 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
732 #if defined(__WXMOTIF__) || defined(__WXGTK__)
733 // wxMotif needs a little extra help...
734 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
735 wxString
s( Text()->GetValue() );
736 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
738 Text()->SetInsertionPoint( pos
);
740 // the other ports can handle a Return key press
746 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
756 if ( !params
.ToLong(&tmp
) )
758 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
762 m_maxChars
= (size_t)tmp
;
767 // ----------------------------------------------------------------------------
768 // wxGridCellNumberEditor
769 // ----------------------------------------------------------------------------
771 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
777 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
779 wxEvtHandler
* evtHandler
)
783 // create a spin ctrl
784 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
785 wxDefaultPosition
, wxDefaultSize
,
789 wxGridCellEditor::Create(parent
, id
, evtHandler
);
793 // just a text control
794 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
797 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
798 #endif // wxUSE_VALIDATORS
802 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
804 // first get the value
805 wxGridTableBase
*table
= grid
->GetTable();
806 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
808 m_valueOld
= table
->GetValueAsLong(row
, col
);
812 wxString sValue
= table
->GetValue(row
, col
);
813 if (! sValue
.ToLong(&m_valueOld
))
815 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
822 Spin()->SetValue((int)m_valueOld
);
827 DoBeginEdit(GetString());
831 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
839 value
= Spin()->GetValue();
840 changed
= value
!= m_valueOld
;
844 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
849 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
850 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
852 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
858 void wxGridCellNumberEditor::Reset()
862 Spin()->SetValue((int)m_valueOld
);
866 DoReset(GetString());
870 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
872 if ( wxGridCellEditor::IsAcceptedKey(event
) )
874 int keycode
= event
.GetKeyCode();
890 case WXK_NUMPAD_SUBTRACT
:
896 if ( (keycode
< 128) && isdigit(keycode
) )
904 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
908 int keycode
= (int) event
.KeyCode();
909 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
911 wxGridCellTextEditor::StartingKey(event
);
921 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
932 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
936 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
940 // skip the error message below
945 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
949 // ----------------------------------------------------------------------------
950 // wxGridCellFloatEditor
951 // ----------------------------------------------------------------------------
953 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
956 m_precision
= precision
;
959 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
961 wxEvtHandler
* evtHandler
)
963 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
966 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
967 #endif // wxUSE_VALIDATORS
970 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
972 // first get the value
973 wxGridTableBase
*table
= grid
->GetTable();
974 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
976 m_valueOld
= table
->GetValueAsDouble(row
, col
);
980 wxString sValue
= table
->GetValue(row
, col
);
981 if (! sValue
.ToDouble(&m_valueOld
))
983 wxFAIL_MSG( _T("this cell doesn't have float value") );
988 DoBeginEdit(GetString());
991 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
995 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
997 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
998 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1000 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1010 void wxGridCellFloatEditor::Reset()
1012 DoReset(GetString());
1015 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1017 int keycode
= (int)event
.KeyCode();
1018 if ( isdigit(keycode
) ||
1019 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1021 wxGridCellTextEditor::StartingKey(event
);
1023 // skip Skip() below
1030 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1041 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1045 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1047 m_precision
= (int)tmp
;
1049 // skip the error message below
1054 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1058 wxString
wxGridCellFloatEditor::GetString() const
1061 if ( m_width
== -1 )
1063 // default width/precision
1066 else if ( m_precision
== -1 )
1068 // default precision
1069 fmt
.Printf(_T("%%%d.g"), m_width
);
1073 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1076 return wxString::Format(fmt
, m_valueOld
);
1079 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1081 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1083 int keycode
= event
.GetKeyCode();
1097 case WXK_NUMPAD_ADD
:
1099 case WXK_NUMPAD_SUBTRACT
:
1101 case WXK_NUMPAD_DECIMAL
:
1105 // additionally accept 'e' as in '1e+6'
1106 if ( (keycode
< 128) &&
1107 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1115 // ----------------------------------------------------------------------------
1116 // wxGridCellBoolEditor
1117 // ----------------------------------------------------------------------------
1119 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1121 wxEvtHandler
* evtHandler
)
1123 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1124 wxDefaultPosition
, wxDefaultSize
,
1127 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1130 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1132 bool resize
= FALSE
;
1133 wxSize size
= m_control
->GetSize();
1134 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1136 // check if the checkbox is not too big/small for this cell
1137 wxSize sizeBest
= m_control
->GetBestSize();
1138 if ( !(size
== sizeBest
) )
1140 // reset to default size if it had been made smaller
1146 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1148 // leave 1 pixel margin
1149 size
.x
= size
.y
= minSize
- 2;
1156 m_control
->SetSize(size
);
1159 // position it in the centre of the rectangle (TODO: support alignment?)
1161 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1162 // the checkbox without label still has some space to the right in wxGTK,
1163 // so shift it to the right
1165 #elif defined(__WXMSW__)
1166 // here too, but in other way
1171 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1174 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1176 m_control
->Show(show
);
1180 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1181 CBox()->SetBackgroundColour(colBg
);
1185 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1187 wxASSERT_MSG(m_control
,
1188 wxT("The wxGridCellEditor must be Created first!"));
1190 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1191 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1194 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1195 m_startValue
= !( !cellval
|| (cellval
== "0") );
1197 CBox()->SetValue(m_startValue
);
1201 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1204 wxASSERT_MSG(m_control
,
1205 wxT("The wxGridCellEditor must be Created first!"));
1207 bool changed
= FALSE
;
1208 bool value
= CBox()->GetValue();
1209 if ( value
!= m_startValue
)
1214 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1215 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1217 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1223 void wxGridCellBoolEditor::Reset()
1225 wxASSERT_MSG(m_control
,
1226 wxT("The wxGridCellEditor must be Created first!"));
1228 CBox()->SetValue(m_startValue
);
1231 void wxGridCellBoolEditor::StartingClick()
1233 CBox()->SetValue(!CBox()->GetValue());
1236 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1238 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1240 int keycode
= event
.GetKeyCode();
1244 case WXK_NUMPAD_MULTIPLY
:
1246 case WXK_NUMPAD_ADD
:
1248 case WXK_NUMPAD_SUBTRACT
:
1259 // ----------------------------------------------------------------------------
1260 // wxGridCellChoiceEditor
1261 // ----------------------------------------------------------------------------
1263 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1264 const wxString choices
[],
1266 : m_allowOthers(allowOthers
)
1270 m_choices
.Alloc(count
);
1271 for ( size_t n
= 0; n
< count
; n
++ )
1273 m_choices
.Add(choices
[n
]);
1278 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1280 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1281 editor
->m_allowOthers
= m_allowOthers
;
1282 editor
->m_choices
= m_choices
;
1287 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1289 wxEvtHandler
* evtHandler
)
1291 size_t count
= m_choices
.GetCount();
1292 wxString
*choices
= new wxString
[count
];
1293 for ( size_t n
= 0; n
< count
; n
++ )
1295 choices
[n
] = m_choices
[n
];
1298 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1299 wxDefaultPosition
, wxDefaultSize
,
1301 m_allowOthers
? 0 : wxCB_READONLY
);
1305 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1308 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1309 wxGridCellAttr
* attr
)
1311 // as we fill the entire client area, don't do anything here to minimize
1314 // TODO: It doesn't actually fill the client area since the height of a
1315 // combo always defaults to the standard... Until someone has time to
1316 // figure out the right rectangle to paint, just do it the normal way...
1317 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1320 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1322 wxASSERT_MSG(m_control
,
1323 wxT("The wxGridCellEditor must be Created first!"));
1325 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1327 Combo()->SetValue(m_startValue
);
1328 size_t count
= m_choices
.GetCount();
1329 for (size_t i
=0; i
<count
; i
++)
1331 if (m_startValue
== m_choices
[i
])
1333 Combo()->SetSelection(i
);
1337 Combo()->SetInsertionPointEnd();
1338 Combo()->SetFocus();
1341 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1344 wxString value
= Combo()->GetValue();
1345 bool changed
= value
!= m_startValue
;
1348 grid
->GetTable()->SetValue(row
, col
, value
);
1350 m_startValue
= wxEmptyString
;
1351 Combo()->SetValue(m_startValue
);
1356 void wxGridCellChoiceEditor::Reset()
1358 Combo()->SetValue(m_startValue
);
1359 Combo()->SetInsertionPointEnd();
1362 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1372 wxStringTokenizer
tk(params
, _T(','));
1373 while ( tk
.HasMoreTokens() )
1375 m_choices
.Add(tk
.GetNextToken());
1379 // ----------------------------------------------------------------------------
1380 // wxGridCellEditorEvtHandler
1381 // ----------------------------------------------------------------------------
1383 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1385 switch ( event
.KeyCode() )
1389 m_grid
->DisableCellEditControl();
1393 event
.Skip( m_grid
->ProcessEvent( event
) );
1397 if (!m_grid
->ProcessEvent(event
))
1398 m_editor
->HandleReturn(event
);
1407 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1409 switch ( event
.KeyCode() )
1421 // ----------------------------------------------------------------------------
1422 // wxGridCellWorker is an (almost) empty common base class for
1423 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1424 // ----------------------------------------------------------------------------
1426 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1431 wxGridCellWorker::~wxGridCellWorker()
1435 // ============================================================================
1437 // ============================================================================
1439 // ----------------------------------------------------------------------------
1440 // wxGridCellRenderer
1441 // ----------------------------------------------------------------------------
1443 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1444 wxGridCellAttr
& attr
,
1447 int WXUNUSED(row
), int WXUNUSED(col
),
1450 dc
.SetBackgroundMode( wxSOLID
);
1454 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1458 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1461 dc
.SetPen( *wxTRANSPARENT_PEN
);
1462 dc
.DrawRectangle(rect
);
1465 // ----------------------------------------------------------------------------
1466 // wxGridCellStringRenderer
1467 // ----------------------------------------------------------------------------
1469 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1470 wxGridCellAttr
& attr
,
1474 dc
.SetBackgroundMode( wxTRANSPARENT
);
1476 // TODO some special colours for attr.IsReadOnly() case?
1480 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1481 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1485 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1486 dc
.SetTextForeground( attr
.GetTextColour() );
1489 dc
.SetFont( attr
.GetFont() );
1492 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1494 const wxString
& text
)
1496 wxCoord x
= 0, y
= 0, max_x
= 0;
1497 dc
.SetFont(attr
.GetFont());
1498 wxStringTokenizer
tk(text
, _T('\n'));
1499 while ( tk
.HasMoreTokens() )
1501 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1502 max_x
= wxMax(max_x
, x
);
1505 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1507 return wxSize(max_x
, y
);
1510 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1511 wxGridCellAttr
& attr
,
1515 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1518 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1519 wxGridCellAttr
& attr
,
1521 const wxRect
& rectCell
,
1525 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1527 // now we only have to draw the text
1528 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1531 attr
.GetAlignment(&hAlign
, &vAlign
);
1533 wxRect rect
= rectCell
;
1536 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1537 rect
, hAlign
, vAlign
);
1540 // ----------------------------------------------------------------------------
1541 // wxGridCellNumberRenderer
1542 // ----------------------------------------------------------------------------
1544 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1546 wxGridTableBase
*table
= grid
.GetTable();
1548 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1550 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1554 text
= table
->GetValue(row
, col
);
1560 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1561 wxGridCellAttr
& attr
,
1563 const wxRect
& rectCell
,
1567 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1569 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1571 // draw the text right aligned by default
1573 attr
.GetAlignment(&hAlign
, &vAlign
);
1574 hAlign
= wxALIGN_RIGHT
;
1576 wxRect rect
= rectCell
;
1579 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1582 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1583 wxGridCellAttr
& attr
,
1587 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1590 // ----------------------------------------------------------------------------
1591 // wxGridCellFloatRenderer
1592 // ----------------------------------------------------------------------------
1594 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1597 SetPrecision(precision
);
1600 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1602 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1603 renderer
->m_width
= m_width
;
1604 renderer
->m_precision
= m_precision
;
1605 renderer
->m_format
= m_format
;
1610 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1612 wxGridTableBase
*table
= grid
.GetTable();
1617 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1619 val
= table
->GetValueAsDouble(row
, col
);
1624 text
= table
->GetValue(row
, col
);
1625 hasDouble
= text
.ToDouble(&val
);
1632 if ( m_width
== -1 )
1634 // default width/precision
1635 m_format
= _T("%f");
1637 else if ( m_precision
== -1 )
1639 // default precision
1640 m_format
.Printf(_T("%%%d.f"), m_width
);
1644 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1648 text
.Printf(m_format
, val
);
1650 //else: text already contains the string
1655 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1656 wxGridCellAttr
& attr
,
1658 const wxRect
& rectCell
,
1662 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1664 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1666 // draw the text right aligned by default
1668 attr
.GetAlignment(&hAlign
, &vAlign
);
1669 hAlign
= wxALIGN_RIGHT
;
1671 wxRect rect
= rectCell
;
1674 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1677 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1678 wxGridCellAttr
& attr
,
1682 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1685 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1691 // reset to defaults
1697 wxString tmp
= params
.BeforeFirst(_T(','));
1701 if ( !tmp
.ToLong(&width
) )
1707 SetWidth((int)width
);
1709 tmp
= params
.AfterFirst(_T(','));
1713 if ( !tmp
.ToLong(&precision
) )
1719 SetPrecision((int)precision
);
1727 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1732 // ----------------------------------------------------------------------------
1733 // wxGridCellBoolRenderer
1734 // ----------------------------------------------------------------------------
1736 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1738 // FIXME these checkbox size calculations are really ugly...
1740 // between checkmark and box
1741 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1743 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1744 wxGridCellAttr
& WXUNUSED(attr
),
1749 // compute it only once (no locks for MT safeness in GUI thread...)
1750 if ( !ms_sizeCheckMark
.x
)
1752 // get checkbox size
1753 wxCoord checkSize
= 0;
1754 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1755 wxSize size
= checkbox
->GetBestSize();
1756 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1758 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1759 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1760 checkSize
-= size
.y
/ 2;
1765 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1768 return ms_sizeCheckMark
;
1771 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1772 wxGridCellAttr
& attr
,
1778 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1780 // draw a check mark in the centre (ignoring alignment - TODO)
1781 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1783 // don't draw outside the cell
1784 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1785 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1787 // and even leave (at least) 1 pixel margin
1788 size
.x
= size
.y
= minSize
- 2;
1791 // draw a border around checkmark
1793 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1794 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1795 rectBorder
.width
= size
.x
;
1796 rectBorder
.height
= size
.y
;
1799 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1800 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1803 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1804 value
= !( !cellval
|| (cellval
== "0") );
1809 wxRect rectMark
= rectBorder
;
1811 // MSW DrawCheckMark() is weird (and should probably be changed...)
1812 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1816 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1819 dc
.SetTextForeground(attr
.GetTextColour());
1820 dc
.DrawCheckMark(rectMark
);
1823 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1824 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1825 dc
.DrawRectangle(rectBorder
);
1828 // ----------------------------------------------------------------------------
1830 // ----------------------------------------------------------------------------
1832 wxGridCellAttr
*wxGridCellAttr::Clone() const
1834 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1835 if ( HasTextColour() )
1836 attr
->SetTextColour(GetTextColour());
1837 if ( HasBackgroundColour() )
1838 attr
->SetBackgroundColour(GetBackgroundColour());
1840 attr
->SetFont(GetFont());
1841 if ( HasAlignment() )
1842 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1846 attr
->SetRenderer(m_renderer
);
1847 m_renderer
->IncRef();
1851 attr
->SetEditor(m_editor
);
1856 attr
->SetReadOnly();
1858 attr
->SetDefAttr(m_defGridAttr
);
1863 const wxColour
& wxGridCellAttr::GetTextColour() const
1865 if (HasTextColour())
1869 else if (m_defGridAttr
!= this)
1871 return m_defGridAttr
->GetTextColour();
1875 wxFAIL_MSG(wxT("Missing default cell attribute"));
1876 return wxNullColour
;
1881 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1883 if (HasBackgroundColour())
1885 else if (m_defGridAttr
!= this)
1886 return m_defGridAttr
->GetBackgroundColour();
1889 wxFAIL_MSG(wxT("Missing default cell attribute"));
1890 return wxNullColour
;
1895 const wxFont
& wxGridCellAttr::GetFont() const
1899 else if (m_defGridAttr
!= this)
1900 return m_defGridAttr
->GetFont();
1903 wxFAIL_MSG(wxT("Missing default cell attribute"));
1909 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1913 if ( hAlign
) *hAlign
= m_hAlign
;
1914 if ( vAlign
) *vAlign
= m_vAlign
;
1916 else if (m_defGridAttr
!= this)
1917 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1920 wxFAIL_MSG(wxT("Missing default cell attribute"));
1925 // GetRenderer and GetEditor use a slightly different decision path about
1926 // which attribute to use. If a non-default attr object has one then it is
1927 // used, otherwise the default editor or renderer is fetched from the grid and
1928 // used. It should be the default for the data type of the cell. If it is
1929 // NULL (because the table has a type that the grid does not have in its
1930 // registry,) then the grid's default editor or renderer is used.
1932 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1934 wxGridCellRenderer
* renderer
= NULL
;
1936 if ( m_defGridAttr
!= this || grid
== NULL
)
1938 renderer
= m_renderer
; // use local attribute
1943 if ( !renderer
&& grid
) // get renderer for the data type
1945 // GetDefaultRendererForCell() will do IncRef() for us
1946 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1951 // if we still don't have one then use the grid default
1952 // (no need for IncRef() here neither)
1953 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1958 wxFAIL_MSG(wxT("Missing default cell attribute"));
1964 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1966 wxGridCellEditor
* editor
= NULL
;
1968 if ( m_defGridAttr
!= this || grid
== NULL
)
1970 editor
= m_editor
; // use local attribute
1975 if ( !editor
&& grid
) // get renderer for the data type
1976 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1979 // if we still don't have one then use the grid default
1980 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1984 wxFAIL_MSG(wxT("Missing default cell attribute"));
1990 // ----------------------------------------------------------------------------
1991 // wxGridCellAttrData
1992 // ----------------------------------------------------------------------------
1994 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1996 int n
= FindIndex(row
, col
);
1997 if ( n
== wxNOT_FOUND
)
1999 // add the attribute
2000 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2006 // change the attribute
2007 m_attrs
[(size_t)n
].attr
= attr
;
2011 // remove this attribute
2012 m_attrs
.RemoveAt((size_t)n
);
2017 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2019 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2021 int n
= FindIndex(row
, col
);
2022 if ( n
!= wxNOT_FOUND
)
2024 attr
= m_attrs
[(size_t)n
].attr
;
2031 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2033 size_t count
= m_attrs
.GetCount();
2034 for ( size_t n
= 0; n
< count
; n
++ )
2036 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2037 wxCoord row
= coords
.GetRow();
2038 if ((size_t)row
>= pos
)
2042 // If rows inserted, include row counter where necessary
2043 coords
.SetRow(row
+ numRows
);
2045 else if (numRows
< 0)
2047 // If rows deleted ...
2048 if ((size_t)row
>= pos
- numRows
)
2050 // ...either decrement row counter (if row still exists)...
2051 coords
.SetRow(row
+ numRows
);
2055 // ...or remove the attribute
2056 m_attrs
.RemoveAt((size_t)n
);
2064 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2066 size_t count
= m_attrs
.GetCount();
2067 for ( size_t n
= 0; n
< count
; n
++ )
2069 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2070 wxCoord col
= coords
.GetCol();
2071 if ( (size_t)col
>= pos
)
2075 // If rows inserted, include row counter where necessary
2076 coords
.SetCol(col
+ numCols
);
2078 else if (numCols
< 0)
2080 // If rows deleted ...
2081 if ((size_t)col
>= pos
- numCols
)
2083 // ...either decrement row counter (if row still exists)...
2084 coords
.SetCol(col
+ numCols
);
2088 // ...or remove the attribute
2089 m_attrs
.RemoveAt((size_t)n
);
2097 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2099 size_t count
= m_attrs
.GetCount();
2100 for ( size_t n
= 0; n
< count
; n
++ )
2102 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2103 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2112 // ----------------------------------------------------------------------------
2113 // wxGridRowOrColAttrData
2114 // ----------------------------------------------------------------------------
2116 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2118 size_t count
= m_attrs
.Count();
2119 for ( size_t n
= 0; n
< count
; n
++ )
2121 m_attrs
[n
]->DecRef();
2125 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2127 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2129 int n
= m_rowsOrCols
.Index(rowOrCol
);
2130 if ( n
!= wxNOT_FOUND
)
2132 attr
= m_attrs
[(size_t)n
];
2139 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2141 int i
= m_rowsOrCols
.Index(rowOrCol
);
2142 if ( i
== wxNOT_FOUND
)
2144 // add the attribute
2145 m_rowsOrCols
.Add(rowOrCol
);
2150 size_t n
= (size_t)i
;
2153 // change the attribute
2154 m_attrs
[n
]->DecRef();
2159 // remove this attribute
2160 m_attrs
[n
]->DecRef();
2161 m_rowsOrCols
.RemoveAt(n
);
2162 m_attrs
.RemoveAt(n
);
2167 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2169 size_t count
= m_attrs
.GetCount();
2170 for ( size_t n
= 0; n
< count
; n
++ )
2172 int & rowOrCol
= m_rowsOrCols
[n
];
2173 if ( (size_t)rowOrCol
>= pos
)
2175 if ( numRowsOrCols
> 0 )
2177 // If rows inserted, include row counter where necessary
2178 rowOrCol
+= numRowsOrCols
;
2180 else if ( numRowsOrCols
< 0)
2182 // If rows deleted, either decrement row counter (if row still exists)
2183 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2184 rowOrCol
+= numRowsOrCols
;
2187 m_rowsOrCols
.RemoveAt((size_t)n
);
2188 m_attrs
.RemoveAt((size_t)n
);
2196 // ----------------------------------------------------------------------------
2197 // wxGridCellAttrProvider
2198 // ----------------------------------------------------------------------------
2200 wxGridCellAttrProvider::wxGridCellAttrProvider()
2202 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2205 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2210 void wxGridCellAttrProvider::InitData()
2212 m_data
= new wxGridCellAttrProviderData
;
2215 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2217 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2220 // first look for the attribute of this specific cell
2221 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2225 // then look for the col attr (col attributes are more common than
2226 // the row ones, hence they have priority)
2227 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2232 // finally try the row attributes
2233 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2240 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2246 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2249 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2254 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2257 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2262 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2265 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2269 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2271 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2275 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2279 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2281 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2285 // ----------------------------------------------------------------------------
2286 // wxGridTypeRegistry
2287 // ----------------------------------------------------------------------------
2289 wxGridTypeRegistry::~wxGridTypeRegistry()
2291 size_t count
= m_typeinfo
.Count();
2292 for ( size_t i
= 0; i
< count
; i
++ )
2293 delete m_typeinfo
[i
];
2297 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2298 wxGridCellRenderer
* renderer
,
2299 wxGridCellEditor
* editor
)
2301 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2303 // is it already registered?
2304 int loc
= FindRegisteredDataType(typeName
);
2305 if ( loc
!= wxNOT_FOUND
)
2307 delete m_typeinfo
[loc
];
2308 m_typeinfo
[loc
] = info
;
2312 m_typeinfo
.Add(info
);
2316 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2318 size_t count
= m_typeinfo
.GetCount();
2319 for ( size_t i
= 0; i
< count
; i
++ )
2321 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2330 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2332 int index
= FindRegisteredDataType(typeName
);
2333 if ( index
== wxNOT_FOUND
)
2335 // check whether this is one of the standard ones, in which case
2336 // register it "on the fly"
2337 if ( typeName
== wxGRID_VALUE_STRING
)
2339 RegisterDataType(wxGRID_VALUE_STRING
,
2340 new wxGridCellStringRenderer
,
2341 new wxGridCellTextEditor
);
2343 else if ( typeName
== wxGRID_VALUE_BOOL
)
2345 RegisterDataType(wxGRID_VALUE_BOOL
,
2346 new wxGridCellBoolRenderer
,
2347 new wxGridCellBoolEditor
);
2349 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2351 RegisterDataType(wxGRID_VALUE_NUMBER
,
2352 new wxGridCellNumberRenderer
,
2353 new wxGridCellNumberEditor
);
2355 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2357 RegisterDataType(wxGRID_VALUE_FLOAT
,
2358 new wxGridCellFloatRenderer
,
2359 new wxGridCellFloatEditor
);
2361 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2363 RegisterDataType(wxGRID_VALUE_CHOICE
,
2364 new wxGridCellStringRenderer
,
2365 new wxGridCellChoiceEditor
);
2372 // we get here only if just added the entry for this type, so return
2374 index
= m_typeinfo
.GetCount() - 1;
2380 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2382 int index
= FindDataType(typeName
);
2383 if ( index
== wxNOT_FOUND
)
2385 // the first part of the typename is the "real" type, anything after ':'
2386 // are the parameters for the renderer
2387 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2388 if ( index
== wxNOT_FOUND
)
2393 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2394 wxGridCellRenderer
*rendererOld
= renderer
;
2395 renderer
= renderer
->Clone();
2396 rendererOld
->DecRef();
2398 wxGridCellEditor
*editor
= GetEditor(index
);
2399 wxGridCellEditor
*editorOld
= editor
;
2400 editor
= editor
->Clone();
2401 editorOld
->DecRef();
2403 // do it even if there are no parameters to reset them to defaults
2404 wxString params
= typeName
.AfterFirst(_T(':'));
2405 renderer
->SetParameters(params
);
2406 editor
->SetParameters(params
);
2408 // register the new typename
2409 RegisterDataType(typeName
, renderer
, editor
);
2411 // we just registered it, it's the last one
2412 index
= m_typeinfo
.GetCount() - 1;
2418 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2420 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2426 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2428 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2434 // ----------------------------------------------------------------------------
2436 // ----------------------------------------------------------------------------
2438 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2441 wxGridTableBase::wxGridTableBase()
2443 m_view
= (wxGrid
*) NULL
;
2444 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2447 wxGridTableBase::~wxGridTableBase()
2449 delete m_attrProvider
;
2452 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2454 delete m_attrProvider
;
2455 m_attrProvider
= attrProvider
;
2458 bool wxGridTableBase::CanHaveAttributes()
2460 if ( ! GetAttrProvider() )
2462 // use the default attr provider by default
2463 SetAttrProvider(new wxGridCellAttrProvider
);
2468 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2470 if ( m_attrProvider
)
2471 return m_attrProvider
->GetAttr(row
, col
);
2473 return (wxGridCellAttr
*)NULL
;
2476 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2478 if ( m_attrProvider
)
2480 m_attrProvider
->SetAttr(attr
, row
, col
);
2484 // as we take ownership of the pointer and don't store it, we must
2490 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2492 if ( m_attrProvider
)
2494 m_attrProvider
->SetRowAttr(attr
, row
);
2498 // as we take ownership of the pointer and don't store it, we must
2504 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2506 if ( m_attrProvider
)
2508 m_attrProvider
->SetColAttr(attr
, col
);
2512 // as we take ownership of the pointer and don't store it, we must
2518 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2519 size_t WXUNUSED(numRows
) )
2521 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2526 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2528 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2533 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2534 size_t WXUNUSED(numRows
) )
2536 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2541 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2542 size_t WXUNUSED(numCols
) )
2544 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2549 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2551 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2556 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2557 size_t WXUNUSED(numCols
) )
2559 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2565 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2568 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2569 // how much it makes sense to us geeks.
2573 wxString
wxGridTableBase::GetColLabelValue( int col
)
2575 // default col labels are:
2576 // cols 0 to 25 : A-Z
2577 // cols 26 to 675 : AA-ZZ
2582 for ( n
= 1; ; n
++ )
2584 s
+= (_T('A') + (wxChar
)( col%26
));
2586 if ( col
< 0 ) break;
2589 // reverse the string...
2591 for ( i
= 0; i
< n
; i
++ )
2600 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2602 return wxGRID_VALUE_STRING
;
2605 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2606 const wxString
& typeName
)
2608 return typeName
== wxGRID_VALUE_STRING
;
2611 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2613 return CanGetValueAs(row
, col
, typeName
);
2616 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2621 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2626 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2631 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2632 long WXUNUSED(value
) )
2636 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2637 double WXUNUSED(value
) )
2641 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2642 bool WXUNUSED(value
) )
2647 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2648 const wxString
& WXUNUSED(typeName
) )
2653 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2654 const wxString
& WXUNUSED(typeName
),
2655 void* WXUNUSED(value
) )
2659 //////////////////////////////////////////////////////////////////////
2661 // Message class for the grid table to send requests and notifications
2665 wxGridTableMessage::wxGridTableMessage()
2667 m_table
= (wxGridTableBase
*) NULL
;
2673 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2674 int commandInt1
, int commandInt2
)
2678 m_comInt1
= commandInt1
;
2679 m_comInt2
= commandInt2
;
2684 //////////////////////////////////////////////////////////////////////
2686 // A basic grid table for string data. An object of this class will
2687 // created by wxGrid if you don't specify an alternative table class.
2690 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2692 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2694 wxGridStringTable::wxGridStringTable()
2699 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2704 m_data
.Alloc( numRows
);
2707 sa
.Alloc( numCols
);
2708 for ( col
= 0; col
< numCols
; col
++ )
2710 sa
.Add( wxEmptyString
);
2713 for ( row
= 0; row
< numRows
; row
++ )
2719 wxGridStringTable::~wxGridStringTable()
2723 int wxGridStringTable::GetNumberRows()
2725 return m_data
.GetCount();
2728 int wxGridStringTable::GetNumberCols()
2730 if ( m_data
.GetCount() > 0 )
2731 return m_data
[0].GetCount();
2736 wxString
wxGridStringTable::GetValue( int row
, int col
)
2738 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2739 _T("invalid row or column index in wxGridStringTable") );
2741 return m_data
[row
][col
];
2744 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2746 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2747 _T("invalid row or column index in wxGridStringTable") );
2749 m_data
[row
][col
] = value
;
2752 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2754 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2755 _T("invalid row or column index in wxGridStringTable") );
2757 return (m_data
[row
][col
] == wxEmptyString
);
2760 void wxGridStringTable::Clear()
2763 int numRows
, numCols
;
2765 numRows
= m_data
.GetCount();
2768 numCols
= m_data
[0].GetCount();
2770 for ( row
= 0; row
< numRows
; row
++ )
2772 for ( col
= 0; col
< numCols
; col
++ )
2774 m_data
[row
][col
] = wxEmptyString
;
2781 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2785 size_t curNumRows
= m_data
.GetCount();
2786 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2787 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2789 if ( pos
>= curNumRows
)
2791 return AppendRows( numRows
);
2795 sa
.Alloc( curNumCols
);
2796 for ( col
= 0; col
< curNumCols
; col
++ )
2798 sa
.Add( wxEmptyString
);
2801 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2803 m_data
.Insert( sa
, row
);
2807 wxGridTableMessage
msg( this,
2808 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2812 GetView()->ProcessTableMessage( msg
);
2818 bool wxGridStringTable::AppendRows( size_t numRows
)
2822 size_t curNumRows
= m_data
.GetCount();
2823 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2824 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2827 if ( curNumCols
> 0 )
2829 sa
.Alloc( curNumCols
);
2830 for ( col
= 0; col
< curNumCols
; col
++ )
2832 sa
.Add( wxEmptyString
);
2836 for ( row
= 0; row
< numRows
; row
++ )
2843 wxGridTableMessage
msg( this,
2844 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2847 GetView()->ProcessTableMessage( msg
);
2853 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2857 size_t curNumRows
= m_data
.GetCount();
2859 if ( pos
>= curNumRows
)
2862 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2863 pos
, numRows
, curNumRows
);
2864 wxFAIL_MSG( errmsg
);
2868 if ( numRows
> curNumRows
- pos
)
2870 numRows
= curNumRows
- pos
;
2873 if ( numRows
>= curNumRows
)
2875 m_data
.Empty(); // don't release memory just yet
2879 for ( n
= 0; n
< numRows
; n
++ )
2881 m_data
.Remove( pos
);
2886 wxGridTableMessage
msg( this,
2887 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2891 GetView()->ProcessTableMessage( msg
);
2897 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2901 size_t curNumRows
= m_data
.GetCount();
2902 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2903 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2905 if ( pos
>= curNumCols
)
2907 return AppendCols( numCols
);
2910 for ( row
= 0; row
< curNumRows
; row
++ )
2912 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2914 m_data
[row
].Insert( wxEmptyString
, col
);
2919 wxGridTableMessage
msg( this,
2920 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2924 GetView()->ProcessTableMessage( msg
);
2930 bool wxGridStringTable::AppendCols( size_t numCols
)
2934 size_t curNumRows
= m_data
.GetCount();
2938 // TODO: something better than this ?
2940 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2945 for ( row
= 0; row
< curNumRows
; row
++ )
2947 for ( n
= 0; n
< numCols
; n
++ )
2949 m_data
[row
].Add( wxEmptyString
);
2955 wxGridTableMessage
msg( this,
2956 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2959 GetView()->ProcessTableMessage( msg
);
2965 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2969 size_t curNumRows
= m_data
.GetCount();
2970 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2971 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2973 if ( pos
>= curNumCols
)
2976 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
2977 pos
, numCols
, curNumCols
);
2978 wxFAIL_MSG( errmsg
);
2982 if ( numCols
> curNumCols
- pos
)
2984 numCols
= curNumCols
- pos
;
2987 for ( row
= 0; row
< curNumRows
; row
++ )
2989 if ( numCols
>= curNumCols
)
2991 m_data
[row
].Clear();
2995 for ( n
= 0; n
< numCols
; n
++ )
2997 m_data
[row
].Remove( pos
);
3003 wxGridTableMessage
msg( this,
3004 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3008 GetView()->ProcessTableMessage( msg
);
3014 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3016 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3018 // using default label
3020 return wxGridTableBase::GetRowLabelValue( row
);
3024 return m_rowLabels
[ row
];
3028 wxString
wxGridStringTable::GetColLabelValue( int col
)
3030 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3032 // using default label
3034 return wxGridTableBase::GetColLabelValue( col
);
3038 return m_colLabels
[ col
];
3042 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3044 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3046 int n
= m_rowLabels
.GetCount();
3048 for ( i
= n
; i
<= row
; i
++ )
3050 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3054 m_rowLabels
[row
] = value
;
3057 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3059 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3061 int n
= m_colLabels
.GetCount();
3063 for ( i
= n
; i
<= col
; i
++ )
3065 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3069 m_colLabels
[col
] = value
;
3074 //////////////////////////////////////////////////////////////////////
3075 //////////////////////////////////////////////////////////////////////
3077 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3079 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3080 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3081 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3082 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3083 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3086 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3088 const wxPoint
&pos
, const wxSize
&size
)
3089 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3094 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3098 // NO - don't do this because it will set both the x and y origin
3099 // coords to match the parent scrolled window and we just want to
3100 // set the y coord - MB
3102 // m_owner->PrepareDC( dc );
3105 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3106 dc
.SetDeviceOrigin( 0, -y
);
3108 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3109 m_owner
->DrawRowLabels( dc
);
3113 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3115 m_owner
->ProcessRowLabelMouseEvent( event
);
3119 // This seems to be required for wxMotif otherwise the mouse
3120 // cursor must be in the cell edit control to get key events
3122 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3124 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3127 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3129 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3134 //////////////////////////////////////////////////////////////////////
3136 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3138 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3139 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3140 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3141 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3142 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3145 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3147 const wxPoint
&pos
, const wxSize
&size
)
3148 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3153 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3157 // NO - don't do this because it will set both the x and y origin
3158 // coords to match the parent scrolled window and we just want to
3159 // set the x coord - MB
3161 // m_owner->PrepareDC( dc );
3164 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3165 dc
.SetDeviceOrigin( -x
, 0 );
3167 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3168 m_owner
->DrawColLabels( dc
);
3172 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3174 m_owner
->ProcessColLabelMouseEvent( event
);
3178 // This seems to be required for wxMotif otherwise the mouse
3179 // cursor must be in the cell edit control to get key events
3181 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3183 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3186 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3188 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3193 //////////////////////////////////////////////////////////////////////
3195 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3197 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3198 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3199 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3200 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3201 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3204 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3206 const wxPoint
&pos
, const wxSize
&size
)
3207 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3212 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3216 int client_height
= 0;
3217 int client_width
= 0;
3218 GetClientSize( &client_width
, &client_height
);
3220 dc
.SetPen( *wxBLACK_PEN
);
3221 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3222 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3224 dc
.SetPen( *wxWHITE_PEN
);
3225 dc
.DrawLine( 0, 0, client_width
, 0 );
3226 dc
.DrawLine( 0, 0, 0, client_height
);
3230 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3232 m_owner
->ProcessCornerLabelMouseEvent( event
);
3236 // This seems to be required for wxMotif otherwise the mouse
3237 // cursor must be in the cell edit control to get key events
3239 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3241 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3244 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3246 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3251 //////////////////////////////////////////////////////////////////////
3253 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3255 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3256 EVT_PAINT( wxGridWindow::OnPaint
)
3257 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3258 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3259 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3260 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3263 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3264 wxGridRowLabelWindow
*rowLblWin
,
3265 wxGridColLabelWindow
*colLblWin
,
3266 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3267 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3270 m_rowLabelWin
= rowLblWin
;
3271 m_colLabelWin
= colLblWin
;
3272 SetBackgroundColour( "WHITE" );
3276 wxGridWindow::~wxGridWindow()
3281 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3283 wxPaintDC
dc( this );
3284 m_owner
->PrepareDC( dc
);
3285 wxRegion reg
= GetUpdateRegion();
3286 m_owner
->CalcCellsExposed( reg
);
3287 m_owner
->DrawGridCellArea( dc
);
3288 #if WXGRID_DRAW_LINES
3289 m_owner
->DrawAllGridLines( dc
, reg
);
3291 m_owner
->DrawGridSpace( dc
);
3292 m_owner
->DrawHighlight( dc
);
3296 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3298 wxPanel::ScrollWindow( dx
, dy
, rect
);
3299 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3300 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3304 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3306 m_owner
->ProcessGridCellMouseEvent( event
);
3310 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3311 // cursor must be in the cell edit control to get key events
3313 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3315 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3318 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3320 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3323 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3328 //////////////////////////////////////////////////////////////////////
3331 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3333 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3334 EVT_PAINT( wxGrid::OnPaint
)
3335 EVT_SIZE( wxGrid::OnSize
)
3336 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3337 EVT_KEY_UP( wxGrid::OnKeyUp
)
3338 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3341 wxGrid::wxGrid( wxWindow
*parent
,
3346 const wxString
& name
)
3347 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3348 m_colMinWidths(GRID_HASH_SIZE
),
3349 m_rowMinHeights(GRID_HASH_SIZE
)
3358 wxSafeDecRef(m_defaultCellAttr
);
3360 #ifdef DEBUG_ATTR_CACHE
3361 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3362 wxPrintf(_T("wxGrid attribute cache statistics: "
3363 "total: %u, hits: %u (%u%%)\n"),
3364 total
, gs_nAttrCacheHits
,
3365 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3371 delete m_typeRegistry
;
3377 // ----- internal init and update functions
3380 void wxGrid::Create()
3382 m_created
= FALSE
; // set to TRUE by CreateGrid
3384 m_table
= (wxGridTableBase
*) NULL
;
3387 m_cellEditCtrlEnabled
= FALSE
;
3389 m_defaultCellAttr
= new wxGridCellAttr
;
3390 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3392 // Set default cell attributes
3393 m_defaultCellAttr
->SetFont(GetFont());
3394 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3395 m_defaultCellAttr
->SetTextColour(
3396 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3397 m_defaultCellAttr
->SetBackgroundColour(
3398 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3399 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3400 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3405 m_currentCellCoords
= wxGridNoCellCoords
;
3407 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3408 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3410 // create the type registry
3411 m_typeRegistry
= new wxGridTypeRegistry
;
3413 // subwindow components that make up the wxGrid
3414 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3419 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3424 m_colLabelWin
= new wxGridColLabelWindow( this,
3429 m_gridWin
= new wxGridWindow( this,
3436 SetTargetWindow( m_gridWin
);
3440 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3441 wxGrid::wxGridSelectionModes selmode
)
3443 wxCHECK_MSG( !m_created
,
3445 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3447 m_numRows
= numRows
;
3448 m_numCols
= numCols
;
3450 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3451 m_table
->SetView( this );
3453 m_selection
= new wxGridSelection( this, selmode
);
3460 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3464 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3467 m_selection
->SetSelectionMode( selmode
);
3470 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3471 wxGrid::wxGridSelectionModes selmode
)
3475 // RD: Actually, this should probably be allowed. I think it would be
3476 // nice to be able to switch multiple Tables in and out of a single
3477 // View at runtime. Is there anything in the implmentation that would
3480 // At least, you now have to cope with m_selection
3481 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3486 m_numRows
= table
->GetNumberRows();
3487 m_numCols
= table
->GetNumberCols();
3490 m_table
->SetView( this );
3493 m_selection
= new wxGridSelection( this, selmode
);
3504 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3505 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3507 if ( m_rowLabelWin
)
3509 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3513 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3516 m_labelTextColour
= wxColour( _T("BLACK") );
3519 m_attrCache
.row
= -1;
3521 // TODO: something better than this ?
3523 m_labelFont
= this->GetFont();
3524 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3526 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3527 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3529 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3530 m_colLabelVertAlign
= wxALIGN_TOP
;
3532 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3533 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3535 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3536 m_defaultRowHeight
+= 8;
3538 m_defaultRowHeight
+= 4;
3541 m_gridLineColour
= wxColour( 128, 128, 255 );
3542 m_gridLinesEnabled
= TRUE
;
3543 m_cellHighlightColour
= m_gridLineColour
;
3545 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3546 m_winCapture
= (wxWindow
*)NULL
;
3547 m_canDragRowSize
= TRUE
;
3548 m_canDragColSize
= TRUE
;
3549 m_canDragGridSize
= TRUE
;
3551 m_dragRowOrCol
= -1;
3552 m_isDragging
= FALSE
;
3553 m_startDragPos
= wxDefaultPosition
;
3555 m_waitForSlowClick
= FALSE
;
3557 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3558 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3560 m_currentCellCoords
= wxGridNoCellCoords
;
3562 m_selectingTopLeft
= wxGridNoCellCoords
;
3563 m_selectingBottomRight
= wxGridNoCellCoords
;
3564 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3565 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3567 m_editable
= TRUE
; // default for whole grid
3569 m_inOnKeyDown
= FALSE
;
3578 // ----------------------------------------------------------------------------
3579 // the idea is to call these functions only when necessary because they create
3580 // quite big arrays which eat memory mostly unnecessary - in particular, if
3581 // default widths/heights are used for all rows/columns, we may not use these
3584 // with some extra code, it should be possible to only store the
3585 // widths/heights different from default ones but this will be done later...
3586 // ----------------------------------------------------------------------------
3588 void wxGrid::InitRowHeights()
3590 m_rowHeights
.Empty();
3591 m_rowBottoms
.Empty();
3593 m_rowHeights
.Alloc( m_numRows
);
3594 m_rowBottoms
.Alloc( m_numRows
);
3597 for ( int i
= 0; i
< m_numRows
; i
++ )
3599 m_rowHeights
.Add( m_defaultRowHeight
);
3600 rowBottom
+= m_defaultRowHeight
;
3601 m_rowBottoms
.Add( rowBottom
);
3605 void wxGrid::InitColWidths()
3607 m_colWidths
.Empty();
3608 m_colRights
.Empty();
3610 m_colWidths
.Alloc( m_numCols
);
3611 m_colRights
.Alloc( m_numCols
);
3613 for ( int i
= 0; i
< m_numCols
; i
++ )
3615 m_colWidths
.Add( m_defaultColWidth
);
3616 colRight
+= m_defaultColWidth
;
3617 m_colRights
.Add( colRight
);
3621 int wxGrid::GetColWidth(int col
) const
3623 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3626 int wxGrid::GetColLeft(int col
) const
3628 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3629 : m_colRights
[col
] - m_colWidths
[col
];
3632 int wxGrid::GetColRight(int col
) const
3634 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3638 int wxGrid::GetRowHeight(int row
) const
3640 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3643 int wxGrid::GetRowTop(int row
) const
3645 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3646 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3649 int wxGrid::GetRowBottom(int row
) const
3651 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3652 : m_rowBottoms
[row
];
3655 void wxGrid::CalcDimensions()
3658 GetClientSize( &cw
, &ch
);
3660 if ( m_numRows
> 0 || m_numCols
> 0 )
3662 int right
= m_numCols
> 0 ? GetColRight( m_numCols
-1 ) + m_extraWidth
: 0;
3663 int bottom
= m_numRows
> 0 ? GetRowBottom( m_numRows
-1 ) + m_extraHeight
: 0;
3665 // TODO: restore the scroll position that we had before sizing
3668 GetViewStart( &x
, &y
);
3669 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3670 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3676 void wxGrid::CalcWindowSizes()
3679 GetClientSize( &cw
, &ch
);
3681 if ( m_cornerLabelWin
->IsShown() )
3682 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3684 if ( m_colLabelWin
->IsShown() )
3685 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3687 if ( m_rowLabelWin
->IsShown() )
3688 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3690 if ( m_gridWin
->IsShown() )
3691 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3695 // this is called when the grid table sends a message to say that it
3696 // has been redimensioned
3698 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3701 bool result
= FALSE
;
3704 // if we were using the default widths/heights so far, we must change them
3706 if ( m_colWidths
.IsEmpty() )
3711 if ( m_rowHeights
.IsEmpty() )
3717 switch ( msg
.GetId() )
3719 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3721 size_t pos
= msg
.GetCommandInt();
3722 int numRows
= msg
.GetCommandInt2();
3724 m_numRows
+= numRows
;
3726 if ( !m_rowHeights
.IsEmpty() )
3728 for ( i
= 0; i
< numRows
; i
++ )
3730 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3731 m_rowBottoms
.Insert( 0, pos
);
3735 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3737 for ( i
= pos
; i
< m_numRows
; i
++ )
3739 bottom
+= m_rowHeights
[i
];
3740 m_rowBottoms
[i
] = bottom
;
3743 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3745 // if we have just inserted cols into an empty grid the current
3746 // cell will be undefined...
3748 SetCurrentCell( 0, 0 );
3750 m_selection
->UpdateRows( pos
, numRows
);
3751 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3753 attrProvider
->UpdateAttrRows( pos
, numRows
);
3755 if ( !GetBatchCount() )
3758 m_rowLabelWin
->Refresh();
3764 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3766 int numRows
= msg
.GetCommandInt();
3767 int oldNumRows
= m_numRows
;
3768 m_numRows
+= numRows
;
3770 if ( !m_rowHeights
.IsEmpty() )
3772 for ( i
= 0; i
< numRows
; i
++ )
3774 m_rowHeights
.Add( m_defaultRowHeight
);
3775 m_rowBottoms
.Add( 0 );
3779 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3781 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3783 bottom
+= m_rowHeights
[i
];
3784 m_rowBottoms
[i
] = bottom
;
3787 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3789 // if we have just inserted cols into an empty grid the current
3790 // cell will be undefined...
3792 SetCurrentCell( 0, 0 );
3794 if ( !GetBatchCount() )
3797 m_rowLabelWin
->Refresh();
3803 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3805 size_t pos
= msg
.GetCommandInt();
3806 int numRows
= msg
.GetCommandInt2();
3807 m_numRows
-= numRows
;
3809 if ( !m_rowHeights
.IsEmpty() )
3811 for ( i
= 0; i
< numRows
; i
++ )
3813 m_rowHeights
.Remove( pos
);
3814 m_rowBottoms
.Remove( pos
);
3818 for ( i
= 0; i
< m_numRows
; i
++ )
3820 h
+= m_rowHeights
[i
];
3821 m_rowBottoms
[i
] = h
;
3826 m_currentCellCoords
= wxGridNoCellCoords
;
3830 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3831 m_currentCellCoords
.Set( 0, 0 );
3833 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3834 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3836 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3837 // ifdef'd out following patch from Paul Gammans
3839 // No need to touch column attributes, unless we
3840 // removed _all_ rows, in this case, we remove
3841 // all column attributes.
3842 // I hate to do this here, but the
3843 // needed data is not available inside UpdateAttrRows.
3844 if ( !GetNumberRows() )
3845 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3848 if ( !GetBatchCount() )
3851 m_rowLabelWin
->Refresh();
3857 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3859 size_t pos
= msg
.GetCommandInt();
3860 int numCols
= msg
.GetCommandInt2();
3861 m_numCols
+= numCols
;
3863 if ( !m_colWidths
.IsEmpty() )
3865 for ( i
= 0; i
< numCols
; i
++ )
3867 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3868 m_colRights
.Insert( 0, pos
);
3872 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3874 for ( i
= pos
; i
< m_numCols
; i
++ )
3876 right
+= m_colWidths
[i
];
3877 m_colRights
[i
] = right
;
3880 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3882 // if we have just inserted cols into an empty grid the current
3883 // cell will be undefined...
3885 SetCurrentCell( 0, 0 );
3887 m_selection
->UpdateCols( pos
, numCols
);
3888 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3890 attrProvider
->UpdateAttrCols( pos
, numCols
);
3891 if ( !GetBatchCount() )
3894 m_colLabelWin
->Refresh();
3901 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3903 int numCols
= msg
.GetCommandInt();
3904 int oldNumCols
= m_numCols
;
3905 m_numCols
+= numCols
;
3906 if ( !m_colWidths
.IsEmpty() )
3908 for ( i
= 0; i
< numCols
; i
++ )
3910 m_colWidths
.Add( m_defaultColWidth
);
3911 m_colRights
.Add( 0 );
3915 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3917 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3919 right
+= m_colWidths
[i
];
3920 m_colRights
[i
] = right
;
3923 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3925 // if we have just inserted cols into an empty grid the current
3926 // cell will be undefined...
3928 SetCurrentCell( 0, 0 );
3930 if ( !GetBatchCount() )
3933 m_colLabelWin
->Refresh();
3939 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3941 size_t pos
= msg
.GetCommandInt();
3942 int numCols
= msg
.GetCommandInt2();
3943 m_numCols
-= numCols
;
3945 if ( !m_colWidths
.IsEmpty() )
3947 for ( i
= 0; i
< numCols
; i
++ )
3949 m_colWidths
.Remove( pos
);
3950 m_colRights
.Remove( pos
);
3954 for ( i
= 0; i
< m_numCols
; i
++ )
3956 w
+= m_colWidths
[i
];
3962 m_currentCellCoords
= wxGridNoCellCoords
;
3966 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3967 m_currentCellCoords
.Set( 0, 0 );
3969 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3970 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3972 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3973 // ifdef'd out following patch from Paul Gammans
3975 // No need to touch row attributes, unless we
3976 // removed _all_ columns, in this case, we remove
3977 // all row attributes.
3978 // I hate to do this here, but the
3979 // needed data is not available inside UpdateAttrCols.
3980 if ( !GetNumberCols() )
3981 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
3984 if ( !GetBatchCount() )
3987 m_colLabelWin
->Refresh();
3992 // There is no path to this code !!!!!!
3998 if (result
&& !GetBatchCount() )
3999 m_gridWin
->Refresh();
4004 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4006 wxRegionIterator
iter( reg
);
4009 m_rowLabelsExposed
.Empty();
4016 // TODO: remove this when we can...
4017 // There is a bug in wxMotif that gives garbage update
4018 // rectangles if you jump-scroll a long way by clicking the
4019 // scrollbar with middle button. This is a work-around
4021 #if defined(__WXMOTIF__)
4023 m_gridWin
->GetClientSize( &cw
, &ch
);
4024 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4025 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4028 // logical bounds of update region
4031 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4032 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4034 // find the row labels within these bounds
4037 for ( row
= 0; row
< m_numRows
; row
++ )
4039 if ( GetRowBottom(row
) < top
)
4042 if ( GetRowTop(row
) > bottom
)
4045 m_rowLabelsExposed
.Add( row
);
4053 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4055 wxRegionIterator
iter( reg
);
4058 m_colLabelsExposed
.Empty();
4065 // TODO: remove this when we can...
4066 // There is a bug in wxMotif that gives garbage update
4067 // rectangles if you jump-scroll a long way by clicking the
4068 // scrollbar with middle button. This is a work-around
4070 #if defined(__WXMOTIF__)
4072 m_gridWin
->GetClientSize( &cw
, &ch
);
4073 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4074 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4077 // logical bounds of update region
4080 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4081 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4083 // find the cells within these bounds
4086 for ( col
= 0; col
< m_numCols
; col
++ )
4088 if ( GetColRight(col
) < left
)
4091 if ( GetColLeft(col
) > right
)
4094 m_colLabelsExposed
.Add( col
);
4102 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4104 wxRegionIterator
iter( reg
);
4107 m_cellsExposed
.Empty();
4108 m_rowsExposed
.Empty();
4109 m_colsExposed
.Empty();
4111 int left
, top
, right
, bottom
;
4116 // TODO: remove this when we can...
4117 // There is a bug in wxMotif that gives garbage update
4118 // rectangles if you jump-scroll a long way by clicking the
4119 // scrollbar with middle button. This is a work-around
4121 #if defined(__WXMOTIF__)
4123 m_gridWin
->GetClientSize( &cw
, &ch
);
4124 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4125 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4126 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4127 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4130 // logical bounds of update region
4132 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4133 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4135 // find the cells within these bounds
4138 for ( row
= 0; row
< m_numRows
; row
++ )
4140 if ( GetRowBottom(row
) <= top
)
4143 if ( GetRowTop(row
) > bottom
)
4146 m_rowsExposed
.Add( row
);
4148 for ( col
= 0; col
< m_numCols
; col
++ )
4150 if ( GetColRight(col
) <= left
)
4153 if ( GetColLeft(col
) > right
)
4156 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4157 m_colsExposed
.Add( col
);
4158 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4167 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4170 wxPoint
pos( event
.GetPosition() );
4171 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4173 if ( event
.Dragging() )
4175 m_isDragging
= TRUE
;
4177 if ( event
.LeftIsDown() )
4179 switch( m_cursorMode
)
4181 case WXGRID_CURSOR_RESIZE_ROW
:
4183 int cw
, ch
, left
, dummy
;
4184 m_gridWin
->GetClientSize( &cw
, &ch
);
4185 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4187 wxClientDC
dc( m_gridWin
);
4190 GetRowTop(m_dragRowOrCol
) +
4191 GetRowMinimalHeight(m_dragRowOrCol
) );
4192 dc
.SetLogicalFunction(wxINVERT
);
4193 if ( m_dragLastPos
>= 0 )
4195 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4197 dc
.DrawLine( left
, y
, left
+cw
, y
);
4202 case WXGRID_CURSOR_SELECT_ROW
:
4203 if ( (row
= YToRow( y
)) >= 0 )
4205 m_selection
->SelectRow( row
,
4206 event
.ControlDown(),
4212 // default label to suppress warnings about "enumeration value
4213 // 'xxx' not handled in switch
4221 m_isDragging
= FALSE
;
4224 // ------------ Entering or leaving the window
4226 if ( event
.Entering() || event
.Leaving() )
4228 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4232 // ------------ Left button pressed
4234 else if ( event
.LeftDown() )
4236 // don't send a label click event for a hit on the
4237 // edge of the row label - this is probably the user
4238 // wanting to resize the row
4240 if ( YToEdgeOfRow(y
) < 0 )
4244 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4246 if ( !event
.ShiftDown() && !event
.ControlDown() )
4248 if ( event
.ShiftDown() )
4249 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4252 GetNumberCols() - 1,
4253 event
.ControlDown(),
4258 m_selection
->SelectRow( row
,
4259 event
.ControlDown(),
4263 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4268 // starting to drag-resize a row
4270 if ( CanDragRowSize() )
4271 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4276 // ------------ Left double click
4278 else if (event
.LeftDClick() )
4280 if ( YToEdgeOfRow(y
) < 0 )
4283 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4288 // ------------ Left button released
4290 else if ( event
.LeftUp() )
4292 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4294 DoEndDragResizeRow();
4296 // Note: we are ending the event *after* doing
4297 // default processing in this case
4299 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4302 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4307 // ------------ Right button down
4309 else if ( event
.RightDown() )
4312 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4314 // no default action at the moment
4319 // ------------ Right double click
4321 else if ( event
.RightDClick() )
4324 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4326 // no default action at the moment
4331 // ------------ No buttons down and mouse moving
4333 else if ( event
.Moving() )
4335 m_dragRowOrCol
= YToEdgeOfRow( y
);
4336 if ( m_dragRowOrCol
>= 0 )
4338 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4340 // don't capture the mouse yet
4341 if ( CanDragRowSize() )
4342 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4345 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4347 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4353 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4356 wxPoint
pos( event
.GetPosition() );
4357 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4359 if ( event
.Dragging() )
4361 m_isDragging
= TRUE
;
4363 if ( event
.LeftIsDown() )
4365 switch( m_cursorMode
)
4367 case WXGRID_CURSOR_RESIZE_COL
:
4369 int cw
, ch
, dummy
, top
;
4370 m_gridWin
->GetClientSize( &cw
, &ch
);
4371 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4373 wxClientDC
dc( m_gridWin
);
4376 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4377 GetColMinimalWidth(m_dragRowOrCol
));
4378 dc
.SetLogicalFunction(wxINVERT
);
4379 if ( m_dragLastPos
>= 0 )
4381 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4383 dc
.DrawLine( x
, top
, x
, top
+ch
);
4388 case WXGRID_CURSOR_SELECT_COL
:
4389 if ( (col
= XToCol( x
)) >= 0 )
4391 m_selection
->SelectCol( col
,
4392 event
.ControlDown(),
4398 // default label to suppress warnings about "enumeration value
4399 // 'xxx' not handled in switch
4407 m_isDragging
= FALSE
;
4410 // ------------ Entering or leaving the window
4412 if ( event
.Entering() || event
.Leaving() )
4414 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4418 // ------------ Left button pressed
4420 else if ( event
.LeftDown() )
4422 // don't send a label click event for a hit on the
4423 // edge of the col label - this is probably the user
4424 // wanting to resize the col
4426 if ( XToEdgeOfCol(x
) < 0 )
4430 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4432 if ( !event
.ShiftDown() && !event
.ControlDown() )
4434 if ( event
.ShiftDown() )
4435 m_selection
->SelectBlock( 0,
4436 m_currentCellCoords
.GetCol(),
4437 GetNumberRows() - 1, col
,
4438 event
.ControlDown(),
4443 m_selection
->SelectCol( col
,
4444 event
.ControlDown(),
4448 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4453 // starting to drag-resize a col
4455 if ( CanDragColSize() )
4456 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4461 // ------------ Left double click
4463 if ( event
.LeftDClick() )
4465 if ( XToEdgeOfCol(x
) < 0 )
4468 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4473 // ------------ Left button released
4475 else if ( event
.LeftUp() )
4477 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4479 DoEndDragResizeCol();
4481 // Note: we are ending the event *after* doing
4482 // default processing in this case
4484 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4487 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4492 // ------------ Right button down
4494 else if ( event
.RightDown() )
4497 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4499 // no default action at the moment
4504 // ------------ Right double click
4506 else if ( event
.RightDClick() )
4509 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4511 // no default action at the moment
4516 // ------------ No buttons down and mouse moving
4518 else if ( event
.Moving() )
4520 m_dragRowOrCol
= XToEdgeOfCol( x
);
4521 if ( m_dragRowOrCol
>= 0 )
4523 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4525 // don't capture the cursor yet
4526 if ( CanDragColSize() )
4527 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4530 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4532 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4538 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4540 if ( event
.LeftDown() )
4542 // indicate corner label by having both row and
4545 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4551 else if ( event
.LeftDClick() )
4553 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4556 else if ( event
.RightDown() )
4558 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4560 // no default action at the moment
4564 else if ( event
.RightDClick() )
4566 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4568 // no default action at the moment
4573 void wxGrid::ChangeCursorMode(CursorMode mode
,
4578 static const wxChar
*cursorModes
[] =
4587 wxLogTrace(_T("grid"),
4588 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4589 win
== m_colLabelWin
? _T("colLabelWin")
4590 : win
? _T("rowLabelWin")
4592 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4593 #endif // __WXDEBUG__
4595 if ( mode
== m_cursorMode
)
4600 // by default use the grid itself
4606 m_winCapture
->ReleaseMouse();
4607 m_winCapture
= (wxWindow
*)NULL
;
4610 m_cursorMode
= mode
;
4612 switch ( m_cursorMode
)
4614 case WXGRID_CURSOR_RESIZE_ROW
:
4615 win
->SetCursor( m_rowResizeCursor
);
4618 case WXGRID_CURSOR_RESIZE_COL
:
4619 win
->SetCursor( m_colResizeCursor
);
4623 win
->SetCursor( *wxSTANDARD_CURSOR
);
4626 // we need to capture mouse when resizing
4627 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4628 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4630 if ( captureMouse
&& resize
)
4632 win
->CaptureMouse();
4637 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4640 wxPoint
pos( event
.GetPosition() );
4641 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4643 wxGridCellCoords coords
;
4644 XYToCell( x
, y
, coords
);
4646 if ( event
.Dragging() )
4648 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4650 // Don't start doing anything until the mouse has been drug at
4651 // least 3 pixels in any direction...
4654 if (m_startDragPos
== wxDefaultPosition
)
4656 m_startDragPos
= pos
;
4659 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4663 m_isDragging
= TRUE
;
4664 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4666 // Hide the edit control, so it
4667 // won't interfer with drag-shrinking.
4668 if ( IsCellEditControlShown() )
4670 HideCellEditControl();
4671 SaveEditControlValue();
4674 // Have we captured the mouse yet?
4677 m_winCapture
= m_gridWin
;
4678 m_winCapture
->CaptureMouse();
4681 if ( coords
!= wxGridNoCellCoords
)
4683 if ( event
.ControlDown() )
4685 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4686 m_selectingKeyboard
= coords
;
4687 HighlightBlock ( m_selectingKeyboard
, coords
);
4691 if ( !IsSelection() )
4693 HighlightBlock( coords
, coords
);
4697 HighlightBlock( m_currentCellCoords
, coords
);
4701 if (! IsVisible(coords
))
4703 MakeCellVisible(coords
);
4704 // TODO: need to introduce a delay or something here. The
4705 // scrolling is way to fast, at least on MSW - also on GTK.
4709 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4711 int cw
, ch
, left
, dummy
;
4712 m_gridWin
->GetClientSize( &cw
, &ch
);
4713 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4715 wxClientDC
dc( m_gridWin
);
4717 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4718 GetRowMinimalHeight(m_dragRowOrCol
) );
4719 dc
.SetLogicalFunction(wxINVERT
);
4720 if ( m_dragLastPos
>= 0 )
4722 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4724 dc
.DrawLine( left
, y
, left
+cw
, y
);
4727 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4729 int cw
, ch
, dummy
, top
;
4730 m_gridWin
->GetClientSize( &cw
, &ch
);
4731 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4733 wxClientDC
dc( m_gridWin
);
4735 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4736 GetColMinimalWidth(m_dragRowOrCol
) );
4737 dc
.SetLogicalFunction(wxINVERT
);
4738 if ( m_dragLastPos
>= 0 )
4740 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4742 dc
.DrawLine( x
, top
, x
, top
+ch
);
4749 m_isDragging
= FALSE
;
4750 m_startDragPos
= wxDefaultPosition
;
4752 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4753 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4756 if ( event
.Entering() || event
.Leaving() )
4758 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4759 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4764 // ------------ Left button pressed
4766 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4768 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4773 if ( !event
.ControlDown() )
4775 if ( event
.ShiftDown() )
4777 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4778 m_currentCellCoords
.GetCol(),
4781 event
.ControlDown(),
4786 else if ( XToEdgeOfCol(x
) < 0 &&
4787 YToEdgeOfRow(y
) < 0 )
4789 DisableCellEditControl();
4790 MakeCellVisible( coords
);
4792 // if this is the second click on this cell then start
4794 if ( m_waitForSlowClick
&&
4795 (coords
== m_currentCellCoords
) &&
4796 CanEnableCellControl())
4798 EnableCellEditControl();
4800 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4801 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4804 editor
->StartingClick();
4808 m_waitForSlowClick
= FALSE
;
4812 if ( event
.ControlDown() )
4814 m_selection
->ToggleCellSelection( coords
.GetRow(),
4816 event
.ControlDown(),
4820 m_selectingTopLeft
= wxGridNoCellCoords
;
4821 m_selectingBottomRight
= wxGridNoCellCoords
;
4822 m_selectingKeyboard
= coords
;
4826 SetCurrentCell( coords
);
4827 if ( m_selection
->GetSelectionMode()
4828 != wxGrid::wxGridSelectCells
)
4829 HighlightBlock( coords
, coords
);
4831 m_waitForSlowClick
= TRUE
;
4838 // ------------ Left double click
4840 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4842 DisableCellEditControl();
4844 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4846 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4854 // ------------ Left button released
4856 else if ( event
.LeftUp() )
4858 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4860 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4861 m_selectingBottomRight
!= wxGridNoCellCoords
)
4865 m_winCapture
->ReleaseMouse();
4866 m_winCapture
= NULL
;
4868 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4869 m_selectingTopLeft
.GetCol(),
4870 m_selectingBottomRight
.GetRow(),
4871 m_selectingBottomRight
.GetCol(),
4872 event
.ControlDown(),
4876 m_selectingTopLeft
= wxGridNoCellCoords
;
4877 m_selectingBottomRight
= wxGridNoCellCoords
;
4880 // Show the edit control, if it has been hidden for
4882 ShowCellEditControl();
4884 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4886 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4887 DoEndDragResizeRow();
4889 // Note: we are ending the event *after* doing
4890 // default processing in this case
4892 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4894 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4896 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4897 DoEndDragResizeCol();
4899 // Note: we are ending the event *after* doing
4900 // default processing in this case
4902 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4909 // ------------ Right button down
4911 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4913 DisableCellEditControl();
4914 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4919 // no default action at the moment
4924 // ------------ Right double click
4926 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4928 DisableCellEditControl();
4929 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4934 // no default action at the moment
4938 // ------------ Moving and no button action
4940 else if ( event
.Moving() && !event
.IsButton() )
4942 int dragRow
= YToEdgeOfRow( y
);
4943 int dragCol
= XToEdgeOfCol( x
);
4945 // Dragging on the corner of a cell to resize in both
4946 // directions is not implemented yet...
4948 if ( dragRow
>= 0 && dragCol
>= 0 )
4950 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4956 m_dragRowOrCol
= dragRow
;
4958 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4960 if ( CanDragRowSize() && CanDragGridSize() )
4961 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4966 m_dragRowOrCol
= dragCol
;
4974 m_dragRowOrCol
= dragCol
;
4976 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4978 if ( CanDragColSize() && CanDragGridSize() )
4979 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4985 // Neither on a row or col edge
4987 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4989 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4995 void wxGrid::DoEndDragResizeRow()
4997 if ( m_dragLastPos
>= 0 )
4999 // erase the last line and resize the row
5001 int cw
, ch
, left
, dummy
;
5002 m_gridWin
->GetClientSize( &cw
, &ch
);
5003 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5005 wxClientDC
dc( m_gridWin
);
5007 dc
.SetLogicalFunction( wxINVERT
);
5008 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5009 HideCellEditControl();
5010 SaveEditControlValue();
5012 int rowTop
= GetRowTop(m_dragRowOrCol
);
5013 SetRowSize( m_dragRowOrCol
,
5014 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5016 if ( !GetBatchCount() )
5018 // Only needed to get the correct rect.y:
5019 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5021 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5022 rect
.width
= m_rowLabelWidth
;
5023 rect
.height
= ch
- rect
.y
;
5024 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5026 m_gridWin
->Refresh( FALSE
, &rect
);
5029 ShowCellEditControl();
5034 void wxGrid::DoEndDragResizeCol()
5036 if ( m_dragLastPos
>= 0 )
5038 // erase the last line and resize the col
5040 int cw
, ch
, dummy
, top
;
5041 m_gridWin
->GetClientSize( &cw
, &ch
);
5042 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5044 wxClientDC
dc( m_gridWin
);
5046 dc
.SetLogicalFunction( wxINVERT
);
5047 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5048 HideCellEditControl();
5049 SaveEditControlValue();
5051 int colLeft
= GetColLeft(m_dragRowOrCol
);
5052 SetColSize( m_dragRowOrCol
,
5053 wxMax( m_dragLastPos
- colLeft
,
5054 GetColMinimalWidth(m_dragRowOrCol
) ) );
5056 if ( !GetBatchCount() )
5058 // Only needed to get the correct rect.x:
5059 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5061 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5062 rect
.width
= cw
- rect
.x
;
5063 rect
.height
= m_colLabelHeight
;
5064 m_colLabelWin
->Refresh( TRUE
, &rect
);
5066 m_gridWin
->Refresh( FALSE
, &rect
);
5069 ShowCellEditControl();
5076 // ------ interaction with data model
5078 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5080 switch ( msg
.GetId() )
5082 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5083 return GetModelValues();
5085 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5086 return SetModelValues();
5088 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5089 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5090 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5091 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5092 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5093 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5094 return Redimension( msg
);
5103 // The behaviour of this function depends on the grid table class
5104 // Clear() function. For the default wxGridStringTable class the
5105 // behavious is to replace all cell contents with wxEmptyString but
5106 // not to change the number of rows or cols.
5108 void wxGrid::ClearGrid()
5112 if (IsCellEditControlEnabled())
5113 DisableCellEditControl();
5116 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5121 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5123 // TODO: something with updateLabels flag
5127 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5133 if (IsCellEditControlEnabled())
5134 DisableCellEditControl();
5136 return m_table
->InsertRows( pos
, numRows
);
5138 // the table will have sent the results of the insert row
5139 // operation to this view object as a grid table message
5145 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5147 // TODO: something with updateLabels flag
5151 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5155 return ( m_table
&& m_table
->AppendRows( numRows
) );
5156 // the table will have sent the results of the append row
5157 // operation to this view object as a grid table message
5161 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5163 // TODO: something with updateLabels flag
5167 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5173 if (IsCellEditControlEnabled())
5174 DisableCellEditControl();
5176 return (m_table
->DeleteRows( pos
, numRows
));
5177 // the table will have sent the results of the delete row
5178 // operation to this view object as a grid table message
5184 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5186 // TODO: something with updateLabels flag
5190 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5196 if (IsCellEditControlEnabled())
5197 DisableCellEditControl();
5199 return m_table
->InsertCols( pos
, numCols
);
5200 // the table will have sent the results of the insert col
5201 // operation to this view object as a grid table message
5207 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5209 // TODO: something with updateLabels flag
5213 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5217 return ( m_table
&& m_table
->AppendCols( numCols
) );
5218 // the table will have sent the results of the append col
5219 // operation to this view object as a grid table message
5223 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5225 // TODO: something with updateLabels flag
5229 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5235 if (IsCellEditControlEnabled())
5236 DisableCellEditControl();
5238 return ( m_table
->DeleteCols( pos
, numCols
) );
5239 // the table will have sent the results of the delete col
5240 // operation to this view object as a grid table message
5248 // ----- event handlers
5251 // Generate a grid event based on a mouse event and
5252 // return the result of ProcessEvent()
5254 bool wxGrid::SendEvent( const wxEventType type
,
5256 wxMouseEvent
& mouseEv
)
5258 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5260 int rowOrCol
= (row
== -1 ? col
: row
);
5262 wxGridSizeEvent
gridEvt( GetId(),
5266 mouseEv
.GetX() + GetColLabelSize(),
5267 mouseEv
.GetY() + GetRowLabelSize(),
5268 mouseEv
.ControlDown(),
5269 mouseEv
.ShiftDown(),
5271 mouseEv
.MetaDown() );
5273 return GetEventHandler()->ProcessEvent(gridEvt
);
5275 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5277 // Right now, it should _never_ end up here!
5278 wxGridRangeSelectEvent
gridEvt( GetId(),
5282 m_selectingBottomRight
,
5284 mouseEv
.ControlDown(),
5285 mouseEv
.ShiftDown(),
5287 mouseEv
.MetaDown() );
5289 return GetEventHandler()->ProcessEvent(gridEvt
);
5293 wxGridEvent
gridEvt( GetId(),
5297 mouseEv
.GetX() + GetColLabelSize(),
5298 mouseEv
.GetY() + GetRowLabelSize(),
5300 mouseEv
.ControlDown(),
5301 mouseEv
.ShiftDown(),
5303 mouseEv
.MetaDown() );
5305 return GetEventHandler()->ProcessEvent(gridEvt
);
5310 // Generate a grid event of specified type and return the result
5311 // of ProcessEvent().
5313 bool wxGrid::SendEvent( const wxEventType type
,
5316 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5318 int rowOrCol
= (row
== -1 ? col
: row
);
5320 wxGridSizeEvent
gridEvt( GetId(),
5325 return GetEventHandler()->ProcessEvent(gridEvt
);
5329 wxGridEvent
gridEvt( GetId(),
5334 return GetEventHandler()->ProcessEvent(gridEvt
);
5339 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5341 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5345 // This is just here to make sure that CalcDimensions gets called when
5346 // the grid view is resized... then the size event is skipped to allow
5347 // the box sizers to handle everything
5349 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5356 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5358 if ( m_inOnKeyDown
)
5360 // shouldn't be here - we are going round in circles...
5362 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5365 m_inOnKeyDown
= TRUE
;
5367 // propagate the event up and see if it gets processed
5369 wxWindow
*parent
= GetParent();
5370 wxKeyEvent
keyEvt( event
);
5371 keyEvt
.SetEventObject( parent
);
5373 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5376 // try local handlers
5378 switch ( event
.KeyCode() )
5381 if ( event
.ControlDown() )
5383 MoveCursorUpBlock( event
.ShiftDown() );
5387 MoveCursorUp( event
.ShiftDown() );
5392 if ( event
.ControlDown() )
5394 MoveCursorDownBlock( event
.ShiftDown() );
5398 MoveCursorDown( event
.ShiftDown() );
5403 if ( event
.ControlDown() )
5405 MoveCursorLeftBlock( event
.ShiftDown() );
5409 MoveCursorLeft( event
.ShiftDown() );
5414 if ( event
.ControlDown() )
5416 MoveCursorRightBlock( event
.ShiftDown() );
5420 MoveCursorRight( event
.ShiftDown() );
5425 if ( event
.ControlDown() )
5427 event
.Skip(); // to let the edit control have the return
5431 if ( GetGridCursorRow() < GetNumberRows()-1 )
5433 MoveCursorDown( event
.ShiftDown() );
5437 // at the bottom of a column
5438 HideCellEditControl();
5439 SaveEditControlValue();
5449 if (event
.ShiftDown())
5451 if ( GetGridCursorCol() > 0 )
5453 MoveCursorLeft( FALSE
);
5458 HideCellEditControl();
5459 SaveEditControlValue();
5464 if ( GetGridCursorCol() < GetNumberCols()-1 )
5466 MoveCursorRight( FALSE
);
5471 HideCellEditControl();
5472 SaveEditControlValue();
5478 if ( event
.ControlDown() )
5480 MakeCellVisible( 0, 0 );
5481 SetCurrentCell( 0, 0 );
5490 if ( event
.ControlDown() )
5492 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5493 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5510 if ( event
.ControlDown() )
5512 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5513 m_currentCellCoords
.GetCol(),
5514 event
.ControlDown(),
5520 if ( !IsEditable() )
5522 MoveCursorRight( FALSE
);
5525 // Otherwise fall through to default
5528 // is it possible to edit the current cell at all?
5529 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5531 // yes, now check whether the cells editor accepts the key
5532 int row
= m_currentCellCoords
.GetRow();
5533 int col
= m_currentCellCoords
.GetCol();
5534 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5535 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5537 // <F2> is special and will always start editing, for
5538 // other keys - ask the editor itself
5539 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5540 || editor
->IsAcceptedKey(event
) )
5542 EnableCellEditControl();
5543 editor
->StartingKey(event
);
5555 // let others process char events with modifiers or all
5556 // char events for readonly cells
5563 m_inOnKeyDown
= FALSE
;
5566 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5568 // try local handlers
5570 if ( event
.KeyCode() == WXK_SHIFT
)
5572 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5573 m_selectingBottomRight
!= wxGridNoCellCoords
)
5574 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5575 m_selectingTopLeft
.GetCol(),
5576 m_selectingBottomRight
.GetRow(),
5577 m_selectingBottomRight
.GetCol(),
5578 event
.ControlDown(),
5582 m_selectingTopLeft
= wxGridNoCellCoords
;
5583 m_selectingBottomRight
= wxGridNoCellCoords
;
5584 m_selectingKeyboard
= wxGridNoCellCoords
;
5588 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5592 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5594 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5596 // the event has been intercepted - do nothing
5600 wxClientDC
dc(m_gridWin
);
5603 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5605 HideCellEditControl();
5606 DisableCellEditControl();
5608 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5611 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5612 if ( !m_gridLinesEnabled
)
5620 CalcCellsExposed( r
);
5622 // Otherwise refresh redraws the highlight!
5623 m_currentCellCoords
= coords
;
5625 DrawGridCellArea(dc
);
5626 DrawAllGridLines( dc
, r
);
5630 m_currentCellCoords
= coords
;
5632 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5633 DrawCellHighlight(dc
, attr
);
5638 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5641 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5643 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5646 rightCol
= GetNumberCols() - 1;
5648 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5651 bottomRow
= GetNumberRows() - 1;
5653 if ( topRow
> bottomRow
)
5660 if ( leftCol
> rightCol
)
5667 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5668 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5670 if ( m_selectingTopLeft
!= updateTopLeft
||
5671 m_selectingBottomRight
!= updateBottomRight
)
5673 // Compute two optimal update rectangles:
5674 // Either one rectangle is a real subset of the
5675 // other, or they are (almost) disjoint!
5677 bool need_refresh
[4];
5681 need_refresh
[3] = FALSE
;
5684 // Store intermediate values
5685 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5686 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5687 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5688 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5690 // Determine the outer/inner coordinates.
5691 if (oldLeft
> leftCol
)
5697 if (oldTop
> topRow
)
5703 if (oldRight
< rightCol
)
5706 oldRight
= rightCol
;
5709 if (oldBottom
< bottomRow
)
5712 oldBottom
= bottomRow
;
5716 // Now, either the stuff marked old is the outer
5717 // rectangle or we don't have a situation where one
5718 // is contained in the other.
5720 if ( oldLeft
< leftCol
)
5722 need_refresh
[0] = TRUE
;
5723 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5725 wxGridCellCoords ( oldBottom
,
5729 if ( oldTop
< topRow
)
5731 need_refresh
[1] = TRUE
;
5732 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5734 wxGridCellCoords ( topRow
- 1,
5738 if ( oldRight
> rightCol
)
5740 need_refresh
[2] = TRUE
;
5741 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5743 wxGridCellCoords ( oldBottom
,
5747 if ( oldBottom
> bottomRow
)
5749 need_refresh
[3] = TRUE
;
5750 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5752 wxGridCellCoords ( oldBottom
,
5758 m_selectingTopLeft
= updateTopLeft
;
5759 m_selectingBottomRight
= updateBottomRight
;
5761 // various Refresh() calls
5762 for (i
= 0; i
< 4; i
++ )
5763 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5764 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5767 // never generate an event as it will be generated from
5768 // wxGridSelection::SelectBlock!
5769 // (old comment from when this was the body of SelectBlock)
5774 // ------ functions to get/send data (see also public functions)
5777 bool wxGrid::GetModelValues()
5781 // all we need to do is repaint the grid
5783 m_gridWin
->Refresh();
5791 bool wxGrid::SetModelValues()
5797 for ( row
= 0; row
< m_numRows
; row
++ )
5799 for ( col
= 0; col
< m_numCols
; col
++ )
5801 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5813 // Note - this function only draws cells that are in the list of
5814 // exposed cells (usually set from the update region by
5815 // CalcExposedCells)
5817 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5819 if ( !m_numRows
|| !m_numCols
) return;
5822 size_t numCells
= m_cellsExposed
.GetCount();
5824 for ( i
= 0; i
< numCells
; i
++ )
5826 DrawCell( dc
, m_cellsExposed
[i
] );
5831 void wxGrid::DrawGridSpace( wxDC
& dc
)
5834 m_gridWin
->GetClientSize( &cw
, &ch
);
5837 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5839 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5840 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5842 if ( right
> rightCol
|| bottom
> bottomRow
)
5845 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5847 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5848 dc
.SetPen( *wxTRANSPARENT_PEN
);
5850 if ( right
> rightCol
)
5852 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5855 if ( bottom
> bottomRow
)
5857 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5863 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5865 int row
= coords
.GetRow();
5866 int col
= coords
.GetCol();
5868 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5871 // we draw the cell border ourselves
5872 #if !WXGRID_DRAW_LINES
5873 if ( m_gridLinesEnabled
)
5874 DrawCellBorder( dc
, coords
);
5877 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5879 bool isCurrent
= coords
== m_currentCellCoords
;
5881 wxRect rect
= CellToRect( row
, col
);
5883 // if the editor is shown, we should use it and not the renderer
5884 // Note: However, only if it is really _shown_, i.e. not hidden!
5885 if ( isCurrent
&& IsCellEditControlShown() )
5887 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5888 editor
->PaintBackground(rect
, attr
);
5893 // but all the rest is drawn by the cell renderer and hence may be
5895 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5896 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5903 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5905 int row
= m_currentCellCoords
.GetRow();
5906 int col
= m_currentCellCoords
.GetCol();
5908 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5911 wxRect rect
= CellToRect(row
, col
);
5913 // hmmm... what could we do here to show that the cell is disabled?
5914 // for now, I just draw a thinner border than for the other ones, but
5915 // it doesn't look really good
5916 dc
.SetPen(wxPen(m_cellHighlightColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5917 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5919 dc
.DrawRectangle(rect
);
5922 // VZ: my experiments with 3d borders...
5924 // how to properly set colours for arbitrary bg?
5925 wxCoord x1
= rect
.x
,
5927 x2
= rect
.x
+ rect
.width
-1,
5928 y2
= rect
.y
+ rect
.height
-1;
5930 dc
.SetPen(*wxWHITE_PEN
);
5931 dc
.DrawLine(x1
, y1
, x2
, y1
);
5932 dc
.DrawLine(x1
, y1
, x1
, y2
);
5934 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5935 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5937 dc
.SetPen(*wxBLACK_PEN
);
5938 dc
.DrawLine(x1
, y2
, x2
, y2
);
5939 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5944 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5946 int row
= coords
.GetRow();
5947 int col
= coords
.GetCol();
5948 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5951 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5953 // right hand border
5955 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5956 GetColRight(col
), GetRowBottom(row
) );
5960 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5961 GetColRight(col
), GetRowBottom(row
) );
5964 void wxGrid::DrawHighlight(wxDC
& dc
)
5966 // This if block was previously in wxGrid::OnPaint but that doesn't
5967 // seem to get called under wxGTK - MB
5969 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5970 m_numRows
&& m_numCols
)
5972 m_currentCellCoords
.Set(0, 0);
5975 if ( IsCellEditControlShown() )
5977 // don't show highlight when the edit control is shown
5981 // if the active cell was repainted, repaint its highlight too because it
5982 // might have been damaged by the grid lines
5983 size_t count
= m_cellsExposed
.GetCount();
5984 for ( size_t n
= 0; n
< count
; n
++ )
5986 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5988 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5989 DrawCellHighlight(dc
, attr
);
5997 // TODO: remove this ???
5998 // This is used to redraw all grid lines e.g. when the grid line colour
6001 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED_GTK(reg
) )
6003 if ( !m_gridLinesEnabled
||
6005 !m_numCols
) return;
6007 int top
, bottom
, left
, right
;
6009 #if 0 //#ifndef __WXGTK__
6013 m_gridWin
->GetClientSize(&cw
, &ch
);
6015 // virtual coords of visible area
6017 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6018 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6023 reg
.GetBox(x
, y
, w
, h
);
6024 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6025 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6029 m_gridWin
->GetClientSize(&cw
, &ch
);
6030 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6031 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6034 // avoid drawing grid lines past the last row and col
6036 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6037 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6039 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6041 // horizontal grid lines
6044 for ( i
= 0; i
< m_numRows
; i
++ )
6046 int bot
= GetRowBottom(i
) - 1;
6055 dc
.DrawLine( left
, bot
, right
, bot
);
6060 // vertical grid lines
6062 for ( i
= 0; i
< m_numCols
; i
++ )
6064 int colRight
= GetColRight(i
) - 1;
6065 if ( colRight
> right
)
6070 if ( colRight
>= left
)
6072 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6078 void wxGrid::DrawRowLabels( wxDC
& dc
)
6080 if ( !m_numRows
) return;
6083 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6085 for ( i
= 0; i
< numLabels
; i
++ )
6087 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6092 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6094 if ( GetRowHeight(row
) <= 0 )
6097 int rowTop
= GetRowTop(row
),
6098 rowBottom
= GetRowBottom(row
) - 1;
6100 dc
.SetPen( *wxBLACK_PEN
);
6101 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6102 m_rowLabelWidth
-1, rowBottom
);
6104 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6106 dc
.SetPen( *wxWHITE_PEN
);
6107 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6108 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6110 dc
.SetBackgroundMode( wxTRANSPARENT
);
6111 dc
.SetTextForeground( GetLabelTextColour() );
6112 dc
.SetFont( GetLabelFont() );
6115 GetRowLabelAlignment( &hAlign
, &vAlign
);
6119 rect
.SetY( GetRowTop(row
) + 2 );
6120 rect
.SetWidth( m_rowLabelWidth
- 4 );
6121 rect
.SetHeight( GetRowHeight(row
) - 4 );
6122 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6126 void wxGrid::DrawColLabels( wxDC
& dc
)
6128 if ( !m_numCols
) return;
6131 size_t numLabels
= m_colLabelsExposed
.GetCount();
6133 for ( i
= 0; i
< numLabels
; i
++ )
6135 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6140 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6142 if ( GetColWidth(col
) <= 0 )
6145 int colLeft
= GetColLeft(col
),
6146 colRight
= GetColRight(col
) - 1;
6148 dc
.SetPen( *wxBLACK_PEN
);
6149 dc
.DrawLine( colRight
, 0,
6150 colRight
, m_colLabelHeight
-1 );
6152 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6153 colRight
, m_colLabelHeight
-1 );
6155 dc
.SetPen( *wxWHITE_PEN
);
6156 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6157 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6159 dc
.SetBackgroundMode( wxTRANSPARENT
);
6160 dc
.SetTextForeground( GetLabelTextColour() );
6161 dc
.SetFont( GetLabelFont() );
6163 dc
.SetBackgroundMode( wxTRANSPARENT
);
6164 dc
.SetTextForeground( GetLabelTextColour() );
6165 dc
.SetFont( GetLabelFont() );
6168 GetColLabelAlignment( &hAlign
, &vAlign
);
6171 rect
.SetX( colLeft
+ 2 );
6173 rect
.SetWidth( GetColWidth(col
) - 4 );
6174 rect
.SetHeight( m_colLabelHeight
- 4 );
6175 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6179 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6180 const wxString
& value
,
6185 long textWidth
, textHeight
;
6186 long lineWidth
, lineHeight
;
6187 wxArrayString lines
;
6189 dc
.SetClippingRegion( rect
);
6190 StringToLines( value
, lines
);
6191 if ( lines
.GetCount() )
6193 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6194 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6197 switch ( horizAlign
)
6200 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6203 case wxALIGN_CENTRE
:
6204 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6213 switch ( vertAlign
)
6215 case wxALIGN_BOTTOM
:
6216 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6219 case wxALIGN_CENTRE
:
6220 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6229 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6231 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6236 dc
.DestroyClippingRegion();
6240 // Split multi line text up into an array of strings. Any existing
6241 // contents of the string array are preserved.
6243 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6247 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6248 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6250 while ( startPos
< (int)tVal
.Length() )
6252 pos
= tVal
.Mid(startPos
).Find( eol
);
6257 else if ( pos
== 0 )
6259 lines
.Add( wxEmptyString
);
6263 lines
.Add( value
.Mid(startPos
, pos
) );
6267 if ( startPos
< (int)value
.Length() )
6269 lines
.Add( value
.Mid( startPos
) );
6274 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6275 wxArrayString
& lines
,
6276 long *width
, long *height
)
6283 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6285 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6286 w
= wxMax( w
, lineW
);
6295 // ------ Batch processing.
6297 void wxGrid::EndBatch()
6299 if ( m_batchCount
> 0 )
6302 if ( !m_batchCount
)
6305 m_rowLabelWin
->Refresh();
6306 m_colLabelWin
->Refresh();
6307 m_cornerLabelWin
->Refresh();
6308 m_gridWin
->Refresh();
6313 // Use this, rather than wxWindow::Refresh(), to force an immediate
6314 // repainting of the grid. Has no effect if you are already inside a
6315 // BeginBatch / EndBatch block.
6317 void wxGrid::ForceRefresh()
6325 // ------ Edit control functions
6329 void wxGrid::EnableEditing( bool edit
)
6331 // TODO: improve this ?
6333 if ( edit
!= m_editable
)
6335 if(!edit
) EnableCellEditControl(edit
);
6341 void wxGrid::EnableCellEditControl( bool enable
)
6346 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6347 SetCurrentCell( 0, 0 );
6349 if ( enable
!= m_cellEditCtrlEnabled
)
6351 // TODO allow the app to Veto() this event?
6352 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6356 // this should be checked by the caller!
6357 wxASSERT_MSG( CanEnableCellControl(),
6358 _T("can't enable editing for this cell!") );
6360 // do it before ShowCellEditControl()
6361 m_cellEditCtrlEnabled
= enable
;
6363 ShowCellEditControl();
6367 HideCellEditControl();
6368 SaveEditControlValue();
6370 // do it after HideCellEditControl()
6371 m_cellEditCtrlEnabled
= enable
;
6376 bool wxGrid::IsCurrentCellReadOnly() const
6379 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6380 bool readonly
= attr
->IsReadOnly();
6386 bool wxGrid::CanEnableCellControl() const
6388 return m_editable
&& !IsCurrentCellReadOnly();
6391 bool wxGrid::IsCellEditControlEnabled() const
6393 // the cell edit control might be disable for all cells or just for the
6394 // current one if it's read only
6395 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6398 bool wxGrid::IsCellEditControlShown() const
6400 bool isShown
= FALSE
;
6402 if ( m_cellEditCtrlEnabled
)
6404 int row
= m_currentCellCoords
.GetRow();
6405 int col
= m_currentCellCoords
.GetCol();
6406 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6407 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6412 if ( editor
->IsCreated() )
6414 isShown
= editor
->GetControl()->IsShown();
6424 void wxGrid::ShowCellEditControl()
6426 if ( IsCellEditControlEnabled() )
6428 if ( !IsVisible( m_currentCellCoords
) )
6430 m_cellEditCtrlEnabled
= FALSE
;
6435 wxRect rect
= CellToRect( m_currentCellCoords
);
6436 int row
= m_currentCellCoords
.GetRow();
6437 int col
= m_currentCellCoords
.GetCol();
6439 // convert to scrolled coords
6441 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6443 // done in PaintBackground()
6445 // erase the highlight and the cell contents because the editor
6446 // might not cover the entire cell
6447 wxClientDC
dc( m_gridWin
);
6449 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6450 dc
.SetPen(*wxTRANSPARENT_PEN
);
6451 dc
.DrawRectangle(rect
);
6454 // cell is shifted by one pixel
6458 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6459 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6460 if ( !editor
->IsCreated() )
6462 editor
->Create(m_gridWin
, -1,
6463 new wxGridCellEditorEvtHandler(this, editor
));
6466 editor
->Show( TRUE
, attr
);
6468 editor
->SetSize( rect
);
6470 editor
->BeginEdit(row
, col
, this);
6479 void wxGrid::HideCellEditControl()
6481 if ( IsCellEditControlEnabled() )
6483 int row
= m_currentCellCoords
.GetRow();
6484 int col
= m_currentCellCoords
.GetCol();
6486 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6487 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6488 editor
->Show( FALSE
);
6491 m_gridWin
->SetFocus();
6492 wxRect
rect( CellToRect( row
, col
) );
6493 m_gridWin
->Refresh( FALSE
, &rect
);
6498 void wxGrid::SaveEditControlValue()
6500 if ( IsCellEditControlEnabled() )
6502 int row
= m_currentCellCoords
.GetRow();
6503 int col
= m_currentCellCoords
.GetCol();
6505 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6506 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6507 bool changed
= editor
->EndEdit(row
, col
, this);
6514 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6515 m_currentCellCoords
.GetRow(),
6516 m_currentCellCoords
.GetCol() );
6523 // ------ Grid location functions
6524 // Note that all of these functions work with the logical coordinates of
6525 // grid cells and labels so you will need to convert from device
6526 // coordinates for mouse events etc.
6529 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6531 int row
= YToRow(y
);
6532 int col
= XToCol(x
);
6534 if ( row
== -1 || col
== -1 )
6536 coords
= wxGridNoCellCoords
;
6540 coords
.Set( row
, col
);
6545 int wxGrid::YToRow( int y
)
6549 for ( i
= 0; i
< m_numRows
; i
++ )
6551 if ( y
< GetRowBottom(i
) )
6559 int wxGrid::XToCol( int x
)
6563 for ( i
= 0; i
< m_numCols
; i
++ )
6565 if ( x
< GetColRight(i
) )
6573 // return the row number that that the y coord is near the edge of, or
6574 // -1 if not near an edge
6576 int wxGrid::YToEdgeOfRow( int y
)
6580 for ( i
= 0; i
< m_numRows
; i
++ )
6582 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6584 d
= abs( y
- GetRowBottom(i
) );
6585 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6594 // return the col number that that the x coord is near the edge of, or
6595 // -1 if not near an edge
6597 int wxGrid::XToEdgeOfCol( int x
)
6601 for ( i
= 0; i
< m_numCols
; i
++ )
6603 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6605 d
= abs( x
- GetColRight(i
) );
6606 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6615 wxRect
wxGrid::CellToRect( int row
, int col
)
6617 wxRect
rect( -1, -1, -1, -1 );
6619 if ( row
>= 0 && row
< m_numRows
&&
6620 col
>= 0 && col
< m_numCols
)
6622 rect
.x
= GetColLeft(col
);
6623 rect
.y
= GetRowTop(row
);
6624 rect
.width
= GetColWidth(col
);
6625 rect
.height
= GetRowHeight(row
);
6628 // if grid lines are enabled, then the area of the cell is a bit smaller
6629 if (m_gridLinesEnabled
) {
6637 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6639 // get the cell rectangle in logical coords
6641 wxRect
r( CellToRect( row
, col
) );
6643 // convert to device coords
6645 int left
, top
, right
, bottom
;
6646 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6647 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6649 // check against the client area of the grid window
6652 m_gridWin
->GetClientSize( &cw
, &ch
);
6654 if ( wholeCellVisible
)
6656 // is the cell wholly visible ?
6658 return ( left
>= 0 && right
<= cw
&&
6659 top
>= 0 && bottom
<= ch
);
6663 // is the cell partly visible ?
6665 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6666 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6671 // make the specified cell location visible by doing a minimal amount
6674 void wxGrid::MakeCellVisible( int row
, int col
)
6677 int xpos
= -1, ypos
= -1;
6679 if ( row
>= 0 && row
< m_numRows
&&
6680 col
>= 0 && col
< m_numCols
)
6682 // get the cell rectangle in logical coords
6684 wxRect
r( CellToRect( row
, col
) );
6686 // convert to device coords
6688 int left
, top
, right
, bottom
;
6689 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6690 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6693 m_gridWin
->GetClientSize( &cw
, &ch
);
6699 else if ( bottom
> ch
)
6701 int h
= r
.GetHeight();
6703 for ( i
= row
-1; i
>= 0; i
-- )
6705 int rowHeight
= GetRowHeight(i
);
6706 if ( h
+ rowHeight
> ch
)
6713 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6714 // have rounding errors (this is important, because if we do, we
6715 // might not scroll at all and some cells won't be redrawn)
6716 ypos
+= GRID_SCROLL_LINE
/ 2;
6723 else if ( right
> cw
)
6725 int w
= r
.GetWidth();
6727 for ( i
= col
-1; i
>= 0; i
-- )
6729 int colWidth
= GetColWidth(i
);
6730 if ( w
+ colWidth
> cw
)
6737 // see comment for ypos above
6738 xpos
+= GRID_SCROLL_LINE
/ 2;
6741 if ( xpos
!= -1 || ypos
!= -1 )
6743 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6744 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6745 Scroll( xpos
, ypos
);
6753 // ------ Grid cursor movement functions
6756 bool wxGrid::MoveCursorUp( bool expandSelection
)
6758 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6759 m_currentCellCoords
.GetRow() >= 0 )
6761 if ( expandSelection
)
6763 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6764 m_selectingKeyboard
= m_currentCellCoords
;
6765 if ( m_selectingKeyboard
.GetRow() > 0 )
6767 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6768 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6769 m_selectingKeyboard
.GetCol() );
6770 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6773 else if ( m_currentCellCoords
.GetRow() > 0 )
6776 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6777 m_currentCellCoords
.GetCol() );
6778 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6779 m_currentCellCoords
.GetCol() );
6790 bool wxGrid::MoveCursorDown( bool expandSelection
)
6792 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6793 m_currentCellCoords
.GetRow() < m_numRows
)
6795 if ( expandSelection
)
6797 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6798 m_selectingKeyboard
= m_currentCellCoords
;
6799 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6801 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6802 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6803 m_selectingKeyboard
.GetCol() );
6804 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6807 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6810 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6811 m_currentCellCoords
.GetCol() );
6812 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6813 m_currentCellCoords
.GetCol() );
6824 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6826 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6827 m_currentCellCoords
.GetCol() >= 0 )
6829 if ( expandSelection
)
6831 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6832 m_selectingKeyboard
= m_currentCellCoords
;
6833 if ( m_selectingKeyboard
.GetCol() > 0 )
6835 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6836 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6837 m_selectingKeyboard
.GetCol() );
6838 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6841 else if ( m_currentCellCoords
.GetCol() > 0 )
6844 MakeCellVisible( m_currentCellCoords
.GetRow(),
6845 m_currentCellCoords
.GetCol() - 1 );
6846 SetCurrentCell( m_currentCellCoords
.GetRow(),
6847 m_currentCellCoords
.GetCol() - 1 );
6858 bool wxGrid::MoveCursorRight( bool expandSelection
)
6860 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6861 m_currentCellCoords
.GetCol() < m_numCols
)
6863 if ( expandSelection
)
6865 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6866 m_selectingKeyboard
= m_currentCellCoords
;
6867 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6869 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6870 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6871 m_selectingKeyboard
.GetCol() );
6872 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6875 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6878 MakeCellVisible( m_currentCellCoords
.GetRow(),
6879 m_currentCellCoords
.GetCol() + 1 );
6880 SetCurrentCell( m_currentCellCoords
.GetRow(),
6881 m_currentCellCoords
.GetCol() + 1 );
6892 bool wxGrid::MovePageUp()
6894 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6896 int row
= m_currentCellCoords
.GetRow();
6900 m_gridWin
->GetClientSize( &cw
, &ch
);
6902 int y
= GetRowTop(row
);
6903 int newRow
= YToRow( y
- ch
+ 1 );
6908 else if ( newRow
== row
)
6913 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6914 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6922 bool wxGrid::MovePageDown()
6924 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6926 int row
= m_currentCellCoords
.GetRow();
6927 if ( row
< m_numRows
)
6930 m_gridWin
->GetClientSize( &cw
, &ch
);
6932 int y
= GetRowTop(row
);
6933 int newRow
= YToRow( y
+ ch
);
6936 newRow
= m_numRows
- 1;
6938 else if ( newRow
== row
)
6943 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6944 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6952 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6955 m_currentCellCoords
!= wxGridNoCellCoords
&&
6956 m_currentCellCoords
.GetRow() > 0 )
6958 int row
= m_currentCellCoords
.GetRow();
6959 int col
= m_currentCellCoords
.GetCol();
6961 if ( m_table
->IsEmptyCell(row
, col
) )
6963 // starting in an empty cell: find the next block of
6969 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6972 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6974 // starting at the top of a block: find the next block
6980 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6985 // starting within a block: find the top of the block
6990 if ( m_table
->IsEmptyCell(row
, col
) )
6998 MakeCellVisible( row
, col
);
6999 if ( expandSelection
)
7001 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7002 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7007 SetCurrentCell( row
, col
);
7015 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7018 m_currentCellCoords
!= wxGridNoCellCoords
&&
7019 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7021 int row
= m_currentCellCoords
.GetRow();
7022 int col
= m_currentCellCoords
.GetCol();
7024 if ( m_table
->IsEmptyCell(row
, col
) )
7026 // starting in an empty cell: find the next block of
7029 while ( row
< m_numRows
-1 )
7032 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7035 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7037 // starting at the bottom of a block: find the next block
7040 while ( row
< m_numRows
-1 )
7043 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7048 // starting within a block: find the bottom of the block
7050 while ( row
< m_numRows
-1 )
7053 if ( m_table
->IsEmptyCell(row
, col
) )
7061 MakeCellVisible( row
, col
);
7062 if ( expandSelection
)
7064 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7065 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7070 SetCurrentCell( row
, col
);
7079 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7082 m_currentCellCoords
!= wxGridNoCellCoords
&&
7083 m_currentCellCoords
.GetCol() > 0 )
7085 int row
= m_currentCellCoords
.GetRow();
7086 int col
= m_currentCellCoords
.GetCol();
7088 if ( m_table
->IsEmptyCell(row
, col
) )
7090 // starting in an empty cell: find the next block of
7096 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7099 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7101 // starting at the left of a block: find the next block
7107 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7112 // starting within a block: find the left of the block
7117 if ( m_table
->IsEmptyCell(row
, col
) )
7125 MakeCellVisible( row
, col
);
7126 if ( expandSelection
)
7128 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7129 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7134 SetCurrentCell( row
, col
);
7143 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7146 m_currentCellCoords
!= wxGridNoCellCoords
&&
7147 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7149 int row
= m_currentCellCoords
.GetRow();
7150 int col
= m_currentCellCoords
.GetCol();
7152 if ( m_table
->IsEmptyCell(row
, col
) )
7154 // starting in an empty cell: find the next block of
7157 while ( col
< m_numCols
-1 )
7160 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7163 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7165 // starting at the right of a block: find the next block
7168 while ( col
< m_numCols
-1 )
7171 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7176 // starting within a block: find the right of the block
7178 while ( col
< m_numCols
-1 )
7181 if ( m_table
->IsEmptyCell(row
, col
) )
7189 MakeCellVisible( row
, col
);
7190 if ( expandSelection
)
7192 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7193 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7198 SetCurrentCell( row
, col
);
7210 // ------ Label values and formatting
7213 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7215 *horiz
= m_rowLabelHorizAlign
;
7216 *vert
= m_rowLabelVertAlign
;
7219 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7221 *horiz
= m_colLabelHorizAlign
;
7222 *vert
= m_colLabelVertAlign
;
7225 wxString
wxGrid::GetRowLabelValue( int row
)
7229 return m_table
->GetRowLabelValue( row
);
7239 wxString
wxGrid::GetColLabelValue( int col
)
7243 return m_table
->GetColLabelValue( col
);
7254 void wxGrid::SetRowLabelSize( int width
)
7256 width
= wxMax( width
, 0 );
7257 if ( width
!= m_rowLabelWidth
)
7261 m_rowLabelWin
->Show( FALSE
);
7262 m_cornerLabelWin
->Show( FALSE
);
7264 else if ( m_rowLabelWidth
== 0 )
7266 m_rowLabelWin
->Show( TRUE
);
7267 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7270 m_rowLabelWidth
= width
;
7277 void wxGrid::SetColLabelSize( int height
)
7279 height
= wxMax( height
, 0 );
7280 if ( height
!= m_colLabelHeight
)
7284 m_colLabelWin
->Show( FALSE
);
7285 m_cornerLabelWin
->Show( FALSE
);
7287 else if ( m_colLabelHeight
== 0 )
7289 m_colLabelWin
->Show( TRUE
);
7290 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7293 m_colLabelHeight
= height
;
7300 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7302 if ( m_labelBackgroundColour
!= colour
)
7304 m_labelBackgroundColour
= colour
;
7305 m_rowLabelWin
->SetBackgroundColour( colour
);
7306 m_colLabelWin
->SetBackgroundColour( colour
);
7307 m_cornerLabelWin
->SetBackgroundColour( colour
);
7309 if ( !GetBatchCount() )
7311 m_rowLabelWin
->Refresh();
7312 m_colLabelWin
->Refresh();
7313 m_cornerLabelWin
->Refresh();
7318 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7320 if ( m_labelTextColour
!= colour
)
7322 m_labelTextColour
= colour
;
7323 if ( !GetBatchCount() )
7325 m_rowLabelWin
->Refresh();
7326 m_colLabelWin
->Refresh();
7331 void wxGrid::SetLabelFont( const wxFont
& font
)
7334 if ( !GetBatchCount() )
7336 m_rowLabelWin
->Refresh();
7337 m_colLabelWin
->Refresh();
7341 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7343 // allow old (incorrect) defs to be used
7346 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7347 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7348 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7353 case wxTOP
: vert
= wxALIGN_TOP
; break;
7354 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7355 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7358 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7360 m_rowLabelHorizAlign
= horiz
;
7363 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7365 m_rowLabelVertAlign
= vert
;
7368 if ( !GetBatchCount() )
7370 m_rowLabelWin
->Refresh();
7374 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7376 // allow old (incorrect) defs to be used
7379 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7380 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7381 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7386 case wxTOP
: vert
= wxALIGN_TOP
; break;
7387 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7388 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7391 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7393 m_colLabelHorizAlign
= horiz
;
7396 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7398 m_colLabelVertAlign
= vert
;
7401 if ( !GetBatchCount() )
7403 m_colLabelWin
->Refresh();
7407 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7411 m_table
->SetRowLabelValue( row
, s
);
7412 if ( !GetBatchCount() )
7414 wxRect rect
= CellToRect( row
, 0);
7415 if ( rect
.height
> 0 )
7417 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7419 rect
.width
= m_rowLabelWidth
;
7420 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7426 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7430 m_table
->SetColLabelValue( col
, s
);
7431 if ( !GetBatchCount() )
7433 wxRect rect
= CellToRect( 0, col
);
7434 if ( rect
.width
> 0 )
7436 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7438 rect
.height
= m_colLabelHeight
;
7439 m_colLabelWin
->Refresh( TRUE
, &rect
);
7445 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7447 if ( m_gridLineColour
!= colour
)
7449 m_gridLineColour
= colour
;
7451 wxClientDC
dc( m_gridWin
);
7453 DrawAllGridLines( dc
, wxRegion() );
7458 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7460 if ( m_cellHighlightColour
!= colour
)
7462 m_cellHighlightColour
= colour
;
7464 wxClientDC
dc( m_gridWin
);
7466 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7467 DrawCellHighlight(dc
, attr
);
7472 void wxGrid::EnableGridLines( bool enable
)
7474 if ( enable
!= m_gridLinesEnabled
)
7476 m_gridLinesEnabled
= enable
;
7478 if ( !GetBatchCount() )
7482 wxClientDC
dc( m_gridWin
);
7484 DrawAllGridLines( dc
, wxRegion() );
7488 m_gridWin
->Refresh();
7495 int wxGrid::GetDefaultRowSize()
7497 return m_defaultRowHeight
;
7500 int wxGrid::GetRowSize( int row
)
7502 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7504 return GetRowHeight(row
);
7507 int wxGrid::GetDefaultColSize()
7509 return m_defaultColWidth
;
7512 int wxGrid::GetColSize( int col
)
7514 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7516 return GetColWidth(col
);
7519 // ============================================================================
7520 // access to the grid attributes: each of them has a default value in the grid
7521 // itself and may be overidden on a per-cell basis
7522 // ============================================================================
7524 // ----------------------------------------------------------------------------
7525 // setting default attributes
7526 // ----------------------------------------------------------------------------
7528 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7530 m_defaultCellAttr
->SetBackgroundColour(col
);
7532 m_gridWin
->SetBackgroundColour(col
);
7536 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7538 m_defaultCellAttr
->SetTextColour(col
);
7541 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7543 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7546 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7548 m_defaultCellAttr
->SetFont(font
);
7551 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7553 m_defaultCellAttr
->SetRenderer(renderer
);
7556 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7558 m_defaultCellAttr
->SetEditor(editor
);
7561 // ----------------------------------------------------------------------------
7562 // access to the default attrbiutes
7563 // ----------------------------------------------------------------------------
7565 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7567 return m_defaultCellAttr
->GetBackgroundColour();
7570 wxColour
wxGrid::GetDefaultCellTextColour()
7572 return m_defaultCellAttr
->GetTextColour();
7575 wxFont
wxGrid::GetDefaultCellFont()
7577 return m_defaultCellAttr
->GetFont();
7580 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7582 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7585 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7587 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7590 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7592 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7595 // ----------------------------------------------------------------------------
7596 // access to cell attributes
7597 // ----------------------------------------------------------------------------
7599 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7601 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7602 wxColour colour
= attr
->GetBackgroundColour();
7607 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7609 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7610 wxColour colour
= attr
->GetTextColour();
7615 wxFont
wxGrid::GetCellFont( int row
, int col
)
7617 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7618 wxFont font
= attr
->GetFont();
7623 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7625 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7626 attr
->GetAlignment(horiz
, vert
);
7630 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7632 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7633 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7639 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7641 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7642 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7648 bool wxGrid::IsReadOnly(int row
, int col
) const
7650 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7651 bool isReadOnly
= attr
->IsReadOnly();
7656 // ----------------------------------------------------------------------------
7657 // attribute support: cache, automatic provider creation, ...
7658 // ----------------------------------------------------------------------------
7660 bool wxGrid::CanHaveAttributes()
7667 return m_table
->CanHaveAttributes();
7670 void wxGrid::ClearAttrCache()
7672 if ( m_attrCache
.row
!= -1 )
7674 wxSafeDecRef(m_attrCache
.attr
);
7675 m_attrCache
.row
= -1;
7679 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7681 wxGrid
*self
= (wxGrid
*)this; // const_cast
7683 self
->ClearAttrCache();
7684 self
->m_attrCache
.row
= row
;
7685 self
->m_attrCache
.col
= col
;
7686 self
->m_attrCache
.attr
= attr
;
7690 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7692 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7694 *attr
= m_attrCache
.attr
;
7695 wxSafeIncRef(m_attrCache
.attr
);
7697 #ifdef DEBUG_ATTR_CACHE
7698 gs_nAttrCacheHits
++;
7705 #ifdef DEBUG_ATTR_CACHE
7706 gs_nAttrCacheMisses
++;
7712 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7714 wxGridCellAttr
*attr
;
7715 if ( !LookupAttr(row
, col
, &attr
) )
7717 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7718 CacheAttr(row
, col
, attr
);
7722 attr
->SetDefAttr(m_defaultCellAttr
);
7726 attr
= m_defaultCellAttr
;
7733 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7735 wxGridCellAttr
*attr
;
7736 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7738 wxASSERT_MSG( m_table
,
7739 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7741 attr
= m_table
->GetAttr(row
, col
);
7744 attr
= new wxGridCellAttr
;
7746 // artificially inc the ref count to match DecRef() in caller
7749 m_table
->SetAttr(attr
, row
, col
);
7752 CacheAttr(row
, col
, attr
);
7754 attr
->SetDefAttr(m_defaultCellAttr
);
7758 // ----------------------------------------------------------------------------
7759 // setting column attributes (wrappers around SetColAttr)
7760 // ----------------------------------------------------------------------------
7762 void wxGrid::SetColFormatBool(int col
)
7764 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7767 void wxGrid::SetColFormatNumber(int col
)
7769 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7772 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7774 wxString typeName
= wxGRID_VALUE_FLOAT
;
7775 if ( (width
!= -1) || (precision
!= -1) )
7777 typeName
<< _T(':') << width
<< _T(',') << precision
;
7780 SetColFormatCustom(col
, typeName
);
7783 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7785 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7786 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7787 attr
->SetRenderer(renderer
);
7789 SetColAttr(col
, attr
);
7792 // ----------------------------------------------------------------------------
7793 // setting cell attributes: this is forwarded to the table
7794 // ----------------------------------------------------------------------------
7796 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7798 if ( CanHaveAttributes() )
7800 m_table
->SetRowAttr(attr
, row
);
7808 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7810 if ( CanHaveAttributes() )
7812 m_table
->SetColAttr(attr
, col
);
7820 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7822 if ( CanHaveAttributes() )
7824 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7825 attr
->SetBackgroundColour(colour
);
7830 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7832 if ( CanHaveAttributes() )
7834 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7835 attr
->SetTextColour(colour
);
7840 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7842 if ( CanHaveAttributes() )
7844 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7845 attr
->SetFont(font
);
7850 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7852 if ( CanHaveAttributes() )
7854 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7855 attr
->SetAlignment(horiz
, vert
);
7860 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7862 if ( CanHaveAttributes() )
7864 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7865 attr
->SetRenderer(renderer
);
7870 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7872 if ( CanHaveAttributes() )
7874 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7875 attr
->SetEditor(editor
);
7880 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7882 if ( CanHaveAttributes() )
7884 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7885 attr
->SetReadOnly(isReadOnly
);
7890 // ----------------------------------------------------------------------------
7891 // Data type registration
7892 // ----------------------------------------------------------------------------
7894 void wxGrid::RegisterDataType(const wxString
& typeName
,
7895 wxGridCellRenderer
* renderer
,
7896 wxGridCellEditor
* editor
)
7898 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7902 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7904 wxString typeName
= m_table
->GetTypeName(row
, col
);
7905 return GetDefaultEditorForType(typeName
);
7908 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7910 wxString typeName
= m_table
->GetTypeName(row
, col
);
7911 return GetDefaultRendererForType(typeName
);
7915 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7917 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7918 if ( index
== wxNOT_FOUND
)
7920 wxFAIL_MSG(wxT("Unknown data type name"));
7925 return m_typeRegistry
->GetEditor(index
);
7929 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7931 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7932 if ( index
== wxNOT_FOUND
)
7934 wxFAIL_MSG(wxT("Unknown data type name"));
7939 return m_typeRegistry
->GetRenderer(index
);
7943 // ----------------------------------------------------------------------------
7945 // ----------------------------------------------------------------------------
7947 void wxGrid::EnableDragRowSize( bool enable
)
7949 m_canDragRowSize
= enable
;
7953 void wxGrid::EnableDragColSize( bool enable
)
7955 m_canDragColSize
= enable
;
7958 void wxGrid::EnableDragGridSize( bool enable
)
7960 m_canDragGridSize
= enable
;
7964 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7966 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7968 if ( resizeExistingRows
)
7976 void wxGrid::SetRowSize( int row
, int height
)
7978 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7980 if ( m_rowHeights
.IsEmpty() )
7982 // need to really create the array
7986 int h
= wxMax( 0, height
);
7987 int diff
= h
- m_rowHeights
[row
];
7989 m_rowHeights
[row
] = h
;
7991 for ( i
= row
; i
< m_numRows
; i
++ )
7993 m_rowBottoms
[i
] += diff
;
7998 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8000 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8002 if ( resizeExistingCols
)
8010 void wxGrid::SetColSize( int col
, int width
)
8012 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8014 // should we check that it's bigger than GetColMinimalWidth(col) here?
8016 if ( m_colWidths
.IsEmpty() )
8018 // need to really create the array
8022 int w
= wxMax( 0, width
);
8023 int diff
= w
- m_colWidths
[col
];
8024 m_colWidths
[col
] = w
;
8027 for ( i
= col
; i
< m_numCols
; i
++ )
8029 m_colRights
[i
] += diff
;
8035 void wxGrid::SetColMinimalWidth( int col
, int width
)
8037 m_colMinWidths
.Put(col
, width
);
8040 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8042 m_rowMinHeights
.Put(row
, width
);
8045 int wxGrid::GetColMinimalWidth(int col
) const
8047 long value
= m_colMinWidths
.Get(col
);
8048 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8051 int wxGrid::GetRowMinimalHeight(int row
) const
8053 long value
= m_rowMinHeights
.Get(row
);
8054 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8057 // ----------------------------------------------------------------------------
8059 // ----------------------------------------------------------------------------
8061 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8063 wxClientDC
dc(m_gridWin
);
8065 // init both of them to avoid compiler warnings, even if weo nly need one
8073 wxCoord extent
, extentMax
= 0;
8074 int max
= column
? m_numRows
: m_numCols
;
8075 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8082 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8083 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8086 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8087 extent
= column
? size
.x
: size
.y
;
8088 if ( extent
> extentMax
)
8099 // now also compare with the column label extent
8101 dc
.SetFont( GetLabelFont() );
8104 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8106 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
8108 extent
= column
? w
: h
;
8109 if ( extent
> extentMax
)
8116 // empty column - give default extent (notice that if extentMax is less
8117 // than default extent but != 0, it's ok)
8118 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8124 // leave some space around text
8134 SetColSize(col
, extentMax
);
8136 SetRowSize(row
, extentMax
);
8141 SetColMinimalWidth(col
, extentMax
);
8143 SetRowMinimalHeight(row
, extentMax
);
8147 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8149 int width
= m_rowLabelWidth
;
8151 for ( int col
= 0; col
< m_numCols
; col
++ )
8155 AutoSizeColumn(col
, setAsMin
);
8158 width
+= GetColWidth(col
);
8164 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8166 int height
= m_colLabelHeight
;
8168 for ( int row
= 0; row
< m_numRows
; row
++ )
8172 AutoSizeRow(row
, setAsMin
);
8175 height
+= GetRowHeight(row
);
8181 void wxGrid::AutoSize()
8184 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8187 wxSize
wxGrid::DoGetBestSize() const
8189 // don't set sizes, only calculate them
8190 wxGrid
*self
= (wxGrid
*)this; // const_cast
8192 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8193 self
->SetOrCalcRowSizes(TRUE
));
8202 wxPen
& wxGrid::GetDividerPen() const
8207 // ----------------------------------------------------------------------------
8208 // cell value accessor functions
8209 // ----------------------------------------------------------------------------
8211 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8215 m_table
->SetValue( row
, col
, s
);
8216 if ( !GetBatchCount() )
8218 wxClientDC
dc( m_gridWin
);
8220 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8223 if ( m_currentCellCoords
.GetRow() == row
&&
8224 m_currentCellCoords
.GetCol() == col
&&
8225 IsCellEditControlShown())
8226 // Note: If we are using IsCellEditControlEnabled,
8227 // this interacts badly with calling SetCellValue from
8228 // an EVT_GRID_CELL_CHANGE handler.
8230 HideCellEditControl();
8231 ShowCellEditControl(); // will reread data from table
8238 // ------ Block, row and col selection
8241 void wxGrid::SelectRow( int row
, bool addToSelected
)
8243 if ( IsSelection() && !addToSelected
)
8246 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8250 void wxGrid::SelectCol( int col
, bool addToSelected
)
8252 if ( IsSelection() && !addToSelected
)
8255 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8259 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8260 bool addToSelected
)
8262 if ( IsSelection() && !addToSelected
)
8265 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8266 FALSE
, addToSelected
);
8270 void wxGrid::SelectAll()
8272 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8275 bool wxGrid::IsSelection()
8277 return ( m_selection
->IsSelection() ||
8278 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8279 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8282 bool wxGrid::IsInSelection( int row
, int col
)
8284 return ( m_selection
->IsInSelection( row
, col
) ||
8285 ( row
>= m_selectingTopLeft
.GetRow() &&
8286 col
>= m_selectingTopLeft
.GetCol() &&
8287 row
<= m_selectingBottomRight
.GetRow() &&
8288 col
<= m_selectingBottomRight
.GetCol() ) );
8291 void wxGrid::ClearSelection()
8293 m_selectingTopLeft
= wxGridNoCellCoords
;
8294 m_selectingBottomRight
= wxGridNoCellCoords
;
8295 m_selection
->ClearSelection();
8299 // This function returns the rectangle that encloses the given block
8300 // in device coords clipped to the client size of the grid window.
8302 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8303 const wxGridCellCoords
&bottomRight
)
8305 wxRect
rect( wxGridNoCellRect
);
8308 cellRect
= CellToRect( topLeft
);
8309 if ( cellRect
!= wxGridNoCellRect
)
8315 rect
= wxRect( 0, 0, 0, 0 );
8318 cellRect
= CellToRect( bottomRight
);
8319 if ( cellRect
!= wxGridNoCellRect
)
8325 return wxGridNoCellRect
;
8328 // convert to scrolled coords
8330 int left
, top
, right
, bottom
;
8331 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8332 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8335 m_gridWin
->GetClientSize( &cw
, &ch
);
8337 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8338 return wxRect( 0, 0, 0, 0);
8340 rect
.SetLeft( wxMax(0, left
) );
8341 rect
.SetTop( wxMax(0, top
) );
8342 rect
.SetRight( wxMin(cw
, right
) );
8343 rect
.SetBottom( wxMin(ch
, bottom
) );
8351 // ------ Grid event classes
8354 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8356 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8357 int row
, int col
, int x
, int y
, bool sel
,
8358 bool control
, bool shift
, bool alt
, bool meta
)
8359 : wxNotifyEvent( type
, id
)
8366 m_control
= control
;
8371 SetEventObject(obj
);
8375 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8377 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8378 int rowOrCol
, int x
, int y
,
8379 bool control
, bool shift
, bool alt
, bool meta
)
8380 : wxNotifyEvent( type
, id
)
8382 m_rowOrCol
= rowOrCol
;
8385 m_control
= control
;
8390 SetEventObject(obj
);
8394 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8396 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8397 const wxGridCellCoords
& topLeft
,
8398 const wxGridCellCoords
& bottomRight
,
8399 bool sel
, bool control
,
8400 bool shift
, bool alt
, bool meta
)
8401 : wxNotifyEvent( type
, id
)
8403 m_topLeft
= topLeft
;
8404 m_bottomRight
= bottomRight
;
8406 m_control
= control
;
8411 SetEventObject(obj
);
8415 #endif // ifndef wxUSE_NEW_GRID