1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
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"
33 #if !defined(wxUSE_NEW_GRID) || !(wxUSE_NEW_GRID)
39 #include "wx/dcclient.h"
40 #include "wx/settings.h"
42 #include "wx/textctrl.h"
43 #include "wx/checkbox.h"
44 #include "wx/combobox.h"
45 #include "wx/valtext.h"
48 #include "wx/textfile.h"
49 #include "wx/spinctrl.h"
50 #include "wx/tokenzr.h"
53 #include "wx/generic/gridsel.h"
55 #if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
58 #define WXUNUSED_MOTIF(identifier) identifier
61 #if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64 #define WXUNUSED_GTK(identifier) identifier
67 // Required for wxIs... functions
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WX_DEFINE_EXPORTED_ARRAY(wxGridCellAttr
*, wxArrayAttrs
);
76 struct wxGridCellWithAttr
78 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
79 : coords(row
, col
), attr(attr_
)
88 wxGridCellCoords coords
;
92 WX_DECLARE_EXPORTED_OBJARRAY(wxGridCellWithAttr
, wxGridCellWithAttrArray
);
94 #include "wx/arrimpl.cpp"
96 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
97 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 const int wxEVT_GRID_CELL_LEFT_CLICK
= wxNewEventType();
104 const int wxEVT_GRID_CELL_RIGHT_CLICK
= wxNewEventType();
105 const int wxEVT_GRID_CELL_LEFT_DCLICK
= wxNewEventType();
106 const int wxEVT_GRID_CELL_RIGHT_DCLICK
= wxNewEventType();
107 const int wxEVT_GRID_LABEL_LEFT_CLICK
= wxNewEventType();
108 const int wxEVT_GRID_LABEL_RIGHT_CLICK
= wxNewEventType();
109 const int wxEVT_GRID_LABEL_LEFT_DCLICK
= wxNewEventType();
110 const int wxEVT_GRID_LABEL_RIGHT_DCLICK
= wxNewEventType();
111 const int wxEVT_GRID_ROW_SIZE
= wxNewEventType();
112 const int wxEVT_GRID_COL_SIZE
= wxNewEventType();
113 const int wxEVT_GRID_RANGE_SELECT
= wxNewEventType();
114 const int wxEVT_GRID_CELL_CHANGE
= wxNewEventType();
115 const int wxEVT_GRID_SELECT_CELL
= wxNewEventType();
116 const int wxEVT_GRID_EDITOR_SHOWN
= wxNewEventType();
117 const int wxEVT_GRID_EDITOR_HIDDEN
= wxNewEventType();
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 class WXDLLEXPORT wxGridRowLabelWindow
: public wxWindow
126 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
127 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
128 const wxPoint
&pos
, const wxSize
&size
);
133 void OnPaint( wxPaintEvent
& event
);
134 void OnMouseEvent( wxMouseEvent
& event
);
135 void OnKeyDown( wxKeyEvent
& event
);
136 void OnKeyUp( wxKeyEvent
& );
138 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
139 DECLARE_EVENT_TABLE()
143 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
146 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
147 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
);
153 void OnPaint( wxPaintEvent
&event
);
154 void OnMouseEvent( wxMouseEvent
& event
);
155 void OnKeyDown( wxKeyEvent
& event
);
156 void OnKeyUp( wxKeyEvent
& );
158 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
159 DECLARE_EVENT_TABLE()
163 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
166 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
167 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
168 const wxPoint
&pos
, const wxSize
&size
);
173 void OnMouseEvent( wxMouseEvent
& event
);
174 void OnKeyDown( wxKeyEvent
& event
);
175 void OnKeyUp( wxKeyEvent
& );
176 void OnPaint( wxPaintEvent
& event
);
178 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
179 DECLARE_EVENT_TABLE()
182 class WXDLLEXPORT wxGridWindow
: public wxPanel
187 m_owner
= (wxGrid
*)NULL
;
188 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
189 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
192 wxGridWindow( wxGrid
*parent
,
193 wxGridRowLabelWindow
*rowLblWin
,
194 wxGridColLabelWindow
*colLblWin
,
195 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
198 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
202 wxGridRowLabelWindow
*m_rowLabelWin
;
203 wxGridColLabelWindow
*m_colLabelWin
;
205 void OnPaint( wxPaintEvent
&event
);
206 void OnMouseEvent( wxMouseEvent
& event
);
207 void OnKeyDown( wxKeyEvent
& );
208 void OnKeyUp( wxKeyEvent
& );
209 void OnEraseBackground( wxEraseEvent
& );
212 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
213 DECLARE_EVENT_TABLE()
218 class wxGridCellEditorEvtHandler
: public wxEvtHandler
221 wxGridCellEditorEvtHandler()
222 : m_grid(0), m_editor(0)
224 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
225 : m_grid(grid
), m_editor(editor
)
228 void OnKeyDown(wxKeyEvent
& event
);
229 void OnChar(wxKeyEvent
& event
);
233 wxGridCellEditor
* m_editor
;
234 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
235 DECLARE_EVENT_TABLE()
239 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
240 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
241 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
242 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
247 // ----------------------------------------------------------------------------
248 // the internal data representation used by wxGridCellAttrProvider
249 // ----------------------------------------------------------------------------
251 // this class stores attributes set for cells
252 class WXDLLEXPORT wxGridCellAttrData
255 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
256 wxGridCellAttr
*GetAttr(int row
, int col
) const;
257 void UpdateAttrRows( size_t pos
, int numRows
);
258 void UpdateAttrCols( size_t pos
, int numCols
);
261 // searches for the attr for given cell, returns wxNOT_FOUND if not found
262 int FindIndex(int row
, int col
) const;
264 wxGridCellWithAttrArray m_attrs
;
267 // this class stores attributes set for rows or columns
268 class WXDLLEXPORT wxGridRowOrColAttrData
271 // empty ctor to suppress warnings
272 wxGridRowOrColAttrData() { }
273 ~wxGridRowOrColAttrData();
275 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
276 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
277 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
280 wxArrayInt m_rowsOrCols
;
281 wxArrayAttrs m_attrs
;
284 // NB: this is just a wrapper around 3 objects: one which stores cell
285 // attributes, and 2 others for row/col ones
286 class WXDLLEXPORT wxGridCellAttrProviderData
289 wxGridCellAttrData m_cellAttrs
;
290 wxGridRowOrColAttrData m_rowAttrs
,
295 // ----------------------------------------------------------------------------
296 // data structures used for the data type registry
297 // ----------------------------------------------------------------------------
299 struct wxGridDataTypeInfo
301 wxGridDataTypeInfo(const wxString
& typeName
,
302 wxGridCellRenderer
* renderer
,
303 wxGridCellEditor
* editor
)
304 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
307 ~wxGridDataTypeInfo()
309 wxSafeDecRef(m_renderer
);
310 wxSafeDecRef(m_editor
);
314 wxGridCellRenderer
* m_renderer
;
315 wxGridCellEditor
* m_editor
;
319 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
322 class WXDLLEXPORT wxGridTypeRegistry
325 wxGridTypeRegistry() {}
326 ~wxGridTypeRegistry();
328 void RegisterDataType(const wxString
& typeName
,
329 wxGridCellRenderer
* renderer
,
330 wxGridCellEditor
* editor
);
332 // find one of already registered data types
333 int FindRegisteredDataType(const wxString
& typeName
);
335 // try to FindRegisteredDataType(), if this fails and typeName is one of
336 // standard typenames, register it and return its index
337 int FindDataType(const wxString
& typeName
);
339 // try to FindDataType(), if it fails see if it is not one of already
340 // registered data types with some params in which case clone the
341 // registered data type and set params for it
342 int FindOrCloneDataType(const wxString
& typeName
);
344 wxGridCellRenderer
* GetRenderer(int index
);
345 wxGridCellEditor
* GetEditor(int index
);
348 wxGridDataTypeInfoArray m_typeinfo
;
351 // ----------------------------------------------------------------------------
352 // conditional compilation
353 // ----------------------------------------------------------------------------
355 #ifndef WXGRID_DRAW_LINES
356 #define WXGRID_DRAW_LINES 1
359 // ----------------------------------------------------------------------------
361 // ----------------------------------------------------------------------------
363 //#define DEBUG_ATTR_CACHE
364 #ifdef DEBUG_ATTR_CACHE
365 static size_t gs_nAttrCacheHits
= 0;
366 static size_t gs_nAttrCacheMisses
= 0;
367 #endif // DEBUG_ATTR_CACHE
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
374 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
377 // TODO: fixed so far - make configurable later (and also different for x/y)
378 static const size_t GRID_SCROLL_LINE
= 10;
380 // the size of hash tables used a bit everywhere (the max number of elements
381 // in these hash tables is the number of rows/columns)
382 static const int GRID_HASH_SIZE
= 100;
384 // ============================================================================
386 // ============================================================================
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 wxGridCellEditor::wxGridCellEditor()
398 wxGridCellEditor::~wxGridCellEditor()
403 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
404 wxWindowID
WXUNUSED(id
),
405 wxEvtHandler
* evtHandler
)
408 m_control
->PushEventHandler(evtHandler
);
411 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
412 wxGridCellAttr
*attr
)
414 // erase the background because we might not fill the cell
415 wxClientDC
dc(m_control
->GetParent());
416 dc
.SetPen(*wxTRANSPARENT_PEN
);
417 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
418 dc
.DrawRectangle(rectCell
);
420 // redraw the control we just painted over
421 m_control
->Refresh();
424 void wxGridCellEditor::Destroy()
428 m_control
->PopEventHandler(TRUE
/* delete it*/);
430 m_control
->Destroy();
435 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
437 wxASSERT_MSG(m_control
,
438 wxT("The wxGridCellEditor must be Created first!"));
439 m_control
->Show(show
);
443 // set the colours/fonts if we have any
446 m_colFgOld
= m_control
->GetForegroundColour();
447 m_control
->SetForegroundColour(attr
->GetTextColour());
449 m_colBgOld
= m_control
->GetBackgroundColour();
450 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
452 m_fontOld
= m_control
->GetFont();
453 m_control
->SetFont(attr
->GetFont());
455 // can't do anything more in the base class version, the other
456 // attributes may only be used by the derived classes
461 // restore the standard colours fonts
462 if ( m_colFgOld
.Ok() )
464 m_control
->SetForegroundColour(m_colFgOld
);
465 m_colFgOld
= wxNullColour
;
468 if ( m_colBgOld
.Ok() )
470 m_control
->SetBackgroundColour(m_colBgOld
);
471 m_colBgOld
= wxNullColour
;
474 if ( m_fontOld
.Ok() )
476 m_control
->SetFont(m_fontOld
);
477 m_fontOld
= wxNullFont
;
482 void wxGridCellEditor::SetSize(const wxRect
& rect
)
484 wxASSERT_MSG(m_control
,
485 wxT("The wxGridCellEditor must be Created first!"));
486 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
489 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
494 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
496 // accept the simple key presses, not anything with Ctrl/Alt/Meta
497 return !(event
.ControlDown() || event
.AltDown());
500 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
505 void wxGridCellEditor::StartingClick()
509 // ----------------------------------------------------------------------------
510 // wxGridCellTextEditor
511 // ----------------------------------------------------------------------------
513 wxGridCellTextEditor::wxGridCellTextEditor()
518 void wxGridCellTextEditor::Create(wxWindow
* parent
,
520 wxEvtHandler
* evtHandler
)
522 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
523 wxDefaultPosition
, wxDefaultSize
524 #if defined(__WXMSW__)
525 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
526 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
530 // TODO: use m_maxChars
532 wxGridCellEditor::Create(parent
, id
, evtHandler
);
535 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
536 wxGridCellAttr
* WXUNUSED(attr
))
538 // as we fill the entire client area, don't do anything here to minimize
542 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
544 wxRect
rect(rectOrig
);
546 // Make the edit control large enough to allow for internal
549 // TODO: remove this if the text ctrl sizing is improved esp. for
552 #if defined(__WXGTK__)
561 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
563 // MB: treat MSW separately here otherwise the caret doesn't show
564 // when the editor is in the first row.
565 #if defined(__WXMSW__)
568 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
571 #if defined(__WXMOTIF__)
575 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
576 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
577 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
578 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
581 wxGridCellEditor::SetSize(rect
);
584 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
586 wxASSERT_MSG(m_control
,
587 wxT("The wxGridCellEditor must be Created first!"));
589 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
591 DoBeginEdit(m_startValue
);
594 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
596 Text()->SetValue(startValue
);
597 Text()->SetInsertionPointEnd();
601 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
604 wxASSERT_MSG(m_control
,
605 wxT("The wxGridCellEditor must be Created first!"));
607 bool changed
= FALSE
;
608 wxString value
= Text()->GetValue();
609 if (value
!= m_startValue
)
613 grid
->GetTable()->SetValue(row
, col
, value
);
615 m_startValue
= wxEmptyString
;
616 Text()->SetValue(m_startValue
);
622 void wxGridCellTextEditor::Reset()
624 wxASSERT_MSG(m_control
,
625 wxT("The wxGridCellEditor must be Created first!"));
627 DoReset(m_startValue
);
630 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
632 Text()->SetValue(startValue
);
633 Text()->SetInsertionPointEnd();
636 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
638 if ( wxGridCellEditor::IsAcceptedKey(event
) )
640 int keycode
= event
.GetKeyCode();
654 case WXK_NUMPAD_MULTIPLY
:
658 case WXK_NUMPAD_SUBTRACT
:
660 case WXK_NUMPAD_DECIMAL
:
662 case WXK_NUMPAD_DIVIDE
:
666 // accept 8 bit chars too if isprint() agrees
667 if ( (keycode
< 255) && (isprint(keycode
)) )
675 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
677 // we don't check for !HasModifiers() because IsAcceptedKey() did it
679 // insert the key in the control
681 int keycode
= event
.GetKeyCode();
694 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
698 case WXK_NUMPAD_MULTIPLY
:
708 case WXK_NUMPAD_SUBTRACT
:
713 case WXK_NUMPAD_DECIMAL
:
718 case WXK_NUMPAD_DIVIDE
:
723 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
725 // FIXME this is not going to work for non letters...
726 if ( !event
.ShiftDown() )
728 keycode
= tolower(keycode
);
731 ch
= (wxChar
)keycode
;
741 Text()->AppendText(ch
);
749 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
750 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
752 #if defined(__WXMOTIF__) || defined(__WXGTK__)
753 // wxMotif needs a little extra help...
754 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
755 wxString
s( Text()->GetValue() );
756 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
758 Text()->SetInsertionPoint( pos
);
760 // the other ports can handle a Return key press
766 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
776 if ( !params
.ToLong(&tmp
) )
778 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
782 m_maxChars
= (size_t)tmp
;
787 // ----------------------------------------------------------------------------
788 // wxGridCellNumberEditor
789 // ----------------------------------------------------------------------------
791 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
797 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
799 wxEvtHandler
* evtHandler
)
803 // create a spin ctrl
804 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
805 wxDefaultPosition
, wxDefaultSize
,
809 wxGridCellEditor::Create(parent
, id
, evtHandler
);
813 // just a text control
814 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
817 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
818 #endif // wxUSE_VALIDATORS
822 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
824 // first get the value
825 wxGridTableBase
*table
= grid
->GetTable();
826 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
828 m_valueOld
= table
->GetValueAsLong(row
, col
);
832 wxString sValue
= table
->GetValue(row
, col
);
833 if (! sValue
.ToLong(&m_valueOld
))
835 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
842 Spin()->SetValue((int)m_valueOld
);
847 DoBeginEdit(GetString());
851 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
859 value
= Spin()->GetValue();
860 changed
= value
!= m_valueOld
;
864 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
869 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
870 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
872 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
878 void wxGridCellNumberEditor::Reset()
882 Spin()->SetValue((int)m_valueOld
);
886 DoReset(GetString());
890 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
892 if ( wxGridCellEditor::IsAcceptedKey(event
) )
894 int keycode
= event
.GetKeyCode();
910 case WXK_NUMPAD_SUBTRACT
:
916 if ( (keycode
< 128) && isdigit(keycode
) )
924 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
928 int keycode
= (int) event
.KeyCode();
929 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
931 wxGridCellTextEditor::StartingKey(event
);
941 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
952 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
956 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
960 // skip the error message below
965 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
969 // ----------------------------------------------------------------------------
970 // wxGridCellFloatEditor
971 // ----------------------------------------------------------------------------
973 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
976 m_precision
= precision
;
979 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
981 wxEvtHandler
* evtHandler
)
983 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
986 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
987 #endif // wxUSE_VALIDATORS
990 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
992 // first get the value
993 wxGridTableBase
*table
= grid
->GetTable();
994 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
996 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1000 wxString sValue
= table
->GetValue(row
, col
);
1001 if (! sValue
.ToDouble(&m_valueOld
))
1003 wxFAIL_MSG( _T("this cell doesn't have float value") );
1008 DoBeginEdit(GetString());
1011 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1015 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1017 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1018 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1020 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1030 void wxGridCellFloatEditor::Reset()
1032 DoReset(GetString());
1035 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1037 int keycode
= (int)event
.KeyCode();
1038 if ( isdigit(keycode
) ||
1039 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1041 wxGridCellTextEditor::StartingKey(event
);
1043 // skip Skip() below
1050 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1061 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1065 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1067 m_precision
= (int)tmp
;
1069 // skip the error message below
1074 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1078 wxString
wxGridCellFloatEditor::GetString() const
1081 if ( m_width
== -1 )
1083 // default width/precision
1086 else if ( m_precision
== -1 )
1088 // default precision
1089 fmt
.Printf(_T("%%%d.g"), m_width
);
1093 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1096 return wxString::Format(fmt
, m_valueOld
);
1099 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1101 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1103 int keycode
= event
.GetKeyCode();
1117 case WXK_NUMPAD_ADD
:
1119 case WXK_NUMPAD_SUBTRACT
:
1121 case WXK_NUMPAD_DECIMAL
:
1125 // additionally accept 'e' as in '1e+6'
1126 if ( (keycode
< 128) &&
1127 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1135 // ----------------------------------------------------------------------------
1136 // wxGridCellBoolEditor
1137 // ----------------------------------------------------------------------------
1139 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1141 wxEvtHandler
* evtHandler
)
1143 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1144 wxDefaultPosition
, wxDefaultSize
,
1147 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1150 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1152 bool resize
= FALSE
;
1153 wxSize size
= m_control
->GetSize();
1154 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1156 // check if the checkbox is not too big/small for this cell
1157 wxSize sizeBest
= m_control
->GetBestSize();
1158 if ( !(size
== sizeBest
) )
1160 // reset to default size if it had been made smaller
1166 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1168 // leave 1 pixel margin
1169 size
.x
= size
.y
= minSize
- 2;
1176 m_control
->SetSize(size
);
1179 // position it in the centre of the rectangle (TODO: support alignment?)
1181 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1182 // the checkbox without label still has some space to the right in wxGTK,
1183 // so shift it to the right
1185 #elif defined(__WXMSW__)
1186 // here too, but in other way
1191 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1194 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1196 m_control
->Show(show
);
1200 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1201 CBox()->SetBackgroundColour(colBg
);
1205 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1207 wxASSERT_MSG(m_control
,
1208 wxT("The wxGridCellEditor must be Created first!"));
1210 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1211 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1214 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1215 m_startValue
= !( !cellval
|| (cellval
== "0") );
1217 CBox()->SetValue(m_startValue
);
1221 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1224 wxASSERT_MSG(m_control
,
1225 wxT("The wxGridCellEditor must be Created first!"));
1227 bool changed
= FALSE
;
1228 bool value
= CBox()->GetValue();
1229 if ( value
!= m_startValue
)
1234 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1235 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1237 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1243 void wxGridCellBoolEditor::Reset()
1245 wxASSERT_MSG(m_control
,
1246 wxT("The wxGridCellEditor must be Created first!"));
1248 CBox()->SetValue(m_startValue
);
1251 void wxGridCellBoolEditor::StartingClick()
1253 CBox()->SetValue(!CBox()->GetValue());
1256 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1258 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1260 int keycode
= event
.GetKeyCode();
1264 case WXK_NUMPAD_MULTIPLY
:
1266 case WXK_NUMPAD_ADD
:
1268 case WXK_NUMPAD_SUBTRACT
:
1279 // ----------------------------------------------------------------------------
1280 // wxGridCellChoiceEditor
1281 // ----------------------------------------------------------------------------
1283 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1284 const wxString choices
[],
1286 : m_allowOthers(allowOthers
)
1290 m_choices
.Alloc(count
);
1291 for ( size_t n
= 0; n
< count
; n
++ )
1293 m_choices
.Add(choices
[n
]);
1298 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1300 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1301 editor
->m_allowOthers
= m_allowOthers
;
1302 editor
->m_choices
= m_choices
;
1307 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1309 wxEvtHandler
* evtHandler
)
1311 size_t count
= m_choices
.GetCount();
1312 wxString
*choices
= new wxString
[count
];
1313 for ( size_t n
= 0; n
< count
; n
++ )
1315 choices
[n
] = m_choices
[n
];
1318 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1319 wxDefaultPosition
, wxDefaultSize
,
1321 m_allowOthers
? 0 : wxCB_READONLY
);
1325 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1328 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1329 wxGridCellAttr
* attr
)
1331 // as we fill the entire client area, don't do anything here to minimize
1334 // TODO: It doesn't actually fill the client area since the height of a
1335 // combo always defaults to the standard... Until someone has time to
1336 // figure out the right rectangle to paint, just do it the normal way...
1337 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1340 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1342 wxASSERT_MSG(m_control
,
1343 wxT("The wxGridCellEditor must be Created first!"));
1345 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1347 Combo()->SetValue(m_startValue
);
1348 size_t count
= m_choices
.GetCount();
1349 for (size_t i
=0; i
<count
; i
++)
1351 if (m_startValue
== m_choices
[i
])
1353 Combo()->SetSelection(i
);
1357 Combo()->SetInsertionPointEnd();
1358 Combo()->SetFocus();
1361 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1364 wxString value
= Combo()->GetValue();
1365 bool changed
= value
!= m_startValue
;
1368 grid
->GetTable()->SetValue(row
, col
, value
);
1370 m_startValue
= wxEmptyString
;
1371 Combo()->SetValue(m_startValue
);
1376 void wxGridCellChoiceEditor::Reset()
1378 Combo()->SetValue(m_startValue
);
1379 Combo()->SetInsertionPointEnd();
1382 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1392 wxStringTokenizer
tk(params
, _T(','));
1393 while ( tk
.HasMoreTokens() )
1395 m_choices
.Add(tk
.GetNextToken());
1399 // ----------------------------------------------------------------------------
1400 // wxGridCellEditorEvtHandler
1401 // ----------------------------------------------------------------------------
1403 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1405 switch ( event
.KeyCode() )
1409 m_grid
->DisableCellEditControl();
1413 event
.Skip( m_grid
->ProcessEvent( event
) );
1416 case WXK_NUMPAD_ENTER
:
1418 if (!m_grid
->ProcessEvent(event
))
1419 m_editor
->HandleReturn(event
);
1428 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1430 switch ( event
.KeyCode() )
1435 case WXK_NUMPAD_ENTER
:
1443 // ----------------------------------------------------------------------------
1444 // wxGridCellWorker is an (almost) empty common base class for
1445 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1446 // ----------------------------------------------------------------------------
1448 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1453 wxGridCellWorker::~wxGridCellWorker()
1457 // ============================================================================
1459 // ============================================================================
1461 // ----------------------------------------------------------------------------
1462 // wxGridCellRenderer
1463 // ----------------------------------------------------------------------------
1465 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1466 wxGridCellAttr
& attr
,
1469 int WXUNUSED(row
), int WXUNUSED(col
),
1472 dc
.SetBackgroundMode( wxSOLID
);
1476 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1480 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1483 dc
.SetPen( *wxTRANSPARENT_PEN
);
1484 dc
.DrawRectangle(rect
);
1487 // ----------------------------------------------------------------------------
1488 // wxGridCellStringRenderer
1489 // ----------------------------------------------------------------------------
1491 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1492 wxGridCellAttr
& attr
,
1496 dc
.SetBackgroundMode( wxTRANSPARENT
);
1498 // TODO some special colours for attr.IsReadOnly() case?
1502 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1503 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1507 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1508 dc
.SetTextForeground( attr
.GetTextColour() );
1511 dc
.SetFont( attr
.GetFont() );
1514 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1516 const wxString
& text
)
1518 wxCoord x
= 0, y
= 0, max_x
= 0;
1519 dc
.SetFont(attr
.GetFont());
1520 wxStringTokenizer
tk(text
, _T('\n'));
1521 while ( tk
.HasMoreTokens() )
1523 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1524 max_x
= wxMax(max_x
, x
);
1527 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1529 return wxSize(max_x
, y
);
1532 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1533 wxGridCellAttr
& attr
,
1537 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1540 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1541 wxGridCellAttr
& attr
,
1543 const wxRect
& rectCell
,
1547 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1549 // now we only have to draw the text
1550 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1553 attr
.GetAlignment(&hAlign
, &vAlign
);
1555 wxRect rect
= rectCell
;
1558 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1559 rect
, hAlign
, vAlign
);
1562 // ----------------------------------------------------------------------------
1563 // wxGridCellNumberRenderer
1564 // ----------------------------------------------------------------------------
1566 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1568 wxGridTableBase
*table
= grid
.GetTable();
1570 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1572 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1576 text
= table
->GetValue(row
, col
);
1582 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1583 wxGridCellAttr
& attr
,
1585 const wxRect
& rectCell
,
1589 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1591 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1593 // draw the text right aligned by default
1595 attr
.GetAlignment(&hAlign
, &vAlign
);
1596 hAlign
= wxALIGN_RIGHT
;
1598 wxRect rect
= rectCell
;
1601 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1604 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1605 wxGridCellAttr
& attr
,
1609 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1612 // ----------------------------------------------------------------------------
1613 // wxGridCellFloatRenderer
1614 // ----------------------------------------------------------------------------
1616 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1619 SetPrecision(precision
);
1622 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1624 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1625 renderer
->m_width
= m_width
;
1626 renderer
->m_precision
= m_precision
;
1627 renderer
->m_format
= m_format
;
1632 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1634 wxGridTableBase
*table
= grid
.GetTable();
1639 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1641 val
= table
->GetValueAsDouble(row
, col
);
1646 text
= table
->GetValue(row
, col
);
1647 hasDouble
= text
.ToDouble(&val
);
1654 if ( m_width
== -1 )
1656 // default width/precision
1657 m_format
= _T("%f");
1659 else if ( m_precision
== -1 )
1661 // default precision
1662 m_format
.Printf(_T("%%%d.f"), m_width
);
1666 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1670 text
.Printf(m_format
, val
);
1672 //else: text already contains the string
1677 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1678 wxGridCellAttr
& attr
,
1680 const wxRect
& rectCell
,
1684 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1686 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1688 // draw the text right aligned by default
1690 attr
.GetAlignment(&hAlign
, &vAlign
);
1691 hAlign
= wxALIGN_RIGHT
;
1693 wxRect rect
= rectCell
;
1696 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1699 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1700 wxGridCellAttr
& attr
,
1704 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1707 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1713 // reset to defaults
1719 wxString tmp
= params
.BeforeFirst(_T(','));
1723 if ( !tmp
.ToLong(&width
) )
1729 SetWidth((int)width
);
1731 tmp
= params
.AfterFirst(_T(','));
1735 if ( !tmp
.ToLong(&precision
) )
1741 SetPrecision((int)precision
);
1749 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1754 // ----------------------------------------------------------------------------
1755 // wxGridCellBoolRenderer
1756 // ----------------------------------------------------------------------------
1758 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1760 // FIXME these checkbox size calculations are really ugly...
1762 // between checkmark and box
1763 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1765 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1766 wxGridCellAttr
& WXUNUSED(attr
),
1771 // compute it only once (no locks for MT safeness in GUI thread...)
1772 if ( !ms_sizeCheckMark
.x
)
1774 // get checkbox size
1775 wxCoord checkSize
= 0;
1776 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1777 wxSize size
= checkbox
->GetBestSize();
1778 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1780 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1781 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1782 checkSize
-= size
.y
/ 2;
1787 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1790 return ms_sizeCheckMark
;
1793 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1794 wxGridCellAttr
& attr
,
1800 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1802 // draw a check mark in the centre (ignoring alignment - TODO)
1803 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1805 // don't draw outside the cell
1806 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1807 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1809 // and even leave (at least) 1 pixel margin
1810 size
.x
= size
.y
= minSize
- 2;
1813 // draw a border around checkmark
1815 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1816 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1817 rectBorder
.width
= size
.x
;
1818 rectBorder
.height
= size
.y
;
1821 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1822 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1825 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1826 value
= !( !cellval
|| (cellval
== "0") );
1831 wxRect rectMark
= rectBorder
;
1833 // MSW DrawCheckMark() is weird (and should probably be changed...)
1834 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1838 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1841 dc
.SetTextForeground(attr
.GetTextColour());
1842 dc
.DrawCheckMark(rectMark
);
1845 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1846 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1847 dc
.DrawRectangle(rectBorder
);
1850 // ----------------------------------------------------------------------------
1852 // ----------------------------------------------------------------------------
1854 wxGridCellAttr
*wxGridCellAttr::Clone() const
1856 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1857 if ( HasTextColour() )
1858 attr
->SetTextColour(GetTextColour());
1859 if ( HasBackgroundColour() )
1860 attr
->SetBackgroundColour(GetBackgroundColour());
1862 attr
->SetFont(GetFont());
1863 if ( HasAlignment() )
1864 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1868 attr
->SetRenderer(m_renderer
);
1869 m_renderer
->IncRef();
1873 attr
->SetEditor(m_editor
);
1878 attr
->SetReadOnly();
1880 attr
->SetDefAttr(m_defGridAttr
);
1885 const wxColour
& wxGridCellAttr::GetTextColour() const
1887 if (HasTextColour())
1891 else if (m_defGridAttr
!= this)
1893 return m_defGridAttr
->GetTextColour();
1897 wxFAIL_MSG(wxT("Missing default cell attribute"));
1898 return wxNullColour
;
1903 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1905 if (HasBackgroundColour())
1907 else if (m_defGridAttr
!= this)
1908 return m_defGridAttr
->GetBackgroundColour();
1911 wxFAIL_MSG(wxT("Missing default cell attribute"));
1912 return wxNullColour
;
1917 const wxFont
& wxGridCellAttr::GetFont() const
1921 else if (m_defGridAttr
!= this)
1922 return m_defGridAttr
->GetFont();
1925 wxFAIL_MSG(wxT("Missing default cell attribute"));
1931 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1935 if ( hAlign
) *hAlign
= m_hAlign
;
1936 if ( vAlign
) *vAlign
= m_vAlign
;
1938 else if (m_defGridAttr
!= this)
1939 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1942 wxFAIL_MSG(wxT("Missing default cell attribute"));
1947 // GetRenderer and GetEditor use a slightly different decision path about
1948 // which attribute to use. If a non-default attr object has one then it is
1949 // used, otherwise the default editor or renderer is fetched from the grid and
1950 // used. It should be the default for the data type of the cell. If it is
1951 // NULL (because the table has a type that the grid does not have in its
1952 // registry,) then the grid's default editor or renderer is used.
1954 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1956 wxGridCellRenderer
* renderer
= NULL
;
1958 if ( m_defGridAttr
!= this || grid
== NULL
)
1960 renderer
= m_renderer
; // use local attribute
1965 if ( !renderer
&& grid
) // get renderer for the data type
1967 // GetDefaultRendererForCell() will do IncRef() for us
1968 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1973 // if we still don't have one then use the grid default
1974 // (no need for IncRef() here neither)
1975 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1980 wxFAIL_MSG(wxT("Missing default cell attribute"));
1986 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1988 wxGridCellEditor
* editor
= NULL
;
1990 if ( m_defGridAttr
!= this || grid
== NULL
)
1992 editor
= m_editor
; // use local attribute
1997 if ( !editor
&& grid
) // get renderer for the data type
1998 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2001 // if we still don't have one then use the grid default
2002 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2006 wxFAIL_MSG(wxT("Missing default cell attribute"));
2012 // ----------------------------------------------------------------------------
2013 // wxGridCellAttrData
2014 // ----------------------------------------------------------------------------
2016 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2018 int n
= FindIndex(row
, col
);
2019 if ( n
== wxNOT_FOUND
)
2021 // add the attribute
2022 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2028 // change the attribute
2029 m_attrs
[(size_t)n
].attr
= attr
;
2033 // remove this attribute
2034 m_attrs
.RemoveAt((size_t)n
);
2039 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2041 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2043 int n
= FindIndex(row
, col
);
2044 if ( n
!= wxNOT_FOUND
)
2046 attr
= m_attrs
[(size_t)n
].attr
;
2053 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2055 size_t count
= m_attrs
.GetCount();
2056 for ( size_t n
= 0; n
< count
; n
++ )
2058 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2059 wxCoord row
= coords
.GetRow();
2060 if ((size_t)row
>= pos
)
2064 // If rows inserted, include row counter where necessary
2065 coords
.SetRow(row
+ numRows
);
2067 else if (numRows
< 0)
2069 // If rows deleted ...
2070 if ((size_t)row
>= pos
- numRows
)
2072 // ...either decrement row counter (if row still exists)...
2073 coords
.SetRow(row
+ numRows
);
2077 // ...or remove the attribute
2078 m_attrs
.RemoveAt((size_t)n
);
2086 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2088 size_t count
= m_attrs
.GetCount();
2089 for ( size_t n
= 0; n
< count
; n
++ )
2091 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2092 wxCoord col
= coords
.GetCol();
2093 if ( (size_t)col
>= pos
)
2097 // If rows inserted, include row counter where necessary
2098 coords
.SetCol(col
+ numCols
);
2100 else if (numCols
< 0)
2102 // If rows deleted ...
2103 if ((size_t)col
>= pos
- numCols
)
2105 // ...either decrement row counter (if row still exists)...
2106 coords
.SetCol(col
+ numCols
);
2110 // ...or remove the attribute
2111 m_attrs
.RemoveAt((size_t)n
);
2119 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2121 size_t count
= m_attrs
.GetCount();
2122 for ( size_t n
= 0; n
< count
; n
++ )
2124 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2125 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2134 // ----------------------------------------------------------------------------
2135 // wxGridRowOrColAttrData
2136 // ----------------------------------------------------------------------------
2138 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2140 size_t count
= m_attrs
.Count();
2141 for ( size_t n
= 0; n
< count
; n
++ )
2143 m_attrs
[n
]->DecRef();
2147 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2149 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2151 int n
= m_rowsOrCols
.Index(rowOrCol
);
2152 if ( n
!= wxNOT_FOUND
)
2154 attr
= m_attrs
[(size_t)n
];
2161 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2163 int i
= m_rowsOrCols
.Index(rowOrCol
);
2164 if ( i
== wxNOT_FOUND
)
2166 // add the attribute
2167 m_rowsOrCols
.Add(rowOrCol
);
2172 size_t n
= (size_t)i
;
2175 // change the attribute
2176 m_attrs
[n
]->DecRef();
2181 // remove this attribute
2182 m_attrs
[n
]->DecRef();
2183 m_rowsOrCols
.RemoveAt(n
);
2184 m_attrs
.RemoveAt(n
);
2189 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2191 size_t count
= m_attrs
.GetCount();
2192 for ( size_t n
= 0; n
< count
; n
++ )
2194 int & rowOrCol
= m_rowsOrCols
[n
];
2195 if ( (size_t)rowOrCol
>= pos
)
2197 if ( numRowsOrCols
> 0 )
2199 // If rows inserted, include row counter where necessary
2200 rowOrCol
+= numRowsOrCols
;
2202 else if ( numRowsOrCols
< 0)
2204 // If rows deleted, either decrement row counter (if row still exists)
2205 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2206 rowOrCol
+= numRowsOrCols
;
2209 m_rowsOrCols
.RemoveAt((size_t)n
);
2210 m_attrs
.RemoveAt((size_t)n
);
2218 // ----------------------------------------------------------------------------
2219 // wxGridCellAttrProvider
2220 // ----------------------------------------------------------------------------
2222 wxGridCellAttrProvider::wxGridCellAttrProvider()
2224 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2227 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2232 void wxGridCellAttrProvider::InitData()
2234 m_data
= new wxGridCellAttrProviderData
;
2237 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2239 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2242 // first look for the attribute of this specific cell
2243 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2247 // then look for the col attr (col attributes are more common than
2248 // the row ones, hence they have priority)
2249 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2254 // finally try the row attributes
2255 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2262 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2268 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2271 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2276 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2279 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2284 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2287 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2291 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2293 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2297 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2301 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2303 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2307 // ----------------------------------------------------------------------------
2308 // wxGridTypeRegistry
2309 // ----------------------------------------------------------------------------
2311 wxGridTypeRegistry::~wxGridTypeRegistry()
2313 size_t count
= m_typeinfo
.Count();
2314 for ( size_t i
= 0; i
< count
; i
++ )
2315 delete m_typeinfo
[i
];
2319 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2320 wxGridCellRenderer
* renderer
,
2321 wxGridCellEditor
* editor
)
2323 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2325 // is it already registered?
2326 int loc
= FindRegisteredDataType(typeName
);
2327 if ( loc
!= wxNOT_FOUND
)
2329 delete m_typeinfo
[loc
];
2330 m_typeinfo
[loc
] = info
;
2334 m_typeinfo
.Add(info
);
2338 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2340 size_t count
= m_typeinfo
.GetCount();
2341 for ( size_t i
= 0; i
< count
; i
++ )
2343 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2352 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2354 int index
= FindRegisteredDataType(typeName
);
2355 if ( index
== wxNOT_FOUND
)
2357 // check whether this is one of the standard ones, in which case
2358 // register it "on the fly"
2359 if ( typeName
== wxGRID_VALUE_STRING
)
2361 RegisterDataType(wxGRID_VALUE_STRING
,
2362 new wxGridCellStringRenderer
,
2363 new wxGridCellTextEditor
);
2365 else if ( typeName
== wxGRID_VALUE_BOOL
)
2367 RegisterDataType(wxGRID_VALUE_BOOL
,
2368 new wxGridCellBoolRenderer
,
2369 new wxGridCellBoolEditor
);
2371 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2373 RegisterDataType(wxGRID_VALUE_NUMBER
,
2374 new wxGridCellNumberRenderer
,
2375 new wxGridCellNumberEditor
);
2377 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2379 RegisterDataType(wxGRID_VALUE_FLOAT
,
2380 new wxGridCellFloatRenderer
,
2381 new wxGridCellFloatEditor
);
2383 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2385 RegisterDataType(wxGRID_VALUE_CHOICE
,
2386 new wxGridCellStringRenderer
,
2387 new wxGridCellChoiceEditor
);
2394 // we get here only if just added the entry for this type, so return
2396 index
= m_typeinfo
.GetCount() - 1;
2402 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2404 int index
= FindDataType(typeName
);
2405 if ( index
== wxNOT_FOUND
)
2407 // the first part of the typename is the "real" type, anything after ':'
2408 // are the parameters for the renderer
2409 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2410 if ( index
== wxNOT_FOUND
)
2415 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2416 wxGridCellRenderer
*rendererOld
= renderer
;
2417 renderer
= renderer
->Clone();
2418 rendererOld
->DecRef();
2420 wxGridCellEditor
*editor
= GetEditor(index
);
2421 wxGridCellEditor
*editorOld
= editor
;
2422 editor
= editor
->Clone();
2423 editorOld
->DecRef();
2425 // do it even if there are no parameters to reset them to defaults
2426 wxString params
= typeName
.AfterFirst(_T(':'));
2427 renderer
->SetParameters(params
);
2428 editor
->SetParameters(params
);
2430 // register the new typename
2431 RegisterDataType(typeName
, renderer
, editor
);
2433 // we just registered it, it's the last one
2434 index
= m_typeinfo
.GetCount() - 1;
2440 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2442 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2448 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2450 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2456 // ----------------------------------------------------------------------------
2458 // ----------------------------------------------------------------------------
2460 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2463 wxGridTableBase::wxGridTableBase()
2465 m_view
= (wxGrid
*) NULL
;
2466 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2469 wxGridTableBase::~wxGridTableBase()
2471 delete m_attrProvider
;
2474 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2476 delete m_attrProvider
;
2477 m_attrProvider
= attrProvider
;
2480 bool wxGridTableBase::CanHaveAttributes()
2482 if ( ! GetAttrProvider() )
2484 // use the default attr provider by default
2485 SetAttrProvider(new wxGridCellAttrProvider
);
2490 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2492 if ( m_attrProvider
)
2493 return m_attrProvider
->GetAttr(row
, col
);
2495 return (wxGridCellAttr
*)NULL
;
2498 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2500 if ( m_attrProvider
)
2502 m_attrProvider
->SetAttr(attr
, row
, col
);
2506 // as we take ownership of the pointer and don't store it, we must
2512 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2514 if ( m_attrProvider
)
2516 m_attrProvider
->SetRowAttr(attr
, row
);
2520 // as we take ownership of the pointer and don't store it, we must
2526 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2528 if ( m_attrProvider
)
2530 m_attrProvider
->SetColAttr(attr
, col
);
2534 // as we take ownership of the pointer and don't store it, we must
2540 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2541 size_t WXUNUSED(numRows
) )
2543 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2548 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2550 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2555 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2556 size_t WXUNUSED(numRows
) )
2558 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2563 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2564 size_t WXUNUSED(numCols
) )
2566 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2571 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2573 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2578 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2579 size_t WXUNUSED(numCols
) )
2581 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2587 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2590 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2591 // how much it makes sense to us geeks.
2595 wxString
wxGridTableBase::GetColLabelValue( int col
)
2597 // default col labels are:
2598 // cols 0 to 25 : A-Z
2599 // cols 26 to 675 : AA-ZZ
2604 for ( n
= 1; ; n
++ )
2606 s
+= (_T('A') + (wxChar
)( col%26
));
2608 if ( col
< 0 ) break;
2611 // reverse the string...
2613 for ( i
= 0; i
< n
; i
++ )
2622 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2624 return wxGRID_VALUE_STRING
;
2627 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2628 const wxString
& typeName
)
2630 return typeName
== wxGRID_VALUE_STRING
;
2633 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2635 return CanGetValueAs(row
, col
, typeName
);
2638 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2643 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2648 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2653 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2654 long WXUNUSED(value
) )
2658 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2659 double WXUNUSED(value
) )
2663 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2664 bool WXUNUSED(value
) )
2669 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2670 const wxString
& WXUNUSED(typeName
) )
2675 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2676 const wxString
& WXUNUSED(typeName
),
2677 void* WXUNUSED(value
) )
2681 //////////////////////////////////////////////////////////////////////
2683 // Message class for the grid table to send requests and notifications
2687 wxGridTableMessage::wxGridTableMessage()
2689 m_table
= (wxGridTableBase
*) NULL
;
2695 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2696 int commandInt1
, int commandInt2
)
2700 m_comInt1
= commandInt1
;
2701 m_comInt2
= commandInt2
;
2706 //////////////////////////////////////////////////////////////////////
2708 // A basic grid table for string data. An object of this class will
2709 // created by wxGrid if you don't specify an alternative table class.
2712 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2714 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2716 wxGridStringTable::wxGridStringTable()
2721 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2726 m_data
.Alloc( numRows
);
2729 sa
.Alloc( numCols
);
2730 for ( col
= 0; col
< numCols
; col
++ )
2732 sa
.Add( wxEmptyString
);
2735 for ( row
= 0; row
< numRows
; row
++ )
2741 wxGridStringTable::~wxGridStringTable()
2745 int wxGridStringTable::GetNumberRows()
2747 return m_data
.GetCount();
2750 int wxGridStringTable::GetNumberCols()
2752 if ( m_data
.GetCount() > 0 )
2753 return m_data
[0].GetCount();
2758 wxString
wxGridStringTable::GetValue( int row
, int col
)
2760 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2761 _T("invalid row or column index in wxGridStringTable") );
2763 return m_data
[row
][col
];
2766 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2768 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2769 _T("invalid row or column index in wxGridStringTable") );
2771 m_data
[row
][col
] = value
;
2774 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2776 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2777 _T("invalid row or column index in wxGridStringTable") );
2779 return (m_data
[row
][col
] == wxEmptyString
);
2782 void wxGridStringTable::Clear()
2785 int numRows
, numCols
;
2787 numRows
= m_data
.GetCount();
2790 numCols
= m_data
[0].GetCount();
2792 for ( row
= 0; row
< numRows
; row
++ )
2794 for ( col
= 0; col
< numCols
; col
++ )
2796 m_data
[row
][col
] = wxEmptyString
;
2803 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2807 size_t curNumRows
= m_data
.GetCount();
2808 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2809 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2811 if ( pos
>= curNumRows
)
2813 return AppendRows( numRows
);
2817 sa
.Alloc( curNumCols
);
2818 for ( col
= 0; col
< curNumCols
; col
++ )
2820 sa
.Add( wxEmptyString
);
2823 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2825 m_data
.Insert( sa
, row
);
2829 wxGridTableMessage
msg( this,
2830 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2834 GetView()->ProcessTableMessage( msg
);
2840 bool wxGridStringTable::AppendRows( size_t numRows
)
2844 size_t curNumRows
= m_data
.GetCount();
2845 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2846 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2849 if ( curNumCols
> 0 )
2851 sa
.Alloc( curNumCols
);
2852 for ( col
= 0; col
< curNumCols
; col
++ )
2854 sa
.Add( wxEmptyString
);
2858 for ( row
= 0; row
< numRows
; row
++ )
2865 wxGridTableMessage
msg( this,
2866 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2869 GetView()->ProcessTableMessage( msg
);
2875 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2879 size_t curNumRows
= m_data
.GetCount();
2881 if ( pos
>= curNumRows
)
2884 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2885 pos
, numRows
, curNumRows
);
2886 wxFAIL_MSG( errmsg
);
2890 if ( numRows
> curNumRows
- pos
)
2892 numRows
= curNumRows
- pos
;
2895 if ( numRows
>= curNumRows
)
2897 m_data
.Empty(); // don't release memory just yet
2901 for ( n
= 0; n
< numRows
; n
++ )
2903 m_data
.Remove( pos
);
2908 wxGridTableMessage
msg( this,
2909 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2913 GetView()->ProcessTableMessage( msg
);
2919 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2923 size_t curNumRows
= m_data
.GetCount();
2924 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2925 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2927 if ( pos
>= curNumCols
)
2929 return AppendCols( numCols
);
2932 for ( row
= 0; row
< curNumRows
; row
++ )
2934 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2936 m_data
[row
].Insert( wxEmptyString
, col
);
2941 wxGridTableMessage
msg( this,
2942 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2946 GetView()->ProcessTableMessage( msg
);
2952 bool wxGridStringTable::AppendCols( size_t numCols
)
2956 size_t curNumRows
= m_data
.GetCount();
2960 // TODO: something better than this ?
2962 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2967 for ( row
= 0; row
< curNumRows
; row
++ )
2969 for ( n
= 0; n
< numCols
; n
++ )
2971 m_data
[row
].Add( wxEmptyString
);
2977 wxGridTableMessage
msg( this,
2978 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2981 GetView()->ProcessTableMessage( msg
);
2987 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
2991 size_t curNumRows
= m_data
.GetCount();
2992 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2993 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2995 if ( pos
>= curNumCols
)
2998 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
2999 pos
, numCols
, curNumCols
);
3000 wxFAIL_MSG( errmsg
);
3004 if ( numCols
> curNumCols
- pos
)
3006 numCols
= curNumCols
- pos
;
3009 for ( row
= 0; row
< curNumRows
; row
++ )
3011 if ( numCols
>= curNumCols
)
3013 m_data
[row
].Clear();
3017 for ( n
= 0; n
< numCols
; n
++ )
3019 m_data
[row
].Remove( pos
);
3025 wxGridTableMessage
msg( this,
3026 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3030 GetView()->ProcessTableMessage( msg
);
3036 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3038 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3040 // using default label
3042 return wxGridTableBase::GetRowLabelValue( row
);
3046 return m_rowLabels
[ row
];
3050 wxString
wxGridStringTable::GetColLabelValue( int col
)
3052 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3054 // using default label
3056 return wxGridTableBase::GetColLabelValue( col
);
3060 return m_colLabels
[ col
];
3064 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3066 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3068 int n
= m_rowLabels
.GetCount();
3070 for ( i
= n
; i
<= row
; i
++ )
3072 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3076 m_rowLabels
[row
] = value
;
3079 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3081 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3083 int n
= m_colLabels
.GetCount();
3085 for ( i
= n
; i
<= col
; i
++ )
3087 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3091 m_colLabels
[col
] = value
;
3096 //////////////////////////////////////////////////////////////////////
3097 //////////////////////////////////////////////////////////////////////
3099 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3101 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3102 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3103 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3104 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3105 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3108 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3110 const wxPoint
&pos
, const wxSize
&size
)
3111 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3116 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3120 // NO - don't do this because it will set both the x and y origin
3121 // coords to match the parent scrolled window and we just want to
3122 // set the y coord - MB
3124 // m_owner->PrepareDC( dc );
3127 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3128 dc
.SetDeviceOrigin( 0, -y
);
3130 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3131 m_owner
->DrawRowLabels( dc
);
3135 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3137 m_owner
->ProcessRowLabelMouseEvent( event
);
3141 // This seems to be required for wxMotif otherwise the mouse
3142 // cursor must be in the cell edit control to get key events
3144 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3146 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3149 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3151 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3156 //////////////////////////////////////////////////////////////////////
3158 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3160 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3161 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3162 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3163 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3164 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3167 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3169 const wxPoint
&pos
, const wxSize
&size
)
3170 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3175 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3179 // NO - don't do this because it will set both the x and y origin
3180 // coords to match the parent scrolled window and we just want to
3181 // set the x coord - MB
3183 // m_owner->PrepareDC( dc );
3186 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3187 dc
.SetDeviceOrigin( -x
, 0 );
3189 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3190 m_owner
->DrawColLabels( dc
);
3194 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3196 m_owner
->ProcessColLabelMouseEvent( event
);
3200 // This seems to be required for wxMotif otherwise the mouse
3201 // cursor must be in the cell edit control to get key events
3203 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3205 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3208 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3210 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3215 //////////////////////////////////////////////////////////////////////
3217 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3219 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3220 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3221 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3222 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3223 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3226 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3228 const wxPoint
&pos
, const wxSize
&size
)
3229 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3234 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3238 int client_height
= 0;
3239 int client_width
= 0;
3240 GetClientSize( &client_width
, &client_height
);
3242 dc
.SetPen( *wxBLACK_PEN
);
3243 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3244 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3246 dc
.SetPen( *wxWHITE_PEN
);
3247 dc
.DrawLine( 0, 0, client_width
, 0 );
3248 dc
.DrawLine( 0, 0, 0, client_height
);
3252 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3254 m_owner
->ProcessCornerLabelMouseEvent( event
);
3258 // This seems to be required for wxMotif otherwise the mouse
3259 // cursor must be in the cell edit control to get key events
3261 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3263 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3266 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3268 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3273 //////////////////////////////////////////////////////////////////////
3275 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3277 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3278 EVT_PAINT( wxGridWindow::OnPaint
)
3279 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3280 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3281 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3282 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3285 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3286 wxGridRowLabelWindow
*rowLblWin
,
3287 wxGridColLabelWindow
*colLblWin
,
3288 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3289 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3292 m_rowLabelWin
= rowLblWin
;
3293 m_colLabelWin
= colLblWin
;
3294 SetBackgroundColour( "WHITE" );
3298 wxGridWindow::~wxGridWindow()
3303 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3305 wxPaintDC
dc( this );
3306 m_owner
->PrepareDC( dc
);
3307 wxRegion reg
= GetUpdateRegion();
3308 m_owner
->CalcCellsExposed( reg
);
3309 m_owner
->DrawGridCellArea( dc
);
3310 #if WXGRID_DRAW_LINES
3311 m_owner
->DrawAllGridLines( dc
, reg
);
3313 m_owner
->DrawGridSpace( dc
);
3314 m_owner
->DrawHighlight( dc
);
3318 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3320 wxPanel::ScrollWindow( dx
, dy
, rect
);
3321 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3322 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3326 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3328 m_owner
->ProcessGridCellMouseEvent( event
);
3332 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3333 // cursor must be in the cell edit control to get key events
3335 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3337 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3340 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3342 if ( !m_owner
->ProcessEvent( event
) ) event
.Skip();
3345 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3350 //////////////////////////////////////////////////////////////////////
3353 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3355 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3356 EVT_PAINT( wxGrid::OnPaint
)
3357 EVT_SIZE( wxGrid::OnSize
)
3358 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3359 EVT_KEY_UP( wxGrid::OnKeyUp
)
3360 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3363 wxGrid::wxGrid( wxWindow
*parent
,
3368 const wxString
& name
)
3369 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3370 m_colMinWidths(GRID_HASH_SIZE
),
3371 m_rowMinHeights(GRID_HASH_SIZE
)
3380 wxSafeDecRef(m_defaultCellAttr
);
3382 #ifdef DEBUG_ATTR_CACHE
3383 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3384 wxPrintf(_T("wxGrid attribute cache statistics: "
3385 "total: %u, hits: %u (%u%%)\n"),
3386 total
, gs_nAttrCacheHits
,
3387 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3393 delete m_typeRegistry
;
3399 // ----- internal init and update functions
3402 void wxGrid::Create()
3404 m_created
= FALSE
; // set to TRUE by CreateGrid
3406 m_table
= (wxGridTableBase
*) NULL
;
3409 m_cellEditCtrlEnabled
= FALSE
;
3411 m_defaultCellAttr
= new wxGridCellAttr
;
3412 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3414 // Set default cell attributes
3415 m_defaultCellAttr
->SetFont(GetFont());
3416 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3417 m_defaultCellAttr
->SetTextColour(
3418 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3419 m_defaultCellAttr
->SetBackgroundColour(
3420 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3421 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3422 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3427 m_currentCellCoords
= wxGridNoCellCoords
;
3429 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3430 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3432 // create the type registry
3433 m_typeRegistry
= new wxGridTypeRegistry
;
3435 // subwindow components that make up the wxGrid
3436 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3441 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3446 m_colLabelWin
= new wxGridColLabelWindow( this,
3451 m_gridWin
= new wxGridWindow( this,
3458 SetTargetWindow( m_gridWin
);
3462 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3463 wxGrid::wxGridSelectionModes selmode
)
3465 wxCHECK_MSG( !m_created
,
3467 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3469 m_numRows
= numRows
;
3470 m_numCols
= numCols
;
3472 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3473 m_table
->SetView( this );
3475 m_selection
= new wxGridSelection( this, selmode
);
3482 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3486 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3489 m_selection
->SetSelectionMode( selmode
);
3492 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3493 wxGrid::wxGridSelectionModes selmode
)
3497 // RD: Actually, this should probably be allowed. I think it would be
3498 // nice to be able to switch multiple Tables in and out of a single
3499 // View at runtime. Is there anything in the implmentation that would
3502 // At least, you now have to cope with m_selection
3503 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3508 m_numRows
= table
->GetNumberRows();
3509 m_numCols
= table
->GetNumberCols();
3512 m_table
->SetView( this );
3515 m_selection
= new wxGridSelection( this, selmode
);
3526 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3527 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3529 if ( m_rowLabelWin
)
3531 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3535 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3538 m_labelTextColour
= wxColour( _T("BLACK") );
3541 m_attrCache
.row
= -1;
3543 // TODO: something better than this ?
3545 m_labelFont
= this->GetFont();
3546 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3548 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3549 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3551 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3552 m_colLabelVertAlign
= wxALIGN_TOP
;
3554 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3555 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3557 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3558 m_defaultRowHeight
+= 8;
3560 m_defaultRowHeight
+= 4;
3563 m_gridLineColour
= wxColour( 128, 128, 255 );
3564 m_gridLinesEnabled
= TRUE
;
3565 m_cellHighlightColour
= m_gridLineColour
;
3567 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3568 m_winCapture
= (wxWindow
*)NULL
;
3569 m_canDragRowSize
= TRUE
;
3570 m_canDragColSize
= TRUE
;
3571 m_canDragGridSize
= TRUE
;
3573 m_dragRowOrCol
= -1;
3574 m_isDragging
= FALSE
;
3575 m_startDragPos
= wxDefaultPosition
;
3577 m_waitForSlowClick
= FALSE
;
3579 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3580 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3582 m_currentCellCoords
= wxGridNoCellCoords
;
3584 m_selectingTopLeft
= wxGridNoCellCoords
;
3585 m_selectingBottomRight
= wxGridNoCellCoords
;
3586 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3587 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3589 m_editable
= TRUE
; // default for whole grid
3591 m_inOnKeyDown
= FALSE
;
3600 // ----------------------------------------------------------------------------
3601 // the idea is to call these functions only when necessary because they create
3602 // quite big arrays which eat memory mostly unnecessary - in particular, if
3603 // default widths/heights are used for all rows/columns, we may not use these
3606 // with some extra code, it should be possible to only store the
3607 // widths/heights different from default ones but this will be done later...
3608 // ----------------------------------------------------------------------------
3610 void wxGrid::InitRowHeights()
3612 m_rowHeights
.Empty();
3613 m_rowBottoms
.Empty();
3615 m_rowHeights
.Alloc( m_numRows
);
3616 m_rowBottoms
.Alloc( m_numRows
);
3619 for ( int i
= 0; i
< m_numRows
; i
++ )
3621 m_rowHeights
.Add( m_defaultRowHeight
);
3622 rowBottom
+= m_defaultRowHeight
;
3623 m_rowBottoms
.Add( rowBottom
);
3627 void wxGrid::InitColWidths()
3629 m_colWidths
.Empty();
3630 m_colRights
.Empty();
3632 m_colWidths
.Alloc( m_numCols
);
3633 m_colRights
.Alloc( m_numCols
);
3635 for ( int i
= 0; i
< m_numCols
; i
++ )
3637 m_colWidths
.Add( m_defaultColWidth
);
3638 colRight
+= m_defaultColWidth
;
3639 m_colRights
.Add( colRight
);
3643 int wxGrid::GetColWidth(int col
) const
3645 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3648 int wxGrid::GetColLeft(int col
) const
3650 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3651 : m_colRights
[col
] - m_colWidths
[col
];
3654 int wxGrid::GetColRight(int col
) const
3656 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3660 int wxGrid::GetRowHeight(int row
) const
3662 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3665 int wxGrid::GetRowTop(int row
) const
3667 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3668 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3671 int wxGrid::GetRowBottom(int row
) const
3673 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3674 : m_rowBottoms
[row
];
3677 void wxGrid::CalcDimensions()
3680 GetClientSize( &cw
, &ch
);
3682 if ( m_numRows
> 0 || m_numCols
> 0 )
3684 int right
= m_numCols
> 0 ? GetColRight( m_numCols
-1 ) + m_extraWidth
: 0;
3685 int bottom
= m_numRows
> 0 ? GetRowBottom( m_numRows
-1 ) + m_extraHeight
: 0;
3687 // TODO: restore the scroll position that we had before sizing
3690 GetViewStart( &x
, &y
);
3691 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3692 right
/GRID_SCROLL_LINE
, bottom
/GRID_SCROLL_LINE
,
3698 void wxGrid::CalcWindowSizes()
3701 GetClientSize( &cw
, &ch
);
3703 if ( m_cornerLabelWin
->IsShown() )
3704 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3706 if ( m_colLabelWin
->IsShown() )
3707 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3709 if ( m_rowLabelWin
->IsShown() )
3710 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3712 if ( m_gridWin
->IsShown() )
3713 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3717 // this is called when the grid table sends a message to say that it
3718 // has been redimensioned
3720 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3723 bool result
= FALSE
;
3726 // if we were using the default widths/heights so far, we must change them
3728 if ( m_colWidths
.IsEmpty() )
3733 if ( m_rowHeights
.IsEmpty() )
3739 switch ( msg
.GetId() )
3741 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3743 size_t pos
= msg
.GetCommandInt();
3744 int numRows
= msg
.GetCommandInt2();
3746 m_numRows
+= numRows
;
3748 if ( !m_rowHeights
.IsEmpty() )
3750 for ( i
= 0; i
< numRows
; i
++ )
3752 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3753 m_rowBottoms
.Insert( 0, pos
);
3757 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3759 for ( i
= pos
; i
< m_numRows
; i
++ )
3761 bottom
+= m_rowHeights
[i
];
3762 m_rowBottoms
[i
] = bottom
;
3765 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3767 // if we have just inserted cols into an empty grid the current
3768 // cell will be undefined...
3770 SetCurrentCell( 0, 0 );
3772 m_selection
->UpdateRows( pos
, numRows
);
3773 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3775 attrProvider
->UpdateAttrRows( pos
, numRows
);
3777 if ( !GetBatchCount() )
3780 m_rowLabelWin
->Refresh();
3786 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3788 int numRows
= msg
.GetCommandInt();
3789 int oldNumRows
= m_numRows
;
3790 m_numRows
+= numRows
;
3792 if ( !m_rowHeights
.IsEmpty() )
3794 for ( i
= 0; i
< numRows
; i
++ )
3796 m_rowHeights
.Add( m_defaultRowHeight
);
3797 m_rowBottoms
.Add( 0 );
3801 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3803 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3805 bottom
+= m_rowHeights
[i
];
3806 m_rowBottoms
[i
] = bottom
;
3809 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3811 // if we have just inserted cols into an empty grid the current
3812 // cell will be undefined...
3814 SetCurrentCell( 0, 0 );
3816 if ( !GetBatchCount() )
3819 m_rowLabelWin
->Refresh();
3825 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3827 size_t pos
= msg
.GetCommandInt();
3828 int numRows
= msg
.GetCommandInt2();
3829 m_numRows
-= numRows
;
3831 if ( !m_rowHeights
.IsEmpty() )
3833 for ( i
= 0; i
< numRows
; i
++ )
3835 m_rowHeights
.Remove( pos
);
3836 m_rowBottoms
.Remove( pos
);
3840 for ( i
= 0; i
< m_numRows
; i
++ )
3842 h
+= m_rowHeights
[i
];
3843 m_rowBottoms
[i
] = h
;
3848 m_currentCellCoords
= wxGridNoCellCoords
;
3852 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3853 m_currentCellCoords
.Set( 0, 0 );
3855 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3856 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3858 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3859 // ifdef'd out following patch from Paul Gammans
3861 // No need to touch column attributes, unless we
3862 // removed _all_ rows, in this case, we remove
3863 // all column attributes.
3864 // I hate to do this here, but the
3865 // needed data is not available inside UpdateAttrRows.
3866 if ( !GetNumberRows() )
3867 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3870 if ( !GetBatchCount() )
3873 m_rowLabelWin
->Refresh();
3879 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3881 size_t pos
= msg
.GetCommandInt();
3882 int numCols
= msg
.GetCommandInt2();
3883 m_numCols
+= numCols
;
3885 if ( !m_colWidths
.IsEmpty() )
3887 for ( i
= 0; i
< numCols
; i
++ )
3889 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3890 m_colRights
.Insert( 0, pos
);
3894 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3896 for ( i
= pos
; i
< m_numCols
; i
++ )
3898 right
+= m_colWidths
[i
];
3899 m_colRights
[i
] = right
;
3902 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3904 // if we have just inserted cols into an empty grid the current
3905 // cell will be undefined...
3907 SetCurrentCell( 0, 0 );
3909 m_selection
->UpdateCols( pos
, numCols
);
3910 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3912 attrProvider
->UpdateAttrCols( pos
, numCols
);
3913 if ( !GetBatchCount() )
3916 m_colLabelWin
->Refresh();
3923 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3925 int numCols
= msg
.GetCommandInt();
3926 int oldNumCols
= m_numCols
;
3927 m_numCols
+= numCols
;
3928 if ( !m_colWidths
.IsEmpty() )
3930 for ( i
= 0; i
< numCols
; i
++ )
3932 m_colWidths
.Add( m_defaultColWidth
);
3933 m_colRights
.Add( 0 );
3937 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
3939 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
3941 right
+= m_colWidths
[i
];
3942 m_colRights
[i
] = right
;
3945 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3947 // if we have just inserted cols into an empty grid the current
3948 // cell will be undefined...
3950 SetCurrentCell( 0, 0 );
3952 if ( !GetBatchCount() )
3955 m_colLabelWin
->Refresh();
3961 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
3963 size_t pos
= msg
.GetCommandInt();
3964 int numCols
= msg
.GetCommandInt2();
3965 m_numCols
-= numCols
;
3967 if ( !m_colWidths
.IsEmpty() )
3969 for ( i
= 0; i
< numCols
; i
++ )
3971 m_colWidths
.Remove( pos
);
3972 m_colRights
.Remove( pos
);
3976 for ( i
= 0; i
< m_numCols
; i
++ )
3978 w
+= m_colWidths
[i
];
3984 m_currentCellCoords
= wxGridNoCellCoords
;
3988 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
3989 m_currentCellCoords
.Set( 0, 0 );
3991 m_selection
->UpdateCols( pos
, -((int)numCols
) );
3992 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3994 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
3995 // ifdef'd out following patch from Paul Gammans
3997 // No need to touch row attributes, unless we
3998 // removed _all_ columns, in this case, we remove
3999 // all row attributes.
4000 // I hate to do this here, but the
4001 // needed data is not available inside UpdateAttrCols.
4002 if ( !GetNumberCols() )
4003 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4006 if ( !GetBatchCount() )
4009 m_colLabelWin
->Refresh();
4014 // There is no path to this code !!!!!!
4020 if (result
&& !GetBatchCount() )
4021 m_gridWin
->Refresh();
4026 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4028 wxRegionIterator
iter( reg
);
4031 m_rowLabelsExposed
.Empty();
4038 // TODO: remove this when we can...
4039 // There is a bug in wxMotif that gives garbage update
4040 // rectangles if you jump-scroll a long way by clicking the
4041 // scrollbar with middle button. This is a work-around
4043 #if defined(__WXMOTIF__)
4045 m_gridWin
->GetClientSize( &cw
, &ch
);
4046 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4047 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4050 // logical bounds of update region
4053 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4054 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4056 // find the row labels within these bounds
4059 for ( row
= 0; row
< m_numRows
; row
++ )
4061 if ( GetRowBottom(row
) < top
)
4064 if ( GetRowTop(row
) > bottom
)
4067 m_rowLabelsExposed
.Add( row
);
4075 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4077 wxRegionIterator
iter( reg
);
4080 m_colLabelsExposed
.Empty();
4087 // TODO: remove this when we can...
4088 // There is a bug in wxMotif that gives garbage update
4089 // rectangles if you jump-scroll a long way by clicking the
4090 // scrollbar with middle button. This is a work-around
4092 #if defined(__WXMOTIF__)
4094 m_gridWin
->GetClientSize( &cw
, &ch
);
4095 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4096 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4099 // logical bounds of update region
4102 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4103 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4105 // find the cells within these bounds
4108 for ( col
= 0; col
< m_numCols
; col
++ )
4110 if ( GetColRight(col
) < left
)
4113 if ( GetColLeft(col
) > right
)
4116 m_colLabelsExposed
.Add( col
);
4124 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4126 wxRegionIterator
iter( reg
);
4129 m_cellsExposed
.Empty();
4130 m_rowsExposed
.Empty();
4131 m_colsExposed
.Empty();
4133 int left
, top
, right
, bottom
;
4138 // TODO: remove this when we can...
4139 // There is a bug in wxMotif that gives garbage update
4140 // rectangles if you jump-scroll a long way by clicking the
4141 // scrollbar with middle button. This is a work-around
4143 #if defined(__WXMOTIF__)
4145 m_gridWin
->GetClientSize( &cw
, &ch
);
4146 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4147 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4148 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4149 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4152 // logical bounds of update region
4154 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4155 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4157 // find the cells within these bounds
4160 for ( row
= 0; row
< m_numRows
; row
++ )
4162 if ( GetRowBottom(row
) <= top
)
4165 if ( GetRowTop(row
) > bottom
)
4168 m_rowsExposed
.Add( row
);
4170 for ( col
= 0; col
< m_numCols
; col
++ )
4172 if ( GetColRight(col
) <= left
)
4175 if ( GetColLeft(col
) > right
)
4178 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4179 m_colsExposed
.Add( col
);
4180 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4189 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4192 wxPoint
pos( event
.GetPosition() );
4193 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4195 if ( event
.Dragging() )
4197 m_isDragging
= TRUE
;
4199 if ( event
.LeftIsDown() )
4201 switch( m_cursorMode
)
4203 case WXGRID_CURSOR_RESIZE_ROW
:
4205 int cw
, ch
, left
, dummy
;
4206 m_gridWin
->GetClientSize( &cw
, &ch
);
4207 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4209 wxClientDC
dc( m_gridWin
);
4212 GetRowTop(m_dragRowOrCol
) +
4213 GetRowMinimalHeight(m_dragRowOrCol
) );
4214 dc
.SetLogicalFunction(wxINVERT
);
4215 if ( m_dragLastPos
>= 0 )
4217 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4219 dc
.DrawLine( left
, y
, left
+cw
, y
);
4224 case WXGRID_CURSOR_SELECT_ROW
:
4225 if ( (row
= YToRow( y
)) >= 0 )
4227 m_selection
->SelectRow( row
,
4228 event
.ControlDown(),
4234 // default label to suppress warnings about "enumeration value
4235 // 'xxx' not handled in switch
4243 m_isDragging
= FALSE
;
4246 // ------------ Entering or leaving the window
4248 if ( event
.Entering() || event
.Leaving() )
4250 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4254 // ------------ Left button pressed
4256 else if ( event
.LeftDown() )
4258 // don't send a label click event for a hit on the
4259 // edge of the row label - this is probably the user
4260 // wanting to resize the row
4262 if ( YToEdgeOfRow(y
) < 0 )
4266 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4268 if ( !event
.ShiftDown() && !event
.ControlDown() )
4270 if ( event
.ShiftDown() )
4271 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4274 GetNumberCols() - 1,
4275 event
.ControlDown(),
4280 m_selection
->SelectRow( row
,
4281 event
.ControlDown(),
4285 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4290 // starting to drag-resize a row
4292 if ( CanDragRowSize() )
4293 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4298 // ------------ Left double click
4300 else if (event
.LeftDClick() )
4302 if ( YToEdgeOfRow(y
) < 0 )
4305 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4310 // ------------ Left button released
4312 else if ( event
.LeftUp() )
4314 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4316 DoEndDragResizeRow();
4318 // Note: we are ending the event *after* doing
4319 // default processing in this case
4321 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4324 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4329 // ------------ Right button down
4331 else if ( event
.RightDown() )
4334 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4336 // no default action at the moment
4341 // ------------ Right double click
4343 else if ( event
.RightDClick() )
4346 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4348 // no default action at the moment
4353 // ------------ No buttons down and mouse moving
4355 else if ( event
.Moving() )
4357 m_dragRowOrCol
= YToEdgeOfRow( y
);
4358 if ( m_dragRowOrCol
>= 0 )
4360 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4362 // don't capture the mouse yet
4363 if ( CanDragRowSize() )
4364 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4367 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4369 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4375 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4378 wxPoint
pos( event
.GetPosition() );
4379 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4381 if ( event
.Dragging() )
4383 m_isDragging
= TRUE
;
4385 if ( event
.LeftIsDown() )
4387 switch( m_cursorMode
)
4389 case WXGRID_CURSOR_RESIZE_COL
:
4391 int cw
, ch
, dummy
, top
;
4392 m_gridWin
->GetClientSize( &cw
, &ch
);
4393 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4395 wxClientDC
dc( m_gridWin
);
4398 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4399 GetColMinimalWidth(m_dragRowOrCol
));
4400 dc
.SetLogicalFunction(wxINVERT
);
4401 if ( m_dragLastPos
>= 0 )
4403 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4405 dc
.DrawLine( x
, top
, x
, top
+ch
);
4410 case WXGRID_CURSOR_SELECT_COL
:
4411 if ( (col
= XToCol( x
)) >= 0 )
4413 m_selection
->SelectCol( col
,
4414 event
.ControlDown(),
4420 // default label to suppress warnings about "enumeration value
4421 // 'xxx' not handled in switch
4429 m_isDragging
= FALSE
;
4432 // ------------ Entering or leaving the window
4434 if ( event
.Entering() || event
.Leaving() )
4436 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4440 // ------------ Left button pressed
4442 else if ( event
.LeftDown() )
4444 // don't send a label click event for a hit on the
4445 // edge of the col label - this is probably the user
4446 // wanting to resize the col
4448 if ( XToEdgeOfCol(x
) < 0 )
4452 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4454 if ( !event
.ShiftDown() && !event
.ControlDown() )
4456 if ( event
.ShiftDown() )
4457 m_selection
->SelectBlock( 0,
4458 m_currentCellCoords
.GetCol(),
4459 GetNumberRows() - 1, col
,
4460 event
.ControlDown(),
4465 m_selection
->SelectCol( col
,
4466 event
.ControlDown(),
4470 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4475 // starting to drag-resize a col
4477 if ( CanDragColSize() )
4478 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4483 // ------------ Left double click
4485 if ( event
.LeftDClick() )
4487 if ( XToEdgeOfCol(x
) < 0 )
4490 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4495 // ------------ Left button released
4497 else if ( event
.LeftUp() )
4499 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4501 DoEndDragResizeCol();
4503 // Note: we are ending the event *after* doing
4504 // default processing in this case
4506 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4509 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4514 // ------------ Right button down
4516 else if ( event
.RightDown() )
4519 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4521 // no default action at the moment
4526 // ------------ Right double click
4528 else if ( event
.RightDClick() )
4531 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4533 // no default action at the moment
4538 // ------------ No buttons down and mouse moving
4540 else if ( event
.Moving() )
4542 m_dragRowOrCol
= XToEdgeOfCol( x
);
4543 if ( m_dragRowOrCol
>= 0 )
4545 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4547 // don't capture the cursor yet
4548 if ( CanDragColSize() )
4549 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4552 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4554 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4560 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4562 if ( event
.LeftDown() )
4564 // indicate corner label by having both row and
4567 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4573 else if ( event
.LeftDClick() )
4575 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4578 else if ( event
.RightDown() )
4580 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4582 // no default action at the moment
4586 else if ( event
.RightDClick() )
4588 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4590 // no default action at the moment
4595 void wxGrid::ChangeCursorMode(CursorMode mode
,
4600 static const wxChar
*cursorModes
[] =
4609 wxLogTrace(_T("grid"),
4610 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4611 win
== m_colLabelWin
? _T("colLabelWin")
4612 : win
? _T("rowLabelWin")
4614 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4615 #endif // __WXDEBUG__
4617 if ( mode
== m_cursorMode
)
4622 // by default use the grid itself
4628 m_winCapture
->ReleaseMouse();
4629 m_winCapture
= (wxWindow
*)NULL
;
4632 m_cursorMode
= mode
;
4634 switch ( m_cursorMode
)
4636 case WXGRID_CURSOR_RESIZE_ROW
:
4637 win
->SetCursor( m_rowResizeCursor
);
4640 case WXGRID_CURSOR_RESIZE_COL
:
4641 win
->SetCursor( m_colResizeCursor
);
4645 win
->SetCursor( *wxSTANDARD_CURSOR
);
4648 // we need to capture mouse when resizing
4649 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4650 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4652 if ( captureMouse
&& resize
)
4654 win
->CaptureMouse();
4659 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4662 wxPoint
pos( event
.GetPosition() );
4663 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4665 wxGridCellCoords coords
;
4666 XYToCell( x
, y
, coords
);
4668 if ( event
.Dragging() )
4670 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4672 // Don't start doing anything until the mouse has been drug at
4673 // least 3 pixels in any direction...
4676 if (m_startDragPos
== wxDefaultPosition
)
4678 m_startDragPos
= pos
;
4681 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4685 m_isDragging
= TRUE
;
4686 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4688 // Hide the edit control, so it
4689 // won't interfer with drag-shrinking.
4690 if ( IsCellEditControlShown() )
4692 HideCellEditControl();
4693 SaveEditControlValue();
4696 // Have we captured the mouse yet?
4699 m_winCapture
= m_gridWin
;
4700 m_winCapture
->CaptureMouse();
4703 if ( coords
!= wxGridNoCellCoords
)
4705 if ( event
.ControlDown() )
4707 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4708 m_selectingKeyboard
= coords
;
4709 HighlightBlock ( m_selectingKeyboard
, coords
);
4713 if ( !IsSelection() )
4715 HighlightBlock( coords
, coords
);
4719 HighlightBlock( m_currentCellCoords
, coords
);
4723 if (! IsVisible(coords
))
4725 MakeCellVisible(coords
);
4726 // TODO: need to introduce a delay or something here. The
4727 // scrolling is way to fast, at least on MSW - also on GTK.
4731 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4733 int cw
, ch
, left
, dummy
;
4734 m_gridWin
->GetClientSize( &cw
, &ch
);
4735 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4737 wxClientDC
dc( m_gridWin
);
4739 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4740 GetRowMinimalHeight(m_dragRowOrCol
) );
4741 dc
.SetLogicalFunction(wxINVERT
);
4742 if ( m_dragLastPos
>= 0 )
4744 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4746 dc
.DrawLine( left
, y
, left
+cw
, y
);
4749 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4751 int cw
, ch
, dummy
, top
;
4752 m_gridWin
->GetClientSize( &cw
, &ch
);
4753 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4755 wxClientDC
dc( m_gridWin
);
4757 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4758 GetColMinimalWidth(m_dragRowOrCol
) );
4759 dc
.SetLogicalFunction(wxINVERT
);
4760 if ( m_dragLastPos
>= 0 )
4762 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4764 dc
.DrawLine( x
, top
, x
, top
+ch
);
4771 m_isDragging
= FALSE
;
4772 m_startDragPos
= wxDefaultPosition
;
4774 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4775 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4778 if ( event
.Entering() || event
.Leaving() )
4780 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4781 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4786 // ------------ Left button pressed
4788 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4790 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4795 if ( !event
.ControlDown() )
4797 if ( event
.ShiftDown() )
4799 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4800 m_currentCellCoords
.GetCol(),
4803 event
.ControlDown(),
4808 else if ( XToEdgeOfCol(x
) < 0 &&
4809 YToEdgeOfRow(y
) < 0 )
4811 DisableCellEditControl();
4812 MakeCellVisible( coords
);
4814 // if this is the second click on this cell then start
4816 if ( m_waitForSlowClick
&&
4817 (coords
== m_currentCellCoords
) &&
4818 CanEnableCellControl())
4820 EnableCellEditControl();
4822 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4823 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4826 editor
->StartingClick();
4830 m_waitForSlowClick
= FALSE
;
4834 if ( event
.ControlDown() )
4836 m_selection
->ToggleCellSelection( coords
.GetRow(),
4838 event
.ControlDown(),
4842 m_selectingTopLeft
= wxGridNoCellCoords
;
4843 m_selectingBottomRight
= wxGridNoCellCoords
;
4844 m_selectingKeyboard
= coords
;
4848 SetCurrentCell( coords
);
4849 if ( m_selection
->GetSelectionMode()
4850 != wxGrid::wxGridSelectCells
)
4851 HighlightBlock( coords
, coords
);
4853 m_waitForSlowClick
= TRUE
;
4860 // ------------ Left double click
4862 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4864 DisableCellEditControl();
4866 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4868 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4876 // ------------ Left button released
4878 else if ( event
.LeftUp() )
4880 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4882 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4883 m_selectingBottomRight
!= wxGridNoCellCoords
)
4887 m_winCapture
->ReleaseMouse();
4888 m_winCapture
= NULL
;
4890 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4891 m_selectingTopLeft
.GetCol(),
4892 m_selectingBottomRight
.GetRow(),
4893 m_selectingBottomRight
.GetCol(),
4894 event
.ControlDown(),
4898 m_selectingTopLeft
= wxGridNoCellCoords
;
4899 m_selectingBottomRight
= wxGridNoCellCoords
;
4902 // Show the edit control, if it has been hidden for
4904 ShowCellEditControl();
4906 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4908 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4909 DoEndDragResizeRow();
4911 // Note: we are ending the event *after* doing
4912 // default processing in this case
4914 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4916 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4918 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4919 DoEndDragResizeCol();
4921 // Note: we are ending the event *after* doing
4922 // default processing in this case
4924 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4931 // ------------ Right button down
4933 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4935 DisableCellEditControl();
4936 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
4941 // no default action at the moment
4946 // ------------ Right double click
4948 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
4950 DisableCellEditControl();
4951 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
4956 // no default action at the moment
4960 // ------------ Moving and no button action
4962 else if ( event
.Moving() && !event
.IsButton() )
4964 int dragRow
= YToEdgeOfRow( y
);
4965 int dragCol
= XToEdgeOfCol( x
);
4967 // Dragging on the corner of a cell to resize in both
4968 // directions is not implemented yet...
4970 if ( dragRow
>= 0 && dragCol
>= 0 )
4972 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4978 m_dragRowOrCol
= dragRow
;
4980 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4982 if ( CanDragRowSize() && CanDragGridSize() )
4983 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
4988 m_dragRowOrCol
= dragCol
;
4996 m_dragRowOrCol
= dragCol
;
4998 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5000 if ( CanDragColSize() && CanDragGridSize() )
5001 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5007 // Neither on a row or col edge
5009 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5011 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5017 void wxGrid::DoEndDragResizeRow()
5019 if ( m_dragLastPos
>= 0 )
5021 // erase the last line and resize the row
5023 int cw
, ch
, left
, dummy
;
5024 m_gridWin
->GetClientSize( &cw
, &ch
);
5025 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5027 wxClientDC
dc( m_gridWin
);
5029 dc
.SetLogicalFunction( wxINVERT
);
5030 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5031 HideCellEditControl();
5032 SaveEditControlValue();
5034 int rowTop
= GetRowTop(m_dragRowOrCol
);
5035 SetRowSize( m_dragRowOrCol
,
5036 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5038 if ( !GetBatchCount() )
5040 // Only needed to get the correct rect.y:
5041 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5043 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5044 rect
.width
= m_rowLabelWidth
;
5045 rect
.height
= ch
- rect
.y
;
5046 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5048 m_gridWin
->Refresh( FALSE
, &rect
);
5051 ShowCellEditControl();
5056 void wxGrid::DoEndDragResizeCol()
5058 if ( m_dragLastPos
>= 0 )
5060 // erase the last line and resize the col
5062 int cw
, ch
, dummy
, top
;
5063 m_gridWin
->GetClientSize( &cw
, &ch
);
5064 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5066 wxClientDC
dc( m_gridWin
);
5068 dc
.SetLogicalFunction( wxINVERT
);
5069 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5070 HideCellEditControl();
5071 SaveEditControlValue();
5073 int colLeft
= GetColLeft(m_dragRowOrCol
);
5074 SetColSize( m_dragRowOrCol
,
5075 wxMax( m_dragLastPos
- colLeft
,
5076 GetColMinimalWidth(m_dragRowOrCol
) ) );
5078 if ( !GetBatchCount() )
5080 // Only needed to get the correct rect.x:
5081 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5083 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5084 rect
.width
= cw
- rect
.x
;
5085 rect
.height
= m_colLabelHeight
;
5086 m_colLabelWin
->Refresh( TRUE
, &rect
);
5088 m_gridWin
->Refresh( FALSE
, &rect
);
5091 ShowCellEditControl();
5098 // ------ interaction with data model
5100 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5102 switch ( msg
.GetId() )
5104 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5105 return GetModelValues();
5107 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5108 return SetModelValues();
5110 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5111 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5112 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5113 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5114 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5115 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5116 return Redimension( msg
);
5125 // The behaviour of this function depends on the grid table class
5126 // Clear() function. For the default wxGridStringTable class the
5127 // behavious is to replace all cell contents with wxEmptyString but
5128 // not to change the number of rows or cols.
5130 void wxGrid::ClearGrid()
5134 if (IsCellEditControlEnabled())
5135 DisableCellEditControl();
5138 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5143 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5145 // TODO: something with updateLabels flag
5149 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5155 if (IsCellEditControlEnabled())
5156 DisableCellEditControl();
5158 return m_table
->InsertRows( pos
, numRows
);
5160 // the table will have sent the results of the insert row
5161 // operation to this view object as a grid table message
5167 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5169 // TODO: something with updateLabels flag
5173 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5177 return ( m_table
&& m_table
->AppendRows( numRows
) );
5178 // the table will have sent the results of the append row
5179 // operation to this view object as a grid table message
5183 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5185 // TODO: something with updateLabels flag
5189 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5195 if (IsCellEditControlEnabled())
5196 DisableCellEditControl();
5198 return (m_table
->DeleteRows( pos
, numRows
));
5199 // the table will have sent the results of the delete row
5200 // operation to this view object as a grid table message
5206 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5208 // TODO: something with updateLabels flag
5212 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5218 if (IsCellEditControlEnabled())
5219 DisableCellEditControl();
5221 return m_table
->InsertCols( pos
, numCols
);
5222 // the table will have sent the results of the insert col
5223 // operation to this view object as a grid table message
5229 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5231 // TODO: something with updateLabels flag
5235 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5239 return ( m_table
&& m_table
->AppendCols( numCols
) );
5240 // the table will have sent the results of the append col
5241 // operation to this view object as a grid table message
5245 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5247 // TODO: something with updateLabels flag
5251 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5257 if (IsCellEditControlEnabled())
5258 DisableCellEditControl();
5260 return ( m_table
->DeleteCols( pos
, numCols
) );
5261 // the table will have sent the results of the delete col
5262 // operation to this view object as a grid table message
5270 // ----- event handlers
5273 // Generate a grid event based on a mouse event and
5274 // return the result of ProcessEvent()
5276 bool wxGrid::SendEvent( const wxEventType type
,
5278 wxMouseEvent
& mouseEv
)
5280 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5282 int rowOrCol
= (row
== -1 ? col
: row
);
5284 wxGridSizeEvent
gridEvt( GetId(),
5288 mouseEv
.GetX() + GetColLabelSize(),
5289 mouseEv
.GetY() + GetRowLabelSize(),
5290 mouseEv
.ControlDown(),
5291 mouseEv
.ShiftDown(),
5293 mouseEv
.MetaDown() );
5295 return GetEventHandler()->ProcessEvent(gridEvt
);
5297 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5299 // Right now, it should _never_ end up here!
5300 wxGridRangeSelectEvent
gridEvt( GetId(),
5304 m_selectingBottomRight
,
5306 mouseEv
.ControlDown(),
5307 mouseEv
.ShiftDown(),
5309 mouseEv
.MetaDown() );
5311 return GetEventHandler()->ProcessEvent(gridEvt
);
5315 wxGridEvent
gridEvt( GetId(),
5319 mouseEv
.GetX() + GetColLabelSize(),
5320 mouseEv
.GetY() + GetRowLabelSize(),
5322 mouseEv
.ControlDown(),
5323 mouseEv
.ShiftDown(),
5325 mouseEv
.MetaDown() );
5327 return GetEventHandler()->ProcessEvent(gridEvt
);
5332 // Generate a grid event of specified type and return the result
5333 // of ProcessEvent().
5335 bool wxGrid::SendEvent( const wxEventType type
,
5338 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5340 int rowOrCol
= (row
== -1 ? col
: row
);
5342 wxGridSizeEvent
gridEvt( GetId(),
5347 return GetEventHandler()->ProcessEvent(gridEvt
);
5351 wxGridEvent
gridEvt( GetId(),
5356 return GetEventHandler()->ProcessEvent(gridEvt
);
5361 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5363 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5367 // This is just here to make sure that CalcDimensions gets called when
5368 // the grid view is resized... then the size event is skipped to allow
5369 // the box sizers to handle everything
5371 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5378 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5380 if ( m_inOnKeyDown
)
5382 // shouldn't be here - we are going round in circles...
5384 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5387 m_inOnKeyDown
= TRUE
;
5389 // propagate the event up and see if it gets processed
5391 wxWindow
*parent
= GetParent();
5392 wxKeyEvent
keyEvt( event
);
5393 keyEvt
.SetEventObject( parent
);
5395 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5398 // try local handlers
5400 switch ( event
.KeyCode() )
5403 if ( event
.ControlDown() )
5405 MoveCursorUpBlock( event
.ShiftDown() );
5409 MoveCursorUp( event
.ShiftDown() );
5414 if ( event
.ControlDown() )
5416 MoveCursorDownBlock( event
.ShiftDown() );
5420 MoveCursorDown( event
.ShiftDown() );
5425 if ( event
.ControlDown() )
5427 MoveCursorLeftBlock( event
.ShiftDown() );
5431 MoveCursorLeft( event
.ShiftDown() );
5436 if ( event
.ControlDown() )
5438 MoveCursorRightBlock( event
.ShiftDown() );
5442 MoveCursorRight( event
.ShiftDown() );
5447 case WXK_NUMPAD_ENTER
:
5448 if ( event
.ControlDown() )
5450 event
.Skip(); // to let the edit control have the return
5454 if ( GetGridCursorRow() < GetNumberRows()-1 )
5456 MoveCursorDown( event
.ShiftDown() );
5460 // at the bottom of a column
5461 HideCellEditControl();
5462 SaveEditControlValue();
5472 if (event
.ShiftDown())
5474 if ( GetGridCursorCol() > 0 )
5476 MoveCursorLeft( FALSE
);
5481 HideCellEditControl();
5482 SaveEditControlValue();
5487 if ( GetGridCursorCol() < GetNumberCols()-1 )
5489 MoveCursorRight( FALSE
);
5494 HideCellEditControl();
5495 SaveEditControlValue();
5501 if ( event
.ControlDown() )
5503 MakeCellVisible( 0, 0 );
5504 SetCurrentCell( 0, 0 );
5513 if ( event
.ControlDown() )
5515 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5516 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5533 if ( event
.ControlDown() )
5535 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5536 m_currentCellCoords
.GetCol(),
5537 event
.ControlDown(),
5543 if ( !IsEditable() )
5545 MoveCursorRight( FALSE
);
5548 // Otherwise fall through to default
5551 // is it possible to edit the current cell at all?
5552 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5554 // yes, now check whether the cells editor accepts the key
5555 int row
= m_currentCellCoords
.GetRow();
5556 int col
= m_currentCellCoords
.GetCol();
5557 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5558 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5560 // <F2> is special and will always start editing, for
5561 // other keys - ask the editor itself
5562 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5563 || editor
->IsAcceptedKey(event
) )
5565 EnableCellEditControl();
5566 editor
->StartingKey(event
);
5578 // let others process char events with modifiers or all
5579 // char events for readonly cells
5586 m_inOnKeyDown
= FALSE
;
5589 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5591 // try local handlers
5593 if ( event
.KeyCode() == WXK_SHIFT
)
5595 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5596 m_selectingBottomRight
!= wxGridNoCellCoords
)
5597 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5598 m_selectingTopLeft
.GetCol(),
5599 m_selectingBottomRight
.GetRow(),
5600 m_selectingBottomRight
.GetCol(),
5601 event
.ControlDown(),
5605 m_selectingTopLeft
= wxGridNoCellCoords
;
5606 m_selectingBottomRight
= wxGridNoCellCoords
;
5607 m_selectingKeyboard
= wxGridNoCellCoords
;
5611 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5615 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5617 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5619 // the event has been intercepted - do nothing
5623 wxClientDC
dc(m_gridWin
);
5626 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5628 HideCellEditControl();
5629 DisableCellEditControl();
5631 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5634 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5635 if ( !m_gridLinesEnabled
)
5643 CalcCellsExposed( r
);
5645 // Otherwise refresh redraws the highlight!
5646 m_currentCellCoords
= coords
;
5648 DrawGridCellArea(dc
);
5649 DrawAllGridLines( dc
, r
);
5653 m_currentCellCoords
= coords
;
5655 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5656 DrawCellHighlight(dc
, attr
);
5661 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5664 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5666 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5669 rightCol
= GetNumberCols() - 1;
5671 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5674 bottomRow
= GetNumberRows() - 1;
5676 if ( topRow
> bottomRow
)
5683 if ( leftCol
> rightCol
)
5690 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5691 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5693 if ( m_selectingTopLeft
!= updateTopLeft
||
5694 m_selectingBottomRight
!= updateBottomRight
)
5696 // Compute two optimal update rectangles:
5697 // Either one rectangle is a real subset of the
5698 // other, or they are (almost) disjoint!
5700 bool need_refresh
[4];
5704 need_refresh
[3] = FALSE
;
5707 // Store intermediate values
5708 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5709 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5710 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5711 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5713 // Determine the outer/inner coordinates.
5714 if (oldLeft
> leftCol
)
5720 if (oldTop
> topRow
)
5726 if (oldRight
< rightCol
)
5729 oldRight
= rightCol
;
5732 if (oldBottom
< bottomRow
)
5735 oldBottom
= bottomRow
;
5739 // Now, either the stuff marked old is the outer
5740 // rectangle or we don't have a situation where one
5741 // is contained in the other.
5743 if ( oldLeft
< leftCol
)
5745 need_refresh
[0] = TRUE
;
5746 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5748 wxGridCellCoords ( oldBottom
,
5752 if ( oldTop
< topRow
)
5754 need_refresh
[1] = TRUE
;
5755 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5757 wxGridCellCoords ( topRow
- 1,
5761 if ( oldRight
> rightCol
)
5763 need_refresh
[2] = TRUE
;
5764 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5766 wxGridCellCoords ( oldBottom
,
5770 if ( oldBottom
> bottomRow
)
5772 need_refresh
[3] = TRUE
;
5773 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5775 wxGridCellCoords ( oldBottom
,
5781 m_selectingTopLeft
= updateTopLeft
;
5782 m_selectingBottomRight
= updateBottomRight
;
5784 // various Refresh() calls
5785 for (i
= 0; i
< 4; i
++ )
5786 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5787 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5790 // never generate an event as it will be generated from
5791 // wxGridSelection::SelectBlock!
5792 // (old comment from when this was the body of SelectBlock)
5797 // ------ functions to get/send data (see also public functions)
5800 bool wxGrid::GetModelValues()
5804 // all we need to do is repaint the grid
5806 m_gridWin
->Refresh();
5814 bool wxGrid::SetModelValues()
5820 for ( row
= 0; row
< m_numRows
; row
++ )
5822 for ( col
= 0; col
< m_numCols
; col
++ )
5824 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5836 // Note - this function only draws cells that are in the list of
5837 // exposed cells (usually set from the update region by
5838 // CalcExposedCells)
5840 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5842 if ( !m_numRows
|| !m_numCols
) return;
5845 size_t numCells
= m_cellsExposed
.GetCount();
5847 for ( i
= 0; i
< numCells
; i
++ )
5849 DrawCell( dc
, m_cellsExposed
[i
] );
5854 void wxGrid::DrawGridSpace( wxDC
& dc
)
5857 m_gridWin
->GetClientSize( &cw
, &ch
);
5860 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5862 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5863 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5865 if ( right
> rightCol
|| bottom
> bottomRow
)
5868 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5870 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5871 dc
.SetPen( *wxTRANSPARENT_PEN
);
5873 if ( right
> rightCol
)
5875 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5878 if ( bottom
> bottomRow
)
5880 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5886 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5888 int row
= coords
.GetRow();
5889 int col
= coords
.GetCol();
5891 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5894 // we draw the cell border ourselves
5895 #if !WXGRID_DRAW_LINES
5896 if ( m_gridLinesEnabled
)
5897 DrawCellBorder( dc
, coords
);
5900 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5902 bool isCurrent
= coords
== m_currentCellCoords
;
5904 wxRect rect
= CellToRect( row
, col
);
5906 // if the editor is shown, we should use it and not the renderer
5907 // Note: However, only if it is really _shown_, i.e. not hidden!
5908 if ( isCurrent
&& IsCellEditControlShown() )
5910 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5911 editor
->PaintBackground(rect
, attr
);
5916 // but all the rest is drawn by the cell renderer and hence may be
5918 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5919 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5926 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5928 int row
= m_currentCellCoords
.GetRow();
5929 int col
= m_currentCellCoords
.GetCol();
5931 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5934 wxRect rect
= CellToRect(row
, col
);
5936 // hmmm... what could we do here to show that the cell is disabled?
5937 // for now, I just draw a thinner border than for the other ones, but
5938 // it doesn't look really good
5939 dc
.SetPen(wxPen(m_cellHighlightColour
, attr
->IsReadOnly() ? 1 : 3, wxSOLID
));
5940 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
5942 dc
.DrawRectangle(rect
);
5945 // VZ: my experiments with 3d borders...
5947 // how to properly set colours for arbitrary bg?
5948 wxCoord x1
= rect
.x
,
5950 x2
= rect
.x
+ rect
.width
-1,
5951 y2
= rect
.y
+ rect
.height
-1;
5953 dc
.SetPen(*wxWHITE_PEN
);
5954 dc
.DrawLine(x1
, y1
, x2
, y1
);
5955 dc
.DrawLine(x1
, y1
, x1
, y2
);
5957 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
5958 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
5960 dc
.SetPen(*wxBLACK_PEN
);
5961 dc
.DrawLine(x1
, y2
, x2
, y2
);
5962 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
5967 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
5969 int row
= coords
.GetRow();
5970 int col
= coords
.GetCol();
5971 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5974 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
5976 // right hand border
5978 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
5979 GetColRight(col
), GetRowBottom(row
) );
5983 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
5984 GetColRight(col
), GetRowBottom(row
) );
5987 void wxGrid::DrawHighlight(wxDC
& dc
)
5989 // This if block was previously in wxGrid::OnPaint but that doesn't
5990 // seem to get called under wxGTK - MB
5992 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
5993 m_numRows
&& m_numCols
)
5995 m_currentCellCoords
.Set(0, 0);
5998 if ( IsCellEditControlShown() )
6000 // don't show highlight when the edit control is shown
6004 // if the active cell was repainted, repaint its highlight too because it
6005 // might have been damaged by the grid lines
6006 size_t count
= m_cellsExposed
.GetCount();
6007 for ( size_t n
= 0; n
< count
; n
++ )
6009 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
6011 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6012 DrawCellHighlight(dc
, attr
);
6020 // TODO: remove this ???
6021 // This is used to redraw all grid lines e.g. when the grid line colour
6024 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED_GTK(reg
) )
6026 if ( !m_gridLinesEnabled
||
6028 !m_numCols
) return;
6030 int top
, bottom
, left
, right
;
6032 #if 0 //#ifndef __WXGTK__
6036 m_gridWin
->GetClientSize(&cw
, &ch
);
6038 // virtual coords of visible area
6040 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6041 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6046 reg
.GetBox(x
, y
, w
, h
);
6047 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6048 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6052 m_gridWin
->GetClientSize(&cw
, &ch
);
6053 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6054 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6057 // avoid drawing grid lines past the last row and col
6059 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6060 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6062 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6064 // horizontal grid lines
6067 for ( i
= 0; i
< m_numRows
; i
++ )
6069 int bot
= GetRowBottom(i
) - 1;
6078 dc
.DrawLine( left
, bot
, right
, bot
);
6083 // vertical grid lines
6085 for ( i
= 0; i
< m_numCols
; i
++ )
6087 int colRight
= GetColRight(i
) - 1;
6088 if ( colRight
> right
)
6093 if ( colRight
>= left
)
6095 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6101 void wxGrid::DrawRowLabels( wxDC
& dc
)
6103 if ( !m_numRows
) return;
6106 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6108 for ( i
= 0; i
< numLabels
; i
++ )
6110 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6115 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6117 if ( GetRowHeight(row
) <= 0 )
6120 int rowTop
= GetRowTop(row
),
6121 rowBottom
= GetRowBottom(row
) - 1;
6123 dc
.SetPen( *wxBLACK_PEN
);
6124 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6125 m_rowLabelWidth
-1, rowBottom
);
6127 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6129 dc
.SetPen( *wxWHITE_PEN
);
6130 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6131 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6133 dc
.SetBackgroundMode( wxTRANSPARENT
);
6134 dc
.SetTextForeground( GetLabelTextColour() );
6135 dc
.SetFont( GetLabelFont() );
6138 GetRowLabelAlignment( &hAlign
, &vAlign
);
6142 rect
.SetY( GetRowTop(row
) + 2 );
6143 rect
.SetWidth( m_rowLabelWidth
- 4 );
6144 rect
.SetHeight( GetRowHeight(row
) - 4 );
6145 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6149 void wxGrid::DrawColLabels( wxDC
& dc
)
6151 if ( !m_numCols
) return;
6154 size_t numLabels
= m_colLabelsExposed
.GetCount();
6156 for ( i
= 0; i
< numLabels
; i
++ )
6158 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6163 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6165 if ( GetColWidth(col
) <= 0 )
6168 int colLeft
= GetColLeft(col
),
6169 colRight
= GetColRight(col
) - 1;
6171 dc
.SetPen( *wxBLACK_PEN
);
6172 dc
.DrawLine( colRight
, 0,
6173 colRight
, m_colLabelHeight
-1 );
6175 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6176 colRight
, m_colLabelHeight
-1 );
6178 dc
.SetPen( *wxWHITE_PEN
);
6179 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6180 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6182 dc
.SetBackgroundMode( wxTRANSPARENT
);
6183 dc
.SetTextForeground( GetLabelTextColour() );
6184 dc
.SetFont( GetLabelFont() );
6186 dc
.SetBackgroundMode( wxTRANSPARENT
);
6187 dc
.SetTextForeground( GetLabelTextColour() );
6188 dc
.SetFont( GetLabelFont() );
6191 GetColLabelAlignment( &hAlign
, &vAlign
);
6194 rect
.SetX( colLeft
+ 2 );
6196 rect
.SetWidth( GetColWidth(col
) - 4 );
6197 rect
.SetHeight( m_colLabelHeight
- 4 );
6198 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6202 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6203 const wxString
& value
,
6208 long textWidth
, textHeight
;
6209 long lineWidth
, lineHeight
;
6210 wxArrayString lines
;
6212 dc
.SetClippingRegion( rect
);
6213 StringToLines( value
, lines
);
6214 if ( lines
.GetCount() )
6216 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6217 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6220 switch ( horizAlign
)
6223 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6226 case wxALIGN_CENTRE
:
6227 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6236 switch ( vertAlign
)
6238 case wxALIGN_BOTTOM
:
6239 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6242 case wxALIGN_CENTRE
:
6243 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6252 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6254 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6259 dc
.DestroyClippingRegion();
6263 // Split multi line text up into an array of strings. Any existing
6264 // contents of the string array are preserved.
6266 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6270 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6271 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6273 while ( startPos
< (int)tVal
.Length() )
6275 pos
= tVal
.Mid(startPos
).Find( eol
);
6280 else if ( pos
== 0 )
6282 lines
.Add( wxEmptyString
);
6286 lines
.Add( value
.Mid(startPos
, pos
) );
6290 if ( startPos
< (int)value
.Length() )
6292 lines
.Add( value
.Mid( startPos
) );
6297 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6298 wxArrayString
& lines
,
6299 long *width
, long *height
)
6306 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6308 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6309 w
= wxMax( w
, lineW
);
6318 // ------ Batch processing.
6320 void wxGrid::EndBatch()
6322 if ( m_batchCount
> 0 )
6325 if ( !m_batchCount
)
6328 m_rowLabelWin
->Refresh();
6329 m_colLabelWin
->Refresh();
6330 m_cornerLabelWin
->Refresh();
6331 m_gridWin
->Refresh();
6336 // Use this, rather than wxWindow::Refresh(), to force an immediate
6337 // repainting of the grid. Has no effect if you are already inside a
6338 // BeginBatch / EndBatch block.
6340 void wxGrid::ForceRefresh()
6348 // ------ Edit control functions
6352 void wxGrid::EnableEditing( bool edit
)
6354 // TODO: improve this ?
6356 if ( edit
!= m_editable
)
6358 if(!edit
) EnableCellEditControl(edit
);
6364 void wxGrid::EnableCellEditControl( bool enable
)
6369 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6370 SetCurrentCell( 0, 0 );
6372 if ( enable
!= m_cellEditCtrlEnabled
)
6374 // TODO allow the app to Veto() this event?
6375 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6379 // this should be checked by the caller!
6380 wxASSERT_MSG( CanEnableCellControl(),
6381 _T("can't enable editing for this cell!") );
6383 // do it before ShowCellEditControl()
6384 m_cellEditCtrlEnabled
= enable
;
6386 ShowCellEditControl();
6390 HideCellEditControl();
6391 SaveEditControlValue();
6393 // do it after HideCellEditControl()
6394 m_cellEditCtrlEnabled
= enable
;
6399 bool wxGrid::IsCurrentCellReadOnly() const
6402 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6403 bool readonly
= attr
->IsReadOnly();
6409 bool wxGrid::CanEnableCellControl() const
6411 return m_editable
&& !IsCurrentCellReadOnly();
6414 bool wxGrid::IsCellEditControlEnabled() const
6416 // the cell edit control might be disable for all cells or just for the
6417 // current one if it's read only
6418 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6421 bool wxGrid::IsCellEditControlShown() const
6423 bool isShown
= FALSE
;
6425 if ( m_cellEditCtrlEnabled
)
6427 int row
= m_currentCellCoords
.GetRow();
6428 int col
= m_currentCellCoords
.GetCol();
6429 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6430 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6435 if ( editor
->IsCreated() )
6437 isShown
= editor
->GetControl()->IsShown();
6447 void wxGrid::ShowCellEditControl()
6449 if ( IsCellEditControlEnabled() )
6451 if ( !IsVisible( m_currentCellCoords
) )
6453 m_cellEditCtrlEnabled
= FALSE
;
6458 wxRect rect
= CellToRect( m_currentCellCoords
);
6459 int row
= m_currentCellCoords
.GetRow();
6460 int col
= m_currentCellCoords
.GetCol();
6462 // convert to scrolled coords
6464 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6466 // done in PaintBackground()
6468 // erase the highlight and the cell contents because the editor
6469 // might not cover the entire cell
6470 wxClientDC
dc( m_gridWin
);
6472 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6473 dc
.SetPen(*wxTRANSPARENT_PEN
);
6474 dc
.DrawRectangle(rect
);
6477 // cell is shifted by one pixel
6481 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6482 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6483 if ( !editor
->IsCreated() )
6485 editor
->Create(m_gridWin
, -1,
6486 new wxGridCellEditorEvtHandler(this, editor
));
6489 editor
->Show( TRUE
, attr
);
6491 editor
->SetSize( rect
);
6493 editor
->BeginEdit(row
, col
, this);
6502 void wxGrid::HideCellEditControl()
6504 if ( IsCellEditControlEnabled() )
6506 int row
= m_currentCellCoords
.GetRow();
6507 int col
= m_currentCellCoords
.GetCol();
6509 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6510 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6511 editor
->Show( FALSE
);
6514 m_gridWin
->SetFocus();
6515 wxRect
rect( CellToRect( row
, col
) );
6516 m_gridWin
->Refresh( FALSE
, &rect
);
6521 void wxGrid::SaveEditControlValue()
6523 if ( IsCellEditControlEnabled() )
6525 int row
= m_currentCellCoords
.GetRow();
6526 int col
= m_currentCellCoords
.GetCol();
6528 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6529 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6530 bool changed
= editor
->EndEdit(row
, col
, this);
6537 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6538 m_currentCellCoords
.GetRow(),
6539 m_currentCellCoords
.GetCol() );
6546 // ------ Grid location functions
6547 // Note that all of these functions work with the logical coordinates of
6548 // grid cells and labels so you will need to convert from device
6549 // coordinates for mouse events etc.
6552 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6554 int row
= YToRow(y
);
6555 int col
= XToCol(x
);
6557 if ( row
== -1 || col
== -1 )
6559 coords
= wxGridNoCellCoords
;
6563 coords
.Set( row
, col
);
6568 int wxGrid::YToRow( int y
)
6572 for ( i
= 0; i
< m_numRows
; i
++ )
6574 if ( y
< GetRowBottom(i
) )
6582 int wxGrid::XToCol( int x
)
6586 for ( i
= 0; i
< m_numCols
; i
++ )
6588 if ( x
< GetColRight(i
) )
6596 // return the row number that that the y coord is near the edge of, or
6597 // -1 if not near an edge
6599 int wxGrid::YToEdgeOfRow( int y
)
6603 for ( i
= 0; i
< m_numRows
; i
++ )
6605 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6607 d
= abs( y
- GetRowBottom(i
) );
6608 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6617 // return the col number that that the x coord is near the edge of, or
6618 // -1 if not near an edge
6620 int wxGrid::XToEdgeOfCol( int x
)
6624 for ( i
= 0; i
< m_numCols
; i
++ )
6626 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6628 d
= abs( x
- GetColRight(i
) );
6629 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6638 wxRect
wxGrid::CellToRect( int row
, int col
)
6640 wxRect
rect( -1, -1, -1, -1 );
6642 if ( row
>= 0 && row
< m_numRows
&&
6643 col
>= 0 && col
< m_numCols
)
6645 rect
.x
= GetColLeft(col
);
6646 rect
.y
= GetRowTop(row
);
6647 rect
.width
= GetColWidth(col
);
6648 rect
.height
= GetRowHeight(row
);
6651 // if grid lines are enabled, then the area of the cell is a bit smaller
6652 if (m_gridLinesEnabled
) {
6660 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6662 // get the cell rectangle in logical coords
6664 wxRect
r( CellToRect( row
, col
) );
6666 // convert to device coords
6668 int left
, top
, right
, bottom
;
6669 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6670 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6672 // check against the client area of the grid window
6675 m_gridWin
->GetClientSize( &cw
, &ch
);
6677 if ( wholeCellVisible
)
6679 // is the cell wholly visible ?
6681 return ( left
>= 0 && right
<= cw
&&
6682 top
>= 0 && bottom
<= ch
);
6686 // is the cell partly visible ?
6688 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6689 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6694 // make the specified cell location visible by doing a minimal amount
6697 void wxGrid::MakeCellVisible( int row
, int col
)
6700 int xpos
= -1, ypos
= -1;
6702 if ( row
>= 0 && row
< m_numRows
&&
6703 col
>= 0 && col
< m_numCols
)
6705 // get the cell rectangle in logical coords
6707 wxRect
r( CellToRect( row
, col
) );
6709 // convert to device coords
6711 int left
, top
, right
, bottom
;
6712 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6713 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6716 m_gridWin
->GetClientSize( &cw
, &ch
);
6722 else if ( bottom
> ch
)
6724 int h
= r
.GetHeight();
6726 for ( i
= row
-1; i
>= 0; i
-- )
6728 int rowHeight
= GetRowHeight(i
);
6729 if ( h
+ rowHeight
> ch
)
6736 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6737 // have rounding errors (this is important, because if we do, we
6738 // might not scroll at all and some cells won't be redrawn)
6739 ypos
+= GRID_SCROLL_LINE
/ 2;
6746 else if ( right
> cw
)
6748 int w
= r
.GetWidth();
6750 for ( i
= col
-1; i
>= 0; i
-- )
6752 int colWidth
= GetColWidth(i
);
6753 if ( w
+ colWidth
> cw
)
6760 // see comment for ypos above
6761 xpos
+= GRID_SCROLL_LINE
/ 2;
6764 if ( xpos
!= -1 || ypos
!= -1 )
6766 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6767 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6768 Scroll( xpos
, ypos
);
6776 // ------ Grid cursor movement functions
6779 bool wxGrid::MoveCursorUp( bool expandSelection
)
6781 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6782 m_currentCellCoords
.GetRow() >= 0 )
6784 if ( expandSelection
)
6786 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6787 m_selectingKeyboard
= m_currentCellCoords
;
6788 if ( m_selectingKeyboard
.GetRow() > 0 )
6790 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6791 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6792 m_selectingKeyboard
.GetCol() );
6793 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6796 else if ( m_currentCellCoords
.GetRow() > 0 )
6799 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6800 m_currentCellCoords
.GetCol() );
6801 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6802 m_currentCellCoords
.GetCol() );
6813 bool wxGrid::MoveCursorDown( bool expandSelection
)
6815 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6816 m_currentCellCoords
.GetRow() < m_numRows
)
6818 if ( expandSelection
)
6820 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6821 m_selectingKeyboard
= m_currentCellCoords
;
6822 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6824 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6825 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6826 m_selectingKeyboard
.GetCol() );
6827 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6830 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6833 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6834 m_currentCellCoords
.GetCol() );
6835 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6836 m_currentCellCoords
.GetCol() );
6847 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6849 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6850 m_currentCellCoords
.GetCol() >= 0 )
6852 if ( expandSelection
)
6854 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6855 m_selectingKeyboard
= m_currentCellCoords
;
6856 if ( m_selectingKeyboard
.GetCol() > 0 )
6858 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6859 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6860 m_selectingKeyboard
.GetCol() );
6861 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6864 else if ( m_currentCellCoords
.GetCol() > 0 )
6867 MakeCellVisible( m_currentCellCoords
.GetRow(),
6868 m_currentCellCoords
.GetCol() - 1 );
6869 SetCurrentCell( m_currentCellCoords
.GetRow(),
6870 m_currentCellCoords
.GetCol() - 1 );
6881 bool wxGrid::MoveCursorRight( bool expandSelection
)
6883 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6884 m_currentCellCoords
.GetCol() < m_numCols
)
6886 if ( expandSelection
)
6888 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6889 m_selectingKeyboard
= m_currentCellCoords
;
6890 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6892 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6893 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6894 m_selectingKeyboard
.GetCol() );
6895 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6898 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6901 MakeCellVisible( m_currentCellCoords
.GetRow(),
6902 m_currentCellCoords
.GetCol() + 1 );
6903 SetCurrentCell( m_currentCellCoords
.GetRow(),
6904 m_currentCellCoords
.GetCol() + 1 );
6915 bool wxGrid::MovePageUp()
6917 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6919 int row
= m_currentCellCoords
.GetRow();
6923 m_gridWin
->GetClientSize( &cw
, &ch
);
6925 int y
= GetRowTop(row
);
6926 int newRow
= YToRow( y
- ch
+ 1 );
6931 else if ( newRow
== row
)
6936 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6937 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6945 bool wxGrid::MovePageDown()
6947 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6949 int row
= m_currentCellCoords
.GetRow();
6950 if ( row
< m_numRows
)
6953 m_gridWin
->GetClientSize( &cw
, &ch
);
6955 int y
= GetRowTop(row
);
6956 int newRow
= YToRow( y
+ ch
);
6959 newRow
= m_numRows
- 1;
6961 else if ( newRow
== row
)
6966 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
6967 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
6975 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
6978 m_currentCellCoords
!= wxGridNoCellCoords
&&
6979 m_currentCellCoords
.GetRow() > 0 )
6981 int row
= m_currentCellCoords
.GetRow();
6982 int col
= m_currentCellCoords
.GetCol();
6984 if ( m_table
->IsEmptyCell(row
, col
) )
6986 // starting in an empty cell: find the next block of
6992 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
6995 else if ( m_table
->IsEmptyCell(row
-1, col
) )
6997 // starting at the top of a block: find the next block
7003 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7008 // starting within a block: find the top of the block
7013 if ( m_table
->IsEmptyCell(row
, col
) )
7021 MakeCellVisible( row
, col
);
7022 if ( expandSelection
)
7024 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7025 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7030 SetCurrentCell( row
, col
);
7038 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7041 m_currentCellCoords
!= wxGridNoCellCoords
&&
7042 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7044 int row
= m_currentCellCoords
.GetRow();
7045 int col
= m_currentCellCoords
.GetCol();
7047 if ( m_table
->IsEmptyCell(row
, col
) )
7049 // starting in an empty cell: find the next block of
7052 while ( row
< m_numRows
-1 )
7055 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7058 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7060 // starting at the bottom of a block: find the next block
7063 while ( row
< m_numRows
-1 )
7066 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7071 // starting within a block: find the bottom of the block
7073 while ( row
< m_numRows
-1 )
7076 if ( m_table
->IsEmptyCell(row
, col
) )
7084 MakeCellVisible( row
, col
);
7085 if ( expandSelection
)
7087 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7088 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7093 SetCurrentCell( row
, col
);
7102 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7105 m_currentCellCoords
!= wxGridNoCellCoords
&&
7106 m_currentCellCoords
.GetCol() > 0 )
7108 int row
= m_currentCellCoords
.GetRow();
7109 int col
= m_currentCellCoords
.GetCol();
7111 if ( m_table
->IsEmptyCell(row
, col
) )
7113 // starting in an empty cell: find the next block of
7119 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7122 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7124 // starting at the left of a block: find the next block
7130 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7135 // starting within a block: find the left of the block
7140 if ( m_table
->IsEmptyCell(row
, col
) )
7148 MakeCellVisible( row
, col
);
7149 if ( expandSelection
)
7151 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7152 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7157 SetCurrentCell( row
, col
);
7166 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7169 m_currentCellCoords
!= wxGridNoCellCoords
&&
7170 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7172 int row
= m_currentCellCoords
.GetRow();
7173 int col
= m_currentCellCoords
.GetCol();
7175 if ( m_table
->IsEmptyCell(row
, col
) )
7177 // starting in an empty cell: find the next block of
7180 while ( col
< m_numCols
-1 )
7183 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7186 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7188 // starting at the right of a block: find the next block
7191 while ( col
< m_numCols
-1 )
7194 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7199 // starting within a block: find the right of the block
7201 while ( col
< m_numCols
-1 )
7204 if ( m_table
->IsEmptyCell(row
, col
) )
7212 MakeCellVisible( row
, col
);
7213 if ( expandSelection
)
7215 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7216 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7221 SetCurrentCell( row
, col
);
7233 // ------ Label values and formatting
7236 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7238 *horiz
= m_rowLabelHorizAlign
;
7239 *vert
= m_rowLabelVertAlign
;
7242 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7244 *horiz
= m_colLabelHorizAlign
;
7245 *vert
= m_colLabelVertAlign
;
7248 wxString
wxGrid::GetRowLabelValue( int row
)
7252 return m_table
->GetRowLabelValue( row
);
7262 wxString
wxGrid::GetColLabelValue( int col
)
7266 return m_table
->GetColLabelValue( col
);
7277 void wxGrid::SetRowLabelSize( int width
)
7279 width
= wxMax( width
, 0 );
7280 if ( width
!= m_rowLabelWidth
)
7284 m_rowLabelWin
->Show( FALSE
);
7285 m_cornerLabelWin
->Show( FALSE
);
7287 else if ( m_rowLabelWidth
== 0 )
7289 m_rowLabelWin
->Show( TRUE
);
7290 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7293 m_rowLabelWidth
= width
;
7300 void wxGrid::SetColLabelSize( int height
)
7302 height
= wxMax( height
, 0 );
7303 if ( height
!= m_colLabelHeight
)
7307 m_colLabelWin
->Show( FALSE
);
7308 m_cornerLabelWin
->Show( FALSE
);
7310 else if ( m_colLabelHeight
== 0 )
7312 m_colLabelWin
->Show( TRUE
);
7313 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7316 m_colLabelHeight
= height
;
7323 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7325 if ( m_labelBackgroundColour
!= colour
)
7327 m_labelBackgroundColour
= colour
;
7328 m_rowLabelWin
->SetBackgroundColour( colour
);
7329 m_colLabelWin
->SetBackgroundColour( colour
);
7330 m_cornerLabelWin
->SetBackgroundColour( colour
);
7332 if ( !GetBatchCount() )
7334 m_rowLabelWin
->Refresh();
7335 m_colLabelWin
->Refresh();
7336 m_cornerLabelWin
->Refresh();
7341 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7343 if ( m_labelTextColour
!= colour
)
7345 m_labelTextColour
= colour
;
7346 if ( !GetBatchCount() )
7348 m_rowLabelWin
->Refresh();
7349 m_colLabelWin
->Refresh();
7354 void wxGrid::SetLabelFont( const wxFont
& font
)
7357 if ( !GetBatchCount() )
7359 m_rowLabelWin
->Refresh();
7360 m_colLabelWin
->Refresh();
7364 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7366 // allow old (incorrect) defs to be used
7369 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7370 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7371 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7376 case wxTOP
: vert
= wxALIGN_TOP
; break;
7377 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7378 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7381 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7383 m_rowLabelHorizAlign
= horiz
;
7386 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7388 m_rowLabelVertAlign
= vert
;
7391 if ( !GetBatchCount() )
7393 m_rowLabelWin
->Refresh();
7397 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7399 // allow old (incorrect) defs to be used
7402 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7403 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7404 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7409 case wxTOP
: vert
= wxALIGN_TOP
; break;
7410 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7411 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7414 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7416 m_colLabelHorizAlign
= horiz
;
7419 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7421 m_colLabelVertAlign
= vert
;
7424 if ( !GetBatchCount() )
7426 m_colLabelWin
->Refresh();
7430 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7434 m_table
->SetRowLabelValue( row
, s
);
7435 if ( !GetBatchCount() )
7437 wxRect rect
= CellToRect( row
, 0);
7438 if ( rect
.height
> 0 )
7440 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7442 rect
.width
= m_rowLabelWidth
;
7443 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7449 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7453 m_table
->SetColLabelValue( col
, s
);
7454 if ( !GetBatchCount() )
7456 wxRect rect
= CellToRect( 0, col
);
7457 if ( rect
.width
> 0 )
7459 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7461 rect
.height
= m_colLabelHeight
;
7462 m_colLabelWin
->Refresh( TRUE
, &rect
);
7468 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7470 if ( m_gridLineColour
!= colour
)
7472 m_gridLineColour
= colour
;
7474 wxClientDC
dc( m_gridWin
);
7476 DrawAllGridLines( dc
, wxRegion() );
7481 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7483 if ( m_cellHighlightColour
!= colour
)
7485 m_cellHighlightColour
= colour
;
7487 wxClientDC
dc( m_gridWin
);
7489 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7490 DrawCellHighlight(dc
, attr
);
7495 void wxGrid::EnableGridLines( bool enable
)
7497 if ( enable
!= m_gridLinesEnabled
)
7499 m_gridLinesEnabled
= enable
;
7501 if ( !GetBatchCount() )
7505 wxClientDC
dc( m_gridWin
);
7507 DrawAllGridLines( dc
, wxRegion() );
7511 m_gridWin
->Refresh();
7518 int wxGrid::GetDefaultRowSize()
7520 return m_defaultRowHeight
;
7523 int wxGrid::GetRowSize( int row
)
7525 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7527 return GetRowHeight(row
);
7530 int wxGrid::GetDefaultColSize()
7532 return m_defaultColWidth
;
7535 int wxGrid::GetColSize( int col
)
7537 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7539 return GetColWidth(col
);
7542 // ============================================================================
7543 // access to the grid attributes: each of them has a default value in the grid
7544 // itself and may be overidden on a per-cell basis
7545 // ============================================================================
7547 // ----------------------------------------------------------------------------
7548 // setting default attributes
7549 // ----------------------------------------------------------------------------
7551 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7553 m_defaultCellAttr
->SetBackgroundColour(col
);
7555 m_gridWin
->SetBackgroundColour(col
);
7559 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7561 m_defaultCellAttr
->SetTextColour(col
);
7564 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7566 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7569 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7571 m_defaultCellAttr
->SetFont(font
);
7574 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7576 m_defaultCellAttr
->SetRenderer(renderer
);
7579 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7581 m_defaultCellAttr
->SetEditor(editor
);
7584 // ----------------------------------------------------------------------------
7585 // access to the default attrbiutes
7586 // ----------------------------------------------------------------------------
7588 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7590 return m_defaultCellAttr
->GetBackgroundColour();
7593 wxColour
wxGrid::GetDefaultCellTextColour()
7595 return m_defaultCellAttr
->GetTextColour();
7598 wxFont
wxGrid::GetDefaultCellFont()
7600 return m_defaultCellAttr
->GetFont();
7603 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7605 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7608 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7610 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7613 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7615 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7618 // ----------------------------------------------------------------------------
7619 // access to cell attributes
7620 // ----------------------------------------------------------------------------
7622 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7624 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7625 wxColour colour
= attr
->GetBackgroundColour();
7630 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7632 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7633 wxColour colour
= attr
->GetTextColour();
7638 wxFont
wxGrid::GetCellFont( int row
, int col
)
7640 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7641 wxFont font
= attr
->GetFont();
7646 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7648 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7649 attr
->GetAlignment(horiz
, vert
);
7653 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7655 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7656 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7662 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7664 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7665 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7671 bool wxGrid::IsReadOnly(int row
, int col
) const
7673 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7674 bool isReadOnly
= attr
->IsReadOnly();
7679 // ----------------------------------------------------------------------------
7680 // attribute support: cache, automatic provider creation, ...
7681 // ----------------------------------------------------------------------------
7683 bool wxGrid::CanHaveAttributes()
7690 return m_table
->CanHaveAttributes();
7693 void wxGrid::ClearAttrCache()
7695 if ( m_attrCache
.row
!= -1 )
7697 wxSafeDecRef(m_attrCache
.attr
);
7698 m_attrCache
.row
= -1;
7702 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7704 wxGrid
*self
= (wxGrid
*)this; // const_cast
7706 self
->ClearAttrCache();
7707 self
->m_attrCache
.row
= row
;
7708 self
->m_attrCache
.col
= col
;
7709 self
->m_attrCache
.attr
= attr
;
7713 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7715 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7717 *attr
= m_attrCache
.attr
;
7718 wxSafeIncRef(m_attrCache
.attr
);
7720 #ifdef DEBUG_ATTR_CACHE
7721 gs_nAttrCacheHits
++;
7728 #ifdef DEBUG_ATTR_CACHE
7729 gs_nAttrCacheMisses
++;
7735 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7737 wxGridCellAttr
*attr
;
7738 if ( !LookupAttr(row
, col
, &attr
) )
7740 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7741 CacheAttr(row
, col
, attr
);
7745 attr
->SetDefAttr(m_defaultCellAttr
);
7749 attr
= m_defaultCellAttr
;
7756 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7758 wxGridCellAttr
*attr
;
7759 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7761 wxASSERT_MSG( m_table
,
7762 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7764 attr
= m_table
->GetAttr(row
, col
);
7767 attr
= new wxGridCellAttr
;
7769 // artificially inc the ref count to match DecRef() in caller
7772 m_table
->SetAttr(attr
, row
, col
);
7775 CacheAttr(row
, col
, attr
);
7777 attr
->SetDefAttr(m_defaultCellAttr
);
7781 // ----------------------------------------------------------------------------
7782 // setting column attributes (wrappers around SetColAttr)
7783 // ----------------------------------------------------------------------------
7785 void wxGrid::SetColFormatBool(int col
)
7787 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7790 void wxGrid::SetColFormatNumber(int col
)
7792 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7795 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7797 wxString typeName
= wxGRID_VALUE_FLOAT
;
7798 if ( (width
!= -1) || (precision
!= -1) )
7800 typeName
<< _T(':') << width
<< _T(',') << precision
;
7803 SetColFormatCustom(col
, typeName
);
7806 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7808 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7809 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7810 attr
->SetRenderer(renderer
);
7812 SetColAttr(col
, attr
);
7815 // ----------------------------------------------------------------------------
7816 // setting cell attributes: this is forwarded to the table
7817 // ----------------------------------------------------------------------------
7819 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7821 if ( CanHaveAttributes() )
7823 m_table
->SetRowAttr(attr
, row
);
7831 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7833 if ( CanHaveAttributes() )
7835 m_table
->SetColAttr(attr
, col
);
7843 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7845 if ( CanHaveAttributes() )
7847 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7848 attr
->SetBackgroundColour(colour
);
7853 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7855 if ( CanHaveAttributes() )
7857 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7858 attr
->SetTextColour(colour
);
7863 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7865 if ( CanHaveAttributes() )
7867 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7868 attr
->SetFont(font
);
7873 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7875 if ( CanHaveAttributes() )
7877 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7878 attr
->SetAlignment(horiz
, vert
);
7883 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7885 if ( CanHaveAttributes() )
7887 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7888 attr
->SetRenderer(renderer
);
7893 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
7895 if ( CanHaveAttributes() )
7897 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7898 attr
->SetEditor(editor
);
7903 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
7905 if ( CanHaveAttributes() )
7907 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7908 attr
->SetReadOnly(isReadOnly
);
7913 // ----------------------------------------------------------------------------
7914 // Data type registration
7915 // ----------------------------------------------------------------------------
7917 void wxGrid::RegisterDataType(const wxString
& typeName
,
7918 wxGridCellRenderer
* renderer
,
7919 wxGridCellEditor
* editor
)
7921 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
7925 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
7927 wxString typeName
= m_table
->GetTypeName(row
, col
);
7928 return GetDefaultEditorForType(typeName
);
7931 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
7933 wxString typeName
= m_table
->GetTypeName(row
, col
);
7934 return GetDefaultRendererForType(typeName
);
7938 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
7940 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7941 if ( index
== wxNOT_FOUND
)
7943 wxFAIL_MSG(wxT("Unknown data type name"));
7948 return m_typeRegistry
->GetEditor(index
);
7952 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
7954 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
7955 if ( index
== wxNOT_FOUND
)
7957 wxFAIL_MSG(wxT("Unknown data type name"));
7962 return m_typeRegistry
->GetRenderer(index
);
7966 // ----------------------------------------------------------------------------
7968 // ----------------------------------------------------------------------------
7970 void wxGrid::EnableDragRowSize( bool enable
)
7972 m_canDragRowSize
= enable
;
7976 void wxGrid::EnableDragColSize( bool enable
)
7978 m_canDragColSize
= enable
;
7981 void wxGrid::EnableDragGridSize( bool enable
)
7983 m_canDragGridSize
= enable
;
7987 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
7989 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
7991 if ( resizeExistingRows
)
7999 void wxGrid::SetRowSize( int row
, int height
)
8001 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8003 if ( m_rowHeights
.IsEmpty() )
8005 // need to really create the array
8009 int h
= wxMax( 0, height
);
8010 int diff
= h
- m_rowHeights
[row
];
8012 m_rowHeights
[row
] = h
;
8014 for ( i
= row
; i
< m_numRows
; i
++ )
8016 m_rowBottoms
[i
] += diff
;
8021 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8023 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8025 if ( resizeExistingCols
)
8033 void wxGrid::SetColSize( int col
, int width
)
8035 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8037 // should we check that it's bigger than GetColMinimalWidth(col) here?
8039 if ( m_colWidths
.IsEmpty() )
8041 // need to really create the array
8045 int w
= wxMax( 0, width
);
8046 int diff
= w
- m_colWidths
[col
];
8047 m_colWidths
[col
] = w
;
8050 for ( i
= col
; i
< m_numCols
; i
++ )
8052 m_colRights
[i
] += diff
;
8058 void wxGrid::SetColMinimalWidth( int col
, int width
)
8060 m_colMinWidths
.Put(col
, width
);
8063 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8065 m_rowMinHeights
.Put(row
, width
);
8068 int wxGrid::GetColMinimalWidth(int col
) const
8070 long value
= m_colMinWidths
.Get(col
);
8071 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8074 int wxGrid::GetRowMinimalHeight(int row
) const
8076 long value
= m_rowMinHeights
.Get(row
);
8077 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8080 // ----------------------------------------------------------------------------
8082 // ----------------------------------------------------------------------------
8084 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8086 wxClientDC
dc(m_gridWin
);
8088 // init both of them to avoid compiler warnings, even if weo nly need one
8096 wxCoord extent
, extentMax
= 0;
8097 int max
= column
? m_numRows
: m_numCols
;
8098 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8105 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8106 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8109 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8110 extent
= column
? size
.x
: size
.y
;
8111 if ( extent
> extentMax
)
8122 // now also compare with the column label extent
8124 dc
.SetFont( GetLabelFont() );
8127 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8129 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8131 extent
= column
? w
: h
;
8132 if ( extent
> extentMax
)
8139 // empty column - give default extent (notice that if extentMax is less
8140 // than default extent but != 0, it's ok)
8141 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8147 // leave some space around text
8157 SetColSize(col
, extentMax
);
8159 SetRowSize(row
, extentMax
);
8164 SetColMinimalWidth(col
, extentMax
);
8166 SetRowMinimalHeight(row
, extentMax
);
8170 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8172 int width
= m_rowLabelWidth
;
8174 for ( int col
= 0; col
< m_numCols
; col
++ )
8178 AutoSizeColumn(col
, setAsMin
);
8181 width
+= GetColWidth(col
);
8187 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8189 int height
= m_colLabelHeight
;
8191 for ( int row
= 0; row
< m_numRows
; row
++ )
8195 AutoSizeRow(row
, setAsMin
);
8198 height
+= GetRowHeight(row
);
8204 void wxGrid::AutoSize()
8207 SetSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8210 wxSize
wxGrid::DoGetBestSize() const
8212 // don't set sizes, only calculate them
8213 wxGrid
*self
= (wxGrid
*)this; // const_cast
8215 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8216 self
->SetOrCalcRowSizes(TRUE
));
8225 wxPen
& wxGrid::GetDividerPen() const
8230 // ----------------------------------------------------------------------------
8231 // cell value accessor functions
8232 // ----------------------------------------------------------------------------
8234 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8238 m_table
->SetValue( row
, col
, s
);
8239 if ( !GetBatchCount() )
8241 wxClientDC
dc( m_gridWin
);
8243 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8246 if ( m_currentCellCoords
.GetRow() == row
&&
8247 m_currentCellCoords
.GetCol() == col
&&
8248 IsCellEditControlShown())
8249 // Note: If we are using IsCellEditControlEnabled,
8250 // this interacts badly with calling SetCellValue from
8251 // an EVT_GRID_CELL_CHANGE handler.
8253 HideCellEditControl();
8254 ShowCellEditControl(); // will reread data from table
8261 // ------ Block, row and col selection
8264 void wxGrid::SelectRow( int row
, bool addToSelected
)
8266 if ( IsSelection() && !addToSelected
)
8269 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8273 void wxGrid::SelectCol( int col
, bool addToSelected
)
8275 if ( IsSelection() && !addToSelected
)
8278 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8282 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8283 bool addToSelected
)
8285 if ( IsSelection() && !addToSelected
)
8288 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8289 FALSE
, addToSelected
);
8293 void wxGrid::SelectAll()
8295 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8299 // ------ Cell, row and col deselection
8302 void wxGrid::DeselectRow( int row
)
8304 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8306 if ( m_selection
->IsInSelection(row
, 0 ) )
8307 m_selection
->ToggleCellSelection( row
, 0);
8311 int nCols
= GetNumberCols();
8312 for ( int i
= 0; i
< nCols
; i
++ )
8314 if ( m_selection
->IsInSelection(row
, i
) )
8315 m_selection
->ToggleCellSelection( row
, i
);
8320 void wxGrid::DeselectCol( int col
)
8322 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8324 if ( m_selection
->IsInSelection(0, col
) )
8325 m_selection
->ToggleCellSelection( 0, col
);
8329 int nRows
= GetNumberRows();
8330 for ( int i
= 0; i
< nRows
; i
++ )
8332 if ( m_selection
->IsInSelection(i
, col
) )
8333 m_selection
->ToggleCellSelection(i
, col
);
8338 void wxGrid::DeselectCell( int row
, int col
)
8340 if ( m_selection
->IsInSelection(row
, col
) )
8341 m_selection
->ToggleCellSelection(row
, col
);
8344 bool wxGrid::IsSelection()
8346 return ( m_selection
->IsSelection() ||
8347 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8348 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8351 bool wxGrid::IsInSelection( int row
, int col
)
8353 return ( m_selection
->IsInSelection( row
, col
) ||
8354 ( row
>= m_selectingTopLeft
.GetRow() &&
8355 col
>= m_selectingTopLeft
.GetCol() &&
8356 row
<= m_selectingBottomRight
.GetRow() &&
8357 col
<= m_selectingBottomRight
.GetCol() ) );
8360 void wxGrid::ClearSelection()
8362 m_selectingTopLeft
= wxGridNoCellCoords
;
8363 m_selectingBottomRight
= wxGridNoCellCoords
;
8364 m_selection
->ClearSelection();
8368 // This function returns the rectangle that encloses the given block
8369 // in device coords clipped to the client size of the grid window.
8371 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8372 const wxGridCellCoords
&bottomRight
)
8374 wxRect
rect( wxGridNoCellRect
);
8377 cellRect
= CellToRect( topLeft
);
8378 if ( cellRect
!= wxGridNoCellRect
)
8384 rect
= wxRect( 0, 0, 0, 0 );
8387 cellRect
= CellToRect( bottomRight
);
8388 if ( cellRect
!= wxGridNoCellRect
)
8394 return wxGridNoCellRect
;
8397 // convert to scrolled coords
8399 int left
, top
, right
, bottom
;
8400 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8401 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8404 m_gridWin
->GetClientSize( &cw
, &ch
);
8406 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8407 return wxRect( 0, 0, 0, 0);
8409 rect
.SetLeft( wxMax(0, left
) );
8410 rect
.SetTop( wxMax(0, top
) );
8411 rect
.SetRight( wxMin(cw
, right
) );
8412 rect
.SetBottom( wxMin(ch
, bottom
) );
8420 // ------ Grid event classes
8423 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8425 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8426 int row
, int col
, int x
, int y
, bool sel
,
8427 bool control
, bool shift
, bool alt
, bool meta
)
8428 : wxNotifyEvent( type
, id
)
8435 m_control
= control
;
8440 SetEventObject(obj
);
8444 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8446 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8447 int rowOrCol
, int x
, int y
,
8448 bool control
, bool shift
, bool alt
, bool meta
)
8449 : wxNotifyEvent( type
, id
)
8451 m_rowOrCol
= rowOrCol
;
8454 m_control
= control
;
8459 SetEventObject(obj
);
8463 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8465 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8466 const wxGridCellCoords
& topLeft
,
8467 const wxGridCellCoords
& bottomRight
,
8468 bool sel
, bool control
,
8469 bool shift
, bool alt
, bool meta
)
8470 : wxNotifyEvent( type
, id
)
8472 m_topLeft
= topLeft
;
8473 m_bottomRight
= bottomRight
;
8475 m_control
= control
;
8480 SetEventObject(obj
);
8484 #endif // ifndef wxUSE_NEW_GRID