1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
35 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
37 #else // wxUSE_NEW_GRID
41 #include "wx/dcclient.h"
42 #include "wx/settings.h"
44 #include "wx/textctrl.h"
45 #include "wx/checkbox.h"
46 #include "wx/combobox.h"
47 #include "wx/valtext.h"
50 #include "wx/textfile.h"
51 #include "wx/spinctrl.h"
52 #include "wx/tokenzr.h"
55 #include "wx/generic/gridsel.h"
57 #if defined(__WXMOTIF__)
58 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
60 #define WXUNUSED_MOTIF(identifier) identifier
63 #if defined(__WXGTK__)
64 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
66 #define WXUNUSED_GTK(identifier) identifier
69 // Required for wxIs... functions
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
78 struct wxGridCellWithAttr
80 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
81 : coords(row
, col
), attr(attr_
)
90 wxGridCellCoords coords
;
94 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
96 #include "wx/arrimpl.cpp"
98 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
99 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
129 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
130 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
131 const wxPoint
&pos
, const wxSize
&size
);
136 void OnPaint( wxPaintEvent
& event
);
137 void OnMouseEvent( wxMouseEvent
& event
);
138 void OnMouseWheel( wxMouseEvent
& event
);
139 void OnKeyDown( wxKeyEvent
& event
);
140 void OnKeyUp( wxKeyEvent
& );
142 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
143 DECLARE_EVENT_TABLE()
147 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
150 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
151 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
152 const wxPoint
&pos
, const wxSize
&size
);
157 void OnPaint( wxPaintEvent
&event
);
158 void OnMouseEvent( wxMouseEvent
& event
);
159 void OnMouseWheel( wxMouseEvent
& event
);
160 void OnKeyDown( wxKeyEvent
& event
);
161 void OnKeyUp( wxKeyEvent
& );
163 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
164 DECLARE_EVENT_TABLE()
168 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
171 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
172 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
173 const wxPoint
&pos
, const wxSize
&size
);
178 void OnMouseEvent( wxMouseEvent
& event
);
179 void OnMouseWheel( wxMouseEvent
& event
);
180 void OnKeyDown( wxKeyEvent
& event
);
181 void OnKeyUp( wxKeyEvent
& );
182 void OnPaint( wxPaintEvent
& event
);
184 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
185 DECLARE_EVENT_TABLE()
188 class WXDLLEXPORT wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
208 wxGridRowLabelWindow
*m_rowLabelWin
;
209 wxGridColLabelWindow
*m_colLabelWin
;
211 void OnPaint( wxPaintEvent
&event
);
212 void OnMouseWheel( wxMouseEvent
& event
);
213 void OnMouseEvent( wxMouseEvent
& event
);
214 void OnKeyDown( wxKeyEvent
& );
215 void OnKeyUp( wxKeyEvent
& );
216 void OnEraseBackground( wxEraseEvent
& );
219 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
220 DECLARE_EVENT_TABLE()
225 class wxGridCellEditorEvtHandler
: public wxEvtHandler
228 wxGridCellEditorEvtHandler()
229 : m_grid(0), m_editor(0)
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
232 : m_grid(grid
), m_editor(editor
)
235 void OnKeyDown(wxKeyEvent
& event
);
236 void OnChar(wxKeyEvent
& event
);
240 wxGridCellEditor
* m_editor
;
241 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
242 DECLARE_EVENT_TABLE()
246 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
247 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
248 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
249 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
254 // ----------------------------------------------------------------------------
255 // the internal data representation used by wxGridCellAttrProvider
256 // ----------------------------------------------------------------------------
258 // this class stores attributes set for cells
259 class WXDLLEXPORT wxGridCellAttrData
262 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
263 wxGridCellAttr
*GetAttr(int row
, int col
) const;
264 void UpdateAttrRows( size_t pos
, int numRows
);
265 void UpdateAttrCols( size_t pos
, int numCols
);
268 // searches for the attr for given cell, returns wxNOT_FOUND if not found
269 int FindIndex(int row
, int col
) const;
271 wxGridCellWithAttrArray m_attrs
;
274 // this class stores attributes set for rows or columns
275 class WXDLLEXPORT wxGridRowOrColAttrData
278 // empty ctor to suppress warnings
279 wxGridRowOrColAttrData() { }
280 ~wxGridRowOrColAttrData();
282 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
283 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
284 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
287 wxArrayInt m_rowsOrCols
;
288 wxArrayAttrs m_attrs
;
291 // NB: this is just a wrapper around 3 objects: one which stores cell
292 // attributes, and 2 others for row/col ones
293 class WXDLLEXPORT wxGridCellAttrProviderData
296 wxGridCellAttrData m_cellAttrs
;
297 wxGridRowOrColAttrData m_rowAttrs
,
302 // ----------------------------------------------------------------------------
303 // data structures used for the data type registry
304 // ----------------------------------------------------------------------------
306 struct wxGridDataTypeInfo
308 wxGridDataTypeInfo(const wxString
& typeName
,
309 wxGridCellRenderer
* renderer
,
310 wxGridCellEditor
* editor
)
311 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
314 ~wxGridDataTypeInfo()
316 wxSafeDecRef(m_renderer
);
317 wxSafeDecRef(m_editor
);
321 wxGridCellRenderer
* m_renderer
;
322 wxGridCellEditor
* m_editor
;
326 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
329 class WXDLLEXPORT wxGridTypeRegistry
332 wxGridTypeRegistry() {}
333 ~wxGridTypeRegistry();
335 void RegisterDataType(const wxString
& typeName
,
336 wxGridCellRenderer
* renderer
,
337 wxGridCellEditor
* editor
);
339 // find one of already registered data types
340 int FindRegisteredDataType(const wxString
& typeName
);
342 // try to FindRegisteredDataType(), if this fails and typeName is one of
343 // standard typenames, register it and return its index
344 int FindDataType(const wxString
& typeName
);
346 // try to FindDataType(), if it fails see if it is not one of already
347 // registered data types with some params in which case clone the
348 // registered data type and set params for it
349 int FindOrCloneDataType(const wxString
& typeName
);
351 wxGridCellRenderer
* GetRenderer(int index
);
352 wxGridCellEditor
* GetEditor(int index
);
355 wxGridDataTypeInfoArray m_typeinfo
;
358 // ----------------------------------------------------------------------------
359 // conditional compilation
360 // ----------------------------------------------------------------------------
362 #ifndef WXGRID_DRAW_LINES
363 #define WXGRID_DRAW_LINES 1
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 //#define DEBUG_ATTR_CACHE
371 #ifdef DEBUG_ATTR_CACHE
372 static size_t gs_nAttrCacheHits
= 0;
373 static size_t gs_nAttrCacheMisses
= 0;
374 #endif // DEBUG_ATTR_CACHE
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
381 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
384 // TODO: this doesn't work at all, grid cells have different sizes and approx
385 // calculations don't work as because of the size mismatch scrollbars
386 // sometimes fail to be shown when they should be or vice versa
388 // The scroll bars may be a little flakey once in a while, but that is
389 // surely much less horrible than having scroll lines of only 1!!!
392 // Well, it's still seriously broken so it might be better but needs
395 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
396 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
398 // the size of hash tables used a bit everywhere (the max number of elements
399 // in these hash tables is the number of rows/columns)
400 static const int GRID_HASH_SIZE
= 100;
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 static inline int GetScrollX(int x
)
408 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
411 static inline int GetScrollY(int y
)
413 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
416 // ============================================================================
418 // ============================================================================
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxGridCellEditor::wxGridCellEditor()
430 wxGridCellEditor::~wxGridCellEditor()
435 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
436 wxWindowID
WXUNUSED(id
),
437 wxEvtHandler
* evtHandler
)
440 m_control
->PushEventHandler(evtHandler
);
443 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
444 wxGridCellAttr
*attr
)
446 // erase the background because we might not fill the cell
447 wxClientDC
dc(m_control
->GetParent());
448 dc
.SetPen(*wxTRANSPARENT_PEN
);
449 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
450 dc
.DrawRectangle(rectCell
);
452 // redraw the control we just painted over
453 m_control
->Refresh();
456 void wxGridCellEditor::Destroy()
460 m_control
->PopEventHandler(TRUE
/* delete it*/);
462 m_control
->Destroy();
467 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
469 wxASSERT_MSG(m_control
,
470 wxT("The wxGridCellEditor must be Created first!"));
471 m_control
->Show(show
);
475 // set the colours/fonts if we have any
478 m_colFgOld
= m_control
->GetForegroundColour();
479 m_control
->SetForegroundColour(attr
->GetTextColour());
481 m_colBgOld
= m_control
->GetBackgroundColour();
482 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
484 m_fontOld
= m_control
->GetFont();
485 m_control
->SetFont(attr
->GetFont());
487 // can't do anything more in the base class version, the other
488 // attributes may only be used by the derived classes
493 // restore the standard colours fonts
494 if ( m_colFgOld
.Ok() )
496 m_control
->SetForegroundColour(m_colFgOld
);
497 m_colFgOld
= wxNullColour
;
500 if ( m_colBgOld
.Ok() )
502 m_control
->SetBackgroundColour(m_colBgOld
);
503 m_colBgOld
= wxNullColour
;
506 if ( m_fontOld
.Ok() )
508 m_control
->SetFont(m_fontOld
);
509 m_fontOld
= wxNullFont
;
514 void wxGridCellEditor::SetSize(const wxRect
& rect
)
516 wxASSERT_MSG(m_control
,
517 wxT("The wxGridCellEditor must be Created first!"));
518 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
521 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
526 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
528 // accept the simple key presses, not anything with Ctrl/Alt/Meta
529 return !(event
.ControlDown() || event
.AltDown());
532 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
537 void wxGridCellEditor::StartingClick()
543 // ----------------------------------------------------------------------------
544 // wxGridCellTextEditor
545 // ----------------------------------------------------------------------------
547 wxGridCellTextEditor::wxGridCellTextEditor()
552 void wxGridCellTextEditor::Create(wxWindow
* parent
,
554 wxEvtHandler
* evtHandler
)
556 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
557 wxDefaultPosition
, wxDefaultSize
558 #if defined(__WXMSW__)
559 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
560 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
564 // TODO: use m_maxChars
566 wxGridCellEditor::Create(parent
, id
, evtHandler
);
569 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
570 wxGridCellAttr
* WXUNUSED(attr
))
572 // as we fill the entire client area, don't do anything here to minimize
576 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
578 wxRect
rect(rectOrig
);
580 // Make the edit control large enough to allow for internal
583 // TODO: remove this if the text ctrl sizing is improved esp. for
586 #if defined(__WXGTK__)
595 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
597 // MB: treat MSW separately here otherwise the caret doesn't show
598 // when the editor is in the first row.
599 #if defined(__WXMSW__)
602 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
605 #if defined(__WXMOTIF__)
609 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
610 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
611 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
612 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
615 wxGridCellEditor::SetSize(rect
);
618 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
620 wxASSERT_MSG(m_control
,
621 wxT("The wxGridCellEditor must be Created first!"));
623 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
625 DoBeginEdit(m_startValue
);
628 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
630 Text()->SetValue(startValue
);
631 Text()->SetInsertionPointEnd();
632 Text()->SetSelection(-1,-1);
636 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
639 wxASSERT_MSG(m_control
,
640 wxT("The wxGridCellEditor must be Created first!"));
642 bool changed
= FALSE
;
643 wxString value
= Text()->GetValue();
644 if (value
!= m_startValue
)
648 grid
->GetTable()->SetValue(row
, col
, value
);
650 m_startValue
= wxEmptyString
;
651 Text()->SetValue(m_startValue
);
657 void wxGridCellTextEditor::Reset()
659 wxASSERT_MSG(m_control
,
660 wxT("The wxGridCellEditor must be Created first!"));
662 DoReset(m_startValue
);
665 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
667 Text()->SetValue(startValue
);
668 Text()->SetInsertionPointEnd();
671 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
673 if ( wxGridCellEditor::IsAcceptedKey(event
) )
675 int keycode
= event
.GetKeyCode();
689 case WXK_NUMPAD_MULTIPLY
:
693 case WXK_NUMPAD_SUBTRACT
:
695 case WXK_NUMPAD_DECIMAL
:
697 case WXK_NUMPAD_DIVIDE
:
701 // accept 8 bit chars too if isprint() agrees
702 if ( (keycode
< 255) && (isprint(keycode
)) )
710 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
712 if ( !Text()->EmulateKeyPress(event
) )
718 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
719 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
721 #if defined(__WXMOTIF__) || defined(__WXGTK__)
722 // wxMotif needs a little extra help...
723 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
724 wxString
s( Text()->GetValue() );
725 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
727 Text()->SetInsertionPoint( pos
);
729 // the other ports can handle a Return key press
735 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
745 if ( !params
.ToLong(&tmp
) )
747 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
751 m_maxChars
= (size_t)tmp
;
756 // ----------------------------------------------------------------------------
757 // wxGridCellNumberEditor
758 // ----------------------------------------------------------------------------
760 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
766 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
768 wxEvtHandler
* evtHandler
)
772 // create a spin ctrl
773 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
774 wxDefaultPosition
, wxDefaultSize
,
778 wxGridCellEditor::Create(parent
, id
, evtHandler
);
782 // just a text control
783 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
786 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
787 #endif // wxUSE_VALIDATORS
791 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
793 // first get the value
794 wxGridTableBase
*table
= grid
->GetTable();
795 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
797 m_valueOld
= table
->GetValueAsLong(row
, col
);
802 wxString sValue
= table
->GetValue(row
, col
);
803 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
805 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
812 Spin()->SetValue((int)m_valueOld
);
817 DoBeginEdit(GetString());
821 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
830 value
= Spin()->GetValue();
831 changed
= value
!= m_valueOld
;
833 text
= wxString::Format(wxT("%ld"), value
);
837 text
= Text()->GetValue();
838 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
843 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
844 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
846 grid
->GetTable()->SetValue(row
, col
, text
);
852 void wxGridCellNumberEditor::Reset()
856 Spin()->SetValue((int)m_valueOld
);
860 DoReset(GetString());
864 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
866 if ( wxGridCellEditor::IsAcceptedKey(event
) )
868 int keycode
= event
.GetKeyCode();
884 case WXK_NUMPAD_SUBTRACT
:
890 if ( (keycode
< 128) && isdigit(keycode
) )
898 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
902 int keycode
= (int) event
.KeyCode();
903 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
905 wxGridCellTextEditor::StartingKey(event
);
915 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
926 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
930 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
934 // skip the error message below
939 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
943 // ----------------------------------------------------------------------------
944 // wxGridCellFloatEditor
945 // ----------------------------------------------------------------------------
947 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
950 m_precision
= precision
;
953 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
955 wxEvtHandler
* evtHandler
)
957 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
960 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
961 #endif // wxUSE_VALIDATORS
964 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
966 // first get the value
967 wxGridTableBase
*table
= grid
->GetTable();
968 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
970 m_valueOld
= table
->GetValueAsDouble(row
, col
);
975 wxString sValue
= table
->GetValue(row
, col
);
976 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
978 wxFAIL_MSG( _T("this cell doesn't have float value") );
983 DoBeginEdit(GetString());
986 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
990 wxString
text(Text()->GetValue());
992 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
994 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
995 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
997 grid
->GetTable()->SetValue(row
, col
, text
);
1007 void wxGridCellFloatEditor::Reset()
1009 DoReset(GetString());
1012 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1014 int keycode
= (int)event
.KeyCode();
1015 if ( isdigit(keycode
) ||
1016 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1018 wxGridCellTextEditor::StartingKey(event
);
1020 // skip Skip() below
1027 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1038 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1042 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1044 m_precision
= (int)tmp
;
1046 // skip the error message below
1051 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1055 wxString
wxGridCellFloatEditor::GetString() const
1058 if ( m_width
== -1 )
1060 // default width/precision
1063 else if ( m_precision
== -1 )
1065 // default precision
1066 fmt
.Printf(_T("%%%d.f"), m_width
);
1070 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1073 return wxString::Format(fmt
, m_valueOld
);
1076 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1078 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1080 int keycode
= event
.GetKeyCode();
1094 case WXK_NUMPAD_ADD
:
1096 case WXK_NUMPAD_SUBTRACT
:
1098 case WXK_NUMPAD_DECIMAL
:
1102 // additionally accept 'e' as in '1e+6'
1103 if ( (keycode
< 128) &&
1104 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1112 #endif // wxUSE_TEXTCTRL
1116 // ----------------------------------------------------------------------------
1117 // wxGridCellBoolEditor
1118 // ----------------------------------------------------------------------------
1120 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1122 wxEvtHandler
* evtHandler
)
1124 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1125 wxDefaultPosition
, wxDefaultSize
,
1128 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1131 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1133 bool resize
= FALSE
;
1134 wxSize size
= m_control
->GetSize();
1135 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1137 // check if the checkbox is not too big/small for this cell
1138 wxSize sizeBest
= m_control
->GetBestSize();
1139 if ( !(size
== sizeBest
) )
1141 // reset to default size if it had been made smaller
1147 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1149 // leave 1 pixel margin
1150 size
.x
= size
.y
= minSize
- 2;
1157 m_control
->SetSize(size
);
1160 // position it in the centre of the rectangle (TODO: support alignment?)
1162 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1163 // the checkbox without label still has some space to the right in wxGTK,
1164 // so shift it to the right
1166 #elif defined(__WXMSW__)
1167 // here too, but in other way
1172 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1175 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1177 m_control
->Show(show
);
1181 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1182 CBox()->SetBackgroundColour(colBg
);
1186 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1188 wxASSERT_MSG(m_control
,
1189 wxT("The wxGridCellEditor must be Created first!"));
1191 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1192 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1195 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1196 m_startValue
= !( !cellval
|| (cellval
== "0") );
1198 CBox()->SetValue(m_startValue
);
1202 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1205 wxASSERT_MSG(m_control
,
1206 wxT("The wxGridCellEditor must be Created first!"));
1208 bool changed
= FALSE
;
1209 bool value
= CBox()->GetValue();
1210 if ( value
!= m_startValue
)
1215 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1216 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1218 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1224 void wxGridCellBoolEditor::Reset()
1226 wxASSERT_MSG(m_control
,
1227 wxT("The wxGridCellEditor must be Created first!"));
1229 CBox()->SetValue(m_startValue
);
1232 void wxGridCellBoolEditor::StartingClick()
1234 CBox()->SetValue(!CBox()->GetValue());
1237 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1239 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1241 int keycode
= event
.GetKeyCode();
1245 case WXK_NUMPAD_MULTIPLY
:
1247 case WXK_NUMPAD_ADD
:
1249 case WXK_NUMPAD_SUBTRACT
:
1260 #endif // wxUSE_CHECKBOX
1264 // ----------------------------------------------------------------------------
1265 // wxGridCellChoiceEditor
1266 // ----------------------------------------------------------------------------
1268 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1269 const wxString choices
[],
1271 : m_allowOthers(allowOthers
)
1275 m_choices
.Alloc(count
);
1276 for ( size_t n
= 0; n
< count
; n
++ )
1278 m_choices
.Add(choices
[n
]);
1283 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1285 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1286 editor
->m_allowOthers
= m_allowOthers
;
1287 editor
->m_choices
= m_choices
;
1292 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1294 wxEvtHandler
* evtHandler
)
1296 size_t count
= m_choices
.GetCount();
1297 wxString
*choices
= new wxString
[count
];
1298 for ( size_t n
= 0; n
< count
; n
++ )
1300 choices
[n
] = m_choices
[n
];
1303 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1304 wxDefaultPosition
, wxDefaultSize
,
1306 m_allowOthers
? 0 : wxCB_READONLY
);
1310 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1313 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1314 wxGridCellAttr
* attr
)
1316 // as we fill the entire client area, don't do anything here to minimize
1319 // TODO: It doesn't actually fill the client area since the height of a
1320 // combo always defaults to the standard... Until someone has time to
1321 // figure out the right rectangle to paint, just do it the normal way...
1322 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1325 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1327 wxASSERT_MSG(m_control
,
1328 wxT("The wxGridCellEditor must be Created first!"));
1330 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1332 Combo()->SetValue(m_startValue
);
1333 size_t count
= m_choices
.GetCount();
1334 for (size_t i
=0; i
<count
; i
++)
1336 if (m_startValue
== m_choices
[i
])
1338 Combo()->SetSelection(i
);
1342 Combo()->SetInsertionPointEnd();
1343 Combo()->SetFocus();
1346 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1349 wxString value
= Combo()->GetValue();
1350 bool changed
= value
!= m_startValue
;
1353 grid
->GetTable()->SetValue(row
, col
, value
);
1355 m_startValue
= wxEmptyString
;
1356 Combo()->SetValue(m_startValue
);
1361 void wxGridCellChoiceEditor::Reset()
1363 Combo()->SetValue(m_startValue
);
1364 Combo()->SetInsertionPointEnd();
1367 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1377 wxStringTokenizer
tk(params
, _T(','));
1378 while ( tk
.HasMoreTokens() )
1380 m_choices
.Add(tk
.GetNextToken());
1384 #endif // wxUSE_COMBOBOX
1386 // ----------------------------------------------------------------------------
1387 // wxGridCellEditorEvtHandler
1388 // ----------------------------------------------------------------------------
1390 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1392 switch ( event
.KeyCode() )
1396 m_grid
->DisableCellEditControl();
1400 m_grid
->GetEventHandler()->ProcessEvent( event
);
1404 case WXK_NUMPAD_ENTER
:
1405 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1406 m_editor
->HandleReturn(event
);
1415 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1417 switch ( event
.KeyCode() )
1422 case WXK_NUMPAD_ENTER
:
1430 // ----------------------------------------------------------------------------
1431 // wxGridCellWorker is an (almost) empty common base class for
1432 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1433 // ----------------------------------------------------------------------------
1435 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1440 wxGridCellWorker::~wxGridCellWorker()
1444 // ============================================================================
1446 // ============================================================================
1448 // ----------------------------------------------------------------------------
1449 // wxGridCellRenderer
1450 // ----------------------------------------------------------------------------
1452 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1453 wxGridCellAttr
& attr
,
1456 int WXUNUSED(row
), int WXUNUSED(col
),
1459 dc
.SetBackgroundMode( wxSOLID
);
1463 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1467 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1470 dc
.SetPen( *wxTRANSPARENT_PEN
);
1471 dc
.DrawRectangle(rect
);
1474 // ----------------------------------------------------------------------------
1475 // wxGridCellStringRenderer
1476 // ----------------------------------------------------------------------------
1478 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1479 wxGridCellAttr
& attr
,
1483 dc
.SetBackgroundMode( wxTRANSPARENT
);
1485 // TODO some special colours for attr.IsReadOnly() case?
1489 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1490 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1494 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1495 dc
.SetTextForeground( attr
.GetTextColour() );
1498 dc
.SetFont( attr
.GetFont() );
1501 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1503 const wxString
& text
)
1505 wxCoord x
= 0, y
= 0, max_x
= 0;
1506 dc
.SetFont(attr
.GetFont());
1507 wxStringTokenizer
tk(text
, _T('\n'));
1508 while ( tk
.HasMoreTokens() )
1510 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1511 max_x
= wxMax(max_x
, x
);
1514 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1516 return wxSize(max_x
, y
);
1519 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1520 wxGridCellAttr
& attr
,
1524 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1527 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1528 wxGridCellAttr
& attr
,
1530 const wxRect
& rectCell
,
1534 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1536 // now we only have to draw the text
1537 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1540 attr
.GetAlignment(&hAlign
, &vAlign
);
1542 wxRect rect
= rectCell
;
1545 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1546 rect
, hAlign
, vAlign
);
1549 // ----------------------------------------------------------------------------
1550 // wxGridCellNumberRenderer
1551 // ----------------------------------------------------------------------------
1553 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1555 wxGridTableBase
*table
= grid
.GetTable();
1557 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1559 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1563 text
= table
->GetValue(row
, col
);
1569 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1570 wxGridCellAttr
& attr
,
1572 const wxRect
& rectCell
,
1576 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1578 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1580 // draw the text right aligned by default
1582 attr
.GetAlignment(&hAlign
, &vAlign
);
1583 hAlign
= wxALIGN_RIGHT
;
1585 wxRect rect
= rectCell
;
1588 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1591 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1592 wxGridCellAttr
& attr
,
1596 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1599 // ----------------------------------------------------------------------------
1600 // wxGridCellFloatRenderer
1601 // ----------------------------------------------------------------------------
1603 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1606 SetPrecision(precision
);
1609 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1611 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1612 renderer
->m_width
= m_width
;
1613 renderer
->m_precision
= m_precision
;
1614 renderer
->m_format
= m_format
;
1619 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1621 wxGridTableBase
*table
= grid
.GetTable();
1626 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1628 val
= table
->GetValueAsDouble(row
, col
);
1633 text
= table
->GetValue(row
, col
);
1634 hasDouble
= text
.ToDouble(&val
);
1641 if ( m_width
== -1 )
1643 if ( m_precision
== -1 )
1645 // default width/precision
1646 m_format
= _T("%f");
1650 m_format
.Printf(_T("%%.%df"), m_precision
);
1653 else if ( m_precision
== -1 )
1655 // default precision
1656 m_format
.Printf(_T("%%%d.f"), m_width
);
1660 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1664 text
.Printf(m_format
, val
);
1667 //else: text already contains the string
1672 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1673 wxGridCellAttr
& attr
,
1675 const wxRect
& rectCell
,
1679 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1681 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1683 // draw the text right aligned by default
1685 attr
.GetAlignment(&hAlign
, &vAlign
);
1686 hAlign
= wxALIGN_RIGHT
;
1688 wxRect rect
= rectCell
;
1691 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1694 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1695 wxGridCellAttr
& attr
,
1699 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1702 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1706 // reset to defaults
1712 wxString tmp
= params
.BeforeFirst(_T(','));
1716 if ( tmp
.ToLong(&width
) )
1718 SetWidth((int)width
);
1722 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1726 tmp
= params
.AfterFirst(_T(','));
1730 if ( tmp
.ToLong(&precision
) )
1732 SetPrecision((int)precision
);
1736 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1744 // ----------------------------------------------------------------------------
1745 // wxGridCellBoolRenderer
1746 // ----------------------------------------------------------------------------
1748 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1750 // FIXME these checkbox size calculations are really ugly...
1752 // between checkmark and box
1753 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1755 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1756 wxGridCellAttr
& WXUNUSED(attr
),
1761 // compute it only once (no locks for MT safeness in GUI thread...)
1762 if ( !ms_sizeCheckMark
.x
)
1764 // get checkbox size
1765 wxCoord checkSize
= 0;
1766 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1767 wxSize size
= checkbox
->GetBestSize();
1768 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1770 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1771 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1772 checkSize
-= size
.y
/ 2;
1777 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1780 return ms_sizeCheckMark
;
1783 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1784 wxGridCellAttr
& attr
,
1790 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1792 // draw a check mark in the centre (ignoring alignment - TODO)
1793 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1795 // don't draw outside the cell
1796 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1797 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1799 // and even leave (at least) 1 pixel margin
1800 size
.x
= size
.y
= minSize
- 2;
1803 // draw a border around checkmark
1805 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1806 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1807 rectBorder
.width
= size
.x
;
1808 rectBorder
.height
= size
.y
;
1811 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1812 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1815 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1816 value
= !( !cellval
|| (cellval
== "0") );
1821 wxRect rectMark
= rectBorder
;
1823 // MSW DrawCheckMark() is weird (and should probably be changed...)
1824 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1828 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1831 dc
.SetTextForeground(attr
.GetTextColour());
1832 dc
.DrawCheckMark(rectMark
);
1835 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1836 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1837 dc
.DrawRectangle(rectBorder
);
1840 // ----------------------------------------------------------------------------
1842 // ----------------------------------------------------------------------------
1844 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
1848 m_isReadOnly
= Unset
;
1853 m_attrkind
= wxGridCellAttr::Cell
;
1855 SetDefAttr(attrDefault
);
1858 wxGridCellAttr
*wxGridCellAttr::Clone() const
1860 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
1862 if ( HasTextColour() )
1863 attr
->SetTextColour(GetTextColour());
1864 if ( HasBackgroundColour() )
1865 attr
->SetBackgroundColour(GetBackgroundColour());
1867 attr
->SetFont(GetFont());
1868 if ( HasAlignment() )
1869 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1873 attr
->SetRenderer(m_renderer
);
1874 m_renderer
->IncRef();
1878 attr
->SetEditor(m_editor
);
1883 attr
->SetReadOnly();
1885 attr
->SetKind( m_attrkind
);
1890 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1892 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1893 SetTextColour(mergefrom
->GetTextColour());
1894 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1895 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1896 if ( !HasFont() && mergefrom
->HasFont() )
1897 SetFont(mergefrom
->GetFont());
1898 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1900 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1901 SetAlignment(hAlign
, vAlign
);
1904 // Directly access member functions as GetRender/Editor don't just return
1905 // m_renderer/m_editor
1907 // Maybe add support for merge of Render and Editor?
1908 if (!HasRenderer() && mergefrom
->HasRenderer() )
1910 m_renderer
= mergefrom
->m_renderer
;
1911 m_renderer
->IncRef();
1913 if ( !HasEditor() && mergefrom
->HasEditor() )
1915 m_editor
= mergefrom
->m_editor
;
1918 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1919 SetReadOnly(mergefrom
->IsReadOnly());
1921 SetDefAttr(mergefrom
->m_defGridAttr
);
1924 const wxColour
& wxGridCellAttr::GetTextColour() const
1926 if (HasTextColour())
1930 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1932 return m_defGridAttr
->GetTextColour();
1936 wxFAIL_MSG(wxT("Missing default cell attribute"));
1937 return wxNullColour
;
1942 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1944 if (HasBackgroundColour())
1946 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1947 return m_defGridAttr
->GetBackgroundColour();
1950 wxFAIL_MSG(wxT("Missing default cell attribute"));
1951 return wxNullColour
;
1956 const wxFont
& wxGridCellAttr::GetFont() const
1960 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1961 return m_defGridAttr
->GetFont();
1964 wxFAIL_MSG(wxT("Missing default cell attribute"));
1970 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1974 if ( hAlign
) *hAlign
= m_hAlign
;
1975 if ( vAlign
) *vAlign
= m_vAlign
;
1977 else if (m_defGridAttr
&& m_defGridAttr
!= this)
1978 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1981 wxFAIL_MSG(wxT("Missing default cell attribute"));
1986 // GetRenderer and GetEditor use a slightly different decision path about
1987 // which attribute to use. If a non-default attr object has one then it is
1988 // used, otherwise the default editor or renderer is fetched from the grid and
1989 // used. It should be the default for the data type of the cell. If it is
1990 // NULL (because the table has a type that the grid does not have in its
1991 // registry,) then the grid's default editor or renderer is used.
1993 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1995 wxGridCellRenderer
*renderer
;
1997 if ( m_renderer
&& this != m_defGridAttr
)
1999 // use the cells renderer if it has one
2000 renderer
= m_renderer
;
2003 else // no non default cell renderer
2005 // get default renderer for the data type
2008 // GetDefaultRendererForCell() will do IncRef() for us
2009 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2018 if (m_defGridAttr
&& this != m_defGridAttr
)
2020 // if we still don't have one then use the grid default
2021 // (no need for IncRef() here neither)
2022 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2024 else // default grid attr
2026 // use m_renderer which we had decided not to use initially
2027 renderer
= m_renderer
;
2034 // we're supposed to always find something
2035 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2040 // same as above, except for s/renderer/editor/g
2041 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2043 wxGridCellEditor
*editor
;
2045 if ( m_editor
&& this != m_defGridAttr
)
2047 // use the cells editor if it has one
2051 else // no non default cell editor
2053 // get default editor for the data type
2056 // GetDefaultEditorForCell() will do IncRef() for us
2057 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2066 if ( m_defGridAttr
&& this != m_defGridAttr
)
2068 // if we still don't have one then use the grid default
2069 // (no need for IncRef() here neither)
2070 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2072 else // default grid attr
2074 // use m_editor which we had decided not to use initially
2082 // we're supposed to always find something
2083 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2088 // ----------------------------------------------------------------------------
2089 // wxGridCellAttrData
2090 // ----------------------------------------------------------------------------
2092 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2094 int n
= FindIndex(row
, col
);
2095 if ( n
== wxNOT_FOUND
)
2097 // add the attribute
2098 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2102 // free the old attribute
2103 m_attrs
[(size_t)n
].attr
->DecRef();
2107 // change the attribute
2108 m_attrs
[(size_t)n
].attr
= attr
;
2112 // remove this attribute
2113 m_attrs
.RemoveAt((size_t)n
);
2118 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2120 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2122 int n
= FindIndex(row
, col
);
2123 if ( n
!= wxNOT_FOUND
)
2125 attr
= m_attrs
[(size_t)n
].attr
;
2132 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2134 size_t count
= m_attrs
.GetCount();
2135 for ( size_t n
= 0; n
< count
; n
++ )
2137 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2138 wxCoord row
= coords
.GetRow();
2139 if ((size_t)row
>= pos
)
2143 // If rows inserted, include row counter where necessary
2144 coords
.SetRow(row
+ numRows
);
2146 else if (numRows
< 0)
2148 // If rows deleted ...
2149 if ((size_t)row
>= pos
- numRows
)
2151 // ...either decrement row counter (if row still exists)...
2152 coords
.SetRow(row
+ numRows
);
2156 // ...or remove the attribute
2157 m_attrs
.RemoveAt((size_t)n
);
2165 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2167 size_t count
= m_attrs
.GetCount();
2168 for ( size_t n
= 0; n
< count
; n
++ )
2170 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2171 wxCoord col
= coords
.GetCol();
2172 if ( (size_t)col
>= pos
)
2176 // If rows inserted, include row counter where necessary
2177 coords
.SetCol(col
+ numCols
);
2179 else if (numCols
< 0)
2181 // If rows deleted ...
2182 if ((size_t)col
>= pos
- numCols
)
2184 // ...either decrement row counter (if row still exists)...
2185 coords
.SetCol(col
+ numCols
);
2189 // ...or remove the attribute
2190 m_attrs
.RemoveAt((size_t)n
);
2198 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2200 size_t count
= m_attrs
.GetCount();
2201 for ( size_t n
= 0; n
< count
; n
++ )
2203 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2204 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2213 // ----------------------------------------------------------------------------
2214 // wxGridRowOrColAttrData
2215 // ----------------------------------------------------------------------------
2217 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2219 size_t count
= m_attrs
.Count();
2220 for ( size_t n
= 0; n
< count
; n
++ )
2222 m_attrs
[n
]->DecRef();
2226 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2228 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2230 int n
= m_rowsOrCols
.Index(rowOrCol
);
2231 if ( n
!= wxNOT_FOUND
)
2233 attr
= m_attrs
[(size_t)n
];
2240 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2242 int i
= m_rowsOrCols
.Index(rowOrCol
);
2243 if ( i
== wxNOT_FOUND
)
2245 // add the attribute
2246 m_rowsOrCols
.Add(rowOrCol
);
2251 size_t n
= (size_t)i
;
2254 // change the attribute
2255 m_attrs
[n
]->DecRef();
2260 // remove this attribute
2261 m_attrs
[n
]->DecRef();
2262 m_rowsOrCols
.RemoveAt(n
);
2263 m_attrs
.RemoveAt(n
);
2268 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2270 size_t count
= m_attrs
.GetCount();
2271 for ( size_t n
= 0; n
< count
; n
++ )
2273 int & rowOrCol
= m_rowsOrCols
[n
];
2274 if ( (size_t)rowOrCol
>= pos
)
2276 if ( numRowsOrCols
> 0 )
2278 // If rows inserted, include row counter where necessary
2279 rowOrCol
+= numRowsOrCols
;
2281 else if ( numRowsOrCols
< 0)
2283 // If rows deleted, either decrement row counter (if row still exists)
2284 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2285 rowOrCol
+= numRowsOrCols
;
2288 m_rowsOrCols
.RemoveAt((size_t)n
);
2289 m_attrs
.RemoveAt((size_t)n
);
2297 // ----------------------------------------------------------------------------
2298 // wxGridCellAttrProvider
2299 // ----------------------------------------------------------------------------
2301 wxGridCellAttrProvider::wxGridCellAttrProvider()
2303 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2306 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2311 void wxGridCellAttrProvider::InitData()
2313 m_data
= new wxGridCellAttrProviderData
;
2316 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2317 wxGridCellAttr::wxAttrKind kind
) const
2319 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2324 case (wxGridCellAttr::Any
):
2325 //Get cached merge attributes.
2326 // Currenlty not used as no cache implemented as not mutiable
2327 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2330 //Basicaly implement old version.
2331 //Also check merge cache, so we don't have to re-merge every time..
2332 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2333 *attrrow
= (wxGridCellAttr
*)NULL
,
2334 *attrcol
= (wxGridCellAttr
*)NULL
;
2336 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2337 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2338 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2340 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2341 // Two or move are non NULL
2342 attr
= new wxGridCellAttr
;
2343 attr
->SetKind(wxGridCellAttr::Merged
);
2347 attr
->MergeWith(attrcell
);
2351 attr
->MergeWith(attrcol
);
2355 attr
->MergeWith(attrrow
);
2358 //store merge attr if cache implemented
2360 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2364 // one or none is non null return it or null.
2365 if(attrrow
) attr
= attrrow
;
2366 if(attrcol
) attr
= attrcol
;
2367 if(attrcell
) attr
= attrcell
;
2371 case (wxGridCellAttr::Cell
):
2372 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2374 case (wxGridCellAttr::Col
):
2375 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2377 case (wxGridCellAttr::Row
):
2378 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2382 // (wxGridCellAttr::Default):
2383 // (wxGridCellAttr::Merged):
2390 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2396 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2399 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2404 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2407 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2412 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2415 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2419 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2421 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2425 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2429 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2431 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2435 // ----------------------------------------------------------------------------
2436 // wxGridTypeRegistry
2437 // ----------------------------------------------------------------------------
2439 wxGridTypeRegistry::~wxGridTypeRegistry()
2441 size_t count
= m_typeinfo
.Count();
2442 for ( size_t i
= 0; i
< count
; i
++ )
2443 delete m_typeinfo
[i
];
2447 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2448 wxGridCellRenderer
* renderer
,
2449 wxGridCellEditor
* editor
)
2451 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2453 // is it already registered?
2454 int loc
= FindRegisteredDataType(typeName
);
2455 if ( loc
!= wxNOT_FOUND
)
2457 delete m_typeinfo
[loc
];
2458 m_typeinfo
[loc
] = info
;
2462 m_typeinfo
.Add(info
);
2466 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2468 size_t count
= m_typeinfo
.GetCount();
2469 for ( size_t i
= 0; i
< count
; i
++ )
2471 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2480 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2482 int index
= FindRegisteredDataType(typeName
);
2483 if ( index
== wxNOT_FOUND
)
2485 // check whether this is one of the standard ones, in which case
2486 // register it "on the fly"
2488 if ( typeName
== wxGRID_VALUE_STRING
)
2490 RegisterDataType(wxGRID_VALUE_STRING
,
2491 new wxGridCellStringRenderer
,
2492 new wxGridCellTextEditor
);
2494 #endif // wxUSE_TEXTCTRL
2496 if ( typeName
== wxGRID_VALUE_BOOL
)
2498 RegisterDataType(wxGRID_VALUE_BOOL
,
2499 new wxGridCellBoolRenderer
,
2500 new wxGridCellBoolEditor
);
2502 #endif // wxUSE_CHECKBOX
2504 if ( typeName
== wxGRID_VALUE_NUMBER
)
2506 RegisterDataType(wxGRID_VALUE_NUMBER
,
2507 new wxGridCellNumberRenderer
,
2508 new wxGridCellNumberEditor
);
2510 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2512 RegisterDataType(wxGRID_VALUE_FLOAT
,
2513 new wxGridCellFloatRenderer
,
2514 new wxGridCellFloatEditor
);
2516 #endif // wxUSE_TEXTCTRL
2518 if ( typeName
== wxGRID_VALUE_CHOICE
)
2520 RegisterDataType(wxGRID_VALUE_CHOICE
,
2521 new wxGridCellStringRenderer
,
2522 new wxGridCellChoiceEditor
);
2524 #endif // wxUSE_COMBOBOX
2529 // we get here only if just added the entry for this type, so return
2531 index
= m_typeinfo
.GetCount() - 1;
2537 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2539 int index
= FindDataType(typeName
);
2540 if ( index
== wxNOT_FOUND
)
2542 // the first part of the typename is the "real" type, anything after ':'
2543 // are the parameters for the renderer
2544 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2545 if ( index
== wxNOT_FOUND
)
2550 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2551 wxGridCellRenderer
*rendererOld
= renderer
;
2552 renderer
= renderer
->Clone();
2553 rendererOld
->DecRef();
2555 wxGridCellEditor
*editor
= GetEditor(index
);
2556 wxGridCellEditor
*editorOld
= editor
;
2557 editor
= editor
->Clone();
2558 editorOld
->DecRef();
2560 // do it even if there are no parameters to reset them to defaults
2561 wxString params
= typeName
.AfterFirst(_T(':'));
2562 renderer
->SetParameters(params
);
2563 editor
->SetParameters(params
);
2565 // register the new typename
2566 RegisterDataType(typeName
, renderer
, editor
);
2568 // we just registered it, it's the last one
2569 index
= m_typeinfo
.GetCount() - 1;
2575 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2577 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2583 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2585 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2591 // ----------------------------------------------------------------------------
2593 // ----------------------------------------------------------------------------
2595 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2598 wxGridTableBase::wxGridTableBase()
2600 m_view
= (wxGrid
*) NULL
;
2601 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2604 wxGridTableBase::~wxGridTableBase()
2606 delete m_attrProvider
;
2609 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2611 delete m_attrProvider
;
2612 m_attrProvider
= attrProvider
;
2615 bool wxGridTableBase::CanHaveAttributes()
2617 if ( ! GetAttrProvider() )
2619 // use the default attr provider by default
2620 SetAttrProvider(new wxGridCellAttrProvider
);
2625 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2627 if ( m_attrProvider
)
2628 return m_attrProvider
->GetAttr(row
, col
, kind
);
2630 return (wxGridCellAttr
*)NULL
;
2633 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2635 if ( m_attrProvider
)
2637 attr
->SetKind(wxGridCellAttr::Cell
);
2638 m_attrProvider
->SetAttr(attr
, row
, col
);
2642 // as we take ownership of the pointer and don't store it, we must
2648 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2650 if ( m_attrProvider
)
2652 attr
->SetKind(wxGridCellAttr::Row
);
2653 m_attrProvider
->SetRowAttr(attr
, row
);
2657 // as we take ownership of the pointer and don't store it, we must
2663 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2665 if ( m_attrProvider
)
2667 attr
->SetKind(wxGridCellAttr::Col
);
2668 m_attrProvider
->SetColAttr(attr
, col
);
2672 // as we take ownership of the pointer and don't store it, we must
2678 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2679 size_t WXUNUSED(numRows
) )
2681 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2686 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2688 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2693 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2694 size_t WXUNUSED(numRows
) )
2696 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2701 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2702 size_t WXUNUSED(numCols
) )
2704 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2709 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2711 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2716 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2717 size_t WXUNUSED(numCols
) )
2719 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2725 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2728 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2729 // how much it makes sense to us geeks.
2733 wxString
wxGridTableBase::GetColLabelValue( int col
)
2735 // default col labels are:
2736 // cols 0 to 25 : A-Z
2737 // cols 26 to 675 : AA-ZZ
2742 for ( n
= 1; ; n
++ )
2744 s
+= (_T('A') + (wxChar
)( col%26
));
2746 if ( col
< 0 ) break;
2749 // reverse the string...
2751 for ( i
= 0; i
< n
; i
++ )
2760 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2762 return wxGRID_VALUE_STRING
;
2765 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2766 const wxString
& typeName
)
2768 return typeName
== wxGRID_VALUE_STRING
;
2771 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2773 return CanGetValueAs(row
, col
, typeName
);
2776 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2781 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2786 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2791 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2792 long WXUNUSED(value
) )
2796 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2797 double WXUNUSED(value
) )
2801 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2802 bool WXUNUSED(value
) )
2807 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2808 const wxString
& WXUNUSED(typeName
) )
2813 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2814 const wxString
& WXUNUSED(typeName
),
2815 void* WXUNUSED(value
) )
2819 //////////////////////////////////////////////////////////////////////
2821 // Message class for the grid table to send requests and notifications
2825 wxGridTableMessage::wxGridTableMessage()
2827 m_table
= (wxGridTableBase
*) NULL
;
2833 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2834 int commandInt1
, int commandInt2
)
2838 m_comInt1
= commandInt1
;
2839 m_comInt2
= commandInt2
;
2844 //////////////////////////////////////////////////////////////////////
2846 // A basic grid table for string data. An object of this class will
2847 // created by wxGrid if you don't specify an alternative table class.
2850 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2852 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2854 wxGridStringTable::wxGridStringTable()
2859 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2864 m_data
.Alloc( numRows
);
2867 sa
.Alloc( numCols
);
2868 for ( col
= 0; col
< numCols
; col
++ )
2870 sa
.Add( wxEmptyString
);
2873 for ( row
= 0; row
< numRows
; row
++ )
2879 wxGridStringTable::~wxGridStringTable()
2883 int wxGridStringTable::GetNumberRows()
2885 return m_data
.GetCount();
2888 int wxGridStringTable::GetNumberCols()
2890 if ( m_data
.GetCount() > 0 )
2891 return m_data
[0].GetCount();
2896 wxString
wxGridStringTable::GetValue( int row
, int col
)
2898 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2899 _T("invalid row or column index in wxGridStringTable") );
2901 return m_data
[row
][col
];
2904 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2906 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2907 _T("invalid row or column index in wxGridStringTable") );
2909 m_data
[row
][col
] = value
;
2912 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2914 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2915 _T("invalid row or column index in wxGridStringTable") );
2917 return (m_data
[row
][col
] == wxEmptyString
);
2920 void wxGridStringTable::Clear()
2923 int numRows
, numCols
;
2925 numRows
= m_data
.GetCount();
2928 numCols
= m_data
[0].GetCount();
2930 for ( row
= 0; row
< numRows
; row
++ )
2932 for ( col
= 0; col
< numCols
; col
++ )
2934 m_data
[row
][col
] = wxEmptyString
;
2941 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2945 size_t curNumRows
= m_data
.GetCount();
2946 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2947 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2949 if ( pos
>= curNumRows
)
2951 return AppendRows( numRows
);
2955 sa
.Alloc( curNumCols
);
2956 for ( col
= 0; col
< curNumCols
; col
++ )
2958 sa
.Add( wxEmptyString
);
2961 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2963 m_data
.Insert( sa
, row
);
2967 wxGridTableMessage
msg( this,
2968 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2972 GetView()->ProcessTableMessage( msg
);
2978 bool wxGridStringTable::AppendRows( size_t numRows
)
2982 size_t curNumRows
= m_data
.GetCount();
2983 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2984 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2987 if ( curNumCols
> 0 )
2989 sa
.Alloc( curNumCols
);
2990 for ( col
= 0; col
< curNumCols
; col
++ )
2992 sa
.Add( wxEmptyString
);
2996 for ( row
= 0; row
< numRows
; row
++ )
3003 wxGridTableMessage
msg( this,
3004 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3007 GetView()->ProcessTableMessage( msg
);
3013 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3017 size_t curNumRows
= m_data
.GetCount();
3019 if ( pos
>= curNumRows
)
3022 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
3023 pos
, numRows
, curNumRows
);
3024 wxFAIL_MSG( errmsg
);
3028 if ( numRows
> curNumRows
- pos
)
3030 numRows
= curNumRows
- pos
;
3033 if ( numRows
>= curNumRows
)
3035 m_data
.Empty(); // don't release memory just yet
3039 for ( n
= 0; n
< numRows
; n
++ )
3041 m_data
.RemoveAt( pos
);
3046 wxGridTableMessage
msg( this,
3047 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3051 GetView()->ProcessTableMessage( msg
);
3057 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3061 size_t curNumRows
= m_data
.GetCount();
3062 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3063 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3065 if ( pos
>= curNumCols
)
3067 return AppendCols( numCols
);
3070 for ( row
= 0; row
< curNumRows
; row
++ )
3072 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3074 m_data
[row
].Insert( wxEmptyString
, col
);
3079 wxGridTableMessage
msg( this,
3080 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3084 GetView()->ProcessTableMessage( msg
);
3090 bool wxGridStringTable::AppendCols( size_t numCols
)
3094 size_t curNumRows
= m_data
.GetCount();
3098 // TODO: something better than this ?
3100 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3105 for ( row
= 0; row
< curNumRows
; row
++ )
3107 for ( n
= 0; n
< numCols
; n
++ )
3109 m_data
[row
].Add( wxEmptyString
);
3115 wxGridTableMessage
msg( this,
3116 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3119 GetView()->ProcessTableMessage( msg
);
3125 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3129 size_t curNumRows
= m_data
.GetCount();
3130 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3131 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3133 if ( pos
>= curNumCols
)
3136 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3137 pos
, numCols
, curNumCols
);
3138 wxFAIL_MSG( errmsg
);
3142 if ( numCols
> curNumCols
- pos
)
3144 numCols
= curNumCols
- pos
;
3147 for ( row
= 0; row
< curNumRows
; row
++ )
3149 if ( numCols
>= curNumCols
)
3151 m_data
[row
].Clear();
3155 for ( n
= 0; n
< numCols
; n
++ )
3157 m_data
[row
].RemoveAt( pos
);
3163 wxGridTableMessage
msg( this,
3164 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3168 GetView()->ProcessTableMessage( msg
);
3174 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3176 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3178 // using default label
3180 return wxGridTableBase::GetRowLabelValue( row
);
3184 return m_rowLabels
[ row
];
3188 wxString
wxGridStringTable::GetColLabelValue( int col
)
3190 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3192 // using default label
3194 return wxGridTableBase::GetColLabelValue( col
);
3198 return m_colLabels
[ col
];
3202 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3204 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3206 int n
= m_rowLabels
.GetCount();
3208 for ( i
= n
; i
<= row
; i
++ )
3210 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3214 m_rowLabels
[row
] = value
;
3217 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3219 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3221 int n
= m_colLabels
.GetCount();
3223 for ( i
= n
; i
<= col
; i
++ )
3225 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3229 m_colLabels
[col
] = value
;
3234 //////////////////////////////////////////////////////////////////////
3235 //////////////////////////////////////////////////////////////////////
3237 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3239 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3240 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3241 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3242 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3243 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3244 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3247 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3249 const wxPoint
&pos
, const wxSize
&size
)
3250 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3255 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3259 // NO - don't do this because it will set both the x and y origin
3260 // coords to match the parent scrolled window and we just want to
3261 // set the y coord - MB
3263 // m_owner->PrepareDC( dc );
3266 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3267 dc
.SetDeviceOrigin( 0, -y
);
3269 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3270 m_owner
->DrawRowLabels( dc
, rows
);
3274 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3276 m_owner
->ProcessRowLabelMouseEvent( event
);
3280 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3282 m_owner
->GetEventHandler()->ProcessEvent(event
);
3286 // This seems to be required for wxMotif otherwise the mouse
3287 // cursor must be in the cell edit control to get key events
3289 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3291 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3294 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3296 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3301 //////////////////////////////////////////////////////////////////////
3303 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3305 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3306 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3307 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3308 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3309 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3310 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3313 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3315 const wxPoint
&pos
, const wxSize
&size
)
3316 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3321 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3325 // NO - don't do this because it will set both the x and y origin
3326 // coords to match the parent scrolled window and we just want to
3327 // set the x coord - MB
3329 // m_owner->PrepareDC( dc );
3332 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3333 dc
.SetDeviceOrigin( -x
, 0 );
3335 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3336 m_owner
->DrawColLabels( dc
, cols
);
3340 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3342 m_owner
->ProcessColLabelMouseEvent( event
);
3345 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3347 m_owner
->GetEventHandler()->ProcessEvent(event
);
3351 // This seems to be required for wxMotif otherwise the mouse
3352 // cursor must be in the cell edit control to get key events
3354 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3356 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3359 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3361 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3366 //////////////////////////////////////////////////////////////////////
3368 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3370 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3371 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3372 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3373 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3374 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3375 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3378 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3380 const wxPoint
&pos
, const wxSize
&size
)
3381 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3386 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3390 int client_height
= 0;
3391 int client_width
= 0;
3392 GetClientSize( &client_width
, &client_height
);
3394 dc
.SetPen( *wxBLACK_PEN
);
3395 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3396 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3398 dc
.SetPen( *wxWHITE_PEN
);
3399 dc
.DrawLine( 0, 0, client_width
, 0 );
3400 dc
.DrawLine( 0, 0, 0, client_height
);
3404 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3406 m_owner
->ProcessCornerLabelMouseEvent( event
);
3410 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3412 m_owner
->GetEventHandler()->ProcessEvent(event
);
3415 // This seems to be required for wxMotif otherwise the mouse
3416 // cursor must be in the cell edit control to get key events
3418 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3420 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3423 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3425 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3430 //////////////////////////////////////////////////////////////////////
3432 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3434 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3435 EVT_PAINT( wxGridWindow::OnPaint
)
3436 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3437 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3438 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3439 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3440 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3443 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3444 wxGridRowLabelWindow
*rowLblWin
,
3445 wxGridColLabelWindow
*colLblWin
,
3446 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3447 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3450 m_rowLabelWin
= rowLblWin
;
3451 m_colLabelWin
= colLblWin
;
3452 SetBackgroundColour(_T("WHITE"));
3456 wxGridWindow::~wxGridWindow()
3461 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3463 wxPaintDC
dc( this );
3464 m_owner
->PrepareDC( dc
);
3465 wxRegion reg
= GetUpdateRegion();
3466 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3467 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3468 #if WXGRID_DRAW_LINES
3469 m_owner
->DrawAllGridLines( dc
, reg
);
3471 m_owner
->DrawGridSpace( dc
);
3472 m_owner
->DrawHighlight( dc
, DirtyCells
);
3476 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3478 wxWindow::ScrollWindow( dx
, dy
, rect
);
3479 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3480 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3484 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3486 m_owner
->ProcessGridCellMouseEvent( event
);
3489 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3491 m_owner
->GetEventHandler()->ProcessEvent(event
);
3494 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3495 // cursor must be in the cell edit control to get key events
3497 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3499 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3502 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3504 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3507 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3512 //////////////////////////////////////////////////////////////////////
3514 // Internal Helper function for computing row or column from some
3515 // (unscrolled) coordinate value, using either
3516 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3517 // of m_rowBottoms/m_ColRights to speed up the search!
3519 // Internal helper macros for simpler use of that function
3521 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3522 const wxArrayInt
& BorderArray
, bool maxOnOverflow
);
3524 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3525 WXGRID_MIN_COL_WIDTH, \
3527 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3528 WXGRID_MIN_ROW_HEIGHT, \
3530 /////////////////////////////////////////////////////////////////////
3532 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3534 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3535 EVT_PAINT( wxGrid::OnPaint
)
3536 EVT_SIZE( wxGrid::OnSize
)
3537 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3538 EVT_KEY_UP( wxGrid::OnKeyUp
)
3539 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3542 wxGrid::wxGrid( wxWindow
*parent
,
3547 const wxString
& name
)
3548 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3549 m_colMinWidths(GRID_HASH_SIZE
),
3550 m_rowMinHeights(GRID_HASH_SIZE
)
3558 // Must do this or ~wxScrollHelper will pop the wrong event handler
3559 SetTargetWindow(this);
3561 wxSafeDecRef(m_defaultCellAttr
);
3563 #ifdef DEBUG_ATTR_CACHE
3564 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3565 wxPrintf(_T("wxGrid attribute cache statistics: "
3566 "total: %u, hits: %u (%u%%)\n"),
3567 total
, gs_nAttrCacheHits
,
3568 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3574 delete m_typeRegistry
;
3580 // ----- internal init and update functions
3583 void wxGrid::Create()
3585 m_created
= FALSE
; // set to TRUE by CreateGrid
3587 m_table
= (wxGridTableBase
*) NULL
;
3590 m_cellEditCtrlEnabled
= FALSE
;
3592 m_defaultCellAttr
= new wxGridCellAttr();
3594 // Set default cell attributes
3595 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3596 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3597 m_defaultCellAttr
->SetFont(GetFont());
3598 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3599 m_defaultCellAttr
->SetTextColour(
3600 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3601 m_defaultCellAttr
->SetBackgroundColour(
3602 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3603 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3604 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3609 m_currentCellCoords
= wxGridNoCellCoords
;
3611 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3612 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3614 // create the type registry
3615 m_typeRegistry
= new wxGridTypeRegistry
;
3618 // subwindow components that make up the wxGrid
3619 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3624 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3629 m_colLabelWin
= new wxGridColLabelWindow( this,
3634 m_gridWin
= new wxGridWindow( this,
3641 SetTargetWindow( m_gridWin
);
3647 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3648 wxGrid::wxGridSelectionModes selmode
)
3650 wxCHECK_MSG( !m_created
,
3652 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3654 m_numRows
= numRows
;
3655 m_numCols
= numCols
;
3657 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3658 m_table
->SetView( this );
3660 m_selection
= new wxGridSelection( this, selmode
);
3669 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3671 wxCHECK_RET( m_created
,
3672 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3674 m_selection
->SetSelectionMode( selmode
);
3677 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3678 wxGrid::wxGridSelectionModes selmode
)
3682 // RD: Actually, this should probably be allowed. I think it would be
3683 // nice to be able to switch multiple Tables in and out of a single
3684 // View at runtime. Is there anything in the implementation that
3685 // would prevent this?
3687 // At least, you now have to cope with m_selection
3688 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3693 m_numRows
= table
->GetNumberRows();
3694 m_numCols
= table
->GetNumberCols();
3697 m_table
->SetView( this );
3700 m_selection
= new wxGridSelection( this, selmode
);
3713 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3714 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3716 if ( m_rowLabelWin
)
3718 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3722 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3725 m_labelTextColour
= wxColour( _T("BLACK") );
3728 m_attrCache
.row
= -1;
3730 // TODO: something better than this ?
3732 m_labelFont
= this->GetFont();
3733 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3735 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3736 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3738 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3739 m_colLabelVertAlign
= wxALIGN_TOP
;
3741 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3742 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3744 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3745 m_defaultRowHeight
+= 8;
3747 m_defaultRowHeight
+= 4;
3750 m_gridLineColour
= wxColour( 128, 128, 255 );
3751 m_gridLinesEnabled
= TRUE
;
3752 m_cellHighlightColour
= m_gridLineColour
;
3753 m_cellHighlightPenWidth
= 2;
3754 m_cellHighlightROPenWidth
= 1;
3756 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3757 m_winCapture
= (wxWindow
*)NULL
;
3758 m_canDragRowSize
= TRUE
;
3759 m_canDragColSize
= TRUE
;
3760 m_canDragGridSize
= TRUE
;
3762 m_dragRowOrCol
= -1;
3763 m_isDragging
= FALSE
;
3764 m_startDragPos
= wxDefaultPosition
;
3766 m_waitForSlowClick
= FALSE
;
3768 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3769 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3771 m_currentCellCoords
= wxGridNoCellCoords
;
3773 m_selectingTopLeft
= wxGridNoCellCoords
;
3774 m_selectingBottomRight
= wxGridNoCellCoords
;
3775 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
3776 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3778 m_editable
= TRUE
; // default for whole grid
3780 m_inOnKeyDown
= FALSE
;
3787 // ----------------------------------------------------------------------------
3788 // the idea is to call these functions only when necessary because they create
3789 // quite big arrays which eat memory mostly unnecessary - in particular, if
3790 // default widths/heights are used for all rows/columns, we may not use these
3793 // with some extra code, it should be possible to only store the
3794 // widths/heights different from default ones but this will be done later...
3795 // ----------------------------------------------------------------------------
3797 void wxGrid::InitRowHeights()
3799 m_rowHeights
.Empty();
3800 m_rowBottoms
.Empty();
3802 m_rowHeights
.Alloc( m_numRows
);
3803 m_rowBottoms
.Alloc( m_numRows
);
3806 for ( int i
= 0; i
< m_numRows
; i
++ )
3808 m_rowHeights
.Add( m_defaultRowHeight
);
3809 rowBottom
+= m_defaultRowHeight
;
3810 m_rowBottoms
.Add( rowBottom
);
3814 void wxGrid::InitColWidths()
3816 m_colWidths
.Empty();
3817 m_colRights
.Empty();
3819 m_colWidths
.Alloc( m_numCols
);
3820 m_colRights
.Alloc( m_numCols
);
3822 for ( int i
= 0; i
< m_numCols
; i
++ )
3824 m_colWidths
.Add( m_defaultColWidth
);
3825 colRight
+= m_defaultColWidth
;
3826 m_colRights
.Add( colRight
);
3830 int wxGrid::GetColWidth(int col
) const
3832 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3835 int wxGrid::GetColLeft(int col
) const
3837 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3838 : m_colRights
[col
] - m_colWidths
[col
];
3841 int wxGrid::GetColRight(int col
) const
3843 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3847 int wxGrid::GetRowHeight(int row
) const
3849 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3852 int wxGrid::GetRowTop(int row
) const
3854 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3855 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3858 int wxGrid::GetRowBottom(int row
) const
3860 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3861 : m_rowBottoms
[row
];
3864 void wxGrid::CalcDimensions()
3867 GetClientSize( &cw
, &ch
);
3869 if ( m_rowLabelWin
->IsShown() )
3870 cw
-= m_rowLabelWidth
;
3871 if ( m_colLabelWin
->IsShown() )
3872 ch
-= m_colLabelHeight
;
3875 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3876 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3878 // preserve (more or less) the previous position
3880 GetViewStart( &x
, &y
);
3882 // maybe we don't need scrollbars at all?
3884 // also adjust the position to be valid for the new scroll rangs
3905 // do set scrollbar parameters
3906 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
3907 GetScrollX(w
), GetScrollY(h
), x
, y
,
3908 GetBatchCount() != 0);
3910 // if our OnSize() hadn't been called (it would if we have scrollbars), we
3911 // still must reposition the children
3916 void wxGrid::CalcWindowSizes()
3919 GetClientSize( &cw
, &ch
);
3921 if ( m_cornerLabelWin
->IsShown() )
3922 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3924 if ( m_colLabelWin
->IsShown() )
3925 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3927 if ( m_rowLabelWin
->IsShown() )
3928 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3930 if ( m_gridWin
->IsShown() )
3931 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3935 // this is called when the grid table sends a message to say that it
3936 // has been redimensioned
3938 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3941 bool result
= FALSE
;
3944 // if we were using the default widths/heights so far, we must change them
3946 if ( m_colWidths
.IsEmpty() )
3951 if ( m_rowHeights
.IsEmpty() )
3957 switch ( msg
.GetId() )
3959 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3961 size_t pos
= msg
.GetCommandInt();
3962 int numRows
= msg
.GetCommandInt2();
3964 m_numRows
+= numRows
;
3966 if ( !m_rowHeights
.IsEmpty() )
3968 for ( i
= 0; i
< numRows
; i
++ )
3970 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3971 m_rowBottoms
.Insert( 0, pos
);
3975 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3977 for ( i
= pos
; i
< m_numRows
; i
++ )
3979 bottom
+= m_rowHeights
[i
];
3980 m_rowBottoms
[i
] = bottom
;
3983 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3985 // if we have just inserted cols into an empty grid the current
3986 // cell will be undefined...
3988 SetCurrentCell( 0, 0 );
3992 m_selection
->UpdateRows( pos
, numRows
);
3993 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3995 attrProvider
->UpdateAttrRows( pos
, numRows
);
3997 if ( !GetBatchCount() )
4000 m_rowLabelWin
->Refresh();
4006 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4008 int numRows
= msg
.GetCommandInt();
4009 int oldNumRows
= m_numRows
;
4010 m_numRows
+= numRows
;
4012 if ( !m_rowHeights
.IsEmpty() )
4014 for ( i
= 0; i
< numRows
; i
++ )
4016 m_rowHeights
.Add( m_defaultRowHeight
);
4017 m_rowBottoms
.Add( 0 );
4021 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4023 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4025 bottom
+= m_rowHeights
[i
];
4026 m_rowBottoms
[i
] = bottom
;
4029 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4031 // if we have just inserted cols into an empty grid the current
4032 // cell will be undefined...
4034 SetCurrentCell( 0, 0 );
4036 if ( !GetBatchCount() )
4039 m_rowLabelWin
->Refresh();
4045 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4047 size_t pos
= msg
.GetCommandInt();
4048 int numRows
= msg
.GetCommandInt2();
4049 m_numRows
-= numRows
;
4051 if ( !m_rowHeights
.IsEmpty() )
4053 for ( i
= 0; i
< numRows
; i
++ )
4055 m_rowHeights
.RemoveAt( pos
);
4056 m_rowBottoms
.RemoveAt( pos
);
4060 for ( i
= 0; i
< m_numRows
; i
++ )
4062 h
+= m_rowHeights
[i
];
4063 m_rowBottoms
[i
] = h
;
4068 m_currentCellCoords
= wxGridNoCellCoords
;
4072 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4073 m_currentCellCoords
.Set( 0, 0 );
4077 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4078 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4080 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4081 // ifdef'd out following patch from Paul Gammans
4083 // No need to touch column attributes, unless we
4084 // removed _all_ rows, in this case, we remove
4085 // all column attributes.
4086 // I hate to do this here, but the
4087 // needed data is not available inside UpdateAttrRows.
4088 if ( !GetNumberRows() )
4089 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4092 if ( !GetBatchCount() )
4095 m_rowLabelWin
->Refresh();
4101 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4103 size_t pos
= msg
.GetCommandInt();
4104 int numCols
= msg
.GetCommandInt2();
4105 m_numCols
+= numCols
;
4107 if ( !m_colWidths
.IsEmpty() )
4109 for ( i
= 0; i
< numCols
; i
++ )
4111 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4112 m_colRights
.Insert( 0, pos
);
4116 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4118 for ( i
= pos
; i
< m_numCols
; i
++ )
4120 right
+= m_colWidths
[i
];
4121 m_colRights
[i
] = right
;
4124 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4126 // if we have just inserted cols into an empty grid the current
4127 // cell will be undefined...
4129 SetCurrentCell( 0, 0 );
4133 m_selection
->UpdateCols( pos
, numCols
);
4134 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4136 attrProvider
->UpdateAttrCols( pos
, numCols
);
4137 if ( !GetBatchCount() )
4140 m_colLabelWin
->Refresh();
4147 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4149 int numCols
= msg
.GetCommandInt();
4150 int oldNumCols
= m_numCols
;
4151 m_numCols
+= numCols
;
4152 if ( !m_colWidths
.IsEmpty() )
4154 for ( i
= 0; i
< numCols
; i
++ )
4156 m_colWidths
.Add( m_defaultColWidth
);
4157 m_colRights
.Add( 0 );
4161 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4163 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4165 right
+= m_colWidths
[i
];
4166 m_colRights
[i
] = right
;
4169 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4171 // if we have just inserted cols into an empty grid the current
4172 // cell will be undefined...
4174 SetCurrentCell( 0, 0 );
4176 if ( !GetBatchCount() )
4179 m_colLabelWin
->Refresh();
4185 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4187 size_t pos
= msg
.GetCommandInt();
4188 int numCols
= msg
.GetCommandInt2();
4189 m_numCols
-= numCols
;
4191 if ( !m_colWidths
.IsEmpty() )
4193 for ( i
= 0; i
< numCols
; i
++ )
4195 m_colWidths
.RemoveAt( pos
);
4196 m_colRights
.RemoveAt( pos
);
4200 for ( i
= 0; i
< m_numCols
; i
++ )
4202 w
+= m_colWidths
[i
];
4208 m_currentCellCoords
= wxGridNoCellCoords
;
4212 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4213 m_currentCellCoords
.Set( 0, 0 );
4217 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4218 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4220 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4221 // ifdef'd out following patch from Paul Gammans
4223 // No need to touch row attributes, unless we
4224 // removed _all_ columns, in this case, we remove
4225 // all row attributes.
4226 // I hate to do this here, but the
4227 // needed data is not available inside UpdateAttrCols.
4228 if ( !GetNumberCols() )
4229 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4232 if ( !GetBatchCount() )
4235 m_colLabelWin
->Refresh();
4242 if (result
&& !GetBatchCount() )
4243 m_gridWin
->Refresh();
4248 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4250 wxRegionIterator
iter( reg
);
4253 wxArrayInt rowlabels
;
4260 // TODO: remove this when we can...
4261 // There is a bug in wxMotif that gives garbage update
4262 // rectangles if you jump-scroll a long way by clicking the
4263 // scrollbar with middle button. This is a work-around
4265 #if defined(__WXMOTIF__)
4267 m_gridWin
->GetClientSize( &cw
, &ch
);
4268 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4269 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4272 // logical bounds of update region
4275 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4276 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4278 // find the row labels within these bounds
4281 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4283 if ( GetRowBottom(row
) < top
)
4286 if ( GetRowTop(row
) > bottom
)
4289 rowlabels
.Add( row
);
4299 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4301 wxRegionIterator
iter( reg
);
4304 wxArrayInt colLabels
;
4311 // TODO: remove this when we can...
4312 // There is a bug in wxMotif that gives garbage update
4313 // rectangles if you jump-scroll a long way by clicking the
4314 // scrollbar with middle button. This is a work-around
4316 #if defined(__WXMOTIF__)
4318 m_gridWin
->GetClientSize( &cw
, &ch
);
4319 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4320 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4323 // logical bounds of update region
4326 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4327 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4329 // find the cells within these bounds
4332 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4334 if ( GetColRight(col
) < left
)
4337 if ( GetColLeft(col
) > right
)
4340 colLabels
.Add( col
);
4349 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4351 wxRegionIterator
iter( reg
);
4354 wxGridCellCoordsArray cellsExposed
;
4356 int left
, top
, right
, bottom
;
4361 // TODO: remove this when we can...
4362 // There is a bug in wxMotif that gives garbage update
4363 // rectangles if you jump-scroll a long way by clicking the
4364 // scrollbar with middle button. This is a work-around
4366 #if defined(__WXMOTIF__)
4368 m_gridWin
->GetClientSize( &cw
, &ch
);
4369 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4370 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4371 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4372 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4375 // logical bounds of update region
4377 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4378 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4380 // find the cells within these bounds
4383 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4385 if ( GetRowBottom(row
) <= top
)
4388 if ( GetRowTop(row
) > bottom
)
4391 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4393 if ( GetColRight(col
) <= left
)
4396 if ( GetColLeft(col
) > right
)
4399 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4406 return cellsExposed
;
4410 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4413 wxPoint
pos( event
.GetPosition() );
4414 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4416 if ( event
.Dragging() )
4420 m_isDragging
= TRUE
;
4421 m_rowLabelWin
->CaptureMouse();
4424 if ( event
.LeftIsDown() )
4426 switch( m_cursorMode
)
4428 case WXGRID_CURSOR_RESIZE_ROW
:
4430 int cw
, ch
, left
, dummy
;
4431 m_gridWin
->GetClientSize( &cw
, &ch
);
4432 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4434 wxClientDC
dc( m_gridWin
);
4437 GetRowTop(m_dragRowOrCol
) +
4438 GetRowMinimalHeight(m_dragRowOrCol
) );
4439 dc
.SetLogicalFunction(wxINVERT
);
4440 if ( m_dragLastPos
>= 0 )
4442 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4444 dc
.DrawLine( left
, y
, left
+cw
, y
);
4449 case WXGRID_CURSOR_SELECT_ROW
:
4450 if ( (row
= YToRow( y
)) >= 0 )
4454 m_selection
->SelectRow( row
,
4455 event
.ControlDown(),
4462 // default label to suppress warnings about "enumeration value
4463 // 'xxx' not handled in switch
4471 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4476 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4477 m_isDragging
= FALSE
;
4480 // ------------ Entering or leaving the window
4482 if ( event
.Entering() || event
.Leaving() )
4484 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4488 // ------------ Left button pressed
4490 else if ( event
.LeftDown() )
4492 // don't send a label click event for a hit on the
4493 // edge of the row label - this is probably the user
4494 // wanting to resize the row
4496 if ( YToEdgeOfRow(y
) < 0 )
4500 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4502 if ( !event
.ShiftDown() && !event
.ControlDown() )
4504 else if ( m_selection
)
4506 if ( event
.ShiftDown() )
4508 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4511 GetNumberCols() - 1,
4512 event
.ControlDown(),
4519 m_selection
->SelectRow( row
,
4520 event
.ControlDown(),
4527 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4532 // starting to drag-resize a row
4534 if ( CanDragRowSize() )
4535 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4540 // ------------ Left double click
4542 else if (event
.LeftDClick() )
4544 if ( YToEdgeOfRow(y
) < 0 )
4547 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4552 // ------------ Left button released
4554 else if ( event
.LeftUp() )
4556 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4558 DoEndDragResizeRow();
4560 // Note: we are ending the event *after* doing
4561 // default processing in this case
4563 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4566 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4571 // ------------ Right button down
4573 else if ( event
.RightDown() )
4576 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4578 // no default action at the moment
4583 // ------------ Right double click
4585 else if ( event
.RightDClick() )
4588 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4590 // no default action at the moment
4595 // ------------ No buttons down and mouse moving
4597 else if ( event
.Moving() )
4599 m_dragRowOrCol
= YToEdgeOfRow( y
);
4600 if ( m_dragRowOrCol
>= 0 )
4602 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4604 // don't capture the mouse yet
4605 if ( CanDragRowSize() )
4606 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4609 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4611 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4617 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4620 wxPoint
pos( event
.GetPosition() );
4621 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4623 if ( event
.Dragging() )
4627 m_isDragging
= TRUE
;
4628 m_colLabelWin
->CaptureMouse();
4631 if ( event
.LeftIsDown() )
4633 switch( m_cursorMode
)
4635 case WXGRID_CURSOR_RESIZE_COL
:
4637 int cw
, ch
, dummy
, top
;
4638 m_gridWin
->GetClientSize( &cw
, &ch
);
4639 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4641 wxClientDC
dc( m_gridWin
);
4644 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4645 GetColMinimalWidth(m_dragRowOrCol
));
4646 dc
.SetLogicalFunction(wxINVERT
);
4647 if ( m_dragLastPos
>= 0 )
4649 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4651 dc
.DrawLine( x
, top
, x
, top
+ch
);
4656 case WXGRID_CURSOR_SELECT_COL
:
4657 if ( (col
= XToCol( x
)) >= 0 )
4661 m_selection
->SelectCol( col
,
4662 event
.ControlDown(),
4669 // default label to suppress warnings about "enumeration value
4670 // 'xxx' not handled in switch
4678 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4683 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
4684 m_isDragging
= FALSE
;
4687 // ------------ Entering or leaving the window
4689 if ( event
.Entering() || event
.Leaving() )
4691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4695 // ------------ Left button pressed
4697 else if ( event
.LeftDown() )
4699 // don't send a label click event for a hit on the
4700 // edge of the col label - this is probably the user
4701 // wanting to resize the col
4703 if ( XToEdgeOfCol(x
) < 0 )
4707 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4709 if ( !event
.ShiftDown() && !event
.ControlDown() )
4713 if ( event
.ShiftDown() )
4715 m_selection
->SelectBlock( 0,
4716 m_currentCellCoords
.GetCol(),
4717 GetNumberRows() - 1, col
,
4718 event
.ControlDown(),
4725 m_selection
->SelectCol( col
,
4726 event
.ControlDown(),
4733 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4738 // starting to drag-resize a col
4740 if ( CanDragColSize() )
4741 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4746 // ------------ Left double click
4748 if ( event
.LeftDClick() )
4750 if ( XToEdgeOfCol(x
) < 0 )
4753 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4758 // ------------ Left button released
4760 else if ( event
.LeftUp() )
4762 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4764 DoEndDragResizeCol();
4766 // Note: we are ending the event *after* doing
4767 // default processing in this case
4769 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4772 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4777 // ------------ Right button down
4779 else if ( event
.RightDown() )
4782 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4784 // no default action at the moment
4789 // ------------ Right double click
4791 else if ( event
.RightDClick() )
4794 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4796 // no default action at the moment
4801 // ------------ No buttons down and mouse moving
4803 else if ( event
.Moving() )
4805 m_dragRowOrCol
= XToEdgeOfCol( x
);
4806 if ( m_dragRowOrCol
>= 0 )
4808 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4810 // don't capture the cursor yet
4811 if ( CanDragColSize() )
4812 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4815 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4817 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4823 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4825 if ( event
.LeftDown() )
4827 // indicate corner label by having both row and
4830 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4836 else if ( event
.LeftDClick() )
4838 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4841 else if ( event
.RightDown() )
4843 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4845 // no default action at the moment
4849 else if ( event
.RightDClick() )
4851 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4853 // no default action at the moment
4858 void wxGrid::ChangeCursorMode(CursorMode mode
,
4863 static const wxChar
*cursorModes
[] =
4872 wxLogTrace(_T("grid"),
4873 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4874 win
== m_colLabelWin
? _T("colLabelWin")
4875 : win
? _T("rowLabelWin")
4877 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4878 #endif // __WXDEBUG__
4880 if ( mode
== m_cursorMode
&&
4881 win
== m_winCapture
&&
4882 captureMouse
== (m_winCapture
!= NULL
))
4887 // by default use the grid itself
4893 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
4894 m_winCapture
= (wxWindow
*)NULL
;
4897 m_cursorMode
= mode
;
4899 switch ( m_cursorMode
)
4901 case WXGRID_CURSOR_RESIZE_ROW
:
4902 win
->SetCursor( m_rowResizeCursor
);
4905 case WXGRID_CURSOR_RESIZE_COL
:
4906 win
->SetCursor( m_colResizeCursor
);
4910 win
->SetCursor( *wxSTANDARD_CURSOR
);
4913 // we need to capture mouse when resizing
4914 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4915 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4917 if ( captureMouse
&& resize
)
4919 win
->CaptureMouse();
4924 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4927 wxPoint
pos( event
.GetPosition() );
4928 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4930 wxGridCellCoords coords
;
4931 XYToCell( x
, y
, coords
);
4933 if ( event
.Dragging() )
4935 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4937 // Don't start doing anything until the mouse has been drug at
4938 // least 3 pixels in any direction...
4941 if (m_startDragPos
== wxDefaultPosition
)
4943 m_startDragPos
= pos
;
4946 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4950 m_isDragging
= TRUE
;
4951 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4953 // Hide the edit control, so it
4954 // won't interfer with drag-shrinking.
4955 if ( IsCellEditControlShown() )
4957 HideCellEditControl();
4958 SaveEditControlValue();
4961 // Have we captured the mouse yet?
4964 m_winCapture
= m_gridWin
;
4965 m_winCapture
->CaptureMouse();
4968 if ( coords
!= wxGridNoCellCoords
)
4970 if ( event
.ControlDown() )
4972 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4973 m_selectingKeyboard
= coords
;
4974 HighlightBlock ( m_selectingKeyboard
, coords
);
4978 if ( !IsSelection() )
4980 HighlightBlock( coords
, coords
);
4984 HighlightBlock( m_currentCellCoords
, coords
);
4988 if (! IsVisible(coords
))
4990 MakeCellVisible(coords
);
4991 // TODO: need to introduce a delay or something here. The
4992 // scrolling is way to fast, at least on MSW - also on GTK.
4996 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4998 int cw
, ch
, left
, dummy
;
4999 m_gridWin
->GetClientSize( &cw
, &ch
);
5000 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5002 wxClientDC
dc( m_gridWin
);
5004 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5005 GetRowMinimalHeight(m_dragRowOrCol
) );
5006 dc
.SetLogicalFunction(wxINVERT
);
5007 if ( m_dragLastPos
>= 0 )
5009 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5011 dc
.DrawLine( left
, y
, left
+cw
, y
);
5014 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5016 int cw
, ch
, dummy
, top
;
5017 m_gridWin
->GetClientSize( &cw
, &ch
);
5018 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5020 wxClientDC
dc( m_gridWin
);
5022 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5023 GetColMinimalWidth(m_dragRowOrCol
) );
5024 dc
.SetLogicalFunction(wxINVERT
);
5025 if ( m_dragLastPos
>= 0 )
5027 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5029 dc
.DrawLine( x
, top
, x
, top
+ch
);
5036 m_isDragging
= FALSE
;
5037 m_startDragPos
= wxDefaultPosition
;
5039 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5040 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5043 if ( event
.Entering() || event
.Leaving() )
5045 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5046 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5051 // ------------ Left button pressed
5053 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5055 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5060 if ( !event
.ControlDown() )
5062 if ( event
.ShiftDown() )
5066 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5067 m_currentCellCoords
.GetCol(),
5070 event
.ControlDown(),
5076 else if ( XToEdgeOfCol(x
) < 0 &&
5077 YToEdgeOfRow(y
) < 0 )
5079 DisableCellEditControl();
5080 MakeCellVisible( coords
);
5082 // if this is the second click on this cell then start
5084 if ( m_waitForSlowClick
&&
5085 (coords
== m_currentCellCoords
) &&
5086 CanEnableCellControl())
5088 EnableCellEditControl();
5090 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
5091 wxGridCellEditor
*editor
= attr
->GetEditor(this,
5094 editor
->StartingClick();
5098 m_waitForSlowClick
= FALSE
;
5102 if ( event
.ControlDown() )
5106 m_selection
->ToggleCellSelection( coords
.GetRow(),
5108 event
.ControlDown(),
5113 m_selectingTopLeft
= wxGridNoCellCoords
;
5114 m_selectingBottomRight
= wxGridNoCellCoords
;
5115 m_selectingKeyboard
= coords
;
5119 SetCurrentCell( coords
);
5122 if ( m_selection
->GetSelectionMode() !=
5123 wxGrid::wxGridSelectCells
)
5125 HighlightBlock( coords
, coords
);
5129 m_waitForSlowClick
= TRUE
;
5136 // ------------ Left double click
5138 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5140 DisableCellEditControl();
5142 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5144 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5152 // ------------ Left button released
5154 else if ( event
.LeftUp() )
5156 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5158 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5159 m_selectingBottomRight
!= wxGridNoCellCoords
)
5163 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5164 m_winCapture
= NULL
;
5169 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5170 m_selectingTopLeft
.GetCol(),
5171 m_selectingBottomRight
.GetRow(),
5172 m_selectingBottomRight
.GetCol(),
5173 event
.ControlDown(),
5179 m_selectingTopLeft
= wxGridNoCellCoords
;
5180 m_selectingBottomRight
= wxGridNoCellCoords
;
5183 // Show the edit control, if it has been hidden for
5185 ShowCellEditControl();
5187 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5189 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5190 DoEndDragResizeRow();
5192 // Note: we are ending the event *after* doing
5193 // default processing in this case
5195 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5197 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5199 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5200 DoEndDragResizeCol();
5202 // Note: we are ending the event *after* doing
5203 // default processing in this case
5205 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5212 // ------------ Right button down
5214 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5216 DisableCellEditControl();
5217 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5222 // no default action at the moment
5227 // ------------ Right double click
5229 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5231 DisableCellEditControl();
5232 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5237 // no default action at the moment
5241 // ------------ Moving and no button action
5243 else if ( event
.Moving() && !event
.IsButton() )
5245 int dragRow
= YToEdgeOfRow( y
);
5246 int dragCol
= XToEdgeOfCol( x
);
5248 // Dragging on the corner of a cell to resize in both
5249 // directions is not implemented yet...
5251 if ( dragRow
>= 0 && dragCol
>= 0 )
5253 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5259 m_dragRowOrCol
= dragRow
;
5261 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5263 if ( CanDragRowSize() && CanDragGridSize() )
5264 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5269 m_dragRowOrCol
= dragCol
;
5277 m_dragRowOrCol
= dragCol
;
5279 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5281 if ( CanDragColSize() && CanDragGridSize() )
5282 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5288 // Neither on a row or col edge
5290 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5292 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5298 void wxGrid::DoEndDragResizeRow()
5300 if ( m_dragLastPos
>= 0 )
5302 // erase the last line and resize the row
5304 int cw
, ch
, left
, dummy
;
5305 m_gridWin
->GetClientSize( &cw
, &ch
);
5306 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5308 wxClientDC
dc( m_gridWin
);
5310 dc
.SetLogicalFunction( wxINVERT
);
5311 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5312 HideCellEditControl();
5313 SaveEditControlValue();
5315 int rowTop
= GetRowTop(m_dragRowOrCol
);
5316 SetRowSize( m_dragRowOrCol
,
5317 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5319 if ( !GetBatchCount() )
5321 // Only needed to get the correct rect.y:
5322 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5324 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5325 rect
.width
= m_rowLabelWidth
;
5326 rect
.height
= ch
- rect
.y
;
5327 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5329 m_gridWin
->Refresh( FALSE
, &rect
);
5332 ShowCellEditControl();
5337 void wxGrid::DoEndDragResizeCol()
5339 if ( m_dragLastPos
>= 0 )
5341 // erase the last line and resize the col
5343 int cw
, ch
, dummy
, top
;
5344 m_gridWin
->GetClientSize( &cw
, &ch
);
5345 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5347 wxClientDC
dc( m_gridWin
);
5349 dc
.SetLogicalFunction( wxINVERT
);
5350 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5351 HideCellEditControl();
5352 SaveEditControlValue();
5354 int colLeft
= GetColLeft(m_dragRowOrCol
);
5355 SetColSize( m_dragRowOrCol
,
5356 wxMax( m_dragLastPos
- colLeft
,
5357 GetColMinimalWidth(m_dragRowOrCol
) ) );
5359 if ( !GetBatchCount() )
5361 // Only needed to get the correct rect.x:
5362 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5364 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5365 rect
.width
= cw
- rect
.x
;
5366 rect
.height
= m_colLabelHeight
;
5367 m_colLabelWin
->Refresh( TRUE
, &rect
);
5369 m_gridWin
->Refresh( FALSE
, &rect
);
5372 ShowCellEditControl();
5379 // ------ interaction with data model
5381 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5383 switch ( msg
.GetId() )
5385 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5386 return GetModelValues();
5388 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5389 return SetModelValues();
5391 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5392 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5393 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5394 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5395 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5396 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5397 return Redimension( msg
);
5406 // The behaviour of this function depends on the grid table class
5407 // Clear() function. For the default wxGridStringTable class the
5408 // behavious is to replace all cell contents with wxEmptyString but
5409 // not to change the number of rows or cols.
5411 void wxGrid::ClearGrid()
5415 if (IsCellEditControlEnabled())
5416 DisableCellEditControl();
5419 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5424 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5426 // TODO: something with updateLabels flag
5430 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5436 if (IsCellEditControlEnabled())
5437 DisableCellEditControl();
5439 return m_table
->InsertRows( pos
, numRows
);
5441 // the table will have sent the results of the insert row
5442 // operation to this view object as a grid table message
5448 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5450 // TODO: something with updateLabels flag
5454 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5458 return ( m_table
&& m_table
->AppendRows( numRows
) );
5459 // the table will have sent the results of the append row
5460 // operation to this view object as a grid table message
5464 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5466 // TODO: something with updateLabels flag
5470 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5476 if (IsCellEditControlEnabled())
5477 DisableCellEditControl();
5479 return (m_table
->DeleteRows( pos
, numRows
));
5480 // the table will have sent the results of the delete row
5481 // operation to this view object as a grid table message
5487 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5489 // TODO: something with updateLabels flag
5493 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5499 if (IsCellEditControlEnabled())
5500 DisableCellEditControl();
5502 return m_table
->InsertCols( pos
, numCols
);
5503 // the table will have sent the results of the insert col
5504 // operation to this view object as a grid table message
5510 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5512 // TODO: something with updateLabels flag
5516 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5520 return ( m_table
&& m_table
->AppendCols( numCols
) );
5521 // the table will have sent the results of the append col
5522 // operation to this view object as a grid table message
5526 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5528 // TODO: something with updateLabels flag
5532 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5538 if (IsCellEditControlEnabled())
5539 DisableCellEditControl();
5541 return ( m_table
->DeleteCols( pos
, numCols
) );
5542 // the table will have sent the results of the delete col
5543 // operation to this view object as a grid table message
5551 // ----- event handlers
5554 // Generate a grid event based on a mouse event and
5555 // return the result of ProcessEvent()
5557 int wxGrid::SendEvent( const wxEventType type
,
5559 wxMouseEvent
& mouseEv
)
5564 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5566 int rowOrCol
= (row
== -1 ? col
: row
);
5568 wxGridSizeEvent
gridEvt( GetId(),
5572 mouseEv
.GetX() + GetRowLabelSize(),
5573 mouseEv
.GetY() + GetColLabelSize(),
5574 mouseEv
.ControlDown(),
5575 mouseEv
.ShiftDown(),
5577 mouseEv
.MetaDown() );
5579 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5580 vetoed
= !gridEvt
.IsAllowed();
5582 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5584 // Right now, it should _never_ end up here!
5585 wxGridRangeSelectEvent
gridEvt( GetId(),
5589 m_selectingBottomRight
,
5591 mouseEv
.ControlDown(),
5592 mouseEv
.ShiftDown(),
5594 mouseEv
.MetaDown() );
5596 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5597 vetoed
= !gridEvt
.IsAllowed();
5601 wxGridEvent
gridEvt( GetId(),
5605 mouseEv
.GetX() + GetRowLabelSize(),
5606 mouseEv
.GetY() + GetColLabelSize(),
5608 mouseEv
.ControlDown(),
5609 mouseEv
.ShiftDown(),
5611 mouseEv
.MetaDown() );
5612 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5613 vetoed
= !gridEvt
.IsAllowed();
5616 // A Veto'd event may not be `claimed' so test this first
5617 if (vetoed
) return -1;
5618 return claimed
? 1 : 0;
5622 // Generate a grid event of specified type and return the result
5623 // of ProcessEvent().
5625 int wxGrid::SendEvent( const wxEventType type
,
5631 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5633 int rowOrCol
= (row
== -1 ? col
: row
);
5635 wxGridSizeEvent
gridEvt( GetId(),
5640 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5641 vetoed
= !gridEvt
.IsAllowed();
5645 wxGridEvent
gridEvt( GetId(),
5650 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
5651 vetoed
= !gridEvt
.IsAllowed();
5654 // A Veto'd event may not be `claimed' so test this first
5655 if (vetoed
) return -1;
5656 return claimed
? 1 : 0;
5660 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5662 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5666 void wxGrid::OnSize( wxSizeEvent
& event
)
5668 // position the child windows
5671 // don't call CalcDimensions() from here, the base class handles the size
5677 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5679 if ( m_inOnKeyDown
)
5681 // shouldn't be here - we are going round in circles...
5683 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5686 m_inOnKeyDown
= TRUE
;
5688 // propagate the event up and see if it gets processed
5690 wxWindow
*parent
= GetParent();
5691 wxKeyEvent
keyEvt( event
);
5692 keyEvt
.SetEventObject( parent
);
5694 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5697 // try local handlers
5699 switch ( event
.KeyCode() )
5702 if ( event
.ControlDown() )
5704 MoveCursorUpBlock( event
.ShiftDown() );
5708 MoveCursorUp( event
.ShiftDown() );
5713 if ( event
.ControlDown() )
5715 MoveCursorDownBlock( event
.ShiftDown() );
5719 MoveCursorDown( event
.ShiftDown() );
5724 if ( event
.ControlDown() )
5726 MoveCursorLeftBlock( event
.ShiftDown() );
5730 MoveCursorLeft( event
.ShiftDown() );
5735 if ( event
.ControlDown() )
5737 MoveCursorRightBlock( event
.ShiftDown() );
5741 MoveCursorRight( event
.ShiftDown() );
5746 case WXK_NUMPAD_ENTER
:
5747 if ( event
.ControlDown() )
5749 event
.Skip(); // to let the edit control have the return
5753 if ( GetGridCursorRow() < GetNumberRows()-1 )
5755 MoveCursorDown( event
.ShiftDown() );
5759 // at the bottom of a column
5760 HideCellEditControl();
5761 SaveEditControlValue();
5771 if (event
.ShiftDown())
5773 if ( GetGridCursorCol() > 0 )
5775 MoveCursorLeft( FALSE
);
5780 HideCellEditControl();
5781 SaveEditControlValue();
5786 if ( GetGridCursorCol() < GetNumberCols()-1 )
5788 MoveCursorRight( FALSE
);
5793 HideCellEditControl();
5794 SaveEditControlValue();
5800 if ( event
.ControlDown() )
5802 MakeCellVisible( 0, 0 );
5803 SetCurrentCell( 0, 0 );
5812 if ( event
.ControlDown() )
5814 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5815 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5832 if ( event
.ControlDown() )
5836 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5837 m_currentCellCoords
.GetCol(),
5838 event
.ControlDown(),
5845 if ( !IsEditable() )
5847 MoveCursorRight( FALSE
);
5850 // Otherwise fall through to default
5853 // is it possible to edit the current cell at all?
5854 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5856 // yes, now check whether the cells editor accepts the key
5857 int row
= m_currentCellCoords
.GetRow();
5858 int col
= m_currentCellCoords
.GetCol();
5859 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5860 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5862 // <F2> is special and will always start editing, for
5863 // other keys - ask the editor itself
5864 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5865 || editor
->IsAcceptedKey(event
) )
5867 EnableCellEditControl();
5869 // the editor could be not shown for a variety of
5870 // reasons (i.e. blocked by the app or whatever), so
5871 // check if it really was created
5872 if ( m_cellEditCtrlEnabled
)
5874 editor
->StartingKey(event
);
5887 // let others process char events with modifiers or all
5888 // char events for readonly cells
5895 m_inOnKeyDown
= FALSE
;
5898 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5900 // try local handlers
5902 if ( event
.KeyCode() == WXK_SHIFT
)
5904 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5905 m_selectingBottomRight
!= wxGridNoCellCoords
)
5909 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5910 m_selectingTopLeft
.GetCol(),
5911 m_selectingBottomRight
.GetRow(),
5912 m_selectingBottomRight
.GetCol(),
5913 event
.ControlDown(),
5920 m_selectingTopLeft
= wxGridNoCellCoords
;
5921 m_selectingBottomRight
= wxGridNoCellCoords
;
5922 m_selectingKeyboard
= wxGridNoCellCoords
;
5926 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5930 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5932 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5934 // the event has been intercepted - do nothing
5938 wxClientDC
dc(m_gridWin
);
5941 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5943 HideCellEditControl();
5944 DisableCellEditControl();
5946 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5949 r
= BlockToDeviceRect(m_currentCellCoords
, coords
);
5950 if ( !m_gridLinesEnabled
)
5958 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
5960 // Otherwise refresh redraws the highlight!
5961 m_currentCellCoords
= coords
;
5963 DrawGridCellArea(dc
,cells
);
5964 DrawAllGridLines( dc
, r
);
5968 m_currentCellCoords
= coords
;
5970 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5971 DrawCellHighlight(dc
, attr
);
5976 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5979 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5983 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5986 rightCol
= GetNumberCols() - 1;
5988 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5991 bottomRow
= GetNumberRows() - 1;
5995 if ( topRow
> bottomRow
)
6002 if ( leftCol
> rightCol
)
6009 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6010 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6012 if ( m_selectingTopLeft
!= updateTopLeft
||
6013 m_selectingBottomRight
!= updateBottomRight
)
6015 // Compute two optimal update rectangles:
6016 // Either one rectangle is a real subset of the
6017 // other, or they are (almost) disjoint!
6019 bool need_refresh
[4];
6023 need_refresh
[3] = FALSE
;
6026 // Store intermediate values
6027 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6028 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6029 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6030 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6032 // Determine the outer/inner coordinates.
6033 if (oldLeft
> leftCol
)
6039 if (oldTop
> topRow
)
6045 if (oldRight
< rightCol
)
6048 oldRight
= rightCol
;
6051 if (oldBottom
< bottomRow
)
6054 oldBottom
= bottomRow
;
6058 // Now, either the stuff marked old is the outer
6059 // rectangle or we don't have a situation where one
6060 // is contained in the other.
6062 if ( oldLeft
< leftCol
)
6064 need_refresh
[0] = TRUE
;
6065 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6067 wxGridCellCoords ( oldBottom
,
6071 if ( oldTop
< topRow
)
6073 need_refresh
[1] = TRUE
;
6074 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6076 wxGridCellCoords ( topRow
- 1,
6080 if ( oldRight
> rightCol
)
6082 need_refresh
[2] = TRUE
;
6083 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6085 wxGridCellCoords ( oldBottom
,
6089 if ( oldBottom
> bottomRow
)
6091 need_refresh
[3] = TRUE
;
6092 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6094 wxGridCellCoords ( oldBottom
,
6100 m_selectingTopLeft
= updateTopLeft
;
6101 m_selectingBottomRight
= updateBottomRight
;
6103 // various Refresh() calls
6104 for (i
= 0; i
< 4; i
++ )
6105 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6106 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6109 // never generate an event as it will be generated from
6110 // wxGridSelection::SelectBlock!
6111 // (old comment from when this was the body of SelectBlock)
6115 // ------ functions to get/send data (see also public functions)
6118 bool wxGrid::GetModelValues()
6122 // all we need to do is repaint the grid
6124 m_gridWin
->Refresh();
6132 bool wxGrid::SetModelValues()
6138 for ( row
= 0; row
< m_numRows
; row
++ )
6140 for ( col
= 0; col
< m_numCols
; col
++ )
6142 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6154 // Note - this function only draws cells that are in the list of
6155 // exposed cells (usually set from the update region by
6156 // CalcExposedCells)
6158 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6160 if ( !m_numRows
|| !m_numCols
) return;
6163 size_t numCells
= cells
.GetCount();
6165 for ( i
= 0; i
< numCells
; i
++ )
6167 DrawCell( dc
, cells
[i
] );
6172 void wxGrid::DrawGridSpace( wxDC
& dc
)
6175 m_gridWin
->GetClientSize( &cw
, &ch
);
6178 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6180 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6181 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6183 if ( right
> rightCol
|| bottom
> bottomRow
)
6186 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6188 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6189 dc
.SetPen( *wxTRANSPARENT_PEN
);
6191 if ( right
> rightCol
)
6193 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6196 if ( bottom
> bottomRow
)
6198 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6204 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6206 int row
= coords
.GetRow();
6207 int col
= coords
.GetCol();
6209 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6212 // we draw the cell border ourselves
6213 #if !WXGRID_DRAW_LINES
6214 if ( m_gridLinesEnabled
)
6215 DrawCellBorder( dc
, coords
);
6218 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6220 bool isCurrent
= coords
== m_currentCellCoords
;
6222 wxRect rect
= CellToRect( row
, col
);
6224 // if the editor is shown, we should use it and not the renderer
6225 // Note: However, only if it is really _shown_, i.e. not hidden!
6226 if ( isCurrent
&& IsCellEditControlShown() )
6228 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6229 editor
->PaintBackground(rect
, attr
);
6234 // but all the rest is drawn by the cell renderer and hence may be
6236 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6237 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6244 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6246 int row
= m_currentCellCoords
.GetRow();
6247 int col
= m_currentCellCoords
.GetCol();
6249 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6252 wxRect rect
= CellToRect(row
, col
);
6254 // hmmm... what could we do here to show that the cell is disabled?
6255 // for now, I just draw a thinner border than for the other ones, but
6256 // it doesn't look really good
6258 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6262 // The center of th drawn line is where the position/width/height of
6263 // the rectangle is actually at, (on wxMSW atr least,) so we will
6264 // reduce the size of the rectangle to compensate for the thickness of
6265 // the line. If this is too strange on non wxMSW platforms then
6266 // please #ifdef this appropriately.
6267 rect
.x
+= penWidth
/2;
6268 rect
.y
+= penWidth
/2;
6269 rect
.width
-= penWidth
-1;
6270 rect
.height
-= penWidth
-1;
6273 // Now draw the rectangle
6274 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6275 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6276 dc
.DrawRectangle(rect
);
6280 // VZ: my experiments with 3d borders...
6282 // how to properly set colours for arbitrary bg?
6283 wxCoord x1
= rect
.x
,
6285 x2
= rect
.x
+ rect
.width
-1,
6286 y2
= rect
.y
+ rect
.height
-1;
6288 dc
.SetPen(*wxWHITE_PEN
);
6289 dc
.DrawLine(x1
, y1
, x2
, y1
);
6290 dc
.DrawLine(x1
, y1
, x1
, y2
);
6292 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6293 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6295 dc
.SetPen(*wxBLACK_PEN
);
6296 dc
.DrawLine(x1
, y2
, x2
, y2
);
6297 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6302 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6304 int row
= coords
.GetRow();
6305 int col
= coords
.GetCol();
6306 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6309 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6311 // right hand border
6313 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6314 GetColRight(col
), GetRowBottom(row
) );
6318 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6319 GetColRight(col
), GetRowBottom(row
) );
6322 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6324 // This if block was previously in wxGrid::OnPaint but that doesn't
6325 // seem to get called under wxGTK - MB
6327 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6328 m_numRows
&& m_numCols
)
6330 m_currentCellCoords
.Set(0, 0);
6333 if ( IsCellEditControlShown() )
6335 // don't show highlight when the edit control is shown
6339 // if the active cell was repainted, repaint its highlight too because it
6340 // might have been damaged by the grid lines
6341 size_t count
= cells
.GetCount();
6342 for ( size_t n
= 0; n
< count
; n
++ )
6344 if ( cells
[n
] == m_currentCellCoords
)
6346 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6347 DrawCellHighlight(dc
, attr
);
6355 // TODO: remove this ???
6356 // This is used to redraw all grid lines e.g. when the grid line colour
6359 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6361 if ( !m_gridLinesEnabled
||
6363 !m_numCols
) return;
6365 int top
, bottom
, left
, right
;
6367 #if 0 //#ifndef __WXGTK__
6371 m_gridWin
->GetClientSize(&cw
, &ch
);
6373 // virtual coords of visible area
6375 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6376 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6381 reg
.GetBox(x
, y
, w
, h
);
6382 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6383 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6387 m_gridWin
->GetClientSize(&cw
, &ch
);
6388 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6389 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6392 // avoid drawing grid lines past the last row and col
6394 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6395 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6397 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6399 // horizontal grid lines
6402 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
6404 int bot
= GetRowBottom(i
) - 1;
6413 dc
.DrawLine( left
, bot
, right
, bot
);
6418 // vertical grid lines
6420 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
6422 int colRight
= GetColRight(i
) - 1;
6423 if ( colRight
> right
)
6428 if ( colRight
>= left
)
6430 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6436 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
6438 if ( !m_numRows
) return;
6441 size_t numLabels
= rows
.GetCount();
6443 for ( i
= 0; i
< numLabels
; i
++ )
6445 DrawRowLabel( dc
, rows
[i
] );
6450 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6452 if ( GetRowHeight(row
) <= 0 )
6455 int rowTop
= GetRowTop(row
),
6456 rowBottom
= GetRowBottom(row
) - 1;
6458 dc
.SetPen( *wxBLACK_PEN
);
6459 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6460 m_rowLabelWidth
-1, rowBottom
);
6462 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6464 dc
.SetPen( *wxWHITE_PEN
);
6465 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6466 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6468 dc
.SetBackgroundMode( wxTRANSPARENT
);
6469 dc
.SetTextForeground( GetLabelTextColour() );
6470 dc
.SetFont( GetLabelFont() );
6473 GetRowLabelAlignment( &hAlign
, &vAlign
);
6477 rect
.SetY( GetRowTop(row
) + 2 );
6478 rect
.SetWidth( m_rowLabelWidth
- 4 );
6479 rect
.SetHeight( GetRowHeight(row
) - 4 );
6480 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6484 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
6486 if ( !m_numCols
) return;
6489 size_t numLabels
= cols
.GetCount();
6491 for ( i
= 0; i
< numLabels
; i
++ )
6493 DrawColLabel( dc
, cols
[i
] );
6498 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6500 if ( GetColWidth(col
) <= 0 )
6503 int colLeft
= GetColLeft(col
),
6504 colRight
= GetColRight(col
) - 1;
6506 dc
.SetPen( *wxBLACK_PEN
);
6507 dc
.DrawLine( colRight
, 0,
6508 colRight
, m_colLabelHeight
-1 );
6510 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6511 colRight
, m_colLabelHeight
-1 );
6513 dc
.SetPen( *wxWHITE_PEN
);
6514 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6515 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6517 dc
.SetBackgroundMode( wxTRANSPARENT
);
6518 dc
.SetTextForeground( GetLabelTextColour() );
6519 dc
.SetFont( GetLabelFont() );
6521 dc
.SetBackgroundMode( wxTRANSPARENT
);
6522 dc
.SetTextForeground( GetLabelTextColour() );
6523 dc
.SetFont( GetLabelFont() );
6526 GetColLabelAlignment( &hAlign
, &vAlign
);
6529 rect
.SetX( colLeft
+ 2 );
6531 rect
.SetWidth( GetColWidth(col
) - 4 );
6532 rect
.SetHeight( m_colLabelHeight
- 4 );
6533 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6536 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6537 const wxString
& value
,
6542 wxArrayString lines
;
6544 dc
.SetClippingRegion( rect
);
6545 StringToLines( value
, lines
);
6548 //Forward to new API.
6549 DrawTextRectangle( dc
,
6557 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6558 const wxArrayString
& lines
,
6563 long textWidth
, textHeight
;
6564 long lineWidth
, lineHeight
;
6566 if ( lines
.GetCount() )
6568 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6569 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6572 switch ( horizAlign
)
6575 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6578 case wxALIGN_CENTRE
:
6579 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6588 switch ( vertAlign
)
6590 case wxALIGN_BOTTOM
:
6591 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6594 case wxALIGN_CENTRE
:
6595 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6604 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6606 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6611 dc
.DestroyClippingRegion();
6615 // Split multi line text up into an array of strings. Any existing
6616 // contents of the string array are preserved.
6618 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6622 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6623 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6625 while ( startPos
< (int)tVal
.Length() )
6627 pos
= tVal
.Mid(startPos
).Find( eol
);
6632 else if ( pos
== 0 )
6634 lines
.Add( wxEmptyString
);
6638 lines
.Add( value
.Mid(startPos
, pos
) );
6642 if ( startPos
< (int)value
.Length() )
6644 lines
.Add( value
.Mid( startPos
) );
6649 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6650 const wxArrayString
& lines
,
6651 long *width
, long *height
)
6658 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6660 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6661 w
= wxMax( w
, lineW
);
6670 // ------ Batch processing.
6672 void wxGrid::EndBatch()
6674 if ( m_batchCount
> 0 )
6677 if ( !m_batchCount
)
6680 m_rowLabelWin
->Refresh();
6681 m_colLabelWin
->Refresh();
6682 m_cornerLabelWin
->Refresh();
6683 m_gridWin
->Refresh();
6688 // Use this, rather than wxWindow::Refresh(), to force an immediate
6689 // repainting of the grid. Has no effect if you are already inside a
6690 // BeginBatch / EndBatch block.
6692 void wxGrid::ForceRefresh()
6700 // ------ Edit control functions
6704 void wxGrid::EnableEditing( bool edit
)
6706 // TODO: improve this ?
6708 if ( edit
!= m_editable
)
6710 if(!edit
) EnableCellEditControl(edit
);
6716 void wxGrid::EnableCellEditControl( bool enable
)
6721 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6722 SetCurrentCell( 0, 0 );
6724 if ( enable
!= m_cellEditCtrlEnabled
)
6728 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
6731 // this should be checked by the caller!
6732 wxASSERT_MSG( CanEnableCellControl(),
6733 _T("can't enable editing for this cell!") );
6735 // do it before ShowCellEditControl()
6736 m_cellEditCtrlEnabled
= enable
;
6738 ShowCellEditControl();
6742 //FIXME:add veto support
6743 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
6745 HideCellEditControl();
6746 SaveEditControlValue();
6748 // do it after HideCellEditControl()
6749 m_cellEditCtrlEnabled
= enable
;
6754 bool wxGrid::IsCurrentCellReadOnly() const
6757 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6758 bool readonly
= attr
->IsReadOnly();
6764 bool wxGrid::CanEnableCellControl() const
6766 return m_editable
&& !IsCurrentCellReadOnly();
6769 bool wxGrid::IsCellEditControlEnabled() const
6771 // the cell edit control might be disable for all cells or just for the
6772 // current one if it's read only
6773 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6776 bool wxGrid::IsCellEditControlShown() const
6778 bool isShown
= FALSE
;
6780 if ( m_cellEditCtrlEnabled
)
6782 int row
= m_currentCellCoords
.GetRow();
6783 int col
= m_currentCellCoords
.GetCol();
6784 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6785 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6790 if ( editor
->IsCreated() )
6792 isShown
= editor
->GetControl()->IsShown();
6802 void wxGrid::ShowCellEditControl()
6804 if ( IsCellEditControlEnabled() )
6806 if ( !IsVisible( m_currentCellCoords
) )
6808 m_cellEditCtrlEnabled
= FALSE
;
6813 wxRect rect
= CellToRect( m_currentCellCoords
);
6814 int row
= m_currentCellCoords
.GetRow();
6815 int col
= m_currentCellCoords
.GetCol();
6817 // convert to scrolled coords
6819 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6821 // done in PaintBackground()
6823 // erase the highlight and the cell contents because the editor
6824 // might not cover the entire cell
6825 wxClientDC
dc( m_gridWin
);
6827 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6828 dc
.SetPen(*wxTRANSPARENT_PEN
);
6829 dc
.DrawRectangle(rect
);
6832 // cell is shifted by one pixel
6833 // However, don't allow x or y to become negative
6834 // since the SetSize() method interprets that as
6841 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6842 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6843 if ( !editor
->IsCreated() )
6845 editor
->Create(m_gridWin
, -1,
6846 new wxGridCellEditorEvtHandler(this, editor
));
6848 wxGridEditorCreatedEvent
evt(GetId(),
6849 wxEVT_GRID_EDITOR_CREATED
,
6853 editor
->GetControl());
6854 GetEventHandler()->ProcessEvent(evt
);
6857 editor
->Show( TRUE
, attr
);
6859 editor
->SetSize( rect
);
6861 editor
->BeginEdit(row
, col
, this);
6870 void wxGrid::HideCellEditControl()
6872 if ( IsCellEditControlEnabled() )
6874 int row
= m_currentCellCoords
.GetRow();
6875 int col
= m_currentCellCoords
.GetCol();
6877 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6878 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6879 editor
->Show( FALSE
);
6882 m_gridWin
->SetFocus();
6883 wxRect
rect( CellToRect( row
, col
) );
6884 m_gridWin
->Refresh( FALSE
, &rect
);
6889 void wxGrid::SaveEditControlValue()
6891 if ( IsCellEditControlEnabled() )
6893 int row
= m_currentCellCoords
.GetRow();
6894 int col
= m_currentCellCoords
.GetCol();
6896 wxString oldval
= GetCellValue(row
,col
);
6898 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6899 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6900 bool changed
= editor
->EndEdit(row
, col
, this);
6907 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
6908 m_currentCellCoords
.GetRow(),
6909 m_currentCellCoords
.GetCol() ) < 0 ) {
6911 // Event has been vetoed, set the data back.
6912 SetCellValue(row
,col
,oldval
);
6920 // ------ Grid location functions
6921 // Note that all of these functions work with the logical coordinates of
6922 // grid cells and labels so you will need to convert from device
6923 // coordinates for mouse events etc.
6926 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6928 int row
= YToRow(y
);
6929 int col
= XToCol(x
);
6931 if ( row
== -1 || col
== -1 )
6933 coords
= wxGridNoCellCoords
;
6937 coords
.Set( row
, col
);
6942 // Internal Helper function for computing row or column from some
6943 // (unscrolled) coordinate value, using either
6944 // m_defaultRowHeight/m_defaultColWidth or binary search on array
6945 // of m_rowBottoms/m_ColRights to speed up the search!
6947 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
6948 const wxArrayInt
& BorderArray
, bool maxOnOverflow
)
6952 size_t i_max
= coord
/ defaultDist
,
6954 if (BorderArray
.IsEmpty())
6959 if ( i_max
>= BorderArray
.GetCount())
6960 i_max
= BorderArray
.GetCount() - 1;
6963 if ( coord
>= BorderArray
[i_max
])
6966 i_max
= coord
/ minDist
;
6968 if ( i_max
>= BorderArray
.GetCount())
6969 i_max
= BorderArray
.GetCount() - 1;
6971 if ( coord
>= BorderArray
[i_max
])
6972 return maxOnOverflow
? (int)i_max
: -1;
6973 if ( coord
< BorderArray
[0] )
6976 while ( i_max
- i_min
> 0 )
6978 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
6979 0, _T("wxGrid: internal error in CoordToRowOrCol"));
6980 if (coord
>= BorderArray
[ i_max
- 1])
6984 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
6985 if (coord
< BorderArray
[median
])
6993 int wxGrid::YToRow( int y
)
6995 return CoordToRowOrCol(y
, m_defaultRowHeight
,
6996 WXGRID_MIN_ROW_HEIGHT
, m_rowBottoms
, FALSE
);
7000 int wxGrid::XToCol( int x
)
7002 return CoordToRowOrCol(x
, m_defaultColWidth
,
7003 WXGRID_MIN_COL_WIDTH
, m_colRights
, FALSE
);
7007 // return the row number that that the y coord is near the edge of, or
7008 // -1 if not near an edge
7010 int wxGrid::YToEdgeOfRow( int y
)
7013 i
= internalYToRow(y
);
7015 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7017 // We know that we are in row i, test whether we are
7018 // close enough to lower or upper border, respectively.
7019 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7021 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7029 // return the col number that that the x coord is near the edge of, or
7030 // -1 if not near an edge
7032 int wxGrid::XToEdgeOfCol( int x
)
7035 i
= internalXToCol(x
);
7037 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7039 // We know that we are in column i, test whether we are
7040 // close enough to right or left border, respectively.
7041 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7043 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7051 wxRect
wxGrid::CellToRect( int row
, int col
)
7053 wxRect
rect( -1, -1, -1, -1 );
7055 if ( row
>= 0 && row
< m_numRows
&&
7056 col
>= 0 && col
< m_numCols
)
7058 rect
.x
= GetColLeft(col
);
7059 rect
.y
= GetRowTop(row
);
7060 rect
.width
= GetColWidth(col
);
7061 rect
.height
= GetRowHeight(row
);
7064 // if grid lines are enabled, then the area of the cell is a bit smaller
7065 if (m_gridLinesEnabled
) {
7073 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7075 // get the cell rectangle in logical coords
7077 wxRect
r( CellToRect( row
, col
) );
7079 // convert to device coords
7081 int left
, top
, right
, bottom
;
7082 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7083 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7085 // check against the client area of the grid window
7088 m_gridWin
->GetClientSize( &cw
, &ch
);
7090 if ( wholeCellVisible
)
7092 // is the cell wholly visible ?
7094 return ( left
>= 0 && right
<= cw
&&
7095 top
>= 0 && bottom
<= ch
);
7099 // is the cell partly visible ?
7101 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7102 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7107 // make the specified cell location visible by doing a minimal amount
7110 void wxGrid::MakeCellVisible( int row
, int col
)
7114 int xpos
= -1, ypos
= -1;
7116 if ( row
>= 0 && row
< m_numRows
&&
7117 col
>= 0 && col
< m_numCols
)
7119 // get the cell rectangle in logical coords
7121 wxRect
r( CellToRect( row
, col
) );
7123 // convert to device coords
7125 int left
, top
, right
, bottom
;
7126 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7127 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7130 m_gridWin
->GetClientSize( &cw
, &ch
);
7136 else if ( bottom
> ch
)
7138 int h
= r
.GetHeight();
7140 for ( i
= row
-1; i
>= 0; i
-- )
7142 int rowHeight
= GetRowHeight(i
);
7143 if ( h
+ rowHeight
> ch
)
7150 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7151 // have rounding errors (this is important, because if we do, we
7152 // might not scroll at all and some cells won't be redrawn)
7154 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7156 ypos
+= GRID_SCROLL_LINE_Y
;
7163 else if ( right
> cw
)
7165 int w
= r
.GetWidth();
7167 for ( i
= col
-1; i
>= 0; i
-- )
7169 int colWidth
= GetColWidth(i
);
7170 if ( w
+ colWidth
> cw
)
7177 // see comment for ypos above
7178 xpos
+= GRID_SCROLL_LINE_X
;
7181 if ( xpos
!= -1 || ypos
!= -1 )
7184 xpos
/= GRID_SCROLL_LINE_X
;
7186 ypos
/= GRID_SCROLL_LINE_Y
;
7187 Scroll( xpos
, ypos
);
7195 // ------ Grid cursor movement functions
7198 bool wxGrid::MoveCursorUp( bool expandSelection
)
7200 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7201 m_currentCellCoords
.GetRow() >= 0 )
7203 if ( expandSelection
)
7205 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7206 m_selectingKeyboard
= m_currentCellCoords
;
7207 if ( m_selectingKeyboard
.GetRow() > 0 )
7209 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7210 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7211 m_selectingKeyboard
.GetCol() );
7212 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7215 else if ( m_currentCellCoords
.GetRow() > 0 )
7218 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7219 m_currentCellCoords
.GetCol() );
7220 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7221 m_currentCellCoords
.GetCol() );
7232 bool wxGrid::MoveCursorDown( bool expandSelection
)
7234 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7235 m_currentCellCoords
.GetRow() < m_numRows
)
7237 if ( expandSelection
)
7239 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7240 m_selectingKeyboard
= m_currentCellCoords
;
7241 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
7243 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
7244 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7245 m_selectingKeyboard
.GetCol() );
7246 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7249 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
7252 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7253 m_currentCellCoords
.GetCol() );
7254 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7255 m_currentCellCoords
.GetCol() );
7266 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7268 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7269 m_currentCellCoords
.GetCol() >= 0 )
7271 if ( expandSelection
)
7273 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7274 m_selectingKeyboard
= m_currentCellCoords
;
7275 if ( m_selectingKeyboard
.GetCol() > 0 )
7277 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7278 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7279 m_selectingKeyboard
.GetCol() );
7280 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7283 else if ( m_currentCellCoords
.GetCol() > 0 )
7286 MakeCellVisible( m_currentCellCoords
.GetRow(),
7287 m_currentCellCoords
.GetCol() - 1 );
7288 SetCurrentCell( m_currentCellCoords
.GetRow(),
7289 m_currentCellCoords
.GetCol() - 1 );
7300 bool wxGrid::MoveCursorRight( bool expandSelection
)
7302 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7303 m_currentCellCoords
.GetCol() < m_numCols
)
7305 if ( expandSelection
)
7307 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7308 m_selectingKeyboard
= m_currentCellCoords
;
7309 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7311 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7312 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7313 m_selectingKeyboard
.GetCol() );
7314 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7317 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7320 MakeCellVisible( m_currentCellCoords
.GetRow(),
7321 m_currentCellCoords
.GetCol() + 1 );
7322 SetCurrentCell( m_currentCellCoords
.GetRow(),
7323 m_currentCellCoords
.GetCol() + 1 );
7334 bool wxGrid::MovePageUp()
7336 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7338 int row
= m_currentCellCoords
.GetRow();
7342 m_gridWin
->GetClientSize( &cw
, &ch
);
7344 int y
= GetRowTop(row
);
7345 int newRow
= YToRow( y
- ch
+ 1 );
7350 else if ( newRow
== row
)
7355 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7356 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7364 bool wxGrid::MovePageDown()
7366 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7368 int row
= m_currentCellCoords
.GetRow();
7369 if ( row
< m_numRows
)
7372 m_gridWin
->GetClientSize( &cw
, &ch
);
7374 int y
= GetRowTop(row
);
7375 int newRow
= YToRow( y
+ ch
);
7378 newRow
= m_numRows
- 1;
7380 else if ( newRow
== row
)
7385 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7386 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7394 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7397 m_currentCellCoords
!= wxGridNoCellCoords
&&
7398 m_currentCellCoords
.GetRow() > 0 )
7400 int row
= m_currentCellCoords
.GetRow();
7401 int col
= m_currentCellCoords
.GetCol();
7403 if ( m_table
->IsEmptyCell(row
, col
) )
7405 // starting in an empty cell: find the next block of
7411 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7414 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7416 // starting at the top of a block: find the next block
7422 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7427 // starting within a block: find the top of the block
7432 if ( m_table
->IsEmptyCell(row
, col
) )
7440 MakeCellVisible( row
, col
);
7441 if ( expandSelection
)
7443 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7444 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7449 SetCurrentCell( row
, col
);
7457 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7460 m_currentCellCoords
!= wxGridNoCellCoords
&&
7461 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7463 int row
= m_currentCellCoords
.GetRow();
7464 int col
= m_currentCellCoords
.GetCol();
7466 if ( m_table
->IsEmptyCell(row
, col
) )
7468 // starting in an empty cell: find the next block of
7471 while ( row
< m_numRows
-1 )
7474 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7477 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7479 // starting at the bottom of a block: find the next block
7482 while ( row
< m_numRows
-1 )
7485 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7490 // starting within a block: find the bottom of the block
7492 while ( row
< m_numRows
-1 )
7495 if ( m_table
->IsEmptyCell(row
, col
) )
7503 MakeCellVisible( row
, col
);
7504 if ( expandSelection
)
7506 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7507 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7512 SetCurrentCell( row
, col
);
7521 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7524 m_currentCellCoords
!= wxGridNoCellCoords
&&
7525 m_currentCellCoords
.GetCol() > 0 )
7527 int row
= m_currentCellCoords
.GetRow();
7528 int col
= m_currentCellCoords
.GetCol();
7530 if ( m_table
->IsEmptyCell(row
, col
) )
7532 // starting in an empty cell: find the next block of
7538 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7541 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7543 // starting at the left of a block: find the next block
7549 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7554 // starting within a block: find the left of the block
7559 if ( m_table
->IsEmptyCell(row
, col
) )
7567 MakeCellVisible( row
, col
);
7568 if ( expandSelection
)
7570 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7571 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7576 SetCurrentCell( row
, col
);
7585 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7588 m_currentCellCoords
!= wxGridNoCellCoords
&&
7589 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7591 int row
= m_currentCellCoords
.GetRow();
7592 int col
= m_currentCellCoords
.GetCol();
7594 if ( m_table
->IsEmptyCell(row
, col
) )
7596 // starting in an empty cell: find the next block of
7599 while ( col
< m_numCols
-1 )
7602 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7605 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7607 // starting at the right of a block: find the next block
7610 while ( col
< m_numCols
-1 )
7613 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7618 // starting within a block: find the right of the block
7620 while ( col
< m_numCols
-1 )
7623 if ( m_table
->IsEmptyCell(row
, col
) )
7631 MakeCellVisible( row
, col
);
7632 if ( expandSelection
)
7634 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7635 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7640 SetCurrentCell( row
, col
);
7652 // ------ Label values and formatting
7655 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7657 *horiz
= m_rowLabelHorizAlign
;
7658 *vert
= m_rowLabelVertAlign
;
7661 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7663 *horiz
= m_colLabelHorizAlign
;
7664 *vert
= m_colLabelVertAlign
;
7667 wxString
wxGrid::GetRowLabelValue( int row
)
7671 return m_table
->GetRowLabelValue( row
);
7681 wxString
wxGrid::GetColLabelValue( int col
)
7685 return m_table
->GetColLabelValue( col
);
7696 void wxGrid::SetRowLabelSize( int width
)
7698 width
= wxMax( width
, 0 );
7699 if ( width
!= m_rowLabelWidth
)
7703 m_rowLabelWin
->Show( FALSE
);
7704 m_cornerLabelWin
->Show( FALSE
);
7706 else if ( m_rowLabelWidth
== 0 )
7708 m_rowLabelWin
->Show( TRUE
);
7709 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7712 m_rowLabelWidth
= width
;
7719 void wxGrid::SetColLabelSize( int height
)
7721 height
= wxMax( height
, 0 );
7722 if ( height
!= m_colLabelHeight
)
7726 m_colLabelWin
->Show( FALSE
);
7727 m_cornerLabelWin
->Show( FALSE
);
7729 else if ( m_colLabelHeight
== 0 )
7731 m_colLabelWin
->Show( TRUE
);
7732 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7735 m_colLabelHeight
= height
;
7742 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7744 if ( m_labelBackgroundColour
!= colour
)
7746 m_labelBackgroundColour
= colour
;
7747 m_rowLabelWin
->SetBackgroundColour( colour
);
7748 m_colLabelWin
->SetBackgroundColour( colour
);
7749 m_cornerLabelWin
->SetBackgroundColour( colour
);
7751 if ( !GetBatchCount() )
7753 m_rowLabelWin
->Refresh();
7754 m_colLabelWin
->Refresh();
7755 m_cornerLabelWin
->Refresh();
7760 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7762 if ( m_labelTextColour
!= colour
)
7764 m_labelTextColour
= colour
;
7765 if ( !GetBatchCount() )
7767 m_rowLabelWin
->Refresh();
7768 m_colLabelWin
->Refresh();
7773 void wxGrid::SetLabelFont( const wxFont
& font
)
7776 if ( !GetBatchCount() )
7778 m_rowLabelWin
->Refresh();
7779 m_colLabelWin
->Refresh();
7783 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7785 // allow old (incorrect) defs to be used
7788 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7789 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7790 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7795 case wxTOP
: vert
= wxALIGN_TOP
; break;
7796 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7797 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7800 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7802 m_rowLabelHorizAlign
= horiz
;
7805 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7807 m_rowLabelVertAlign
= vert
;
7810 if ( !GetBatchCount() )
7812 m_rowLabelWin
->Refresh();
7816 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7818 // allow old (incorrect) defs to be used
7821 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7822 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7823 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7828 case wxTOP
: vert
= wxALIGN_TOP
; break;
7829 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7830 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7833 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7835 m_colLabelHorizAlign
= horiz
;
7838 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7840 m_colLabelVertAlign
= vert
;
7843 if ( !GetBatchCount() )
7845 m_colLabelWin
->Refresh();
7849 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7853 m_table
->SetRowLabelValue( row
, s
);
7854 if ( !GetBatchCount() )
7856 wxRect rect
= CellToRect( row
, 0);
7857 if ( rect
.height
> 0 )
7859 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7861 rect
.width
= m_rowLabelWidth
;
7862 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7868 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7872 m_table
->SetColLabelValue( col
, s
);
7873 if ( !GetBatchCount() )
7875 wxRect rect
= CellToRect( 0, col
);
7876 if ( rect
.width
> 0 )
7878 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7880 rect
.height
= m_colLabelHeight
;
7881 m_colLabelWin
->Refresh( TRUE
, &rect
);
7887 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7889 if ( m_gridLineColour
!= colour
)
7891 m_gridLineColour
= colour
;
7893 wxClientDC
dc( m_gridWin
);
7895 DrawAllGridLines( dc
, wxRegion() );
7900 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7902 if ( m_cellHighlightColour
!= colour
)
7904 m_cellHighlightColour
= colour
;
7906 wxClientDC
dc( m_gridWin
);
7908 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7909 DrawCellHighlight(dc
, attr
);
7914 void wxGrid::SetCellHighlightPenWidth(int width
)
7916 if (m_cellHighlightPenWidth
!= width
) {
7917 m_cellHighlightPenWidth
= width
;
7919 // Just redrawing the cell highlight is not enough since that won't
7920 // make any visible change if the the thickness is getting smaller.
7921 int row
= m_currentCellCoords
.GetRow();
7922 int col
= m_currentCellCoords
.GetCol();
7923 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7925 wxRect rect
= CellToRect(row
, col
);
7926 m_gridWin
->Refresh(TRUE
, &rect
);
7930 void wxGrid::SetCellHighlightROPenWidth(int width
)
7932 if (m_cellHighlightROPenWidth
!= width
) {
7933 m_cellHighlightROPenWidth
= width
;
7935 // Just redrawing the cell highlight is not enough since that won't
7936 // make any visible change if the the thickness is getting smaller.
7937 int row
= m_currentCellCoords
.GetRow();
7938 int col
= m_currentCellCoords
.GetCol();
7939 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7941 wxRect rect
= CellToRect(row
, col
);
7942 m_gridWin
->Refresh(TRUE
, &rect
);
7946 void wxGrid::EnableGridLines( bool enable
)
7948 if ( enable
!= m_gridLinesEnabled
)
7950 m_gridLinesEnabled
= enable
;
7952 if ( !GetBatchCount() )
7956 wxClientDC
dc( m_gridWin
);
7958 DrawAllGridLines( dc
, wxRegion() );
7962 m_gridWin
->Refresh();
7969 int wxGrid::GetDefaultRowSize()
7971 return m_defaultRowHeight
;
7974 int wxGrid::GetRowSize( int row
)
7976 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7978 return GetRowHeight(row
);
7981 int wxGrid::GetDefaultColSize()
7983 return m_defaultColWidth
;
7986 int wxGrid::GetColSize( int col
)
7988 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7990 return GetColWidth(col
);
7993 // ============================================================================
7994 // access to the grid attributes: each of them has a default value in the grid
7995 // itself and may be overidden on a per-cell basis
7996 // ============================================================================
7998 // ----------------------------------------------------------------------------
7999 // setting default attributes
8000 // ----------------------------------------------------------------------------
8002 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8004 m_defaultCellAttr
->SetBackgroundColour(col
);
8006 m_gridWin
->SetBackgroundColour(col
);
8010 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8012 m_defaultCellAttr
->SetTextColour(col
);
8015 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8017 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8020 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8022 m_defaultCellAttr
->SetFont(font
);
8025 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8027 m_defaultCellAttr
->SetRenderer(renderer
);
8030 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8032 m_defaultCellAttr
->SetEditor(editor
);
8035 // ----------------------------------------------------------------------------
8036 // access to the default attrbiutes
8037 // ----------------------------------------------------------------------------
8039 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8041 return m_defaultCellAttr
->GetBackgroundColour();
8044 wxColour
wxGrid::GetDefaultCellTextColour()
8046 return m_defaultCellAttr
->GetTextColour();
8049 wxFont
wxGrid::GetDefaultCellFont()
8051 return m_defaultCellAttr
->GetFont();
8054 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8056 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8059 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8061 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8064 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8066 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8069 // ----------------------------------------------------------------------------
8070 // access to cell attributes
8071 // ----------------------------------------------------------------------------
8073 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8075 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8076 wxColour colour
= attr
->GetBackgroundColour();
8081 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8083 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8084 wxColour colour
= attr
->GetTextColour();
8089 wxFont
wxGrid::GetCellFont( int row
, int col
)
8091 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8092 wxFont font
= attr
->GetFont();
8097 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8099 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8100 attr
->GetAlignment(horiz
, vert
);
8104 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8106 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8107 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8113 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8115 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8116 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8122 bool wxGrid::IsReadOnly(int row
, int col
) const
8124 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8125 bool isReadOnly
= attr
->IsReadOnly();
8130 // ----------------------------------------------------------------------------
8131 // attribute support: cache, automatic provider creation, ...
8132 // ----------------------------------------------------------------------------
8134 bool wxGrid::CanHaveAttributes()
8141 return m_table
->CanHaveAttributes();
8144 void wxGrid::ClearAttrCache()
8146 if ( m_attrCache
.row
!= -1 )
8148 wxSafeDecRef(m_attrCache
.attr
);
8149 m_attrCache
.attr
= NULL
;
8150 m_attrCache
.row
= -1;
8154 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8156 wxGrid
*self
= (wxGrid
*)this; // const_cast
8158 self
->ClearAttrCache();
8159 self
->m_attrCache
.row
= row
;
8160 self
->m_attrCache
.col
= col
;
8161 self
->m_attrCache
.attr
= attr
;
8165 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8167 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8169 *attr
= m_attrCache
.attr
;
8170 wxSafeIncRef(m_attrCache
.attr
);
8172 #ifdef DEBUG_ATTR_CACHE
8173 gs_nAttrCacheHits
++;
8180 #ifdef DEBUG_ATTR_CACHE
8181 gs_nAttrCacheMisses
++;
8187 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
8189 wxGridCellAttr
*attr
;
8190 if ( !LookupAttr(row
, col
, &attr
) )
8192 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
8193 CacheAttr(row
, col
, attr
);
8197 attr
->SetDefAttr(m_defaultCellAttr
);
8201 attr
= m_defaultCellAttr
;
8208 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
8210 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8212 wxCHECK_MSG( m_table
, attr
,
8213 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
8215 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
8218 attr
= new wxGridCellAttr(m_defaultCellAttr
);
8220 // artificially inc the ref count to match DecRef() in caller
8222 m_table
->SetAttr(attr
, row
, col
);
8228 // ----------------------------------------------------------------------------
8229 // setting column attributes (wrappers around SetColAttr)
8230 // ----------------------------------------------------------------------------
8232 void wxGrid::SetColFormatBool(int col
)
8234 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
8237 void wxGrid::SetColFormatNumber(int col
)
8239 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
8242 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
8244 wxString typeName
= wxGRID_VALUE_FLOAT
;
8245 if ( (width
!= -1) || (precision
!= -1) )
8247 typeName
<< _T(':') << width
<< _T(',') << precision
;
8250 SetColFormatCustom(col
, typeName
);
8253 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8255 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8257 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8259 attr
= new wxGridCellAttr
;
8260 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8261 attr
->SetRenderer(renderer
);
8263 SetColAttr(col
, attr
);
8267 // ----------------------------------------------------------------------------
8268 // setting cell attributes: this is forwarded to the table
8269 // ----------------------------------------------------------------------------
8271 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8273 if ( CanHaveAttributes() )
8275 m_table
->SetRowAttr(attr
, row
);
8284 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8286 if ( CanHaveAttributes() )
8288 m_table
->SetColAttr(attr
, col
);
8297 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8299 if ( CanHaveAttributes() )
8301 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8302 attr
->SetBackgroundColour(colour
);
8307 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8309 if ( CanHaveAttributes() )
8311 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8312 attr
->SetTextColour(colour
);
8317 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8319 if ( CanHaveAttributes() )
8321 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8322 attr
->SetFont(font
);
8327 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8329 if ( CanHaveAttributes() )
8331 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8332 attr
->SetAlignment(horiz
, vert
);
8337 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8339 if ( CanHaveAttributes() )
8341 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8342 attr
->SetRenderer(renderer
);
8347 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8349 if ( CanHaveAttributes() )
8351 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8352 attr
->SetEditor(editor
);
8357 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8359 if ( CanHaveAttributes() )
8361 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8362 attr
->SetReadOnly(isReadOnly
);
8367 // ----------------------------------------------------------------------------
8368 // Data type registration
8369 // ----------------------------------------------------------------------------
8371 void wxGrid::RegisterDataType(const wxString
& typeName
,
8372 wxGridCellRenderer
* renderer
,
8373 wxGridCellEditor
* editor
)
8375 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8379 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8381 wxString typeName
= m_table
->GetTypeName(row
, col
);
8382 return GetDefaultEditorForType(typeName
);
8385 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8387 wxString typeName
= m_table
->GetTypeName(row
, col
);
8388 return GetDefaultRendererForType(typeName
);
8392 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8394 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8395 if ( index
== wxNOT_FOUND
)
8397 wxFAIL_MSG(wxT("Unknown data type name"));
8402 return m_typeRegistry
->GetEditor(index
);
8406 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8408 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8409 if ( index
== wxNOT_FOUND
)
8411 wxFAIL_MSG(wxT("Unknown data type name"));
8416 return m_typeRegistry
->GetRenderer(index
);
8420 // ----------------------------------------------------------------------------
8422 // ----------------------------------------------------------------------------
8424 void wxGrid::EnableDragRowSize( bool enable
)
8426 m_canDragRowSize
= enable
;
8430 void wxGrid::EnableDragColSize( bool enable
)
8432 m_canDragColSize
= enable
;
8435 void wxGrid::EnableDragGridSize( bool enable
)
8437 m_canDragGridSize
= enable
;
8441 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8443 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8445 if ( resizeExistingRows
)
8447 // since we are resizing all rows to the default row size,
8448 // we can simply clear the row heights and row bottoms
8449 // arrays (which also allows us to take advantage of
8450 // some speed optimisations)
8451 m_rowHeights
.Empty();
8452 m_rowBottoms
.Empty();
8453 if ( !GetBatchCount() )
8458 void wxGrid::SetRowSize( int row
, int height
)
8460 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8462 if ( m_rowHeights
.IsEmpty() )
8464 // need to really create the array
8468 int h
= wxMax( 0, height
);
8469 int diff
= h
- m_rowHeights
[row
];
8471 m_rowHeights
[row
] = h
;
8473 for ( i
= row
; i
< m_numRows
; i
++ )
8475 m_rowBottoms
[i
] += diff
;
8477 if ( !GetBatchCount() )
8481 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8483 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8485 if ( resizeExistingCols
)
8487 // since we are resizing all columns to the default column size,
8488 // we can simply clear the col widths and col rights
8489 // arrays (which also allows us to take advantage of
8490 // some speed optimisations)
8491 m_colWidths
.Empty();
8492 m_colRights
.Empty();
8493 if ( !GetBatchCount() )
8498 void wxGrid::SetColSize( int col
, int width
)
8500 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8502 // should we check that it's bigger than GetColMinimalWidth(col) here?
8504 if ( m_colWidths
.IsEmpty() )
8506 // need to really create the array
8510 int w
= wxMax( 0, width
);
8511 int diff
= w
- m_colWidths
[col
];
8512 m_colWidths
[col
] = w
;
8515 for ( i
= col
; i
< m_numCols
; i
++ )
8517 m_colRights
[i
] += diff
;
8519 if ( !GetBatchCount() )
8524 void wxGrid::SetColMinimalWidth( int col
, int width
)
8526 m_colMinWidths
.Put(col
, width
);
8529 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8531 m_rowMinHeights
.Put(row
, width
);
8534 int wxGrid::GetColMinimalWidth(int col
) const
8536 long value
= m_colMinWidths
.Get(col
);
8537 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8540 int wxGrid::GetRowMinimalHeight(int row
) const
8542 long value
= m_rowMinHeights
.Get(row
);
8543 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8546 // ----------------------------------------------------------------------------
8548 // ----------------------------------------------------------------------------
8550 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8552 wxClientDC
dc(m_gridWin
);
8554 // init both of them to avoid compiler warnings, even if weo nly need one
8562 wxCoord extent
, extentMax
= 0;
8563 int max
= column
? m_numRows
: m_numCols
;
8564 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8571 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8572 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8575 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8576 extent
= column
? size
.x
: size
.y
;
8577 if ( extent
> extentMax
)
8588 // now also compare with the column label extent
8590 dc
.SetFont( GetLabelFont() );
8593 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8595 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8597 extent
= column
? w
: h
;
8598 if ( extent
> extentMax
)
8605 // empty column - give default extent (notice that if extentMax is less
8606 // than default extent but != 0, it's ok)
8607 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8613 // leave some space around text
8624 SetColSize(col
, extentMax
);
8625 if ( !GetBatchCount() )
8628 m_gridWin
->GetClientSize( &cw
, &ch
);
8629 wxRect
rect ( CellToRect( 0, col
) );
8631 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8632 rect
.width
= cw
- rect
.x
;
8633 rect
.height
= m_colLabelHeight
;
8634 m_colLabelWin
->Refresh( TRUE
, &rect
);
8639 SetRowSize(row
, extentMax
);
8640 if ( !GetBatchCount() )
8643 m_gridWin
->GetClientSize( &cw
, &ch
);
8644 wxRect
rect ( CellToRect( row
, 0 ) );
8646 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8647 rect
.width
= m_rowLabelWidth
;
8648 rect
.height
= ch
- rect
.y
;
8649 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8655 SetColMinimalWidth(col
, extentMax
);
8657 SetRowMinimalHeight(row
, extentMax
);
8661 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8663 int width
= m_rowLabelWidth
;
8668 for ( int col
= 0; col
< m_numCols
; col
++ )
8672 AutoSizeColumn(col
, setAsMin
);
8675 width
+= GetColWidth(col
);
8684 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8686 int height
= m_colLabelHeight
;
8691 for ( int row
= 0; row
< m_numRows
; row
++ )
8695 AutoSizeRow(row
, setAsMin
);
8698 height
+= GetRowHeight(row
);
8707 void wxGrid::AutoSize()
8711 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8713 // round up the size to a multiple of scroll step - this ensures that we
8714 // won't get the scrollbars if we're sized exactly to this width
8715 wxSize
sizeFit(GetScrollX(size
.x
) * GRID_SCROLL_LINE_X
,
8716 GetScrollY(size
.y
) * GRID_SCROLL_LINE_Y
);
8718 // distribute the extra space between teh columns/rows to avoid having
8719 // extra white space
8720 wxCoord diff
= sizeFit
.x
- size
.x
;
8723 // try to resize the columns uniformly
8724 wxCoord diffPerCol
= diff
/ m_numCols
;
8727 for ( int col
= 0; col
< m_numCols
; col
++ )
8729 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
8733 // add remaining amount to the last columns
8734 diff
-= diffPerCol
* m_numCols
;
8737 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
8739 SetColSize(col
, GetColWidth(col
) + 1);
8745 diff
= sizeFit
.y
- size
.y
;
8748 // try to resize the columns uniformly
8749 wxCoord diffPerRow
= diff
/ m_numRows
;
8752 for ( int row
= 0; row
< m_numRows
; row
++ )
8754 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
8758 // add remaining amount to the last rows
8759 diff
-= diffPerRow
* m_numRows
;
8762 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
8764 SetRowSize(row
, GetRowHeight(row
) + 1);
8771 SetClientSize(sizeFit
);
8774 wxSize
wxGrid::DoGetBestSize() const
8776 // don't set sizes, only calculate them
8777 wxGrid
*self
= (wxGrid
*)this; // const_cast
8779 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8780 self
->SetOrCalcRowSizes(TRUE
));
8789 wxPen
& wxGrid::GetDividerPen() const
8794 // ----------------------------------------------------------------------------
8795 // cell value accessor functions
8796 // ----------------------------------------------------------------------------
8798 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8802 m_table
->SetValue( row
, col
, s
);
8803 if ( !GetBatchCount() )
8805 wxClientDC
dc( m_gridWin
);
8807 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8810 if ( m_currentCellCoords
.GetRow() == row
&&
8811 m_currentCellCoords
.GetCol() == col
&&
8812 IsCellEditControlShown())
8813 // Note: If we are using IsCellEditControlEnabled,
8814 // this interacts badly with calling SetCellValue from
8815 // an EVT_GRID_CELL_CHANGE handler.
8817 HideCellEditControl();
8818 ShowCellEditControl(); // will reread data from table
8825 // ------ Block, row and col selection
8828 void wxGrid::SelectRow( int row
, bool addToSelected
)
8830 if ( IsSelection() && !addToSelected
)
8834 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8838 void wxGrid::SelectCol( int col
, bool addToSelected
)
8840 if ( IsSelection() && !addToSelected
)
8844 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8848 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8849 bool addToSelected
)
8851 if ( IsSelection() && !addToSelected
)
8855 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8856 FALSE
, addToSelected
);
8860 void wxGrid::SelectAll()
8862 if ( m_numRows
> 0 && m_numCols
> 0 )
8865 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8870 // ------ Cell, row and col deselection
8873 void wxGrid::DeselectRow( int row
)
8878 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8880 if ( m_selection
->IsInSelection(row
, 0 ) )
8881 m_selection
->ToggleCellSelection( row
, 0);
8885 int nCols
= GetNumberCols();
8886 for ( int i
= 0; i
< nCols
; i
++ )
8888 if ( m_selection
->IsInSelection(row
, i
) )
8889 m_selection
->ToggleCellSelection( row
, i
);
8894 void wxGrid::DeselectCol( int col
)
8899 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8901 if ( m_selection
->IsInSelection(0, col
) )
8902 m_selection
->ToggleCellSelection( 0, col
);
8906 int nRows
= GetNumberRows();
8907 for ( int i
= 0; i
< nRows
; i
++ )
8909 if ( m_selection
->IsInSelection(i
, col
) )
8910 m_selection
->ToggleCellSelection(i
, col
);
8915 void wxGrid::DeselectCell( int row
, int col
)
8917 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
8918 m_selection
->ToggleCellSelection(row
, col
);
8921 bool wxGrid::IsSelection()
8923 return ( m_selection
&& (m_selection
->IsSelection() ||
8924 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8925 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
8928 bool wxGrid::IsInSelection( int row
, int col
)
8930 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
8931 ( row
>= m_selectingTopLeft
.GetRow() &&
8932 col
>= m_selectingTopLeft
.GetCol() &&
8933 row
<= m_selectingBottomRight
.GetRow() &&
8934 col
<= m_selectingBottomRight
.GetCol() )) );
8937 void wxGrid::ClearSelection()
8939 m_selectingTopLeft
= wxGridNoCellCoords
;
8940 m_selectingBottomRight
= wxGridNoCellCoords
;
8942 m_selection
->ClearSelection();
8946 // This function returns the rectangle that encloses the given block
8947 // in device coords clipped to the client size of the grid window.
8949 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8950 const wxGridCellCoords
&bottomRight
)
8952 wxRect
rect( wxGridNoCellRect
);
8955 cellRect
= CellToRect( topLeft
);
8956 if ( cellRect
!= wxGridNoCellRect
)
8962 rect
= wxRect( 0, 0, 0, 0 );
8965 cellRect
= CellToRect( bottomRight
);
8966 if ( cellRect
!= wxGridNoCellRect
)
8972 return wxGridNoCellRect
;
8975 // convert to scrolled coords
8977 int left
, top
, right
, bottom
;
8978 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8979 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8982 m_gridWin
->GetClientSize( &cw
, &ch
);
8984 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8985 return wxRect( 0, 0, 0, 0);
8987 rect
.SetLeft( wxMax(0, left
) );
8988 rect
.SetTop( wxMax(0, top
) );
8989 rect
.SetRight( wxMin(cw
, right
) );
8990 rect
.SetBottom( wxMin(ch
, bottom
) );
8998 // ------ Grid event classes
9001 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
9003 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
9004 int row
, int col
, int x
, int y
, bool sel
,
9005 bool control
, bool shift
, bool alt
, bool meta
)
9006 : wxNotifyEvent( type
, id
)
9013 m_control
= control
;
9018 SetEventObject(obj
);
9022 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
9024 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
9025 int rowOrCol
, int x
, int y
,
9026 bool control
, bool shift
, bool alt
, bool meta
)
9027 : wxNotifyEvent( type
, id
)
9029 m_rowOrCol
= rowOrCol
;
9032 m_control
= control
;
9037 SetEventObject(obj
);
9041 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
9043 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
9044 const wxGridCellCoords
& topLeft
,
9045 const wxGridCellCoords
& bottomRight
,
9046 bool sel
, bool control
,
9047 bool shift
, bool alt
, bool meta
)
9048 : wxNotifyEvent( type
, id
)
9050 m_topLeft
= topLeft
;
9051 m_bottomRight
= bottomRight
;
9053 m_control
= control
;
9058 SetEventObject(obj
);
9062 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
9064 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
9065 wxObject
* obj
, int row
,
9066 int col
, wxControl
* ctrl
)
9067 : wxCommandEvent(type
, id
)
9069 SetEventObject(obj
);
9076 #endif // !wxUSE_NEW_GRID/wxUSE_NEW_GRID
9078 #endif // wxUSE_GRID