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 // ----------------------------------------------------------------------------
757 // wxGridCellNumberEditor
758 // ----------------------------------------------------------------------------
760 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
766 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
768 wxEvtHandler
* evtHandler
)
772 // create a spin ctrl
773 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
774 wxDefaultPosition
, wxDefaultSize
,
778 wxGridCellEditor::Create(parent
, id
, evtHandler
);
782 // just a text control
783 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
786 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
787 #endif // wxUSE_VALIDATORS
791 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
793 // first get the value
794 wxGridTableBase
*table
= grid
->GetTable();
795 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
797 m_valueOld
= table
->GetValueAsLong(row
, col
);
802 wxString sValue
= table
->GetValue(row
, col
);
803 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
805 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
812 Spin()->SetValue((int)m_valueOld
);
817 DoBeginEdit(GetString());
821 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
830 value
= Spin()->GetValue();
831 changed
= value
!= m_valueOld
;
833 text
= wxString::Format(wxT("%ld"), value
);
837 text
= Text()->GetValue();
838 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
843 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
844 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
846 grid
->GetTable()->SetValue(row
, col
, text
);
852 void wxGridCellNumberEditor::Reset()
856 Spin()->SetValue((int)m_valueOld
);
860 DoReset(GetString());
864 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
866 if ( wxGridCellEditor::IsAcceptedKey(event
) )
868 int keycode
= event
.GetKeyCode();
884 case WXK_NUMPAD_SUBTRACT
:
890 if ( (keycode
< 128) && isdigit(keycode
) )
898 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
902 int keycode
= (int) event
.KeyCode();
903 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
904 || keycode
== WXK_NUMPAD0
905 || keycode
== WXK_NUMPAD1
906 || keycode
== WXK_NUMPAD2
907 || keycode
== WXK_NUMPAD3
908 || keycode
== WXK_NUMPAD4
909 || keycode
== WXK_NUMPAD5
910 || keycode
== WXK_NUMPAD6
911 || keycode
== WXK_NUMPAD7
912 || keycode
== WXK_NUMPAD8
913 || keycode
== WXK_NUMPAD9
914 || keycode
== WXK_ADD
915 || keycode
== WXK_NUMPAD_ADD
916 || keycode
== WXK_SUBTRACT
917 || keycode
== WXK_NUMPAD_SUBTRACT
)
919 wxGridCellTextEditor::StartingKey(event
);
929 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
940 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
944 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
948 // skip the error message below
953 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
957 // ----------------------------------------------------------------------------
958 // wxGridCellFloatEditor
959 // ----------------------------------------------------------------------------
961 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
964 m_precision
= precision
;
967 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
969 wxEvtHandler
* evtHandler
)
971 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
974 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
975 #endif // wxUSE_VALIDATORS
978 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
980 // first get the value
981 wxGridTableBase
*table
= grid
->GetTable();
982 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
984 m_valueOld
= table
->GetValueAsDouble(row
, col
);
989 wxString sValue
= table
->GetValue(row
, col
);
990 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
992 wxFAIL_MSG( _T("this cell doesn't have float value") );
997 DoBeginEdit(GetString());
1000 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1004 wxString
text(Text()->GetValue());
1006 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1008 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1009 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1011 grid
->GetTable()->SetValue(row
, col
, text
);
1018 void wxGridCellFloatEditor::Reset()
1020 DoReset(GetString());
1023 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1025 int keycode
= (int)event
.KeyCode();
1026 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1027 || keycode
== WXK_NUMPAD0
1028 || keycode
== WXK_NUMPAD1
1029 || keycode
== WXK_NUMPAD2
1030 || keycode
== WXK_NUMPAD3
1031 || keycode
== WXK_NUMPAD4
1032 || keycode
== WXK_NUMPAD5
1033 || keycode
== WXK_NUMPAD6
1034 || keycode
== WXK_NUMPAD7
1035 || keycode
== WXK_NUMPAD8
1036 || keycode
== WXK_NUMPAD9
1037 || keycode
== WXK_ADD
1038 || keycode
== WXK_NUMPAD_ADD
1039 || keycode
== WXK_SUBTRACT
1040 || keycode
== WXK_NUMPAD_SUBTRACT
)
1042 wxGridCellTextEditor::StartingKey(event
);
1044 // skip Skip() below
1051 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1062 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1066 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1068 m_precision
= (int)tmp
;
1070 // skip the error message below
1075 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1079 wxString
wxGridCellFloatEditor::GetString() const
1082 if ( m_width
== -1 )
1084 // default width/precision
1087 else if ( m_precision
== -1 )
1089 // default precision
1090 fmt
.Printf(_T("%%%d.f"), m_width
);
1094 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1097 return wxString::Format(fmt
, m_valueOld
);
1100 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1102 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1104 int keycode
= event
.GetKeyCode();
1118 case WXK_NUMPAD_ADD
:
1120 case WXK_NUMPAD_SUBTRACT
:
1122 case WXK_NUMPAD_DECIMAL
:
1126 // additionally accept 'e' as in '1e+6'
1127 if ( (keycode
< 128) &&
1128 (isdigit(keycode
) || tolower(keycode
) == wxT('e')) )
1136 #endif // wxUSE_TEXTCTRL
1140 // ----------------------------------------------------------------------------
1141 // wxGridCellBoolEditor
1142 // ----------------------------------------------------------------------------
1144 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1146 wxEvtHandler
* evtHandler
)
1148 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1149 wxDefaultPosition
, wxDefaultSize
,
1152 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1155 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1157 bool resize
= FALSE
;
1158 wxSize size
= m_control
->GetSize();
1159 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1161 // check if the checkbox is not too big/small for this cell
1162 wxSize sizeBest
= m_control
->GetBestSize();
1163 if ( !(size
== sizeBest
) )
1165 // reset to default size if it had been made smaller
1171 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1173 // leave 1 pixel margin
1174 size
.x
= size
.y
= minSize
- 2;
1181 m_control
->SetSize(size
);
1184 // position it in the centre of the rectangle (TODO: support alignment?)
1186 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1187 // the checkbox without label still has some space to the right in wxGTK,
1188 // so shift it to the right
1190 #elif defined(__WXMSW__)
1191 // here too, but in other way
1196 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1199 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1201 m_control
->Show(show
);
1205 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1206 CBox()->SetBackgroundColour(colBg
);
1210 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1212 wxASSERT_MSG(m_control
,
1213 wxT("The wxGridCellEditor must be Created first!"));
1215 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1216 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1219 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1220 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1222 CBox()->SetValue(m_startValue
);
1226 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1229 wxASSERT_MSG(m_control
,
1230 wxT("The wxGridCellEditor must be Created first!"));
1232 bool changed
= FALSE
;
1233 bool value
= CBox()->GetValue();
1234 if ( value
!= m_startValue
)
1239 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1240 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1242 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1248 void wxGridCellBoolEditor::Reset()
1250 wxASSERT_MSG(m_control
,
1251 wxT("The wxGridCellEditor must be Created first!"));
1253 CBox()->SetValue(m_startValue
);
1256 void wxGridCellBoolEditor::StartingClick()
1258 CBox()->SetValue(!CBox()->GetValue());
1261 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1263 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1265 int keycode
= event
.GetKeyCode();
1269 case WXK_NUMPAD_MULTIPLY
:
1271 case WXK_NUMPAD_ADD
:
1273 case WXK_NUMPAD_SUBTRACT
:
1284 #endif // wxUSE_CHECKBOX
1288 // ----------------------------------------------------------------------------
1289 // wxGridCellChoiceEditor
1290 // ----------------------------------------------------------------------------
1292 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1293 const wxString choices
[],
1295 : m_allowOthers(allowOthers
)
1299 m_choices
.Alloc(count
);
1300 for ( size_t n
= 0; n
< count
; n
++ )
1302 m_choices
.Add(choices
[n
]);
1307 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1309 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1310 editor
->m_allowOthers
= m_allowOthers
;
1311 editor
->m_choices
= m_choices
;
1316 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1318 wxEvtHandler
* evtHandler
)
1320 size_t count
= m_choices
.GetCount();
1321 wxString
*choices
= new wxString
[count
];
1322 for ( size_t n
= 0; n
< count
; n
++ )
1324 choices
[n
] = m_choices
[n
];
1327 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1328 wxDefaultPosition
, wxDefaultSize
,
1330 m_allowOthers
? 0 : wxCB_READONLY
);
1334 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1337 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1338 wxGridCellAttr
* attr
)
1340 // as we fill the entire client area, don't do anything here to minimize
1343 // TODO: It doesn't actually fill the client area since the height of a
1344 // combo always defaults to the standard... Until someone has time to
1345 // figure out the right rectangle to paint, just do it the normal way...
1346 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1349 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1351 wxASSERT_MSG(m_control
,
1352 wxT("The wxGridCellEditor must be Created first!"));
1354 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1356 Combo()->SetValue(m_startValue
);
1357 size_t count
= m_choices
.GetCount();
1358 for (size_t i
=0; i
<count
; i
++)
1360 if (m_startValue
== m_choices
[i
])
1362 Combo()->SetSelection(i
);
1366 Combo()->SetInsertionPointEnd();
1367 Combo()->SetFocus();
1370 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1373 wxString value
= Combo()->GetValue();
1374 bool changed
= value
!= m_startValue
;
1377 grid
->GetTable()->SetValue(row
, col
, value
);
1379 m_startValue
= wxEmptyString
;
1380 Combo()->SetValue(m_startValue
);
1385 void wxGridCellChoiceEditor::Reset()
1387 Combo()->SetValue(m_startValue
);
1388 Combo()->SetInsertionPointEnd();
1391 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1401 wxStringTokenizer
tk(params
, _T(','));
1402 while ( tk
.HasMoreTokens() )
1404 m_choices
.Add(tk
.GetNextToken());
1408 #endif // wxUSE_COMBOBOX
1410 // ----------------------------------------------------------------------------
1411 // wxGridCellEditorEvtHandler
1412 // ----------------------------------------------------------------------------
1414 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1416 switch ( event
.KeyCode() )
1420 m_grid
->DisableCellEditControl();
1424 m_grid
->GetEventHandler()->ProcessEvent( event
);
1428 case WXK_NUMPAD_ENTER
:
1429 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1430 m_editor
->HandleReturn(event
);
1439 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1441 switch ( event
.KeyCode() )
1446 case WXK_NUMPAD_ENTER
:
1454 // ----------------------------------------------------------------------------
1455 // wxGridCellWorker is an (almost) empty common base class for
1456 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1457 // ----------------------------------------------------------------------------
1459 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1464 wxGridCellWorker::~wxGridCellWorker()
1468 // ============================================================================
1470 // ============================================================================
1472 // ----------------------------------------------------------------------------
1473 // wxGridCellRenderer
1474 // ----------------------------------------------------------------------------
1476 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1477 wxGridCellAttr
& attr
,
1480 int WXUNUSED(row
), int WXUNUSED(col
),
1483 dc
.SetBackgroundMode( wxSOLID
);
1487 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1491 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1494 dc
.SetPen( *wxTRANSPARENT_PEN
);
1495 dc
.DrawRectangle(rect
);
1498 // ----------------------------------------------------------------------------
1499 // wxGridCellStringRenderer
1500 // ----------------------------------------------------------------------------
1502 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1503 wxGridCellAttr
& attr
,
1507 dc
.SetBackgroundMode( wxTRANSPARENT
);
1509 // TODO some special colours for attr.IsReadOnly() case?
1513 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1514 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1518 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1519 dc
.SetTextForeground( attr
.GetTextColour() );
1522 dc
.SetFont( attr
.GetFont() );
1525 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1527 const wxString
& text
)
1529 wxCoord x
= 0, y
= 0, max_x
= 0;
1530 dc
.SetFont(attr
.GetFont());
1531 wxStringTokenizer
tk(text
, _T('\n'));
1532 while ( tk
.HasMoreTokens() )
1534 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1535 max_x
= wxMax(max_x
, x
);
1538 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1540 return wxSize(max_x
, y
);
1543 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1544 wxGridCellAttr
& attr
,
1548 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1551 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1552 wxGridCellAttr
& attr
,
1554 const wxRect
& rectCell
,
1558 wxRect rect
= rectCell
;
1560 if (attr
.GetOverflow())
1562 int cols
= grid
.GetNumberCols();
1563 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1564 int cell_rows
, cell_cols
;
1565 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1566 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1568 int i
, c_cols
, c_rows
;
1569 for (i
= col
+cell_cols
; i
< cols
; i
++)
1571 // check w/ anchor cell for multicell block
1572 grid
.GetCellSize(row
, i
, &c_rows
, &c_cols
);
1573 if (c_rows
> 0) c_rows
= 0;
1574 if (grid
.GetTable()->IsEmptyCell(row
+c_rows
, i
))
1576 rect
.width
+= grid
.GetColSize(i
);
1577 if (rect
.width
>= best_width
) break;
1584 // erase only this cells background, overflow cells should have been erased
1585 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1587 // now we only have to draw the text
1588 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1591 attr
.GetAlignment(&hAlign
, &vAlign
);
1595 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1596 rect
, hAlign
, vAlign
);
1599 // ----------------------------------------------------------------------------
1600 // wxGridCellNumberRenderer
1601 // ----------------------------------------------------------------------------
1603 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1605 wxGridTableBase
*table
= grid
.GetTable();
1607 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1609 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1613 text
= table
->GetValue(row
, col
);
1619 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1620 wxGridCellAttr
& attr
,
1622 const wxRect
& rectCell
,
1626 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1628 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1630 // draw the text right aligned by default
1632 attr
.GetAlignment(&hAlign
, &vAlign
);
1633 hAlign
= wxALIGN_RIGHT
;
1635 wxRect rect
= rectCell
;
1638 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1641 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1642 wxGridCellAttr
& attr
,
1646 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1649 // ----------------------------------------------------------------------------
1650 // wxGridCellFloatRenderer
1651 // ----------------------------------------------------------------------------
1653 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1656 SetPrecision(precision
);
1659 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1661 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1662 renderer
->m_width
= m_width
;
1663 renderer
->m_precision
= m_precision
;
1664 renderer
->m_format
= m_format
;
1669 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1671 wxGridTableBase
*table
= grid
.GetTable();
1676 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1678 val
= table
->GetValueAsDouble(row
, col
);
1683 text
= table
->GetValue(row
, col
);
1684 hasDouble
= text
.ToDouble(&val
);
1691 if ( m_width
== -1 )
1693 if ( m_precision
== -1 )
1695 // default width/precision
1696 m_format
= _T("%f");
1700 m_format
.Printf(_T("%%.%df"), m_precision
);
1703 else if ( m_precision
== -1 )
1705 // default precision
1706 m_format
.Printf(_T("%%%d.f"), m_width
);
1710 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1714 text
.Printf(m_format
, val
);
1717 //else: text already contains the string
1722 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1723 wxGridCellAttr
& attr
,
1725 const wxRect
& rectCell
,
1729 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1731 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1733 // draw the text right aligned by default
1735 attr
.GetAlignment(&hAlign
, &vAlign
);
1736 hAlign
= wxALIGN_RIGHT
;
1738 wxRect rect
= rectCell
;
1741 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1744 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1745 wxGridCellAttr
& attr
,
1749 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1752 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1756 // reset to defaults
1762 wxString tmp
= params
.BeforeFirst(_T(','));
1766 if ( tmp
.ToLong(&width
) )
1768 SetWidth((int)width
);
1772 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1776 tmp
= params
.AfterFirst(_T(','));
1780 if ( tmp
.ToLong(&precision
) )
1782 SetPrecision((int)precision
);
1786 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1794 // ----------------------------------------------------------------------------
1795 // wxGridCellBoolRenderer
1796 // ----------------------------------------------------------------------------
1798 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1800 // FIXME these checkbox size calculations are really ugly...
1802 // between checkmark and box
1803 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1805 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1806 wxGridCellAttr
& WXUNUSED(attr
),
1811 // compute it only once (no locks for MT safeness in GUI thread...)
1812 if ( !ms_sizeCheckMark
.x
)
1814 // get checkbox size
1815 wxCoord checkSize
= 0;
1816 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1817 wxSize size
= checkbox
->GetBestSize();
1818 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1820 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1821 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1822 checkSize
-= size
.y
/ 2;
1827 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1830 return ms_sizeCheckMark
;
1833 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1834 wxGridCellAttr
& attr
,
1840 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1842 // draw a check mark in the centre (ignoring alignment - TODO)
1843 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1845 // don't draw outside the cell
1846 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1847 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1849 // and even leave (at least) 1 pixel margin
1850 size
.x
= size
.y
= minSize
- 2;
1853 // draw a border around checkmark
1855 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1856 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1857 rectBorder
.width
= size
.x
;
1858 rectBorder
.height
= size
.y
;
1861 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1862 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1865 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1866 value
= !( !cellval
|| (cellval
== wxT("0")) );
1871 wxRect rectMark
= rectBorder
;
1873 // MSW DrawCheckMark() is weird (and should probably be changed...)
1874 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1878 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1881 dc
.SetTextForeground(attr
.GetTextColour());
1882 dc
.DrawCheckMark(rectMark
);
1885 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1886 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1887 dc
.DrawRectangle(rectBorder
);
1890 // ----------------------------------------------------------------------------
1892 // ----------------------------------------------------------------------------
1894 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1898 m_isReadOnly
= Unset
;
1903 m_attrkind
= wxGridCellAttr::Cell
;
1905 m_sizeRows
= m_sizeCols
= 1;
1908 SetDefAttr(attrDefault
);
1911 wxGridCellAttr
*wxGridCellAttr::Clone() const
1913 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1915 if ( HasTextColour() )
1916 attr
->SetTextColour(GetTextColour());
1917 if ( HasBackgroundColour() )
1918 attr
->SetBackgroundColour(GetBackgroundColour());
1920 attr
->SetFont(GetFont());
1921 if ( HasAlignment() )
1922 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1924 attr
->SetSize( m_sizeRows
, m_sizeCols
);
1928 attr
->SetRenderer(m_renderer
);
1929 m_renderer
->IncRef();
1933 attr
->SetEditor(m_editor
);
1938 attr
->SetReadOnly();
1940 attr
->SetKind( m_attrkind
);
1945 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1947 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1948 SetTextColour(mergefrom
->GetTextColour());
1949 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1950 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1951 if ( !HasFont() && mergefrom
->HasFont() )
1952 SetFont(mergefrom
->GetFont());
1953 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
1955 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1956 SetAlignment(hAlign
, vAlign
);
1959 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
1961 // Directly access member functions as GetRender/Editor don't just return
1962 // m_renderer/m_editor
1964 // Maybe add support for merge of Render and Editor?
1965 if (!HasRenderer() && mergefrom
->HasRenderer() )
1967 m_renderer
= mergefrom
->m_renderer
;
1968 m_renderer
->IncRef();
1970 if ( !HasEditor() && mergefrom
->HasEditor() )
1972 m_editor
= mergefrom
->m_editor
;
1975 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1976 SetReadOnly(mergefrom
->IsReadOnly());
1978 SetDefAttr(mergefrom
->m_defGridAttr
);
1981 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
1983 // The size of a cell is normally 1,1
1985 // If this cell is larger (2,2) then this is the top left cell
1986 // the other cells that will be covered (lower right cells) must be
1987 // set to negative or zero values such that
1988 // row + num_rows of the covered cell points to the larger cell (this cell)
1989 // same goes for the col + num_cols.
1991 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
1993 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
1994 !((num_rows
<=0)&&(num_cols
>0)) ||
1995 !((num_rows
==0)&&(num_cols
==0))),
1996 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
1998 m_sizeRows
= num_rows
;
1999 m_sizeCols
= num_cols
;
2002 const wxColour
& wxGridCellAttr::GetTextColour() const
2004 if (HasTextColour())
2008 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2010 return m_defGridAttr
->GetTextColour();
2014 wxFAIL_MSG(wxT("Missing default cell attribute"));
2015 return wxNullColour
;
2020 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2022 if (HasBackgroundColour())
2024 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2025 return m_defGridAttr
->GetBackgroundColour();
2028 wxFAIL_MSG(wxT("Missing default cell attribute"));
2029 return wxNullColour
;
2034 const wxFont
& wxGridCellAttr::GetFont() const
2038 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2039 return m_defGridAttr
->GetFont();
2042 wxFAIL_MSG(wxT("Missing default cell attribute"));
2048 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2052 if ( hAlign
) *hAlign
= m_hAlign
;
2053 if ( vAlign
) *vAlign
= m_vAlign
;
2055 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2056 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2059 wxFAIL_MSG(wxT("Missing default cell attribute"));
2063 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2065 if ( num_rows
) *num_rows
= m_sizeRows
;
2066 if ( num_cols
) *num_cols
= m_sizeCols
;
2069 // GetRenderer and GetEditor use a slightly different decision path about
2070 // which attribute to use. If a non-default attr object has one then it is
2071 // used, otherwise the default editor or renderer is fetched from the grid and
2072 // used. It should be the default for the data type of the cell. If it is
2073 // NULL (because the table has a type that the grid does not have in its
2074 // registry,) then the grid's default editor or renderer is used.
2076 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2078 wxGridCellRenderer
*renderer
;
2080 if ( m_renderer
&& this != m_defGridAttr
)
2082 // use the cells renderer if it has one
2083 renderer
= m_renderer
;
2086 else // no non default cell renderer
2088 // get default renderer for the data type
2091 // GetDefaultRendererForCell() will do IncRef() for us
2092 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2101 if (m_defGridAttr
&& this != m_defGridAttr
)
2103 // if we still don't have one then use the grid default
2104 // (no need for IncRef() here neither)
2105 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2107 else // default grid attr
2109 // use m_renderer which we had decided not to use initially
2110 renderer
= m_renderer
;
2117 // we're supposed to always find something
2118 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2123 // same as above, except for s/renderer/editor/g
2124 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2126 wxGridCellEditor
*editor
;
2128 if ( m_editor
&& this != m_defGridAttr
)
2130 // use the cells editor if it has one
2134 else // no non default cell editor
2136 // get default editor for the data type
2139 // GetDefaultEditorForCell() will do IncRef() for us
2140 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2149 if ( m_defGridAttr
&& this != m_defGridAttr
)
2151 // if we still don't have one then use the grid default
2152 // (no need for IncRef() here neither)
2153 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2155 else // default grid attr
2157 // use m_editor which we had decided not to use initially
2165 // we're supposed to always find something
2166 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2171 // ----------------------------------------------------------------------------
2172 // wxGridCellAttrData
2173 // ----------------------------------------------------------------------------
2175 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2177 int n
= FindIndex(row
, col
);
2178 if ( n
== wxNOT_FOUND
)
2180 // add the attribute
2181 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2185 // free the old attribute
2186 m_attrs
[(size_t)n
].attr
->DecRef();
2190 // change the attribute
2191 m_attrs
[(size_t)n
].attr
= attr
;
2195 // remove this attribute
2196 m_attrs
.RemoveAt((size_t)n
);
2201 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2203 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2205 int n
= FindIndex(row
, col
);
2206 if ( n
!= wxNOT_FOUND
)
2208 attr
= m_attrs
[(size_t)n
].attr
;
2215 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2217 size_t count
= m_attrs
.GetCount();
2218 for ( size_t n
= 0; n
< count
; n
++ )
2220 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2221 wxCoord row
= coords
.GetRow();
2222 if ((size_t)row
>= pos
)
2226 // If rows inserted, include row counter where necessary
2227 coords
.SetRow(row
+ numRows
);
2229 else if (numRows
< 0)
2231 // If rows deleted ...
2232 if ((size_t)row
>= pos
- numRows
)
2234 // ...either decrement row counter (if row still exists)...
2235 coords
.SetRow(row
+ numRows
);
2239 // ...or remove the attribute
2240 m_attrs
.RemoveAt((size_t)n
);
2248 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2250 size_t count
= m_attrs
.GetCount();
2251 for ( size_t n
= 0; n
< count
; n
++ )
2253 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2254 wxCoord col
= coords
.GetCol();
2255 if ( (size_t)col
>= pos
)
2259 // If rows inserted, include row counter where necessary
2260 coords
.SetCol(col
+ numCols
);
2262 else if (numCols
< 0)
2264 // If rows deleted ...
2265 if ((size_t)col
>= pos
- numCols
)
2267 // ...either decrement row counter (if row still exists)...
2268 coords
.SetCol(col
+ numCols
);
2272 // ...or remove the attribute
2273 m_attrs
.RemoveAt((size_t)n
);
2281 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2283 size_t count
= m_attrs
.GetCount();
2284 for ( size_t n
= 0; n
< count
; n
++ )
2286 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2287 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2296 // ----------------------------------------------------------------------------
2297 // wxGridRowOrColAttrData
2298 // ----------------------------------------------------------------------------
2300 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2302 size_t count
= m_attrs
.Count();
2303 for ( size_t n
= 0; n
< count
; n
++ )
2305 m_attrs
[n
]->DecRef();
2309 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2311 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2313 int n
= m_rowsOrCols
.Index(rowOrCol
);
2314 if ( n
!= wxNOT_FOUND
)
2316 attr
= m_attrs
[(size_t)n
];
2323 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2325 int i
= m_rowsOrCols
.Index(rowOrCol
);
2326 if ( i
== wxNOT_FOUND
)
2328 // add the attribute
2329 m_rowsOrCols
.Add(rowOrCol
);
2334 size_t n
= (size_t)i
;
2337 // change the attribute
2338 m_attrs
[n
]->DecRef();
2343 // remove this attribute
2344 m_attrs
[n
]->DecRef();
2345 m_rowsOrCols
.RemoveAt(n
);
2346 m_attrs
.RemoveAt(n
);
2351 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2353 size_t count
= m_attrs
.GetCount();
2354 for ( size_t n
= 0; n
< count
; n
++ )
2356 int & rowOrCol
= m_rowsOrCols
[n
];
2357 if ( (size_t)rowOrCol
>= pos
)
2359 if ( numRowsOrCols
> 0 )
2361 // If rows inserted, include row counter where necessary
2362 rowOrCol
+= numRowsOrCols
;
2364 else if ( numRowsOrCols
< 0)
2366 // If rows deleted, either decrement row counter (if row still exists)
2367 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2368 rowOrCol
+= numRowsOrCols
;
2371 m_rowsOrCols
.RemoveAt((size_t)n
);
2372 m_attrs
.RemoveAt((size_t)n
);
2380 // ----------------------------------------------------------------------------
2381 // wxGridCellAttrProvider
2382 // ----------------------------------------------------------------------------
2384 wxGridCellAttrProvider::wxGridCellAttrProvider()
2386 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2389 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2394 void wxGridCellAttrProvider::InitData()
2396 m_data
= new wxGridCellAttrProviderData
;
2399 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2400 wxGridCellAttr::wxAttrKind kind
) const
2402 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2407 case (wxGridCellAttr::Any
):
2408 //Get cached merge attributes.
2409 // Currenlty not used as no cache implemented as not mutiable
2410 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2413 //Basicaly implement old version.
2414 //Also check merge cache, so we don't have to re-merge every time..
2415 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2416 *attrrow
= (wxGridCellAttr
*)NULL
,
2417 *attrcol
= (wxGridCellAttr
*)NULL
;
2419 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2420 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2421 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2423 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2424 // Two or move are non NULL
2425 attr
= new wxGridCellAttr
;
2426 attr
->SetKind(wxGridCellAttr::Merged
);
2430 attr
->MergeWith(attrcell
);
2434 attr
->MergeWith(attrcol
);
2438 attr
->MergeWith(attrrow
);
2441 //store merge attr if cache implemented
2443 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2447 // one or none is non null return it or null.
2448 if(attrrow
) attr
= attrrow
;
2449 if(attrcol
) attr
= attrcol
;
2450 if(attrcell
) attr
= attrcell
;
2454 case (wxGridCellAttr::Cell
):
2455 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2457 case (wxGridCellAttr::Col
):
2458 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2460 case (wxGridCellAttr::Row
):
2461 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2465 // (wxGridCellAttr::Default):
2466 // (wxGridCellAttr::Merged):
2473 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2479 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2482 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2487 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2490 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2495 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2498 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2502 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2504 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2508 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2512 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2514 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2518 // ----------------------------------------------------------------------------
2519 // wxGridTypeRegistry
2520 // ----------------------------------------------------------------------------
2522 wxGridTypeRegistry::~wxGridTypeRegistry()
2524 size_t count
= m_typeinfo
.Count();
2525 for ( size_t i
= 0; i
< count
; i
++ )
2526 delete m_typeinfo
[i
];
2530 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2531 wxGridCellRenderer
* renderer
,
2532 wxGridCellEditor
* editor
)
2534 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2536 // is it already registered?
2537 int loc
= FindRegisteredDataType(typeName
);
2538 if ( loc
!= wxNOT_FOUND
)
2540 delete m_typeinfo
[loc
];
2541 m_typeinfo
[loc
] = info
;
2545 m_typeinfo
.Add(info
);
2549 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2551 size_t count
= m_typeinfo
.GetCount();
2552 for ( size_t i
= 0; i
< count
; i
++ )
2554 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2563 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2565 int index
= FindRegisteredDataType(typeName
);
2566 if ( index
== wxNOT_FOUND
)
2568 // check whether this is one of the standard ones, in which case
2569 // register it "on the fly"
2571 if ( typeName
== wxGRID_VALUE_STRING
)
2573 RegisterDataType(wxGRID_VALUE_STRING
,
2574 new wxGridCellStringRenderer
,
2575 new wxGridCellTextEditor
);
2577 #endif // wxUSE_TEXTCTRL
2579 if ( typeName
== wxGRID_VALUE_BOOL
)
2581 RegisterDataType(wxGRID_VALUE_BOOL
,
2582 new wxGridCellBoolRenderer
,
2583 new wxGridCellBoolEditor
);
2585 #endif // wxUSE_CHECKBOX
2587 if ( typeName
== wxGRID_VALUE_NUMBER
)
2589 RegisterDataType(wxGRID_VALUE_NUMBER
,
2590 new wxGridCellNumberRenderer
,
2591 new wxGridCellNumberEditor
);
2593 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2595 RegisterDataType(wxGRID_VALUE_FLOAT
,
2596 new wxGridCellFloatRenderer
,
2597 new wxGridCellFloatEditor
);
2599 #endif // wxUSE_TEXTCTRL
2601 if ( typeName
== wxGRID_VALUE_CHOICE
)
2603 RegisterDataType(wxGRID_VALUE_CHOICE
,
2604 new wxGridCellStringRenderer
,
2605 new wxGridCellChoiceEditor
);
2607 #endif // wxUSE_COMBOBOX
2612 // we get here only if just added the entry for this type, so return
2614 index
= m_typeinfo
.GetCount() - 1;
2620 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2622 int index
= FindDataType(typeName
);
2623 if ( index
== wxNOT_FOUND
)
2625 // the first part of the typename is the "real" type, anything after ':'
2626 // are the parameters for the renderer
2627 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2628 if ( index
== wxNOT_FOUND
)
2633 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2634 wxGridCellRenderer
*rendererOld
= renderer
;
2635 renderer
= renderer
->Clone();
2636 rendererOld
->DecRef();
2638 wxGridCellEditor
*editor
= GetEditor(index
);
2639 wxGridCellEditor
*editorOld
= editor
;
2640 editor
= editor
->Clone();
2641 editorOld
->DecRef();
2643 // do it even if there are no parameters to reset them to defaults
2644 wxString params
= typeName
.AfterFirst(_T(':'));
2645 renderer
->SetParameters(params
);
2646 editor
->SetParameters(params
);
2648 // register the new typename
2649 RegisterDataType(typeName
, renderer
, editor
);
2651 // we just registered it, it's the last one
2652 index
= m_typeinfo
.GetCount() - 1;
2658 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2660 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2666 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2668 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2674 // ----------------------------------------------------------------------------
2676 // ----------------------------------------------------------------------------
2678 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2681 wxGridTableBase::wxGridTableBase()
2683 m_view
= (wxGrid
*) NULL
;
2684 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2687 wxGridTableBase::~wxGridTableBase()
2689 delete m_attrProvider
;
2692 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2694 delete m_attrProvider
;
2695 m_attrProvider
= attrProvider
;
2698 bool wxGridTableBase::CanHaveAttributes()
2700 if ( ! GetAttrProvider() )
2702 // use the default attr provider by default
2703 SetAttrProvider(new wxGridCellAttrProvider
);
2708 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2710 if ( m_attrProvider
)
2711 return m_attrProvider
->GetAttr(row
, col
, kind
);
2713 return (wxGridCellAttr
*)NULL
;
2716 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2718 if ( m_attrProvider
)
2720 attr
->SetKind(wxGridCellAttr::Cell
);
2721 m_attrProvider
->SetAttr(attr
, row
, col
);
2725 // as we take ownership of the pointer and don't store it, we must
2731 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2733 if ( m_attrProvider
)
2735 attr
->SetKind(wxGridCellAttr::Row
);
2736 m_attrProvider
->SetRowAttr(attr
, row
);
2740 // as we take ownership of the pointer and don't store it, we must
2746 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2748 if ( m_attrProvider
)
2750 attr
->SetKind(wxGridCellAttr::Col
);
2751 m_attrProvider
->SetColAttr(attr
, col
);
2755 // as we take ownership of the pointer and don't store it, we must
2761 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2762 size_t WXUNUSED(numRows
) )
2764 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2769 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2771 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2776 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2777 size_t WXUNUSED(numRows
) )
2779 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2784 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2785 size_t WXUNUSED(numCols
) )
2787 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2792 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2794 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2799 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2800 size_t WXUNUSED(numCols
) )
2802 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2808 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2811 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2812 // how much it makes sense to us geeks.
2816 wxString
wxGridTableBase::GetColLabelValue( int col
)
2818 // default col labels are:
2819 // cols 0 to 25 : A-Z
2820 // cols 26 to 675 : AA-ZZ
2825 for ( n
= 1; ; n
++ )
2827 s
+= (_T('A') + (wxChar
)( col%26
));
2829 if ( col
< 0 ) break;
2832 // reverse the string...
2834 for ( i
= 0; i
< n
; i
++ )
2843 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2845 return wxGRID_VALUE_STRING
;
2848 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2849 const wxString
& typeName
)
2851 return typeName
== wxGRID_VALUE_STRING
;
2854 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2856 return CanGetValueAs(row
, col
, typeName
);
2859 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2864 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2869 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2874 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2875 long WXUNUSED(value
) )
2879 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2880 double WXUNUSED(value
) )
2884 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2885 bool WXUNUSED(value
) )
2890 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2891 const wxString
& WXUNUSED(typeName
) )
2896 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2897 const wxString
& WXUNUSED(typeName
),
2898 void* WXUNUSED(value
) )
2902 //////////////////////////////////////////////////////////////////////
2904 // Message class for the grid table to send requests and notifications
2908 wxGridTableMessage::wxGridTableMessage()
2910 m_table
= (wxGridTableBase
*) NULL
;
2916 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2917 int commandInt1
, int commandInt2
)
2921 m_comInt1
= commandInt1
;
2922 m_comInt2
= commandInt2
;
2927 //////////////////////////////////////////////////////////////////////
2929 // A basic grid table for string data. An object of this class will
2930 // created by wxGrid if you don't specify an alternative table class.
2933 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2935 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2937 wxGridStringTable::wxGridStringTable()
2942 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2945 m_data
.Alloc( numRows
);
2948 sa
.Alloc( numCols
);
2949 sa
.Add( wxEmptyString
, numCols
);
2951 m_data
.Add( sa
, numRows
);
2954 wxGridStringTable::~wxGridStringTable()
2958 int wxGridStringTable::GetNumberRows()
2960 return m_data
.GetCount();
2963 int wxGridStringTable::GetNumberCols()
2965 if ( m_data
.GetCount() > 0 )
2966 return m_data
[0].GetCount();
2971 wxString
wxGridStringTable::GetValue( int row
, int col
)
2973 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2974 _T("invalid row or column index in wxGridStringTable") );
2976 return m_data
[row
][col
];
2979 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2981 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2982 _T("invalid row or column index in wxGridStringTable") );
2984 m_data
[row
][col
] = value
;
2987 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2989 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2990 _T("invalid row or column index in wxGridStringTable") );
2992 return (m_data
[row
][col
] == wxEmptyString
);
2995 void wxGridStringTable::Clear()
2998 int numRows
, numCols
;
3000 numRows
= m_data
.GetCount();
3003 numCols
= m_data
[0].GetCount();
3005 for ( row
= 0; row
< numRows
; row
++ )
3007 for ( col
= 0; col
< numCols
; col
++ )
3009 m_data
[row
][col
] = wxEmptyString
;
3016 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3018 size_t curNumRows
= m_data
.GetCount();
3019 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3020 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3022 if ( pos
>= curNumRows
)
3024 return AppendRows( numRows
);
3028 sa
.Alloc( curNumCols
);
3029 sa
.Add( wxEmptyString
, curNumCols
);
3030 m_data
.Insert( sa
, pos
, numRows
);
3033 wxGridTableMessage
msg( this,
3034 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3038 GetView()->ProcessTableMessage( msg
);
3044 bool wxGridStringTable::AppendRows( size_t numRows
)
3046 size_t curNumRows
= m_data
.GetCount();
3047 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3048 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3051 if ( curNumCols
> 0 )
3053 sa
.Alloc( curNumCols
);
3054 sa
.Add( wxEmptyString
, curNumCols
);
3057 m_data
.Add( sa
, numRows
);
3061 wxGridTableMessage
msg( this,
3062 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3065 GetView()->ProcessTableMessage( msg
);
3071 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3073 size_t curNumRows
= m_data
.GetCount();
3075 if ( pos
>= curNumRows
)
3078 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3079 pos
, numRows
, curNumRows
);
3080 wxFAIL_MSG( errmsg
);
3084 if ( numRows
> curNumRows
- pos
)
3086 numRows
= curNumRows
- pos
;
3089 if ( numRows
>= curNumRows
)
3091 m_data
.Empty(); // don't release memory just yet
3095 m_data
.RemoveAt( pos
, numRows
);
3099 wxGridTableMessage
msg( this,
3100 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3104 GetView()->ProcessTableMessage( msg
);
3110 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3114 size_t curNumRows
= m_data
.GetCount();
3115 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3116 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3118 if ( pos
>= curNumCols
)
3120 return AppendCols( numCols
);
3123 for ( row
= 0; row
< curNumRows
; row
++ )
3125 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3127 m_data
[row
].Insert( wxEmptyString
, col
);
3132 wxGridTableMessage
msg( this,
3133 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3137 GetView()->ProcessTableMessage( msg
);
3143 bool wxGridStringTable::AppendCols( size_t numCols
)
3147 size_t curNumRows
= m_data
.GetCount();
3151 // TODO: something better than this ?
3153 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3158 for ( row
= 0; row
< curNumRows
; row
++ )
3160 m_data
[row
].Add( wxEmptyString
, numCols
);
3165 wxGridTableMessage
msg( this,
3166 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3169 GetView()->ProcessTableMessage( msg
);
3175 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3179 size_t curNumRows
= m_data
.GetCount();
3180 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3181 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3183 if ( pos
>= curNumCols
)
3186 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3187 pos
, numCols
, curNumCols
);
3188 wxFAIL_MSG( errmsg
);
3192 if ( numCols
> curNumCols
- pos
)
3194 numCols
= curNumCols
- pos
;
3197 for ( row
= 0; row
< curNumRows
; row
++ )
3199 if ( numCols
>= curNumCols
)
3201 m_data
[row
].Clear();
3205 m_data
[row
].RemoveAt( pos
, numCols
);
3210 wxGridTableMessage
msg( this,
3211 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3215 GetView()->ProcessTableMessage( msg
);
3221 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3223 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3225 // using default label
3227 return wxGridTableBase::GetRowLabelValue( row
);
3231 return m_rowLabels
[ row
];
3235 wxString
wxGridStringTable::GetColLabelValue( int col
)
3237 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3239 // using default label
3241 return wxGridTableBase::GetColLabelValue( col
);
3245 return m_colLabels
[ col
];
3249 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3251 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3253 int n
= m_rowLabels
.GetCount();
3255 for ( i
= n
; i
<= row
; i
++ )
3257 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3261 m_rowLabels
[row
] = value
;
3264 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3266 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3268 int n
= m_colLabels
.GetCount();
3270 for ( i
= n
; i
<= col
; i
++ )
3272 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3276 m_colLabels
[col
] = value
;
3281 //////////////////////////////////////////////////////////////////////
3282 //////////////////////////////////////////////////////////////////////
3284 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3286 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3287 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3288 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3289 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3290 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3291 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3294 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3296 const wxPoint
&pos
, const wxSize
&size
)
3297 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3302 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3306 // NO - don't do this because it will set both the x and y origin
3307 // coords to match the parent scrolled window and we just want to
3308 // set the y coord - MB
3310 // m_owner->PrepareDC( dc );
3313 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3314 dc
.SetDeviceOrigin( 0, -y
);
3316 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3317 m_owner
->DrawRowLabels( dc
, rows
);
3321 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3323 m_owner
->ProcessRowLabelMouseEvent( event
);
3327 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3329 m_owner
->GetEventHandler()->ProcessEvent(event
);
3333 // This seems to be required for wxMotif otherwise the mouse
3334 // cursor must be in the cell edit control to get key events
3336 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3338 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3341 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3343 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3348 //////////////////////////////////////////////////////////////////////
3350 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3352 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3353 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3354 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3355 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3356 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3357 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3360 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3362 const wxPoint
&pos
, const wxSize
&size
)
3363 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3368 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3372 // NO - don't do this because it will set both the x and y origin
3373 // coords to match the parent scrolled window and we just want to
3374 // set the x coord - MB
3376 // m_owner->PrepareDC( dc );
3379 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3380 dc
.SetDeviceOrigin( -x
, 0 );
3382 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3383 m_owner
->DrawColLabels( dc
, cols
);
3387 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3389 m_owner
->ProcessColLabelMouseEvent( event
);
3392 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3394 m_owner
->GetEventHandler()->ProcessEvent(event
);
3398 // This seems to be required for wxMotif otherwise the mouse
3399 // cursor must be in the cell edit control to get key events
3401 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3403 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3406 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3408 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3413 //////////////////////////////////////////////////////////////////////
3415 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3417 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3418 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3419 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3420 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3421 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3422 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3425 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3427 const wxPoint
&pos
, const wxSize
&size
)
3428 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3433 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3437 int client_height
= 0;
3438 int client_width
= 0;
3439 GetClientSize( &client_width
, &client_height
);
3441 dc
.SetPen( *wxBLACK_PEN
);
3442 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3443 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3445 dc
.SetPen( *wxWHITE_PEN
);
3446 dc
.DrawLine( 0, 0, client_width
, 0 );
3447 dc
.DrawLine( 0, 0, 0, client_height
);
3451 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3453 m_owner
->ProcessCornerLabelMouseEvent( event
);
3457 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3459 m_owner
->GetEventHandler()->ProcessEvent(event
);
3462 // This seems to be required for wxMotif otherwise the mouse
3463 // cursor must be in the cell edit control to get key events
3465 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3467 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3470 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3472 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3477 //////////////////////////////////////////////////////////////////////
3479 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3481 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3482 EVT_PAINT( wxGridWindow::OnPaint
)
3483 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3484 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3485 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3486 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3487 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3490 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3491 wxGridRowLabelWindow
*rowLblWin
,
3492 wxGridColLabelWindow
*colLblWin
,
3493 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3494 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, wxT("grid window") )
3497 m_rowLabelWin
= rowLblWin
;
3498 m_colLabelWin
= colLblWin
;
3499 SetBackgroundColour(_T("WHITE"));
3503 wxGridWindow::~wxGridWindow()
3508 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3510 wxPaintDC
dc( this );
3511 m_owner
->PrepareDC( dc
);
3512 wxRegion reg
= GetUpdateRegion();
3513 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3514 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3515 #if WXGRID_DRAW_LINES
3516 m_owner
->DrawAllGridLines( dc
, reg
);
3518 m_owner
->DrawGridSpace( dc
);
3519 m_owner
->DrawHighlight( dc
, DirtyCells
);
3523 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3525 wxWindow::ScrollWindow( dx
, dy
, rect
);
3526 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3527 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3531 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3533 m_owner
->ProcessGridCellMouseEvent( event
);
3536 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3538 m_owner
->GetEventHandler()->ProcessEvent(event
);
3541 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3542 // cursor must be in the cell edit control to get key events
3544 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3546 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3549 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3551 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3554 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3559 //////////////////////////////////////////////////////////////////////
3561 // Internal Helper function for computing row or column from some
3562 // (unscrolled) coordinate value, using either
3563 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3564 // of m_rowBottoms/m_ColRights to speed up the search!
3566 // Internal helper macros for simpler use of that function
3568 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3569 const wxArrayInt
& BorderArray
, bool maxOnOverflow
);
3571 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3572 WXGRID_MIN_COL_WIDTH, \
3574 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3575 WXGRID_MIN_ROW_HEIGHT, \
3577 /////////////////////////////////////////////////////////////////////
3579 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3581 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3582 EVT_PAINT( wxGrid::OnPaint
)
3583 EVT_SIZE( wxGrid::OnSize
)
3584 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3585 EVT_KEY_UP( wxGrid::OnKeyUp
)
3586 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3589 wxGrid::wxGrid( wxWindow
*parent
,
3594 const wxString
& name
)
3595 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3596 m_colMinWidths(GRID_HASH_SIZE
),
3597 m_rowMinHeights(GRID_HASH_SIZE
)
3605 // Must do this or ~wxScrollHelper will pop the wrong event handler
3606 SetTargetWindow(this);
3608 wxSafeDecRef(m_defaultCellAttr
);
3610 #ifdef DEBUG_ATTR_CACHE
3611 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3612 wxPrintf(_T("wxGrid attribute cache statistics: "
3613 "total: %u, hits: %u (%u%%)\n"),
3614 total
, gs_nAttrCacheHits
,
3615 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3621 delete m_typeRegistry
;
3627 // ----- internal init and update functions
3630 void wxGrid::Create()
3632 m_created
= FALSE
; // set to TRUE by CreateGrid
3634 m_table
= (wxGridTableBase
*) NULL
;
3637 m_cellEditCtrlEnabled
= FALSE
;
3639 m_defaultCellAttr
= new wxGridCellAttr();
3641 // Set default cell attributes
3642 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3643 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3644 m_defaultCellAttr
->SetFont(GetFont());
3645 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3646 m_defaultCellAttr
->SetTextColour(
3647 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3648 m_defaultCellAttr
->SetBackgroundColour(
3649 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3650 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3651 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3656 m_currentCellCoords
= wxGridNoCellCoords
;
3658 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3659 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3661 // create the type registry
3662 m_typeRegistry
= new wxGridTypeRegistry
;
3665 // subwindow components that make up the wxGrid
3666 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3671 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3676 m_colLabelWin
= new wxGridColLabelWindow( this,
3681 m_gridWin
= new wxGridWindow( this,
3688 SetTargetWindow( m_gridWin
);
3694 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3695 wxGrid::wxGridSelectionModes selmode
)
3697 wxCHECK_MSG( !m_created
,
3699 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3701 m_numRows
= numRows
;
3702 m_numCols
= numCols
;
3704 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3705 m_table
->SetView( this );
3707 m_selection
= new wxGridSelection( this, selmode
);
3716 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3718 wxCHECK_RET( m_created
,
3719 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3721 m_selection
->SetSelectionMode( selmode
);
3724 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3725 wxGrid::wxGridSelectionModes selmode
)
3729 // RD: Actually, this should probably be allowed. I think it would be
3730 // nice to be able to switch multiple Tables in and out of a single
3731 // View at runtime. Is there anything in the implementation that
3732 // would prevent this?
3734 // At least, you now have to cope with m_selection
3735 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3740 m_numRows
= table
->GetNumberRows();
3741 m_numCols
= table
->GetNumberCols();
3744 m_table
->SetView( this );
3747 m_selection
= new wxGridSelection( this, selmode
);
3760 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3761 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3763 if ( m_rowLabelWin
)
3765 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3769 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3772 m_labelTextColour
= wxColour( _T("BLACK") );
3775 m_attrCache
.row
= -1;
3777 // TODO: something better than this ?
3779 m_labelFont
= this->GetFont();
3780 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3782 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3783 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3785 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3786 m_colLabelVertAlign
= wxALIGN_TOP
;
3788 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3789 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3791 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3792 m_defaultRowHeight
+= 8;
3794 m_defaultRowHeight
+= 4;
3797 m_gridLineColour
= wxColour( 128, 128, 255 );
3798 m_gridLinesEnabled
= TRUE
;
3799 m_cellHighlightColour
= m_gridLineColour
;
3800 m_cellHighlightPenWidth
= 2;
3801 m_cellHighlightROPenWidth
= 1;
3803 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3804 m_winCapture
= (wxWindow
*)NULL
;
3805 m_canDragRowSize
= TRUE
;
3806 m_canDragColSize
= TRUE
;
3807 m_canDragGridSize
= TRUE
;
3809 m_dragRowOrCol
= -1;
3810 m_isDragging
= FALSE
;
3811 m_startDragPos
= wxDefaultPosition
;
3813 m_waitForSlowClick
= FALSE
;
3815 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3816 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3818 m_currentCellCoords
= wxGridNoCellCoords
;
3820 m_selectingTopLeft
= wxGridNoCellCoords
;
3821 m_selectingBottomRight
= wxGridNoCellCoords
;
3822 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3823 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3825 m_editable
= TRUE
; // default for whole grid
3827 m_inOnKeyDown
= FALSE
;
3834 // ----------------------------------------------------------------------------
3835 // the idea is to call these functions only when necessary because they create
3836 // quite big arrays which eat memory mostly unnecessary - in particular, if
3837 // default widths/heights are used for all rows/columns, we may not use these
3840 // with some extra code, it should be possible to only store the
3841 // widths/heights different from default ones but this will be done later...
3842 // ----------------------------------------------------------------------------
3844 void wxGrid::InitRowHeights()
3846 m_rowHeights
.Empty();
3847 m_rowBottoms
.Empty();
3849 m_rowHeights
.Alloc( m_numRows
);
3850 m_rowBottoms
.Alloc( m_numRows
);
3854 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
3856 for ( int i
= 0; i
< m_numRows
; i
++ )
3858 rowBottom
+= m_defaultRowHeight
;
3859 m_rowBottoms
.Add( rowBottom
);
3863 void wxGrid::InitColWidths()
3865 m_colWidths
.Empty();
3866 m_colRights
.Empty();
3868 m_colWidths
.Alloc( m_numCols
);
3869 m_colRights
.Alloc( m_numCols
);
3872 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
3874 for ( int i
= 0; i
< m_numCols
; i
++ )
3876 colRight
+= m_defaultColWidth
;
3877 m_colRights
.Add( colRight
);
3881 int wxGrid::GetColWidth(int col
) const
3883 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3886 int wxGrid::GetColLeft(int col
) const
3888 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3889 : m_colRights
[col
] - m_colWidths
[col
];
3892 int wxGrid::GetColRight(int col
) const
3894 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3898 int wxGrid::GetRowHeight(int row
) const
3900 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3903 int wxGrid::GetRowTop(int row
) const
3905 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3906 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3909 int wxGrid::GetRowBottom(int row
) const
3911 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3912 : m_rowBottoms
[row
];
3915 void wxGrid::CalcDimensions()
3918 GetClientSize( &cw
, &ch
);
3920 if ( m_rowLabelWin
->IsShown() )
3921 cw
-= m_rowLabelWidth
;
3922 if ( m_colLabelWin
->IsShown() )
3923 ch
-= m_colLabelHeight
;
3926 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3927 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3929 // preserve (more or less) the previous position
3931 GetViewStart( &x
, &y
);
3933 // maybe we don't need scrollbars at all?
3935 // also adjust the position to be valid for the new scroll rangs
3956 // do set scrollbar parameters
3957 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3958 GetScrollX(w
), GetScrollY(h
), x
, y
,
3959 GetBatchCount() != 0);
3961 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3962 // still must reposition the children
3967 void wxGrid::CalcWindowSizes()
3970 GetClientSize( &cw
, &ch
);
3972 if ( m_cornerLabelWin
->IsShown() )
3973 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3975 if ( m_colLabelWin
->IsShown() )
3976 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3978 if ( m_rowLabelWin
->IsShown() )
3979 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3981 if ( m_gridWin
->IsShown() )
3982 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3986 // this is called when the grid table sends a message to say that it
3987 // has been redimensioned
3989 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3992 bool result
= FALSE
;
3994 // Clear the attribute cache as the attribute might refer to a different
3995 // cell than stored in the cache after adding/removing rows/columns.
3997 // By the same reasoning, the editor should be dismissed if columns are
3998 // added or removed. And for consistency, it should IMHO always be
3999 // removed, not only if the cell "underneath" it actually changes.
4000 // For now, I intentionally do not save the editor's content as the
4001 // cell it might want to save that stuff to might no longer exist.
4002 DisableCellEditControl();
4004 // if we were using the default widths/heights so far, we must change them
4006 if ( m_colWidths
.IsEmpty() )
4011 if ( m_rowHeights
.IsEmpty() )
4017 switch ( msg
.GetId() )
4019 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4021 size_t pos
= msg
.GetCommandInt();
4022 int numRows
= msg
.GetCommandInt2();
4024 m_numRows
+= numRows
;
4026 if ( !m_rowHeights
.IsEmpty() )
4028 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4029 m_rowBottoms
.Insert( 0, pos
, numRows
);
4032 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4034 for ( i
= pos
; i
< m_numRows
; i
++ )
4036 bottom
+= m_rowHeights
[i
];
4037 m_rowBottoms
[i
] = bottom
;
4040 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4042 // if we have just inserted cols into an empty grid the current
4043 // cell will be undefined...
4045 SetCurrentCell( 0, 0 );
4049 m_selection
->UpdateRows( pos
, numRows
);
4050 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4052 attrProvider
->UpdateAttrRows( pos
, numRows
);
4054 if ( !GetBatchCount() )
4057 m_rowLabelWin
->Refresh();
4063 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4065 int numRows
= msg
.GetCommandInt();
4066 int oldNumRows
= m_numRows
;
4067 m_numRows
+= numRows
;
4069 if ( !m_rowHeights
.IsEmpty() )
4071 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4072 m_rowBottoms
.Add( 0, numRows
);
4075 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4077 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4079 bottom
+= m_rowHeights
[i
];
4080 m_rowBottoms
[i
] = bottom
;
4083 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4085 // if we have just inserted cols into an empty grid the current
4086 // cell will be undefined...
4088 SetCurrentCell( 0, 0 );
4090 if ( !GetBatchCount() )
4093 m_rowLabelWin
->Refresh();
4099 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4101 size_t pos
= msg
.GetCommandInt();
4102 int numRows
= msg
.GetCommandInt2();
4103 m_numRows
-= numRows
;
4105 if ( !m_rowHeights
.IsEmpty() )
4107 m_rowHeights
.RemoveAt( pos
, numRows
);
4108 m_rowBottoms
.RemoveAt( pos
, numRows
);
4111 for ( i
= 0; i
< m_numRows
; i
++ )
4113 h
+= m_rowHeights
[i
];
4114 m_rowBottoms
[i
] = h
;
4119 m_currentCellCoords
= wxGridNoCellCoords
;
4123 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4124 m_currentCellCoords
.Set( 0, 0 );
4128 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4129 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4131 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4132 // ifdef'd out following patch from Paul Gammans
4134 // No need to touch column attributes, unless we
4135 // removed _all_ rows, in this case, we remove
4136 // all column attributes.
4137 // I hate to do this here, but the
4138 // needed data is not available inside UpdateAttrRows.
4139 if ( !GetNumberRows() )
4140 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4143 if ( !GetBatchCount() )
4146 m_rowLabelWin
->Refresh();
4152 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4154 size_t pos
= msg
.GetCommandInt();
4155 int numCols
= msg
.GetCommandInt2();
4156 m_numCols
+= numCols
;
4158 if ( !m_colWidths
.IsEmpty() )
4160 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4161 m_colRights
.Insert( 0, pos
, numCols
);
4164 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4166 for ( i
= pos
; i
< m_numCols
; i
++ )
4168 right
+= m_colWidths
[i
];
4169 m_colRights
[i
] = right
;
4172 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4174 // if we have just inserted cols into an empty grid the current
4175 // cell will be undefined...
4177 SetCurrentCell( 0, 0 );
4181 m_selection
->UpdateCols( pos
, numCols
);
4182 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4184 attrProvider
->UpdateAttrCols( pos
, numCols
);
4185 if ( !GetBatchCount() )
4188 m_colLabelWin
->Refresh();
4195 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4197 int numCols
= msg
.GetCommandInt();
4198 int oldNumCols
= m_numCols
;
4199 m_numCols
+= numCols
;
4200 if ( !m_colWidths
.IsEmpty() )
4202 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4203 m_colRights
.Add( 0, numCols
);
4206 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4208 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4210 right
+= m_colWidths
[i
];
4211 m_colRights
[i
] = right
;
4214 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4216 // if we have just inserted cols into an empty grid the current
4217 // cell will be undefined...
4219 SetCurrentCell( 0, 0 );
4221 if ( !GetBatchCount() )
4224 m_colLabelWin
->Refresh();
4230 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4232 size_t pos
= msg
.GetCommandInt();
4233 int numCols
= msg
.GetCommandInt2();
4234 m_numCols
-= numCols
;
4236 if ( !m_colWidths
.IsEmpty() )
4238 m_colWidths
.RemoveAt( pos
, numCols
);
4239 m_colRights
.RemoveAt( pos
, numCols
);
4242 for ( i
= 0; i
< m_numCols
; i
++ )
4244 w
+= m_colWidths
[i
];
4250 m_currentCellCoords
= wxGridNoCellCoords
;
4254 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4255 m_currentCellCoords
.Set( 0, 0 );
4259 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4260 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4262 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4263 // ifdef'd out following patch from Paul Gammans
4265 // No need to touch row attributes, unless we
4266 // removed _all_ columns, in this case, we remove
4267 // all row attributes.
4268 // I hate to do this here, but the
4269 // needed data is not available inside UpdateAttrCols.
4270 if ( !GetNumberCols() )
4271 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4274 if ( !GetBatchCount() )
4277 m_colLabelWin
->Refresh();
4284 if (result
&& !GetBatchCount() )
4285 m_gridWin
->Refresh();
4290 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4292 wxRegionIterator
iter( reg
);
4295 wxArrayInt rowlabels
;
4302 // TODO: remove this when we can...
4303 // There is a bug in wxMotif that gives garbage update
4304 // rectangles if you jump-scroll a long way by clicking the
4305 // scrollbar with middle button. This is a work-around
4307 #if defined(__WXMOTIF__)
4309 m_gridWin
->GetClientSize( &cw
, &ch
);
4310 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4311 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4314 // logical bounds of update region
4317 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4318 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4320 // find the row labels within these bounds
4323 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4325 if ( GetRowBottom(row
) < top
)
4328 if ( GetRowTop(row
) > bottom
)
4331 rowlabels
.Add( row
);
4341 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4343 wxRegionIterator
iter( reg
);
4346 wxArrayInt colLabels
;
4353 // TODO: remove this when we can...
4354 // There is a bug in wxMotif that gives garbage update
4355 // rectangles if you jump-scroll a long way by clicking the
4356 // scrollbar with middle button. This is a work-around
4358 #if defined(__WXMOTIF__)
4360 m_gridWin
->GetClientSize( &cw
, &ch
);
4361 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4362 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4365 // logical bounds of update region
4368 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4369 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4371 // find the cells within these bounds
4374 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4376 if ( GetColRight(col
) < left
)
4379 if ( GetColLeft(col
) > right
)
4382 colLabels
.Add( col
);
4391 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4393 wxRegionIterator
iter( reg
);
4396 wxGridCellCoordsArray cellsExposed
;
4398 int left
, top
, right
, bottom
;
4403 // TODO: remove this when we can...
4404 // There is a bug in wxMotif that gives garbage update
4405 // rectangles if you jump-scroll a long way by clicking the
4406 // scrollbar with middle button. This is a work-around
4408 #if defined(__WXMOTIF__)
4410 m_gridWin
->GetClientSize( &cw
, &ch
);
4411 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4412 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4413 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4414 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4417 // logical bounds of update region
4419 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4420 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4422 // find the cells within these bounds
4425 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4427 if ( GetRowBottom(row
) <= top
)
4430 if ( GetRowTop(row
) > bottom
)
4433 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4435 if ( GetColRight(col
) <= left
)
4438 if ( GetColLeft(col
) > right
)
4441 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4448 return cellsExposed
;
4452 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4455 wxPoint
pos( event
.GetPosition() );
4456 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4458 if ( event
.Dragging() )
4462 m_isDragging
= TRUE
;
4463 m_rowLabelWin
->CaptureMouse();
4466 if ( event
.LeftIsDown() )
4468 switch( m_cursorMode
)
4470 case WXGRID_CURSOR_RESIZE_ROW
:
4472 int cw
, ch
, left
, dummy
;
4473 m_gridWin
->GetClientSize( &cw
, &ch
);
4474 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4476 wxClientDC
dc( m_gridWin
);
4479 GetRowTop(m_dragRowOrCol
) +
4480 GetRowMinimalHeight(m_dragRowOrCol
) );
4481 dc
.SetLogicalFunction(wxINVERT
);
4482 if ( m_dragLastPos
>= 0 )
4484 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4486 dc
.DrawLine( left
, y
, left
+cw
, y
);
4491 case WXGRID_CURSOR_SELECT_ROW
:
4492 if ( (row
= YToRow( y
)) >= 0 )
4496 m_selection
->SelectRow( row
,
4497 event
.ControlDown(),
4504 // default label to suppress warnings about "enumeration value
4505 // 'xxx' not handled in switch
4513 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4518 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4519 m_isDragging
= FALSE
;
4522 // ------------ Entering or leaving the window
4524 if ( event
.Entering() || event
.Leaving() )
4526 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4530 // ------------ Left button pressed
4532 else if ( event
.LeftDown() )
4534 // don't send a label click event for a hit on the
4535 // edge of the row label - this is probably the user
4536 // wanting to resize the row
4538 if ( YToEdgeOfRow(y
) < 0 )
4542 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4544 if ( !event
.ShiftDown() && !event
.ControlDown() )
4546 else if ( m_selection
)
4548 if ( event
.ShiftDown() )
4550 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4553 GetNumberCols() - 1,
4554 event
.ControlDown(),
4561 m_selection
->SelectRow( row
,
4562 event
.ControlDown(),
4569 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4574 // starting to drag-resize a row
4576 if ( CanDragRowSize() )
4577 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4582 // ------------ Left double click
4584 else if (event
.LeftDClick() )
4586 if ( YToEdgeOfRow(y
) < 0 )
4589 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4594 // ------------ Left button released
4596 else if ( event
.LeftUp() )
4598 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4600 DoEndDragResizeRow();
4602 // Note: we are ending the event *after* doing
4603 // default processing in this case
4605 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4608 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4613 // ------------ Right button down
4615 else if ( event
.RightDown() )
4618 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4620 // no default action at the moment
4625 // ------------ Right double click
4627 else if ( event
.RightDClick() )
4630 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4632 // no default action at the moment
4637 // ------------ No buttons down and mouse moving
4639 else if ( event
.Moving() )
4641 m_dragRowOrCol
= YToEdgeOfRow( y
);
4642 if ( m_dragRowOrCol
>= 0 )
4644 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4646 // don't capture the mouse yet
4647 if ( CanDragRowSize() )
4648 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4651 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4653 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4659 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4662 wxPoint
pos( event
.GetPosition() );
4663 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4665 if ( event
.Dragging() )
4669 m_isDragging
= TRUE
;
4670 m_colLabelWin
->CaptureMouse();
4673 if ( event
.LeftIsDown() )
4675 switch( m_cursorMode
)
4677 case WXGRID_CURSOR_RESIZE_COL
:
4679 int cw
, ch
, dummy
, top
;
4680 m_gridWin
->GetClientSize( &cw
, &ch
);
4681 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4683 wxClientDC
dc( m_gridWin
);
4686 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4687 GetColMinimalWidth(m_dragRowOrCol
));
4688 dc
.SetLogicalFunction(wxINVERT
);
4689 if ( m_dragLastPos
>= 0 )
4691 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4693 dc
.DrawLine( x
, top
, x
, top
+ch
);
4698 case WXGRID_CURSOR_SELECT_COL
:
4699 if ( (col
= XToCol( x
)) >= 0 )
4703 m_selection
->SelectCol( col
,
4704 event
.ControlDown(),
4711 // default label to suppress warnings about "enumeration value
4712 // 'xxx' not handled in switch
4720 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4725 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4726 m_isDragging
= FALSE
;
4729 // ------------ Entering or leaving the window
4731 if ( event
.Entering() || event
.Leaving() )
4733 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4737 // ------------ Left button pressed
4739 else if ( event
.LeftDown() )
4741 // don't send a label click event for a hit on the
4742 // edge of the col label - this is probably the user
4743 // wanting to resize the col
4745 if ( XToEdgeOfCol(x
) < 0 )
4749 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4751 if ( !event
.ShiftDown() && !event
.ControlDown() )
4755 if ( event
.ShiftDown() )
4757 m_selection
->SelectBlock( 0,
4758 m_currentCellCoords
.GetCol(),
4759 GetNumberRows() - 1, col
,
4760 event
.ControlDown(),
4767 m_selection
->SelectCol( col
,
4768 event
.ControlDown(),
4775 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4780 // starting to drag-resize a col
4782 if ( CanDragColSize() )
4783 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4788 // ------------ Left double click
4790 if ( event
.LeftDClick() )
4792 if ( XToEdgeOfCol(x
) < 0 )
4795 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4800 // ------------ Left button released
4802 else if ( event
.LeftUp() )
4804 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4806 DoEndDragResizeCol();
4808 // Note: we are ending the event *after* doing
4809 // default processing in this case
4811 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4814 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4819 // ------------ Right button down
4821 else if ( event
.RightDown() )
4824 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4826 // no default action at the moment
4831 // ------------ Right double click
4833 else if ( event
.RightDClick() )
4836 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4838 // no default action at the moment
4843 // ------------ No buttons down and mouse moving
4845 else if ( event
.Moving() )
4847 m_dragRowOrCol
= XToEdgeOfCol( x
);
4848 if ( m_dragRowOrCol
>= 0 )
4850 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4852 // don't capture the cursor yet
4853 if ( CanDragColSize() )
4854 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4857 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4859 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4865 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4867 if ( event
.LeftDown() )
4869 // indicate corner label by having both row and
4872 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4878 else if ( event
.LeftDClick() )
4880 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4883 else if ( event
.RightDown() )
4885 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4887 // no default action at the moment
4891 else if ( event
.RightDClick() )
4893 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4895 // no default action at the moment
4900 void wxGrid::ChangeCursorMode(CursorMode mode
,
4905 static const wxChar
*cursorModes
[] =
4914 wxLogTrace(_T("grid"),
4915 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4916 win
== m_colLabelWin
? _T("colLabelWin")
4917 : win
? _T("rowLabelWin")
4919 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4920 #endif // __WXDEBUG__
4922 if ( mode
== m_cursorMode
&&
4923 win
== m_winCapture
&&
4924 captureMouse
== (m_winCapture
!= NULL
))
4929 // by default use the grid itself
4935 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4936 m_winCapture
= (wxWindow
*)NULL
;
4939 m_cursorMode
= mode
;
4941 switch ( m_cursorMode
)
4943 case WXGRID_CURSOR_RESIZE_ROW
:
4944 win
->SetCursor( m_rowResizeCursor
);
4947 case WXGRID_CURSOR_RESIZE_COL
:
4948 win
->SetCursor( m_colResizeCursor
);
4952 win
->SetCursor( *wxSTANDARD_CURSOR
);
4955 // we need to capture mouse when resizing
4956 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4957 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4959 if ( captureMouse
&& resize
)
4961 win
->CaptureMouse();
4966 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4969 wxPoint
pos( event
.GetPosition() );
4970 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4972 wxGridCellCoords coords
;
4973 XYToCell( x
, y
, coords
);
4975 int cell_rows
, cell_cols
;
4976 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4977 if ((cell_rows
< 0) || (cell_cols
< 0))
4979 coords
.SetRow(coords
.GetRow() + cell_rows
);
4980 coords
.SetCol(coords
.GetCol() + cell_cols
);
4983 if ( event
.Dragging() )
4985 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4987 // Don't start doing anything until the mouse has been drug at
4988 // least 3 pixels in any direction...
4991 if (m_startDragPos
== wxDefaultPosition
)
4993 m_startDragPos
= pos
;
4996 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5000 m_isDragging
= TRUE
;
5001 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5003 // Hide the edit control, so it
5004 // won't interfer with drag-shrinking.
5005 if ( IsCellEditControlShown() )
5007 HideCellEditControl();
5008 SaveEditControlValue();
5011 // Have we captured the mouse yet?
5014 m_winCapture
= m_gridWin
;
5015 m_winCapture
->CaptureMouse();
5018 if ( coords
!= wxGridNoCellCoords
)
5020 if ( event
.ControlDown() )
5022 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5023 m_selectingKeyboard
= coords
;
5024 HighlightBlock ( m_selectingKeyboard
, coords
);
5028 if ( !IsSelection() )
5030 HighlightBlock( coords
, coords
);
5034 HighlightBlock( m_currentCellCoords
, coords
);
5038 if (! IsVisible(coords
))
5040 MakeCellVisible(coords
);
5041 // TODO: need to introduce a delay or something here. The
5042 // scrolling is way to fast, at least on MSW - also on GTK.
5046 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5048 int cw
, ch
, left
, dummy
;
5049 m_gridWin
->GetClientSize( &cw
, &ch
);
5050 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5052 wxClientDC
dc( m_gridWin
);
5054 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5055 GetRowMinimalHeight(m_dragRowOrCol
) );
5056 dc
.SetLogicalFunction(wxINVERT
);
5057 if ( m_dragLastPos
>= 0 )
5059 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5061 dc
.DrawLine( left
, y
, left
+cw
, y
);
5064 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5066 int cw
, ch
, dummy
, top
;
5067 m_gridWin
->GetClientSize( &cw
, &ch
);
5068 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5070 wxClientDC
dc( m_gridWin
);
5072 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5073 GetColMinimalWidth(m_dragRowOrCol
) );
5074 dc
.SetLogicalFunction(wxINVERT
);
5075 if ( m_dragLastPos
>= 0 )
5077 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5079 dc
.DrawLine( x
, top
, x
, top
+ch
);
5086 m_isDragging
= FALSE
;
5087 m_startDragPos
= wxDefaultPosition
;
5089 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5090 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5093 if ( event
.Entering() || event
.Leaving() )
5095 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5096 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5101 // ------------ Left button pressed
5103 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5105 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5110 if ( !event
.ControlDown() )
5112 if ( event
.ShiftDown() )
5116 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5117 m_currentCellCoords
.GetCol(),
5120 event
.ControlDown(),
5126 else if ( XToEdgeOfCol(x
) < 0 &&
5127 YToEdgeOfRow(y
) < 0 )
5129 DisableCellEditControl();
5130 MakeCellVisible( coords
);
5132 // if this is the second click on this cell then start
5134 if ( m_waitForSlowClick
&&
5135 (coords
== m_currentCellCoords
) &&
5136 CanEnableCellControl())
5138 EnableCellEditControl();
5140 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5141 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5144 editor
->StartingClick();
5148 m_waitForSlowClick
= FALSE
;
5152 if ( event
.ControlDown() )
5156 m_selection
->ToggleCellSelection( coords
.GetRow(),
5158 event
.ControlDown(),
5163 m_selectingTopLeft
= wxGridNoCellCoords
;
5164 m_selectingBottomRight
= wxGridNoCellCoords
;
5165 m_selectingKeyboard
= coords
;
5169 SetCurrentCell( coords
);
5172 if ( m_selection
->GetSelectionMode() !=
5173 wxGrid::wxGridSelectCells
)
5175 HighlightBlock( coords
, coords
);
5179 m_waitForSlowClick
= TRUE
;
5186 // ------------ Left double click
5188 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5190 DisableCellEditControl();
5192 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5194 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5202 // ------------ Left button released
5204 else if ( event
.LeftUp() )
5206 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5208 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5209 m_selectingBottomRight
!= wxGridNoCellCoords
)
5213 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5214 m_winCapture
= NULL
;
5219 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5220 m_selectingTopLeft
.GetCol(),
5221 m_selectingBottomRight
.GetRow(),
5222 m_selectingBottomRight
.GetCol(),
5223 event
.ControlDown(),
5229 m_selectingTopLeft
= wxGridNoCellCoords
;
5230 m_selectingBottomRight
= wxGridNoCellCoords
;
5233 // Show the edit control, if it has been hidden for
5235 ShowCellEditControl();
5237 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5239 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5240 DoEndDragResizeRow();
5242 // Note: we are ending the event *after* doing
5243 // default processing in this case
5245 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5247 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5249 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5250 DoEndDragResizeCol();
5252 // Note: we are ending the event *after* doing
5253 // default processing in this case
5255 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5262 // ------------ Right button down
5264 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5266 DisableCellEditControl();
5267 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5272 // no default action at the moment
5277 // ------------ Right double click
5279 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5281 DisableCellEditControl();
5282 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5287 // no default action at the moment
5291 // ------------ Moving and no button action
5293 else if ( event
.Moving() && !event
.IsButton() )
5295 int dragRow
= YToEdgeOfRow( y
);
5296 int dragCol
= XToEdgeOfCol( x
);
5298 // Dragging on the corner of a cell to resize in both
5299 // directions is not implemented yet...
5301 if ( dragRow
>= 0 && dragCol
>= 0 )
5303 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5309 m_dragRowOrCol
= dragRow
;
5311 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5313 if ( CanDragRowSize() && CanDragGridSize() )
5314 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5319 m_dragRowOrCol
= dragCol
;
5327 m_dragRowOrCol
= dragCol
;
5329 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5331 if ( CanDragColSize() && CanDragGridSize() )
5332 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5338 // Neither on a row or col edge
5340 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5342 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5348 void wxGrid::DoEndDragResizeRow()
5350 if ( m_dragLastPos
>= 0 )
5352 // erase the last line and resize the row
5354 int cw
, ch
, left
, dummy
;
5355 m_gridWin
->GetClientSize( &cw
, &ch
);
5356 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5358 wxClientDC
dc( m_gridWin
);
5360 dc
.SetLogicalFunction( wxINVERT
);
5361 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5362 HideCellEditControl();
5363 SaveEditControlValue();
5365 int rowTop
= GetRowTop(m_dragRowOrCol
);
5366 SetRowSize( m_dragRowOrCol
,
5367 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5369 if ( !GetBatchCount() )
5371 // Only needed to get the correct rect.y:
5372 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5374 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5375 rect
.width
= m_rowLabelWidth
;
5376 rect
.height
= ch
- rect
.y
;
5377 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5379 // if there is a multicell block, paint all of it
5382 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5383 int leftCol
= XToCol(left
);
5384 int rightCol
= XToCol(left
+cw
);
5387 if (rightCol
< 0) rightCol
= m_numCols
;
5388 for (i
=leftCol
; i
<rightCol
; i
++)
5390 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5391 if (cell_rows
< subtract_rows
)
5392 subtract_rows
= cell_rows
;
5394 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5395 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5396 rect
.height
= ch
- rect
.y
;
5399 m_gridWin
->Refresh( FALSE
, &rect
);
5402 ShowCellEditControl();
5407 void wxGrid::DoEndDragResizeCol()
5409 if ( m_dragLastPos
>= 0 )
5411 // erase the last line and resize the col
5413 int cw
, ch
, dummy
, top
;
5414 m_gridWin
->GetClientSize( &cw
, &ch
);
5415 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5417 wxClientDC
dc( m_gridWin
);
5419 dc
.SetLogicalFunction( wxINVERT
);
5420 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5421 HideCellEditControl();
5422 SaveEditControlValue();
5424 int colLeft
= GetColLeft(m_dragRowOrCol
);
5425 SetColSize( m_dragRowOrCol
,
5426 wxMax( m_dragLastPos
- colLeft
,
5427 GetColMinimalWidth(m_dragRowOrCol
) ) );
5429 if ( !GetBatchCount() )
5431 // Only needed to get the correct rect.x:
5432 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5434 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5435 rect
.width
= cw
- rect
.x
;
5436 rect
.height
= m_colLabelHeight
;
5437 m_colLabelWin
->Refresh( TRUE
, &rect
);
5439 // if there is a multicell block, paint all of it
5442 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5443 int topRow
= YToRow(top
);
5444 int bottomRow
= YToRow(top
+cw
);
5447 if (bottomRow
< 0) bottomRow
= m_numRows
;
5448 for (i
=topRow
; i
<bottomRow
; i
++)
5450 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5451 if (cell_cols
< subtract_cols
)
5452 subtract_cols
= cell_cols
;
5454 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5455 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5456 rect
.width
= cw
- rect
.x
;
5459 m_gridWin
->Refresh( FALSE
, &rect
);
5462 ShowCellEditControl();
5469 // ------ interaction with data model
5471 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5473 switch ( msg
.GetId() )
5475 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5476 return GetModelValues();
5478 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5479 return SetModelValues();
5481 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5482 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5483 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5484 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5485 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5486 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5487 return Redimension( msg
);
5496 // The behaviour of this function depends on the grid table class
5497 // Clear() function. For the default wxGridStringTable class the
5498 // behavious is to replace all cell contents with wxEmptyString but
5499 // not to change the number of rows or cols.
5501 void wxGrid::ClearGrid()
5505 if (IsCellEditControlEnabled())
5506 DisableCellEditControl();
5509 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5514 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5516 // TODO: something with updateLabels flag
5520 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5526 if (IsCellEditControlEnabled())
5527 DisableCellEditControl();
5529 return m_table
->InsertRows( pos
, numRows
);
5531 // the table will have sent the results of the insert row
5532 // operation to this view object as a grid table message
5538 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5540 // TODO: something with updateLabels flag
5544 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5548 return ( m_table
&& m_table
->AppendRows( numRows
) );
5549 // the table will have sent the results of the append row
5550 // operation to this view object as a grid table message
5554 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5556 // TODO: something with updateLabels flag
5560 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5566 if (IsCellEditControlEnabled())
5567 DisableCellEditControl();
5569 return (m_table
->DeleteRows( pos
, numRows
));
5570 // the table will have sent the results of the delete row
5571 // operation to this view object as a grid table message
5577 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5579 // TODO: something with updateLabels flag
5583 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5589 if (IsCellEditControlEnabled())
5590 DisableCellEditControl();
5592 return m_table
->InsertCols( pos
, numCols
);
5593 // the table will have sent the results of the insert col
5594 // operation to this view object as a grid table message
5600 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5602 // TODO: something with updateLabels flag
5606 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5610 return ( m_table
&& m_table
->AppendCols( numCols
) );
5611 // the table will have sent the results of the append col
5612 // operation to this view object as a grid table message
5616 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5618 // TODO: something with updateLabels flag
5622 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5628 if (IsCellEditControlEnabled())
5629 DisableCellEditControl();
5631 return ( m_table
->DeleteCols( pos
, numCols
) );
5632 // the table will have sent the results of the delete col
5633 // operation to this view object as a grid table message
5641 // ----- event handlers
5644 // Generate a grid event based on a mouse event and
5645 // return the result of ProcessEvent()
5647 int wxGrid::SendEvent( const wxEventType type
,
5649 wxMouseEvent
& mouseEv
)
5654 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5656 int rowOrCol
= (row
== -1 ? col
: row
);
5658 wxGridSizeEvent
gridEvt( GetId(),
5662 mouseEv
.GetX() + GetRowLabelSize(),
5663 mouseEv
.GetY() + GetColLabelSize(),
5664 mouseEv
.ControlDown(),
5665 mouseEv
.ShiftDown(),
5667 mouseEv
.MetaDown() );
5669 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5670 vetoed
= !gridEvt
.IsAllowed();
5672 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5674 // Right now, it should _never_ end up here!
5675 wxGridRangeSelectEvent
gridEvt( GetId(),
5679 m_selectingBottomRight
,
5681 mouseEv
.ControlDown(),
5682 mouseEv
.ShiftDown(),
5684 mouseEv
.MetaDown() );
5686 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5687 vetoed
= !gridEvt
.IsAllowed();
5691 wxGridEvent
gridEvt( GetId(),
5695 mouseEv
.GetX() + GetRowLabelSize(),
5696 mouseEv
.GetY() + GetColLabelSize(),
5698 mouseEv
.ControlDown(),
5699 mouseEv
.ShiftDown(),
5701 mouseEv
.MetaDown() );
5702 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5703 vetoed
= !gridEvt
.IsAllowed();
5706 // A Veto'd event may not be `claimed' so test this first
5707 if (vetoed
) return -1;
5708 return claimed
? 1 : 0;
5712 // Generate a grid event of specified type and return the result
5713 // of ProcessEvent().
5715 int wxGrid::SendEvent( const wxEventType type
,
5721 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5723 int rowOrCol
= (row
== -1 ? col
: row
);
5725 wxGridSizeEvent
gridEvt( GetId(),
5730 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5731 vetoed
= !gridEvt
.IsAllowed();
5735 wxGridEvent
gridEvt( GetId(),
5740 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5741 vetoed
= !gridEvt
.IsAllowed();
5744 // A Veto'd event may not be `claimed' so test this first
5745 if (vetoed
) return -1;
5746 return claimed
? 1 : 0;
5750 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5752 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5755 void wxGrid::Refresh(bool eraseb
, wxRect
* rect
)
5757 // Don't do anything if between Begin/EndBatch...
5758 // EndBatch() will do all this on the last nested one anyway.
5759 if (! GetBatchCount())
5761 wxScrolledWindow::Refresh(eraseb
,rect
);
5763 int off_x
=0 ,off_y
=0;
5764 wxRect
* anotherrect
= NULL
;
5768 //Copy rectangle can get scroll offsets..
5769 anotherrect
= new wxRect(*rect
);
5770 CalcScrolledPosition( 0, 0, &off_x
, &off_y
);
5772 //Corner label Doesn't move from the origin.
5773 m_cornerLabelWin
->Refresh(eraseb
,rect
);
5775 //Move Rect down for row labels...
5777 rect
->Offset(0,off_y
);
5778 m_rowLabelWin
->Refresh(eraseb
,rect
);
5780 //Move rect copy along for col labels...
5782 anotherrect
->Offset(off_x
,0);
5783 m_colLabelWin
->Refresh(eraseb
,anotherrect
);
5785 //Move main rect along (so it's down and across!)
5788 rect
->Offset(off_x
,0);
5789 m_gridWin
->Refresh(eraseb
,rect
);
5793 void wxGrid::OnSize( wxSizeEvent
& event
)
5795 // position the child windows
5798 // don't call CalcDimensions() from here, the base class handles the size
5804 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5806 if ( m_inOnKeyDown
)
5808 // shouldn't be here - we are going round in circles...
5810 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5813 m_inOnKeyDown
= TRUE
;
5815 // propagate the event up and see if it gets processed
5817 wxWindow
*parent
= GetParent();
5818 wxKeyEvent
keyEvt( event
);
5819 keyEvt
.SetEventObject( parent
);
5821 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5824 // try local handlers
5826 switch ( event
.KeyCode() )
5829 if ( event
.ControlDown() )
5831 MoveCursorUpBlock( event
.ShiftDown() );
5835 MoveCursorUp( event
.ShiftDown() );
5840 if ( event
.ControlDown() )
5842 MoveCursorDownBlock( event
.ShiftDown() );
5846 MoveCursorDown( event
.ShiftDown() );
5851 if ( event
.ControlDown() )
5853 MoveCursorLeftBlock( event
.ShiftDown() );
5857 MoveCursorLeft( event
.ShiftDown() );
5862 if ( event
.ControlDown() )
5864 MoveCursorRightBlock( event
.ShiftDown() );
5868 MoveCursorRight( event
.ShiftDown() );
5873 case WXK_NUMPAD_ENTER
:
5874 if ( event
.ControlDown() )
5876 event
.Skip(); // to let the edit control have the return
5880 if ( GetGridCursorRow() < GetNumberRows()-1 )
5882 MoveCursorDown( event
.ShiftDown() );
5886 // at the bottom of a column
5887 HideCellEditControl();
5888 SaveEditControlValue();
5898 if (event
.ShiftDown())
5900 if ( GetGridCursorCol() > 0 )
5902 MoveCursorLeft( FALSE
);
5907 HideCellEditControl();
5908 SaveEditControlValue();
5913 if ( GetGridCursorCol() < GetNumberCols()-1 )
5915 MoveCursorRight( FALSE
);
5920 HideCellEditControl();
5921 SaveEditControlValue();
5927 if ( event
.ControlDown() )
5929 MakeCellVisible( 0, 0 );
5930 SetCurrentCell( 0, 0 );
5939 if ( event
.ControlDown() )
5941 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5942 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5959 if ( event
.ControlDown() )
5963 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5964 m_currentCellCoords
.GetCol(),
5965 event
.ControlDown(),
5972 if ( !IsEditable() )
5974 MoveCursorRight( FALSE
);
5977 // Otherwise fall through to default
5980 // is it possible to edit the current cell at all?
5981 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5983 // yes, now check whether the cells editor accepts the key
5984 int row
= m_currentCellCoords
.GetRow();
5985 int col
= m_currentCellCoords
.GetCol();
5986 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5987 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5989 // <F2> is special and will always start editing, for
5990 // other keys - ask the editor itself
5991 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5992 || editor
->IsAcceptedKey(event
) )
5994 EnableCellEditControl();
5996 // the editor could be not shown for a variety of
5997 // reasons (i.e. blocked by the app or whatever), so
5998 // check if it really was created
5999 if ( m_cellEditCtrlEnabled
)
6001 editor
->StartingKey(event
);
6014 // let others process char events with modifiers or all
6015 // char events for readonly cells
6022 m_inOnKeyDown
= FALSE
;
6025 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6027 // try local handlers
6029 if ( event
.KeyCode() == WXK_SHIFT
)
6031 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6032 m_selectingBottomRight
!= wxGridNoCellCoords
)
6036 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6037 m_selectingTopLeft
.GetCol(),
6038 m_selectingBottomRight
.GetRow(),
6039 m_selectingBottomRight
.GetCol(),
6040 event
.ControlDown(),
6047 m_selectingTopLeft
= wxGridNoCellCoords
;
6048 m_selectingBottomRight
= wxGridNoCellCoords
;
6049 m_selectingKeyboard
= wxGridNoCellCoords
;
6053 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6057 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6059 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6061 // the event has been intercepted - do nothing
6065 wxClientDC
dc(m_gridWin
);
6068 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6070 HideCellEditControl();
6071 DisableCellEditControl();
6073 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6076 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6077 if ( !m_gridLinesEnabled
)
6085 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6087 // Otherwise refresh redraws the highlight!
6088 m_currentCellCoords
= coords
;
6090 DrawGridCellArea(dc
,cells
);
6091 DrawAllGridLines( dc
, r
);
6095 m_currentCellCoords
= coords
;
6097 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6098 DrawCellHighlight(dc
, attr
);
6103 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6106 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6110 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6113 rightCol
= GetNumberCols() - 1;
6115 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6118 bottomRow
= GetNumberRows() - 1;
6122 if ( topRow
> bottomRow
)
6129 if ( leftCol
> rightCol
)
6136 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6137 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6139 // First the case that we selected a completely new area
6140 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6141 m_selectingBottomRight
== wxGridNoCellCoords
)
6144 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6145 wxGridCellCoords ( bottomRow
, rightCol
) );
6146 m_gridWin
->Refresh( FALSE
, &rect
);
6148 // Now handle changing an existing selection area.
6149 else if ( m_selectingTopLeft
!= updateTopLeft
||
6150 m_selectingBottomRight
!= updateBottomRight
)
6152 // Compute two optimal update rectangles:
6153 // Either one rectangle is a real subset of the
6154 // other, or they are (almost) disjoint!
6156 bool need_refresh
[4];
6160 need_refresh
[3] = FALSE
;
6163 // Store intermediate values
6164 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6165 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6166 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6167 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6169 // Determine the outer/inner coordinates.
6170 if (oldLeft
> leftCol
)
6176 if (oldTop
> topRow
)
6182 if (oldRight
< rightCol
)
6185 oldRight
= rightCol
;
6188 if (oldBottom
< bottomRow
)
6191 oldBottom
= bottomRow
;
6195 // Now, either the stuff marked old is the outer
6196 // rectangle or we don't have a situation where one
6197 // is contained in the other.
6199 if ( oldLeft
< leftCol
)
6201 // Refresh the newly selected or deselected
6202 // area to the left of the old or new selection.
6203 need_refresh
[0] = TRUE
;
6204 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6206 wxGridCellCoords ( oldBottom
,
6210 if ( oldTop
< topRow
)
6212 // Refresh the newly selected or deselected
6213 // area above the old or new selection.
6214 need_refresh
[1] = TRUE
;
6215 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6217 wxGridCellCoords ( topRow
- 1,
6221 if ( oldRight
> rightCol
)
6223 // Refresh the newly selected or deselected
6224 // area to the right of the old or new selection.
6225 need_refresh
[2] = TRUE
;
6226 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6228 wxGridCellCoords ( oldBottom
,
6232 if ( oldBottom
> bottomRow
)
6234 // Refresh the newly selected or deselected
6235 // area below the old or new selection.
6236 need_refresh
[3] = TRUE
;
6237 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6239 wxGridCellCoords ( oldBottom
,
6243 // various Refresh() calls
6244 for (i
= 0; i
< 4; i
++ )
6245 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6246 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6249 m_selectingTopLeft
= updateTopLeft
;
6250 m_selectingBottomRight
= updateBottomRight
;
6254 // ------ functions to get/send data (see also public functions)
6257 bool wxGrid::GetModelValues()
6261 // all we need to do is repaint the grid
6263 m_gridWin
->Refresh();
6271 bool wxGrid::SetModelValues()
6277 for ( row
= 0; row
< m_numRows
; row
++ )
6279 for ( col
= 0; col
< m_numCols
; col
++ )
6281 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6293 // Note - this function only draws cells that are in the list of
6294 // exposed cells (usually set from the update region by
6295 // CalcExposedCells)
6297 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6299 if ( !m_numRows
|| !m_numCols
) return;
6301 int i
, j
, k
, l
, numCells
= cells
.GetCount();
6302 int row
, col
, cell_rows
, cell_cols
;
6303 wxGridCellCoordsArray redrawCells
;
6305 for ( i
= numCells
-1; i
>= 0; i
-- )
6307 row
= cells
[i
].GetRow();
6308 col
= cells
[i
].GetCol();
6309 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6311 // If this cell is part of a multicell block, find owner for repaint
6312 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6314 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6315 bool marked
= FALSE
;
6316 for ( j
= 0; j
< numCells
; j
++ )
6318 if ( cell
== cells
[j
] )
6326 int count
= redrawCells
.GetCount();
6327 for (j
= 0; j
< count
; j
++)
6329 if ( cell
== redrawCells
[j
] )
6335 if (!marked
) redrawCells
.Add( cell
);
6337 continue; // don't bother drawing this cell
6340 // If this cell is empty, find cell to left that might want to overflow
6341 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6343 for ( l
= 0; l
< cell_rows
; l
++ )
6345 for (j
= col
-1; j
>= 0; j
--)
6347 if (!m_table
->IsEmptyCell(row
+l
, j
))
6349 if (GetCellOverflow(row
+l
, j
))
6351 wxGridCellCoords
cell(row
+l
, j
);
6352 bool marked
= FALSE
;
6354 for (k
= 0; k
< numCells
; k
++)
6356 if ( cell
== cells
[k
] )
6364 int count
= redrawCells
.GetCount();
6365 for (k
= 0; k
< count
; k
++)
6367 if ( cell
== redrawCells
[k
] )
6373 if (!marked
) redrawCells
.Add( cell
);
6381 DrawCell( dc
, cells
[i
] );
6384 numCells
= redrawCells
.GetCount();
6386 for ( i
= numCells
- 1; i
>= 0; i
-- )
6388 DrawCell( dc
, redrawCells
[i
] );
6393 void wxGrid::DrawGridSpace( wxDC
& dc
)
6396 m_gridWin
->GetClientSize( &cw
, &ch
);
6399 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6401 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6402 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6404 if ( right
> rightCol
|| bottom
> bottomRow
)
6407 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6409 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6410 dc
.SetPen( *wxTRANSPARENT_PEN
);
6412 if ( right
> rightCol
)
6414 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6417 if ( bottom
> bottomRow
)
6419 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6425 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6427 int row
= coords
.GetRow();
6428 int col
= coords
.GetCol();
6430 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6433 // we draw the cell border ourselves
6434 #if !WXGRID_DRAW_LINES
6435 if ( m_gridLinesEnabled
)
6436 DrawCellBorder( dc
, coords
);
6439 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6441 bool isCurrent
= coords
== m_currentCellCoords
;
6443 wxRect rect
= CellToRect( row
, col
);
6445 // if the editor is shown, we should use it and not the renderer
6446 // Note: However, only if it is really _shown_, i.e. not hidden!
6447 if ( isCurrent
&& IsCellEditControlShown() )
6449 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6450 editor
->PaintBackground(rect
, attr
);
6455 // but all the rest is drawn by the cell renderer and hence may be
6457 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6458 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6465 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6467 int row
= m_currentCellCoords
.GetRow();
6468 int col
= m_currentCellCoords
.GetCol();
6470 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6473 wxRect rect
= CellToRect(row
, col
);
6475 // hmmm... what could we do here to show that the cell is disabled?
6476 // for now, I just draw a thinner border than for the other ones, but
6477 // it doesn't look really good
6479 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6483 // The center of th drawn line is where the position/width/height of
6484 // the rectangle is actually at, (on wxMSW atr least,) so we will
6485 // reduce the size of the rectangle to compensate for the thickness of
6486 // the line. If this is too strange on non wxMSW platforms then
6487 // please #ifdef this appropriately.
6488 rect
.x
+= penWidth
/2;
6489 rect
.y
+= penWidth
/2;
6490 rect
.width
-= penWidth
-1;
6491 rect
.height
-= penWidth
-1;
6494 // Now draw the rectangle
6495 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6496 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6497 dc
.DrawRectangle(rect
);
6501 // VZ: my experiments with 3d borders...
6503 // how to properly set colours for arbitrary bg?
6504 wxCoord x1
= rect
.x
,
6506 x2
= rect
.x
+ rect
.width
-1,
6507 y2
= rect
.y
+ rect
.height
-1;
6509 dc
.SetPen(*wxWHITE_PEN
);
6510 dc
.DrawLine(x1
, y1
, x2
, y1
);
6511 dc
.DrawLine(x1
, y1
, x1
, y2
);
6513 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6514 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6516 dc
.SetPen(*wxBLACK_PEN
);
6517 dc
.DrawLine(x1
, y2
, x2
, y2
);
6518 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6523 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6525 int row
= coords
.GetRow();
6526 int col
= coords
.GetCol();
6527 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6530 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6532 wxRect rect
= CellToRect( row
, col
);
6534 // right hand border
6536 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6537 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6541 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6542 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6545 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6547 // This if block was previously in wxGrid::OnPaint but that doesn't
6548 // seem to get called under wxGTK - MB
6550 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6551 m_numRows
&& m_numCols
)
6553 m_currentCellCoords
.Set(0, 0);
6556 if ( IsCellEditControlShown() )
6558 // don't show highlight when the edit control is shown
6562 // if the active cell was repainted, repaint its highlight too because it
6563 // might have been damaged by the grid lines
6564 size_t count
= cells
.GetCount();
6565 for ( size_t n
= 0; n
< count
; n
++ )
6567 if ( cells
[n
] == m_currentCellCoords
)
6569 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6570 DrawCellHighlight(dc
, attr
);
6578 // TODO: remove this ???
6579 // This is used to redraw all grid lines e.g. when the grid line colour
6582 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6584 #if !WXGRID_DRAW_LINES
6588 if ( !m_gridLinesEnabled
||
6590 !m_numCols
) return;
6592 int top
, bottom
, left
, right
;
6594 #if 0 //#ifndef __WXGTK__
6598 m_gridWin
->GetClientSize(&cw
, &ch
);
6600 // virtual coords of visible area
6602 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6603 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6608 reg
.GetBox(x
, y
, w
, h
);
6609 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6610 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6614 m_gridWin
->GetClientSize(&cw
, &ch
);
6615 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6616 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6619 // avoid drawing grid lines past the last row and col
6621 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6622 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6624 // no gridlines inside multicells, clip them out
6625 int leftCol
= XToCol(left
);
6626 int topRow
= YToRow(top
);
6627 int rightCol
= XToCol(right
);
6628 int bottomRow
= YToRow(bottom
);
6629 wxRegion
clippedcells(0, 0, cw
, ch
);
6631 if ((leftCol
>= 0) && (topRow
>= 0))
6633 if (rightCol
< 0) rightCol
= m_numCols
;
6634 if (bottomRow
< 0) bottomRow
= m_numRows
;
6636 int i
, j
, cell_rows
, cell_cols
;
6639 for (j
=topRow
; j
<bottomRow
; j
++)
6641 for (i
=leftCol
; i
<rightCol
; i
++)
6643 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6644 if ((cell_rows
> 1) || (cell_cols
> 1))
6646 rect
= CellToRect(j
,i
);
6647 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6648 clippedcells
.Subtract(rect
);
6650 else if ((cell_rows
< 0) || (cell_cols
< 0))
6652 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6653 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6654 clippedcells
.Subtract(rect
);
6659 dc
.SetClippingRegion( clippedcells
);
6661 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6663 // horizontal grid lines
6666 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6668 int bot
= GetRowBottom(i
) - 1;
6677 dc
.DrawLine( left
, bot
, right
, bot
);
6682 // vertical grid lines
6684 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6686 int colRight
= GetColRight(i
) - 1;
6687 if ( colRight
> right
)
6692 if ( colRight
>= left
)
6694 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6697 dc
.DestroyClippingRegion();
6701 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6703 if ( !m_numRows
) return;
6706 size_t numLabels
= rows
.GetCount();
6708 for ( i
= 0; i
< numLabels
; i
++ )
6710 DrawRowLabel( dc
, rows
[i
] );
6715 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6717 if ( GetRowHeight(row
) <= 0 )
6720 int rowTop
= GetRowTop(row
),
6721 rowBottom
= GetRowBottom(row
) - 1;
6723 dc
.SetPen( *wxBLACK_PEN
);
6724 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6725 m_rowLabelWidth
-1, rowBottom
);
6727 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6729 dc
.SetPen( *wxWHITE_PEN
);
6730 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6731 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6733 dc
.SetBackgroundMode( wxTRANSPARENT
);
6734 dc
.SetTextForeground( GetLabelTextColour() );
6735 dc
.SetFont( GetLabelFont() );
6738 GetRowLabelAlignment( &hAlign
, &vAlign
);
6742 rect
.SetY( GetRowTop(row
) + 2 );
6743 rect
.SetWidth( m_rowLabelWidth
- 4 );
6744 rect
.SetHeight( GetRowHeight(row
) - 4 );
6745 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6749 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6751 if ( !m_numCols
) return;
6754 size_t numLabels
= cols
.GetCount();
6756 for ( i
= 0; i
< numLabels
; i
++ )
6758 DrawColLabel( dc
, cols
[i
] );
6763 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6765 if ( GetColWidth(col
) <= 0 )
6768 int colLeft
= GetColLeft(col
),
6769 colRight
= GetColRight(col
) - 1;
6771 dc
.SetPen( *wxBLACK_PEN
);
6772 dc
.DrawLine( colRight
, 0,
6773 colRight
, m_colLabelHeight
-1 );
6775 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6776 colRight
, m_colLabelHeight
-1 );
6778 dc
.SetPen( *wxWHITE_PEN
);
6779 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6780 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6782 dc
.SetBackgroundMode( wxTRANSPARENT
);
6783 dc
.SetTextForeground( GetLabelTextColour() );
6784 dc
.SetFont( GetLabelFont() );
6786 dc
.SetBackgroundMode( wxTRANSPARENT
);
6787 dc
.SetTextForeground( GetLabelTextColour() );
6788 dc
.SetFont( GetLabelFont() );
6791 GetColLabelAlignment( &hAlign
, &vAlign
);
6794 rect
.SetX( colLeft
+ 2 );
6796 rect
.SetWidth( GetColWidth(col
) - 4 );
6797 rect
.SetHeight( m_colLabelHeight
- 4 );
6798 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6801 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6802 const wxString
& value
,
6807 wxArrayString lines
;
6809 StringToLines( value
, lines
);
6812 //Forward to new API.
6813 DrawTextRectangle( dc
,
6821 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6822 const wxArrayString
& lines
,
6827 long textWidth
, textHeight
;
6828 long lineWidth
, lineHeight
;
6830 dc
.SetClippingRegion( rect
);
6831 if ( lines
.GetCount() )
6833 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6834 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6837 switch ( horizAlign
)
6840 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6843 case wxALIGN_CENTRE
:
6844 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6853 switch ( vertAlign
)
6855 case wxALIGN_BOTTOM
:
6856 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6859 case wxALIGN_CENTRE
:
6860 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6869 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6871 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6876 dc
.DestroyClippingRegion();
6880 // Split multi line text up into an array of strings. Any existing
6881 // contents of the string array are preserved.
6883 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6887 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6888 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6890 while ( startPos
< (int)tVal
.Length() )
6892 pos
= tVal
.Mid(startPos
).Find( eol
);
6897 else if ( pos
== 0 )
6899 lines
.Add( wxEmptyString
);
6903 lines
.Add( value
.Mid(startPos
, pos
) );
6907 if ( startPos
< (int)value
.Length() )
6909 lines
.Add( value
.Mid( startPos
) );
6914 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6915 const wxArrayString
& lines
,
6916 long *width
, long *height
)
6923 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6925 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6926 w
= wxMax( w
, lineW
);
6935 // ------ Batch processing.
6937 void wxGrid::EndBatch()
6939 if ( m_batchCount
> 0 )
6942 if ( !m_batchCount
)
6945 m_rowLabelWin
->Refresh();
6946 m_colLabelWin
->Refresh();
6947 m_cornerLabelWin
->Refresh();
6948 m_gridWin
->Refresh();
6953 // Use this, rather than wxWindow::Refresh(), to force an immediate
6954 // repainting of the grid. Has no effect if you are already inside a
6955 // BeginBatch / EndBatch block.
6957 void wxGrid::ForceRefresh()
6965 // ------ Edit control functions
6969 void wxGrid::EnableEditing( bool edit
)
6971 // TODO: improve this ?
6973 if ( edit
!= m_editable
)
6975 if(!edit
) EnableCellEditControl(edit
);
6981 void wxGrid::EnableCellEditControl( bool enable
)
6986 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6987 SetCurrentCell( 0, 0 );
6989 if ( enable
!= m_cellEditCtrlEnabled
)
6993 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6996 // this should be checked by the caller!
6997 wxASSERT_MSG( CanEnableCellControl(),
6998 _T("can't enable editing for this cell!") );
7000 // do it before ShowCellEditControl()
7001 m_cellEditCtrlEnabled
= enable
;
7003 ShowCellEditControl();
7007 //FIXME:add veto support
7008 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7010 HideCellEditControl();
7011 SaveEditControlValue();
7013 // do it after HideCellEditControl()
7014 m_cellEditCtrlEnabled
= enable
;
7019 bool wxGrid::IsCurrentCellReadOnly() const
7022 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7023 bool readonly
= attr
->IsReadOnly();
7029 bool wxGrid::CanEnableCellControl() const
7031 return m_editable
&& !IsCurrentCellReadOnly();
7034 bool wxGrid::IsCellEditControlEnabled() const
7036 // the cell edit control might be disable for all cells or just for the
7037 // current one if it's read only
7038 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7041 bool wxGrid::IsCellEditControlShown() const
7043 bool isShown
= FALSE
;
7045 if ( m_cellEditCtrlEnabled
)
7047 int row
= m_currentCellCoords
.GetRow();
7048 int col
= m_currentCellCoords
.GetCol();
7049 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7050 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7055 if ( editor
->IsCreated() )
7057 isShown
= editor
->GetControl()->IsShown();
7067 void wxGrid::ShowCellEditControl()
7069 if ( IsCellEditControlEnabled() )
7071 if ( !IsVisible( m_currentCellCoords
) )
7073 m_cellEditCtrlEnabled
= FALSE
;
7078 wxRect rect
= CellToRect( m_currentCellCoords
);
7079 int row
= m_currentCellCoords
.GetRow();
7080 int col
= m_currentCellCoords
.GetCol();
7082 // if this is part of a multicell, find owner (topleft)
7083 int cell_rows
, cell_cols
;
7084 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7085 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7089 m_currentCellCoords
.SetRow( row
);
7090 m_currentCellCoords
.SetCol( col
);
7093 // convert to scrolled coords
7095 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7097 // done in PaintBackground()
7099 // erase the highlight and the cell contents because the editor
7100 // might not cover the entire cell
7101 wxClientDC
dc( m_gridWin
);
7103 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7104 dc
.SetPen(*wxTRANSPARENT_PEN
);
7105 dc
.DrawRectangle(rect
);
7108 // cell is shifted by one pixel
7109 // However, don't allow x or y to become negative
7110 // since the SetSize() method interprets that as
7117 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7118 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7119 if ( !editor
->IsCreated() )
7121 editor
->Create(m_gridWin
, -1,
7122 new wxGridCellEditorEvtHandler(this, editor
));
7124 wxGridEditorCreatedEvent
evt(GetId(),
7125 wxEVT_GRID_EDITOR_CREATED
,
7129 editor
->GetControl());
7130 GetEventHandler()->ProcessEvent(evt
);
7133 editor
->Show( TRUE
, attr
);
7135 // resize editor to overflow into righthand cells if allowed
7136 wxString value
= GetCellValue(row
, col
);
7137 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7139 wxClientDC
dc(m_gridWin
);
7140 wxCoord y
= 0, best_width
= 0;
7141 dc
.SetFont(attr
->GetFont());
7142 dc
.GetTextExtent(value
, &best_width
, &y
);
7144 int cell_rows
, cell_cols
;
7145 attr
->GetSize( &cell_rows
, &cell_cols
);
7147 if ((best_width
> rect
.width
) && (col
< m_numCols
) && m_table
)
7150 for (i
= col
+cell_cols
; i
< m_numCols
; i
++)
7152 if (m_table
->IsEmptyCell(row
,i
))
7154 rect
.width
+= GetColWidth(i
);
7155 if (rect
.width
>= best_width
) break;
7162 editor
->SetSize( rect
);
7164 editor
->BeginEdit(row
, col
, this);
7173 void wxGrid::HideCellEditControl()
7175 if ( IsCellEditControlEnabled() )
7177 int row
= m_currentCellCoords
.GetRow();
7178 int col
= m_currentCellCoords
.GetCol();
7180 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7181 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7182 editor
->Show( FALSE
);
7185 m_gridWin
->SetFocus();
7186 // refresh whole row to the right
7187 wxRect
rect( CellToRect(row
, col
) );
7188 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7189 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7190 m_gridWin
->Refresh( FALSE
, &rect
);
7195 void wxGrid::SaveEditControlValue()
7197 if ( IsCellEditControlEnabled() )
7199 int row
= m_currentCellCoords
.GetRow();
7200 int col
= m_currentCellCoords
.GetCol();
7202 wxString oldval
= GetCellValue(row
,col
);
7204 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7205 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7206 bool changed
= editor
->EndEdit(row
, col
, this);
7213 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7214 m_currentCellCoords
.GetRow(),
7215 m_currentCellCoords
.GetCol() ) < 0 ) {
7217 // Event has been vetoed, set the data back.
7218 SetCellValue(row
,col
,oldval
);
7226 // ------ Grid location functions
7227 // Note that all of these functions work with the logical coordinates of
7228 // grid cells and labels so you will need to convert from device
7229 // coordinates for mouse events etc.
7232 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7234 int row
= YToRow(y
);
7235 int col
= XToCol(x
);
7237 if ( row
== -1 || col
== -1 )
7239 coords
= wxGridNoCellCoords
;
7243 coords
.Set( row
, col
);
7248 // Internal Helper function for computing row or column from some
7249 // (unscrolled) coordinate value, using either
7250 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7251 // of m_rowBottoms/m_ColRights to speed up the search!
7253 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7254 const wxArrayInt
& BorderArray
, bool maxOnOverflow
)
7258 size_t i_max
= coord
/ defaultDist
,
7260 if (BorderArray
.IsEmpty())
7262 return maxOnOverflow
? (int)i_max
: -1;
7265 if ( i_max
>= BorderArray
.GetCount())
7266 i_max
= BorderArray
.GetCount() - 1;
7269 if ( coord
>= BorderArray
[i_max
])
7272 i_max
= coord
/ minDist
;
7274 if ( i_max
>= BorderArray
.GetCount())
7275 i_max
= BorderArray
.GetCount() - 1;
7277 if ( coord
>= BorderArray
[i_max
])
7278 return maxOnOverflow
? (int)i_max
: -1;
7279 if ( coord
< BorderArray
[0] )
7282 while ( i_max
- i_min
> 0 )
7284 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7285 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7286 if (coord
>= BorderArray
[ i_max
- 1])
7290 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7291 if (coord
< BorderArray
[median
])
7299 int wxGrid::YToRow( int y
)
7301 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7302 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, FALSE
);
7306 int wxGrid::XToCol( int x
)
7308 return CoordToRowOrCol(x
, m_defaultColWidth
,
7309 WXGRID_MIN_COL_WIDTH
, m_colRights
, FALSE
);
7313 // return the row number that that the y coord is near the edge of, or
7314 // -1 if not near an edge
7316 int wxGrid::YToEdgeOfRow( int y
)
7319 i
= internalYToRow(y
);
7321 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7323 // We know that we are in row i, test whether we are
7324 // close enough to lower or upper border, respectively.
7325 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7327 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7335 // return the col number that that the x coord is near the edge of, or
7336 // -1 if not near an edge
7338 int wxGrid::XToEdgeOfCol( int x
)
7341 i
= internalXToCol(x
);
7343 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7345 // We know that we are in column i, test whether we are
7346 // close enough to right or left border, respectively.
7347 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7349 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7357 wxRect
wxGrid::CellToRect( int row
, int col
)
7359 wxRect
rect( -1, -1, -1, -1 );
7361 if ( row
>= 0 && row
< m_numRows
&&
7362 col
>= 0 && col
< m_numCols
)
7364 int i
, cell_rows
, cell_cols
;
7365 rect
.width
= rect
.height
= 0;
7366 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7367 // if negative then find multicell owner
7368 if (cell_rows
< 0) row
+= cell_rows
;
7369 if (cell_cols
< 0) col
+= cell_cols
;
7370 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7372 rect
.x
= GetColLeft(col
);
7373 rect
.y
= GetRowTop(row
);
7374 for (i
=col
; i
<col
+cell_cols
; i
++)
7375 rect
.width
+= GetColWidth(i
);
7376 for (i
=row
; i
<row
+cell_rows
; i
++)
7377 rect
.height
+= GetRowHeight(i
);
7380 // if grid lines are enabled, then the area of the cell is a bit smaller
7381 if (m_gridLinesEnabled
) {
7389 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7391 // get the cell rectangle in logical coords
7393 wxRect
r( CellToRect( row
, col
) );
7395 // convert to device coords
7397 int left
, top
, right
, bottom
;
7398 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7399 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7401 // check against the client area of the grid window
7404 m_gridWin
->GetClientSize( &cw
, &ch
);
7406 if ( wholeCellVisible
)
7408 // is the cell wholly visible ?
7410 return ( left
>= 0 && right
<= cw
&&
7411 top
>= 0 && bottom
<= ch
);
7415 // is the cell partly visible ?
7417 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7418 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7423 // make the specified cell location visible by doing a minimal amount
7426 void wxGrid::MakeCellVisible( int row
, int col
)
7430 int xpos
= -1, ypos
= -1;
7432 if ( row
>= 0 && row
< m_numRows
&&
7433 col
>= 0 && col
< m_numCols
)
7435 // get the cell rectangle in logical coords
7437 wxRect
r( CellToRect( row
, col
) );
7439 // convert to device coords
7441 int left
, top
, right
, bottom
;
7442 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7443 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7446 m_gridWin
->GetClientSize( &cw
, &ch
);
7452 else if ( bottom
> ch
)
7454 int h
= r
.GetHeight();
7456 for ( i
= row
-1; i
>= 0; i
-- )
7458 int rowHeight
= GetRowHeight(i
);
7459 if ( h
+ rowHeight
> ch
)
7466 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7467 // have rounding errors (this is important, because if we do, we
7468 // might not scroll at all and some cells won't be redrawn)
7470 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7472 ypos
+= GRID_SCROLL_LINE_Y
;
7479 else if ( right
> cw
)
7481 int w
= r
.GetWidth();
7483 for ( i
= col
-1; i
>= 0; i
-- )
7485 int colWidth
= GetColWidth(i
);
7486 if ( w
+ colWidth
> cw
)
7493 // see comment for ypos above
7494 xpos
+= GRID_SCROLL_LINE_X
;
7497 if ( xpos
!= -1 || ypos
!= -1 )
7500 xpos
/= GRID_SCROLL_LINE_X
;
7502 ypos
/= GRID_SCROLL_LINE_Y
;
7503 Scroll( xpos
, ypos
);
7511 // ------ Grid cursor movement functions
7514 bool wxGrid::MoveCursorUp( bool expandSelection
)
7516 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7517 m_currentCellCoords
.GetRow() >= 0 )
7519 if ( expandSelection
)
7521 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7522 m_selectingKeyboard
= m_currentCellCoords
;
7523 if ( m_selectingKeyboard
.GetRow() > 0 )
7525 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7526 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7527 m_selectingKeyboard
.GetCol() );
7528 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7531 else if ( m_currentCellCoords
.GetRow() > 0 )
7534 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7535 m_currentCellCoords
.GetCol() );
7536 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7537 m_currentCellCoords
.GetCol() );
7548 bool wxGrid::MoveCursorDown( bool expandSelection
)
7550 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7551 m_currentCellCoords
.GetRow() < m_numRows
)
7553 if ( expandSelection
)
7555 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7556 m_selectingKeyboard
= m_currentCellCoords
;
7557 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7559 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7560 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7561 m_selectingKeyboard
.GetCol() );
7562 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7565 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7568 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7569 m_currentCellCoords
.GetCol() );
7570 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7571 m_currentCellCoords
.GetCol() );
7582 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7584 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7585 m_currentCellCoords
.GetCol() >= 0 )
7587 if ( expandSelection
)
7589 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7590 m_selectingKeyboard
= m_currentCellCoords
;
7591 if ( m_selectingKeyboard
.GetCol() > 0 )
7593 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7594 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7595 m_selectingKeyboard
.GetCol() );
7596 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7599 else if ( m_currentCellCoords
.GetCol() > 0 )
7602 MakeCellVisible( m_currentCellCoords
.GetRow(),
7603 m_currentCellCoords
.GetCol() - 1 );
7604 SetCurrentCell( m_currentCellCoords
.GetRow(),
7605 m_currentCellCoords
.GetCol() - 1 );
7616 bool wxGrid::MoveCursorRight( bool expandSelection
)
7618 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7619 m_currentCellCoords
.GetCol() < m_numCols
)
7621 if ( expandSelection
)
7623 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7624 m_selectingKeyboard
= m_currentCellCoords
;
7625 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7627 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7628 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7629 m_selectingKeyboard
.GetCol() );
7630 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7633 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7636 MakeCellVisible( m_currentCellCoords
.GetRow(),
7637 m_currentCellCoords
.GetCol() + 1 );
7638 SetCurrentCell( m_currentCellCoords
.GetRow(),
7639 m_currentCellCoords
.GetCol() + 1 );
7650 bool wxGrid::MovePageUp()
7652 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7654 int row
= m_currentCellCoords
.GetRow();
7658 m_gridWin
->GetClientSize( &cw
, &ch
);
7660 int y
= GetRowTop(row
);
7661 int newRow
= YToRow( y
- ch
+ 1 );
7666 else if ( newRow
== row
)
7671 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7672 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7680 bool wxGrid::MovePageDown()
7682 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7684 int row
= m_currentCellCoords
.GetRow();
7685 if ( row
< m_numRows
)
7688 m_gridWin
->GetClientSize( &cw
, &ch
);
7690 int y
= GetRowTop(row
);
7691 int newRow
= YToRow( y
+ ch
);
7694 newRow
= m_numRows
- 1;
7696 else if ( newRow
== row
)
7701 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7702 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7710 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7713 m_currentCellCoords
!= wxGridNoCellCoords
&&
7714 m_currentCellCoords
.GetRow() > 0 )
7716 int row
= m_currentCellCoords
.GetRow();
7717 int col
= m_currentCellCoords
.GetCol();
7719 if ( m_table
->IsEmptyCell(row
, col
) )
7721 // starting in an empty cell: find the next block of
7727 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7730 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7732 // starting at the top of a block: find the next block
7738 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7743 // starting within a block: find the top of the block
7748 if ( m_table
->IsEmptyCell(row
, col
) )
7756 MakeCellVisible( row
, col
);
7757 if ( expandSelection
)
7759 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7760 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7765 SetCurrentCell( row
, col
);
7773 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7776 m_currentCellCoords
!= wxGridNoCellCoords
&&
7777 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7779 int row
= m_currentCellCoords
.GetRow();
7780 int col
= m_currentCellCoords
.GetCol();
7782 if ( m_table
->IsEmptyCell(row
, col
) )
7784 // starting in an empty cell: find the next block of
7787 while ( row
< m_numRows
-1 )
7790 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7793 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7795 // starting at the bottom of a block: find the next block
7798 while ( row
< m_numRows
-1 )
7801 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7806 // starting within a block: find the bottom of the block
7808 while ( row
< m_numRows
-1 )
7811 if ( m_table
->IsEmptyCell(row
, col
) )
7819 MakeCellVisible( row
, col
);
7820 if ( expandSelection
)
7822 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7823 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7828 SetCurrentCell( row
, col
);
7837 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7840 m_currentCellCoords
!= wxGridNoCellCoords
&&
7841 m_currentCellCoords
.GetCol() > 0 )
7843 int row
= m_currentCellCoords
.GetRow();
7844 int col
= m_currentCellCoords
.GetCol();
7846 if ( m_table
->IsEmptyCell(row
, col
) )
7848 // starting in an empty cell: find the next block of
7854 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7857 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7859 // starting at the left of a block: find the next block
7865 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7870 // starting within a block: find the left of the block
7875 if ( m_table
->IsEmptyCell(row
, col
) )
7883 MakeCellVisible( row
, col
);
7884 if ( expandSelection
)
7886 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7887 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7892 SetCurrentCell( row
, col
);
7901 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7904 m_currentCellCoords
!= wxGridNoCellCoords
&&
7905 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7907 int row
= m_currentCellCoords
.GetRow();
7908 int col
= m_currentCellCoords
.GetCol();
7910 if ( m_table
->IsEmptyCell(row
, col
) )
7912 // starting in an empty cell: find the next block of
7915 while ( col
< m_numCols
-1 )
7918 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7921 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7923 // starting at the right of a block: find the next block
7926 while ( col
< m_numCols
-1 )
7929 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7934 // starting within a block: find the right of the block
7936 while ( col
< m_numCols
-1 )
7939 if ( m_table
->IsEmptyCell(row
, col
) )
7947 MakeCellVisible( row
, col
);
7948 if ( expandSelection
)
7950 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7951 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7956 SetCurrentCell( row
, col
);
7968 // ------ Label values and formatting
7971 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7973 *horiz
= m_rowLabelHorizAlign
;
7974 *vert
= m_rowLabelVertAlign
;
7977 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7979 *horiz
= m_colLabelHorizAlign
;
7980 *vert
= m_colLabelVertAlign
;
7983 wxString
wxGrid::GetRowLabelValue( int row
)
7987 return m_table
->GetRowLabelValue( row
);
7997 wxString
wxGrid::GetColLabelValue( int col
)
8001 return m_table
->GetColLabelValue( col
);
8012 void wxGrid::SetRowLabelSize( int width
)
8014 width
= wxMax( width
, 0 );
8015 if ( width
!= m_rowLabelWidth
)
8019 m_rowLabelWin
->Show( FALSE
);
8020 m_cornerLabelWin
->Show( FALSE
);
8022 else if ( m_rowLabelWidth
== 0 )
8024 m_rowLabelWin
->Show( TRUE
);
8025 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8028 m_rowLabelWidth
= width
;
8030 wxScrolledWindow::Refresh( TRUE
);
8035 void wxGrid::SetColLabelSize( int height
)
8037 height
= wxMax( height
, 0 );
8038 if ( height
!= m_colLabelHeight
)
8042 m_colLabelWin
->Show( FALSE
);
8043 m_cornerLabelWin
->Show( FALSE
);
8045 else if ( m_colLabelHeight
== 0 )
8047 m_colLabelWin
->Show( TRUE
);
8048 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8051 m_colLabelHeight
= height
;
8053 wxScrolledWindow::Refresh( TRUE
);
8058 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8060 if ( m_labelBackgroundColour
!= colour
)
8062 m_labelBackgroundColour
= colour
;
8063 m_rowLabelWin
->SetBackgroundColour( colour
);
8064 m_colLabelWin
->SetBackgroundColour( colour
);
8065 m_cornerLabelWin
->SetBackgroundColour( colour
);
8067 if ( !GetBatchCount() )
8069 m_rowLabelWin
->Refresh();
8070 m_colLabelWin
->Refresh();
8071 m_cornerLabelWin
->Refresh();
8076 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8078 if ( m_labelTextColour
!= colour
)
8080 m_labelTextColour
= colour
;
8081 if ( !GetBatchCount() )
8083 m_rowLabelWin
->Refresh();
8084 m_colLabelWin
->Refresh();
8089 void wxGrid::SetLabelFont( const wxFont
& font
)
8092 if ( !GetBatchCount() )
8094 m_rowLabelWin
->Refresh();
8095 m_colLabelWin
->Refresh();
8099 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8101 // allow old (incorrect) defs to be used
8104 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8105 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8106 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8111 case wxTOP
: vert
= wxALIGN_TOP
; break;
8112 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8113 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8116 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8118 m_rowLabelHorizAlign
= horiz
;
8121 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8123 m_rowLabelVertAlign
= vert
;
8126 if ( !GetBatchCount() )
8128 m_rowLabelWin
->Refresh();
8132 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8134 // allow old (incorrect) defs to be used
8137 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8138 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8139 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8144 case wxTOP
: vert
= wxALIGN_TOP
; break;
8145 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8146 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8149 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8151 m_colLabelHorizAlign
= horiz
;
8154 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8156 m_colLabelVertAlign
= vert
;
8159 if ( !GetBatchCount() )
8161 m_colLabelWin
->Refresh();
8165 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8169 m_table
->SetRowLabelValue( row
, s
);
8170 if ( !GetBatchCount() )
8172 wxRect rect
= CellToRect( row
, 0);
8173 if ( rect
.height
> 0 )
8175 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8177 rect
.width
= m_rowLabelWidth
;
8178 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8184 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8188 m_table
->SetColLabelValue( col
, s
);
8189 if ( !GetBatchCount() )
8191 wxRect rect
= CellToRect( 0, col
);
8192 if ( rect
.width
> 0 )
8194 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8196 rect
.height
= m_colLabelHeight
;
8197 m_colLabelWin
->Refresh( TRUE
, &rect
);
8203 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8205 if ( m_gridLineColour
!= colour
)
8207 m_gridLineColour
= colour
;
8209 wxClientDC
dc( m_gridWin
);
8211 DrawAllGridLines( dc
, wxRegion() );
8216 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8218 if ( m_cellHighlightColour
!= colour
)
8220 m_cellHighlightColour
= colour
;
8222 wxClientDC
dc( m_gridWin
);
8224 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8225 DrawCellHighlight(dc
, attr
);
8230 void wxGrid::SetCellHighlightPenWidth(int width
)
8232 if (m_cellHighlightPenWidth
!= width
) {
8233 m_cellHighlightPenWidth
= width
;
8235 // Just redrawing the cell highlight is not enough since that won't
8236 // make any visible change if the the thickness is getting smaller.
8237 int row
= m_currentCellCoords
.GetRow();
8238 int col
= m_currentCellCoords
.GetCol();
8239 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8241 wxRect rect
= CellToRect(row
, col
);
8242 m_gridWin
->Refresh(TRUE
, &rect
);
8246 void wxGrid::SetCellHighlightROPenWidth(int width
)
8248 if (m_cellHighlightROPenWidth
!= width
) {
8249 m_cellHighlightROPenWidth
= width
;
8251 // Just redrawing the cell highlight is not enough since that won't
8252 // make any visible change if the the thickness is getting smaller.
8253 int row
= m_currentCellCoords
.GetRow();
8254 int col
= m_currentCellCoords
.GetCol();
8255 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8257 wxRect rect
= CellToRect(row
, col
);
8258 m_gridWin
->Refresh(TRUE
, &rect
);
8262 void wxGrid::EnableGridLines( bool enable
)
8264 if ( enable
!= m_gridLinesEnabled
)
8266 m_gridLinesEnabled
= enable
;
8268 if ( !GetBatchCount() )
8272 wxClientDC
dc( m_gridWin
);
8274 DrawAllGridLines( dc
, wxRegion() );
8278 m_gridWin
->Refresh();
8285 int wxGrid::GetDefaultRowSize()
8287 return m_defaultRowHeight
;
8290 int wxGrid::GetRowSize( int row
)
8292 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8294 return GetRowHeight(row
);
8297 int wxGrid::GetDefaultColSize()
8299 return m_defaultColWidth
;
8302 int wxGrid::GetColSize( int col
)
8304 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8306 return GetColWidth(col
);
8309 // ============================================================================
8310 // access to the grid attributes: each of them has a default value in the grid
8311 // itself and may be overidden on a per-cell basis
8312 // ============================================================================
8314 // ----------------------------------------------------------------------------
8315 // setting default attributes
8316 // ----------------------------------------------------------------------------
8318 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8320 m_defaultCellAttr
->SetBackgroundColour(col
);
8322 m_gridWin
->SetBackgroundColour(col
);
8326 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8328 m_defaultCellAttr
->SetTextColour(col
);
8331 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8333 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8336 void wxGrid::SetDefaultCellOverflow( bool allow
)
8338 m_defaultCellAttr
->SetOverflow(allow
);
8341 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8343 m_defaultCellAttr
->SetFont(font
);
8346 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8348 m_defaultCellAttr
->SetRenderer(renderer
);
8351 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8353 m_defaultCellAttr
->SetEditor(editor
);
8356 // ----------------------------------------------------------------------------
8357 // access to the default attrbiutes
8358 // ----------------------------------------------------------------------------
8360 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8362 return m_defaultCellAttr
->GetBackgroundColour();
8365 wxColour
wxGrid::GetDefaultCellTextColour()
8367 return m_defaultCellAttr
->GetTextColour();
8370 wxFont
wxGrid::GetDefaultCellFont()
8372 return m_defaultCellAttr
->GetFont();
8375 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8377 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8380 bool wxGrid::GetDefaultCellOverflow()
8382 return m_defaultCellAttr
->GetOverflow();
8385 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8387 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8390 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8392 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8395 // ----------------------------------------------------------------------------
8396 // access to cell attributes
8397 // ----------------------------------------------------------------------------
8399 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8401 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8402 wxColour colour
= attr
->GetBackgroundColour();
8407 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8409 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8410 wxColour colour
= attr
->GetTextColour();
8415 wxFont
wxGrid::GetCellFont( int row
, int col
)
8417 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8418 wxFont font
= attr
->GetFont();
8423 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8425 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8426 attr
->GetAlignment(horiz
, vert
);
8430 bool wxGrid::GetCellOverflow( int row
, int col
)
8432 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8433 bool allow
= attr
->GetOverflow();
8438 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8440 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8441 attr
->GetSize( num_rows
, num_cols
);
8445 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8447 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8448 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8454 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8456 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8457 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8463 bool wxGrid::IsReadOnly(int row
, int col
) const
8465 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8466 bool isReadOnly
= attr
->IsReadOnly();
8471 // ----------------------------------------------------------------------------
8472 // attribute support: cache, automatic provider creation, ...
8473 // ----------------------------------------------------------------------------
8475 bool wxGrid::CanHaveAttributes()
8482 return m_table
->CanHaveAttributes();
8485 void wxGrid::ClearAttrCache()
8487 if ( m_attrCache
.row
!= -1 )
8489 wxSafeDecRef(m_attrCache
.attr
);
8490 m_attrCache
.attr
= NULL
;
8491 m_attrCache
.row
= -1;
8495 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8497 wxGrid
*self
= (wxGrid
*)this; // const_cast
8499 self
->ClearAttrCache();
8500 self
->m_attrCache
.row
= row
;
8501 self
->m_attrCache
.col
= col
;
8502 self
->m_attrCache
.attr
= attr
;
8506 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8508 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8510 *attr
= m_attrCache
.attr
;
8511 wxSafeIncRef(m_attrCache
.attr
);
8513 #ifdef DEBUG_ATTR_CACHE
8514 gs_nAttrCacheHits
++;
8521 #ifdef DEBUG_ATTR_CACHE
8522 gs_nAttrCacheMisses
++;
8528 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8530 wxGridCellAttr
*attr
= NULL
;
8531 // Additional test to avoid looking at the cache e.g. for
8532 // wxNoCellCoords, as this will confuse memory management.
8535 if ( !LookupAttr(row
, col
, &attr
) )
8537 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8538 : (wxGridCellAttr
*)NULL
;
8539 CacheAttr(row
, col
, attr
);
8544 attr
->SetDefAttr(m_defaultCellAttr
);
8548 attr
= m_defaultCellAttr
;
8555 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8557 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8559 wxCHECK_MSG( m_table
, attr
,
8560 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8562 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8565 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8567 // artificially inc the ref count to match DecRef() in caller
8569 m_table
->SetAttr(attr
, row
, col
);
8575 // ----------------------------------------------------------------------------
8576 // setting column attributes (wrappers around SetColAttr)
8577 // ----------------------------------------------------------------------------
8579 void wxGrid::SetColFormatBool(int col
)
8581 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8584 void wxGrid::SetColFormatNumber(int col
)
8586 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8589 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8591 wxString typeName
= wxGRID_VALUE_FLOAT
;
8592 if ( (width
!= -1) || (precision
!= -1) )
8594 typeName
<< _T(':') << width
<< _T(',') << precision
;
8597 SetColFormatCustom(col
, typeName
);
8600 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8602 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8604 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8606 attr
= new wxGridCellAttr
;
8607 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8608 attr
->SetRenderer(renderer
);
8610 SetColAttr(col
, attr
);
8614 // ----------------------------------------------------------------------------
8615 // setting cell attributes: this is forwarded to the table
8616 // ----------------------------------------------------------------------------
8618 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8620 if ( CanHaveAttributes() )
8622 m_table
->SetAttr(attr
, row
, col
);
8631 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8633 if ( CanHaveAttributes() )
8635 m_table
->SetRowAttr(attr
, row
);
8644 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8646 if ( CanHaveAttributes() )
8648 m_table
->SetColAttr(attr
, col
);
8657 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8659 if ( CanHaveAttributes() )
8661 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8662 attr
->SetBackgroundColour(colour
);
8667 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8669 if ( CanHaveAttributes() )
8671 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8672 attr
->SetTextColour(colour
);
8677 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8679 if ( CanHaveAttributes() )
8681 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8682 attr
->SetFont(font
);
8687 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8689 if ( CanHaveAttributes() )
8691 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8692 attr
->SetAlignment(horiz
, vert
);
8697 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8699 if ( CanHaveAttributes() )
8701 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8702 attr
->SetOverflow(allow
);
8707 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8709 if ( CanHaveAttributes() )
8711 int cell_rows
, cell_cols
;
8713 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8714 attr
->GetSize(&cell_rows
, &cell_cols
);
8715 attr
->SetSize(num_rows
, num_cols
);
8718 // Cannot set the size of a cell to 0 or negative values
8719 // While it is perfectly legal to do that, this function cannot
8720 // handle all the possibilies, do it by hand by getting the CellAttr.
8721 // You can only set the size of a cell to 1,1 or greater with this fn
8722 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8723 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8724 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8725 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8727 // if this was already a multicell then "turn off" the other cells first
8728 if ((cell_rows
> 1) || (cell_rows
> 1))
8731 for (j
=row
; j
<row
+cell_rows
; j
++)
8733 for (i
=col
; i
<col
+cell_cols
; i
++)
8735 if ((i
!= col
) || (j
!= row
))
8737 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8738 attr_stub
->SetSize( 1, 1 );
8739 attr_stub
->DecRef();
8745 // mark the cells that will be covered by this cell to
8746 // negative or zero values to point back at this cell
8747 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8750 for (j
=row
; j
<row
+num_rows
; j
++)
8752 for (i
=col
; i
<col
+num_cols
; i
++)
8754 if ((i
!= col
) || (j
!= row
))
8756 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8757 attr_stub
->SetSize( row
-j
, col
-i
);
8758 attr_stub
->DecRef();
8766 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8768 if ( CanHaveAttributes() )
8770 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8771 attr
->SetRenderer(renderer
);
8776 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8778 if ( CanHaveAttributes() )
8780 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8781 attr
->SetEditor(editor
);
8786 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8788 if ( CanHaveAttributes() )
8790 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8791 attr
->SetReadOnly(isReadOnly
);
8796 // ----------------------------------------------------------------------------
8797 // Data type registration
8798 // ----------------------------------------------------------------------------
8800 void wxGrid::RegisterDataType(const wxString
& typeName
,
8801 wxGridCellRenderer
* renderer
,
8802 wxGridCellEditor
* editor
)
8804 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8808 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8810 wxString typeName
= m_table
->GetTypeName(row
, col
);
8811 return GetDefaultEditorForType(typeName
);
8814 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8816 wxString typeName
= m_table
->GetTypeName(row
, col
);
8817 return GetDefaultRendererForType(typeName
);
8821 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8823 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8824 if ( index
== wxNOT_FOUND
)
8826 wxFAIL_MSG(wxT("Unknown data type name"));
8831 return m_typeRegistry
->GetEditor(index
);
8835 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8837 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8838 if ( index
== wxNOT_FOUND
)
8840 wxFAIL_MSG(wxT("Unknown data type name"));
8845 return m_typeRegistry
->GetRenderer(index
);
8849 // ----------------------------------------------------------------------------
8851 // ----------------------------------------------------------------------------
8853 void wxGrid::EnableDragRowSize( bool enable
)
8855 m_canDragRowSize
= enable
;
8859 void wxGrid::EnableDragColSize( bool enable
)
8861 m_canDragColSize
= enable
;
8864 void wxGrid::EnableDragGridSize( bool enable
)
8866 m_canDragGridSize
= enable
;
8870 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8872 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8874 if ( resizeExistingRows
)
8876 // since we are resizing all rows to the default row size,
8877 // we can simply clear the row heights and row bottoms
8878 // arrays (which also allows us to take advantage of
8879 // some speed optimisations)
8880 m_rowHeights
.Empty();
8881 m_rowBottoms
.Empty();
8882 if ( !GetBatchCount() )
8887 void wxGrid::SetRowSize( int row
, int height
)
8889 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8891 if ( m_rowHeights
.IsEmpty() )
8893 // need to really create the array
8897 int h
= wxMax( 0, height
);
8898 int diff
= h
- m_rowHeights
[row
];
8900 m_rowHeights
[row
] = h
;
8902 for ( i
= row
; i
< m_numRows
; i
++ )
8904 m_rowBottoms
[i
] += diff
;
8906 if ( !GetBatchCount() )
8910 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8912 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8914 if ( resizeExistingCols
)
8916 // since we are resizing all columns to the default column size,
8917 // we can simply clear the col widths and col rights
8918 // arrays (which also allows us to take advantage of
8919 // some speed optimisations)
8920 m_colWidths
.Empty();
8921 m_colRights
.Empty();
8922 if ( !GetBatchCount() )
8927 void wxGrid::SetColSize( int col
, int width
)
8929 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8931 // should we check that it's bigger than GetColMinimalWidth(col) here?
8933 if ( m_colWidths
.IsEmpty() )
8935 // need to really create the array
8939 int w
= wxMax( 0, width
);
8940 int diff
= w
- m_colWidths
[col
];
8941 m_colWidths
[col
] = w
;
8944 for ( i
= col
; i
< m_numCols
; i
++ )
8946 m_colRights
[i
] += diff
;
8948 if ( !GetBatchCount() )
8953 void wxGrid::SetColMinimalWidth( int col
, int width
)
8955 m_colMinWidths
.Put(col
, width
);
8958 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8960 m_rowMinHeights
.Put(row
, width
);
8963 int wxGrid::GetColMinimalWidth(int col
) const
8965 long value
= m_colMinWidths
.Get(col
);
8966 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8969 int wxGrid::GetRowMinimalHeight(int row
) const
8971 long value
= m_rowMinHeights
.Get(row
);
8972 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8975 // ----------------------------------------------------------------------------
8977 // ----------------------------------------------------------------------------
8979 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8981 wxClientDC
dc(m_gridWin
);
8983 // init both of them to avoid compiler warnings, even if weo nly need one
8991 wxCoord extent
, extentMax
= 0;
8992 int max
= column
? m_numRows
: m_numCols
;
8993 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9000 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9001 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9004 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9005 extent
= column
? size
.x
: size
.y
;
9006 if ( extent
> extentMax
)
9017 // now also compare with the column label extent
9019 dc
.SetFont( GetLabelFont() );
9022 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9024 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9026 extent
= column
? w
: h
;
9027 if ( extent
> extentMax
)
9034 // empty column - give default extent (notice that if extentMax is less
9035 // than default extent but != 0, it's ok)
9036 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9042 // leave some space around text
9053 SetColSize(col
, extentMax
);
9054 if ( !GetBatchCount() )
9057 m_gridWin
->GetClientSize( &cw
, &ch
);
9058 wxRect
rect ( CellToRect( 0, col
) );
9060 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9061 rect
.width
= cw
- rect
.x
;
9062 rect
.height
= m_colLabelHeight
;
9063 m_colLabelWin
->Refresh( TRUE
, &rect
);
9068 SetRowSize(row
, extentMax
);
9069 if ( !GetBatchCount() )
9072 m_gridWin
->GetClientSize( &cw
, &ch
);
9073 wxRect
rect ( CellToRect( row
, 0 ) );
9075 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9076 rect
.width
= m_rowLabelWidth
;
9077 rect
.height
= ch
- rect
.y
;
9078 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9084 SetColMinimalWidth(col
, extentMax
);
9086 SetRowMinimalHeight(row
, extentMax
);
9090 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9092 int width
= m_rowLabelWidth
;
9097 for ( int col
= 0; col
< m_numCols
; col
++ )
9101 AutoSizeColumn(col
, setAsMin
);
9104 width
+= GetColWidth(col
);
9113 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9115 int height
= m_colLabelHeight
;
9120 for ( int row
= 0; row
< m_numRows
; row
++ )
9124 AutoSizeRow(row
, setAsMin
);
9127 height
+= GetRowHeight(row
);
9136 void wxGrid::AutoSize()
9140 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9142 // round up the size to a multiple of scroll step - this ensures that we
9143 // won't get the scrollbars if we're sized exactly to this width
9144 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
9145 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
9147 // distribute the extra space between teh columns/rows to avoid having
9148 // extra white space
9149 wxCoord diff
= sizeFit
.x
- size
.x
;
9152 // try to resize the columns uniformly
9153 wxCoord diffPerCol
= diff
/ m_numCols
;
9156 for ( int col
= 0; col
< m_numCols
; col
++ )
9158 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9162 // add remaining amount to the last columns
9163 diff
-= diffPerCol
* m_numCols
;
9166 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9168 SetColSize(col
, GetColWidth(col
) + 1);
9174 diff
= sizeFit
.y
- size
.y
;
9177 // try to resize the columns uniformly
9178 wxCoord diffPerRow
= diff
/ m_numRows
;
9181 for ( int row
= 0; row
< m_numRows
; row
++ )
9183 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9187 // add remaining amount to the last rows
9188 diff
-= diffPerRow
* m_numRows
;
9191 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9193 SetRowSize(row
, GetRowHeight(row
) + 1);
9200 SetClientSize(sizeFit
);
9203 wxSize
wxGrid::DoGetBestSize() const
9205 // don't set sizes, only calculate them
9206 wxGrid
*self
= (wxGrid
*)this; // const_cast
9209 width
= self
->SetOrCalcColumnSizes(TRUE
);
9210 height
= self
->SetOrCalcRowSizes(TRUE
);
9212 int maxwidth
, maxheight
;
9213 wxDisplaySize( & maxwidth
, & maxheight
);
9215 if ( width
> maxwidth
) width
= maxwidth
;
9216 if ( height
> maxheight
) height
= maxheight
;
9218 return wxSize( width
, height
);
9227 wxPen
& wxGrid::GetDividerPen() const
9232 // ----------------------------------------------------------------------------
9233 // cell value accessor functions
9234 // ----------------------------------------------------------------------------
9236 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9240 m_table
->SetValue( row
, col
, s
);
9241 if ( !GetBatchCount() )
9244 wxRect
rect( CellToRect( row
, col
) );
9246 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9247 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9248 m_gridWin
->Refresh( FALSE
, &rect
);
9251 if ( m_currentCellCoords
.GetRow() == row
&&
9252 m_currentCellCoords
.GetCol() == col
&&
9253 IsCellEditControlShown())
9254 // Note: If we are using IsCellEditControlEnabled,
9255 // this interacts badly with calling SetCellValue from
9256 // an EVT_GRID_CELL_CHANGE handler.
9258 HideCellEditControl();
9259 ShowCellEditControl(); // will reread data from table
9266 // ------ Block, row and col selection
9269 void wxGrid::SelectRow( int row
, bool addToSelected
)
9271 if ( IsSelection() && !addToSelected
)
9275 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9279 void wxGrid::SelectCol( int col
, bool addToSelected
)
9281 if ( IsSelection() && !addToSelected
)
9285 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9289 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9290 bool addToSelected
)
9292 if ( IsSelection() && !addToSelected
)
9296 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9297 FALSE
, addToSelected
);
9301 void wxGrid::SelectAll()
9303 if ( m_numRows
> 0 && m_numCols
> 0 )
9306 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9311 // ------ Cell, row and col deselection
9314 void wxGrid::DeselectRow( int row
)
9319 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9321 if ( m_selection
->IsInSelection(row
, 0 ) )
9322 m_selection
->ToggleCellSelection( row
, 0);
9326 int nCols
= GetNumberCols();
9327 for ( int i
= 0; i
< nCols
; i
++ )
9329 if ( m_selection
->IsInSelection(row
, i
) )
9330 m_selection
->ToggleCellSelection( row
, i
);
9335 void wxGrid::DeselectCol( int col
)
9340 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9342 if ( m_selection
->IsInSelection(0, col
) )
9343 m_selection
->ToggleCellSelection( 0, col
);
9347 int nRows
= GetNumberRows();
9348 for ( int i
= 0; i
< nRows
; i
++ )
9350 if ( m_selection
->IsInSelection(i
, col
) )
9351 m_selection
->ToggleCellSelection(i
, col
);
9356 void wxGrid::DeselectCell( int row
, int col
)
9358 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9359 m_selection
->ToggleCellSelection(row
, col
);
9362 bool wxGrid::IsSelection()
9364 return ( m_selection
&& (m_selection
->IsSelection() ||
9365 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9366 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9369 bool wxGrid::IsInSelection( int row
, int col
) const
9371 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9372 ( row
>= m_selectingTopLeft
.GetRow() &&
9373 col
>= m_selectingTopLeft
.GetCol() &&
9374 row
<= m_selectingBottomRight
.GetRow() &&
9375 col
<= m_selectingBottomRight
.GetCol() )) );
9378 void wxGrid::ClearSelection()
9380 m_selectingTopLeft
= wxGridNoCellCoords
;
9381 m_selectingBottomRight
= wxGridNoCellCoords
;
9383 m_selection
->ClearSelection();
9387 // This function returns the rectangle that encloses the given block
9388 // in device coords clipped to the client size of the grid window.
9390 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9391 const wxGridCellCoords
&bottomRight
)
9393 wxRect
rect( wxGridNoCellRect
);
9396 cellRect
= CellToRect( topLeft
);
9397 if ( cellRect
!= wxGridNoCellRect
)
9403 rect
= wxRect( 0, 0, 0, 0 );
9406 cellRect
= CellToRect( bottomRight
);
9407 if ( cellRect
!= wxGridNoCellRect
)
9413 return wxGridNoCellRect
;
9417 int left
= rect
.GetLeft();
9418 int top
= rect
.GetTop();
9419 int right
= rect
.GetRight();
9420 int bottom
= rect
.GetBottom();
9422 int leftCol
= topLeft
.GetCol();
9423 int topRow
= topLeft
.GetRow();
9424 int rightCol
= bottomRight
.GetCol();
9425 int bottomRow
= bottomRight
.GetRow();
9448 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9450 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9452 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9454 cellRect
= CellToRect( j
, i
);
9456 if (cellRect
.x
< left
)
9458 if (cellRect
.y
< top
)
9460 if (cellRect
.x
+ cellRect
.width
> right
)
9461 right
= cellRect
.x
+ cellRect
.width
;
9462 if (cellRect
.y
+ cellRect
.height
> bottom
)
9463 bottom
= cellRect
.y
+ cellRect
.height
;
9465 else i
= rightCol
; // jump over inner cells.
9469 // convert to scrolled coords
9471 CalcScrolledPosition( left
, top
, &left
, &top
);
9472 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9475 m_gridWin
->GetClientSize( &cw
, &ch
);
9477 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9478 return wxRect( 0, 0, 0, 0);
9480 rect
.SetLeft( wxMax(0, left
) );
9481 rect
.SetTop( wxMax(0, top
) );
9482 rect
.SetRight( wxMin(cw
, right
) );
9483 rect
.SetBottom( wxMin(ch
, bottom
) );
9491 // ------ Grid event classes
9494 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9496 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9497 int row
, int col
, int x
, int y
, bool sel
,
9498 bool control
, bool shift
, bool alt
, bool meta
)
9499 : wxNotifyEvent( type
, id
)
9506 m_control
= control
;
9511 SetEventObject(obj
);
9515 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9517 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9518 int rowOrCol
, int x
, int y
,
9519 bool control
, bool shift
, bool alt
, bool meta
)
9520 : wxNotifyEvent( type
, id
)
9522 m_rowOrCol
= rowOrCol
;
9525 m_control
= control
;
9530 SetEventObject(obj
);
9534 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9536 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9537 const wxGridCellCoords
& topLeft
,
9538 const wxGridCellCoords
& bottomRight
,
9539 bool sel
, bool control
,
9540 bool shift
, bool alt
, bool meta
)
9541 : wxNotifyEvent( type
, id
)
9543 m_topLeft
= topLeft
;
9544 m_bottomRight
= bottomRight
;
9546 m_control
= control
;
9551 SetEventObject(obj
);
9555 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9557 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9558 wxObject
* obj
, int row
,
9559 int col
, wxControl
* ctrl
)
9560 : wxCommandEvent(type
, id
)
9562 SetEventObject(obj
);
9569 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9571 #endif // wxUSE_GRID