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
;
1561 // erase only this cells background, overflow cells should have been erased
1562 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1565 attr
.GetAlignment(&hAlign
, &vAlign
);
1567 int overflowCols
= 0;
1569 if (attr
.GetOverflow())
1571 int cols
= grid
.GetNumberCols();
1572 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1573 int cell_rows
, cell_cols
;
1574 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1575 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1577 int i
, c_cols
, c_rows
;
1578 for (i
= col
+cell_cols
; i
< cols
; i
++)
1580 bool is_empty
= TRUE
;
1581 for (int j
=row
; j
<row
+cell_rows
; j
++)
1583 // check w/ anchor cell for multicell block
1584 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1585 if (c_rows
> 0) c_rows
= 0;
1586 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1593 rect
.width
+= grid
.GetColSize(i
);
1599 if (rect
.width
>= best_width
) break;
1601 overflowCols
= i
- col
- cell_cols
+ 1;
1602 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1605 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1607 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1609 clip
.x
+= rectCell
.width
;
1610 // draw each overflow cell individually
1611 int col_end
= col
+cell_cols
+overflowCols
;
1612 if (col_end
>= grid
.GetNumberCols())
1613 col_end
= grid
.GetNumberCols() - 1;
1614 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1616 clip
.width
= grid
.GetColSize(i
) - 1;
1617 dc
.DestroyClippingRegion();
1618 dc
.SetClippingRegion(clip
);
1620 SetTextColoursAndFont(grid
, attr
, dc
,
1621 grid
.IsInSelection(row
,i
));
1623 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1624 rect
, hAlign
, vAlign
);
1625 clip
.x
+= grid
.GetColSize(i
) - 1;
1631 dc
.DestroyClippingRegion();
1635 // now we only have to draw the text
1636 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1638 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1639 rect
, hAlign
, vAlign
);
1642 // ----------------------------------------------------------------------------
1643 // wxGridCellNumberRenderer
1644 // ----------------------------------------------------------------------------
1646 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1648 wxGridTableBase
*table
= grid
.GetTable();
1650 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1652 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1656 text
= table
->GetValue(row
, col
);
1662 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1663 wxGridCellAttr
& attr
,
1665 const wxRect
& rectCell
,
1669 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1671 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1673 // draw the text right aligned by default
1675 attr
.GetAlignment(&hAlign
, &vAlign
);
1676 hAlign
= wxALIGN_RIGHT
;
1678 wxRect rect
= rectCell
;
1681 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1684 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1685 wxGridCellAttr
& attr
,
1689 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1692 // ----------------------------------------------------------------------------
1693 // wxGridCellFloatRenderer
1694 // ----------------------------------------------------------------------------
1696 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1699 SetPrecision(precision
);
1702 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1704 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1705 renderer
->m_width
= m_width
;
1706 renderer
->m_precision
= m_precision
;
1707 renderer
->m_format
= m_format
;
1712 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1714 wxGridTableBase
*table
= grid
.GetTable();
1719 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1721 val
= table
->GetValueAsDouble(row
, col
);
1726 text
= table
->GetValue(row
, col
);
1727 hasDouble
= text
.ToDouble(&val
);
1734 if ( m_width
== -1 )
1736 if ( m_precision
== -1 )
1738 // default width/precision
1739 m_format
= _T("%f");
1743 m_format
.Printf(_T("%%.%df"), m_precision
);
1746 else if ( m_precision
== -1 )
1748 // default precision
1749 m_format
.Printf(_T("%%%d.f"), m_width
);
1753 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1757 text
.Printf(m_format
, val
);
1760 //else: text already contains the string
1765 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1766 wxGridCellAttr
& attr
,
1768 const wxRect
& rectCell
,
1772 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1774 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1776 // draw the text right aligned by default
1778 attr
.GetAlignment(&hAlign
, &vAlign
);
1779 hAlign
= wxALIGN_RIGHT
;
1781 wxRect rect
= rectCell
;
1784 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1787 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1788 wxGridCellAttr
& attr
,
1792 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1795 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1799 // reset to defaults
1805 wxString tmp
= params
.BeforeFirst(_T(','));
1809 if ( tmp
.ToLong(&width
) )
1811 SetWidth((int)width
);
1815 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1819 tmp
= params
.AfterFirst(_T(','));
1823 if ( tmp
.ToLong(&precision
) )
1825 SetPrecision((int)precision
);
1829 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1837 // ----------------------------------------------------------------------------
1838 // wxGridCellBoolRenderer
1839 // ----------------------------------------------------------------------------
1841 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1843 // FIXME these checkbox size calculations are really ugly...
1845 // between checkmark and box
1846 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1848 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1849 wxGridCellAttr
& WXUNUSED(attr
),
1854 // compute it only once (no locks for MT safeness in GUI thread...)
1855 if ( !ms_sizeCheckMark
.x
)
1857 // get checkbox size
1858 wxCoord checkSize
= 0;
1859 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1860 wxSize size
= checkbox
->GetBestSize();
1861 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1863 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1864 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1865 checkSize
-= size
.y
/ 2;
1870 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1873 return ms_sizeCheckMark
;
1876 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1877 wxGridCellAttr
& attr
,
1883 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1885 // draw a check mark in the centre (ignoring alignment - TODO)
1886 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1888 // don't draw outside the cell
1889 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1890 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1892 // and even leave (at least) 1 pixel margin
1893 size
.x
= size
.y
= minSize
- 2;
1896 // draw a border around checkmark
1898 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1899 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1900 rectBorder
.width
= size
.x
;
1901 rectBorder
.height
= size
.y
;
1904 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1905 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1908 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1909 value
= !( !cellval
|| (cellval
== wxT("0")) );
1914 wxRect rectMark
= rectBorder
;
1916 // MSW DrawCheckMark() is weird (and should probably be changed...)
1917 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1921 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1924 dc
.SetTextForeground(attr
.GetTextColour());
1925 dc
.DrawCheckMark(rectMark
);
1928 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1929 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1930 dc
.DrawRectangle(rectBorder
);
1933 // ----------------------------------------------------------------------------
1935 // ----------------------------------------------------------------------------
1937 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1941 m_isReadOnly
= Unset
;
1946 m_attrkind
= wxGridCellAttr::Cell
;
1948 m_sizeRows
= m_sizeCols
= 1;
1951 SetDefAttr(attrDefault
);
1954 wxGridCellAttr
*wxGridCellAttr::Clone() const
1956 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1958 if ( HasTextColour() )
1959 attr
->SetTextColour(GetTextColour());
1960 if ( HasBackgroundColour() )
1961 attr
->SetBackgroundColour(GetBackgroundColour());
1963 attr
->SetFont(GetFont());
1964 if ( HasAlignment() )
1965 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1967 attr
->SetSize( m_sizeRows
, m_sizeCols
);
1971 attr
->SetRenderer(m_renderer
);
1972 m_renderer
->IncRef();
1976 attr
->SetEditor(m_editor
);
1981 attr
->SetReadOnly();
1983 attr
->SetKind( m_attrkind
);
1988 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1990 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1991 SetTextColour(mergefrom
->GetTextColour());
1992 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1993 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1994 if ( !HasFont() && mergefrom
->HasFont() )
1995 SetFont(mergefrom
->GetFont());
1996 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
1998 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1999 SetAlignment(hAlign
, vAlign
);
2002 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2004 // Directly access member functions as GetRender/Editor don't just return
2005 // m_renderer/m_editor
2007 // Maybe add support for merge of Render and Editor?
2008 if (!HasRenderer() && mergefrom
->HasRenderer() )
2010 m_renderer
= mergefrom
->m_renderer
;
2011 m_renderer
->IncRef();
2013 if ( !HasEditor() && mergefrom
->HasEditor() )
2015 m_editor
= mergefrom
->m_editor
;
2018 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2019 SetReadOnly(mergefrom
->IsReadOnly());
2021 SetDefAttr(mergefrom
->m_defGridAttr
);
2024 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2026 // The size of a cell is normally 1,1
2028 // If this cell is larger (2,2) then this is the top left cell
2029 // the other cells that will be covered (lower right cells) must be
2030 // set to negative or zero values such that
2031 // row + num_rows of the covered cell points to the larger cell (this cell)
2032 // same goes for the col + num_cols.
2034 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2036 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2037 !((num_rows
<=0)&&(num_cols
>0)) ||
2038 !((num_rows
==0)&&(num_cols
==0))),
2039 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2041 m_sizeRows
= num_rows
;
2042 m_sizeCols
= num_cols
;
2045 const wxColour
& wxGridCellAttr::GetTextColour() const
2047 if (HasTextColour())
2051 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2053 return m_defGridAttr
->GetTextColour();
2057 wxFAIL_MSG(wxT("Missing default cell attribute"));
2058 return wxNullColour
;
2063 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2065 if (HasBackgroundColour())
2067 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2068 return m_defGridAttr
->GetBackgroundColour();
2071 wxFAIL_MSG(wxT("Missing default cell attribute"));
2072 return wxNullColour
;
2077 const wxFont
& wxGridCellAttr::GetFont() const
2081 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2082 return m_defGridAttr
->GetFont();
2085 wxFAIL_MSG(wxT("Missing default cell attribute"));
2091 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2095 if ( hAlign
) *hAlign
= m_hAlign
;
2096 if ( vAlign
) *vAlign
= m_vAlign
;
2098 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2099 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2102 wxFAIL_MSG(wxT("Missing default cell attribute"));
2106 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2108 if ( num_rows
) *num_rows
= m_sizeRows
;
2109 if ( num_cols
) *num_cols
= m_sizeCols
;
2112 // GetRenderer and GetEditor use a slightly different decision path about
2113 // which attribute to use. If a non-default attr object has one then it is
2114 // used, otherwise the default editor or renderer is fetched from the grid and
2115 // used. It should be the default for the data type of the cell. If it is
2116 // NULL (because the table has a type that the grid does not have in its
2117 // registry,) then the grid's default editor or renderer is used.
2119 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2121 wxGridCellRenderer
*renderer
;
2123 if ( m_renderer
&& this != m_defGridAttr
)
2125 // use the cells renderer if it has one
2126 renderer
= m_renderer
;
2129 else // no non default cell renderer
2131 // get default renderer for the data type
2134 // GetDefaultRendererForCell() will do IncRef() for us
2135 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2144 if (m_defGridAttr
&& this != m_defGridAttr
)
2146 // if we still don't have one then use the grid default
2147 // (no need for IncRef() here neither)
2148 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2150 else // default grid attr
2152 // use m_renderer which we had decided not to use initially
2153 renderer
= m_renderer
;
2160 // we're supposed to always find something
2161 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2166 // same as above, except for s/renderer/editor/g
2167 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2169 wxGridCellEditor
*editor
;
2171 if ( m_editor
&& this != m_defGridAttr
)
2173 // use the cells editor if it has one
2177 else // no non default cell editor
2179 // get default editor for the data type
2182 // GetDefaultEditorForCell() will do IncRef() for us
2183 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2192 if ( m_defGridAttr
&& this != m_defGridAttr
)
2194 // if we still don't have one then use the grid default
2195 // (no need for IncRef() here neither)
2196 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2198 else // default grid attr
2200 // use m_editor which we had decided not to use initially
2208 // we're supposed to always find something
2209 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2214 // ----------------------------------------------------------------------------
2215 // wxGridCellAttrData
2216 // ----------------------------------------------------------------------------
2218 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2220 int n
= FindIndex(row
, col
);
2221 if ( n
== wxNOT_FOUND
)
2223 // add the attribute
2224 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2228 // free the old attribute
2229 m_attrs
[(size_t)n
].attr
->DecRef();
2233 // change the attribute
2234 m_attrs
[(size_t)n
].attr
= attr
;
2238 // remove this attribute
2239 m_attrs
.RemoveAt((size_t)n
);
2244 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2246 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2248 int n
= FindIndex(row
, col
);
2249 if ( n
!= wxNOT_FOUND
)
2251 attr
= m_attrs
[(size_t)n
].attr
;
2258 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2260 size_t count
= m_attrs
.GetCount();
2261 for ( size_t n
= 0; n
< count
; n
++ )
2263 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2264 wxCoord row
= coords
.GetRow();
2265 if ((size_t)row
>= pos
)
2269 // If rows inserted, include row counter where necessary
2270 coords
.SetRow(row
+ numRows
);
2272 else if (numRows
< 0)
2274 // If rows deleted ...
2275 if ((size_t)row
>= pos
- numRows
)
2277 // ...either decrement row counter (if row still exists)...
2278 coords
.SetRow(row
+ numRows
);
2282 // ...or remove the attribute
2283 m_attrs
.RemoveAt((size_t)n
);
2291 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2293 size_t count
= m_attrs
.GetCount();
2294 for ( size_t n
= 0; n
< count
; n
++ )
2296 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2297 wxCoord col
= coords
.GetCol();
2298 if ( (size_t)col
>= pos
)
2302 // If rows inserted, include row counter where necessary
2303 coords
.SetCol(col
+ numCols
);
2305 else if (numCols
< 0)
2307 // If rows deleted ...
2308 if ((size_t)col
>= pos
- numCols
)
2310 // ...either decrement row counter (if row still exists)...
2311 coords
.SetCol(col
+ numCols
);
2315 // ...or remove the attribute
2316 m_attrs
.RemoveAt((size_t)n
);
2324 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2326 size_t count
= m_attrs
.GetCount();
2327 for ( size_t n
= 0; n
< count
; n
++ )
2329 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2330 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2339 // ----------------------------------------------------------------------------
2340 // wxGridRowOrColAttrData
2341 // ----------------------------------------------------------------------------
2343 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2345 size_t count
= m_attrs
.Count();
2346 for ( size_t n
= 0; n
< count
; n
++ )
2348 m_attrs
[n
]->DecRef();
2352 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2354 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2356 int n
= m_rowsOrCols
.Index(rowOrCol
);
2357 if ( n
!= wxNOT_FOUND
)
2359 attr
= m_attrs
[(size_t)n
];
2366 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2368 int i
= m_rowsOrCols
.Index(rowOrCol
);
2369 if ( i
== wxNOT_FOUND
)
2371 // add the attribute
2372 m_rowsOrCols
.Add(rowOrCol
);
2377 size_t n
= (size_t)i
;
2380 // change the attribute
2381 m_attrs
[n
]->DecRef();
2386 // remove this attribute
2387 m_attrs
[n
]->DecRef();
2388 m_rowsOrCols
.RemoveAt(n
);
2389 m_attrs
.RemoveAt(n
);
2394 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2396 size_t count
= m_attrs
.GetCount();
2397 for ( size_t n
= 0; n
< count
; n
++ )
2399 int & rowOrCol
= m_rowsOrCols
[n
];
2400 if ( (size_t)rowOrCol
>= pos
)
2402 if ( numRowsOrCols
> 0 )
2404 // If rows inserted, include row counter where necessary
2405 rowOrCol
+= numRowsOrCols
;
2407 else if ( numRowsOrCols
< 0)
2409 // If rows deleted, either decrement row counter (if row still exists)
2410 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2411 rowOrCol
+= numRowsOrCols
;
2414 m_rowsOrCols
.RemoveAt((size_t)n
);
2415 m_attrs
.RemoveAt((size_t)n
);
2423 // ----------------------------------------------------------------------------
2424 // wxGridCellAttrProvider
2425 // ----------------------------------------------------------------------------
2427 wxGridCellAttrProvider::wxGridCellAttrProvider()
2429 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2432 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2437 void wxGridCellAttrProvider::InitData()
2439 m_data
= new wxGridCellAttrProviderData
;
2442 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2443 wxGridCellAttr::wxAttrKind kind
) const
2445 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2450 case (wxGridCellAttr::Any
):
2451 //Get cached merge attributes.
2452 // Currenlty not used as no cache implemented as not mutiable
2453 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2456 //Basicaly implement old version.
2457 //Also check merge cache, so we don't have to re-merge every time..
2458 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2459 *attrrow
= (wxGridCellAttr
*)NULL
,
2460 *attrcol
= (wxGridCellAttr
*)NULL
;
2462 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2463 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2464 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2466 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2467 // Two or move are non NULL
2468 attr
= new wxGridCellAttr
;
2469 attr
->SetKind(wxGridCellAttr::Merged
);
2473 attr
->MergeWith(attrcell
);
2477 attr
->MergeWith(attrcol
);
2481 attr
->MergeWith(attrrow
);
2484 //store merge attr if cache implemented
2486 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2490 // one or none is non null return it or null.
2491 if(attrrow
) attr
= attrrow
;
2492 if(attrcol
) attr
= attrcol
;
2493 if(attrcell
) attr
= attrcell
;
2497 case (wxGridCellAttr::Cell
):
2498 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2500 case (wxGridCellAttr::Col
):
2501 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2503 case (wxGridCellAttr::Row
):
2504 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2508 // (wxGridCellAttr::Default):
2509 // (wxGridCellAttr::Merged):
2516 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2522 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2525 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2530 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2533 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2538 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2541 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2545 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2547 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2551 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2555 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2557 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2561 // ----------------------------------------------------------------------------
2562 // wxGridTypeRegistry
2563 // ----------------------------------------------------------------------------
2565 wxGridTypeRegistry::~wxGridTypeRegistry()
2567 size_t count
= m_typeinfo
.Count();
2568 for ( size_t i
= 0; i
< count
; i
++ )
2569 delete m_typeinfo
[i
];
2573 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2574 wxGridCellRenderer
* renderer
,
2575 wxGridCellEditor
* editor
)
2577 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2579 // is it already registered?
2580 int loc
= FindRegisteredDataType(typeName
);
2581 if ( loc
!= wxNOT_FOUND
)
2583 delete m_typeinfo
[loc
];
2584 m_typeinfo
[loc
] = info
;
2588 m_typeinfo
.Add(info
);
2592 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2594 size_t count
= m_typeinfo
.GetCount();
2595 for ( size_t i
= 0; i
< count
; i
++ )
2597 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2606 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2608 int index
= FindRegisteredDataType(typeName
);
2609 if ( index
== wxNOT_FOUND
)
2611 // check whether this is one of the standard ones, in which case
2612 // register it "on the fly"
2614 if ( typeName
== wxGRID_VALUE_STRING
)
2616 RegisterDataType(wxGRID_VALUE_STRING
,
2617 new wxGridCellStringRenderer
,
2618 new wxGridCellTextEditor
);
2620 #endif // wxUSE_TEXTCTRL
2622 if ( typeName
== wxGRID_VALUE_BOOL
)
2624 RegisterDataType(wxGRID_VALUE_BOOL
,
2625 new wxGridCellBoolRenderer
,
2626 new wxGridCellBoolEditor
);
2628 #endif // wxUSE_CHECKBOX
2630 if ( typeName
== wxGRID_VALUE_NUMBER
)
2632 RegisterDataType(wxGRID_VALUE_NUMBER
,
2633 new wxGridCellNumberRenderer
,
2634 new wxGridCellNumberEditor
);
2636 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2638 RegisterDataType(wxGRID_VALUE_FLOAT
,
2639 new wxGridCellFloatRenderer
,
2640 new wxGridCellFloatEditor
);
2642 #endif // wxUSE_TEXTCTRL
2644 if ( typeName
== wxGRID_VALUE_CHOICE
)
2646 RegisterDataType(wxGRID_VALUE_CHOICE
,
2647 new wxGridCellStringRenderer
,
2648 new wxGridCellChoiceEditor
);
2650 #endif // wxUSE_COMBOBOX
2655 // we get here only if just added the entry for this type, so return
2657 index
= m_typeinfo
.GetCount() - 1;
2663 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2665 int index
= FindDataType(typeName
);
2666 if ( index
== wxNOT_FOUND
)
2668 // the first part of the typename is the "real" type, anything after ':'
2669 // are the parameters for the renderer
2670 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2671 if ( index
== wxNOT_FOUND
)
2676 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2677 wxGridCellRenderer
*rendererOld
= renderer
;
2678 renderer
= renderer
->Clone();
2679 rendererOld
->DecRef();
2681 wxGridCellEditor
*editor
= GetEditor(index
);
2682 wxGridCellEditor
*editorOld
= editor
;
2683 editor
= editor
->Clone();
2684 editorOld
->DecRef();
2686 // do it even if there are no parameters to reset them to defaults
2687 wxString params
= typeName
.AfterFirst(_T(':'));
2688 renderer
->SetParameters(params
);
2689 editor
->SetParameters(params
);
2691 // register the new typename
2692 RegisterDataType(typeName
, renderer
, editor
);
2694 // we just registered it, it's the last one
2695 index
= m_typeinfo
.GetCount() - 1;
2701 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2703 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2709 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2711 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2717 // ----------------------------------------------------------------------------
2719 // ----------------------------------------------------------------------------
2721 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2724 wxGridTableBase::wxGridTableBase()
2726 m_view
= (wxGrid
*) NULL
;
2727 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2730 wxGridTableBase::~wxGridTableBase()
2732 delete m_attrProvider
;
2735 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2737 delete m_attrProvider
;
2738 m_attrProvider
= attrProvider
;
2741 bool wxGridTableBase::CanHaveAttributes()
2743 if ( ! GetAttrProvider() )
2745 // use the default attr provider by default
2746 SetAttrProvider(new wxGridCellAttrProvider
);
2751 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2753 if ( m_attrProvider
)
2754 return m_attrProvider
->GetAttr(row
, col
, kind
);
2756 return (wxGridCellAttr
*)NULL
;
2759 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2761 if ( m_attrProvider
)
2763 attr
->SetKind(wxGridCellAttr::Cell
);
2764 m_attrProvider
->SetAttr(attr
, row
, col
);
2768 // as we take ownership of the pointer and don't store it, we must
2774 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2776 if ( m_attrProvider
)
2778 attr
->SetKind(wxGridCellAttr::Row
);
2779 m_attrProvider
->SetRowAttr(attr
, row
);
2783 // as we take ownership of the pointer and don't store it, we must
2789 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2791 if ( m_attrProvider
)
2793 attr
->SetKind(wxGridCellAttr::Col
);
2794 m_attrProvider
->SetColAttr(attr
, col
);
2798 // as we take ownership of the pointer and don't store it, we must
2804 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2805 size_t WXUNUSED(numRows
) )
2807 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2812 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2814 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2819 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2820 size_t WXUNUSED(numRows
) )
2822 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2827 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2828 size_t WXUNUSED(numCols
) )
2830 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2835 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2837 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2842 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2843 size_t WXUNUSED(numCols
) )
2845 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2851 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2854 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2855 // how much it makes sense to us geeks.
2859 wxString
wxGridTableBase::GetColLabelValue( int col
)
2861 // default col labels are:
2862 // cols 0 to 25 : A-Z
2863 // cols 26 to 675 : AA-ZZ
2868 for ( n
= 1; ; n
++ )
2870 s
+= (_T('A') + (wxChar
)( col%26
));
2872 if ( col
< 0 ) break;
2875 // reverse the string...
2877 for ( i
= 0; i
< n
; i
++ )
2886 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2888 return wxGRID_VALUE_STRING
;
2891 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2892 const wxString
& typeName
)
2894 return typeName
== wxGRID_VALUE_STRING
;
2897 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2899 return CanGetValueAs(row
, col
, typeName
);
2902 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2907 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2912 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2917 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2918 long WXUNUSED(value
) )
2922 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2923 double WXUNUSED(value
) )
2927 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2928 bool WXUNUSED(value
) )
2933 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2934 const wxString
& WXUNUSED(typeName
) )
2939 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2940 const wxString
& WXUNUSED(typeName
),
2941 void* WXUNUSED(value
) )
2945 //////////////////////////////////////////////////////////////////////
2947 // Message class for the grid table to send requests and notifications
2951 wxGridTableMessage::wxGridTableMessage()
2953 m_table
= (wxGridTableBase
*) NULL
;
2959 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2960 int commandInt1
, int commandInt2
)
2964 m_comInt1
= commandInt1
;
2965 m_comInt2
= commandInt2
;
2970 //////////////////////////////////////////////////////////////////////
2972 // A basic grid table for string data. An object of this class will
2973 // created by wxGrid if you don't specify an alternative table class.
2976 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2978 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2980 wxGridStringTable::wxGridStringTable()
2985 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2988 m_data
.Alloc( numRows
);
2991 sa
.Alloc( numCols
);
2992 sa
.Add( wxEmptyString
, numCols
);
2994 m_data
.Add( sa
, numRows
);
2997 wxGridStringTable::~wxGridStringTable()
3001 int wxGridStringTable::GetNumberRows()
3003 return m_data
.GetCount();
3006 int wxGridStringTable::GetNumberCols()
3008 if ( m_data
.GetCount() > 0 )
3009 return m_data
[0].GetCount();
3014 wxString
wxGridStringTable::GetValue( int row
, int col
)
3016 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3017 _T("invalid row or column index in wxGridStringTable") );
3019 return m_data
[row
][col
];
3022 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3024 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3025 _T("invalid row or column index in wxGridStringTable") );
3027 m_data
[row
][col
] = value
;
3030 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3032 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3033 _T("invalid row or column index in wxGridStringTable") );
3035 return (m_data
[row
][col
] == wxEmptyString
);
3038 void wxGridStringTable::Clear()
3041 int numRows
, numCols
;
3043 numRows
= m_data
.GetCount();
3046 numCols
= m_data
[0].GetCount();
3048 for ( row
= 0; row
< numRows
; row
++ )
3050 for ( col
= 0; col
< numCols
; col
++ )
3052 m_data
[row
][col
] = wxEmptyString
;
3059 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3061 size_t curNumRows
= m_data
.GetCount();
3062 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3063 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3065 if ( pos
>= curNumRows
)
3067 return AppendRows( numRows
);
3071 sa
.Alloc( curNumCols
);
3072 sa
.Add( wxEmptyString
, curNumCols
);
3073 m_data
.Insert( sa
, pos
, numRows
);
3076 wxGridTableMessage
msg( this,
3077 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3081 GetView()->ProcessTableMessage( msg
);
3087 bool wxGridStringTable::AppendRows( size_t numRows
)
3089 size_t curNumRows
= m_data
.GetCount();
3090 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3091 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3094 if ( curNumCols
> 0 )
3096 sa
.Alloc( curNumCols
);
3097 sa
.Add( wxEmptyString
, curNumCols
);
3100 m_data
.Add( sa
, numRows
);
3104 wxGridTableMessage
msg( this,
3105 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3108 GetView()->ProcessTableMessage( msg
);
3114 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3116 size_t curNumRows
= m_data
.GetCount();
3118 if ( pos
>= curNumRows
)
3120 wxFAIL_MSG( wxString::Format
3122 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3124 (unsigned long)numRows
,
3125 (unsigned long)curNumRows
3131 if ( numRows
> curNumRows
- pos
)
3133 numRows
= curNumRows
- pos
;
3136 if ( numRows
>= curNumRows
)
3142 m_data
.RemoveAt( pos
, numRows
);
3146 wxGridTableMessage
msg( this,
3147 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3151 GetView()->ProcessTableMessage( msg
);
3157 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3161 size_t curNumRows
= m_data
.GetCount();
3162 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3163 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3165 if ( pos
>= curNumCols
)
3167 return AppendCols( numCols
);
3170 for ( row
= 0; row
< curNumRows
; row
++ )
3172 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3174 m_data
[row
].Insert( wxEmptyString
, col
);
3179 wxGridTableMessage
msg( this,
3180 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3184 GetView()->ProcessTableMessage( msg
);
3190 bool wxGridStringTable::AppendCols( size_t numCols
)
3194 size_t curNumRows
= m_data
.GetCount();
3198 // TODO: something better than this ?
3200 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3205 for ( row
= 0; row
< curNumRows
; row
++ )
3207 m_data
[row
].Add( wxEmptyString
, numCols
);
3212 wxGridTableMessage
msg( this,
3213 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3216 GetView()->ProcessTableMessage( msg
);
3222 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3226 size_t curNumRows
= m_data
.GetCount();
3227 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3228 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3230 if ( pos
>= curNumCols
)
3232 wxFAIL_MSG( wxString::Format
3234 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3236 (unsigned long)numCols
,
3237 (unsigned long)curNumCols
3242 if ( numCols
> curNumCols
- pos
)
3244 numCols
= curNumCols
- pos
;
3247 for ( row
= 0; row
< curNumRows
; row
++ )
3249 if ( numCols
>= curNumCols
)
3251 m_data
[row
].Clear();
3255 m_data
[row
].RemoveAt( pos
, numCols
);
3260 wxGridTableMessage
msg( this,
3261 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3265 GetView()->ProcessTableMessage( msg
);
3271 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3273 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3275 // using default label
3277 return wxGridTableBase::GetRowLabelValue( row
);
3281 return m_rowLabels
[ row
];
3285 wxString
wxGridStringTable::GetColLabelValue( int col
)
3287 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3289 // using default label
3291 return wxGridTableBase::GetColLabelValue( col
);
3295 return m_colLabels
[ col
];
3299 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3301 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3303 int n
= m_rowLabels
.GetCount();
3305 for ( i
= n
; i
<= row
; i
++ )
3307 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3311 m_rowLabels
[row
] = value
;
3314 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3316 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3318 int n
= m_colLabels
.GetCount();
3320 for ( i
= n
; i
<= col
; i
++ )
3322 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3326 m_colLabels
[col
] = value
;
3331 //////////////////////////////////////////////////////////////////////
3332 //////////////////////////////////////////////////////////////////////
3334 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3336 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3337 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3338 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3339 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3340 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3341 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3344 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3346 const wxPoint
&pos
, const wxSize
&size
)
3347 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3352 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3356 // NO - don't do this because it will set both the x and y origin
3357 // coords to match the parent scrolled window and we just want to
3358 // set the y coord - MB
3360 // m_owner->PrepareDC( dc );
3363 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3364 dc
.SetDeviceOrigin( 0, -y
);
3366 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3367 m_owner
->DrawRowLabels( dc
, rows
);
3371 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3373 m_owner
->ProcessRowLabelMouseEvent( event
);
3377 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3379 m_owner
->GetEventHandler()->ProcessEvent(event
);
3383 // This seems to be required for wxMotif otherwise the mouse
3384 // cursor must be in the cell edit control to get key events
3386 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3388 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3391 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3393 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3398 //////////////////////////////////////////////////////////////////////
3400 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3402 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3403 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3404 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3405 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3406 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3407 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3410 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3412 const wxPoint
&pos
, const wxSize
&size
)
3413 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3418 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3422 // NO - don't do this because it will set both the x and y origin
3423 // coords to match the parent scrolled window and we just want to
3424 // set the x coord - MB
3426 // m_owner->PrepareDC( dc );
3429 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3430 dc
.SetDeviceOrigin( -x
, 0 );
3432 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3433 m_owner
->DrawColLabels( dc
, cols
);
3437 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3439 m_owner
->ProcessColLabelMouseEvent( event
);
3442 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3444 m_owner
->GetEventHandler()->ProcessEvent(event
);
3448 // This seems to be required for wxMotif otherwise the mouse
3449 // cursor must be in the cell edit control to get key events
3451 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3453 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3456 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3458 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3463 //////////////////////////////////////////////////////////////////////
3465 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3467 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3468 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3469 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3470 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3471 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3472 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3475 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3477 const wxPoint
&pos
, const wxSize
&size
)
3478 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3483 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3487 int client_height
= 0;
3488 int client_width
= 0;
3489 GetClientSize( &client_width
, &client_height
);
3491 dc
.SetPen( *wxBLACK_PEN
);
3492 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3493 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3495 dc
.SetPen( *wxWHITE_PEN
);
3496 dc
.DrawLine( 0, 0, client_width
, 0 );
3497 dc
.DrawLine( 0, 0, 0, client_height
);
3501 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3503 m_owner
->ProcessCornerLabelMouseEvent( event
);
3507 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3509 m_owner
->GetEventHandler()->ProcessEvent(event
);
3512 // This seems to be required for wxMotif otherwise the mouse
3513 // cursor must be in the cell edit control to get key events
3515 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3517 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3520 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3522 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3527 //////////////////////////////////////////////////////////////////////
3529 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3531 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3532 EVT_PAINT( wxGridWindow::OnPaint
)
3533 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3534 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3535 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3536 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3537 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3540 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3541 wxGridRowLabelWindow
*rowLblWin
,
3542 wxGridColLabelWindow
*colLblWin
,
3543 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3544 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, wxT("grid window") )
3547 m_rowLabelWin
= rowLblWin
;
3548 m_colLabelWin
= colLblWin
;
3549 SetBackgroundColour(_T("WHITE"));
3553 wxGridWindow::~wxGridWindow()
3558 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3560 wxPaintDC
dc( this );
3561 m_owner
->PrepareDC( dc
);
3562 wxRegion reg
= GetUpdateRegion();
3563 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3564 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3565 #if WXGRID_DRAW_LINES
3566 m_owner
->DrawAllGridLines( dc
, reg
);
3568 m_owner
->DrawGridSpace( dc
);
3569 m_owner
->DrawHighlight( dc
, DirtyCells
);
3573 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3575 wxWindow::ScrollWindow( dx
, dy
, rect
);
3576 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3577 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3581 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3583 m_owner
->ProcessGridCellMouseEvent( event
);
3586 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3588 m_owner
->GetEventHandler()->ProcessEvent(event
);
3591 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3592 // cursor must be in the cell edit control to get key events
3594 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3596 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3599 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3601 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3604 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3609 //////////////////////////////////////////////////////////////////////
3611 // Internal Helper function for computing row or column from some
3612 // (unscrolled) coordinate value, using either
3613 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3614 // of m_rowBottoms/m_ColRights to speed up the search!
3616 // Internal helper macros for simpler use of that function
3618 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3619 const wxArrayInt
& BorderArray
, int nMax
,
3620 bool maxOnOverflow
);
3622 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3623 WXGRID_MIN_COL_WIDTH, \
3624 m_colRights, m_numCols, TRUE)
3625 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3626 WXGRID_MIN_ROW_HEIGHT, \
3627 m_rowBottoms, m_numRows, TRUE)
3628 /////////////////////////////////////////////////////////////////////
3630 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3632 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3633 EVT_PAINT( wxGrid::OnPaint
)
3634 EVT_SIZE( wxGrid::OnSize
)
3635 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3636 EVT_KEY_UP( wxGrid::OnKeyUp
)
3637 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3640 wxGrid::wxGrid( wxWindow
*parent
,
3645 const wxString
& name
)
3646 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3647 m_colMinWidths(GRID_HASH_SIZE
),
3648 m_rowMinHeights(GRID_HASH_SIZE
)
3656 // Must do this or ~wxScrollHelper will pop the wrong event handler
3657 SetTargetWindow(this);
3659 wxSafeDecRef(m_defaultCellAttr
);
3661 #ifdef DEBUG_ATTR_CACHE
3662 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3663 wxPrintf(_T("wxGrid attribute cache statistics: "
3664 "total: %u, hits: %u (%u%%)\n"),
3665 total
, gs_nAttrCacheHits
,
3666 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3672 delete m_typeRegistry
;
3678 // ----- internal init and update functions
3681 void wxGrid::Create()
3683 m_created
= FALSE
; // set to TRUE by CreateGrid
3685 m_table
= (wxGridTableBase
*) NULL
;
3688 m_cellEditCtrlEnabled
= FALSE
;
3690 m_defaultCellAttr
= new wxGridCellAttr();
3692 // Set default cell attributes
3693 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3694 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3695 m_defaultCellAttr
->SetFont(GetFont());
3696 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3697 m_defaultCellAttr
->SetTextColour(
3698 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3699 m_defaultCellAttr
->SetBackgroundColour(
3700 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3701 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3702 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3707 m_currentCellCoords
= wxGridNoCellCoords
;
3709 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3710 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3712 // create the type registry
3713 m_typeRegistry
= new wxGridTypeRegistry
;
3716 // subwindow components that make up the wxGrid
3717 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3722 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3727 m_colLabelWin
= new wxGridColLabelWindow( this,
3732 m_gridWin
= new wxGridWindow( this,
3739 SetTargetWindow( m_gridWin
);
3745 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3746 wxGrid::wxGridSelectionModes selmode
)
3748 wxCHECK_MSG( !m_created
,
3750 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3752 m_numRows
= numRows
;
3753 m_numCols
= numCols
;
3755 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3756 m_table
->SetView( this );
3758 m_selection
= new wxGridSelection( this, selmode
);
3767 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3769 wxCHECK_RET( m_created
,
3770 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3772 m_selection
->SetSelectionMode( selmode
);
3775 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3777 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3778 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3780 return m_selection
->GetSelectionMode();
3783 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3784 wxGrid::wxGridSelectionModes selmode
)
3788 // RD: Actually, this should probably be allowed. I think it would be
3789 // nice to be able to switch multiple Tables in and out of a single
3790 // View at runtime. Is there anything in the implementation that
3791 // would prevent this?
3793 // At least, you now have to cope with m_selection
3794 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3799 m_numRows
= table
->GetNumberRows();
3800 m_numCols
= table
->GetNumberCols();
3803 m_table
->SetView( this );
3806 m_selection
= new wxGridSelection( this, selmode
);
3819 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3820 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3822 if ( m_rowLabelWin
)
3824 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3828 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3831 m_labelTextColour
= wxColour( _T("BLACK") );
3834 m_attrCache
.row
= -1;
3836 // TODO: something better than this ?
3838 m_labelFont
= this->GetFont();
3839 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3841 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3842 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3844 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3845 m_colLabelVertAlign
= wxALIGN_TOP
;
3847 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3848 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3850 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3851 m_defaultRowHeight
+= 8;
3853 m_defaultRowHeight
+= 4;
3856 m_gridLineColour
= wxColour( 128, 128, 255 );
3857 m_gridLinesEnabled
= TRUE
;
3858 m_cellHighlightColour
= m_gridLineColour
;
3859 m_cellHighlightPenWidth
= 2;
3860 m_cellHighlightROPenWidth
= 1;
3862 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3863 m_winCapture
= (wxWindow
*)NULL
;
3864 m_canDragRowSize
= TRUE
;
3865 m_canDragColSize
= TRUE
;
3866 m_canDragGridSize
= TRUE
;
3868 m_dragRowOrCol
= -1;
3869 m_isDragging
= FALSE
;
3870 m_startDragPos
= wxDefaultPosition
;
3872 m_waitForSlowClick
= FALSE
;
3874 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3875 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3877 m_currentCellCoords
= wxGridNoCellCoords
;
3879 m_selectingTopLeft
= wxGridNoCellCoords
;
3880 m_selectingBottomRight
= wxGridNoCellCoords
;
3881 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3882 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3884 m_editable
= TRUE
; // default for whole grid
3886 m_inOnKeyDown
= FALSE
;
3893 // ----------------------------------------------------------------------------
3894 // the idea is to call these functions only when necessary because they create
3895 // quite big arrays which eat memory mostly unnecessary - in particular, if
3896 // default widths/heights are used for all rows/columns, we may not use these
3899 // with some extra code, it should be possible to only store the
3900 // widths/heights different from default ones but this will be done later...
3901 // ----------------------------------------------------------------------------
3903 void wxGrid::InitRowHeights()
3905 m_rowHeights
.Empty();
3906 m_rowBottoms
.Empty();
3908 m_rowHeights
.Alloc( m_numRows
);
3909 m_rowBottoms
.Alloc( m_numRows
);
3913 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
3915 for ( int i
= 0; i
< m_numRows
; i
++ )
3917 rowBottom
+= m_defaultRowHeight
;
3918 m_rowBottoms
.Add( rowBottom
);
3922 void wxGrid::InitColWidths()
3924 m_colWidths
.Empty();
3925 m_colRights
.Empty();
3927 m_colWidths
.Alloc( m_numCols
);
3928 m_colRights
.Alloc( m_numCols
);
3931 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
3933 for ( int i
= 0; i
< m_numCols
; i
++ )
3935 colRight
+= m_defaultColWidth
;
3936 m_colRights
.Add( colRight
);
3940 int wxGrid::GetColWidth(int col
) const
3942 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3945 int wxGrid::GetColLeft(int col
) const
3947 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3948 : m_colRights
[col
] - m_colWidths
[col
];
3951 int wxGrid::GetColRight(int col
) const
3953 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3957 int wxGrid::GetRowHeight(int row
) const
3959 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3962 int wxGrid::GetRowTop(int row
) const
3964 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3965 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3968 int wxGrid::GetRowBottom(int row
) const
3970 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3971 : m_rowBottoms
[row
];
3974 void wxGrid::CalcDimensions()
3977 GetClientSize( &cw
, &ch
);
3979 if ( m_rowLabelWin
->IsShown() )
3980 cw
-= m_rowLabelWidth
;
3981 if ( m_colLabelWin
->IsShown() )
3982 ch
-= m_colLabelHeight
;
3985 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3986 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3988 // preserve (more or less) the previous position
3990 GetViewStart( &x
, &y
);
3992 // maybe we don't need scrollbars at all?
3994 // also adjust the position to be valid for the new scroll rangs
4015 // do set scrollbar parameters
4016 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4017 GetScrollX(w
), GetScrollY(h
), x
, y
,
4018 GetBatchCount() != 0);
4020 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4021 // still must reposition the children
4026 void wxGrid::CalcWindowSizes()
4029 GetClientSize( &cw
, &ch
);
4031 if ( m_cornerLabelWin
->IsShown() )
4032 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4034 if ( m_colLabelWin
->IsShown() )
4035 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4037 if ( m_rowLabelWin
->IsShown() )
4038 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4040 if ( m_gridWin
->IsShown() )
4041 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4045 // this is called when the grid table sends a message to say that it
4046 // has been redimensioned
4048 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4051 bool result
= FALSE
;
4053 // Clear the attribute cache as the attribute might refer to a different
4054 // cell than stored in the cache after adding/removing rows/columns.
4056 // By the same reasoning, the editor should be dismissed if columns are
4057 // added or removed. And for consistency, it should IMHO always be
4058 // removed, not only if the cell "underneath" it actually changes.
4059 // For now, I intentionally do not save the editor's content as the
4060 // cell it might want to save that stuff to might no longer exist.
4061 HideCellEditControl();
4063 // if we were using the default widths/heights so far, we must change them
4065 if ( m_colWidths
.IsEmpty() )
4070 if ( m_rowHeights
.IsEmpty() )
4076 switch ( msg
.GetId() )
4078 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4080 size_t pos
= msg
.GetCommandInt();
4081 int numRows
= msg
.GetCommandInt2();
4083 m_numRows
+= numRows
;
4085 if ( !m_rowHeights
.IsEmpty() )
4087 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4088 m_rowBottoms
.Insert( 0, pos
, numRows
);
4091 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4093 for ( i
= pos
; 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 );
4108 m_selection
->UpdateRows( pos
, numRows
);
4109 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4111 attrProvider
->UpdateAttrRows( pos
, numRows
);
4113 if ( !GetBatchCount() )
4116 m_rowLabelWin
->Refresh();
4122 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4124 int numRows
= msg
.GetCommandInt();
4125 int oldNumRows
= m_numRows
;
4126 m_numRows
+= numRows
;
4128 if ( !m_rowHeights
.IsEmpty() )
4130 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4131 m_rowBottoms
.Add( 0, numRows
);
4134 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4136 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4138 bottom
+= m_rowHeights
[i
];
4139 m_rowBottoms
[i
] = bottom
;
4142 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4144 // if we have just inserted cols into an empty grid the current
4145 // cell will be undefined...
4147 SetCurrentCell( 0, 0 );
4149 if ( !GetBatchCount() )
4152 m_rowLabelWin
->Refresh();
4158 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4160 size_t pos
= msg
.GetCommandInt();
4161 int numRows
= msg
.GetCommandInt2();
4162 m_numRows
-= numRows
;
4164 if ( !m_rowHeights
.IsEmpty() )
4166 m_rowHeights
.RemoveAt( pos
, numRows
);
4167 m_rowBottoms
.RemoveAt( pos
, numRows
);
4170 for ( i
= 0; i
< m_numRows
; i
++ )
4172 h
+= m_rowHeights
[i
];
4173 m_rowBottoms
[i
] = h
;
4178 m_currentCellCoords
= wxGridNoCellCoords
;
4182 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4183 m_currentCellCoords
.Set( 0, 0 );
4187 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4188 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4190 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4191 // ifdef'd out following patch from Paul Gammans
4193 // No need to touch column attributes, unless we
4194 // removed _all_ rows, in this case, we remove
4195 // all column attributes.
4196 // I hate to do this here, but the
4197 // needed data is not available inside UpdateAttrRows.
4198 if ( !GetNumberRows() )
4199 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4202 if ( !GetBatchCount() )
4205 m_rowLabelWin
->Refresh();
4211 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4213 size_t pos
= msg
.GetCommandInt();
4214 int numCols
= msg
.GetCommandInt2();
4215 m_numCols
+= numCols
;
4217 if ( !m_colWidths
.IsEmpty() )
4219 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4220 m_colRights
.Insert( 0, pos
, numCols
);
4223 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4225 for ( i
= pos
; i
< m_numCols
; i
++ )
4227 right
+= m_colWidths
[i
];
4228 m_colRights
[i
] = right
;
4231 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4233 // if we have just inserted cols into an empty grid the current
4234 // cell will be undefined...
4236 SetCurrentCell( 0, 0 );
4240 m_selection
->UpdateCols( pos
, numCols
);
4241 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4243 attrProvider
->UpdateAttrCols( pos
, numCols
);
4244 if ( !GetBatchCount() )
4247 m_colLabelWin
->Refresh();
4254 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4256 int numCols
= msg
.GetCommandInt();
4257 int oldNumCols
= m_numCols
;
4258 m_numCols
+= numCols
;
4259 if ( !m_colWidths
.IsEmpty() )
4261 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4262 m_colRights
.Add( 0, numCols
);
4265 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4267 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4269 right
+= m_colWidths
[i
];
4270 m_colRights
[i
] = right
;
4273 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4275 // if we have just inserted cols into an empty grid the current
4276 // cell will be undefined...
4278 SetCurrentCell( 0, 0 );
4280 if ( !GetBatchCount() )
4283 m_colLabelWin
->Refresh();
4289 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4291 size_t pos
= msg
.GetCommandInt();
4292 int numCols
= msg
.GetCommandInt2();
4293 m_numCols
-= numCols
;
4295 if ( !m_colWidths
.IsEmpty() )
4297 m_colWidths
.RemoveAt( pos
, numCols
);
4298 m_colRights
.RemoveAt( pos
, numCols
);
4301 for ( i
= 0; i
< m_numCols
; i
++ )
4303 w
+= m_colWidths
[i
];
4309 m_currentCellCoords
= wxGridNoCellCoords
;
4313 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4314 m_currentCellCoords
.Set( 0, 0 );
4318 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4319 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4321 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4322 // ifdef'd out following patch from Paul Gammans
4324 // No need to touch row attributes, unless we
4325 // removed _all_ columns, in this case, we remove
4326 // all row attributes.
4327 // I hate to do this here, but the
4328 // needed data is not available inside UpdateAttrCols.
4329 if ( !GetNumberCols() )
4330 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4333 if ( !GetBatchCount() )
4336 m_colLabelWin
->Refresh();
4343 if (result
&& !GetBatchCount() )
4344 m_gridWin
->Refresh();
4349 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4351 wxRegionIterator
iter( reg
);
4354 wxArrayInt rowlabels
;
4361 // TODO: remove this when we can...
4362 // There is a bug in wxMotif that gives garbage update
4363 // rectangles if you jump-scroll a long way by clicking the
4364 // scrollbar with middle button. This is a work-around
4366 #if defined(__WXMOTIF__)
4368 m_gridWin
->GetClientSize( &cw
, &ch
);
4369 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4370 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4373 // logical bounds of update region
4376 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4377 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4379 // find the row labels within these bounds
4382 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4384 if ( GetRowBottom(row
) < top
)
4387 if ( GetRowTop(row
) > bottom
)
4390 rowlabels
.Add( row
);
4400 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4402 wxRegionIterator
iter( reg
);
4405 wxArrayInt colLabels
;
4412 // TODO: remove this when we can...
4413 // There is a bug in wxMotif that gives garbage update
4414 // rectangles if you jump-scroll a long way by clicking the
4415 // scrollbar with middle button. This is a work-around
4417 #if defined(__WXMOTIF__)
4419 m_gridWin
->GetClientSize( &cw
, &ch
);
4420 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4421 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4424 // logical bounds of update region
4427 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4428 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4430 // find the cells within these bounds
4433 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4435 if ( GetColRight(col
) < left
)
4438 if ( GetColLeft(col
) > right
)
4441 colLabels
.Add( col
);
4450 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4452 wxRegionIterator
iter( reg
);
4455 wxGridCellCoordsArray cellsExposed
;
4457 int left
, top
, right
, bottom
;
4462 // TODO: remove this when we can...
4463 // There is a bug in wxMotif that gives garbage update
4464 // rectangles if you jump-scroll a long way by clicking the
4465 // scrollbar with middle button. This is a work-around
4467 #if defined(__WXMOTIF__)
4469 m_gridWin
->GetClientSize( &cw
, &ch
);
4470 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4471 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4472 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4473 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4476 // logical bounds of update region
4478 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4479 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4481 // find the cells within these bounds
4484 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4486 if ( GetRowBottom(row
) <= top
)
4489 if ( GetRowTop(row
) > bottom
)
4492 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4494 if ( GetColRight(col
) <= left
)
4497 if ( GetColLeft(col
) > right
)
4500 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4507 return cellsExposed
;
4511 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4514 wxPoint
pos( event
.GetPosition() );
4515 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4517 if ( event
.Dragging() )
4521 m_isDragging
= TRUE
;
4522 m_rowLabelWin
->CaptureMouse();
4525 if ( event
.LeftIsDown() )
4527 switch( m_cursorMode
)
4529 case WXGRID_CURSOR_RESIZE_ROW
:
4531 int cw
, ch
, left
, dummy
;
4532 m_gridWin
->GetClientSize( &cw
, &ch
);
4533 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4535 wxClientDC
dc( m_gridWin
);
4538 GetRowTop(m_dragRowOrCol
) +
4539 GetRowMinimalHeight(m_dragRowOrCol
) );
4540 dc
.SetLogicalFunction(wxINVERT
);
4541 if ( m_dragLastPos
>= 0 )
4543 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4545 dc
.DrawLine( left
, y
, left
+cw
, y
);
4550 case WXGRID_CURSOR_SELECT_ROW
:
4551 if ( (row
= YToRow( y
)) >= 0 )
4555 m_selection
->SelectRow( row
,
4556 event
.ControlDown(),
4563 // default label to suppress warnings about "enumeration value
4564 // 'xxx' not handled in switch
4572 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4577 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4578 m_isDragging
= FALSE
;
4581 // ------------ Entering or leaving the window
4583 if ( event
.Entering() || event
.Leaving() )
4585 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4589 // ------------ Left button pressed
4591 else if ( event
.LeftDown() )
4593 // don't send a label click event for a hit on the
4594 // edge of the row label - this is probably the user
4595 // wanting to resize the row
4597 if ( YToEdgeOfRow(y
) < 0 )
4601 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4603 if ( !event
.ShiftDown() && !event
.ControlDown() )
4607 if ( event
.ShiftDown() )
4609 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4612 GetNumberCols() - 1,
4613 event
.ControlDown(),
4620 m_selection
->SelectRow( row
,
4621 event
.ControlDown(),
4628 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4633 // starting to drag-resize a row
4635 if ( CanDragRowSize() )
4636 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4641 // ------------ Left double click
4643 else if (event
.LeftDClick() )
4645 if ( YToEdgeOfRow(y
) < 0 )
4648 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4653 // ------------ Left button released
4655 else if ( event
.LeftUp() )
4657 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4659 DoEndDragResizeRow();
4661 // Note: we are ending the event *after* doing
4662 // default processing in this case
4664 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4667 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4672 // ------------ Right button down
4674 else if ( event
.RightDown() )
4677 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4679 // no default action at the moment
4684 // ------------ Right double click
4686 else if ( event
.RightDClick() )
4689 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4691 // no default action at the moment
4696 // ------------ No buttons down and mouse moving
4698 else if ( event
.Moving() )
4700 m_dragRowOrCol
= YToEdgeOfRow( y
);
4701 if ( m_dragRowOrCol
>= 0 )
4703 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4705 // don't capture the mouse yet
4706 if ( CanDragRowSize() )
4707 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4710 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4712 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4718 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4721 wxPoint
pos( event
.GetPosition() );
4722 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4724 if ( event
.Dragging() )
4728 m_isDragging
= TRUE
;
4729 m_colLabelWin
->CaptureMouse();
4732 if ( event
.LeftIsDown() )
4734 switch( m_cursorMode
)
4736 case WXGRID_CURSOR_RESIZE_COL
:
4738 int cw
, ch
, dummy
, top
;
4739 m_gridWin
->GetClientSize( &cw
, &ch
);
4740 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4742 wxClientDC
dc( m_gridWin
);
4745 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4746 GetColMinimalWidth(m_dragRowOrCol
));
4747 dc
.SetLogicalFunction(wxINVERT
);
4748 if ( m_dragLastPos
>= 0 )
4750 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4752 dc
.DrawLine( x
, top
, x
, top
+ch
);
4757 case WXGRID_CURSOR_SELECT_COL
:
4758 if ( (col
= XToCol( x
)) >= 0 )
4762 m_selection
->SelectCol( col
,
4763 event
.ControlDown(),
4770 // default label to suppress warnings about "enumeration value
4771 // 'xxx' not handled in switch
4779 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4784 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4785 m_isDragging
= FALSE
;
4788 // ------------ Entering or leaving the window
4790 if ( event
.Entering() || event
.Leaving() )
4792 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4796 // ------------ Left button pressed
4798 else if ( event
.LeftDown() )
4800 // don't send a label click event for a hit on the
4801 // edge of the col label - this is probably the user
4802 // wanting to resize the col
4804 if ( XToEdgeOfCol(x
) < 0 )
4808 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4810 if ( !event
.ShiftDown() && !event
.ControlDown() )
4814 if ( event
.ShiftDown() )
4816 m_selection
->SelectBlock( 0,
4817 m_currentCellCoords
.GetCol(),
4818 GetNumberRows() - 1, col
,
4819 event
.ControlDown(),
4826 m_selection
->SelectCol( col
,
4827 event
.ControlDown(),
4834 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4839 // starting to drag-resize a col
4841 if ( CanDragColSize() )
4842 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4847 // ------------ Left double click
4849 if ( event
.LeftDClick() )
4851 if ( XToEdgeOfCol(x
) < 0 )
4854 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4859 // ------------ Left button released
4861 else if ( event
.LeftUp() )
4863 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4865 DoEndDragResizeCol();
4867 // Note: we are ending the event *after* doing
4868 // default processing in this case
4870 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4873 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4878 // ------------ Right button down
4880 else if ( event
.RightDown() )
4883 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4885 // no default action at the moment
4890 // ------------ Right double click
4892 else if ( event
.RightDClick() )
4895 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4897 // no default action at the moment
4902 // ------------ No buttons down and mouse moving
4904 else if ( event
.Moving() )
4906 m_dragRowOrCol
= XToEdgeOfCol( x
);
4907 if ( m_dragRowOrCol
>= 0 )
4909 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4911 // don't capture the cursor yet
4912 if ( CanDragColSize() )
4913 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4916 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4918 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4924 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4926 if ( event
.LeftDown() )
4928 // indicate corner label by having both row and
4931 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4937 else if ( event
.LeftDClick() )
4939 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4942 else if ( event
.RightDown() )
4944 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4946 // no default action at the moment
4950 else if ( event
.RightDClick() )
4952 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4954 // no default action at the moment
4959 void wxGrid::ChangeCursorMode(CursorMode mode
,
4964 static const wxChar
*cursorModes
[] =
4973 wxLogTrace(_T("grid"),
4974 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4975 win
== m_colLabelWin
? _T("colLabelWin")
4976 : win
? _T("rowLabelWin")
4978 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4979 #endif // __WXDEBUG__
4981 if ( mode
== m_cursorMode
&&
4982 win
== m_winCapture
&&
4983 captureMouse
== (m_winCapture
!= NULL
))
4988 // by default use the grid itself
4994 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4995 m_winCapture
= (wxWindow
*)NULL
;
4998 m_cursorMode
= mode
;
5000 switch ( m_cursorMode
)
5002 case WXGRID_CURSOR_RESIZE_ROW
:
5003 win
->SetCursor( m_rowResizeCursor
);
5006 case WXGRID_CURSOR_RESIZE_COL
:
5007 win
->SetCursor( m_colResizeCursor
);
5011 win
->SetCursor( *wxSTANDARD_CURSOR
);
5014 // we need to capture mouse when resizing
5015 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5016 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5018 if ( captureMouse
&& resize
)
5020 win
->CaptureMouse();
5025 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5028 wxPoint
pos( event
.GetPosition() );
5029 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5031 wxGridCellCoords coords
;
5032 XYToCell( x
, y
, coords
);
5034 int cell_rows
, cell_cols
;
5035 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5036 if ((cell_rows
< 0) || (cell_cols
< 0))
5038 coords
.SetRow(coords
.GetRow() + cell_rows
);
5039 coords
.SetCol(coords
.GetCol() + cell_cols
);
5042 if ( event
.Dragging() )
5044 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5046 // Don't start doing anything until the mouse has been drug at
5047 // least 3 pixels in any direction...
5050 if (m_startDragPos
== wxDefaultPosition
)
5052 m_startDragPos
= pos
;
5055 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5059 m_isDragging
= TRUE
;
5060 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5062 // Hide the edit control, so it
5063 // won't interfer with drag-shrinking.
5064 if ( IsCellEditControlShown() )
5066 HideCellEditControl();
5067 SaveEditControlValue();
5070 // Have we captured the mouse yet?
5073 m_winCapture
= m_gridWin
;
5074 m_winCapture
->CaptureMouse();
5077 if ( coords
!= wxGridNoCellCoords
)
5079 if ( event
.ControlDown() )
5081 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5082 m_selectingKeyboard
= coords
;
5083 HighlightBlock ( m_selectingKeyboard
, coords
);
5087 if ( !IsSelection() )
5089 HighlightBlock( coords
, coords
);
5093 HighlightBlock( m_currentCellCoords
, coords
);
5097 if (! IsVisible(coords
))
5099 MakeCellVisible(coords
);
5100 // TODO: need to introduce a delay or something here. The
5101 // scrolling is way to fast, at least on MSW - also on GTK.
5105 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5107 int cw
, ch
, left
, dummy
;
5108 m_gridWin
->GetClientSize( &cw
, &ch
);
5109 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5111 wxClientDC
dc( m_gridWin
);
5113 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5114 GetRowMinimalHeight(m_dragRowOrCol
) );
5115 dc
.SetLogicalFunction(wxINVERT
);
5116 if ( m_dragLastPos
>= 0 )
5118 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5120 dc
.DrawLine( left
, y
, left
+cw
, y
);
5123 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5125 int cw
, ch
, dummy
, top
;
5126 m_gridWin
->GetClientSize( &cw
, &ch
);
5127 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5129 wxClientDC
dc( m_gridWin
);
5131 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5132 GetColMinimalWidth(m_dragRowOrCol
) );
5133 dc
.SetLogicalFunction(wxINVERT
);
5134 if ( m_dragLastPos
>= 0 )
5136 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5138 dc
.DrawLine( x
, top
, x
, top
+ch
);
5145 m_isDragging
= FALSE
;
5146 m_startDragPos
= wxDefaultPosition
;
5148 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5149 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5152 if ( event
.Entering() || event
.Leaving() )
5154 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5155 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5160 // ------------ Left button pressed
5162 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5164 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5169 if ( !event
.ControlDown() )
5171 if ( event
.ShiftDown() )
5175 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5176 m_currentCellCoords
.GetCol(),
5179 event
.ControlDown(),
5185 else if ( XToEdgeOfCol(x
) < 0 &&
5186 YToEdgeOfRow(y
) < 0 )
5188 DisableCellEditControl();
5189 MakeCellVisible( coords
);
5191 // if this is the second click on this cell then start
5193 if ( m_waitForSlowClick
&&
5194 (coords
== m_currentCellCoords
) &&
5195 CanEnableCellControl())
5197 EnableCellEditControl();
5199 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5200 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5203 editor
->StartingClick();
5207 m_waitForSlowClick
= FALSE
;
5211 if ( event
.ControlDown() )
5215 m_selection
->ToggleCellSelection( coords
.GetRow(),
5217 event
.ControlDown(),
5222 m_selectingTopLeft
= wxGridNoCellCoords
;
5223 m_selectingBottomRight
= wxGridNoCellCoords
;
5224 m_selectingKeyboard
= coords
;
5228 SetCurrentCell( coords
);
5231 if ( m_selection
->GetSelectionMode() !=
5232 wxGrid::wxGridSelectCells
)
5234 HighlightBlock( coords
, coords
);
5238 m_waitForSlowClick
= TRUE
;
5245 // ------------ Left double click
5247 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5249 DisableCellEditControl();
5251 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5253 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5261 // ------------ Left button released
5263 else if ( event
.LeftUp() )
5265 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5267 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5268 m_selectingBottomRight
!= wxGridNoCellCoords
)
5272 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5273 m_winCapture
= NULL
;
5278 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5279 m_selectingTopLeft
.GetCol(),
5280 m_selectingBottomRight
.GetRow(),
5281 m_selectingBottomRight
.GetCol(),
5282 event
.ControlDown(),
5288 m_selectingTopLeft
= wxGridNoCellCoords
;
5289 m_selectingBottomRight
= wxGridNoCellCoords
;
5292 // Show the edit control, if it has been hidden for
5294 ShowCellEditControl();
5296 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5298 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5299 DoEndDragResizeRow();
5301 // Note: we are ending the event *after* doing
5302 // default processing in this case
5304 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5306 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5308 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5309 DoEndDragResizeCol();
5311 // Note: we are ending the event *after* doing
5312 // default processing in this case
5314 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5321 // ------------ Right button down
5323 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5325 DisableCellEditControl();
5326 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5331 // no default action at the moment
5336 // ------------ Right double click
5338 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5340 DisableCellEditControl();
5341 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5346 // no default action at the moment
5350 // ------------ Moving and no button action
5352 else if ( event
.Moving() && !event
.IsButton() )
5354 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5356 // out of grid cell area
5357 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5361 int dragRow
= YToEdgeOfRow( y
);
5362 int dragCol
= XToEdgeOfCol( x
);
5364 // Dragging on the corner of a cell to resize in both
5365 // directions is not implemented yet...
5367 if ( dragRow
>= 0 && dragCol
>= 0 )
5369 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5375 m_dragRowOrCol
= dragRow
;
5377 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5379 if ( CanDragRowSize() && CanDragGridSize() )
5380 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5385 m_dragRowOrCol
= dragCol
;
5393 m_dragRowOrCol
= dragCol
;
5395 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5397 if ( CanDragColSize() && CanDragGridSize() )
5398 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5404 // Neither on a row or col edge
5406 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5408 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5414 void wxGrid::DoEndDragResizeRow()
5416 if ( m_dragLastPos
>= 0 )
5418 // erase the last line and resize the row
5420 int cw
, ch
, left
, dummy
;
5421 m_gridWin
->GetClientSize( &cw
, &ch
);
5422 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5424 wxClientDC
dc( m_gridWin
);
5426 dc
.SetLogicalFunction( wxINVERT
);
5427 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5428 HideCellEditControl();
5429 SaveEditControlValue();
5431 int rowTop
= GetRowTop(m_dragRowOrCol
);
5432 SetRowSize( m_dragRowOrCol
,
5433 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5435 if ( !GetBatchCount() )
5437 // Only needed to get the correct rect.y:
5438 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5440 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5441 rect
.width
= m_rowLabelWidth
;
5442 rect
.height
= ch
- rect
.y
;
5443 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5445 // if there is a multicell block, paint all of it
5448 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5449 int leftCol
= XToCol(left
);
5450 int rightCol
= XToCol(left
+cw
);
5453 if (rightCol
< 0) rightCol
= m_numCols
;
5454 for (i
=leftCol
; i
<rightCol
; i
++)
5456 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5457 if (cell_rows
< subtract_rows
)
5458 subtract_rows
= cell_rows
;
5460 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5461 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5462 rect
.height
= ch
- rect
.y
;
5465 m_gridWin
->Refresh( FALSE
, &rect
);
5468 ShowCellEditControl();
5473 void wxGrid::DoEndDragResizeCol()
5475 if ( m_dragLastPos
>= 0 )
5477 // erase the last line and resize the col
5479 int cw
, ch
, dummy
, top
;
5480 m_gridWin
->GetClientSize( &cw
, &ch
);
5481 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5483 wxClientDC
dc( m_gridWin
);
5485 dc
.SetLogicalFunction( wxINVERT
);
5486 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5487 HideCellEditControl();
5488 SaveEditControlValue();
5490 int colLeft
= GetColLeft(m_dragRowOrCol
);
5491 SetColSize( m_dragRowOrCol
,
5492 wxMax( m_dragLastPos
- colLeft
,
5493 GetColMinimalWidth(m_dragRowOrCol
) ) );
5495 if ( !GetBatchCount() )
5497 // Only needed to get the correct rect.x:
5498 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5500 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5501 rect
.width
= cw
- rect
.x
;
5502 rect
.height
= m_colLabelHeight
;
5503 m_colLabelWin
->Refresh( TRUE
, &rect
);
5505 // if there is a multicell block, paint all of it
5508 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5509 int topRow
= YToRow(top
);
5510 int bottomRow
= YToRow(top
+cw
);
5513 if (bottomRow
< 0) bottomRow
= m_numRows
;
5514 for (i
=topRow
; i
<bottomRow
; i
++)
5516 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5517 if (cell_cols
< subtract_cols
)
5518 subtract_cols
= cell_cols
;
5520 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5521 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5522 rect
.width
= cw
- rect
.x
;
5525 m_gridWin
->Refresh( FALSE
, &rect
);
5528 ShowCellEditControl();
5535 // ------ interaction with data model
5537 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5539 switch ( msg
.GetId() )
5541 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5542 return GetModelValues();
5544 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5545 return SetModelValues();
5547 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5548 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5549 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5550 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5551 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5552 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5553 return Redimension( msg
);
5562 // The behaviour of this function depends on the grid table class
5563 // Clear() function. For the default wxGridStringTable class the
5564 // behavious is to replace all cell contents with wxEmptyString but
5565 // not to change the number of rows or cols.
5567 void wxGrid::ClearGrid()
5571 if (IsCellEditControlEnabled())
5572 DisableCellEditControl();
5575 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5580 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5582 // TODO: something with updateLabels flag
5586 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5592 if (IsCellEditControlEnabled())
5593 DisableCellEditControl();
5595 return m_table
->InsertRows( pos
, numRows
);
5597 // the table will have sent the results of the insert row
5598 // operation to this view object as a grid table message
5604 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5606 // TODO: something with updateLabels flag
5610 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5614 return ( m_table
&& m_table
->AppendRows( numRows
) );
5615 // the table will have sent the results of the append row
5616 // operation to this view object as a grid table message
5620 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5622 // TODO: something with updateLabels flag
5626 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5632 if (IsCellEditControlEnabled())
5633 DisableCellEditControl();
5635 return (m_table
->DeleteRows( pos
, numRows
));
5636 // the table will have sent the results of the delete row
5637 // operation to this view object as a grid table message
5643 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5645 // TODO: something with updateLabels flag
5649 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5655 if (IsCellEditControlEnabled())
5656 DisableCellEditControl();
5658 return m_table
->InsertCols( pos
, numCols
);
5659 // the table will have sent the results of the insert col
5660 // operation to this view object as a grid table message
5666 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5668 // TODO: something with updateLabels flag
5672 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5676 return ( m_table
&& m_table
->AppendCols( numCols
) );
5677 // the table will have sent the results of the append col
5678 // operation to this view object as a grid table message
5682 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5684 // TODO: something with updateLabels flag
5688 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5694 if (IsCellEditControlEnabled())
5695 DisableCellEditControl();
5697 return ( m_table
->DeleteCols( pos
, numCols
) );
5698 // the table will have sent the results of the delete col
5699 // operation to this view object as a grid table message
5707 // ----- event handlers
5710 // Generate a grid event based on a mouse event and
5711 // return the result of ProcessEvent()
5713 int wxGrid::SendEvent( const wxEventType type
,
5715 wxMouseEvent
& mouseEv
)
5720 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5722 int rowOrCol
= (row
== -1 ? col
: row
);
5724 wxGridSizeEvent
gridEvt( GetId(),
5728 mouseEv
.GetX() + GetRowLabelSize(),
5729 mouseEv
.GetY() + GetColLabelSize(),
5730 mouseEv
.ControlDown(),
5731 mouseEv
.ShiftDown(),
5733 mouseEv
.MetaDown() );
5735 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5736 vetoed
= !gridEvt
.IsAllowed();
5738 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5740 // Right now, it should _never_ end up here!
5741 wxGridRangeSelectEvent
gridEvt( GetId(),
5745 m_selectingBottomRight
,
5747 mouseEv
.ControlDown(),
5748 mouseEv
.ShiftDown(),
5750 mouseEv
.MetaDown() );
5752 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5753 vetoed
= !gridEvt
.IsAllowed();
5757 wxGridEvent
gridEvt( GetId(),
5761 mouseEv
.GetX() + GetRowLabelSize(),
5762 mouseEv
.GetY() + GetColLabelSize(),
5764 mouseEv
.ControlDown(),
5765 mouseEv
.ShiftDown(),
5767 mouseEv
.MetaDown() );
5768 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5769 vetoed
= !gridEvt
.IsAllowed();
5772 // A Veto'd event may not be `claimed' so test this first
5773 if (vetoed
) return -1;
5774 return claimed
? 1 : 0;
5778 // Generate a grid event of specified type and return the result
5779 // of ProcessEvent().
5781 int wxGrid::SendEvent( const wxEventType type
,
5787 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5789 int rowOrCol
= (row
== -1 ? col
: row
);
5791 wxGridSizeEvent
gridEvt( GetId(),
5796 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5797 vetoed
= !gridEvt
.IsAllowed();
5801 wxGridEvent
gridEvt( GetId(),
5806 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5807 vetoed
= !gridEvt
.IsAllowed();
5810 // A Veto'd event may not be `claimed' so test this first
5811 if (vetoed
) return -1;
5812 return claimed
? 1 : 0;
5816 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5818 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5821 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5823 // Don't do anything if between Begin/EndBatch...
5824 // EndBatch() will do all this on the last nested one anyway.
5825 if (! GetBatchCount())
5827 // Refresh to get correct scrolled position:
5828 wxScrolledWindow::Refresh(eraseb
,rect
);
5832 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5833 int width_label
, width_cell
, height_label
, height_cell
;
5836 //Copy rectangle can get scroll offsets..
5837 rect_x
= rect
->GetX();
5838 rect_y
= rect
->GetY();
5839 rectWidth
= rect
->GetWidth();
5840 rectHeight
= rect
->GetHeight();
5842 width_label
= m_rowLabelWidth
- rect_x
;
5843 if (width_label
> rectWidth
) width_label
= rectWidth
;
5845 height_label
= m_colLabelHeight
- rect_y
;
5846 if (height_label
> rectHeight
) height_label
= rectHeight
;
5848 if (rect_x
> m_rowLabelWidth
)
5850 x
= rect_x
- m_rowLabelWidth
;
5851 width_cell
= rectWidth
;
5856 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
5859 if (rect_y
> m_colLabelHeight
)
5861 y
= rect_y
- m_colLabelHeight
;
5862 height_cell
= rectHeight
;
5867 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
5870 // Paint corner label part intersecting rect.
5871 if ( width_label
> 0 && height_label
> 0 )
5873 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
5874 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
5877 // Paint col labels part intersecting rect.
5878 if ( width_cell
> 0 && height_label
> 0 )
5880 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
5881 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
5884 // Paint row labels part intersecting rect.
5885 if ( width_label
> 0 && height_cell
> 0 )
5887 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
5888 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
5891 // Paint cell area part intersecting rect.
5892 if ( width_cell
> 0 && height_cell
> 0 )
5894 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
5895 m_gridWin
->Refresh(eraseb
, &anotherrect
);
5900 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
5901 m_colLabelWin
->Refresh(eraseb
, NULL
);
5902 m_rowLabelWin
->Refresh(eraseb
, NULL
);
5903 m_gridWin
->Refresh(eraseb
, NULL
);
5908 void wxGrid::OnSize( wxSizeEvent
& event
)
5910 // position the child windows
5913 // don't call CalcDimensions() from here, the base class handles the size
5919 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5921 if ( m_inOnKeyDown
)
5923 // shouldn't be here - we are going round in circles...
5925 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5928 m_inOnKeyDown
= TRUE
;
5930 // propagate the event up and see if it gets processed
5932 wxWindow
*parent
= GetParent();
5933 wxKeyEvent
keyEvt( event
);
5934 keyEvt
.SetEventObject( parent
);
5936 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5939 // try local handlers
5941 switch ( event
.KeyCode() )
5944 if ( event
.ControlDown() )
5946 MoveCursorUpBlock( event
.ShiftDown() );
5950 MoveCursorUp( event
.ShiftDown() );
5955 if ( event
.ControlDown() )
5957 MoveCursorDownBlock( event
.ShiftDown() );
5961 MoveCursorDown( event
.ShiftDown() );
5966 if ( event
.ControlDown() )
5968 MoveCursorLeftBlock( event
.ShiftDown() );
5972 MoveCursorLeft( event
.ShiftDown() );
5977 if ( event
.ControlDown() )
5979 MoveCursorRightBlock( event
.ShiftDown() );
5983 MoveCursorRight( event
.ShiftDown() );
5988 case WXK_NUMPAD_ENTER
:
5989 if ( event
.ControlDown() )
5991 event
.Skip(); // to let the edit control have the return
5995 if ( GetGridCursorRow() < GetNumberRows()-1 )
5997 MoveCursorDown( event
.ShiftDown() );
6001 // at the bottom of a column
6002 HideCellEditControl();
6003 SaveEditControlValue();
6013 if (event
.ShiftDown())
6015 if ( GetGridCursorCol() > 0 )
6017 MoveCursorLeft( FALSE
);
6022 HideCellEditControl();
6023 SaveEditControlValue();
6028 if ( GetGridCursorCol() < GetNumberCols()-1 )
6030 MoveCursorRight( FALSE
);
6035 HideCellEditControl();
6036 SaveEditControlValue();
6042 if ( event
.ControlDown() )
6044 MakeCellVisible( 0, 0 );
6045 SetCurrentCell( 0, 0 );
6054 if ( event
.ControlDown() )
6056 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6057 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6074 if ( event
.ControlDown() )
6078 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6079 m_currentCellCoords
.GetCol(),
6080 event
.ControlDown(),
6087 if ( !IsEditable() )
6089 MoveCursorRight( FALSE
);
6092 // Otherwise fall through to default
6095 // is it possible to edit the current cell at all?
6096 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6098 // yes, now check whether the cells editor accepts the key
6099 int row
= m_currentCellCoords
.GetRow();
6100 int col
= m_currentCellCoords
.GetCol();
6101 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6102 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6104 // <F2> is special and will always start editing, for
6105 // other keys - ask the editor itself
6106 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6107 || editor
->IsAcceptedKey(event
) )
6109 EnableCellEditControl();
6111 // the editor could be not shown for a variety of
6112 // reasons (i.e. blocked by the app or whatever), so
6113 // check if it really was created
6114 if ( m_cellEditCtrlEnabled
)
6116 editor
->StartingKey(event
);
6129 // let others process char events with modifiers or all
6130 // char events for readonly cells
6137 m_inOnKeyDown
= FALSE
;
6140 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6142 // try local handlers
6144 if ( event
.KeyCode() == WXK_SHIFT
)
6146 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6147 m_selectingBottomRight
!= wxGridNoCellCoords
)
6151 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6152 m_selectingTopLeft
.GetCol(),
6153 m_selectingBottomRight
.GetRow(),
6154 m_selectingBottomRight
.GetCol(),
6155 event
.ControlDown(),
6162 m_selectingTopLeft
= wxGridNoCellCoords
;
6163 m_selectingBottomRight
= wxGridNoCellCoords
;
6164 m_selectingKeyboard
= wxGridNoCellCoords
;
6168 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6172 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6174 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6176 // the event has been intercepted - do nothing
6180 wxClientDC
dc(m_gridWin
);
6183 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6185 HideCellEditControl();
6186 DisableCellEditControl();
6188 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6191 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6192 if ( !m_gridLinesEnabled
)
6200 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6202 // Otherwise refresh redraws the highlight!
6203 m_currentCellCoords
= coords
;
6205 DrawGridCellArea(dc
,cells
);
6206 DrawAllGridLines( dc
, r
);
6210 m_currentCellCoords
= coords
;
6212 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6213 DrawCellHighlight(dc
, attr
);
6218 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6221 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6225 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6228 rightCol
= GetNumberCols() - 1;
6230 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6233 bottomRow
= GetNumberRows() - 1;
6237 if ( topRow
> bottomRow
)
6244 if ( leftCol
> rightCol
)
6251 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6252 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6254 // First the case that we selected a completely new area
6255 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6256 m_selectingBottomRight
== wxGridNoCellCoords
)
6259 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6260 wxGridCellCoords ( bottomRow
, rightCol
) );
6261 m_gridWin
->Refresh( FALSE
, &rect
);
6263 // Now handle changing an existing selection area.
6264 else if ( m_selectingTopLeft
!= updateTopLeft
||
6265 m_selectingBottomRight
!= updateBottomRight
)
6267 // Compute two optimal update rectangles:
6268 // Either one rectangle is a real subset of the
6269 // other, or they are (almost) disjoint!
6271 bool need_refresh
[4];
6275 need_refresh
[3] = FALSE
;
6278 // Store intermediate values
6279 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6280 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6281 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6282 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6284 // Determine the outer/inner coordinates.
6285 if (oldLeft
> leftCol
)
6291 if (oldTop
> topRow
)
6297 if (oldRight
< rightCol
)
6300 oldRight
= rightCol
;
6303 if (oldBottom
< bottomRow
)
6306 oldBottom
= bottomRow
;
6310 // Now, either the stuff marked old is the outer
6311 // rectangle or we don't have a situation where one
6312 // is contained in the other.
6314 if ( oldLeft
< leftCol
)
6316 // Refresh the newly selected or deselected
6317 // area to the left of the old or new selection.
6318 need_refresh
[0] = TRUE
;
6319 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6321 wxGridCellCoords ( oldBottom
,
6325 if ( oldTop
< topRow
)
6327 // Refresh the newly selected or deselected
6328 // area above the old or new selection.
6329 need_refresh
[1] = TRUE
;
6330 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6332 wxGridCellCoords ( topRow
- 1,
6336 if ( oldRight
> rightCol
)
6338 // Refresh the newly selected or deselected
6339 // area to the right of the old or new selection.
6340 need_refresh
[2] = TRUE
;
6341 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6343 wxGridCellCoords ( oldBottom
,
6347 if ( oldBottom
> bottomRow
)
6349 // Refresh the newly selected or deselected
6350 // area below the old or new selection.
6351 need_refresh
[3] = TRUE
;
6352 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6354 wxGridCellCoords ( oldBottom
,
6358 // various Refresh() calls
6359 for (i
= 0; i
< 4; i
++ )
6360 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6361 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6364 m_selectingTopLeft
= updateTopLeft
;
6365 m_selectingBottomRight
= updateBottomRight
;
6369 // ------ functions to get/send data (see also public functions)
6372 bool wxGrid::GetModelValues()
6374 // Hide the editor, so it won't hide a changed value.
6375 HideCellEditControl();
6379 // all we need to do is repaint the grid
6381 m_gridWin
->Refresh();
6389 bool wxGrid::SetModelValues()
6393 // Disable the editor, so it won't hide a changed value.
6394 // Do we also want to save the current value of the editor first?
6396 DisableCellEditControl();
6400 for ( row
= 0; row
< m_numRows
; row
++ )
6402 for ( col
= 0; col
< m_numCols
; col
++ )
6404 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6416 // Note - this function only draws cells that are in the list of
6417 // exposed cells (usually set from the update region by
6418 // CalcExposedCells)
6420 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6422 if ( !m_numRows
|| !m_numCols
) return;
6424 int i
, numCells
= cells
.GetCount();
6425 int row
, col
, cell_rows
, cell_cols
;
6426 wxGridCellCoordsArray redrawCells
;
6428 for ( i
= numCells
-1; i
>= 0; i
-- )
6430 row
= cells
[i
].GetRow();
6431 col
= cells
[i
].GetCol();
6432 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6434 // If this cell is part of a multicell block, find owner for repaint
6435 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6437 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6438 bool marked
= FALSE
;
6439 for ( int j
= 0; j
< numCells
; j
++ )
6441 if ( cell
== cells
[j
] )
6449 int count
= redrawCells
.GetCount();
6450 for (int j
= 0; j
< count
; j
++)
6452 if ( cell
== redrawCells
[j
] )
6458 if (!marked
) redrawCells
.Add( cell
);
6460 continue; // don't bother drawing this cell
6463 // If this cell is empty, find cell to left that might want to overflow
6464 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6466 for ( int l
= 0; l
< cell_rows
; l
++ )
6468 // find a cell in this row to left alreay marked for repaint
6470 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6471 if ((redrawCells
[k
].GetCol() < left
) &&
6472 (redrawCells
[k
].GetRow() == row
))
6473 left
=redrawCells
[k
].GetCol();
6475 if (left
== col
) left
= 0; // oh well
6477 for (int j
= col
-1; j
>= left
; j
--)
6479 if (!m_table
->IsEmptyCell(row
+l
, j
))
6481 if (GetCellOverflow(row
+l
, j
))
6483 wxGridCellCoords
cell(row
+l
, j
);
6484 bool marked
= FALSE
;
6486 for (int k
= 0; k
< numCells
; k
++)
6488 if ( cell
== cells
[k
] )
6496 int count
= redrawCells
.GetCount();
6497 for (int k
= 0; k
< count
; k
++)
6499 if ( cell
== redrawCells
[k
] )
6505 if (!marked
) redrawCells
.Add( cell
);
6513 DrawCell( dc
, cells
[i
] );
6516 numCells
= redrawCells
.GetCount();
6518 for ( i
= numCells
- 1; i
>= 0; i
-- )
6520 DrawCell( dc
, redrawCells
[i
] );
6525 void wxGrid::DrawGridSpace( wxDC
& dc
)
6528 m_gridWin
->GetClientSize( &cw
, &ch
);
6531 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6533 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6534 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6536 if ( right
> rightCol
|| bottom
> bottomRow
)
6539 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6541 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6542 dc
.SetPen( *wxTRANSPARENT_PEN
);
6544 if ( right
> rightCol
)
6546 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6549 if ( bottom
> bottomRow
)
6551 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6557 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6559 int row
= coords
.GetRow();
6560 int col
= coords
.GetCol();
6562 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6565 // we draw the cell border ourselves
6566 #if !WXGRID_DRAW_LINES
6567 if ( m_gridLinesEnabled
)
6568 DrawCellBorder( dc
, coords
);
6571 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6573 bool isCurrent
= coords
== m_currentCellCoords
;
6575 wxRect rect
= CellToRect( row
, col
);
6577 // if the editor is shown, we should use it and not the renderer
6578 // Note: However, only if it is really _shown_, i.e. not hidden!
6579 if ( isCurrent
&& IsCellEditControlShown() )
6581 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6582 editor
->PaintBackground(rect
, attr
);
6587 // but all the rest is drawn by the cell renderer and hence may be
6589 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6590 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6597 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6599 int row
= m_currentCellCoords
.GetRow();
6600 int col
= m_currentCellCoords
.GetCol();
6602 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6605 wxRect rect
= CellToRect(row
, col
);
6607 // hmmm... what could we do here to show that the cell is disabled?
6608 // for now, I just draw a thinner border than for the other ones, but
6609 // it doesn't look really good
6611 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6615 // The center of th drawn line is where the position/width/height of
6616 // the rectangle is actually at, (on wxMSW atr least,) so we will
6617 // reduce the size of the rectangle to compensate for the thickness of
6618 // the line. If this is too strange on non wxMSW platforms then
6619 // please #ifdef this appropriately.
6620 rect
.x
+= penWidth
/2;
6621 rect
.y
+= penWidth
/2;
6622 rect
.width
-= penWidth
-1;
6623 rect
.height
-= penWidth
-1;
6626 // Now draw the rectangle
6627 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6628 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6629 dc
.DrawRectangle(rect
);
6633 // VZ: my experiments with 3d borders...
6635 // how to properly set colours for arbitrary bg?
6636 wxCoord x1
= rect
.x
,
6638 x2
= rect
.x
+ rect
.width
-1,
6639 y2
= rect
.y
+ rect
.height
-1;
6641 dc
.SetPen(*wxWHITE_PEN
);
6642 dc
.DrawLine(x1
, y1
, x2
, y1
);
6643 dc
.DrawLine(x1
, y1
, x1
, y2
);
6645 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6646 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6648 dc
.SetPen(*wxBLACK_PEN
);
6649 dc
.DrawLine(x1
, y2
, x2
, y2
);
6650 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6655 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6657 int row
= coords
.GetRow();
6658 int col
= coords
.GetCol();
6659 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6662 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6664 wxRect rect
= CellToRect( row
, col
);
6666 // right hand border
6668 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6669 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6673 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6674 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6677 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6679 // This if block was previously in wxGrid::OnPaint but that doesn't
6680 // seem to get called under wxGTK - MB
6682 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6683 m_numRows
&& m_numCols
)
6685 m_currentCellCoords
.Set(0, 0);
6688 if ( IsCellEditControlShown() )
6690 // don't show highlight when the edit control is shown
6694 // if the active cell was repainted, repaint its highlight too because it
6695 // might have been damaged by the grid lines
6696 size_t count
= cells
.GetCount();
6697 for ( size_t n
= 0; n
< count
; n
++ )
6699 if ( cells
[n
] == m_currentCellCoords
)
6701 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6702 DrawCellHighlight(dc
, attr
);
6710 // TODO: remove this ???
6711 // This is used to redraw all grid lines e.g. when the grid line colour
6714 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6716 #if !WXGRID_DRAW_LINES
6720 if ( !m_gridLinesEnabled
||
6722 !m_numCols
) return;
6724 int top
, bottom
, left
, right
;
6726 #if 0 //#ifndef __WXGTK__
6730 m_gridWin
->GetClientSize(&cw
, &ch
);
6732 // virtual coords of visible area
6734 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6735 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6740 reg
.GetBox(x
, y
, w
, h
);
6741 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6742 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6746 m_gridWin
->GetClientSize(&cw
, &ch
);
6747 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6748 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6751 // avoid drawing grid lines past the last row and col
6753 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6754 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6756 // no gridlines inside multicells, clip them out
6757 int leftCol
= XToCol(left
);
6758 int topRow
= YToRow(top
);
6759 int rightCol
= XToCol(right
);
6760 int bottomRow
= YToRow(bottom
);
6761 wxRegion
clippedcells(0, 0, cw
, ch
);
6763 if ((leftCol
>= 0) && (topRow
>= 0))
6765 if (rightCol
< 0) rightCol
= m_numCols
;
6766 if (bottomRow
< 0) bottomRow
= m_numRows
;
6768 int i
, j
, cell_rows
, cell_cols
;
6771 for (j
=topRow
; j
<bottomRow
; j
++)
6773 for (i
=leftCol
; i
<rightCol
; i
++)
6775 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6776 if ((cell_rows
> 1) || (cell_cols
> 1))
6778 rect
= CellToRect(j
,i
);
6779 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6780 clippedcells
.Subtract(rect
);
6782 else if ((cell_rows
< 0) || (cell_cols
< 0))
6784 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6785 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6786 clippedcells
.Subtract(rect
);
6791 dc
.SetClippingRegion( clippedcells
);
6793 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6795 // horizontal grid lines
6798 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6800 int bot
= GetRowBottom(i
) - 1;
6809 dc
.DrawLine( left
, bot
, right
, bot
);
6814 // vertical grid lines
6816 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6818 int colRight
= GetColRight(i
) - 1;
6819 if ( colRight
> right
)
6824 if ( colRight
>= left
)
6826 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6829 dc
.DestroyClippingRegion();
6833 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6835 if ( !m_numRows
) return;
6838 size_t numLabels
= rows
.GetCount();
6840 for ( i
= 0; i
< numLabels
; i
++ )
6842 DrawRowLabel( dc
, rows
[i
] );
6847 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6849 if ( GetRowHeight(row
) <= 0 )
6852 int rowTop
= GetRowTop(row
),
6853 rowBottom
= GetRowBottom(row
) - 1;
6855 dc
.SetPen( *wxBLACK_PEN
);
6856 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6857 m_rowLabelWidth
-1, rowBottom
);
6859 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6861 dc
.SetPen( *wxWHITE_PEN
);
6862 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6863 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6865 dc
.SetBackgroundMode( wxTRANSPARENT
);
6866 dc
.SetTextForeground( GetLabelTextColour() );
6867 dc
.SetFont( GetLabelFont() );
6870 GetRowLabelAlignment( &hAlign
, &vAlign
);
6874 rect
.SetY( GetRowTop(row
) + 2 );
6875 rect
.SetWidth( m_rowLabelWidth
- 4 );
6876 rect
.SetHeight( GetRowHeight(row
) - 4 );
6877 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6881 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6883 if ( !m_numCols
) return;
6886 size_t numLabels
= cols
.GetCount();
6888 for ( i
= 0; i
< numLabels
; i
++ )
6890 DrawColLabel( dc
, cols
[i
] );
6895 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6897 if ( GetColWidth(col
) <= 0 )
6900 int colLeft
= GetColLeft(col
),
6901 colRight
= GetColRight(col
) - 1;
6903 dc
.SetPen( *wxBLACK_PEN
);
6904 dc
.DrawLine( colRight
, 0,
6905 colRight
, m_colLabelHeight
-1 );
6907 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6908 colRight
, m_colLabelHeight
-1 );
6910 dc
.SetPen( *wxWHITE_PEN
);
6911 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6912 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6914 dc
.SetBackgroundMode( wxTRANSPARENT
);
6915 dc
.SetTextForeground( GetLabelTextColour() );
6916 dc
.SetFont( GetLabelFont() );
6918 dc
.SetBackgroundMode( wxTRANSPARENT
);
6919 dc
.SetTextForeground( GetLabelTextColour() );
6920 dc
.SetFont( GetLabelFont() );
6923 GetColLabelAlignment( &hAlign
, &vAlign
);
6926 rect
.SetX( colLeft
+ 2 );
6928 rect
.SetWidth( GetColWidth(col
) - 4 );
6929 rect
.SetHeight( m_colLabelHeight
- 4 );
6930 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6933 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6934 const wxString
& value
,
6939 wxArrayString lines
;
6941 StringToLines( value
, lines
);
6944 //Forward to new API.
6945 DrawTextRectangle( dc
,
6953 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6954 const wxArrayString
& lines
,
6959 long textWidth
, textHeight
;
6960 long lineWidth
, lineHeight
;
6962 dc
.SetClippingRegion( rect
);
6963 if ( lines
.GetCount() )
6965 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6966 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6969 switch ( horizAlign
)
6972 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6975 case wxALIGN_CENTRE
:
6976 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6985 switch ( vertAlign
)
6987 case wxALIGN_BOTTOM
:
6988 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6991 case wxALIGN_CENTRE
:
6992 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7001 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
7003 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
7008 dc
.DestroyClippingRegion();
7012 // Split multi line text up into an array of strings. Any existing
7013 // contents of the string array are preserved.
7015 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7019 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7020 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7022 while ( startPos
< (int)tVal
.Length() )
7024 pos
= tVal
.Mid(startPos
).Find( eol
);
7029 else if ( pos
== 0 )
7031 lines
.Add( wxEmptyString
);
7035 lines
.Add( value
.Mid(startPos
, pos
) );
7039 if ( startPos
< (int)value
.Length() )
7041 lines
.Add( value
.Mid( startPos
) );
7046 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7047 const wxArrayString
& lines
,
7048 long *width
, long *height
)
7055 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7057 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7058 w
= wxMax( w
, lineW
);
7067 // ------ Batch processing.
7069 void wxGrid::EndBatch()
7071 if ( m_batchCount
> 0 )
7074 if ( !m_batchCount
)
7077 m_rowLabelWin
->Refresh();
7078 m_colLabelWin
->Refresh();
7079 m_cornerLabelWin
->Refresh();
7080 m_gridWin
->Refresh();
7085 // Use this, rather than wxWindow::Refresh(), to force an immediate
7086 // repainting of the grid. Has no effect if you are already inside a
7087 // BeginBatch / EndBatch block.
7089 void wxGrid::ForceRefresh()
7097 // ------ Edit control functions
7101 void wxGrid::EnableEditing( bool edit
)
7103 // TODO: improve this ?
7105 if ( edit
!= m_editable
)
7107 if(!edit
) EnableCellEditControl(edit
);
7113 void wxGrid::EnableCellEditControl( bool enable
)
7118 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7119 SetCurrentCell( 0, 0 );
7121 if ( enable
!= m_cellEditCtrlEnabled
)
7125 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7128 // this should be checked by the caller!
7129 wxASSERT_MSG( CanEnableCellControl(),
7130 _T("can't enable editing for this cell!") );
7132 // do it before ShowCellEditControl()
7133 m_cellEditCtrlEnabled
= enable
;
7135 ShowCellEditControl();
7139 //FIXME:add veto support
7140 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7142 HideCellEditControl();
7143 SaveEditControlValue();
7145 // do it after HideCellEditControl()
7146 m_cellEditCtrlEnabled
= enable
;
7151 bool wxGrid::IsCurrentCellReadOnly() const
7154 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7155 bool readonly
= attr
->IsReadOnly();
7161 bool wxGrid::CanEnableCellControl() const
7163 return m_editable
&& !IsCurrentCellReadOnly();
7166 bool wxGrid::IsCellEditControlEnabled() const
7168 // the cell edit control might be disable for all cells or just for the
7169 // current one if it's read only
7170 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7173 bool wxGrid::IsCellEditControlShown() const
7175 bool isShown
= FALSE
;
7177 if ( m_cellEditCtrlEnabled
)
7179 int row
= m_currentCellCoords
.GetRow();
7180 int col
= m_currentCellCoords
.GetCol();
7181 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7182 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7187 if ( editor
->IsCreated() )
7189 isShown
= editor
->GetControl()->IsShown();
7199 void wxGrid::ShowCellEditControl()
7201 if ( IsCellEditControlEnabled() )
7203 if ( !IsVisible( m_currentCellCoords
) )
7205 m_cellEditCtrlEnabled
= FALSE
;
7210 wxRect rect
= CellToRect( m_currentCellCoords
);
7211 int row
= m_currentCellCoords
.GetRow();
7212 int col
= m_currentCellCoords
.GetCol();
7214 // if this is part of a multicell, find owner (topleft)
7215 int cell_rows
, cell_cols
;
7216 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7217 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7221 m_currentCellCoords
.SetRow( row
);
7222 m_currentCellCoords
.SetCol( col
);
7225 // convert to scrolled coords
7227 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7229 // done in PaintBackground()
7231 // erase the highlight and the cell contents because the editor
7232 // might not cover the entire cell
7233 wxClientDC
dc( m_gridWin
);
7235 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7236 dc
.SetPen(*wxTRANSPARENT_PEN
);
7237 dc
.DrawRectangle(rect
);
7240 // cell is shifted by one pixel
7241 // However, don't allow x or y to become negative
7242 // since the SetSize() method interprets that as
7249 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7250 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7251 if ( !editor
->IsCreated() )
7253 editor
->Create(m_gridWin
, -1,
7254 new wxGridCellEditorEvtHandler(this, editor
));
7256 wxGridEditorCreatedEvent
evt(GetId(),
7257 wxEVT_GRID_EDITOR_CREATED
,
7261 editor
->GetControl());
7262 GetEventHandler()->ProcessEvent(evt
);
7265 editor
->Show( TRUE
, attr
);
7267 // resize editor to overflow into righthand cells if allowed
7268 int maxWidth
= rect
.width
;
7269 wxString value
= GetCellValue(row
, col
);
7270 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7273 GetTextExtent(value
, &maxWidth
, &y
,
7274 NULL
, NULL
, &attr
->GetFont());
7275 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7277 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7278 if (rect
.x
+maxWidth
> client_right
)
7279 maxWidth
= client_right
- rect
.x
;
7281 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7283 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7284 // may have changed earlier
7285 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7288 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7289 // looks weird going over a multicell
7290 if (m_table
->IsEmptyCell(row
,i
) &&
7291 (rect
.width
< maxWidth
) && (c_rows
== 1))
7292 rect
.width
+= GetColWidth(i
);
7296 if (rect
.GetRight() > client_right
)
7297 rect
.SetRight(client_right
-1);
7299 editor
->SetSize( rect
);
7301 editor
->BeginEdit(row
, col
, this);
7310 void wxGrid::HideCellEditControl()
7312 if ( IsCellEditControlEnabled() )
7314 int row
= m_currentCellCoords
.GetRow();
7315 int col
= m_currentCellCoords
.GetCol();
7317 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7318 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7319 editor
->Show( FALSE
);
7322 m_gridWin
->SetFocus();
7323 // refresh whole row to the right
7324 wxRect
rect( CellToRect(row
, col
) );
7325 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7326 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7327 m_gridWin
->Refresh( FALSE
, &rect
);
7332 void wxGrid::SaveEditControlValue()
7334 if ( IsCellEditControlEnabled() )
7336 int row
= m_currentCellCoords
.GetRow();
7337 int col
= m_currentCellCoords
.GetCol();
7339 wxString oldval
= GetCellValue(row
,col
);
7341 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7342 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7343 bool changed
= editor
->EndEdit(row
, col
, this);
7350 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7351 m_currentCellCoords
.GetRow(),
7352 m_currentCellCoords
.GetCol() ) < 0 ) {
7354 // Event has been vetoed, set the data back.
7355 SetCellValue(row
,col
,oldval
);
7363 // ------ Grid location functions
7364 // Note that all of these functions work with the logical coordinates of
7365 // grid cells and labels so you will need to convert from device
7366 // coordinates for mouse events etc.
7369 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7371 int row
= YToRow(y
);
7372 int col
= XToCol(x
);
7374 if ( row
== -1 || col
== -1 )
7376 coords
= wxGridNoCellCoords
;
7380 coords
.Set( row
, col
);
7385 // Internal Helper function for computing row or column from some
7386 // (unscrolled) coordinate value, using either
7387 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7388 // of m_rowBottoms/m_ColRights to speed up the search!
7390 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7391 const wxArrayInt
& BorderArray
, int nMax
,
7396 size_t i_max
= coord
/ defaultDist
,
7399 if (BorderArray
.IsEmpty())
7401 if((int) i_max
< nMax
)
7403 return maxOnOverflow
? nMax
- 1 : -1;
7406 if ( i_max
>= BorderArray
.GetCount())
7407 i_max
= BorderArray
.GetCount() - 1;
7410 if ( coord
>= BorderArray
[i_max
])
7413 i_max
= coord
/ minDist
;
7415 if ( i_max
>= BorderArray
.GetCount())
7416 i_max
= BorderArray
.GetCount() - 1;
7418 if ( coord
>= BorderArray
[i_max
])
7419 return maxOnOverflow
? (int)i_max
: -1;
7420 if ( coord
< BorderArray
[0] )
7423 while ( i_max
- i_min
> 0 )
7425 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7426 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7427 if (coord
>= BorderArray
[ i_max
- 1])
7431 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7432 if (coord
< BorderArray
[median
])
7440 int wxGrid::YToRow( int y
)
7442 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7443 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7447 int wxGrid::XToCol( int x
)
7449 return CoordToRowOrCol(x
, m_defaultColWidth
,
7450 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7454 // return the row number that that the y coord is near the edge of, or
7455 // -1 if not near an edge
7457 int wxGrid::YToEdgeOfRow( int y
)
7460 i
= internalYToRow(y
);
7462 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7464 // We know that we are in row i, test whether we are
7465 // close enough to lower or upper border, respectively.
7466 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7468 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7476 // return the col number that that the x coord is near the edge of, or
7477 // -1 if not near an edge
7479 int wxGrid::XToEdgeOfCol( int x
)
7482 i
= internalXToCol(x
);
7484 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7486 // We know that we are in column i, test whether we are
7487 // close enough to right or left border, respectively.
7488 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7490 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7498 wxRect
wxGrid::CellToRect( int row
, int col
)
7500 wxRect
rect( -1, -1, -1, -1 );
7502 if ( row
>= 0 && row
< m_numRows
&&
7503 col
>= 0 && col
< m_numCols
)
7505 int i
, cell_rows
, cell_cols
;
7506 rect
.width
= rect
.height
= 0;
7507 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7508 // if negative then find multicell owner
7509 if (cell_rows
< 0) row
+= cell_rows
;
7510 if (cell_cols
< 0) col
+= cell_cols
;
7511 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7513 rect
.x
= GetColLeft(col
);
7514 rect
.y
= GetRowTop(row
);
7515 for (i
=col
; i
<col
+cell_cols
; i
++)
7516 rect
.width
+= GetColWidth(i
);
7517 for (i
=row
; i
<row
+cell_rows
; i
++)
7518 rect
.height
+= GetRowHeight(i
);
7521 // if grid lines are enabled, then the area of the cell is a bit smaller
7522 if (m_gridLinesEnabled
) {
7530 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7532 // get the cell rectangle in logical coords
7534 wxRect
r( CellToRect( row
, col
) );
7536 // convert to device coords
7538 int left
, top
, right
, bottom
;
7539 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7540 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7542 // check against the client area of the grid window
7545 m_gridWin
->GetClientSize( &cw
, &ch
);
7547 if ( wholeCellVisible
)
7549 // is the cell wholly visible ?
7551 return ( left
>= 0 && right
<= cw
&&
7552 top
>= 0 && bottom
<= ch
);
7556 // is the cell partly visible ?
7558 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7559 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7564 // make the specified cell location visible by doing a minimal amount
7567 void wxGrid::MakeCellVisible( int row
, int col
)
7571 int xpos
= -1, ypos
= -1;
7573 if ( row
>= 0 && row
< m_numRows
&&
7574 col
>= 0 && col
< m_numCols
)
7576 // get the cell rectangle in logical coords
7578 wxRect
r( CellToRect( row
, col
) );
7580 // convert to device coords
7582 int left
, top
, right
, bottom
;
7583 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7584 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7587 m_gridWin
->GetClientSize( &cw
, &ch
);
7593 else if ( bottom
> ch
)
7595 int h
= r
.GetHeight();
7597 for ( i
= row
-1; i
>= 0; i
-- )
7599 int rowHeight
= GetRowHeight(i
);
7600 if ( h
+ rowHeight
> ch
)
7607 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7608 // have rounding errors (this is important, because if we do, we
7609 // might not scroll at all and some cells won't be redrawn)
7611 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7613 ypos
+= GRID_SCROLL_LINE_Y
;
7620 else if ( right
> cw
)
7622 int w
= r
.GetWidth();
7624 for ( i
= col
-1; i
>= 0; i
-- )
7626 int colWidth
= GetColWidth(i
);
7627 if ( w
+ colWidth
> cw
)
7634 // see comment for ypos above
7635 xpos
+= GRID_SCROLL_LINE_X
;
7638 if ( xpos
!= -1 || ypos
!= -1 )
7641 xpos
/= GRID_SCROLL_LINE_X
;
7643 ypos
/= GRID_SCROLL_LINE_Y
;
7644 Scroll( xpos
, ypos
);
7652 // ------ Grid cursor movement functions
7655 bool wxGrid::MoveCursorUp( bool expandSelection
)
7657 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7658 m_currentCellCoords
.GetRow() >= 0 )
7660 if ( expandSelection
)
7662 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7663 m_selectingKeyboard
= m_currentCellCoords
;
7664 if ( m_selectingKeyboard
.GetRow() > 0 )
7666 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7667 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7668 m_selectingKeyboard
.GetCol() );
7669 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7672 else if ( m_currentCellCoords
.GetRow() > 0 )
7675 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7676 m_currentCellCoords
.GetCol() );
7677 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7678 m_currentCellCoords
.GetCol() );
7689 bool wxGrid::MoveCursorDown( bool expandSelection
)
7691 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7692 m_currentCellCoords
.GetRow() < m_numRows
)
7694 if ( expandSelection
)
7696 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7697 m_selectingKeyboard
= m_currentCellCoords
;
7698 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7700 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7701 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7702 m_selectingKeyboard
.GetCol() );
7703 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7706 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7709 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7710 m_currentCellCoords
.GetCol() );
7711 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7712 m_currentCellCoords
.GetCol() );
7723 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7725 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7726 m_currentCellCoords
.GetCol() >= 0 )
7728 if ( expandSelection
)
7730 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7731 m_selectingKeyboard
= m_currentCellCoords
;
7732 if ( m_selectingKeyboard
.GetCol() > 0 )
7734 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7735 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7736 m_selectingKeyboard
.GetCol() );
7737 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7740 else if ( m_currentCellCoords
.GetCol() > 0 )
7743 MakeCellVisible( m_currentCellCoords
.GetRow(),
7744 m_currentCellCoords
.GetCol() - 1 );
7745 SetCurrentCell( m_currentCellCoords
.GetRow(),
7746 m_currentCellCoords
.GetCol() - 1 );
7757 bool wxGrid::MoveCursorRight( bool expandSelection
)
7759 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7760 m_currentCellCoords
.GetCol() < m_numCols
)
7762 if ( expandSelection
)
7764 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7765 m_selectingKeyboard
= m_currentCellCoords
;
7766 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7768 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7769 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7770 m_selectingKeyboard
.GetCol() );
7771 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7774 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7777 MakeCellVisible( m_currentCellCoords
.GetRow(),
7778 m_currentCellCoords
.GetCol() + 1 );
7779 SetCurrentCell( m_currentCellCoords
.GetRow(),
7780 m_currentCellCoords
.GetCol() + 1 );
7791 bool wxGrid::MovePageUp()
7793 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7795 int row
= m_currentCellCoords
.GetRow();
7799 m_gridWin
->GetClientSize( &cw
, &ch
);
7801 int y
= GetRowTop(row
);
7802 int newRow
= YToRow( y
- ch
+ 1 );
7807 else if ( newRow
== row
)
7812 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7813 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7821 bool wxGrid::MovePageDown()
7823 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7825 int row
= m_currentCellCoords
.GetRow();
7826 if ( row
< m_numRows
)
7829 m_gridWin
->GetClientSize( &cw
, &ch
);
7831 int y
= GetRowTop(row
);
7832 int newRow
= YToRow( y
+ ch
);
7835 newRow
= m_numRows
- 1;
7837 else if ( newRow
== row
)
7842 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7843 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7851 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7854 m_currentCellCoords
!= wxGridNoCellCoords
&&
7855 m_currentCellCoords
.GetRow() > 0 )
7857 int row
= m_currentCellCoords
.GetRow();
7858 int col
= m_currentCellCoords
.GetCol();
7860 if ( m_table
->IsEmptyCell(row
, col
) )
7862 // starting in an empty cell: find the next block of
7868 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7871 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7873 // starting at the top of a block: find the next block
7879 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7884 // starting within a block: find the top of the block
7889 if ( m_table
->IsEmptyCell(row
, col
) )
7897 MakeCellVisible( row
, col
);
7898 if ( expandSelection
)
7900 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7901 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7906 SetCurrentCell( row
, col
);
7914 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7917 m_currentCellCoords
!= wxGridNoCellCoords
&&
7918 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7920 int row
= m_currentCellCoords
.GetRow();
7921 int col
= m_currentCellCoords
.GetCol();
7923 if ( m_table
->IsEmptyCell(row
, col
) )
7925 // starting in an empty cell: find the next block of
7928 while ( row
< m_numRows
-1 )
7931 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7934 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7936 // starting at the bottom of a block: find the next block
7939 while ( row
< m_numRows
-1 )
7942 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7947 // starting within a block: find the bottom of the block
7949 while ( row
< m_numRows
-1 )
7952 if ( m_table
->IsEmptyCell(row
, col
) )
7960 MakeCellVisible( row
, col
);
7961 if ( expandSelection
)
7963 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7964 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7969 SetCurrentCell( row
, col
);
7978 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7981 m_currentCellCoords
!= wxGridNoCellCoords
&&
7982 m_currentCellCoords
.GetCol() > 0 )
7984 int row
= m_currentCellCoords
.GetRow();
7985 int col
= m_currentCellCoords
.GetCol();
7987 if ( m_table
->IsEmptyCell(row
, col
) )
7989 // starting in an empty cell: find the next block of
7995 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7998 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8000 // starting at the left of a block: find the next block
8006 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8011 // starting within a block: find the left of the block
8016 if ( m_table
->IsEmptyCell(row
, col
) )
8024 MakeCellVisible( row
, col
);
8025 if ( expandSelection
)
8027 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8028 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8033 SetCurrentCell( row
, col
);
8042 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8045 m_currentCellCoords
!= wxGridNoCellCoords
&&
8046 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8048 int row
= m_currentCellCoords
.GetRow();
8049 int col
= m_currentCellCoords
.GetCol();
8051 if ( m_table
->IsEmptyCell(row
, col
) )
8053 // starting in an empty cell: find the next block of
8056 while ( col
< m_numCols
-1 )
8059 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8062 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8064 // starting at the right of a block: find the next block
8067 while ( col
< m_numCols
-1 )
8070 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8075 // starting within a block: find the right of the block
8077 while ( col
< m_numCols
-1 )
8080 if ( m_table
->IsEmptyCell(row
, col
) )
8088 MakeCellVisible( row
, col
);
8089 if ( expandSelection
)
8091 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8092 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8097 SetCurrentCell( row
, col
);
8109 // ------ Label values and formatting
8112 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8114 *horiz
= m_rowLabelHorizAlign
;
8115 *vert
= m_rowLabelVertAlign
;
8118 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8120 *horiz
= m_colLabelHorizAlign
;
8121 *vert
= m_colLabelVertAlign
;
8124 wxString
wxGrid::GetRowLabelValue( int row
)
8128 return m_table
->GetRowLabelValue( row
);
8138 wxString
wxGrid::GetColLabelValue( int col
)
8142 return m_table
->GetColLabelValue( col
);
8153 void wxGrid::SetRowLabelSize( int width
)
8155 width
= wxMax( width
, 0 );
8156 if ( width
!= m_rowLabelWidth
)
8160 m_rowLabelWin
->Show( FALSE
);
8161 m_cornerLabelWin
->Show( FALSE
);
8163 else if ( m_rowLabelWidth
== 0 )
8165 m_rowLabelWin
->Show( TRUE
);
8166 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8169 m_rowLabelWidth
= width
;
8171 wxScrolledWindow::Refresh( TRUE
);
8176 void wxGrid::SetColLabelSize( int height
)
8178 height
= wxMax( height
, 0 );
8179 if ( height
!= m_colLabelHeight
)
8183 m_colLabelWin
->Show( FALSE
);
8184 m_cornerLabelWin
->Show( FALSE
);
8186 else if ( m_colLabelHeight
== 0 )
8188 m_colLabelWin
->Show( TRUE
);
8189 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8192 m_colLabelHeight
= height
;
8194 wxScrolledWindow::Refresh( TRUE
);
8199 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8201 if ( m_labelBackgroundColour
!= colour
)
8203 m_labelBackgroundColour
= colour
;
8204 m_rowLabelWin
->SetBackgroundColour( colour
);
8205 m_colLabelWin
->SetBackgroundColour( colour
);
8206 m_cornerLabelWin
->SetBackgroundColour( colour
);
8208 if ( !GetBatchCount() )
8210 m_rowLabelWin
->Refresh();
8211 m_colLabelWin
->Refresh();
8212 m_cornerLabelWin
->Refresh();
8217 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8219 if ( m_labelTextColour
!= colour
)
8221 m_labelTextColour
= colour
;
8222 if ( !GetBatchCount() )
8224 m_rowLabelWin
->Refresh();
8225 m_colLabelWin
->Refresh();
8230 void wxGrid::SetLabelFont( const wxFont
& font
)
8233 if ( !GetBatchCount() )
8235 m_rowLabelWin
->Refresh();
8236 m_colLabelWin
->Refresh();
8240 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8242 // allow old (incorrect) defs to be used
8245 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8246 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8247 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8252 case wxTOP
: vert
= wxALIGN_TOP
; break;
8253 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8254 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8257 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8259 m_rowLabelHorizAlign
= horiz
;
8262 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8264 m_rowLabelVertAlign
= vert
;
8267 if ( !GetBatchCount() )
8269 m_rowLabelWin
->Refresh();
8273 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8275 // allow old (incorrect) defs to be used
8278 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8279 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8280 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8285 case wxTOP
: vert
= wxALIGN_TOP
; break;
8286 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8287 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8290 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8292 m_colLabelHorizAlign
= horiz
;
8295 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8297 m_colLabelVertAlign
= vert
;
8300 if ( !GetBatchCount() )
8302 m_colLabelWin
->Refresh();
8306 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8310 m_table
->SetRowLabelValue( row
, s
);
8311 if ( !GetBatchCount() )
8313 wxRect rect
= CellToRect( row
, 0);
8314 if ( rect
.height
> 0 )
8316 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8318 rect
.width
= m_rowLabelWidth
;
8319 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8325 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8329 m_table
->SetColLabelValue( col
, s
);
8330 if ( !GetBatchCount() )
8332 wxRect rect
= CellToRect( 0, col
);
8333 if ( rect
.width
> 0 )
8335 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8337 rect
.height
= m_colLabelHeight
;
8338 m_colLabelWin
->Refresh( TRUE
, &rect
);
8344 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8346 if ( m_gridLineColour
!= colour
)
8348 m_gridLineColour
= colour
;
8350 wxClientDC
dc( m_gridWin
);
8352 DrawAllGridLines( dc
, wxRegion() );
8357 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8359 if ( m_cellHighlightColour
!= colour
)
8361 m_cellHighlightColour
= colour
;
8363 wxClientDC
dc( m_gridWin
);
8365 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8366 DrawCellHighlight(dc
, attr
);
8371 void wxGrid::SetCellHighlightPenWidth(int width
)
8373 if (m_cellHighlightPenWidth
!= width
) {
8374 m_cellHighlightPenWidth
= width
;
8376 // Just redrawing the cell highlight is not enough since that won't
8377 // make any visible change if the the thickness is getting smaller.
8378 int row
= m_currentCellCoords
.GetRow();
8379 int col
= m_currentCellCoords
.GetCol();
8380 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8382 wxRect rect
= CellToRect(row
, col
);
8383 m_gridWin
->Refresh(TRUE
, &rect
);
8387 void wxGrid::SetCellHighlightROPenWidth(int width
)
8389 if (m_cellHighlightROPenWidth
!= width
) {
8390 m_cellHighlightROPenWidth
= width
;
8392 // Just redrawing the cell highlight is not enough since that won't
8393 // make any visible change if the the thickness is getting smaller.
8394 int row
= m_currentCellCoords
.GetRow();
8395 int col
= m_currentCellCoords
.GetCol();
8396 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8398 wxRect rect
= CellToRect(row
, col
);
8399 m_gridWin
->Refresh(TRUE
, &rect
);
8403 void wxGrid::EnableGridLines( bool enable
)
8405 if ( enable
!= m_gridLinesEnabled
)
8407 m_gridLinesEnabled
= enable
;
8409 if ( !GetBatchCount() )
8413 wxClientDC
dc( m_gridWin
);
8415 DrawAllGridLines( dc
, wxRegion() );
8419 m_gridWin
->Refresh();
8426 int wxGrid::GetDefaultRowSize()
8428 return m_defaultRowHeight
;
8431 int wxGrid::GetRowSize( int row
)
8433 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8435 return GetRowHeight(row
);
8438 int wxGrid::GetDefaultColSize()
8440 return m_defaultColWidth
;
8443 int wxGrid::GetColSize( int col
)
8445 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8447 return GetColWidth(col
);
8450 // ============================================================================
8451 // access to the grid attributes: each of them has a default value in the grid
8452 // itself and may be overidden on a per-cell basis
8453 // ============================================================================
8455 // ----------------------------------------------------------------------------
8456 // setting default attributes
8457 // ----------------------------------------------------------------------------
8459 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8461 m_defaultCellAttr
->SetBackgroundColour(col
);
8463 m_gridWin
->SetBackgroundColour(col
);
8467 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8469 m_defaultCellAttr
->SetTextColour(col
);
8472 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8474 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8477 void wxGrid::SetDefaultCellOverflow( bool allow
)
8479 m_defaultCellAttr
->SetOverflow(allow
);
8482 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8484 m_defaultCellAttr
->SetFont(font
);
8487 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8489 m_defaultCellAttr
->SetRenderer(renderer
);
8492 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8494 m_defaultCellAttr
->SetEditor(editor
);
8497 // ----------------------------------------------------------------------------
8498 // access to the default attrbiutes
8499 // ----------------------------------------------------------------------------
8501 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8503 return m_defaultCellAttr
->GetBackgroundColour();
8506 wxColour
wxGrid::GetDefaultCellTextColour()
8508 return m_defaultCellAttr
->GetTextColour();
8511 wxFont
wxGrid::GetDefaultCellFont()
8513 return m_defaultCellAttr
->GetFont();
8516 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8518 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8521 bool wxGrid::GetDefaultCellOverflow()
8523 return m_defaultCellAttr
->GetOverflow();
8526 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8528 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8531 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8533 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8536 // ----------------------------------------------------------------------------
8537 // access to cell attributes
8538 // ----------------------------------------------------------------------------
8540 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8542 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8543 wxColour colour
= attr
->GetBackgroundColour();
8548 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8550 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8551 wxColour colour
= attr
->GetTextColour();
8556 wxFont
wxGrid::GetCellFont( int row
, int col
)
8558 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8559 wxFont font
= attr
->GetFont();
8564 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8566 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8567 attr
->GetAlignment(horiz
, vert
);
8571 bool wxGrid::GetCellOverflow( int row
, int col
)
8573 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8574 bool allow
= attr
->GetOverflow();
8579 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8581 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8582 attr
->GetSize( num_rows
, num_cols
);
8586 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8588 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8589 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8595 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8597 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8598 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8604 bool wxGrid::IsReadOnly(int row
, int col
) const
8606 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8607 bool isReadOnly
= attr
->IsReadOnly();
8612 // ----------------------------------------------------------------------------
8613 // attribute support: cache, automatic provider creation, ...
8614 // ----------------------------------------------------------------------------
8616 bool wxGrid::CanHaveAttributes()
8623 return m_table
->CanHaveAttributes();
8626 void wxGrid::ClearAttrCache()
8628 if ( m_attrCache
.row
!= -1 )
8630 wxSafeDecRef(m_attrCache
.attr
);
8631 m_attrCache
.attr
= NULL
;
8632 m_attrCache
.row
= -1;
8636 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8638 wxGrid
*self
= (wxGrid
*)this; // const_cast
8640 self
->ClearAttrCache();
8641 self
->m_attrCache
.row
= row
;
8642 self
->m_attrCache
.col
= col
;
8643 self
->m_attrCache
.attr
= attr
;
8647 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8649 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8651 *attr
= m_attrCache
.attr
;
8652 wxSafeIncRef(m_attrCache
.attr
);
8654 #ifdef DEBUG_ATTR_CACHE
8655 gs_nAttrCacheHits
++;
8662 #ifdef DEBUG_ATTR_CACHE
8663 gs_nAttrCacheMisses
++;
8669 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8671 wxGridCellAttr
*attr
= NULL
;
8672 // Additional test to avoid looking at the cache e.g. for
8673 // wxNoCellCoords, as this will confuse memory management.
8676 if ( !LookupAttr(row
, col
, &attr
) )
8678 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8679 : (wxGridCellAttr
*)NULL
;
8680 CacheAttr(row
, col
, attr
);
8685 attr
->SetDefAttr(m_defaultCellAttr
);
8689 attr
= m_defaultCellAttr
;
8696 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8698 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8700 wxCHECK_MSG( m_table
, attr
,
8701 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8703 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8706 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8708 // artificially inc the ref count to match DecRef() in caller
8710 m_table
->SetAttr(attr
, row
, col
);
8716 // ----------------------------------------------------------------------------
8717 // setting column attributes (wrappers around SetColAttr)
8718 // ----------------------------------------------------------------------------
8720 void wxGrid::SetColFormatBool(int col
)
8722 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8725 void wxGrid::SetColFormatNumber(int col
)
8727 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8730 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8732 wxString typeName
= wxGRID_VALUE_FLOAT
;
8733 if ( (width
!= -1) || (precision
!= -1) )
8735 typeName
<< _T(':') << width
<< _T(',') << precision
;
8738 SetColFormatCustom(col
, typeName
);
8741 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8743 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8745 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8747 attr
= new wxGridCellAttr
;
8748 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8749 attr
->SetRenderer(renderer
);
8751 SetColAttr(col
, attr
);
8755 // ----------------------------------------------------------------------------
8756 // setting cell attributes: this is forwarded to the table
8757 // ----------------------------------------------------------------------------
8759 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8761 if ( CanHaveAttributes() )
8763 m_table
->SetAttr(attr
, row
, col
);
8772 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8774 if ( CanHaveAttributes() )
8776 m_table
->SetRowAttr(attr
, row
);
8785 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8787 if ( CanHaveAttributes() )
8789 m_table
->SetColAttr(attr
, col
);
8798 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8800 if ( CanHaveAttributes() )
8802 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8803 attr
->SetBackgroundColour(colour
);
8808 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8810 if ( CanHaveAttributes() )
8812 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8813 attr
->SetTextColour(colour
);
8818 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8820 if ( CanHaveAttributes() )
8822 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8823 attr
->SetFont(font
);
8828 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8830 if ( CanHaveAttributes() )
8832 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8833 attr
->SetAlignment(horiz
, vert
);
8838 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8840 if ( CanHaveAttributes() )
8842 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8843 attr
->SetOverflow(allow
);
8848 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8850 if ( CanHaveAttributes() )
8852 int cell_rows
, cell_cols
;
8854 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8855 attr
->GetSize(&cell_rows
, &cell_cols
);
8856 attr
->SetSize(num_rows
, num_cols
);
8859 // Cannot set the size of a cell to 0 or negative values
8860 // While it is perfectly legal to do that, this function cannot
8861 // handle all the possibilies, do it by hand by getting the CellAttr.
8862 // You can only set the size of a cell to 1,1 or greater with this fn
8863 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8864 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8865 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8866 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8868 // if this was already a multicell then "turn off" the other cells first
8869 if ((cell_rows
> 1) || (cell_rows
> 1))
8872 for (j
=row
; j
<row
+cell_rows
; j
++)
8874 for (i
=col
; i
<col
+cell_cols
; i
++)
8876 if ((i
!= col
) || (j
!= row
))
8878 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8879 attr_stub
->SetSize( 1, 1 );
8880 attr_stub
->DecRef();
8886 // mark the cells that will be covered by this cell to
8887 // negative or zero values to point back at this cell
8888 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8891 for (j
=row
; j
<row
+num_rows
; j
++)
8893 for (i
=col
; i
<col
+num_cols
; i
++)
8895 if ((i
!= col
) || (j
!= row
))
8897 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8898 attr_stub
->SetSize( row
-j
, col
-i
);
8899 attr_stub
->DecRef();
8907 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8909 if ( CanHaveAttributes() )
8911 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8912 attr
->SetRenderer(renderer
);
8917 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8919 if ( CanHaveAttributes() )
8921 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8922 attr
->SetEditor(editor
);
8927 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8929 if ( CanHaveAttributes() )
8931 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8932 attr
->SetReadOnly(isReadOnly
);
8937 // ----------------------------------------------------------------------------
8938 // Data type registration
8939 // ----------------------------------------------------------------------------
8941 void wxGrid::RegisterDataType(const wxString
& typeName
,
8942 wxGridCellRenderer
* renderer
,
8943 wxGridCellEditor
* editor
)
8945 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8949 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8951 wxString typeName
= m_table
->GetTypeName(row
, col
);
8952 return GetDefaultEditorForType(typeName
);
8955 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8957 wxString typeName
= m_table
->GetTypeName(row
, col
);
8958 return GetDefaultRendererForType(typeName
);
8962 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8964 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8965 if ( index
== wxNOT_FOUND
)
8967 wxFAIL_MSG(wxT("Unknown data type name"));
8972 return m_typeRegistry
->GetEditor(index
);
8976 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8978 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8979 if ( index
== wxNOT_FOUND
)
8981 wxFAIL_MSG(wxT("Unknown data type name"));
8986 return m_typeRegistry
->GetRenderer(index
);
8990 // ----------------------------------------------------------------------------
8992 // ----------------------------------------------------------------------------
8994 void wxGrid::EnableDragRowSize( bool enable
)
8996 m_canDragRowSize
= enable
;
9000 void wxGrid::EnableDragColSize( bool enable
)
9002 m_canDragColSize
= enable
;
9005 void wxGrid::EnableDragGridSize( bool enable
)
9007 m_canDragGridSize
= enable
;
9011 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9013 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9015 if ( resizeExistingRows
)
9017 // since we are resizing all rows to the default row size,
9018 // we can simply clear the row heights and row bottoms
9019 // arrays (which also allows us to take advantage of
9020 // some speed optimisations)
9021 m_rowHeights
.Empty();
9022 m_rowBottoms
.Empty();
9023 if ( !GetBatchCount() )
9028 void wxGrid::SetRowSize( int row
, int height
)
9030 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9032 if ( m_rowHeights
.IsEmpty() )
9034 // need to really create the array
9038 int h
= wxMax( 0, height
);
9039 int diff
= h
- m_rowHeights
[row
];
9041 m_rowHeights
[row
] = h
;
9043 for ( i
= row
; i
< m_numRows
; i
++ )
9045 m_rowBottoms
[i
] += diff
;
9047 if ( !GetBatchCount() )
9051 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9053 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9055 if ( resizeExistingCols
)
9057 // since we are resizing all columns to the default column size,
9058 // we can simply clear the col widths and col rights
9059 // arrays (which also allows us to take advantage of
9060 // some speed optimisations)
9061 m_colWidths
.Empty();
9062 m_colRights
.Empty();
9063 if ( !GetBatchCount() )
9068 void wxGrid::SetColSize( int col
, int width
)
9070 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9072 // should we check that it's bigger than GetColMinimalWidth(col) here?
9074 if ( m_colWidths
.IsEmpty() )
9076 // need to really create the array
9080 int w
= wxMax( 0, width
);
9081 int diff
= w
- m_colWidths
[col
];
9082 m_colWidths
[col
] = w
;
9085 for ( i
= col
; i
< m_numCols
; i
++ )
9087 m_colRights
[i
] += diff
;
9089 if ( !GetBatchCount() )
9094 void wxGrid::SetColMinimalWidth( int col
, int width
)
9096 m_colMinWidths
.Put(col
, width
);
9099 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9101 m_rowMinHeights
.Put(row
, width
);
9104 int wxGrid::GetColMinimalWidth(int col
) const
9106 long value
= m_colMinWidths
.Get(col
);
9107 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9110 int wxGrid::GetRowMinimalHeight(int row
) const
9112 long value
= m_rowMinHeights
.Get(row
);
9113 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9116 // ----------------------------------------------------------------------------
9118 // ----------------------------------------------------------------------------
9120 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9122 wxClientDC
dc(m_gridWin
);
9124 // init both of them to avoid compiler warnings, even if weo nly need one
9132 wxCoord extent
, extentMax
= 0;
9133 int max
= column
? m_numRows
: m_numCols
;
9134 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9141 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9142 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9145 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9146 extent
= column
? size
.x
: size
.y
;
9147 if ( extent
> extentMax
)
9158 // now also compare with the column label extent
9160 dc
.SetFont( GetLabelFont() );
9163 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9165 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9167 extent
= column
? w
: h
;
9168 if ( extent
> extentMax
)
9175 // empty column - give default extent (notice that if extentMax is less
9176 // than default extent but != 0, it's ok)
9177 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9183 // leave some space around text
9194 SetColSize(col
, extentMax
);
9195 if ( !GetBatchCount() )
9198 m_gridWin
->GetClientSize( &cw
, &ch
);
9199 wxRect
rect ( CellToRect( 0, col
) );
9201 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9202 rect
.width
= cw
- rect
.x
;
9203 rect
.height
= m_colLabelHeight
;
9204 m_colLabelWin
->Refresh( TRUE
, &rect
);
9209 SetRowSize(row
, extentMax
);
9210 if ( !GetBatchCount() )
9213 m_gridWin
->GetClientSize( &cw
, &ch
);
9214 wxRect
rect ( CellToRect( row
, 0 ) );
9216 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9217 rect
.width
= m_rowLabelWidth
;
9218 rect
.height
= ch
- rect
.y
;
9219 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9225 SetColMinimalWidth(col
, extentMax
);
9227 SetRowMinimalHeight(row
, extentMax
);
9231 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9233 int width
= m_rowLabelWidth
;
9238 for ( int col
= 0; col
< m_numCols
; col
++ )
9242 AutoSizeColumn(col
, setAsMin
);
9245 width
+= GetColWidth(col
);
9254 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9256 int height
= m_colLabelHeight
;
9261 for ( int row
= 0; row
< m_numRows
; row
++ )
9265 AutoSizeRow(row
, setAsMin
);
9268 height
+= GetRowHeight(row
);
9277 void wxGrid::AutoSize()
9281 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9283 // round up the size to a multiple of scroll step - this ensures that we
9284 // won't get the scrollbars if we're sized exactly to this width
9285 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
9286 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
9288 // distribute the extra space between teh columns/rows to avoid having
9289 // extra white space
9290 wxCoord diff
= sizeFit
.x
- size
.x
;
9293 // try to resize the columns uniformly
9294 wxCoord diffPerCol
= diff
/ m_numCols
;
9297 for ( int col
= 0; col
< m_numCols
; col
++ )
9299 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9303 // add remaining amount to the last columns
9304 diff
-= diffPerCol
* m_numCols
;
9307 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9309 SetColSize(col
, GetColWidth(col
) + 1);
9315 diff
= sizeFit
.y
- size
.y
;
9318 // try to resize the columns uniformly
9319 wxCoord diffPerRow
= diff
/ m_numRows
;
9322 for ( int row
= 0; row
< m_numRows
; row
++ )
9324 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9328 // add remaining amount to the last rows
9329 diff
-= diffPerRow
* m_numRows
;
9332 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9334 SetRowSize(row
, GetRowHeight(row
) + 1);
9341 SetClientSize(sizeFit
);
9344 wxSize
wxGrid::DoGetBestSize() const
9346 // don't set sizes, only calculate them
9347 wxGrid
*self
= (wxGrid
*)this; // const_cast
9350 width
= self
->SetOrCalcColumnSizes(TRUE
);
9351 height
= self
->SetOrCalcRowSizes(TRUE
);
9353 int maxwidth
, maxheight
;
9354 wxDisplaySize( & maxwidth
, & maxheight
);
9356 if ( width
> maxwidth
) width
= maxwidth
;
9357 if ( height
> maxheight
) height
= maxheight
;
9359 return wxSize( width
, height
);
9368 wxPen
& wxGrid::GetDividerPen() const
9373 // ----------------------------------------------------------------------------
9374 // cell value accessor functions
9375 // ----------------------------------------------------------------------------
9377 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9381 m_table
->SetValue( row
, col
, s
);
9382 if ( !GetBatchCount() )
9385 wxRect
rect( CellToRect( row
, col
) );
9387 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9388 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9389 m_gridWin
->Refresh( FALSE
, &rect
);
9392 if ( m_currentCellCoords
.GetRow() == row
&&
9393 m_currentCellCoords
.GetCol() == col
&&
9394 IsCellEditControlShown())
9395 // Note: If we are using IsCellEditControlEnabled,
9396 // this interacts badly with calling SetCellValue from
9397 // an EVT_GRID_CELL_CHANGE handler.
9399 HideCellEditControl();
9400 ShowCellEditControl(); // will reread data from table
9407 // ------ Block, row and col selection
9410 void wxGrid::SelectRow( int row
, bool addToSelected
)
9412 if ( IsSelection() && !addToSelected
)
9416 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9420 void wxGrid::SelectCol( int col
, bool addToSelected
)
9422 if ( IsSelection() && !addToSelected
)
9426 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9430 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9431 bool addToSelected
)
9433 if ( IsSelection() && !addToSelected
)
9437 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9438 FALSE
, addToSelected
);
9442 void wxGrid::SelectAll()
9444 if ( m_numRows
> 0 && m_numCols
> 0 )
9447 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9452 // ------ Cell, row and col deselection
9455 void wxGrid::DeselectRow( int row
)
9460 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9462 if ( m_selection
->IsInSelection(row
, 0 ) )
9463 m_selection
->ToggleCellSelection( row
, 0);
9467 int nCols
= GetNumberCols();
9468 for ( int i
= 0; i
< nCols
; i
++ )
9470 if ( m_selection
->IsInSelection(row
, i
) )
9471 m_selection
->ToggleCellSelection( row
, i
);
9476 void wxGrid::DeselectCol( int col
)
9481 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9483 if ( m_selection
->IsInSelection(0, col
) )
9484 m_selection
->ToggleCellSelection( 0, col
);
9488 int nRows
= GetNumberRows();
9489 for ( int i
= 0; i
< nRows
; i
++ )
9491 if ( m_selection
->IsInSelection(i
, col
) )
9492 m_selection
->ToggleCellSelection(i
, col
);
9497 void wxGrid::DeselectCell( int row
, int col
)
9499 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9500 m_selection
->ToggleCellSelection(row
, col
);
9503 bool wxGrid::IsSelection()
9505 return ( m_selection
&& (m_selection
->IsSelection() ||
9506 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9507 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9510 bool wxGrid::IsInSelection( int row
, int col
) const
9512 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9513 ( row
>= m_selectingTopLeft
.GetRow() &&
9514 col
>= m_selectingTopLeft
.GetCol() &&
9515 row
<= m_selectingBottomRight
.GetRow() &&
9516 col
<= m_selectingBottomRight
.GetCol() )) );
9519 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9521 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9522 return m_selection
->m_cellSelection
;
9524 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9526 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9527 return m_selection
->m_blockSelectionTopLeft
;
9529 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9531 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9532 return m_selection
->m_blockSelectionBottomRight
;
9534 wxArrayInt
wxGrid::GetSelectedRows() const
9536 if (!m_selection
) { wxArrayInt a
; return a
; }
9537 return m_selection
->m_rowSelection
;
9539 wxArrayInt
wxGrid::GetSelectedCols() const
9541 if (!m_selection
) { wxArrayInt a
; return a
; }
9542 return m_selection
->m_colSelection
;
9546 void wxGrid::ClearSelection()
9548 m_selectingTopLeft
= wxGridNoCellCoords
;
9549 m_selectingBottomRight
= wxGridNoCellCoords
;
9551 m_selection
->ClearSelection();
9555 // This function returns the rectangle that encloses the given block
9556 // in device coords clipped to the client size of the grid window.
9558 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9559 const wxGridCellCoords
&bottomRight
)
9561 wxRect
rect( wxGridNoCellRect
);
9564 cellRect
= CellToRect( topLeft
);
9565 if ( cellRect
!= wxGridNoCellRect
)
9571 rect
= wxRect( 0, 0, 0, 0 );
9574 cellRect
= CellToRect( bottomRight
);
9575 if ( cellRect
!= wxGridNoCellRect
)
9581 return wxGridNoCellRect
;
9585 int left
= rect
.GetLeft();
9586 int top
= rect
.GetTop();
9587 int right
= rect
.GetRight();
9588 int bottom
= rect
.GetBottom();
9590 int leftCol
= topLeft
.GetCol();
9591 int topRow
= topLeft
.GetRow();
9592 int rightCol
= bottomRight
.GetCol();
9593 int bottomRow
= bottomRight
.GetRow();
9616 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9618 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9620 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9622 cellRect
= CellToRect( j
, i
);
9624 if (cellRect
.x
< left
)
9626 if (cellRect
.y
< top
)
9628 if (cellRect
.x
+ cellRect
.width
> right
)
9629 right
= cellRect
.x
+ cellRect
.width
;
9630 if (cellRect
.y
+ cellRect
.height
> bottom
)
9631 bottom
= cellRect
.y
+ cellRect
.height
;
9633 else i
= rightCol
; // jump over inner cells.
9637 // convert to scrolled coords
9639 CalcScrolledPosition( left
, top
, &left
, &top
);
9640 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9643 m_gridWin
->GetClientSize( &cw
, &ch
);
9645 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9646 return wxRect( 0, 0, 0, 0);
9648 rect
.SetLeft( wxMax(0, left
) );
9649 rect
.SetTop( wxMax(0, top
) );
9650 rect
.SetRight( wxMin(cw
, right
) );
9651 rect
.SetBottom( wxMin(ch
, bottom
) );
9659 // ------ Grid event classes
9662 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9664 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9665 int row
, int col
, int x
, int y
, bool sel
,
9666 bool control
, bool shift
, bool alt
, bool meta
)
9667 : wxNotifyEvent( type
, id
)
9674 m_control
= control
;
9679 SetEventObject(obj
);
9683 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9685 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9686 int rowOrCol
, int x
, int y
,
9687 bool control
, bool shift
, bool alt
, bool meta
)
9688 : wxNotifyEvent( type
, id
)
9690 m_rowOrCol
= rowOrCol
;
9693 m_control
= control
;
9698 SetEventObject(obj
);
9702 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9704 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9705 const wxGridCellCoords
& topLeft
,
9706 const wxGridCellCoords
& bottomRight
,
9707 bool sel
, bool control
,
9708 bool shift
, bool alt
, bool meta
)
9709 : wxNotifyEvent( type
, id
)
9711 m_topLeft
= topLeft
;
9712 m_bottomRight
= bottomRight
;
9714 m_control
= control
;
9719 SetEventObject(obj
);
9723 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9725 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9726 wxObject
* obj
, int row
,
9727 int col
, wxControl
* ctrl
)
9728 : wxCommandEvent(type
, id
)
9730 SetEventObject(obj
);
9737 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9739 #endif // wxUSE_GRID