1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/dcclient.h"
38 #include "wx/settings.h"
40 #include "wx/textctrl.h"
41 #include "wx/checkbox.h"
42 #include "wx/combobox.h"
43 #include "wx/valtext.h"
47 #include "wx/textfile.h"
48 #include "wx/spinctrl.h"
49 #include "wx/tokenzr.h"
52 #include "wx/generic/gridsel.h"
54 #if defined(__WXMOTIF__)
55 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
57 #define WXUNUSED_MOTIF(identifier) identifier
60 #if defined(__WXGTK__)
61 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
63 #define WXUNUSED_GTK(identifier) identifier
66 // Required for wxIs... functions
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
74 class WXDLLIMPEXP_ADV
);
76 struct wxGridCellWithAttr
78 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
79 : coords(row
, col
), attr(attr_
)
88 wxGridCellCoords coords
;
92 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
93 // without rewriting the macros, which require a public copy constructor.
96 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
97 class WXDLLIMPEXP_ADV
);
99 #include "wx/arrimpl.cpp"
101 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
102 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
123 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
132 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
133 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
134 const wxPoint
&pos
, const wxSize
&size
);
139 void OnPaint( wxPaintEvent
& event
);
140 void OnMouseEvent( wxMouseEvent
& event
);
141 void OnMouseWheel( wxMouseEvent
& event
);
142 void OnKeyDown( wxKeyEvent
& event
);
143 void OnKeyUp( wxKeyEvent
& );
145 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
146 DECLARE_EVENT_TABLE()
147 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
151 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
154 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
155 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
156 const wxPoint
&pos
, const wxSize
&size
);
161 void OnPaint( wxPaintEvent
&event
);
162 void OnMouseEvent( wxMouseEvent
& event
);
163 void OnMouseWheel( wxMouseEvent
& event
);
164 void OnKeyDown( wxKeyEvent
& event
);
165 void OnKeyUp( wxKeyEvent
& );
167 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
168 DECLARE_EVENT_TABLE()
169 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
173 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
176 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
177 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
178 const wxPoint
&pos
, const wxSize
&size
);
183 void OnMouseEvent( wxMouseEvent
& event
);
184 void OnMouseWheel( wxMouseEvent
& event
);
185 void OnKeyDown( wxKeyEvent
& event
);
186 void OnKeyUp( wxKeyEvent
& );
187 void OnPaint( wxPaintEvent
& event
);
189 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
190 DECLARE_EVENT_TABLE()
191 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
194 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
199 m_owner
= (wxGrid
*)NULL
;
200 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
201 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
204 wxGridWindow( wxGrid
*parent
,
205 wxGridRowLabelWindow
*rowLblWin
,
206 wxGridColLabelWindow
*colLblWin
,
207 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
210 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
212 wxGrid
* GetOwner() { return m_owner
; }
216 wxGridRowLabelWindow
*m_rowLabelWin
;
217 wxGridColLabelWindow
*m_colLabelWin
;
219 void OnPaint( wxPaintEvent
&event
);
220 void OnMouseWheel( wxMouseEvent
& event
);
221 void OnMouseEvent( wxMouseEvent
& event
);
222 void OnKeyDown( wxKeyEvent
& );
223 void OnKeyUp( wxKeyEvent
& );
224 void OnEraseBackground( wxEraseEvent
& );
225 void OnFocus( wxFocusEvent
& );
227 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
228 DECLARE_EVENT_TABLE()
229 DECLARE_NO_COPY_CLASS(wxGridWindow
)
234 class wxGridCellEditorEvtHandler
: public wxEvtHandler
237 wxGridCellEditorEvtHandler()
238 : m_grid(0), m_editor(0)
240 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
241 : m_grid(grid
), m_editor(editor
)
244 void OnKeyDown(wxKeyEvent
& event
);
245 void OnChar(wxKeyEvent
& event
);
249 wxGridCellEditor
* m_editor
;
250 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
251 DECLARE_EVENT_TABLE()
252 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
256 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
257 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
258 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
259 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
264 // ----------------------------------------------------------------------------
265 // the internal data representation used by wxGridCellAttrProvider
266 // ----------------------------------------------------------------------------
268 // this class stores attributes set for cells
269 class WXDLLIMPEXP_ADV wxGridCellAttrData
272 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
273 wxGridCellAttr
*GetAttr(int row
, int col
) const;
274 void UpdateAttrRows( size_t pos
, int numRows
);
275 void UpdateAttrCols( size_t pos
, int numCols
);
278 // searches for the attr for given cell, returns wxNOT_FOUND if not found
279 int FindIndex(int row
, int col
) const;
281 wxGridCellWithAttrArray m_attrs
;
284 // this class stores attributes set for rows or columns
285 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
288 // empty ctor to suppress warnings
289 wxGridRowOrColAttrData() { }
290 ~wxGridRowOrColAttrData();
292 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
293 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
294 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
297 wxArrayInt m_rowsOrCols
;
298 wxArrayAttrs m_attrs
;
301 // NB: this is just a wrapper around 3 objects: one which stores cell
302 // attributes, and 2 others for row/col ones
303 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
306 wxGridCellAttrData m_cellAttrs
;
307 wxGridRowOrColAttrData m_rowAttrs
,
312 // ----------------------------------------------------------------------------
313 // data structures used for the data type registry
314 // ----------------------------------------------------------------------------
316 struct wxGridDataTypeInfo
318 wxGridDataTypeInfo(const wxString
& typeName
,
319 wxGridCellRenderer
* renderer
,
320 wxGridCellEditor
* editor
)
321 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
324 ~wxGridDataTypeInfo()
326 wxSafeDecRef(m_renderer
);
327 wxSafeDecRef(m_editor
);
331 wxGridCellRenderer
* m_renderer
;
332 wxGridCellEditor
* m_editor
;
334 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
338 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
339 class WXDLLIMPEXP_ADV
);
342 class WXDLLIMPEXP_ADV wxGridTypeRegistry
345 wxGridTypeRegistry() {}
346 ~wxGridTypeRegistry();
348 void RegisterDataType(const wxString
& typeName
,
349 wxGridCellRenderer
* renderer
,
350 wxGridCellEditor
* editor
);
352 // find one of already registered data types
353 int FindRegisteredDataType(const wxString
& typeName
);
355 // try to FindRegisteredDataType(), if this fails and typeName is one of
356 // standard typenames, register it and return its index
357 int FindDataType(const wxString
& typeName
);
359 // try to FindDataType(), if it fails see if it is not one of already
360 // registered data types with some params in which case clone the
361 // registered data type and set params for it
362 int FindOrCloneDataType(const wxString
& typeName
);
364 wxGridCellRenderer
* GetRenderer(int index
);
365 wxGridCellEditor
* GetEditor(int index
);
368 wxGridDataTypeInfoArray m_typeinfo
;
371 // ----------------------------------------------------------------------------
372 // conditional compilation
373 // ----------------------------------------------------------------------------
375 #ifndef WXGRID_DRAW_LINES
376 #define WXGRID_DRAW_LINES 1
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 //#define DEBUG_ATTR_CACHE
384 #ifdef DEBUG_ATTR_CACHE
385 static size_t gs_nAttrCacheHits
= 0;
386 static size_t gs_nAttrCacheMisses
= 0;
387 #endif // DEBUG_ATTR_CACHE
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
394 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
397 // TODO: this doesn't work at all, grid cells have different sizes and approx
398 // calculations don't work as because of the size mismatch scrollbars
399 // sometimes fail to be shown when they should be or vice versa
401 // The scroll bars may be a little flakey once in a while, but that is
402 // surely much less horrible than having scroll lines of only 1!!!
405 // Well, it's still seriously broken so it might be better but needs
408 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
409 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
411 // the size of hash tables used a bit everywhere (the max number of elements
412 // in these hash tables is the number of rows/columns)
413 static const int GRID_HASH_SIZE
= 100;
415 // ----------------------------------------------------------------------------
417 // ----------------------------------------------------------------------------
419 static inline int GetScrollX(int x
)
421 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
424 static inline int GetScrollY(int y
)
426 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
429 // ============================================================================
431 // ============================================================================
433 // ----------------------------------------------------------------------------
435 // ----------------------------------------------------------------------------
437 wxGridCellEditor::wxGridCellEditor()
444 wxGridCellEditor::~wxGridCellEditor()
449 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
450 wxWindowID
WXUNUSED(id
),
451 wxEvtHandler
* evtHandler
)
454 m_control
->PushEventHandler(evtHandler
);
457 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
458 wxGridCellAttr
*attr
)
460 // erase the background because we might not fill the cell
461 wxClientDC
dc(m_control
->GetParent());
462 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
464 gridWindow
->GetOwner()->PrepareDC(dc
);
466 dc
.SetPen(*wxTRANSPARENT_PEN
);
467 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
468 dc
.DrawRectangle(rectCell
);
470 // redraw the control we just painted over
471 m_control
->Refresh();
474 void wxGridCellEditor::Destroy()
478 m_control
->PopEventHandler(true /* delete it*/);
480 m_control
->Destroy();
485 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
487 wxASSERT_MSG(m_control
,
488 wxT("The wxGridCellEditor must be Created first!"));
489 m_control
->Show(show
);
493 // set the colours/fonts if we have any
496 m_colFgOld
= m_control
->GetForegroundColour();
497 m_control
->SetForegroundColour(attr
->GetTextColour());
499 m_colBgOld
= m_control
->GetBackgroundColour();
500 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
502 m_fontOld
= m_control
->GetFont();
503 m_control
->SetFont(attr
->GetFont());
505 // can't do anything more in the base class version, the other
506 // attributes may only be used by the derived classes
511 // restore the standard colours fonts
512 if ( m_colFgOld
.Ok() )
514 m_control
->SetForegroundColour(m_colFgOld
);
515 m_colFgOld
= wxNullColour
;
518 if ( m_colBgOld
.Ok() )
520 m_control
->SetBackgroundColour(m_colBgOld
);
521 m_colBgOld
= wxNullColour
;
524 if ( m_fontOld
.Ok() )
526 m_control
->SetFont(m_fontOld
);
527 m_fontOld
= wxNullFont
;
532 void wxGridCellEditor::SetSize(const wxRect
& rect
)
534 wxASSERT_MSG(m_control
,
535 wxT("The wxGridCellEditor must be Created first!"));
536 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
539 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
544 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
546 // accept the simple key presses, not anything with Ctrl/Alt/Meta
547 return !(event
.ControlDown() || event
.AltDown());
550 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
555 void wxGridCellEditor::StartingClick()
561 // ----------------------------------------------------------------------------
562 // wxGridCellTextEditor
563 // ----------------------------------------------------------------------------
565 wxGridCellTextEditor::wxGridCellTextEditor()
570 void wxGridCellTextEditor::Create(wxWindow
* parent
,
572 wxEvtHandler
* evtHandler
)
574 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
575 wxDefaultPosition
, wxDefaultSize
576 #if defined(__WXMSW__)
577 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
581 // set max length allowed in the textctrl, if the parameter was set
584 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
587 wxGridCellEditor::Create(parent
, id
, evtHandler
);
590 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
591 wxGridCellAttr
* WXUNUSED(attr
))
593 // as we fill the entire client area, don't do anything here to minimize
597 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
599 wxRect
rect(rectOrig
);
601 // Make the edit control large enough to allow for internal
604 // TODO: remove this if the text ctrl sizing is improved esp. for
607 #if defined(__WXGTK__)
616 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
618 // MB: treat MSW separately here otherwise the caret doesn't show
619 // when the editor is in the first row.
620 #if defined(__WXMSW__)
623 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
626 #if defined(__WXMOTIF__)
630 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
631 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
632 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
633 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
636 wxGridCellEditor::SetSize(rect
);
639 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
641 wxASSERT_MSG(m_control
,
642 wxT("The wxGridCellEditor must be Created first!"));
644 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
646 DoBeginEdit(m_startValue
);
649 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
651 Text()->SetValue(startValue
);
652 Text()->SetInsertionPointEnd();
653 Text()->SetSelection(-1,-1);
657 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
660 wxASSERT_MSG(m_control
,
661 wxT("The wxGridCellEditor must be Created first!"));
663 bool changed
= false;
664 wxString value
= Text()->GetValue();
665 if (value
!= m_startValue
)
669 grid
->GetTable()->SetValue(row
, col
, value
);
671 m_startValue
= wxEmptyString
;
672 // No point in setting the text of the hidden control
673 //Text()->SetValue(m_startValue);
679 void wxGridCellTextEditor::Reset()
681 wxASSERT_MSG(m_control
,
682 wxT("The wxGridCellEditor must be Created first!"));
684 DoReset(m_startValue
);
687 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
689 Text()->SetValue(startValue
);
690 Text()->SetInsertionPointEnd();
693 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
695 if ( wxGridCellEditor::IsAcceptedKey(event
) )
697 int keycode
= event
.GetKeyCode();
711 case WXK_NUMPAD_MULTIPLY
:
715 case WXK_NUMPAD_SUBTRACT
:
717 case WXK_NUMPAD_DECIMAL
:
719 case WXK_NUMPAD_DIVIDE
:
723 // accept 8 bit chars too if isprint() agrees
724 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
732 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
734 if ( !Text()->EmulateKeyPress(event
) )
740 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
741 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
743 #if defined(__WXMOTIF__) || defined(__WXGTK__)
744 // wxMotif needs a little extra help...
745 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
746 wxString
s( Text()->GetValue() );
747 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
749 Text()->SetInsertionPoint( pos
);
751 // the other ports can handle a Return key press
757 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
767 if ( !params
.ToLong(&tmp
) )
769 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
773 m_maxChars
= (size_t)tmp
;
778 // return the value in the text control
779 wxString
wxGridCellTextEditor::GetValue() const
781 return Text()->GetValue();
784 // ----------------------------------------------------------------------------
785 // wxGridCellNumberEditor
786 // ----------------------------------------------------------------------------
788 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
794 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
796 wxEvtHandler
* evtHandler
)
800 // create a spin ctrl
801 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
802 wxDefaultPosition
, wxDefaultSize
,
806 wxGridCellEditor::Create(parent
, id
, evtHandler
);
810 // just a text control
811 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
814 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
815 #endif // wxUSE_VALIDATORS
819 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
821 // first get the value
822 wxGridTableBase
*table
= grid
->GetTable();
823 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
825 m_valueOld
= table
->GetValueAsLong(row
, col
);
830 wxString sValue
= table
->GetValue(row
, col
);
831 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
833 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
840 Spin()->SetValue((int)m_valueOld
);
845 DoBeginEdit(GetString());
849 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
858 value
= Spin()->GetValue();
859 changed
= value
!= m_valueOld
;
861 text
= wxString::Format(wxT("%ld"), value
);
865 text
= Text()->GetValue();
866 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
871 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
872 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
874 grid
->GetTable()->SetValue(row
, col
, text
);
880 void wxGridCellNumberEditor::Reset()
884 Spin()->SetValue((int)m_valueOld
);
888 DoReset(GetString());
892 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
894 if ( wxGridCellEditor::IsAcceptedKey(event
) )
896 int keycode
= event
.GetKeyCode();
912 case WXK_NUMPAD_SUBTRACT
:
918 if ( (keycode
< 128) && wxIsdigit(keycode
) )
926 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
930 int keycode
= event
.GetKeyCode();
931 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
932 || keycode
== WXK_NUMPAD0
933 || keycode
== WXK_NUMPAD1
934 || keycode
== WXK_NUMPAD2
935 || keycode
== WXK_NUMPAD3
936 || keycode
== WXK_NUMPAD4
937 || keycode
== WXK_NUMPAD5
938 || keycode
== WXK_NUMPAD6
939 || keycode
== WXK_NUMPAD7
940 || keycode
== WXK_NUMPAD8
941 || keycode
== WXK_NUMPAD9
942 || keycode
== WXK_ADD
943 || keycode
== WXK_NUMPAD_ADD
944 || keycode
== WXK_SUBTRACT
945 || keycode
== WXK_NUMPAD_SUBTRACT
)
947 wxGridCellTextEditor::StartingKey(event
);
957 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
968 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
972 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
976 // skip the error message below
981 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
985 // return the value in the spin control if it is there (the text control otherwise)
986 wxString
wxGridCellNumberEditor::GetValue() const
992 long value
= Spin()->GetValue();
993 s
.Printf(wxT("%ld"), value
);
997 s
= Text()->GetValue();
1002 // ----------------------------------------------------------------------------
1003 // wxGridCellFloatEditor
1004 // ----------------------------------------------------------------------------
1006 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1009 m_precision
= precision
;
1012 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1014 wxEvtHandler
* evtHandler
)
1016 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1018 #if wxUSE_VALIDATORS
1019 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1020 #endif // wxUSE_VALIDATORS
1023 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1025 // first get the value
1026 wxGridTableBase
*table
= grid
->GetTable();
1027 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1029 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1034 wxString sValue
= table
->GetValue(row
, col
);
1035 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1037 wxFAIL_MSG( _T("this cell doesn't have float value") );
1042 DoBeginEdit(GetString());
1045 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1049 wxString
text(Text()->GetValue());
1051 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1053 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1054 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1056 grid
->GetTable()->SetValue(row
, col
, text
);
1063 void wxGridCellFloatEditor::Reset()
1065 DoReset(GetString());
1068 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1070 int keycode
= event
.GetKeyCode();
1072 tmpbuf
[0] = (char) keycode
;
1074 bool is_decimal_point
= ( wxString(tmpbuf
, *wxConvCurrent
) ==
1075 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1076 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1078 || keycode
== WXK_NUMPAD0
1079 || keycode
== WXK_NUMPAD1
1080 || keycode
== WXK_NUMPAD2
1081 || keycode
== WXK_NUMPAD3
1082 || keycode
== WXK_NUMPAD4
1083 || keycode
== WXK_NUMPAD5
1084 || keycode
== WXK_NUMPAD6
1085 || keycode
== WXK_NUMPAD7
1086 || keycode
== WXK_NUMPAD8
1087 || keycode
== WXK_NUMPAD9
1088 || keycode
== WXK_ADD
1089 || keycode
== WXK_NUMPAD_ADD
1090 || keycode
== WXK_SUBTRACT
1091 || keycode
== WXK_NUMPAD_SUBTRACT
)
1093 wxGridCellTextEditor::StartingKey(event
);
1095 // skip Skip() below
1102 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1113 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1117 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1119 m_precision
= (int)tmp
;
1121 // skip the error message below
1126 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1130 wxString
wxGridCellFloatEditor::GetString() const
1133 if ( m_width
== -1 )
1135 // default width/precision
1138 else if ( m_precision
== -1 )
1140 // default precision
1141 fmt
.Printf(_T("%%%d.f"), m_width
);
1145 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1148 return wxString::Format(fmt
, m_valueOld
);
1151 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1153 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1155 int keycode
= event
.GetKeyCode();
1169 case WXK_NUMPAD_ADD
:
1171 case WXK_NUMPAD_SUBTRACT
:
1173 case WXK_NUMPAD_DECIMAL
:
1178 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1180 tmpbuf
[0] = (char) keycode
;
1182 bool is_decimal_point
=
1183 ( wxString(tmpbuf
, *wxConvCurrent
) ==
1184 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1185 wxLOCALE_CAT_NUMBER
) );
1186 if ( (keycode
< 128) &&
1187 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1188 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1197 #endif // wxUSE_TEXTCTRL
1201 // ----------------------------------------------------------------------------
1202 // wxGridCellBoolEditor
1203 // ----------------------------------------------------------------------------
1205 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1207 wxEvtHandler
* evtHandler
)
1209 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1210 wxDefaultPosition
, wxDefaultSize
,
1213 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1216 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1218 bool resize
= false;
1219 wxSize size
= m_control
->GetSize();
1220 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1222 // check if the checkbox is not too big/small for this cell
1223 wxSize sizeBest
= m_control
->GetBestSize();
1224 if ( !(size
== sizeBest
) )
1226 // reset to default size if it had been made smaller
1232 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1234 // leave 1 pixel margin
1235 size
.x
= size
.y
= minSize
- 2;
1242 m_control
->SetSize(size
);
1245 // position it in the centre of the rectangle (TODO: support alignment?)
1247 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1248 // the checkbox without label still has some space to the right in wxGTK,
1249 // so shift it to the right
1251 #elif defined(__WXMSW__)
1252 // here too, but in other way
1257 int hAlign
= wxALIGN_CENTRE
;
1258 int vAlign
= wxALIGN_CENTRE
;
1260 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1263 if (hAlign
== wxALIGN_LEFT
)
1269 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1271 else if (hAlign
== wxALIGN_RIGHT
)
1273 x
= r
.x
+ r
.width
- size
.x
- 2;
1274 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1276 else if (hAlign
== wxALIGN_CENTRE
)
1278 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1279 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1282 m_control
->Move(x
, y
);
1285 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1287 m_control
->Show(show
);
1291 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1292 CBox()->SetBackgroundColour(colBg
);
1296 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1298 wxASSERT_MSG(m_control
,
1299 wxT("The wxGridCellEditor must be Created first!"));
1301 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1302 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1305 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1306 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1308 CBox()->SetValue(m_startValue
);
1312 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1315 wxASSERT_MSG(m_control
,
1316 wxT("The wxGridCellEditor must be Created first!"));
1318 bool changed
= false;
1319 bool value
= CBox()->GetValue();
1320 if ( value
!= m_startValue
)
1325 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1326 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1328 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1334 void wxGridCellBoolEditor::Reset()
1336 wxASSERT_MSG(m_control
,
1337 wxT("The wxGridCellEditor must be Created first!"));
1339 CBox()->SetValue(m_startValue
);
1342 void wxGridCellBoolEditor::StartingClick()
1344 CBox()->SetValue(!CBox()->GetValue());
1347 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1349 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1351 int keycode
= event
.GetKeyCode();
1355 case WXK_NUMPAD_MULTIPLY
:
1357 case WXK_NUMPAD_ADD
:
1359 case WXK_NUMPAD_SUBTRACT
:
1370 // return the value as "1" for true and the empty string for false
1371 wxString
wxGridCellBoolEditor::GetValue() const
1373 bool bSet
= CBox()->GetValue();
1374 return bSet
? _T("1") : wxEmptyString
;
1377 #endif // wxUSE_CHECKBOX
1381 // ----------------------------------------------------------------------------
1382 // wxGridCellChoiceEditor
1383 // ----------------------------------------------------------------------------
1385 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1387 : m_choices(choices
),
1388 m_allowOthers(allowOthers
) { }
1390 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1391 const wxString choices
[],
1393 : m_allowOthers(allowOthers
)
1397 m_choices
.Alloc(count
);
1398 for ( size_t n
= 0; n
< count
; n
++ )
1400 m_choices
.Add(choices
[n
]);
1405 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1407 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1408 editor
->m_allowOthers
= m_allowOthers
;
1409 editor
->m_choices
= m_choices
;
1414 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1416 wxEvtHandler
* evtHandler
)
1418 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1419 wxDefaultPosition
, wxDefaultSize
,
1421 m_allowOthers
? 0 : wxCB_READONLY
);
1423 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1426 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1427 wxGridCellAttr
* attr
)
1429 // as we fill the entire client area, don't do anything here to minimize
1432 // TODO: It doesn't actually fill the client area since the height of a
1433 // combo always defaults to the standard... Until someone has time to
1434 // figure out the right rectangle to paint, just do it the normal way...
1435 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1438 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1440 wxASSERT_MSG(m_control
,
1441 wxT("The wxGridCellEditor must be Created first!"));
1443 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1446 Combo()->SetValue(m_startValue
);
1449 // find the right position, or default to the first if not found
1450 int pos
= Combo()->FindString(m_startValue
);
1453 Combo()->SetSelection(pos
);
1455 Combo()->SetInsertionPointEnd();
1456 Combo()->SetFocus();
1459 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1462 wxString value
= Combo()->GetValue();
1463 bool changed
= value
!= m_startValue
;
1466 grid
->GetTable()->SetValue(row
, col
, value
);
1468 m_startValue
= wxEmptyString
;
1470 Combo()->SetValue(m_startValue
);
1472 Combo()->SetSelection(0);
1477 void wxGridCellChoiceEditor::Reset()
1479 Combo()->SetValue(m_startValue
);
1480 Combo()->SetInsertionPointEnd();
1483 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1493 wxStringTokenizer
tk(params
, _T(','));
1494 while ( tk
.HasMoreTokens() )
1496 m_choices
.Add(tk
.GetNextToken());
1500 // return the value in the text control
1501 wxString
wxGridCellChoiceEditor::GetValue() const
1503 return Combo()->GetValue();
1506 #endif // wxUSE_COMBOBOX
1508 // ----------------------------------------------------------------------------
1509 // wxGridCellEditorEvtHandler
1510 // ----------------------------------------------------------------------------
1512 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1514 switch ( event
.GetKeyCode() )
1518 m_grid
->DisableCellEditControl();
1522 m_grid
->GetEventHandler()->ProcessEvent( event
);
1526 case WXK_NUMPAD_ENTER
:
1527 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1528 m_editor
->HandleReturn(event
);
1537 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1539 switch ( event
.GetKeyCode() )
1544 case WXK_NUMPAD_ENTER
:
1552 // ----------------------------------------------------------------------------
1553 // wxGridCellWorker is an (almost) empty common base class for
1554 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1555 // ----------------------------------------------------------------------------
1557 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1562 wxGridCellWorker::~wxGridCellWorker()
1566 // ============================================================================
1568 // ============================================================================
1570 // ----------------------------------------------------------------------------
1571 // wxGridCellRenderer
1572 // ----------------------------------------------------------------------------
1574 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1575 wxGridCellAttr
& attr
,
1578 int WXUNUSED(row
), int WXUNUSED(col
),
1581 dc
.SetBackgroundMode( wxSOLID
);
1583 // grey out fields if the grid is disabled
1584 if( grid
.IsEnabled() )
1588 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1592 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1597 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1600 dc
.SetPen( *wxTRANSPARENT_PEN
);
1601 dc
.DrawRectangle(rect
);
1604 // ----------------------------------------------------------------------------
1605 // wxGridCellStringRenderer
1606 // ----------------------------------------------------------------------------
1608 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1609 wxGridCellAttr
& attr
,
1613 dc
.SetBackgroundMode( wxTRANSPARENT
);
1615 // TODO some special colours for attr.IsReadOnly() case?
1617 // different coloured text when the grid is disabled
1618 if( grid
.IsEnabled() )
1622 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1623 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1627 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1628 dc
.SetTextForeground( attr
.GetTextColour() );
1633 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1634 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1637 dc
.SetFont( attr
.GetFont() );
1640 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1642 const wxString
& text
)
1644 wxCoord x
= 0, y
= 0, max_x
= 0;
1645 dc
.SetFont(attr
.GetFont());
1646 wxStringTokenizer
tk(text
, _T('\n'));
1647 while ( tk
.HasMoreTokens() )
1649 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1650 max_x
= wxMax(max_x
, x
);
1653 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1655 return wxSize(max_x
, y
);
1658 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1659 wxGridCellAttr
& attr
,
1663 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1666 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1667 wxGridCellAttr
& attr
,
1669 const wxRect
& rectCell
,
1673 wxRect rect
= rectCell
;
1676 // erase only this cells background, overflow cells should have been erased
1677 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1680 attr
.GetAlignment(&hAlign
, &vAlign
);
1682 int overflowCols
= 0;
1684 if (attr
.GetOverflow())
1686 int cols
= grid
.GetNumberCols();
1687 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1688 int cell_rows
, cell_cols
;
1689 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1690 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1692 int i
, c_cols
, c_rows
;
1693 for (i
= col
+cell_cols
; i
< cols
; i
++)
1695 bool is_empty
= true;
1696 for (int j
=row
; j
<row
+cell_rows
; j
++)
1698 // check w/ anchor cell for multicell block
1699 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1700 if (c_rows
> 0) c_rows
= 0;
1701 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1708 rect
.width
+= grid
.GetColSize(i
);
1714 if (rect
.width
>= best_width
) break;
1716 overflowCols
= i
- col
- cell_cols
+ 1;
1717 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1720 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1722 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1724 clip
.x
+= rectCell
.width
;
1725 // draw each overflow cell individually
1726 int col_end
= col
+cell_cols
+overflowCols
;
1727 if (col_end
>= grid
.GetNumberCols())
1728 col_end
= grid
.GetNumberCols() - 1;
1729 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1731 clip
.width
= grid
.GetColSize(i
) - 1;
1732 dc
.DestroyClippingRegion();
1733 dc
.SetClippingRegion(clip
);
1735 SetTextColoursAndFont(grid
, attr
, dc
,
1736 grid
.IsInSelection(row
,i
));
1738 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1739 rect
, hAlign
, vAlign
);
1740 clip
.x
+= grid
.GetColSize(i
) - 1;
1746 dc
.DestroyClippingRegion();
1750 // now we only have to draw the text
1751 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1753 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1754 rect
, hAlign
, vAlign
);
1757 // ----------------------------------------------------------------------------
1758 // wxGridCellNumberRenderer
1759 // ----------------------------------------------------------------------------
1761 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1763 wxGridTableBase
*table
= grid
.GetTable();
1765 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1767 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1771 text
= table
->GetValue(row
, col
);
1777 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1778 wxGridCellAttr
& attr
,
1780 const wxRect
& rectCell
,
1784 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1786 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1788 // draw the text right aligned by default
1790 attr
.GetAlignment(&hAlign
, &vAlign
);
1791 hAlign
= wxALIGN_RIGHT
;
1793 wxRect rect
= rectCell
;
1796 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1799 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1800 wxGridCellAttr
& attr
,
1804 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1807 // ----------------------------------------------------------------------------
1808 // wxGridCellFloatRenderer
1809 // ----------------------------------------------------------------------------
1811 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1814 SetPrecision(precision
);
1817 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1819 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1820 renderer
->m_width
= m_width
;
1821 renderer
->m_precision
= m_precision
;
1822 renderer
->m_format
= m_format
;
1827 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1829 wxGridTableBase
*table
= grid
.GetTable();
1834 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1836 val
= table
->GetValueAsDouble(row
, col
);
1841 text
= table
->GetValue(row
, col
);
1842 hasDouble
= text
.ToDouble(&val
);
1849 if ( m_width
== -1 )
1851 if ( m_precision
== -1 )
1853 // default width/precision
1854 m_format
= _T("%f");
1858 m_format
.Printf(_T("%%.%df"), m_precision
);
1861 else if ( m_precision
== -1 )
1863 // default precision
1864 m_format
.Printf(_T("%%%d.f"), m_width
);
1868 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1872 text
.Printf(m_format
, val
);
1875 //else: text already contains the string
1880 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1881 wxGridCellAttr
& attr
,
1883 const wxRect
& rectCell
,
1887 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1889 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1891 // draw the text right aligned by default
1893 attr
.GetAlignment(&hAlign
, &vAlign
);
1894 hAlign
= wxALIGN_RIGHT
;
1896 wxRect rect
= rectCell
;
1899 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1902 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1903 wxGridCellAttr
& attr
,
1907 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1910 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1914 // reset to defaults
1920 wxString tmp
= params
.BeforeFirst(_T(','));
1924 if ( tmp
.ToLong(&width
) )
1926 SetWidth((int)width
);
1930 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1934 tmp
= params
.AfterFirst(_T(','));
1938 if ( tmp
.ToLong(&precision
) )
1940 SetPrecision((int)precision
);
1944 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1952 // ----------------------------------------------------------------------------
1953 // wxGridCellBoolRenderer
1954 // ----------------------------------------------------------------------------
1956 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1958 // FIXME these checkbox size calculations are really ugly...
1960 // between checkmark and box
1961 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1963 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1964 wxGridCellAttr
& WXUNUSED(attr
),
1969 // compute it only once (no locks for MT safeness in GUI thread...)
1970 if ( !ms_sizeCheckMark
.x
)
1972 // get checkbox size
1973 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1974 wxSize size
= checkbox
->GetBestSize();
1975 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1977 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1978 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1979 checkSize
-= size
.y
/ 2;
1984 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1987 return ms_sizeCheckMark
;
1990 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1991 wxGridCellAttr
& attr
,
1997 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1999 // draw a check mark in the centre (ignoring alignment - TODO)
2000 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2002 // don't draw outside the cell
2003 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2004 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2006 // and even leave (at least) 1 pixel margin
2007 size
.x
= size
.y
= minSize
- 2;
2010 // draw a border around checkmark
2012 attr
.GetAlignment(& hAlign
, &vAlign
);
2015 if (hAlign
== wxALIGN_CENTRE
)
2017 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2018 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2019 rectBorder
.width
= size
.x
;
2020 rectBorder
.height
= size
.y
;
2022 else if (hAlign
== wxALIGN_LEFT
)
2024 rectBorder
.x
= rect
.x
+ 2;
2025 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2026 rectBorder
.width
= size
.x
;
2027 rectBorder
.height
= size
.y
;
2029 else if (hAlign
== wxALIGN_RIGHT
)
2031 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2032 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2033 rectBorder
.width
= size
.x
;
2034 rectBorder
.height
= size
.y
;
2038 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2039 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2042 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2043 value
= !( !cellval
|| (cellval
== wxT("0")) );
2048 wxRect rectMark
= rectBorder
;
2050 // MSW DrawCheckMark() is weird (and should probably be changed...)
2051 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2055 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2058 dc
.SetTextForeground(attr
.GetTextColour());
2059 dc
.DrawCheckMark(rectMark
);
2062 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2063 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2064 dc
.DrawRectangle(rectBorder
);
2067 // ----------------------------------------------------------------------------
2069 // ----------------------------------------------------------------------------
2071 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2075 m_isReadOnly
= Unset
;
2080 m_attrkind
= wxGridCellAttr::Cell
;
2082 m_sizeRows
= m_sizeCols
= 1;
2083 m_overflow
= UnsetOverflow
;
2085 SetDefAttr(attrDefault
);
2088 wxGridCellAttr
*wxGridCellAttr::Clone() const
2090 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2092 if ( HasTextColour() )
2093 attr
->SetTextColour(GetTextColour());
2094 if ( HasBackgroundColour() )
2095 attr
->SetBackgroundColour(GetBackgroundColour());
2097 attr
->SetFont(GetFont());
2098 if ( HasAlignment() )
2099 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2101 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2105 attr
->SetRenderer(m_renderer
);
2106 m_renderer
->IncRef();
2110 attr
->SetEditor(m_editor
);
2115 attr
->SetReadOnly();
2117 attr
->SetKind( m_attrkind
);
2122 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2124 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2125 SetTextColour(mergefrom
->GetTextColour());
2126 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2127 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2128 if ( !HasFont() && mergefrom
->HasFont() )
2129 SetFont(mergefrom
->GetFont());
2130 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2132 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2133 SetAlignment(hAlign
, vAlign
);
2136 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2138 // Directly access member functions as GetRender/Editor don't just return
2139 // m_renderer/m_editor
2141 // Maybe add support for merge of Render and Editor?
2142 if (!HasRenderer() && mergefrom
->HasRenderer() )
2144 m_renderer
= mergefrom
->m_renderer
;
2145 m_renderer
->IncRef();
2147 if ( !HasEditor() && mergefrom
->HasEditor() )
2149 m_editor
= mergefrom
->m_editor
;
2152 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2153 SetReadOnly(mergefrom
->IsReadOnly());
2155 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2156 SetOverflow(mergefrom
->GetOverflow());
2158 SetDefAttr(mergefrom
->m_defGridAttr
);
2161 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2163 // The size of a cell is normally 1,1
2165 // If this cell is larger (2,2) then this is the top left cell
2166 // the other cells that will be covered (lower right cells) must be
2167 // set to negative or zero values such that
2168 // row + num_rows of the covered cell points to the larger cell (this cell)
2169 // same goes for the col + num_cols.
2171 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2173 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2174 !((num_rows
<=0)&&(num_cols
>0)) ||
2175 !((num_rows
==0)&&(num_cols
==0))),
2176 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2178 m_sizeRows
= num_rows
;
2179 m_sizeCols
= num_cols
;
2182 const wxColour
& wxGridCellAttr::GetTextColour() const
2184 if (HasTextColour())
2188 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2190 return m_defGridAttr
->GetTextColour();
2194 wxFAIL_MSG(wxT("Missing default cell attribute"));
2195 return wxNullColour
;
2200 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2202 if (HasBackgroundColour())
2204 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2205 return m_defGridAttr
->GetBackgroundColour();
2208 wxFAIL_MSG(wxT("Missing default cell attribute"));
2209 return wxNullColour
;
2214 const wxFont
& wxGridCellAttr::GetFont() const
2218 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2219 return m_defGridAttr
->GetFont();
2222 wxFAIL_MSG(wxT("Missing default cell attribute"));
2228 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2232 if ( hAlign
) *hAlign
= m_hAlign
;
2233 if ( vAlign
) *vAlign
= m_vAlign
;
2235 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2236 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2239 wxFAIL_MSG(wxT("Missing default cell attribute"));
2243 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2245 if ( num_rows
) *num_rows
= m_sizeRows
;
2246 if ( num_cols
) *num_cols
= m_sizeCols
;
2249 // GetRenderer and GetEditor use a slightly different decision path about
2250 // which attribute to use. If a non-default attr object has one then it is
2251 // used, otherwise the default editor or renderer is fetched from the grid and
2252 // used. It should be the default for the data type of the cell. If it is
2253 // NULL (because the table has a type that the grid does not have in its
2254 // registry,) then the grid's default editor or renderer is used.
2256 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2258 wxGridCellRenderer
*renderer
;
2260 if ( m_renderer
&& this != m_defGridAttr
)
2262 // use the cells renderer if it has one
2263 renderer
= m_renderer
;
2266 else // no non default cell renderer
2268 // get default renderer for the data type
2271 // GetDefaultRendererForCell() will do IncRef() for us
2272 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2281 if (m_defGridAttr
&& this != m_defGridAttr
)
2283 // if we still don't have one then use the grid default
2284 // (no need for IncRef() here neither)
2285 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2287 else // default grid attr
2289 // use m_renderer which we had decided not to use initially
2290 renderer
= m_renderer
;
2297 // we're supposed to always find something
2298 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2303 // same as above, except for s/renderer/editor/g
2304 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2306 wxGridCellEditor
*editor
;
2308 if ( m_editor
&& this != m_defGridAttr
)
2310 // use the cells editor if it has one
2314 else // no non default cell editor
2316 // get default editor for the data type
2319 // GetDefaultEditorForCell() will do IncRef() for us
2320 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2329 if ( m_defGridAttr
&& this != m_defGridAttr
)
2331 // if we still don't have one then use the grid default
2332 // (no need for IncRef() here neither)
2333 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2335 else // default grid attr
2337 // use m_editor which we had decided not to use initially
2345 // we're supposed to always find something
2346 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2351 // ----------------------------------------------------------------------------
2352 // wxGridCellAttrData
2353 // ----------------------------------------------------------------------------
2355 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2357 int n
= FindIndex(row
, col
);
2358 if ( n
== wxNOT_FOUND
)
2360 // add the attribute
2361 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2365 // free the old attribute
2366 m_attrs
[(size_t)n
].attr
->DecRef();
2370 // change the attribute
2371 m_attrs
[(size_t)n
].attr
= attr
;
2375 // remove this attribute
2376 m_attrs
.RemoveAt((size_t)n
);
2381 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2383 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2385 int n
= FindIndex(row
, col
);
2386 if ( n
!= wxNOT_FOUND
)
2388 attr
= m_attrs
[(size_t)n
].attr
;
2395 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2397 size_t count
= m_attrs
.GetCount();
2398 for ( size_t n
= 0; n
< count
; n
++ )
2400 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2401 wxCoord row
= coords
.GetRow();
2402 if ((size_t)row
>= pos
)
2406 // If rows inserted, include row counter where necessary
2407 coords
.SetRow(row
+ numRows
);
2409 else if (numRows
< 0)
2411 // If rows deleted ...
2412 if ((size_t)row
>= pos
- numRows
)
2414 // ...either decrement row counter (if row still exists)...
2415 coords
.SetRow(row
+ numRows
);
2419 // ...or remove the attribute
2420 m_attrs
.RemoveAt((size_t)n
);
2428 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2430 size_t count
= m_attrs
.GetCount();
2431 for ( size_t n
= 0; n
< count
; n
++ )
2433 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2434 wxCoord col
= coords
.GetCol();
2435 if ( (size_t)col
>= pos
)
2439 // If rows inserted, include row counter where necessary
2440 coords
.SetCol(col
+ numCols
);
2442 else if (numCols
< 0)
2444 // If rows deleted ...
2445 if ((size_t)col
>= pos
- numCols
)
2447 // ...either decrement row counter (if row still exists)...
2448 coords
.SetCol(col
+ numCols
);
2452 // ...or remove the attribute
2453 m_attrs
.RemoveAt((size_t)n
);
2461 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2463 size_t count
= m_attrs
.GetCount();
2464 for ( size_t n
= 0; n
< count
; n
++ )
2466 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2467 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2476 // ----------------------------------------------------------------------------
2477 // wxGridRowOrColAttrData
2478 // ----------------------------------------------------------------------------
2480 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2482 size_t count
= m_attrs
.Count();
2483 for ( size_t n
= 0; n
< count
; n
++ )
2485 m_attrs
[n
]->DecRef();
2489 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2491 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2493 int n
= m_rowsOrCols
.Index(rowOrCol
);
2494 if ( n
!= wxNOT_FOUND
)
2496 attr
= m_attrs
[(size_t)n
];
2503 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2505 int i
= m_rowsOrCols
.Index(rowOrCol
);
2506 if ( i
== wxNOT_FOUND
)
2508 // add the attribute
2509 m_rowsOrCols
.Add(rowOrCol
);
2514 size_t n
= (size_t)i
;
2517 // change the attribute
2518 m_attrs
[n
]->DecRef();
2523 // remove this attribute
2524 m_attrs
[n
]->DecRef();
2525 m_rowsOrCols
.RemoveAt(n
);
2526 m_attrs
.RemoveAt(n
);
2531 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2533 size_t count
= m_attrs
.GetCount();
2534 for ( size_t n
= 0; n
< count
; n
++ )
2536 int & rowOrCol
= m_rowsOrCols
[n
];
2537 if ( (size_t)rowOrCol
>= pos
)
2539 if ( numRowsOrCols
> 0 )
2541 // If rows inserted, include row counter where necessary
2542 rowOrCol
+= numRowsOrCols
;
2544 else if ( numRowsOrCols
< 0)
2546 // If rows deleted, either decrement row counter (if row still exists)
2547 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2548 rowOrCol
+= numRowsOrCols
;
2551 m_rowsOrCols
.RemoveAt((size_t)n
);
2552 m_attrs
.RemoveAt((size_t)n
);
2560 // ----------------------------------------------------------------------------
2561 // wxGridCellAttrProvider
2562 // ----------------------------------------------------------------------------
2564 wxGridCellAttrProvider::wxGridCellAttrProvider()
2566 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2569 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2574 void wxGridCellAttrProvider::InitData()
2576 m_data
= new wxGridCellAttrProviderData
;
2579 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2580 wxGridCellAttr::wxAttrKind kind
) const
2582 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2587 case (wxGridCellAttr::Any
):
2588 //Get cached merge attributes.
2589 // Currenlty not used as no cache implemented as not mutiable
2590 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2593 //Basicaly implement old version.
2594 //Also check merge cache, so we don't have to re-merge every time..
2595 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2596 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2597 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2599 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2600 // Two or move are non NULL
2601 attr
= new wxGridCellAttr
;
2602 attr
->SetKind(wxGridCellAttr::Merged
);
2606 attr
->MergeWith(attrcell
);
2610 attr
->MergeWith(attrcol
);
2614 attr
->MergeWith(attrrow
);
2617 //store merge attr if cache implemented
2619 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2623 // one or none is non null return it or null.
2624 if(attrrow
) attr
= attrrow
;
2625 if(attrcol
) attr
= attrcol
;
2626 if(attrcell
) attr
= attrcell
;
2630 case (wxGridCellAttr::Cell
):
2631 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2633 case (wxGridCellAttr::Col
):
2634 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2636 case (wxGridCellAttr::Row
):
2637 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2641 // (wxGridCellAttr::Default):
2642 // (wxGridCellAttr::Merged):
2649 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2655 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2658 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2663 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2666 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2671 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2674 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2678 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2680 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2684 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2688 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2690 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2694 // ----------------------------------------------------------------------------
2695 // wxGridTypeRegistry
2696 // ----------------------------------------------------------------------------
2698 wxGridTypeRegistry::~wxGridTypeRegistry()
2700 size_t count
= m_typeinfo
.Count();
2701 for ( size_t i
= 0; i
< count
; i
++ )
2702 delete m_typeinfo
[i
];
2706 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2707 wxGridCellRenderer
* renderer
,
2708 wxGridCellEditor
* editor
)
2710 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2712 // is it already registered?
2713 int loc
= FindRegisteredDataType(typeName
);
2714 if ( loc
!= wxNOT_FOUND
)
2716 delete m_typeinfo
[loc
];
2717 m_typeinfo
[loc
] = info
;
2721 m_typeinfo
.Add(info
);
2725 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2727 size_t count
= m_typeinfo
.GetCount();
2728 for ( size_t i
= 0; i
< count
; i
++ )
2730 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2739 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2741 int index
= FindRegisteredDataType(typeName
);
2742 if ( index
== wxNOT_FOUND
)
2744 // check whether this is one of the standard ones, in which case
2745 // register it "on the fly"
2747 if ( typeName
== wxGRID_VALUE_STRING
)
2749 RegisterDataType(wxGRID_VALUE_STRING
,
2750 new wxGridCellStringRenderer
,
2751 new wxGridCellTextEditor
);
2753 #endif // wxUSE_TEXTCTRL
2755 if ( typeName
== wxGRID_VALUE_BOOL
)
2757 RegisterDataType(wxGRID_VALUE_BOOL
,
2758 new wxGridCellBoolRenderer
,
2759 new wxGridCellBoolEditor
);
2761 #endif // wxUSE_CHECKBOX
2763 if ( typeName
== wxGRID_VALUE_NUMBER
)
2765 RegisterDataType(wxGRID_VALUE_NUMBER
,
2766 new wxGridCellNumberRenderer
,
2767 new wxGridCellNumberEditor
);
2769 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2771 RegisterDataType(wxGRID_VALUE_FLOAT
,
2772 new wxGridCellFloatRenderer
,
2773 new wxGridCellFloatEditor
);
2775 #endif // wxUSE_TEXTCTRL
2777 if ( typeName
== wxGRID_VALUE_CHOICE
)
2779 RegisterDataType(wxGRID_VALUE_CHOICE
,
2780 new wxGridCellStringRenderer
,
2781 new wxGridCellChoiceEditor
);
2783 #endif // wxUSE_COMBOBOX
2788 // we get here only if just added the entry for this type, so return
2790 index
= m_typeinfo
.GetCount() - 1;
2796 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2798 int index
= FindDataType(typeName
);
2799 if ( index
== wxNOT_FOUND
)
2801 // the first part of the typename is the "real" type, anything after ':'
2802 // are the parameters for the renderer
2803 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2804 if ( index
== wxNOT_FOUND
)
2809 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2810 wxGridCellRenderer
*rendererOld
= renderer
;
2811 renderer
= renderer
->Clone();
2812 rendererOld
->DecRef();
2814 wxGridCellEditor
*editor
= GetEditor(index
);
2815 wxGridCellEditor
*editorOld
= editor
;
2816 editor
= editor
->Clone();
2817 editorOld
->DecRef();
2819 // do it even if there are no parameters to reset them to defaults
2820 wxString params
= typeName
.AfterFirst(_T(':'));
2821 renderer
->SetParameters(params
);
2822 editor
->SetParameters(params
);
2824 // register the new typename
2825 RegisterDataType(typeName
, renderer
, editor
);
2827 // we just registered it, it's the last one
2828 index
= m_typeinfo
.GetCount() - 1;
2834 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2836 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2842 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2844 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2850 // ----------------------------------------------------------------------------
2852 // ----------------------------------------------------------------------------
2854 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2857 wxGridTableBase::wxGridTableBase()
2859 m_view
= (wxGrid
*) NULL
;
2860 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2863 wxGridTableBase::~wxGridTableBase()
2865 delete m_attrProvider
;
2868 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2870 delete m_attrProvider
;
2871 m_attrProvider
= attrProvider
;
2874 bool wxGridTableBase::CanHaveAttributes()
2876 if ( ! GetAttrProvider() )
2878 // use the default attr provider by default
2879 SetAttrProvider(new wxGridCellAttrProvider
);
2884 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2886 if ( m_attrProvider
)
2887 return m_attrProvider
->GetAttr(row
, col
, kind
);
2889 return (wxGridCellAttr
*)NULL
;
2892 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2894 if ( m_attrProvider
)
2896 attr
->SetKind(wxGridCellAttr::Cell
);
2897 m_attrProvider
->SetAttr(attr
, row
, col
);
2901 // as we take ownership of the pointer and don't store it, we must
2907 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2909 if ( m_attrProvider
)
2911 attr
->SetKind(wxGridCellAttr::Row
);
2912 m_attrProvider
->SetRowAttr(attr
, row
);
2916 // as we take ownership of the pointer and don't store it, we must
2922 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2924 if ( m_attrProvider
)
2926 attr
->SetKind(wxGridCellAttr::Col
);
2927 m_attrProvider
->SetColAttr(attr
, col
);
2931 // as we take ownership of the pointer and don't store it, we must
2937 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2938 size_t WXUNUSED(numRows
) )
2940 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2945 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2947 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2952 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2953 size_t WXUNUSED(numRows
) )
2955 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2960 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2961 size_t WXUNUSED(numCols
) )
2963 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2968 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2970 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2975 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2976 size_t WXUNUSED(numCols
) )
2978 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2984 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2987 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2988 // how much it makes sense to us geeks.
2992 wxString
wxGridTableBase::GetColLabelValue( int col
)
2994 // default col labels are:
2995 // cols 0 to 25 : A-Z
2996 // cols 26 to 675 : AA-ZZ
3001 for ( n
= 1; ; n
++ )
3003 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3005 if ( col
< 0 ) break;
3008 // reverse the string...
3010 for ( i
= 0; i
< n
; i
++ )
3019 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3021 return wxGRID_VALUE_STRING
;
3024 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3025 const wxString
& typeName
)
3027 return typeName
== wxGRID_VALUE_STRING
;
3030 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3032 return CanGetValueAs(row
, col
, typeName
);
3035 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3040 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3045 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3050 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3051 long WXUNUSED(value
) )
3055 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3056 double WXUNUSED(value
) )
3060 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3061 bool WXUNUSED(value
) )
3066 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3067 const wxString
& WXUNUSED(typeName
) )
3072 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3073 const wxString
& WXUNUSED(typeName
),
3074 void* WXUNUSED(value
) )
3078 //////////////////////////////////////////////////////////////////////
3080 // Message class for the grid table to send requests and notifications
3084 wxGridTableMessage::wxGridTableMessage()
3086 m_table
= (wxGridTableBase
*) NULL
;
3092 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3093 int commandInt1
, int commandInt2
)
3097 m_comInt1
= commandInt1
;
3098 m_comInt2
= commandInt2
;
3103 //////////////////////////////////////////////////////////////////////
3105 // A basic grid table for string data. An object of this class will
3106 // created by wxGrid if you don't specify an alternative table class.
3109 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3111 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3113 wxGridStringTable::wxGridStringTable()
3118 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3121 m_data
.Alloc( numRows
);
3124 sa
.Alloc( numCols
);
3125 sa
.Add( wxEmptyString
, numCols
);
3127 m_data
.Add( sa
, numRows
);
3130 wxGridStringTable::~wxGridStringTable()
3134 int wxGridStringTable::GetNumberRows()
3136 return m_data
.GetCount();
3139 int wxGridStringTable::GetNumberCols()
3141 if ( m_data
.GetCount() > 0 )
3142 return m_data
[0].GetCount();
3147 wxString
wxGridStringTable::GetValue( int row
, int col
)
3149 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3151 _T("invalid row or column index in wxGridStringTable") );
3153 return m_data
[row
][col
];
3156 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3158 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3159 _T("invalid row or column index in wxGridStringTable") );
3161 m_data
[row
][col
] = value
;
3164 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3166 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3168 _T("invalid row or column index in wxGridStringTable") );
3170 return (m_data
[row
][col
] == wxEmptyString
);
3173 void wxGridStringTable::Clear()
3176 int numRows
, numCols
;
3178 numRows
= m_data
.GetCount();
3181 numCols
= m_data
[0].GetCount();
3183 for ( row
= 0; row
< numRows
; row
++ )
3185 for ( col
= 0; col
< numCols
; col
++ )
3187 m_data
[row
][col
] = wxEmptyString
;
3194 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3196 size_t curNumRows
= m_data
.GetCount();
3197 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3198 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3200 if ( pos
>= curNumRows
)
3202 return AppendRows( numRows
);
3206 sa
.Alloc( curNumCols
);
3207 sa
.Add( wxEmptyString
, curNumCols
);
3208 m_data
.Insert( sa
, pos
, numRows
);
3211 wxGridTableMessage
msg( this,
3212 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3216 GetView()->ProcessTableMessage( msg
);
3222 bool wxGridStringTable::AppendRows( size_t numRows
)
3224 size_t curNumRows
= m_data
.GetCount();
3225 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3226 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3229 if ( curNumCols
> 0 )
3231 sa
.Alloc( curNumCols
);
3232 sa
.Add( wxEmptyString
, curNumCols
);
3235 m_data
.Add( sa
, numRows
);
3239 wxGridTableMessage
msg( this,
3240 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3243 GetView()->ProcessTableMessage( msg
);
3249 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3251 size_t curNumRows
= m_data
.GetCount();
3253 if ( pos
>= curNumRows
)
3255 wxFAIL_MSG( wxString::Format
3257 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3259 (unsigned long)numRows
,
3260 (unsigned long)curNumRows
3266 if ( numRows
> curNumRows
- pos
)
3268 numRows
= curNumRows
- pos
;
3271 if ( numRows
>= curNumRows
)
3277 m_data
.RemoveAt( pos
, numRows
);
3281 wxGridTableMessage
msg( this,
3282 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3286 GetView()->ProcessTableMessage( msg
);
3292 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3296 size_t curNumRows
= m_data
.GetCount();
3297 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3298 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3300 if ( pos
>= curNumCols
)
3302 return AppendCols( numCols
);
3305 for ( row
= 0; row
< curNumRows
; row
++ )
3307 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3309 m_data
[row
].Insert( wxEmptyString
, col
);
3314 wxGridTableMessage
msg( this,
3315 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3319 GetView()->ProcessTableMessage( msg
);
3325 bool wxGridStringTable::AppendCols( size_t numCols
)
3329 size_t curNumRows
= m_data
.GetCount();
3333 // TODO: something better than this ?
3335 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3340 for ( row
= 0; row
< curNumRows
; row
++ )
3342 m_data
[row
].Add( wxEmptyString
, numCols
);
3347 wxGridTableMessage
msg( this,
3348 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3351 GetView()->ProcessTableMessage( msg
);
3357 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3361 size_t curNumRows
= m_data
.GetCount();
3362 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3363 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3365 if ( pos
>= curNumCols
)
3367 wxFAIL_MSG( wxString::Format
3369 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3371 (unsigned long)numCols
,
3372 (unsigned long)curNumCols
3377 if ( numCols
> curNumCols
- pos
)
3379 numCols
= curNumCols
- pos
;
3382 for ( row
= 0; row
< curNumRows
; row
++ )
3384 if ( numCols
>= curNumCols
)
3386 m_data
[row
].Clear();
3390 m_data
[row
].RemoveAt( pos
, numCols
);
3395 wxGridTableMessage
msg( this,
3396 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3400 GetView()->ProcessTableMessage( msg
);
3406 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3408 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3410 // using default label
3412 return wxGridTableBase::GetRowLabelValue( row
);
3416 return m_rowLabels
[ row
];
3420 wxString
wxGridStringTable::GetColLabelValue( int col
)
3422 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3424 // using default label
3426 return wxGridTableBase::GetColLabelValue( col
);
3430 return m_colLabels
[ col
];
3434 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3436 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3438 int n
= m_rowLabels
.GetCount();
3440 for ( i
= n
; i
<= row
; i
++ )
3442 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3446 m_rowLabels
[row
] = value
;
3449 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3451 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3453 int n
= m_colLabels
.GetCount();
3455 for ( i
= n
; i
<= col
; i
++ )
3457 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3461 m_colLabels
[col
] = value
;
3466 //////////////////////////////////////////////////////////////////////
3467 //////////////////////////////////////////////////////////////////////
3469 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3471 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3472 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3473 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3474 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3475 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3476 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3479 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3481 const wxPoint
&pos
, const wxSize
&size
)
3482 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3487 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3491 // NO - don't do this because it will set both the x and y origin
3492 // coords to match the parent scrolled window and we just want to
3493 // set the y coord - MB
3495 // m_owner->PrepareDC( dc );
3498 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3499 dc
.SetDeviceOrigin( 0, -y
);
3501 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3502 m_owner
->DrawRowLabels( dc
, rows
);
3506 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3508 m_owner
->ProcessRowLabelMouseEvent( event
);
3512 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3514 m_owner
->GetEventHandler()->ProcessEvent(event
);
3518 // This seems to be required for wxMotif otherwise the mouse
3519 // cursor must be in the cell edit control to get key events
3521 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3523 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3526 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3528 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3533 //////////////////////////////////////////////////////////////////////
3535 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3537 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3538 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3539 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3540 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3541 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3542 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3545 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3547 const wxPoint
&pos
, const wxSize
&size
)
3548 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3553 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3557 // NO - don't do this because it will set both the x and y origin
3558 // coords to match the parent scrolled window and we just want to
3559 // set the x coord - MB
3561 // m_owner->PrepareDC( dc );
3564 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3565 dc
.SetDeviceOrigin( -x
, 0 );
3567 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3568 m_owner
->DrawColLabels( dc
, cols
);
3572 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3574 m_owner
->ProcessColLabelMouseEvent( event
);
3577 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3579 m_owner
->GetEventHandler()->ProcessEvent(event
);
3583 // This seems to be required for wxMotif otherwise the mouse
3584 // cursor must be in the cell edit control to get key events
3586 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3588 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3591 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3593 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3598 //////////////////////////////////////////////////////////////////////
3600 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3602 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3603 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3604 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3605 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3606 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3607 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3610 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3612 const wxPoint
&pos
, const wxSize
&size
)
3613 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3618 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3622 int client_height
= 0;
3623 int client_width
= 0;
3624 GetClientSize( &client_width
, &client_height
);
3626 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3627 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3628 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3629 dc
.DrawLine( 0, 0, client_width
, 0 );
3630 dc
.DrawLine( 0, 0, 0, client_height
);
3632 dc
.SetPen( *wxWHITE_PEN
);
3633 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3634 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3638 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3640 m_owner
->ProcessCornerLabelMouseEvent( event
);
3644 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3646 m_owner
->GetEventHandler()->ProcessEvent(event
);
3649 // This seems to be required for wxMotif otherwise the mouse
3650 // cursor must be in the cell edit control to get key events
3652 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3654 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3657 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3659 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3664 //////////////////////////////////////////////////////////////////////
3666 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3668 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3669 EVT_PAINT( wxGridWindow::OnPaint
)
3670 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3671 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3672 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3673 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3674 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3675 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3676 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3679 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3680 wxGridRowLabelWindow
*rowLblWin
,
3681 wxGridColLabelWindow
*colLblWin
,
3684 const wxSize
&size
)
3685 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3686 wxT("grid window") )
3690 m_rowLabelWin
= rowLblWin
;
3691 m_colLabelWin
= colLblWin
;
3695 wxGridWindow::~wxGridWindow()
3700 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3702 wxPaintDC
dc( this );
3703 m_owner
->PrepareDC( dc
);
3704 wxRegion reg
= GetUpdateRegion();
3705 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3706 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3707 #if WXGRID_DRAW_LINES
3708 m_owner
->DrawAllGridLines( dc
, reg
);
3710 m_owner
->DrawGridSpace( dc
);
3711 m_owner
->DrawHighlight( dc
, DirtyCells
);
3715 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3717 wxWindow::ScrollWindow( dx
, dy
, rect
);
3718 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3719 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3723 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3725 m_owner
->ProcessGridCellMouseEvent( event
);
3728 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3730 m_owner
->GetEventHandler()->ProcessEvent(event
);
3733 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3734 // cursor must be in the cell edit control to get key events
3736 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3738 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3741 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3743 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3746 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3750 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3752 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3756 //////////////////////////////////////////////////////////////////////
3758 // Internal Helper function for computing row or column from some
3759 // (unscrolled) coordinate value, using either
3760 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3761 // of m_rowBottoms/m_ColRights to speed up the search!
3763 // Internal helper macros for simpler use of that function
3765 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3766 const wxArrayInt
& BorderArray
, int nMax
,
3769 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3770 m_minAcceptableColWidth, \
3771 m_colRights, m_numCols, true)
3772 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3773 m_minAcceptableRowHeight, \
3774 m_rowBottoms, m_numRows, true)
3775 /////////////////////////////////////////////////////////////////////
3777 #if wxUSE_EXTENDED_RTTI
3778 WX_DEFINE_FLAGS( wxGridStyle
)
3780 wxBEGIN_FLAGS( wxGridStyle
)
3781 // new style border flags, we put them first to
3782 // use them for streaming out
3783 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3784 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3785 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3786 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3787 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3788 wxFLAGS_MEMBER(wxBORDER_NONE
)
3790 // old style border flags
3791 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3792 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3793 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3794 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3795 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3796 wxFLAGS_MEMBER(wxBORDER
)
3798 // standard window styles
3799 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3800 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3801 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3802 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3803 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3804 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3805 wxFLAGS_MEMBER(wxVSCROLL
)
3806 wxFLAGS_MEMBER(wxHSCROLL
)
3808 wxEND_FLAGS( wxGridStyle
)
3810 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3812 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3813 wxHIDE_PROPERTY( Children
)
3814 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3815 wxEND_PROPERTIES_TABLE()
3817 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3818 wxEND_HANDLERS_TABLE()
3820 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3823 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3826 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3829 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3830 EVT_PAINT( wxGrid::OnPaint
)
3831 EVT_SIZE( wxGrid::OnSize
)
3832 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3833 EVT_KEY_UP( wxGrid::OnKeyUp
)
3834 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3839 // in order to make sure that a size event is not
3840 // trigerred in a unfinished state
3841 m_cornerLabelWin
= NULL
;
3842 m_rowLabelWin
= NULL
;
3843 m_colLabelWin
= NULL
;
3847 wxGrid::wxGrid( wxWindow
*parent
,
3852 const wxString
& name
)
3853 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3854 m_colMinWidths(GRID_HASH_SIZE
),
3855 m_rowMinHeights(GRID_HASH_SIZE
)
3860 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3861 const wxPoint
& pos
, const wxSize
& size
,
3862 long style
, const wxString
& name
)
3864 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3865 style
| wxWANTS_CHARS
, name
))
3868 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3869 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3880 // Must do this or ~wxScrollHelper will pop the wrong event handler
3881 SetTargetWindow(this);
3883 wxSafeDecRef(m_defaultCellAttr
);
3885 #ifdef DEBUG_ATTR_CACHE
3886 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3887 wxPrintf(_T("wxGrid attribute cache statistics: "
3888 "total: %u, hits: %u (%u%%)\n"),
3889 total
, gs_nAttrCacheHits
,
3890 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3896 delete m_typeRegistry
;
3902 // ----- internal init and update functions
3905 // NOTE: If using the default visual attributes works everywhere then this can
3906 // be removed as well as the #else cases below.
3907 #define _USE_VISATTR 0
3910 #include "wx/listbox.h"
3913 void wxGrid::Create()
3915 m_created
= false; // set to true by CreateGrid
3917 m_table
= (wxGridTableBase
*) NULL
;
3920 m_cellEditCtrlEnabled
= false;
3922 m_defaultCellAttr
= new wxGridCellAttr();
3924 // Set default cell attributes
3925 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3926 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3927 m_defaultCellAttr
->SetFont(GetFont());
3928 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3929 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3930 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3933 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3934 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3936 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3937 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3940 m_defaultCellAttr
->SetTextColour(
3941 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3942 m_defaultCellAttr
->SetBackgroundColour(
3943 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3948 m_currentCellCoords
= wxGridNoCellCoords
;
3950 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3951 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3953 // create the type registry
3954 m_typeRegistry
= new wxGridTypeRegistry
;
3957 // subwindow components that make up the wxGrid
3958 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3963 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3968 m_colLabelWin
= new wxGridColLabelWindow( this,
3973 m_gridWin
= new wxGridWindow( this,
3980 SetTargetWindow( m_gridWin
);
3983 wxColour gfg
= gva
.colFg
;
3984 wxColour gbg
= gva
.colBg
;
3985 wxColour lfg
= lva
.colFg
;
3986 wxColour lbg
= lva
.colBg
;
3988 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3989 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3990 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3991 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
3993 m_cornerLabelWin
->SetDefaultForegroundColour(lfg
);
3994 m_cornerLabelWin
->SetDefaultBackgroundColour(lbg
);
3995 m_rowLabelWin
->SetDefaultForegroundColour(lfg
);
3996 m_rowLabelWin
->SetDefaultBackgroundColour(lbg
);
3997 m_colLabelWin
->SetDefaultForegroundColour(lfg
);
3998 m_colLabelWin
->SetDefaultBackgroundColour(lbg
);
4000 m_gridWin
->SetDefaultForegroundColour(gfg
);
4001 m_gridWin
->SetDefaultBackgroundColour(gbg
);
4007 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4008 wxGrid::wxGridSelectionModes selmode
)
4010 wxCHECK_MSG( !m_created
,
4012 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4014 m_numRows
= numRows
;
4015 m_numCols
= numCols
;
4017 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4018 m_table
->SetView( this );
4020 m_selection
= new wxGridSelection( this, selmode
);
4029 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4031 wxCHECK_RET( m_created
,
4032 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4034 m_selection
->SetSelectionMode( selmode
);
4037 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4039 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4040 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4042 return m_selection
->GetSelectionMode();
4045 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4046 wxGrid::wxGridSelectionModes selmode
)
4050 // stop all processing
4055 wxGridTableBase
*t
=m_table
;
4068 m_numRows
= table
->GetNumberRows();
4069 m_numCols
= table
->GetNumberCols();
4072 m_table
->SetView( this );
4075 m_selection
= new wxGridSelection( this, selmode
);
4088 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4089 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4091 if ( m_rowLabelWin
)
4093 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4097 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4100 m_labelTextColour
= wxColour( _T("BLACK") );
4103 m_attrCache
.row
= -1;
4104 m_attrCache
.col
= -1;
4105 m_attrCache
.attr
= NULL
;
4107 // TODO: something better than this ?
4109 m_labelFont
= this->GetFont();
4110 m_labelFont
.SetWeight( wxBOLD
);
4112 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4113 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4115 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4116 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4117 m_colLabelTextOrientation
= wxHORIZONTAL
;
4119 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4120 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4122 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4123 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4125 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4126 m_defaultRowHeight
+= 8;
4128 m_defaultRowHeight
+= 4;
4131 m_gridLineColour
= wxColour( 192,192,192 );
4132 m_gridLinesEnabled
= true;
4133 m_cellHighlightColour
= *wxBLACK
;
4134 m_cellHighlightPenWidth
= 2;
4135 m_cellHighlightROPenWidth
= 1;
4137 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4138 m_winCapture
= (wxWindow
*)NULL
;
4139 m_canDragRowSize
= true;
4140 m_canDragColSize
= true;
4141 m_canDragGridSize
= true;
4143 m_dragRowOrCol
= -1;
4144 m_isDragging
= false;
4145 m_startDragPos
= wxDefaultPosition
;
4147 m_waitForSlowClick
= false;
4149 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4150 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4152 m_currentCellCoords
= wxGridNoCellCoords
;
4154 m_selectingTopLeft
= wxGridNoCellCoords
;
4155 m_selectingBottomRight
= wxGridNoCellCoords
;
4156 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4157 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4159 m_editable
= true; // default for whole grid
4161 m_inOnKeyDown
= false;
4168 // ----------------------------------------------------------------------------
4169 // the idea is to call these functions only when necessary because they create
4170 // quite big arrays which eat memory mostly unnecessary - in particular, if
4171 // default widths/heights are used for all rows/columns, we may not use these
4174 // with some extra code, it should be possible to only store the
4175 // widths/heights different from default ones but this will be done later...
4176 // ----------------------------------------------------------------------------
4178 void wxGrid::InitRowHeights()
4180 m_rowHeights
.Empty();
4181 m_rowBottoms
.Empty();
4183 m_rowHeights
.Alloc( m_numRows
);
4184 m_rowBottoms
.Alloc( m_numRows
);
4188 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4190 for ( int i
= 0; i
< m_numRows
; i
++ )
4192 rowBottom
+= m_defaultRowHeight
;
4193 m_rowBottoms
.Add( rowBottom
);
4197 void wxGrid::InitColWidths()
4199 m_colWidths
.Empty();
4200 m_colRights
.Empty();
4202 m_colWidths
.Alloc( m_numCols
);
4203 m_colRights
.Alloc( m_numCols
);
4206 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4208 for ( int i
= 0; i
< m_numCols
; i
++ )
4210 colRight
+= m_defaultColWidth
;
4211 m_colRights
.Add( colRight
);
4215 int wxGrid::GetColWidth(int col
) const
4217 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4220 int wxGrid::GetColLeft(int col
) const
4222 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4223 : m_colRights
[col
] - m_colWidths
[col
];
4226 int wxGrid::GetColRight(int col
) const
4228 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4232 int wxGrid::GetRowHeight(int row
) const
4234 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4237 int wxGrid::GetRowTop(int row
) const
4239 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4240 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4243 int wxGrid::GetRowBottom(int row
) const
4245 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4246 : m_rowBottoms
[row
];
4249 void wxGrid::CalcDimensions()
4252 GetClientSize( &cw
, &ch
);
4254 if ( m_rowLabelWin
->IsShown() )
4255 cw
-= m_rowLabelWidth
;
4256 if ( m_colLabelWin
->IsShown() )
4257 ch
-= m_colLabelHeight
;
4260 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4261 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4263 // take into account editor if shown
4264 if( IsCellEditControlShown() )
4267 int r
= m_currentCellCoords
.GetRow();
4268 int c
= m_currentCellCoords
.GetCol();
4269 int x
= GetColLeft(c
);
4270 int y
= GetRowTop(r
);
4272 // how big is the editor
4273 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4274 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4275 editor
->GetControl()->GetSize(&w2
, &h2
);
4278 if( w2
> w
) w
= w2
;
4279 if( h2
> h
) h
= h2
;
4284 // preserve (more or less) the previous position
4286 GetViewStart( &x
, &y
);
4288 // ensure the position is valid for the new scroll ranges
4290 x
= wxMax( w
- 1, 0 );
4292 y
= wxMax( h
- 1, 0 );
4294 // do set scrollbar parameters
4295 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4296 GetScrollX(w
), GetScrollY(h
), x
, y
,
4297 GetBatchCount() != 0);
4299 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4300 // still must reposition the children
4305 void wxGrid::CalcWindowSizes()
4307 // escape if the window is has not been fully created yet
4309 if ( m_cornerLabelWin
== NULL
)
4313 GetClientSize( &cw
, &ch
);
4315 if ( m_cornerLabelWin
->IsShown() )
4316 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4318 if ( m_colLabelWin
->IsShown() )
4319 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4321 if ( m_rowLabelWin
->IsShown() )
4322 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4324 if ( m_gridWin
->IsShown() )
4325 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4329 // this is called when the grid table sends a message to say that it
4330 // has been redimensioned
4332 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4335 bool result
= false;
4337 // Clear the attribute cache as the attribute might refer to a different
4338 // cell than stored in the cache after adding/removing rows/columns.
4340 // By the same reasoning, the editor should be dismissed if columns are
4341 // added or removed. And for consistency, it should IMHO always be
4342 // removed, not only if the cell "underneath" it actually changes.
4343 // For now, I intentionally do not save the editor's content as the
4344 // cell it might want to save that stuff to might no longer exist.
4345 HideCellEditControl();
4347 // if we were using the default widths/heights so far, we must change them
4349 if ( m_colWidths
.IsEmpty() )
4354 if ( m_rowHeights
.IsEmpty() )
4360 switch ( msg
.GetId() )
4362 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4364 size_t pos
= msg
.GetCommandInt();
4365 int numRows
= msg
.GetCommandInt2();
4367 m_numRows
+= numRows
;
4369 if ( !m_rowHeights
.IsEmpty() )
4371 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4372 m_rowBottoms
.Insert( 0, pos
, numRows
);
4375 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4377 for ( i
= pos
; i
< m_numRows
; i
++ )
4379 bottom
+= m_rowHeights
[i
];
4380 m_rowBottoms
[i
] = bottom
;
4383 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4385 // if we have just inserted cols into an empty grid the current
4386 // cell will be undefined...
4388 SetCurrentCell( 0, 0 );
4392 m_selection
->UpdateRows( pos
, numRows
);
4393 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4395 attrProvider
->UpdateAttrRows( pos
, numRows
);
4397 if ( !GetBatchCount() )
4400 m_rowLabelWin
->Refresh();
4406 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4408 int numRows
= msg
.GetCommandInt();
4409 int oldNumRows
= m_numRows
;
4410 m_numRows
+= numRows
;
4412 if ( !m_rowHeights
.IsEmpty() )
4414 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4415 m_rowBottoms
.Add( 0, numRows
);
4418 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4420 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4422 bottom
+= m_rowHeights
[i
];
4423 m_rowBottoms
[i
] = bottom
;
4426 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4428 // if we have just inserted cols into an empty grid the current
4429 // cell will be undefined...
4431 SetCurrentCell( 0, 0 );
4433 if ( !GetBatchCount() )
4436 m_rowLabelWin
->Refresh();
4442 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4444 size_t pos
= msg
.GetCommandInt();
4445 int numRows
= msg
.GetCommandInt2();
4446 m_numRows
-= numRows
;
4448 if ( !m_rowHeights
.IsEmpty() )
4450 m_rowHeights
.RemoveAt( pos
, numRows
);
4451 m_rowBottoms
.RemoveAt( pos
, numRows
);
4454 for ( i
= 0; i
< m_numRows
; i
++ )
4456 h
+= m_rowHeights
[i
];
4457 m_rowBottoms
[i
] = h
;
4462 m_currentCellCoords
= wxGridNoCellCoords
;
4466 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4467 m_currentCellCoords
.Set( 0, 0 );
4471 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4472 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4474 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4475 // ifdef'd out following patch from Paul Gammans
4477 // No need to touch column attributes, unless we
4478 // removed _all_ rows, in this case, we remove
4479 // all column attributes.
4480 // I hate to do this here, but the
4481 // needed data is not available inside UpdateAttrRows.
4482 if ( !GetNumberRows() )
4483 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4486 if ( !GetBatchCount() )
4489 m_rowLabelWin
->Refresh();
4495 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4497 size_t pos
= msg
.GetCommandInt();
4498 int numCols
= msg
.GetCommandInt2();
4499 m_numCols
+= numCols
;
4501 if ( !m_colWidths
.IsEmpty() )
4503 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4504 m_colRights
.Insert( 0, pos
, numCols
);
4507 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4509 for ( i
= pos
; i
< m_numCols
; i
++ )
4511 right
+= m_colWidths
[i
];
4512 m_colRights
[i
] = right
;
4515 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4517 // if we have just inserted cols into an empty grid the current
4518 // cell will be undefined...
4520 SetCurrentCell( 0, 0 );
4524 m_selection
->UpdateCols( pos
, numCols
);
4525 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4527 attrProvider
->UpdateAttrCols( pos
, numCols
);
4528 if ( !GetBatchCount() )
4531 m_colLabelWin
->Refresh();
4538 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4540 int numCols
= msg
.GetCommandInt();
4541 int oldNumCols
= m_numCols
;
4542 m_numCols
+= numCols
;
4543 if ( !m_colWidths
.IsEmpty() )
4545 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4546 m_colRights
.Add( 0, numCols
);
4549 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4551 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4553 right
+= m_colWidths
[i
];
4554 m_colRights
[i
] = right
;
4557 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4559 // if we have just inserted cols into an empty grid the current
4560 // cell will be undefined...
4562 SetCurrentCell( 0, 0 );
4564 if ( !GetBatchCount() )
4567 m_colLabelWin
->Refresh();
4573 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4575 size_t pos
= msg
.GetCommandInt();
4576 int numCols
= msg
.GetCommandInt2();
4577 m_numCols
-= numCols
;
4579 if ( !m_colWidths
.IsEmpty() )
4581 m_colWidths
.RemoveAt( pos
, numCols
);
4582 m_colRights
.RemoveAt( pos
, numCols
);
4585 for ( i
= 0; i
< m_numCols
; i
++ )
4587 w
+= m_colWidths
[i
];
4593 m_currentCellCoords
= wxGridNoCellCoords
;
4597 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4598 m_currentCellCoords
.Set( 0, 0 );
4602 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4603 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4605 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4606 // ifdef'd out following patch from Paul Gammans
4608 // No need to touch row attributes, unless we
4609 // removed _all_ columns, in this case, we remove
4610 // all row attributes.
4611 // I hate to do this here, but the
4612 // needed data is not available inside UpdateAttrCols.
4613 if ( !GetNumberCols() )
4614 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4617 if ( !GetBatchCount() )
4620 m_colLabelWin
->Refresh();
4627 if (result
&& !GetBatchCount() )
4628 m_gridWin
->Refresh();
4633 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4635 wxRegionIterator
iter( reg
);
4638 wxArrayInt rowlabels
;
4645 // TODO: remove this when we can...
4646 // There is a bug in wxMotif that gives garbage update
4647 // rectangles if you jump-scroll a long way by clicking the
4648 // scrollbar with middle button. This is a work-around
4650 #if defined(__WXMOTIF__)
4652 m_gridWin
->GetClientSize( &cw
, &ch
);
4653 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4654 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4657 // logical bounds of update region
4660 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4661 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4663 // find the row labels within these bounds
4666 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4668 if ( GetRowBottom(row
) < top
)
4671 if ( GetRowTop(row
) > bottom
)
4674 rowlabels
.Add( row
);
4684 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4686 wxRegionIterator
iter( reg
);
4689 wxArrayInt colLabels
;
4696 // TODO: remove this when we can...
4697 // There is a bug in wxMotif that gives garbage update
4698 // rectangles if you jump-scroll a long way by clicking the
4699 // scrollbar with middle button. This is a work-around
4701 #if defined(__WXMOTIF__)
4703 m_gridWin
->GetClientSize( &cw
, &ch
);
4704 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4705 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4708 // logical bounds of update region
4711 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4712 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4714 // find the cells within these bounds
4717 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4719 if ( GetColRight(col
) < left
)
4722 if ( GetColLeft(col
) > right
)
4725 colLabels
.Add( col
);
4734 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4736 wxRegionIterator
iter( reg
);
4739 wxGridCellCoordsArray cellsExposed
;
4741 int left
, top
, right
, bottom
;
4746 // TODO: remove this when we can...
4747 // There is a bug in wxMotif that gives garbage update
4748 // rectangles if you jump-scroll a long way by clicking the
4749 // scrollbar with middle button. This is a work-around
4751 #if defined(__WXMOTIF__)
4753 m_gridWin
->GetClientSize( &cw
, &ch
);
4754 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4755 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4756 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4757 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4760 // logical bounds of update region
4762 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4763 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4765 // find the cells within these bounds
4768 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4770 if ( GetRowBottom(row
) <= top
)
4773 if ( GetRowTop(row
) > bottom
)
4776 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4778 if ( GetColRight(col
) <= left
)
4781 if ( GetColLeft(col
) > right
)
4784 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4791 return cellsExposed
;
4795 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4798 wxPoint
pos( event
.GetPosition() );
4799 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4801 if ( event
.Dragging() )
4805 m_isDragging
= true;
4806 m_rowLabelWin
->CaptureMouse();
4809 if ( event
.LeftIsDown() )
4811 switch( m_cursorMode
)
4813 case WXGRID_CURSOR_RESIZE_ROW
:
4815 int cw
, ch
, left
, dummy
;
4816 m_gridWin
->GetClientSize( &cw
, &ch
);
4817 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4819 wxClientDC
dc( m_gridWin
);
4822 GetRowTop(m_dragRowOrCol
) +
4823 GetRowMinimalHeight(m_dragRowOrCol
) );
4824 dc
.SetLogicalFunction(wxINVERT
);
4825 if ( m_dragLastPos
>= 0 )
4827 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4829 dc
.DrawLine( left
, y
, left
+cw
, y
);
4834 case WXGRID_CURSOR_SELECT_ROW
:
4835 if ( (row
= YToRow( y
)) >= 0 )
4839 m_selection
->SelectRow( row
,
4840 event
.ControlDown(),
4847 // default label to suppress warnings about "enumeration value
4848 // 'xxx' not handled in switch
4856 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4861 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4862 m_isDragging
= false;
4865 // ------------ Entering or leaving the window
4867 if ( event
.Entering() || event
.Leaving() )
4869 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4873 // ------------ Left button pressed
4875 else if ( event
.LeftDown() )
4877 // don't send a label click event for a hit on the
4878 // edge of the row label - this is probably the user
4879 // wanting to resize the row
4881 if ( YToEdgeOfRow(y
) < 0 )
4885 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4887 if ( !event
.ShiftDown() && !event
.ControlDown() )
4891 if ( event
.ShiftDown() )
4893 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4896 GetNumberCols() - 1,
4897 event
.ControlDown(),
4904 m_selection
->SelectRow( row
,
4905 event
.ControlDown(),
4912 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4917 // starting to drag-resize a row
4919 if ( CanDragRowSize() )
4920 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4925 // ------------ Left double click
4927 else if (event
.LeftDClick() )
4929 int row
= YToEdgeOfRow(y
);
4934 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4936 // no default action at the moment
4941 // adjust row height depending on label text
4942 AutoSizeRowLabelSize( row
);
4944 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4950 // ------------ Left button released
4952 else if ( event
.LeftUp() )
4954 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4956 DoEndDragResizeRow();
4958 // Note: we are ending the event *after* doing
4959 // default processing in this case
4961 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4964 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4969 // ------------ Right button down
4971 else if ( event
.RightDown() )
4975 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4977 // no default action at the moment
4982 // ------------ Right double click
4984 else if ( event
.RightDClick() )
4988 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4990 // no default action at the moment
4995 // ------------ No buttons down and mouse moving
4997 else if ( event
.Moving() )
4999 m_dragRowOrCol
= YToEdgeOfRow( y
);
5000 if ( m_dragRowOrCol
>= 0 )
5002 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5004 // don't capture the mouse yet
5005 if ( CanDragRowSize() )
5006 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5009 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5011 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5017 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5020 wxPoint
pos( event
.GetPosition() );
5021 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5023 if ( event
.Dragging() )
5027 m_isDragging
= true;
5028 m_colLabelWin
->CaptureMouse();
5031 if ( event
.LeftIsDown() )
5033 switch( m_cursorMode
)
5035 case WXGRID_CURSOR_RESIZE_COL
:
5037 int cw
, ch
, dummy
, top
;
5038 m_gridWin
->GetClientSize( &cw
, &ch
);
5039 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5041 wxClientDC
dc( m_gridWin
);
5044 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5045 GetColMinimalWidth(m_dragRowOrCol
));
5046 dc
.SetLogicalFunction(wxINVERT
);
5047 if ( m_dragLastPos
>= 0 )
5049 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5051 dc
.DrawLine( x
, top
, x
, top
+ch
);
5056 case WXGRID_CURSOR_SELECT_COL
:
5057 if ( (col
= XToCol( x
)) >= 0 )
5061 m_selection
->SelectCol( col
,
5062 event
.ControlDown(),
5069 // default label to suppress warnings about "enumeration value
5070 // 'xxx' not handled in switch
5078 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5083 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5084 m_isDragging
= false;
5087 // ------------ Entering or leaving the window
5089 if ( event
.Entering() || event
.Leaving() )
5091 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5095 // ------------ Left button pressed
5097 else if ( event
.LeftDown() )
5099 // don't send a label click event for a hit on the
5100 // edge of the col label - this is probably the user
5101 // wanting to resize the col
5103 if ( XToEdgeOfCol(x
) < 0 )
5107 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5109 if ( !event
.ShiftDown() && !event
.ControlDown() )
5113 if ( event
.ShiftDown() )
5115 m_selection
->SelectBlock( 0,
5116 m_currentCellCoords
.GetCol(),
5117 GetNumberRows() - 1, col
,
5118 event
.ControlDown(),
5125 m_selection
->SelectCol( col
,
5126 event
.ControlDown(),
5133 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5138 // starting to drag-resize a col
5140 if ( CanDragColSize() )
5141 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5146 // ------------ Left double click
5148 if ( event
.LeftDClick() )
5150 int col
= XToEdgeOfCol(x
);
5155 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5157 // no default action at the moment
5162 // adjust column width depending on label text
5163 AutoSizeColLabelSize( col
);
5165 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5171 // ------------ Left button released
5173 else if ( event
.LeftUp() )
5175 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5177 DoEndDragResizeCol();
5179 // Note: we are ending the event *after* doing
5180 // default processing in this case
5182 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5185 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5190 // ------------ Right button down
5192 else if ( event
.RightDown() )
5196 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5198 // no default action at the moment
5203 // ------------ Right double click
5205 else if ( event
.RightDClick() )
5209 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5211 // no default action at the moment
5216 // ------------ No buttons down and mouse moving
5218 else if ( event
.Moving() )
5220 m_dragRowOrCol
= XToEdgeOfCol( x
);
5221 if ( m_dragRowOrCol
>= 0 )
5223 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5225 // don't capture the cursor yet
5226 if ( CanDragColSize() )
5227 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5230 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5232 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5238 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5240 if ( event
.LeftDown() )
5242 // indicate corner label by having both row and
5245 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5251 else if ( event
.LeftDClick() )
5253 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5256 else if ( event
.RightDown() )
5258 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5260 // no default action at the moment
5264 else if ( event
.RightDClick() )
5266 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5268 // no default action at the moment
5273 void wxGrid::ChangeCursorMode(CursorMode mode
,
5278 static const wxChar
*cursorModes
[] =
5287 wxLogTrace(_T("grid"),
5288 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5289 win
== m_colLabelWin
? _T("colLabelWin")
5290 : win
? _T("rowLabelWin")
5292 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5293 #endif // __WXDEBUG__
5295 if ( mode
== m_cursorMode
&&
5296 win
== m_winCapture
&&
5297 captureMouse
== (m_winCapture
!= NULL
))
5302 // by default use the grid itself
5308 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5309 m_winCapture
= (wxWindow
*)NULL
;
5312 m_cursorMode
= mode
;
5314 switch ( m_cursorMode
)
5316 case WXGRID_CURSOR_RESIZE_ROW
:
5317 win
->SetCursor( m_rowResizeCursor
);
5320 case WXGRID_CURSOR_RESIZE_COL
:
5321 win
->SetCursor( m_colResizeCursor
);
5325 win
->SetCursor( *wxSTANDARD_CURSOR
);
5328 // we need to capture mouse when resizing
5329 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5330 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5332 if ( captureMouse
&& resize
)
5334 win
->CaptureMouse();
5339 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5342 wxPoint
pos( event
.GetPosition() );
5343 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5345 wxGridCellCoords coords
;
5346 XYToCell( x
, y
, coords
);
5348 int cell_rows
, cell_cols
;
5349 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5350 if ((cell_rows
< 0) || (cell_cols
< 0))
5352 coords
.SetRow(coords
.GetRow() + cell_rows
);
5353 coords
.SetCol(coords
.GetCol() + cell_cols
);
5356 if ( event
.Dragging() )
5358 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5360 // Don't start doing anything until the mouse has been drug at
5361 // least 3 pixels in any direction...
5364 if (m_startDragPos
== wxDefaultPosition
)
5366 m_startDragPos
= pos
;
5369 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5373 m_isDragging
= true;
5374 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5376 // Hide the edit control, so it
5377 // won't interfer with drag-shrinking.
5378 if ( IsCellEditControlShown() )
5380 HideCellEditControl();
5381 SaveEditControlValue();
5384 // Have we captured the mouse yet?
5387 m_winCapture
= m_gridWin
;
5388 m_winCapture
->CaptureMouse();
5391 if ( coords
!= wxGridNoCellCoords
)
5393 if ( event
.ControlDown() )
5395 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5396 m_selectingKeyboard
= coords
;
5397 HighlightBlock ( m_selectingKeyboard
, coords
);
5401 if ( !IsSelection() )
5403 HighlightBlock( coords
, coords
);
5407 HighlightBlock( m_currentCellCoords
, coords
);
5411 if (! IsVisible(coords
))
5413 MakeCellVisible(coords
);
5414 // TODO: need to introduce a delay or something here. The
5415 // scrolling is way to fast, at least on MSW - also on GTK.
5419 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5421 int cw
, ch
, left
, dummy
;
5422 m_gridWin
->GetClientSize( &cw
, &ch
);
5423 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5425 wxClientDC
dc( m_gridWin
);
5427 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5428 GetRowMinimalHeight(m_dragRowOrCol
) );
5429 dc
.SetLogicalFunction(wxINVERT
);
5430 if ( m_dragLastPos
>= 0 )
5432 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5434 dc
.DrawLine( left
, y
, left
+cw
, y
);
5437 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5439 int cw
, ch
, dummy
, top
;
5440 m_gridWin
->GetClientSize( &cw
, &ch
);
5441 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5443 wxClientDC
dc( m_gridWin
);
5445 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5446 GetColMinimalWidth(m_dragRowOrCol
) );
5447 dc
.SetLogicalFunction(wxINVERT
);
5448 if ( m_dragLastPos
>= 0 )
5450 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5452 dc
.DrawLine( x
, top
, x
, top
+ch
);
5459 m_isDragging
= false;
5460 m_startDragPos
= wxDefaultPosition
;
5462 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5463 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5466 if ( event
.Entering() || event
.Leaving() )
5468 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5469 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5474 // ------------ Left button pressed
5476 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5478 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5483 if ( !event
.ControlDown() )
5485 if ( event
.ShiftDown() )
5489 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5490 m_currentCellCoords
.GetCol(),
5493 event
.ControlDown(),
5499 else if ( XToEdgeOfCol(x
) < 0 &&
5500 YToEdgeOfRow(y
) < 0 )
5502 DisableCellEditControl();
5503 MakeCellVisible( coords
);
5505 if ( event
.ControlDown() )
5509 m_selection
->ToggleCellSelection( coords
.GetRow(),
5511 event
.ControlDown(),
5516 m_selectingTopLeft
= wxGridNoCellCoords
;
5517 m_selectingBottomRight
= wxGridNoCellCoords
;
5518 m_selectingKeyboard
= coords
;
5522 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5523 SetCurrentCell( coords
);
5526 if ( m_selection
->GetSelectionMode() !=
5527 wxGrid::wxGridSelectCells
)
5529 HighlightBlock( coords
, coords
);
5538 // ------------ Left double click
5540 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5542 DisableCellEditControl();
5544 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5546 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5554 // ------------ Left button released
5556 else if ( event
.LeftUp() )
5558 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5562 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5563 m_winCapture
= NULL
;
5566 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5569 EnableCellEditControl();
5571 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5572 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5573 editor
->StartingClick();
5577 m_waitForSlowClick
= false;
5579 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5580 m_selectingBottomRight
!= wxGridNoCellCoords
)
5584 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5585 m_selectingTopLeft
.GetCol(),
5586 m_selectingBottomRight
.GetRow(),
5587 m_selectingBottomRight
.GetCol(),
5588 event
.ControlDown(),
5594 m_selectingTopLeft
= wxGridNoCellCoords
;
5595 m_selectingBottomRight
= wxGridNoCellCoords
;
5597 // Show the edit control, if it has been hidden for
5599 ShowCellEditControl();
5602 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5604 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5605 DoEndDragResizeRow();
5607 // Note: we are ending the event *after* doing
5608 // default processing in this case
5610 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5612 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5614 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5615 DoEndDragResizeCol();
5617 // Note: we are ending the event *after* doing
5618 // default processing in this case
5620 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5627 // ------------ Right button down
5629 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5631 DisableCellEditControl();
5632 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5637 // no default action at the moment
5642 // ------------ Right double click
5644 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5646 DisableCellEditControl();
5647 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5652 // no default action at the moment
5656 // ------------ Moving and no button action
5658 else if ( event
.Moving() && !event
.IsButton() )
5660 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5662 // out of grid cell area
5663 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5667 int dragRow
= YToEdgeOfRow( y
);
5668 int dragCol
= XToEdgeOfCol( x
);
5670 // Dragging on the corner of a cell to resize in both
5671 // directions is not implemented yet...
5673 if ( dragRow
>= 0 && dragCol
>= 0 )
5675 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5681 m_dragRowOrCol
= dragRow
;
5683 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5685 if ( CanDragRowSize() && CanDragGridSize() )
5686 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5691 m_dragRowOrCol
= dragCol
;
5699 m_dragRowOrCol
= dragCol
;
5701 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5703 if ( CanDragColSize() && CanDragGridSize() )
5704 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5710 // Neither on a row or col edge
5712 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5714 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5720 void wxGrid::DoEndDragResizeRow()
5722 if ( m_dragLastPos
>= 0 )
5724 // erase the last line and resize the row
5726 int cw
, ch
, left
, dummy
;
5727 m_gridWin
->GetClientSize( &cw
, &ch
);
5728 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5730 wxClientDC
dc( m_gridWin
);
5732 dc
.SetLogicalFunction( wxINVERT
);
5733 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5734 HideCellEditControl();
5735 SaveEditControlValue();
5737 int rowTop
= GetRowTop(m_dragRowOrCol
);
5738 SetRowSize( m_dragRowOrCol
,
5739 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5741 if ( !GetBatchCount() )
5743 // Only needed to get the correct rect.y:
5744 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5746 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5747 rect
.width
= m_rowLabelWidth
;
5748 rect
.height
= ch
- rect
.y
;
5749 m_rowLabelWin
->Refresh( true, &rect
);
5751 // if there is a multicell block, paint all of it
5754 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5755 int leftCol
= XToCol(left
);
5756 int rightCol
= internalXToCol(left
+cw
);
5759 for (i
=leftCol
; i
<rightCol
; i
++)
5761 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5762 if (cell_rows
< subtract_rows
)
5763 subtract_rows
= cell_rows
;
5765 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5766 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5767 rect
.height
= ch
- rect
.y
;
5770 m_gridWin
->Refresh( false, &rect
);
5773 ShowCellEditControl();
5778 void wxGrid::DoEndDragResizeCol()
5780 if ( m_dragLastPos
>= 0 )
5782 // erase the last line and resize the col
5784 int cw
, ch
, dummy
, top
;
5785 m_gridWin
->GetClientSize( &cw
, &ch
);
5786 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5788 wxClientDC
dc( m_gridWin
);
5790 dc
.SetLogicalFunction( wxINVERT
);
5791 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5792 HideCellEditControl();
5793 SaveEditControlValue();
5795 int colLeft
= GetColLeft(m_dragRowOrCol
);
5796 SetColSize( m_dragRowOrCol
,
5797 wxMax( m_dragLastPos
- colLeft
,
5798 GetColMinimalWidth(m_dragRowOrCol
) ) );
5800 if ( !GetBatchCount() )
5802 // Only needed to get the correct rect.x:
5803 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5805 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5806 rect
.width
= cw
- rect
.x
;
5807 rect
.height
= m_colLabelHeight
;
5808 m_colLabelWin
->Refresh( true, &rect
);
5810 // if there is a multicell block, paint all of it
5813 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5814 int topRow
= YToRow(top
);
5815 int bottomRow
= internalYToRow(top
+cw
);
5818 for (i
=topRow
; i
<bottomRow
; i
++)
5820 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5821 if (cell_cols
< subtract_cols
)
5822 subtract_cols
= cell_cols
;
5824 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5825 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5826 rect
.width
= cw
- rect
.x
;
5829 m_gridWin
->Refresh( false, &rect
);
5832 ShowCellEditControl();
5839 // ------ interaction with data model
5841 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5843 switch ( msg
.GetId() )
5845 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5846 return GetModelValues();
5848 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5849 return SetModelValues();
5851 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5852 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5853 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5854 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5855 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5856 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5857 return Redimension( msg
);
5866 // The behaviour of this function depends on the grid table class
5867 // Clear() function. For the default wxGridStringTable class the
5868 // behavious is to replace all cell contents with wxEmptyString but
5869 // not to change the number of rows or cols.
5871 void wxGrid::ClearGrid()
5875 if (IsCellEditControlEnabled())
5876 DisableCellEditControl();
5879 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5884 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5886 // TODO: something with updateLabels flag
5890 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5896 if (IsCellEditControlEnabled())
5897 DisableCellEditControl();
5899 bool done
= m_table
->InsertRows( pos
, numRows
);
5902 // the table will have sent the results of the insert row
5903 // operation to this view object as a grid table message
5909 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5911 // TODO: something with updateLabels flag
5915 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5921 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5923 // the table will have sent the results of the append row
5924 // operation to this view object as a grid table message
5930 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5932 // TODO: something with updateLabels flag
5936 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5942 if (IsCellEditControlEnabled())
5943 DisableCellEditControl();
5945 bool done
= m_table
->DeleteRows( pos
, numRows
);
5947 // the table will have sent the results of the delete row
5948 // operation to this view object as a grid table message
5954 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5956 // TODO: something with updateLabels flag
5960 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5966 if (IsCellEditControlEnabled())
5967 DisableCellEditControl();
5969 bool done
= m_table
->InsertCols( pos
, numCols
);
5971 // the table will have sent the results of the insert col
5972 // operation to this view object as a grid table message
5978 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5980 // TODO: something with updateLabels flag
5984 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5990 bool done
= m_table
->AppendCols( numCols
);
5992 // the table will have sent the results of the append col
5993 // operation to this view object as a grid table message
5999 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6001 // TODO: something with updateLabels flag
6005 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6011 if (IsCellEditControlEnabled())
6012 DisableCellEditControl();
6014 bool done
= m_table
->DeleteCols( pos
, numCols
);
6016 // the table will have sent the results of the delete col
6017 // operation to this view object as a grid table message
6025 // ----- event handlers
6028 // Generate a grid event based on a mouse event and
6029 // return the result of ProcessEvent()
6031 int wxGrid::SendEvent( const wxEventType type
,
6033 wxMouseEvent
& mouseEv
)
6038 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6040 int rowOrCol
= (row
== -1 ? col
: row
);
6042 wxGridSizeEvent
gridEvt( GetId(),
6046 mouseEv
.GetX() + GetRowLabelSize(),
6047 mouseEv
.GetY() + GetColLabelSize(),
6048 mouseEv
.ControlDown(),
6049 mouseEv
.ShiftDown(),
6051 mouseEv
.MetaDown() );
6053 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6054 vetoed
= !gridEvt
.IsAllowed();
6056 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6058 // Right now, it should _never_ end up here!
6059 wxGridRangeSelectEvent
gridEvt( GetId(),
6063 m_selectingBottomRight
,
6065 mouseEv
.ControlDown(),
6066 mouseEv
.ShiftDown(),
6068 mouseEv
.MetaDown() );
6070 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6071 vetoed
= !gridEvt
.IsAllowed();
6075 wxGridEvent
gridEvt( GetId(),
6079 mouseEv
.GetX() + GetRowLabelSize(),
6080 mouseEv
.GetY() + GetColLabelSize(),
6082 mouseEv
.ControlDown(),
6083 mouseEv
.ShiftDown(),
6085 mouseEv
.MetaDown() );
6086 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6087 vetoed
= !gridEvt
.IsAllowed();
6090 // A Veto'd event may not be `claimed' so test this first
6091 if (vetoed
) return -1;
6092 return claimed
? 1 : 0;
6096 // Generate a grid event of specified type and return the result
6097 // of ProcessEvent().
6099 int wxGrid::SendEvent( const wxEventType type
,
6105 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6107 int rowOrCol
= (row
== -1 ? col
: row
);
6109 wxGridSizeEvent
gridEvt( GetId(),
6114 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6115 vetoed
= !gridEvt
.IsAllowed();
6119 wxGridEvent
gridEvt( GetId(),
6124 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6125 vetoed
= !gridEvt
.IsAllowed();
6128 // A Veto'd event may not be `claimed' so test this first
6129 if (vetoed
) return -1;
6130 return claimed
? 1 : 0;
6134 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6136 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6139 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6141 // Don't do anything if between Begin/EndBatch...
6142 // EndBatch() will do all this on the last nested one anyway.
6143 if (! GetBatchCount())
6145 // Refresh to get correct scrolled position:
6146 wxScrolledWindow::Refresh(eraseb
,rect
);
6150 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6151 int width_label
, width_cell
, height_label
, height_cell
;
6154 //Copy rectangle can get scroll offsets..
6155 rect_x
= rect
->GetX();
6156 rect_y
= rect
->GetY();
6157 rectWidth
= rect
->GetWidth();
6158 rectHeight
= rect
->GetHeight();
6160 width_label
= m_rowLabelWidth
- rect_x
;
6161 if (width_label
> rectWidth
) width_label
= rectWidth
;
6163 height_label
= m_colLabelHeight
- rect_y
;
6164 if (height_label
> rectHeight
) height_label
= rectHeight
;
6166 if (rect_x
> m_rowLabelWidth
)
6168 x
= rect_x
- m_rowLabelWidth
;
6169 width_cell
= rectWidth
;
6174 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6177 if (rect_y
> m_colLabelHeight
)
6179 y
= rect_y
- m_colLabelHeight
;
6180 height_cell
= rectHeight
;
6185 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6188 // Paint corner label part intersecting rect.
6189 if ( width_label
> 0 && height_label
> 0 )
6191 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6192 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6195 // Paint col labels part intersecting rect.
6196 if ( width_cell
> 0 && height_label
> 0 )
6198 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6199 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6202 // Paint row labels part intersecting rect.
6203 if ( width_label
> 0 && height_cell
> 0 )
6205 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6206 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6209 // Paint cell area part intersecting rect.
6210 if ( width_cell
> 0 && height_cell
> 0 )
6212 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6213 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6218 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6219 m_colLabelWin
->Refresh(eraseb
, NULL
);
6220 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6221 m_gridWin
->Refresh(eraseb
, NULL
);
6226 void wxGrid::OnSize( wxSizeEvent
& event
)
6228 // position the child windows
6231 // don't call CalcDimensions() from here, the base class handles the size
6237 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6239 if ( m_inOnKeyDown
)
6241 // shouldn't be here - we are going round in circles...
6243 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6246 m_inOnKeyDown
= true;
6248 // propagate the event up and see if it gets processed
6250 wxWindow
*parent
= GetParent();
6251 wxKeyEvent
keyEvt( event
);
6252 keyEvt
.SetEventObject( parent
);
6254 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6257 // try local handlers
6259 switch ( event
.GetKeyCode() )
6262 if ( event
.ControlDown() )
6264 MoveCursorUpBlock( event
.ShiftDown() );
6268 MoveCursorUp( event
.ShiftDown() );
6273 if ( event
.ControlDown() )
6275 MoveCursorDownBlock( event
.ShiftDown() );
6279 MoveCursorDown( event
.ShiftDown() );
6284 if ( event
.ControlDown() )
6286 MoveCursorLeftBlock( event
.ShiftDown() );
6290 MoveCursorLeft( event
.ShiftDown() );
6295 if ( event
.ControlDown() )
6297 MoveCursorRightBlock( event
.ShiftDown() );
6301 MoveCursorRight( event
.ShiftDown() );
6306 case WXK_NUMPAD_ENTER
:
6307 if ( event
.ControlDown() )
6309 event
.Skip(); // to let the edit control have the return
6313 if ( GetGridCursorRow() < GetNumberRows()-1 )
6315 MoveCursorDown( event
.ShiftDown() );
6319 // at the bottom of a column
6320 DisableCellEditControl();
6330 if (event
.ShiftDown())
6332 if ( GetGridCursorCol() > 0 )
6334 MoveCursorLeft( false );
6339 DisableCellEditControl();
6344 if ( GetGridCursorCol() < GetNumberCols()-1 )
6346 MoveCursorRight( false );
6351 DisableCellEditControl();
6357 if ( event
.ControlDown() )
6359 MakeCellVisible( 0, 0 );
6360 SetCurrentCell( 0, 0 );
6369 if ( event
.ControlDown() )
6371 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6372 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6389 if ( event
.ControlDown() )
6393 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6394 m_currentCellCoords
.GetCol(),
6395 event
.ControlDown(),
6402 if ( !IsEditable() )
6404 MoveCursorRight( false );
6407 // Otherwise fall through to default
6410 // is it possible to edit the current cell at all?
6411 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6413 // yes, now check whether the cells editor accepts the key
6414 int row
= m_currentCellCoords
.GetRow();
6415 int col
= m_currentCellCoords
.GetCol();
6416 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6417 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6419 // <F2> is special and will always start editing, for
6420 // other keys - ask the editor itself
6421 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6422 || editor
->IsAcceptedKey(event
) )
6424 // ensure cell is visble
6425 MakeCellVisible(row
, col
);
6426 EnableCellEditControl();
6428 // a problem can arise if the cell is not completely
6429 // visible (even after calling MakeCellVisible the
6430 // control is not created and calling StartingKey will
6432 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6444 // let others process char events with modifiers or all
6445 // char events for readonly cells
6452 m_inOnKeyDown
= false;
6455 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6457 // try local handlers
6459 if ( event
.GetKeyCode() == WXK_SHIFT
)
6461 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6462 m_selectingBottomRight
!= wxGridNoCellCoords
)
6466 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6467 m_selectingTopLeft
.GetCol(),
6468 m_selectingBottomRight
.GetRow(),
6469 m_selectingBottomRight
.GetCol(),
6470 event
.ControlDown(),
6477 m_selectingTopLeft
= wxGridNoCellCoords
;
6478 m_selectingBottomRight
= wxGridNoCellCoords
;
6479 m_selectingKeyboard
= wxGridNoCellCoords
;
6483 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6487 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6489 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6491 // the event has been intercepted - do nothing
6495 wxClientDC
dc(m_gridWin
);
6498 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6500 DisableCellEditControl();
6502 if ( IsVisible( m_currentCellCoords
, false ) )
6505 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6506 if ( !m_gridLinesEnabled
)
6514 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6516 // Otherwise refresh redraws the highlight!
6517 m_currentCellCoords
= coords
;
6519 DrawGridCellArea(dc
,cells
);
6520 DrawAllGridLines( dc
, r
);
6524 m_currentCellCoords
= coords
;
6526 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6527 DrawCellHighlight(dc
, attr
);
6532 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6535 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6539 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6542 rightCol
= GetNumberCols() - 1;
6544 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6547 bottomRow
= GetNumberRows() - 1;
6551 if ( topRow
> bottomRow
)
6558 if ( leftCol
> rightCol
)
6565 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6566 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6568 // First the case that we selected a completely new area
6569 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6570 m_selectingBottomRight
== wxGridNoCellCoords
)
6573 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6574 wxGridCellCoords ( bottomRow
, rightCol
) );
6575 m_gridWin
->Refresh( false, &rect
);
6577 // Now handle changing an existing selection area.
6578 else if ( m_selectingTopLeft
!= updateTopLeft
||
6579 m_selectingBottomRight
!= updateBottomRight
)
6581 // Compute two optimal update rectangles:
6582 // Either one rectangle is a real subset of the
6583 // other, or they are (almost) disjoint!
6585 bool need_refresh
[4];
6589 need_refresh
[3] = false;
6592 // Store intermediate values
6593 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6594 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6595 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6596 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6598 // Determine the outer/inner coordinates.
6599 if (oldLeft
> leftCol
)
6605 if (oldTop
> topRow
)
6611 if (oldRight
< rightCol
)
6614 oldRight
= rightCol
;
6617 if (oldBottom
< bottomRow
)
6620 oldBottom
= bottomRow
;
6624 // Now, either the stuff marked old is the outer
6625 // rectangle or we don't have a situation where one
6626 // is contained in the other.
6628 if ( oldLeft
< leftCol
)
6630 // Refresh the newly selected or deselected
6631 // area to the left of the old or new selection.
6632 need_refresh
[0] = true;
6633 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6635 wxGridCellCoords ( oldBottom
,
6639 if ( oldTop
< topRow
)
6641 // Refresh the newly selected or deselected
6642 // area above the old or new selection.
6643 need_refresh
[1] = true;
6644 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6646 wxGridCellCoords ( topRow
- 1,
6650 if ( oldRight
> rightCol
)
6652 // Refresh the newly selected or deselected
6653 // area to the right of the old or new selection.
6654 need_refresh
[2] = true;
6655 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6657 wxGridCellCoords ( oldBottom
,
6661 if ( oldBottom
> bottomRow
)
6663 // Refresh the newly selected or deselected
6664 // area below the old or new selection.
6665 need_refresh
[3] = true;
6666 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6668 wxGridCellCoords ( oldBottom
,
6672 // various Refresh() calls
6673 for (i
= 0; i
< 4; i
++ )
6674 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6675 m_gridWin
->Refresh( false, &(rect
[i
]) );
6678 m_selectingTopLeft
= updateTopLeft
;
6679 m_selectingBottomRight
= updateBottomRight
;
6683 // ------ functions to get/send data (see also public functions)
6686 bool wxGrid::GetModelValues()
6688 // Hide the editor, so it won't hide a changed value.
6689 HideCellEditControl();
6693 // all we need to do is repaint the grid
6695 m_gridWin
->Refresh();
6703 bool wxGrid::SetModelValues()
6707 // Disable the editor, so it won't hide a changed value.
6708 // Do we also want to save the current value of the editor first?
6710 DisableCellEditControl();
6714 for ( row
= 0; row
< m_numRows
; row
++ )
6716 for ( col
= 0; col
< m_numCols
; col
++ )
6718 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6730 // Note - this function only draws cells that are in the list of
6731 // exposed cells (usually set from the update region by
6732 // CalcExposedCells)
6734 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6736 if ( !m_numRows
|| !m_numCols
) return;
6738 int i
, numCells
= cells
.GetCount();
6739 int row
, col
, cell_rows
, cell_cols
;
6740 wxGridCellCoordsArray redrawCells
;
6742 for ( i
= numCells
-1; i
>= 0; i
-- )
6744 row
= cells
[i
].GetRow();
6745 col
= cells
[i
].GetCol();
6746 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6748 // If this cell is part of a multicell block, find owner for repaint
6749 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6751 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6752 bool marked
= false;
6753 for ( int j
= 0; j
< numCells
; j
++ )
6755 if ( cell
== cells
[j
] )
6763 int count
= redrawCells
.GetCount();
6764 for (int j
= 0; j
< count
; j
++)
6766 if ( cell
== redrawCells
[j
] )
6772 if (!marked
) redrawCells
.Add( cell
);
6774 continue; // don't bother drawing this cell
6777 // If this cell is empty, find cell to left that might want to overflow
6778 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6780 for ( int l
= 0; l
< cell_rows
; l
++ )
6782 // find a cell in this row to left alreay marked for repaint
6784 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6785 if ((redrawCells
[k
].GetCol() < left
) &&
6786 (redrawCells
[k
].GetRow() == row
))
6787 left
=redrawCells
[k
].GetCol();
6789 if (left
== col
) left
= 0; // oh well
6791 for (int j
= col
-1; j
>= left
; j
--)
6793 if (!m_table
->IsEmptyCell(row
+l
, j
))
6795 if (GetCellOverflow(row
+l
, j
))
6797 wxGridCellCoords
cell(row
+l
, j
);
6798 bool marked
= false;
6800 for (int k
= 0; k
< numCells
; k
++)
6802 if ( cell
== cells
[k
] )
6810 int count
= redrawCells
.GetCount();
6811 for (int k
= 0; k
< count
; k
++)
6813 if ( cell
== redrawCells
[k
] )
6819 if (!marked
) redrawCells
.Add( cell
);
6827 DrawCell( dc
, cells
[i
] );
6830 numCells
= redrawCells
.GetCount();
6832 for ( i
= numCells
- 1; i
>= 0; i
-- )
6834 DrawCell( dc
, redrawCells
[i
] );
6839 void wxGrid::DrawGridSpace( wxDC
& dc
)
6842 m_gridWin
->GetClientSize( &cw
, &ch
);
6845 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6847 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6848 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6850 if ( right
> rightCol
|| bottom
> bottomRow
)
6853 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6855 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6856 dc
.SetPen( *wxTRANSPARENT_PEN
);
6858 if ( right
> rightCol
)
6860 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6863 if ( bottom
> bottomRow
)
6865 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6871 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6873 int row
= coords
.GetRow();
6874 int col
= coords
.GetCol();
6876 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6879 // we draw the cell border ourselves
6880 #if !WXGRID_DRAW_LINES
6881 if ( m_gridLinesEnabled
)
6882 DrawCellBorder( dc
, coords
);
6885 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6887 bool isCurrent
= coords
== m_currentCellCoords
;
6889 wxRect rect
= CellToRect( row
, col
);
6891 // if the editor is shown, we should use it and not the renderer
6892 // Note: However, only if it is really _shown_, i.e. not hidden!
6893 if ( isCurrent
&& IsCellEditControlShown() )
6895 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6896 editor
->PaintBackground(rect
, attr
);
6901 // but all the rest is drawn by the cell renderer and hence may be
6903 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6904 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6911 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6913 int row
= m_currentCellCoords
.GetRow();
6914 int col
= m_currentCellCoords
.GetCol();
6916 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6919 wxRect rect
= CellToRect(row
, col
);
6921 // hmmm... what could we do here to show that the cell is disabled?
6922 // for now, I just draw a thinner border than for the other ones, but
6923 // it doesn't look really good
6925 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6929 // The center of th drawn line is where the position/width/height of
6930 // the rectangle is actually at, (on wxMSW atr least,) so we will
6931 // reduce the size of the rectangle to compensate for the thickness of
6932 // the line. If this is too strange on non wxMSW platforms then
6933 // please #ifdef this appropriately.
6934 rect
.x
+= penWidth
/2;
6935 rect
.y
+= penWidth
/2;
6936 rect
.width
-= penWidth
-1;
6937 rect
.height
-= penWidth
-1;
6940 // Now draw the rectangle
6941 // use the cellHighlightColour if the cell is inside a selection, this
6942 // will ensure the cell is always visible.
6943 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6944 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6945 dc
.DrawRectangle(rect
);
6949 // VZ: my experiments with 3d borders...
6951 // how to properly set colours for arbitrary bg?
6952 wxCoord x1
= rect
.x
,
6954 x2
= rect
.x
+ rect
.width
-1,
6955 y2
= rect
.y
+ rect
.height
-1;
6957 dc
.SetPen(*wxWHITE_PEN
);
6958 dc
.DrawLine(x1
, y1
, x2
, y1
);
6959 dc
.DrawLine(x1
, y1
, x1
, y2
);
6961 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6962 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6964 dc
.SetPen(*wxBLACK_PEN
);
6965 dc
.DrawLine(x1
, y2
, x2
, y2
);
6966 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6971 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6973 int row
= coords
.GetRow();
6974 int col
= coords
.GetCol();
6975 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6978 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6980 wxRect rect
= CellToRect( row
, col
);
6982 // right hand border
6984 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6985 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6989 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6990 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6993 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6995 // This if block was previously in wxGrid::OnPaint but that doesn't
6996 // seem to get called under wxGTK - MB
6998 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6999 m_numRows
&& m_numCols
)
7001 m_currentCellCoords
.Set(0, 0);
7004 if ( IsCellEditControlShown() )
7006 // don't show highlight when the edit control is shown
7010 // if the active cell was repainted, repaint its highlight too because it
7011 // might have been damaged by the grid lines
7012 size_t count
= cells
.GetCount();
7013 for ( size_t n
= 0; n
< count
; n
++ )
7015 if ( cells
[n
] == m_currentCellCoords
)
7017 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7018 DrawCellHighlight(dc
, attr
);
7026 // TODO: remove this ???
7027 // This is used to redraw all grid lines e.g. when the grid line colour
7030 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7032 #if !WXGRID_DRAW_LINES
7036 if ( !m_gridLinesEnabled
||
7038 !m_numCols
) return;
7040 int top
, bottom
, left
, right
;
7042 #if 0 //#ifndef __WXGTK__
7046 m_gridWin
->GetClientSize(&cw
, &ch
);
7048 // virtual coords of visible area
7050 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7051 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7056 reg
.GetBox(x
, y
, w
, h
);
7057 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7058 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7062 m_gridWin
->GetClientSize(&cw
, &ch
);
7063 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7064 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7067 // avoid drawing grid lines past the last row and col
7069 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7070 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7072 // no gridlines inside multicells, clip them out
7073 int leftCol
= internalXToCol(left
);
7074 int topRow
= internalYToRow(top
);
7075 int rightCol
= internalXToCol(right
);
7076 int bottomRow
= internalYToRow(bottom
);
7077 wxRegion
clippedcells(0, 0, cw
, ch
);
7080 int i
, j
, cell_rows
, cell_cols
;
7083 for (j
=topRow
; j
<bottomRow
; j
++)
7085 for (i
=leftCol
; i
<rightCol
; i
++)
7087 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7088 if ((cell_rows
> 1) || (cell_cols
> 1))
7090 rect
= CellToRect(j
,i
);
7091 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7092 clippedcells
.Subtract(rect
);
7094 else if ((cell_rows
< 0) || (cell_cols
< 0))
7096 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7097 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7098 clippedcells
.Subtract(rect
);
7102 dc
.SetClippingRegion( clippedcells
);
7104 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7106 // horizontal grid lines
7108 // already declared above - int i;
7109 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7111 int bot
= GetRowBottom(i
) - 1;
7120 dc
.DrawLine( left
, bot
, right
, bot
);
7125 // vertical grid lines
7127 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7129 int colRight
= GetColRight(i
) - 1;
7130 if ( colRight
> right
)
7135 if ( colRight
>= left
)
7137 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7140 dc
.DestroyClippingRegion();
7144 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7146 if ( !m_numRows
) return;
7149 size_t numLabels
= rows
.GetCount();
7151 for ( i
= 0; i
< numLabels
; i
++ )
7153 DrawRowLabel( dc
, rows
[i
] );
7158 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7160 if ( GetRowHeight(row
) <= 0 )
7163 int rowTop
= GetRowTop(row
),
7164 rowBottom
= GetRowBottom(row
) - 1;
7166 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7167 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7168 m_rowLabelWidth
-1, rowBottom
);
7170 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7172 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7174 dc
.SetPen( *wxWHITE_PEN
);
7175 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7176 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7178 dc
.SetBackgroundMode( wxTRANSPARENT
);
7179 dc
.SetTextForeground( GetLabelTextColour() );
7180 dc
.SetFont( GetLabelFont() );
7183 GetRowLabelAlignment( &hAlign
, &vAlign
);
7187 rect
.SetY( GetRowTop(row
) + 2 );
7188 rect
.SetWidth( m_rowLabelWidth
- 4 );
7189 rect
.SetHeight( GetRowHeight(row
) - 4 );
7190 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7194 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7196 if ( !m_numCols
) return;
7199 size_t numLabels
= cols
.GetCount();
7201 for ( i
= 0; i
< numLabels
; i
++ )
7203 DrawColLabel( dc
, cols
[i
] );
7208 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7210 if ( GetColWidth(col
) <= 0 )
7213 int colLeft
= GetColLeft(col
),
7214 colRight
= GetColRight(col
) - 1;
7216 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7217 dc
.DrawLine( colRight
, 0,
7218 colRight
, m_colLabelHeight
-1 );
7220 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7222 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7223 colRight
+1, m_colLabelHeight
-1 );
7225 dc
.SetPen( *wxWHITE_PEN
);
7226 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7227 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7229 dc
.SetBackgroundMode( wxTRANSPARENT
);
7230 dc
.SetTextForeground( GetLabelTextColour() );
7231 dc
.SetFont( GetLabelFont() );
7233 int hAlign
, vAlign
, orient
;
7234 GetColLabelAlignment( &hAlign
, &vAlign
);
7235 orient
= GetColLabelTextOrientation();
7238 rect
.SetX( colLeft
+ 2 );
7240 rect
.SetWidth( GetColWidth(col
) - 4 );
7241 rect
.SetHeight( m_colLabelHeight
- 4 );
7242 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7245 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7246 const wxString
& value
,
7250 int textOrientation
)
7252 wxArrayString lines
;
7254 StringToLines( value
, lines
);
7257 //Forward to new API.
7258 DrawTextRectangle( dc
,
7267 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7268 const wxArrayString
& lines
,
7272 int textOrientation
)
7274 long textWidth
, textHeight
;
7275 long lineWidth
, lineHeight
;
7278 dc
.SetClippingRegion( rect
);
7280 nLines
= lines
.GetCount();
7284 float x
= 0.0, y
= 0.0;
7286 if( textOrientation
== wxHORIZONTAL
)
7287 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7289 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7293 case wxALIGN_BOTTOM
:
7294 if( textOrientation
== wxHORIZONTAL
)
7295 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7297 x
= rect
.x
+ rect
.width
- textWidth
;
7300 case wxALIGN_CENTRE
:
7301 if( textOrientation
== wxHORIZONTAL
)
7302 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7304 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7309 if( textOrientation
== wxHORIZONTAL
)
7316 // Align each line of a multi-line label
7317 for( l
= 0; l
< nLines
; l
++ )
7319 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7321 switch( horizAlign
)
7324 if( textOrientation
== wxHORIZONTAL
)
7325 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7327 y
= rect
.y
+ lineWidth
+ 1;
7330 case wxALIGN_CENTRE
:
7331 if( textOrientation
== wxHORIZONTAL
)
7332 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7334 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7339 if( textOrientation
== wxHORIZONTAL
)
7342 y
= rect
.y
+ rect
.height
- 1;
7346 if( textOrientation
== wxHORIZONTAL
)
7348 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7353 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7358 dc
.DestroyClippingRegion();
7362 // Split multi line text up into an array of strings. Any existing
7363 // contents of the string array are preserved.
7365 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7369 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7370 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7372 while ( startPos
< (int)tVal
.Length() )
7374 pos
= tVal
.Mid(startPos
).Find( eol
);
7379 else if ( pos
== 0 )
7381 lines
.Add( wxEmptyString
);
7385 lines
.Add( value
.Mid(startPos
, pos
) );
7389 if ( startPos
< (int)value
.Length() )
7391 lines
.Add( value
.Mid( startPos
) );
7396 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7397 const wxArrayString
& lines
,
7398 long *width
, long *height
)
7405 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7407 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7408 w
= wxMax( w
, lineW
);
7417 // ------ Batch processing.
7419 void wxGrid::EndBatch()
7421 if ( m_batchCount
> 0 )
7424 if ( !m_batchCount
)
7427 m_rowLabelWin
->Refresh();
7428 m_colLabelWin
->Refresh();
7429 m_cornerLabelWin
->Refresh();
7430 m_gridWin
->Refresh();
7435 // Use this, rather than wxWindow::Refresh(), to force an immediate
7436 // repainting of the grid. Has no effect if you are already inside a
7437 // BeginBatch / EndBatch block.
7439 void wxGrid::ForceRefresh()
7447 // ------ Edit control functions
7451 void wxGrid::EnableEditing( bool edit
)
7453 // TODO: improve this ?
7455 if ( edit
!= m_editable
)
7457 if(!edit
) EnableCellEditControl(edit
);
7463 void wxGrid::EnableCellEditControl( bool enable
)
7468 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7469 SetCurrentCell( 0, 0 );
7471 if ( enable
!= m_cellEditCtrlEnabled
)
7475 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7478 // this should be checked by the caller!
7479 wxASSERT_MSG( CanEnableCellControl(),
7480 _T("can't enable editing for this cell!") );
7482 // do it before ShowCellEditControl()
7483 m_cellEditCtrlEnabled
= enable
;
7485 ShowCellEditControl();
7489 //FIXME:add veto support
7490 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7492 HideCellEditControl();
7493 SaveEditControlValue();
7495 // do it after HideCellEditControl()
7496 m_cellEditCtrlEnabled
= enable
;
7501 bool wxGrid::IsCurrentCellReadOnly() const
7504 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7505 bool readonly
= attr
->IsReadOnly();
7511 bool wxGrid::CanEnableCellControl() const
7513 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7514 !IsCurrentCellReadOnly();
7518 bool wxGrid::IsCellEditControlEnabled() const
7520 // the cell edit control might be disable for all cells or just for the
7521 // current one if it's read only
7522 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7525 bool wxGrid::IsCellEditControlShown() const
7527 bool isShown
= false;
7529 if ( m_cellEditCtrlEnabled
)
7531 int row
= m_currentCellCoords
.GetRow();
7532 int col
= m_currentCellCoords
.GetCol();
7533 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7534 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7539 if ( editor
->IsCreated() )
7541 isShown
= editor
->GetControl()->IsShown();
7551 void wxGrid::ShowCellEditControl()
7553 if ( IsCellEditControlEnabled() )
7555 if ( !IsVisible( m_currentCellCoords
) )
7557 m_cellEditCtrlEnabled
= false;
7562 wxRect rect
= CellToRect( m_currentCellCoords
);
7563 int row
= m_currentCellCoords
.GetRow();
7564 int col
= m_currentCellCoords
.GetCol();
7566 // if this is part of a multicell, find owner (topleft)
7567 int cell_rows
, cell_cols
;
7568 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7569 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7573 m_currentCellCoords
.SetRow( row
);
7574 m_currentCellCoords
.SetCol( col
);
7577 // convert to scrolled coords
7579 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7581 // done in PaintBackground()
7583 // erase the highlight and the cell contents because the editor
7584 // might not cover the entire cell
7585 wxClientDC
dc( m_gridWin
);
7587 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7588 dc
.SetPen(*wxTRANSPARENT_PEN
);
7589 dc
.DrawRectangle(rect
);
7592 // cell is shifted by one pixel
7593 // However, don't allow x or y to become negative
7594 // since the SetSize() method interprets that as
7601 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7602 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7603 if ( !editor
->IsCreated() )
7605 editor
->Create(m_gridWin
, wxID_ANY
,
7606 new wxGridCellEditorEvtHandler(this, editor
));
7608 wxGridEditorCreatedEvent
evt(GetId(),
7609 wxEVT_GRID_EDITOR_CREATED
,
7613 editor
->GetControl());
7614 GetEventHandler()->ProcessEvent(evt
);
7618 // resize editor to overflow into righthand cells if allowed
7619 int maxWidth
= rect
.width
;
7620 wxString value
= GetCellValue(row
, col
);
7621 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7624 GetTextExtent(value
, &maxWidth
, &y
,
7625 NULL
, NULL
, &attr
->GetFont());
7626 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7628 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7629 if (rect
.x
+maxWidth
> client_right
)
7630 maxWidth
= client_right
- rect
.x
;
7632 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7634 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7635 // may have changed earlier
7636 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7639 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7640 // looks weird going over a multicell
7641 if (m_table
->IsEmptyCell(row
,i
) &&
7642 (rect
.width
< maxWidth
) && (c_rows
== 1))
7643 rect
.width
+= GetColWidth(i
);
7647 if (rect
.GetRight() > client_right
)
7648 rect
.SetRight(client_right
-1);
7651 editor
->SetCellAttr(attr
);
7652 editor
->SetSize( rect
);
7653 editor
->Show( true, attr
);
7655 // recalc dimensions in case we need to
7656 // expand the scrolled window to account for editor
7659 editor
->BeginEdit(row
, col
, this);
7660 editor
->SetCellAttr(NULL
);
7669 void wxGrid::HideCellEditControl()
7671 if ( IsCellEditControlEnabled() )
7673 int row
= m_currentCellCoords
.GetRow();
7674 int col
= m_currentCellCoords
.GetCol();
7676 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7677 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7678 editor
->Show( false );
7681 m_gridWin
->SetFocus();
7682 // refresh whole row to the right
7683 wxRect
rect( CellToRect(row
, col
) );
7684 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7685 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7686 m_gridWin
->Refresh( false, &rect
);
7691 void wxGrid::SaveEditControlValue()
7693 if ( IsCellEditControlEnabled() )
7695 int row
= m_currentCellCoords
.GetRow();
7696 int col
= m_currentCellCoords
.GetCol();
7698 wxString oldval
= GetCellValue(row
,col
);
7700 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7701 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7702 bool changed
= editor
->EndEdit(row
, col
, this);
7709 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7710 m_currentCellCoords
.GetRow(),
7711 m_currentCellCoords
.GetCol() ) < 0 ) {
7713 // Event has been vetoed, set the data back.
7714 SetCellValue(row
,col
,oldval
);
7722 // ------ Grid location functions
7723 // Note that all of these functions work with the logical coordinates of
7724 // grid cells and labels so you will need to convert from device
7725 // coordinates for mouse events etc.
7728 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7730 int row
= YToRow(y
);
7731 int col
= XToCol(x
);
7733 if ( row
== -1 || col
== -1 )
7735 coords
= wxGridNoCellCoords
;
7739 coords
.Set( row
, col
);
7744 // Internal Helper function for computing row or column from some
7745 // (unscrolled) coordinate value, using either
7746 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7747 // of m_rowBottoms/m_ColRights to speed up the search!
7749 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7750 const wxArrayInt
& BorderArray
, int nMax
,
7755 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7761 size_t i_max
= coord
/ defaultDist
,
7764 if (BorderArray
.IsEmpty())
7766 if((int) i_max
< nMax
)
7768 return clipToMinMax
? nMax
- 1 : -1;
7771 if ( i_max
>= BorderArray
.GetCount())
7772 i_max
= BorderArray
.GetCount() - 1;
7775 if ( coord
>= BorderArray
[i_max
])
7779 i_max
= coord
/ minDist
;
7781 i_max
= BorderArray
.GetCount() - 1;
7783 if ( i_max
>= BorderArray
.GetCount())
7784 i_max
= BorderArray
.GetCount() - 1;
7786 if ( coord
>= BorderArray
[i_max
])
7787 return clipToMinMax
? (int)i_max
: -1;
7788 if ( coord
< BorderArray
[0] )
7791 while ( i_max
- i_min
> 0 )
7793 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7794 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7795 if (coord
>= BorderArray
[ i_max
- 1])
7799 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7800 if (coord
< BorderArray
[median
])
7808 int wxGrid::YToRow( int y
)
7810 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7811 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7815 int wxGrid::XToCol( int x
)
7817 return CoordToRowOrCol(x
, m_defaultColWidth
,
7818 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7822 // return the row number that that the y coord is near the edge of, or
7823 // -1 if not near an edge
7825 int wxGrid::YToEdgeOfRow( int y
)
7828 i
= internalYToRow(y
);
7830 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7832 // We know that we are in row i, test whether we are
7833 // close enough to lower or upper border, respectively.
7834 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7836 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7844 // return the col number that that the x coord is near the edge of, or
7845 // -1 if not near an edge
7847 int wxGrid::XToEdgeOfCol( int x
)
7850 i
= internalXToCol(x
);
7852 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7854 // We know that we are in column i, test whether we are
7855 // close enough to right or left border, respectively.
7856 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7858 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7866 wxRect
wxGrid::CellToRect( int row
, int col
)
7868 wxRect
rect( -1, -1, -1, -1 );
7870 if ( row
>= 0 && row
< m_numRows
&&
7871 col
>= 0 && col
< m_numCols
)
7873 int i
, cell_rows
, cell_cols
;
7874 rect
.width
= rect
.height
= 0;
7875 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7876 // if negative then find multicell owner
7877 if (cell_rows
< 0) row
+= cell_rows
;
7878 if (cell_cols
< 0) col
+= cell_cols
;
7879 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7881 rect
.x
= GetColLeft(col
);
7882 rect
.y
= GetRowTop(row
);
7883 for (i
=col
; i
<col
+cell_cols
; i
++)
7884 rect
.width
+= GetColWidth(i
);
7885 for (i
=row
; i
<row
+cell_rows
; i
++)
7886 rect
.height
+= GetRowHeight(i
);
7889 // if grid lines are enabled, then the area of the cell is a bit smaller
7890 if (m_gridLinesEnabled
) {
7898 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7900 // get the cell rectangle in logical coords
7902 wxRect
r( CellToRect( row
, col
) );
7904 // convert to device coords
7906 int left
, top
, right
, bottom
;
7907 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7908 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7910 // check against the client area of the grid window
7913 m_gridWin
->GetClientSize( &cw
, &ch
);
7915 if ( wholeCellVisible
)
7917 // is the cell wholly visible ?
7919 return ( left
>= 0 && right
<= cw
&&
7920 top
>= 0 && bottom
<= ch
);
7924 // is the cell partly visible ?
7926 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7927 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7932 // make the specified cell location visible by doing a minimal amount
7935 void wxGrid::MakeCellVisible( int row
, int col
)
7939 int xpos
= -1, ypos
= -1;
7941 if ( row
>= 0 && row
< m_numRows
&&
7942 col
>= 0 && col
< m_numCols
)
7944 // get the cell rectangle in logical coords
7946 wxRect
r( CellToRect( row
, col
) );
7948 // convert to device coords
7950 int left
, top
, right
, bottom
;
7951 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7952 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7955 m_gridWin
->GetClientSize( &cw
, &ch
);
7961 else if ( bottom
> ch
)
7963 int h
= r
.GetHeight();
7965 for ( i
= row
-1; i
>= 0; i
-- )
7967 int rowHeight
= GetRowHeight(i
);
7968 if ( h
+ rowHeight
> ch
)
7975 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7976 // have rounding errors (this is important, because if we do, we
7977 // might not scroll at all and some cells won't be redrawn)
7979 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7981 ypos
+= GRID_SCROLL_LINE_Y
;
7988 else if ( right
> cw
)
7990 // position the view so that the cell is on the right
7992 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7993 xpos
= x0
+ (right
- cw
);
7995 // see comment for ypos above
7996 xpos
+= GRID_SCROLL_LINE_X
;
7999 if ( xpos
!= -1 || ypos
!= -1 )
8002 xpos
/= GRID_SCROLL_LINE_X
;
8004 ypos
/= GRID_SCROLL_LINE_Y
;
8005 Scroll( xpos
, ypos
);
8013 // ------ Grid cursor movement functions
8016 bool wxGrid::MoveCursorUp( bool expandSelection
)
8018 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8019 m_currentCellCoords
.GetRow() >= 0 )
8021 if ( expandSelection
)
8023 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8024 m_selectingKeyboard
= m_currentCellCoords
;
8025 if ( m_selectingKeyboard
.GetRow() > 0 )
8027 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8028 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8029 m_selectingKeyboard
.GetCol() );
8030 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8033 else if ( m_currentCellCoords
.GetRow() > 0 )
8036 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8037 m_currentCellCoords
.GetCol() );
8038 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8039 m_currentCellCoords
.GetCol() );
8050 bool wxGrid::MoveCursorDown( bool expandSelection
)
8052 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8053 m_currentCellCoords
.GetRow() < m_numRows
)
8055 if ( expandSelection
)
8057 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8058 m_selectingKeyboard
= m_currentCellCoords
;
8059 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8061 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8062 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8063 m_selectingKeyboard
.GetCol() );
8064 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8067 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8070 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8071 m_currentCellCoords
.GetCol() );
8072 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8073 m_currentCellCoords
.GetCol() );
8084 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8086 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8087 m_currentCellCoords
.GetCol() >= 0 )
8089 if ( expandSelection
)
8091 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8092 m_selectingKeyboard
= m_currentCellCoords
;
8093 if ( m_selectingKeyboard
.GetCol() > 0 )
8095 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8096 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8097 m_selectingKeyboard
.GetCol() );
8098 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8101 else if ( m_currentCellCoords
.GetCol() > 0 )
8104 MakeCellVisible( m_currentCellCoords
.GetRow(),
8105 m_currentCellCoords
.GetCol() - 1 );
8106 SetCurrentCell( m_currentCellCoords
.GetRow(),
8107 m_currentCellCoords
.GetCol() - 1 );
8118 bool wxGrid::MoveCursorRight( bool expandSelection
)
8120 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8121 m_currentCellCoords
.GetCol() < m_numCols
)
8123 if ( expandSelection
)
8125 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8126 m_selectingKeyboard
= m_currentCellCoords
;
8127 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8129 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8130 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8131 m_selectingKeyboard
.GetCol() );
8132 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8135 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8138 MakeCellVisible( m_currentCellCoords
.GetRow(),
8139 m_currentCellCoords
.GetCol() + 1 );
8140 SetCurrentCell( m_currentCellCoords
.GetRow(),
8141 m_currentCellCoords
.GetCol() + 1 );
8152 bool wxGrid::MovePageUp()
8154 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8156 int row
= m_currentCellCoords
.GetRow();
8160 m_gridWin
->GetClientSize( &cw
, &ch
);
8162 int y
= GetRowTop(row
);
8163 int newRow
= internalYToRow( y
- ch
+ 1 );
8165 if ( newRow
== row
)
8167 //row > 0 , so newrow can never be less than 0 here.
8171 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8172 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8180 bool wxGrid::MovePageDown()
8182 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8184 int row
= m_currentCellCoords
.GetRow();
8185 if ( (row
+1) < m_numRows
)
8188 m_gridWin
->GetClientSize( &cw
, &ch
);
8190 int y
= GetRowTop(row
);
8191 int newRow
= internalYToRow( y
+ ch
);
8192 if ( newRow
== row
)
8194 // row < m_numRows , so newrow can't overflow here.
8198 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8199 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8207 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8210 m_currentCellCoords
!= wxGridNoCellCoords
&&
8211 m_currentCellCoords
.GetRow() > 0 )
8213 int row
= m_currentCellCoords
.GetRow();
8214 int col
= m_currentCellCoords
.GetCol();
8216 if ( m_table
->IsEmptyCell(row
, col
) )
8218 // starting in an empty cell: find the next block of
8224 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8227 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8229 // starting at the top of a block: find the next block
8235 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8240 // starting within a block: find the top of the block
8245 if ( m_table
->IsEmptyCell(row
, col
) )
8253 MakeCellVisible( row
, col
);
8254 if ( expandSelection
)
8256 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8257 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8262 SetCurrentCell( row
, col
);
8270 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8273 m_currentCellCoords
!= wxGridNoCellCoords
&&
8274 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8276 int row
= m_currentCellCoords
.GetRow();
8277 int col
= m_currentCellCoords
.GetCol();
8279 if ( m_table
->IsEmptyCell(row
, col
) )
8281 // starting in an empty cell: find the next block of
8284 while ( row
< m_numRows
-1 )
8287 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8290 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8292 // starting at the bottom of a block: find the next block
8295 while ( row
< m_numRows
-1 )
8298 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8303 // starting within a block: find the bottom of the block
8305 while ( row
< m_numRows
-1 )
8308 if ( m_table
->IsEmptyCell(row
, col
) )
8316 MakeCellVisible( row
, col
);
8317 if ( expandSelection
)
8319 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8320 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8325 SetCurrentCell( row
, col
);
8334 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8337 m_currentCellCoords
!= wxGridNoCellCoords
&&
8338 m_currentCellCoords
.GetCol() > 0 )
8340 int row
= m_currentCellCoords
.GetRow();
8341 int col
= m_currentCellCoords
.GetCol();
8343 if ( m_table
->IsEmptyCell(row
, col
) )
8345 // starting in an empty cell: find the next block of
8351 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8354 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8356 // starting at the left of a block: find the next block
8362 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8367 // starting within a block: find the left of the block
8372 if ( m_table
->IsEmptyCell(row
, col
) )
8380 MakeCellVisible( row
, col
);
8381 if ( expandSelection
)
8383 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8384 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8389 SetCurrentCell( row
, col
);
8398 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8401 m_currentCellCoords
!= wxGridNoCellCoords
&&
8402 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8404 int row
= m_currentCellCoords
.GetRow();
8405 int col
= m_currentCellCoords
.GetCol();
8407 if ( m_table
->IsEmptyCell(row
, col
) )
8409 // starting in an empty cell: find the next block of
8412 while ( col
< m_numCols
-1 )
8415 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8418 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8420 // starting at the right of a block: find the next block
8423 while ( col
< m_numCols
-1 )
8426 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8431 // starting within a block: find the right of the block
8433 while ( col
< m_numCols
-1 )
8436 if ( m_table
->IsEmptyCell(row
, col
) )
8444 MakeCellVisible( row
, col
);
8445 if ( expandSelection
)
8447 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8448 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8453 SetCurrentCell( row
, col
);
8465 // ------ Label values and formatting
8468 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8470 *horiz
= m_rowLabelHorizAlign
;
8471 *vert
= m_rowLabelVertAlign
;
8474 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8476 *horiz
= m_colLabelHorizAlign
;
8477 *vert
= m_colLabelVertAlign
;
8480 int wxGrid::GetColLabelTextOrientation()
8482 return m_colLabelTextOrientation
;
8485 wxString
wxGrid::GetRowLabelValue( int row
)
8489 return m_table
->GetRowLabelValue( row
);
8499 wxString
wxGrid::GetColLabelValue( int col
)
8503 return m_table
->GetColLabelValue( col
);
8514 void wxGrid::SetRowLabelSize( int width
)
8516 width
= wxMax( width
, 0 );
8517 if ( width
!= m_rowLabelWidth
)
8521 m_rowLabelWin
->Show( false );
8522 m_cornerLabelWin
->Show( false );
8524 else if ( m_rowLabelWidth
== 0 )
8526 m_rowLabelWin
->Show( true );
8527 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8530 m_rowLabelWidth
= width
;
8532 wxScrolledWindow::Refresh( true );
8537 void wxGrid::SetColLabelSize( int height
)
8539 height
= wxMax( height
, 0 );
8540 if ( height
!= m_colLabelHeight
)
8544 m_colLabelWin
->Show( false );
8545 m_cornerLabelWin
->Show( false );
8547 else if ( m_colLabelHeight
== 0 )
8549 m_colLabelWin
->Show( true );
8550 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8553 m_colLabelHeight
= height
;
8555 wxScrolledWindow::Refresh( true );
8560 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8562 if ( m_labelBackgroundColour
!= colour
)
8564 m_labelBackgroundColour
= colour
;
8565 m_rowLabelWin
->SetBackgroundColour( colour
);
8566 m_colLabelWin
->SetBackgroundColour( colour
);
8567 m_cornerLabelWin
->SetBackgroundColour( colour
);
8569 if ( !GetBatchCount() )
8571 m_rowLabelWin
->Refresh();
8572 m_colLabelWin
->Refresh();
8573 m_cornerLabelWin
->Refresh();
8578 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8580 if ( m_labelTextColour
!= colour
)
8582 m_labelTextColour
= colour
;
8583 if ( !GetBatchCount() )
8585 m_rowLabelWin
->Refresh();
8586 m_colLabelWin
->Refresh();
8591 void wxGrid::SetLabelFont( const wxFont
& font
)
8594 if ( !GetBatchCount() )
8596 m_rowLabelWin
->Refresh();
8597 m_colLabelWin
->Refresh();
8601 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8603 // allow old (incorrect) defs to be used
8606 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8607 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8608 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8613 case wxTOP
: vert
= wxALIGN_TOP
; break;
8614 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8615 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8618 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8620 m_rowLabelHorizAlign
= horiz
;
8623 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8625 m_rowLabelVertAlign
= vert
;
8628 if ( !GetBatchCount() )
8630 m_rowLabelWin
->Refresh();
8634 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8636 // allow old (incorrect) defs to be used
8639 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8640 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8641 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8646 case wxTOP
: vert
= wxALIGN_TOP
; break;
8647 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8648 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8651 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8653 m_colLabelHorizAlign
= horiz
;
8656 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8658 m_colLabelVertAlign
= vert
;
8661 if ( !GetBatchCount() )
8663 m_colLabelWin
->Refresh();
8667 // Note: under MSW, the default column label font must be changed because it
8668 // does not support vertical printing
8670 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8671 // pGrid->SetLabelFont(font);
8672 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8674 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8676 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8678 m_colLabelTextOrientation
= textOrientation
;
8681 if ( !GetBatchCount() )
8683 m_colLabelWin
->Refresh();
8687 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8691 m_table
->SetRowLabelValue( row
, s
);
8692 if ( !GetBatchCount() )
8694 wxRect rect
= CellToRect( row
, 0);
8695 if ( rect
.height
> 0 )
8697 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8699 rect
.width
= m_rowLabelWidth
;
8700 m_rowLabelWin
->Refresh( true, &rect
);
8706 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8710 m_table
->SetColLabelValue( col
, s
);
8711 if ( !GetBatchCount() )
8713 wxRect rect
= CellToRect( 0, col
);
8714 if ( rect
.width
> 0 )
8716 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8718 rect
.height
= m_colLabelHeight
;
8719 m_colLabelWin
->Refresh( true, &rect
);
8725 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8727 if ( m_gridLineColour
!= colour
)
8729 m_gridLineColour
= colour
;
8731 wxClientDC
dc( m_gridWin
);
8733 DrawAllGridLines( dc
, wxRegion() );
8738 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8740 if ( m_cellHighlightColour
!= colour
)
8742 m_cellHighlightColour
= colour
;
8744 wxClientDC
dc( m_gridWin
);
8746 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8747 DrawCellHighlight(dc
, attr
);
8752 void wxGrid::SetCellHighlightPenWidth(int width
)
8754 if (m_cellHighlightPenWidth
!= width
) {
8755 m_cellHighlightPenWidth
= width
;
8757 // Just redrawing the cell highlight is not enough since that won't
8758 // make any visible change if the the thickness is getting smaller.
8759 int row
= m_currentCellCoords
.GetRow();
8760 int col
= m_currentCellCoords
.GetCol();
8761 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8763 wxRect rect
= CellToRect(row
, col
);
8764 m_gridWin
->Refresh(true, &rect
);
8768 void wxGrid::SetCellHighlightROPenWidth(int width
)
8770 if (m_cellHighlightROPenWidth
!= width
) {
8771 m_cellHighlightROPenWidth
= width
;
8773 // Just redrawing the cell highlight is not enough since that won't
8774 // make any visible change if the the thickness is getting smaller.
8775 int row
= m_currentCellCoords
.GetRow();
8776 int col
= m_currentCellCoords
.GetCol();
8777 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8779 wxRect rect
= CellToRect(row
, col
);
8780 m_gridWin
->Refresh(true, &rect
);
8784 void wxGrid::EnableGridLines( bool enable
)
8786 if ( enable
!= m_gridLinesEnabled
)
8788 m_gridLinesEnabled
= enable
;
8790 if ( !GetBatchCount() )
8794 wxClientDC
dc( m_gridWin
);
8796 DrawAllGridLines( dc
, wxRegion() );
8800 m_gridWin
->Refresh();
8807 int wxGrid::GetDefaultRowSize()
8809 return m_defaultRowHeight
;
8812 int wxGrid::GetRowSize( int row
)
8814 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8816 return GetRowHeight(row
);
8819 int wxGrid::GetDefaultColSize()
8821 return m_defaultColWidth
;
8824 int wxGrid::GetColSize( int col
)
8826 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8828 return GetColWidth(col
);
8831 // ============================================================================
8832 // access to the grid attributes: each of them has a default value in the grid
8833 // itself and may be overidden on a per-cell basis
8834 // ============================================================================
8836 // ----------------------------------------------------------------------------
8837 // setting default attributes
8838 // ----------------------------------------------------------------------------
8840 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8842 m_defaultCellAttr
->SetBackgroundColour(col
);
8844 m_gridWin
->SetBackgroundColour(col
);
8848 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8850 m_defaultCellAttr
->SetTextColour(col
);
8853 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8855 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8858 void wxGrid::SetDefaultCellOverflow( bool allow
)
8860 m_defaultCellAttr
->SetOverflow(allow
);
8863 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8865 m_defaultCellAttr
->SetFont(font
);
8868 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8870 m_defaultCellAttr
->SetRenderer(renderer
);
8873 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8875 m_defaultCellAttr
->SetEditor(editor
);
8878 // ----------------------------------------------------------------------------
8879 // access to the default attrbiutes
8880 // ----------------------------------------------------------------------------
8882 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8884 return m_defaultCellAttr
->GetBackgroundColour();
8887 wxColour
wxGrid::GetDefaultCellTextColour()
8889 return m_defaultCellAttr
->GetTextColour();
8892 wxFont
wxGrid::GetDefaultCellFont()
8894 return m_defaultCellAttr
->GetFont();
8897 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8899 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8902 bool wxGrid::GetDefaultCellOverflow()
8904 return m_defaultCellAttr
->GetOverflow();
8907 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8909 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8912 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8914 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8917 // ----------------------------------------------------------------------------
8918 // access to cell attributes
8919 // ----------------------------------------------------------------------------
8921 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8923 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8924 wxColour colour
= attr
->GetBackgroundColour();
8929 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8931 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8932 wxColour colour
= attr
->GetTextColour();
8937 wxFont
wxGrid::GetCellFont( int row
, int col
)
8939 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8940 wxFont font
= attr
->GetFont();
8945 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8947 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8948 attr
->GetAlignment(horiz
, vert
);
8952 bool wxGrid::GetCellOverflow( int row
, int col
)
8954 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8955 bool allow
= attr
->GetOverflow();
8960 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8962 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8963 attr
->GetSize( num_rows
, num_cols
);
8967 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8969 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8970 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8976 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8978 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8979 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8985 bool wxGrid::IsReadOnly(int row
, int col
) const
8987 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8988 bool isReadOnly
= attr
->IsReadOnly();
8993 // ----------------------------------------------------------------------------
8994 // attribute support: cache, automatic provider creation, ...
8995 // ----------------------------------------------------------------------------
8997 bool wxGrid::CanHaveAttributes()
9004 return m_table
->CanHaveAttributes();
9007 void wxGrid::ClearAttrCache()
9009 if ( m_attrCache
.row
!= -1 )
9011 wxSafeDecRef(m_attrCache
.attr
);
9012 m_attrCache
.attr
= NULL
;
9013 m_attrCache
.row
= -1;
9017 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9021 wxGrid
*self
= (wxGrid
*)this; // const_cast
9023 self
->ClearAttrCache();
9024 self
->m_attrCache
.row
= row
;
9025 self
->m_attrCache
.col
= col
;
9026 self
->m_attrCache
.attr
= attr
;
9031 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9033 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9035 *attr
= m_attrCache
.attr
;
9036 wxSafeIncRef(m_attrCache
.attr
);
9038 #ifdef DEBUG_ATTR_CACHE
9039 gs_nAttrCacheHits
++;
9046 #ifdef DEBUG_ATTR_CACHE
9047 gs_nAttrCacheMisses
++;
9053 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9055 wxGridCellAttr
*attr
= NULL
;
9056 // Additional test to avoid looking at the cache e.g. for
9057 // wxNoCellCoords, as this will confuse memory management.
9060 if ( !LookupAttr(row
, col
, &attr
) )
9062 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9063 : (wxGridCellAttr
*)NULL
;
9064 CacheAttr(row
, col
, attr
);
9069 attr
->SetDefAttr(m_defaultCellAttr
);
9073 attr
= m_defaultCellAttr
;
9080 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9082 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9084 wxCHECK_MSG( m_table
, attr
,
9085 _T("we may only be called if CanHaveAttributes() returned true and then m_table should be !NULL") );
9087 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9090 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9092 // artificially inc the ref count to match DecRef() in caller
9094 m_table
->SetAttr(attr
, row
, col
);
9100 // ----------------------------------------------------------------------------
9101 // setting column attributes (wrappers around SetColAttr)
9102 // ----------------------------------------------------------------------------
9104 void wxGrid::SetColFormatBool(int col
)
9106 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9109 void wxGrid::SetColFormatNumber(int col
)
9111 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9114 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9116 wxString typeName
= wxGRID_VALUE_FLOAT
;
9117 if ( (width
!= -1) || (precision
!= -1) )
9119 typeName
<< _T(':') << width
<< _T(',') << precision
;
9122 SetColFormatCustom(col
, typeName
);
9125 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9127 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9129 attr
= new wxGridCellAttr
;
9130 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9131 attr
->SetRenderer(renderer
);
9133 SetColAttr(col
, attr
);
9137 // ----------------------------------------------------------------------------
9138 // setting cell attributes: this is forwarded to the table
9139 // ----------------------------------------------------------------------------
9141 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9143 if ( CanHaveAttributes() )
9145 m_table
->SetAttr(attr
, row
, col
);
9154 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9156 if ( CanHaveAttributes() )
9158 m_table
->SetRowAttr(attr
, row
);
9167 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9169 if ( CanHaveAttributes() )
9171 m_table
->SetColAttr(attr
, col
);
9180 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9182 if ( CanHaveAttributes() )
9184 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9185 attr
->SetBackgroundColour(colour
);
9190 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9192 if ( CanHaveAttributes() )
9194 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9195 attr
->SetTextColour(colour
);
9200 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9202 if ( CanHaveAttributes() )
9204 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9205 attr
->SetFont(font
);
9210 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9212 if ( CanHaveAttributes() )
9214 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9215 attr
->SetAlignment(horiz
, vert
);
9220 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9222 if ( CanHaveAttributes() )
9224 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9225 attr
->SetOverflow(allow
);
9230 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9232 if ( CanHaveAttributes() )
9234 int cell_rows
, cell_cols
;
9236 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9237 attr
->GetSize(&cell_rows
, &cell_cols
);
9238 attr
->SetSize(num_rows
, num_cols
);
9241 // Cannot set the size of a cell to 0 or negative values
9242 // While it is perfectly legal to do that, this function cannot
9243 // handle all the possibilies, do it by hand by getting the CellAttr.
9244 // You can only set the size of a cell to 1,1 or greater with this fn
9245 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9246 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9247 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9248 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9250 // if this was already a multicell then "turn off" the other cells first
9251 if ((cell_rows
> 1) || (cell_rows
> 1))
9254 for (j
=row
; j
<row
+cell_rows
; j
++)
9256 for (i
=col
; i
<col
+cell_cols
; i
++)
9258 if ((i
!= col
) || (j
!= row
))
9260 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9261 attr_stub
->SetSize( 1, 1 );
9262 attr_stub
->DecRef();
9268 // mark the cells that will be covered by this cell to
9269 // negative or zero values to point back at this cell
9270 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9273 for (j
=row
; j
<row
+num_rows
; j
++)
9275 for (i
=col
; i
<col
+num_cols
; i
++)
9277 if ((i
!= col
) || (j
!= row
))
9279 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9280 attr_stub
->SetSize( row
-j
, col
-i
);
9281 attr_stub
->DecRef();
9289 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9291 if ( CanHaveAttributes() )
9293 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9294 attr
->SetRenderer(renderer
);
9299 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9301 if ( CanHaveAttributes() )
9303 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9304 attr
->SetEditor(editor
);
9309 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9311 if ( CanHaveAttributes() )
9313 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9314 attr
->SetReadOnly(isReadOnly
);
9319 // ----------------------------------------------------------------------------
9320 // Data type registration
9321 // ----------------------------------------------------------------------------
9323 void wxGrid::RegisterDataType(const wxString
& typeName
,
9324 wxGridCellRenderer
* renderer
,
9325 wxGridCellEditor
* editor
)
9327 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9331 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9333 wxString typeName
= m_table
->GetTypeName(row
, col
);
9334 return GetDefaultEditorForType(typeName
);
9337 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9339 wxString typeName
= m_table
->GetTypeName(row
, col
);
9340 return GetDefaultRendererForType(typeName
);
9344 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9346 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9347 if ( index
== wxNOT_FOUND
)
9349 wxFAIL_MSG(wxT("Unknown data type name"));
9354 return m_typeRegistry
->GetEditor(index
);
9358 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9360 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9361 if ( index
== wxNOT_FOUND
)
9363 wxFAIL_MSG(wxT("Unknown data type name"));
9368 return m_typeRegistry
->GetRenderer(index
);
9372 // ----------------------------------------------------------------------------
9374 // ----------------------------------------------------------------------------
9376 void wxGrid::EnableDragRowSize( bool enable
)
9378 m_canDragRowSize
= enable
;
9382 void wxGrid::EnableDragColSize( bool enable
)
9384 m_canDragColSize
= enable
;
9387 void wxGrid::EnableDragGridSize( bool enable
)
9389 m_canDragGridSize
= enable
;
9393 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9395 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9397 if ( resizeExistingRows
)
9399 // since we are resizing all rows to the default row size,
9400 // we can simply clear the row heights and row bottoms
9401 // arrays (which also allows us to take advantage of
9402 // some speed optimisations)
9403 m_rowHeights
.Empty();
9404 m_rowBottoms
.Empty();
9405 if ( !GetBatchCount() )
9410 void wxGrid::SetRowSize( int row
, int height
)
9412 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9414 // See comment in SetColSize
9415 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9417 if ( m_rowHeights
.IsEmpty() )
9419 // need to really create the array
9423 int h
= wxMax( 0, height
);
9424 int diff
= h
- m_rowHeights
[row
];
9426 m_rowHeights
[row
] = h
;
9428 for ( i
= row
; i
< m_numRows
; i
++ )
9430 m_rowBottoms
[i
] += diff
;
9432 if ( !GetBatchCount() )
9436 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9438 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9440 if ( resizeExistingCols
)
9442 // since we are resizing all columns to the default column size,
9443 // we can simply clear the col widths and col rights
9444 // arrays (which also allows us to take advantage of
9445 // some speed optimisations)
9446 m_colWidths
.Empty();
9447 m_colRights
.Empty();
9448 if ( !GetBatchCount() )
9453 void wxGrid::SetColSize( int col
, int width
)
9455 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9457 // should we check that it's bigger than GetColMinimalWidth(col) here?
9459 // No, because it is reasonable to assume the library user know's
9460 // what he is doing. However whe should test against the weaker
9461 // constariant of minimalAcceptableWidth, as this breaks rendering
9463 // This test then fixes sf.net bug #645734
9465 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9467 if ( m_colWidths
.IsEmpty() )
9469 // need to really create the array
9473 // if < 0 calc new width from label
9477 wxArrayString lines
;
9478 wxClientDC
dc(m_colLabelWin
);
9479 dc
.SetFont(GetLabelFont());
9480 StringToLines(GetColLabelValue(col
), lines
);
9481 GetTextBoxSize(dc
, lines
, &w
, &h
);
9484 int w
= wxMax( 0, width
);
9485 int diff
= w
- m_colWidths
[col
];
9486 m_colWidths
[col
] = w
;
9489 for ( i
= col
; i
< m_numCols
; i
++ )
9491 m_colRights
[i
] += diff
;
9493 if ( !GetBatchCount() )
9498 void wxGrid::SetColMinimalWidth( int col
, int width
)
9500 if (width
> GetColMinimalAcceptableWidth()) {
9501 m_colMinWidths
[col
] = width
;
9505 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9507 if (width
> GetRowMinimalAcceptableHeight()) {
9508 m_rowMinHeights
[row
] = width
;
9512 int wxGrid::GetColMinimalWidth(int col
) const
9514 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9515 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9518 int wxGrid::GetRowMinimalHeight(int row
) const
9520 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9521 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9524 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9526 // We do allow a width of 0 since this gives us
9527 // an easy way to temporarily hidding columns.
9530 m_minAcceptableColWidth
= width
;
9533 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9535 // We do allow a height of 0 since this gives us
9536 // an easy way to temporarily hidding rows.
9539 m_minAcceptableRowHeight
= height
;
9542 int wxGrid::GetColMinimalAcceptableWidth() const
9544 return m_minAcceptableColWidth
;
9547 int wxGrid::GetRowMinimalAcceptableHeight() const
9549 return m_minAcceptableRowHeight
;
9552 // ----------------------------------------------------------------------------
9554 // ----------------------------------------------------------------------------
9556 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9558 wxClientDC
dc(m_gridWin
);
9560 //Cancel editting of cell
9561 HideCellEditControl();
9562 SaveEditControlValue();
9564 // init both of them to avoid compiler warnings, even if weo nly need one
9572 wxCoord extent
, extentMax
= 0;
9573 int max
= column
? m_numRows
: m_numCols
;
9574 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9581 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9582 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9585 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9586 extent
= column
? size
.x
: size
.y
;
9587 if ( extent
> extentMax
)
9598 // now also compare with the column label extent
9600 dc
.SetFont( GetLabelFont() );
9604 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9605 if( GetColLabelTextOrientation() == wxVERTICAL
)
9609 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9611 extent
= column
? w
: h
;
9612 if ( extent
> extentMax
)
9619 // empty column - give default extent (notice that if extentMax is less
9620 // than default extent but != 0, it's ok)
9621 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9627 // leave some space around text
9638 SetColSize(col
, extentMax
);
9639 if ( !GetBatchCount() )
9642 m_gridWin
->GetClientSize( &cw
, &ch
);
9643 wxRect
rect ( CellToRect( 0, col
) );
9645 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9646 rect
.width
= cw
- rect
.x
;
9647 rect
.height
= m_colLabelHeight
;
9648 m_colLabelWin
->Refresh( true, &rect
);
9653 SetRowSize(row
, extentMax
);
9654 if ( !GetBatchCount() )
9657 m_gridWin
->GetClientSize( &cw
, &ch
);
9658 wxRect
rect ( CellToRect( row
, 0 ) );
9660 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9661 rect
.width
= m_rowLabelWidth
;
9662 rect
.height
= ch
- rect
.y
;
9663 m_rowLabelWin
->Refresh( true, &rect
);
9669 SetColMinimalWidth(col
, extentMax
);
9671 SetRowMinimalHeight(row
, extentMax
);
9675 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9677 int width
= m_rowLabelWidth
;
9682 for ( int col
= 0; col
< m_numCols
; col
++ )
9686 AutoSizeColumn(col
, setAsMin
);
9689 width
+= GetColWidth(col
);
9698 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9700 int height
= m_colLabelHeight
;
9705 for ( int row
= 0; row
< m_numRows
; row
++ )
9709 AutoSizeRow(row
, setAsMin
);
9712 height
+= GetRowHeight(row
);
9721 void wxGrid::AutoSize()
9725 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9727 // round up the size to a multiple of scroll step - this ensures that we
9728 // won't get the scrollbars if we're sized exactly to this width
9729 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9731 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9732 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9734 // distribute the extra space between the columns/rows to avoid having
9735 // extra white space
9737 // Remove the extra m_extraWidth + 1 added above
9738 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9739 if ( diff
&& m_numCols
)
9741 // try to resize the columns uniformly
9742 wxCoord diffPerCol
= diff
/ m_numCols
;
9745 for ( int col
= 0; col
< m_numCols
; col
++ )
9747 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9751 // add remaining amount to the last columns
9752 diff
-= diffPerCol
* m_numCols
;
9755 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9757 SetColSize(col
, GetColWidth(col
) + 1);
9763 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9764 if ( diff
&& m_numRows
)
9766 // try to resize the columns uniformly
9767 wxCoord diffPerRow
= diff
/ m_numRows
;
9770 for ( int row
= 0; row
< m_numRows
; row
++ )
9772 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9776 // add remaining amount to the last rows
9777 diff
-= diffPerRow
* m_numRows
;
9780 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9782 SetRowSize(row
, GetRowHeight(row
) + 1);
9789 SetClientSize(sizeFit
);
9792 void wxGrid::AutoSizeRowLabelSize( int row
)
9794 wxArrayString lines
;
9797 // Hide the edit control, so it
9798 // won't interfer with drag-shrinking.
9799 if( IsCellEditControlShown() )
9801 HideCellEditControl();
9802 SaveEditControlValue();
9805 // autosize row height depending on label text
9806 StringToLines( GetRowLabelValue( row
), lines
);
9807 wxClientDC
dc( m_rowLabelWin
);
9808 GetTextBoxSize( dc
, lines
, &w
, &h
);
9809 if( h
< m_defaultRowHeight
)
9810 h
= m_defaultRowHeight
;
9815 void wxGrid::AutoSizeColLabelSize( int col
)
9817 wxArrayString lines
;
9820 // Hide the edit control, so it
9821 // won't interfer with drag-shrinking.
9822 if( IsCellEditControlShown() )
9824 HideCellEditControl();
9825 SaveEditControlValue();
9828 // autosize column width depending on label text
9829 StringToLines( GetColLabelValue( col
), lines
);
9830 wxClientDC
dc( m_colLabelWin
);
9831 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9832 GetTextBoxSize( dc
, lines
, &w
, &h
);
9834 GetTextBoxSize( dc
, lines
, &h
, &w
);
9835 if( w
< m_defaultColWidth
)
9836 w
= m_defaultColWidth
;
9841 wxSize
wxGrid::DoGetBestSize() const
9843 // don't set sizes, only calculate them
9844 wxGrid
*self
= (wxGrid
*)this; // const_cast
9847 width
= self
->SetOrCalcColumnSizes(true);
9848 height
= self
->SetOrCalcRowSizes(true);
9850 int maxwidth
, maxheight
;
9851 wxDisplaySize( & maxwidth
, & maxheight
);
9853 if ( width
> maxwidth
) width
= maxwidth
;
9854 if ( height
> maxheight
) height
= maxheight
;
9856 return wxSize( width
, height
);
9865 wxPen
& wxGrid::GetDividerPen() const
9870 // ----------------------------------------------------------------------------
9871 // cell value accessor functions
9872 // ----------------------------------------------------------------------------
9874 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9878 m_table
->SetValue( row
, col
, s
);
9879 if ( !GetBatchCount() )
9882 wxRect
rect( CellToRect( row
, col
) );
9884 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9885 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9886 m_gridWin
->Refresh( false, &rect
);
9889 if ( m_currentCellCoords
.GetRow() == row
&&
9890 m_currentCellCoords
.GetCol() == col
&&
9891 IsCellEditControlShown())
9892 // Note: If we are using IsCellEditControlEnabled,
9893 // this interacts badly with calling SetCellValue from
9894 // an EVT_GRID_CELL_CHANGE handler.
9896 HideCellEditControl();
9897 ShowCellEditControl(); // will reread data from table
9904 // ------ Block, row and col selection
9907 void wxGrid::SelectRow( int row
, bool addToSelected
)
9909 if ( IsSelection() && !addToSelected
)
9913 m_selection
->SelectRow( row
, false, addToSelected
);
9917 void wxGrid::SelectCol( int col
, bool addToSelected
)
9919 if ( IsSelection() && !addToSelected
)
9923 m_selection
->SelectCol( col
, false, addToSelected
);
9927 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9928 bool addToSelected
)
9930 if ( IsSelection() && !addToSelected
)
9934 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9935 false, addToSelected
);
9939 void wxGrid::SelectAll()
9941 if ( m_numRows
> 0 && m_numCols
> 0 )
9944 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9949 // ------ Cell, row and col deselection
9952 void wxGrid::DeselectRow( int row
)
9957 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9959 if ( m_selection
->IsInSelection(row
, 0 ) )
9960 m_selection
->ToggleCellSelection( row
, 0);
9964 int nCols
= GetNumberCols();
9965 for ( int i
= 0; i
< nCols
; i
++ )
9967 if ( m_selection
->IsInSelection(row
, i
) )
9968 m_selection
->ToggleCellSelection( row
, i
);
9973 void wxGrid::DeselectCol( int col
)
9978 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9980 if ( m_selection
->IsInSelection(0, col
) )
9981 m_selection
->ToggleCellSelection( 0, col
);
9985 int nRows
= GetNumberRows();
9986 for ( int i
= 0; i
< nRows
; i
++ )
9988 if ( m_selection
->IsInSelection(i
, col
) )
9989 m_selection
->ToggleCellSelection(i
, col
);
9994 void wxGrid::DeselectCell( int row
, int col
)
9996 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9997 m_selection
->ToggleCellSelection(row
, col
);
10000 bool wxGrid::IsSelection()
10002 return ( m_selection
&& (m_selection
->IsSelection() ||
10003 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10004 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10007 bool wxGrid::IsInSelection( int row
, int col
) const
10009 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10010 ( row
>= m_selectingTopLeft
.GetRow() &&
10011 col
>= m_selectingTopLeft
.GetCol() &&
10012 row
<= m_selectingBottomRight
.GetRow() &&
10013 col
<= m_selectingBottomRight
.GetCol() )) );
10016 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10018 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10019 return m_selection
->m_cellSelection
;
10021 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10023 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10024 return m_selection
->m_blockSelectionTopLeft
;
10026 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10028 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10029 return m_selection
->m_blockSelectionBottomRight
;
10031 wxArrayInt
wxGrid::GetSelectedRows() const
10033 if (!m_selection
) { wxArrayInt a
; return a
; }
10034 return m_selection
->m_rowSelection
;
10036 wxArrayInt
wxGrid::GetSelectedCols() const
10038 if (!m_selection
) { wxArrayInt a
; return a
; }
10039 return m_selection
->m_colSelection
;
10043 void wxGrid::ClearSelection()
10045 m_selectingTopLeft
= wxGridNoCellCoords
;
10046 m_selectingBottomRight
= wxGridNoCellCoords
;
10048 m_selection
->ClearSelection();
10052 // This function returns the rectangle that encloses the given block
10053 // in device coords clipped to the client size of the grid window.
10055 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10056 const wxGridCellCoords
&bottomRight
)
10058 wxRect
rect( wxGridNoCellRect
);
10061 cellRect
= CellToRect( topLeft
);
10062 if ( cellRect
!= wxGridNoCellRect
)
10068 rect
= wxRect( 0, 0, 0, 0 );
10071 cellRect
= CellToRect( bottomRight
);
10072 if ( cellRect
!= wxGridNoCellRect
)
10078 return wxGridNoCellRect
;
10082 int left
= rect
.GetLeft();
10083 int top
= rect
.GetTop();
10084 int right
= rect
.GetRight();
10085 int bottom
= rect
.GetBottom();
10087 int leftCol
= topLeft
.GetCol();
10088 int topRow
= topLeft
.GetRow();
10089 int rightCol
= bottomRight
.GetCol();
10090 int bottomRow
= bottomRight
.GetRow();
10108 topRow
= bottomRow
;
10113 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10115 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10117 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10119 cellRect
= CellToRect( j
, i
);
10121 if (cellRect
.x
< left
)
10123 if (cellRect
.y
< top
)
10125 if (cellRect
.x
+ cellRect
.width
> right
)
10126 right
= cellRect
.x
+ cellRect
.width
;
10127 if (cellRect
.y
+ cellRect
.height
> bottom
)
10128 bottom
= cellRect
.y
+ cellRect
.height
;
10130 else i
= rightCol
; // jump over inner cells.
10134 // convert to scrolled coords
10136 CalcScrolledPosition( left
, top
, &left
, &top
);
10137 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10140 m_gridWin
->GetClientSize( &cw
, &ch
);
10142 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10143 return wxRect( 0, 0, 0, 0);
10145 rect
.SetLeft( wxMax(0, left
) );
10146 rect
.SetTop( wxMax(0, top
) );
10147 rect
.SetRight( wxMin(cw
, right
) );
10148 rect
.SetBottom( wxMin(ch
, bottom
) );
10154 // ------ Grid event classes
10157 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10159 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10160 int row
, int col
, int x
, int y
, bool sel
,
10161 bool control
, bool shift
, bool alt
, bool meta
)
10162 : wxNotifyEvent( type
, id
)
10169 m_control
= control
;
10174 SetEventObject(obj
);
10178 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10180 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10181 int rowOrCol
, int x
, int y
,
10182 bool control
, bool shift
, bool alt
, bool meta
)
10183 : wxNotifyEvent( type
, id
)
10185 m_rowOrCol
= rowOrCol
;
10188 m_control
= control
;
10193 SetEventObject(obj
);
10197 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10199 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10200 const wxGridCellCoords
& topLeft
,
10201 const wxGridCellCoords
& bottomRight
,
10202 bool sel
, bool control
,
10203 bool shift
, bool alt
, bool meta
)
10204 : wxNotifyEvent( type
, id
)
10206 m_topLeft
= topLeft
;
10207 m_bottomRight
= bottomRight
;
10209 m_control
= control
;
10214 SetEventObject(obj
);
10218 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10220 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10221 wxObject
* obj
, int row
,
10222 int col
, wxControl
* ctrl
)
10223 : wxCommandEvent(type
, id
)
10225 SetEventObject(obj
);
10231 #endif // wxUSE_GRID