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 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1076 bool is_decimal_point
= ( strbuf
==
1077 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1078 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1080 || keycode
== WXK_NUMPAD0
1081 || keycode
== WXK_NUMPAD1
1082 || keycode
== WXK_NUMPAD2
1083 || keycode
== WXK_NUMPAD3
1084 || keycode
== WXK_NUMPAD4
1085 || keycode
== WXK_NUMPAD5
1086 || keycode
== WXK_NUMPAD6
1087 || keycode
== WXK_NUMPAD7
1088 || keycode
== WXK_NUMPAD8
1089 || keycode
== WXK_NUMPAD9
1090 || keycode
== WXK_ADD
1091 || keycode
== WXK_NUMPAD_ADD
1092 || keycode
== WXK_SUBTRACT
1093 || keycode
== WXK_NUMPAD_SUBTRACT
)
1095 wxGridCellTextEditor::StartingKey(event
);
1097 // skip Skip() below
1104 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1115 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1119 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1121 m_precision
= (int)tmp
;
1123 // skip the error message below
1128 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1132 wxString
wxGridCellFloatEditor::GetString() const
1135 if ( m_width
== -1 )
1137 // default width/precision
1140 else if ( m_precision
== -1 )
1142 // default precision
1143 fmt
.Printf(_T("%%%d.f"), m_width
);
1147 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1150 return wxString::Format(fmt
, m_valueOld
);
1153 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1155 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1157 int keycode
= event
.GetKeyCode();
1171 case WXK_NUMPAD_ADD
:
1173 case WXK_NUMPAD_SUBTRACT
:
1175 case WXK_NUMPAD_DECIMAL
:
1180 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1182 tmpbuf
[0] = (char) keycode
;
1184 bool is_decimal_point
=
1185 ( wxString(tmpbuf
, *wxConvCurrent
) ==
1186 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1187 wxLOCALE_CAT_NUMBER
) );
1188 if ( (keycode
< 128) &&
1189 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1190 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1199 #endif // wxUSE_TEXTCTRL
1203 // ----------------------------------------------------------------------------
1204 // wxGridCellBoolEditor
1205 // ----------------------------------------------------------------------------
1207 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1209 wxEvtHandler
* evtHandler
)
1211 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1212 wxDefaultPosition
, wxDefaultSize
,
1215 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1218 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1220 bool resize
= false;
1221 wxSize size
= m_control
->GetSize();
1222 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1224 // check if the checkbox is not too big/small for this cell
1225 wxSize sizeBest
= m_control
->GetBestSize();
1226 if ( !(size
== sizeBest
) )
1228 // reset to default size if it had been made smaller
1234 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1236 // leave 1 pixel margin
1237 size
.x
= size
.y
= minSize
- 2;
1244 m_control
->SetSize(size
);
1247 // position it in the centre of the rectangle (TODO: support alignment?)
1249 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1250 // the checkbox without label still has some space to the right in wxGTK,
1251 // so shift it to the right
1253 #elif defined(__WXMSW__)
1254 // here too, but in other way
1259 int hAlign
= wxALIGN_CENTRE
;
1260 int vAlign
= wxALIGN_CENTRE
;
1262 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1265 if (hAlign
== wxALIGN_LEFT
)
1271 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1273 else if (hAlign
== wxALIGN_RIGHT
)
1275 x
= r
.x
+ r
.width
- size
.x
- 2;
1276 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1278 else if (hAlign
== wxALIGN_CENTRE
)
1280 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1281 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1284 m_control
->Move(x
, y
);
1287 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1289 m_control
->Show(show
);
1293 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1294 CBox()->SetBackgroundColour(colBg
);
1298 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1300 wxASSERT_MSG(m_control
,
1301 wxT("The wxGridCellEditor must be Created first!"));
1303 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1304 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1307 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1308 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1310 CBox()->SetValue(m_startValue
);
1314 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1317 wxASSERT_MSG(m_control
,
1318 wxT("The wxGridCellEditor must be Created first!"));
1320 bool changed
= false;
1321 bool value
= CBox()->GetValue();
1322 if ( value
!= m_startValue
)
1327 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1328 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1330 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1336 void wxGridCellBoolEditor::Reset()
1338 wxASSERT_MSG(m_control
,
1339 wxT("The wxGridCellEditor must be Created first!"));
1341 CBox()->SetValue(m_startValue
);
1344 void wxGridCellBoolEditor::StartingClick()
1346 CBox()->SetValue(!CBox()->GetValue());
1349 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1351 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1353 int keycode
= event
.GetKeyCode();
1357 case WXK_NUMPAD_MULTIPLY
:
1359 case WXK_NUMPAD_ADD
:
1361 case WXK_NUMPAD_SUBTRACT
:
1372 // return the value as "1" for true and the empty string for false
1373 wxString
wxGridCellBoolEditor::GetValue() const
1375 bool bSet
= CBox()->GetValue();
1376 return bSet
? _T("1") : wxEmptyString
;
1379 #endif // wxUSE_CHECKBOX
1383 // ----------------------------------------------------------------------------
1384 // wxGridCellChoiceEditor
1385 // ----------------------------------------------------------------------------
1387 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1389 : m_choices(choices
),
1390 m_allowOthers(allowOthers
) { }
1392 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1393 const wxString choices
[],
1395 : m_allowOthers(allowOthers
)
1399 m_choices
.Alloc(count
);
1400 for ( size_t n
= 0; n
< count
; n
++ )
1402 m_choices
.Add(choices
[n
]);
1407 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1409 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1410 editor
->m_allowOthers
= m_allowOthers
;
1411 editor
->m_choices
= m_choices
;
1416 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1418 wxEvtHandler
* evtHandler
)
1420 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1421 wxDefaultPosition
, wxDefaultSize
,
1423 m_allowOthers
? 0 : wxCB_READONLY
);
1425 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1428 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1429 wxGridCellAttr
* attr
)
1431 // as we fill the entire client area, don't do anything here to minimize
1434 // TODO: It doesn't actually fill the client area since the height of a
1435 // combo always defaults to the standard... Until someone has time to
1436 // figure out the right rectangle to paint, just do it the normal way...
1437 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1440 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1442 wxASSERT_MSG(m_control
,
1443 wxT("The wxGridCellEditor must be Created first!"));
1445 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1448 Combo()->SetValue(m_startValue
);
1451 // find the right position, or default to the first if not found
1452 int pos
= Combo()->FindString(m_startValue
);
1455 Combo()->SetSelection(pos
);
1457 Combo()->SetInsertionPointEnd();
1458 Combo()->SetFocus();
1461 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1464 wxString value
= Combo()->GetValue();
1465 if ( value
== m_startValue
)
1468 grid
->GetTable()->SetValue(row
, col
, value
);
1473 void wxGridCellChoiceEditor::Reset()
1475 Combo()->SetValue(m_startValue
);
1476 Combo()->SetInsertionPointEnd();
1479 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1489 wxStringTokenizer
tk(params
, _T(','));
1490 while ( tk
.HasMoreTokens() )
1492 m_choices
.Add(tk
.GetNextToken());
1496 // return the value in the text control
1497 wxString
wxGridCellChoiceEditor::GetValue() const
1499 return Combo()->GetValue();
1502 #endif // wxUSE_COMBOBOX
1504 // ----------------------------------------------------------------------------
1505 // wxGridCellEditorEvtHandler
1506 // ----------------------------------------------------------------------------
1508 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1510 switch ( event
.GetKeyCode() )
1514 m_grid
->DisableCellEditControl();
1518 m_grid
->GetEventHandler()->ProcessEvent( event
);
1522 case WXK_NUMPAD_ENTER
:
1523 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1524 m_editor
->HandleReturn(event
);
1533 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1535 switch ( event
.GetKeyCode() )
1540 case WXK_NUMPAD_ENTER
:
1548 // ----------------------------------------------------------------------------
1549 // wxGridCellWorker is an (almost) empty common base class for
1550 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1551 // ----------------------------------------------------------------------------
1553 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1558 wxGridCellWorker::~wxGridCellWorker()
1562 // ============================================================================
1564 // ============================================================================
1566 // ----------------------------------------------------------------------------
1567 // wxGridCellRenderer
1568 // ----------------------------------------------------------------------------
1570 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1571 wxGridCellAttr
& attr
,
1574 int WXUNUSED(row
), int WXUNUSED(col
),
1577 dc
.SetBackgroundMode( wxSOLID
);
1579 // grey out fields if the grid is disabled
1580 if( grid
.IsEnabled() )
1584 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1588 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1593 dc
.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1596 dc
.SetPen( *wxTRANSPARENT_PEN
);
1597 dc
.DrawRectangle(rect
);
1600 // ----------------------------------------------------------------------------
1601 // wxGridCellStringRenderer
1602 // ----------------------------------------------------------------------------
1604 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1605 wxGridCellAttr
& attr
,
1609 dc
.SetBackgroundMode( wxTRANSPARENT
);
1611 // TODO some special colours for attr.IsReadOnly() case?
1613 // different coloured text when the grid is disabled
1614 if( grid
.IsEnabled() )
1618 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1619 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1623 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1624 dc
.SetTextForeground( attr
.GetTextColour() );
1629 dc
.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE
));
1630 dc
.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT
));
1633 dc
.SetFont( attr
.GetFont() );
1636 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1638 const wxString
& text
)
1640 wxCoord x
= 0, y
= 0, max_x
= 0;
1641 dc
.SetFont(attr
.GetFont());
1642 wxStringTokenizer
tk(text
, _T('\n'));
1643 while ( tk
.HasMoreTokens() )
1645 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1646 max_x
= wxMax(max_x
, x
);
1649 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1651 return wxSize(max_x
, y
);
1654 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1655 wxGridCellAttr
& attr
,
1659 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1662 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1663 wxGridCellAttr
& attr
,
1665 const wxRect
& rectCell
,
1669 wxRect rect
= rectCell
;
1672 // erase only this cells background, overflow cells should have been erased
1673 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1676 attr
.GetAlignment(&hAlign
, &vAlign
);
1678 int overflowCols
= 0;
1680 if (attr
.GetOverflow())
1682 int cols
= grid
.GetNumberCols();
1683 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1684 int cell_rows
, cell_cols
;
1685 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1686 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1688 int i
, c_cols
, c_rows
;
1689 for (i
= col
+cell_cols
; i
< cols
; i
++)
1691 bool is_empty
= true;
1692 for (int j
=row
; j
<row
+cell_rows
; j
++)
1694 // check w/ anchor cell for multicell block
1695 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1696 if (c_rows
> 0) c_rows
= 0;
1697 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1704 rect
.width
+= grid
.GetColSize(i
);
1710 if (rect
.width
>= best_width
) break;
1712 overflowCols
= i
- col
- cell_cols
+ 1;
1713 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1716 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1718 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1720 clip
.x
+= rectCell
.width
;
1721 // draw each overflow cell individually
1722 int col_end
= col
+cell_cols
+overflowCols
;
1723 if (col_end
>= grid
.GetNumberCols())
1724 col_end
= grid
.GetNumberCols() - 1;
1725 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1727 clip
.width
= grid
.GetColSize(i
) - 1;
1728 dc
.DestroyClippingRegion();
1729 dc
.SetClippingRegion(clip
);
1731 SetTextColoursAndFont(grid
, attr
, dc
,
1732 grid
.IsInSelection(row
,i
));
1734 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1735 rect
, hAlign
, vAlign
);
1736 clip
.x
+= grid
.GetColSize(i
) - 1;
1742 dc
.DestroyClippingRegion();
1746 // now we only have to draw the text
1747 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1749 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1750 rect
, hAlign
, vAlign
);
1753 // ----------------------------------------------------------------------------
1754 // wxGridCellNumberRenderer
1755 // ----------------------------------------------------------------------------
1757 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1759 wxGridTableBase
*table
= grid
.GetTable();
1761 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1763 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1767 text
= table
->GetValue(row
, col
);
1773 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1774 wxGridCellAttr
& attr
,
1776 const wxRect
& rectCell
,
1780 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1782 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1784 // draw the text right aligned by default
1786 attr
.GetAlignment(&hAlign
, &vAlign
);
1787 hAlign
= wxALIGN_RIGHT
;
1789 wxRect rect
= rectCell
;
1792 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1795 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1796 wxGridCellAttr
& attr
,
1800 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1803 // ----------------------------------------------------------------------------
1804 // wxGridCellFloatRenderer
1805 // ----------------------------------------------------------------------------
1807 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1810 SetPrecision(precision
);
1813 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1815 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1816 renderer
->m_width
= m_width
;
1817 renderer
->m_precision
= m_precision
;
1818 renderer
->m_format
= m_format
;
1823 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1825 wxGridTableBase
*table
= grid
.GetTable();
1830 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1832 val
= table
->GetValueAsDouble(row
, col
);
1837 text
= table
->GetValue(row
, col
);
1838 hasDouble
= text
.ToDouble(&val
);
1845 if ( m_width
== -1 )
1847 if ( m_precision
== -1 )
1849 // default width/precision
1850 m_format
= _T("%f");
1854 m_format
.Printf(_T("%%.%df"), m_precision
);
1857 else if ( m_precision
== -1 )
1859 // default precision
1860 m_format
.Printf(_T("%%%d.f"), m_width
);
1864 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1868 text
.Printf(m_format
, val
);
1871 //else: text already contains the string
1876 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1877 wxGridCellAttr
& attr
,
1879 const wxRect
& rectCell
,
1883 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1885 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1887 // draw the text right aligned by default
1889 attr
.GetAlignment(&hAlign
, &vAlign
);
1890 hAlign
= wxALIGN_RIGHT
;
1892 wxRect rect
= rectCell
;
1895 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1898 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1899 wxGridCellAttr
& attr
,
1903 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1906 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1910 // reset to defaults
1916 wxString tmp
= params
.BeforeFirst(_T(','));
1920 if ( tmp
.ToLong(&width
) )
1922 SetWidth((int)width
);
1926 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1930 tmp
= params
.AfterFirst(_T(','));
1934 if ( tmp
.ToLong(&precision
) )
1936 SetPrecision((int)precision
);
1940 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1948 // ----------------------------------------------------------------------------
1949 // wxGridCellBoolRenderer
1950 // ----------------------------------------------------------------------------
1952 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1954 // FIXME these checkbox size calculations are really ugly...
1956 // between checkmark and box
1957 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1959 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1960 wxGridCellAttr
& WXUNUSED(attr
),
1965 // compute it only once (no locks for MT safeness in GUI thread...)
1966 if ( !ms_sizeCheckMark
.x
)
1968 // get checkbox size
1969 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1970 wxSize size
= checkbox
->GetBestSize();
1971 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1973 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1974 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1975 checkSize
-= size
.y
/ 2;
1980 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1983 return ms_sizeCheckMark
;
1986 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1987 wxGridCellAttr
& attr
,
1993 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1995 // draw a check mark in the centre (ignoring alignment - TODO)
1996 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1998 // don't draw outside the cell
1999 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2000 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2002 // and even leave (at least) 1 pixel margin
2003 size
.x
= size
.y
= minSize
- 2;
2006 // draw a border around checkmark
2008 attr
.GetAlignment(& hAlign
, &vAlign
);
2011 if (hAlign
== wxALIGN_CENTRE
)
2013 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2014 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2015 rectBorder
.width
= size
.x
;
2016 rectBorder
.height
= size
.y
;
2018 else if (hAlign
== wxALIGN_LEFT
)
2020 rectBorder
.x
= rect
.x
+ 2;
2021 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2022 rectBorder
.width
= size
.x
;
2023 rectBorder
.height
= size
.y
;
2025 else if (hAlign
== wxALIGN_RIGHT
)
2027 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2028 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2029 rectBorder
.width
= size
.x
;
2030 rectBorder
.height
= size
.y
;
2034 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2035 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2038 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2039 value
= !( !cellval
|| (cellval
== wxT("0")) );
2044 wxRect rectMark
= rectBorder
;
2046 // MSW DrawCheckMark() is weird (and should probably be changed...)
2047 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2051 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2054 dc
.SetTextForeground(attr
.GetTextColour());
2055 dc
.DrawCheckMark(rectMark
);
2058 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2059 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2060 dc
.DrawRectangle(rectBorder
);
2063 // ----------------------------------------------------------------------------
2065 // ----------------------------------------------------------------------------
2067 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2071 m_isReadOnly
= Unset
;
2076 m_attrkind
= wxGridCellAttr::Cell
;
2078 m_sizeRows
= m_sizeCols
= 1;
2079 m_overflow
= UnsetOverflow
;
2081 SetDefAttr(attrDefault
);
2084 wxGridCellAttr
*wxGridCellAttr::Clone() const
2086 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2088 if ( HasTextColour() )
2089 attr
->SetTextColour(GetTextColour());
2090 if ( HasBackgroundColour() )
2091 attr
->SetBackgroundColour(GetBackgroundColour());
2093 attr
->SetFont(GetFont());
2094 if ( HasAlignment() )
2095 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2097 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2101 attr
->SetRenderer(m_renderer
);
2102 m_renderer
->IncRef();
2106 attr
->SetEditor(m_editor
);
2111 attr
->SetReadOnly();
2113 attr
->SetKind( m_attrkind
);
2118 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2120 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2121 SetTextColour(mergefrom
->GetTextColour());
2122 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2123 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2124 if ( !HasFont() && mergefrom
->HasFont() )
2125 SetFont(mergefrom
->GetFont());
2126 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2128 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2129 SetAlignment(hAlign
, vAlign
);
2132 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2134 // Directly access member functions as GetRender/Editor don't just return
2135 // m_renderer/m_editor
2137 // Maybe add support for merge of Render and Editor?
2138 if (!HasRenderer() && mergefrom
->HasRenderer() )
2140 m_renderer
= mergefrom
->m_renderer
;
2141 m_renderer
->IncRef();
2143 if ( !HasEditor() && mergefrom
->HasEditor() )
2145 m_editor
= mergefrom
->m_editor
;
2148 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2149 SetReadOnly(mergefrom
->IsReadOnly());
2151 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2152 SetOverflow(mergefrom
->GetOverflow());
2154 SetDefAttr(mergefrom
->m_defGridAttr
);
2157 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2159 // The size of a cell is normally 1,1
2161 // If this cell is larger (2,2) then this is the top left cell
2162 // the other cells that will be covered (lower right cells) must be
2163 // set to negative or zero values such that
2164 // row + num_rows of the covered cell points to the larger cell (this cell)
2165 // same goes for the col + num_cols.
2167 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2169 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2170 !((num_rows
<=0)&&(num_cols
>0)) ||
2171 !((num_rows
==0)&&(num_cols
==0))),
2172 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2174 m_sizeRows
= num_rows
;
2175 m_sizeCols
= num_cols
;
2178 const wxColour
& wxGridCellAttr::GetTextColour() const
2180 if (HasTextColour())
2184 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2186 return m_defGridAttr
->GetTextColour();
2190 wxFAIL_MSG(wxT("Missing default cell attribute"));
2191 return wxNullColour
;
2196 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2198 if (HasBackgroundColour())
2200 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2201 return m_defGridAttr
->GetBackgroundColour();
2204 wxFAIL_MSG(wxT("Missing default cell attribute"));
2205 return wxNullColour
;
2210 const wxFont
& wxGridCellAttr::GetFont() const
2214 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2215 return m_defGridAttr
->GetFont();
2218 wxFAIL_MSG(wxT("Missing default cell attribute"));
2224 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2228 if ( hAlign
) *hAlign
= m_hAlign
;
2229 if ( vAlign
) *vAlign
= m_vAlign
;
2231 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2232 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2235 wxFAIL_MSG(wxT("Missing default cell attribute"));
2239 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2241 if ( num_rows
) *num_rows
= m_sizeRows
;
2242 if ( num_cols
) *num_cols
= m_sizeCols
;
2245 // GetRenderer and GetEditor use a slightly different decision path about
2246 // which attribute to use. If a non-default attr object has one then it is
2247 // used, otherwise the default editor or renderer is fetched from the grid and
2248 // used. It should be the default for the data type of the cell. If it is
2249 // NULL (because the table has a type that the grid does not have in its
2250 // registry,) then the grid's default editor or renderer is used.
2252 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2254 wxGridCellRenderer
*renderer
;
2256 if ( m_renderer
&& this != m_defGridAttr
)
2258 // use the cells renderer if it has one
2259 renderer
= m_renderer
;
2262 else // no non default cell renderer
2264 // get default renderer for the data type
2267 // GetDefaultRendererForCell() will do IncRef() for us
2268 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2277 if (m_defGridAttr
&& this != m_defGridAttr
)
2279 // if we still don't have one then use the grid default
2280 // (no need for IncRef() here neither)
2281 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2283 else // default grid attr
2285 // use m_renderer which we had decided not to use initially
2286 renderer
= m_renderer
;
2293 // we're supposed to always find something
2294 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2299 // same as above, except for s/renderer/editor/g
2300 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2302 wxGridCellEditor
*editor
;
2304 if ( m_editor
&& this != m_defGridAttr
)
2306 // use the cells editor if it has one
2310 else // no non default cell editor
2312 // get default editor for the data type
2315 // GetDefaultEditorForCell() will do IncRef() for us
2316 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2325 if ( m_defGridAttr
&& this != m_defGridAttr
)
2327 // if we still don't have one then use the grid default
2328 // (no need for IncRef() here neither)
2329 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2331 else // default grid attr
2333 // use m_editor which we had decided not to use initially
2341 // we're supposed to always find something
2342 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2347 // ----------------------------------------------------------------------------
2348 // wxGridCellAttrData
2349 // ----------------------------------------------------------------------------
2351 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2353 int n
= FindIndex(row
, col
);
2354 if ( n
== wxNOT_FOUND
)
2356 // add the attribute
2357 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2361 // free the old attribute
2362 m_attrs
[(size_t)n
].attr
->DecRef();
2366 // change the attribute
2367 m_attrs
[(size_t)n
].attr
= attr
;
2371 // remove this attribute
2372 m_attrs
.RemoveAt((size_t)n
);
2377 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2379 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2381 int n
= FindIndex(row
, col
);
2382 if ( n
!= wxNOT_FOUND
)
2384 attr
= m_attrs
[(size_t)n
].attr
;
2391 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2393 size_t count
= m_attrs
.GetCount();
2394 for ( size_t n
= 0; n
< count
; n
++ )
2396 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2397 wxCoord row
= coords
.GetRow();
2398 if ((size_t)row
>= pos
)
2402 // If rows inserted, include row counter where necessary
2403 coords
.SetRow(row
+ numRows
);
2405 else if (numRows
< 0)
2407 // If rows deleted ...
2408 if ((size_t)row
>= pos
- numRows
)
2410 // ...either decrement row counter (if row still exists)...
2411 coords
.SetRow(row
+ numRows
);
2415 // ...or remove the attribute
2416 m_attrs
.RemoveAt((size_t)n
);
2424 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2426 size_t count
= m_attrs
.GetCount();
2427 for ( size_t n
= 0; n
< count
; n
++ )
2429 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2430 wxCoord col
= coords
.GetCol();
2431 if ( (size_t)col
>= pos
)
2435 // If rows inserted, include row counter where necessary
2436 coords
.SetCol(col
+ numCols
);
2438 else if (numCols
< 0)
2440 // If rows deleted ...
2441 if ((size_t)col
>= pos
- numCols
)
2443 // ...either decrement row counter (if row still exists)...
2444 coords
.SetCol(col
+ numCols
);
2448 // ...or remove the attribute
2449 m_attrs
.RemoveAt((size_t)n
);
2457 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2459 size_t count
= m_attrs
.GetCount();
2460 for ( size_t n
= 0; n
< count
; n
++ )
2462 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2463 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2472 // ----------------------------------------------------------------------------
2473 // wxGridRowOrColAttrData
2474 // ----------------------------------------------------------------------------
2476 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2478 size_t count
= m_attrs
.Count();
2479 for ( size_t n
= 0; n
< count
; n
++ )
2481 m_attrs
[n
]->DecRef();
2485 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2487 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2489 int n
= m_rowsOrCols
.Index(rowOrCol
);
2490 if ( n
!= wxNOT_FOUND
)
2492 attr
= m_attrs
[(size_t)n
];
2499 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2501 int i
= m_rowsOrCols
.Index(rowOrCol
);
2502 if ( i
== wxNOT_FOUND
)
2504 // add the attribute
2505 m_rowsOrCols
.Add(rowOrCol
);
2510 size_t n
= (size_t)i
;
2513 // change the attribute
2514 m_attrs
[n
]->DecRef();
2519 // remove this attribute
2520 m_attrs
[n
]->DecRef();
2521 m_rowsOrCols
.RemoveAt(n
);
2522 m_attrs
.RemoveAt(n
);
2527 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2529 size_t count
= m_attrs
.GetCount();
2530 for ( size_t n
= 0; n
< count
; n
++ )
2532 int & rowOrCol
= m_rowsOrCols
[n
];
2533 if ( (size_t)rowOrCol
>= pos
)
2535 if ( numRowsOrCols
> 0 )
2537 // If rows inserted, include row counter where necessary
2538 rowOrCol
+= numRowsOrCols
;
2540 else if ( numRowsOrCols
< 0)
2542 // If rows deleted, either decrement row counter (if row still exists)
2543 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2544 rowOrCol
+= numRowsOrCols
;
2547 m_rowsOrCols
.RemoveAt((size_t)n
);
2548 m_attrs
.RemoveAt((size_t)n
);
2556 // ----------------------------------------------------------------------------
2557 // wxGridCellAttrProvider
2558 // ----------------------------------------------------------------------------
2560 wxGridCellAttrProvider::wxGridCellAttrProvider()
2562 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2565 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2570 void wxGridCellAttrProvider::InitData()
2572 m_data
= new wxGridCellAttrProviderData
;
2575 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2576 wxGridCellAttr::wxAttrKind kind
) const
2578 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2583 case (wxGridCellAttr::Any
):
2584 //Get cached merge attributes.
2585 // Currenlty not used as no cache implemented as not mutiable
2586 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2589 //Basicaly implement old version.
2590 //Also check merge cache, so we don't have to re-merge every time..
2591 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2592 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2593 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2595 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2596 // Two or more are non NULL
2597 attr
= new wxGridCellAttr
;
2598 attr
->SetKind(wxGridCellAttr::Merged
);
2602 attr
->MergeWith(attrcell
);
2606 attr
->MergeWith(attrcol
);
2610 attr
->MergeWith(attrrow
);
2613 //store merge attr if cache implemented
2615 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2619 // one or none is non null return it or null.
2620 if(attrrow
) attr
= attrrow
;
2636 case (wxGridCellAttr::Cell
):
2637 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2639 case (wxGridCellAttr::Col
):
2640 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2642 case (wxGridCellAttr::Row
):
2643 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2647 // (wxGridCellAttr::Default):
2648 // (wxGridCellAttr::Merged):
2655 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2661 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2664 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2669 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2672 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2677 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2680 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2684 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2686 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2690 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2694 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2696 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2700 // ----------------------------------------------------------------------------
2701 // wxGridTypeRegistry
2702 // ----------------------------------------------------------------------------
2704 wxGridTypeRegistry::~wxGridTypeRegistry()
2706 size_t count
= m_typeinfo
.Count();
2707 for ( size_t i
= 0; i
< count
; i
++ )
2708 delete m_typeinfo
[i
];
2712 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2713 wxGridCellRenderer
* renderer
,
2714 wxGridCellEditor
* editor
)
2716 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2718 // is it already registered?
2719 int loc
= FindRegisteredDataType(typeName
);
2720 if ( loc
!= wxNOT_FOUND
)
2722 delete m_typeinfo
[loc
];
2723 m_typeinfo
[loc
] = info
;
2727 m_typeinfo
.Add(info
);
2731 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2733 size_t count
= m_typeinfo
.GetCount();
2734 for ( size_t i
= 0; i
< count
; i
++ )
2736 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2745 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2747 int index
= FindRegisteredDataType(typeName
);
2748 if ( index
== wxNOT_FOUND
)
2750 // check whether this is one of the standard ones, in which case
2751 // register it "on the fly"
2753 if ( typeName
== wxGRID_VALUE_STRING
)
2755 RegisterDataType(wxGRID_VALUE_STRING
,
2756 new wxGridCellStringRenderer
,
2757 new wxGridCellTextEditor
);
2759 #endif // wxUSE_TEXTCTRL
2761 if ( typeName
== wxGRID_VALUE_BOOL
)
2763 RegisterDataType(wxGRID_VALUE_BOOL
,
2764 new wxGridCellBoolRenderer
,
2765 new wxGridCellBoolEditor
);
2767 #endif // wxUSE_CHECKBOX
2769 if ( typeName
== wxGRID_VALUE_NUMBER
)
2771 RegisterDataType(wxGRID_VALUE_NUMBER
,
2772 new wxGridCellNumberRenderer
,
2773 new wxGridCellNumberEditor
);
2775 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2777 RegisterDataType(wxGRID_VALUE_FLOAT
,
2778 new wxGridCellFloatRenderer
,
2779 new wxGridCellFloatEditor
);
2781 #endif // wxUSE_TEXTCTRL
2783 if ( typeName
== wxGRID_VALUE_CHOICE
)
2785 RegisterDataType(wxGRID_VALUE_CHOICE
,
2786 new wxGridCellStringRenderer
,
2787 new wxGridCellChoiceEditor
);
2789 #endif // wxUSE_COMBOBOX
2794 // we get here only if just added the entry for this type, so return
2796 index
= m_typeinfo
.GetCount() - 1;
2802 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2804 int index
= FindDataType(typeName
);
2805 if ( index
== wxNOT_FOUND
)
2807 // the first part of the typename is the "real" type, anything after ':'
2808 // are the parameters for the renderer
2809 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2810 if ( index
== wxNOT_FOUND
)
2815 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2816 wxGridCellRenderer
*rendererOld
= renderer
;
2817 renderer
= renderer
->Clone();
2818 rendererOld
->DecRef();
2820 wxGridCellEditor
*editor
= GetEditor(index
);
2821 wxGridCellEditor
*editorOld
= editor
;
2822 editor
= editor
->Clone();
2823 editorOld
->DecRef();
2825 // do it even if there are no parameters to reset them to defaults
2826 wxString params
= typeName
.AfterFirst(_T(':'));
2827 renderer
->SetParameters(params
);
2828 editor
->SetParameters(params
);
2830 // register the new typename
2831 RegisterDataType(typeName
, renderer
, editor
);
2833 // we just registered it, it's the last one
2834 index
= m_typeinfo
.GetCount() - 1;
2840 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2842 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2848 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2850 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2856 // ----------------------------------------------------------------------------
2858 // ----------------------------------------------------------------------------
2860 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2863 wxGridTableBase::wxGridTableBase()
2865 m_view
= (wxGrid
*) NULL
;
2866 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2869 wxGridTableBase::~wxGridTableBase()
2871 delete m_attrProvider
;
2874 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2876 delete m_attrProvider
;
2877 m_attrProvider
= attrProvider
;
2880 bool wxGridTableBase::CanHaveAttributes()
2882 if ( ! GetAttrProvider() )
2884 // use the default attr provider by default
2885 SetAttrProvider(new wxGridCellAttrProvider
);
2890 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2892 if ( m_attrProvider
)
2893 return m_attrProvider
->GetAttr(row
, col
, kind
);
2895 return (wxGridCellAttr
*)NULL
;
2898 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2900 if ( m_attrProvider
)
2902 attr
->SetKind(wxGridCellAttr::Cell
);
2903 m_attrProvider
->SetAttr(attr
, row
, col
);
2907 // as we take ownership of the pointer and don't store it, we must
2913 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2915 if ( m_attrProvider
)
2917 attr
->SetKind(wxGridCellAttr::Row
);
2918 m_attrProvider
->SetRowAttr(attr
, row
);
2922 // as we take ownership of the pointer and don't store it, we must
2928 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2930 if ( m_attrProvider
)
2932 attr
->SetKind(wxGridCellAttr::Col
);
2933 m_attrProvider
->SetColAttr(attr
, col
);
2937 // as we take ownership of the pointer and don't store it, we must
2943 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2944 size_t WXUNUSED(numRows
) )
2946 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2951 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2953 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2958 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2959 size_t WXUNUSED(numRows
) )
2961 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2966 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2967 size_t WXUNUSED(numCols
) )
2969 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2974 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2976 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2981 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2982 size_t WXUNUSED(numCols
) )
2984 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2990 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2993 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2994 // how much it makes sense to us geeks.
2998 wxString
wxGridTableBase::GetColLabelValue( int col
)
3000 // default col labels are:
3001 // cols 0 to 25 : A-Z
3002 // cols 26 to 675 : AA-ZZ
3007 for ( n
= 1; ; n
++ )
3009 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3011 if ( col
< 0 ) break;
3014 // reverse the string...
3016 for ( i
= 0; i
< n
; i
++ )
3025 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3027 return wxGRID_VALUE_STRING
;
3030 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3031 const wxString
& typeName
)
3033 return typeName
== wxGRID_VALUE_STRING
;
3036 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3038 return CanGetValueAs(row
, col
, typeName
);
3041 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3046 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3051 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3056 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3057 long WXUNUSED(value
) )
3061 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3062 double WXUNUSED(value
) )
3066 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3067 bool WXUNUSED(value
) )
3072 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3073 const wxString
& WXUNUSED(typeName
) )
3078 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3079 const wxString
& WXUNUSED(typeName
),
3080 void* WXUNUSED(value
) )
3084 //////////////////////////////////////////////////////////////////////
3086 // Message class for the grid table to send requests and notifications
3090 wxGridTableMessage::wxGridTableMessage()
3092 m_table
= (wxGridTableBase
*) NULL
;
3098 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3099 int commandInt1
, int commandInt2
)
3103 m_comInt1
= commandInt1
;
3104 m_comInt2
= commandInt2
;
3109 //////////////////////////////////////////////////////////////////////
3111 // A basic grid table for string data. An object of this class will
3112 // created by wxGrid if you don't specify an alternative table class.
3115 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3117 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3119 wxGridStringTable::wxGridStringTable()
3124 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3127 m_data
.Alloc( numRows
);
3130 sa
.Alloc( numCols
);
3131 sa
.Add( wxEmptyString
, numCols
);
3133 m_data
.Add( sa
, numRows
);
3136 wxGridStringTable::~wxGridStringTable()
3140 int wxGridStringTable::GetNumberRows()
3142 return m_data
.GetCount();
3145 int wxGridStringTable::GetNumberCols()
3147 if ( m_data
.GetCount() > 0 )
3148 return m_data
[0].GetCount();
3153 wxString
wxGridStringTable::GetValue( int row
, int col
)
3155 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3157 _T("invalid row or column index in wxGridStringTable") );
3159 return m_data
[row
][col
];
3162 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3164 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3165 _T("invalid row or column index in wxGridStringTable") );
3167 m_data
[row
][col
] = value
;
3170 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3172 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3174 _T("invalid row or column index in wxGridStringTable") );
3176 return (m_data
[row
][col
] == wxEmptyString
);
3179 void wxGridStringTable::Clear()
3182 int numRows
, numCols
;
3184 numRows
= m_data
.GetCount();
3187 numCols
= m_data
[0].GetCount();
3189 for ( row
= 0; row
< numRows
; row
++ )
3191 for ( col
= 0; col
< numCols
; col
++ )
3193 m_data
[row
][col
] = wxEmptyString
;
3200 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3202 size_t curNumRows
= m_data
.GetCount();
3203 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3204 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3206 if ( pos
>= curNumRows
)
3208 return AppendRows( numRows
);
3212 sa
.Alloc( curNumCols
);
3213 sa
.Add( wxEmptyString
, curNumCols
);
3214 m_data
.Insert( sa
, pos
, numRows
);
3217 wxGridTableMessage
msg( this,
3218 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3222 GetView()->ProcessTableMessage( msg
);
3228 bool wxGridStringTable::AppendRows( size_t numRows
)
3230 size_t curNumRows
= m_data
.GetCount();
3231 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3232 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3235 if ( curNumCols
> 0 )
3237 sa
.Alloc( curNumCols
);
3238 sa
.Add( wxEmptyString
, curNumCols
);
3241 m_data
.Add( sa
, numRows
);
3245 wxGridTableMessage
msg( this,
3246 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3249 GetView()->ProcessTableMessage( msg
);
3255 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3257 size_t curNumRows
= m_data
.GetCount();
3259 if ( pos
>= curNumRows
)
3261 wxFAIL_MSG( wxString::Format
3263 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3265 (unsigned long)numRows
,
3266 (unsigned long)curNumRows
3272 if ( numRows
> curNumRows
- pos
)
3274 numRows
= curNumRows
- pos
;
3277 if ( numRows
>= curNumRows
)
3283 m_data
.RemoveAt( pos
, numRows
);
3287 wxGridTableMessage
msg( this,
3288 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3292 GetView()->ProcessTableMessage( msg
);
3298 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3302 size_t curNumRows
= m_data
.GetCount();
3303 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3304 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3306 if ( pos
>= curNumCols
)
3308 return AppendCols( numCols
);
3311 for ( row
= 0; row
< curNumRows
; row
++ )
3313 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3315 m_data
[row
].Insert( wxEmptyString
, col
);
3320 wxGridTableMessage
msg( this,
3321 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3325 GetView()->ProcessTableMessage( msg
);
3331 bool wxGridStringTable::AppendCols( size_t numCols
)
3335 size_t curNumRows
= m_data
.GetCount();
3339 // TODO: something better than this ?
3341 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3346 for ( row
= 0; row
< curNumRows
; row
++ )
3348 m_data
[row
].Add( wxEmptyString
, numCols
);
3353 wxGridTableMessage
msg( this,
3354 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3357 GetView()->ProcessTableMessage( msg
);
3363 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3367 size_t curNumRows
= m_data
.GetCount();
3368 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3369 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3371 if ( pos
>= curNumCols
)
3373 wxFAIL_MSG( wxString::Format
3375 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3377 (unsigned long)numCols
,
3378 (unsigned long)curNumCols
3383 if ( numCols
> curNumCols
- pos
)
3385 numCols
= curNumCols
- pos
;
3388 for ( row
= 0; row
< curNumRows
; row
++ )
3390 if ( numCols
>= curNumCols
)
3392 m_data
[row
].Clear();
3396 m_data
[row
].RemoveAt( pos
, numCols
);
3401 wxGridTableMessage
msg( this,
3402 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3406 GetView()->ProcessTableMessage( msg
);
3412 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3414 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3416 // using default label
3418 return wxGridTableBase::GetRowLabelValue( row
);
3422 return m_rowLabels
[ row
];
3426 wxString
wxGridStringTable::GetColLabelValue( int col
)
3428 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3430 // using default label
3432 return wxGridTableBase::GetColLabelValue( col
);
3436 return m_colLabels
[ col
];
3440 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3442 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3444 int n
= m_rowLabels
.GetCount();
3446 for ( i
= n
; i
<= row
; i
++ )
3448 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3452 m_rowLabels
[row
] = value
;
3455 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3457 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3459 int n
= m_colLabels
.GetCount();
3461 for ( i
= n
; i
<= col
; i
++ )
3463 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3467 m_colLabels
[col
] = value
;
3472 //////////////////////////////////////////////////////////////////////
3473 //////////////////////////////////////////////////////////////////////
3475 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3477 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3478 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3479 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3480 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3481 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3482 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3485 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3487 const wxPoint
&pos
, const wxSize
&size
)
3488 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3493 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3497 // NO - don't do this because it will set both the x and y origin
3498 // coords to match the parent scrolled window and we just want to
3499 // set the y coord - MB
3501 // m_owner->PrepareDC( dc );
3504 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3505 dc
.SetDeviceOrigin( 0, -y
);
3507 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3508 m_owner
->DrawRowLabels( dc
, rows
);
3512 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3514 m_owner
->ProcessRowLabelMouseEvent( event
);
3518 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3520 m_owner
->GetEventHandler()->ProcessEvent(event
);
3524 // This seems to be required for wxMotif otherwise the mouse
3525 // cursor must be in the cell edit control to get key events
3527 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3529 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3532 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3534 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3539 //////////////////////////////////////////////////////////////////////
3541 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3543 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3544 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3545 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3546 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3547 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3548 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3551 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3553 const wxPoint
&pos
, const wxSize
&size
)
3554 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3559 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3563 // NO - don't do this because it will set both the x and y origin
3564 // coords to match the parent scrolled window and we just want to
3565 // set the x coord - MB
3567 // m_owner->PrepareDC( dc );
3570 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3571 dc
.SetDeviceOrigin( -x
, 0 );
3573 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3574 m_owner
->DrawColLabels( dc
, cols
);
3578 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3580 m_owner
->ProcessColLabelMouseEvent( event
);
3583 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3585 m_owner
->GetEventHandler()->ProcessEvent(event
);
3589 // This seems to be required for wxMotif otherwise the mouse
3590 // cursor must be in the cell edit control to get key events
3592 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3594 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3597 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3599 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3604 //////////////////////////////////////////////////////////////////////
3606 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3608 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3609 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3610 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3611 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3612 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3613 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3616 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3618 const wxPoint
&pos
, const wxSize
&size
)
3619 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3624 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3628 int client_height
= 0;
3629 int client_width
= 0;
3630 GetClientSize( &client_width
, &client_height
);
3632 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3633 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3634 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3635 dc
.DrawLine( 0, 0, client_width
, 0 );
3636 dc
.DrawLine( 0, 0, 0, client_height
);
3638 dc
.SetPen( *wxWHITE_PEN
);
3639 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3640 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3644 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3646 m_owner
->ProcessCornerLabelMouseEvent( event
);
3650 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3652 m_owner
->GetEventHandler()->ProcessEvent(event
);
3655 // This seems to be required for wxMotif otherwise the mouse
3656 // cursor must be in the cell edit control to get key events
3658 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3660 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3663 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3665 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3670 //////////////////////////////////////////////////////////////////////
3672 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3674 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3675 EVT_PAINT( wxGridWindow::OnPaint
)
3676 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3677 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3678 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3679 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3680 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3681 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3682 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3685 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3686 wxGridRowLabelWindow
*rowLblWin
,
3687 wxGridColLabelWindow
*colLblWin
,
3690 const wxSize
&size
)
3691 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3692 wxT("grid window") )
3696 m_rowLabelWin
= rowLblWin
;
3697 m_colLabelWin
= colLblWin
;
3701 wxGridWindow::~wxGridWindow()
3706 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3708 wxPaintDC
dc( this );
3709 m_owner
->PrepareDC( dc
);
3710 wxRegion reg
= GetUpdateRegion();
3711 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3712 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3713 #if WXGRID_DRAW_LINES
3714 m_owner
->DrawAllGridLines( dc
, reg
);
3716 m_owner
->DrawGridSpace( dc
);
3717 m_owner
->DrawHighlight( dc
, DirtyCells
);
3721 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3723 wxWindow::ScrollWindow( dx
, dy
, rect
);
3724 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3725 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3729 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3731 m_owner
->ProcessGridCellMouseEvent( event
);
3734 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3736 m_owner
->GetEventHandler()->ProcessEvent(event
);
3739 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3740 // cursor must be in the cell edit control to get key events
3742 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3744 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3747 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3749 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3752 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3756 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3758 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3762 //////////////////////////////////////////////////////////////////////
3764 // Internal Helper function for computing row or column from some
3765 // (unscrolled) coordinate value, using either
3766 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3767 // of m_rowBottoms/m_ColRights to speed up the search!
3769 // Internal helper macros for simpler use of that function
3771 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3772 const wxArrayInt
& BorderArray
, int nMax
,
3775 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3776 m_minAcceptableColWidth, \
3777 m_colRights, m_numCols, true)
3778 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3779 m_minAcceptableRowHeight, \
3780 m_rowBottoms, m_numRows, true)
3781 /////////////////////////////////////////////////////////////////////
3783 #if wxUSE_EXTENDED_RTTI
3784 WX_DEFINE_FLAGS( wxGridStyle
)
3786 wxBEGIN_FLAGS( wxGridStyle
)
3787 // new style border flags, we put them first to
3788 // use them for streaming out
3789 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3790 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3791 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3792 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3793 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3794 wxFLAGS_MEMBER(wxBORDER_NONE
)
3796 // old style border flags
3797 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3798 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3799 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3800 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3801 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3802 wxFLAGS_MEMBER(wxBORDER
)
3804 // standard window styles
3805 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3806 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3807 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3808 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3809 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3810 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3811 wxFLAGS_MEMBER(wxVSCROLL
)
3812 wxFLAGS_MEMBER(wxHSCROLL
)
3814 wxEND_FLAGS( wxGridStyle
)
3816 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3818 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3819 wxHIDE_PROPERTY( Children
)
3820 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3821 wxEND_PROPERTIES_TABLE()
3823 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3824 wxEND_HANDLERS_TABLE()
3826 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3829 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3832 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3835 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3836 EVT_PAINT( wxGrid::OnPaint
)
3837 EVT_SIZE( wxGrid::OnSize
)
3838 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3839 EVT_KEY_UP( wxGrid::OnKeyUp
)
3840 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3845 // in order to make sure that a size event is not
3846 // trigerred in a unfinished state
3847 m_cornerLabelWin
= NULL
;
3848 m_rowLabelWin
= NULL
;
3849 m_colLabelWin
= NULL
;
3853 wxGrid::wxGrid( wxWindow
*parent
,
3858 const wxString
& name
)
3859 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3860 m_colMinWidths(GRID_HASH_SIZE
),
3861 m_rowMinHeights(GRID_HASH_SIZE
)
3864 SetBestFittingSize(size
);
3867 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3868 const wxPoint
& pos
, const wxSize
& size
,
3869 long style
, const wxString
& name
)
3871 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3872 style
| wxWANTS_CHARS
, name
))
3875 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3876 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3879 SetBestFittingSize(size
);
3887 // Must do this or ~wxScrollHelper will pop the wrong event handler
3888 SetTargetWindow(this);
3890 wxSafeDecRef(m_defaultCellAttr
);
3892 #ifdef DEBUG_ATTR_CACHE
3893 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3894 wxPrintf(_T("wxGrid attribute cache statistics: "
3895 "total: %u, hits: %u (%u%%)\n"),
3896 total
, gs_nAttrCacheHits
,
3897 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3903 delete m_typeRegistry
;
3909 // ----- internal init and update functions
3912 // NOTE: If using the default visual attributes works everywhere then this can
3913 // be removed as well as the #else cases below.
3914 #define _USE_VISATTR 0
3917 #include "wx/listbox.h"
3920 void wxGrid::Create()
3922 m_created
= false; // set to true by CreateGrid
3924 m_table
= (wxGridTableBase
*) NULL
;
3927 m_cellEditCtrlEnabled
= false;
3929 m_defaultCellAttr
= new wxGridCellAttr();
3931 // Set default cell attributes
3932 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3933 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3934 m_defaultCellAttr
->SetFont(GetFont());
3935 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3936 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3937 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3940 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3941 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3943 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3944 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3947 m_defaultCellAttr
->SetTextColour(
3948 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3949 m_defaultCellAttr
->SetBackgroundColour(
3950 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3955 m_currentCellCoords
= wxGridNoCellCoords
;
3957 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3958 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3960 // create the type registry
3961 m_typeRegistry
= new wxGridTypeRegistry
;
3964 // subwindow components that make up the wxGrid
3965 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3970 m_rowLabelWin
= new wxGridRowLabelWindow( this,
3975 m_colLabelWin
= new wxGridColLabelWindow( this,
3980 m_gridWin
= new wxGridWindow( this,
3987 SetTargetWindow( m_gridWin
);
3990 wxColour gfg
= gva
.colFg
;
3991 wxColour gbg
= gva
.colBg
;
3992 wxColour lfg
= lva
.colFg
;
3993 wxColour lbg
= lva
.colBg
;
3995 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3996 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
3997 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
3998 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4000 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4001 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4002 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4003 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4004 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4005 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4007 m_gridWin
->SetOwnForegroundColour(gfg
);
4008 m_gridWin
->SetOwnBackgroundColour(gbg
);
4014 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4015 wxGrid::wxGridSelectionModes selmode
)
4017 wxCHECK_MSG( !m_created
,
4019 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4021 m_numRows
= numRows
;
4022 m_numCols
= numCols
;
4024 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4025 m_table
->SetView( this );
4027 m_selection
= new wxGridSelection( this, selmode
);
4036 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4038 wxCHECK_RET( m_created
,
4039 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4041 m_selection
->SetSelectionMode( selmode
);
4044 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4046 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4047 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4049 return m_selection
->GetSelectionMode();
4052 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4053 wxGrid::wxGridSelectionModes selmode
)
4057 // stop all processing
4062 wxGridTableBase
*t
=m_table
;
4075 m_numRows
= table
->GetNumberRows();
4076 m_numCols
= table
->GetNumberCols();
4079 m_table
->SetView( this );
4082 m_selection
= new wxGridSelection( this, selmode
);
4095 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4096 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4098 if ( m_rowLabelWin
)
4100 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4104 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4107 m_labelTextColour
= wxColour( _T("BLACK") );
4110 m_attrCache
.row
= -1;
4111 m_attrCache
.col
= -1;
4112 m_attrCache
.attr
= NULL
;
4114 // TODO: something better than this ?
4116 m_labelFont
= this->GetFont();
4117 m_labelFont
.SetWeight( wxBOLD
);
4119 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4120 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4122 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4123 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4124 m_colLabelTextOrientation
= wxHORIZONTAL
;
4126 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4127 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4129 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4130 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4132 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4133 m_defaultRowHeight
+= 8;
4135 m_defaultRowHeight
+= 4;
4138 m_gridLineColour
= wxColour( 192,192,192 );
4139 m_gridLinesEnabled
= true;
4140 m_cellHighlightColour
= *wxBLACK
;
4141 m_cellHighlightPenWidth
= 2;
4142 m_cellHighlightROPenWidth
= 1;
4144 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4145 m_winCapture
= (wxWindow
*)NULL
;
4146 m_canDragRowSize
= true;
4147 m_canDragColSize
= true;
4148 m_canDragGridSize
= true;
4149 m_canDragCell
= false;
4151 m_dragRowOrCol
= -1;
4152 m_isDragging
= false;
4153 m_startDragPos
= wxDefaultPosition
;
4155 m_waitForSlowClick
= false;
4157 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4158 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4160 m_currentCellCoords
= wxGridNoCellCoords
;
4162 m_selectingTopLeft
= wxGridNoCellCoords
;
4163 m_selectingBottomRight
= wxGridNoCellCoords
;
4164 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4165 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4167 m_editable
= true; // default for whole grid
4169 m_inOnKeyDown
= false;
4176 // ----------------------------------------------------------------------------
4177 // the idea is to call these functions only when necessary because they create
4178 // quite big arrays which eat memory mostly unnecessary - in particular, if
4179 // default widths/heights are used for all rows/columns, we may not use these
4182 // with some extra code, it should be possible to only store the
4183 // widths/heights different from default ones but this will be done later...
4184 // ----------------------------------------------------------------------------
4186 void wxGrid::InitRowHeights()
4188 m_rowHeights
.Empty();
4189 m_rowBottoms
.Empty();
4191 m_rowHeights
.Alloc( m_numRows
);
4192 m_rowBottoms
.Alloc( m_numRows
);
4196 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4198 for ( int i
= 0; i
< m_numRows
; i
++ )
4200 rowBottom
+= m_defaultRowHeight
;
4201 m_rowBottoms
.Add( rowBottom
);
4205 void wxGrid::InitColWidths()
4207 m_colWidths
.Empty();
4208 m_colRights
.Empty();
4210 m_colWidths
.Alloc( m_numCols
);
4211 m_colRights
.Alloc( m_numCols
);
4214 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4216 for ( int i
= 0; i
< m_numCols
; i
++ )
4218 colRight
+= m_defaultColWidth
;
4219 m_colRights
.Add( colRight
);
4223 int wxGrid::GetColWidth(int col
) const
4225 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4228 int wxGrid::GetColLeft(int col
) const
4230 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4231 : m_colRights
[col
] - m_colWidths
[col
];
4234 int wxGrid::GetColRight(int col
) const
4236 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4240 int wxGrid::GetRowHeight(int row
) const
4242 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4245 int wxGrid::GetRowTop(int row
) const
4247 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4248 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4251 int wxGrid::GetRowBottom(int row
) const
4253 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4254 : m_rowBottoms
[row
];
4257 void wxGrid::CalcDimensions()
4260 GetClientSize( &cw
, &ch
);
4262 if ( m_rowLabelWin
->IsShown() )
4263 cw
-= m_rowLabelWidth
;
4264 if ( m_colLabelWin
->IsShown() )
4265 ch
-= m_colLabelHeight
;
4268 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4269 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4271 // take into account editor if shown
4272 if( IsCellEditControlShown() )
4275 int r
= m_currentCellCoords
.GetRow();
4276 int c
= m_currentCellCoords
.GetCol();
4277 int x
= GetColLeft(c
);
4278 int y
= GetRowTop(r
);
4280 // how big is the editor
4281 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4282 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4283 editor
->GetControl()->GetSize(&w2
, &h2
);
4286 if( w2
> w
) w
= w2
;
4287 if( h2
> h
) h
= h2
;
4292 // preserve (more or less) the previous position
4294 GetViewStart( &x
, &y
);
4296 // ensure the position is valid for the new scroll ranges
4298 x
= wxMax( w
- 1, 0 );
4300 y
= wxMax( h
- 1, 0 );
4302 // do set scrollbar parameters
4303 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4304 GetScrollX(w
), GetScrollY(h
), x
, y
,
4305 GetBatchCount() != 0);
4307 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4308 // still must reposition the children
4313 void wxGrid::CalcWindowSizes()
4315 // escape if the window is has not been fully created yet
4317 if ( m_cornerLabelWin
== NULL
)
4321 GetClientSize( &cw
, &ch
);
4323 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4324 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4326 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4327 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4329 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4330 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4332 if ( m_gridWin
&& m_gridWin
->IsShown() )
4333 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4337 // this is called when the grid table sends a message to say that it
4338 // has been redimensioned
4340 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4343 bool result
= false;
4345 // Clear the attribute cache as the attribute might refer to a different
4346 // cell than stored in the cache after adding/removing rows/columns.
4348 // By the same reasoning, the editor should be dismissed if columns are
4349 // added or removed. And for consistency, it should IMHO always be
4350 // removed, not only if the cell "underneath" it actually changes.
4351 // For now, I intentionally do not save the editor's content as the
4352 // cell it might want to save that stuff to might no longer exist.
4353 HideCellEditControl();
4355 // if we were using the default widths/heights so far, we must change them
4357 if ( m_colWidths
.IsEmpty() )
4362 if ( m_rowHeights
.IsEmpty() )
4368 switch ( msg
.GetId() )
4370 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4372 size_t pos
= msg
.GetCommandInt();
4373 int numRows
= msg
.GetCommandInt2();
4375 m_numRows
+= numRows
;
4377 if ( !m_rowHeights
.IsEmpty() )
4379 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4380 m_rowBottoms
.Insert( 0, pos
, numRows
);
4383 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4385 for ( i
= pos
; i
< m_numRows
; i
++ )
4387 bottom
+= m_rowHeights
[i
];
4388 m_rowBottoms
[i
] = bottom
;
4391 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4393 // if we have just inserted cols into an empty grid the current
4394 // cell will be undefined...
4396 SetCurrentCell( 0, 0 );
4400 m_selection
->UpdateRows( pos
, numRows
);
4401 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4403 attrProvider
->UpdateAttrRows( pos
, numRows
);
4405 if ( !GetBatchCount() )
4408 m_rowLabelWin
->Refresh();
4414 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4416 int numRows
= msg
.GetCommandInt();
4417 int oldNumRows
= m_numRows
;
4418 m_numRows
+= numRows
;
4420 if ( !m_rowHeights
.IsEmpty() )
4422 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4423 m_rowBottoms
.Add( 0, numRows
);
4426 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4428 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4430 bottom
+= m_rowHeights
[i
];
4431 m_rowBottoms
[i
] = bottom
;
4434 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4436 // if we have just inserted cols into an empty grid the current
4437 // cell will be undefined...
4439 SetCurrentCell( 0, 0 );
4441 if ( !GetBatchCount() )
4444 m_rowLabelWin
->Refresh();
4450 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4452 size_t pos
= msg
.GetCommandInt();
4453 int numRows
= msg
.GetCommandInt2();
4454 m_numRows
-= numRows
;
4456 if ( !m_rowHeights
.IsEmpty() )
4458 m_rowHeights
.RemoveAt( pos
, numRows
);
4459 m_rowBottoms
.RemoveAt( pos
, numRows
);
4462 for ( i
= 0; i
< m_numRows
; i
++ )
4464 h
+= m_rowHeights
[i
];
4465 m_rowBottoms
[i
] = h
;
4470 m_currentCellCoords
= wxGridNoCellCoords
;
4474 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4475 m_currentCellCoords
.Set( 0, 0 );
4479 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4480 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4482 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4483 // ifdef'd out following patch from Paul Gammans
4485 // No need to touch column attributes, unless we
4486 // removed _all_ rows, in this case, we remove
4487 // all column attributes.
4488 // I hate to do this here, but the
4489 // needed data is not available inside UpdateAttrRows.
4490 if ( !GetNumberRows() )
4491 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4494 if ( !GetBatchCount() )
4497 m_rowLabelWin
->Refresh();
4503 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4505 size_t pos
= msg
.GetCommandInt();
4506 int numCols
= msg
.GetCommandInt2();
4507 m_numCols
+= numCols
;
4509 if ( !m_colWidths
.IsEmpty() )
4511 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4512 m_colRights
.Insert( 0, pos
, numCols
);
4515 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4517 for ( i
= pos
; i
< m_numCols
; i
++ )
4519 right
+= m_colWidths
[i
];
4520 m_colRights
[i
] = right
;
4523 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4525 // if we have just inserted cols into an empty grid the current
4526 // cell will be undefined...
4528 SetCurrentCell( 0, 0 );
4532 m_selection
->UpdateCols( pos
, numCols
);
4533 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4535 attrProvider
->UpdateAttrCols( pos
, numCols
);
4536 if ( !GetBatchCount() )
4539 m_colLabelWin
->Refresh();
4546 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4548 int numCols
= msg
.GetCommandInt();
4549 int oldNumCols
= m_numCols
;
4550 m_numCols
+= numCols
;
4551 if ( !m_colWidths
.IsEmpty() )
4553 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4554 m_colRights
.Add( 0, numCols
);
4557 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4559 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4561 right
+= m_colWidths
[i
];
4562 m_colRights
[i
] = right
;
4565 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4567 // if we have just inserted cols into an empty grid the current
4568 // cell will be undefined...
4570 SetCurrentCell( 0, 0 );
4572 if ( !GetBatchCount() )
4575 m_colLabelWin
->Refresh();
4581 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4583 size_t pos
= msg
.GetCommandInt();
4584 int numCols
= msg
.GetCommandInt2();
4585 m_numCols
-= numCols
;
4587 if ( !m_colWidths
.IsEmpty() )
4589 m_colWidths
.RemoveAt( pos
, numCols
);
4590 m_colRights
.RemoveAt( pos
, numCols
);
4593 for ( i
= 0; i
< m_numCols
; i
++ )
4595 w
+= m_colWidths
[i
];
4601 m_currentCellCoords
= wxGridNoCellCoords
;
4605 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4606 m_currentCellCoords
.Set( 0, 0 );
4610 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4611 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4613 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4614 // ifdef'd out following patch from Paul Gammans
4616 // No need to touch row attributes, unless we
4617 // removed _all_ columns, in this case, we remove
4618 // all row attributes.
4619 // I hate to do this here, but the
4620 // needed data is not available inside UpdateAttrCols.
4621 if ( !GetNumberCols() )
4622 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4625 if ( !GetBatchCount() )
4628 m_colLabelWin
->Refresh();
4635 if (result
&& !GetBatchCount() )
4636 m_gridWin
->Refresh();
4641 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4643 wxRegionIterator
iter( reg
);
4646 wxArrayInt rowlabels
;
4653 // TODO: remove this when we can...
4654 // There is a bug in wxMotif that gives garbage update
4655 // rectangles if you jump-scroll a long way by clicking the
4656 // scrollbar with middle button. This is a work-around
4658 #if defined(__WXMOTIF__)
4660 m_gridWin
->GetClientSize( &cw
, &ch
);
4661 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4662 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4665 // logical bounds of update region
4668 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4669 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4671 // find the row labels within these bounds
4674 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4676 if ( GetRowBottom(row
) < top
)
4679 if ( GetRowTop(row
) > bottom
)
4682 rowlabels
.Add( row
);
4692 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4694 wxRegionIterator
iter( reg
);
4697 wxArrayInt colLabels
;
4704 // TODO: remove this when we can...
4705 // There is a bug in wxMotif that gives garbage update
4706 // rectangles if you jump-scroll a long way by clicking the
4707 // scrollbar with middle button. This is a work-around
4709 #if defined(__WXMOTIF__)
4711 m_gridWin
->GetClientSize( &cw
, &ch
);
4712 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4713 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4716 // logical bounds of update region
4719 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4720 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4722 // find the cells within these bounds
4725 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4727 if ( GetColRight(col
) < left
)
4730 if ( GetColLeft(col
) > right
)
4733 colLabels
.Add( col
);
4742 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4744 wxRegionIterator
iter( reg
);
4747 wxGridCellCoordsArray cellsExposed
;
4749 int left
, top
, right
, bottom
;
4754 // TODO: remove this when we can...
4755 // There is a bug in wxMotif that gives garbage update
4756 // rectangles if you jump-scroll a long way by clicking the
4757 // scrollbar with middle button. This is a work-around
4759 #if defined(__WXMOTIF__)
4761 m_gridWin
->GetClientSize( &cw
, &ch
);
4762 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4763 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4764 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4765 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4768 // logical bounds of update region
4770 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4771 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4773 // find the cells within these bounds
4776 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4778 if ( GetRowBottom(row
) <= top
)
4781 if ( GetRowTop(row
) > bottom
)
4784 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4786 if ( GetColRight(col
) <= left
)
4789 if ( GetColLeft(col
) > right
)
4792 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4799 return cellsExposed
;
4803 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4806 wxPoint
pos( event
.GetPosition() );
4807 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4809 if ( event
.Dragging() )
4813 m_isDragging
= true;
4814 m_rowLabelWin
->CaptureMouse();
4817 if ( event
.LeftIsDown() )
4819 switch( m_cursorMode
)
4821 case WXGRID_CURSOR_RESIZE_ROW
:
4823 int cw
, ch
, left
, dummy
;
4824 m_gridWin
->GetClientSize( &cw
, &ch
);
4825 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4827 wxClientDC
dc( m_gridWin
);
4830 GetRowTop(m_dragRowOrCol
) +
4831 GetRowMinimalHeight(m_dragRowOrCol
) );
4832 dc
.SetLogicalFunction(wxINVERT
);
4833 if ( m_dragLastPos
>= 0 )
4835 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4837 dc
.DrawLine( left
, y
, left
+cw
, y
);
4842 case WXGRID_CURSOR_SELECT_ROW
:
4843 if ( (row
= YToRow( y
)) >= 0 )
4847 m_selection
->SelectRow( row
,
4848 event
.ControlDown(),
4855 // default label to suppress warnings about "enumeration value
4856 // 'xxx' not handled in switch
4864 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4869 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4870 m_isDragging
= false;
4873 // ------------ Entering or leaving the window
4875 if ( event
.Entering() || event
.Leaving() )
4877 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4881 // ------------ Left button pressed
4883 else if ( event
.LeftDown() )
4885 // don't send a label click event for a hit on the
4886 // edge of the row label - this is probably the user
4887 // wanting to resize the row
4889 if ( YToEdgeOfRow(y
) < 0 )
4893 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4895 if ( !event
.ShiftDown() && !event
.ControlDown() )
4899 if ( event
.ShiftDown() )
4901 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4904 GetNumberCols() - 1,
4905 event
.ControlDown(),
4912 m_selection
->SelectRow( row
,
4913 event
.ControlDown(),
4920 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4925 // starting to drag-resize a row
4927 if ( CanDragRowSize() )
4928 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4933 // ------------ Left double click
4935 else if (event
.LeftDClick() )
4937 int row
= YToEdgeOfRow(y
);
4942 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4944 // no default action at the moment
4949 // adjust row height depending on label text
4950 AutoSizeRowLabelSize( row
);
4952 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4958 // ------------ Left button released
4960 else if ( event
.LeftUp() )
4962 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4964 DoEndDragResizeRow();
4966 // Note: we are ending the event *after* doing
4967 // default processing in this case
4969 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
4972 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4977 // ------------ Right button down
4979 else if ( event
.RightDown() )
4983 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
4985 // no default action at the moment
4990 // ------------ Right double click
4992 else if ( event
.RightDClick() )
4996 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
4998 // no default action at the moment
5003 // ------------ No buttons down and mouse moving
5005 else if ( event
.Moving() )
5007 m_dragRowOrCol
= YToEdgeOfRow( y
);
5008 if ( m_dragRowOrCol
>= 0 )
5010 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5012 // don't capture the mouse yet
5013 if ( CanDragRowSize() )
5014 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5017 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5019 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5025 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5028 wxPoint
pos( event
.GetPosition() );
5029 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5031 if ( event
.Dragging() )
5035 m_isDragging
= true;
5036 m_colLabelWin
->CaptureMouse();
5039 if ( event
.LeftIsDown() )
5041 switch( m_cursorMode
)
5043 case WXGRID_CURSOR_RESIZE_COL
:
5045 int cw
, ch
, dummy
, top
;
5046 m_gridWin
->GetClientSize( &cw
, &ch
);
5047 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5049 wxClientDC
dc( m_gridWin
);
5052 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5053 GetColMinimalWidth(m_dragRowOrCol
));
5054 dc
.SetLogicalFunction(wxINVERT
);
5055 if ( m_dragLastPos
>= 0 )
5057 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5059 dc
.DrawLine( x
, top
, x
, top
+ch
);
5064 case WXGRID_CURSOR_SELECT_COL
:
5065 if ( (col
= XToCol( x
)) >= 0 )
5069 m_selection
->SelectCol( col
,
5070 event
.ControlDown(),
5077 // default label to suppress warnings about "enumeration value
5078 // 'xxx' not handled in switch
5086 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5091 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5092 m_isDragging
= false;
5095 // ------------ Entering or leaving the window
5097 if ( event
.Entering() || event
.Leaving() )
5099 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5103 // ------------ Left button pressed
5105 else if ( event
.LeftDown() )
5107 // don't send a label click event for a hit on the
5108 // edge of the col label - this is probably the user
5109 // wanting to resize the col
5111 if ( XToEdgeOfCol(x
) < 0 )
5115 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5117 if ( !event
.ShiftDown() && !event
.ControlDown() )
5121 if ( event
.ShiftDown() )
5123 m_selection
->SelectBlock( 0,
5124 m_currentCellCoords
.GetCol(),
5125 GetNumberRows() - 1, col
,
5126 event
.ControlDown(),
5133 m_selection
->SelectCol( col
,
5134 event
.ControlDown(),
5141 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5146 // starting to drag-resize a col
5148 if ( CanDragColSize() )
5149 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5154 // ------------ Left double click
5156 if ( event
.LeftDClick() )
5158 int col
= XToEdgeOfCol(x
);
5163 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5165 // no default action at the moment
5170 // adjust column width depending on label text
5171 AutoSizeColLabelSize( col
);
5173 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5179 // ------------ Left button released
5181 else if ( event
.LeftUp() )
5183 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5185 DoEndDragResizeCol();
5187 // Note: we are ending the event *after* doing
5188 // default processing in this case
5190 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5193 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5198 // ------------ Right button down
5200 else if ( event
.RightDown() )
5204 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5206 // no default action at the moment
5211 // ------------ Right double click
5213 else if ( event
.RightDClick() )
5217 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5219 // no default action at the moment
5224 // ------------ No buttons down and mouse moving
5226 else if ( event
.Moving() )
5228 m_dragRowOrCol
= XToEdgeOfCol( x
);
5229 if ( m_dragRowOrCol
>= 0 )
5231 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5233 // don't capture the cursor yet
5234 if ( CanDragColSize() )
5235 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5238 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5240 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5246 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5248 if ( event
.LeftDown() )
5250 // indicate corner label by having both row and
5253 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5259 else if ( event
.LeftDClick() )
5261 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5264 else if ( event
.RightDown() )
5266 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5268 // no default action at the moment
5272 else if ( event
.RightDClick() )
5274 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5276 // no default action at the moment
5281 void wxGrid::ChangeCursorMode(CursorMode mode
,
5286 static const wxChar
*cursorModes
[] =
5295 wxLogTrace(_T("grid"),
5296 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5297 win
== m_colLabelWin
? _T("colLabelWin")
5298 : win
? _T("rowLabelWin")
5300 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5301 #endif // __WXDEBUG__
5303 if ( mode
== m_cursorMode
&&
5304 win
== m_winCapture
&&
5305 captureMouse
== (m_winCapture
!= NULL
))
5310 // by default use the grid itself
5316 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5317 m_winCapture
= (wxWindow
*)NULL
;
5320 m_cursorMode
= mode
;
5322 switch ( m_cursorMode
)
5324 case WXGRID_CURSOR_RESIZE_ROW
:
5325 win
->SetCursor( m_rowResizeCursor
);
5328 case WXGRID_CURSOR_RESIZE_COL
:
5329 win
->SetCursor( m_colResizeCursor
);
5333 win
->SetCursor( *wxSTANDARD_CURSOR
);
5336 // we need to capture mouse when resizing
5337 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5338 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5340 if ( captureMouse
&& resize
)
5342 win
->CaptureMouse();
5347 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5350 wxPoint
pos( event
.GetPosition() );
5351 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5353 wxGridCellCoords coords
;
5354 XYToCell( x
, y
, coords
);
5356 int cell_rows
, cell_cols
;
5357 bool isFirstDrag
= !m_isDragging
;
5358 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5359 if ((cell_rows
< 0) || (cell_cols
< 0))
5361 coords
.SetRow(coords
.GetRow() + cell_rows
);
5362 coords
.SetCol(coords
.GetCol() + cell_cols
);
5365 if ( event
.Dragging() )
5367 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5369 // Don't start doing anything until the mouse has been drug at
5370 // least 3 pixels in any direction...
5373 if (m_startDragPos
== wxDefaultPosition
)
5375 m_startDragPos
= pos
;
5378 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5382 m_isDragging
= true;
5383 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5385 // Hide the edit control, so it
5386 // won't interfer with drag-shrinking.
5387 if ( IsCellEditControlShown() )
5389 HideCellEditControl();
5390 SaveEditControlValue();
5393 // Have we captured the mouse yet?
5396 m_winCapture
= m_gridWin
;
5397 m_winCapture
->CaptureMouse();
5400 if ( coords
!= wxGridNoCellCoords
)
5402 if ( event
.ControlDown() )
5404 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5405 m_selectingKeyboard
= coords
;
5406 HighlightBlock ( m_selectingKeyboard
, coords
);
5408 else if ( CanDragCell() )
5412 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5413 m_selectingKeyboard
= coords
;
5415 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5423 if ( !IsSelection() )
5425 HighlightBlock( coords
, coords
);
5429 HighlightBlock( m_currentCellCoords
, coords
);
5433 if (! IsVisible(coords
))
5435 MakeCellVisible(coords
);
5436 // TODO: need to introduce a delay or something here. The
5437 // scrolling is way to fast, at least on MSW - also on GTK.
5441 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5443 int cw
, ch
, left
, dummy
;
5444 m_gridWin
->GetClientSize( &cw
, &ch
);
5445 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5447 wxClientDC
dc( m_gridWin
);
5449 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5450 GetRowMinimalHeight(m_dragRowOrCol
) );
5451 dc
.SetLogicalFunction(wxINVERT
);
5452 if ( m_dragLastPos
>= 0 )
5454 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5456 dc
.DrawLine( left
, y
, left
+cw
, y
);
5459 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5461 int cw
, ch
, dummy
, top
;
5462 m_gridWin
->GetClientSize( &cw
, &ch
);
5463 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5465 wxClientDC
dc( m_gridWin
);
5467 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5468 GetColMinimalWidth(m_dragRowOrCol
) );
5469 dc
.SetLogicalFunction(wxINVERT
);
5470 if ( m_dragLastPos
>= 0 )
5472 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5474 dc
.DrawLine( x
, top
, x
, top
+ch
);
5481 m_isDragging
= false;
5482 m_startDragPos
= wxDefaultPosition
;
5484 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5485 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5488 if ( event
.Entering() || event
.Leaving() )
5490 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5491 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5496 // ------------ Left button pressed
5498 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5500 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5505 if ( !event
.ControlDown() )
5507 if ( event
.ShiftDown() )
5511 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5512 m_currentCellCoords
.GetCol(),
5515 event
.ControlDown(),
5521 else if ( XToEdgeOfCol(x
) < 0 &&
5522 YToEdgeOfRow(y
) < 0 )
5524 DisableCellEditControl();
5525 MakeCellVisible( coords
);
5527 if ( event
.ControlDown() )
5531 m_selection
->ToggleCellSelection( coords
.GetRow(),
5533 event
.ControlDown(),
5538 m_selectingTopLeft
= wxGridNoCellCoords
;
5539 m_selectingBottomRight
= wxGridNoCellCoords
;
5540 m_selectingKeyboard
= coords
;
5544 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5545 SetCurrentCell( coords
);
5548 if ( m_selection
->GetSelectionMode() !=
5549 wxGrid::wxGridSelectCells
)
5551 HighlightBlock( coords
, coords
);
5560 // ------------ Left double click
5562 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5564 DisableCellEditControl();
5566 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5568 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5573 // we want double click to select a cell and start editing
5574 // (i.e. to behave in same way as sequence of two slow clicks):
5575 m_waitForSlowClick
= true;
5582 // ------------ Left button released
5584 else if ( event
.LeftUp() )
5586 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5590 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5591 m_winCapture
= NULL
;
5594 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5597 EnableCellEditControl();
5599 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5600 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5601 editor
->StartingClick();
5605 m_waitForSlowClick
= false;
5607 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5608 m_selectingBottomRight
!= wxGridNoCellCoords
)
5612 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5613 m_selectingTopLeft
.GetCol(),
5614 m_selectingBottomRight
.GetRow(),
5615 m_selectingBottomRight
.GetCol(),
5616 event
.ControlDown(),
5622 m_selectingTopLeft
= wxGridNoCellCoords
;
5623 m_selectingBottomRight
= wxGridNoCellCoords
;
5625 // Show the edit control, if it has been hidden for
5627 ShowCellEditControl();
5630 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5632 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5633 DoEndDragResizeRow();
5635 // Note: we are ending the event *after* doing
5636 // default processing in this case
5638 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5640 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5642 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5643 DoEndDragResizeCol();
5645 // Note: we are ending the event *after* doing
5646 // default processing in this case
5648 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5655 // ------------ Right button down
5657 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5659 DisableCellEditControl();
5660 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5665 // no default action at the moment
5670 // ------------ Right double click
5672 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5674 DisableCellEditControl();
5675 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5680 // no default action at the moment
5684 // ------------ Moving and no button action
5686 else if ( event
.Moving() && !event
.IsButton() )
5688 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5690 // out of grid cell area
5691 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5695 int dragRow
= YToEdgeOfRow( y
);
5696 int dragCol
= XToEdgeOfCol( x
);
5698 // Dragging on the corner of a cell to resize in both
5699 // directions is not implemented yet...
5701 if ( dragRow
>= 0 && dragCol
>= 0 )
5703 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5709 m_dragRowOrCol
= dragRow
;
5711 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5713 if ( CanDragRowSize() && CanDragGridSize() )
5714 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5719 m_dragRowOrCol
= dragCol
;
5727 m_dragRowOrCol
= dragCol
;
5729 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5731 if ( CanDragColSize() && CanDragGridSize() )
5732 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5738 // Neither on a row or col edge
5740 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5742 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5748 void wxGrid::DoEndDragResizeRow()
5750 if ( m_dragLastPos
>= 0 )
5752 // erase the last line and resize the row
5754 int cw
, ch
, left
, dummy
;
5755 m_gridWin
->GetClientSize( &cw
, &ch
);
5756 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5758 wxClientDC
dc( m_gridWin
);
5760 dc
.SetLogicalFunction( wxINVERT
);
5761 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5762 HideCellEditControl();
5763 SaveEditControlValue();
5765 int rowTop
= GetRowTop(m_dragRowOrCol
);
5766 SetRowSize( m_dragRowOrCol
,
5767 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5769 if ( !GetBatchCount() )
5771 // Only needed to get the correct rect.y:
5772 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5774 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5775 rect
.width
= m_rowLabelWidth
;
5776 rect
.height
= ch
- rect
.y
;
5777 m_rowLabelWin
->Refresh( true, &rect
);
5779 // if there is a multicell block, paint all of it
5782 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5783 int leftCol
= XToCol(left
);
5784 int rightCol
= internalXToCol(left
+cw
);
5787 for (i
=leftCol
; i
<rightCol
; i
++)
5789 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5790 if (cell_rows
< subtract_rows
)
5791 subtract_rows
= cell_rows
;
5793 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5794 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5795 rect
.height
= ch
- rect
.y
;
5798 m_gridWin
->Refresh( false, &rect
);
5801 ShowCellEditControl();
5806 void wxGrid::DoEndDragResizeCol()
5808 if ( m_dragLastPos
>= 0 )
5810 // erase the last line and resize the col
5812 int cw
, ch
, dummy
, top
;
5813 m_gridWin
->GetClientSize( &cw
, &ch
);
5814 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5816 wxClientDC
dc( m_gridWin
);
5818 dc
.SetLogicalFunction( wxINVERT
);
5819 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5820 HideCellEditControl();
5821 SaveEditControlValue();
5823 int colLeft
= GetColLeft(m_dragRowOrCol
);
5824 SetColSize( m_dragRowOrCol
,
5825 wxMax( m_dragLastPos
- colLeft
,
5826 GetColMinimalWidth(m_dragRowOrCol
) ) );
5828 if ( !GetBatchCount() )
5830 // Only needed to get the correct rect.x:
5831 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5833 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5834 rect
.width
= cw
- rect
.x
;
5835 rect
.height
= m_colLabelHeight
;
5836 m_colLabelWin
->Refresh( true, &rect
);
5838 // if there is a multicell block, paint all of it
5841 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5842 int topRow
= YToRow(top
);
5843 int bottomRow
= internalYToRow(top
+cw
);
5846 for (i
=topRow
; i
<bottomRow
; i
++)
5848 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5849 if (cell_cols
< subtract_cols
)
5850 subtract_cols
= cell_cols
;
5852 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5853 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5854 rect
.width
= cw
- rect
.x
;
5857 m_gridWin
->Refresh( false, &rect
);
5860 ShowCellEditControl();
5867 // ------ interaction with data model
5869 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5871 switch ( msg
.GetId() )
5873 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5874 return GetModelValues();
5876 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5877 return SetModelValues();
5879 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5880 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5881 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5882 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5883 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5884 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5885 return Redimension( msg
);
5894 // The behaviour of this function depends on the grid table class
5895 // Clear() function. For the default wxGridStringTable class the
5896 // behavious is to replace all cell contents with wxEmptyString but
5897 // not to change the number of rows or cols.
5899 void wxGrid::ClearGrid()
5903 if (IsCellEditControlEnabled())
5904 DisableCellEditControl();
5907 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5912 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5914 // TODO: something with updateLabels flag
5918 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5924 if (IsCellEditControlEnabled())
5925 DisableCellEditControl();
5927 bool done
= m_table
->InsertRows( pos
, numRows
);
5930 // the table will have sent the results of the insert row
5931 // operation to this view object as a grid table message
5937 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5939 // TODO: something with updateLabels flag
5943 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5949 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5951 // the table will have sent the results of the append row
5952 // operation to this view object as a grid table message
5958 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5960 // TODO: something with updateLabels flag
5964 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5970 if (IsCellEditControlEnabled())
5971 DisableCellEditControl();
5973 bool done
= m_table
->DeleteRows( pos
, numRows
);
5975 // the table will have sent the results of the delete row
5976 // operation to this view object as a grid table message
5982 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
5984 // TODO: something with updateLabels flag
5988 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
5994 if (IsCellEditControlEnabled())
5995 DisableCellEditControl();
5997 bool done
= m_table
->InsertCols( pos
, numCols
);
5999 // the table will have sent the results of the insert col
6000 // operation to this view object as a grid table message
6006 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6008 // TODO: something with updateLabels flag
6012 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6018 bool done
= m_table
->AppendCols( numCols
);
6020 // the table will have sent the results of the append col
6021 // operation to this view object as a grid table message
6027 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6029 // TODO: something with updateLabels flag
6033 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6039 if (IsCellEditControlEnabled())
6040 DisableCellEditControl();
6042 bool done
= m_table
->DeleteCols( pos
, numCols
);
6044 // the table will have sent the results of the delete col
6045 // operation to this view object as a grid table message
6053 // ----- event handlers
6056 // Generate a grid event based on a mouse event and
6057 // return the result of ProcessEvent()
6059 int wxGrid::SendEvent( const wxEventType type
,
6061 wxMouseEvent
& mouseEv
)
6066 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6068 int rowOrCol
= (row
== -1 ? col
: row
);
6070 wxGridSizeEvent
gridEvt( GetId(),
6074 mouseEv
.GetX() + GetRowLabelSize(),
6075 mouseEv
.GetY() + GetColLabelSize(),
6076 mouseEv
.ControlDown(),
6077 mouseEv
.ShiftDown(),
6079 mouseEv
.MetaDown() );
6081 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6082 vetoed
= !gridEvt
.IsAllowed();
6084 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6086 // Right now, it should _never_ end up here!
6087 wxGridRangeSelectEvent
gridEvt( GetId(),
6091 m_selectingBottomRight
,
6093 mouseEv
.ControlDown(),
6094 mouseEv
.ShiftDown(),
6096 mouseEv
.MetaDown() );
6098 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6099 vetoed
= !gridEvt
.IsAllowed();
6103 wxGridEvent
gridEvt( GetId(),
6107 mouseEv
.GetX() + GetRowLabelSize(),
6108 mouseEv
.GetY() + GetColLabelSize(),
6110 mouseEv
.ControlDown(),
6111 mouseEv
.ShiftDown(),
6113 mouseEv
.MetaDown() );
6114 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6115 vetoed
= !gridEvt
.IsAllowed();
6118 // A Veto'd event may not be `claimed' so test this first
6119 if (vetoed
) return -1;
6120 return claimed
? 1 : 0;
6124 // Generate a grid event of specified type and return the result
6125 // of ProcessEvent().
6127 int wxGrid::SendEvent( const wxEventType type
,
6133 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6135 int rowOrCol
= (row
== -1 ? col
: row
);
6137 wxGridSizeEvent
gridEvt( GetId(),
6142 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6143 vetoed
= !gridEvt
.IsAllowed();
6147 wxGridEvent
gridEvt( GetId(),
6152 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6153 vetoed
= !gridEvt
.IsAllowed();
6156 // A Veto'd event may not be `claimed' so test this first
6157 if (vetoed
) return -1;
6158 return claimed
? 1 : 0;
6162 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6164 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6167 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6169 // Don't do anything if between Begin/EndBatch...
6170 // EndBatch() will do all this on the last nested one anyway.
6171 if (! GetBatchCount())
6173 // Refresh to get correct scrolled position:
6174 wxScrolledWindow::Refresh(eraseb
,rect
);
6178 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6179 int width_label
, width_cell
, height_label
, height_cell
;
6182 //Copy rectangle can get scroll offsets..
6183 rect_x
= rect
->GetX();
6184 rect_y
= rect
->GetY();
6185 rectWidth
= rect
->GetWidth();
6186 rectHeight
= rect
->GetHeight();
6188 width_label
= m_rowLabelWidth
- rect_x
;
6189 if (width_label
> rectWidth
) width_label
= rectWidth
;
6191 height_label
= m_colLabelHeight
- rect_y
;
6192 if (height_label
> rectHeight
) height_label
= rectHeight
;
6194 if (rect_x
> m_rowLabelWidth
)
6196 x
= rect_x
- m_rowLabelWidth
;
6197 width_cell
= rectWidth
;
6202 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6205 if (rect_y
> m_colLabelHeight
)
6207 y
= rect_y
- m_colLabelHeight
;
6208 height_cell
= rectHeight
;
6213 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6216 // Paint corner label part intersecting rect.
6217 if ( width_label
> 0 && height_label
> 0 )
6219 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6220 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6223 // Paint col labels part intersecting rect.
6224 if ( width_cell
> 0 && height_label
> 0 )
6226 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6227 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6230 // Paint row labels part intersecting rect.
6231 if ( width_label
> 0 && height_cell
> 0 )
6233 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6234 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6237 // Paint cell area part intersecting rect.
6238 if ( width_cell
> 0 && height_cell
> 0 )
6240 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6241 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6246 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6247 m_colLabelWin
->Refresh(eraseb
, NULL
);
6248 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6249 m_gridWin
->Refresh(eraseb
, NULL
);
6254 void wxGrid::OnSize( wxSizeEvent
& event
)
6256 // position the child windows
6259 // don't call CalcDimensions() from here, the base class handles the size
6265 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6267 if ( m_inOnKeyDown
)
6269 // shouldn't be here - we are going round in circles...
6271 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6274 m_inOnKeyDown
= true;
6276 // propagate the event up and see if it gets processed
6278 wxWindow
*parent
= GetParent();
6279 wxKeyEvent
keyEvt( event
);
6280 keyEvt
.SetEventObject( parent
);
6282 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6285 // try local handlers
6287 switch ( event
.GetKeyCode() )
6290 if ( event
.ControlDown() )
6292 MoveCursorUpBlock( event
.ShiftDown() );
6296 MoveCursorUp( event
.ShiftDown() );
6301 if ( event
.ControlDown() )
6303 MoveCursorDownBlock( event
.ShiftDown() );
6307 MoveCursorDown( event
.ShiftDown() );
6312 if ( event
.ControlDown() )
6314 MoveCursorLeftBlock( event
.ShiftDown() );
6318 MoveCursorLeft( event
.ShiftDown() );
6323 if ( event
.ControlDown() )
6325 MoveCursorRightBlock( event
.ShiftDown() );
6329 MoveCursorRight( event
.ShiftDown() );
6334 case WXK_NUMPAD_ENTER
:
6335 if ( event
.ControlDown() )
6337 event
.Skip(); // to let the edit control have the return
6341 if ( GetGridCursorRow() < GetNumberRows()-1 )
6343 MoveCursorDown( event
.ShiftDown() );
6347 // at the bottom of a column
6348 DisableCellEditControl();
6358 if (event
.ShiftDown())
6360 if ( GetGridCursorCol() > 0 )
6362 MoveCursorLeft( false );
6367 DisableCellEditControl();
6372 if ( GetGridCursorCol() < GetNumberCols()-1 )
6374 MoveCursorRight( false );
6379 DisableCellEditControl();
6385 if ( event
.ControlDown() )
6387 MakeCellVisible( 0, 0 );
6388 SetCurrentCell( 0, 0 );
6397 if ( event
.ControlDown() )
6399 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6400 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6417 if ( event
.ControlDown() )
6421 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6422 m_currentCellCoords
.GetCol(),
6423 event
.ControlDown(),
6430 if ( !IsEditable() )
6432 MoveCursorRight( false );
6435 // Otherwise fall through to default
6438 // is it possible to edit the current cell at all?
6439 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6441 // yes, now check whether the cells editor accepts the key
6442 int row
= m_currentCellCoords
.GetRow();
6443 int col
= m_currentCellCoords
.GetCol();
6444 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6445 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6447 // <F2> is special and will always start editing, for
6448 // other keys - ask the editor itself
6449 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6450 || editor
->IsAcceptedKey(event
) )
6452 // ensure cell is visble
6453 MakeCellVisible(row
, col
);
6454 EnableCellEditControl();
6456 // a problem can arise if the cell is not completely
6457 // visible (even after calling MakeCellVisible the
6458 // control is not created and calling StartingKey will
6460 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6472 // let others process char events with modifiers or all
6473 // char events for readonly cells
6480 m_inOnKeyDown
= false;
6483 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6485 // try local handlers
6487 if ( event
.GetKeyCode() == WXK_SHIFT
)
6489 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6490 m_selectingBottomRight
!= wxGridNoCellCoords
)
6494 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6495 m_selectingTopLeft
.GetCol(),
6496 m_selectingBottomRight
.GetRow(),
6497 m_selectingBottomRight
.GetCol(),
6498 event
.ControlDown(),
6505 m_selectingTopLeft
= wxGridNoCellCoords
;
6506 m_selectingBottomRight
= wxGridNoCellCoords
;
6507 m_selectingKeyboard
= wxGridNoCellCoords
;
6511 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6515 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6517 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6519 // the event has been intercepted - do nothing
6523 wxClientDC
dc(m_gridWin
);
6526 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6528 DisableCellEditControl();
6530 if ( IsVisible( m_currentCellCoords
, false ) )
6533 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6534 if ( !m_gridLinesEnabled
)
6542 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6544 // Otherwise refresh redraws the highlight!
6545 m_currentCellCoords
= coords
;
6547 DrawGridCellArea(dc
,cells
);
6548 DrawAllGridLines( dc
, r
);
6552 m_currentCellCoords
= coords
;
6554 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6555 DrawCellHighlight(dc
, attr
);
6560 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6563 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6567 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6570 rightCol
= GetNumberCols() - 1;
6572 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6575 bottomRow
= GetNumberRows() - 1;
6579 if ( topRow
> bottomRow
)
6586 if ( leftCol
> rightCol
)
6593 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6594 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6596 // First the case that we selected a completely new area
6597 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6598 m_selectingBottomRight
== wxGridNoCellCoords
)
6601 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6602 wxGridCellCoords ( bottomRow
, rightCol
) );
6603 m_gridWin
->Refresh( false, &rect
);
6605 // Now handle changing an existing selection area.
6606 else if ( m_selectingTopLeft
!= updateTopLeft
||
6607 m_selectingBottomRight
!= updateBottomRight
)
6609 // Compute two optimal update rectangles:
6610 // Either one rectangle is a real subset of the
6611 // other, or they are (almost) disjoint!
6613 bool need_refresh
[4];
6617 need_refresh
[3] = false;
6620 // Store intermediate values
6621 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6622 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6623 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6624 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6626 // Determine the outer/inner coordinates.
6627 if (oldLeft
> leftCol
)
6633 if (oldTop
> topRow
)
6639 if (oldRight
< rightCol
)
6642 oldRight
= rightCol
;
6645 if (oldBottom
< bottomRow
)
6648 oldBottom
= bottomRow
;
6652 // Now, either the stuff marked old is the outer
6653 // rectangle or we don't have a situation where one
6654 // is contained in the other.
6656 if ( oldLeft
< leftCol
)
6658 // Refresh the newly selected or deselected
6659 // area to the left of the old or new selection.
6660 need_refresh
[0] = true;
6661 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6663 wxGridCellCoords ( oldBottom
,
6667 if ( oldTop
< topRow
)
6669 // Refresh the newly selected or deselected
6670 // area above the old or new selection.
6671 need_refresh
[1] = true;
6672 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6674 wxGridCellCoords ( topRow
- 1,
6678 if ( oldRight
> rightCol
)
6680 // Refresh the newly selected or deselected
6681 // area to the right of the old or new selection.
6682 need_refresh
[2] = true;
6683 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6685 wxGridCellCoords ( oldBottom
,
6689 if ( oldBottom
> bottomRow
)
6691 // Refresh the newly selected or deselected
6692 // area below the old or new selection.
6693 need_refresh
[3] = true;
6694 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6696 wxGridCellCoords ( oldBottom
,
6700 // various Refresh() calls
6701 for (i
= 0; i
< 4; i
++ )
6702 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6703 m_gridWin
->Refresh( false, &(rect
[i
]) );
6706 m_selectingTopLeft
= updateTopLeft
;
6707 m_selectingBottomRight
= updateBottomRight
;
6711 // ------ functions to get/send data (see also public functions)
6714 bool wxGrid::GetModelValues()
6716 // Hide the editor, so it won't hide a changed value.
6717 HideCellEditControl();
6721 // all we need to do is repaint the grid
6723 m_gridWin
->Refresh();
6731 bool wxGrid::SetModelValues()
6735 // Disable the editor, so it won't hide a changed value.
6736 // Do we also want to save the current value of the editor first?
6738 DisableCellEditControl();
6742 for ( row
= 0; row
< m_numRows
; row
++ )
6744 for ( col
= 0; col
< m_numCols
; col
++ )
6746 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6758 // Note - this function only draws cells that are in the list of
6759 // exposed cells (usually set from the update region by
6760 // CalcExposedCells)
6762 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6764 if ( !m_numRows
|| !m_numCols
) return;
6766 int i
, numCells
= cells
.GetCount();
6767 int row
, col
, cell_rows
, cell_cols
;
6768 wxGridCellCoordsArray redrawCells
;
6770 for ( i
= numCells
-1; i
>= 0; i
-- )
6772 row
= cells
[i
].GetRow();
6773 col
= cells
[i
].GetCol();
6774 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6776 // If this cell is part of a multicell block, find owner for repaint
6777 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6779 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6780 bool marked
= false;
6781 for ( int j
= 0; j
< numCells
; j
++ )
6783 if ( cell
== cells
[j
] )
6791 int count
= redrawCells
.GetCount();
6792 for (int j
= 0; j
< count
; j
++)
6794 if ( cell
== redrawCells
[j
] )
6800 if (!marked
) redrawCells
.Add( cell
);
6802 continue; // don't bother drawing this cell
6805 // If this cell is empty, find cell to left that might want to overflow
6806 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6808 for ( int l
= 0; l
< cell_rows
; l
++ )
6810 // find a cell in this row to left alreay marked for repaint
6812 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6813 if ((redrawCells
[k
].GetCol() < left
) &&
6814 (redrawCells
[k
].GetRow() == row
))
6815 left
=redrawCells
[k
].GetCol();
6817 if (left
== col
) left
= 0; // oh well
6819 for (int j
= col
-1; j
>= left
; j
--)
6821 if (!m_table
->IsEmptyCell(row
+l
, j
))
6823 if (GetCellOverflow(row
+l
, j
))
6825 wxGridCellCoords
cell(row
+l
, j
);
6826 bool marked
= false;
6828 for (int k
= 0; k
< numCells
; k
++)
6830 if ( cell
== cells
[k
] )
6838 int count
= redrawCells
.GetCount();
6839 for (int k
= 0; k
< count
; k
++)
6841 if ( cell
== redrawCells
[k
] )
6847 if (!marked
) redrawCells
.Add( cell
);
6855 DrawCell( dc
, cells
[i
] );
6858 numCells
= redrawCells
.GetCount();
6860 for ( i
= numCells
- 1; i
>= 0; i
-- )
6862 DrawCell( dc
, redrawCells
[i
] );
6867 void wxGrid::DrawGridSpace( wxDC
& dc
)
6870 m_gridWin
->GetClientSize( &cw
, &ch
);
6873 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6875 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6876 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6878 if ( right
> rightCol
|| bottom
> bottomRow
)
6881 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6883 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6884 dc
.SetPen( *wxTRANSPARENT_PEN
);
6886 if ( right
> rightCol
)
6888 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6891 if ( bottom
> bottomRow
)
6893 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6899 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6901 int row
= coords
.GetRow();
6902 int col
= coords
.GetCol();
6904 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6907 // we draw the cell border ourselves
6908 #if !WXGRID_DRAW_LINES
6909 if ( m_gridLinesEnabled
)
6910 DrawCellBorder( dc
, coords
);
6913 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6915 bool isCurrent
= coords
== m_currentCellCoords
;
6917 wxRect rect
= CellToRect( row
, col
);
6919 // if the editor is shown, we should use it and not the renderer
6920 // Note: However, only if it is really _shown_, i.e. not hidden!
6921 if ( isCurrent
&& IsCellEditControlShown() )
6923 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6924 editor
->PaintBackground(rect
, attr
);
6929 // but all the rest is drawn by the cell renderer and hence may be
6931 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6932 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6939 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6941 int row
= m_currentCellCoords
.GetRow();
6942 int col
= m_currentCellCoords
.GetCol();
6944 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6947 wxRect rect
= CellToRect(row
, col
);
6949 // hmmm... what could we do here to show that the cell is disabled?
6950 // for now, I just draw a thinner border than for the other ones, but
6951 // it doesn't look really good
6953 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6957 // The center of th drawn line is where the position/width/height of
6958 // the rectangle is actually at, (on wxMSW atr least,) so we will
6959 // reduce the size of the rectangle to compensate for the thickness of
6960 // the line. If this is too strange on non wxMSW platforms then
6961 // please #ifdef this appropriately.
6962 rect
.x
+= penWidth
/2;
6963 rect
.y
+= penWidth
/2;
6964 rect
.width
-= penWidth
-1;
6965 rect
.height
-= penWidth
-1;
6968 // Now draw the rectangle
6969 // use the cellHighlightColour if the cell is inside a selection, this
6970 // will ensure the cell is always visible.
6971 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
6972 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
6973 dc
.DrawRectangle(rect
);
6977 // VZ: my experiments with 3d borders...
6979 // how to properly set colours for arbitrary bg?
6980 wxCoord x1
= rect
.x
,
6982 x2
= rect
.x
+ rect
.width
-1,
6983 y2
= rect
.y
+ rect
.height
-1;
6985 dc
.SetPen(*wxWHITE_PEN
);
6986 dc
.DrawLine(x1
, y1
, x2
, y1
);
6987 dc
.DrawLine(x1
, y1
, x1
, y2
);
6989 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
6990 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
6992 dc
.SetPen(*wxBLACK_PEN
);
6993 dc
.DrawLine(x1
, y2
, x2
, y2
);
6994 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
6999 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7001 int row
= coords
.GetRow();
7002 int col
= coords
.GetCol();
7003 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7006 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7008 wxRect rect
= CellToRect( row
, col
);
7010 // right hand border
7012 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7013 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7017 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7018 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7021 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7023 // This if block was previously in wxGrid::OnPaint but that doesn't
7024 // seem to get called under wxGTK - MB
7026 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7027 m_numRows
&& m_numCols
)
7029 m_currentCellCoords
.Set(0, 0);
7032 if ( IsCellEditControlShown() )
7034 // don't show highlight when the edit control is shown
7038 // if the active cell was repainted, repaint its highlight too because it
7039 // might have been damaged by the grid lines
7040 size_t count
= cells
.GetCount();
7041 for ( size_t n
= 0; n
< count
; n
++ )
7043 if ( cells
[n
] == m_currentCellCoords
)
7045 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7046 DrawCellHighlight(dc
, attr
);
7054 // TODO: remove this ???
7055 // This is used to redraw all grid lines e.g. when the grid line colour
7058 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7060 #if !WXGRID_DRAW_LINES
7064 if ( !m_gridLinesEnabled
||
7066 !m_numCols
) return;
7068 int top
, bottom
, left
, right
;
7070 #if 0 //#ifndef __WXGTK__
7074 m_gridWin
->GetClientSize(&cw
, &ch
);
7076 // virtual coords of visible area
7078 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7079 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7084 reg
.GetBox(x
, y
, w
, h
);
7085 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7086 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7090 m_gridWin
->GetClientSize(&cw
, &ch
);
7091 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7092 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7095 // avoid drawing grid lines past the last row and col
7097 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7098 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7100 // no gridlines inside multicells, clip them out
7101 int leftCol
= internalXToCol(left
);
7102 int topRow
= internalYToRow(top
);
7103 int rightCol
= internalXToCol(right
);
7104 int bottomRow
= internalYToRow(bottom
);
7105 wxRegion
clippedcells(0, 0, cw
, ch
);
7108 int i
, j
, cell_rows
, cell_cols
;
7111 for (j
=topRow
; j
<bottomRow
; j
++)
7113 for (i
=leftCol
; i
<rightCol
; i
++)
7115 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7116 if ((cell_rows
> 1) || (cell_cols
> 1))
7118 rect
= CellToRect(j
,i
);
7119 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7120 clippedcells
.Subtract(rect
);
7122 else if ((cell_rows
< 0) || (cell_cols
< 0))
7124 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7125 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7126 clippedcells
.Subtract(rect
);
7130 dc
.SetClippingRegion( clippedcells
);
7132 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7134 // horizontal grid lines
7136 // already declared above - int i;
7137 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7139 int bot
= GetRowBottom(i
) - 1;
7148 dc
.DrawLine( left
, bot
, right
, bot
);
7153 // vertical grid lines
7155 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7157 int colRight
= GetColRight(i
) - 1;
7158 if ( colRight
> right
)
7163 if ( colRight
>= left
)
7165 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7168 dc
.DestroyClippingRegion();
7172 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7174 if ( !m_numRows
) return;
7177 size_t numLabels
= rows
.GetCount();
7179 for ( i
= 0; i
< numLabels
; i
++ )
7181 DrawRowLabel( dc
, rows
[i
] );
7186 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7188 if ( GetRowHeight(row
) <= 0 )
7191 int rowTop
= GetRowTop(row
),
7192 rowBottom
= GetRowBottom(row
) - 1;
7194 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7195 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7196 m_rowLabelWidth
-1, rowBottom
);
7198 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7200 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7202 dc
.SetPen( *wxWHITE_PEN
);
7203 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7204 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7206 dc
.SetBackgroundMode( wxTRANSPARENT
);
7207 dc
.SetTextForeground( GetLabelTextColour() );
7208 dc
.SetFont( GetLabelFont() );
7211 GetRowLabelAlignment( &hAlign
, &vAlign
);
7215 rect
.SetY( GetRowTop(row
) + 2 );
7216 rect
.SetWidth( m_rowLabelWidth
- 4 );
7217 rect
.SetHeight( GetRowHeight(row
) - 4 );
7218 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7222 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7224 if ( !m_numCols
) return;
7227 size_t numLabels
= cols
.GetCount();
7229 for ( i
= 0; i
< numLabels
; i
++ )
7231 DrawColLabel( dc
, cols
[i
] );
7236 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7238 if ( GetColWidth(col
) <= 0 )
7241 int colLeft
= GetColLeft(col
),
7242 colRight
= GetColRight(col
) - 1;
7244 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7245 dc
.DrawLine( colRight
, 0,
7246 colRight
, m_colLabelHeight
-1 );
7248 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7250 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7251 colRight
+1, m_colLabelHeight
-1 );
7253 dc
.SetPen( *wxWHITE_PEN
);
7254 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7255 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7257 dc
.SetBackgroundMode( wxTRANSPARENT
);
7258 dc
.SetTextForeground( GetLabelTextColour() );
7259 dc
.SetFont( GetLabelFont() );
7261 int hAlign
, vAlign
, orient
;
7262 GetColLabelAlignment( &hAlign
, &vAlign
);
7263 orient
= GetColLabelTextOrientation();
7266 rect
.SetX( colLeft
+ 2 );
7268 rect
.SetWidth( GetColWidth(col
) - 4 );
7269 rect
.SetHeight( m_colLabelHeight
- 4 );
7270 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7273 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7274 const wxString
& value
,
7278 int textOrientation
)
7280 wxArrayString lines
;
7282 StringToLines( value
, lines
);
7285 //Forward to new API.
7286 DrawTextRectangle( dc
,
7295 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7296 const wxArrayString
& lines
,
7300 int textOrientation
)
7302 long textWidth
, textHeight
;
7303 long lineWidth
, lineHeight
;
7306 dc
.SetClippingRegion( rect
);
7308 nLines
= lines
.GetCount();
7312 float x
= 0.0, y
= 0.0;
7314 if( textOrientation
== wxHORIZONTAL
)
7315 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7317 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7321 case wxALIGN_BOTTOM
:
7322 if( textOrientation
== wxHORIZONTAL
)
7323 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7325 x
= rect
.x
+ rect
.width
- textWidth
;
7328 case wxALIGN_CENTRE
:
7329 if( textOrientation
== wxHORIZONTAL
)
7330 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7332 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7337 if( textOrientation
== wxHORIZONTAL
)
7344 // Align each line of a multi-line label
7345 for( l
= 0; l
< nLines
; l
++ )
7347 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7349 switch( horizAlign
)
7352 if( textOrientation
== wxHORIZONTAL
)
7353 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7355 y
= rect
.y
+ lineWidth
+ 1;
7358 case wxALIGN_CENTRE
:
7359 if( textOrientation
== wxHORIZONTAL
)
7360 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7362 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7367 if( textOrientation
== wxHORIZONTAL
)
7370 y
= rect
.y
+ rect
.height
- 1;
7374 if( textOrientation
== wxHORIZONTAL
)
7376 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7381 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7386 dc
.DestroyClippingRegion();
7390 // Split multi line text up into an array of strings. Any existing
7391 // contents of the string array are preserved.
7393 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7397 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7398 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7400 while ( startPos
< (int)tVal
.Length() )
7402 pos
= tVal
.Mid(startPos
).Find( eol
);
7407 else if ( pos
== 0 )
7409 lines
.Add( wxEmptyString
);
7413 lines
.Add( value
.Mid(startPos
, pos
) );
7417 if ( startPos
< (int)value
.Length() )
7419 lines
.Add( value
.Mid( startPos
) );
7424 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7425 const wxArrayString
& lines
,
7426 long *width
, long *height
)
7433 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7435 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7436 w
= wxMax( w
, lineW
);
7445 // ------ Batch processing.
7447 void wxGrid::EndBatch()
7449 if ( m_batchCount
> 0 )
7452 if ( !m_batchCount
)
7455 m_rowLabelWin
->Refresh();
7456 m_colLabelWin
->Refresh();
7457 m_cornerLabelWin
->Refresh();
7458 m_gridWin
->Refresh();
7463 // Use this, rather than wxWindow::Refresh(), to force an immediate
7464 // repainting of the grid. Has no effect if you are already inside a
7465 // BeginBatch / EndBatch block.
7467 void wxGrid::ForceRefresh()
7475 // ------ Edit control functions
7479 void wxGrid::EnableEditing( bool edit
)
7481 // TODO: improve this ?
7483 if ( edit
!= m_editable
)
7485 if(!edit
) EnableCellEditControl(edit
);
7491 void wxGrid::EnableCellEditControl( bool enable
)
7496 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7497 SetCurrentCell( 0, 0 );
7499 if ( enable
!= m_cellEditCtrlEnabled
)
7503 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7506 // this should be checked by the caller!
7507 wxASSERT_MSG( CanEnableCellControl(),
7508 _T("can't enable editing for this cell!") );
7510 // do it before ShowCellEditControl()
7511 m_cellEditCtrlEnabled
= enable
;
7513 ShowCellEditControl();
7517 //FIXME:add veto support
7518 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7520 HideCellEditControl();
7521 SaveEditControlValue();
7523 // do it after HideCellEditControl()
7524 m_cellEditCtrlEnabled
= enable
;
7529 bool wxGrid::IsCurrentCellReadOnly() const
7532 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7533 bool readonly
= attr
->IsReadOnly();
7539 bool wxGrid::CanEnableCellControl() const
7541 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7542 !IsCurrentCellReadOnly();
7546 bool wxGrid::IsCellEditControlEnabled() const
7548 // the cell edit control might be disable for all cells or just for the
7549 // current one if it's read only
7550 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7553 bool wxGrid::IsCellEditControlShown() const
7555 bool isShown
= false;
7557 if ( m_cellEditCtrlEnabled
)
7559 int row
= m_currentCellCoords
.GetRow();
7560 int col
= m_currentCellCoords
.GetCol();
7561 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7562 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7567 if ( editor
->IsCreated() )
7569 isShown
= editor
->GetControl()->IsShown();
7579 void wxGrid::ShowCellEditControl()
7581 if ( IsCellEditControlEnabled() )
7583 if ( !IsVisible( m_currentCellCoords
) )
7585 m_cellEditCtrlEnabled
= false;
7590 wxRect rect
= CellToRect( m_currentCellCoords
);
7591 int row
= m_currentCellCoords
.GetRow();
7592 int col
= m_currentCellCoords
.GetCol();
7594 // if this is part of a multicell, find owner (topleft)
7595 int cell_rows
, cell_cols
;
7596 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7597 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7601 m_currentCellCoords
.SetRow( row
);
7602 m_currentCellCoords
.SetCol( col
);
7605 // convert to scrolled coords
7607 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7609 // done in PaintBackground()
7611 // erase the highlight and the cell contents because the editor
7612 // might not cover the entire cell
7613 wxClientDC
dc( m_gridWin
);
7615 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7616 dc
.SetPen(*wxTRANSPARENT_PEN
);
7617 dc
.DrawRectangle(rect
);
7620 // cell is shifted by one pixel
7621 // However, don't allow x or y to become negative
7622 // since the SetSize() method interprets that as
7629 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7630 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7631 if ( !editor
->IsCreated() )
7633 editor
->Create(m_gridWin
, wxID_ANY
,
7634 new wxGridCellEditorEvtHandler(this, editor
));
7636 wxGridEditorCreatedEvent
evt(GetId(),
7637 wxEVT_GRID_EDITOR_CREATED
,
7641 editor
->GetControl());
7642 GetEventHandler()->ProcessEvent(evt
);
7646 // resize editor to overflow into righthand cells if allowed
7647 int maxWidth
= rect
.width
;
7648 wxString value
= GetCellValue(row
, col
);
7649 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7652 GetTextExtent(value
, &maxWidth
, &y
,
7653 NULL
, NULL
, &attr
->GetFont());
7654 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7656 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7657 if (rect
.x
+maxWidth
> client_right
)
7658 maxWidth
= client_right
- rect
.x
;
7660 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7662 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7663 // may have changed earlier
7664 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7667 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7668 // looks weird going over a multicell
7669 if (m_table
->IsEmptyCell(row
,i
) &&
7670 (rect
.width
< maxWidth
) && (c_rows
== 1))
7671 rect
.width
+= GetColWidth(i
);
7675 if (rect
.GetRight() > client_right
)
7676 rect
.SetRight(client_right
-1);
7679 editor
->SetCellAttr(attr
);
7680 editor
->SetSize( rect
);
7681 editor
->Show( true, attr
);
7683 // recalc dimensions in case we need to
7684 // expand the scrolled window to account for editor
7687 editor
->BeginEdit(row
, col
, this);
7688 editor
->SetCellAttr(NULL
);
7697 void wxGrid::HideCellEditControl()
7699 if ( IsCellEditControlEnabled() )
7701 int row
= m_currentCellCoords
.GetRow();
7702 int col
= m_currentCellCoords
.GetCol();
7704 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7705 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7706 editor
->Show( false );
7709 m_gridWin
->SetFocus();
7710 // refresh whole row to the right
7711 wxRect
rect( CellToRect(row
, col
) );
7712 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7713 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7714 m_gridWin
->Refresh( false, &rect
);
7719 void wxGrid::SaveEditControlValue()
7721 if ( IsCellEditControlEnabled() )
7723 int row
= m_currentCellCoords
.GetRow();
7724 int col
= m_currentCellCoords
.GetCol();
7726 wxString oldval
= GetCellValue(row
,col
);
7728 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7729 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7730 bool changed
= editor
->EndEdit(row
, col
, this);
7737 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7738 m_currentCellCoords
.GetRow(),
7739 m_currentCellCoords
.GetCol() ) < 0 ) {
7741 // Event has been vetoed, set the data back.
7742 SetCellValue(row
,col
,oldval
);
7750 // ------ Grid location functions
7751 // Note that all of these functions work with the logical coordinates of
7752 // grid cells and labels so you will need to convert from device
7753 // coordinates for mouse events etc.
7756 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7758 int row
= YToRow(y
);
7759 int col
= XToCol(x
);
7761 if ( row
== -1 || col
== -1 )
7763 coords
= wxGridNoCellCoords
;
7767 coords
.Set( row
, col
);
7772 // Internal Helper function for computing row or column from some
7773 // (unscrolled) coordinate value, using either
7774 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7775 // of m_rowBottoms/m_ColRights to speed up the search!
7777 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7778 const wxArrayInt
& BorderArray
, int nMax
,
7783 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7789 size_t i_max
= coord
/ defaultDist
,
7792 if (BorderArray
.IsEmpty())
7794 if((int) i_max
< nMax
)
7796 return clipToMinMax
? nMax
- 1 : -1;
7799 if ( i_max
>= BorderArray
.GetCount())
7800 i_max
= BorderArray
.GetCount() - 1;
7803 if ( coord
>= BorderArray
[i_max
])
7807 i_max
= coord
/ minDist
;
7809 i_max
= BorderArray
.GetCount() - 1;
7811 if ( i_max
>= BorderArray
.GetCount())
7812 i_max
= BorderArray
.GetCount() - 1;
7814 if ( coord
>= BorderArray
[i_max
])
7815 return clipToMinMax
? (int)i_max
: -1;
7816 if ( coord
< BorderArray
[0] )
7819 while ( i_max
- i_min
> 0 )
7821 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7822 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7823 if (coord
>= BorderArray
[ i_max
- 1])
7827 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7828 if (coord
< BorderArray
[median
])
7836 int wxGrid::YToRow( int y
)
7838 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7839 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7843 int wxGrid::XToCol( int x
)
7845 return CoordToRowOrCol(x
, m_defaultColWidth
,
7846 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7850 // return the row number that that the y coord is near the edge of, or
7851 // -1 if not near an edge
7853 int wxGrid::YToEdgeOfRow( int y
)
7856 i
= internalYToRow(y
);
7858 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7860 // We know that we are in row i, test whether we are
7861 // close enough to lower or upper border, respectively.
7862 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7864 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7872 // return the col number that that the x coord is near the edge of, or
7873 // -1 if not near an edge
7875 int wxGrid::XToEdgeOfCol( int x
)
7878 i
= internalXToCol(x
);
7880 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7882 // We know that we are in column i, test whether we are
7883 // close enough to right or left border, respectively.
7884 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7886 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7894 wxRect
wxGrid::CellToRect( int row
, int col
)
7896 wxRect
rect( -1, -1, -1, -1 );
7898 if ( row
>= 0 && row
< m_numRows
&&
7899 col
>= 0 && col
< m_numCols
)
7901 int i
, cell_rows
, cell_cols
;
7902 rect
.width
= rect
.height
= 0;
7903 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7904 // if negative then find multicell owner
7905 if (cell_rows
< 0) row
+= cell_rows
;
7906 if (cell_cols
< 0) col
+= cell_cols
;
7907 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7909 rect
.x
= GetColLeft(col
);
7910 rect
.y
= GetRowTop(row
);
7911 for (i
=col
; i
<col
+cell_cols
; i
++)
7912 rect
.width
+= GetColWidth(i
);
7913 for (i
=row
; i
<row
+cell_rows
; i
++)
7914 rect
.height
+= GetRowHeight(i
);
7917 // if grid lines are enabled, then the area of the cell is a bit smaller
7918 if (m_gridLinesEnabled
) {
7926 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7928 // get the cell rectangle in logical coords
7930 wxRect
r( CellToRect( row
, col
) );
7932 // convert to device coords
7934 int left
, top
, right
, bottom
;
7935 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7936 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7938 // check against the client area of the grid window
7941 m_gridWin
->GetClientSize( &cw
, &ch
);
7943 if ( wholeCellVisible
)
7945 // is the cell wholly visible ?
7947 return ( left
>= 0 && right
<= cw
&&
7948 top
>= 0 && bottom
<= ch
);
7952 // is the cell partly visible ?
7954 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
7955 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
7960 // make the specified cell location visible by doing a minimal amount
7963 void wxGrid::MakeCellVisible( int row
, int col
)
7967 int xpos
= -1, ypos
= -1;
7969 if ( row
>= 0 && row
< m_numRows
&&
7970 col
>= 0 && col
< m_numCols
)
7972 // get the cell rectangle in logical coords
7974 wxRect
r( CellToRect( row
, col
) );
7976 // convert to device coords
7978 int left
, top
, right
, bottom
;
7979 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7980 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7983 m_gridWin
->GetClientSize( &cw
, &ch
);
7989 else if ( bottom
> ch
)
7991 int h
= r
.GetHeight();
7993 for ( i
= row
-1; i
>= 0; i
-- )
7995 int rowHeight
= GetRowHeight(i
);
7996 if ( h
+ rowHeight
> ch
)
8003 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8004 // have rounding errors (this is important, because if we do, we
8005 // might not scroll at all and some cells won't be redrawn)
8007 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8009 ypos
+= GRID_SCROLL_LINE_Y
;
8016 else if ( right
> cw
)
8018 // position the view so that the cell is on the right
8020 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8021 xpos
= x0
+ (right
- cw
);
8023 // see comment for ypos above
8024 xpos
+= GRID_SCROLL_LINE_X
;
8027 if ( xpos
!= -1 || ypos
!= -1 )
8030 xpos
/= GRID_SCROLL_LINE_X
;
8032 ypos
/= GRID_SCROLL_LINE_Y
;
8033 Scroll( xpos
, ypos
);
8041 // ------ Grid cursor movement functions
8044 bool wxGrid::MoveCursorUp( bool expandSelection
)
8046 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8047 m_currentCellCoords
.GetRow() >= 0 )
8049 if ( expandSelection
)
8051 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8052 m_selectingKeyboard
= m_currentCellCoords
;
8053 if ( m_selectingKeyboard
.GetRow() > 0 )
8055 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8056 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8057 m_selectingKeyboard
.GetCol() );
8058 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8061 else if ( m_currentCellCoords
.GetRow() > 0 )
8064 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8065 m_currentCellCoords
.GetCol() );
8066 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8067 m_currentCellCoords
.GetCol() );
8078 bool wxGrid::MoveCursorDown( bool expandSelection
)
8080 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8081 m_currentCellCoords
.GetRow() < m_numRows
)
8083 if ( expandSelection
)
8085 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8086 m_selectingKeyboard
= m_currentCellCoords
;
8087 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8089 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8090 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8091 m_selectingKeyboard
.GetCol() );
8092 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8095 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8098 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8099 m_currentCellCoords
.GetCol() );
8100 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8101 m_currentCellCoords
.GetCol() );
8112 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8114 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8115 m_currentCellCoords
.GetCol() >= 0 )
8117 if ( expandSelection
)
8119 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8120 m_selectingKeyboard
= m_currentCellCoords
;
8121 if ( m_selectingKeyboard
.GetCol() > 0 )
8123 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8124 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8125 m_selectingKeyboard
.GetCol() );
8126 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8129 else if ( m_currentCellCoords
.GetCol() > 0 )
8132 MakeCellVisible( m_currentCellCoords
.GetRow(),
8133 m_currentCellCoords
.GetCol() - 1 );
8134 SetCurrentCell( m_currentCellCoords
.GetRow(),
8135 m_currentCellCoords
.GetCol() - 1 );
8146 bool wxGrid::MoveCursorRight( bool expandSelection
)
8148 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8149 m_currentCellCoords
.GetCol() < m_numCols
)
8151 if ( expandSelection
)
8153 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8154 m_selectingKeyboard
= m_currentCellCoords
;
8155 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8157 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8158 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8159 m_selectingKeyboard
.GetCol() );
8160 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8163 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8166 MakeCellVisible( m_currentCellCoords
.GetRow(),
8167 m_currentCellCoords
.GetCol() + 1 );
8168 SetCurrentCell( m_currentCellCoords
.GetRow(),
8169 m_currentCellCoords
.GetCol() + 1 );
8180 bool wxGrid::MovePageUp()
8182 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8184 int row
= m_currentCellCoords
.GetRow();
8188 m_gridWin
->GetClientSize( &cw
, &ch
);
8190 int y
= GetRowTop(row
);
8191 int newRow
= internalYToRow( y
- ch
+ 1 );
8193 if ( newRow
== row
)
8195 //row > 0 , so newrow can never be less than 0 here.
8199 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8200 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8208 bool wxGrid::MovePageDown()
8210 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8212 int row
= m_currentCellCoords
.GetRow();
8213 if ( (row
+1) < m_numRows
)
8216 m_gridWin
->GetClientSize( &cw
, &ch
);
8218 int y
= GetRowTop(row
);
8219 int newRow
= internalYToRow( y
+ ch
);
8220 if ( newRow
== row
)
8222 // row < m_numRows , so newrow can't overflow here.
8226 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8227 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8235 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8238 m_currentCellCoords
!= wxGridNoCellCoords
&&
8239 m_currentCellCoords
.GetRow() > 0 )
8241 int row
= m_currentCellCoords
.GetRow();
8242 int col
= m_currentCellCoords
.GetCol();
8244 if ( m_table
->IsEmptyCell(row
, col
) )
8246 // starting in an empty cell: find the next block of
8252 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8255 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8257 // starting at the top of a block: find the next block
8263 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8268 // starting within a block: find the top of the block
8273 if ( m_table
->IsEmptyCell(row
, col
) )
8281 MakeCellVisible( row
, col
);
8282 if ( expandSelection
)
8284 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8285 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8290 SetCurrentCell( row
, col
);
8298 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8301 m_currentCellCoords
!= wxGridNoCellCoords
&&
8302 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8304 int row
= m_currentCellCoords
.GetRow();
8305 int col
= m_currentCellCoords
.GetCol();
8307 if ( m_table
->IsEmptyCell(row
, col
) )
8309 // starting in an empty cell: find the next block of
8312 while ( row
< m_numRows
-1 )
8315 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8318 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8320 // starting at the bottom of a block: find the next block
8323 while ( row
< m_numRows
-1 )
8326 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8331 // starting within a block: find the bottom of the block
8333 while ( row
< m_numRows
-1 )
8336 if ( m_table
->IsEmptyCell(row
, col
) )
8344 MakeCellVisible( row
, col
);
8345 if ( expandSelection
)
8347 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8348 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8353 SetCurrentCell( row
, col
);
8362 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8365 m_currentCellCoords
!= wxGridNoCellCoords
&&
8366 m_currentCellCoords
.GetCol() > 0 )
8368 int row
= m_currentCellCoords
.GetRow();
8369 int col
= m_currentCellCoords
.GetCol();
8371 if ( m_table
->IsEmptyCell(row
, col
) )
8373 // starting in an empty cell: find the next block of
8379 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8382 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8384 // starting at the left of a block: find the next block
8390 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8395 // starting within a block: find the left of the block
8400 if ( m_table
->IsEmptyCell(row
, col
) )
8408 MakeCellVisible( row
, col
);
8409 if ( expandSelection
)
8411 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8412 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8417 SetCurrentCell( row
, col
);
8426 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8429 m_currentCellCoords
!= wxGridNoCellCoords
&&
8430 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8432 int row
= m_currentCellCoords
.GetRow();
8433 int col
= m_currentCellCoords
.GetCol();
8435 if ( m_table
->IsEmptyCell(row
, col
) )
8437 // starting in an empty cell: find the next block of
8440 while ( col
< m_numCols
-1 )
8443 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8446 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8448 // starting at the right of a block: find the next block
8451 while ( col
< m_numCols
-1 )
8454 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8459 // starting within a block: find the right of the block
8461 while ( col
< m_numCols
-1 )
8464 if ( m_table
->IsEmptyCell(row
, col
) )
8472 MakeCellVisible( row
, col
);
8473 if ( expandSelection
)
8475 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8476 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8481 SetCurrentCell( row
, col
);
8493 // ------ Label values and formatting
8496 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8498 *horiz
= m_rowLabelHorizAlign
;
8499 *vert
= m_rowLabelVertAlign
;
8502 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8504 *horiz
= m_colLabelHorizAlign
;
8505 *vert
= m_colLabelVertAlign
;
8508 int wxGrid::GetColLabelTextOrientation()
8510 return m_colLabelTextOrientation
;
8513 wxString
wxGrid::GetRowLabelValue( int row
)
8517 return m_table
->GetRowLabelValue( row
);
8527 wxString
wxGrid::GetColLabelValue( int col
)
8531 return m_table
->GetColLabelValue( col
);
8542 void wxGrid::SetRowLabelSize( int width
)
8544 width
= wxMax( width
, 0 );
8545 if ( width
!= m_rowLabelWidth
)
8549 m_rowLabelWin
->Show( false );
8550 m_cornerLabelWin
->Show( false );
8552 else if ( m_rowLabelWidth
== 0 )
8554 m_rowLabelWin
->Show( true );
8555 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8558 m_rowLabelWidth
= width
;
8560 wxScrolledWindow::Refresh( true );
8565 void wxGrid::SetColLabelSize( int height
)
8567 height
= wxMax( height
, 0 );
8568 if ( height
!= m_colLabelHeight
)
8572 m_colLabelWin
->Show( false );
8573 m_cornerLabelWin
->Show( false );
8575 else if ( m_colLabelHeight
== 0 )
8577 m_colLabelWin
->Show( true );
8578 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8581 m_colLabelHeight
= height
;
8583 wxScrolledWindow::Refresh( true );
8588 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8590 if ( m_labelBackgroundColour
!= colour
)
8592 m_labelBackgroundColour
= colour
;
8593 m_rowLabelWin
->SetBackgroundColour( colour
);
8594 m_colLabelWin
->SetBackgroundColour( colour
);
8595 m_cornerLabelWin
->SetBackgroundColour( colour
);
8597 if ( !GetBatchCount() )
8599 m_rowLabelWin
->Refresh();
8600 m_colLabelWin
->Refresh();
8601 m_cornerLabelWin
->Refresh();
8606 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8608 if ( m_labelTextColour
!= colour
)
8610 m_labelTextColour
= colour
;
8611 if ( !GetBatchCount() )
8613 m_rowLabelWin
->Refresh();
8614 m_colLabelWin
->Refresh();
8619 void wxGrid::SetLabelFont( const wxFont
& font
)
8622 if ( !GetBatchCount() )
8624 m_rowLabelWin
->Refresh();
8625 m_colLabelWin
->Refresh();
8629 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8631 // allow old (incorrect) defs to be used
8634 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8635 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8636 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8641 case wxTOP
: vert
= wxALIGN_TOP
; break;
8642 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8643 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8646 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8648 m_rowLabelHorizAlign
= horiz
;
8651 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8653 m_rowLabelVertAlign
= vert
;
8656 if ( !GetBatchCount() )
8658 m_rowLabelWin
->Refresh();
8662 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8664 // allow old (incorrect) defs to be used
8667 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8668 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8669 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8674 case wxTOP
: vert
= wxALIGN_TOP
; break;
8675 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8676 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8679 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8681 m_colLabelHorizAlign
= horiz
;
8684 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8686 m_colLabelVertAlign
= vert
;
8689 if ( !GetBatchCount() )
8691 m_colLabelWin
->Refresh();
8695 // Note: under MSW, the default column label font must be changed because it
8696 // does not support vertical printing
8698 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8699 // pGrid->SetLabelFont(font);
8700 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8702 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8704 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8706 m_colLabelTextOrientation
= textOrientation
;
8709 if ( !GetBatchCount() )
8711 m_colLabelWin
->Refresh();
8715 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8719 m_table
->SetRowLabelValue( row
, s
);
8720 if ( !GetBatchCount() )
8722 wxRect rect
= CellToRect( row
, 0);
8723 if ( rect
.height
> 0 )
8725 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8727 rect
.width
= m_rowLabelWidth
;
8728 m_rowLabelWin
->Refresh( true, &rect
);
8734 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8738 m_table
->SetColLabelValue( col
, s
);
8739 if ( !GetBatchCount() )
8741 wxRect rect
= CellToRect( 0, col
);
8742 if ( rect
.width
> 0 )
8744 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8746 rect
.height
= m_colLabelHeight
;
8747 m_colLabelWin
->Refresh( true, &rect
);
8753 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8755 if ( m_gridLineColour
!= colour
)
8757 m_gridLineColour
= colour
;
8759 wxClientDC
dc( m_gridWin
);
8761 DrawAllGridLines( dc
, wxRegion() );
8766 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8768 if ( m_cellHighlightColour
!= colour
)
8770 m_cellHighlightColour
= colour
;
8772 wxClientDC
dc( m_gridWin
);
8774 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8775 DrawCellHighlight(dc
, attr
);
8780 void wxGrid::SetCellHighlightPenWidth(int width
)
8782 if (m_cellHighlightPenWidth
!= width
) {
8783 m_cellHighlightPenWidth
= width
;
8785 // Just redrawing the cell highlight is not enough since that won't
8786 // make any visible change if the the thickness is getting smaller.
8787 int row
= m_currentCellCoords
.GetRow();
8788 int col
= m_currentCellCoords
.GetCol();
8789 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8791 wxRect rect
= CellToRect(row
, col
);
8792 m_gridWin
->Refresh(true, &rect
);
8796 void wxGrid::SetCellHighlightROPenWidth(int width
)
8798 if (m_cellHighlightROPenWidth
!= width
) {
8799 m_cellHighlightROPenWidth
= width
;
8801 // Just redrawing the cell highlight is not enough since that won't
8802 // make any visible change if the the thickness is getting smaller.
8803 int row
= m_currentCellCoords
.GetRow();
8804 int col
= m_currentCellCoords
.GetCol();
8805 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8807 wxRect rect
= CellToRect(row
, col
);
8808 m_gridWin
->Refresh(true, &rect
);
8812 void wxGrid::EnableGridLines( bool enable
)
8814 if ( enable
!= m_gridLinesEnabled
)
8816 m_gridLinesEnabled
= enable
;
8818 if ( !GetBatchCount() )
8822 wxClientDC
dc( m_gridWin
);
8824 DrawAllGridLines( dc
, wxRegion() );
8828 m_gridWin
->Refresh();
8835 int wxGrid::GetDefaultRowSize()
8837 return m_defaultRowHeight
;
8840 int wxGrid::GetRowSize( int row
)
8842 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8844 return GetRowHeight(row
);
8847 int wxGrid::GetDefaultColSize()
8849 return m_defaultColWidth
;
8852 int wxGrid::GetColSize( int col
)
8854 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8856 return GetColWidth(col
);
8859 // ============================================================================
8860 // access to the grid attributes: each of them has a default value in the grid
8861 // itself and may be overidden on a per-cell basis
8862 // ============================================================================
8864 // ----------------------------------------------------------------------------
8865 // setting default attributes
8866 // ----------------------------------------------------------------------------
8868 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8870 m_defaultCellAttr
->SetBackgroundColour(col
);
8872 m_gridWin
->SetBackgroundColour(col
);
8876 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8878 m_defaultCellAttr
->SetTextColour(col
);
8881 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8883 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8886 void wxGrid::SetDefaultCellOverflow( bool allow
)
8888 m_defaultCellAttr
->SetOverflow(allow
);
8891 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8893 m_defaultCellAttr
->SetFont(font
);
8897 // For editors and renderers the type registry takes precedence over the
8898 // default attr, so we need to register the new editor/renderer for the string
8899 // data type in order to make setting a default editor/renderer appear to
8902 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8904 RegisterDataType(wxGRID_VALUE_STRING
,
8906 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8909 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8911 RegisterDataType(wxGRID_VALUE_STRING
,
8912 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8916 // ----------------------------------------------------------------------------
8917 // access to the default attrbiutes
8918 // ----------------------------------------------------------------------------
8920 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8922 return m_defaultCellAttr
->GetBackgroundColour();
8925 wxColour
wxGrid::GetDefaultCellTextColour()
8927 return m_defaultCellAttr
->GetTextColour();
8930 wxFont
wxGrid::GetDefaultCellFont()
8932 return m_defaultCellAttr
->GetFont();
8935 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8937 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8940 bool wxGrid::GetDefaultCellOverflow()
8942 return m_defaultCellAttr
->GetOverflow();
8945 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8947 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
8950 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
8952 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
8955 // ----------------------------------------------------------------------------
8956 // access to cell attributes
8957 // ----------------------------------------------------------------------------
8959 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
8961 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8962 wxColour colour
= attr
->GetBackgroundColour();
8967 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
8969 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8970 wxColour colour
= attr
->GetTextColour();
8975 wxFont
wxGrid::GetCellFont( int row
, int col
)
8977 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8978 wxFont font
= attr
->GetFont();
8983 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
8985 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8986 attr
->GetAlignment(horiz
, vert
);
8990 bool wxGrid::GetCellOverflow( int row
, int col
)
8992 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8993 bool allow
= attr
->GetOverflow();
8998 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9000 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9001 attr
->GetSize( num_rows
, num_cols
);
9005 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9007 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9008 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9014 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9016 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9017 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9023 bool wxGrid::IsReadOnly(int row
, int col
) const
9025 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9026 bool isReadOnly
= attr
->IsReadOnly();
9031 // ----------------------------------------------------------------------------
9032 // attribute support: cache, automatic provider creation, ...
9033 // ----------------------------------------------------------------------------
9035 bool wxGrid::CanHaveAttributes()
9042 return m_table
->CanHaveAttributes();
9045 void wxGrid::ClearAttrCache()
9047 if ( m_attrCache
.row
!= -1 )
9049 wxSafeDecRef(m_attrCache
.attr
);
9050 m_attrCache
.attr
= NULL
;
9051 m_attrCache
.row
= -1;
9055 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9059 wxGrid
*self
= (wxGrid
*)this; // const_cast
9061 self
->ClearAttrCache();
9062 self
->m_attrCache
.row
= row
;
9063 self
->m_attrCache
.col
= col
;
9064 self
->m_attrCache
.attr
= attr
;
9069 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9071 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9073 *attr
= m_attrCache
.attr
;
9074 wxSafeIncRef(m_attrCache
.attr
);
9076 #ifdef DEBUG_ATTR_CACHE
9077 gs_nAttrCacheHits
++;
9084 #ifdef DEBUG_ATTR_CACHE
9085 gs_nAttrCacheMisses
++;
9091 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9093 wxGridCellAttr
*attr
= NULL
;
9094 // Additional test to avoid looking at the cache e.g. for
9095 // wxNoCellCoords, as this will confuse memory management.
9098 if ( !LookupAttr(row
, col
, &attr
) )
9100 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9101 : (wxGridCellAttr
*)NULL
;
9102 CacheAttr(row
, col
, attr
);
9107 attr
->SetDefAttr(m_defaultCellAttr
);
9111 attr
= m_defaultCellAttr
;
9118 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9120 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9121 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9123 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9124 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9126 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9129 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9131 // artificially inc the ref count to match DecRef() in caller
9133 m_table
->SetAttr(attr
, row
, col
);
9139 // ----------------------------------------------------------------------------
9140 // setting column attributes (wrappers around SetColAttr)
9141 // ----------------------------------------------------------------------------
9143 void wxGrid::SetColFormatBool(int col
)
9145 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9148 void wxGrid::SetColFormatNumber(int col
)
9150 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9153 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9155 wxString typeName
= wxGRID_VALUE_FLOAT
;
9156 if ( (width
!= -1) || (precision
!= -1) )
9158 typeName
<< _T(':') << width
<< _T(',') << precision
;
9161 SetColFormatCustom(col
, typeName
);
9164 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9166 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9168 attr
= new wxGridCellAttr
;
9169 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9170 attr
->SetRenderer(renderer
);
9172 SetColAttr(col
, attr
);
9176 // ----------------------------------------------------------------------------
9177 // setting cell attributes: this is forwarded to the table
9178 // ----------------------------------------------------------------------------
9180 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9182 if ( CanHaveAttributes() )
9184 m_table
->SetAttr(attr
, row
, col
);
9193 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9195 if ( CanHaveAttributes() )
9197 m_table
->SetRowAttr(attr
, row
);
9206 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9208 if ( CanHaveAttributes() )
9210 m_table
->SetColAttr(attr
, col
);
9219 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9221 if ( CanHaveAttributes() )
9223 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9224 attr
->SetBackgroundColour(colour
);
9229 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9231 if ( CanHaveAttributes() )
9233 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9234 attr
->SetTextColour(colour
);
9239 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9241 if ( CanHaveAttributes() )
9243 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9244 attr
->SetFont(font
);
9249 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9251 if ( CanHaveAttributes() )
9253 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9254 attr
->SetAlignment(horiz
, vert
);
9259 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9261 if ( CanHaveAttributes() )
9263 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9264 attr
->SetOverflow(allow
);
9269 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9271 if ( CanHaveAttributes() )
9273 int cell_rows
, cell_cols
;
9275 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9276 attr
->GetSize(&cell_rows
, &cell_cols
);
9277 attr
->SetSize(num_rows
, num_cols
);
9280 // Cannot set the size of a cell to 0 or negative values
9281 // While it is perfectly legal to do that, this function cannot
9282 // handle all the possibilies, do it by hand by getting the CellAttr.
9283 // You can only set the size of a cell to 1,1 or greater with this fn
9284 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9285 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9286 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9287 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9289 // if this was already a multicell then "turn off" the other cells first
9290 if ((cell_rows
> 1) || (cell_rows
> 1))
9293 for (j
=row
; j
<row
+cell_rows
; j
++)
9295 for (i
=col
; i
<col
+cell_cols
; i
++)
9297 if ((i
!= col
) || (j
!= row
))
9299 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9300 attr_stub
->SetSize( 1, 1 );
9301 attr_stub
->DecRef();
9307 // mark the cells that will be covered by this cell to
9308 // negative or zero values to point back at this cell
9309 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9312 for (j
=row
; j
<row
+num_rows
; j
++)
9314 for (i
=col
; i
<col
+num_cols
; i
++)
9316 if ((i
!= col
) || (j
!= row
))
9318 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9319 attr_stub
->SetSize( row
-j
, col
-i
);
9320 attr_stub
->DecRef();
9328 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9330 if ( CanHaveAttributes() )
9332 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9333 attr
->SetRenderer(renderer
);
9338 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9340 if ( CanHaveAttributes() )
9342 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9343 attr
->SetEditor(editor
);
9348 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9350 if ( CanHaveAttributes() )
9352 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9353 attr
->SetReadOnly(isReadOnly
);
9358 // ----------------------------------------------------------------------------
9359 // Data type registration
9360 // ----------------------------------------------------------------------------
9362 void wxGrid::RegisterDataType(const wxString
& typeName
,
9363 wxGridCellRenderer
* renderer
,
9364 wxGridCellEditor
* editor
)
9366 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9370 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9372 wxString typeName
= m_table
->GetTypeName(row
, col
);
9373 return GetDefaultEditorForType(typeName
);
9376 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9378 wxString typeName
= m_table
->GetTypeName(row
, col
);
9379 return GetDefaultRendererForType(typeName
);
9383 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9385 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9386 if ( index
== wxNOT_FOUND
)
9388 wxFAIL_MSG(wxT("Unknown data type name"));
9393 return m_typeRegistry
->GetEditor(index
);
9397 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9399 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9400 if ( index
== wxNOT_FOUND
)
9402 wxFAIL_MSG(wxT("Unknown data type name"));
9407 return m_typeRegistry
->GetRenderer(index
);
9411 // ----------------------------------------------------------------------------
9413 // ----------------------------------------------------------------------------
9415 void wxGrid::EnableDragRowSize( bool enable
)
9417 m_canDragRowSize
= enable
;
9421 void wxGrid::EnableDragColSize( bool enable
)
9423 m_canDragColSize
= enable
;
9426 void wxGrid::EnableDragGridSize( bool enable
)
9428 m_canDragGridSize
= enable
;
9431 void wxGrid::EnableDragCell( bool enable
)
9433 m_canDragCell
= enable
;
9436 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9438 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9440 if ( resizeExistingRows
)
9442 // since we are resizing all rows to the default row size,
9443 // we can simply clear the row heights and row bottoms
9444 // arrays (which also allows us to take advantage of
9445 // some speed optimisations)
9446 m_rowHeights
.Empty();
9447 m_rowBottoms
.Empty();
9448 if ( !GetBatchCount() )
9453 void wxGrid::SetRowSize( int row
, int height
)
9455 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9457 // See comment in SetColSize
9458 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9460 if ( m_rowHeights
.IsEmpty() )
9462 // need to really create the array
9466 int h
= wxMax( 0, height
);
9467 int diff
= h
- m_rowHeights
[row
];
9469 m_rowHeights
[row
] = h
;
9471 for ( i
= row
; i
< m_numRows
; i
++ )
9473 m_rowBottoms
[i
] += diff
;
9475 if ( !GetBatchCount() )
9479 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9481 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9483 if ( resizeExistingCols
)
9485 // since we are resizing all columns to the default column size,
9486 // we can simply clear the col widths and col rights
9487 // arrays (which also allows us to take advantage of
9488 // some speed optimisations)
9489 m_colWidths
.Empty();
9490 m_colRights
.Empty();
9491 if ( !GetBatchCount() )
9496 void wxGrid::SetColSize( int col
, int width
)
9498 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9500 // should we check that it's bigger than GetColMinimalWidth(col) here?
9502 // No, because it is reasonable to assume the library user know's
9503 // what he is doing. However whe should test against the weaker
9504 // constariant of minimalAcceptableWidth, as this breaks rendering
9506 // This test then fixes sf.net bug #645734
9508 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9510 if ( m_colWidths
.IsEmpty() )
9512 // need to really create the array
9516 // if < 0 calc new width from label
9520 wxArrayString lines
;
9521 wxClientDC
dc(m_colLabelWin
);
9522 dc
.SetFont(GetLabelFont());
9523 StringToLines(GetColLabelValue(col
), lines
);
9524 GetTextBoxSize(dc
, lines
, &w
, &h
);
9527 int w
= wxMax( 0, width
);
9528 int diff
= w
- m_colWidths
[col
];
9529 m_colWidths
[col
] = w
;
9532 for ( i
= col
; i
< m_numCols
; i
++ )
9534 m_colRights
[i
] += diff
;
9536 if ( !GetBatchCount() )
9541 void wxGrid::SetColMinimalWidth( int col
, int width
)
9543 if (width
> GetColMinimalAcceptableWidth()) {
9544 m_colMinWidths
[col
] = width
;
9548 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9550 if (width
> GetRowMinimalAcceptableHeight()) {
9551 m_rowMinHeights
[row
] = width
;
9555 int wxGrid::GetColMinimalWidth(int col
) const
9557 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(col
);
9558 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9561 int wxGrid::GetRowMinimalHeight(int row
) const
9563 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(row
);
9564 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9567 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9569 // We do allow a width of 0 since this gives us
9570 // an easy way to temporarily hidding columns.
9573 m_minAcceptableColWidth
= width
;
9576 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9578 // We do allow a height of 0 since this gives us
9579 // an easy way to temporarily hidding rows.
9582 m_minAcceptableRowHeight
= height
;
9585 int wxGrid::GetColMinimalAcceptableWidth() const
9587 return m_minAcceptableColWidth
;
9590 int wxGrid::GetRowMinimalAcceptableHeight() const
9592 return m_minAcceptableRowHeight
;
9595 // ----------------------------------------------------------------------------
9597 // ----------------------------------------------------------------------------
9599 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9601 wxClientDC
dc(m_gridWin
);
9603 //Cancel editting of cell
9604 HideCellEditControl();
9605 SaveEditControlValue();
9607 // init both of them to avoid compiler warnings, even if weo nly need one
9615 wxCoord extent
, extentMax
= 0;
9616 int max
= column
? m_numRows
: m_numCols
;
9617 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9624 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9625 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9628 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9629 extent
= column
? size
.x
: size
.y
;
9630 if ( extent
> extentMax
)
9641 // now also compare with the column label extent
9643 dc
.SetFont( GetLabelFont() );
9647 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9648 if( GetColLabelTextOrientation() == wxVERTICAL
)
9652 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9654 extent
= column
? w
: h
;
9655 if ( extent
> extentMax
)
9662 // empty column - give default extent (notice that if extentMax is less
9663 // than default extent but != 0, it's ok)
9664 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9670 // leave some space around text
9681 SetColSize(col
, extentMax
);
9682 if ( !GetBatchCount() )
9685 m_gridWin
->GetClientSize( &cw
, &ch
);
9686 wxRect
rect ( CellToRect( 0, col
) );
9688 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9689 rect
.width
= cw
- rect
.x
;
9690 rect
.height
= m_colLabelHeight
;
9691 m_colLabelWin
->Refresh( true, &rect
);
9696 SetRowSize(row
, extentMax
);
9697 if ( !GetBatchCount() )
9700 m_gridWin
->GetClientSize( &cw
, &ch
);
9701 wxRect
rect ( CellToRect( row
, 0 ) );
9703 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9704 rect
.width
= m_rowLabelWidth
;
9705 rect
.height
= ch
- rect
.y
;
9706 m_rowLabelWin
->Refresh( true, &rect
);
9712 SetColMinimalWidth(col
, extentMax
);
9714 SetRowMinimalHeight(row
, extentMax
);
9718 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9720 int width
= m_rowLabelWidth
;
9725 for ( int col
= 0; col
< m_numCols
; col
++ )
9729 AutoSizeColumn(col
, setAsMin
);
9732 width
+= GetColWidth(col
);
9741 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9743 int height
= m_colLabelHeight
;
9748 for ( int row
= 0; row
< m_numRows
; row
++ )
9752 AutoSizeRow(row
, setAsMin
);
9755 height
+= GetRowHeight(row
);
9764 void wxGrid::AutoSize()
9768 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9770 // round up the size to a multiple of scroll step - this ensures that we
9771 // won't get the scrollbars if we're sized exactly to this width
9772 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9774 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9775 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9777 // distribute the extra space between the columns/rows to avoid having
9778 // extra white space
9780 // Remove the extra m_extraWidth + 1 added above
9781 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9782 if ( diff
&& m_numCols
)
9784 // try to resize the columns uniformly
9785 wxCoord diffPerCol
= diff
/ m_numCols
;
9788 for ( int col
= 0; col
< m_numCols
; col
++ )
9790 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9794 // add remaining amount to the last columns
9795 diff
-= diffPerCol
* m_numCols
;
9798 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9800 SetColSize(col
, GetColWidth(col
) + 1);
9806 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9807 if ( diff
&& m_numRows
)
9809 // try to resize the columns uniformly
9810 wxCoord diffPerRow
= diff
/ m_numRows
;
9813 for ( int row
= 0; row
< m_numRows
; row
++ )
9815 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9819 // add remaining amount to the last rows
9820 diff
-= diffPerRow
* m_numRows
;
9823 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9825 SetRowSize(row
, GetRowHeight(row
) + 1);
9832 SetClientSize(sizeFit
);
9835 void wxGrid::AutoSizeRowLabelSize( int row
)
9837 wxArrayString lines
;
9840 // Hide the edit control, so it
9841 // won't interfer with drag-shrinking.
9842 if( IsCellEditControlShown() )
9844 HideCellEditControl();
9845 SaveEditControlValue();
9848 // autosize row height depending on label text
9849 StringToLines( GetRowLabelValue( row
), lines
);
9850 wxClientDC
dc( m_rowLabelWin
);
9851 GetTextBoxSize( dc
, lines
, &w
, &h
);
9852 if( h
< m_defaultRowHeight
)
9853 h
= m_defaultRowHeight
;
9858 void wxGrid::AutoSizeColLabelSize( int col
)
9860 wxArrayString lines
;
9863 // Hide the edit control, so it
9864 // won't interfer with drag-shrinking.
9865 if( IsCellEditControlShown() )
9867 HideCellEditControl();
9868 SaveEditControlValue();
9871 // autosize column width depending on label text
9872 StringToLines( GetColLabelValue( col
), lines
);
9873 wxClientDC
dc( m_colLabelWin
);
9874 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9875 GetTextBoxSize( dc
, lines
, &w
, &h
);
9877 GetTextBoxSize( dc
, lines
, &h
, &w
);
9878 if( w
< m_defaultColWidth
)
9879 w
= m_defaultColWidth
;
9884 wxSize
wxGrid::DoGetBestSize() const
9886 // don't set sizes, only calculate them
9887 wxGrid
*self
= (wxGrid
*)this; // const_cast
9890 width
= self
->SetOrCalcColumnSizes(true);
9891 height
= self
->SetOrCalcRowSizes(true);
9893 if (!width
) width
=100;
9894 if (!height
) height
=80;
9896 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9897 // of the scrollbars, is there any magic incantaion for that?
9899 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9901 width
+= 1 + xpu
- (width
% xpu
);
9903 height
+= 1 + ypu
- (height
% ypu
);
9905 // limit to 1/4 of the screen size
9906 int maxwidth
, maxheight
;
9907 wxDisplaySize( & maxwidth
, & maxheight
);
9910 if ( width
> maxwidth
) width
= maxwidth
;
9911 if ( height
> maxheight
) height
= maxheight
;
9914 wxSize
best(width
, height
);
9915 // NOTE: This size should be cached, but first we need to add calls to
9916 // InvalidateBestSize everywhere that could change the results of this
9918 // CacheBestSize(size);
9928 wxPen
& wxGrid::GetDividerPen() const
9933 // ----------------------------------------------------------------------------
9934 // cell value accessor functions
9935 // ----------------------------------------------------------------------------
9937 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9941 m_table
->SetValue( row
, col
, s
);
9942 if ( !GetBatchCount() )
9945 wxRect
rect( CellToRect( row
, col
) );
9947 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
9948 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9949 m_gridWin
->Refresh( false, &rect
);
9952 if ( m_currentCellCoords
.GetRow() == row
&&
9953 m_currentCellCoords
.GetCol() == col
&&
9954 IsCellEditControlShown())
9955 // Note: If we are using IsCellEditControlEnabled,
9956 // this interacts badly with calling SetCellValue from
9957 // an EVT_GRID_CELL_CHANGE handler.
9959 HideCellEditControl();
9960 ShowCellEditControl(); // will reread data from table
9967 // ------ Block, row and col selection
9970 void wxGrid::SelectRow( int row
, bool addToSelected
)
9972 if ( IsSelection() && !addToSelected
)
9976 m_selection
->SelectRow( row
, false, addToSelected
);
9980 void wxGrid::SelectCol( int col
, bool addToSelected
)
9982 if ( IsSelection() && !addToSelected
)
9986 m_selection
->SelectCol( col
, false, addToSelected
);
9990 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
9991 bool addToSelected
)
9993 if ( IsSelection() && !addToSelected
)
9997 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
9998 false, addToSelected
);
10002 void wxGrid::SelectAll()
10004 if ( m_numRows
> 0 && m_numCols
> 0 )
10007 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10012 // ------ Cell, row and col deselection
10015 void wxGrid::DeselectRow( int row
)
10017 if ( !m_selection
)
10020 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10022 if ( m_selection
->IsInSelection(row
, 0 ) )
10023 m_selection
->ToggleCellSelection( row
, 0);
10027 int nCols
= GetNumberCols();
10028 for ( int i
= 0; i
< nCols
; i
++ )
10030 if ( m_selection
->IsInSelection(row
, i
) )
10031 m_selection
->ToggleCellSelection( row
, i
);
10036 void wxGrid::DeselectCol( int col
)
10038 if ( !m_selection
)
10041 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10043 if ( m_selection
->IsInSelection(0, col
) )
10044 m_selection
->ToggleCellSelection( 0, col
);
10048 int nRows
= GetNumberRows();
10049 for ( int i
= 0; i
< nRows
; i
++ )
10051 if ( m_selection
->IsInSelection(i
, col
) )
10052 m_selection
->ToggleCellSelection(i
, col
);
10057 void wxGrid::DeselectCell( int row
, int col
)
10059 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10060 m_selection
->ToggleCellSelection(row
, col
);
10063 bool wxGrid::IsSelection()
10065 return ( m_selection
&& (m_selection
->IsSelection() ||
10066 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10067 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10070 bool wxGrid::IsInSelection( int row
, int col
) const
10072 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10073 ( row
>= m_selectingTopLeft
.GetRow() &&
10074 col
>= m_selectingTopLeft
.GetCol() &&
10075 row
<= m_selectingBottomRight
.GetRow() &&
10076 col
<= m_selectingBottomRight
.GetCol() )) );
10079 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10081 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10082 return m_selection
->m_cellSelection
;
10084 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10086 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10087 return m_selection
->m_blockSelectionTopLeft
;
10089 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10091 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10092 return m_selection
->m_blockSelectionBottomRight
;
10094 wxArrayInt
wxGrid::GetSelectedRows() const
10096 if (!m_selection
) { wxArrayInt a
; return a
; }
10097 return m_selection
->m_rowSelection
;
10099 wxArrayInt
wxGrid::GetSelectedCols() const
10101 if (!m_selection
) { wxArrayInt a
; return a
; }
10102 return m_selection
->m_colSelection
;
10106 void wxGrid::ClearSelection()
10108 m_selectingTopLeft
= wxGridNoCellCoords
;
10109 m_selectingBottomRight
= wxGridNoCellCoords
;
10111 m_selection
->ClearSelection();
10115 // This function returns the rectangle that encloses the given block
10116 // in device coords clipped to the client size of the grid window.
10118 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10119 const wxGridCellCoords
&bottomRight
)
10121 wxRect
rect( wxGridNoCellRect
);
10124 cellRect
= CellToRect( topLeft
);
10125 if ( cellRect
!= wxGridNoCellRect
)
10131 rect
= wxRect( 0, 0, 0, 0 );
10134 cellRect
= CellToRect( bottomRight
);
10135 if ( cellRect
!= wxGridNoCellRect
)
10141 return wxGridNoCellRect
;
10145 int left
= rect
.GetLeft();
10146 int top
= rect
.GetTop();
10147 int right
= rect
.GetRight();
10148 int bottom
= rect
.GetBottom();
10150 int leftCol
= topLeft
.GetCol();
10151 int topRow
= topLeft
.GetRow();
10152 int rightCol
= bottomRight
.GetCol();
10153 int bottomRow
= bottomRight
.GetRow();
10171 topRow
= bottomRow
;
10176 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10178 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10180 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10182 cellRect
= CellToRect( j
, i
);
10184 if (cellRect
.x
< left
)
10186 if (cellRect
.y
< top
)
10188 if (cellRect
.x
+ cellRect
.width
> right
)
10189 right
= cellRect
.x
+ cellRect
.width
;
10190 if (cellRect
.y
+ cellRect
.height
> bottom
)
10191 bottom
= cellRect
.y
+ cellRect
.height
;
10193 else i
= rightCol
; // jump over inner cells.
10197 // convert to scrolled coords
10199 CalcScrolledPosition( left
, top
, &left
, &top
);
10200 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10203 m_gridWin
->GetClientSize( &cw
, &ch
);
10205 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10206 return wxRect( 0, 0, 0, 0);
10208 rect
.SetLeft( wxMax(0, left
) );
10209 rect
.SetTop( wxMax(0, top
) );
10210 rect
.SetRight( wxMin(cw
, right
) );
10211 rect
.SetBottom( wxMin(ch
, bottom
) );
10217 // ------ Grid event classes
10220 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10222 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10223 int row
, int col
, int x
, int y
, bool sel
,
10224 bool control
, bool shift
, bool alt
, bool meta
)
10225 : wxNotifyEvent( type
, id
)
10232 m_control
= control
;
10237 SetEventObject(obj
);
10241 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10243 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10244 int rowOrCol
, int x
, int y
,
10245 bool control
, bool shift
, bool alt
, bool meta
)
10246 : wxNotifyEvent( type
, id
)
10248 m_rowOrCol
= rowOrCol
;
10251 m_control
= control
;
10256 SetEventObject(obj
);
10260 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10262 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10263 const wxGridCellCoords
& topLeft
,
10264 const wxGridCellCoords
& bottomRight
,
10265 bool sel
, bool control
,
10266 bool shift
, bool alt
, bool meta
)
10267 : wxNotifyEvent( type
, id
)
10269 m_topLeft
= topLeft
;
10270 m_bottomRight
= bottomRight
;
10272 m_control
= control
;
10277 SetEventObject(obj
);
10281 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10283 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10284 wxObject
* obj
, int row
,
10285 int col
, wxControl
* ctrl
)
10286 : wxCommandEvent(type
, id
)
10288 SetEventObject(obj
);
10294 #endif // wxUSE_GRID