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
) + "\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
);
1021 void wxGridCellFloatEditor::Reset()
1023 DoReset(GetString());
1026 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1028 int keycode
= (int)event
.KeyCode();
1029 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1030 || keycode
== WXK_NUMPAD0
1031 || keycode
== WXK_NUMPAD1
1032 || keycode
== WXK_NUMPAD2
1033 || keycode
== WXK_NUMPAD3
1034 || keycode
== WXK_NUMPAD4
1035 || keycode
== WXK_NUMPAD5
1036 || keycode
== WXK_NUMPAD6
1037 || keycode
== WXK_NUMPAD7
1038 || keycode
== WXK_NUMPAD8
1039 || keycode
== WXK_NUMPAD9
1040 || keycode
== WXK_ADD
1041 || keycode
== WXK_NUMPAD_ADD
1042 || keycode
== WXK_SUBTRACT
1043 || keycode
== WXK_NUMPAD_SUBTRACT
)
1045 wxGridCellTextEditor::StartingKey(event
);
1047 // skip Skip() below
1054 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1065 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1069 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1071 m_precision
= (int)tmp
;
1073 // skip the error message below
1078 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1082 wxString
wxGridCellFloatEditor::GetString() const
1085 if ( m_width
== -1 )
1087 // default width/precision
1090 else if ( m_precision
== -1 )
1092 // default precision
1093 fmt
.Printf(_T("%%%d.f"), m_width
);
1097 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1100 return wxString::Format(fmt
, m_valueOld
);
1103 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1105 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1107 int keycode
= event
.GetKeyCode();
1121 case WXK_NUMPAD_ADD
:
1123 case WXK_NUMPAD_SUBTRACT
:
1125 case WXK_NUMPAD_DECIMAL
:
1129 // additionally accept 'e' as in '1e+6'
1130 if ( (keycode
< 128) &&
1131 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1139 #endif // wxUSE_TEXTCTRL
1143 // ----------------------------------------------------------------------------
1144 // wxGridCellBoolEditor
1145 // ----------------------------------------------------------------------------
1147 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1149 wxEvtHandler
* evtHandler
)
1151 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1152 wxDefaultPosition
, wxDefaultSize
,
1155 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1158 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1160 bool resize
= FALSE
;
1161 wxSize size
= m_control
->GetSize();
1162 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1164 // check if the checkbox is not too big/small for this cell
1165 wxSize sizeBest
= m_control
->GetBestSize();
1166 if ( !(size
== sizeBest
) )
1168 // reset to default size if it had been made smaller
1174 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1176 // leave 1 pixel margin
1177 size
.x
= size
.y
= minSize
- 2;
1184 m_control
->SetSize(size
);
1187 // position it in the centre of the rectangle (TODO: support alignment?)
1189 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1190 // the checkbox without label still has some space to the right in wxGTK,
1191 // so shift it to the right
1193 #elif defined(__WXMSW__)
1194 // here too, but in other way
1199 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1202 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1204 m_control
->Show(show
);
1208 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1209 CBox()->SetBackgroundColour(colBg
);
1213 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1215 wxASSERT_MSG(m_control
,
1216 wxT("The wxGridCellEditor must be Created first!"));
1218 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1219 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1222 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1223 m_startValue
= !( !cellval
|| (cellval
== "0") );
1225 CBox()->SetValue(m_startValue
);
1229 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1232 wxASSERT_MSG(m_control
,
1233 wxT("The wxGridCellEditor must be Created first!"));
1235 bool changed
= FALSE
;
1236 bool value
= CBox()->GetValue();
1237 if ( value
!= m_startValue
)
1242 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1243 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1245 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1251 void wxGridCellBoolEditor::Reset()
1253 wxASSERT_MSG(m_control
,
1254 wxT("The wxGridCellEditor must be Created first!"));
1256 CBox()->SetValue(m_startValue
);
1259 void wxGridCellBoolEditor::StartingClick()
1261 CBox()->SetValue(!CBox()->GetValue());
1264 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1266 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1268 int keycode
= event
.GetKeyCode();
1272 case WXK_NUMPAD_MULTIPLY
:
1274 case WXK_NUMPAD_ADD
:
1276 case WXK_NUMPAD_SUBTRACT
:
1287 #endif // wxUSE_CHECKBOX
1291 // ----------------------------------------------------------------------------
1292 // wxGridCellChoiceEditor
1293 // ----------------------------------------------------------------------------
1295 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1296 const wxString choices
[],
1298 : m_allowOthers(allowOthers
)
1302 m_choices
.Alloc(count
);
1303 for ( size_t n
= 0; n
< count
; n
++ )
1305 m_choices
.Add(choices
[n
]);
1310 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1312 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1313 editor
->m_allowOthers
= m_allowOthers
;
1314 editor
->m_choices
= m_choices
;
1319 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1321 wxEvtHandler
* evtHandler
)
1323 size_t count
= m_choices
.GetCount();
1324 wxString
*choices
= new wxString
[count
];
1325 for ( size_t n
= 0; n
< count
; n
++ )
1327 choices
[n
] = m_choices
[n
];
1330 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1331 wxDefaultPosition
, wxDefaultSize
,
1333 m_allowOthers
? 0 : wxCB_READONLY
);
1337 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1340 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1341 wxGridCellAttr
* attr
)
1343 // as we fill the entire client area, don't do anything here to minimize
1346 // TODO: It doesn't actually fill the client area since the height of a
1347 // combo always defaults to the standard... Until someone has time to
1348 // figure out the right rectangle to paint, just do it the normal way...
1349 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1352 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1354 wxASSERT_MSG(m_control
,
1355 wxT("The wxGridCellEditor must be Created first!"));
1357 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1359 Combo()->SetValue(m_startValue
);
1360 size_t count
= m_choices
.GetCount();
1361 for (size_t i
=0; i
<count
; i
++)
1363 if (m_startValue
== m_choices
[i
])
1365 Combo()->SetSelection(i
);
1369 Combo()->SetInsertionPointEnd();
1370 Combo()->SetFocus();
1373 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1376 wxString value
= Combo()->GetValue();
1377 bool changed
= value
!= m_startValue
;
1380 grid
->GetTable()->SetValue(row
, col
, value
);
1382 m_startValue
= wxEmptyString
;
1383 Combo()->SetValue(m_startValue
);
1388 void wxGridCellChoiceEditor::Reset()
1390 Combo()->SetValue(m_startValue
);
1391 Combo()->SetInsertionPointEnd();
1394 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1404 wxStringTokenizer
tk(params
, _T(','));
1405 while ( tk
.HasMoreTokens() )
1407 m_choices
.Add(tk
.GetNextToken());
1411 #endif // wxUSE_COMBOBOX
1413 // ----------------------------------------------------------------------------
1414 // wxGridCellEditorEvtHandler
1415 // ----------------------------------------------------------------------------
1417 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1419 switch ( event
.KeyCode() )
1423 m_grid
->DisableCellEditControl();
1427 m_grid
->GetEventHandler()->ProcessEvent( event
);
1431 case WXK_NUMPAD_ENTER
:
1432 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1433 m_editor
->HandleReturn(event
);
1442 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1444 switch ( event
.KeyCode() )
1449 case WXK_NUMPAD_ENTER
:
1457 // ----------------------------------------------------------------------------
1458 // wxGridCellWorker is an (almost) empty common base class for
1459 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1460 // ----------------------------------------------------------------------------
1462 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1467 wxGridCellWorker::~wxGridCellWorker()
1471 // ============================================================================
1473 // ============================================================================
1475 // ----------------------------------------------------------------------------
1476 // wxGridCellRenderer
1477 // ----------------------------------------------------------------------------
1479 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1480 wxGridCellAttr
& attr
,
1483 int WXUNUSED(row
), int WXUNUSED(col
),
1486 dc
.SetBackgroundMode( wxSOLID
);
1490 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1494 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1497 dc
.SetPen( *wxTRANSPARENT_PEN
);
1498 dc
.DrawRectangle(rect
);
1501 // ----------------------------------------------------------------------------
1502 // wxGridCellStringRenderer
1503 // ----------------------------------------------------------------------------
1505 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1506 wxGridCellAttr
& attr
,
1510 dc
.SetBackgroundMode( wxTRANSPARENT
);
1512 // TODO some special colours for attr.IsReadOnly() case?
1516 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1517 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1521 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1522 dc
.SetTextForeground( attr
.GetTextColour() );
1525 dc
.SetFont( attr
.GetFont() );
1528 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1530 const wxString
& text
)
1532 wxCoord x
= 0, y
= 0, max_x
= 0;
1533 dc
.SetFont(attr
.GetFont());
1534 wxStringTokenizer
tk(text
, _T('\n'));
1535 while ( tk
.HasMoreTokens() )
1537 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1538 max_x
= wxMax(max_x
, x
);
1541 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1543 return wxSize(max_x
, y
);
1546 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1547 wxGridCellAttr
& attr
,
1551 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1554 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1555 wxGridCellAttr
& attr
,
1557 const wxRect
& rectCell
,
1561 wxRect rect
= rectCell
;
1563 if (attr
.GetOverflow())
1565 int cols
= grid
.GetNumberCols();
1566 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1567 int cell_rows
, cell_cols
;
1568 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1569 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1571 int i
, c_cols
, c_rows
;
1572 for (i
= col
+cell_cols
; i
< cols
; i
++)
1574 // check w/ anchor cell for multicell block
1575 grid
.GetCellSize(row
, i
, &c_rows
, &c_cols
);
1576 if (c_rows
> 0) c_rows
= 0;
1577 if (grid
.GetTable()->IsEmptyCell(row
+c_rows
, i
))
1579 rect
.width
+= grid
.GetColSize(i
);
1580 if (rect
.width
>= best_width
) break;
1587 // erase only this cells background, overflow cells should have been erased
1588 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1590 // now we only have to draw the text
1591 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1594 attr
.GetAlignment(&hAlign
, &vAlign
);
1598 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1599 rect
, hAlign
, vAlign
);
1602 // ----------------------------------------------------------------------------
1603 // wxGridCellNumberRenderer
1604 // ----------------------------------------------------------------------------
1606 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1608 wxGridTableBase
*table
= grid
.GetTable();
1610 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1612 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1616 text
= table
->GetValue(row
, col
);
1622 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1623 wxGridCellAttr
& attr
,
1625 const wxRect
& rectCell
,
1629 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1631 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1633 // draw the text right aligned by default
1635 attr
.GetAlignment(&hAlign
, &vAlign
);
1636 hAlign
= wxALIGN_RIGHT
;
1638 wxRect rect
= rectCell
;
1641 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1644 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1645 wxGridCellAttr
& attr
,
1649 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1652 // ----------------------------------------------------------------------------
1653 // wxGridCellFloatRenderer
1654 // ----------------------------------------------------------------------------
1656 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1659 SetPrecision(precision
);
1662 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1664 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1665 renderer
->m_width
= m_width
;
1666 renderer
->m_precision
= m_precision
;
1667 renderer
->m_format
= m_format
;
1672 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1674 wxGridTableBase
*table
= grid
.GetTable();
1679 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1681 val
= table
->GetValueAsDouble(row
, col
);
1686 text
= table
->GetValue(row
, col
);
1687 hasDouble
= text
.ToDouble(&val
);
1694 if ( m_width
== -1 )
1696 if ( m_precision
== -1 )
1698 // default width/precision
1699 m_format
= _T("%f");
1703 m_format
.Printf(_T("%%.%df"), m_precision
);
1706 else if ( m_precision
== -1 )
1708 // default precision
1709 m_format
.Printf(_T("%%%d.f"), m_width
);
1713 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1717 text
.Printf(m_format
, val
);
1720 //else: text already contains the string
1725 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1726 wxGridCellAttr
& attr
,
1728 const wxRect
& rectCell
,
1732 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1734 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1736 // draw the text right aligned by default
1738 attr
.GetAlignment(&hAlign
, &vAlign
);
1739 hAlign
= wxALIGN_RIGHT
;
1741 wxRect rect
= rectCell
;
1744 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1747 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1748 wxGridCellAttr
& attr
,
1752 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1755 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1759 // reset to defaults
1765 wxString tmp
= params
.BeforeFirst(_T(','));
1769 if ( tmp
.ToLong(&width
) )
1771 SetWidth((int)width
);
1775 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1779 tmp
= params
.AfterFirst(_T(','));
1783 if ( tmp
.ToLong(&precision
) )
1785 SetPrecision((int)precision
);
1789 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1797 // ----------------------------------------------------------------------------
1798 // wxGridCellBoolRenderer
1799 // ----------------------------------------------------------------------------
1801 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1803 // FIXME these checkbox size calculations are really ugly...
1805 // between checkmark and box
1806 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1808 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1809 wxGridCellAttr
& WXUNUSED(attr
),
1814 // compute it only once (no locks for MT safeness in GUI thread...)
1815 if ( !ms_sizeCheckMark
.x
)
1817 // get checkbox size
1818 wxCoord checkSize
= 0;
1819 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1820 wxSize size
= checkbox
->GetBestSize();
1821 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1823 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1824 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1825 checkSize
-= size
.y
/ 2;
1830 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1833 return ms_sizeCheckMark
;
1836 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1837 wxGridCellAttr
& attr
,
1843 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1845 // draw a check mark in the centre (ignoring alignment - TODO)
1846 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1848 // don't draw outside the cell
1849 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1850 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1852 // and even leave (at least) 1 pixel margin
1853 size
.x
= size
.y
= minSize
- 2;
1856 // draw a border around checkmark
1858 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1859 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1860 rectBorder
.width
= size
.x
;
1861 rectBorder
.height
= size
.y
;
1864 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1865 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1868 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1869 value
= !( !cellval
|| (cellval
== "0") );
1874 wxRect rectMark
= rectBorder
;
1876 // MSW DrawCheckMark() is weird (and should probably be changed...)
1877 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1881 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1884 dc
.SetTextForeground(attr
.GetTextColour());
1885 dc
.DrawCheckMark(rectMark
);
1888 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1889 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1890 dc
.DrawRectangle(rectBorder
);
1893 // ----------------------------------------------------------------------------
1895 // ----------------------------------------------------------------------------
1897 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1901 m_isReadOnly
= Unset
;
1906 m_attrkind
= wxGridCellAttr::Cell
;
1908 m_sizeRows
= m_sizeCols
= 1;
1911 SetDefAttr(attrDefault
);
1914 wxGridCellAttr
*wxGridCellAttr::Clone() const
1916 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1918 if ( HasTextColour() )
1919 attr
->SetTextColour(GetTextColour());
1920 if ( HasBackgroundColour() )
1921 attr
->SetBackgroundColour(GetBackgroundColour());
1923 attr
->SetFont(GetFont());
1924 if ( HasAlignment() )
1925 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1927 attr
->SetSize( m_sizeRows
, m_sizeCols
);
1931 attr
->SetRenderer(m_renderer
);
1932 m_renderer
->IncRef();
1936 attr
->SetEditor(m_editor
);
1941 attr
->SetReadOnly();
1943 attr
->SetKind( m_attrkind
);
1948 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1950 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1951 SetTextColour(mergefrom
->GetTextColour());
1952 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1953 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1954 if ( !HasFont() && mergefrom
->HasFont() )
1955 SetFont(mergefrom
->GetFont());
1956 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
1958 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1959 SetAlignment(hAlign
, vAlign
);
1962 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
1964 // Directly access member functions as GetRender/Editor don't just return
1965 // m_renderer/m_editor
1967 // Maybe add support for merge of Render and Editor?
1968 if (!HasRenderer() && mergefrom
->HasRenderer() )
1970 m_renderer
= mergefrom
->m_renderer
;
1971 m_renderer
->IncRef();
1973 if ( !HasEditor() && mergefrom
->HasEditor() )
1975 m_editor
= mergefrom
->m_editor
;
1978 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1979 SetReadOnly(mergefrom
->IsReadOnly());
1981 SetDefAttr(mergefrom
->m_defGridAttr
);
1984 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
1986 // The size of a cell is normally 1,1
1988 // If this cell is larger (2,2) then this is the top left cell
1989 // the other cells that will be covered (lower right cells) must be
1990 // set to negative or zero values such that
1991 // row + num_rows of the covered cell points to the larger cell (this cell)
1992 // same goes for the col + num_cols.
1994 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
1996 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
1997 !((num_rows
<=0)&&(num_cols
>0)) ||
1998 !((num_rows
==0)&&(num_cols
==0))),
1999 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2001 m_sizeRows
= num_rows
;
2002 m_sizeCols
= num_cols
;
2005 const wxColour
& wxGridCellAttr::GetTextColour() const
2007 if (HasTextColour())
2011 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2013 return m_defGridAttr
->GetTextColour();
2017 wxFAIL_MSG(wxT("Missing default cell attribute"));
2018 return wxNullColour
;
2023 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2025 if (HasBackgroundColour())
2027 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2028 return m_defGridAttr
->GetBackgroundColour();
2031 wxFAIL_MSG(wxT("Missing default cell attribute"));
2032 return wxNullColour
;
2037 const wxFont
& wxGridCellAttr::GetFont() const
2041 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2042 return m_defGridAttr
->GetFont();
2045 wxFAIL_MSG(wxT("Missing default cell attribute"));
2051 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2055 if ( hAlign
) *hAlign
= m_hAlign
;
2056 if ( vAlign
) *vAlign
= m_vAlign
;
2058 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2059 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2062 wxFAIL_MSG(wxT("Missing default cell attribute"));
2066 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2068 if ( num_rows
) *num_rows
= m_sizeRows
;
2069 if ( num_cols
) *num_cols
= m_sizeCols
;
2072 // GetRenderer and GetEditor use a slightly different decision path about
2073 // which attribute to use. If a non-default attr object has one then it is
2074 // used, otherwise the default editor or renderer is fetched from the grid and
2075 // used. It should be the default for the data type of the cell. If it is
2076 // NULL (because the table has a type that the grid does not have in its
2077 // registry,) then the grid's default editor or renderer is used.
2079 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2081 wxGridCellRenderer
*renderer
;
2083 if ( m_renderer
&& this != m_defGridAttr
)
2085 // use the cells renderer if it has one
2086 renderer
= m_renderer
;
2089 else // no non default cell renderer
2091 // get default renderer for the data type
2094 // GetDefaultRendererForCell() will do IncRef() for us
2095 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2104 if (m_defGridAttr
&& this != m_defGridAttr
)
2106 // if we still don't have one then use the grid default
2107 // (no need for IncRef() here neither)
2108 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2110 else // default grid attr
2112 // use m_renderer which we had decided not to use initially
2113 renderer
= m_renderer
;
2120 // we're supposed to always find something
2121 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2126 // same as above, except for s/renderer/editor/g
2127 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2129 wxGridCellEditor
*editor
;
2131 if ( m_editor
&& this != m_defGridAttr
)
2133 // use the cells editor if it has one
2137 else // no non default cell editor
2139 // get default editor for the data type
2142 // GetDefaultEditorForCell() will do IncRef() for us
2143 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2152 if ( m_defGridAttr
&& this != m_defGridAttr
)
2154 // if we still don't have one then use the grid default
2155 // (no need for IncRef() here neither)
2156 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2158 else // default grid attr
2160 // use m_editor which we had decided not to use initially
2168 // we're supposed to always find something
2169 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2174 // ----------------------------------------------------------------------------
2175 // wxGridCellAttrData
2176 // ----------------------------------------------------------------------------
2178 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2180 int n
= FindIndex(row
, col
);
2181 if ( n
== wxNOT_FOUND
)
2183 // add the attribute
2184 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2188 // free the old attribute
2189 m_attrs
[(size_t)n
].attr
->DecRef();
2193 // change the attribute
2194 m_attrs
[(size_t)n
].attr
= attr
;
2198 // remove this attribute
2199 m_attrs
.RemoveAt((size_t)n
);
2204 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2206 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2208 int n
= FindIndex(row
, col
);
2209 if ( n
!= wxNOT_FOUND
)
2211 attr
= m_attrs
[(size_t)n
].attr
;
2218 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2220 size_t count
= m_attrs
.GetCount();
2221 for ( size_t n
= 0; n
< count
; n
++ )
2223 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2224 wxCoord row
= coords
.GetRow();
2225 if ((size_t)row
>= pos
)
2229 // If rows inserted, include row counter where necessary
2230 coords
.SetRow(row
+ numRows
);
2232 else if (numRows
< 0)
2234 // If rows deleted ...
2235 if ((size_t)row
>= pos
- numRows
)
2237 // ...either decrement row counter (if row still exists)...
2238 coords
.SetRow(row
+ numRows
);
2242 // ...or remove the attribute
2243 m_attrs
.RemoveAt((size_t)n
);
2251 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2253 size_t count
= m_attrs
.GetCount();
2254 for ( size_t n
= 0; n
< count
; n
++ )
2256 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2257 wxCoord col
= coords
.GetCol();
2258 if ( (size_t)col
>= pos
)
2262 // If rows inserted, include row counter where necessary
2263 coords
.SetCol(col
+ numCols
);
2265 else if (numCols
< 0)
2267 // If rows deleted ...
2268 if ((size_t)col
>= pos
- numCols
)
2270 // ...either decrement row counter (if row still exists)...
2271 coords
.SetCol(col
+ numCols
);
2275 // ...or remove the attribute
2276 m_attrs
.RemoveAt((size_t)n
);
2284 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2286 size_t count
= m_attrs
.GetCount();
2287 for ( size_t n
= 0; n
< count
; n
++ )
2289 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2290 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2299 // ----------------------------------------------------------------------------
2300 // wxGridRowOrColAttrData
2301 // ----------------------------------------------------------------------------
2303 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2305 size_t count
= m_attrs
.Count();
2306 for ( size_t n
= 0; n
< count
; n
++ )
2308 m_attrs
[n
]->DecRef();
2312 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2314 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2316 int n
= m_rowsOrCols
.Index(rowOrCol
);
2317 if ( n
!= wxNOT_FOUND
)
2319 attr
= m_attrs
[(size_t)n
];
2326 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2328 int i
= m_rowsOrCols
.Index(rowOrCol
);
2329 if ( i
== wxNOT_FOUND
)
2331 // add the attribute
2332 m_rowsOrCols
.Add(rowOrCol
);
2337 size_t n
= (size_t)i
;
2340 // change the attribute
2341 m_attrs
[n
]->DecRef();
2346 // remove this attribute
2347 m_attrs
[n
]->DecRef();
2348 m_rowsOrCols
.RemoveAt(n
);
2349 m_attrs
.RemoveAt(n
);
2354 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2356 size_t count
= m_attrs
.GetCount();
2357 for ( size_t n
= 0; n
< count
; n
++ )
2359 int & rowOrCol
= m_rowsOrCols
[n
];
2360 if ( (size_t)rowOrCol
>= pos
)
2362 if ( numRowsOrCols
> 0 )
2364 // If rows inserted, include row counter where necessary
2365 rowOrCol
+= numRowsOrCols
;
2367 else if ( numRowsOrCols
< 0)
2369 // If rows deleted, either decrement row counter (if row still exists)
2370 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2371 rowOrCol
+= numRowsOrCols
;
2374 m_rowsOrCols
.RemoveAt((size_t)n
);
2375 m_attrs
.RemoveAt((size_t)n
);
2383 // ----------------------------------------------------------------------------
2384 // wxGridCellAttrProvider
2385 // ----------------------------------------------------------------------------
2387 wxGridCellAttrProvider::wxGridCellAttrProvider()
2389 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2392 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2397 void wxGridCellAttrProvider::InitData()
2399 m_data
= new wxGridCellAttrProviderData
;
2402 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2403 wxGridCellAttr::wxAttrKind kind
) const
2405 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2410 case (wxGridCellAttr::Any
):
2411 //Get cached merge attributes.
2412 // Currenlty not used as no cache implemented as not mutiable
2413 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2416 //Basicaly implement old version.
2417 //Also check merge cache, so we don't have to re-merge every time..
2418 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2419 *attrrow
= (wxGridCellAttr
*)NULL
,
2420 *attrcol
= (wxGridCellAttr
*)NULL
;
2422 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2423 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2424 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2426 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2427 // Two or move are non NULL
2428 attr
= new wxGridCellAttr
;
2429 attr
->SetKind(wxGridCellAttr::Merged
);
2433 attr
->MergeWith(attrcell
);
2437 attr
->MergeWith(attrcol
);
2441 attr
->MergeWith(attrrow
);
2444 //store merge attr if cache implemented
2446 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2450 // one or none is non null return it or null.
2451 if(attrrow
) attr
= attrrow
;
2452 if(attrcol
) attr
= attrcol
;
2453 if(attrcell
) attr
= attrcell
;
2457 case (wxGridCellAttr::Cell
):
2458 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2460 case (wxGridCellAttr::Col
):
2461 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2463 case (wxGridCellAttr::Row
):
2464 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2468 // (wxGridCellAttr::Default):
2469 // (wxGridCellAttr::Merged):
2476 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2482 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2485 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2490 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2493 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2498 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2501 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2505 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2507 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2511 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2515 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2517 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2521 // ----------------------------------------------------------------------------
2522 // wxGridTypeRegistry
2523 // ----------------------------------------------------------------------------
2525 wxGridTypeRegistry::~wxGridTypeRegistry()
2527 size_t count
= m_typeinfo
.Count();
2528 for ( size_t i
= 0; i
< count
; i
++ )
2529 delete m_typeinfo
[i
];
2533 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2534 wxGridCellRenderer
* renderer
,
2535 wxGridCellEditor
* editor
)
2537 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2539 // is it already registered?
2540 int loc
= FindRegisteredDataType(typeName
);
2541 if ( loc
!= wxNOT_FOUND
)
2543 delete m_typeinfo
[loc
];
2544 m_typeinfo
[loc
] = info
;
2548 m_typeinfo
.Add(info
);
2552 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2554 size_t count
= m_typeinfo
.GetCount();
2555 for ( size_t i
= 0; i
< count
; i
++ )
2557 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2566 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2568 int index
= FindRegisteredDataType(typeName
);
2569 if ( index
== wxNOT_FOUND
)
2571 // check whether this is one of the standard ones, in which case
2572 // register it "on the fly"
2574 if ( typeName
== wxGRID_VALUE_STRING
)
2576 RegisterDataType(wxGRID_VALUE_STRING
,
2577 new wxGridCellStringRenderer
,
2578 new wxGridCellTextEditor
);
2580 #endif // wxUSE_TEXTCTRL
2582 if ( typeName
== wxGRID_VALUE_BOOL
)
2584 RegisterDataType(wxGRID_VALUE_BOOL
,
2585 new wxGridCellBoolRenderer
,
2586 new wxGridCellBoolEditor
);
2588 #endif // wxUSE_CHECKBOX
2590 if ( typeName
== wxGRID_VALUE_NUMBER
)
2592 RegisterDataType(wxGRID_VALUE_NUMBER
,
2593 new wxGridCellNumberRenderer
,
2594 new wxGridCellNumberEditor
);
2596 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2598 RegisterDataType(wxGRID_VALUE_FLOAT
,
2599 new wxGridCellFloatRenderer
,
2600 new wxGridCellFloatEditor
);
2602 #endif // wxUSE_TEXTCTRL
2604 if ( typeName
== wxGRID_VALUE_CHOICE
)
2606 RegisterDataType(wxGRID_VALUE_CHOICE
,
2607 new wxGridCellStringRenderer
,
2608 new wxGridCellChoiceEditor
);
2610 #endif // wxUSE_COMBOBOX
2615 // we get here only if just added the entry for this type, so return
2617 index
= m_typeinfo
.GetCount() - 1;
2623 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2625 int index
= FindDataType(typeName
);
2626 if ( index
== wxNOT_FOUND
)
2628 // the first part of the typename is the "real" type, anything after ':'
2629 // are the parameters for the renderer
2630 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2631 if ( index
== wxNOT_FOUND
)
2636 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2637 wxGridCellRenderer
*rendererOld
= renderer
;
2638 renderer
= renderer
->Clone();
2639 rendererOld
->DecRef();
2641 wxGridCellEditor
*editor
= GetEditor(index
);
2642 wxGridCellEditor
*editorOld
= editor
;
2643 editor
= editor
->Clone();
2644 editorOld
->DecRef();
2646 // do it even if there are no parameters to reset them to defaults
2647 wxString params
= typeName
.AfterFirst(_T(':'));
2648 renderer
->SetParameters(params
);
2649 editor
->SetParameters(params
);
2651 // register the new typename
2652 RegisterDataType(typeName
, renderer
, editor
);
2654 // we just registered it, it's the last one
2655 index
= m_typeinfo
.GetCount() - 1;
2661 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2663 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2669 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2671 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2677 // ----------------------------------------------------------------------------
2679 // ----------------------------------------------------------------------------
2681 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2684 wxGridTableBase::wxGridTableBase()
2686 m_view
= (wxGrid
*) NULL
;
2687 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2690 wxGridTableBase::~wxGridTableBase()
2692 delete m_attrProvider
;
2695 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2697 delete m_attrProvider
;
2698 m_attrProvider
= attrProvider
;
2701 bool wxGridTableBase::CanHaveAttributes()
2703 if ( ! GetAttrProvider() )
2705 // use the default attr provider by default
2706 SetAttrProvider(new wxGridCellAttrProvider
);
2711 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2713 if ( m_attrProvider
)
2714 return m_attrProvider
->GetAttr(row
, col
, kind
);
2716 return (wxGridCellAttr
*)NULL
;
2719 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2721 if ( m_attrProvider
)
2723 attr
->SetKind(wxGridCellAttr::Cell
);
2724 m_attrProvider
->SetAttr(attr
, row
, col
);
2728 // as we take ownership of the pointer and don't store it, we must
2734 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2736 if ( m_attrProvider
)
2738 attr
->SetKind(wxGridCellAttr::Row
);
2739 m_attrProvider
->SetRowAttr(attr
, row
);
2743 // as we take ownership of the pointer and don't store it, we must
2749 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2751 if ( m_attrProvider
)
2753 attr
->SetKind(wxGridCellAttr::Col
);
2754 m_attrProvider
->SetColAttr(attr
, col
);
2758 // as we take ownership of the pointer and don't store it, we must
2764 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2765 size_t WXUNUSED(numRows
) )
2767 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2772 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2774 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2779 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2780 size_t WXUNUSED(numRows
) )
2782 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2787 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2788 size_t WXUNUSED(numCols
) )
2790 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2795 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2797 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2802 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2803 size_t WXUNUSED(numCols
) )
2805 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2811 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2814 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2815 // how much it makes sense to us geeks.
2819 wxString
wxGridTableBase::GetColLabelValue( int col
)
2821 // default col labels are:
2822 // cols 0 to 25 : A-Z
2823 // cols 26 to 675 : AA-ZZ
2828 for ( n
= 1; ; n
++ )
2830 s
+= (_T('A') + (wxChar
)( col%26
));
2832 if ( col
< 0 ) break;
2835 // reverse the string...
2837 for ( i
= 0; i
< n
; i
++ )
2846 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2848 return wxGRID_VALUE_STRING
;
2851 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2852 const wxString
& typeName
)
2854 return typeName
== wxGRID_VALUE_STRING
;
2857 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2859 return CanGetValueAs(row
, col
, typeName
);
2862 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2867 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2872 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2877 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2878 long WXUNUSED(value
) )
2882 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2883 double WXUNUSED(value
) )
2887 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2888 bool WXUNUSED(value
) )
2893 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2894 const wxString
& WXUNUSED(typeName
) )
2899 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2900 const wxString
& WXUNUSED(typeName
),
2901 void* WXUNUSED(value
) )
2905 //////////////////////////////////////////////////////////////////////
2907 // Message class for the grid table to send requests and notifications
2911 wxGridTableMessage::wxGridTableMessage()
2913 m_table
= (wxGridTableBase
*) NULL
;
2919 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2920 int commandInt1
, int commandInt2
)
2924 m_comInt1
= commandInt1
;
2925 m_comInt2
= commandInt2
;
2930 //////////////////////////////////////////////////////////////////////
2932 // A basic grid table for string data. An object of this class will
2933 // created by wxGrid if you don't specify an alternative table class.
2936 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2938 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2940 wxGridStringTable::wxGridStringTable()
2945 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2948 m_data
.Alloc( numRows
);
2951 sa
.Alloc( numCols
);
2952 sa
.Add( wxEmptyString
, numCols
);
2954 m_data
.Add( sa
, numRows
);
2957 wxGridStringTable::~wxGridStringTable()
2961 int wxGridStringTable::GetNumberRows()
2963 return m_data
.GetCount();
2966 int wxGridStringTable::GetNumberCols()
2968 if ( m_data
.GetCount() > 0 )
2969 return m_data
[0].GetCount();
2974 wxString
wxGridStringTable::GetValue( int row
, int col
)
2976 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2977 _T("invalid row or column index in wxGridStringTable") );
2979 return m_data
[row
][col
];
2982 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2984 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2985 _T("invalid row or column index in wxGridStringTable") );
2987 m_data
[row
][col
] = value
;
2990 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2992 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2993 _T("invalid row or column index in wxGridStringTable") );
2995 return (m_data
[row
][col
] == wxEmptyString
);
2998 void wxGridStringTable::Clear()
3001 int numRows
, numCols
;
3003 numRows
= m_data
.GetCount();
3006 numCols
= m_data
[0].GetCount();
3008 for ( row
= 0; row
< numRows
; row
++ )
3010 for ( col
= 0; col
< numCols
; col
++ )
3012 m_data
[row
][col
] = wxEmptyString
;
3019 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3021 size_t curNumRows
= m_data
.GetCount();
3022 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3023 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3025 if ( pos
>= curNumRows
)
3027 return AppendRows( numRows
);
3031 sa
.Alloc( curNumCols
);
3032 sa
.Add( wxEmptyString
, curNumCols
);
3033 m_data
.Insert( sa
, pos
, numRows
);
3036 wxGridTableMessage
msg( this,
3037 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3041 GetView()->ProcessTableMessage( msg
);
3047 bool wxGridStringTable::AppendRows( size_t numRows
)
3049 size_t curNumRows
= m_data
.GetCount();
3050 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3051 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3054 if ( curNumCols
> 0 )
3056 sa
.Alloc( curNumCols
);
3057 sa
.Add( wxEmptyString
, curNumCols
);
3060 m_data
.Add( sa
, numRows
);
3064 wxGridTableMessage
msg( this,
3065 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3068 GetView()->ProcessTableMessage( msg
);
3074 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3076 size_t curNumRows
= m_data
.GetCount();
3078 if ( pos
>= curNumRows
)
3081 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3082 pos
, numRows
, curNumRows
);
3083 wxFAIL_MSG( errmsg
);
3087 if ( numRows
> curNumRows
- pos
)
3089 numRows
= curNumRows
- pos
;
3092 if ( numRows
>= curNumRows
)
3094 m_data
.Empty(); // don't release memory just yet
3098 m_data
.RemoveAt( pos
, numRows
);
3102 wxGridTableMessage
msg( this,
3103 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3107 GetView()->ProcessTableMessage( msg
);
3113 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3117 size_t curNumRows
= m_data
.GetCount();
3118 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3119 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3121 if ( pos
>= curNumCols
)
3123 return AppendCols( numCols
);
3126 for ( row
= 0; row
< curNumRows
; row
++ )
3128 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3130 m_data
[row
].Insert( wxEmptyString
, col
);
3135 wxGridTableMessage
msg( this,
3136 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3140 GetView()->ProcessTableMessage( msg
);
3146 bool wxGridStringTable::AppendCols( size_t numCols
)
3150 size_t curNumRows
= m_data
.GetCount();
3154 // TODO: something better than this ?
3156 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3161 for ( row
= 0; row
< curNumRows
; row
++ )
3163 m_data
[row
].Add( wxEmptyString
, numCols
);
3168 wxGridTableMessage
msg( this,
3169 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3172 GetView()->ProcessTableMessage( msg
);
3178 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3182 size_t curNumRows
= m_data
.GetCount();
3183 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3184 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3186 if ( pos
>= curNumCols
)
3189 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3190 pos
, numCols
, curNumCols
);
3191 wxFAIL_MSG( errmsg
);
3195 if ( numCols
> curNumCols
- pos
)
3197 numCols
= curNumCols
- pos
;
3200 for ( row
= 0; row
< curNumRows
; row
++ )
3202 if ( numCols
>= curNumCols
)
3204 m_data
[row
].Clear();
3208 m_data
[row
].RemoveAt( pos
, numCols
);
3213 wxGridTableMessage
msg( this,
3214 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3218 GetView()->ProcessTableMessage( msg
);
3224 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3226 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3228 // using default label
3230 return wxGridTableBase::GetRowLabelValue( row
);
3234 return m_rowLabels
[ row
];
3238 wxString
wxGridStringTable::GetColLabelValue( int col
)
3240 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3242 // using default label
3244 return wxGridTableBase::GetColLabelValue( col
);
3248 return m_colLabels
[ col
];
3252 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3254 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3256 int n
= m_rowLabels
.GetCount();
3258 for ( i
= n
; i
<= row
; i
++ )
3260 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3264 m_rowLabels
[row
] = value
;
3267 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3269 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3271 int n
= m_colLabels
.GetCount();
3273 for ( i
= n
; i
<= col
; i
++ )
3275 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3279 m_colLabels
[col
] = value
;
3284 //////////////////////////////////////////////////////////////////////
3285 //////////////////////////////////////////////////////////////////////
3287 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3289 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3290 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3291 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3292 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3293 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3294 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3297 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3299 const wxPoint
&pos
, const wxSize
&size
)
3300 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3305 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3309 // NO - don't do this because it will set both the x and y origin
3310 // coords to match the parent scrolled window and we just want to
3311 // set the y coord - MB
3313 // m_owner->PrepareDC( dc );
3316 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3317 dc
.SetDeviceOrigin( 0, -y
);
3319 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3320 m_owner
->DrawRowLabels( dc
, rows
);
3324 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3326 m_owner
->ProcessRowLabelMouseEvent( event
);
3330 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3332 m_owner
->GetEventHandler()->ProcessEvent(event
);
3336 // This seems to be required for wxMotif otherwise the mouse
3337 // cursor must be in the cell edit control to get key events
3339 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3341 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3344 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3346 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3351 //////////////////////////////////////////////////////////////////////
3353 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3355 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3356 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3357 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3358 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3359 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3360 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3363 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3365 const wxPoint
&pos
, const wxSize
&size
)
3366 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3371 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3375 // NO - don't do this because it will set both the x and y origin
3376 // coords to match the parent scrolled window and we just want to
3377 // set the x coord - MB
3379 // m_owner->PrepareDC( dc );
3382 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3383 dc
.SetDeviceOrigin( -x
, 0 );
3385 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3386 m_owner
->DrawColLabels( dc
, cols
);
3390 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3392 m_owner
->ProcessColLabelMouseEvent( event
);
3395 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3397 m_owner
->GetEventHandler()->ProcessEvent(event
);
3401 // This seems to be required for wxMotif otherwise the mouse
3402 // cursor must be in the cell edit control to get key events
3404 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3406 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3409 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3411 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3416 //////////////////////////////////////////////////////////////////////
3418 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3420 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3421 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3422 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3423 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3424 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3425 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3428 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3430 const wxPoint
&pos
, const wxSize
&size
)
3431 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3436 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3440 int client_height
= 0;
3441 int client_width
= 0;
3442 GetClientSize( &client_width
, &client_height
);
3444 dc
.SetPen( *wxBLACK_PEN
);
3445 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3446 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3448 dc
.SetPen( *wxWHITE_PEN
);
3449 dc
.DrawLine( 0, 0, client_width
, 0 );
3450 dc
.DrawLine( 0, 0, 0, client_height
);
3454 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3456 m_owner
->ProcessCornerLabelMouseEvent( event
);
3460 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3462 m_owner
->GetEventHandler()->ProcessEvent(event
);
3465 // This seems to be required for wxMotif otherwise the mouse
3466 // cursor must be in the cell edit control to get key events
3468 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3470 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3473 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3475 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3480 //////////////////////////////////////////////////////////////////////
3482 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3484 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3485 EVT_PAINT( wxGridWindow::OnPaint
)
3486 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3487 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3488 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3489 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3490 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3493 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3494 wxGridRowLabelWindow
*rowLblWin
,
3495 wxGridColLabelWindow
*colLblWin
,
3496 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3497 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3500 m_rowLabelWin
= rowLblWin
;
3501 m_colLabelWin
= colLblWin
;
3502 SetBackgroundColour(_T("WHITE"));
3506 wxGridWindow::~wxGridWindow()
3511 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3513 wxPaintDC
dc( this );
3514 m_owner
->PrepareDC( dc
);
3515 wxRegion reg
= GetUpdateRegion();
3516 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3517 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3518 #if WXGRID_DRAW_LINES
3519 m_owner
->DrawAllGridLines( dc
, reg
);
3521 m_owner
->DrawGridSpace( dc
);
3522 m_owner
->DrawHighlight( dc
, DirtyCells
);
3526 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3528 wxWindow::ScrollWindow( dx
, dy
, rect
);
3529 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3530 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3534 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3536 m_owner
->ProcessGridCellMouseEvent( event
);
3539 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3541 m_owner
->GetEventHandler()->ProcessEvent(event
);
3544 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3545 // cursor must be in the cell edit control to get key events
3547 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3549 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3552 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3554 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3557 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3562 //////////////////////////////////////////////////////////////////////
3564 // Internal Helper function for computing row or column from some
3565 // (unscrolled) coordinate value, using either
3566 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3567 // of m_rowBottoms/m_ColRights to speed up the search!
3569 // Internal helper macros for simpler use of that function
3571 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3572 const wxArrayInt
& BorderArray
, bool maxOnOverflow
);
3574 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3575 WXGRID_MIN_COL_WIDTH, \
3577 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3578 WXGRID_MIN_ROW_HEIGHT, \
3580 /////////////////////////////////////////////////////////////////////
3582 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3584 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3585 EVT_PAINT( wxGrid::OnPaint
)
3586 EVT_SIZE( wxGrid::OnSize
)
3587 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3588 EVT_KEY_UP( wxGrid::OnKeyUp
)
3589 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3592 wxGrid::wxGrid( wxWindow
*parent
,
3597 const wxString
& name
)
3598 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3599 m_colMinWidths(GRID_HASH_SIZE
),
3600 m_rowMinHeights(GRID_HASH_SIZE
)
3608 // Must do this or ~wxScrollHelper will pop the wrong event handler
3609 SetTargetWindow(this);
3611 wxSafeDecRef(m_defaultCellAttr
);
3613 #ifdef DEBUG_ATTR_CACHE
3614 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3615 wxPrintf(_T("wxGrid attribute cache statistics: "
3616 "total: %u, hits: %u (%u%%)\n"),
3617 total
, gs_nAttrCacheHits
,
3618 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3624 delete m_typeRegistry
;
3630 // ----- internal init and update functions
3633 void wxGrid::Create()
3635 m_created
= FALSE
; // set to TRUE by CreateGrid
3637 m_table
= (wxGridTableBase
*) NULL
;
3640 m_cellEditCtrlEnabled
= FALSE
;
3642 m_defaultCellAttr
= new wxGridCellAttr();
3644 // Set default cell attributes
3645 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3646 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3647 m_defaultCellAttr
->SetFont(GetFont());
3648 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3649 m_defaultCellAttr
->SetTextColour(
3650 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3651 m_defaultCellAttr
->SetBackgroundColour(
3652 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3653 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3654 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3659 m_currentCellCoords
= wxGridNoCellCoords
;
3661 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3662 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3664 // create the type registry
3665 m_typeRegistry
= new wxGridTypeRegistry
;
3668 // subwindow components that make up the wxGrid
3669 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3674 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3679 m_colLabelWin
= new wxGridColLabelWindow( this,
3684 m_gridWin
= new wxGridWindow( this,
3691 SetTargetWindow( m_gridWin
);
3697 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3698 wxGrid::wxGridSelectionModes selmode
)
3700 wxCHECK_MSG( !m_created
,
3702 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3704 m_numRows
= numRows
;
3705 m_numCols
= numCols
;
3707 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3708 m_table
->SetView( this );
3710 m_selection
= new wxGridSelection( this, selmode
);
3719 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3721 wxCHECK_RET( m_created
,
3722 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3724 m_selection
->SetSelectionMode( selmode
);
3727 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3728 wxGrid::wxGridSelectionModes selmode
)
3732 // RD: Actually, this should probably be allowed. I think it would be
3733 // nice to be able to switch multiple Tables in and out of a single
3734 // View at runtime. Is there anything in the implementation that
3735 // would prevent this?
3737 // At least, you now have to cope with m_selection
3738 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3743 m_numRows
= table
->GetNumberRows();
3744 m_numCols
= table
->GetNumberCols();
3747 m_table
->SetView( this );
3750 m_selection
= new wxGridSelection( this, selmode
);
3763 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3764 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3766 if ( m_rowLabelWin
)
3768 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3772 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3775 m_labelTextColour
= wxColour( _T("BLACK") );
3778 m_attrCache
.row
= -1;
3780 // TODO: something better than this ?
3782 m_labelFont
= this->GetFont();
3783 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3785 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3786 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3788 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3789 m_colLabelVertAlign
= wxALIGN_TOP
;
3791 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3792 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3794 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3795 m_defaultRowHeight
+= 8;
3797 m_defaultRowHeight
+= 4;
3800 m_gridLineColour
= wxColour( 128, 128, 255 );
3801 m_gridLinesEnabled
= TRUE
;
3802 m_cellHighlightColour
= m_gridLineColour
;
3803 m_cellHighlightPenWidth
= 2;
3804 m_cellHighlightROPenWidth
= 1;
3806 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3807 m_winCapture
= (wxWindow
*)NULL
;
3808 m_canDragRowSize
= TRUE
;
3809 m_canDragColSize
= TRUE
;
3810 m_canDragGridSize
= TRUE
;
3812 m_dragRowOrCol
= -1;
3813 m_isDragging
= FALSE
;
3814 m_startDragPos
= wxDefaultPosition
;
3816 m_waitForSlowClick
= FALSE
;
3818 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3819 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3821 m_currentCellCoords
= wxGridNoCellCoords
;
3823 m_selectingTopLeft
= wxGridNoCellCoords
;
3824 m_selectingBottomRight
= wxGridNoCellCoords
;
3825 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3826 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3828 m_editable
= TRUE
; // default for whole grid
3830 m_inOnKeyDown
= FALSE
;
3837 // ----------------------------------------------------------------------------
3838 // the idea is to call these functions only when necessary because they create
3839 // quite big arrays which eat memory mostly unnecessary - in particular, if
3840 // default widths/heights are used for all rows/columns, we may not use these
3843 // with some extra code, it should be possible to only store the
3844 // widths/heights different from default ones but this will be done later...
3845 // ----------------------------------------------------------------------------
3847 void wxGrid::InitRowHeights()
3849 m_rowHeights
.Empty();
3850 m_rowBottoms
.Empty();
3852 m_rowHeights
.Alloc( m_numRows
);
3853 m_rowBottoms
.Alloc( m_numRows
);
3857 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
3859 for ( int i
= 0; i
< m_numRows
; i
++ )
3861 rowBottom
+= m_defaultRowHeight
;
3862 m_rowBottoms
.Add( rowBottom
);
3866 void wxGrid::InitColWidths()
3868 m_colWidths
.Empty();
3869 m_colRights
.Empty();
3871 m_colWidths
.Alloc( m_numCols
);
3872 m_colRights
.Alloc( m_numCols
);
3875 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
3877 for ( int i
= 0; i
< m_numCols
; i
++ )
3879 colRight
+= m_defaultColWidth
;
3880 m_colRights
.Add( colRight
);
3884 int wxGrid::GetColWidth(int col
) const
3886 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3889 int wxGrid::GetColLeft(int col
) const
3891 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3892 : m_colRights
[col
] - m_colWidths
[col
];
3895 int wxGrid::GetColRight(int col
) const
3897 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3901 int wxGrid::GetRowHeight(int row
) const
3903 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3906 int wxGrid::GetRowTop(int row
) const
3908 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3909 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3912 int wxGrid::GetRowBottom(int row
) const
3914 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3915 : m_rowBottoms
[row
];
3918 void wxGrid::CalcDimensions()
3921 GetClientSize( &cw
, &ch
);
3923 if ( m_rowLabelWin
->IsShown() )
3924 cw
-= m_rowLabelWidth
;
3925 if ( m_colLabelWin
->IsShown() )
3926 ch
-= m_colLabelHeight
;
3929 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3930 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3932 // preserve (more or less) the previous position
3934 GetViewStart( &x
, &y
);
3936 // maybe we don't need scrollbars at all?
3938 // also adjust the position to be valid for the new scroll rangs
3959 // do set scrollbar parameters
3960 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3961 GetScrollX(w
), GetScrollY(h
), x
, y
,
3962 GetBatchCount() != 0);
3964 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3965 // still must reposition the children
3970 void wxGrid::CalcWindowSizes()
3973 GetClientSize( &cw
, &ch
);
3975 if ( m_cornerLabelWin
->IsShown() )
3976 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3978 if ( m_colLabelWin
->IsShown() )
3979 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3981 if ( m_rowLabelWin
->IsShown() )
3982 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3984 if ( m_gridWin
->IsShown() )
3985 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3989 // this is called when the grid table sends a message to say that it
3990 // has been redimensioned
3992 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3995 bool result
= FALSE
;
3997 // Clear the attribute cache as the attribute might refer to a different
3998 // cell than stored in the cache after adding/removing rows/columns.
4000 // By the same reasoning, the editor should be dismissed if columns are
4001 // added or removed. And for consistency, it should IMHO always be
4002 // removed, not only if the cell "underneath" it actually changes.
4003 // For now, I intentionally do not save the editor's content as the
4004 // cell it might want to save that stuff to might no longer exist.
4005 DisableCellEditControl();
4007 // if we were using the default widths/heights so far, we must change them
4009 if ( m_colWidths
.IsEmpty() )
4014 if ( m_rowHeights
.IsEmpty() )
4020 switch ( msg
.GetId() )
4022 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4024 size_t pos
= msg
.GetCommandInt();
4025 int numRows
= msg
.GetCommandInt2();
4027 m_numRows
+= numRows
;
4029 if ( !m_rowHeights
.IsEmpty() )
4031 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4032 m_rowBottoms
.Insert( 0, pos
, numRows
);
4035 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4037 for ( i
= pos
; i
< m_numRows
; i
++ )
4039 bottom
+= m_rowHeights
[i
];
4040 m_rowBottoms
[i
] = bottom
;
4043 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4045 // if we have just inserted cols into an empty grid the current
4046 // cell will be undefined...
4048 SetCurrentCell( 0, 0 );
4052 m_selection
->UpdateRows( pos
, numRows
);
4053 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4055 attrProvider
->UpdateAttrRows( pos
, numRows
);
4057 if ( !GetBatchCount() )
4060 m_rowLabelWin
->Refresh();
4066 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4068 int numRows
= msg
.GetCommandInt();
4069 int oldNumRows
= m_numRows
;
4070 m_numRows
+= numRows
;
4072 if ( !m_rowHeights
.IsEmpty() )
4074 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4075 m_rowBottoms
.Add( 0, numRows
);
4078 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4080 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4082 bottom
+= m_rowHeights
[i
];
4083 m_rowBottoms
[i
] = bottom
;
4086 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4088 // if we have just inserted cols into an empty grid the current
4089 // cell will be undefined...
4091 SetCurrentCell( 0, 0 );
4093 if ( !GetBatchCount() )
4096 m_rowLabelWin
->Refresh();
4102 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4104 size_t pos
= msg
.GetCommandInt();
4105 int numRows
= msg
.GetCommandInt2();
4106 m_numRows
-= numRows
;
4108 if ( !m_rowHeights
.IsEmpty() )
4110 m_rowHeights
.RemoveAt( pos
, numRows
);
4111 m_rowBottoms
.RemoveAt( pos
, numRows
);
4114 for ( i
= 0; i
< m_numRows
; i
++ )
4116 h
+= m_rowHeights
[i
];
4117 m_rowBottoms
[i
] = h
;
4122 m_currentCellCoords
= wxGridNoCellCoords
;
4126 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4127 m_currentCellCoords
.Set( 0, 0 );
4131 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4132 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4134 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4135 // ifdef'd out following patch from Paul Gammans
4137 // No need to touch column attributes, unless we
4138 // removed _all_ rows, in this case, we remove
4139 // all column attributes.
4140 // I hate to do this here, but the
4141 // needed data is not available inside UpdateAttrRows.
4142 if ( !GetNumberRows() )
4143 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4146 if ( !GetBatchCount() )
4149 m_rowLabelWin
->Refresh();
4155 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4157 size_t pos
= msg
.GetCommandInt();
4158 int numCols
= msg
.GetCommandInt2();
4159 m_numCols
+= numCols
;
4161 if ( !m_colWidths
.IsEmpty() )
4163 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4164 m_colRights
.Insert( 0, pos
, numCols
);
4167 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4169 for ( i
= pos
; i
< m_numCols
; i
++ )
4171 right
+= m_colWidths
[i
];
4172 m_colRights
[i
] = right
;
4175 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4177 // if we have just inserted cols into an empty grid the current
4178 // cell will be undefined...
4180 SetCurrentCell( 0, 0 );
4184 m_selection
->UpdateCols( pos
, numCols
);
4185 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4187 attrProvider
->UpdateAttrCols( pos
, numCols
);
4188 if ( !GetBatchCount() )
4191 m_colLabelWin
->Refresh();
4198 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4200 int numCols
= msg
.GetCommandInt();
4201 int oldNumCols
= m_numCols
;
4202 m_numCols
+= numCols
;
4203 if ( !m_colWidths
.IsEmpty() )
4205 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4206 m_colRights
.Add( 0, numCols
);
4209 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4211 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4213 right
+= m_colWidths
[i
];
4214 m_colRights
[i
] = right
;
4217 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4219 // if we have just inserted cols into an empty grid the current
4220 // cell will be undefined...
4222 SetCurrentCell( 0, 0 );
4224 if ( !GetBatchCount() )
4227 m_colLabelWin
->Refresh();
4233 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4235 size_t pos
= msg
.GetCommandInt();
4236 int numCols
= msg
.GetCommandInt2();
4237 m_numCols
-= numCols
;
4239 if ( !m_colWidths
.IsEmpty() )
4241 m_colWidths
.RemoveAt( pos
, numCols
);
4242 m_colRights
.RemoveAt( pos
, numCols
);
4245 for ( i
= 0; i
< m_numCols
; i
++ )
4247 w
+= m_colWidths
[i
];
4253 m_currentCellCoords
= wxGridNoCellCoords
;
4257 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4258 m_currentCellCoords
.Set( 0, 0 );
4262 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4263 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4265 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4266 // ifdef'd out following patch from Paul Gammans
4268 // No need to touch row attributes, unless we
4269 // removed _all_ columns, in this case, we remove
4270 // all row attributes.
4271 // I hate to do this here, but the
4272 // needed data is not available inside UpdateAttrCols.
4273 if ( !GetNumberCols() )
4274 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4277 if ( !GetBatchCount() )
4280 m_colLabelWin
->Refresh();
4287 if (result
&& !GetBatchCount() )
4288 m_gridWin
->Refresh();
4293 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4295 wxRegionIterator
iter( reg
);
4298 wxArrayInt rowlabels
;
4305 // TODO: remove this when we can...
4306 // There is a bug in wxMotif that gives garbage update
4307 // rectangles if you jump-scroll a long way by clicking the
4308 // scrollbar with middle button. This is a work-around
4310 #if defined(__WXMOTIF__)
4312 m_gridWin
->GetClientSize( &cw
, &ch
);
4313 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4314 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4317 // logical bounds of update region
4320 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4321 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4323 // find the row labels within these bounds
4326 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4328 if ( GetRowBottom(row
) < top
)
4331 if ( GetRowTop(row
) > bottom
)
4334 rowlabels
.Add( row
);
4344 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4346 wxRegionIterator
iter( reg
);
4349 wxArrayInt colLabels
;
4356 // TODO: remove this when we can...
4357 // There is a bug in wxMotif that gives garbage update
4358 // rectangles if you jump-scroll a long way by clicking the
4359 // scrollbar with middle button. This is a work-around
4361 #if defined(__WXMOTIF__)
4363 m_gridWin
->GetClientSize( &cw
, &ch
);
4364 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4365 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4368 // logical bounds of update region
4371 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4372 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4374 // find the cells within these bounds
4377 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4379 if ( GetColRight(col
) < left
)
4382 if ( GetColLeft(col
) > right
)
4385 colLabels
.Add( col
);
4394 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4396 wxRegionIterator
iter( reg
);
4399 wxGridCellCoordsArray cellsExposed
;
4401 int left
, top
, right
, bottom
;
4406 // TODO: remove this when we can...
4407 // There is a bug in wxMotif that gives garbage update
4408 // rectangles if you jump-scroll a long way by clicking the
4409 // scrollbar with middle button. This is a work-around
4411 #if defined(__WXMOTIF__)
4413 m_gridWin
->GetClientSize( &cw
, &ch
);
4414 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4415 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4416 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4417 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4420 // logical bounds of update region
4422 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4423 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4425 // find the cells within these bounds
4428 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4430 if ( GetRowBottom(row
) <= top
)
4433 if ( GetRowTop(row
) > bottom
)
4436 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4438 if ( GetColRight(col
) <= left
)
4441 if ( GetColLeft(col
) > right
)
4444 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4451 return cellsExposed
;
4455 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4458 wxPoint
pos( event
.GetPosition() );
4459 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4461 if ( event
.Dragging() )
4465 m_isDragging
= TRUE
;
4466 m_rowLabelWin
->CaptureMouse();
4469 if ( event
.LeftIsDown() )
4471 switch( m_cursorMode
)
4473 case WXGRID_CURSOR_RESIZE_ROW
:
4475 int cw
, ch
, left
, dummy
;
4476 m_gridWin
->GetClientSize( &cw
, &ch
);
4477 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4479 wxClientDC
dc( m_gridWin
);
4482 GetRowTop(m_dragRowOrCol
) +
4483 GetRowMinimalHeight(m_dragRowOrCol
) );
4484 dc
.SetLogicalFunction(wxINVERT
);
4485 if ( m_dragLastPos
>= 0 )
4487 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4489 dc
.DrawLine( left
, y
, left
+cw
, y
);
4494 case WXGRID_CURSOR_SELECT_ROW
:
4495 if ( (row
= YToRow( y
)) >= 0 )
4499 m_selection
->SelectRow( row
,
4500 event
.ControlDown(),
4507 // default label to suppress warnings about "enumeration value
4508 // 'xxx' not handled in switch
4516 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4521 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4522 m_isDragging
= FALSE
;
4525 // ------------ Entering or leaving the window
4527 if ( event
.Entering() || event
.Leaving() )
4529 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4533 // ------------ Left button pressed
4535 else if ( event
.LeftDown() )
4537 // don't send a label click event for a hit on the
4538 // edge of the row label - this is probably the user
4539 // wanting to resize the row
4541 if ( YToEdgeOfRow(y
) < 0 )
4545 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4547 if ( !event
.ShiftDown() && !event
.ControlDown() )
4549 else if ( m_selection
)
4551 if ( event
.ShiftDown() )
4553 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4556 GetNumberCols() - 1,
4557 event
.ControlDown(),
4564 m_selection
->SelectRow( row
,
4565 event
.ControlDown(),
4572 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4577 // starting to drag-resize a row
4579 if ( CanDragRowSize() )
4580 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4585 // ------------ Left double click
4587 else if (event
.LeftDClick() )
4589 if ( YToEdgeOfRow(y
) < 0 )
4592 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4597 // ------------ Left button released
4599 else if ( event
.LeftUp() )
4601 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4603 DoEndDragResizeRow();
4605 // Note: we are ending the event *after* doing
4606 // default processing in this case
4608 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4611 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4616 // ------------ Right button down
4618 else if ( event
.RightDown() )
4621 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4623 // no default action at the moment
4628 // ------------ Right double click
4630 else if ( event
.RightDClick() )
4633 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4635 // no default action at the moment
4640 // ------------ No buttons down and mouse moving
4642 else if ( event
.Moving() )
4644 m_dragRowOrCol
= YToEdgeOfRow( y
);
4645 if ( m_dragRowOrCol
>= 0 )
4647 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4649 // don't capture the mouse yet
4650 if ( CanDragRowSize() )
4651 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4654 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4656 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4662 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4665 wxPoint
pos( event
.GetPosition() );
4666 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4668 if ( event
.Dragging() )
4672 m_isDragging
= TRUE
;
4673 m_colLabelWin
->CaptureMouse();
4676 if ( event
.LeftIsDown() )
4678 switch( m_cursorMode
)
4680 case WXGRID_CURSOR_RESIZE_COL
:
4682 int cw
, ch
, dummy
, top
;
4683 m_gridWin
->GetClientSize( &cw
, &ch
);
4684 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4686 wxClientDC
dc( m_gridWin
);
4689 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4690 GetColMinimalWidth(m_dragRowOrCol
));
4691 dc
.SetLogicalFunction(wxINVERT
);
4692 if ( m_dragLastPos
>= 0 )
4694 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4696 dc
.DrawLine( x
, top
, x
, top
+ch
);
4701 case WXGRID_CURSOR_SELECT_COL
:
4702 if ( (col
= XToCol( x
)) >= 0 )
4706 m_selection
->SelectCol( col
,
4707 event
.ControlDown(),
4714 // default label to suppress warnings about "enumeration value
4715 // 'xxx' not handled in switch
4723 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4728 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4729 m_isDragging
= FALSE
;
4732 // ------------ Entering or leaving the window
4734 if ( event
.Entering() || event
.Leaving() )
4736 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4740 // ------------ Left button pressed
4742 else if ( event
.LeftDown() )
4744 // don't send a label click event for a hit on the
4745 // edge of the col label - this is probably the user
4746 // wanting to resize the col
4748 if ( XToEdgeOfCol(x
) < 0 )
4752 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4754 if ( !event
.ShiftDown() && !event
.ControlDown() )
4758 if ( event
.ShiftDown() )
4760 m_selection
->SelectBlock( 0,
4761 m_currentCellCoords
.GetCol(),
4762 GetNumberRows() - 1, col
,
4763 event
.ControlDown(),
4770 m_selection
->SelectCol( col
,
4771 event
.ControlDown(),
4778 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4783 // starting to drag-resize a col
4785 if ( CanDragColSize() )
4786 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4791 // ------------ Left double click
4793 if ( event
.LeftDClick() )
4795 if ( XToEdgeOfCol(x
) < 0 )
4798 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4803 // ------------ Left button released
4805 else if ( event
.LeftUp() )
4807 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4809 DoEndDragResizeCol();
4811 // Note: we are ending the event *after* doing
4812 // default processing in this case
4814 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4817 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4822 // ------------ Right button down
4824 else if ( event
.RightDown() )
4827 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4829 // no default action at the moment
4834 // ------------ Right double click
4836 else if ( event
.RightDClick() )
4839 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4841 // no default action at the moment
4846 // ------------ No buttons down and mouse moving
4848 else if ( event
.Moving() )
4850 m_dragRowOrCol
= XToEdgeOfCol( x
);
4851 if ( m_dragRowOrCol
>= 0 )
4853 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4855 // don't capture the cursor yet
4856 if ( CanDragColSize() )
4857 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4860 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4862 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4868 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4870 if ( event
.LeftDown() )
4872 // indicate corner label by having both row and
4875 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4881 else if ( event
.LeftDClick() )
4883 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4886 else if ( event
.RightDown() )
4888 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4890 // no default action at the moment
4894 else if ( event
.RightDClick() )
4896 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4898 // no default action at the moment
4903 void wxGrid::ChangeCursorMode(CursorMode mode
,
4908 static const wxChar
*cursorModes
[] =
4917 wxLogTrace(_T("grid"),
4918 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4919 win
== m_colLabelWin
? _T("colLabelWin")
4920 : win
? _T("rowLabelWin")
4922 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4923 #endif // __WXDEBUG__
4925 if ( mode
== m_cursorMode
&&
4926 win
== m_winCapture
&&
4927 captureMouse
== (m_winCapture
!= NULL
))
4932 // by default use the grid itself
4938 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4939 m_winCapture
= (wxWindow
*)NULL
;
4942 m_cursorMode
= mode
;
4944 switch ( m_cursorMode
)
4946 case WXGRID_CURSOR_RESIZE_ROW
:
4947 win
->SetCursor( m_rowResizeCursor
);
4950 case WXGRID_CURSOR_RESIZE_COL
:
4951 win
->SetCursor( m_colResizeCursor
);
4955 win
->SetCursor( *wxSTANDARD_CURSOR
);
4958 // we need to capture mouse when resizing
4959 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4960 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4962 if ( captureMouse
&& resize
)
4964 win
->CaptureMouse();
4969 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4972 wxPoint
pos( event
.GetPosition() );
4973 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4975 wxGridCellCoords coords
;
4976 XYToCell( x
, y
, coords
);
4978 int cell_rows
, cell_cols
;
4979 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4980 if ((cell_rows
< 0) || (cell_cols
< 0))
4982 coords
.SetRow(coords
.GetRow() + cell_rows
);
4983 coords
.SetCol(coords
.GetCol() + cell_cols
);
4986 if ( event
.Dragging() )
4988 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4990 // Don't start doing anything until the mouse has been drug at
4991 // least 3 pixels in any direction...
4994 if (m_startDragPos
== wxDefaultPosition
)
4996 m_startDragPos
= pos
;
4999 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5003 m_isDragging
= TRUE
;
5004 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5006 // Hide the edit control, so it
5007 // won't interfer with drag-shrinking.
5008 if ( IsCellEditControlShown() )
5010 HideCellEditControl();
5011 SaveEditControlValue();
5014 // Have we captured the mouse yet?
5017 m_winCapture
= m_gridWin
;
5018 m_winCapture
->CaptureMouse();
5021 if ( coords
!= wxGridNoCellCoords
)
5023 if ( event
.ControlDown() )
5025 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5026 m_selectingKeyboard
= coords
;
5027 HighlightBlock ( m_selectingKeyboard
, coords
);
5031 if ( !IsSelection() )
5033 HighlightBlock( coords
, coords
);
5037 HighlightBlock( m_currentCellCoords
, coords
);
5041 if (! IsVisible(coords
))
5043 MakeCellVisible(coords
);
5044 // TODO: need to introduce a delay or something here. The
5045 // scrolling is way to fast, at least on MSW - also on GTK.
5049 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5051 int cw
, ch
, left
, dummy
;
5052 m_gridWin
->GetClientSize( &cw
, &ch
);
5053 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5055 wxClientDC
dc( m_gridWin
);
5057 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5058 GetRowMinimalHeight(m_dragRowOrCol
) );
5059 dc
.SetLogicalFunction(wxINVERT
);
5060 if ( m_dragLastPos
>= 0 )
5062 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5064 dc
.DrawLine( left
, y
, left
+cw
, y
);
5067 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5069 int cw
, ch
, dummy
, top
;
5070 m_gridWin
->GetClientSize( &cw
, &ch
);
5071 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5073 wxClientDC
dc( m_gridWin
);
5075 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5076 GetColMinimalWidth(m_dragRowOrCol
) );
5077 dc
.SetLogicalFunction(wxINVERT
);
5078 if ( m_dragLastPos
>= 0 )
5080 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5082 dc
.DrawLine( x
, top
, x
, top
+ch
);
5089 m_isDragging
= FALSE
;
5090 m_startDragPos
= wxDefaultPosition
;
5092 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5093 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5096 if ( event
.Entering() || event
.Leaving() )
5098 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5099 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5104 // ------------ Left button pressed
5106 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5108 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5113 if ( !event
.ControlDown() )
5115 if ( event
.ShiftDown() )
5119 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5120 m_currentCellCoords
.GetCol(),
5123 event
.ControlDown(),
5129 else if ( XToEdgeOfCol(x
) < 0 &&
5130 YToEdgeOfRow(y
) < 0 )
5132 DisableCellEditControl();
5133 MakeCellVisible( coords
);
5135 // if this is the second click on this cell then start
5137 if ( m_waitForSlowClick
&&
5138 (coords
== m_currentCellCoords
) &&
5139 CanEnableCellControl())
5141 EnableCellEditControl();
5143 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5144 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5147 editor
->StartingClick();
5151 m_waitForSlowClick
= FALSE
;
5155 if ( event
.ControlDown() )
5159 m_selection
->ToggleCellSelection( coords
.GetRow(),
5161 event
.ControlDown(),
5166 m_selectingTopLeft
= wxGridNoCellCoords
;
5167 m_selectingBottomRight
= wxGridNoCellCoords
;
5168 m_selectingKeyboard
= coords
;
5172 SetCurrentCell( coords
);
5175 if ( m_selection
->GetSelectionMode() !=
5176 wxGrid::wxGridSelectCells
)
5178 HighlightBlock( coords
, coords
);
5182 m_waitForSlowClick
= TRUE
;
5189 // ------------ Left double click
5191 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5193 DisableCellEditControl();
5195 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5197 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5205 // ------------ Left button released
5207 else if ( event
.LeftUp() )
5209 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5211 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5212 m_selectingBottomRight
!= wxGridNoCellCoords
)
5216 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5217 m_winCapture
= NULL
;
5222 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5223 m_selectingTopLeft
.GetCol(),
5224 m_selectingBottomRight
.GetRow(),
5225 m_selectingBottomRight
.GetCol(),
5226 event
.ControlDown(),
5232 m_selectingTopLeft
= wxGridNoCellCoords
;
5233 m_selectingBottomRight
= wxGridNoCellCoords
;
5236 // Show the edit control, if it has been hidden for
5238 ShowCellEditControl();
5240 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5242 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5243 DoEndDragResizeRow();
5245 // Note: we are ending the event *after* doing
5246 // default processing in this case
5248 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5250 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5252 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5253 DoEndDragResizeCol();
5255 // Note: we are ending the event *after* doing
5256 // default processing in this case
5258 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5265 // ------------ Right button down
5267 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5269 DisableCellEditControl();
5270 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5275 // no default action at the moment
5280 // ------------ Right double click
5282 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5284 DisableCellEditControl();
5285 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5290 // no default action at the moment
5294 // ------------ Moving and no button action
5296 else if ( event
.Moving() && !event
.IsButton() )
5298 int dragRow
= YToEdgeOfRow( y
);
5299 int dragCol
= XToEdgeOfCol( x
);
5301 // Dragging on the corner of a cell to resize in both
5302 // directions is not implemented yet...
5304 if ( dragRow
>= 0 && dragCol
>= 0 )
5306 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5312 m_dragRowOrCol
= dragRow
;
5314 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5316 if ( CanDragRowSize() && CanDragGridSize() )
5317 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5322 m_dragRowOrCol
= dragCol
;
5330 m_dragRowOrCol
= dragCol
;
5332 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5334 if ( CanDragColSize() && CanDragGridSize() )
5335 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5341 // Neither on a row or col edge
5343 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5345 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5351 void wxGrid::DoEndDragResizeRow()
5353 if ( m_dragLastPos
>= 0 )
5355 // erase the last line and resize the row
5357 int cw
, ch
, left
, dummy
;
5358 m_gridWin
->GetClientSize( &cw
, &ch
);
5359 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5361 wxClientDC
dc( m_gridWin
);
5363 dc
.SetLogicalFunction( wxINVERT
);
5364 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5365 HideCellEditControl();
5366 SaveEditControlValue();
5368 int rowTop
= GetRowTop(m_dragRowOrCol
);
5369 SetRowSize( m_dragRowOrCol
,
5370 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5372 if ( !GetBatchCount() )
5374 // Only needed to get the correct rect.y:
5375 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5377 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5378 rect
.width
= m_rowLabelWidth
;
5379 rect
.height
= ch
- rect
.y
;
5380 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5382 // if there is a multicell block, paint all of it
5385 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5386 int leftCol
= XToCol(left
);
5387 int rightCol
= XToCol(left
+cw
);
5390 if (rightCol
< 0) rightCol
= m_numCols
;
5391 for (i
=leftCol
; i
<rightCol
; i
++)
5393 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5394 if (cell_rows
< subtract_rows
)
5395 subtract_rows
= cell_rows
;
5397 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5398 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5399 rect
.height
= ch
- rect
.y
;
5402 m_gridWin
->Refresh( FALSE
, &rect
);
5405 ShowCellEditControl();
5410 void wxGrid::DoEndDragResizeCol()
5412 if ( m_dragLastPos
>= 0 )
5414 // erase the last line and resize the col
5416 int cw
, ch
, dummy
, top
;
5417 m_gridWin
->GetClientSize( &cw
, &ch
);
5418 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5420 wxClientDC
dc( m_gridWin
);
5422 dc
.SetLogicalFunction( wxINVERT
);
5423 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5424 HideCellEditControl();
5425 SaveEditControlValue();
5427 int colLeft
= GetColLeft(m_dragRowOrCol
);
5428 SetColSize( m_dragRowOrCol
,
5429 wxMax( m_dragLastPos
- colLeft
,
5430 GetColMinimalWidth(m_dragRowOrCol
) ) );
5432 if ( !GetBatchCount() )
5434 // Only needed to get the correct rect.x:
5435 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5437 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5438 rect
.width
= cw
- rect
.x
;
5439 rect
.height
= m_colLabelHeight
;
5440 m_colLabelWin
->Refresh( TRUE
, &rect
);
5442 // if there is a multicell block, paint all of it
5445 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5446 int topRow
= YToRow(top
);
5447 int bottomRow
= YToRow(top
+cw
);
5450 if (bottomRow
< 0) bottomRow
= m_numRows
;
5451 for (i
=topRow
; i
<bottomRow
; i
++)
5453 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5454 if (cell_cols
< subtract_cols
)
5455 subtract_cols
= cell_cols
;
5457 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5458 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5459 rect
.width
= cw
- rect
.x
;
5462 m_gridWin
->Refresh( FALSE
, &rect
);
5465 ShowCellEditControl();
5472 // ------ interaction with data model
5474 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5476 switch ( msg
.GetId() )
5478 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5479 return GetModelValues();
5481 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5482 return SetModelValues();
5484 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5485 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5486 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5487 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5488 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5489 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5490 return Redimension( msg
);
5499 // The behaviour of this function depends on the grid table class
5500 // Clear() function. For the default wxGridStringTable class the
5501 // behavious is to replace all cell contents with wxEmptyString but
5502 // not to change the number of rows or cols.
5504 void wxGrid::ClearGrid()
5508 if (IsCellEditControlEnabled())
5509 DisableCellEditControl();
5512 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5517 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5519 // TODO: something with updateLabels flag
5523 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5529 if (IsCellEditControlEnabled())
5530 DisableCellEditControl();
5532 return m_table
->InsertRows( pos
, numRows
);
5534 // the table will have sent the results of the insert row
5535 // operation to this view object as a grid table message
5541 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5543 // TODO: something with updateLabels flag
5547 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5551 return ( m_table
&& m_table
->AppendRows( numRows
) );
5552 // the table will have sent the results of the append row
5553 // operation to this view object as a grid table message
5557 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5559 // TODO: something with updateLabels flag
5563 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5569 if (IsCellEditControlEnabled())
5570 DisableCellEditControl();
5572 return (m_table
->DeleteRows( pos
, numRows
));
5573 // the table will have sent the results of the delete row
5574 // operation to this view object as a grid table message
5580 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5582 // TODO: something with updateLabels flag
5586 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5592 if (IsCellEditControlEnabled())
5593 DisableCellEditControl();
5595 return m_table
->InsertCols( pos
, numCols
);
5596 // the table will have sent the results of the insert col
5597 // operation to this view object as a grid table message
5603 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5605 // TODO: something with updateLabels flag
5609 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5613 return ( m_table
&& m_table
->AppendCols( numCols
) );
5614 // the table will have sent the results of the append col
5615 // operation to this view object as a grid table message
5619 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5621 // TODO: something with updateLabels flag
5625 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5631 if (IsCellEditControlEnabled())
5632 DisableCellEditControl();
5634 return ( m_table
->DeleteCols( pos
, numCols
) );
5635 // the table will have sent the results of the delete col
5636 // operation to this view object as a grid table message
5644 // ----- event handlers
5647 // Generate a grid event based on a mouse event and
5648 // return the result of ProcessEvent()
5650 int wxGrid::SendEvent( const wxEventType type
,
5652 wxMouseEvent
& mouseEv
)
5657 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5659 int rowOrCol
= (row
== -1 ? col
: row
);
5661 wxGridSizeEvent
gridEvt( GetId(),
5665 mouseEv
.GetX() + GetRowLabelSize(),
5666 mouseEv
.GetY() + GetColLabelSize(),
5667 mouseEv
.ControlDown(),
5668 mouseEv
.ShiftDown(),
5670 mouseEv
.MetaDown() );
5672 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5673 vetoed
= !gridEvt
.IsAllowed();
5675 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5677 // Right now, it should _never_ end up here!
5678 wxGridRangeSelectEvent
gridEvt( GetId(),
5682 m_selectingBottomRight
,
5684 mouseEv
.ControlDown(),
5685 mouseEv
.ShiftDown(),
5687 mouseEv
.MetaDown() );
5689 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5690 vetoed
= !gridEvt
.IsAllowed();
5694 wxGridEvent
gridEvt( GetId(),
5698 mouseEv
.GetX() + GetRowLabelSize(),
5699 mouseEv
.GetY() + GetColLabelSize(),
5701 mouseEv
.ControlDown(),
5702 mouseEv
.ShiftDown(),
5704 mouseEv
.MetaDown() );
5705 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5706 vetoed
= !gridEvt
.IsAllowed();
5709 // A Veto'd event may not be `claimed' so test this first
5710 if (vetoed
) return -1;
5711 return claimed
? 1 : 0;
5715 // Generate a grid event of specified type and return the result
5716 // of ProcessEvent().
5718 int wxGrid::SendEvent( const wxEventType type
,
5724 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5726 int rowOrCol
= (row
== -1 ? col
: row
);
5728 wxGridSizeEvent
gridEvt( GetId(),
5733 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5734 vetoed
= !gridEvt
.IsAllowed();
5738 wxGridEvent
gridEvt( GetId(),
5743 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5744 vetoed
= !gridEvt
.IsAllowed();
5747 // A Veto'd event may not be `claimed' so test this first
5748 if (vetoed
) return -1;
5749 return claimed
? 1 : 0;
5753 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5755 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5758 void wxGrid::Refresh(bool eraseb
, wxRect
* rect
)
5760 // Don't do anything if between Begin/EndBatch...
5761 // EndBatch() will do all this on the last nested one anyway.
5762 if (! GetBatchCount())
5764 wxScrolledWindow::Refresh(eraseb
,rect
);
5766 int off_x
=0 ,off_y
=0;
5767 wxRect
* anotherrect
= NULL
;
5771 //Copy rectangle can get scroll offsets..
5772 anotherrect
= new wxRect(*rect
);
5773 CalcScrolledPosition( 0, 0, &off_x
, &off_y
);
5775 //Corner label Doesn't move from the origin.
5776 m_cornerLabelWin
->Refresh(eraseb
,rect
);
5778 //Move Rect down for row labels...
5780 rect
->Offset(0,off_y
);
5781 m_rowLabelWin
->Refresh(eraseb
,rect
);
5783 //Move rect copy along for col labels...
5785 anotherrect
->Offset(off_x
,0);
5786 m_colLabelWin
->Refresh(eraseb
,anotherrect
);
5788 //Move main rect along (so it's down and across!)
5791 rect
->Offset(off_x
,0);
5792 m_gridWin
->Refresh(eraseb
,rect
);
5796 void wxGrid::OnSize( wxSizeEvent
& event
)
5798 // position the child windows
5801 // don't call CalcDimensions() from here, the base class handles the size
5807 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5809 if ( m_inOnKeyDown
)
5811 // shouldn't be here - we are going round in circles...
5813 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5816 m_inOnKeyDown
= TRUE
;
5818 // propagate the event up and see if it gets processed
5820 wxWindow
*parent
= GetParent();
5821 wxKeyEvent
keyEvt( event
);
5822 keyEvt
.SetEventObject( parent
);
5824 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5827 // try local handlers
5829 switch ( event
.KeyCode() )
5832 if ( event
.ControlDown() )
5834 MoveCursorUpBlock( event
.ShiftDown() );
5838 MoveCursorUp( event
.ShiftDown() );
5843 if ( event
.ControlDown() )
5845 MoveCursorDownBlock( event
.ShiftDown() );
5849 MoveCursorDown( event
.ShiftDown() );
5854 if ( event
.ControlDown() )
5856 MoveCursorLeftBlock( event
.ShiftDown() );
5860 MoveCursorLeft( event
.ShiftDown() );
5865 if ( event
.ControlDown() )
5867 MoveCursorRightBlock( event
.ShiftDown() );
5871 MoveCursorRight( event
.ShiftDown() );
5876 case WXK_NUMPAD_ENTER
:
5877 if ( event
.ControlDown() )
5879 event
.Skip(); // to let the edit control have the return
5883 if ( GetGridCursorRow() < GetNumberRows()-1 )
5885 MoveCursorDown( event
.ShiftDown() );
5889 // at the bottom of a column
5890 HideCellEditControl();
5891 SaveEditControlValue();
5901 if (event
.ShiftDown())
5903 if ( GetGridCursorCol() > 0 )
5905 MoveCursorLeft( FALSE
);
5910 HideCellEditControl();
5911 SaveEditControlValue();
5916 if ( GetGridCursorCol() < GetNumberCols()-1 )
5918 MoveCursorRight( FALSE
);
5923 HideCellEditControl();
5924 SaveEditControlValue();
5930 if ( event
.ControlDown() )
5932 MakeCellVisible( 0, 0 );
5933 SetCurrentCell( 0, 0 );
5942 if ( event
.ControlDown() )
5944 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5945 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5962 if ( event
.ControlDown() )
5966 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5967 m_currentCellCoords
.GetCol(),
5968 event
.ControlDown(),
5975 if ( !IsEditable() )
5977 MoveCursorRight( FALSE
);
5980 // Otherwise fall through to default
5983 // is it possible to edit the current cell at all?
5984 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5986 // yes, now check whether the cells editor accepts the key
5987 int row
= m_currentCellCoords
.GetRow();
5988 int col
= m_currentCellCoords
.GetCol();
5989 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5990 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5992 // <F2> is special and will always start editing, for
5993 // other keys - ask the editor itself
5994 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5995 || editor
->IsAcceptedKey(event
) )
5997 EnableCellEditControl();
5999 // the editor could be not shown for a variety of
6000 // reasons (i.e. blocked by the app or whatever), so
6001 // check if it really was created
6002 if ( m_cellEditCtrlEnabled
)
6004 editor
->StartingKey(event
);
6017 // let others process char events with modifiers or all
6018 // char events for readonly cells
6025 m_inOnKeyDown
= FALSE
;
6028 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6030 // try local handlers
6032 if ( event
.KeyCode() == WXK_SHIFT
)
6034 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6035 m_selectingBottomRight
!= wxGridNoCellCoords
)
6039 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6040 m_selectingTopLeft
.GetCol(),
6041 m_selectingBottomRight
.GetRow(),
6042 m_selectingBottomRight
.GetCol(),
6043 event
.ControlDown(),
6050 m_selectingTopLeft
= wxGridNoCellCoords
;
6051 m_selectingBottomRight
= wxGridNoCellCoords
;
6052 m_selectingKeyboard
= wxGridNoCellCoords
;
6056 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6060 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6062 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6064 // the event has been intercepted - do nothing
6068 wxClientDC
dc(m_gridWin
);
6071 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6073 HideCellEditControl();
6074 DisableCellEditControl();
6076 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6079 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
6080 if ( !m_gridLinesEnabled
)
6088 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6090 // Otherwise refresh redraws the highlight!
6091 m_currentCellCoords
= coords
;
6093 DrawGridCellArea(dc
,cells
);
6094 DrawAllGridLines( dc
, r
);
6098 m_currentCellCoords
= coords
;
6100 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6101 DrawCellHighlight(dc
, attr
);
6106 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6109 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6113 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6116 rightCol
= GetNumberCols() - 1;
6118 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6121 bottomRow
= GetNumberRows() - 1;
6125 if ( topRow
> bottomRow
)
6132 if ( leftCol
> rightCol
)
6139 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6140 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6142 if ( m_selectingTopLeft
!= updateTopLeft
||
6143 m_selectingBottomRight
!= updateBottomRight
)
6145 // Compute two optimal update rectangles:
6146 // Either one rectangle is a real subset of the
6147 // other, or they are (almost) disjoint!
6149 bool need_refresh
[4];
6153 need_refresh
[3] = FALSE
;
6156 // Store intermediate values
6157 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6158 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6159 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6160 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6162 // Determine the outer/inner coordinates.
6163 if (oldLeft
> leftCol
)
6169 if (oldTop
> topRow
)
6175 if (oldRight
< rightCol
)
6178 oldRight
= rightCol
;
6181 if (oldBottom
< bottomRow
)
6184 oldBottom
= bottomRow
;
6188 // Now, either the stuff marked old is the outer
6189 // rectangle or we don't have a situation where one
6190 // is contained in the other.
6192 if ( oldLeft
< leftCol
)
6194 need_refresh
[0] = TRUE
;
6195 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6197 wxGridCellCoords ( oldBottom
,
6201 if ( oldTop
< topRow
)
6203 need_refresh
[1] = TRUE
;
6204 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6206 wxGridCellCoords ( topRow
- 1,
6210 if ( oldRight
> rightCol
)
6212 need_refresh
[2] = TRUE
;
6213 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6215 wxGridCellCoords ( oldBottom
,
6219 if ( oldBottom
> bottomRow
)
6221 need_refresh
[3] = TRUE
;
6222 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6224 wxGridCellCoords ( oldBottom
,
6230 m_selectingTopLeft
= updateTopLeft
;
6231 m_selectingBottomRight
= updateBottomRight
;
6233 // various Refresh() calls
6234 for (i
= 0; i
< 4; i
++ )
6235 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6236 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6239 // never generate an event as it will be generated from
6240 // wxGridSelection::SelectBlock!
6241 // (old comment from when this was the body of SelectBlock)
6245 // ------ functions to get/send data (see also public functions)
6248 bool wxGrid::GetModelValues()
6252 // all we need to do is repaint the grid
6254 m_gridWin
->Refresh();
6262 bool wxGrid::SetModelValues()
6268 for ( row
= 0; row
< m_numRows
; row
++ )
6270 for ( col
= 0; col
< m_numCols
; col
++ )
6272 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6284 // Note - this function only draws cells that are in the list of
6285 // exposed cells (usually set from the update region by
6286 // CalcExposedCells)
6288 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6290 if ( !m_numRows
|| !m_numCols
) return;
6292 int i
, j
, k
, l
, numCells
= cells
.GetCount();
6293 int row
, col
, cell_rows
, cell_cols
;
6294 wxGridCellCoordsArray redrawCells
;
6296 for ( i
= numCells
-1; i
>= 0; i
-- )
6298 row
= cells
[i
].GetRow();
6299 col
= cells
[i
].GetCol();
6300 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6302 // If this cell is part of a multicell block, find owner for repaint
6303 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6305 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6306 bool marked
= FALSE
;
6307 for ( j
= 0; j
< numCells
; j
++ )
6309 if ( cell
== cells
[j
] )
6317 int count
= redrawCells
.GetCount();
6318 for (j
= 0; j
< count
; j
++)
6320 if ( cell
== redrawCells
[j
] )
6326 if (!marked
) redrawCells
.Add( cell
);
6328 continue; // don't bother drawing this cell
6331 // If this cell is empty, find cell to left that might want to overflow
6332 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6334 for ( l
= 0; l
< cell_rows
; l
++ )
6336 for (j
= col
-1; j
>= 0; j
--)
6338 if (!m_table
->IsEmptyCell(row
+l
, j
))
6340 if (GetCellOverflow(row
+l
, j
))
6342 wxGridCellCoords
cell(row
+l
, j
);
6343 bool marked
= FALSE
;
6345 for (k
= 0; k
< numCells
; k
++)
6347 if ( cell
== cells
[k
] )
6355 int count
= redrawCells
.GetCount();
6356 for (k
= 0; k
< count
; k
++)
6358 if ( cell
== redrawCells
[k
] )
6364 if (!marked
) redrawCells
.Add( cell
);
6372 DrawCell( dc
, cells
[i
] );
6375 numCells
= redrawCells
.GetCount();
6377 for ( i
= numCells
- 1; i
>= 0; i
-- )
6379 DrawCell( dc
, redrawCells
[i
] );
6384 void wxGrid::DrawGridSpace( wxDC
& dc
)
6387 m_gridWin
->GetClientSize( &cw
, &ch
);
6390 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6392 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6393 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6395 if ( right
> rightCol
|| bottom
> bottomRow
)
6398 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6400 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6401 dc
.SetPen( *wxTRANSPARENT_PEN
);
6403 if ( right
> rightCol
)
6405 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6408 if ( bottom
> bottomRow
)
6410 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6416 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6418 int row
= coords
.GetRow();
6419 int col
= coords
.GetCol();
6421 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6424 // we draw the cell border ourselves
6425 #if !WXGRID_DRAW_LINES
6426 if ( m_gridLinesEnabled
)
6427 DrawCellBorder( dc
, coords
);
6430 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6432 bool isCurrent
= coords
== m_currentCellCoords
;
6434 wxRect rect
= CellToRect( row
, col
);
6436 // if the editor is shown, we should use it and not the renderer
6437 // Note: However, only if it is really _shown_, i.e. not hidden!
6438 if ( isCurrent
&& IsCellEditControlShown() )
6440 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6441 editor
->PaintBackground(rect
, attr
);
6446 // but all the rest is drawn by the cell renderer and hence may be
6448 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6449 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6456 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6458 int row
= m_currentCellCoords
.GetRow();
6459 int col
= m_currentCellCoords
.GetCol();
6461 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6464 wxRect rect
= CellToRect(row
, col
);
6466 // hmmm... what could we do here to show that the cell is disabled?
6467 // for now, I just draw a thinner border than for the other ones, but
6468 // it doesn't look really good
6470 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6474 // The center of th drawn line is where the position/width/height of
6475 // the rectangle is actually at, (on wxMSW atr least,) so we will
6476 // reduce the size of the rectangle to compensate for the thickness of
6477 // the line. If this is too strange on non wxMSW platforms then
6478 // please #ifdef this appropriately.
6479 rect
.x
+= penWidth
/2;
6480 rect
.y
+= penWidth
/2;
6481 rect
.width
-= penWidth
-1;
6482 rect
.height
-= penWidth
-1;
6485 // Now draw the rectangle
6486 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6487 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6488 dc
.DrawRectangle(rect
);
6492 // VZ: my experiments with 3d borders...
6494 // how to properly set colours for arbitrary bg?
6495 wxCoord x1
= rect
.x
,
6497 x2
= rect
.x
+ rect
.width
-1,
6498 y2
= rect
.y
+ rect
.height
-1;
6500 dc
.SetPen(*wxWHITE_PEN
);
6501 dc
.DrawLine(x1
, y1
, x2
, y1
);
6502 dc
.DrawLine(x1
, y1
, x1
, y2
);
6504 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6505 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6507 dc
.SetPen(*wxBLACK_PEN
);
6508 dc
.DrawLine(x1
, y2
, x2
, y2
);
6509 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6514 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6516 int row
= coords
.GetRow();
6517 int col
= coords
.GetCol();
6518 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6521 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6523 wxRect rect
= CellToRect( row
, col
);
6525 // right hand border
6527 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6528 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6532 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6533 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6536 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6538 // This if block was previously in wxGrid::OnPaint but that doesn't
6539 // seem to get called under wxGTK - MB
6541 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6542 m_numRows
&& m_numCols
)
6544 m_currentCellCoords
.Set(0, 0);
6547 if ( IsCellEditControlShown() )
6549 // don't show highlight when the edit control is shown
6553 // if the active cell was repainted, repaint its highlight too because it
6554 // might have been damaged by the grid lines
6555 size_t count
= cells
.GetCount();
6556 for ( size_t n
= 0; n
< count
; n
++ )
6558 if ( cells
[n
] == m_currentCellCoords
)
6560 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6561 DrawCellHighlight(dc
, attr
);
6569 // TODO: remove this ???
6570 // This is used to redraw all grid lines e.g. when the grid line colour
6573 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6575 #if !WXGRID_DRAW_LINES
6579 if ( !m_gridLinesEnabled
||
6581 !m_numCols
) return;
6583 int top
, bottom
, left
, right
;
6585 #if 0 //#ifndef __WXGTK__
6589 m_gridWin
->GetClientSize(&cw
, &ch
);
6591 // virtual coords of visible area
6593 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6594 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6599 reg
.GetBox(x
, y
, w
, h
);
6600 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6601 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6605 m_gridWin
->GetClientSize(&cw
, &ch
);
6606 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6607 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6610 // avoid drawing grid lines past the last row and col
6612 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6613 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6615 // no gridlines inside multicells, clip them out
6616 int leftCol
= XToCol(left
);
6617 int topRow
= YToRow(top
);
6618 int rightCol
= XToCol(right
);
6619 int bottomRow
= YToRow(bottom
);
6620 wxRegion
clippedcells(0, 0, cw
, ch
);
6622 if ((leftCol
>= 0) && (topRow
>= 0))
6624 if (rightCol
< 0) rightCol
= m_numCols
;
6625 if (bottomRow
< 0) bottomRow
= m_numRows
;
6627 int i
, j
, cell_rows
, cell_cols
;
6630 for (j
=topRow
; j
<bottomRow
; j
++)
6632 for (i
=leftCol
; i
<rightCol
; i
++)
6634 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6635 if ((cell_rows
> 1) || (cell_cols
> 1))
6637 rect
= CellToRect(j
,i
);
6638 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6639 clippedcells
.Subtract(rect
);
6641 else if ((cell_rows
< 0) || (cell_cols
< 0))
6643 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6644 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6645 clippedcells
.Subtract(rect
);
6650 dc
.SetClippingRegion( clippedcells
);
6652 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6654 // horizontal grid lines
6657 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6659 int bot
= GetRowBottom(i
) - 1;
6668 dc
.DrawLine( left
, bot
, right
, bot
);
6673 // vertical grid lines
6675 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6677 int colRight
= GetColRight(i
) - 1;
6678 if ( colRight
> right
)
6683 if ( colRight
>= left
)
6685 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6688 dc
.DestroyClippingRegion();
6692 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6694 if ( !m_numRows
) return;
6697 size_t numLabels
= rows
.GetCount();
6699 for ( i
= 0; i
< numLabels
; i
++ )
6701 DrawRowLabel( dc
, rows
[i
] );
6706 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6708 if ( GetRowHeight(row
) <= 0 )
6711 int rowTop
= GetRowTop(row
),
6712 rowBottom
= GetRowBottom(row
) - 1;
6714 dc
.SetPen( *wxBLACK_PEN
);
6715 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6716 m_rowLabelWidth
-1, rowBottom
);
6718 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6720 dc
.SetPen( *wxWHITE_PEN
);
6721 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6722 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6724 dc
.SetBackgroundMode( wxTRANSPARENT
);
6725 dc
.SetTextForeground( GetLabelTextColour() );
6726 dc
.SetFont( GetLabelFont() );
6729 GetRowLabelAlignment( &hAlign
, &vAlign
);
6733 rect
.SetY( GetRowTop(row
) + 2 );
6734 rect
.SetWidth( m_rowLabelWidth
- 4 );
6735 rect
.SetHeight( GetRowHeight(row
) - 4 );
6736 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6740 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6742 if ( !m_numCols
) return;
6745 size_t numLabels
= cols
.GetCount();
6747 for ( i
= 0; i
< numLabels
; i
++ )
6749 DrawColLabel( dc
, cols
[i
] );
6754 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6756 if ( GetColWidth(col
) <= 0 )
6759 int colLeft
= GetColLeft(col
),
6760 colRight
= GetColRight(col
) - 1;
6762 dc
.SetPen( *wxBLACK_PEN
);
6763 dc
.DrawLine( colRight
, 0,
6764 colRight
, m_colLabelHeight
-1 );
6766 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6767 colRight
, m_colLabelHeight
-1 );
6769 dc
.SetPen( *wxWHITE_PEN
);
6770 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6771 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6773 dc
.SetBackgroundMode( wxTRANSPARENT
);
6774 dc
.SetTextForeground( GetLabelTextColour() );
6775 dc
.SetFont( GetLabelFont() );
6777 dc
.SetBackgroundMode( wxTRANSPARENT
);
6778 dc
.SetTextForeground( GetLabelTextColour() );
6779 dc
.SetFont( GetLabelFont() );
6782 GetColLabelAlignment( &hAlign
, &vAlign
);
6785 rect
.SetX( colLeft
+ 2 );
6787 rect
.SetWidth( GetColWidth(col
) - 4 );
6788 rect
.SetHeight( m_colLabelHeight
- 4 );
6789 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6792 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6793 const wxString
& value
,
6798 wxArrayString lines
;
6800 StringToLines( value
, lines
);
6803 //Forward to new API.
6804 DrawTextRectangle( dc
,
6812 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6813 const wxArrayString
& lines
,
6818 long textWidth
, textHeight
;
6819 long lineWidth
, lineHeight
;
6821 dc
.SetClippingRegion( rect
);
6822 if ( lines
.GetCount() )
6824 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6825 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6828 switch ( horizAlign
)
6831 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6834 case wxALIGN_CENTRE
:
6835 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6844 switch ( vertAlign
)
6846 case wxALIGN_BOTTOM
:
6847 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6850 case wxALIGN_CENTRE
:
6851 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6860 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6862 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6867 dc
.DestroyClippingRegion();
6871 // Split multi line text up into an array of strings. Any existing
6872 // contents of the string array are preserved.
6874 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6878 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6879 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6881 while ( startPos
< (int)tVal
.Length() )
6883 pos
= tVal
.Mid(startPos
).Find( eol
);
6888 else if ( pos
== 0 )
6890 lines
.Add( wxEmptyString
);
6894 lines
.Add( value
.Mid(startPos
, pos
) );
6898 if ( startPos
< (int)value
.Length() )
6900 lines
.Add( value
.Mid( startPos
) );
6905 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6906 const wxArrayString
& lines
,
6907 long *width
, long *height
)
6914 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6916 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6917 w
= wxMax( w
, lineW
);
6926 // ------ Batch processing.
6928 void wxGrid::EndBatch()
6930 if ( m_batchCount
> 0 )
6933 if ( !m_batchCount
)
6936 m_rowLabelWin
->Refresh();
6937 m_colLabelWin
->Refresh();
6938 m_cornerLabelWin
->Refresh();
6939 m_gridWin
->Refresh();
6944 // Use this, rather than wxWindow::Refresh(), to force an immediate
6945 // repainting of the grid. Has no effect if you are already inside a
6946 // BeginBatch / EndBatch block.
6948 void wxGrid::ForceRefresh()
6956 // ------ Edit control functions
6960 void wxGrid::EnableEditing( bool edit
)
6962 // TODO: improve this ?
6964 if ( edit
!= m_editable
)
6966 if(!edit
) EnableCellEditControl(edit
);
6972 void wxGrid::EnableCellEditControl( bool enable
)
6977 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6978 SetCurrentCell( 0, 0 );
6980 if ( enable
!= m_cellEditCtrlEnabled
)
6984 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6987 // this should be checked by the caller!
6988 wxASSERT_MSG( CanEnableCellControl(),
6989 _T("can't enable editing for this cell!") );
6991 // do it before ShowCellEditControl()
6992 m_cellEditCtrlEnabled
= enable
;
6994 ShowCellEditControl();
6998 //FIXME:add veto support
6999 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7001 HideCellEditControl();
7002 SaveEditControlValue();
7004 // do it after HideCellEditControl()
7005 m_cellEditCtrlEnabled
= enable
;
7010 bool wxGrid::IsCurrentCellReadOnly() const
7013 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7014 bool readonly
= attr
->IsReadOnly();
7020 bool wxGrid::CanEnableCellControl() const
7022 return m_editable
&& !IsCurrentCellReadOnly();
7025 bool wxGrid::IsCellEditControlEnabled() const
7027 // the cell edit control might be disable for all cells or just for the
7028 // current one if it's read only
7029 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7032 bool wxGrid::IsCellEditControlShown() const
7034 bool isShown
= FALSE
;
7036 if ( m_cellEditCtrlEnabled
)
7038 int row
= m_currentCellCoords
.GetRow();
7039 int col
= m_currentCellCoords
.GetCol();
7040 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7041 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7046 if ( editor
->IsCreated() )
7048 isShown
= editor
->GetControl()->IsShown();
7058 void wxGrid::ShowCellEditControl()
7060 if ( IsCellEditControlEnabled() )
7062 if ( !IsVisible( m_currentCellCoords
) )
7064 m_cellEditCtrlEnabled
= FALSE
;
7069 wxRect rect
= CellToRect( m_currentCellCoords
);
7070 int row
= m_currentCellCoords
.GetRow();
7071 int col
= m_currentCellCoords
.GetCol();
7073 // if this is part of a multicell, find owner (topleft)
7074 int cell_rows
, cell_cols
;
7075 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7076 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7080 m_currentCellCoords
.SetRow( row
);
7081 m_currentCellCoords
.SetCol( col
);
7084 // convert to scrolled coords
7086 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7088 // done in PaintBackground()
7090 // erase the highlight and the cell contents because the editor
7091 // might not cover the entire cell
7092 wxClientDC
dc( m_gridWin
);
7094 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7095 dc
.SetPen(*wxTRANSPARENT_PEN
);
7096 dc
.DrawRectangle(rect
);
7099 // cell is shifted by one pixel
7100 // However, don't allow x or y to become negative
7101 // since the SetSize() method interprets that as
7108 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7109 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7110 if ( !editor
->IsCreated() )
7112 editor
->Create(m_gridWin
, -1,
7113 new wxGridCellEditorEvtHandler(this, editor
));
7115 wxGridEditorCreatedEvent
evt(GetId(),
7116 wxEVT_GRID_EDITOR_CREATED
,
7120 editor
->GetControl());
7121 GetEventHandler()->ProcessEvent(evt
);
7124 editor
->Show( TRUE
, attr
);
7126 // resize editor to overflow into righthand cells if allowed
7127 wxString value
= GetCellValue(row
, col
);
7128 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7130 wxClientDC
dc(m_gridWin
);
7131 wxCoord y
= 0, best_width
= 0;
7132 dc
.SetFont(attr
->GetFont());
7133 dc
.GetTextExtent(value
, &best_width
, &y
);
7135 int cell_rows
, cell_cols
;
7136 attr
->GetSize( &cell_rows
, &cell_cols
);
7138 if ((best_width
> rect
.width
) && (col
< m_numCols
) && m_table
)
7141 for (i
= col
+cell_cols
; i
< m_numCols
; i
++)
7143 if (m_table
->IsEmptyCell(row
,i
))
7145 rect
.width
+= GetColWidth(i
);
7146 if (rect
.width
>= best_width
) break;
7153 editor
->SetSize( rect
);
7155 editor
->BeginEdit(row
, col
, this);
7164 void wxGrid::HideCellEditControl()
7166 if ( IsCellEditControlEnabled() )
7168 int row
= m_currentCellCoords
.GetRow();
7169 int col
= m_currentCellCoords
.GetCol();
7171 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7172 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7173 editor
->Show( FALSE
);
7176 m_gridWin
->SetFocus();
7177 // refresh whole row to the right
7178 wxRect
rect( CellToRect(row
, col
) );
7179 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7180 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7181 m_gridWin
->Refresh( FALSE
, &rect
);
7186 void wxGrid::SaveEditControlValue()
7188 if ( IsCellEditControlEnabled() )
7190 int row
= m_currentCellCoords
.GetRow();
7191 int col
= m_currentCellCoords
.GetCol();
7193 wxString oldval
= GetCellValue(row
,col
);
7195 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7196 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7197 bool changed
= editor
->EndEdit(row
, col
, this);
7204 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7205 m_currentCellCoords
.GetRow(),
7206 m_currentCellCoords
.GetCol() ) < 0 ) {
7208 // Event has been vetoed, set the data back.
7209 SetCellValue(row
,col
,oldval
);
7217 // ------ Grid location functions
7218 // Note that all of these functions work with the logical coordinates of
7219 // grid cells and labels so you will need to convert from device
7220 // coordinates for mouse events etc.
7223 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7225 int row
= YToRow(y
);
7226 int col
= XToCol(x
);
7228 if ( row
== -1 || col
== -1 )
7230 coords
= wxGridNoCellCoords
;
7234 coords
.Set( row
, col
);
7239 // Internal Helper function for computing row or column from some
7240 // (unscrolled) coordinate value, using either
7241 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7242 // of m_rowBottoms/m_ColRights to speed up the search!
7244 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7245 const wxArrayInt
& BorderArray
, bool maxOnOverflow
)
7249 size_t i_max
= coord
/ defaultDist
,
7251 if (BorderArray
.IsEmpty())
7256 if ( i_max
>= BorderArray
.GetCount())
7257 i_max
= BorderArray
.GetCount() - 1;
7260 if ( coord
>= BorderArray
[i_max
])
7263 i_max
= coord
/ minDist
;
7265 if ( i_max
>= BorderArray
.GetCount())
7266 i_max
= BorderArray
.GetCount() - 1;
7268 if ( coord
>= BorderArray
[i_max
])
7269 return maxOnOverflow
? (int)i_max
: -1;
7270 if ( coord
< BorderArray
[0] )
7273 while ( i_max
- i_min
> 0 )
7275 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7276 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7277 if (coord
>= BorderArray
[ i_max
- 1])
7281 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7282 if (coord
< BorderArray
[median
])
7290 int wxGrid::YToRow( int y
)
7292 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7293 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, FALSE
);
7297 int wxGrid::XToCol( int x
)
7299 return CoordToRowOrCol(x
, m_defaultColWidth
,
7300 WXGRID_MIN_COL_WIDTH
, m_colRights
, FALSE
);
7304 // return the row number that that the y coord is near the edge of, or
7305 // -1 if not near an edge
7307 int wxGrid::YToEdgeOfRow( int y
)
7310 i
= internalYToRow(y
);
7312 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7314 // We know that we are in row i, test whether we are
7315 // close enough to lower or upper border, respectively.
7316 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7318 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7326 // return the col number that that the x coord is near the edge of, or
7327 // -1 if not near an edge
7329 int wxGrid::XToEdgeOfCol( int x
)
7332 i
= internalXToCol(x
);
7334 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7336 // We know that we are in column i, test whether we are
7337 // close enough to right or left border, respectively.
7338 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7340 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7348 wxRect
wxGrid::CellToRect( int row
, int col
)
7350 wxRect
rect( -1, -1, -1, -1 );
7352 if ( row
>= 0 && row
< m_numRows
&&
7353 col
>= 0 && col
< m_numCols
)
7355 int i
, cell_rows
, cell_cols
;
7356 rect
.width
= rect
.height
= 0;
7357 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7358 // if negative then find multicell owner
7359 if (cell_rows
< 0) row
+= cell_rows
;
7360 if (cell_cols
< 0) col
+= cell_cols
;
7361 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7363 rect
.x
= GetColLeft(col
);
7364 rect
.y
= GetRowTop(row
);
7365 for (i
=col
; i
<col
+cell_cols
; i
++)
7366 rect
.width
+= GetColWidth(i
);
7367 for (i
=row
; i
<row
+cell_rows
; i
++)
7368 rect
.height
+= GetRowHeight(i
);
7371 // if grid lines are enabled, then the area of the cell is a bit smaller
7372 if (m_gridLinesEnabled
) {
7380 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7382 // get the cell rectangle in logical coords
7384 wxRect
r( CellToRect( row
, col
) );
7386 // convert to device coords
7388 int left
, top
, right
, bottom
;
7389 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7390 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7392 // check against the client area of the grid window
7395 m_gridWin
->GetClientSize( &cw
, &ch
);
7397 if ( wholeCellVisible
)
7399 // is the cell wholly visible ?
7401 return ( left
>= 0 && right
<= cw
&&
7402 top
>= 0 && bottom
<= ch
);
7406 // is the cell partly visible ?
7408 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7409 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7414 // make the specified cell location visible by doing a minimal amount
7417 void wxGrid::MakeCellVisible( int row
, int col
)
7421 int xpos
= -1, ypos
= -1;
7423 if ( row
>= 0 && row
< m_numRows
&&
7424 col
>= 0 && col
< m_numCols
)
7426 // get the cell rectangle in logical coords
7428 wxRect
r( CellToRect( row
, col
) );
7430 // convert to device coords
7432 int left
, top
, right
, bottom
;
7433 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7434 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7437 m_gridWin
->GetClientSize( &cw
, &ch
);
7443 else if ( bottom
> ch
)
7445 int h
= r
.GetHeight();
7447 for ( i
= row
-1; i
>= 0; i
-- )
7449 int rowHeight
= GetRowHeight(i
);
7450 if ( h
+ rowHeight
> ch
)
7457 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7458 // have rounding errors (this is important, because if we do, we
7459 // might not scroll at all and some cells won't be redrawn)
7461 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7463 ypos
+= GRID_SCROLL_LINE_Y
;
7470 else if ( right
> cw
)
7472 int w
= r
.GetWidth();
7474 for ( i
= col
-1; i
>= 0; i
-- )
7476 int colWidth
= GetColWidth(i
);
7477 if ( w
+ colWidth
> cw
)
7484 // see comment for ypos above
7485 xpos
+= GRID_SCROLL_LINE_X
;
7488 if ( xpos
!= -1 || ypos
!= -1 )
7491 xpos
/= GRID_SCROLL_LINE_X
;
7493 ypos
/= GRID_SCROLL_LINE_Y
;
7494 Scroll( xpos
, ypos
);
7502 // ------ Grid cursor movement functions
7505 bool wxGrid::MoveCursorUp( bool expandSelection
)
7507 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7508 m_currentCellCoords
.GetRow() >= 0 )
7510 if ( expandSelection
)
7512 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7513 m_selectingKeyboard
= m_currentCellCoords
;
7514 if ( m_selectingKeyboard
.GetRow() > 0 )
7516 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7517 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7518 m_selectingKeyboard
.GetCol() );
7519 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7522 else if ( m_currentCellCoords
.GetRow() > 0 )
7525 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7526 m_currentCellCoords
.GetCol() );
7527 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7528 m_currentCellCoords
.GetCol() );
7539 bool wxGrid::MoveCursorDown( bool expandSelection
)
7541 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7542 m_currentCellCoords
.GetRow() < m_numRows
)
7544 if ( expandSelection
)
7546 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7547 m_selectingKeyboard
= m_currentCellCoords
;
7548 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7550 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7551 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7552 m_selectingKeyboard
.GetCol() );
7553 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7556 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7559 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7560 m_currentCellCoords
.GetCol() );
7561 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7562 m_currentCellCoords
.GetCol() );
7573 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7575 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7576 m_currentCellCoords
.GetCol() >= 0 )
7578 if ( expandSelection
)
7580 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7581 m_selectingKeyboard
= m_currentCellCoords
;
7582 if ( m_selectingKeyboard
.GetCol() > 0 )
7584 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7585 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7586 m_selectingKeyboard
.GetCol() );
7587 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7590 else if ( m_currentCellCoords
.GetCol() > 0 )
7593 MakeCellVisible( m_currentCellCoords
.GetRow(),
7594 m_currentCellCoords
.GetCol() - 1 );
7595 SetCurrentCell( m_currentCellCoords
.GetRow(),
7596 m_currentCellCoords
.GetCol() - 1 );
7607 bool wxGrid::MoveCursorRight( bool expandSelection
)
7609 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7610 m_currentCellCoords
.GetCol() < m_numCols
)
7612 if ( expandSelection
)
7614 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7615 m_selectingKeyboard
= m_currentCellCoords
;
7616 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7618 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7619 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7620 m_selectingKeyboard
.GetCol() );
7621 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7624 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7627 MakeCellVisible( m_currentCellCoords
.GetRow(),
7628 m_currentCellCoords
.GetCol() + 1 );
7629 SetCurrentCell( m_currentCellCoords
.GetRow(),
7630 m_currentCellCoords
.GetCol() + 1 );
7641 bool wxGrid::MovePageUp()
7643 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7645 int row
= m_currentCellCoords
.GetRow();
7649 m_gridWin
->GetClientSize( &cw
, &ch
);
7651 int y
= GetRowTop(row
);
7652 int newRow
= YToRow( y
- ch
+ 1 );
7657 else if ( newRow
== row
)
7662 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7663 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7671 bool wxGrid::MovePageDown()
7673 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7675 int row
= m_currentCellCoords
.GetRow();
7676 if ( row
< m_numRows
)
7679 m_gridWin
->GetClientSize( &cw
, &ch
);
7681 int y
= GetRowTop(row
);
7682 int newRow
= YToRow( y
+ ch
);
7685 newRow
= m_numRows
- 1;
7687 else if ( newRow
== row
)
7692 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7693 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7701 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7704 m_currentCellCoords
!= wxGridNoCellCoords
&&
7705 m_currentCellCoords
.GetRow() > 0 )
7707 int row
= m_currentCellCoords
.GetRow();
7708 int col
= m_currentCellCoords
.GetCol();
7710 if ( m_table
->IsEmptyCell(row
, col
) )
7712 // starting in an empty cell: find the next block of
7718 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7721 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7723 // starting at the top of a block: find the next block
7729 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7734 // starting within a block: find the top of the block
7739 if ( m_table
->IsEmptyCell(row
, col
) )
7747 MakeCellVisible( row
, col
);
7748 if ( expandSelection
)
7750 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7751 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7756 SetCurrentCell( row
, col
);
7764 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7767 m_currentCellCoords
!= wxGridNoCellCoords
&&
7768 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7770 int row
= m_currentCellCoords
.GetRow();
7771 int col
= m_currentCellCoords
.GetCol();
7773 if ( m_table
->IsEmptyCell(row
, col
) )
7775 // starting in an empty cell: find the next block of
7778 while ( row
< m_numRows
-1 )
7781 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7784 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7786 // starting at the bottom of a block: find the next block
7789 while ( row
< m_numRows
-1 )
7792 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7797 // starting within a block: find the bottom of the block
7799 while ( row
< m_numRows
-1 )
7802 if ( m_table
->IsEmptyCell(row
, col
) )
7810 MakeCellVisible( row
, col
);
7811 if ( expandSelection
)
7813 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7814 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7819 SetCurrentCell( row
, col
);
7828 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7831 m_currentCellCoords
!= wxGridNoCellCoords
&&
7832 m_currentCellCoords
.GetCol() > 0 )
7834 int row
= m_currentCellCoords
.GetRow();
7835 int col
= m_currentCellCoords
.GetCol();
7837 if ( m_table
->IsEmptyCell(row
, col
) )
7839 // starting in an empty cell: find the next block of
7845 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7848 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7850 // starting at the left of a block: find the next block
7856 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7861 // starting within a block: find the left of the block
7866 if ( m_table
->IsEmptyCell(row
, col
) )
7874 MakeCellVisible( row
, col
);
7875 if ( expandSelection
)
7877 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7878 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7883 SetCurrentCell( row
, col
);
7892 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7895 m_currentCellCoords
!= wxGridNoCellCoords
&&
7896 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7898 int row
= m_currentCellCoords
.GetRow();
7899 int col
= m_currentCellCoords
.GetCol();
7901 if ( m_table
->IsEmptyCell(row
, col
) )
7903 // starting in an empty cell: find the next block of
7906 while ( col
< m_numCols
-1 )
7909 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7912 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7914 // starting at the right of a block: find the next block
7917 while ( col
< m_numCols
-1 )
7920 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7925 // starting within a block: find the right of the block
7927 while ( col
< m_numCols
-1 )
7930 if ( m_table
->IsEmptyCell(row
, col
) )
7938 MakeCellVisible( row
, col
);
7939 if ( expandSelection
)
7941 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7942 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7947 SetCurrentCell( row
, col
);
7959 // ------ Label values and formatting
7962 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7964 *horiz
= m_rowLabelHorizAlign
;
7965 *vert
= m_rowLabelVertAlign
;
7968 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7970 *horiz
= m_colLabelHorizAlign
;
7971 *vert
= m_colLabelVertAlign
;
7974 wxString
wxGrid::GetRowLabelValue( int row
)
7978 return m_table
->GetRowLabelValue( row
);
7988 wxString
wxGrid::GetColLabelValue( int col
)
7992 return m_table
->GetColLabelValue( col
);
8003 void wxGrid::SetRowLabelSize( int width
)
8005 width
= wxMax( width
, 0 );
8006 if ( width
!= m_rowLabelWidth
)
8010 m_rowLabelWin
->Show( FALSE
);
8011 m_cornerLabelWin
->Show( FALSE
);
8013 else if ( m_rowLabelWidth
== 0 )
8015 m_rowLabelWin
->Show( TRUE
);
8016 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8019 m_rowLabelWidth
= width
;
8021 wxScrolledWindow::Refresh( TRUE
);
8026 void wxGrid::SetColLabelSize( int height
)
8028 height
= wxMax( height
, 0 );
8029 if ( height
!= m_colLabelHeight
)
8033 m_colLabelWin
->Show( FALSE
);
8034 m_cornerLabelWin
->Show( FALSE
);
8036 else if ( m_colLabelHeight
== 0 )
8038 m_colLabelWin
->Show( TRUE
);
8039 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8042 m_colLabelHeight
= height
;
8044 wxScrolledWindow::Refresh( TRUE
);
8049 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8051 if ( m_labelBackgroundColour
!= colour
)
8053 m_labelBackgroundColour
= colour
;
8054 m_rowLabelWin
->SetBackgroundColour( colour
);
8055 m_colLabelWin
->SetBackgroundColour( colour
);
8056 m_cornerLabelWin
->SetBackgroundColour( colour
);
8058 if ( !GetBatchCount() )
8060 m_rowLabelWin
->Refresh();
8061 m_colLabelWin
->Refresh();
8062 m_cornerLabelWin
->Refresh();
8067 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8069 if ( m_labelTextColour
!= colour
)
8071 m_labelTextColour
= colour
;
8072 if ( !GetBatchCount() )
8074 m_rowLabelWin
->Refresh();
8075 m_colLabelWin
->Refresh();
8080 void wxGrid::SetLabelFont( const wxFont
& font
)
8083 if ( !GetBatchCount() )
8085 m_rowLabelWin
->Refresh();
8086 m_colLabelWin
->Refresh();
8090 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8092 // allow old (incorrect) defs to be used
8095 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8096 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8097 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8102 case wxTOP
: vert
= wxALIGN_TOP
; break;
8103 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8104 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8107 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8109 m_rowLabelHorizAlign
= horiz
;
8112 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8114 m_rowLabelVertAlign
= vert
;
8117 if ( !GetBatchCount() )
8119 m_rowLabelWin
->Refresh();
8123 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8125 // allow old (incorrect) defs to be used
8128 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8129 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8130 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8135 case wxTOP
: vert
= wxALIGN_TOP
; break;
8136 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8137 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8140 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8142 m_colLabelHorizAlign
= horiz
;
8145 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8147 m_colLabelVertAlign
= vert
;
8150 if ( !GetBatchCount() )
8152 m_colLabelWin
->Refresh();
8156 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8160 m_table
->SetRowLabelValue( row
, s
);
8161 if ( !GetBatchCount() )
8163 wxRect rect
= CellToRect( row
, 0);
8164 if ( rect
.height
> 0 )
8166 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8168 rect
.width
= m_rowLabelWidth
;
8169 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8175 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8179 m_table
->SetColLabelValue( col
, s
);
8180 if ( !GetBatchCount() )
8182 wxRect rect
= CellToRect( 0, col
);
8183 if ( rect
.width
> 0 )
8185 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8187 rect
.height
= m_colLabelHeight
;
8188 m_colLabelWin
->Refresh( TRUE
, &rect
);
8194 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8196 if ( m_gridLineColour
!= colour
)
8198 m_gridLineColour
= colour
;
8200 wxClientDC
dc( m_gridWin
);
8202 DrawAllGridLines( dc
, wxRegion() );
8207 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8209 if ( m_cellHighlightColour
!= colour
)
8211 m_cellHighlightColour
= colour
;
8213 wxClientDC
dc( m_gridWin
);
8215 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8216 DrawCellHighlight(dc
, attr
);
8221 void wxGrid::SetCellHighlightPenWidth(int width
)
8223 if (m_cellHighlightPenWidth
!= width
) {
8224 m_cellHighlightPenWidth
= width
;
8226 // Just redrawing the cell highlight is not enough since that won't
8227 // make any visible change if the the thickness is getting smaller.
8228 int row
= m_currentCellCoords
.GetRow();
8229 int col
= m_currentCellCoords
.GetCol();
8230 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8232 wxRect rect
= CellToRect(row
, col
);
8233 m_gridWin
->Refresh(TRUE
, &rect
);
8237 void wxGrid::SetCellHighlightROPenWidth(int width
)
8239 if (m_cellHighlightROPenWidth
!= width
) {
8240 m_cellHighlightROPenWidth
= width
;
8242 // Just redrawing the cell highlight is not enough since that won't
8243 // make any visible change if the the thickness is getting smaller.
8244 int row
= m_currentCellCoords
.GetRow();
8245 int col
= m_currentCellCoords
.GetCol();
8246 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8248 wxRect rect
= CellToRect(row
, col
);
8249 m_gridWin
->Refresh(TRUE
, &rect
);
8253 void wxGrid::EnableGridLines( bool enable
)
8255 if ( enable
!= m_gridLinesEnabled
)
8257 m_gridLinesEnabled
= enable
;
8259 if ( !GetBatchCount() )
8263 wxClientDC
dc( m_gridWin
);
8265 DrawAllGridLines( dc
, wxRegion() );
8269 m_gridWin
->Refresh();
8276 int wxGrid::GetDefaultRowSize()
8278 return m_defaultRowHeight
;
8281 int wxGrid::GetRowSize( int row
)
8283 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8285 return GetRowHeight(row
);
8288 int wxGrid::GetDefaultColSize()
8290 return m_defaultColWidth
;
8293 int wxGrid::GetColSize( int col
)
8295 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8297 return GetColWidth(col
);
8300 // ============================================================================
8301 // access to the grid attributes: each of them has a default value in the grid
8302 // itself and may be overidden on a per-cell basis
8303 // ============================================================================
8305 // ----------------------------------------------------------------------------
8306 // setting default attributes
8307 // ----------------------------------------------------------------------------
8309 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8311 m_defaultCellAttr
->SetBackgroundColour(col
);
8313 m_gridWin
->SetBackgroundColour(col
);
8317 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8319 m_defaultCellAttr
->SetTextColour(col
);
8322 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8324 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8327 void wxGrid::SetDefaultCellOverflow( bool allow
)
8329 m_defaultCellAttr
->SetOverflow(allow
);
8332 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8334 m_defaultCellAttr
->SetFont(font
);
8337 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8339 m_defaultCellAttr
->SetRenderer(renderer
);
8342 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8344 m_defaultCellAttr
->SetEditor(editor
);
8347 // ----------------------------------------------------------------------------
8348 // access to the default attrbiutes
8349 // ----------------------------------------------------------------------------
8351 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8353 return m_defaultCellAttr
->GetBackgroundColour();
8356 wxColour
wxGrid::GetDefaultCellTextColour()
8358 return m_defaultCellAttr
->GetTextColour();
8361 wxFont
wxGrid::GetDefaultCellFont()
8363 return m_defaultCellAttr
->GetFont();
8366 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8368 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8371 bool wxGrid::GetDefaultCellOverflow()
8373 return m_defaultCellAttr
->GetOverflow();
8376 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8378 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8381 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8383 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8386 // ----------------------------------------------------------------------------
8387 // access to cell attributes
8388 // ----------------------------------------------------------------------------
8390 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8392 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8393 wxColour colour
= attr
->GetBackgroundColour();
8398 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8400 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8401 wxColour colour
= attr
->GetTextColour();
8406 wxFont
wxGrid::GetCellFont( int row
, int col
)
8408 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8409 wxFont font
= attr
->GetFont();
8414 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8416 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8417 attr
->GetAlignment(horiz
, vert
);
8421 bool wxGrid::GetCellOverflow( int row
, int col
)
8423 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8424 bool allow
= attr
->GetOverflow();
8429 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8431 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8432 attr
->GetSize( num_rows
, num_cols
);
8436 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8438 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8439 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8445 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8447 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8448 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8454 bool wxGrid::IsReadOnly(int row
, int col
) const
8456 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8457 bool isReadOnly
= attr
->IsReadOnly();
8462 // ----------------------------------------------------------------------------
8463 // attribute support: cache, automatic provider creation, ...
8464 // ----------------------------------------------------------------------------
8466 bool wxGrid::CanHaveAttributes()
8473 return m_table
->CanHaveAttributes();
8476 void wxGrid::ClearAttrCache()
8478 if ( m_attrCache
.row
!= -1 )
8480 wxSafeDecRef(m_attrCache
.attr
);
8481 m_attrCache
.attr
= NULL
;
8482 m_attrCache
.row
= -1;
8486 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8488 wxGrid
*self
= (wxGrid
*)this; // const_cast
8490 self
->ClearAttrCache();
8491 self
->m_attrCache
.row
= row
;
8492 self
->m_attrCache
.col
= col
;
8493 self
->m_attrCache
.attr
= attr
;
8497 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8499 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8501 *attr
= m_attrCache
.attr
;
8502 wxSafeIncRef(m_attrCache
.attr
);
8504 #ifdef DEBUG_ATTR_CACHE
8505 gs_nAttrCacheHits
++;
8512 #ifdef DEBUG_ATTR_CACHE
8513 gs_nAttrCacheMisses
++;
8519 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8521 wxGridCellAttr
*attr
= NULL
;
8522 // Additional test to avoid looking at the cache e.g. for
8523 // wxNoCellCoords, as this will confuse memory management.
8526 if ( !LookupAttr(row
, col
, &attr
) )
8528 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8529 : (wxGridCellAttr
*)NULL
;
8530 CacheAttr(row
, col
, attr
);
8535 attr
->SetDefAttr(m_defaultCellAttr
);
8539 attr
= m_defaultCellAttr
;
8546 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8548 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8550 wxCHECK_MSG( m_table
, attr
,
8551 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8553 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8556 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8558 // artificially inc the ref count to match DecRef() in caller
8560 m_table
->SetAttr(attr
, row
, col
);
8566 // ----------------------------------------------------------------------------
8567 // setting column attributes (wrappers around SetColAttr)
8568 // ----------------------------------------------------------------------------
8570 void wxGrid::SetColFormatBool(int col
)
8572 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8575 void wxGrid::SetColFormatNumber(int col
)
8577 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8580 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8582 wxString typeName
= wxGRID_VALUE_FLOAT
;
8583 if ( (width
!= -1) || (precision
!= -1) )
8585 typeName
<< _T(':') << width
<< _T(',') << precision
;
8588 SetColFormatCustom(col
, typeName
);
8591 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8593 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8595 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8597 attr
= new wxGridCellAttr
;
8598 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8599 attr
->SetRenderer(renderer
);
8601 SetColAttr(col
, attr
);
8605 // ----------------------------------------------------------------------------
8606 // setting cell attributes: this is forwarded to the table
8607 // ----------------------------------------------------------------------------
8609 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8611 if ( CanHaveAttributes() )
8613 m_table
->SetAttr(attr
, row
, col
);
8622 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8624 if ( CanHaveAttributes() )
8626 m_table
->SetRowAttr(attr
, row
);
8635 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8637 if ( CanHaveAttributes() )
8639 m_table
->SetColAttr(attr
, col
);
8648 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8650 if ( CanHaveAttributes() )
8652 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8653 attr
->SetBackgroundColour(colour
);
8658 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8660 if ( CanHaveAttributes() )
8662 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8663 attr
->SetTextColour(colour
);
8668 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8670 if ( CanHaveAttributes() )
8672 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8673 attr
->SetFont(font
);
8678 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8680 if ( CanHaveAttributes() )
8682 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8683 attr
->SetAlignment(horiz
, vert
);
8688 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8690 if ( CanHaveAttributes() )
8692 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8693 attr
->SetOverflow(allow
);
8698 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8700 if ( CanHaveAttributes() )
8702 int cell_rows
, cell_cols
;
8704 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8705 attr
->GetSize(&cell_rows
, &cell_cols
);
8706 attr
->SetSize(num_rows
, num_cols
);
8709 // Cannot set the size of a cell to 0 or negative values
8710 // While it is perfectly legal to do that, this function cannot
8711 // handle all the possibilies, do it by hand by getting the CellAttr.
8712 // You can only set the size of a cell to 1,1 or greater with this fn
8713 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8714 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8715 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8716 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8718 // if this was already a multicell then "turn off" the other cells first
8719 if ((cell_rows
> 1) || (cell_rows
> 1))
8722 for (j
=row
; j
<row
+cell_rows
; j
++)
8724 for (i
=col
; i
<col
+cell_cols
; i
++)
8726 if ((i
!= col
) || (j
!= row
))
8728 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8729 attr_stub
->SetSize( 1, 1 );
8730 attr_stub
->DecRef();
8736 // mark the cells that will be covered by this cell to
8737 // negative or zero values to point back at this cell
8738 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8741 for (j
=row
; j
<row
+num_rows
; j
++)
8743 for (i
=col
; i
<col
+num_cols
; i
++)
8745 if ((i
!= col
) || (j
!= row
))
8747 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8748 attr_stub
->SetSize( row
-j
, col
-i
);
8749 attr_stub
->DecRef();
8757 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8759 if ( CanHaveAttributes() )
8761 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8762 attr
->SetRenderer(renderer
);
8767 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8769 if ( CanHaveAttributes() )
8771 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8772 attr
->SetEditor(editor
);
8777 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8779 if ( CanHaveAttributes() )
8781 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8782 attr
->SetReadOnly(isReadOnly
);
8787 // ----------------------------------------------------------------------------
8788 // Data type registration
8789 // ----------------------------------------------------------------------------
8791 void wxGrid::RegisterDataType(const wxString
& typeName
,
8792 wxGridCellRenderer
* renderer
,
8793 wxGridCellEditor
* editor
)
8795 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8799 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8801 wxString typeName
= m_table
->GetTypeName(row
, col
);
8802 return GetDefaultEditorForType(typeName
);
8805 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8807 wxString typeName
= m_table
->GetTypeName(row
, col
);
8808 return GetDefaultRendererForType(typeName
);
8812 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8814 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8815 if ( index
== wxNOT_FOUND
)
8817 wxFAIL_MSG(wxT("Unknown data type name"));
8822 return m_typeRegistry
->GetEditor(index
);
8826 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8828 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8829 if ( index
== wxNOT_FOUND
)
8831 wxFAIL_MSG(wxT("Unknown data type name"));
8836 return m_typeRegistry
->GetRenderer(index
);
8840 // ----------------------------------------------------------------------------
8842 // ----------------------------------------------------------------------------
8844 void wxGrid::EnableDragRowSize( bool enable
)
8846 m_canDragRowSize
= enable
;
8850 void wxGrid::EnableDragColSize( bool enable
)
8852 m_canDragColSize
= enable
;
8855 void wxGrid::EnableDragGridSize( bool enable
)
8857 m_canDragGridSize
= enable
;
8861 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8863 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8865 if ( resizeExistingRows
)
8867 // since we are resizing all rows to the default row size,
8868 // we can simply clear the row heights and row bottoms
8869 // arrays (which also allows us to take advantage of
8870 // some speed optimisations)
8871 m_rowHeights
.Empty();
8872 m_rowBottoms
.Empty();
8873 if ( !GetBatchCount() )
8878 void wxGrid::SetRowSize( int row
, int height
)
8880 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8882 if ( m_rowHeights
.IsEmpty() )
8884 // need to really create the array
8888 int h
= wxMax( 0, height
);
8889 int diff
= h
- m_rowHeights
[row
];
8891 m_rowHeights
[row
] = h
;
8893 for ( i
= row
; i
< m_numRows
; i
++ )
8895 m_rowBottoms
[i
] += diff
;
8897 if ( !GetBatchCount() )
8901 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8903 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8905 if ( resizeExistingCols
)
8907 // since we are resizing all columns to the default column size,
8908 // we can simply clear the col widths and col rights
8909 // arrays (which also allows us to take advantage of
8910 // some speed optimisations)
8911 m_colWidths
.Empty();
8912 m_colRights
.Empty();
8913 if ( !GetBatchCount() )
8918 void wxGrid::SetColSize( int col
, int width
)
8920 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8922 // should we check that it's bigger than GetColMinimalWidth(col) here?
8924 if ( m_colWidths
.IsEmpty() )
8926 // need to really create the array
8930 int w
= wxMax( 0, width
);
8931 int diff
= w
- m_colWidths
[col
];
8932 m_colWidths
[col
] = w
;
8935 for ( i
= col
; i
< m_numCols
; i
++ )
8937 m_colRights
[i
] += diff
;
8939 if ( !GetBatchCount() )
8944 void wxGrid::SetColMinimalWidth( int col
, int width
)
8946 m_colMinWidths
.Put(col
, width
);
8949 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8951 m_rowMinHeights
.Put(row
, width
);
8954 int wxGrid::GetColMinimalWidth(int col
) const
8956 long value
= m_colMinWidths
.Get(col
);
8957 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8960 int wxGrid::GetRowMinimalHeight(int row
) const
8962 long value
= m_rowMinHeights
.Get(row
);
8963 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8966 // ----------------------------------------------------------------------------
8968 // ----------------------------------------------------------------------------
8970 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8972 wxClientDC
dc(m_gridWin
);
8974 // init both of them to avoid compiler warnings, even if weo nly need one
8982 wxCoord extent
, extentMax
= 0;
8983 int max
= column
? m_numRows
: m_numCols
;
8984 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8991 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8992 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8995 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8996 extent
= column
? size
.x
: size
.y
;
8997 if ( extent
> extentMax
)
9008 // now also compare with the column label extent
9010 dc
.SetFont( GetLabelFont() );
9013 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9015 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9017 extent
= column
? w
: h
;
9018 if ( extent
> extentMax
)
9025 // empty column - give default extent (notice that if extentMax is less
9026 // than default extent but != 0, it's ok)
9027 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9033 // leave some space around text
9044 SetColSize(col
, extentMax
);
9045 if ( !GetBatchCount() )
9048 m_gridWin
->GetClientSize( &cw
, &ch
);
9049 wxRect
rect ( CellToRect( 0, col
) );
9051 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9052 rect
.width
= cw
- rect
.x
;
9053 rect
.height
= m_colLabelHeight
;
9054 m_colLabelWin
->Refresh( TRUE
, &rect
);
9059 SetRowSize(row
, extentMax
);
9060 if ( !GetBatchCount() )
9063 m_gridWin
->GetClientSize( &cw
, &ch
);
9064 wxRect
rect ( CellToRect( row
, 0 ) );
9066 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9067 rect
.width
= m_rowLabelWidth
;
9068 rect
.height
= ch
- rect
.y
;
9069 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9075 SetColMinimalWidth(col
, extentMax
);
9077 SetRowMinimalHeight(row
, extentMax
);
9081 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9083 int width
= m_rowLabelWidth
;
9088 for ( int col
= 0; col
< m_numCols
; col
++ )
9092 AutoSizeColumn(col
, setAsMin
);
9095 width
+= GetColWidth(col
);
9104 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9106 int height
= m_colLabelHeight
;
9111 for ( int row
= 0; row
< m_numRows
; row
++ )
9115 AutoSizeRow(row
, setAsMin
);
9118 height
+= GetRowHeight(row
);
9127 void wxGrid::AutoSize()
9131 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9133 // round up the size to a multiple of scroll step - this ensures that we
9134 // won't get the scrollbars if we're sized exactly to this width
9135 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
9136 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
9138 // distribute the extra space between teh columns/rows to avoid having
9139 // extra white space
9140 wxCoord diff
= sizeFit
.x
- size
.x
;
9143 // try to resize the columns uniformly
9144 wxCoord diffPerCol
= diff
/ m_numCols
;
9147 for ( int col
= 0; col
< m_numCols
; col
++ )
9149 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9153 // add remaining amount to the last columns
9154 diff
-= diffPerCol
* m_numCols
;
9157 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9159 SetColSize(col
, GetColWidth(col
) + 1);
9165 diff
= sizeFit
.y
- size
.y
;
9168 // try to resize the columns uniformly
9169 wxCoord diffPerRow
= diff
/ m_numRows
;
9172 for ( int row
= 0; row
< m_numRows
; row
++ )
9174 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9178 // add remaining amount to the last rows
9179 diff
-= diffPerRow
* m_numRows
;
9182 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9184 SetRowSize(row
, GetRowHeight(row
) + 1);
9191 SetClientSize(sizeFit
);
9194 wxSize
wxGrid::DoGetBestSize() const
9196 // don't set sizes, only calculate them
9197 wxGrid
*self
= (wxGrid
*)this; // const_cast
9200 width
= self
->SetOrCalcColumnSizes(TRUE
);
9201 height
= self
->SetOrCalcRowSizes(TRUE
);
9203 int maxwidth
, maxheight
;
9204 wxDisplaySize( & maxwidth
, & maxheight
);
9206 if ( width
> maxwidth
) width
= maxwidth
;
9207 if ( height
> maxheight
) height
= maxheight
;
9209 return wxSize( width
, height
);
9218 wxPen
& wxGrid::GetDividerPen() const
9223 // ----------------------------------------------------------------------------
9224 // cell value accessor functions
9225 // ----------------------------------------------------------------------------
9227 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9231 m_table
->SetValue( row
, col
, s
);
9232 if ( !GetBatchCount() )
9235 wxRect
rect( CellToRect( row
, col
) );
9237 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9238 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9239 m_gridWin
->Refresh( FALSE
, &rect
);
9242 if ( m_currentCellCoords
.GetRow() == row
&&
9243 m_currentCellCoords
.GetCol() == col
&&
9244 IsCellEditControlShown())
9245 // Note: If we are using IsCellEditControlEnabled,
9246 // this interacts badly with calling SetCellValue from
9247 // an EVT_GRID_CELL_CHANGE handler.
9249 HideCellEditControl();
9250 ShowCellEditControl(); // will reread data from table
9257 // ------ Block, row and col selection
9260 void wxGrid::SelectRow( int row
, bool addToSelected
)
9262 if ( IsSelection() && !addToSelected
)
9266 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9270 void wxGrid::SelectCol( int col
, bool addToSelected
)
9272 if ( IsSelection() && !addToSelected
)
9276 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9280 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9281 bool addToSelected
)
9283 if ( IsSelection() && !addToSelected
)
9287 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9288 FALSE
, addToSelected
);
9292 void wxGrid::SelectAll()
9294 if ( m_numRows
> 0 && m_numCols
> 0 )
9297 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9302 // ------ Cell, row and col deselection
9305 void wxGrid::DeselectRow( int row
)
9310 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9312 if ( m_selection
->IsInSelection(row
, 0 ) )
9313 m_selection
->ToggleCellSelection( row
, 0);
9317 int nCols
= GetNumberCols();
9318 for ( int i
= 0; i
< nCols
; i
++ )
9320 if ( m_selection
->IsInSelection(row
, i
) )
9321 m_selection
->ToggleCellSelection( row
, i
);
9326 void wxGrid::DeselectCol( int col
)
9331 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9333 if ( m_selection
->IsInSelection(0, col
) )
9334 m_selection
->ToggleCellSelection( 0, col
);
9338 int nRows
= GetNumberRows();
9339 for ( int i
= 0; i
< nRows
; i
++ )
9341 if ( m_selection
->IsInSelection(i
, col
) )
9342 m_selection
->ToggleCellSelection(i
, col
);
9347 void wxGrid::DeselectCell( int row
, int col
)
9349 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9350 m_selection
->ToggleCellSelection(row
, col
);
9353 bool wxGrid::IsSelection()
9355 return ( m_selection
&& (m_selection
->IsSelection() ||
9356 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9357 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9360 bool wxGrid::IsInSelection( int row
, int col
) const
9362 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9363 ( row
>= m_selectingTopLeft
.GetRow() &&
9364 col
>= m_selectingTopLeft
.GetCol() &&
9365 row
<= m_selectingBottomRight
.GetRow() &&
9366 col
<= m_selectingBottomRight
.GetCol() )) );
9369 void wxGrid::ClearSelection()
9371 m_selectingTopLeft
= wxGridNoCellCoords
;
9372 m_selectingBottomRight
= wxGridNoCellCoords
;
9374 m_selection
->ClearSelection();
9378 // This function returns the rectangle that encloses the given block
9379 // in device coords clipped to the client size of the grid window.
9381 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9382 const wxGridCellCoords
&bottomRight
)
9384 wxRect
rect( wxGridNoCellRect
);
9387 cellRect
= CellToRect( topLeft
);
9388 if ( cellRect
!= wxGridNoCellRect
)
9394 rect
= wxRect( 0, 0, 0, 0 );
9397 cellRect
= CellToRect( bottomRight
);
9398 if ( cellRect
!= wxGridNoCellRect
)
9404 return wxGridNoCellRect
;
9408 int left
= rect
.GetLeft();
9409 int top
= rect
.GetTop();
9410 int right
= rect
.GetRight();
9411 int bottom
= rect
.GetBottom();
9413 int leftCol
= topLeft
.GetCol();
9414 int topRow
= topLeft
.GetRow();
9415 int rightCol
= bottomRight
.GetCol();
9416 int bottomRow
= bottomRight
.GetRow();
9418 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9420 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9422 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9424 cellRect
= CellToRect( j
, i
);
9426 if (cellRect
.x
< left
)
9428 if (cellRect
.y
< top
)
9430 if (cellRect
.x
+ cellRect
.width
> right
)
9431 right
= cellRect
.x
+ cellRect
.width
;
9432 if (cellRect
.y
+ cellRect
.height
> bottom
)
9433 bottom
= cellRect
.y
+ cellRect
.height
;
9438 // convert to scrolled coords
9440 CalcScrolledPosition( left
, top
, &left
, &top
);
9441 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9444 m_gridWin
->GetClientSize( &cw
, &ch
);
9446 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9447 return wxRect( 0, 0, 0, 0);
9449 rect
.SetLeft( wxMax(0, left
) );
9450 rect
.SetTop( wxMax(0, top
) );
9451 rect
.SetRight( wxMin(cw
, right
) );
9452 rect
.SetBottom( wxMin(ch
, bottom
) );
9460 // ------ Grid event classes
9463 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9465 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9466 int row
, int col
, int x
, int y
, bool sel
,
9467 bool control
, bool shift
, bool alt
, bool meta
)
9468 : wxNotifyEvent( type
, id
)
9475 m_control
= control
;
9480 SetEventObject(obj
);
9484 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9486 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9487 int rowOrCol
, int x
, int y
,
9488 bool control
, bool shift
, bool alt
, bool meta
)
9489 : wxNotifyEvent( type
, id
)
9491 m_rowOrCol
= rowOrCol
;
9494 m_control
= control
;
9499 SetEventObject(obj
);
9503 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9505 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9506 const wxGridCellCoords
& topLeft
,
9507 const wxGridCellCoords
& bottomRight
,
9508 bool sel
, bool control
,
9509 bool shift
, bool alt
, bool meta
)
9510 : wxNotifyEvent( type
, id
)
9512 m_topLeft
= topLeft
;
9513 m_bottomRight
= bottomRight
;
9515 m_control
= control
;
9520 SetEventObject(obj
);
9524 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9526 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9527 wxObject
* obj
, int row
,
9528 int col
, wxControl
* ctrl
)
9529 : wxCommandEvent(type
, id
)
9531 SetEventObject(obj
);
9538 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9540 #endif // wxUSE_GRID