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 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
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 OnMouseWheel( wxMouseEvent
& event
);
136 void OnKeyDown( wxKeyEvent
& event
);
137 void OnKeyUp( wxKeyEvent
& );
139 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
140 DECLARE_EVENT_TABLE()
144 class WXDLLEXPORT wxGridColLabelWindow
: public wxWindow
147 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
148 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
149 const wxPoint
&pos
, const wxSize
&size
);
154 void OnPaint( wxPaintEvent
&event
);
155 void OnMouseEvent( wxMouseEvent
& event
);
156 void OnMouseWheel( wxMouseEvent
& event
);
157 void OnKeyDown( wxKeyEvent
& event
);
158 void OnKeyUp( wxKeyEvent
& );
160 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
161 DECLARE_EVENT_TABLE()
165 class WXDLLEXPORT wxGridCornerLabelWindow
: public wxWindow
168 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
169 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
170 const wxPoint
&pos
, const wxSize
&size
);
175 void OnMouseEvent( wxMouseEvent
& event
);
176 void OnMouseWheel( wxMouseEvent
& event
);
177 void OnKeyDown( wxKeyEvent
& event
);
178 void OnKeyUp( wxKeyEvent
& );
179 void OnPaint( wxPaintEvent
& event
);
181 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
182 DECLARE_EVENT_TABLE()
185 class WXDLLEXPORT wxGridWindow
: public wxPanel
190 m_owner
= (wxGrid
*)NULL
;
191 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
192 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
195 wxGridWindow( wxGrid
*parent
,
196 wxGridRowLabelWindow
*rowLblWin
,
197 wxGridColLabelWindow
*colLblWin
,
198 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
201 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
205 wxGridRowLabelWindow
*m_rowLabelWin
;
206 wxGridColLabelWindow
*m_colLabelWin
;
208 void OnPaint( wxPaintEvent
&event
);
209 void OnMouseWheel( wxMouseEvent
& event
);
210 void OnMouseEvent( wxMouseEvent
& event
);
211 void OnKeyDown( wxKeyEvent
& );
212 void OnKeyUp( wxKeyEvent
& );
213 void OnEraseBackground( wxEraseEvent
& );
216 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
217 DECLARE_EVENT_TABLE()
222 class wxGridCellEditorEvtHandler
: public wxEvtHandler
225 wxGridCellEditorEvtHandler()
226 : m_grid(0), m_editor(0)
228 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
229 : m_grid(grid
), m_editor(editor
)
232 void OnKeyDown(wxKeyEvent
& event
);
233 void OnChar(wxKeyEvent
& event
);
237 wxGridCellEditor
* m_editor
;
238 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
239 DECLARE_EVENT_TABLE()
243 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
244 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
245 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
246 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
251 // ----------------------------------------------------------------------------
252 // the internal data representation used by wxGridCellAttrProvider
253 // ----------------------------------------------------------------------------
255 // this class stores attributes set for cells
256 class WXDLLEXPORT wxGridCellAttrData
259 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
260 wxGridCellAttr
*GetAttr(int row
, int col
) const;
261 void UpdateAttrRows( size_t pos
, int numRows
);
262 void UpdateAttrCols( size_t pos
, int numCols
);
265 // searches for the attr for given cell, returns wxNOT_FOUND if not found
266 int FindIndex(int row
, int col
) const;
268 wxGridCellWithAttrArray m_attrs
;
271 // this class stores attributes set for rows or columns
272 class WXDLLEXPORT wxGridRowOrColAttrData
275 // empty ctor to suppress warnings
276 wxGridRowOrColAttrData() { }
277 ~wxGridRowOrColAttrData();
279 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
280 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
281 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
284 wxArrayInt m_rowsOrCols
;
285 wxArrayAttrs m_attrs
;
288 // NB: this is just a wrapper around 3 objects: one which stores cell
289 // attributes, and 2 others for row/col ones
290 class WXDLLEXPORT wxGridCellAttrProviderData
293 wxGridCellAttrData m_cellAttrs
;
294 wxGridRowOrColAttrData m_rowAttrs
,
299 // ----------------------------------------------------------------------------
300 // data structures used for the data type registry
301 // ----------------------------------------------------------------------------
303 struct wxGridDataTypeInfo
305 wxGridDataTypeInfo(const wxString
& typeName
,
306 wxGridCellRenderer
* renderer
,
307 wxGridCellEditor
* editor
)
308 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
311 ~wxGridDataTypeInfo()
313 wxSafeDecRef(m_renderer
);
314 wxSafeDecRef(m_editor
);
318 wxGridCellRenderer
* m_renderer
;
319 wxGridCellEditor
* m_editor
;
323 WX_DEFINE_EXPORTED_ARRAY(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
);
326 class WXDLLEXPORT wxGridTypeRegistry
329 wxGridTypeRegistry() {}
330 ~wxGridTypeRegistry();
332 void RegisterDataType(const wxString
& typeName
,
333 wxGridCellRenderer
* renderer
,
334 wxGridCellEditor
* editor
);
336 // find one of already registered data types
337 int FindRegisteredDataType(const wxString
& typeName
);
339 // try to FindRegisteredDataType(), if this fails and typeName is one of
340 // standard typenames, register it and return its index
341 int FindDataType(const wxString
& typeName
);
343 // try to FindDataType(), if it fails see if it is not one of already
344 // registered data types with some params in which case clone the
345 // registered data type and set params for it
346 int FindOrCloneDataType(const wxString
& typeName
);
348 wxGridCellRenderer
* GetRenderer(int index
);
349 wxGridCellEditor
* GetEditor(int index
);
352 wxGridDataTypeInfoArray m_typeinfo
;
355 // ----------------------------------------------------------------------------
356 // conditional compilation
357 // ----------------------------------------------------------------------------
359 #ifndef WXGRID_DRAW_LINES
360 #define WXGRID_DRAW_LINES 1
363 // ----------------------------------------------------------------------------
365 // ----------------------------------------------------------------------------
367 //#define DEBUG_ATTR_CACHE
368 #ifdef DEBUG_ATTR_CACHE
369 static size_t gs_nAttrCacheHits
= 0;
370 static size_t gs_nAttrCacheMisses
= 0;
371 #endif // DEBUG_ATTR_CACHE
373 // ----------------------------------------------------------------------------
375 // ----------------------------------------------------------------------------
377 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
378 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
381 // TODO: this doesn't work at all, grid cells have different sizes and approx
382 // calculations don't work as because of the size mismatch scrollbars
383 // sometimes fail to be shown when they should be or vice versa
385 // The scroll bars may be a little flakey once in a while, but that is
386 // surely much less horrible than having scroll lines of only 1!!!
388 static const size_t GRID_SCROLL_LINE
= 15; // 1;
391 // the size of hash tables used a bit everywhere (the max number of elements
392 // in these hash tables is the number of rows/columns)
393 static const int GRID_HASH_SIZE
= 100;
395 // ============================================================================
397 // ============================================================================
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 wxGridCellEditor::wxGridCellEditor()
409 wxGridCellEditor::~wxGridCellEditor()
414 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
415 wxWindowID
WXUNUSED(id
),
416 wxEvtHandler
* evtHandler
)
419 m_control
->PushEventHandler(evtHandler
);
422 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
423 wxGridCellAttr
*attr
)
425 // erase the background because we might not fill the cell
426 wxClientDC
dc(m_control
->GetParent());
427 dc
.SetPen(*wxTRANSPARENT_PEN
);
428 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
429 dc
.DrawRectangle(rectCell
);
431 // redraw the control we just painted over
432 m_control
->Refresh();
435 void wxGridCellEditor::Destroy()
439 m_control
->PopEventHandler(TRUE
/* delete it*/);
441 m_control
->Destroy();
446 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
448 wxASSERT_MSG(m_control
,
449 wxT("The wxGridCellEditor must be Created first!"));
450 m_control
->Show(show
);
454 // set the colours/fonts if we have any
457 m_colFgOld
= m_control
->GetForegroundColour();
458 m_control
->SetForegroundColour(attr
->GetTextColour());
460 m_colBgOld
= m_control
->GetBackgroundColour();
461 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
463 m_fontOld
= m_control
->GetFont();
464 m_control
->SetFont(attr
->GetFont());
466 // can't do anything more in the base class version, the other
467 // attributes may only be used by the derived classes
472 // restore the standard colours fonts
473 if ( m_colFgOld
.Ok() )
475 m_control
->SetForegroundColour(m_colFgOld
);
476 m_colFgOld
= wxNullColour
;
479 if ( m_colBgOld
.Ok() )
481 m_control
->SetBackgroundColour(m_colBgOld
);
482 m_colBgOld
= wxNullColour
;
485 if ( m_fontOld
.Ok() )
487 m_control
->SetFont(m_fontOld
);
488 m_fontOld
= wxNullFont
;
493 void wxGridCellEditor::SetSize(const wxRect
& rect
)
495 wxASSERT_MSG(m_control
,
496 wxT("The wxGridCellEditor must be Created first!"));
497 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
500 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
505 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
507 // accept the simple key presses, not anything with Ctrl/Alt/Meta
508 return !(event
.ControlDown() || event
.AltDown());
511 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
516 void wxGridCellEditor::StartingClick()
520 // ----------------------------------------------------------------------------
521 // wxGridCellTextEditor
522 // ----------------------------------------------------------------------------
524 wxGridCellTextEditor::wxGridCellTextEditor()
529 void wxGridCellTextEditor::Create(wxWindow
* parent
,
531 wxEvtHandler
* evtHandler
)
533 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
534 wxDefaultPosition
, wxDefaultSize
535 #if defined(__WXMSW__)
536 , wxTE_PROCESS_TAB
| wxTE_MULTILINE
|
537 wxTE_NO_VSCROLL
| wxTE_AUTO_SCROLL
541 // TODO: use m_maxChars
543 wxGridCellEditor::Create(parent
, id
, evtHandler
);
546 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
547 wxGridCellAttr
* WXUNUSED(attr
))
549 // as we fill the entire client area, don't do anything here to minimize
553 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
555 wxRect
rect(rectOrig
);
557 // Make the edit control large enough to allow for internal
560 // TODO: remove this if the text ctrl sizing is improved esp. for
563 #if defined(__WXGTK__)
572 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
574 // MB: treat MSW separately here otherwise the caret doesn't show
575 // when the editor is in the first row.
576 #if defined(__WXMSW__)
579 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
582 #if defined(__WXMOTIF__)
586 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
587 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
588 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
589 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
592 wxGridCellEditor::SetSize(rect
);
595 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
597 wxASSERT_MSG(m_control
,
598 wxT("The wxGridCellEditor must be Created first!"));
600 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
602 DoBeginEdit(m_startValue
);
605 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
607 Text()->SetValue(startValue
);
608 Text()->SetInsertionPointEnd();
612 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
615 wxASSERT_MSG(m_control
,
616 wxT("The wxGridCellEditor must be Created first!"));
618 bool changed
= FALSE
;
619 wxString value
= Text()->GetValue();
620 if (value
!= m_startValue
)
624 grid
->GetTable()->SetValue(row
, col
, value
);
626 m_startValue
= wxEmptyString
;
627 Text()->SetValue(m_startValue
);
633 void wxGridCellTextEditor::Reset()
635 wxASSERT_MSG(m_control
,
636 wxT("The wxGridCellEditor must be Created first!"));
638 DoReset(m_startValue
);
641 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
643 Text()->SetValue(startValue
);
644 Text()->SetInsertionPointEnd();
647 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
649 if ( wxGridCellEditor::IsAcceptedKey(event
) )
651 int keycode
= event
.GetKeyCode();
665 case WXK_NUMPAD_MULTIPLY
:
669 case WXK_NUMPAD_SUBTRACT
:
671 case WXK_NUMPAD_DECIMAL
:
673 case WXK_NUMPAD_DIVIDE
:
677 // accept 8 bit chars too if isprint() agrees
678 if ( (keycode
< 255) && (isprint(keycode
)) )
686 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
688 // we don't check for !HasModifiers() because IsAcceptedKey() did it
690 // insert the key in the control
692 int keycode
= event
.GetKeyCode();
705 ch
= _T('0') + keycode
- WXK_NUMPAD0
;
709 case WXK_NUMPAD_MULTIPLY
:
719 case WXK_NUMPAD_SUBTRACT
:
724 case WXK_NUMPAD_DECIMAL
:
729 case WXK_NUMPAD_DIVIDE
:
734 if ( keycode
< 256 && keycode
>= 0 && isprint(keycode
) )
736 // FIXME this is not going to work for non letters...
737 if ( !event
.ShiftDown() )
739 keycode
= tolower(keycode
);
742 ch
= (wxChar
)keycode
;
752 Text()->AppendText(ch
);
760 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
761 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
763 #if defined(__WXMOTIF__) || defined(__WXGTK__)
764 // wxMotif needs a little extra help...
765 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
766 wxString
s( Text()->GetValue() );
767 s
= s
.Left(pos
) + "\n" + s
.Mid(pos
);
769 Text()->SetInsertionPoint( pos
);
771 // the other ports can handle a Return key press
777 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
787 if ( !params
.ToLong(&tmp
) )
789 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
793 m_maxChars
= (size_t)tmp
;
798 // ----------------------------------------------------------------------------
799 // wxGridCellNumberEditor
800 // ----------------------------------------------------------------------------
802 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
808 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
810 wxEvtHandler
* evtHandler
)
814 // create a spin ctrl
815 m_control
= new wxSpinCtrl(parent
, -1, wxEmptyString
,
816 wxDefaultPosition
, wxDefaultSize
,
820 wxGridCellEditor::Create(parent
, id
, evtHandler
);
824 // just a text control
825 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
828 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
829 #endif // wxUSE_VALIDATORS
833 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
835 // first get the value
836 wxGridTableBase
*table
= grid
->GetTable();
837 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
839 m_valueOld
= table
->GetValueAsLong(row
, col
);
843 wxString sValue
= table
->GetValue(row
, col
);
844 if (! sValue
.ToLong(&m_valueOld
))
846 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
853 Spin()->SetValue((int)m_valueOld
);
858 DoBeginEdit(GetString());
862 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
870 value
= Spin()->GetValue();
871 changed
= value
!= m_valueOld
;
875 changed
= Text()->GetValue().ToLong(&value
) && (value
!= m_valueOld
);
880 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
881 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
883 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%ld"), value
));
889 void wxGridCellNumberEditor::Reset()
893 Spin()->SetValue((int)m_valueOld
);
897 DoReset(GetString());
901 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
903 if ( wxGridCellEditor::IsAcceptedKey(event
) )
905 int keycode
= event
.GetKeyCode();
921 case WXK_NUMPAD_SUBTRACT
:
927 if ( (keycode
< 128) && isdigit(keycode
) )
935 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
939 int keycode
= (int) event
.KeyCode();
940 if ( isdigit(keycode
) || keycode
== '+' || keycode
== '-' )
942 wxGridCellTextEditor::StartingKey(event
);
952 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
963 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
967 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
971 // skip the error message below
976 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
980 // ----------------------------------------------------------------------------
981 // wxGridCellFloatEditor
982 // ----------------------------------------------------------------------------
984 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
987 m_precision
= precision
;
990 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
992 wxEvtHandler
* evtHandler
)
994 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
997 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
998 #endif // wxUSE_VALIDATORS
1001 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1003 // first get the value
1004 wxGridTableBase
*table
= grid
->GetTable();
1005 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1007 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1011 wxString sValue
= table
->GetValue(row
, col
);
1012 if (! sValue
.ToDouble(&m_valueOld
))
1014 wxFAIL_MSG( _T("this cell doesn't have float value") );
1019 DoBeginEdit(GetString());
1022 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1026 if ( Text()->GetValue().ToDouble(&value
) && (value
!= m_valueOld
) )
1028 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1029 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1031 grid
->GetTable()->SetValue(row
, col
, wxString::Format(wxT("%f"), value
));
1041 void wxGridCellFloatEditor::Reset()
1043 DoReset(GetString());
1046 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1048 int keycode
= (int)event
.KeyCode();
1049 if ( isdigit(keycode
) ||
1050 keycode
== '+' || keycode
== '-' || keycode
== '.' )
1052 wxGridCellTextEditor::StartingKey(event
);
1054 // skip Skip() below
1061 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1072 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1076 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1078 m_precision
= (int)tmp
;
1080 // skip the error message below
1085 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1089 wxString
wxGridCellFloatEditor::GetString() const
1092 if ( m_width
== -1 )
1094 // default width/precision
1097 else if ( m_precision
== -1 )
1099 // default precision
1100 fmt
.Printf(_T("%%%d.g"), m_width
);
1104 fmt
.Printf(_T("%%%d.%dg"), m_width
, m_precision
);
1107 return wxString::Format(fmt
, m_valueOld
);
1110 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1112 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1114 int keycode
= event
.GetKeyCode();
1128 case WXK_NUMPAD_ADD
:
1130 case WXK_NUMPAD_SUBTRACT
:
1132 case WXK_NUMPAD_DECIMAL
:
1136 // additionally accept 'e' as in '1e+6'
1137 if ( (keycode
< 128) &&
1138 (isdigit(keycode
) || tolower(keycode
) == 'e') )
1146 // ----------------------------------------------------------------------------
1147 // wxGridCellBoolEditor
1148 // ----------------------------------------------------------------------------
1150 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1152 wxEvtHandler
* evtHandler
)
1154 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1155 wxDefaultPosition
, wxDefaultSize
,
1158 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1161 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1163 bool resize
= FALSE
;
1164 wxSize size
= m_control
->GetSize();
1165 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1167 // check if the checkbox is not too big/small for this cell
1168 wxSize sizeBest
= m_control
->GetBestSize();
1169 if ( !(size
== sizeBest
) )
1171 // reset to default size if it had been made smaller
1177 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1179 // leave 1 pixel margin
1180 size
.x
= size
.y
= minSize
- 2;
1187 m_control
->SetSize(size
);
1190 // position it in the centre of the rectangle (TODO: support alignment?)
1192 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1193 // the checkbox without label still has some space to the right in wxGTK,
1194 // so shift it to the right
1196 #elif defined(__WXMSW__)
1197 // here too, but in other way
1202 m_control
->Move(r
.x
+ r
.width
/2 - size
.x
/2, r
.y
+ r
.height
/2 - size
.y
/2);
1205 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1207 m_control
->Show(show
);
1211 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1212 CBox()->SetBackgroundColour(colBg
);
1216 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1218 wxASSERT_MSG(m_control
,
1219 wxT("The wxGridCellEditor must be Created first!"));
1221 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1222 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1225 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1226 m_startValue
= !( !cellval
|| (cellval
== "0") );
1228 CBox()->SetValue(m_startValue
);
1232 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1235 wxASSERT_MSG(m_control
,
1236 wxT("The wxGridCellEditor must be Created first!"));
1238 bool changed
= FALSE
;
1239 bool value
= CBox()->GetValue();
1240 if ( value
!= m_startValue
)
1245 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1246 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1248 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1254 void wxGridCellBoolEditor::Reset()
1256 wxASSERT_MSG(m_control
,
1257 wxT("The wxGridCellEditor must be Created first!"));
1259 CBox()->SetValue(m_startValue
);
1262 void wxGridCellBoolEditor::StartingClick()
1264 CBox()->SetValue(!CBox()->GetValue());
1267 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1269 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1271 int keycode
= event
.GetKeyCode();
1275 case WXK_NUMPAD_MULTIPLY
:
1277 case WXK_NUMPAD_ADD
:
1279 case WXK_NUMPAD_SUBTRACT
:
1290 // ----------------------------------------------------------------------------
1291 // wxGridCellChoiceEditor
1292 // ----------------------------------------------------------------------------
1294 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1295 const wxString choices
[],
1297 : m_allowOthers(allowOthers
)
1301 m_choices
.Alloc(count
);
1302 for ( size_t n
= 0; n
< count
; n
++ )
1304 m_choices
.Add(choices
[n
]);
1309 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1311 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1312 editor
->m_allowOthers
= m_allowOthers
;
1313 editor
->m_choices
= m_choices
;
1318 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1320 wxEvtHandler
* evtHandler
)
1322 size_t count
= m_choices
.GetCount();
1323 wxString
*choices
= new wxString
[count
];
1324 for ( size_t n
= 0; n
< count
; n
++ )
1326 choices
[n
] = m_choices
[n
];
1329 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1330 wxDefaultPosition
, wxDefaultSize
,
1332 m_allowOthers
? 0 : wxCB_READONLY
);
1336 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1339 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1340 wxGridCellAttr
* attr
)
1342 // as we fill the entire client area, don't do anything here to minimize
1345 // TODO: It doesn't actually fill the client area since the height of a
1346 // combo always defaults to the standard... Until someone has time to
1347 // figure out the right rectangle to paint, just do it the normal way...
1348 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1351 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1353 wxASSERT_MSG(m_control
,
1354 wxT("The wxGridCellEditor must be Created first!"));
1356 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1358 Combo()->SetValue(m_startValue
);
1359 size_t count
= m_choices
.GetCount();
1360 for (size_t i
=0; i
<count
; i
++)
1362 if (m_startValue
== m_choices
[i
])
1364 Combo()->SetSelection(i
);
1368 Combo()->SetInsertionPointEnd();
1369 Combo()->SetFocus();
1372 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1375 wxString value
= Combo()->GetValue();
1376 bool changed
= value
!= m_startValue
;
1379 grid
->GetTable()->SetValue(row
, col
, value
);
1381 m_startValue
= wxEmptyString
;
1382 Combo()->SetValue(m_startValue
);
1387 void wxGridCellChoiceEditor::Reset()
1389 Combo()->SetValue(m_startValue
);
1390 Combo()->SetInsertionPointEnd();
1393 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1403 wxStringTokenizer
tk(params
, _T(','));
1404 while ( tk
.HasMoreTokens() )
1406 m_choices
.Add(tk
.GetNextToken());
1410 // ----------------------------------------------------------------------------
1411 // wxGridCellEditorEvtHandler
1412 // ----------------------------------------------------------------------------
1414 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1416 switch ( event
.KeyCode() )
1420 m_grid
->DisableCellEditControl();
1424 m_grid
->GetEventHandler()->ProcessEvent( event
);
1428 case WXK_NUMPAD_ENTER
:
1429 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1430 m_editor
->HandleReturn(event
);
1439 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1441 switch ( event
.KeyCode() )
1446 case WXK_NUMPAD_ENTER
:
1454 // ----------------------------------------------------------------------------
1455 // wxGridCellWorker is an (almost) empty common base class for
1456 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1457 // ----------------------------------------------------------------------------
1459 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1464 wxGridCellWorker::~wxGridCellWorker()
1468 // ============================================================================
1470 // ============================================================================
1472 // ----------------------------------------------------------------------------
1473 // wxGridCellRenderer
1474 // ----------------------------------------------------------------------------
1476 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1477 wxGridCellAttr
& attr
,
1480 int WXUNUSED(row
), int WXUNUSED(col
),
1483 dc
.SetBackgroundMode( wxSOLID
);
1487 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1491 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1494 dc
.SetPen( *wxTRANSPARENT_PEN
);
1495 dc
.DrawRectangle(rect
);
1498 // ----------------------------------------------------------------------------
1499 // wxGridCellStringRenderer
1500 // ----------------------------------------------------------------------------
1502 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1503 wxGridCellAttr
& attr
,
1507 dc
.SetBackgroundMode( wxTRANSPARENT
);
1509 // TODO some special colours for attr.IsReadOnly() case?
1513 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1514 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1518 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1519 dc
.SetTextForeground( attr
.GetTextColour() );
1522 dc
.SetFont( attr
.GetFont() );
1525 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1527 const wxString
& text
)
1529 wxCoord x
= 0, y
= 0, max_x
= 0;
1530 dc
.SetFont(attr
.GetFont());
1531 wxStringTokenizer
tk(text
, _T('\n'));
1532 while ( tk
.HasMoreTokens() )
1534 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1535 max_x
= wxMax(max_x
, x
);
1538 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1540 return wxSize(max_x
, y
);
1543 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1544 wxGridCellAttr
& attr
,
1548 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1551 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1552 wxGridCellAttr
& attr
,
1554 const wxRect
& rectCell
,
1558 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1560 // now we only have to draw the text
1561 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1564 attr
.GetAlignment(&hAlign
, &vAlign
);
1566 wxRect rect
= rectCell
;
1569 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1570 rect
, hAlign
, vAlign
);
1573 // ----------------------------------------------------------------------------
1574 // wxGridCellNumberRenderer
1575 // ----------------------------------------------------------------------------
1577 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1579 wxGridTableBase
*table
= grid
.GetTable();
1581 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1583 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1587 text
= table
->GetValue(row
, col
);
1593 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1594 wxGridCellAttr
& attr
,
1596 const wxRect
& rectCell
,
1600 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1602 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1604 // draw the text right aligned by default
1606 attr
.GetAlignment(&hAlign
, &vAlign
);
1607 hAlign
= wxALIGN_RIGHT
;
1609 wxRect rect
= rectCell
;
1612 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1615 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1616 wxGridCellAttr
& attr
,
1620 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1623 // ----------------------------------------------------------------------------
1624 // wxGridCellFloatRenderer
1625 // ----------------------------------------------------------------------------
1627 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1630 SetPrecision(precision
);
1633 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1635 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1636 renderer
->m_width
= m_width
;
1637 renderer
->m_precision
= m_precision
;
1638 renderer
->m_format
= m_format
;
1643 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1645 wxGridTableBase
*table
= grid
.GetTable();
1650 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1652 val
= table
->GetValueAsDouble(row
, col
);
1657 text
= table
->GetValue(row
, col
);
1658 hasDouble
= text
.ToDouble(&val
);
1665 if ( m_width
== -1 )
1667 if ( m_precision
== -1 )
1669 // default width/precision
1670 m_format
= _T("%f");
1674 m_format
.Printf(_T("%%.%df"), m_precision
);
1677 else if ( m_precision
== -1 )
1679 // default precision
1680 m_format
.Printf(_T("%%%d.f"), m_width
);
1684 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1688 text
.Printf(m_format
, val
);
1691 //else: text already contains the string
1696 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1697 wxGridCellAttr
& attr
,
1699 const wxRect
& rectCell
,
1703 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1705 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1707 // draw the text right aligned by default
1709 attr
.GetAlignment(&hAlign
, &vAlign
);
1710 hAlign
= wxALIGN_RIGHT
;
1712 wxRect rect
= rectCell
;
1715 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1718 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1719 wxGridCellAttr
& attr
,
1723 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1726 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1730 // reset to defaults
1736 wxString tmp
= params
.BeforeFirst(_T(','));
1740 if ( tmp
.ToLong(&width
) )
1742 SetWidth((int)width
);
1746 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1750 tmp
= params
.AfterFirst(_T(','));
1754 if ( tmp
.ToLong(&precision
) )
1756 SetPrecision((int)precision
);
1760 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1768 // ----------------------------------------------------------------------------
1769 // wxGridCellBoolRenderer
1770 // ----------------------------------------------------------------------------
1772 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1774 // FIXME these checkbox size calculations are really ugly...
1776 // between checkmark and box
1777 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1779 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1780 wxGridCellAttr
& WXUNUSED(attr
),
1785 // compute it only once (no locks for MT safeness in GUI thread...)
1786 if ( !ms_sizeCheckMark
.x
)
1788 // get checkbox size
1789 wxCoord checkSize
= 0;
1790 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1791 wxSize size
= checkbox
->GetBestSize();
1792 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1794 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1795 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1796 checkSize
-= size
.y
/ 2;
1801 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1804 return ms_sizeCheckMark
;
1807 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1808 wxGridCellAttr
& attr
,
1814 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1816 // draw a check mark in the centre (ignoring alignment - TODO)
1817 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1819 // don't draw outside the cell
1820 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1821 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1823 // and even leave (at least) 1 pixel margin
1824 size
.x
= size
.y
= minSize
- 2;
1827 // draw a border around checkmark
1829 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1830 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1831 rectBorder
.width
= size
.x
;
1832 rectBorder
.height
= size
.y
;
1835 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1836 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1839 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1840 value
= !( !cellval
|| (cellval
== "0") );
1845 wxRect rectMark
= rectBorder
;
1847 // MSW DrawCheckMark() is weird (and should probably be changed...)
1848 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1852 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1855 dc
.SetTextForeground(attr
.GetTextColour());
1856 dc
.DrawCheckMark(rectMark
);
1859 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1860 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1861 dc
.DrawRectangle(rectBorder
);
1864 // ----------------------------------------------------------------------------
1866 // ----------------------------------------------------------------------------
1868 wxGridCellAttr
*wxGridCellAttr::Clone() const
1870 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1871 if ( HasTextColour() )
1872 attr
->SetTextColour(GetTextColour());
1873 if ( HasBackgroundColour() )
1874 attr
->SetBackgroundColour(GetBackgroundColour());
1876 attr
->SetFont(GetFont());
1877 if ( HasAlignment() )
1878 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1882 attr
->SetRenderer(m_renderer
);
1883 m_renderer
->IncRef();
1887 attr
->SetEditor(m_editor
);
1892 attr
->SetReadOnly();
1894 attr
->SetKind( m_attrkind
);
1896 attr
->SetDefAttr(m_defGridAttr
);
1901 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
1903 if ( !HasTextColour() && mergefrom
->HasTextColour() )
1904 SetTextColour(mergefrom
->GetTextColour());
1905 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
1906 SetBackgroundColour(mergefrom
->GetBackgroundColour());
1907 if ( !HasFont() && mergefrom
->HasFont() )
1908 SetFont(mergefrom
->GetFont());
1909 if ( !!HasAlignment() && mergefrom
->HasAlignment() ){
1911 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
1912 SetAlignment(hAlign
, vAlign
);
1915 // Directly access member functions as GetRender/Editor don't just return
1916 // m_renderer/m_editor
1918 // Maybe add support for merge of Render and Editor?
1919 if (!HasRenderer() && mergefrom
->HasRenderer() )
1921 m_renderer
= mergefrom
->m_renderer
;
1922 m_renderer
->IncRef();
1924 if ( !HasEditor() && mergefrom
->HasEditor() )
1926 m_editor
= mergefrom
->m_editor
;
1929 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
1930 SetReadOnly(mergefrom
->IsReadOnly());
1932 SetDefAttr(mergefrom
->m_defGridAttr
);
1935 const wxColour
& wxGridCellAttr::GetTextColour() const
1937 if (HasTextColour())
1941 else if (m_defGridAttr
!= this)
1943 return m_defGridAttr
->GetTextColour();
1947 wxFAIL_MSG(wxT("Missing default cell attribute"));
1948 return wxNullColour
;
1953 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1955 if (HasBackgroundColour())
1957 else if (m_defGridAttr
!= this)
1958 return m_defGridAttr
->GetBackgroundColour();
1961 wxFAIL_MSG(wxT("Missing default cell attribute"));
1962 return wxNullColour
;
1967 const wxFont
& wxGridCellAttr::GetFont() const
1971 else if (m_defGridAttr
!= this)
1972 return m_defGridAttr
->GetFont();
1975 wxFAIL_MSG(wxT("Missing default cell attribute"));
1981 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1985 if ( hAlign
) *hAlign
= m_hAlign
;
1986 if ( vAlign
) *vAlign
= m_vAlign
;
1988 else if (m_defGridAttr
!= this)
1989 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1992 wxFAIL_MSG(wxT("Missing default cell attribute"));
1997 // GetRenderer and GetEditor use a slightly different decision path about
1998 // which attribute to use. If a non-default attr object has one then it is
1999 // used, otherwise the default editor or renderer is fetched from the grid and
2000 // used. It should be the default for the data type of the cell. If it is
2001 // NULL (because the table has a type that the grid does not have in its
2002 // registry,) then the grid's default editor or renderer is used.
2004 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2006 wxGridCellRenderer
* renderer
= NULL
;
2008 if ( m_defGridAttr
!= this || grid
== NULL
)
2010 renderer
= m_renderer
; // use local attribute
2015 if ( !renderer
&& grid
) // get renderer for the data type
2017 // GetDefaultRendererForCell() will do IncRef() for us
2018 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2023 // if we still don't have one then use the grid default
2024 // (no need for IncRef() here neither)
2025 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
2030 wxFAIL_MSG(wxT("Missing default cell attribute"));
2036 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2038 wxGridCellEditor
* editor
= NULL
;
2040 if ( m_defGridAttr
!= this || grid
== NULL
)
2042 editor
= m_editor
; // use local attribute
2047 if ( !editor
&& grid
) // get renderer for the data type
2048 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2051 // if we still don't have one then use the grid default
2052 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2056 wxFAIL_MSG(wxT("Missing default cell attribute"));
2062 // ----------------------------------------------------------------------------
2063 // wxGridCellAttrData
2064 // ----------------------------------------------------------------------------
2066 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2068 int n
= FindIndex(row
, col
);
2069 if ( n
== wxNOT_FOUND
)
2071 // add the attribute
2072 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2078 // change the attribute
2079 m_attrs
[(size_t)n
].attr
= attr
;
2083 // remove this attribute
2084 m_attrs
.RemoveAt((size_t)n
);
2089 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2091 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2093 int n
= FindIndex(row
, col
);
2094 if ( n
!= wxNOT_FOUND
)
2096 attr
= m_attrs
[(size_t)n
].attr
;
2103 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2105 size_t count
= m_attrs
.GetCount();
2106 for ( size_t n
= 0; n
< count
; n
++ )
2108 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2109 wxCoord row
= coords
.GetRow();
2110 if ((size_t)row
>= pos
)
2114 // If rows inserted, include row counter where necessary
2115 coords
.SetRow(row
+ numRows
);
2117 else if (numRows
< 0)
2119 // If rows deleted ...
2120 if ((size_t)row
>= pos
- numRows
)
2122 // ...either decrement row counter (if row still exists)...
2123 coords
.SetRow(row
+ numRows
);
2127 // ...or remove the attribute
2128 m_attrs
.RemoveAt((size_t)n
);
2136 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2138 size_t count
= m_attrs
.GetCount();
2139 for ( size_t n
= 0; n
< count
; n
++ )
2141 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2142 wxCoord col
= coords
.GetCol();
2143 if ( (size_t)col
>= pos
)
2147 // If rows inserted, include row counter where necessary
2148 coords
.SetCol(col
+ numCols
);
2150 else if (numCols
< 0)
2152 // If rows deleted ...
2153 if ((size_t)col
>= pos
- numCols
)
2155 // ...either decrement row counter (if row still exists)...
2156 coords
.SetCol(col
+ numCols
);
2160 // ...or remove the attribute
2161 m_attrs
.RemoveAt((size_t)n
);
2169 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2171 size_t count
= m_attrs
.GetCount();
2172 for ( size_t n
= 0; n
< count
; n
++ )
2174 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2175 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2184 // ----------------------------------------------------------------------------
2185 // wxGridRowOrColAttrData
2186 // ----------------------------------------------------------------------------
2188 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2190 size_t count
= m_attrs
.Count();
2191 for ( size_t n
= 0; n
< count
; n
++ )
2193 m_attrs
[n
]->DecRef();
2197 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2199 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2201 int n
= m_rowsOrCols
.Index(rowOrCol
);
2202 if ( n
!= wxNOT_FOUND
)
2204 attr
= m_attrs
[(size_t)n
];
2211 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2213 int i
= m_rowsOrCols
.Index(rowOrCol
);
2214 if ( i
== wxNOT_FOUND
)
2216 // add the attribute
2217 m_rowsOrCols
.Add(rowOrCol
);
2222 size_t n
= (size_t)i
;
2225 // change the attribute
2226 m_attrs
[n
]->DecRef();
2231 // remove this attribute
2232 m_attrs
[n
]->DecRef();
2233 m_rowsOrCols
.RemoveAt(n
);
2234 m_attrs
.RemoveAt(n
);
2239 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2241 size_t count
= m_attrs
.GetCount();
2242 for ( size_t n
= 0; n
< count
; n
++ )
2244 int & rowOrCol
= m_rowsOrCols
[n
];
2245 if ( (size_t)rowOrCol
>= pos
)
2247 if ( numRowsOrCols
> 0 )
2249 // If rows inserted, include row counter where necessary
2250 rowOrCol
+= numRowsOrCols
;
2252 else if ( numRowsOrCols
< 0)
2254 // If rows deleted, either decrement row counter (if row still exists)
2255 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2256 rowOrCol
+= numRowsOrCols
;
2259 m_rowsOrCols
.RemoveAt((size_t)n
);
2260 m_attrs
.RemoveAt((size_t)n
);
2268 // ----------------------------------------------------------------------------
2269 // wxGridCellAttrProvider
2270 // ----------------------------------------------------------------------------
2272 wxGridCellAttrProvider::wxGridCellAttrProvider()
2274 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2277 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2282 void wxGridCellAttrProvider::InitData()
2284 m_data
= new wxGridCellAttrProviderData
;
2287 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2288 wxGridCellAttr::wxAttrKind kind
) const
2290 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2295 case (wxGridCellAttr::Any
):
2296 //Get cached merge attributes.
2297 // Currenlty not used as no cache implemented as not mutiable
2298 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2301 //Basicaly implement old version.
2302 //Also check merge cache, so we don't have to re-merge every time..
2303 wxGridCellAttr
*attrcell
= (wxGridCellAttr
*)NULL
,
2304 *attrrow
= (wxGridCellAttr
*)NULL
,
2305 *attrcol
= (wxGridCellAttr
*)NULL
;
2307 attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2308 attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2309 attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2311 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2312 // Two or move are non NULL
2313 attr
= new wxGridCellAttr
;
2314 attr
->SetKind(wxGridCellAttr::Merged
);
2318 attr
->MergeWith(attrcell
);
2322 attr
->MergeWith(attrcol
);
2326 attr
->MergeWith(attrrow
);
2329 //store merge attr if cache implemented
2331 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2335 // one or none is non null return it or null.
2336 if(attrrow
) attr
= attrrow
;
2337 if(attrcol
) attr
= attrcol
;
2338 if(attrcell
) attr
= attrcell
;
2342 case (wxGridCellAttr::Cell
):
2343 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2345 case (wxGridCellAttr::Col
):
2346 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2348 case (wxGridCellAttr::Row
):
2349 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2353 // (wxGridCellAttr::Default):
2354 // (wxGridCellAttr::Merged):
2361 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2367 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2370 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2375 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2378 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2383 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2386 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2390 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2392 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2396 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2400 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2402 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2406 // ----------------------------------------------------------------------------
2407 // wxGridTypeRegistry
2408 // ----------------------------------------------------------------------------
2410 wxGridTypeRegistry::~wxGridTypeRegistry()
2412 size_t count
= m_typeinfo
.Count();
2413 for ( size_t i
= 0; i
< count
; i
++ )
2414 delete m_typeinfo
[i
];
2418 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2419 wxGridCellRenderer
* renderer
,
2420 wxGridCellEditor
* editor
)
2422 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2424 // is it already registered?
2425 int loc
= FindRegisteredDataType(typeName
);
2426 if ( loc
!= wxNOT_FOUND
)
2428 delete m_typeinfo
[loc
];
2429 m_typeinfo
[loc
] = info
;
2433 m_typeinfo
.Add(info
);
2437 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2439 size_t count
= m_typeinfo
.GetCount();
2440 for ( size_t i
= 0; i
< count
; i
++ )
2442 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2451 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2453 int index
= FindRegisteredDataType(typeName
);
2454 if ( index
== wxNOT_FOUND
)
2456 // check whether this is one of the standard ones, in which case
2457 // register it "on the fly"
2458 if ( typeName
== wxGRID_VALUE_STRING
)
2460 RegisterDataType(wxGRID_VALUE_STRING
,
2461 new wxGridCellStringRenderer
,
2462 new wxGridCellTextEditor
);
2464 else if ( typeName
== wxGRID_VALUE_BOOL
)
2466 RegisterDataType(wxGRID_VALUE_BOOL
,
2467 new wxGridCellBoolRenderer
,
2468 new wxGridCellBoolEditor
);
2470 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2472 RegisterDataType(wxGRID_VALUE_NUMBER
,
2473 new wxGridCellNumberRenderer
,
2474 new wxGridCellNumberEditor
);
2476 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2478 RegisterDataType(wxGRID_VALUE_FLOAT
,
2479 new wxGridCellFloatRenderer
,
2480 new wxGridCellFloatEditor
);
2482 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2484 RegisterDataType(wxGRID_VALUE_CHOICE
,
2485 new wxGridCellStringRenderer
,
2486 new wxGridCellChoiceEditor
);
2493 // we get here only if just added the entry for this type, so return
2495 index
= m_typeinfo
.GetCount() - 1;
2501 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2503 int index
= FindDataType(typeName
);
2504 if ( index
== wxNOT_FOUND
)
2506 // the first part of the typename is the "real" type, anything after ':'
2507 // are the parameters for the renderer
2508 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2509 if ( index
== wxNOT_FOUND
)
2514 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2515 wxGridCellRenderer
*rendererOld
= renderer
;
2516 renderer
= renderer
->Clone();
2517 rendererOld
->DecRef();
2519 wxGridCellEditor
*editor
= GetEditor(index
);
2520 wxGridCellEditor
*editorOld
= editor
;
2521 editor
= editor
->Clone();
2522 editorOld
->DecRef();
2524 // do it even if there are no parameters to reset them to defaults
2525 wxString params
= typeName
.AfterFirst(_T(':'));
2526 renderer
->SetParameters(params
);
2527 editor
->SetParameters(params
);
2529 // register the new typename
2530 RegisterDataType(typeName
, renderer
, editor
);
2532 // we just registered it, it's the last one
2533 index
= m_typeinfo
.GetCount() - 1;
2539 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2541 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2547 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2549 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2555 // ----------------------------------------------------------------------------
2557 // ----------------------------------------------------------------------------
2559 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2562 wxGridTableBase::wxGridTableBase()
2564 m_view
= (wxGrid
*) NULL
;
2565 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2568 wxGridTableBase::~wxGridTableBase()
2570 delete m_attrProvider
;
2573 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2575 delete m_attrProvider
;
2576 m_attrProvider
= attrProvider
;
2579 bool wxGridTableBase::CanHaveAttributes()
2581 if ( ! GetAttrProvider() )
2583 // use the default attr provider by default
2584 SetAttrProvider(new wxGridCellAttrProvider
);
2589 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2591 if ( m_attrProvider
)
2592 return m_attrProvider
->GetAttr(row
, col
, kind
);
2594 return (wxGridCellAttr
*)NULL
;
2597 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2599 if ( m_attrProvider
)
2601 attr
->SetKind(wxGridCellAttr::Cell
);
2602 m_attrProvider
->SetAttr(attr
, row
, col
);
2606 // as we take ownership of the pointer and don't store it, we must
2612 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2614 if ( m_attrProvider
)
2616 attr
->SetKind(wxGridCellAttr::Row
);
2617 m_attrProvider
->SetRowAttr(attr
, row
);
2621 // as we take ownership of the pointer and don't store it, we must
2627 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2629 if ( m_attrProvider
)
2631 attr
->SetKind(wxGridCellAttr::Col
);
2632 m_attrProvider
->SetColAttr(attr
, col
);
2636 // as we take ownership of the pointer and don't store it, we must
2642 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2643 size_t WXUNUSED(numRows
) )
2645 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2650 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2652 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2657 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2658 size_t WXUNUSED(numRows
) )
2660 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2665 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2666 size_t WXUNUSED(numCols
) )
2668 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2673 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2675 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2680 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2681 size_t WXUNUSED(numCols
) )
2683 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2689 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2692 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2693 // how much it makes sense to us geeks.
2697 wxString
wxGridTableBase::GetColLabelValue( int col
)
2699 // default col labels are:
2700 // cols 0 to 25 : A-Z
2701 // cols 26 to 675 : AA-ZZ
2706 for ( n
= 1; ; n
++ )
2708 s
+= (_T('A') + (wxChar
)( col%26
));
2710 if ( col
< 0 ) break;
2713 // reverse the string...
2715 for ( i
= 0; i
< n
; i
++ )
2724 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2726 return wxGRID_VALUE_STRING
;
2729 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2730 const wxString
& typeName
)
2732 return typeName
== wxGRID_VALUE_STRING
;
2735 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2737 return CanGetValueAs(row
, col
, typeName
);
2740 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2745 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2750 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2755 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2756 long WXUNUSED(value
) )
2760 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2761 double WXUNUSED(value
) )
2765 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2766 bool WXUNUSED(value
) )
2771 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2772 const wxString
& WXUNUSED(typeName
) )
2777 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2778 const wxString
& WXUNUSED(typeName
),
2779 void* WXUNUSED(value
) )
2783 //////////////////////////////////////////////////////////////////////
2785 // Message class for the grid table to send requests and notifications
2789 wxGridTableMessage::wxGridTableMessage()
2791 m_table
= (wxGridTableBase
*) NULL
;
2797 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2798 int commandInt1
, int commandInt2
)
2802 m_comInt1
= commandInt1
;
2803 m_comInt2
= commandInt2
;
2808 //////////////////////////////////////////////////////////////////////
2810 // A basic grid table for string data. An object of this class will
2811 // created by wxGrid if you don't specify an alternative table class.
2814 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2816 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2818 wxGridStringTable::wxGridStringTable()
2823 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2828 m_data
.Alloc( numRows
);
2831 sa
.Alloc( numCols
);
2832 for ( col
= 0; col
< numCols
; col
++ )
2834 sa
.Add( wxEmptyString
);
2837 for ( row
= 0; row
< numRows
; row
++ )
2843 wxGridStringTable::~wxGridStringTable()
2847 int wxGridStringTable::GetNumberRows()
2849 return m_data
.GetCount();
2852 int wxGridStringTable::GetNumberCols()
2854 if ( m_data
.GetCount() > 0 )
2855 return m_data
[0].GetCount();
2860 wxString
wxGridStringTable::GetValue( int row
, int col
)
2862 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2863 _T("invalid row or column index in wxGridStringTable") );
2865 return m_data
[row
][col
];
2868 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2870 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2871 _T("invalid row or column index in wxGridStringTable") );
2873 m_data
[row
][col
] = value
;
2876 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2878 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2879 _T("invalid row or column index in wxGridStringTable") );
2881 return (m_data
[row
][col
] == wxEmptyString
);
2884 void wxGridStringTable::Clear()
2887 int numRows
, numCols
;
2889 numRows
= m_data
.GetCount();
2892 numCols
= m_data
[0].GetCount();
2894 for ( row
= 0; row
< numRows
; row
++ )
2896 for ( col
= 0; col
< numCols
; col
++ )
2898 m_data
[row
][col
] = wxEmptyString
;
2905 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2909 size_t curNumRows
= m_data
.GetCount();
2910 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2911 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2913 if ( pos
>= curNumRows
)
2915 return AppendRows( numRows
);
2919 sa
.Alloc( curNumCols
);
2920 for ( col
= 0; col
< curNumCols
; col
++ )
2922 sa
.Add( wxEmptyString
);
2925 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2927 m_data
.Insert( sa
, row
);
2931 wxGridTableMessage
msg( this,
2932 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2936 GetView()->ProcessTableMessage( msg
);
2942 bool wxGridStringTable::AppendRows( size_t numRows
)
2946 size_t curNumRows
= m_data
.GetCount();
2947 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2948 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2951 if ( curNumCols
> 0 )
2953 sa
.Alloc( curNumCols
);
2954 for ( col
= 0; col
< curNumCols
; col
++ )
2956 sa
.Add( wxEmptyString
);
2960 for ( row
= 0; row
< numRows
; row
++ )
2967 wxGridTableMessage
msg( this,
2968 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2971 GetView()->ProcessTableMessage( msg
);
2977 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2981 size_t curNumRows
= m_data
.GetCount();
2983 if ( pos
>= curNumRows
)
2986 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2987 pos
, numRows
, curNumRows
);
2988 wxFAIL_MSG( errmsg
);
2992 if ( numRows
> curNumRows
- pos
)
2994 numRows
= curNumRows
- pos
;
2997 if ( numRows
>= curNumRows
)
2999 m_data
.Empty(); // don't release memory just yet
3003 for ( n
= 0; n
< numRows
; n
++ )
3005 m_data
.Remove( pos
);
3010 wxGridTableMessage
msg( this,
3011 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3015 GetView()->ProcessTableMessage( msg
);
3021 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3025 size_t curNumRows
= m_data
.GetCount();
3026 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3027 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3029 if ( pos
>= curNumCols
)
3031 return AppendCols( numCols
);
3034 for ( row
= 0; row
< curNumRows
; row
++ )
3036 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3038 m_data
[row
].Insert( wxEmptyString
, col
);
3043 wxGridTableMessage
msg( this,
3044 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3048 GetView()->ProcessTableMessage( msg
);
3054 bool wxGridStringTable::AppendCols( size_t numCols
)
3058 size_t curNumRows
= m_data
.GetCount();
3062 // TODO: something better than this ?
3064 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3069 for ( row
= 0; row
< curNumRows
; row
++ )
3071 for ( n
= 0; n
< numCols
; n
++ )
3073 m_data
[row
].Add( wxEmptyString
);
3079 wxGridTableMessage
msg( this,
3080 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3083 GetView()->ProcessTableMessage( msg
);
3089 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3093 size_t curNumRows
= m_data
.GetCount();
3094 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3095 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3097 if ( pos
>= curNumCols
)
3100 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3101 pos
, numCols
, curNumCols
);
3102 wxFAIL_MSG( errmsg
);
3106 if ( numCols
> curNumCols
- pos
)
3108 numCols
= curNumCols
- pos
;
3111 for ( row
= 0; row
< curNumRows
; row
++ )
3113 if ( numCols
>= curNumCols
)
3115 m_data
[row
].Clear();
3119 for ( n
= 0; n
< numCols
; n
++ )
3121 m_data
[row
].Remove( pos
);
3127 wxGridTableMessage
msg( this,
3128 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3132 GetView()->ProcessTableMessage( msg
);
3138 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3140 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3142 // using default label
3144 return wxGridTableBase::GetRowLabelValue( row
);
3148 return m_rowLabels
[ row
];
3152 wxString
wxGridStringTable::GetColLabelValue( int col
)
3154 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3156 // using default label
3158 return wxGridTableBase::GetColLabelValue( col
);
3162 return m_colLabels
[ col
];
3166 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3168 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3170 int n
= m_rowLabels
.GetCount();
3172 for ( i
= n
; i
<= row
; i
++ )
3174 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3178 m_rowLabels
[row
] = value
;
3181 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3183 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3185 int n
= m_colLabels
.GetCount();
3187 for ( i
= n
; i
<= col
; i
++ )
3189 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3193 m_colLabels
[col
] = value
;
3198 //////////////////////////////////////////////////////////////////////
3199 //////////////////////////////////////////////////////////////////////
3201 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3203 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3204 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3205 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3206 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3207 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3208 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3211 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3213 const wxPoint
&pos
, const wxSize
&size
)
3214 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3219 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3223 // NO - don't do this because it will set both the x and y origin
3224 // coords to match the parent scrolled window and we just want to
3225 // set the y coord - MB
3227 // m_owner->PrepareDC( dc );
3230 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3231 dc
.SetDeviceOrigin( 0, -y
);
3233 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3234 m_owner
->DrawRowLabels( dc
);
3238 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3240 m_owner
->ProcessRowLabelMouseEvent( event
);
3244 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3246 m_owner
->GetEventHandler()->ProcessEvent(event
);
3250 // This seems to be required for wxMotif otherwise the mouse
3251 // cursor must be in the cell edit control to get key events
3253 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3255 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3258 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3260 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3265 //////////////////////////////////////////////////////////////////////
3267 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3269 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3270 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3271 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3272 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3273 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3274 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3277 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3279 const wxPoint
&pos
, const wxSize
&size
)
3280 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3285 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3289 // NO - don't do this because it will set both the x and y origin
3290 // coords to match the parent scrolled window and we just want to
3291 // set the x coord - MB
3293 // m_owner->PrepareDC( dc );
3296 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3297 dc
.SetDeviceOrigin( -x
, 0 );
3299 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3300 m_owner
->DrawColLabels( dc
);
3304 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3306 m_owner
->ProcessColLabelMouseEvent( event
);
3309 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3311 m_owner
->GetEventHandler()->ProcessEvent(event
);
3315 // This seems to be required for wxMotif otherwise the mouse
3316 // cursor must be in the cell edit control to get key events
3318 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3320 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3323 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3325 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3330 //////////////////////////////////////////////////////////////////////
3332 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3334 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3335 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3336 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3337 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3338 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3339 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3342 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3344 const wxPoint
&pos
, const wxSize
&size
)
3345 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3350 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3354 int client_height
= 0;
3355 int client_width
= 0;
3356 GetClientSize( &client_width
, &client_height
);
3358 dc
.SetPen( *wxBLACK_PEN
);
3359 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3360 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3362 dc
.SetPen( *wxWHITE_PEN
);
3363 dc
.DrawLine( 0, 0, client_width
, 0 );
3364 dc
.DrawLine( 0, 0, 0, client_height
);
3368 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3370 m_owner
->ProcessCornerLabelMouseEvent( event
);
3374 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3376 m_owner
->GetEventHandler()->ProcessEvent(event
);
3379 // This seems to be required for wxMotif otherwise the mouse
3380 // cursor must be in the cell edit control to get key events
3382 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3384 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3387 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3389 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3394 //////////////////////////////////////////////////////////////////////
3396 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3398 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3399 EVT_PAINT( wxGridWindow::OnPaint
)
3400 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3401 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3402 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3403 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3404 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3407 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3408 wxGridRowLabelWindow
*rowLblWin
,
3409 wxGridColLabelWindow
*colLblWin
,
3410 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3411 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3414 m_rowLabelWin
= rowLblWin
;
3415 m_colLabelWin
= colLblWin
;
3416 SetBackgroundColour( "WHITE" );
3420 wxGridWindow::~wxGridWindow()
3425 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3427 wxPaintDC
dc( this );
3428 m_owner
->PrepareDC( dc
);
3429 wxRegion reg
= GetUpdateRegion();
3430 m_owner
->CalcCellsExposed( reg
);
3431 m_owner
->DrawGridCellArea( dc
);
3432 #if WXGRID_DRAW_LINES
3433 m_owner
->DrawAllGridLines( dc
, reg
);
3435 m_owner
->DrawGridSpace( dc
);
3436 m_owner
->DrawHighlight( dc
);
3440 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3442 wxPanel::ScrollWindow( dx
, dy
, rect
);
3443 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3444 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3448 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3450 m_owner
->ProcessGridCellMouseEvent( event
);
3453 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3455 m_owner
->GetEventHandler()->ProcessEvent(event
);
3458 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3459 // cursor must be in the cell edit control to get key events
3461 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3463 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3466 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3468 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3471 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3476 //////////////////////////////////////////////////////////////////////
3479 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3481 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3482 EVT_PAINT( wxGrid::OnPaint
)
3483 EVT_SIZE( wxGrid::OnSize
)
3484 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3485 EVT_KEY_UP( wxGrid::OnKeyUp
)
3486 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3489 wxGrid::wxGrid( wxWindow
*parent
,
3494 const wxString
& name
)
3495 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3496 m_colMinWidths(GRID_HASH_SIZE
),
3497 m_rowMinHeights(GRID_HASH_SIZE
)
3506 wxSafeDecRef(m_defaultCellAttr
);
3508 #ifdef DEBUG_ATTR_CACHE
3509 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3510 wxPrintf(_T("wxGrid attribute cache statistics: "
3511 "total: %u, hits: %u (%u%%)\n"),
3512 total
, gs_nAttrCacheHits
,
3513 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3519 delete m_typeRegistry
;
3525 // ----- internal init and update functions
3528 void wxGrid::Create()
3530 m_created
= FALSE
; // set to TRUE by CreateGrid
3532 m_table
= (wxGridTableBase
*) NULL
;
3535 m_cellEditCtrlEnabled
= FALSE
;
3537 m_defaultCellAttr
= new wxGridCellAttr
;
3538 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3540 // Set default cell attributes
3541 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3542 m_defaultCellAttr
->SetFont(GetFont());
3543 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3544 m_defaultCellAttr
->SetTextColour(
3545 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3546 m_defaultCellAttr
->SetBackgroundColour(
3547 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3548 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3549 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3554 m_currentCellCoords
= wxGridNoCellCoords
;
3556 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3557 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3559 // create the type registry
3560 m_typeRegistry
= new wxGridTypeRegistry
;
3562 // subwindow components that make up the wxGrid
3563 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3568 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3573 m_colLabelWin
= new wxGridColLabelWindow( this,
3578 m_gridWin
= new wxGridWindow( this,
3585 SetTargetWindow( m_gridWin
);
3589 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3590 wxGrid::wxGridSelectionModes selmode
)
3592 wxCHECK_MSG( !m_created
,
3594 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3596 m_numRows
= numRows
;
3597 m_numCols
= numCols
;
3599 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3600 m_table
->SetView( this );
3602 m_selection
= new wxGridSelection( this, selmode
);
3609 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3613 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3616 m_selection
->SetSelectionMode( selmode
);
3619 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3620 wxGrid::wxGridSelectionModes selmode
)
3624 // RD: Actually, this should probably be allowed. I think it would be
3625 // nice to be able to switch multiple Tables in and out of a single
3626 // View at runtime. Is there anything in the implmentation that would
3629 // At least, you now have to cope with m_selection
3630 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3635 m_numRows
= table
->GetNumberRows();
3636 m_numCols
= table
->GetNumberCols();
3639 m_table
->SetView( this );
3642 m_selection
= new wxGridSelection( this, selmode
);
3653 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3654 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3656 if ( m_rowLabelWin
)
3658 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3662 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3665 m_labelTextColour
= wxColour( _T("BLACK") );
3668 m_attrCache
.row
= -1;
3670 // TODO: something better than this ?
3672 m_labelFont
= this->GetFont();
3673 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3675 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3676 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3678 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3679 m_colLabelVertAlign
= wxALIGN_TOP
;
3681 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3682 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3684 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3685 m_defaultRowHeight
+= 8;
3687 m_defaultRowHeight
+= 4;
3690 m_gridLineColour
= wxColour( 128, 128, 255 );
3691 m_gridLinesEnabled
= TRUE
;
3692 m_cellHighlightColour
= m_gridLineColour
;
3693 m_cellHighlightPenWidth
= 3;
3694 m_cellHighlightROPenWidth
= 1;
3696 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3697 m_winCapture
= (wxWindow
*)NULL
;
3698 m_canDragRowSize
= TRUE
;
3699 m_canDragColSize
= TRUE
;
3700 m_canDragGridSize
= TRUE
;
3702 m_dragRowOrCol
= -1;
3703 m_isDragging
= FALSE
;
3704 m_startDragPos
= wxDefaultPosition
;
3706 m_waitForSlowClick
= FALSE
;
3708 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3709 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3711 m_currentCellCoords
= wxGridNoCellCoords
;
3713 m_selectingTopLeft
= wxGridNoCellCoords
;
3714 m_selectingBottomRight
= wxGridNoCellCoords
;
3715 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3716 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3718 m_editable
= TRUE
; // default for whole grid
3720 m_inOnKeyDown
= FALSE
;
3729 // ----------------------------------------------------------------------------
3730 // the idea is to call these functions only when necessary because they create
3731 // quite big arrays which eat memory mostly unnecessary - in particular, if
3732 // default widths/heights are used for all rows/columns, we may not use these
3735 // with some extra code, it should be possible to only store the
3736 // widths/heights different from default ones but this will be done later...
3737 // ----------------------------------------------------------------------------
3739 void wxGrid::InitRowHeights()
3741 m_rowHeights
.Empty();
3742 m_rowBottoms
.Empty();
3744 m_rowHeights
.Alloc( m_numRows
);
3745 m_rowBottoms
.Alloc( m_numRows
);
3748 for ( int i
= 0; i
< m_numRows
; i
++ )
3750 m_rowHeights
.Add( m_defaultRowHeight
);
3751 rowBottom
+= m_defaultRowHeight
;
3752 m_rowBottoms
.Add( rowBottom
);
3756 void wxGrid::InitColWidths()
3758 m_colWidths
.Empty();
3759 m_colRights
.Empty();
3761 m_colWidths
.Alloc( m_numCols
);
3762 m_colRights
.Alloc( m_numCols
);
3764 for ( int i
= 0; i
< m_numCols
; i
++ )
3766 m_colWidths
.Add( m_defaultColWidth
);
3767 colRight
+= m_defaultColWidth
;
3768 m_colRights
.Add( colRight
);
3772 int wxGrid::GetColWidth(int col
) const
3774 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3777 int wxGrid::GetColLeft(int col
) const
3779 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3780 : m_colRights
[col
] - m_colWidths
[col
];
3783 int wxGrid::GetColRight(int col
) const
3785 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3789 int wxGrid::GetRowHeight(int row
) const
3791 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3794 int wxGrid::GetRowTop(int row
) const
3796 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3797 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3800 int wxGrid::GetRowBottom(int row
) const
3802 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3803 : m_rowBottoms
[row
];
3806 void wxGrid::CalcDimensions()
3809 GetClientSize( &cw
, &ch
);
3811 if ( m_colLabelWin
->IsShown() )
3812 cw
-= m_rowLabelWidth
;
3813 if ( m_rowLabelWin
->IsShown() )
3814 ch
-= m_colLabelHeight
;
3817 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3818 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3820 // preserve (more or less) the previous position
3822 GetViewStart( &x
, &y
);
3823 // maybe we don't need scrollbars at all? and if we do, transform w and h
3824 // from pixels into logical units
3831 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3841 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3846 // do set scrollbar parameters
3847 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3848 w
, h
, x
, y
, (GetBatchCount() != 0));
3852 void wxGrid::CalcWindowSizes()
3855 GetClientSize( &cw
, &ch
);
3857 if ( m_cornerLabelWin
->IsShown() )
3858 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3860 if ( m_colLabelWin
->IsShown() )
3861 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3863 if ( m_rowLabelWin
->IsShown() )
3864 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3866 if ( m_gridWin
->IsShown() )
3867 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3871 // this is called when the grid table sends a message to say that it
3872 // has been redimensioned
3874 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3877 bool result
= FALSE
;
3880 // if we were using the default widths/heights so far, we must change them
3882 if ( m_colWidths
.IsEmpty() )
3887 if ( m_rowHeights
.IsEmpty() )
3893 switch ( msg
.GetId() )
3895 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3897 size_t pos
= msg
.GetCommandInt();
3898 int numRows
= msg
.GetCommandInt2();
3900 m_numRows
+= numRows
;
3902 if ( !m_rowHeights
.IsEmpty() )
3904 for ( i
= 0; i
< numRows
; i
++ )
3906 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3907 m_rowBottoms
.Insert( 0, pos
);
3911 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3913 for ( i
= pos
; i
< m_numRows
; i
++ )
3915 bottom
+= m_rowHeights
[i
];
3916 m_rowBottoms
[i
] = bottom
;
3919 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3921 // if we have just inserted cols into an empty grid the current
3922 // cell will be undefined...
3924 SetCurrentCell( 0, 0 );
3926 m_selection
->UpdateRows( pos
, numRows
);
3927 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3929 attrProvider
->UpdateAttrRows( pos
, numRows
);
3931 if ( !GetBatchCount() )
3934 m_rowLabelWin
->Refresh();
3940 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3942 int numRows
= msg
.GetCommandInt();
3943 int oldNumRows
= m_numRows
;
3944 m_numRows
+= numRows
;
3946 if ( !m_rowHeights
.IsEmpty() )
3948 for ( i
= 0; i
< numRows
; i
++ )
3950 m_rowHeights
.Add( m_defaultRowHeight
);
3951 m_rowBottoms
.Add( 0 );
3955 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3957 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3959 bottom
+= m_rowHeights
[i
];
3960 m_rowBottoms
[i
] = bottom
;
3963 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3965 // if we have just inserted cols into an empty grid the current
3966 // cell will be undefined...
3968 SetCurrentCell( 0, 0 );
3970 if ( !GetBatchCount() )
3973 m_rowLabelWin
->Refresh();
3979 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3981 size_t pos
= msg
.GetCommandInt();
3982 int numRows
= msg
.GetCommandInt2();
3983 m_numRows
-= numRows
;
3985 if ( !m_rowHeights
.IsEmpty() )
3987 for ( i
= 0; i
< numRows
; i
++ )
3989 m_rowHeights
.Remove( pos
);
3990 m_rowBottoms
.Remove( pos
);
3994 for ( i
= 0; i
< m_numRows
; i
++ )
3996 h
+= m_rowHeights
[i
];
3997 m_rowBottoms
[i
] = h
;
4002 m_currentCellCoords
= wxGridNoCellCoords
;
4006 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4007 m_currentCellCoords
.Set( 0, 0 );
4009 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4010 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4012 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4013 // ifdef'd out following patch from Paul Gammans
4015 // No need to touch column attributes, unless we
4016 // removed _all_ rows, in this case, we remove
4017 // all column attributes.
4018 // I hate to do this here, but the
4019 // needed data is not available inside UpdateAttrRows.
4020 if ( !GetNumberRows() )
4021 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4024 if ( !GetBatchCount() )
4027 m_rowLabelWin
->Refresh();
4033 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4035 size_t pos
= msg
.GetCommandInt();
4036 int numCols
= msg
.GetCommandInt2();
4037 m_numCols
+= numCols
;
4039 if ( !m_colWidths
.IsEmpty() )
4041 for ( i
= 0; i
< numCols
; i
++ )
4043 m_colWidths
.Insert( m_defaultColWidth
, pos
);
4044 m_colRights
.Insert( 0, pos
);
4048 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4050 for ( i
= pos
; i
< m_numCols
; i
++ )
4052 right
+= m_colWidths
[i
];
4053 m_colRights
[i
] = right
;
4056 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4058 // if we have just inserted cols into an empty grid the current
4059 // cell will be undefined...
4061 SetCurrentCell( 0, 0 );
4063 m_selection
->UpdateCols( pos
, numCols
);
4064 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4066 attrProvider
->UpdateAttrCols( pos
, numCols
);
4067 if ( !GetBatchCount() )
4070 m_colLabelWin
->Refresh();
4077 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4079 int numCols
= msg
.GetCommandInt();
4080 int oldNumCols
= m_numCols
;
4081 m_numCols
+= numCols
;
4082 if ( !m_colWidths
.IsEmpty() )
4084 for ( i
= 0; i
< numCols
; i
++ )
4086 m_colWidths
.Add( m_defaultColWidth
);
4087 m_colRights
.Add( 0 );
4091 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4093 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4095 right
+= m_colWidths
[i
];
4096 m_colRights
[i
] = right
;
4099 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4101 // if we have just inserted cols into an empty grid the current
4102 // cell will be undefined...
4104 SetCurrentCell( 0, 0 );
4106 if ( !GetBatchCount() )
4109 m_colLabelWin
->Refresh();
4115 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4117 size_t pos
= msg
.GetCommandInt();
4118 int numCols
= msg
.GetCommandInt2();
4119 m_numCols
-= numCols
;
4121 if ( !m_colWidths
.IsEmpty() )
4123 for ( i
= 0; i
< numCols
; i
++ )
4125 m_colWidths
.Remove( pos
);
4126 m_colRights
.Remove( pos
);
4130 for ( i
= 0; i
< m_numCols
; i
++ )
4132 w
+= m_colWidths
[i
];
4138 m_currentCellCoords
= wxGridNoCellCoords
;
4142 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4143 m_currentCellCoords
.Set( 0, 0 );
4145 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4146 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4148 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4149 // ifdef'd out following patch from Paul Gammans
4151 // No need to touch row attributes, unless we
4152 // removed _all_ columns, in this case, we remove
4153 // all row attributes.
4154 // I hate to do this here, but the
4155 // needed data is not available inside UpdateAttrCols.
4156 if ( !GetNumberCols() )
4157 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4160 if ( !GetBatchCount() )
4163 m_colLabelWin
->Refresh();
4170 if (result
&& !GetBatchCount() )
4171 m_gridWin
->Refresh();
4176 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4178 wxRegionIterator
iter( reg
);
4181 m_rowLabelsExposed
.Empty();
4188 // TODO: remove this when we can...
4189 // There is a bug in wxMotif that gives garbage update
4190 // rectangles if you jump-scroll a long way by clicking the
4191 // scrollbar with middle button. This is a work-around
4193 #if defined(__WXMOTIF__)
4195 m_gridWin
->GetClientSize( &cw
, &ch
);
4196 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4197 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4200 // logical bounds of update region
4203 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4204 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4206 // find the row labels within these bounds
4209 for ( row
= 0; row
< m_numRows
; row
++ )
4211 if ( GetRowBottom(row
) < top
)
4214 if ( GetRowTop(row
) > bottom
)
4217 m_rowLabelsExposed
.Add( row
);
4225 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4227 wxRegionIterator
iter( reg
);
4230 m_colLabelsExposed
.Empty();
4237 // TODO: remove this when we can...
4238 // There is a bug in wxMotif that gives garbage update
4239 // rectangles if you jump-scroll a long way by clicking the
4240 // scrollbar with middle button. This is a work-around
4242 #if defined(__WXMOTIF__)
4244 m_gridWin
->GetClientSize( &cw
, &ch
);
4245 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4246 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4249 // logical bounds of update region
4252 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4253 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4255 // find the cells within these bounds
4258 for ( col
= 0; col
< m_numCols
; col
++ )
4260 if ( GetColRight(col
) < left
)
4263 if ( GetColLeft(col
) > right
)
4266 m_colLabelsExposed
.Add( col
);
4274 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4276 wxRegionIterator
iter( reg
);
4279 m_cellsExposed
.Empty();
4280 m_rowsExposed
.Empty();
4281 m_colsExposed
.Empty();
4283 int left
, top
, right
, bottom
;
4288 // TODO: remove this when we can...
4289 // There is a bug in wxMotif that gives garbage update
4290 // rectangles if you jump-scroll a long way by clicking the
4291 // scrollbar with middle button. This is a work-around
4293 #if defined(__WXMOTIF__)
4295 m_gridWin
->GetClientSize( &cw
, &ch
);
4296 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4297 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4298 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4299 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4302 // logical bounds of update region
4304 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4305 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4307 // find the cells within these bounds
4310 for ( row
= 0; row
< m_numRows
; row
++ )
4312 if ( GetRowBottom(row
) <= top
)
4315 if ( GetRowTop(row
) > bottom
)
4318 m_rowsExposed
.Add( row
);
4320 for ( col
= 0; col
< m_numCols
; col
++ )
4322 if ( GetColRight(col
) <= left
)
4325 if ( GetColLeft(col
) > right
)
4328 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4329 m_colsExposed
.Add( col
);
4330 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4339 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4342 wxPoint
pos( event
.GetPosition() );
4343 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4345 if ( event
.Dragging() )
4347 m_isDragging
= TRUE
;
4349 if ( event
.LeftIsDown() )
4351 switch( m_cursorMode
)
4353 case WXGRID_CURSOR_RESIZE_ROW
:
4355 int cw
, ch
, left
, dummy
;
4356 m_gridWin
->GetClientSize( &cw
, &ch
);
4357 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4359 wxClientDC
dc( m_gridWin
);
4362 GetRowTop(m_dragRowOrCol
) +
4363 GetRowMinimalHeight(m_dragRowOrCol
) );
4364 dc
.SetLogicalFunction(wxINVERT
);
4365 if ( m_dragLastPos
>= 0 )
4367 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4369 dc
.DrawLine( left
, y
, left
+cw
, y
);
4374 case WXGRID_CURSOR_SELECT_ROW
:
4375 if ( (row
= YToRow( y
)) >= 0 )
4377 m_selection
->SelectRow( row
,
4378 event
.ControlDown(),
4384 // default label to suppress warnings about "enumeration value
4385 // 'xxx' not handled in switch
4393 m_isDragging
= FALSE
;
4396 // ------------ Entering or leaving the window
4398 if ( event
.Entering() || event
.Leaving() )
4400 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4404 // ------------ Left button pressed
4406 else if ( event
.LeftDown() )
4408 // don't send a label click event for a hit on the
4409 // edge of the row label - this is probably the user
4410 // wanting to resize the row
4412 if ( YToEdgeOfRow(y
) < 0 )
4416 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4418 if ( !event
.ShiftDown() && !event
.ControlDown() )
4420 if ( event
.ShiftDown() )
4421 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4424 GetNumberCols() - 1,
4425 event
.ControlDown(),
4430 m_selection
->SelectRow( row
,
4431 event
.ControlDown(),
4435 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4440 // starting to drag-resize a row
4442 if ( CanDragRowSize() )
4443 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4448 // ------------ Left double click
4450 else if (event
.LeftDClick() )
4452 if ( YToEdgeOfRow(y
) < 0 )
4455 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4460 // ------------ Left button released
4462 else if ( event
.LeftUp() )
4464 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4466 DoEndDragResizeRow();
4468 // Note: we are ending the event *after* doing
4469 // default processing in this case
4471 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4474 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4479 // ------------ Right button down
4481 else if ( event
.RightDown() )
4484 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4486 // no default action at the moment
4491 // ------------ Right double click
4493 else if ( event
.RightDClick() )
4496 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4498 // no default action at the moment
4503 // ------------ No buttons down and mouse moving
4505 else if ( event
.Moving() )
4507 m_dragRowOrCol
= YToEdgeOfRow( y
);
4508 if ( m_dragRowOrCol
>= 0 )
4510 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4512 // don't capture the mouse yet
4513 if ( CanDragRowSize() )
4514 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4517 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4519 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4525 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4528 wxPoint
pos( event
.GetPosition() );
4529 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4531 if ( event
.Dragging() )
4533 m_isDragging
= TRUE
;
4535 if ( event
.LeftIsDown() )
4537 switch( m_cursorMode
)
4539 case WXGRID_CURSOR_RESIZE_COL
:
4541 int cw
, ch
, dummy
, top
;
4542 m_gridWin
->GetClientSize( &cw
, &ch
);
4543 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4545 wxClientDC
dc( m_gridWin
);
4548 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4549 GetColMinimalWidth(m_dragRowOrCol
));
4550 dc
.SetLogicalFunction(wxINVERT
);
4551 if ( m_dragLastPos
>= 0 )
4553 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4555 dc
.DrawLine( x
, top
, x
, top
+ch
);
4560 case WXGRID_CURSOR_SELECT_COL
:
4561 if ( (col
= XToCol( x
)) >= 0 )
4563 m_selection
->SelectCol( col
,
4564 event
.ControlDown(),
4570 // default label to suppress warnings about "enumeration value
4571 // 'xxx' not handled in switch
4579 m_isDragging
= FALSE
;
4582 // ------------ Entering or leaving the window
4584 if ( event
.Entering() || event
.Leaving() )
4586 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4590 // ------------ Left button pressed
4592 else if ( event
.LeftDown() )
4594 // don't send a label click event for a hit on the
4595 // edge of the col label - this is probably the user
4596 // wanting to resize the col
4598 if ( XToEdgeOfCol(x
) < 0 )
4602 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4604 if ( !event
.ShiftDown() && !event
.ControlDown() )
4606 if ( event
.ShiftDown() )
4607 m_selection
->SelectBlock( 0,
4608 m_currentCellCoords
.GetCol(),
4609 GetNumberRows() - 1, col
,
4610 event
.ControlDown(),
4615 m_selection
->SelectCol( col
,
4616 event
.ControlDown(),
4620 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4625 // starting to drag-resize a col
4627 if ( CanDragColSize() )
4628 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4633 // ------------ Left double click
4635 if ( event
.LeftDClick() )
4637 if ( XToEdgeOfCol(x
) < 0 )
4640 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4645 // ------------ Left button released
4647 else if ( event
.LeftUp() )
4649 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4651 DoEndDragResizeCol();
4653 // Note: we are ending the event *after* doing
4654 // default processing in this case
4656 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4659 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4664 // ------------ Right button down
4666 else if ( event
.RightDown() )
4669 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4671 // no default action at the moment
4676 // ------------ Right double click
4678 else if ( event
.RightDClick() )
4681 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4683 // no default action at the moment
4688 // ------------ No buttons down and mouse moving
4690 else if ( event
.Moving() )
4692 m_dragRowOrCol
= XToEdgeOfCol( x
);
4693 if ( m_dragRowOrCol
>= 0 )
4695 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4697 // don't capture the cursor yet
4698 if ( CanDragColSize() )
4699 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4702 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4704 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4710 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4712 if ( event
.LeftDown() )
4714 // indicate corner label by having both row and
4717 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4723 else if ( event
.LeftDClick() )
4725 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4728 else if ( event
.RightDown() )
4730 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4732 // no default action at the moment
4736 else if ( event
.RightDClick() )
4738 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4740 // no default action at the moment
4745 void wxGrid::ChangeCursorMode(CursorMode mode
,
4750 static const wxChar
*cursorModes
[] =
4759 wxLogTrace(_T("grid"),
4760 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4761 win
== m_colLabelWin
? _T("colLabelWin")
4762 : win
? _T("rowLabelWin")
4764 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4765 #endif // __WXDEBUG__
4767 if ( mode
== m_cursorMode
&&
4768 win
== m_winCapture
&&
4769 captureMouse
== (m_winCapture
!= NULL
))
4774 // by default use the grid itself
4780 m_winCapture
->ReleaseMouse();
4781 m_winCapture
= (wxWindow
*)NULL
;
4784 m_cursorMode
= mode
;
4786 switch ( m_cursorMode
)
4788 case WXGRID_CURSOR_RESIZE_ROW
:
4789 win
->SetCursor( m_rowResizeCursor
);
4792 case WXGRID_CURSOR_RESIZE_COL
:
4793 win
->SetCursor( m_colResizeCursor
);
4797 win
->SetCursor( *wxSTANDARD_CURSOR
);
4800 // we need to capture mouse when resizing
4801 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4802 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4804 if ( captureMouse
&& resize
)
4806 win
->CaptureMouse();
4811 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4814 wxPoint
pos( event
.GetPosition() );
4815 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4817 wxGridCellCoords coords
;
4818 XYToCell( x
, y
, coords
);
4820 if ( event
.Dragging() )
4822 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4824 // Don't start doing anything until the mouse has been drug at
4825 // least 3 pixels in any direction...
4828 if (m_startDragPos
== wxDefaultPosition
)
4830 m_startDragPos
= pos
;
4833 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4837 m_isDragging
= TRUE
;
4838 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4840 // Hide the edit control, so it
4841 // won't interfer with drag-shrinking.
4842 if ( IsCellEditControlShown() )
4844 HideCellEditControl();
4845 SaveEditControlValue();
4848 // Have we captured the mouse yet?
4851 m_winCapture
= m_gridWin
;
4852 m_winCapture
->CaptureMouse();
4855 if ( coords
!= wxGridNoCellCoords
)
4857 if ( event
.ControlDown() )
4859 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4860 m_selectingKeyboard
= coords
;
4861 HighlightBlock ( m_selectingKeyboard
, coords
);
4865 if ( !IsSelection() )
4867 HighlightBlock( coords
, coords
);
4871 HighlightBlock( m_currentCellCoords
, coords
);
4875 if (! IsVisible(coords
))
4877 MakeCellVisible(coords
);
4878 // TODO: need to introduce a delay or something here. The
4879 // scrolling is way to fast, at least on MSW - also on GTK.
4883 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4885 int cw
, ch
, left
, dummy
;
4886 m_gridWin
->GetClientSize( &cw
, &ch
);
4887 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4889 wxClientDC
dc( m_gridWin
);
4891 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4892 GetRowMinimalHeight(m_dragRowOrCol
) );
4893 dc
.SetLogicalFunction(wxINVERT
);
4894 if ( m_dragLastPos
>= 0 )
4896 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4898 dc
.DrawLine( left
, y
, left
+cw
, y
);
4901 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4903 int cw
, ch
, dummy
, top
;
4904 m_gridWin
->GetClientSize( &cw
, &ch
);
4905 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4907 wxClientDC
dc( m_gridWin
);
4909 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4910 GetColMinimalWidth(m_dragRowOrCol
) );
4911 dc
.SetLogicalFunction(wxINVERT
);
4912 if ( m_dragLastPos
>= 0 )
4914 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4916 dc
.DrawLine( x
, top
, x
, top
+ch
);
4923 m_isDragging
= FALSE
;
4924 m_startDragPos
= wxDefaultPosition
;
4926 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4927 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4930 if ( event
.Entering() || event
.Leaving() )
4932 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4933 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4938 // ------------ Left button pressed
4940 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4942 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4947 if ( !event
.ControlDown() )
4949 if ( event
.ShiftDown() )
4951 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4952 m_currentCellCoords
.GetCol(),
4955 event
.ControlDown(),
4960 else if ( XToEdgeOfCol(x
) < 0 &&
4961 YToEdgeOfRow(y
) < 0 )
4963 DisableCellEditControl();
4964 MakeCellVisible( coords
);
4966 // if this is the second click on this cell then start
4968 if ( m_waitForSlowClick
&&
4969 (coords
== m_currentCellCoords
) &&
4970 CanEnableCellControl())
4972 EnableCellEditControl();
4974 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4975 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4978 editor
->StartingClick();
4982 m_waitForSlowClick
= FALSE
;
4986 if ( event
.ControlDown() )
4988 m_selection
->ToggleCellSelection( coords
.GetRow(),
4990 event
.ControlDown(),
4994 m_selectingTopLeft
= wxGridNoCellCoords
;
4995 m_selectingBottomRight
= wxGridNoCellCoords
;
4996 m_selectingKeyboard
= coords
;
5000 SetCurrentCell( coords
);
5001 if ( m_selection
->GetSelectionMode()
5002 != wxGrid::wxGridSelectCells
)
5003 HighlightBlock( coords
, coords
);
5005 m_waitForSlowClick
= TRUE
;
5012 // ------------ Left double click
5014 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5016 DisableCellEditControl();
5018 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5020 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5028 // ------------ Left button released
5030 else if ( event
.LeftUp() )
5032 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5034 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5035 m_selectingBottomRight
!= wxGridNoCellCoords
)
5039 m_winCapture
->ReleaseMouse();
5040 m_winCapture
= NULL
;
5042 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5043 m_selectingTopLeft
.GetCol(),
5044 m_selectingBottomRight
.GetRow(),
5045 m_selectingBottomRight
.GetCol(),
5046 event
.ControlDown(),
5050 m_selectingTopLeft
= wxGridNoCellCoords
;
5051 m_selectingBottomRight
= wxGridNoCellCoords
;
5054 // Show the edit control, if it has been hidden for
5056 ShowCellEditControl();
5058 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5060 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5061 DoEndDragResizeRow();
5063 // Note: we are ending the event *after* doing
5064 // default processing in this case
5066 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5068 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5070 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5071 DoEndDragResizeCol();
5073 // Note: we are ending the event *after* doing
5074 // default processing in this case
5076 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5083 // ------------ Right button down
5085 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5087 DisableCellEditControl();
5088 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5093 // no default action at the moment
5098 // ------------ Right double click
5100 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5102 DisableCellEditControl();
5103 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5108 // no default action at the moment
5112 // ------------ Moving and no button action
5114 else if ( event
.Moving() && !event
.IsButton() )
5116 int dragRow
= YToEdgeOfRow( y
);
5117 int dragCol
= XToEdgeOfCol( x
);
5119 // Dragging on the corner of a cell to resize in both
5120 // directions is not implemented yet...
5122 if ( dragRow
>= 0 && dragCol
>= 0 )
5124 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5130 m_dragRowOrCol
= dragRow
;
5132 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5134 if ( CanDragRowSize() && CanDragGridSize() )
5135 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5140 m_dragRowOrCol
= dragCol
;
5148 m_dragRowOrCol
= dragCol
;
5150 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5152 if ( CanDragColSize() && CanDragGridSize() )
5153 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5159 // Neither on a row or col edge
5161 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5163 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5169 void wxGrid::DoEndDragResizeRow()
5171 if ( m_dragLastPos
>= 0 )
5173 // erase the last line and resize the row
5175 int cw
, ch
, left
, dummy
;
5176 m_gridWin
->GetClientSize( &cw
, &ch
);
5177 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5179 wxClientDC
dc( m_gridWin
);
5181 dc
.SetLogicalFunction( wxINVERT
);
5182 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5183 HideCellEditControl();
5184 SaveEditControlValue();
5186 int rowTop
= GetRowTop(m_dragRowOrCol
);
5187 SetRowSize( m_dragRowOrCol
,
5188 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5190 if ( !GetBatchCount() )
5192 // Only needed to get the correct rect.y:
5193 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5195 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5196 rect
.width
= m_rowLabelWidth
;
5197 rect
.height
= ch
- rect
.y
;
5198 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5200 m_gridWin
->Refresh( FALSE
, &rect
);
5203 ShowCellEditControl();
5208 void wxGrid::DoEndDragResizeCol()
5210 if ( m_dragLastPos
>= 0 )
5212 // erase the last line and resize the col
5214 int cw
, ch
, dummy
, top
;
5215 m_gridWin
->GetClientSize( &cw
, &ch
);
5216 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5218 wxClientDC
dc( m_gridWin
);
5220 dc
.SetLogicalFunction( wxINVERT
);
5221 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5222 HideCellEditControl();
5223 SaveEditControlValue();
5225 int colLeft
= GetColLeft(m_dragRowOrCol
);
5226 SetColSize( m_dragRowOrCol
,
5227 wxMax( m_dragLastPos
- colLeft
,
5228 GetColMinimalWidth(m_dragRowOrCol
) ) );
5230 if ( !GetBatchCount() )
5232 // Only needed to get the correct rect.x:
5233 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5235 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5236 rect
.width
= cw
- rect
.x
;
5237 rect
.height
= m_colLabelHeight
;
5238 m_colLabelWin
->Refresh( TRUE
, &rect
);
5240 m_gridWin
->Refresh( FALSE
, &rect
);
5243 ShowCellEditControl();
5250 // ------ interaction with data model
5252 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5254 switch ( msg
.GetId() )
5256 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5257 return GetModelValues();
5259 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5260 return SetModelValues();
5262 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5263 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5264 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5265 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5266 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5267 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5268 return Redimension( msg
);
5277 // The behaviour of this function depends on the grid table class
5278 // Clear() function. For the default wxGridStringTable class the
5279 // behavious is to replace all cell contents with wxEmptyString but
5280 // not to change the number of rows or cols.
5282 void wxGrid::ClearGrid()
5286 if (IsCellEditControlEnabled())
5287 DisableCellEditControl();
5290 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5295 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5297 // TODO: something with updateLabels flag
5301 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5307 if (IsCellEditControlEnabled())
5308 DisableCellEditControl();
5310 return m_table
->InsertRows( pos
, numRows
);
5312 // the table will have sent the results of the insert row
5313 // operation to this view object as a grid table message
5319 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5321 // TODO: something with updateLabels flag
5325 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5329 return ( m_table
&& m_table
->AppendRows( numRows
) );
5330 // the table will have sent the results of the append row
5331 // operation to this view object as a grid table message
5335 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5337 // TODO: something with updateLabels flag
5341 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5347 if (IsCellEditControlEnabled())
5348 DisableCellEditControl();
5350 return (m_table
->DeleteRows( pos
, numRows
));
5351 // the table will have sent the results of the delete row
5352 // operation to this view object as a grid table message
5358 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5360 // TODO: something with updateLabels flag
5364 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5370 if (IsCellEditControlEnabled())
5371 DisableCellEditControl();
5373 return m_table
->InsertCols( pos
, numCols
);
5374 // the table will have sent the results of the insert col
5375 // operation to this view object as a grid table message
5381 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5383 // TODO: something with updateLabels flag
5387 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5391 return ( m_table
&& m_table
->AppendCols( numCols
) );
5392 // the table will have sent the results of the append col
5393 // operation to this view object as a grid table message
5397 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5399 // TODO: something with updateLabels flag
5403 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5409 if (IsCellEditControlEnabled())
5410 DisableCellEditControl();
5412 return ( m_table
->DeleteCols( pos
, numCols
) );
5413 // the table will have sent the results of the delete col
5414 // operation to this view object as a grid table message
5422 // ----- event handlers
5425 // Generate a grid event based on a mouse event and
5426 // return the result of ProcessEvent()
5428 bool wxGrid::SendEvent( const wxEventType type
,
5430 wxMouseEvent
& mouseEv
)
5432 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5434 int rowOrCol
= (row
== -1 ? col
: row
);
5436 wxGridSizeEvent
gridEvt( GetId(),
5440 mouseEv
.GetX() + GetRowLabelSize(),
5441 mouseEv
.GetY() + GetColLabelSize(),
5442 mouseEv
.ControlDown(),
5443 mouseEv
.ShiftDown(),
5445 mouseEv
.MetaDown() );
5446 return GetEventHandler()->ProcessEvent(gridEvt
);
5448 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5450 // Right now, it should _never_ end up here!
5451 wxGridRangeSelectEvent
gridEvt( GetId(),
5455 m_selectingBottomRight
,
5457 mouseEv
.ControlDown(),
5458 mouseEv
.ShiftDown(),
5460 mouseEv
.MetaDown() );
5462 return GetEventHandler()->ProcessEvent(gridEvt
);
5466 wxGridEvent
gridEvt( GetId(),
5470 mouseEv
.GetX() + GetRowLabelSize(),
5471 mouseEv
.GetY() + GetColLabelSize(),
5473 mouseEv
.ControlDown(),
5474 mouseEv
.ShiftDown(),
5476 mouseEv
.MetaDown() );
5477 return GetEventHandler()->ProcessEvent(gridEvt
);
5482 // Generate a grid event of specified type and return the result
5483 // of ProcessEvent().
5485 bool wxGrid::SendEvent( const wxEventType type
,
5488 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5490 int rowOrCol
= (row
== -1 ? col
: row
);
5492 wxGridSizeEvent
gridEvt( GetId(),
5497 return GetEventHandler()->ProcessEvent(gridEvt
);
5501 wxGridEvent
gridEvt( GetId(),
5506 return GetEventHandler()->ProcessEvent(gridEvt
);
5511 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5513 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5517 // This is just here to make sure that CalcDimensions gets called when
5518 // the grid view is resized... then the size event is skipped to allow
5519 // the box sizers to handle everything
5521 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5528 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5530 if ( m_inOnKeyDown
)
5532 // shouldn't be here - we are going round in circles...
5534 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5537 m_inOnKeyDown
= TRUE
;
5539 // propagate the event up and see if it gets processed
5541 wxWindow
*parent
= GetParent();
5542 wxKeyEvent
keyEvt( event
);
5543 keyEvt
.SetEventObject( parent
);
5545 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5548 // try local handlers
5550 switch ( event
.KeyCode() )
5553 if ( event
.ControlDown() )
5555 MoveCursorUpBlock( event
.ShiftDown() );
5559 MoveCursorUp( event
.ShiftDown() );
5564 if ( event
.ControlDown() )
5566 MoveCursorDownBlock( event
.ShiftDown() );
5570 MoveCursorDown( event
.ShiftDown() );
5575 if ( event
.ControlDown() )
5577 MoveCursorLeftBlock( event
.ShiftDown() );
5581 MoveCursorLeft( event
.ShiftDown() );
5586 if ( event
.ControlDown() )
5588 MoveCursorRightBlock( event
.ShiftDown() );
5592 MoveCursorRight( event
.ShiftDown() );
5597 case WXK_NUMPAD_ENTER
:
5598 if ( event
.ControlDown() )
5600 event
.Skip(); // to let the edit control have the return
5604 if ( GetGridCursorRow() < GetNumberRows()-1 )
5606 MoveCursorDown( event
.ShiftDown() );
5610 // at the bottom of a column
5611 HideCellEditControl();
5612 SaveEditControlValue();
5622 if (event
.ShiftDown())
5624 if ( GetGridCursorCol() > 0 )
5626 MoveCursorLeft( FALSE
);
5631 HideCellEditControl();
5632 SaveEditControlValue();
5637 if ( GetGridCursorCol() < GetNumberCols()-1 )
5639 MoveCursorRight( FALSE
);
5644 HideCellEditControl();
5645 SaveEditControlValue();
5651 if ( event
.ControlDown() )
5653 MakeCellVisible( 0, 0 );
5654 SetCurrentCell( 0, 0 );
5663 if ( event
.ControlDown() )
5665 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5666 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5683 if ( event
.ControlDown() )
5685 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5686 m_currentCellCoords
.GetCol(),
5687 event
.ControlDown(),
5693 if ( !IsEditable() )
5695 MoveCursorRight( FALSE
);
5698 // Otherwise fall through to default
5701 // is it possible to edit the current cell at all?
5702 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5704 // yes, now check whether the cells editor accepts the key
5705 int row
= m_currentCellCoords
.GetRow();
5706 int col
= m_currentCellCoords
.GetCol();
5707 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5708 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5710 // <F2> is special and will always start editing, for
5711 // other keys - ask the editor itself
5712 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5713 || editor
->IsAcceptedKey(event
) )
5715 EnableCellEditControl();
5716 editor
->StartingKey(event
);
5728 // let others process char events with modifiers or all
5729 // char events for readonly cells
5736 m_inOnKeyDown
= FALSE
;
5739 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5741 // try local handlers
5743 if ( event
.KeyCode() == WXK_SHIFT
)
5745 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5746 m_selectingBottomRight
!= wxGridNoCellCoords
)
5747 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5748 m_selectingTopLeft
.GetCol(),
5749 m_selectingBottomRight
.GetRow(),
5750 m_selectingBottomRight
.GetCol(),
5751 event
.ControlDown(),
5755 m_selectingTopLeft
= wxGridNoCellCoords
;
5756 m_selectingBottomRight
= wxGridNoCellCoords
;
5757 m_selectingKeyboard
= wxGridNoCellCoords
;
5761 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5765 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5767 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5769 // the event has been intercepted - do nothing
5773 wxClientDC
dc(m_gridWin
);
5776 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5778 HideCellEditControl();
5779 DisableCellEditControl();
5781 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5784 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5785 if ( !m_gridLinesEnabled
)
5793 CalcCellsExposed( r
);
5795 // Otherwise refresh redraws the highlight!
5796 m_currentCellCoords
= coords
;
5798 DrawGridCellArea(dc
);
5799 DrawAllGridLines( dc
, r
);
5803 m_currentCellCoords
= coords
;
5805 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5806 DrawCellHighlight(dc
, attr
);
5811 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5814 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5816 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5819 rightCol
= GetNumberCols() - 1;
5821 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5824 bottomRow
= GetNumberRows() - 1;
5826 if ( topRow
> bottomRow
)
5833 if ( leftCol
> rightCol
)
5840 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5841 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5843 if ( m_selectingTopLeft
!= updateTopLeft
||
5844 m_selectingBottomRight
!= updateBottomRight
)
5846 // Compute two optimal update rectangles:
5847 // Either one rectangle is a real subset of the
5848 // other, or they are (almost) disjoint!
5850 bool need_refresh
[4];
5854 need_refresh
[3] = FALSE
;
5857 // Store intermediate values
5858 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5859 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5860 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5861 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5863 // Determine the outer/inner coordinates.
5864 if (oldLeft
> leftCol
)
5870 if (oldTop
> topRow
)
5876 if (oldRight
< rightCol
)
5879 oldRight
= rightCol
;
5882 if (oldBottom
< bottomRow
)
5885 oldBottom
= bottomRow
;
5889 // Now, either the stuff marked old is the outer
5890 // rectangle or we don't have a situation where one
5891 // is contained in the other.
5893 if ( oldLeft
< leftCol
)
5895 need_refresh
[0] = TRUE
;
5896 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5898 wxGridCellCoords ( oldBottom
,
5902 if ( oldTop
< topRow
)
5904 need_refresh
[1] = TRUE
;
5905 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5907 wxGridCellCoords ( topRow
- 1,
5911 if ( oldRight
> rightCol
)
5913 need_refresh
[2] = TRUE
;
5914 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5916 wxGridCellCoords ( oldBottom
,
5920 if ( oldBottom
> bottomRow
)
5922 need_refresh
[3] = TRUE
;
5923 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5925 wxGridCellCoords ( oldBottom
,
5931 m_selectingTopLeft
= updateTopLeft
;
5932 m_selectingBottomRight
= updateBottomRight
;
5934 // various Refresh() calls
5935 for (i
= 0; i
< 4; i
++ )
5936 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5937 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5940 // never generate an event as it will be generated from
5941 // wxGridSelection::SelectBlock!
5942 // (old comment from when this was the body of SelectBlock)
5946 // ------ functions to get/send data (see also public functions)
5949 bool wxGrid::GetModelValues()
5953 // all we need to do is repaint the grid
5955 m_gridWin
->Refresh();
5963 bool wxGrid::SetModelValues()
5969 for ( row
= 0; row
< m_numRows
; row
++ )
5971 for ( col
= 0; col
< m_numCols
; col
++ )
5973 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5985 // Note - this function only draws cells that are in the list of
5986 // exposed cells (usually set from the update region by
5987 // CalcExposedCells)
5989 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5991 if ( !m_numRows
|| !m_numCols
) return;
5994 size_t numCells
= m_cellsExposed
.GetCount();
5996 for ( i
= 0; i
< numCells
; i
++ )
5998 DrawCell( dc
, m_cellsExposed
[i
] );
6003 void wxGrid::DrawGridSpace( wxDC
& dc
)
6006 m_gridWin
->GetClientSize( &cw
, &ch
);
6009 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6011 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6012 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6014 if ( right
> rightCol
|| bottom
> bottomRow
)
6017 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6019 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6020 dc
.SetPen( *wxTRANSPARENT_PEN
);
6022 if ( right
> rightCol
)
6024 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6027 if ( bottom
> bottomRow
)
6029 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6035 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6037 int row
= coords
.GetRow();
6038 int col
= coords
.GetCol();
6040 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6043 // we draw the cell border ourselves
6044 #if !WXGRID_DRAW_LINES
6045 if ( m_gridLinesEnabled
)
6046 DrawCellBorder( dc
, coords
);
6049 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6051 bool isCurrent
= coords
== m_currentCellCoords
;
6053 wxRect rect
= CellToRect( row
, col
);
6055 // if the editor is shown, we should use it and not the renderer
6056 // Note: However, only if it is really _shown_, i.e. not hidden!
6057 if ( isCurrent
&& IsCellEditControlShown() )
6059 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6060 editor
->PaintBackground(rect
, attr
);
6065 // but all the rest is drawn by the cell renderer and hence may be
6067 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6068 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6075 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6077 int row
= m_currentCellCoords
.GetRow();
6078 int col
= m_currentCellCoords
.GetCol();
6080 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6083 wxRect rect
= CellToRect(row
, col
);
6085 // hmmm... what could we do here to show that the cell is disabled?
6086 // for now, I just draw a thinner border than for the other ones, but
6087 // it doesn't look really good
6089 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6093 // The center of th drawn line is where the position/width/height of
6094 // the rectangle is actually at, (on wxMSW atr least,) so we will
6095 // reduce the size of the rectangle to compensate for the thickness of
6096 // the line. If this is too strange on non wxMSW platforms then
6097 // please #ifdef this appropriately.
6098 rect
.x
+= penWidth
/2;
6099 rect
.y
+= penWidth
/2;
6100 rect
.width
-= penWidth
-1;
6101 rect
.height
-= penWidth
-1;
6104 // Now draw the rectangle
6105 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6106 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6107 dc
.DrawRectangle(rect
);
6111 // VZ: my experiments with 3d borders...
6113 // how to properly set colours for arbitrary bg?
6114 wxCoord x1
= rect
.x
,
6116 x2
= rect
.x
+ rect
.width
-1,
6117 y2
= rect
.y
+ rect
.height
-1;
6119 dc
.SetPen(*wxWHITE_PEN
);
6120 dc
.DrawLine(x1
, y1
, x2
, y1
);
6121 dc
.DrawLine(x1
, y1
, x1
, y2
);
6123 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6124 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6126 dc
.SetPen(*wxBLACK_PEN
);
6127 dc
.DrawLine(x1
, y2
, x2
, y2
);
6128 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6133 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6135 int row
= coords
.GetRow();
6136 int col
= coords
.GetCol();
6137 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6140 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6142 // right hand border
6144 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6145 GetColRight(col
), GetRowBottom(row
) );
6149 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6150 GetColRight(col
), GetRowBottom(row
) );
6153 void wxGrid::DrawHighlight(wxDC
& dc
)
6155 // This if block was previously in wxGrid::OnPaint but that doesn't
6156 // seem to get called under wxGTK - MB
6158 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6159 m_numRows
&& m_numCols
)
6161 m_currentCellCoords
.Set(0, 0);
6164 if ( IsCellEditControlShown() )
6166 // don't show highlight when the edit control is shown
6170 // if the active cell was repainted, repaint its highlight too because it
6171 // might have been damaged by the grid lines
6172 size_t count
= m_cellsExposed
.GetCount();
6173 for ( size_t n
= 0; n
< count
; n
++ )
6175 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
6177 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6178 DrawCellHighlight(dc
, attr
);
6186 // TODO: remove this ???
6187 // This is used to redraw all grid lines e.g. when the grid line colour
6190 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6192 if ( !m_gridLinesEnabled
||
6194 !m_numCols
) return;
6196 int top
, bottom
, left
, right
;
6198 #if 0 //#ifndef __WXGTK__
6202 m_gridWin
->GetClientSize(&cw
, &ch
);
6204 // virtual coords of visible area
6206 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6207 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6212 reg
.GetBox(x
, y
, w
, h
);
6213 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6214 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6218 m_gridWin
->GetClientSize(&cw
, &ch
);
6219 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6220 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6223 // avoid drawing grid lines past the last row and col
6225 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6226 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6228 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6230 // horizontal grid lines
6233 for ( i
= 0; i
< m_numRows
; i
++ )
6235 int bot
= GetRowBottom(i
) - 1;
6244 dc
.DrawLine( left
, bot
, right
, bot
);
6249 // vertical grid lines
6251 for ( i
= 0; i
< m_numCols
; i
++ )
6253 int colRight
= GetColRight(i
) - 1;
6254 if ( colRight
> right
)
6259 if ( colRight
>= left
)
6261 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6267 void wxGrid::DrawRowLabels( wxDC
& dc
)
6269 if ( !m_numRows
) return;
6272 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6274 for ( i
= 0; i
< numLabels
; i
++ )
6276 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6281 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6283 if ( GetRowHeight(row
) <= 0 )
6286 int rowTop
= GetRowTop(row
),
6287 rowBottom
= GetRowBottom(row
) - 1;
6289 dc
.SetPen( *wxBLACK_PEN
);
6290 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6291 m_rowLabelWidth
-1, rowBottom
);
6293 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6295 dc
.SetPen( *wxWHITE_PEN
);
6296 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6297 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6299 dc
.SetBackgroundMode( wxTRANSPARENT
);
6300 dc
.SetTextForeground( GetLabelTextColour() );
6301 dc
.SetFont( GetLabelFont() );
6304 GetRowLabelAlignment( &hAlign
, &vAlign
);
6308 rect
.SetY( GetRowTop(row
) + 2 );
6309 rect
.SetWidth( m_rowLabelWidth
- 4 );
6310 rect
.SetHeight( GetRowHeight(row
) - 4 );
6311 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6315 void wxGrid::DrawColLabels( wxDC
& dc
)
6317 if ( !m_numCols
) return;
6320 size_t numLabels
= m_colLabelsExposed
.GetCount();
6322 for ( i
= 0; i
< numLabels
; i
++ )
6324 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6329 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6331 if ( GetColWidth(col
) <= 0 )
6334 int colLeft
= GetColLeft(col
),
6335 colRight
= GetColRight(col
) - 1;
6337 dc
.SetPen( *wxBLACK_PEN
);
6338 dc
.DrawLine( colRight
, 0,
6339 colRight
, m_colLabelHeight
-1 );
6341 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6342 colRight
, m_colLabelHeight
-1 );
6344 dc
.SetPen( *wxWHITE_PEN
);
6345 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6346 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6348 dc
.SetBackgroundMode( wxTRANSPARENT
);
6349 dc
.SetTextForeground( GetLabelTextColour() );
6350 dc
.SetFont( GetLabelFont() );
6352 dc
.SetBackgroundMode( wxTRANSPARENT
);
6353 dc
.SetTextForeground( GetLabelTextColour() );
6354 dc
.SetFont( GetLabelFont() );
6357 GetColLabelAlignment( &hAlign
, &vAlign
);
6360 rect
.SetX( colLeft
+ 2 );
6362 rect
.SetWidth( GetColWidth(col
) - 4 );
6363 rect
.SetHeight( m_colLabelHeight
- 4 );
6364 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6368 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6369 const wxString
& value
,
6374 long textWidth
, textHeight
;
6375 long lineWidth
, lineHeight
;
6376 wxArrayString lines
;
6378 dc
.SetClippingRegion( rect
);
6379 StringToLines( value
, lines
);
6380 if ( lines
.GetCount() )
6382 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6383 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6386 switch ( horizAlign
)
6389 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6392 case wxALIGN_CENTRE
:
6393 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6402 switch ( vertAlign
)
6404 case wxALIGN_BOTTOM
:
6405 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6408 case wxALIGN_CENTRE
:
6409 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6418 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6420 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6425 dc
.DestroyClippingRegion();
6429 // Split multi line text up into an array of strings. Any existing
6430 // contents of the string array are preserved.
6432 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6436 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6437 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6439 while ( startPos
< (int)tVal
.Length() )
6441 pos
= tVal
.Mid(startPos
).Find( eol
);
6446 else if ( pos
== 0 )
6448 lines
.Add( wxEmptyString
);
6452 lines
.Add( value
.Mid(startPos
, pos
) );
6456 if ( startPos
< (int)value
.Length() )
6458 lines
.Add( value
.Mid( startPos
) );
6463 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6464 wxArrayString
& lines
,
6465 long *width
, long *height
)
6472 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6474 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6475 w
= wxMax( w
, lineW
);
6484 // ------ Batch processing.
6486 void wxGrid::EndBatch()
6488 if ( m_batchCount
> 0 )
6491 if ( !m_batchCount
)
6494 m_rowLabelWin
->Refresh();
6495 m_colLabelWin
->Refresh();
6496 m_cornerLabelWin
->Refresh();
6497 m_gridWin
->Refresh();
6502 // Use this, rather than wxWindow::Refresh(), to force an immediate
6503 // repainting of the grid. Has no effect if you are already inside a
6504 // BeginBatch / EndBatch block.
6506 void wxGrid::ForceRefresh()
6514 // ------ Edit control functions
6518 void wxGrid::EnableEditing( bool edit
)
6520 // TODO: improve this ?
6522 if ( edit
!= m_editable
)
6524 if(!edit
) EnableCellEditControl(edit
);
6530 void wxGrid::EnableCellEditControl( bool enable
)
6535 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6536 SetCurrentCell( 0, 0 );
6538 if ( enable
!= m_cellEditCtrlEnabled
)
6540 // TODO allow the app to Veto() this event?
6541 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6545 // this should be checked by the caller!
6546 wxASSERT_MSG( CanEnableCellControl(),
6547 _T("can't enable editing for this cell!") );
6549 // do it before ShowCellEditControl()
6550 m_cellEditCtrlEnabled
= enable
;
6552 ShowCellEditControl();
6556 HideCellEditControl();
6557 SaveEditControlValue();
6559 // do it after HideCellEditControl()
6560 m_cellEditCtrlEnabled
= enable
;
6565 bool wxGrid::IsCurrentCellReadOnly() const
6568 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6569 bool readonly
= attr
->IsReadOnly();
6575 bool wxGrid::CanEnableCellControl() const
6577 return m_editable
&& !IsCurrentCellReadOnly();
6580 bool wxGrid::IsCellEditControlEnabled() const
6582 // the cell edit control might be disable for all cells or just for the
6583 // current one if it's read only
6584 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6587 bool wxGrid::IsCellEditControlShown() const
6589 bool isShown
= FALSE
;
6591 if ( m_cellEditCtrlEnabled
)
6593 int row
= m_currentCellCoords
.GetRow();
6594 int col
= m_currentCellCoords
.GetCol();
6595 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6596 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6601 if ( editor
->IsCreated() )
6603 isShown
= editor
->GetControl()->IsShown();
6613 void wxGrid::ShowCellEditControl()
6615 if ( IsCellEditControlEnabled() )
6617 if ( !IsVisible( m_currentCellCoords
) )
6619 m_cellEditCtrlEnabled
= FALSE
;
6624 wxRect rect
= CellToRect( m_currentCellCoords
);
6625 int row
= m_currentCellCoords
.GetRow();
6626 int col
= m_currentCellCoords
.GetCol();
6628 // convert to scrolled coords
6630 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6632 // done in PaintBackground()
6634 // erase the highlight and the cell contents because the editor
6635 // might not cover the entire cell
6636 wxClientDC
dc( m_gridWin
);
6638 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6639 dc
.SetPen(*wxTRANSPARENT_PEN
);
6640 dc
.DrawRectangle(rect
);
6643 // cell is shifted by one pixel
6647 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6648 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6649 if ( !editor
->IsCreated() )
6651 editor
->Create(m_gridWin
, -1,
6652 new wxGridCellEditorEvtHandler(this, editor
));
6655 editor
->Show( TRUE
, attr
);
6657 editor
->SetSize( rect
);
6659 editor
->BeginEdit(row
, col
, this);
6668 void wxGrid::HideCellEditControl()
6670 if ( IsCellEditControlEnabled() )
6672 int row
= m_currentCellCoords
.GetRow();
6673 int col
= m_currentCellCoords
.GetCol();
6675 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6676 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6677 editor
->Show( FALSE
);
6680 m_gridWin
->SetFocus();
6681 wxRect
rect( CellToRect( row
, col
) );
6682 m_gridWin
->Refresh( FALSE
, &rect
);
6687 void wxGrid::SaveEditControlValue()
6689 if ( IsCellEditControlEnabled() )
6691 int row
= m_currentCellCoords
.GetRow();
6692 int col
= m_currentCellCoords
.GetCol();
6694 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6695 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6696 bool changed
= editor
->EndEdit(row
, col
, this);
6703 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6704 m_currentCellCoords
.GetRow(),
6705 m_currentCellCoords
.GetCol() );
6712 // ------ Grid location functions
6713 // Note that all of these functions work with the logical coordinates of
6714 // grid cells and labels so you will need to convert from device
6715 // coordinates for mouse events etc.
6718 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6720 int row
= YToRow(y
);
6721 int col
= XToCol(x
);
6723 if ( row
== -1 || col
== -1 )
6725 coords
= wxGridNoCellCoords
;
6729 coords
.Set( row
, col
);
6734 int wxGrid::YToRow( int y
)
6738 for ( i
= 0; i
< m_numRows
; i
++ )
6740 if ( y
< GetRowBottom(i
) )
6748 int wxGrid::XToCol( int x
)
6752 for ( i
= 0; i
< m_numCols
; i
++ )
6754 if ( x
< GetColRight(i
) )
6762 // return the row number that that the y coord is near the edge of, or
6763 // -1 if not near an edge
6765 int wxGrid::YToEdgeOfRow( int y
)
6769 for ( i
= 0; i
< m_numRows
; i
++ )
6771 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6773 d
= abs( y
- GetRowBottom(i
) );
6774 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6783 // return the col number that that the x coord is near the edge of, or
6784 // -1 if not near an edge
6786 int wxGrid::XToEdgeOfCol( int x
)
6790 for ( i
= 0; i
< m_numCols
; i
++ )
6792 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6794 d
= abs( x
- GetColRight(i
) );
6795 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6804 wxRect
wxGrid::CellToRect( int row
, int col
)
6806 wxRect
rect( -1, -1, -1, -1 );
6808 if ( row
>= 0 && row
< m_numRows
&&
6809 col
>= 0 && col
< m_numCols
)
6811 rect
.x
= GetColLeft(col
);
6812 rect
.y
= GetRowTop(row
);
6813 rect
.width
= GetColWidth(col
);
6814 rect
.height
= GetRowHeight(row
);
6817 // if grid lines are enabled, then the area of the cell is a bit smaller
6818 if (m_gridLinesEnabled
) {
6826 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6828 // get the cell rectangle in logical coords
6830 wxRect
r( CellToRect( row
, col
) );
6832 // convert to device coords
6834 int left
, top
, right
, bottom
;
6835 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6836 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6838 // check against the client area of the grid window
6841 m_gridWin
->GetClientSize( &cw
, &ch
);
6843 if ( wholeCellVisible
)
6845 // is the cell wholly visible ?
6847 return ( left
>= 0 && right
<= cw
&&
6848 top
>= 0 && bottom
<= ch
);
6852 // is the cell partly visible ?
6854 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6855 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6860 // make the specified cell location visible by doing a minimal amount
6863 void wxGrid::MakeCellVisible( int row
, int col
)
6866 int xpos
= -1, ypos
= -1;
6868 if ( row
>= 0 && row
< m_numRows
&&
6869 col
>= 0 && col
< m_numCols
)
6871 // get the cell rectangle in logical coords
6873 wxRect
r( CellToRect( row
, col
) );
6875 // convert to device coords
6877 int left
, top
, right
, bottom
;
6878 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6879 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6882 m_gridWin
->GetClientSize( &cw
, &ch
);
6888 else if ( bottom
> ch
)
6890 int h
= r
.GetHeight();
6892 for ( i
= row
-1; i
>= 0; i
-- )
6894 int rowHeight
= GetRowHeight(i
);
6895 if ( h
+ rowHeight
> ch
)
6902 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6903 // have rounding errors (this is important, because if we do, we
6904 // might not scroll at all and some cells won't be redrawn)
6905 ypos
+= GRID_SCROLL_LINE
/ 2;
6912 else if ( right
> cw
)
6914 int w
= r
.GetWidth();
6916 for ( i
= col
-1; i
>= 0; i
-- )
6918 int colWidth
= GetColWidth(i
);
6919 if ( w
+ colWidth
> cw
)
6926 // see comment for ypos above
6927 xpos
+= GRID_SCROLL_LINE
/ 2;
6930 if ( xpos
!= -1 || ypos
!= -1 )
6932 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6933 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6934 Scroll( xpos
, ypos
);
6942 // ------ Grid cursor movement functions
6945 bool wxGrid::MoveCursorUp( bool expandSelection
)
6947 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6948 m_currentCellCoords
.GetRow() >= 0 )
6950 if ( expandSelection
)
6952 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6953 m_selectingKeyboard
= m_currentCellCoords
;
6954 if ( m_selectingKeyboard
.GetRow() > 0 )
6956 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6957 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6958 m_selectingKeyboard
.GetCol() );
6959 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6962 else if ( m_currentCellCoords
.GetRow() > 0 )
6965 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6966 m_currentCellCoords
.GetCol() );
6967 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6968 m_currentCellCoords
.GetCol() );
6979 bool wxGrid::MoveCursorDown( bool expandSelection
)
6981 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6982 m_currentCellCoords
.GetRow() < m_numRows
)
6984 if ( expandSelection
)
6986 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6987 m_selectingKeyboard
= m_currentCellCoords
;
6988 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6990 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6991 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6992 m_selectingKeyboard
.GetCol() );
6993 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6996 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6999 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
7000 m_currentCellCoords
.GetCol() );
7001 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
7002 m_currentCellCoords
.GetCol() );
7013 bool wxGrid::MoveCursorLeft( bool expandSelection
)
7015 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7016 m_currentCellCoords
.GetCol() >= 0 )
7018 if ( expandSelection
)
7020 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7021 m_selectingKeyboard
= m_currentCellCoords
;
7022 if ( m_selectingKeyboard
.GetCol() > 0 )
7024 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
7025 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7026 m_selectingKeyboard
.GetCol() );
7027 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7030 else if ( m_currentCellCoords
.GetCol() > 0 )
7033 MakeCellVisible( m_currentCellCoords
.GetRow(),
7034 m_currentCellCoords
.GetCol() - 1 );
7035 SetCurrentCell( m_currentCellCoords
.GetRow(),
7036 m_currentCellCoords
.GetCol() - 1 );
7047 bool wxGrid::MoveCursorRight( bool expandSelection
)
7049 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7050 m_currentCellCoords
.GetCol() < m_numCols
)
7052 if ( expandSelection
)
7054 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7055 m_selectingKeyboard
= m_currentCellCoords
;
7056 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
7058 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
7059 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7060 m_selectingKeyboard
.GetCol() );
7061 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7064 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
7067 MakeCellVisible( m_currentCellCoords
.GetRow(),
7068 m_currentCellCoords
.GetCol() + 1 );
7069 SetCurrentCell( m_currentCellCoords
.GetRow(),
7070 m_currentCellCoords
.GetCol() + 1 );
7081 bool wxGrid::MovePageUp()
7083 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7085 int row
= m_currentCellCoords
.GetRow();
7089 m_gridWin
->GetClientSize( &cw
, &ch
);
7091 int y
= GetRowTop(row
);
7092 int newRow
= YToRow( y
- ch
+ 1 );
7097 else if ( newRow
== row
)
7102 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7103 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7111 bool wxGrid::MovePageDown()
7113 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7115 int row
= m_currentCellCoords
.GetRow();
7116 if ( row
< m_numRows
)
7119 m_gridWin
->GetClientSize( &cw
, &ch
);
7121 int y
= GetRowTop(row
);
7122 int newRow
= YToRow( y
+ ch
);
7125 newRow
= m_numRows
- 1;
7127 else if ( newRow
== row
)
7132 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7133 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7141 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7144 m_currentCellCoords
!= wxGridNoCellCoords
&&
7145 m_currentCellCoords
.GetRow() > 0 )
7147 int row
= m_currentCellCoords
.GetRow();
7148 int col
= m_currentCellCoords
.GetCol();
7150 if ( m_table
->IsEmptyCell(row
, col
) )
7152 // starting in an empty cell: find the next block of
7158 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7161 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7163 // starting at the top of a block: find the next block
7169 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7174 // starting within a block: find the top of the block
7179 if ( m_table
->IsEmptyCell(row
, col
) )
7187 MakeCellVisible( row
, col
);
7188 if ( expandSelection
)
7190 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7191 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7196 SetCurrentCell( row
, col
);
7204 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7207 m_currentCellCoords
!= wxGridNoCellCoords
&&
7208 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7210 int row
= m_currentCellCoords
.GetRow();
7211 int col
= m_currentCellCoords
.GetCol();
7213 if ( m_table
->IsEmptyCell(row
, col
) )
7215 // starting in an empty cell: find the next block of
7218 while ( row
< m_numRows
-1 )
7221 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7224 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7226 // starting at the bottom of a block: find the next block
7229 while ( row
< m_numRows
-1 )
7232 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7237 // starting within a block: find the bottom of the block
7239 while ( row
< m_numRows
-1 )
7242 if ( m_table
->IsEmptyCell(row
, col
) )
7250 MakeCellVisible( row
, col
);
7251 if ( expandSelection
)
7253 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7254 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7259 SetCurrentCell( row
, col
);
7268 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7271 m_currentCellCoords
!= wxGridNoCellCoords
&&
7272 m_currentCellCoords
.GetCol() > 0 )
7274 int row
= m_currentCellCoords
.GetRow();
7275 int col
= m_currentCellCoords
.GetCol();
7277 if ( m_table
->IsEmptyCell(row
, col
) )
7279 // starting in an empty cell: find the next block of
7285 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7288 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7290 // starting at the left of a block: find the next block
7296 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7301 // starting within a block: find the left of the block
7306 if ( m_table
->IsEmptyCell(row
, col
) )
7314 MakeCellVisible( row
, col
);
7315 if ( expandSelection
)
7317 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7318 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7323 SetCurrentCell( row
, col
);
7332 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7335 m_currentCellCoords
!= wxGridNoCellCoords
&&
7336 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7338 int row
= m_currentCellCoords
.GetRow();
7339 int col
= m_currentCellCoords
.GetCol();
7341 if ( m_table
->IsEmptyCell(row
, col
) )
7343 // starting in an empty cell: find the next block of
7346 while ( col
< m_numCols
-1 )
7349 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7352 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7354 // starting at the right of a block: find the next block
7357 while ( col
< m_numCols
-1 )
7360 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7365 // starting within a block: find the right of the block
7367 while ( col
< m_numCols
-1 )
7370 if ( m_table
->IsEmptyCell(row
, col
) )
7378 MakeCellVisible( row
, col
);
7379 if ( expandSelection
)
7381 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7382 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7387 SetCurrentCell( row
, col
);
7399 // ------ Label values and formatting
7402 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7404 *horiz
= m_rowLabelHorizAlign
;
7405 *vert
= m_rowLabelVertAlign
;
7408 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7410 *horiz
= m_colLabelHorizAlign
;
7411 *vert
= m_colLabelVertAlign
;
7414 wxString
wxGrid::GetRowLabelValue( int row
)
7418 return m_table
->GetRowLabelValue( row
);
7428 wxString
wxGrid::GetColLabelValue( int col
)
7432 return m_table
->GetColLabelValue( col
);
7443 void wxGrid::SetRowLabelSize( int width
)
7445 width
= wxMax( width
, 0 );
7446 if ( width
!= m_rowLabelWidth
)
7450 m_rowLabelWin
->Show( FALSE
);
7451 m_cornerLabelWin
->Show( FALSE
);
7453 else if ( m_rowLabelWidth
== 0 )
7455 m_rowLabelWin
->Show( TRUE
);
7456 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7459 m_rowLabelWidth
= width
;
7466 void wxGrid::SetColLabelSize( int height
)
7468 height
= wxMax( height
, 0 );
7469 if ( height
!= m_colLabelHeight
)
7473 m_colLabelWin
->Show( FALSE
);
7474 m_cornerLabelWin
->Show( FALSE
);
7476 else if ( m_colLabelHeight
== 0 )
7478 m_colLabelWin
->Show( TRUE
);
7479 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7482 m_colLabelHeight
= height
;
7489 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7491 if ( m_labelBackgroundColour
!= colour
)
7493 m_labelBackgroundColour
= colour
;
7494 m_rowLabelWin
->SetBackgroundColour( colour
);
7495 m_colLabelWin
->SetBackgroundColour( colour
);
7496 m_cornerLabelWin
->SetBackgroundColour( colour
);
7498 if ( !GetBatchCount() )
7500 m_rowLabelWin
->Refresh();
7501 m_colLabelWin
->Refresh();
7502 m_cornerLabelWin
->Refresh();
7507 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7509 if ( m_labelTextColour
!= colour
)
7511 m_labelTextColour
= colour
;
7512 if ( !GetBatchCount() )
7514 m_rowLabelWin
->Refresh();
7515 m_colLabelWin
->Refresh();
7520 void wxGrid::SetLabelFont( const wxFont
& font
)
7523 if ( !GetBatchCount() )
7525 m_rowLabelWin
->Refresh();
7526 m_colLabelWin
->Refresh();
7530 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7532 // allow old (incorrect) defs to be used
7535 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7536 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7537 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7542 case wxTOP
: vert
= wxALIGN_TOP
; break;
7543 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7544 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7547 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7549 m_rowLabelHorizAlign
= horiz
;
7552 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7554 m_rowLabelVertAlign
= vert
;
7557 if ( !GetBatchCount() )
7559 m_rowLabelWin
->Refresh();
7563 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7565 // allow old (incorrect) defs to be used
7568 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7569 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7570 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7575 case wxTOP
: vert
= wxALIGN_TOP
; break;
7576 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7577 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7580 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7582 m_colLabelHorizAlign
= horiz
;
7585 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7587 m_colLabelVertAlign
= vert
;
7590 if ( !GetBatchCount() )
7592 m_colLabelWin
->Refresh();
7596 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7600 m_table
->SetRowLabelValue( row
, s
);
7601 if ( !GetBatchCount() )
7603 wxRect rect
= CellToRect( row
, 0);
7604 if ( rect
.height
> 0 )
7606 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7608 rect
.width
= m_rowLabelWidth
;
7609 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7615 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7619 m_table
->SetColLabelValue( col
, s
);
7620 if ( !GetBatchCount() )
7622 wxRect rect
= CellToRect( 0, col
);
7623 if ( rect
.width
> 0 )
7625 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7627 rect
.height
= m_colLabelHeight
;
7628 m_colLabelWin
->Refresh( TRUE
, &rect
);
7634 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7636 if ( m_gridLineColour
!= colour
)
7638 m_gridLineColour
= colour
;
7640 wxClientDC
dc( m_gridWin
);
7642 DrawAllGridLines( dc
, wxRegion() );
7647 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7649 if ( m_cellHighlightColour
!= colour
)
7651 m_cellHighlightColour
= colour
;
7653 wxClientDC
dc( m_gridWin
);
7655 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7656 DrawCellHighlight(dc
, attr
);
7661 void wxGrid::SetCellHighlightPenWidth(int width
)
7663 if (m_cellHighlightPenWidth
!= width
) {
7664 m_cellHighlightPenWidth
= width
;
7666 // Just redrawing the cell highlight is not enough since that won't
7667 // make any visible change if the the thickness is getting smaller.
7668 int row
= m_currentCellCoords
.GetRow();
7669 int col
= m_currentCellCoords
.GetCol();
7670 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7672 wxRect rect
= CellToRect(row
, col
);
7673 m_gridWin
->Refresh(TRUE
, &rect
);
7677 void wxGrid::SetCellHighlightROPenWidth(int width
)
7679 if (m_cellHighlightROPenWidth
!= width
) {
7680 m_cellHighlightROPenWidth
= width
;
7682 // Just redrawing the cell highlight is not enough since that won't
7683 // make any visible change if the the thickness is getting smaller.
7684 int row
= m_currentCellCoords
.GetRow();
7685 int col
= m_currentCellCoords
.GetCol();
7686 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7688 wxRect rect
= CellToRect(row
, col
);
7689 m_gridWin
->Refresh(TRUE
, &rect
);
7693 void wxGrid::EnableGridLines( bool enable
)
7695 if ( enable
!= m_gridLinesEnabled
)
7697 m_gridLinesEnabled
= enable
;
7699 if ( !GetBatchCount() )
7703 wxClientDC
dc( m_gridWin
);
7705 DrawAllGridLines( dc
, wxRegion() );
7709 m_gridWin
->Refresh();
7716 int wxGrid::GetDefaultRowSize()
7718 return m_defaultRowHeight
;
7721 int wxGrid::GetRowSize( int row
)
7723 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7725 return GetRowHeight(row
);
7728 int wxGrid::GetDefaultColSize()
7730 return m_defaultColWidth
;
7733 int wxGrid::GetColSize( int col
)
7735 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7737 return GetColWidth(col
);
7740 // ============================================================================
7741 // access to the grid attributes: each of them has a default value in the grid
7742 // itself and may be overidden on a per-cell basis
7743 // ============================================================================
7745 // ----------------------------------------------------------------------------
7746 // setting default attributes
7747 // ----------------------------------------------------------------------------
7749 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7751 m_defaultCellAttr
->SetBackgroundColour(col
);
7753 m_gridWin
->SetBackgroundColour(col
);
7757 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7759 m_defaultCellAttr
->SetTextColour(col
);
7762 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7764 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7767 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7769 m_defaultCellAttr
->SetFont(font
);
7772 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7774 m_defaultCellAttr
->SetRenderer(renderer
);
7777 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7779 m_defaultCellAttr
->SetEditor(editor
);
7782 // ----------------------------------------------------------------------------
7783 // access to the default attrbiutes
7784 // ----------------------------------------------------------------------------
7786 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7788 return m_defaultCellAttr
->GetBackgroundColour();
7791 wxColour
wxGrid::GetDefaultCellTextColour()
7793 return m_defaultCellAttr
->GetTextColour();
7796 wxFont
wxGrid::GetDefaultCellFont()
7798 return m_defaultCellAttr
->GetFont();
7801 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7803 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7806 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7808 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7811 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7813 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7816 // ----------------------------------------------------------------------------
7817 // access to cell attributes
7818 // ----------------------------------------------------------------------------
7820 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7822 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7823 wxColour colour
= attr
->GetBackgroundColour();
7828 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7830 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7831 wxColour colour
= attr
->GetTextColour();
7836 wxFont
wxGrid::GetCellFont( int row
, int col
)
7838 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7839 wxFont font
= attr
->GetFont();
7844 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7846 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7847 attr
->GetAlignment(horiz
, vert
);
7851 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7853 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7854 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7860 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7862 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7863 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7869 bool wxGrid::IsReadOnly(int row
, int col
) const
7871 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7872 bool isReadOnly
= attr
->IsReadOnly();
7877 // ----------------------------------------------------------------------------
7878 // attribute support: cache, automatic provider creation, ...
7879 // ----------------------------------------------------------------------------
7881 bool wxGrid::CanHaveAttributes()
7888 return m_table
->CanHaveAttributes();
7891 void wxGrid::ClearAttrCache()
7893 if ( m_attrCache
.row
!= -1 )
7895 wxSafeDecRef(m_attrCache
.attr
);
7896 m_attrCache
.attr
= NULL
;
7897 m_attrCache
.row
= -1;
7901 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7903 wxGrid
*self
= (wxGrid
*)this; // const_cast
7905 self
->ClearAttrCache();
7906 self
->m_attrCache
.row
= row
;
7907 self
->m_attrCache
.col
= col
;
7908 self
->m_attrCache
.attr
= attr
;
7912 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7914 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7916 *attr
= m_attrCache
.attr
;
7917 wxSafeIncRef(m_attrCache
.attr
);
7919 #ifdef DEBUG_ATTR_CACHE
7920 gs_nAttrCacheHits
++;
7927 #ifdef DEBUG_ATTR_CACHE
7928 gs_nAttrCacheMisses
++;
7934 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7936 wxGridCellAttr
*attr
;
7937 if ( !LookupAttr(row
, col
, &attr
) )
7939 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
) : (wxGridCellAttr
*)NULL
;
7940 CacheAttr(row
, col
, attr
);
7944 attr
->SetDefAttr(m_defaultCellAttr
);
7948 attr
= m_defaultCellAttr
;
7955 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7957 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
7958 wxASSERT_MSG( m_table
,
7959 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7961 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
7964 attr
= new wxGridCellAttr
;
7966 // artificially inc the ref count to match DecRef() in caller
7968 m_table
->SetAttr(attr
, row
, col
);
7970 attr
->SetDefAttr(m_defaultCellAttr
);
7974 // ----------------------------------------------------------------------------
7975 // setting column attributes (wrappers around SetColAttr)
7976 // ----------------------------------------------------------------------------
7978 void wxGrid::SetColFormatBool(int col
)
7980 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7983 void wxGrid::SetColFormatNumber(int col
)
7985 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7988 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7990 wxString typeName
= wxGRID_VALUE_FLOAT
;
7991 if ( (width
!= -1) || (precision
!= -1) )
7993 typeName
<< _T(':') << width
<< _T(',') << precision
;
7996 SetColFormatCustom(col
, typeName
);
7999 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
8001 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
8003 attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
8005 attr
= new wxGridCellAttr
;
8006 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
8007 attr
->SetRenderer(renderer
);
8009 SetColAttr(col
, attr
);
8013 // ----------------------------------------------------------------------------
8014 // setting cell attributes: this is forwarded to the table
8015 // ----------------------------------------------------------------------------
8017 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
8019 if ( CanHaveAttributes() )
8021 m_table
->SetRowAttr(attr
, row
);
8030 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
8032 if ( CanHaveAttributes() )
8034 m_table
->SetColAttr(attr
, col
);
8043 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
8045 if ( CanHaveAttributes() )
8047 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8048 attr
->SetBackgroundColour(colour
);
8053 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
8055 if ( CanHaveAttributes() )
8057 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8058 attr
->SetTextColour(colour
);
8063 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
8065 if ( CanHaveAttributes() )
8067 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8068 attr
->SetFont(font
);
8073 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
8075 if ( CanHaveAttributes() )
8077 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8078 attr
->SetAlignment(horiz
, vert
);
8083 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
8085 if ( CanHaveAttributes() )
8087 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8088 attr
->SetRenderer(renderer
);
8093 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8095 if ( CanHaveAttributes() )
8097 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8098 attr
->SetEditor(editor
);
8103 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8105 if ( CanHaveAttributes() )
8107 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8108 attr
->SetReadOnly(isReadOnly
);
8113 // ----------------------------------------------------------------------------
8114 // Data type registration
8115 // ----------------------------------------------------------------------------
8117 void wxGrid::RegisterDataType(const wxString
& typeName
,
8118 wxGridCellRenderer
* renderer
,
8119 wxGridCellEditor
* editor
)
8121 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8125 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8127 wxString typeName
= m_table
->GetTypeName(row
, col
);
8128 return GetDefaultEditorForType(typeName
);
8131 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8133 wxString typeName
= m_table
->GetTypeName(row
, col
);
8134 return GetDefaultRendererForType(typeName
);
8138 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8140 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8141 if ( index
== wxNOT_FOUND
)
8143 wxFAIL_MSG(wxT("Unknown data type name"));
8148 return m_typeRegistry
->GetEditor(index
);
8152 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8154 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8155 if ( index
== wxNOT_FOUND
)
8157 wxFAIL_MSG(wxT("Unknown data type name"));
8162 return m_typeRegistry
->GetRenderer(index
);
8166 // ----------------------------------------------------------------------------
8168 // ----------------------------------------------------------------------------
8170 void wxGrid::EnableDragRowSize( bool enable
)
8172 m_canDragRowSize
= enable
;
8176 void wxGrid::EnableDragColSize( bool enable
)
8178 m_canDragColSize
= enable
;
8181 void wxGrid::EnableDragGridSize( bool enable
)
8183 m_canDragGridSize
= enable
;
8187 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8189 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8191 if ( resizeExistingRows
)
8194 if ( !GetBatchCount() )
8199 void wxGrid::SetRowSize( int row
, int height
)
8201 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8203 if ( m_rowHeights
.IsEmpty() )
8205 // need to really create the array
8209 int h
= wxMax( 0, height
);
8210 int diff
= h
- m_rowHeights
[row
];
8212 m_rowHeights
[row
] = h
;
8214 for ( i
= row
; i
< m_numRows
; i
++ )
8216 m_rowBottoms
[i
] += diff
;
8218 if ( !GetBatchCount() )
8222 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8224 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8226 if ( resizeExistingCols
)
8229 if ( !GetBatchCount() )
8234 void wxGrid::SetColSize( int col
, int width
)
8236 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8238 // should we check that it's bigger than GetColMinimalWidth(col) here?
8240 if ( m_colWidths
.IsEmpty() )
8242 // need to really create the array
8246 int w
= wxMax( 0, width
);
8247 int diff
= w
- m_colWidths
[col
];
8248 m_colWidths
[col
] = w
;
8251 for ( i
= col
; i
< m_numCols
; i
++ )
8253 m_colRights
[i
] += diff
;
8255 if ( !GetBatchCount() )
8260 void wxGrid::SetColMinimalWidth( int col
, int width
)
8262 m_colMinWidths
.Put(col
, width
);
8265 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8267 m_rowMinHeights
.Put(row
, width
);
8270 int wxGrid::GetColMinimalWidth(int col
) const
8272 long value
= m_colMinWidths
.Get(col
);
8273 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8276 int wxGrid::GetRowMinimalHeight(int row
) const
8278 long value
= m_rowMinHeights
.Get(row
);
8279 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8282 // ----------------------------------------------------------------------------
8284 // ----------------------------------------------------------------------------
8286 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8288 wxClientDC
dc(m_gridWin
);
8290 // init both of them to avoid compiler warnings, even if weo nly need one
8298 wxCoord extent
, extentMax
= 0;
8299 int max
= column
? m_numRows
: m_numCols
;
8300 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8307 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8308 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8311 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8312 extent
= column
? size
.x
: size
.y
;
8313 if ( extent
> extentMax
)
8324 // now also compare with the column label extent
8326 dc
.SetFont( GetLabelFont() );
8329 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8331 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8333 extent
= column
? w
: h
;
8334 if ( extent
> extentMax
)
8341 // empty column - give default extent (notice that if extentMax is less
8342 // than default extent but != 0, it's ok)
8343 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8349 // leave some space around text
8359 SetColSize(col
, extentMax
);
8360 if ( !GetBatchCount() )
8363 m_gridWin
->GetClientSize( &cw
, &ch
);
8364 wxRect
rect ( CellToRect( 0, col
) );
8366 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8367 rect
.width
= cw
- rect
.x
;
8368 rect
.height
= m_colLabelHeight
;
8369 m_colLabelWin
->Refresh( TRUE
, &rect
);
8373 SetRowSize(row
, extentMax
);
8374 if ( !GetBatchCount() )
8377 m_gridWin
->GetClientSize( &cw
, &ch
);
8378 wxRect
rect ( CellToRect( row
, 0 ) );
8380 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8381 rect
.width
= m_rowLabelWidth
;
8382 rect
.height
= ch
- rect
.y
;
8383 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8389 SetColMinimalWidth(col
, extentMax
);
8391 SetRowMinimalHeight(row
, extentMax
);
8395 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8397 int width
= m_rowLabelWidth
;
8401 for ( int col
= 0; col
< m_numCols
; col
++ )
8405 AutoSizeColumn(col
, setAsMin
);
8408 width
+= GetColWidth(col
);
8415 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8417 int height
= m_colLabelHeight
;
8421 for ( int row
= 0; row
< m_numRows
; row
++ )
8425 AutoSizeRow(row
, setAsMin
);
8428 height
+= GetRowHeight(row
);
8435 void wxGrid::AutoSize()
8438 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8441 wxSize
wxGrid::DoGetBestSize() const
8443 // don't set sizes, only calculate them
8444 wxGrid
*self
= (wxGrid
*)this; // const_cast
8446 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8447 self
->SetOrCalcRowSizes(TRUE
));
8456 wxPen
& wxGrid::GetDividerPen() const
8461 // ----------------------------------------------------------------------------
8462 // cell value accessor functions
8463 // ----------------------------------------------------------------------------
8465 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8469 m_table
->SetValue( row
, col
, s
);
8470 if ( !GetBatchCount() )
8472 wxClientDC
dc( m_gridWin
);
8474 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8477 if ( m_currentCellCoords
.GetRow() == row
&&
8478 m_currentCellCoords
.GetCol() == col
&&
8479 IsCellEditControlShown())
8480 // Note: If we are using IsCellEditControlEnabled,
8481 // this interacts badly with calling SetCellValue from
8482 // an EVT_GRID_CELL_CHANGE handler.
8484 HideCellEditControl();
8485 ShowCellEditControl(); // will reread data from table
8492 // ------ Block, row and col selection
8495 void wxGrid::SelectRow( int row
, bool addToSelected
)
8497 if ( IsSelection() && !addToSelected
)
8500 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8504 void wxGrid::SelectCol( int col
, bool addToSelected
)
8506 if ( IsSelection() && !addToSelected
)
8509 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8513 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8514 bool addToSelected
)
8516 if ( IsSelection() && !addToSelected
)
8519 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8520 FALSE
, addToSelected
);
8524 void wxGrid::SelectAll()
8526 if ( m_numRows
> 0 && m_numCols
> 0 )
8527 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8531 // ------ Cell, row and col deselection
8534 void wxGrid::DeselectRow( int row
)
8536 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8538 if ( m_selection
->IsInSelection(row
, 0 ) )
8539 m_selection
->ToggleCellSelection( row
, 0);
8543 int nCols
= GetNumberCols();
8544 for ( int i
= 0; i
< nCols
; i
++ )
8546 if ( m_selection
->IsInSelection(row
, i
) )
8547 m_selection
->ToggleCellSelection( row
, i
);
8552 void wxGrid::DeselectCol( int col
)
8554 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8556 if ( m_selection
->IsInSelection(0, col
) )
8557 m_selection
->ToggleCellSelection( 0, col
);
8561 int nRows
= GetNumberRows();
8562 for ( int i
= 0; i
< nRows
; i
++ )
8564 if ( m_selection
->IsInSelection(i
, col
) )
8565 m_selection
->ToggleCellSelection(i
, col
);
8570 void wxGrid::DeselectCell( int row
, int col
)
8572 if ( m_selection
->IsInSelection(row
, col
) )
8573 m_selection
->ToggleCellSelection(row
, col
);
8576 bool wxGrid::IsSelection()
8578 return ( m_selection
->IsSelection() ||
8579 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8580 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8583 bool wxGrid::IsInSelection( int row
, int col
)
8585 return ( m_selection
->IsInSelection( row
, col
) ||
8586 ( row
>= m_selectingTopLeft
.GetRow() &&
8587 col
>= m_selectingTopLeft
.GetCol() &&
8588 row
<= m_selectingBottomRight
.GetRow() &&
8589 col
<= m_selectingBottomRight
.GetCol() ) );
8592 void wxGrid::ClearSelection()
8594 m_selectingTopLeft
= wxGridNoCellCoords
;
8595 m_selectingBottomRight
= wxGridNoCellCoords
;
8596 m_selection
->ClearSelection();
8600 // This function returns the rectangle that encloses the given block
8601 // in device coords clipped to the client size of the grid window.
8603 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8604 const wxGridCellCoords
&bottomRight
)
8606 wxRect
rect( wxGridNoCellRect
);
8609 cellRect
= CellToRect( topLeft
);
8610 if ( cellRect
!= wxGridNoCellRect
)
8616 rect
= wxRect( 0, 0, 0, 0 );
8619 cellRect
= CellToRect( bottomRight
);
8620 if ( cellRect
!= wxGridNoCellRect
)
8626 return wxGridNoCellRect
;
8629 // convert to scrolled coords
8631 int left
, top
, right
, bottom
;
8632 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8633 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8636 m_gridWin
->GetClientSize( &cw
, &ch
);
8638 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8639 return wxRect( 0, 0, 0, 0);
8641 rect
.SetLeft( wxMax(0, left
) );
8642 rect
.SetTop( wxMax(0, top
) );
8643 rect
.SetRight( wxMin(cw
, right
) );
8644 rect
.SetBottom( wxMin(ch
, bottom
) );
8652 // ------ Grid event classes
8655 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8657 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8658 int row
, int col
, int x
, int y
, bool sel
,
8659 bool control
, bool shift
, bool alt
, bool meta
)
8660 : wxNotifyEvent( type
, id
)
8667 m_control
= control
;
8672 SetEventObject(obj
);
8676 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8678 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8679 int rowOrCol
, int x
, int y
,
8680 bool control
, bool shift
, bool alt
, bool meta
)
8681 : wxNotifyEvent( type
, id
)
8683 m_rowOrCol
= rowOrCol
;
8686 m_control
= control
;
8691 SetEventObject(obj
);
8695 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8697 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8698 const wxGridCellCoords
& topLeft
,
8699 const wxGridCellCoords
& bottomRight
,
8700 bool sel
, bool control
,
8701 bool shift
, bool alt
, bool meta
)
8702 : wxNotifyEvent( type
, id
)
8704 m_topLeft
= topLeft
;
8705 m_bottomRight
= bottomRight
;
8707 m_control
= control
;
8712 SetEventObject(obj
);
8716 #endif // ifndef wxUSE_NEW_GRID