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 more 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
;
2635 case (wxGridCellAttr::Cell
):
2636 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2638 case (wxGridCellAttr::Col
):
2639 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2641 case (wxGridCellAttr::Row
):
2642 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2646 // (wxGridCellAttr::Default):
2647 // (wxGridCellAttr::Merged):
2654 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2660 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2663 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2668 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2671 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2676 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2679 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2683 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2685 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2689 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2693 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2695 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2699 // ----------------------------------------------------------------------------
2700 // wxGridTypeRegistry
2701 // ----------------------------------------------------------------------------
2703 wxGridTypeRegistry::~wxGridTypeRegistry()
2705 size_t count
= m_typeinfo
.Count();
2706 for ( size_t i
= 0; i
< count
; i
++ )
2707 delete m_typeinfo
[i
];
2711 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2712 wxGridCellRenderer
* renderer
,
2713 wxGridCellEditor
* editor
)
2715 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2717 // is it already registered?
2718 int loc
= FindRegisteredDataType(typeName
);
2719 if ( loc
!= wxNOT_FOUND
)
2721 delete m_typeinfo
[loc
];
2722 m_typeinfo
[loc
] = info
;
2726 m_typeinfo
.Add(info
);
2730 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2732 size_t count
= m_typeinfo
.GetCount();
2733 for ( size_t i
= 0; i
< count
; i
++ )
2735 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2744 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2746 int index
= FindRegisteredDataType(typeName
);
2747 if ( index
== wxNOT_FOUND
)
2749 // check whether this is one of the standard ones, in which case
2750 // register it "on the fly"
2752 if ( typeName
== wxGRID_VALUE_STRING
)
2754 RegisterDataType(wxGRID_VALUE_STRING
,
2755 new wxGridCellStringRenderer
,
2756 new wxGridCellTextEditor
);
2758 #endif // wxUSE_TEXTCTRL
2760 if ( typeName
== wxGRID_VALUE_BOOL
)
2762 RegisterDataType(wxGRID_VALUE_BOOL
,
2763 new wxGridCellBoolRenderer
,
2764 new wxGridCellBoolEditor
);
2766 #endif // wxUSE_CHECKBOX
2768 if ( typeName
== wxGRID_VALUE_NUMBER
)
2770 RegisterDataType(wxGRID_VALUE_NUMBER
,
2771 new wxGridCellNumberRenderer
,
2772 new wxGridCellNumberEditor
);
2774 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2776 RegisterDataType(wxGRID_VALUE_FLOAT
,
2777 new wxGridCellFloatRenderer
,
2778 new wxGridCellFloatEditor
);
2780 #endif // wxUSE_TEXTCTRL
2782 if ( typeName
== wxGRID_VALUE_CHOICE
)
2784 RegisterDataType(wxGRID_VALUE_CHOICE
,
2785 new wxGridCellStringRenderer
,
2786 new wxGridCellChoiceEditor
);
2788 #endif // wxUSE_COMBOBOX
2793 // we get here only if just added the entry for this type, so return
2795 index
= m_typeinfo
.GetCount() - 1;
2801 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2803 int index
= FindDataType(typeName
);
2804 if ( index
== wxNOT_FOUND
)
2806 // the first part of the typename is the "real" type, anything after ':'
2807 // are the parameters for the renderer
2808 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2809 if ( index
== wxNOT_FOUND
)
2814 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2815 wxGridCellRenderer
*rendererOld
= renderer
;
2816 renderer
= renderer
->Clone();
2817 rendererOld
->DecRef();
2819 wxGridCellEditor
*editor
= GetEditor(index
);
2820 wxGridCellEditor
*editorOld
= editor
;
2821 editor
= editor
->Clone();
2822 editorOld
->DecRef();
2824 // do it even if there are no parameters to reset them to defaults
2825 wxString params
= typeName
.AfterFirst(_T(':'));
2826 renderer
->SetParameters(params
);
2827 editor
->SetParameters(params
);
2829 // register the new typename
2830 RegisterDataType(typeName
, renderer
, editor
);
2832 // we just registered it, it's the last one
2833 index
= m_typeinfo
.GetCount() - 1;
2839 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2841 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2847 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2849 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2855 // ----------------------------------------------------------------------------
2857 // ----------------------------------------------------------------------------
2859 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2862 wxGridTableBase::wxGridTableBase()
2864 m_view
= (wxGrid
*) NULL
;
2865 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2868 wxGridTableBase::~wxGridTableBase()
2870 delete m_attrProvider
;
2873 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2875 delete m_attrProvider
;
2876 m_attrProvider
= attrProvider
;
2879 bool wxGridTableBase::CanHaveAttributes()
2881 if ( ! GetAttrProvider() )
2883 // use the default attr provider by default
2884 SetAttrProvider(new wxGridCellAttrProvider
);
2889 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2891 if ( m_attrProvider
)
2892 return m_attrProvider
->GetAttr(row
, col
, kind
);
2894 return (wxGridCellAttr
*)NULL
;
2897 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2899 if ( m_attrProvider
)
2901 attr
->SetKind(wxGridCellAttr::Cell
);
2902 m_attrProvider
->SetAttr(attr
, row
, col
);
2906 // as we take ownership of the pointer and don't store it, we must
2912 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2914 if ( m_attrProvider
)
2916 attr
->SetKind(wxGridCellAttr::Row
);
2917 m_attrProvider
->SetRowAttr(attr
, row
);
2921 // as we take ownership of the pointer and don't store it, we must
2927 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2929 if ( m_attrProvider
)
2931 attr
->SetKind(wxGridCellAttr::Col
);
2932 m_attrProvider
->SetColAttr(attr
, col
);
2936 // as we take ownership of the pointer and don't store it, we must
2942 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2943 size_t WXUNUSED(numRows
) )
2945 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2950 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2952 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2957 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2958 size_t WXUNUSED(numRows
) )
2960 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2965 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2966 size_t WXUNUSED(numCols
) )
2968 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2973 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2975 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2980 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2981 size_t WXUNUSED(numCols
) )
2983 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2989 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2992 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2993 // how much it makes sense to us geeks.
2997 wxString
wxGridTableBase::GetColLabelValue( int col
)
2999 // default col labels are:
3000 // cols 0 to 25 : A-Z
3001 // cols 26 to 675 : AA-ZZ
3006 for ( n
= 1; ; n
++ )
3008 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3010 if ( col
< 0 ) break;
3013 // reverse the string...
3015 for ( i
= 0; i
< n
; i
++ )
3024 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3026 return wxGRID_VALUE_STRING
;
3029 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3030 const wxString
& typeName
)
3032 return typeName
== wxGRID_VALUE_STRING
;
3035 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3037 return CanGetValueAs(row
, col
, typeName
);
3040 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3045 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3050 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3055 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3056 long WXUNUSED(value
) )
3060 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3061 double WXUNUSED(value
) )
3065 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3066 bool WXUNUSED(value
) )
3071 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3072 const wxString
& WXUNUSED(typeName
) )
3077 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3078 const wxString
& WXUNUSED(typeName
),
3079 void* WXUNUSED(value
) )
3083 //////////////////////////////////////////////////////////////////////
3085 // Message class for the grid table to send requests and notifications
3089 wxGridTableMessage::wxGridTableMessage()
3091 m_table
= (wxGridTableBase
*) NULL
;
3097 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3098 int commandInt1
, int commandInt2
)
3102 m_comInt1
= commandInt1
;
3103 m_comInt2
= commandInt2
;
3108 //////////////////////////////////////////////////////////////////////
3110 // A basic grid table for string data. An object of this class will
3111 // created by wxGrid if you don't specify an alternative table class.
3114 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3116 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3118 wxGridStringTable::wxGridStringTable()
3123 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3126 m_data
.Alloc( numRows
);
3129 sa
.Alloc( numCols
);
3130 sa
.Add( wxEmptyString
, numCols
);
3132 m_data
.Add( sa
, numRows
);
3135 wxGridStringTable::~wxGridStringTable()
3139 int wxGridStringTable::GetNumberRows()
3141 return m_data
.GetCount();
3144 int wxGridStringTable::GetNumberCols()
3146 if ( m_data
.GetCount() > 0 )
3147 return m_data
[0].GetCount();
3152 wxString
wxGridStringTable::GetValue( int row
, int col
)
3154 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3156 _T("invalid row or column index in wxGridStringTable") );
3158 return m_data
[row
][col
];
3161 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3163 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3164 _T("invalid row or column index in wxGridStringTable") );
3166 m_data
[row
][col
] = value
;
3169 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3171 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3173 _T("invalid row or column index in wxGridStringTable") );
3175 return (m_data
[row
][col
] == wxEmptyString
);
3178 void wxGridStringTable::Clear()
3181 int numRows
, numCols
;
3183 numRows
= m_data
.GetCount();
3186 numCols
= m_data
[0].GetCount();
3188 for ( row
= 0; row
< numRows
; row
++ )
3190 for ( col
= 0; col
< numCols
; col
++ )
3192 m_data
[row
][col
] = wxEmptyString
;
3199 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3201 size_t curNumRows
= m_data
.GetCount();
3202 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3203 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3205 if ( pos
>= curNumRows
)
3207 return AppendRows( numRows
);
3211 sa
.Alloc( curNumCols
);
3212 sa
.Add( wxEmptyString
, curNumCols
);
3213 m_data
.Insert( sa
, pos
, numRows
);
3216 wxGridTableMessage
msg( this,
3217 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3221 GetView()->ProcessTableMessage( msg
);
3227 bool wxGridStringTable::AppendRows( size_t numRows
)
3229 size_t curNumRows
= m_data
.GetCount();
3230 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3231 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3234 if ( curNumCols
> 0 )
3236 sa
.Alloc( curNumCols
);
3237 sa
.Add( wxEmptyString
, curNumCols
);
3240 m_data
.Add( sa
, numRows
);
3244 wxGridTableMessage
msg( this,
3245 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3248 GetView()->ProcessTableMessage( msg
);
3254 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3256 size_t curNumRows
= m_data
.GetCount();
3258 if ( pos
>= curNumRows
)
3260 wxFAIL_MSG( wxString::Format
3262 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3264 (unsigned long)numRows
,
3265 (unsigned long)curNumRows
3271 if ( numRows
> curNumRows
- pos
)
3273 numRows
= curNumRows
- pos
;
3276 if ( numRows
>= curNumRows
)
3282 m_data
.RemoveAt( pos
, numRows
);
3286 wxGridTableMessage
msg( this,
3287 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3291 GetView()->ProcessTableMessage( msg
);
3297 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3301 size_t curNumRows
= m_data
.GetCount();
3302 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3303 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3305 if ( pos
>= curNumCols
)
3307 return AppendCols( numCols
);
3310 for ( row
= 0; row
< curNumRows
; row
++ )
3312 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3314 m_data
[row
].Insert( wxEmptyString
, col
);
3319 wxGridTableMessage
msg( this,
3320 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3324 GetView()->ProcessTableMessage( msg
);
3330 bool wxGridStringTable::AppendCols( size_t numCols
)
3334 size_t curNumRows
= m_data
.GetCount();
3338 // TODO: something better than this ?
3340 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3345 for ( row
= 0; row
< curNumRows
; row
++ )
3347 m_data
[row
].Add( wxEmptyString
, numCols
);
3352 wxGridTableMessage
msg( this,
3353 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3356 GetView()->ProcessTableMessage( msg
);
3362 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3366 size_t curNumRows
= m_data
.GetCount();
3367 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3368 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3370 if ( pos
>= curNumCols
)
3372 wxFAIL_MSG( wxString::Format
3374 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3376 (unsigned long)numCols
,
3377 (unsigned long)curNumCols
3382 if ( numCols
> curNumCols
- pos
)
3384 numCols
= curNumCols
- pos
;
3387 for ( row
= 0; row
< curNumRows
; row
++ )
3389 if ( numCols
>= curNumCols
)
3391 m_data
[row
].Clear();
3395 m_data
[row
].RemoveAt( pos
, numCols
);
3400 wxGridTableMessage
msg( this,
3401 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3405 GetView()->ProcessTableMessage( msg
);
3411 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3413 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3415 // using default label
3417 return wxGridTableBase::GetRowLabelValue( row
);
3421 return m_rowLabels
[ row
];
3425 wxString
wxGridStringTable::GetColLabelValue( int col
)
3427 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3429 // using default label
3431 return wxGridTableBase::GetColLabelValue( col
);
3435 return m_colLabels
[ col
];
3439 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3441 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3443 int n
= m_rowLabels
.GetCount();
3445 for ( i
= n
; i
<= row
; i
++ )
3447 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3451 m_rowLabels
[row
] = value
;
3454 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3456 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3458 int n
= m_colLabels
.GetCount();
3460 for ( i
= n
; i
<= col
; i
++ )
3462 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3466 m_colLabels
[col
] = value
;
3471 //////////////////////////////////////////////////////////////////////
3472 //////////////////////////////////////////////////////////////////////
3474 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3476 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3477 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3478 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3479 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3480 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3481 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3484 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3486 const wxPoint
&pos
, const wxSize
&size
)
3487 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3492 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3496 // NO - don't do this because it will set both the x and y origin
3497 // coords to match the parent scrolled window and we just want to
3498 // set the y coord - MB
3500 // m_owner->PrepareDC( dc );
3503 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3504 dc
.SetDeviceOrigin( 0, -y
);
3506 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3507 m_owner
->DrawRowLabels( dc
, rows
);
3511 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3513 m_owner
->ProcessRowLabelMouseEvent( event
);
3517 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3519 m_owner
->GetEventHandler()->ProcessEvent(event
);
3523 // This seems to be required for wxMotif otherwise the mouse
3524 // cursor must be in the cell edit control to get key events
3526 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3528 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3531 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3533 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3538 //////////////////////////////////////////////////////////////////////
3540 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3542 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3543 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3544 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3545 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3546 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3547 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3550 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3552 const wxPoint
&pos
, const wxSize
&size
)
3553 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3558 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3562 // NO - don't do this because it will set both the x and y origin
3563 // coords to match the parent scrolled window and we just want to
3564 // set the x coord - MB
3566 // m_owner->PrepareDC( dc );
3569 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3570 dc
.SetDeviceOrigin( -x
, 0 );
3572 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3573 m_owner
->DrawColLabels( dc
, cols
);
3577 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3579 m_owner
->ProcessColLabelMouseEvent( event
);
3582 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3584 m_owner
->GetEventHandler()->ProcessEvent(event
);
3588 // This seems to be required for wxMotif otherwise the mouse
3589 // cursor must be in the cell edit control to get key events
3591 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3593 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3596 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3598 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3603 //////////////////////////////////////////////////////////////////////
3605 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3607 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3608 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3609 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3610 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3611 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3612 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3615 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3617 const wxPoint
&pos
, const wxSize
&size
)
3618 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3623 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3627 int client_height
= 0;
3628 int client_width
= 0;
3629 GetClientSize( &client_width
, &client_height
);
3631 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3632 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3633 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3634 dc
.DrawLine( 0, 0, client_width
, 0 );
3635 dc
.DrawLine( 0, 0, 0, client_height
);
3637 dc
.SetPen( *wxWHITE_PEN
);
3638 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3639 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3643 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3645 m_owner
->ProcessCornerLabelMouseEvent( event
);
3649 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3651 m_owner
->GetEventHandler()->ProcessEvent(event
);
3654 // This seems to be required for wxMotif otherwise the mouse
3655 // cursor must be in the cell edit control to get key events
3657 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3659 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3662 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3664 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3669 //////////////////////////////////////////////////////////////////////
3671 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3673 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3674 EVT_PAINT( wxGridWindow::OnPaint
)
3675 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3676 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3677 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3678 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3679 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3680 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3681 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3684 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3685 wxGridRowLabelWindow
*rowLblWin
,
3686 wxGridColLabelWindow
*colLblWin
,
3689 const wxSize
&size
)
3690 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3691 wxT("grid window") )
3695 m_rowLabelWin
= rowLblWin
;
3696 m_colLabelWin
= colLblWin
;
3700 wxGridWindow::~wxGridWindow()
3705 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3707 wxPaintDC
dc( this );
3708 m_owner
->PrepareDC( dc
);
3709 wxRegion reg
= GetUpdateRegion();
3710 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3711 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3712 #if WXGRID_DRAW_LINES
3713 m_owner
->DrawAllGridLines( dc
, reg
);
3715 m_owner
->DrawGridSpace( dc
);
3716 m_owner
->DrawHighlight( dc
, DirtyCells
);
3720 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3722 wxWindow::ScrollWindow( dx
, dy
, rect
);
3723 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3724 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3728 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3730 m_owner
->ProcessGridCellMouseEvent( event
);
3733 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3735 m_owner
->GetEventHandler()->ProcessEvent(event
);
3738 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3739 // cursor must be in the cell edit control to get key events
3741 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3743 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3746 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3748 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3751 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3755 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3757 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3761 //////////////////////////////////////////////////////////////////////
3763 // Internal Helper function for computing row or column from some
3764 // (unscrolled) coordinate value, using either
3765 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3766 // of m_rowBottoms/m_ColRights to speed up the search!
3768 // Internal helper macros for simpler use of that function
3770 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3771 const wxArrayInt
& BorderArray
, int nMax
,
3774 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3775 m_minAcceptableColWidth, \
3776 m_colRights, m_numCols, true)
3777 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3778 m_minAcceptableRowHeight, \
3779 m_rowBottoms, m_numRows, true)
3780 /////////////////////////////////////////////////////////////////////
3782 #if wxUSE_EXTENDED_RTTI
3783 WX_DEFINE_FLAGS( wxGridStyle
)
3785 wxBEGIN_FLAGS( wxGridStyle
)
3786 // new style border flags, we put them first to
3787 // use them for streaming out
3788 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3789 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3790 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3791 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3792 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3793 wxFLAGS_MEMBER(wxBORDER_NONE
)
3795 // old style border flags
3796 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3797 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3798 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3799 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3800 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3801 wxFLAGS_MEMBER(wxBORDER
)
3803 // standard window styles
3804 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3805 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3806 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3807 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3808 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3809 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3810 wxFLAGS_MEMBER(wxVSCROLL
)
3811 wxFLAGS_MEMBER(wxHSCROLL
)
3813 wxEND_FLAGS( wxGridStyle
)
3815 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3817 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3818 wxHIDE_PROPERTY( Children
)
3819 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3820 wxEND_PROPERTIES_TABLE()
3822 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3823 wxEND_HANDLERS_TABLE()
3825 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3828 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3831 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3834 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3835 EVT_PAINT( wxGrid::OnPaint
)
3836 EVT_SIZE( wxGrid::OnSize
)
3837 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3838 EVT_KEY_UP( wxGrid::OnKeyUp
)
3839 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3844 // in order to make sure that a size event is not
3845 // trigerred in a unfinished state
3846 m_cornerLabelWin
= NULL
;
3847 m_rowLabelWin
= NULL
;
3848 m_colLabelWin
= NULL
;
3852 wxGrid::wxGrid( wxWindow
*parent
,
3857 const wxString
& name
)
3858 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3859 m_colMinWidths(GRID_HASH_SIZE
),
3860 m_rowMinHeights(GRID_HASH_SIZE
)
3863 SetBestFittingSize(size
);
3866 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3867 const wxPoint
& pos
, const wxSize
& size
,
3868 long style
, const wxString
& name
)
3870 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3871 style
| wxWANTS_CHARS
, name
))
3874 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3875 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3878 SetBestFittingSize(size
);
3886 // Must do this or ~wxScrollHelper will pop the wrong event handler
3887 SetTargetWindow(this);
3889 wxSafeDecRef(m_defaultCellAttr
);
3891 #ifdef DEBUG_ATTR_CACHE
3892 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3893 wxPrintf(_T("wxGrid attribute cache statistics: "
3894 "total: %u, hits: %u (%u%%)\n"),
3895 total
, gs_nAttrCacheHits
,
3896 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3902 delete m_typeRegistry
;
3908 // ----- internal init and update functions
3911 // NOTE: If using the default visual attributes works everywhere then this can
3912 // be removed as well as the #else cases below.
3913 #define _USE_VISATTR 0
3916 #include "wx/listbox.h"
3919 void wxGrid::Create()
3921 m_created
= false; // set to true by CreateGrid
3923 m_table
= (wxGridTableBase
*) NULL
;
3926 m_cellEditCtrlEnabled
= false;
3928 m_defaultCellAttr
= new wxGridCellAttr();
3930 // Set default cell attributes
3931 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3932 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3933 m_defaultCellAttr
->SetFont(GetFont());
3934 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3935 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3936 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3939 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3940 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3942 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3943 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3946 m_defaultCellAttr
->SetTextColour(
3947 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3948 m_defaultCellAttr
->SetBackgroundColour(
3949 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3954 m_currentCellCoords
= wxGridNoCellCoords
;
3956 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3957 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3959 // create the type registry
3960 m_typeRegistry
= new wxGridTypeRegistry
;
3963 // subwindow components that make up the wxGrid
3964 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3969 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3974 m_colLabelWin
= new wxGridColLabelWindow( this,
3979 m_gridWin
= new wxGridWindow( this,
3986 SetTargetWindow( m_gridWin
);
3989 wxColour gfg
= gva
.colFg
;
3990 wxColour gbg
= gva
.colBg
;
3991 wxColour lfg
= lva
.colFg
;
3992 wxColour lbg
= lva
.colBg
;
3994 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3995 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3996 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3997 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
3999 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4000 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4001 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4002 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4003 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4004 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4006 m_gridWin
->SetOwnForegroundColour(gfg
);
4007 m_gridWin
->SetOwnBackgroundColour(gbg
);
4013 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4014 wxGrid::wxGridSelectionModes selmode
)
4016 wxCHECK_MSG( !m_created
,
4018 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4020 m_numRows
= numRows
;
4021 m_numCols
= numCols
;
4023 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4024 m_table
->SetView( this );
4026 m_selection
= new wxGridSelection( this, selmode
);
4035 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4037 wxCHECK_RET( m_created
,
4038 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4040 m_selection
->SetSelectionMode( selmode
);
4043 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4045 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4046 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4048 return m_selection
->GetSelectionMode();
4051 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4052 wxGrid::wxGridSelectionModes selmode
)
4056 // stop all processing
4061 wxGridTableBase
*t
=m_table
;
4074 m_numRows
= table
->GetNumberRows();
4075 m_numCols
= table
->GetNumberCols();
4078 m_table
->SetView( this );
4081 m_selection
= new wxGridSelection( this, selmode
);
4094 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4095 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4097 if ( m_rowLabelWin
)
4099 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4103 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4106 m_labelTextColour
= wxColour( _T("BLACK") );
4109 m_attrCache
.row
= -1;
4110 m_attrCache
.col
= -1;
4111 m_attrCache
.attr
= NULL
;
4113 // TODO: something better than this ?
4115 m_labelFont
= this->GetFont();
4116 m_labelFont
.SetWeight( wxBOLD
);
4118 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4119 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4121 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4122 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4123 m_colLabelTextOrientation
= wxHORIZONTAL
;
4125 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4126 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4128 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4129 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4131 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4132 m_defaultRowHeight
+= 8;
4134 m_defaultRowHeight
+= 4;
4137 m_gridLineColour
= wxColour( 192,192,192 );
4138 m_gridLinesEnabled
= true;
4139 m_cellHighlightColour
= *wxBLACK
;
4140 m_cellHighlightPenWidth
= 2;
4141 m_cellHighlightROPenWidth
= 1;
4143 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4144 m_winCapture
= (wxWindow
*)NULL
;
4145 m_canDragRowSize
= true;
4146 m_canDragColSize
= true;
4147 m_canDragGridSize
= true;
4148 m_canDragCell
= false;
4150 m_dragRowOrCol
= -1;
4151 m_isDragging
= false;
4152 m_startDragPos
= wxDefaultPosition
;
4154 m_waitForSlowClick
= false;
4156 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4157 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4159 m_currentCellCoords
= wxGridNoCellCoords
;
4161 m_selectingTopLeft
= wxGridNoCellCoords
;
4162 m_selectingBottomRight
= wxGridNoCellCoords
;
4163 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4164 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4166 m_editable
= true; // default for whole grid
4168 m_inOnKeyDown
= false;
4175 // ----------------------------------------------------------------------------
4176 // the idea is to call these functions only when necessary because they create
4177 // quite big arrays which eat memory mostly unnecessary - in particular, if
4178 // default widths/heights are used for all rows/columns, we may not use these
4181 // with some extra code, it should be possible to only store the
4182 // widths/heights different from default ones but this will be done later...
4183 // ----------------------------------------------------------------------------
4185 void wxGrid::InitRowHeights()
4187 m_rowHeights
.Empty();
4188 m_rowBottoms
.Empty();
4190 m_rowHeights
.Alloc( m_numRows
);
4191 m_rowBottoms
.Alloc( m_numRows
);
4195 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4197 for ( int i
= 0; i
< m_numRows
; i
++ )
4199 rowBottom
+= m_defaultRowHeight
;
4200 m_rowBottoms
.Add( rowBottom
);
4204 void wxGrid::InitColWidths()
4206 m_colWidths
.Empty();
4207 m_colRights
.Empty();
4209 m_colWidths
.Alloc( m_numCols
);
4210 m_colRights
.Alloc( m_numCols
);
4213 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4215 for ( int i
= 0; i
< m_numCols
; i
++ )
4217 colRight
+= m_defaultColWidth
;
4218 m_colRights
.Add( colRight
);
4222 int wxGrid::GetColWidth(int col
) const
4224 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4227 int wxGrid::GetColLeft(int col
) const
4229 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4230 : m_colRights
[col
] - m_colWidths
[col
];
4233 int wxGrid::GetColRight(int col
) const
4235 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4239 int wxGrid::GetRowHeight(int row
) const
4241 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4244 int wxGrid::GetRowTop(int row
) const
4246 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4247 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4250 int wxGrid::GetRowBottom(int row
) const
4252 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4253 : m_rowBottoms
[row
];
4256 void wxGrid::CalcDimensions()
4259 GetClientSize( &cw
, &ch
);
4261 if ( m_rowLabelWin
->IsShown() )
4262 cw
-= m_rowLabelWidth
;
4263 if ( m_colLabelWin
->IsShown() )
4264 ch
-= m_colLabelHeight
;
4267 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4268 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4270 // take into account editor if shown
4271 if( IsCellEditControlShown() )
4274 int r
= m_currentCellCoords
.GetRow();
4275 int c
= m_currentCellCoords
.GetCol();
4276 int x
= GetColLeft(c
);
4277 int y
= GetRowTop(r
);
4279 // how big is the editor
4280 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4281 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4282 editor
->GetControl()->GetSize(&w2
, &h2
);
4285 if( w2
> w
) w
= w2
;
4286 if( h2
> h
) h
= h2
;
4291 // preserve (more or less) the previous position
4293 GetViewStart( &x
, &y
);
4295 // ensure the position is valid for the new scroll ranges
4297 x
= wxMax( w
- 1, 0 );
4299 y
= wxMax( h
- 1, 0 );
4301 // do set scrollbar parameters
4302 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4303 GetScrollX(w
), GetScrollY(h
), x
, y
,
4304 GetBatchCount() != 0);
4306 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4307 // still must reposition the children
4312 void wxGrid::CalcWindowSizes()
4314 // escape if the window is has not been fully created yet
4316 if ( m_cornerLabelWin
== NULL
)
4320 GetClientSize( &cw
, &ch
);
4322 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4323 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4325 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4326 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4328 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4329 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4331 if ( m_gridWin
&& m_gridWin
->IsShown() )
4332 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4336 // this is called when the grid table sends a message to say that it
4337 // has been redimensioned
4339 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4342 bool result
= false;
4344 // Clear the attribute cache as the attribute might refer to a different
4345 // cell than stored in the cache after adding/removing rows/columns.
4347 // By the same reasoning, the editor should be dismissed if columns are
4348 // added or removed. And for consistency, it should IMHO always be
4349 // removed, not only if the cell "underneath" it actually changes.
4350 // For now, I intentionally do not save the editor's content as the
4351 // cell it might want to save that stuff to might no longer exist.
4352 HideCellEditControl();
4354 // if we were using the default widths/heights so far, we must change them
4356 if ( m_colWidths
.IsEmpty() )
4361 if ( m_rowHeights
.IsEmpty() )
4367 switch ( msg
.GetId() )
4369 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4371 size_t pos
= msg
.GetCommandInt();
4372 int numRows
= msg
.GetCommandInt2();
4374 m_numRows
+= numRows
;
4376 if ( !m_rowHeights
.IsEmpty() )
4378 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4379 m_rowBottoms
.Insert( 0, pos
, numRows
);
4382 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4384 for ( i
= pos
; i
< m_numRows
; i
++ )
4386 bottom
+= m_rowHeights
[i
];
4387 m_rowBottoms
[i
] = bottom
;
4390 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4392 // if we have just inserted cols into an empty grid the current
4393 // cell will be undefined...
4395 SetCurrentCell( 0, 0 );
4399 m_selection
->UpdateRows( pos
, numRows
);
4400 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4402 attrProvider
->UpdateAttrRows( pos
, numRows
);
4404 if ( !GetBatchCount() )
4407 m_rowLabelWin
->Refresh();
4413 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4415 int numRows
= msg
.GetCommandInt();
4416 int oldNumRows
= m_numRows
;
4417 m_numRows
+= numRows
;
4419 if ( !m_rowHeights
.IsEmpty() )
4421 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4422 m_rowBottoms
.Add( 0, numRows
);
4425 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4427 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4429 bottom
+= m_rowHeights
[i
];
4430 m_rowBottoms
[i
] = bottom
;
4433 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4435 // if we have just inserted cols into an empty grid the current
4436 // cell will be undefined...
4438 SetCurrentCell( 0, 0 );
4440 if ( !GetBatchCount() )
4443 m_rowLabelWin
->Refresh();
4449 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4451 size_t pos
= msg
.GetCommandInt();
4452 int numRows
= msg
.GetCommandInt2();
4453 m_numRows
-= numRows
;
4455 if ( !m_rowHeights
.IsEmpty() )
4457 m_rowHeights
.RemoveAt( pos
, numRows
);
4458 m_rowBottoms
.RemoveAt( pos
, numRows
);
4461 for ( i
= 0; i
< m_numRows
; i
++ )
4463 h
+= m_rowHeights
[i
];
4464 m_rowBottoms
[i
] = h
;
4469 m_currentCellCoords
= wxGridNoCellCoords
;
4473 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4474 m_currentCellCoords
.Set( 0, 0 );
4478 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4479 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4481 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4482 // ifdef'd out following patch from Paul Gammans
4484 // No need to touch column attributes, unless we
4485 // removed _all_ rows, in this case, we remove
4486 // all column attributes.
4487 // I hate to do this here, but the
4488 // needed data is not available inside UpdateAttrRows.
4489 if ( !GetNumberRows() )
4490 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4493 if ( !GetBatchCount() )
4496 m_rowLabelWin
->Refresh();
4502 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4504 size_t pos
= msg
.GetCommandInt();
4505 int numCols
= msg
.GetCommandInt2();
4506 m_numCols
+= numCols
;
4508 if ( !m_colWidths
.IsEmpty() )
4510 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4511 m_colRights
.Insert( 0, pos
, numCols
);
4514 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4516 for ( i
= pos
; i
< m_numCols
; i
++ )
4518 right
+= m_colWidths
[i
];
4519 m_colRights
[i
] = right
;
4522 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4524 // if we have just inserted cols into an empty grid the current
4525 // cell will be undefined...
4527 SetCurrentCell( 0, 0 );
4531 m_selection
->UpdateCols( pos
, numCols
);
4532 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4534 attrProvider
->UpdateAttrCols( pos
, numCols
);
4535 if ( !GetBatchCount() )
4538 m_colLabelWin
->Refresh();
4545 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4547 int numCols
= msg
.GetCommandInt();
4548 int oldNumCols
= m_numCols
;
4549 m_numCols
+= numCols
;
4550 if ( !m_colWidths
.IsEmpty() )
4552 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4553 m_colRights
.Add( 0, numCols
);
4556 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4558 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4560 right
+= m_colWidths
[i
];
4561 m_colRights
[i
] = right
;
4564 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4566 // if we have just inserted cols into an empty grid the current
4567 // cell will be undefined...
4569 SetCurrentCell( 0, 0 );
4571 if ( !GetBatchCount() )
4574 m_colLabelWin
->Refresh();
4580 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4582 size_t pos
= msg
.GetCommandInt();
4583 int numCols
= msg
.GetCommandInt2();
4584 m_numCols
-= numCols
;
4586 if ( !m_colWidths
.IsEmpty() )
4588 m_colWidths
.RemoveAt( pos
, numCols
);
4589 m_colRights
.RemoveAt( pos
, numCols
);
4592 for ( i
= 0; i
< m_numCols
; i
++ )
4594 w
+= m_colWidths
[i
];
4600 m_currentCellCoords
= wxGridNoCellCoords
;
4604 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4605 m_currentCellCoords
.Set( 0, 0 );
4609 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4610 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4612 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4613 // ifdef'd out following patch from Paul Gammans
4615 // No need to touch row attributes, unless we
4616 // removed _all_ columns, in this case, we remove
4617 // all row attributes.
4618 // I hate to do this here, but the
4619 // needed data is not available inside UpdateAttrCols.
4620 if ( !GetNumberCols() )
4621 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4624 if ( !GetBatchCount() )
4627 m_colLabelWin
->Refresh();
4634 if (result
&& !GetBatchCount() )
4635 m_gridWin
->Refresh();
4640 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4642 wxRegionIterator
iter( reg
);
4645 wxArrayInt rowlabels
;
4652 // TODO: remove this when we can...
4653 // There is a bug in wxMotif that gives garbage update
4654 // rectangles if you jump-scroll a long way by clicking the
4655 // scrollbar with middle button. This is a work-around
4657 #if defined(__WXMOTIF__)
4659 m_gridWin
->GetClientSize( &cw
, &ch
);
4660 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4661 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4664 // logical bounds of update region
4667 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4668 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4670 // find the row labels within these bounds
4673 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4675 if ( GetRowBottom(row
) < top
)
4678 if ( GetRowTop(row
) > bottom
)
4681 rowlabels
.Add( row
);
4691 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4693 wxRegionIterator
iter( reg
);
4696 wxArrayInt colLabels
;
4703 // TODO: remove this when we can...
4704 // There is a bug in wxMotif that gives garbage update
4705 // rectangles if you jump-scroll a long way by clicking the
4706 // scrollbar with middle button. This is a work-around
4708 #if defined(__WXMOTIF__)
4710 m_gridWin
->GetClientSize( &cw
, &ch
);
4711 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4712 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4715 // logical bounds of update region
4718 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4719 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4721 // find the cells within these bounds
4724 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4726 if ( GetColRight(col
) < left
)
4729 if ( GetColLeft(col
) > right
)
4732 colLabels
.Add( col
);
4741 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4743 wxRegionIterator
iter( reg
);
4746 wxGridCellCoordsArray cellsExposed
;
4748 int left
, top
, right
, bottom
;
4753 // TODO: remove this when we can...
4754 // There is a bug in wxMotif that gives garbage update
4755 // rectangles if you jump-scroll a long way by clicking the
4756 // scrollbar with middle button. This is a work-around
4758 #if defined(__WXMOTIF__)
4760 m_gridWin
->GetClientSize( &cw
, &ch
);
4761 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4762 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4763 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4764 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4767 // logical bounds of update region
4769 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4770 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4772 // find the cells within these bounds
4775 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4777 if ( GetRowBottom(row
) <= top
)
4780 if ( GetRowTop(row
) > bottom
)
4783 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4785 if ( GetColRight(col
) <= left
)
4788 if ( GetColLeft(col
) > right
)
4791 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4798 return cellsExposed
;
4802 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4805 wxPoint
pos( event
.GetPosition() );
4806 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4808 if ( event
.Dragging() )
4812 m_isDragging
= true;
4813 m_rowLabelWin
->CaptureMouse();
4816 if ( event
.LeftIsDown() )
4818 switch( m_cursorMode
)
4820 case WXGRID_CURSOR_RESIZE_ROW
:
4822 int cw
, ch
, left
, dummy
;
4823 m_gridWin
->GetClientSize( &cw
, &ch
);
4824 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4826 wxClientDC
dc( m_gridWin
);
4829 GetRowTop(m_dragRowOrCol
) +
4830 GetRowMinimalHeight(m_dragRowOrCol
) );
4831 dc
.SetLogicalFunction(wxINVERT
);
4832 if ( m_dragLastPos
>= 0 )
4834 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4836 dc
.DrawLine( left
, y
, left
+cw
, y
);
4841 case WXGRID_CURSOR_SELECT_ROW
:
4842 if ( (row
= YToRow( y
)) >= 0 )
4846 m_selection
->SelectRow( row
,
4847 event
.ControlDown(),
4854 // default label to suppress warnings about "enumeration value
4855 // 'xxx' not handled in switch
4863 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4868 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4869 m_isDragging
= false;
4872 // ------------ Entering or leaving the window
4874 if ( event
.Entering() || event
.Leaving() )
4876 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4880 // ------------ Left button pressed
4882 else if ( event
.LeftDown() )
4884 // don't send a label click event for a hit on the
4885 // edge of the row label - this is probably the user
4886 // wanting to resize the row
4888 if ( YToEdgeOfRow(y
) < 0 )
4892 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4894 if ( !event
.ShiftDown() && !event
.ControlDown() )
4898 if ( event
.ShiftDown() )
4900 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4903 GetNumberCols() - 1,
4904 event
.ControlDown(),
4911 m_selection
->SelectRow( row
,
4912 event
.ControlDown(),
4919 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4924 // starting to drag-resize a row
4926 if ( CanDragRowSize() )
4927 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4932 // ------------ Left double click
4934 else if (event
.LeftDClick() )
4936 int row
= YToEdgeOfRow(y
);
4941 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4943 // no default action at the moment
4948 // adjust row height depending on label text
4949 AutoSizeRowLabelSize( row
);
4951 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4957 // ------------ Left button released
4959 else if ( event
.LeftUp() )
4961 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4963 DoEndDragResizeRow();
4965 // Note: we are ending the event *after* doing
4966 // default processing in this case
4968 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4971 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4976 // ------------ Right button down
4978 else if ( event
.RightDown() )
4982 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4984 // no default action at the moment
4989 // ------------ Right double click
4991 else if ( event
.RightDClick() )
4995 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4997 // no default action at the moment
5002 // ------------ No buttons down and mouse moving
5004 else if ( event
.Moving() )
5006 m_dragRowOrCol
= YToEdgeOfRow( y
);
5007 if ( m_dragRowOrCol
>= 0 )
5009 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5011 // don't capture the mouse yet
5012 if ( CanDragRowSize() )
5013 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5016 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5018 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5024 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5027 wxPoint
pos( event
.GetPosition() );
5028 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5030 if ( event
.Dragging() )
5034 m_isDragging
= true;
5035 m_colLabelWin
->CaptureMouse();
5038 if ( event
.LeftIsDown() )
5040 switch( m_cursorMode
)
5042 case WXGRID_CURSOR_RESIZE_COL
:
5044 int cw
, ch
, dummy
, top
;
5045 m_gridWin
->GetClientSize( &cw
, &ch
);
5046 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5048 wxClientDC
dc( m_gridWin
);
5051 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5052 GetColMinimalWidth(m_dragRowOrCol
));
5053 dc
.SetLogicalFunction(wxINVERT
);
5054 if ( m_dragLastPos
>= 0 )
5056 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5058 dc
.DrawLine( x
, top
, x
, top
+ch
);
5063 case WXGRID_CURSOR_SELECT_COL
:
5064 if ( (col
= XToCol( x
)) >= 0 )
5068 m_selection
->SelectCol( col
,
5069 event
.ControlDown(),
5076 // default label to suppress warnings about "enumeration value
5077 // 'xxx' not handled in switch
5085 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5090 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5091 m_isDragging
= false;
5094 // ------------ Entering or leaving the window
5096 if ( event
.Entering() || event
.Leaving() )
5098 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5102 // ------------ Left button pressed
5104 else if ( event
.LeftDown() )
5106 // don't send a label click event for a hit on the
5107 // edge of the col label - this is probably the user
5108 // wanting to resize the col
5110 if ( XToEdgeOfCol(x
) < 0 )
5114 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5116 if ( !event
.ShiftDown() && !event
.ControlDown() )
5120 if ( event
.ShiftDown() )
5122 m_selection
->SelectBlock( 0,
5123 m_currentCellCoords
.GetCol(),
5124 GetNumberRows() - 1, col
,
5125 event
.ControlDown(),
5132 m_selection
->SelectCol( col
,
5133 event
.ControlDown(),
5140 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5145 // starting to drag-resize a col
5147 if ( CanDragColSize() )
5148 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5153 // ------------ Left double click
5155 if ( event
.LeftDClick() )
5157 int col
= XToEdgeOfCol(x
);
5162 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5164 // no default action at the moment
5169 // adjust column width depending on label text
5170 AutoSizeColLabelSize( col
);
5172 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5178 // ------------ Left button released
5180 else if ( event
.LeftUp() )
5182 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5184 DoEndDragResizeCol();
5186 // Note: we are ending the event *after* doing
5187 // default processing in this case
5189 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5192 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5197 // ------------ Right button down
5199 else if ( event
.RightDown() )
5203 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5205 // no default action at the moment
5210 // ------------ Right double click
5212 else if ( event
.RightDClick() )
5216 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5218 // no default action at the moment
5223 // ------------ No buttons down and mouse moving
5225 else if ( event
.Moving() )
5227 m_dragRowOrCol
= XToEdgeOfCol( x
);
5228 if ( m_dragRowOrCol
>= 0 )
5230 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5232 // don't capture the cursor yet
5233 if ( CanDragColSize() )
5234 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5237 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5239 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5245 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5247 if ( event
.LeftDown() )
5249 // indicate corner label by having both row and
5252 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5258 else if ( event
.LeftDClick() )
5260 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5263 else if ( event
.RightDown() )
5265 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5267 // no default action at the moment
5271 else if ( event
.RightDClick() )
5273 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5275 // no default action at the moment
5280 void wxGrid::ChangeCursorMode(CursorMode mode
,
5285 static const wxChar
*cursorModes
[] =
5294 wxLogTrace(_T("grid"),
5295 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5296 win
== m_colLabelWin
? _T("colLabelWin")
5297 : win
? _T("rowLabelWin")
5299 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5300 #endif // __WXDEBUG__
5302 if ( mode
== m_cursorMode
&&
5303 win
== m_winCapture
&&
5304 captureMouse
== (m_winCapture
!= NULL
))
5309 // by default use the grid itself
5315 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5316 m_winCapture
= (wxWindow
*)NULL
;
5319 m_cursorMode
= mode
;
5321 switch ( m_cursorMode
)
5323 case WXGRID_CURSOR_RESIZE_ROW
:
5324 win
->SetCursor( m_rowResizeCursor
);
5327 case WXGRID_CURSOR_RESIZE_COL
:
5328 win
->SetCursor( m_colResizeCursor
);
5332 win
->SetCursor( *wxSTANDARD_CURSOR
);
5335 // we need to capture mouse when resizing
5336 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5337 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5339 if ( captureMouse
&& resize
)
5341 win
->CaptureMouse();
5346 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5349 wxPoint
pos( event
.GetPosition() );
5350 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5352 wxGridCellCoords coords
;
5353 XYToCell( x
, y
, coords
);
5355 int cell_rows
, cell_cols
;
5356 bool isFirstDrag
= !m_isDragging
;
5357 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5358 if ((cell_rows
< 0) || (cell_cols
< 0))
5360 coords
.SetRow(coords
.GetRow() + cell_rows
);
5361 coords
.SetCol(coords
.GetCol() + cell_cols
);
5364 if ( event
.Dragging() )
5366 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5368 // Don't start doing anything until the mouse has been drug at
5369 // least 3 pixels in any direction...
5372 if (m_startDragPos
== wxDefaultPosition
)
5374 m_startDragPos
= pos
;
5377 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5381 m_isDragging
= true;
5382 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5384 // Hide the edit control, so it
5385 // won't interfer with drag-shrinking.
5386 if ( IsCellEditControlShown() )
5388 HideCellEditControl();
5389 SaveEditControlValue();
5392 // Have we captured the mouse yet?
5395 m_winCapture
= m_gridWin
;
5396 m_winCapture
->CaptureMouse();
5399 if ( coords
!= wxGridNoCellCoords
)
5401 if ( event
.ControlDown() )
5403 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5404 m_selectingKeyboard
= coords
;
5405 HighlightBlock ( m_selectingKeyboard
, coords
);
5407 else if ( CanDragCell() )
5411 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5412 m_selectingKeyboard
= coords
;
5414 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5422 if ( !IsSelection() )
5424 HighlightBlock( coords
, coords
);
5428 HighlightBlock( m_currentCellCoords
, coords
);
5432 if (! IsVisible(coords
))
5434 MakeCellVisible(coords
);
5435 // TODO: need to introduce a delay or something here. The
5436 // scrolling is way to fast, at least on MSW - also on GTK.
5440 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5442 int cw
, ch
, left
, dummy
;
5443 m_gridWin
->GetClientSize( &cw
, &ch
);
5444 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5446 wxClientDC
dc( m_gridWin
);
5448 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5449 GetRowMinimalHeight(m_dragRowOrCol
) );
5450 dc
.SetLogicalFunction(wxINVERT
);
5451 if ( m_dragLastPos
>= 0 )
5453 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5455 dc
.DrawLine( left
, y
, left
+cw
, y
);
5458 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5460 int cw
, ch
, dummy
, top
;
5461 m_gridWin
->GetClientSize( &cw
, &ch
);
5462 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5464 wxClientDC
dc( m_gridWin
);
5466 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5467 GetColMinimalWidth(m_dragRowOrCol
) );
5468 dc
.SetLogicalFunction(wxINVERT
);
5469 if ( m_dragLastPos
>= 0 )
5471 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5473 dc
.DrawLine( x
, top
, x
, top
+ch
);
5480 m_isDragging
= false;
5481 m_startDragPos
= wxDefaultPosition
;
5483 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5484 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5487 if ( event
.Entering() || event
.Leaving() )
5489 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5490 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5495 // ------------ Left button pressed
5497 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5499 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5504 if ( !event
.ControlDown() )
5506 if ( event
.ShiftDown() )
5510 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5511 m_currentCellCoords
.GetCol(),
5514 event
.ControlDown(),
5520 else if ( XToEdgeOfCol(x
) < 0 &&
5521 YToEdgeOfRow(y
) < 0 )
5523 DisableCellEditControl();
5524 MakeCellVisible( coords
);
5526 if ( event
.ControlDown() )
5530 m_selection
->ToggleCellSelection( coords
.GetRow(),
5532 event
.ControlDown(),
5537 m_selectingTopLeft
= wxGridNoCellCoords
;
5538 m_selectingBottomRight
= wxGridNoCellCoords
;
5539 m_selectingKeyboard
= coords
;
5543 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5544 SetCurrentCell( coords
);
5547 if ( m_selection
->GetSelectionMode() !=
5548 wxGrid::wxGridSelectCells
)
5550 HighlightBlock( coords
, coords
);
5559 // ------------ Left double click
5561 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5563 DisableCellEditControl();
5565 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5567 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5572 // we want double click to select a cell and start editing
5573 // (i.e. to behave in same way as sequence of two slow clicks):
5574 m_waitForSlowClick
= true;
5581 // ------------ Left button released
5583 else if ( event
.LeftUp() )
5585 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5589 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5590 m_winCapture
= NULL
;
5593 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5596 EnableCellEditControl();
5598 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5599 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5600 editor
->StartingClick();
5604 m_waitForSlowClick
= false;
5606 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5607 m_selectingBottomRight
!= wxGridNoCellCoords
)
5611 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5612 m_selectingTopLeft
.GetCol(),
5613 m_selectingBottomRight
.GetRow(),
5614 m_selectingBottomRight
.GetCol(),
5615 event
.ControlDown(),
5621 m_selectingTopLeft
= wxGridNoCellCoords
;
5622 m_selectingBottomRight
= wxGridNoCellCoords
;
5624 // Show the edit control, if it has been hidden for
5626 ShowCellEditControl();
5629 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5631 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5632 DoEndDragResizeRow();
5634 // Note: we are ending the event *after* doing
5635 // default processing in this case
5637 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5639 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5641 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5642 DoEndDragResizeCol();
5644 // Note: we are ending the event *after* doing
5645 // default processing in this case
5647 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5654 // ------------ Right button down
5656 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5658 DisableCellEditControl();
5659 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5664 // no default action at the moment
5669 // ------------ Right double click
5671 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5673 DisableCellEditControl();
5674 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5679 // no default action at the moment
5683 // ------------ Moving and no button action
5685 else if ( event
.Moving() && !event
.IsButton() )
5687 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5689 // out of grid cell area
5690 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5694 int dragRow
= YToEdgeOfRow( y
);
5695 int dragCol
= XToEdgeOfCol( x
);
5697 // Dragging on the corner of a cell to resize in both
5698 // directions is not implemented yet...
5700 if ( dragRow
>= 0 && dragCol
>= 0 )
5702 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5708 m_dragRowOrCol
= dragRow
;
5710 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5712 if ( CanDragRowSize() && CanDragGridSize() )
5713 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5718 m_dragRowOrCol
= dragCol
;
5726 m_dragRowOrCol
= dragCol
;
5728 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5730 if ( CanDragColSize() && CanDragGridSize() )
5731 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5737 // Neither on a row or col edge
5739 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5741 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5747 void wxGrid::DoEndDragResizeRow()
5749 if ( m_dragLastPos
>= 0 )
5751 // erase the last line and resize the row
5753 int cw
, ch
, left
, dummy
;
5754 m_gridWin
->GetClientSize( &cw
, &ch
);
5755 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5757 wxClientDC
dc( m_gridWin
);
5759 dc
.SetLogicalFunction( wxINVERT
);
5760 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5761 HideCellEditControl();
5762 SaveEditControlValue();
5764 int rowTop
= GetRowTop(m_dragRowOrCol
);
5765 SetRowSize( m_dragRowOrCol
,
5766 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5768 if ( !GetBatchCount() )
5770 // Only needed to get the correct rect.y:
5771 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5773 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5774 rect
.width
= m_rowLabelWidth
;
5775 rect
.height
= ch
- rect
.y
;
5776 m_rowLabelWin
->Refresh( true, &rect
);
5778 // if there is a multicell block, paint all of it
5781 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5782 int leftCol
= XToCol(left
);
5783 int rightCol
= internalXToCol(left
+cw
);
5786 for (i
=leftCol
; i
<rightCol
; i
++)
5788 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5789 if (cell_rows
< subtract_rows
)
5790 subtract_rows
= cell_rows
;
5792 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5793 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5794 rect
.height
= ch
- rect
.y
;
5797 m_gridWin
->Refresh( false, &rect
);
5800 ShowCellEditControl();
5805 void wxGrid::DoEndDragResizeCol()
5807 if ( m_dragLastPos
>= 0 )
5809 // erase the last line and resize the col
5811 int cw
, ch
, dummy
, top
;
5812 m_gridWin
->GetClientSize( &cw
, &ch
);
5813 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5815 wxClientDC
dc( m_gridWin
);
5817 dc
.SetLogicalFunction( wxINVERT
);
5818 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5819 HideCellEditControl();
5820 SaveEditControlValue();
5822 int colLeft
= GetColLeft(m_dragRowOrCol
);
5823 SetColSize( m_dragRowOrCol
,
5824 wxMax( m_dragLastPos
- colLeft
,
5825 GetColMinimalWidth(m_dragRowOrCol
) ) );
5827 if ( !GetBatchCount() )
5829 // Only needed to get the correct rect.x:
5830 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5832 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5833 rect
.width
= cw
- rect
.x
;
5834 rect
.height
= m_colLabelHeight
;
5835 m_colLabelWin
->Refresh( true, &rect
);
5837 // if there is a multicell block, paint all of it
5840 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5841 int topRow
= YToRow(top
);
5842 int bottomRow
= internalYToRow(top
+cw
);
5845 for (i
=topRow
; i
<bottomRow
; i
++)
5847 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5848 if (cell_cols
< subtract_cols
)
5849 subtract_cols
= cell_cols
;
5851 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5852 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5853 rect
.width
= cw
- rect
.x
;
5856 m_gridWin
->Refresh( false, &rect
);
5859 ShowCellEditControl();
5866 // ------ interaction with data model
5868 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5870 switch ( msg
.GetId() )
5872 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5873 return GetModelValues();
5875 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5876 return SetModelValues();
5878 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5879 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5880 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5881 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5882 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5883 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5884 return Redimension( msg
);
5893 // The behaviour of this function depends on the grid table class
5894 // Clear() function. For the default wxGridStringTable class the
5895 // behavious is to replace all cell contents with wxEmptyString but
5896 // not to change the number of rows or cols.
5898 void wxGrid::ClearGrid()
5902 if (IsCellEditControlEnabled())
5903 DisableCellEditControl();
5906 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5911 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5913 // TODO: something with updateLabels flag
5917 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5923 if (IsCellEditControlEnabled())
5924 DisableCellEditControl();
5926 bool done
= m_table
->InsertRows( pos
, numRows
);
5929 // the table will have sent the results of the insert row
5930 // operation to this view object as a grid table message
5936 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5938 // TODO: something with updateLabels flag
5942 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5948 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5950 // the table will have sent the results of the append row
5951 // operation to this view object as a grid table message
5957 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5959 // TODO: something with updateLabels flag
5963 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5969 if (IsCellEditControlEnabled())
5970 DisableCellEditControl();
5972 bool done
= m_table
->DeleteRows( pos
, numRows
);
5974 // the table will have sent the results of the delete row
5975 // operation to this view object as a grid table message
5981 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5983 // TODO: something with updateLabels flag
5987 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5993 if (IsCellEditControlEnabled())
5994 DisableCellEditControl();
5996 bool done
= m_table
->InsertCols( pos
, numCols
);
5998 // the table will have sent the results of the insert col
5999 // operation to this view object as a grid table message
6005 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6007 // TODO: something with updateLabels flag
6011 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6017 bool done
= m_table
->AppendCols( numCols
);
6019 // the table will have sent the results of the append col
6020 // operation to this view object as a grid table message
6026 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6028 // TODO: something with updateLabels flag
6032 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6038 if (IsCellEditControlEnabled())
6039 DisableCellEditControl();
6041 bool done
= m_table
->DeleteCols( pos
, numCols
);
6043 // the table will have sent the results of the delete col
6044 // operation to this view object as a grid table message
6052 // ----- event handlers
6055 // Generate a grid event based on a mouse event and
6056 // return the result of ProcessEvent()
6058 int wxGrid::SendEvent( const wxEventType type
,
6060 wxMouseEvent
& mouseEv
)
6065 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6067 int rowOrCol
= (row
== -1 ? col
: row
);
6069 wxGridSizeEvent
gridEvt( GetId(),
6073 mouseEv
.GetX() + GetRowLabelSize(),
6074 mouseEv
.GetY() + GetColLabelSize(),
6075 mouseEv
.ControlDown(),
6076 mouseEv
.ShiftDown(),
6078 mouseEv
.MetaDown() );
6080 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6081 vetoed
= !gridEvt
.IsAllowed();
6083 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6085 // Right now, it should _never_ end up here!
6086 wxGridRangeSelectEvent
gridEvt( GetId(),
6090 m_selectingBottomRight
,
6092 mouseEv
.ControlDown(),
6093 mouseEv
.ShiftDown(),
6095 mouseEv
.MetaDown() );
6097 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6098 vetoed
= !gridEvt
.IsAllowed();
6102 wxGridEvent
gridEvt( GetId(),
6106 mouseEv
.GetX() + GetRowLabelSize(),
6107 mouseEv
.GetY() + GetColLabelSize(),
6109 mouseEv
.ControlDown(),
6110 mouseEv
.ShiftDown(),
6112 mouseEv
.MetaDown() );
6113 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6114 vetoed
= !gridEvt
.IsAllowed();
6117 // A Veto'd event may not be `claimed' so test this first
6118 if (vetoed
) return -1;
6119 return claimed
? 1 : 0;
6123 // Generate a grid event of specified type and return the result
6124 // of ProcessEvent().
6126 int wxGrid::SendEvent( const wxEventType type
,
6132 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6134 int rowOrCol
= (row
== -1 ? col
: row
);
6136 wxGridSizeEvent
gridEvt( GetId(),
6141 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6142 vetoed
= !gridEvt
.IsAllowed();
6146 wxGridEvent
gridEvt( GetId(),
6151 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6152 vetoed
= !gridEvt
.IsAllowed();
6155 // A Veto'd event may not be `claimed' so test this first
6156 if (vetoed
) return -1;
6157 return claimed
? 1 : 0;
6161 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6163 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6166 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6168 // Don't do anything if between Begin/EndBatch...
6169 // EndBatch() will do all this on the last nested one anyway.
6170 if (! GetBatchCount())
6172 // Refresh to get correct scrolled position:
6173 wxScrolledWindow::Refresh(eraseb
,rect
);
6177 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6178 int width_label
, width_cell
, height_label
, height_cell
;
6181 //Copy rectangle can get scroll offsets..
6182 rect_x
= rect
->GetX();
6183 rect_y
= rect
->GetY();
6184 rectWidth
= rect
->GetWidth();
6185 rectHeight
= rect
->GetHeight();
6187 width_label
= m_rowLabelWidth
- rect_x
;
6188 if (width_label
> rectWidth
) width_label
= rectWidth
;
6190 height_label
= m_colLabelHeight
- rect_y
;
6191 if (height_label
> rectHeight
) height_label
= rectHeight
;
6193 if (rect_x
> m_rowLabelWidth
)
6195 x
= rect_x
- m_rowLabelWidth
;
6196 width_cell
= rectWidth
;
6201 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6204 if (rect_y
> m_colLabelHeight
)
6206 y
= rect_y
- m_colLabelHeight
;
6207 height_cell
= rectHeight
;
6212 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6215 // Paint corner label part intersecting rect.
6216 if ( width_label
> 0 && height_label
> 0 )
6218 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6219 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6222 // Paint col labels part intersecting rect.
6223 if ( width_cell
> 0 && height_label
> 0 )
6225 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6226 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6229 // Paint row labels part intersecting rect.
6230 if ( width_label
> 0 && height_cell
> 0 )
6232 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6233 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6236 // Paint cell area part intersecting rect.
6237 if ( width_cell
> 0 && height_cell
> 0 )
6239 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6240 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6245 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6246 m_colLabelWin
->Refresh(eraseb
, NULL
);
6247 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6248 m_gridWin
->Refresh(eraseb
, NULL
);
6253 void wxGrid::OnSize( wxSizeEvent
& event
)
6255 // position the child windows
6258 // don't call CalcDimensions() from here, the base class handles the size
6264 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6266 if ( m_inOnKeyDown
)
6268 // shouldn't be here - we are going round in circles...
6270 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6273 m_inOnKeyDown
= true;
6275 // propagate the event up and see if it gets processed
6277 wxWindow
*parent
= GetParent();
6278 wxKeyEvent
keyEvt( event
);
6279 keyEvt
.SetEventObject( parent
);
6281 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6284 // try local handlers
6286 switch ( event
.GetKeyCode() )
6289 if ( event
.ControlDown() )
6291 MoveCursorUpBlock( event
.ShiftDown() );
6295 MoveCursorUp( event
.ShiftDown() );
6300 if ( event
.ControlDown() )
6302 MoveCursorDownBlock( event
.ShiftDown() );
6306 MoveCursorDown( event
.ShiftDown() );
6311 if ( event
.ControlDown() )
6313 MoveCursorLeftBlock( event
.ShiftDown() );
6317 MoveCursorLeft( event
.ShiftDown() );
6322 if ( event
.ControlDown() )
6324 MoveCursorRightBlock( event
.ShiftDown() );
6328 MoveCursorRight( event
.ShiftDown() );
6333 case WXK_NUMPAD_ENTER
:
6334 if ( event
.ControlDown() )
6336 event
.Skip(); // to let the edit control have the return
6340 if ( GetGridCursorRow() < GetNumberRows()-1 )
6342 MoveCursorDown( event
.ShiftDown() );
6346 // at the bottom of a column
6347 DisableCellEditControl();
6357 if (event
.ShiftDown())
6359 if ( GetGridCursorCol() > 0 )
6361 MoveCursorLeft( false );
6366 DisableCellEditControl();
6371 if ( GetGridCursorCol() < GetNumberCols()-1 )
6373 MoveCursorRight( false );
6378 DisableCellEditControl();
6384 if ( event
.ControlDown() )
6386 MakeCellVisible( 0, 0 );
6387 SetCurrentCell( 0, 0 );
6396 if ( event
.ControlDown() )
6398 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6399 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6416 if ( event
.ControlDown() )
6420 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6421 m_currentCellCoords
.GetCol(),
6422 event
.ControlDown(),
6429 if ( !IsEditable() )
6431 MoveCursorRight( false );
6434 // Otherwise fall through to default
6437 // is it possible to edit the current cell at all?
6438 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6440 // yes, now check whether the cells editor accepts the key
6441 int row
= m_currentCellCoords
.GetRow();
6442 int col
= m_currentCellCoords
.GetCol();
6443 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6444 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6446 // <F2> is special and will always start editing, for
6447 // other keys - ask the editor itself
6448 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6449 || editor
->IsAcceptedKey(event
) )
6451 // ensure cell is visble
6452 MakeCellVisible(row
, col
);
6453 EnableCellEditControl();
6455 // a problem can arise if the cell is not completely
6456 // visible (even after calling MakeCellVisible the
6457 // control is not created and calling StartingKey will
6459 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6471 // let others process char events with modifiers or all
6472 // char events for readonly cells
6479 m_inOnKeyDown
= false;
6482 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6484 // try local handlers
6486 if ( event
.GetKeyCode() == WXK_SHIFT
)
6488 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6489 m_selectingBottomRight
!= wxGridNoCellCoords
)
6493 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6494 m_selectingTopLeft
.GetCol(),
6495 m_selectingBottomRight
.GetRow(),
6496 m_selectingBottomRight
.GetCol(),
6497 event
.ControlDown(),
6504 m_selectingTopLeft
= wxGridNoCellCoords
;
6505 m_selectingBottomRight
= wxGridNoCellCoords
;
6506 m_selectingKeyboard
= wxGridNoCellCoords
;
6510 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6514 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6516 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6518 // the event has been intercepted - do nothing
6522 wxClientDC
dc(m_gridWin
);
6525 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6527 DisableCellEditControl();
6529 if ( IsVisible( m_currentCellCoords
, false ) )
6532 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6533 if ( !m_gridLinesEnabled
)
6541 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6543 // Otherwise refresh redraws the highlight!
6544 m_currentCellCoords
= coords
;
6546 DrawGridCellArea(dc
,cells
);
6547 DrawAllGridLines( dc
, r
);
6551 m_currentCellCoords
= coords
;
6553 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6554 DrawCellHighlight(dc
, attr
);
6559 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6562 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6566 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6569 rightCol
= GetNumberCols() - 1;
6571 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6574 bottomRow
= GetNumberRows() - 1;
6578 if ( topRow
> bottomRow
)
6585 if ( leftCol
> rightCol
)
6592 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6593 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6595 // First the case that we selected a completely new area
6596 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6597 m_selectingBottomRight
== wxGridNoCellCoords
)
6600 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6601 wxGridCellCoords ( bottomRow
, rightCol
) );
6602 m_gridWin
->Refresh( false, &rect
);
6604 // Now handle changing an existing selection area.
6605 else if ( m_selectingTopLeft
!= updateTopLeft
||
6606 m_selectingBottomRight
!= updateBottomRight
)
6608 // Compute two optimal update rectangles:
6609 // Either one rectangle is a real subset of the
6610 // other, or they are (almost) disjoint!
6612 bool need_refresh
[4];
6616 need_refresh
[3] = false;
6619 // Store intermediate values
6620 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6621 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6622 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6623 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6625 // Determine the outer/inner coordinates.
6626 if (oldLeft
> leftCol
)
6632 if (oldTop
> topRow
)
6638 if (oldRight
< rightCol
)
6641 oldRight
= rightCol
;
6644 if (oldBottom
< bottomRow
)
6647 oldBottom
= bottomRow
;
6651 // Now, either the stuff marked old is the outer
6652 // rectangle or we don't have a situation where one
6653 // is contained in the other.
6655 if ( oldLeft
< leftCol
)
6657 // Refresh the newly selected or deselected
6658 // area to the left of the old or new selection.
6659 need_refresh
[0] = true;
6660 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6662 wxGridCellCoords ( oldBottom
,
6666 if ( oldTop
< topRow
)
6668 // Refresh the newly selected or deselected
6669 // area above the old or new selection.
6670 need_refresh
[1] = true;
6671 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6673 wxGridCellCoords ( topRow
- 1,
6677 if ( oldRight
> rightCol
)
6679 // Refresh the newly selected or deselected
6680 // area to the right of the old or new selection.
6681 need_refresh
[2] = true;
6682 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6684 wxGridCellCoords ( oldBottom
,
6688 if ( oldBottom
> bottomRow
)
6690 // Refresh the newly selected or deselected
6691 // area below the old or new selection.
6692 need_refresh
[3] = true;
6693 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6695 wxGridCellCoords ( oldBottom
,
6699 // various Refresh() calls
6700 for (i
= 0; i
< 4; i
++ )
6701 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6702 m_gridWin
->Refresh( false, &(rect
[i
]) );
6705 m_selectingTopLeft
= updateTopLeft
;
6706 m_selectingBottomRight
= updateBottomRight
;
6710 // ------ functions to get/send data (see also public functions)
6713 bool wxGrid::GetModelValues()
6715 // Hide the editor, so it won't hide a changed value.
6716 HideCellEditControl();
6720 // all we need to do is repaint the grid
6722 m_gridWin
->Refresh();
6730 bool wxGrid::SetModelValues()
6734 // Disable the editor, so it won't hide a changed value.
6735 // Do we also want to save the current value of the editor first?
6737 DisableCellEditControl();
6741 for ( row
= 0; row
< m_numRows
; row
++ )
6743 for ( col
= 0; col
< m_numCols
; col
++ )
6745 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6757 // Note - this function only draws cells that are in the list of
6758 // exposed cells (usually set from the update region by
6759 // CalcExposedCells)
6761 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6763 if ( !m_numRows
|| !m_numCols
) return;
6765 int i
, numCells
= cells
.GetCount();
6766 int row
, col
, cell_rows
, cell_cols
;
6767 wxGridCellCoordsArray redrawCells
;
6769 for ( i
= numCells
-1; i
>= 0; i
-- )
6771 row
= cells
[i
].GetRow();
6772 col
= cells
[i
].GetCol();
6773 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6775 // If this cell is part of a multicell block, find owner for repaint
6776 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6778 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6779 bool marked
= false;
6780 for ( int j
= 0; j
< numCells
; j
++ )
6782 if ( cell
== cells
[j
] )
6790 int count
= redrawCells
.GetCount();
6791 for (int j
= 0; j
< count
; j
++)
6793 if ( cell
== redrawCells
[j
] )
6799 if (!marked
) redrawCells
.Add( cell
);
6801 continue; // don't bother drawing this cell
6804 // If this cell is empty, find cell to left that might want to overflow
6805 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6807 for ( int l
= 0; l
< cell_rows
; l
++ )
6809 // find a cell in this row to left alreay marked for repaint
6811 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6812 if ((redrawCells
[k
].GetCol() < left
) &&
6813 (redrawCells
[k
].GetRow() == row
))
6814 left
=redrawCells
[k
].GetCol();
6816 if (left
== col
) left
= 0; // oh well
6818 for (int j
= col
-1; j
>= left
; j
--)
6820 if (!m_table
->IsEmptyCell(row
+l
, j
))
6822 if (GetCellOverflow(row
+l
, j
))
6824 wxGridCellCoords
cell(row
+l
, j
);
6825 bool marked
= false;
6827 for (int k
= 0; k
< numCells
; k
++)
6829 if ( cell
== cells
[k
] )
6837 int count
= redrawCells
.GetCount();
6838 for (int k
= 0; k
< count
; k
++)
6840 if ( cell
== redrawCells
[k
] )
6846 if (!marked
) redrawCells
.Add( cell
);
6854 DrawCell( dc
, cells
[i
] );
6857 numCells
= redrawCells
.GetCount();
6859 for ( i
= numCells
- 1; i
>= 0; i
-- )
6861 DrawCell( dc
, redrawCells
[i
] );
6866 void wxGrid::DrawGridSpace( wxDC
& dc
)
6869 m_gridWin
->GetClientSize( &cw
, &ch
);
6872 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6874 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6875 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6877 if ( right
> rightCol
|| bottom
> bottomRow
)
6880 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6882 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6883 dc
.SetPen( *wxTRANSPARENT_PEN
);
6885 if ( right
> rightCol
)
6887 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6890 if ( bottom
> bottomRow
)
6892 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6898 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6900 int row
= coords
.GetRow();
6901 int col
= coords
.GetCol();
6903 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6906 // we draw the cell border ourselves
6907 #if !WXGRID_DRAW_LINES
6908 if ( m_gridLinesEnabled
)
6909 DrawCellBorder( dc
, coords
);
6912 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6914 bool isCurrent
= coords
== m_currentCellCoords
;
6916 wxRect rect
= CellToRect( row
, col
);
6918 // if the editor is shown, we should use it and not the renderer
6919 // Note: However, only if it is really _shown_, i.e. not hidden!
6920 if ( isCurrent
&& IsCellEditControlShown() )
6922 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6923 editor
->PaintBackground(rect
, attr
);
6928 // but all the rest is drawn by the cell renderer and hence may be
6930 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6931 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6938 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6940 int row
= m_currentCellCoords
.GetRow();
6941 int col
= m_currentCellCoords
.GetCol();
6943 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6946 wxRect rect
= CellToRect(row
, col
);
6948 // hmmm... what could we do here to show that the cell is disabled?
6949 // for now, I just draw a thinner border than for the other ones, but
6950 // it doesn't look really good
6952 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6956 // The center of th drawn line is where the position/width/height of
6957 // the rectangle is actually at, (on wxMSW atr least,) so we will
6958 // reduce the size of the rectangle to compensate for the thickness of
6959 // the line. If this is too strange on non wxMSW platforms then
6960 // please #ifdef this appropriately.
6961 rect
.x
+= penWidth
/2;
6962 rect
.y
+= penWidth
/2;
6963 rect
.width
-= penWidth
-1;
6964 rect
.height
-= penWidth
-1;
6967 // Now draw the rectangle
6968 // use the cellHighlightColour if the cell is inside a selection, this
6969 // will ensure the cell is always visible.
6970 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6971 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6972 dc
.DrawRectangle(rect
);
6976 // VZ: my experiments with 3d borders...
6978 // how to properly set colours for arbitrary bg?
6979 wxCoord x1
= rect
.x
,
6981 x2
= rect
.x
+ rect
.width
-1,
6982 y2
= rect
.y
+ rect
.height
-1;
6984 dc
.SetPen(*wxWHITE_PEN
);
6985 dc
.DrawLine(x1
, y1
, x2
, y1
);
6986 dc
.DrawLine(x1
, y1
, x1
, y2
);
6988 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6989 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6991 dc
.SetPen(*wxBLACK_PEN
);
6992 dc
.DrawLine(x1
, y2
, x2
, y2
);
6993 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6998 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7000 int row
= coords
.GetRow();
7001 int col
= coords
.GetCol();
7002 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7005 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7007 wxRect rect
= CellToRect( row
, col
);
7009 // right hand border
7011 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7012 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7016 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7017 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7020 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7022 // This if block was previously in wxGrid::OnPaint but that doesn't
7023 // seem to get called under wxGTK - MB
7025 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7026 m_numRows
&& m_numCols
)
7028 m_currentCellCoords
.Set(0, 0);
7031 if ( IsCellEditControlShown() )
7033 // don't show highlight when the edit control is shown
7037 // if the active cell was repainted, repaint its highlight too because it
7038 // might have been damaged by the grid lines
7039 size_t count
= cells
.GetCount();
7040 for ( size_t n
= 0; n
< count
; n
++ )
7042 if ( cells
[n
] == m_currentCellCoords
)
7044 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7045 DrawCellHighlight(dc
, attr
);
7053 // TODO: remove this ???
7054 // This is used to redraw all grid lines e.g. when the grid line colour
7057 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7059 #if !WXGRID_DRAW_LINES
7063 if ( !m_gridLinesEnabled
||
7065 !m_numCols
) return;
7067 int top
, bottom
, left
, right
;
7069 #if 0 //#ifndef __WXGTK__
7073 m_gridWin
->GetClientSize(&cw
, &ch
);
7075 // virtual coords of visible area
7077 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7078 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7083 reg
.GetBox(x
, y
, w
, h
);
7084 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7085 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7089 m_gridWin
->GetClientSize(&cw
, &ch
);
7090 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7091 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7094 // avoid drawing grid lines past the last row and col
7096 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7097 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7099 // no gridlines inside multicells, clip them out
7100 int leftCol
= internalXToCol(left
);
7101 int topRow
= internalYToRow(top
);
7102 int rightCol
= internalXToCol(right
);
7103 int bottomRow
= internalYToRow(bottom
);
7104 wxRegion
clippedcells(0, 0, cw
, ch
);
7107 int i
, j
, cell_rows
, cell_cols
;
7110 for (j
=topRow
; j
<bottomRow
; j
++)
7112 for (i
=leftCol
; i
<rightCol
; i
++)
7114 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7115 if ((cell_rows
> 1) || (cell_cols
> 1))
7117 rect
= CellToRect(j
,i
);
7118 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7119 clippedcells
.Subtract(rect
);
7121 else if ((cell_rows
< 0) || (cell_cols
< 0))
7123 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7124 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7125 clippedcells
.Subtract(rect
);
7129 dc
.SetClippingRegion( clippedcells
);
7131 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7133 // horizontal grid lines
7135 // already declared above - int i;
7136 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7138 int bot
= GetRowBottom(i
) - 1;
7147 dc
.DrawLine( left
, bot
, right
, bot
);
7152 // vertical grid lines
7154 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7156 int colRight
= GetColRight(i
) - 1;
7157 if ( colRight
> right
)
7162 if ( colRight
>= left
)
7164 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7167 dc
.DestroyClippingRegion();
7171 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7173 if ( !m_numRows
) return;
7176 size_t numLabels
= rows
.GetCount();
7178 for ( i
= 0; i
< numLabels
; i
++ )
7180 DrawRowLabel( dc
, rows
[i
] );
7185 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7187 if ( GetRowHeight(row
) <= 0 )
7190 int rowTop
= GetRowTop(row
),
7191 rowBottom
= GetRowBottom(row
) - 1;
7193 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7194 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7195 m_rowLabelWidth
-1, rowBottom
);
7197 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7199 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7201 dc
.SetPen( *wxWHITE_PEN
);
7202 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7203 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7205 dc
.SetBackgroundMode( wxTRANSPARENT
);
7206 dc
.SetTextForeground( GetLabelTextColour() );
7207 dc
.SetFont( GetLabelFont() );
7210 GetRowLabelAlignment( &hAlign
, &vAlign
);
7214 rect
.SetY( GetRowTop(row
) + 2 );
7215 rect
.SetWidth( m_rowLabelWidth
- 4 );
7216 rect
.SetHeight( GetRowHeight(row
) - 4 );
7217 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7221 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7223 if ( !m_numCols
) return;
7226 size_t numLabels
= cols
.GetCount();
7228 for ( i
= 0; i
< numLabels
; i
++ )
7230 DrawColLabel( dc
, cols
[i
] );
7235 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7237 if ( GetColWidth(col
) <= 0 )
7240 int colLeft
= GetColLeft(col
),
7241 colRight
= GetColRight(col
) - 1;
7243 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7244 dc
.DrawLine( colRight
, 0,
7245 colRight
, m_colLabelHeight
-1 );
7247 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7249 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7250 colRight
+1, m_colLabelHeight
-1 );
7252 dc
.SetPen( *wxWHITE_PEN
);
7253 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7254 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7256 dc
.SetBackgroundMode( wxTRANSPARENT
);
7257 dc
.SetTextForeground( GetLabelTextColour() );
7258 dc
.SetFont( GetLabelFont() );
7260 int hAlign
, vAlign
, orient
;
7261 GetColLabelAlignment( &hAlign
, &vAlign
);
7262 orient
= GetColLabelTextOrientation();
7265 rect
.SetX( colLeft
+ 2 );
7267 rect
.SetWidth( GetColWidth(col
) - 4 );
7268 rect
.SetHeight( m_colLabelHeight
- 4 );
7269 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7272 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7273 const wxString
& value
,
7277 int textOrientation
)
7279 wxArrayString lines
;
7281 StringToLines( value
, lines
);
7284 //Forward to new API.
7285 DrawTextRectangle( dc
,
7294 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7295 const wxArrayString
& lines
,
7299 int textOrientation
)
7301 long textWidth
, textHeight
;
7302 long lineWidth
, lineHeight
;
7305 dc
.SetClippingRegion( rect
);
7307 nLines
= lines
.GetCount();
7311 float x
= 0.0, y
= 0.0;
7313 if( textOrientation
== wxHORIZONTAL
)
7314 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7316 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7320 case wxALIGN_BOTTOM
:
7321 if( textOrientation
== wxHORIZONTAL
)
7322 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7324 x
= rect
.x
+ rect
.width
- textWidth
;
7327 case wxALIGN_CENTRE
:
7328 if( textOrientation
== wxHORIZONTAL
)
7329 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7331 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7336 if( textOrientation
== wxHORIZONTAL
)
7343 // Align each line of a multi-line label
7344 for( l
= 0; l
< nLines
; l
++ )
7346 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7348 switch( horizAlign
)
7351 if( textOrientation
== wxHORIZONTAL
)
7352 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7354 y
= rect
.y
+ lineWidth
+ 1;
7357 case wxALIGN_CENTRE
:
7358 if( textOrientation
== wxHORIZONTAL
)
7359 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7361 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7366 if( textOrientation
== wxHORIZONTAL
)
7369 y
= rect
.y
+ rect
.height
- 1;
7373 if( textOrientation
== wxHORIZONTAL
)
7375 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7380 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7385 dc
.DestroyClippingRegion();
7389 // Split multi line text up into an array of strings. Any existing
7390 // contents of the string array are preserved.
7392 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7396 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7397 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7399 while ( startPos
< (int)tVal
.Length() )
7401 pos
= tVal
.Mid(startPos
).Find( eol
);
7406 else if ( pos
== 0 )
7408 lines
.Add( wxEmptyString
);
7412 lines
.Add( value
.Mid(startPos
, pos
) );
7416 if ( startPos
< (int)value
.Length() )
7418 lines
.Add( value
.Mid( startPos
) );
7423 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7424 const wxArrayString
& lines
,
7425 long *width
, long *height
)
7432 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7434 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7435 w
= wxMax( w
, lineW
);
7444 // ------ Batch processing.
7446 void wxGrid::EndBatch()
7448 if ( m_batchCount
> 0 )
7451 if ( !m_batchCount
)
7454 m_rowLabelWin
->Refresh();
7455 m_colLabelWin
->Refresh();
7456 m_cornerLabelWin
->Refresh();
7457 m_gridWin
->Refresh();
7462 // Use this, rather than wxWindow::Refresh(), to force an immediate
7463 // repainting of the grid. Has no effect if you are already inside a
7464 // BeginBatch / EndBatch block.
7466 void wxGrid::ForceRefresh()
7474 // ------ Edit control functions
7478 void wxGrid::EnableEditing( bool edit
)
7480 // TODO: improve this ?
7482 if ( edit
!= m_editable
)
7484 if(!edit
) EnableCellEditControl(edit
);
7490 void wxGrid::EnableCellEditControl( bool enable
)
7495 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7496 SetCurrentCell( 0, 0 );
7498 if ( enable
!= m_cellEditCtrlEnabled
)
7502 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7505 // this should be checked by the caller!
7506 wxASSERT_MSG( CanEnableCellControl(),
7507 _T("can't enable editing for this cell!") );
7509 // do it before ShowCellEditControl()
7510 m_cellEditCtrlEnabled
= enable
;
7512 ShowCellEditControl();
7516 //FIXME:add veto support
7517 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7519 HideCellEditControl();
7520 SaveEditControlValue();
7522 // do it after HideCellEditControl()
7523 m_cellEditCtrlEnabled
= enable
;
7528 bool wxGrid::IsCurrentCellReadOnly() const
7531 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7532 bool readonly
= attr
->IsReadOnly();
7538 bool wxGrid::CanEnableCellControl() const
7540 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7541 !IsCurrentCellReadOnly();
7545 bool wxGrid::IsCellEditControlEnabled() const
7547 // the cell edit control might be disable for all cells or just for the
7548 // current one if it's read only
7549 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7552 bool wxGrid::IsCellEditControlShown() const
7554 bool isShown
= false;
7556 if ( m_cellEditCtrlEnabled
)
7558 int row
= m_currentCellCoords
.GetRow();
7559 int col
= m_currentCellCoords
.GetCol();
7560 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7561 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7566 if ( editor
->IsCreated() )
7568 isShown
= editor
->GetControl()->IsShown();
7578 void wxGrid::ShowCellEditControl()
7580 if ( IsCellEditControlEnabled() )
7582 if ( !IsVisible( m_currentCellCoords
) )
7584 m_cellEditCtrlEnabled
= false;
7589 wxRect rect
= CellToRect( m_currentCellCoords
);
7590 int row
= m_currentCellCoords
.GetRow();
7591 int col
= m_currentCellCoords
.GetCol();
7593 // if this is part of a multicell, find owner (topleft)
7594 int cell_rows
, cell_cols
;
7595 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7596 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7600 m_currentCellCoords
.SetRow( row
);
7601 m_currentCellCoords
.SetCol( col
);
7604 // convert to scrolled coords
7606 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7608 // done in PaintBackground()
7610 // erase the highlight and the cell contents because the editor
7611 // might not cover the entire cell
7612 wxClientDC
dc( m_gridWin
);
7614 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7615 dc
.SetPen(*wxTRANSPARENT_PEN
);
7616 dc
.DrawRectangle(rect
);
7619 // cell is shifted by one pixel
7620 // However, don't allow x or y to become negative
7621 // since the SetSize() method interprets that as
7628 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7629 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7630 if ( !editor
->IsCreated() )
7632 editor
->Create(m_gridWin
, wxID_ANY
,
7633 new wxGridCellEditorEvtHandler(this, editor
));
7635 wxGridEditorCreatedEvent
evt(GetId(),
7636 wxEVT_GRID_EDITOR_CREATED
,
7640 editor
->GetControl());
7641 GetEventHandler()->ProcessEvent(evt
);
7645 // resize editor to overflow into righthand cells if allowed
7646 int maxWidth
= rect
.width
;
7647 wxString value
= GetCellValue(row
, col
);
7648 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7651 GetTextExtent(value
, &maxWidth
, &y
,
7652 NULL
, NULL
, &attr
->GetFont());
7653 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7655 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7656 if (rect
.x
+maxWidth
> client_right
)
7657 maxWidth
= client_right
- rect
.x
;
7659 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7661 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7662 // may have changed earlier
7663 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7666 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7667 // looks weird going over a multicell
7668 if (m_table
->IsEmptyCell(row
,i
) &&
7669 (rect
.width
< maxWidth
) && (c_rows
== 1))
7670 rect
.width
+= GetColWidth(i
);
7674 if (rect
.GetRight() > client_right
)
7675 rect
.SetRight(client_right
-1);
7678 editor
->SetCellAttr(attr
);
7679 editor
->SetSize( rect
);
7680 editor
->Show( true, attr
);
7682 // recalc dimensions in case we need to
7683 // expand the scrolled window to account for editor
7686 editor
->BeginEdit(row
, col
, this);
7687 editor
->SetCellAttr(NULL
);
7696 void wxGrid::HideCellEditControl()
7698 if ( IsCellEditControlEnabled() )
7700 int row
= m_currentCellCoords
.GetRow();
7701 int col
= m_currentCellCoords
.GetCol();
7703 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7704 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7705 editor
->Show( false );
7708 m_gridWin
->SetFocus();
7709 // refresh whole row to the right
7710 wxRect
rect( CellToRect(row
, col
) );
7711 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7712 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7713 m_gridWin
->Refresh( false, &rect
);
7718 void wxGrid::SaveEditControlValue()
7720 if ( IsCellEditControlEnabled() )
7722 int row
= m_currentCellCoords
.GetRow();
7723 int col
= m_currentCellCoords
.GetCol();
7725 wxString oldval
= GetCellValue(row
,col
);
7727 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7728 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7729 bool changed
= editor
->EndEdit(row
, col
, this);
7736 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7737 m_currentCellCoords
.GetRow(),
7738 m_currentCellCoords
.GetCol() ) < 0 ) {
7740 // Event has been vetoed, set the data back.
7741 SetCellValue(row
,col
,oldval
);
7749 // ------ Grid location functions
7750 // Note that all of these functions work with the logical coordinates of
7751 // grid cells and labels so you will need to convert from device
7752 // coordinates for mouse events etc.
7755 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7757 int row
= YToRow(y
);
7758 int col
= XToCol(x
);
7760 if ( row
== -1 || col
== -1 )
7762 coords
= wxGridNoCellCoords
;
7766 coords
.Set( row
, col
);
7771 // Internal Helper function for computing row or column from some
7772 // (unscrolled) coordinate value, using either
7773 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7774 // of m_rowBottoms/m_ColRights to speed up the search!
7776 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7777 const wxArrayInt
& BorderArray
, int nMax
,
7782 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7788 size_t i_max
= coord
/ defaultDist
,
7791 if (BorderArray
.IsEmpty())
7793 if((int) i_max
< nMax
)
7795 return clipToMinMax
? nMax
- 1 : -1;
7798 if ( i_max
>= BorderArray
.GetCount())
7799 i_max
= BorderArray
.GetCount() - 1;
7802 if ( coord
>= BorderArray
[i_max
])
7806 i_max
= coord
/ minDist
;
7808 i_max
= BorderArray
.GetCount() - 1;
7810 if ( i_max
>= BorderArray
.GetCount())
7811 i_max
= BorderArray
.GetCount() - 1;
7813 if ( coord
>= BorderArray
[i_max
])
7814 return clipToMinMax
? (int)i_max
: -1;
7815 if ( coord
< BorderArray
[0] )
7818 while ( i_max
- i_min
> 0 )
7820 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7821 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7822 if (coord
>= BorderArray
[ i_max
- 1])
7826 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7827 if (coord
< BorderArray
[median
])
7835 int wxGrid::YToRow( int y
)
7837 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7838 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7842 int wxGrid::XToCol( int x
)
7844 return CoordToRowOrCol(x
, m_defaultColWidth
,
7845 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7849 // return the row number that that the y coord is near the edge of, or
7850 // -1 if not near an edge
7852 int wxGrid::YToEdgeOfRow( int y
)
7855 i
= internalYToRow(y
);
7857 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7859 // We know that we are in row i, test whether we are
7860 // close enough to lower or upper border, respectively.
7861 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7863 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7871 // return the col number that that the x coord is near the edge of, or
7872 // -1 if not near an edge
7874 int wxGrid::XToEdgeOfCol( int x
)
7877 i
= internalXToCol(x
);
7879 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7881 // We know that we are in column i, test whether we are
7882 // close enough to right or left border, respectively.
7883 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7885 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7893 wxRect
wxGrid::CellToRect( int row
, int col
)
7895 wxRect
rect( -1, -1, -1, -1 );
7897 if ( row
>= 0 && row
< m_numRows
&&
7898 col
>= 0 && col
< m_numCols
)
7900 int i
, cell_rows
, cell_cols
;
7901 rect
.width
= rect
.height
= 0;
7902 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7903 // if negative then find multicell owner
7904 if (cell_rows
< 0) row
+= cell_rows
;
7905 if (cell_cols
< 0) col
+= cell_cols
;
7906 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7908 rect
.x
= GetColLeft(col
);
7909 rect
.y
= GetRowTop(row
);
7910 for (i
=col
; i
<col
+cell_cols
; i
++)
7911 rect
.width
+= GetColWidth(i
);
7912 for (i
=row
; i
<row
+cell_rows
; i
++)
7913 rect
.height
+= GetRowHeight(i
);
7916 // if grid lines are enabled, then the area of the cell is a bit smaller
7917 if (m_gridLinesEnabled
) {
7925 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7927 // get the cell rectangle in logical coords
7929 wxRect
r( CellToRect( row
, col
) );
7931 // convert to device coords
7933 int left
, top
, right
, bottom
;
7934 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7935 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7937 // check against the client area of the grid window
7940 m_gridWin
->GetClientSize( &cw
, &ch
);
7942 if ( wholeCellVisible
)
7944 // is the cell wholly visible ?
7946 return ( left
>= 0 && right
<= cw
&&
7947 top
>= 0 && bottom
<= ch
);
7951 // is the cell partly visible ?
7953 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7954 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7959 // make the specified cell location visible by doing a minimal amount
7962 void wxGrid::MakeCellVisible( int row
, int col
)
7966 int xpos
= -1, ypos
= -1;
7968 if ( row
>= 0 && row
< m_numRows
&&
7969 col
>= 0 && col
< m_numCols
)
7971 // get the cell rectangle in logical coords
7973 wxRect
r( CellToRect( row
, col
) );
7975 // convert to device coords
7977 int left
, top
, right
, bottom
;
7978 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7979 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7982 m_gridWin
->GetClientSize( &cw
, &ch
);
7988 else if ( bottom
> ch
)
7990 int h
= r
.GetHeight();
7992 for ( i
= row
-1; i
>= 0; i
-- )
7994 int rowHeight
= GetRowHeight(i
);
7995 if ( h
+ rowHeight
> ch
)
8002 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8003 // have rounding errors (this is important, because if we do, we
8004 // might not scroll at all and some cells won't be redrawn)
8006 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8008 ypos
+= GRID_SCROLL_LINE_Y
;
8015 else if ( right
> cw
)
8017 // position the view so that the cell is on the right
8019 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8020 xpos
= x0
+ (right
- cw
);
8022 // see comment for ypos above
8023 xpos
+= GRID_SCROLL_LINE_X
;
8026 if ( xpos
!= -1 || ypos
!= -1 )
8029 xpos
/= GRID_SCROLL_LINE_X
;
8031 ypos
/= GRID_SCROLL_LINE_Y
;
8032 Scroll( xpos
, ypos
);
8040 // ------ Grid cursor movement functions
8043 bool wxGrid::MoveCursorUp( bool expandSelection
)
8045 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8046 m_currentCellCoords
.GetRow() >= 0 )
8048 if ( expandSelection
)
8050 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8051 m_selectingKeyboard
= m_currentCellCoords
;
8052 if ( m_selectingKeyboard
.GetRow() > 0 )
8054 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8055 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8056 m_selectingKeyboard
.GetCol() );
8057 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8060 else if ( m_currentCellCoords
.GetRow() > 0 )
8063 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8064 m_currentCellCoords
.GetCol() );
8065 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8066 m_currentCellCoords
.GetCol() );
8077 bool wxGrid::MoveCursorDown( bool expandSelection
)
8079 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8080 m_currentCellCoords
.GetRow() < m_numRows
)
8082 if ( expandSelection
)
8084 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8085 m_selectingKeyboard
= m_currentCellCoords
;
8086 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8088 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8089 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8090 m_selectingKeyboard
.GetCol() );
8091 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8094 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8097 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8098 m_currentCellCoords
.GetCol() );
8099 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8100 m_currentCellCoords
.GetCol() );
8111 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8113 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8114 m_currentCellCoords
.GetCol() >= 0 )
8116 if ( expandSelection
)
8118 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8119 m_selectingKeyboard
= m_currentCellCoords
;
8120 if ( m_selectingKeyboard
.GetCol() > 0 )
8122 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8123 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8124 m_selectingKeyboard
.GetCol() );
8125 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8128 else if ( m_currentCellCoords
.GetCol() > 0 )
8131 MakeCellVisible( m_currentCellCoords
.GetRow(),
8132 m_currentCellCoords
.GetCol() - 1 );
8133 SetCurrentCell( m_currentCellCoords
.GetRow(),
8134 m_currentCellCoords
.GetCol() - 1 );
8145 bool wxGrid::MoveCursorRight( bool expandSelection
)
8147 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8148 m_currentCellCoords
.GetCol() < m_numCols
)
8150 if ( expandSelection
)
8152 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8153 m_selectingKeyboard
= m_currentCellCoords
;
8154 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8156 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8157 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8158 m_selectingKeyboard
.GetCol() );
8159 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8162 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8165 MakeCellVisible( m_currentCellCoords
.GetRow(),
8166 m_currentCellCoords
.GetCol() + 1 );
8167 SetCurrentCell( m_currentCellCoords
.GetRow(),
8168 m_currentCellCoords
.GetCol() + 1 );
8179 bool wxGrid::MovePageUp()
8181 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8183 int row
= m_currentCellCoords
.GetRow();
8187 m_gridWin
->GetClientSize( &cw
, &ch
);
8189 int y
= GetRowTop(row
);
8190 int newRow
= internalYToRow( y
- ch
+ 1 );
8192 if ( newRow
== row
)
8194 //row > 0 , so newrow can never be less than 0 here.
8198 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8199 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8207 bool wxGrid::MovePageDown()
8209 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8211 int row
= m_currentCellCoords
.GetRow();
8212 if ( (row
+1) < m_numRows
)
8215 m_gridWin
->GetClientSize( &cw
, &ch
);
8217 int y
= GetRowTop(row
);
8218 int newRow
= internalYToRow( y
+ ch
);
8219 if ( newRow
== row
)
8221 // row < m_numRows , so newrow can't overflow here.
8225 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8226 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8234 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8237 m_currentCellCoords
!= wxGridNoCellCoords
&&
8238 m_currentCellCoords
.GetRow() > 0 )
8240 int row
= m_currentCellCoords
.GetRow();
8241 int col
= m_currentCellCoords
.GetCol();
8243 if ( m_table
->IsEmptyCell(row
, col
) )
8245 // starting in an empty cell: find the next block of
8251 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8254 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8256 // starting at the top of a block: find the next block
8262 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8267 // starting within a block: find the top of the block
8272 if ( m_table
->IsEmptyCell(row
, col
) )
8280 MakeCellVisible( row
, col
);
8281 if ( expandSelection
)
8283 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8284 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8289 SetCurrentCell( row
, col
);
8297 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8300 m_currentCellCoords
!= wxGridNoCellCoords
&&
8301 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8303 int row
= m_currentCellCoords
.GetRow();
8304 int col
= m_currentCellCoords
.GetCol();
8306 if ( m_table
->IsEmptyCell(row
, col
) )
8308 // starting in an empty cell: find the next block of
8311 while ( row
< m_numRows
-1 )
8314 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8317 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8319 // starting at the bottom of a block: find the next block
8322 while ( row
< m_numRows
-1 )
8325 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8330 // starting within a block: find the bottom of the block
8332 while ( row
< m_numRows
-1 )
8335 if ( m_table
->IsEmptyCell(row
, col
) )
8343 MakeCellVisible( row
, col
);
8344 if ( expandSelection
)
8346 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8347 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8352 SetCurrentCell( row
, col
);
8361 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8364 m_currentCellCoords
!= wxGridNoCellCoords
&&
8365 m_currentCellCoords
.GetCol() > 0 )
8367 int row
= m_currentCellCoords
.GetRow();
8368 int col
= m_currentCellCoords
.GetCol();
8370 if ( m_table
->IsEmptyCell(row
, col
) )
8372 // starting in an empty cell: find the next block of
8378 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8381 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8383 // starting at the left of a block: find the next block
8389 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8394 // starting within a block: find the left of the block
8399 if ( m_table
->IsEmptyCell(row
, col
) )
8407 MakeCellVisible( row
, col
);
8408 if ( expandSelection
)
8410 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8411 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8416 SetCurrentCell( row
, col
);
8425 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8428 m_currentCellCoords
!= wxGridNoCellCoords
&&
8429 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8431 int row
= m_currentCellCoords
.GetRow();
8432 int col
= m_currentCellCoords
.GetCol();
8434 if ( m_table
->IsEmptyCell(row
, col
) )
8436 // starting in an empty cell: find the next block of
8439 while ( col
< m_numCols
-1 )
8442 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8445 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8447 // starting at the right of a block: find the next block
8450 while ( col
< m_numCols
-1 )
8453 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8458 // starting within a block: find the right of the block
8460 while ( col
< m_numCols
-1 )
8463 if ( m_table
->IsEmptyCell(row
, col
) )
8471 MakeCellVisible( row
, col
);
8472 if ( expandSelection
)
8474 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8475 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8480 SetCurrentCell( row
, col
);
8492 // ------ Label values and formatting
8495 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8497 *horiz
= m_rowLabelHorizAlign
;
8498 *vert
= m_rowLabelVertAlign
;
8501 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8503 *horiz
= m_colLabelHorizAlign
;
8504 *vert
= m_colLabelVertAlign
;
8507 int wxGrid::GetColLabelTextOrientation()
8509 return m_colLabelTextOrientation
;
8512 wxString
wxGrid::GetRowLabelValue( int row
)
8516 return m_table
->GetRowLabelValue( row
);
8526 wxString
wxGrid::GetColLabelValue( int col
)
8530 return m_table
->GetColLabelValue( col
);
8541 void wxGrid::SetRowLabelSize( int width
)
8543 width
= wxMax( width
, 0 );
8544 if ( width
!= m_rowLabelWidth
)
8548 m_rowLabelWin
->Show( false );
8549 m_cornerLabelWin
->Show( false );
8551 else if ( m_rowLabelWidth
== 0 )
8553 m_rowLabelWin
->Show( true );
8554 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8557 m_rowLabelWidth
= width
;
8559 wxScrolledWindow::Refresh( true );
8564 void wxGrid::SetColLabelSize( int height
)
8566 height
= wxMax( height
, 0 );
8567 if ( height
!= m_colLabelHeight
)
8571 m_colLabelWin
->Show( false );
8572 m_cornerLabelWin
->Show( false );
8574 else if ( m_colLabelHeight
== 0 )
8576 m_colLabelWin
->Show( true );
8577 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8580 m_colLabelHeight
= height
;
8582 wxScrolledWindow::Refresh( true );
8587 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8589 if ( m_labelBackgroundColour
!= colour
)
8591 m_labelBackgroundColour
= colour
;
8592 m_rowLabelWin
->SetBackgroundColour( colour
);
8593 m_colLabelWin
->SetBackgroundColour( colour
);
8594 m_cornerLabelWin
->SetBackgroundColour( colour
);
8596 if ( !GetBatchCount() )
8598 m_rowLabelWin
->Refresh();
8599 m_colLabelWin
->Refresh();
8600 m_cornerLabelWin
->Refresh();
8605 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8607 if ( m_labelTextColour
!= colour
)
8609 m_labelTextColour
= colour
;
8610 if ( !GetBatchCount() )
8612 m_rowLabelWin
->Refresh();
8613 m_colLabelWin
->Refresh();
8618 void wxGrid::SetLabelFont( const wxFont
& font
)
8621 if ( !GetBatchCount() )
8623 m_rowLabelWin
->Refresh();
8624 m_colLabelWin
->Refresh();
8628 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8630 // allow old (incorrect) defs to be used
8633 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8634 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8635 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8640 case wxTOP
: vert
= wxALIGN_TOP
; break;
8641 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8642 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8645 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8647 m_rowLabelHorizAlign
= horiz
;
8650 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8652 m_rowLabelVertAlign
= vert
;
8655 if ( !GetBatchCount() )
8657 m_rowLabelWin
->Refresh();
8661 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8663 // allow old (incorrect) defs to be used
8666 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8667 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8668 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8673 case wxTOP
: vert
= wxALIGN_TOP
; break;
8674 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8675 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8678 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8680 m_colLabelHorizAlign
= horiz
;
8683 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8685 m_colLabelVertAlign
= vert
;
8688 if ( !GetBatchCount() )
8690 m_colLabelWin
->Refresh();
8694 // Note: under MSW, the default column label font must be changed because it
8695 // does not support vertical printing
8697 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8698 // pGrid->SetLabelFont(font);
8699 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8701 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8703 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8705 m_colLabelTextOrientation
= textOrientation
;
8708 if ( !GetBatchCount() )
8710 m_colLabelWin
->Refresh();
8714 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8718 m_table
->SetRowLabelValue( row
, s
);
8719 if ( !GetBatchCount() )
8721 wxRect rect
= CellToRect( row
, 0);
8722 if ( rect
.height
> 0 )
8724 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8726 rect
.width
= m_rowLabelWidth
;
8727 m_rowLabelWin
->Refresh( true, &rect
);
8733 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8737 m_table
->SetColLabelValue( col
, s
);
8738 if ( !GetBatchCount() )
8740 wxRect rect
= CellToRect( 0, col
);
8741 if ( rect
.width
> 0 )
8743 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8745 rect
.height
= m_colLabelHeight
;
8746 m_colLabelWin
->Refresh( true, &rect
);
8752 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8754 if ( m_gridLineColour
!= colour
)
8756 m_gridLineColour
= colour
;
8758 wxClientDC
dc( m_gridWin
);
8760 DrawAllGridLines( dc
, wxRegion() );
8765 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8767 if ( m_cellHighlightColour
!= colour
)
8769 m_cellHighlightColour
= colour
;
8771 wxClientDC
dc( m_gridWin
);
8773 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8774 DrawCellHighlight(dc
, attr
);
8779 void wxGrid::SetCellHighlightPenWidth(int width
)
8781 if (m_cellHighlightPenWidth
!= width
) {
8782 m_cellHighlightPenWidth
= width
;
8784 // Just redrawing the cell highlight is not enough since that won't
8785 // make any visible change if the the thickness is getting smaller.
8786 int row
= m_currentCellCoords
.GetRow();
8787 int col
= m_currentCellCoords
.GetCol();
8788 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8790 wxRect rect
= CellToRect(row
, col
);
8791 m_gridWin
->Refresh(true, &rect
);
8795 void wxGrid::SetCellHighlightROPenWidth(int width
)
8797 if (m_cellHighlightROPenWidth
!= width
) {
8798 m_cellHighlightROPenWidth
= width
;
8800 // Just redrawing the cell highlight is not enough since that won't
8801 // make any visible change if the the thickness is getting smaller.
8802 int row
= m_currentCellCoords
.GetRow();
8803 int col
= m_currentCellCoords
.GetCol();
8804 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8806 wxRect rect
= CellToRect(row
, col
);
8807 m_gridWin
->Refresh(true, &rect
);
8811 void wxGrid::EnableGridLines( bool enable
)
8813 if ( enable
!= m_gridLinesEnabled
)
8815 m_gridLinesEnabled
= enable
;
8817 if ( !GetBatchCount() )
8821 wxClientDC
dc( m_gridWin
);
8823 DrawAllGridLines( dc
, wxRegion() );
8827 m_gridWin
->Refresh();
8834 int wxGrid::GetDefaultRowSize()
8836 return m_defaultRowHeight
;
8839 int wxGrid::GetRowSize( int row
)
8841 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8843 return GetRowHeight(row
);
8846 int wxGrid::GetDefaultColSize()
8848 return m_defaultColWidth
;
8851 int wxGrid::GetColSize( int col
)
8853 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8855 return GetColWidth(col
);
8858 // ============================================================================
8859 // access to the grid attributes: each of them has a default value in the grid
8860 // itself and may be overidden on a per-cell basis
8861 // ============================================================================
8863 // ----------------------------------------------------------------------------
8864 // setting default attributes
8865 // ----------------------------------------------------------------------------
8867 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8869 m_defaultCellAttr
->SetBackgroundColour(col
);
8871 m_gridWin
->SetBackgroundColour(col
);
8875 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8877 m_defaultCellAttr
->SetTextColour(col
);
8880 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8882 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8885 void wxGrid::SetDefaultCellOverflow( bool allow
)
8887 m_defaultCellAttr
->SetOverflow(allow
);
8890 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8892 m_defaultCellAttr
->SetFont(font
);
8896 // For editors and renderers the type registry takes precedence over the
8897 // default attr, so we need to register the new editor/renderer for the string
8898 // data type in order to make setting a default editor/renderer appear to
8901 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8903 RegisterDataType(wxGRID_VALUE_STRING
,
8905 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8908 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8910 RegisterDataType(wxGRID_VALUE_STRING
,
8911 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8915 // ----------------------------------------------------------------------------
8916 // access to the default attrbiutes
8917 // ----------------------------------------------------------------------------
8919 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8921 return m_defaultCellAttr
->GetBackgroundColour();
8924 wxColour
wxGrid::GetDefaultCellTextColour()
8926 return m_defaultCellAttr
->GetTextColour();
8929 wxFont
wxGrid::GetDefaultCellFont()
8931 return m_defaultCellAttr
->GetFont();
8934 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8936 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8939 bool wxGrid::GetDefaultCellOverflow()
8941 return m_defaultCellAttr
->GetOverflow();
8944 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8946 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8949 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8951 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8954 // ----------------------------------------------------------------------------
8955 // access to cell attributes
8956 // ----------------------------------------------------------------------------
8958 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8960 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8961 wxColour colour
= attr
->GetBackgroundColour();
8966 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8968 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8969 wxColour colour
= attr
->GetTextColour();
8974 wxFont
wxGrid::GetCellFont( int row
, int col
)
8976 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8977 wxFont font
= attr
->GetFont();
8982 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8984 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8985 attr
->GetAlignment(horiz
, vert
);
8989 bool wxGrid::GetCellOverflow( int row
, int col
)
8991 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8992 bool allow
= attr
->GetOverflow();
8997 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
8999 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9000 attr
->GetSize( num_rows
, num_cols
);
9004 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9006 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9007 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9013 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9015 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9016 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9022 bool wxGrid::IsReadOnly(int row
, int col
) const
9024 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9025 bool isReadOnly
= attr
->IsReadOnly();
9030 // ----------------------------------------------------------------------------
9031 // attribute support: cache, automatic provider creation, ...
9032 // ----------------------------------------------------------------------------
9034 bool wxGrid::CanHaveAttributes()
9041 return m_table
->CanHaveAttributes();
9044 void wxGrid::ClearAttrCache()
9046 if ( m_attrCache
.row
!= -1 )
9048 wxSafeDecRef(m_attrCache
.attr
);
9049 m_attrCache
.attr
= NULL
;
9050 m_attrCache
.row
= -1;
9054 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9058 wxGrid
*self
= (wxGrid
*)this; // const_cast
9060 self
->ClearAttrCache();
9061 self
->m_attrCache
.row
= row
;
9062 self
->m_attrCache
.col
= col
;
9063 self
->m_attrCache
.attr
= attr
;
9068 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9070 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9072 *attr
= m_attrCache
.attr
;
9073 wxSafeIncRef(m_attrCache
.attr
);
9075 #ifdef DEBUG_ATTR_CACHE
9076 gs_nAttrCacheHits
++;
9083 #ifdef DEBUG_ATTR_CACHE
9084 gs_nAttrCacheMisses
++;
9090 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9092 wxGridCellAttr
*attr
= NULL
;
9093 // Additional test to avoid looking at the cache e.g. for
9094 // wxNoCellCoords, as this will confuse memory management.
9097 if ( !LookupAttr(row
, col
, &attr
) )
9099 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9100 : (wxGridCellAttr
*)NULL
;
9101 CacheAttr(row
, col
, attr
);
9106 attr
->SetDefAttr(m_defaultCellAttr
);
9110 attr
= m_defaultCellAttr
;
9117 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9119 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9120 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9122 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9123 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9125 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9128 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9130 // artificially inc the ref count to match DecRef() in caller
9132 m_table
->SetAttr(attr
, row
, col
);
9138 // ----------------------------------------------------------------------------
9139 // setting column attributes (wrappers around SetColAttr)
9140 // ----------------------------------------------------------------------------
9142 void wxGrid::SetColFormatBool(int col
)
9144 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9147 void wxGrid::SetColFormatNumber(int col
)
9149 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9152 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9154 wxString typeName
= wxGRID_VALUE_FLOAT
;
9155 if ( (width
!= -1) || (precision
!= -1) )
9157 typeName
<< _T(':') << width
<< _T(',') << precision
;
9160 SetColFormatCustom(col
, typeName
);
9163 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9165 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9167 attr
= new wxGridCellAttr
;
9168 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9169 attr
->SetRenderer(renderer
);
9171 SetColAttr(col
, attr
);
9175 // ----------------------------------------------------------------------------
9176 // setting cell attributes: this is forwarded to the table
9177 // ----------------------------------------------------------------------------
9179 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9181 if ( CanHaveAttributes() )
9183 m_table
->SetAttr(attr
, row
, col
);
9192 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9194 if ( CanHaveAttributes() )
9196 m_table
->SetRowAttr(attr
, row
);
9205 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9207 if ( CanHaveAttributes() )
9209 m_table
->SetColAttr(attr
, col
);
9218 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9220 if ( CanHaveAttributes() )
9222 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9223 attr
->SetBackgroundColour(colour
);
9228 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9230 if ( CanHaveAttributes() )
9232 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9233 attr
->SetTextColour(colour
);
9238 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9240 if ( CanHaveAttributes() )
9242 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9243 attr
->SetFont(font
);
9248 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9250 if ( CanHaveAttributes() )
9252 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9253 attr
->SetAlignment(horiz
, vert
);
9258 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9260 if ( CanHaveAttributes() )
9262 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9263 attr
->SetOverflow(allow
);
9268 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9270 if ( CanHaveAttributes() )
9272 int cell_rows
, cell_cols
;
9274 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9275 attr
->GetSize(&cell_rows
, &cell_cols
);
9276 attr
->SetSize(num_rows
, num_cols
);
9279 // Cannot set the size of a cell to 0 or negative values
9280 // While it is perfectly legal to do that, this function cannot
9281 // handle all the possibilies, do it by hand by getting the CellAttr.
9282 // You can only set the size of a cell to 1,1 or greater with this fn
9283 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9284 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9285 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9286 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9288 // if this was already a multicell then "turn off" the other cells first
9289 if ((cell_rows
> 1) || (cell_rows
> 1))
9292 for (j
=row
; j
<row
+cell_rows
; j
++)
9294 for (i
=col
; i
<col
+cell_cols
; i
++)
9296 if ((i
!= col
) || (j
!= row
))
9298 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9299 attr_stub
->SetSize( 1, 1 );
9300 attr_stub
->DecRef();
9306 // mark the cells that will be covered by this cell to
9307 // negative or zero values to point back at this cell
9308 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9311 for (j
=row
; j
<row
+num_rows
; j
++)
9313 for (i
=col
; i
<col
+num_cols
; i
++)
9315 if ((i
!= col
) || (j
!= row
))
9317 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9318 attr_stub
->SetSize( row
-j
, col
-i
);
9319 attr_stub
->DecRef();
9327 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9329 if ( CanHaveAttributes() )
9331 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9332 attr
->SetRenderer(renderer
);
9337 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9339 if ( CanHaveAttributes() )
9341 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9342 attr
->SetEditor(editor
);
9347 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9349 if ( CanHaveAttributes() )
9351 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9352 attr
->SetReadOnly(isReadOnly
);
9357 // ----------------------------------------------------------------------------
9358 // Data type registration
9359 // ----------------------------------------------------------------------------
9361 void wxGrid::RegisterDataType(const wxString
& typeName
,
9362 wxGridCellRenderer
* renderer
,
9363 wxGridCellEditor
* editor
)
9365 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9369 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9371 wxString typeName
= m_table
->GetTypeName(row
, col
);
9372 return GetDefaultEditorForType(typeName
);
9375 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9377 wxString typeName
= m_table
->GetTypeName(row
, col
);
9378 return GetDefaultRendererForType(typeName
);
9382 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9384 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9385 if ( index
== wxNOT_FOUND
)
9387 wxFAIL_MSG(wxT("Unknown data type name"));
9392 return m_typeRegistry
->GetEditor(index
);
9396 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9398 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9399 if ( index
== wxNOT_FOUND
)
9401 wxFAIL_MSG(wxT("Unknown data type name"));
9406 return m_typeRegistry
->GetRenderer(index
);
9410 // ----------------------------------------------------------------------------
9412 // ----------------------------------------------------------------------------
9414 void wxGrid::EnableDragRowSize( bool enable
)
9416 m_canDragRowSize
= enable
;
9420 void wxGrid::EnableDragColSize( bool enable
)
9422 m_canDragColSize
= enable
;
9425 void wxGrid::EnableDragGridSize( bool enable
)
9427 m_canDragGridSize
= enable
;
9430 void wxGrid::EnableDragCell( bool enable
)
9432 m_canDragCell
= enable
;
9435 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9437 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9439 if ( resizeExistingRows
)
9441 // since we are resizing all rows to the default row size,
9442 // we can simply clear the row heights and row bottoms
9443 // arrays (which also allows us to take advantage of
9444 // some speed optimisations)
9445 m_rowHeights
.Empty();
9446 m_rowBottoms
.Empty();
9447 if ( !GetBatchCount() )
9452 void wxGrid::SetRowSize( int row
, int height
)
9454 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9456 // See comment in SetColSize
9457 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9459 if ( m_rowHeights
.IsEmpty() )
9461 // need to really create the array
9465 int h
= wxMax( 0, height
);
9466 int diff
= h
- m_rowHeights
[row
];
9468 m_rowHeights
[row
] = h
;
9470 for ( i
= row
; i
< m_numRows
; i
++ )
9472 m_rowBottoms
[i
] += diff
;
9474 if ( !GetBatchCount() )
9478 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9480 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9482 if ( resizeExistingCols
)
9484 // since we are resizing all columns to the default column size,
9485 // we can simply clear the col widths and col rights
9486 // arrays (which also allows us to take advantage of
9487 // some speed optimisations)
9488 m_colWidths
.Empty();
9489 m_colRights
.Empty();
9490 if ( !GetBatchCount() )
9495 void wxGrid::SetColSize( int col
, int width
)
9497 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9499 // should we check that it's bigger than GetColMinimalWidth(col) here?
9501 // No, because it is reasonable to assume the library user know's
9502 // what he is doing. However whe should test against the weaker
9503 // constariant of minimalAcceptableWidth, as this breaks rendering
9505 // This test then fixes sf.net bug #645734
9507 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9509 if ( m_colWidths
.IsEmpty() )
9511 // need to really create the array
9515 // if < 0 calc new width from label
9519 wxArrayString lines
;
9520 wxClientDC
dc(m_colLabelWin
);
9521 dc
.SetFont(GetLabelFont());
9522 StringToLines(GetColLabelValue(col
), lines
);
9523 GetTextBoxSize(dc
, lines
, &w
, &h
);
9526 int w
= wxMax( 0, width
);
9527 int diff
= w
- m_colWidths
[col
];
9528 m_colWidths
[col
] = w
;
9531 for ( i
= col
; i
< m_numCols
; i
++ )
9533 m_colRights
[i
] += diff
;
9535 if ( !GetBatchCount() )
9540 void wxGrid::SetColMinimalWidth( int col
, int width
)
9542 if (width
> GetColMinimalAcceptableWidth()) {
9543 m_colMinWidths
[col
] = width
;
9547 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9549 if (width
> GetRowMinimalAcceptableHeight()) {
9550 m_rowMinHeights
[row
] = width
;
9554 int wxGrid::GetColMinimalWidth(int col
) const
9556 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9557 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9560 int wxGrid::GetRowMinimalHeight(int row
) const
9562 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9563 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9566 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9568 // We do allow a width of 0 since this gives us
9569 // an easy way to temporarily hidding columns.
9572 m_minAcceptableColWidth
= width
;
9575 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9577 // We do allow a height of 0 since this gives us
9578 // an easy way to temporarily hidding rows.
9581 m_minAcceptableRowHeight
= height
;
9584 int wxGrid::GetColMinimalAcceptableWidth() const
9586 return m_minAcceptableColWidth
;
9589 int wxGrid::GetRowMinimalAcceptableHeight() const
9591 return m_minAcceptableRowHeight
;
9594 // ----------------------------------------------------------------------------
9596 // ----------------------------------------------------------------------------
9598 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9600 wxClientDC
dc(m_gridWin
);
9602 //Cancel editting of cell
9603 HideCellEditControl();
9604 SaveEditControlValue();
9606 // init both of them to avoid compiler warnings, even if weo nly need one
9614 wxCoord extent
, extentMax
= 0;
9615 int max
= column
? m_numRows
: m_numCols
;
9616 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9623 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9624 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9627 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9628 extent
= column
? size
.x
: size
.y
;
9629 if ( extent
> extentMax
)
9640 // now also compare with the column label extent
9642 dc
.SetFont( GetLabelFont() );
9646 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9647 if( GetColLabelTextOrientation() == wxVERTICAL
)
9651 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9653 extent
= column
? w
: h
;
9654 if ( extent
> extentMax
)
9661 // empty column - give default extent (notice that if extentMax is less
9662 // than default extent but != 0, it's ok)
9663 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9669 // leave some space around text
9680 SetColSize(col
, extentMax
);
9681 if ( !GetBatchCount() )
9684 m_gridWin
->GetClientSize( &cw
, &ch
);
9685 wxRect
rect ( CellToRect( 0, col
) );
9687 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9688 rect
.width
= cw
- rect
.x
;
9689 rect
.height
= m_colLabelHeight
;
9690 m_colLabelWin
->Refresh( true, &rect
);
9695 SetRowSize(row
, extentMax
);
9696 if ( !GetBatchCount() )
9699 m_gridWin
->GetClientSize( &cw
, &ch
);
9700 wxRect
rect ( CellToRect( row
, 0 ) );
9702 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9703 rect
.width
= m_rowLabelWidth
;
9704 rect
.height
= ch
- rect
.y
;
9705 m_rowLabelWin
->Refresh( true, &rect
);
9711 SetColMinimalWidth(col
, extentMax
);
9713 SetRowMinimalHeight(row
, extentMax
);
9717 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9719 int width
= m_rowLabelWidth
;
9724 for ( int col
= 0; col
< m_numCols
; col
++ )
9728 AutoSizeColumn(col
, setAsMin
);
9731 width
+= GetColWidth(col
);
9740 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9742 int height
= m_colLabelHeight
;
9747 for ( int row
= 0; row
< m_numRows
; row
++ )
9751 AutoSizeRow(row
, setAsMin
);
9754 height
+= GetRowHeight(row
);
9763 void wxGrid::AutoSize()
9767 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9769 // round up the size to a multiple of scroll step - this ensures that we
9770 // won't get the scrollbars if we're sized exactly to this width
9771 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9773 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9774 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9776 // distribute the extra space between the columns/rows to avoid having
9777 // extra white space
9779 // Remove the extra m_extraWidth + 1 added above
9780 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9781 if ( diff
&& m_numCols
)
9783 // try to resize the columns uniformly
9784 wxCoord diffPerCol
= diff
/ m_numCols
;
9787 for ( int col
= 0; col
< m_numCols
; col
++ )
9789 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9793 // add remaining amount to the last columns
9794 diff
-= diffPerCol
* m_numCols
;
9797 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9799 SetColSize(col
, GetColWidth(col
) + 1);
9805 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9806 if ( diff
&& m_numRows
)
9808 // try to resize the columns uniformly
9809 wxCoord diffPerRow
= diff
/ m_numRows
;
9812 for ( int row
= 0; row
< m_numRows
; row
++ )
9814 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9818 // add remaining amount to the last rows
9819 diff
-= diffPerRow
* m_numRows
;
9822 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9824 SetRowSize(row
, GetRowHeight(row
) + 1);
9831 SetClientSize(sizeFit
);
9834 void wxGrid::AutoSizeRowLabelSize( int row
)
9836 wxArrayString lines
;
9839 // Hide the edit control, so it
9840 // won't interfer with drag-shrinking.
9841 if( IsCellEditControlShown() )
9843 HideCellEditControl();
9844 SaveEditControlValue();
9847 // autosize row height depending on label text
9848 StringToLines( GetRowLabelValue( row
), lines
);
9849 wxClientDC
dc( m_rowLabelWin
);
9850 GetTextBoxSize( dc
, lines
, &w
, &h
);
9851 if( h
< m_defaultRowHeight
)
9852 h
= m_defaultRowHeight
;
9857 void wxGrid::AutoSizeColLabelSize( int col
)
9859 wxArrayString lines
;
9862 // Hide the edit control, so it
9863 // won't interfer with drag-shrinking.
9864 if( IsCellEditControlShown() )
9866 HideCellEditControl();
9867 SaveEditControlValue();
9870 // autosize column width depending on label text
9871 StringToLines( GetColLabelValue( col
), lines
);
9872 wxClientDC
dc( m_colLabelWin
);
9873 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9874 GetTextBoxSize( dc
, lines
, &w
, &h
);
9876 GetTextBoxSize( dc
, lines
, &h
, &w
);
9877 if( w
< m_defaultColWidth
)
9878 w
= m_defaultColWidth
;
9883 wxSize
wxGrid::DoGetBestSize() const
9885 // don't set sizes, only calculate them
9886 wxGrid
*self
= (wxGrid
*)this; // const_cast
9889 width
= self
->SetOrCalcColumnSizes(true);
9890 height
= self
->SetOrCalcRowSizes(true);
9892 if (!width
) width
=100;
9893 if (!height
) height
=80;
9895 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9896 // of the scrollbars, is there any magic incantaion for that?
9898 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9900 width
+= 1 + xpu
- (width
% xpu
);
9902 height
+= 1 + ypu
- (height
% ypu
);
9904 // limit to 1/4 of the screen size
9905 int maxwidth
, maxheight
;
9906 wxDisplaySize( & maxwidth
, & maxheight
);
9909 if ( width
> maxwidth
) width
= maxwidth
;
9910 if ( height
> maxheight
) height
= maxheight
;
9913 wxSize
best(width
, height
);
9914 // NOTE: This size should be cached, but first we need to add calls to
9915 // InvalidateBestSize everywhere that could change the results of this
9917 // CacheBestSize(size);
9927 wxPen
& wxGrid::GetDividerPen() const
9932 // ----------------------------------------------------------------------------
9933 // cell value accessor functions
9934 // ----------------------------------------------------------------------------
9936 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9940 m_table
->SetValue( row
, col
, s
);
9941 if ( !GetBatchCount() )
9944 wxRect
rect( CellToRect( row
, col
) );
9946 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9947 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9948 m_gridWin
->Refresh( false, &rect
);
9951 if ( m_currentCellCoords
.GetRow() == row
&&
9952 m_currentCellCoords
.GetCol() == col
&&
9953 IsCellEditControlShown())
9954 // Note: If we are using IsCellEditControlEnabled,
9955 // this interacts badly with calling SetCellValue from
9956 // an EVT_GRID_CELL_CHANGE handler.
9958 HideCellEditControl();
9959 ShowCellEditControl(); // will reread data from table
9966 // ------ Block, row and col selection
9969 void wxGrid::SelectRow( int row
, bool addToSelected
)
9971 if ( IsSelection() && !addToSelected
)
9975 m_selection
->SelectRow( row
, false, addToSelected
);
9979 void wxGrid::SelectCol( int col
, bool addToSelected
)
9981 if ( IsSelection() && !addToSelected
)
9985 m_selection
->SelectCol( col
, false, addToSelected
);
9989 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9990 bool addToSelected
)
9992 if ( IsSelection() && !addToSelected
)
9996 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9997 false, addToSelected
);
10001 void wxGrid::SelectAll()
10003 if ( m_numRows
> 0 && m_numCols
> 0 )
10006 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10011 // ------ Cell, row and col deselection
10014 void wxGrid::DeselectRow( int row
)
10016 if ( !m_selection
)
10019 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10021 if ( m_selection
->IsInSelection(row
, 0 ) )
10022 m_selection
->ToggleCellSelection( row
, 0);
10026 int nCols
= GetNumberCols();
10027 for ( int i
= 0; i
< nCols
; i
++ )
10029 if ( m_selection
->IsInSelection(row
, i
) )
10030 m_selection
->ToggleCellSelection( row
, i
);
10035 void wxGrid::DeselectCol( int col
)
10037 if ( !m_selection
)
10040 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10042 if ( m_selection
->IsInSelection(0, col
) )
10043 m_selection
->ToggleCellSelection( 0, col
);
10047 int nRows
= GetNumberRows();
10048 for ( int i
= 0; i
< nRows
; i
++ )
10050 if ( m_selection
->IsInSelection(i
, col
) )
10051 m_selection
->ToggleCellSelection(i
, col
);
10056 void wxGrid::DeselectCell( int row
, int col
)
10058 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10059 m_selection
->ToggleCellSelection(row
, col
);
10062 bool wxGrid::IsSelection()
10064 return ( m_selection
&& (m_selection
->IsSelection() ||
10065 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10066 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10069 bool wxGrid::IsInSelection( int row
, int col
) const
10071 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10072 ( row
>= m_selectingTopLeft
.GetRow() &&
10073 col
>= m_selectingTopLeft
.GetCol() &&
10074 row
<= m_selectingBottomRight
.GetRow() &&
10075 col
<= m_selectingBottomRight
.GetCol() )) );
10078 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10080 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10081 return m_selection
->m_cellSelection
;
10083 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10085 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10086 return m_selection
->m_blockSelectionTopLeft
;
10088 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10090 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10091 return m_selection
->m_blockSelectionBottomRight
;
10093 wxArrayInt
wxGrid::GetSelectedRows() const
10095 if (!m_selection
) { wxArrayInt a
; return a
; }
10096 return m_selection
->m_rowSelection
;
10098 wxArrayInt
wxGrid::GetSelectedCols() const
10100 if (!m_selection
) { wxArrayInt a
; return a
; }
10101 return m_selection
->m_colSelection
;
10105 void wxGrid::ClearSelection()
10107 m_selectingTopLeft
= wxGridNoCellCoords
;
10108 m_selectingBottomRight
= wxGridNoCellCoords
;
10110 m_selection
->ClearSelection();
10114 // This function returns the rectangle that encloses the given block
10115 // in device coords clipped to the client size of the grid window.
10117 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10118 const wxGridCellCoords
&bottomRight
)
10120 wxRect
rect( wxGridNoCellRect
);
10123 cellRect
= CellToRect( topLeft
);
10124 if ( cellRect
!= wxGridNoCellRect
)
10130 rect
= wxRect( 0, 0, 0, 0 );
10133 cellRect
= CellToRect( bottomRight
);
10134 if ( cellRect
!= wxGridNoCellRect
)
10140 return wxGridNoCellRect
;
10144 int left
= rect
.GetLeft();
10145 int top
= rect
.GetTop();
10146 int right
= rect
.GetRight();
10147 int bottom
= rect
.GetBottom();
10149 int leftCol
= topLeft
.GetCol();
10150 int topRow
= topLeft
.GetRow();
10151 int rightCol
= bottomRight
.GetCol();
10152 int bottomRow
= bottomRight
.GetRow();
10170 topRow
= bottomRow
;
10175 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10177 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10179 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10181 cellRect
= CellToRect( j
, i
);
10183 if (cellRect
.x
< left
)
10185 if (cellRect
.y
< top
)
10187 if (cellRect
.x
+ cellRect
.width
> right
)
10188 right
= cellRect
.x
+ cellRect
.width
;
10189 if (cellRect
.y
+ cellRect
.height
> bottom
)
10190 bottom
= cellRect
.y
+ cellRect
.height
;
10192 else i
= rightCol
; // jump over inner cells.
10196 // convert to scrolled coords
10198 CalcScrolledPosition( left
, top
, &left
, &top
);
10199 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10202 m_gridWin
->GetClientSize( &cw
, &ch
);
10204 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10205 return wxRect( 0, 0, 0, 0);
10207 rect
.SetLeft( wxMax(0, left
) );
10208 rect
.SetTop( wxMax(0, top
) );
10209 rect
.SetRight( wxMin(cw
, right
) );
10210 rect
.SetBottom( wxMin(ch
, bottom
) );
10216 // ------ Grid event classes
10219 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10221 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10222 int row
, int col
, int x
, int y
, bool sel
,
10223 bool control
, bool shift
, bool alt
, bool meta
)
10224 : wxNotifyEvent( type
, id
)
10231 m_control
= control
;
10236 SetEventObject(obj
);
10240 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10242 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10243 int rowOrCol
, int x
, int y
,
10244 bool control
, bool shift
, bool alt
, bool meta
)
10245 : wxNotifyEvent( type
, id
)
10247 m_rowOrCol
= rowOrCol
;
10250 m_control
= control
;
10255 SetEventObject(obj
);
10259 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10261 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10262 const wxGridCellCoords
& topLeft
,
10263 const wxGridCellCoords
& bottomRight
,
10264 bool sel
, bool control
,
10265 bool shift
, bool alt
, bool meta
)
10266 : wxNotifyEvent( type
, id
)
10268 m_topLeft
= topLeft
;
10269 m_bottomRight
= bottomRight
;
10271 m_control
= control
;
10276 SetEventObject(obj
);
10280 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10282 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10283 wxObject
* obj
, int row
,
10284 int col
, wxControl
* ctrl
)
10285 : wxCommandEvent(type
, id
)
10287 SetEventObject(obj
);
10293 #endif // wxUSE_GRID