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__)
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
1461 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 4;
1463 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1466 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1467 wxGridCellAttr
& WXUNUSED(attr
),
1472 // compute it only once (no locks for MT safeness in GUI thread...)
1473 if ( !ms_sizeCheckMark
.x
)
1475 // get checkbox size
1476 wxCoord checkSize
= 0;
1477 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1478 wxSize size
= checkbox
->GetBestSize();
1479 checkSize
= size
.y
+ wxGRID_CHECKMARK_MARGIN
;
1481 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1482 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1483 checkSize
-= size
.y
/ 2;
1488 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1491 return ms_sizeCheckMark
;
1494 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1495 wxGridCellAttr
& attr
,
1501 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1503 // draw a check mark in the centre (ignoring alignment - TODO)
1504 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1506 // don't draw outside the cell
1507 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1508 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1510 // and even leave (at least) 1 pixel margin
1511 size
.x
= size
.y
= minSize
- 2;
1514 // draw a border around checkmark
1516 rectMark
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1517 rectMark
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1518 rectMark
.width
= size
.x
;
1519 rectMark
.height
= size
.y
;
1521 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1522 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1523 dc
.DrawRectangle(rectMark
);
1525 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1528 // looks nicer under MSW
1533 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1534 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1536 value
= !!grid
.GetTable()->GetValue(row
, col
);
1540 dc
.SetTextForeground(attr
.GetTextColour());
1541 dc
.DrawCheckMark(rectMark
);
1545 // ----------------------------------------------------------------------------
1547 // ----------------------------------------------------------------------------
1549 wxGridCellAttr
*wxGridCellAttr::Clone() const
1551 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1552 if ( HasTextColour() )
1553 attr
->SetTextColour(GetTextColour());
1554 if ( HasBackgroundColour() )
1555 attr
->SetBackgroundColour(GetBackgroundColour());
1557 attr
->SetFont(GetFont());
1558 if ( HasAlignment() )
1559 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1563 attr
->SetRenderer(m_renderer
);
1564 m_renderer
->IncRef();
1568 attr
->SetEditor(m_editor
);
1573 attr
->SetReadOnly();
1575 attr
->SetDefAttr(m_defGridAttr
);
1580 const wxColour
& wxGridCellAttr::GetTextColour() const
1582 if (HasTextColour())
1586 else if (m_defGridAttr
!= this)
1588 return m_defGridAttr
->GetTextColour();
1592 wxFAIL_MSG(wxT("Missing default cell attribute"));
1593 return wxNullColour
;
1598 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1600 if (HasBackgroundColour())
1602 else if (m_defGridAttr
!= this)
1603 return m_defGridAttr
->GetBackgroundColour();
1606 wxFAIL_MSG(wxT("Missing default cell attribute"));
1607 return wxNullColour
;
1612 const wxFont
& wxGridCellAttr::GetFont() const
1616 else if (m_defGridAttr
!= this)
1617 return m_defGridAttr
->GetFont();
1620 wxFAIL_MSG(wxT("Missing default cell attribute"));
1626 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1630 if ( hAlign
) *hAlign
= m_hAlign
;
1631 if ( vAlign
) *vAlign
= m_vAlign
;
1633 else if (m_defGridAttr
!= this)
1634 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1637 wxFAIL_MSG(wxT("Missing default cell attribute"));
1642 // GetRenderer and GetEditor use a slightly different decision path about
1643 // which attribute to use. If a non-default attr object has one then it is
1644 // used, otherwise the default editor or renderer is fetched from the grid and
1645 // used. It should be the default for the data type of the cell. If it is
1646 // NULL (because the table has a type that the grid does not have in its
1647 // registry,) then the grid's default editor or renderer is used.
1649 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1651 wxGridCellRenderer
* renderer
= NULL
;
1653 if ( m_defGridAttr
!= this || grid
== NULL
)
1655 renderer
= m_renderer
; // use local attribute
1660 if ( !renderer
&& grid
) // get renderer for the data type
1662 // GetDefaultRendererForCell() will do IncRef() for us
1663 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1668 // if we still don't have one then use the grid default
1669 // (no need for IncRef() here neither)
1670 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1675 wxFAIL_MSG(wxT("Missing default cell attribute"));
1681 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1683 wxGridCellEditor
* editor
= NULL
;
1685 if ( m_defGridAttr
!= this || grid
== NULL
)
1687 editor
= m_editor
; // use local attribute
1692 if ( grid
) // get renderer for the data type
1693 editor
= grid
->GetDefaultEditorForCell(row
, col
);
1696 // if we still don't have one then use the grid default
1697 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
1701 wxFAIL_MSG(wxT("Missing default cell attribute"));
1707 // ----------------------------------------------------------------------------
1708 // wxGridCellAttrData
1709 // ----------------------------------------------------------------------------
1711 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
1713 int n
= FindIndex(row
, col
);
1714 if ( n
== wxNOT_FOUND
)
1716 // add the attribute
1717 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
1723 // change the attribute
1724 m_attrs
[(size_t)n
].attr
= attr
;
1728 // remove this attribute
1729 m_attrs
.RemoveAt((size_t)n
);
1734 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
1736 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1738 int n
= FindIndex(row
, col
);
1739 if ( n
!= wxNOT_FOUND
)
1741 attr
= m_attrs
[(size_t)n
].attr
;
1748 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
1750 size_t count
= m_attrs
.GetCount();
1751 for ( size_t n
= 0; n
< count
; n
++ )
1753 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1754 wxCoord row
= coords
.GetRow();
1755 if ((size_t)row
>= pos
)
1759 // If rows inserted, include row counter where necessary
1760 coords
.SetRow(row
+ numRows
);
1762 else if (numRows
< 0)
1764 // If rows deleted ...
1765 if ((size_t)row
>= pos
- numRows
)
1767 // ...either decrement row counter (if row still exists)...
1768 coords
.SetRow(row
+ numRows
);
1772 // ...or remove the attribute
1773 m_attrs
.RemoveAt((size_t)n
);
1781 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
1783 size_t count
= m_attrs
.GetCount();
1784 for ( size_t n
= 0; n
< count
; n
++ )
1786 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1787 wxCoord col
= coords
.GetCol();
1788 if ( (size_t)col
>= pos
)
1792 // If rows inserted, include row counter where necessary
1793 coords
.SetCol(col
+ numCols
);
1795 else if (numCols
< 0)
1797 // If rows deleted ...
1798 if ((size_t)col
>= pos
- numCols
)
1800 // ...either decrement row counter (if row still exists)...
1801 coords
.SetCol(col
+ numCols
);
1805 // ...or remove the attribute
1806 m_attrs
.RemoveAt((size_t)n
);
1814 int wxGridCellAttrData::FindIndex(int row
, int col
) const
1816 size_t count
= m_attrs
.GetCount();
1817 for ( size_t n
= 0; n
< count
; n
++ )
1819 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
1820 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
1829 // ----------------------------------------------------------------------------
1830 // wxGridRowOrColAttrData
1831 // ----------------------------------------------------------------------------
1833 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
1835 size_t count
= m_attrs
.Count();
1836 for ( size_t n
= 0; n
< count
; n
++ )
1838 m_attrs
[n
]->DecRef();
1842 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
1844 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1846 int n
= m_rowsOrCols
.Index(rowOrCol
);
1847 if ( n
!= wxNOT_FOUND
)
1849 attr
= m_attrs
[(size_t)n
];
1856 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
1858 int n
= m_rowsOrCols
.Index(rowOrCol
);
1859 if ( n
== wxNOT_FOUND
)
1861 // add the attribute
1862 m_rowsOrCols
.Add(rowOrCol
);
1869 // change the attribute
1870 m_attrs
[(size_t)n
] = attr
;
1874 // remove this attribute
1875 m_attrs
[(size_t)n
]->DecRef();
1876 m_rowsOrCols
.RemoveAt((size_t)n
);
1877 m_attrs
.RemoveAt((size_t)n
);
1882 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
1884 size_t count
= m_attrs
.GetCount();
1885 for ( size_t n
= 0; n
< count
; n
++ )
1887 int & rowOrCol
= m_rowsOrCols
[n
];
1888 if ( (size_t)rowOrCol
>= pos
)
1890 if ( numRowsOrCols
> 0 )
1892 // If rows inserted, include row counter where necessary
1893 rowOrCol
+= numRowsOrCols
;
1895 else if ( numRowsOrCols
< 0)
1897 // If rows deleted, either decrement row counter (if row still exists)
1898 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
1899 rowOrCol
+= numRowsOrCols
;
1902 m_rowsOrCols
.RemoveAt((size_t)n
);
1903 m_attrs
.RemoveAt((size_t)n
);
1911 // ----------------------------------------------------------------------------
1912 // wxGridCellAttrProvider
1913 // ----------------------------------------------------------------------------
1915 wxGridCellAttrProvider::wxGridCellAttrProvider()
1917 m_data
= (wxGridCellAttrProviderData
*)NULL
;
1920 wxGridCellAttrProvider::~wxGridCellAttrProvider()
1925 void wxGridCellAttrProvider::InitData()
1927 m_data
= new wxGridCellAttrProviderData
;
1930 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
1932 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
1935 // first look for the attribute of this specific cell
1936 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
1940 // then look for the col attr (col attributes are more common than
1941 // the row ones, hence they have priority)
1942 attr
= m_data
->m_colAttrs
.GetAttr(col
);
1947 // finally try the row attributes
1948 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
1955 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
1961 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
1964 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
1969 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
1972 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
1977 m_data
->m_colAttrs
.SetAttr(attr
, col
);
1980 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
1984 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
1986 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
1990 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
1994 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
1996 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2000 // ----------------------------------------------------------------------------
2001 // wxGridTypeRegistry
2002 // ----------------------------------------------------------------------------
2004 wxGridTypeRegistry::~wxGridTypeRegistry()
2006 size_t count
= m_typeinfo
.Count();
2007 for ( size_t i
= 0; i
< count
; i
++ )
2008 delete m_typeinfo
[i
];
2012 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2013 wxGridCellRenderer
* renderer
,
2014 wxGridCellEditor
* editor
)
2016 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2018 // is it already registered?
2019 int loc
= FindRegisteredDataType(typeName
);
2020 if ( loc
!= wxNOT_FOUND
)
2022 delete m_typeinfo
[loc
];
2023 m_typeinfo
[loc
] = info
;
2027 m_typeinfo
.Add(info
);
2031 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2033 size_t count
= m_typeinfo
.GetCount();
2034 for ( size_t i
= 0; i
< count
; i
++ )
2036 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2045 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2047 int index
= FindRegisteredDataType(typeName
);
2048 if ( index
== wxNOT_FOUND
)
2050 // check whether this is one of the standard ones, in which case
2051 // register it "on the fly"
2052 if ( typeName
== wxGRID_VALUE_STRING
)
2054 RegisterDataType(wxGRID_VALUE_STRING
,
2055 new wxGridCellStringRenderer
,
2056 new wxGridCellTextEditor
);
2058 else if ( typeName
== wxGRID_VALUE_BOOL
)
2060 RegisterDataType(wxGRID_VALUE_BOOL
,
2061 new wxGridCellBoolRenderer
,
2062 new wxGridCellBoolEditor
);
2064 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2066 RegisterDataType(wxGRID_VALUE_NUMBER
,
2067 new wxGridCellNumberRenderer
,
2068 new wxGridCellNumberEditor
);
2070 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2072 RegisterDataType(wxGRID_VALUE_FLOAT
,
2073 new wxGridCellFloatRenderer
,
2074 new wxGridCellFloatEditor
);
2076 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2078 RegisterDataType(wxGRID_VALUE_CHOICE
,
2079 new wxGridCellStringRenderer
,
2080 new wxGridCellChoiceEditor
);
2087 // we get here only if just added the entry for this type, so return
2089 index
= m_typeinfo
.GetCount() - 1;
2095 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2097 int index
= FindDataType(typeName
);
2098 if ( index
== wxNOT_FOUND
)
2100 // the first part of the typename is the "real" type, anything after ':'
2101 // are the parameters for the renderer
2102 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2103 if ( index
== wxNOT_FOUND
)
2108 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2109 wxGridCellRenderer
*rendererOld
= renderer
;
2110 renderer
= renderer
->Clone();
2111 rendererOld
->DecRef();
2113 wxGridCellEditor
*editor
= GetEditor(index
);
2114 wxGridCellEditor
*editorOld
= editor
;
2115 editor
= editor
->Clone();
2116 editorOld
->DecRef();
2118 // do it even if there are no parameters to reset them to defaults
2119 wxString params
= typeName
.AfterFirst(_T(':'));
2120 renderer
->SetParameters(params
);
2121 editor
->SetParameters(params
);
2123 // register the new typename
2126 RegisterDataType(typeName
, renderer
, editor
);
2128 // we just registered it, it's the last one
2129 index
= m_typeinfo
.GetCount() - 1;
2135 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2137 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2142 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2144 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2149 // ----------------------------------------------------------------------------
2151 // ----------------------------------------------------------------------------
2153 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2156 wxGridTableBase::wxGridTableBase()
2158 m_view
= (wxGrid
*) NULL
;
2159 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2162 wxGridTableBase::~wxGridTableBase()
2164 delete m_attrProvider
;
2167 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2169 delete m_attrProvider
;
2170 m_attrProvider
= attrProvider
;
2173 bool wxGridTableBase::CanHaveAttributes()
2175 if ( ! GetAttrProvider() )
2177 // use the default attr provider by default
2178 SetAttrProvider(new wxGridCellAttrProvider
);
2183 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2185 if ( m_attrProvider
)
2186 return m_attrProvider
->GetAttr(row
, col
);
2188 return (wxGridCellAttr
*)NULL
;
2191 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2193 if ( m_attrProvider
)
2195 m_attrProvider
->SetAttr(attr
, row
, col
);
2199 // as we take ownership of the pointer and don't store it, we must
2205 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2207 if ( m_attrProvider
)
2209 m_attrProvider
->SetRowAttr(attr
, row
);
2213 // as we take ownership of the pointer and don't store it, we must
2219 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2221 if ( m_attrProvider
)
2223 m_attrProvider
->SetColAttr(attr
, col
);
2227 // as we take ownership of the pointer and don't store it, we must
2233 void wxGridTableBase::UpdateAttrRows( size_t pos
, int numRows
)
2235 if ( m_attrProvider
)
2237 m_attrProvider
->UpdateAttrRows( pos
, numRows
);
2241 void wxGridTableBase::UpdateAttrCols( size_t pos
, int numCols
)
2243 if ( m_attrProvider
)
2245 m_attrProvider
->UpdateAttrCols( pos
, numCols
);
2249 bool wxGridTableBase::InsertRows( size_t pos
, size_t numRows
)
2251 wxFAIL_MSG( wxT("Called grid table class function InsertRows\n"
2252 "but your derived table class does not override this function") );
2257 bool wxGridTableBase::AppendRows( size_t numRows
)
2259 wxFAIL_MSG( wxT("Called grid table class function AppendRows\n"
2260 "but your derived table class does not override this function"));
2265 bool wxGridTableBase::DeleteRows( size_t pos
, size_t numRows
)
2267 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\n"
2268 "but your derived table class does not override this function"));
2273 bool wxGridTableBase::InsertCols( size_t pos
, size_t numCols
)
2275 wxFAIL_MSG( wxT("Called grid table class function InsertCols\n"
2276 "but your derived table class does not override this function"));
2281 bool wxGridTableBase::AppendCols( size_t numCols
)
2283 wxFAIL_MSG(wxT("Called grid table class function AppendCols\n"
2284 "but your derived table class does not override this function"));
2289 bool wxGridTableBase::DeleteCols( size_t pos
, size_t numCols
)
2291 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\n"
2292 "but your derived table class does not override this function"));
2298 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2301 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2302 // how much it makes sense to us geeks.
2306 wxString
wxGridTableBase::GetColLabelValue( int col
)
2308 // default col labels are:
2309 // cols 0 to 25 : A-Z
2310 // cols 26 to 675 : AA-ZZ
2315 for ( n
= 1; ; n
++ )
2317 s
+= (_T('A') + (wxChar
)( col%26
));
2319 if ( col
< 0 ) break;
2322 // reverse the string...
2324 for ( i
= 0; i
< n
; i
++ )
2333 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2335 return wxGRID_VALUE_STRING
;
2338 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2339 const wxString
& typeName
)
2341 return typeName
== wxGRID_VALUE_STRING
;
2344 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2346 return CanGetValueAs(row
, col
, typeName
);
2349 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2354 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2359 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2364 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2365 long WXUNUSED(value
) )
2369 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2370 double WXUNUSED(value
) )
2374 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2375 bool WXUNUSED(value
) )
2380 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2381 const wxString
& WXUNUSED(typeName
) )
2386 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2387 const wxString
& WXUNUSED(typeName
),
2388 void* WXUNUSED(value
) )
2392 //////////////////////////////////////////////////////////////////////
2394 // Message class for the grid table to send requests and notifications
2398 wxGridTableMessage::wxGridTableMessage()
2400 m_table
= (wxGridTableBase
*) NULL
;
2406 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2407 int commandInt1
, int commandInt2
)
2411 m_comInt1
= commandInt1
;
2412 m_comInt2
= commandInt2
;
2417 //////////////////////////////////////////////////////////////////////
2419 // A basic grid table for string data. An object of this class will
2420 // created by wxGrid if you don't specify an alternative table class.
2423 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2425 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2427 wxGridStringTable::wxGridStringTable()
2432 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2437 m_data
.Alloc( numRows
);
2440 sa
.Alloc( numCols
);
2441 for ( col
= 0; col
< numCols
; col
++ )
2443 sa
.Add( wxEmptyString
);
2446 for ( row
= 0; row
< numRows
; row
++ )
2452 wxGridStringTable::~wxGridStringTable()
2456 long wxGridStringTable::GetNumberRows()
2458 return m_data
.GetCount();
2461 long wxGridStringTable::GetNumberCols()
2463 if ( m_data
.GetCount() > 0 )
2464 return m_data
[0].GetCount();
2469 wxString
wxGridStringTable::GetValue( int row
, int col
)
2471 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2472 _T("invalid row or column index in wxGridStringTable") );
2474 return m_data
[row
][col
];
2477 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2479 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2480 _T("invalid row or column index in wxGridStringTable") );
2482 m_data
[row
][col
] = value
;
2485 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2487 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2488 _T("invalid row or column index in wxGridStringTable") );
2490 return (m_data
[row
][col
] == wxEmptyString
);
2493 void wxGridStringTable::Clear()
2496 int numRows
, numCols
;
2498 numRows
= m_data
.GetCount();
2501 numCols
= m_data
[0].GetCount();
2503 for ( row
= 0; row
< numRows
; row
++ )
2505 for ( col
= 0; col
< numCols
; col
++ )
2507 m_data
[row
][col
] = wxEmptyString
;
2514 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2518 size_t curNumRows
= m_data
.GetCount();
2519 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2521 if ( pos
>= curNumRows
)
2523 return AppendRows( numRows
);
2527 sa
.Alloc( curNumCols
);
2528 for ( col
= 0; col
< curNumCols
; col
++ )
2530 sa
.Add( wxEmptyString
);
2533 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2535 m_data
.Insert( sa
, row
);
2537 UpdateAttrRows( pos
, numRows
);
2540 wxGridTableMessage
msg( this,
2541 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2545 GetView()->ProcessTableMessage( msg
);
2551 bool wxGridStringTable::AppendRows( size_t numRows
)
2555 size_t curNumRows
= m_data
.GetCount();
2556 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2559 if ( curNumCols
> 0 )
2561 sa
.Alloc( curNumCols
);
2562 for ( col
= 0; col
< curNumCols
; col
++ )
2564 sa
.Add( wxEmptyString
);
2568 for ( row
= 0; row
< numRows
; row
++ )
2575 wxGridTableMessage
msg( this,
2576 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2579 GetView()->ProcessTableMessage( msg
);
2585 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2589 size_t curNumRows
= m_data
.GetCount();
2591 if ( pos
>= curNumRows
)
2594 errmsg
.Printf("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\n"
2595 "Pos value is invalid for present table with %d rows",
2596 pos
, numRows
, curNumRows
);
2597 wxFAIL_MSG( wxT(errmsg
) );
2601 if ( numRows
> curNumRows
- pos
)
2603 numRows
= curNumRows
- pos
;
2606 if ( numRows
>= curNumRows
)
2608 m_data
.Empty(); // don't release memory just yet
2612 for ( n
= 0; n
< numRows
; n
++ )
2614 m_data
.Remove( pos
);
2617 UpdateAttrRows( pos
, -((int)numRows
) );
2620 wxGridTableMessage
msg( this,
2621 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2625 GetView()->ProcessTableMessage( msg
);
2631 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2635 size_t curNumRows
= m_data
.GetCount();
2636 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2638 if ( pos
>= curNumCols
)
2640 return AppendCols( numCols
);
2643 for ( row
= 0; row
< curNumRows
; row
++ )
2645 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2647 m_data
[row
].Insert( wxEmptyString
, col
);
2650 UpdateAttrCols( pos
, numCols
);
2653 wxGridTableMessage
msg( this,
2654 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2658 GetView()->ProcessTableMessage( msg
);
2664 bool wxGridStringTable::AppendCols( size_t numCols
)
2668 size_t curNumRows
= m_data
.GetCount();
2671 // TODO: something better than this ?
2673 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\n"
2674 "Call AppendRows() first") );
2678 for ( row
= 0; row
< curNumRows
; row
++ )
2680 for ( n
= 0; n
< numCols
; n
++ )
2682 m_data
[row
].Add( wxEmptyString
);
2688 wxGridTableMessage
msg( this,
2689 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2692 GetView()->ProcessTableMessage( msg
);
2698 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2702 size_t curNumRows
= m_data
.GetCount();
2703 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() : 0 );
2705 if ( pos
>= curNumCols
)
2708 errmsg
.Printf( "Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\n"
2709 "Pos value is invalid for present table with %d cols",
2710 pos
, numCols
, curNumCols
);
2711 wxFAIL_MSG( wxT( errmsg
) );
2715 if ( numCols
> curNumCols
- pos
)
2717 numCols
= curNumCols
- pos
;
2720 for ( row
= 0; row
< curNumRows
; row
++ )
2722 if ( numCols
>= curNumCols
)
2724 m_data
[row
].Clear();
2728 for ( n
= 0; n
< numCols
; n
++ )
2730 m_data
[row
].Remove( pos
);
2734 UpdateAttrCols( pos
, -((int)numCols
) );
2737 wxGridTableMessage
msg( this,
2738 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
2742 GetView()->ProcessTableMessage( msg
);
2748 wxString
wxGridStringTable::GetRowLabelValue( int row
)
2750 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2752 // using default label
2754 return wxGridTableBase::GetRowLabelValue( row
);
2758 return m_rowLabels
[ row
];
2762 wxString
wxGridStringTable::GetColLabelValue( int col
)
2764 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2766 // using default label
2768 return wxGridTableBase::GetColLabelValue( col
);
2772 return m_colLabels
[ col
];
2776 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
2778 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
2780 int n
= m_rowLabels
.GetCount();
2782 for ( i
= n
; i
<= row
; i
++ )
2784 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
2788 m_rowLabels
[row
] = value
;
2791 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
2793 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
2795 int n
= m_colLabels
.GetCount();
2797 for ( i
= n
; i
<= col
; i
++ )
2799 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
2803 m_colLabels
[col
] = value
;
2808 //////////////////////////////////////////////////////////////////////
2809 //////////////////////////////////////////////////////////////////////
2811 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
2813 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
2814 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
2815 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
2816 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
2819 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
2821 const wxPoint
&pos
, const wxSize
&size
)
2822 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2827 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
&event
)
2831 // NO - don't do this because it will set both the x and y origin
2832 // coords to match the parent scrolled window and we just want to
2833 // set the y coord - MB
2835 // m_owner->PrepareDC( dc );
2838 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2839 dc
.SetDeviceOrigin( 0, -y
);
2841 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
2842 m_owner
->DrawRowLabels( dc
);
2846 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2848 m_owner
->ProcessRowLabelMouseEvent( event
);
2852 // This seems to be required for wxMotif otherwise the mouse
2853 // cursor must be in the cell edit control to get key events
2855 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2857 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2862 //////////////////////////////////////////////////////////////////////
2864 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
2866 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
2867 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
2868 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
2869 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
2872 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
2874 const wxPoint
&pos
, const wxSize
&size
)
2875 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2880 void wxGridColLabelWindow::OnPaint( wxPaintEvent
&event
)
2884 // NO - don't do this because it will set both the x and y origin
2885 // coords to match the parent scrolled window and we just want to
2886 // set the x coord - MB
2888 // m_owner->PrepareDC( dc );
2891 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
2892 dc
.SetDeviceOrigin( -x
, 0 );
2894 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
2895 m_owner
->DrawColLabels( dc
);
2899 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2901 m_owner
->ProcessColLabelMouseEvent( event
);
2905 // This seems to be required for wxMotif otherwise the mouse
2906 // cursor must be in the cell edit control to get key events
2908 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2910 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2915 //////////////////////////////////////////////////////////////////////
2917 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
2919 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
2920 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
2921 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
2922 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
2925 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
2927 const wxPoint
&pos
, const wxSize
&size
)
2928 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
2933 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
2937 int client_height
= 0;
2938 int client_width
= 0;
2939 GetClientSize( &client_width
, &client_height
);
2941 dc
.SetPen( *wxBLACK_PEN
);
2942 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
2943 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
2945 dc
.SetPen( *wxWHITE_PEN
);
2946 dc
.DrawLine( 0, 0, client_width
, 0 );
2947 dc
.DrawLine( 0, 0, 0, client_height
);
2951 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
2953 m_owner
->ProcessCornerLabelMouseEvent( event
);
2957 // This seems to be required for wxMotif otherwise the mouse
2958 // cursor must be in the cell edit control to get key events
2960 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
2962 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
2967 //////////////////////////////////////////////////////////////////////
2969 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
2971 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
2972 EVT_PAINT( wxGridWindow::OnPaint
)
2973 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
2974 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
2975 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
2978 wxGridWindow::wxGridWindow( wxGrid
*parent
,
2979 wxGridRowLabelWindow
*rowLblWin
,
2980 wxGridColLabelWindow
*colLblWin
,
2981 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
2982 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
2985 m_rowLabelWin
= rowLblWin
;
2986 m_colLabelWin
= colLblWin
;
2987 SetBackgroundColour( "WHITE" );
2991 wxGridWindow::~wxGridWindow()
2996 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2998 wxPaintDC
dc( this );
2999 m_owner
->PrepareDC( dc
);
3000 wxRegion reg
= GetUpdateRegion();
3001 m_owner
->CalcCellsExposed( reg
);
3002 m_owner
->DrawGridCellArea( dc
);
3003 #if WXGRID_DRAW_LINES
3004 m_owner
->DrawAllGridLines( dc
, reg
);
3006 m_owner
->DrawGridSpace( dc
);
3007 m_owner
->DrawHighlight( dc
);
3011 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3013 wxPanel::ScrollWindow( dx
, dy
, rect
);
3014 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3015 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3019 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3021 m_owner
->ProcessGridCellMouseEvent( event
);
3025 // This seems to be required for wxMotif otherwise the mouse
3026 // cursor must be in the cell edit control to get key events
3028 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3030 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3034 void wxGridWindow::OnEraseBackground(wxEraseEvent
& event
)
3039 //////////////////////////////////////////////////////////////////////
3042 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3044 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3045 EVT_PAINT( wxGrid::OnPaint
)
3046 EVT_SIZE( wxGrid::OnSize
)
3047 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3048 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3051 wxGrid::wxGrid( wxWindow
*parent
,
3056 const wxString
& name
)
3057 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3058 m_colMinWidths(GRID_HASH_SIZE
),
3059 m_rowMinHeights(GRID_HASH_SIZE
)
3068 wxSafeDecRef(m_defaultCellAttr
);
3070 #ifdef DEBUG_ATTR_CACHE
3071 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3072 wxPrintf(_T("wxGrid attribute cache statistics: "
3073 "total: %u, hits: %u (%u%%)\n"),
3074 total
, gs_nAttrCacheHits
,
3075 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3081 delete m_typeRegistry
;
3086 // ----- internal init and update functions
3089 void wxGrid::Create()
3091 m_created
= FALSE
; // set to TRUE by CreateGrid
3092 m_displayed
= TRUE
; // FALSE; // set to TRUE by OnPaint
3094 m_table
= (wxGridTableBase
*) NULL
;
3097 m_cellEditCtrlEnabled
= FALSE
;
3099 m_defaultCellAttr
= new wxGridCellAttr
;
3100 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3102 // Set default cell attributes
3103 m_defaultCellAttr
->SetFont(GetFont());
3104 m_defaultCellAttr
->SetAlignment(wxLEFT
, wxTOP
);
3105 m_defaultCellAttr
->SetTextColour(
3106 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3107 m_defaultCellAttr
->SetBackgroundColour(
3108 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3109 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3110 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3115 m_currentCellCoords
= wxGridNoCellCoords
;
3117 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3118 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3120 // create the type registry
3121 m_typeRegistry
= new wxGridTypeRegistry
;
3123 // subwindow components that make up the wxGrid
3124 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3129 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3134 m_colLabelWin
= new wxGridColLabelWindow( this,
3139 m_gridWin
= new wxGridWindow( this,
3146 SetTargetWindow( m_gridWin
);
3150 bool wxGrid::CreateGrid( int numRows
, int numCols
)
3154 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3159 m_numRows
= numRows
;
3160 m_numCols
= numCols
;
3162 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3163 m_table
->SetView( this );
3172 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
)
3176 // RD: Actually, this should probably be allowed. I think it would be
3177 // nice to be able to switch multiple Tables in and out of a single
3178 // View at runtime. Is there anything in the implmentation that would
3181 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3186 m_numRows
= table
->GetNumberRows();
3187 m_numCols
= table
->GetNumberCols();
3190 m_table
->SetView( this );
3203 if ( m_numRows
<= 0 )
3204 m_numRows
= WXGRID_DEFAULT_NUMBER_ROWS
;
3206 if ( m_numCols
<= 0 )
3207 m_numCols
= WXGRID_DEFAULT_NUMBER_COLS
;
3209 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3210 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3212 if ( m_rowLabelWin
)
3214 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3218 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3221 m_labelTextColour
= wxColour( _T("BLACK") );
3224 m_attrCache
.row
= -1;
3226 // TODO: something better than this ?
3228 m_labelFont
= this->GetFont();
3229 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3231 m_rowLabelHorizAlign
= wxLEFT
;
3232 m_rowLabelVertAlign
= wxCENTRE
;
3234 m_colLabelHorizAlign
= wxCENTRE
;
3235 m_colLabelVertAlign
= wxTOP
;
3237 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3238 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3240 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3241 m_defaultRowHeight
+= 8;
3243 m_defaultRowHeight
+= 4;
3246 m_gridLineColour
= wxColour( 128, 128, 255 );
3247 m_gridLinesEnabled
= TRUE
;
3249 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3250 m_winCapture
= (wxWindow
*)NULL
;
3251 m_canDragRowSize
= TRUE
;
3252 m_canDragColSize
= TRUE
;
3253 m_canDragGridSize
= TRUE
;
3255 m_dragRowOrCol
= -1;
3256 m_isDragging
= FALSE
;
3257 m_startDragPos
= wxDefaultPosition
;
3259 m_waitForSlowClick
= FALSE
;
3261 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3262 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3264 m_currentCellCoords
= wxGridNoCellCoords
;
3266 m_selectedTopLeft
= wxGridNoCellCoords
;
3267 m_selectedBottomRight
= wxGridNoCellCoords
;
3268 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3269 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3271 m_editable
= TRUE
; // default for whole grid
3273 m_inOnKeyDown
= FALSE
;
3282 // ----------------------------------------------------------------------------
3283 // the idea is to call these functions only when necessary because they create
3284 // quite big arrays which eat memory mostly unnecessary - in particular, if
3285 // default widths/heights are used for all rows/columns, we may not use these
3288 // with some extra code, it should be possible to only store the
3289 // widths/heights different from default ones but this will be done later...
3290 // ----------------------------------------------------------------------------
3292 void wxGrid::InitRowHeights()
3294 m_rowHeights
.Empty();
3295 m_rowBottoms
.Empty();
3297 m_rowHeights
.Alloc( m_numRows
);
3298 m_rowBottoms
.Alloc( m_numRows
);
3301 for ( int i
= 0; i
< m_numRows
; i
++ )
3303 m_rowHeights
.Add( m_defaultRowHeight
);
3304 rowBottom
+= m_defaultRowHeight
;
3305 m_rowBottoms
.Add( rowBottom
);
3309 void wxGrid::InitColWidths()
3311 m_colWidths
.Empty();
3312 m_colRights
.Empty();
3314 m_colWidths
.Alloc( m_numCols
);
3315 m_colRights
.Alloc( m_numCols
);
3317 for ( int i
= 0; i
< m_numCols
; i
++ )
3319 m_colWidths
.Add( m_defaultColWidth
);
3320 colRight
+= m_defaultColWidth
;
3321 m_colRights
.Add( colRight
);
3325 int wxGrid::GetColWidth(int col
) const
3327 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3330 int wxGrid::GetColLeft(int col
) const
3332 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3333 : m_colRights
[col
] - m_colWidths
[col
];
3336 int wxGrid::GetColRight(int col
) const
3338 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3342 int wxGrid::GetRowHeight(int row
) const
3344 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3347 int wxGrid::GetRowTop(int row
) const
3349 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3350 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3353 int wxGrid::GetRowBottom(int row
) const
3355 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3356 : m_rowBottoms
[row
];
3359 void wxGrid::CalcDimensions()
3362 GetClientSize( &cw
, &ch
);
3364 if ( m_numRows
> 0 && m_numCols
> 0 )
3366 int right
= GetColRight( m_numCols
-1 ) + m_extraWidth
;
3367 int bottom
= GetRowBottom( m_numRows
-1 ) + m_extraHeight
;
3369 // TODO: restore the scroll position that we had before sizing
3372 GetViewStart( &x
, &y
);
3373 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3374 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3380 void wxGrid::CalcWindowSizes()
3383 GetClientSize( &cw
, &ch
);
3385 if ( m_cornerLabelWin
->IsShown() )
3386 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3388 if ( m_colLabelWin
->IsShown() )
3389 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3391 if ( m_rowLabelWin
->IsShown() )
3392 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3394 if ( m_gridWin
->IsShown() )
3395 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3399 // this is called when the grid table sends a message to say that it
3400 // has been redimensioned
3402 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3406 // if we were using the default widths/heights so far, we must change them
3408 if ( m_colWidths
.IsEmpty() )
3413 if ( m_rowHeights
.IsEmpty() )
3418 switch ( msg
.GetId() )
3420 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3422 size_t pos
= msg
.GetCommandInt();
3423 int numRows
= msg
.GetCommandInt2();
3424 for ( i
= 0; i
< numRows
; i
++ )
3426 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3427 m_rowBottoms
.Insert( 0, pos
);
3429 m_numRows
+= numRows
;
3432 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3434 for ( i
= pos
; i
< m_numRows
; i
++ )
3436 bottom
+= m_rowHeights
[i
];
3437 m_rowBottoms
[i
] = bottom
;
3443 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3445 int numRows
= msg
.GetCommandInt();
3446 for ( i
= 0; i
< numRows
; i
++ )
3448 m_rowHeights
.Add( m_defaultRowHeight
);
3449 m_rowBottoms
.Add( 0 );
3452 int oldNumRows
= m_numRows
;
3453 m_numRows
+= numRows
;
3456 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3458 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3460 bottom
+= m_rowHeights
[i
];
3461 m_rowBottoms
[i
] = bottom
;
3467 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3469 size_t pos
= msg
.GetCommandInt();
3470 int numRows
= msg
.GetCommandInt2();
3471 for ( i
= 0; i
< numRows
; i
++ )
3473 m_rowHeights
.Remove( pos
);
3474 m_rowBottoms
.Remove( pos
);
3476 m_numRows
-= numRows
;
3481 m_colWidths
.Clear();
3482 m_colRights
.Clear();
3483 m_currentCellCoords
= wxGridNoCellCoords
;
3487 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3488 m_currentCellCoords
.Set( 0, 0 );
3491 for ( i
= 0; i
< m_numRows
; i
++ )
3493 h
+= m_rowHeights
[i
];
3494 m_rowBottoms
[i
] = h
;
3502 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3504 size_t pos
= msg
.GetCommandInt();
3505 int numCols
= msg
.GetCommandInt2();
3506 for ( i
= 0; i
< numCols
; i
++ )
3508 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3509 m_colRights
.Insert( 0, pos
);
3511 m_numCols
+= numCols
;
3514 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3516 for ( i
= pos
; i
< m_numCols
; i
++ )
3518 right
+= m_colWidths
[i
];
3519 m_colRights
[i
] = right
;
3525 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3527 int numCols
= msg
.GetCommandInt();
3528 for ( i
= 0; i
< numCols
; i
++ )
3530 m_colWidths
.Add( m_defaultColWidth
);
3531 m_colRights
.Add( 0 );
3534 int oldNumCols
= m_numCols
;
3535 m_numCols
+= numCols
;
3538 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3540 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3542 right
+= m_colWidths
[i
];
3543 m_colRights
[i
] = right
;
3549 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3551 size_t pos
= msg
.GetCommandInt();
3552 int numCols
= msg
.GetCommandInt2();
3553 for ( i
= 0; i
< numCols
; i
++ )
3555 m_colWidths
.Remove( pos
);
3556 m_colRights
.Remove( pos
);
3558 m_numCols
-= numCols
;
3562 #if 0 // leave the row alone here so that AppendCols will work subsequently
3564 m_rowHeights
.Clear();
3565 m_rowBottoms
.Clear();
3567 m_currentCellCoords
= wxGridNoCellCoords
;
3571 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3572 m_currentCellCoords
.Set( 0, 0 );
3575 for ( i
= 0; i
< m_numCols
; i
++ )
3577 w
+= m_colWidths
[i
];
3590 void wxGrid::CalcRowLabelsExposed( wxRegion
& reg
)
3592 wxRegionIterator
iter( reg
);
3595 m_rowLabelsExposed
.Empty();
3602 // TODO: remove this when we can...
3603 // There is a bug in wxMotif that gives garbage update
3604 // rectangles if you jump-scroll a long way by clicking the
3605 // scrollbar with middle button. This is a work-around
3607 #if defined(__WXMOTIF__)
3609 m_gridWin
->GetClientSize( &cw
, &ch
);
3610 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3611 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3614 // logical bounds of update region
3617 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
3618 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
3620 // find the row labels within these bounds
3623 for ( row
= 0; row
< m_numRows
; row
++ )
3625 if ( GetRowBottom(row
) < top
)
3628 if ( GetRowTop(row
) > bottom
)
3631 m_rowLabelsExposed
.Add( row
);
3639 void wxGrid::CalcColLabelsExposed( wxRegion
& reg
)
3641 wxRegionIterator
iter( reg
);
3644 m_colLabelsExposed
.Empty();
3651 // TODO: remove this when we can...
3652 // There is a bug in wxMotif that gives garbage update
3653 // rectangles if you jump-scroll a long way by clicking the
3654 // scrollbar with middle button. This is a work-around
3656 #if defined(__WXMOTIF__)
3658 m_gridWin
->GetClientSize( &cw
, &ch
);
3659 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3660 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3663 // logical bounds of update region
3666 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
3667 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
3669 // find the cells within these bounds
3672 for ( col
= 0; col
< m_numCols
; col
++ )
3674 if ( GetColRight(col
) < left
)
3677 if ( GetColLeft(col
) > right
)
3680 m_colLabelsExposed
.Add( col
);
3688 void wxGrid::CalcCellsExposed( wxRegion
& reg
)
3690 wxRegionIterator
iter( reg
);
3693 m_cellsExposed
.Empty();
3694 m_rowsExposed
.Empty();
3695 m_colsExposed
.Empty();
3697 int left
, top
, right
, bottom
;
3702 // TODO: remove this when we can...
3703 // There is a bug in wxMotif that gives garbage update
3704 // rectangles if you jump-scroll a long way by clicking the
3705 // scrollbar with middle button. This is a work-around
3707 #if defined(__WXMOTIF__)
3709 m_gridWin
->GetClientSize( &cw
, &ch
);
3710 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
3711 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
3712 r
.SetRight( wxMin( r
.GetRight(), cw
) );
3713 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
3716 // logical bounds of update region
3718 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
3719 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
3721 // find the cells within these bounds
3724 for ( row
= 0; row
< m_numRows
; row
++ )
3726 if ( GetRowBottom(row
) <= top
)
3729 if ( GetRowTop(row
) > bottom
)
3732 m_rowsExposed
.Add( row
);
3734 for ( col
= 0; col
< m_numCols
; col
++ )
3736 if ( GetColRight(col
) <= left
)
3739 if ( GetColLeft(col
) > right
)
3742 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
3743 m_colsExposed
.Add( col
);
3744 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
3753 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
3756 wxPoint
pos( event
.GetPosition() );
3757 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3759 if ( event
.Dragging() )
3761 m_isDragging
= TRUE
;
3763 if ( event
.LeftIsDown() )
3765 switch( m_cursorMode
)
3767 case WXGRID_CURSOR_RESIZE_ROW
:
3769 int cw
, ch
, left
, dummy
;
3770 m_gridWin
->GetClientSize( &cw
, &ch
);
3771 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
3773 wxClientDC
dc( m_gridWin
);
3776 GetRowTop(m_dragRowOrCol
) +
3777 GetRowMinimalHeight(m_dragRowOrCol
) );
3778 dc
.SetLogicalFunction(wxINVERT
);
3779 if ( m_dragLastPos
>= 0 )
3781 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
3783 dc
.DrawLine( left
, y
, left
+cw
, y
);
3788 case WXGRID_CURSOR_SELECT_ROW
:
3789 if ( (row
= YToRow( y
)) >= 0 &&
3790 !IsInSelection( row
, 0 ) )
3792 SelectRow( row
, TRUE
);
3795 // default label to suppress warnings about "enumeration value
3796 // 'xxx' not handled in switch
3804 m_isDragging
= FALSE
;
3807 // ------------ Entering or leaving the window
3809 if ( event
.Entering() || event
.Leaving() )
3811 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3815 // ------------ Left button pressed
3817 else if ( event
.LeftDown() )
3819 // don't send a label click event for a hit on the
3820 // edge of the row label - this is probably the user
3821 // wanting to resize the row
3823 if ( YToEdgeOfRow(y
) < 0 )
3827 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
3829 SelectRow( row
, event
.ShiftDown() );
3830 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
3835 // starting to drag-resize a row
3837 if ( CanDragRowSize() )
3838 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
3843 // ------------ Left double click
3845 else if (event
.LeftDClick() )
3847 if ( YToEdgeOfRow(y
) < 0 )
3850 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
3855 // ------------ Left button released
3857 else if ( event
.LeftUp() )
3859 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
3861 DoEndDragResizeRow();
3863 // Note: we are ending the event *after* doing
3864 // default processing in this case
3866 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
3869 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
3874 // ------------ Right button down
3876 else if ( event
.RightDown() )
3879 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
3881 // no default action at the moment
3886 // ------------ Right double click
3888 else if ( event
.RightDClick() )
3891 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
3893 // no default action at the moment
3898 // ------------ No buttons down and mouse moving
3900 else if ( event
.Moving() )
3902 m_dragRowOrCol
= YToEdgeOfRow( y
);
3903 if ( m_dragRowOrCol
>= 0 )
3905 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
3907 // don't capture the mouse yet
3908 if ( CanDragRowSize() )
3909 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
3912 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
3914 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
3920 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
3923 wxPoint
pos( event
.GetPosition() );
3924 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
3926 if ( event
.Dragging() )
3928 m_isDragging
= TRUE
;
3930 if ( event
.LeftIsDown() )
3932 switch( m_cursorMode
)
3934 case WXGRID_CURSOR_RESIZE_COL
:
3936 int cw
, ch
, dummy
, top
;
3937 m_gridWin
->GetClientSize( &cw
, &ch
);
3938 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
3940 wxClientDC
dc( m_gridWin
);
3943 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
3944 GetColMinimalWidth(m_dragRowOrCol
));
3945 dc
.SetLogicalFunction(wxINVERT
);
3946 if ( m_dragLastPos
>= 0 )
3948 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
3950 dc
.DrawLine( x
, top
, x
, top
+ch
);
3955 case WXGRID_CURSOR_SELECT_COL
:
3956 if ( (col
= XToCol( x
)) >= 0 &&
3957 !IsInSelection( 0, col
) )
3959 SelectCol( col
, TRUE
);
3962 // default label to suppress warnings about "enumeration value
3963 // 'xxx' not handled in switch
3971 m_isDragging
= FALSE
;
3974 // ------------ Entering or leaving the window
3976 if ( event
.Entering() || event
.Leaving() )
3978 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
3982 // ------------ Left button pressed
3984 else if ( event
.LeftDown() )
3986 // don't send a label click event for a hit on the
3987 // edge of the col label - this is probably the user
3988 // wanting to resize the col
3990 if ( XToEdgeOfCol(x
) < 0 )
3994 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
3996 SelectCol( col
, event
.ShiftDown() );
3997 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4002 // starting to drag-resize a col
4004 if ( CanDragColSize() )
4005 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4010 // ------------ Left double click
4012 if ( event
.LeftDClick() )
4014 if ( XToEdgeOfCol(x
) < 0 )
4017 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4022 // ------------ Left button released
4024 else if ( event
.LeftUp() )
4026 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4028 DoEndDragResizeCol();
4030 // Note: we are ending the event *after* doing
4031 // default processing in this case
4033 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4036 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4041 // ------------ Right button down
4043 else if ( event
.RightDown() )
4046 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4048 // no default action at the moment
4053 // ------------ Right double click
4055 else if ( event
.RightDClick() )
4058 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4060 // no default action at the moment
4065 // ------------ No buttons down and mouse moving
4067 else if ( event
.Moving() )
4069 m_dragRowOrCol
= XToEdgeOfCol( x
);
4070 if ( m_dragRowOrCol
>= 0 )
4072 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4074 // don't capture the cursor yet
4075 if ( CanDragColSize() )
4076 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4079 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4081 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4087 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4089 if ( event
.LeftDown() )
4091 // indicate corner label by having both row and
4094 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4100 else if ( event
.LeftDClick() )
4102 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4105 else if ( event
.RightDown() )
4107 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4109 // no default action at the moment
4113 else if ( event
.RightDClick() )
4115 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4117 // no default action at the moment
4122 void wxGrid::ChangeCursorMode(CursorMode mode
,
4127 static const wxChar
*cursorModes
[] =
4136 wxLogTrace(_T("grid"),
4137 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4138 win
== m_colLabelWin
? _T("colLabelWin")
4139 : win
? _T("rowLabelWin")
4141 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4142 #endif // __WXDEBUG__
4144 if ( mode
== m_cursorMode
)
4149 // by default use the grid itself
4155 m_winCapture
->ReleaseMouse();
4156 m_winCapture
= (wxWindow
*)NULL
;
4159 m_cursorMode
= mode
;
4161 switch ( m_cursorMode
)
4163 case WXGRID_CURSOR_RESIZE_ROW
:
4164 win
->SetCursor( m_rowResizeCursor
);
4167 case WXGRID_CURSOR_RESIZE_COL
:
4168 win
->SetCursor( m_colResizeCursor
);
4172 win
->SetCursor( *wxSTANDARD_CURSOR
);
4175 // we need to capture mouse when resizing
4176 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4177 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4179 if ( captureMouse
&& resize
)
4181 win
->CaptureMouse();
4186 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4189 wxPoint
pos( event
.GetPosition() );
4190 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4192 wxGridCellCoords coords
;
4193 XYToCell( x
, y
, coords
);
4195 if ( event
.Dragging() )
4197 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4199 // Don't start doing anything until the mouse has been drug at
4200 // least 3 pixels in any direction...
4203 if (m_startDragPos
== wxDefaultPosition
)
4205 m_startDragPos
= pos
;
4208 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4212 m_isDragging
= TRUE
;
4213 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4215 // Hide the edit control, so it
4216 // won't interfer with drag-shrinking.
4217 if ( IsCellEditControlEnabled() )
4219 HideCellEditControl();
4220 SaveEditControlValue();
4223 // Have we captured the mouse yet?
4226 m_winCapture
= m_gridWin
;
4227 m_winCapture
->CaptureMouse();
4230 if ( coords
!= wxGridNoCellCoords
)
4232 if ( !IsSelection() )
4234 SelectBlock( coords
, coords
);
4238 SelectBlock( m_currentCellCoords
, coords
);
4241 if (! IsVisible(coords
))
4243 MakeCellVisible(coords
);
4244 // TODO: need to introduce a delay or something here. The
4245 // scrolling is way to fast, at least on MSW.
4249 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4251 int cw
, ch
, left
, dummy
;
4252 m_gridWin
->GetClientSize( &cw
, &ch
);
4253 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4255 wxClientDC
dc( m_gridWin
);
4257 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) + WXGRID_MIN_ROW_HEIGHT
);
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
);
7266 wxCoord extent
, extentMax
= 0;
7267 int max
= column
? m_numRows
: m_numCols
;
7268 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
7275 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7276 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7279 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
7280 extent
= column
? size
.x
: size
.y
;
7281 if ( extent
> extentMax
)
7292 // now also compare with the column label extent
7294 dc
.SetFont( GetLabelFont() );
7297 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
7299 dc
.GetTextExtent( GetRowLabelValue(col
), &w
, &h
);
7301 extent
= column
? w
: h
;
7302 if ( extent
> extentMax
)
7309 // empty column - give default extent (notice that if extentMax is less
7310 // than default extent but != 0, it's ok)
7311 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
7315 // leave some space around text
7320 SetColSize(col
, extentMax
);
7322 SetRowSize(row
, extentMax
);
7327 SetColMinimalWidth(col
, extentMax
);
7329 SetRowMinimalHeight(row
, extentMax
);
7333 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
7335 int width
= m_rowLabelWidth
;
7337 for ( int col
= 0; col
< m_numCols
; col
++ )
7341 AutoSizeColumn(col
, setAsMin
);
7344 width
+= GetColWidth(col
);
7350 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
7352 int height
= m_colLabelHeight
;
7354 for ( int row
= 0; row
< m_numRows
; row
++ )
7358 AutoSizeRow(row
, setAsMin
);
7361 height
+= GetRowHeight(row
);
7367 void wxGrid::AutoSize()
7370 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
7373 wxSize
wxGrid::DoGetBestSize() const
7375 // don't set sizes, only calculate them
7376 wxGrid
*self
= (wxGrid
*)this; // const_cast
7378 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
7379 self
->SetOrCalcRowSizes(TRUE
));
7387 // ----------------------------------------------------------------------------
7388 // cell value accessor functions
7389 // ----------------------------------------------------------------------------
7391 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
7395 m_table
->SetValue( row
, col
, s
.c_str() );
7396 if ( !GetBatchCount() )
7398 wxClientDC
dc( m_gridWin
);
7400 DrawCell( dc
, wxGridCellCoords(row
, col
) );
7403 if ( m_currentCellCoords
.GetRow() == row
&&
7404 m_currentCellCoords
.GetCol() == col
&&
7405 IsCellEditControlEnabled())
7407 HideCellEditControl();
7408 ShowCellEditControl(); // will reread data from table
7415 // ------ Block, row and col selection
7418 void wxGrid::SelectRow( int row
, bool addToSelected
)
7422 if ( IsSelection() && addToSelected
)
7425 bool need_refresh
[4];
7429 need_refresh
[3] = FALSE
;
7433 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7434 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7435 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7436 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7440 need_refresh
[0] = TRUE
;
7441 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( row
, 0 ),
7442 wxGridCellCoords ( oldTop
- 1,
7444 m_selectedTopLeft
.SetRow( row
);
7449 need_refresh
[1] = TRUE
;
7450 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
, 0 ),
7451 wxGridCellCoords ( oldBottom
,
7454 m_selectedTopLeft
.SetCol( 0 );
7457 if ( oldBottom
< row
)
7459 need_refresh
[2] = TRUE
;
7460 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1, 0 ),
7461 wxGridCellCoords ( row
,
7463 m_selectedBottomRight
.SetRow( row
);
7466 if ( oldRight
< m_numCols
- 1 )
7468 need_refresh
[3] = TRUE
;
7469 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7471 wxGridCellCoords ( oldBottom
,
7473 m_selectedBottomRight
.SetCol( m_numCols
- 1 );
7476 for (i
= 0; i
< 4; i
++ )
7477 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7478 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7482 r
= SelectionToDeviceRect();
7484 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7486 m_selectedTopLeft
.Set( row
, 0 );
7487 m_selectedBottomRight
.Set( row
, m_numCols
-1 );
7488 r
= SelectionToDeviceRect();
7489 m_gridWin
->Refresh( FALSE
, &r
);
7492 wxGridRangeSelectEvent
gridEvt( GetId(),
7493 wxEVT_GRID_RANGE_SELECT
,
7496 m_selectedBottomRight
);
7498 GetEventHandler()->ProcessEvent(gridEvt
);
7502 void wxGrid::SelectCol( int col
, bool addToSelected
)
7504 if ( IsSelection() && addToSelected
)
7507 bool need_refresh
[4];
7511 need_refresh
[3] = FALSE
;
7514 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7515 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7516 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7517 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7519 if ( oldLeft
> col
)
7521 need_refresh
[0] = TRUE
;
7522 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( 0, col
),
7523 wxGridCellCoords ( m_numRows
- 1,
7525 m_selectedTopLeft
.SetCol( col
);
7530 need_refresh
[1] = TRUE
;
7531 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( 0, oldLeft
),
7532 wxGridCellCoords ( oldTop
- 1,
7534 m_selectedTopLeft
.SetRow( 0 );
7537 if ( oldRight
< col
)
7539 need_refresh
[2] = TRUE
;
7540 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( 0, oldRight
+ 1 ),
7541 wxGridCellCoords ( m_numRows
- 1,
7543 m_selectedBottomRight
.SetCol( col
);
7546 if ( oldBottom
< m_numRows
- 1 )
7548 need_refresh
[3] = TRUE
;
7549 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( oldBottom
+ 1,
7551 wxGridCellCoords ( m_numRows
- 1,
7553 m_selectedBottomRight
.SetRow( m_numRows
- 1 );
7556 for (i
= 0; i
< 4; i
++ )
7557 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7558 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7564 r
= SelectionToDeviceRect();
7566 if ( r
!= wxGridNoCellRect
) m_gridWin
->Refresh( FALSE
, &r
);
7568 m_selectedTopLeft
.Set( 0, col
);
7569 m_selectedBottomRight
.Set( m_numRows
-1, col
);
7570 r
= SelectionToDeviceRect();
7571 m_gridWin
->Refresh( FALSE
, &r
);
7574 wxGridRangeSelectEvent
gridEvt( GetId(),
7575 wxEVT_GRID_RANGE_SELECT
,
7578 m_selectedBottomRight
);
7580 GetEventHandler()->ProcessEvent(gridEvt
);
7584 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
7587 wxGridCellCoords updateTopLeft
, updateBottomRight
;
7589 if ( topRow
> bottomRow
)
7596 if ( leftCol
> rightCol
)
7603 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
7604 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
7606 if ( m_selectedTopLeft
!= updateTopLeft
||
7607 m_selectedBottomRight
!= updateBottomRight
)
7609 // Compute two optimal update rectangles:
7610 // Either one rectangle is a real subset of the
7611 // other, or they are (almost) disjoint!
7613 bool need_refresh
[4];
7617 need_refresh
[3] = FALSE
;
7620 // Store intermediate values
7621 wxCoord oldLeft
= m_selectedTopLeft
.GetCol();
7622 wxCoord oldTop
= m_selectedTopLeft
.GetRow();
7623 wxCoord oldRight
= m_selectedBottomRight
.GetCol();
7624 wxCoord oldBottom
= m_selectedBottomRight
.GetRow();
7626 // Determine the outer/inner coordinates.
7627 if (oldLeft
> leftCol
)
7633 if (oldTop
> topRow
)
7639 if (oldRight
< rightCol
)
7642 oldRight
= rightCol
;
7645 if (oldBottom
< bottomRow
)
7648 oldBottom
= bottomRow
;
7652 // Now, either the stuff marked old is the outer
7653 // rectangle or we don't have a situation where one
7654 // is contained in the other.
7656 if ( oldLeft
< leftCol
)
7658 need_refresh
[0] = TRUE
;
7659 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7661 wxGridCellCoords ( oldBottom
,
7665 if ( oldTop
< topRow
)
7667 need_refresh
[1] = TRUE
;
7668 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7670 wxGridCellCoords ( topRow
- 1,
7674 if ( oldRight
> rightCol
)
7676 need_refresh
[2] = TRUE
;
7677 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
7679 wxGridCellCoords ( oldBottom
,
7683 if ( oldBottom
> bottomRow
)
7685 need_refresh
[3] = TRUE
;
7686 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
7688 wxGridCellCoords ( oldBottom
,
7694 m_selectedTopLeft
= updateTopLeft
;
7695 m_selectedBottomRight
= updateBottomRight
;
7697 // various Refresh() calls
7698 for (i
= 0; i
< 4; i
++ )
7699 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
7700 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
7703 // only generate an event if the block is not being selected by
7704 // dragging the mouse (in which case the event will be generated in
7705 // the mouse event handler)
7706 if ( !m_isDragging
)
7708 wxGridRangeSelectEvent
gridEvt( GetId(),
7709 wxEVT_GRID_RANGE_SELECT
,
7712 m_selectedBottomRight
);
7714 GetEventHandler()->ProcessEvent(gridEvt
);
7718 void wxGrid::SelectAll()
7720 m_selectedTopLeft
.Set( 0, 0 );
7721 m_selectedBottomRight
.Set( m_numRows
-1, m_numCols
-1 );
7723 m_gridWin
->Refresh();
7727 void wxGrid::ClearSelection()
7729 m_selectedTopLeft
= wxGridNoCellCoords
;
7730 m_selectedBottomRight
= wxGridNoCellCoords
;
7734 // This function returns the rectangle that encloses the given block
7735 // in device coords clipped to the client size of the grid window.
7737 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
7738 const wxGridCellCoords
&bottomRight
)
7740 wxRect
rect( wxGridNoCellRect
);
7743 cellRect
= CellToRect( topLeft
);
7744 if ( cellRect
!= wxGridNoCellRect
)
7750 rect
= wxRect( 0, 0, 0, 0 );
7753 cellRect
= CellToRect( bottomRight
);
7754 if ( cellRect
!= wxGridNoCellRect
)
7760 return wxGridNoCellRect
;
7763 // convert to scrolled coords
7765 int left
, top
, right
, bottom
;
7766 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
7767 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
7770 m_gridWin
->GetClientSize( &cw
, &ch
);
7772 rect
.SetLeft( wxMax(0, left
) );
7773 rect
.SetTop( wxMax(0, top
) );
7774 rect
.SetRight( wxMin(cw
, right
) );
7775 rect
.SetBottom( wxMin(ch
, bottom
) );
7783 // ------ Grid event classes
7786 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
7788 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
7789 int row
, int col
, int x
, int y
,
7790 bool control
, bool shift
, bool alt
, bool meta
)
7791 : wxNotifyEvent( type
, id
)
7797 m_control
= control
;
7802 SetEventObject(obj
);
7806 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
7808 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
7809 int rowOrCol
, int x
, int y
,
7810 bool control
, bool shift
, bool alt
, bool meta
)
7811 : wxNotifyEvent( type
, id
)
7813 m_rowOrCol
= rowOrCol
;
7816 m_control
= control
;
7821 SetEventObject(obj
);
7825 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
7827 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
7828 const wxGridCellCoords
& topLeft
,
7829 const wxGridCellCoords
& bottomRight
,
7830 bool control
, bool shift
, bool alt
, bool meta
)
7831 : wxNotifyEvent( type
, id
)
7833 m_topLeft
= topLeft
;
7834 m_bottomRight
= bottomRight
;
7835 m_control
= control
;
7840 SetEventObject(obj
);
7844 #endif // ifndef wxUSE_NEW_GRID