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
)
3853 SetBestFittingSize(size
);
3856 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3857 const wxPoint
& pos
, const wxSize
& size
,
3858 long style
, const wxString
& name
)
3860 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3861 style
| wxWANTS_CHARS
, name
))
3864 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3865 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3868 SetBestFittingSize(size
);
3876 // Must do this or ~wxScrollHelper will pop the wrong event handler
3877 SetTargetWindow(this);
3879 wxSafeDecRef(m_defaultCellAttr
);
3881 #ifdef DEBUG_ATTR_CACHE
3882 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3883 wxPrintf(_T("wxGrid attribute cache statistics: "
3884 "total: %u, hits: %u (%u%%)\n"),
3885 total
, gs_nAttrCacheHits
,
3886 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3892 delete m_typeRegistry
;
3898 // ----- internal init and update functions
3901 // NOTE: If using the default visual attributes works everywhere then this can
3902 // be removed as well as the #else cases below.
3903 #define _USE_VISATTR 0
3906 #include "wx/listbox.h"
3909 void wxGrid::Create()
3911 m_created
= false; // set to true by CreateGrid
3913 m_table
= (wxGridTableBase
*) NULL
;
3916 m_cellEditCtrlEnabled
= false;
3918 m_defaultCellAttr
= new wxGridCellAttr();
3920 // Set default cell attributes
3921 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3922 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3923 m_defaultCellAttr
->SetFont(GetFont());
3924 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3925 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3926 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3929 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3930 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3932 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3933 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3936 m_defaultCellAttr
->SetTextColour(
3937 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3938 m_defaultCellAttr
->SetBackgroundColour(
3939 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3944 m_currentCellCoords
= wxGridNoCellCoords
;
3946 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3947 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3949 // create the type registry
3950 m_typeRegistry
= new wxGridTypeRegistry
;
3953 // subwindow components that make up the wxGrid
3954 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3959 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3964 m_colLabelWin
= new wxGridColLabelWindow( this,
3969 m_gridWin
= new wxGridWindow( this,
3976 SetTargetWindow( m_gridWin
);
3979 wxColour gfg
= gva
.colFg
;
3980 wxColour gbg
= gva
.colBg
;
3981 wxColour lfg
= lva
.colFg
;
3982 wxColour lbg
= lva
.colBg
;
3984 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3985 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3986 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3987 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
3989 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
3990 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
3991 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
3992 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
3993 m_colLabelWin
->SetOwnForegroundColour(lfg
);
3994 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
3996 m_gridWin
->SetOwnForegroundColour(gfg
);
3997 m_gridWin
->SetOwnBackgroundColour(gbg
);
4003 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4004 wxGrid::wxGridSelectionModes selmode
)
4006 wxCHECK_MSG( !m_created
,
4008 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4010 m_numRows
= numRows
;
4011 m_numCols
= numCols
;
4013 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4014 m_table
->SetView( this );
4016 m_selection
= new wxGridSelection( this, selmode
);
4025 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4027 wxCHECK_RET( m_created
,
4028 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4030 m_selection
->SetSelectionMode( selmode
);
4033 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4035 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4036 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4038 return m_selection
->GetSelectionMode();
4041 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4042 wxGrid::wxGridSelectionModes selmode
)
4046 // stop all processing
4051 wxGridTableBase
*t
=m_table
;
4064 m_numRows
= table
->GetNumberRows();
4065 m_numCols
= table
->GetNumberCols();
4068 m_table
->SetView( this );
4071 m_selection
= new wxGridSelection( this, selmode
);
4084 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4085 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4087 if ( m_rowLabelWin
)
4089 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4093 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4096 m_labelTextColour
= wxColour( _T("BLACK") );
4099 m_attrCache
.row
= -1;
4100 m_attrCache
.col
= -1;
4101 m_attrCache
.attr
= NULL
;
4103 // TODO: something better than this ?
4105 m_labelFont
= this->GetFont();
4106 m_labelFont
.SetWeight( wxBOLD
);
4108 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4109 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4111 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4112 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4113 m_colLabelTextOrientation
= wxHORIZONTAL
;
4115 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4116 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4118 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4119 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4121 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4122 m_defaultRowHeight
+= 8;
4124 m_defaultRowHeight
+= 4;
4127 m_gridLineColour
= wxColour( 192,192,192 );
4128 m_gridLinesEnabled
= true;
4129 m_cellHighlightColour
= *wxBLACK
;
4130 m_cellHighlightPenWidth
= 2;
4131 m_cellHighlightROPenWidth
= 1;
4133 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4134 m_winCapture
= (wxWindow
*)NULL
;
4135 m_canDragRowSize
= true;
4136 m_canDragColSize
= true;
4137 m_canDragGridSize
= true;
4138 m_canDragCell
= false;
4140 m_dragRowOrCol
= -1;
4141 m_isDragging
= false;
4142 m_startDragPos
= wxDefaultPosition
;
4144 m_waitForSlowClick
= false;
4146 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4147 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4149 m_currentCellCoords
= wxGridNoCellCoords
;
4151 m_selectingTopLeft
= wxGridNoCellCoords
;
4152 m_selectingBottomRight
= wxGridNoCellCoords
;
4153 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4154 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4156 m_editable
= true; // default for whole grid
4158 m_inOnKeyDown
= false;
4165 // ----------------------------------------------------------------------------
4166 // the idea is to call these functions only when necessary because they create
4167 // quite big arrays which eat memory mostly unnecessary - in particular, if
4168 // default widths/heights are used for all rows/columns, we may not use these
4171 // with some extra code, it should be possible to only store the
4172 // widths/heights different from default ones but this will be done later...
4173 // ----------------------------------------------------------------------------
4175 void wxGrid::InitRowHeights()
4177 m_rowHeights
.Empty();
4178 m_rowBottoms
.Empty();
4180 m_rowHeights
.Alloc( m_numRows
);
4181 m_rowBottoms
.Alloc( m_numRows
);
4185 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4187 for ( int i
= 0; i
< m_numRows
; i
++ )
4189 rowBottom
+= m_defaultRowHeight
;
4190 m_rowBottoms
.Add( rowBottom
);
4194 void wxGrid::InitColWidths()
4196 m_colWidths
.Empty();
4197 m_colRights
.Empty();
4199 m_colWidths
.Alloc( m_numCols
);
4200 m_colRights
.Alloc( m_numCols
);
4203 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4205 for ( int i
= 0; i
< m_numCols
; i
++ )
4207 colRight
+= m_defaultColWidth
;
4208 m_colRights
.Add( colRight
);
4212 int wxGrid::GetColWidth(int col
) const
4214 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4217 int wxGrid::GetColLeft(int col
) const
4219 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4220 : m_colRights
[col
] - m_colWidths
[col
];
4223 int wxGrid::GetColRight(int col
) const
4225 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4229 int wxGrid::GetRowHeight(int row
) const
4231 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4234 int wxGrid::GetRowTop(int row
) const
4236 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4237 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4240 int wxGrid::GetRowBottom(int row
) const
4242 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4243 : m_rowBottoms
[row
];
4246 void wxGrid::CalcDimensions()
4249 GetClientSize( &cw
, &ch
);
4251 if ( m_rowLabelWin
->IsShown() )
4252 cw
-= m_rowLabelWidth
;
4253 if ( m_colLabelWin
->IsShown() )
4254 ch
-= m_colLabelHeight
;
4257 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4258 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4260 // take into account editor if shown
4261 if( IsCellEditControlShown() )
4264 int r
= m_currentCellCoords
.GetRow();
4265 int c
= m_currentCellCoords
.GetCol();
4266 int x
= GetColLeft(c
);
4267 int y
= GetRowTop(r
);
4269 // how big is the editor
4270 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4271 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4272 editor
->GetControl()->GetSize(&w2
, &h2
);
4275 if( w2
> w
) w
= w2
;
4276 if( h2
> h
) h
= h2
;
4281 // preserve (more or less) the previous position
4283 GetViewStart( &x
, &y
);
4285 // ensure the position is valid for the new scroll ranges
4287 x
= wxMax( w
- 1, 0 );
4289 y
= wxMax( h
- 1, 0 );
4291 // do set scrollbar parameters
4292 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4293 GetScrollX(w
), GetScrollY(h
), x
, y
,
4294 GetBatchCount() != 0);
4296 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4297 // still must reposition the children
4302 void wxGrid::CalcWindowSizes()
4304 // escape if the window is has not been fully created yet
4306 if ( m_cornerLabelWin
== NULL
)
4310 GetClientSize( &cw
, &ch
);
4312 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4313 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4315 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4316 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4318 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4319 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4321 if ( m_gridWin
&& m_gridWin
->IsShown() )
4322 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4326 // this is called when the grid table sends a message to say that it
4327 // has been redimensioned
4329 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4332 bool result
= false;
4334 // Clear the attribute cache as the attribute might refer to a different
4335 // cell than stored in the cache after adding/removing rows/columns.
4337 // By the same reasoning, the editor should be dismissed if columns are
4338 // added or removed. And for consistency, it should IMHO always be
4339 // removed, not only if the cell "underneath" it actually changes.
4340 // For now, I intentionally do not save the editor's content as the
4341 // cell it might want to save that stuff to might no longer exist.
4342 HideCellEditControl();
4344 // if we were using the default widths/heights so far, we must change them
4346 if ( m_colWidths
.IsEmpty() )
4351 if ( m_rowHeights
.IsEmpty() )
4357 switch ( msg
.GetId() )
4359 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4361 size_t pos
= msg
.GetCommandInt();
4362 int numRows
= msg
.GetCommandInt2();
4364 m_numRows
+= numRows
;
4366 if ( !m_rowHeights
.IsEmpty() )
4368 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4369 m_rowBottoms
.Insert( 0, pos
, numRows
);
4372 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4374 for ( i
= pos
; i
< m_numRows
; i
++ )
4376 bottom
+= m_rowHeights
[i
];
4377 m_rowBottoms
[i
] = bottom
;
4380 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4382 // if we have just inserted cols into an empty grid the current
4383 // cell will be undefined...
4385 SetCurrentCell( 0, 0 );
4389 m_selection
->UpdateRows( pos
, numRows
);
4390 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4392 attrProvider
->UpdateAttrRows( pos
, numRows
);
4394 if ( !GetBatchCount() )
4397 m_rowLabelWin
->Refresh();
4403 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4405 int numRows
= msg
.GetCommandInt();
4406 int oldNumRows
= m_numRows
;
4407 m_numRows
+= numRows
;
4409 if ( !m_rowHeights
.IsEmpty() )
4411 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4412 m_rowBottoms
.Add( 0, numRows
);
4415 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4417 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4419 bottom
+= m_rowHeights
[i
];
4420 m_rowBottoms
[i
] = bottom
;
4423 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4425 // if we have just inserted cols into an empty grid the current
4426 // cell will be undefined...
4428 SetCurrentCell( 0, 0 );
4430 if ( !GetBatchCount() )
4433 m_rowLabelWin
->Refresh();
4439 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4441 size_t pos
= msg
.GetCommandInt();
4442 int numRows
= msg
.GetCommandInt2();
4443 m_numRows
-= numRows
;
4445 if ( !m_rowHeights
.IsEmpty() )
4447 m_rowHeights
.RemoveAt( pos
, numRows
);
4448 m_rowBottoms
.RemoveAt( pos
, numRows
);
4451 for ( i
= 0; i
< m_numRows
; i
++ )
4453 h
+= m_rowHeights
[i
];
4454 m_rowBottoms
[i
] = h
;
4459 m_currentCellCoords
= wxGridNoCellCoords
;
4463 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4464 m_currentCellCoords
.Set( 0, 0 );
4468 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4469 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4471 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4472 // ifdef'd out following patch from Paul Gammans
4474 // No need to touch column attributes, unless we
4475 // removed _all_ rows, in this case, we remove
4476 // all column attributes.
4477 // I hate to do this here, but the
4478 // needed data is not available inside UpdateAttrRows.
4479 if ( !GetNumberRows() )
4480 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4483 if ( !GetBatchCount() )
4486 m_rowLabelWin
->Refresh();
4492 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4494 size_t pos
= msg
.GetCommandInt();
4495 int numCols
= msg
.GetCommandInt2();
4496 m_numCols
+= numCols
;
4498 if ( !m_colWidths
.IsEmpty() )
4500 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4501 m_colRights
.Insert( 0, pos
, numCols
);
4504 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4506 for ( i
= pos
; i
< m_numCols
; i
++ )
4508 right
+= m_colWidths
[i
];
4509 m_colRights
[i
] = right
;
4512 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4514 // if we have just inserted cols into an empty grid the current
4515 // cell will be undefined...
4517 SetCurrentCell( 0, 0 );
4521 m_selection
->UpdateCols( pos
, numCols
);
4522 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4524 attrProvider
->UpdateAttrCols( pos
, numCols
);
4525 if ( !GetBatchCount() )
4528 m_colLabelWin
->Refresh();
4535 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4537 int numCols
= msg
.GetCommandInt();
4538 int oldNumCols
= m_numCols
;
4539 m_numCols
+= numCols
;
4540 if ( !m_colWidths
.IsEmpty() )
4542 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4543 m_colRights
.Add( 0, numCols
);
4546 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4548 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4550 right
+= m_colWidths
[i
];
4551 m_colRights
[i
] = right
;
4554 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4556 // if we have just inserted cols into an empty grid the current
4557 // cell will be undefined...
4559 SetCurrentCell( 0, 0 );
4561 if ( !GetBatchCount() )
4564 m_colLabelWin
->Refresh();
4570 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4572 size_t pos
= msg
.GetCommandInt();
4573 int numCols
= msg
.GetCommandInt2();
4574 m_numCols
-= numCols
;
4576 if ( !m_colWidths
.IsEmpty() )
4578 m_colWidths
.RemoveAt( pos
, numCols
);
4579 m_colRights
.RemoveAt( pos
, numCols
);
4582 for ( i
= 0; i
< m_numCols
; i
++ )
4584 w
+= m_colWidths
[i
];
4590 m_currentCellCoords
= wxGridNoCellCoords
;
4594 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4595 m_currentCellCoords
.Set( 0, 0 );
4599 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4600 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4602 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4603 // ifdef'd out following patch from Paul Gammans
4605 // No need to touch row attributes, unless we
4606 // removed _all_ columns, in this case, we remove
4607 // all row attributes.
4608 // I hate to do this here, but the
4609 // needed data is not available inside UpdateAttrCols.
4610 if ( !GetNumberCols() )
4611 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4614 if ( !GetBatchCount() )
4617 m_colLabelWin
->Refresh();
4624 if (result
&& !GetBatchCount() )
4625 m_gridWin
->Refresh();
4630 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4632 wxRegionIterator
iter( reg
);
4635 wxArrayInt rowlabels
;
4642 // TODO: remove this when we can...
4643 // There is a bug in wxMotif that gives garbage update
4644 // rectangles if you jump-scroll a long way by clicking the
4645 // scrollbar with middle button. This is a work-around
4647 #if defined(__WXMOTIF__)
4649 m_gridWin
->GetClientSize( &cw
, &ch
);
4650 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4651 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4654 // logical bounds of update region
4657 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4658 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4660 // find the row labels within these bounds
4663 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4665 if ( GetRowBottom(row
) < top
)
4668 if ( GetRowTop(row
) > bottom
)
4671 rowlabels
.Add( row
);
4681 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4683 wxRegionIterator
iter( reg
);
4686 wxArrayInt colLabels
;
4693 // TODO: remove this when we can...
4694 // There is a bug in wxMotif that gives garbage update
4695 // rectangles if you jump-scroll a long way by clicking the
4696 // scrollbar with middle button. This is a work-around
4698 #if defined(__WXMOTIF__)
4700 m_gridWin
->GetClientSize( &cw
, &ch
);
4701 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4702 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4705 // logical bounds of update region
4708 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4709 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4711 // find the cells within these bounds
4714 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4716 if ( GetColRight(col
) < left
)
4719 if ( GetColLeft(col
) > right
)
4722 colLabels
.Add( col
);
4731 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4733 wxRegionIterator
iter( reg
);
4736 wxGridCellCoordsArray cellsExposed
;
4738 int left
, top
, right
, bottom
;
4743 // TODO: remove this when we can...
4744 // There is a bug in wxMotif that gives garbage update
4745 // rectangles if you jump-scroll a long way by clicking the
4746 // scrollbar with middle button. This is a work-around
4748 #if defined(__WXMOTIF__)
4750 m_gridWin
->GetClientSize( &cw
, &ch
);
4751 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4752 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4753 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4754 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4757 // logical bounds of update region
4759 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4760 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4762 // find the cells within these bounds
4765 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4767 if ( GetRowBottom(row
) <= top
)
4770 if ( GetRowTop(row
) > bottom
)
4773 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4775 if ( GetColRight(col
) <= left
)
4778 if ( GetColLeft(col
) > right
)
4781 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4788 return cellsExposed
;
4792 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4795 wxPoint
pos( event
.GetPosition() );
4796 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4798 if ( event
.Dragging() )
4802 m_isDragging
= true;
4803 m_rowLabelWin
->CaptureMouse();
4806 if ( event
.LeftIsDown() )
4808 switch( m_cursorMode
)
4810 case WXGRID_CURSOR_RESIZE_ROW
:
4812 int cw
, ch
, left
, dummy
;
4813 m_gridWin
->GetClientSize( &cw
, &ch
);
4814 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4816 wxClientDC
dc( m_gridWin
);
4819 GetRowTop(m_dragRowOrCol
) +
4820 GetRowMinimalHeight(m_dragRowOrCol
) );
4821 dc
.SetLogicalFunction(wxINVERT
);
4822 if ( m_dragLastPos
>= 0 )
4824 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4826 dc
.DrawLine( left
, y
, left
+cw
, y
);
4831 case WXGRID_CURSOR_SELECT_ROW
:
4832 if ( (row
= YToRow( y
)) >= 0 )
4836 m_selection
->SelectRow( row
,
4837 event
.ControlDown(),
4844 // default label to suppress warnings about "enumeration value
4845 // 'xxx' not handled in switch
4853 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4858 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4859 m_isDragging
= false;
4862 // ------------ Entering or leaving the window
4864 if ( event
.Entering() || event
.Leaving() )
4866 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4870 // ------------ Left button pressed
4872 else if ( event
.LeftDown() )
4874 // don't send a label click event for a hit on the
4875 // edge of the row label - this is probably the user
4876 // wanting to resize the row
4878 if ( YToEdgeOfRow(y
) < 0 )
4882 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4884 if ( !event
.ShiftDown() && !event
.ControlDown() )
4888 if ( event
.ShiftDown() )
4890 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4893 GetNumberCols() - 1,
4894 event
.ControlDown(),
4901 m_selection
->SelectRow( row
,
4902 event
.ControlDown(),
4909 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4914 // starting to drag-resize a row
4916 if ( CanDragRowSize() )
4917 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4922 // ------------ Left double click
4924 else if (event
.LeftDClick() )
4926 int row
= YToEdgeOfRow(y
);
4931 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4933 // no default action at the moment
4938 // adjust row height depending on label text
4939 AutoSizeRowLabelSize( row
);
4941 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4947 // ------------ Left button released
4949 else if ( event
.LeftUp() )
4951 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4953 DoEndDragResizeRow();
4955 // Note: we are ending the event *after* doing
4956 // default processing in this case
4958 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4961 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4966 // ------------ Right button down
4968 else if ( event
.RightDown() )
4972 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4974 // no default action at the moment
4979 // ------------ Right double click
4981 else if ( event
.RightDClick() )
4985 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4987 // no default action at the moment
4992 // ------------ No buttons down and mouse moving
4994 else if ( event
.Moving() )
4996 m_dragRowOrCol
= YToEdgeOfRow( y
);
4997 if ( m_dragRowOrCol
>= 0 )
4999 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5001 // don't capture the mouse yet
5002 if ( CanDragRowSize() )
5003 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5006 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5008 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5014 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5017 wxPoint
pos( event
.GetPosition() );
5018 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5020 if ( event
.Dragging() )
5024 m_isDragging
= true;
5025 m_colLabelWin
->CaptureMouse();
5028 if ( event
.LeftIsDown() )
5030 switch( m_cursorMode
)
5032 case WXGRID_CURSOR_RESIZE_COL
:
5034 int cw
, ch
, dummy
, top
;
5035 m_gridWin
->GetClientSize( &cw
, &ch
);
5036 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5038 wxClientDC
dc( m_gridWin
);
5041 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5042 GetColMinimalWidth(m_dragRowOrCol
));
5043 dc
.SetLogicalFunction(wxINVERT
);
5044 if ( m_dragLastPos
>= 0 )
5046 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5048 dc
.DrawLine( x
, top
, x
, top
+ch
);
5053 case WXGRID_CURSOR_SELECT_COL
:
5054 if ( (col
= XToCol( x
)) >= 0 )
5058 m_selection
->SelectCol( col
,
5059 event
.ControlDown(),
5066 // default label to suppress warnings about "enumeration value
5067 // 'xxx' not handled in switch
5075 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5080 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5081 m_isDragging
= false;
5084 // ------------ Entering or leaving the window
5086 if ( event
.Entering() || event
.Leaving() )
5088 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5092 // ------------ Left button pressed
5094 else if ( event
.LeftDown() )
5096 // don't send a label click event for a hit on the
5097 // edge of the col label - this is probably the user
5098 // wanting to resize the col
5100 if ( XToEdgeOfCol(x
) < 0 )
5104 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5106 if ( !event
.ShiftDown() && !event
.ControlDown() )
5110 if ( event
.ShiftDown() )
5112 m_selection
->SelectBlock( 0,
5113 m_currentCellCoords
.GetCol(),
5114 GetNumberRows() - 1, col
,
5115 event
.ControlDown(),
5122 m_selection
->SelectCol( col
,
5123 event
.ControlDown(),
5130 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5135 // starting to drag-resize a col
5137 if ( CanDragColSize() )
5138 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5143 // ------------ Left double click
5145 if ( event
.LeftDClick() )
5147 int col
= XToEdgeOfCol(x
);
5152 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5154 // no default action at the moment
5159 // adjust column width depending on label text
5160 AutoSizeColLabelSize( col
);
5162 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5168 // ------------ Left button released
5170 else if ( event
.LeftUp() )
5172 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5174 DoEndDragResizeCol();
5176 // Note: we are ending the event *after* doing
5177 // default processing in this case
5179 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5182 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5187 // ------------ Right button down
5189 else if ( event
.RightDown() )
5193 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5195 // no default action at the moment
5200 // ------------ Right double click
5202 else if ( event
.RightDClick() )
5206 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5208 // no default action at the moment
5213 // ------------ No buttons down and mouse moving
5215 else if ( event
.Moving() )
5217 m_dragRowOrCol
= XToEdgeOfCol( x
);
5218 if ( m_dragRowOrCol
>= 0 )
5220 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5222 // don't capture the cursor yet
5223 if ( CanDragColSize() )
5224 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5227 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5229 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5235 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5237 if ( event
.LeftDown() )
5239 // indicate corner label by having both row and
5242 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5248 else if ( event
.LeftDClick() )
5250 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5253 else if ( event
.RightDown() )
5255 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5257 // no default action at the moment
5261 else if ( event
.RightDClick() )
5263 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5265 // no default action at the moment
5270 void wxGrid::ChangeCursorMode(CursorMode mode
,
5275 static const wxChar
*cursorModes
[] =
5284 wxLogTrace(_T("grid"),
5285 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5286 win
== m_colLabelWin
? _T("colLabelWin")
5287 : win
? _T("rowLabelWin")
5289 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5290 #endif // __WXDEBUG__
5292 if ( mode
== m_cursorMode
&&
5293 win
== m_winCapture
&&
5294 captureMouse
== (m_winCapture
!= NULL
))
5299 // by default use the grid itself
5305 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5306 m_winCapture
= (wxWindow
*)NULL
;
5309 m_cursorMode
= mode
;
5311 switch ( m_cursorMode
)
5313 case WXGRID_CURSOR_RESIZE_ROW
:
5314 win
->SetCursor( m_rowResizeCursor
);
5317 case WXGRID_CURSOR_RESIZE_COL
:
5318 win
->SetCursor( m_colResizeCursor
);
5322 win
->SetCursor( *wxSTANDARD_CURSOR
);
5325 // we need to capture mouse when resizing
5326 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5327 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5329 if ( captureMouse
&& resize
)
5331 win
->CaptureMouse();
5336 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5339 wxPoint
pos( event
.GetPosition() );
5340 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5342 wxGridCellCoords coords
;
5343 XYToCell( x
, y
, coords
);
5345 int cell_rows
, cell_cols
;
5346 bool isFirstDrag
= !m_isDragging
;
5347 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5348 if ((cell_rows
< 0) || (cell_cols
< 0))
5350 coords
.SetRow(coords
.GetRow() + cell_rows
);
5351 coords
.SetCol(coords
.GetCol() + cell_cols
);
5354 if ( event
.Dragging() )
5356 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5358 // Don't start doing anything until the mouse has been drug at
5359 // least 3 pixels in any direction...
5362 if (m_startDragPos
== wxDefaultPosition
)
5364 m_startDragPos
= pos
;
5367 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5371 m_isDragging
= true;
5372 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5374 // Hide the edit control, so it
5375 // won't interfer with drag-shrinking.
5376 if ( IsCellEditControlShown() )
5378 HideCellEditControl();
5379 SaveEditControlValue();
5382 // Have we captured the mouse yet?
5385 m_winCapture
= m_gridWin
;
5386 m_winCapture
->CaptureMouse();
5389 if ( coords
!= wxGridNoCellCoords
)
5391 if ( event
.ControlDown() )
5393 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5394 m_selectingKeyboard
= coords
;
5395 HighlightBlock ( m_selectingKeyboard
, coords
);
5397 else if ( CanDragCell() )
5401 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5402 m_selectingKeyboard
= coords
;
5404 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5412 if ( !IsSelection() )
5414 HighlightBlock( coords
, coords
);
5418 HighlightBlock( m_currentCellCoords
, coords
);
5422 if (! IsVisible(coords
))
5424 MakeCellVisible(coords
);
5425 // TODO: need to introduce a delay or something here. The
5426 // scrolling is way to fast, at least on MSW - also on GTK.
5430 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5432 int cw
, ch
, left
, dummy
;
5433 m_gridWin
->GetClientSize( &cw
, &ch
);
5434 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5436 wxClientDC
dc( m_gridWin
);
5438 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5439 GetRowMinimalHeight(m_dragRowOrCol
) );
5440 dc
.SetLogicalFunction(wxINVERT
);
5441 if ( m_dragLastPos
>= 0 )
5443 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5445 dc
.DrawLine( left
, y
, left
+cw
, y
);
5448 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5450 int cw
, ch
, dummy
, top
;
5451 m_gridWin
->GetClientSize( &cw
, &ch
);
5452 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5454 wxClientDC
dc( m_gridWin
);
5456 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5457 GetColMinimalWidth(m_dragRowOrCol
) );
5458 dc
.SetLogicalFunction(wxINVERT
);
5459 if ( m_dragLastPos
>= 0 )
5461 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5463 dc
.DrawLine( x
, top
, x
, top
+ch
);
5470 m_isDragging
= false;
5471 m_startDragPos
= wxDefaultPosition
;
5473 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5474 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5477 if ( event
.Entering() || event
.Leaving() )
5479 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5480 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5485 // ------------ Left button pressed
5487 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5489 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5494 if ( !event
.ControlDown() )
5496 if ( event
.ShiftDown() )
5500 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5501 m_currentCellCoords
.GetCol(),
5504 event
.ControlDown(),
5510 else if ( XToEdgeOfCol(x
) < 0 &&
5511 YToEdgeOfRow(y
) < 0 )
5513 DisableCellEditControl();
5514 MakeCellVisible( coords
);
5516 if ( event
.ControlDown() )
5520 m_selection
->ToggleCellSelection( coords
.GetRow(),
5522 event
.ControlDown(),
5527 m_selectingTopLeft
= wxGridNoCellCoords
;
5528 m_selectingBottomRight
= wxGridNoCellCoords
;
5529 m_selectingKeyboard
= coords
;
5533 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5534 SetCurrentCell( coords
);
5537 if ( m_selection
->GetSelectionMode() !=
5538 wxGrid::wxGridSelectCells
)
5540 HighlightBlock( coords
, coords
);
5549 // ------------ Left double click
5551 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5553 DisableCellEditControl();
5555 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5557 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5562 // we want double click to select a cell and start editing
5563 // (i.e. to behave in same way as sequence of two slow clicks):
5564 m_waitForSlowClick
= true;
5571 // ------------ Left button released
5573 else if ( event
.LeftUp() )
5575 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5579 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5580 m_winCapture
= NULL
;
5583 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5586 EnableCellEditControl();
5588 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5589 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5590 editor
->StartingClick();
5594 m_waitForSlowClick
= false;
5596 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5597 m_selectingBottomRight
!= wxGridNoCellCoords
)
5601 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5602 m_selectingTopLeft
.GetCol(),
5603 m_selectingBottomRight
.GetRow(),
5604 m_selectingBottomRight
.GetCol(),
5605 event
.ControlDown(),
5611 m_selectingTopLeft
= wxGridNoCellCoords
;
5612 m_selectingBottomRight
= wxGridNoCellCoords
;
5614 // Show the edit control, if it has been hidden for
5616 ShowCellEditControl();
5619 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5621 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5622 DoEndDragResizeRow();
5624 // Note: we are ending the event *after* doing
5625 // default processing in this case
5627 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5629 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5631 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5632 DoEndDragResizeCol();
5634 // Note: we are ending the event *after* doing
5635 // default processing in this case
5637 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5644 // ------------ Right button down
5646 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5648 DisableCellEditControl();
5649 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5654 // no default action at the moment
5659 // ------------ Right double click
5661 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5663 DisableCellEditControl();
5664 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5669 // no default action at the moment
5673 // ------------ Moving and no button action
5675 else if ( event
.Moving() && !event
.IsButton() )
5677 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5679 // out of grid cell area
5680 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5684 int dragRow
= YToEdgeOfRow( y
);
5685 int dragCol
= XToEdgeOfCol( x
);
5687 // Dragging on the corner of a cell to resize in both
5688 // directions is not implemented yet...
5690 if ( dragRow
>= 0 && dragCol
>= 0 )
5692 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5698 m_dragRowOrCol
= dragRow
;
5700 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5702 if ( CanDragRowSize() && CanDragGridSize() )
5703 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5708 m_dragRowOrCol
= dragCol
;
5716 m_dragRowOrCol
= dragCol
;
5718 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5720 if ( CanDragColSize() && CanDragGridSize() )
5721 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5727 // Neither on a row or col edge
5729 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5731 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5737 void wxGrid::DoEndDragResizeRow()
5739 if ( m_dragLastPos
>= 0 )
5741 // erase the last line and resize the row
5743 int cw
, ch
, left
, dummy
;
5744 m_gridWin
->GetClientSize( &cw
, &ch
);
5745 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5747 wxClientDC
dc( m_gridWin
);
5749 dc
.SetLogicalFunction( wxINVERT
);
5750 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5751 HideCellEditControl();
5752 SaveEditControlValue();
5754 int rowTop
= GetRowTop(m_dragRowOrCol
);
5755 SetRowSize( m_dragRowOrCol
,
5756 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5758 if ( !GetBatchCount() )
5760 // Only needed to get the correct rect.y:
5761 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5763 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5764 rect
.width
= m_rowLabelWidth
;
5765 rect
.height
= ch
- rect
.y
;
5766 m_rowLabelWin
->Refresh( true, &rect
);
5768 // if there is a multicell block, paint all of it
5771 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5772 int leftCol
= XToCol(left
);
5773 int rightCol
= internalXToCol(left
+cw
);
5776 for (i
=leftCol
; i
<rightCol
; i
++)
5778 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5779 if (cell_rows
< subtract_rows
)
5780 subtract_rows
= cell_rows
;
5782 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5783 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5784 rect
.height
= ch
- rect
.y
;
5787 m_gridWin
->Refresh( false, &rect
);
5790 ShowCellEditControl();
5795 void wxGrid::DoEndDragResizeCol()
5797 if ( m_dragLastPos
>= 0 )
5799 // erase the last line and resize the col
5801 int cw
, ch
, dummy
, top
;
5802 m_gridWin
->GetClientSize( &cw
, &ch
);
5803 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5805 wxClientDC
dc( m_gridWin
);
5807 dc
.SetLogicalFunction( wxINVERT
);
5808 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5809 HideCellEditControl();
5810 SaveEditControlValue();
5812 int colLeft
= GetColLeft(m_dragRowOrCol
);
5813 SetColSize( m_dragRowOrCol
,
5814 wxMax( m_dragLastPos
- colLeft
,
5815 GetColMinimalWidth(m_dragRowOrCol
) ) );
5817 if ( !GetBatchCount() )
5819 // Only needed to get the correct rect.x:
5820 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5822 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5823 rect
.width
= cw
- rect
.x
;
5824 rect
.height
= m_colLabelHeight
;
5825 m_colLabelWin
->Refresh( true, &rect
);
5827 // if there is a multicell block, paint all of it
5830 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5831 int topRow
= YToRow(top
);
5832 int bottomRow
= internalYToRow(top
+cw
);
5835 for (i
=topRow
; i
<bottomRow
; i
++)
5837 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5838 if (cell_cols
< subtract_cols
)
5839 subtract_cols
= cell_cols
;
5841 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5842 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5843 rect
.width
= cw
- rect
.x
;
5846 m_gridWin
->Refresh( false, &rect
);
5849 ShowCellEditControl();
5856 // ------ interaction with data model
5858 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5860 switch ( msg
.GetId() )
5862 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5863 return GetModelValues();
5865 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5866 return SetModelValues();
5868 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5869 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5870 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5871 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5872 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5873 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5874 return Redimension( msg
);
5883 // The behaviour of this function depends on the grid table class
5884 // Clear() function. For the default wxGridStringTable class the
5885 // behavious is to replace all cell contents with wxEmptyString but
5886 // not to change the number of rows or cols.
5888 void wxGrid::ClearGrid()
5892 if (IsCellEditControlEnabled())
5893 DisableCellEditControl();
5896 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5901 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5903 // TODO: something with updateLabels flag
5907 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5913 if (IsCellEditControlEnabled())
5914 DisableCellEditControl();
5916 bool done
= m_table
->InsertRows( pos
, numRows
);
5919 // the table will have sent the results of the insert row
5920 // operation to this view object as a grid table message
5926 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5928 // TODO: something with updateLabels flag
5932 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5938 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5940 // the table will have sent the results of the append row
5941 // operation to this view object as a grid table message
5947 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5949 // TODO: something with updateLabels flag
5953 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5959 if (IsCellEditControlEnabled())
5960 DisableCellEditControl();
5962 bool done
= m_table
->DeleteRows( pos
, numRows
);
5964 // the table will have sent the results of the delete row
5965 // operation to this view object as a grid table message
5971 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5973 // TODO: something with updateLabels flag
5977 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5983 if (IsCellEditControlEnabled())
5984 DisableCellEditControl();
5986 bool done
= m_table
->InsertCols( pos
, numCols
);
5988 // the table will have sent the results of the insert col
5989 // operation to this view object as a grid table message
5995 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
5997 // TODO: something with updateLabels flag
6001 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6007 bool done
= m_table
->AppendCols( numCols
);
6009 // the table will have sent the results of the append col
6010 // operation to this view object as a grid table message
6016 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6018 // TODO: something with updateLabels flag
6022 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6028 if (IsCellEditControlEnabled())
6029 DisableCellEditControl();
6031 bool done
= m_table
->DeleteCols( pos
, numCols
);
6033 // the table will have sent the results of the delete col
6034 // operation to this view object as a grid table message
6042 // ----- event handlers
6045 // Generate a grid event based on a mouse event and
6046 // return the result of ProcessEvent()
6048 int wxGrid::SendEvent( const wxEventType type
,
6050 wxMouseEvent
& mouseEv
)
6055 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6057 int rowOrCol
= (row
== -1 ? col
: row
);
6059 wxGridSizeEvent
gridEvt( GetId(),
6063 mouseEv
.GetX() + GetRowLabelSize(),
6064 mouseEv
.GetY() + GetColLabelSize(),
6065 mouseEv
.ControlDown(),
6066 mouseEv
.ShiftDown(),
6068 mouseEv
.MetaDown() );
6070 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6071 vetoed
= !gridEvt
.IsAllowed();
6073 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6075 // Right now, it should _never_ end up here!
6076 wxGridRangeSelectEvent
gridEvt( GetId(),
6080 m_selectingBottomRight
,
6082 mouseEv
.ControlDown(),
6083 mouseEv
.ShiftDown(),
6085 mouseEv
.MetaDown() );
6087 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6088 vetoed
= !gridEvt
.IsAllowed();
6092 wxGridEvent
gridEvt( GetId(),
6096 mouseEv
.GetX() + GetRowLabelSize(),
6097 mouseEv
.GetY() + GetColLabelSize(),
6099 mouseEv
.ControlDown(),
6100 mouseEv
.ShiftDown(),
6102 mouseEv
.MetaDown() );
6103 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6104 vetoed
= !gridEvt
.IsAllowed();
6107 // A Veto'd event may not be `claimed' so test this first
6108 if (vetoed
) return -1;
6109 return claimed
? 1 : 0;
6113 // Generate a grid event of specified type and return the result
6114 // of ProcessEvent().
6116 int wxGrid::SendEvent( const wxEventType type
,
6122 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6124 int rowOrCol
= (row
== -1 ? col
: row
);
6126 wxGridSizeEvent
gridEvt( GetId(),
6131 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6132 vetoed
= !gridEvt
.IsAllowed();
6136 wxGridEvent
gridEvt( GetId(),
6141 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6142 vetoed
= !gridEvt
.IsAllowed();
6145 // A Veto'd event may not be `claimed' so test this first
6146 if (vetoed
) return -1;
6147 return claimed
? 1 : 0;
6151 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6153 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6156 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6158 // Don't do anything if between Begin/EndBatch...
6159 // EndBatch() will do all this on the last nested one anyway.
6160 if (! GetBatchCount())
6162 // Refresh to get correct scrolled position:
6163 wxScrolledWindow::Refresh(eraseb
,rect
);
6167 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6168 int width_label
, width_cell
, height_label
, height_cell
;
6171 //Copy rectangle can get scroll offsets..
6172 rect_x
= rect
->GetX();
6173 rect_y
= rect
->GetY();
6174 rectWidth
= rect
->GetWidth();
6175 rectHeight
= rect
->GetHeight();
6177 width_label
= m_rowLabelWidth
- rect_x
;
6178 if (width_label
> rectWidth
) width_label
= rectWidth
;
6180 height_label
= m_colLabelHeight
- rect_y
;
6181 if (height_label
> rectHeight
) height_label
= rectHeight
;
6183 if (rect_x
> m_rowLabelWidth
)
6185 x
= rect_x
- m_rowLabelWidth
;
6186 width_cell
= rectWidth
;
6191 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6194 if (rect_y
> m_colLabelHeight
)
6196 y
= rect_y
- m_colLabelHeight
;
6197 height_cell
= rectHeight
;
6202 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6205 // Paint corner label part intersecting rect.
6206 if ( width_label
> 0 && height_label
> 0 )
6208 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6209 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6212 // Paint col labels part intersecting rect.
6213 if ( width_cell
> 0 && height_label
> 0 )
6215 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6216 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6219 // Paint row labels part intersecting rect.
6220 if ( width_label
> 0 && height_cell
> 0 )
6222 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6223 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6226 // Paint cell area part intersecting rect.
6227 if ( width_cell
> 0 && height_cell
> 0 )
6229 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6230 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6235 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6236 m_colLabelWin
->Refresh(eraseb
, NULL
);
6237 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6238 m_gridWin
->Refresh(eraseb
, NULL
);
6243 void wxGrid::OnSize( wxSizeEvent
& event
)
6245 // position the child windows
6248 // don't call CalcDimensions() from here, the base class handles the size
6254 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6256 if ( m_inOnKeyDown
)
6258 // shouldn't be here - we are going round in circles...
6260 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6263 m_inOnKeyDown
= true;
6265 // propagate the event up and see if it gets processed
6267 wxWindow
*parent
= GetParent();
6268 wxKeyEvent
keyEvt( event
);
6269 keyEvt
.SetEventObject( parent
);
6271 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6274 // try local handlers
6276 switch ( event
.GetKeyCode() )
6279 if ( event
.ControlDown() )
6281 MoveCursorUpBlock( event
.ShiftDown() );
6285 MoveCursorUp( event
.ShiftDown() );
6290 if ( event
.ControlDown() )
6292 MoveCursorDownBlock( event
.ShiftDown() );
6296 MoveCursorDown( event
.ShiftDown() );
6301 if ( event
.ControlDown() )
6303 MoveCursorLeftBlock( event
.ShiftDown() );
6307 MoveCursorLeft( event
.ShiftDown() );
6312 if ( event
.ControlDown() )
6314 MoveCursorRightBlock( event
.ShiftDown() );
6318 MoveCursorRight( event
.ShiftDown() );
6323 case WXK_NUMPAD_ENTER
:
6324 if ( event
.ControlDown() )
6326 event
.Skip(); // to let the edit control have the return
6330 if ( GetGridCursorRow() < GetNumberRows()-1 )
6332 MoveCursorDown( event
.ShiftDown() );
6336 // at the bottom of a column
6337 DisableCellEditControl();
6347 if (event
.ShiftDown())
6349 if ( GetGridCursorCol() > 0 )
6351 MoveCursorLeft( false );
6356 DisableCellEditControl();
6361 if ( GetGridCursorCol() < GetNumberCols()-1 )
6363 MoveCursorRight( false );
6368 DisableCellEditControl();
6374 if ( event
.ControlDown() )
6376 MakeCellVisible( 0, 0 );
6377 SetCurrentCell( 0, 0 );
6386 if ( event
.ControlDown() )
6388 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6389 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6406 if ( event
.ControlDown() )
6410 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6411 m_currentCellCoords
.GetCol(),
6412 event
.ControlDown(),
6419 if ( !IsEditable() )
6421 MoveCursorRight( false );
6424 // Otherwise fall through to default
6427 // is it possible to edit the current cell at all?
6428 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6430 // yes, now check whether the cells editor accepts the key
6431 int row
= m_currentCellCoords
.GetRow();
6432 int col
= m_currentCellCoords
.GetCol();
6433 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6434 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6436 // <F2> is special and will always start editing, for
6437 // other keys - ask the editor itself
6438 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6439 || editor
->IsAcceptedKey(event
) )
6441 // ensure cell is visble
6442 MakeCellVisible(row
, col
);
6443 EnableCellEditControl();
6445 // a problem can arise if the cell is not completely
6446 // visible (even after calling MakeCellVisible the
6447 // control is not created and calling StartingKey will
6449 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6461 // let others process char events with modifiers or all
6462 // char events for readonly cells
6469 m_inOnKeyDown
= false;
6472 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6474 // try local handlers
6476 if ( event
.GetKeyCode() == WXK_SHIFT
)
6478 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6479 m_selectingBottomRight
!= wxGridNoCellCoords
)
6483 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6484 m_selectingTopLeft
.GetCol(),
6485 m_selectingBottomRight
.GetRow(),
6486 m_selectingBottomRight
.GetCol(),
6487 event
.ControlDown(),
6494 m_selectingTopLeft
= wxGridNoCellCoords
;
6495 m_selectingBottomRight
= wxGridNoCellCoords
;
6496 m_selectingKeyboard
= wxGridNoCellCoords
;
6500 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6504 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6506 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6508 // the event has been intercepted - do nothing
6512 wxClientDC
dc(m_gridWin
);
6515 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6517 DisableCellEditControl();
6519 if ( IsVisible( m_currentCellCoords
, false ) )
6522 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6523 if ( !m_gridLinesEnabled
)
6531 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6533 // Otherwise refresh redraws the highlight!
6534 m_currentCellCoords
= coords
;
6536 DrawGridCellArea(dc
,cells
);
6537 DrawAllGridLines( dc
, r
);
6541 m_currentCellCoords
= coords
;
6543 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6544 DrawCellHighlight(dc
, attr
);
6549 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6552 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6556 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6559 rightCol
= GetNumberCols() - 1;
6561 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6564 bottomRow
= GetNumberRows() - 1;
6568 if ( topRow
> bottomRow
)
6575 if ( leftCol
> rightCol
)
6582 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6583 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6585 // First the case that we selected a completely new area
6586 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6587 m_selectingBottomRight
== wxGridNoCellCoords
)
6590 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6591 wxGridCellCoords ( bottomRow
, rightCol
) );
6592 m_gridWin
->Refresh( false, &rect
);
6594 // Now handle changing an existing selection area.
6595 else if ( m_selectingTopLeft
!= updateTopLeft
||
6596 m_selectingBottomRight
!= updateBottomRight
)
6598 // Compute two optimal update rectangles:
6599 // Either one rectangle is a real subset of the
6600 // other, or they are (almost) disjoint!
6602 bool need_refresh
[4];
6606 need_refresh
[3] = false;
6609 // Store intermediate values
6610 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6611 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6612 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6613 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6615 // Determine the outer/inner coordinates.
6616 if (oldLeft
> leftCol
)
6622 if (oldTop
> topRow
)
6628 if (oldRight
< rightCol
)
6631 oldRight
= rightCol
;
6634 if (oldBottom
< bottomRow
)
6637 oldBottom
= bottomRow
;
6641 // Now, either the stuff marked old is the outer
6642 // rectangle or we don't have a situation where one
6643 // is contained in the other.
6645 if ( oldLeft
< leftCol
)
6647 // Refresh the newly selected or deselected
6648 // area to the left of the old or new selection.
6649 need_refresh
[0] = true;
6650 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6652 wxGridCellCoords ( oldBottom
,
6656 if ( oldTop
< topRow
)
6658 // Refresh the newly selected or deselected
6659 // area above the old or new selection.
6660 need_refresh
[1] = true;
6661 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6663 wxGridCellCoords ( topRow
- 1,
6667 if ( oldRight
> rightCol
)
6669 // Refresh the newly selected or deselected
6670 // area to the right of the old or new selection.
6671 need_refresh
[2] = true;
6672 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6674 wxGridCellCoords ( oldBottom
,
6678 if ( oldBottom
> bottomRow
)
6680 // Refresh the newly selected or deselected
6681 // area below the old or new selection.
6682 need_refresh
[3] = true;
6683 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6685 wxGridCellCoords ( oldBottom
,
6689 // various Refresh() calls
6690 for (i
= 0; i
< 4; i
++ )
6691 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6692 m_gridWin
->Refresh( false, &(rect
[i
]) );
6695 m_selectingTopLeft
= updateTopLeft
;
6696 m_selectingBottomRight
= updateBottomRight
;
6700 // ------ functions to get/send data (see also public functions)
6703 bool wxGrid::GetModelValues()
6705 // Hide the editor, so it won't hide a changed value.
6706 HideCellEditControl();
6710 // all we need to do is repaint the grid
6712 m_gridWin
->Refresh();
6720 bool wxGrid::SetModelValues()
6724 // Disable the editor, so it won't hide a changed value.
6725 // Do we also want to save the current value of the editor first?
6727 DisableCellEditControl();
6731 for ( row
= 0; row
< m_numRows
; row
++ )
6733 for ( col
= 0; col
< m_numCols
; col
++ )
6735 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6747 // Note - this function only draws cells that are in the list of
6748 // exposed cells (usually set from the update region by
6749 // CalcExposedCells)
6751 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6753 if ( !m_numRows
|| !m_numCols
) return;
6755 int i
, numCells
= cells
.GetCount();
6756 int row
, col
, cell_rows
, cell_cols
;
6757 wxGridCellCoordsArray redrawCells
;
6759 for ( i
= numCells
-1; i
>= 0; i
-- )
6761 row
= cells
[i
].GetRow();
6762 col
= cells
[i
].GetCol();
6763 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6765 // If this cell is part of a multicell block, find owner for repaint
6766 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6768 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6769 bool marked
= false;
6770 for ( int j
= 0; j
< numCells
; j
++ )
6772 if ( cell
== cells
[j
] )
6780 int count
= redrawCells
.GetCount();
6781 for (int j
= 0; j
< count
; j
++)
6783 if ( cell
== redrawCells
[j
] )
6789 if (!marked
) redrawCells
.Add( cell
);
6791 continue; // don't bother drawing this cell
6794 // If this cell is empty, find cell to left that might want to overflow
6795 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6797 for ( int l
= 0; l
< cell_rows
; l
++ )
6799 // find a cell in this row to left alreay marked for repaint
6801 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6802 if ((redrawCells
[k
].GetCol() < left
) &&
6803 (redrawCells
[k
].GetRow() == row
))
6804 left
=redrawCells
[k
].GetCol();
6806 if (left
== col
) left
= 0; // oh well
6808 for (int j
= col
-1; j
>= left
; j
--)
6810 if (!m_table
->IsEmptyCell(row
+l
, j
))
6812 if (GetCellOverflow(row
+l
, j
))
6814 wxGridCellCoords
cell(row
+l
, j
);
6815 bool marked
= false;
6817 for (int k
= 0; k
< numCells
; k
++)
6819 if ( cell
== cells
[k
] )
6827 int count
= redrawCells
.GetCount();
6828 for (int k
= 0; k
< count
; k
++)
6830 if ( cell
== redrawCells
[k
] )
6836 if (!marked
) redrawCells
.Add( cell
);
6844 DrawCell( dc
, cells
[i
] );
6847 numCells
= redrawCells
.GetCount();
6849 for ( i
= numCells
- 1; i
>= 0; i
-- )
6851 DrawCell( dc
, redrawCells
[i
] );
6856 void wxGrid::DrawGridSpace( wxDC
& dc
)
6859 m_gridWin
->GetClientSize( &cw
, &ch
);
6862 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6864 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6865 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6867 if ( right
> rightCol
|| bottom
> bottomRow
)
6870 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6872 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6873 dc
.SetPen( *wxTRANSPARENT_PEN
);
6875 if ( right
> rightCol
)
6877 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6880 if ( bottom
> bottomRow
)
6882 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6888 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6890 int row
= coords
.GetRow();
6891 int col
= coords
.GetCol();
6893 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6896 // we draw the cell border ourselves
6897 #if !WXGRID_DRAW_LINES
6898 if ( m_gridLinesEnabled
)
6899 DrawCellBorder( dc
, coords
);
6902 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6904 bool isCurrent
= coords
== m_currentCellCoords
;
6906 wxRect rect
= CellToRect( row
, col
);
6908 // if the editor is shown, we should use it and not the renderer
6909 // Note: However, only if it is really _shown_, i.e. not hidden!
6910 if ( isCurrent
&& IsCellEditControlShown() )
6912 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6913 editor
->PaintBackground(rect
, attr
);
6918 // but all the rest is drawn by the cell renderer and hence may be
6920 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6921 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6928 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6930 int row
= m_currentCellCoords
.GetRow();
6931 int col
= m_currentCellCoords
.GetCol();
6933 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6936 wxRect rect
= CellToRect(row
, col
);
6938 // hmmm... what could we do here to show that the cell is disabled?
6939 // for now, I just draw a thinner border than for the other ones, but
6940 // it doesn't look really good
6942 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6946 // The center of th drawn line is where the position/width/height of
6947 // the rectangle is actually at, (on wxMSW atr least,) so we will
6948 // reduce the size of the rectangle to compensate for the thickness of
6949 // the line. If this is too strange on non wxMSW platforms then
6950 // please #ifdef this appropriately.
6951 rect
.x
+= penWidth
/2;
6952 rect
.y
+= penWidth
/2;
6953 rect
.width
-= penWidth
-1;
6954 rect
.height
-= penWidth
-1;
6957 // Now draw the rectangle
6958 // use the cellHighlightColour if the cell is inside a selection, this
6959 // will ensure the cell is always visible.
6960 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6961 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6962 dc
.DrawRectangle(rect
);
6966 // VZ: my experiments with 3d borders...
6968 // how to properly set colours for arbitrary bg?
6969 wxCoord x1
= rect
.x
,
6971 x2
= rect
.x
+ rect
.width
-1,
6972 y2
= rect
.y
+ rect
.height
-1;
6974 dc
.SetPen(*wxWHITE_PEN
);
6975 dc
.DrawLine(x1
, y1
, x2
, y1
);
6976 dc
.DrawLine(x1
, y1
, x1
, y2
);
6978 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6979 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6981 dc
.SetPen(*wxBLACK_PEN
);
6982 dc
.DrawLine(x1
, y2
, x2
, y2
);
6983 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6988 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
6990 int row
= coords
.GetRow();
6991 int col
= coords
.GetCol();
6992 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6995 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
6997 wxRect rect
= CellToRect( row
, col
);
6999 // right hand border
7001 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7002 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7006 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7007 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7010 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7012 // This if block was previously in wxGrid::OnPaint but that doesn't
7013 // seem to get called under wxGTK - MB
7015 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7016 m_numRows
&& m_numCols
)
7018 m_currentCellCoords
.Set(0, 0);
7021 if ( IsCellEditControlShown() )
7023 // don't show highlight when the edit control is shown
7027 // if the active cell was repainted, repaint its highlight too because it
7028 // might have been damaged by the grid lines
7029 size_t count
= cells
.GetCount();
7030 for ( size_t n
= 0; n
< count
; n
++ )
7032 if ( cells
[n
] == m_currentCellCoords
)
7034 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7035 DrawCellHighlight(dc
, attr
);
7043 // TODO: remove this ???
7044 // This is used to redraw all grid lines e.g. when the grid line colour
7047 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7049 #if !WXGRID_DRAW_LINES
7053 if ( !m_gridLinesEnabled
||
7055 !m_numCols
) return;
7057 int top
, bottom
, left
, right
;
7059 #if 0 //#ifndef __WXGTK__
7063 m_gridWin
->GetClientSize(&cw
, &ch
);
7065 // virtual coords of visible area
7067 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7068 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7073 reg
.GetBox(x
, y
, w
, h
);
7074 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7075 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7079 m_gridWin
->GetClientSize(&cw
, &ch
);
7080 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7081 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7084 // avoid drawing grid lines past the last row and col
7086 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7087 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7089 // no gridlines inside multicells, clip them out
7090 int leftCol
= internalXToCol(left
);
7091 int topRow
= internalYToRow(top
);
7092 int rightCol
= internalXToCol(right
);
7093 int bottomRow
= internalYToRow(bottom
);
7094 wxRegion
clippedcells(0, 0, cw
, ch
);
7097 int i
, j
, cell_rows
, cell_cols
;
7100 for (j
=topRow
; j
<bottomRow
; j
++)
7102 for (i
=leftCol
; i
<rightCol
; i
++)
7104 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7105 if ((cell_rows
> 1) || (cell_cols
> 1))
7107 rect
= CellToRect(j
,i
);
7108 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7109 clippedcells
.Subtract(rect
);
7111 else if ((cell_rows
< 0) || (cell_cols
< 0))
7113 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7114 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7115 clippedcells
.Subtract(rect
);
7119 dc
.SetClippingRegion( clippedcells
);
7121 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7123 // horizontal grid lines
7125 // already declared above - int i;
7126 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7128 int bot
= GetRowBottom(i
) - 1;
7137 dc
.DrawLine( left
, bot
, right
, bot
);
7142 // vertical grid lines
7144 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7146 int colRight
= GetColRight(i
) - 1;
7147 if ( colRight
> right
)
7152 if ( colRight
>= left
)
7154 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7157 dc
.DestroyClippingRegion();
7161 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7163 if ( !m_numRows
) return;
7166 size_t numLabels
= rows
.GetCount();
7168 for ( i
= 0; i
< numLabels
; i
++ )
7170 DrawRowLabel( dc
, rows
[i
] );
7175 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7177 if ( GetRowHeight(row
) <= 0 )
7180 int rowTop
= GetRowTop(row
),
7181 rowBottom
= GetRowBottom(row
) - 1;
7183 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7184 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7185 m_rowLabelWidth
-1, rowBottom
);
7187 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7189 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7191 dc
.SetPen( *wxWHITE_PEN
);
7192 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7193 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7195 dc
.SetBackgroundMode( wxTRANSPARENT
);
7196 dc
.SetTextForeground( GetLabelTextColour() );
7197 dc
.SetFont( GetLabelFont() );
7200 GetRowLabelAlignment( &hAlign
, &vAlign
);
7204 rect
.SetY( GetRowTop(row
) + 2 );
7205 rect
.SetWidth( m_rowLabelWidth
- 4 );
7206 rect
.SetHeight( GetRowHeight(row
) - 4 );
7207 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7211 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7213 if ( !m_numCols
) return;
7216 size_t numLabels
= cols
.GetCount();
7218 for ( i
= 0; i
< numLabels
; i
++ )
7220 DrawColLabel( dc
, cols
[i
] );
7225 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7227 if ( GetColWidth(col
) <= 0 )
7230 int colLeft
= GetColLeft(col
),
7231 colRight
= GetColRight(col
) - 1;
7233 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7234 dc
.DrawLine( colRight
, 0,
7235 colRight
, m_colLabelHeight
-1 );
7237 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7239 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7240 colRight
+1, m_colLabelHeight
-1 );
7242 dc
.SetPen( *wxWHITE_PEN
);
7243 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7244 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7246 dc
.SetBackgroundMode( wxTRANSPARENT
);
7247 dc
.SetTextForeground( GetLabelTextColour() );
7248 dc
.SetFont( GetLabelFont() );
7250 int hAlign
, vAlign
, orient
;
7251 GetColLabelAlignment( &hAlign
, &vAlign
);
7252 orient
= GetColLabelTextOrientation();
7255 rect
.SetX( colLeft
+ 2 );
7257 rect
.SetWidth( GetColWidth(col
) - 4 );
7258 rect
.SetHeight( m_colLabelHeight
- 4 );
7259 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7262 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7263 const wxString
& value
,
7267 int textOrientation
)
7269 wxArrayString lines
;
7271 StringToLines( value
, lines
);
7274 //Forward to new API.
7275 DrawTextRectangle( dc
,
7284 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7285 const wxArrayString
& lines
,
7289 int textOrientation
)
7291 long textWidth
, textHeight
;
7292 long lineWidth
, lineHeight
;
7295 dc
.SetClippingRegion( rect
);
7297 nLines
= lines
.GetCount();
7301 float x
= 0.0, y
= 0.0;
7303 if( textOrientation
== wxHORIZONTAL
)
7304 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7306 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7310 case wxALIGN_BOTTOM
:
7311 if( textOrientation
== wxHORIZONTAL
)
7312 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7314 x
= rect
.x
+ rect
.width
- textWidth
;
7317 case wxALIGN_CENTRE
:
7318 if( textOrientation
== wxHORIZONTAL
)
7319 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7321 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7326 if( textOrientation
== wxHORIZONTAL
)
7333 // Align each line of a multi-line label
7334 for( l
= 0; l
< nLines
; l
++ )
7336 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7338 switch( horizAlign
)
7341 if( textOrientation
== wxHORIZONTAL
)
7342 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7344 y
= rect
.y
+ lineWidth
+ 1;
7347 case wxALIGN_CENTRE
:
7348 if( textOrientation
== wxHORIZONTAL
)
7349 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7351 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7356 if( textOrientation
== wxHORIZONTAL
)
7359 y
= rect
.y
+ rect
.height
- 1;
7363 if( textOrientation
== wxHORIZONTAL
)
7365 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7370 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7375 dc
.DestroyClippingRegion();
7379 // Split multi line text up into an array of strings. Any existing
7380 // contents of the string array are preserved.
7382 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7386 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7387 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7389 while ( startPos
< (int)tVal
.Length() )
7391 pos
= tVal
.Mid(startPos
).Find( eol
);
7396 else if ( pos
== 0 )
7398 lines
.Add( wxEmptyString
);
7402 lines
.Add( value
.Mid(startPos
, pos
) );
7406 if ( startPos
< (int)value
.Length() )
7408 lines
.Add( value
.Mid( startPos
) );
7413 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7414 const wxArrayString
& lines
,
7415 long *width
, long *height
)
7422 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7424 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7425 w
= wxMax( w
, lineW
);
7434 // ------ Batch processing.
7436 void wxGrid::EndBatch()
7438 if ( m_batchCount
> 0 )
7441 if ( !m_batchCount
)
7444 m_rowLabelWin
->Refresh();
7445 m_colLabelWin
->Refresh();
7446 m_cornerLabelWin
->Refresh();
7447 m_gridWin
->Refresh();
7452 // Use this, rather than wxWindow::Refresh(), to force an immediate
7453 // repainting of the grid. Has no effect if you are already inside a
7454 // BeginBatch / EndBatch block.
7456 void wxGrid::ForceRefresh()
7464 // ------ Edit control functions
7468 void wxGrid::EnableEditing( bool edit
)
7470 // TODO: improve this ?
7472 if ( edit
!= m_editable
)
7474 if(!edit
) EnableCellEditControl(edit
);
7480 void wxGrid::EnableCellEditControl( bool enable
)
7485 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7486 SetCurrentCell( 0, 0 );
7488 if ( enable
!= m_cellEditCtrlEnabled
)
7492 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7495 // this should be checked by the caller!
7496 wxASSERT_MSG( CanEnableCellControl(),
7497 _T("can't enable editing for this cell!") );
7499 // do it before ShowCellEditControl()
7500 m_cellEditCtrlEnabled
= enable
;
7502 ShowCellEditControl();
7506 //FIXME:add veto support
7507 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7509 HideCellEditControl();
7510 SaveEditControlValue();
7512 // do it after HideCellEditControl()
7513 m_cellEditCtrlEnabled
= enable
;
7518 bool wxGrid::IsCurrentCellReadOnly() const
7521 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7522 bool readonly
= attr
->IsReadOnly();
7528 bool wxGrid::CanEnableCellControl() const
7530 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7531 !IsCurrentCellReadOnly();
7535 bool wxGrid::IsCellEditControlEnabled() const
7537 // the cell edit control might be disable for all cells or just for the
7538 // current one if it's read only
7539 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7542 bool wxGrid::IsCellEditControlShown() const
7544 bool isShown
= false;
7546 if ( m_cellEditCtrlEnabled
)
7548 int row
= m_currentCellCoords
.GetRow();
7549 int col
= m_currentCellCoords
.GetCol();
7550 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7551 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7556 if ( editor
->IsCreated() )
7558 isShown
= editor
->GetControl()->IsShown();
7568 void wxGrid::ShowCellEditControl()
7570 if ( IsCellEditControlEnabled() )
7572 if ( !IsVisible( m_currentCellCoords
) )
7574 m_cellEditCtrlEnabled
= false;
7579 wxRect rect
= CellToRect( m_currentCellCoords
);
7580 int row
= m_currentCellCoords
.GetRow();
7581 int col
= m_currentCellCoords
.GetCol();
7583 // if this is part of a multicell, find owner (topleft)
7584 int cell_rows
, cell_cols
;
7585 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7586 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7590 m_currentCellCoords
.SetRow( row
);
7591 m_currentCellCoords
.SetCol( col
);
7594 // convert to scrolled coords
7596 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7598 // done in PaintBackground()
7600 // erase the highlight and the cell contents because the editor
7601 // might not cover the entire cell
7602 wxClientDC
dc( m_gridWin
);
7604 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7605 dc
.SetPen(*wxTRANSPARENT_PEN
);
7606 dc
.DrawRectangle(rect
);
7609 // cell is shifted by one pixel
7610 // However, don't allow x or y to become negative
7611 // since the SetSize() method interprets that as
7618 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7619 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7620 if ( !editor
->IsCreated() )
7622 editor
->Create(m_gridWin
, wxID_ANY
,
7623 new wxGridCellEditorEvtHandler(this, editor
));
7625 wxGridEditorCreatedEvent
evt(GetId(),
7626 wxEVT_GRID_EDITOR_CREATED
,
7630 editor
->GetControl());
7631 GetEventHandler()->ProcessEvent(evt
);
7635 // resize editor to overflow into righthand cells if allowed
7636 int maxWidth
= rect
.width
;
7637 wxString value
= GetCellValue(row
, col
);
7638 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7641 GetTextExtent(value
, &maxWidth
, &y
,
7642 NULL
, NULL
, &attr
->GetFont());
7643 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7645 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7646 if (rect
.x
+maxWidth
> client_right
)
7647 maxWidth
= client_right
- rect
.x
;
7649 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7651 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7652 // may have changed earlier
7653 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7656 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7657 // looks weird going over a multicell
7658 if (m_table
->IsEmptyCell(row
,i
) &&
7659 (rect
.width
< maxWidth
) && (c_rows
== 1))
7660 rect
.width
+= GetColWidth(i
);
7664 if (rect
.GetRight() > client_right
)
7665 rect
.SetRight(client_right
-1);
7668 editor
->SetCellAttr(attr
);
7669 editor
->SetSize( rect
);
7670 editor
->Show( true, attr
);
7672 // recalc dimensions in case we need to
7673 // expand the scrolled window to account for editor
7676 editor
->BeginEdit(row
, col
, this);
7677 editor
->SetCellAttr(NULL
);
7686 void wxGrid::HideCellEditControl()
7688 if ( IsCellEditControlEnabled() )
7690 int row
= m_currentCellCoords
.GetRow();
7691 int col
= m_currentCellCoords
.GetCol();
7693 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7694 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7695 editor
->Show( false );
7698 m_gridWin
->SetFocus();
7699 // refresh whole row to the right
7700 wxRect
rect( CellToRect(row
, col
) );
7701 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7702 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7703 m_gridWin
->Refresh( false, &rect
);
7708 void wxGrid::SaveEditControlValue()
7710 if ( IsCellEditControlEnabled() )
7712 int row
= m_currentCellCoords
.GetRow();
7713 int col
= m_currentCellCoords
.GetCol();
7715 wxString oldval
= GetCellValue(row
,col
);
7717 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7718 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7719 bool changed
= editor
->EndEdit(row
, col
, this);
7726 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7727 m_currentCellCoords
.GetRow(),
7728 m_currentCellCoords
.GetCol() ) < 0 ) {
7730 // Event has been vetoed, set the data back.
7731 SetCellValue(row
,col
,oldval
);
7739 // ------ Grid location functions
7740 // Note that all of these functions work with the logical coordinates of
7741 // grid cells and labels so you will need to convert from device
7742 // coordinates for mouse events etc.
7745 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7747 int row
= YToRow(y
);
7748 int col
= XToCol(x
);
7750 if ( row
== -1 || col
== -1 )
7752 coords
= wxGridNoCellCoords
;
7756 coords
.Set( row
, col
);
7761 // Internal Helper function for computing row or column from some
7762 // (unscrolled) coordinate value, using either
7763 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7764 // of m_rowBottoms/m_ColRights to speed up the search!
7766 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7767 const wxArrayInt
& BorderArray
, int nMax
,
7772 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7778 size_t i_max
= coord
/ defaultDist
,
7781 if (BorderArray
.IsEmpty())
7783 if((int) i_max
< nMax
)
7785 return clipToMinMax
? nMax
- 1 : -1;
7788 if ( i_max
>= BorderArray
.GetCount())
7789 i_max
= BorderArray
.GetCount() - 1;
7792 if ( coord
>= BorderArray
[i_max
])
7796 i_max
= coord
/ minDist
;
7798 i_max
= BorderArray
.GetCount() - 1;
7800 if ( i_max
>= BorderArray
.GetCount())
7801 i_max
= BorderArray
.GetCount() - 1;
7803 if ( coord
>= BorderArray
[i_max
])
7804 return clipToMinMax
? (int)i_max
: -1;
7805 if ( coord
< BorderArray
[0] )
7808 while ( i_max
- i_min
> 0 )
7810 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7811 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7812 if (coord
>= BorderArray
[ i_max
- 1])
7816 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7817 if (coord
< BorderArray
[median
])
7825 int wxGrid::YToRow( int y
)
7827 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7828 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7832 int wxGrid::XToCol( int x
)
7834 return CoordToRowOrCol(x
, m_defaultColWidth
,
7835 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7839 // return the row number that that the y coord is near the edge of, or
7840 // -1 if not near an edge
7842 int wxGrid::YToEdgeOfRow( int y
)
7845 i
= internalYToRow(y
);
7847 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7849 // We know that we are in row i, test whether we are
7850 // close enough to lower or upper border, respectively.
7851 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7853 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7861 // return the col number that that the x coord is near the edge of, or
7862 // -1 if not near an edge
7864 int wxGrid::XToEdgeOfCol( int x
)
7867 i
= internalXToCol(x
);
7869 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7871 // We know that we are in column i, test whether we are
7872 // close enough to right or left border, respectively.
7873 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7875 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7883 wxRect
wxGrid::CellToRect( int row
, int col
)
7885 wxRect
rect( -1, -1, -1, -1 );
7887 if ( row
>= 0 && row
< m_numRows
&&
7888 col
>= 0 && col
< m_numCols
)
7890 int i
, cell_rows
, cell_cols
;
7891 rect
.width
= rect
.height
= 0;
7892 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7893 // if negative then find multicell owner
7894 if (cell_rows
< 0) row
+= cell_rows
;
7895 if (cell_cols
< 0) col
+= cell_cols
;
7896 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7898 rect
.x
= GetColLeft(col
);
7899 rect
.y
= GetRowTop(row
);
7900 for (i
=col
; i
<col
+cell_cols
; i
++)
7901 rect
.width
+= GetColWidth(i
);
7902 for (i
=row
; i
<row
+cell_rows
; i
++)
7903 rect
.height
+= GetRowHeight(i
);
7906 // if grid lines are enabled, then the area of the cell is a bit smaller
7907 if (m_gridLinesEnabled
) {
7915 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7917 // get the cell rectangle in logical coords
7919 wxRect
r( CellToRect( row
, col
) );
7921 // convert to device coords
7923 int left
, top
, right
, bottom
;
7924 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7925 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7927 // check against the client area of the grid window
7930 m_gridWin
->GetClientSize( &cw
, &ch
);
7932 if ( wholeCellVisible
)
7934 // is the cell wholly visible ?
7936 return ( left
>= 0 && right
<= cw
&&
7937 top
>= 0 && bottom
<= ch
);
7941 // is the cell partly visible ?
7943 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7944 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7949 // make the specified cell location visible by doing a minimal amount
7952 void wxGrid::MakeCellVisible( int row
, int col
)
7956 int xpos
= -1, ypos
= -1;
7958 if ( row
>= 0 && row
< m_numRows
&&
7959 col
>= 0 && col
< m_numCols
)
7961 // get the cell rectangle in logical coords
7963 wxRect
r( CellToRect( row
, col
) );
7965 // convert to device coords
7967 int left
, top
, right
, bottom
;
7968 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7969 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7972 m_gridWin
->GetClientSize( &cw
, &ch
);
7978 else if ( bottom
> ch
)
7980 int h
= r
.GetHeight();
7982 for ( i
= row
-1; i
>= 0; i
-- )
7984 int rowHeight
= GetRowHeight(i
);
7985 if ( h
+ rowHeight
> ch
)
7992 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
7993 // have rounding errors (this is important, because if we do, we
7994 // might not scroll at all and some cells won't be redrawn)
7996 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
7998 ypos
+= GRID_SCROLL_LINE_Y
;
8005 else if ( right
> cw
)
8007 // position the view so that the cell is on the right
8009 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8010 xpos
= x0
+ (right
- cw
);
8012 // see comment for ypos above
8013 xpos
+= GRID_SCROLL_LINE_X
;
8016 if ( xpos
!= -1 || ypos
!= -1 )
8019 xpos
/= GRID_SCROLL_LINE_X
;
8021 ypos
/= GRID_SCROLL_LINE_Y
;
8022 Scroll( xpos
, ypos
);
8030 // ------ Grid cursor movement functions
8033 bool wxGrid::MoveCursorUp( bool expandSelection
)
8035 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8036 m_currentCellCoords
.GetRow() >= 0 )
8038 if ( expandSelection
)
8040 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8041 m_selectingKeyboard
= m_currentCellCoords
;
8042 if ( m_selectingKeyboard
.GetRow() > 0 )
8044 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8045 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8046 m_selectingKeyboard
.GetCol() );
8047 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8050 else if ( m_currentCellCoords
.GetRow() > 0 )
8053 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8054 m_currentCellCoords
.GetCol() );
8055 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8056 m_currentCellCoords
.GetCol() );
8067 bool wxGrid::MoveCursorDown( bool expandSelection
)
8069 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8070 m_currentCellCoords
.GetRow() < m_numRows
)
8072 if ( expandSelection
)
8074 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8075 m_selectingKeyboard
= m_currentCellCoords
;
8076 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8078 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8079 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8080 m_selectingKeyboard
.GetCol() );
8081 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8084 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8087 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8088 m_currentCellCoords
.GetCol() );
8089 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8090 m_currentCellCoords
.GetCol() );
8101 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8103 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8104 m_currentCellCoords
.GetCol() >= 0 )
8106 if ( expandSelection
)
8108 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8109 m_selectingKeyboard
= m_currentCellCoords
;
8110 if ( m_selectingKeyboard
.GetCol() > 0 )
8112 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8113 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8114 m_selectingKeyboard
.GetCol() );
8115 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8118 else if ( m_currentCellCoords
.GetCol() > 0 )
8121 MakeCellVisible( m_currentCellCoords
.GetRow(),
8122 m_currentCellCoords
.GetCol() - 1 );
8123 SetCurrentCell( m_currentCellCoords
.GetRow(),
8124 m_currentCellCoords
.GetCol() - 1 );
8135 bool wxGrid::MoveCursorRight( bool expandSelection
)
8137 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8138 m_currentCellCoords
.GetCol() < m_numCols
)
8140 if ( expandSelection
)
8142 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8143 m_selectingKeyboard
= m_currentCellCoords
;
8144 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8146 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8147 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8148 m_selectingKeyboard
.GetCol() );
8149 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8152 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8155 MakeCellVisible( m_currentCellCoords
.GetRow(),
8156 m_currentCellCoords
.GetCol() + 1 );
8157 SetCurrentCell( m_currentCellCoords
.GetRow(),
8158 m_currentCellCoords
.GetCol() + 1 );
8169 bool wxGrid::MovePageUp()
8171 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8173 int row
= m_currentCellCoords
.GetRow();
8177 m_gridWin
->GetClientSize( &cw
, &ch
);
8179 int y
= GetRowTop(row
);
8180 int newRow
= internalYToRow( y
- ch
+ 1 );
8182 if ( newRow
== row
)
8184 //row > 0 , so newrow can never be less than 0 here.
8188 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8189 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8197 bool wxGrid::MovePageDown()
8199 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8201 int row
= m_currentCellCoords
.GetRow();
8202 if ( (row
+1) < m_numRows
)
8205 m_gridWin
->GetClientSize( &cw
, &ch
);
8207 int y
= GetRowTop(row
);
8208 int newRow
= internalYToRow( y
+ ch
);
8209 if ( newRow
== row
)
8211 // row < m_numRows , so newrow can't overflow here.
8215 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8216 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8224 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8227 m_currentCellCoords
!= wxGridNoCellCoords
&&
8228 m_currentCellCoords
.GetRow() > 0 )
8230 int row
= m_currentCellCoords
.GetRow();
8231 int col
= m_currentCellCoords
.GetCol();
8233 if ( m_table
->IsEmptyCell(row
, col
) )
8235 // starting in an empty cell: find the next block of
8241 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8244 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8246 // starting at the top of a block: find the next block
8252 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8257 // starting within a block: find the top of the block
8262 if ( m_table
->IsEmptyCell(row
, col
) )
8270 MakeCellVisible( row
, col
);
8271 if ( expandSelection
)
8273 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8274 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8279 SetCurrentCell( row
, col
);
8287 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8290 m_currentCellCoords
!= wxGridNoCellCoords
&&
8291 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8293 int row
= m_currentCellCoords
.GetRow();
8294 int col
= m_currentCellCoords
.GetCol();
8296 if ( m_table
->IsEmptyCell(row
, col
) )
8298 // starting in an empty cell: find the next block of
8301 while ( row
< m_numRows
-1 )
8304 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8307 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8309 // starting at the bottom of a block: find the next block
8312 while ( row
< m_numRows
-1 )
8315 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8320 // starting within a block: find the bottom of the block
8322 while ( row
< m_numRows
-1 )
8325 if ( m_table
->IsEmptyCell(row
, col
) )
8333 MakeCellVisible( row
, col
);
8334 if ( expandSelection
)
8336 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8337 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8342 SetCurrentCell( row
, col
);
8351 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8354 m_currentCellCoords
!= wxGridNoCellCoords
&&
8355 m_currentCellCoords
.GetCol() > 0 )
8357 int row
= m_currentCellCoords
.GetRow();
8358 int col
= m_currentCellCoords
.GetCol();
8360 if ( m_table
->IsEmptyCell(row
, col
) )
8362 // starting in an empty cell: find the next block of
8368 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8371 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8373 // starting at the left of a block: find the next block
8379 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8384 // starting within a block: find the left of the block
8389 if ( m_table
->IsEmptyCell(row
, col
) )
8397 MakeCellVisible( row
, col
);
8398 if ( expandSelection
)
8400 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8401 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8406 SetCurrentCell( row
, col
);
8415 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8418 m_currentCellCoords
!= wxGridNoCellCoords
&&
8419 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8421 int row
= m_currentCellCoords
.GetRow();
8422 int col
= m_currentCellCoords
.GetCol();
8424 if ( m_table
->IsEmptyCell(row
, col
) )
8426 // starting in an empty cell: find the next block of
8429 while ( col
< m_numCols
-1 )
8432 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8435 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8437 // starting at the right of a block: find the next block
8440 while ( col
< m_numCols
-1 )
8443 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8448 // starting within a block: find the right of the block
8450 while ( col
< m_numCols
-1 )
8453 if ( m_table
->IsEmptyCell(row
, col
) )
8461 MakeCellVisible( row
, col
);
8462 if ( expandSelection
)
8464 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8465 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8470 SetCurrentCell( row
, col
);
8482 // ------ Label values and formatting
8485 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8487 *horiz
= m_rowLabelHorizAlign
;
8488 *vert
= m_rowLabelVertAlign
;
8491 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8493 *horiz
= m_colLabelHorizAlign
;
8494 *vert
= m_colLabelVertAlign
;
8497 int wxGrid::GetColLabelTextOrientation()
8499 return m_colLabelTextOrientation
;
8502 wxString
wxGrid::GetRowLabelValue( int row
)
8506 return m_table
->GetRowLabelValue( row
);
8516 wxString
wxGrid::GetColLabelValue( int col
)
8520 return m_table
->GetColLabelValue( col
);
8531 void wxGrid::SetRowLabelSize( int width
)
8533 width
= wxMax( width
, 0 );
8534 if ( width
!= m_rowLabelWidth
)
8538 m_rowLabelWin
->Show( false );
8539 m_cornerLabelWin
->Show( false );
8541 else if ( m_rowLabelWidth
== 0 )
8543 m_rowLabelWin
->Show( true );
8544 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8547 m_rowLabelWidth
= width
;
8549 wxScrolledWindow::Refresh( true );
8554 void wxGrid::SetColLabelSize( int height
)
8556 height
= wxMax( height
, 0 );
8557 if ( height
!= m_colLabelHeight
)
8561 m_colLabelWin
->Show( false );
8562 m_cornerLabelWin
->Show( false );
8564 else if ( m_colLabelHeight
== 0 )
8566 m_colLabelWin
->Show( true );
8567 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8570 m_colLabelHeight
= height
;
8572 wxScrolledWindow::Refresh( true );
8577 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8579 if ( m_labelBackgroundColour
!= colour
)
8581 m_labelBackgroundColour
= colour
;
8582 m_rowLabelWin
->SetBackgroundColour( colour
);
8583 m_colLabelWin
->SetBackgroundColour( colour
);
8584 m_cornerLabelWin
->SetBackgroundColour( colour
);
8586 if ( !GetBatchCount() )
8588 m_rowLabelWin
->Refresh();
8589 m_colLabelWin
->Refresh();
8590 m_cornerLabelWin
->Refresh();
8595 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8597 if ( m_labelTextColour
!= colour
)
8599 m_labelTextColour
= colour
;
8600 if ( !GetBatchCount() )
8602 m_rowLabelWin
->Refresh();
8603 m_colLabelWin
->Refresh();
8608 void wxGrid::SetLabelFont( const wxFont
& font
)
8611 if ( !GetBatchCount() )
8613 m_rowLabelWin
->Refresh();
8614 m_colLabelWin
->Refresh();
8618 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8620 // allow old (incorrect) defs to be used
8623 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8624 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8625 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8630 case wxTOP
: vert
= wxALIGN_TOP
; break;
8631 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8632 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8635 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8637 m_rowLabelHorizAlign
= horiz
;
8640 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8642 m_rowLabelVertAlign
= vert
;
8645 if ( !GetBatchCount() )
8647 m_rowLabelWin
->Refresh();
8651 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8653 // allow old (incorrect) defs to be used
8656 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8657 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8658 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8663 case wxTOP
: vert
= wxALIGN_TOP
; break;
8664 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8665 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8668 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8670 m_colLabelHorizAlign
= horiz
;
8673 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8675 m_colLabelVertAlign
= vert
;
8678 if ( !GetBatchCount() )
8680 m_colLabelWin
->Refresh();
8684 // Note: under MSW, the default column label font must be changed because it
8685 // does not support vertical printing
8687 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8688 // pGrid->SetLabelFont(font);
8689 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8691 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8693 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8695 m_colLabelTextOrientation
= textOrientation
;
8698 if ( !GetBatchCount() )
8700 m_colLabelWin
->Refresh();
8704 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8708 m_table
->SetRowLabelValue( row
, s
);
8709 if ( !GetBatchCount() )
8711 wxRect rect
= CellToRect( row
, 0);
8712 if ( rect
.height
> 0 )
8714 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8716 rect
.width
= m_rowLabelWidth
;
8717 m_rowLabelWin
->Refresh( true, &rect
);
8723 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8727 m_table
->SetColLabelValue( col
, s
);
8728 if ( !GetBatchCount() )
8730 wxRect rect
= CellToRect( 0, col
);
8731 if ( rect
.width
> 0 )
8733 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8735 rect
.height
= m_colLabelHeight
;
8736 m_colLabelWin
->Refresh( true, &rect
);
8742 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8744 if ( m_gridLineColour
!= colour
)
8746 m_gridLineColour
= colour
;
8748 wxClientDC
dc( m_gridWin
);
8750 DrawAllGridLines( dc
, wxRegion() );
8755 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8757 if ( m_cellHighlightColour
!= colour
)
8759 m_cellHighlightColour
= colour
;
8761 wxClientDC
dc( m_gridWin
);
8763 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8764 DrawCellHighlight(dc
, attr
);
8769 void wxGrid::SetCellHighlightPenWidth(int width
)
8771 if (m_cellHighlightPenWidth
!= width
) {
8772 m_cellHighlightPenWidth
= width
;
8774 // Just redrawing the cell highlight is not enough since that won't
8775 // make any visible change if the the thickness is getting smaller.
8776 int row
= m_currentCellCoords
.GetRow();
8777 int col
= m_currentCellCoords
.GetCol();
8778 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8780 wxRect rect
= CellToRect(row
, col
);
8781 m_gridWin
->Refresh(true, &rect
);
8785 void wxGrid::SetCellHighlightROPenWidth(int width
)
8787 if (m_cellHighlightROPenWidth
!= width
) {
8788 m_cellHighlightROPenWidth
= width
;
8790 // Just redrawing the cell highlight is not enough since that won't
8791 // make any visible change if the the thickness is getting smaller.
8792 int row
= m_currentCellCoords
.GetRow();
8793 int col
= m_currentCellCoords
.GetCol();
8794 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8796 wxRect rect
= CellToRect(row
, col
);
8797 m_gridWin
->Refresh(true, &rect
);
8801 void wxGrid::EnableGridLines( bool enable
)
8803 if ( enable
!= m_gridLinesEnabled
)
8805 m_gridLinesEnabled
= enable
;
8807 if ( !GetBatchCount() )
8811 wxClientDC
dc( m_gridWin
);
8813 DrawAllGridLines( dc
, wxRegion() );
8817 m_gridWin
->Refresh();
8824 int wxGrid::GetDefaultRowSize()
8826 return m_defaultRowHeight
;
8829 int wxGrid::GetRowSize( int row
)
8831 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8833 return GetRowHeight(row
);
8836 int wxGrid::GetDefaultColSize()
8838 return m_defaultColWidth
;
8841 int wxGrid::GetColSize( int col
)
8843 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8845 return GetColWidth(col
);
8848 // ============================================================================
8849 // access to the grid attributes: each of them has a default value in the grid
8850 // itself and may be overidden on a per-cell basis
8851 // ============================================================================
8853 // ----------------------------------------------------------------------------
8854 // setting default attributes
8855 // ----------------------------------------------------------------------------
8857 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8859 m_defaultCellAttr
->SetBackgroundColour(col
);
8861 m_gridWin
->SetBackgroundColour(col
);
8865 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8867 m_defaultCellAttr
->SetTextColour(col
);
8870 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8872 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8875 void wxGrid::SetDefaultCellOverflow( bool allow
)
8877 m_defaultCellAttr
->SetOverflow(allow
);
8880 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8882 m_defaultCellAttr
->SetFont(font
);
8886 // For editors and renderers the type registry takes precedence over the
8887 // default attr, so we need to register the new editor/renderer for the string
8888 // data type in order to make setting a default editor/renderer appear to
8891 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8893 RegisterDataType(wxGRID_VALUE_STRING
,
8895 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8898 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8900 RegisterDataType(wxGRID_VALUE_STRING
,
8901 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8905 // ----------------------------------------------------------------------------
8906 // access to the default attrbiutes
8907 // ----------------------------------------------------------------------------
8909 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8911 return m_defaultCellAttr
->GetBackgroundColour();
8914 wxColour
wxGrid::GetDefaultCellTextColour()
8916 return m_defaultCellAttr
->GetTextColour();
8919 wxFont
wxGrid::GetDefaultCellFont()
8921 return m_defaultCellAttr
->GetFont();
8924 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8926 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8929 bool wxGrid::GetDefaultCellOverflow()
8931 return m_defaultCellAttr
->GetOverflow();
8934 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8936 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8939 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8941 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8944 // ----------------------------------------------------------------------------
8945 // access to cell attributes
8946 // ----------------------------------------------------------------------------
8948 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8950 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8951 wxColour colour
= attr
->GetBackgroundColour();
8956 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8958 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8959 wxColour colour
= attr
->GetTextColour();
8964 wxFont
wxGrid::GetCellFont( int row
, int col
)
8966 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8967 wxFont font
= attr
->GetFont();
8972 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8974 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8975 attr
->GetAlignment(horiz
, vert
);
8979 bool wxGrid::GetCellOverflow( int row
, int col
)
8981 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8982 bool allow
= attr
->GetOverflow();
8987 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8989 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8990 attr
->GetSize( num_rows
, num_cols
);
8994 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
8996 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8997 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9003 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9005 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9006 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9012 bool wxGrid::IsReadOnly(int row
, int col
) const
9014 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9015 bool isReadOnly
= attr
->IsReadOnly();
9020 // ----------------------------------------------------------------------------
9021 // attribute support: cache, automatic provider creation, ...
9022 // ----------------------------------------------------------------------------
9024 bool wxGrid::CanHaveAttributes()
9031 return m_table
->CanHaveAttributes();
9034 void wxGrid::ClearAttrCache()
9036 if ( m_attrCache
.row
!= -1 )
9038 wxSafeDecRef(m_attrCache
.attr
);
9039 m_attrCache
.attr
= NULL
;
9040 m_attrCache
.row
= -1;
9044 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9048 wxGrid
*self
= (wxGrid
*)this; // const_cast
9050 self
->ClearAttrCache();
9051 self
->m_attrCache
.row
= row
;
9052 self
->m_attrCache
.col
= col
;
9053 self
->m_attrCache
.attr
= attr
;
9058 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9060 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9062 *attr
= m_attrCache
.attr
;
9063 wxSafeIncRef(m_attrCache
.attr
);
9065 #ifdef DEBUG_ATTR_CACHE
9066 gs_nAttrCacheHits
++;
9073 #ifdef DEBUG_ATTR_CACHE
9074 gs_nAttrCacheMisses
++;
9080 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9082 wxGridCellAttr
*attr
= NULL
;
9083 // Additional test to avoid looking at the cache e.g. for
9084 // wxNoCellCoords, as this will confuse memory management.
9087 if ( !LookupAttr(row
, col
, &attr
) )
9089 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9090 : (wxGridCellAttr
*)NULL
;
9091 CacheAttr(row
, col
, attr
);
9096 attr
->SetDefAttr(m_defaultCellAttr
);
9100 attr
= m_defaultCellAttr
;
9107 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9109 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9110 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9112 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9113 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9115 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9118 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9120 // artificially inc the ref count to match DecRef() in caller
9122 m_table
->SetAttr(attr
, row
, col
);
9128 // ----------------------------------------------------------------------------
9129 // setting column attributes (wrappers around SetColAttr)
9130 // ----------------------------------------------------------------------------
9132 void wxGrid::SetColFormatBool(int col
)
9134 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9137 void wxGrid::SetColFormatNumber(int col
)
9139 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9142 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9144 wxString typeName
= wxGRID_VALUE_FLOAT
;
9145 if ( (width
!= -1) || (precision
!= -1) )
9147 typeName
<< _T(':') << width
<< _T(',') << precision
;
9150 SetColFormatCustom(col
, typeName
);
9153 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9155 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9157 attr
= new wxGridCellAttr
;
9158 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9159 attr
->SetRenderer(renderer
);
9161 SetColAttr(col
, attr
);
9165 // ----------------------------------------------------------------------------
9166 // setting cell attributes: this is forwarded to the table
9167 // ----------------------------------------------------------------------------
9169 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9171 if ( CanHaveAttributes() )
9173 m_table
->SetAttr(attr
, row
, col
);
9182 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9184 if ( CanHaveAttributes() )
9186 m_table
->SetRowAttr(attr
, row
);
9195 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9197 if ( CanHaveAttributes() )
9199 m_table
->SetColAttr(attr
, col
);
9208 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9210 if ( CanHaveAttributes() )
9212 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9213 attr
->SetBackgroundColour(colour
);
9218 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9220 if ( CanHaveAttributes() )
9222 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9223 attr
->SetTextColour(colour
);
9228 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9230 if ( CanHaveAttributes() )
9232 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9233 attr
->SetFont(font
);
9238 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9240 if ( CanHaveAttributes() )
9242 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9243 attr
->SetAlignment(horiz
, vert
);
9248 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9250 if ( CanHaveAttributes() )
9252 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9253 attr
->SetOverflow(allow
);
9258 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9260 if ( CanHaveAttributes() )
9262 int cell_rows
, cell_cols
;
9264 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9265 attr
->GetSize(&cell_rows
, &cell_cols
);
9266 attr
->SetSize(num_rows
, num_cols
);
9269 // Cannot set the size of a cell to 0 or negative values
9270 // While it is perfectly legal to do that, this function cannot
9271 // handle all the possibilies, do it by hand by getting the CellAttr.
9272 // You can only set the size of a cell to 1,1 or greater with this fn
9273 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9274 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9275 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9276 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9278 // if this was already a multicell then "turn off" the other cells first
9279 if ((cell_rows
> 1) || (cell_rows
> 1))
9282 for (j
=row
; j
<row
+cell_rows
; j
++)
9284 for (i
=col
; i
<col
+cell_cols
; i
++)
9286 if ((i
!= col
) || (j
!= row
))
9288 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9289 attr_stub
->SetSize( 1, 1 );
9290 attr_stub
->DecRef();
9296 // mark the cells that will be covered by this cell to
9297 // negative or zero values to point back at this cell
9298 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9301 for (j
=row
; j
<row
+num_rows
; j
++)
9303 for (i
=col
; i
<col
+num_cols
; i
++)
9305 if ((i
!= col
) || (j
!= row
))
9307 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9308 attr_stub
->SetSize( row
-j
, col
-i
);
9309 attr_stub
->DecRef();
9317 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9319 if ( CanHaveAttributes() )
9321 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9322 attr
->SetRenderer(renderer
);
9327 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9329 if ( CanHaveAttributes() )
9331 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9332 attr
->SetEditor(editor
);
9337 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9339 if ( CanHaveAttributes() )
9341 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9342 attr
->SetReadOnly(isReadOnly
);
9347 // ----------------------------------------------------------------------------
9348 // Data type registration
9349 // ----------------------------------------------------------------------------
9351 void wxGrid::RegisterDataType(const wxString
& typeName
,
9352 wxGridCellRenderer
* renderer
,
9353 wxGridCellEditor
* editor
)
9355 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9359 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9361 wxString typeName
= m_table
->GetTypeName(row
, col
);
9362 return GetDefaultEditorForType(typeName
);
9365 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9367 wxString typeName
= m_table
->GetTypeName(row
, col
);
9368 return GetDefaultRendererForType(typeName
);
9372 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9374 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9375 if ( index
== wxNOT_FOUND
)
9377 wxFAIL_MSG(wxT("Unknown data type name"));
9382 return m_typeRegistry
->GetEditor(index
);
9386 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9388 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9389 if ( index
== wxNOT_FOUND
)
9391 wxFAIL_MSG(wxT("Unknown data type name"));
9396 return m_typeRegistry
->GetRenderer(index
);
9400 // ----------------------------------------------------------------------------
9402 // ----------------------------------------------------------------------------
9404 void wxGrid::EnableDragRowSize( bool enable
)
9406 m_canDragRowSize
= enable
;
9410 void wxGrid::EnableDragColSize( bool enable
)
9412 m_canDragColSize
= enable
;
9415 void wxGrid::EnableDragGridSize( bool enable
)
9417 m_canDragGridSize
= enable
;
9420 void wxGrid::EnableDragCell( bool enable
)
9422 m_canDragCell
= enable
;
9425 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9427 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9429 if ( resizeExistingRows
)
9431 // since we are resizing all rows to the default row size,
9432 // we can simply clear the row heights and row bottoms
9433 // arrays (which also allows us to take advantage of
9434 // some speed optimisations)
9435 m_rowHeights
.Empty();
9436 m_rowBottoms
.Empty();
9437 if ( !GetBatchCount() )
9442 void wxGrid::SetRowSize( int row
, int height
)
9444 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9446 // See comment in SetColSize
9447 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9449 if ( m_rowHeights
.IsEmpty() )
9451 // need to really create the array
9455 int h
= wxMax( 0, height
);
9456 int diff
= h
- m_rowHeights
[row
];
9458 m_rowHeights
[row
] = h
;
9460 for ( i
= row
; i
< m_numRows
; i
++ )
9462 m_rowBottoms
[i
] += diff
;
9464 if ( !GetBatchCount() )
9468 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9470 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9472 if ( resizeExistingCols
)
9474 // since we are resizing all columns to the default column size,
9475 // we can simply clear the col widths and col rights
9476 // arrays (which also allows us to take advantage of
9477 // some speed optimisations)
9478 m_colWidths
.Empty();
9479 m_colRights
.Empty();
9480 if ( !GetBatchCount() )
9485 void wxGrid::SetColSize( int col
, int width
)
9487 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9489 // should we check that it's bigger than GetColMinimalWidth(col) here?
9491 // No, because it is reasonable to assume the library user know's
9492 // what he is doing. However whe should test against the weaker
9493 // constariant of minimalAcceptableWidth, as this breaks rendering
9495 // This test then fixes sf.net bug #645734
9497 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9499 if ( m_colWidths
.IsEmpty() )
9501 // need to really create the array
9505 // if < 0 calc new width from label
9509 wxArrayString lines
;
9510 wxClientDC
dc(m_colLabelWin
);
9511 dc
.SetFont(GetLabelFont());
9512 StringToLines(GetColLabelValue(col
), lines
);
9513 GetTextBoxSize(dc
, lines
, &w
, &h
);
9516 int w
= wxMax( 0, width
);
9517 int diff
= w
- m_colWidths
[col
];
9518 m_colWidths
[col
] = w
;
9521 for ( i
= col
; i
< m_numCols
; i
++ )
9523 m_colRights
[i
] += diff
;
9525 if ( !GetBatchCount() )
9530 void wxGrid::SetColMinimalWidth( int col
, int width
)
9532 if (width
> GetColMinimalAcceptableWidth()) {
9533 m_colMinWidths
[col
] = width
;
9537 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9539 if (width
> GetRowMinimalAcceptableHeight()) {
9540 m_rowMinHeights
[row
] = width
;
9544 int wxGrid::GetColMinimalWidth(int col
) const
9546 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9547 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9550 int wxGrid::GetRowMinimalHeight(int row
) const
9552 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9553 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9556 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9558 // We do allow a width of 0 since this gives us
9559 // an easy way to temporarily hidding columns.
9562 m_minAcceptableColWidth
= width
;
9565 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9567 // We do allow a height of 0 since this gives us
9568 // an easy way to temporarily hidding rows.
9571 m_minAcceptableRowHeight
= height
;
9574 int wxGrid::GetColMinimalAcceptableWidth() const
9576 return m_minAcceptableColWidth
;
9579 int wxGrid::GetRowMinimalAcceptableHeight() const
9581 return m_minAcceptableRowHeight
;
9584 // ----------------------------------------------------------------------------
9586 // ----------------------------------------------------------------------------
9588 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9590 wxClientDC
dc(m_gridWin
);
9592 //Cancel editting of cell
9593 HideCellEditControl();
9594 SaveEditControlValue();
9596 // init both of them to avoid compiler warnings, even if weo nly need one
9604 wxCoord extent
, extentMax
= 0;
9605 int max
= column
? m_numRows
: m_numCols
;
9606 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9613 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9614 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9617 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9618 extent
= column
? size
.x
: size
.y
;
9619 if ( extent
> extentMax
)
9630 // now also compare with the column label extent
9632 dc
.SetFont( GetLabelFont() );
9636 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9637 if( GetColLabelTextOrientation() == wxVERTICAL
)
9641 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9643 extent
= column
? w
: h
;
9644 if ( extent
> extentMax
)
9651 // empty column - give default extent (notice that if extentMax is less
9652 // than default extent but != 0, it's ok)
9653 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9659 // leave some space around text
9670 SetColSize(col
, extentMax
);
9671 if ( !GetBatchCount() )
9674 m_gridWin
->GetClientSize( &cw
, &ch
);
9675 wxRect
rect ( CellToRect( 0, col
) );
9677 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9678 rect
.width
= cw
- rect
.x
;
9679 rect
.height
= m_colLabelHeight
;
9680 m_colLabelWin
->Refresh( true, &rect
);
9685 SetRowSize(row
, extentMax
);
9686 if ( !GetBatchCount() )
9689 m_gridWin
->GetClientSize( &cw
, &ch
);
9690 wxRect
rect ( CellToRect( row
, 0 ) );
9692 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9693 rect
.width
= m_rowLabelWidth
;
9694 rect
.height
= ch
- rect
.y
;
9695 m_rowLabelWin
->Refresh( true, &rect
);
9701 SetColMinimalWidth(col
, extentMax
);
9703 SetRowMinimalHeight(row
, extentMax
);
9707 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9709 int width
= m_rowLabelWidth
;
9714 for ( int col
= 0; col
< m_numCols
; col
++ )
9718 AutoSizeColumn(col
, setAsMin
);
9721 width
+= GetColWidth(col
);
9730 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9732 int height
= m_colLabelHeight
;
9737 for ( int row
= 0; row
< m_numRows
; row
++ )
9741 AutoSizeRow(row
, setAsMin
);
9744 height
+= GetRowHeight(row
);
9753 void wxGrid::AutoSize()
9757 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9759 // round up the size to a multiple of scroll step - this ensures that we
9760 // won't get the scrollbars if we're sized exactly to this width
9761 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9763 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9764 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9766 // distribute the extra space between the columns/rows to avoid having
9767 // extra white space
9769 // Remove the extra m_extraWidth + 1 added above
9770 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9771 if ( diff
&& m_numCols
)
9773 // try to resize the columns uniformly
9774 wxCoord diffPerCol
= diff
/ m_numCols
;
9777 for ( int col
= 0; col
< m_numCols
; col
++ )
9779 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9783 // add remaining amount to the last columns
9784 diff
-= diffPerCol
* m_numCols
;
9787 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9789 SetColSize(col
, GetColWidth(col
) + 1);
9795 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9796 if ( diff
&& m_numRows
)
9798 // try to resize the columns uniformly
9799 wxCoord diffPerRow
= diff
/ m_numRows
;
9802 for ( int row
= 0; row
< m_numRows
; row
++ )
9804 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9808 // add remaining amount to the last rows
9809 diff
-= diffPerRow
* m_numRows
;
9812 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9814 SetRowSize(row
, GetRowHeight(row
) + 1);
9821 SetClientSize(sizeFit
);
9824 void wxGrid::AutoSizeRowLabelSize( int row
)
9826 wxArrayString lines
;
9829 // Hide the edit control, so it
9830 // won't interfer with drag-shrinking.
9831 if( IsCellEditControlShown() )
9833 HideCellEditControl();
9834 SaveEditControlValue();
9837 // autosize row height depending on label text
9838 StringToLines( GetRowLabelValue( row
), lines
);
9839 wxClientDC
dc( m_rowLabelWin
);
9840 GetTextBoxSize( dc
, lines
, &w
, &h
);
9841 if( h
< m_defaultRowHeight
)
9842 h
= m_defaultRowHeight
;
9847 void wxGrid::AutoSizeColLabelSize( int col
)
9849 wxArrayString lines
;
9852 // Hide the edit control, so it
9853 // won't interfer with drag-shrinking.
9854 if( IsCellEditControlShown() )
9856 HideCellEditControl();
9857 SaveEditControlValue();
9860 // autosize column width depending on label text
9861 StringToLines( GetColLabelValue( col
), lines
);
9862 wxClientDC
dc( m_colLabelWin
);
9863 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9864 GetTextBoxSize( dc
, lines
, &w
, &h
);
9866 GetTextBoxSize( dc
, lines
, &h
, &w
);
9867 if( w
< m_defaultColWidth
)
9868 w
= m_defaultColWidth
;
9873 wxSize
wxGrid::DoGetBestSize() const
9875 // don't set sizes, only calculate them
9876 wxGrid
*self
= (wxGrid
*)this; // const_cast
9879 width
= self
->SetOrCalcColumnSizes(true);
9880 height
= self
->SetOrCalcRowSizes(true);
9882 if (!width
) width
=100;
9883 if (!height
) height
=80;
9885 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9886 // of the scrollbars, is there any magic incantaion for that?
9888 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9890 width
+= 1 + xpu
- (width
% xpu
);
9892 height
+= 1 + ypu
- (height
% ypu
);
9894 // limit to 1/4 of the screen size
9895 int maxwidth
, maxheight
;
9896 wxDisplaySize( & maxwidth
, & maxheight
);
9899 if ( width
> maxwidth
) width
= maxwidth
;
9900 if ( height
> maxheight
) height
= maxheight
;
9903 wxSize
best(width
, height
);
9904 // NOTE: This size should be cached, but first we need to add calls to
9905 // InvalidateBestSize everywhere that could change the results of this
9907 // CacheBestSize(size);
9917 wxPen
& wxGrid::GetDividerPen() const
9922 // ----------------------------------------------------------------------------
9923 // cell value accessor functions
9924 // ----------------------------------------------------------------------------
9926 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9930 m_table
->SetValue( row
, col
, s
);
9931 if ( !GetBatchCount() )
9934 wxRect
rect( CellToRect( row
, col
) );
9936 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9937 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9938 m_gridWin
->Refresh( false, &rect
);
9941 if ( m_currentCellCoords
.GetRow() == row
&&
9942 m_currentCellCoords
.GetCol() == col
&&
9943 IsCellEditControlShown())
9944 // Note: If we are using IsCellEditControlEnabled,
9945 // this interacts badly with calling SetCellValue from
9946 // an EVT_GRID_CELL_CHANGE handler.
9948 HideCellEditControl();
9949 ShowCellEditControl(); // will reread data from table
9956 // ------ Block, row and col selection
9959 void wxGrid::SelectRow( int row
, bool addToSelected
)
9961 if ( IsSelection() && !addToSelected
)
9965 m_selection
->SelectRow( row
, false, addToSelected
);
9969 void wxGrid::SelectCol( int col
, bool addToSelected
)
9971 if ( IsSelection() && !addToSelected
)
9975 m_selection
->SelectCol( col
, false, addToSelected
);
9979 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9980 bool addToSelected
)
9982 if ( IsSelection() && !addToSelected
)
9986 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9987 false, addToSelected
);
9991 void wxGrid::SelectAll()
9993 if ( m_numRows
> 0 && m_numCols
> 0 )
9996 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10001 // ------ Cell, row and col deselection
10004 void wxGrid::DeselectRow( int row
)
10006 if ( !m_selection
)
10009 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10011 if ( m_selection
->IsInSelection(row
, 0 ) )
10012 m_selection
->ToggleCellSelection( row
, 0);
10016 int nCols
= GetNumberCols();
10017 for ( int i
= 0; i
< nCols
; i
++ )
10019 if ( m_selection
->IsInSelection(row
, i
) )
10020 m_selection
->ToggleCellSelection( row
, i
);
10025 void wxGrid::DeselectCol( int col
)
10027 if ( !m_selection
)
10030 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10032 if ( m_selection
->IsInSelection(0, col
) )
10033 m_selection
->ToggleCellSelection( 0, col
);
10037 int nRows
= GetNumberRows();
10038 for ( int i
= 0; i
< nRows
; i
++ )
10040 if ( m_selection
->IsInSelection(i
, col
) )
10041 m_selection
->ToggleCellSelection(i
, col
);
10046 void wxGrid::DeselectCell( int row
, int col
)
10048 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10049 m_selection
->ToggleCellSelection(row
, col
);
10052 bool wxGrid::IsSelection()
10054 return ( m_selection
&& (m_selection
->IsSelection() ||
10055 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10056 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10059 bool wxGrid::IsInSelection( int row
, int col
) const
10061 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10062 ( row
>= m_selectingTopLeft
.GetRow() &&
10063 col
>= m_selectingTopLeft
.GetCol() &&
10064 row
<= m_selectingBottomRight
.GetRow() &&
10065 col
<= m_selectingBottomRight
.GetCol() )) );
10068 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10070 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10071 return m_selection
->m_cellSelection
;
10073 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10075 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10076 return m_selection
->m_blockSelectionTopLeft
;
10078 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10080 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10081 return m_selection
->m_blockSelectionBottomRight
;
10083 wxArrayInt
wxGrid::GetSelectedRows() const
10085 if (!m_selection
) { wxArrayInt a
; return a
; }
10086 return m_selection
->m_rowSelection
;
10088 wxArrayInt
wxGrid::GetSelectedCols() const
10090 if (!m_selection
) { wxArrayInt a
; return a
; }
10091 return m_selection
->m_colSelection
;
10095 void wxGrid::ClearSelection()
10097 m_selectingTopLeft
= wxGridNoCellCoords
;
10098 m_selectingBottomRight
= wxGridNoCellCoords
;
10100 m_selection
->ClearSelection();
10104 // This function returns the rectangle that encloses the given block
10105 // in device coords clipped to the client size of the grid window.
10107 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10108 const wxGridCellCoords
&bottomRight
)
10110 wxRect
rect( wxGridNoCellRect
);
10113 cellRect
= CellToRect( topLeft
);
10114 if ( cellRect
!= wxGridNoCellRect
)
10120 rect
= wxRect( 0, 0, 0, 0 );
10123 cellRect
= CellToRect( bottomRight
);
10124 if ( cellRect
!= wxGridNoCellRect
)
10130 return wxGridNoCellRect
;
10134 int left
= rect
.GetLeft();
10135 int top
= rect
.GetTop();
10136 int right
= rect
.GetRight();
10137 int bottom
= rect
.GetBottom();
10139 int leftCol
= topLeft
.GetCol();
10140 int topRow
= topLeft
.GetRow();
10141 int rightCol
= bottomRight
.GetCol();
10142 int bottomRow
= bottomRight
.GetRow();
10160 topRow
= bottomRow
;
10165 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10167 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10169 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10171 cellRect
= CellToRect( j
, i
);
10173 if (cellRect
.x
< left
)
10175 if (cellRect
.y
< top
)
10177 if (cellRect
.x
+ cellRect
.width
> right
)
10178 right
= cellRect
.x
+ cellRect
.width
;
10179 if (cellRect
.y
+ cellRect
.height
> bottom
)
10180 bottom
= cellRect
.y
+ cellRect
.height
;
10182 else i
= rightCol
; // jump over inner cells.
10186 // convert to scrolled coords
10188 CalcScrolledPosition( left
, top
, &left
, &top
);
10189 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10192 m_gridWin
->GetClientSize( &cw
, &ch
);
10194 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10195 return wxRect( 0, 0, 0, 0);
10197 rect
.SetLeft( wxMax(0, left
) );
10198 rect
.SetTop( wxMax(0, top
) );
10199 rect
.SetRight( wxMin(cw
, right
) );
10200 rect
.SetBottom( wxMin(ch
, bottom
) );
10206 // ------ Grid event classes
10209 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10211 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10212 int row
, int col
, int x
, int y
, bool sel
,
10213 bool control
, bool shift
, bool alt
, bool meta
)
10214 : wxNotifyEvent( type
, id
)
10221 m_control
= control
;
10226 SetEventObject(obj
);
10230 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10232 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10233 int rowOrCol
, int x
, int y
,
10234 bool control
, bool shift
, bool alt
, bool meta
)
10235 : wxNotifyEvent( type
, id
)
10237 m_rowOrCol
= rowOrCol
;
10240 m_control
= control
;
10245 SetEventObject(obj
);
10249 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10251 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10252 const wxGridCellCoords
& topLeft
,
10253 const wxGridCellCoords
& bottomRight
,
10254 bool sel
, bool control
,
10255 bool shift
, bool alt
, bool meta
)
10256 : wxNotifyEvent( type
, id
)
10258 m_topLeft
= topLeft
;
10259 m_bottomRight
= bottomRight
;
10261 m_control
= control
;
10266 SetEventObject(obj
);
10270 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10272 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10273 wxObject
* obj
, int row
,
10274 int col
, wxControl
* ctrl
)
10275 : wxCommandEvent(type
, id
)
10277 SetEventObject(obj
);
10283 #endif // wxUSE_GRID