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.
4001 // if we were using the default widths/heights so far, we must change them
4003 if ( m_colWidths
.IsEmpty() )
4008 if ( m_rowHeights
.IsEmpty() )
4014 switch ( msg
.GetId() )
4016 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4018 size_t pos
= msg
.GetCommandInt();
4019 int numRows
= msg
.GetCommandInt2();
4021 m_numRows
+= numRows
;
4023 if ( !m_rowHeights
.IsEmpty() )
4025 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4026 m_rowBottoms
.Insert( 0, pos
, numRows
);
4029 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4031 for ( i
= pos
; i
< m_numRows
; i
++ )
4033 bottom
+= m_rowHeights
[i
];
4034 m_rowBottoms
[i
] = bottom
;
4037 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4039 // if we have just inserted cols into an empty grid the current
4040 // cell will be undefined...
4042 SetCurrentCell( 0, 0 );
4046 m_selection
->UpdateRows( pos
, numRows
);
4047 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4049 attrProvider
->UpdateAttrRows( pos
, numRows
);
4051 if ( !GetBatchCount() )
4054 m_rowLabelWin
->Refresh();
4060 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4062 int numRows
= msg
.GetCommandInt();
4063 int oldNumRows
= m_numRows
;
4064 m_numRows
+= numRows
;
4066 if ( !m_rowHeights
.IsEmpty() )
4068 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4069 m_rowBottoms
.Add( 0, numRows
);
4072 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4074 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4076 bottom
+= m_rowHeights
[i
];
4077 m_rowBottoms
[i
] = bottom
;
4080 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4082 // if we have just inserted cols into an empty grid the current
4083 // cell will be undefined...
4085 SetCurrentCell( 0, 0 );
4087 if ( !GetBatchCount() )
4090 m_rowLabelWin
->Refresh();
4096 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4098 size_t pos
= msg
.GetCommandInt();
4099 int numRows
= msg
.GetCommandInt2();
4100 m_numRows
-= numRows
;
4102 if ( !m_rowHeights
.IsEmpty() )
4104 m_rowHeights
.RemoveAt( pos
, numRows
);
4105 m_rowBottoms
.RemoveAt( pos
, numRows
);
4108 for ( i
= 0; i
< m_numRows
; i
++ )
4110 h
+= m_rowHeights
[i
];
4111 m_rowBottoms
[i
] = h
;
4116 m_currentCellCoords
= wxGridNoCellCoords
;
4120 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4121 m_currentCellCoords
.Set( 0, 0 );
4125 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4126 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4128 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4129 // ifdef'd out following patch from Paul Gammans
4131 // No need to touch column attributes, unless we
4132 // removed _all_ rows, in this case, we remove
4133 // all column attributes.
4134 // I hate to do this here, but the
4135 // needed data is not available inside UpdateAttrRows.
4136 if ( !GetNumberRows() )
4137 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4140 if ( !GetBatchCount() )
4143 m_rowLabelWin
->Refresh();
4149 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4151 size_t pos
= msg
.GetCommandInt();
4152 int numCols
= msg
.GetCommandInt2();
4153 m_numCols
+= numCols
;
4155 if ( !m_colWidths
.IsEmpty() )
4157 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4158 m_colRights
.Insert( 0, pos
, numCols
);
4161 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4163 for ( i
= pos
; i
< m_numCols
; i
++ )
4165 right
+= m_colWidths
[i
];
4166 m_colRights
[i
] = right
;
4169 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4171 // if we have just inserted cols into an empty grid the current
4172 // cell will be undefined...
4174 SetCurrentCell( 0, 0 );
4178 m_selection
->UpdateCols( pos
, numCols
);
4179 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4181 attrProvider
->UpdateAttrCols( pos
, numCols
);
4182 if ( !GetBatchCount() )
4185 m_colLabelWin
->Refresh();
4192 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4194 int numCols
= msg
.GetCommandInt();
4195 int oldNumCols
= m_numCols
;
4196 m_numCols
+= numCols
;
4197 if ( !m_colWidths
.IsEmpty() )
4199 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4200 m_colRights
.Add( 0, numCols
);
4203 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4205 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4207 right
+= m_colWidths
[i
];
4208 m_colRights
[i
] = right
;
4211 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4213 // if we have just inserted cols into an empty grid the current
4214 // cell will be undefined...
4216 SetCurrentCell( 0, 0 );
4218 if ( !GetBatchCount() )
4221 m_colLabelWin
->Refresh();
4227 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4229 size_t pos
= msg
.GetCommandInt();
4230 int numCols
= msg
.GetCommandInt2();
4231 m_numCols
-= numCols
;
4233 if ( !m_colWidths
.IsEmpty() )
4235 m_colWidths
.RemoveAt( pos
, numCols
);
4236 m_colRights
.RemoveAt( pos
, numCols
);
4239 for ( i
= 0; i
< m_numCols
; i
++ )
4241 w
+= m_colWidths
[i
];
4247 m_currentCellCoords
= wxGridNoCellCoords
;
4251 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4252 m_currentCellCoords
.Set( 0, 0 );
4256 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4257 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4259 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4260 // ifdef'd out following patch from Paul Gammans
4262 // No need to touch row attributes, unless we
4263 // removed _all_ columns, in this case, we remove
4264 // all row attributes.
4265 // I hate to do this here, but the
4266 // needed data is not available inside UpdateAttrCols.
4267 if ( !GetNumberCols() )
4268 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4271 if ( !GetBatchCount() )
4274 m_colLabelWin
->Refresh();
4281 if (result
&& !GetBatchCount() )
4282 m_gridWin
->Refresh();
4287 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4289 wxRegionIterator
iter( reg
);
4292 wxArrayInt rowlabels
;
4299 // TODO: remove this when we can...
4300 // There is a bug in wxMotif that gives garbage update
4301 // rectangles if you jump-scroll a long way by clicking the
4302 // scrollbar with middle button. This is a work-around
4304 #if defined(__WXMOTIF__)
4306 m_gridWin
->GetClientSize( &cw
, &ch
);
4307 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4308 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4311 // logical bounds of update region
4314 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4315 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4317 // find the row labels within these bounds
4320 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4322 if ( GetRowBottom(row
) < top
)
4325 if ( GetRowTop(row
) > bottom
)
4328 rowlabels
.Add( row
);
4338 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4340 wxRegionIterator
iter( reg
);
4343 wxArrayInt colLabels
;
4350 // TODO: remove this when we can...
4351 // There is a bug in wxMotif that gives garbage update
4352 // rectangles if you jump-scroll a long way by clicking the
4353 // scrollbar with middle button. This is a work-around
4355 #if defined(__WXMOTIF__)
4357 m_gridWin
->GetClientSize( &cw
, &ch
);
4358 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4359 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4362 // logical bounds of update region
4365 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4366 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4368 // find the cells within these bounds
4371 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4373 if ( GetColRight(col
) < left
)
4376 if ( GetColLeft(col
) > right
)
4379 colLabels
.Add( col
);
4388 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4390 wxRegionIterator
iter( reg
);
4393 wxGridCellCoordsArray cellsExposed
;
4395 int left
, top
, right
, bottom
;
4400 // TODO: remove this when we can...
4401 // There is a bug in wxMotif that gives garbage update
4402 // rectangles if you jump-scroll a long way by clicking the
4403 // scrollbar with middle button. This is a work-around
4405 #if defined(__WXMOTIF__)
4407 m_gridWin
->GetClientSize( &cw
, &ch
);
4408 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4409 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4410 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4411 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4414 // logical bounds of update region
4416 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4417 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4419 // find the cells within these bounds
4422 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4424 if ( GetRowBottom(row
) <= top
)
4427 if ( GetRowTop(row
) > bottom
)
4430 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4432 if ( GetColRight(col
) <= left
)
4435 if ( GetColLeft(col
) > right
)
4438 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4445 return cellsExposed
;
4449 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4452 wxPoint
pos( event
.GetPosition() );
4453 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4455 if ( event
.Dragging() )
4459 m_isDragging
= TRUE
;
4460 m_rowLabelWin
->CaptureMouse();
4463 if ( event
.LeftIsDown() )
4465 switch( m_cursorMode
)
4467 case WXGRID_CURSOR_RESIZE_ROW
:
4469 int cw
, ch
, left
, dummy
;
4470 m_gridWin
->GetClientSize( &cw
, &ch
);
4471 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4473 wxClientDC
dc( m_gridWin
);
4476 GetRowTop(m_dragRowOrCol
) +
4477 GetRowMinimalHeight(m_dragRowOrCol
) );
4478 dc
.SetLogicalFunction(wxINVERT
);
4479 if ( m_dragLastPos
>= 0 )
4481 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4483 dc
.DrawLine( left
, y
, left
+cw
, y
);
4488 case WXGRID_CURSOR_SELECT_ROW
:
4489 if ( (row
= YToRow( y
)) >= 0 )
4493 m_selection
->SelectRow( row
,
4494 event
.ControlDown(),
4501 // default label to suppress warnings about "enumeration value
4502 // 'xxx' not handled in switch
4510 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4515 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4516 m_isDragging
= FALSE
;
4519 // ------------ Entering or leaving the window
4521 if ( event
.Entering() || event
.Leaving() )
4523 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4527 // ------------ Left button pressed
4529 else if ( event
.LeftDown() )
4531 // don't send a label click event for a hit on the
4532 // edge of the row label - this is probably the user
4533 // wanting to resize the row
4535 if ( YToEdgeOfRow(y
) < 0 )
4539 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4541 if ( !event
.ShiftDown() && !event
.ControlDown() )
4543 else if ( m_selection
)
4545 if ( event
.ShiftDown() )
4547 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4550 GetNumberCols() - 1,
4551 event
.ControlDown(),
4558 m_selection
->SelectRow( row
,
4559 event
.ControlDown(),
4566 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4571 // starting to drag-resize a row
4573 if ( CanDragRowSize() )
4574 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4579 // ------------ Left double click
4581 else if (event
.LeftDClick() )
4583 if ( YToEdgeOfRow(y
) < 0 )
4586 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4591 // ------------ Left button released
4593 else if ( event
.LeftUp() )
4595 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4597 DoEndDragResizeRow();
4599 // Note: we are ending the event *after* doing
4600 // default processing in this case
4602 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4605 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4610 // ------------ Right button down
4612 else if ( event
.RightDown() )
4615 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4617 // no default action at the moment
4622 // ------------ Right double click
4624 else if ( event
.RightDClick() )
4627 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4629 // no default action at the moment
4634 // ------------ No buttons down and mouse moving
4636 else if ( event
.Moving() )
4638 m_dragRowOrCol
= YToEdgeOfRow( y
);
4639 if ( m_dragRowOrCol
>= 0 )
4641 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4643 // don't capture the mouse yet
4644 if ( CanDragRowSize() )
4645 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4648 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4650 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4656 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4659 wxPoint
pos( event
.GetPosition() );
4660 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4662 if ( event
.Dragging() )
4666 m_isDragging
= TRUE
;
4667 m_colLabelWin
->CaptureMouse();
4670 if ( event
.LeftIsDown() )
4672 switch( m_cursorMode
)
4674 case WXGRID_CURSOR_RESIZE_COL
:
4676 int cw
, ch
, dummy
, top
;
4677 m_gridWin
->GetClientSize( &cw
, &ch
);
4678 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4680 wxClientDC
dc( m_gridWin
);
4683 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4684 GetColMinimalWidth(m_dragRowOrCol
));
4685 dc
.SetLogicalFunction(wxINVERT
);
4686 if ( m_dragLastPos
>= 0 )
4688 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4690 dc
.DrawLine( x
, top
, x
, top
+ch
);
4695 case WXGRID_CURSOR_SELECT_COL
:
4696 if ( (col
= XToCol( x
)) >= 0 )
4700 m_selection
->SelectCol( col
,
4701 event
.ControlDown(),
4708 // default label to suppress warnings about "enumeration value
4709 // 'xxx' not handled in switch
4717 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4722 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4723 m_isDragging
= FALSE
;
4726 // ------------ Entering or leaving the window
4728 if ( event
.Entering() || event
.Leaving() )
4730 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4734 // ------------ Left button pressed
4736 else if ( event
.LeftDown() )
4738 // don't send a label click event for a hit on the
4739 // edge of the col label - this is probably the user
4740 // wanting to resize the col
4742 if ( XToEdgeOfCol(x
) < 0 )
4746 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4748 if ( !event
.ShiftDown() && !event
.ControlDown() )
4752 if ( event
.ShiftDown() )
4754 m_selection
->SelectBlock( 0,
4755 m_currentCellCoords
.GetCol(),
4756 GetNumberRows() - 1, col
,
4757 event
.ControlDown(),
4764 m_selection
->SelectCol( col
,
4765 event
.ControlDown(),
4772 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4777 // starting to drag-resize a col
4779 if ( CanDragColSize() )
4780 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4785 // ------------ Left double click
4787 if ( event
.LeftDClick() )
4789 if ( XToEdgeOfCol(x
) < 0 )
4792 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4797 // ------------ Left button released
4799 else if ( event
.LeftUp() )
4801 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4803 DoEndDragResizeCol();
4805 // Note: we are ending the event *after* doing
4806 // default processing in this case
4808 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4811 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4816 // ------------ Right button down
4818 else if ( event
.RightDown() )
4821 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4823 // no default action at the moment
4828 // ------------ Right double click
4830 else if ( event
.RightDClick() )
4833 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4835 // no default action at the moment
4840 // ------------ No buttons down and mouse moving
4842 else if ( event
.Moving() )
4844 m_dragRowOrCol
= XToEdgeOfCol( x
);
4845 if ( m_dragRowOrCol
>= 0 )
4847 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4849 // don't capture the cursor yet
4850 if ( CanDragColSize() )
4851 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4854 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4856 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4862 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4864 if ( event
.LeftDown() )
4866 // indicate corner label by having both row and
4869 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4875 else if ( event
.LeftDClick() )
4877 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4880 else if ( event
.RightDown() )
4882 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4884 // no default action at the moment
4888 else if ( event
.RightDClick() )
4890 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4892 // no default action at the moment
4897 void wxGrid::ChangeCursorMode(CursorMode mode
,
4902 static const wxChar
*cursorModes
[] =
4911 wxLogTrace(_T("grid"),
4912 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4913 win
== m_colLabelWin
? _T("colLabelWin")
4914 : win
? _T("rowLabelWin")
4916 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4917 #endif // __WXDEBUG__
4919 if ( mode
== m_cursorMode
&&
4920 win
== m_winCapture
&&
4921 captureMouse
== (m_winCapture
!= NULL
))
4926 // by default use the grid itself
4932 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4933 m_winCapture
= (wxWindow
*)NULL
;
4936 m_cursorMode
= mode
;
4938 switch ( m_cursorMode
)
4940 case WXGRID_CURSOR_RESIZE_ROW
:
4941 win
->SetCursor( m_rowResizeCursor
);
4944 case WXGRID_CURSOR_RESIZE_COL
:
4945 win
->SetCursor( m_colResizeCursor
);
4949 win
->SetCursor( *wxSTANDARD_CURSOR
);
4952 // we need to capture mouse when resizing
4953 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4954 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4956 if ( captureMouse
&& resize
)
4958 win
->CaptureMouse();
4963 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4966 wxPoint
pos( event
.GetPosition() );
4967 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4969 wxGridCellCoords coords
;
4970 XYToCell( x
, y
, coords
);
4972 int cell_rows
, cell_cols
;
4973 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
4974 if ((cell_rows
< 0) || (cell_cols
< 0))
4976 coords
.SetRow(coords
.GetRow() + cell_rows
);
4977 coords
.SetCol(coords
.GetCol() + cell_cols
);
4980 if ( event
.Dragging() )
4982 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4984 // Don't start doing anything until the mouse has been drug at
4985 // least 3 pixels in any direction...
4988 if (m_startDragPos
== wxDefaultPosition
)
4990 m_startDragPos
= pos
;
4993 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4997 m_isDragging
= TRUE
;
4998 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5000 // Hide the edit control, so it
5001 // won't interfer with drag-shrinking.
5002 if ( IsCellEditControlShown() )
5004 HideCellEditControl();
5005 SaveEditControlValue();
5008 // Have we captured the mouse yet?
5011 m_winCapture
= m_gridWin
;
5012 m_winCapture
->CaptureMouse();
5015 if ( coords
!= wxGridNoCellCoords
)
5017 if ( event
.ControlDown() )
5019 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5020 m_selectingKeyboard
= coords
;
5021 HighlightBlock ( m_selectingKeyboard
, coords
);
5025 if ( !IsSelection() )
5027 HighlightBlock( coords
, coords
);
5031 HighlightBlock( m_currentCellCoords
, coords
);
5035 if (! IsVisible(coords
))
5037 MakeCellVisible(coords
);
5038 // TODO: need to introduce a delay or something here. The
5039 // scrolling is way to fast, at least on MSW - also on GTK.
5043 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5045 int cw
, ch
, left
, dummy
;
5046 m_gridWin
->GetClientSize( &cw
, &ch
);
5047 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5049 wxClientDC
dc( m_gridWin
);
5051 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5052 GetRowMinimalHeight(m_dragRowOrCol
) );
5053 dc
.SetLogicalFunction(wxINVERT
);
5054 if ( m_dragLastPos
>= 0 )
5056 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5058 dc
.DrawLine( left
, y
, left
+cw
, y
);
5061 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5063 int cw
, ch
, dummy
, top
;
5064 m_gridWin
->GetClientSize( &cw
, &ch
);
5065 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5067 wxClientDC
dc( m_gridWin
);
5069 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5070 GetColMinimalWidth(m_dragRowOrCol
) );
5071 dc
.SetLogicalFunction(wxINVERT
);
5072 if ( m_dragLastPos
>= 0 )
5074 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5076 dc
.DrawLine( x
, top
, x
, top
+ch
);
5083 m_isDragging
= FALSE
;
5084 m_startDragPos
= wxDefaultPosition
;
5086 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5087 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5090 if ( event
.Entering() || event
.Leaving() )
5092 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5093 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5098 // ------------ Left button pressed
5100 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5102 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5107 if ( !event
.ControlDown() )
5109 if ( event
.ShiftDown() )
5113 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5114 m_currentCellCoords
.GetCol(),
5117 event
.ControlDown(),
5123 else if ( XToEdgeOfCol(x
) < 0 &&
5124 YToEdgeOfRow(y
) < 0 )
5126 DisableCellEditControl();
5127 MakeCellVisible( coords
);
5129 // if this is the second click on this cell then start
5131 if ( m_waitForSlowClick
&&
5132 (coords
== m_currentCellCoords
) &&
5133 CanEnableCellControl())
5135 EnableCellEditControl();
5137 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5138 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5141 editor
->StartingClick();
5145 m_waitForSlowClick
= FALSE
;
5149 if ( event
.ControlDown() )
5153 m_selection
->ToggleCellSelection( coords
.GetRow(),
5155 event
.ControlDown(),
5160 m_selectingTopLeft
= wxGridNoCellCoords
;
5161 m_selectingBottomRight
= wxGridNoCellCoords
;
5162 m_selectingKeyboard
= coords
;
5166 SetCurrentCell( coords
);
5169 if ( m_selection
->GetSelectionMode() !=
5170 wxGrid::wxGridSelectCells
)
5172 HighlightBlock( coords
, coords
);
5176 m_waitForSlowClick
= TRUE
;
5183 // ------------ Left double click
5185 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5187 DisableCellEditControl();
5189 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5191 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5199 // ------------ Left button released
5201 else if ( event
.LeftUp() )
5203 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5205 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5206 m_selectingBottomRight
!= wxGridNoCellCoords
)
5210 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5211 m_winCapture
= NULL
;
5216 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5217 m_selectingTopLeft
.GetCol(),
5218 m_selectingBottomRight
.GetRow(),
5219 m_selectingBottomRight
.GetCol(),
5220 event
.ControlDown(),
5226 m_selectingTopLeft
= wxGridNoCellCoords
;
5227 m_selectingBottomRight
= wxGridNoCellCoords
;
5230 // Show the edit control, if it has been hidden for
5232 ShowCellEditControl();
5234 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5236 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5237 DoEndDragResizeRow();
5239 // Note: we are ending the event *after* doing
5240 // default processing in this case
5242 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5244 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5246 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5247 DoEndDragResizeCol();
5249 // Note: we are ending the event *after* doing
5250 // default processing in this case
5252 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5259 // ------------ Right button down
5261 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5263 DisableCellEditControl();
5264 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5269 // no default action at the moment
5274 // ------------ Right double click
5276 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5278 DisableCellEditControl();
5279 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5284 // no default action at the moment
5288 // ------------ Moving and no button action
5290 else if ( event
.Moving() && !event
.IsButton() )
5292 int dragRow
= YToEdgeOfRow( y
);
5293 int dragCol
= XToEdgeOfCol( x
);
5295 // Dragging on the corner of a cell to resize in both
5296 // directions is not implemented yet...
5298 if ( dragRow
>= 0 && dragCol
>= 0 )
5300 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5306 m_dragRowOrCol
= dragRow
;
5308 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5310 if ( CanDragRowSize() && CanDragGridSize() )
5311 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5316 m_dragRowOrCol
= dragCol
;
5324 m_dragRowOrCol
= dragCol
;
5326 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5328 if ( CanDragColSize() && CanDragGridSize() )
5329 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5335 // Neither on a row or col edge
5337 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5339 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5345 void wxGrid::DoEndDragResizeRow()
5347 if ( m_dragLastPos
>= 0 )
5349 // erase the last line and resize the row
5351 int cw
, ch
, left
, dummy
;
5352 m_gridWin
->GetClientSize( &cw
, &ch
);
5353 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5355 wxClientDC
dc( m_gridWin
);
5357 dc
.SetLogicalFunction( wxINVERT
);
5358 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5359 HideCellEditControl();
5360 SaveEditControlValue();
5362 int rowTop
= GetRowTop(m_dragRowOrCol
);
5363 SetRowSize( m_dragRowOrCol
,
5364 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5366 if ( !GetBatchCount() )
5368 // Only needed to get the correct rect.y:
5369 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5371 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5372 rect
.width
= m_rowLabelWidth
;
5373 rect
.height
= ch
- rect
.y
;
5374 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5376 // if there is a multicell block, paint all of it
5379 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5380 int leftCol
= XToCol(left
);
5381 int rightCol
= XToCol(left
+cw
);
5384 if (rightCol
< 0) rightCol
= m_numCols
;
5385 for (i
=leftCol
; i
<rightCol
; i
++)
5387 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5388 if (cell_rows
< subtract_rows
)
5389 subtract_rows
= cell_rows
;
5391 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5392 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5393 rect
.height
= ch
- rect
.y
;
5396 m_gridWin
->Refresh( FALSE
, &rect
);
5399 ShowCellEditControl();
5404 void wxGrid::DoEndDragResizeCol()
5406 if ( m_dragLastPos
>= 0 )
5408 // erase the last line and resize the col
5410 int cw
, ch
, dummy
, top
;
5411 m_gridWin
->GetClientSize( &cw
, &ch
);
5412 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5414 wxClientDC
dc( m_gridWin
);
5416 dc
.SetLogicalFunction( wxINVERT
);
5417 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5418 HideCellEditControl();
5419 SaveEditControlValue();
5421 int colLeft
= GetColLeft(m_dragRowOrCol
);
5422 SetColSize( m_dragRowOrCol
,
5423 wxMax( m_dragLastPos
- colLeft
,
5424 GetColMinimalWidth(m_dragRowOrCol
) ) );
5426 if ( !GetBatchCount() )
5428 // Only needed to get the correct rect.x:
5429 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5431 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5432 rect
.width
= cw
- rect
.x
;
5433 rect
.height
= m_colLabelHeight
;
5434 m_colLabelWin
->Refresh( TRUE
, &rect
);
5436 // if there is a multicell block, paint all of it
5439 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5440 int topRow
= YToRow(top
);
5441 int bottomRow
= YToRow(top
+cw
);
5444 if (bottomRow
< 0) bottomRow
= m_numRows
;
5445 for (i
=topRow
; i
<bottomRow
; i
++)
5447 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5448 if (cell_cols
< subtract_cols
)
5449 subtract_cols
= cell_cols
;
5451 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5452 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5453 rect
.width
= cw
- rect
.x
;
5456 m_gridWin
->Refresh( FALSE
, &rect
);
5459 ShowCellEditControl();
5466 // ------ interaction with data model
5468 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5470 switch ( msg
.GetId() )
5472 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5473 return GetModelValues();
5475 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5476 return SetModelValues();
5478 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5479 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5480 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5481 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5482 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5483 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5484 return Redimension( msg
);
5493 // The behaviour of this function depends on the grid table class
5494 // Clear() function. For the default wxGridStringTable class the
5495 // behavious is to replace all cell contents with wxEmptyString but
5496 // not to change the number of rows or cols.
5498 void wxGrid::ClearGrid()
5502 if (IsCellEditControlEnabled())
5503 DisableCellEditControl();
5506 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5511 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5513 // TODO: something with updateLabels flag
5517 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5523 if (IsCellEditControlEnabled())
5524 DisableCellEditControl();
5526 return m_table
->InsertRows( pos
, numRows
);
5528 // the table will have sent the results of the insert row
5529 // operation to this view object as a grid table message
5535 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5537 // TODO: something with updateLabels flag
5541 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5545 return ( m_table
&& m_table
->AppendRows( numRows
) );
5546 // the table will have sent the results of the append row
5547 // operation to this view object as a grid table message
5551 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5553 // TODO: something with updateLabels flag
5557 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5563 if (IsCellEditControlEnabled())
5564 DisableCellEditControl();
5566 return (m_table
->DeleteRows( pos
, numRows
));
5567 // the table will have sent the results of the delete row
5568 // operation to this view object as a grid table message
5574 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5576 // TODO: something with updateLabels flag
5580 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5586 if (IsCellEditControlEnabled())
5587 DisableCellEditControl();
5589 return m_table
->InsertCols( pos
, numCols
);
5590 // the table will have sent the results of the insert col
5591 // operation to this view object as a grid table message
5597 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5599 // TODO: something with updateLabels flag
5603 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5607 return ( m_table
&& m_table
->AppendCols( numCols
) );
5608 // the table will have sent the results of the append col
5609 // operation to this view object as a grid table message
5613 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5615 // TODO: something with updateLabels flag
5619 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5625 if (IsCellEditControlEnabled())
5626 DisableCellEditControl();
5628 return ( m_table
->DeleteCols( pos
, numCols
) );
5629 // the table will have sent the results of the delete col
5630 // operation to this view object as a grid table message
5638 // ----- event handlers
5641 // Generate a grid event based on a mouse event and
5642 // return the result of ProcessEvent()
5644 int wxGrid::SendEvent( const wxEventType type
,
5646 wxMouseEvent
& mouseEv
)
5651 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5653 int rowOrCol
= (row
== -1 ? col
: row
);
5655 wxGridSizeEvent
gridEvt( GetId(),
5659 mouseEv
.GetX() + GetRowLabelSize(),
5660 mouseEv
.GetY() + GetColLabelSize(),
5661 mouseEv
.ControlDown(),
5662 mouseEv
.ShiftDown(),
5664 mouseEv
.MetaDown() );
5666 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5667 vetoed
= !gridEvt
.IsAllowed();
5669 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5671 // Right now, it should _never_ end up here!
5672 wxGridRangeSelectEvent
gridEvt( GetId(),
5676 m_selectingBottomRight
,
5678 mouseEv
.ControlDown(),
5679 mouseEv
.ShiftDown(),
5681 mouseEv
.MetaDown() );
5683 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5684 vetoed
= !gridEvt
.IsAllowed();
5688 wxGridEvent
gridEvt( GetId(),
5692 mouseEv
.GetX() + GetRowLabelSize(),
5693 mouseEv
.GetY() + GetColLabelSize(),
5695 mouseEv
.ControlDown(),
5696 mouseEv
.ShiftDown(),
5698 mouseEv
.MetaDown() );
5699 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5700 vetoed
= !gridEvt
.IsAllowed();
5703 // A Veto'd event may not be `claimed' so test this first
5704 if (vetoed
) return -1;
5705 return claimed
? 1 : 0;
5709 // Generate a grid event of specified type and return the result
5710 // of ProcessEvent().
5712 int wxGrid::SendEvent( const wxEventType type
,
5718 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5720 int rowOrCol
= (row
== -1 ? col
: row
);
5722 wxGridSizeEvent
gridEvt( GetId(),
5727 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5728 vetoed
= !gridEvt
.IsAllowed();
5732 wxGridEvent
gridEvt( GetId(),
5737 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5738 vetoed
= !gridEvt
.IsAllowed();
5741 // A Veto'd event may not be `claimed' so test this first
5742 if (vetoed
) return -1;
5743 return claimed
? 1 : 0;
5747 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5749 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5752 void wxGrid::Refresh(bool eraseb
, wxRect
* rect
)
5754 // Don't do anything if between Begin/EndBatch...
5755 // EndBatch() will do all this on the last nested one anyway.
5756 if (! GetBatchCount())
5758 wxScrolledWindow::Refresh(eraseb
,rect
);
5760 int off_x
=0 ,off_y
=0;
5761 wxRect
* anotherrect
= NULL
;
5765 //Copy rectangle can get scroll offsets..
5766 anotherrect
= new wxRect(*rect
);
5767 CalcScrolledPosition( 0, 0, &off_x
, &off_y
);
5769 //Corner label Doesn't move from the origin.
5770 m_cornerLabelWin
->Refresh(eraseb
,rect
);
5772 //Move Rect down for row labels...
5774 rect
->Offset(0,off_y
);
5775 m_rowLabelWin
->Refresh(eraseb
,rect
);
5777 //Move rect copy along for col labels...
5779 anotherrect
->Offset(off_x
,0);
5780 m_colLabelWin
->Refresh(eraseb
,anotherrect
);
5782 //Move main rect along (so it's down and across!)
5785 rect
->Offset(off_x
,0);
5786 m_gridWin
->Refresh(eraseb
,rect
);
5790 void wxGrid::OnSize( wxSizeEvent
& event
)
5792 // position the child windows
5795 // don't call CalcDimensions() from here, the base class handles the size
5801 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5803 if ( m_inOnKeyDown
)
5805 // shouldn't be here - we are going round in circles...
5807 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5810 m_inOnKeyDown
= TRUE
;
5812 // propagate the event up and see if it gets processed
5814 wxWindow
*parent
= GetParent();
5815 wxKeyEvent
keyEvt( event
);
5816 keyEvt
.SetEventObject( parent
);
5818 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5821 // try local handlers
5823 switch ( event
.KeyCode() )
5826 if ( event
.ControlDown() )
5828 MoveCursorUpBlock( event
.ShiftDown() );
5832 MoveCursorUp( event
.ShiftDown() );
5837 if ( event
.ControlDown() )
5839 MoveCursorDownBlock( event
.ShiftDown() );
5843 MoveCursorDown( event
.ShiftDown() );
5848 if ( event
.ControlDown() )
5850 MoveCursorLeftBlock( event
.ShiftDown() );
5854 MoveCursorLeft( event
.ShiftDown() );
5859 if ( event
.ControlDown() )
5861 MoveCursorRightBlock( event
.ShiftDown() );
5865 MoveCursorRight( event
.ShiftDown() );
5870 case WXK_NUMPAD_ENTER
:
5871 if ( event
.ControlDown() )
5873 event
.Skip(); // to let the edit control have the return
5877 if ( GetGridCursorRow() < GetNumberRows()-1 )
5879 MoveCursorDown( event
.ShiftDown() );
5883 // at the bottom of a column
5884 HideCellEditControl();
5885 SaveEditControlValue();
5895 if (event
.ShiftDown())
5897 if ( GetGridCursorCol() > 0 )
5899 MoveCursorLeft( FALSE
);
5904 HideCellEditControl();
5905 SaveEditControlValue();
5910 if ( GetGridCursorCol() < GetNumberCols()-1 )
5912 MoveCursorRight( FALSE
);
5917 HideCellEditControl();
5918 SaveEditControlValue();
5924 if ( event
.ControlDown() )
5926 MakeCellVisible( 0, 0 );
5927 SetCurrentCell( 0, 0 );
5936 if ( event
.ControlDown() )
5938 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5939 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5956 if ( event
.ControlDown() )
5960 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5961 m_currentCellCoords
.GetCol(),
5962 event
.ControlDown(),
5969 if ( !IsEditable() )
5971 MoveCursorRight( FALSE
);
5974 // Otherwise fall through to default
5977 // is it possible to edit the current cell at all?
5978 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5980 // yes, now check whether the cells editor accepts the key
5981 int row
= m_currentCellCoords
.GetRow();
5982 int col
= m_currentCellCoords
.GetCol();
5983 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5984 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5986 // <F2> is special and will always start editing, for
5987 // other keys - ask the editor itself
5988 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5989 || editor
->IsAcceptedKey(event
) )
5991 EnableCellEditControl();
5993 // the editor could be not shown for a variety of
5994 // reasons (i.e. blocked by the app or whatever), so
5995 // check if it really was created
5996 if ( m_cellEditCtrlEnabled
)
5998 editor
->StartingKey(event
);
6011 // let others process char events with modifiers or all
6012 // char events for readonly cells
6019 m_inOnKeyDown
= FALSE
;
6022 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6024 // try local handlers
6026 if ( event
.KeyCode() == WXK_SHIFT
)
6028 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6029 m_selectingBottomRight
!= wxGridNoCellCoords
)
6033 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6034 m_selectingTopLeft
.GetCol(),
6035 m_selectingBottomRight
.GetRow(),
6036 m_selectingBottomRight
.GetCol(),
6037 event
.ControlDown(),
6044 m_selectingTopLeft
= wxGridNoCellCoords
;
6045 m_selectingBottomRight
= wxGridNoCellCoords
;
6046 m_selectingKeyboard
= wxGridNoCellCoords
;
6050 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6054 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6056 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6058 // the event has been intercepted - do nothing
6062 wxClientDC
dc(m_gridWin
);
6065 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6067 HideCellEditControl();
6068 DisableCellEditControl();
6070 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6073 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
6074 if ( !m_gridLinesEnabled
)
6082 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6084 // Otherwise refresh redraws the highlight!
6085 m_currentCellCoords
= coords
;
6087 DrawGridCellArea(dc
,cells
);
6088 DrawAllGridLines( dc
, r
);
6092 m_currentCellCoords
= coords
;
6094 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6095 DrawCellHighlight(dc
, attr
);
6100 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6103 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6107 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6110 rightCol
= GetNumberCols() - 1;
6112 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6115 bottomRow
= GetNumberRows() - 1;
6119 if ( topRow
> bottomRow
)
6126 if ( leftCol
> rightCol
)
6133 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6134 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6136 if ( m_selectingTopLeft
!= updateTopLeft
||
6137 m_selectingBottomRight
!= updateBottomRight
)
6139 // Compute two optimal update rectangles:
6140 // Either one rectangle is a real subset of the
6141 // other, or they are (almost) disjoint!
6143 bool need_refresh
[4];
6147 need_refresh
[3] = FALSE
;
6150 // Store intermediate values
6151 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6152 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6153 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6154 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6156 // Determine the outer/inner coordinates.
6157 if (oldLeft
> leftCol
)
6163 if (oldTop
> topRow
)
6169 if (oldRight
< rightCol
)
6172 oldRight
= rightCol
;
6175 if (oldBottom
< bottomRow
)
6178 oldBottom
= bottomRow
;
6182 // Now, either the stuff marked old is the outer
6183 // rectangle or we don't have a situation where one
6184 // is contained in the other.
6186 if ( oldLeft
< leftCol
)
6188 need_refresh
[0] = TRUE
;
6189 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6191 wxGridCellCoords ( oldBottom
,
6195 if ( oldTop
< topRow
)
6197 need_refresh
[1] = TRUE
;
6198 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6200 wxGridCellCoords ( topRow
- 1,
6204 if ( oldRight
> rightCol
)
6206 need_refresh
[2] = TRUE
;
6207 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6209 wxGridCellCoords ( oldBottom
,
6213 if ( oldBottom
> bottomRow
)
6215 need_refresh
[3] = TRUE
;
6216 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6218 wxGridCellCoords ( oldBottom
,
6224 m_selectingTopLeft
= updateTopLeft
;
6225 m_selectingBottomRight
= updateBottomRight
;
6227 // various Refresh() calls
6228 for (i
= 0; i
< 4; i
++ )
6229 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6230 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6233 // never generate an event as it will be generated from
6234 // wxGridSelection::SelectBlock!
6235 // (old comment from when this was the body of SelectBlock)
6239 // ------ functions to get/send data (see also public functions)
6242 bool wxGrid::GetModelValues()
6246 // all we need to do is repaint the grid
6248 m_gridWin
->Refresh();
6256 bool wxGrid::SetModelValues()
6262 for ( row
= 0; row
< m_numRows
; row
++ )
6264 for ( col
= 0; col
< m_numCols
; col
++ )
6266 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6278 // Note - this function only draws cells that are in the list of
6279 // exposed cells (usually set from the update region by
6280 // CalcExposedCells)
6282 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6284 if ( !m_numRows
|| !m_numCols
) return;
6286 int i
, j
, k
, l
, numCells
= cells
.GetCount();
6287 int row
, col
, cell_rows
, cell_cols
;
6288 wxGridCellCoordsArray redrawCells
;
6290 for ( i
= numCells
-1; i
>= 0; i
-- )
6292 row
= cells
[i
].GetRow();
6293 col
= cells
[i
].GetCol();
6294 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6296 // If this cell is part of a multicell block, find owner for repaint
6297 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6299 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6300 bool marked
= FALSE
;
6301 for ( j
= 0; j
< numCells
; j
++ )
6303 if ( cell
== cells
[j
] )
6311 int count
= redrawCells
.GetCount();
6312 for (j
= 0; j
< count
; j
++)
6314 if ( cell
== redrawCells
[j
] )
6320 if (!marked
) redrawCells
.Add( cell
);
6322 continue; // don't bother drawing this cell
6325 // If this cell is empty, find cell to left that might want to overflow
6326 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6328 for ( l
= 0; l
< cell_rows
; l
++ )
6330 for (j
= col
-1; j
>= 0; j
--)
6332 if (!m_table
->IsEmptyCell(row
+l
, j
))
6334 if (GetCellOverflow(row
+l
, j
))
6336 wxGridCellCoords
cell(row
+l
, j
);
6337 bool marked
= FALSE
;
6339 for (k
= 0; k
< numCells
; k
++)
6341 if ( cell
== cells
[k
] )
6349 int count
= redrawCells
.GetCount();
6350 for (k
= 0; k
< count
; k
++)
6352 if ( cell
== redrawCells
[k
] )
6358 if (!marked
) redrawCells
.Add( cell
);
6366 DrawCell( dc
, cells
[i
] );
6369 numCells
= redrawCells
.GetCount();
6371 for ( i
= numCells
- 1; i
>= 0; i
-- )
6373 DrawCell( dc
, redrawCells
[i
] );
6378 void wxGrid::DrawGridSpace( wxDC
& dc
)
6381 m_gridWin
->GetClientSize( &cw
, &ch
);
6384 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6386 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6387 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6389 if ( right
> rightCol
|| bottom
> bottomRow
)
6392 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6394 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6395 dc
.SetPen( *wxTRANSPARENT_PEN
);
6397 if ( right
> rightCol
)
6399 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6402 if ( bottom
> bottomRow
)
6404 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6410 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6412 int row
= coords
.GetRow();
6413 int col
= coords
.GetCol();
6415 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6418 // we draw the cell border ourselves
6419 #if !WXGRID_DRAW_LINES
6420 if ( m_gridLinesEnabled
)
6421 DrawCellBorder( dc
, coords
);
6424 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6426 bool isCurrent
= coords
== m_currentCellCoords
;
6428 wxRect rect
= CellToRect( row
, col
);
6430 // if the editor is shown, we should use it and not the renderer
6431 // Note: However, only if it is really _shown_, i.e. not hidden!
6432 if ( isCurrent
&& IsCellEditControlShown() )
6434 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6435 editor
->PaintBackground(rect
, attr
);
6440 // but all the rest is drawn by the cell renderer and hence may be
6442 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6443 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6450 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6452 int row
= m_currentCellCoords
.GetRow();
6453 int col
= m_currentCellCoords
.GetCol();
6455 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6458 wxRect rect
= CellToRect(row
, col
);
6460 // hmmm... what could we do here to show that the cell is disabled?
6461 // for now, I just draw a thinner border than for the other ones, but
6462 // it doesn't look really good
6464 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6468 // The center of th drawn line is where the position/width/height of
6469 // the rectangle is actually at, (on wxMSW atr least,) so we will
6470 // reduce the size of the rectangle to compensate for the thickness of
6471 // the line. If this is too strange on non wxMSW platforms then
6472 // please #ifdef this appropriately.
6473 rect
.x
+= penWidth
/2;
6474 rect
.y
+= penWidth
/2;
6475 rect
.width
-= penWidth
-1;
6476 rect
.height
-= penWidth
-1;
6479 // Now draw the rectangle
6480 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6481 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6482 dc
.DrawRectangle(rect
);
6486 // VZ: my experiments with 3d borders...
6488 // how to properly set colours for arbitrary bg?
6489 wxCoord x1
= rect
.x
,
6491 x2
= rect
.x
+ rect
.width
-1,
6492 y2
= rect
.y
+ rect
.height
-1;
6494 dc
.SetPen(*wxWHITE_PEN
);
6495 dc
.DrawLine(x1
, y1
, x2
, y1
);
6496 dc
.DrawLine(x1
, y1
, x1
, y2
);
6498 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6499 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6501 dc
.SetPen(*wxBLACK_PEN
);
6502 dc
.DrawLine(x1
, y2
, x2
, y2
);
6503 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6508 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6510 int row
= coords
.GetRow();
6511 int col
= coords
.GetCol();
6512 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6515 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6517 wxRect rect
= CellToRect( row
, col
);
6519 // right hand border
6521 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6522 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6526 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6527 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6530 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6532 // This if block was previously in wxGrid::OnPaint but that doesn't
6533 // seem to get called under wxGTK - MB
6535 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6536 m_numRows
&& m_numCols
)
6538 m_currentCellCoords
.Set(0, 0);
6541 if ( IsCellEditControlShown() )
6543 // don't show highlight when the edit control is shown
6547 // if the active cell was repainted, repaint its highlight too because it
6548 // might have been damaged by the grid lines
6549 size_t count
= cells
.GetCount();
6550 for ( size_t n
= 0; n
< count
; n
++ )
6552 if ( cells
[n
] == m_currentCellCoords
)
6554 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6555 DrawCellHighlight(dc
, attr
);
6563 // TODO: remove this ???
6564 // This is used to redraw all grid lines e.g. when the grid line colour
6567 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6569 #if !WXGRID_DRAW_LINES
6573 if ( !m_gridLinesEnabled
||
6575 !m_numCols
) return;
6577 int top
, bottom
, left
, right
;
6579 #if 0 //#ifndef __WXGTK__
6583 m_gridWin
->GetClientSize(&cw
, &ch
);
6585 // virtual coords of visible area
6587 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6588 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6593 reg
.GetBox(x
, y
, w
, h
);
6594 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6595 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6599 m_gridWin
->GetClientSize(&cw
, &ch
);
6600 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6601 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6604 // avoid drawing grid lines past the last row and col
6606 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6607 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6609 // no gridlines inside multicells, clip them out
6610 int leftCol
= XToCol(left
);
6611 int topRow
= YToRow(top
);
6612 int rightCol
= XToCol(right
);
6613 int bottomRow
= YToRow(bottom
);
6614 wxRegion
clippedcells(0, 0, cw
, ch
);
6616 if ((leftCol
>= 0) && (topRow
>= 0))
6618 if (rightCol
< 0) rightCol
= m_numCols
;
6619 if (bottomRow
< 0) bottomRow
= m_numRows
;
6621 int i
, j
, cell_rows
, cell_cols
;
6624 for (j
=topRow
; j
<bottomRow
; j
++)
6626 for (i
=leftCol
; i
<rightCol
; i
++)
6628 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6629 if ((cell_rows
> 1) || (cell_cols
> 1))
6631 rect
= CellToRect(j
,i
);
6632 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6633 clippedcells
.Subtract(rect
);
6635 else if ((cell_rows
< 0) || (cell_cols
< 0))
6637 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6638 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6639 clippedcells
.Subtract(rect
);
6644 dc
.SetClippingRegion( clippedcells
);
6646 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6648 // horizontal grid lines
6651 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6653 int bot
= GetRowBottom(i
) - 1;
6662 dc
.DrawLine( left
, bot
, right
, bot
);
6667 // vertical grid lines
6669 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6671 int colRight
= GetColRight(i
) - 1;
6672 if ( colRight
> right
)
6677 if ( colRight
>= left
)
6679 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6682 dc
.DestroyClippingRegion();
6686 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6688 if ( !m_numRows
) return;
6691 size_t numLabels
= rows
.GetCount();
6693 for ( i
= 0; i
< numLabels
; i
++ )
6695 DrawRowLabel( dc
, rows
[i
] );
6700 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6702 if ( GetRowHeight(row
) <= 0 )
6705 int rowTop
= GetRowTop(row
),
6706 rowBottom
= GetRowBottom(row
) - 1;
6708 dc
.SetPen( *wxBLACK_PEN
);
6709 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6710 m_rowLabelWidth
-1, rowBottom
);
6712 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6714 dc
.SetPen( *wxWHITE_PEN
);
6715 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6716 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6718 dc
.SetBackgroundMode( wxTRANSPARENT
);
6719 dc
.SetTextForeground( GetLabelTextColour() );
6720 dc
.SetFont( GetLabelFont() );
6723 GetRowLabelAlignment( &hAlign
, &vAlign
);
6727 rect
.SetY( GetRowTop(row
) + 2 );
6728 rect
.SetWidth( m_rowLabelWidth
- 4 );
6729 rect
.SetHeight( GetRowHeight(row
) - 4 );
6730 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6734 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6736 if ( !m_numCols
) return;
6739 size_t numLabels
= cols
.GetCount();
6741 for ( i
= 0; i
< numLabels
; i
++ )
6743 DrawColLabel( dc
, cols
[i
] );
6748 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6750 if ( GetColWidth(col
) <= 0 )
6753 int colLeft
= GetColLeft(col
),
6754 colRight
= GetColRight(col
) - 1;
6756 dc
.SetPen( *wxBLACK_PEN
);
6757 dc
.DrawLine( colRight
, 0,
6758 colRight
, m_colLabelHeight
-1 );
6760 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6761 colRight
, m_colLabelHeight
-1 );
6763 dc
.SetPen( *wxWHITE_PEN
);
6764 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6765 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6767 dc
.SetBackgroundMode( wxTRANSPARENT
);
6768 dc
.SetTextForeground( GetLabelTextColour() );
6769 dc
.SetFont( GetLabelFont() );
6771 dc
.SetBackgroundMode( wxTRANSPARENT
);
6772 dc
.SetTextForeground( GetLabelTextColour() );
6773 dc
.SetFont( GetLabelFont() );
6776 GetColLabelAlignment( &hAlign
, &vAlign
);
6779 rect
.SetX( colLeft
+ 2 );
6781 rect
.SetWidth( GetColWidth(col
) - 4 );
6782 rect
.SetHeight( m_colLabelHeight
- 4 );
6783 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6786 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6787 const wxString
& value
,
6792 wxArrayString lines
;
6794 StringToLines( value
, lines
);
6797 //Forward to new API.
6798 DrawTextRectangle( dc
,
6806 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6807 const wxArrayString
& lines
,
6812 long textWidth
, textHeight
;
6813 long lineWidth
, lineHeight
;
6815 dc
.SetClippingRegion( rect
);
6816 if ( lines
.GetCount() )
6818 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6819 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6822 switch ( horizAlign
)
6825 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6828 case wxALIGN_CENTRE
:
6829 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6838 switch ( vertAlign
)
6840 case wxALIGN_BOTTOM
:
6841 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6844 case wxALIGN_CENTRE
:
6845 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6854 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6856 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6861 dc
.DestroyClippingRegion();
6865 // Split multi line text up into an array of strings. Any existing
6866 // contents of the string array are preserved.
6868 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6872 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6873 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6875 while ( startPos
< (int)tVal
.Length() )
6877 pos
= tVal
.Mid(startPos
).Find( eol
);
6882 else if ( pos
== 0 )
6884 lines
.Add( wxEmptyString
);
6888 lines
.Add( value
.Mid(startPos
, pos
) );
6892 if ( startPos
< (int)value
.Length() )
6894 lines
.Add( value
.Mid( startPos
) );
6899 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6900 const wxArrayString
& lines
,
6901 long *width
, long *height
)
6908 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6910 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6911 w
= wxMax( w
, lineW
);
6920 // ------ Batch processing.
6922 void wxGrid::EndBatch()
6924 if ( m_batchCount
> 0 )
6927 if ( !m_batchCount
)
6930 m_rowLabelWin
->Refresh();
6931 m_colLabelWin
->Refresh();
6932 m_cornerLabelWin
->Refresh();
6933 m_gridWin
->Refresh();
6938 // Use this, rather than wxWindow::Refresh(), to force an immediate
6939 // repainting of the grid. Has no effect if you are already inside a
6940 // BeginBatch / EndBatch block.
6942 void wxGrid::ForceRefresh()
6950 // ------ Edit control functions
6954 void wxGrid::EnableEditing( bool edit
)
6956 // TODO: improve this ?
6958 if ( edit
!= m_editable
)
6960 if(!edit
) EnableCellEditControl(edit
);
6966 void wxGrid::EnableCellEditControl( bool enable
)
6971 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6972 SetCurrentCell( 0, 0 );
6974 if ( enable
!= m_cellEditCtrlEnabled
)
6978 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6981 // this should be checked by the caller!
6982 wxASSERT_MSG( CanEnableCellControl(),
6983 _T("can't enable editing for this cell!") );
6985 // do it before ShowCellEditControl()
6986 m_cellEditCtrlEnabled
= enable
;
6988 ShowCellEditControl();
6992 //FIXME:add veto support
6993 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6995 HideCellEditControl();
6996 SaveEditControlValue();
6998 // do it after HideCellEditControl()
6999 m_cellEditCtrlEnabled
= enable
;
7004 bool wxGrid::IsCurrentCellReadOnly() const
7007 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7008 bool readonly
= attr
->IsReadOnly();
7014 bool wxGrid::CanEnableCellControl() const
7016 return m_editable
&& !IsCurrentCellReadOnly();
7019 bool wxGrid::IsCellEditControlEnabled() const
7021 // the cell edit control might be disable for all cells or just for the
7022 // current one if it's read only
7023 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7026 bool wxGrid::IsCellEditControlShown() const
7028 bool isShown
= FALSE
;
7030 if ( m_cellEditCtrlEnabled
)
7032 int row
= m_currentCellCoords
.GetRow();
7033 int col
= m_currentCellCoords
.GetCol();
7034 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7035 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7040 if ( editor
->IsCreated() )
7042 isShown
= editor
->GetControl()->IsShown();
7052 void wxGrid::ShowCellEditControl()
7054 if ( IsCellEditControlEnabled() )
7056 if ( !IsVisible( m_currentCellCoords
) )
7058 m_cellEditCtrlEnabled
= FALSE
;
7063 wxRect rect
= CellToRect( m_currentCellCoords
);
7064 int row
= m_currentCellCoords
.GetRow();
7065 int col
= m_currentCellCoords
.GetCol();
7067 // if this is part of a multicell, find owner (topleft)
7068 int cell_rows
, cell_cols
;
7069 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7070 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7074 m_currentCellCoords
.SetRow( row
);
7075 m_currentCellCoords
.SetCol( col
);
7078 // convert to scrolled coords
7080 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7082 // done in PaintBackground()
7084 // erase the highlight and the cell contents because the editor
7085 // might not cover the entire cell
7086 wxClientDC
dc( m_gridWin
);
7088 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7089 dc
.SetPen(*wxTRANSPARENT_PEN
);
7090 dc
.DrawRectangle(rect
);
7093 // cell is shifted by one pixel
7094 // However, don't allow x or y to become negative
7095 // since the SetSize() method interprets that as
7102 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7103 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7104 if ( !editor
->IsCreated() )
7106 editor
->Create(m_gridWin
, -1,
7107 new wxGridCellEditorEvtHandler(this, editor
));
7109 wxGridEditorCreatedEvent
evt(GetId(),
7110 wxEVT_GRID_EDITOR_CREATED
,
7114 editor
->GetControl());
7115 GetEventHandler()->ProcessEvent(evt
);
7118 editor
->Show( TRUE
, attr
);
7120 // resize editor to overflow into righthand cells if allowed
7121 wxString value
= GetCellValue(row
, col
);
7122 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7124 wxClientDC
dc(m_gridWin
);
7125 wxCoord y
= 0, best_width
= 0;
7126 dc
.SetFont(attr
->GetFont());
7127 dc
.GetTextExtent(value
, &best_width
, &y
);
7129 int cell_rows
, cell_cols
;
7130 attr
->GetSize( &cell_rows
, &cell_cols
);
7132 if ((best_width
> rect
.width
) && (col
< m_numCols
) && m_table
)
7135 for (i
= col
+cell_cols
; i
< m_numCols
; i
++)
7137 if (m_table
->IsEmptyCell(row
,i
))
7139 rect
.width
+= GetColWidth(i
);
7140 if (rect
.width
>= best_width
) break;
7147 editor
->SetSize( rect
);
7149 editor
->BeginEdit(row
, col
, this);
7158 void wxGrid::HideCellEditControl()
7160 if ( IsCellEditControlEnabled() )
7162 int row
= m_currentCellCoords
.GetRow();
7163 int col
= m_currentCellCoords
.GetCol();
7165 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7166 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7167 editor
->Show( FALSE
);
7170 m_gridWin
->SetFocus();
7171 // refresh whole row to the right
7172 wxRect
rect( CellToRect(row
, col
) );
7173 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7174 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7175 m_gridWin
->Refresh( FALSE
, &rect
);
7180 void wxGrid::SaveEditControlValue()
7182 if ( IsCellEditControlEnabled() )
7184 int row
= m_currentCellCoords
.GetRow();
7185 int col
= m_currentCellCoords
.GetCol();
7187 wxString oldval
= GetCellValue(row
,col
);
7189 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7190 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7191 bool changed
= editor
->EndEdit(row
, col
, this);
7198 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7199 m_currentCellCoords
.GetRow(),
7200 m_currentCellCoords
.GetCol() ) < 0 ) {
7202 // Event has been vetoed, set the data back.
7203 SetCellValue(row
,col
,oldval
);
7211 // ------ Grid location functions
7212 // Note that all of these functions work with the logical coordinates of
7213 // grid cells and labels so you will need to convert from device
7214 // coordinates for mouse events etc.
7217 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7219 int row
= YToRow(y
);
7220 int col
= XToCol(x
);
7222 if ( row
== -1 || col
== -1 )
7224 coords
= wxGridNoCellCoords
;
7228 coords
.Set( row
, col
);
7233 // Internal Helper function for computing row or column from some
7234 // (unscrolled) coordinate value, using either
7235 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7236 // of m_rowBottoms/m_ColRights to speed up the search!
7238 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7239 const wxArrayInt
& BorderArray
, bool maxOnOverflow
)
7243 size_t i_max
= coord
/ defaultDist
,
7245 if (BorderArray
.IsEmpty())
7250 if ( i_max
>= BorderArray
.GetCount())
7251 i_max
= BorderArray
.GetCount() - 1;
7254 if ( coord
>= BorderArray
[i_max
])
7257 i_max
= coord
/ minDist
;
7259 if ( i_max
>= BorderArray
.GetCount())
7260 i_max
= BorderArray
.GetCount() - 1;
7262 if ( coord
>= BorderArray
[i_max
])
7263 return maxOnOverflow
? (int)i_max
: -1;
7264 if ( coord
< BorderArray
[0] )
7267 while ( i_max
- i_min
> 0 )
7269 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7270 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7271 if (coord
>= BorderArray
[ i_max
- 1])
7275 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7276 if (coord
< BorderArray
[median
])
7284 int wxGrid::YToRow( int y
)
7286 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7287 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, FALSE
);
7291 int wxGrid::XToCol( int x
)
7293 return CoordToRowOrCol(x
, m_defaultColWidth
,
7294 WXGRID_MIN_COL_WIDTH
, m_colRights
, FALSE
);
7298 // return the row number that that the y coord is near the edge of, or
7299 // -1 if not near an edge
7301 int wxGrid::YToEdgeOfRow( int y
)
7304 i
= internalYToRow(y
);
7306 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7308 // We know that we are in row i, test whether we are
7309 // close enough to lower or upper border, respectively.
7310 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7312 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7320 // return the col number that that the x coord is near the edge of, or
7321 // -1 if not near an edge
7323 int wxGrid::XToEdgeOfCol( int x
)
7326 i
= internalXToCol(x
);
7328 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7330 // We know that we are in column i, test whether we are
7331 // close enough to right or left border, respectively.
7332 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7334 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7342 wxRect
wxGrid::CellToRect( int row
, int col
)
7344 wxRect
rect( -1, -1, -1, -1 );
7346 if ( row
>= 0 && row
< m_numRows
&&
7347 col
>= 0 && col
< m_numCols
)
7349 int i
, cell_rows
, cell_cols
;
7350 rect
.width
= rect
.height
= 0;
7351 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7352 // if negative then find multicell owner
7353 if (cell_rows
< 0) row
+= cell_rows
;
7354 if (cell_cols
< 0) col
+= cell_cols
;
7355 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7357 rect
.x
= GetColLeft(col
);
7358 rect
.y
= GetRowTop(row
);
7359 for (i
=col
; i
<col
+cell_cols
; i
++)
7360 rect
.width
+= GetColWidth(i
);
7361 for (i
=row
; i
<row
+cell_rows
; i
++)
7362 rect
.height
+= GetRowHeight(i
);
7365 // if grid lines are enabled, then the area of the cell is a bit smaller
7366 if (m_gridLinesEnabled
) {
7374 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7376 // get the cell rectangle in logical coords
7378 wxRect
r( CellToRect( row
, col
) );
7380 // convert to device coords
7382 int left
, top
, right
, bottom
;
7383 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7384 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7386 // check against the client area of the grid window
7389 m_gridWin
->GetClientSize( &cw
, &ch
);
7391 if ( wholeCellVisible
)
7393 // is the cell wholly visible ?
7395 return ( left
>= 0 && right
<= cw
&&
7396 top
>= 0 && bottom
<= ch
);
7400 // is the cell partly visible ?
7402 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7403 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7408 // make the specified cell location visible by doing a minimal amount
7411 void wxGrid::MakeCellVisible( int row
, int col
)
7415 int xpos
= -1, ypos
= -1;
7417 if ( row
>= 0 && row
< m_numRows
&&
7418 col
>= 0 && col
< m_numCols
)
7420 // get the cell rectangle in logical coords
7422 wxRect
r( CellToRect( row
, col
) );
7424 // convert to device coords
7426 int left
, top
, right
, bottom
;
7427 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7428 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7431 m_gridWin
->GetClientSize( &cw
, &ch
);
7437 else if ( bottom
> ch
)
7439 int h
= r
.GetHeight();
7441 for ( i
= row
-1; i
>= 0; i
-- )
7443 int rowHeight
= GetRowHeight(i
);
7444 if ( h
+ rowHeight
> ch
)
7451 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7452 // have rounding errors (this is important, because if we do, we
7453 // might not scroll at all and some cells won't be redrawn)
7455 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7457 ypos
+= GRID_SCROLL_LINE_Y
;
7464 else if ( right
> cw
)
7466 int w
= r
.GetWidth();
7468 for ( i
= col
-1; i
>= 0; i
-- )
7470 int colWidth
= GetColWidth(i
);
7471 if ( w
+ colWidth
> cw
)
7478 // see comment for ypos above
7479 xpos
+= GRID_SCROLL_LINE_X
;
7482 if ( xpos
!= -1 || ypos
!= -1 )
7485 xpos
/= GRID_SCROLL_LINE_X
;
7487 ypos
/= GRID_SCROLL_LINE_Y
;
7488 Scroll( xpos
, ypos
);
7496 // ------ Grid cursor movement functions
7499 bool wxGrid::MoveCursorUp( bool expandSelection
)
7501 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7502 m_currentCellCoords
.GetRow() >= 0 )
7504 if ( expandSelection
)
7506 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7507 m_selectingKeyboard
= m_currentCellCoords
;
7508 if ( m_selectingKeyboard
.GetRow() > 0 )
7510 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7511 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7512 m_selectingKeyboard
.GetCol() );
7513 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7516 else if ( m_currentCellCoords
.GetRow() > 0 )
7519 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7520 m_currentCellCoords
.GetCol() );
7521 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7522 m_currentCellCoords
.GetCol() );
7533 bool wxGrid::MoveCursorDown( bool expandSelection
)
7535 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7536 m_currentCellCoords
.GetRow() < m_numRows
)
7538 if ( expandSelection
)
7540 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7541 m_selectingKeyboard
= m_currentCellCoords
;
7542 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7544 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7545 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7546 m_selectingKeyboard
.GetCol() );
7547 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7550 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7553 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7554 m_currentCellCoords
.GetCol() );
7555 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7556 m_currentCellCoords
.GetCol() );
7567 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7569 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7570 m_currentCellCoords
.GetCol() >= 0 )
7572 if ( expandSelection
)
7574 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7575 m_selectingKeyboard
= m_currentCellCoords
;
7576 if ( m_selectingKeyboard
.GetCol() > 0 )
7578 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7579 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7580 m_selectingKeyboard
.GetCol() );
7581 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7584 else if ( m_currentCellCoords
.GetCol() > 0 )
7587 MakeCellVisible( m_currentCellCoords
.GetRow(),
7588 m_currentCellCoords
.GetCol() - 1 );
7589 SetCurrentCell( m_currentCellCoords
.GetRow(),
7590 m_currentCellCoords
.GetCol() - 1 );
7601 bool wxGrid::MoveCursorRight( bool expandSelection
)
7603 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7604 m_currentCellCoords
.GetCol() < m_numCols
)
7606 if ( expandSelection
)
7608 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7609 m_selectingKeyboard
= m_currentCellCoords
;
7610 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7612 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7613 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7614 m_selectingKeyboard
.GetCol() );
7615 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7618 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7621 MakeCellVisible( m_currentCellCoords
.GetRow(),
7622 m_currentCellCoords
.GetCol() + 1 );
7623 SetCurrentCell( m_currentCellCoords
.GetRow(),
7624 m_currentCellCoords
.GetCol() + 1 );
7635 bool wxGrid::MovePageUp()
7637 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7639 int row
= m_currentCellCoords
.GetRow();
7643 m_gridWin
->GetClientSize( &cw
, &ch
);
7645 int y
= GetRowTop(row
);
7646 int newRow
= YToRow( y
- ch
+ 1 );
7651 else if ( newRow
== row
)
7656 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7657 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7665 bool wxGrid::MovePageDown()
7667 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7669 int row
= m_currentCellCoords
.GetRow();
7670 if ( row
< m_numRows
)
7673 m_gridWin
->GetClientSize( &cw
, &ch
);
7675 int y
= GetRowTop(row
);
7676 int newRow
= YToRow( y
+ ch
);
7679 newRow
= m_numRows
- 1;
7681 else if ( newRow
== row
)
7686 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7687 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7695 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7698 m_currentCellCoords
!= wxGridNoCellCoords
&&
7699 m_currentCellCoords
.GetRow() > 0 )
7701 int row
= m_currentCellCoords
.GetRow();
7702 int col
= m_currentCellCoords
.GetCol();
7704 if ( m_table
->IsEmptyCell(row
, col
) )
7706 // starting in an empty cell: find the next block of
7712 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7715 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7717 // starting at the top of a block: find the next block
7723 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7728 // starting within a block: find the top of the block
7733 if ( m_table
->IsEmptyCell(row
, col
) )
7741 MakeCellVisible( row
, col
);
7742 if ( expandSelection
)
7744 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7745 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7750 SetCurrentCell( row
, col
);
7758 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7761 m_currentCellCoords
!= wxGridNoCellCoords
&&
7762 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7764 int row
= m_currentCellCoords
.GetRow();
7765 int col
= m_currentCellCoords
.GetCol();
7767 if ( m_table
->IsEmptyCell(row
, col
) )
7769 // starting in an empty cell: find the next block of
7772 while ( row
< m_numRows
-1 )
7775 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7778 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7780 // starting at the bottom of a block: find the next block
7783 while ( row
< m_numRows
-1 )
7786 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7791 // starting within a block: find the bottom of the block
7793 while ( row
< m_numRows
-1 )
7796 if ( m_table
->IsEmptyCell(row
, col
) )
7804 MakeCellVisible( row
, col
);
7805 if ( expandSelection
)
7807 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7808 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7813 SetCurrentCell( row
, col
);
7822 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7825 m_currentCellCoords
!= wxGridNoCellCoords
&&
7826 m_currentCellCoords
.GetCol() > 0 )
7828 int row
= m_currentCellCoords
.GetRow();
7829 int col
= m_currentCellCoords
.GetCol();
7831 if ( m_table
->IsEmptyCell(row
, col
) )
7833 // starting in an empty cell: find the next block of
7839 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7842 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7844 // starting at the left of a block: find the next block
7850 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7855 // starting within a block: find the left of the block
7860 if ( m_table
->IsEmptyCell(row
, col
) )
7868 MakeCellVisible( row
, col
);
7869 if ( expandSelection
)
7871 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7872 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7877 SetCurrentCell( row
, col
);
7886 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7889 m_currentCellCoords
!= wxGridNoCellCoords
&&
7890 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7892 int row
= m_currentCellCoords
.GetRow();
7893 int col
= m_currentCellCoords
.GetCol();
7895 if ( m_table
->IsEmptyCell(row
, col
) )
7897 // starting in an empty cell: find the next block of
7900 while ( col
< m_numCols
-1 )
7903 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7906 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7908 // starting at the right of a block: find the next block
7911 while ( col
< m_numCols
-1 )
7914 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7919 // starting within a block: find the right of the block
7921 while ( col
< m_numCols
-1 )
7924 if ( m_table
->IsEmptyCell(row
, col
) )
7932 MakeCellVisible( row
, col
);
7933 if ( expandSelection
)
7935 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7936 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7941 SetCurrentCell( row
, col
);
7953 // ------ Label values and formatting
7956 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7958 *horiz
= m_rowLabelHorizAlign
;
7959 *vert
= m_rowLabelVertAlign
;
7962 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7964 *horiz
= m_colLabelHorizAlign
;
7965 *vert
= m_colLabelVertAlign
;
7968 wxString
wxGrid::GetRowLabelValue( int row
)
7972 return m_table
->GetRowLabelValue( row
);
7982 wxString
wxGrid::GetColLabelValue( int col
)
7986 return m_table
->GetColLabelValue( col
);
7997 void wxGrid::SetRowLabelSize( int width
)
7999 width
= wxMax( width
, 0 );
8000 if ( width
!= m_rowLabelWidth
)
8004 m_rowLabelWin
->Show( FALSE
);
8005 m_cornerLabelWin
->Show( FALSE
);
8007 else if ( m_rowLabelWidth
== 0 )
8009 m_rowLabelWin
->Show( TRUE
);
8010 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8013 m_rowLabelWidth
= width
;
8015 wxScrolledWindow::Refresh( TRUE
);
8020 void wxGrid::SetColLabelSize( int height
)
8022 height
= wxMax( height
, 0 );
8023 if ( height
!= m_colLabelHeight
)
8027 m_colLabelWin
->Show( FALSE
);
8028 m_cornerLabelWin
->Show( FALSE
);
8030 else if ( m_colLabelHeight
== 0 )
8032 m_colLabelWin
->Show( TRUE
);
8033 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8036 m_colLabelHeight
= height
;
8038 wxScrolledWindow::Refresh( TRUE
);
8043 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8045 if ( m_labelBackgroundColour
!= colour
)
8047 m_labelBackgroundColour
= colour
;
8048 m_rowLabelWin
->SetBackgroundColour( colour
);
8049 m_colLabelWin
->SetBackgroundColour( colour
);
8050 m_cornerLabelWin
->SetBackgroundColour( colour
);
8052 if ( !GetBatchCount() )
8054 m_rowLabelWin
->Refresh();
8055 m_colLabelWin
->Refresh();
8056 m_cornerLabelWin
->Refresh();
8061 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8063 if ( m_labelTextColour
!= colour
)
8065 m_labelTextColour
= colour
;
8066 if ( !GetBatchCount() )
8068 m_rowLabelWin
->Refresh();
8069 m_colLabelWin
->Refresh();
8074 void wxGrid::SetLabelFont( const wxFont
& font
)
8077 if ( !GetBatchCount() )
8079 m_rowLabelWin
->Refresh();
8080 m_colLabelWin
->Refresh();
8084 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8086 // allow old (incorrect) defs to be used
8089 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8090 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8091 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8096 case wxTOP
: vert
= wxALIGN_TOP
; break;
8097 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8098 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8101 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8103 m_rowLabelHorizAlign
= horiz
;
8106 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8108 m_rowLabelVertAlign
= vert
;
8111 if ( !GetBatchCount() )
8113 m_rowLabelWin
->Refresh();
8117 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8119 // allow old (incorrect) defs to be used
8122 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8123 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8124 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8129 case wxTOP
: vert
= wxALIGN_TOP
; break;
8130 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8131 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8134 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8136 m_colLabelHorizAlign
= horiz
;
8139 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8141 m_colLabelVertAlign
= vert
;
8144 if ( !GetBatchCount() )
8146 m_colLabelWin
->Refresh();
8150 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8154 m_table
->SetRowLabelValue( row
, s
);
8155 if ( !GetBatchCount() )
8157 wxRect rect
= CellToRect( row
, 0);
8158 if ( rect
.height
> 0 )
8160 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8162 rect
.width
= m_rowLabelWidth
;
8163 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8169 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8173 m_table
->SetColLabelValue( col
, s
);
8174 if ( !GetBatchCount() )
8176 wxRect rect
= CellToRect( 0, col
);
8177 if ( rect
.width
> 0 )
8179 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8181 rect
.height
= m_colLabelHeight
;
8182 m_colLabelWin
->Refresh( TRUE
, &rect
);
8188 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8190 if ( m_gridLineColour
!= colour
)
8192 m_gridLineColour
= colour
;
8194 wxClientDC
dc( m_gridWin
);
8196 DrawAllGridLines( dc
, wxRegion() );
8201 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8203 if ( m_cellHighlightColour
!= colour
)
8205 m_cellHighlightColour
= colour
;
8207 wxClientDC
dc( m_gridWin
);
8209 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8210 DrawCellHighlight(dc
, attr
);
8215 void wxGrid::SetCellHighlightPenWidth(int width
)
8217 if (m_cellHighlightPenWidth
!= width
) {
8218 m_cellHighlightPenWidth
= width
;
8220 // Just redrawing the cell highlight is not enough since that won't
8221 // make any visible change if the the thickness is getting smaller.
8222 int row
= m_currentCellCoords
.GetRow();
8223 int col
= m_currentCellCoords
.GetCol();
8224 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8226 wxRect rect
= CellToRect(row
, col
);
8227 m_gridWin
->Refresh(TRUE
, &rect
);
8231 void wxGrid::SetCellHighlightROPenWidth(int width
)
8233 if (m_cellHighlightROPenWidth
!= width
) {
8234 m_cellHighlightROPenWidth
= width
;
8236 // Just redrawing the cell highlight is not enough since that won't
8237 // make any visible change if the the thickness is getting smaller.
8238 int row
= m_currentCellCoords
.GetRow();
8239 int col
= m_currentCellCoords
.GetCol();
8240 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8242 wxRect rect
= CellToRect(row
, col
);
8243 m_gridWin
->Refresh(TRUE
, &rect
);
8247 void wxGrid::EnableGridLines( bool enable
)
8249 if ( enable
!= m_gridLinesEnabled
)
8251 m_gridLinesEnabled
= enable
;
8253 if ( !GetBatchCount() )
8257 wxClientDC
dc( m_gridWin
);
8259 DrawAllGridLines( dc
, wxRegion() );
8263 m_gridWin
->Refresh();
8270 int wxGrid::GetDefaultRowSize()
8272 return m_defaultRowHeight
;
8275 int wxGrid::GetRowSize( int row
)
8277 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8279 return GetRowHeight(row
);
8282 int wxGrid::GetDefaultColSize()
8284 return m_defaultColWidth
;
8287 int wxGrid::GetColSize( int col
)
8289 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8291 return GetColWidth(col
);
8294 // ============================================================================
8295 // access to the grid attributes: each of them has a default value in the grid
8296 // itself and may be overidden on a per-cell basis
8297 // ============================================================================
8299 // ----------------------------------------------------------------------------
8300 // setting default attributes
8301 // ----------------------------------------------------------------------------
8303 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8305 m_defaultCellAttr
->SetBackgroundColour(col
);
8307 m_gridWin
->SetBackgroundColour(col
);
8311 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8313 m_defaultCellAttr
->SetTextColour(col
);
8316 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8318 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8321 void wxGrid::SetDefaultCellOverflow( bool allow
)
8323 m_defaultCellAttr
->SetOverflow(allow
);
8326 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8328 m_defaultCellAttr
->SetFont(font
);
8331 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8333 m_defaultCellAttr
->SetRenderer(renderer
);
8336 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8338 m_defaultCellAttr
->SetEditor(editor
);
8341 // ----------------------------------------------------------------------------
8342 // access to the default attrbiutes
8343 // ----------------------------------------------------------------------------
8345 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8347 return m_defaultCellAttr
->GetBackgroundColour();
8350 wxColour
wxGrid::GetDefaultCellTextColour()
8352 return m_defaultCellAttr
->GetTextColour();
8355 wxFont
wxGrid::GetDefaultCellFont()
8357 return m_defaultCellAttr
->GetFont();
8360 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8362 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8365 bool wxGrid::GetDefaultCellOverflow()
8367 return m_defaultCellAttr
->GetOverflow();
8370 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8372 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8375 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8377 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8380 // ----------------------------------------------------------------------------
8381 // access to cell attributes
8382 // ----------------------------------------------------------------------------
8384 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8386 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8387 wxColour colour
= attr
->GetBackgroundColour();
8392 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8394 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8395 wxColour colour
= attr
->GetTextColour();
8400 wxFont
wxGrid::GetCellFont( int row
, int col
)
8402 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8403 wxFont font
= attr
->GetFont();
8408 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8410 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8411 attr
->GetAlignment(horiz
, vert
);
8415 bool wxGrid::GetCellOverflow( int row
, int col
)
8417 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8418 bool allow
= attr
->GetOverflow();
8423 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8425 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8426 attr
->GetSize( num_rows
, num_cols
);
8430 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8432 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8433 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8439 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8441 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8442 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8448 bool wxGrid::IsReadOnly(int row
, int col
) const
8450 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8451 bool isReadOnly
= attr
->IsReadOnly();
8456 // ----------------------------------------------------------------------------
8457 // attribute support: cache, automatic provider creation, ...
8458 // ----------------------------------------------------------------------------
8460 bool wxGrid::CanHaveAttributes()
8467 return m_table
->CanHaveAttributes();
8470 void wxGrid::ClearAttrCache()
8472 if ( m_attrCache
.row
!= -1 )
8474 wxSafeDecRef(m_attrCache
.attr
);
8475 m_attrCache
.attr
= NULL
;
8476 m_attrCache
.row
= -1;
8480 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8482 wxGrid
*self
= (wxGrid
*)this; // const_cast
8484 self
->ClearAttrCache();
8485 self
->m_attrCache
.row
= row
;
8486 self
->m_attrCache
.col
= col
;
8487 self
->m_attrCache
.attr
= attr
;
8491 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8493 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8495 *attr
= m_attrCache
.attr
;
8496 wxSafeIncRef(m_attrCache
.attr
);
8498 #ifdef DEBUG_ATTR_CACHE
8499 gs_nAttrCacheHits
++;
8506 #ifdef DEBUG_ATTR_CACHE
8507 gs_nAttrCacheMisses
++;
8513 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8515 wxGridCellAttr
*attr
= NULL
;
8516 // Additional test to avoid looking at the cache e.g. for
8517 // wxNoCellCoords, as this will confuse memory management.
8520 if ( !LookupAttr(row
, col
, &attr
) )
8522 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8523 : (wxGridCellAttr
*)NULL
;
8524 CacheAttr(row
, col
, attr
);
8529 attr
->SetDefAttr(m_defaultCellAttr
);
8533 attr
= m_defaultCellAttr
;
8540 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8542 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8544 wxCHECK_MSG( m_table
, attr
,
8545 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8547 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8550 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8552 // artificially inc the ref count to match DecRef() in caller
8554 m_table
->SetAttr(attr
, row
, col
);
8560 // ----------------------------------------------------------------------------
8561 // setting column attributes (wrappers around SetColAttr)
8562 // ----------------------------------------------------------------------------
8564 void wxGrid::SetColFormatBool(int col
)
8566 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8569 void wxGrid::SetColFormatNumber(int col
)
8571 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8574 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8576 wxString typeName
= wxGRID_VALUE_FLOAT
;
8577 if ( (width
!= -1) || (precision
!= -1) )
8579 typeName
<< _T(':') << width
<< _T(',') << precision
;
8582 SetColFormatCustom(col
, typeName
);
8585 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8587 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8589 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8591 attr
= new wxGridCellAttr
;
8592 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8593 attr
->SetRenderer(renderer
);
8595 SetColAttr(col
, attr
);
8599 // ----------------------------------------------------------------------------
8600 // setting cell attributes: this is forwarded to the table
8601 // ----------------------------------------------------------------------------
8603 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8605 if ( CanHaveAttributes() )
8607 m_table
->SetAttr(attr
, row
, col
);
8616 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8618 if ( CanHaveAttributes() )
8620 m_table
->SetRowAttr(attr
, row
);
8629 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8631 if ( CanHaveAttributes() )
8633 m_table
->SetColAttr(attr
, col
);
8642 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8644 if ( CanHaveAttributes() )
8646 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8647 attr
->SetBackgroundColour(colour
);
8652 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8654 if ( CanHaveAttributes() )
8656 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8657 attr
->SetTextColour(colour
);
8662 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8664 if ( CanHaveAttributes() )
8666 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8667 attr
->SetFont(font
);
8672 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8674 if ( CanHaveAttributes() )
8676 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8677 attr
->SetAlignment(horiz
, vert
);
8682 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8684 if ( CanHaveAttributes() )
8686 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8687 attr
->SetOverflow(allow
);
8692 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8694 if ( CanHaveAttributes() )
8696 int cell_rows
, cell_cols
;
8698 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8699 attr
->GetSize(&cell_rows
, &cell_cols
);
8700 attr
->SetSize(num_rows
, num_cols
);
8703 // Cannot set the size of a cell to 0 or negative values
8704 // While it is perfectly legal to do that, this function cannot
8705 // handle all the possibilies, do it by hand by getting the CellAttr.
8706 // You can only set the size of a cell to 1,1 or greater with this fn
8707 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8708 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8709 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8710 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8712 // if this was already a multicell then "turn off" the other cells first
8713 if ((cell_rows
> 1) || (cell_rows
> 1))
8716 for (j
=row
; j
<row
+cell_rows
; j
++)
8718 for (i
=col
; i
<col
+cell_cols
; i
++)
8720 if ((i
!= col
) || (j
!= row
))
8722 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8723 attr_stub
->SetSize( 1, 1 );
8724 attr_stub
->DecRef();
8730 // mark the cells that will be covered by this cell to
8731 // negative or zero values to point back at this cell
8732 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8735 for (j
=row
; j
<row
+num_rows
; j
++)
8737 for (i
=col
; i
<col
+num_cols
; i
++)
8739 if ((i
!= col
) || (j
!= row
))
8741 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8742 attr_stub
->SetSize( row
-j
, col
-i
);
8743 attr_stub
->DecRef();
8751 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8753 if ( CanHaveAttributes() )
8755 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8756 attr
->SetRenderer(renderer
);
8761 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8763 if ( CanHaveAttributes() )
8765 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8766 attr
->SetEditor(editor
);
8771 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8773 if ( CanHaveAttributes() )
8775 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8776 attr
->SetReadOnly(isReadOnly
);
8781 // ----------------------------------------------------------------------------
8782 // Data type registration
8783 // ----------------------------------------------------------------------------
8785 void wxGrid::RegisterDataType(const wxString
& typeName
,
8786 wxGridCellRenderer
* renderer
,
8787 wxGridCellEditor
* editor
)
8789 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8793 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8795 wxString typeName
= m_table
->GetTypeName(row
, col
);
8796 return GetDefaultEditorForType(typeName
);
8799 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8801 wxString typeName
= m_table
->GetTypeName(row
, col
);
8802 return GetDefaultRendererForType(typeName
);
8806 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8808 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8809 if ( index
== wxNOT_FOUND
)
8811 wxFAIL_MSG(wxT("Unknown data type name"));
8816 return m_typeRegistry
->GetEditor(index
);
8820 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8822 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8823 if ( index
== wxNOT_FOUND
)
8825 wxFAIL_MSG(wxT("Unknown data type name"));
8830 return m_typeRegistry
->GetRenderer(index
);
8834 // ----------------------------------------------------------------------------
8836 // ----------------------------------------------------------------------------
8838 void wxGrid::EnableDragRowSize( bool enable
)
8840 m_canDragRowSize
= enable
;
8844 void wxGrid::EnableDragColSize( bool enable
)
8846 m_canDragColSize
= enable
;
8849 void wxGrid::EnableDragGridSize( bool enable
)
8851 m_canDragGridSize
= enable
;
8855 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8857 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8859 if ( resizeExistingRows
)
8861 // since we are resizing all rows to the default row size,
8862 // we can simply clear the row heights and row bottoms
8863 // arrays (which also allows us to take advantage of
8864 // some speed optimisations)
8865 m_rowHeights
.Empty();
8866 m_rowBottoms
.Empty();
8867 if ( !GetBatchCount() )
8872 void wxGrid::SetRowSize( int row
, int height
)
8874 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8876 if ( m_rowHeights
.IsEmpty() )
8878 // need to really create the array
8882 int h
= wxMax( 0, height
);
8883 int diff
= h
- m_rowHeights
[row
];
8885 m_rowHeights
[row
] = h
;
8887 for ( i
= row
; i
< m_numRows
; i
++ )
8889 m_rowBottoms
[i
] += diff
;
8891 if ( !GetBatchCount() )
8895 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8897 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8899 if ( resizeExistingCols
)
8901 // since we are resizing all columns to the default column size,
8902 // we can simply clear the col widths and col rights
8903 // arrays (which also allows us to take advantage of
8904 // some speed optimisations)
8905 m_colWidths
.Empty();
8906 m_colRights
.Empty();
8907 if ( !GetBatchCount() )
8912 void wxGrid::SetColSize( int col
, int width
)
8914 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8916 // should we check that it's bigger than GetColMinimalWidth(col) here?
8918 if ( m_colWidths
.IsEmpty() )
8920 // need to really create the array
8924 int w
= wxMax( 0, width
);
8925 int diff
= w
- m_colWidths
[col
];
8926 m_colWidths
[col
] = w
;
8929 for ( i
= col
; i
< m_numCols
; i
++ )
8931 m_colRights
[i
] += diff
;
8933 if ( !GetBatchCount() )
8938 void wxGrid::SetColMinimalWidth( int col
, int width
)
8940 m_colMinWidths
.Put(col
, width
);
8943 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8945 m_rowMinHeights
.Put(row
, width
);
8948 int wxGrid::GetColMinimalWidth(int col
) const
8950 long value
= m_colMinWidths
.Get(col
);
8951 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8954 int wxGrid::GetRowMinimalHeight(int row
) const
8956 long value
= m_rowMinHeights
.Get(row
);
8957 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8960 // ----------------------------------------------------------------------------
8962 // ----------------------------------------------------------------------------
8964 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8966 wxClientDC
dc(m_gridWin
);
8968 // init both of them to avoid compiler warnings, even if weo nly need one
8976 wxCoord extent
, extentMax
= 0;
8977 int max
= column
? m_numRows
: m_numCols
;
8978 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8985 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8986 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8989 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8990 extent
= column
? size
.x
: size
.y
;
8991 if ( extent
> extentMax
)
9002 // now also compare with the column label extent
9004 dc
.SetFont( GetLabelFont() );
9007 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9009 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9011 extent
= column
? w
: h
;
9012 if ( extent
> extentMax
)
9019 // empty column - give default extent (notice that if extentMax is less
9020 // than default extent but != 0, it's ok)
9021 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9027 // leave some space around text
9038 SetColSize(col
, extentMax
);
9039 if ( !GetBatchCount() )
9042 m_gridWin
->GetClientSize( &cw
, &ch
);
9043 wxRect
rect ( CellToRect( 0, col
) );
9045 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9046 rect
.width
= cw
- rect
.x
;
9047 rect
.height
= m_colLabelHeight
;
9048 m_colLabelWin
->Refresh( TRUE
, &rect
);
9053 SetRowSize(row
, extentMax
);
9054 if ( !GetBatchCount() )
9057 m_gridWin
->GetClientSize( &cw
, &ch
);
9058 wxRect
rect ( CellToRect( row
, 0 ) );
9060 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9061 rect
.width
= m_rowLabelWidth
;
9062 rect
.height
= ch
- rect
.y
;
9063 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9069 SetColMinimalWidth(col
, extentMax
);
9071 SetRowMinimalHeight(row
, extentMax
);
9075 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9077 int width
= m_rowLabelWidth
;
9082 for ( int col
= 0; col
< m_numCols
; col
++ )
9086 AutoSizeColumn(col
, setAsMin
);
9089 width
+= GetColWidth(col
);
9098 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9100 int height
= m_colLabelHeight
;
9105 for ( int row
= 0; row
< m_numRows
; row
++ )
9109 AutoSizeRow(row
, setAsMin
);
9112 height
+= GetRowHeight(row
);
9121 void wxGrid::AutoSize()
9125 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9127 // round up the size to a multiple of scroll step - this ensures that we
9128 // won't get the scrollbars if we're sized exactly to this width
9129 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
9130 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
9132 // distribute the extra space between teh columns/rows to avoid having
9133 // extra white space
9134 wxCoord diff
= sizeFit
.x
- size
.x
;
9137 // try to resize the columns uniformly
9138 wxCoord diffPerCol
= diff
/ m_numCols
;
9141 for ( int col
= 0; col
< m_numCols
; col
++ )
9143 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9147 // add remaining amount to the last columns
9148 diff
-= diffPerCol
* m_numCols
;
9151 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9153 SetColSize(col
, GetColWidth(col
) + 1);
9159 diff
= sizeFit
.y
- size
.y
;
9162 // try to resize the columns uniformly
9163 wxCoord diffPerRow
= diff
/ m_numRows
;
9166 for ( int row
= 0; row
< m_numRows
; row
++ )
9168 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9172 // add remaining amount to the last rows
9173 diff
-= diffPerRow
* m_numRows
;
9176 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9178 SetRowSize(row
, GetRowHeight(row
) + 1);
9185 SetClientSize(sizeFit
);
9188 wxSize
wxGrid::DoGetBestSize() const
9190 // don't set sizes, only calculate them
9191 wxGrid
*self
= (wxGrid
*)this; // const_cast
9194 width
= self
->SetOrCalcColumnSizes(TRUE
);
9195 height
= self
->SetOrCalcRowSizes(TRUE
);
9197 int maxwidth
, maxheight
;
9198 wxDisplaySize( & maxwidth
, & maxheight
);
9200 if ( width
> maxwidth
) width
= maxwidth
;
9201 if ( height
> maxheight
) height
= maxheight
;
9203 return wxSize( width
, height
);
9212 wxPen
& wxGrid::GetDividerPen() const
9217 // ----------------------------------------------------------------------------
9218 // cell value accessor functions
9219 // ----------------------------------------------------------------------------
9221 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9225 m_table
->SetValue( row
, col
, s
);
9226 if ( !GetBatchCount() )
9229 wxRect
rect( CellToRect( row
, col
) );
9231 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9232 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9233 m_gridWin
->Refresh( FALSE
, &rect
);
9236 if ( m_currentCellCoords
.GetRow() == row
&&
9237 m_currentCellCoords
.GetCol() == col
&&
9238 IsCellEditControlShown())
9239 // Note: If we are using IsCellEditControlEnabled,
9240 // this interacts badly with calling SetCellValue from
9241 // an EVT_GRID_CELL_CHANGE handler.
9243 HideCellEditControl();
9244 ShowCellEditControl(); // will reread data from table
9251 // ------ Block, row and col selection
9254 void wxGrid::SelectRow( int row
, bool addToSelected
)
9256 if ( IsSelection() && !addToSelected
)
9260 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9264 void wxGrid::SelectCol( int col
, bool addToSelected
)
9266 if ( IsSelection() && !addToSelected
)
9270 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9274 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9275 bool addToSelected
)
9277 if ( IsSelection() && !addToSelected
)
9281 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9282 FALSE
, addToSelected
);
9286 void wxGrid::SelectAll()
9288 if ( m_numRows
> 0 && m_numCols
> 0 )
9291 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9296 // ------ Cell, row and col deselection
9299 void wxGrid::DeselectRow( int row
)
9304 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9306 if ( m_selection
->IsInSelection(row
, 0 ) )
9307 m_selection
->ToggleCellSelection( row
, 0);
9311 int nCols
= GetNumberCols();
9312 for ( int i
= 0; i
< nCols
; i
++ )
9314 if ( m_selection
->IsInSelection(row
, i
) )
9315 m_selection
->ToggleCellSelection( row
, i
);
9320 void wxGrid::DeselectCol( int col
)
9325 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9327 if ( m_selection
->IsInSelection(0, col
) )
9328 m_selection
->ToggleCellSelection( 0, col
);
9332 int nRows
= GetNumberRows();
9333 for ( int i
= 0; i
< nRows
; i
++ )
9335 if ( m_selection
->IsInSelection(i
, col
) )
9336 m_selection
->ToggleCellSelection(i
, col
);
9341 void wxGrid::DeselectCell( int row
, int col
)
9343 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9344 m_selection
->ToggleCellSelection(row
, col
);
9347 bool wxGrid::IsSelection()
9349 return ( m_selection
&& (m_selection
->IsSelection() ||
9350 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9351 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9354 bool wxGrid::IsInSelection( int row
, int col
) const
9356 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9357 ( row
>= m_selectingTopLeft
.GetRow() &&
9358 col
>= m_selectingTopLeft
.GetCol() &&
9359 row
<= m_selectingBottomRight
.GetRow() &&
9360 col
<= m_selectingBottomRight
.GetCol() )) );
9363 void wxGrid::ClearSelection()
9365 m_selectingTopLeft
= wxGridNoCellCoords
;
9366 m_selectingBottomRight
= wxGridNoCellCoords
;
9368 m_selection
->ClearSelection();
9372 // This function returns the rectangle that encloses the given block
9373 // in device coords clipped to the client size of the grid window.
9375 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9376 const wxGridCellCoords
&bottomRight
)
9378 wxRect
rect( wxGridNoCellRect
);
9381 cellRect
= CellToRect( topLeft
);
9382 if ( cellRect
!= wxGridNoCellRect
)
9388 rect
= wxRect( 0, 0, 0, 0 );
9391 cellRect
= CellToRect( bottomRight
);
9392 if ( cellRect
!= wxGridNoCellRect
)
9398 return wxGridNoCellRect
;
9402 int left
= rect
.GetLeft();
9403 int top
= rect
.GetTop();
9404 int right
= rect
.GetRight();
9405 int bottom
= rect
.GetBottom();
9407 int leftCol
= topLeft
.GetCol();
9408 int topRow
= topLeft
.GetRow();
9409 int rightCol
= bottomRight
.GetCol();
9410 int bottomRow
= bottomRight
.GetRow();
9412 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9414 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9416 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9418 cellRect
= CellToRect( j
, i
);
9420 if (cellRect
.x
< left
)
9422 if (cellRect
.y
< top
)
9424 if (cellRect
.x
+ cellRect
.width
> right
)
9425 right
= cellRect
.x
+ cellRect
.width
;
9426 if (cellRect
.y
+ cellRect
.height
> bottom
)
9427 bottom
= cellRect
.y
+ cellRect
.height
;
9432 // convert to scrolled coords
9434 CalcScrolledPosition( left
, top
, &left
, &top
);
9435 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9438 m_gridWin
->GetClientSize( &cw
, &ch
);
9440 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9441 return wxRect( 0, 0, 0, 0);
9443 rect
.SetLeft( wxMax(0, left
) );
9444 rect
.SetTop( wxMax(0, top
) );
9445 rect
.SetRight( wxMin(cw
, right
) );
9446 rect
.SetBottom( wxMin(ch
, bottom
) );
9454 // ------ Grid event classes
9457 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9459 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9460 int row
, int col
, int x
, int y
, bool sel
,
9461 bool control
, bool shift
, bool alt
, bool meta
)
9462 : wxNotifyEvent( type
, id
)
9469 m_control
= control
;
9474 SetEventObject(obj
);
9478 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9480 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9481 int rowOrCol
, int x
, int y
,
9482 bool control
, bool shift
, bool alt
, bool meta
)
9483 : wxNotifyEvent( type
, id
)
9485 m_rowOrCol
= rowOrCol
;
9488 m_control
= control
;
9493 SetEventObject(obj
);
9497 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9499 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9500 const wxGridCellCoords
& topLeft
,
9501 const wxGridCellCoords
& bottomRight
,
9502 bool sel
, bool control
,
9503 bool shift
, bool alt
, bool meta
)
9504 : wxNotifyEvent( type
, id
)
9506 m_topLeft
= topLeft
;
9507 m_bottomRight
= bottomRight
;
9509 m_control
= control
;
9514 SetEventObject(obj
);
9518 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9520 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9521 wxObject
* obj
, int row
,
9522 int col
, wxControl
* ctrl
)
9523 : wxCommandEvent(type
, id
)
9525 SetEventObject(obj
);
9532 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9534 #endif // wxUSE_GRID