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 // default width/precision
1668 m_format
= _T("%f");
1670 else if ( m_precision
== -1 )
1672 // default precision
1673 m_format
.Printf(_T("%%%d.f"), m_width
);
1677 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1681 text
.Printf(m_format
, val
);
1683 //else: text already contains the string
1688 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1689 wxGridCellAttr
& attr
,
1691 const wxRect
& rectCell
,
1695 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1697 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1699 // draw the text right aligned by default
1701 attr
.GetAlignment(&hAlign
, &vAlign
);
1702 hAlign
= wxALIGN_RIGHT
;
1704 wxRect rect
= rectCell
;
1707 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1710 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1711 wxGridCellAttr
& attr
,
1715 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1718 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1724 // reset to defaults
1730 wxString tmp
= params
.BeforeFirst(_T(','));
1734 if ( !tmp
.ToLong(&width
) )
1740 SetWidth((int)width
);
1742 tmp
= params
.AfterFirst(_T(','));
1746 if ( !tmp
.ToLong(&precision
) )
1752 SetPrecision((int)precision
);
1760 wxLogDebug(_T("Invalid wxGridCellFloatRenderer parameter string '%s ignored"), params
.c_str());
1765 // ----------------------------------------------------------------------------
1766 // wxGridCellBoolRenderer
1767 // ----------------------------------------------------------------------------
1769 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1771 // FIXME these checkbox size calculations are really ugly...
1773 // between checkmark and box
1774 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1776 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1777 wxGridCellAttr
& WXUNUSED(attr
),
1782 // compute it only once (no locks for MT safeness in GUI thread...)
1783 if ( !ms_sizeCheckMark
.x
)
1785 // get checkbox size
1786 wxCoord checkSize
= 0;
1787 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1788 wxSize size
= checkbox
->GetBestSize();
1789 checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1791 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1792 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1793 checkSize
-= size
.y
/ 2;
1798 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1801 return ms_sizeCheckMark
;
1804 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1805 wxGridCellAttr
& attr
,
1811 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1813 // draw a check mark in the centre (ignoring alignment - TODO)
1814 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1816 // don't draw outside the cell
1817 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1818 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1820 // and even leave (at least) 1 pixel margin
1821 size
.x
= size
.y
= minSize
- 2;
1824 // draw a border around checkmark
1826 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
1827 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
1828 rectBorder
.width
= size
.x
;
1829 rectBorder
.height
= size
.y
;
1832 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
1833 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
1836 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
1837 value
= !( !cellval
|| (cellval
== "0") );
1842 wxRect rectMark
= rectBorder
;
1844 // MSW DrawCheckMark() is weird (and should probably be changed...)
1845 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
1849 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
1852 dc
.SetTextForeground(attr
.GetTextColour());
1853 dc
.DrawCheckMark(rectMark
);
1856 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1857 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
1858 dc
.DrawRectangle(rectBorder
);
1861 // ----------------------------------------------------------------------------
1863 // ----------------------------------------------------------------------------
1865 wxGridCellAttr
*wxGridCellAttr::Clone() const
1867 wxGridCellAttr
*attr
= new wxGridCellAttr
;
1868 if ( HasTextColour() )
1869 attr
->SetTextColour(GetTextColour());
1870 if ( HasBackgroundColour() )
1871 attr
->SetBackgroundColour(GetBackgroundColour());
1873 attr
->SetFont(GetFont());
1874 if ( HasAlignment() )
1875 attr
->SetAlignment(m_hAlign
, m_vAlign
);
1879 attr
->SetRenderer(m_renderer
);
1880 m_renderer
->IncRef();
1884 attr
->SetEditor(m_editor
);
1889 attr
->SetReadOnly();
1891 attr
->SetDefAttr(m_defGridAttr
);
1896 const wxColour
& wxGridCellAttr::GetTextColour() const
1898 if (HasTextColour())
1902 else if (m_defGridAttr
!= this)
1904 return m_defGridAttr
->GetTextColour();
1908 wxFAIL_MSG(wxT("Missing default cell attribute"));
1909 return wxNullColour
;
1914 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
1916 if (HasBackgroundColour())
1918 else if (m_defGridAttr
!= this)
1919 return m_defGridAttr
->GetBackgroundColour();
1922 wxFAIL_MSG(wxT("Missing default cell attribute"));
1923 return wxNullColour
;
1928 const wxFont
& wxGridCellAttr::GetFont() const
1932 else if (m_defGridAttr
!= this)
1933 return m_defGridAttr
->GetFont();
1936 wxFAIL_MSG(wxT("Missing default cell attribute"));
1942 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
1946 if ( hAlign
) *hAlign
= m_hAlign
;
1947 if ( vAlign
) *vAlign
= m_vAlign
;
1949 else if (m_defGridAttr
!= this)
1950 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
1953 wxFAIL_MSG(wxT("Missing default cell attribute"));
1958 // GetRenderer and GetEditor use a slightly different decision path about
1959 // which attribute to use. If a non-default attr object has one then it is
1960 // used, otherwise the default editor or renderer is fetched from the grid and
1961 // used. It should be the default for the data type of the cell. If it is
1962 // NULL (because the table has a type that the grid does not have in its
1963 // registry,) then the grid's default editor or renderer is used.
1965 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
1967 wxGridCellRenderer
* renderer
= NULL
;
1969 if ( m_defGridAttr
!= this || grid
== NULL
)
1971 renderer
= m_renderer
; // use local attribute
1976 if ( !renderer
&& grid
) // get renderer for the data type
1978 // GetDefaultRendererForCell() will do IncRef() for us
1979 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
1984 // if we still don't have one then use the grid default
1985 // (no need for IncRef() here neither)
1986 renderer
= m_defGridAttr
->GetRenderer(NULL
,0,0);
1991 wxFAIL_MSG(wxT("Missing default cell attribute"));
1997 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
1999 wxGridCellEditor
* editor
= NULL
;
2001 if ( m_defGridAttr
!= this || grid
== NULL
)
2003 editor
= m_editor
; // use local attribute
2008 if ( !editor
&& grid
) // get renderer for the data type
2009 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2012 // if we still don't have one then use the grid default
2013 editor
= m_defGridAttr
->GetEditor(NULL
,0,0);
2017 wxFAIL_MSG(wxT("Missing default cell attribute"));
2023 // ----------------------------------------------------------------------------
2024 // wxGridCellAttrData
2025 // ----------------------------------------------------------------------------
2027 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2029 int n
= FindIndex(row
, col
);
2030 if ( n
== wxNOT_FOUND
)
2032 // add the attribute
2033 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2039 // change the attribute
2040 m_attrs
[(size_t)n
].attr
= attr
;
2044 // remove this attribute
2045 m_attrs
.RemoveAt((size_t)n
);
2050 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2052 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2054 int n
= FindIndex(row
, col
);
2055 if ( n
!= wxNOT_FOUND
)
2057 attr
= m_attrs
[(size_t)n
].attr
;
2064 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2066 size_t count
= m_attrs
.GetCount();
2067 for ( size_t n
= 0; n
< count
; n
++ )
2069 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2070 wxCoord row
= coords
.GetRow();
2071 if ((size_t)row
>= pos
)
2075 // If rows inserted, include row counter where necessary
2076 coords
.SetRow(row
+ numRows
);
2078 else if (numRows
< 0)
2080 // If rows deleted ...
2081 if ((size_t)row
>= pos
- numRows
)
2083 // ...either decrement row counter (if row still exists)...
2084 coords
.SetRow(row
+ numRows
);
2088 // ...or remove the attribute
2089 m_attrs
.RemoveAt((size_t)n
);
2097 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2099 size_t count
= m_attrs
.GetCount();
2100 for ( size_t n
= 0; n
< count
; n
++ )
2102 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2103 wxCoord col
= coords
.GetCol();
2104 if ( (size_t)col
>= pos
)
2108 // If rows inserted, include row counter where necessary
2109 coords
.SetCol(col
+ numCols
);
2111 else if (numCols
< 0)
2113 // If rows deleted ...
2114 if ((size_t)col
>= pos
- numCols
)
2116 // ...either decrement row counter (if row still exists)...
2117 coords
.SetCol(col
+ numCols
);
2121 // ...or remove the attribute
2122 m_attrs
.RemoveAt((size_t)n
);
2130 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2132 size_t count
= m_attrs
.GetCount();
2133 for ( size_t n
= 0; n
< count
; n
++ )
2135 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2136 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2145 // ----------------------------------------------------------------------------
2146 // wxGridRowOrColAttrData
2147 // ----------------------------------------------------------------------------
2149 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2151 size_t count
= m_attrs
.Count();
2152 for ( size_t n
= 0; n
< count
; n
++ )
2154 m_attrs
[n
]->DecRef();
2158 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2160 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2162 int n
= m_rowsOrCols
.Index(rowOrCol
);
2163 if ( n
!= wxNOT_FOUND
)
2165 attr
= m_attrs
[(size_t)n
];
2172 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2174 int i
= m_rowsOrCols
.Index(rowOrCol
);
2175 if ( i
== wxNOT_FOUND
)
2177 // add the attribute
2178 m_rowsOrCols
.Add(rowOrCol
);
2183 size_t n
= (size_t)i
;
2186 // change the attribute
2187 m_attrs
[n
]->DecRef();
2192 // remove this attribute
2193 m_attrs
[n
]->DecRef();
2194 m_rowsOrCols
.RemoveAt(n
);
2195 m_attrs
.RemoveAt(n
);
2200 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2202 size_t count
= m_attrs
.GetCount();
2203 for ( size_t n
= 0; n
< count
; n
++ )
2205 int & rowOrCol
= m_rowsOrCols
[n
];
2206 if ( (size_t)rowOrCol
>= pos
)
2208 if ( numRowsOrCols
> 0 )
2210 // If rows inserted, include row counter where necessary
2211 rowOrCol
+= numRowsOrCols
;
2213 else if ( numRowsOrCols
< 0)
2215 // If rows deleted, either decrement row counter (if row still exists)
2216 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2217 rowOrCol
+= numRowsOrCols
;
2220 m_rowsOrCols
.RemoveAt((size_t)n
);
2221 m_attrs
.RemoveAt((size_t)n
);
2229 // ----------------------------------------------------------------------------
2230 // wxGridCellAttrProvider
2231 // ----------------------------------------------------------------------------
2233 wxGridCellAttrProvider::wxGridCellAttrProvider()
2235 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2238 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2243 void wxGridCellAttrProvider::InitData()
2245 m_data
= new wxGridCellAttrProviderData
;
2248 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
) const
2250 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2253 // first look for the attribute of this specific cell
2254 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2258 // then look for the col attr (col attributes are more common than
2259 // the row ones, hence they have priority)
2260 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2265 // finally try the row attributes
2266 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2273 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2279 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2282 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2287 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2290 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2295 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2298 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2302 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2304 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2308 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2312 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2314 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2318 // ----------------------------------------------------------------------------
2319 // wxGridTypeRegistry
2320 // ----------------------------------------------------------------------------
2322 wxGridTypeRegistry::~wxGridTypeRegistry()
2324 size_t count
= m_typeinfo
.Count();
2325 for ( size_t i
= 0; i
< count
; i
++ )
2326 delete m_typeinfo
[i
];
2330 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2331 wxGridCellRenderer
* renderer
,
2332 wxGridCellEditor
* editor
)
2334 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2336 // is it already registered?
2337 int loc
= FindRegisteredDataType(typeName
);
2338 if ( loc
!= wxNOT_FOUND
)
2340 delete m_typeinfo
[loc
];
2341 m_typeinfo
[loc
] = info
;
2345 m_typeinfo
.Add(info
);
2349 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2351 size_t count
= m_typeinfo
.GetCount();
2352 for ( size_t i
= 0; i
< count
; i
++ )
2354 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2363 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2365 int index
= FindRegisteredDataType(typeName
);
2366 if ( index
== wxNOT_FOUND
)
2368 // check whether this is one of the standard ones, in which case
2369 // register it "on the fly"
2370 if ( typeName
== wxGRID_VALUE_STRING
)
2372 RegisterDataType(wxGRID_VALUE_STRING
,
2373 new wxGridCellStringRenderer
,
2374 new wxGridCellTextEditor
);
2376 else if ( typeName
== wxGRID_VALUE_BOOL
)
2378 RegisterDataType(wxGRID_VALUE_BOOL
,
2379 new wxGridCellBoolRenderer
,
2380 new wxGridCellBoolEditor
);
2382 else if ( typeName
== wxGRID_VALUE_NUMBER
)
2384 RegisterDataType(wxGRID_VALUE_NUMBER
,
2385 new wxGridCellNumberRenderer
,
2386 new wxGridCellNumberEditor
);
2388 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2390 RegisterDataType(wxGRID_VALUE_FLOAT
,
2391 new wxGridCellFloatRenderer
,
2392 new wxGridCellFloatEditor
);
2394 else if ( typeName
== wxGRID_VALUE_CHOICE
)
2396 RegisterDataType(wxGRID_VALUE_CHOICE
,
2397 new wxGridCellStringRenderer
,
2398 new wxGridCellChoiceEditor
);
2405 // we get here only if just added the entry for this type, so return
2407 index
= m_typeinfo
.GetCount() - 1;
2413 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2415 int index
= FindDataType(typeName
);
2416 if ( index
== wxNOT_FOUND
)
2418 // the first part of the typename is the "real" type, anything after ':'
2419 // are the parameters for the renderer
2420 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2421 if ( index
== wxNOT_FOUND
)
2426 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2427 wxGridCellRenderer
*rendererOld
= renderer
;
2428 renderer
= renderer
->Clone();
2429 rendererOld
->DecRef();
2431 wxGridCellEditor
*editor
= GetEditor(index
);
2432 wxGridCellEditor
*editorOld
= editor
;
2433 editor
= editor
->Clone();
2434 editorOld
->DecRef();
2436 // do it even if there are no parameters to reset them to defaults
2437 wxString params
= typeName
.AfterFirst(_T(':'));
2438 renderer
->SetParameters(params
);
2439 editor
->SetParameters(params
);
2441 // register the new typename
2442 RegisterDataType(typeName
, renderer
, editor
);
2444 // we just registered it, it's the last one
2445 index
= m_typeinfo
.GetCount() - 1;
2451 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2453 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2459 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2461 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2467 // ----------------------------------------------------------------------------
2469 // ----------------------------------------------------------------------------
2471 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2474 wxGridTableBase::wxGridTableBase()
2476 m_view
= (wxGrid
*) NULL
;
2477 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2480 wxGridTableBase::~wxGridTableBase()
2482 delete m_attrProvider
;
2485 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2487 delete m_attrProvider
;
2488 m_attrProvider
= attrProvider
;
2491 bool wxGridTableBase::CanHaveAttributes()
2493 if ( ! GetAttrProvider() )
2495 // use the default attr provider by default
2496 SetAttrProvider(new wxGridCellAttrProvider
);
2501 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
)
2503 if ( m_attrProvider
)
2504 return m_attrProvider
->GetAttr(row
, col
);
2506 return (wxGridCellAttr
*)NULL
;
2509 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2511 if ( m_attrProvider
)
2513 m_attrProvider
->SetAttr(attr
, row
, col
);
2517 // as we take ownership of the pointer and don't store it, we must
2523 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2525 if ( m_attrProvider
)
2527 m_attrProvider
->SetRowAttr(attr
, row
);
2531 // as we take ownership of the pointer and don't store it, we must
2537 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2539 if ( m_attrProvider
)
2541 m_attrProvider
->SetColAttr(attr
, col
);
2545 // as we take ownership of the pointer and don't store it, we must
2551 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2552 size_t WXUNUSED(numRows
) )
2554 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2559 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2561 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2566 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2567 size_t WXUNUSED(numRows
) )
2569 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2574 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2575 size_t WXUNUSED(numCols
) )
2577 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2582 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2584 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2589 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2590 size_t WXUNUSED(numCols
) )
2592 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2598 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2601 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2602 // how much it makes sense to us geeks.
2606 wxString
wxGridTableBase::GetColLabelValue( int col
)
2608 // default col labels are:
2609 // cols 0 to 25 : A-Z
2610 // cols 26 to 675 : AA-ZZ
2615 for ( n
= 1; ; n
++ )
2617 s
+= (_T('A') + (wxChar
)( col%26
));
2619 if ( col
< 0 ) break;
2622 // reverse the string...
2624 for ( i
= 0; i
< n
; i
++ )
2633 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
2635 return wxGRID_VALUE_STRING
;
2638 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
2639 const wxString
& typeName
)
2641 return typeName
== wxGRID_VALUE_STRING
;
2644 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
2646 return CanGetValueAs(row
, col
, typeName
);
2649 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
2654 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
2659 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
2664 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
2665 long WXUNUSED(value
) )
2669 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
2670 double WXUNUSED(value
) )
2674 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
2675 bool WXUNUSED(value
) )
2680 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2681 const wxString
& WXUNUSED(typeName
) )
2686 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
2687 const wxString
& WXUNUSED(typeName
),
2688 void* WXUNUSED(value
) )
2692 //////////////////////////////////////////////////////////////////////
2694 // Message class for the grid table to send requests and notifications
2698 wxGridTableMessage::wxGridTableMessage()
2700 m_table
= (wxGridTableBase
*) NULL
;
2706 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
2707 int commandInt1
, int commandInt2
)
2711 m_comInt1
= commandInt1
;
2712 m_comInt2
= commandInt2
;
2717 //////////////////////////////////////////////////////////////////////
2719 // A basic grid table for string data. An object of this class will
2720 // created by wxGrid if you don't specify an alternative table class.
2723 WX_DEFINE_OBJARRAY(wxGridStringArray
)
2725 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
2727 wxGridStringTable::wxGridStringTable()
2732 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
2737 m_data
.Alloc( numRows
);
2740 sa
.Alloc( numCols
);
2741 for ( col
= 0; col
< numCols
; col
++ )
2743 sa
.Add( wxEmptyString
);
2746 for ( row
= 0; row
< numRows
; row
++ )
2752 wxGridStringTable::~wxGridStringTable()
2756 int wxGridStringTable::GetNumberRows()
2758 return m_data
.GetCount();
2761 int wxGridStringTable::GetNumberCols()
2763 if ( m_data
.GetCount() > 0 )
2764 return m_data
[0].GetCount();
2769 wxString
wxGridStringTable::GetValue( int row
, int col
)
2771 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2772 _T("invalid row or column index in wxGridStringTable") );
2774 return m_data
[row
][col
];
2777 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
2779 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2780 _T("invalid row or column index in wxGridStringTable") );
2782 m_data
[row
][col
] = value
;
2785 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
2787 wxASSERT_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
2788 _T("invalid row or column index in wxGridStringTable") );
2790 return (m_data
[row
][col
] == wxEmptyString
);
2793 void wxGridStringTable::Clear()
2796 int numRows
, numCols
;
2798 numRows
= m_data
.GetCount();
2801 numCols
= m_data
[0].GetCount();
2803 for ( row
= 0; row
< numRows
; row
++ )
2805 for ( col
= 0; col
< numCols
; col
++ )
2807 m_data
[row
][col
] = wxEmptyString
;
2814 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
2818 size_t curNumRows
= m_data
.GetCount();
2819 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2820 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2822 if ( pos
>= curNumRows
)
2824 return AppendRows( numRows
);
2828 sa
.Alloc( curNumCols
);
2829 for ( col
= 0; col
< curNumCols
; col
++ )
2831 sa
.Add( wxEmptyString
);
2834 for ( row
= pos
; row
< pos
+ numRows
; row
++ )
2836 m_data
.Insert( sa
, row
);
2840 wxGridTableMessage
msg( this,
2841 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
2845 GetView()->ProcessTableMessage( msg
);
2851 bool wxGridStringTable::AppendRows( size_t numRows
)
2855 size_t curNumRows
= m_data
.GetCount();
2856 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2857 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2860 if ( curNumCols
> 0 )
2862 sa
.Alloc( curNumCols
);
2863 for ( col
= 0; col
< curNumCols
; col
++ )
2865 sa
.Add( wxEmptyString
);
2869 for ( row
= 0; row
< numRows
; row
++ )
2876 wxGridTableMessage
msg( this,
2877 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
2880 GetView()->ProcessTableMessage( msg
);
2886 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
2890 size_t curNumRows
= m_data
.GetCount();
2892 if ( pos
>= curNumRows
)
2895 errmsg
.Printf(wxT("Called wxGridStringTable::DeleteRows(pos=%d, N=%d)\nPos value is invalid for present table with %d rows"),
2896 pos
, numRows
, curNumRows
);
2897 wxFAIL_MSG( errmsg
);
2901 if ( numRows
> curNumRows
- pos
)
2903 numRows
= curNumRows
- pos
;
2906 if ( numRows
>= curNumRows
)
2908 m_data
.Empty(); // don't release memory just yet
2912 for ( n
= 0; n
< numRows
; n
++ )
2914 m_data
.Remove( pos
);
2919 wxGridTableMessage
msg( this,
2920 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
2924 GetView()->ProcessTableMessage( msg
);
2930 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
2934 size_t curNumRows
= m_data
.GetCount();
2935 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
2936 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
2938 if ( pos
>= curNumCols
)
2940 return AppendCols( numCols
);
2943 for ( row
= 0; row
< curNumRows
; row
++ )
2945 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
2947 m_data
[row
].Insert( wxEmptyString
, col
);
2952 wxGridTableMessage
msg( this,
2953 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
2957 GetView()->ProcessTableMessage( msg
);
2963 bool wxGridStringTable::AppendCols( size_t numCols
)
2967 size_t curNumRows
= m_data
.GetCount();
2971 // TODO: something better than this ?
2973 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
2978 for ( row
= 0; row
< curNumRows
; row
++ )
2980 for ( n
= 0; n
< numCols
; n
++ )
2982 m_data
[row
].Add( wxEmptyString
);
2988 wxGridTableMessage
msg( this,
2989 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
2992 GetView()->ProcessTableMessage( msg
);
2998 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3002 size_t curNumRows
= m_data
.GetCount();
3003 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3004 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3006 if ( pos
>= curNumCols
)
3009 errmsg
.Printf( wxT("Called wxGridStringTable::DeleteCols(pos=%d, N=%d)...\nPos value is invalid for present table with %d cols"),
3010 pos
, numCols
, curNumCols
);
3011 wxFAIL_MSG( errmsg
);
3015 if ( numCols
> curNumCols
- pos
)
3017 numCols
= curNumCols
- pos
;
3020 for ( row
= 0; row
< curNumRows
; row
++ )
3022 if ( numCols
>= curNumCols
)
3024 m_data
[row
].Clear();
3028 for ( n
= 0; n
< numCols
; n
++ )
3030 m_data
[row
].Remove( pos
);
3036 wxGridTableMessage
msg( this,
3037 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3041 GetView()->ProcessTableMessage( msg
);
3047 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3049 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3051 // using default label
3053 return wxGridTableBase::GetRowLabelValue( row
);
3057 return m_rowLabels
[ row
];
3061 wxString
wxGridStringTable::GetColLabelValue( int col
)
3063 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3065 // using default label
3067 return wxGridTableBase::GetColLabelValue( col
);
3071 return m_colLabels
[ col
];
3075 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3077 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3079 int n
= m_rowLabels
.GetCount();
3081 for ( i
= n
; i
<= row
; i
++ )
3083 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3087 m_rowLabels
[row
] = value
;
3090 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3092 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3094 int n
= m_colLabels
.GetCount();
3096 for ( i
= n
; i
<= col
; i
++ )
3098 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3102 m_colLabels
[col
] = value
;
3107 //////////////////////////////////////////////////////////////////////
3108 //////////////////////////////////////////////////////////////////////
3110 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3112 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3113 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3114 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3115 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3116 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3117 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3120 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3122 const wxPoint
&pos
, const wxSize
&size
)
3123 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3128 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3132 // NO - don't do this because it will set both the x and y origin
3133 // coords to match the parent scrolled window and we just want to
3134 // set the y coord - MB
3136 // m_owner->PrepareDC( dc );
3139 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3140 dc
.SetDeviceOrigin( 0, -y
);
3142 m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3143 m_owner
->DrawRowLabels( dc
);
3147 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3149 m_owner
->ProcessRowLabelMouseEvent( event
);
3153 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3155 m_owner
->GetEventHandler()->ProcessEvent(event
);
3159 // This seems to be required for wxMotif otherwise the mouse
3160 // cursor must be in the cell edit control to get key events
3162 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3164 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3167 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3169 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3174 //////////////////////////////////////////////////////////////////////
3176 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3178 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3179 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3180 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3181 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3182 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3183 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3186 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3188 const wxPoint
&pos
, const wxSize
&size
)
3189 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3194 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3198 // NO - don't do this because it will set both the x and y origin
3199 // coords to match the parent scrolled window and we just want to
3200 // set the x coord - MB
3202 // m_owner->PrepareDC( dc );
3205 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3206 dc
.SetDeviceOrigin( -x
, 0 );
3208 m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3209 m_owner
->DrawColLabels( dc
);
3213 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3215 m_owner
->ProcessColLabelMouseEvent( event
);
3218 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3220 m_owner
->GetEventHandler()->ProcessEvent(event
);
3224 // This seems to be required for wxMotif otherwise the mouse
3225 // cursor must be in the cell edit control to get key events
3227 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3229 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3232 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3234 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3239 //////////////////////////////////////////////////////////////////////
3241 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3243 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3244 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3245 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3246 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3247 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3248 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3251 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3253 const wxPoint
&pos
, const wxSize
&size
)
3254 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
)
3259 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3263 int client_height
= 0;
3264 int client_width
= 0;
3265 GetClientSize( &client_width
, &client_height
);
3267 dc
.SetPen( *wxBLACK_PEN
);
3268 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3269 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3271 dc
.SetPen( *wxWHITE_PEN
);
3272 dc
.DrawLine( 0, 0, client_width
, 0 );
3273 dc
.DrawLine( 0, 0, 0, client_height
);
3277 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3279 m_owner
->ProcessCornerLabelMouseEvent( event
);
3283 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3285 m_owner
->GetEventHandler()->ProcessEvent(event
);
3288 // This seems to be required for wxMotif otherwise the mouse
3289 // cursor must be in the cell edit control to get key events
3291 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3293 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3296 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3298 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3303 //////////////////////////////////////////////////////////////////////
3305 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxPanel
)
3307 BEGIN_EVENT_TABLE( wxGridWindow
, wxPanel
)
3308 EVT_PAINT( wxGridWindow::OnPaint
)
3309 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3310 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3311 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3312 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3313 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3316 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3317 wxGridRowLabelWindow
*rowLblWin
,
3318 wxGridColLabelWindow
*colLblWin
,
3319 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
3320 : wxPanel( parent
, id
, pos
, size
, wxWANTS_CHARS
, "grid window" )
3323 m_rowLabelWin
= rowLblWin
;
3324 m_colLabelWin
= colLblWin
;
3325 SetBackgroundColour( "WHITE" );
3329 wxGridWindow::~wxGridWindow()
3334 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3336 wxPaintDC
dc( this );
3337 m_owner
->PrepareDC( dc
);
3338 wxRegion reg
= GetUpdateRegion();
3339 m_owner
->CalcCellsExposed( reg
);
3340 m_owner
->DrawGridCellArea( dc
);
3341 #if WXGRID_DRAW_LINES
3342 m_owner
->DrawAllGridLines( dc
, reg
);
3344 m_owner
->DrawGridSpace( dc
);
3345 m_owner
->DrawHighlight( dc
);
3349 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3351 wxPanel::ScrollWindow( dx
, dy
, rect
);
3352 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3353 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3357 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3359 m_owner
->ProcessGridCellMouseEvent( event
);
3362 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3364 m_owner
->GetEventHandler()->ProcessEvent(event
);
3367 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3368 // cursor must be in the cell edit control to get key events
3370 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3372 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3375 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3377 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3380 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3385 //////////////////////////////////////////////////////////////////////
3388 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3390 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3391 EVT_PAINT( wxGrid::OnPaint
)
3392 EVT_SIZE( wxGrid::OnSize
)
3393 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3394 EVT_KEY_UP( wxGrid::OnKeyUp
)
3395 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3398 wxGrid::wxGrid( wxWindow
*parent
,
3403 const wxString
& name
)
3404 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3405 m_colMinWidths(GRID_HASH_SIZE
),
3406 m_rowMinHeights(GRID_HASH_SIZE
)
3415 wxSafeDecRef(m_defaultCellAttr
);
3417 #ifdef DEBUG_ATTR_CACHE
3418 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3419 wxPrintf(_T("wxGrid attribute cache statistics: "
3420 "total: %u, hits: %u (%u%%)\n"),
3421 total
, gs_nAttrCacheHits
,
3422 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3428 delete m_typeRegistry
;
3434 // ----- internal init and update functions
3437 void wxGrid::Create()
3439 m_created
= FALSE
; // set to TRUE by CreateGrid
3441 m_table
= (wxGridTableBase
*) NULL
;
3444 m_cellEditCtrlEnabled
= FALSE
;
3446 m_defaultCellAttr
= new wxGridCellAttr
;
3447 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3449 // Set default cell attributes
3450 m_defaultCellAttr
->SetFont(GetFont());
3451 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3452 m_defaultCellAttr
->SetTextColour(
3453 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT
));
3454 m_defaultCellAttr
->SetBackgroundColour(
3455 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
3456 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3457 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3462 m_currentCellCoords
= wxGridNoCellCoords
;
3464 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3465 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3467 // create the type registry
3468 m_typeRegistry
= new wxGridTypeRegistry
;
3470 // subwindow components that make up the wxGrid
3471 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3476 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3481 m_colLabelWin
= new wxGridColLabelWindow( this,
3486 m_gridWin
= new wxGridWindow( this,
3493 SetTargetWindow( m_gridWin
);
3497 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3498 wxGrid::wxGridSelectionModes selmode
)
3500 wxCHECK_MSG( !m_created
,
3502 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3504 m_numRows
= numRows
;
3505 m_numCols
= numCols
;
3507 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3508 m_table
->SetView( this );
3510 m_selection
= new wxGridSelection( this, selmode
);
3517 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3521 wxFAIL_MSG( wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3524 m_selection
->SetSelectionMode( selmode
);
3527 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3528 wxGrid::wxGridSelectionModes selmode
)
3532 // RD: Actually, this should probably be allowed. I think it would be
3533 // nice to be able to switch multiple Tables in and out of a single
3534 // View at runtime. Is there anything in the implmentation that would
3537 // At least, you now have to cope with m_selection
3538 wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3543 m_numRows
= table
->GetNumberRows();
3544 m_numCols
= table
->GetNumberCols();
3547 m_table
->SetView( this );
3550 m_selection
= new wxGridSelection( this, selmode
);
3561 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3562 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3564 if ( m_rowLabelWin
)
3566 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
3570 m_labelBackgroundColour
= wxColour( _T("WHITE") );
3573 m_labelTextColour
= wxColour( _T("BLACK") );
3576 m_attrCache
.row
= -1;
3578 // TODO: something better than this ?
3580 m_labelFont
= this->GetFont();
3581 m_labelFont
.SetWeight( m_labelFont
.GetWeight() + 2 );
3583 m_rowLabelHorizAlign
= wxALIGN_LEFT
;
3584 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
3586 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
3587 m_colLabelVertAlign
= wxALIGN_TOP
;
3589 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
3590 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
3592 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
3593 m_defaultRowHeight
+= 8;
3595 m_defaultRowHeight
+= 4;
3598 m_gridLineColour
= wxColour( 128, 128, 255 );
3599 m_gridLinesEnabled
= TRUE
;
3600 m_cellHighlightColour
= m_gridLineColour
;
3601 m_cellHighlightPenWidth
= 3;
3602 m_cellHighlightROPenWidth
= 1;
3604 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
3605 m_winCapture
= (wxWindow
*)NULL
;
3606 m_canDragRowSize
= TRUE
;
3607 m_canDragColSize
= TRUE
;
3608 m_canDragGridSize
= TRUE
;
3610 m_dragRowOrCol
= -1;
3611 m_isDragging
= FALSE
;
3612 m_startDragPos
= wxDefaultPosition
;
3614 m_waitForSlowClick
= FALSE
;
3616 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
3617 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
3619 m_currentCellCoords
= wxGridNoCellCoords
;
3621 m_selectingTopLeft
= wxGridNoCellCoords
;
3622 m_selectingBottomRight
= wxGridNoCellCoords
;
3623 m_selectionBackground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
);
3624 m_selectionForeground
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
3626 m_editable
= TRUE
; // default for whole grid
3628 m_inOnKeyDown
= FALSE
;
3637 // ----------------------------------------------------------------------------
3638 // the idea is to call these functions only when necessary because they create
3639 // quite big arrays which eat memory mostly unnecessary - in particular, if
3640 // default widths/heights are used for all rows/columns, we may not use these
3643 // with some extra code, it should be possible to only store the
3644 // widths/heights different from default ones but this will be done later...
3645 // ----------------------------------------------------------------------------
3647 void wxGrid::InitRowHeights()
3649 m_rowHeights
.Empty();
3650 m_rowBottoms
.Empty();
3652 m_rowHeights
.Alloc( m_numRows
);
3653 m_rowBottoms
.Alloc( m_numRows
);
3656 for ( int i
= 0; i
< m_numRows
; i
++ )
3658 m_rowHeights
.Add( m_defaultRowHeight
);
3659 rowBottom
+= m_defaultRowHeight
;
3660 m_rowBottoms
.Add( rowBottom
);
3664 void wxGrid::InitColWidths()
3666 m_colWidths
.Empty();
3667 m_colRights
.Empty();
3669 m_colWidths
.Alloc( m_numCols
);
3670 m_colRights
.Alloc( m_numCols
);
3672 for ( int i
= 0; i
< m_numCols
; i
++ )
3674 m_colWidths
.Add( m_defaultColWidth
);
3675 colRight
+= m_defaultColWidth
;
3676 m_colRights
.Add( colRight
);
3680 int wxGrid::GetColWidth(int col
) const
3682 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
3685 int wxGrid::GetColLeft(int col
) const
3687 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
3688 : m_colRights
[col
] - m_colWidths
[col
];
3691 int wxGrid::GetColRight(int col
) const
3693 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
3697 int wxGrid::GetRowHeight(int row
) const
3699 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
3702 int wxGrid::GetRowTop(int row
) const
3704 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
3705 : m_rowBottoms
[row
] - m_rowHeights
[row
];
3708 int wxGrid::GetRowBottom(int row
) const
3710 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
3711 : m_rowBottoms
[row
];
3714 void wxGrid::CalcDimensions()
3717 GetClientSize( &cw
, &ch
);
3719 if ( m_colLabelWin
->IsShown() )
3720 cw
-= m_rowLabelWidth
;
3721 if ( m_rowLabelWin
->IsShown() )
3722 ch
-= m_colLabelHeight
;
3725 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
3726 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
3728 // preserve (more or less) the previous position
3730 GetViewStart( &x
, &y
);
3731 // maybe we don't need scrollbars at all? and if we do, transform w and h
3732 // from pixels into logical units
3739 w
= (w
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3749 h
= (h
+ GRID_SCROLL_LINE
- 1)/GRID_SCROLL_LINE
;
3754 // do set scrollbar parameters
3755 SetScrollbars( GRID_SCROLL_LINE
, GRID_SCROLL_LINE
,
3756 w
, h
, x
, y
, (GetBatchCount() != 0));
3760 void wxGrid::CalcWindowSizes()
3763 GetClientSize( &cw
, &ch
);
3765 if ( m_cornerLabelWin
->IsShown() )
3766 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
3768 if ( m_colLabelWin
->IsShown() )
3769 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
3771 if ( m_rowLabelWin
->IsShown() )
3772 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
3774 if ( m_gridWin
->IsShown() )
3775 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
3779 // this is called when the grid table sends a message to say that it
3780 // has been redimensioned
3782 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
3785 bool result
= FALSE
;
3788 // if we were using the default widths/heights so far, we must change them
3790 if ( m_colWidths
.IsEmpty() )
3795 if ( m_rowHeights
.IsEmpty() )
3801 switch ( msg
.GetId() )
3803 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
3805 size_t pos
= msg
.GetCommandInt();
3806 int numRows
= msg
.GetCommandInt2();
3808 m_numRows
+= numRows
;
3810 if ( !m_rowHeights
.IsEmpty() )
3812 for ( i
= 0; i
< numRows
; i
++ )
3814 m_rowHeights
.Insert( m_defaultRowHeight
, pos
);
3815 m_rowBottoms
.Insert( 0, pos
);
3819 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
3821 for ( i
= pos
; i
< m_numRows
; i
++ )
3823 bottom
+= m_rowHeights
[i
];
3824 m_rowBottoms
[i
] = bottom
;
3827 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3829 // if we have just inserted cols into an empty grid the current
3830 // cell will be undefined...
3832 SetCurrentCell( 0, 0 );
3834 m_selection
->UpdateRows( pos
, numRows
);
3835 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3837 attrProvider
->UpdateAttrRows( pos
, numRows
);
3839 if ( !GetBatchCount() )
3842 m_rowLabelWin
->Refresh();
3848 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
3850 int numRows
= msg
.GetCommandInt();
3851 int oldNumRows
= m_numRows
;
3852 m_numRows
+= numRows
;
3854 if ( !m_rowHeights
.IsEmpty() )
3856 for ( i
= 0; i
< numRows
; i
++ )
3858 m_rowHeights
.Add( m_defaultRowHeight
);
3859 m_rowBottoms
.Add( 0 );
3863 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
3865 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
3867 bottom
+= m_rowHeights
[i
];
3868 m_rowBottoms
[i
] = bottom
;
3871 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3873 // if we have just inserted cols into an empty grid the current
3874 // cell will be undefined...
3876 SetCurrentCell( 0, 0 );
3878 if ( !GetBatchCount() )
3881 m_rowLabelWin
->Refresh();
3887 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
3889 size_t pos
= msg
.GetCommandInt();
3890 int numRows
= msg
.GetCommandInt2();
3891 m_numRows
-= numRows
;
3893 if ( !m_rowHeights
.IsEmpty() )
3895 for ( i
= 0; i
< numRows
; i
++ )
3897 m_rowHeights
.Remove( pos
);
3898 m_rowBottoms
.Remove( pos
);
3902 for ( i
= 0; i
< m_numRows
; i
++ )
3904 h
+= m_rowHeights
[i
];
3905 m_rowBottoms
[i
] = h
;
3910 m_currentCellCoords
= wxGridNoCellCoords
;
3914 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
3915 m_currentCellCoords
.Set( 0, 0 );
3917 m_selection
->UpdateRows( pos
, -((int)numRows
) );
3918 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3920 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
3921 // ifdef'd out following patch from Paul Gammans
3923 // No need to touch column attributes, unless we
3924 // removed _all_ rows, in this case, we remove
3925 // all column attributes.
3926 // I hate to do this here, but the
3927 // needed data is not available inside UpdateAttrRows.
3928 if ( !GetNumberRows() )
3929 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
3932 if ( !GetBatchCount() )
3935 m_rowLabelWin
->Refresh();
3941 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
3943 size_t pos
= msg
.GetCommandInt();
3944 int numCols
= msg
.GetCommandInt2();
3945 m_numCols
+= numCols
;
3947 if ( !m_colWidths
.IsEmpty() )
3949 for ( i
= 0; i
< numCols
; i
++ )
3951 m_colWidths
.Insert( m_defaultColWidth
, pos
);
3952 m_colRights
.Insert( 0, pos
);
3956 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
3958 for ( i
= pos
; i
< m_numCols
; i
++ )
3960 right
+= m_colWidths
[i
];
3961 m_colRights
[i
] = right
;
3964 if ( m_currentCellCoords
== wxGridNoCellCoords
)
3966 // if we have just inserted cols into an empty grid the current
3967 // cell will be undefined...
3969 SetCurrentCell( 0, 0 );
3971 m_selection
->UpdateCols( pos
, numCols
);
3972 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
3974 attrProvider
->UpdateAttrCols( pos
, numCols
);
3975 if ( !GetBatchCount() )
3978 m_colLabelWin
->Refresh();
3985 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
3987 int numCols
= msg
.GetCommandInt();
3988 int oldNumCols
= m_numCols
;
3989 m_numCols
+= numCols
;
3990 if ( !m_colWidths
.IsEmpty() )
3992 for ( i
= 0; i
< numCols
; i
++ )
3994 m_colWidths
.Add( m_defaultColWidth
);
3995 m_colRights
.Add( 0 );
3999 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4001 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4003 right
+= m_colWidths
[i
];
4004 m_colRights
[i
] = right
;
4007 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4009 // if we have just inserted cols into an empty grid the current
4010 // cell will be undefined...
4012 SetCurrentCell( 0, 0 );
4014 if ( !GetBatchCount() )
4017 m_colLabelWin
->Refresh();
4023 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4025 size_t pos
= msg
.GetCommandInt();
4026 int numCols
= msg
.GetCommandInt2();
4027 m_numCols
-= numCols
;
4029 if ( !m_colWidths
.IsEmpty() )
4031 for ( i
= 0; i
< numCols
; i
++ )
4033 m_colWidths
.Remove( pos
);
4034 m_colRights
.Remove( pos
);
4038 for ( i
= 0; i
< m_numCols
; i
++ )
4040 w
+= m_colWidths
[i
];
4046 m_currentCellCoords
= wxGridNoCellCoords
;
4050 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4051 m_currentCellCoords
.Set( 0, 0 );
4053 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4054 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4056 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4057 // ifdef'd out following patch from Paul Gammans
4059 // No need to touch row attributes, unless we
4060 // removed _all_ columns, in this case, we remove
4061 // all row attributes.
4062 // I hate to do this here, but the
4063 // needed data is not available inside UpdateAttrCols.
4064 if ( !GetNumberCols() )
4065 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4068 if ( !GetBatchCount() )
4071 m_colLabelWin
->Refresh();
4078 if (result
&& !GetBatchCount() )
4079 m_gridWin
->Refresh();
4084 void wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4086 wxRegionIterator
iter( reg
);
4089 m_rowLabelsExposed
.Empty();
4096 // TODO: remove this when we can...
4097 // There is a bug in wxMotif that gives garbage update
4098 // rectangles if you jump-scroll a long way by clicking the
4099 // scrollbar with middle button. This is a work-around
4101 #if defined(__WXMOTIF__)
4103 m_gridWin
->GetClientSize( &cw
, &ch
);
4104 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4105 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4108 // logical bounds of update region
4111 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4112 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4114 // find the row labels within these bounds
4117 for ( row
= 0; row
< m_numRows
; row
++ )
4119 if ( GetRowBottom(row
) < top
)
4122 if ( GetRowTop(row
) > bottom
)
4125 m_rowLabelsExposed
.Add( row
);
4133 void wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4135 wxRegionIterator
iter( reg
);
4138 m_colLabelsExposed
.Empty();
4145 // TODO: remove this when we can...
4146 // There is a bug in wxMotif that gives garbage update
4147 // rectangles if you jump-scroll a long way by clicking the
4148 // scrollbar with middle button. This is a work-around
4150 #if defined(__WXMOTIF__)
4152 m_gridWin
->GetClientSize( &cw
, &ch
);
4153 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4154 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4157 // logical bounds of update region
4160 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4161 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4163 // find the cells within these bounds
4166 for ( col
= 0; col
< m_numCols
; col
++ )
4168 if ( GetColRight(col
) < left
)
4171 if ( GetColLeft(col
) > right
)
4174 m_colLabelsExposed
.Add( col
);
4182 void wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4184 wxRegionIterator
iter( reg
);
4187 m_cellsExposed
.Empty();
4188 m_rowsExposed
.Empty();
4189 m_colsExposed
.Empty();
4191 int left
, top
, right
, bottom
;
4196 // TODO: remove this when we can...
4197 // There is a bug in wxMotif that gives garbage update
4198 // rectangles if you jump-scroll a long way by clicking the
4199 // scrollbar with middle button. This is a work-around
4201 #if defined(__WXMOTIF__)
4203 m_gridWin
->GetClientSize( &cw
, &ch
);
4204 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4205 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4206 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4207 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4210 // logical bounds of update region
4212 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4213 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4215 // find the cells within these bounds
4218 for ( row
= 0; row
< m_numRows
; row
++ )
4220 if ( GetRowBottom(row
) <= top
)
4223 if ( GetRowTop(row
) > bottom
)
4226 m_rowsExposed
.Add( row
);
4228 for ( col
= 0; col
< m_numCols
; col
++ )
4230 if ( GetColRight(col
) <= left
)
4233 if ( GetColLeft(col
) > right
)
4236 if ( m_colsExposed
.Index( col
) == wxNOT_FOUND
)
4237 m_colsExposed
.Add( col
);
4238 m_cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4247 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4250 wxPoint
pos( event
.GetPosition() );
4251 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4253 if ( event
.Dragging() )
4255 m_isDragging
= TRUE
;
4257 if ( event
.LeftIsDown() )
4259 switch( m_cursorMode
)
4261 case WXGRID_CURSOR_RESIZE_ROW
:
4263 int cw
, ch
, left
, dummy
;
4264 m_gridWin
->GetClientSize( &cw
, &ch
);
4265 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4267 wxClientDC
dc( m_gridWin
);
4270 GetRowTop(m_dragRowOrCol
) +
4271 GetRowMinimalHeight(m_dragRowOrCol
) );
4272 dc
.SetLogicalFunction(wxINVERT
);
4273 if ( m_dragLastPos
>= 0 )
4275 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4277 dc
.DrawLine( left
, y
, left
+cw
, y
);
4282 case WXGRID_CURSOR_SELECT_ROW
:
4283 if ( (row
= YToRow( y
)) >= 0 )
4285 m_selection
->SelectRow( row
,
4286 event
.ControlDown(),
4292 // default label to suppress warnings about "enumeration value
4293 // 'xxx' not handled in switch
4301 m_isDragging
= FALSE
;
4304 // ------------ Entering or leaving the window
4306 if ( event
.Entering() || event
.Leaving() )
4308 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4312 // ------------ Left button pressed
4314 else if ( event
.LeftDown() )
4316 // don't send a label click event for a hit on the
4317 // edge of the row label - this is probably the user
4318 // wanting to resize the row
4320 if ( YToEdgeOfRow(y
) < 0 )
4324 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4326 if ( !event
.ShiftDown() && !event
.ControlDown() )
4328 if ( event
.ShiftDown() )
4329 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4332 GetNumberCols() - 1,
4333 event
.ControlDown(),
4338 m_selection
->SelectRow( row
,
4339 event
.ControlDown(),
4343 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4348 // starting to drag-resize a row
4350 if ( CanDragRowSize() )
4351 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4356 // ------------ Left double click
4358 else if (event
.LeftDClick() )
4360 if ( YToEdgeOfRow(y
) < 0 )
4363 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
);
4368 // ------------ Left button released
4370 else if ( event
.LeftUp() )
4372 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4374 DoEndDragResizeRow();
4376 // Note: we are ending the event *after* doing
4377 // default processing in this case
4379 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4382 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4387 // ------------ Right button down
4389 else if ( event
.RightDown() )
4392 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4394 // no default action at the moment
4399 // ------------ Right double click
4401 else if ( event
.RightDClick() )
4404 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4406 // no default action at the moment
4411 // ------------ No buttons down and mouse moving
4413 else if ( event
.Moving() )
4415 m_dragRowOrCol
= YToEdgeOfRow( y
);
4416 if ( m_dragRowOrCol
>= 0 )
4418 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4420 // don't capture the mouse yet
4421 if ( CanDragRowSize() )
4422 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4425 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4427 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4433 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4436 wxPoint
pos( event
.GetPosition() );
4437 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4439 if ( event
.Dragging() )
4441 m_isDragging
= TRUE
;
4443 if ( event
.LeftIsDown() )
4445 switch( m_cursorMode
)
4447 case WXGRID_CURSOR_RESIZE_COL
:
4449 int cw
, ch
, dummy
, top
;
4450 m_gridWin
->GetClientSize( &cw
, &ch
);
4451 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4453 wxClientDC
dc( m_gridWin
);
4456 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4457 GetColMinimalWidth(m_dragRowOrCol
));
4458 dc
.SetLogicalFunction(wxINVERT
);
4459 if ( m_dragLastPos
>= 0 )
4461 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4463 dc
.DrawLine( x
, top
, x
, top
+ch
);
4468 case WXGRID_CURSOR_SELECT_COL
:
4469 if ( (col
= XToCol( x
)) >= 0 )
4471 m_selection
->SelectCol( col
,
4472 event
.ControlDown(),
4478 // default label to suppress warnings about "enumeration value
4479 // 'xxx' not handled in switch
4487 m_isDragging
= FALSE
;
4490 // ------------ Entering or leaving the window
4492 if ( event
.Entering() || event
.Leaving() )
4494 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4498 // ------------ Left button pressed
4500 else if ( event
.LeftDown() )
4502 // don't send a label click event for a hit on the
4503 // edge of the col label - this is probably the user
4504 // wanting to resize the col
4506 if ( XToEdgeOfCol(x
) < 0 )
4510 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
4512 if ( !event
.ShiftDown() && !event
.ControlDown() )
4514 if ( event
.ShiftDown() )
4515 m_selection
->SelectBlock( 0,
4516 m_currentCellCoords
.GetCol(),
4517 GetNumberRows() - 1, col
,
4518 event
.ControlDown(),
4523 m_selection
->SelectCol( col
,
4524 event
.ControlDown(),
4528 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
4533 // starting to drag-resize a col
4535 if ( CanDragColSize() )
4536 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
4541 // ------------ Left double click
4543 if ( event
.LeftDClick() )
4545 if ( XToEdgeOfCol(x
) < 0 )
4548 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
);
4553 // ------------ Left button released
4555 else if ( event
.LeftUp() )
4557 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4559 DoEndDragResizeCol();
4561 // Note: we are ending the event *after* doing
4562 // default processing in this case
4564 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4567 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4572 // ------------ Right button down
4574 else if ( event
.RightDown() )
4577 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
4579 // no default action at the moment
4584 // ------------ Right double click
4586 else if ( event
.RightDClick() )
4589 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
4591 // no default action at the moment
4596 // ------------ No buttons down and mouse moving
4598 else if ( event
.Moving() )
4600 m_dragRowOrCol
= XToEdgeOfCol( x
);
4601 if ( m_dragRowOrCol
>= 0 )
4603 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4605 // don't capture the cursor yet
4606 if ( CanDragColSize() )
4607 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
4610 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4612 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
4618 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
4620 if ( event
.LeftDown() )
4622 // indicate corner label by having both row and
4625 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
4631 else if ( event
.LeftDClick() )
4633 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
4636 else if ( event
.RightDown() )
4638 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
4640 // no default action at the moment
4644 else if ( event
.RightDClick() )
4646 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
4648 // no default action at the moment
4653 void wxGrid::ChangeCursorMode(CursorMode mode
,
4658 static const wxChar
*cursorModes
[] =
4667 wxLogTrace(_T("grid"),
4668 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
4669 win
== m_colLabelWin
? _T("colLabelWin")
4670 : win
? _T("rowLabelWin")
4672 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
4673 #endif // __WXDEBUG__
4675 if ( mode
== m_cursorMode
&&
4676 win
== m_winCapture
&&
4677 captureMouse
== (m_winCapture
!= NULL
))
4682 // by default use the grid itself
4688 m_winCapture
->ReleaseMouse();
4689 m_winCapture
= (wxWindow
*)NULL
;
4692 m_cursorMode
= mode
;
4694 switch ( m_cursorMode
)
4696 case WXGRID_CURSOR_RESIZE_ROW
:
4697 win
->SetCursor( m_rowResizeCursor
);
4700 case WXGRID_CURSOR_RESIZE_COL
:
4701 win
->SetCursor( m_colResizeCursor
);
4705 win
->SetCursor( *wxSTANDARD_CURSOR
);
4708 // we need to capture mouse when resizing
4709 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
4710 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
4712 if ( captureMouse
&& resize
)
4714 win
->CaptureMouse();
4719 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
4722 wxPoint
pos( event
.GetPosition() );
4723 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4725 wxGridCellCoords coords
;
4726 XYToCell( x
, y
, coords
);
4728 if ( event
.Dragging() )
4730 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
4732 // Don't start doing anything until the mouse has been drug at
4733 // least 3 pixels in any direction...
4736 if (m_startDragPos
== wxDefaultPosition
)
4738 m_startDragPos
= pos
;
4741 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
4745 m_isDragging
= TRUE
;
4746 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4748 // Hide the edit control, so it
4749 // won't interfer with drag-shrinking.
4750 if ( IsCellEditControlShown() )
4752 HideCellEditControl();
4753 SaveEditControlValue();
4756 // Have we captured the mouse yet?
4759 m_winCapture
= m_gridWin
;
4760 m_winCapture
->CaptureMouse();
4763 if ( coords
!= wxGridNoCellCoords
)
4765 if ( event
.ControlDown() )
4767 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
4768 m_selectingKeyboard
= coords
;
4769 HighlightBlock ( m_selectingKeyboard
, coords
);
4773 if ( !IsSelection() )
4775 HighlightBlock( coords
, coords
);
4779 HighlightBlock( m_currentCellCoords
, coords
);
4783 if (! IsVisible(coords
))
4785 MakeCellVisible(coords
);
4786 // TODO: need to introduce a delay or something here. The
4787 // scrolling is way to fast, at least on MSW - also on GTK.
4791 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4793 int cw
, ch
, left
, dummy
;
4794 m_gridWin
->GetClientSize( &cw
, &ch
);
4795 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4797 wxClientDC
dc( m_gridWin
);
4799 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
4800 GetRowMinimalHeight(m_dragRowOrCol
) );
4801 dc
.SetLogicalFunction(wxINVERT
);
4802 if ( m_dragLastPos
>= 0 )
4804 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4806 dc
.DrawLine( left
, y
, left
+cw
, y
);
4809 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4811 int cw
, ch
, dummy
, top
;
4812 m_gridWin
->GetClientSize( &cw
, &ch
);
4813 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4815 wxClientDC
dc( m_gridWin
);
4817 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4818 GetColMinimalWidth(m_dragRowOrCol
) );
4819 dc
.SetLogicalFunction(wxINVERT
);
4820 if ( m_dragLastPos
>= 0 )
4822 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
4824 dc
.DrawLine( x
, top
, x
, top
+ch
);
4831 m_isDragging
= FALSE
;
4832 m_startDragPos
= wxDefaultPosition
;
4834 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
4835 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
4838 if ( event
.Entering() || event
.Leaving() )
4840 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4841 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
4846 // ------------ Left button pressed
4848 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
4850 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
4855 if ( !event
.ControlDown() )
4857 if ( event
.ShiftDown() )
4859 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4860 m_currentCellCoords
.GetCol(),
4863 event
.ControlDown(),
4868 else if ( XToEdgeOfCol(x
) < 0 &&
4869 YToEdgeOfRow(y
) < 0 )
4871 DisableCellEditControl();
4872 MakeCellVisible( coords
);
4874 // if this is the second click on this cell then start
4876 if ( m_waitForSlowClick
&&
4877 (coords
== m_currentCellCoords
) &&
4878 CanEnableCellControl())
4880 EnableCellEditControl();
4882 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
4883 wxGridCellEditor
*editor
= attr
->GetEditor(this,
4886 editor
->StartingClick();
4890 m_waitForSlowClick
= FALSE
;
4894 if ( event
.ControlDown() )
4896 m_selection
->ToggleCellSelection( coords
.GetRow(),
4898 event
.ControlDown(),
4902 m_selectingTopLeft
= wxGridNoCellCoords
;
4903 m_selectingBottomRight
= wxGridNoCellCoords
;
4904 m_selectingKeyboard
= coords
;
4908 SetCurrentCell( coords
);
4909 if ( m_selection
->GetSelectionMode()
4910 != wxGrid::wxGridSelectCells
)
4911 HighlightBlock( coords
, coords
);
4913 m_waitForSlowClick
= TRUE
;
4920 // ------------ Left double click
4922 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
4924 DisableCellEditControl();
4926 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
4928 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
4936 // ------------ Left button released
4938 else if ( event
.LeftUp() )
4940 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4942 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
4943 m_selectingBottomRight
!= wxGridNoCellCoords
)
4947 m_winCapture
->ReleaseMouse();
4948 m_winCapture
= NULL
;
4950 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
4951 m_selectingTopLeft
.GetCol(),
4952 m_selectingBottomRight
.GetRow(),
4953 m_selectingBottomRight
.GetCol(),
4954 event
.ControlDown(),
4958 m_selectingTopLeft
= wxGridNoCellCoords
;
4959 m_selectingBottomRight
= wxGridNoCellCoords
;
4962 // Show the edit control, if it has been hidden for
4964 ShowCellEditControl();
4966 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4969 DoEndDragResizeRow();
4971 // Note: we are ending the event *after* doing
4972 // default processing in this case
4974 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4976 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
4978 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
4979 DoEndDragResizeCol();
4981 // Note: we are ending the event *after* doing
4982 // default processing in this case
4984 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
4991 // ------------ Right button down
4993 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
4995 DisableCellEditControl();
4996 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5001 // no default action at the moment
5006 // ------------ Right double click
5008 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5010 DisableCellEditControl();
5011 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5016 // no default action at the moment
5020 // ------------ Moving and no button action
5022 else if ( event
.Moving() && !event
.IsButton() )
5024 int dragRow
= YToEdgeOfRow( y
);
5025 int dragCol
= XToEdgeOfCol( x
);
5027 // Dragging on the corner of a cell to resize in both
5028 // directions is not implemented yet...
5030 if ( dragRow
>= 0 && dragCol
>= 0 )
5032 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5038 m_dragRowOrCol
= dragRow
;
5040 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5042 if ( CanDragRowSize() && CanDragGridSize() )
5043 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5048 m_dragRowOrCol
= dragCol
;
5056 m_dragRowOrCol
= dragCol
;
5058 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5060 if ( CanDragColSize() && CanDragGridSize() )
5061 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5067 // Neither on a row or col edge
5069 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5071 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5077 void wxGrid::DoEndDragResizeRow()
5079 if ( m_dragLastPos
>= 0 )
5081 // erase the last line and resize the row
5083 int cw
, ch
, left
, dummy
;
5084 m_gridWin
->GetClientSize( &cw
, &ch
);
5085 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5087 wxClientDC
dc( m_gridWin
);
5089 dc
.SetLogicalFunction( wxINVERT
);
5090 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5091 HideCellEditControl();
5092 SaveEditControlValue();
5094 int rowTop
= GetRowTop(m_dragRowOrCol
);
5095 SetRowSize( m_dragRowOrCol
,
5096 wxMax( m_dragLastPos
- rowTop
, WXGRID_MIN_ROW_HEIGHT
) );
5098 if ( !GetBatchCount() )
5100 // Only needed to get the correct rect.y:
5101 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5103 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5104 rect
.width
= m_rowLabelWidth
;
5105 rect
.height
= ch
- rect
.y
;
5106 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5108 m_gridWin
->Refresh( FALSE
, &rect
);
5111 ShowCellEditControl();
5116 void wxGrid::DoEndDragResizeCol()
5118 if ( m_dragLastPos
>= 0 )
5120 // erase the last line and resize the col
5122 int cw
, ch
, dummy
, top
;
5123 m_gridWin
->GetClientSize( &cw
, &ch
);
5124 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5126 wxClientDC
dc( m_gridWin
);
5128 dc
.SetLogicalFunction( wxINVERT
);
5129 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5130 HideCellEditControl();
5131 SaveEditControlValue();
5133 int colLeft
= GetColLeft(m_dragRowOrCol
);
5134 SetColSize( m_dragRowOrCol
,
5135 wxMax( m_dragLastPos
- colLeft
,
5136 GetColMinimalWidth(m_dragRowOrCol
) ) );
5138 if ( !GetBatchCount() )
5140 // Only needed to get the correct rect.x:
5141 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5143 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5144 rect
.width
= cw
- rect
.x
;
5145 rect
.height
= m_colLabelHeight
;
5146 m_colLabelWin
->Refresh( TRUE
, &rect
);
5148 m_gridWin
->Refresh( FALSE
, &rect
);
5151 ShowCellEditControl();
5158 // ------ interaction with data model
5160 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5162 switch ( msg
.GetId() )
5164 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5165 return GetModelValues();
5167 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5168 return SetModelValues();
5170 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5171 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5172 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5173 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5174 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5175 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5176 return Redimension( msg
);
5185 // The behaviour of this function depends on the grid table class
5186 // Clear() function. For the default wxGridStringTable class the
5187 // behavious is to replace all cell contents with wxEmptyString but
5188 // not to change the number of rows or cols.
5190 void wxGrid::ClearGrid()
5194 if (IsCellEditControlEnabled())
5195 DisableCellEditControl();
5198 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5203 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5205 // TODO: something with updateLabels flag
5209 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5215 if (IsCellEditControlEnabled())
5216 DisableCellEditControl();
5218 return m_table
->InsertRows( pos
, numRows
);
5220 // the table will have sent the results of the insert row
5221 // operation to this view object as a grid table message
5227 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5229 // TODO: something with updateLabels flag
5233 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5237 return ( m_table
&& m_table
->AppendRows( numRows
) );
5238 // the table will have sent the results of the append row
5239 // operation to this view object as a grid table message
5243 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5245 // TODO: something with updateLabels flag
5249 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5255 if (IsCellEditControlEnabled())
5256 DisableCellEditControl();
5258 return (m_table
->DeleteRows( pos
, numRows
));
5259 // the table will have sent the results of the delete row
5260 // operation to this view object as a grid table message
5266 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5268 // TODO: something with updateLabels flag
5272 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5278 if (IsCellEditControlEnabled())
5279 DisableCellEditControl();
5281 return m_table
->InsertCols( pos
, numCols
);
5282 // the table will have sent the results of the insert col
5283 // operation to this view object as a grid table message
5289 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5291 // TODO: something with updateLabels flag
5295 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5299 return ( m_table
&& m_table
->AppendCols( numCols
) );
5300 // the table will have sent the results of the append col
5301 // operation to this view object as a grid table message
5305 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5307 // TODO: something with updateLabels flag
5311 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5317 if (IsCellEditControlEnabled())
5318 DisableCellEditControl();
5320 return ( m_table
->DeleteCols( pos
, numCols
) );
5321 // the table will have sent the results of the delete col
5322 // operation to this view object as a grid table message
5330 // ----- event handlers
5333 // Generate a grid event based on a mouse event and
5334 // return the result of ProcessEvent()
5336 bool wxGrid::SendEvent( const wxEventType type
,
5338 wxMouseEvent
& mouseEv
)
5340 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5342 int rowOrCol
= (row
== -1 ? col
: row
);
5344 wxGridSizeEvent
gridEvt( GetId(),
5348 mouseEv
.GetX() + GetRowLabelSize(),
5349 mouseEv
.GetY() + GetColLabelSize(),
5350 mouseEv
.ControlDown(),
5351 mouseEv
.ShiftDown(),
5353 mouseEv
.MetaDown() );
5354 return GetEventHandler()->ProcessEvent(gridEvt
);
5356 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
5358 // Right now, it should _never_ end up here!
5359 wxGridRangeSelectEvent
gridEvt( GetId(),
5363 m_selectingBottomRight
,
5365 mouseEv
.ControlDown(),
5366 mouseEv
.ShiftDown(),
5368 mouseEv
.MetaDown() );
5370 return GetEventHandler()->ProcessEvent(gridEvt
);
5374 wxGridEvent
gridEvt( GetId(),
5378 mouseEv
.GetX() + GetRowLabelSize(),
5379 mouseEv
.GetY() + GetColLabelSize(),
5381 mouseEv
.ControlDown(),
5382 mouseEv
.ShiftDown(),
5384 mouseEv
.MetaDown() );
5385 return GetEventHandler()->ProcessEvent(gridEvt
);
5390 // Generate a grid event of specified type and return the result
5391 // of ProcessEvent().
5393 bool wxGrid::SendEvent( const wxEventType type
,
5396 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5398 int rowOrCol
= (row
== -1 ? col
: row
);
5400 wxGridSizeEvent
gridEvt( GetId(),
5405 return GetEventHandler()->ProcessEvent(gridEvt
);
5409 wxGridEvent
gridEvt( GetId(),
5414 return GetEventHandler()->ProcessEvent(gridEvt
);
5419 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
5421 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
5425 // This is just here to make sure that CalcDimensions gets called when
5426 // the grid view is resized... then the size event is skipped to allow
5427 // the box sizers to handle everything
5429 void wxGrid::OnSize( wxSizeEvent
& WXUNUSED(event
) )
5436 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
5438 if ( m_inOnKeyDown
)
5440 // shouldn't be here - we are going round in circles...
5442 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
5445 m_inOnKeyDown
= TRUE
;
5447 // propagate the event up and see if it gets processed
5449 wxWindow
*parent
= GetParent();
5450 wxKeyEvent
keyEvt( event
);
5451 keyEvt
.SetEventObject( parent
);
5453 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
5456 // try local handlers
5458 switch ( event
.KeyCode() )
5461 if ( event
.ControlDown() )
5463 MoveCursorUpBlock( event
.ShiftDown() );
5467 MoveCursorUp( event
.ShiftDown() );
5472 if ( event
.ControlDown() )
5474 MoveCursorDownBlock( event
.ShiftDown() );
5478 MoveCursorDown( event
.ShiftDown() );
5483 if ( event
.ControlDown() )
5485 MoveCursorLeftBlock( event
.ShiftDown() );
5489 MoveCursorLeft( event
.ShiftDown() );
5494 if ( event
.ControlDown() )
5496 MoveCursorRightBlock( event
.ShiftDown() );
5500 MoveCursorRight( event
.ShiftDown() );
5505 case WXK_NUMPAD_ENTER
:
5506 if ( event
.ControlDown() )
5508 event
.Skip(); // to let the edit control have the return
5512 if ( GetGridCursorRow() < GetNumberRows()-1 )
5514 MoveCursorDown( event
.ShiftDown() );
5518 // at the bottom of a column
5519 HideCellEditControl();
5520 SaveEditControlValue();
5530 if (event
.ShiftDown())
5532 if ( GetGridCursorCol() > 0 )
5534 MoveCursorLeft( FALSE
);
5539 HideCellEditControl();
5540 SaveEditControlValue();
5545 if ( GetGridCursorCol() < GetNumberCols()-1 )
5547 MoveCursorRight( FALSE
);
5552 HideCellEditControl();
5553 SaveEditControlValue();
5559 if ( event
.ControlDown() )
5561 MakeCellVisible( 0, 0 );
5562 SetCurrentCell( 0, 0 );
5571 if ( event
.ControlDown() )
5573 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
5574 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
5591 if ( event
.ControlDown() )
5593 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
5594 m_currentCellCoords
.GetCol(),
5595 event
.ControlDown(),
5601 if ( !IsEditable() )
5603 MoveCursorRight( FALSE
);
5606 // Otherwise fall through to default
5609 // is it possible to edit the current cell at all?
5610 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
5612 // yes, now check whether the cells editor accepts the key
5613 int row
= m_currentCellCoords
.GetRow();
5614 int col
= m_currentCellCoords
.GetCol();
5615 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5616 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5618 // <F2> is special and will always start editing, for
5619 // other keys - ask the editor itself
5620 if ( (event
.KeyCode() == WXK_F2
&& !event
.HasModifiers())
5621 || editor
->IsAcceptedKey(event
) )
5623 EnableCellEditControl();
5624 editor
->StartingKey(event
);
5636 // let others process char events with modifiers or all
5637 // char events for readonly cells
5644 m_inOnKeyDown
= FALSE
;
5647 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
5649 // try local handlers
5651 if ( event
.KeyCode() == WXK_SHIFT
)
5653 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5654 m_selectingBottomRight
!= wxGridNoCellCoords
)
5655 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5656 m_selectingTopLeft
.GetCol(),
5657 m_selectingBottomRight
.GetRow(),
5658 m_selectingBottomRight
.GetCol(),
5659 event
.ControlDown(),
5663 m_selectingTopLeft
= wxGridNoCellCoords
;
5664 m_selectingBottomRight
= wxGridNoCellCoords
;
5665 m_selectingKeyboard
= wxGridNoCellCoords
;
5669 void wxGrid::OnEraseBackground(wxEraseEvent
&)
5673 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
5675 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
5677 // the event has been intercepted - do nothing
5681 wxClientDC
dc(m_gridWin
);
5684 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
5686 HideCellEditControl();
5687 DisableCellEditControl();
5689 if ( IsVisible( m_currentCellCoords
, FALSE
) )
5692 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
5693 if ( !m_gridLinesEnabled
)
5701 CalcCellsExposed( r
);
5703 // Otherwise refresh redraws the highlight!
5704 m_currentCellCoords
= coords
;
5706 DrawGridCellArea(dc
);
5707 DrawAllGridLines( dc
, r
);
5711 m_currentCellCoords
= coords
;
5713 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5714 DrawCellHighlight(dc
, attr
);
5719 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
5722 wxGridCellCoords updateTopLeft
, updateBottomRight
;
5724 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
5727 rightCol
= GetNumberCols() - 1;
5729 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
5732 bottomRow
= GetNumberRows() - 1;
5734 if ( topRow
> bottomRow
)
5741 if ( leftCol
> rightCol
)
5748 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
5749 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
5751 if ( m_selectingTopLeft
!= updateTopLeft
||
5752 m_selectingBottomRight
!= updateBottomRight
)
5754 // Compute two optimal update rectangles:
5755 // Either one rectangle is a real subset of the
5756 // other, or they are (almost) disjoint!
5758 bool need_refresh
[4];
5762 need_refresh
[3] = FALSE
;
5765 // Store intermediate values
5766 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
5767 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
5768 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
5769 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
5771 // Determine the outer/inner coordinates.
5772 if (oldLeft
> leftCol
)
5778 if (oldTop
> topRow
)
5784 if (oldRight
< rightCol
)
5787 oldRight
= rightCol
;
5790 if (oldBottom
< bottomRow
)
5793 oldBottom
= bottomRow
;
5797 // Now, either the stuff marked old is the outer
5798 // rectangle or we don't have a situation where one
5799 // is contained in the other.
5801 if ( oldLeft
< leftCol
)
5803 need_refresh
[0] = TRUE
;
5804 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5806 wxGridCellCoords ( oldBottom
,
5810 if ( oldTop
< topRow
)
5812 need_refresh
[1] = TRUE
;
5813 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5815 wxGridCellCoords ( topRow
- 1,
5819 if ( oldRight
> rightCol
)
5821 need_refresh
[2] = TRUE
;
5822 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
5824 wxGridCellCoords ( oldBottom
,
5828 if ( oldBottom
> bottomRow
)
5830 need_refresh
[3] = TRUE
;
5831 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
5833 wxGridCellCoords ( oldBottom
,
5839 m_selectingTopLeft
= updateTopLeft
;
5840 m_selectingBottomRight
= updateBottomRight
;
5842 // various Refresh() calls
5843 for (i
= 0; i
< 4; i
++ )
5844 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
5845 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
5848 // never generate an event as it will be generated from
5849 // wxGridSelection::SelectBlock!
5850 // (old comment from when this was the body of SelectBlock)
5854 // ------ functions to get/send data (see also public functions)
5857 bool wxGrid::GetModelValues()
5861 // all we need to do is repaint the grid
5863 m_gridWin
->Refresh();
5871 bool wxGrid::SetModelValues()
5877 for ( row
= 0; row
< m_numRows
; row
++ )
5879 for ( col
= 0; col
< m_numCols
; col
++ )
5881 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
5893 // Note - this function only draws cells that are in the list of
5894 // exposed cells (usually set from the update region by
5895 // CalcExposedCells)
5897 void wxGrid::DrawGridCellArea( wxDC
& dc
)
5899 if ( !m_numRows
|| !m_numCols
) return;
5902 size_t numCells
= m_cellsExposed
.GetCount();
5904 for ( i
= 0; i
< numCells
; i
++ )
5906 DrawCell( dc
, m_cellsExposed
[i
] );
5911 void wxGrid::DrawGridSpace( wxDC
& dc
)
5914 m_gridWin
->GetClientSize( &cw
, &ch
);
5917 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
5919 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
5920 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
5922 if ( right
> rightCol
|| bottom
> bottomRow
)
5925 CalcUnscrolledPosition( 0, 0, &left
, &top
);
5927 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
5928 dc
.SetPen( *wxTRANSPARENT_PEN
);
5930 if ( right
> rightCol
)
5932 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
5935 if ( bottom
> bottomRow
)
5937 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
5943 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
5945 int row
= coords
.GetRow();
5946 int col
= coords
.GetCol();
5948 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5951 // we draw the cell border ourselves
5952 #if !WXGRID_DRAW_LINES
5953 if ( m_gridLinesEnabled
)
5954 DrawCellBorder( dc
, coords
);
5957 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
5959 bool isCurrent
= coords
== m_currentCellCoords
;
5961 wxRect rect
= CellToRect( row
, col
);
5963 // if the editor is shown, we should use it and not the renderer
5964 // Note: However, only if it is really _shown_, i.e. not hidden!
5965 if ( isCurrent
&& IsCellEditControlShown() )
5967 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
5968 editor
->PaintBackground(rect
, attr
);
5973 // but all the rest is drawn by the cell renderer and hence may be
5975 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
5976 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
5983 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
5985 int row
= m_currentCellCoords
.GetRow();
5986 int col
= m_currentCellCoords
.GetCol();
5988 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
5991 wxRect rect
= CellToRect(row
, col
);
5993 // hmmm... what could we do here to show that the cell is disabled?
5994 // for now, I just draw a thinner border than for the other ones, but
5995 // it doesn't look really good
5997 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6001 // The center of th drawn line is where the position/width/height of
6002 // the rectangle is actually at, (on wxMSW atr least,) so we will
6003 // reduce the size of the rectangle to compensate for the thickness of
6004 // the line. If this is too strange on non wxMSW platforms then
6005 // please #ifdef this appropriately.
6006 rect
.x
+= penWidth
/2;
6007 rect
.y
+= penWidth
/2;
6008 rect
.width
-= penWidth
-1;
6009 rect
.height
-= penWidth
-1;
6012 // Now draw the rectangle
6013 dc
.SetPen(wxPen(m_cellHighlightColour
, penWidth
, wxSOLID
));
6014 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6015 dc
.DrawRectangle(rect
);
6019 // VZ: my experiments with 3d borders...
6021 // how to properly set colours for arbitrary bg?
6022 wxCoord x1
= rect
.x
,
6024 x2
= rect
.x
+ rect
.width
-1,
6025 y2
= rect
.y
+ rect
.height
-1;
6027 dc
.SetPen(*wxWHITE_PEN
);
6028 dc
.DrawLine(x1
, y1
, x2
, y1
);
6029 dc
.DrawLine(x1
, y1
, x1
, y2
);
6031 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6032 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6034 dc
.SetPen(*wxBLACK_PEN
);
6035 dc
.DrawLine(x1
, y2
, x2
, y2
);
6036 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6041 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6043 int row
= coords
.GetRow();
6044 int col
= coords
.GetCol();
6045 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6048 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6050 // right hand border
6052 dc
.DrawLine( GetColRight(col
), GetRowTop(row
),
6053 GetColRight(col
), GetRowBottom(row
) );
6057 dc
.DrawLine( GetColLeft(col
), GetRowBottom(row
),
6058 GetColRight(col
), GetRowBottom(row
) );
6061 void wxGrid::DrawHighlight(wxDC
& dc
)
6063 // This if block was previously in wxGrid::OnPaint but that doesn't
6064 // seem to get called under wxGTK - MB
6066 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6067 m_numRows
&& m_numCols
)
6069 m_currentCellCoords
.Set(0, 0);
6072 if ( IsCellEditControlShown() )
6074 // don't show highlight when the edit control is shown
6078 // if the active cell was repainted, repaint its highlight too because it
6079 // might have been damaged by the grid lines
6080 size_t count
= m_cellsExposed
.GetCount();
6081 for ( size_t n
= 0; n
< count
; n
++ )
6083 if ( m_cellsExposed
[n
] == m_currentCellCoords
)
6085 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6086 DrawCellHighlight(dc
, attr
);
6094 // TODO: remove this ???
6095 // This is used to redraw all grid lines e.g. when the grid line colour
6098 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6100 if ( !m_gridLinesEnabled
||
6102 !m_numCols
) return;
6104 int top
, bottom
, left
, right
;
6106 #if 0 //#ifndef __WXGTK__
6110 m_gridWin
->GetClientSize(&cw
, &ch
);
6112 // virtual coords of visible area
6114 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6115 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6120 reg
.GetBox(x
, y
, w
, h
);
6121 CalcUnscrolledPosition( x
, y
, &left
, &top
);
6122 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
6126 m_gridWin
->GetClientSize(&cw
, &ch
);
6127 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6128 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6131 // avoid drawing grid lines past the last row and col
6133 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
6134 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
6136 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6138 // horizontal grid lines
6141 for ( i
= 0; i
< m_numRows
; i
++ )
6143 int bot
= GetRowBottom(i
) - 1;
6152 dc
.DrawLine( left
, bot
, right
, bot
);
6157 // vertical grid lines
6159 for ( i
= 0; i
< m_numCols
; i
++ )
6161 int colRight
= GetColRight(i
) - 1;
6162 if ( colRight
> right
)
6167 if ( colRight
>= left
)
6169 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
6175 void wxGrid::DrawRowLabels( wxDC
& dc
)
6177 if ( !m_numRows
) return;
6180 size_t numLabels
= m_rowLabelsExposed
.GetCount();
6182 for ( i
= 0; i
< numLabels
; i
++ )
6184 DrawRowLabel( dc
, m_rowLabelsExposed
[i
] );
6189 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
6191 if ( GetRowHeight(row
) <= 0 )
6194 int rowTop
= GetRowTop(row
),
6195 rowBottom
= GetRowBottom(row
) - 1;
6197 dc
.SetPen( *wxBLACK_PEN
);
6198 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
6199 m_rowLabelWidth
-1, rowBottom
);
6201 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
-1, rowBottom
);
6203 dc
.SetPen( *wxWHITE_PEN
);
6204 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
6205 dc
.DrawLine( 0, rowTop
, m_rowLabelWidth
-1, rowTop
);
6207 dc
.SetBackgroundMode( wxTRANSPARENT
);
6208 dc
.SetTextForeground( GetLabelTextColour() );
6209 dc
.SetFont( GetLabelFont() );
6212 GetRowLabelAlignment( &hAlign
, &vAlign
);
6216 rect
.SetY( GetRowTop(row
) + 2 );
6217 rect
.SetWidth( m_rowLabelWidth
- 4 );
6218 rect
.SetHeight( GetRowHeight(row
) - 4 );
6219 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
6223 void wxGrid::DrawColLabels( wxDC
& dc
)
6225 if ( !m_numCols
) return;
6228 size_t numLabels
= m_colLabelsExposed
.GetCount();
6230 for ( i
= 0; i
< numLabels
; i
++ )
6232 DrawColLabel( dc
, m_colLabelsExposed
[i
] );
6237 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
6239 if ( GetColWidth(col
) <= 0 )
6242 int colLeft
= GetColLeft(col
),
6243 colRight
= GetColRight(col
) - 1;
6245 dc
.SetPen( *wxBLACK_PEN
);
6246 dc
.DrawLine( colRight
, 0,
6247 colRight
, m_colLabelHeight
-1 );
6249 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
6250 colRight
, m_colLabelHeight
-1 );
6252 dc
.SetPen( *wxWHITE_PEN
);
6253 dc
.DrawLine( colLeft
, 0, colLeft
, m_colLabelHeight
-1 );
6254 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
6256 dc
.SetBackgroundMode( wxTRANSPARENT
);
6257 dc
.SetTextForeground( GetLabelTextColour() );
6258 dc
.SetFont( GetLabelFont() );
6260 dc
.SetBackgroundMode( wxTRANSPARENT
);
6261 dc
.SetTextForeground( GetLabelTextColour() );
6262 dc
.SetFont( GetLabelFont() );
6265 GetColLabelAlignment( &hAlign
, &vAlign
);
6268 rect
.SetX( colLeft
+ 2 );
6270 rect
.SetWidth( GetColWidth(col
) - 4 );
6271 rect
.SetHeight( m_colLabelHeight
- 4 );
6272 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
);
6276 void wxGrid::DrawTextRectangle( wxDC
& dc
,
6277 const wxString
& value
,
6282 long textWidth
, textHeight
;
6283 long lineWidth
, lineHeight
;
6284 wxArrayString lines
;
6286 dc
.SetClippingRegion( rect
);
6287 StringToLines( value
, lines
);
6288 if ( lines
.GetCount() )
6290 GetTextBoxSize( dc
, lines
, &textWidth
, &textHeight
);
6291 dc
.GetTextExtent( lines
[0], &lineWidth
, &lineHeight
);
6294 switch ( horizAlign
)
6297 x
= rect
.x
+ (rect
.width
- textWidth
- 1);
6300 case wxALIGN_CENTRE
:
6301 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
6310 switch ( vertAlign
)
6312 case wxALIGN_BOTTOM
:
6313 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
6316 case wxALIGN_CENTRE
:
6317 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
6326 for ( size_t i
= 0; i
< lines
.GetCount(); i
++ )
6328 dc
.DrawText( lines
[i
], (int)x
, (int)y
);
6333 dc
.DestroyClippingRegion();
6337 // Split multi line text up into an array of strings. Any existing
6338 // contents of the string array are preserved.
6340 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
6344 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
6345 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
6347 while ( startPos
< (int)tVal
.Length() )
6349 pos
= tVal
.Mid(startPos
).Find( eol
);
6354 else if ( pos
== 0 )
6356 lines
.Add( wxEmptyString
);
6360 lines
.Add( value
.Mid(startPos
, pos
) );
6364 if ( startPos
< (int)value
.Length() )
6366 lines
.Add( value
.Mid( startPos
) );
6371 void wxGrid::GetTextBoxSize( wxDC
& dc
,
6372 wxArrayString
& lines
,
6373 long *width
, long *height
)
6380 for ( i
= 0; i
< lines
.GetCount(); i
++ )
6382 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
6383 w
= wxMax( w
, lineW
);
6392 // ------ Batch processing.
6394 void wxGrid::EndBatch()
6396 if ( m_batchCount
> 0 )
6399 if ( !m_batchCount
)
6402 m_rowLabelWin
->Refresh();
6403 m_colLabelWin
->Refresh();
6404 m_cornerLabelWin
->Refresh();
6405 m_gridWin
->Refresh();
6410 // Use this, rather than wxWindow::Refresh(), to force an immediate
6411 // repainting of the grid. Has no effect if you are already inside a
6412 // BeginBatch / EndBatch block.
6414 void wxGrid::ForceRefresh()
6422 // ------ Edit control functions
6426 void wxGrid::EnableEditing( bool edit
)
6428 // TODO: improve this ?
6430 if ( edit
!= m_editable
)
6432 if(!edit
) EnableCellEditControl(edit
);
6438 void wxGrid::EnableCellEditControl( bool enable
)
6443 if ( m_currentCellCoords
== wxGridNoCellCoords
)
6444 SetCurrentCell( 0, 0 );
6446 if ( enable
!= m_cellEditCtrlEnabled
)
6448 // TODO allow the app to Veto() this event?
6449 SendEvent(enable
? wxEVT_GRID_EDITOR_SHOWN
: wxEVT_GRID_EDITOR_HIDDEN
);
6453 // this should be checked by the caller!
6454 wxASSERT_MSG( CanEnableCellControl(),
6455 _T("can't enable editing for this cell!") );
6457 // do it before ShowCellEditControl()
6458 m_cellEditCtrlEnabled
= enable
;
6460 ShowCellEditControl();
6464 HideCellEditControl();
6465 SaveEditControlValue();
6467 // do it after HideCellEditControl()
6468 m_cellEditCtrlEnabled
= enable
;
6473 bool wxGrid::IsCurrentCellReadOnly() const
6476 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
6477 bool readonly
= attr
->IsReadOnly();
6483 bool wxGrid::CanEnableCellControl() const
6485 return m_editable
&& !IsCurrentCellReadOnly();
6488 bool wxGrid::IsCellEditControlEnabled() const
6490 // the cell edit control might be disable for all cells or just for the
6491 // current one if it's read only
6492 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
6495 bool wxGrid::IsCellEditControlShown() const
6497 bool isShown
= FALSE
;
6499 if ( m_cellEditCtrlEnabled
)
6501 int row
= m_currentCellCoords
.GetRow();
6502 int col
= m_currentCellCoords
.GetCol();
6503 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6504 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
6509 if ( editor
->IsCreated() )
6511 isShown
= editor
->GetControl()->IsShown();
6521 void wxGrid::ShowCellEditControl()
6523 if ( IsCellEditControlEnabled() )
6525 if ( !IsVisible( m_currentCellCoords
) )
6527 m_cellEditCtrlEnabled
= FALSE
;
6532 wxRect rect
= CellToRect( m_currentCellCoords
);
6533 int row
= m_currentCellCoords
.GetRow();
6534 int col
= m_currentCellCoords
.GetCol();
6536 // convert to scrolled coords
6538 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
6540 // done in PaintBackground()
6542 // erase the highlight and the cell contents because the editor
6543 // might not cover the entire cell
6544 wxClientDC
dc( m_gridWin
);
6546 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
6547 dc
.SetPen(*wxTRANSPARENT_PEN
);
6548 dc
.DrawRectangle(rect
);
6551 // cell is shifted by one pixel
6555 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6556 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6557 if ( !editor
->IsCreated() )
6559 editor
->Create(m_gridWin
, -1,
6560 new wxGridCellEditorEvtHandler(this, editor
));
6563 editor
->Show( TRUE
, attr
);
6565 editor
->SetSize( rect
);
6567 editor
->BeginEdit(row
, col
, this);
6576 void wxGrid::HideCellEditControl()
6578 if ( IsCellEditControlEnabled() )
6580 int row
= m_currentCellCoords
.GetRow();
6581 int col
= m_currentCellCoords
.GetCol();
6583 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6584 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6585 editor
->Show( FALSE
);
6588 m_gridWin
->SetFocus();
6589 wxRect
rect( CellToRect( row
, col
) );
6590 m_gridWin
->Refresh( FALSE
, &rect
);
6595 void wxGrid::SaveEditControlValue()
6597 if ( IsCellEditControlEnabled() )
6599 int row
= m_currentCellCoords
.GetRow();
6600 int col
= m_currentCellCoords
.GetCol();
6602 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6603 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
6604 bool changed
= editor
->EndEdit(row
, col
, this);
6611 SendEvent( wxEVT_GRID_CELL_CHANGE
,
6612 m_currentCellCoords
.GetRow(),
6613 m_currentCellCoords
.GetCol() );
6620 // ------ Grid location functions
6621 // Note that all of these functions work with the logical coordinates of
6622 // grid cells and labels so you will need to convert from device
6623 // coordinates for mouse events etc.
6626 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
6628 int row
= YToRow(y
);
6629 int col
= XToCol(x
);
6631 if ( row
== -1 || col
== -1 )
6633 coords
= wxGridNoCellCoords
;
6637 coords
.Set( row
, col
);
6642 int wxGrid::YToRow( int y
)
6646 for ( i
= 0; i
< m_numRows
; i
++ )
6648 if ( y
< GetRowBottom(i
) )
6656 int wxGrid::XToCol( int x
)
6660 for ( i
= 0; i
< m_numCols
; i
++ )
6662 if ( x
< GetColRight(i
) )
6670 // return the row number that that the y coord is near the edge of, or
6671 // -1 if not near an edge
6673 int wxGrid::YToEdgeOfRow( int y
)
6677 for ( i
= 0; i
< m_numRows
; i
++ )
6679 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
6681 d
= abs( y
- GetRowBottom(i
) );
6682 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6691 // return the col number that that the x coord is near the edge of, or
6692 // -1 if not near an edge
6694 int wxGrid::XToEdgeOfCol( int x
)
6698 for ( i
= 0; i
< m_numCols
; i
++ )
6700 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
6702 d
= abs( x
- GetColRight(i
) );
6703 if ( d
< WXGRID_LABEL_EDGE_ZONE
)
6712 wxRect
wxGrid::CellToRect( int row
, int col
)
6714 wxRect
rect( -1, -1, -1, -1 );
6716 if ( row
>= 0 && row
< m_numRows
&&
6717 col
>= 0 && col
< m_numCols
)
6719 rect
.x
= GetColLeft(col
);
6720 rect
.y
= GetRowTop(row
);
6721 rect
.width
= GetColWidth(col
);
6722 rect
.height
= GetRowHeight(row
);
6725 // if grid lines are enabled, then the area of the cell is a bit smaller
6726 if (m_gridLinesEnabled
) {
6734 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
6736 // get the cell rectangle in logical coords
6738 wxRect
r( CellToRect( row
, col
) );
6740 // convert to device coords
6742 int left
, top
, right
, bottom
;
6743 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6744 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6746 // check against the client area of the grid window
6749 m_gridWin
->GetClientSize( &cw
, &ch
);
6751 if ( wholeCellVisible
)
6753 // is the cell wholly visible ?
6755 return ( left
>= 0 && right
<= cw
&&
6756 top
>= 0 && bottom
<= ch
);
6760 // is the cell partly visible ?
6762 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
6763 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
6768 // make the specified cell location visible by doing a minimal amount
6771 void wxGrid::MakeCellVisible( int row
, int col
)
6774 int xpos
= -1, ypos
= -1;
6776 if ( row
>= 0 && row
< m_numRows
&&
6777 col
>= 0 && col
< m_numCols
)
6779 // get the cell rectangle in logical coords
6781 wxRect
r( CellToRect( row
, col
) );
6783 // convert to device coords
6785 int left
, top
, right
, bottom
;
6786 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
6787 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
6790 m_gridWin
->GetClientSize( &cw
, &ch
);
6796 else if ( bottom
> ch
)
6798 int h
= r
.GetHeight();
6800 for ( i
= row
-1; i
>= 0; i
-- )
6802 int rowHeight
= GetRowHeight(i
);
6803 if ( h
+ rowHeight
> ch
)
6810 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
6811 // have rounding errors (this is important, because if we do, we
6812 // might not scroll at all and some cells won't be redrawn)
6813 ypos
+= GRID_SCROLL_LINE
/ 2;
6820 else if ( right
> cw
)
6822 int w
= r
.GetWidth();
6824 for ( i
= col
-1; i
>= 0; i
-- )
6826 int colWidth
= GetColWidth(i
);
6827 if ( w
+ colWidth
> cw
)
6834 // see comment for ypos above
6835 xpos
+= GRID_SCROLL_LINE
/ 2;
6838 if ( xpos
!= -1 || ypos
!= -1 )
6840 if ( xpos
!= -1 ) xpos
/= GRID_SCROLL_LINE
;
6841 if ( ypos
!= -1 ) ypos
/= GRID_SCROLL_LINE
;
6842 Scroll( xpos
, ypos
);
6850 // ------ Grid cursor movement functions
6853 bool wxGrid::MoveCursorUp( bool expandSelection
)
6855 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6856 m_currentCellCoords
.GetRow() >= 0 )
6858 if ( expandSelection
)
6860 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6861 m_selectingKeyboard
= m_currentCellCoords
;
6862 if ( m_selectingKeyboard
.GetRow() > 0 )
6864 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
6865 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6866 m_selectingKeyboard
.GetCol() );
6867 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6870 else if ( m_currentCellCoords
.GetRow() > 0 )
6873 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
6874 m_currentCellCoords
.GetCol() );
6875 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
6876 m_currentCellCoords
.GetCol() );
6887 bool wxGrid::MoveCursorDown( bool expandSelection
)
6889 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6890 m_currentCellCoords
.GetRow() < m_numRows
)
6892 if ( expandSelection
)
6894 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6895 m_selectingKeyboard
= m_currentCellCoords
;
6896 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
6898 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
6899 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6900 m_selectingKeyboard
.GetCol() );
6901 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6904 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
6907 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
6908 m_currentCellCoords
.GetCol() );
6909 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
6910 m_currentCellCoords
.GetCol() );
6921 bool wxGrid::MoveCursorLeft( bool expandSelection
)
6923 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6924 m_currentCellCoords
.GetCol() >= 0 )
6926 if ( expandSelection
)
6928 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6929 m_selectingKeyboard
= m_currentCellCoords
;
6930 if ( m_selectingKeyboard
.GetCol() > 0 )
6932 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
6933 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6934 m_selectingKeyboard
.GetCol() );
6935 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6938 else if ( m_currentCellCoords
.GetCol() > 0 )
6941 MakeCellVisible( m_currentCellCoords
.GetRow(),
6942 m_currentCellCoords
.GetCol() - 1 );
6943 SetCurrentCell( m_currentCellCoords
.GetRow(),
6944 m_currentCellCoords
.GetCol() - 1 );
6955 bool wxGrid::MoveCursorRight( bool expandSelection
)
6957 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
6958 m_currentCellCoords
.GetCol() < m_numCols
)
6960 if ( expandSelection
)
6962 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
6963 m_selectingKeyboard
= m_currentCellCoords
;
6964 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
6966 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
6967 MakeCellVisible( m_selectingKeyboard
.GetRow(),
6968 m_selectingKeyboard
.GetCol() );
6969 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
6972 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
6975 MakeCellVisible( m_currentCellCoords
.GetRow(),
6976 m_currentCellCoords
.GetCol() + 1 );
6977 SetCurrentCell( m_currentCellCoords
.GetRow(),
6978 m_currentCellCoords
.GetCol() + 1 );
6989 bool wxGrid::MovePageUp()
6991 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
6993 int row
= m_currentCellCoords
.GetRow();
6997 m_gridWin
->GetClientSize( &cw
, &ch
);
6999 int y
= GetRowTop(row
);
7000 int newRow
= YToRow( y
- ch
+ 1 );
7005 else if ( newRow
== row
)
7010 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7011 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7019 bool wxGrid::MovePageDown()
7021 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
7023 int row
= m_currentCellCoords
.GetRow();
7024 if ( row
< m_numRows
)
7027 m_gridWin
->GetClientSize( &cw
, &ch
);
7029 int y
= GetRowTop(row
);
7030 int newRow
= YToRow( y
+ ch
);
7033 newRow
= m_numRows
- 1;
7035 else if ( newRow
== row
)
7040 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
7041 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
7049 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
7052 m_currentCellCoords
!= wxGridNoCellCoords
&&
7053 m_currentCellCoords
.GetRow() > 0 )
7055 int row
= m_currentCellCoords
.GetRow();
7056 int col
= m_currentCellCoords
.GetCol();
7058 if ( m_table
->IsEmptyCell(row
, col
) )
7060 // starting in an empty cell: find the next block of
7066 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7069 else if ( m_table
->IsEmptyCell(row
-1, col
) )
7071 // starting at the top of a block: find the next block
7077 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7082 // starting within a block: find the top of the block
7087 if ( m_table
->IsEmptyCell(row
, col
) )
7095 MakeCellVisible( row
, col
);
7096 if ( expandSelection
)
7098 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7099 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7104 SetCurrentCell( row
, col
);
7112 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
7115 m_currentCellCoords
!= wxGridNoCellCoords
&&
7116 m_currentCellCoords
.GetRow() < m_numRows
-1 )
7118 int row
= m_currentCellCoords
.GetRow();
7119 int col
= m_currentCellCoords
.GetCol();
7121 if ( m_table
->IsEmptyCell(row
, col
) )
7123 // starting in an empty cell: find the next block of
7126 while ( row
< m_numRows
-1 )
7129 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7132 else if ( m_table
->IsEmptyCell(row
+1, col
) )
7134 // starting at the bottom of a block: find the next block
7137 while ( row
< m_numRows
-1 )
7140 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7145 // starting within a block: find the bottom of the block
7147 while ( row
< m_numRows
-1 )
7150 if ( m_table
->IsEmptyCell(row
, col
) )
7158 MakeCellVisible( row
, col
);
7159 if ( expandSelection
)
7161 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7162 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7167 SetCurrentCell( row
, col
);
7176 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
7179 m_currentCellCoords
!= wxGridNoCellCoords
&&
7180 m_currentCellCoords
.GetCol() > 0 )
7182 int row
= m_currentCellCoords
.GetRow();
7183 int col
= m_currentCellCoords
.GetCol();
7185 if ( m_table
->IsEmptyCell(row
, col
) )
7187 // starting in an empty cell: find the next block of
7193 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7196 else if ( m_table
->IsEmptyCell(row
, col
-1) )
7198 // starting at the left of a block: find the next block
7204 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7209 // starting within a block: find the left of the block
7214 if ( m_table
->IsEmptyCell(row
, col
) )
7222 MakeCellVisible( row
, col
);
7223 if ( expandSelection
)
7225 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7226 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7231 SetCurrentCell( row
, col
);
7240 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
7243 m_currentCellCoords
!= wxGridNoCellCoords
&&
7244 m_currentCellCoords
.GetCol() < m_numCols
-1 )
7246 int row
= m_currentCellCoords
.GetRow();
7247 int col
= m_currentCellCoords
.GetCol();
7249 if ( m_table
->IsEmptyCell(row
, col
) )
7251 // starting in an empty cell: find the next block of
7254 while ( col
< m_numCols
-1 )
7257 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7260 else if ( m_table
->IsEmptyCell(row
, col
+1) )
7262 // starting at the right of a block: find the next block
7265 while ( col
< m_numCols
-1 )
7268 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
7273 // starting within a block: find the right of the block
7275 while ( col
< m_numCols
-1 )
7278 if ( m_table
->IsEmptyCell(row
, col
) )
7286 MakeCellVisible( row
, col
);
7287 if ( expandSelection
)
7289 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
7290 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7295 SetCurrentCell( row
, col
);
7307 // ------ Label values and formatting
7310 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
7312 *horiz
= m_rowLabelHorizAlign
;
7313 *vert
= m_rowLabelVertAlign
;
7316 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
7318 *horiz
= m_colLabelHorizAlign
;
7319 *vert
= m_colLabelVertAlign
;
7322 wxString
wxGrid::GetRowLabelValue( int row
)
7326 return m_table
->GetRowLabelValue( row
);
7336 wxString
wxGrid::GetColLabelValue( int col
)
7340 return m_table
->GetColLabelValue( col
);
7351 void wxGrid::SetRowLabelSize( int width
)
7353 width
= wxMax( width
, 0 );
7354 if ( width
!= m_rowLabelWidth
)
7358 m_rowLabelWin
->Show( FALSE
);
7359 m_cornerLabelWin
->Show( FALSE
);
7361 else if ( m_rowLabelWidth
== 0 )
7363 m_rowLabelWin
->Show( TRUE
);
7364 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7367 m_rowLabelWidth
= width
;
7374 void wxGrid::SetColLabelSize( int height
)
7376 height
= wxMax( height
, 0 );
7377 if ( height
!= m_colLabelHeight
)
7381 m_colLabelWin
->Show( FALSE
);
7382 m_cornerLabelWin
->Show( FALSE
);
7384 else if ( m_colLabelHeight
== 0 )
7386 m_colLabelWin
->Show( TRUE
);
7387 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
7390 m_colLabelHeight
= height
;
7397 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
7399 if ( m_labelBackgroundColour
!= colour
)
7401 m_labelBackgroundColour
= colour
;
7402 m_rowLabelWin
->SetBackgroundColour( colour
);
7403 m_colLabelWin
->SetBackgroundColour( colour
);
7404 m_cornerLabelWin
->SetBackgroundColour( colour
);
7406 if ( !GetBatchCount() )
7408 m_rowLabelWin
->Refresh();
7409 m_colLabelWin
->Refresh();
7410 m_cornerLabelWin
->Refresh();
7415 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
7417 if ( m_labelTextColour
!= colour
)
7419 m_labelTextColour
= colour
;
7420 if ( !GetBatchCount() )
7422 m_rowLabelWin
->Refresh();
7423 m_colLabelWin
->Refresh();
7428 void wxGrid::SetLabelFont( const wxFont
& font
)
7431 if ( !GetBatchCount() )
7433 m_rowLabelWin
->Refresh();
7434 m_colLabelWin
->Refresh();
7438 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
7440 // allow old (incorrect) defs to be used
7443 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7444 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7445 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7450 case wxTOP
: vert
= wxALIGN_TOP
; break;
7451 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7452 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7455 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7457 m_rowLabelHorizAlign
= horiz
;
7460 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7462 m_rowLabelVertAlign
= vert
;
7465 if ( !GetBatchCount() )
7467 m_rowLabelWin
->Refresh();
7471 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
7473 // allow old (incorrect) defs to be used
7476 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
7477 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
7478 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
7483 case wxTOP
: vert
= wxALIGN_TOP
; break;
7484 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
7485 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
7488 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
7490 m_colLabelHorizAlign
= horiz
;
7493 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
7495 m_colLabelVertAlign
= vert
;
7498 if ( !GetBatchCount() )
7500 m_colLabelWin
->Refresh();
7504 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
7508 m_table
->SetRowLabelValue( row
, s
);
7509 if ( !GetBatchCount() )
7511 wxRect rect
= CellToRect( row
, 0);
7512 if ( rect
.height
> 0 )
7514 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
7516 rect
.width
= m_rowLabelWidth
;
7517 m_rowLabelWin
->Refresh( TRUE
, &rect
);
7523 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
7527 m_table
->SetColLabelValue( col
, s
);
7528 if ( !GetBatchCount() )
7530 wxRect rect
= CellToRect( 0, col
);
7531 if ( rect
.width
> 0 )
7533 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
7535 rect
.height
= m_colLabelHeight
;
7536 m_colLabelWin
->Refresh( TRUE
, &rect
);
7542 void wxGrid::SetGridLineColour( const wxColour
& colour
)
7544 if ( m_gridLineColour
!= colour
)
7546 m_gridLineColour
= colour
;
7548 wxClientDC
dc( m_gridWin
);
7550 DrawAllGridLines( dc
, wxRegion() );
7555 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
7557 if ( m_cellHighlightColour
!= colour
)
7559 m_cellHighlightColour
= colour
;
7561 wxClientDC
dc( m_gridWin
);
7563 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7564 DrawCellHighlight(dc
, attr
);
7569 void wxGrid::SetCellHighlightPenWidth(int width
)
7571 if (m_cellHighlightPenWidth
!= width
) {
7572 m_cellHighlightPenWidth
= width
;
7574 // Just redrawing the cell highlight is not enough since that won't
7575 // make any visible change if the the thickness is getting smaller.
7576 int row
= m_currentCellCoords
.GetRow();
7577 int col
= m_currentCellCoords
.GetCol();
7578 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7580 wxRect rect
= CellToRect(row
, col
);
7581 m_gridWin
->Refresh(TRUE
, &rect
);
7585 void wxGrid::SetCellHighlightROPenWidth(int width
)
7587 if (m_cellHighlightROPenWidth
!= width
) {
7588 m_cellHighlightROPenWidth
= width
;
7590 // Just redrawing the cell highlight is not enough since that won't
7591 // make any visible change if the the thickness is getting smaller.
7592 int row
= m_currentCellCoords
.GetRow();
7593 int col
= m_currentCellCoords
.GetCol();
7594 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7596 wxRect rect
= CellToRect(row
, col
);
7597 m_gridWin
->Refresh(TRUE
, &rect
);
7601 void wxGrid::EnableGridLines( bool enable
)
7603 if ( enable
!= m_gridLinesEnabled
)
7605 m_gridLinesEnabled
= enable
;
7607 if ( !GetBatchCount() )
7611 wxClientDC
dc( m_gridWin
);
7613 DrawAllGridLines( dc
, wxRegion() );
7617 m_gridWin
->Refresh();
7624 int wxGrid::GetDefaultRowSize()
7626 return m_defaultRowHeight
;
7629 int wxGrid::GetRowSize( int row
)
7631 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
7633 return GetRowHeight(row
);
7636 int wxGrid::GetDefaultColSize()
7638 return m_defaultColWidth
;
7641 int wxGrid::GetColSize( int col
)
7643 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
7645 return GetColWidth(col
);
7648 // ============================================================================
7649 // access to the grid attributes: each of them has a default value in the grid
7650 // itself and may be overidden on a per-cell basis
7651 // ============================================================================
7653 // ----------------------------------------------------------------------------
7654 // setting default attributes
7655 // ----------------------------------------------------------------------------
7657 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
7659 m_defaultCellAttr
->SetBackgroundColour(col
);
7661 m_gridWin
->SetBackgroundColour(col
);
7665 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
7667 m_defaultCellAttr
->SetTextColour(col
);
7670 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
7672 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
7675 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
7677 m_defaultCellAttr
->SetFont(font
);
7680 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
7682 m_defaultCellAttr
->SetRenderer(renderer
);
7685 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
7687 m_defaultCellAttr
->SetEditor(editor
);
7690 // ----------------------------------------------------------------------------
7691 // access to the default attrbiutes
7692 // ----------------------------------------------------------------------------
7694 wxColour
wxGrid::GetDefaultCellBackgroundColour()
7696 return m_defaultCellAttr
->GetBackgroundColour();
7699 wxColour
wxGrid::GetDefaultCellTextColour()
7701 return m_defaultCellAttr
->GetTextColour();
7704 wxFont
wxGrid::GetDefaultCellFont()
7706 return m_defaultCellAttr
->GetFont();
7709 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
7711 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
7714 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
7716 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
7719 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
7721 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
7724 // ----------------------------------------------------------------------------
7725 // access to cell attributes
7726 // ----------------------------------------------------------------------------
7728 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
7730 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7731 wxColour colour
= attr
->GetBackgroundColour();
7736 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
7738 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7739 wxColour colour
= attr
->GetTextColour();
7744 wxFont
wxGrid::GetCellFont( int row
, int col
)
7746 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7747 wxFont font
= attr
->GetFont();
7752 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
7754 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
7755 attr
->GetAlignment(horiz
, vert
);
7759 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
7761 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7762 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
7768 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
7770 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7771 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7777 bool wxGrid::IsReadOnly(int row
, int col
) const
7779 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7780 bool isReadOnly
= attr
->IsReadOnly();
7785 // ----------------------------------------------------------------------------
7786 // attribute support: cache, automatic provider creation, ...
7787 // ----------------------------------------------------------------------------
7789 bool wxGrid::CanHaveAttributes()
7796 return m_table
->CanHaveAttributes();
7799 void wxGrid::ClearAttrCache()
7801 if ( m_attrCache
.row
!= -1 )
7803 wxSafeDecRef(m_attrCache
.attr
);
7804 m_attrCache
.row
= -1;
7808 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
7810 wxGrid
*self
= (wxGrid
*)this; // const_cast
7812 self
->ClearAttrCache();
7813 self
->m_attrCache
.row
= row
;
7814 self
->m_attrCache
.col
= col
;
7815 self
->m_attrCache
.attr
= attr
;
7819 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
7821 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
7823 *attr
= m_attrCache
.attr
;
7824 wxSafeIncRef(m_attrCache
.attr
);
7826 #ifdef DEBUG_ATTR_CACHE
7827 gs_nAttrCacheHits
++;
7834 #ifdef DEBUG_ATTR_CACHE
7835 gs_nAttrCacheMisses
++;
7841 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
7843 wxGridCellAttr
*attr
;
7844 if ( !LookupAttr(row
, col
, &attr
) )
7846 attr
= m_table
? m_table
->GetAttr(row
, col
) : (wxGridCellAttr
*)NULL
;
7847 CacheAttr(row
, col
, attr
);
7851 attr
->SetDefAttr(m_defaultCellAttr
);
7855 attr
= m_defaultCellAttr
;
7862 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
7864 wxGridCellAttr
*attr
;
7865 if ( !LookupAttr(row
, col
, &attr
) || !attr
)
7867 wxASSERT_MSG( m_table
,
7868 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
7870 attr
= m_table
->GetAttr(row
, col
);
7873 attr
= new wxGridCellAttr
;
7875 // artificially inc the ref count to match DecRef() in caller
7878 m_table
->SetAttr(attr
, row
, col
);
7881 CacheAttr(row
, col
, attr
);
7883 attr
->SetDefAttr(m_defaultCellAttr
);
7887 // ----------------------------------------------------------------------------
7888 // setting column attributes (wrappers around SetColAttr)
7889 // ----------------------------------------------------------------------------
7891 void wxGrid::SetColFormatBool(int col
)
7893 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
7896 void wxGrid::SetColFormatNumber(int col
)
7898 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
7901 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
7903 wxString typeName
= wxGRID_VALUE_FLOAT
;
7904 if ( (width
!= -1) || (precision
!= -1) )
7906 typeName
<< _T(':') << width
<< _T(',') << precision
;
7909 SetColFormatCustom(col
, typeName
);
7912 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
7914 wxGridCellAttr
*attr
= new wxGridCellAttr
;
7915 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
7916 attr
->SetRenderer(renderer
);
7918 SetColAttr(col
, attr
);
7921 // ----------------------------------------------------------------------------
7922 // setting cell attributes: this is forwarded to the table
7923 // ----------------------------------------------------------------------------
7925 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
7927 if ( CanHaveAttributes() )
7929 m_table
->SetRowAttr(attr
, row
);
7937 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
7939 if ( CanHaveAttributes() )
7941 m_table
->SetColAttr(attr
, col
);
7949 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
7951 if ( CanHaveAttributes() )
7953 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7954 attr
->SetBackgroundColour(colour
);
7959 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
7961 if ( CanHaveAttributes() )
7963 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7964 attr
->SetTextColour(colour
);
7969 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
7971 if ( CanHaveAttributes() )
7973 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7974 attr
->SetFont(font
);
7979 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
7981 if ( CanHaveAttributes() )
7983 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7984 attr
->SetAlignment(horiz
, vert
);
7989 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
7991 if ( CanHaveAttributes() )
7993 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
7994 attr
->SetRenderer(renderer
);
7999 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
8001 if ( CanHaveAttributes() )
8003 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8004 attr
->SetEditor(editor
);
8009 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
8011 if ( CanHaveAttributes() )
8013 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
8014 attr
->SetReadOnly(isReadOnly
);
8019 // ----------------------------------------------------------------------------
8020 // Data type registration
8021 // ----------------------------------------------------------------------------
8023 void wxGrid::RegisterDataType(const wxString
& typeName
,
8024 wxGridCellRenderer
* renderer
,
8025 wxGridCellEditor
* editor
)
8027 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
8031 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
8033 wxString typeName
= m_table
->GetTypeName(row
, col
);
8034 return GetDefaultEditorForType(typeName
);
8037 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
8039 wxString typeName
= m_table
->GetTypeName(row
, col
);
8040 return GetDefaultRendererForType(typeName
);
8044 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
8046 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8047 if ( index
== wxNOT_FOUND
)
8049 wxFAIL_MSG(wxT("Unknown data type name"));
8054 return m_typeRegistry
->GetEditor(index
);
8058 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
8060 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
8061 if ( index
== wxNOT_FOUND
)
8063 wxFAIL_MSG(wxT("Unknown data type name"));
8068 return m_typeRegistry
->GetRenderer(index
);
8072 // ----------------------------------------------------------------------------
8074 // ----------------------------------------------------------------------------
8076 void wxGrid::EnableDragRowSize( bool enable
)
8078 m_canDragRowSize
= enable
;
8082 void wxGrid::EnableDragColSize( bool enable
)
8084 m_canDragColSize
= enable
;
8087 void wxGrid::EnableDragGridSize( bool enable
)
8089 m_canDragGridSize
= enable
;
8093 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
8095 m_defaultRowHeight
= wxMax( height
, WXGRID_MIN_ROW_HEIGHT
);
8097 if ( resizeExistingRows
)
8100 if ( !GetBatchCount() )
8105 void wxGrid::SetRowSize( int row
, int height
)
8107 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
8109 if ( m_rowHeights
.IsEmpty() )
8111 // need to really create the array
8115 int h
= wxMax( 0, height
);
8116 int diff
= h
- m_rowHeights
[row
];
8118 m_rowHeights
[row
] = h
;
8120 for ( i
= row
; i
< m_numRows
; i
++ )
8122 m_rowBottoms
[i
] += diff
;
8124 if ( !GetBatchCount() )
8128 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
8130 m_defaultColWidth
= wxMax( width
, WXGRID_MIN_COL_WIDTH
);
8132 if ( resizeExistingCols
)
8135 if ( !GetBatchCount() )
8140 void wxGrid::SetColSize( int col
, int width
)
8142 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
8144 // should we check that it's bigger than GetColMinimalWidth(col) here?
8146 if ( m_colWidths
.IsEmpty() )
8148 // need to really create the array
8152 int w
= wxMax( 0, width
);
8153 int diff
= w
- m_colWidths
[col
];
8154 m_colWidths
[col
] = w
;
8157 for ( i
= col
; i
< m_numCols
; i
++ )
8159 m_colRights
[i
] += diff
;
8161 if ( !GetBatchCount() )
8166 void wxGrid::SetColMinimalWidth( int col
, int width
)
8168 m_colMinWidths
.Put(col
, width
);
8171 void wxGrid::SetRowMinimalHeight( int row
, int width
)
8173 m_rowMinHeights
.Put(row
, width
);
8176 int wxGrid::GetColMinimalWidth(int col
) const
8178 long value
= m_colMinWidths
.Get(col
);
8179 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_COL_WIDTH
;
8182 int wxGrid::GetRowMinimalHeight(int row
) const
8184 long value
= m_rowMinHeights
.Get(row
);
8185 return value
!= wxNOT_FOUND
? (int)value
: WXGRID_MIN_ROW_HEIGHT
;
8188 // ----------------------------------------------------------------------------
8190 // ----------------------------------------------------------------------------
8192 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
8194 wxClientDC
dc(m_gridWin
);
8196 // init both of them to avoid compiler warnings, even if weo nly need one
8204 wxCoord extent
, extentMax
= 0;
8205 int max
= column
? m_numRows
: m_numCols
;
8206 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
8213 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8214 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8217 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
8218 extent
= column
? size
.x
: size
.y
;
8219 if ( extent
> extentMax
)
8230 // now also compare with the column label extent
8232 dc
.SetFont( GetLabelFont() );
8235 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
8237 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
8239 extent
= column
? w
: h
;
8240 if ( extent
> extentMax
)
8247 // empty column - give default extent (notice that if extentMax is less
8248 // than default extent but != 0, it's ok)
8249 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
8255 // leave some space around text
8265 SetColSize(col
, extentMax
);
8266 if ( !GetBatchCount() )
8269 m_gridWin
->GetClientSize( &cw
, &ch
);
8270 wxRect
rect ( CellToRect( 0, col
) );
8272 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
8273 rect
.width
= cw
- rect
.x
;
8274 rect
.height
= m_colLabelHeight
;
8275 m_colLabelWin
->Refresh( TRUE
, &rect
);
8279 SetRowSize(row
, extentMax
);
8280 if ( !GetBatchCount() )
8283 m_gridWin
->GetClientSize( &cw
, &ch
);
8284 wxRect
rect ( CellToRect( row
, 0 ) );
8286 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
8287 rect
.width
= m_rowLabelWidth
;
8288 rect
.height
= ch
- rect
.y
;
8289 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8295 SetColMinimalWidth(col
, extentMax
);
8297 SetRowMinimalHeight(row
, extentMax
);
8301 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
8303 int width
= m_rowLabelWidth
;
8307 for ( int col
= 0; col
< m_numCols
; col
++ )
8311 AutoSizeColumn(col
, setAsMin
);
8314 width
+= GetColWidth(col
);
8321 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
8323 int height
= m_colLabelHeight
;
8327 for ( int row
= 0; row
< m_numRows
; row
++ )
8331 AutoSizeRow(row
, setAsMin
);
8334 height
+= GetRowHeight(row
);
8341 void wxGrid::AutoSize()
8344 SetClientSize(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
8347 wxSize
wxGrid::DoGetBestSize() const
8349 // don't set sizes, only calculate them
8350 wxGrid
*self
= (wxGrid
*)this; // const_cast
8352 return wxSize(self
->SetOrCalcColumnSizes(TRUE
),
8353 self
->SetOrCalcRowSizes(TRUE
));
8362 wxPen
& wxGrid::GetDividerPen() const
8367 // ----------------------------------------------------------------------------
8368 // cell value accessor functions
8369 // ----------------------------------------------------------------------------
8371 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
8375 m_table
->SetValue( row
, col
, s
);
8376 if ( !GetBatchCount() )
8378 wxClientDC
dc( m_gridWin
);
8380 DrawCell( dc
, wxGridCellCoords(row
, col
) );
8383 if ( m_currentCellCoords
.GetRow() == row
&&
8384 m_currentCellCoords
.GetCol() == col
&&
8385 IsCellEditControlShown())
8386 // Note: If we are using IsCellEditControlEnabled,
8387 // this interacts badly with calling SetCellValue from
8388 // an EVT_GRID_CELL_CHANGE handler.
8390 HideCellEditControl();
8391 ShowCellEditControl(); // will reread data from table
8398 // ------ Block, row and col selection
8401 void wxGrid::SelectRow( int row
, bool addToSelected
)
8403 if ( IsSelection() && !addToSelected
)
8406 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
8410 void wxGrid::SelectCol( int col
, bool addToSelected
)
8412 if ( IsSelection() && !addToSelected
)
8415 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
8419 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
8420 bool addToSelected
)
8422 if ( IsSelection() && !addToSelected
)
8425 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
8426 FALSE
, addToSelected
);
8430 void wxGrid::SelectAll()
8432 if ( m_numRows
> 0 && m_numCols
> 0 )
8433 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
8437 // ------ Cell, row and col deselection
8440 void wxGrid::DeselectRow( int row
)
8442 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
8444 if ( m_selection
->IsInSelection(row
, 0 ) )
8445 m_selection
->ToggleCellSelection( row
, 0);
8449 int nCols
= GetNumberCols();
8450 for ( int i
= 0; i
< nCols
; i
++ )
8452 if ( m_selection
->IsInSelection(row
, i
) )
8453 m_selection
->ToggleCellSelection( row
, i
);
8458 void wxGrid::DeselectCol( int col
)
8460 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
8462 if ( m_selection
->IsInSelection(0, col
) )
8463 m_selection
->ToggleCellSelection( 0, col
);
8467 int nRows
= GetNumberRows();
8468 for ( int i
= 0; i
< nRows
; i
++ )
8470 if ( m_selection
->IsInSelection(i
, col
) )
8471 m_selection
->ToggleCellSelection(i
, col
);
8476 void wxGrid::DeselectCell( int row
, int col
)
8478 if ( m_selection
->IsInSelection(row
, col
) )
8479 m_selection
->ToggleCellSelection(row
, col
);
8482 bool wxGrid::IsSelection()
8484 return ( m_selection
->IsSelection() ||
8485 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
8486 m_selectingBottomRight
!= wxGridNoCellCoords
) );
8489 bool wxGrid::IsInSelection( int row
, int col
)
8491 return ( m_selection
->IsInSelection( row
, col
) ||
8492 ( row
>= m_selectingTopLeft
.GetRow() &&
8493 col
>= m_selectingTopLeft
.GetCol() &&
8494 row
<= m_selectingBottomRight
.GetRow() &&
8495 col
<= m_selectingBottomRight
.GetCol() ) );
8498 void wxGrid::ClearSelection()
8500 m_selectingTopLeft
= wxGridNoCellCoords
;
8501 m_selectingBottomRight
= wxGridNoCellCoords
;
8502 m_selection
->ClearSelection();
8506 // This function returns the rectangle that encloses the given block
8507 // in device coords clipped to the client size of the grid window.
8509 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
8510 const wxGridCellCoords
&bottomRight
)
8512 wxRect
rect( wxGridNoCellRect
);
8515 cellRect
= CellToRect( topLeft
);
8516 if ( cellRect
!= wxGridNoCellRect
)
8522 rect
= wxRect( 0, 0, 0, 0 );
8525 cellRect
= CellToRect( bottomRight
);
8526 if ( cellRect
!= wxGridNoCellRect
)
8532 return wxGridNoCellRect
;
8535 // convert to scrolled coords
8537 int left
, top
, right
, bottom
;
8538 CalcScrolledPosition( rect
.GetLeft(), rect
.GetTop(), &left
, &top
);
8539 CalcScrolledPosition( rect
.GetRight(), rect
.GetBottom(), &right
, &bottom
);
8542 m_gridWin
->GetClientSize( &cw
, &ch
);
8544 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
8545 return wxRect( 0, 0, 0, 0);
8547 rect
.SetLeft( wxMax(0, left
) );
8548 rect
.SetTop( wxMax(0, top
) );
8549 rect
.SetRight( wxMin(cw
, right
) );
8550 rect
.SetBottom( wxMin(ch
, bottom
) );
8558 // ------ Grid event classes
8561 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxEvent
)
8563 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
8564 int row
, int col
, int x
, int y
, bool sel
,
8565 bool control
, bool shift
, bool alt
, bool meta
)
8566 : wxNotifyEvent( type
, id
)
8573 m_control
= control
;
8578 SetEventObject(obj
);
8582 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxEvent
)
8584 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
8585 int rowOrCol
, int x
, int y
,
8586 bool control
, bool shift
, bool alt
, bool meta
)
8587 : wxNotifyEvent( type
, id
)
8589 m_rowOrCol
= rowOrCol
;
8592 m_control
= control
;
8597 SetEventObject(obj
);
8601 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxEvent
)
8603 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
8604 const wxGridCellCoords
& topLeft
,
8605 const wxGridCellCoords
& bottomRight
,
8606 bool sel
, bool control
,
8607 bool shift
, bool alt
, bool meta
)
8608 : wxNotifyEvent( type
, id
)
8610 m_topLeft
= topLeft
;
8611 m_bottomRight
= bottomRight
;
8613 m_control
= control
;
8618 SetEventObject(obj
);
8622 #endif // ifndef wxUSE_NEW_GRID