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 precompilation, 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"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 WX_DEFINE_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
60 struct wxGridCellWithAttr
62 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
63 : coords(row
, col
), attr(attr_
)
72 wxGridCellCoords coords
;
76 WX_DECLARE_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
78 #include "wx/arrimpl.cpp"
80 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
81 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
90 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
91 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
92 const wxPoint
&pos
, const wxSize
&size
);
97 void OnPaint( wxPaintEvent
& event
);
98 void OnMouseEvent( wxMouseEvent
& event
);
99 void OnKeyDown( wxKeyEvent
& event
);
101 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
102 DECLARE_EVENT_TABLE()
106 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
109 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
110 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
111 const wxPoint
&pos
, const wxSize
&size
);
116 void OnPaint( wxPaintEvent
&event
);
117 void OnMouseEvent( wxMouseEvent
& event
);
118 void OnKeyDown( wxKeyEvent
& event
);
120 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
121 DECLARE_EVENT_TABLE()
125 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
128 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
129 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
130 const wxPoint
&pos
, const wxSize
&size
);
135 void OnMouseEvent( wxMouseEvent
& event
);
136 void OnKeyDown( wxKeyEvent
& event
);
137 void OnPaint( wxPaintEvent
& event
);
139 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
140 DECLARE_EVENT_TABLE()
143 class WXDLLEXPORT wxGridWindow
: public wxPanel
148 m_owner
= (wxGrid
*)NULL
;
149 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
150 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
153 wxGridWindow( wxGrid
*parent
,
154 wxGridRowLabelWindow
*rowLblWin
,
155 wxGridColLabelWindow
*colLblWin
,
156 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
159 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
163 wxGridRowLabelWindow
*m_rowLabelWin
;
164 wxGridColLabelWindow
*m_colLabelWin
;
166 void OnPaint( wxPaintEvent
&event
);
167 void OnMouseEvent( wxMouseEvent
& event
);
168 void OnKeyDown( wxKeyEvent
& );
169 void OnEraseBackground( wxEraseEvent
& );
172 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
173 DECLARE_EVENT_TABLE()
178 class wxGridCellEditorEvtHandler
: public wxEvtHandler
181 wxGridCellEditorEvtHandler()
182 : m_grid(0), m_editor(0)
184 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
185 : m_grid(grid
), m_editor(editor
)
188 void OnKeyDown(wxKeyEvent
& event
);
189 void OnChar(wxKeyEvent
& event
);
193 wxGridCellEditor
* m_editor
;
194 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
195 DECLARE_EVENT_TABLE()
199 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
200 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
201 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
202 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
207 // ----------------------------------------------------------------------------
208 // the internal data representation used by wxGridCellAttrProvider
209 // ----------------------------------------------------------------------------
211 // this class stores attributes set for cells
212 class WXDLLEXPORT wxGridCellAttrData
215 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
216 wxGridCellAttr
*GetAttr(int row
, int col
) const;
217 void UpdateAttrRows( size_t pos
, int numRows
);
218 void UpdateAttrCols( size_t pos
, int numCols
);
221 // searches for the attr for given cell, returns wxNOT_FOUND if not found
222 int FindIndex(int row
, int col
) const;
224 wxGridCellWithAttrArray m_attrs
;
227 // this class stores attributes set for rows or columns
228 class WXDLLEXPORT wxGridRowOrColAttrData
231 // empty ctor to suppress warnings
232 wxGridRowOrColAttrData() { }
233 ~wxGridRowOrColAttrData();
235 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
236 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
237 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
240 wxArrayInt m_rowsOrCols
;
241 wxArrayAttrs m_attrs
;
244 // NB: this is just a wrapper around 3 objects: one which stores cell
245 // attributes, and 2 others for row/col ones
246 class WXDLLEXPORT wxGridCellAttrProviderData
249 wxGridCellAttrData m_cellAttrs
;
250 wxGridRowOrColAttrData m_rowAttrs
,
255 // ----------------------------------------------------------------------------
256 // data structures used for the data type registry
257 // ----------------------------------------------------------------------------
259 struct wxGridDataTypeInfo
261 wxGridDataTypeInfo(const wxString
& typeName
,
262 wxGridCellRenderer
* renderer
,
263 wxGridCellEditor
* editor
)
264 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
267 ~wxGridDataTypeInfo()
269 wxSafeDecRef(m_renderer
);
270 wxSafeDecRef(m_editor
);
274 wxGridCellRenderer
* m_renderer
;
275 wxGridCellEditor
* m_editor
;
279 WX_DEFINE_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
282 class WXDLLEXPORT wxGridTypeRegistry
285 ~wxGridTypeRegistry();
287 void RegisterDataType(const wxString
& typeName
,
288 wxGridCellRenderer
* renderer
,
289 wxGridCellEditor
* editor
);
291 // find one of already registered data types
292 int FindRegisteredDataType(const wxString
& typeName
);
294 // try to FindRegisteredDataType(), if this fails and typeName is one of
295 // standard typenames, register it and return its index
296 int FindDataType(const wxString
& typeName
);
298 // try to FindDataType(), if it fails see if it is not one of already
299 // registered data types with some params in which case clone the
300 // registered data type and set params for it
301 int FindOrCloneDataType(const wxString
& typeName
);
303 wxGridCellRenderer
* GetRenderer(int index
);
304 wxGridCellEditor
* GetEditor(int index
);
307 wxGridDataTypeInfoArray m_typeinfo
;
310 // ----------------------------------------------------------------------------
311 // conditional compilation
312 // ----------------------------------------------------------------------------
314 #ifndef WXGRID_DRAW_LINES
315 #define WXGRID_DRAW_LINES 1
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 //#define DEBUG_ATTR_CACHE
323 #ifdef DEBUG_ATTR_CACHE
324 static size_t gs_nAttrCacheHits
= 0;
325 static size_t gs_nAttrCacheMisses
= 0;
326 #endif // DEBUG_ATTR_CACHE
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
333 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
336 // TODO: fixed so far - make configurable later (and also different for x/y)
337 static const size_t GRID_SCROLL_LINE
= 10;
339 // the size of hash tables used a bit everywhere (the max number of elements
340 // in these hash tables is the number of rows/columns)
341 static const int GRID_HASH_SIZE
= 100;
343 // ============================================================================
345 // ============================================================================
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 wxGridCellEditor::wxGridCellEditor()
357 wxGridCellEditor::~wxGridCellEditor()
362 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
363 wxWindowID
WXUNUSED(id
),
364 wxEvtHandler
* evtHandler
)
367 m_control
->PushEventHandler(evtHandler
);
370 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
371 wxGridCellAttr
*attr
)
373 // erase the background because we might not fill the cell
374 wxClientDC
dc(m_control
->GetParent());
375 dc
.SetPen(*wxTRANSPARENT_PEN
);
376 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
377 dc
.DrawRectangle(rectCell
);
379 // redraw the control we just painted over
380 m_control
->Refresh();
383 void wxGridCellEditor::Destroy()
387 m_control
->PopEventHandler(TRUE
/* delete it*/);
389 m_control
->Destroy();
394 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
396 wxASSERT_MSG(m_control
,
397 wxT("The wxGridCellEditor must be Created first!"));
398 m_control
->Show(show
);
402 // set the colours/fonts if we have any
405 m_colFgOld
= m_control
->GetForegroundColour();
406 m_control
->SetForegroundColour(attr
->GetTextColour());
408 m_colBgOld
= m_control
->GetBackgroundColour();
409 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
411 m_fontOld
= m_control
->GetFont();
412 m_control
->SetFont(attr
->GetFont());
414 // can't do anything more in the base class version, the other
415 // attributes may only be used by the derived classes
420 // restore the standard colours fonts
421 if ( m_colFgOld
.Ok() )
423 m_control
->SetForegroundColour(m_colFgOld
);
424 m_colFgOld
= wxNullColour
;
427 if ( m_colBgOld
.Ok() )
429 m_control
->SetBackgroundColour(m_colBgOld
);
430 m_colBgOld
= wxNullColour
;
433 if ( m_fontOld
.Ok() )
435 m_control
->SetFont(m_fontOld
);
436 m_fontOld
= wxNullFont
;
441 void wxGridCellEditor::SetSize(const wxRect
& rect
)
443 wxASSERT_MSG(m_control
,
444 wxT("The wxGridCellEditor must be Created first!"));
445 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
448 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
454 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
459 void wxGridCellEditor::StartingClick()
463 // ----------------------------------------------------------------------------
464 // wxGridCellTextEditor
465 // ----------------------------------------------------------------------------
467 wxGridCellTextEditor::wxGridCellTextEditor()
472 void wxGridCellTextEditor::Create(wxWindow
* parent
,
474 wxEvtHandler
* evtHandler
)
476 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
477 wxDefaultPosition
, wxDefaultSize
478 #if defined(__WXMSW__)
479 , wxTE_MULTILINE
| wxTE_NO_VSCROLL
// necessary ???
483 // TODO: use m_maxChars
485 wxGridCellEditor::Create(parent
, id
, evtHandler
);
488 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
489 wxGridCellAttr
* WXUNUSED(attr
))
491 // as we fill the entire client area, don't do anything here to minimize
495 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
497 wxRect
rect(rectOrig
);
499 // Make the edit control large enough to allow for internal
502 // TODO: remove this if the text ctrl sizing is improved esp. for
505 #if defined(__WXGTK__)
514 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
515 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
516 #if defined(__WXMOTIF__)
520 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
521 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
522 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
523 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
526 wxGridCellEditor::SetSize(rect
);
529 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
531 wxASSERT_MSG(m_control
,
532 wxT("The wxGridCellEditor must be Created first!"));
534 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
536 DoBeginEdit(m_startValue
);
539 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
541 Text()->SetValue(startValue
);
542 Text()->SetInsertionPointEnd();
546 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
549 wxASSERT_MSG(m_control
,
550 wxT("The wxGridCellEditor must be Created first!"));
552 bool changed
= FALSE
;
553 wxString value
= Text()->GetValue();
554 if (value
!= m_startValue
)
558 grid
->GetTable()->SetValue(row
, col
, value
);
560 m_startValue
= wxEmptyString
;
561 Text()->SetValue(m_startValue
);
567 void wxGridCellTextEditor::Reset()
569 wxASSERT_MSG(m_control
,
570 wxT("The wxGridCellEditor must be Created first!"));
572 DoReset(m_startValue
);
575 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
577 Text()->SetValue(startValue
);
578 Text()->SetInsertionPointEnd();
581 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
583 if ( !event
.AltDown() && !event
.MetaDown() && !event
.ControlDown() )
585 // insert the key in the control
586 long keycode
= event
.KeyCode();
587 if ( isprint(keycode
) )
589 // FIXME this is not going to work for non letters...
590 if ( !event
.ShiftDown() )
592 keycode
= tolower(keycode
);
595 Text()->AppendText((wxChar
)keycode
);
605 void wxGridCellTextEditor::HandleReturn(wxKeyEvent
& event
)
607 #if defined(__WXMOTIF__) || defined(__WXGTK__)
608 // wxMotif needs a little extra help...
609 int pos
= Text()->GetInsertionPoint();
610 wxString
s( Text()->GetValue() );
611 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
613 Text()->SetInsertionPoint( pos
);
615 // the other ports can handle a Return key press
621 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
631 if ( !params
.ToLong(&tmp
) )
633 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string "
634 "'%s' ignored"), params
.c_str());
638 m_maxChars
= (size_t)tmp
;
643 // ----------------------------------------------------------------------------
644 // wxGridCellNumberEditor
645 // ----------------------------------------------------------------------------
647 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
653 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
655 wxEvtHandler
* evtHandler
)
659 // create a spin ctrl
660 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
661 wxDefaultPosition
, wxDefaultSize
,
665 wxGridCellEditor::Create(parent
, id
, evtHandler
);
669 // just a text control
670 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
673 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
674 #endif // wxUSE_VALIDATORS
678 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
680 // first get the value
681 wxGridTableBase
*table
= grid
->GetTable();
682 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
684 m_valueOld
= table
->GetValueAsLong(row
, col
);
688 wxString sValue
= table
->GetValue(row
, col
);
689 if (! sValue
.ToLong(&m_valueOld
))
691 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
698 Spin()->SetValue(m_valueOld
);
702 DoBeginEdit(GetString());
706 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
714 value
= Spin()->GetValue();
715 changed
= value
!= m_valueOld
;
719 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
724 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
725 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
727 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%ld", value
));
733 void wxGridCellNumberEditor::Reset()
737 Spin()->SetValue(m_valueOld
);
741 DoReset(GetString());
745 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
749 long keycode
= event
.KeyCode();
750 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
752 wxGridCellTextEditor::StartingKey(event
);
762 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
773 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
777 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
781 // skip the error message below
786 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string "
787 "'%s' ignored"), params
.c_str());
791 // ----------------------------------------------------------------------------
792 // wxGridCellFloatEditor
793 // ----------------------------------------------------------------------------
795 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
797 wxEvtHandler
* evtHandler
)
799 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
802 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
803 #endif // wxUSE_VALIDATORS
806 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
808 // first get the value
809 wxGridTableBase
*table
= grid
->GetTable();
810 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
812 m_valueOld
= table
->GetValueAsDouble(row
, col
);
816 wxString sValue
= table
->GetValue(row
, col
);
817 if (! sValue
.ToDouble(&m_valueOld
))
819 wxFAIL_MSG( _T("this cell doesn't have float value") );
824 DoBeginEdit(GetString());
827 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
831 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
833 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
834 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
836 grid
->GetTable()->SetValue(row
, col
, wxString::Format("%f", value
));
846 void wxGridCellFloatEditor::Reset()
848 DoReset(GetString());
851 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
853 long keycode
= event
.KeyCode();
854 if ( isdigit(keycode
) ||
855 keycode
== '+' || keycode
== '-' || keycode
== '.' )
857 wxGridCellTextEditor::StartingKey(event
);
866 // ----------------------------------------------------------------------------
867 // wxGridCellBoolEditor
868 // ----------------------------------------------------------------------------
870 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
872 wxEvtHandler
* evtHandler
)
874 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
875 wxDefaultPosition
, wxDefaultSize
,
878 wxGridCellEditor::Create(parent
, id
, evtHandler
);
881 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
884 wxSize size
= m_control
->GetSize();
885 wxCoord minSize
= wxMin(r
.width
, r
.height
);
887 // check if the checkbox is not too big/small for this cell
888 wxSize sizeBest
= m_control
->GetBestSize();
889 if ( !(size
== sizeBest
) )
891 // reset to default size if it had been made smaller
897 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
899 // leave 1 pixel margin
900 size
.x
= size
.y
= minSize
- 2;
907 m_control
->SetSize(size
);
910 // position it in the centre of the rectangle (TODO: support alignment?)
912 #if defined(__WXGTK__) || defined (__WXMOTIF__)
913 // the checkbox without label still has some space to the right in wxGTK,
914 // so shift it to the right
916 #elif defined(__WXMSW__)
917 // here too, but in other way
922 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
925 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
927 m_control
->Show(show
);
931 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
932 CBox()->SetBackgroundColour(colBg
);
936 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
938 wxASSERT_MSG(m_control
,
939 wxT("The wxGridCellEditor must be Created first!"));
941 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
942 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
944 m_startValue
= !!grid
->GetTable()->GetValue(row
, col
);
945 CBox()->SetValue(m_startValue
);
949 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
952 wxASSERT_MSG(m_control
,
953 wxT("The wxGridCellEditor must be Created first!"));
955 bool changed
= FALSE
;
956 bool value
= CBox()->GetValue();
957 if ( value
!= m_startValue
)
962 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
963 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
965 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
971 void wxGridCellBoolEditor::Reset()
973 wxASSERT_MSG(m_control
,
974 wxT("The wxGridCellEditor must be Created first!"));
976 CBox()->SetValue(m_startValue
);
979 void wxGridCellBoolEditor::StartingClick()
981 CBox()->SetValue(!CBox()->GetValue());
984 // ----------------------------------------------------------------------------
985 // wxGridCellChoiceEditor
986 // ----------------------------------------------------------------------------
988 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
989 const wxChar
* choices
[],
991 : m_allowOthers(allowOthers
)
995 m_choices
.Alloc(count
);
996 for ( size_t n
= 0; n
< count
; n
++ )
998 m_choices
.Add(choices
[n
]);
1003 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1005 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1006 editor
->m_allowOthers
= m_allowOthers
;
1007 editor
->m_choices
= m_choices
;
1012 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1014 wxEvtHandler
* evtHandler
)
1016 size_t count
= m_choices
.GetCount();
1017 wxString
*choices
= new wxString
[count
];
1018 for ( size_t n
= 0; n
< count
; n
++ )
1020 choices
[n
] = m_choices
[n
];
1023 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1024 wxDefaultPosition
, wxDefaultSize
,
1026 m_allowOthers
? 0 : wxCB_READONLY
);
1030 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1033 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1034 wxGridCellAttr
* attr
)
1036 // as we fill the entire client area, don't do anything here to minimize
1039 // TODO: It doesn't actually fill the client area since the height of a
1040 // combo always defaults to the standard... Until someone has time to
1041 // figure out the right rectangle to paint, just do it the normal way...
1042 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1045 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1047 wxASSERT_MSG(m_control
,
1048 wxT("The wxGridCellEditor must be Created first!"));
1050 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1052 Combo()->SetValue(m_startValue
);
1053 size_t count
= m_choices
.GetCount();
1054 for (size_t i
=0; i
<count
; i
++)
1056 if (m_startValue
== m_choices
[i
])
1058 Combo()->SetSelection(i
);
1062 Combo()->SetInsertionPointEnd();
1063 Combo()->SetFocus();
1066 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1069 wxString value
= Combo()->GetValue();
1070 bool changed
= value
!= m_startValue
;
1073 grid
->GetTable()->SetValue(row
, col
, value
);
1075 m_startValue
= wxEmptyString
;
1076 Combo()->SetValue(m_startValue
);
1081 void wxGridCellChoiceEditor::Reset()
1083 Combo()->SetValue(m_startValue
);
1084 Combo()->SetInsertionPointEnd();
1087 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1097 wxStringTokenizer
tk(params
, _T(','));
1098 while ( tk
.HasMoreTokens() )
1100 m_choices
.Add(tk
.GetNextToken());
1104 // ----------------------------------------------------------------------------
1105 // wxGridCellEditorEvtHandler
1106 // ----------------------------------------------------------------------------
1108 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1110 switch ( event
.KeyCode() )
1114 m_grid
->DisableCellEditControl();
1118 event
.Skip( m_grid
->ProcessEvent( event
) );
1122 if (!m_grid
->ProcessEvent(event
))
1123 m_editor
->HandleReturn(event
);
1132 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1134 switch ( event
.KeyCode() )
1146 // ----------------------------------------------------------------------------
1147 // wxGridCellWorker is an (almost) empty common base class for
1148 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1149 // ----------------------------------------------------------------------------
1151 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1156 wxGridCellWorker::~wxGridCellWorker()
1160 // ============================================================================
1162 // ============================================================================
1164 // ----------------------------------------------------------------------------
1165 // wxGridCellRenderer
1166 // ----------------------------------------------------------------------------
1168 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1169 wxGridCellAttr
& attr
,
1175 dc
.SetBackgroundMode( wxSOLID
);
1179 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1183 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1186 dc
.SetPen( *wxTRANSPARENT_PEN
);
1187 dc
.DrawRectangle(rect
);
1190 // ----------------------------------------------------------------------------
1191 // wxGridCellStringRenderer
1192 // ----------------------------------------------------------------------------
1194 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1195 wxGridCellAttr
& attr
,
1199 dc
.SetBackgroundMode( wxTRANSPARENT
);
1201 // TODO some special colours for attr.IsReadOnly() case?
1205 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1206 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1210 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1211 dc
.SetTextForeground( attr
.GetTextColour() );
1214 dc
.SetFont( attr
.GetFont() );
1217 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1219 const wxString
& text
)
1222 dc
.SetFont(attr
.GetFont());
1223 dc
.GetTextExtent(text
, &x
, &y
);
1225 return wxSize(x
, y
);
1228 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1229 wxGridCellAttr
& attr
,
1233 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1236 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1237 wxGridCellAttr
& attr
,
1239 const wxRect
& rectCell
,
1243 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1245 // now we only have to draw the text
1246 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1249 attr
.GetAlignment(&hAlign
, &vAlign
);
1251 wxRect rect
= rectCell
;
1254 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1255 rect
, hAlign
, vAlign
);
1258 // ----------------------------------------------------------------------------
1259 // wxGridCellNumberRenderer
1260 // ----------------------------------------------------------------------------
1262 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1264 wxGridTableBase
*table
= grid
.GetTable();
1266 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1268 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1272 text
= table
->GetValue(row
, col
);
1278 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1279 wxGridCellAttr
& attr
,
1281 const wxRect
& rectCell
,
1285 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1287 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1289 // draw the text right aligned by default
1291 attr
.GetAlignment(&hAlign
, &vAlign
);
1294 wxRect rect
= rectCell
;
1297 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1300 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1301 wxGridCellAttr
& attr
,
1305 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1308 // ----------------------------------------------------------------------------
1309 // wxGridCellFloatRenderer
1310 // ----------------------------------------------------------------------------
1312 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1315 SetPrecision(precision
);
1318 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1320 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1321 renderer
->m_width
= m_width
;
1322 renderer
->m_precision
= m_precision
;
1323 renderer
->m_format
= m_format
;
1328 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1330 wxGridTableBase
*table
= grid
.GetTable();
1335 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1337 val
= table
->GetValueAsDouble(row
, col
);
1342 text
= table
->GetValue(row
, col
);
1343 hasDouble
= text
.ToDouble(&val
);
1350 if ( m_width
== -1 )
1352 // default width/precision
1353 m_format
= _T("%f");
1355 else if ( m_precision
== -1 )
1357 // default precision
1358 m_format
.Printf(_T("%%%d.f"), m_width
);
1362 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1366 text
.Printf(m_format
, val
);
1368 //else: text already contains the string
1373 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1374 wxGridCellAttr
& attr
,
1376 const wxRect
& rectCell
,
1380 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1382 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1384 // draw the text right aligned by default
1386 attr
.GetAlignment(&hAlign
, &vAlign
);
1389 wxRect rect
= rectCell
;
1392 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1395 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1396 wxGridCellAttr
& attr
,
1400 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1403 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1409 // reset to defaults
1415 wxString tmp
= params
.BeforeFirst(_T(','));
1419 if ( !tmp
.ToLong(&width
) )
1425 SetWidth((int)width
);
1427 tmp
= params
.AfterFirst(_T(','));
1431 if ( !tmp
.ToLong(&precision
) )
1437 SetPrecision((int)precision
);
1445 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string "
1446 "'%s ignored"), params
.c_str());
1451 // ----------------------------------------------------------------------------
1452 // wxGridCellBoolRenderer
1453 // ----------------------------------------------------------------------------
1455 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1457 // FIXME these checkbox size calculations are really ugly...
1459 // between checkmark and box
1460 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1462 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1463 wxGridCellAttr
& WXUNUSED(attr
),
1468 // compute it only once (no locks for MT safeness in GUI thread...)
1469 if ( !ms_sizeCheckMark
.x
)
1471 // get checkbox size
1472 wxCoord checkSize
= 0;
1473 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1474 wxSize size
= checkbox
->GetBestSize();
1475 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1477 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1478 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1479 checkSize
-= size
.y
/ 2;
1484 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1487 return ms_sizeCheckMark
;
1490 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1491 wxGridCellAttr
& attr
,
1497 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1499 // draw a check mark in the centre (ignoring alignment - TODO)
1500 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1502 // don't draw outside the cell
1503 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1504 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1506 // and even leave (at least) 1 pixel margin
1507 size
.x
= size
.y
= minSize
- 2;
1510 // draw a border around checkmark
1512 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1513 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1514 rectBorder
.width
= size
.x
;
1515 rectBorder
.height
= size
.y
;
1518 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1519 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1521 value
= !!grid
.GetTable()->GetValue(row
, col
);
1525 wxRect rectMark
= rectBorder
;
1527 // MSW DrawCheckMark() is weird (and should probably be changed...)
1528 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1532 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1535 dc
.SetTextForeground(attr
.GetTextColour());
1536 dc
.DrawCheckMark(rectMark
);
1539 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1540 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1541 dc
.DrawRectangle(rectBorder
);
1544 // ----------------------------------------------------------------------------
1546 // ----------------------------------------------------------------------------
1548 wxGridCellAttr
*wxGridCellAttr::Clone() const
1550 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1551 if ( HasTextColour() )
1552 attr
->SetTextColour(GetTextColour());
1553 if ( HasBackgroundColour() )
1554 attr
->SetBackgroundColour(GetBackgroundColour());
1556 attr
->SetFont(GetFont());
1557 if ( HasAlignment() )
1558 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1562 attr
->SetRenderer(m_renderer
);
1563 m_renderer
->IncRef();
1567 attr
->SetEditor(m_editor
);
1572 attr
->SetReadOnly();
1574 attr
->SetDefAttr(m_defGridAttr
);
1579 const wxColour
& wxGridCellAttr::GetTextColour() const
1581 if (HasTextColour())
1585 else if (m_defGridAttr
!= this)
1587 return m_defGridAttr
->GetTextColour();
1591 wxFAIL_MSG(wxT("Missing default cell attribute"));
1592 return wxNullColour
;
1597 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1599 if (HasBackgroundColour())
1601 else if (m_defGridAttr
!= this)
1602 return m_defGridAttr
->GetBackgroundColour();
1605 wxFAIL_MSG(wxT("Missing default cell attribute"));
1606 return wxNullColour
;
1611 const wxFont
& wxGridCellAttr::GetFont() const
1615 else if (m_defGridAttr
!= this)
1616 return m_defGridAttr
->GetFont();
1619 wxFAIL_MSG(wxT("Missing default cell attribute"));
1625 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1629 if ( hAlign
) *hAlign
= m_hAlign
;
1630 if ( vAlign
) *vAlign
= m_vAlign
;
1632 else if (m_defGridAttr
!= this)
1633 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1636 wxFAIL_MSG(wxT("Missing default cell attribute"));
1641 // GetRenderer and GetEditor use a slightly different decision path about
1642 // which attribute to use. If a non-default attr object has one then it is
1643 // used, otherwise the default editor or renderer is fetched from the grid and
1644 // used. It should be the default for the data type of the cell. If it is
1645 // NULL (because the table has a type that the grid does not have in its
1646 // registry,) then the grid's default editor or renderer is used.
1648 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1650 wxGridCellRenderer
* renderer
= NULL
;
1652 if ( m_defGridAttr
!= this || grid
== NULL
)
1654 renderer
= m_renderer
; // use local attribute
1659 if ( !renderer
&& grid
) // get renderer for the data type
1661 // GetDefaultRendererForCell() will do IncRef() for us
1662 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1667 // if we still don't have one then use the grid default
1668 // (no need for IncRef() here neither)
1669 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1674 wxFAIL_MSG(wxT("Missing default cell attribute"));
1680 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1682 wxGridCellEditor
* editor
= NULL
;
1684 if ( m_defGridAttr
!= this || grid
== NULL
)
1686 editor
= m_editor
; // use local attribute
1691 if ( !editor
&& grid
) // get renderer for the data type
1692 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1695 // if we still don't have one then use the grid default
1696 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1700 wxFAIL_MSG(wxT("Missing default cell attribute"));
1706 // ----------------------------------------------------------------------------
1707 // wxGridCellAttrData
1708 // ----------------------------------------------------------------------------
1710 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1712 int n
= FindIndex(row
, col
);
1713 if ( n
== wxNOT_FOUND
)
1715 // add the attribute
1716 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1722 // change the attribute
1723 m_attrs
[(size_t)n
].attr
= attr
;
1727 // remove this attribute
1728 m_attrs
.RemoveAt((size_t)n
);
1733 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1735 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1737 int n
= FindIndex(row
, col
);
1738 if ( n
!= wxNOT_FOUND
)
1740 attr
= m_attrs
[(size_t)n
].attr
;
1747 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1749 size_t count
= m_attrs
.GetCount();
1750 for ( size_t n
= 0; n
< count
; n
++ )
1752 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1753 wxCoord row
= coords
.GetRow();
1754 if ((size_t)row
>= pos
)
1758 // If rows inserted, include row counter where necessary
1759 coords
.SetRow(row
+ numRows
);
1761 else if (numRows
< 0)
1763 // If rows deleted ...
1764 if ((size_t)row
>= pos
- numRows
)
1766 // ...either decrement row counter (if row still exists)...
1767 coords
.SetRow(row
+ numRows
);
1771 // ...or remove the attribute
1772 m_attrs
.RemoveAt((size_t)n
);
1780 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1782 size_t count
= m_attrs
.GetCount();
1783 for ( size_t n
= 0; n
< count
; n
++ )
1785 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1786 wxCoord col
= coords
.GetCol();
1787 if ( (size_t)col
>= pos
)
1791 // If rows inserted, include row counter where necessary
1792 coords
.SetCol(col
+ numCols
);
1794 else if (numCols
< 0)
1796 // If rows deleted ...
1797 if ((size_t)col
>= pos
- numCols
)
1799 // ...either decrement row counter (if row still exists)...
1800 coords
.SetCol(col
+ numCols
);
1804 // ...or remove the attribute
1805 m_attrs
.RemoveAt((size_t)n
);
1813 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1815 size_t count
= m_attrs
.GetCount();
1816 for ( size_t n
= 0; n
< count
; n
++ )
1818 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1819 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1828 // ----------------------------------------------------------------------------
1829 // wxGridRowOrColAttrData
1830 // ----------------------------------------------------------------------------
1832 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1834 size_t count
= m_attrs
.Count();
1835 for ( size_t n
= 0; n
< count
; n
++ )
1837 m_attrs
[n
]->DecRef();
1841 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1843 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1845 int n
= m_rowsOrCols
.Index(rowOrCol
);
1846 if ( n
!= wxNOT_FOUND
)
1848 attr
= m_attrs
[(size_t)n
];
1855 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1857 int i
= m_rowsOrCols
.Index(rowOrCol
);
1858 if ( i
== wxNOT_FOUND
)
1860 // add the attribute
1861 m_rowsOrCols
.Add(rowOrCol
);
1866 size_t n
= (size_t)i
;
1869 // change the attribute
1870 m_attrs
[n
]->DecRef();
1875 // remove this attribute
1876 m_attrs
[n
]->DecRef();
1877 m_rowsOrCols
.RemoveAt(n
);
1878 m_attrs
.RemoveAt(n
);
1883 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1885 size_t count
= m_attrs
.GetCount();
1886 for ( size_t n
= 0; n
< count
; n
++ )
1888 int & rowOrCol
= m_rowsOrCols
[n
];
1889 if ( (size_t)rowOrCol
>= pos
)
1891 if ( numRowsOrCols
> 0 )
1893 // If rows inserted, include row counter where necessary
1894 rowOrCol
+= numRowsOrCols
;
1896 else if ( numRowsOrCols
< 0)
1898 // If rows deleted, either decrement row counter (if row still exists)
1899 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1900 rowOrCol
+= numRowsOrCols
;
1903 m_rowsOrCols
.RemoveAt((size_t)n
);
1904 m_attrs
.RemoveAt((size_t)n
);
1912 // ----------------------------------------------------------------------------
1913 // wxGridCellAttrProvider
1914 // ----------------------------------------------------------------------------
1916 wxGridCellAttrProvider::wxGridCellAttrProvider()
1918 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1921 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1926 void wxGridCellAttrProvider::InitData()
1928 m_data
= new wxGridCellAttrProviderData
;
1931 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1933 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1936 // first look for the attribute of this specific cell
1937 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1941 // then look for the col attr (col attributes are more common than
1942 // the row ones, hence they have priority)
1943 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1948 // finally try the row attributes
1949 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1956 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1962 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1965 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1970 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1973 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1978 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1981 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1985 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1987 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1991 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1995 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1997 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2001 // ----------------------------------------------------------------------------
2002 // wxGridTypeRegistry
2003 // ----------------------------------------------------------------------------
2005 wxGridTypeRegistry::~wxGridTypeRegistry()
2007 size_t count
= m_typeinfo
.Count();
2008 for ( size_t i
= 0; i
< count
; i
++ )
2009 delete m_typeinfo
[i
];
2013 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2014 wxGridCellRenderer
* renderer
,
2015 wxGridCellEditor
* editor
)
2017 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2019 // is it already registered?
2020 int loc
= FindRegisteredDataType(typeName
);
2021 if ( loc
!= wxNOT_FOUND
)
2023 delete m_typeinfo
[loc
];
2024 m_typeinfo
[loc
] = info
;
2028 m_typeinfo
.Add(info
);
2032 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2034 size_t count
= m_typeinfo
.GetCount();
2035 for ( size_t i
= 0; i
< count
; i
++ )
2037 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2046 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2048 int index
= FindRegisteredDataType(typeName
);
2049 if ( index
== wxNOT_FOUND
)
2051 // check whether this is one of the standard ones, in which case
2052 // register it "on the fly"
2053 if ( typeName
== wxGRID_VALUE_STRING
)
2055 RegisterDataType(wxGRID_VALUE_STRING
,
2056 new wxGridCellStringRenderer
,
2057 new wxGridCellTextEditor
);
2059 else if ( typeName
== wxGRID_VALUE_BOOL
)
2061 RegisterDataType(wxGRID_VALUE_BOOL
,
2062 new wxGridCellBoolRenderer
,
2063 new wxGridCellBoolEditor
);
2065 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2067 RegisterDataType(wxGRID_VALUE_NUMBER
,
2068 new wxGridCellNumberRenderer
,
2069 new wxGridCellNumberEditor
);
2071 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2073 RegisterDataType(wxGRID_VALUE_FLOAT
,
2074 new wxGridCellFloatRenderer
,
2075 new wxGridCellFloatEditor
);
2077 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2079 RegisterDataType(wxGRID_VALUE_CHOICE
,
2080 new wxGridCellStringRenderer
,
2081 new wxGridCellChoiceEditor
);
2088 // we get here only if just added the entry for this type, so return
2090 index
= m_typeinfo
.GetCount() - 1;
2096 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2098 int index
= FindDataType(typeName
);
2099 if ( index
== wxNOT_FOUND
)
2101 // the first part of the typename is the "real" type, anything after ':'
2102 // are the parameters for the renderer
2103 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2104 if ( index
== wxNOT_FOUND
)
2109 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2110 wxGridCellRenderer
*rendererOld
= renderer
;
2111 renderer
= renderer
->Clone();
2112 rendererOld
->DecRef();
2114 wxGridCellEditor
*editor
= GetEditor(index
);
2115 wxGridCellEditor
*editorOld
= editor
;
2116 editor
= editor
->Clone();
2117 editorOld
->DecRef();
2119 // do it even if there are no parameters to reset them to defaults
2120 wxString params
= typeName
.AfterFirst(_T(':'));
2121 renderer
->SetParameters(params
);
2122 editor
->SetParameters(params
);
2124 // register the new typename
2125 RegisterDataType(typeName
, renderer
, editor
);
2127 // we just registered it, it's the last one
2128 index
= m_typeinfo
.GetCount() - 1;
2134 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2136 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2141 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2143 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2148 // ----------------------------------------------------------------------------
2150 // ----------------------------------------------------------------------------
2152 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2155 wxGridTableBase::wxGridTableBase()
2157 m_view
= (wxGrid
*) NULL
;
2158 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2161 wxGridTableBase::~wxGridTableBase()
2163 delete m_attrProvider
;
2166 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2168 delete m_attrProvider
;
2169 m_attrProvider
= attrProvider
;
2172 bool wxGridTableBase::CanHaveAttributes()
2174 if ( ! GetAttrProvider() )
2176 // use the default attr provider by default
2177 SetAttrProvider(new wxGridCellAttrProvider
);
2182 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2184 if ( m_attrProvider
)
2185 return m_attrProvider
->GetAttr(row
, col
);
2187 return (wxGridCellAttr
*)NULL
;
2190 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2192 if ( m_attrProvider
)
2194 m_attrProvider
->SetAttr(attr
, row
, col
);
2198 // as we take ownership of the pointer and don't store it, we must
2204 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2206 if ( m_attrProvider
)
2208 m_attrProvider
->SetRowAttr(attr
, row
);
2212 // as we take ownership of the pointer and don't store it, we must
2218 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2220 if ( m_attrProvider
)
2222 m_attrProvider
->SetColAttr(attr
, col
);
2226 // as we take ownership of the pointer and don't store it, we must
2232 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
2234 if ( m_attrProvider
)
2236 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
2240 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
2242 if ( m_attrProvider
)
2244 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
2248 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
2250 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2251 "but your derived table class does not override this function") );
2256 bool wxGridTableBase::AppendRows( size_t numRows
)
2258 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2259 "but your derived table class does not override this function"));
2264 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
2266 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2267 "but your derived table class does not override this function"));
2272 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
2274 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2275 "but your derived table class does not override this function"));
2280 bool wxGridTableBase::AppendCols( size_t numCols
)
2282 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2283 "but your derived table class does not override this function"));
2288 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
2290 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2291 "but your derived table class does not override this function"));
2297 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2300 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2301 // how much it makes sense to us geeks.
2305 wxString
wxGridTableBase::GetColLabelValue( int col
)
2307 // default col labels are:
2308 // cols 0 to 25 : A-Z
2309 // cols 26 to 675 : AA-ZZ
2314 for ( n
= 1; ; n
++ )
2316 s
+= (_T('A') + (wxChar
)( col%26
));
2318 if ( col
< 0 ) break;
2321 // reverse the string...
2323 for ( i
= 0; i
< n
; i
++ )
2332 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2334 return wxGRID_VALUE_STRING
;
2337 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2338 const wxString
& typeName
)
2340 return typeName
== wxGRID_VALUE_STRING
;
2343 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2345 return CanGetValueAs(row
, col
, typeName
);
2348 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2353 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2358 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2363 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2364 long WXUNUSED(value
) )
2368 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2369 double WXUNUSED(value
) )
2373 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2374 bool WXUNUSED(value
) )
2379 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2380 const wxString
& WXUNUSED(typeName
) )
2385 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2386 const wxString
& WXUNUSED(typeName
),
2387 void* WXUNUSED(value
) )
2391 //////////////////////////////////////////////////////////////////////
2393 // Message class for the grid table to send requests and notifications
2397 wxGridTableMessage::wxGridTableMessage()
2399 m_table
= (wxGridTableBase
*) NULL
;
2405 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2406 int commandInt1
, int commandInt2
)
2410 m_comInt1
= commandInt1
;
2411 m_comInt2
= commandInt2
;
2416 //////////////////////////////////////////////////////////////////////
2418 // A basic grid table for string data. An object of this class will
2419 // created by wxGrid if you don't specify an alternative table class.
2422 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2424 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2426 wxGridStringTable::wxGridStringTable()
2431 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2436 m_data
.Alloc( numRows
);
2439 sa
.Alloc( numCols
);
2440 for ( col
= 0; col
< numCols
; col
++ )
2442 sa
.Add( wxEmptyString
);
2445 for ( row
= 0; row
< numRows
; row
++ )
2451 wxGridStringTable::~wxGridStringTable()
2455 long wxGridStringTable::GetNumberRows()
2457 return m_data
.GetCount();
2460 long wxGridStringTable::GetNumberCols()
2462 if ( m_data
.GetCount() > 0 )
2463 return m_data
[0].GetCount();
2468 wxString
wxGridStringTable::GetValue( int row
, int col
)
2470 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2471 _T("invalid row or column index in wxGridStringTable") );
2473 return m_data
[row
][col
];
2476 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2478 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2479 _T("invalid row or column index in wxGridStringTable") );
2481 m_data
[row
][col
] = value
;
2484 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2486 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2487 _T("invalid row or column index in wxGridStringTable") );
2489 return (m_data
[row
][col
] == wxEmptyString
);
2492 void wxGridStringTable::Clear()
2495 int numRows
, numCols
;
2497 numRows
= m_data
.GetCount();
2500 numCols
= m_data
[0].GetCount();
2502 for ( row
= 0; row
< numRows
; row
++ )
2504 for ( col
= 0; col
< numCols
; col
++ )
2506 m_data
[row
][col
] = wxEmptyString
;
2513 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2517 size_t curNumRows
= m_data
.GetCount();
2518 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2520 if ( pos
>= curNumRows
)
2522 return AppendRows( numRows
);
2526 sa
.Alloc( curNumCols
);
2527 for ( col
= 0; col
< curNumCols
; col
++ )
2529 sa
.Add( wxEmptyString
);
2532 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2534 m_data
.Insert( sa
, row
);
2536 UpdateAttrRows( pos
, numRows
);
2539 wxGridTableMessage
msg( this,
2540 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2544 GetView()->ProcessTableMessage( msg
);
2550 bool wxGridStringTable::AppendRows( size_t numRows
)
2554 size_t curNumRows
= m_data
.GetCount();
2555 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2558 if ( curNumCols
> 0 )
2560 sa
.Alloc( curNumCols
);
2561 for ( col
= 0; col
< curNumCols
; col
++ )
2563 sa
.Add( wxEmptyString
);
2567 for ( row
= 0; row
< numRows
; row
++ )
2574 wxGridTableMessage
msg( this,
2575 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2578 GetView()->ProcessTableMessage( msg
);
2584 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2588 size_t curNumRows
= m_data
.GetCount();
2590 if ( pos
>= curNumRows
)
2593 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2594 "Pos value is invalid for present table with %d rows",
2595 pos
, numRows
, curNumRows
);
2596 wxFAIL_MSG( wxT(errmsg
) );
2600 if ( numRows
> curNumRows
- pos
)
2602 numRows
= curNumRows
- pos
;
2605 if ( numRows
>= curNumRows
)
2607 m_data
.Empty(); // don't release memory just yet
2611 for ( n
= 0; n
< numRows
; n
++ )
2613 m_data
.Remove( pos
);
2616 UpdateAttrRows( pos
, -((int)numRows
) );
2619 wxGridTableMessage
msg( this,
2620 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2624 GetView()->ProcessTableMessage( msg
);
2630 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2634 size_t curNumRows
= m_data
.GetCount();
2635 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2637 if ( pos
>= curNumCols
)
2639 return AppendCols( numCols
);
2642 for ( row
= 0; row
< curNumRows
; row
++ )
2644 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2646 m_data
[row
].Insert( wxEmptyString
, col
);
2649 UpdateAttrCols( pos
, numCols
);
2652 wxGridTableMessage
msg( this,
2653 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2657 GetView()->ProcessTableMessage( msg
);
2663 bool wxGridStringTable::AppendCols( size_t numCols
)
2667 size_t curNumRows
= m_data
.GetCount();
2670 // TODO: something better than this ?
2672 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2673 "Call AppendRows() first") );
2677 for ( row
= 0; row
< curNumRows
; row
++ )
2679 for ( n
= 0; n
< numCols
; n
++ )
2681 m_data
[row
].Add( wxEmptyString
);
2687 wxGridTableMessage
msg( this,
2688 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2691 GetView()->ProcessTableMessage( msg
);
2697 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2701 size_t curNumRows
= m_data
.GetCount();
2702 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2704 if ( pos
>= curNumCols
)
2707 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2708 "Pos value is invalid for present table with %d cols",
2709 pos
, numCols
, curNumCols
);
2710 wxFAIL_MSG( wxT( errmsg
) );
2714 if ( numCols
> curNumCols
- pos
)
2716 numCols
= curNumCols
- pos
;
2719 for ( row
= 0; row
< curNumRows
; row
++ )
2721 if ( numCols
>= curNumCols
)
2723 m_data
[row
].Clear();
2727 for ( n
= 0; n
< numCols
; n
++ )
2729 m_data
[row
].Remove( pos
);
2733 UpdateAttrCols( pos
, -((int)numCols
) );
2736 wxGridTableMessage
msg( this,
2737 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2741 GetView()->ProcessTableMessage( msg
);
2747 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2749 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2751 // using default label
2753 return wxGridTableBase::GetRowLabelValue( row
);
2757 return m_rowLabels
[ row
];
2761 wxString
wxGridStringTable::GetColLabelValue( int col
)
2763 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2765 // using default label
2767 return wxGridTableBase::GetColLabelValue( col
);
2771 return m_colLabels
[ col
];
2775 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2777 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2779 int n
= m_rowLabels
.GetCount();
2781 for ( i
= n
; i
<= row
; i
++ )
2783 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2787 m_rowLabels
[row
] = value
;
2790 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2792 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2794 int n
= m_colLabels
.GetCount();
2796 for ( i
= n
; i
<= col
; i
++ )
2798 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2802 m_colLabels
[col
] = value
;
2807 //////////////////////////////////////////////////////////////////////
2808 //////////////////////////////////////////////////////////////////////
2810 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2812 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2813 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2814 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2815 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2818 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2820 const wxPoint
&pos
, const wxSize
&size
)
2821 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2826 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2830 // NO - don't do this because it will set both the x and y origin
2831 // coords to match the parent scrolled window and we just want to
2832 // set the y coord - MB
2834 // m_owner->PrepareDC( dc );
2837 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2838 dc
.SetDeviceOrigin( 0, -y
);
2840 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2841 m_owner
->DrawRowLabels( dc
);
2845 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2847 m_owner
->ProcessRowLabelMouseEvent( event
);
2851 // This seems to be required for wxMotif otherwise the mouse
2852 // cursor must be in the cell edit control to get key events
2854 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2856 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2861 //////////////////////////////////////////////////////////////////////
2863 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2865 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2866 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2867 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2868 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2871 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2873 const wxPoint
&pos
, const wxSize
&size
)
2874 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2879 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2883 // NO - don't do this because it will set both the x and y origin
2884 // coords to match the parent scrolled window and we just want to
2885 // set the x coord - MB
2887 // m_owner->PrepareDC( dc );
2890 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2891 dc
.SetDeviceOrigin( -x
, 0 );
2893 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2894 m_owner
->DrawColLabels( dc
);
2898 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2900 m_owner
->ProcessColLabelMouseEvent( event
);
2904 // This seems to be required for wxMotif otherwise the mouse
2905 // cursor must be in the cell edit control to get key events
2907 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2909 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2914 //////////////////////////////////////////////////////////////////////
2916 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2918 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2919 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2920 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2921 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2924 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2926 const wxPoint
&pos
, const wxSize
&size
)
2927 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2932 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2936 int client_height
= 0;
2937 int client_width
= 0;
2938 GetClientSize( &client_width
, &client_height
);
2940 dc
.SetPen( *wxBLACK_PEN
);
2941 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2942 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2944 dc
.SetPen( *wxWHITE_PEN
);
2945 dc
.DrawLine( 0, 0, client_width
, 0 );
2946 dc
.DrawLine( 0, 0, 0, client_height
);
2950 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2952 m_owner
->ProcessCornerLabelMouseEvent( event
);
2956 // This seems to be required for wxMotif otherwise the mouse
2957 // cursor must be in the cell edit control to get key events
2959 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2961 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2966 //////////////////////////////////////////////////////////////////////
2968 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2970 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2971 EVT_PAINT( wxGridWindow::OnPaint
)
2972 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2973 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2974 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2977 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2978 wxGridRowLabelWindow
*rowLblWin
,
2979 wxGridColLabelWindow
*colLblWin
,
2980 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2981 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2984 m_rowLabelWin
= rowLblWin
;
2985 m_colLabelWin
= colLblWin
;
2986 SetBackgroundColour( "WHITE" );
2990 wxGridWindow::~wxGridWindow()
2995 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2997 wxPaintDC
dc( this );
2998 m_owner
->PrepareDC( dc
);
2999 wxRegion reg
= GetUpdateRegion();
3000 m_owner
->CalcCellsExposed( reg
);
3001 m_owner
->DrawGridCellArea( dc
);
3002 #if WXGRID_DRAW_LINES
3003 m_owner
->DrawAllGridLines( dc
, reg
);
3005 m_owner
->DrawGridSpace( dc
);
3006 m_owner
->DrawHighlight( dc
);
3010 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3012 wxPanel::ScrollWindow( dx
, dy
, rect
);
3013 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3014 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3018 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3020 m_owner
->ProcessGridCellMouseEvent( event
);
3024 // This seems to be required for wxMotif otherwise the mouse
3025 // cursor must be in the cell edit control to get key events
3027 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3029 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3033 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
3038 //////////////////////////////////////////////////////////////////////
3041 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3043 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3044 EVT_PAINT( wxGrid::OnPaint
)
3045 EVT_SIZE( wxGrid::OnSize
)
3046 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3047 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3050 wxGrid::wxGrid( wxWindow
*parent
,
3055 const wxString
& name
)
3056 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3057 m_colMinWidths(GRID_HASH_SIZE
),
3058 m_rowMinHeights(GRID_HASH_SIZE
)
3067 wxSafeDecRef(m_defaultCellAttr
);
3069 #ifdef DEBUG_ATTR_CACHE
3070 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3071 wxPrintf(_T("wxGrid attribute cache statistics: "
3072 "total: %u, hits: %u (%u%%)\n"),
3073 total
, gs_nAttrCacheHits
,
3074 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3080 delete m_typeRegistry
;
3085 // ----- internal init and update functions
3088 void wxGrid::Create()
3090 m_created
= FALSE
; // set to TRUE by CreateGrid
3091 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
3093 m_table
= (wxGridTableBase
*) NULL
;
3096 m_cellEditCtrlEnabled
= FALSE
;
3098 m_defaultCellAttr
= new wxGridCellAttr
;
3099 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3101 // Set default cell attributes
3102 m_defaultCellAttr
->SetFont(GetFont());
3103 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
3104 m_defaultCellAttr
->SetTextColour(
3105 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3106 m_defaultCellAttr
->SetBackgroundColour(
3107 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3108 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3109 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3114 m_currentCellCoords
= wxGridNoCellCoords
;
3116 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3117 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3119 // create the type registry
3120 m_typeRegistry
= new wxGridTypeRegistry
;
3122 // subwindow components that make up the wxGrid
3123 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3128 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3133 m_colLabelWin
= new wxGridColLabelWindow( this,
3138 m_gridWin
= new wxGridWindow( this,
3145 SetTargetWindow( m_gridWin
);
3149 bool wxGrid::CreateGrid( int numRows
, int numCols
)
3153 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3158 m_numRows
= numRows
;
3159 m_numCols
= numCols
;
3161 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3162 m_table
->SetView( this );
3171 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
3175 // RD: Actually, this should probably be allowed. I think it would be
3176 // nice to be able to switch multiple Tables in and out of a single
3177 // View at runtime. Is there anything in the implmentation that would
3180 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3185 m_numRows
= table
->GetNumberRows();
3186 m_numCols
= table
->GetNumberCols();
3189 m_table
->SetView( this );
3202 if ( m_numRows
<= 0 )
3203 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
3205 if ( m_numCols
<= 0 )
3206 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
3208 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3209 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3211 if ( m_rowLabelWin
)
3213 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3217 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3220 m_labelTextColour
= wxColour( _T("BLACK") );
3223 m_attrCache
.row
= -1;
3225 // TODO: something better than this ?
3227 m_labelFont
= this->GetFont();
3228 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3230 m_rowLabelHorizAlign
= wxLEFT
;
3231 m_rowLabelVertAlign
= wxCENTRE
;
3233 m_colLabelHorizAlign
= wxCENTRE
;
3234 m_colLabelVertAlign
= wxTOP
;
3236 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3237 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3239 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3240 m_defaultRowHeight
+= 8;
3242 m_defaultRowHeight
+= 4;
3245 m_gridLineColour
= wxColour( 128, 128, 255 );
3246 m_gridLinesEnabled
= TRUE
;
3248 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3249 m_winCapture
= (wxWindow
*)NULL
;
3250 m_canDragRowSize
= TRUE
;
3251 m_canDragColSize
= TRUE
;
3252 m_canDragGridSize
= TRUE
;
3254 m_dragRowOrCol
= -1;
3255 m_isDragging
= FALSE
;
3256 m_startDragPos
= wxDefaultPosition
;
3258 m_waitForSlowClick
= FALSE
;
3260 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3261 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3263 m_currentCellCoords
= wxGridNoCellCoords
;
3265 m_selectedTopLeft
= wxGridNoCellCoords
;
3266 m_selectedBottomRight
= wxGridNoCellCoords
;
3267 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3268 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3270 m_editable
= TRUE
; // default for whole grid
3272 m_inOnKeyDown
= FALSE
;
3281 // ----------------------------------------------------------------------------
3282 // the idea is to call these functions only when necessary because they create
3283 // quite big arrays which eat memory mostly unnecessary - in particular, if
3284 // default widths/heights are used for all rows/columns, we may not use these
3287 // with some extra code, it should be possible to only store the
3288 // widths/heights different from default ones but this will be done later...
3289 // ----------------------------------------------------------------------------
3291 void wxGrid::InitRowHeights()
3293 m_rowHeights
.Empty();
3294 m_rowBottoms
.Empty();
3296 m_rowHeights
.Alloc( m_numRows
);
3297 m_rowBottoms
.Alloc( m_numRows
);
3300 for ( int i
= 0; i
< m_numRows
; i
++ )
3302 m_rowHeights
.Add( m_defaultRowHeight
);
3303 rowBottom
+= m_defaultRowHeight
;
3304 m_rowBottoms
.Add( rowBottom
);
3308 void wxGrid::InitColWidths()
3310 m_colWidths
.Empty();
3311 m_colRights
.Empty();
3313 m_colWidths
.Alloc( m_numCols
);
3314 m_colRights
.Alloc( m_numCols
);
3316 for ( int i
= 0; i
< m_numCols
; i
++ )
3318 m_colWidths
.Add( m_defaultColWidth
);
3319 colRight
+= m_defaultColWidth
;
3320 m_colRights
.Add( colRight
);
3324 int wxGrid::GetColWidth(int col
) const
3326 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3329 int wxGrid::GetColLeft(int col
) const
3331 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3332 : m_colRights
[col
] - m_colWidths
[col
];
3335 int wxGrid::GetColRight(int col
) const
3337 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3341 int wxGrid::GetRowHeight(int row
) const
3343 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3346 int wxGrid::GetRowTop(int row
) const
3348 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3349 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3352 int wxGrid::GetRowBottom(int row
) const
3354 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3355 : m_rowBottoms
[row
];
3358 void wxGrid::CalcDimensions()
3361 GetClientSize( &cw
, &ch
);
3363 if ( m_numRows
> 0 && m_numCols
> 0 )
3365 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3366 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3368 // TODO: restore the scroll position that we had before sizing
3371 GetViewStart( &x
, &y
);
3372 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3373 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3379 void wxGrid::CalcWindowSizes()
3382 GetClientSize( &cw
, &ch
);
3384 if ( m_cornerLabelWin
->IsShown() )
3385 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3387 if ( m_colLabelWin
->IsShown() )
3388 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3390 if ( m_rowLabelWin
->IsShown() )
3391 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3393 if ( m_gridWin
->IsShown() )
3394 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3398 // this is called when the grid table sends a message to say that it
3399 // has been redimensioned
3401 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3405 // if we were using the default widths/heights so far, we must change them
3407 if ( m_colWidths
.IsEmpty() )
3412 if ( m_rowHeights
.IsEmpty() )
3417 switch ( msg
.GetId() )
3419 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3421 size_t pos
= msg
.GetCommandInt();
3422 int numRows
= msg
.GetCommandInt2();
3423 for ( i
= 0; i
< numRows
; i
++ )
3425 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3426 m_rowBottoms
.Insert( 0, pos
);
3428 m_numRows
+= numRows
;
3431 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3433 for ( i
= pos
; i
< m_numRows
; i
++ )
3435 bottom
+= m_rowHeights
[i
];
3436 m_rowBottoms
[i
] = bottom
;
3442 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3444 int numRows
= msg
.GetCommandInt();
3445 for ( i
= 0; i
< numRows
; i
++ )
3447 m_rowHeights
.Add( m_defaultRowHeight
);
3448 m_rowBottoms
.Add( 0 );
3451 int oldNumRows
= m_numRows
;
3452 m_numRows
+= numRows
;
3455 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3457 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3459 bottom
+= m_rowHeights
[i
];
3460 m_rowBottoms
[i
] = bottom
;
3466 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3468 size_t pos
= msg
.GetCommandInt();
3469 int numRows
= msg
.GetCommandInt2();
3470 for ( i
= 0; i
< numRows
; i
++ )
3472 m_rowHeights
.Remove( pos
);
3473 m_rowBottoms
.Remove( pos
);
3475 m_numRows
-= numRows
;
3480 m_colWidths
.Clear();
3481 m_colRights
.Clear();
3482 m_currentCellCoords
= wxGridNoCellCoords
;
3486 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3487 m_currentCellCoords
.Set( 0, 0 );
3490 for ( i
= 0; i
< m_numRows
; i
++ )
3492 h
+= m_rowHeights
[i
];
3493 m_rowBottoms
[i
] = h
;
3501 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3503 size_t pos
= msg
.GetCommandInt();
3504 int numCols
= msg
.GetCommandInt2();
3505 for ( i
= 0; i
< numCols
; i
++ )
3507 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3508 m_colRights
.Insert( 0, pos
);
3510 m_numCols
+= numCols
;
3513 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3515 for ( i
= pos
; i
< m_numCols
; i
++ )
3517 right
+= m_colWidths
[i
];
3518 m_colRights
[i
] = right
;
3524 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3526 int numCols
= msg
.GetCommandInt();
3527 for ( i
= 0; i
< numCols
; i
++ )
3529 m_colWidths
.Add( m_defaultColWidth
);
3530 m_colRights
.Add( 0 );
3533 int oldNumCols
= m_numCols
;
3534 m_numCols
+= numCols
;
3537 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3539 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3541 right
+= m_colWidths
[i
];
3542 m_colRights
[i
] = right
;
3548 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3550 size_t pos
= msg
.GetCommandInt();
3551 int numCols
= msg
.GetCommandInt2();
3552 for ( i
= 0; i
< numCols
; i
++ )
3554 m_colWidths
.Remove( pos
);
3555 m_colRights
.Remove( pos
);
3557 m_numCols
-= numCols
;
3561 #if 0 // leave the row alone here so that AppendCols will work subsequently
3563 m_rowHeights
.Clear();
3564 m_rowBottoms
.Clear();
3566 m_currentCellCoords
= wxGridNoCellCoords
;
3570 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3571 m_currentCellCoords
.Set( 0, 0 );
3574 for ( i
= 0; i
< m_numCols
; i
++ )
3576 w
+= m_colWidths
[i
];
3589 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3591 wxRegionIterator
iter( reg
);
3594 m_rowLabelsExposed
.Empty();
3601 // TODO: remove this when we can...
3602 // There is a bug in wxMotif that gives garbage update
3603 // rectangles if you jump-scroll a long way by clicking the
3604 // scrollbar with middle button. This is a work-around
3606 #if defined(__WXMOTIF__)
3608 m_gridWin
->GetClientSize( &cw
, &ch
);
3609 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3610 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3613 // logical bounds of update region
3616 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3617 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3619 // find the row labels within these bounds
3622 for ( row
= 0; row
< m_numRows
; row
++ )
3624 if ( GetRowBottom(row
) < top
)
3627 if ( GetRowTop(row
) > bottom
)
3630 m_rowLabelsExposed
.Add( row
);
3638 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3640 wxRegionIterator
iter( reg
);
3643 m_colLabelsExposed
.Empty();
3650 // TODO: remove this when we can...
3651 // There is a bug in wxMotif that gives garbage update
3652 // rectangles if you jump-scroll a long way by clicking the
3653 // scrollbar with middle button. This is a work-around
3655 #if defined(__WXMOTIF__)
3657 m_gridWin
->GetClientSize( &cw
, &ch
);
3658 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3659 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3662 // logical bounds of update region
3665 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3666 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3668 // find the cells within these bounds
3671 for ( col
= 0; col
< m_numCols
; col
++ )
3673 if ( GetColRight(col
) < left
)
3676 if ( GetColLeft(col
) > right
)
3679 m_colLabelsExposed
.Add( col
);
3687 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3689 wxRegionIterator
iter( reg
);
3692 m_cellsExposed
.Empty();
3693 m_rowsExposed
.Empty();
3694 m_colsExposed
.Empty();
3696 int left
, top
, right
, bottom
;
3701 // TODO: remove this when we can...
3702 // There is a bug in wxMotif that gives garbage update
3703 // rectangles if you jump-scroll a long way by clicking the
3704 // scrollbar with middle button. This is a work-around
3706 #if defined(__WXMOTIF__)
3708 m_gridWin
->GetClientSize( &cw
, &ch
);
3709 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3710 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3711 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3712 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3715 // logical bounds of update region
3717 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3718 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3720 // find the cells within these bounds
3723 for ( row
= 0; row
< m_numRows
; row
++ )
3725 if ( GetRowBottom(row
) <= top
)
3728 if ( GetRowTop(row
) > bottom
)
3731 m_rowsExposed
.Add( row
);
3733 for ( col
= 0; col
< m_numCols
; col
++ )
3735 if ( GetColRight(col
) <= left
)
3738 if ( GetColLeft(col
) > right
)
3741 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3742 m_colsExposed
.Add( col
);
3743 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3752 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3755 wxPoint
pos( event
.GetPosition() );
3756 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3758 if ( event
.Dragging() )
3760 m_isDragging
= TRUE
;
3762 if ( event
.LeftIsDown() )
3764 switch( m_cursorMode
)
3766 case WXGRID_CURSOR_RESIZE_ROW
:
3768 int cw
, ch
, left
, dummy
;
3769 m_gridWin
->GetClientSize( &cw
, &ch
);
3770 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3772 wxClientDC
dc( m_gridWin
);
3775 GetRowTop(m_dragRowOrCol
) +
3776 GetRowMinimalHeight(m_dragRowOrCol
) );
3777 dc
.SetLogicalFunction(wxINVERT
);
3778 if ( m_dragLastPos
>= 0 )
3780 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3782 dc
.DrawLine( left
, y
, left
+cw
, y
);
3787 case WXGRID_CURSOR_SELECT_ROW
:
3788 if ( (row
= YToRow( y
)) >= 0 &&
3789 !IsInSelection( row
, 0 ) )
3791 SelectRow( row
, TRUE
);
3794 // default label to suppress warnings about "enumeration value
3795 // 'xxx' not handled in switch
3803 m_isDragging
= FALSE
;
3806 // ------------ Entering or leaving the window
3808 if ( event
.Entering() || event
.Leaving() )
3810 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3814 // ------------ Left button pressed
3816 else if ( event
.LeftDown() )
3818 // don't send a label click event for a hit on the
3819 // edge of the row label - this is probably the user
3820 // wanting to resize the row
3822 if ( YToEdgeOfRow(y
) < 0 )
3826 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3828 SelectRow( row
, event
.ShiftDown() );
3829 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3834 // starting to drag-resize a row
3836 if ( CanDragRowSize() )
3837 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3842 // ------------ Left double click
3844 else if (event
.LeftDClick() )
3846 if ( YToEdgeOfRow(y
) < 0 )
3849 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3854 // ------------ Left button released
3856 else if ( event
.LeftUp() )
3858 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3860 DoEndDragResizeRow();
3862 // Note: we are ending the event *after* doing
3863 // default processing in this case
3865 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3868 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3873 // ------------ Right button down
3875 else if ( event
.RightDown() )
3878 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3880 // no default action at the moment
3885 // ------------ Right double click
3887 else if ( event
.RightDClick() )
3890 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3892 // no default action at the moment
3897 // ------------ No buttons down and mouse moving
3899 else if ( event
.Moving() )
3901 m_dragRowOrCol
= YToEdgeOfRow( y
);
3902 if ( m_dragRowOrCol
>= 0 )
3904 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3906 // don't capture the mouse yet
3907 if ( CanDragRowSize() )
3908 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3911 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3913 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3919 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3922 wxPoint
pos( event
.GetPosition() );
3923 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3925 if ( event
.Dragging() )
3927 m_isDragging
= TRUE
;
3929 if ( event
.LeftIsDown() )
3931 switch( m_cursorMode
)
3933 case WXGRID_CURSOR_RESIZE_COL
:
3935 int cw
, ch
, dummy
, top
;
3936 m_gridWin
->GetClientSize( &cw
, &ch
);
3937 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3939 wxClientDC
dc( m_gridWin
);
3942 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3943 GetColMinimalWidth(m_dragRowOrCol
));
3944 dc
.SetLogicalFunction(wxINVERT
);
3945 if ( m_dragLastPos
>= 0 )
3947 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3949 dc
.DrawLine( x
, top
, x
, top
+ch
);
3954 case WXGRID_CURSOR_SELECT_COL
:
3955 if ( (col
= XToCol( x
)) >= 0 &&
3956 !IsInSelection( 0, col
) )
3958 SelectCol( col
, TRUE
);
3961 // default label to suppress warnings about "enumeration value
3962 // 'xxx' not handled in switch
3970 m_isDragging
= FALSE
;
3973 // ------------ Entering or leaving the window
3975 if ( event
.Entering() || event
.Leaving() )
3977 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3981 // ------------ Left button pressed
3983 else if ( event
.LeftDown() )
3985 // don't send a label click event for a hit on the
3986 // edge of the col label - this is probably the user
3987 // wanting to resize the col
3989 if ( XToEdgeOfCol(x
) < 0 )
3993 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3995 SelectCol( col
, event
.ShiftDown() );
3996 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4001 // starting to drag-resize a col
4003 if ( CanDragColSize() )
4004 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4009 // ------------ Left double click
4011 if ( event
.LeftDClick() )
4013 if ( XToEdgeOfCol(x
) < 0 )
4016 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4021 // ------------ Left button released
4023 else if ( event
.LeftUp() )
4025 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4027 DoEndDragResizeCol();
4029 // Note: we are ending the event *after* doing
4030 // default processing in this case
4032 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4035 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4040 // ------------ Right button down
4042 else if ( event
.RightDown() )
4045 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4047 // no default action at the moment
4052 // ------------ Right double click
4054 else if ( event
.RightDClick() )
4057 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4059 // no default action at the moment
4064 // ------------ No buttons down and mouse moving
4066 else if ( event
.Moving() )
4068 m_dragRowOrCol
= XToEdgeOfCol( x
);
4069 if ( m_dragRowOrCol
>= 0 )
4071 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4073 // don't capture the cursor yet
4074 if ( CanDragColSize() )
4075 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4078 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4080 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4086 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4088 if ( event
.LeftDown() )
4090 // indicate corner label by having both row and
4093 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4099 else if ( event
.LeftDClick() )
4101 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4104 else if ( event
.RightDown() )
4106 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4108 // no default action at the moment
4112 else if ( event
.RightDClick() )
4114 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4116 // no default action at the moment
4121 void wxGrid::ChangeCursorMode(CursorMode mode
,
4126 static const wxChar
*cursorModes
[] =
4135 wxLogTrace(_T("grid"),
4136 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4137 win
== m_colLabelWin
? _T("colLabelWin")
4138 : win
? _T("rowLabelWin")
4140 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4141 #endif // __WXDEBUG__
4143 if ( mode
== m_cursorMode
)
4148 // by default use the grid itself
4154 m_winCapture
->ReleaseMouse();
4155 m_winCapture
= (wxWindow
*)NULL
;
4158 m_cursorMode
= mode
;
4160 switch ( m_cursorMode
)
4162 case WXGRID_CURSOR_RESIZE_ROW
:
4163 win
->SetCursor( m_rowResizeCursor
);
4166 case WXGRID_CURSOR_RESIZE_COL
:
4167 win
->SetCursor( m_colResizeCursor
);
4171 win
->SetCursor( *wxSTANDARD_CURSOR
);
4174 // we need to capture mouse when resizing
4175 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4176 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4178 if ( captureMouse
&& resize
)
4180 win
->CaptureMouse();
4185 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4188 wxPoint
pos( event
.GetPosition() );
4189 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4191 wxGridCellCoords coords
;
4192 XYToCell( x
, y
, coords
);
4194 if ( event
.Dragging() )
4196 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4198 // Don't start doing anything until the mouse has been drug at
4199 // least 3 pixels in any direction...
4202 if (m_startDragPos
== wxDefaultPosition
)
4204 m_startDragPos
= pos
;
4207 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4211 m_isDragging
= TRUE
;
4212 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4214 // Hide the edit control, so it
4215 // won't interfer with drag-shrinking.
4216 if ( IsCellEditControlEnabled() )
4218 HideCellEditControl();
4219 SaveEditControlValue();
4222 // Have we captured the mouse yet?
4225 m_winCapture
= m_gridWin
;
4226 m_winCapture
->CaptureMouse();
4229 if ( coords
!= wxGridNoCellCoords
)
4231 if ( !IsSelection() )
4233 SelectBlock( coords
, coords
);
4237 SelectBlock( m_currentCellCoords
, coords
);
4240 if (! IsVisible(coords
))
4242 MakeCellVisible(coords
);
4243 // TODO: need to introduce a delay or something here. The
4244 // scrolling is way to fast, at least on MSW.
4248 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4250 int cw
, ch
, left
, dummy
;
4251 m_gridWin
->GetClientSize( &cw
, &ch
);
4252 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4254 wxClientDC
dc( m_gridWin
);
4256 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4257 GetRowMinimalHeight(m_dragRowOrCol
) );
4258 dc
.SetLogicalFunction(wxINVERT
);
4259 if ( m_dragLastPos
>= 0 )
4261 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4263 dc
.DrawLine( left
, y
, left
+cw
, y
);
4266 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4268 int cw
, ch
, dummy
, top
;
4269 m_gridWin
->GetClientSize( &cw
, &ch
);
4270 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4272 wxClientDC
dc( m_gridWin
);
4274 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4275 GetColMinimalWidth(m_dragRowOrCol
) );
4276 dc
.SetLogicalFunction(wxINVERT
);
4277 if ( m_dragLastPos
>= 0 )
4279 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4281 dc
.DrawLine( x
, top
, x
, top
+ch
);
4288 m_isDragging
= FALSE
;
4289 m_startDragPos
= wxDefaultPosition
;
4291 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4292 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4295 if ( event
.Entering() || event
.Leaving() )
4297 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4298 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4303 // ------------ Left button pressed
4305 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4307 if ( event
.ShiftDown() )
4309 SelectBlock( m_currentCellCoords
, coords
);
4311 else if ( XToEdgeOfCol(x
) < 0 &&
4312 YToEdgeOfRow(y
) < 0 )
4314 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4319 DisableCellEditControl();
4320 MakeCellVisible( coords
);
4322 // if this is the second click on this cell then start
4324 if ( m_waitForSlowClick
&&
4325 (coords
== m_currentCellCoords
) &&
4326 CanEnableCellControl())
4328 EnableCellEditControl();
4330 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4331 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4334 editor
->StartingClick();
4338 m_waitForSlowClick
= FALSE
;
4342 SetCurrentCell( coords
);
4343 m_waitForSlowClick
= TRUE
;
4350 // ------------ Left double click
4352 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4354 DisableCellEditControl();
4356 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4358 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4366 // ------------ Left button released
4368 else if ( event
.LeftUp() )
4370 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4372 if ( IsSelection() )
4376 m_winCapture
->ReleaseMouse();
4377 m_winCapture
= NULL
;
4379 SendEvent( wxEVT_GRID_RANGE_SELECT
, -1, -1, event
);
4382 // Show the edit control, if it has been hidden for
4384 ShowCellEditControl();
4386 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4388 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4389 DoEndDragResizeRow();
4391 // Note: we are ending the event *after* doing
4392 // default processing in this case
4394 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4396 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4398 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4399 DoEndDragResizeCol();
4401 // Note: we are ending the event *after* doing
4402 // default processing in this case
4404 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4411 // ------------ Right button down
4413 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4415 DisableCellEditControl();
4416 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4421 // no default action at the moment
4426 // ------------ Right double click
4428 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4430 DisableCellEditControl();
4431 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4436 // no default action at the moment
4440 // ------------ Moving and no button action
4442 else if ( event
.Moving() && !event
.IsButton() )
4444 int dragRow
= YToEdgeOfRow( y
);
4445 int dragCol
= XToEdgeOfCol( x
);
4447 // Dragging on the corner of a cell to resize in both
4448 // directions is not implemented yet...
4450 if ( dragRow
>= 0 && dragCol
>= 0 )
4452 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4458 m_dragRowOrCol
= dragRow
;
4460 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4462 if ( CanDragRowSize() && CanDragGridSize() )
4463 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4468 m_dragRowOrCol
= dragCol
;
4476 m_dragRowOrCol
= dragCol
;
4478 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4480 if ( CanDragColSize() && CanDragGridSize() )
4481 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
4487 // Neither on a row or col edge
4489 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4491 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4497 void wxGrid::DoEndDragResizeRow()
4499 if ( m_dragLastPos
>= 0 )
4501 // erase the last line and resize the row
4503 int cw
, ch
, left
, dummy
;
4504 m_gridWin
->GetClientSize( &cw
, &ch
);
4505 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4507 wxClientDC
dc( m_gridWin
);
4509 dc
.SetLogicalFunction( wxINVERT
);
4510 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4511 HideCellEditControl();
4512 SaveEditControlValue();
4514 int rowTop
= GetRowTop(m_dragRowOrCol
);
4515 SetRowSize( m_dragRowOrCol
,
4516 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
4518 if ( !GetBatchCount() )
4520 // Only needed to get the correct rect.y:
4521 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
4523 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
4524 rect
.width
= m_rowLabelWidth
;
4525 rect
.height
= ch
- rect
.y
;
4526 m_rowLabelWin
->Refresh( TRUE
, &rect
);
4528 m_gridWin
->Refresh( FALSE
, &rect
);
4531 ShowCellEditControl();
4536 void wxGrid::DoEndDragResizeCol()
4538 if ( m_dragLastPos
>= 0 )
4540 // erase the last line and resize the col
4542 int cw
, ch
, dummy
, top
;
4543 m_gridWin
->GetClientSize( &cw
, &ch
);
4544 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4546 wxClientDC
dc( m_gridWin
);
4548 dc
.SetLogicalFunction( wxINVERT
);
4549 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4550 HideCellEditControl();
4551 SaveEditControlValue();
4553 int colLeft
= GetColLeft(m_dragRowOrCol
);
4554 SetColSize( m_dragRowOrCol
,
4555 wxMax( m_dragLastPos
- colLeft
,
4556 GetColMinimalWidth(m_dragRowOrCol
) ) );
4558 if ( !GetBatchCount() )
4560 // Only needed to get the correct rect.x:
4561 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
4563 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
4564 rect
.width
= cw
- rect
.x
;
4565 rect
.height
= m_colLabelHeight
;
4566 m_colLabelWin
->Refresh( TRUE
, &rect
);
4568 m_gridWin
->Refresh( FALSE
, &rect
);
4571 ShowCellEditControl();
4578 // ------ interaction with data model
4580 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
4582 switch ( msg
.GetId() )
4584 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
4585 return GetModelValues();
4587 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
4588 return SetModelValues();
4590 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4591 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4592 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4593 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4594 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4595 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4596 return Redimension( msg
);
4605 // The behaviour of this function depends on the grid table class
4606 // Clear() function. For the default wxGridStringTable class the
4607 // behavious is to replace all cell contents with wxEmptyString but
4608 // not to change the number of rows or cols.
4610 void wxGrid::ClearGrid()
4614 if (IsCellEditControlEnabled())
4615 DisableCellEditControl();
4618 if ( !GetBatchCount() ) m_gridWin
->Refresh();
4623 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4625 // TODO: something with updateLabels flag
4629 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
4635 if (IsCellEditControlEnabled())
4636 DisableCellEditControl();
4638 bool ok
= m_table
->InsertRows( pos
, numRows
);
4640 // the table will have sent the results of the insert row
4641 // operation to this view object as a grid table message
4645 if ( m_numCols
== 0 )
4647 m_table
->AppendCols( WXGRID_DEFAULT_NUMBER_COLS
);
4649 // TODO: perhaps instead of appending the default number of cols
4650 // we should remember what the last non-zero number of cols was ?
4654 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4656 // if we have just inserted cols into an empty grid the current
4657 // cell will be undefined...
4659 SetCurrentCell( 0, 0 );
4663 if ( !GetBatchCount() ) Refresh();
4675 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
4677 // TODO: something with updateLabels flag
4681 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
4685 if ( m_table
&& m_table
->AppendRows( numRows
) )
4687 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4689 // if we have just inserted cols into an empty grid the current
4690 // cell will be undefined...
4692 SetCurrentCell( 0, 0 );
4695 // the table will have sent the results of the append row
4696 // operation to this view object as a grid table message
4699 if ( !GetBatchCount() ) Refresh();
4709 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
4711 // TODO: something with updateLabels flag
4715 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
4721 if (IsCellEditControlEnabled())
4722 DisableCellEditControl();
4724 if (m_table
->DeleteRows( pos
, numRows
))
4727 // the table will have sent the results of the delete row
4728 // operation to this view object as a grid table message
4731 if ( !GetBatchCount() ) Refresh();
4739 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4741 // TODO: something with updateLabels flag
4745 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
4751 if (IsCellEditControlEnabled())
4752 DisableCellEditControl();
4754 bool ok
= m_table
->InsertCols( pos
, numCols
);
4756 // the table will have sent the results of the insert col
4757 // operation to this view object as a grid table message
4761 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4763 // if we have just inserted cols into an empty grid the current
4764 // cell will be undefined...
4766 SetCurrentCell( 0, 0 );
4770 if ( !GetBatchCount() ) Refresh();
4782 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
4784 // TODO: something with updateLabels flag
4788 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
4792 if ( m_table
&& m_table
->AppendCols( numCols
) )
4794 // the table will have sent the results of the append col
4795 // operation to this view object as a grid table message
4797 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4799 // if we have just inserted cols into an empty grid the current
4800 // cell will be undefined...
4802 SetCurrentCell( 0, 0 );
4806 if ( !GetBatchCount() ) Refresh();
4816 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
4818 // TODO: something with updateLabels flag
4822 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
4828 if (IsCellEditControlEnabled())
4829 DisableCellEditControl();
4831 if ( m_table
->DeleteCols( pos
, numCols
) )
4833 // the table will have sent the results of the delete col
4834 // operation to this view object as a grid table message
4837 if ( !GetBatchCount() ) Refresh();
4847 // ----- event handlers
4850 // Generate a grid event based on a mouse event and
4851 // return the result of ProcessEvent()
4853 bool wxGrid::SendEvent( const wxEventType type
,
4855 wxMouseEvent
& mouseEv
)
4857 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4859 int rowOrCol
= (row
== -1 ? col
: row
);
4861 wxGridSizeEvent
gridEvt( GetId(),
4865 mouseEv
.GetX(), mouseEv
.GetY(),
4866 mouseEv
.ControlDown(),
4867 mouseEv
.ShiftDown(),
4869 mouseEv
.MetaDown() );
4871 return GetEventHandler()->ProcessEvent(gridEvt
);
4873 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
4875 wxGridRangeSelectEvent
gridEvt( GetId(),
4879 m_selectedBottomRight
,
4880 mouseEv
.ControlDown(),
4881 mouseEv
.ShiftDown(),
4883 mouseEv
.MetaDown() );
4885 return GetEventHandler()->ProcessEvent(gridEvt
);
4889 wxGridEvent
gridEvt( GetId(),
4893 mouseEv
.GetX(), mouseEv
.GetY(),
4894 mouseEv
.ControlDown(),
4895 mouseEv
.ShiftDown(),
4897 mouseEv
.MetaDown() );
4899 return GetEventHandler()->ProcessEvent(gridEvt
);
4904 // Generate a grid event of specified type and return the result
4905 // of ProcessEvent().
4907 bool wxGrid::SendEvent( const wxEventType type
,
4910 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
4912 int rowOrCol
= (row
== -1 ? col
: row
);
4914 wxGridSizeEvent
gridEvt( GetId(),
4919 return GetEventHandler()->ProcessEvent(gridEvt
);
4923 wxGridEvent
gridEvt( GetId(),
4928 return GetEventHandler()->ProcessEvent(gridEvt
);
4933 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
4935 wxPaintDC
dc( this );
4937 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
4938 m_numRows
&& m_numCols
)
4940 m_currentCellCoords
.Set(0, 0);
4941 ShowCellEditControl();
4948 // This is just here to make sure that CalcDimensions gets called when
4949 // the grid view is resized... then the size event is skipped to allow
4950 // the box sizers to handle everything
4952 void wxGrid::OnSize( wxSizeEvent
& event
)
4959 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
4961 if ( m_inOnKeyDown
)
4963 // shouldn't be here - we are going round in circles...
4965 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
4968 m_inOnKeyDown
= TRUE
;
4970 // propagate the event up and see if it gets processed
4972 wxWindow
*parent
= GetParent();
4973 wxKeyEvent
keyEvt( event
);
4974 keyEvt
.SetEventObject( parent
);
4976 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
4979 // TODO: Should also support Shift-cursor keys for
4980 // extending the selection. Maybe add a flag to
4981 // MoveCursorXXX() and MoveCursorXXXBlock() and
4982 // just send event.ShiftDown().
4984 // try local handlers
4986 switch ( event
.KeyCode() )
4989 if ( event
.ControlDown() )
4991 MoveCursorUpBlock();
5000 if ( event
.ControlDown() )
5002 MoveCursorDownBlock();
5011 if ( event
.ControlDown() )
5013 MoveCursorLeftBlock();
5022 if ( event
.ControlDown() )
5024 MoveCursorRightBlock();
5033 if ( event
.ControlDown() )
5035 event
.Skip(); // to let the edit control have the return
5044 if (event
.ShiftDown())
5051 if ( event
.ControlDown() )
5053 MakeCellVisible( 0, 0 );
5054 SetCurrentCell( 0, 0 );
5063 if ( event
.ControlDown() )
5065 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5066 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5083 if ( !IsEditable() )
5088 // Otherwise fall through to default
5091 // alphanumeric keys or F2 (special key just for this) enable
5092 // the cell edit control
5093 if ( !(event
.AltDown() ||
5095 event
.ControlDown()) &&
5096 (isalnum(event
.KeyCode()) || event
.KeyCode() == WXK_F2
) &&
5097 !IsCellEditControlEnabled() &&
5098 CanEnableCellControl() )
5100 EnableCellEditControl();
5101 int row
= m_currentCellCoords
.GetRow();
5102 int col
= m_currentCellCoords
.GetCol();
5103 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5104 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5105 editor
->StartingKey(event
);
5111 // let others process char events with modifiers or all
5112 // char events for readonly cells
5119 m_inOnKeyDown
= FALSE
;
5123 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5127 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5129 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5131 // the event has been intercepted - do nothing
5136 m_currentCellCoords
!= wxGridNoCellCoords
)
5138 HideCellEditControl();
5139 DisableCellEditControl();
5141 // Clear the old current cell highlight
5142 wxRect r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5144 // Otherwise refresh redraws the highlight!
5145 m_currentCellCoords
= coords
;
5147 m_gridWin
->Refresh( FALSE
, &r
);
5150 m_currentCellCoords
= coords
;
5154 wxClientDC
dc(m_gridWin
);
5157 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5158 DrawCellHighlight(dc
, attr
);
5161 if ( IsSelection() )
5163 wxRect
r( SelectionToDeviceRect() );
5165 if ( !GetBatchCount() ) m_gridWin
->Refresh( FALSE
, &r
);
5172 // ------ functions to get/send data (see also public functions)
5175 bool wxGrid::GetModelValues()
5179 // all we need to do is repaint the grid
5181 m_gridWin
->Refresh();
5189 bool wxGrid::SetModelValues()
5195 for ( row
= 0; row
< m_numRows
; row
++ )
5197 for ( col
= 0; col
< m_numCols
; col
++ )
5199 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5211 // Note - this function only draws cells that are in the list of
5212 // exposed cells (usually set from the update region by
5213 // CalcExposedCells)
5215 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5217 if ( !m_numRows
|| !m_numCols
) return;
5220 size_t numCells
= m_cellsExposed
.GetCount();
5222 for ( i
= 0; i
< numCells
; i
++ )
5224 DrawCell( dc
, m_cellsExposed
[i
] );
5229 void wxGrid::DrawGridSpace( wxDC
& dc
)
5231 if ( m_numRows
&& m_numCols
)
5234 m_gridWin
->GetClientSize( &cw
, &ch
);
5237 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5239 if ( right
> GetColRight(m_numCols
-1) ||
5240 bottom
> GetRowBottom(m_numRows
-1) )
5243 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5245 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5246 dc
.SetPen( *wxTRANSPARENT_PEN
);
5248 if ( right
> GetColRight(m_numCols
-1) )
5249 dc
.DrawRectangle( GetColRight(m_numCols
-1), top
,
5250 right
- GetColRight(m_numCols
-1), ch
);
5252 if ( bottom
> GetRowBottom(m_numRows
-1) )
5253 dc
.DrawRectangle( left
, GetRowBottom(m_numRows
-1),
5254 cw
, bottom
- GetRowBottom(m_numRows
-1) );
5260 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5262 int row
= coords
.GetRow();
5263 int col
= coords
.GetCol();
5265 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5268 // we draw the cell border ourselves
5269 #if !WXGRID_DRAW_LINES
5270 if ( m_gridLinesEnabled
)
5271 DrawCellBorder( dc
, coords
);
5274 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5276 bool isCurrent
= coords
== m_currentCellCoords
;
5279 rect
.x
= GetColLeft(col
);
5280 rect
.y
= GetRowTop(row
);
5281 rect
.width
= GetColWidth(col
) - 1;
5282 rect
.height
= GetRowHeight(row
) - 1;
5284 // if the editor is shown, we should use it and not the renderer
5285 if ( isCurrent
&& IsCellEditControlEnabled() )
5287 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5288 editor
->PaintBackground(rect
, attr
);
5293 // but all the rest is drawn by the cell renderer and hence may be
5295 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5296 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5303 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5305 int row
= m_currentCellCoords
.GetRow();
5306 int col
= m_currentCellCoords
.GetCol();
5308 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5312 rect
.x
= GetColLeft(col
);
5313 rect
.y
= GetRowTop(row
);
5314 rect
.width
= GetColWidth(col
) - 1;
5315 rect
.height
= GetRowHeight(row
) - 1;
5317 // hmmm... what could we do here to show that the cell is disabled?
5318 // for now, I just draw a thinner border than for the other ones, but
5319 // it doesn't look really good
5320 dc
.SetPen(wxPen(m_gridLineColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5321 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5323 dc
.DrawRectangle(rect
);
5326 // VZ: my experiments with 3d borders...
5328 // how to properly set colours for arbitrary bg?
5329 wxCoord x1
= rect
.x
,
5331 x2
= rect
.x
+ rect
.width
-1,
5332 y2
= rect
.y
+ rect
.height
-1;
5334 dc
.SetPen(*wxWHITE_PEN
);
5335 dc
.DrawLine(x1
, y1
, x2
, y1
);
5336 dc
.DrawLine(x1
, y1
, x1
, y2
);
5338 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5339 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5341 dc
.SetPen(*wxBLACK_PEN
);
5342 dc
.DrawLine(x1
, y2
, x2
, y2
);
5343 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5348 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5350 int row
= coords
.GetRow();
5351 int col
= coords
.GetCol();
5352 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5355 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5357 // right hand border
5359 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5360 GetColRight(col
), GetRowBottom(row
) );
5364 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5365 GetColRight(col
), GetRowBottom(row
) );
5368 void wxGrid::DrawHighlight(wxDC
& dc
)
5370 if ( IsCellEditControlEnabled() )
5372 // don't show highlight when the edit control is shown
5376 // if the active cell was repainted, repaint its highlight too because it
5377 // might have been damaged by the grid lines
5378 size_t count
= m_cellsExposed
.GetCount();
5379 for ( size_t n
= 0; n
< count
; n
++ )
5381 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
5383 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5384 DrawCellHighlight(dc
, attr
);
5392 // TODO: remove this ???
5393 // This is used to redraw all grid lines e.g. when the grid line colour
5396 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& reg
)
5398 if ( !m_gridLinesEnabled
||
5400 !m_numCols
) return;
5402 int top
, bottom
, left
, right
;
5408 m_gridWin
->GetClientSize(&cw
, &ch
);
5410 // virtual coords of visible area
5412 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5413 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5418 reg
.GetBox(x
, y
, w
, h
);
5419 CalcUnscrolledPosition( x
, y
, &left
, &top
);
5420 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
5424 m_gridWin
->GetClientSize(&cw
, &ch
);
5425 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5426 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5429 // avoid drawing grid lines past the last row and col
5431 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
5432 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
5434 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5436 // horizontal grid lines
5439 for ( i
= 0; i
< m_numRows
; i
++ )
5441 int bot
= GetRowBottom(i
) - 1;
5450 dc
.DrawLine( left
, bot
, right
, bot
);
5455 // vertical grid lines
5457 for ( i
= 0; i
< m_numCols
; i
++ )
5459 int colRight
= GetColRight(i
) - 1;
5460 if ( colRight
> right
)
5465 if ( colRight
>= left
)
5467 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
5473 void wxGrid::DrawRowLabels( wxDC
& dc
)
5475 if ( !m_numRows
|| !m_numCols
) return;
5478 size_t numLabels
= m_rowLabelsExposed
.GetCount();
5480 for ( i
= 0; i
< numLabels
; i
++ )
5482 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
5487 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
5489 if ( GetRowHeight(row
) <= 0 )
5492 int rowTop
= GetRowTop(row
),
5493 rowBottom
= GetRowBottom(row
) - 1;
5495 dc
.SetPen( *wxBLACK_PEN
);
5496 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
5497 m_rowLabelWidth
-1, rowBottom
);
5499 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
5501 dc
.SetPen( *wxWHITE_PEN
);
5502 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
5503 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
5505 dc
.SetBackgroundMode( wxTRANSPARENT
);
5506 dc
.SetTextForeground( GetLabelTextColour() );
5507 dc
.SetFont( GetLabelFont() );
5510 GetRowLabelAlignment( &hAlign
, &vAlign
);
5514 rect
.SetY( GetRowTop(row
) + 2 );
5515 rect
.SetWidth( m_rowLabelWidth
- 4 );
5516 rect
.SetHeight( GetRowHeight(row
) - 4 );
5517 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
5521 void wxGrid::DrawColLabels( wxDC
& dc
)
5523 if ( !m_numRows
|| !m_numCols
) return;
5526 size_t numLabels
= m_colLabelsExposed
.GetCount();
5528 for ( i
= 0; i
< numLabels
; i
++ )
5530 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
5535 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
5537 if ( GetColWidth(col
) <= 0 )
5540 int colLeft
= GetColLeft(col
),
5541 colRight
= GetColRight(col
) - 1;
5543 dc
.SetPen( *wxBLACK_PEN
);
5544 dc
.DrawLine( colRight
, 0,
5545 colRight
, m_colLabelHeight
-1 );
5547 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
5548 colRight
, m_colLabelHeight
-1 );
5550 dc
.SetPen( *wxWHITE_PEN
);
5551 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
5552 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
5554 dc
.SetBackgroundMode( wxTRANSPARENT
);
5555 dc
.SetTextForeground( GetLabelTextColour() );
5556 dc
.SetFont( GetLabelFont() );
5558 dc
.SetBackgroundMode( wxTRANSPARENT
);
5559 dc
.SetTextForeground( GetLabelTextColour() );
5560 dc
.SetFont( GetLabelFont() );
5563 GetColLabelAlignment( &hAlign
, &vAlign
);
5566 rect
.SetX( colLeft
+ 2 );
5568 rect
.SetWidth( GetColWidth(col
) - 4 );
5569 rect
.SetHeight( m_colLabelHeight
- 4 );
5570 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
5574 void wxGrid::DrawTextRectangle( wxDC
& dc
,
5575 const wxString
& value
,
5580 long textWidth
, textHeight
;
5581 long lineWidth
, lineHeight
;
5582 wxArrayString lines
;
5584 dc
.SetClippingRegion( rect
);
5585 StringToLines( value
, lines
);
5586 if ( lines
.GetCount() )
5588 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
5589 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
5592 switch ( horizAlign
)
5595 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
5599 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
5608 switch ( vertAlign
)
5611 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
5615 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
5624 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
5626 dc
.DrawText( lines
[i
], (long)x
, (long)y
);
5631 dc
.DestroyClippingRegion();
5635 // Split multi line text up into an array of strings. Any existing
5636 // contents of the string array are preserved.
5638 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
5642 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
5643 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
5645 while ( startPos
< (int)tVal
.Length() )
5647 pos
= tVal
.Mid(startPos
).Find( eol
);
5652 else if ( pos
== 0 )
5654 lines
.Add( wxEmptyString
);
5658 lines
.Add( value
.Mid(startPos
, pos
) );
5662 if ( startPos
< (int)value
.Length() )
5664 lines
.Add( value
.Mid( startPos
) );
5669 void wxGrid::GetTextBoxSize( wxDC
& dc
,
5670 wxArrayString
& lines
,
5671 long *width
, long *height
)
5678 for ( i
= 0; i
< lines
.GetCount(); i
++ )
5680 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
5681 w
= wxMax( w
, lineW
);
5691 // ------ Edit control functions
5695 void wxGrid::EnableEditing( bool edit
)
5697 // TODO: improve this ?
5699 if ( edit
!= m_editable
)
5703 // FIXME IMHO this won't disable the edit control if edit == FALSE
5704 // because of the check in the beginning of
5705 // EnableCellEditControl() just below (VZ)
5706 EnableCellEditControl(m_editable
);
5711 void wxGrid::EnableCellEditControl( bool enable
)
5716 if ( m_currentCellCoords
== wxGridNoCellCoords
)
5717 SetCurrentCell( 0, 0 );
5719 if ( enable
!= m_cellEditCtrlEnabled
)
5721 // TODO allow the app to Veto() this event?
5722 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
5726 // this should be checked by the caller!
5727 wxASSERT_MSG( CanEnableCellControl(),
5728 _T("can't enable editing for this cell!") );
5730 // do it before ShowCellEditControl()
5731 m_cellEditCtrlEnabled
= enable
;
5733 ShowCellEditControl();
5737 HideCellEditControl();
5738 SaveEditControlValue();
5740 // do it after HideCellEditControl()
5741 m_cellEditCtrlEnabled
= enable
;
5746 bool wxGrid::IsCurrentCellReadOnly() const
5749 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
5750 bool readonly
= attr
->IsReadOnly();
5756 bool wxGrid::CanEnableCellControl() const
5758 return m_editable
&& !IsCurrentCellReadOnly();
5761 bool wxGrid::IsCellEditControlEnabled() const
5763 // the cell edit control might be disable for all cells or just for the
5764 // current one if it's read only
5765 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
5768 void wxGrid::ShowCellEditControl()
5770 if ( IsCellEditControlEnabled() )
5772 if ( !IsVisible( m_currentCellCoords
) )
5778 wxRect rect
= CellToRect( m_currentCellCoords
);
5779 int row
= m_currentCellCoords
.GetRow();
5780 int col
= m_currentCellCoords
.GetCol();
5782 // convert to scrolled coords
5784 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
5786 // done in PaintBackground()
5788 // erase the highlight and the cell contents because the editor
5789 // might not cover the entire cell
5790 wxClientDC
dc( m_gridWin
);
5792 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
5793 dc
.SetPen(*wxTRANSPARENT_PEN
);
5794 dc
.DrawRectangle(rect
);
5797 // cell is shifted by one pixel
5801 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5802 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5803 if ( !editor
->IsCreated() )
5805 editor
->Create(m_gridWin
, -1,
5806 new wxGridCellEditorEvtHandler(this, editor
));
5809 editor
->Show( TRUE
, attr
);
5811 editor
->SetSize( rect
);
5813 editor
->BeginEdit(row
, col
, this);
5822 void wxGrid::HideCellEditControl()
5824 if ( IsCellEditControlEnabled() )
5826 int row
= m_currentCellCoords
.GetRow();
5827 int col
= m_currentCellCoords
.GetCol();
5829 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5830 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5831 editor
->Show( FALSE
);
5834 m_gridWin
->SetFocus();
5839 void wxGrid::SaveEditControlValue()
5841 if ( IsCellEditControlEnabled() )
5843 int row
= m_currentCellCoords
.GetRow();
5844 int col
= m_currentCellCoords
.GetCol();
5846 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5847 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
5848 bool changed
= editor
->EndEdit(row
, col
, this);
5855 SendEvent( wxEVT_GRID_CELL_CHANGE
,
5856 m_currentCellCoords
.GetRow(),
5857 m_currentCellCoords
.GetCol() );
5864 // ------ Grid location functions
5865 // Note that all of these functions work with the logical coordinates of
5866 // grid cells and labels so you will need to convert from device
5867 // coordinates for mouse events etc.
5870 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
5872 int row
= YToRow(y
);
5873 int col
= XToCol(x
);
5875 if ( row
== -1 || col
== -1 )
5877 coords
= wxGridNoCellCoords
;
5881 coords
.Set( row
, col
);
5886 int wxGrid::YToRow( int y
)
5890 for ( i
= 0; i
< m_numRows
; i
++ )
5892 if ( y
< GetRowBottom(i
) )
5900 int wxGrid::XToCol( int x
)
5904 for ( i
= 0; i
< m_numCols
; i
++ )
5906 if ( x
< GetColRight(i
) )
5914 // return the row number that that the y coord is near the edge of, or
5915 // -1 if not near an edge
5917 int wxGrid::YToEdgeOfRow( int y
)
5921 for ( i
= 0; i
< m_numRows
; i
++ )
5923 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
5925 d
= abs( y
- GetRowBottom(i
) );
5926 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5935 // return the col number that that the x coord is near the edge of, or
5936 // -1 if not near an edge
5938 int wxGrid::XToEdgeOfCol( int x
)
5942 for ( i
= 0; i
< m_numCols
; i
++ )
5944 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
5946 d
= abs( x
- GetColRight(i
) );
5947 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
5956 wxRect
wxGrid::CellToRect( int row
, int col
)
5958 wxRect
rect( -1, -1, -1, -1 );
5960 if ( row
>= 0 && row
< m_numRows
&&
5961 col
>= 0 && col
< m_numCols
)
5963 rect
.x
= GetColLeft(col
);
5964 rect
.y
= GetRowTop(row
);
5965 rect
.width
= GetColWidth(col
);
5966 rect
.height
= GetRowHeight(row
);
5973 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
5975 // get the cell rectangle in logical coords
5977 wxRect
r( CellToRect( row
, col
) );
5979 // convert to device coords
5981 int left
, top
, right
, bottom
;
5982 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5983 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5985 // check against the client area of the grid window
5988 m_gridWin
->GetClientSize( &cw
, &ch
);
5990 if ( wholeCellVisible
)
5992 // is the cell wholly visible ?
5994 return ( left
>= 0 && right
<= cw
&&
5995 top
>= 0 && bottom
<= ch
);
5999 // is the cell partly visible ?
6001 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6002 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6007 // make the specified cell location visible by doing a minimal amount
6010 void wxGrid::MakeCellVisible( int row
, int col
)
6013 int xpos
= -1, ypos
= -1;
6015 if ( row
>= 0 && row
< m_numRows
&&
6016 col
>= 0 && col
< m_numCols
)
6018 // get the cell rectangle in logical coords
6020 wxRect
r( CellToRect( row
, col
) );
6022 // convert to device coords
6024 int left
, top
, right
, bottom
;
6025 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6026 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6029 m_gridWin
->GetClientSize( &cw
, &ch
);
6035 else if ( bottom
> ch
)
6037 int h
= r
.GetHeight();
6039 for ( i
= row
-1; i
>= 0; i
-- )
6041 int rowHeight
= GetRowHeight(i
);
6042 if ( h
+ rowHeight
> ch
)
6049 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6050 // have rounding errors (this is important, because if we do, we
6051 // might not scroll at all and some cells won't be redrawn)
6052 ypos
+= GRID_SCROLL_LINE
/ 2;
6059 else if ( right
> cw
)
6061 int w
= r
.GetWidth();
6063 for ( i
= col
-1; i
>= 0; i
-- )
6065 int colWidth
= GetColWidth(i
);
6066 if ( w
+ colWidth
> cw
)
6073 // see comment for ypos above
6074 xpos
+= GRID_SCROLL_LINE
/ 2;
6077 if ( xpos
!= -1 || ypos
!= -1 )
6079 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6080 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6081 Scroll( xpos
, ypos
);
6089 // ------ Grid cursor movement functions
6092 bool wxGrid::MoveCursorUp()
6094 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6095 m_currentCellCoords
.GetRow() > 0 )
6097 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6098 m_currentCellCoords
.GetCol() );
6100 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6101 m_currentCellCoords
.GetCol() );
6110 bool wxGrid::MoveCursorDown()
6112 // TODO: allow for scrolling
6114 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6115 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6117 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6118 m_currentCellCoords
.GetCol() );
6120 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6121 m_currentCellCoords
.GetCol() );
6130 bool wxGrid::MoveCursorLeft()
6132 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6133 m_currentCellCoords
.GetCol() > 0 )
6135 MakeCellVisible( m_currentCellCoords
.GetRow(),
6136 m_currentCellCoords
.GetCol() - 1 );
6138 SetCurrentCell( m_currentCellCoords
.GetRow(),
6139 m_currentCellCoords
.GetCol() - 1 );
6148 bool wxGrid::MoveCursorRight()
6150 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6151 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6153 MakeCellVisible( m_currentCellCoords
.GetRow(),
6154 m_currentCellCoords
.GetCol() + 1 );
6156 SetCurrentCell( m_currentCellCoords
.GetRow(),
6157 m_currentCellCoords
.GetCol() + 1 );
6166 bool wxGrid::MovePageUp()
6168 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6170 int row
= m_currentCellCoords
.GetRow();
6174 m_gridWin
->GetClientSize( &cw
, &ch
);
6176 int y
= GetRowTop(row
);
6177 int newRow
= YToRow( y
- ch
+ 1 );
6182 else if ( newRow
== row
)
6187 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6188 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6196 bool wxGrid::MovePageDown()
6198 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6200 int row
= m_currentCellCoords
.GetRow();
6201 if ( row
< m_numRows
)
6204 m_gridWin
->GetClientSize( &cw
, &ch
);
6206 int y
= GetRowTop(row
);
6207 int newRow
= YToRow( y
+ ch
);
6210 newRow
= m_numRows
- 1;
6212 else if ( newRow
== row
)
6217 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6218 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6226 bool wxGrid::MoveCursorUpBlock()
6229 m_currentCellCoords
!= wxGridNoCellCoords
&&
6230 m_currentCellCoords
.GetRow() > 0 )
6232 int row
= m_currentCellCoords
.GetRow();
6233 int col
= m_currentCellCoords
.GetCol();
6235 if ( m_table
->IsEmptyCell(row
, col
) )
6237 // starting in an empty cell: find the next block of
6243 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6246 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6248 // starting at the top of a block: find the next block
6254 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6259 // starting within a block: find the top of the block
6264 if ( m_table
->IsEmptyCell(row
, col
) )
6272 MakeCellVisible( row
, col
);
6273 SetCurrentCell( row
, col
);
6281 bool wxGrid::MoveCursorDownBlock()
6284 m_currentCellCoords
!= wxGridNoCellCoords
&&
6285 m_currentCellCoords
.GetRow() < m_numRows
-1 )
6287 int row
= m_currentCellCoords
.GetRow();
6288 int col
= m_currentCellCoords
.GetCol();
6290 if ( m_table
->IsEmptyCell(row
, col
) )
6292 // starting in an empty cell: find the next block of
6295 while ( row
< m_numRows
-1 )
6298 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6301 else if ( m_table
->IsEmptyCell(row
+1, col
) )
6303 // starting at the bottom of a block: find the next block
6306 while ( row
< m_numRows
-1 )
6309 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6314 // starting within a block: find the bottom of the block
6316 while ( row
< m_numRows
-1 )
6319 if ( m_table
->IsEmptyCell(row
, col
) )
6327 MakeCellVisible( row
, col
);
6328 SetCurrentCell( row
, col
);
6336 bool wxGrid::MoveCursorLeftBlock()
6339 m_currentCellCoords
!= wxGridNoCellCoords
&&
6340 m_currentCellCoords
.GetCol() > 0 )
6342 int row
= m_currentCellCoords
.GetRow();
6343 int col
= m_currentCellCoords
.GetCol();
6345 if ( m_table
->IsEmptyCell(row
, col
) )
6347 // starting in an empty cell: find the next block of
6353 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6356 else if ( m_table
->IsEmptyCell(row
, col
-1) )
6358 // starting at the left of a block: find the next block
6364 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6369 // starting within a block: find the left of the block
6374 if ( m_table
->IsEmptyCell(row
, col
) )
6382 MakeCellVisible( row
, col
);
6383 SetCurrentCell( row
, col
);
6391 bool wxGrid::MoveCursorRightBlock()
6394 m_currentCellCoords
!= wxGridNoCellCoords
&&
6395 m_currentCellCoords
.GetCol() < m_numCols
-1 )
6397 int row
= m_currentCellCoords
.GetRow();
6398 int col
= m_currentCellCoords
.GetCol();
6400 if ( m_table
->IsEmptyCell(row
, col
) )
6402 // starting in an empty cell: find the next block of
6405 while ( col
< m_numCols
-1 )
6408 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6411 else if ( m_table
->IsEmptyCell(row
, col
+1) )
6413 // starting at the right of a block: find the next block
6416 while ( col
< m_numCols
-1 )
6419 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6424 // starting within a block: find the right of the block
6426 while ( col
< m_numCols
-1 )
6429 if ( m_table
->IsEmptyCell(row
, col
) )
6437 MakeCellVisible( row
, col
);
6438 SetCurrentCell( row
, col
);
6449 // ------ Label values and formatting
6452 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
6454 *horiz
= m_rowLabelHorizAlign
;
6455 *vert
= m_rowLabelVertAlign
;
6458 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
6460 *horiz
= m_colLabelHorizAlign
;
6461 *vert
= m_colLabelVertAlign
;
6464 wxString
wxGrid::GetRowLabelValue( int row
)
6468 return m_table
->GetRowLabelValue( row
);
6478 wxString
wxGrid::GetColLabelValue( int col
)
6482 return m_table
->GetColLabelValue( col
);
6493 void wxGrid::SetRowLabelSize( int width
)
6495 width
= wxMax( width
, 0 );
6496 if ( width
!= m_rowLabelWidth
)
6500 m_rowLabelWin
->Show( FALSE
);
6501 m_cornerLabelWin
->Show( FALSE
);
6503 else if ( m_rowLabelWidth
== 0 )
6505 m_rowLabelWin
->Show( TRUE
);
6506 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6509 m_rowLabelWidth
= width
;
6516 void wxGrid::SetColLabelSize( int height
)
6518 height
= wxMax( height
, 0 );
6519 if ( height
!= m_colLabelHeight
)
6523 m_colLabelWin
->Show( FALSE
);
6524 m_cornerLabelWin
->Show( FALSE
);
6526 else if ( m_colLabelHeight
== 0 )
6528 m_colLabelWin
->Show( TRUE
);
6529 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
6532 m_colLabelHeight
= height
;
6539 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
6541 if ( m_labelBackgroundColour
!= colour
)
6543 m_labelBackgroundColour
= colour
;
6544 m_rowLabelWin
->SetBackgroundColour( colour
);
6545 m_colLabelWin
->SetBackgroundColour( colour
);
6546 m_cornerLabelWin
->SetBackgroundColour( colour
);
6548 if ( !GetBatchCount() )
6550 m_rowLabelWin
->Refresh();
6551 m_colLabelWin
->Refresh();
6552 m_cornerLabelWin
->Refresh();
6557 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
6559 if ( m_labelTextColour
!= colour
)
6561 m_labelTextColour
= colour
;
6562 if ( !GetBatchCount() )
6564 m_rowLabelWin
->Refresh();
6565 m_colLabelWin
->Refresh();
6570 void wxGrid::SetLabelFont( const wxFont
& font
)
6573 if ( !GetBatchCount() )
6575 m_rowLabelWin
->Refresh();
6576 m_colLabelWin
->Refresh();
6580 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
6582 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6584 m_rowLabelHorizAlign
= horiz
;
6587 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6589 m_rowLabelVertAlign
= vert
;
6592 if ( !GetBatchCount() )
6594 m_rowLabelWin
->Refresh();
6598 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
6600 if ( horiz
== wxLEFT
|| horiz
== wxCENTRE
|| horiz
== wxRIGHT
)
6602 m_colLabelHorizAlign
= horiz
;
6605 if ( vert
== wxTOP
|| vert
== wxCENTRE
|| vert
== wxBOTTOM
)
6607 m_colLabelVertAlign
= vert
;
6610 if ( !GetBatchCount() )
6612 m_colLabelWin
->Refresh();
6616 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
6620 m_table
->SetRowLabelValue( row
, s
);
6621 if ( !GetBatchCount() )
6623 wxRect rect
= CellToRect( row
, 0);
6624 if ( rect
.height
> 0 )
6626 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
6628 rect
.width
= m_rowLabelWidth
;
6629 m_rowLabelWin
->Refresh( TRUE
, &rect
);
6635 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
6639 m_table
->SetColLabelValue( col
, s
);
6640 if ( !GetBatchCount() )
6642 wxRect rect
= CellToRect( 0, col
);
6643 if ( rect
.width
> 0 )
6645 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
6647 rect
.height
= m_colLabelHeight
;
6648 m_colLabelWin
->Refresh( TRUE
, &rect
);
6654 void wxGrid::SetGridLineColour( const wxColour
& colour
)
6656 if ( m_gridLineColour
!= colour
)
6658 m_gridLineColour
= colour
;
6660 wxClientDC
dc( m_gridWin
);
6662 DrawAllGridLines( dc
, wxRegion() );
6666 void wxGrid::EnableGridLines( bool enable
)
6668 if ( enable
!= m_gridLinesEnabled
)
6670 m_gridLinesEnabled
= enable
;
6672 if ( !GetBatchCount() )
6676 wxClientDC
dc( m_gridWin
);
6678 DrawAllGridLines( dc
, wxRegion() );
6682 m_gridWin
->Refresh();
6689 int wxGrid::GetDefaultRowSize()
6691 return m_defaultRowHeight
;
6694 int wxGrid::GetRowSize( int row
)
6696 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
6698 return GetRowHeight(row
);
6701 int wxGrid::GetDefaultColSize()
6703 return m_defaultColWidth
;
6706 int wxGrid::GetColSize( int col
)
6708 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
6710 return GetColWidth(col
);
6713 // ============================================================================
6714 // access to the grid attributes: each of them has a default value in the grid
6715 // itself and may be overidden on a per-cell basis
6716 // ============================================================================
6718 // ----------------------------------------------------------------------------
6719 // setting default attributes
6720 // ----------------------------------------------------------------------------
6722 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
6724 m_defaultCellAttr
->SetBackgroundColour(col
);
6726 m_gridWin
->SetBackgroundColour(col
);
6730 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
6732 m_defaultCellAttr
->SetTextColour(col
);
6735 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
6737 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
6740 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
6742 m_defaultCellAttr
->SetFont(font
);
6745 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
6747 m_defaultCellAttr
->SetRenderer(renderer
);
6750 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
6752 m_defaultCellAttr
->SetEditor(editor
);
6755 // ----------------------------------------------------------------------------
6756 // access to the default attrbiutes
6757 // ----------------------------------------------------------------------------
6759 wxColour
wxGrid::GetDefaultCellBackgroundColour()
6761 return m_defaultCellAttr
->GetBackgroundColour();
6764 wxColour
wxGrid::GetDefaultCellTextColour()
6766 return m_defaultCellAttr
->GetTextColour();
6769 wxFont
wxGrid::GetDefaultCellFont()
6771 return m_defaultCellAttr
->GetFont();
6774 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
6776 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
6779 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
6781 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
6784 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
6786 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
6789 // ----------------------------------------------------------------------------
6790 // access to cell attributes
6791 // ----------------------------------------------------------------------------
6793 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
6795 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6796 wxColour colour
= attr
->GetBackgroundColour();
6801 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
6803 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6804 wxColour colour
= attr
->GetTextColour();
6809 wxFont
wxGrid::GetCellFont( int row
, int col
)
6811 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6812 wxFont font
= attr
->GetFont();
6817 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
6819 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6820 attr
->GetAlignment(horiz
, vert
);
6824 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
6826 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6827 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
6833 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
6835 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6836 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6842 bool wxGrid::IsReadOnly(int row
, int col
) const
6844 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6845 bool isReadOnly
= attr
->IsReadOnly();
6850 // ----------------------------------------------------------------------------
6851 // attribute support: cache, automatic provider creation, ...
6852 // ----------------------------------------------------------------------------
6854 bool wxGrid::CanHaveAttributes()
6861 return m_table
->CanHaveAttributes();
6864 void wxGrid::ClearAttrCache()
6866 if ( m_attrCache
.row
!= -1 )
6868 wxSafeDecRef(m_attrCache
.attr
);
6869 m_attrCache
.row
= -1;
6873 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
6875 wxGrid
*self
= (wxGrid
*)this; // const_cast
6877 self
->ClearAttrCache();
6878 self
->m_attrCache
.row
= row
;
6879 self
->m_attrCache
.col
= col
;
6880 self
->m_attrCache
.attr
= attr
;
6884 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
6886 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
6888 *attr
= m_attrCache
.attr
;
6889 wxSafeIncRef(m_attrCache
.attr
);
6891 #ifdef DEBUG_ATTR_CACHE
6892 gs_nAttrCacheHits
++;
6899 #ifdef DEBUG_ATTR_CACHE
6900 gs_nAttrCacheMisses
++;
6906 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
6908 wxGridCellAttr
*attr
;
6909 if ( !LookupAttr(row
, col
, &attr
) )
6911 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
6912 CacheAttr(row
, col
, attr
);
6916 attr
->SetDefAttr(m_defaultCellAttr
);
6920 attr
= m_defaultCellAttr
;
6927 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
6929 wxGridCellAttr
*attr
;
6930 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
6932 wxASSERT_MSG( m_table
,
6933 _T("we may only be called if CanHaveAttributes() "
6934 "returned TRUE and then m_table should be !NULL") );
6936 attr
= m_table
->GetAttr(row
, col
);
6939 attr
= new wxGridCellAttr
;
6941 // artificially inc the ref count to match DecRef() in caller
6944 m_table
->SetAttr(attr
, row
, col
);
6947 CacheAttr(row
, col
, attr
);
6949 attr
->SetDefAttr(m_defaultCellAttr
);
6953 // ----------------------------------------------------------------------------
6954 // setting column attributes (wrappers around SetColAttr)
6955 // ----------------------------------------------------------------------------
6957 void wxGrid::SetColFormatBool(int col
)
6959 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
6962 void wxGrid::SetColFormatNumber(int col
)
6964 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
6967 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
6969 wxString typeName
= wxGRID_VALUE_FLOAT
;
6970 if ( (width
!= -1) || (precision
!= -1) )
6972 typeName
<< _T(':') << width
<< _T(',') << precision
;
6975 SetColFormatCustom(col
, typeName
);
6978 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
6980 wxGridCellAttr
*attr
= new wxGridCellAttr
;
6981 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
6982 attr
->SetRenderer(renderer
);
6984 SetColAttr(col
, attr
);
6987 // ----------------------------------------------------------------------------
6988 // setting cell attributes: this is forwarded to the table
6989 // ----------------------------------------------------------------------------
6991 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
6993 if ( CanHaveAttributes() )
6995 m_table
->SetRowAttr(attr
, row
);
7003 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7005 if ( CanHaveAttributes() )
7007 m_table
->SetColAttr(attr
, col
);
7015 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7017 if ( CanHaveAttributes() )
7019 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7020 attr
->SetBackgroundColour(colour
);
7025 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7027 if ( CanHaveAttributes() )
7029 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7030 attr
->SetTextColour(colour
);
7035 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7037 if ( CanHaveAttributes() )
7039 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7040 attr
->SetFont(font
);
7045 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7047 if ( CanHaveAttributes() )
7049 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7050 attr
->SetAlignment(horiz
, vert
);
7055 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7057 if ( CanHaveAttributes() )
7059 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7060 attr
->SetRenderer(renderer
);
7065 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7067 if ( CanHaveAttributes() )
7069 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7070 attr
->SetEditor(editor
);
7075 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7077 if ( CanHaveAttributes() )
7079 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7080 attr
->SetReadOnly(isReadOnly
);
7085 // ----------------------------------------------------------------------------
7086 // Data type registration
7087 // ----------------------------------------------------------------------------
7089 void wxGrid::RegisterDataType(const wxString
& typeName
,
7090 wxGridCellRenderer
* renderer
,
7091 wxGridCellEditor
* editor
)
7093 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7097 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7099 wxString typeName
= m_table
->GetTypeName(row
, col
);
7100 return GetDefaultEditorForType(typeName
);
7103 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7105 wxString typeName
= m_table
->GetTypeName(row
, col
);
7106 return GetDefaultRendererForType(typeName
);
7110 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7112 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7113 if ( index
== wxNOT_FOUND
)
7115 wxFAIL_MSG(wxT("Unknown data type name"));
7120 return m_typeRegistry
->GetEditor(index
);
7124 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7126 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7127 if ( index
== wxNOT_FOUND
)
7129 wxFAIL_MSG(wxT("Unknown data type name"));
7134 return m_typeRegistry
->GetRenderer(index
);
7138 // ----------------------------------------------------------------------------
7140 // ----------------------------------------------------------------------------
7142 void wxGrid::EnableDragRowSize( bool enable
)
7144 m_canDragRowSize
= enable
;
7148 void wxGrid::EnableDragColSize( bool enable
)
7150 m_canDragColSize
= enable
;
7153 void wxGrid::EnableDragGridSize( bool enable
)
7155 m_canDragGridSize
= enable
;
7159 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7161 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7163 if ( resizeExistingRows
)
7171 void wxGrid::SetRowSize( int row
, int height
)
7173 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
7175 if ( m_rowHeights
.IsEmpty() )
7177 // need to really create the array
7181 int h
= wxMax( 0, height
);
7182 int diff
= h
- m_rowHeights
[row
];
7184 m_rowHeights
[row
] = h
;
7186 for ( i
= row
; i
< m_numRows
; i
++ )
7188 m_rowBottoms
[i
] += diff
;
7193 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
7195 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
7197 if ( resizeExistingCols
)
7205 void wxGrid::SetColSize( int col
, int width
)
7207 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
7209 // should we check that it's bigger than GetColMinimalWidth(col) here?
7211 if ( m_colWidths
.IsEmpty() )
7213 // need to really create the array
7217 int w
= wxMax( 0, width
);
7218 int diff
= w
- m_colWidths
[col
];
7219 m_colWidths
[col
] = w
;
7222 for ( i
= col
; i
< m_numCols
; i
++ )
7224 m_colRights
[i
] += diff
;
7230 void wxGrid::SetColMinimalWidth( int col
, int width
)
7232 m_colMinWidths
.Put(col
, width
);
7235 void wxGrid::SetRowMinimalHeight( int row
, int width
)
7237 m_rowMinHeights
.Put(row
, width
);
7240 int wxGrid::GetColMinimalWidth(int col
) const
7242 long value
= m_colMinWidths
.Get(col
);
7243 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
7246 int wxGrid::GetRowMinimalHeight(int row
) const
7248 long value
= m_rowMinHeights
.Get(row
);
7249 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
7252 // ----------------------------------------------------------------------------
7254 // ----------------------------------------------------------------------------
7256 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
7258 wxClientDC
dc(m_gridWin
);
7260 // init both of them to avoid compiler warnings, even if weo nly need one
7268 wxCoord extent
, extentMax
= 0;
7269 int max
= column
? m_numRows
: m_numCols
;
7270 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7277 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7278 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7281 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7282 extent
= column
? size
.x
: size
.y
;
7283 if ( extent
> extentMax
)
7294 // now also compare with the column label extent
7296 dc
.SetFont( GetLabelFont() );
7299 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
7301 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
7303 extent
= column
? w
: h
;
7304 if ( extent
> extentMax
)
7311 // empty column - give default extent (notice that if extentMax is less
7312 // than default extent but != 0, it's ok)
7313 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7319 // leave some space around text
7325 SetColSize(col
, extentMax
);
7327 SetRowSize(row
, extentMax
);
7332 SetColMinimalWidth(col
, extentMax
);
7334 SetRowMinimalHeight(row
, extentMax
);
7338 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7340 int width
= m_rowLabelWidth
;
7342 for ( int col
= 0; col
< m_numCols
; col
++ )
7346 AutoSizeColumn(col
, setAsMin
);
7349 width
+= GetColWidth(col
);
7355 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7357 int height
= m_colLabelHeight
;
7359 for ( int row
= 0; row
< m_numRows
; row
++ )
7363 AutoSizeRow(row
, setAsMin
);
7366 height
+= GetRowHeight(row
);
7372 void wxGrid::AutoSize()
7375 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7378 wxSize
wxGrid::DoGetBestSize() const
7380 // don't set sizes, only calculate them
7381 wxGrid
*self
= (wxGrid
*)this; // const_cast
7383 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7384 self
->SetOrCalcRowSizes(TRUE
));
7392 // ----------------------------------------------------------------------------
7393 // cell value accessor functions
7394 // ----------------------------------------------------------------------------
7396 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7400 m_table
->SetValue( row
, col
, s
.c_str() );
7401 if ( !GetBatchCount() )
7403 wxClientDC
dc( m_gridWin
);
7405 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7408 if ( m_currentCellCoords
.GetRow() == row
&&
7409 m_currentCellCoords
.GetCol() == col
&&
7410 IsCellEditControlEnabled())
7412 HideCellEditControl();
7413 ShowCellEditControl(); // will reread data from table
7420 // ------ Block, row and col selection
7423 void wxGrid::SelectRow( int row
, bool addToSelected
)
7427 if ( IsSelection() && addToSelected
)
7430 bool need_refresh
[4];
7434 need_refresh
[3] = FALSE
;
7438 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7439 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7440 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7441 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7445 need_refresh
[0] = TRUE
;
7446 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7447 wxGridCellCoords ( oldTop
- 1,
7449 m_selectedTopLeft
.SetRow( row
);
7454 need_refresh
[1] = TRUE
;
7455 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7456 wxGridCellCoords ( oldBottom
,
7459 m_selectedTopLeft
.SetCol( 0 );
7462 if ( oldBottom
< row
)
7464 need_refresh
[2] = TRUE
;
7465 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7466 wxGridCellCoords ( row
,
7468 m_selectedBottomRight
.SetRow( row
);
7471 if ( oldRight
< m_numCols
- 1 )
7473 need_refresh
[3] = TRUE
;
7474 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7476 wxGridCellCoords ( oldBottom
,
7478 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7481 for (i
= 0; i
< 4; i
++ )
7482 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7483 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7487 r
= SelectionToDeviceRect();
7489 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7491 m_selectedTopLeft
.Set( row
, 0 );
7492 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7493 r
= SelectionToDeviceRect();
7494 m_gridWin
->Refresh( FALSE
, &r
);
7497 wxGridRangeSelectEvent
gridEvt( GetId(),
7498 wxEVT_GRID_RANGE_SELECT
,
7501 m_selectedBottomRight
);
7503 GetEventHandler()->ProcessEvent(gridEvt
);
7507 void wxGrid::SelectCol( int col
, bool addToSelected
)
7509 if ( IsSelection() && addToSelected
)
7512 bool need_refresh
[4];
7516 need_refresh
[3] = FALSE
;
7519 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7520 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7521 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7522 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7524 if ( oldLeft
> col
)
7526 need_refresh
[0] = TRUE
;
7527 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7528 wxGridCellCoords ( m_numRows
- 1,
7530 m_selectedTopLeft
.SetCol( col
);
7535 need_refresh
[1] = TRUE
;
7536 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7537 wxGridCellCoords ( oldTop
- 1,
7539 m_selectedTopLeft
.SetRow( 0 );
7542 if ( oldRight
< col
)
7544 need_refresh
[2] = TRUE
;
7545 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7546 wxGridCellCoords ( m_numRows
- 1,
7548 m_selectedBottomRight
.SetCol( col
);
7551 if ( oldBottom
< m_numRows
- 1 )
7553 need_refresh
[3] = TRUE
;
7554 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7556 wxGridCellCoords ( m_numRows
- 1,
7558 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7561 for (i
= 0; i
< 4; i
++ )
7562 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7563 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7569 r
= SelectionToDeviceRect();
7571 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7573 m_selectedTopLeft
.Set( 0, col
);
7574 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7575 r
= SelectionToDeviceRect();
7576 m_gridWin
->Refresh( FALSE
, &r
);
7579 wxGridRangeSelectEvent
gridEvt( GetId(),
7580 wxEVT_GRID_RANGE_SELECT
,
7583 m_selectedBottomRight
);
7585 GetEventHandler()->ProcessEvent(gridEvt
);
7589 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7592 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7594 if ( topRow
> bottomRow
)
7601 if ( leftCol
> rightCol
)
7608 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7609 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7611 if ( m_selectedTopLeft
!= updateTopLeft
||
7612 m_selectedBottomRight
!= updateBottomRight
)
7614 // Compute two optimal update rectangles:
7615 // Either one rectangle is a real subset of the
7616 // other, or they are (almost) disjoint!
7618 bool need_refresh
[4];
7622 need_refresh
[3] = FALSE
;
7625 // Store intermediate values
7626 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7627 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7628 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7629 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7631 // Determine the outer/inner coordinates.
7632 if (oldLeft
> leftCol
)
7638 if (oldTop
> topRow
)
7644 if (oldRight
< rightCol
)
7647 oldRight
= rightCol
;
7650 if (oldBottom
< bottomRow
)
7653 oldBottom
= bottomRow
;
7657 // Now, either the stuff marked old is the outer
7658 // rectangle or we don't have a situation where one
7659 // is contained in the other.
7661 if ( oldLeft
< leftCol
)
7663 need_refresh
[0] = TRUE
;
7664 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7666 wxGridCellCoords ( oldBottom
,
7670 if ( oldTop
< topRow
)
7672 need_refresh
[1] = TRUE
;
7673 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7675 wxGridCellCoords ( topRow
- 1,
7679 if ( oldRight
> rightCol
)
7681 need_refresh
[2] = TRUE
;
7682 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7684 wxGridCellCoords ( oldBottom
,
7688 if ( oldBottom
> bottomRow
)
7690 need_refresh
[3] = TRUE
;
7691 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7693 wxGridCellCoords ( oldBottom
,
7699 m_selectedTopLeft
= updateTopLeft
;
7700 m_selectedBottomRight
= updateBottomRight
;
7702 // various Refresh() calls
7703 for (i
= 0; i
< 4; i
++ )
7704 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7705 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7708 // only generate an event if the block is not being selected by
7709 // dragging the mouse (in which case the event will be generated in
7710 // the mouse event handler)
7711 if ( !m_isDragging
)
7713 wxGridRangeSelectEvent
gridEvt( GetId(),
7714 wxEVT_GRID_RANGE_SELECT
,
7717 m_selectedBottomRight
);
7719 GetEventHandler()->ProcessEvent(gridEvt
);
7723 void wxGrid::SelectAll()
7725 m_selectedTopLeft
.Set( 0, 0 );
7726 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7728 m_gridWin
->Refresh();
7732 void wxGrid::ClearSelection()
7734 m_selectedTopLeft
= wxGridNoCellCoords
;
7735 m_selectedBottomRight
= wxGridNoCellCoords
;
7739 // This function returns the rectangle that encloses the given block
7740 // in device coords clipped to the client size of the grid window.
7742 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7743 const wxGridCellCoords
&bottomRight
)
7745 wxRect
rect( wxGridNoCellRect
);
7748 cellRect
= CellToRect( topLeft
);
7749 if ( cellRect
!= wxGridNoCellRect
)
7755 rect
= wxRect( 0, 0, 0, 0 );
7758 cellRect
= CellToRect( bottomRight
);
7759 if ( cellRect
!= wxGridNoCellRect
)
7765 return wxGridNoCellRect
;
7768 // convert to scrolled coords
7770 int left
, top
, right
, bottom
;
7771 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7772 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7775 m_gridWin
->GetClientSize( &cw
, &ch
);
7777 rect
.SetLeft( wxMax(0, left
) );
7778 rect
.SetTop( wxMax(0, top
) );
7779 rect
.SetRight( wxMin(cw
, right
) );
7780 rect
.SetBottom( wxMin(ch
, bottom
) );
7788 // ------ Grid event classes
7791 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7793 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7794 int row
, int col
, int x
, int y
,
7795 bool control
, bool shift
, bool alt
, bool meta
)
7796 : wxNotifyEvent( type
, id
)
7802 m_control
= control
;
7807 SetEventObject(obj
);
7811 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7813 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7814 int rowOrCol
, int x
, int y
,
7815 bool control
, bool shift
, bool alt
, bool meta
)
7816 : wxNotifyEvent( type
, id
)
7818 m_rowOrCol
= rowOrCol
;
7821 m_control
= control
;
7826 SetEventObject(obj
);
7830 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7832 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7833 const wxGridCellCoords
& topLeft
,
7834 const wxGridCellCoords
& bottomRight
,
7835 bool control
, bool shift
, bool alt
, bool meta
)
7836 : wxNotifyEvent( type
, id
)
7838 m_topLeft
= topLeft
;
7839 m_bottomRight
= bottomRight
;
7840 m_control
= control
;
7845 SetEventObject(obj
);
7849 #endif // ifndef wxUSE_NEW_GRID