1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
430 wxGridCellEditor::~wxGridCellEditor()
435 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
436 wxWindowID
WXUNUSED(id
),
437 wxEvtHandler
* evtHandler
)
440 m_control
->PushEventHandler(evtHandler
);
443 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
444 wxGridCellAttr
*attr
)
446 // erase the background because we might not fill the cell
447 wxClientDC
dc(m_control
->GetParent());
448 dc
.SetPen(*wxTRANSPARENT_PEN
);
449 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
450 dc
.DrawRectangle(rectCell
);
452 // redraw the control we just painted over
453 m_control
->Refresh();
456 void wxGridCellEditor::Destroy()
460 m_control
->PopEventHandler(TRUE
/* delete it*/);
462 m_control
->Destroy();
467 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
469 wxASSERT_MSG(m_control
,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control
->Show(show
);
475 // set the colours/fonts if we have any
478 m_colFgOld
= m_control
->GetForegroundColour();
479 m_control
->SetForegroundColour(attr
->GetTextColour());
481 m_colBgOld
= m_control
->GetBackgroundColour();
482 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
484 m_fontOld
= m_control
->GetFont();
485 m_control
->SetFont(attr
->GetFont());
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
493 // restore the standard colours fonts
494 if ( m_colFgOld
.Ok() )
496 m_control
->SetForegroundColour(m_colFgOld
);
497 m_colFgOld
= wxNullColour
;
500 if ( m_colBgOld
.Ok() )
502 m_control
->SetBackgroundColour(m_colBgOld
);
503 m_colBgOld
= wxNullColour
;
506 if ( m_fontOld
.Ok() )
508 m_control
->SetFont(m_fontOld
);
509 m_fontOld
= wxNullFont
;
514 void wxGridCellEditor::SetSize(const wxRect
& rect
)
516 wxASSERT_MSG(m_control
,
517 wxT("The wxGridCellEditor must be Created first!"));
518 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
521 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
526 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
529 return !(event
.ControlDown() || event
.AltDown());
532 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
537 void wxGridCellEditor::StartingClick()
543 // ----------------------------------------------------------------------------
544 // wxGridCellTextEditor
545 // ----------------------------------------------------------------------------
547 wxGridCellTextEditor::wxGridCellTextEditor()
552 void wxGridCellTextEditor::Create(wxWindow
* parent
,
554 wxEvtHandler
* evtHandler
)
556 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
557 wxDefaultPosition
, wxDefaultSize
558 #if defined(__WXMSW__)
559 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
560 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
564 // TODO: use m_maxChars
566 wxGridCellEditor::Create(parent
, id
, evtHandler
);
569 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
570 wxGridCellAttr
* WXUNUSED(attr
))
572 // as we fill the entire client area, don't do anything here to minimize
576 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
578 wxRect
rect(rectOrig
);
580 // Make the edit control large enough to allow for internal
583 // TODO: remove this if the text ctrl sizing is improved esp. for
586 #if defined(__WXGTK__)
595 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
597 // MB: treat MSW separately here otherwise the caret doesn't show
598 // when the editor is in the first row.
599 #if defined(__WXMSW__)
602 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
605 #if defined(__WXMOTIF__)
609 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
610 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
611 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
612 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
615 wxGridCellEditor::SetSize(rect
);
618 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
625 DoBeginEdit(m_startValue
);
628 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
630 Text()->SetValue(startValue
);
631 Text()->SetInsertionPointEnd();
632 Text()->SetSelection(-1,-1);
636 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
639 wxASSERT_MSG(m_control
,
640 wxT("The wxGridCellEditor must be Created first!"));
642 bool changed
= FALSE
;
643 wxString value
= Text()->GetValue();
644 if (value
!= m_startValue
)
648 grid
->GetTable()->SetValue(row
, col
, value
);
650 m_startValue
= wxEmptyString
;
651 Text()->SetValue(m_startValue
);
657 void wxGridCellTextEditor::Reset()
659 wxASSERT_MSG(m_control
,
660 wxT("The wxGridCellEditor must be Created first!"));
662 DoReset(m_startValue
);
665 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
667 Text()->SetValue(startValue
);
668 Text()->SetInsertionPointEnd();
671 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
673 if ( wxGridCellEditor::IsAcceptedKey(event
) )
675 int keycode
= event
.GetKeyCode();
689 case WXK_NUMPAD_MULTIPLY
:
693 case WXK_NUMPAD_SUBTRACT
:
695 case WXK_NUMPAD_DECIMAL
:
697 case WXK_NUMPAD_DIVIDE
:
701 // accept 8 bit chars too if isprint() agrees
702 if ( (keycode
< 255) && (isprint(keycode
)) )
710 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
712 if ( !Text()->EmulateKeyPress(event
) )
718 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
719 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
721 #if defined(__WXMOTIF__) || defined(__WXGTK__)
722 // wxMotif needs a little extra help...
723 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
724 wxString
s( Text()->GetValue() );
725 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
727 Text()->SetInsertionPoint( pos
);
729 // the other ports can handle a Return key press
735 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
745 if ( !params
.ToLong(&tmp
) )
747 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
751 m_maxChars
= (size_t)tmp
;
756 // return the value in the text control
757 wxString
wxGridCellTextEditor::GetValue() const
759 return Text()->GetValue();
762 // ----------------------------------------------------------------------------
763 // wxGridCellNumberEditor
764 // ----------------------------------------------------------------------------
766 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
772 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
774 wxEvtHandler
* evtHandler
)
778 // create a spin ctrl
779 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
780 wxDefaultPosition
, wxDefaultSize
,
784 wxGridCellEditor::Create(parent
, id
, evtHandler
);
788 // just a text control
789 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
792 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
793 #endif // wxUSE_VALIDATORS
797 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
799 // first get the value
800 wxGridTableBase
*table
= grid
->GetTable();
801 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
803 m_valueOld
= table
->GetValueAsLong(row
, col
);
808 wxString sValue
= table
->GetValue(row
, col
);
809 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
811 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
818 Spin()->SetValue((int)m_valueOld
);
823 DoBeginEdit(GetString());
827 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
836 value
= Spin()->GetValue();
837 changed
= value
!= m_valueOld
;
839 text
= wxString::Format(wxT("%ld"), value
);
843 text
= Text()->GetValue();
844 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
849 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
850 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
852 grid
->GetTable()->SetValue(row
, col
, text
);
858 void wxGridCellNumberEditor::Reset()
862 Spin()->SetValue((int)m_valueOld
);
866 DoReset(GetString());
870 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
872 if ( wxGridCellEditor::IsAcceptedKey(event
) )
874 int keycode
= event
.GetKeyCode();
890 case WXK_NUMPAD_SUBTRACT
:
896 if ( (keycode
< 128) && isdigit(keycode
) )
904 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
908 int keycode
= (int) event
.KeyCode();
909 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
910 || keycode
== WXK_NUMPAD0
911 || keycode
== WXK_NUMPAD1
912 || keycode
== WXK_NUMPAD2
913 || keycode
== WXK_NUMPAD3
914 || keycode
== WXK_NUMPAD4
915 || keycode
== WXK_NUMPAD5
916 || keycode
== WXK_NUMPAD6
917 || keycode
== WXK_NUMPAD7
918 || keycode
== WXK_NUMPAD8
919 || keycode
== WXK_NUMPAD9
920 || keycode
== WXK_ADD
921 || keycode
== WXK_NUMPAD_ADD
922 || keycode
== WXK_SUBTRACT
923 || keycode
== WXK_NUMPAD_SUBTRACT
)
925 wxGridCellTextEditor::StartingKey(event
);
935 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
946 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
950 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
954 // skip the error message below
959 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
963 // return the value in the spin control if it is there (the text control otherwise)
964 wxString
wxGridCellNumberEditor::GetValue() const
970 int value
= Spin()->GetValue();
971 s
.Printf(wxT("%ld"), value
);
975 s
= Text()->GetValue();
980 // ----------------------------------------------------------------------------
981 // wxGridCellFloatEditor
982 // ----------------------------------------------------------------------------
984 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
987 m_precision
= precision
;
990 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
992 wxEvtHandler
* evtHandler
)
994 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
997 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
998 #endif // wxUSE_VALIDATORS
1001 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1003 // first get the value
1004 wxGridTableBase
*table
= grid
->GetTable();
1005 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1007 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1012 wxString sValue
= table
->GetValue(row
, col
);
1013 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1015 wxFAIL_MSG( _T("this cell doesn't have float value") );
1020 DoBeginEdit(GetString());
1023 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1027 wxString
text(Text()->GetValue());
1029 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1031 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1032 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1034 grid
->GetTable()->SetValue(row
, col
, text
);
1041 void wxGridCellFloatEditor::Reset()
1043 DoReset(GetString());
1046 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1048 int keycode
= (int)event
.KeyCode();
1049 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1050 || keycode
== WXK_NUMPAD0
1051 || keycode
== WXK_NUMPAD1
1052 || keycode
== WXK_NUMPAD2
1053 || keycode
== WXK_NUMPAD3
1054 || keycode
== WXK_NUMPAD4
1055 || keycode
== WXK_NUMPAD5
1056 || keycode
== WXK_NUMPAD6
1057 || keycode
== WXK_NUMPAD7
1058 || keycode
== WXK_NUMPAD8
1059 || keycode
== WXK_NUMPAD9
1060 || keycode
== WXK_ADD
1061 || keycode
== WXK_NUMPAD_ADD
1062 || keycode
== WXK_SUBTRACT
1063 || keycode
== WXK_NUMPAD_SUBTRACT
)
1065 wxGridCellTextEditor::StartingKey(event
);
1067 // skip Skip() below
1074 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1085 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1089 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1091 m_precision
= (int)tmp
;
1093 // skip the error message below
1098 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1102 wxString
wxGridCellFloatEditor::GetString() const
1105 if ( m_width
== -1 )
1107 // default width/precision
1110 else if ( m_precision
== -1 )
1112 // default precision
1113 fmt
.Printf(_T("%%%d.f"), m_width
);
1117 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1120 return wxString::Format(fmt
, m_valueOld
);
1123 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1125 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1127 int keycode
= event
.GetKeyCode();
1141 case WXK_NUMPAD_ADD
:
1143 case WXK_NUMPAD_SUBTRACT
:
1145 case WXK_NUMPAD_DECIMAL
:
1149 // additionally accept 'e' as in '1e+6'
1150 if ( (keycode
< 128) &&
1151 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1159 #endif // wxUSE_TEXTCTRL
1163 // ----------------------------------------------------------------------------
1164 // wxGridCellBoolEditor
1165 // ----------------------------------------------------------------------------
1167 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1169 wxEvtHandler
* evtHandler
)
1171 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1172 wxDefaultPosition
, wxDefaultSize
,
1175 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1178 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1180 bool resize
= FALSE
;
1181 wxSize size
= m_control
->GetSize();
1182 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1184 // check if the checkbox is not too big/small for this cell
1185 wxSize sizeBest
= m_control
->GetBestSize();
1186 if ( !(size
== sizeBest
) )
1188 // reset to default size if it had been made smaller
1194 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1196 // leave 1 pixel margin
1197 size
.x
= size
.y
= minSize
- 2;
1204 m_control
->SetSize(size
);
1207 // position it in the centre of the rectangle (TODO: support alignment?)
1209 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1210 // the checkbox without label still has some space to the right in wxGTK,
1211 // so shift it to the right
1213 #elif defined(__WXMSW__)
1214 // here too, but in other way
1219 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1222 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1224 m_control
->Show(show
);
1228 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1229 CBox()->SetBackgroundColour(colBg
);
1233 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1235 wxASSERT_MSG(m_control
,
1236 wxT("The wxGridCellEditor must be Created first!"));
1238 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1239 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1242 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1243 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1245 CBox()->SetValue(m_startValue
);
1249 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1252 wxASSERT_MSG(m_control
,
1253 wxT("The wxGridCellEditor must be Created first!"));
1255 bool changed
= FALSE
;
1256 bool value
= CBox()->GetValue();
1257 if ( value
!= m_startValue
)
1262 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1263 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1265 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1271 void wxGridCellBoolEditor::Reset()
1273 wxASSERT_MSG(m_control
,
1274 wxT("The wxGridCellEditor must be Created first!"));
1276 CBox()->SetValue(m_startValue
);
1279 void wxGridCellBoolEditor::StartingClick()
1281 CBox()->SetValue(!CBox()->GetValue());
1284 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1286 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1288 int keycode
= event
.GetKeyCode();
1292 case WXK_NUMPAD_MULTIPLY
:
1294 case WXK_NUMPAD_ADD
:
1296 case WXK_NUMPAD_SUBTRACT
:
1307 // return the value as "1" for true and the empty string for false
1308 wxString
wxGridCellBoolEditor::GetValue() const
1310 bool bSet
= CBox()->GetValue();
1311 return bSet
? _T("1") : wxEmptyString
;
1314 #endif // wxUSE_CHECKBOX
1318 // ----------------------------------------------------------------------------
1319 // wxGridCellChoiceEditor
1320 // ----------------------------------------------------------------------------
1322 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1323 const wxString choices
[],
1325 : m_allowOthers(allowOthers
)
1329 m_choices
.Alloc(count
);
1330 for ( size_t n
= 0; n
< count
; n
++ )
1332 m_choices
.Add(choices
[n
]);
1337 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1339 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1340 editor
->m_allowOthers
= m_allowOthers
;
1341 editor
->m_choices
= m_choices
;
1346 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1348 wxEvtHandler
* evtHandler
)
1350 size_t count
= m_choices
.GetCount();
1351 wxString
*choices
= new wxString
[count
];
1352 for ( size_t n
= 0; n
< count
; n
++ )
1354 choices
[n
] = m_choices
[n
];
1357 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1358 wxDefaultPosition
, wxDefaultSize
,
1360 m_allowOthers
? 0 : wxCB_READONLY
);
1364 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1367 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1368 wxGridCellAttr
* attr
)
1370 // as we fill the entire client area, don't do anything here to minimize
1373 // TODO: It doesn't actually fill the client area since the height of a
1374 // combo always defaults to the standard... Until someone has time to
1375 // figure out the right rectangle to paint, just do it the normal way...
1376 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1379 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1381 wxASSERT_MSG(m_control
,
1382 wxT("The wxGridCellEditor must be Created first!"));
1384 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1387 Combo()->SetValue(m_startValue
);
1390 // find the right position, or default to the first if not found
1391 int pos
= Combo()->FindString(m_startValue
);
1394 Combo()->SetSelection(pos
);
1396 Combo()->SetInsertionPointEnd();
1397 Combo()->SetFocus();
1400 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1403 wxString value
= Combo()->GetValue();
1404 bool changed
= value
!= m_startValue
;
1407 grid
->GetTable()->SetValue(row
, col
, value
);
1409 m_startValue
= wxEmptyString
;
1411 Combo()->SetValue(m_startValue
);
1413 Combo()->SetSelection(0);
1418 void wxGridCellChoiceEditor::Reset()
1420 Combo()->SetValue(m_startValue
);
1421 Combo()->SetInsertionPointEnd();
1424 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1434 wxStringTokenizer
tk(params
, _T(','));
1435 while ( tk
.HasMoreTokens() )
1437 m_choices
.Add(tk
.GetNextToken());
1441 // return the value in the text control
1442 wxString
wxGridCellChoiceEditor::GetValue() const
1444 return Combo()->GetValue();
1447 #endif // wxUSE_COMBOBOX
1449 // ----------------------------------------------------------------------------
1450 // wxGridCellEditorEvtHandler
1451 // ----------------------------------------------------------------------------
1453 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1455 switch ( event
.KeyCode() )
1459 m_grid
->DisableCellEditControl();
1463 m_grid
->GetEventHandler()->ProcessEvent( event
);
1467 case WXK_NUMPAD_ENTER
:
1468 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1469 m_editor
->HandleReturn(event
);
1478 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1480 switch ( event
.KeyCode() )
1485 case WXK_NUMPAD_ENTER
:
1493 // ----------------------------------------------------------------------------
1494 // wxGridCellWorker is an (almost) empty common base class for
1495 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1496 // ----------------------------------------------------------------------------
1498 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1503 wxGridCellWorker::~wxGridCellWorker()
1507 // ============================================================================
1509 // ============================================================================
1511 // ----------------------------------------------------------------------------
1512 // wxGridCellRenderer
1513 // ----------------------------------------------------------------------------
1515 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1516 wxGridCellAttr
& attr
,
1519 int WXUNUSED(row
), int WXUNUSED(col
),
1522 dc
.SetBackgroundMode( wxSOLID
);
1524 // grey out fields if the grid is disabled
1525 if( grid
.IsEnabled() )
1529 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1533 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1538 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1541 dc
.SetPen( *wxTRANSPARENT_PEN
);
1542 dc
.DrawRectangle(rect
);
1545 // ----------------------------------------------------------------------------
1546 // wxGridCellStringRenderer
1547 // ----------------------------------------------------------------------------
1549 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1550 wxGridCellAttr
& attr
,
1554 dc
.SetBackgroundMode( wxTRANSPARENT
);
1556 // TODO some special colours for attr.IsReadOnly() case?
1558 // different coloured text when the grid is disabled
1559 if( grid
.IsEnabled() )
1563 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1564 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1568 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1569 dc
.SetTextForeground( attr
.GetTextColour() );
1574 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1575 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1578 dc
.SetFont( attr
.GetFont() );
1581 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1583 const wxString
& text
)
1585 wxCoord x
= 0, y
= 0, max_x
= 0;
1586 dc
.SetFont(attr
.GetFont());
1587 wxStringTokenizer
tk(text
, _T('\n'));
1588 while ( tk
.HasMoreTokens() )
1590 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1591 max_x
= wxMax(max_x
, x
);
1594 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1596 return wxSize(max_x
, y
);
1599 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1600 wxGridCellAttr
& attr
,
1604 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1607 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1608 wxGridCellAttr
& attr
,
1610 const wxRect
& rectCell
,
1614 wxRect rect
= rectCell
;
1617 // erase only this cells background, overflow cells should have been erased
1618 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1621 attr
.GetAlignment(&hAlign
, &vAlign
);
1623 int overflowCols
= 0;
1625 if (attr
.GetOverflow())
1627 int cols
= grid
.GetNumberCols();
1628 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1629 int cell_rows
, cell_cols
;
1630 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1631 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1633 int i
, c_cols
, c_rows
;
1634 for (i
= col
+cell_cols
; i
< cols
; i
++)
1636 bool is_empty
= TRUE
;
1637 for (int j
=row
; j
<row
+cell_rows
; j
++)
1639 // check w/ anchor cell for multicell block
1640 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1641 if (c_rows
> 0) c_rows
= 0;
1642 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1649 rect
.width
+= grid
.GetColSize(i
);
1655 if (rect
.width
>= best_width
) break;
1657 overflowCols
= i
- col
- cell_cols
+ 1;
1658 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1661 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1663 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1665 clip
.x
+= rectCell
.width
;
1666 // draw each overflow cell individually
1667 int col_end
= col
+cell_cols
+overflowCols
;
1668 if (col_end
>= grid
.GetNumberCols())
1669 col_end
= grid
.GetNumberCols() - 1;
1670 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1672 clip
.width
= grid
.GetColSize(i
) - 1;
1673 dc
.DestroyClippingRegion();
1674 dc
.SetClippingRegion(clip
);
1676 SetTextColoursAndFont(grid
, attr
, dc
,
1677 grid
.IsInSelection(row
,i
));
1679 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1680 rect
, hAlign
, vAlign
);
1681 clip
.x
+= grid
.GetColSize(i
) - 1;
1687 dc
.DestroyClippingRegion();
1691 // now we only have to draw the text
1692 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1694 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1695 rect
, hAlign
, vAlign
);
1698 // ----------------------------------------------------------------------------
1699 // wxGridCellNumberRenderer
1700 // ----------------------------------------------------------------------------
1702 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1704 wxGridTableBase
*table
= grid
.GetTable();
1706 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1708 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1712 text
= table
->GetValue(row
, col
);
1718 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1719 wxGridCellAttr
& attr
,
1721 const wxRect
& rectCell
,
1725 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1727 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1729 // draw the text right aligned by default
1731 attr
.GetAlignment(&hAlign
, &vAlign
);
1732 hAlign
= wxALIGN_RIGHT
;
1734 wxRect rect
= rectCell
;
1737 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1740 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1741 wxGridCellAttr
& attr
,
1745 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1748 // ----------------------------------------------------------------------------
1749 // wxGridCellFloatRenderer
1750 // ----------------------------------------------------------------------------
1752 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1755 SetPrecision(precision
);
1758 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1760 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1761 renderer
->m_width
= m_width
;
1762 renderer
->m_precision
= m_precision
;
1763 renderer
->m_format
= m_format
;
1768 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1770 wxGridTableBase
*table
= grid
.GetTable();
1775 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1777 val
= table
->GetValueAsDouble(row
, col
);
1782 text
= table
->GetValue(row
, col
);
1783 hasDouble
= text
.ToDouble(&val
);
1790 if ( m_width
== -1 )
1792 if ( m_precision
== -1 )
1794 // default width/precision
1795 m_format
= _T("%f");
1799 m_format
.Printf(_T("%%.%df"), m_precision
);
1802 else if ( m_precision
== -1 )
1804 // default precision
1805 m_format
.Printf(_T("%%%d.f"), m_width
);
1809 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1813 text
.Printf(m_format
, val
);
1816 //else: text already contains the string
1821 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1822 wxGridCellAttr
& attr
,
1824 const wxRect
& rectCell
,
1828 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1830 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1832 // draw the text right aligned by default
1834 attr
.GetAlignment(&hAlign
, &vAlign
);
1835 hAlign
= wxALIGN_RIGHT
;
1837 wxRect rect
= rectCell
;
1840 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1843 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1844 wxGridCellAttr
& attr
,
1848 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1851 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1855 // reset to defaults
1861 wxString tmp
= params
.BeforeFirst(_T(','));
1865 if ( tmp
.ToLong(&width
) )
1867 SetWidth((int)width
);
1871 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1875 tmp
= params
.AfterFirst(_T(','));
1879 if ( tmp
.ToLong(&precision
) )
1881 SetPrecision((int)precision
);
1885 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1893 // ----------------------------------------------------------------------------
1894 // wxGridCellBoolRenderer
1895 // ----------------------------------------------------------------------------
1897 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1899 // FIXME these checkbox size calculations are really ugly...
1901 // between checkmark and box
1902 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1904 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1905 wxGridCellAttr
& WXUNUSED(attr
),
1910 // compute it only once (no locks for MT safeness in GUI thread...)
1911 if ( !ms_sizeCheckMark
.x
)
1913 // get checkbox size
1914 wxCoord checkSize
= 0;
1915 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1916 wxSize size
= checkbox
->GetBestSize();
1917 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1919 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1920 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1921 checkSize
-= size
.y
/ 2;
1926 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1929 return ms_sizeCheckMark
;
1932 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1933 wxGridCellAttr
& attr
,
1939 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1941 // draw a check mark in the centre (ignoring alignment - TODO)
1942 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1944 // don't draw outside the cell
1945 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1946 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1948 // and even leave (at least) 1 pixel margin
1949 size
.x
= size
.y
= minSize
- 2;
1952 // draw a border around checkmark
1954 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1955 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1956 rectBorder
.width
= size
.x
;
1957 rectBorder
.height
= size
.y
;
1960 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1961 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1964 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1965 value
= !( !cellval
|| (cellval
== wxT("0")) );
1970 wxRect rectMark
= rectBorder
;
1972 // MSW DrawCheckMark() is weird (and should probably be changed...)
1973 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1977 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1980 dc
.SetTextForeground(attr
.GetTextColour());
1981 dc
.DrawCheckMark(rectMark
);
1984 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1985 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1986 dc
.DrawRectangle(rectBorder
);
1989 // ----------------------------------------------------------------------------
1991 // ----------------------------------------------------------------------------
1993 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1997 m_isReadOnly
= Unset
;
2002 m_attrkind
= wxGridCellAttr::Cell
;
2004 m_sizeRows
= m_sizeCols
= 1;
2007 SetDefAttr(attrDefault
);
2010 wxGridCellAttr
*wxGridCellAttr::Clone() const
2012 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2014 if ( HasTextColour() )
2015 attr
->SetTextColour(GetTextColour());
2016 if ( HasBackgroundColour() )
2017 attr
->SetBackgroundColour(GetBackgroundColour());
2019 attr
->SetFont(GetFont());
2020 if ( HasAlignment() )
2021 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2023 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2027 attr
->SetRenderer(m_renderer
);
2028 m_renderer
->IncRef();
2032 attr
->SetEditor(m_editor
);
2037 attr
->SetReadOnly();
2039 attr
->SetKind( m_attrkind
);
2044 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2046 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2047 SetTextColour(mergefrom
->GetTextColour());
2048 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2049 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2050 if ( !HasFont() && mergefrom
->HasFont() )
2051 SetFont(mergefrom
->GetFont());
2052 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2054 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2055 SetAlignment(hAlign
, vAlign
);
2058 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2060 // Directly access member functions as GetRender/Editor don't just return
2061 // m_renderer/m_editor
2063 // Maybe add support for merge of Render and Editor?
2064 if (!HasRenderer() && mergefrom
->HasRenderer() )
2066 m_renderer
= mergefrom
->m_renderer
;
2067 m_renderer
->IncRef();
2069 if ( !HasEditor() && mergefrom
->HasEditor() )
2071 m_editor
= mergefrom
->m_editor
;
2074 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2075 SetReadOnly(mergefrom
->IsReadOnly());
2077 SetDefAttr(mergefrom
->m_defGridAttr
);
2080 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2082 // The size of a cell is normally 1,1
2084 // If this cell is larger (2,2) then this is the top left cell
2085 // the other cells that will be covered (lower right cells) must be
2086 // set to negative or zero values such that
2087 // row + num_rows of the covered cell points to the larger cell (this cell)
2088 // same goes for the col + num_cols.
2090 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2092 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2093 !((num_rows
<=0)&&(num_cols
>0)) ||
2094 !((num_rows
==0)&&(num_cols
==0))),
2095 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2097 m_sizeRows
= num_rows
;
2098 m_sizeCols
= num_cols
;
2101 const wxColour
& wxGridCellAttr::GetTextColour() const
2103 if (HasTextColour())
2107 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2109 return m_defGridAttr
->GetTextColour();
2113 wxFAIL_MSG(wxT("Missing default cell attribute"));
2114 return wxNullColour
;
2119 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2121 if (HasBackgroundColour())
2123 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2124 return m_defGridAttr
->GetBackgroundColour();
2127 wxFAIL_MSG(wxT("Missing default cell attribute"));
2128 return wxNullColour
;
2133 const wxFont
& wxGridCellAttr::GetFont() const
2137 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2138 return m_defGridAttr
->GetFont();
2141 wxFAIL_MSG(wxT("Missing default cell attribute"));
2147 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2151 if ( hAlign
) *hAlign
= m_hAlign
;
2152 if ( vAlign
) *vAlign
= m_vAlign
;
2154 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2155 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2158 wxFAIL_MSG(wxT("Missing default cell attribute"));
2162 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2164 if ( num_rows
) *num_rows
= m_sizeRows
;
2165 if ( num_cols
) *num_cols
= m_sizeCols
;
2168 // GetRenderer and GetEditor use a slightly different decision path about
2169 // which attribute to use. If a non-default attr object has one then it is
2170 // used, otherwise the default editor or renderer is fetched from the grid and
2171 // used. It should be the default for the data type of the cell. If it is
2172 // NULL (because the table has a type that the grid does not have in its
2173 // registry,) then the grid's default editor or renderer is used.
2175 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2177 wxGridCellRenderer
*renderer
;
2179 if ( m_renderer
&& this != m_defGridAttr
)
2181 // use the cells renderer if it has one
2182 renderer
= m_renderer
;
2185 else // no non default cell renderer
2187 // get default renderer for the data type
2190 // GetDefaultRendererForCell() will do IncRef() for us
2191 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2200 if (m_defGridAttr
&& this != m_defGridAttr
)
2202 // if we still don't have one then use the grid default
2203 // (no need for IncRef() here neither)
2204 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2206 else // default grid attr
2208 // use m_renderer which we had decided not to use initially
2209 renderer
= m_renderer
;
2216 // we're supposed to always find something
2217 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2222 // same as above, except for s/renderer/editor/g
2223 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2225 wxGridCellEditor
*editor
;
2227 if ( m_editor
&& this != m_defGridAttr
)
2229 // use the cells editor if it has one
2233 else // no non default cell editor
2235 // get default editor for the data type
2238 // GetDefaultEditorForCell() will do IncRef() for us
2239 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2248 if ( m_defGridAttr
&& this != m_defGridAttr
)
2250 // if we still don't have one then use the grid default
2251 // (no need for IncRef() here neither)
2252 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2254 else // default grid attr
2256 // use m_editor which we had decided not to use initially
2264 // we're supposed to always find something
2265 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2270 // ----------------------------------------------------------------------------
2271 // wxGridCellAttrData
2272 // ----------------------------------------------------------------------------
2274 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2276 int n
= FindIndex(row
, col
);
2277 if ( n
== wxNOT_FOUND
)
2279 // add the attribute
2280 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2284 // free the old attribute
2285 m_attrs
[(size_t)n
].attr
->DecRef();
2289 // change the attribute
2290 m_attrs
[(size_t)n
].attr
= attr
;
2294 // remove this attribute
2295 m_attrs
.RemoveAt((size_t)n
);
2300 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2302 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2304 int n
= FindIndex(row
, col
);
2305 if ( n
!= wxNOT_FOUND
)
2307 attr
= m_attrs
[(size_t)n
].attr
;
2314 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2316 size_t count
= m_attrs
.GetCount();
2317 for ( size_t n
= 0; n
< count
; n
++ )
2319 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2320 wxCoord row
= coords
.GetRow();
2321 if ((size_t)row
>= pos
)
2325 // If rows inserted, include row counter where necessary
2326 coords
.SetRow(row
+ numRows
);
2328 else if (numRows
< 0)
2330 // If rows deleted ...
2331 if ((size_t)row
>= pos
- numRows
)
2333 // ...either decrement row counter (if row still exists)...
2334 coords
.SetRow(row
+ numRows
);
2338 // ...or remove the attribute
2339 m_attrs
.RemoveAt((size_t)n
);
2347 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2349 size_t count
= m_attrs
.GetCount();
2350 for ( size_t n
= 0; n
< count
; n
++ )
2352 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2353 wxCoord col
= coords
.GetCol();
2354 if ( (size_t)col
>= pos
)
2358 // If rows inserted, include row counter where necessary
2359 coords
.SetCol(col
+ numCols
);
2361 else if (numCols
< 0)
2363 // If rows deleted ...
2364 if ((size_t)col
>= pos
- numCols
)
2366 // ...either decrement row counter (if row still exists)...
2367 coords
.SetCol(col
+ numCols
);
2371 // ...or remove the attribute
2372 m_attrs
.RemoveAt((size_t)n
);
2380 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2382 size_t count
= m_attrs
.GetCount();
2383 for ( size_t n
= 0; n
< count
; n
++ )
2385 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2386 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2395 // ----------------------------------------------------------------------------
2396 // wxGridRowOrColAttrData
2397 // ----------------------------------------------------------------------------
2399 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2401 size_t count
= m_attrs
.Count();
2402 for ( size_t n
= 0; n
< count
; n
++ )
2404 m_attrs
[n
]->DecRef();
2408 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2410 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2412 int n
= m_rowsOrCols
.Index(rowOrCol
);
2413 if ( n
!= wxNOT_FOUND
)
2415 attr
= m_attrs
[(size_t)n
];
2422 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2424 int i
= m_rowsOrCols
.Index(rowOrCol
);
2425 if ( i
== wxNOT_FOUND
)
2427 // add the attribute
2428 m_rowsOrCols
.Add(rowOrCol
);
2433 size_t n
= (size_t)i
;
2436 // change the attribute
2437 m_attrs
[n
]->DecRef();
2442 // remove this attribute
2443 m_attrs
[n
]->DecRef();
2444 m_rowsOrCols
.RemoveAt(n
);
2445 m_attrs
.RemoveAt(n
);
2450 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2452 size_t count
= m_attrs
.GetCount();
2453 for ( size_t n
= 0; n
< count
; n
++ )
2455 int & rowOrCol
= m_rowsOrCols
[n
];
2456 if ( (size_t)rowOrCol
>= pos
)
2458 if ( numRowsOrCols
> 0 )
2460 // If rows inserted, include row counter where necessary
2461 rowOrCol
+= numRowsOrCols
;
2463 else if ( numRowsOrCols
< 0)
2465 // If rows deleted, either decrement row counter (if row still exists)
2466 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2467 rowOrCol
+= numRowsOrCols
;
2470 m_rowsOrCols
.RemoveAt((size_t)n
);
2471 m_attrs
.RemoveAt((size_t)n
);
2479 // ----------------------------------------------------------------------------
2480 // wxGridCellAttrProvider
2481 // ----------------------------------------------------------------------------
2483 wxGridCellAttrProvider::wxGridCellAttrProvider()
2485 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2488 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2493 void wxGridCellAttrProvider::InitData()
2495 m_data
= new wxGridCellAttrProviderData
;
2498 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2499 wxGridCellAttr::wxAttrKind kind
) const
2501 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2506 case (wxGridCellAttr::Any
):
2507 //Get cached merge attributes.
2508 // Currenlty not used as no cache implemented as not mutiable
2509 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2512 //Basicaly implement old version.
2513 //Also check merge cache, so we don't have to re-merge every time..
2514 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2515 *attrrow
= (wxGridCellAttr
*)NULL
,
2516 *attrcol
= (wxGridCellAttr
*)NULL
;
2518 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2519 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2520 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2522 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2523 // Two or move are non NULL
2524 attr
= new wxGridCellAttr
;
2525 attr
->SetKind(wxGridCellAttr::Merged
);
2529 attr
->MergeWith(attrcell
);
2533 attr
->MergeWith(attrcol
);
2537 attr
->MergeWith(attrrow
);
2540 //store merge attr if cache implemented
2542 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2546 // one or none is non null return it or null.
2547 if(attrrow
) attr
= attrrow
;
2548 if(attrcol
) attr
= attrcol
;
2549 if(attrcell
) attr
= attrcell
;
2553 case (wxGridCellAttr::Cell
):
2554 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2556 case (wxGridCellAttr::Col
):
2557 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2559 case (wxGridCellAttr::Row
):
2560 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2564 // (wxGridCellAttr::Default):
2565 // (wxGridCellAttr::Merged):
2572 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2578 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2581 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2586 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2589 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2594 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2597 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2601 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2603 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2607 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2611 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2613 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2617 // ----------------------------------------------------------------------------
2618 // wxGridTypeRegistry
2619 // ----------------------------------------------------------------------------
2621 wxGridTypeRegistry::~wxGridTypeRegistry()
2623 size_t count
= m_typeinfo
.Count();
2624 for ( size_t i
= 0; i
< count
; i
++ )
2625 delete m_typeinfo
[i
];
2629 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2630 wxGridCellRenderer
* renderer
,
2631 wxGridCellEditor
* editor
)
2633 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2635 // is it already registered?
2636 int loc
= FindRegisteredDataType(typeName
);
2637 if ( loc
!= wxNOT_FOUND
)
2639 delete m_typeinfo
[loc
];
2640 m_typeinfo
[loc
] = info
;
2644 m_typeinfo
.Add(info
);
2648 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2650 size_t count
= m_typeinfo
.GetCount();
2651 for ( size_t i
= 0; i
< count
; i
++ )
2653 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2662 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2664 int index
= FindRegisteredDataType(typeName
);
2665 if ( index
== wxNOT_FOUND
)
2667 // check whether this is one of the standard ones, in which case
2668 // register it "on the fly"
2670 if ( typeName
== wxGRID_VALUE_STRING
)
2672 RegisterDataType(wxGRID_VALUE_STRING
,
2673 new wxGridCellStringRenderer
,
2674 new wxGridCellTextEditor
);
2676 #endif // wxUSE_TEXTCTRL
2678 if ( typeName
== wxGRID_VALUE_BOOL
)
2680 RegisterDataType(wxGRID_VALUE_BOOL
,
2681 new wxGridCellBoolRenderer
,
2682 new wxGridCellBoolEditor
);
2684 #endif // wxUSE_CHECKBOX
2686 if ( typeName
== wxGRID_VALUE_NUMBER
)
2688 RegisterDataType(wxGRID_VALUE_NUMBER
,
2689 new wxGridCellNumberRenderer
,
2690 new wxGridCellNumberEditor
);
2692 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2694 RegisterDataType(wxGRID_VALUE_FLOAT
,
2695 new wxGridCellFloatRenderer
,
2696 new wxGridCellFloatEditor
);
2698 #endif // wxUSE_TEXTCTRL
2700 if ( typeName
== wxGRID_VALUE_CHOICE
)
2702 RegisterDataType(wxGRID_VALUE_CHOICE
,
2703 new wxGridCellStringRenderer
,
2704 new wxGridCellChoiceEditor
);
2706 #endif // wxUSE_COMBOBOX
2711 // we get here only if just added the entry for this type, so return
2713 index
= m_typeinfo
.GetCount() - 1;
2719 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2721 int index
= FindDataType(typeName
);
2722 if ( index
== wxNOT_FOUND
)
2724 // the first part of the typename is the "real" type, anything after ':'
2725 // are the parameters for the renderer
2726 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2727 if ( index
== wxNOT_FOUND
)
2732 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2733 wxGridCellRenderer
*rendererOld
= renderer
;
2734 renderer
= renderer
->Clone();
2735 rendererOld
->DecRef();
2737 wxGridCellEditor
*editor
= GetEditor(index
);
2738 wxGridCellEditor
*editorOld
= editor
;
2739 editor
= editor
->Clone();
2740 editorOld
->DecRef();
2742 // do it even if there are no parameters to reset them to defaults
2743 wxString params
= typeName
.AfterFirst(_T(':'));
2744 renderer
->SetParameters(params
);
2745 editor
->SetParameters(params
);
2747 // register the new typename
2748 RegisterDataType(typeName
, renderer
, editor
);
2750 // we just registered it, it's the last one
2751 index
= m_typeinfo
.GetCount() - 1;
2757 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2759 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2765 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2767 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2773 // ----------------------------------------------------------------------------
2775 // ----------------------------------------------------------------------------
2777 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2780 wxGridTableBase::wxGridTableBase()
2782 m_view
= (wxGrid
*) NULL
;
2783 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2786 wxGridTableBase::~wxGridTableBase()
2788 delete m_attrProvider
;
2791 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2793 delete m_attrProvider
;
2794 m_attrProvider
= attrProvider
;
2797 bool wxGridTableBase::CanHaveAttributes()
2799 if ( ! GetAttrProvider() )
2801 // use the default attr provider by default
2802 SetAttrProvider(new wxGridCellAttrProvider
);
2807 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2809 if ( m_attrProvider
)
2810 return m_attrProvider
->GetAttr(row
, col
, kind
);
2812 return (wxGridCellAttr
*)NULL
;
2815 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2817 if ( m_attrProvider
)
2819 attr
->SetKind(wxGridCellAttr::Cell
);
2820 m_attrProvider
->SetAttr(attr
, row
, col
);
2824 // as we take ownership of the pointer and don't store it, we must
2830 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2832 if ( m_attrProvider
)
2834 attr
->SetKind(wxGridCellAttr::Row
);
2835 m_attrProvider
->SetRowAttr(attr
, row
);
2839 // as we take ownership of the pointer and don't store it, we must
2845 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2847 if ( m_attrProvider
)
2849 attr
->SetKind(wxGridCellAttr::Col
);
2850 m_attrProvider
->SetColAttr(attr
, col
);
2854 // as we take ownership of the pointer and don't store it, we must
2860 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2861 size_t WXUNUSED(numRows
) )
2863 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2868 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2870 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2875 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2876 size_t WXUNUSED(numRows
) )
2878 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2883 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2884 size_t WXUNUSED(numCols
) )
2886 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2891 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2893 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2898 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2899 size_t WXUNUSED(numCols
) )
2901 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2907 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2910 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2911 // how much it makes sense to us geeks.
2915 wxString
wxGridTableBase::GetColLabelValue( int col
)
2917 // default col labels are:
2918 // cols 0 to 25 : A-Z
2919 // cols 26 to 675 : AA-ZZ
2924 for ( n
= 1; ; n
++ )
2926 s
+= (_T('A') + (wxChar
)( col%26
));
2928 if ( col
< 0 ) break;
2931 // reverse the string...
2933 for ( i
= 0; i
< n
; i
++ )
2942 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2944 return wxGRID_VALUE_STRING
;
2947 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2948 const wxString
& typeName
)
2950 return typeName
== wxGRID_VALUE_STRING
;
2953 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2955 return CanGetValueAs(row
, col
, typeName
);
2958 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2963 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2968 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2973 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2974 long WXUNUSED(value
) )
2978 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2979 double WXUNUSED(value
) )
2983 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2984 bool WXUNUSED(value
) )
2989 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2990 const wxString
& WXUNUSED(typeName
) )
2995 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2996 const wxString
& WXUNUSED(typeName
),
2997 void* WXUNUSED(value
) )
3001 //////////////////////////////////////////////////////////////////////
3003 // Message class for the grid table to send requests and notifications
3007 wxGridTableMessage::wxGridTableMessage()
3009 m_table
= (wxGridTableBase
*) NULL
;
3015 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3016 int commandInt1
, int commandInt2
)
3020 m_comInt1
= commandInt1
;
3021 m_comInt2
= commandInt2
;
3026 //////////////////////////////////////////////////////////////////////
3028 // A basic grid table for string data. An object of this class will
3029 // created by wxGrid if you don't specify an alternative table class.
3032 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3034 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3036 wxGridStringTable::wxGridStringTable()
3041 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3044 m_data
.Alloc( numRows
);
3047 sa
.Alloc( numCols
);
3048 sa
.Add( wxEmptyString
, numCols
);
3050 m_data
.Add( sa
, numRows
);
3053 wxGridStringTable::~wxGridStringTable()
3057 int wxGridStringTable::GetNumberRows()
3059 return m_data
.GetCount();
3062 int wxGridStringTable::GetNumberCols()
3064 if ( m_data
.GetCount() > 0 )
3065 return m_data
[0].GetCount();
3070 wxString
wxGridStringTable::GetValue( int row
, int col
)
3072 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3073 _T("invalid row or column index in wxGridStringTable") );
3075 return m_data
[row
][col
];
3078 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3080 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3081 _T("invalid row or column index in wxGridStringTable") );
3083 m_data
[row
][col
] = value
;
3086 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3088 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3089 _T("invalid row or column index in wxGridStringTable") );
3091 return (m_data
[row
][col
] == wxEmptyString
);
3094 void wxGridStringTable::Clear()
3097 int numRows
, numCols
;
3099 numRows
= m_data
.GetCount();
3102 numCols
= m_data
[0].GetCount();
3104 for ( row
= 0; row
< numRows
; row
++ )
3106 for ( col
= 0; col
< numCols
; col
++ )
3108 m_data
[row
][col
] = wxEmptyString
;
3115 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3117 size_t curNumRows
= m_data
.GetCount();
3118 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3119 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3121 if ( pos
>= curNumRows
)
3123 return AppendRows( numRows
);
3127 sa
.Alloc( curNumCols
);
3128 sa
.Add( wxEmptyString
, curNumCols
);
3129 m_data
.Insert( sa
, pos
, numRows
);
3132 wxGridTableMessage
msg( this,
3133 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3137 GetView()->ProcessTableMessage( msg
);
3143 bool wxGridStringTable::AppendRows( size_t numRows
)
3145 size_t curNumRows
= m_data
.GetCount();
3146 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3147 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3150 if ( curNumCols
> 0 )
3152 sa
.Alloc( curNumCols
);
3153 sa
.Add( wxEmptyString
, curNumCols
);
3156 m_data
.Add( sa
, numRows
);
3160 wxGridTableMessage
msg( this,
3161 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3164 GetView()->ProcessTableMessage( msg
);
3170 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3172 size_t curNumRows
= m_data
.GetCount();
3174 if ( pos
>= curNumRows
)
3176 wxFAIL_MSG( wxString::Format
3178 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3180 (unsigned long)numRows
,
3181 (unsigned long)curNumRows
3187 if ( numRows
> curNumRows
- pos
)
3189 numRows
= curNumRows
- pos
;
3192 if ( numRows
>= curNumRows
)
3198 m_data
.RemoveAt( pos
, numRows
);
3202 wxGridTableMessage
msg( this,
3203 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3207 GetView()->ProcessTableMessage( msg
);
3213 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3217 size_t curNumRows
= m_data
.GetCount();
3218 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3219 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3221 if ( pos
>= curNumCols
)
3223 return AppendCols( numCols
);
3226 for ( row
= 0; row
< curNumRows
; row
++ )
3228 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3230 m_data
[row
].Insert( wxEmptyString
, col
);
3235 wxGridTableMessage
msg( this,
3236 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3240 GetView()->ProcessTableMessage( msg
);
3246 bool wxGridStringTable::AppendCols( size_t numCols
)
3250 size_t curNumRows
= m_data
.GetCount();
3254 // TODO: something better than this ?
3256 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3261 for ( row
= 0; row
< curNumRows
; row
++ )
3263 m_data
[row
].Add( wxEmptyString
, numCols
);
3268 wxGridTableMessage
msg( this,
3269 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3272 GetView()->ProcessTableMessage( msg
);
3278 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3282 size_t curNumRows
= m_data
.GetCount();
3283 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3284 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3286 if ( pos
>= curNumCols
)
3288 wxFAIL_MSG( wxString::Format
3290 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3292 (unsigned long)numCols
,
3293 (unsigned long)curNumCols
3298 if ( numCols
> curNumCols
- pos
)
3300 numCols
= curNumCols
- pos
;
3303 for ( row
= 0; row
< curNumRows
; row
++ )
3305 if ( numCols
>= curNumCols
)
3307 m_data
[row
].Clear();
3311 m_data
[row
].RemoveAt( pos
, numCols
);
3316 wxGridTableMessage
msg( this,
3317 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3321 GetView()->ProcessTableMessage( msg
);
3327 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3329 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3331 // using default label
3333 return wxGridTableBase::GetRowLabelValue( row
);
3337 return m_rowLabels
[ row
];
3341 wxString
wxGridStringTable::GetColLabelValue( int col
)
3343 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3345 // using default label
3347 return wxGridTableBase::GetColLabelValue( col
);
3351 return m_colLabels
[ col
];
3355 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3357 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3359 int n
= m_rowLabels
.GetCount();
3361 for ( i
= n
; i
<= row
; i
++ )
3363 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3367 m_rowLabels
[row
] = value
;
3370 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3372 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3374 int n
= m_colLabels
.GetCount();
3376 for ( i
= n
; i
<= col
; i
++ )
3378 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3382 m_colLabels
[col
] = value
;
3387 //////////////////////////////////////////////////////////////////////
3388 //////////////////////////////////////////////////////////////////////
3390 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3392 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3393 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3394 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3395 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3396 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3397 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3400 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3402 const wxPoint
&pos
, const wxSize
&size
)
3403 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3408 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3412 // NO - don't do this because it will set both the x and y origin
3413 // coords to match the parent scrolled window and we just want to
3414 // set the y coord - MB
3416 // m_owner->PrepareDC( dc );
3419 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3420 dc
.SetDeviceOrigin( 0, -y
);
3422 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3423 m_owner
->DrawRowLabels( dc
, rows
);
3427 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3429 m_owner
->ProcessRowLabelMouseEvent( event
);
3433 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3435 m_owner
->GetEventHandler()->ProcessEvent(event
);
3439 // This seems to be required for wxMotif otherwise the mouse
3440 // cursor must be in the cell edit control to get key events
3442 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3444 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3447 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3449 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3454 //////////////////////////////////////////////////////////////////////
3456 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3458 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3459 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3460 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3461 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3462 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3463 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3466 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3468 const wxPoint
&pos
, const wxSize
&size
)
3469 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3474 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3478 // NO - don't do this because it will set both the x and y origin
3479 // coords to match the parent scrolled window and we just want to
3480 // set the x coord - MB
3482 // m_owner->PrepareDC( dc );
3485 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3486 dc
.SetDeviceOrigin( -x
, 0 );
3488 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3489 m_owner
->DrawColLabels( dc
, cols
);
3493 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3495 m_owner
->ProcessColLabelMouseEvent( event
);
3498 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3500 m_owner
->GetEventHandler()->ProcessEvent(event
);
3504 // This seems to be required for wxMotif otherwise the mouse
3505 // cursor must be in the cell edit control to get key events
3507 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3509 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3512 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3514 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3519 //////////////////////////////////////////////////////////////////////
3521 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3523 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3524 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3525 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3526 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3527 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3528 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3531 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3533 const wxPoint
&pos
, const wxSize
&size
)
3534 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3539 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3543 int client_height
= 0;
3544 int client_width
= 0;
3545 GetClientSize( &client_width
, &client_height
);
3547 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3548 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3549 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3550 dc
.DrawLine( 0, 0, client_width
, 0 );
3551 dc
.DrawLine( 0, 0, 0, client_height
);
3553 dc
.SetPen( *wxWHITE_PEN
);
3554 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3555 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3559 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3561 m_owner
->ProcessCornerLabelMouseEvent( event
);
3565 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3567 m_owner
->GetEventHandler()->ProcessEvent(event
);
3570 // This seems to be required for wxMotif otherwise the mouse
3571 // cursor must be in the cell edit control to get key events
3573 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3575 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3578 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3580 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3585 //////////////////////////////////////////////////////////////////////
3587 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3589 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3590 EVT_PAINT( wxGridWindow::OnPaint
)
3591 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3592 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3593 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3594 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3595 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3598 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3599 wxGridRowLabelWindow
*rowLblWin
,
3600 wxGridColLabelWindow
*colLblWin
,
3603 const wxSize
&size
)
3604 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxCLIP_CHILDREN
,
3605 wxT("grid window") )
3609 m_rowLabelWin
= rowLblWin
;
3610 m_colLabelWin
= colLblWin
;
3611 SetBackgroundColour(_T("WHITE"));
3615 wxGridWindow::~wxGridWindow()
3620 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3622 wxPaintDC
dc( this );
3623 m_owner
->PrepareDC( dc
);
3624 wxRegion reg
= GetUpdateRegion();
3625 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3626 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3627 #if WXGRID_DRAW_LINES
3628 m_owner
->DrawAllGridLines( dc
, reg
);
3630 m_owner
->DrawGridSpace( dc
);
3631 m_owner
->DrawHighlight( dc
, DirtyCells
);
3635 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3637 wxWindow::ScrollWindow( dx
, dy
, rect
);
3638 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3639 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3643 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3645 m_owner
->ProcessGridCellMouseEvent( event
);
3648 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3650 m_owner
->GetEventHandler()->ProcessEvent(event
);
3653 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3654 // cursor must be in the cell edit control to get key events
3656 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3658 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3661 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3663 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3666 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3671 //////////////////////////////////////////////////////////////////////
3673 // Internal Helper function for computing row or column from some
3674 // (unscrolled) coordinate value, using either
3675 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3676 // of m_rowBottoms/m_ColRights to speed up the search!
3678 // Internal helper macros for simpler use of that function
3680 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3681 const wxArrayInt
& BorderArray
, int nMax
,
3682 bool maxOnOverflow
);
3684 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3685 WXGRID_MIN_COL_WIDTH, \
3686 m_colRights, m_numCols, TRUE)
3687 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3688 WXGRID_MIN_ROW_HEIGHT, \
3689 m_rowBottoms, m_numRows, TRUE)
3690 /////////////////////////////////////////////////////////////////////
3692 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3694 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3695 EVT_PAINT( wxGrid::OnPaint
)
3696 EVT_SIZE( wxGrid::OnSize
)
3697 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3698 EVT_KEY_UP( wxGrid::OnKeyUp
)
3699 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3702 wxGrid::wxGrid( wxWindow
*parent
,
3707 const wxString
& name
)
3708 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3709 m_colMinWidths(GRID_HASH_SIZE
),
3710 m_rowMinHeights(GRID_HASH_SIZE
)
3718 // Must do this or ~wxScrollHelper will pop the wrong event handler
3719 SetTargetWindow(this);
3721 wxSafeDecRef(m_defaultCellAttr
);
3723 #ifdef DEBUG_ATTR_CACHE
3724 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3725 wxPrintf(_T("wxGrid attribute cache statistics: "
3726 "total: %u, hits: %u (%u%%)\n"),
3727 total
, gs_nAttrCacheHits
,
3728 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3734 delete m_typeRegistry
;
3740 // ----- internal init and update functions
3743 void wxGrid::Create()
3745 m_created
= FALSE
; // set to TRUE by CreateGrid
3747 m_table
= (wxGridTableBase
*) NULL
;
3750 m_cellEditCtrlEnabled
= FALSE
;
3752 m_defaultCellAttr
= new wxGridCellAttr();
3754 // Set default cell attributes
3755 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3756 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3757 m_defaultCellAttr
->SetFont(GetFont());
3758 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3759 m_defaultCellAttr
->SetTextColour(
3760 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3761 m_defaultCellAttr
->SetBackgroundColour(
3762 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3763 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3764 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3769 m_currentCellCoords
= wxGridNoCellCoords
;
3771 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3772 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3774 // create the type registry
3775 m_typeRegistry
= new wxGridTypeRegistry
;
3778 // subwindow components that make up the wxGrid
3779 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3784 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3789 m_colLabelWin
= new wxGridColLabelWindow( this,
3794 m_gridWin
= new wxGridWindow( this,
3801 SetTargetWindow( m_gridWin
);
3807 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3808 wxGrid::wxGridSelectionModes selmode
)
3810 wxCHECK_MSG( !m_created
,
3812 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3814 m_numRows
= numRows
;
3815 m_numCols
= numCols
;
3817 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3818 m_table
->SetView( this );
3820 m_selection
= new wxGridSelection( this, selmode
);
3829 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3831 wxCHECK_RET( m_created
,
3832 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3834 m_selection
->SetSelectionMode( selmode
);
3837 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3839 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3840 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3842 return m_selection
->GetSelectionMode();
3845 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3846 wxGrid::wxGridSelectionModes selmode
)
3850 // RD: Actually, this should probably be allowed. I think it would be
3851 // nice to be able to switch multiple Tables in and out of a single
3852 // View at runtime. Is there anything in the implementation that
3853 // would prevent this?
3855 // At least, you now have to cope with m_selection
3856 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3861 m_numRows
= table
->GetNumberRows();
3862 m_numCols
= table
->GetNumberCols();
3865 m_table
->SetView( this );
3868 m_selection
= new wxGridSelection( this, selmode
);
3881 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3882 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3884 if ( m_rowLabelWin
)
3886 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3890 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3893 m_labelTextColour
= wxColour( _T("BLACK") );
3896 m_attrCache
.row
= -1;
3897 m_attrCache
.col
= -1;
3898 m_attrCache
.attr
= NULL
;
3900 // TODO: something better than this ?
3902 m_labelFont
= this->GetFont();
3903 // m_labelFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3904 // m_labelFont.SetWeight( m_labelFont.GetWeight() + 2 );
3906 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
3907 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3909 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3910 m_colLabelVertAlign
= wxALIGN_CENTRE
;
3912 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3913 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3915 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3916 m_defaultRowHeight
+= 8;
3918 m_defaultRowHeight
+= 4;
3921 m_gridLineColour
= wxColour( 192,192,192 );
3922 m_gridLinesEnabled
= TRUE
;
3923 m_cellHighlightColour
= *wxBLACK
;
3924 m_cellHighlightPenWidth
= 2;
3925 m_cellHighlightROPenWidth
= 1;
3927 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3928 m_winCapture
= (wxWindow
*)NULL
;
3929 m_canDragRowSize
= TRUE
;
3930 m_canDragColSize
= TRUE
;
3931 m_canDragGridSize
= TRUE
;
3933 m_dragRowOrCol
= -1;
3934 m_isDragging
= FALSE
;
3935 m_startDragPos
= wxDefaultPosition
;
3937 m_waitForSlowClick
= FALSE
;
3939 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3940 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3942 m_currentCellCoords
= wxGridNoCellCoords
;
3944 m_selectingTopLeft
= wxGridNoCellCoords
;
3945 m_selectingBottomRight
= wxGridNoCellCoords
;
3946 // m_selectionBackground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
3947 // m_selectionForeground = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
3948 m_selectionBackground
= *wxBLACK
;
3949 m_selectionForeground
= *wxWHITE
;
3951 m_editable
= TRUE
; // default for whole grid
3953 m_inOnKeyDown
= FALSE
;
3960 // ----------------------------------------------------------------------------
3961 // the idea is to call these functions only when necessary because they create
3962 // quite big arrays which eat memory mostly unnecessary - in particular, if
3963 // default widths/heights are used for all rows/columns, we may not use these
3966 // with some extra code, it should be possible to only store the
3967 // widths/heights different from default ones but this will be done later...
3968 // ----------------------------------------------------------------------------
3970 void wxGrid::InitRowHeights()
3972 m_rowHeights
.Empty();
3973 m_rowBottoms
.Empty();
3975 m_rowHeights
.Alloc( m_numRows
);
3976 m_rowBottoms
.Alloc( m_numRows
);
3980 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
3982 for ( int i
= 0; i
< m_numRows
; i
++ )
3984 rowBottom
+= m_defaultRowHeight
;
3985 m_rowBottoms
.Add( rowBottom
);
3989 void wxGrid::InitColWidths()
3991 m_colWidths
.Empty();
3992 m_colRights
.Empty();
3994 m_colWidths
.Alloc( m_numCols
);
3995 m_colRights
.Alloc( m_numCols
);
3998 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4000 for ( int i
= 0; i
< m_numCols
; i
++ )
4002 colRight
+= m_defaultColWidth
;
4003 m_colRights
.Add( colRight
);
4007 int wxGrid::GetColWidth(int col
) const
4009 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4012 int wxGrid::GetColLeft(int col
) const
4014 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4015 : m_colRights
[col
] - m_colWidths
[col
];
4018 int wxGrid::GetColRight(int col
) const
4020 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4024 int wxGrid::GetRowHeight(int row
) const
4026 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4029 int wxGrid::GetRowTop(int row
) const
4031 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4032 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4035 int wxGrid::GetRowBottom(int row
) const
4037 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4038 : m_rowBottoms
[row
];
4041 void wxGrid::CalcDimensions()
4044 GetClientSize( &cw
, &ch
);
4046 if ( m_rowLabelWin
->IsShown() )
4047 cw
-= m_rowLabelWidth
;
4048 if ( m_colLabelWin
->IsShown() )
4049 ch
-= m_colLabelHeight
;
4052 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4053 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4055 // take into account editor if shown
4056 if( IsCellEditControlShown() )
4059 int r
= m_currentCellCoords
.GetRow();
4060 int c
= m_currentCellCoords
.GetCol();
4061 int x
= GetColLeft(c
);
4062 int y
= GetRowTop(r
);
4064 // how big is the editor
4065 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4066 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4067 editor
->GetControl()->GetSize(&w2
, &h2
);
4070 if( w2
> w
) w
= w2
;
4071 if( h2
> h
) h
= h2
;
4076 // preserve (more or less) the previous position
4078 GetViewStart( &x
, &y
);
4080 // maybe we don't need scrollbars at all?
4082 // also adjust the position to be valid for the new scroll rangs
4103 // do set scrollbar parameters
4104 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4105 GetScrollX(w
), GetScrollY(h
), x
, y
,
4106 GetBatchCount() != 0);
4108 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4109 // still must reposition the children
4114 void wxGrid::CalcWindowSizes()
4117 GetClientSize( &cw
, &ch
);
4119 if ( m_cornerLabelWin
->IsShown() )
4120 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4122 if ( m_colLabelWin
->IsShown() )
4123 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4125 if ( m_rowLabelWin
->IsShown() )
4126 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4128 if ( m_gridWin
->IsShown() )
4129 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4133 // this is called when the grid table sends a message to say that it
4134 // has been redimensioned
4136 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4139 bool result
= FALSE
;
4141 // Clear the attribute cache as the attribute might refer to a different
4142 // cell than stored in the cache after adding/removing rows/columns.
4144 // By the same reasoning, the editor should be dismissed if columns are
4145 // added or removed. And for consistency, it should IMHO always be
4146 // removed, not only if the cell "underneath" it actually changes.
4147 // For now, I intentionally do not save the editor's content as the
4148 // cell it might want to save that stuff to might no longer exist.
4149 HideCellEditControl();
4151 // if we were using the default widths/heights so far, we must change them
4153 if ( m_colWidths
.IsEmpty() )
4158 if ( m_rowHeights
.IsEmpty() )
4164 switch ( msg
.GetId() )
4166 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4168 size_t pos
= msg
.GetCommandInt();
4169 int numRows
= msg
.GetCommandInt2();
4171 m_numRows
+= numRows
;
4173 if ( !m_rowHeights
.IsEmpty() )
4175 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4176 m_rowBottoms
.Insert( 0, pos
, numRows
);
4179 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4181 for ( i
= pos
; i
< m_numRows
; i
++ )
4183 bottom
+= m_rowHeights
[i
];
4184 m_rowBottoms
[i
] = bottom
;
4187 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4189 // if we have just inserted cols into an empty grid the current
4190 // cell will be undefined...
4192 SetCurrentCell( 0, 0 );
4196 m_selection
->UpdateRows( pos
, numRows
);
4197 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4199 attrProvider
->UpdateAttrRows( pos
, numRows
);
4201 if ( !GetBatchCount() )
4204 m_rowLabelWin
->Refresh();
4210 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4212 int numRows
= msg
.GetCommandInt();
4213 int oldNumRows
= m_numRows
;
4214 m_numRows
+= numRows
;
4216 if ( !m_rowHeights
.IsEmpty() )
4218 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4219 m_rowBottoms
.Add( 0, numRows
);
4222 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4224 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4226 bottom
+= m_rowHeights
[i
];
4227 m_rowBottoms
[i
] = bottom
;
4230 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4232 // if we have just inserted cols into an empty grid the current
4233 // cell will be undefined...
4235 SetCurrentCell( 0, 0 );
4237 if ( !GetBatchCount() )
4240 m_rowLabelWin
->Refresh();
4246 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4248 size_t pos
= msg
.GetCommandInt();
4249 int numRows
= msg
.GetCommandInt2();
4250 m_numRows
-= numRows
;
4252 if ( !m_rowHeights
.IsEmpty() )
4254 m_rowHeights
.RemoveAt( pos
, numRows
);
4255 m_rowBottoms
.RemoveAt( pos
, numRows
);
4258 for ( i
= 0; i
< m_numRows
; i
++ )
4260 h
+= m_rowHeights
[i
];
4261 m_rowBottoms
[i
] = h
;
4266 m_currentCellCoords
= wxGridNoCellCoords
;
4270 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4271 m_currentCellCoords
.Set( 0, 0 );
4275 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4276 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4278 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4279 // ifdef'd out following patch from Paul Gammans
4281 // No need to touch column attributes, unless we
4282 // removed _all_ rows, in this case, we remove
4283 // all column attributes.
4284 // I hate to do this here, but the
4285 // needed data is not available inside UpdateAttrRows.
4286 if ( !GetNumberRows() )
4287 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4290 if ( !GetBatchCount() )
4293 m_rowLabelWin
->Refresh();
4299 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4301 size_t pos
= msg
.GetCommandInt();
4302 int numCols
= msg
.GetCommandInt2();
4303 m_numCols
+= numCols
;
4305 if ( !m_colWidths
.IsEmpty() )
4307 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4308 m_colRights
.Insert( 0, pos
, numCols
);
4311 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4313 for ( i
= pos
; i
< m_numCols
; i
++ )
4315 right
+= m_colWidths
[i
];
4316 m_colRights
[i
] = right
;
4319 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4321 // if we have just inserted cols into an empty grid the current
4322 // cell will be undefined...
4324 SetCurrentCell( 0, 0 );
4328 m_selection
->UpdateCols( pos
, numCols
);
4329 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4331 attrProvider
->UpdateAttrCols( pos
, numCols
);
4332 if ( !GetBatchCount() )
4335 m_colLabelWin
->Refresh();
4342 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4344 int numCols
= msg
.GetCommandInt();
4345 int oldNumCols
= m_numCols
;
4346 m_numCols
+= numCols
;
4347 if ( !m_colWidths
.IsEmpty() )
4349 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4350 m_colRights
.Add( 0, numCols
);
4353 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4355 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4357 right
+= m_colWidths
[i
];
4358 m_colRights
[i
] = right
;
4361 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4363 // if we have just inserted cols into an empty grid the current
4364 // cell will be undefined...
4366 SetCurrentCell( 0, 0 );
4368 if ( !GetBatchCount() )
4371 m_colLabelWin
->Refresh();
4377 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4379 size_t pos
= msg
.GetCommandInt();
4380 int numCols
= msg
.GetCommandInt2();
4381 m_numCols
-= numCols
;
4383 if ( !m_colWidths
.IsEmpty() )
4385 m_colWidths
.RemoveAt( pos
, numCols
);
4386 m_colRights
.RemoveAt( pos
, numCols
);
4389 for ( i
= 0; i
< m_numCols
; i
++ )
4391 w
+= m_colWidths
[i
];
4397 m_currentCellCoords
= wxGridNoCellCoords
;
4401 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4402 m_currentCellCoords
.Set( 0, 0 );
4406 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4407 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4409 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4410 // ifdef'd out following patch from Paul Gammans
4412 // No need to touch row attributes, unless we
4413 // removed _all_ columns, in this case, we remove
4414 // all row attributes.
4415 // I hate to do this here, but the
4416 // needed data is not available inside UpdateAttrCols.
4417 if ( !GetNumberCols() )
4418 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4421 if ( !GetBatchCount() )
4424 m_colLabelWin
->Refresh();
4431 if (result
&& !GetBatchCount() )
4432 m_gridWin
->Refresh();
4437 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4439 wxRegionIterator
iter( reg
);
4442 wxArrayInt rowlabels
;
4449 // TODO: remove this when we can...
4450 // There is a bug in wxMotif that gives garbage update
4451 // rectangles if you jump-scroll a long way by clicking the
4452 // scrollbar with middle button. This is a work-around
4454 #if defined(__WXMOTIF__)
4456 m_gridWin
->GetClientSize( &cw
, &ch
);
4457 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4458 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4461 // logical bounds of update region
4464 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4465 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4467 // find the row labels within these bounds
4470 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4472 if ( GetRowBottom(row
) < top
)
4475 if ( GetRowTop(row
) > bottom
)
4478 rowlabels
.Add( row
);
4488 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4490 wxRegionIterator
iter( reg
);
4493 wxArrayInt colLabels
;
4500 // TODO: remove this when we can...
4501 // There is a bug in wxMotif that gives garbage update
4502 // rectangles if you jump-scroll a long way by clicking the
4503 // scrollbar with middle button. This is a work-around
4505 #if defined(__WXMOTIF__)
4507 m_gridWin
->GetClientSize( &cw
, &ch
);
4508 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4509 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4512 // logical bounds of update region
4515 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4516 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4518 // find the cells within these bounds
4521 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4523 if ( GetColRight(col
) < left
)
4526 if ( GetColLeft(col
) > right
)
4529 colLabels
.Add( col
);
4538 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4540 wxRegionIterator
iter( reg
);
4543 wxGridCellCoordsArray cellsExposed
;
4545 int left
, top
, right
, bottom
;
4550 // TODO: remove this when we can...
4551 // There is a bug in wxMotif that gives garbage update
4552 // rectangles if you jump-scroll a long way by clicking the
4553 // scrollbar with middle button. This is a work-around
4555 #if defined(__WXMOTIF__)
4557 m_gridWin
->GetClientSize( &cw
, &ch
);
4558 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4559 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4560 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4561 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4564 // logical bounds of update region
4566 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4567 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4569 // find the cells within these bounds
4572 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4574 if ( GetRowBottom(row
) <= top
)
4577 if ( GetRowTop(row
) > bottom
)
4580 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4582 if ( GetColRight(col
) <= left
)
4585 if ( GetColLeft(col
) > right
)
4588 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4595 return cellsExposed
;
4599 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4602 wxPoint
pos( event
.GetPosition() );
4603 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4605 if ( event
.Dragging() )
4609 m_isDragging
= TRUE
;
4610 m_rowLabelWin
->CaptureMouse();
4613 if ( event
.LeftIsDown() )
4615 switch( m_cursorMode
)
4617 case WXGRID_CURSOR_RESIZE_ROW
:
4619 int cw
, ch
, left
, dummy
;
4620 m_gridWin
->GetClientSize( &cw
, &ch
);
4621 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4623 wxClientDC
dc( m_gridWin
);
4626 GetRowTop(m_dragRowOrCol
) +
4627 GetRowMinimalHeight(m_dragRowOrCol
) );
4628 dc
.SetLogicalFunction(wxINVERT
);
4629 if ( m_dragLastPos
>= 0 )
4631 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4633 dc
.DrawLine( left
, y
, left
+cw
, y
);
4638 case WXGRID_CURSOR_SELECT_ROW
:
4639 if ( (row
= YToRow( y
)) >= 0 )
4643 m_selection
->SelectRow( row
,
4644 event
.ControlDown(),
4651 // default label to suppress warnings about "enumeration value
4652 // 'xxx' not handled in switch
4660 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4665 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4666 m_isDragging
= FALSE
;
4669 // ------------ Entering or leaving the window
4671 if ( event
.Entering() || event
.Leaving() )
4673 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4677 // ------------ Left button pressed
4679 else if ( event
.LeftDown() )
4681 // don't send a label click event for a hit on the
4682 // edge of the row label - this is probably the user
4683 // wanting to resize the row
4685 if ( YToEdgeOfRow(y
) < 0 )
4689 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4691 if ( !event
.ShiftDown() && !event
.ControlDown() )
4695 if ( event
.ShiftDown() )
4697 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4700 GetNumberCols() - 1,
4701 event
.ControlDown(),
4708 m_selection
->SelectRow( row
,
4709 event
.ControlDown(),
4716 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4721 // starting to drag-resize a row
4723 if ( CanDragRowSize() )
4724 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4729 // ------------ Left double click
4731 else if (event
.LeftDClick() )
4733 if ( YToEdgeOfRow(y
) < 0 )
4736 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4741 // ------------ Left button released
4743 else if ( event
.LeftUp() )
4745 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4747 DoEndDragResizeRow();
4749 // Note: we are ending the event *after* doing
4750 // default processing in this case
4752 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4755 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4760 // ------------ Right button down
4762 else if ( event
.RightDown() )
4765 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4767 // no default action at the moment
4772 // ------------ Right double click
4774 else if ( event
.RightDClick() )
4777 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4779 // no default action at the moment
4784 // ------------ No buttons down and mouse moving
4786 else if ( event
.Moving() )
4788 m_dragRowOrCol
= YToEdgeOfRow( y
);
4789 if ( m_dragRowOrCol
>= 0 )
4791 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4793 // don't capture the mouse yet
4794 if ( CanDragRowSize() )
4795 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4798 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4800 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4806 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4809 wxPoint
pos( event
.GetPosition() );
4810 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4812 if ( event
.Dragging() )
4816 m_isDragging
= TRUE
;
4817 m_colLabelWin
->CaptureMouse();
4820 if ( event
.LeftIsDown() )
4822 switch( m_cursorMode
)
4824 case WXGRID_CURSOR_RESIZE_COL
:
4826 int cw
, ch
, dummy
, top
;
4827 m_gridWin
->GetClientSize( &cw
, &ch
);
4828 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4830 wxClientDC
dc( m_gridWin
);
4833 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4834 GetColMinimalWidth(m_dragRowOrCol
));
4835 dc
.SetLogicalFunction(wxINVERT
);
4836 if ( m_dragLastPos
>= 0 )
4838 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4840 dc
.DrawLine( x
, top
, x
, top
+ch
);
4845 case WXGRID_CURSOR_SELECT_COL
:
4846 if ( (col
= XToCol( x
)) >= 0 )
4850 m_selection
->SelectCol( col
,
4851 event
.ControlDown(),
4858 // default label to suppress warnings about "enumeration value
4859 // 'xxx' not handled in switch
4867 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4872 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4873 m_isDragging
= FALSE
;
4876 // ------------ Entering or leaving the window
4878 if ( event
.Entering() || event
.Leaving() )
4880 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4884 // ------------ Left button pressed
4886 else if ( event
.LeftDown() )
4888 // don't send a label click event for a hit on the
4889 // edge of the col label - this is probably the user
4890 // wanting to resize the col
4892 if ( XToEdgeOfCol(x
) < 0 )
4896 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4898 if ( !event
.ShiftDown() && !event
.ControlDown() )
4902 if ( event
.ShiftDown() )
4904 m_selection
->SelectBlock( 0,
4905 m_currentCellCoords
.GetCol(),
4906 GetNumberRows() - 1, col
,
4907 event
.ControlDown(),
4914 m_selection
->SelectCol( col
,
4915 event
.ControlDown(),
4922 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4927 // starting to drag-resize a col
4929 if ( CanDragColSize() )
4930 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4935 // ------------ Left double click
4937 if ( event
.LeftDClick() )
4939 if ( XToEdgeOfCol(x
) < 0 )
4942 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4947 // ------------ Left button released
4949 else if ( event
.LeftUp() )
4951 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4953 DoEndDragResizeCol();
4955 // Note: we are ending the event *after* doing
4956 // default processing in this case
4958 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4966 // ------------ Right button down
4968 else if ( event
.RightDown() )
4971 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4973 // no default action at the moment
4978 // ------------ Right double click
4980 else if ( event
.RightDClick() )
4983 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4985 // no default action at the moment
4990 // ------------ No buttons down and mouse moving
4992 else if ( event
.Moving() )
4994 m_dragRowOrCol
= XToEdgeOfCol( x
);
4995 if ( m_dragRowOrCol
>= 0 )
4997 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4999 // don't capture the cursor yet
5000 if ( CanDragColSize() )
5001 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5004 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5006 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5012 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5014 if ( event
.LeftDown() )
5016 // indicate corner label by having both row and
5019 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5025 else if ( event
.LeftDClick() )
5027 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5030 else if ( event
.RightDown() )
5032 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5034 // no default action at the moment
5038 else if ( event
.RightDClick() )
5040 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5042 // no default action at the moment
5047 void wxGrid::ChangeCursorMode(CursorMode mode
,
5052 static const wxChar
*cursorModes
[] =
5061 wxLogTrace(_T("grid"),
5062 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5063 win
== m_colLabelWin
? _T("colLabelWin")
5064 : win
? _T("rowLabelWin")
5066 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5067 #endif // __WXDEBUG__
5069 if ( mode
== m_cursorMode
&&
5070 win
== m_winCapture
&&
5071 captureMouse
== (m_winCapture
!= NULL
))
5076 // by default use the grid itself
5082 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5083 m_winCapture
= (wxWindow
*)NULL
;
5086 m_cursorMode
= mode
;
5088 switch ( m_cursorMode
)
5090 case WXGRID_CURSOR_RESIZE_ROW
:
5091 win
->SetCursor( m_rowResizeCursor
);
5094 case WXGRID_CURSOR_RESIZE_COL
:
5095 win
->SetCursor( m_colResizeCursor
);
5099 win
->SetCursor( *wxSTANDARD_CURSOR
);
5102 // we need to capture mouse when resizing
5103 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5104 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5106 if ( captureMouse
&& resize
)
5108 win
->CaptureMouse();
5113 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5116 wxPoint
pos( event
.GetPosition() );
5117 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5119 wxGridCellCoords coords
;
5120 XYToCell( x
, y
, coords
);
5122 int cell_rows
, cell_cols
;
5123 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5124 if ((cell_rows
< 0) || (cell_cols
< 0))
5126 coords
.SetRow(coords
.GetRow() + cell_rows
);
5127 coords
.SetCol(coords
.GetCol() + cell_cols
);
5130 if ( event
.Dragging() )
5132 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5134 // Don't start doing anything until the mouse has been drug at
5135 // least 3 pixels in any direction...
5138 if (m_startDragPos
== wxDefaultPosition
)
5140 m_startDragPos
= pos
;
5143 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5147 m_isDragging
= TRUE
;
5148 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5150 // Hide the edit control, so it
5151 // won't interfer with drag-shrinking.
5152 if ( IsCellEditControlShown() )
5154 HideCellEditControl();
5155 SaveEditControlValue();
5158 // Have we captured the mouse yet?
5161 m_winCapture
= m_gridWin
;
5162 m_winCapture
->CaptureMouse();
5165 if ( coords
!= wxGridNoCellCoords
)
5167 if ( event
.ControlDown() )
5169 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5170 m_selectingKeyboard
= coords
;
5171 HighlightBlock ( m_selectingKeyboard
, coords
);
5175 if ( !IsSelection() )
5177 HighlightBlock( coords
, coords
);
5181 HighlightBlock( m_currentCellCoords
, coords
);
5185 if (! IsVisible(coords
))
5187 MakeCellVisible(coords
);
5188 // TODO: need to introduce a delay or something here. The
5189 // scrolling is way to fast, at least on MSW - also on GTK.
5193 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5195 int cw
, ch
, left
, dummy
;
5196 m_gridWin
->GetClientSize( &cw
, &ch
);
5197 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5199 wxClientDC
dc( m_gridWin
);
5201 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5202 GetRowMinimalHeight(m_dragRowOrCol
) );
5203 dc
.SetLogicalFunction(wxINVERT
);
5204 if ( m_dragLastPos
>= 0 )
5206 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5208 dc
.DrawLine( left
, y
, left
+cw
, y
);
5211 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5213 int cw
, ch
, dummy
, top
;
5214 m_gridWin
->GetClientSize( &cw
, &ch
);
5215 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5217 wxClientDC
dc( m_gridWin
);
5219 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5220 GetColMinimalWidth(m_dragRowOrCol
) );
5221 dc
.SetLogicalFunction(wxINVERT
);
5222 if ( m_dragLastPos
>= 0 )
5224 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5226 dc
.DrawLine( x
, top
, x
, top
+ch
);
5233 m_isDragging
= FALSE
;
5234 m_startDragPos
= wxDefaultPosition
;
5236 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5237 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5240 if ( event
.Entering() || event
.Leaving() )
5242 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5243 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5248 // ------------ Left button pressed
5250 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5252 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5257 if ( !event
.ControlDown() )
5259 if ( event
.ShiftDown() )
5263 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5264 m_currentCellCoords
.GetCol(),
5267 event
.ControlDown(),
5273 else if ( XToEdgeOfCol(x
) < 0 &&
5274 YToEdgeOfRow(y
) < 0 )
5276 DisableCellEditControl();
5277 MakeCellVisible( coords
);
5279 if ( event
.ControlDown() )
5283 m_selection
->ToggleCellSelection( coords
.GetRow(),
5285 event
.ControlDown(),
5290 m_selectingTopLeft
= wxGridNoCellCoords
;
5291 m_selectingBottomRight
= wxGridNoCellCoords
;
5292 m_selectingKeyboard
= coords
;
5296 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5297 SetCurrentCell( coords
);
5300 if ( m_selection
->GetSelectionMode() !=
5301 wxGrid::wxGridSelectCells
)
5303 HighlightBlock( coords
, coords
);
5312 // ------------ Left double click
5314 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5316 DisableCellEditControl();
5318 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5320 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5328 // ------------ Left button released
5330 else if ( event
.LeftUp() )
5332 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5334 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5335 m_selectingBottomRight
!= wxGridNoCellCoords
)
5339 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5340 m_winCapture
= NULL
;
5345 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5346 m_selectingTopLeft
.GetCol(),
5347 m_selectingBottomRight
.GetRow(),
5348 m_selectingBottomRight
.GetCol(),
5349 event
.ControlDown(),
5355 m_selectingTopLeft
= wxGridNoCellCoords
;
5356 m_selectingBottomRight
= wxGridNoCellCoords
;
5358 // Show the edit control, if it has been hidden for
5360 ShowCellEditControl();
5364 if( m_waitForSlowClick
&& CanEnableCellControl())
5366 EnableCellEditControl();
5368 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5369 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5370 editor
->StartingClick();
5374 m_waitForSlowClick
= FALSE
;
5378 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5380 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5381 DoEndDragResizeRow();
5383 // Note: we are ending the event *after* doing
5384 // default processing in this case
5386 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5388 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5390 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5391 DoEndDragResizeCol();
5393 // Note: we are ending the event *after* doing
5394 // default processing in this case
5396 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5403 // ------------ Right button down
5405 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5407 DisableCellEditControl();
5408 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5413 // no default action at the moment
5418 // ------------ Right double click
5420 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5422 DisableCellEditControl();
5423 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5428 // no default action at the moment
5432 // ------------ Moving and no button action
5434 else if ( event
.Moving() && !event
.IsButton() )
5436 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5438 // out of grid cell area
5439 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5443 int dragRow
= YToEdgeOfRow( y
);
5444 int dragCol
= XToEdgeOfCol( x
);
5446 // Dragging on the corner of a cell to resize in both
5447 // directions is not implemented yet...
5449 if ( dragRow
>= 0 && dragCol
>= 0 )
5451 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5457 m_dragRowOrCol
= dragRow
;
5459 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5461 if ( CanDragRowSize() && CanDragGridSize() )
5462 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5467 m_dragRowOrCol
= dragCol
;
5475 m_dragRowOrCol
= dragCol
;
5477 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5479 if ( CanDragColSize() && CanDragGridSize() )
5480 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5486 // Neither on a row or col edge
5488 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5490 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5496 void wxGrid::DoEndDragResizeRow()
5498 if ( m_dragLastPos
>= 0 )
5500 // erase the last line and resize the row
5502 int cw
, ch
, left
, dummy
;
5503 m_gridWin
->GetClientSize( &cw
, &ch
);
5504 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5506 wxClientDC
dc( m_gridWin
);
5508 dc
.SetLogicalFunction( wxINVERT
);
5509 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5510 HideCellEditControl();
5511 SaveEditControlValue();
5513 int rowTop
= GetRowTop(m_dragRowOrCol
);
5514 SetRowSize( m_dragRowOrCol
,
5515 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5517 if ( !GetBatchCount() )
5519 // Only needed to get the correct rect.y:
5520 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5522 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5523 rect
.width
= m_rowLabelWidth
;
5524 rect
.height
= ch
- rect
.y
;
5525 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5527 // if there is a multicell block, paint all of it
5530 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5531 int leftCol
= XToCol(left
);
5532 int rightCol
= XToCol(left
+cw
);
5535 if (rightCol
< 0) rightCol
= m_numCols
;
5536 for (i
=leftCol
; i
<rightCol
; i
++)
5538 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5539 if (cell_rows
< subtract_rows
)
5540 subtract_rows
= cell_rows
;
5542 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5543 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5544 rect
.height
= ch
- rect
.y
;
5547 m_gridWin
->Refresh( FALSE
, &rect
);
5550 ShowCellEditControl();
5555 void wxGrid::DoEndDragResizeCol()
5557 if ( m_dragLastPos
>= 0 )
5559 // erase the last line and resize the col
5561 int cw
, ch
, dummy
, top
;
5562 m_gridWin
->GetClientSize( &cw
, &ch
);
5563 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5565 wxClientDC
dc( m_gridWin
);
5567 dc
.SetLogicalFunction( wxINVERT
);
5568 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5569 HideCellEditControl();
5570 SaveEditControlValue();
5572 int colLeft
= GetColLeft(m_dragRowOrCol
);
5573 SetColSize( m_dragRowOrCol
,
5574 wxMax( m_dragLastPos
- colLeft
,
5575 GetColMinimalWidth(m_dragRowOrCol
) ) );
5577 if ( !GetBatchCount() )
5579 // Only needed to get the correct rect.x:
5580 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5582 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5583 rect
.width
= cw
- rect
.x
;
5584 rect
.height
= m_colLabelHeight
;
5585 m_colLabelWin
->Refresh( TRUE
, &rect
);
5587 // if there is a multicell block, paint all of it
5590 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5591 int topRow
= YToRow(top
);
5592 int bottomRow
= YToRow(top
+cw
);
5595 if (bottomRow
< 0) bottomRow
= m_numRows
;
5596 for (i
=topRow
; i
<bottomRow
; i
++)
5598 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5599 if (cell_cols
< subtract_cols
)
5600 subtract_cols
= cell_cols
;
5602 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5603 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5604 rect
.width
= cw
- rect
.x
;
5607 m_gridWin
->Refresh( FALSE
, &rect
);
5610 ShowCellEditControl();
5617 // ------ interaction with data model
5619 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5621 switch ( msg
.GetId() )
5623 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5624 return GetModelValues();
5626 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5627 return SetModelValues();
5629 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5630 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5631 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5632 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5633 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5634 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5635 return Redimension( msg
);
5644 // The behaviour of this function depends on the grid table class
5645 // Clear() function. For the default wxGridStringTable class the
5646 // behavious is to replace all cell contents with wxEmptyString but
5647 // not to change the number of rows or cols.
5649 void wxGrid::ClearGrid()
5653 if (IsCellEditControlEnabled())
5654 DisableCellEditControl();
5657 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5662 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5664 // TODO: something with updateLabels flag
5668 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5674 if (IsCellEditControlEnabled())
5675 DisableCellEditControl();
5677 return m_table
->InsertRows( pos
, numRows
);
5679 // the table will have sent the results of the insert row
5680 // operation to this view object as a grid table message
5686 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5688 // TODO: something with updateLabels flag
5692 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5696 return ( m_table
&& m_table
->AppendRows( numRows
) );
5697 // the table will have sent the results of the append row
5698 // operation to this view object as a grid table message
5702 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5704 // TODO: something with updateLabels flag
5708 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5714 if (IsCellEditControlEnabled())
5715 DisableCellEditControl();
5717 return (m_table
->DeleteRows( pos
, numRows
));
5718 // the table will have sent the results of the delete row
5719 // operation to this view object as a grid table message
5725 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5727 // TODO: something with updateLabels flag
5731 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5737 if (IsCellEditControlEnabled())
5738 DisableCellEditControl();
5740 return m_table
->InsertCols( pos
, numCols
);
5741 // the table will have sent the results of the insert col
5742 // operation to this view object as a grid table message
5748 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5750 // TODO: something with updateLabels flag
5754 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5758 return ( m_table
&& m_table
->AppendCols( numCols
) );
5759 // the table will have sent the results of the append col
5760 // operation to this view object as a grid table message
5764 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5766 // TODO: something with updateLabels flag
5770 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5776 if (IsCellEditControlEnabled())
5777 DisableCellEditControl();
5779 return ( m_table
->DeleteCols( pos
, numCols
) );
5780 // the table will have sent the results of the delete col
5781 // operation to this view object as a grid table message
5789 // ----- event handlers
5792 // Generate a grid event based on a mouse event and
5793 // return the result of ProcessEvent()
5795 int wxGrid::SendEvent( const wxEventType type
,
5797 wxMouseEvent
& mouseEv
)
5802 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5804 int rowOrCol
= (row
== -1 ? col
: row
);
5806 wxGridSizeEvent
gridEvt( GetId(),
5810 mouseEv
.GetX() + GetRowLabelSize(),
5811 mouseEv
.GetY() + GetColLabelSize(),
5812 mouseEv
.ControlDown(),
5813 mouseEv
.ShiftDown(),
5815 mouseEv
.MetaDown() );
5817 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5818 vetoed
= !gridEvt
.IsAllowed();
5820 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5822 // Right now, it should _never_ end up here!
5823 wxGridRangeSelectEvent
gridEvt( GetId(),
5827 m_selectingBottomRight
,
5829 mouseEv
.ControlDown(),
5830 mouseEv
.ShiftDown(),
5832 mouseEv
.MetaDown() );
5834 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5835 vetoed
= !gridEvt
.IsAllowed();
5839 wxGridEvent
gridEvt( GetId(),
5843 mouseEv
.GetX() + GetRowLabelSize(),
5844 mouseEv
.GetY() + GetColLabelSize(),
5846 mouseEv
.ControlDown(),
5847 mouseEv
.ShiftDown(),
5849 mouseEv
.MetaDown() );
5850 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5851 vetoed
= !gridEvt
.IsAllowed();
5854 // A Veto'd event may not be `claimed' so test this first
5855 if (vetoed
) return -1;
5856 return claimed
? 1 : 0;
5860 // Generate a grid event of specified type and return the result
5861 // of ProcessEvent().
5863 int wxGrid::SendEvent( const wxEventType type
,
5869 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5871 int rowOrCol
= (row
== -1 ? col
: row
);
5873 wxGridSizeEvent
gridEvt( GetId(),
5878 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5879 vetoed
= !gridEvt
.IsAllowed();
5883 wxGridEvent
gridEvt( GetId(),
5888 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5889 vetoed
= !gridEvt
.IsAllowed();
5892 // A Veto'd event may not be `claimed' so test this first
5893 if (vetoed
) return -1;
5894 return claimed
? 1 : 0;
5898 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5900 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5903 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5905 // Don't do anything if between Begin/EndBatch...
5906 // EndBatch() will do all this on the last nested one anyway.
5907 if (! GetBatchCount())
5909 // Refresh to get correct scrolled position:
5910 wxScrolledWindow::Refresh(eraseb
,rect
);
5914 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5915 int width_label
, width_cell
, height_label
, height_cell
;
5918 //Copy rectangle can get scroll offsets..
5919 rect_x
= rect
->GetX();
5920 rect_y
= rect
->GetY();
5921 rectWidth
= rect
->GetWidth();
5922 rectHeight
= rect
->GetHeight();
5924 width_label
= m_rowLabelWidth
- rect_x
;
5925 if (width_label
> rectWidth
) width_label
= rectWidth
;
5927 height_label
= m_colLabelHeight
- rect_y
;
5928 if (height_label
> rectHeight
) height_label
= rectHeight
;
5930 if (rect_x
> m_rowLabelWidth
)
5932 x
= rect_x
- m_rowLabelWidth
;
5933 width_cell
= rectWidth
;
5938 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
5941 if (rect_y
> m_colLabelHeight
)
5943 y
= rect_y
- m_colLabelHeight
;
5944 height_cell
= rectHeight
;
5949 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
5952 // Paint corner label part intersecting rect.
5953 if ( width_label
> 0 && height_label
> 0 )
5955 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
5956 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
5959 // Paint col labels part intersecting rect.
5960 if ( width_cell
> 0 && height_label
> 0 )
5962 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
5963 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
5966 // Paint row labels part intersecting rect.
5967 if ( width_label
> 0 && height_cell
> 0 )
5969 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
5970 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
5973 // Paint cell area part intersecting rect.
5974 if ( width_cell
> 0 && height_cell
> 0 )
5976 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
5977 m_gridWin
->Refresh(eraseb
, &anotherrect
);
5982 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
5983 m_colLabelWin
->Refresh(eraseb
, NULL
);
5984 m_rowLabelWin
->Refresh(eraseb
, NULL
);
5985 m_gridWin
->Refresh(eraseb
, NULL
);
5990 void wxGrid::OnSize( wxSizeEvent
& event
)
5992 // position the child windows
5995 // don't call CalcDimensions() from here, the base class handles the size
6001 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6003 if ( m_inOnKeyDown
)
6005 // shouldn't be here - we are going round in circles...
6007 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6010 m_inOnKeyDown
= TRUE
;
6012 // propagate the event up and see if it gets processed
6014 wxWindow
*parent
= GetParent();
6015 wxKeyEvent
keyEvt( event
);
6016 keyEvt
.SetEventObject( parent
);
6018 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6021 // try local handlers
6023 switch ( event
.KeyCode() )
6026 if ( event
.ControlDown() )
6028 MoveCursorUpBlock( event
.ShiftDown() );
6032 MoveCursorUp( event
.ShiftDown() );
6037 if ( event
.ControlDown() )
6039 MoveCursorDownBlock( event
.ShiftDown() );
6043 MoveCursorDown( event
.ShiftDown() );
6048 if ( event
.ControlDown() )
6050 MoveCursorLeftBlock( event
.ShiftDown() );
6054 MoveCursorLeft( event
.ShiftDown() );
6059 if ( event
.ControlDown() )
6061 MoveCursorRightBlock( event
.ShiftDown() );
6065 MoveCursorRight( event
.ShiftDown() );
6070 case WXK_NUMPAD_ENTER
:
6071 if ( event
.ControlDown() )
6073 event
.Skip(); // to let the edit control have the return
6077 if ( GetGridCursorRow() < GetNumberRows()-1 )
6079 MoveCursorDown( event
.ShiftDown() );
6083 // at the bottom of a column
6084 HideCellEditControl();
6085 SaveEditControlValue();
6095 if (event
.ShiftDown())
6097 if ( GetGridCursorCol() > 0 )
6099 MoveCursorLeft( FALSE
);
6104 HideCellEditControl();
6105 SaveEditControlValue();
6110 if ( GetGridCursorCol() < GetNumberCols()-1 )
6112 MoveCursorRight( FALSE
);
6117 HideCellEditControl();
6118 SaveEditControlValue();
6124 if ( event
.ControlDown() )
6126 MakeCellVisible( 0, 0 );
6127 SetCurrentCell( 0, 0 );
6136 if ( event
.ControlDown() )
6138 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6139 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6156 if ( event
.ControlDown() )
6160 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6161 m_currentCellCoords
.GetCol(),
6162 event
.ControlDown(),
6169 if ( !IsEditable() )
6171 MoveCursorRight( FALSE
);
6174 // Otherwise fall through to default
6177 // is it possible to edit the current cell at all?
6178 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6180 // yes, now check whether the cells editor accepts the key
6181 int row
= m_currentCellCoords
.GetRow();
6182 int col
= m_currentCellCoords
.GetCol();
6183 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6184 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6186 // <F2> is special and will always start editing, for
6187 // other keys - ask the editor itself
6188 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6189 || editor
->IsAcceptedKey(event
) )
6191 // ensure cell is visble
6192 MakeCellVisible(row
, col
);
6193 EnableCellEditControl();
6195 // a problem can arise if the cell is not completely
6196 // visible (even after calling MakeCellVisible the
6197 // control is not created and calling StartingKey will
6199 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6211 // let others process char events with modifiers or all
6212 // char events for readonly cells
6219 m_inOnKeyDown
= FALSE
;
6222 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6224 // try local handlers
6226 if ( event
.KeyCode() == WXK_SHIFT
)
6228 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6229 m_selectingBottomRight
!= wxGridNoCellCoords
)
6233 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6234 m_selectingTopLeft
.GetCol(),
6235 m_selectingBottomRight
.GetRow(),
6236 m_selectingBottomRight
.GetCol(),
6237 event
.ControlDown(),
6244 m_selectingTopLeft
= wxGridNoCellCoords
;
6245 m_selectingBottomRight
= wxGridNoCellCoords
;
6246 m_selectingKeyboard
= wxGridNoCellCoords
;
6250 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6254 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6256 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6258 // the event has been intercepted - do nothing
6262 wxClientDC
dc(m_gridWin
);
6265 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6267 HideCellEditControl();
6268 DisableCellEditControl();
6270 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6273 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6274 if ( !m_gridLinesEnabled
)
6282 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6284 // Otherwise refresh redraws the highlight!
6285 m_currentCellCoords
= coords
;
6287 DrawGridCellArea(dc
,cells
);
6288 DrawAllGridLines( dc
, r
);
6292 m_currentCellCoords
= coords
;
6294 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6295 DrawCellHighlight(dc
, attr
);
6300 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6303 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6307 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6310 rightCol
= GetNumberCols() - 1;
6312 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6315 bottomRow
= GetNumberRows() - 1;
6319 if ( topRow
> bottomRow
)
6326 if ( leftCol
> rightCol
)
6333 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6334 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6336 // First the case that we selected a completely new area
6337 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6338 m_selectingBottomRight
== wxGridNoCellCoords
)
6341 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6342 wxGridCellCoords ( bottomRow
, rightCol
) );
6343 m_gridWin
->Refresh( FALSE
, &rect
);
6345 // Now handle changing an existing selection area.
6346 else if ( m_selectingTopLeft
!= updateTopLeft
||
6347 m_selectingBottomRight
!= updateBottomRight
)
6349 // Compute two optimal update rectangles:
6350 // Either one rectangle is a real subset of the
6351 // other, or they are (almost) disjoint!
6353 bool need_refresh
[4];
6357 need_refresh
[3] = FALSE
;
6360 // Store intermediate values
6361 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6362 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6363 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6364 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6366 // Determine the outer/inner coordinates.
6367 if (oldLeft
> leftCol
)
6373 if (oldTop
> topRow
)
6379 if (oldRight
< rightCol
)
6382 oldRight
= rightCol
;
6385 if (oldBottom
< bottomRow
)
6388 oldBottom
= bottomRow
;
6392 // Now, either the stuff marked old is the outer
6393 // rectangle or we don't have a situation where one
6394 // is contained in the other.
6396 if ( oldLeft
< leftCol
)
6398 // Refresh the newly selected or deselected
6399 // area to the left of the old or new selection.
6400 need_refresh
[0] = TRUE
;
6401 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6403 wxGridCellCoords ( oldBottom
,
6407 if ( oldTop
< topRow
)
6409 // Refresh the newly selected or deselected
6410 // area above the old or new selection.
6411 need_refresh
[1] = TRUE
;
6412 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6414 wxGridCellCoords ( topRow
- 1,
6418 if ( oldRight
> rightCol
)
6420 // Refresh the newly selected or deselected
6421 // area to the right of the old or new selection.
6422 need_refresh
[2] = TRUE
;
6423 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6425 wxGridCellCoords ( oldBottom
,
6429 if ( oldBottom
> bottomRow
)
6431 // Refresh the newly selected or deselected
6432 // area below the old or new selection.
6433 need_refresh
[3] = TRUE
;
6434 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6436 wxGridCellCoords ( oldBottom
,
6440 // various Refresh() calls
6441 for (i
= 0; i
< 4; i
++ )
6442 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6443 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6446 m_selectingTopLeft
= updateTopLeft
;
6447 m_selectingBottomRight
= updateBottomRight
;
6451 // ------ functions to get/send data (see also public functions)
6454 bool wxGrid::GetModelValues()
6456 // Hide the editor, so it won't hide a changed value.
6457 HideCellEditControl();
6461 // all we need to do is repaint the grid
6463 m_gridWin
->Refresh();
6471 bool wxGrid::SetModelValues()
6475 // Disable the editor, so it won't hide a changed value.
6476 // Do we also want to save the current value of the editor first?
6478 DisableCellEditControl();
6482 for ( row
= 0; row
< m_numRows
; row
++ )
6484 for ( col
= 0; col
< m_numCols
; col
++ )
6486 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6498 // Note - this function only draws cells that are in the list of
6499 // exposed cells (usually set from the update region by
6500 // CalcExposedCells)
6502 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6504 if ( !m_numRows
|| !m_numCols
) return;
6506 int i
, numCells
= cells
.GetCount();
6507 int row
, col
, cell_rows
, cell_cols
;
6508 wxGridCellCoordsArray redrawCells
;
6510 for ( i
= numCells
-1; i
>= 0; i
-- )
6512 row
= cells
[i
].GetRow();
6513 col
= cells
[i
].GetCol();
6514 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6516 // If this cell is part of a multicell block, find owner for repaint
6517 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6519 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6520 bool marked
= FALSE
;
6521 for ( int j
= 0; j
< numCells
; j
++ )
6523 if ( cell
== cells
[j
] )
6531 int count
= redrawCells
.GetCount();
6532 for (int j
= 0; j
< count
; j
++)
6534 if ( cell
== redrawCells
[j
] )
6540 if (!marked
) redrawCells
.Add( cell
);
6542 continue; // don't bother drawing this cell
6545 // If this cell is empty, find cell to left that might want to overflow
6546 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6548 for ( int l
= 0; l
< cell_rows
; l
++ )
6550 // find a cell in this row to left alreay marked for repaint
6552 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6553 if ((redrawCells
[k
].GetCol() < left
) &&
6554 (redrawCells
[k
].GetRow() == row
))
6555 left
=redrawCells
[k
].GetCol();
6557 if (left
== col
) left
= 0; // oh well
6559 for (int j
= col
-1; j
>= left
; j
--)
6561 if (!m_table
->IsEmptyCell(row
+l
, j
))
6563 if (GetCellOverflow(row
+l
, j
))
6565 wxGridCellCoords
cell(row
+l
, j
);
6566 bool marked
= FALSE
;
6568 for (int k
= 0; k
< numCells
; k
++)
6570 if ( cell
== cells
[k
] )
6578 int count
= redrawCells
.GetCount();
6579 for (int k
= 0; k
< count
; k
++)
6581 if ( cell
== redrawCells
[k
] )
6587 if (!marked
) redrawCells
.Add( cell
);
6595 DrawCell( dc
, cells
[i
] );
6598 numCells
= redrawCells
.GetCount();
6600 for ( i
= numCells
- 1; i
>= 0; i
-- )
6602 DrawCell( dc
, redrawCells
[i
] );
6607 void wxGrid::DrawGridSpace( wxDC
& dc
)
6610 m_gridWin
->GetClientSize( &cw
, &ch
);
6613 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6615 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6616 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6618 if ( right
> rightCol
|| bottom
> bottomRow
)
6621 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6623 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6624 dc
.SetPen( *wxTRANSPARENT_PEN
);
6626 if ( right
> rightCol
)
6628 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6631 if ( bottom
> bottomRow
)
6633 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6639 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6641 int row
= coords
.GetRow();
6642 int col
= coords
.GetCol();
6644 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6647 // we draw the cell border ourselves
6648 #if !WXGRID_DRAW_LINES
6649 if ( m_gridLinesEnabled
)
6650 DrawCellBorder( dc
, coords
);
6653 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6655 bool isCurrent
= coords
== m_currentCellCoords
;
6657 wxRect rect
= CellToRect( row
, col
);
6659 // if the editor is shown, we should use it and not the renderer
6660 // Note: However, only if it is really _shown_, i.e. not hidden!
6661 if ( isCurrent
&& IsCellEditControlShown() )
6663 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6664 editor
->PaintBackground(rect
, attr
);
6669 // but all the rest is drawn by the cell renderer and hence may be
6671 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6672 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6679 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6681 int row
= m_currentCellCoords
.GetRow();
6682 int col
= m_currentCellCoords
.GetCol();
6684 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6687 wxRect rect
= CellToRect(row
, col
);
6689 // hmmm... what could we do here to show that the cell is disabled?
6690 // for now, I just draw a thinner border than for the other ones, but
6691 // it doesn't look really good
6693 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6697 // The center of th drawn line is where the position/width/height of
6698 // the rectangle is actually at, (on wxMSW atr least,) so we will
6699 // reduce the size of the rectangle to compensate for the thickness of
6700 // the line. If this is too strange on non wxMSW platforms then
6701 // please #ifdef this appropriately.
6702 rect
.x
+= penWidth
/2;
6703 rect
.y
+= penWidth
/2;
6704 rect
.width
-= penWidth
-1;
6705 rect
.height
-= penWidth
-1;
6708 // Now draw the rectangle
6709 // use the cellHighlightColour if the cell is inside a selection, this
6710 // will ensure the cell is always visible.
6711 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6712 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6713 dc
.DrawRectangle(rect
);
6717 // VZ: my experiments with 3d borders...
6719 // how to properly set colours for arbitrary bg?
6720 wxCoord x1
= rect
.x
,
6722 x2
= rect
.x
+ rect
.width
-1,
6723 y2
= rect
.y
+ rect
.height
-1;
6725 dc
.SetPen(*wxWHITE_PEN
);
6726 dc
.DrawLine(x1
, y1
, x2
, y1
);
6727 dc
.DrawLine(x1
, y1
, x1
, y2
);
6729 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6730 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6732 dc
.SetPen(*wxBLACK_PEN
);
6733 dc
.DrawLine(x1
, y2
, x2
, y2
);
6734 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6739 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6741 int row
= coords
.GetRow();
6742 int col
= coords
.GetCol();
6743 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6746 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6748 wxRect rect
= CellToRect( row
, col
);
6750 // right hand border
6752 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6753 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6757 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6758 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6761 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6763 // This if block was previously in wxGrid::OnPaint but that doesn't
6764 // seem to get called under wxGTK - MB
6766 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6767 m_numRows
&& m_numCols
)
6769 m_currentCellCoords
.Set(0, 0);
6772 if ( IsCellEditControlShown() )
6774 // don't show highlight when the edit control is shown
6778 // if the active cell was repainted, repaint its highlight too because it
6779 // might have been damaged by the grid lines
6780 size_t count
= cells
.GetCount();
6781 for ( size_t n
= 0; n
< count
; n
++ )
6783 if ( cells
[n
] == m_currentCellCoords
)
6785 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6786 DrawCellHighlight(dc
, attr
);
6794 // TODO: remove this ???
6795 // This is used to redraw all grid lines e.g. when the grid line colour
6798 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6800 #if !WXGRID_DRAW_LINES
6804 if ( !m_gridLinesEnabled
||
6806 !m_numCols
) return;
6808 int top
, bottom
, left
, right
;
6810 #if 0 //#ifndef __WXGTK__
6814 m_gridWin
->GetClientSize(&cw
, &ch
);
6816 // virtual coords of visible area
6818 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6819 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6824 reg
.GetBox(x
, y
, w
, h
);
6825 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6826 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6830 m_gridWin
->GetClientSize(&cw
, &ch
);
6831 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6832 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6835 // avoid drawing grid lines past the last row and col
6837 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6838 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6840 // no gridlines inside multicells, clip them out
6841 int leftCol
= XToCol(left
);
6842 int topRow
= YToRow(top
);
6843 int rightCol
= XToCol(right
);
6844 int bottomRow
= YToRow(bottom
);
6845 wxRegion
clippedcells(0, 0, cw
, ch
);
6847 if ((leftCol
>= 0) && (topRow
>= 0))
6849 if (rightCol
< 0) rightCol
= m_numCols
;
6850 if (bottomRow
< 0) bottomRow
= m_numRows
;
6852 int i
, j
, cell_rows
, cell_cols
;
6855 for (j
=topRow
; j
<bottomRow
; j
++)
6857 for (i
=leftCol
; i
<rightCol
; i
++)
6859 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6860 if ((cell_rows
> 1) || (cell_cols
> 1))
6862 rect
= CellToRect(j
,i
);
6863 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6864 clippedcells
.Subtract(rect
);
6866 else if ((cell_rows
< 0) || (cell_cols
< 0))
6868 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6869 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6870 clippedcells
.Subtract(rect
);
6875 dc
.SetClippingRegion( clippedcells
);
6877 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6879 // horizontal grid lines
6882 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6884 int bot
= GetRowBottom(i
) - 1;
6893 dc
.DrawLine( left
, bot
, right
, bot
);
6898 // vertical grid lines
6900 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6902 int colRight
= GetColRight(i
) - 1;
6903 if ( colRight
> right
)
6908 if ( colRight
>= left
)
6910 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6913 dc
.DestroyClippingRegion();
6917 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6919 if ( !m_numRows
) return;
6922 size_t numLabels
= rows
.GetCount();
6924 for ( i
= 0; i
< numLabels
; i
++ )
6926 DrawRowLabel( dc
, rows
[i
] );
6931 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6933 if ( GetRowHeight(row
) <= 0 )
6936 int rowTop
= GetRowTop(row
),
6937 rowBottom
= GetRowBottom(row
) - 1;
6939 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
6940 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6941 m_rowLabelWidth
-1, rowBottom
);
6943 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6945 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
6947 dc
.SetPen( *wxWHITE_PEN
);
6948 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
6949 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
6951 dc
.SetBackgroundMode( wxTRANSPARENT
);
6952 dc
.SetTextForeground( GetLabelTextColour() );
6953 dc
.SetFont( GetLabelFont() );
6956 GetRowLabelAlignment( &hAlign
, &vAlign
);
6960 rect
.SetY( GetRowTop(row
) + 2 );
6961 rect
.SetWidth( m_rowLabelWidth
- 4 );
6962 rect
.SetHeight( GetRowHeight(row
) - 4 );
6963 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6967 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6969 if ( !m_numCols
) return;
6972 size_t numLabels
= cols
.GetCount();
6974 for ( i
= 0; i
< numLabels
; i
++ )
6976 DrawColLabel( dc
, cols
[i
] );
6981 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6983 if ( GetColWidth(col
) <= 0 )
6986 int colLeft
= GetColLeft(col
),
6987 colRight
= GetColRight(col
) - 1;
6989 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
6990 dc
.DrawLine( colRight
, 0,
6991 colRight
, m_colLabelHeight
-1 );
6993 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6995 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6996 colRight
+1, m_colLabelHeight
-1 );
6998 dc
.SetPen( *wxWHITE_PEN
);
6999 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7000 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7002 dc
.SetBackgroundMode( wxTRANSPARENT
);
7003 dc
.SetTextForeground( GetLabelTextColour() );
7004 dc
.SetFont( GetLabelFont() );
7006 dc
.SetBackgroundMode( wxTRANSPARENT
);
7007 dc
.SetTextForeground( GetLabelTextColour() );
7008 dc
.SetFont( GetLabelFont() );
7011 GetColLabelAlignment( &hAlign
, &vAlign
);
7014 rect
.SetX( colLeft
+ 2 );
7016 rect
.SetWidth( GetColWidth(col
) - 4 );
7017 rect
.SetHeight( m_colLabelHeight
- 4 );
7018 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
7021 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7022 const wxString
& value
,
7027 wxArrayString lines
;
7029 StringToLines( value
, lines
);
7032 //Forward to new API.
7033 DrawTextRectangle( dc
,
7041 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7042 const wxArrayString
& lines
,
7047 long textWidth
, textHeight
;
7048 long lineWidth
, lineHeight
;
7051 dc
.SetClippingRegion( rect
);
7053 nLines
= lines
.GetCount();
7058 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7061 case wxALIGN_BOTTOM
:
7062 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7065 case wxALIGN_CENTRE
:
7066 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7075 // Align each line of a multi-line label
7076 for( l
= 0; l
< nLines
; l
++ )
7078 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7080 switch( horizAlign
)
7083 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7086 case wxALIGN_CENTRE
:
7087 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7096 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7100 dc
.DestroyClippingRegion();
7104 // Split multi line text up into an array of strings. Any existing
7105 // contents of the string array are preserved.
7107 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7111 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7112 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7114 while ( startPos
< (int)tVal
.Length() )
7116 pos
= tVal
.Mid(startPos
).Find( eol
);
7121 else if ( pos
== 0 )
7123 lines
.Add( wxEmptyString
);
7127 lines
.Add( value
.Mid(startPos
, pos
) );
7131 if ( startPos
< (int)value
.Length() )
7133 lines
.Add( value
.Mid( startPos
) );
7138 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7139 const wxArrayString
& lines
,
7140 long *width
, long *height
)
7147 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7149 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7150 w
= wxMax( w
, lineW
);
7159 // ------ Batch processing.
7161 void wxGrid::EndBatch()
7163 if ( m_batchCount
> 0 )
7166 if ( !m_batchCount
)
7169 m_rowLabelWin
->Refresh();
7170 m_colLabelWin
->Refresh();
7171 m_cornerLabelWin
->Refresh();
7172 m_gridWin
->Refresh();
7177 // Use this, rather than wxWindow::Refresh(), to force an immediate
7178 // repainting of the grid. Has no effect if you are already inside a
7179 // BeginBatch / EndBatch block.
7181 void wxGrid::ForceRefresh()
7189 // ------ Edit control functions
7193 void wxGrid::EnableEditing( bool edit
)
7195 // TODO: improve this ?
7197 if ( edit
!= m_editable
)
7199 if(!edit
) EnableCellEditControl(edit
);
7205 void wxGrid::EnableCellEditControl( bool enable
)
7210 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7211 SetCurrentCell( 0, 0 );
7213 if ( enable
!= m_cellEditCtrlEnabled
)
7217 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7220 // this should be checked by the caller!
7221 wxASSERT_MSG( CanEnableCellControl(),
7222 _T("can't enable editing for this cell!") );
7224 // do it before ShowCellEditControl()
7225 m_cellEditCtrlEnabled
= enable
;
7227 ShowCellEditControl();
7231 //FIXME:add veto support
7232 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7234 HideCellEditControl();
7235 SaveEditControlValue();
7237 // do it after HideCellEditControl()
7238 m_cellEditCtrlEnabled
= enable
;
7243 bool wxGrid::IsCurrentCellReadOnly() const
7246 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7247 bool readonly
= attr
->IsReadOnly();
7253 bool wxGrid::CanEnableCellControl() const
7255 return m_editable
&& !IsCurrentCellReadOnly();
7258 bool wxGrid::IsCellEditControlEnabled() const
7260 // the cell edit control might be disable for all cells or just for the
7261 // current one if it's read only
7262 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7265 bool wxGrid::IsCellEditControlShown() const
7267 bool isShown
= FALSE
;
7269 if ( m_cellEditCtrlEnabled
)
7271 int row
= m_currentCellCoords
.GetRow();
7272 int col
= m_currentCellCoords
.GetCol();
7273 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7274 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7279 if ( editor
->IsCreated() )
7281 isShown
= editor
->GetControl()->IsShown();
7291 void wxGrid::ShowCellEditControl()
7293 if ( IsCellEditControlEnabled() )
7295 if ( !IsVisible( m_currentCellCoords
) )
7297 m_cellEditCtrlEnabled
= FALSE
;
7302 wxRect rect
= CellToRect( m_currentCellCoords
);
7303 int row
= m_currentCellCoords
.GetRow();
7304 int col
= m_currentCellCoords
.GetCol();
7306 // if this is part of a multicell, find owner (topleft)
7307 int cell_rows
, cell_cols
;
7308 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7309 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7313 m_currentCellCoords
.SetRow( row
);
7314 m_currentCellCoords
.SetCol( col
);
7317 // convert to scrolled coords
7319 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7321 // done in PaintBackground()
7323 // erase the highlight and the cell contents because the editor
7324 // might not cover the entire cell
7325 wxClientDC
dc( m_gridWin
);
7327 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7328 dc
.SetPen(*wxTRANSPARENT_PEN
);
7329 dc
.DrawRectangle(rect
);
7332 // cell is shifted by one pixel
7333 // However, don't allow x or y to become negative
7334 // since the SetSize() method interprets that as
7341 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7342 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7343 if ( !editor
->IsCreated() )
7345 editor
->Create(m_gridWin
, -1,
7346 new wxGridCellEditorEvtHandler(this, editor
));
7348 wxGridEditorCreatedEvent
evt(GetId(),
7349 wxEVT_GRID_EDITOR_CREATED
,
7353 editor
->GetControl());
7354 GetEventHandler()->ProcessEvent(evt
);
7358 // resize editor to overflow into righthand cells if allowed
7359 int maxWidth
= rect
.width
;
7360 wxString value
= GetCellValue(row
, col
);
7361 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7364 GetTextExtent(value
, &maxWidth
, &y
,
7365 NULL
, NULL
, &attr
->GetFont());
7366 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7368 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7369 if (rect
.x
+maxWidth
> client_right
)
7370 maxWidth
= client_right
- rect
.x
;
7372 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7374 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7375 // may have changed earlier
7376 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7379 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7380 // looks weird going over a multicell
7381 if (m_table
->IsEmptyCell(row
,i
) &&
7382 (rect
.width
< maxWidth
) && (c_rows
== 1))
7383 rect
.width
+= GetColWidth(i
);
7387 if (rect
.GetRight() > client_right
)
7388 rect
.SetRight(client_right
-1);
7391 editor
->SetSize( rect
);
7392 editor
->Show( TRUE
, attr
);
7394 // recalc dimensions in case we need to
7395 // expand the scrolled window to account for editor
7398 editor
->BeginEdit(row
, col
, this);
7407 void wxGrid::HideCellEditControl()
7409 if ( IsCellEditControlEnabled() )
7411 int row
= m_currentCellCoords
.GetRow();
7412 int col
= m_currentCellCoords
.GetCol();
7414 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7415 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7416 editor
->Show( FALSE
);
7419 m_gridWin
->SetFocus();
7420 // refresh whole row to the right
7421 wxRect
rect( CellToRect(row
, col
) );
7422 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7423 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7424 m_gridWin
->Refresh( FALSE
, &rect
);
7429 void wxGrid::SaveEditControlValue()
7431 if ( IsCellEditControlEnabled() )
7433 int row
= m_currentCellCoords
.GetRow();
7434 int col
= m_currentCellCoords
.GetCol();
7436 wxString oldval
= GetCellValue(row
,col
);
7438 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7439 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7440 bool changed
= editor
->EndEdit(row
, col
, this);
7447 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7448 m_currentCellCoords
.GetRow(),
7449 m_currentCellCoords
.GetCol() ) < 0 ) {
7451 // Event has been vetoed, set the data back.
7452 SetCellValue(row
,col
,oldval
);
7460 // ------ Grid location functions
7461 // Note that all of these functions work with the logical coordinates of
7462 // grid cells and labels so you will need to convert from device
7463 // coordinates for mouse events etc.
7466 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7468 int row
= YToRow(y
);
7469 int col
= XToCol(x
);
7471 if ( row
== -1 || col
== -1 )
7473 coords
= wxGridNoCellCoords
;
7477 coords
.Set( row
, col
);
7482 // Internal Helper function for computing row or column from some
7483 // (unscrolled) coordinate value, using either
7484 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7485 // of m_rowBottoms/m_ColRights to speed up the search!
7487 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7488 const wxArrayInt
& BorderArray
, int nMax
,
7493 size_t i_max
= coord
/ defaultDist
,
7496 if (BorderArray
.IsEmpty())
7498 if((int) i_max
< nMax
)
7500 return maxOnOverflow
? nMax
- 1 : -1;
7503 if ( i_max
>= BorderArray
.GetCount())
7504 i_max
= BorderArray
.GetCount() - 1;
7507 if ( coord
>= BorderArray
[i_max
])
7510 i_max
= coord
/ minDist
;
7512 if ( i_max
>= BorderArray
.GetCount())
7513 i_max
= BorderArray
.GetCount() - 1;
7515 if ( coord
>= BorderArray
[i_max
])
7516 return maxOnOverflow
? (int)i_max
: -1;
7517 if ( coord
< BorderArray
[0] )
7520 while ( i_max
- i_min
> 0 )
7522 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7523 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7524 if (coord
>= BorderArray
[ i_max
- 1])
7528 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7529 if (coord
< BorderArray
[median
])
7537 int wxGrid::YToRow( int y
)
7539 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7540 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7544 int wxGrid::XToCol( int x
)
7546 return CoordToRowOrCol(x
, m_defaultColWidth
,
7547 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7551 // return the row number that that the y coord is near the edge of, or
7552 // -1 if not near an edge
7554 int wxGrid::YToEdgeOfRow( int y
)
7557 i
= internalYToRow(y
);
7559 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7561 // We know that we are in row i, test whether we are
7562 // close enough to lower or upper border, respectively.
7563 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7565 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7573 // return the col number that that the x coord is near the edge of, or
7574 // -1 if not near an edge
7576 int wxGrid::XToEdgeOfCol( int x
)
7579 i
= internalXToCol(x
);
7581 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7583 // We know that we are in column i, test whether we are
7584 // close enough to right or left border, respectively.
7585 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7587 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7595 wxRect
wxGrid::CellToRect( int row
, int col
)
7597 wxRect
rect( -1, -1, -1, -1 );
7599 if ( row
>= 0 && row
< m_numRows
&&
7600 col
>= 0 && col
< m_numCols
)
7602 int i
, cell_rows
, cell_cols
;
7603 rect
.width
= rect
.height
= 0;
7604 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7605 // if negative then find multicell owner
7606 if (cell_rows
< 0) row
+= cell_rows
;
7607 if (cell_cols
< 0) col
+= cell_cols
;
7608 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7610 rect
.x
= GetColLeft(col
);
7611 rect
.y
= GetRowTop(row
);
7612 for (i
=col
; i
<col
+cell_cols
; i
++)
7613 rect
.width
+= GetColWidth(i
);
7614 for (i
=row
; i
<row
+cell_rows
; i
++)
7615 rect
.height
+= GetRowHeight(i
);
7618 // if grid lines are enabled, then the area of the cell is a bit smaller
7619 if (m_gridLinesEnabled
) {
7627 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7629 // get the cell rectangle in logical coords
7631 wxRect
r( CellToRect( row
, col
) );
7633 // convert to device coords
7635 int left
, top
, right
, bottom
;
7636 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7637 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7639 // check against the client area of the grid window
7642 m_gridWin
->GetClientSize( &cw
, &ch
);
7644 if ( wholeCellVisible
)
7646 // is the cell wholly visible ?
7648 return ( left
>= 0 && right
<= cw
&&
7649 top
>= 0 && bottom
<= ch
);
7653 // is the cell partly visible ?
7655 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7656 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7661 // make the specified cell location visible by doing a minimal amount
7664 void wxGrid::MakeCellVisible( int row
, int col
)
7668 int xpos
= -1, ypos
= -1;
7670 if ( row
>= 0 && row
< m_numRows
&&
7671 col
>= 0 && col
< m_numCols
)
7673 // get the cell rectangle in logical coords
7675 wxRect
r( CellToRect( row
, col
) );
7677 // convert to device coords
7679 int left
, top
, right
, bottom
;
7680 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7681 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7684 m_gridWin
->GetClientSize( &cw
, &ch
);
7690 else if ( bottom
> ch
)
7692 int h
= r
.GetHeight();
7694 for ( i
= row
-1; i
>= 0; i
-- )
7696 int rowHeight
= GetRowHeight(i
);
7697 if ( h
+ rowHeight
> ch
)
7704 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7705 // have rounding errors (this is important, because if we do, we
7706 // might not scroll at all and some cells won't be redrawn)
7708 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7710 ypos
+= GRID_SCROLL_LINE_Y
;
7717 else if ( right
> cw
)
7719 // position the view so that the cell is on the right
7721 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7722 xpos
= x0
+ (right
- cw
);
7724 // see comment for ypos above
7725 xpos
+= GRID_SCROLL_LINE_X
;
7728 if ( xpos
!= -1 || ypos
!= -1 )
7731 xpos
/= GRID_SCROLL_LINE_X
;
7733 ypos
/= GRID_SCROLL_LINE_Y
;
7734 Scroll( xpos
, ypos
);
7742 // ------ Grid cursor movement functions
7745 bool wxGrid::MoveCursorUp( bool expandSelection
)
7747 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7748 m_currentCellCoords
.GetRow() >= 0 )
7750 if ( expandSelection
)
7752 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7753 m_selectingKeyboard
= m_currentCellCoords
;
7754 if ( m_selectingKeyboard
.GetRow() > 0 )
7756 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7757 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7758 m_selectingKeyboard
.GetCol() );
7759 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7762 else if ( m_currentCellCoords
.GetRow() > 0 )
7765 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7766 m_currentCellCoords
.GetCol() );
7767 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7768 m_currentCellCoords
.GetCol() );
7779 bool wxGrid::MoveCursorDown( bool expandSelection
)
7781 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7782 m_currentCellCoords
.GetRow() < m_numRows
)
7784 if ( expandSelection
)
7786 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7787 m_selectingKeyboard
= m_currentCellCoords
;
7788 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7790 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7791 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7792 m_selectingKeyboard
.GetCol() );
7793 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7796 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7799 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7800 m_currentCellCoords
.GetCol() );
7801 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7802 m_currentCellCoords
.GetCol() );
7813 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7815 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7816 m_currentCellCoords
.GetCol() >= 0 )
7818 if ( expandSelection
)
7820 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7821 m_selectingKeyboard
= m_currentCellCoords
;
7822 if ( m_selectingKeyboard
.GetCol() > 0 )
7824 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7825 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7826 m_selectingKeyboard
.GetCol() );
7827 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7830 else if ( m_currentCellCoords
.GetCol() > 0 )
7833 MakeCellVisible( m_currentCellCoords
.GetRow(),
7834 m_currentCellCoords
.GetCol() - 1 );
7835 SetCurrentCell( m_currentCellCoords
.GetRow(),
7836 m_currentCellCoords
.GetCol() - 1 );
7847 bool wxGrid::MoveCursorRight( bool expandSelection
)
7849 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7850 m_currentCellCoords
.GetCol() < m_numCols
)
7852 if ( expandSelection
)
7854 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7855 m_selectingKeyboard
= m_currentCellCoords
;
7856 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7858 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7859 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7860 m_selectingKeyboard
.GetCol() );
7861 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7864 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7867 MakeCellVisible( m_currentCellCoords
.GetRow(),
7868 m_currentCellCoords
.GetCol() + 1 );
7869 SetCurrentCell( m_currentCellCoords
.GetRow(),
7870 m_currentCellCoords
.GetCol() + 1 );
7881 bool wxGrid::MovePageUp()
7883 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7885 int row
= m_currentCellCoords
.GetRow();
7889 m_gridWin
->GetClientSize( &cw
, &ch
);
7891 int y
= GetRowTop(row
);
7892 int newRow
= YToRow( y
- ch
+ 1 );
7897 else if ( newRow
== row
)
7902 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7903 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7911 bool wxGrid::MovePageDown()
7913 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7915 int row
= m_currentCellCoords
.GetRow();
7916 if ( row
< m_numRows
)
7919 m_gridWin
->GetClientSize( &cw
, &ch
);
7921 int y
= GetRowTop(row
);
7922 int newRow
= YToRow( y
+ ch
);
7925 newRow
= m_numRows
- 1;
7927 else if ( newRow
== row
)
7932 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7933 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7941 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7944 m_currentCellCoords
!= wxGridNoCellCoords
&&
7945 m_currentCellCoords
.GetRow() > 0 )
7947 int row
= m_currentCellCoords
.GetRow();
7948 int col
= m_currentCellCoords
.GetCol();
7950 if ( m_table
->IsEmptyCell(row
, col
) )
7952 // starting in an empty cell: find the next block of
7958 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7961 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7963 // starting at the top of a block: find the next block
7969 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7974 // starting within a block: find the top of the block
7979 if ( m_table
->IsEmptyCell(row
, col
) )
7987 MakeCellVisible( row
, col
);
7988 if ( expandSelection
)
7990 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7991 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7996 SetCurrentCell( row
, col
);
8004 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8007 m_currentCellCoords
!= wxGridNoCellCoords
&&
8008 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8010 int row
= m_currentCellCoords
.GetRow();
8011 int col
= m_currentCellCoords
.GetCol();
8013 if ( m_table
->IsEmptyCell(row
, col
) )
8015 // starting in an empty cell: find the next block of
8018 while ( row
< m_numRows
-1 )
8021 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8024 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8026 // starting at the bottom of a block: find the next block
8029 while ( row
< m_numRows
-1 )
8032 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8037 // starting within a block: find the bottom of the block
8039 while ( row
< m_numRows
-1 )
8042 if ( m_table
->IsEmptyCell(row
, col
) )
8050 MakeCellVisible( row
, col
);
8051 if ( expandSelection
)
8053 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8054 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8059 SetCurrentCell( row
, col
);
8068 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8071 m_currentCellCoords
!= wxGridNoCellCoords
&&
8072 m_currentCellCoords
.GetCol() > 0 )
8074 int row
= m_currentCellCoords
.GetRow();
8075 int col
= m_currentCellCoords
.GetCol();
8077 if ( m_table
->IsEmptyCell(row
, col
) )
8079 // starting in an empty cell: find the next block of
8085 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8088 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8090 // starting at the left of a block: find the next block
8096 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8101 // starting within a block: find the left of the block
8106 if ( m_table
->IsEmptyCell(row
, col
) )
8114 MakeCellVisible( row
, col
);
8115 if ( expandSelection
)
8117 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8118 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8123 SetCurrentCell( row
, col
);
8132 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8135 m_currentCellCoords
!= wxGridNoCellCoords
&&
8136 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8138 int row
= m_currentCellCoords
.GetRow();
8139 int col
= m_currentCellCoords
.GetCol();
8141 if ( m_table
->IsEmptyCell(row
, col
) )
8143 // starting in an empty cell: find the next block of
8146 while ( col
< m_numCols
-1 )
8149 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8152 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8154 // starting at the right of a block: find the next block
8157 while ( col
< m_numCols
-1 )
8160 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8165 // starting within a block: find the right of the block
8167 while ( col
< m_numCols
-1 )
8170 if ( m_table
->IsEmptyCell(row
, col
) )
8178 MakeCellVisible( row
, col
);
8179 if ( expandSelection
)
8181 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8182 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8187 SetCurrentCell( row
, col
);
8199 // ------ Label values and formatting
8202 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8204 *horiz
= m_rowLabelHorizAlign
;
8205 *vert
= m_rowLabelVertAlign
;
8208 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8210 *horiz
= m_colLabelHorizAlign
;
8211 *vert
= m_colLabelVertAlign
;
8214 wxString
wxGrid::GetRowLabelValue( int row
)
8218 return m_table
->GetRowLabelValue( row
);
8228 wxString
wxGrid::GetColLabelValue( int col
)
8232 return m_table
->GetColLabelValue( col
);
8243 void wxGrid::SetRowLabelSize( int width
)
8245 width
= wxMax( width
, 0 );
8246 if ( width
!= m_rowLabelWidth
)
8250 m_rowLabelWin
->Show( FALSE
);
8251 m_cornerLabelWin
->Show( FALSE
);
8253 else if ( m_rowLabelWidth
== 0 )
8255 m_rowLabelWin
->Show( TRUE
);
8256 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8259 m_rowLabelWidth
= width
;
8261 wxScrolledWindow::Refresh( TRUE
);
8266 void wxGrid::SetColLabelSize( int height
)
8268 height
= wxMax( height
, 0 );
8269 if ( height
!= m_colLabelHeight
)
8273 m_colLabelWin
->Show( FALSE
);
8274 m_cornerLabelWin
->Show( FALSE
);
8276 else if ( m_colLabelHeight
== 0 )
8278 m_colLabelWin
->Show( TRUE
);
8279 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8282 m_colLabelHeight
= height
;
8284 wxScrolledWindow::Refresh( TRUE
);
8289 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8291 if ( m_labelBackgroundColour
!= colour
)
8293 m_labelBackgroundColour
= colour
;
8294 m_rowLabelWin
->SetBackgroundColour( colour
);
8295 m_colLabelWin
->SetBackgroundColour( colour
);
8296 m_cornerLabelWin
->SetBackgroundColour( colour
);
8298 if ( !GetBatchCount() )
8300 m_rowLabelWin
->Refresh();
8301 m_colLabelWin
->Refresh();
8302 m_cornerLabelWin
->Refresh();
8307 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8309 if ( m_labelTextColour
!= colour
)
8311 m_labelTextColour
= colour
;
8312 if ( !GetBatchCount() )
8314 m_rowLabelWin
->Refresh();
8315 m_colLabelWin
->Refresh();
8320 void wxGrid::SetLabelFont( const wxFont
& font
)
8323 if ( !GetBatchCount() )
8325 m_rowLabelWin
->Refresh();
8326 m_colLabelWin
->Refresh();
8330 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8332 // allow old (incorrect) defs to be used
8335 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8336 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8337 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8342 case wxTOP
: vert
= wxALIGN_TOP
; break;
8343 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8344 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8347 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8349 m_rowLabelHorizAlign
= horiz
;
8352 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8354 m_rowLabelVertAlign
= vert
;
8357 if ( !GetBatchCount() )
8359 m_rowLabelWin
->Refresh();
8363 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8365 // allow old (incorrect) defs to be used
8368 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8369 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8370 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8375 case wxTOP
: vert
= wxALIGN_TOP
; break;
8376 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8377 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8380 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8382 m_colLabelHorizAlign
= horiz
;
8385 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8387 m_colLabelVertAlign
= vert
;
8390 if ( !GetBatchCount() )
8392 m_colLabelWin
->Refresh();
8396 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8400 m_table
->SetRowLabelValue( row
, s
);
8401 if ( !GetBatchCount() )
8403 wxRect rect
= CellToRect( row
, 0);
8404 if ( rect
.height
> 0 )
8406 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8408 rect
.width
= m_rowLabelWidth
;
8409 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8415 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8419 m_table
->SetColLabelValue( col
, s
);
8420 if ( !GetBatchCount() )
8422 wxRect rect
= CellToRect( 0, col
);
8423 if ( rect
.width
> 0 )
8425 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8427 rect
.height
= m_colLabelHeight
;
8428 m_colLabelWin
->Refresh( TRUE
, &rect
);
8434 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8436 if ( m_gridLineColour
!= colour
)
8438 m_gridLineColour
= colour
;
8440 wxClientDC
dc( m_gridWin
);
8442 DrawAllGridLines( dc
, wxRegion() );
8447 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8449 if ( m_cellHighlightColour
!= colour
)
8451 m_cellHighlightColour
= colour
;
8453 wxClientDC
dc( m_gridWin
);
8455 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8456 DrawCellHighlight(dc
, attr
);
8461 void wxGrid::SetCellHighlightPenWidth(int width
)
8463 if (m_cellHighlightPenWidth
!= width
) {
8464 m_cellHighlightPenWidth
= width
;
8466 // Just redrawing the cell highlight is not enough since that won't
8467 // make any visible change if the the thickness is getting smaller.
8468 int row
= m_currentCellCoords
.GetRow();
8469 int col
= m_currentCellCoords
.GetCol();
8470 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8472 wxRect rect
= CellToRect(row
, col
);
8473 m_gridWin
->Refresh(TRUE
, &rect
);
8477 void wxGrid::SetCellHighlightROPenWidth(int width
)
8479 if (m_cellHighlightROPenWidth
!= width
) {
8480 m_cellHighlightROPenWidth
= width
;
8482 // Just redrawing the cell highlight is not enough since that won't
8483 // make any visible change if the the thickness is getting smaller.
8484 int row
= m_currentCellCoords
.GetRow();
8485 int col
= m_currentCellCoords
.GetCol();
8486 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8488 wxRect rect
= CellToRect(row
, col
);
8489 m_gridWin
->Refresh(TRUE
, &rect
);
8493 void wxGrid::EnableGridLines( bool enable
)
8495 if ( enable
!= m_gridLinesEnabled
)
8497 m_gridLinesEnabled
= enable
;
8499 if ( !GetBatchCount() )
8503 wxClientDC
dc( m_gridWin
);
8505 DrawAllGridLines( dc
, wxRegion() );
8509 m_gridWin
->Refresh();
8516 int wxGrid::GetDefaultRowSize()
8518 return m_defaultRowHeight
;
8521 int wxGrid::GetRowSize( int row
)
8523 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8525 return GetRowHeight(row
);
8528 int wxGrid::GetDefaultColSize()
8530 return m_defaultColWidth
;
8533 int wxGrid::GetColSize( int col
)
8535 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8537 return GetColWidth(col
);
8540 // ============================================================================
8541 // access to the grid attributes: each of them has a default value in the grid
8542 // itself and may be overidden on a per-cell basis
8543 // ============================================================================
8545 // ----------------------------------------------------------------------------
8546 // setting default attributes
8547 // ----------------------------------------------------------------------------
8549 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8551 m_defaultCellAttr
->SetBackgroundColour(col
);
8553 m_gridWin
->SetBackgroundColour(col
);
8557 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8559 m_defaultCellAttr
->SetTextColour(col
);
8562 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8564 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8567 void wxGrid::SetDefaultCellOverflow( bool allow
)
8569 m_defaultCellAttr
->SetOverflow(allow
);
8572 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8574 m_defaultCellAttr
->SetFont(font
);
8577 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8579 m_defaultCellAttr
->SetRenderer(renderer
);
8582 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8584 m_defaultCellAttr
->SetEditor(editor
);
8587 // ----------------------------------------------------------------------------
8588 // access to the default attrbiutes
8589 // ----------------------------------------------------------------------------
8591 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8593 return m_defaultCellAttr
->GetBackgroundColour();
8596 wxColour
wxGrid::GetDefaultCellTextColour()
8598 return m_defaultCellAttr
->GetTextColour();
8601 wxFont
wxGrid::GetDefaultCellFont()
8603 return m_defaultCellAttr
->GetFont();
8606 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8608 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8611 bool wxGrid::GetDefaultCellOverflow()
8613 return m_defaultCellAttr
->GetOverflow();
8616 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8618 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8621 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8623 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8626 // ----------------------------------------------------------------------------
8627 // access to cell attributes
8628 // ----------------------------------------------------------------------------
8630 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8632 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8633 wxColour colour
= attr
->GetBackgroundColour();
8638 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8640 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8641 wxColour colour
= attr
->GetTextColour();
8646 wxFont
wxGrid::GetCellFont( int row
, int col
)
8648 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8649 wxFont font
= attr
->GetFont();
8654 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8656 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8657 attr
->GetAlignment(horiz
, vert
);
8661 bool wxGrid::GetCellOverflow( int row
, int col
)
8663 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8664 bool allow
= attr
->GetOverflow();
8669 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8671 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8672 attr
->GetSize( num_rows
, num_cols
);
8676 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8678 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8679 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8685 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8687 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8688 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8694 bool wxGrid::IsReadOnly(int row
, int col
) const
8696 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8697 bool isReadOnly
= attr
->IsReadOnly();
8702 // ----------------------------------------------------------------------------
8703 // attribute support: cache, automatic provider creation, ...
8704 // ----------------------------------------------------------------------------
8706 bool wxGrid::CanHaveAttributes()
8713 return m_table
->CanHaveAttributes();
8716 void wxGrid::ClearAttrCache()
8718 if ( m_attrCache
.row
!= -1 )
8720 wxSafeDecRef(m_attrCache
.attr
);
8721 m_attrCache
.attr
= NULL
;
8722 m_attrCache
.row
= -1;
8726 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8730 wxGrid
*self
= (wxGrid
*)this; // const_cast
8732 self
->ClearAttrCache();
8733 self
->m_attrCache
.row
= row
;
8734 self
->m_attrCache
.col
= col
;
8735 self
->m_attrCache
.attr
= attr
;
8740 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8742 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8744 *attr
= m_attrCache
.attr
;
8745 wxSafeIncRef(m_attrCache
.attr
);
8747 #ifdef DEBUG_ATTR_CACHE
8748 gs_nAttrCacheHits
++;
8755 #ifdef DEBUG_ATTR_CACHE
8756 gs_nAttrCacheMisses
++;
8762 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8764 wxGridCellAttr
*attr
= NULL
;
8765 // Additional test to avoid looking at the cache e.g. for
8766 // wxNoCellCoords, as this will confuse memory management.
8769 if ( !LookupAttr(row
, col
, &attr
) )
8771 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8772 : (wxGridCellAttr
*)NULL
;
8773 CacheAttr(row
, col
, attr
);
8778 attr
->SetDefAttr(m_defaultCellAttr
);
8782 attr
= m_defaultCellAttr
;
8789 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8791 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8793 wxCHECK_MSG( m_table
, attr
,
8794 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8796 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8799 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8801 // artificially inc the ref count to match DecRef() in caller
8803 m_table
->SetAttr(attr
, row
, col
);
8809 // ----------------------------------------------------------------------------
8810 // setting column attributes (wrappers around SetColAttr)
8811 // ----------------------------------------------------------------------------
8813 void wxGrid::SetColFormatBool(int col
)
8815 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8818 void wxGrid::SetColFormatNumber(int col
)
8820 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8823 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8825 wxString typeName
= wxGRID_VALUE_FLOAT
;
8826 if ( (width
!= -1) || (precision
!= -1) )
8828 typeName
<< _T(':') << width
<< _T(',') << precision
;
8831 SetColFormatCustom(col
, typeName
);
8834 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8836 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8838 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8840 attr
= new wxGridCellAttr
;
8841 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8842 attr
->SetRenderer(renderer
);
8844 SetColAttr(col
, attr
);
8848 // ----------------------------------------------------------------------------
8849 // setting cell attributes: this is forwarded to the table
8850 // ----------------------------------------------------------------------------
8852 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8854 if ( CanHaveAttributes() )
8856 m_table
->SetAttr(attr
, row
, col
);
8865 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8867 if ( CanHaveAttributes() )
8869 m_table
->SetRowAttr(attr
, row
);
8878 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8880 if ( CanHaveAttributes() )
8882 m_table
->SetColAttr(attr
, col
);
8891 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8893 if ( CanHaveAttributes() )
8895 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8896 attr
->SetBackgroundColour(colour
);
8901 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8903 if ( CanHaveAttributes() )
8905 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8906 attr
->SetTextColour(colour
);
8911 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8913 if ( CanHaveAttributes() )
8915 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8916 attr
->SetFont(font
);
8921 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8923 if ( CanHaveAttributes() )
8925 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8926 attr
->SetAlignment(horiz
, vert
);
8931 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8933 if ( CanHaveAttributes() )
8935 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8936 attr
->SetOverflow(allow
);
8941 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8943 if ( CanHaveAttributes() )
8945 int cell_rows
, cell_cols
;
8947 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8948 attr
->GetSize(&cell_rows
, &cell_cols
);
8949 attr
->SetSize(num_rows
, num_cols
);
8952 // Cannot set the size of a cell to 0 or negative values
8953 // While it is perfectly legal to do that, this function cannot
8954 // handle all the possibilies, do it by hand by getting the CellAttr.
8955 // You can only set the size of a cell to 1,1 or greater with this fn
8956 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8957 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8958 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8959 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8961 // if this was already a multicell then "turn off" the other cells first
8962 if ((cell_rows
> 1) || (cell_rows
> 1))
8965 for (j
=row
; j
<row
+cell_rows
; j
++)
8967 for (i
=col
; i
<col
+cell_cols
; i
++)
8969 if ((i
!= col
) || (j
!= row
))
8971 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8972 attr_stub
->SetSize( 1, 1 );
8973 attr_stub
->DecRef();
8979 // mark the cells that will be covered by this cell to
8980 // negative or zero values to point back at this cell
8981 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8984 for (j
=row
; j
<row
+num_rows
; j
++)
8986 for (i
=col
; i
<col
+num_cols
; i
++)
8988 if ((i
!= col
) || (j
!= row
))
8990 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8991 attr_stub
->SetSize( row
-j
, col
-i
);
8992 attr_stub
->DecRef();
9000 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9002 if ( CanHaveAttributes() )
9004 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9005 attr
->SetRenderer(renderer
);
9010 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9012 if ( CanHaveAttributes() )
9014 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9015 attr
->SetEditor(editor
);
9020 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9022 if ( CanHaveAttributes() )
9024 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9025 attr
->SetReadOnly(isReadOnly
);
9030 // ----------------------------------------------------------------------------
9031 // Data type registration
9032 // ----------------------------------------------------------------------------
9034 void wxGrid::RegisterDataType(const wxString
& typeName
,
9035 wxGridCellRenderer
* renderer
,
9036 wxGridCellEditor
* editor
)
9038 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9042 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9044 wxString typeName
= m_table
->GetTypeName(row
, col
);
9045 return GetDefaultEditorForType(typeName
);
9048 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9050 wxString typeName
= m_table
->GetTypeName(row
, col
);
9051 return GetDefaultRendererForType(typeName
);
9055 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9057 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9058 if ( index
== wxNOT_FOUND
)
9060 wxFAIL_MSG(wxT("Unknown data type name"));
9065 return m_typeRegistry
->GetEditor(index
);
9069 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9071 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9072 if ( index
== wxNOT_FOUND
)
9074 wxFAIL_MSG(wxT("Unknown data type name"));
9079 return m_typeRegistry
->GetRenderer(index
);
9083 // ----------------------------------------------------------------------------
9085 // ----------------------------------------------------------------------------
9087 void wxGrid::EnableDragRowSize( bool enable
)
9089 m_canDragRowSize
= enable
;
9093 void wxGrid::EnableDragColSize( bool enable
)
9095 m_canDragColSize
= enable
;
9098 void wxGrid::EnableDragGridSize( bool enable
)
9100 m_canDragGridSize
= enable
;
9104 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9106 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9108 if ( resizeExistingRows
)
9110 // since we are resizing all rows to the default row size,
9111 // we can simply clear the row heights and row bottoms
9112 // arrays (which also allows us to take advantage of
9113 // some speed optimisations)
9114 m_rowHeights
.Empty();
9115 m_rowBottoms
.Empty();
9116 if ( !GetBatchCount() )
9121 void wxGrid::SetRowSize( int row
, int height
)
9123 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9125 if ( m_rowHeights
.IsEmpty() )
9127 // need to really create the array
9131 int h
= wxMax( 0, height
);
9132 int diff
= h
- m_rowHeights
[row
];
9134 m_rowHeights
[row
] = h
;
9136 for ( i
= row
; i
< m_numRows
; i
++ )
9138 m_rowBottoms
[i
] += diff
;
9140 if ( !GetBatchCount() )
9144 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9146 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9148 if ( resizeExistingCols
)
9150 // since we are resizing all columns to the default column size,
9151 // we can simply clear the col widths and col rights
9152 // arrays (which also allows us to take advantage of
9153 // some speed optimisations)
9154 m_colWidths
.Empty();
9155 m_colRights
.Empty();
9156 if ( !GetBatchCount() )
9161 void wxGrid::SetColSize( int col
, int width
)
9163 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9165 // should we check that it's bigger than GetColMinimalWidth(col) here?
9167 if ( m_colWidths
.IsEmpty() )
9169 // need to really create the array
9173 // if < 0 calc new width from label
9177 wxArrayString lines
;
9178 wxClientDC
dc(m_colLabelWin
);
9179 dc
.SetFont(GetLabelFont());
9180 StringToLines(GetColLabelValue(col
), lines
);
9181 GetTextBoxSize(dc
, lines
, &w
, &h
);
9184 int w
= wxMax( 0, width
);
9185 int diff
= w
- m_colWidths
[col
];
9186 m_colWidths
[col
] = w
;
9189 for ( i
= col
; i
< m_numCols
; i
++ )
9191 m_colRights
[i
] += diff
;
9193 if ( !GetBatchCount() )
9198 void wxGrid::SetColMinimalWidth( int col
, int width
)
9200 m_colMinWidths
.Put(col
, width
);
9203 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9205 m_rowMinHeights
.Put(row
, width
);
9208 int wxGrid::GetColMinimalWidth(int col
) const
9210 long value
= m_colMinWidths
.Get(col
);
9211 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9214 int wxGrid::GetRowMinimalHeight(int row
) const
9216 long value
= m_rowMinHeights
.Get(row
);
9217 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9220 // ----------------------------------------------------------------------------
9222 // ----------------------------------------------------------------------------
9224 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9226 wxClientDC
dc(m_gridWin
);
9228 // init both of them to avoid compiler warnings, even if weo nly need one
9236 wxCoord extent
, extentMax
= 0;
9237 int max
= column
? m_numRows
: m_numCols
;
9238 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9245 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9246 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9249 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9250 extent
= column
? size
.x
: size
.y
;
9251 if ( extent
> extentMax
)
9262 // now also compare with the column label extent
9264 dc
.SetFont( GetLabelFont() );
9267 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9269 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9271 extent
= column
? w
: h
;
9272 if ( extent
> extentMax
)
9279 // empty column - give default extent (notice that if extentMax is less
9280 // than default extent but != 0, it's ok)
9281 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9287 // leave some space around text
9298 SetColSize(col
, extentMax
);
9299 if ( !GetBatchCount() )
9302 m_gridWin
->GetClientSize( &cw
, &ch
);
9303 wxRect
rect ( CellToRect( 0, col
) );
9305 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9306 rect
.width
= cw
- rect
.x
;
9307 rect
.height
= m_colLabelHeight
;
9308 m_colLabelWin
->Refresh( TRUE
, &rect
);
9313 SetRowSize(row
, extentMax
);
9314 if ( !GetBatchCount() )
9317 m_gridWin
->GetClientSize( &cw
, &ch
);
9318 wxRect
rect ( CellToRect( row
, 0 ) );
9320 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9321 rect
.width
= m_rowLabelWidth
;
9322 rect
.height
= ch
- rect
.y
;
9323 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9329 SetColMinimalWidth(col
, extentMax
);
9331 SetRowMinimalHeight(row
, extentMax
);
9335 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9337 int width
= m_rowLabelWidth
;
9342 for ( int col
= 0; col
< m_numCols
; col
++ )
9346 AutoSizeColumn(col
, setAsMin
);
9349 width
+= GetColWidth(col
);
9358 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9360 int height
= m_colLabelHeight
;
9365 for ( int row
= 0; row
< m_numRows
; row
++ )
9369 AutoSizeRow(row
, setAsMin
);
9372 height
+= GetRowHeight(row
);
9381 void wxGrid::AutoSize()
9385 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9387 // round up the size to a multiple of scroll step - this ensures that we
9388 // won't get the scrollbars if we're sized exactly to this width
9389 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9391 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9392 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9394 // distribute the extra space between the columns/rows to avoid having
9395 // extra white space
9397 // Remove the extra m_extraWidth + 1 added above
9398 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9399 if ( diff
&& m_numCols
)
9401 // try to resize the columns uniformly
9402 wxCoord diffPerCol
= diff
/ m_numCols
;
9405 for ( int col
= 0; col
< m_numCols
; col
++ )
9407 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9411 // add remaining amount to the last columns
9412 diff
-= diffPerCol
* m_numCols
;
9415 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9417 SetColSize(col
, GetColWidth(col
) + 1);
9423 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9424 if ( diff
&& m_numRows
)
9426 // try to resize the columns uniformly
9427 wxCoord diffPerRow
= diff
/ m_numRows
;
9430 for ( int row
= 0; row
< m_numRows
; row
++ )
9432 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9436 // add remaining amount to the last rows
9437 diff
-= diffPerRow
* m_numRows
;
9440 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9442 SetRowSize(row
, GetRowHeight(row
) + 1);
9449 SetClientSize(sizeFit
);
9452 wxSize
wxGrid::DoGetBestSize() const
9454 // don't set sizes, only calculate them
9455 wxGrid
*self
= (wxGrid
*)this; // const_cast
9458 width
= self
->SetOrCalcColumnSizes(TRUE
);
9459 height
= self
->SetOrCalcRowSizes(TRUE
);
9461 int maxwidth
, maxheight
;
9462 wxDisplaySize( & maxwidth
, & maxheight
);
9464 if ( width
> maxwidth
) width
= maxwidth
;
9465 if ( height
> maxheight
) height
= maxheight
;
9467 return wxSize( width
, height
);
9476 wxPen
& wxGrid::GetDividerPen() const
9481 // ----------------------------------------------------------------------------
9482 // cell value accessor functions
9483 // ----------------------------------------------------------------------------
9485 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9489 m_table
->SetValue( row
, col
, s
);
9490 if ( !GetBatchCount() )
9493 wxRect
rect( CellToRect( row
, col
) );
9495 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9496 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9497 m_gridWin
->Refresh( FALSE
, &rect
);
9500 if ( m_currentCellCoords
.GetRow() == row
&&
9501 m_currentCellCoords
.GetCol() == col
&&
9502 IsCellEditControlShown())
9503 // Note: If we are using IsCellEditControlEnabled,
9504 // this interacts badly with calling SetCellValue from
9505 // an EVT_GRID_CELL_CHANGE handler.
9507 HideCellEditControl();
9508 ShowCellEditControl(); // will reread data from table
9515 // ------ Block, row and col selection
9518 void wxGrid::SelectRow( int row
, bool addToSelected
)
9520 if ( IsSelection() && !addToSelected
)
9524 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9528 void wxGrid::SelectCol( int col
, bool addToSelected
)
9530 if ( IsSelection() && !addToSelected
)
9534 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9538 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9539 bool addToSelected
)
9541 if ( IsSelection() && !addToSelected
)
9545 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9546 FALSE
, addToSelected
);
9550 void wxGrid::SelectAll()
9552 if ( m_numRows
> 0 && m_numCols
> 0 )
9555 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9560 // ------ Cell, row and col deselection
9563 void wxGrid::DeselectRow( int row
)
9568 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9570 if ( m_selection
->IsInSelection(row
, 0 ) )
9571 m_selection
->ToggleCellSelection( row
, 0);
9575 int nCols
= GetNumberCols();
9576 for ( int i
= 0; i
< nCols
; i
++ )
9578 if ( m_selection
->IsInSelection(row
, i
) )
9579 m_selection
->ToggleCellSelection( row
, i
);
9584 void wxGrid::DeselectCol( int col
)
9589 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9591 if ( m_selection
->IsInSelection(0, col
) )
9592 m_selection
->ToggleCellSelection( 0, col
);
9596 int nRows
= GetNumberRows();
9597 for ( int i
= 0; i
< nRows
; i
++ )
9599 if ( m_selection
->IsInSelection(i
, col
) )
9600 m_selection
->ToggleCellSelection(i
, col
);
9605 void wxGrid::DeselectCell( int row
, int col
)
9607 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9608 m_selection
->ToggleCellSelection(row
, col
);
9611 bool wxGrid::IsSelection()
9613 return ( m_selection
&& (m_selection
->IsSelection() ||
9614 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9615 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9618 bool wxGrid::IsInSelection( int row
, int col
) const
9620 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9621 ( row
>= m_selectingTopLeft
.GetRow() &&
9622 col
>= m_selectingTopLeft
.GetCol() &&
9623 row
<= m_selectingBottomRight
.GetRow() &&
9624 col
<= m_selectingBottomRight
.GetCol() )) );
9627 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9629 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9630 return m_selection
->m_cellSelection
;
9632 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9634 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9635 return m_selection
->m_blockSelectionTopLeft
;
9637 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9639 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9640 return m_selection
->m_blockSelectionBottomRight
;
9642 wxArrayInt
wxGrid::GetSelectedRows() const
9644 if (!m_selection
) { wxArrayInt a
; return a
; }
9645 return m_selection
->m_rowSelection
;
9647 wxArrayInt
wxGrid::GetSelectedCols() const
9649 if (!m_selection
) { wxArrayInt a
; return a
; }
9650 return m_selection
->m_colSelection
;
9654 void wxGrid::ClearSelection()
9656 m_selectingTopLeft
= wxGridNoCellCoords
;
9657 m_selectingBottomRight
= wxGridNoCellCoords
;
9659 m_selection
->ClearSelection();
9663 // This function returns the rectangle that encloses the given block
9664 // in device coords clipped to the client size of the grid window.
9666 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9667 const wxGridCellCoords
&bottomRight
)
9669 wxRect
rect( wxGridNoCellRect
);
9672 cellRect
= CellToRect( topLeft
);
9673 if ( cellRect
!= wxGridNoCellRect
)
9679 rect
= wxRect( 0, 0, 0, 0 );
9682 cellRect
= CellToRect( bottomRight
);
9683 if ( cellRect
!= wxGridNoCellRect
)
9689 return wxGridNoCellRect
;
9693 int left
= rect
.GetLeft();
9694 int top
= rect
.GetTop();
9695 int right
= rect
.GetRight();
9696 int bottom
= rect
.GetBottom();
9698 int leftCol
= topLeft
.GetCol();
9699 int topRow
= topLeft
.GetRow();
9700 int rightCol
= bottomRight
.GetCol();
9701 int bottomRow
= bottomRight
.GetRow();
9724 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9726 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9728 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9730 cellRect
= CellToRect( j
, i
);
9732 if (cellRect
.x
< left
)
9734 if (cellRect
.y
< top
)
9736 if (cellRect
.x
+ cellRect
.width
> right
)
9737 right
= cellRect
.x
+ cellRect
.width
;
9738 if (cellRect
.y
+ cellRect
.height
> bottom
)
9739 bottom
= cellRect
.y
+ cellRect
.height
;
9741 else i
= rightCol
; // jump over inner cells.
9745 // convert to scrolled coords
9747 CalcScrolledPosition( left
, top
, &left
, &top
);
9748 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9751 m_gridWin
->GetClientSize( &cw
, &ch
);
9753 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9754 return wxRect( 0, 0, 0, 0);
9756 rect
.SetLeft( wxMax(0, left
) );
9757 rect
.SetTop( wxMax(0, top
) );
9758 rect
.SetRight( wxMin(cw
, right
) );
9759 rect
.SetBottom( wxMin(ch
, bottom
) );
9767 // ------ Grid event classes
9770 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9772 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9773 int row
, int col
, int x
, int y
, bool sel
,
9774 bool control
, bool shift
, bool alt
, bool meta
)
9775 : wxNotifyEvent( type
, id
)
9782 m_control
= control
;
9787 SetEventObject(obj
);
9791 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9793 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9794 int rowOrCol
, int x
, int y
,
9795 bool control
, bool shift
, bool alt
, bool meta
)
9796 : wxNotifyEvent( type
, id
)
9798 m_rowOrCol
= rowOrCol
;
9801 m_control
= control
;
9806 SetEventObject(obj
);
9810 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9812 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9813 const wxGridCellCoords
& topLeft
,
9814 const wxGridCellCoords
& bottomRight
,
9815 bool sel
, bool control
,
9816 bool shift
, bool alt
, bool meta
)
9817 : wxNotifyEvent( type
, id
)
9819 m_topLeft
= topLeft
;
9820 m_bottomRight
= bottomRight
;
9822 m_control
= control
;
9827 SetEventObject(obj
);
9831 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9833 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9834 wxObject
* obj
, int row
,
9835 int col
, wxControl
* ctrl
)
9836 : wxCommandEvent(type
, id
)
9838 SetEventObject(obj
);
9845 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9847 #endif // wxUSE_GRID