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
, -1, 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();
1071 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1072 || keycode
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
)[0u]
1073 || keycode
== WXK_NUMPAD0
1074 || keycode
== WXK_NUMPAD1
1075 || keycode
== WXK_NUMPAD2
1076 || keycode
== WXK_NUMPAD3
1077 || keycode
== WXK_NUMPAD4
1078 || keycode
== WXK_NUMPAD5
1079 || keycode
== WXK_NUMPAD6
1080 || keycode
== WXK_NUMPAD7
1081 || keycode
== WXK_NUMPAD8
1082 || keycode
== WXK_NUMPAD9
1083 || keycode
== WXK_ADD
1084 || keycode
== WXK_NUMPAD_ADD
1085 || keycode
== WXK_SUBTRACT
1086 || keycode
== WXK_NUMPAD_SUBTRACT
)
1088 wxGridCellTextEditor::StartingKey(event
);
1090 // skip Skip() below
1097 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1108 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1112 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1114 m_precision
= (int)tmp
;
1116 // skip the error message below
1121 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1125 wxString
wxGridCellFloatEditor::GetString() const
1128 if ( m_width
== -1 )
1130 // default width/precision
1133 else if ( m_precision
== -1 )
1135 // default precision
1136 fmt
.Printf(_T("%%%d.f"), m_width
);
1140 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1143 return wxString::Format(fmt
, m_valueOld
);
1146 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1148 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1150 int keycode
= event
.GetKeyCode();
1164 case WXK_NUMPAD_ADD
:
1166 case WXK_NUMPAD_SUBTRACT
:
1168 case WXK_NUMPAD_DECIMAL
:
1172 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1173 if ( (keycode
< 128) &&
1174 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1175 keycode
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
)[0u] ||
1176 keycode
== '+' || keycode
== '-') )
1184 #endif // wxUSE_TEXTCTRL
1188 // ----------------------------------------------------------------------------
1189 // wxGridCellBoolEditor
1190 // ----------------------------------------------------------------------------
1192 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1194 wxEvtHandler
* evtHandler
)
1196 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1197 wxDefaultPosition
, wxDefaultSize
,
1200 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1203 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1205 bool resize
= FALSE
;
1206 wxSize size
= m_control
->GetSize();
1207 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1209 // check if the checkbox is not too big/small for this cell
1210 wxSize sizeBest
= m_control
->GetBestSize();
1211 if ( !(size
== sizeBest
) )
1213 // reset to default size if it had been made smaller
1219 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1221 // leave 1 pixel margin
1222 size
.x
= size
.y
= minSize
- 2;
1229 m_control
->SetSize(size
);
1232 // position it in the centre of the rectangle (TODO: support alignment?)
1234 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1235 // the checkbox without label still has some space to the right in wxGTK,
1236 // so shift it to the right
1238 #elif defined(__WXMSW__)
1239 // here too, but in other way
1244 int hAlign
= wxALIGN_CENTRE
;
1245 int vAlign
= wxALIGN_CENTRE
;
1247 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1250 if (hAlign
== wxALIGN_LEFT
)
1256 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1258 else if (hAlign
== wxALIGN_RIGHT
)
1260 x
= r
.x
+ r
.width
- size
.x
- 2;
1261 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1263 else if (hAlign
== wxALIGN_CENTRE
)
1265 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1266 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1269 m_control
->Move(x
, y
);
1272 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1274 m_control
->Show(show
);
1278 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1279 CBox()->SetBackgroundColour(colBg
);
1283 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1285 wxASSERT_MSG(m_control
,
1286 wxT("The wxGridCellEditor must be Created first!"));
1288 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1289 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1292 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1293 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1295 CBox()->SetValue(m_startValue
);
1299 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1302 wxASSERT_MSG(m_control
,
1303 wxT("The wxGridCellEditor must be Created first!"));
1305 bool changed
= FALSE
;
1306 bool value
= CBox()->GetValue();
1307 if ( value
!= m_startValue
)
1312 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1313 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1315 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1321 void wxGridCellBoolEditor::Reset()
1323 wxASSERT_MSG(m_control
,
1324 wxT("The wxGridCellEditor must be Created first!"));
1326 CBox()->SetValue(m_startValue
);
1329 void wxGridCellBoolEditor::StartingClick()
1331 CBox()->SetValue(!CBox()->GetValue());
1334 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1336 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1338 int keycode
= event
.GetKeyCode();
1342 case WXK_NUMPAD_MULTIPLY
:
1344 case WXK_NUMPAD_ADD
:
1346 case WXK_NUMPAD_SUBTRACT
:
1357 // return the value as "1" for true and the empty string for false
1358 wxString
wxGridCellBoolEditor::GetValue() const
1360 bool bSet
= CBox()->GetValue();
1361 return bSet
? _T("1") : wxEmptyString
;
1364 #endif // wxUSE_CHECKBOX
1368 // ----------------------------------------------------------------------------
1369 // wxGridCellChoiceEditor
1370 // ----------------------------------------------------------------------------
1372 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1374 : m_choices(choices
),
1375 m_allowOthers(allowOthers
) { }
1377 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1378 const wxString choices
[],
1380 : m_allowOthers(allowOthers
)
1384 m_choices
.Alloc(count
);
1385 for ( size_t n
= 0; n
< count
; n
++ )
1387 m_choices
.Add(choices
[n
]);
1392 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1394 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1395 editor
->m_allowOthers
= m_allowOthers
;
1396 editor
->m_choices
= m_choices
;
1401 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1403 wxEvtHandler
* evtHandler
)
1405 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1406 wxDefaultPosition
, wxDefaultSize
,
1408 m_allowOthers
? 0 : wxCB_READONLY
);
1410 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1413 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1414 wxGridCellAttr
* attr
)
1416 // as we fill the entire client area, don't do anything here to minimize
1419 // TODO: It doesn't actually fill the client area since the height of a
1420 // combo always defaults to the standard... Until someone has time to
1421 // figure out the right rectangle to paint, just do it the normal way...
1422 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1425 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1427 wxASSERT_MSG(m_control
,
1428 wxT("The wxGridCellEditor must be Created first!"));
1430 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1433 Combo()->SetValue(m_startValue
);
1436 // find the right position, or default to the first if not found
1437 int pos
= Combo()->FindString(m_startValue
);
1440 Combo()->SetSelection(pos
);
1442 Combo()->SetInsertionPointEnd();
1443 Combo()->SetFocus();
1446 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1449 wxString value
= Combo()->GetValue();
1450 bool changed
= value
!= m_startValue
;
1453 grid
->GetTable()->SetValue(row
, col
, value
);
1455 m_startValue
= wxEmptyString
;
1457 Combo()->SetValue(m_startValue
);
1459 Combo()->SetSelection(0);
1464 void wxGridCellChoiceEditor::Reset()
1466 Combo()->SetValue(m_startValue
);
1467 Combo()->SetInsertionPointEnd();
1470 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1480 wxStringTokenizer
tk(params
, _T(','));
1481 while ( tk
.HasMoreTokens() )
1483 m_choices
.Add(tk
.GetNextToken());
1487 // return the value in the text control
1488 wxString
wxGridCellChoiceEditor::GetValue() const
1490 return Combo()->GetValue();
1493 #endif // wxUSE_COMBOBOX
1495 // ----------------------------------------------------------------------------
1496 // wxGridCellEditorEvtHandler
1497 // ----------------------------------------------------------------------------
1499 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1501 switch ( event
.GetKeyCode() )
1505 m_grid
->DisableCellEditControl();
1509 m_grid
->GetEventHandler()->ProcessEvent( event
);
1513 case WXK_NUMPAD_ENTER
:
1514 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1515 m_editor
->HandleReturn(event
);
1524 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1526 switch ( event
.GetKeyCode() )
1531 case WXK_NUMPAD_ENTER
:
1539 // ----------------------------------------------------------------------------
1540 // wxGridCellWorker is an (almost) empty common base class for
1541 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1542 // ----------------------------------------------------------------------------
1544 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1549 wxGridCellWorker::~wxGridCellWorker()
1553 // ============================================================================
1555 // ============================================================================
1557 // ----------------------------------------------------------------------------
1558 // wxGridCellRenderer
1559 // ----------------------------------------------------------------------------
1561 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1562 wxGridCellAttr
& attr
,
1565 int WXUNUSED(row
), int WXUNUSED(col
),
1568 dc
.SetBackgroundMode( wxSOLID
);
1570 // grey out fields if the grid is disabled
1571 if( grid
.IsEnabled() )
1575 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1579 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1584 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1587 dc
.SetPen( *wxTRANSPARENT_PEN
);
1588 dc
.DrawRectangle(rect
);
1591 // ----------------------------------------------------------------------------
1592 // wxGridCellStringRenderer
1593 // ----------------------------------------------------------------------------
1595 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1596 wxGridCellAttr
& attr
,
1600 dc
.SetBackgroundMode( wxTRANSPARENT
);
1602 // TODO some special colours for attr.IsReadOnly() case?
1604 // different coloured text when the grid is disabled
1605 if( grid
.IsEnabled() )
1609 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1610 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1614 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1615 dc
.SetTextForeground( attr
.GetTextColour() );
1620 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1621 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1624 dc
.SetFont( attr
.GetFont() );
1627 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1629 const wxString
& text
)
1631 wxCoord x
= 0, y
= 0, max_x
= 0;
1632 dc
.SetFont(attr
.GetFont());
1633 wxStringTokenizer
tk(text
, _T('\n'));
1634 while ( tk
.HasMoreTokens() )
1636 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1637 max_x
= wxMax(max_x
, x
);
1640 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1642 return wxSize(max_x
, y
);
1645 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1646 wxGridCellAttr
& attr
,
1650 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1653 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1654 wxGridCellAttr
& attr
,
1656 const wxRect
& rectCell
,
1660 wxRect rect
= rectCell
;
1663 // erase only this cells background, overflow cells should have been erased
1664 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1667 attr
.GetAlignment(&hAlign
, &vAlign
);
1669 int overflowCols
= 0;
1671 if (attr
.GetOverflow())
1673 int cols
= grid
.GetNumberCols();
1674 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1675 int cell_rows
, cell_cols
;
1676 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1677 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1679 int i
, c_cols
, c_rows
;
1680 for (i
= col
+cell_cols
; i
< cols
; i
++)
1682 bool is_empty
= TRUE
;
1683 for (int j
=row
; j
<row
+cell_rows
; j
++)
1685 // check w/ anchor cell for multicell block
1686 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1687 if (c_rows
> 0) c_rows
= 0;
1688 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1695 rect
.width
+= grid
.GetColSize(i
);
1701 if (rect
.width
>= best_width
) break;
1703 overflowCols
= i
- col
- cell_cols
+ 1;
1704 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1707 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1709 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1711 clip
.x
+= rectCell
.width
;
1712 // draw each overflow cell individually
1713 int col_end
= col
+cell_cols
+overflowCols
;
1714 if (col_end
>= grid
.GetNumberCols())
1715 col_end
= grid
.GetNumberCols() - 1;
1716 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1718 clip
.width
= grid
.GetColSize(i
) - 1;
1719 dc
.DestroyClippingRegion();
1720 dc
.SetClippingRegion(clip
);
1722 SetTextColoursAndFont(grid
, attr
, dc
,
1723 grid
.IsInSelection(row
,i
));
1725 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1726 rect
, hAlign
, vAlign
);
1727 clip
.x
+= grid
.GetColSize(i
) - 1;
1733 dc
.DestroyClippingRegion();
1737 // now we only have to draw the text
1738 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1740 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1741 rect
, hAlign
, vAlign
);
1744 // ----------------------------------------------------------------------------
1745 // wxGridCellNumberRenderer
1746 // ----------------------------------------------------------------------------
1748 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1750 wxGridTableBase
*table
= grid
.GetTable();
1752 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1754 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1758 text
= table
->GetValue(row
, col
);
1764 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1765 wxGridCellAttr
& attr
,
1767 const wxRect
& rectCell
,
1771 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1773 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1775 // draw the text right aligned by default
1777 attr
.GetAlignment(&hAlign
, &vAlign
);
1778 hAlign
= wxALIGN_RIGHT
;
1780 wxRect rect
= rectCell
;
1783 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1786 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1787 wxGridCellAttr
& attr
,
1791 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1794 // ----------------------------------------------------------------------------
1795 // wxGridCellFloatRenderer
1796 // ----------------------------------------------------------------------------
1798 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1801 SetPrecision(precision
);
1804 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1806 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1807 renderer
->m_width
= m_width
;
1808 renderer
->m_precision
= m_precision
;
1809 renderer
->m_format
= m_format
;
1814 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1816 wxGridTableBase
*table
= grid
.GetTable();
1821 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1823 val
= table
->GetValueAsDouble(row
, col
);
1828 text
= table
->GetValue(row
, col
);
1829 hasDouble
= text
.ToDouble(&val
);
1836 if ( m_width
== -1 )
1838 if ( m_precision
== -1 )
1840 // default width/precision
1841 m_format
= _T("%f");
1845 m_format
.Printf(_T("%%.%df"), m_precision
);
1848 else if ( m_precision
== -1 )
1850 // default precision
1851 m_format
.Printf(_T("%%%d.f"), m_width
);
1855 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1859 text
.Printf(m_format
, val
);
1862 //else: text already contains the string
1867 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1868 wxGridCellAttr
& attr
,
1870 const wxRect
& rectCell
,
1874 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1876 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1878 // draw the text right aligned by default
1880 attr
.GetAlignment(&hAlign
, &vAlign
);
1881 hAlign
= wxALIGN_RIGHT
;
1883 wxRect rect
= rectCell
;
1886 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1889 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1890 wxGridCellAttr
& attr
,
1894 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1897 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1901 // reset to defaults
1907 wxString tmp
= params
.BeforeFirst(_T(','));
1911 if ( tmp
.ToLong(&width
) )
1913 SetWidth((int)width
);
1917 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1921 tmp
= params
.AfterFirst(_T(','));
1925 if ( tmp
.ToLong(&precision
) )
1927 SetPrecision((int)precision
);
1931 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1939 // ----------------------------------------------------------------------------
1940 // wxGridCellBoolRenderer
1941 // ----------------------------------------------------------------------------
1943 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1945 // FIXME these checkbox size calculations are really ugly...
1947 // between checkmark and box
1948 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1950 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1951 wxGridCellAttr
& WXUNUSED(attr
),
1956 // compute it only once (no locks for MT safeness in GUI thread...)
1957 if ( !ms_sizeCheckMark
.x
)
1959 // get checkbox size
1960 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, -1, wxEmptyString
);
1961 wxSize size
= checkbox
->GetBestSize();
1962 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1964 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1965 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1966 checkSize
-= size
.y
/ 2;
1971 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1974 return ms_sizeCheckMark
;
1977 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1978 wxGridCellAttr
& attr
,
1984 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1986 // draw a check mark in the centre (ignoring alignment - TODO)
1987 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1989 // don't draw outside the cell
1990 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1991 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1993 // and even leave (at least) 1 pixel margin
1994 size
.x
= size
.y
= minSize
- 2;
1997 // draw a border around checkmark
1999 attr
.GetAlignment(& hAlign
, &vAlign
);
2002 if (hAlign
== wxALIGN_CENTRE
)
2004 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2005 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2006 rectBorder
.width
= size
.x
;
2007 rectBorder
.height
= size
.y
;
2009 else if (hAlign
== wxALIGN_LEFT
)
2011 rectBorder
.x
= rect
.x
+ 2;
2012 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2013 rectBorder
.width
= size
.x
;
2014 rectBorder
.height
= size
.y
;
2016 else if (hAlign
== wxALIGN_RIGHT
)
2018 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2019 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2020 rectBorder
.width
= size
.x
;
2021 rectBorder
.height
= size
.y
;
2025 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2026 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2029 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2030 value
= !( !cellval
|| (cellval
== wxT("0")) );
2035 wxRect rectMark
= rectBorder
;
2037 // MSW DrawCheckMark() is weird (and should probably be changed...)
2038 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2042 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2045 dc
.SetTextForeground(attr
.GetTextColour());
2046 dc
.DrawCheckMark(rectMark
);
2049 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2050 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2051 dc
.DrawRectangle(rectBorder
);
2054 // ----------------------------------------------------------------------------
2056 // ----------------------------------------------------------------------------
2058 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2062 m_isReadOnly
= Unset
;
2067 m_attrkind
= wxGridCellAttr::Cell
;
2069 m_sizeRows
= m_sizeCols
= 1;
2070 m_overflow
= UnsetOverflow
;
2072 SetDefAttr(attrDefault
);
2075 wxGridCellAttr
*wxGridCellAttr::Clone() const
2077 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2079 if ( HasTextColour() )
2080 attr
->SetTextColour(GetTextColour());
2081 if ( HasBackgroundColour() )
2082 attr
->SetBackgroundColour(GetBackgroundColour());
2084 attr
->SetFont(GetFont());
2085 if ( HasAlignment() )
2086 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2088 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2092 attr
->SetRenderer(m_renderer
);
2093 m_renderer
->IncRef();
2097 attr
->SetEditor(m_editor
);
2102 attr
->SetReadOnly();
2104 attr
->SetKind( m_attrkind
);
2109 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2111 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2112 SetTextColour(mergefrom
->GetTextColour());
2113 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2114 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2115 if ( !HasFont() && mergefrom
->HasFont() )
2116 SetFont(mergefrom
->GetFont());
2117 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2119 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2120 SetAlignment(hAlign
, vAlign
);
2123 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2125 // Directly access member functions as GetRender/Editor don't just return
2126 // m_renderer/m_editor
2128 // Maybe add support for merge of Render and Editor?
2129 if (!HasRenderer() && mergefrom
->HasRenderer() )
2131 m_renderer
= mergefrom
->m_renderer
;
2132 m_renderer
->IncRef();
2134 if ( !HasEditor() && mergefrom
->HasEditor() )
2136 m_editor
= mergefrom
->m_editor
;
2139 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2140 SetReadOnly(mergefrom
->IsReadOnly());
2142 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2143 SetOverflow(mergefrom
->GetOverflow());
2145 SetDefAttr(mergefrom
->m_defGridAttr
);
2148 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2150 // The size of a cell is normally 1,1
2152 // If this cell is larger (2,2) then this is the top left cell
2153 // the other cells that will be covered (lower right cells) must be
2154 // set to negative or zero values such that
2155 // row + num_rows of the covered cell points to the larger cell (this cell)
2156 // same goes for the col + num_cols.
2158 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2160 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2161 !((num_rows
<=0)&&(num_cols
>0)) ||
2162 !((num_rows
==0)&&(num_cols
==0))),
2163 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2165 m_sizeRows
= num_rows
;
2166 m_sizeCols
= num_cols
;
2169 const wxColour
& wxGridCellAttr::GetTextColour() const
2171 if (HasTextColour())
2175 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2177 return m_defGridAttr
->GetTextColour();
2181 wxFAIL_MSG(wxT("Missing default cell attribute"));
2182 return wxNullColour
;
2187 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2189 if (HasBackgroundColour())
2191 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2192 return m_defGridAttr
->GetBackgroundColour();
2195 wxFAIL_MSG(wxT("Missing default cell attribute"));
2196 return wxNullColour
;
2201 const wxFont
& wxGridCellAttr::GetFont() const
2205 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2206 return m_defGridAttr
->GetFont();
2209 wxFAIL_MSG(wxT("Missing default cell attribute"));
2215 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2219 if ( hAlign
) *hAlign
= m_hAlign
;
2220 if ( vAlign
) *vAlign
= m_vAlign
;
2222 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2223 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2226 wxFAIL_MSG(wxT("Missing default cell attribute"));
2230 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2232 if ( num_rows
) *num_rows
= m_sizeRows
;
2233 if ( num_cols
) *num_cols
= m_sizeCols
;
2236 // GetRenderer and GetEditor use a slightly different decision path about
2237 // which attribute to use. If a non-default attr object has one then it is
2238 // used, otherwise the default editor or renderer is fetched from the grid and
2239 // used. It should be the default for the data type of the cell. If it is
2240 // NULL (because the table has a type that the grid does not have in its
2241 // registry,) then the grid's default editor or renderer is used.
2243 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2245 wxGridCellRenderer
*renderer
;
2247 if ( m_renderer
&& this != m_defGridAttr
)
2249 // use the cells renderer if it has one
2250 renderer
= m_renderer
;
2253 else // no non default cell renderer
2255 // get default renderer for the data type
2258 // GetDefaultRendererForCell() will do IncRef() for us
2259 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2268 if (m_defGridAttr
&& this != m_defGridAttr
)
2270 // if we still don't have one then use the grid default
2271 // (no need for IncRef() here neither)
2272 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2274 else // default grid attr
2276 // use m_renderer which we had decided not to use initially
2277 renderer
= m_renderer
;
2284 // we're supposed to always find something
2285 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2290 // same as above, except for s/renderer/editor/g
2291 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2293 wxGridCellEditor
*editor
;
2295 if ( m_editor
&& this != m_defGridAttr
)
2297 // use the cells editor if it has one
2301 else // no non default cell editor
2303 // get default editor for the data type
2306 // GetDefaultEditorForCell() will do IncRef() for us
2307 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2316 if ( m_defGridAttr
&& this != m_defGridAttr
)
2318 // if we still don't have one then use the grid default
2319 // (no need for IncRef() here neither)
2320 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2322 else // default grid attr
2324 // use m_editor which we had decided not to use initially
2332 // we're supposed to always find something
2333 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2338 // ----------------------------------------------------------------------------
2339 // wxGridCellAttrData
2340 // ----------------------------------------------------------------------------
2342 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2344 int n
= FindIndex(row
, col
);
2345 if ( n
== wxNOT_FOUND
)
2347 // add the attribute
2348 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2352 // free the old attribute
2353 m_attrs
[(size_t)n
].attr
->DecRef();
2357 // change the attribute
2358 m_attrs
[(size_t)n
].attr
= attr
;
2362 // remove this attribute
2363 m_attrs
.RemoveAt((size_t)n
);
2368 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2370 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2372 int n
= FindIndex(row
, col
);
2373 if ( n
!= wxNOT_FOUND
)
2375 attr
= m_attrs
[(size_t)n
].attr
;
2382 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2384 size_t count
= m_attrs
.GetCount();
2385 for ( size_t n
= 0; n
< count
; n
++ )
2387 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2388 wxCoord row
= coords
.GetRow();
2389 if ((size_t)row
>= pos
)
2393 // If rows inserted, include row counter where necessary
2394 coords
.SetRow(row
+ numRows
);
2396 else if (numRows
< 0)
2398 // If rows deleted ...
2399 if ((size_t)row
>= pos
- numRows
)
2401 // ...either decrement row counter (if row still exists)...
2402 coords
.SetRow(row
+ numRows
);
2406 // ...or remove the attribute
2407 m_attrs
.RemoveAt((size_t)n
);
2415 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2417 size_t count
= m_attrs
.GetCount();
2418 for ( size_t n
= 0; n
< count
; n
++ )
2420 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2421 wxCoord col
= coords
.GetCol();
2422 if ( (size_t)col
>= pos
)
2426 // If rows inserted, include row counter where necessary
2427 coords
.SetCol(col
+ numCols
);
2429 else if (numCols
< 0)
2431 // If rows deleted ...
2432 if ((size_t)col
>= pos
- numCols
)
2434 // ...either decrement row counter (if row still exists)...
2435 coords
.SetCol(col
+ numCols
);
2439 // ...or remove the attribute
2440 m_attrs
.RemoveAt((size_t)n
);
2448 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2450 size_t count
= m_attrs
.GetCount();
2451 for ( size_t n
= 0; n
< count
; n
++ )
2453 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2454 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2463 // ----------------------------------------------------------------------------
2464 // wxGridRowOrColAttrData
2465 // ----------------------------------------------------------------------------
2467 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2469 size_t count
= m_attrs
.Count();
2470 for ( size_t n
= 0; n
< count
; n
++ )
2472 m_attrs
[n
]->DecRef();
2476 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2478 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2480 int n
= m_rowsOrCols
.Index(rowOrCol
);
2481 if ( n
!= wxNOT_FOUND
)
2483 attr
= m_attrs
[(size_t)n
];
2490 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2492 int i
= m_rowsOrCols
.Index(rowOrCol
);
2493 if ( i
== wxNOT_FOUND
)
2495 // add the attribute
2496 m_rowsOrCols
.Add(rowOrCol
);
2501 size_t n
= (size_t)i
;
2504 // change the attribute
2505 m_attrs
[n
]->DecRef();
2510 // remove this attribute
2511 m_attrs
[n
]->DecRef();
2512 m_rowsOrCols
.RemoveAt(n
);
2513 m_attrs
.RemoveAt(n
);
2518 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2520 size_t count
= m_attrs
.GetCount();
2521 for ( size_t n
= 0; n
< count
; n
++ )
2523 int & rowOrCol
= m_rowsOrCols
[n
];
2524 if ( (size_t)rowOrCol
>= pos
)
2526 if ( numRowsOrCols
> 0 )
2528 // If rows inserted, include row counter where necessary
2529 rowOrCol
+= numRowsOrCols
;
2531 else if ( numRowsOrCols
< 0)
2533 // If rows deleted, either decrement row counter (if row still exists)
2534 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2535 rowOrCol
+= numRowsOrCols
;
2538 m_rowsOrCols
.RemoveAt((size_t)n
);
2539 m_attrs
.RemoveAt((size_t)n
);
2547 // ----------------------------------------------------------------------------
2548 // wxGridCellAttrProvider
2549 // ----------------------------------------------------------------------------
2551 wxGridCellAttrProvider::wxGridCellAttrProvider()
2553 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2556 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2561 void wxGridCellAttrProvider::InitData()
2563 m_data
= new wxGridCellAttrProviderData
;
2566 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2567 wxGridCellAttr::wxAttrKind kind
) const
2569 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2574 case (wxGridCellAttr::Any
):
2575 //Get cached merge attributes.
2576 // Currenlty not used as no cache implemented as not mutiable
2577 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2580 //Basicaly implement old version.
2581 //Also check merge cache, so we don't have to re-merge every time..
2582 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2583 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2584 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2586 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2587 // Two or move are non NULL
2588 attr
= new wxGridCellAttr
;
2589 attr
->SetKind(wxGridCellAttr::Merged
);
2593 attr
->MergeWith(attrcell
);
2597 attr
->MergeWith(attrcol
);
2601 attr
->MergeWith(attrrow
);
2604 //store merge attr if cache implemented
2606 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2610 // one or none is non null return it or null.
2611 if(attrrow
) attr
= attrrow
;
2612 if(attrcol
) attr
= attrcol
;
2613 if(attrcell
) attr
= attrcell
;
2617 case (wxGridCellAttr::Cell
):
2618 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2620 case (wxGridCellAttr::Col
):
2621 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2623 case (wxGridCellAttr::Row
):
2624 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2628 // (wxGridCellAttr::Default):
2629 // (wxGridCellAttr::Merged):
2636 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2642 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2645 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2650 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2653 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2658 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2661 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2665 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2667 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2671 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2675 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2677 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2681 // ----------------------------------------------------------------------------
2682 // wxGridTypeRegistry
2683 // ----------------------------------------------------------------------------
2685 wxGridTypeRegistry::~wxGridTypeRegistry()
2687 size_t count
= m_typeinfo
.Count();
2688 for ( size_t i
= 0; i
< count
; i
++ )
2689 delete m_typeinfo
[i
];
2693 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2694 wxGridCellRenderer
* renderer
,
2695 wxGridCellEditor
* editor
)
2697 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2699 // is it already registered?
2700 int loc
= FindRegisteredDataType(typeName
);
2701 if ( loc
!= wxNOT_FOUND
)
2703 delete m_typeinfo
[loc
];
2704 m_typeinfo
[loc
] = info
;
2708 m_typeinfo
.Add(info
);
2712 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2714 size_t count
= m_typeinfo
.GetCount();
2715 for ( size_t i
= 0; i
< count
; i
++ )
2717 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2726 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2728 int index
= FindRegisteredDataType(typeName
);
2729 if ( index
== wxNOT_FOUND
)
2731 // check whether this is one of the standard ones, in which case
2732 // register it "on the fly"
2734 if ( typeName
== wxGRID_VALUE_STRING
)
2736 RegisterDataType(wxGRID_VALUE_STRING
,
2737 new wxGridCellStringRenderer
,
2738 new wxGridCellTextEditor
);
2740 #endif // wxUSE_TEXTCTRL
2742 if ( typeName
== wxGRID_VALUE_BOOL
)
2744 RegisterDataType(wxGRID_VALUE_BOOL
,
2745 new wxGridCellBoolRenderer
,
2746 new wxGridCellBoolEditor
);
2748 #endif // wxUSE_CHECKBOX
2750 if ( typeName
== wxGRID_VALUE_NUMBER
)
2752 RegisterDataType(wxGRID_VALUE_NUMBER
,
2753 new wxGridCellNumberRenderer
,
2754 new wxGridCellNumberEditor
);
2756 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2758 RegisterDataType(wxGRID_VALUE_FLOAT
,
2759 new wxGridCellFloatRenderer
,
2760 new wxGridCellFloatEditor
);
2762 #endif // wxUSE_TEXTCTRL
2764 if ( typeName
== wxGRID_VALUE_CHOICE
)
2766 RegisterDataType(wxGRID_VALUE_CHOICE
,
2767 new wxGridCellStringRenderer
,
2768 new wxGridCellChoiceEditor
);
2770 #endif // wxUSE_COMBOBOX
2775 // we get here only if just added the entry for this type, so return
2777 index
= m_typeinfo
.GetCount() - 1;
2783 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2785 int index
= FindDataType(typeName
);
2786 if ( index
== wxNOT_FOUND
)
2788 // the first part of the typename is the "real" type, anything after ':'
2789 // are the parameters for the renderer
2790 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2791 if ( index
== wxNOT_FOUND
)
2796 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2797 wxGridCellRenderer
*rendererOld
= renderer
;
2798 renderer
= renderer
->Clone();
2799 rendererOld
->DecRef();
2801 wxGridCellEditor
*editor
= GetEditor(index
);
2802 wxGridCellEditor
*editorOld
= editor
;
2803 editor
= editor
->Clone();
2804 editorOld
->DecRef();
2806 // do it even if there are no parameters to reset them to defaults
2807 wxString params
= typeName
.AfterFirst(_T(':'));
2808 renderer
->SetParameters(params
);
2809 editor
->SetParameters(params
);
2811 // register the new typename
2812 RegisterDataType(typeName
, renderer
, editor
);
2814 // we just registered it, it's the last one
2815 index
= m_typeinfo
.GetCount() - 1;
2821 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2823 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2829 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2831 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2837 // ----------------------------------------------------------------------------
2839 // ----------------------------------------------------------------------------
2841 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2844 wxGridTableBase::wxGridTableBase()
2846 m_view
= (wxGrid
*) NULL
;
2847 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2850 wxGridTableBase::~wxGridTableBase()
2852 delete m_attrProvider
;
2855 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2857 delete m_attrProvider
;
2858 m_attrProvider
= attrProvider
;
2861 bool wxGridTableBase::CanHaveAttributes()
2863 if ( ! GetAttrProvider() )
2865 // use the default attr provider by default
2866 SetAttrProvider(new wxGridCellAttrProvider
);
2871 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2873 if ( m_attrProvider
)
2874 return m_attrProvider
->GetAttr(row
, col
, kind
);
2876 return (wxGridCellAttr
*)NULL
;
2879 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2881 if ( m_attrProvider
)
2883 attr
->SetKind(wxGridCellAttr::Cell
);
2884 m_attrProvider
->SetAttr(attr
, row
, col
);
2888 // as we take ownership of the pointer and don't store it, we must
2894 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2896 if ( m_attrProvider
)
2898 attr
->SetKind(wxGridCellAttr::Row
);
2899 m_attrProvider
->SetRowAttr(attr
, row
);
2903 // as we take ownership of the pointer and don't store it, we must
2909 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2911 if ( m_attrProvider
)
2913 attr
->SetKind(wxGridCellAttr::Col
);
2914 m_attrProvider
->SetColAttr(attr
, col
);
2918 // as we take ownership of the pointer and don't store it, we must
2924 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2925 size_t WXUNUSED(numRows
) )
2927 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2932 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2934 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2939 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2940 size_t WXUNUSED(numRows
) )
2942 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2947 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2948 size_t WXUNUSED(numCols
) )
2950 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2955 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2957 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2962 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2963 size_t WXUNUSED(numCols
) )
2965 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2971 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2974 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2975 // how much it makes sense to us geeks.
2979 wxString
wxGridTableBase::GetColLabelValue( int col
)
2981 // default col labels are:
2982 // cols 0 to 25 : A-Z
2983 // cols 26 to 675 : AA-ZZ
2988 for ( n
= 1; ; n
++ )
2990 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
2992 if ( col
< 0 ) break;
2995 // reverse the string...
2997 for ( i
= 0; i
< n
; i
++ )
3006 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3008 return wxGRID_VALUE_STRING
;
3011 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3012 const wxString
& typeName
)
3014 return typeName
== wxGRID_VALUE_STRING
;
3017 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3019 return CanGetValueAs(row
, col
, typeName
);
3022 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3027 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3032 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3037 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3038 long WXUNUSED(value
) )
3042 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3043 double WXUNUSED(value
) )
3047 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3048 bool WXUNUSED(value
) )
3053 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3054 const wxString
& WXUNUSED(typeName
) )
3059 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3060 const wxString
& WXUNUSED(typeName
),
3061 void* WXUNUSED(value
) )
3065 //////////////////////////////////////////////////////////////////////
3067 // Message class for the grid table to send requests and notifications
3071 wxGridTableMessage::wxGridTableMessage()
3073 m_table
= (wxGridTableBase
*) NULL
;
3079 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3080 int commandInt1
, int commandInt2
)
3084 m_comInt1
= commandInt1
;
3085 m_comInt2
= commandInt2
;
3090 //////////////////////////////////////////////////////////////////////
3092 // A basic grid table for string data. An object of this class will
3093 // created by wxGrid if you don't specify an alternative table class.
3096 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3098 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3100 wxGridStringTable::wxGridStringTable()
3105 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3108 m_data
.Alloc( numRows
);
3111 sa
.Alloc( numCols
);
3112 sa
.Add( wxEmptyString
, numCols
);
3114 m_data
.Add( sa
, numRows
);
3117 wxGridStringTable::~wxGridStringTable()
3121 int wxGridStringTable::GetNumberRows()
3123 return m_data
.GetCount();
3126 int wxGridStringTable::GetNumberCols()
3128 if ( m_data
.GetCount() > 0 )
3129 return m_data
[0].GetCount();
3134 wxString
wxGridStringTable::GetValue( int row
, int col
)
3136 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3138 _T("invalid row or column index in wxGridStringTable") );
3140 return m_data
[row
][col
];
3143 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3145 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3146 _T("invalid row or column index in wxGridStringTable") );
3148 m_data
[row
][col
] = value
;
3151 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3153 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3155 _T("invalid row or column index in wxGridStringTable") );
3157 return (m_data
[row
][col
] == wxEmptyString
);
3160 void wxGridStringTable::Clear()
3163 int numRows
, numCols
;
3165 numRows
= m_data
.GetCount();
3168 numCols
= m_data
[0].GetCount();
3170 for ( row
= 0; row
< numRows
; row
++ )
3172 for ( col
= 0; col
< numCols
; col
++ )
3174 m_data
[row
][col
] = wxEmptyString
;
3181 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3183 size_t curNumRows
= m_data
.GetCount();
3184 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3185 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3187 if ( pos
>= curNumRows
)
3189 return AppendRows( numRows
);
3193 sa
.Alloc( curNumCols
);
3194 sa
.Add( wxEmptyString
, curNumCols
);
3195 m_data
.Insert( sa
, pos
, numRows
);
3198 wxGridTableMessage
msg( this,
3199 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3203 GetView()->ProcessTableMessage( msg
);
3209 bool wxGridStringTable::AppendRows( size_t numRows
)
3211 size_t curNumRows
= m_data
.GetCount();
3212 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3213 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3216 if ( curNumCols
> 0 )
3218 sa
.Alloc( curNumCols
);
3219 sa
.Add( wxEmptyString
, curNumCols
);
3222 m_data
.Add( sa
, numRows
);
3226 wxGridTableMessage
msg( this,
3227 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3230 GetView()->ProcessTableMessage( msg
);
3236 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3238 size_t curNumRows
= m_data
.GetCount();
3240 if ( pos
>= curNumRows
)
3242 wxFAIL_MSG( wxString::Format
3244 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3246 (unsigned long)numRows
,
3247 (unsigned long)curNumRows
3253 if ( numRows
> curNumRows
- pos
)
3255 numRows
= curNumRows
- pos
;
3258 if ( numRows
>= curNumRows
)
3264 m_data
.RemoveAt( pos
, numRows
);
3268 wxGridTableMessage
msg( this,
3269 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3273 GetView()->ProcessTableMessage( msg
);
3279 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3283 size_t curNumRows
= m_data
.GetCount();
3284 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3285 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3287 if ( pos
>= curNumCols
)
3289 return AppendCols( numCols
);
3292 for ( row
= 0; row
< curNumRows
; row
++ )
3294 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3296 m_data
[row
].Insert( wxEmptyString
, col
);
3301 wxGridTableMessage
msg( this,
3302 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3306 GetView()->ProcessTableMessage( msg
);
3312 bool wxGridStringTable::AppendCols( size_t numCols
)
3316 size_t curNumRows
= m_data
.GetCount();
3320 // TODO: something better than this ?
3322 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3327 for ( row
= 0; row
< curNumRows
; row
++ )
3329 m_data
[row
].Add( wxEmptyString
, numCols
);
3334 wxGridTableMessage
msg( this,
3335 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3338 GetView()->ProcessTableMessage( msg
);
3344 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3348 size_t curNumRows
= m_data
.GetCount();
3349 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3350 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3352 if ( pos
>= curNumCols
)
3354 wxFAIL_MSG( wxString::Format
3356 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3358 (unsigned long)numCols
,
3359 (unsigned long)curNumCols
3364 if ( numCols
> curNumCols
- pos
)
3366 numCols
= curNumCols
- pos
;
3369 for ( row
= 0; row
< curNumRows
; row
++ )
3371 if ( numCols
>= curNumCols
)
3373 m_data
[row
].Clear();
3377 m_data
[row
].RemoveAt( pos
, numCols
);
3382 wxGridTableMessage
msg( this,
3383 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3387 GetView()->ProcessTableMessage( msg
);
3393 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3395 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3397 // using default label
3399 return wxGridTableBase::GetRowLabelValue( row
);
3403 return m_rowLabels
[ row
];
3407 wxString
wxGridStringTable::GetColLabelValue( int col
)
3409 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3411 // using default label
3413 return wxGridTableBase::GetColLabelValue( col
);
3417 return m_colLabels
[ col
];
3421 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3423 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3425 int n
= m_rowLabels
.GetCount();
3427 for ( i
= n
; i
<= row
; i
++ )
3429 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3433 m_rowLabels
[row
] = value
;
3436 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3438 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3440 int n
= m_colLabels
.GetCount();
3442 for ( i
= n
; i
<= col
; i
++ )
3444 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3448 m_colLabels
[col
] = value
;
3453 //////////////////////////////////////////////////////////////////////
3454 //////////////////////////////////////////////////////////////////////
3456 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3458 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3459 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3460 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3461 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3462 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3463 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3466 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3468 const wxPoint
&pos
, const wxSize
&size
)
3469 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3474 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3478 // NO - don't do this because it will set both the x and y origin
3479 // coords to match the parent scrolled window and we just want to
3480 // set the y coord - MB
3482 // m_owner->PrepareDC( dc );
3485 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3486 dc
.SetDeviceOrigin( 0, -y
);
3488 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3489 m_owner
->DrawRowLabels( dc
, rows
);
3493 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3495 m_owner
->ProcessRowLabelMouseEvent( event
);
3499 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3501 m_owner
->GetEventHandler()->ProcessEvent(event
);
3505 // This seems to be required for wxMotif otherwise the mouse
3506 // cursor must be in the cell edit control to get key events
3508 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3510 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3513 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3515 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3520 //////////////////////////////////////////////////////////////////////
3522 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3524 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3525 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3526 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3527 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3528 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3529 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3532 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3534 const wxPoint
&pos
, const wxSize
&size
)
3535 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3540 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3544 // NO - don't do this because it will set both the x and y origin
3545 // coords to match the parent scrolled window and we just want to
3546 // set the x coord - MB
3548 // m_owner->PrepareDC( dc );
3551 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3552 dc
.SetDeviceOrigin( -x
, 0 );
3554 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3555 m_owner
->DrawColLabels( dc
, cols
);
3559 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3561 m_owner
->ProcessColLabelMouseEvent( event
);
3564 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3566 m_owner
->GetEventHandler()->ProcessEvent(event
);
3570 // This seems to be required for wxMotif otherwise the mouse
3571 // cursor must be in the cell edit control to get key events
3573 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3575 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3578 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3580 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3585 //////////////////////////////////////////////////////////////////////
3587 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3589 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3590 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3591 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3592 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3593 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3594 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3597 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3599 const wxPoint
&pos
, const wxSize
&size
)
3600 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
)
3605 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3609 int client_height
= 0;
3610 int client_width
= 0;
3611 GetClientSize( &client_width
, &client_height
);
3613 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3614 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3615 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3616 dc
.DrawLine( 0, 0, client_width
, 0 );
3617 dc
.DrawLine( 0, 0, 0, client_height
);
3619 dc
.SetPen( *wxWHITE_PEN
);
3620 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3621 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3625 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3627 m_owner
->ProcessCornerLabelMouseEvent( event
);
3631 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3633 m_owner
->GetEventHandler()->ProcessEvent(event
);
3636 // This seems to be required for wxMotif otherwise the mouse
3637 // cursor must be in the cell edit control to get key events
3639 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3641 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3644 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3646 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3651 //////////////////////////////////////////////////////////////////////
3653 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3655 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3656 EVT_PAINT( wxGridWindow::OnPaint
)
3657 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3658 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3659 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3660 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3661 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3662 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3663 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3666 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3667 wxGridRowLabelWindow
*rowLblWin
,
3668 wxGridColLabelWindow
*colLblWin
,
3671 const wxSize
&size
)
3672 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
,
3673 wxT("grid window") )
3677 m_rowLabelWin
= rowLblWin
;
3678 m_colLabelWin
= colLblWin
;
3679 SetBackgroundColour(_T("WHITE"));
3683 wxGridWindow::~wxGridWindow()
3688 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3690 wxPaintDC
dc( this );
3691 m_owner
->PrepareDC( dc
);
3692 wxRegion reg
= GetUpdateRegion();
3693 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3694 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3695 #if WXGRID_DRAW_LINES
3696 m_owner
->DrawAllGridLines( dc
, reg
);
3698 m_owner
->DrawGridSpace( dc
);
3699 m_owner
->DrawHighlight( dc
, DirtyCells
);
3703 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3705 wxWindow::ScrollWindow( dx
, dy
, rect
);
3706 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3707 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3711 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3713 m_owner
->ProcessGridCellMouseEvent( event
);
3716 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3718 m_owner
->GetEventHandler()->ProcessEvent(event
);
3721 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3722 // cursor must be in the cell edit control to get key events
3724 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3726 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3729 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3731 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3734 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3738 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3740 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3744 //////////////////////////////////////////////////////////////////////
3746 // Internal Helper function for computing row or column from some
3747 // (unscrolled) coordinate value, using either
3748 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3749 // of m_rowBottoms/m_ColRights to speed up the search!
3751 // Internal helper macros for simpler use of that function
3753 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3754 const wxArrayInt
& BorderArray
, int nMax
,
3757 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3758 m_minAcceptableColWidth, \
3759 m_colRights, m_numCols, TRUE)
3760 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3761 m_minAcceptableRowHeight, \
3762 m_rowBottoms, m_numRows, TRUE)
3763 /////////////////////////////////////////////////////////////////////
3765 #if wxUSE_EXTENDED_RTTI
3766 WX_DEFINE_FLAGS( wxGridStyle
)
3768 wxBEGIN_FLAGS( wxGridStyle
)
3769 // new style border flags, we put them first to
3770 // use them for streaming out
3771 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3772 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3773 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3774 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3775 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3776 wxFLAGS_MEMBER(wxBORDER_NONE
)
3778 // old style border flags
3779 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3780 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3781 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3782 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3783 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3784 wxFLAGS_MEMBER(wxBORDER
)
3786 // standard window styles
3787 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3788 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3789 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3790 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3791 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3792 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3793 wxFLAGS_MEMBER(wxVSCROLL
)
3794 wxFLAGS_MEMBER(wxHSCROLL
)
3796 wxEND_FLAGS( wxGridStyle
)
3798 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3800 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3801 wxHIDE_PROPERTY( Children
)
3802 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3803 wxEND_PROPERTIES_TABLE()
3805 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3806 wxEND_HANDLERS_TABLE()
3808 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3811 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3814 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3817 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3818 EVT_PAINT( wxGrid::OnPaint
)
3819 EVT_SIZE( wxGrid::OnSize
)
3820 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3821 EVT_KEY_UP( wxGrid::OnKeyUp
)
3822 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3827 // in order to make sure that a size event is not
3828 // trigerred in a unfinished state
3829 m_cornerLabelWin
= NULL
;
3830 m_rowLabelWin
= NULL
;
3831 m_colLabelWin
= NULL
;
3835 wxGrid::wxGrid( wxWindow
*parent
,
3840 const wxString
& name
)
3841 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3842 m_colMinWidths(GRID_HASH_SIZE
),
3843 m_rowMinHeights(GRID_HASH_SIZE
)
3848 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3849 const wxPoint
& pos
, const wxSize
& size
,
3850 long style
, const wxString
& name
)
3852 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3853 style
| wxWANTS_CHARS
, name
))
3856 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3857 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3868 // Must do this or ~wxScrollHelper will pop the wrong event handler
3869 SetTargetWindow(this);
3871 wxSafeDecRef(m_defaultCellAttr
);
3873 #ifdef DEBUG_ATTR_CACHE
3874 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3875 wxPrintf(_T("wxGrid attribute cache statistics: "
3876 "total: %u, hits: %u (%u%%)\n"),
3877 total
, gs_nAttrCacheHits
,
3878 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3884 delete m_typeRegistry
;
3890 // ----- internal init and update functions
3893 void wxGrid::Create()
3895 m_created
= FALSE
; // set to TRUE by CreateGrid
3897 m_table
= (wxGridTableBase
*) NULL
;
3900 m_cellEditCtrlEnabled
= FALSE
;
3902 m_defaultCellAttr
= new wxGridCellAttr();
3904 // Set default cell attributes
3905 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3906 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3907 m_defaultCellAttr
->SetFont(GetFont());
3908 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3909 m_defaultCellAttr
->SetTextColour(
3910 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3911 m_defaultCellAttr
->SetBackgroundColour(
3912 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3913 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3914 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3919 m_currentCellCoords
= wxGridNoCellCoords
;
3921 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3922 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3924 // create the type registry
3925 m_typeRegistry
= new wxGridTypeRegistry
;
3928 // subwindow components that make up the wxGrid
3929 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3934 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3939 m_colLabelWin
= new wxGridColLabelWindow( this,
3944 m_gridWin
= new wxGridWindow( this,
3951 SetTargetWindow( m_gridWin
);
3957 bool wxGrid::CreateGrid( int numRows
, int numCols
,
3958 wxGrid::wxGridSelectionModes selmode
)
3960 wxCHECK_MSG( !m_created
,
3962 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
3964 m_numRows
= numRows
;
3965 m_numCols
= numCols
;
3967 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
3968 m_table
->SetView( this );
3970 m_selection
= new wxGridSelection( this, selmode
);
3979 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
3981 wxCHECK_RET( m_created
,
3982 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
3984 m_selection
->SetSelectionMode( selmode
);
3987 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
3989 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
3990 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
3992 return m_selection
->GetSelectionMode();
3995 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
3996 wxGrid::wxGridSelectionModes selmode
)
4000 // stop all processing
4005 wxGridTableBase
*t
=m_table
;
4018 m_numRows
= table
->GetNumberRows();
4019 m_numCols
= table
->GetNumberCols();
4022 m_table
->SetView( this );
4025 m_selection
= new wxGridSelection( this, selmode
);
4038 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4039 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4041 if ( m_rowLabelWin
)
4043 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4047 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4050 m_labelTextColour
= wxColour( _T("BLACK") );
4053 m_attrCache
.row
= -1;
4054 m_attrCache
.col
= -1;
4055 m_attrCache
.attr
= NULL
;
4057 // TODO: something better than this ?
4059 m_labelFont
= this->GetFont();
4060 m_labelFont
.SetWeight( wxBOLD
);
4062 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4063 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4065 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4066 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4067 m_colLabelTextOrientation
= wxHORIZONTAL
;
4069 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4070 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4072 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4073 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4075 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4076 m_defaultRowHeight
+= 8;
4078 m_defaultRowHeight
+= 4;
4081 m_gridLineColour
= wxColour( 192,192,192 );
4082 m_gridLinesEnabled
= TRUE
;
4083 m_cellHighlightColour
= *wxBLACK
;
4084 m_cellHighlightPenWidth
= 2;
4085 m_cellHighlightROPenWidth
= 1;
4087 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4088 m_winCapture
= (wxWindow
*)NULL
;
4089 m_canDragRowSize
= TRUE
;
4090 m_canDragColSize
= TRUE
;
4091 m_canDragGridSize
= TRUE
;
4093 m_dragRowOrCol
= -1;
4094 m_isDragging
= FALSE
;
4095 m_startDragPos
= wxDefaultPosition
;
4097 m_waitForSlowClick
= FALSE
;
4099 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4100 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4102 m_currentCellCoords
= wxGridNoCellCoords
;
4104 m_selectingTopLeft
= wxGridNoCellCoords
;
4105 m_selectingBottomRight
= wxGridNoCellCoords
;
4106 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4107 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4109 m_editable
= TRUE
; // default for whole grid
4111 m_inOnKeyDown
= FALSE
;
4118 // ----------------------------------------------------------------------------
4119 // the idea is to call these functions only when necessary because they create
4120 // quite big arrays which eat memory mostly unnecessary - in particular, if
4121 // default widths/heights are used for all rows/columns, we may not use these
4124 // with some extra code, it should be possible to only store the
4125 // widths/heights different from default ones but this will be done later...
4126 // ----------------------------------------------------------------------------
4128 void wxGrid::InitRowHeights()
4130 m_rowHeights
.Empty();
4131 m_rowBottoms
.Empty();
4133 m_rowHeights
.Alloc( m_numRows
);
4134 m_rowBottoms
.Alloc( m_numRows
);
4138 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4140 for ( int i
= 0; i
< m_numRows
; i
++ )
4142 rowBottom
+= m_defaultRowHeight
;
4143 m_rowBottoms
.Add( rowBottom
);
4147 void wxGrid::InitColWidths()
4149 m_colWidths
.Empty();
4150 m_colRights
.Empty();
4152 m_colWidths
.Alloc( m_numCols
);
4153 m_colRights
.Alloc( m_numCols
);
4156 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4158 for ( int i
= 0; i
< m_numCols
; i
++ )
4160 colRight
+= m_defaultColWidth
;
4161 m_colRights
.Add( colRight
);
4165 int wxGrid::GetColWidth(int col
) const
4167 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4170 int wxGrid::GetColLeft(int col
) const
4172 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4173 : m_colRights
[col
] - m_colWidths
[col
];
4176 int wxGrid::GetColRight(int col
) const
4178 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4182 int wxGrid::GetRowHeight(int row
) const
4184 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4187 int wxGrid::GetRowTop(int row
) const
4189 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4190 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4193 int wxGrid::GetRowBottom(int row
) const
4195 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4196 : m_rowBottoms
[row
];
4199 void wxGrid::CalcDimensions()
4202 GetClientSize( &cw
, &ch
);
4204 if ( m_rowLabelWin
->IsShown() )
4205 cw
-= m_rowLabelWidth
;
4206 if ( m_colLabelWin
->IsShown() )
4207 ch
-= m_colLabelHeight
;
4210 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4211 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4213 // take into account editor if shown
4214 if( IsCellEditControlShown() )
4217 int r
= m_currentCellCoords
.GetRow();
4218 int c
= m_currentCellCoords
.GetCol();
4219 int x
= GetColLeft(c
);
4220 int y
= GetRowTop(r
);
4222 // how big is the editor
4223 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4224 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4225 editor
->GetControl()->GetSize(&w2
, &h2
);
4228 if( w2
> w
) w
= w2
;
4229 if( h2
> h
) h
= h2
;
4234 // preserve (more or less) the previous position
4236 GetViewStart( &x
, &y
);
4238 // ensure the position is valid for the new scroll ranges
4240 x
= wxMax( w
- 1, 0 );
4242 y
= wxMax( h
- 1, 0 );
4244 // do set scrollbar parameters
4245 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4246 GetScrollX(w
), GetScrollY(h
), x
, y
,
4247 GetBatchCount() != 0);
4249 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4250 // still must reposition the children
4255 void wxGrid::CalcWindowSizes()
4257 // escape if the window is has not been fully created yet
4259 if ( m_cornerLabelWin
== NULL
)
4263 GetClientSize( &cw
, &ch
);
4265 if ( m_cornerLabelWin
->IsShown() )
4266 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4268 if ( m_colLabelWin
->IsShown() )
4269 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4271 if ( m_rowLabelWin
->IsShown() )
4272 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4274 if ( m_gridWin
->IsShown() )
4275 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4279 // this is called when the grid table sends a message to say that it
4280 // has been redimensioned
4282 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4285 bool result
= FALSE
;
4287 // Clear the attribute cache as the attribute might refer to a different
4288 // cell than stored in the cache after adding/removing rows/columns.
4290 // By the same reasoning, the editor should be dismissed if columns are
4291 // added or removed. And for consistency, it should IMHO always be
4292 // removed, not only if the cell "underneath" it actually changes.
4293 // For now, I intentionally do not save the editor's content as the
4294 // cell it might want to save that stuff to might no longer exist.
4295 HideCellEditControl();
4297 // if we were using the default widths/heights so far, we must change them
4299 if ( m_colWidths
.IsEmpty() )
4304 if ( m_rowHeights
.IsEmpty() )
4310 switch ( msg
.GetId() )
4312 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4314 size_t pos
= msg
.GetCommandInt();
4315 int numRows
= msg
.GetCommandInt2();
4317 m_numRows
+= numRows
;
4319 if ( !m_rowHeights
.IsEmpty() )
4321 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4322 m_rowBottoms
.Insert( 0, pos
, numRows
);
4325 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4327 for ( i
= pos
; i
< m_numRows
; i
++ )
4329 bottom
+= m_rowHeights
[i
];
4330 m_rowBottoms
[i
] = bottom
;
4333 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4335 // if we have just inserted cols into an empty grid the current
4336 // cell will be undefined...
4338 SetCurrentCell( 0, 0 );
4342 m_selection
->UpdateRows( pos
, numRows
);
4343 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4345 attrProvider
->UpdateAttrRows( pos
, numRows
);
4347 if ( !GetBatchCount() )
4350 m_rowLabelWin
->Refresh();
4356 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4358 int numRows
= msg
.GetCommandInt();
4359 int oldNumRows
= m_numRows
;
4360 m_numRows
+= numRows
;
4362 if ( !m_rowHeights
.IsEmpty() )
4364 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4365 m_rowBottoms
.Add( 0, numRows
);
4368 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4370 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4372 bottom
+= m_rowHeights
[i
];
4373 m_rowBottoms
[i
] = bottom
;
4376 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4378 // if we have just inserted cols into an empty grid the current
4379 // cell will be undefined...
4381 SetCurrentCell( 0, 0 );
4383 if ( !GetBatchCount() )
4386 m_rowLabelWin
->Refresh();
4392 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4394 size_t pos
= msg
.GetCommandInt();
4395 int numRows
= msg
.GetCommandInt2();
4396 m_numRows
-= numRows
;
4398 if ( !m_rowHeights
.IsEmpty() )
4400 m_rowHeights
.RemoveAt( pos
, numRows
);
4401 m_rowBottoms
.RemoveAt( pos
, numRows
);
4404 for ( i
= 0; i
< m_numRows
; i
++ )
4406 h
+= m_rowHeights
[i
];
4407 m_rowBottoms
[i
] = h
;
4412 m_currentCellCoords
= wxGridNoCellCoords
;
4416 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4417 m_currentCellCoords
.Set( 0, 0 );
4421 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4422 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4424 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4425 // ifdef'd out following patch from Paul Gammans
4427 // No need to touch column attributes, unless we
4428 // removed _all_ rows, in this case, we remove
4429 // all column attributes.
4430 // I hate to do this here, but the
4431 // needed data is not available inside UpdateAttrRows.
4432 if ( !GetNumberRows() )
4433 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4436 if ( !GetBatchCount() )
4439 m_rowLabelWin
->Refresh();
4445 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4447 size_t pos
= msg
.GetCommandInt();
4448 int numCols
= msg
.GetCommandInt2();
4449 m_numCols
+= numCols
;
4451 if ( !m_colWidths
.IsEmpty() )
4453 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4454 m_colRights
.Insert( 0, pos
, numCols
);
4457 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4459 for ( i
= pos
; i
< m_numCols
; i
++ )
4461 right
+= m_colWidths
[i
];
4462 m_colRights
[i
] = right
;
4465 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4467 // if we have just inserted cols into an empty grid the current
4468 // cell will be undefined...
4470 SetCurrentCell( 0, 0 );
4474 m_selection
->UpdateCols( pos
, numCols
);
4475 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4477 attrProvider
->UpdateAttrCols( pos
, numCols
);
4478 if ( !GetBatchCount() )
4481 m_colLabelWin
->Refresh();
4488 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4490 int numCols
= msg
.GetCommandInt();
4491 int oldNumCols
= m_numCols
;
4492 m_numCols
+= numCols
;
4493 if ( !m_colWidths
.IsEmpty() )
4495 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4496 m_colRights
.Add( 0, numCols
);
4499 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4501 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4503 right
+= m_colWidths
[i
];
4504 m_colRights
[i
] = right
;
4507 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4509 // if we have just inserted cols into an empty grid the current
4510 // cell will be undefined...
4512 SetCurrentCell( 0, 0 );
4514 if ( !GetBatchCount() )
4517 m_colLabelWin
->Refresh();
4523 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4525 size_t pos
= msg
.GetCommandInt();
4526 int numCols
= msg
.GetCommandInt2();
4527 m_numCols
-= numCols
;
4529 if ( !m_colWidths
.IsEmpty() )
4531 m_colWidths
.RemoveAt( pos
, numCols
);
4532 m_colRights
.RemoveAt( pos
, numCols
);
4535 for ( i
= 0; i
< m_numCols
; i
++ )
4537 w
+= m_colWidths
[i
];
4543 m_currentCellCoords
= wxGridNoCellCoords
;
4547 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4548 m_currentCellCoords
.Set( 0, 0 );
4552 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4553 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4555 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4556 // ifdef'd out following patch from Paul Gammans
4558 // No need to touch row attributes, unless we
4559 // removed _all_ columns, in this case, we remove
4560 // all row attributes.
4561 // I hate to do this here, but the
4562 // needed data is not available inside UpdateAttrCols.
4563 if ( !GetNumberCols() )
4564 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4567 if ( !GetBatchCount() )
4570 m_colLabelWin
->Refresh();
4577 if (result
&& !GetBatchCount() )
4578 m_gridWin
->Refresh();
4583 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4585 wxRegionIterator
iter( reg
);
4588 wxArrayInt rowlabels
;
4595 // TODO: remove this when we can...
4596 // There is a bug in wxMotif that gives garbage update
4597 // rectangles if you jump-scroll a long way by clicking the
4598 // scrollbar with middle button. This is a work-around
4600 #if defined(__WXMOTIF__)
4602 m_gridWin
->GetClientSize( &cw
, &ch
);
4603 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4604 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4607 // logical bounds of update region
4610 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4611 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4613 // find the row labels within these bounds
4616 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4618 if ( GetRowBottom(row
) < top
)
4621 if ( GetRowTop(row
) > bottom
)
4624 rowlabels
.Add( row
);
4634 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4636 wxRegionIterator
iter( reg
);
4639 wxArrayInt colLabels
;
4646 // TODO: remove this when we can...
4647 // There is a bug in wxMotif that gives garbage update
4648 // rectangles if you jump-scroll a long way by clicking the
4649 // scrollbar with middle button. This is a work-around
4651 #if defined(__WXMOTIF__)
4653 m_gridWin
->GetClientSize( &cw
, &ch
);
4654 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4655 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4658 // logical bounds of update region
4661 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4662 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4664 // find the cells within these bounds
4667 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4669 if ( GetColRight(col
) < left
)
4672 if ( GetColLeft(col
) > right
)
4675 colLabels
.Add( col
);
4684 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4686 wxRegionIterator
iter( reg
);
4689 wxGridCellCoordsArray cellsExposed
;
4691 int left
, top
, right
, bottom
;
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
.GetTop() > ch
) r
.SetTop( 0 );
4705 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4706 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4707 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4710 // logical bounds of update region
4712 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4713 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4715 // find the cells within these bounds
4718 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4720 if ( GetRowBottom(row
) <= top
)
4723 if ( GetRowTop(row
) > bottom
)
4726 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4728 if ( GetColRight(col
) <= left
)
4731 if ( GetColLeft(col
) > right
)
4734 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4741 return cellsExposed
;
4745 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4748 wxPoint
pos( event
.GetPosition() );
4749 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4751 if ( event
.Dragging() )
4755 m_isDragging
= TRUE
;
4756 m_rowLabelWin
->CaptureMouse();
4759 if ( event
.LeftIsDown() )
4761 switch( m_cursorMode
)
4763 case WXGRID_CURSOR_RESIZE_ROW
:
4765 int cw
, ch
, left
, dummy
;
4766 m_gridWin
->GetClientSize( &cw
, &ch
);
4767 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4769 wxClientDC
dc( m_gridWin
);
4772 GetRowTop(m_dragRowOrCol
) +
4773 GetRowMinimalHeight(m_dragRowOrCol
) );
4774 dc
.SetLogicalFunction(wxINVERT
);
4775 if ( m_dragLastPos
>= 0 )
4777 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4779 dc
.DrawLine( left
, y
, left
+cw
, y
);
4784 case WXGRID_CURSOR_SELECT_ROW
:
4785 if ( (row
= YToRow( y
)) >= 0 )
4789 m_selection
->SelectRow( row
,
4790 event
.ControlDown(),
4797 // default label to suppress warnings about "enumeration value
4798 // 'xxx' not handled in switch
4806 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4811 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4812 m_isDragging
= FALSE
;
4815 // ------------ Entering or leaving the window
4817 if ( event
.Entering() || event
.Leaving() )
4819 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4823 // ------------ Left button pressed
4825 else if ( event
.LeftDown() )
4827 // don't send a label click event for a hit on the
4828 // edge of the row label - this is probably the user
4829 // wanting to resize the row
4831 if ( YToEdgeOfRow(y
) < 0 )
4835 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4837 if ( !event
.ShiftDown() && !event
.ControlDown() )
4841 if ( event
.ShiftDown() )
4843 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4846 GetNumberCols() - 1,
4847 event
.ControlDown(),
4854 m_selection
->SelectRow( row
,
4855 event
.ControlDown(),
4862 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4867 // starting to drag-resize a row
4869 if ( CanDragRowSize() )
4870 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4875 // ------------ Left double click
4877 else if (event
.LeftDClick() )
4879 int row
= YToEdgeOfRow(y
);
4884 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4886 // no default action at the moment
4891 // adjust row height depending on label text
4892 AutoSizeRowLabelSize( row
);
4894 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4900 // ------------ Left button released
4902 else if ( event
.LeftUp() )
4904 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4906 DoEndDragResizeRow();
4908 // Note: we are ending the event *after* doing
4909 // default processing in this case
4911 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4914 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4919 // ------------ Right button down
4921 else if ( event
.RightDown() )
4925 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4927 // no default action at the moment
4932 // ------------ Right double click
4934 else if ( event
.RightDClick() )
4938 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4940 // no default action at the moment
4945 // ------------ No buttons down and mouse moving
4947 else if ( event
.Moving() )
4949 m_dragRowOrCol
= YToEdgeOfRow( y
);
4950 if ( m_dragRowOrCol
>= 0 )
4952 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
4954 // don't capture the mouse yet
4955 if ( CanDragRowSize() )
4956 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, FALSE
);
4959 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
4961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, FALSE
);
4967 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
4970 wxPoint
pos( event
.GetPosition() );
4971 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4973 if ( event
.Dragging() )
4977 m_isDragging
= TRUE
;
4978 m_colLabelWin
->CaptureMouse();
4981 if ( event
.LeftIsDown() )
4983 switch( m_cursorMode
)
4985 case WXGRID_CURSOR_RESIZE_COL
:
4987 int cw
, ch
, dummy
, top
;
4988 m_gridWin
->GetClientSize( &cw
, &ch
);
4989 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
4991 wxClientDC
dc( m_gridWin
);
4994 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
4995 GetColMinimalWidth(m_dragRowOrCol
));
4996 dc
.SetLogicalFunction(wxINVERT
);
4997 if ( m_dragLastPos
>= 0 )
4999 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5001 dc
.DrawLine( x
, top
, x
, top
+ch
);
5006 case WXGRID_CURSOR_SELECT_COL
:
5007 if ( (col
= XToCol( x
)) >= 0 )
5011 m_selection
->SelectCol( col
,
5012 event
.ControlDown(),
5019 // default label to suppress warnings about "enumeration value
5020 // 'xxx' not handled in switch
5028 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5033 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5034 m_isDragging
= FALSE
;
5037 // ------------ Entering or leaving the window
5039 if ( event
.Entering() || event
.Leaving() )
5041 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5045 // ------------ Left button pressed
5047 else if ( event
.LeftDown() )
5049 // don't send a label click event for a hit on the
5050 // edge of the col label - this is probably the user
5051 // wanting to resize the col
5053 if ( XToEdgeOfCol(x
) < 0 )
5057 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5059 if ( !event
.ShiftDown() && !event
.ControlDown() )
5063 if ( event
.ShiftDown() )
5065 m_selection
->SelectBlock( 0,
5066 m_currentCellCoords
.GetCol(),
5067 GetNumberRows() - 1, col
,
5068 event
.ControlDown(),
5075 m_selection
->SelectCol( col
,
5076 event
.ControlDown(),
5083 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5088 // starting to drag-resize a col
5090 if ( CanDragColSize() )
5091 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5096 // ------------ Left double click
5098 if ( event
.LeftDClick() )
5100 int col
= XToEdgeOfCol(x
);
5105 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5107 // no default action at the moment
5112 // adjust column width depending on label text
5113 AutoSizeColLabelSize( col
);
5115 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5121 // ------------ Left button released
5123 else if ( event
.LeftUp() )
5125 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5127 DoEndDragResizeCol();
5129 // Note: we are ending the event *after* doing
5130 // default processing in this case
5132 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5135 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5140 // ------------ Right button down
5142 else if ( event
.RightDown() )
5146 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5148 // no default action at the moment
5153 // ------------ Right double click
5155 else if ( event
.RightDClick() )
5159 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5161 // no default action at the moment
5166 // ------------ No buttons down and mouse moving
5168 else if ( event
.Moving() )
5170 m_dragRowOrCol
= XToEdgeOfCol( x
);
5171 if ( m_dragRowOrCol
>= 0 )
5173 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5175 // don't capture the cursor yet
5176 if ( CanDragColSize() )
5177 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, FALSE
);
5180 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5182 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, FALSE
);
5188 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5190 if ( event
.LeftDown() )
5192 // indicate corner label by having both row and
5195 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5201 else if ( event
.LeftDClick() )
5203 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5206 else if ( event
.RightDown() )
5208 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5210 // no default action at the moment
5214 else if ( event
.RightDClick() )
5216 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5218 // no default action at the moment
5223 void wxGrid::ChangeCursorMode(CursorMode mode
,
5228 static const wxChar
*cursorModes
[] =
5237 wxLogTrace(_T("grid"),
5238 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5239 win
== m_colLabelWin
? _T("colLabelWin")
5240 : win
? _T("rowLabelWin")
5242 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5243 #endif // __WXDEBUG__
5245 if ( mode
== m_cursorMode
&&
5246 win
== m_winCapture
&&
5247 captureMouse
== (m_winCapture
!= NULL
))
5252 // by default use the grid itself
5258 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5259 m_winCapture
= (wxWindow
*)NULL
;
5262 m_cursorMode
= mode
;
5264 switch ( m_cursorMode
)
5266 case WXGRID_CURSOR_RESIZE_ROW
:
5267 win
->SetCursor( m_rowResizeCursor
);
5270 case WXGRID_CURSOR_RESIZE_COL
:
5271 win
->SetCursor( m_colResizeCursor
);
5275 win
->SetCursor( *wxSTANDARD_CURSOR
);
5278 // we need to capture mouse when resizing
5279 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5280 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5282 if ( captureMouse
&& resize
)
5284 win
->CaptureMouse();
5289 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5292 wxPoint
pos( event
.GetPosition() );
5293 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5295 wxGridCellCoords coords
;
5296 XYToCell( x
, y
, coords
);
5298 int cell_rows
, cell_cols
;
5299 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5300 if ((cell_rows
< 0) || (cell_cols
< 0))
5302 coords
.SetRow(coords
.GetRow() + cell_rows
);
5303 coords
.SetCol(coords
.GetCol() + cell_cols
);
5306 if ( event
.Dragging() )
5308 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5310 // Don't start doing anything until the mouse has been drug at
5311 // least 3 pixels in any direction...
5314 if (m_startDragPos
== wxDefaultPosition
)
5316 m_startDragPos
= pos
;
5319 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5323 m_isDragging
= TRUE
;
5324 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5326 // Hide the edit control, so it
5327 // won't interfer with drag-shrinking.
5328 if ( IsCellEditControlShown() )
5330 HideCellEditControl();
5331 SaveEditControlValue();
5334 // Have we captured the mouse yet?
5337 m_winCapture
= m_gridWin
;
5338 m_winCapture
->CaptureMouse();
5341 if ( coords
!= wxGridNoCellCoords
)
5343 if ( event
.ControlDown() )
5345 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5346 m_selectingKeyboard
= coords
;
5347 HighlightBlock ( m_selectingKeyboard
, coords
);
5351 if ( !IsSelection() )
5353 HighlightBlock( coords
, coords
);
5357 HighlightBlock( m_currentCellCoords
, coords
);
5361 if (! IsVisible(coords
))
5363 MakeCellVisible(coords
);
5364 // TODO: need to introduce a delay or something here. The
5365 // scrolling is way to fast, at least on MSW - also on GTK.
5369 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5371 int cw
, ch
, left
, dummy
;
5372 m_gridWin
->GetClientSize( &cw
, &ch
);
5373 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5375 wxClientDC
dc( m_gridWin
);
5377 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5378 GetRowMinimalHeight(m_dragRowOrCol
) );
5379 dc
.SetLogicalFunction(wxINVERT
);
5380 if ( m_dragLastPos
>= 0 )
5382 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5384 dc
.DrawLine( left
, y
, left
+cw
, y
);
5387 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5389 int cw
, ch
, dummy
, top
;
5390 m_gridWin
->GetClientSize( &cw
, &ch
);
5391 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5393 wxClientDC
dc( m_gridWin
);
5395 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5396 GetColMinimalWidth(m_dragRowOrCol
) );
5397 dc
.SetLogicalFunction(wxINVERT
);
5398 if ( m_dragLastPos
>= 0 )
5400 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5402 dc
.DrawLine( x
, top
, x
, top
+ch
);
5409 m_isDragging
= FALSE
;
5410 m_startDragPos
= wxDefaultPosition
;
5412 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5413 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5416 if ( event
.Entering() || event
.Leaving() )
5418 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5419 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5424 // ------------ Left button pressed
5426 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5428 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5433 if ( !event
.ControlDown() )
5435 if ( event
.ShiftDown() )
5439 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5440 m_currentCellCoords
.GetCol(),
5443 event
.ControlDown(),
5449 else if ( XToEdgeOfCol(x
) < 0 &&
5450 YToEdgeOfRow(y
) < 0 )
5452 DisableCellEditControl();
5453 MakeCellVisible( coords
);
5455 if ( event
.ControlDown() )
5459 m_selection
->ToggleCellSelection( coords
.GetRow(),
5461 event
.ControlDown(),
5466 m_selectingTopLeft
= wxGridNoCellCoords
;
5467 m_selectingBottomRight
= wxGridNoCellCoords
;
5468 m_selectingKeyboard
= coords
;
5472 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5473 SetCurrentCell( coords
);
5476 if ( m_selection
->GetSelectionMode() !=
5477 wxGrid::wxGridSelectCells
)
5479 HighlightBlock( coords
, coords
);
5488 // ------------ Left double click
5490 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5492 DisableCellEditControl();
5494 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5496 SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5504 // ------------ Left button released
5506 else if ( event
.LeftUp() )
5508 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5512 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5513 m_winCapture
= NULL
;
5516 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5519 EnableCellEditControl();
5521 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5522 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5523 editor
->StartingClick();
5527 m_waitForSlowClick
= FALSE
;
5529 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5530 m_selectingBottomRight
!= wxGridNoCellCoords
)
5534 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5535 m_selectingTopLeft
.GetCol(),
5536 m_selectingBottomRight
.GetRow(),
5537 m_selectingBottomRight
.GetCol(),
5538 event
.ControlDown(),
5544 m_selectingTopLeft
= wxGridNoCellCoords
;
5545 m_selectingBottomRight
= wxGridNoCellCoords
;
5547 // Show the edit control, if it has been hidden for
5549 ShowCellEditControl();
5552 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5554 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5555 DoEndDragResizeRow();
5557 // Note: we are ending the event *after* doing
5558 // default processing in this case
5560 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5562 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5564 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5565 DoEndDragResizeCol();
5567 // Note: we are ending the event *after* doing
5568 // default processing in this case
5570 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5577 // ------------ Right button down
5579 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5581 DisableCellEditControl();
5582 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5587 // no default action at the moment
5592 // ------------ Right double click
5594 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5596 DisableCellEditControl();
5597 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5602 // no default action at the moment
5606 // ------------ Moving and no button action
5608 else if ( event
.Moving() && !event
.IsButton() )
5610 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5612 // out of grid cell area
5613 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5617 int dragRow
= YToEdgeOfRow( y
);
5618 int dragCol
= XToEdgeOfCol( x
);
5620 // Dragging on the corner of a cell to resize in both
5621 // directions is not implemented yet...
5623 if ( dragRow
>= 0 && dragCol
>= 0 )
5625 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5631 m_dragRowOrCol
= dragRow
;
5633 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5635 if ( CanDragRowSize() && CanDragGridSize() )
5636 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5641 m_dragRowOrCol
= dragCol
;
5649 m_dragRowOrCol
= dragCol
;
5651 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5653 if ( CanDragColSize() && CanDragGridSize() )
5654 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5660 // Neither on a row or col edge
5662 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5664 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5670 void wxGrid::DoEndDragResizeRow()
5672 if ( m_dragLastPos
>= 0 )
5674 // erase the last line and resize the row
5676 int cw
, ch
, left
, dummy
;
5677 m_gridWin
->GetClientSize( &cw
, &ch
);
5678 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5680 wxClientDC
dc( m_gridWin
);
5682 dc
.SetLogicalFunction( wxINVERT
);
5683 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5684 HideCellEditControl();
5685 SaveEditControlValue();
5687 int rowTop
= GetRowTop(m_dragRowOrCol
);
5688 SetRowSize( m_dragRowOrCol
,
5689 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5691 if ( !GetBatchCount() )
5693 // Only needed to get the correct rect.y:
5694 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5696 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5697 rect
.width
= m_rowLabelWidth
;
5698 rect
.height
= ch
- rect
.y
;
5699 m_rowLabelWin
->Refresh( TRUE
, &rect
);
5701 // if there is a multicell block, paint all of it
5704 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5705 int leftCol
= XToCol(left
);
5706 int rightCol
= internalXToCol(left
+cw
);
5709 for (i
=leftCol
; i
<rightCol
; i
++)
5711 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5712 if (cell_rows
< subtract_rows
)
5713 subtract_rows
= cell_rows
;
5715 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5716 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5717 rect
.height
= ch
- rect
.y
;
5720 m_gridWin
->Refresh( FALSE
, &rect
);
5723 ShowCellEditControl();
5728 void wxGrid::DoEndDragResizeCol()
5730 if ( m_dragLastPos
>= 0 )
5732 // erase the last line and resize the col
5734 int cw
, ch
, dummy
, top
;
5735 m_gridWin
->GetClientSize( &cw
, &ch
);
5736 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5738 wxClientDC
dc( m_gridWin
);
5740 dc
.SetLogicalFunction( wxINVERT
);
5741 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5742 HideCellEditControl();
5743 SaveEditControlValue();
5745 int colLeft
= GetColLeft(m_dragRowOrCol
);
5746 SetColSize( m_dragRowOrCol
,
5747 wxMax( m_dragLastPos
- colLeft
,
5748 GetColMinimalWidth(m_dragRowOrCol
) ) );
5750 if ( !GetBatchCount() )
5752 // Only needed to get the correct rect.x:
5753 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5755 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5756 rect
.width
= cw
- rect
.x
;
5757 rect
.height
= m_colLabelHeight
;
5758 m_colLabelWin
->Refresh( TRUE
, &rect
);
5760 // if there is a multicell block, paint all of it
5763 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5764 int topRow
= YToRow(top
);
5765 int bottomRow
= internalYToRow(top
+cw
);
5768 for (i
=topRow
; i
<bottomRow
; i
++)
5770 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5771 if (cell_cols
< subtract_cols
)
5772 subtract_cols
= cell_cols
;
5774 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5775 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5776 rect
.width
= cw
- rect
.x
;
5779 m_gridWin
->Refresh( FALSE
, &rect
);
5782 ShowCellEditControl();
5789 // ------ interaction with data model
5791 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5793 switch ( msg
.GetId() )
5795 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5796 return GetModelValues();
5798 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5799 return SetModelValues();
5801 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5802 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5803 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5804 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5805 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5806 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5807 return Redimension( msg
);
5816 // The behaviour of this function depends on the grid table class
5817 // Clear() function. For the default wxGridStringTable class the
5818 // behavious is to replace all cell contents with wxEmptyString but
5819 // not to change the number of rows or cols.
5821 void wxGrid::ClearGrid()
5825 if (IsCellEditControlEnabled())
5826 DisableCellEditControl();
5829 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5834 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5836 // TODO: something with updateLabels flag
5840 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5846 if (IsCellEditControlEnabled())
5847 DisableCellEditControl();
5849 bool done
= m_table
->InsertRows( pos
, numRows
);
5852 // the table will have sent the results of the insert row
5853 // operation to this view object as a grid table message
5859 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5861 // TODO: something with updateLabels flag
5865 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5871 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5873 // the table will have sent the results of the append row
5874 // operation to this view object as a grid table message
5880 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5882 // TODO: something with updateLabels flag
5886 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5892 if (IsCellEditControlEnabled())
5893 DisableCellEditControl();
5895 bool done
= m_table
->DeleteRows( pos
, numRows
);
5897 // the table will have sent the results of the delete row
5898 // operation to this view object as a grid table message
5904 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5906 // TODO: something with updateLabels flag
5910 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5916 if (IsCellEditControlEnabled())
5917 DisableCellEditControl();
5919 bool done
= m_table
->InsertCols( pos
, numCols
);
5921 // the table will have sent the results of the insert col
5922 // operation to this view object as a grid table message
5928 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5930 // TODO: something with updateLabels flag
5934 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
5940 bool done
= m_table
->AppendCols( numCols
);
5942 // the table will have sent the results of the append col
5943 // operation to this view object as a grid table message
5949 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5951 // TODO: something with updateLabels flag
5955 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
5961 if (IsCellEditControlEnabled())
5962 DisableCellEditControl();
5964 bool done
= m_table
->DeleteCols( pos
, numCols
);
5966 // the table will have sent the results of the delete col
5967 // operation to this view object as a grid table message
5975 // ----- event handlers
5978 // Generate a grid event based on a mouse event and
5979 // return the result of ProcessEvent()
5981 int wxGrid::SendEvent( const wxEventType type
,
5983 wxMouseEvent
& mouseEv
)
5988 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
5990 int rowOrCol
= (row
== -1 ? col
: row
);
5992 wxGridSizeEvent
gridEvt( GetId(),
5996 mouseEv
.GetX() + GetRowLabelSize(),
5997 mouseEv
.GetY() + GetColLabelSize(),
5998 mouseEv
.ControlDown(),
5999 mouseEv
.ShiftDown(),
6001 mouseEv
.MetaDown() );
6003 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6004 vetoed
= !gridEvt
.IsAllowed();
6006 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6008 // Right now, it should _never_ end up here!
6009 wxGridRangeSelectEvent
gridEvt( GetId(),
6013 m_selectingBottomRight
,
6015 mouseEv
.ControlDown(),
6016 mouseEv
.ShiftDown(),
6018 mouseEv
.MetaDown() );
6020 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6021 vetoed
= !gridEvt
.IsAllowed();
6025 wxGridEvent
gridEvt( GetId(),
6029 mouseEv
.GetX() + GetRowLabelSize(),
6030 mouseEv
.GetY() + GetColLabelSize(),
6032 mouseEv
.ControlDown(),
6033 mouseEv
.ShiftDown(),
6035 mouseEv
.MetaDown() );
6036 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6037 vetoed
= !gridEvt
.IsAllowed();
6040 // A Veto'd event may not be `claimed' so test this first
6041 if (vetoed
) return -1;
6042 return claimed
? 1 : 0;
6046 // Generate a grid event of specified type and return the result
6047 // of ProcessEvent().
6049 int wxGrid::SendEvent( const wxEventType type
,
6055 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6057 int rowOrCol
= (row
== -1 ? col
: row
);
6059 wxGridSizeEvent
gridEvt( GetId(),
6064 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6065 vetoed
= !gridEvt
.IsAllowed();
6069 wxGridEvent
gridEvt( GetId(),
6074 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6075 vetoed
= !gridEvt
.IsAllowed();
6078 // A Veto'd event may not be `claimed' so test this first
6079 if (vetoed
) return -1;
6080 return claimed
? 1 : 0;
6084 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6086 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6089 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6091 // Don't do anything if between Begin/EndBatch...
6092 // EndBatch() will do all this on the last nested one anyway.
6093 if (! GetBatchCount())
6095 // Refresh to get correct scrolled position:
6096 wxScrolledWindow::Refresh(eraseb
,rect
);
6100 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6101 int width_label
, width_cell
, height_label
, height_cell
;
6104 //Copy rectangle can get scroll offsets..
6105 rect_x
= rect
->GetX();
6106 rect_y
= rect
->GetY();
6107 rectWidth
= rect
->GetWidth();
6108 rectHeight
= rect
->GetHeight();
6110 width_label
= m_rowLabelWidth
- rect_x
;
6111 if (width_label
> rectWidth
) width_label
= rectWidth
;
6113 height_label
= m_colLabelHeight
- rect_y
;
6114 if (height_label
> rectHeight
) height_label
= rectHeight
;
6116 if (rect_x
> m_rowLabelWidth
)
6118 x
= rect_x
- m_rowLabelWidth
;
6119 width_cell
= rectWidth
;
6124 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6127 if (rect_y
> m_colLabelHeight
)
6129 y
= rect_y
- m_colLabelHeight
;
6130 height_cell
= rectHeight
;
6135 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6138 // Paint corner label part intersecting rect.
6139 if ( width_label
> 0 && height_label
> 0 )
6141 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6142 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6145 // Paint col labels part intersecting rect.
6146 if ( width_cell
> 0 && height_label
> 0 )
6148 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6149 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6152 // Paint row labels part intersecting rect.
6153 if ( width_label
> 0 && height_cell
> 0 )
6155 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6156 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6159 // Paint cell area part intersecting rect.
6160 if ( width_cell
> 0 && height_cell
> 0 )
6162 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6163 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6168 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6169 m_colLabelWin
->Refresh(eraseb
, NULL
);
6170 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6171 m_gridWin
->Refresh(eraseb
, NULL
);
6176 void wxGrid::OnSize( wxSizeEvent
& event
)
6178 // position the child windows
6181 // don't call CalcDimensions() from here, the base class handles the size
6187 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6189 if ( m_inOnKeyDown
)
6191 // shouldn't be here - we are going round in circles...
6193 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6196 m_inOnKeyDown
= TRUE
;
6198 // propagate the event up and see if it gets processed
6200 wxWindow
*parent
= GetParent();
6201 wxKeyEvent
keyEvt( event
);
6202 keyEvt
.SetEventObject( parent
);
6204 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6207 // try local handlers
6209 switch ( event
.GetKeyCode() )
6212 if ( event
.ControlDown() )
6214 MoveCursorUpBlock( event
.ShiftDown() );
6218 MoveCursorUp( event
.ShiftDown() );
6223 if ( event
.ControlDown() )
6225 MoveCursorDownBlock( event
.ShiftDown() );
6229 MoveCursorDown( event
.ShiftDown() );
6234 if ( event
.ControlDown() )
6236 MoveCursorLeftBlock( event
.ShiftDown() );
6240 MoveCursorLeft( event
.ShiftDown() );
6245 if ( event
.ControlDown() )
6247 MoveCursorRightBlock( event
.ShiftDown() );
6251 MoveCursorRight( event
.ShiftDown() );
6256 case WXK_NUMPAD_ENTER
:
6257 if ( event
.ControlDown() )
6259 event
.Skip(); // to let the edit control have the return
6263 if ( GetGridCursorRow() < GetNumberRows()-1 )
6265 MoveCursorDown( event
.ShiftDown() );
6269 // at the bottom of a column
6270 HideCellEditControl();
6271 SaveEditControlValue();
6281 if (event
.ShiftDown())
6283 if ( GetGridCursorCol() > 0 )
6285 MoveCursorLeft( FALSE
);
6290 HideCellEditControl();
6291 SaveEditControlValue();
6296 if ( GetGridCursorCol() < GetNumberCols()-1 )
6298 MoveCursorRight( FALSE
);
6303 HideCellEditControl();
6304 SaveEditControlValue();
6310 if ( event
.ControlDown() )
6312 MakeCellVisible( 0, 0 );
6313 SetCurrentCell( 0, 0 );
6322 if ( event
.ControlDown() )
6324 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6325 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6342 if ( event
.ControlDown() )
6346 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6347 m_currentCellCoords
.GetCol(),
6348 event
.ControlDown(),
6355 if ( !IsEditable() )
6357 MoveCursorRight( FALSE
);
6360 // Otherwise fall through to default
6363 // is it possible to edit the current cell at all?
6364 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6366 // yes, now check whether the cells editor accepts the key
6367 int row
= m_currentCellCoords
.GetRow();
6368 int col
= m_currentCellCoords
.GetCol();
6369 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6370 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6372 // <F2> is special and will always start editing, for
6373 // other keys - ask the editor itself
6374 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6375 || editor
->IsAcceptedKey(event
) )
6377 // ensure cell is visble
6378 MakeCellVisible(row
, col
);
6379 EnableCellEditControl();
6381 // a problem can arise if the cell is not completely
6382 // visible (even after calling MakeCellVisible the
6383 // control is not created and calling StartingKey will
6385 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6397 // let others process char events with modifiers or all
6398 // char events for readonly cells
6405 m_inOnKeyDown
= FALSE
;
6408 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6410 // try local handlers
6412 if ( event
.GetKeyCode() == WXK_SHIFT
)
6414 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6415 m_selectingBottomRight
!= wxGridNoCellCoords
)
6419 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6420 m_selectingTopLeft
.GetCol(),
6421 m_selectingBottomRight
.GetRow(),
6422 m_selectingBottomRight
.GetCol(),
6423 event
.ControlDown(),
6430 m_selectingTopLeft
= wxGridNoCellCoords
;
6431 m_selectingBottomRight
= wxGridNoCellCoords
;
6432 m_selectingKeyboard
= wxGridNoCellCoords
;
6436 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6440 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6442 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6444 // the event has been intercepted - do nothing
6448 wxClientDC
dc(m_gridWin
);
6451 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6453 HideCellEditControl();
6454 DisableCellEditControl();
6456 if ( IsVisible( m_currentCellCoords
, FALSE
) )
6459 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6460 if ( !m_gridLinesEnabled
)
6468 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6470 // Otherwise refresh redraws the highlight!
6471 m_currentCellCoords
= coords
;
6473 DrawGridCellArea(dc
,cells
);
6474 DrawAllGridLines( dc
, r
);
6478 m_currentCellCoords
= coords
;
6480 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6481 DrawCellHighlight(dc
, attr
);
6486 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6489 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6493 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6496 rightCol
= GetNumberCols() - 1;
6498 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6501 bottomRow
= GetNumberRows() - 1;
6505 if ( topRow
> bottomRow
)
6512 if ( leftCol
> rightCol
)
6519 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6520 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6522 // First the case that we selected a completely new area
6523 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6524 m_selectingBottomRight
== wxGridNoCellCoords
)
6527 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6528 wxGridCellCoords ( bottomRow
, rightCol
) );
6529 m_gridWin
->Refresh( FALSE
, &rect
);
6531 // Now handle changing an existing selection area.
6532 else if ( m_selectingTopLeft
!= updateTopLeft
||
6533 m_selectingBottomRight
!= updateBottomRight
)
6535 // Compute two optimal update rectangles:
6536 // Either one rectangle is a real subset of the
6537 // other, or they are (almost) disjoint!
6539 bool need_refresh
[4];
6543 need_refresh
[3] = FALSE
;
6546 // Store intermediate values
6547 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6548 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6549 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6550 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6552 // Determine the outer/inner coordinates.
6553 if (oldLeft
> leftCol
)
6559 if (oldTop
> topRow
)
6565 if (oldRight
< rightCol
)
6568 oldRight
= rightCol
;
6571 if (oldBottom
< bottomRow
)
6574 oldBottom
= bottomRow
;
6578 // Now, either the stuff marked old is the outer
6579 // rectangle or we don't have a situation where one
6580 // is contained in the other.
6582 if ( oldLeft
< leftCol
)
6584 // Refresh the newly selected or deselected
6585 // area to the left of the old or new selection.
6586 need_refresh
[0] = TRUE
;
6587 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6589 wxGridCellCoords ( oldBottom
,
6593 if ( oldTop
< topRow
)
6595 // Refresh the newly selected or deselected
6596 // area above the old or new selection.
6597 need_refresh
[1] = TRUE
;
6598 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6600 wxGridCellCoords ( topRow
- 1,
6604 if ( oldRight
> rightCol
)
6606 // Refresh the newly selected or deselected
6607 // area to the right of the old or new selection.
6608 need_refresh
[2] = TRUE
;
6609 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6611 wxGridCellCoords ( oldBottom
,
6615 if ( oldBottom
> bottomRow
)
6617 // Refresh the newly selected or deselected
6618 // area below the old or new selection.
6619 need_refresh
[3] = TRUE
;
6620 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6622 wxGridCellCoords ( oldBottom
,
6626 // various Refresh() calls
6627 for (i
= 0; i
< 4; i
++ )
6628 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6629 m_gridWin
->Refresh( FALSE
, &(rect
[i
]) );
6632 m_selectingTopLeft
= updateTopLeft
;
6633 m_selectingBottomRight
= updateBottomRight
;
6637 // ------ functions to get/send data (see also public functions)
6640 bool wxGrid::GetModelValues()
6642 // Hide the editor, so it won't hide a changed value.
6643 HideCellEditControl();
6647 // all we need to do is repaint the grid
6649 m_gridWin
->Refresh();
6657 bool wxGrid::SetModelValues()
6661 // Disable the editor, so it won't hide a changed value.
6662 // Do we also want to save the current value of the editor first?
6664 DisableCellEditControl();
6668 for ( row
= 0; row
< m_numRows
; row
++ )
6670 for ( col
= 0; col
< m_numCols
; col
++ )
6672 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6684 // Note - this function only draws cells that are in the list of
6685 // exposed cells (usually set from the update region by
6686 // CalcExposedCells)
6688 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6690 if ( !m_numRows
|| !m_numCols
) return;
6692 int i
, numCells
= cells
.GetCount();
6693 int row
, col
, cell_rows
, cell_cols
;
6694 wxGridCellCoordsArray redrawCells
;
6696 for ( i
= numCells
-1; i
>= 0; i
-- )
6698 row
= cells
[i
].GetRow();
6699 col
= cells
[i
].GetCol();
6700 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6702 // If this cell is part of a multicell block, find owner for repaint
6703 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6705 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6706 bool marked
= FALSE
;
6707 for ( int j
= 0; j
< numCells
; j
++ )
6709 if ( cell
== cells
[j
] )
6717 int count
= redrawCells
.GetCount();
6718 for (int j
= 0; j
< count
; j
++)
6720 if ( cell
== redrawCells
[j
] )
6726 if (!marked
) redrawCells
.Add( cell
);
6728 continue; // don't bother drawing this cell
6731 // If this cell is empty, find cell to left that might want to overflow
6732 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6734 for ( int l
= 0; l
< cell_rows
; l
++ )
6736 // find a cell in this row to left alreay marked for repaint
6738 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6739 if ((redrawCells
[k
].GetCol() < left
) &&
6740 (redrawCells
[k
].GetRow() == row
))
6741 left
=redrawCells
[k
].GetCol();
6743 if (left
== col
) left
= 0; // oh well
6745 for (int j
= col
-1; j
>= left
; j
--)
6747 if (!m_table
->IsEmptyCell(row
+l
, j
))
6749 if (GetCellOverflow(row
+l
, j
))
6751 wxGridCellCoords
cell(row
+l
, j
);
6752 bool marked
= FALSE
;
6754 for (int k
= 0; k
< numCells
; k
++)
6756 if ( cell
== cells
[k
] )
6764 int count
= redrawCells
.GetCount();
6765 for (int k
= 0; k
< count
; k
++)
6767 if ( cell
== redrawCells
[k
] )
6773 if (!marked
) redrawCells
.Add( cell
);
6781 DrawCell( dc
, cells
[i
] );
6784 numCells
= redrawCells
.GetCount();
6786 for ( i
= numCells
- 1; i
>= 0; i
-- )
6788 DrawCell( dc
, redrawCells
[i
] );
6793 void wxGrid::DrawGridSpace( wxDC
& dc
)
6796 m_gridWin
->GetClientSize( &cw
, &ch
);
6799 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6801 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6802 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6804 if ( right
> rightCol
|| bottom
> bottomRow
)
6807 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6809 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6810 dc
.SetPen( *wxTRANSPARENT_PEN
);
6812 if ( right
> rightCol
)
6814 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6817 if ( bottom
> bottomRow
)
6819 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6825 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6827 int row
= coords
.GetRow();
6828 int col
= coords
.GetCol();
6830 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6833 // we draw the cell border ourselves
6834 #if !WXGRID_DRAW_LINES
6835 if ( m_gridLinesEnabled
)
6836 DrawCellBorder( dc
, coords
);
6839 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6841 bool isCurrent
= coords
== m_currentCellCoords
;
6843 wxRect rect
= CellToRect( row
, col
);
6845 // if the editor is shown, we should use it and not the renderer
6846 // Note: However, only if it is really _shown_, i.e. not hidden!
6847 if ( isCurrent
&& IsCellEditControlShown() )
6849 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6850 editor
->PaintBackground(rect
, attr
);
6855 // but all the rest is drawn by the cell renderer and hence may be
6857 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6858 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6865 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6867 int row
= m_currentCellCoords
.GetRow();
6868 int col
= m_currentCellCoords
.GetCol();
6870 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6873 wxRect rect
= CellToRect(row
, col
);
6875 // hmmm... what could we do here to show that the cell is disabled?
6876 // for now, I just draw a thinner border than for the other ones, but
6877 // it doesn't look really good
6879 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6883 // The center of th drawn line is where the position/width/height of
6884 // the rectangle is actually at, (on wxMSW atr least,) so we will
6885 // reduce the size of the rectangle to compensate for the thickness of
6886 // the line. If this is too strange on non wxMSW platforms then
6887 // please #ifdef this appropriately.
6888 rect
.x
+= penWidth
/2;
6889 rect
.y
+= penWidth
/2;
6890 rect
.width
-= penWidth
-1;
6891 rect
.height
-= penWidth
-1;
6894 // Now draw the rectangle
6895 // use the cellHighlightColour if the cell is inside a selection, this
6896 // will ensure the cell is always visible.
6897 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6898 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6899 dc
.DrawRectangle(rect
);
6903 // VZ: my experiments with 3d borders...
6905 // how to properly set colours for arbitrary bg?
6906 wxCoord x1
= rect
.x
,
6908 x2
= rect
.x
+ rect
.width
-1,
6909 y2
= rect
.y
+ rect
.height
-1;
6911 dc
.SetPen(*wxWHITE_PEN
);
6912 dc
.DrawLine(x1
, y1
, x2
, y1
);
6913 dc
.DrawLine(x1
, y1
, x1
, y2
);
6915 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6916 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6918 dc
.SetPen(*wxBLACK_PEN
);
6919 dc
.DrawLine(x1
, y2
, x2
, y2
);
6920 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6925 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6927 int row
= coords
.GetRow();
6928 int col
= coords
.GetCol();
6929 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6932 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6934 wxRect rect
= CellToRect( row
, col
);
6936 // right hand border
6938 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
6939 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
6943 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
6944 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
6947 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
6949 // This if block was previously in wxGrid::OnPaint but that doesn't
6950 // seem to get called under wxGTK - MB
6952 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
6953 m_numRows
&& m_numCols
)
6955 m_currentCellCoords
.Set(0, 0);
6958 if ( IsCellEditControlShown() )
6960 // don't show highlight when the edit control is shown
6964 // if the active cell was repainted, repaint its highlight too because it
6965 // might have been damaged by the grid lines
6966 size_t count
= cells
.GetCount();
6967 for ( size_t n
= 0; n
< count
; n
++ )
6969 if ( cells
[n
] == m_currentCellCoords
)
6971 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
6972 DrawCellHighlight(dc
, attr
);
6980 // TODO: remove this ???
6981 // This is used to redraw all grid lines e.g. when the grid line colour
6984 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
6986 #if !WXGRID_DRAW_LINES
6990 if ( !m_gridLinesEnabled
||
6992 !m_numCols
) return;
6994 int top
, bottom
, left
, right
;
6996 #if 0 //#ifndef __WXGTK__
7000 m_gridWin
->GetClientSize(&cw
, &ch
);
7002 // virtual coords of visible area
7004 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7005 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7010 reg
.GetBox(x
, y
, w
, h
);
7011 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7012 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7016 m_gridWin
->GetClientSize(&cw
, &ch
);
7017 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7018 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7021 // avoid drawing grid lines past the last row and col
7023 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7024 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7026 // no gridlines inside multicells, clip them out
7027 int leftCol
= internalXToCol(left
);
7028 int topRow
= internalYToRow(top
);
7029 int rightCol
= internalXToCol(right
);
7030 int bottomRow
= internalYToRow(bottom
);
7031 wxRegion
clippedcells(0, 0, cw
, ch
);
7034 int i
, j
, cell_rows
, cell_cols
;
7037 for (j
=topRow
; j
<bottomRow
; j
++)
7039 for (i
=leftCol
; i
<rightCol
; i
++)
7041 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7042 if ((cell_rows
> 1) || (cell_cols
> 1))
7044 rect
= CellToRect(j
,i
);
7045 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7046 clippedcells
.Subtract(rect
);
7048 else if ((cell_rows
< 0) || (cell_cols
< 0))
7050 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7051 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7052 clippedcells
.Subtract(rect
);
7056 dc
.SetClippingRegion( clippedcells
);
7058 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7060 // horizontal grid lines
7062 // already declared above - int i;
7063 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7065 int bot
= GetRowBottom(i
) - 1;
7074 dc
.DrawLine( left
, bot
, right
, bot
);
7079 // vertical grid lines
7081 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7083 int colRight
= GetColRight(i
) - 1;
7084 if ( colRight
> right
)
7089 if ( colRight
>= left
)
7091 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7094 dc
.DestroyClippingRegion();
7098 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7100 if ( !m_numRows
) return;
7103 size_t numLabels
= rows
.GetCount();
7105 for ( i
= 0; i
< numLabels
; i
++ )
7107 DrawRowLabel( dc
, rows
[i
] );
7112 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7114 if ( GetRowHeight(row
) <= 0 )
7117 int rowTop
= GetRowTop(row
),
7118 rowBottom
= GetRowBottom(row
) - 1;
7120 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7121 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7122 m_rowLabelWidth
-1, rowBottom
);
7124 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7126 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7128 dc
.SetPen( *wxWHITE_PEN
);
7129 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7130 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7132 dc
.SetBackgroundMode( wxTRANSPARENT
);
7133 dc
.SetTextForeground( GetLabelTextColour() );
7134 dc
.SetFont( GetLabelFont() );
7137 GetRowLabelAlignment( &hAlign
, &vAlign
);
7141 rect
.SetY( GetRowTop(row
) + 2 );
7142 rect
.SetWidth( m_rowLabelWidth
- 4 );
7143 rect
.SetHeight( GetRowHeight(row
) - 4 );
7144 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7148 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7150 if ( !m_numCols
) return;
7153 size_t numLabels
= cols
.GetCount();
7155 for ( i
= 0; i
< numLabels
; i
++ )
7157 DrawColLabel( dc
, cols
[i
] );
7162 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7164 if ( GetColWidth(col
) <= 0 )
7167 int colLeft
= GetColLeft(col
),
7168 colRight
= GetColRight(col
) - 1;
7170 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7171 dc
.DrawLine( colRight
, 0,
7172 colRight
, m_colLabelHeight
-1 );
7174 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7176 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7177 colRight
+1, m_colLabelHeight
-1 );
7179 dc
.SetPen( *wxWHITE_PEN
);
7180 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7181 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7183 dc
.SetBackgroundMode( wxTRANSPARENT
);
7184 dc
.SetTextForeground( GetLabelTextColour() );
7185 dc
.SetFont( GetLabelFont() );
7187 int hAlign
, vAlign
, orient
;
7188 GetColLabelAlignment( &hAlign
, &vAlign
);
7189 orient
= GetColLabelTextOrientation();
7192 rect
.SetX( colLeft
+ 2 );
7194 rect
.SetWidth( GetColWidth(col
) - 4 );
7195 rect
.SetHeight( m_colLabelHeight
- 4 );
7196 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7199 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7200 const wxString
& value
,
7204 int textOrientation
)
7206 wxArrayString lines
;
7208 StringToLines( value
, lines
);
7211 //Forward to new API.
7212 DrawTextRectangle( dc
,
7221 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7222 const wxArrayString
& lines
,
7226 int textOrientation
)
7228 long textWidth
, textHeight
;
7229 long lineWidth
, lineHeight
;
7232 dc
.SetClippingRegion( rect
);
7234 nLines
= lines
.GetCount();
7238 float x
= 0.0, y
= 0.0;
7240 if( textOrientation
== wxHORIZONTAL
)
7241 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7243 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7247 case wxALIGN_BOTTOM
:
7248 if( textOrientation
== wxHORIZONTAL
)
7249 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7251 x
= rect
.x
+ rect
.width
- textWidth
;
7254 case wxALIGN_CENTRE
:
7255 if( textOrientation
== wxHORIZONTAL
)
7256 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7258 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7263 if( textOrientation
== wxHORIZONTAL
)
7270 // Align each line of a multi-line label
7271 for( l
= 0; l
< nLines
; l
++ )
7273 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7275 switch( horizAlign
)
7278 if( textOrientation
== wxHORIZONTAL
)
7279 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7281 y
= rect
.y
+ lineWidth
+ 1;
7284 case wxALIGN_CENTRE
:
7285 if( textOrientation
== wxHORIZONTAL
)
7286 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7288 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7293 if( textOrientation
== wxHORIZONTAL
)
7296 y
= rect
.y
+ rect
.height
- 1;
7300 if( textOrientation
== wxHORIZONTAL
)
7302 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7307 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7312 dc
.DestroyClippingRegion();
7316 // Split multi line text up into an array of strings. Any existing
7317 // contents of the string array are preserved.
7319 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7323 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7324 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7326 while ( startPos
< (int)tVal
.Length() )
7328 pos
= tVal
.Mid(startPos
).Find( eol
);
7333 else if ( pos
== 0 )
7335 lines
.Add( wxEmptyString
);
7339 lines
.Add( value
.Mid(startPos
, pos
) );
7343 if ( startPos
< (int)value
.Length() )
7345 lines
.Add( value
.Mid( startPos
) );
7350 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7351 const wxArrayString
& lines
,
7352 long *width
, long *height
)
7359 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7361 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7362 w
= wxMax( w
, lineW
);
7371 // ------ Batch processing.
7373 void wxGrid::EndBatch()
7375 if ( m_batchCount
> 0 )
7378 if ( !m_batchCount
)
7381 m_rowLabelWin
->Refresh();
7382 m_colLabelWin
->Refresh();
7383 m_cornerLabelWin
->Refresh();
7384 m_gridWin
->Refresh();
7389 // Use this, rather than wxWindow::Refresh(), to force an immediate
7390 // repainting of the grid. Has no effect if you are already inside a
7391 // BeginBatch / EndBatch block.
7393 void wxGrid::ForceRefresh()
7401 // ------ Edit control functions
7405 void wxGrid::EnableEditing( bool edit
)
7407 // TODO: improve this ?
7409 if ( edit
!= m_editable
)
7411 if(!edit
) EnableCellEditControl(edit
);
7417 void wxGrid::EnableCellEditControl( bool enable
)
7422 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7423 SetCurrentCell( 0, 0 );
7425 if ( enable
!= m_cellEditCtrlEnabled
)
7429 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7432 // this should be checked by the caller!
7433 wxASSERT_MSG( CanEnableCellControl(),
7434 _T("can't enable editing for this cell!") );
7436 // do it before ShowCellEditControl()
7437 m_cellEditCtrlEnabled
= enable
;
7439 ShowCellEditControl();
7443 //FIXME:add veto support
7444 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7446 HideCellEditControl();
7447 SaveEditControlValue();
7449 // do it after HideCellEditControl()
7450 m_cellEditCtrlEnabled
= enable
;
7455 bool wxGrid::IsCurrentCellReadOnly() const
7458 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7459 bool readonly
= attr
->IsReadOnly();
7465 bool wxGrid::CanEnableCellControl() const
7467 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7468 !IsCurrentCellReadOnly();
7472 bool wxGrid::IsCellEditControlEnabled() const
7474 // the cell edit control might be disable for all cells or just for the
7475 // current one if it's read only
7476 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : FALSE
;
7479 bool wxGrid::IsCellEditControlShown() const
7481 bool isShown
= FALSE
;
7483 if ( m_cellEditCtrlEnabled
)
7485 int row
= m_currentCellCoords
.GetRow();
7486 int col
= m_currentCellCoords
.GetCol();
7487 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7488 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7493 if ( editor
->IsCreated() )
7495 isShown
= editor
->GetControl()->IsShown();
7505 void wxGrid::ShowCellEditControl()
7507 if ( IsCellEditControlEnabled() )
7509 if ( !IsVisible( m_currentCellCoords
) )
7511 m_cellEditCtrlEnabled
= FALSE
;
7516 wxRect rect
= CellToRect( m_currentCellCoords
);
7517 int row
= m_currentCellCoords
.GetRow();
7518 int col
= m_currentCellCoords
.GetCol();
7520 // if this is part of a multicell, find owner (topleft)
7521 int cell_rows
, cell_cols
;
7522 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7523 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7527 m_currentCellCoords
.SetRow( row
);
7528 m_currentCellCoords
.SetCol( col
);
7531 // convert to scrolled coords
7533 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7535 // done in PaintBackground()
7537 // erase the highlight and the cell contents because the editor
7538 // might not cover the entire cell
7539 wxClientDC
dc( m_gridWin
);
7541 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7542 dc
.SetPen(*wxTRANSPARENT_PEN
);
7543 dc
.DrawRectangle(rect
);
7546 // cell is shifted by one pixel
7547 // However, don't allow x or y to become negative
7548 // since the SetSize() method interprets that as
7555 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7556 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7557 if ( !editor
->IsCreated() )
7559 editor
->Create(m_gridWin
, -1,
7560 new wxGridCellEditorEvtHandler(this, editor
));
7562 wxGridEditorCreatedEvent
evt(GetId(),
7563 wxEVT_GRID_EDITOR_CREATED
,
7567 editor
->GetControl());
7568 GetEventHandler()->ProcessEvent(evt
);
7572 // resize editor to overflow into righthand cells if allowed
7573 int maxWidth
= rect
.width
;
7574 wxString value
= GetCellValue(row
, col
);
7575 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7578 GetTextExtent(value
, &maxWidth
, &y
,
7579 NULL
, NULL
, &attr
->GetFont());
7580 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7582 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7583 if (rect
.x
+maxWidth
> client_right
)
7584 maxWidth
= client_right
- rect
.x
;
7586 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7588 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7589 // may have changed earlier
7590 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7593 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7594 // looks weird going over a multicell
7595 if (m_table
->IsEmptyCell(row
,i
) &&
7596 (rect
.width
< maxWidth
) && (c_rows
== 1))
7597 rect
.width
+= GetColWidth(i
);
7601 if (rect
.GetRight() > client_right
)
7602 rect
.SetRight(client_right
-1);
7605 editor
->SetCellAttr(attr
);
7606 editor
->SetSize( rect
);
7607 editor
->Show( TRUE
, attr
);
7609 // recalc dimensions in case we need to
7610 // expand the scrolled window to account for editor
7613 editor
->BeginEdit(row
, col
, this);
7614 editor
->SetCellAttr(NULL
);
7623 void wxGrid::HideCellEditControl()
7625 if ( IsCellEditControlEnabled() )
7627 int row
= m_currentCellCoords
.GetRow();
7628 int col
= m_currentCellCoords
.GetCol();
7630 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7631 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7632 editor
->Show( FALSE
);
7635 m_gridWin
->SetFocus();
7636 // refresh whole row to the right
7637 wxRect
rect( CellToRect(row
, col
) );
7638 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7639 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7640 m_gridWin
->Refresh( FALSE
, &rect
);
7645 void wxGrid::SaveEditControlValue()
7647 if ( IsCellEditControlEnabled() )
7649 int row
= m_currentCellCoords
.GetRow();
7650 int col
= m_currentCellCoords
.GetCol();
7652 wxString oldval
= GetCellValue(row
,col
);
7654 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7655 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7656 bool changed
= editor
->EndEdit(row
, col
, this);
7663 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7664 m_currentCellCoords
.GetRow(),
7665 m_currentCellCoords
.GetCol() ) < 0 ) {
7667 // Event has been vetoed, set the data back.
7668 SetCellValue(row
,col
,oldval
);
7676 // ------ Grid location functions
7677 // Note that all of these functions work with the logical coordinates of
7678 // grid cells and labels so you will need to convert from device
7679 // coordinates for mouse events etc.
7682 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7684 int row
= YToRow(y
);
7685 int col
= XToCol(x
);
7687 if ( row
== -1 || col
== -1 )
7689 coords
= wxGridNoCellCoords
;
7693 coords
.Set( row
, col
);
7698 // Internal Helper function for computing row or column from some
7699 // (unscrolled) coordinate value, using either
7700 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7701 // of m_rowBottoms/m_ColRights to speed up the search!
7703 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7704 const wxArrayInt
& BorderArray
, int nMax
,
7709 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7715 size_t i_max
= coord
/ defaultDist
,
7718 if (BorderArray
.IsEmpty())
7720 if((int) i_max
< nMax
)
7722 return clipToMinMax
? nMax
- 1 : -1;
7725 if ( i_max
>= BorderArray
.GetCount())
7726 i_max
= BorderArray
.GetCount() - 1;
7729 if ( coord
>= BorderArray
[i_max
])
7733 i_max
= coord
/ minDist
;
7735 i_max
= BorderArray
.GetCount() - 1;
7737 if ( i_max
>= BorderArray
.GetCount())
7738 i_max
= BorderArray
.GetCount() - 1;
7740 if ( coord
>= BorderArray
[i_max
])
7741 return clipToMinMax
? (int)i_max
: -1;
7742 if ( coord
< BorderArray
[0] )
7745 while ( i_max
- i_min
> 0 )
7747 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7748 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7749 if (coord
>= BorderArray
[ i_max
- 1])
7753 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7754 if (coord
< BorderArray
[median
])
7762 int wxGrid::YToRow( int y
)
7764 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7765 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, FALSE
);
7769 int wxGrid::XToCol( int x
)
7771 return CoordToRowOrCol(x
, m_defaultColWidth
,
7772 m_minAcceptableColWidth
, m_colRights
, m_numCols
, FALSE
);
7776 // return the row number that that the y coord is near the edge of, or
7777 // -1 if not near an edge
7779 int wxGrid::YToEdgeOfRow( int y
)
7782 i
= internalYToRow(y
);
7784 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7786 // We know that we are in row i, test whether we are
7787 // close enough to lower or upper border, respectively.
7788 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7790 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7798 // return the col number that that the x coord is near the edge of, or
7799 // -1 if not near an edge
7801 int wxGrid::XToEdgeOfCol( int x
)
7804 i
= internalXToCol(x
);
7806 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7808 // We know that we are in column i, test whether we are
7809 // close enough to right or left border, respectively.
7810 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7812 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7820 wxRect
wxGrid::CellToRect( int row
, int col
)
7822 wxRect
rect( -1, -1, -1, -1 );
7824 if ( row
>= 0 && row
< m_numRows
&&
7825 col
>= 0 && col
< m_numCols
)
7827 int i
, cell_rows
, cell_cols
;
7828 rect
.width
= rect
.height
= 0;
7829 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7830 // if negative then find multicell owner
7831 if (cell_rows
< 0) row
+= cell_rows
;
7832 if (cell_cols
< 0) col
+= cell_cols
;
7833 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7835 rect
.x
= GetColLeft(col
);
7836 rect
.y
= GetRowTop(row
);
7837 for (i
=col
; i
<col
+cell_cols
; i
++)
7838 rect
.width
+= GetColWidth(i
);
7839 for (i
=row
; i
<row
+cell_rows
; i
++)
7840 rect
.height
+= GetRowHeight(i
);
7843 // if grid lines are enabled, then the area of the cell is a bit smaller
7844 if (m_gridLinesEnabled
) {
7852 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7854 // get the cell rectangle in logical coords
7856 wxRect
r( CellToRect( row
, col
) );
7858 // convert to device coords
7860 int left
, top
, right
, bottom
;
7861 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7862 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7864 // check against the client area of the grid window
7867 m_gridWin
->GetClientSize( &cw
, &ch
);
7869 if ( wholeCellVisible
)
7871 // is the cell wholly visible ?
7873 return ( left
>= 0 && right
<= cw
&&
7874 top
>= 0 && bottom
<= ch
);
7878 // is the cell partly visible ?
7880 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7881 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7886 // make the specified cell location visible by doing a minimal amount
7889 void wxGrid::MakeCellVisible( int row
, int col
)
7893 int xpos
= -1, ypos
= -1;
7895 if ( row
>= 0 && row
< m_numRows
&&
7896 col
>= 0 && col
< m_numCols
)
7898 // get the cell rectangle in logical coords
7900 wxRect
r( CellToRect( row
, col
) );
7902 // convert to device coords
7904 int left
, top
, right
, bottom
;
7905 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7906 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7909 m_gridWin
->GetClientSize( &cw
, &ch
);
7915 else if ( bottom
> ch
)
7917 int h
= r
.GetHeight();
7919 for ( i
= row
-1; i
>= 0; i
-- )
7921 int rowHeight
= GetRowHeight(i
);
7922 if ( h
+ rowHeight
> ch
)
7929 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7930 // have rounding errors (this is important, because if we do, we
7931 // might not scroll at all and some cells won't be redrawn)
7933 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7935 ypos
+= GRID_SCROLL_LINE_Y
;
7942 else if ( right
> cw
)
7944 // position the view so that the cell is on the right
7946 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
7947 xpos
= x0
+ (right
- cw
);
7949 // see comment for ypos above
7950 xpos
+= GRID_SCROLL_LINE_X
;
7953 if ( xpos
!= -1 || ypos
!= -1 )
7956 xpos
/= GRID_SCROLL_LINE_X
;
7958 ypos
/= GRID_SCROLL_LINE_Y
;
7959 Scroll( xpos
, ypos
);
7967 // ------ Grid cursor movement functions
7970 bool wxGrid::MoveCursorUp( bool expandSelection
)
7972 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
7973 m_currentCellCoords
.GetRow() >= 0 )
7975 if ( expandSelection
)
7977 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
7978 m_selectingKeyboard
= m_currentCellCoords
;
7979 if ( m_selectingKeyboard
.GetRow() > 0 )
7981 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
7982 MakeCellVisible( m_selectingKeyboard
.GetRow(),
7983 m_selectingKeyboard
.GetCol() );
7984 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
7987 else if ( m_currentCellCoords
.GetRow() > 0 )
7990 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
7991 m_currentCellCoords
.GetCol() );
7992 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
7993 m_currentCellCoords
.GetCol() );
8004 bool wxGrid::MoveCursorDown( bool expandSelection
)
8006 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8007 m_currentCellCoords
.GetRow() < m_numRows
)
8009 if ( expandSelection
)
8011 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8012 m_selectingKeyboard
= m_currentCellCoords
;
8013 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8015 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8016 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8017 m_selectingKeyboard
.GetCol() );
8018 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8021 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8024 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8025 m_currentCellCoords
.GetCol() );
8026 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8027 m_currentCellCoords
.GetCol() );
8038 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8040 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8041 m_currentCellCoords
.GetCol() >= 0 )
8043 if ( expandSelection
)
8045 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8046 m_selectingKeyboard
= m_currentCellCoords
;
8047 if ( m_selectingKeyboard
.GetCol() > 0 )
8049 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8050 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8051 m_selectingKeyboard
.GetCol() );
8052 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8055 else if ( m_currentCellCoords
.GetCol() > 0 )
8058 MakeCellVisible( m_currentCellCoords
.GetRow(),
8059 m_currentCellCoords
.GetCol() - 1 );
8060 SetCurrentCell( m_currentCellCoords
.GetRow(),
8061 m_currentCellCoords
.GetCol() - 1 );
8072 bool wxGrid::MoveCursorRight( bool expandSelection
)
8074 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8075 m_currentCellCoords
.GetCol() < m_numCols
)
8077 if ( expandSelection
)
8079 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8080 m_selectingKeyboard
= m_currentCellCoords
;
8081 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8083 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8084 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8085 m_selectingKeyboard
.GetCol() );
8086 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8089 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8092 MakeCellVisible( m_currentCellCoords
.GetRow(),
8093 m_currentCellCoords
.GetCol() + 1 );
8094 SetCurrentCell( m_currentCellCoords
.GetRow(),
8095 m_currentCellCoords
.GetCol() + 1 );
8106 bool wxGrid::MovePageUp()
8108 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8110 int row
= m_currentCellCoords
.GetRow();
8114 m_gridWin
->GetClientSize( &cw
, &ch
);
8116 int y
= GetRowTop(row
);
8117 int newRow
= internalYToRow( y
- ch
+ 1 );
8119 if ( newRow
== row
)
8121 //row > 0 , so newrow can never be less than 0 here.
8125 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8126 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8134 bool wxGrid::MovePageDown()
8136 if ( m_currentCellCoords
== wxGridNoCellCoords
) return FALSE
;
8138 int row
= m_currentCellCoords
.GetRow();
8139 if ( (row
+1) < m_numRows
)
8142 m_gridWin
->GetClientSize( &cw
, &ch
);
8144 int y
= GetRowTop(row
);
8145 int newRow
= internalYToRow( y
+ ch
);
8146 if ( newRow
== row
)
8148 // row < m_numRows , so newrow can't overflow here.
8152 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8153 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8161 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8164 m_currentCellCoords
!= wxGridNoCellCoords
&&
8165 m_currentCellCoords
.GetRow() > 0 )
8167 int row
= m_currentCellCoords
.GetRow();
8168 int col
= m_currentCellCoords
.GetCol();
8170 if ( m_table
->IsEmptyCell(row
, col
) )
8172 // starting in an empty cell: find the next block of
8178 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8181 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8183 // starting at the top of a block: find the next block
8189 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8194 // starting within a block: find the top of the block
8199 if ( m_table
->IsEmptyCell(row
, col
) )
8207 MakeCellVisible( row
, col
);
8208 if ( expandSelection
)
8210 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8211 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8216 SetCurrentCell( row
, col
);
8224 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8227 m_currentCellCoords
!= wxGridNoCellCoords
&&
8228 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8230 int row
= m_currentCellCoords
.GetRow();
8231 int col
= m_currentCellCoords
.GetCol();
8233 if ( m_table
->IsEmptyCell(row
, col
) )
8235 // starting in an empty cell: find the next block of
8238 while ( row
< m_numRows
-1 )
8241 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8244 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8246 // starting at the bottom of a block: find the next block
8249 while ( row
< m_numRows
-1 )
8252 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8257 // starting within a block: find the bottom of the block
8259 while ( row
< m_numRows
-1 )
8262 if ( m_table
->IsEmptyCell(row
, col
) )
8270 MakeCellVisible( row
, col
);
8271 if ( expandSelection
)
8273 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8274 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8279 SetCurrentCell( row
, col
);
8288 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8291 m_currentCellCoords
!= wxGridNoCellCoords
&&
8292 m_currentCellCoords
.GetCol() > 0 )
8294 int row
= m_currentCellCoords
.GetRow();
8295 int col
= m_currentCellCoords
.GetCol();
8297 if ( m_table
->IsEmptyCell(row
, col
) )
8299 // starting in an empty cell: find the next block of
8305 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8308 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8310 // starting at the left of a block: find the next block
8316 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8321 // starting within a block: find the left of the block
8326 if ( m_table
->IsEmptyCell(row
, col
) )
8334 MakeCellVisible( row
, col
);
8335 if ( expandSelection
)
8337 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8338 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8343 SetCurrentCell( row
, col
);
8352 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8355 m_currentCellCoords
!= wxGridNoCellCoords
&&
8356 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8358 int row
= m_currentCellCoords
.GetRow();
8359 int col
= m_currentCellCoords
.GetCol();
8361 if ( m_table
->IsEmptyCell(row
, col
) )
8363 // starting in an empty cell: find the next block of
8366 while ( col
< m_numCols
-1 )
8369 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8372 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8374 // starting at the right of a block: find the next block
8377 while ( col
< m_numCols
-1 )
8380 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8385 // starting within a block: find the right of the block
8387 while ( col
< m_numCols
-1 )
8390 if ( m_table
->IsEmptyCell(row
, col
) )
8398 MakeCellVisible( row
, col
);
8399 if ( expandSelection
)
8401 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8402 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8407 SetCurrentCell( row
, col
);
8419 // ------ Label values and formatting
8422 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8424 *horiz
= m_rowLabelHorizAlign
;
8425 *vert
= m_rowLabelVertAlign
;
8428 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8430 *horiz
= m_colLabelHorizAlign
;
8431 *vert
= m_colLabelVertAlign
;
8434 int wxGrid::GetColLabelTextOrientation()
8436 return m_colLabelTextOrientation
;
8439 wxString
wxGrid::GetRowLabelValue( int row
)
8443 return m_table
->GetRowLabelValue( row
);
8453 wxString
wxGrid::GetColLabelValue( int col
)
8457 return m_table
->GetColLabelValue( col
);
8468 void wxGrid::SetRowLabelSize( int width
)
8470 width
= wxMax( width
, 0 );
8471 if ( width
!= m_rowLabelWidth
)
8475 m_rowLabelWin
->Show( FALSE
);
8476 m_cornerLabelWin
->Show( FALSE
);
8478 else if ( m_rowLabelWidth
== 0 )
8480 m_rowLabelWin
->Show( TRUE
);
8481 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8484 m_rowLabelWidth
= width
;
8486 wxScrolledWindow::Refresh( TRUE
);
8491 void wxGrid::SetColLabelSize( int height
)
8493 height
= wxMax( height
, 0 );
8494 if ( height
!= m_colLabelHeight
)
8498 m_colLabelWin
->Show( FALSE
);
8499 m_cornerLabelWin
->Show( FALSE
);
8501 else if ( m_colLabelHeight
== 0 )
8503 m_colLabelWin
->Show( TRUE
);
8504 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( TRUE
);
8507 m_colLabelHeight
= height
;
8509 wxScrolledWindow::Refresh( TRUE
);
8514 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8516 if ( m_labelBackgroundColour
!= colour
)
8518 m_labelBackgroundColour
= colour
;
8519 m_rowLabelWin
->SetBackgroundColour( colour
);
8520 m_colLabelWin
->SetBackgroundColour( colour
);
8521 m_cornerLabelWin
->SetBackgroundColour( colour
);
8523 if ( !GetBatchCount() )
8525 m_rowLabelWin
->Refresh();
8526 m_colLabelWin
->Refresh();
8527 m_cornerLabelWin
->Refresh();
8532 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8534 if ( m_labelTextColour
!= colour
)
8536 m_labelTextColour
= colour
;
8537 if ( !GetBatchCount() )
8539 m_rowLabelWin
->Refresh();
8540 m_colLabelWin
->Refresh();
8545 void wxGrid::SetLabelFont( const wxFont
& font
)
8548 if ( !GetBatchCount() )
8550 m_rowLabelWin
->Refresh();
8551 m_colLabelWin
->Refresh();
8555 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8557 // allow old (incorrect) defs to be used
8560 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8561 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8562 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8567 case wxTOP
: vert
= wxALIGN_TOP
; break;
8568 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8569 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8572 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8574 m_rowLabelHorizAlign
= horiz
;
8577 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8579 m_rowLabelVertAlign
= vert
;
8582 if ( !GetBatchCount() )
8584 m_rowLabelWin
->Refresh();
8588 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8590 // allow old (incorrect) defs to be used
8593 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8594 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8595 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8600 case wxTOP
: vert
= wxALIGN_TOP
; break;
8601 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8602 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8605 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8607 m_colLabelHorizAlign
= horiz
;
8610 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8612 m_colLabelVertAlign
= vert
;
8615 if ( !GetBatchCount() )
8617 m_colLabelWin
->Refresh();
8621 // Note: under MSW, the default column label font must be changed because it
8622 // does not support vertical printing
8624 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8625 // pGrid->SetLabelFont(font);
8626 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8628 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8630 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8632 m_colLabelTextOrientation
= textOrientation
;
8635 if ( !GetBatchCount() )
8637 m_colLabelWin
->Refresh();
8641 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8645 m_table
->SetRowLabelValue( row
, s
);
8646 if ( !GetBatchCount() )
8648 wxRect rect
= CellToRect( row
, 0);
8649 if ( rect
.height
> 0 )
8651 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8653 rect
.width
= m_rowLabelWidth
;
8654 m_rowLabelWin
->Refresh( TRUE
, &rect
);
8660 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8664 m_table
->SetColLabelValue( col
, s
);
8665 if ( !GetBatchCount() )
8667 wxRect rect
= CellToRect( 0, col
);
8668 if ( rect
.width
> 0 )
8670 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8672 rect
.height
= m_colLabelHeight
;
8673 m_colLabelWin
->Refresh( TRUE
, &rect
);
8679 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8681 if ( m_gridLineColour
!= colour
)
8683 m_gridLineColour
= colour
;
8685 wxClientDC
dc( m_gridWin
);
8687 DrawAllGridLines( dc
, wxRegion() );
8692 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8694 if ( m_cellHighlightColour
!= colour
)
8696 m_cellHighlightColour
= colour
;
8698 wxClientDC
dc( m_gridWin
);
8700 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8701 DrawCellHighlight(dc
, attr
);
8706 void wxGrid::SetCellHighlightPenWidth(int width
)
8708 if (m_cellHighlightPenWidth
!= width
) {
8709 m_cellHighlightPenWidth
= width
;
8711 // Just redrawing the cell highlight is not enough since that won't
8712 // make any visible change if the the thickness is getting smaller.
8713 int row
= m_currentCellCoords
.GetRow();
8714 int col
= m_currentCellCoords
.GetCol();
8715 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8717 wxRect rect
= CellToRect(row
, col
);
8718 m_gridWin
->Refresh(TRUE
, &rect
);
8722 void wxGrid::SetCellHighlightROPenWidth(int width
)
8724 if (m_cellHighlightROPenWidth
!= width
) {
8725 m_cellHighlightROPenWidth
= width
;
8727 // Just redrawing the cell highlight is not enough since that won't
8728 // make any visible change if the the thickness is getting smaller.
8729 int row
= m_currentCellCoords
.GetRow();
8730 int col
= m_currentCellCoords
.GetCol();
8731 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8733 wxRect rect
= CellToRect(row
, col
);
8734 m_gridWin
->Refresh(TRUE
, &rect
);
8738 void wxGrid::EnableGridLines( bool enable
)
8740 if ( enable
!= m_gridLinesEnabled
)
8742 m_gridLinesEnabled
= enable
;
8744 if ( !GetBatchCount() )
8748 wxClientDC
dc( m_gridWin
);
8750 DrawAllGridLines( dc
, wxRegion() );
8754 m_gridWin
->Refresh();
8761 int wxGrid::GetDefaultRowSize()
8763 return m_defaultRowHeight
;
8766 int wxGrid::GetRowSize( int row
)
8768 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8770 return GetRowHeight(row
);
8773 int wxGrid::GetDefaultColSize()
8775 return m_defaultColWidth
;
8778 int wxGrid::GetColSize( int col
)
8780 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8782 return GetColWidth(col
);
8785 // ============================================================================
8786 // access to the grid attributes: each of them has a default value in the grid
8787 // itself and may be overidden on a per-cell basis
8788 // ============================================================================
8790 // ----------------------------------------------------------------------------
8791 // setting default attributes
8792 // ----------------------------------------------------------------------------
8794 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8796 m_defaultCellAttr
->SetBackgroundColour(col
);
8798 m_gridWin
->SetBackgroundColour(col
);
8802 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8804 m_defaultCellAttr
->SetTextColour(col
);
8807 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8809 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8812 void wxGrid::SetDefaultCellOverflow( bool allow
)
8814 m_defaultCellAttr
->SetOverflow(allow
);
8817 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8819 m_defaultCellAttr
->SetFont(font
);
8822 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8824 m_defaultCellAttr
->SetRenderer(renderer
);
8827 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8829 m_defaultCellAttr
->SetEditor(editor
);
8832 // ----------------------------------------------------------------------------
8833 // access to the default attrbiutes
8834 // ----------------------------------------------------------------------------
8836 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8838 return m_defaultCellAttr
->GetBackgroundColour();
8841 wxColour
wxGrid::GetDefaultCellTextColour()
8843 return m_defaultCellAttr
->GetTextColour();
8846 wxFont
wxGrid::GetDefaultCellFont()
8848 return m_defaultCellAttr
->GetFont();
8851 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8853 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8856 bool wxGrid::GetDefaultCellOverflow()
8858 return m_defaultCellAttr
->GetOverflow();
8861 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8863 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8866 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8868 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8871 // ----------------------------------------------------------------------------
8872 // access to cell attributes
8873 // ----------------------------------------------------------------------------
8875 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8877 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8878 wxColour colour
= attr
->GetBackgroundColour();
8883 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8885 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8886 wxColour colour
= attr
->GetTextColour();
8891 wxFont
wxGrid::GetCellFont( int row
, int col
)
8893 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8894 wxFont font
= attr
->GetFont();
8899 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8901 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8902 attr
->GetAlignment(horiz
, vert
);
8906 bool wxGrid::GetCellOverflow( int row
, int col
)
8908 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8909 bool allow
= attr
->GetOverflow();
8914 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8916 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8917 attr
->GetSize( num_rows
, num_cols
);
8921 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8923 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8924 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
8930 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
8932 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8933 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8939 bool wxGrid::IsReadOnly(int row
, int col
) const
8941 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8942 bool isReadOnly
= attr
->IsReadOnly();
8947 // ----------------------------------------------------------------------------
8948 // attribute support: cache, automatic provider creation, ...
8949 // ----------------------------------------------------------------------------
8951 bool wxGrid::CanHaveAttributes()
8958 return m_table
->CanHaveAttributes();
8961 void wxGrid::ClearAttrCache()
8963 if ( m_attrCache
.row
!= -1 )
8965 wxSafeDecRef(m_attrCache
.attr
);
8966 m_attrCache
.attr
= NULL
;
8967 m_attrCache
.row
= -1;
8971 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
8975 wxGrid
*self
= (wxGrid
*)this; // const_cast
8977 self
->ClearAttrCache();
8978 self
->m_attrCache
.row
= row
;
8979 self
->m_attrCache
.col
= col
;
8980 self
->m_attrCache
.attr
= attr
;
8985 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
8987 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
8989 *attr
= m_attrCache
.attr
;
8990 wxSafeIncRef(m_attrCache
.attr
);
8992 #ifdef DEBUG_ATTR_CACHE
8993 gs_nAttrCacheHits
++;
9000 #ifdef DEBUG_ATTR_CACHE
9001 gs_nAttrCacheMisses
++;
9007 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9009 wxGridCellAttr
*attr
= NULL
;
9010 // Additional test to avoid looking at the cache e.g. for
9011 // wxNoCellCoords, as this will confuse memory management.
9014 if ( !LookupAttr(row
, col
, &attr
) )
9016 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9017 : (wxGridCellAttr
*)NULL
;
9018 CacheAttr(row
, col
, attr
);
9023 attr
->SetDefAttr(m_defaultCellAttr
);
9027 attr
= m_defaultCellAttr
;
9034 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9036 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9038 wxCHECK_MSG( m_table
, attr
,
9039 _T("we may only be called if CanHaveAttributes() returned TRUE and then m_table should be !NULL") );
9041 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9044 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9046 // artificially inc the ref count to match DecRef() in caller
9048 m_table
->SetAttr(attr
, row
, col
);
9054 // ----------------------------------------------------------------------------
9055 // setting column attributes (wrappers around SetColAttr)
9056 // ----------------------------------------------------------------------------
9058 void wxGrid::SetColFormatBool(int col
)
9060 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9063 void wxGrid::SetColFormatNumber(int col
)
9065 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9068 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9070 wxString typeName
= wxGRID_VALUE_FLOAT
;
9071 if ( (width
!= -1) || (precision
!= -1) )
9073 typeName
<< _T(':') << width
<< _T(',') << precision
;
9076 SetColFormatCustom(col
, typeName
);
9079 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9081 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9083 attr
= new wxGridCellAttr
;
9084 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9085 attr
->SetRenderer(renderer
);
9087 SetColAttr(col
, attr
);
9091 // ----------------------------------------------------------------------------
9092 // setting cell attributes: this is forwarded to the table
9093 // ----------------------------------------------------------------------------
9095 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9097 if ( CanHaveAttributes() )
9099 m_table
->SetAttr(attr
, row
, col
);
9108 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9110 if ( CanHaveAttributes() )
9112 m_table
->SetRowAttr(attr
, row
);
9121 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9123 if ( CanHaveAttributes() )
9125 m_table
->SetColAttr(attr
, col
);
9134 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9136 if ( CanHaveAttributes() )
9138 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9139 attr
->SetBackgroundColour(colour
);
9144 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9146 if ( CanHaveAttributes() )
9148 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9149 attr
->SetTextColour(colour
);
9154 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9156 if ( CanHaveAttributes() )
9158 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9159 attr
->SetFont(font
);
9164 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9166 if ( CanHaveAttributes() )
9168 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9169 attr
->SetAlignment(horiz
, vert
);
9174 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9176 if ( CanHaveAttributes() )
9178 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9179 attr
->SetOverflow(allow
);
9184 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9186 if ( CanHaveAttributes() )
9188 int cell_rows
, cell_cols
;
9190 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9191 attr
->GetSize(&cell_rows
, &cell_cols
);
9192 attr
->SetSize(num_rows
, num_cols
);
9195 // Cannot set the size of a cell to 0 or negative values
9196 // While it is perfectly legal to do that, this function cannot
9197 // handle all the possibilies, do it by hand by getting the CellAttr.
9198 // You can only set the size of a cell to 1,1 or greater with this fn
9199 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9200 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9201 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9202 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9204 // if this was already a multicell then "turn off" the other cells first
9205 if ((cell_rows
> 1) || (cell_rows
> 1))
9208 for (j
=row
; j
<row
+cell_rows
; j
++)
9210 for (i
=col
; i
<col
+cell_cols
; i
++)
9212 if ((i
!= col
) || (j
!= row
))
9214 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9215 attr_stub
->SetSize( 1, 1 );
9216 attr_stub
->DecRef();
9222 // mark the cells that will be covered by this cell to
9223 // negative or zero values to point back at this cell
9224 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9227 for (j
=row
; j
<row
+num_rows
; j
++)
9229 for (i
=col
; i
<col
+num_cols
; i
++)
9231 if ((i
!= col
) || (j
!= row
))
9233 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9234 attr_stub
->SetSize( row
-j
, col
-i
);
9235 attr_stub
->DecRef();
9243 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9245 if ( CanHaveAttributes() )
9247 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9248 attr
->SetRenderer(renderer
);
9253 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9255 if ( CanHaveAttributes() )
9257 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9258 attr
->SetEditor(editor
);
9263 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9265 if ( CanHaveAttributes() )
9267 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9268 attr
->SetReadOnly(isReadOnly
);
9273 // ----------------------------------------------------------------------------
9274 // Data type registration
9275 // ----------------------------------------------------------------------------
9277 void wxGrid::RegisterDataType(const wxString
& typeName
,
9278 wxGridCellRenderer
* renderer
,
9279 wxGridCellEditor
* editor
)
9281 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9285 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9287 wxString typeName
= m_table
->GetTypeName(row
, col
);
9288 return GetDefaultEditorForType(typeName
);
9291 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9293 wxString typeName
= m_table
->GetTypeName(row
, col
);
9294 return GetDefaultRendererForType(typeName
);
9298 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9300 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9301 if ( index
== wxNOT_FOUND
)
9303 wxFAIL_MSG(wxT("Unknown data type name"));
9308 return m_typeRegistry
->GetEditor(index
);
9312 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9314 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9315 if ( index
== wxNOT_FOUND
)
9317 wxFAIL_MSG(wxT("Unknown data type name"));
9322 return m_typeRegistry
->GetRenderer(index
);
9326 // ----------------------------------------------------------------------------
9328 // ----------------------------------------------------------------------------
9330 void wxGrid::EnableDragRowSize( bool enable
)
9332 m_canDragRowSize
= enable
;
9336 void wxGrid::EnableDragColSize( bool enable
)
9338 m_canDragColSize
= enable
;
9341 void wxGrid::EnableDragGridSize( bool enable
)
9343 m_canDragGridSize
= enable
;
9347 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9349 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9351 if ( resizeExistingRows
)
9353 // since we are resizing all rows to the default row size,
9354 // we can simply clear the row heights and row bottoms
9355 // arrays (which also allows us to take advantage of
9356 // some speed optimisations)
9357 m_rowHeights
.Empty();
9358 m_rowBottoms
.Empty();
9359 if ( !GetBatchCount() )
9364 void wxGrid::SetRowSize( int row
, int height
)
9366 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9368 // See comment in SetColSize
9369 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9371 if ( m_rowHeights
.IsEmpty() )
9373 // need to really create the array
9377 int h
= wxMax( 0, height
);
9378 int diff
= h
- m_rowHeights
[row
];
9380 m_rowHeights
[row
] = h
;
9382 for ( i
= row
; i
< m_numRows
; i
++ )
9384 m_rowBottoms
[i
] += diff
;
9386 if ( !GetBatchCount() )
9390 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9392 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9394 if ( resizeExistingCols
)
9396 // since we are resizing all columns to the default column size,
9397 // we can simply clear the col widths and col rights
9398 // arrays (which also allows us to take advantage of
9399 // some speed optimisations)
9400 m_colWidths
.Empty();
9401 m_colRights
.Empty();
9402 if ( !GetBatchCount() )
9407 void wxGrid::SetColSize( int col
, int width
)
9409 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9411 // should we check that it's bigger than GetColMinimalWidth(col) here?
9413 // No, because it is reasonable to assume the library user know's
9414 // what he is doing. However whe should test against the weaker
9415 // constariant of minimalAcceptableWidth, as this breaks rendering
9417 // This test then fixes sf.net bug #645734
9419 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9421 if ( m_colWidths
.IsEmpty() )
9423 // need to really create the array
9427 // if < 0 calc new width from label
9431 wxArrayString lines
;
9432 wxClientDC
dc(m_colLabelWin
);
9433 dc
.SetFont(GetLabelFont());
9434 StringToLines(GetColLabelValue(col
), lines
);
9435 GetTextBoxSize(dc
, lines
, &w
, &h
);
9438 int w
= wxMax( 0, width
);
9439 int diff
= w
- m_colWidths
[col
];
9440 m_colWidths
[col
] = w
;
9443 for ( i
= col
; i
< m_numCols
; i
++ )
9445 m_colRights
[i
] += diff
;
9447 if ( !GetBatchCount() )
9452 void wxGrid::SetColMinimalWidth( int col
, int width
)
9454 if (width
> GetColMinimalAcceptableWidth()) {
9455 m_colMinWidths
[col
] = width
;
9459 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9461 if (width
> GetRowMinimalAcceptableHeight()) {
9462 m_rowMinHeights
[row
] = width
;
9466 int wxGrid::GetColMinimalWidth(int col
) const
9468 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9469 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9472 int wxGrid::GetRowMinimalHeight(int row
) const
9474 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9475 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9478 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9480 // We do allow a width of 0 since this gives us
9481 // an easy way to temporarily hidding columns.
9484 m_minAcceptableColWidth
= width
;
9487 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9489 // We do allow a height of 0 since this gives us
9490 // an easy way to temporarily hidding rows.
9493 m_minAcceptableRowHeight
= height
;
9496 int wxGrid::GetColMinimalAcceptableWidth() const
9498 return m_minAcceptableColWidth
;
9501 int wxGrid::GetRowMinimalAcceptableHeight() const
9503 return m_minAcceptableRowHeight
;
9506 // ----------------------------------------------------------------------------
9508 // ----------------------------------------------------------------------------
9510 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9512 wxClientDC
dc(m_gridWin
);
9514 // init both of them to avoid compiler warnings, even if weo nly need one
9522 wxCoord extent
, extentMax
= 0;
9523 int max
= column
? m_numRows
: m_numCols
;
9524 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9531 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9532 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9535 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9536 extent
= column
? size
.x
: size
.y
;
9537 if ( extent
> extentMax
)
9548 // now also compare with the column label extent
9550 dc
.SetFont( GetLabelFont() );
9554 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9555 if( GetColLabelTextOrientation() == wxVERTICAL
)
9559 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9561 extent
= column
? w
: h
;
9562 if ( extent
> extentMax
)
9569 // empty column - give default extent (notice that if extentMax is less
9570 // than default extent but != 0, it's ok)
9571 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9577 // leave some space around text
9588 SetColSize(col
, extentMax
);
9589 if ( !GetBatchCount() )
9592 m_gridWin
->GetClientSize( &cw
, &ch
);
9593 wxRect
rect ( CellToRect( 0, col
) );
9595 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9596 rect
.width
= cw
- rect
.x
;
9597 rect
.height
= m_colLabelHeight
;
9598 m_colLabelWin
->Refresh( TRUE
, &rect
);
9603 SetRowSize(row
, extentMax
);
9604 if ( !GetBatchCount() )
9607 m_gridWin
->GetClientSize( &cw
, &ch
);
9608 wxRect
rect ( CellToRect( row
, 0 ) );
9610 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9611 rect
.width
= m_rowLabelWidth
;
9612 rect
.height
= ch
- rect
.y
;
9613 m_rowLabelWin
->Refresh( TRUE
, &rect
);
9619 SetColMinimalWidth(col
, extentMax
);
9621 SetRowMinimalHeight(row
, extentMax
);
9625 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9627 int width
= m_rowLabelWidth
;
9632 for ( int col
= 0; col
< m_numCols
; col
++ )
9636 AutoSizeColumn(col
, setAsMin
);
9639 width
+= GetColWidth(col
);
9648 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9650 int height
= m_colLabelHeight
;
9655 for ( int row
= 0; row
< m_numRows
; row
++ )
9659 AutoSizeRow(row
, setAsMin
);
9662 height
+= GetRowHeight(row
);
9671 void wxGrid::AutoSize()
9675 wxSize
size(SetOrCalcColumnSizes(FALSE
), SetOrCalcRowSizes(FALSE
));
9677 // round up the size to a multiple of scroll step - this ensures that we
9678 // won't get the scrollbars if we're sized exactly to this width
9679 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9681 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9682 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9684 // distribute the extra space between the columns/rows to avoid having
9685 // extra white space
9687 // Remove the extra m_extraWidth + 1 added above
9688 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9689 if ( diff
&& m_numCols
)
9691 // try to resize the columns uniformly
9692 wxCoord diffPerCol
= diff
/ m_numCols
;
9695 for ( int col
= 0; col
< m_numCols
; col
++ )
9697 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9701 // add remaining amount to the last columns
9702 diff
-= diffPerCol
* m_numCols
;
9705 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9707 SetColSize(col
, GetColWidth(col
) + 1);
9713 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9714 if ( diff
&& m_numRows
)
9716 // try to resize the columns uniformly
9717 wxCoord diffPerRow
= diff
/ m_numRows
;
9720 for ( int row
= 0; row
< m_numRows
; row
++ )
9722 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9726 // add remaining amount to the last rows
9727 diff
-= diffPerRow
* m_numRows
;
9730 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9732 SetRowSize(row
, GetRowHeight(row
) + 1);
9739 SetClientSize(sizeFit
);
9742 void wxGrid::AutoSizeRowLabelSize( int row
)
9744 wxArrayString lines
;
9747 // Hide the edit control, so it
9748 // won't interfer with drag-shrinking.
9749 if( IsCellEditControlShown() )
9751 HideCellEditControl();
9752 SaveEditControlValue();
9755 // autosize row height depending on label text
9756 StringToLines( GetRowLabelValue( row
), lines
);
9757 wxClientDC
dc( m_rowLabelWin
);
9758 GetTextBoxSize( dc
, lines
, &w
, &h
);
9759 if( h
< m_defaultRowHeight
)
9760 h
= m_defaultRowHeight
;
9765 void wxGrid::AutoSizeColLabelSize( int col
)
9767 wxArrayString lines
;
9770 // Hide the edit control, so it
9771 // won't interfer with drag-shrinking.
9772 if( IsCellEditControlShown() )
9774 HideCellEditControl();
9775 SaveEditControlValue();
9778 // autosize column width depending on label text
9779 StringToLines( GetColLabelValue( col
), lines
);
9780 wxClientDC
dc( m_colLabelWin
);
9781 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9782 GetTextBoxSize( dc
, lines
, &w
, &h
);
9784 GetTextBoxSize( dc
, lines
, &h
, &w
);
9785 if( w
< m_defaultColWidth
)
9786 w
= m_defaultColWidth
;
9791 wxSize
wxGrid::DoGetBestSize() const
9793 // don't set sizes, only calculate them
9794 wxGrid
*self
= (wxGrid
*)this; // const_cast
9797 width
= self
->SetOrCalcColumnSizes(TRUE
);
9798 height
= self
->SetOrCalcRowSizes(TRUE
);
9800 int maxwidth
, maxheight
;
9801 wxDisplaySize( & maxwidth
, & maxheight
);
9803 if ( width
> maxwidth
) width
= maxwidth
;
9804 if ( height
> maxheight
) height
= maxheight
;
9806 return wxSize( width
, height
);
9815 wxPen
& wxGrid::GetDividerPen() const
9820 // ----------------------------------------------------------------------------
9821 // cell value accessor functions
9822 // ----------------------------------------------------------------------------
9824 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9828 m_table
->SetValue( row
, col
, s
);
9829 if ( !GetBatchCount() )
9832 wxRect
rect( CellToRect( row
, col
) );
9834 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9835 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9836 m_gridWin
->Refresh( FALSE
, &rect
);
9839 if ( m_currentCellCoords
.GetRow() == row
&&
9840 m_currentCellCoords
.GetCol() == col
&&
9841 IsCellEditControlShown())
9842 // Note: If we are using IsCellEditControlEnabled,
9843 // this interacts badly with calling SetCellValue from
9844 // an EVT_GRID_CELL_CHANGE handler.
9846 HideCellEditControl();
9847 ShowCellEditControl(); // will reread data from table
9854 // ------ Block, row and col selection
9857 void wxGrid::SelectRow( int row
, bool addToSelected
)
9859 if ( IsSelection() && !addToSelected
)
9863 m_selection
->SelectRow( row
, FALSE
, addToSelected
);
9867 void wxGrid::SelectCol( int col
, bool addToSelected
)
9869 if ( IsSelection() && !addToSelected
)
9873 m_selection
->SelectCol( col
, FALSE
, addToSelected
);
9877 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9878 bool addToSelected
)
9880 if ( IsSelection() && !addToSelected
)
9884 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9885 FALSE
, addToSelected
);
9889 void wxGrid::SelectAll()
9891 if ( m_numRows
> 0 && m_numCols
> 0 )
9894 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9899 // ------ Cell, row and col deselection
9902 void wxGrid::DeselectRow( int row
)
9907 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
9909 if ( m_selection
->IsInSelection(row
, 0 ) )
9910 m_selection
->ToggleCellSelection( row
, 0);
9914 int nCols
= GetNumberCols();
9915 for ( int i
= 0; i
< nCols
; i
++ )
9917 if ( m_selection
->IsInSelection(row
, i
) )
9918 m_selection
->ToggleCellSelection( row
, i
);
9923 void wxGrid::DeselectCol( int col
)
9928 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
9930 if ( m_selection
->IsInSelection(0, col
) )
9931 m_selection
->ToggleCellSelection( 0, col
);
9935 int nRows
= GetNumberRows();
9936 for ( int i
= 0; i
< nRows
; i
++ )
9938 if ( m_selection
->IsInSelection(i
, col
) )
9939 m_selection
->ToggleCellSelection(i
, col
);
9944 void wxGrid::DeselectCell( int row
, int col
)
9946 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
9947 m_selection
->ToggleCellSelection(row
, col
);
9950 bool wxGrid::IsSelection()
9952 return ( m_selection
&& (m_selection
->IsSelection() ||
9953 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
9954 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
9957 bool wxGrid::IsInSelection( int row
, int col
) const
9959 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
9960 ( row
>= m_selectingTopLeft
.GetRow() &&
9961 col
>= m_selectingTopLeft
.GetCol() &&
9962 row
<= m_selectingBottomRight
.GetRow() &&
9963 col
<= m_selectingBottomRight
.GetCol() )) );
9966 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
9968 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9969 return m_selection
->m_cellSelection
;
9971 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
9973 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9974 return m_selection
->m_blockSelectionTopLeft
;
9976 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
9978 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
9979 return m_selection
->m_blockSelectionBottomRight
;
9981 wxArrayInt
wxGrid::GetSelectedRows() const
9983 if (!m_selection
) { wxArrayInt a
; return a
; }
9984 return m_selection
->m_rowSelection
;
9986 wxArrayInt
wxGrid::GetSelectedCols() const
9988 if (!m_selection
) { wxArrayInt a
; return a
; }
9989 return m_selection
->m_colSelection
;
9993 void wxGrid::ClearSelection()
9995 m_selectingTopLeft
= wxGridNoCellCoords
;
9996 m_selectingBottomRight
= wxGridNoCellCoords
;
9998 m_selection
->ClearSelection();
10002 // This function returns the rectangle that encloses the given block
10003 // in device coords clipped to the client size of the grid window.
10005 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10006 const wxGridCellCoords
&bottomRight
)
10008 wxRect
rect( wxGridNoCellRect
);
10011 cellRect
= CellToRect( topLeft
);
10012 if ( cellRect
!= wxGridNoCellRect
)
10018 rect
= wxRect( 0, 0, 0, 0 );
10021 cellRect
= CellToRect( bottomRight
);
10022 if ( cellRect
!= wxGridNoCellRect
)
10028 return wxGridNoCellRect
;
10032 int left
= rect
.GetLeft();
10033 int top
= rect
.GetTop();
10034 int right
= rect
.GetRight();
10035 int bottom
= rect
.GetBottom();
10037 int leftCol
= topLeft
.GetCol();
10038 int topRow
= topLeft
.GetRow();
10039 int rightCol
= bottomRight
.GetCol();
10040 int bottomRow
= bottomRight
.GetRow();
10058 topRow
= bottomRow
;
10063 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10065 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10067 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10069 cellRect
= CellToRect( j
, i
);
10071 if (cellRect
.x
< left
)
10073 if (cellRect
.y
< top
)
10075 if (cellRect
.x
+ cellRect
.width
> right
)
10076 right
= cellRect
.x
+ cellRect
.width
;
10077 if (cellRect
.y
+ cellRect
.height
> bottom
)
10078 bottom
= cellRect
.y
+ cellRect
.height
;
10080 else i
= rightCol
; // jump over inner cells.
10084 // convert to scrolled coords
10086 CalcScrolledPosition( left
, top
, &left
, &top
);
10087 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10090 m_gridWin
->GetClientSize( &cw
, &ch
);
10092 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10093 return wxRect( 0, 0, 0, 0);
10095 rect
.SetLeft( wxMax(0, left
) );
10096 rect
.SetTop( wxMax(0, top
) );
10097 rect
.SetRight( wxMin(cw
, right
) );
10098 rect
.SetBottom( wxMin(ch
, bottom
) );
10104 // ------ Grid event classes
10107 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10109 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10110 int row
, int col
, int x
, int y
, bool sel
,
10111 bool control
, bool shift
, bool alt
, bool meta
)
10112 : wxNotifyEvent( type
, id
)
10119 m_control
= control
;
10124 SetEventObject(obj
);
10128 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10130 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10131 int rowOrCol
, int x
, int y
,
10132 bool control
, bool shift
, bool alt
, bool meta
)
10133 : wxNotifyEvent( type
, id
)
10135 m_rowOrCol
= rowOrCol
;
10138 m_control
= control
;
10143 SetEventObject(obj
);
10147 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10149 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10150 const wxGridCellCoords
& topLeft
,
10151 const wxGridCellCoords
& bottomRight
,
10152 bool sel
, bool control
,
10153 bool shift
, bool alt
, bool meta
)
10154 : wxNotifyEvent( type
, id
)
10156 m_topLeft
= topLeft
;
10157 m_bottomRight
= bottomRight
;
10159 m_control
= control
;
10164 SetEventObject(obj
);
10168 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10170 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10171 wxObject
* obj
, int row
,
10172 int col
, wxControl
* ctrl
)
10173 : wxCommandEvent(type
, id
)
10175 SetEventObject(obj
);
10181 #endif // wxUSE_GRID