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
) == '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
)
3077 wxFAIL_MSG( wxString::Format
3079 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3081 (unsigned long)numRows
,
3082 (unsigned long)curNumRows
3088 if ( numRows
> curNumRows
- pos
)
3090 numRows
= curNumRows
- pos
;
3093 if ( numRows
>= curNumRows
)
3095 m_data
.Clear(); // don't release memory just yet
3099 m_data
.RemoveAt( pos
, numRows
);
3103 wxGridTableMessage
msg( this,
3104 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3108 GetView()->ProcessTableMessage( msg
);
3114 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3118 size_t curNumRows
= m_data
.GetCount();
3119 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3120 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3122 if ( pos
>= curNumCols
)
3124 return AppendCols( numCols
);
3127 for ( row
= 0; row
< curNumRows
; row
++ )
3129 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3131 m_data
[row
].Insert( wxEmptyString
, col
);
3136 wxGridTableMessage
msg( this,
3137 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3141 GetView()->ProcessTableMessage( msg
);
3147 bool wxGridStringTable::AppendCols( size_t numCols
)
3151 size_t curNumRows
= m_data
.GetCount();
3155 // TODO: something better than this ?
3157 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3162 for ( row
= 0; row
< curNumRows
; row
++ )
3164 m_data
[row
].Add( wxEmptyString
, numCols
);
3169 wxGridTableMessage
msg( this,
3170 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3173 GetView()->ProcessTableMessage( msg
);
3179 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3183 size_t curNumRows
= m_data
.GetCount();
3184 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3185 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3187 if ( pos
>= curNumCols
)
3189 wxFAIL_MSG( wxString::Format
3191 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3193 (unsigned long)numCols
,
3194 (unsigned long)curNumCols
3199 if ( numCols
> curNumCols
- pos
)
3201 numCols
= curNumCols
- pos
;
3204 for ( row
= 0; row
< curNumRows
; row
++ )
3206 if ( numCols
>= curNumCols
)
3208 m_data
[row
].Clear();
3212 m_data
[row
].RemoveAt( pos
, numCols
);
3217 wxGridTableMessage
msg( this,
3218 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3222 GetView()->ProcessTableMessage( msg
);
3228 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3230 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3232 // using default label
3234 return wxGridTableBase::GetRowLabelValue( row
);
3238 return m_rowLabels
[ row
];
3242 wxString
wxGridStringTable::GetColLabelValue( int col
)
3244 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3246 // using default label
3248 return wxGridTableBase::GetColLabelValue( col
);
3252 return m_colLabels
[ col
];
3256 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3258 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3260 int n
= m_rowLabels
.GetCount();
3262 for ( i
= n
; i
<= row
; i
++ )
3264 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3268 m_rowLabels
[row
] = value
;
3271 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3273 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3275 int n
= m_colLabels
.GetCount();
3277 for ( i
= n
; i
<= col
; i
++ )
3279 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3283 m_colLabels
[col
] = value
;
3288 //////////////////////////////////////////////////////////////////////
3289 //////////////////////////////////////////////////////////////////////
3291 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3293 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3294 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3295 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3296 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3297 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3298 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3301 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3303 const wxPoint
&pos
, const wxSize
&size
)
3304 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3309 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3313 // NO - don't do this because it will set both the x and y origin
3314 // coords to match the parent scrolled window and we just want to
3315 // set the y coord - MB
3317 // m_owner->PrepareDC( dc );
3320 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3321 dc
.SetDeviceOrigin( 0, -y
);
3323 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3324 m_owner
->DrawRowLabels( dc
, rows
);
3328 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3330 m_owner
->ProcessRowLabelMouseEvent( event
);
3334 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3336 m_owner
->GetEventHandler()->ProcessEvent(event
);
3340 // This seems to be required for wxMotif otherwise the mouse
3341 // cursor must be in the cell edit control to get key events
3343 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3345 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3348 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3350 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3355 //////////////////////////////////////////////////////////////////////
3357 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3359 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3360 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3361 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3362 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3363 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3364 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3367 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3369 const wxPoint
&pos
, const wxSize
&size
)
3370 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3375 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3379 // NO - don't do this because it will set both the x and y origin
3380 // coords to match the parent scrolled window and we just want to
3381 // set the x coord - MB
3383 // m_owner->PrepareDC( dc );
3386 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3387 dc
.SetDeviceOrigin( -x
, 0 );
3389 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3390 m_owner
->DrawColLabels( dc
, cols
);
3394 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3396 m_owner
->ProcessColLabelMouseEvent( event
);
3399 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3401 m_owner
->GetEventHandler()->ProcessEvent(event
);
3405 // This seems to be required for wxMotif otherwise the mouse
3406 // cursor must be in the cell edit control to get key events
3408 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3410 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3413 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3415 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3420 //////////////////////////////////////////////////////////////////////
3422 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3424 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3425 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3426 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3427 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3428 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3429 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3432 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3434 const wxPoint
&pos
, const wxSize
&size
)
3435 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3440 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3444 int client_height
= 0;
3445 int client_width
= 0;
3446 GetClientSize( &client_width
, &client_height
);
3448 dc
.SetPen( *wxBLACK_PEN
);
3449 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3450 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3452 dc
.SetPen( *wxWHITE_PEN
);
3453 dc
.DrawLine( 0, 0, client_width
, 0 );
3454 dc
.DrawLine( 0, 0, 0, client_height
);
3458 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3460 m_owner
->ProcessCornerLabelMouseEvent( event
);
3464 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3466 m_owner
->GetEventHandler()->ProcessEvent(event
);
3469 // This seems to be required for wxMotif otherwise the mouse
3470 // cursor must be in the cell edit control to get key events
3472 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3474 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3477 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3479 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3484 //////////////////////////////////////////////////////////////////////
3486 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3488 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3489 EVT_PAINT( wxGridWindow::OnPaint
)
3490 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3491 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3492 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3493 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3494 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3497 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3498 wxGridRowLabelWindow
*rowLblWin
,
3499 wxGridColLabelWindow
*colLblWin
,
3500 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3501 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, wxT("grid window") )
3504 m_rowLabelWin
= rowLblWin
;
3505 m_colLabelWin
= colLblWin
;
3506 SetBackgroundColour(_T("WHITE"));
3510 wxGridWindow::~wxGridWindow()
3515 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3517 wxPaintDC
dc( this );
3518 m_owner
->PrepareDC( dc
);
3519 wxRegion reg
= GetUpdateRegion();
3520 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3521 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3522 #if WXGRID_DRAW_LINES
3523 m_owner
->DrawAllGridLines( dc
, reg
);
3525 m_owner
->DrawGridSpace( dc
);
3526 m_owner
->DrawHighlight( dc
, DirtyCells
);
3530 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3532 wxWindow::ScrollWindow( dx
, dy
, rect
);
3533 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3534 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3538 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3540 m_owner
->ProcessGridCellMouseEvent( event
);
3543 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3545 m_owner
->GetEventHandler()->ProcessEvent(event
);
3548 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3549 // cursor must be in the cell edit control to get key events
3551 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3553 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3556 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3558 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3561 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3566 //////////////////////////////////////////////////////////////////////
3568 // Internal Helper function for computing row or column from some
3569 // (unscrolled) coordinate value, using either
3570 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3571 // of m_rowBottoms/m_ColRights to speed up the search!
3573 // Internal helper macros for simpler use of that function
3575 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3576 const wxArrayInt
& BorderArray
, int nMax
,
3577 bool maxOnOverflow
);
3579 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3580 WXGRID_MIN_COL_WIDTH, \
3581 m_colRights, m_numCols, TRUE)
3582 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3583 WXGRID_MIN_ROW_HEIGHT, \
3584 m_rowBottoms, m_numRows, TRUE)
3585 /////////////////////////////////////////////////////////////////////
3587 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3589 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3590 EVT_PAINT( wxGrid::OnPaint
)
3591 EVT_SIZE( wxGrid::OnSize
)
3592 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3593 EVT_KEY_UP( wxGrid::OnKeyUp
)
3594 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3597 wxGrid::wxGrid( wxWindow
*parent
,
3602 const wxString
& name
)
3603 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3604 m_colMinWidths(GRID_HASH_SIZE
),
3605 m_rowMinHeights(GRID_HASH_SIZE
)
3613 // Must do this or ~wxScrollHelper will pop the wrong event handler
3614 SetTargetWindow(this);
3616 wxSafeDecRef(m_defaultCellAttr
);
3618 #ifdef DEBUG_ATTR_CACHE
3619 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3620 wxPrintf(_T("wxGrid attribute cache statistics: "
3621 "total: %u, hits: %u (%u%%)\n"),
3622 total
, gs_nAttrCacheHits
,
3623 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3629 delete m_typeRegistry
;
3635 // ----- internal init and update functions
3638 void wxGrid::Create()
3640 m_created
= FALSE
; // set to TRUE by CreateGrid
3642 m_table
= (wxGridTableBase
*) NULL
;
3645 m_cellEditCtrlEnabled
= FALSE
;
3647 m_defaultCellAttr
= new wxGridCellAttr();
3649 // Set default cell attributes
3650 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3651 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3652 m_defaultCellAttr
->SetFont(GetFont());
3653 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3654 m_defaultCellAttr
->SetTextColour(
3655 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3656 m_defaultCellAttr
->SetBackgroundColour(
3657 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3658 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3659 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3664 m_currentCellCoords
= wxGridNoCellCoords
;
3666 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3667 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3669 // create the type registry
3670 m_typeRegistry
= new wxGridTypeRegistry
;
3673 // subwindow components that make up the wxGrid
3674 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3679 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3684 m_colLabelWin
= new wxGridColLabelWindow( this,
3689 m_gridWin
= new wxGridWindow( this,
3696 SetTargetWindow( m_gridWin
);
3702 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3703 wxGrid::wxGridSelectionModes selmode
)
3705 wxCHECK_MSG( !m_created
,
3707 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3709 m_numRows
= numRows
;
3710 m_numCols
= numCols
;
3712 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3713 m_table
->SetView( this );
3715 m_selection
= new wxGridSelection( this, selmode
);
3724 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3726 wxCHECK_RET( m_created
,
3727 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3729 m_selection
->SetSelectionMode( selmode
);
3732 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3734 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3735 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3737 return m_selection
->GetSelectionMode();
3740 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3741 wxGrid::wxGridSelectionModes selmode
)
3745 // RD: Actually, this should probably be allowed. I think it would be
3746 // nice to be able to switch multiple Tables in and out of a single
3747 // View at runtime. Is there anything in the implementation that
3748 // would prevent this?
3750 // At least, you now have to cope with m_selection
3751 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3756 m_numRows
= table
->GetNumberRows();
3757 m_numCols
= table
->GetNumberCols();
3760 m_table
->SetView( this );
3763 m_selection
= new wxGridSelection( this, selmode
);
3776 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3777 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3779 if ( m_rowLabelWin
)
3781 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3785 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3788 m_labelTextColour
= wxColour( _T("BLACK") );
3791 m_attrCache
.row
= -1;
3793 // TODO: something better than this ?
3795 m_labelFont
= this->GetFont();
3796 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3798 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3799 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3801 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3802 m_colLabelVertAlign
= wxALIGN_TOP
;
3804 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3805 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3807 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3808 m_defaultRowHeight
+= 8;
3810 m_defaultRowHeight
+= 4;
3813 m_gridLineColour
= wxColour( 128, 128, 255 );
3814 m_gridLinesEnabled
= TRUE
;
3815 m_cellHighlightColour
= m_gridLineColour
;
3816 m_cellHighlightPenWidth
= 2;
3817 m_cellHighlightROPenWidth
= 1;
3819 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3820 m_winCapture
= (wxWindow
*)NULL
;
3821 m_canDragRowSize
= TRUE
;
3822 m_canDragColSize
= TRUE
;
3823 m_canDragGridSize
= TRUE
;
3825 m_dragRowOrCol
= -1;
3826 m_isDragging
= FALSE
;
3827 m_startDragPos
= wxDefaultPosition
;
3829 m_waitForSlowClick
= FALSE
;
3831 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3832 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3834 m_currentCellCoords
= wxGridNoCellCoords
;
3836 m_selectingTopLeft
= wxGridNoCellCoords
;
3837 m_selectingBottomRight
= wxGridNoCellCoords
;
3838 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3839 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3841 m_editable
= TRUE
; // default for whole grid
3843 m_inOnKeyDown
= FALSE
;
3850 // ----------------------------------------------------------------------------
3851 // the idea is to call these functions only when necessary because they create
3852 // quite big arrays which eat memory mostly unnecessary - in particular, if
3853 // default widths/heights are used for all rows/columns, we may not use these
3856 // with some extra code, it should be possible to only store the
3857 // widths/heights different from default ones but this will be done later...
3858 // ----------------------------------------------------------------------------
3860 void wxGrid::InitRowHeights()
3862 m_rowHeights
.Empty();
3863 m_rowBottoms
.Empty();
3865 m_rowHeights
.Alloc( m_numRows
);
3866 m_rowBottoms
.Alloc( m_numRows
);
3870 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
3872 for ( int i
= 0; i
< m_numRows
; i
++ )
3874 rowBottom
+= m_defaultRowHeight
;
3875 m_rowBottoms
.Add( rowBottom
);
3879 void wxGrid::InitColWidths()
3881 m_colWidths
.Empty();
3882 m_colRights
.Empty();
3884 m_colWidths
.Alloc( m_numCols
);
3885 m_colRights
.Alloc( m_numCols
);
3888 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
3890 for ( int i
= 0; i
< m_numCols
; i
++ )
3892 colRight
+= m_defaultColWidth
;
3893 m_colRights
.Add( colRight
);
3897 int wxGrid::GetColWidth(int col
) const
3899 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3902 int wxGrid::GetColLeft(int col
) const
3904 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3905 : m_colRights
[col
] - m_colWidths
[col
];
3908 int wxGrid::GetColRight(int col
) const
3910 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3914 int wxGrid::GetRowHeight(int row
) const
3916 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3919 int wxGrid::GetRowTop(int row
) const
3921 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3922 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3925 int wxGrid::GetRowBottom(int row
) const
3927 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3928 : m_rowBottoms
[row
];
3931 void wxGrid::CalcDimensions()
3934 GetClientSize( &cw
, &ch
);
3936 if ( m_rowLabelWin
->IsShown() )
3937 cw
-= m_rowLabelWidth
;
3938 if ( m_colLabelWin
->IsShown() )
3939 ch
-= m_colLabelHeight
;
3942 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3943 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3945 // preserve (more or less) the previous position
3947 GetViewStart( &x
, &y
);
3949 // maybe we don't need scrollbars at all?
3951 // also adjust the position to be valid for the new scroll rangs
3972 // do set scrollbar parameters
3973 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3974 GetScrollX(w
), GetScrollY(h
), x
, y
,
3975 GetBatchCount() != 0);
3977 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3978 // still must reposition the children
3983 void wxGrid::CalcWindowSizes()
3986 GetClientSize( &cw
, &ch
);
3988 if ( m_cornerLabelWin
->IsShown() )
3989 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3991 if ( m_colLabelWin
->IsShown() )
3992 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3994 if ( m_rowLabelWin
->IsShown() )
3995 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3997 if ( m_gridWin
->IsShown() )
3998 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4002 // this is called when the grid table sends a message to say that it
4003 // has been redimensioned
4005 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4008 bool result
= FALSE
;
4010 // Clear the attribute cache as the attribute might refer to a different
4011 // cell than stored in the cache after adding/removing rows/columns.
4013 // By the same reasoning, the editor should be dismissed if columns are
4014 // added or removed. And for consistency, it should IMHO always be
4015 // removed, not only if the cell "underneath" it actually changes.
4016 // For now, I intentionally do not save the editor's content as the
4017 // cell it might want to save that stuff to might no longer exist.
4018 HideCellEditControl();
4020 // if we were using the default widths/heights so far, we must change them
4022 if ( m_colWidths
.IsEmpty() )
4027 if ( m_rowHeights
.IsEmpty() )
4033 switch ( msg
.GetId() )
4035 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4037 size_t pos
= msg
.GetCommandInt();
4038 int numRows
= msg
.GetCommandInt2();
4040 m_numRows
+= numRows
;
4042 if ( !m_rowHeights
.IsEmpty() )
4044 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4045 m_rowBottoms
.Insert( 0, pos
, numRows
);
4048 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4050 for ( i
= pos
; i
< m_numRows
; i
++ )
4052 bottom
+= m_rowHeights
[i
];
4053 m_rowBottoms
[i
] = bottom
;
4056 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4058 // if we have just inserted cols into an empty grid the current
4059 // cell will be undefined...
4061 SetCurrentCell( 0, 0 );
4065 m_selection
->UpdateRows( pos
, numRows
);
4066 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4068 attrProvider
->UpdateAttrRows( pos
, numRows
);
4070 if ( !GetBatchCount() )
4073 m_rowLabelWin
->Refresh();
4079 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4081 int numRows
= msg
.GetCommandInt();
4082 int oldNumRows
= m_numRows
;
4083 m_numRows
+= numRows
;
4085 if ( !m_rowHeights
.IsEmpty() )
4087 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4088 m_rowBottoms
.Add( 0, numRows
);
4091 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4093 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4095 bottom
+= m_rowHeights
[i
];
4096 m_rowBottoms
[i
] = bottom
;
4099 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4101 // if we have just inserted cols into an empty grid the current
4102 // cell will be undefined...
4104 SetCurrentCell( 0, 0 );
4106 if ( !GetBatchCount() )
4109 m_rowLabelWin
->Refresh();
4115 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4117 size_t pos
= msg
.GetCommandInt();
4118 int numRows
= msg
.GetCommandInt2();
4119 m_numRows
-= numRows
;
4121 if ( !m_rowHeights
.IsEmpty() )
4123 m_rowHeights
.RemoveAt( pos
, numRows
);
4124 m_rowBottoms
.RemoveAt( pos
, numRows
);
4127 for ( i
= 0; i
< m_numRows
; i
++ )
4129 h
+= m_rowHeights
[i
];
4130 m_rowBottoms
[i
] = h
;
4135 m_currentCellCoords
= wxGridNoCellCoords
;
4139 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4140 m_currentCellCoords
.Set( 0, 0 );
4144 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4145 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4147 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4148 // ifdef'd out following patch from Paul Gammans
4150 // No need to touch column attributes, unless we
4151 // removed _all_ rows, in this case, we remove
4152 // all column attributes.
4153 // I hate to do this here, but the
4154 // needed data is not available inside UpdateAttrRows.
4155 if ( !GetNumberRows() )
4156 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4159 if ( !GetBatchCount() )
4162 m_rowLabelWin
->Refresh();
4168 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4170 size_t pos
= msg
.GetCommandInt();
4171 int numCols
= msg
.GetCommandInt2();
4172 m_numCols
+= numCols
;
4174 if ( !m_colWidths
.IsEmpty() )
4176 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4177 m_colRights
.Insert( 0, pos
, numCols
);
4180 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4182 for ( i
= pos
; i
< m_numCols
; i
++ )
4184 right
+= m_colWidths
[i
];
4185 m_colRights
[i
] = right
;
4188 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4190 // if we have just inserted cols into an empty grid the current
4191 // cell will be undefined...
4193 SetCurrentCell( 0, 0 );
4197 m_selection
->UpdateCols( pos
, numCols
);
4198 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4200 attrProvider
->UpdateAttrCols( pos
, numCols
);
4201 if ( !GetBatchCount() )
4204 m_colLabelWin
->Refresh();
4211 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4213 int numCols
= msg
.GetCommandInt();
4214 int oldNumCols
= m_numCols
;
4215 m_numCols
+= numCols
;
4216 if ( !m_colWidths
.IsEmpty() )
4218 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4219 m_colRights
.Add( 0, numCols
);
4222 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4224 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4226 right
+= m_colWidths
[i
];
4227 m_colRights
[i
] = right
;
4230 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4232 // if we have just inserted cols into an empty grid the current
4233 // cell will be undefined...
4235 SetCurrentCell( 0, 0 );
4237 if ( !GetBatchCount() )
4240 m_colLabelWin
->Refresh();
4246 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4248 size_t pos
= msg
.GetCommandInt();
4249 int numCols
= msg
.GetCommandInt2();
4250 m_numCols
-= numCols
;
4252 if ( !m_colWidths
.IsEmpty() )
4254 m_colWidths
.RemoveAt( pos
, numCols
);
4255 m_colRights
.RemoveAt( pos
, numCols
);
4258 for ( i
= 0; i
< m_numCols
; i
++ )
4260 w
+= m_colWidths
[i
];
4266 m_currentCellCoords
= wxGridNoCellCoords
;
4270 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4271 m_currentCellCoords
.Set( 0, 0 );
4275 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4276 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4278 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4279 // ifdef'd out following patch from Paul Gammans
4281 // No need to touch row attributes, unless we
4282 // removed _all_ columns, in this case, we remove
4283 // all row attributes.
4284 // I hate to do this here, but the
4285 // needed data is not available inside UpdateAttrCols.
4286 if ( !GetNumberCols() )
4287 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4290 if ( !GetBatchCount() )
4293 m_colLabelWin
->Refresh();
4300 if (result
&& !GetBatchCount() )
4301 m_gridWin
->Refresh();
4306 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4308 wxRegionIterator
iter( reg
);
4311 wxArrayInt rowlabels
;
4318 // TODO: remove this when we can...
4319 // There is a bug in wxMotif that gives garbage update
4320 // rectangles if you jump-scroll a long way by clicking the
4321 // scrollbar with middle button. This is a work-around
4323 #if defined(__WXMOTIF__)
4325 m_gridWin
->GetClientSize( &cw
, &ch
);
4326 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4327 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4330 // logical bounds of update region
4333 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4334 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4336 // find the row labels within these bounds
4339 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4341 if ( GetRowBottom(row
) < top
)
4344 if ( GetRowTop(row
) > bottom
)
4347 rowlabels
.Add( row
);
4357 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4359 wxRegionIterator
iter( reg
);
4362 wxArrayInt colLabels
;
4369 // TODO: remove this when we can...
4370 // There is a bug in wxMotif that gives garbage update
4371 // rectangles if you jump-scroll a long way by clicking the
4372 // scrollbar with middle button. This is a work-around
4374 #if defined(__WXMOTIF__)
4376 m_gridWin
->GetClientSize( &cw
, &ch
);
4377 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4378 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4381 // logical bounds of update region
4384 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4385 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4387 // find the cells within these bounds
4390 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4392 if ( GetColRight(col
) < left
)
4395 if ( GetColLeft(col
) > right
)
4398 colLabels
.Add( col
);
4407 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4409 wxRegionIterator
iter( reg
);
4412 wxGridCellCoordsArray cellsExposed
;
4414 int left
, top
, right
, bottom
;
4419 // TODO: remove this when we can...
4420 // There is a bug in wxMotif that gives garbage update
4421 // rectangles if you jump-scroll a long way by clicking the
4422 // scrollbar with middle button. This is a work-around
4424 #if defined(__WXMOTIF__)
4426 m_gridWin
->GetClientSize( &cw
, &ch
);
4427 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4428 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4429 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4430 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4433 // logical bounds of update region
4435 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4436 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4438 // find the cells within these bounds
4441 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4443 if ( GetRowBottom(row
) <= top
)
4446 if ( GetRowTop(row
) > bottom
)
4449 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4451 if ( GetColRight(col
) <= left
)
4454 if ( GetColLeft(col
) > right
)
4457 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4464 return cellsExposed
;
4468 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4471 wxPoint
pos( event
.GetPosition() );
4472 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4474 if ( event
.Dragging() )
4478 m_isDragging
= TRUE
;
4479 m_rowLabelWin
->CaptureMouse();
4482 if ( event
.LeftIsDown() )
4484 switch( m_cursorMode
)
4486 case WXGRID_CURSOR_RESIZE_ROW
:
4488 int cw
, ch
, left
, dummy
;
4489 m_gridWin
->GetClientSize( &cw
, &ch
);
4490 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4492 wxClientDC
dc( m_gridWin
);
4495 GetRowTop(m_dragRowOrCol
) +
4496 GetRowMinimalHeight(m_dragRowOrCol
) );
4497 dc
.SetLogicalFunction(wxINVERT
);
4498 if ( m_dragLastPos
>= 0 )
4500 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4502 dc
.DrawLine( left
, y
, left
+cw
, y
);
4507 case WXGRID_CURSOR_SELECT_ROW
:
4508 if ( (row
= YToRow( y
)) >= 0 )
4512 m_selection
->SelectRow( row
,
4513 event
.ControlDown(),
4520 // default label to suppress warnings about "enumeration value
4521 // 'xxx' not handled in switch
4529 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4534 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4535 m_isDragging
= FALSE
;
4538 // ------------ Entering or leaving the window
4540 if ( event
.Entering() || event
.Leaving() )
4542 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4546 // ------------ Left button pressed
4548 else if ( event
.LeftDown() )
4550 // don't send a label click event for a hit on the
4551 // edge of the row label - this is probably the user
4552 // wanting to resize the row
4554 if ( YToEdgeOfRow(y
) < 0 )
4558 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4560 if ( !event
.ShiftDown() && !event
.ControlDown() )
4564 if ( event
.ShiftDown() )
4566 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4569 GetNumberCols() - 1,
4570 event
.ControlDown(),
4577 m_selection
->SelectRow( row
,
4578 event
.ControlDown(),
4585 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4590 // starting to drag-resize a row
4592 if ( CanDragRowSize() )
4593 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4598 // ------------ Left double click
4600 else if (event
.LeftDClick() )
4602 if ( YToEdgeOfRow(y
) < 0 )
4605 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4610 // ------------ Left button released
4612 else if ( event
.LeftUp() )
4614 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4616 DoEndDragResizeRow();
4618 // Note: we are ending the event *after* doing
4619 // default processing in this case
4621 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4624 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4629 // ------------ Right button down
4631 else if ( event
.RightDown() )
4634 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4636 // no default action at the moment
4641 // ------------ Right double click
4643 else if ( event
.RightDClick() )
4646 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4648 // no default action at the moment
4653 // ------------ No buttons down and mouse moving
4655 else if ( event
.Moving() )
4657 m_dragRowOrCol
= YToEdgeOfRow( y
);
4658 if ( m_dragRowOrCol
>= 0 )
4660 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4662 // don't capture the mouse yet
4663 if ( CanDragRowSize() )
4664 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4667 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4669 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4675 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4678 wxPoint
pos( event
.GetPosition() );
4679 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4681 if ( event
.Dragging() )
4685 m_isDragging
= TRUE
;
4686 m_colLabelWin
->CaptureMouse();
4689 if ( event
.LeftIsDown() )
4691 switch( m_cursorMode
)
4693 case WXGRID_CURSOR_RESIZE_COL
:
4695 int cw
, ch
, dummy
, top
;
4696 m_gridWin
->GetClientSize( &cw
, &ch
);
4697 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4699 wxClientDC
dc( m_gridWin
);
4702 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4703 GetColMinimalWidth(m_dragRowOrCol
));
4704 dc
.SetLogicalFunction(wxINVERT
);
4705 if ( m_dragLastPos
>= 0 )
4707 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4709 dc
.DrawLine( x
, top
, x
, top
+ch
);
4714 case WXGRID_CURSOR_SELECT_COL
:
4715 if ( (col
= XToCol( x
)) >= 0 )
4719 m_selection
->SelectCol( col
,
4720 event
.ControlDown(),
4727 // default label to suppress warnings about "enumeration value
4728 // 'xxx' not handled in switch
4736 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4741 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4742 m_isDragging
= FALSE
;
4745 // ------------ Entering or leaving the window
4747 if ( event
.Entering() || event
.Leaving() )
4749 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4753 // ------------ Left button pressed
4755 else if ( event
.LeftDown() )
4757 // don't send a label click event for a hit on the
4758 // edge of the col label - this is probably the user
4759 // wanting to resize the col
4761 if ( XToEdgeOfCol(x
) < 0 )
4765 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4767 if ( !event
.ShiftDown() && !event
.ControlDown() )
4771 if ( event
.ShiftDown() )
4773 m_selection
->SelectBlock( 0,
4774 m_currentCellCoords
.GetCol(),
4775 GetNumberRows() - 1, col
,
4776 event
.ControlDown(),
4783 m_selection
->SelectCol( col
,
4784 event
.ControlDown(),
4791 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4796 // starting to drag-resize a col
4798 if ( CanDragColSize() )
4799 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4804 // ------------ Left double click
4806 if ( event
.LeftDClick() )
4808 if ( XToEdgeOfCol(x
) < 0 )
4811 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4816 // ------------ Left button released
4818 else if ( event
.LeftUp() )
4820 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4822 DoEndDragResizeCol();
4824 // Note: we are ending the event *after* doing
4825 // default processing in this case
4827 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4830 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4835 // ------------ Right button down
4837 else if ( event
.RightDown() )
4840 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4842 // no default action at the moment
4847 // ------------ Right double click
4849 else if ( event
.RightDClick() )
4852 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4854 // no default action at the moment
4859 // ------------ No buttons down and mouse moving
4861 else if ( event
.Moving() )
4863 m_dragRowOrCol
= XToEdgeOfCol( x
);
4864 if ( m_dragRowOrCol
>= 0 )
4866 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4868 // don't capture the cursor yet
4869 if ( CanDragColSize() )
4870 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4873 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4875 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4881 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4883 if ( event
.LeftDown() )
4885 // indicate corner label by having both row and
4888 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4894 else if ( event
.LeftDClick() )
4896 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4899 else if ( event
.RightDown() )
4901 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4903 // no default action at the moment
4907 else if ( event
.RightDClick() )
4909 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4911 // no default action at the moment
4916 void wxGrid::ChangeCursorMode(CursorMode mode
,
4921 static const wxChar
*cursorModes
[] =
4930 wxLogTrace(_T("grid"),
4931 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4932 win
== m_colLabelWin
? _T("colLabelWin")
4933 : win
? _T("rowLabelWin")
4935 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4936 #endif // __WXDEBUG__
4938 if ( mode
== m_cursorMode
&&
4939 win
== m_winCapture
&&
4940 captureMouse
== (m_winCapture
!= NULL
))
4945 // by default use the grid itself
4951 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4952 m_winCapture
= (wxWindow
*)NULL
;
4955 m_cursorMode
= mode
;
4957 switch ( m_cursorMode
)
4959 case WXGRID_CURSOR_RESIZE_ROW
:
4960 win
->SetCursor( m_rowResizeCursor
);
4963 case WXGRID_CURSOR_RESIZE_COL
:
4964 win
->SetCursor( m_colResizeCursor
);
4968 win
->SetCursor( *wxSTANDARD_CURSOR
);
4971 // we need to capture mouse when resizing
4972 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4973 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4975 if ( captureMouse
&& resize
)
4977 win
->CaptureMouse();
4982 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4985 wxPoint
pos( event
.GetPosition() );
4986 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4988 wxGridCellCoords coords
;
4989 XYToCell( x
, y
, coords
);
4991 int cell_rows
, cell_cols
;
4992 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4993 if ((cell_rows
< 0) || (cell_cols
< 0))
4995 coords
.SetRow(coords
.GetRow() + cell_rows
);
4996 coords
.SetCol(coords
.GetCol() + cell_cols
);
4999 if ( event
.Dragging() )
5001 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5003 // Don't start doing anything until the mouse has been drug at
5004 // least 3 pixels in any direction...
5007 if (m_startDragPos
== wxDefaultPosition
)
5009 m_startDragPos
= pos
;
5012 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5016 m_isDragging
= TRUE
;
5017 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5019 // Hide the edit control, so it
5020 // won't interfer with drag-shrinking.
5021 if ( IsCellEditControlShown() )
5023 HideCellEditControl();
5024 SaveEditControlValue();
5027 // Have we captured the mouse yet?
5030 m_winCapture
= m_gridWin
;
5031 m_winCapture
->CaptureMouse();
5034 if ( coords
!= wxGridNoCellCoords
)
5036 if ( event
.ControlDown() )
5038 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5039 m_selectingKeyboard
= coords
;
5040 HighlightBlock ( m_selectingKeyboard
, coords
);
5044 if ( !IsSelection() )
5046 HighlightBlock( coords
, coords
);
5050 HighlightBlock( m_currentCellCoords
, coords
);
5054 if (! IsVisible(coords
))
5056 MakeCellVisible(coords
);
5057 // TODO: need to introduce a delay or something here. The
5058 // scrolling is way to fast, at least on MSW - also on GTK.
5062 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5064 int cw
, ch
, left
, dummy
;
5065 m_gridWin
->GetClientSize( &cw
, &ch
);
5066 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5068 wxClientDC
dc( m_gridWin
);
5070 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5071 GetRowMinimalHeight(m_dragRowOrCol
) );
5072 dc
.SetLogicalFunction(wxINVERT
);
5073 if ( m_dragLastPos
>= 0 )
5075 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5077 dc
.DrawLine( left
, y
, left
+cw
, y
);
5080 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5082 int cw
, ch
, dummy
, top
;
5083 m_gridWin
->GetClientSize( &cw
, &ch
);
5084 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5086 wxClientDC
dc( m_gridWin
);
5088 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5089 GetColMinimalWidth(m_dragRowOrCol
) );
5090 dc
.SetLogicalFunction(wxINVERT
);
5091 if ( m_dragLastPos
>= 0 )
5093 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5095 dc
.DrawLine( x
, top
, x
, top
+ch
);
5102 m_isDragging
= FALSE
;
5103 m_startDragPos
= wxDefaultPosition
;
5105 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5106 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5109 if ( event
.Entering() || event
.Leaving() )
5111 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5112 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5117 // ------------ Left button pressed
5119 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5121 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5126 if ( !event
.ControlDown() )
5128 if ( event
.ShiftDown() )
5132 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5133 m_currentCellCoords
.GetCol(),
5136 event
.ControlDown(),
5142 else if ( XToEdgeOfCol(x
) < 0 &&
5143 YToEdgeOfRow(y
) < 0 )
5145 DisableCellEditControl();
5146 MakeCellVisible( coords
);
5148 // if this is the second click on this cell then start
5150 if ( m_waitForSlowClick
&&
5151 (coords
== m_currentCellCoords
) &&
5152 CanEnableCellControl())
5154 EnableCellEditControl();
5156 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5157 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5160 editor
->StartingClick();
5164 m_waitForSlowClick
= FALSE
;
5168 if ( event
.ControlDown() )
5172 m_selection
->ToggleCellSelection( coords
.GetRow(),
5174 event
.ControlDown(),
5179 m_selectingTopLeft
= wxGridNoCellCoords
;
5180 m_selectingBottomRight
= wxGridNoCellCoords
;
5181 m_selectingKeyboard
= coords
;
5185 SetCurrentCell( coords
);
5188 if ( m_selection
->GetSelectionMode() !=
5189 wxGrid::wxGridSelectCells
)
5191 HighlightBlock( coords
, coords
);
5195 m_waitForSlowClick
= TRUE
;
5202 // ------------ Left double click
5204 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5206 DisableCellEditControl();
5208 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5210 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5218 // ------------ Left button released
5220 else if ( event
.LeftUp() )
5222 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5224 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5225 m_selectingBottomRight
!= wxGridNoCellCoords
)
5229 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5230 m_winCapture
= NULL
;
5235 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5236 m_selectingTopLeft
.GetCol(),
5237 m_selectingBottomRight
.GetRow(),
5238 m_selectingBottomRight
.GetCol(),
5239 event
.ControlDown(),
5245 m_selectingTopLeft
= wxGridNoCellCoords
;
5246 m_selectingBottomRight
= wxGridNoCellCoords
;
5249 // Show the edit control, if it has been hidden for
5251 ShowCellEditControl();
5253 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5255 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5256 DoEndDragResizeRow();
5258 // Note: we are ending the event *after* doing
5259 // default processing in this case
5261 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5263 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5265 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5266 DoEndDragResizeCol();
5268 // Note: we are ending the event *after* doing
5269 // default processing in this case
5271 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5278 // ------------ Right button down
5280 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5282 DisableCellEditControl();
5283 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5288 // no default action at the moment
5293 // ------------ Right double click
5295 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5297 DisableCellEditControl();
5298 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5303 // no default action at the moment
5307 // ------------ Moving and no button action
5309 else if ( event
.Moving() && !event
.IsButton() )
5311 int dragRow
= YToEdgeOfRow( y
);
5312 int dragCol
= XToEdgeOfCol( x
);
5314 // Dragging on the corner of a cell to resize in both
5315 // directions is not implemented yet...
5317 if ( dragRow
>= 0 && dragCol
>= 0 )
5319 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5323 if ( dragRow
>= 0 && dragRow
< GetNumberRows())
5325 m_dragRowOrCol
= dragRow
;
5327 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5329 if ( CanDragRowSize() && CanDragGridSize() )
5330 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5335 m_dragRowOrCol
= dragCol
;
5341 if ( dragCol
>= 0 && dragCol
< GetNumberCols())
5343 m_dragRowOrCol
= dragCol
;
5345 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5347 if ( CanDragColSize() && CanDragGridSize() )
5348 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5354 // Neither on a row or col edge
5356 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5358 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5364 void wxGrid::DoEndDragResizeRow()
5366 if ( m_dragLastPos
>= 0 )
5368 // erase the last line and resize the row
5370 int cw
, ch
, left
, dummy
;
5371 m_gridWin
->GetClientSize( &cw
, &ch
);
5372 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5374 wxClientDC
dc( m_gridWin
);
5376 dc
.SetLogicalFunction( wxINVERT
);
5377 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5378 HideCellEditControl();
5379 SaveEditControlValue();
5381 int rowTop
= GetRowTop(m_dragRowOrCol
);
5382 SetRowSize( m_dragRowOrCol
,
5383 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5385 if ( !GetBatchCount() )
5387 // Only needed to get the correct rect.y:
5388 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5390 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5391 rect
.width
= m_rowLabelWidth
;
5392 rect
.height
= ch
- rect
.y
;
5393 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5395 // if there is a multicell block, paint all of it
5398 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5399 int leftCol
= XToCol(left
);
5400 int rightCol
= XToCol(left
+cw
);
5403 if (rightCol
< 0) rightCol
= m_numCols
;
5404 for (i
=leftCol
; i
<rightCol
; i
++)
5406 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5407 if (cell_rows
< subtract_rows
)
5408 subtract_rows
= cell_rows
;
5410 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5411 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5412 rect
.height
= ch
- rect
.y
;
5415 m_gridWin
->Refresh( FALSE
, &rect
);
5418 ShowCellEditControl();
5423 void wxGrid::DoEndDragResizeCol()
5425 if ( m_dragLastPos
>= 0 )
5427 // erase the last line and resize the col
5429 int cw
, ch
, dummy
, top
;
5430 m_gridWin
->GetClientSize( &cw
, &ch
);
5431 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5433 wxClientDC
dc( m_gridWin
);
5435 dc
.SetLogicalFunction( wxINVERT
);
5436 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5437 HideCellEditControl();
5438 SaveEditControlValue();
5440 int colLeft
= GetColLeft(m_dragRowOrCol
);
5441 SetColSize( m_dragRowOrCol
,
5442 wxMax( m_dragLastPos
- colLeft
,
5443 GetColMinimalWidth(m_dragRowOrCol
) ) );
5445 if ( !GetBatchCount() )
5447 // Only needed to get the correct rect.x:
5448 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5450 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5451 rect
.width
= cw
- rect
.x
;
5452 rect
.height
= m_colLabelHeight
;
5453 m_colLabelWin
->Refresh( TRUE
, &rect
);
5455 // if there is a multicell block, paint all of it
5458 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5459 int topRow
= YToRow(top
);
5460 int bottomRow
= YToRow(top
+cw
);
5463 if (bottomRow
< 0) bottomRow
= m_numRows
;
5464 for (i
=topRow
; i
<bottomRow
; i
++)
5466 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5467 if (cell_cols
< subtract_cols
)
5468 subtract_cols
= cell_cols
;
5470 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5471 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5472 rect
.width
= cw
- rect
.x
;
5475 m_gridWin
->Refresh( FALSE
, &rect
);
5478 ShowCellEditControl();
5485 // ------ interaction with data model
5487 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5489 switch ( msg
.GetId() )
5491 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5492 return GetModelValues();
5494 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5495 return SetModelValues();
5497 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5498 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5499 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5500 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5501 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5502 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5503 return Redimension( msg
);
5512 // The behaviour of this function depends on the grid table class
5513 // Clear() function. For the default wxGridStringTable class the
5514 // behavious is to replace all cell contents with wxEmptyString but
5515 // not to change the number of rows or cols.
5517 void wxGrid::ClearGrid()
5521 if (IsCellEditControlEnabled())
5522 DisableCellEditControl();
5525 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5530 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5532 // TODO: something with updateLabels flag
5536 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5542 if (IsCellEditControlEnabled())
5543 DisableCellEditControl();
5545 return m_table
->InsertRows( pos
, numRows
);
5547 // the table will have sent the results of the insert row
5548 // operation to this view object as a grid table message
5554 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5556 // TODO: something with updateLabels flag
5560 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5564 return ( m_table
&& m_table
->AppendRows( numRows
) );
5565 // the table will have sent the results of the append row
5566 // operation to this view object as a grid table message
5570 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5572 // TODO: something with updateLabels flag
5576 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5582 if (IsCellEditControlEnabled())
5583 DisableCellEditControl();
5585 return (m_table
->DeleteRows( pos
, numRows
));
5586 // the table will have sent the results of the delete row
5587 // operation to this view object as a grid table message
5593 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5595 // TODO: something with updateLabels flag
5599 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5605 if (IsCellEditControlEnabled())
5606 DisableCellEditControl();
5608 return m_table
->InsertCols( pos
, numCols
);
5609 // the table will have sent the results of the insert col
5610 // operation to this view object as a grid table message
5616 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5618 // TODO: something with updateLabels flag
5622 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5626 return ( m_table
&& m_table
->AppendCols( numCols
) );
5627 // the table will have sent the results of the append col
5628 // operation to this view object as a grid table message
5632 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5634 // TODO: something with updateLabels flag
5638 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5644 if (IsCellEditControlEnabled())
5645 DisableCellEditControl();
5647 return ( m_table
->DeleteCols( pos
, numCols
) );
5648 // the table will have sent the results of the delete col
5649 // operation to this view object as a grid table message
5657 // ----- event handlers
5660 // Generate a grid event based on a mouse event and
5661 // return the result of ProcessEvent()
5663 int wxGrid::SendEvent( const wxEventType type
,
5665 wxMouseEvent
& mouseEv
)
5670 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5672 int rowOrCol
= (row
== -1 ? col
: row
);
5674 wxGridSizeEvent
gridEvt( GetId(),
5678 mouseEv
.GetX() + GetRowLabelSize(),
5679 mouseEv
.GetY() + GetColLabelSize(),
5680 mouseEv
.ControlDown(),
5681 mouseEv
.ShiftDown(),
5683 mouseEv
.MetaDown() );
5685 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5686 vetoed
= !gridEvt
.IsAllowed();
5688 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5690 // Right now, it should _never_ end up here!
5691 wxGridRangeSelectEvent
gridEvt( GetId(),
5695 m_selectingBottomRight
,
5697 mouseEv
.ControlDown(),
5698 mouseEv
.ShiftDown(),
5700 mouseEv
.MetaDown() );
5702 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5703 vetoed
= !gridEvt
.IsAllowed();
5707 wxGridEvent
gridEvt( GetId(),
5711 mouseEv
.GetX() + GetRowLabelSize(),
5712 mouseEv
.GetY() + GetColLabelSize(),
5714 mouseEv
.ControlDown(),
5715 mouseEv
.ShiftDown(),
5717 mouseEv
.MetaDown() );
5718 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5719 vetoed
= !gridEvt
.IsAllowed();
5722 // A Veto'd event may not be `claimed' so test this first
5723 if (vetoed
) return -1;
5724 return claimed
? 1 : 0;
5728 // Generate a grid event of specified type and return the result
5729 // of ProcessEvent().
5731 int wxGrid::SendEvent( const wxEventType type
,
5737 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5739 int rowOrCol
= (row
== -1 ? col
: row
);
5741 wxGridSizeEvent
gridEvt( GetId(),
5746 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5747 vetoed
= !gridEvt
.IsAllowed();
5751 wxGridEvent
gridEvt( GetId(),
5756 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5757 vetoed
= !gridEvt
.IsAllowed();
5760 // A Veto'd event may not be `claimed' so test this first
5761 if (vetoed
) return -1;
5762 return claimed
? 1 : 0;
5766 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5768 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5771 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5773 // Don't do anything if between Begin/EndBatch...
5774 // EndBatch() will do all this on the last nested one anyway.
5775 if (! GetBatchCount())
5777 // Refresh to get correct scrolled position:
5778 wxScrolledWindow::Refresh(eraseb
,rect
);
5782 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5783 int width_label
, width_cell
, height_label
, height_cell
;
5786 //Copy rectangle can get scroll offsets..
5787 rect_x
= rect
->GetX();
5788 rect_y
= rect
->GetY();
5789 rectWidth
= rect
->GetWidth();
5790 rectHeight
= rect
->GetHeight();
5792 width_label
= m_rowLabelWidth
- rect_x
;
5793 if (width_label
> rectWidth
) width_label
= rectWidth
;
5795 height_label
= m_colLabelHeight
- rect_y
;
5796 if (height_label
> rectHeight
) height_label
= rectHeight
;
5798 if (rect_x
> m_rowLabelWidth
)
5800 x
= rect_x
- m_rowLabelWidth
;
5801 width_cell
= rectWidth
;
5806 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
5809 if (rect_y
> m_colLabelHeight
)
5811 y
= rect_y
- m_colLabelHeight
;
5812 height_cell
= rectHeight
;
5817 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
5820 // Paint corner label part intersecting rect.
5821 if ( width_label
> 0 && height_label
> 0 )
5823 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
5824 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
5827 // Paint col labels part intersecting rect.
5828 if ( width_cell
> 0 && height_label
> 0 )
5830 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
5831 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
5834 // Paint row labels part intersecting rect.
5835 if ( width_label
> 0 && height_cell
> 0 )
5837 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
5838 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
5841 // Paint cell area part intersecting rect.
5842 if ( width_cell
> 0 && height_cell
> 0 )
5844 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
5845 m_gridWin
->Refresh(eraseb
, &anotherrect
);
5850 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
5851 m_colLabelWin
->Refresh(eraseb
, NULL
);
5852 m_rowLabelWin
->Refresh(eraseb
, NULL
);
5853 m_gridWin
->Refresh(eraseb
, NULL
);
5858 void wxGrid::OnSize( wxSizeEvent
& event
)
5860 // position the child windows
5863 // don't call CalcDimensions() from here, the base class handles the size
5869 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5871 if ( m_inOnKeyDown
)
5873 // shouldn't be here - we are going round in circles...
5875 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5878 m_inOnKeyDown
= TRUE
;
5880 // propagate the event up and see if it gets processed
5882 wxWindow
*parent
= GetParent();
5883 wxKeyEvent
keyEvt( event
);
5884 keyEvt
.SetEventObject( parent
);
5886 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5889 // try local handlers
5891 switch ( event
.KeyCode() )
5894 if ( event
.ControlDown() )
5896 MoveCursorUpBlock( event
.ShiftDown() );
5900 MoveCursorUp( event
.ShiftDown() );
5905 if ( event
.ControlDown() )
5907 MoveCursorDownBlock( event
.ShiftDown() );
5911 MoveCursorDown( event
.ShiftDown() );
5916 if ( event
.ControlDown() )
5918 MoveCursorLeftBlock( event
.ShiftDown() );
5922 MoveCursorLeft( event
.ShiftDown() );
5927 if ( event
.ControlDown() )
5929 MoveCursorRightBlock( event
.ShiftDown() );
5933 MoveCursorRight( event
.ShiftDown() );
5938 case WXK_NUMPAD_ENTER
:
5939 if ( event
.ControlDown() )
5941 event
.Skip(); // to let the edit control have the return
5945 if ( GetGridCursorRow() < GetNumberRows()-1 )
5947 MoveCursorDown( event
.ShiftDown() );
5951 // at the bottom of a column
5952 HideCellEditControl();
5953 SaveEditControlValue();
5963 if (event
.ShiftDown())
5965 if ( GetGridCursorCol() > 0 )
5967 MoveCursorLeft( FALSE
);
5972 HideCellEditControl();
5973 SaveEditControlValue();
5978 if ( GetGridCursorCol() < GetNumberCols()-1 )
5980 MoveCursorRight( FALSE
);
5985 HideCellEditControl();
5986 SaveEditControlValue();
5992 if ( event
.ControlDown() )
5994 MakeCellVisible( 0, 0 );
5995 SetCurrentCell( 0, 0 );
6004 if ( event
.ControlDown() )
6006 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6007 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6024 if ( event
.ControlDown() )
6028 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6029 m_currentCellCoords
.GetCol(),
6030 event
.ControlDown(),
6037 if ( !IsEditable() )
6039 MoveCursorRight( FALSE
);
6042 // Otherwise fall through to default
6045 // is it possible to edit the current cell at all?
6046 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6048 // yes, now check whether the cells editor accepts the key
6049 int row
= m_currentCellCoords
.GetRow();
6050 int col
= m_currentCellCoords
.GetCol();
6051 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6052 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6054 // <F2> is special and will always start editing, for
6055 // other keys - ask the editor itself
6056 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6057 || editor
->IsAcceptedKey(event
) )
6059 EnableCellEditControl();
6061 // the editor could be not shown for a variety of
6062 // reasons (i.e. blocked by the app or whatever), so
6063 // check if it really was created
6064 if ( m_cellEditCtrlEnabled
)
6066 editor
->StartingKey(event
);
6079 // let others process char events with modifiers or all
6080 // char events for readonly cells
6087 m_inOnKeyDown
= FALSE
;
6090 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6092 // try local handlers
6094 if ( event
.KeyCode() == WXK_SHIFT
)
6096 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6097 m_selectingBottomRight
!= wxGridNoCellCoords
)
6101 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6102 m_selectingTopLeft
.GetCol(),
6103 m_selectingBottomRight
.GetRow(),
6104 m_selectingBottomRight
.GetCol(),
6105 event
.ControlDown(),
6112 m_selectingTopLeft
= wxGridNoCellCoords
;
6113 m_selectingBottomRight
= wxGridNoCellCoords
;
6114 m_selectingKeyboard
= wxGridNoCellCoords
;
6118 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6122 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6124 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6126 // the event has been intercepted - do nothing
6130 wxClientDC
dc(m_gridWin
);
6133 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6135 HideCellEditControl();
6136 DisableCellEditControl();
6138 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6141 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6142 if ( !m_gridLinesEnabled
)
6150 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6152 // Otherwise refresh redraws the highlight!
6153 m_currentCellCoords
= coords
;
6155 DrawGridCellArea(dc
,cells
);
6156 DrawAllGridLines( dc
, r
);
6160 m_currentCellCoords
= coords
;
6162 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6163 DrawCellHighlight(dc
, attr
);
6168 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6171 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6175 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6178 rightCol
= GetNumberCols() - 1;
6180 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6183 bottomRow
= GetNumberRows() - 1;
6187 if ( topRow
> bottomRow
)
6194 if ( leftCol
> rightCol
)
6201 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6202 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6204 // First the case that we selected a completely new area
6205 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6206 m_selectingBottomRight
== wxGridNoCellCoords
)
6209 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6210 wxGridCellCoords ( bottomRow
, rightCol
) );
6211 m_gridWin
->Refresh( FALSE
, &rect
);
6213 // Now handle changing an existing selection area.
6214 else if ( m_selectingTopLeft
!= updateTopLeft
||
6215 m_selectingBottomRight
!= updateBottomRight
)
6217 // Compute two optimal update rectangles:
6218 // Either one rectangle is a real subset of the
6219 // other, or they are (almost) disjoint!
6221 bool need_refresh
[4];
6225 need_refresh
[3] = FALSE
;
6228 // Store intermediate values
6229 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6230 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6231 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6232 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6234 // Determine the outer/inner coordinates.
6235 if (oldLeft
> leftCol
)
6241 if (oldTop
> topRow
)
6247 if (oldRight
< rightCol
)
6250 oldRight
= rightCol
;
6253 if (oldBottom
< bottomRow
)
6256 oldBottom
= bottomRow
;
6260 // Now, either the stuff marked old is the outer
6261 // rectangle or we don't have a situation where one
6262 // is contained in the other.
6264 if ( oldLeft
< leftCol
)
6266 // Refresh the newly selected or deselected
6267 // area to the left of the old or new selection.
6268 need_refresh
[0] = TRUE
;
6269 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6271 wxGridCellCoords ( oldBottom
,
6275 if ( oldTop
< topRow
)
6277 // Refresh the newly selected or deselected
6278 // area above the old or new selection.
6279 need_refresh
[1] = TRUE
;
6280 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6282 wxGridCellCoords ( topRow
- 1,
6286 if ( oldRight
> rightCol
)
6288 // Refresh the newly selected or deselected
6289 // area to the right of the old or new selection.
6290 need_refresh
[2] = TRUE
;
6291 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6293 wxGridCellCoords ( oldBottom
,
6297 if ( oldBottom
> bottomRow
)
6299 // Refresh the newly selected or deselected
6300 // area below the old or new selection.
6301 need_refresh
[3] = TRUE
;
6302 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6304 wxGridCellCoords ( oldBottom
,
6308 // various Refresh() calls
6309 for (i
= 0; i
< 4; i
++ )
6310 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6311 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6314 m_selectingTopLeft
= updateTopLeft
;
6315 m_selectingBottomRight
= updateBottomRight
;
6319 // ------ functions to get/send data (see also public functions)
6322 bool wxGrid::GetModelValues()
6324 // Hide the editor, so it won't hide a changed value.
6325 HideCellEditControl();
6329 // all we need to do is repaint the grid
6331 m_gridWin
->Refresh();
6339 bool wxGrid::SetModelValues()
6343 // Disable the editor, so it won't hide a changed value.
6344 // Do we also want to save the current value of the editor first?
6346 DisableCellEditControl();
6350 for ( row
= 0; row
< m_numRows
; row
++ )
6352 for ( col
= 0; col
< m_numCols
; col
++ )
6354 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6366 // Note - this function only draws cells that are in the list of
6367 // exposed cells (usually set from the update region by
6368 // CalcExposedCells)
6370 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6372 if ( !m_numRows
|| !m_numCols
) return;
6374 int i
, j
, k
, l
, numCells
= cells
.GetCount();
6375 int row
, col
, cell_rows
, cell_cols
;
6376 wxGridCellCoordsArray redrawCells
;
6378 for ( i
= numCells
-1; i
>= 0; i
-- )
6380 row
= cells
[i
].GetRow();
6381 col
= cells
[i
].GetCol();
6382 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6384 // If this cell is part of a multicell block, find owner for repaint
6385 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6387 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6388 bool marked
= FALSE
;
6389 for ( j
= 0; j
< numCells
; j
++ )
6391 if ( cell
== cells
[j
] )
6399 int count
= redrawCells
.GetCount();
6400 for (j
= 0; j
< count
; j
++)
6402 if ( cell
== redrawCells
[j
] )
6408 if (!marked
) redrawCells
.Add( cell
);
6410 continue; // don't bother drawing this cell
6413 // If this cell is empty, find cell to left that might want to overflow
6414 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6416 for ( l
= 0; l
< cell_rows
; l
++ )
6418 for (j
= col
-1; j
>= 0; j
--)
6420 if (!m_table
->IsEmptyCell(row
+l
, j
))
6422 if (GetCellOverflow(row
+l
, j
))
6424 wxGridCellCoords
cell(row
+l
, j
);
6425 bool marked
= FALSE
;
6427 for (k
= 0; k
< numCells
; k
++)
6429 if ( cell
== cells
[k
] )
6437 int count
= redrawCells
.GetCount();
6438 for (k
= 0; k
< count
; k
++)
6440 if ( cell
== redrawCells
[k
] )
6446 if (!marked
) redrawCells
.Add( cell
);
6454 DrawCell( dc
, cells
[i
] );
6457 numCells
= redrawCells
.GetCount();
6459 for ( i
= numCells
- 1; i
>= 0; i
-- )
6461 DrawCell( dc
, redrawCells
[i
] );
6466 void wxGrid::DrawGridSpace( wxDC
& dc
)
6469 m_gridWin
->GetClientSize( &cw
, &ch
);
6472 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6474 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6475 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6477 if ( right
> rightCol
|| bottom
> bottomRow
)
6480 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6482 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6483 dc
.SetPen( *wxTRANSPARENT_PEN
);
6485 if ( right
> rightCol
)
6487 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6490 if ( bottom
> bottomRow
)
6492 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6498 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6500 int row
= coords
.GetRow();
6501 int col
= coords
.GetCol();
6503 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6506 // we draw the cell border ourselves
6507 #if !WXGRID_DRAW_LINES
6508 if ( m_gridLinesEnabled
)
6509 DrawCellBorder( dc
, coords
);
6512 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6514 bool isCurrent
= coords
== m_currentCellCoords
;
6516 wxRect rect
= CellToRect( row
, col
);
6518 // if the editor is shown, we should use it and not the renderer
6519 // Note: However, only if it is really _shown_, i.e. not hidden!
6520 if ( isCurrent
&& IsCellEditControlShown() )
6522 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6523 editor
->PaintBackground(rect
, attr
);
6528 // but all the rest is drawn by the cell renderer and hence may be
6530 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6531 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6538 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6540 int row
= m_currentCellCoords
.GetRow();
6541 int col
= m_currentCellCoords
.GetCol();
6543 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6546 wxRect rect
= CellToRect(row
, col
);
6548 // hmmm... what could we do here to show that the cell is disabled?
6549 // for now, I just draw a thinner border than for the other ones, but
6550 // it doesn't look really good
6552 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6556 // The center of th drawn line is where the position/width/height of
6557 // the rectangle is actually at, (on wxMSW atr least,) so we will
6558 // reduce the size of the rectangle to compensate for the thickness of
6559 // the line. If this is too strange on non wxMSW platforms then
6560 // please #ifdef this appropriately.
6561 rect
.x
+= penWidth
/2;
6562 rect
.y
+= penWidth
/2;
6563 rect
.width
-= penWidth
-1;
6564 rect
.height
-= penWidth
-1;
6567 // Now draw the rectangle
6568 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6569 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6570 dc
.DrawRectangle(rect
);
6574 // VZ: my experiments with 3d borders...
6576 // how to properly set colours for arbitrary bg?
6577 wxCoord x1
= rect
.x
,
6579 x2
= rect
.x
+ rect
.width
-1,
6580 y2
= rect
.y
+ rect
.height
-1;
6582 dc
.SetPen(*wxWHITE_PEN
);
6583 dc
.DrawLine(x1
, y1
, x2
, y1
);
6584 dc
.DrawLine(x1
, y1
, x1
, y2
);
6586 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6587 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6589 dc
.SetPen(*wxBLACK_PEN
);
6590 dc
.DrawLine(x1
, y2
, x2
, y2
);
6591 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6596 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6598 int row
= coords
.GetRow();
6599 int col
= coords
.GetCol();
6600 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6603 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6605 wxRect rect
= CellToRect( row
, col
);
6607 // right hand border
6609 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6610 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6614 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6615 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6618 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6620 // This if block was previously in wxGrid::OnPaint but that doesn't
6621 // seem to get called under wxGTK - MB
6623 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6624 m_numRows
&& m_numCols
)
6626 m_currentCellCoords
.Set(0, 0);
6629 if ( IsCellEditControlShown() )
6631 // don't show highlight when the edit control is shown
6635 // if the active cell was repainted, repaint its highlight too because it
6636 // might have been damaged by the grid lines
6637 size_t count
= cells
.GetCount();
6638 for ( size_t n
= 0; n
< count
; n
++ )
6640 if ( cells
[n
] == m_currentCellCoords
)
6642 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6643 DrawCellHighlight(dc
, attr
);
6651 // TODO: remove this ???
6652 // This is used to redraw all grid lines e.g. when the grid line colour
6655 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6657 #if !WXGRID_DRAW_LINES
6661 if ( !m_gridLinesEnabled
||
6663 !m_numCols
) return;
6665 int top
, bottom
, left
, right
;
6667 #if 0 //#ifndef __WXGTK__
6671 m_gridWin
->GetClientSize(&cw
, &ch
);
6673 // virtual coords of visible area
6675 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6676 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6681 reg
.GetBox(x
, y
, w
, h
);
6682 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6683 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6687 m_gridWin
->GetClientSize(&cw
, &ch
);
6688 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6689 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6692 // avoid drawing grid lines past the last row and col
6694 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6695 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6697 // no gridlines inside multicells, clip them out
6698 int leftCol
= XToCol(left
);
6699 int topRow
= YToRow(top
);
6700 int rightCol
= XToCol(right
);
6701 int bottomRow
= YToRow(bottom
);
6702 wxRegion
clippedcells(0, 0, cw
, ch
);
6704 if ((leftCol
>= 0) && (topRow
>= 0))
6706 if (rightCol
< 0) rightCol
= m_numCols
;
6707 if (bottomRow
< 0) bottomRow
= m_numRows
;
6709 int i
, j
, cell_rows
, cell_cols
;
6712 for (j
=topRow
; j
<bottomRow
; j
++)
6714 for (i
=leftCol
; i
<rightCol
; i
++)
6716 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6717 if ((cell_rows
> 1) || (cell_cols
> 1))
6719 rect
= CellToRect(j
,i
);
6720 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6721 clippedcells
.Subtract(rect
);
6723 else if ((cell_rows
< 0) || (cell_cols
< 0))
6725 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6726 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6727 clippedcells
.Subtract(rect
);
6732 dc
.SetClippingRegion( clippedcells
);
6734 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6736 // horizontal grid lines
6739 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6741 int bot
= GetRowBottom(i
) - 1;
6750 dc
.DrawLine( left
, bot
, right
, bot
);
6755 // vertical grid lines
6757 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6759 int colRight
= GetColRight(i
) - 1;
6760 if ( colRight
> right
)
6765 if ( colRight
>= left
)
6767 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6770 dc
.DestroyClippingRegion();
6774 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6776 if ( !m_numRows
) return;
6779 size_t numLabels
= rows
.GetCount();
6781 for ( i
= 0; i
< numLabels
; i
++ )
6783 DrawRowLabel( dc
, rows
[i
] );
6788 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6790 if ( GetRowHeight(row
) <= 0 )
6793 int rowTop
= GetRowTop(row
),
6794 rowBottom
= GetRowBottom(row
) - 1;
6796 dc
.SetPen( *wxBLACK_PEN
);
6797 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6798 m_rowLabelWidth
-1, rowBottom
);
6800 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6802 dc
.SetPen( *wxWHITE_PEN
);
6803 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6804 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6806 dc
.SetBackgroundMode( wxTRANSPARENT
);
6807 dc
.SetTextForeground( GetLabelTextColour() );
6808 dc
.SetFont( GetLabelFont() );
6811 GetRowLabelAlignment( &hAlign
, &vAlign
);
6815 rect
.SetY( GetRowTop(row
) + 2 );
6816 rect
.SetWidth( m_rowLabelWidth
- 4 );
6817 rect
.SetHeight( GetRowHeight(row
) - 4 );
6818 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6822 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6824 if ( !m_numCols
) return;
6827 size_t numLabels
= cols
.GetCount();
6829 for ( i
= 0; i
< numLabels
; i
++ )
6831 DrawColLabel( dc
, cols
[i
] );
6836 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6838 if ( GetColWidth(col
) <= 0 )
6841 int colLeft
= GetColLeft(col
),
6842 colRight
= GetColRight(col
) - 1;
6844 dc
.SetPen( *wxBLACK_PEN
);
6845 dc
.DrawLine( colRight
, 0,
6846 colRight
, m_colLabelHeight
-1 );
6848 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6849 colRight
, m_colLabelHeight
-1 );
6851 dc
.SetPen( *wxWHITE_PEN
);
6852 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6853 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6855 dc
.SetBackgroundMode( wxTRANSPARENT
);
6856 dc
.SetTextForeground( GetLabelTextColour() );
6857 dc
.SetFont( GetLabelFont() );
6859 dc
.SetBackgroundMode( wxTRANSPARENT
);
6860 dc
.SetTextForeground( GetLabelTextColour() );
6861 dc
.SetFont( GetLabelFont() );
6864 GetColLabelAlignment( &hAlign
, &vAlign
);
6867 rect
.SetX( colLeft
+ 2 );
6869 rect
.SetWidth( GetColWidth(col
) - 4 );
6870 rect
.SetHeight( m_colLabelHeight
- 4 );
6871 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6874 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6875 const wxString
& value
,
6880 wxArrayString lines
;
6882 StringToLines( value
, lines
);
6885 //Forward to new API.
6886 DrawTextRectangle( dc
,
6894 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6895 const wxArrayString
& lines
,
6900 long textWidth
, textHeight
;
6901 long lineWidth
, lineHeight
;
6903 dc
.SetClippingRegion( rect
);
6904 if ( lines
.GetCount() )
6906 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6907 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6910 switch ( horizAlign
)
6913 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6916 case wxALIGN_CENTRE
:
6917 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6926 switch ( vertAlign
)
6928 case wxALIGN_BOTTOM
:
6929 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6932 case wxALIGN_CENTRE
:
6933 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6942 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6944 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6949 dc
.DestroyClippingRegion();
6953 // Split multi line text up into an array of strings. Any existing
6954 // contents of the string array are preserved.
6956 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6960 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6961 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6963 while ( startPos
< (int)tVal
.Length() )
6965 pos
= tVal
.Mid(startPos
).Find( eol
);
6970 else if ( pos
== 0 )
6972 lines
.Add( wxEmptyString
);
6976 lines
.Add( value
.Mid(startPos
, pos
) );
6980 if ( startPos
< (int)value
.Length() )
6982 lines
.Add( value
.Mid( startPos
) );
6987 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6988 const wxArrayString
& lines
,
6989 long *width
, long *height
)
6996 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6998 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6999 w
= wxMax( w
, lineW
);
7008 // ------ Batch processing.
7010 void wxGrid::EndBatch()
7012 if ( m_batchCount
> 0 )
7015 if ( !m_batchCount
)
7018 m_rowLabelWin
->Refresh();
7019 m_colLabelWin
->Refresh();
7020 m_cornerLabelWin
->Refresh();
7021 m_gridWin
->Refresh();
7026 // Use this, rather than wxWindow::Refresh(), to force an immediate
7027 // repainting of the grid. Has no effect if you are already inside a
7028 // BeginBatch / EndBatch block.
7030 void wxGrid::ForceRefresh()
7038 // ------ Edit control functions
7042 void wxGrid::EnableEditing( bool edit
)
7044 // TODO: improve this ?
7046 if ( edit
!= m_editable
)
7048 if(!edit
) EnableCellEditControl(edit
);
7054 void wxGrid::EnableCellEditControl( bool enable
)
7059 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7060 SetCurrentCell( 0, 0 );
7062 if ( enable
!= m_cellEditCtrlEnabled
)
7066 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7069 // this should be checked by the caller!
7070 wxASSERT_MSG( CanEnableCellControl(),
7071 _T("can't enable editing for this cell!") );
7073 // do it before ShowCellEditControl()
7074 m_cellEditCtrlEnabled
= enable
;
7076 ShowCellEditControl();
7080 //FIXME:add veto support
7081 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7083 HideCellEditControl();
7084 SaveEditControlValue();
7086 // do it after HideCellEditControl()
7087 m_cellEditCtrlEnabled
= enable
;
7092 bool wxGrid::IsCurrentCellReadOnly() const
7095 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7096 bool readonly
= attr
->IsReadOnly();
7102 bool wxGrid::CanEnableCellControl() const
7104 return m_editable
&& !IsCurrentCellReadOnly();
7107 bool wxGrid::IsCellEditControlEnabled() const
7109 // the cell edit control might be disable for all cells or just for the
7110 // current one if it's read only
7111 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7114 bool wxGrid::IsCellEditControlShown() const
7116 bool isShown
= FALSE
;
7118 if ( m_cellEditCtrlEnabled
)
7120 int row
= m_currentCellCoords
.GetRow();
7121 int col
= m_currentCellCoords
.GetCol();
7122 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7123 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7128 if ( editor
->IsCreated() )
7130 isShown
= editor
->GetControl()->IsShown();
7140 void wxGrid::ShowCellEditControl()
7142 if ( IsCellEditControlEnabled() )
7144 if ( !IsVisible( m_currentCellCoords
) )
7146 m_cellEditCtrlEnabled
= FALSE
;
7151 wxRect rect
= CellToRect( m_currentCellCoords
);
7152 int row
= m_currentCellCoords
.GetRow();
7153 int col
= m_currentCellCoords
.GetCol();
7155 // if this is part of a multicell, find owner (topleft)
7156 int cell_rows
, cell_cols
;
7157 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7158 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7162 m_currentCellCoords
.SetRow( row
);
7163 m_currentCellCoords
.SetCol( col
);
7166 // convert to scrolled coords
7168 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7170 // done in PaintBackground()
7172 // erase the highlight and the cell contents because the editor
7173 // might not cover the entire cell
7174 wxClientDC
dc( m_gridWin
);
7176 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7177 dc
.SetPen(*wxTRANSPARENT_PEN
);
7178 dc
.DrawRectangle(rect
);
7181 // cell is shifted by one pixel
7182 // However, don't allow x or y to become negative
7183 // since the SetSize() method interprets that as
7190 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7191 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7192 if ( !editor
->IsCreated() )
7194 editor
->Create(m_gridWin
, -1,
7195 new wxGridCellEditorEvtHandler(this, editor
));
7197 wxGridEditorCreatedEvent
evt(GetId(),
7198 wxEVT_GRID_EDITOR_CREATED
,
7202 editor
->GetControl());
7203 GetEventHandler()->ProcessEvent(evt
);
7206 editor
->Show( TRUE
, attr
);
7208 // resize editor to overflow into righthand cells if allowed
7209 wxString value
= GetCellValue(row
, col
);
7210 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7212 wxClientDC
dc(m_gridWin
);
7213 wxCoord y
= 0, best_width
= 0;
7214 dc
.SetFont(attr
->GetFont());
7215 dc
.GetTextExtent(value
, &best_width
, &y
);
7217 int cell_rows
, cell_cols
;
7218 attr
->GetSize( &cell_rows
, &cell_cols
);
7220 if ((best_width
> rect
.width
) && (col
< m_numCols
) && m_table
)
7223 for (i
= col
+cell_cols
; i
< m_numCols
; i
++)
7225 if (m_table
->IsEmptyCell(row
,i
))
7227 rect
.width
+= GetColWidth(i
);
7228 if (rect
.width
>= best_width
) break;
7235 editor
->SetSize( rect
);
7237 editor
->BeginEdit(row
, col
, this);
7246 void wxGrid::HideCellEditControl()
7248 if ( IsCellEditControlEnabled() )
7250 int row
= m_currentCellCoords
.GetRow();
7251 int col
= m_currentCellCoords
.GetCol();
7253 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7254 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7255 editor
->Show( FALSE
);
7258 m_gridWin
->SetFocus();
7259 // refresh whole row to the right
7260 wxRect
rect( CellToRect(row
, col
) );
7261 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7262 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7263 m_gridWin
->Refresh( FALSE
, &rect
);
7268 void wxGrid::SaveEditControlValue()
7270 if ( IsCellEditControlEnabled() )
7272 int row
= m_currentCellCoords
.GetRow();
7273 int col
= m_currentCellCoords
.GetCol();
7275 wxString oldval
= GetCellValue(row
,col
);
7277 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7278 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7279 bool changed
= editor
->EndEdit(row
, col
, this);
7286 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7287 m_currentCellCoords
.GetRow(),
7288 m_currentCellCoords
.GetCol() ) < 0 ) {
7290 // Event has been vetoed, set the data back.
7291 SetCellValue(row
,col
,oldval
);
7299 // ------ Grid location functions
7300 // Note that all of these functions work with the logical coordinates of
7301 // grid cells and labels so you will need to convert from device
7302 // coordinates for mouse events etc.
7305 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7307 int row
= YToRow(y
);
7308 int col
= XToCol(x
);
7310 if ( row
== -1 || col
== -1 )
7312 coords
= wxGridNoCellCoords
;
7316 coords
.Set( row
, col
);
7321 // Internal Helper function for computing row or column from some
7322 // (unscrolled) coordinate value, using either
7323 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7324 // of m_rowBottoms/m_ColRights to speed up the search!
7326 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7327 const wxArrayInt
& BorderArray
, int nMax
,
7332 size_t i_max
= coord
/ defaultDist
,
7334 if (BorderArray
.IsEmpty())
7336 if((int) i_max
<= nMax
)
7338 return maxOnOverflow
? (int)i_max
: -1;
7341 if ( i_max
>= BorderArray
.GetCount())
7342 i_max
= BorderArray
.GetCount() - 1;
7345 if ( coord
>= BorderArray
[i_max
])
7348 i_max
= coord
/ minDist
;
7350 if ( i_max
>= BorderArray
.GetCount())
7351 i_max
= BorderArray
.GetCount() - 1;
7353 if ( coord
>= BorderArray
[i_max
])
7354 return maxOnOverflow
? (int)i_max
: -1;
7355 if ( coord
< BorderArray
[0] )
7358 while ( i_max
- i_min
> 0 )
7360 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7361 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7362 if (coord
>= BorderArray
[ i_max
- 1])
7366 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7367 if (coord
< BorderArray
[median
])
7375 int wxGrid::YToRow( int y
)
7377 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7378 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7382 int wxGrid::XToCol( int x
)
7384 return CoordToRowOrCol(x
, m_defaultColWidth
,
7385 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7389 // return the row number that that the y coord is near the edge of, or
7390 // -1 if not near an edge
7392 int wxGrid::YToEdgeOfRow( int y
)
7395 i
= internalYToRow(y
);
7397 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7399 // We know that we are in row i, test whether we are
7400 // close enough to lower or upper border, respectively.
7401 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7403 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7411 // return the col number that that the x coord is near the edge of, or
7412 // -1 if not near an edge
7414 int wxGrid::XToEdgeOfCol( int x
)
7417 i
= internalXToCol(x
);
7419 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7421 // We know that we are in column i, test whether we are
7422 // close enough to right or left border, respectively.
7423 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7425 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7433 wxRect
wxGrid::CellToRect( int row
, int col
)
7435 wxRect
rect( -1, -1, -1, -1 );
7437 if ( row
>= 0 && row
< m_numRows
&&
7438 col
>= 0 && col
< m_numCols
)
7440 int i
, cell_rows
, cell_cols
;
7441 rect
.width
= rect
.height
= 0;
7442 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7443 // if negative then find multicell owner
7444 if (cell_rows
< 0) row
+= cell_rows
;
7445 if (cell_cols
< 0) col
+= cell_cols
;
7446 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7448 rect
.x
= GetColLeft(col
);
7449 rect
.y
= GetRowTop(row
);
7450 for (i
=col
; i
<col
+cell_cols
; i
++)
7451 rect
.width
+= GetColWidth(i
);
7452 for (i
=row
; i
<row
+cell_rows
; i
++)
7453 rect
.height
+= GetRowHeight(i
);
7456 // if grid lines are enabled, then the area of the cell is a bit smaller
7457 if (m_gridLinesEnabled
) {
7465 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7467 // get the cell rectangle in logical coords
7469 wxRect
r( CellToRect( row
, col
) );
7471 // convert to device coords
7473 int left
, top
, right
, bottom
;
7474 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7475 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7477 // check against the client area of the grid window
7480 m_gridWin
->GetClientSize( &cw
, &ch
);
7482 if ( wholeCellVisible
)
7484 // is the cell wholly visible ?
7486 return ( left
>= 0 && right
<= cw
&&
7487 top
>= 0 && bottom
<= ch
);
7491 // is the cell partly visible ?
7493 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7494 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7499 // make the specified cell location visible by doing a minimal amount
7502 void wxGrid::MakeCellVisible( int row
, int col
)
7506 int xpos
= -1, ypos
= -1;
7508 if ( row
>= 0 && row
< m_numRows
&&
7509 col
>= 0 && col
< m_numCols
)
7511 // get the cell rectangle in logical coords
7513 wxRect
r( CellToRect( row
, col
) );
7515 // convert to device coords
7517 int left
, top
, right
, bottom
;
7518 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7519 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7522 m_gridWin
->GetClientSize( &cw
, &ch
);
7528 else if ( bottom
> ch
)
7530 int h
= r
.GetHeight();
7532 for ( i
= row
-1; i
>= 0; i
-- )
7534 int rowHeight
= GetRowHeight(i
);
7535 if ( h
+ rowHeight
> ch
)
7542 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7543 // have rounding errors (this is important, because if we do, we
7544 // might not scroll at all and some cells won't be redrawn)
7546 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7548 ypos
+= GRID_SCROLL_LINE_Y
;
7555 else if ( right
> cw
)
7557 int w
= r
.GetWidth();
7559 for ( i
= col
-1; i
>= 0; i
-- )
7561 int colWidth
= GetColWidth(i
);
7562 if ( w
+ colWidth
> cw
)
7569 // see comment for ypos above
7570 xpos
+= GRID_SCROLL_LINE_X
;
7573 if ( xpos
!= -1 || ypos
!= -1 )
7576 xpos
/= GRID_SCROLL_LINE_X
;
7578 ypos
/= GRID_SCROLL_LINE_Y
;
7579 Scroll( xpos
, ypos
);
7587 // ------ Grid cursor movement functions
7590 bool wxGrid::MoveCursorUp( bool expandSelection
)
7592 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7593 m_currentCellCoords
.GetRow() >= 0 )
7595 if ( expandSelection
)
7597 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7598 m_selectingKeyboard
= m_currentCellCoords
;
7599 if ( m_selectingKeyboard
.GetRow() > 0 )
7601 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7602 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7603 m_selectingKeyboard
.GetCol() );
7604 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7607 else if ( m_currentCellCoords
.GetRow() > 0 )
7610 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7611 m_currentCellCoords
.GetCol() );
7612 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7613 m_currentCellCoords
.GetCol() );
7624 bool wxGrid::MoveCursorDown( bool expandSelection
)
7626 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7627 m_currentCellCoords
.GetRow() < m_numRows
)
7629 if ( expandSelection
)
7631 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7632 m_selectingKeyboard
= m_currentCellCoords
;
7633 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7635 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7636 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7637 m_selectingKeyboard
.GetCol() );
7638 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7641 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7644 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7645 m_currentCellCoords
.GetCol() );
7646 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7647 m_currentCellCoords
.GetCol() );
7658 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7660 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7661 m_currentCellCoords
.GetCol() >= 0 )
7663 if ( expandSelection
)
7665 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7666 m_selectingKeyboard
= m_currentCellCoords
;
7667 if ( m_selectingKeyboard
.GetCol() > 0 )
7669 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7670 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7671 m_selectingKeyboard
.GetCol() );
7672 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7675 else if ( m_currentCellCoords
.GetCol() > 0 )
7678 MakeCellVisible( m_currentCellCoords
.GetRow(),
7679 m_currentCellCoords
.GetCol() - 1 );
7680 SetCurrentCell( m_currentCellCoords
.GetRow(),
7681 m_currentCellCoords
.GetCol() - 1 );
7692 bool wxGrid::MoveCursorRight( bool expandSelection
)
7694 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7695 m_currentCellCoords
.GetCol() < m_numCols
)
7697 if ( expandSelection
)
7699 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7700 m_selectingKeyboard
= m_currentCellCoords
;
7701 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7703 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7704 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7705 m_selectingKeyboard
.GetCol() );
7706 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7709 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7712 MakeCellVisible( m_currentCellCoords
.GetRow(),
7713 m_currentCellCoords
.GetCol() + 1 );
7714 SetCurrentCell( m_currentCellCoords
.GetRow(),
7715 m_currentCellCoords
.GetCol() + 1 );
7726 bool wxGrid::MovePageUp()
7728 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7730 int row
= m_currentCellCoords
.GetRow();
7734 m_gridWin
->GetClientSize( &cw
, &ch
);
7736 int y
= GetRowTop(row
);
7737 int newRow
= YToRow( y
- ch
+ 1 );
7742 else if ( newRow
== row
)
7747 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7748 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7756 bool wxGrid::MovePageDown()
7758 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7760 int row
= m_currentCellCoords
.GetRow();
7761 if ( row
< m_numRows
)
7764 m_gridWin
->GetClientSize( &cw
, &ch
);
7766 int y
= GetRowTop(row
);
7767 int newRow
= YToRow( y
+ ch
);
7770 newRow
= m_numRows
- 1;
7772 else if ( newRow
== row
)
7777 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7778 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7786 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7789 m_currentCellCoords
!= wxGridNoCellCoords
&&
7790 m_currentCellCoords
.GetRow() > 0 )
7792 int row
= m_currentCellCoords
.GetRow();
7793 int col
= m_currentCellCoords
.GetCol();
7795 if ( m_table
->IsEmptyCell(row
, col
) )
7797 // starting in an empty cell: find the next block of
7803 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7806 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7808 // starting at the top of a block: find the next block
7814 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7819 // starting within a block: find the top of the block
7824 if ( m_table
->IsEmptyCell(row
, col
) )
7832 MakeCellVisible( row
, col
);
7833 if ( expandSelection
)
7835 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7836 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7841 SetCurrentCell( row
, col
);
7849 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7852 m_currentCellCoords
!= wxGridNoCellCoords
&&
7853 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7855 int row
= m_currentCellCoords
.GetRow();
7856 int col
= m_currentCellCoords
.GetCol();
7858 if ( m_table
->IsEmptyCell(row
, col
) )
7860 // starting in an empty cell: find the next block of
7863 while ( row
< m_numRows
-1 )
7866 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7869 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7871 // starting at the bottom of a block: find the next block
7874 while ( row
< m_numRows
-1 )
7877 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7882 // starting within a block: find the bottom of the block
7884 while ( row
< m_numRows
-1 )
7887 if ( m_table
->IsEmptyCell(row
, col
) )
7895 MakeCellVisible( row
, col
);
7896 if ( expandSelection
)
7898 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7899 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7904 SetCurrentCell( row
, col
);
7913 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7916 m_currentCellCoords
!= wxGridNoCellCoords
&&
7917 m_currentCellCoords
.GetCol() > 0 )
7919 int row
= m_currentCellCoords
.GetRow();
7920 int col
= m_currentCellCoords
.GetCol();
7922 if ( m_table
->IsEmptyCell(row
, col
) )
7924 // starting in an empty cell: find the next block of
7930 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7933 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7935 // starting at the left of a block: find the next block
7941 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7946 // starting within a block: find the left of the block
7951 if ( m_table
->IsEmptyCell(row
, col
) )
7959 MakeCellVisible( row
, col
);
7960 if ( expandSelection
)
7962 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7963 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7968 SetCurrentCell( row
, col
);
7977 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7980 m_currentCellCoords
!= wxGridNoCellCoords
&&
7981 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7983 int row
= m_currentCellCoords
.GetRow();
7984 int col
= m_currentCellCoords
.GetCol();
7986 if ( m_table
->IsEmptyCell(row
, col
) )
7988 // starting in an empty cell: find the next block of
7991 while ( col
< m_numCols
-1 )
7994 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7997 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7999 // starting at the right of a block: find the next block
8002 while ( col
< m_numCols
-1 )
8005 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8010 // starting within a block: find the right of the block
8012 while ( col
< m_numCols
-1 )
8015 if ( m_table
->IsEmptyCell(row
, col
) )
8023 MakeCellVisible( row
, col
);
8024 if ( expandSelection
)
8026 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8027 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8032 SetCurrentCell( row
, col
);
8044 // ------ Label values and formatting
8047 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8049 *horiz
= m_rowLabelHorizAlign
;
8050 *vert
= m_rowLabelVertAlign
;
8053 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8055 *horiz
= m_colLabelHorizAlign
;
8056 *vert
= m_colLabelVertAlign
;
8059 wxString
wxGrid::GetRowLabelValue( int row
)
8063 return m_table
->GetRowLabelValue( row
);
8073 wxString
wxGrid::GetColLabelValue( int col
)
8077 return m_table
->GetColLabelValue( col
);
8088 void wxGrid::SetRowLabelSize( int width
)
8090 width
= wxMax( width
, 0 );
8091 if ( width
!= m_rowLabelWidth
)
8095 m_rowLabelWin
->Show( FALSE
);
8096 m_cornerLabelWin
->Show( FALSE
);
8098 else if ( m_rowLabelWidth
== 0 )
8100 m_rowLabelWin
->Show( TRUE
);
8101 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8104 m_rowLabelWidth
= width
;
8106 wxScrolledWindow::Refresh( TRUE
);
8111 void wxGrid::SetColLabelSize( int height
)
8113 height
= wxMax( height
, 0 );
8114 if ( height
!= m_colLabelHeight
)
8118 m_colLabelWin
->Show( FALSE
);
8119 m_cornerLabelWin
->Show( FALSE
);
8121 else if ( m_colLabelHeight
== 0 )
8123 m_colLabelWin
->Show( TRUE
);
8124 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8127 m_colLabelHeight
= height
;
8129 wxScrolledWindow::Refresh( TRUE
);
8134 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8136 if ( m_labelBackgroundColour
!= colour
)
8138 m_labelBackgroundColour
= colour
;
8139 m_rowLabelWin
->SetBackgroundColour( colour
);
8140 m_colLabelWin
->SetBackgroundColour( colour
);
8141 m_cornerLabelWin
->SetBackgroundColour( colour
);
8143 if ( !GetBatchCount() )
8145 m_rowLabelWin
->Refresh();
8146 m_colLabelWin
->Refresh();
8147 m_cornerLabelWin
->Refresh();
8152 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8154 if ( m_labelTextColour
!= colour
)
8156 m_labelTextColour
= colour
;
8157 if ( !GetBatchCount() )
8159 m_rowLabelWin
->Refresh();
8160 m_colLabelWin
->Refresh();
8165 void wxGrid::SetLabelFont( const wxFont
& font
)
8168 if ( !GetBatchCount() )
8170 m_rowLabelWin
->Refresh();
8171 m_colLabelWin
->Refresh();
8175 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8177 // allow old (incorrect) defs to be used
8180 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8181 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8182 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8187 case wxTOP
: vert
= wxALIGN_TOP
; break;
8188 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8189 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8192 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8194 m_rowLabelHorizAlign
= horiz
;
8197 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8199 m_rowLabelVertAlign
= vert
;
8202 if ( !GetBatchCount() )
8204 m_rowLabelWin
->Refresh();
8208 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8210 // allow old (incorrect) defs to be used
8213 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8214 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8215 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8220 case wxTOP
: vert
= wxALIGN_TOP
; break;
8221 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8222 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8225 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8227 m_colLabelHorizAlign
= horiz
;
8230 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8232 m_colLabelVertAlign
= vert
;
8235 if ( !GetBatchCount() )
8237 m_colLabelWin
->Refresh();
8241 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8245 m_table
->SetRowLabelValue( row
, s
);
8246 if ( !GetBatchCount() )
8248 wxRect rect
= CellToRect( row
, 0);
8249 if ( rect
.height
> 0 )
8251 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8253 rect
.width
= m_rowLabelWidth
;
8254 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8260 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8264 m_table
->SetColLabelValue( col
, s
);
8265 if ( !GetBatchCount() )
8267 wxRect rect
= CellToRect( 0, col
);
8268 if ( rect
.width
> 0 )
8270 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8272 rect
.height
= m_colLabelHeight
;
8273 m_colLabelWin
->Refresh( TRUE
, &rect
);
8279 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8281 if ( m_gridLineColour
!= colour
)
8283 m_gridLineColour
= colour
;
8285 wxClientDC
dc( m_gridWin
);
8287 DrawAllGridLines( dc
, wxRegion() );
8292 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8294 if ( m_cellHighlightColour
!= colour
)
8296 m_cellHighlightColour
= colour
;
8298 wxClientDC
dc( m_gridWin
);
8300 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8301 DrawCellHighlight(dc
, attr
);
8306 void wxGrid::SetCellHighlightPenWidth(int width
)
8308 if (m_cellHighlightPenWidth
!= width
) {
8309 m_cellHighlightPenWidth
= width
;
8311 // Just redrawing the cell highlight is not enough since that won't
8312 // make any visible change if the the thickness is getting smaller.
8313 int row
= m_currentCellCoords
.GetRow();
8314 int col
= m_currentCellCoords
.GetCol();
8315 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8317 wxRect rect
= CellToRect(row
, col
);
8318 m_gridWin
->Refresh(TRUE
, &rect
);
8322 void wxGrid::SetCellHighlightROPenWidth(int width
)
8324 if (m_cellHighlightROPenWidth
!= width
) {
8325 m_cellHighlightROPenWidth
= width
;
8327 // Just redrawing the cell highlight is not enough since that won't
8328 // make any visible change if the the thickness is getting smaller.
8329 int row
= m_currentCellCoords
.GetRow();
8330 int col
= m_currentCellCoords
.GetCol();
8331 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8333 wxRect rect
= CellToRect(row
, col
);
8334 m_gridWin
->Refresh(TRUE
, &rect
);
8338 void wxGrid::EnableGridLines( bool enable
)
8340 if ( enable
!= m_gridLinesEnabled
)
8342 m_gridLinesEnabled
= enable
;
8344 if ( !GetBatchCount() )
8348 wxClientDC
dc( m_gridWin
);
8350 DrawAllGridLines( dc
, wxRegion() );
8354 m_gridWin
->Refresh();
8361 int wxGrid::GetDefaultRowSize()
8363 return m_defaultRowHeight
;
8366 int wxGrid::GetRowSize( int row
)
8368 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8370 return GetRowHeight(row
);
8373 int wxGrid::GetDefaultColSize()
8375 return m_defaultColWidth
;
8378 int wxGrid::GetColSize( int col
)
8380 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8382 return GetColWidth(col
);
8385 // ============================================================================
8386 // access to the grid attributes: each of them has a default value in the grid
8387 // itself and may be overidden on a per-cell basis
8388 // ============================================================================
8390 // ----------------------------------------------------------------------------
8391 // setting default attributes
8392 // ----------------------------------------------------------------------------
8394 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8396 m_defaultCellAttr
->SetBackgroundColour(col
);
8398 m_gridWin
->SetBackgroundColour(col
);
8402 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8404 m_defaultCellAttr
->SetTextColour(col
);
8407 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8409 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8412 void wxGrid::SetDefaultCellOverflow( bool allow
)
8414 m_defaultCellAttr
->SetOverflow(allow
);
8417 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8419 m_defaultCellAttr
->SetFont(font
);
8422 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8424 m_defaultCellAttr
->SetRenderer(renderer
);
8427 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8429 m_defaultCellAttr
->SetEditor(editor
);
8432 // ----------------------------------------------------------------------------
8433 // access to the default attrbiutes
8434 // ----------------------------------------------------------------------------
8436 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8438 return m_defaultCellAttr
->GetBackgroundColour();
8441 wxColour
wxGrid::GetDefaultCellTextColour()
8443 return m_defaultCellAttr
->GetTextColour();
8446 wxFont
wxGrid::GetDefaultCellFont()
8448 return m_defaultCellAttr
->GetFont();
8451 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8453 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8456 bool wxGrid::GetDefaultCellOverflow()
8458 return m_defaultCellAttr
->GetOverflow();
8461 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8463 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8466 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8468 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8471 // ----------------------------------------------------------------------------
8472 // access to cell attributes
8473 // ----------------------------------------------------------------------------
8475 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8477 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8478 wxColour colour
= attr
->GetBackgroundColour();
8483 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8485 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8486 wxColour colour
= attr
->GetTextColour();
8491 wxFont
wxGrid::GetCellFont( int row
, int col
)
8493 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8494 wxFont font
= attr
->GetFont();
8499 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8501 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8502 attr
->GetAlignment(horiz
, vert
);
8506 bool wxGrid::GetCellOverflow( int row
, int col
)
8508 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8509 bool allow
= attr
->GetOverflow();
8514 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8516 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8517 attr
->GetSize( num_rows
, num_cols
);
8521 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8523 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8524 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8530 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8532 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8533 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8539 bool wxGrid::IsReadOnly(int row
, int col
) const
8541 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8542 bool isReadOnly
= attr
->IsReadOnly();
8547 // ----------------------------------------------------------------------------
8548 // attribute support: cache, automatic provider creation, ...
8549 // ----------------------------------------------------------------------------
8551 bool wxGrid::CanHaveAttributes()
8558 return m_table
->CanHaveAttributes();
8561 void wxGrid::ClearAttrCache()
8563 if ( m_attrCache
.row
!= -1 )
8565 wxSafeDecRef(m_attrCache
.attr
);
8566 m_attrCache
.attr
= NULL
;
8567 m_attrCache
.row
= -1;
8571 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8573 wxGrid
*self
= (wxGrid
*)this; // const_cast
8575 self
->ClearAttrCache();
8576 self
->m_attrCache
.row
= row
;
8577 self
->m_attrCache
.col
= col
;
8578 self
->m_attrCache
.attr
= attr
;
8582 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8584 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8586 *attr
= m_attrCache
.attr
;
8587 wxSafeIncRef(m_attrCache
.attr
);
8589 #ifdef DEBUG_ATTR_CACHE
8590 gs_nAttrCacheHits
++;
8597 #ifdef DEBUG_ATTR_CACHE
8598 gs_nAttrCacheMisses
++;
8604 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8606 wxGridCellAttr
*attr
= NULL
;
8607 // Additional test to avoid looking at the cache e.g. for
8608 // wxNoCellCoords, as this will confuse memory management.
8611 if ( !LookupAttr(row
, col
, &attr
) )
8613 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8614 : (wxGridCellAttr
*)NULL
;
8615 CacheAttr(row
, col
, attr
);
8620 attr
->SetDefAttr(m_defaultCellAttr
);
8624 attr
= m_defaultCellAttr
;
8631 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8633 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8635 wxCHECK_MSG( m_table
, attr
,
8636 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8638 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8641 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8643 // artificially inc the ref count to match DecRef() in caller
8645 m_table
->SetAttr(attr
, row
, col
);
8651 // ----------------------------------------------------------------------------
8652 // setting column attributes (wrappers around SetColAttr)
8653 // ----------------------------------------------------------------------------
8655 void wxGrid::SetColFormatBool(int col
)
8657 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8660 void wxGrid::SetColFormatNumber(int col
)
8662 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8665 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8667 wxString typeName
= wxGRID_VALUE_FLOAT
;
8668 if ( (width
!= -1) || (precision
!= -1) )
8670 typeName
<< _T(':') << width
<< _T(',') << precision
;
8673 SetColFormatCustom(col
, typeName
);
8676 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8678 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8680 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8682 attr
= new wxGridCellAttr
;
8683 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8684 attr
->SetRenderer(renderer
);
8686 SetColAttr(col
, attr
);
8690 // ----------------------------------------------------------------------------
8691 // setting cell attributes: this is forwarded to the table
8692 // ----------------------------------------------------------------------------
8694 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8696 if ( CanHaveAttributes() )
8698 m_table
->SetAttr(attr
, row
, col
);
8707 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8709 if ( CanHaveAttributes() )
8711 m_table
->SetRowAttr(attr
, row
);
8720 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8722 if ( CanHaveAttributes() )
8724 m_table
->SetColAttr(attr
, col
);
8733 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8735 if ( CanHaveAttributes() )
8737 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8738 attr
->SetBackgroundColour(colour
);
8743 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8745 if ( CanHaveAttributes() )
8747 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8748 attr
->SetTextColour(colour
);
8753 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8755 if ( CanHaveAttributes() )
8757 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8758 attr
->SetFont(font
);
8763 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8765 if ( CanHaveAttributes() )
8767 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8768 attr
->SetAlignment(horiz
, vert
);
8773 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8775 if ( CanHaveAttributes() )
8777 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8778 attr
->SetOverflow(allow
);
8783 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8785 if ( CanHaveAttributes() )
8787 int cell_rows
, cell_cols
;
8789 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8790 attr
->GetSize(&cell_rows
, &cell_cols
);
8791 attr
->SetSize(num_rows
, num_cols
);
8794 // Cannot set the size of a cell to 0 or negative values
8795 // While it is perfectly legal to do that, this function cannot
8796 // handle all the possibilies, do it by hand by getting the CellAttr.
8797 // You can only set the size of a cell to 1,1 or greater with this fn
8798 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8799 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8800 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8801 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8803 // if this was already a multicell then "turn off" the other cells first
8804 if ((cell_rows
> 1) || (cell_rows
> 1))
8807 for (j
=row
; j
<row
+cell_rows
; j
++)
8809 for (i
=col
; i
<col
+cell_cols
; i
++)
8811 if ((i
!= col
) || (j
!= row
))
8813 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8814 attr_stub
->SetSize( 1, 1 );
8815 attr_stub
->DecRef();
8821 // mark the cells that will be covered by this cell to
8822 // negative or zero values to point back at this cell
8823 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8826 for (j
=row
; j
<row
+num_rows
; j
++)
8828 for (i
=col
; i
<col
+num_cols
; i
++)
8830 if ((i
!= col
) || (j
!= row
))
8832 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8833 attr_stub
->SetSize( row
-j
, col
-i
);
8834 attr_stub
->DecRef();
8842 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8844 if ( CanHaveAttributes() )
8846 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8847 attr
->SetRenderer(renderer
);
8852 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8854 if ( CanHaveAttributes() )
8856 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8857 attr
->SetEditor(editor
);
8862 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8864 if ( CanHaveAttributes() )
8866 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8867 attr
->SetReadOnly(isReadOnly
);
8872 // ----------------------------------------------------------------------------
8873 // Data type registration
8874 // ----------------------------------------------------------------------------
8876 void wxGrid::RegisterDataType(const wxString
& typeName
,
8877 wxGridCellRenderer
* renderer
,
8878 wxGridCellEditor
* editor
)
8880 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8884 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8886 wxString typeName
= m_table
->GetTypeName(row
, col
);
8887 return GetDefaultEditorForType(typeName
);
8890 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8892 wxString typeName
= m_table
->GetTypeName(row
, col
);
8893 return GetDefaultRendererForType(typeName
);
8897 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8899 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8900 if ( index
== wxNOT_FOUND
)
8902 wxFAIL_MSG(wxT("Unknown data type name"));
8907 return m_typeRegistry
->GetEditor(index
);
8911 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8913 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8914 if ( index
== wxNOT_FOUND
)
8916 wxFAIL_MSG(wxT("Unknown data type name"));
8921 return m_typeRegistry
->GetRenderer(index
);
8925 // ----------------------------------------------------------------------------
8927 // ----------------------------------------------------------------------------
8929 void wxGrid::EnableDragRowSize( bool enable
)
8931 m_canDragRowSize
= enable
;
8935 void wxGrid::EnableDragColSize( bool enable
)
8937 m_canDragColSize
= enable
;
8940 void wxGrid::EnableDragGridSize( bool enable
)
8942 m_canDragGridSize
= enable
;
8946 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8948 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8950 if ( resizeExistingRows
)
8952 // since we are resizing all rows to the default row size,
8953 // we can simply clear the row heights and row bottoms
8954 // arrays (which also allows us to take advantage of
8955 // some speed optimisations)
8956 m_rowHeights
.Empty();
8957 m_rowBottoms
.Empty();
8958 if ( !GetBatchCount() )
8963 void wxGrid::SetRowSize( int row
, int height
)
8965 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8967 if ( m_rowHeights
.IsEmpty() )
8969 // need to really create the array
8973 int h
= wxMax( 0, height
);
8974 int diff
= h
- m_rowHeights
[row
];
8976 m_rowHeights
[row
] = h
;
8978 for ( i
= row
; i
< m_numRows
; i
++ )
8980 m_rowBottoms
[i
] += diff
;
8982 if ( !GetBatchCount() )
8986 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8988 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8990 if ( resizeExistingCols
)
8992 // since we are resizing all columns to the default column size,
8993 // we can simply clear the col widths and col rights
8994 // arrays (which also allows us to take advantage of
8995 // some speed optimisations)
8996 m_colWidths
.Empty();
8997 m_colRights
.Empty();
8998 if ( !GetBatchCount() )
9003 void wxGrid::SetColSize( int col
, int width
)
9005 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9007 // should we check that it's bigger than GetColMinimalWidth(col) here?
9009 if ( m_colWidths
.IsEmpty() )
9011 // need to really create the array
9015 int w
= wxMax( 0, width
);
9016 int diff
= w
- m_colWidths
[col
];
9017 m_colWidths
[col
] = w
;
9020 for ( i
= col
; i
< m_numCols
; i
++ )
9022 m_colRights
[i
] += diff
;
9024 if ( !GetBatchCount() )
9029 void wxGrid::SetColMinimalWidth( int col
, int width
)
9031 m_colMinWidths
.Put(col
, width
);
9034 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9036 m_rowMinHeights
.Put(row
, width
);
9039 int wxGrid::GetColMinimalWidth(int col
) const
9041 long value
= m_colMinWidths
.Get(col
);
9042 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9045 int wxGrid::GetRowMinimalHeight(int row
) const
9047 long value
= m_rowMinHeights
.Get(row
);
9048 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9051 // ----------------------------------------------------------------------------
9053 // ----------------------------------------------------------------------------
9055 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9057 wxClientDC
dc(m_gridWin
);
9059 // init both of them to avoid compiler warnings, even if weo nly need one
9067 wxCoord extent
, extentMax
= 0;
9068 int max
= column
? m_numRows
: m_numCols
;
9069 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9076 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9077 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9080 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9081 extent
= column
? size
.x
: size
.y
;
9082 if ( extent
> extentMax
)
9093 // now also compare with the column label extent
9095 dc
.SetFont( GetLabelFont() );
9098 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9100 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9102 extent
= column
? w
: h
;
9103 if ( extent
> extentMax
)
9110 // empty column - give default extent (notice that if extentMax is less
9111 // than default extent but != 0, it's ok)
9112 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9118 // leave some space around text
9129 SetColSize(col
, extentMax
);
9130 if ( !GetBatchCount() )
9133 m_gridWin
->GetClientSize( &cw
, &ch
);
9134 wxRect
rect ( CellToRect( 0, col
) );
9136 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9137 rect
.width
= cw
- rect
.x
;
9138 rect
.height
= m_colLabelHeight
;
9139 m_colLabelWin
->Refresh( TRUE
, &rect
);
9144 SetRowSize(row
, extentMax
);
9145 if ( !GetBatchCount() )
9148 m_gridWin
->GetClientSize( &cw
, &ch
);
9149 wxRect
rect ( CellToRect( row
, 0 ) );
9151 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9152 rect
.width
= m_rowLabelWidth
;
9153 rect
.height
= ch
- rect
.y
;
9154 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9160 SetColMinimalWidth(col
, extentMax
);
9162 SetRowMinimalHeight(row
, extentMax
);
9166 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9168 int width
= m_rowLabelWidth
;
9173 for ( int col
= 0; col
< m_numCols
; col
++ )
9177 AutoSizeColumn(col
, setAsMin
);
9180 width
+= GetColWidth(col
);
9189 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9191 int height
= m_colLabelHeight
;
9196 for ( int row
= 0; row
< m_numRows
; row
++ )
9200 AutoSizeRow(row
, setAsMin
);
9203 height
+= GetRowHeight(row
);
9212 void wxGrid::AutoSize()
9216 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9218 // round up the size to a multiple of scroll step - this ensures that we
9219 // won't get the scrollbars if we're sized exactly to this width
9220 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
9221 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
9223 // distribute the extra space between teh columns/rows to avoid having
9224 // extra white space
9225 wxCoord diff
= sizeFit
.x
- size
.x
;
9228 // try to resize the columns uniformly
9229 wxCoord diffPerCol
= diff
/ m_numCols
;
9232 for ( int col
= 0; col
< m_numCols
; col
++ )
9234 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9238 // add remaining amount to the last columns
9239 diff
-= diffPerCol
* m_numCols
;
9242 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9244 SetColSize(col
, GetColWidth(col
) + 1);
9250 diff
= sizeFit
.y
- size
.y
;
9253 // try to resize the columns uniformly
9254 wxCoord diffPerRow
= diff
/ m_numRows
;
9257 for ( int row
= 0; row
< m_numRows
; row
++ )
9259 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9263 // add remaining amount to the last rows
9264 diff
-= diffPerRow
* m_numRows
;
9267 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9269 SetRowSize(row
, GetRowHeight(row
) + 1);
9276 SetClientSize(sizeFit
);
9279 wxSize
wxGrid::DoGetBestSize() const
9281 // don't set sizes, only calculate them
9282 wxGrid
*self
= (wxGrid
*)this; // const_cast
9285 width
= self
->SetOrCalcColumnSizes(TRUE
);
9286 height
= self
->SetOrCalcRowSizes(TRUE
);
9288 int maxwidth
, maxheight
;
9289 wxDisplaySize( & maxwidth
, & maxheight
);
9291 if ( width
> maxwidth
) width
= maxwidth
;
9292 if ( height
> maxheight
) height
= maxheight
;
9294 return wxSize( width
, height
);
9303 wxPen
& wxGrid::GetDividerPen() const
9308 // ----------------------------------------------------------------------------
9309 // cell value accessor functions
9310 // ----------------------------------------------------------------------------
9312 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9316 m_table
->SetValue( row
, col
, s
);
9317 if ( !GetBatchCount() )
9320 wxRect
rect( CellToRect( row
, col
) );
9322 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9323 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9324 m_gridWin
->Refresh( FALSE
, &rect
);
9327 if ( m_currentCellCoords
.GetRow() == row
&&
9328 m_currentCellCoords
.GetCol() == col
&&
9329 IsCellEditControlShown())
9330 // Note: If we are using IsCellEditControlEnabled,
9331 // this interacts badly with calling SetCellValue from
9332 // an EVT_GRID_CELL_CHANGE handler.
9334 HideCellEditControl();
9335 ShowCellEditControl(); // will reread data from table
9342 // ------ Block, row and col selection
9345 void wxGrid::SelectRow( int row
, bool addToSelected
)
9347 if ( IsSelection() && !addToSelected
)
9351 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9355 void wxGrid::SelectCol( int col
, bool addToSelected
)
9357 if ( IsSelection() && !addToSelected
)
9361 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9365 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9366 bool addToSelected
)
9368 if ( IsSelection() && !addToSelected
)
9372 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9373 FALSE
, addToSelected
);
9377 void wxGrid::SelectAll()
9379 if ( m_numRows
> 0 && m_numCols
> 0 )
9382 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9387 // ------ Cell, row and col deselection
9390 void wxGrid::DeselectRow( int row
)
9395 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9397 if ( m_selection
->IsInSelection(row
, 0 ) )
9398 m_selection
->ToggleCellSelection( row
, 0);
9402 int nCols
= GetNumberCols();
9403 for ( int i
= 0; i
< nCols
; i
++ )
9405 if ( m_selection
->IsInSelection(row
, i
) )
9406 m_selection
->ToggleCellSelection( row
, i
);
9411 void wxGrid::DeselectCol( int col
)
9416 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9418 if ( m_selection
->IsInSelection(0, col
) )
9419 m_selection
->ToggleCellSelection( 0, col
);
9423 int nRows
= GetNumberRows();
9424 for ( int i
= 0; i
< nRows
; i
++ )
9426 if ( m_selection
->IsInSelection(i
, col
) )
9427 m_selection
->ToggleCellSelection(i
, col
);
9432 void wxGrid::DeselectCell( int row
, int col
)
9434 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9435 m_selection
->ToggleCellSelection(row
, col
);
9438 bool wxGrid::IsSelection()
9440 return ( m_selection
&& (m_selection
->IsSelection() ||
9441 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9442 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9445 bool wxGrid::IsInSelection( int row
, int col
) const
9447 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9448 ( row
>= m_selectingTopLeft
.GetRow() &&
9449 col
>= m_selectingTopLeft
.GetCol() &&
9450 row
<= m_selectingBottomRight
.GetRow() &&
9451 col
<= m_selectingBottomRight
.GetCol() )) );
9454 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9456 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9457 return m_selection
->m_cellSelection
;
9459 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9461 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9462 return m_selection
->m_blockSelectionTopLeft
;
9464 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9466 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9467 return m_selection
->m_blockSelectionBottomRight
;
9469 wxArrayInt
wxGrid::GetSelectedRows() const
9471 if (!m_selection
) { wxArrayInt a
; return a
; }
9472 return m_selection
->m_rowSelection
;
9474 wxArrayInt
wxGrid::GetSelectedCols() const
9476 if (!m_selection
) { wxArrayInt a
; return a
; }
9477 return m_selection
->m_colSelection
;
9481 void wxGrid::ClearSelection()
9483 m_selectingTopLeft
= wxGridNoCellCoords
;
9484 m_selectingBottomRight
= wxGridNoCellCoords
;
9486 m_selection
->ClearSelection();
9490 // This function returns the rectangle that encloses the given block
9491 // in device coords clipped to the client size of the grid window.
9493 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9494 const wxGridCellCoords
&bottomRight
)
9496 wxRect
rect( wxGridNoCellRect
);
9499 cellRect
= CellToRect( topLeft
);
9500 if ( cellRect
!= wxGridNoCellRect
)
9506 rect
= wxRect( 0, 0, 0, 0 );
9509 cellRect
= CellToRect( bottomRight
);
9510 if ( cellRect
!= wxGridNoCellRect
)
9516 return wxGridNoCellRect
;
9520 int left
= rect
.GetLeft();
9521 int top
= rect
.GetTop();
9522 int right
= rect
.GetRight();
9523 int bottom
= rect
.GetBottom();
9525 int leftCol
= topLeft
.GetCol();
9526 int topRow
= topLeft
.GetRow();
9527 int rightCol
= bottomRight
.GetCol();
9528 int bottomRow
= bottomRight
.GetRow();
9551 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9553 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9555 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9557 cellRect
= CellToRect( j
, i
);
9559 if (cellRect
.x
< left
)
9561 if (cellRect
.y
< top
)
9563 if (cellRect
.x
+ cellRect
.width
> right
)
9564 right
= cellRect
.x
+ cellRect
.width
;
9565 if (cellRect
.y
+ cellRect
.height
> bottom
)
9566 bottom
= cellRect
.y
+ cellRect
.height
;
9568 else i
= rightCol
; // jump over inner cells.
9572 // convert to scrolled coords
9574 CalcScrolledPosition( left
, top
, &left
, &top
);
9575 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9578 m_gridWin
->GetClientSize( &cw
, &ch
);
9580 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9581 return wxRect( 0, 0, 0, 0);
9583 rect
.SetLeft( wxMax(0, left
) );
9584 rect
.SetTop( wxMax(0, top
) );
9585 rect
.SetRight( wxMin(cw
, right
) );
9586 rect
.SetBottom( wxMin(ch
, bottom
) );
9594 // ------ Grid event classes
9597 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9599 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9600 int row
, int col
, int x
, int y
, bool sel
,
9601 bool control
, bool shift
, bool alt
, bool meta
)
9602 : wxNotifyEvent( type
, id
)
9609 m_control
= control
;
9614 SetEventObject(obj
);
9618 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9620 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9621 int rowOrCol
, int x
, int y
,
9622 bool control
, bool shift
, bool alt
, bool meta
)
9623 : wxNotifyEvent( type
, id
)
9625 m_rowOrCol
= rowOrCol
;
9628 m_control
= control
;
9633 SetEventObject(obj
);
9637 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9639 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9640 const wxGridCellCoords
& topLeft
,
9641 const wxGridCellCoords
& bottomRight
,
9642 bool sel
, bool control
,
9643 bool shift
, bool alt
, bool meta
)
9644 : wxNotifyEvent( type
, id
)
9646 m_topLeft
= topLeft
;
9647 m_bottomRight
= bottomRight
;
9649 m_control
= control
;
9654 SetEventObject(obj
);
9658 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9660 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9661 wxObject
* obj
, int row
,
9662 int col
, wxControl
* ctrl
)
9663 : wxCommandEvent(type
, id
)
9665 SetEventObject(obj
);
9672 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9674 #endif // wxUSE_GRID