1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
430 wxGridCellEditor::~wxGridCellEditor()
435 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
436 wxWindowID
WXUNUSED(id
),
437 wxEvtHandler
* evtHandler
)
440 m_control
->PushEventHandler(evtHandler
);
443 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
444 wxGridCellAttr
*attr
)
446 // erase the background because we might not fill the cell
447 wxClientDC
dc(m_control
->GetParent());
448 dc
.SetPen(*wxTRANSPARENT_PEN
);
449 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
450 dc
.DrawRectangle(rectCell
);
452 // redraw the control we just painted over
453 m_control
->Refresh();
456 void wxGridCellEditor::Destroy()
460 m_control
->PopEventHandler(TRUE
/* delete it*/);
462 m_control
->Destroy();
467 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
469 wxASSERT_MSG(m_control
,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control
->Show(show
);
475 // set the colours/fonts if we have any
478 m_colFgOld
= m_control
->GetForegroundColour();
479 m_control
->SetForegroundColour(attr
->GetTextColour());
481 m_colBgOld
= m_control
->GetBackgroundColour();
482 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
484 m_fontOld
= m_control
->GetFont();
485 m_control
->SetFont(attr
->GetFont());
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
493 // restore the standard colours fonts
494 if ( m_colFgOld
.Ok() )
496 m_control
->SetForegroundColour(m_colFgOld
);
497 m_colFgOld
= wxNullColour
;
500 if ( m_colBgOld
.Ok() )
502 m_control
->SetBackgroundColour(m_colBgOld
);
503 m_colBgOld
= wxNullColour
;
506 if ( m_fontOld
.Ok() )
508 m_control
->SetFont(m_fontOld
);
509 m_fontOld
= wxNullFont
;
514 void wxGridCellEditor::SetSize(const wxRect
& rect
)
516 wxASSERT_MSG(m_control
,
517 wxT("The wxGridCellEditor must be Created first!"));
518 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
521 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
526 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
529 return !(event
.ControlDown() || event
.AltDown());
532 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
537 void wxGridCellEditor::StartingClick()
543 // ----------------------------------------------------------------------------
544 // wxGridCellTextEditor
545 // ----------------------------------------------------------------------------
547 wxGridCellTextEditor::wxGridCellTextEditor()
552 void wxGridCellTextEditor::Create(wxWindow
* parent
,
554 wxEvtHandler
* evtHandler
)
556 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
557 wxDefaultPosition
, wxDefaultSize
558 #if defined(__WXMSW__)
559 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
560 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
564 // TODO: use m_maxChars
566 wxGridCellEditor::Create(parent
, id
, evtHandler
);
569 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
570 wxGridCellAttr
* WXUNUSED(attr
))
572 // as we fill the entire client area, don't do anything here to minimize
576 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
578 wxRect
rect(rectOrig
);
580 // Make the edit control large enough to allow for internal
583 // TODO: remove this if the text ctrl sizing is improved esp. for
586 #if defined(__WXGTK__)
595 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
597 // MB: treat MSW separately here otherwise the caret doesn't show
598 // when the editor is in the first row.
599 #if defined(__WXMSW__)
602 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
605 #if defined(__WXMOTIF__)
609 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
610 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
611 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
612 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
615 wxGridCellEditor::SetSize(rect
);
618 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
625 DoBeginEdit(m_startValue
);
628 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
630 Text()->SetValue(startValue
);
631 Text()->SetInsertionPointEnd();
632 Text()->SetSelection(-1,-1);
636 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
639 wxASSERT_MSG(m_control
,
640 wxT("The wxGridCellEditor must be Created first!"));
642 bool changed
= FALSE
;
643 wxString value
= Text()->GetValue();
644 if (value
!= m_startValue
)
648 grid
->GetTable()->SetValue(row
, col
, value
);
650 m_startValue
= wxEmptyString
;
651 Text()->SetValue(m_startValue
);
657 void wxGridCellTextEditor::Reset()
659 wxASSERT_MSG(m_control
,
660 wxT("The wxGridCellEditor must be Created first!"));
662 DoReset(m_startValue
);
665 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
667 Text()->SetValue(startValue
);
668 Text()->SetInsertionPointEnd();
671 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
673 if ( wxGridCellEditor::IsAcceptedKey(event
) )
675 int keycode
= event
.GetKeyCode();
689 case WXK_NUMPAD_MULTIPLY
:
693 case WXK_NUMPAD_SUBTRACT
:
695 case WXK_NUMPAD_DECIMAL
:
697 case WXK_NUMPAD_DIVIDE
:
701 // accept 8 bit chars too if isprint() agrees
702 if ( (keycode
< 255) && (isprint(keycode
)) )
710 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
712 if ( !Text()->EmulateKeyPress(event
) )
718 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
719 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
721 #if defined(__WXMOTIF__) || defined(__WXGTK__)
722 // wxMotif needs a little extra help...
723 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
724 wxString
s( Text()->GetValue() );
725 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
727 Text()->SetInsertionPoint( pos
);
729 // the other ports can handle a Return key press
735 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
745 if ( !params
.ToLong(&tmp
) )
747 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
751 m_maxChars
= (size_t)tmp
;
756 // ----------------------------------------------------------------------------
757 // wxGridCellNumberEditor
758 // ----------------------------------------------------------------------------
760 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
766 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
768 wxEvtHandler
* evtHandler
)
772 // create a spin ctrl
773 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
774 wxDefaultPosition
, wxDefaultSize
,
778 wxGridCellEditor::Create(parent
, id
, evtHandler
);
782 // just a text control
783 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
786 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
787 #endif // wxUSE_VALIDATORS
791 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
793 // first get the value
794 wxGridTableBase
*table
= grid
->GetTable();
795 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
797 m_valueOld
= table
->GetValueAsLong(row
, col
);
802 wxString sValue
= table
->GetValue(row
, col
);
803 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
805 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
812 Spin()->SetValue((int)m_valueOld
);
817 DoBeginEdit(GetString());
821 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
830 value
= Spin()->GetValue();
831 changed
= value
!= m_valueOld
;
833 text
= wxString::Format(wxT("%ld"), value
);
837 text
= Text()->GetValue();
838 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
843 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
844 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
846 grid
->GetTable()->SetValue(row
, col
, text
);
852 void wxGridCellNumberEditor::Reset()
856 Spin()->SetValue((int)m_valueOld
);
860 DoReset(GetString());
864 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
866 if ( wxGridCellEditor::IsAcceptedKey(event
) )
868 int keycode
= event
.GetKeyCode();
884 case WXK_NUMPAD_SUBTRACT
:
890 if ( (keycode
< 128) && isdigit(keycode
) )
898 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
902 int keycode
= (int) event
.KeyCode();
903 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-'
904 || keycode
== WXK_NUMPAD0
905 || keycode
== WXK_NUMPAD1
906 || keycode
== WXK_NUMPAD2
907 || keycode
== WXK_NUMPAD3
908 || keycode
== WXK_NUMPAD4
909 || keycode
== WXK_NUMPAD5
910 || keycode
== WXK_NUMPAD6
911 || keycode
== WXK_NUMPAD7
912 || keycode
== WXK_NUMPAD8
913 || keycode
== WXK_NUMPAD9
914 || keycode
== WXK_ADD
915 || keycode
== WXK_NUMPAD_ADD
916 || keycode
== WXK_SUBTRACT
917 || keycode
== WXK_NUMPAD_SUBTRACT
)
919 wxGridCellTextEditor::StartingKey(event
);
929 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
940 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
944 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
948 // skip the error message below
953 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
957 // ----------------------------------------------------------------------------
958 // wxGridCellFloatEditor
959 // ----------------------------------------------------------------------------
961 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
964 m_precision
= precision
;
967 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
969 wxEvtHandler
* evtHandler
)
971 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
974 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
975 #endif // wxUSE_VALIDATORS
978 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
980 // first get the value
981 wxGridTableBase
*table
= grid
->GetTable();
982 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
984 m_valueOld
= table
->GetValueAsDouble(row
, col
);
989 wxString sValue
= table
->GetValue(row
, col
);
990 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
992 wxFAIL_MSG( _T("this cell doesn't have float value") );
997 DoBeginEdit(GetString());
1000 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1004 wxString
text(Text()->GetValue());
1006 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1008 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1009 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1011 grid
->GetTable()->SetValue(row
, col
, text
);
1018 void wxGridCellFloatEditor::Reset()
1020 DoReset(GetString());
1023 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1025 int keycode
= (int)event
.KeyCode();
1026 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' || keycode
== '.'
1027 || keycode
== WXK_NUMPAD0
1028 || keycode
== WXK_NUMPAD1
1029 || keycode
== WXK_NUMPAD2
1030 || keycode
== WXK_NUMPAD3
1031 || keycode
== WXK_NUMPAD4
1032 || keycode
== WXK_NUMPAD5
1033 || keycode
== WXK_NUMPAD6
1034 || keycode
== WXK_NUMPAD7
1035 || keycode
== WXK_NUMPAD8
1036 || keycode
== WXK_NUMPAD9
1037 || keycode
== WXK_ADD
1038 || keycode
== WXK_NUMPAD_ADD
1039 || keycode
== WXK_SUBTRACT
1040 || keycode
== WXK_NUMPAD_SUBTRACT
)
1042 wxGridCellTextEditor::StartingKey(event
);
1044 // skip Skip() below
1051 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1062 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1066 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1068 m_precision
= (int)tmp
;
1070 // skip the error message below
1075 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1079 wxString
wxGridCellFloatEditor::GetString() const
1082 if ( m_width
== -1 )
1084 // default width/precision
1087 else if ( m_precision
== -1 )
1089 // default precision
1090 fmt
.Printf(_T("%%%d.f"), m_width
);
1094 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1097 return wxString::Format(fmt
, m_valueOld
);
1100 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1102 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1104 int keycode
= event
.GetKeyCode();
1118 case WXK_NUMPAD_ADD
:
1120 case WXK_NUMPAD_SUBTRACT
:
1122 case WXK_NUMPAD_DECIMAL
:
1126 // additionally accept 'e' as in '1e+6'
1127 if ( (keycode
< 128) &&
1128 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1136 #endif // wxUSE_TEXTCTRL
1140 // ----------------------------------------------------------------------------
1141 // wxGridCellBoolEditor
1142 // ----------------------------------------------------------------------------
1144 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1146 wxEvtHandler
* evtHandler
)
1148 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1149 wxDefaultPosition
, wxDefaultSize
,
1152 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1155 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1157 bool resize
= FALSE
;
1158 wxSize size
= m_control
->GetSize();
1159 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1161 // check if the checkbox is not too big/small for this cell
1162 wxSize sizeBest
= m_control
->GetBestSize();
1163 if ( !(size
== sizeBest
) )
1165 // reset to default size if it had been made smaller
1171 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1173 // leave 1 pixel margin
1174 size
.x
= size
.y
= minSize
- 2;
1181 m_control
->SetSize(size
);
1184 // position it in the centre of the rectangle (TODO: support alignment?)
1186 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1187 // the checkbox without label still has some space to the right in wxGTK,
1188 // so shift it to the right
1190 #elif defined(__WXMSW__)
1191 // here too, but in other way
1196 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1199 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1201 m_control
->Show(show
);
1205 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1206 CBox()->SetBackgroundColour(colBg
);
1210 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1212 wxASSERT_MSG(m_control
,
1213 wxT("The wxGridCellEditor must be Created first!"));
1215 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1216 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1219 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1220 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1222 CBox()->SetValue(m_startValue
);
1226 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1229 wxASSERT_MSG(m_control
,
1230 wxT("The wxGridCellEditor must be Created first!"));
1232 bool changed
= FALSE
;
1233 bool value
= CBox()->GetValue();
1234 if ( value
!= m_startValue
)
1239 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1240 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1242 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1248 void wxGridCellBoolEditor::Reset()
1250 wxASSERT_MSG(m_control
,
1251 wxT("The wxGridCellEditor must be Created first!"));
1253 CBox()->SetValue(m_startValue
);
1256 void wxGridCellBoolEditor::StartingClick()
1258 CBox()->SetValue(!CBox()->GetValue());
1261 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1263 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1265 int keycode
= event
.GetKeyCode();
1269 case WXK_NUMPAD_MULTIPLY
:
1271 case WXK_NUMPAD_ADD
:
1273 case WXK_NUMPAD_SUBTRACT
:
1284 #endif // wxUSE_CHECKBOX
1288 // ----------------------------------------------------------------------------
1289 // wxGridCellChoiceEditor
1290 // ----------------------------------------------------------------------------
1292 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1293 const wxString choices
[],
1295 : m_allowOthers(allowOthers
)
1299 m_choices
.Alloc(count
);
1300 for ( size_t n
= 0; n
< count
; n
++ )
1302 m_choices
.Add(choices
[n
]);
1307 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1309 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1310 editor
->m_allowOthers
= m_allowOthers
;
1311 editor
->m_choices
= m_choices
;
1316 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1318 wxEvtHandler
* evtHandler
)
1320 size_t count
= m_choices
.GetCount();
1321 wxString
*choices
= new wxString
[count
];
1322 for ( size_t n
= 0; n
< count
; n
++ )
1324 choices
[n
] = m_choices
[n
];
1327 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1328 wxDefaultPosition
, wxDefaultSize
,
1330 m_allowOthers
? 0 : wxCB_READONLY
);
1334 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1337 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1338 wxGridCellAttr
* attr
)
1340 // as we fill the entire client area, don't do anything here to minimize
1343 // TODO: It doesn't actually fill the client area since the height of a
1344 // combo always defaults to the standard... Until someone has time to
1345 // figure out the right rectangle to paint, just do it the normal way...
1346 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1349 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1351 wxASSERT_MSG(m_control
,
1352 wxT("The wxGridCellEditor must be Created first!"));
1354 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1356 Combo()->SetValue(m_startValue
);
1357 size_t count
= m_choices
.GetCount();
1358 for (size_t i
=0; i
<count
; i
++)
1360 if (m_startValue
== m_choices
[i
])
1362 Combo()->SetSelection(i
);
1366 Combo()->SetInsertionPointEnd();
1367 Combo()->SetFocus();
1370 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1373 wxString value
= Combo()->GetValue();
1374 bool changed
= value
!= m_startValue
;
1377 grid
->GetTable()->SetValue(row
, col
, value
);
1379 m_startValue
= wxEmptyString
;
1380 Combo()->SetValue(m_startValue
);
1385 void wxGridCellChoiceEditor::Reset()
1387 Combo()->SetValue(m_startValue
);
1388 Combo()->SetInsertionPointEnd();
1391 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1401 wxStringTokenizer
tk(params
, _T(','));
1402 while ( tk
.HasMoreTokens() )
1404 m_choices
.Add(tk
.GetNextToken());
1408 #endif // wxUSE_COMBOBOX
1410 // ----------------------------------------------------------------------------
1411 // wxGridCellEditorEvtHandler
1412 // ----------------------------------------------------------------------------
1414 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1416 switch ( event
.KeyCode() )
1420 m_grid
->DisableCellEditControl();
1424 m_grid
->GetEventHandler()->ProcessEvent( event
);
1428 case WXK_NUMPAD_ENTER
:
1429 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1430 m_editor
->HandleReturn(event
);
1439 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1441 switch ( event
.KeyCode() )
1446 case WXK_NUMPAD_ENTER
:
1454 // ----------------------------------------------------------------------------
1455 // wxGridCellWorker is an (almost) empty common base class for
1456 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1457 // ----------------------------------------------------------------------------
1459 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1464 wxGridCellWorker::~wxGridCellWorker()
1468 // ============================================================================
1470 // ============================================================================
1472 // ----------------------------------------------------------------------------
1473 // wxGridCellRenderer
1474 // ----------------------------------------------------------------------------
1476 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1477 wxGridCellAttr
& attr
,
1480 int WXUNUSED(row
), int WXUNUSED(col
),
1483 dc
.SetBackgroundMode( wxSOLID
);
1487 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1491 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1494 dc
.SetPen( *wxTRANSPARENT_PEN
);
1495 dc
.DrawRectangle(rect
);
1498 // ----------------------------------------------------------------------------
1499 // wxGridCellStringRenderer
1500 // ----------------------------------------------------------------------------
1502 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1503 wxGridCellAttr
& attr
,
1507 dc
.SetBackgroundMode( wxTRANSPARENT
);
1509 // TODO some special colours for attr.IsReadOnly() case?
1513 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1514 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1518 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1519 dc
.SetTextForeground( attr
.GetTextColour() );
1522 dc
.SetFont( attr
.GetFont() );
1525 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1527 const wxString
& text
)
1529 wxCoord x
= 0, y
= 0, max_x
= 0;
1530 dc
.SetFont(attr
.GetFont());
1531 wxStringTokenizer
tk(text
, _T('\n'));
1532 while ( tk
.HasMoreTokens() )
1534 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1535 max_x
= wxMax(max_x
, x
);
1538 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1540 return wxSize(max_x
, y
);
1543 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1544 wxGridCellAttr
& attr
,
1548 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1551 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1552 wxGridCellAttr
& attr
,
1554 const wxRect
& rectCell
,
1558 wxRect rect
= rectCell
;
1561 // erase only this cells background, overflow cells should have been erased
1562 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1565 attr
.GetAlignment(&hAlign
, &vAlign
);
1567 if (attr
.GetOverflow())
1569 int cols
= grid
.GetNumberCols();
1570 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1571 int overflowCols
= 0;
1572 int cell_rows
, cell_cols
;
1573 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1574 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1576 int i
, c_cols
, c_rows
;
1577 for (i
= col
+cell_cols
; i
< cols
; i
++)
1579 // check w/ anchor cell for multicell block
1580 grid
.GetCellSize(row
, i
, &c_rows
, &c_cols
);
1581 if (c_rows
> 0) c_rows
= 0;
1582 if (grid
.GetTable()->IsEmptyCell(row
+c_rows
, i
))
1584 rect
.width
+= grid
.GetColSize(i
);
1585 if (rect
.width
>= best_width
) break;
1593 overflowCols
= i
- col
- cell_cols
;
1594 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1597 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1601 // draw each overflow cell individually
1602 int col_end
= col
+cell_cols
+overflowCols
;
1603 if (col_end
>= grid
.GetNumberCols())
1604 col_end
= grid
.GetNumberCols() - 1;
1606 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1608 clip
.x
+= grid
.GetColSize(i
-1) - 1;
1609 clip
.width
= grid
.GetColSize(i
) - 1;
1610 dc
.DestroyClippingRegion();
1611 dc
.SetClippingRegion(clip
);
1613 SetTextColoursAndFont(grid
, attr
, dc
, grid
.IsInSelection(row
,i
));
1614 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1615 rect
, hAlign
, vAlign
);
1621 dc
.DestroyClippingRegion(); // DrawTextRectangle sets it again
1625 // now we only have to draw the text
1626 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1627 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1628 rect
, hAlign
, vAlign
);
1631 // ----------------------------------------------------------------------------
1632 // wxGridCellNumberRenderer
1633 // ----------------------------------------------------------------------------
1635 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1637 wxGridTableBase
*table
= grid
.GetTable();
1639 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1641 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1645 text
= table
->GetValue(row
, col
);
1651 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1652 wxGridCellAttr
& attr
,
1654 const wxRect
& rectCell
,
1658 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1660 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1662 // draw the text right aligned by default
1664 attr
.GetAlignment(&hAlign
, &vAlign
);
1665 hAlign
= wxALIGN_RIGHT
;
1667 wxRect rect
= rectCell
;
1670 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1673 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1674 wxGridCellAttr
& attr
,
1678 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1681 // ----------------------------------------------------------------------------
1682 // wxGridCellFloatRenderer
1683 // ----------------------------------------------------------------------------
1685 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1688 SetPrecision(precision
);
1691 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1693 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1694 renderer
->m_width
= m_width
;
1695 renderer
->m_precision
= m_precision
;
1696 renderer
->m_format
= m_format
;
1701 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1703 wxGridTableBase
*table
= grid
.GetTable();
1708 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1710 val
= table
->GetValueAsDouble(row
, col
);
1715 text
= table
->GetValue(row
, col
);
1716 hasDouble
= text
.ToDouble(&val
);
1723 if ( m_width
== -1 )
1725 if ( m_precision
== -1 )
1727 // default width/precision
1728 m_format
= _T("%f");
1732 m_format
.Printf(_T("%%.%df"), m_precision
);
1735 else if ( m_precision
== -1 )
1737 // default precision
1738 m_format
.Printf(_T("%%%d.f"), m_width
);
1742 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1746 text
.Printf(m_format
, val
);
1749 //else: text already contains the string
1754 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1755 wxGridCellAttr
& attr
,
1757 const wxRect
& rectCell
,
1761 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1763 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1765 // draw the text right aligned by default
1767 attr
.GetAlignment(&hAlign
, &vAlign
);
1768 hAlign
= wxALIGN_RIGHT
;
1770 wxRect rect
= rectCell
;
1773 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1776 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1777 wxGridCellAttr
& attr
,
1781 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1784 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1788 // reset to defaults
1794 wxString tmp
= params
.BeforeFirst(_T(','));
1798 if ( tmp
.ToLong(&width
) )
1800 SetWidth((int)width
);
1804 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1808 tmp
= params
.AfterFirst(_T(','));
1812 if ( tmp
.ToLong(&precision
) )
1814 SetPrecision((int)precision
);
1818 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1826 // ----------------------------------------------------------------------------
1827 // wxGridCellBoolRenderer
1828 // ----------------------------------------------------------------------------
1830 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1832 // FIXME these checkbox size calculations are really ugly...
1834 // between checkmark and box
1835 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1837 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1838 wxGridCellAttr
& WXUNUSED(attr
),
1843 // compute it only once (no locks for MT safeness in GUI thread...)
1844 if ( !ms_sizeCheckMark
.x
)
1846 // get checkbox size
1847 wxCoord checkSize
= 0;
1848 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1849 wxSize size
= checkbox
->GetBestSize();
1850 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1852 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1853 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1854 checkSize
-= size
.y
/ 2;
1859 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1862 return ms_sizeCheckMark
;
1865 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1866 wxGridCellAttr
& attr
,
1872 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1874 // draw a check mark in the centre (ignoring alignment - TODO)
1875 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1877 // don't draw outside the cell
1878 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1879 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1881 // and even leave (at least) 1 pixel margin
1882 size
.x
= size
.y
= minSize
- 2;
1885 // draw a border around checkmark
1887 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1888 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1889 rectBorder
.width
= size
.x
;
1890 rectBorder
.height
= size
.y
;
1893 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1894 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1897 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1898 value
= !( !cellval
|| (cellval
== wxT("0")) );
1903 wxRect rectMark
= rectBorder
;
1905 // MSW DrawCheckMark() is weird (and should probably be changed...)
1906 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1910 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1913 dc
.SetTextForeground(attr
.GetTextColour());
1914 dc
.DrawCheckMark(rectMark
);
1917 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1918 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1919 dc
.DrawRectangle(rectBorder
);
1922 // ----------------------------------------------------------------------------
1924 // ----------------------------------------------------------------------------
1926 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1930 m_isReadOnly
= Unset
;
1935 m_attrkind
= wxGridCellAttr::Cell
;
1937 m_sizeRows
= m_sizeCols
= 1;
1940 SetDefAttr(attrDefault
);
1943 wxGridCellAttr
*wxGridCellAttr::Clone() const
1945 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1947 if ( HasTextColour() )
1948 attr
->SetTextColour(GetTextColour());
1949 if ( HasBackgroundColour() )
1950 attr
->SetBackgroundColour(GetBackgroundColour());
1952 attr
->SetFont(GetFont());
1953 if ( HasAlignment() )
1954 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1956 attr
->SetSize( m_sizeRows
, m_sizeCols
);
1960 attr
->SetRenderer(m_renderer
);
1961 m_renderer
->IncRef();
1965 attr
->SetEditor(m_editor
);
1970 attr
->SetReadOnly();
1972 attr
->SetKind( m_attrkind
);
1977 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1979 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1980 SetTextColour(mergefrom
->GetTextColour());
1981 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1982 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1983 if ( !HasFont() && mergefrom
->HasFont() )
1984 SetFont(mergefrom
->GetFont());
1985 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
1987 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1988 SetAlignment(hAlign
, vAlign
);
1991 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
1993 // Directly access member functions as GetRender/Editor don't just return
1994 // m_renderer/m_editor
1996 // Maybe add support for merge of Render and Editor?
1997 if (!HasRenderer() && mergefrom
->HasRenderer() )
1999 m_renderer
= mergefrom
->m_renderer
;
2000 m_renderer
->IncRef();
2002 if ( !HasEditor() && mergefrom
->HasEditor() )
2004 m_editor
= mergefrom
->m_editor
;
2007 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2008 SetReadOnly(mergefrom
->IsReadOnly());
2010 SetDefAttr(mergefrom
->m_defGridAttr
);
2013 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2015 // The size of a cell is normally 1,1
2017 // If this cell is larger (2,2) then this is the top left cell
2018 // the other cells that will be covered (lower right cells) must be
2019 // set to negative or zero values such that
2020 // row + num_rows of the covered cell points to the larger cell (this cell)
2021 // same goes for the col + num_cols.
2023 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2025 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2026 !((num_rows
<=0)&&(num_cols
>0)) ||
2027 !((num_rows
==0)&&(num_cols
==0))),
2028 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2030 m_sizeRows
= num_rows
;
2031 m_sizeCols
= num_cols
;
2034 const wxColour
& wxGridCellAttr::GetTextColour() const
2036 if (HasTextColour())
2040 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2042 return m_defGridAttr
->GetTextColour();
2046 wxFAIL_MSG(wxT("Missing default cell attribute"));
2047 return wxNullColour
;
2052 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2054 if (HasBackgroundColour())
2056 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2057 return m_defGridAttr
->GetBackgroundColour();
2060 wxFAIL_MSG(wxT("Missing default cell attribute"));
2061 return wxNullColour
;
2066 const wxFont
& wxGridCellAttr::GetFont() const
2070 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2071 return m_defGridAttr
->GetFont();
2074 wxFAIL_MSG(wxT("Missing default cell attribute"));
2080 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2084 if ( hAlign
) *hAlign
= m_hAlign
;
2085 if ( vAlign
) *vAlign
= m_vAlign
;
2087 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2088 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2091 wxFAIL_MSG(wxT("Missing default cell attribute"));
2095 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2097 if ( num_rows
) *num_rows
= m_sizeRows
;
2098 if ( num_cols
) *num_cols
= m_sizeCols
;
2101 // GetRenderer and GetEditor use a slightly different decision path about
2102 // which attribute to use. If a non-default attr object has one then it is
2103 // used, otherwise the default editor or renderer is fetched from the grid and
2104 // used. It should be the default for the data type of the cell. If it is
2105 // NULL (because the table has a type that the grid does not have in its
2106 // registry,) then the grid's default editor or renderer is used.
2108 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2110 wxGridCellRenderer
*renderer
;
2112 if ( m_renderer
&& this != m_defGridAttr
)
2114 // use the cells renderer if it has one
2115 renderer
= m_renderer
;
2118 else // no non default cell renderer
2120 // get default renderer for the data type
2123 // GetDefaultRendererForCell() will do IncRef() for us
2124 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2133 if (m_defGridAttr
&& this != m_defGridAttr
)
2135 // if we still don't have one then use the grid default
2136 // (no need for IncRef() here neither)
2137 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2139 else // default grid attr
2141 // use m_renderer which we had decided not to use initially
2142 renderer
= m_renderer
;
2149 // we're supposed to always find something
2150 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2155 // same as above, except for s/renderer/editor/g
2156 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2158 wxGridCellEditor
*editor
;
2160 if ( m_editor
&& this != m_defGridAttr
)
2162 // use the cells editor if it has one
2166 else // no non default cell editor
2168 // get default editor for the data type
2171 // GetDefaultEditorForCell() will do IncRef() for us
2172 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2181 if ( m_defGridAttr
&& this != m_defGridAttr
)
2183 // if we still don't have one then use the grid default
2184 // (no need for IncRef() here neither)
2185 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2187 else // default grid attr
2189 // use m_editor which we had decided not to use initially
2197 // we're supposed to always find something
2198 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2203 // ----------------------------------------------------------------------------
2204 // wxGridCellAttrData
2205 // ----------------------------------------------------------------------------
2207 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2209 int n
= FindIndex(row
, col
);
2210 if ( n
== wxNOT_FOUND
)
2212 // add the attribute
2213 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2217 // free the old attribute
2218 m_attrs
[(size_t)n
].attr
->DecRef();
2222 // change the attribute
2223 m_attrs
[(size_t)n
].attr
= attr
;
2227 // remove this attribute
2228 m_attrs
.RemoveAt((size_t)n
);
2233 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2235 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2237 int n
= FindIndex(row
, col
);
2238 if ( n
!= wxNOT_FOUND
)
2240 attr
= m_attrs
[(size_t)n
].attr
;
2247 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2249 size_t count
= m_attrs
.GetCount();
2250 for ( size_t n
= 0; n
< count
; n
++ )
2252 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2253 wxCoord row
= coords
.GetRow();
2254 if ((size_t)row
>= pos
)
2258 // If rows inserted, include row counter where necessary
2259 coords
.SetRow(row
+ numRows
);
2261 else if (numRows
< 0)
2263 // If rows deleted ...
2264 if ((size_t)row
>= pos
- numRows
)
2266 // ...either decrement row counter (if row still exists)...
2267 coords
.SetRow(row
+ numRows
);
2271 // ...or remove the attribute
2272 m_attrs
.RemoveAt((size_t)n
);
2280 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2282 size_t count
= m_attrs
.GetCount();
2283 for ( size_t n
= 0; n
< count
; n
++ )
2285 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2286 wxCoord col
= coords
.GetCol();
2287 if ( (size_t)col
>= pos
)
2291 // If rows inserted, include row counter where necessary
2292 coords
.SetCol(col
+ numCols
);
2294 else if (numCols
< 0)
2296 // If rows deleted ...
2297 if ((size_t)col
>= pos
- numCols
)
2299 // ...either decrement row counter (if row still exists)...
2300 coords
.SetCol(col
+ numCols
);
2304 // ...or remove the attribute
2305 m_attrs
.RemoveAt((size_t)n
);
2313 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2315 size_t count
= m_attrs
.GetCount();
2316 for ( size_t n
= 0; n
< count
; n
++ )
2318 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2319 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2328 // ----------------------------------------------------------------------------
2329 // wxGridRowOrColAttrData
2330 // ----------------------------------------------------------------------------
2332 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2334 size_t count
= m_attrs
.Count();
2335 for ( size_t n
= 0; n
< count
; n
++ )
2337 m_attrs
[n
]->DecRef();
2341 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2343 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2345 int n
= m_rowsOrCols
.Index(rowOrCol
);
2346 if ( n
!= wxNOT_FOUND
)
2348 attr
= m_attrs
[(size_t)n
];
2355 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2357 int i
= m_rowsOrCols
.Index(rowOrCol
);
2358 if ( i
== wxNOT_FOUND
)
2360 // add the attribute
2361 m_rowsOrCols
.Add(rowOrCol
);
2366 size_t n
= (size_t)i
;
2369 // change the attribute
2370 m_attrs
[n
]->DecRef();
2375 // remove this attribute
2376 m_attrs
[n
]->DecRef();
2377 m_rowsOrCols
.RemoveAt(n
);
2378 m_attrs
.RemoveAt(n
);
2383 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2385 size_t count
= m_attrs
.GetCount();
2386 for ( size_t n
= 0; n
< count
; n
++ )
2388 int & rowOrCol
= m_rowsOrCols
[n
];
2389 if ( (size_t)rowOrCol
>= pos
)
2391 if ( numRowsOrCols
> 0 )
2393 // If rows inserted, include row counter where necessary
2394 rowOrCol
+= numRowsOrCols
;
2396 else if ( numRowsOrCols
< 0)
2398 // If rows deleted, either decrement row counter (if row still exists)
2399 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2400 rowOrCol
+= numRowsOrCols
;
2403 m_rowsOrCols
.RemoveAt((size_t)n
);
2404 m_attrs
.RemoveAt((size_t)n
);
2412 // ----------------------------------------------------------------------------
2413 // wxGridCellAttrProvider
2414 // ----------------------------------------------------------------------------
2416 wxGridCellAttrProvider::wxGridCellAttrProvider()
2418 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2421 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2426 void wxGridCellAttrProvider::InitData()
2428 m_data
= new wxGridCellAttrProviderData
;
2431 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2432 wxGridCellAttr::wxAttrKind kind
) const
2434 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2439 case (wxGridCellAttr::Any
):
2440 //Get cached merge attributes.
2441 // Currenlty not used as no cache implemented as not mutiable
2442 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2445 //Basicaly implement old version.
2446 //Also check merge cache, so we don't have to re-merge every time..
2447 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2448 *attrrow
= (wxGridCellAttr
*)NULL
,
2449 *attrcol
= (wxGridCellAttr
*)NULL
;
2451 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2452 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2453 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2455 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2456 // Two or move are non NULL
2457 attr
= new wxGridCellAttr
;
2458 attr
->SetKind(wxGridCellAttr::Merged
);
2462 attr
->MergeWith(attrcell
);
2466 attr
->MergeWith(attrcol
);
2470 attr
->MergeWith(attrrow
);
2473 //store merge attr if cache implemented
2475 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2479 // one or none is non null return it or null.
2480 if(attrrow
) attr
= attrrow
;
2481 if(attrcol
) attr
= attrcol
;
2482 if(attrcell
) attr
= attrcell
;
2486 case (wxGridCellAttr::Cell
):
2487 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2489 case (wxGridCellAttr::Col
):
2490 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2492 case (wxGridCellAttr::Row
):
2493 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2497 // (wxGridCellAttr::Default):
2498 // (wxGridCellAttr::Merged):
2505 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2511 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2514 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2519 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2522 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2527 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2530 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2534 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2536 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2540 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2544 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2546 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2550 // ----------------------------------------------------------------------------
2551 // wxGridTypeRegistry
2552 // ----------------------------------------------------------------------------
2554 wxGridTypeRegistry::~wxGridTypeRegistry()
2556 size_t count
= m_typeinfo
.Count();
2557 for ( size_t i
= 0; i
< count
; i
++ )
2558 delete m_typeinfo
[i
];
2562 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2563 wxGridCellRenderer
* renderer
,
2564 wxGridCellEditor
* editor
)
2566 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2568 // is it already registered?
2569 int loc
= FindRegisteredDataType(typeName
);
2570 if ( loc
!= wxNOT_FOUND
)
2572 delete m_typeinfo
[loc
];
2573 m_typeinfo
[loc
] = info
;
2577 m_typeinfo
.Add(info
);
2581 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2583 size_t count
= m_typeinfo
.GetCount();
2584 for ( size_t i
= 0; i
< count
; i
++ )
2586 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2595 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2597 int index
= FindRegisteredDataType(typeName
);
2598 if ( index
== wxNOT_FOUND
)
2600 // check whether this is one of the standard ones, in which case
2601 // register it "on the fly"
2603 if ( typeName
== wxGRID_VALUE_STRING
)
2605 RegisterDataType(wxGRID_VALUE_STRING
,
2606 new wxGridCellStringRenderer
,
2607 new wxGridCellTextEditor
);
2609 #endif // wxUSE_TEXTCTRL
2611 if ( typeName
== wxGRID_VALUE_BOOL
)
2613 RegisterDataType(wxGRID_VALUE_BOOL
,
2614 new wxGridCellBoolRenderer
,
2615 new wxGridCellBoolEditor
);
2617 #endif // wxUSE_CHECKBOX
2619 if ( typeName
== wxGRID_VALUE_NUMBER
)
2621 RegisterDataType(wxGRID_VALUE_NUMBER
,
2622 new wxGridCellNumberRenderer
,
2623 new wxGridCellNumberEditor
);
2625 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2627 RegisterDataType(wxGRID_VALUE_FLOAT
,
2628 new wxGridCellFloatRenderer
,
2629 new wxGridCellFloatEditor
);
2631 #endif // wxUSE_TEXTCTRL
2633 if ( typeName
== wxGRID_VALUE_CHOICE
)
2635 RegisterDataType(wxGRID_VALUE_CHOICE
,
2636 new wxGridCellStringRenderer
,
2637 new wxGridCellChoiceEditor
);
2639 #endif // wxUSE_COMBOBOX
2644 // we get here only if just added the entry for this type, so return
2646 index
= m_typeinfo
.GetCount() - 1;
2652 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2654 int index
= FindDataType(typeName
);
2655 if ( index
== wxNOT_FOUND
)
2657 // the first part of the typename is the "real" type, anything after ':'
2658 // are the parameters for the renderer
2659 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2660 if ( index
== wxNOT_FOUND
)
2665 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2666 wxGridCellRenderer
*rendererOld
= renderer
;
2667 renderer
= renderer
->Clone();
2668 rendererOld
->DecRef();
2670 wxGridCellEditor
*editor
= GetEditor(index
);
2671 wxGridCellEditor
*editorOld
= editor
;
2672 editor
= editor
->Clone();
2673 editorOld
->DecRef();
2675 // do it even if there are no parameters to reset them to defaults
2676 wxString params
= typeName
.AfterFirst(_T(':'));
2677 renderer
->SetParameters(params
);
2678 editor
->SetParameters(params
);
2680 // register the new typename
2681 RegisterDataType(typeName
, renderer
, editor
);
2683 // we just registered it, it's the last one
2684 index
= m_typeinfo
.GetCount() - 1;
2690 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2692 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2698 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2700 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2706 // ----------------------------------------------------------------------------
2708 // ----------------------------------------------------------------------------
2710 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2713 wxGridTableBase::wxGridTableBase()
2715 m_view
= (wxGrid
*) NULL
;
2716 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2719 wxGridTableBase::~wxGridTableBase()
2721 delete m_attrProvider
;
2724 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2726 delete m_attrProvider
;
2727 m_attrProvider
= attrProvider
;
2730 bool wxGridTableBase::CanHaveAttributes()
2732 if ( ! GetAttrProvider() )
2734 // use the default attr provider by default
2735 SetAttrProvider(new wxGridCellAttrProvider
);
2740 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2742 if ( m_attrProvider
)
2743 return m_attrProvider
->GetAttr(row
, col
, kind
);
2745 return (wxGridCellAttr
*)NULL
;
2748 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2750 if ( m_attrProvider
)
2752 attr
->SetKind(wxGridCellAttr::Cell
);
2753 m_attrProvider
->SetAttr(attr
, row
, col
);
2757 // as we take ownership of the pointer and don't store it, we must
2763 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2765 if ( m_attrProvider
)
2767 attr
->SetKind(wxGridCellAttr::Row
);
2768 m_attrProvider
->SetRowAttr(attr
, row
);
2772 // as we take ownership of the pointer and don't store it, we must
2778 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2780 if ( m_attrProvider
)
2782 attr
->SetKind(wxGridCellAttr::Col
);
2783 m_attrProvider
->SetColAttr(attr
, col
);
2787 // as we take ownership of the pointer and don't store it, we must
2793 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2794 size_t WXUNUSED(numRows
) )
2796 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2801 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2803 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2808 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2809 size_t WXUNUSED(numRows
) )
2811 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2816 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2817 size_t WXUNUSED(numCols
) )
2819 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2824 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2826 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2831 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2832 size_t WXUNUSED(numCols
) )
2834 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2840 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2843 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2844 // how much it makes sense to us geeks.
2848 wxString
wxGridTableBase::GetColLabelValue( int col
)
2850 // default col labels are:
2851 // cols 0 to 25 : A-Z
2852 // cols 26 to 675 : AA-ZZ
2857 for ( n
= 1; ; n
++ )
2859 s
+= (_T('A') + (wxChar
)( col%26
));
2861 if ( col
< 0 ) break;
2864 // reverse the string...
2866 for ( i
= 0; i
< n
; i
++ )
2875 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2877 return wxGRID_VALUE_STRING
;
2880 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2881 const wxString
& typeName
)
2883 return typeName
== wxGRID_VALUE_STRING
;
2886 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2888 return CanGetValueAs(row
, col
, typeName
);
2891 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2896 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2901 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2906 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2907 long WXUNUSED(value
) )
2911 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2912 double WXUNUSED(value
) )
2916 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2917 bool WXUNUSED(value
) )
2922 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2923 const wxString
& WXUNUSED(typeName
) )
2928 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2929 const wxString
& WXUNUSED(typeName
),
2930 void* WXUNUSED(value
) )
2934 //////////////////////////////////////////////////////////////////////
2936 // Message class for the grid table to send requests and notifications
2940 wxGridTableMessage::wxGridTableMessage()
2942 m_table
= (wxGridTableBase
*) NULL
;
2948 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2949 int commandInt1
, int commandInt2
)
2953 m_comInt1
= commandInt1
;
2954 m_comInt2
= commandInt2
;
2959 //////////////////////////////////////////////////////////////////////
2961 // A basic grid table for string data. An object of this class will
2962 // created by wxGrid if you don't specify an alternative table class.
2965 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2967 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2969 wxGridStringTable::wxGridStringTable()
2974 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2977 m_data
.Alloc( numRows
);
2980 sa
.Alloc( numCols
);
2981 sa
.Add( wxEmptyString
, numCols
);
2983 m_data
.Add( sa
, numRows
);
2986 wxGridStringTable::~wxGridStringTable()
2990 int wxGridStringTable::GetNumberRows()
2992 return m_data
.GetCount();
2995 int wxGridStringTable::GetNumberCols()
2997 if ( m_data
.GetCount() > 0 )
2998 return m_data
[0].GetCount();
3003 wxString
wxGridStringTable::GetValue( int row
, int col
)
3005 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3006 _T("invalid row or column index in wxGridStringTable") );
3008 return m_data
[row
][col
];
3011 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3013 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3014 _T("invalid row or column index in wxGridStringTable") );
3016 m_data
[row
][col
] = value
;
3019 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3021 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3022 _T("invalid row or column index in wxGridStringTable") );
3024 return (m_data
[row
][col
] == wxEmptyString
);
3027 void wxGridStringTable::Clear()
3030 int numRows
, numCols
;
3032 numRows
= m_data
.GetCount();
3035 numCols
= m_data
[0].GetCount();
3037 for ( row
= 0; row
< numRows
; row
++ )
3039 for ( col
= 0; col
< numCols
; col
++ )
3041 m_data
[row
][col
] = wxEmptyString
;
3048 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3050 size_t curNumRows
= m_data
.GetCount();
3051 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3052 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3054 if ( pos
>= curNumRows
)
3056 return AppendRows( numRows
);
3060 sa
.Alloc( curNumCols
);
3061 sa
.Add( wxEmptyString
, curNumCols
);
3062 m_data
.Insert( sa
, pos
, numRows
);
3065 wxGridTableMessage
msg( this,
3066 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3070 GetView()->ProcessTableMessage( msg
);
3076 bool wxGridStringTable::AppendRows( size_t numRows
)
3078 size_t curNumRows
= m_data
.GetCount();
3079 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3080 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3083 if ( curNumCols
> 0 )
3085 sa
.Alloc( curNumCols
);
3086 sa
.Add( wxEmptyString
, curNumCols
);
3089 m_data
.Add( sa
, numRows
);
3093 wxGridTableMessage
msg( this,
3094 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3097 GetView()->ProcessTableMessage( msg
);
3103 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3105 size_t curNumRows
= m_data
.GetCount();
3107 if ( pos
>= curNumRows
)
3109 wxFAIL_MSG( wxString::Format
3111 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3113 (unsigned long)numRows
,
3114 (unsigned long)curNumRows
3120 if ( numRows
> curNumRows
- pos
)
3122 numRows
= curNumRows
- pos
;
3125 if ( numRows
>= curNumRows
)
3131 m_data
.RemoveAt( pos
, numRows
);
3135 wxGridTableMessage
msg( this,
3136 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3140 GetView()->ProcessTableMessage( msg
);
3146 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3150 size_t curNumRows
= m_data
.GetCount();
3151 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3152 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3154 if ( pos
>= curNumCols
)
3156 return AppendCols( numCols
);
3159 for ( row
= 0; row
< curNumRows
; row
++ )
3161 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3163 m_data
[row
].Insert( wxEmptyString
, col
);
3168 wxGridTableMessage
msg( this,
3169 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3173 GetView()->ProcessTableMessage( msg
);
3179 bool wxGridStringTable::AppendCols( size_t numCols
)
3183 size_t curNumRows
= m_data
.GetCount();
3187 // TODO: something better than this ?
3189 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3194 for ( row
= 0; row
< curNumRows
; row
++ )
3196 m_data
[row
].Add( wxEmptyString
, numCols
);
3201 wxGridTableMessage
msg( this,
3202 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3205 GetView()->ProcessTableMessage( msg
);
3211 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3215 size_t curNumRows
= m_data
.GetCount();
3216 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3217 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3219 if ( pos
>= curNumCols
)
3221 wxFAIL_MSG( wxString::Format
3223 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3225 (unsigned long)numCols
,
3226 (unsigned long)curNumCols
3231 if ( numCols
> curNumCols
- pos
)
3233 numCols
= curNumCols
- pos
;
3236 for ( row
= 0; row
< curNumRows
; row
++ )
3238 if ( numCols
>= curNumCols
)
3240 m_data
[row
].Clear();
3244 m_data
[row
].RemoveAt( pos
, numCols
);
3249 wxGridTableMessage
msg( this,
3250 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3254 GetView()->ProcessTableMessage( msg
);
3260 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3262 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3264 // using default label
3266 return wxGridTableBase::GetRowLabelValue( row
);
3270 return m_rowLabels
[ row
];
3274 wxString
wxGridStringTable::GetColLabelValue( int col
)
3276 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3278 // using default label
3280 return wxGridTableBase::GetColLabelValue( col
);
3284 return m_colLabels
[ col
];
3288 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3290 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3292 int n
= m_rowLabels
.GetCount();
3294 for ( i
= n
; i
<= row
; i
++ )
3296 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3300 m_rowLabels
[row
] = value
;
3303 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3305 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3307 int n
= m_colLabels
.GetCount();
3309 for ( i
= n
; i
<= col
; i
++ )
3311 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3315 m_colLabels
[col
] = value
;
3320 //////////////////////////////////////////////////////////////////////
3321 //////////////////////////////////////////////////////////////////////
3323 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3325 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3326 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3327 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3328 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3329 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3330 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3333 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3335 const wxPoint
&pos
, const wxSize
&size
)
3336 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3341 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3345 // NO - don't do this because it will set both the x and y origin
3346 // coords to match the parent scrolled window and we just want to
3347 // set the y coord - MB
3349 // m_owner->PrepareDC( dc );
3352 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3353 dc
.SetDeviceOrigin( 0, -y
);
3355 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3356 m_owner
->DrawRowLabels( dc
, rows
);
3360 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3362 m_owner
->ProcessRowLabelMouseEvent( event
);
3366 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3368 m_owner
->GetEventHandler()->ProcessEvent(event
);
3372 // This seems to be required for wxMotif otherwise the mouse
3373 // cursor must be in the cell edit control to get key events
3375 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3377 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3380 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3382 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3387 //////////////////////////////////////////////////////////////////////
3389 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3391 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3392 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3393 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3394 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3395 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3396 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3399 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3401 const wxPoint
&pos
, const wxSize
&size
)
3402 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3407 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3411 // NO - don't do this because it will set both the x and y origin
3412 // coords to match the parent scrolled window and we just want to
3413 // set the x coord - MB
3415 // m_owner->PrepareDC( dc );
3418 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3419 dc
.SetDeviceOrigin( -x
, 0 );
3421 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3422 m_owner
->DrawColLabels( dc
, cols
);
3426 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3428 m_owner
->ProcessColLabelMouseEvent( event
);
3431 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3433 m_owner
->GetEventHandler()->ProcessEvent(event
);
3437 // This seems to be required for wxMotif otherwise the mouse
3438 // cursor must be in the cell edit control to get key events
3440 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3442 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3445 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3447 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3452 //////////////////////////////////////////////////////////////////////
3454 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3456 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3457 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3458 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3459 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3460 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3461 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3464 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3466 const wxPoint
&pos
, const wxSize
&size
)
3467 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3472 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3476 int client_height
= 0;
3477 int client_width
= 0;
3478 GetClientSize( &client_width
, &client_height
);
3480 dc
.SetPen( *wxBLACK_PEN
);
3481 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3482 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3484 dc
.SetPen( *wxWHITE_PEN
);
3485 dc
.DrawLine( 0, 0, client_width
, 0 );
3486 dc
.DrawLine( 0, 0, 0, client_height
);
3490 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3492 m_owner
->ProcessCornerLabelMouseEvent( event
);
3496 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3498 m_owner
->GetEventHandler()->ProcessEvent(event
);
3501 // This seems to be required for wxMotif otherwise the mouse
3502 // cursor must be in the cell edit control to get key events
3504 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3506 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3509 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3511 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3516 //////////////////////////////////////////////////////////////////////
3518 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3520 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3521 EVT_PAINT( wxGridWindow::OnPaint
)
3522 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3523 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3524 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3525 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3526 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3529 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3530 wxGridRowLabelWindow
*rowLblWin
,
3531 wxGridColLabelWindow
*colLblWin
,
3532 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3533 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, wxT("grid window") )
3536 m_rowLabelWin
= rowLblWin
;
3537 m_colLabelWin
= colLblWin
;
3538 SetBackgroundColour(_T("WHITE"));
3542 wxGridWindow::~wxGridWindow()
3547 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3549 wxPaintDC
dc( this );
3550 m_owner
->PrepareDC( dc
);
3551 wxRegion reg
= GetUpdateRegion();
3552 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3553 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3554 #if WXGRID_DRAW_LINES
3555 m_owner
->DrawAllGridLines( dc
, reg
);
3557 m_owner
->DrawGridSpace( dc
);
3558 m_owner
->DrawHighlight( dc
, DirtyCells
);
3562 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3564 wxWindow::ScrollWindow( dx
, dy
, rect
);
3565 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3566 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3570 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3572 m_owner
->ProcessGridCellMouseEvent( event
);
3575 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3577 m_owner
->GetEventHandler()->ProcessEvent(event
);
3580 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3581 // cursor must be in the cell edit control to get key events
3583 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3585 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3588 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3590 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3593 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3598 //////////////////////////////////////////////////////////////////////
3600 // Internal Helper function for computing row or column from some
3601 // (unscrolled) coordinate value, using either
3602 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3603 // of m_rowBottoms/m_ColRights to speed up the search!
3605 // Internal helper macros for simpler use of that function
3607 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3608 const wxArrayInt
& BorderArray
, int nMax
,
3609 bool maxOnOverflow
);
3611 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3612 WXGRID_MIN_COL_WIDTH, \
3613 m_colRights, m_numCols, TRUE)
3614 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3615 WXGRID_MIN_ROW_HEIGHT, \
3616 m_rowBottoms, m_numRows, TRUE)
3617 /////////////////////////////////////////////////////////////////////
3619 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3621 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3622 EVT_PAINT( wxGrid::OnPaint
)
3623 EVT_SIZE( wxGrid::OnSize
)
3624 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3625 EVT_KEY_UP( wxGrid::OnKeyUp
)
3626 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3629 wxGrid::wxGrid( wxWindow
*parent
,
3634 const wxString
& name
)
3635 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3636 m_colMinWidths(GRID_HASH_SIZE
),
3637 m_rowMinHeights(GRID_HASH_SIZE
)
3645 // Must do this or ~wxScrollHelper will pop the wrong event handler
3646 SetTargetWindow(this);
3648 wxSafeDecRef(m_defaultCellAttr
);
3650 #ifdef DEBUG_ATTR_CACHE
3651 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3652 wxPrintf(_T("wxGrid attribute cache statistics: "
3653 "total: %u, hits: %u (%u%%)\n"),
3654 total
, gs_nAttrCacheHits
,
3655 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3661 delete m_typeRegistry
;
3667 // ----- internal init and update functions
3670 void wxGrid::Create()
3672 m_created
= FALSE
; // set to TRUE by CreateGrid
3674 m_table
= (wxGridTableBase
*) NULL
;
3677 m_cellEditCtrlEnabled
= FALSE
;
3679 m_defaultCellAttr
= new wxGridCellAttr();
3681 // Set default cell attributes
3682 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3683 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3684 m_defaultCellAttr
->SetFont(GetFont());
3685 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3686 m_defaultCellAttr
->SetTextColour(
3687 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3688 m_defaultCellAttr
->SetBackgroundColour(
3689 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3690 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3691 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3696 m_currentCellCoords
= wxGridNoCellCoords
;
3698 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3699 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3701 // create the type registry
3702 m_typeRegistry
= new wxGridTypeRegistry
;
3705 // subwindow components that make up the wxGrid
3706 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3711 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3716 m_colLabelWin
= new wxGridColLabelWindow( this,
3721 m_gridWin
= new wxGridWindow( this,
3728 SetTargetWindow( m_gridWin
);
3734 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3735 wxGrid::wxGridSelectionModes selmode
)
3737 wxCHECK_MSG( !m_created
,
3739 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3741 m_numRows
= numRows
;
3742 m_numCols
= numCols
;
3744 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3745 m_table
->SetView( this );
3747 m_selection
= new wxGridSelection( this, selmode
);
3756 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3758 wxCHECK_RET( m_created
,
3759 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3761 m_selection
->SetSelectionMode( selmode
);
3764 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3766 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3767 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3769 return m_selection
->GetSelectionMode();
3772 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3773 wxGrid::wxGridSelectionModes selmode
)
3777 // RD: Actually, this should probably be allowed. I think it would be
3778 // nice to be able to switch multiple Tables in and out of a single
3779 // View at runtime. Is there anything in the implementation that
3780 // would prevent this?
3782 // At least, you now have to cope with m_selection
3783 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3788 m_numRows
= table
->GetNumberRows();
3789 m_numCols
= table
->GetNumberCols();
3792 m_table
->SetView( this );
3795 m_selection
= new wxGridSelection( this, selmode
);
3808 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3809 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3811 if ( m_rowLabelWin
)
3813 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3817 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3820 m_labelTextColour
= wxColour( _T("BLACK") );
3823 m_attrCache
.row
= -1;
3825 // TODO: something better than this ?
3827 m_labelFont
= this->GetFont();
3828 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3830 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3831 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3833 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3834 m_colLabelVertAlign
= wxALIGN_TOP
;
3836 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3837 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3839 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3840 m_defaultRowHeight
+= 8;
3842 m_defaultRowHeight
+= 4;
3845 m_gridLineColour
= wxColour( 128, 128, 255 );
3846 m_gridLinesEnabled
= TRUE
;
3847 m_cellHighlightColour
= m_gridLineColour
;
3848 m_cellHighlightPenWidth
= 2;
3849 m_cellHighlightROPenWidth
= 1;
3851 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3852 m_winCapture
= (wxWindow
*)NULL
;
3853 m_canDragRowSize
= TRUE
;
3854 m_canDragColSize
= TRUE
;
3855 m_canDragGridSize
= TRUE
;
3857 m_dragRowOrCol
= -1;
3858 m_isDragging
= FALSE
;
3859 m_startDragPos
= wxDefaultPosition
;
3861 m_waitForSlowClick
= FALSE
;
3863 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3864 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3866 m_currentCellCoords
= wxGridNoCellCoords
;
3868 m_selectingTopLeft
= wxGridNoCellCoords
;
3869 m_selectingBottomRight
= wxGridNoCellCoords
;
3870 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3871 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3873 m_editable
= TRUE
; // default for whole grid
3875 m_inOnKeyDown
= FALSE
;
3882 // ----------------------------------------------------------------------------
3883 // the idea is to call these functions only when necessary because they create
3884 // quite big arrays which eat memory mostly unnecessary - in particular, if
3885 // default widths/heights are used for all rows/columns, we may not use these
3888 // with some extra code, it should be possible to only store the
3889 // widths/heights different from default ones but this will be done later...
3890 // ----------------------------------------------------------------------------
3892 void wxGrid::InitRowHeights()
3894 m_rowHeights
.Empty();
3895 m_rowBottoms
.Empty();
3897 m_rowHeights
.Alloc( m_numRows
);
3898 m_rowBottoms
.Alloc( m_numRows
);
3902 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
3904 for ( int i
= 0; i
< m_numRows
; i
++ )
3906 rowBottom
+= m_defaultRowHeight
;
3907 m_rowBottoms
.Add( rowBottom
);
3911 void wxGrid::InitColWidths()
3913 m_colWidths
.Empty();
3914 m_colRights
.Empty();
3916 m_colWidths
.Alloc( m_numCols
);
3917 m_colRights
.Alloc( m_numCols
);
3920 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
3922 for ( int i
= 0; i
< m_numCols
; i
++ )
3924 colRight
+= m_defaultColWidth
;
3925 m_colRights
.Add( colRight
);
3929 int wxGrid::GetColWidth(int col
) const
3931 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3934 int wxGrid::GetColLeft(int col
) const
3936 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3937 : m_colRights
[col
] - m_colWidths
[col
];
3940 int wxGrid::GetColRight(int col
) const
3942 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3946 int wxGrid::GetRowHeight(int row
) const
3948 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3951 int wxGrid::GetRowTop(int row
) const
3953 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3954 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3957 int wxGrid::GetRowBottom(int row
) const
3959 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3960 : m_rowBottoms
[row
];
3963 void wxGrid::CalcDimensions()
3966 GetClientSize( &cw
, &ch
);
3968 if ( m_rowLabelWin
->IsShown() )
3969 cw
-= m_rowLabelWidth
;
3970 if ( m_colLabelWin
->IsShown() )
3971 ch
-= m_colLabelHeight
;
3974 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3975 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3977 // preserve (more or less) the previous position
3979 GetViewStart( &x
, &y
);
3981 // maybe we don't need scrollbars at all?
3983 // also adjust the position to be valid for the new scroll rangs
4004 // do set scrollbar parameters
4005 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4006 GetScrollX(w
), GetScrollY(h
), x
, y
,
4007 GetBatchCount() != 0);
4009 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4010 // still must reposition the children
4015 void wxGrid::CalcWindowSizes()
4018 GetClientSize( &cw
, &ch
);
4020 if ( m_cornerLabelWin
->IsShown() )
4021 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4023 if ( m_colLabelWin
->IsShown() )
4024 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4026 if ( m_rowLabelWin
->IsShown() )
4027 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4029 if ( m_gridWin
->IsShown() )
4030 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4034 // this is called when the grid table sends a message to say that it
4035 // has been redimensioned
4037 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4040 bool result
= FALSE
;
4042 // Clear the attribute cache as the attribute might refer to a different
4043 // cell than stored in the cache after adding/removing rows/columns.
4045 // By the same reasoning, the editor should be dismissed if columns are
4046 // added or removed. And for consistency, it should IMHO always be
4047 // removed, not only if the cell "underneath" it actually changes.
4048 // For now, I intentionally do not save the editor's content as the
4049 // cell it might want to save that stuff to might no longer exist.
4050 HideCellEditControl();
4052 // if we were using the default widths/heights so far, we must change them
4054 if ( m_colWidths
.IsEmpty() )
4059 if ( m_rowHeights
.IsEmpty() )
4065 switch ( msg
.GetId() )
4067 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4069 size_t pos
= msg
.GetCommandInt();
4070 int numRows
= msg
.GetCommandInt2();
4072 m_numRows
+= numRows
;
4074 if ( !m_rowHeights
.IsEmpty() )
4076 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4077 m_rowBottoms
.Insert( 0, pos
, numRows
);
4080 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4082 for ( i
= pos
; i
< m_numRows
; i
++ )
4084 bottom
+= m_rowHeights
[i
];
4085 m_rowBottoms
[i
] = bottom
;
4088 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4090 // if we have just inserted cols into an empty grid the current
4091 // cell will be undefined...
4093 SetCurrentCell( 0, 0 );
4097 m_selection
->UpdateRows( pos
, numRows
);
4098 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4100 attrProvider
->UpdateAttrRows( pos
, numRows
);
4102 if ( !GetBatchCount() )
4105 m_rowLabelWin
->Refresh();
4111 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4113 int numRows
= msg
.GetCommandInt();
4114 int oldNumRows
= m_numRows
;
4115 m_numRows
+= numRows
;
4117 if ( !m_rowHeights
.IsEmpty() )
4119 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4120 m_rowBottoms
.Add( 0, numRows
);
4123 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4125 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4127 bottom
+= m_rowHeights
[i
];
4128 m_rowBottoms
[i
] = bottom
;
4131 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4133 // if we have just inserted cols into an empty grid the current
4134 // cell will be undefined...
4136 SetCurrentCell( 0, 0 );
4138 if ( !GetBatchCount() )
4141 m_rowLabelWin
->Refresh();
4147 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4149 size_t pos
= msg
.GetCommandInt();
4150 int numRows
= msg
.GetCommandInt2();
4151 m_numRows
-= numRows
;
4153 if ( !m_rowHeights
.IsEmpty() )
4155 m_rowHeights
.RemoveAt( pos
, numRows
);
4156 m_rowBottoms
.RemoveAt( pos
, numRows
);
4159 for ( i
= 0; i
< m_numRows
; i
++ )
4161 h
+= m_rowHeights
[i
];
4162 m_rowBottoms
[i
] = h
;
4167 m_currentCellCoords
= wxGridNoCellCoords
;
4171 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4172 m_currentCellCoords
.Set( 0, 0 );
4176 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4177 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4179 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4180 // ifdef'd out following patch from Paul Gammans
4182 // No need to touch column attributes, unless we
4183 // removed _all_ rows, in this case, we remove
4184 // all column attributes.
4185 // I hate to do this here, but the
4186 // needed data is not available inside UpdateAttrRows.
4187 if ( !GetNumberRows() )
4188 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4191 if ( !GetBatchCount() )
4194 m_rowLabelWin
->Refresh();
4200 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4202 size_t pos
= msg
.GetCommandInt();
4203 int numCols
= msg
.GetCommandInt2();
4204 m_numCols
+= numCols
;
4206 if ( !m_colWidths
.IsEmpty() )
4208 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4209 m_colRights
.Insert( 0, pos
, numCols
);
4212 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4214 for ( i
= pos
; i
< m_numCols
; i
++ )
4216 right
+= m_colWidths
[i
];
4217 m_colRights
[i
] = right
;
4220 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4222 // if we have just inserted cols into an empty grid the current
4223 // cell will be undefined...
4225 SetCurrentCell( 0, 0 );
4229 m_selection
->UpdateCols( pos
, numCols
);
4230 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4232 attrProvider
->UpdateAttrCols( pos
, numCols
);
4233 if ( !GetBatchCount() )
4236 m_colLabelWin
->Refresh();
4243 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4245 int numCols
= msg
.GetCommandInt();
4246 int oldNumCols
= m_numCols
;
4247 m_numCols
+= numCols
;
4248 if ( !m_colWidths
.IsEmpty() )
4250 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4251 m_colRights
.Add( 0, numCols
);
4254 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4256 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4258 right
+= m_colWidths
[i
];
4259 m_colRights
[i
] = right
;
4262 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4264 // if we have just inserted cols into an empty grid the current
4265 // cell will be undefined...
4267 SetCurrentCell( 0, 0 );
4269 if ( !GetBatchCount() )
4272 m_colLabelWin
->Refresh();
4278 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4280 size_t pos
= msg
.GetCommandInt();
4281 int numCols
= msg
.GetCommandInt2();
4282 m_numCols
-= numCols
;
4284 if ( !m_colWidths
.IsEmpty() )
4286 m_colWidths
.RemoveAt( pos
, numCols
);
4287 m_colRights
.RemoveAt( pos
, numCols
);
4290 for ( i
= 0; i
< m_numCols
; i
++ )
4292 w
+= m_colWidths
[i
];
4298 m_currentCellCoords
= wxGridNoCellCoords
;
4302 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4303 m_currentCellCoords
.Set( 0, 0 );
4307 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4308 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4310 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4311 // ifdef'd out following patch from Paul Gammans
4313 // No need to touch row attributes, unless we
4314 // removed _all_ columns, in this case, we remove
4315 // all row attributes.
4316 // I hate to do this here, but the
4317 // needed data is not available inside UpdateAttrCols.
4318 if ( !GetNumberCols() )
4319 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4322 if ( !GetBatchCount() )
4325 m_colLabelWin
->Refresh();
4332 if (result
&& !GetBatchCount() )
4333 m_gridWin
->Refresh();
4338 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4340 wxRegionIterator
iter( reg
);
4343 wxArrayInt rowlabels
;
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
.GetTop() > ch
) r
.SetTop( 0 );
4359 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4362 // logical bounds of update region
4365 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4366 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4368 // find the row labels within these bounds
4371 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4373 if ( GetRowBottom(row
) < top
)
4376 if ( GetRowTop(row
) > bottom
)
4379 rowlabels
.Add( row
);
4389 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4391 wxRegionIterator
iter( reg
);
4394 wxArrayInt colLabels
;
4401 // TODO: remove this when we can...
4402 // There is a bug in wxMotif that gives garbage update
4403 // rectangles if you jump-scroll a long way by clicking the
4404 // scrollbar with middle button. This is a work-around
4406 #if defined(__WXMOTIF__)
4408 m_gridWin
->GetClientSize( &cw
, &ch
);
4409 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4410 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4413 // logical bounds of update region
4416 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4417 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4419 // find the cells within these bounds
4422 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4424 if ( GetColRight(col
) < left
)
4427 if ( GetColLeft(col
) > right
)
4430 colLabels
.Add( col
);
4439 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4441 wxRegionIterator
iter( reg
);
4444 wxGridCellCoordsArray cellsExposed
;
4446 int left
, top
, right
, bottom
;
4451 // TODO: remove this when we can...
4452 // There is a bug in wxMotif that gives garbage update
4453 // rectangles if you jump-scroll a long way by clicking the
4454 // scrollbar with middle button. This is a work-around
4456 #if defined(__WXMOTIF__)
4458 m_gridWin
->GetClientSize( &cw
, &ch
);
4459 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4460 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4461 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4462 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4465 // logical bounds of update region
4467 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4468 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4470 // find the cells within these bounds
4473 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4475 if ( GetRowBottom(row
) <= top
)
4478 if ( GetRowTop(row
) > bottom
)
4481 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4483 if ( GetColRight(col
) <= left
)
4486 if ( GetColLeft(col
) > right
)
4489 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4496 return cellsExposed
;
4500 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4503 wxPoint
pos( event
.GetPosition() );
4504 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4506 if ( event
.Dragging() )
4510 m_isDragging
= TRUE
;
4511 m_rowLabelWin
->CaptureMouse();
4514 if ( event
.LeftIsDown() )
4516 switch( m_cursorMode
)
4518 case WXGRID_CURSOR_RESIZE_ROW
:
4520 int cw
, ch
, left
, dummy
;
4521 m_gridWin
->GetClientSize( &cw
, &ch
);
4522 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4524 wxClientDC
dc( m_gridWin
);
4527 GetRowTop(m_dragRowOrCol
) +
4528 GetRowMinimalHeight(m_dragRowOrCol
) );
4529 dc
.SetLogicalFunction(wxINVERT
);
4530 if ( m_dragLastPos
>= 0 )
4532 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4534 dc
.DrawLine( left
, y
, left
+cw
, y
);
4539 case WXGRID_CURSOR_SELECT_ROW
:
4540 if ( (row
= YToRow( y
)) >= 0 )
4544 m_selection
->SelectRow( row
,
4545 event
.ControlDown(),
4552 // default label to suppress warnings about "enumeration value
4553 // 'xxx' not handled in switch
4561 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4566 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4567 m_isDragging
= FALSE
;
4570 // ------------ Entering or leaving the window
4572 if ( event
.Entering() || event
.Leaving() )
4574 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4578 // ------------ Left button pressed
4580 else if ( event
.LeftDown() )
4582 // don't send a label click event for a hit on the
4583 // edge of the row label - this is probably the user
4584 // wanting to resize the row
4586 if ( YToEdgeOfRow(y
) < 0 )
4590 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4592 if ( !event
.ShiftDown() && !event
.ControlDown() )
4596 if ( event
.ShiftDown() )
4598 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4601 GetNumberCols() - 1,
4602 event
.ControlDown(),
4609 m_selection
->SelectRow( row
,
4610 event
.ControlDown(),
4617 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4622 // starting to drag-resize a row
4624 if ( CanDragRowSize() )
4625 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4630 // ------------ Left double click
4632 else if (event
.LeftDClick() )
4634 if ( YToEdgeOfRow(y
) < 0 )
4637 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4642 // ------------ Left button released
4644 else if ( event
.LeftUp() )
4646 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4648 DoEndDragResizeRow();
4650 // Note: we are ending the event *after* doing
4651 // default processing in this case
4653 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4656 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4661 // ------------ Right button down
4663 else if ( event
.RightDown() )
4666 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4668 // no default action at the moment
4673 // ------------ Right double click
4675 else if ( event
.RightDClick() )
4678 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4680 // no default action at the moment
4685 // ------------ No buttons down and mouse moving
4687 else if ( event
.Moving() )
4689 m_dragRowOrCol
= YToEdgeOfRow( y
);
4690 if ( m_dragRowOrCol
>= 0 )
4692 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4694 // don't capture the mouse yet
4695 if ( CanDragRowSize() )
4696 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4699 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4701 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4707 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4710 wxPoint
pos( event
.GetPosition() );
4711 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4713 if ( event
.Dragging() )
4717 m_isDragging
= TRUE
;
4718 m_colLabelWin
->CaptureMouse();
4721 if ( event
.LeftIsDown() )
4723 switch( m_cursorMode
)
4725 case WXGRID_CURSOR_RESIZE_COL
:
4727 int cw
, ch
, dummy
, top
;
4728 m_gridWin
->GetClientSize( &cw
, &ch
);
4729 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4731 wxClientDC
dc( m_gridWin
);
4734 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4735 GetColMinimalWidth(m_dragRowOrCol
));
4736 dc
.SetLogicalFunction(wxINVERT
);
4737 if ( m_dragLastPos
>= 0 )
4739 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4741 dc
.DrawLine( x
, top
, x
, top
+ch
);
4746 case WXGRID_CURSOR_SELECT_COL
:
4747 if ( (col
= XToCol( x
)) >= 0 )
4751 m_selection
->SelectCol( col
,
4752 event
.ControlDown(),
4759 // default label to suppress warnings about "enumeration value
4760 // 'xxx' not handled in switch
4768 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4773 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4774 m_isDragging
= FALSE
;
4777 // ------------ Entering or leaving the window
4779 if ( event
.Entering() || event
.Leaving() )
4781 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4785 // ------------ Left button pressed
4787 else if ( event
.LeftDown() )
4789 // don't send a label click event for a hit on the
4790 // edge of the col label - this is probably the user
4791 // wanting to resize the col
4793 if ( XToEdgeOfCol(x
) < 0 )
4797 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4799 if ( !event
.ShiftDown() && !event
.ControlDown() )
4803 if ( event
.ShiftDown() )
4805 m_selection
->SelectBlock( 0,
4806 m_currentCellCoords
.GetCol(),
4807 GetNumberRows() - 1, col
,
4808 event
.ControlDown(),
4815 m_selection
->SelectCol( col
,
4816 event
.ControlDown(),
4823 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4828 // starting to drag-resize a col
4830 if ( CanDragColSize() )
4831 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4836 // ------------ Left double click
4838 if ( event
.LeftDClick() )
4840 if ( XToEdgeOfCol(x
) < 0 )
4843 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4848 // ------------ Left button released
4850 else if ( event
.LeftUp() )
4852 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4854 DoEndDragResizeCol();
4856 // Note: we are ending the event *after* doing
4857 // default processing in this case
4859 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4862 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4867 // ------------ Right button down
4869 else if ( event
.RightDown() )
4872 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4874 // no default action at the moment
4879 // ------------ Right double click
4881 else if ( event
.RightDClick() )
4884 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4886 // no default action at the moment
4891 // ------------ No buttons down and mouse moving
4893 else if ( event
.Moving() )
4895 m_dragRowOrCol
= XToEdgeOfCol( x
);
4896 if ( m_dragRowOrCol
>= 0 )
4898 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4900 // don't capture the cursor yet
4901 if ( CanDragColSize() )
4902 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4905 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4907 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4913 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4915 if ( event
.LeftDown() )
4917 // indicate corner label by having both row and
4920 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4926 else if ( event
.LeftDClick() )
4928 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4931 else if ( event
.RightDown() )
4933 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4935 // no default action at the moment
4939 else if ( event
.RightDClick() )
4941 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4943 // no default action at the moment
4948 void wxGrid::ChangeCursorMode(CursorMode mode
,
4953 static const wxChar
*cursorModes
[] =
4962 wxLogTrace(_T("grid"),
4963 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4964 win
== m_colLabelWin
? _T("colLabelWin")
4965 : win
? _T("rowLabelWin")
4967 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4968 #endif // __WXDEBUG__
4970 if ( mode
== m_cursorMode
&&
4971 win
== m_winCapture
&&
4972 captureMouse
== (m_winCapture
!= NULL
))
4977 // by default use the grid itself
4983 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4984 m_winCapture
= (wxWindow
*)NULL
;
4987 m_cursorMode
= mode
;
4989 switch ( m_cursorMode
)
4991 case WXGRID_CURSOR_RESIZE_ROW
:
4992 win
->SetCursor( m_rowResizeCursor
);
4995 case WXGRID_CURSOR_RESIZE_COL
:
4996 win
->SetCursor( m_colResizeCursor
);
5000 win
->SetCursor( *wxSTANDARD_CURSOR
);
5003 // we need to capture mouse when resizing
5004 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5005 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5007 if ( captureMouse
&& resize
)
5009 win
->CaptureMouse();
5014 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5017 wxPoint
pos( event
.GetPosition() );
5018 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5020 wxGridCellCoords coords
;
5021 XYToCell( x
, y
, coords
);
5023 int cell_rows
, cell_cols
;
5024 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5025 if ((cell_rows
< 0) || (cell_cols
< 0))
5027 coords
.SetRow(coords
.GetRow() + cell_rows
);
5028 coords
.SetCol(coords
.GetCol() + cell_cols
);
5031 if ( event
.Dragging() )
5033 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5035 // Don't start doing anything until the mouse has been drug at
5036 // least 3 pixels in any direction...
5039 if (m_startDragPos
== wxDefaultPosition
)
5041 m_startDragPos
= pos
;
5044 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5048 m_isDragging
= TRUE
;
5049 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5051 // Hide the edit control, so it
5052 // won't interfer with drag-shrinking.
5053 if ( IsCellEditControlShown() )
5055 HideCellEditControl();
5056 SaveEditControlValue();
5059 // Have we captured the mouse yet?
5062 m_winCapture
= m_gridWin
;
5063 m_winCapture
->CaptureMouse();
5066 if ( coords
!= wxGridNoCellCoords
)
5068 if ( event
.ControlDown() )
5070 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5071 m_selectingKeyboard
= coords
;
5072 HighlightBlock ( m_selectingKeyboard
, coords
);
5076 if ( !IsSelection() )
5078 HighlightBlock( coords
, coords
);
5082 HighlightBlock( m_currentCellCoords
, coords
);
5086 if (! IsVisible(coords
))
5088 MakeCellVisible(coords
);
5089 // TODO: need to introduce a delay or something here. The
5090 // scrolling is way to fast, at least on MSW - also on GTK.
5094 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5096 int cw
, ch
, left
, dummy
;
5097 m_gridWin
->GetClientSize( &cw
, &ch
);
5098 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5100 wxClientDC
dc( m_gridWin
);
5102 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5103 GetRowMinimalHeight(m_dragRowOrCol
) );
5104 dc
.SetLogicalFunction(wxINVERT
);
5105 if ( m_dragLastPos
>= 0 )
5107 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5109 dc
.DrawLine( left
, y
, left
+cw
, y
);
5112 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5114 int cw
, ch
, dummy
, top
;
5115 m_gridWin
->GetClientSize( &cw
, &ch
);
5116 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5118 wxClientDC
dc( m_gridWin
);
5120 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5121 GetColMinimalWidth(m_dragRowOrCol
) );
5122 dc
.SetLogicalFunction(wxINVERT
);
5123 if ( m_dragLastPos
>= 0 )
5125 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5127 dc
.DrawLine( x
, top
, x
, top
+ch
);
5134 m_isDragging
= FALSE
;
5135 m_startDragPos
= wxDefaultPosition
;
5137 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5138 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5141 if ( event
.Entering() || event
.Leaving() )
5143 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5144 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5149 // ------------ Left button pressed
5151 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5153 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5158 if ( !event
.ControlDown() )
5160 if ( event
.ShiftDown() )
5164 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5165 m_currentCellCoords
.GetCol(),
5168 event
.ControlDown(),
5174 else if ( XToEdgeOfCol(x
) < 0 &&
5175 YToEdgeOfRow(y
) < 0 )
5177 DisableCellEditControl();
5178 MakeCellVisible( coords
);
5180 // if this is the second click on this cell then start
5182 if ( m_waitForSlowClick
&&
5183 (coords
== m_currentCellCoords
) &&
5184 CanEnableCellControl())
5186 EnableCellEditControl();
5188 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5189 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5192 editor
->StartingClick();
5196 m_waitForSlowClick
= FALSE
;
5200 if ( event
.ControlDown() )
5204 m_selection
->ToggleCellSelection( coords
.GetRow(),
5206 event
.ControlDown(),
5211 m_selectingTopLeft
= wxGridNoCellCoords
;
5212 m_selectingBottomRight
= wxGridNoCellCoords
;
5213 m_selectingKeyboard
= coords
;
5217 SetCurrentCell( coords
);
5220 if ( m_selection
->GetSelectionMode() !=
5221 wxGrid::wxGridSelectCells
)
5223 HighlightBlock( coords
, coords
);
5227 m_waitForSlowClick
= TRUE
;
5234 // ------------ Left double click
5236 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5238 DisableCellEditControl();
5240 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5242 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5250 // ------------ Left button released
5252 else if ( event
.LeftUp() )
5254 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5256 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5257 m_selectingBottomRight
!= wxGridNoCellCoords
)
5261 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5262 m_winCapture
= NULL
;
5267 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5268 m_selectingTopLeft
.GetCol(),
5269 m_selectingBottomRight
.GetRow(),
5270 m_selectingBottomRight
.GetCol(),
5271 event
.ControlDown(),
5277 m_selectingTopLeft
= wxGridNoCellCoords
;
5278 m_selectingBottomRight
= wxGridNoCellCoords
;
5281 // Show the edit control, if it has been hidden for
5283 ShowCellEditControl();
5285 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5287 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5288 DoEndDragResizeRow();
5290 // Note: we are ending the event *after* doing
5291 // default processing in this case
5293 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5295 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5297 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5298 DoEndDragResizeCol();
5300 // Note: we are ending the event *after* doing
5301 // default processing in this case
5303 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5310 // ------------ Right button down
5312 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5314 DisableCellEditControl();
5315 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5320 // no default action at the moment
5325 // ------------ Right double click
5327 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5329 DisableCellEditControl();
5330 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5335 // no default action at the moment
5339 // ------------ Moving and no button action
5341 else if ( event
.Moving() && !event
.IsButton() )
5343 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5345 // out of grid cell area
5346 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5350 int dragRow
= YToEdgeOfRow( y
);
5351 int dragCol
= XToEdgeOfCol( x
);
5353 // Dragging on the corner of a cell to resize in both
5354 // directions is not implemented yet...
5356 if ( dragRow
>= 0 && dragCol
>= 0 )
5358 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5364 m_dragRowOrCol
= dragRow
;
5366 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5368 if ( CanDragRowSize() && CanDragGridSize() )
5369 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5374 m_dragRowOrCol
= dragCol
;
5382 m_dragRowOrCol
= dragCol
;
5384 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5386 if ( CanDragColSize() && CanDragGridSize() )
5387 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5393 // Neither on a row or col edge
5395 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5397 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5403 void wxGrid::DoEndDragResizeRow()
5405 if ( m_dragLastPos
>= 0 )
5407 // erase the last line and resize the row
5409 int cw
, ch
, left
, dummy
;
5410 m_gridWin
->GetClientSize( &cw
, &ch
);
5411 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5413 wxClientDC
dc( m_gridWin
);
5415 dc
.SetLogicalFunction( wxINVERT
);
5416 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5417 HideCellEditControl();
5418 SaveEditControlValue();
5420 int rowTop
= GetRowTop(m_dragRowOrCol
);
5421 SetRowSize( m_dragRowOrCol
,
5422 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5424 if ( !GetBatchCount() )
5426 // Only needed to get the correct rect.y:
5427 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5429 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5430 rect
.width
= m_rowLabelWidth
;
5431 rect
.height
= ch
- rect
.y
;
5432 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5434 // if there is a multicell block, paint all of it
5437 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5438 int leftCol
= XToCol(left
);
5439 int rightCol
= XToCol(left
+cw
);
5442 if (rightCol
< 0) rightCol
= m_numCols
;
5443 for (i
=leftCol
; i
<rightCol
; i
++)
5445 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5446 if (cell_rows
< subtract_rows
)
5447 subtract_rows
= cell_rows
;
5449 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5450 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5451 rect
.height
= ch
- rect
.y
;
5454 m_gridWin
->Refresh( FALSE
, &rect
);
5457 ShowCellEditControl();
5462 void wxGrid::DoEndDragResizeCol()
5464 if ( m_dragLastPos
>= 0 )
5466 // erase the last line and resize the col
5468 int cw
, ch
, dummy
, top
;
5469 m_gridWin
->GetClientSize( &cw
, &ch
);
5470 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5472 wxClientDC
dc( m_gridWin
);
5474 dc
.SetLogicalFunction( wxINVERT
);
5475 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5476 HideCellEditControl();
5477 SaveEditControlValue();
5479 int colLeft
= GetColLeft(m_dragRowOrCol
);
5480 SetColSize( m_dragRowOrCol
,
5481 wxMax( m_dragLastPos
- colLeft
,
5482 GetColMinimalWidth(m_dragRowOrCol
) ) );
5484 if ( !GetBatchCount() )
5486 // Only needed to get the correct rect.x:
5487 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5489 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5490 rect
.width
= cw
- rect
.x
;
5491 rect
.height
= m_colLabelHeight
;
5492 m_colLabelWin
->Refresh( TRUE
, &rect
);
5494 // if there is a multicell block, paint all of it
5497 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5498 int topRow
= YToRow(top
);
5499 int bottomRow
= YToRow(top
+cw
);
5502 if (bottomRow
< 0) bottomRow
= m_numRows
;
5503 for (i
=topRow
; i
<bottomRow
; i
++)
5505 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5506 if (cell_cols
< subtract_cols
)
5507 subtract_cols
= cell_cols
;
5509 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5510 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5511 rect
.width
= cw
- rect
.x
;
5514 m_gridWin
->Refresh( FALSE
, &rect
);
5517 ShowCellEditControl();
5524 // ------ interaction with data model
5526 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5528 switch ( msg
.GetId() )
5530 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5531 return GetModelValues();
5533 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5534 return SetModelValues();
5536 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5537 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5538 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5539 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5540 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5541 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5542 return Redimension( msg
);
5551 // The behaviour of this function depends on the grid table class
5552 // Clear() function. For the default wxGridStringTable class the
5553 // behavious is to replace all cell contents with wxEmptyString but
5554 // not to change the number of rows or cols.
5556 void wxGrid::ClearGrid()
5560 if (IsCellEditControlEnabled())
5561 DisableCellEditControl();
5564 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5569 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5571 // TODO: something with updateLabels flag
5575 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5581 if (IsCellEditControlEnabled())
5582 DisableCellEditControl();
5584 return m_table
->InsertRows( pos
, numRows
);
5586 // the table will have sent the results of the insert row
5587 // operation to this view object as a grid table message
5593 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5595 // TODO: something with updateLabels flag
5599 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5603 return ( m_table
&& m_table
->AppendRows( numRows
) );
5604 // the table will have sent the results of the append row
5605 // operation to this view object as a grid table message
5609 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5611 // TODO: something with updateLabels flag
5615 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5621 if (IsCellEditControlEnabled())
5622 DisableCellEditControl();
5624 return (m_table
->DeleteRows( pos
, numRows
));
5625 // the table will have sent the results of the delete row
5626 // operation to this view object as a grid table message
5632 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5634 // TODO: something with updateLabels flag
5638 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5644 if (IsCellEditControlEnabled())
5645 DisableCellEditControl();
5647 return m_table
->InsertCols( pos
, numCols
);
5648 // the table will have sent the results of the insert col
5649 // operation to this view object as a grid table message
5655 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5657 // TODO: something with updateLabels flag
5661 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5665 return ( m_table
&& m_table
->AppendCols( numCols
) );
5666 // the table will have sent the results of the append col
5667 // operation to this view object as a grid table message
5671 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5673 // TODO: something with updateLabels flag
5677 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5683 if (IsCellEditControlEnabled())
5684 DisableCellEditControl();
5686 return ( m_table
->DeleteCols( pos
, numCols
) );
5687 // the table will have sent the results of the delete col
5688 // operation to this view object as a grid table message
5696 // ----- event handlers
5699 // Generate a grid event based on a mouse event and
5700 // return the result of ProcessEvent()
5702 int wxGrid::SendEvent( const wxEventType type
,
5704 wxMouseEvent
& mouseEv
)
5709 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5711 int rowOrCol
= (row
== -1 ? col
: row
);
5713 wxGridSizeEvent
gridEvt( GetId(),
5717 mouseEv
.GetX() + GetRowLabelSize(),
5718 mouseEv
.GetY() + GetColLabelSize(),
5719 mouseEv
.ControlDown(),
5720 mouseEv
.ShiftDown(),
5722 mouseEv
.MetaDown() );
5724 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5725 vetoed
= !gridEvt
.IsAllowed();
5727 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5729 // Right now, it should _never_ end up here!
5730 wxGridRangeSelectEvent
gridEvt( GetId(),
5734 m_selectingBottomRight
,
5736 mouseEv
.ControlDown(),
5737 mouseEv
.ShiftDown(),
5739 mouseEv
.MetaDown() );
5741 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5742 vetoed
= !gridEvt
.IsAllowed();
5746 wxGridEvent
gridEvt( GetId(),
5750 mouseEv
.GetX() + GetRowLabelSize(),
5751 mouseEv
.GetY() + GetColLabelSize(),
5753 mouseEv
.ControlDown(),
5754 mouseEv
.ShiftDown(),
5756 mouseEv
.MetaDown() );
5757 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5758 vetoed
= !gridEvt
.IsAllowed();
5761 // A Veto'd event may not be `claimed' so test this first
5762 if (vetoed
) return -1;
5763 return claimed
? 1 : 0;
5767 // Generate a grid event of specified type and return the result
5768 // of ProcessEvent().
5770 int wxGrid::SendEvent( const wxEventType type
,
5776 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5778 int rowOrCol
= (row
== -1 ? col
: row
);
5780 wxGridSizeEvent
gridEvt( GetId(),
5785 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5786 vetoed
= !gridEvt
.IsAllowed();
5790 wxGridEvent
gridEvt( GetId(),
5795 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5796 vetoed
= !gridEvt
.IsAllowed();
5799 // A Veto'd event may not be `claimed' so test this first
5800 if (vetoed
) return -1;
5801 return claimed
? 1 : 0;
5805 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5807 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5810 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
5812 // Don't do anything if between Begin/EndBatch...
5813 // EndBatch() will do all this on the last nested one anyway.
5814 if (! GetBatchCount())
5816 // Refresh to get correct scrolled position:
5817 wxScrolledWindow::Refresh(eraseb
,rect
);
5821 int rect_x
, rect_y
, rectWidth
, rectHeight
;
5822 int width_label
, width_cell
, height_label
, height_cell
;
5825 //Copy rectangle can get scroll offsets..
5826 rect_x
= rect
->GetX();
5827 rect_y
= rect
->GetY();
5828 rectWidth
= rect
->GetWidth();
5829 rectHeight
= rect
->GetHeight();
5831 width_label
= m_rowLabelWidth
- rect_x
;
5832 if (width_label
> rectWidth
) width_label
= rectWidth
;
5834 height_label
= m_colLabelHeight
- rect_y
;
5835 if (height_label
> rectHeight
) height_label
= rectHeight
;
5837 if (rect_x
> m_rowLabelWidth
)
5839 x
= rect_x
- m_rowLabelWidth
;
5840 width_cell
= rectWidth
;
5845 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
5848 if (rect_y
> m_colLabelHeight
)
5850 y
= rect_y
- m_colLabelHeight
;
5851 height_cell
= rectHeight
;
5856 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
5859 // Paint corner label part intersecting rect.
5860 if ( width_label
> 0 && height_label
> 0 )
5862 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
5863 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
5866 // Paint col labels part intersecting rect.
5867 if ( width_cell
> 0 && height_label
> 0 )
5869 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
5870 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
5873 // Paint row labels part intersecting rect.
5874 if ( width_label
> 0 && height_cell
> 0 )
5876 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
5877 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
5880 // Paint cell area part intersecting rect.
5881 if ( width_cell
> 0 && height_cell
> 0 )
5883 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
5884 m_gridWin
->Refresh(eraseb
, &anotherrect
);
5889 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
5890 m_colLabelWin
->Refresh(eraseb
, NULL
);
5891 m_rowLabelWin
->Refresh(eraseb
, NULL
);
5892 m_gridWin
->Refresh(eraseb
, NULL
);
5897 void wxGrid::OnSize( wxSizeEvent
& event
)
5899 // position the child windows
5902 // don't call CalcDimensions() from here, the base class handles the size
5908 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5910 if ( m_inOnKeyDown
)
5912 // shouldn't be here - we are going round in circles...
5914 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5917 m_inOnKeyDown
= TRUE
;
5919 // propagate the event up and see if it gets processed
5921 wxWindow
*parent
= GetParent();
5922 wxKeyEvent
keyEvt( event
);
5923 keyEvt
.SetEventObject( parent
);
5925 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5928 // try local handlers
5930 switch ( event
.KeyCode() )
5933 if ( event
.ControlDown() )
5935 MoveCursorUpBlock( event
.ShiftDown() );
5939 MoveCursorUp( event
.ShiftDown() );
5944 if ( event
.ControlDown() )
5946 MoveCursorDownBlock( event
.ShiftDown() );
5950 MoveCursorDown( event
.ShiftDown() );
5955 if ( event
.ControlDown() )
5957 MoveCursorLeftBlock( event
.ShiftDown() );
5961 MoveCursorLeft( event
.ShiftDown() );
5966 if ( event
.ControlDown() )
5968 MoveCursorRightBlock( event
.ShiftDown() );
5972 MoveCursorRight( event
.ShiftDown() );
5977 case WXK_NUMPAD_ENTER
:
5978 if ( event
.ControlDown() )
5980 event
.Skip(); // to let the edit control have the return
5984 if ( GetGridCursorRow() < GetNumberRows()-1 )
5986 MoveCursorDown( event
.ShiftDown() );
5990 // at the bottom of a column
5991 HideCellEditControl();
5992 SaveEditControlValue();
6002 if (event
.ShiftDown())
6004 if ( GetGridCursorCol() > 0 )
6006 MoveCursorLeft( FALSE
);
6011 HideCellEditControl();
6012 SaveEditControlValue();
6017 if ( GetGridCursorCol() < GetNumberCols()-1 )
6019 MoveCursorRight( FALSE
);
6024 HideCellEditControl();
6025 SaveEditControlValue();
6031 if ( event
.ControlDown() )
6033 MakeCellVisible( 0, 0 );
6034 SetCurrentCell( 0, 0 );
6043 if ( event
.ControlDown() )
6045 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6046 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6063 if ( event
.ControlDown() )
6067 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6068 m_currentCellCoords
.GetCol(),
6069 event
.ControlDown(),
6076 if ( !IsEditable() )
6078 MoveCursorRight( FALSE
);
6081 // Otherwise fall through to default
6084 // is it possible to edit the current cell at all?
6085 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6087 // yes, now check whether the cells editor accepts the key
6088 int row
= m_currentCellCoords
.GetRow();
6089 int col
= m_currentCellCoords
.GetCol();
6090 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6091 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6093 // <F2> is special and will always start editing, for
6094 // other keys - ask the editor itself
6095 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
6096 || editor
->IsAcceptedKey(event
) )
6098 EnableCellEditControl();
6100 // the editor could be not shown for a variety of
6101 // reasons (i.e. blocked by the app or whatever), so
6102 // check if it really was created
6103 if ( m_cellEditCtrlEnabled
)
6105 editor
->StartingKey(event
);
6118 // let others process char events with modifiers or all
6119 // char events for readonly cells
6126 m_inOnKeyDown
= FALSE
;
6129 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6131 // try local handlers
6133 if ( event
.KeyCode() == WXK_SHIFT
)
6135 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6136 m_selectingBottomRight
!= wxGridNoCellCoords
)
6140 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6141 m_selectingTopLeft
.GetCol(),
6142 m_selectingBottomRight
.GetRow(),
6143 m_selectingBottomRight
.GetCol(),
6144 event
.ControlDown(),
6151 m_selectingTopLeft
= wxGridNoCellCoords
;
6152 m_selectingBottomRight
= wxGridNoCellCoords
;
6153 m_selectingKeyboard
= wxGridNoCellCoords
;
6157 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6161 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6163 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6165 // the event has been intercepted - do nothing
6169 wxClientDC
dc(m_gridWin
);
6172 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6174 HideCellEditControl();
6175 DisableCellEditControl();
6177 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6180 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6181 if ( !m_gridLinesEnabled
)
6189 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6191 // Otherwise refresh redraws the highlight!
6192 m_currentCellCoords
= coords
;
6194 DrawGridCellArea(dc
,cells
);
6195 DrawAllGridLines( dc
, r
);
6199 m_currentCellCoords
= coords
;
6201 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6202 DrawCellHighlight(dc
, attr
);
6207 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6210 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6214 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6217 rightCol
= GetNumberCols() - 1;
6219 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6222 bottomRow
= GetNumberRows() - 1;
6226 if ( topRow
> bottomRow
)
6233 if ( leftCol
> rightCol
)
6240 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6241 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6243 // First the case that we selected a completely new area
6244 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6245 m_selectingBottomRight
== wxGridNoCellCoords
)
6248 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6249 wxGridCellCoords ( bottomRow
, rightCol
) );
6250 m_gridWin
->Refresh( FALSE
, &rect
);
6252 // Now handle changing an existing selection area.
6253 else if ( m_selectingTopLeft
!= updateTopLeft
||
6254 m_selectingBottomRight
!= updateBottomRight
)
6256 // Compute two optimal update rectangles:
6257 // Either one rectangle is a real subset of the
6258 // other, or they are (almost) disjoint!
6260 bool need_refresh
[4];
6264 need_refresh
[3] = FALSE
;
6267 // Store intermediate values
6268 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6269 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6270 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6271 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6273 // Determine the outer/inner coordinates.
6274 if (oldLeft
> leftCol
)
6280 if (oldTop
> topRow
)
6286 if (oldRight
< rightCol
)
6289 oldRight
= rightCol
;
6292 if (oldBottom
< bottomRow
)
6295 oldBottom
= bottomRow
;
6299 // Now, either the stuff marked old is the outer
6300 // rectangle or we don't have a situation where one
6301 // is contained in the other.
6303 if ( oldLeft
< leftCol
)
6305 // Refresh the newly selected or deselected
6306 // area to the left of the old or new selection.
6307 need_refresh
[0] = TRUE
;
6308 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6310 wxGridCellCoords ( oldBottom
,
6314 if ( oldTop
< topRow
)
6316 // Refresh the newly selected or deselected
6317 // area above the old or new selection.
6318 need_refresh
[1] = TRUE
;
6319 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6321 wxGridCellCoords ( topRow
- 1,
6325 if ( oldRight
> rightCol
)
6327 // Refresh the newly selected or deselected
6328 // area to the right of the old or new selection.
6329 need_refresh
[2] = TRUE
;
6330 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6332 wxGridCellCoords ( oldBottom
,
6336 if ( oldBottom
> bottomRow
)
6338 // Refresh the newly selected or deselected
6339 // area below the old or new selection.
6340 need_refresh
[3] = TRUE
;
6341 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6343 wxGridCellCoords ( oldBottom
,
6347 // various Refresh() calls
6348 for (i
= 0; i
< 4; i
++ )
6349 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6350 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6353 m_selectingTopLeft
= updateTopLeft
;
6354 m_selectingBottomRight
= updateBottomRight
;
6358 // ------ functions to get/send data (see also public functions)
6361 bool wxGrid::GetModelValues()
6363 // Hide the editor, so it won't hide a changed value.
6364 HideCellEditControl();
6368 // all we need to do is repaint the grid
6370 m_gridWin
->Refresh();
6378 bool wxGrid::SetModelValues()
6382 // Disable the editor, so it won't hide a changed value.
6383 // Do we also want to save the current value of the editor first?
6385 DisableCellEditControl();
6389 for ( row
= 0; row
< m_numRows
; row
++ )
6391 for ( col
= 0; col
< m_numCols
; col
++ )
6393 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6405 // Note - this function only draws cells that are in the list of
6406 // exposed cells (usually set from the update region by
6407 // CalcExposedCells)
6409 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6411 if ( !m_numRows
|| !m_numCols
) return;
6413 int i
, numCells
= cells
.GetCount();
6414 int row
, col
, cell_rows
, cell_cols
;
6415 wxGridCellCoordsArray redrawCells
;
6417 for ( i
= numCells
-1; i
>= 0; i
-- )
6419 row
= cells
[i
].GetRow();
6420 col
= cells
[i
].GetCol();
6421 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6423 // If this cell is part of a multicell block, find owner for repaint
6424 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6426 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6427 bool marked
= FALSE
;
6428 for ( int j
= 0; j
< numCells
; j
++ )
6430 if ( cell
== cells
[j
] )
6438 int count
= redrawCells
.GetCount();
6439 for (int j
= 0; j
< count
; j
++)
6441 if ( cell
== redrawCells
[j
] )
6447 if (!marked
) redrawCells
.Add( cell
);
6449 continue; // don't bother drawing this cell
6452 // If this cell is empty, find cell to left that might want to overflow
6453 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6455 for ( int l
= 0; l
< cell_rows
; l
++ )
6457 // find a cell in this row to left alreay marked for repaint
6459 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6460 if ((redrawCells
[k
].GetCol() < left
) &&
6461 (redrawCells
[k
].GetRow() == row
))
6462 left
=redrawCells
[k
].GetCol();
6464 if (left
== col
) left
= 0; // oh well
6466 for (int j
= col
-1; j
>= left
; j
--)
6468 if (!m_table
->IsEmptyCell(row
+l
, j
))
6470 if (GetCellOverflow(row
+l
, j
))
6472 wxGridCellCoords
cell(row
+l
, j
);
6473 bool marked
= FALSE
;
6475 for (int k
= 0; k
< numCells
; k
++)
6477 if ( cell
== cells
[k
] )
6485 int count
= redrawCells
.GetCount();
6486 for (int k
= 0; k
< count
; k
++)
6488 if ( cell
== redrawCells
[k
] )
6494 if (!marked
) redrawCells
.Add( cell
);
6502 DrawCell( dc
, cells
[i
] );
6505 numCells
= redrawCells
.GetCount();
6507 for ( i
= numCells
- 1; i
>= 0; i
-- )
6509 DrawCell( dc
, redrawCells
[i
] );
6514 void wxGrid::DrawGridSpace( wxDC
& dc
)
6517 m_gridWin
->GetClientSize( &cw
, &ch
);
6520 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6522 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6523 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6525 if ( right
> rightCol
|| bottom
> bottomRow
)
6528 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6530 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6531 dc
.SetPen( *wxTRANSPARENT_PEN
);
6533 if ( right
> rightCol
)
6535 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6538 if ( bottom
> bottomRow
)
6540 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6546 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6548 int row
= coords
.GetRow();
6549 int col
= coords
.GetCol();
6551 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6554 // we draw the cell border ourselves
6555 #if !WXGRID_DRAW_LINES
6556 if ( m_gridLinesEnabled
)
6557 DrawCellBorder( dc
, coords
);
6560 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6562 bool isCurrent
= coords
== m_currentCellCoords
;
6564 wxRect rect
= CellToRect( row
, col
);
6566 // if the editor is shown, we should use it and not the renderer
6567 // Note: However, only if it is really _shown_, i.e. not hidden!
6568 if ( isCurrent
&& IsCellEditControlShown() )
6570 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6571 editor
->PaintBackground(rect
, attr
);
6576 // but all the rest is drawn by the cell renderer and hence may be
6578 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6579 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6586 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6588 int row
= m_currentCellCoords
.GetRow();
6589 int col
= m_currentCellCoords
.GetCol();
6591 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6594 wxRect rect
= CellToRect(row
, col
);
6596 // hmmm... what could we do here to show that the cell is disabled?
6597 // for now, I just draw a thinner border than for the other ones, but
6598 // it doesn't look really good
6600 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6604 // The center of th drawn line is where the position/width/height of
6605 // the rectangle is actually at, (on wxMSW atr least,) so we will
6606 // reduce the size of the rectangle to compensate for the thickness of
6607 // the line. If this is too strange on non wxMSW platforms then
6608 // please #ifdef this appropriately.
6609 rect
.x
+= penWidth
/2;
6610 rect
.y
+= penWidth
/2;
6611 rect
.width
-= penWidth
-1;
6612 rect
.height
-= penWidth
-1;
6615 // Now draw the rectangle
6616 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6617 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6618 dc
.DrawRectangle(rect
);
6622 // VZ: my experiments with 3d borders...
6624 // how to properly set colours for arbitrary bg?
6625 wxCoord x1
= rect
.x
,
6627 x2
= rect
.x
+ rect
.width
-1,
6628 y2
= rect
.y
+ rect
.height
-1;
6630 dc
.SetPen(*wxWHITE_PEN
);
6631 dc
.DrawLine(x1
, y1
, x2
, y1
);
6632 dc
.DrawLine(x1
, y1
, x1
, y2
);
6634 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6635 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6637 dc
.SetPen(*wxBLACK_PEN
);
6638 dc
.DrawLine(x1
, y2
, x2
, y2
);
6639 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6644 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6646 int row
= coords
.GetRow();
6647 int col
= coords
.GetCol();
6648 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6651 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6653 wxRect rect
= CellToRect( row
, col
);
6655 // right hand border
6657 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6658 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6662 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6663 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6666 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6668 // This if block was previously in wxGrid::OnPaint but that doesn't
6669 // seem to get called under wxGTK - MB
6671 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6672 m_numRows
&& m_numCols
)
6674 m_currentCellCoords
.Set(0, 0);
6677 if ( IsCellEditControlShown() )
6679 // don't show highlight when the edit control is shown
6683 // if the active cell was repainted, repaint its highlight too because it
6684 // might have been damaged by the grid lines
6685 size_t count
= cells
.GetCount();
6686 for ( size_t n
= 0; n
< count
; n
++ )
6688 if ( cells
[n
] == m_currentCellCoords
)
6690 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6691 DrawCellHighlight(dc
, attr
);
6699 // TODO: remove this ???
6700 // This is used to redraw all grid lines e.g. when the grid line colour
6703 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6705 #if !WXGRID_DRAW_LINES
6709 if ( !m_gridLinesEnabled
||
6711 !m_numCols
) return;
6713 int top
, bottom
, left
, right
;
6715 #if 0 //#ifndef __WXGTK__
6719 m_gridWin
->GetClientSize(&cw
, &ch
);
6721 // virtual coords of visible area
6723 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6724 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6729 reg
.GetBox(x
, y
, w
, h
);
6730 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6731 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6735 m_gridWin
->GetClientSize(&cw
, &ch
);
6736 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6737 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6740 // avoid drawing grid lines past the last row and col
6742 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6743 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6745 // no gridlines inside multicells, clip them out
6746 int leftCol
= XToCol(left
);
6747 int topRow
= YToRow(top
);
6748 int rightCol
= XToCol(right
);
6749 int bottomRow
= YToRow(bottom
);
6750 wxRegion
clippedcells(0, 0, cw
, ch
);
6752 if ((leftCol
>= 0) && (topRow
>= 0))
6754 if (rightCol
< 0) rightCol
= m_numCols
;
6755 if (bottomRow
< 0) bottomRow
= m_numRows
;
6757 int i
, j
, cell_rows
, cell_cols
;
6760 for (j
=topRow
; j
<bottomRow
; j
++)
6762 for (i
=leftCol
; i
<rightCol
; i
++)
6764 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
6765 if ((cell_rows
> 1) || (cell_cols
> 1))
6767 rect
= CellToRect(j
,i
);
6768 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6769 clippedcells
.Subtract(rect
);
6771 else if ((cell_rows
< 0) || (cell_cols
< 0))
6773 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
6774 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6775 clippedcells
.Subtract(rect
);
6780 dc
.SetClippingRegion( clippedcells
);
6782 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6784 // horizontal grid lines
6787 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6789 int bot
= GetRowBottom(i
) - 1;
6798 dc
.DrawLine( left
, bot
, right
, bot
);
6803 // vertical grid lines
6805 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6807 int colRight
= GetColRight(i
) - 1;
6808 if ( colRight
> right
)
6813 if ( colRight
>= left
)
6815 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6818 dc
.DestroyClippingRegion();
6822 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6824 if ( !m_numRows
) return;
6827 size_t numLabels
= rows
.GetCount();
6829 for ( i
= 0; i
< numLabels
; i
++ )
6831 DrawRowLabel( dc
, rows
[i
] );
6836 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6838 if ( GetRowHeight(row
) <= 0 )
6841 int rowTop
= GetRowTop(row
),
6842 rowBottom
= GetRowBottom(row
) - 1;
6844 dc
.SetPen( *wxBLACK_PEN
);
6845 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6846 m_rowLabelWidth
-1, rowBottom
);
6848 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6850 dc
.SetPen( *wxWHITE_PEN
);
6851 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6852 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6854 dc
.SetBackgroundMode( wxTRANSPARENT
);
6855 dc
.SetTextForeground( GetLabelTextColour() );
6856 dc
.SetFont( GetLabelFont() );
6859 GetRowLabelAlignment( &hAlign
, &vAlign
);
6863 rect
.SetY( GetRowTop(row
) + 2 );
6864 rect
.SetWidth( m_rowLabelWidth
- 4 );
6865 rect
.SetHeight( GetRowHeight(row
) - 4 );
6866 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6870 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6872 if ( !m_numCols
) return;
6875 size_t numLabels
= cols
.GetCount();
6877 for ( i
= 0; i
< numLabels
; i
++ )
6879 DrawColLabel( dc
, cols
[i
] );
6884 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6886 if ( GetColWidth(col
) <= 0 )
6889 int colLeft
= GetColLeft(col
),
6890 colRight
= GetColRight(col
) - 1;
6892 dc
.SetPen( *wxBLACK_PEN
);
6893 dc
.DrawLine( colRight
, 0,
6894 colRight
, m_colLabelHeight
-1 );
6896 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6897 colRight
, m_colLabelHeight
-1 );
6899 dc
.SetPen( *wxWHITE_PEN
);
6900 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6901 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6903 dc
.SetBackgroundMode( wxTRANSPARENT
);
6904 dc
.SetTextForeground( GetLabelTextColour() );
6905 dc
.SetFont( GetLabelFont() );
6907 dc
.SetBackgroundMode( wxTRANSPARENT
);
6908 dc
.SetTextForeground( GetLabelTextColour() );
6909 dc
.SetFont( GetLabelFont() );
6912 GetColLabelAlignment( &hAlign
, &vAlign
);
6915 rect
.SetX( colLeft
+ 2 );
6917 rect
.SetWidth( GetColWidth(col
) - 4 );
6918 rect
.SetHeight( m_colLabelHeight
- 4 );
6919 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6922 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6923 const wxString
& value
,
6928 wxArrayString lines
;
6930 StringToLines( value
, lines
);
6933 //Forward to new API.
6934 DrawTextRectangle( dc
,
6942 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6943 const wxArrayString
& lines
,
6948 long textWidth
, textHeight
;
6949 long lineWidth
, lineHeight
;
6951 dc
.SetClippingRegion( rect
);
6952 if ( lines
.GetCount() )
6954 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6955 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6958 switch ( horizAlign
)
6961 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6964 case wxALIGN_CENTRE
:
6965 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6974 switch ( vertAlign
)
6976 case wxALIGN_BOTTOM
:
6977 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6980 case wxALIGN_CENTRE
:
6981 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6990 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6992 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6997 dc
.DestroyClippingRegion();
7001 // Split multi line text up into an array of strings. Any existing
7002 // contents of the string array are preserved.
7004 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7008 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7009 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7011 while ( startPos
< (int)tVal
.Length() )
7013 pos
= tVal
.Mid(startPos
).Find( eol
);
7018 else if ( pos
== 0 )
7020 lines
.Add( wxEmptyString
);
7024 lines
.Add( value
.Mid(startPos
, pos
) );
7028 if ( startPos
< (int)value
.Length() )
7030 lines
.Add( value
.Mid( startPos
) );
7035 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7036 const wxArrayString
& lines
,
7037 long *width
, long *height
)
7044 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7046 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7047 w
= wxMax( w
, lineW
);
7056 // ------ Batch processing.
7058 void wxGrid::EndBatch()
7060 if ( m_batchCount
> 0 )
7063 if ( !m_batchCount
)
7066 m_rowLabelWin
->Refresh();
7067 m_colLabelWin
->Refresh();
7068 m_cornerLabelWin
->Refresh();
7069 m_gridWin
->Refresh();
7074 // Use this, rather than wxWindow::Refresh(), to force an immediate
7075 // repainting of the grid. Has no effect if you are already inside a
7076 // BeginBatch / EndBatch block.
7078 void wxGrid::ForceRefresh()
7086 // ------ Edit control functions
7090 void wxGrid::EnableEditing( bool edit
)
7092 // TODO: improve this ?
7094 if ( edit
!= m_editable
)
7096 if(!edit
) EnableCellEditControl(edit
);
7102 void wxGrid::EnableCellEditControl( bool enable
)
7107 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7108 SetCurrentCell( 0, 0 );
7110 if ( enable
!= m_cellEditCtrlEnabled
)
7114 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7117 // this should be checked by the caller!
7118 wxASSERT_MSG( CanEnableCellControl(),
7119 _T("can't enable editing for this cell!") );
7121 // do it before ShowCellEditControl()
7122 m_cellEditCtrlEnabled
= enable
;
7124 ShowCellEditControl();
7128 //FIXME:add veto support
7129 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7131 HideCellEditControl();
7132 SaveEditControlValue();
7134 // do it after HideCellEditControl()
7135 m_cellEditCtrlEnabled
= enable
;
7140 bool wxGrid::IsCurrentCellReadOnly() const
7143 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7144 bool readonly
= attr
->IsReadOnly();
7150 bool wxGrid::CanEnableCellControl() const
7152 return m_editable
&& !IsCurrentCellReadOnly();
7155 bool wxGrid::IsCellEditControlEnabled() const
7157 // the cell edit control might be disable for all cells or just for the
7158 // current one if it's read only
7159 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7162 bool wxGrid::IsCellEditControlShown() const
7164 bool isShown
= FALSE
;
7166 if ( m_cellEditCtrlEnabled
)
7168 int row
= m_currentCellCoords
.GetRow();
7169 int col
= m_currentCellCoords
.GetCol();
7170 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7171 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7176 if ( editor
->IsCreated() )
7178 isShown
= editor
->GetControl()->IsShown();
7188 void wxGrid::ShowCellEditControl()
7190 if ( IsCellEditControlEnabled() )
7192 if ( !IsVisible( m_currentCellCoords
) )
7194 m_cellEditCtrlEnabled
= FALSE
;
7199 wxRect rect
= CellToRect( m_currentCellCoords
);
7200 int row
= m_currentCellCoords
.GetRow();
7201 int col
= m_currentCellCoords
.GetCol();
7203 // if this is part of a multicell, find owner (topleft)
7204 int cell_rows
, cell_cols
;
7205 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7206 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7210 m_currentCellCoords
.SetRow( row
);
7211 m_currentCellCoords
.SetCol( col
);
7214 // convert to scrolled coords
7216 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7218 // done in PaintBackground()
7220 // erase the highlight and the cell contents because the editor
7221 // might not cover the entire cell
7222 wxClientDC
dc( m_gridWin
);
7224 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7225 dc
.SetPen(*wxTRANSPARENT_PEN
);
7226 dc
.DrawRectangle(rect
);
7229 // cell is shifted by one pixel
7230 // However, don't allow x or y to become negative
7231 // since the SetSize() method interprets that as
7238 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7239 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7240 if ( !editor
->IsCreated() )
7242 editor
->Create(m_gridWin
, -1,
7243 new wxGridCellEditorEvtHandler(this, editor
));
7245 wxGridEditorCreatedEvent
evt(GetId(),
7246 wxEVT_GRID_EDITOR_CREATED
,
7250 editor
->GetControl());
7251 GetEventHandler()->ProcessEvent(evt
);
7254 editor
->Show( TRUE
, attr
);
7256 // resize editor to overflow into righthand cells if allowed
7257 wxCoord maxRight
= rect
.width
;
7258 wxString value
= GetCellValue(row
, col
);
7259 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7261 wxClientDC
dc(m_gridWin
);
7263 dc
.SetFont(attr
->GetFont());
7264 dc
.GetTextExtent(value
, &maxRight
, &y
);
7265 if (maxRight
> m_gridWin
->GetClientSize().GetWidth())
7266 maxRight
= m_gridWin
->GetClientSize().GetWidth();
7268 int cell_rows
, cell_cols
;
7269 attr
->GetSize( &cell_rows
, &cell_cols
);
7271 if ((maxRight
> rect
.width
) && (col
< m_numCols
) && m_table
)
7274 for (i
= col
+cell_cols
; i
< m_numCols
; i
++)
7277 // looks weird going over a multicell
7278 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7279 if (m_table
->IsEmptyCell(row
,i
) && (rect
.GetRight() < maxRight
) && (c_rows
== 1))
7280 rect
.width
+= GetColWidth(i
);
7284 if (rect
.GetRight() > maxRight
) rect
.SetRight(maxRight
-1);
7287 editor
->SetSize( rect
);
7289 editor
->BeginEdit(row
, col
, this);
7298 void wxGrid::HideCellEditControl()
7300 if ( IsCellEditControlEnabled() )
7302 int row
= m_currentCellCoords
.GetRow();
7303 int col
= m_currentCellCoords
.GetCol();
7305 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7306 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7307 editor
->Show( FALSE
);
7310 m_gridWin
->SetFocus();
7311 // refresh whole row to the right
7312 wxRect
rect( CellToRect(row
, col
) );
7313 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7314 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7315 m_gridWin
->Refresh( FALSE
, &rect
);
7320 void wxGrid::SaveEditControlValue()
7322 if ( IsCellEditControlEnabled() )
7324 int row
= m_currentCellCoords
.GetRow();
7325 int col
= m_currentCellCoords
.GetCol();
7327 wxString oldval
= GetCellValue(row
,col
);
7329 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7330 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7331 bool changed
= editor
->EndEdit(row
, col
, this);
7338 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7339 m_currentCellCoords
.GetRow(),
7340 m_currentCellCoords
.GetCol() ) < 0 ) {
7342 // Event has been vetoed, set the data back.
7343 SetCellValue(row
,col
,oldval
);
7351 // ------ Grid location functions
7352 // Note that all of these functions work with the logical coordinates of
7353 // grid cells and labels so you will need to convert from device
7354 // coordinates for mouse events etc.
7357 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7359 int row
= YToRow(y
);
7360 int col
= XToCol(x
);
7362 if ( row
== -1 || col
== -1 )
7364 coords
= wxGridNoCellCoords
;
7368 coords
.Set( row
, col
);
7373 // Internal Helper function for computing row or column from some
7374 // (unscrolled) coordinate value, using either
7375 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7376 // of m_rowBottoms/m_ColRights to speed up the search!
7378 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7379 const wxArrayInt
& BorderArray
, int nMax
,
7384 size_t i_max
= coord
/ defaultDist
,
7387 if (BorderArray
.IsEmpty())
7389 if((int) i_max
< nMax
)
7391 return maxOnOverflow
? nMax
- 1 : -1;
7394 if ( i_max
>= BorderArray
.GetCount())
7395 i_max
= BorderArray
.GetCount() - 1;
7398 if ( coord
>= BorderArray
[i_max
])
7401 i_max
= coord
/ minDist
;
7403 if ( i_max
>= BorderArray
.GetCount())
7404 i_max
= BorderArray
.GetCount() - 1;
7406 if ( coord
>= BorderArray
[i_max
])
7407 return maxOnOverflow
? (int)i_max
: -1;
7408 if ( coord
< BorderArray
[0] )
7411 while ( i_max
- i_min
> 0 )
7413 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7414 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7415 if (coord
>= BorderArray
[ i_max
- 1])
7419 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7420 if (coord
< BorderArray
[median
])
7428 int wxGrid::YToRow( int y
)
7430 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7431 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, m_numRows
, FALSE
);
7435 int wxGrid::XToCol( int x
)
7437 return CoordToRowOrCol(x
, m_defaultColWidth
,
7438 WXGRID_MIN_COL_WIDTH
, m_colRights
, m_numCols
, FALSE
);
7442 // return the row number that that the y coord is near the edge of, or
7443 // -1 if not near an edge
7445 int wxGrid::YToEdgeOfRow( int y
)
7448 i
= internalYToRow(y
);
7450 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7452 // We know that we are in row i, test whether we are
7453 // close enough to lower or upper border, respectively.
7454 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7456 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7464 // return the col number that that the x coord is near the edge of, or
7465 // -1 if not near an edge
7467 int wxGrid::XToEdgeOfCol( int x
)
7470 i
= internalXToCol(x
);
7472 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7474 // We know that we are in column i, test whether we are
7475 // close enough to right or left border, respectively.
7476 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7478 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7486 wxRect
wxGrid::CellToRect( int row
, int col
)
7488 wxRect
rect( -1, -1, -1, -1 );
7490 if ( row
>= 0 && row
< m_numRows
&&
7491 col
>= 0 && col
< m_numCols
)
7493 int i
, cell_rows
, cell_cols
;
7494 rect
.width
= rect
.height
= 0;
7495 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7496 // if negative then find multicell owner
7497 if (cell_rows
< 0) row
+= cell_rows
;
7498 if (cell_cols
< 0) col
+= cell_cols
;
7499 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7501 rect
.x
= GetColLeft(col
);
7502 rect
.y
= GetRowTop(row
);
7503 for (i
=col
; i
<col
+cell_cols
; i
++)
7504 rect
.width
+= GetColWidth(i
);
7505 for (i
=row
; i
<row
+cell_rows
; i
++)
7506 rect
.height
+= GetRowHeight(i
);
7509 // if grid lines are enabled, then the area of the cell is a bit smaller
7510 if (m_gridLinesEnabled
) {
7518 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7520 // get the cell rectangle in logical coords
7522 wxRect
r( CellToRect( row
, col
) );
7524 // convert to device coords
7526 int left
, top
, right
, bottom
;
7527 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7528 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7530 // check against the client area of the grid window
7533 m_gridWin
->GetClientSize( &cw
, &ch
);
7535 if ( wholeCellVisible
)
7537 // is the cell wholly visible ?
7539 return ( left
>= 0 && right
<= cw
&&
7540 top
>= 0 && bottom
<= ch
);
7544 // is the cell partly visible ?
7546 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7547 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7552 // make the specified cell location visible by doing a minimal amount
7555 void wxGrid::MakeCellVisible( int row
, int col
)
7559 int xpos
= -1, ypos
= -1;
7561 if ( row
>= 0 && row
< m_numRows
&&
7562 col
>= 0 && col
< m_numCols
)
7564 // get the cell rectangle in logical coords
7566 wxRect
r( CellToRect( row
, col
) );
7568 // convert to device coords
7570 int left
, top
, right
, bottom
;
7571 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7572 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7575 m_gridWin
->GetClientSize( &cw
, &ch
);
7581 else if ( bottom
> ch
)
7583 int h
= r
.GetHeight();
7585 for ( i
= row
-1; i
>= 0; i
-- )
7587 int rowHeight
= GetRowHeight(i
);
7588 if ( h
+ rowHeight
> ch
)
7595 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7596 // have rounding errors (this is important, because if we do, we
7597 // might not scroll at all and some cells won't be redrawn)
7599 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7601 ypos
+= GRID_SCROLL_LINE_Y
;
7608 else if ( right
> cw
)
7610 int w
= r
.GetWidth();
7612 for ( i
= col
-1; i
>= 0; i
-- )
7614 int colWidth
= GetColWidth(i
);
7615 if ( w
+ colWidth
> cw
)
7622 // see comment for ypos above
7623 xpos
+= GRID_SCROLL_LINE_X
;
7626 if ( xpos
!= -1 || ypos
!= -1 )
7629 xpos
/= GRID_SCROLL_LINE_X
;
7631 ypos
/= GRID_SCROLL_LINE_Y
;
7632 Scroll( xpos
, ypos
);
7640 // ------ Grid cursor movement functions
7643 bool wxGrid::MoveCursorUp( bool expandSelection
)
7645 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7646 m_currentCellCoords
.GetRow() >= 0 )
7648 if ( expandSelection
)
7650 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7651 m_selectingKeyboard
= m_currentCellCoords
;
7652 if ( m_selectingKeyboard
.GetRow() > 0 )
7654 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7655 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7656 m_selectingKeyboard
.GetCol() );
7657 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7660 else if ( m_currentCellCoords
.GetRow() > 0 )
7663 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7664 m_currentCellCoords
.GetCol() );
7665 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7666 m_currentCellCoords
.GetCol() );
7677 bool wxGrid::MoveCursorDown( bool expandSelection
)
7679 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7680 m_currentCellCoords
.GetRow() < m_numRows
)
7682 if ( expandSelection
)
7684 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7685 m_selectingKeyboard
= m_currentCellCoords
;
7686 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7688 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7689 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7690 m_selectingKeyboard
.GetCol() );
7691 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7694 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7697 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7698 m_currentCellCoords
.GetCol() );
7699 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7700 m_currentCellCoords
.GetCol() );
7711 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7713 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7714 m_currentCellCoords
.GetCol() >= 0 )
7716 if ( expandSelection
)
7718 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7719 m_selectingKeyboard
= m_currentCellCoords
;
7720 if ( m_selectingKeyboard
.GetCol() > 0 )
7722 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7723 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7724 m_selectingKeyboard
.GetCol() );
7725 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7728 else if ( m_currentCellCoords
.GetCol() > 0 )
7731 MakeCellVisible( m_currentCellCoords
.GetRow(),
7732 m_currentCellCoords
.GetCol() - 1 );
7733 SetCurrentCell( m_currentCellCoords
.GetRow(),
7734 m_currentCellCoords
.GetCol() - 1 );
7745 bool wxGrid::MoveCursorRight( bool expandSelection
)
7747 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7748 m_currentCellCoords
.GetCol() < m_numCols
)
7750 if ( expandSelection
)
7752 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7753 m_selectingKeyboard
= m_currentCellCoords
;
7754 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7756 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7757 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7758 m_selectingKeyboard
.GetCol() );
7759 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7762 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7765 MakeCellVisible( m_currentCellCoords
.GetRow(),
7766 m_currentCellCoords
.GetCol() + 1 );
7767 SetCurrentCell( m_currentCellCoords
.GetRow(),
7768 m_currentCellCoords
.GetCol() + 1 );
7779 bool wxGrid::MovePageUp()
7781 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7783 int row
= m_currentCellCoords
.GetRow();
7787 m_gridWin
->GetClientSize( &cw
, &ch
);
7789 int y
= GetRowTop(row
);
7790 int newRow
= YToRow( y
- ch
+ 1 );
7795 else if ( newRow
== row
)
7800 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7801 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7809 bool wxGrid::MovePageDown()
7811 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7813 int row
= m_currentCellCoords
.GetRow();
7814 if ( row
< m_numRows
)
7817 m_gridWin
->GetClientSize( &cw
, &ch
);
7819 int y
= GetRowTop(row
);
7820 int newRow
= YToRow( y
+ ch
);
7823 newRow
= m_numRows
- 1;
7825 else if ( newRow
== row
)
7830 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7831 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7839 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7842 m_currentCellCoords
!= wxGridNoCellCoords
&&
7843 m_currentCellCoords
.GetRow() > 0 )
7845 int row
= m_currentCellCoords
.GetRow();
7846 int col
= m_currentCellCoords
.GetCol();
7848 if ( m_table
->IsEmptyCell(row
, col
) )
7850 // starting in an empty cell: find the next block of
7856 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7859 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7861 // starting at the top of a block: find the next block
7867 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7872 // starting within a block: find the top of the block
7877 if ( m_table
->IsEmptyCell(row
, col
) )
7885 MakeCellVisible( row
, col
);
7886 if ( expandSelection
)
7888 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7889 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7894 SetCurrentCell( row
, col
);
7902 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7905 m_currentCellCoords
!= wxGridNoCellCoords
&&
7906 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7908 int row
= m_currentCellCoords
.GetRow();
7909 int col
= m_currentCellCoords
.GetCol();
7911 if ( m_table
->IsEmptyCell(row
, col
) )
7913 // starting in an empty cell: find the next block of
7916 while ( row
< m_numRows
-1 )
7919 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7922 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7924 // starting at the bottom of a block: find the next block
7927 while ( row
< m_numRows
-1 )
7930 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7935 // starting within a block: find the bottom of the block
7937 while ( row
< m_numRows
-1 )
7940 if ( m_table
->IsEmptyCell(row
, col
) )
7948 MakeCellVisible( row
, col
);
7949 if ( expandSelection
)
7951 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7952 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7957 SetCurrentCell( row
, col
);
7966 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7969 m_currentCellCoords
!= wxGridNoCellCoords
&&
7970 m_currentCellCoords
.GetCol() > 0 )
7972 int row
= m_currentCellCoords
.GetRow();
7973 int col
= m_currentCellCoords
.GetCol();
7975 if ( m_table
->IsEmptyCell(row
, col
) )
7977 // starting in an empty cell: find the next block of
7983 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7986 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7988 // starting at the left of a block: find the next block
7994 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7999 // starting within a block: find the left of the block
8004 if ( m_table
->IsEmptyCell(row
, col
) )
8012 MakeCellVisible( row
, col
);
8013 if ( expandSelection
)
8015 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8016 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8021 SetCurrentCell( row
, col
);
8030 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8033 m_currentCellCoords
!= wxGridNoCellCoords
&&
8034 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8036 int row
= m_currentCellCoords
.GetRow();
8037 int col
= m_currentCellCoords
.GetCol();
8039 if ( m_table
->IsEmptyCell(row
, col
) )
8041 // starting in an empty cell: find the next block of
8044 while ( col
< m_numCols
-1 )
8047 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8050 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8052 // starting at the right of a block: find the next block
8055 while ( col
< m_numCols
-1 )
8058 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8063 // starting within a block: find the right of the block
8065 while ( col
< m_numCols
-1 )
8068 if ( m_table
->IsEmptyCell(row
, col
) )
8076 MakeCellVisible( row
, col
);
8077 if ( expandSelection
)
8079 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8080 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8085 SetCurrentCell( row
, col
);
8097 // ------ Label values and formatting
8100 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8102 *horiz
= m_rowLabelHorizAlign
;
8103 *vert
= m_rowLabelVertAlign
;
8106 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8108 *horiz
= m_colLabelHorizAlign
;
8109 *vert
= m_colLabelVertAlign
;
8112 wxString
wxGrid::GetRowLabelValue( int row
)
8116 return m_table
->GetRowLabelValue( row
);
8126 wxString
wxGrid::GetColLabelValue( int col
)
8130 return m_table
->GetColLabelValue( col
);
8141 void wxGrid::SetRowLabelSize( int width
)
8143 width
= wxMax( width
, 0 );
8144 if ( width
!= m_rowLabelWidth
)
8148 m_rowLabelWin
->Show( FALSE
);
8149 m_cornerLabelWin
->Show( FALSE
);
8151 else if ( m_rowLabelWidth
== 0 )
8153 m_rowLabelWin
->Show( TRUE
);
8154 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8157 m_rowLabelWidth
= width
;
8159 wxScrolledWindow::Refresh( TRUE
);
8164 void wxGrid::SetColLabelSize( int height
)
8166 height
= wxMax( height
, 0 );
8167 if ( height
!= m_colLabelHeight
)
8171 m_colLabelWin
->Show( FALSE
);
8172 m_cornerLabelWin
->Show( FALSE
);
8174 else if ( m_colLabelHeight
== 0 )
8176 m_colLabelWin
->Show( TRUE
);
8177 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8180 m_colLabelHeight
= height
;
8182 wxScrolledWindow::Refresh( TRUE
);
8187 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8189 if ( m_labelBackgroundColour
!= colour
)
8191 m_labelBackgroundColour
= colour
;
8192 m_rowLabelWin
->SetBackgroundColour( colour
);
8193 m_colLabelWin
->SetBackgroundColour( colour
);
8194 m_cornerLabelWin
->SetBackgroundColour( colour
);
8196 if ( !GetBatchCount() )
8198 m_rowLabelWin
->Refresh();
8199 m_colLabelWin
->Refresh();
8200 m_cornerLabelWin
->Refresh();
8205 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8207 if ( m_labelTextColour
!= colour
)
8209 m_labelTextColour
= colour
;
8210 if ( !GetBatchCount() )
8212 m_rowLabelWin
->Refresh();
8213 m_colLabelWin
->Refresh();
8218 void wxGrid::SetLabelFont( const wxFont
& font
)
8221 if ( !GetBatchCount() )
8223 m_rowLabelWin
->Refresh();
8224 m_colLabelWin
->Refresh();
8228 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8230 // allow old (incorrect) defs to be used
8233 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8234 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8235 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8240 case wxTOP
: vert
= wxALIGN_TOP
; break;
8241 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8242 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8245 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8247 m_rowLabelHorizAlign
= horiz
;
8250 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8252 m_rowLabelVertAlign
= vert
;
8255 if ( !GetBatchCount() )
8257 m_rowLabelWin
->Refresh();
8261 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8263 // allow old (incorrect) defs to be used
8266 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8267 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8268 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8273 case wxTOP
: vert
= wxALIGN_TOP
; break;
8274 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8275 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8278 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8280 m_colLabelHorizAlign
= horiz
;
8283 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8285 m_colLabelVertAlign
= vert
;
8288 if ( !GetBatchCount() )
8290 m_colLabelWin
->Refresh();
8294 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8298 m_table
->SetRowLabelValue( row
, s
);
8299 if ( !GetBatchCount() )
8301 wxRect rect
= CellToRect( row
, 0);
8302 if ( rect
.height
> 0 )
8304 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8306 rect
.width
= m_rowLabelWidth
;
8307 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8313 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8317 m_table
->SetColLabelValue( col
, s
);
8318 if ( !GetBatchCount() )
8320 wxRect rect
= CellToRect( 0, col
);
8321 if ( rect
.width
> 0 )
8323 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8325 rect
.height
= m_colLabelHeight
;
8326 m_colLabelWin
->Refresh( TRUE
, &rect
);
8332 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8334 if ( m_gridLineColour
!= colour
)
8336 m_gridLineColour
= colour
;
8338 wxClientDC
dc( m_gridWin
);
8340 DrawAllGridLines( dc
, wxRegion() );
8345 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8347 if ( m_cellHighlightColour
!= colour
)
8349 m_cellHighlightColour
= colour
;
8351 wxClientDC
dc( m_gridWin
);
8353 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8354 DrawCellHighlight(dc
, attr
);
8359 void wxGrid::SetCellHighlightPenWidth(int width
)
8361 if (m_cellHighlightPenWidth
!= width
) {
8362 m_cellHighlightPenWidth
= width
;
8364 // Just redrawing the cell highlight is not enough since that won't
8365 // make any visible change if the the thickness is getting smaller.
8366 int row
= m_currentCellCoords
.GetRow();
8367 int col
= m_currentCellCoords
.GetCol();
8368 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8370 wxRect rect
= CellToRect(row
, col
);
8371 m_gridWin
->Refresh(TRUE
, &rect
);
8375 void wxGrid::SetCellHighlightROPenWidth(int width
)
8377 if (m_cellHighlightROPenWidth
!= width
) {
8378 m_cellHighlightROPenWidth
= width
;
8380 // Just redrawing the cell highlight is not enough since that won't
8381 // make any visible change if the the thickness is getting smaller.
8382 int row
= m_currentCellCoords
.GetRow();
8383 int col
= m_currentCellCoords
.GetCol();
8384 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8386 wxRect rect
= CellToRect(row
, col
);
8387 m_gridWin
->Refresh(TRUE
, &rect
);
8391 void wxGrid::EnableGridLines( bool enable
)
8393 if ( enable
!= m_gridLinesEnabled
)
8395 m_gridLinesEnabled
= enable
;
8397 if ( !GetBatchCount() )
8401 wxClientDC
dc( m_gridWin
);
8403 DrawAllGridLines( dc
, wxRegion() );
8407 m_gridWin
->Refresh();
8414 int wxGrid::GetDefaultRowSize()
8416 return m_defaultRowHeight
;
8419 int wxGrid::GetRowSize( int row
)
8421 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8423 return GetRowHeight(row
);
8426 int wxGrid::GetDefaultColSize()
8428 return m_defaultColWidth
;
8431 int wxGrid::GetColSize( int col
)
8433 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8435 return GetColWidth(col
);
8438 // ============================================================================
8439 // access to the grid attributes: each of them has a default value in the grid
8440 // itself and may be overidden on a per-cell basis
8441 // ============================================================================
8443 // ----------------------------------------------------------------------------
8444 // setting default attributes
8445 // ----------------------------------------------------------------------------
8447 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8449 m_defaultCellAttr
->SetBackgroundColour(col
);
8451 m_gridWin
->SetBackgroundColour(col
);
8455 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8457 m_defaultCellAttr
->SetTextColour(col
);
8460 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8462 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8465 void wxGrid::SetDefaultCellOverflow( bool allow
)
8467 m_defaultCellAttr
->SetOverflow(allow
);
8470 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8472 m_defaultCellAttr
->SetFont(font
);
8475 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8477 m_defaultCellAttr
->SetRenderer(renderer
);
8480 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8482 m_defaultCellAttr
->SetEditor(editor
);
8485 // ----------------------------------------------------------------------------
8486 // access to the default attrbiutes
8487 // ----------------------------------------------------------------------------
8489 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8491 return m_defaultCellAttr
->GetBackgroundColour();
8494 wxColour
wxGrid::GetDefaultCellTextColour()
8496 return m_defaultCellAttr
->GetTextColour();
8499 wxFont
wxGrid::GetDefaultCellFont()
8501 return m_defaultCellAttr
->GetFont();
8504 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8506 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8509 bool wxGrid::GetDefaultCellOverflow()
8511 return m_defaultCellAttr
->GetOverflow();
8514 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8516 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8519 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8521 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8524 // ----------------------------------------------------------------------------
8525 // access to cell attributes
8526 // ----------------------------------------------------------------------------
8528 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8530 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8531 wxColour colour
= attr
->GetBackgroundColour();
8536 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8538 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8539 wxColour colour
= attr
->GetTextColour();
8544 wxFont
wxGrid::GetCellFont( int row
, int col
)
8546 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8547 wxFont font
= attr
->GetFont();
8552 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8554 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8555 attr
->GetAlignment(horiz
, vert
);
8559 bool wxGrid::GetCellOverflow( int row
, int col
)
8561 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8562 bool allow
= attr
->GetOverflow();
8567 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8569 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8570 attr
->GetSize( num_rows
, num_cols
);
8574 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8576 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8577 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8583 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8585 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8586 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8592 bool wxGrid::IsReadOnly(int row
, int col
) const
8594 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8595 bool isReadOnly
= attr
->IsReadOnly();
8600 // ----------------------------------------------------------------------------
8601 // attribute support: cache, automatic provider creation, ...
8602 // ----------------------------------------------------------------------------
8604 bool wxGrid::CanHaveAttributes()
8611 return m_table
->CanHaveAttributes();
8614 void wxGrid::ClearAttrCache()
8616 if ( m_attrCache
.row
!= -1 )
8618 wxSafeDecRef(m_attrCache
.attr
);
8619 m_attrCache
.attr
= NULL
;
8620 m_attrCache
.row
= -1;
8624 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8626 wxGrid
*self
= (wxGrid
*)this; // const_cast
8628 self
->ClearAttrCache();
8629 self
->m_attrCache
.row
= row
;
8630 self
->m_attrCache
.col
= col
;
8631 self
->m_attrCache
.attr
= attr
;
8635 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8637 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8639 *attr
= m_attrCache
.attr
;
8640 wxSafeIncRef(m_attrCache
.attr
);
8642 #ifdef DEBUG_ATTR_CACHE
8643 gs_nAttrCacheHits
++;
8650 #ifdef DEBUG_ATTR_CACHE
8651 gs_nAttrCacheMisses
++;
8657 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8659 wxGridCellAttr
*attr
= NULL
;
8660 // Additional test to avoid looking at the cache e.g. for
8661 // wxNoCellCoords, as this will confuse memory management.
8664 if ( !LookupAttr(row
, col
, &attr
) )
8666 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
8667 : (wxGridCellAttr
*)NULL
;
8668 CacheAttr(row
, col
, attr
);
8673 attr
->SetDefAttr(m_defaultCellAttr
);
8677 attr
= m_defaultCellAttr
;
8684 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8686 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8688 wxCHECK_MSG( m_table
, attr
,
8689 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8691 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8694 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8696 // artificially inc the ref count to match DecRef() in caller
8698 m_table
->SetAttr(attr
, row
, col
);
8704 // ----------------------------------------------------------------------------
8705 // setting column attributes (wrappers around SetColAttr)
8706 // ----------------------------------------------------------------------------
8708 void wxGrid::SetColFormatBool(int col
)
8710 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8713 void wxGrid::SetColFormatNumber(int col
)
8715 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8718 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8720 wxString typeName
= wxGRID_VALUE_FLOAT
;
8721 if ( (width
!= -1) || (precision
!= -1) )
8723 typeName
<< _T(':') << width
<< _T(',') << precision
;
8726 SetColFormatCustom(col
, typeName
);
8729 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8731 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8733 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8735 attr
= new wxGridCellAttr
;
8736 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8737 attr
->SetRenderer(renderer
);
8739 SetColAttr(col
, attr
);
8743 // ----------------------------------------------------------------------------
8744 // setting cell attributes: this is forwarded to the table
8745 // ----------------------------------------------------------------------------
8747 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
8749 if ( CanHaveAttributes() )
8751 m_table
->SetAttr(attr
, row
, col
);
8760 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8762 if ( CanHaveAttributes() )
8764 m_table
->SetRowAttr(attr
, row
);
8773 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8775 if ( CanHaveAttributes() )
8777 m_table
->SetColAttr(attr
, col
);
8786 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8788 if ( CanHaveAttributes() )
8790 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8791 attr
->SetBackgroundColour(colour
);
8796 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8798 if ( CanHaveAttributes() )
8800 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8801 attr
->SetTextColour(colour
);
8806 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8808 if ( CanHaveAttributes() )
8810 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8811 attr
->SetFont(font
);
8816 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8818 if ( CanHaveAttributes() )
8820 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8821 attr
->SetAlignment(horiz
, vert
);
8826 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
8828 if ( CanHaveAttributes() )
8830 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8831 attr
->SetOverflow(allow
);
8836 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
8838 if ( CanHaveAttributes() )
8840 int cell_rows
, cell_cols
;
8842 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8843 attr
->GetSize(&cell_rows
, &cell_cols
);
8844 attr
->SetSize(num_rows
, num_cols
);
8847 // Cannot set the size of a cell to 0 or negative values
8848 // While it is perfectly legal to do that, this function cannot
8849 // handle all the possibilies, do it by hand by getting the CellAttr.
8850 // You can only set the size of a cell to 1,1 or greater with this fn
8851 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
8852 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
8853 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
8854 wxT("wxGrid::SetCellSize setting cell size to < 1"));
8856 // if this was already a multicell then "turn off" the other cells first
8857 if ((cell_rows
> 1) || (cell_rows
> 1))
8860 for (j
=row
; j
<row
+cell_rows
; j
++)
8862 for (i
=col
; i
<col
+cell_cols
; i
++)
8864 if ((i
!= col
) || (j
!= row
))
8866 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8867 attr_stub
->SetSize( 1, 1 );
8868 attr_stub
->DecRef();
8874 // mark the cells that will be covered by this cell to
8875 // negative or zero values to point back at this cell
8876 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
8879 for (j
=row
; j
<row
+num_rows
; j
++)
8881 for (i
=col
; i
<col
+num_cols
; i
++)
8883 if ((i
!= col
) || (j
!= row
))
8885 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
8886 attr_stub
->SetSize( row
-j
, col
-i
);
8887 attr_stub
->DecRef();
8895 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8897 if ( CanHaveAttributes() )
8899 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8900 attr
->SetRenderer(renderer
);
8905 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8907 if ( CanHaveAttributes() )
8909 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8910 attr
->SetEditor(editor
);
8915 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8917 if ( CanHaveAttributes() )
8919 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8920 attr
->SetReadOnly(isReadOnly
);
8925 // ----------------------------------------------------------------------------
8926 // Data type registration
8927 // ----------------------------------------------------------------------------
8929 void wxGrid::RegisterDataType(const wxString
& typeName
,
8930 wxGridCellRenderer
* renderer
,
8931 wxGridCellEditor
* editor
)
8933 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8937 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8939 wxString typeName
= m_table
->GetTypeName(row
, col
);
8940 return GetDefaultEditorForType(typeName
);
8943 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8945 wxString typeName
= m_table
->GetTypeName(row
, col
);
8946 return GetDefaultRendererForType(typeName
);
8950 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8952 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8953 if ( index
== wxNOT_FOUND
)
8955 wxFAIL_MSG(wxT("Unknown data type name"));
8960 return m_typeRegistry
->GetEditor(index
);
8964 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8966 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8967 if ( index
== wxNOT_FOUND
)
8969 wxFAIL_MSG(wxT("Unknown data type name"));
8974 return m_typeRegistry
->GetRenderer(index
);
8978 // ----------------------------------------------------------------------------
8980 // ----------------------------------------------------------------------------
8982 void wxGrid::EnableDragRowSize( bool enable
)
8984 m_canDragRowSize
= enable
;
8988 void wxGrid::EnableDragColSize( bool enable
)
8990 m_canDragColSize
= enable
;
8993 void wxGrid::EnableDragGridSize( bool enable
)
8995 m_canDragGridSize
= enable
;
8999 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9001 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
9003 if ( resizeExistingRows
)
9005 // since we are resizing all rows to the default row size,
9006 // we can simply clear the row heights and row bottoms
9007 // arrays (which also allows us to take advantage of
9008 // some speed optimisations)
9009 m_rowHeights
.Empty();
9010 m_rowBottoms
.Empty();
9011 if ( !GetBatchCount() )
9016 void wxGrid::SetRowSize( int row
, int height
)
9018 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9020 if ( m_rowHeights
.IsEmpty() )
9022 // need to really create the array
9026 int h
= wxMax( 0, height
);
9027 int diff
= h
- m_rowHeights
[row
];
9029 m_rowHeights
[row
] = h
;
9031 for ( i
= row
; i
< m_numRows
; i
++ )
9033 m_rowBottoms
[i
] += diff
;
9035 if ( !GetBatchCount() )
9039 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9041 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
9043 if ( resizeExistingCols
)
9045 // since we are resizing all columns to the default column size,
9046 // we can simply clear the col widths and col rights
9047 // arrays (which also allows us to take advantage of
9048 // some speed optimisations)
9049 m_colWidths
.Empty();
9050 m_colRights
.Empty();
9051 if ( !GetBatchCount() )
9056 void wxGrid::SetColSize( int col
, int width
)
9058 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9060 // should we check that it's bigger than GetColMinimalWidth(col) here?
9062 if ( m_colWidths
.IsEmpty() )
9064 // need to really create the array
9068 int w
= wxMax( 0, width
);
9069 int diff
= w
- m_colWidths
[col
];
9070 m_colWidths
[col
] = w
;
9073 for ( i
= col
; i
< m_numCols
; i
++ )
9075 m_colRights
[i
] += diff
;
9077 if ( !GetBatchCount() )
9082 void wxGrid::SetColMinimalWidth( int col
, int width
)
9084 m_colMinWidths
.Put(col
, width
);
9087 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9089 m_rowMinHeights
.Put(row
, width
);
9092 int wxGrid::GetColMinimalWidth(int col
) const
9094 long value
= m_colMinWidths
.Get(col
);
9095 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
9098 int wxGrid::GetRowMinimalHeight(int row
) const
9100 long value
= m_rowMinHeights
.Get(row
);
9101 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
9104 // ----------------------------------------------------------------------------
9106 // ----------------------------------------------------------------------------
9108 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9110 wxClientDC
dc(m_gridWin
);
9112 // init both of them to avoid compiler warnings, even if weo nly need one
9120 wxCoord extent
, extentMax
= 0;
9121 int max
= column
? m_numRows
: m_numCols
;
9122 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9129 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9130 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9133 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9134 extent
= column
? size
.x
: size
.y
;
9135 if ( extent
> extentMax
)
9146 // now also compare with the column label extent
9148 dc
.SetFont( GetLabelFont() );
9151 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9153 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9155 extent
= column
? w
: h
;
9156 if ( extent
> extentMax
)
9163 // empty column - give default extent (notice that if extentMax is less
9164 // than default extent but != 0, it's ok)
9165 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9171 // leave some space around text
9182 SetColSize(col
, extentMax
);
9183 if ( !GetBatchCount() )
9186 m_gridWin
->GetClientSize( &cw
, &ch
);
9187 wxRect
rect ( CellToRect( 0, col
) );
9189 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9190 rect
.width
= cw
- rect
.x
;
9191 rect
.height
= m_colLabelHeight
;
9192 m_colLabelWin
->Refresh( TRUE
, &rect
);
9197 SetRowSize(row
, extentMax
);
9198 if ( !GetBatchCount() )
9201 m_gridWin
->GetClientSize( &cw
, &ch
);
9202 wxRect
rect ( CellToRect( row
, 0 ) );
9204 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9205 rect
.width
= m_rowLabelWidth
;
9206 rect
.height
= ch
- rect
.y
;
9207 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9213 SetColMinimalWidth(col
, extentMax
);
9215 SetRowMinimalHeight(row
, extentMax
);
9219 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9221 int width
= m_rowLabelWidth
;
9226 for ( int col
= 0; col
< m_numCols
; col
++ )
9230 AutoSizeColumn(col
, setAsMin
);
9233 width
+= GetColWidth(col
);
9242 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9244 int height
= m_colLabelHeight
;
9249 for ( int row
= 0; row
< m_numRows
; row
++ )
9253 AutoSizeRow(row
, setAsMin
);
9256 height
+= GetRowHeight(row
);
9265 void wxGrid::AutoSize()
9269 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9271 // round up the size to a multiple of scroll step - this ensures that we
9272 // won't get the scrollbars if we're sized exactly to this width
9273 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
9274 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
9276 // distribute the extra space between teh columns/rows to avoid having
9277 // extra white space
9278 wxCoord diff
= sizeFit
.x
- size
.x
;
9281 // try to resize the columns uniformly
9282 wxCoord diffPerCol
= diff
/ m_numCols
;
9285 for ( int col
= 0; col
< m_numCols
; col
++ )
9287 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9291 // add remaining amount to the last columns
9292 diff
-= diffPerCol
* m_numCols
;
9295 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9297 SetColSize(col
, GetColWidth(col
) + 1);
9303 diff
= sizeFit
.y
- size
.y
;
9306 // try to resize the columns uniformly
9307 wxCoord diffPerRow
= diff
/ m_numRows
;
9310 for ( int row
= 0; row
< m_numRows
; row
++ )
9312 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9316 // add remaining amount to the last rows
9317 diff
-= diffPerRow
* m_numRows
;
9320 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9322 SetRowSize(row
, GetRowHeight(row
) + 1);
9329 SetClientSize(sizeFit
);
9332 wxSize
wxGrid::DoGetBestSize() const
9334 // don't set sizes, only calculate them
9335 wxGrid
*self
= (wxGrid
*)this; // const_cast
9338 width
= self
->SetOrCalcColumnSizes(TRUE
);
9339 height
= self
->SetOrCalcRowSizes(TRUE
);
9341 int maxwidth
, maxheight
;
9342 wxDisplaySize( & maxwidth
, & maxheight
);
9344 if ( width
> maxwidth
) width
= maxwidth
;
9345 if ( height
> maxheight
) height
= maxheight
;
9347 return wxSize( width
, height
);
9356 wxPen
& wxGrid::GetDividerPen() const
9361 // ----------------------------------------------------------------------------
9362 // cell value accessor functions
9363 // ----------------------------------------------------------------------------
9365 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9369 m_table
->SetValue( row
, col
, s
);
9370 if ( !GetBatchCount() )
9373 wxRect
rect( CellToRect( row
, col
) );
9375 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9376 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9377 m_gridWin
->Refresh( FALSE
, &rect
);
9380 if ( m_currentCellCoords
.GetRow() == row
&&
9381 m_currentCellCoords
.GetCol() == col
&&
9382 IsCellEditControlShown())
9383 // Note: If we are using IsCellEditControlEnabled,
9384 // this interacts badly with calling SetCellValue from
9385 // an EVT_GRID_CELL_CHANGE handler.
9387 HideCellEditControl();
9388 ShowCellEditControl(); // will reread data from table
9395 // ------ Block, row and col selection
9398 void wxGrid::SelectRow( int row
, bool addToSelected
)
9400 if ( IsSelection() && !addToSelected
)
9404 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9408 void wxGrid::SelectCol( int col
, bool addToSelected
)
9410 if ( IsSelection() && !addToSelected
)
9414 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9418 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9419 bool addToSelected
)
9421 if ( IsSelection() && !addToSelected
)
9425 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9426 FALSE
, addToSelected
);
9430 void wxGrid::SelectAll()
9432 if ( m_numRows
> 0 && m_numCols
> 0 )
9435 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9440 // ------ Cell, row and col deselection
9443 void wxGrid::DeselectRow( int row
)
9448 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9450 if ( m_selection
->IsInSelection(row
, 0 ) )
9451 m_selection
->ToggleCellSelection( row
, 0);
9455 int nCols
= GetNumberCols();
9456 for ( int i
= 0; i
< nCols
; i
++ )
9458 if ( m_selection
->IsInSelection(row
, i
) )
9459 m_selection
->ToggleCellSelection( row
, i
);
9464 void wxGrid::DeselectCol( int col
)
9469 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9471 if ( m_selection
->IsInSelection(0, col
) )
9472 m_selection
->ToggleCellSelection( 0, col
);
9476 int nRows
= GetNumberRows();
9477 for ( int i
= 0; i
< nRows
; i
++ )
9479 if ( m_selection
->IsInSelection(i
, col
) )
9480 m_selection
->ToggleCellSelection(i
, col
);
9485 void wxGrid::DeselectCell( int row
, int col
)
9487 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9488 m_selection
->ToggleCellSelection(row
, col
);
9491 bool wxGrid::IsSelection()
9493 return ( m_selection
&& (m_selection
->IsSelection() ||
9494 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9495 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9498 bool wxGrid::IsInSelection( int row
, int col
) const
9500 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9501 ( row
>= m_selectingTopLeft
.GetRow() &&
9502 col
>= m_selectingTopLeft
.GetCol() &&
9503 row
<= m_selectingBottomRight
.GetRow() &&
9504 col
<= m_selectingBottomRight
.GetCol() )) );
9507 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9509 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9510 return m_selection
->m_cellSelection
;
9512 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9514 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9515 return m_selection
->m_blockSelectionTopLeft
;
9517 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9519 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9520 return m_selection
->m_blockSelectionBottomRight
;
9522 wxArrayInt
wxGrid::GetSelectedRows() const
9524 if (!m_selection
) { wxArrayInt a
; return a
; }
9525 return m_selection
->m_rowSelection
;
9527 wxArrayInt
wxGrid::GetSelectedCols() const
9529 if (!m_selection
) { wxArrayInt a
; return a
; }
9530 return m_selection
->m_colSelection
;
9534 void wxGrid::ClearSelection()
9536 m_selectingTopLeft
= wxGridNoCellCoords
;
9537 m_selectingBottomRight
= wxGridNoCellCoords
;
9539 m_selection
->ClearSelection();
9543 // This function returns the rectangle that encloses the given block
9544 // in device coords clipped to the client size of the grid window.
9546 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
9547 const wxGridCellCoords
&bottomRight
)
9549 wxRect
rect( wxGridNoCellRect
);
9552 cellRect
= CellToRect( topLeft
);
9553 if ( cellRect
!= wxGridNoCellRect
)
9559 rect
= wxRect( 0, 0, 0, 0 );
9562 cellRect
= CellToRect( bottomRight
);
9563 if ( cellRect
!= wxGridNoCellRect
)
9569 return wxGridNoCellRect
;
9573 int left
= rect
.GetLeft();
9574 int top
= rect
.GetTop();
9575 int right
= rect
.GetRight();
9576 int bottom
= rect
.GetBottom();
9578 int leftCol
= topLeft
.GetCol();
9579 int topRow
= topLeft
.GetRow();
9580 int rightCol
= bottomRight
.GetCol();
9581 int bottomRow
= bottomRight
.GetRow();
9604 for ( j
= topRow
; j
<= bottomRow
; j
++ )
9606 for ( i
= leftCol
; i
<= rightCol
; i
++ )
9608 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
9610 cellRect
= CellToRect( j
, i
);
9612 if (cellRect
.x
< left
)
9614 if (cellRect
.y
< top
)
9616 if (cellRect
.x
+ cellRect
.width
> right
)
9617 right
= cellRect
.x
+ cellRect
.width
;
9618 if (cellRect
.y
+ cellRect
.height
> bottom
)
9619 bottom
= cellRect
.y
+ cellRect
.height
;
9621 else i
= rightCol
; // jump over inner cells.
9625 // convert to scrolled coords
9627 CalcScrolledPosition( left
, top
, &left
, &top
);
9628 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
9631 m_gridWin
->GetClientSize( &cw
, &ch
);
9633 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
9634 return wxRect( 0, 0, 0, 0);
9636 rect
.SetLeft( wxMax(0, left
) );
9637 rect
.SetTop( wxMax(0, top
) );
9638 rect
.SetRight( wxMin(cw
, right
) );
9639 rect
.SetBottom( wxMin(ch
, bottom
) );
9647 // ------ Grid event classes
9650 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9652 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9653 int row
, int col
, int x
, int y
, bool sel
,
9654 bool control
, bool shift
, bool alt
, bool meta
)
9655 : wxNotifyEvent( type
, id
)
9662 m_control
= control
;
9667 SetEventObject(obj
);
9671 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9673 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9674 int rowOrCol
, int x
, int y
,
9675 bool control
, bool shift
, bool alt
, bool meta
)
9676 : wxNotifyEvent( type
, id
)
9678 m_rowOrCol
= rowOrCol
;
9681 m_control
= control
;
9686 SetEventObject(obj
);
9690 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9692 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9693 const wxGridCellCoords
& topLeft
,
9694 const wxGridCellCoords
& bottomRight
,
9695 bool sel
, bool control
,
9696 bool shift
, bool alt
, bool meta
)
9697 : wxNotifyEvent( type
, id
)
9699 m_topLeft
= topLeft
;
9700 m_bottomRight
= bottomRight
;
9702 m_control
= control
;
9707 SetEventObject(obj
);
9711 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9713 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9714 wxObject
* obj
, int row
,
9715 int col
, wxControl
* ctrl
)
9716 : wxCommandEvent(type
, id
)
9718 SetEventObject(obj
);
9725 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9727 #endif // wxUSE_GRID