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_CELL_BEGIN_DRAG
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
123 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
124 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
133 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
134 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
135 const wxPoint
&pos
, const wxSize
&size
);
140 void OnPaint( wxPaintEvent
& event
);
141 void OnMouseEvent( wxMouseEvent
& event
);
142 void OnMouseWheel( wxMouseEvent
& event
);
143 void OnKeyDown( wxKeyEvent
& event
);
144 void OnKeyUp( wxKeyEvent
& );
146 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
147 DECLARE_EVENT_TABLE()
148 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
152 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
155 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
156 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
157 const wxPoint
&pos
, const wxSize
&size
);
162 void OnPaint( wxPaintEvent
&event
);
163 void OnMouseEvent( wxMouseEvent
& event
);
164 void OnMouseWheel( wxMouseEvent
& event
);
165 void OnKeyDown( wxKeyEvent
& event
);
166 void OnKeyUp( wxKeyEvent
& );
168 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
169 DECLARE_EVENT_TABLE()
170 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
174 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
177 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
178 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
179 const wxPoint
&pos
, const wxSize
&size
);
184 void OnMouseEvent( wxMouseEvent
& event
);
185 void OnMouseWheel( wxMouseEvent
& event
);
186 void OnKeyDown( wxKeyEvent
& event
);
187 void OnKeyUp( wxKeyEvent
& );
188 void OnPaint( wxPaintEvent
& event
);
190 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
191 DECLARE_EVENT_TABLE()
192 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
195 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
200 m_owner
= (wxGrid
*)NULL
;
201 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
202 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
205 wxGridWindow( wxGrid
*parent
,
206 wxGridRowLabelWindow
*rowLblWin
,
207 wxGridColLabelWindow
*colLblWin
,
208 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
211 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
213 wxGrid
* GetOwner() { return m_owner
; }
217 wxGridRowLabelWindow
*m_rowLabelWin
;
218 wxGridColLabelWindow
*m_colLabelWin
;
220 void OnPaint( wxPaintEvent
&event
);
221 void OnMouseWheel( wxMouseEvent
& event
);
222 void OnMouseEvent( wxMouseEvent
& event
);
223 void OnKeyDown( wxKeyEvent
& );
224 void OnKeyUp( wxKeyEvent
& );
225 void OnEraseBackground( wxEraseEvent
& );
226 void OnFocus( wxFocusEvent
& );
228 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
229 DECLARE_EVENT_TABLE()
230 DECLARE_NO_COPY_CLASS(wxGridWindow
)
235 class wxGridCellEditorEvtHandler
: public wxEvtHandler
238 wxGridCellEditorEvtHandler()
239 : m_grid(0), m_editor(0)
241 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
242 : m_grid(grid
), m_editor(editor
)
245 void OnKeyDown(wxKeyEvent
& event
);
246 void OnChar(wxKeyEvent
& event
);
250 wxGridCellEditor
* m_editor
;
251 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
252 DECLARE_EVENT_TABLE()
253 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
257 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
258 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
259 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
260 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
265 // ----------------------------------------------------------------------------
266 // the internal data representation used by wxGridCellAttrProvider
267 // ----------------------------------------------------------------------------
269 // this class stores attributes set for cells
270 class WXDLLIMPEXP_ADV wxGridCellAttrData
273 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
274 wxGridCellAttr
*GetAttr(int row
, int col
) const;
275 void UpdateAttrRows( size_t pos
, int numRows
);
276 void UpdateAttrCols( size_t pos
, int numCols
);
279 // searches for the attr for given cell, returns wxNOT_FOUND if not found
280 int FindIndex(int row
, int col
) const;
282 wxGridCellWithAttrArray m_attrs
;
285 // this class stores attributes set for rows or columns
286 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
289 // empty ctor to suppress warnings
290 wxGridRowOrColAttrData() { }
291 ~wxGridRowOrColAttrData();
293 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
294 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
295 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
298 wxArrayInt m_rowsOrCols
;
299 wxArrayAttrs m_attrs
;
302 // NB: this is just a wrapper around 3 objects: one which stores cell
303 // attributes, and 2 others for row/col ones
304 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
307 wxGridCellAttrData m_cellAttrs
;
308 wxGridRowOrColAttrData m_rowAttrs
,
313 // ----------------------------------------------------------------------------
314 // data structures used for the data type registry
315 // ----------------------------------------------------------------------------
317 struct wxGridDataTypeInfo
319 wxGridDataTypeInfo(const wxString
& typeName
,
320 wxGridCellRenderer
* renderer
,
321 wxGridCellEditor
* editor
)
322 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
325 ~wxGridDataTypeInfo()
327 wxSafeDecRef(m_renderer
);
328 wxSafeDecRef(m_editor
);
332 wxGridCellRenderer
* m_renderer
;
333 wxGridCellEditor
* m_editor
;
335 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
339 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
340 class WXDLLIMPEXP_ADV
);
343 class WXDLLIMPEXP_ADV wxGridTypeRegistry
346 wxGridTypeRegistry() {}
347 ~wxGridTypeRegistry();
349 void RegisterDataType(const wxString
& typeName
,
350 wxGridCellRenderer
* renderer
,
351 wxGridCellEditor
* editor
);
353 // find one of already registered data types
354 int FindRegisteredDataType(const wxString
& typeName
);
356 // try to FindRegisteredDataType(), if this fails and typeName is one of
357 // standard typenames, register it and return its index
358 int FindDataType(const wxString
& typeName
);
360 // try to FindDataType(), if it fails see if it is not one of already
361 // registered data types with some params in which case clone the
362 // registered data type and set params for it
363 int FindOrCloneDataType(const wxString
& typeName
);
365 wxGridCellRenderer
* GetRenderer(int index
);
366 wxGridCellEditor
* GetEditor(int index
);
369 wxGridDataTypeInfoArray m_typeinfo
;
372 // ----------------------------------------------------------------------------
373 // conditional compilation
374 // ----------------------------------------------------------------------------
376 #ifndef WXGRID_DRAW_LINES
377 #define WXGRID_DRAW_LINES 1
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 //#define DEBUG_ATTR_CACHE
385 #ifdef DEBUG_ATTR_CACHE
386 static size_t gs_nAttrCacheHits
= 0;
387 static size_t gs_nAttrCacheMisses
= 0;
388 #endif // DEBUG_ATTR_CACHE
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
395 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
398 // TODO: this doesn't work at all, grid cells have different sizes and approx
399 // calculations don't work as because of the size mismatch scrollbars
400 // sometimes fail to be shown when they should be or vice versa
402 // The scroll bars may be a little flakey once in a while, but that is
403 // surely much less horrible than having scroll lines of only 1!!!
406 // Well, it's still seriously broken so it might be better but needs
409 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
410 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
412 // the size of hash tables used a bit everywhere (the max number of elements
413 // in these hash tables is the number of rows/columns)
414 static const int GRID_HASH_SIZE
= 100;
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 static inline int GetScrollX(int x
)
422 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
425 static inline int GetScrollY(int y
)
427 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
430 // ============================================================================
432 // ============================================================================
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 wxGridCellEditor::wxGridCellEditor()
445 wxGridCellEditor::~wxGridCellEditor()
450 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
451 wxWindowID
WXUNUSED(id
),
452 wxEvtHandler
* evtHandler
)
455 m_control
->PushEventHandler(evtHandler
);
458 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
459 wxGridCellAttr
*attr
)
461 // erase the background because we might not fill the cell
462 wxClientDC
dc(m_control
->GetParent());
463 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
465 gridWindow
->GetOwner()->PrepareDC(dc
);
467 dc
.SetPen(*wxTRANSPARENT_PEN
);
468 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
469 dc
.DrawRectangle(rectCell
);
471 // redraw the control we just painted over
472 m_control
->Refresh();
475 void wxGridCellEditor::Destroy()
479 m_control
->PopEventHandler(true /* delete it*/);
481 m_control
->Destroy();
486 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
488 wxASSERT_MSG(m_control
,
489 wxT("The wxGridCellEditor must be Created first!"));
490 m_control
->Show(show
);
494 // set the colours/fonts if we have any
497 m_colFgOld
= m_control
->GetForegroundColour();
498 m_control
->SetForegroundColour(attr
->GetTextColour());
500 m_colBgOld
= m_control
->GetBackgroundColour();
501 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
503 m_fontOld
= m_control
->GetFont();
504 m_control
->SetFont(attr
->GetFont());
506 // can't do anything more in the base class version, the other
507 // attributes may only be used by the derived classes
512 // restore the standard colours fonts
513 if ( m_colFgOld
.Ok() )
515 m_control
->SetForegroundColour(m_colFgOld
);
516 m_colFgOld
= wxNullColour
;
519 if ( m_colBgOld
.Ok() )
521 m_control
->SetBackgroundColour(m_colBgOld
);
522 m_colBgOld
= wxNullColour
;
525 if ( m_fontOld
.Ok() )
527 m_control
->SetFont(m_fontOld
);
528 m_fontOld
= wxNullFont
;
533 void wxGridCellEditor::SetSize(const wxRect
& rect
)
535 wxASSERT_MSG(m_control
,
536 wxT("The wxGridCellEditor must be Created first!"));
537 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
540 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
545 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
547 // accept the simple key presses, not anything with Ctrl/Alt/Meta
548 return !(event
.ControlDown() || event
.AltDown());
551 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
556 void wxGridCellEditor::StartingClick()
562 // ----------------------------------------------------------------------------
563 // wxGridCellTextEditor
564 // ----------------------------------------------------------------------------
566 wxGridCellTextEditor::wxGridCellTextEditor()
571 void wxGridCellTextEditor::Create(wxWindow
* parent
,
573 wxEvtHandler
* evtHandler
)
575 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
576 wxDefaultPosition
, wxDefaultSize
577 #if defined(__WXMSW__)
578 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
582 // set max length allowed in the textctrl, if the parameter was set
585 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
588 wxGridCellEditor::Create(parent
, id
, evtHandler
);
591 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
592 wxGridCellAttr
* WXUNUSED(attr
))
594 // as we fill the entire client area, don't do anything here to minimize
598 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
600 wxRect
rect(rectOrig
);
602 // Make the edit control large enough to allow for internal
605 // TODO: remove this if the text ctrl sizing is improved esp. for
608 #if defined(__WXGTK__)
617 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
619 // MB: treat MSW separately here otherwise the caret doesn't show
620 // when the editor is in the first row.
621 #if defined(__WXMSW__)
624 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
627 #if defined(__WXMOTIF__)
631 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
632 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
633 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
634 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
637 wxGridCellEditor::SetSize(rect
);
640 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
642 wxASSERT_MSG(m_control
,
643 wxT("The wxGridCellEditor must be Created first!"));
645 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
647 DoBeginEdit(m_startValue
);
650 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
652 Text()->SetValue(startValue
);
653 Text()->SetInsertionPointEnd();
654 Text()->SetSelection(-1,-1);
658 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
661 wxASSERT_MSG(m_control
,
662 wxT("The wxGridCellEditor must be Created first!"));
664 bool changed
= false;
665 wxString value
= Text()->GetValue();
666 if (value
!= m_startValue
)
670 grid
->GetTable()->SetValue(row
, col
, value
);
672 m_startValue
= wxEmptyString
;
673 // No point in setting the text of the hidden control
674 //Text()->SetValue(m_startValue);
680 void wxGridCellTextEditor::Reset()
682 wxASSERT_MSG(m_control
,
683 wxT("The wxGridCellEditor must be Created first!"));
685 DoReset(m_startValue
);
688 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
690 Text()->SetValue(startValue
);
691 Text()->SetInsertionPointEnd();
694 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
696 if ( wxGridCellEditor::IsAcceptedKey(event
) )
698 int keycode
= event
.GetKeyCode();
712 case WXK_NUMPAD_MULTIPLY
:
716 case WXK_NUMPAD_SUBTRACT
:
718 case WXK_NUMPAD_DECIMAL
:
720 case WXK_NUMPAD_DIVIDE
:
724 // accept 8 bit chars too if isprint() agrees
725 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
733 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
735 if ( !Text()->EmulateKeyPress(event
) )
741 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
742 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
744 #if defined(__WXMOTIF__) || defined(__WXGTK__)
745 // wxMotif needs a little extra help...
746 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
747 wxString
s( Text()->GetValue() );
748 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
750 Text()->SetInsertionPoint( pos
);
752 // the other ports can handle a Return key press
758 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
768 if ( !params
.ToLong(&tmp
) )
770 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
774 m_maxChars
= (size_t)tmp
;
779 // return the value in the text control
780 wxString
wxGridCellTextEditor::GetValue() const
782 return Text()->GetValue();
785 // ----------------------------------------------------------------------------
786 // wxGridCellNumberEditor
787 // ----------------------------------------------------------------------------
789 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
795 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
797 wxEvtHandler
* evtHandler
)
801 // create a spin ctrl
802 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
803 wxDefaultPosition
, wxDefaultSize
,
807 wxGridCellEditor::Create(parent
, id
, evtHandler
);
811 // just a text control
812 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
815 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
816 #endif // wxUSE_VALIDATORS
820 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
822 // first get the value
823 wxGridTableBase
*table
= grid
->GetTable();
824 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
826 m_valueOld
= table
->GetValueAsLong(row
, col
);
831 wxString sValue
= table
->GetValue(row
, col
);
832 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.IsEmpty())
834 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
841 Spin()->SetValue((int)m_valueOld
);
846 DoBeginEdit(GetString());
850 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
859 value
= Spin()->GetValue();
860 changed
= value
!= m_valueOld
;
862 text
= wxString::Format(wxT("%ld"), value
);
866 text
= Text()->GetValue();
867 changed
= (text
.IsEmpty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
872 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
873 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
875 grid
->GetTable()->SetValue(row
, col
, text
);
881 void wxGridCellNumberEditor::Reset()
885 Spin()->SetValue((int)m_valueOld
);
889 DoReset(GetString());
893 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
895 if ( wxGridCellEditor::IsAcceptedKey(event
) )
897 int keycode
= event
.GetKeyCode();
913 case WXK_NUMPAD_SUBTRACT
:
919 if ( (keycode
< 128) && wxIsdigit(keycode
) )
927 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
931 int keycode
= event
.GetKeyCode();
932 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
933 || keycode
== WXK_NUMPAD0
934 || keycode
== WXK_NUMPAD1
935 || keycode
== WXK_NUMPAD2
936 || keycode
== WXK_NUMPAD3
937 || keycode
== WXK_NUMPAD4
938 || keycode
== WXK_NUMPAD5
939 || keycode
== WXK_NUMPAD6
940 || keycode
== WXK_NUMPAD7
941 || keycode
== WXK_NUMPAD8
942 || keycode
== WXK_NUMPAD9
943 || keycode
== WXK_ADD
944 || keycode
== WXK_NUMPAD_ADD
945 || keycode
== WXK_SUBTRACT
946 || keycode
== WXK_NUMPAD_SUBTRACT
)
948 wxGridCellTextEditor::StartingKey(event
);
958 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
969 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
973 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
977 // skip the error message below
982 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
986 // return the value in the spin control if it is there (the text control otherwise)
987 wxString
wxGridCellNumberEditor::GetValue() const
993 long value
= Spin()->GetValue();
994 s
.Printf(wxT("%ld"), value
);
998 s
= Text()->GetValue();
1003 // ----------------------------------------------------------------------------
1004 // wxGridCellFloatEditor
1005 // ----------------------------------------------------------------------------
1007 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1010 m_precision
= precision
;
1013 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1015 wxEvtHandler
* evtHandler
)
1017 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1019 #if wxUSE_VALIDATORS
1020 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1021 #endif // wxUSE_VALIDATORS
1024 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1026 // first get the value
1027 wxGridTableBase
*table
= grid
->GetTable();
1028 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1030 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1035 wxString sValue
= table
->GetValue(row
, col
);
1036 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.IsEmpty())
1038 wxFAIL_MSG( _T("this cell doesn't have float value") );
1043 DoBeginEdit(GetString());
1046 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1050 wxString
text(Text()->GetValue());
1052 if ( (text
.IsEmpty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1054 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1055 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1057 grid
->GetTable()->SetValue(row
, col
, text
);
1064 void wxGridCellFloatEditor::Reset()
1066 DoReset(GetString());
1069 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1071 int keycode
= event
.GetKeyCode();
1073 tmpbuf
[0] = (char) keycode
;
1075 bool is_decimal_point
= ( wxString(tmpbuf
, *wxConvCurrent
) ==
1076 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1077 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1079 || keycode
== WXK_NUMPAD0
1080 || keycode
== WXK_NUMPAD1
1081 || keycode
== WXK_NUMPAD2
1082 || keycode
== WXK_NUMPAD3
1083 || keycode
== WXK_NUMPAD4
1084 || keycode
== WXK_NUMPAD5
1085 || keycode
== WXK_NUMPAD6
1086 || keycode
== WXK_NUMPAD7
1087 || keycode
== WXK_NUMPAD8
1088 || keycode
== WXK_NUMPAD9
1089 || keycode
== WXK_ADD
1090 || keycode
== WXK_NUMPAD_ADD
1091 || keycode
== WXK_SUBTRACT
1092 || keycode
== WXK_NUMPAD_SUBTRACT
)
1094 wxGridCellTextEditor::StartingKey(event
);
1096 // skip Skip() below
1103 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1114 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1118 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1120 m_precision
= (int)tmp
;
1122 // skip the error message below
1127 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1131 wxString
wxGridCellFloatEditor::GetString() const
1134 if ( m_width
== -1 )
1136 // default width/precision
1139 else if ( m_precision
== -1 )
1141 // default precision
1142 fmt
.Printf(_T("%%%d.f"), m_width
);
1146 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1149 return wxString::Format(fmt
, m_valueOld
);
1152 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1154 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1156 int keycode
= event
.GetKeyCode();
1170 case WXK_NUMPAD_ADD
:
1172 case WXK_NUMPAD_SUBTRACT
:
1174 case WXK_NUMPAD_DECIMAL
:
1179 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1181 tmpbuf
[0] = (char) keycode
;
1183 bool is_decimal_point
=
1184 ( wxString(tmpbuf
, *wxConvCurrent
) ==
1185 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1186 wxLOCALE_CAT_NUMBER
) );
1187 if ( (keycode
< 128) &&
1188 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1189 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1198 #endif // wxUSE_TEXTCTRL
1202 // ----------------------------------------------------------------------------
1203 // wxGridCellBoolEditor
1204 // ----------------------------------------------------------------------------
1206 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1208 wxEvtHandler
* evtHandler
)
1210 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1211 wxDefaultPosition
, wxDefaultSize
,
1214 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1217 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1219 bool resize
= false;
1220 wxSize size
= m_control
->GetSize();
1221 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1223 // check if the checkbox is not too big/small for this cell
1224 wxSize sizeBest
= m_control
->GetBestSize();
1225 if ( !(size
== sizeBest
) )
1227 // reset to default size if it had been made smaller
1233 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1235 // leave 1 pixel margin
1236 size
.x
= size
.y
= minSize
- 2;
1243 m_control
->SetSize(size
);
1246 // position it in the centre of the rectangle (TODO: support alignment?)
1248 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1249 // the checkbox without label still has some space to the right in wxGTK,
1250 // so shift it to the right
1252 #elif defined(__WXMSW__)
1253 // here too, but in other way
1258 int hAlign
= wxALIGN_CENTRE
;
1259 int vAlign
= wxALIGN_CENTRE
;
1261 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1264 if (hAlign
== wxALIGN_LEFT
)
1270 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1272 else if (hAlign
== wxALIGN_RIGHT
)
1274 x
= r
.x
+ r
.width
- size
.x
- 2;
1275 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1277 else if (hAlign
== wxALIGN_CENTRE
)
1279 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1280 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1283 m_control
->Move(x
, y
);
1286 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1288 m_control
->Show(show
);
1292 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1293 CBox()->SetBackgroundColour(colBg
);
1297 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1299 wxASSERT_MSG(m_control
,
1300 wxT("The wxGridCellEditor must be Created first!"));
1302 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1303 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1306 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1307 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1309 CBox()->SetValue(m_startValue
);
1313 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1316 wxASSERT_MSG(m_control
,
1317 wxT("The wxGridCellEditor must be Created first!"));
1319 bool changed
= false;
1320 bool value
= CBox()->GetValue();
1321 if ( value
!= m_startValue
)
1326 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1327 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1329 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1335 void wxGridCellBoolEditor::Reset()
1337 wxASSERT_MSG(m_control
,
1338 wxT("The wxGridCellEditor must be Created first!"));
1340 CBox()->SetValue(m_startValue
);
1343 void wxGridCellBoolEditor::StartingClick()
1345 CBox()->SetValue(!CBox()->GetValue());
1348 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1350 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1352 int keycode
= event
.GetKeyCode();
1356 case WXK_NUMPAD_MULTIPLY
:
1358 case WXK_NUMPAD_ADD
:
1360 case WXK_NUMPAD_SUBTRACT
:
1371 // return the value as "1" for true and the empty string for false
1372 wxString
wxGridCellBoolEditor::GetValue() const
1374 bool bSet
= CBox()->GetValue();
1375 return bSet
? _T("1") : wxEmptyString
;
1378 #endif // wxUSE_CHECKBOX
1382 // ----------------------------------------------------------------------------
1383 // wxGridCellChoiceEditor
1384 // ----------------------------------------------------------------------------
1386 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1388 : m_choices(choices
),
1389 m_allowOthers(allowOthers
) { }
1391 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1392 const wxString choices
[],
1394 : m_allowOthers(allowOthers
)
1398 m_choices
.Alloc(count
);
1399 for ( size_t n
= 0; n
< count
; n
++ )
1401 m_choices
.Add(choices
[n
]);
1406 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1408 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1409 editor
->m_allowOthers
= m_allowOthers
;
1410 editor
->m_choices
= m_choices
;
1415 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1417 wxEvtHandler
* evtHandler
)
1419 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1420 wxDefaultPosition
, wxDefaultSize
,
1422 m_allowOthers
? 0 : wxCB_READONLY
);
1424 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1427 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1428 wxGridCellAttr
* attr
)
1430 // as we fill the entire client area, don't do anything here to minimize
1433 // TODO: It doesn't actually fill the client area since the height of a
1434 // combo always defaults to the standard... Until someone has time to
1435 // figure out the right rectangle to paint, just do it the normal way...
1436 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1439 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1441 wxASSERT_MSG(m_control
,
1442 wxT("The wxGridCellEditor must be Created first!"));
1444 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1447 Combo()->SetValue(m_startValue
);
1450 // find the right position, or default to the first if not found
1451 int pos
= Combo()->FindString(m_startValue
);
1454 Combo()->SetSelection(pos
);
1456 Combo()->SetInsertionPointEnd();
1457 Combo()->SetFocus();
1460 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1463 wxString value
= Combo()->GetValue();
1464 if ( value
== m_startValue
)
1467 grid
->GetTable()->SetValue(row
, col
, value
);
1472 void wxGridCellChoiceEditor::Reset()
1474 Combo()->SetValue(m_startValue
);
1475 Combo()->SetInsertionPointEnd();
1478 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1488 wxStringTokenizer
tk(params
, _T(','));
1489 while ( tk
.HasMoreTokens() )
1491 m_choices
.Add(tk
.GetNextToken());
1495 // return the value in the text control
1496 wxString
wxGridCellChoiceEditor::GetValue() const
1498 return Combo()->GetValue();
1501 #endif // wxUSE_COMBOBOX
1503 // ----------------------------------------------------------------------------
1504 // wxGridCellEditorEvtHandler
1505 // ----------------------------------------------------------------------------
1507 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1509 switch ( event
.GetKeyCode() )
1513 m_grid
->DisableCellEditControl();
1517 m_grid
->GetEventHandler()->ProcessEvent( event
);
1521 case WXK_NUMPAD_ENTER
:
1522 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1523 m_editor
->HandleReturn(event
);
1532 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1534 switch ( event
.GetKeyCode() )
1539 case WXK_NUMPAD_ENTER
:
1547 // ----------------------------------------------------------------------------
1548 // wxGridCellWorker is an (almost) empty common base class for
1549 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1550 // ----------------------------------------------------------------------------
1552 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1557 wxGridCellWorker::~wxGridCellWorker()
1561 // ============================================================================
1563 // ============================================================================
1565 // ----------------------------------------------------------------------------
1566 // wxGridCellRenderer
1567 // ----------------------------------------------------------------------------
1569 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1570 wxGridCellAttr
& attr
,
1573 int WXUNUSED(row
), int WXUNUSED(col
),
1576 dc
.SetBackgroundMode( wxSOLID
);
1578 // grey out fields if the grid is disabled
1579 if( grid
.IsEnabled() )
1583 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1587 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1592 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1595 dc
.SetPen( *wxTRANSPARENT_PEN
);
1596 dc
.DrawRectangle(rect
);
1599 // ----------------------------------------------------------------------------
1600 // wxGridCellStringRenderer
1601 // ----------------------------------------------------------------------------
1603 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1604 wxGridCellAttr
& attr
,
1608 dc
.SetBackgroundMode( wxTRANSPARENT
);
1610 // TODO some special colours for attr.IsReadOnly() case?
1612 // different coloured text when the grid is disabled
1613 if( grid
.IsEnabled() )
1617 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1618 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1622 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1623 dc
.SetTextForeground( attr
.GetTextColour() );
1628 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1629 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1632 dc
.SetFont( attr
.GetFont() );
1635 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1637 const wxString
& text
)
1639 wxCoord x
= 0, y
= 0, max_x
= 0;
1640 dc
.SetFont(attr
.GetFont());
1641 wxStringTokenizer
tk(text
, _T('\n'));
1642 while ( tk
.HasMoreTokens() )
1644 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1645 max_x
= wxMax(max_x
, x
);
1648 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1650 return wxSize(max_x
, y
);
1653 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1654 wxGridCellAttr
& attr
,
1658 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1661 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1662 wxGridCellAttr
& attr
,
1664 const wxRect
& rectCell
,
1668 wxRect rect
= rectCell
;
1671 // erase only this cells background, overflow cells should have been erased
1672 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1675 attr
.GetAlignment(&hAlign
, &vAlign
);
1677 int overflowCols
= 0;
1679 if (attr
.GetOverflow())
1681 int cols
= grid
.GetNumberCols();
1682 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1683 int cell_rows
, cell_cols
;
1684 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1685 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1687 int i
, c_cols
, c_rows
;
1688 for (i
= col
+cell_cols
; i
< cols
; i
++)
1690 bool is_empty
= true;
1691 for (int j
=row
; j
<row
+cell_rows
; j
++)
1693 // check w/ anchor cell for multicell block
1694 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1695 if (c_rows
> 0) c_rows
= 0;
1696 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1703 rect
.width
+= grid
.GetColSize(i
);
1709 if (rect
.width
>= best_width
) break;
1711 overflowCols
= i
- col
- cell_cols
+ 1;
1712 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1715 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1717 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1719 clip
.x
+= rectCell
.width
;
1720 // draw each overflow cell individually
1721 int col_end
= col
+cell_cols
+overflowCols
;
1722 if (col_end
>= grid
.GetNumberCols())
1723 col_end
= grid
.GetNumberCols() - 1;
1724 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1726 clip
.width
= grid
.GetColSize(i
) - 1;
1727 dc
.DestroyClippingRegion();
1728 dc
.SetClippingRegion(clip
);
1730 SetTextColoursAndFont(grid
, attr
, dc
,
1731 grid
.IsInSelection(row
,i
));
1733 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1734 rect
, hAlign
, vAlign
);
1735 clip
.x
+= grid
.GetColSize(i
) - 1;
1741 dc
.DestroyClippingRegion();
1745 // now we only have to draw the text
1746 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1748 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1749 rect
, hAlign
, vAlign
);
1752 // ----------------------------------------------------------------------------
1753 // wxGridCellNumberRenderer
1754 // ----------------------------------------------------------------------------
1756 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1758 wxGridTableBase
*table
= grid
.GetTable();
1760 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1762 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1766 text
= table
->GetValue(row
, col
);
1772 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1773 wxGridCellAttr
& attr
,
1775 const wxRect
& rectCell
,
1779 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1781 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1783 // draw the text right aligned by default
1785 attr
.GetAlignment(&hAlign
, &vAlign
);
1786 hAlign
= wxALIGN_RIGHT
;
1788 wxRect rect
= rectCell
;
1791 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1794 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1795 wxGridCellAttr
& attr
,
1799 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1802 // ----------------------------------------------------------------------------
1803 // wxGridCellFloatRenderer
1804 // ----------------------------------------------------------------------------
1806 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1809 SetPrecision(precision
);
1812 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1814 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1815 renderer
->m_width
= m_width
;
1816 renderer
->m_precision
= m_precision
;
1817 renderer
->m_format
= m_format
;
1822 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1824 wxGridTableBase
*table
= grid
.GetTable();
1829 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1831 val
= table
->GetValueAsDouble(row
, col
);
1836 text
= table
->GetValue(row
, col
);
1837 hasDouble
= text
.ToDouble(&val
);
1844 if ( m_width
== -1 )
1846 if ( m_precision
== -1 )
1848 // default width/precision
1849 m_format
= _T("%f");
1853 m_format
.Printf(_T("%%.%df"), m_precision
);
1856 else if ( m_precision
== -1 )
1858 // default precision
1859 m_format
.Printf(_T("%%%d.f"), m_width
);
1863 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1867 text
.Printf(m_format
, val
);
1870 //else: text already contains the string
1875 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1876 wxGridCellAttr
& attr
,
1878 const wxRect
& rectCell
,
1882 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1884 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1886 // draw the text right aligned by default
1888 attr
.GetAlignment(&hAlign
, &vAlign
);
1889 hAlign
= wxALIGN_RIGHT
;
1891 wxRect rect
= rectCell
;
1894 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1897 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1898 wxGridCellAttr
& attr
,
1902 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1905 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1909 // reset to defaults
1915 wxString tmp
= params
.BeforeFirst(_T(','));
1919 if ( tmp
.ToLong(&width
) )
1921 SetWidth((int)width
);
1925 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1929 tmp
= params
.AfterFirst(_T(','));
1933 if ( tmp
.ToLong(&precision
) )
1935 SetPrecision((int)precision
);
1939 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1947 // ----------------------------------------------------------------------------
1948 // wxGridCellBoolRenderer
1949 // ----------------------------------------------------------------------------
1951 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1953 // FIXME these checkbox size calculations are really ugly...
1955 // between checkmark and box
1956 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1958 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1959 wxGridCellAttr
& WXUNUSED(attr
),
1964 // compute it only once (no locks for MT safeness in GUI thread...)
1965 if ( !ms_sizeCheckMark
.x
)
1967 // get checkbox size
1968 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1969 wxSize size
= checkbox
->GetBestSize();
1970 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1972 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1973 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1974 checkSize
-= size
.y
/ 2;
1979 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1982 return ms_sizeCheckMark
;
1985 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1986 wxGridCellAttr
& attr
,
1992 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1994 // draw a check mark in the centre (ignoring alignment - TODO)
1995 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1997 // don't draw outside the cell
1998 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1999 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2001 // and even leave (at least) 1 pixel margin
2002 size
.x
= size
.y
= minSize
- 2;
2005 // draw a border around checkmark
2007 attr
.GetAlignment(& hAlign
, &vAlign
);
2010 if (hAlign
== wxALIGN_CENTRE
)
2012 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2013 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2014 rectBorder
.width
= size
.x
;
2015 rectBorder
.height
= size
.y
;
2017 else if (hAlign
== wxALIGN_LEFT
)
2019 rectBorder
.x
= rect
.x
+ 2;
2020 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2021 rectBorder
.width
= size
.x
;
2022 rectBorder
.height
= size
.y
;
2024 else if (hAlign
== wxALIGN_RIGHT
)
2026 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2027 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2028 rectBorder
.width
= size
.x
;
2029 rectBorder
.height
= size
.y
;
2033 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2034 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2037 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2038 value
= !( !cellval
|| (cellval
== wxT("0")) );
2043 wxRect rectMark
= rectBorder
;
2045 // MSW DrawCheckMark() is weird (and should probably be changed...)
2046 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2050 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2053 dc
.SetTextForeground(attr
.GetTextColour());
2054 dc
.DrawCheckMark(rectMark
);
2057 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2058 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2059 dc
.DrawRectangle(rectBorder
);
2062 // ----------------------------------------------------------------------------
2064 // ----------------------------------------------------------------------------
2066 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2070 m_isReadOnly
= Unset
;
2075 m_attrkind
= wxGridCellAttr::Cell
;
2077 m_sizeRows
= m_sizeCols
= 1;
2078 m_overflow
= UnsetOverflow
;
2080 SetDefAttr(attrDefault
);
2083 wxGridCellAttr
*wxGridCellAttr::Clone() const
2085 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2087 if ( HasTextColour() )
2088 attr
->SetTextColour(GetTextColour());
2089 if ( HasBackgroundColour() )
2090 attr
->SetBackgroundColour(GetBackgroundColour());
2092 attr
->SetFont(GetFont());
2093 if ( HasAlignment() )
2094 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2096 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2100 attr
->SetRenderer(m_renderer
);
2101 m_renderer
->IncRef();
2105 attr
->SetEditor(m_editor
);
2110 attr
->SetReadOnly();
2112 attr
->SetKind( m_attrkind
);
2117 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2119 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2120 SetTextColour(mergefrom
->GetTextColour());
2121 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2122 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2123 if ( !HasFont() && mergefrom
->HasFont() )
2124 SetFont(mergefrom
->GetFont());
2125 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2127 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2128 SetAlignment(hAlign
, vAlign
);
2131 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2133 // Directly access member functions as GetRender/Editor don't just return
2134 // m_renderer/m_editor
2136 // Maybe add support for merge of Render and Editor?
2137 if (!HasRenderer() && mergefrom
->HasRenderer() )
2139 m_renderer
= mergefrom
->m_renderer
;
2140 m_renderer
->IncRef();
2142 if ( !HasEditor() && mergefrom
->HasEditor() )
2144 m_editor
= mergefrom
->m_editor
;
2147 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2148 SetReadOnly(mergefrom
->IsReadOnly());
2150 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2151 SetOverflow(mergefrom
->GetOverflow());
2153 SetDefAttr(mergefrom
->m_defGridAttr
);
2156 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2158 // The size of a cell is normally 1,1
2160 // If this cell is larger (2,2) then this is the top left cell
2161 // the other cells that will be covered (lower right cells) must be
2162 // set to negative or zero values such that
2163 // row + num_rows of the covered cell points to the larger cell (this cell)
2164 // same goes for the col + num_cols.
2166 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2168 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2169 !((num_rows
<=0)&&(num_cols
>0)) ||
2170 !((num_rows
==0)&&(num_cols
==0))),
2171 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2173 m_sizeRows
= num_rows
;
2174 m_sizeCols
= num_cols
;
2177 const wxColour
& wxGridCellAttr::GetTextColour() const
2179 if (HasTextColour())
2183 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2185 return m_defGridAttr
->GetTextColour();
2189 wxFAIL_MSG(wxT("Missing default cell attribute"));
2190 return wxNullColour
;
2195 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2197 if (HasBackgroundColour())
2199 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2200 return m_defGridAttr
->GetBackgroundColour();
2203 wxFAIL_MSG(wxT("Missing default cell attribute"));
2204 return wxNullColour
;
2209 const wxFont
& wxGridCellAttr::GetFont() const
2213 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2214 return m_defGridAttr
->GetFont();
2217 wxFAIL_MSG(wxT("Missing default cell attribute"));
2223 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2227 if ( hAlign
) *hAlign
= m_hAlign
;
2228 if ( vAlign
) *vAlign
= m_vAlign
;
2230 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2231 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2234 wxFAIL_MSG(wxT("Missing default cell attribute"));
2238 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2240 if ( num_rows
) *num_rows
= m_sizeRows
;
2241 if ( num_cols
) *num_cols
= m_sizeCols
;
2244 // GetRenderer and GetEditor use a slightly different decision path about
2245 // which attribute to use. If a non-default attr object has one then it is
2246 // used, otherwise the default editor or renderer is fetched from the grid and
2247 // used. It should be the default for the data type of the cell. If it is
2248 // NULL (because the table has a type that the grid does not have in its
2249 // registry,) then the grid's default editor or renderer is used.
2251 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2253 wxGridCellRenderer
*renderer
;
2255 if ( m_renderer
&& this != m_defGridAttr
)
2257 // use the cells renderer if it has one
2258 renderer
= m_renderer
;
2261 else // no non default cell renderer
2263 // get default renderer for the data type
2266 // GetDefaultRendererForCell() will do IncRef() for us
2267 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2276 if (m_defGridAttr
&& this != m_defGridAttr
)
2278 // if we still don't have one then use the grid default
2279 // (no need for IncRef() here neither)
2280 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2282 else // default grid attr
2284 // use m_renderer which we had decided not to use initially
2285 renderer
= m_renderer
;
2292 // we're supposed to always find something
2293 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2298 // same as above, except for s/renderer/editor/g
2299 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2301 wxGridCellEditor
*editor
;
2303 if ( m_editor
&& this != m_defGridAttr
)
2305 // use the cells editor if it has one
2309 else // no non default cell editor
2311 // get default editor for the data type
2314 // GetDefaultEditorForCell() will do IncRef() for us
2315 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2324 if ( m_defGridAttr
&& this != m_defGridAttr
)
2326 // if we still don't have one then use the grid default
2327 // (no need for IncRef() here neither)
2328 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2330 else // default grid attr
2332 // use m_editor which we had decided not to use initially
2340 // we're supposed to always find something
2341 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2346 // ----------------------------------------------------------------------------
2347 // wxGridCellAttrData
2348 // ----------------------------------------------------------------------------
2350 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2352 int n
= FindIndex(row
, col
);
2353 if ( n
== wxNOT_FOUND
)
2355 // add the attribute
2356 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2360 // free the old attribute
2361 m_attrs
[(size_t)n
].attr
->DecRef();
2365 // change the attribute
2366 m_attrs
[(size_t)n
].attr
= attr
;
2370 // remove this attribute
2371 m_attrs
.RemoveAt((size_t)n
);
2376 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2378 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2380 int n
= FindIndex(row
, col
);
2381 if ( n
!= wxNOT_FOUND
)
2383 attr
= m_attrs
[(size_t)n
].attr
;
2390 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2392 size_t count
= m_attrs
.GetCount();
2393 for ( size_t n
= 0; n
< count
; n
++ )
2395 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2396 wxCoord row
= coords
.GetRow();
2397 if ((size_t)row
>= pos
)
2401 // If rows inserted, include row counter where necessary
2402 coords
.SetRow(row
+ numRows
);
2404 else if (numRows
< 0)
2406 // If rows deleted ...
2407 if ((size_t)row
>= pos
- numRows
)
2409 // ...either decrement row counter (if row still exists)...
2410 coords
.SetRow(row
+ numRows
);
2414 // ...or remove the attribute
2415 m_attrs
.RemoveAt((size_t)n
);
2423 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2425 size_t count
= m_attrs
.GetCount();
2426 for ( size_t n
= 0; n
< count
; n
++ )
2428 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2429 wxCoord col
= coords
.GetCol();
2430 if ( (size_t)col
>= pos
)
2434 // If rows inserted, include row counter where necessary
2435 coords
.SetCol(col
+ numCols
);
2437 else if (numCols
< 0)
2439 // If rows deleted ...
2440 if ((size_t)col
>= pos
- numCols
)
2442 // ...either decrement row counter (if row still exists)...
2443 coords
.SetCol(col
+ numCols
);
2447 // ...or remove the attribute
2448 m_attrs
.RemoveAt((size_t)n
);
2456 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2458 size_t count
= m_attrs
.GetCount();
2459 for ( size_t n
= 0; n
< count
; n
++ )
2461 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2462 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2471 // ----------------------------------------------------------------------------
2472 // wxGridRowOrColAttrData
2473 // ----------------------------------------------------------------------------
2475 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2477 size_t count
= m_attrs
.Count();
2478 for ( size_t n
= 0; n
< count
; n
++ )
2480 m_attrs
[n
]->DecRef();
2484 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2486 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2488 int n
= m_rowsOrCols
.Index(rowOrCol
);
2489 if ( n
!= wxNOT_FOUND
)
2491 attr
= m_attrs
[(size_t)n
];
2498 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2500 int i
= m_rowsOrCols
.Index(rowOrCol
);
2501 if ( i
== wxNOT_FOUND
)
2503 // add the attribute
2504 m_rowsOrCols
.Add(rowOrCol
);
2509 size_t n
= (size_t)i
;
2512 // change the attribute
2513 m_attrs
[n
]->DecRef();
2518 // remove this attribute
2519 m_attrs
[n
]->DecRef();
2520 m_rowsOrCols
.RemoveAt(n
);
2521 m_attrs
.RemoveAt(n
);
2526 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2528 size_t count
= m_attrs
.GetCount();
2529 for ( size_t n
= 0; n
< count
; n
++ )
2531 int & rowOrCol
= m_rowsOrCols
[n
];
2532 if ( (size_t)rowOrCol
>= pos
)
2534 if ( numRowsOrCols
> 0 )
2536 // If rows inserted, include row counter where necessary
2537 rowOrCol
+= numRowsOrCols
;
2539 else if ( numRowsOrCols
< 0)
2541 // If rows deleted, either decrement row counter (if row still exists)
2542 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2543 rowOrCol
+= numRowsOrCols
;
2546 m_rowsOrCols
.RemoveAt((size_t)n
);
2547 m_attrs
.RemoveAt((size_t)n
);
2555 // ----------------------------------------------------------------------------
2556 // wxGridCellAttrProvider
2557 // ----------------------------------------------------------------------------
2559 wxGridCellAttrProvider::wxGridCellAttrProvider()
2561 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2564 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2569 void wxGridCellAttrProvider::InitData()
2571 m_data
= new wxGridCellAttrProviderData
;
2574 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2575 wxGridCellAttr::wxAttrKind kind
) const
2577 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2582 case (wxGridCellAttr::Any
):
2583 //Get cached merge attributes.
2584 // Currenlty not used as no cache implemented as not mutiable
2585 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2588 //Basicaly implement old version.
2589 //Also check merge cache, so we don't have to re-merge every time..
2590 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2591 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2592 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2594 if((attrcell
!= attrrow
) && (attrrow
!=attrcol
) && (attrcell
!= attrcol
)){
2595 // Two or move are non NULL
2596 attr
= new wxGridCellAttr
;
2597 attr
->SetKind(wxGridCellAttr::Merged
);
2601 attr
->MergeWith(attrcell
);
2605 attr
->MergeWith(attrcol
);
2609 attr
->MergeWith(attrrow
);
2612 //store merge attr if cache implemented
2614 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2618 // one or none is non null return it or null.
2619 if(attrrow
) attr
= attrrow
;
2620 if(attrcol
) attr
= attrcol
;
2621 if(attrcell
) attr
= attrcell
;
2625 case (wxGridCellAttr::Cell
):
2626 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2628 case (wxGridCellAttr::Col
):
2629 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2631 case (wxGridCellAttr::Row
):
2632 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2636 // (wxGridCellAttr::Default):
2637 // (wxGridCellAttr::Merged):
2644 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2650 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2653 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2658 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2661 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2666 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2669 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2673 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2675 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2679 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2683 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2685 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2689 // ----------------------------------------------------------------------------
2690 // wxGridTypeRegistry
2691 // ----------------------------------------------------------------------------
2693 wxGridTypeRegistry::~wxGridTypeRegistry()
2695 size_t count
= m_typeinfo
.Count();
2696 for ( size_t i
= 0; i
< count
; i
++ )
2697 delete m_typeinfo
[i
];
2701 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2702 wxGridCellRenderer
* renderer
,
2703 wxGridCellEditor
* editor
)
2705 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2707 // is it already registered?
2708 int loc
= FindRegisteredDataType(typeName
);
2709 if ( loc
!= wxNOT_FOUND
)
2711 delete m_typeinfo
[loc
];
2712 m_typeinfo
[loc
] = info
;
2716 m_typeinfo
.Add(info
);
2720 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2722 size_t count
= m_typeinfo
.GetCount();
2723 for ( size_t i
= 0; i
< count
; i
++ )
2725 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2734 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2736 int index
= FindRegisteredDataType(typeName
);
2737 if ( index
== wxNOT_FOUND
)
2739 // check whether this is one of the standard ones, in which case
2740 // register it "on the fly"
2742 if ( typeName
== wxGRID_VALUE_STRING
)
2744 RegisterDataType(wxGRID_VALUE_STRING
,
2745 new wxGridCellStringRenderer
,
2746 new wxGridCellTextEditor
);
2748 #endif // wxUSE_TEXTCTRL
2750 if ( typeName
== wxGRID_VALUE_BOOL
)
2752 RegisterDataType(wxGRID_VALUE_BOOL
,
2753 new wxGridCellBoolRenderer
,
2754 new wxGridCellBoolEditor
);
2756 #endif // wxUSE_CHECKBOX
2758 if ( typeName
== wxGRID_VALUE_NUMBER
)
2760 RegisterDataType(wxGRID_VALUE_NUMBER
,
2761 new wxGridCellNumberRenderer
,
2762 new wxGridCellNumberEditor
);
2764 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2766 RegisterDataType(wxGRID_VALUE_FLOAT
,
2767 new wxGridCellFloatRenderer
,
2768 new wxGridCellFloatEditor
);
2770 #endif // wxUSE_TEXTCTRL
2772 if ( typeName
== wxGRID_VALUE_CHOICE
)
2774 RegisterDataType(wxGRID_VALUE_CHOICE
,
2775 new wxGridCellStringRenderer
,
2776 new wxGridCellChoiceEditor
);
2778 #endif // wxUSE_COMBOBOX
2783 // we get here only if just added the entry for this type, so return
2785 index
= m_typeinfo
.GetCount() - 1;
2791 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2793 int index
= FindDataType(typeName
);
2794 if ( index
== wxNOT_FOUND
)
2796 // the first part of the typename is the "real" type, anything after ':'
2797 // are the parameters for the renderer
2798 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2799 if ( index
== wxNOT_FOUND
)
2804 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2805 wxGridCellRenderer
*rendererOld
= renderer
;
2806 renderer
= renderer
->Clone();
2807 rendererOld
->DecRef();
2809 wxGridCellEditor
*editor
= GetEditor(index
);
2810 wxGridCellEditor
*editorOld
= editor
;
2811 editor
= editor
->Clone();
2812 editorOld
->DecRef();
2814 // do it even if there are no parameters to reset them to defaults
2815 wxString params
= typeName
.AfterFirst(_T(':'));
2816 renderer
->SetParameters(params
);
2817 editor
->SetParameters(params
);
2819 // register the new typename
2820 RegisterDataType(typeName
, renderer
, editor
);
2822 // we just registered it, it's the last one
2823 index
= m_typeinfo
.GetCount() - 1;
2829 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2831 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2837 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2839 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2845 // ----------------------------------------------------------------------------
2847 // ----------------------------------------------------------------------------
2849 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2852 wxGridTableBase::wxGridTableBase()
2854 m_view
= (wxGrid
*) NULL
;
2855 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2858 wxGridTableBase::~wxGridTableBase()
2860 delete m_attrProvider
;
2863 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2865 delete m_attrProvider
;
2866 m_attrProvider
= attrProvider
;
2869 bool wxGridTableBase::CanHaveAttributes()
2871 if ( ! GetAttrProvider() )
2873 // use the default attr provider by default
2874 SetAttrProvider(new wxGridCellAttrProvider
);
2879 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2881 if ( m_attrProvider
)
2882 return m_attrProvider
->GetAttr(row
, col
, kind
);
2884 return (wxGridCellAttr
*)NULL
;
2887 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2889 if ( m_attrProvider
)
2891 attr
->SetKind(wxGridCellAttr::Cell
);
2892 m_attrProvider
->SetAttr(attr
, row
, col
);
2896 // as we take ownership of the pointer and don't store it, we must
2902 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2904 if ( m_attrProvider
)
2906 attr
->SetKind(wxGridCellAttr::Row
);
2907 m_attrProvider
->SetRowAttr(attr
, row
);
2911 // as we take ownership of the pointer and don't store it, we must
2917 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2919 if ( m_attrProvider
)
2921 attr
->SetKind(wxGridCellAttr::Col
);
2922 m_attrProvider
->SetColAttr(attr
, col
);
2926 // as we take ownership of the pointer and don't store it, we must
2932 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2933 size_t WXUNUSED(numRows
) )
2935 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2940 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2942 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2947 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2948 size_t WXUNUSED(numRows
) )
2950 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2955 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2956 size_t WXUNUSED(numCols
) )
2958 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2963 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2965 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2970 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2971 size_t WXUNUSED(numCols
) )
2973 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2979 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2982 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2983 // how much it makes sense to us geeks.
2987 wxString
wxGridTableBase::GetColLabelValue( int col
)
2989 // default col labels are:
2990 // cols 0 to 25 : A-Z
2991 // cols 26 to 675 : AA-ZZ
2996 for ( n
= 1; ; n
++ )
2998 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3000 if ( col
< 0 ) break;
3003 // reverse the string...
3005 for ( i
= 0; i
< n
; i
++ )
3014 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3016 return wxGRID_VALUE_STRING
;
3019 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3020 const wxString
& typeName
)
3022 return typeName
== wxGRID_VALUE_STRING
;
3025 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3027 return CanGetValueAs(row
, col
, typeName
);
3030 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3035 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3040 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3045 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3046 long WXUNUSED(value
) )
3050 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3051 double WXUNUSED(value
) )
3055 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3056 bool WXUNUSED(value
) )
3061 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3062 const wxString
& WXUNUSED(typeName
) )
3067 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3068 const wxString
& WXUNUSED(typeName
),
3069 void* WXUNUSED(value
) )
3073 //////////////////////////////////////////////////////////////////////
3075 // Message class for the grid table to send requests and notifications
3079 wxGridTableMessage::wxGridTableMessage()
3081 m_table
= (wxGridTableBase
*) NULL
;
3087 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3088 int commandInt1
, int commandInt2
)
3092 m_comInt1
= commandInt1
;
3093 m_comInt2
= commandInt2
;
3098 //////////////////////////////////////////////////////////////////////
3100 // A basic grid table for string data. An object of this class will
3101 // created by wxGrid if you don't specify an alternative table class.
3104 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3106 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3108 wxGridStringTable::wxGridStringTable()
3113 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3116 m_data
.Alloc( numRows
);
3119 sa
.Alloc( numCols
);
3120 sa
.Add( wxEmptyString
, numCols
);
3122 m_data
.Add( sa
, numRows
);
3125 wxGridStringTable::~wxGridStringTable()
3129 int wxGridStringTable::GetNumberRows()
3131 return m_data
.GetCount();
3134 int wxGridStringTable::GetNumberCols()
3136 if ( m_data
.GetCount() > 0 )
3137 return m_data
[0].GetCount();
3142 wxString
wxGridStringTable::GetValue( int row
, int col
)
3144 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3146 _T("invalid row or column index in wxGridStringTable") );
3148 return m_data
[row
][col
];
3151 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3153 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3154 _T("invalid row or column index in wxGridStringTable") );
3156 m_data
[row
][col
] = value
;
3159 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3161 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3163 _T("invalid row or column index in wxGridStringTable") );
3165 return (m_data
[row
][col
] == wxEmptyString
);
3168 void wxGridStringTable::Clear()
3171 int numRows
, numCols
;
3173 numRows
= m_data
.GetCount();
3176 numCols
= m_data
[0].GetCount();
3178 for ( row
= 0; row
< numRows
; row
++ )
3180 for ( col
= 0; col
< numCols
; col
++ )
3182 m_data
[row
][col
] = wxEmptyString
;
3189 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3191 size_t curNumRows
= m_data
.GetCount();
3192 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3193 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3195 if ( pos
>= curNumRows
)
3197 return AppendRows( numRows
);
3201 sa
.Alloc( curNumCols
);
3202 sa
.Add( wxEmptyString
, curNumCols
);
3203 m_data
.Insert( sa
, pos
, numRows
);
3206 wxGridTableMessage
msg( this,
3207 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3211 GetView()->ProcessTableMessage( msg
);
3217 bool wxGridStringTable::AppendRows( size_t numRows
)
3219 size_t curNumRows
= m_data
.GetCount();
3220 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3221 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3224 if ( curNumCols
> 0 )
3226 sa
.Alloc( curNumCols
);
3227 sa
.Add( wxEmptyString
, curNumCols
);
3230 m_data
.Add( sa
, numRows
);
3234 wxGridTableMessage
msg( this,
3235 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3238 GetView()->ProcessTableMessage( msg
);
3244 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3246 size_t curNumRows
= m_data
.GetCount();
3248 if ( pos
>= curNumRows
)
3250 wxFAIL_MSG( wxString::Format
3252 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3254 (unsigned long)numRows
,
3255 (unsigned long)curNumRows
3261 if ( numRows
> curNumRows
- pos
)
3263 numRows
= curNumRows
- pos
;
3266 if ( numRows
>= curNumRows
)
3272 m_data
.RemoveAt( pos
, numRows
);
3276 wxGridTableMessage
msg( this,
3277 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3281 GetView()->ProcessTableMessage( msg
);
3287 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3291 size_t curNumRows
= m_data
.GetCount();
3292 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3293 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3295 if ( pos
>= curNumCols
)
3297 return AppendCols( numCols
);
3300 for ( row
= 0; row
< curNumRows
; row
++ )
3302 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3304 m_data
[row
].Insert( wxEmptyString
, col
);
3309 wxGridTableMessage
msg( this,
3310 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3314 GetView()->ProcessTableMessage( msg
);
3320 bool wxGridStringTable::AppendCols( size_t numCols
)
3324 size_t curNumRows
= m_data
.GetCount();
3328 // TODO: something better than this ?
3330 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3335 for ( row
= 0; row
< curNumRows
; row
++ )
3337 m_data
[row
].Add( wxEmptyString
, numCols
);
3342 wxGridTableMessage
msg( this,
3343 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3346 GetView()->ProcessTableMessage( msg
);
3352 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3356 size_t curNumRows
= m_data
.GetCount();
3357 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3358 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3360 if ( pos
>= curNumCols
)
3362 wxFAIL_MSG( wxString::Format
3364 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3366 (unsigned long)numCols
,
3367 (unsigned long)curNumCols
3372 if ( numCols
> curNumCols
- pos
)
3374 numCols
= curNumCols
- pos
;
3377 for ( row
= 0; row
< curNumRows
; row
++ )
3379 if ( numCols
>= curNumCols
)
3381 m_data
[row
].Clear();
3385 m_data
[row
].RemoveAt( pos
, numCols
);
3390 wxGridTableMessage
msg( this,
3391 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3395 GetView()->ProcessTableMessage( msg
);
3401 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3403 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3405 // using default label
3407 return wxGridTableBase::GetRowLabelValue( row
);
3411 return m_rowLabels
[ row
];
3415 wxString
wxGridStringTable::GetColLabelValue( int col
)
3417 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3419 // using default label
3421 return wxGridTableBase::GetColLabelValue( col
);
3425 return m_colLabels
[ col
];
3429 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3431 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3433 int n
= m_rowLabels
.GetCount();
3435 for ( i
= n
; i
<= row
; i
++ )
3437 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3441 m_rowLabels
[row
] = value
;
3444 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3446 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3448 int n
= m_colLabels
.GetCount();
3450 for ( i
= n
; i
<= col
; i
++ )
3452 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3456 m_colLabels
[col
] = value
;
3461 //////////////////////////////////////////////////////////////////////
3462 //////////////////////////////////////////////////////////////////////
3464 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3466 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3467 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3468 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3469 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3470 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3471 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3474 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3476 const wxPoint
&pos
, const wxSize
&size
)
3477 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3482 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3486 // NO - don't do this because it will set both the x and y origin
3487 // coords to match the parent scrolled window and we just want to
3488 // set the y coord - MB
3490 // m_owner->PrepareDC( dc );
3493 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3494 dc
.SetDeviceOrigin( 0, -y
);
3496 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3497 m_owner
->DrawRowLabels( dc
, rows
);
3501 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3503 m_owner
->ProcessRowLabelMouseEvent( event
);
3507 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3509 m_owner
->GetEventHandler()->ProcessEvent(event
);
3513 // This seems to be required for wxMotif otherwise the mouse
3514 // cursor must be in the cell edit control to get key events
3516 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3518 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3521 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3523 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3528 //////////////////////////////////////////////////////////////////////
3530 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3532 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3533 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3534 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3535 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3536 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3537 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3540 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3542 const wxPoint
&pos
, const wxSize
&size
)
3543 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3548 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3552 // NO - don't do this because it will set both the x and y origin
3553 // coords to match the parent scrolled window and we just want to
3554 // set the x coord - MB
3556 // m_owner->PrepareDC( dc );
3559 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3560 dc
.SetDeviceOrigin( -x
, 0 );
3562 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3563 m_owner
->DrawColLabels( dc
, cols
);
3567 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3569 m_owner
->ProcessColLabelMouseEvent( event
);
3572 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3574 m_owner
->GetEventHandler()->ProcessEvent(event
);
3578 // This seems to be required for wxMotif otherwise the mouse
3579 // cursor must be in the cell edit control to get key events
3581 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3583 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3586 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3588 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3593 //////////////////////////////////////////////////////////////////////
3595 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3597 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3598 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3599 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3600 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3601 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3602 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3605 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3607 const wxPoint
&pos
, const wxSize
&size
)
3608 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3613 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3617 int client_height
= 0;
3618 int client_width
= 0;
3619 GetClientSize( &client_width
, &client_height
);
3621 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3622 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3623 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3624 dc
.DrawLine( 0, 0, client_width
, 0 );
3625 dc
.DrawLine( 0, 0, 0, client_height
);
3627 dc
.SetPen( *wxWHITE_PEN
);
3628 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3629 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3633 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3635 m_owner
->ProcessCornerLabelMouseEvent( event
);
3639 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3641 m_owner
->GetEventHandler()->ProcessEvent(event
);
3644 // This seems to be required for wxMotif otherwise the mouse
3645 // cursor must be in the cell edit control to get key events
3647 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3649 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3652 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3654 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3659 //////////////////////////////////////////////////////////////////////
3661 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3663 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3664 EVT_PAINT( wxGridWindow::OnPaint
)
3665 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3666 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3667 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3668 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3669 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3670 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3671 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3674 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3675 wxGridRowLabelWindow
*rowLblWin
,
3676 wxGridColLabelWindow
*colLblWin
,
3679 const wxSize
&size
)
3680 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3681 wxT("grid window") )
3685 m_rowLabelWin
= rowLblWin
;
3686 m_colLabelWin
= colLblWin
;
3690 wxGridWindow::~wxGridWindow()
3695 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3697 wxPaintDC
dc( this );
3698 m_owner
->PrepareDC( dc
);
3699 wxRegion reg
= GetUpdateRegion();
3700 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3701 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3702 #if WXGRID_DRAW_LINES
3703 m_owner
->DrawAllGridLines( dc
, reg
);
3705 m_owner
->DrawGridSpace( dc
);
3706 m_owner
->DrawHighlight( dc
, DirtyCells
);
3710 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3712 wxWindow::ScrollWindow( dx
, dy
, rect
);
3713 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3714 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3718 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3720 m_owner
->ProcessGridCellMouseEvent( event
);
3723 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3725 m_owner
->GetEventHandler()->ProcessEvent(event
);
3728 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3729 // cursor must be in the cell edit control to get key events
3731 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3733 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3736 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3738 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3741 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3745 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3747 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3751 //////////////////////////////////////////////////////////////////////
3753 // Internal Helper function for computing row or column from some
3754 // (unscrolled) coordinate value, using either
3755 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3756 // of m_rowBottoms/m_ColRights to speed up the search!
3758 // Internal helper macros for simpler use of that function
3760 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3761 const wxArrayInt
& BorderArray
, int nMax
,
3764 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3765 m_minAcceptableColWidth, \
3766 m_colRights, m_numCols, true)
3767 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3768 m_minAcceptableRowHeight, \
3769 m_rowBottoms, m_numRows, true)
3770 /////////////////////////////////////////////////////////////////////
3772 #if wxUSE_EXTENDED_RTTI
3773 WX_DEFINE_FLAGS( wxGridStyle
)
3775 wxBEGIN_FLAGS( wxGridStyle
)
3776 // new style border flags, we put them first to
3777 // use them for streaming out
3778 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3779 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3780 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3781 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3782 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3783 wxFLAGS_MEMBER(wxBORDER_NONE
)
3785 // old style border flags
3786 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3787 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3788 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3789 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3790 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3791 wxFLAGS_MEMBER(wxBORDER
)
3793 // standard window styles
3794 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3795 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3796 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3797 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3798 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3799 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3800 wxFLAGS_MEMBER(wxVSCROLL
)
3801 wxFLAGS_MEMBER(wxHSCROLL
)
3803 wxEND_FLAGS( wxGridStyle
)
3805 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3807 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3808 wxHIDE_PROPERTY( Children
)
3809 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3810 wxEND_PROPERTIES_TABLE()
3812 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3813 wxEND_HANDLERS_TABLE()
3815 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3818 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
3821 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3824 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3825 EVT_PAINT( wxGrid::OnPaint
)
3826 EVT_SIZE( wxGrid::OnSize
)
3827 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3828 EVT_KEY_UP( wxGrid::OnKeyUp
)
3829 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3834 // in order to make sure that a size event is not
3835 // trigerred in a unfinished state
3836 m_cornerLabelWin
= NULL
;
3837 m_rowLabelWin
= NULL
;
3838 m_colLabelWin
= NULL
;
3842 wxGrid::wxGrid( wxWindow
*parent
,
3847 const wxString
& name
)
3848 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3849 m_colMinWidths(GRID_HASH_SIZE
),
3850 m_rowMinHeights(GRID_HASH_SIZE
)
3852 // in order to make sure that a size event is not
3853 // trigerred in a unfinished state
3854 m_cornerLabelWin
= NULL
;
3855 m_rowLabelWin
= NULL
;
3856 m_colLabelWin
= NULL
;
3859 SetBestFittingSize(size
);
3863 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3864 const wxPoint
& pos
, const wxSize
& size
,
3865 long style
, const wxString
& name
)
3867 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3868 style
| wxWANTS_CHARS
, name
))
3871 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3872 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3874 SetBestFittingSize(size
);
3883 // Must do this or ~wxScrollHelper will pop the wrong event handler
3884 SetTargetWindow(this);
3886 wxSafeDecRef(m_defaultCellAttr
);
3888 #ifdef DEBUG_ATTR_CACHE
3889 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3890 wxPrintf(_T("wxGrid attribute cache statistics: "
3891 "total: %u, hits: %u (%u%%)\n"),
3892 total
, gs_nAttrCacheHits
,
3893 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3899 delete m_typeRegistry
;
3905 // ----- internal init and update functions
3908 // NOTE: If using the default visual attributes works everywhere then this can
3909 // be removed as well as the #else cases below.
3910 #define _USE_VISATTR 0
3913 #include "wx/listbox.h"
3916 void wxGrid::Create()
3918 m_created
= false; // set to true by CreateGrid
3920 m_table
= (wxGridTableBase
*) NULL
;
3923 m_cellEditCtrlEnabled
= false;
3925 m_defaultCellAttr
= new wxGridCellAttr();
3927 // Set default cell attributes
3928 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3929 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3930 m_defaultCellAttr
->SetFont(GetFont());
3931 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3932 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3933 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3936 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3937 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3939 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3940 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3943 m_defaultCellAttr
->SetTextColour(
3944 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3945 m_defaultCellAttr
->SetBackgroundColour(
3946 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3951 m_currentCellCoords
= wxGridNoCellCoords
;
3953 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3954 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3956 // create the type registry
3957 m_typeRegistry
= new wxGridTypeRegistry
;
3960 // subwindow components that make up the wxGrid
3961 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3966 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3971 m_colLabelWin
= new wxGridColLabelWindow( this,
3976 m_gridWin
= new wxGridWindow( this,
3983 SetTargetWindow( m_gridWin
);
3986 wxColour gfg
= gva
.colFg
;
3987 wxColour gbg
= gva
.colBg
;
3988 wxColour lfg
= lva
.colFg
;
3989 wxColour lbg
= lva
.colBg
;
3991 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3992 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3993 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3994 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
3996 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
3997 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
3998 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
3999 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4000 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4001 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4003 m_gridWin
->SetOwnForegroundColour(gfg
);
4004 m_gridWin
->SetOwnBackgroundColour(gbg
);
4010 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4011 wxGrid::wxGridSelectionModes selmode
)
4013 wxCHECK_MSG( !m_created
,
4015 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4017 m_numRows
= numRows
;
4018 m_numCols
= numCols
;
4020 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4021 m_table
->SetView( this );
4023 m_selection
= new wxGridSelection( this, selmode
);
4032 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4034 wxCHECK_RET( m_created
,
4035 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4037 m_selection
->SetSelectionMode( selmode
);
4040 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4042 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4043 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4045 return m_selection
->GetSelectionMode();
4048 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4049 wxGrid::wxGridSelectionModes selmode
)
4053 // stop all processing
4058 wxGridTableBase
*t
=m_table
;
4071 m_numRows
= table
->GetNumberRows();
4072 m_numCols
= table
->GetNumberCols();
4075 m_table
->SetView( this );
4078 m_selection
= new wxGridSelection( this, selmode
);
4091 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4092 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4094 if ( m_rowLabelWin
)
4096 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4100 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4103 m_labelTextColour
= wxColour( _T("BLACK") );
4106 m_attrCache
.row
= -1;
4107 m_attrCache
.col
= -1;
4108 m_attrCache
.attr
= NULL
;
4110 // TODO: something better than this ?
4112 m_labelFont
= this->GetFont();
4113 m_labelFont
.SetWeight( wxBOLD
);
4115 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4116 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4118 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4119 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4120 m_colLabelTextOrientation
= wxHORIZONTAL
;
4122 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4123 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4125 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4126 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4128 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4129 m_defaultRowHeight
+= 8;
4131 m_defaultRowHeight
+= 4;
4134 m_gridLineColour
= wxColour( 192,192,192 );
4135 m_gridLinesEnabled
= true;
4136 m_cellHighlightColour
= *wxBLACK
;
4137 m_cellHighlightPenWidth
= 2;
4138 m_cellHighlightROPenWidth
= 1;
4140 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4141 m_winCapture
= (wxWindow
*)NULL
;
4142 m_canDragRowSize
= true;
4143 m_canDragColSize
= true;
4144 m_canDragGridSize
= true;
4145 m_canDragCell
= false;
4147 m_dragRowOrCol
= -1;
4148 m_isDragging
= false;
4149 m_startDragPos
= wxDefaultPosition
;
4151 m_waitForSlowClick
= false;
4153 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4154 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4156 m_currentCellCoords
= wxGridNoCellCoords
;
4158 m_selectingTopLeft
= wxGridNoCellCoords
;
4159 m_selectingBottomRight
= wxGridNoCellCoords
;
4160 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4161 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4163 m_editable
= true; // default for whole grid
4165 m_inOnKeyDown
= false;
4172 // ----------------------------------------------------------------------------
4173 // the idea is to call these functions only when necessary because they create
4174 // quite big arrays which eat memory mostly unnecessary - in particular, if
4175 // default widths/heights are used for all rows/columns, we may not use these
4178 // with some extra code, it should be possible to only store the
4179 // widths/heights different from default ones but this will be done later...
4180 // ----------------------------------------------------------------------------
4182 void wxGrid::InitRowHeights()
4184 m_rowHeights
.Empty();
4185 m_rowBottoms
.Empty();
4187 m_rowHeights
.Alloc( m_numRows
);
4188 m_rowBottoms
.Alloc( m_numRows
);
4192 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4194 for ( int i
= 0; i
< m_numRows
; i
++ )
4196 rowBottom
+= m_defaultRowHeight
;
4197 m_rowBottoms
.Add( rowBottom
);
4201 void wxGrid::InitColWidths()
4203 m_colWidths
.Empty();
4204 m_colRights
.Empty();
4206 m_colWidths
.Alloc( m_numCols
);
4207 m_colRights
.Alloc( m_numCols
);
4210 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4212 for ( int i
= 0; i
< m_numCols
; i
++ )
4214 colRight
+= m_defaultColWidth
;
4215 m_colRights
.Add( colRight
);
4219 int wxGrid::GetColWidth(int col
) const
4221 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4224 int wxGrid::GetColLeft(int col
) const
4226 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4227 : m_colRights
[col
] - m_colWidths
[col
];
4230 int wxGrid::GetColRight(int col
) const
4232 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4236 int wxGrid::GetRowHeight(int row
) const
4238 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4241 int wxGrid::GetRowTop(int row
) const
4243 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4244 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4247 int wxGrid::GetRowBottom(int row
) const
4249 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4250 : m_rowBottoms
[row
];
4253 void wxGrid::CalcDimensions()
4256 GetClientSize( &cw
, &ch
);
4258 if ( m_rowLabelWin
->IsShown() )
4259 cw
-= m_rowLabelWidth
;
4260 if ( m_colLabelWin
->IsShown() )
4261 ch
-= m_colLabelHeight
;
4264 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4265 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4267 // take into account editor if shown
4268 if( IsCellEditControlShown() )
4271 int r
= m_currentCellCoords
.GetRow();
4272 int c
= m_currentCellCoords
.GetCol();
4273 int x
= GetColLeft(c
);
4274 int y
= GetRowTop(r
);
4276 // how big is the editor
4277 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4278 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4279 editor
->GetControl()->GetSize(&w2
, &h2
);
4282 if( w2
> w
) w
= w2
;
4283 if( h2
> h
) h
= h2
;
4288 // preserve (more or less) the previous position
4290 GetViewStart( &x
, &y
);
4292 // ensure the position is valid for the new scroll ranges
4294 x
= wxMax( w
- 1, 0 );
4296 y
= wxMax( h
- 1, 0 );
4298 // do set scrollbar parameters
4299 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4300 GetScrollX(w
), GetScrollY(h
), x
, y
,
4301 GetBatchCount() != 0);
4303 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4304 // still must reposition the children
4309 void wxGrid::CalcWindowSizes()
4311 // escape if the window is has not been fully created yet
4313 if ( m_cornerLabelWin
== NULL
)
4317 GetClientSize( &cw
, &ch
);
4319 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4320 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4322 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4323 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4325 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4326 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4328 if ( m_gridWin
&& m_gridWin
->IsShown() )
4329 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4333 // this is called when the grid table sends a message to say that it
4334 // has been redimensioned
4336 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4339 bool result
= false;
4341 // Clear the attribute cache as the attribute might refer to a different
4342 // cell than stored in the cache after adding/removing rows/columns.
4344 // By the same reasoning, the editor should be dismissed if columns are
4345 // added or removed. And for consistency, it should IMHO always be
4346 // removed, not only if the cell "underneath" it actually changes.
4347 // For now, I intentionally do not save the editor's content as the
4348 // cell it might want to save that stuff to might no longer exist.
4349 HideCellEditControl();
4351 // if we were using the default widths/heights so far, we must change them
4353 if ( m_colWidths
.IsEmpty() )
4358 if ( m_rowHeights
.IsEmpty() )
4364 switch ( msg
.GetId() )
4366 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4368 size_t pos
= msg
.GetCommandInt();
4369 int numRows
= msg
.GetCommandInt2();
4371 m_numRows
+= numRows
;
4373 if ( !m_rowHeights
.IsEmpty() )
4375 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4376 m_rowBottoms
.Insert( 0, pos
, numRows
);
4379 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4381 for ( i
= pos
; i
< m_numRows
; i
++ )
4383 bottom
+= m_rowHeights
[i
];
4384 m_rowBottoms
[i
] = bottom
;
4387 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4389 // if we have just inserted cols into an empty grid the current
4390 // cell will be undefined...
4392 SetCurrentCell( 0, 0 );
4396 m_selection
->UpdateRows( pos
, numRows
);
4397 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4399 attrProvider
->UpdateAttrRows( pos
, numRows
);
4401 if ( !GetBatchCount() )
4404 m_rowLabelWin
->Refresh();
4410 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4412 int numRows
= msg
.GetCommandInt();
4413 int oldNumRows
= m_numRows
;
4414 m_numRows
+= numRows
;
4416 if ( !m_rowHeights
.IsEmpty() )
4418 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4419 m_rowBottoms
.Add( 0, numRows
);
4422 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4424 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4426 bottom
+= m_rowHeights
[i
];
4427 m_rowBottoms
[i
] = bottom
;
4430 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4432 // if we have just inserted cols into an empty grid the current
4433 // cell will be undefined...
4435 SetCurrentCell( 0, 0 );
4437 if ( !GetBatchCount() )
4440 m_rowLabelWin
->Refresh();
4446 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4448 size_t pos
= msg
.GetCommandInt();
4449 int numRows
= msg
.GetCommandInt2();
4450 m_numRows
-= numRows
;
4452 if ( !m_rowHeights
.IsEmpty() )
4454 m_rowHeights
.RemoveAt( pos
, numRows
);
4455 m_rowBottoms
.RemoveAt( pos
, numRows
);
4458 for ( i
= 0; i
< m_numRows
; i
++ )
4460 h
+= m_rowHeights
[i
];
4461 m_rowBottoms
[i
] = h
;
4466 m_currentCellCoords
= wxGridNoCellCoords
;
4470 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4471 m_currentCellCoords
.Set( 0, 0 );
4475 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4476 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4478 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4479 // ifdef'd out following patch from Paul Gammans
4481 // No need to touch column attributes, unless we
4482 // removed _all_ rows, in this case, we remove
4483 // all column attributes.
4484 // I hate to do this here, but the
4485 // needed data is not available inside UpdateAttrRows.
4486 if ( !GetNumberRows() )
4487 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4490 if ( !GetBatchCount() )
4493 m_rowLabelWin
->Refresh();
4499 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4501 size_t pos
= msg
.GetCommandInt();
4502 int numCols
= msg
.GetCommandInt2();
4503 m_numCols
+= numCols
;
4505 if ( !m_colWidths
.IsEmpty() )
4507 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4508 m_colRights
.Insert( 0, pos
, numCols
);
4511 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4513 for ( i
= pos
; i
< m_numCols
; i
++ )
4515 right
+= m_colWidths
[i
];
4516 m_colRights
[i
] = right
;
4519 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4521 // if we have just inserted cols into an empty grid the current
4522 // cell will be undefined...
4524 SetCurrentCell( 0, 0 );
4528 m_selection
->UpdateCols( pos
, numCols
);
4529 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4531 attrProvider
->UpdateAttrCols( pos
, numCols
);
4532 if ( !GetBatchCount() )
4535 m_colLabelWin
->Refresh();
4542 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4544 int numCols
= msg
.GetCommandInt();
4545 int oldNumCols
= m_numCols
;
4546 m_numCols
+= numCols
;
4547 if ( !m_colWidths
.IsEmpty() )
4549 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4550 m_colRights
.Add( 0, numCols
);
4553 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4555 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4557 right
+= m_colWidths
[i
];
4558 m_colRights
[i
] = right
;
4561 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4563 // if we have just inserted cols into an empty grid the current
4564 // cell will be undefined...
4566 SetCurrentCell( 0, 0 );
4568 if ( !GetBatchCount() )
4571 m_colLabelWin
->Refresh();
4577 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4579 size_t pos
= msg
.GetCommandInt();
4580 int numCols
= msg
.GetCommandInt2();
4581 m_numCols
-= numCols
;
4583 if ( !m_colWidths
.IsEmpty() )
4585 m_colWidths
.RemoveAt( pos
, numCols
);
4586 m_colRights
.RemoveAt( pos
, numCols
);
4589 for ( i
= 0; i
< m_numCols
; i
++ )
4591 w
+= m_colWidths
[i
];
4597 m_currentCellCoords
= wxGridNoCellCoords
;
4601 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4602 m_currentCellCoords
.Set( 0, 0 );
4606 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4607 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4609 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4610 // ifdef'd out following patch from Paul Gammans
4612 // No need to touch row attributes, unless we
4613 // removed _all_ columns, in this case, we remove
4614 // all row attributes.
4615 // I hate to do this here, but the
4616 // needed data is not available inside UpdateAttrCols.
4617 if ( !GetNumberCols() )
4618 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4621 if ( !GetBatchCount() )
4624 m_colLabelWin
->Refresh();
4631 if (result
&& !GetBatchCount() )
4632 m_gridWin
->Refresh();
4637 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4639 wxRegionIterator
iter( reg
);
4642 wxArrayInt rowlabels
;
4649 // TODO: remove this when we can...
4650 // There is a bug in wxMotif that gives garbage update
4651 // rectangles if you jump-scroll a long way by clicking the
4652 // scrollbar with middle button. This is a work-around
4654 #if defined(__WXMOTIF__)
4656 m_gridWin
->GetClientSize( &cw
, &ch
);
4657 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4658 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4661 // logical bounds of update region
4664 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4665 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4667 // find the row labels within these bounds
4670 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4672 if ( GetRowBottom(row
) < top
)
4675 if ( GetRowTop(row
) > bottom
)
4678 rowlabels
.Add( row
);
4688 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4690 wxRegionIterator
iter( reg
);
4693 wxArrayInt colLabels
;
4700 // TODO: remove this when we can...
4701 // There is a bug in wxMotif that gives garbage update
4702 // rectangles if you jump-scroll a long way by clicking the
4703 // scrollbar with middle button. This is a work-around
4705 #if defined(__WXMOTIF__)
4707 m_gridWin
->GetClientSize( &cw
, &ch
);
4708 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4709 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4712 // logical bounds of update region
4715 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4716 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4718 // find the cells within these bounds
4721 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4723 if ( GetColRight(col
) < left
)
4726 if ( GetColLeft(col
) > right
)
4729 colLabels
.Add( col
);
4738 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4740 wxRegionIterator
iter( reg
);
4743 wxGridCellCoordsArray cellsExposed
;
4745 int left
, top
, right
, bottom
;
4750 // TODO: remove this when we can...
4751 // There is a bug in wxMotif that gives garbage update
4752 // rectangles if you jump-scroll a long way by clicking the
4753 // scrollbar with middle button. This is a work-around
4755 #if defined(__WXMOTIF__)
4757 m_gridWin
->GetClientSize( &cw
, &ch
);
4758 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4759 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4760 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4761 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4764 // logical bounds of update region
4766 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4767 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4769 // find the cells within these bounds
4772 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4774 if ( GetRowBottom(row
) <= top
)
4777 if ( GetRowTop(row
) > bottom
)
4780 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4782 if ( GetColRight(col
) <= left
)
4785 if ( GetColLeft(col
) > right
)
4788 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4795 return cellsExposed
;
4799 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4802 wxPoint
pos( event
.GetPosition() );
4803 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4805 if ( event
.Dragging() )
4809 m_isDragging
= true;
4810 m_rowLabelWin
->CaptureMouse();
4813 if ( event
.LeftIsDown() )
4815 switch( m_cursorMode
)
4817 case WXGRID_CURSOR_RESIZE_ROW
:
4819 int cw
, ch
, left
, dummy
;
4820 m_gridWin
->GetClientSize( &cw
, &ch
);
4821 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4823 wxClientDC
dc( m_gridWin
);
4826 GetRowTop(m_dragRowOrCol
) +
4827 GetRowMinimalHeight(m_dragRowOrCol
) );
4828 dc
.SetLogicalFunction(wxINVERT
);
4829 if ( m_dragLastPos
>= 0 )
4831 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4833 dc
.DrawLine( left
, y
, left
+cw
, y
);
4838 case WXGRID_CURSOR_SELECT_ROW
:
4839 if ( (row
= YToRow( y
)) >= 0 )
4843 m_selection
->SelectRow( row
,
4844 event
.ControlDown(),
4851 // default label to suppress warnings about "enumeration value
4852 // 'xxx' not handled in switch
4860 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4865 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4866 m_isDragging
= false;
4869 // ------------ Entering or leaving the window
4871 if ( event
.Entering() || event
.Leaving() )
4873 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4877 // ------------ Left button pressed
4879 else if ( event
.LeftDown() )
4881 // don't send a label click event for a hit on the
4882 // edge of the row label - this is probably the user
4883 // wanting to resize the row
4885 if ( YToEdgeOfRow(y
) < 0 )
4889 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4891 if ( !event
.ShiftDown() && !event
.ControlDown() )
4895 if ( event
.ShiftDown() )
4897 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4900 GetNumberCols() - 1,
4901 event
.ControlDown(),
4908 m_selection
->SelectRow( row
,
4909 event
.ControlDown(),
4916 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4921 // starting to drag-resize a row
4923 if ( CanDragRowSize() )
4924 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4929 // ------------ Left double click
4931 else if (event
.LeftDClick() )
4933 int row
= YToEdgeOfRow(y
);
4938 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4940 // no default action at the moment
4945 // adjust row height depending on label text
4946 AutoSizeRowLabelSize( row
);
4948 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4954 // ------------ Left button released
4956 else if ( event
.LeftUp() )
4958 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4960 DoEndDragResizeRow();
4962 // Note: we are ending the event *after* doing
4963 // default processing in this case
4965 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4968 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4973 // ------------ Right button down
4975 else if ( event
.RightDown() )
4979 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4981 // no default action at the moment
4986 // ------------ Right double click
4988 else if ( event
.RightDClick() )
4992 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4994 // no default action at the moment
4999 // ------------ No buttons down and mouse moving
5001 else if ( event
.Moving() )
5003 m_dragRowOrCol
= YToEdgeOfRow( y
);
5004 if ( m_dragRowOrCol
>= 0 )
5006 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5008 // don't capture the mouse yet
5009 if ( CanDragRowSize() )
5010 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5013 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5015 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5021 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5024 wxPoint
pos( event
.GetPosition() );
5025 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5027 if ( event
.Dragging() )
5031 m_isDragging
= true;
5032 m_colLabelWin
->CaptureMouse();
5035 if ( event
.LeftIsDown() )
5037 switch( m_cursorMode
)
5039 case WXGRID_CURSOR_RESIZE_COL
:
5041 int cw
, ch
, dummy
, top
;
5042 m_gridWin
->GetClientSize( &cw
, &ch
);
5043 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5045 wxClientDC
dc( m_gridWin
);
5048 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5049 GetColMinimalWidth(m_dragRowOrCol
));
5050 dc
.SetLogicalFunction(wxINVERT
);
5051 if ( m_dragLastPos
>= 0 )
5053 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5055 dc
.DrawLine( x
, top
, x
, top
+ch
);
5060 case WXGRID_CURSOR_SELECT_COL
:
5061 if ( (col
= XToCol( x
)) >= 0 )
5065 m_selection
->SelectCol( col
,
5066 event
.ControlDown(),
5073 // default label to suppress warnings about "enumeration value
5074 // 'xxx' not handled in switch
5082 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5087 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5088 m_isDragging
= false;
5091 // ------------ Entering or leaving the window
5093 if ( event
.Entering() || event
.Leaving() )
5095 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5099 // ------------ Left button pressed
5101 else if ( event
.LeftDown() )
5103 // don't send a label click event for a hit on the
5104 // edge of the col label - this is probably the user
5105 // wanting to resize the col
5107 if ( XToEdgeOfCol(x
) < 0 )
5111 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5113 if ( !event
.ShiftDown() && !event
.ControlDown() )
5117 if ( event
.ShiftDown() )
5119 m_selection
->SelectBlock( 0,
5120 m_currentCellCoords
.GetCol(),
5121 GetNumberRows() - 1, col
,
5122 event
.ControlDown(),
5129 m_selection
->SelectCol( col
,
5130 event
.ControlDown(),
5137 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5142 // starting to drag-resize a col
5144 if ( CanDragColSize() )
5145 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5150 // ------------ Left double click
5152 if ( event
.LeftDClick() )
5154 int col
= XToEdgeOfCol(x
);
5159 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5161 // no default action at the moment
5166 // adjust column width depending on label text
5167 AutoSizeColLabelSize( col
);
5169 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5175 // ------------ Left button released
5177 else if ( event
.LeftUp() )
5179 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5181 DoEndDragResizeCol();
5183 // Note: we are ending the event *after* doing
5184 // default processing in this case
5186 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5189 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5194 // ------------ Right button down
5196 else if ( event
.RightDown() )
5200 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5202 // no default action at the moment
5207 // ------------ Right double click
5209 else if ( event
.RightDClick() )
5213 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5215 // no default action at the moment
5220 // ------------ No buttons down and mouse moving
5222 else if ( event
.Moving() )
5224 m_dragRowOrCol
= XToEdgeOfCol( x
);
5225 if ( m_dragRowOrCol
>= 0 )
5227 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5229 // don't capture the cursor yet
5230 if ( CanDragColSize() )
5231 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5234 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5236 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5242 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5244 if ( event
.LeftDown() )
5246 // indicate corner label by having both row and
5249 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5255 else if ( event
.LeftDClick() )
5257 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5260 else if ( event
.RightDown() )
5262 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5264 // no default action at the moment
5268 else if ( event
.RightDClick() )
5270 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5272 // no default action at the moment
5277 void wxGrid::ChangeCursorMode(CursorMode mode
,
5282 static const wxChar
*cursorModes
[] =
5291 wxLogTrace(_T("grid"),
5292 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5293 win
== m_colLabelWin
? _T("colLabelWin")
5294 : win
? _T("rowLabelWin")
5296 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5297 #endif // __WXDEBUG__
5299 if ( mode
== m_cursorMode
&&
5300 win
== m_winCapture
&&
5301 captureMouse
== (m_winCapture
!= NULL
))
5306 // by default use the grid itself
5312 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5313 m_winCapture
= (wxWindow
*)NULL
;
5316 m_cursorMode
= mode
;
5318 switch ( m_cursorMode
)
5320 case WXGRID_CURSOR_RESIZE_ROW
:
5321 win
->SetCursor( m_rowResizeCursor
);
5324 case WXGRID_CURSOR_RESIZE_COL
:
5325 win
->SetCursor( m_colResizeCursor
);
5329 win
->SetCursor( *wxSTANDARD_CURSOR
);
5332 // we need to capture mouse when resizing
5333 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5334 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5336 if ( captureMouse
&& resize
)
5338 win
->CaptureMouse();
5343 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5346 wxPoint
pos( event
.GetPosition() );
5347 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5349 wxGridCellCoords coords
;
5350 XYToCell( x
, y
, coords
);
5352 int cell_rows
, cell_cols
;
5353 bool isFirstDrag
= !m_isDragging
;
5354 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5355 if ((cell_rows
< 0) || (cell_cols
< 0))
5357 coords
.SetRow(coords
.GetRow() + cell_rows
);
5358 coords
.SetCol(coords
.GetCol() + cell_cols
);
5361 if ( event
.Dragging() )
5363 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5365 // Don't start doing anything until the mouse has been drug at
5366 // least 3 pixels in any direction...
5369 if (m_startDragPos
== wxDefaultPosition
)
5371 m_startDragPos
= pos
;
5374 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5378 m_isDragging
= true;
5379 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5381 // Hide the edit control, so it
5382 // won't interfer with drag-shrinking.
5383 if ( IsCellEditControlShown() )
5385 HideCellEditControl();
5386 SaveEditControlValue();
5389 // Have we captured the mouse yet?
5392 m_winCapture
= m_gridWin
;
5393 m_winCapture
->CaptureMouse();
5396 if ( coords
!= wxGridNoCellCoords
)
5398 if ( event
.ControlDown() )
5400 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5401 m_selectingKeyboard
= coords
;
5402 HighlightBlock ( m_selectingKeyboard
, coords
);
5404 else if ( CanDragCell() )
5408 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5409 m_selectingKeyboard
= coords
;
5411 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5419 if ( !IsSelection() )
5421 HighlightBlock( coords
, coords
);
5425 HighlightBlock( m_currentCellCoords
, coords
);
5429 if (! IsVisible(coords
))
5431 MakeCellVisible(coords
);
5432 // TODO: need to introduce a delay or something here. The
5433 // scrolling is way to fast, at least on MSW - also on GTK.
5437 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5439 int cw
, ch
, left
, dummy
;
5440 m_gridWin
->GetClientSize( &cw
, &ch
);
5441 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5443 wxClientDC
dc( m_gridWin
);
5445 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5446 GetRowMinimalHeight(m_dragRowOrCol
) );
5447 dc
.SetLogicalFunction(wxINVERT
);
5448 if ( m_dragLastPos
>= 0 )
5450 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5452 dc
.DrawLine( left
, y
, left
+cw
, y
);
5455 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5457 int cw
, ch
, dummy
, top
;
5458 m_gridWin
->GetClientSize( &cw
, &ch
);
5459 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5461 wxClientDC
dc( m_gridWin
);
5463 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5464 GetColMinimalWidth(m_dragRowOrCol
) );
5465 dc
.SetLogicalFunction(wxINVERT
);
5466 if ( m_dragLastPos
>= 0 )
5468 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5470 dc
.DrawLine( x
, top
, x
, top
+ch
);
5477 m_isDragging
= false;
5478 m_startDragPos
= wxDefaultPosition
;
5480 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5481 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5484 if ( event
.Entering() || event
.Leaving() )
5486 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5487 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5492 // ------------ Left button pressed
5494 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5496 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5501 if ( !event
.ControlDown() )
5503 if ( event
.ShiftDown() )
5507 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5508 m_currentCellCoords
.GetCol(),
5511 event
.ControlDown(),
5517 else if ( XToEdgeOfCol(x
) < 0 &&
5518 YToEdgeOfRow(y
) < 0 )
5520 DisableCellEditControl();
5521 MakeCellVisible( coords
);
5523 if ( event
.ControlDown() )
5527 m_selection
->ToggleCellSelection( coords
.GetRow(),
5529 event
.ControlDown(),
5534 m_selectingTopLeft
= wxGridNoCellCoords
;
5535 m_selectingBottomRight
= wxGridNoCellCoords
;
5536 m_selectingKeyboard
= coords
;
5540 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5541 SetCurrentCell( coords
);
5544 if ( m_selection
->GetSelectionMode() !=
5545 wxGrid::wxGridSelectCells
)
5547 HighlightBlock( coords
, coords
);
5556 // ------------ Left double click
5558 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5560 DisableCellEditControl();
5562 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5564 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5569 // we want double click to select a cell and start editing
5570 // (i.e. to behave in same way as sequence of two slow clicks):
5571 m_waitForSlowClick
= true;
5578 // ------------ Left button released
5580 else if ( event
.LeftUp() )
5582 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5586 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5587 m_winCapture
= NULL
;
5590 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5593 EnableCellEditControl();
5595 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5596 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5597 editor
->StartingClick();
5601 m_waitForSlowClick
= false;
5603 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5604 m_selectingBottomRight
!= wxGridNoCellCoords
)
5608 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5609 m_selectingTopLeft
.GetCol(),
5610 m_selectingBottomRight
.GetRow(),
5611 m_selectingBottomRight
.GetCol(),
5612 event
.ControlDown(),
5618 m_selectingTopLeft
= wxGridNoCellCoords
;
5619 m_selectingBottomRight
= wxGridNoCellCoords
;
5621 // Show the edit control, if it has been hidden for
5623 ShowCellEditControl();
5626 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5628 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5629 DoEndDragResizeRow();
5631 // Note: we are ending the event *after* doing
5632 // default processing in this case
5634 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5636 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5638 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5639 DoEndDragResizeCol();
5641 // Note: we are ending the event *after* doing
5642 // default processing in this case
5644 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5651 // ------------ Right button down
5653 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5655 DisableCellEditControl();
5656 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5661 // no default action at the moment
5666 // ------------ Right double click
5668 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5670 DisableCellEditControl();
5671 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5676 // no default action at the moment
5680 // ------------ Moving and no button action
5682 else if ( event
.Moving() && !event
.IsButton() )
5684 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5686 // out of grid cell area
5687 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5691 int dragRow
= YToEdgeOfRow( y
);
5692 int dragCol
= XToEdgeOfCol( x
);
5694 // Dragging on the corner of a cell to resize in both
5695 // directions is not implemented yet...
5697 if ( dragRow
>= 0 && dragCol
>= 0 )
5699 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5705 m_dragRowOrCol
= dragRow
;
5707 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5709 if ( CanDragRowSize() && CanDragGridSize() )
5710 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5715 m_dragRowOrCol
= dragCol
;
5723 m_dragRowOrCol
= dragCol
;
5725 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5727 if ( CanDragColSize() && CanDragGridSize() )
5728 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5734 // Neither on a row or col edge
5736 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5738 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5744 void wxGrid::DoEndDragResizeRow()
5746 if ( m_dragLastPos
>= 0 )
5748 // erase the last line and resize the row
5750 int cw
, ch
, left
, dummy
;
5751 m_gridWin
->GetClientSize( &cw
, &ch
);
5752 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5754 wxClientDC
dc( m_gridWin
);
5756 dc
.SetLogicalFunction( wxINVERT
);
5757 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5758 HideCellEditControl();
5759 SaveEditControlValue();
5761 int rowTop
= GetRowTop(m_dragRowOrCol
);
5762 SetRowSize( m_dragRowOrCol
,
5763 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5765 if ( !GetBatchCount() )
5767 // Only needed to get the correct rect.y:
5768 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5770 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5771 rect
.width
= m_rowLabelWidth
;
5772 rect
.height
= ch
- rect
.y
;
5773 m_rowLabelWin
->Refresh( true, &rect
);
5775 // if there is a multicell block, paint all of it
5778 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5779 int leftCol
= XToCol(left
);
5780 int rightCol
= internalXToCol(left
+cw
);
5783 for (i
=leftCol
; i
<rightCol
; i
++)
5785 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5786 if (cell_rows
< subtract_rows
)
5787 subtract_rows
= cell_rows
;
5789 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5790 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5791 rect
.height
= ch
- rect
.y
;
5794 m_gridWin
->Refresh( false, &rect
);
5797 ShowCellEditControl();
5802 void wxGrid::DoEndDragResizeCol()
5804 if ( m_dragLastPos
>= 0 )
5806 // erase the last line and resize the col
5808 int cw
, ch
, dummy
, top
;
5809 m_gridWin
->GetClientSize( &cw
, &ch
);
5810 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5812 wxClientDC
dc( m_gridWin
);
5814 dc
.SetLogicalFunction( wxINVERT
);
5815 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5816 HideCellEditControl();
5817 SaveEditControlValue();
5819 int colLeft
= GetColLeft(m_dragRowOrCol
);
5820 SetColSize( m_dragRowOrCol
,
5821 wxMax( m_dragLastPos
- colLeft
,
5822 GetColMinimalWidth(m_dragRowOrCol
) ) );
5824 if ( !GetBatchCount() )
5826 // Only needed to get the correct rect.x:
5827 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5829 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5830 rect
.width
= cw
- rect
.x
;
5831 rect
.height
= m_colLabelHeight
;
5832 m_colLabelWin
->Refresh( true, &rect
);
5834 // if there is a multicell block, paint all of it
5837 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5838 int topRow
= YToRow(top
);
5839 int bottomRow
= internalYToRow(top
+cw
);
5842 for (i
=topRow
; i
<bottomRow
; i
++)
5844 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5845 if (cell_cols
< subtract_cols
)
5846 subtract_cols
= cell_cols
;
5848 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5849 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5850 rect
.width
= cw
- rect
.x
;
5853 m_gridWin
->Refresh( false, &rect
);
5856 ShowCellEditControl();
5863 // ------ interaction with data model
5865 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5867 switch ( msg
.GetId() )
5869 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5870 return GetModelValues();
5872 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5873 return SetModelValues();
5875 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5876 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5877 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5878 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5879 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5880 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5881 return Redimension( msg
);
5890 // The behaviour of this function depends on the grid table class
5891 // Clear() function. For the default wxGridStringTable class the
5892 // behavious is to replace all cell contents with wxEmptyString but
5893 // not to change the number of rows or cols.
5895 void wxGrid::ClearGrid()
5899 if (IsCellEditControlEnabled())
5900 DisableCellEditControl();
5903 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5908 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5910 // TODO: something with updateLabels flag
5914 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5920 if (IsCellEditControlEnabled())
5921 DisableCellEditControl();
5923 bool done
= m_table
->InsertRows( pos
, numRows
);
5926 // the table will have sent the results of the insert row
5927 // operation to this view object as a grid table message
5933 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5935 // TODO: something with updateLabels flag
5939 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5945 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5947 // the table will have sent the results of the append row
5948 // operation to this view object as a grid table message
5954 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5956 // TODO: something with updateLabels flag
5960 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5966 if (IsCellEditControlEnabled())
5967 DisableCellEditControl();
5969 bool done
= m_table
->DeleteRows( pos
, numRows
);
5971 // the table will have sent the results of the delete row
5972 // operation to this view object as a grid table message
5978 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5980 // TODO: something with updateLabels flag
5984 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5990 if (IsCellEditControlEnabled())
5991 DisableCellEditControl();
5993 bool done
= m_table
->InsertCols( pos
, numCols
);
5995 // the table will have sent the results of the insert col
5996 // operation to this view object as a grid table message
6002 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6004 // TODO: something with updateLabels flag
6008 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6014 bool done
= m_table
->AppendCols( numCols
);
6016 // the table will have sent the results of the append col
6017 // operation to this view object as a grid table message
6023 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6025 // TODO: something with updateLabels flag
6029 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6035 if (IsCellEditControlEnabled())
6036 DisableCellEditControl();
6038 bool done
= m_table
->DeleteCols( pos
, numCols
);
6040 // the table will have sent the results of the delete col
6041 // operation to this view object as a grid table message
6049 // ----- event handlers
6052 // Generate a grid event based on a mouse event and
6053 // return the result of ProcessEvent()
6055 int wxGrid::SendEvent( const wxEventType type
,
6057 wxMouseEvent
& mouseEv
)
6062 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6064 int rowOrCol
= (row
== -1 ? col
: row
);
6066 wxGridSizeEvent
gridEvt( GetId(),
6070 mouseEv
.GetX() + GetRowLabelSize(),
6071 mouseEv
.GetY() + GetColLabelSize(),
6072 mouseEv
.ControlDown(),
6073 mouseEv
.ShiftDown(),
6075 mouseEv
.MetaDown() );
6077 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6078 vetoed
= !gridEvt
.IsAllowed();
6080 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6082 // Right now, it should _never_ end up here!
6083 wxGridRangeSelectEvent
gridEvt( GetId(),
6087 m_selectingBottomRight
,
6089 mouseEv
.ControlDown(),
6090 mouseEv
.ShiftDown(),
6092 mouseEv
.MetaDown() );
6094 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6095 vetoed
= !gridEvt
.IsAllowed();
6099 wxGridEvent
gridEvt( GetId(),
6103 mouseEv
.GetX() + GetRowLabelSize(),
6104 mouseEv
.GetY() + GetColLabelSize(),
6106 mouseEv
.ControlDown(),
6107 mouseEv
.ShiftDown(),
6109 mouseEv
.MetaDown() );
6110 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6111 vetoed
= !gridEvt
.IsAllowed();
6114 // A Veto'd event may not be `claimed' so test this first
6115 if (vetoed
) return -1;
6116 return claimed
? 1 : 0;
6120 // Generate a grid event of specified type and return the result
6121 // of ProcessEvent().
6123 int wxGrid::SendEvent( const wxEventType type
,
6129 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6131 int rowOrCol
= (row
== -1 ? col
: row
);
6133 wxGridSizeEvent
gridEvt( GetId(),
6138 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6139 vetoed
= !gridEvt
.IsAllowed();
6143 wxGridEvent
gridEvt( GetId(),
6148 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6149 vetoed
= !gridEvt
.IsAllowed();
6152 // A Veto'd event may not be `claimed' so test this first
6153 if (vetoed
) return -1;
6154 return claimed
? 1 : 0;
6158 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6160 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6163 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6165 // Don't do anything if between Begin/EndBatch...
6166 // EndBatch() will do all this on the last nested one anyway.
6167 if (! GetBatchCount())
6169 // Refresh to get correct scrolled position:
6170 wxScrolledWindow::Refresh(eraseb
,rect
);
6174 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6175 int width_label
, width_cell
, height_label
, height_cell
;
6178 //Copy rectangle can get scroll offsets..
6179 rect_x
= rect
->GetX();
6180 rect_y
= rect
->GetY();
6181 rectWidth
= rect
->GetWidth();
6182 rectHeight
= rect
->GetHeight();
6184 width_label
= m_rowLabelWidth
- rect_x
;
6185 if (width_label
> rectWidth
) width_label
= rectWidth
;
6187 height_label
= m_colLabelHeight
- rect_y
;
6188 if (height_label
> rectHeight
) height_label
= rectHeight
;
6190 if (rect_x
> m_rowLabelWidth
)
6192 x
= rect_x
- m_rowLabelWidth
;
6193 width_cell
= rectWidth
;
6198 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6201 if (rect_y
> m_colLabelHeight
)
6203 y
= rect_y
- m_colLabelHeight
;
6204 height_cell
= rectHeight
;
6209 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6212 // Paint corner label part intersecting rect.
6213 if ( width_label
> 0 && height_label
> 0 )
6215 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6216 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6219 // Paint col labels part intersecting rect.
6220 if ( width_cell
> 0 && height_label
> 0 )
6222 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6223 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6226 // Paint row labels part intersecting rect.
6227 if ( width_label
> 0 && height_cell
> 0 )
6229 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6230 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6233 // Paint cell area part intersecting rect.
6234 if ( width_cell
> 0 && height_cell
> 0 )
6236 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6237 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6242 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6243 m_colLabelWin
->Refresh(eraseb
, NULL
);
6244 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6245 m_gridWin
->Refresh(eraseb
, NULL
);
6250 void wxGrid::OnSize( wxSizeEvent
& event
)
6252 // position the child windows
6255 // don't call CalcDimensions() from here, the base class handles the size
6261 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6263 if ( m_inOnKeyDown
)
6265 // shouldn't be here - we are going round in circles...
6267 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6270 m_inOnKeyDown
= true;
6272 // propagate the event up and see if it gets processed
6274 wxWindow
*parent
= GetParent();
6275 wxKeyEvent
keyEvt( event
);
6276 keyEvt
.SetEventObject( parent
);
6278 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6281 // try local handlers
6283 switch ( event
.GetKeyCode() )
6286 if ( event
.ControlDown() )
6288 MoveCursorUpBlock( event
.ShiftDown() );
6292 MoveCursorUp( event
.ShiftDown() );
6297 if ( event
.ControlDown() )
6299 MoveCursorDownBlock( event
.ShiftDown() );
6303 MoveCursorDown( event
.ShiftDown() );
6308 if ( event
.ControlDown() )
6310 MoveCursorLeftBlock( event
.ShiftDown() );
6314 MoveCursorLeft( event
.ShiftDown() );
6319 if ( event
.ControlDown() )
6321 MoveCursorRightBlock( event
.ShiftDown() );
6325 MoveCursorRight( event
.ShiftDown() );
6330 case WXK_NUMPAD_ENTER
:
6331 if ( event
.ControlDown() )
6333 event
.Skip(); // to let the edit control have the return
6337 if ( GetGridCursorRow() < GetNumberRows()-1 )
6339 MoveCursorDown( event
.ShiftDown() );
6343 // at the bottom of a column
6344 DisableCellEditControl();
6354 if (event
.ShiftDown())
6356 if ( GetGridCursorCol() > 0 )
6358 MoveCursorLeft( false );
6363 DisableCellEditControl();
6368 if ( GetGridCursorCol() < GetNumberCols()-1 )
6370 MoveCursorRight( false );
6375 DisableCellEditControl();
6381 if ( event
.ControlDown() )
6383 MakeCellVisible( 0, 0 );
6384 SetCurrentCell( 0, 0 );
6393 if ( event
.ControlDown() )
6395 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6396 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6413 if ( event
.ControlDown() )
6417 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6418 m_currentCellCoords
.GetCol(),
6419 event
.ControlDown(),
6426 if ( !IsEditable() )
6428 MoveCursorRight( false );
6431 // Otherwise fall through to default
6434 // is it possible to edit the current cell at all?
6435 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6437 // yes, now check whether the cells editor accepts the key
6438 int row
= m_currentCellCoords
.GetRow();
6439 int col
= m_currentCellCoords
.GetCol();
6440 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6441 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6443 // <F2> is special and will always start editing, for
6444 // other keys - ask the editor itself
6445 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6446 || editor
->IsAcceptedKey(event
) )
6448 // ensure cell is visble
6449 MakeCellVisible(row
, col
);
6450 EnableCellEditControl();
6452 // a problem can arise if the cell is not completely
6453 // visible (even after calling MakeCellVisible the
6454 // control is not created and calling StartingKey will
6456 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6468 // let others process char events with modifiers or all
6469 // char events for readonly cells
6476 m_inOnKeyDown
= false;
6479 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6481 // try local handlers
6483 if ( event
.GetKeyCode() == WXK_SHIFT
)
6485 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6486 m_selectingBottomRight
!= wxGridNoCellCoords
)
6490 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6491 m_selectingTopLeft
.GetCol(),
6492 m_selectingBottomRight
.GetRow(),
6493 m_selectingBottomRight
.GetCol(),
6494 event
.ControlDown(),
6501 m_selectingTopLeft
= wxGridNoCellCoords
;
6502 m_selectingBottomRight
= wxGridNoCellCoords
;
6503 m_selectingKeyboard
= wxGridNoCellCoords
;
6507 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6511 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6513 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6515 // the event has been intercepted - do nothing
6519 wxClientDC
dc(m_gridWin
);
6522 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6524 DisableCellEditControl();
6526 if ( IsVisible( m_currentCellCoords
, false ) )
6529 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6530 if ( !m_gridLinesEnabled
)
6538 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6540 // Otherwise refresh redraws the highlight!
6541 m_currentCellCoords
= coords
;
6543 DrawGridCellArea(dc
,cells
);
6544 DrawAllGridLines( dc
, r
);
6548 m_currentCellCoords
= coords
;
6550 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6551 DrawCellHighlight(dc
, attr
);
6556 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6559 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6563 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6566 rightCol
= GetNumberCols() - 1;
6568 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6571 bottomRow
= GetNumberRows() - 1;
6575 if ( topRow
> bottomRow
)
6582 if ( leftCol
> rightCol
)
6589 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6590 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6592 // First the case that we selected a completely new area
6593 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6594 m_selectingBottomRight
== wxGridNoCellCoords
)
6597 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6598 wxGridCellCoords ( bottomRow
, rightCol
) );
6599 m_gridWin
->Refresh( false, &rect
);
6601 // Now handle changing an existing selection area.
6602 else if ( m_selectingTopLeft
!= updateTopLeft
||
6603 m_selectingBottomRight
!= updateBottomRight
)
6605 // Compute two optimal update rectangles:
6606 // Either one rectangle is a real subset of the
6607 // other, or they are (almost) disjoint!
6609 bool need_refresh
[4];
6613 need_refresh
[3] = false;
6616 // Store intermediate values
6617 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6618 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6619 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6620 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6622 // Determine the outer/inner coordinates.
6623 if (oldLeft
> leftCol
)
6629 if (oldTop
> topRow
)
6635 if (oldRight
< rightCol
)
6638 oldRight
= rightCol
;
6641 if (oldBottom
< bottomRow
)
6644 oldBottom
= bottomRow
;
6648 // Now, either the stuff marked old is the outer
6649 // rectangle or we don't have a situation where one
6650 // is contained in the other.
6652 if ( oldLeft
< leftCol
)
6654 // Refresh the newly selected or deselected
6655 // area to the left of the old or new selection.
6656 need_refresh
[0] = true;
6657 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6659 wxGridCellCoords ( oldBottom
,
6663 if ( oldTop
< topRow
)
6665 // Refresh the newly selected or deselected
6666 // area above the old or new selection.
6667 need_refresh
[1] = true;
6668 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6670 wxGridCellCoords ( topRow
- 1,
6674 if ( oldRight
> rightCol
)
6676 // Refresh the newly selected or deselected
6677 // area to the right of the old or new selection.
6678 need_refresh
[2] = true;
6679 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6681 wxGridCellCoords ( oldBottom
,
6685 if ( oldBottom
> bottomRow
)
6687 // Refresh the newly selected or deselected
6688 // area below the old or new selection.
6689 need_refresh
[3] = true;
6690 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6692 wxGridCellCoords ( oldBottom
,
6696 // various Refresh() calls
6697 for (i
= 0; i
< 4; i
++ )
6698 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6699 m_gridWin
->Refresh( false, &(rect
[i
]) );
6702 m_selectingTopLeft
= updateTopLeft
;
6703 m_selectingBottomRight
= updateBottomRight
;
6707 // ------ functions to get/send data (see also public functions)
6710 bool wxGrid::GetModelValues()
6712 // Hide the editor, so it won't hide a changed value.
6713 HideCellEditControl();
6717 // all we need to do is repaint the grid
6719 m_gridWin
->Refresh();
6727 bool wxGrid::SetModelValues()
6731 // Disable the editor, so it won't hide a changed value.
6732 // Do we also want to save the current value of the editor first?
6734 DisableCellEditControl();
6738 for ( row
= 0; row
< m_numRows
; row
++ )
6740 for ( col
= 0; col
< m_numCols
; col
++ )
6742 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6754 // Note - this function only draws cells that are in the list of
6755 // exposed cells (usually set from the update region by
6756 // CalcExposedCells)
6758 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6760 if ( !m_numRows
|| !m_numCols
) return;
6762 int i
, numCells
= cells
.GetCount();
6763 int row
, col
, cell_rows
, cell_cols
;
6764 wxGridCellCoordsArray redrawCells
;
6766 for ( i
= numCells
-1; i
>= 0; i
-- )
6768 row
= cells
[i
].GetRow();
6769 col
= cells
[i
].GetCol();
6770 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6772 // If this cell is part of a multicell block, find owner for repaint
6773 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6775 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6776 bool marked
= false;
6777 for ( int j
= 0; j
< numCells
; j
++ )
6779 if ( cell
== cells
[j
] )
6787 int count
= redrawCells
.GetCount();
6788 for (int j
= 0; j
< count
; j
++)
6790 if ( cell
== redrawCells
[j
] )
6796 if (!marked
) redrawCells
.Add( cell
);
6798 continue; // don't bother drawing this cell
6801 // If this cell is empty, find cell to left that might want to overflow
6802 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6804 for ( int l
= 0; l
< cell_rows
; l
++ )
6806 // find a cell in this row to left alreay marked for repaint
6808 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6809 if ((redrawCells
[k
].GetCol() < left
) &&
6810 (redrawCells
[k
].GetRow() == row
))
6811 left
=redrawCells
[k
].GetCol();
6813 if (left
== col
) left
= 0; // oh well
6815 for (int j
= col
-1; j
>= left
; j
--)
6817 if (!m_table
->IsEmptyCell(row
+l
, j
))
6819 if (GetCellOverflow(row
+l
, j
))
6821 wxGridCellCoords
cell(row
+l
, j
);
6822 bool marked
= false;
6824 for (int k
= 0; k
< numCells
; k
++)
6826 if ( cell
== cells
[k
] )
6834 int count
= redrawCells
.GetCount();
6835 for (int k
= 0; k
< count
; k
++)
6837 if ( cell
== redrawCells
[k
] )
6843 if (!marked
) redrawCells
.Add( cell
);
6851 DrawCell( dc
, cells
[i
] );
6854 numCells
= redrawCells
.GetCount();
6856 for ( i
= numCells
- 1; i
>= 0; i
-- )
6858 DrawCell( dc
, redrawCells
[i
] );
6863 void wxGrid::DrawGridSpace( wxDC
& dc
)
6866 m_gridWin
->GetClientSize( &cw
, &ch
);
6869 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6871 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6872 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6874 if ( right
> rightCol
|| bottom
> bottomRow
)
6877 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6879 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6880 dc
.SetPen( *wxTRANSPARENT_PEN
);
6882 if ( right
> rightCol
)
6884 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6887 if ( bottom
> bottomRow
)
6889 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6895 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6897 int row
= coords
.GetRow();
6898 int col
= coords
.GetCol();
6900 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6903 // we draw the cell border ourselves
6904 #if !WXGRID_DRAW_LINES
6905 if ( m_gridLinesEnabled
)
6906 DrawCellBorder( dc
, coords
);
6909 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6911 bool isCurrent
= coords
== m_currentCellCoords
;
6913 wxRect rect
= CellToRect( row
, col
);
6915 // if the editor is shown, we should use it and not the renderer
6916 // Note: However, only if it is really _shown_, i.e. not hidden!
6917 if ( isCurrent
&& IsCellEditControlShown() )
6919 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6920 editor
->PaintBackground(rect
, attr
);
6925 // but all the rest is drawn by the cell renderer and hence may be
6927 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6928 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6935 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6937 int row
= m_currentCellCoords
.GetRow();
6938 int col
= m_currentCellCoords
.GetCol();
6940 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6943 wxRect rect
= CellToRect(row
, col
);
6945 // hmmm... what could we do here to show that the cell is disabled?
6946 // for now, I just draw a thinner border than for the other ones, but
6947 // it doesn't look really good
6949 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6953 // The center of th drawn line is where the position/width/height of
6954 // the rectangle is actually at, (on wxMSW atr least,) so we will
6955 // reduce the size of the rectangle to compensate for the thickness of
6956 // the line. If this is too strange on non wxMSW platforms then
6957 // please #ifdef this appropriately.
6958 rect
.x
+= penWidth
/2;
6959 rect
.y
+= penWidth
/2;
6960 rect
.width
-= penWidth
-1;
6961 rect
.height
-= penWidth
-1;
6964 // Now draw the rectangle
6965 // use the cellHighlightColour if the cell is inside a selection, this
6966 // will ensure the cell is always visible.
6967 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6968 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6969 dc
.DrawRectangle(rect
);
6973 // VZ: my experiments with 3d borders...
6975 // how to properly set colours for arbitrary bg?
6976 wxCoord x1
= rect
.x
,
6978 x2
= rect
.x
+ rect
.width
-1,
6979 y2
= rect
.y
+ rect
.height
-1;
6981 dc
.SetPen(*wxWHITE_PEN
);
6982 dc
.DrawLine(x1
, y1
, x2
, y1
);
6983 dc
.DrawLine(x1
, y1
, x1
, y2
);
6985 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6986 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6988 dc
.SetPen(*wxBLACK_PEN
);
6989 dc
.DrawLine(x1
, y2
, x2
, y2
);
6990 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6995 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6997 int row
= coords
.GetRow();
6998 int col
= coords
.GetCol();
6999 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7002 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7004 wxRect rect
= CellToRect( row
, col
);
7006 // right hand border
7008 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7009 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7013 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7014 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7017 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7019 // This if block was previously in wxGrid::OnPaint but that doesn't
7020 // seem to get called under wxGTK - MB
7022 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7023 m_numRows
&& m_numCols
)
7025 m_currentCellCoords
.Set(0, 0);
7028 if ( IsCellEditControlShown() )
7030 // don't show highlight when the edit control is shown
7034 // if the active cell was repainted, repaint its highlight too because it
7035 // might have been damaged by the grid lines
7036 size_t count
= cells
.GetCount();
7037 for ( size_t n
= 0; n
< count
; n
++ )
7039 if ( cells
[n
] == m_currentCellCoords
)
7041 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7042 DrawCellHighlight(dc
, attr
);
7050 // TODO: remove this ???
7051 // This is used to redraw all grid lines e.g. when the grid line colour
7054 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7056 #if !WXGRID_DRAW_LINES
7060 if ( !m_gridLinesEnabled
||
7062 !m_numCols
) return;
7064 int top
, bottom
, left
, right
;
7066 #if 0 //#ifndef __WXGTK__
7070 m_gridWin
->GetClientSize(&cw
, &ch
);
7072 // virtual coords of visible area
7074 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7075 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7080 reg
.GetBox(x
, y
, w
, h
);
7081 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7082 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7086 m_gridWin
->GetClientSize(&cw
, &ch
);
7087 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7088 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7091 // avoid drawing grid lines past the last row and col
7093 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7094 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7096 // no gridlines inside multicells, clip them out
7097 int leftCol
= internalXToCol(left
);
7098 int topRow
= internalYToRow(top
);
7099 int rightCol
= internalXToCol(right
);
7100 int bottomRow
= internalYToRow(bottom
);
7101 wxRegion
clippedcells(0, 0, cw
, ch
);
7104 int i
, j
, cell_rows
, cell_cols
;
7107 for (j
=topRow
; j
<bottomRow
; j
++)
7109 for (i
=leftCol
; i
<rightCol
; i
++)
7111 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7112 if ((cell_rows
> 1) || (cell_cols
> 1))
7114 rect
= CellToRect(j
,i
);
7115 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7116 clippedcells
.Subtract(rect
);
7118 else if ((cell_rows
< 0) || (cell_cols
< 0))
7120 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7121 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7122 clippedcells
.Subtract(rect
);
7126 dc
.SetClippingRegion( clippedcells
);
7128 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7130 // horizontal grid lines
7132 // already declared above - int i;
7133 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7135 int bot
= GetRowBottom(i
) - 1;
7144 dc
.DrawLine( left
, bot
, right
, bot
);
7149 // vertical grid lines
7151 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7153 int colRight
= GetColRight(i
) - 1;
7154 if ( colRight
> right
)
7159 if ( colRight
>= left
)
7161 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7164 dc
.DestroyClippingRegion();
7168 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7170 if ( !m_numRows
) return;
7173 size_t numLabels
= rows
.GetCount();
7175 for ( i
= 0; i
< numLabels
; i
++ )
7177 DrawRowLabel( dc
, rows
[i
] );
7182 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7184 if ( GetRowHeight(row
) <= 0 )
7187 int rowTop
= GetRowTop(row
),
7188 rowBottom
= GetRowBottom(row
) - 1;
7190 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7191 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7192 m_rowLabelWidth
-1, rowBottom
);
7194 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7196 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7198 dc
.SetPen( *wxWHITE_PEN
);
7199 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7200 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7202 dc
.SetBackgroundMode( wxTRANSPARENT
);
7203 dc
.SetTextForeground( GetLabelTextColour() );
7204 dc
.SetFont( GetLabelFont() );
7207 GetRowLabelAlignment( &hAlign
, &vAlign
);
7211 rect
.SetY( GetRowTop(row
) + 2 );
7212 rect
.SetWidth( m_rowLabelWidth
- 4 );
7213 rect
.SetHeight( GetRowHeight(row
) - 4 );
7214 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7218 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7220 if ( !m_numCols
) return;
7223 size_t numLabels
= cols
.GetCount();
7225 for ( i
= 0; i
< numLabels
; i
++ )
7227 DrawColLabel( dc
, cols
[i
] );
7232 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7234 if ( GetColWidth(col
) <= 0 )
7237 int colLeft
= GetColLeft(col
),
7238 colRight
= GetColRight(col
) - 1;
7240 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7241 dc
.DrawLine( colRight
, 0,
7242 colRight
, m_colLabelHeight
-1 );
7244 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7246 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7247 colRight
+1, m_colLabelHeight
-1 );
7249 dc
.SetPen( *wxWHITE_PEN
);
7250 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7251 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7253 dc
.SetBackgroundMode( wxTRANSPARENT
);
7254 dc
.SetTextForeground( GetLabelTextColour() );
7255 dc
.SetFont( GetLabelFont() );
7257 int hAlign
, vAlign
, orient
;
7258 GetColLabelAlignment( &hAlign
, &vAlign
);
7259 orient
= GetColLabelTextOrientation();
7262 rect
.SetX( colLeft
+ 2 );
7264 rect
.SetWidth( GetColWidth(col
) - 4 );
7265 rect
.SetHeight( m_colLabelHeight
- 4 );
7266 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7269 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7270 const wxString
& value
,
7274 int textOrientation
)
7276 wxArrayString lines
;
7278 StringToLines( value
, lines
);
7281 //Forward to new API.
7282 DrawTextRectangle( dc
,
7291 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7292 const wxArrayString
& lines
,
7296 int textOrientation
)
7298 long textWidth
, textHeight
;
7299 long lineWidth
, lineHeight
;
7302 dc
.SetClippingRegion( rect
);
7304 nLines
= lines
.GetCount();
7308 float x
= 0.0, y
= 0.0;
7310 if( textOrientation
== wxHORIZONTAL
)
7311 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7313 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7317 case wxALIGN_BOTTOM
:
7318 if( textOrientation
== wxHORIZONTAL
)
7319 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7321 x
= rect
.x
+ rect
.width
- textWidth
;
7324 case wxALIGN_CENTRE
:
7325 if( textOrientation
== wxHORIZONTAL
)
7326 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7328 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7333 if( textOrientation
== wxHORIZONTAL
)
7340 // Align each line of a multi-line label
7341 for( l
= 0; l
< nLines
; l
++ )
7343 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7345 switch( horizAlign
)
7348 if( textOrientation
== wxHORIZONTAL
)
7349 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7351 y
= rect
.y
+ lineWidth
+ 1;
7354 case wxALIGN_CENTRE
:
7355 if( textOrientation
== wxHORIZONTAL
)
7356 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7358 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7363 if( textOrientation
== wxHORIZONTAL
)
7366 y
= rect
.y
+ rect
.height
- 1;
7370 if( textOrientation
== wxHORIZONTAL
)
7372 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7377 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7382 dc
.DestroyClippingRegion();
7386 // Split multi line text up into an array of strings. Any existing
7387 // contents of the string array are preserved.
7389 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7393 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7394 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7396 while ( startPos
< (int)tVal
.Length() )
7398 pos
= tVal
.Mid(startPos
).Find( eol
);
7403 else if ( pos
== 0 )
7405 lines
.Add( wxEmptyString
);
7409 lines
.Add( value
.Mid(startPos
, pos
) );
7413 if ( startPos
< (int)value
.Length() )
7415 lines
.Add( value
.Mid( startPos
) );
7420 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7421 const wxArrayString
& lines
,
7422 long *width
, long *height
)
7429 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7431 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7432 w
= wxMax( w
, lineW
);
7441 // ------ Batch processing.
7443 void wxGrid::EndBatch()
7445 if ( m_batchCount
> 0 )
7448 if ( !m_batchCount
)
7451 m_rowLabelWin
->Refresh();
7452 m_colLabelWin
->Refresh();
7453 m_cornerLabelWin
->Refresh();
7454 m_gridWin
->Refresh();
7459 // Use this, rather than wxWindow::Refresh(), to force an immediate
7460 // repainting of the grid. Has no effect if you are already inside a
7461 // BeginBatch / EndBatch block.
7463 void wxGrid::ForceRefresh()
7471 // ------ Edit control functions
7475 void wxGrid::EnableEditing( bool edit
)
7477 // TODO: improve this ?
7479 if ( edit
!= m_editable
)
7481 if(!edit
) EnableCellEditControl(edit
);
7487 void wxGrid::EnableCellEditControl( bool enable
)
7492 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7493 SetCurrentCell( 0, 0 );
7495 if ( enable
!= m_cellEditCtrlEnabled
)
7499 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7502 // this should be checked by the caller!
7503 wxASSERT_MSG( CanEnableCellControl(),
7504 _T("can't enable editing for this cell!") );
7506 // do it before ShowCellEditControl()
7507 m_cellEditCtrlEnabled
= enable
;
7509 ShowCellEditControl();
7513 //FIXME:add veto support
7514 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7516 HideCellEditControl();
7517 SaveEditControlValue();
7519 // do it after HideCellEditControl()
7520 m_cellEditCtrlEnabled
= enable
;
7525 bool wxGrid::IsCurrentCellReadOnly() const
7528 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7529 bool readonly
= attr
->IsReadOnly();
7535 bool wxGrid::CanEnableCellControl() const
7537 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7538 !IsCurrentCellReadOnly();
7542 bool wxGrid::IsCellEditControlEnabled() const
7544 // the cell edit control might be disable for all cells or just for the
7545 // current one if it's read only
7546 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7549 bool wxGrid::IsCellEditControlShown() const
7551 bool isShown
= false;
7553 if ( m_cellEditCtrlEnabled
)
7555 int row
= m_currentCellCoords
.GetRow();
7556 int col
= m_currentCellCoords
.GetCol();
7557 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7558 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7563 if ( editor
->IsCreated() )
7565 isShown
= editor
->GetControl()->IsShown();
7575 void wxGrid::ShowCellEditControl()
7577 if ( IsCellEditControlEnabled() )
7579 if ( !IsVisible( m_currentCellCoords
) )
7581 m_cellEditCtrlEnabled
= false;
7586 wxRect rect
= CellToRect( m_currentCellCoords
);
7587 int row
= m_currentCellCoords
.GetRow();
7588 int col
= m_currentCellCoords
.GetCol();
7590 // if this is part of a multicell, find owner (topleft)
7591 int cell_rows
, cell_cols
;
7592 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7593 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7597 m_currentCellCoords
.SetRow( row
);
7598 m_currentCellCoords
.SetCol( col
);
7601 // convert to scrolled coords
7603 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7605 // done in PaintBackground()
7607 // erase the highlight and the cell contents because the editor
7608 // might not cover the entire cell
7609 wxClientDC
dc( m_gridWin
);
7611 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7612 dc
.SetPen(*wxTRANSPARENT_PEN
);
7613 dc
.DrawRectangle(rect
);
7616 // cell is shifted by one pixel
7617 // However, don't allow x or y to become negative
7618 // since the SetSize() method interprets that as
7625 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7626 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7627 if ( !editor
->IsCreated() )
7629 editor
->Create(m_gridWin
, wxID_ANY
,
7630 new wxGridCellEditorEvtHandler(this, editor
));
7632 wxGridEditorCreatedEvent
evt(GetId(),
7633 wxEVT_GRID_EDITOR_CREATED
,
7637 editor
->GetControl());
7638 GetEventHandler()->ProcessEvent(evt
);
7642 // resize editor to overflow into righthand cells if allowed
7643 int maxWidth
= rect
.width
;
7644 wxString value
= GetCellValue(row
, col
);
7645 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7648 GetTextExtent(value
, &maxWidth
, &y
,
7649 NULL
, NULL
, &attr
->GetFont());
7650 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7652 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7653 if (rect
.x
+maxWidth
> client_right
)
7654 maxWidth
= client_right
- rect
.x
;
7656 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7658 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7659 // may have changed earlier
7660 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7663 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7664 // looks weird going over a multicell
7665 if (m_table
->IsEmptyCell(row
,i
) &&
7666 (rect
.width
< maxWidth
) && (c_rows
== 1))
7667 rect
.width
+= GetColWidth(i
);
7671 if (rect
.GetRight() > client_right
)
7672 rect
.SetRight(client_right
-1);
7675 editor
->SetCellAttr(attr
);
7676 editor
->SetSize( rect
);
7677 editor
->Show( true, attr
);
7679 // recalc dimensions in case we need to
7680 // expand the scrolled window to account for editor
7683 editor
->BeginEdit(row
, col
, this);
7684 editor
->SetCellAttr(NULL
);
7693 void wxGrid::HideCellEditControl()
7695 if ( IsCellEditControlEnabled() )
7697 int row
= m_currentCellCoords
.GetRow();
7698 int col
= m_currentCellCoords
.GetCol();
7700 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7701 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7702 editor
->Show( false );
7705 m_gridWin
->SetFocus();
7706 // refresh whole row to the right
7707 wxRect
rect( CellToRect(row
, col
) );
7708 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7709 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7710 m_gridWin
->Refresh( false, &rect
);
7715 void wxGrid::SaveEditControlValue()
7717 if ( IsCellEditControlEnabled() )
7719 int row
= m_currentCellCoords
.GetRow();
7720 int col
= m_currentCellCoords
.GetCol();
7722 wxString oldval
= GetCellValue(row
,col
);
7724 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7725 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7726 bool changed
= editor
->EndEdit(row
, col
, this);
7733 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7734 m_currentCellCoords
.GetRow(),
7735 m_currentCellCoords
.GetCol() ) < 0 ) {
7737 // Event has been vetoed, set the data back.
7738 SetCellValue(row
,col
,oldval
);
7746 // ------ Grid location functions
7747 // Note that all of these functions work with the logical coordinates of
7748 // grid cells and labels so you will need to convert from device
7749 // coordinates for mouse events etc.
7752 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7754 int row
= YToRow(y
);
7755 int col
= XToCol(x
);
7757 if ( row
== -1 || col
== -1 )
7759 coords
= wxGridNoCellCoords
;
7763 coords
.Set( row
, col
);
7768 // Internal Helper function for computing row or column from some
7769 // (unscrolled) coordinate value, using either
7770 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7771 // of m_rowBottoms/m_ColRights to speed up the search!
7773 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7774 const wxArrayInt
& BorderArray
, int nMax
,
7779 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7785 size_t i_max
= coord
/ defaultDist
,
7788 if (BorderArray
.IsEmpty())
7790 if((int) i_max
< nMax
)
7792 return clipToMinMax
? nMax
- 1 : -1;
7795 if ( i_max
>= BorderArray
.GetCount())
7796 i_max
= BorderArray
.GetCount() - 1;
7799 if ( coord
>= BorderArray
[i_max
])
7803 i_max
= coord
/ minDist
;
7805 i_max
= BorderArray
.GetCount() - 1;
7807 if ( i_max
>= BorderArray
.GetCount())
7808 i_max
= BorderArray
.GetCount() - 1;
7810 if ( coord
>= BorderArray
[i_max
])
7811 return clipToMinMax
? (int)i_max
: -1;
7812 if ( coord
< BorderArray
[0] )
7815 while ( i_max
- i_min
> 0 )
7817 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7818 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7819 if (coord
>= BorderArray
[ i_max
- 1])
7823 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7824 if (coord
< BorderArray
[median
])
7832 int wxGrid::YToRow( int y
)
7834 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7835 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7839 int wxGrid::XToCol( int x
)
7841 return CoordToRowOrCol(x
, m_defaultColWidth
,
7842 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7846 // return the row number that that the y coord is near the edge of, or
7847 // -1 if not near an edge
7849 int wxGrid::YToEdgeOfRow( int y
)
7852 i
= internalYToRow(y
);
7854 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7856 // We know that we are in row i, test whether we are
7857 // close enough to lower or upper border, respectively.
7858 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7860 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7868 // return the col number that that the x coord is near the edge of, or
7869 // -1 if not near an edge
7871 int wxGrid::XToEdgeOfCol( int x
)
7874 i
= internalXToCol(x
);
7876 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7878 // We know that we are in column i, test whether we are
7879 // close enough to right or left border, respectively.
7880 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7882 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7890 wxRect
wxGrid::CellToRect( int row
, int col
)
7892 wxRect
rect( -1, -1, -1, -1 );
7894 if ( row
>= 0 && row
< m_numRows
&&
7895 col
>= 0 && col
< m_numCols
)
7897 int i
, cell_rows
, cell_cols
;
7898 rect
.width
= rect
.height
= 0;
7899 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7900 // if negative then find multicell owner
7901 if (cell_rows
< 0) row
+= cell_rows
;
7902 if (cell_cols
< 0) col
+= cell_cols
;
7903 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7905 rect
.x
= GetColLeft(col
);
7906 rect
.y
= GetRowTop(row
);
7907 for (i
=col
; i
<col
+cell_cols
; i
++)
7908 rect
.width
+= GetColWidth(i
);
7909 for (i
=row
; i
<row
+cell_rows
; i
++)
7910 rect
.height
+= GetRowHeight(i
);
7913 // if grid lines are enabled, then the area of the cell is a bit smaller
7914 if (m_gridLinesEnabled
) {
7922 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7924 // get the cell rectangle in logical coords
7926 wxRect
r( CellToRect( row
, col
) );
7928 // convert to device coords
7930 int left
, top
, right
, bottom
;
7931 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7932 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7934 // check against the client area of the grid window
7937 m_gridWin
->GetClientSize( &cw
, &ch
);
7939 if ( wholeCellVisible
)
7941 // is the cell wholly visible ?
7943 return ( left
>= 0 && right
<= cw
&&
7944 top
>= 0 && bottom
<= ch
);
7948 // is the cell partly visible ?
7950 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7951 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7956 // make the specified cell location visible by doing a minimal amount
7959 void wxGrid::MakeCellVisible( int row
, int col
)
7963 int xpos
= -1, ypos
= -1;
7965 if ( row
>= 0 && row
< m_numRows
&&
7966 col
>= 0 && col
< m_numCols
)
7968 // get the cell rectangle in logical coords
7970 wxRect
r( CellToRect( row
, col
) );
7972 // convert to device coords
7974 int left
, top
, right
, bottom
;
7975 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7976 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7979 m_gridWin
->GetClientSize( &cw
, &ch
);
7985 else if ( bottom
> ch
)
7987 int h
= r
.GetHeight();
7989 for ( i
= row
-1; i
>= 0; i
-- )
7991 int rowHeight
= GetRowHeight(i
);
7992 if ( h
+ rowHeight
> ch
)
7999 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8000 // have rounding errors (this is important, because if we do, we
8001 // might not scroll at all and some cells won't be redrawn)
8003 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8005 ypos
+= GRID_SCROLL_LINE_Y
;
8012 else if ( right
> cw
)
8014 // position the view so that the cell is on the right
8016 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8017 xpos
= x0
+ (right
- cw
);
8019 // see comment for ypos above
8020 xpos
+= GRID_SCROLL_LINE_X
;
8023 if ( xpos
!= -1 || ypos
!= -1 )
8026 xpos
/= GRID_SCROLL_LINE_X
;
8028 ypos
/= GRID_SCROLL_LINE_Y
;
8029 Scroll( xpos
, ypos
);
8037 // ------ Grid cursor movement functions
8040 bool wxGrid::MoveCursorUp( bool expandSelection
)
8042 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8043 m_currentCellCoords
.GetRow() >= 0 )
8045 if ( expandSelection
)
8047 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8048 m_selectingKeyboard
= m_currentCellCoords
;
8049 if ( m_selectingKeyboard
.GetRow() > 0 )
8051 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8052 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8053 m_selectingKeyboard
.GetCol() );
8054 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8057 else if ( m_currentCellCoords
.GetRow() > 0 )
8060 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8061 m_currentCellCoords
.GetCol() );
8062 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8063 m_currentCellCoords
.GetCol() );
8074 bool wxGrid::MoveCursorDown( bool expandSelection
)
8076 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8077 m_currentCellCoords
.GetRow() < m_numRows
)
8079 if ( expandSelection
)
8081 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8082 m_selectingKeyboard
= m_currentCellCoords
;
8083 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8085 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8086 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8087 m_selectingKeyboard
.GetCol() );
8088 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8091 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8094 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8095 m_currentCellCoords
.GetCol() );
8096 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8097 m_currentCellCoords
.GetCol() );
8108 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8110 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8111 m_currentCellCoords
.GetCol() >= 0 )
8113 if ( expandSelection
)
8115 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8116 m_selectingKeyboard
= m_currentCellCoords
;
8117 if ( m_selectingKeyboard
.GetCol() > 0 )
8119 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8120 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8121 m_selectingKeyboard
.GetCol() );
8122 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8125 else if ( m_currentCellCoords
.GetCol() > 0 )
8128 MakeCellVisible( m_currentCellCoords
.GetRow(),
8129 m_currentCellCoords
.GetCol() - 1 );
8130 SetCurrentCell( m_currentCellCoords
.GetRow(),
8131 m_currentCellCoords
.GetCol() - 1 );
8142 bool wxGrid::MoveCursorRight( bool expandSelection
)
8144 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8145 m_currentCellCoords
.GetCol() < m_numCols
)
8147 if ( expandSelection
)
8149 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8150 m_selectingKeyboard
= m_currentCellCoords
;
8151 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8153 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8154 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8155 m_selectingKeyboard
.GetCol() );
8156 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8159 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8162 MakeCellVisible( m_currentCellCoords
.GetRow(),
8163 m_currentCellCoords
.GetCol() + 1 );
8164 SetCurrentCell( m_currentCellCoords
.GetRow(),
8165 m_currentCellCoords
.GetCol() + 1 );
8176 bool wxGrid::MovePageUp()
8178 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8180 int row
= m_currentCellCoords
.GetRow();
8184 m_gridWin
->GetClientSize( &cw
, &ch
);
8186 int y
= GetRowTop(row
);
8187 int newRow
= internalYToRow( y
- ch
+ 1 );
8189 if ( newRow
== row
)
8191 //row > 0 , so newrow can never be less than 0 here.
8195 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8196 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8204 bool wxGrid::MovePageDown()
8206 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8208 int row
= m_currentCellCoords
.GetRow();
8209 if ( (row
+1) < m_numRows
)
8212 m_gridWin
->GetClientSize( &cw
, &ch
);
8214 int y
= GetRowTop(row
);
8215 int newRow
= internalYToRow( y
+ ch
);
8216 if ( newRow
== row
)
8218 // row < m_numRows , so newrow can't overflow here.
8222 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8223 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8231 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8234 m_currentCellCoords
!= wxGridNoCellCoords
&&
8235 m_currentCellCoords
.GetRow() > 0 )
8237 int row
= m_currentCellCoords
.GetRow();
8238 int col
= m_currentCellCoords
.GetCol();
8240 if ( m_table
->IsEmptyCell(row
, col
) )
8242 // starting in an empty cell: find the next block of
8248 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8251 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8253 // starting at the top of a block: find the next block
8259 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8264 // starting within a block: find the top of the block
8269 if ( m_table
->IsEmptyCell(row
, col
) )
8277 MakeCellVisible( row
, col
);
8278 if ( expandSelection
)
8280 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8281 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8286 SetCurrentCell( row
, col
);
8294 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8297 m_currentCellCoords
!= wxGridNoCellCoords
&&
8298 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8300 int row
= m_currentCellCoords
.GetRow();
8301 int col
= m_currentCellCoords
.GetCol();
8303 if ( m_table
->IsEmptyCell(row
, col
) )
8305 // starting in an empty cell: find the next block of
8308 while ( row
< m_numRows
-1 )
8311 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8314 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8316 // starting at the bottom of a block: find the next block
8319 while ( row
< m_numRows
-1 )
8322 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8327 // starting within a block: find the bottom of the block
8329 while ( row
< m_numRows
-1 )
8332 if ( m_table
->IsEmptyCell(row
, col
) )
8340 MakeCellVisible( row
, col
);
8341 if ( expandSelection
)
8343 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8344 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8349 SetCurrentCell( row
, col
);
8358 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8361 m_currentCellCoords
!= wxGridNoCellCoords
&&
8362 m_currentCellCoords
.GetCol() > 0 )
8364 int row
= m_currentCellCoords
.GetRow();
8365 int col
= m_currentCellCoords
.GetCol();
8367 if ( m_table
->IsEmptyCell(row
, col
) )
8369 // starting in an empty cell: find the next block of
8375 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8378 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8380 // starting at the left of a block: find the next block
8386 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8391 // starting within a block: find the left of the block
8396 if ( m_table
->IsEmptyCell(row
, col
) )
8404 MakeCellVisible( row
, col
);
8405 if ( expandSelection
)
8407 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8408 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8413 SetCurrentCell( row
, col
);
8422 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8425 m_currentCellCoords
!= wxGridNoCellCoords
&&
8426 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8428 int row
= m_currentCellCoords
.GetRow();
8429 int col
= m_currentCellCoords
.GetCol();
8431 if ( m_table
->IsEmptyCell(row
, col
) )
8433 // starting in an empty cell: find the next block of
8436 while ( col
< m_numCols
-1 )
8439 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8442 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8444 // starting at the right of a block: find the next block
8447 while ( col
< m_numCols
-1 )
8450 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8455 // starting within a block: find the right of the block
8457 while ( col
< m_numCols
-1 )
8460 if ( m_table
->IsEmptyCell(row
, col
) )
8468 MakeCellVisible( row
, col
);
8469 if ( expandSelection
)
8471 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8472 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8477 SetCurrentCell( row
, col
);
8489 // ------ Label values and formatting
8492 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8494 *horiz
= m_rowLabelHorizAlign
;
8495 *vert
= m_rowLabelVertAlign
;
8498 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8500 *horiz
= m_colLabelHorizAlign
;
8501 *vert
= m_colLabelVertAlign
;
8504 int wxGrid::GetColLabelTextOrientation()
8506 return m_colLabelTextOrientation
;
8509 wxString
wxGrid::GetRowLabelValue( int row
)
8513 return m_table
->GetRowLabelValue( row
);
8523 wxString
wxGrid::GetColLabelValue( int col
)
8527 return m_table
->GetColLabelValue( col
);
8538 void wxGrid::SetRowLabelSize( int width
)
8540 width
= wxMax( width
, 0 );
8541 if ( width
!= m_rowLabelWidth
)
8545 m_rowLabelWin
->Show( false );
8546 m_cornerLabelWin
->Show( false );
8548 else if ( m_rowLabelWidth
== 0 )
8550 m_rowLabelWin
->Show( true );
8551 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8554 m_rowLabelWidth
= width
;
8556 wxScrolledWindow::Refresh( true );
8561 void wxGrid::SetColLabelSize( int height
)
8563 height
= wxMax( height
, 0 );
8564 if ( height
!= m_colLabelHeight
)
8568 m_colLabelWin
->Show( false );
8569 m_cornerLabelWin
->Show( false );
8571 else if ( m_colLabelHeight
== 0 )
8573 m_colLabelWin
->Show( true );
8574 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8577 m_colLabelHeight
= height
;
8579 wxScrolledWindow::Refresh( true );
8584 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8586 if ( m_labelBackgroundColour
!= colour
)
8588 m_labelBackgroundColour
= colour
;
8589 m_rowLabelWin
->SetBackgroundColour( colour
);
8590 m_colLabelWin
->SetBackgroundColour( colour
);
8591 m_cornerLabelWin
->SetBackgroundColour( colour
);
8593 if ( !GetBatchCount() )
8595 m_rowLabelWin
->Refresh();
8596 m_colLabelWin
->Refresh();
8597 m_cornerLabelWin
->Refresh();
8602 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8604 if ( m_labelTextColour
!= colour
)
8606 m_labelTextColour
= colour
;
8607 if ( !GetBatchCount() )
8609 m_rowLabelWin
->Refresh();
8610 m_colLabelWin
->Refresh();
8615 void wxGrid::SetLabelFont( const wxFont
& font
)
8618 if ( !GetBatchCount() )
8620 m_rowLabelWin
->Refresh();
8621 m_colLabelWin
->Refresh();
8625 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8627 // allow old (incorrect) defs to be used
8630 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8631 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8632 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8637 case wxTOP
: vert
= wxALIGN_TOP
; break;
8638 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8639 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8642 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8644 m_rowLabelHorizAlign
= horiz
;
8647 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8649 m_rowLabelVertAlign
= vert
;
8652 if ( !GetBatchCount() )
8654 m_rowLabelWin
->Refresh();
8658 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8660 // allow old (incorrect) defs to be used
8663 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8664 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8665 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8670 case wxTOP
: vert
= wxALIGN_TOP
; break;
8671 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8672 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8675 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8677 m_colLabelHorizAlign
= horiz
;
8680 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8682 m_colLabelVertAlign
= vert
;
8685 if ( !GetBatchCount() )
8687 m_colLabelWin
->Refresh();
8691 // Note: under MSW, the default column label font must be changed because it
8692 // does not support vertical printing
8694 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8695 // pGrid->SetLabelFont(font);
8696 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8698 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8700 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8702 m_colLabelTextOrientation
= textOrientation
;
8705 if ( !GetBatchCount() )
8707 m_colLabelWin
->Refresh();
8711 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8715 m_table
->SetRowLabelValue( row
, s
);
8716 if ( !GetBatchCount() )
8718 wxRect rect
= CellToRect( row
, 0);
8719 if ( rect
.height
> 0 )
8721 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8723 rect
.width
= m_rowLabelWidth
;
8724 m_rowLabelWin
->Refresh( true, &rect
);
8730 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8734 m_table
->SetColLabelValue( col
, s
);
8735 if ( !GetBatchCount() )
8737 wxRect rect
= CellToRect( 0, col
);
8738 if ( rect
.width
> 0 )
8740 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8742 rect
.height
= m_colLabelHeight
;
8743 m_colLabelWin
->Refresh( true, &rect
);
8749 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8751 if ( m_gridLineColour
!= colour
)
8753 m_gridLineColour
= colour
;
8755 wxClientDC
dc( m_gridWin
);
8757 DrawAllGridLines( dc
, wxRegion() );
8762 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8764 if ( m_cellHighlightColour
!= colour
)
8766 m_cellHighlightColour
= colour
;
8768 wxClientDC
dc( m_gridWin
);
8770 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8771 DrawCellHighlight(dc
, attr
);
8776 void wxGrid::SetCellHighlightPenWidth(int width
)
8778 if (m_cellHighlightPenWidth
!= width
) {
8779 m_cellHighlightPenWidth
= width
;
8781 // Just redrawing the cell highlight is not enough since that won't
8782 // make any visible change if the the thickness is getting smaller.
8783 int row
= m_currentCellCoords
.GetRow();
8784 int col
= m_currentCellCoords
.GetCol();
8785 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8787 wxRect rect
= CellToRect(row
, col
);
8788 m_gridWin
->Refresh(true, &rect
);
8792 void wxGrid::SetCellHighlightROPenWidth(int width
)
8794 if (m_cellHighlightROPenWidth
!= width
) {
8795 m_cellHighlightROPenWidth
= width
;
8797 // Just redrawing the cell highlight is not enough since that won't
8798 // make any visible change if the the thickness is getting smaller.
8799 int row
= m_currentCellCoords
.GetRow();
8800 int col
= m_currentCellCoords
.GetCol();
8801 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8803 wxRect rect
= CellToRect(row
, col
);
8804 m_gridWin
->Refresh(true, &rect
);
8808 void wxGrid::EnableGridLines( bool enable
)
8810 if ( enable
!= m_gridLinesEnabled
)
8812 m_gridLinesEnabled
= enable
;
8814 if ( !GetBatchCount() )
8818 wxClientDC
dc( m_gridWin
);
8820 DrawAllGridLines( dc
, wxRegion() );
8824 m_gridWin
->Refresh();
8831 int wxGrid::GetDefaultRowSize()
8833 return m_defaultRowHeight
;
8836 int wxGrid::GetRowSize( int row
)
8838 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8840 return GetRowHeight(row
);
8843 int wxGrid::GetDefaultColSize()
8845 return m_defaultColWidth
;
8848 int wxGrid::GetColSize( int col
)
8850 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8852 return GetColWidth(col
);
8855 // ============================================================================
8856 // access to the grid attributes: each of them has a default value in the grid
8857 // itself and may be overidden on a per-cell basis
8858 // ============================================================================
8860 // ----------------------------------------------------------------------------
8861 // setting default attributes
8862 // ----------------------------------------------------------------------------
8864 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8866 m_defaultCellAttr
->SetBackgroundColour(col
);
8868 m_gridWin
->SetBackgroundColour(col
);
8872 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8874 m_defaultCellAttr
->SetTextColour(col
);
8877 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8879 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8882 void wxGrid::SetDefaultCellOverflow( bool allow
)
8884 m_defaultCellAttr
->SetOverflow(allow
);
8887 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8889 m_defaultCellAttr
->SetFont(font
);
8892 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8894 m_defaultCellAttr
->SetRenderer(renderer
);
8897 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8899 m_defaultCellAttr
->SetEditor(editor
);
8902 // ----------------------------------------------------------------------------
8903 // access to the default attrbiutes
8904 // ----------------------------------------------------------------------------
8906 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8908 return m_defaultCellAttr
->GetBackgroundColour();
8911 wxColour
wxGrid::GetDefaultCellTextColour()
8913 return m_defaultCellAttr
->GetTextColour();
8916 wxFont
wxGrid::GetDefaultCellFont()
8918 return m_defaultCellAttr
->GetFont();
8921 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8923 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8926 bool wxGrid::GetDefaultCellOverflow()
8928 return m_defaultCellAttr
->GetOverflow();
8931 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8933 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8936 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8938 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8941 // ----------------------------------------------------------------------------
8942 // access to cell attributes
8943 // ----------------------------------------------------------------------------
8945 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8947 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8948 wxColour colour
= attr
->GetBackgroundColour();
8953 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8955 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8956 wxColour colour
= attr
->GetTextColour();
8961 wxFont
wxGrid::GetCellFont( int row
, int col
)
8963 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8964 wxFont font
= attr
->GetFont();
8969 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8971 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8972 attr
->GetAlignment(horiz
, vert
);
8976 bool wxGrid::GetCellOverflow( int row
, int col
)
8978 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8979 bool allow
= attr
->GetOverflow();
8984 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8986 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8987 attr
->GetSize( num_rows
, num_cols
);
8991 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8993 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8994 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9000 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9002 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9003 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9009 bool wxGrid::IsReadOnly(int row
, int col
) const
9011 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9012 bool isReadOnly
= attr
->IsReadOnly();
9017 // ----------------------------------------------------------------------------
9018 // attribute support: cache, automatic provider creation, ...
9019 // ----------------------------------------------------------------------------
9021 bool wxGrid::CanHaveAttributes()
9028 return m_table
->CanHaveAttributes();
9031 void wxGrid::ClearAttrCache()
9033 if ( m_attrCache
.row
!= -1 )
9035 wxSafeDecRef(m_attrCache
.attr
);
9036 m_attrCache
.attr
= NULL
;
9037 m_attrCache
.row
= -1;
9041 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9045 wxGrid
*self
= (wxGrid
*)this; // const_cast
9047 self
->ClearAttrCache();
9048 self
->m_attrCache
.row
= row
;
9049 self
->m_attrCache
.col
= col
;
9050 self
->m_attrCache
.attr
= attr
;
9055 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9057 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9059 *attr
= m_attrCache
.attr
;
9060 wxSafeIncRef(m_attrCache
.attr
);
9062 #ifdef DEBUG_ATTR_CACHE
9063 gs_nAttrCacheHits
++;
9070 #ifdef DEBUG_ATTR_CACHE
9071 gs_nAttrCacheMisses
++;
9077 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9079 wxGridCellAttr
*attr
= NULL
;
9080 // Additional test to avoid looking at the cache e.g. for
9081 // wxNoCellCoords, as this will confuse memory management.
9084 if ( !LookupAttr(row
, col
, &attr
) )
9086 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9087 : (wxGridCellAttr
*)NULL
;
9088 CacheAttr(row
, col
, attr
);
9093 attr
->SetDefAttr(m_defaultCellAttr
);
9097 attr
= m_defaultCellAttr
;
9104 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9106 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9108 wxCHECK_MSG( m_table
, attr
,
9109 _T("we may only be called if CanHaveAttributes() returned true and then m_table should be !NULL") );
9111 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9114 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9116 // artificially inc the ref count to match DecRef() in caller
9118 m_table
->SetAttr(attr
, row
, col
);
9124 // ----------------------------------------------------------------------------
9125 // setting column attributes (wrappers around SetColAttr)
9126 // ----------------------------------------------------------------------------
9128 void wxGrid::SetColFormatBool(int col
)
9130 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9133 void wxGrid::SetColFormatNumber(int col
)
9135 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9138 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9140 wxString typeName
= wxGRID_VALUE_FLOAT
;
9141 if ( (width
!= -1) || (precision
!= -1) )
9143 typeName
<< _T(':') << width
<< _T(',') << precision
;
9146 SetColFormatCustom(col
, typeName
);
9149 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9151 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9153 attr
= new wxGridCellAttr
;
9154 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9155 attr
->SetRenderer(renderer
);
9157 SetColAttr(col
, attr
);
9161 // ----------------------------------------------------------------------------
9162 // setting cell attributes: this is forwarded to the table
9163 // ----------------------------------------------------------------------------
9165 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9167 if ( CanHaveAttributes() )
9169 m_table
->SetAttr(attr
, row
, col
);
9178 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9180 if ( CanHaveAttributes() )
9182 m_table
->SetRowAttr(attr
, row
);
9191 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9193 if ( CanHaveAttributes() )
9195 m_table
->SetColAttr(attr
, col
);
9204 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9206 if ( CanHaveAttributes() )
9208 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9209 attr
->SetBackgroundColour(colour
);
9214 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9216 if ( CanHaveAttributes() )
9218 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9219 attr
->SetTextColour(colour
);
9224 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9226 if ( CanHaveAttributes() )
9228 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9229 attr
->SetFont(font
);
9234 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9236 if ( CanHaveAttributes() )
9238 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9239 attr
->SetAlignment(horiz
, vert
);
9244 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9246 if ( CanHaveAttributes() )
9248 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9249 attr
->SetOverflow(allow
);
9254 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9256 if ( CanHaveAttributes() )
9258 int cell_rows
, cell_cols
;
9260 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9261 attr
->GetSize(&cell_rows
, &cell_cols
);
9262 attr
->SetSize(num_rows
, num_cols
);
9265 // Cannot set the size of a cell to 0 or negative values
9266 // While it is perfectly legal to do that, this function cannot
9267 // handle all the possibilies, do it by hand by getting the CellAttr.
9268 // You can only set the size of a cell to 1,1 or greater with this fn
9269 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9270 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9271 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9272 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9274 // if this was already a multicell then "turn off" the other cells first
9275 if ((cell_rows
> 1) || (cell_rows
> 1))
9278 for (j
=row
; j
<row
+cell_rows
; j
++)
9280 for (i
=col
; i
<col
+cell_cols
; i
++)
9282 if ((i
!= col
) || (j
!= row
))
9284 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9285 attr_stub
->SetSize( 1, 1 );
9286 attr_stub
->DecRef();
9292 // mark the cells that will be covered by this cell to
9293 // negative or zero values to point back at this cell
9294 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9297 for (j
=row
; j
<row
+num_rows
; j
++)
9299 for (i
=col
; i
<col
+num_cols
; i
++)
9301 if ((i
!= col
) || (j
!= row
))
9303 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9304 attr_stub
->SetSize( row
-j
, col
-i
);
9305 attr_stub
->DecRef();
9313 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9315 if ( CanHaveAttributes() )
9317 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9318 attr
->SetRenderer(renderer
);
9323 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9325 if ( CanHaveAttributes() )
9327 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9328 attr
->SetEditor(editor
);
9333 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9335 if ( CanHaveAttributes() )
9337 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9338 attr
->SetReadOnly(isReadOnly
);
9343 // ----------------------------------------------------------------------------
9344 // Data type registration
9345 // ----------------------------------------------------------------------------
9347 void wxGrid::RegisterDataType(const wxString
& typeName
,
9348 wxGridCellRenderer
* renderer
,
9349 wxGridCellEditor
* editor
)
9351 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9355 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9357 wxString typeName
= m_table
->GetTypeName(row
, col
);
9358 return GetDefaultEditorForType(typeName
);
9361 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9363 wxString typeName
= m_table
->GetTypeName(row
, col
);
9364 return GetDefaultRendererForType(typeName
);
9368 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9370 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9371 if ( index
== wxNOT_FOUND
)
9373 wxFAIL_MSG(wxT("Unknown data type name"));
9378 return m_typeRegistry
->GetEditor(index
);
9382 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9384 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9385 if ( index
== wxNOT_FOUND
)
9387 wxFAIL_MSG(wxT("Unknown data type name"));
9392 return m_typeRegistry
->GetRenderer(index
);
9396 // ----------------------------------------------------------------------------
9398 // ----------------------------------------------------------------------------
9400 void wxGrid::EnableDragRowSize( bool enable
)
9402 m_canDragRowSize
= enable
;
9406 void wxGrid::EnableDragColSize( bool enable
)
9408 m_canDragColSize
= enable
;
9411 void wxGrid::EnableDragGridSize( bool enable
)
9413 m_canDragGridSize
= enable
;
9416 void wxGrid::EnableDragCell( bool enable
)
9418 m_canDragCell
= enable
;
9421 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9423 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9425 if ( resizeExistingRows
)
9427 // since we are resizing all rows to the default row size,
9428 // we can simply clear the row heights and row bottoms
9429 // arrays (which also allows us to take advantage of
9430 // some speed optimisations)
9431 m_rowHeights
.Empty();
9432 m_rowBottoms
.Empty();
9433 if ( !GetBatchCount() )
9438 void wxGrid::SetRowSize( int row
, int height
)
9440 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9442 // See comment in SetColSize
9443 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9445 if ( m_rowHeights
.IsEmpty() )
9447 // need to really create the array
9451 int h
= wxMax( 0, height
);
9452 int diff
= h
- m_rowHeights
[row
];
9454 m_rowHeights
[row
] = h
;
9456 for ( i
= row
; i
< m_numRows
; i
++ )
9458 m_rowBottoms
[i
] += diff
;
9460 if ( !GetBatchCount() )
9464 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9466 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9468 if ( resizeExistingCols
)
9470 // since we are resizing all columns to the default column size,
9471 // we can simply clear the col widths and col rights
9472 // arrays (which also allows us to take advantage of
9473 // some speed optimisations)
9474 m_colWidths
.Empty();
9475 m_colRights
.Empty();
9476 if ( !GetBatchCount() )
9481 void wxGrid::SetColSize( int col
, int width
)
9483 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9485 // should we check that it's bigger than GetColMinimalWidth(col) here?
9487 // No, because it is reasonable to assume the library user know's
9488 // what he is doing. However whe should test against the weaker
9489 // constariant of minimalAcceptableWidth, as this breaks rendering
9491 // This test then fixes sf.net bug #645734
9493 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9495 if ( m_colWidths
.IsEmpty() )
9497 // need to really create the array
9501 // if < 0 calc new width from label
9505 wxArrayString lines
;
9506 wxClientDC
dc(m_colLabelWin
);
9507 dc
.SetFont(GetLabelFont());
9508 StringToLines(GetColLabelValue(col
), lines
);
9509 GetTextBoxSize(dc
, lines
, &w
, &h
);
9512 int w
= wxMax( 0, width
);
9513 int diff
= w
- m_colWidths
[col
];
9514 m_colWidths
[col
] = w
;
9517 for ( i
= col
; i
< m_numCols
; i
++ )
9519 m_colRights
[i
] += diff
;
9521 if ( !GetBatchCount() )
9526 void wxGrid::SetColMinimalWidth( int col
, int width
)
9528 if (width
> GetColMinimalAcceptableWidth()) {
9529 m_colMinWidths
[col
] = width
;
9533 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9535 if (width
> GetRowMinimalAcceptableHeight()) {
9536 m_rowMinHeights
[row
] = width
;
9540 int wxGrid::GetColMinimalWidth(int col
) const
9542 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9543 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9546 int wxGrid::GetRowMinimalHeight(int row
) const
9548 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9549 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9552 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9554 // We do allow a width of 0 since this gives us
9555 // an easy way to temporarily hidding columns.
9558 m_minAcceptableColWidth
= width
;
9561 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9563 // We do allow a height of 0 since this gives us
9564 // an easy way to temporarily hidding rows.
9567 m_minAcceptableRowHeight
= height
;
9570 int wxGrid::GetColMinimalAcceptableWidth() const
9572 return m_minAcceptableColWidth
;
9575 int wxGrid::GetRowMinimalAcceptableHeight() const
9577 return m_minAcceptableRowHeight
;
9580 // ----------------------------------------------------------------------------
9582 // ----------------------------------------------------------------------------
9584 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9586 wxClientDC
dc(m_gridWin
);
9588 //Cancel editting of cell
9589 HideCellEditControl();
9590 SaveEditControlValue();
9592 // init both of them to avoid compiler warnings, even if weo nly need one
9600 wxCoord extent
, extentMax
= 0;
9601 int max
= column
? m_numRows
: m_numCols
;
9602 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9609 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9610 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9613 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9614 extent
= column
? size
.x
: size
.y
;
9615 if ( extent
> extentMax
)
9626 // now also compare with the column label extent
9628 dc
.SetFont( GetLabelFont() );
9632 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9633 if( GetColLabelTextOrientation() == wxVERTICAL
)
9637 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9639 extent
= column
? w
: h
;
9640 if ( extent
> extentMax
)
9647 // empty column - give default extent (notice that if extentMax is less
9648 // than default extent but != 0, it's ok)
9649 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9655 // leave some space around text
9666 SetColSize(col
, extentMax
);
9667 if ( !GetBatchCount() )
9670 m_gridWin
->GetClientSize( &cw
, &ch
);
9671 wxRect
rect ( CellToRect( 0, col
) );
9673 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9674 rect
.width
= cw
- rect
.x
;
9675 rect
.height
= m_colLabelHeight
;
9676 m_colLabelWin
->Refresh( true, &rect
);
9681 SetRowSize(row
, extentMax
);
9682 if ( !GetBatchCount() )
9685 m_gridWin
->GetClientSize( &cw
, &ch
);
9686 wxRect
rect ( CellToRect( row
, 0 ) );
9688 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9689 rect
.width
= m_rowLabelWidth
;
9690 rect
.height
= ch
- rect
.y
;
9691 m_rowLabelWin
->Refresh( true, &rect
);
9697 SetColMinimalWidth(col
, extentMax
);
9699 SetRowMinimalHeight(row
, extentMax
);
9703 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9705 int width
= m_rowLabelWidth
;
9710 for ( int col
= 0; col
< m_numCols
; col
++ )
9714 AutoSizeColumn(col
, setAsMin
);
9717 width
+= GetColWidth(col
);
9726 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9728 int height
= m_colLabelHeight
;
9733 for ( int row
= 0; row
< m_numRows
; row
++ )
9737 AutoSizeRow(row
, setAsMin
);
9740 height
+= GetRowHeight(row
);
9749 void wxGrid::AutoSize()
9753 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9755 // round up the size to a multiple of scroll step - this ensures that we
9756 // won't get the scrollbars if we're sized exactly to this width
9757 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9759 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9760 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9762 // distribute the extra space between the columns/rows to avoid having
9763 // extra white space
9765 // Remove the extra m_extraWidth + 1 added above
9766 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9767 if ( diff
&& m_numCols
)
9769 // try to resize the columns uniformly
9770 wxCoord diffPerCol
= diff
/ m_numCols
;
9773 for ( int col
= 0; col
< m_numCols
; col
++ )
9775 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9779 // add remaining amount to the last columns
9780 diff
-= diffPerCol
* m_numCols
;
9783 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9785 SetColSize(col
, GetColWidth(col
) + 1);
9791 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9792 if ( diff
&& m_numRows
)
9794 // try to resize the columns uniformly
9795 wxCoord diffPerRow
= diff
/ m_numRows
;
9798 for ( int row
= 0; row
< m_numRows
; row
++ )
9800 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9804 // add remaining amount to the last rows
9805 diff
-= diffPerRow
* m_numRows
;
9808 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9810 SetRowSize(row
, GetRowHeight(row
) + 1);
9817 SetClientSize(sizeFit
);
9820 void wxGrid::AutoSizeRowLabelSize( int row
)
9822 wxArrayString lines
;
9825 // Hide the edit control, so it
9826 // won't interfer with drag-shrinking.
9827 if( IsCellEditControlShown() )
9829 HideCellEditControl();
9830 SaveEditControlValue();
9833 // autosize row height depending on label text
9834 StringToLines( GetRowLabelValue( row
), lines
);
9835 wxClientDC
dc( m_rowLabelWin
);
9836 GetTextBoxSize( dc
, lines
, &w
, &h
);
9837 if( h
< m_defaultRowHeight
)
9838 h
= m_defaultRowHeight
;
9843 void wxGrid::AutoSizeColLabelSize( int col
)
9845 wxArrayString lines
;
9848 // Hide the edit control, so it
9849 // won't interfer with drag-shrinking.
9850 if( IsCellEditControlShown() )
9852 HideCellEditControl();
9853 SaveEditControlValue();
9856 // autosize column width depending on label text
9857 StringToLines( GetColLabelValue( col
), lines
);
9858 wxClientDC
dc( m_colLabelWin
);
9859 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9860 GetTextBoxSize( dc
, lines
, &w
, &h
);
9862 GetTextBoxSize( dc
, lines
, &h
, &w
);
9863 if( w
< m_defaultColWidth
)
9864 w
= m_defaultColWidth
;
9869 wxSize
wxGrid::DoGetBestSize() const
9871 // don't set sizes, only calculate them
9872 wxGrid
*self
= (wxGrid
*)this; // const_cast
9875 width
= self
->SetOrCalcColumnSizes(true);
9876 height
= self
->SetOrCalcRowSizes(true);
9878 if (!width
) width
=100;
9879 if (!height
) height
=80;
9881 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9882 // of the scrollbars, is there any magic incantaion for that?
9884 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9886 width
+= 1 + xpu
- (width
% xpu
);
9888 height
+= 1 + ypu
- (height
% ypu
);
9890 // limit to 1/4 of the screen size
9891 int maxwidth
, maxheight
;
9892 wxDisplaySize( & maxwidth
, & maxheight
);
9895 if ( width
> maxwidth
) width
= maxwidth
;
9896 if ( height
> maxheight
) height
= maxheight
;
9899 wxSize
best(width
, height
);
9900 // NOTE: This size should be cached, but first we need to add calls to
9901 // InvalidateBestSize everywhere that could change the results of this
9903 // CacheBestSize(size);
9913 wxPen
& wxGrid::GetDividerPen() const
9918 // ----------------------------------------------------------------------------
9919 // cell value accessor functions
9920 // ----------------------------------------------------------------------------
9922 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9926 m_table
->SetValue( row
, col
, s
);
9927 if ( !GetBatchCount() )
9930 wxRect
rect( CellToRect( row
, col
) );
9932 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9933 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9934 m_gridWin
->Refresh( false, &rect
);
9937 if ( m_currentCellCoords
.GetRow() == row
&&
9938 m_currentCellCoords
.GetCol() == col
&&
9939 IsCellEditControlShown())
9940 // Note: If we are using IsCellEditControlEnabled,
9941 // this interacts badly with calling SetCellValue from
9942 // an EVT_GRID_CELL_CHANGE handler.
9944 HideCellEditControl();
9945 ShowCellEditControl(); // will reread data from table
9952 // ------ Block, row and col selection
9955 void wxGrid::SelectRow( int row
, bool addToSelected
)
9957 if ( IsSelection() && !addToSelected
)
9961 m_selection
->SelectRow( row
, false, addToSelected
);
9965 void wxGrid::SelectCol( int col
, bool addToSelected
)
9967 if ( IsSelection() && !addToSelected
)
9971 m_selection
->SelectCol( col
, false, addToSelected
);
9975 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9976 bool addToSelected
)
9978 if ( IsSelection() && !addToSelected
)
9982 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9983 false, addToSelected
);
9987 void wxGrid::SelectAll()
9989 if ( m_numRows
> 0 && m_numCols
> 0 )
9992 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
9997 // ------ Cell, row and col deselection
10000 void wxGrid::DeselectRow( int row
)
10002 if ( !m_selection
)
10005 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10007 if ( m_selection
->IsInSelection(row
, 0 ) )
10008 m_selection
->ToggleCellSelection( row
, 0);
10012 int nCols
= GetNumberCols();
10013 for ( int i
= 0; i
< nCols
; i
++ )
10015 if ( m_selection
->IsInSelection(row
, i
) )
10016 m_selection
->ToggleCellSelection( row
, i
);
10021 void wxGrid::DeselectCol( int col
)
10023 if ( !m_selection
)
10026 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10028 if ( m_selection
->IsInSelection(0, col
) )
10029 m_selection
->ToggleCellSelection( 0, col
);
10033 int nRows
= GetNumberRows();
10034 for ( int i
= 0; i
< nRows
; i
++ )
10036 if ( m_selection
->IsInSelection(i
, col
) )
10037 m_selection
->ToggleCellSelection(i
, col
);
10042 void wxGrid::DeselectCell( int row
, int col
)
10044 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10045 m_selection
->ToggleCellSelection(row
, col
);
10048 bool wxGrid::IsSelection()
10050 return ( m_selection
&& (m_selection
->IsSelection() ||
10051 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10052 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10055 bool wxGrid::IsInSelection( int row
, int col
) const
10057 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10058 ( row
>= m_selectingTopLeft
.GetRow() &&
10059 col
>= m_selectingTopLeft
.GetCol() &&
10060 row
<= m_selectingBottomRight
.GetRow() &&
10061 col
<= m_selectingBottomRight
.GetCol() )) );
10064 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10066 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10067 return m_selection
->m_cellSelection
;
10069 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10071 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10072 return m_selection
->m_blockSelectionTopLeft
;
10074 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10076 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10077 return m_selection
->m_blockSelectionBottomRight
;
10079 wxArrayInt
wxGrid::GetSelectedRows() const
10081 if (!m_selection
) { wxArrayInt a
; return a
; }
10082 return m_selection
->m_rowSelection
;
10084 wxArrayInt
wxGrid::GetSelectedCols() const
10086 if (!m_selection
) { wxArrayInt a
; return a
; }
10087 return m_selection
->m_colSelection
;
10091 void wxGrid::ClearSelection()
10093 m_selectingTopLeft
= wxGridNoCellCoords
;
10094 m_selectingBottomRight
= wxGridNoCellCoords
;
10096 m_selection
->ClearSelection();
10100 // This function returns the rectangle that encloses the given block
10101 // in device coords clipped to the client size of the grid window.
10103 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10104 const wxGridCellCoords
&bottomRight
)
10106 wxRect
rect( wxGridNoCellRect
);
10109 cellRect
= CellToRect( topLeft
);
10110 if ( cellRect
!= wxGridNoCellRect
)
10116 rect
= wxRect( 0, 0, 0, 0 );
10119 cellRect
= CellToRect( bottomRight
);
10120 if ( cellRect
!= wxGridNoCellRect
)
10126 return wxGridNoCellRect
;
10130 int left
= rect
.GetLeft();
10131 int top
= rect
.GetTop();
10132 int right
= rect
.GetRight();
10133 int bottom
= rect
.GetBottom();
10135 int leftCol
= topLeft
.GetCol();
10136 int topRow
= topLeft
.GetRow();
10137 int rightCol
= bottomRight
.GetCol();
10138 int bottomRow
= bottomRight
.GetRow();
10156 topRow
= bottomRow
;
10161 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10163 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10165 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10167 cellRect
= CellToRect( j
, i
);
10169 if (cellRect
.x
< left
)
10171 if (cellRect
.y
< top
)
10173 if (cellRect
.x
+ cellRect
.width
> right
)
10174 right
= cellRect
.x
+ cellRect
.width
;
10175 if (cellRect
.y
+ cellRect
.height
> bottom
)
10176 bottom
= cellRect
.y
+ cellRect
.height
;
10178 else i
= rightCol
; // jump over inner cells.
10182 // convert to scrolled coords
10184 CalcScrolledPosition( left
, top
, &left
, &top
);
10185 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10188 m_gridWin
->GetClientSize( &cw
, &ch
);
10190 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10191 return wxRect( 0, 0, 0, 0);
10193 rect
.SetLeft( wxMax(0, left
) );
10194 rect
.SetTop( wxMax(0, top
) );
10195 rect
.SetRight( wxMin(cw
, right
) );
10196 rect
.SetBottom( wxMin(ch
, bottom
) );
10202 // ------ Grid event classes
10205 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10207 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10208 int row
, int col
, int x
, int y
, bool sel
,
10209 bool control
, bool shift
, bool alt
, bool meta
)
10210 : wxNotifyEvent( type
, id
)
10217 m_control
= control
;
10222 SetEventObject(obj
);
10226 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10228 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10229 int rowOrCol
, int x
, int y
,
10230 bool control
, bool shift
, bool alt
, bool meta
)
10231 : wxNotifyEvent( type
, id
)
10233 m_rowOrCol
= rowOrCol
;
10236 m_control
= control
;
10241 SetEventObject(obj
);
10245 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10247 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10248 const wxGridCellCoords
& topLeft
,
10249 const wxGridCellCoords
& bottomRight
,
10250 bool sel
, bool control
,
10251 bool shift
, bool alt
, bool meta
)
10252 : wxNotifyEvent( type
, id
)
10254 m_topLeft
= topLeft
;
10255 m_bottomRight
= bottomRight
;
10257 m_control
= control
;
10262 SetEventObject(obj
);
10266 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10268 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10269 wxObject
* obj
, int row
,
10270 int col
, wxControl
* ctrl
)
10271 : wxCommandEvent(type
, id
)
10273 SetEventObject(obj
);
10279 #endif // wxUSE_GRID