1 ///////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilatixon, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/dcclient.h"
26 #include "wx/settings.h"
28 #include "wx/textctrl.h"
29 #include "wx/checkbox.h"
30 #include "wx/combobox.h"
31 #include "wx/valtext.h"
36 #include "wx/textfile.h"
37 #include "wx/spinctrl.h"
38 #include "wx/tokenzr.h"
39 #include "wx/renderer.h"
42 #include "wx/generic/gridsel.h"
44 #if defined(__WXMOTIF__)
45 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
47 #define WXUNUSED_MOTIF(identifier) identifier
50 #if defined(__WXGTK__)
51 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
53 #define WXUNUSED_GTK(identifier) identifier
56 // Required for wxIs... functions
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
64 class WXDLLIMPEXP_ADV
);
66 struct wxGridCellWithAttr
68 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
69 : coords(row
, col
), attr(attr_
)
78 wxGridCellCoords coords
;
82 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
83 // without rewriting the macros, which require a public copy constructor.
86 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
87 class WXDLLIMPEXP_ADV
);
89 #include "wx/arrimpl.cpp"
91 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
92 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
99 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
102 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
103 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
107 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
108 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
109 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
123 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
124 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
125 const wxPoint
&pos
, const wxSize
&size
);
130 void OnPaint( wxPaintEvent
& event
);
131 void OnMouseEvent( wxMouseEvent
& event
);
132 void OnMouseWheel( wxMouseEvent
& event
);
133 void OnKeyDown( wxKeyEvent
& event
);
134 void OnKeyUp( wxKeyEvent
& );
135 void OnChar( wxKeyEvent
& );
137 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
138 DECLARE_EVENT_TABLE()
139 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
143 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
146 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
147 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
148 const wxPoint
&pos
, const wxSize
&size
);
153 void OnPaint( wxPaintEvent
&event
);
154 void OnMouseEvent( wxMouseEvent
& event
);
155 void OnMouseWheel( wxMouseEvent
& event
);
156 void OnKeyDown( wxKeyEvent
& event
);
157 void OnKeyUp( wxKeyEvent
& );
158 void OnChar( wxKeyEvent
& );
160 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
161 DECLARE_EVENT_TABLE()
162 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
166 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
169 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
170 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
171 const wxPoint
&pos
, const wxSize
&size
);
176 void OnMouseEvent( wxMouseEvent
& event
);
177 void OnMouseWheel( wxMouseEvent
& event
);
178 void OnKeyDown( wxKeyEvent
& event
);
179 void OnKeyUp( wxKeyEvent
& );
180 void OnChar( wxKeyEvent
& );
181 void OnPaint( wxPaintEvent
& event
);
183 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
184 DECLARE_EVENT_TABLE()
185 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
188 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
193 m_owner
= (wxGrid
*)NULL
;
194 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
195 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
198 wxGridWindow( wxGrid
*parent
,
199 wxGridRowLabelWindow
*rowLblWin
,
200 wxGridColLabelWindow
*colLblWin
,
201 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
204 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
206 wxGrid
* GetOwner() { return m_owner
; }
210 wxGridRowLabelWindow
*m_rowLabelWin
;
211 wxGridColLabelWindow
*m_colLabelWin
;
213 void OnPaint( wxPaintEvent
&event
);
214 void OnMouseWheel( wxMouseEvent
& event
);
215 void OnMouseEvent( wxMouseEvent
& event
);
216 void OnKeyDown( wxKeyEvent
& );
217 void OnKeyUp( wxKeyEvent
& );
218 void OnChar( wxKeyEvent
& );
219 void OnEraseBackground( wxEraseEvent
& );
220 void OnFocus( wxFocusEvent
& );
222 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
223 DECLARE_EVENT_TABLE()
224 DECLARE_NO_COPY_CLASS(wxGridWindow
)
228 class wxGridCellEditorEvtHandler
: public wxEvtHandler
231 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
238 void OnKillFocus(wxFocusEvent
& event
);
239 void OnKeyDown(wxKeyEvent
& event
);
240 void OnChar(wxKeyEvent
& event
);
242 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
246 wxGridCellEditor
*m_editor
;
248 // Work around the fact that a focus kill event can be sent to
249 // a combobox within a set focus event.
252 DECLARE_EVENT_TABLE()
253 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
254 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
258 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
260 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
261 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
262 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
263 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
268 // ----------------------------------------------------------------------------
269 // the internal data representation used by wxGridCellAttrProvider
270 // ----------------------------------------------------------------------------
272 // this class stores attributes set for cells
273 class WXDLLIMPEXP_ADV wxGridCellAttrData
276 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
277 wxGridCellAttr
*GetAttr(int row
, int col
) const;
278 void UpdateAttrRows( size_t pos
, int numRows
);
279 void UpdateAttrCols( size_t pos
, int numCols
);
282 // searches for the attr for given cell, returns wxNOT_FOUND if not found
283 int FindIndex(int row
, int col
) const;
285 wxGridCellWithAttrArray m_attrs
;
288 // this class stores attributes set for rows or columns
289 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
292 // empty ctor to suppress warnings
293 wxGridRowOrColAttrData() {}
294 ~wxGridRowOrColAttrData();
296 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
297 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
298 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
301 wxArrayInt m_rowsOrCols
;
302 wxArrayAttrs m_attrs
;
305 // NB: this is just a wrapper around 3 objects: one which stores cell
306 // attributes, and 2 others for row/col ones
307 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
310 wxGridCellAttrData m_cellAttrs
;
311 wxGridRowOrColAttrData m_rowAttrs
,
316 // ----------------------------------------------------------------------------
317 // data structures used for the data type registry
318 // ----------------------------------------------------------------------------
320 struct wxGridDataTypeInfo
322 wxGridDataTypeInfo(const wxString
& typeName
,
323 wxGridCellRenderer
* renderer
,
324 wxGridCellEditor
* editor
)
325 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
328 ~wxGridDataTypeInfo()
330 wxSafeDecRef(m_renderer
);
331 wxSafeDecRef(m_editor
);
335 wxGridCellRenderer
* m_renderer
;
336 wxGridCellEditor
* m_editor
;
338 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
342 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
343 class WXDLLIMPEXP_ADV
);
346 class WXDLLIMPEXP_ADV wxGridTypeRegistry
349 wxGridTypeRegistry() {}
350 ~wxGridTypeRegistry();
352 void RegisterDataType(const wxString
& typeName
,
353 wxGridCellRenderer
* renderer
,
354 wxGridCellEditor
* editor
);
356 // find one of already registered data types
357 int FindRegisteredDataType(const wxString
& typeName
);
359 // try to FindRegisteredDataType(), if this fails and typeName is one of
360 // standard typenames, register it and return its index
361 int FindDataType(const wxString
& typeName
);
363 // try to FindDataType(), if it fails see if it is not one of already
364 // registered data types with some params in which case clone the
365 // registered data type and set params for it
366 int FindOrCloneDataType(const wxString
& typeName
);
368 wxGridCellRenderer
* GetRenderer(int index
);
369 wxGridCellEditor
* GetEditor(int index
);
372 wxGridDataTypeInfoArray m_typeinfo
;
375 // ----------------------------------------------------------------------------
376 // conditional compilation
377 // ----------------------------------------------------------------------------
379 #ifndef WXGRID_DRAW_LINES
380 #define WXGRID_DRAW_LINES 1
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 //#define DEBUG_ATTR_CACHE
388 #ifdef DEBUG_ATTR_CACHE
389 static size_t gs_nAttrCacheHits
= 0;
390 static size_t gs_nAttrCacheMisses
= 0;
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
397 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
398 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
401 // TODO: this doesn't work at all, grid cells have different sizes and approx
402 // calculations don't work as because of the size mismatch scrollbars
403 // sometimes fail to be shown when they should be or vice versa
405 // The scroll bars may be a little flakey once in a while, but that is
406 // surely much less horrible than having scroll lines of only 1!!!
409 // Well, it's still seriously broken so it might be better but needs
412 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
413 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
415 // the size of hash tables used a bit everywhere (the max number of elements
416 // in these hash tables is the number of rows/columns)
417 static const int GRID_HASH_SIZE
= 100;
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 static inline int GetScrollX(int x
)
426 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
429 static inline int GetScrollY(int y
)
431 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
435 // ============================================================================
437 // ============================================================================
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 wxGridCellEditor::wxGridCellEditor()
449 wxGridCellEditor::~wxGridCellEditor()
454 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
455 wxWindowID
WXUNUSED(id
),
456 wxEvtHandler
* evtHandler
)
459 m_control
->PushEventHandler(evtHandler
);
462 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
463 wxGridCellAttr
*attr
)
465 // erase the background because we might not fill the cell
466 wxClientDC
dc(m_control
->GetParent());
467 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
469 gridWindow
->GetOwner()->PrepareDC(dc
);
471 dc
.SetPen(*wxTRANSPARENT_PEN
);
472 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
473 dc
.DrawRectangle(rectCell
);
475 // redraw the control we just painted over
476 m_control
->Refresh();
479 void wxGridCellEditor::Destroy()
483 m_control
->PopEventHandler(true /* delete it*/);
485 m_control
->Destroy();
490 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
492 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
494 m_control
->Show(show
);
498 // set the colours/fonts if we have any
501 m_colFgOld
= m_control
->GetForegroundColour();
502 m_control
->SetForegroundColour(attr
->GetTextColour());
504 m_colBgOld
= m_control
->GetBackgroundColour();
505 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
507 // Workaround for GTK+1 font setting problem on some platforms
508 #if !defined(__WXGTK__) || defined(__WXGTK20__)
509 m_fontOld
= m_control
->GetFont();
510 m_control
->SetFont(attr
->GetFont());
512 // can't do anything more in the base class version, the other
513 // attributes may only be used by the derived classes
518 // restore the standard colours fonts
519 if ( m_colFgOld
.Ok() )
521 m_control
->SetForegroundColour(m_colFgOld
);
522 m_colFgOld
= wxNullColour
;
525 if ( m_colBgOld
.Ok() )
527 m_control
->SetBackgroundColour(m_colBgOld
);
528 m_colBgOld
= wxNullColour
;
531 // Workaround for GTK+1 font setting problem on some platforms
532 #if !defined(__WXGTK__) || defined(__WXGTK20__)
533 if ( m_fontOld
.Ok() )
535 m_control
->SetFont(m_fontOld
);
536 m_fontOld
= wxNullFont
;
542 void wxGridCellEditor::SetSize(const wxRect
& rect
)
544 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
546 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
549 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
554 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
556 bool ctrl
= event
.ControlDown();
557 bool alt
= event
.AltDown();
560 // On the Mac the Alt key is more like shift and is used for entry of
561 // valid characters, so check for Ctrl and Meta instead.
562 alt
= event
.MetaDown();
565 // Assume it's not a valid char if ctrl or alt is down, but if both are
566 // down then it may be because of an AltGr key combination, so let them
567 // through in that case.
568 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
575 // if the unicode key code is not really a unicode character (it may
576 // be a function key or etc., the platforms appear to always give us a
577 // small value in this case) then fallback to the ASCII key code but
578 // don't do anything for function keys or etc.
579 key
= event
.GetUnicodeKey();
582 key
= event
.GetKeyCode();
583 keyOk
= (key
<= 127);
586 key
= event
.GetKeyCode();
587 keyOk
= (key
<= 255);
593 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
598 void wxGridCellEditor::StartingClick()
604 // ----------------------------------------------------------------------------
605 // wxGridCellTextEditor
606 // ----------------------------------------------------------------------------
608 wxGridCellTextEditor::wxGridCellTextEditor()
613 void wxGridCellTextEditor::Create(wxWindow
* parent
,
615 wxEvtHandler
* evtHandler
)
617 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
618 wxDefaultPosition
, wxDefaultSize
619 #if defined(__WXMSW__)
620 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
624 // set max length allowed in the textctrl, if the parameter was set
627 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
630 wxGridCellEditor::Create(parent
, id
, evtHandler
);
633 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
634 wxGridCellAttr
* WXUNUSED(attr
))
636 // as we fill the entire client area, don't do anything here to minimize
640 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
642 wxRect
rect(rectOrig
);
644 // Make the edit control large enough to allow for internal margins
646 // TODO: remove this if the text ctrl sizing is improved esp. for unix
648 #if defined(__WXGTK__)
657 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
659 // MB: treat MSW separately here otherwise the caret doesn't show
660 // when the editor is in the first row.
661 #if defined(__WXMSW__)
664 int extra_y
= ( rect
.y
> 2 ) ? 2 : 1;
667 #if defined(__WXMOTIF__)
672 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
673 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
674 rect
.SetRight( rect
.GetRight() + 2 * extra_x
);
675 rect
.SetBottom( rect
.GetBottom() + 2 * extra_y
);
678 wxGridCellEditor::SetSize(rect
);
681 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
683 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
685 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
687 DoBeginEdit(m_startValue
);
690 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
692 Text()->SetValue(startValue
);
693 Text()->SetInsertionPointEnd();
694 Text()->SetSelection(-1, -1);
698 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
701 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
703 bool changed
= false;
704 wxString value
= Text()->GetValue();
705 if (value
!= m_startValue
)
709 grid
->GetTable()->SetValue(row
, col
, value
);
711 m_startValue
= wxEmptyString
;
712 // No point in setting the text of the hidden control
713 //Text()->SetValue(m_startValue);
718 void wxGridCellTextEditor::Reset()
720 wxASSERT_MSG(m_control
, wxT("The wxGridCellEditor must be created first!"));
722 DoReset(m_startValue
);
725 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
727 Text()->SetValue(startValue
);
728 Text()->SetInsertionPointEnd();
731 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
733 return wxGridCellEditor::IsAcceptedKey(event
);
736 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
738 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
739 // longer an appropriate way to get the character into the text control.
740 // Do it ourselves instead. We know that if we get this far that we have
741 // a valid character, so not a whole lot of testing needs to be done.
743 wxTextCtrl
* tc
= Text();
748 ch
= event
.GetUnicodeKey();
750 ch
= (wxChar
)event
.GetKeyCode();
752 ch
= (wxChar
)event
.GetKeyCode();
758 // delete the character at the cursor
759 pos
= tc
->GetInsertionPoint();
760 if (pos
< tc
->GetLastPosition())
761 tc
->Remove(pos
, pos
+ 1);
765 // delete the character before the cursor
766 pos
= tc
->GetInsertionPoint();
768 tc
->Remove(pos
- 1, pos
);
777 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
778 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
780 #if defined(__WXMOTIF__) || defined(__WXGTK__)
781 // wxMotif needs a little extra help...
782 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
783 wxString
s( Text()->GetValue() );
784 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
786 Text()->SetInsertionPoint( pos
);
788 // the other ports can handle a Return key press
794 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
804 if ( !params
.ToLong(&tmp
) )
806 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
810 m_maxChars
= (size_t)tmp
;
815 // return the value in the text control
816 wxString
wxGridCellTextEditor::GetValue() const
818 return Text()->GetValue();
821 // ----------------------------------------------------------------------------
822 // wxGridCellNumberEditor
823 // ----------------------------------------------------------------------------
825 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
831 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
833 wxEvtHandler
* evtHandler
)
838 // create a spin ctrl
839 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
840 wxDefaultPosition
, wxDefaultSize
,
844 wxGridCellEditor::Create(parent
, id
, evtHandler
);
849 // just a text control
850 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
853 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
854 #endif // wxUSE_VALIDATORS
858 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
860 // first get the value
861 wxGridTableBase
*table
= grid
->GetTable();
862 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
864 m_valueOld
= table
->GetValueAsLong(row
, col
);
869 wxString sValue
= table
->GetValue(row
, col
);
870 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
872 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
880 Spin()->SetValue((int)m_valueOld
);
886 DoBeginEdit(GetString());
890 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
900 value
= Spin()->GetValue();
901 changed
= value
!= m_valueOld
;
903 text
= wxString::Format(wxT("%ld"), value
);
908 text
= Text()->GetValue();
909 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
914 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
915 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
917 grid
->GetTable()->SetValue(row
, col
, text
);
923 void wxGridCellNumberEditor::Reset()
928 Spin()->SetValue((int)m_valueOld
);
933 DoReset(GetString());
937 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
939 if ( wxGridCellEditor::IsAcceptedKey(event
) )
941 int keycode
= event
.GetKeyCode();
942 if ( (keycode
< 128) &&
943 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
952 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
954 int keycode
= event
.GetKeyCode();
957 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
959 wxGridCellTextEditor::StartingKey(event
);
968 if ( wxIsdigit(keycode
) )
970 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
971 spin
->SetValue(keycode
- '0');
972 spin
->SetSelection(1,1);
981 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
992 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
996 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1000 // skip the error message below
1005 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1009 // return the value in the spin control if it is there (the text control otherwise)
1010 wxString
wxGridCellNumberEditor::GetValue() const
1017 long value
= Spin()->GetValue();
1018 s
.Printf(wxT("%ld"), value
);
1023 s
= Text()->GetValue();
1029 // ----------------------------------------------------------------------------
1030 // wxGridCellFloatEditor
1031 // ----------------------------------------------------------------------------
1033 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1036 m_precision
= precision
;
1039 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1041 wxEvtHandler
* evtHandler
)
1043 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1045 #if wxUSE_VALIDATORS
1046 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1047 #endif // wxUSE_VALIDATORS
1050 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1052 // first get the value
1053 wxGridTableBase
*table
= grid
->GetTable();
1054 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1056 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1061 wxString sValue
= table
->GetValue(row
, col
);
1062 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1064 wxFAIL_MSG( _T("this cell doesn't have float value") );
1069 DoBeginEdit(GetString());
1072 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1076 wxString
text(Text()->GetValue());
1078 if ( (text
.empty() || text
.ToDouble(&value
)) &&
1079 !wxIsSameDouble(value
, m_valueOld
) )
1081 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1082 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1084 grid
->GetTable()->SetValue(row
, col
, text
);
1092 void wxGridCellFloatEditor::Reset()
1094 DoReset(GetString());
1097 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1099 int keycode
= event
.GetKeyCode();
1101 tmpbuf
[0] = (char) keycode
;
1103 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1106 bool is_decimal_point
= ( strbuf
==
1107 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1109 bool is_decimal_point
= ( strbuf
== _T(".") );
1112 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1113 || is_decimal_point
)
1115 wxGridCellTextEditor::StartingKey(event
);
1117 // skip Skip() below
1124 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1135 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1139 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1141 m_precision
= (int)tmp
;
1143 // skip the error message below
1148 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1152 wxString
wxGridCellFloatEditor::GetString() const
1155 if ( m_precision
== -1 && m_width
!= -1)
1157 // default precision
1158 fmt
.Printf(_T("%%%d.f"), m_width
);
1160 else if ( m_precision
!= -1 && m_width
== -1)
1163 fmt
.Printf(_T("%%.%df"), m_precision
);
1165 else if ( m_precision
!= -1 && m_width
!= -1 )
1167 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1171 // default width/precision
1175 return wxString::Format(fmt
, m_valueOld
);
1178 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1180 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1182 int keycode
= event
.GetKeyCode();
1183 printf("%d\n", keycode
);
1184 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1186 tmpbuf
[0] = (char) keycode
;
1188 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1191 bool is_decimal_point
=
1192 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1193 wxLOCALE_CAT_NUMBER
) );
1195 bool is_decimal_point
= ( strbuf
== _T(".") );
1198 if ( (keycode
< 128) &&
1199 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1200 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1209 #endif // wxUSE_TEXTCTRL
1213 // ----------------------------------------------------------------------------
1214 // wxGridCellBoolEditor
1215 // ----------------------------------------------------------------------------
1217 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1219 wxEvtHandler
* evtHandler
)
1221 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1222 wxDefaultPosition
, wxDefaultSize
,
1225 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1228 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1230 bool resize
= false;
1231 wxSize size
= m_control
->GetSize();
1232 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1234 // check if the checkbox is not too big/small for this cell
1235 wxSize sizeBest
= m_control
->GetBestSize();
1236 if ( !(size
== sizeBest
) )
1238 // reset to default size if it had been made smaller
1244 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1246 // leave 1 pixel margin
1247 size
.x
= size
.y
= minSize
- 2;
1254 m_control
->SetSize(size
);
1257 // position it in the centre of the rectangle (TODO: support alignment?)
1259 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1260 // the checkbox without label still has some space to the right in wxGTK,
1261 // so shift it to the right
1263 #elif defined(__WXMSW__)
1264 // here too, but in other way
1269 int hAlign
= wxALIGN_CENTRE
;
1270 int vAlign
= wxALIGN_CENTRE
;
1272 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1275 if (hAlign
== wxALIGN_LEFT
)
1283 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1285 else if (hAlign
== wxALIGN_RIGHT
)
1287 x
= r
.x
+ r
.width
- size
.x
- 2;
1288 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1290 else if (hAlign
== wxALIGN_CENTRE
)
1292 x
= r
.x
+ r
.width
/ 2 - size
.x
/ 2;
1293 y
= r
.y
+ r
.height
/ 2 - size
.y
/ 2;
1296 m_control
->Move(x
, y
);
1299 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1301 m_control
->Show(show
);
1305 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1306 CBox()->SetBackgroundColour(colBg
);
1310 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1312 wxASSERT_MSG(m_control
,
1313 wxT("The wxGridCellEditor must be Created first!"));
1315 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1317 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1321 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1322 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1325 CBox()->SetValue(m_startValue
);
1329 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1332 wxASSERT_MSG(m_control
,
1333 wxT("The wxGridCellEditor must be Created first!"));
1335 bool changed
= false;
1336 bool value
= CBox()->GetValue();
1337 if ( value
!= m_startValue
)
1342 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1343 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1345 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1351 void wxGridCellBoolEditor::Reset()
1353 wxASSERT_MSG(m_control
,
1354 wxT("The wxGridCellEditor must be Created first!"));
1356 CBox()->SetValue(m_startValue
);
1359 void wxGridCellBoolEditor::StartingClick()
1361 CBox()->SetValue(!CBox()->GetValue());
1364 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1366 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1368 int keycode
= event
.GetKeyCode();
1381 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1383 int keycode
= event
.GetKeyCode();
1387 CBox()->SetValue(!CBox()->GetValue());
1391 CBox()->SetValue(true);
1395 CBox()->SetValue(false);
1401 // return the value as "1" for true and the empty string for false
1402 wxString
wxGridCellBoolEditor::GetValue() const
1404 bool bSet
= CBox()->GetValue();
1405 return bSet
? _T("1") : wxEmptyString
;
1408 #endif // wxUSE_CHECKBOX
1412 // ----------------------------------------------------------------------------
1413 // wxGridCellChoiceEditor
1414 // ----------------------------------------------------------------------------
1416 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1418 : m_choices(choices
),
1419 m_allowOthers(allowOthers
) { }
1421 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1422 const wxString choices
[],
1424 : m_allowOthers(allowOthers
)
1428 m_choices
.Alloc(count
);
1429 for ( size_t n
= 0; n
< count
; n
++ )
1431 m_choices
.Add(choices
[n
]);
1436 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1438 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1439 editor
->m_allowOthers
= m_allowOthers
;
1440 editor
->m_choices
= m_choices
;
1445 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1447 wxEvtHandler
* evtHandler
)
1449 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1450 wxDefaultPosition
, wxDefaultSize
,
1452 m_allowOthers
? 0 : wxCB_READONLY
);
1454 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1457 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1458 wxGridCellAttr
* attr
)
1460 // as we fill the entire client area, don't do anything here to minimize
1463 // TODO: It doesn't actually fill the client area since the height of a
1464 // combo always defaults to the standard... Until someone has time to
1465 // figure out the right rectangle to paint, just do it the normal way...
1466 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1469 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1471 wxASSERT_MSG(m_control
,
1472 wxT("The wxGridCellEditor must be Created first!"));
1474 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1476 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1478 // Don't immediately end if we get a kill focus event within BeginEdit
1480 evtHandler
->SetInSetFocus(true);
1482 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1486 Combo()->SetValue(m_startValue
);
1490 // find the right position, or default to the first if not found
1491 int pos
= Combo()->FindString(m_startValue
);
1492 if (pos
== wxNOT_FOUND
)
1494 Combo()->SetSelection(pos
);
1497 Combo()->SetInsertionPointEnd();
1498 Combo()->SetFocus();
1502 // When dropping down the menu, a kill focus event
1503 // happens after this point, so we can't reset the flag yet.
1504 #if !defined(__WXGTK20__)
1505 evtHandler
->SetInSetFocus(false);
1510 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1513 wxString value
= Combo()->GetValue();
1514 if ( value
== m_startValue
)
1517 grid
->GetTable()->SetValue(row
, col
, value
);
1522 void wxGridCellChoiceEditor::Reset()
1524 Combo()->SetValue(m_startValue
);
1525 Combo()->SetInsertionPointEnd();
1528 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1538 wxStringTokenizer
tk(params
, _T(','));
1539 while ( tk
.HasMoreTokens() )
1541 m_choices
.Add(tk
.GetNextToken());
1545 // return the value in the text control
1546 wxString
wxGridCellChoiceEditor::GetValue() const
1548 return Combo()->GetValue();
1551 #endif // wxUSE_COMBOBOX
1553 // ----------------------------------------------------------------------------
1554 // wxGridCellEditorEvtHandler
1555 // ----------------------------------------------------------------------------
1557 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1559 // Don't disable the cell if we're just starting to edit it
1564 m_grid
->DisableCellEditControl();
1569 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1571 switch ( event
.GetKeyCode() )
1575 m_grid
->DisableCellEditControl();
1579 m_grid
->GetEventHandler()->ProcessEvent( event
);
1583 case WXK_NUMPAD_ENTER
:
1584 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1585 m_editor
->HandleReturn(event
);
1594 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1596 int row
= m_grid
->GetGridCursorRow();
1597 int col
= m_grid
->GetGridCursorCol();
1598 wxRect rect
= m_grid
->CellToRect( row
, col
);
1600 m_grid
->GetGridWindow()->GetClientSize( &cw
, &ch
);
1602 // if cell width is smaller than grid client area, cell is wholly visible
1603 bool wholeCellVisible
= (rect
.GetWidth() < cw
);
1605 switch ( event
.GetKeyCode() )
1610 case WXK_NUMPAD_ENTER
:
1615 if ( wholeCellVisible
)
1617 // no special processing needed...
1622 // do special processing for partly visible cell...
1624 // get the widths of all cells previous to this one
1626 for ( int i
= 0; i
< col
; i
++ )
1628 colXPos
+= m_grid
->GetColSize(i
);
1631 int xUnit
= 1, yUnit
= 1;
1632 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1635 m_grid
->Scroll(colXPos
/ xUnit
- 1, m_grid
->GetScrollPos(wxVERTICAL
));
1639 m_grid
->Scroll(colXPos
/ xUnit
, m_grid
->GetScrollPos(wxVERTICAL
));
1646 if ( wholeCellVisible
)
1648 // no special processing needed...
1653 // do special processing for partly visible cell...
1656 wxString value
= m_grid
->GetCellValue(row
, col
);
1657 if ( wxEmptyString
!= value
)
1659 // get width of cell CONTENTS (text)
1661 wxFont font
= m_grid
->GetCellFont(row
, col
);
1662 m_grid
->GetTextExtent(value
, &textWidth
, &y
, NULL
, NULL
, &font
);
1664 // try to RIGHT align the text by scrolling
1665 int client_right
= m_grid
->GetGridWindow()->GetClientSize().GetWidth();
1667 // (m_grid->GetScrollLineX()*2) is a factor for not scrolling to far,
1668 // otherwise the last part of the cell content might be hidden below the scroll bar
1669 // FIXME: maybe there is a more suitable correction?
1670 textWidth
-= (client_right
- (m_grid
->GetScrollLineX() * 2));
1671 if ( textWidth
< 0 )
1677 // get the widths of all cells previous to this one
1679 for ( int i
= 0; i
< col
; i
++ )
1681 colXPos
+= m_grid
->GetColSize(i
);
1684 // and add the (modified) text width of the cell contents
1685 // as we'd like to see the last part of the cell contents
1686 colXPos
+= textWidth
;
1688 int xUnit
= 1, yUnit
= 1;
1689 m_grid
->GetScrollPixelsPerUnit(&xUnit
, &yUnit
);
1690 m_grid
->Scroll(colXPos
/xUnit
-1, m_grid
->GetScrollPos(wxVERTICAL
));
1700 // ----------------------------------------------------------------------------
1701 // wxGridCellWorker is an (almost) empty common base class for
1702 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1703 // ----------------------------------------------------------------------------
1705 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1710 wxGridCellWorker::~wxGridCellWorker()
1714 // ============================================================================
1716 // ============================================================================
1718 // ----------------------------------------------------------------------------
1719 // wxGridCellRenderer
1720 // ----------------------------------------------------------------------------
1722 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1723 wxGridCellAttr
& attr
,
1726 int WXUNUSED(row
), int WXUNUSED(col
),
1729 dc
.SetBackgroundMode( wxSOLID
);
1731 // grey out fields if the grid is disabled
1732 if ( grid
.IsEnabled() )
1736 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1740 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1745 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1748 dc
.SetPen( *wxTRANSPARENT_PEN
);
1749 dc
.DrawRectangle(rect
);
1752 // ----------------------------------------------------------------------------
1753 // wxGridCellStringRenderer
1754 // ----------------------------------------------------------------------------
1756 void wxGridCellStringRenderer::SetTextColoursAndFont(const wxGrid
& grid
,
1757 const wxGridCellAttr
& attr
,
1761 dc
.SetBackgroundMode( wxTRANSPARENT
);
1763 // TODO some special colours for attr.IsReadOnly() case?
1765 // different coloured text when the grid is disabled
1766 if ( grid
.IsEnabled() )
1770 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1771 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1775 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1776 dc
.SetTextForeground( attr
.GetTextColour() );
1781 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1782 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1785 dc
.SetFont( attr
.GetFont() );
1788 wxSize
wxGridCellStringRenderer::DoGetBestSize(const wxGridCellAttr
& attr
,
1790 const wxString
& text
)
1792 wxCoord x
= 0, y
= 0, max_x
= 0;
1793 dc
.SetFont(attr
.GetFont());
1794 wxStringTokenizer
tk(text
, _T('\n'));
1795 while ( tk
.HasMoreTokens() )
1797 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1798 max_x
= wxMax(max_x
, x
);
1801 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1803 return wxSize(max_x
, y
);
1806 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1807 wxGridCellAttr
& attr
,
1811 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1814 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1815 wxGridCellAttr
& attr
,
1817 const wxRect
& rectCell
,
1821 wxRect rect
= rectCell
;
1824 // erase only this cells background, overflow cells should have been erased
1825 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1828 attr
.GetAlignment(&hAlign
, &vAlign
);
1830 int overflowCols
= 0;
1832 if (attr
.GetOverflow())
1834 int cols
= grid
.GetNumberCols();
1835 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1836 int cell_rows
, cell_cols
;
1837 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <= 0
1838 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1840 int i
, c_cols
, c_rows
;
1841 for (i
= col
+cell_cols
; i
< cols
; i
++)
1843 bool is_empty
= true;
1844 for (int j
=row
; j
< row
+ cell_rows
; j
++)
1846 // check w/ anchor cell for multicell block
1847 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1850 if (!grid
.GetTable()->IsEmptyCell(j
+ c_rows
, i
))
1858 rect
.width
+= grid
.GetColSize(i
);
1865 if (rect
.width
>= best_width
)
1869 overflowCols
= i
- col
- cell_cols
+ 1;
1870 if (overflowCols
>= cols
)
1871 overflowCols
= cols
- 1;
1874 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1876 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1878 clip
.x
+= rectCell
.width
;
1879 // draw each overflow cell individually
1880 int col_end
= col
+cell_cols
+ overflowCols
;
1881 if (col_end
>= grid
.GetNumberCols())
1882 col_end
= grid
.GetNumberCols() - 1;
1883 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1885 clip
.width
= grid
.GetColSize(i
) - 1;
1886 dc
.DestroyClippingRegion();
1887 dc
.SetClippingRegion(clip
);
1889 SetTextColoursAndFont(grid
, attr
, dc
,
1890 grid
.IsInSelection(row
,i
));
1892 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1893 rect
, hAlign
, vAlign
);
1894 clip
.x
+= grid
.GetColSize(i
) - 1;
1900 dc
.DestroyClippingRegion();
1904 // now we only have to draw the text
1905 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1907 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1908 rect
, hAlign
, vAlign
);
1911 // ----------------------------------------------------------------------------
1912 // wxGridCellNumberRenderer
1913 // ----------------------------------------------------------------------------
1915 wxString
wxGridCellNumberRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1917 wxGridTableBase
*table
= grid
.GetTable();
1919 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1921 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1925 text
= table
->GetValue(row
, col
);
1931 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1932 wxGridCellAttr
& attr
,
1934 const wxRect
& rectCell
,
1938 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1940 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1942 // draw the text right aligned by default
1944 attr
.GetAlignment(&hAlign
, &vAlign
);
1945 hAlign
= wxALIGN_RIGHT
;
1947 wxRect rect
= rectCell
;
1950 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1953 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1954 wxGridCellAttr
& attr
,
1958 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1961 // ----------------------------------------------------------------------------
1962 // wxGridCellFloatRenderer
1963 // ----------------------------------------------------------------------------
1965 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1968 SetPrecision(precision
);
1971 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1973 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1974 renderer
->m_width
= m_width
;
1975 renderer
->m_precision
= m_precision
;
1976 renderer
->m_format
= m_format
;
1981 wxString
wxGridCellFloatRenderer::GetString(const wxGrid
& grid
, int row
, int col
)
1983 wxGridTableBase
*table
= grid
.GetTable();
1988 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1990 val
= table
->GetValueAsDouble(row
, col
);
1995 text
= table
->GetValue(row
, col
);
1996 hasDouble
= text
.ToDouble(&val
);
2003 if ( m_width
== -1 )
2005 if ( m_precision
== -1 )
2007 // default width/precision
2008 m_format
= _T("%f");
2012 m_format
.Printf(_T("%%.%df"), m_precision
);
2015 else if ( m_precision
== -1 )
2017 // default precision
2018 m_format
.Printf(_T("%%%d.f"), m_width
);
2022 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
2026 text
.Printf(m_format
, val
);
2029 //else: text already contains the string
2034 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
2035 wxGridCellAttr
& attr
,
2037 const wxRect
& rectCell
,
2041 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
2043 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
2045 // draw the text right aligned by default
2047 attr
.GetAlignment(&hAlign
, &vAlign
);
2048 hAlign
= wxALIGN_RIGHT
;
2050 wxRect rect
= rectCell
;
2053 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
2056 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
2057 wxGridCellAttr
& attr
,
2061 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
2064 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
2068 // reset to defaults
2074 wxString tmp
= params
.BeforeFirst(_T(','));
2078 if ( tmp
.ToLong(&width
) )
2080 SetWidth((int)width
);
2084 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
2088 tmp
= params
.AfterFirst(_T(','));
2092 if ( tmp
.ToLong(&precision
) )
2094 SetPrecision((int)precision
);
2098 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
2104 // ----------------------------------------------------------------------------
2105 // wxGridCellBoolRenderer
2106 // ----------------------------------------------------------------------------
2108 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
2110 // FIXME these checkbox size calculations are really ugly...
2112 // between checkmark and box
2113 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2115 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2116 wxGridCellAttr
& WXUNUSED(attr
),
2121 // compute it only once (no locks for MT safeness in GUI thread...)
2122 if ( !ms_sizeCheckMark
.x
)
2124 // get checkbox size
2125 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2126 wxSize size
= checkbox
->GetBestSize();
2127 wxCoord checkSize
= size
.y
+ 2 * wxGRID_CHECKMARK_MARGIN
;
2129 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2130 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2131 checkSize
-= size
.y
/ 2;
2136 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2139 return ms_sizeCheckMark
;
2142 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2143 wxGridCellAttr
& attr
,
2149 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2151 // draw a check mark in the centre (ignoring alignment - TODO)
2152 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2154 // don't draw outside the cell
2155 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2156 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2158 // and even leave (at least) 1 pixel margin
2159 size
.x
= size
.y
= minSize
- 2;
2162 // draw a border around checkmark
2164 attr
.GetAlignment(& hAlign
, &vAlign
);
2167 if (hAlign
== wxALIGN_CENTRE
)
2169 rectBorder
.x
= rect
.x
+ rect
.width
/ 2 - size
.x
/ 2;
2170 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2171 rectBorder
.width
= size
.x
;
2172 rectBorder
.height
= size
.y
;
2174 else if (hAlign
== wxALIGN_LEFT
)
2176 rectBorder
.x
= rect
.x
+ 2;
2177 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2178 rectBorder
.width
= size
.x
;
2179 rectBorder
.height
= size
.y
;
2181 else if (hAlign
== wxALIGN_RIGHT
)
2183 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2184 rectBorder
.y
= rect
.y
+ rect
.height
/ 2 - size
.y
/ 2;
2185 rectBorder
.width
= size
.x
;
2186 rectBorder
.height
= size
.y
;
2190 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2191 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2194 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2195 value
= !( !cellval
|| (cellval
== wxT("0")) );
2200 wxRect rectMark
= rectBorder
;
2203 // MSW DrawCheckMark() is weird (and should probably be changed...)
2204 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/ 2);
2208 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2211 dc
.SetTextForeground(attr
.GetTextColour());
2212 dc
.DrawCheckMark(rectMark
);
2215 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2216 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2217 dc
.DrawRectangle(rectBorder
);
2220 // ----------------------------------------------------------------------------
2222 // ----------------------------------------------------------------------------
2224 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2228 m_isReadOnly
= Unset
;
2233 m_attrkind
= wxGridCellAttr::Cell
;
2235 m_sizeRows
= m_sizeCols
= 1;
2236 m_overflow
= UnsetOverflow
;
2238 SetDefAttr(attrDefault
);
2241 wxGridCellAttr
*wxGridCellAttr::Clone() const
2243 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2245 if ( HasTextColour() )
2246 attr
->SetTextColour(GetTextColour());
2247 if ( HasBackgroundColour() )
2248 attr
->SetBackgroundColour(GetBackgroundColour());
2250 attr
->SetFont(GetFont());
2251 if ( HasAlignment() )
2252 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2254 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2258 attr
->SetRenderer(m_renderer
);
2259 m_renderer
->IncRef();
2263 attr
->SetEditor(m_editor
);
2268 attr
->SetReadOnly();
2270 attr
->SetKind( m_attrkind
);
2275 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2277 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2278 SetTextColour(mergefrom
->GetTextColour());
2279 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2280 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2281 if ( !HasFont() && mergefrom
->HasFont() )
2282 SetFont(mergefrom
->GetFont());
2283 if ( !HasAlignment() && mergefrom
->HasAlignment() )
2286 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2287 SetAlignment(hAlign
, vAlign
);
2289 if ( !HasSize() && mergefrom
->HasSize() )
2290 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2292 // Directly access member functions as GetRender/Editor don't just return
2293 // m_renderer/m_editor
2295 // Maybe add support for merge of Render and Editor?
2296 if (!HasRenderer() && mergefrom
->HasRenderer() )
2298 m_renderer
= mergefrom
->m_renderer
;
2299 m_renderer
->IncRef();
2301 if ( !HasEditor() && mergefrom
->HasEditor() )
2303 m_editor
= mergefrom
->m_editor
;
2306 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2307 SetReadOnly(mergefrom
->IsReadOnly());
2309 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2310 SetOverflow(mergefrom
->GetOverflow());
2312 SetDefAttr(mergefrom
->m_defGridAttr
);
2315 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2317 // The size of a cell is normally 1,1
2319 // If this cell is larger (2,2) then this is the top left cell
2320 // the other cells that will be covered (lower right cells) must be
2321 // set to negative or zero values such that
2322 // row + num_rows of the covered cell points to the larger cell (this cell)
2323 // same goes for the col + num_cols.
2325 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2327 wxASSERT_MSG( (!((num_rows
> 0) && (num_cols
<= 0)) ||
2328 !((num_rows
<= 0) && (num_cols
> 0)) ||
2329 !((num_rows
== 0) && (num_cols
== 0))),
2330 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2332 m_sizeRows
= num_rows
;
2333 m_sizeCols
= num_cols
;
2336 const wxColour
& wxGridCellAttr::GetTextColour() const
2338 if (HasTextColour())
2342 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2344 return m_defGridAttr
->GetTextColour();
2348 wxFAIL_MSG(wxT("Missing default cell attribute"));
2349 return wxNullColour
;
2353 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2355 if (HasBackgroundColour())
2359 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2361 return m_defGridAttr
->GetBackgroundColour();
2365 wxFAIL_MSG(wxT("Missing default cell attribute"));
2366 return wxNullColour
;
2370 const wxFont
& wxGridCellAttr::GetFont() const
2376 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2378 return m_defGridAttr
->GetFont();
2382 wxFAIL_MSG(wxT("Missing default cell attribute"));
2387 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2396 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2397 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2400 wxFAIL_MSG(wxT("Missing default cell attribute"));
2404 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2407 *num_rows
= m_sizeRows
;
2409 *num_cols
= m_sizeCols
;
2412 // GetRenderer and GetEditor use a slightly different decision path about
2413 // which attribute to use. If a non-default attr object has one then it is
2414 // used, otherwise the default editor or renderer is fetched from the grid and
2415 // used. It should be the default for the data type of the cell. If it is
2416 // NULL (because the table has a type that the grid does not have in its
2417 // registry,) then the grid's default editor or renderer is used.
2419 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2421 wxGridCellRenderer
*renderer
;
2423 if ( m_renderer
&& this != m_defGridAttr
)
2425 // use the cells renderer if it has one
2426 renderer
= m_renderer
;
2429 else // no non-default cell renderer
2431 // get default renderer for the data type
2434 // GetDefaultRendererForCell() will do IncRef() for us
2435 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2444 if (m_defGridAttr
&& this != m_defGridAttr
)
2446 // if we still don't have one then use the grid default
2447 // (no need for IncRef() here neither)
2448 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2450 else // default grid attr
2452 // use m_renderer which we had decided not to use initially
2453 renderer
= m_renderer
;
2460 // we're supposed to always find something
2461 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2466 // same as above, except for s/renderer/editor/g
2467 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2469 wxGridCellEditor
*editor
;
2471 if ( m_editor
&& this != m_defGridAttr
)
2473 // use the cells editor if it has one
2477 else // no non default cell editor
2479 // get default editor for the data type
2482 // GetDefaultEditorForCell() will do IncRef() for us
2483 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2492 if ( m_defGridAttr
&& this != m_defGridAttr
)
2494 // if we still don't have one then use the grid default
2495 // (no need for IncRef() here neither)
2496 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2498 else // default grid attr
2500 // use m_editor which we had decided not to use initially
2508 // we're supposed to always find something
2509 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2514 // ----------------------------------------------------------------------------
2515 // wxGridCellAttrData
2516 // ----------------------------------------------------------------------------
2518 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2520 int n
= FindIndex(row
, col
);
2521 if ( n
== wxNOT_FOUND
)
2523 // add the attribute
2524 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2528 // free the old attribute
2529 m_attrs
[(size_t)n
].attr
->DecRef();
2533 // change the attribute
2534 m_attrs
[(size_t)n
].attr
= attr
;
2538 // remove this attribute
2539 m_attrs
.RemoveAt((size_t)n
);
2544 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2546 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2548 int n
= FindIndex(row
, col
);
2549 if ( n
!= wxNOT_FOUND
)
2551 attr
= m_attrs
[(size_t)n
].attr
;
2558 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2560 size_t count
= m_attrs
.GetCount();
2561 for ( size_t n
= 0; n
< count
; n
++ )
2563 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2564 wxCoord row
= coords
.GetRow();
2565 if ((size_t)row
>= pos
)
2569 // If rows inserted, include row counter where necessary
2570 coords
.SetRow(row
+ numRows
);
2572 else if (numRows
< 0)
2574 // If rows deleted ...
2575 if ((size_t)row
>= pos
- numRows
)
2577 // ...either decrement row counter (if row still exists)...
2578 coords
.SetRow(row
+ numRows
);
2582 // ...or remove the attribute
2583 // No need to DecRef the attribute itself since this is
2584 // done be wxGridCellWithAttr's destructor!
2585 m_attrs
.RemoveAt(n
);
2594 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2596 size_t count
= m_attrs
.GetCount();
2597 for ( size_t n
= 0; n
< count
; n
++ )
2599 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2600 wxCoord col
= coords
.GetCol();
2601 if ( (size_t)col
>= pos
)
2605 // If rows inserted, include row counter where necessary
2606 coords
.SetCol(col
+ numCols
);
2608 else if (numCols
< 0)
2610 // If rows deleted ...
2611 if ((size_t)col
>= pos
- numCols
)
2613 // ...either decrement row counter (if row still exists)...
2614 coords
.SetCol(col
+ numCols
);
2618 // ...or remove the attribute
2619 // No need to DecRef the attribute itself since this is
2620 // done be wxGridCellWithAttr's destructor!
2621 m_attrs
.RemoveAt(n
);
2630 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2632 size_t count
= m_attrs
.GetCount();
2633 for ( size_t n
= 0; n
< count
; n
++ )
2635 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2636 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2645 // ----------------------------------------------------------------------------
2646 // wxGridRowOrColAttrData
2647 // ----------------------------------------------------------------------------
2649 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2651 size_t count
= m_attrs
.Count();
2652 for ( size_t n
= 0; n
< count
; n
++ )
2654 m_attrs
[n
]->DecRef();
2658 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2660 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2662 int n
= m_rowsOrCols
.Index(rowOrCol
);
2663 if ( n
!= wxNOT_FOUND
)
2665 attr
= m_attrs
[(size_t)n
];
2672 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2674 int i
= m_rowsOrCols
.Index(rowOrCol
);
2675 if ( i
== wxNOT_FOUND
)
2677 // add the attribute
2678 m_rowsOrCols
.Add(rowOrCol
);
2683 size_t n
= (size_t)i
;
2686 // change the attribute
2687 m_attrs
[n
]->DecRef();
2692 // remove this attribute
2693 m_attrs
[n
]->DecRef();
2694 m_rowsOrCols
.RemoveAt(n
);
2695 m_attrs
.RemoveAt(n
);
2700 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2702 size_t count
= m_attrs
.GetCount();
2703 for ( size_t n
= 0; n
< count
; n
++ )
2705 int & rowOrCol
= m_rowsOrCols
[n
];
2706 if ( (size_t)rowOrCol
>= pos
)
2708 if ( numRowsOrCols
> 0 )
2710 // If rows inserted, include row counter where necessary
2711 rowOrCol
+= numRowsOrCols
;
2713 else if ( numRowsOrCols
< 0)
2715 // If rows deleted, either decrement row counter (if row still exists)
2716 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2717 rowOrCol
+= numRowsOrCols
;
2720 m_rowsOrCols
.RemoveAt(n
);
2721 m_attrs
[n
]->DecRef();
2722 m_attrs
.RemoveAt(n
);
2731 // ----------------------------------------------------------------------------
2732 // wxGridCellAttrProvider
2733 // ----------------------------------------------------------------------------
2735 wxGridCellAttrProvider::wxGridCellAttrProvider()
2737 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2740 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2745 void wxGridCellAttrProvider::InitData()
2747 m_data
= new wxGridCellAttrProviderData
;
2750 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2751 wxGridCellAttr::wxAttrKind kind
) const
2753 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2758 case (wxGridCellAttr::Any
):
2759 // Get cached merge attributes.
2760 // Currently not used as no cache implemented as not mutable
2761 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2764 // Basically implement old version.
2765 // Also check merge cache, so we don't have to re-merge every time..
2766 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2767 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2768 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2770 if ((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
))
2772 // Two or more are non NULL
2773 attr
= new wxGridCellAttr
;
2774 attr
->SetKind(wxGridCellAttr::Merged
);
2776 // Order is important..
2779 attr
->MergeWith(attrcell
);
2784 attr
->MergeWith(attrcol
);
2789 attr
->MergeWith(attrrow
);
2793 // store merge attr if cache implemented
2795 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2799 // one or none is non null return it or null.
2818 case (wxGridCellAttr::Cell
):
2819 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2822 case (wxGridCellAttr::Col
):
2823 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2826 case (wxGridCellAttr::Row
):
2827 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2832 // (wxGridCellAttr::Default):
2833 // (wxGridCellAttr::Merged):
2841 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2847 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2850 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2855 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2858 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2863 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2866 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2870 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2872 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2876 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2880 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2882 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2886 // ----------------------------------------------------------------------------
2887 // wxGridTypeRegistry
2888 // ----------------------------------------------------------------------------
2890 wxGridTypeRegistry::~wxGridTypeRegistry()
2892 size_t count
= m_typeinfo
.Count();
2893 for ( size_t i
= 0; i
< count
; i
++ )
2894 delete m_typeinfo
[i
];
2897 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2898 wxGridCellRenderer
* renderer
,
2899 wxGridCellEditor
* editor
)
2901 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2903 // is it already registered?
2904 int loc
= FindRegisteredDataType(typeName
);
2905 if ( loc
!= wxNOT_FOUND
)
2907 delete m_typeinfo
[loc
];
2908 m_typeinfo
[loc
] = info
;
2912 m_typeinfo
.Add(info
);
2916 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2918 size_t count
= m_typeinfo
.GetCount();
2919 for ( size_t i
= 0; i
< count
; i
++ )
2921 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2930 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2932 int index
= FindRegisteredDataType(typeName
);
2933 if ( index
== wxNOT_FOUND
)
2935 // check whether this is one of the standard ones, in which case
2936 // register it "on the fly"
2938 if ( typeName
== wxGRID_VALUE_STRING
)
2940 RegisterDataType(wxGRID_VALUE_STRING
,
2941 new wxGridCellStringRenderer
,
2942 new wxGridCellTextEditor
);
2945 #endif // wxUSE_TEXTCTRL
2947 if ( typeName
== wxGRID_VALUE_BOOL
)
2949 RegisterDataType(wxGRID_VALUE_BOOL
,
2950 new wxGridCellBoolRenderer
,
2951 new wxGridCellBoolEditor
);
2954 #endif // wxUSE_CHECKBOX
2956 if ( typeName
== wxGRID_VALUE_NUMBER
)
2958 RegisterDataType(wxGRID_VALUE_NUMBER
,
2959 new wxGridCellNumberRenderer
,
2960 new wxGridCellNumberEditor
);
2962 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2964 RegisterDataType(wxGRID_VALUE_FLOAT
,
2965 new wxGridCellFloatRenderer
,
2966 new wxGridCellFloatEditor
);
2969 #endif // wxUSE_TEXTCTRL
2971 if ( typeName
== wxGRID_VALUE_CHOICE
)
2973 RegisterDataType(wxGRID_VALUE_CHOICE
,
2974 new wxGridCellStringRenderer
,
2975 new wxGridCellChoiceEditor
);
2978 #endif // wxUSE_COMBOBOX
2983 // we get here only if just added the entry for this type, so return
2985 index
= m_typeinfo
.GetCount() - 1;
2991 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2993 int index
= FindDataType(typeName
);
2994 if ( index
== wxNOT_FOUND
)
2996 // the first part of the typename is the "real" type, anything after ':'
2997 // are the parameters for the renderer
2998 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2999 if ( index
== wxNOT_FOUND
)
3004 wxGridCellRenderer
*renderer
= GetRenderer(index
);
3005 wxGridCellRenderer
*rendererOld
= renderer
;
3006 renderer
= renderer
->Clone();
3007 rendererOld
->DecRef();
3009 wxGridCellEditor
*editor
= GetEditor(index
);
3010 wxGridCellEditor
*editorOld
= editor
;
3011 editor
= editor
->Clone();
3012 editorOld
->DecRef();
3014 // do it even if there are no parameters to reset them to defaults
3015 wxString params
= typeName
.AfterFirst(_T(':'));
3016 renderer
->SetParameters(params
);
3017 editor
->SetParameters(params
);
3019 // register the new typename
3020 RegisterDataType(typeName
, renderer
, editor
);
3022 // we just registered it, it's the last one
3023 index
= m_typeinfo
.GetCount() - 1;
3029 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
3031 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
3038 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
3040 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
3047 // ----------------------------------------------------------------------------
3049 // ----------------------------------------------------------------------------
3051 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
3053 wxGridTableBase::wxGridTableBase()
3055 m_view
= (wxGrid
*) NULL
;
3056 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
3059 wxGridTableBase::~wxGridTableBase()
3061 delete m_attrProvider
;
3064 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
3066 delete m_attrProvider
;
3067 m_attrProvider
= attrProvider
;
3070 bool wxGridTableBase::CanHaveAttributes()
3072 if ( ! GetAttrProvider() )
3074 // use the default attr provider by default
3075 SetAttrProvider(new wxGridCellAttrProvider
);
3081 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
3083 if ( m_attrProvider
)
3084 return m_attrProvider
->GetAttr(row
, col
, kind
);
3086 return (wxGridCellAttr
*)NULL
;
3089 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
3091 if ( m_attrProvider
)
3093 attr
->SetKind(wxGridCellAttr::Cell
);
3094 m_attrProvider
->SetAttr(attr
, row
, col
);
3098 // as we take ownership of the pointer and don't store it, we must
3104 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
3106 if ( m_attrProvider
)
3108 attr
->SetKind(wxGridCellAttr::Row
);
3109 m_attrProvider
->SetRowAttr(attr
, row
);
3113 // as we take ownership of the pointer and don't store it, we must
3119 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
3121 if ( m_attrProvider
)
3123 attr
->SetKind(wxGridCellAttr::Col
);
3124 m_attrProvider
->SetColAttr(attr
, col
);
3128 // as we take ownership of the pointer and don't store it, we must
3134 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
3135 size_t WXUNUSED(numRows
) )
3137 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3142 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3144 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3149 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3150 size_t WXUNUSED(numRows
) )
3152 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3157 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3158 size_t WXUNUSED(numCols
) )
3160 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3165 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3167 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3172 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3173 size_t WXUNUSED(numCols
) )
3175 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3180 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3184 // RD: Starting the rows at zero confuses users,
3185 // no matter how much it makes sense to us geeks.
3191 wxString
wxGridTableBase::GetColLabelValue( int col
)
3193 // default col labels are:
3194 // cols 0 to 25 : A-Z
3195 // cols 26 to 675 : AA-ZZ
3200 for ( n
= 1; ; n
++ )
3202 s
+= (wxChar
) (_T('A') + (wxChar
)(col
% 26));
3208 // reverse the string...
3210 for ( i
= 0; i
< n
; i
++ )
3218 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3220 return wxGRID_VALUE_STRING
;
3223 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3224 const wxString
& typeName
)
3226 return typeName
== wxGRID_VALUE_STRING
;
3229 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3231 return CanGetValueAs(row
, col
, typeName
);
3234 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3239 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3244 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3249 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3250 long WXUNUSED(value
) )
3254 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3255 double WXUNUSED(value
) )
3259 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3260 bool WXUNUSED(value
) )
3264 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3265 const wxString
& WXUNUSED(typeName
) )
3270 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3271 const wxString
& WXUNUSED(typeName
),
3272 void* WXUNUSED(value
) )
3276 //////////////////////////////////////////////////////////////////////
3278 // Message class for the grid table to send requests and notifications
3282 wxGridTableMessage::wxGridTableMessage()
3284 m_table
= (wxGridTableBase
*) NULL
;
3290 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3291 int commandInt1
, int commandInt2
)
3295 m_comInt1
= commandInt1
;
3296 m_comInt2
= commandInt2
;
3299 //////////////////////////////////////////////////////////////////////
3301 // A basic grid table for string data. An object of this class will
3302 // created by wxGrid if you don't specify an alternative table class.
3305 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3307 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3309 wxGridStringTable::wxGridStringTable()
3314 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3317 m_data
.Alloc( numRows
);
3320 sa
.Alloc( numCols
);
3321 sa
.Add( wxEmptyString
, numCols
);
3323 m_data
.Add( sa
, numRows
);
3326 wxGridStringTable::~wxGridStringTable()
3330 int wxGridStringTable::GetNumberRows()
3332 return m_data
.GetCount();
3335 int wxGridStringTable::GetNumberCols()
3337 if ( m_data
.GetCount() > 0 )
3338 return m_data
[0].GetCount();
3343 wxString
wxGridStringTable::GetValue( int row
, int col
)
3345 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3347 _T("invalid row or column index in wxGridStringTable") );
3349 return m_data
[row
][col
];
3352 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3354 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3355 _T("invalid row or column index in wxGridStringTable") );
3357 m_data
[row
][col
] = value
;
3360 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3362 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3364 _T("invalid row or column index in wxGridStringTable") );
3366 return (m_data
[row
][col
] == wxEmptyString
);
3369 void wxGridStringTable::Clear()
3372 int numRows
, numCols
;
3374 numRows
= m_data
.GetCount();
3377 numCols
= m_data
[0].GetCount();
3379 for ( row
= 0; row
< numRows
; row
++ )
3381 for ( col
= 0; col
< numCols
; col
++ )
3383 m_data
[row
][col
] = wxEmptyString
;
3389 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3391 size_t curNumRows
= m_data
.GetCount();
3392 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3393 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3395 if ( pos
>= curNumRows
)
3397 return AppendRows( numRows
);
3401 sa
.Alloc( curNumCols
);
3402 sa
.Add( wxEmptyString
, curNumCols
);
3403 m_data
.Insert( sa
, pos
, numRows
);
3407 wxGridTableMessage
msg( this,
3408 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3412 GetView()->ProcessTableMessage( msg
);
3418 bool wxGridStringTable::AppendRows( size_t numRows
)
3420 size_t curNumRows
= m_data
.GetCount();
3421 size_t curNumCols
= ( curNumRows
> 0
3422 ? m_data
[0].GetCount()
3423 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3426 if ( curNumCols
> 0 )
3428 sa
.Alloc( curNumCols
);
3429 sa
.Add( wxEmptyString
, curNumCols
);
3432 m_data
.Add( sa
, numRows
);
3436 wxGridTableMessage
msg( this,
3437 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3440 GetView()->ProcessTableMessage( msg
);
3446 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3448 size_t curNumRows
= m_data
.GetCount();
3450 if ( pos
>= curNumRows
)
3452 wxFAIL_MSG( wxString::Format
3454 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3456 (unsigned long)numRows
,
3457 (unsigned long)curNumRows
3463 if ( numRows
> curNumRows
- pos
)
3465 numRows
= curNumRows
- pos
;
3468 if ( numRows
>= curNumRows
)
3474 m_data
.RemoveAt( pos
, numRows
);
3479 wxGridTableMessage
msg( this,
3480 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3484 GetView()->ProcessTableMessage( msg
);
3490 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3494 size_t curNumRows
= m_data
.GetCount();
3495 size_t curNumCols
= ( curNumRows
> 0
3496 ? m_data
[0].GetCount()
3497 : ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3499 if ( pos
>= curNumCols
)
3501 return AppendCols( numCols
);
3504 for ( row
= 0; row
< curNumRows
; row
++ )
3506 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3508 m_data
[row
].Insert( wxEmptyString
, col
);
3514 wxGridTableMessage
msg( this,
3515 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3519 GetView()->ProcessTableMessage( msg
);
3525 bool wxGridStringTable::AppendCols( size_t numCols
)
3529 size_t curNumRows
= m_data
.GetCount();
3534 // TODO: something better than this ?
3536 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3541 for ( row
= 0; row
< curNumRows
; row
++ )
3543 m_data
[row
].Add( wxEmptyString
, numCols
);
3548 wxGridTableMessage
msg( this,
3549 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3552 GetView()->ProcessTableMessage( msg
);
3558 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3562 size_t curNumRows
= m_data
.GetCount();
3563 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3564 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3566 if ( pos
>= curNumCols
)
3568 wxFAIL_MSG( wxString::Format
3570 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3572 (unsigned long)numCols
,
3573 (unsigned long)curNumCols
3578 if ( numCols
> curNumCols
- pos
)
3580 numCols
= curNumCols
- pos
;
3583 for ( row
= 0; row
< curNumRows
; row
++ )
3585 if ( numCols
>= curNumCols
)
3587 m_data
[row
].Clear();
3591 m_data
[row
].RemoveAt( pos
, numCols
);
3597 wxGridTableMessage
msg( this,
3598 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3602 GetView()->ProcessTableMessage( msg
);
3608 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3610 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3612 // using default label
3614 return wxGridTableBase::GetRowLabelValue( row
);
3618 return m_rowLabels
[row
];
3622 wxString
wxGridStringTable::GetColLabelValue( int col
)
3624 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3626 // using default label
3628 return wxGridTableBase::GetColLabelValue( col
);
3632 return m_colLabels
[col
];
3636 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3638 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3640 int n
= m_rowLabels
.GetCount();
3643 for ( i
= n
; i
<= row
; i
++ )
3645 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3649 m_rowLabels
[row
] = value
;
3652 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3654 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3656 int n
= m_colLabels
.GetCount();
3659 for ( i
= n
; i
<= col
; i
++ )
3661 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3665 m_colLabels
[col
] = value
;
3669 //////////////////////////////////////////////////////////////////////
3670 //////////////////////////////////////////////////////////////////////
3672 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3674 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3675 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3676 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3677 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3678 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3679 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3680 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3683 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3685 const wxPoint
&pos
, const wxSize
&size
)
3686 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3691 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3695 // NO - don't do this because it will set both the x and y origin
3696 // coords to match the parent scrolled window and we just want to
3697 // set the y coord - MB
3699 // m_owner->PrepareDC( dc );
3702 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3703 dc
.SetDeviceOrigin( 0, -y
);
3705 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3706 m_owner
->DrawRowLabels( dc
, rows
);
3709 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3711 m_owner
->ProcessRowLabelMouseEvent( event
);
3714 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3716 m_owner
->GetEventHandler()->ProcessEvent(event
);
3719 // This seems to be required for wxMotif otherwise the mouse
3720 // cursor must be in the cell edit control to get key events
3722 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3724 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3728 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3730 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3734 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3736 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3740 //////////////////////////////////////////////////////////////////////
3742 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3744 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3745 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3746 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3747 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3748 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3749 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3750 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3753 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3755 const wxPoint
&pos
, const wxSize
&size
)
3756 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3761 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3765 // NO - don't do this because it will set both the x and y origin
3766 // coords to match the parent scrolled window and we just want to
3767 // set the x coord - MB
3769 // m_owner->PrepareDC( dc );
3772 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3773 dc
.SetDeviceOrigin( -x
, 0 );
3775 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3776 m_owner
->DrawColLabels( dc
, cols
);
3779 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3781 m_owner
->ProcessColLabelMouseEvent( event
);
3784 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3786 m_owner
->GetEventHandler()->ProcessEvent(event
);
3789 // This seems to be required for wxMotif otherwise the mouse
3790 // cursor must be in the cell edit control to get key events
3792 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3794 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3798 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3800 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3804 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3806 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3810 //////////////////////////////////////////////////////////////////////
3812 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3814 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3815 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3816 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3817 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3818 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3819 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3820 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3823 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3825 const wxPoint
&pos
, const wxSize
&size
)
3826 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3831 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3835 int client_height
= 0;
3836 int client_width
= 0;
3837 GetClientSize( &client_width
, &client_height
);
3839 // VZ: any reason for this ifdef? (FIXME)
3844 rect
.SetWidth( client_width
- 2 );
3845 rect
.SetHeight( client_height
- 2 );
3847 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3849 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3850 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3851 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3852 dc
.DrawLine( 0, 0, client_width
, 0 );
3853 dc
.DrawLine( 0, 0, 0, client_height
);
3855 dc
.SetPen( *wxWHITE_PEN
);
3856 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3857 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3858 #endif // __WXGTK__/!__WXGTK__
3861 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3863 m_owner
->ProcessCornerLabelMouseEvent( event
);
3866 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3868 m_owner
->GetEventHandler()->ProcessEvent(event
);
3871 // This seems to be required for wxMotif otherwise the mouse
3872 // cursor must be in the cell edit control to get key events
3874 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3876 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3880 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3882 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3886 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3888 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3892 //////////////////////////////////////////////////////////////////////
3894 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3896 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3897 EVT_PAINT( wxGridWindow::OnPaint
)
3898 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3899 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3900 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3901 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3902 EVT_CHAR ( wxGridWindow::OnChar
)
3903 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3904 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3905 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3908 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3909 wxGridRowLabelWindow
*rowLblWin
,
3910 wxGridColLabelWindow
*colLblWin
,
3913 const wxSize
&size
)
3914 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3915 wxT("grid window") )
3918 m_rowLabelWin
= rowLblWin
;
3919 m_colLabelWin
= colLblWin
;
3922 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3924 wxPaintDC
dc( this );
3925 m_owner
->PrepareDC( dc
);
3926 wxRegion reg
= GetUpdateRegion();
3927 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3928 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3930 #if WXGRID_DRAW_LINES
3931 m_owner
->DrawAllGridLines( dc
, reg
);
3934 m_owner
->DrawGridSpace( dc
);
3935 m_owner
->DrawHighlight( dc
, DirtyCells
);
3938 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3940 wxWindow::ScrollWindow( dx
, dy
, rect
);
3941 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3942 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3945 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3947 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3950 m_owner
->ProcessGridCellMouseEvent( event
);
3953 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3955 m_owner
->GetEventHandler()->ProcessEvent(event
);
3958 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3959 // cursor must be in the cell edit control to get key events
3961 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3963 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3967 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3969 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3973 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3975 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3979 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3983 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3985 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3989 //////////////////////////////////////////////////////////////////////
3991 // Internal Helper function for computing row or column from some
3992 // (unscrolled) coordinate value, using either
3993 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3994 // of m_rowBottoms/m_ColRights to speed up the search!
3996 // Internal helper macros for simpler use of that function
3998 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3999 const wxArrayInt
& BorderArray
, int nMax
,
4002 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
4003 m_minAcceptableColWidth, \
4004 m_colRights, m_numCols, true)
4005 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
4006 m_minAcceptableRowHeight, \
4007 m_rowBottoms, m_numRows, true)
4008 /////////////////////////////////////////////////////////////////////
4010 #if wxUSE_EXTENDED_RTTI
4011 WX_DEFINE_FLAGS( wxGridStyle
)
4013 wxBEGIN_FLAGS( wxGridStyle
)
4014 // new style border flags, we put them first to
4015 // use them for streaming out
4016 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
4017 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
4018 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
4019 wxFLAGS_MEMBER(wxBORDER_RAISED
)
4020 wxFLAGS_MEMBER(wxBORDER_STATIC
)
4021 wxFLAGS_MEMBER(wxBORDER_NONE
)
4023 // old style border flags
4024 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
4025 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
4026 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
4027 wxFLAGS_MEMBER(wxRAISED_BORDER
)
4028 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
4029 wxFLAGS_MEMBER(wxBORDER
)
4031 // standard window styles
4032 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
4033 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
4034 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
4035 wxFLAGS_MEMBER(wxWANTS_CHARS
)
4036 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
4037 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
4038 wxFLAGS_MEMBER(wxVSCROLL
)
4039 wxFLAGS_MEMBER(wxHSCROLL
)
4041 wxEND_FLAGS( wxGridStyle
)
4043 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
4045 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
4046 wxHIDE_PROPERTY( Children
)
4047 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
4048 wxEND_PROPERTIES_TABLE()
4050 wxBEGIN_HANDLERS_TABLE(wxGrid
)
4051 wxEND_HANDLERS_TABLE()
4053 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
4056 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
4059 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
4062 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
4063 EVT_PAINT( wxGrid::OnPaint
)
4064 EVT_SIZE( wxGrid::OnSize
)
4065 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
4066 EVT_KEY_UP( wxGrid::OnKeyUp
)
4067 EVT_CHAR ( wxGrid::OnChar
)
4068 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
4073 // in order to make sure that a size event is not
4074 // trigerred in a unfinished state
4075 m_cornerLabelWin
= NULL
;
4076 m_rowLabelWin
= NULL
;
4077 m_colLabelWin
= NULL
;
4081 wxGrid::wxGrid( wxWindow
*parent
,
4086 const wxString
& name
)
4087 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
4088 m_colMinWidths(GRID_HASH_SIZE
),
4089 m_rowMinHeights(GRID_HASH_SIZE
)
4092 SetBestFittingSize(size
);
4095 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
4096 const wxPoint
& pos
, const wxSize
& size
,
4097 long style
, const wxString
& name
)
4099 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
4100 style
| wxWANTS_CHARS
, name
))
4103 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4104 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
);
4107 SetBestFittingSize(size
);
4114 // Must do this or ~wxScrollHelper will pop the wrong event handler
4115 SetTargetWindow(this);
4117 wxSafeDecRef(m_defaultCellAttr
);
4119 #ifdef DEBUG_ATTR_CACHE
4120 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
4121 wxPrintf(_T("wxGrid attribute cache statistics: "
4122 "total: %u, hits: %u (%u%%)\n"),
4123 total
, gs_nAttrCacheHits
,
4124 total
? (gs_nAttrCacheHits
*100) / total
: 0);
4130 delete m_typeRegistry
;
4135 // ----- internal init and update functions
4138 // NOTE: If using the default visual attributes works everywhere then this can
4139 // be removed as well as the #else cases below.
4140 #define _USE_VISATTR 0
4143 #include "wx/listbox.h"
4146 void wxGrid::Create()
4148 m_created
= false; // set to true by CreateGrid
4150 m_table
= (wxGridTableBase
*) NULL
;
4153 m_cellEditCtrlEnabled
= false;
4155 m_defaultCellAttr
= new wxGridCellAttr();
4157 // Set default cell attributes
4158 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4159 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4160 m_defaultCellAttr
->SetFont(GetFont());
4161 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4162 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4163 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4166 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4167 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4169 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4170 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4173 m_defaultCellAttr
->SetTextColour(
4174 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4175 m_defaultCellAttr
->SetBackgroundColour(
4176 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4181 m_currentCellCoords
= wxGridNoCellCoords
;
4183 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4184 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4186 // create the type registry
4187 m_typeRegistry
= new wxGridTypeRegistry
;
4190 // subwindow components that make up the wxGrid
4191 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4196 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4201 m_colLabelWin
= new wxGridColLabelWindow( this,
4206 m_gridWin
= new wxGridWindow( this,
4213 SetTargetWindow( m_gridWin
);
4216 wxColour gfg
= gva
.colFg
;
4217 wxColour gbg
= gva
.colBg
;
4218 wxColour lfg
= lva
.colFg
;
4219 wxColour lbg
= lva
.colBg
;
4221 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4222 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4223 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4224 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4227 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4228 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4229 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4230 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4231 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4232 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4234 m_gridWin
->SetOwnForegroundColour(gfg
);
4235 m_gridWin
->SetOwnBackgroundColour(gbg
);
4240 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4241 wxGrid::wxGridSelectionModes selmode
)
4243 wxCHECK_MSG( !m_created
,
4245 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4247 m_numRows
= numRows
;
4248 m_numCols
= numCols
;
4250 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4251 m_table
->SetView( this );
4253 m_selection
= new wxGridSelection( this, selmode
);
4262 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4264 wxCHECK_RET( m_created
,
4265 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4267 m_selection
->SetSelectionMode( selmode
);
4270 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4272 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4273 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4275 return m_selection
->GetSelectionMode();
4278 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4279 wxGrid::wxGridSelectionModes selmode
)
4283 // stop all processing
4288 wxGridTableBase
*t
=m_table
;
4303 m_numRows
= table
->GetNumberRows();
4304 m_numCols
= table
->GetNumberCols();
4307 m_table
->SetView( this );
4308 m_ownTable
= takeOwnership
;
4309 m_selection
= new wxGridSelection( this, selmode
);
4321 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4322 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4324 if ( m_rowLabelWin
)
4326 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4330 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4333 m_labelTextColour
= wxColour( _T("BLACK") );
4336 m_attrCache
.row
= -1;
4337 m_attrCache
.col
= -1;
4338 m_attrCache
.attr
= NULL
;
4340 // TODO: something better than this ?
4342 m_labelFont
= this->GetFont();
4343 m_labelFont
.SetWeight( wxBOLD
);
4345 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4346 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4348 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4349 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4350 m_colLabelTextOrientation
= wxHORIZONTAL
;
4352 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4353 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4355 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4356 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4358 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4359 m_defaultRowHeight
+= 8;
4361 m_defaultRowHeight
+= 4;
4364 m_gridLineColour
= wxColour( 192,192,192 );
4365 m_gridLinesEnabled
= true;
4366 m_cellHighlightColour
= *wxBLACK
;
4367 m_cellHighlightPenWidth
= 2;
4368 m_cellHighlightROPenWidth
= 1;
4370 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4371 m_winCapture
= (wxWindow
*)NULL
;
4372 m_canDragRowSize
= true;
4373 m_canDragColSize
= true;
4374 m_canDragGridSize
= true;
4375 m_canDragCell
= false;
4377 m_dragRowOrCol
= -1;
4378 m_isDragging
= false;
4379 m_startDragPos
= wxDefaultPosition
;
4381 m_waitForSlowClick
= false;
4383 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4384 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4386 m_currentCellCoords
= wxGridNoCellCoords
;
4388 m_selectingTopLeft
= wxGridNoCellCoords
;
4389 m_selectingBottomRight
= wxGridNoCellCoords
;
4390 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4391 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4393 m_editable
= true; // default for whole grid
4395 m_inOnKeyDown
= false;
4401 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4402 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4405 // ----------------------------------------------------------------------------
4406 // the idea is to call these functions only when necessary because they create
4407 // quite big arrays which eat memory mostly unnecessary - in particular, if
4408 // default widths/heights are used for all rows/columns, we may not use these
4411 // with some extra code, it should be possible to only store the
4412 // widths/heights different from default ones but this will be done later...
4413 // ----------------------------------------------------------------------------
4415 void wxGrid::InitRowHeights()
4417 m_rowHeights
.Empty();
4418 m_rowBottoms
.Empty();
4420 m_rowHeights
.Alloc( m_numRows
);
4421 m_rowBottoms
.Alloc( m_numRows
);
4425 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4427 for ( int i
= 0; i
< m_numRows
; i
++ )
4429 rowBottom
+= m_defaultRowHeight
;
4430 m_rowBottoms
.Add( rowBottom
);
4434 void wxGrid::InitColWidths()
4436 m_colWidths
.Empty();
4437 m_colRights
.Empty();
4439 m_colWidths
.Alloc( m_numCols
);
4440 m_colRights
.Alloc( m_numCols
);
4443 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4445 for ( int i
= 0; i
< m_numCols
; i
++ )
4447 colRight
+= m_defaultColWidth
;
4448 m_colRights
.Add( colRight
);
4452 int wxGrid::GetColWidth(int col
) const
4454 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4457 int wxGrid::GetColLeft(int col
) const
4459 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4460 : m_colRights
[col
] - m_colWidths
[col
];
4463 int wxGrid::GetColRight(int col
) const
4465 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4469 int wxGrid::GetRowHeight(int row
) const
4471 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4474 int wxGrid::GetRowTop(int row
) const
4476 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4477 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4480 int wxGrid::GetRowBottom(int row
) const
4482 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4483 : m_rowBottoms
[row
];
4486 void wxGrid::CalcDimensions()
4489 GetClientSize( &cw
, &ch
);
4491 if ( m_rowLabelWin
->IsShown() )
4492 cw
-= m_rowLabelWidth
;
4493 if ( m_colLabelWin
->IsShown() )
4494 ch
-= m_colLabelHeight
;
4497 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4498 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4500 // take into account editor if shown
4501 if ( IsCellEditControlShown() )
4504 int r
= m_currentCellCoords
.GetRow();
4505 int c
= m_currentCellCoords
.GetCol();
4506 int x
= GetColLeft(c
);
4507 int y
= GetRowTop(r
);
4509 // how big is the editor
4510 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4511 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4512 editor
->GetControl()->GetSize(&w2
, &h2
);
4523 // preserve (more or less) the previous position
4525 GetViewStart( &x
, &y
);
4527 // ensure the position is valid for the new scroll ranges
4529 x
= wxMax( w
- 1, 0 );
4531 y
= wxMax( h
- 1, 0 );
4533 // do set scrollbar parameters
4534 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4535 GetScrollX(w
), GetScrollY(h
), x
, y
,
4536 GetBatchCount() != 0);
4538 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4539 // still must reposition the children
4544 void wxGrid::CalcWindowSizes()
4546 // escape if the window is has not been fully created yet
4548 if ( m_cornerLabelWin
== NULL
)
4552 GetClientSize( &cw
, &ch
);
4554 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4555 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4557 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4558 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4560 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4561 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4563 if ( m_gridWin
&& m_gridWin
->IsShown() )
4564 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4568 // this is called when the grid table sends a message to say that it
4569 // has been redimensioned
4571 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4574 bool result
= false;
4576 // Clear the attribute cache as the attribute might refer to a different
4577 // cell than stored in the cache after adding/removing rows/columns.
4580 // By the same reasoning, the editor should be dismissed if columns are
4581 // added or removed. And for consistency, it should IMHO always be
4582 // removed, not only if the cell "underneath" it actually changes.
4583 // For now, I intentionally do not save the editor's content as the
4584 // cell it might want to save that stuff to might no longer exist.
4585 HideCellEditControl();
4588 // if we were using the default widths/heights so far, we must change them
4590 if ( m_colWidths
.IsEmpty() )
4595 if ( m_rowHeights
.IsEmpty() )
4601 switch ( msg
.GetId() )
4603 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4605 size_t pos
= msg
.GetCommandInt();
4606 int numRows
= msg
.GetCommandInt2();
4608 m_numRows
+= numRows
;
4610 if ( !m_rowHeights
.IsEmpty() )
4612 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4613 m_rowBottoms
.Insert( 0, pos
, numRows
);
4617 bottom
= m_rowBottoms
[pos
- 1];
4619 for ( i
= pos
; i
< m_numRows
; i
++ )
4621 bottom
+= m_rowHeights
[i
];
4622 m_rowBottoms
[i
] = bottom
;
4626 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4628 // if we have just inserted cols into an empty grid the current
4629 // cell will be undefined...
4631 SetCurrentCell( 0, 0 );
4635 m_selection
->UpdateRows( pos
, numRows
);
4636 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4638 attrProvider
->UpdateAttrRows( pos
, numRows
);
4640 if ( !GetBatchCount() )
4643 m_rowLabelWin
->Refresh();
4649 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4651 int numRows
= msg
.GetCommandInt();
4652 int oldNumRows
= m_numRows
;
4653 m_numRows
+= numRows
;
4655 if ( !m_rowHeights
.IsEmpty() )
4657 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4658 m_rowBottoms
.Add( 0, numRows
);
4661 if ( oldNumRows
> 0 )
4662 bottom
= m_rowBottoms
[oldNumRows
- 1];
4664 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4666 bottom
+= m_rowHeights
[i
];
4667 m_rowBottoms
[i
] = bottom
;
4671 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4673 // if we have just inserted cols into an empty grid the current
4674 // cell will be undefined...
4676 SetCurrentCell( 0, 0 );
4679 if ( !GetBatchCount() )
4682 m_rowLabelWin
->Refresh();
4688 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4690 size_t pos
= msg
.GetCommandInt();
4691 int numRows
= msg
.GetCommandInt2();
4692 m_numRows
-= numRows
;
4694 if ( !m_rowHeights
.IsEmpty() )
4696 m_rowHeights
.RemoveAt( pos
, numRows
);
4697 m_rowBottoms
.RemoveAt( pos
, numRows
);
4700 for ( i
= 0; i
< m_numRows
; i
++ )
4702 h
+= m_rowHeights
[i
];
4703 m_rowBottoms
[i
] = h
;
4708 m_currentCellCoords
= wxGridNoCellCoords
;
4712 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4713 m_currentCellCoords
.Set( 0, 0 );
4717 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4718 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4721 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4722 // ifdef'd out following patch from Paul Gammans
4724 // No need to touch column attributes, unless we
4725 // removed _all_ rows, in this case, we remove
4726 // all column attributes.
4727 // I hate to do this here, but the
4728 // needed data is not available inside UpdateAttrRows.
4729 if ( !GetNumberRows() )
4730 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4733 if ( !GetBatchCount() )
4736 m_rowLabelWin
->Refresh();
4742 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4744 size_t pos
= msg
.GetCommandInt();
4745 int numCols
= msg
.GetCommandInt2();
4746 m_numCols
+= numCols
;
4748 if ( !m_colWidths
.IsEmpty() )
4750 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4751 m_colRights
.Insert( 0, pos
, numCols
);
4755 right
= m_colRights
[pos
- 1];
4757 for ( i
= pos
; i
< m_numCols
; i
++ )
4759 right
+= m_colWidths
[i
];
4760 m_colRights
[i
] = right
;
4764 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4766 // if we have just inserted cols into an empty grid the current
4767 // cell will be undefined...
4769 SetCurrentCell( 0, 0 );
4773 m_selection
->UpdateCols( pos
, numCols
);
4774 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4776 attrProvider
->UpdateAttrCols( pos
, numCols
);
4777 if ( !GetBatchCount() )
4780 m_colLabelWin
->Refresh();
4787 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4789 int numCols
= msg
.GetCommandInt();
4790 int oldNumCols
= m_numCols
;
4791 m_numCols
+= numCols
;
4792 if ( !m_colWidths
.IsEmpty() )
4794 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4795 m_colRights
.Add( 0, numCols
);
4798 if ( oldNumCols
> 0 )
4799 right
= m_colRights
[oldNumCols
- 1];
4801 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4803 right
+= m_colWidths
[i
];
4804 m_colRights
[i
] = right
;
4808 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4810 // if we have just inserted cols into an empty grid the current
4811 // cell will be undefined...
4813 SetCurrentCell( 0, 0 );
4815 if ( !GetBatchCount() )
4818 m_colLabelWin
->Refresh();
4824 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4826 size_t pos
= msg
.GetCommandInt();
4827 int numCols
= msg
.GetCommandInt2();
4828 m_numCols
-= numCols
;
4830 if ( !m_colWidths
.IsEmpty() )
4832 m_colWidths
.RemoveAt( pos
, numCols
);
4833 m_colRights
.RemoveAt( pos
, numCols
);
4836 for ( i
= 0; i
< m_numCols
; i
++ )
4838 w
+= m_colWidths
[i
];
4845 m_currentCellCoords
= wxGridNoCellCoords
;
4849 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4850 m_currentCellCoords
.Set( 0, 0 );
4854 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4855 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4858 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4859 // ifdef'd out following patch from Paul Gammans
4861 // No need to touch row attributes, unless we
4862 // removed _all_ columns, in this case, we remove
4863 // all row attributes.
4864 // I hate to do this here, but the
4865 // needed data is not available inside UpdateAttrCols.
4866 if ( !GetNumberCols() )
4867 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4870 if ( !GetBatchCount() )
4873 m_colLabelWin
->Refresh();
4880 if (result
&& !GetBatchCount() )
4881 m_gridWin
->Refresh();
4886 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4888 wxRegionIterator
iter( reg
);
4891 wxArrayInt rowlabels
;
4898 // TODO: remove this when we can...
4899 // There is a bug in wxMotif that gives garbage update
4900 // rectangles if you jump-scroll a long way by clicking the
4901 // scrollbar with middle button. This is a work-around
4903 #if defined(__WXMOTIF__)
4905 m_gridWin
->GetClientSize( &cw
, &ch
);
4906 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4907 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4910 // logical bounds of update region
4913 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4914 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4916 // find the row labels within these bounds
4919 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4921 if ( GetRowBottom(row
) < top
)
4924 if ( GetRowTop(row
) > bottom
)
4927 rowlabels
.Add( row
);
4936 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4938 wxRegionIterator
iter( reg
);
4941 wxArrayInt colLabels
;
4948 // TODO: remove this when we can...
4949 // There is a bug in wxMotif that gives garbage update
4950 // rectangles if you jump-scroll a long way by clicking the
4951 // scrollbar with middle button. This is a work-around
4953 #if defined(__WXMOTIF__)
4955 m_gridWin
->GetClientSize( &cw
, &ch
);
4956 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4957 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4960 // logical bounds of update region
4963 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4964 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4966 // find the cells within these bounds
4969 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4971 if ( GetColRight(col
) < left
)
4974 if ( GetColLeft(col
) > right
)
4977 colLabels
.Add( col
);
4986 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4988 wxRegionIterator
iter( reg
);
4991 wxGridCellCoordsArray cellsExposed
;
4993 int left
, top
, right
, bottom
;
4998 // TODO: remove this when we can...
4999 // There is a bug in wxMotif that gives garbage update
5000 // rectangles if you jump-scroll a long way by clicking the
5001 // scrollbar with middle button. This is a work-around
5003 #if defined(__WXMOTIF__)
5005 m_gridWin
->GetClientSize( &cw
, &ch
);
5006 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
5007 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
5008 r
.SetRight( wxMin( r
.GetRight(), cw
) );
5009 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
5012 // logical bounds of update region
5014 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
5015 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
5017 // find the cells within these bounds
5020 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
5022 if ( GetRowBottom(row
) <= top
)
5025 if ( GetRowTop(row
) > bottom
)
5028 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
5030 if ( GetColRight(col
) <= left
)
5033 if ( GetColLeft(col
) > right
)
5036 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
5043 return cellsExposed
;
5047 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
5050 wxPoint
pos( event
.GetPosition() );
5051 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5053 if ( event
.Dragging() )
5057 m_isDragging
= true;
5058 m_rowLabelWin
->CaptureMouse();
5061 if ( event
.LeftIsDown() )
5063 switch ( m_cursorMode
)
5065 case WXGRID_CURSOR_RESIZE_ROW
:
5067 int cw
, ch
, left
, dummy
;
5068 m_gridWin
->GetClientSize( &cw
, &ch
);
5069 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5071 wxClientDC
dc( m_gridWin
);
5074 GetRowTop(m_dragRowOrCol
) +
5075 GetRowMinimalHeight(m_dragRowOrCol
) );
5076 dc
.SetLogicalFunction(wxINVERT
);
5077 if ( m_dragLastPos
>= 0 )
5079 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5081 dc
.DrawLine( left
, y
, left
+cw
, y
);
5086 case WXGRID_CURSOR_SELECT_ROW
:
5088 if ( (row
= YToRow( y
)) >= 0 )
5092 m_selection
->SelectRow( row
,
5093 event
.ControlDown(),
5102 // default label to suppress warnings about "enumeration value
5103 // 'xxx' not handled in switch
5111 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5116 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
5117 m_isDragging
= false;
5120 // ------------ Entering or leaving the window
5122 if ( event
.Entering() || event
.Leaving() )
5124 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5127 // ------------ Left button pressed
5129 else if ( event
.LeftDown() )
5131 // don't send a label click event for a hit on the
5132 // edge of the row label - this is probably the user
5133 // wanting to resize the row
5135 if ( YToEdgeOfRow(y
) < 0 )
5139 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
5141 if ( !event
.ShiftDown() && !event
.CmdDown() )
5145 if ( event
.ShiftDown() )
5147 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5150 GetNumberCols() - 1,
5151 event
.ControlDown(),
5158 m_selection
->SelectRow( row
,
5159 event
.ControlDown(),
5166 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5171 // starting to drag-resize a row
5173 if ( CanDragRowSize() )
5174 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5178 // ------------ Left double click
5180 else if (event
.LeftDClick() )
5182 row
= YToEdgeOfRow(y
);
5187 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5189 // no default action at the moment
5194 // adjust row height depending on label text
5195 AutoSizeRowLabelSize( row
);
5197 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5202 // ------------ Left button released
5204 else if ( event
.LeftUp() )
5206 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5208 DoEndDragResizeRow();
5210 // Note: we are ending the event *after* doing
5211 // default processing in this case
5213 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5216 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5220 // ------------ Right button down
5222 else if ( event
.RightDown() )
5226 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5228 // no default action at the moment
5232 // ------------ Right double click
5234 else if ( event
.RightDClick() )
5238 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5240 // no default action at the moment
5244 // ------------ No buttons down and mouse moving
5246 else if ( event
.Moving() )
5248 m_dragRowOrCol
= YToEdgeOfRow( y
);
5249 if ( m_dragRowOrCol
>= 0 )
5251 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5253 // don't capture the mouse yet
5254 if ( CanDragRowSize() )
5255 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5258 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5260 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5265 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5268 wxPoint
pos( event
.GetPosition() );
5269 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5271 if ( event
.Dragging() )
5275 m_isDragging
= true;
5276 m_colLabelWin
->CaptureMouse();
5279 if ( event
.LeftIsDown() )
5281 switch ( m_cursorMode
)
5283 case WXGRID_CURSOR_RESIZE_COL
:
5285 int cw
, ch
, dummy
, top
;
5286 m_gridWin
->GetClientSize( &cw
, &ch
);
5287 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5289 wxClientDC
dc( m_gridWin
);
5292 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5293 GetColMinimalWidth(m_dragRowOrCol
));
5294 dc
.SetLogicalFunction(wxINVERT
);
5295 if ( m_dragLastPos
>= 0 )
5297 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5299 dc
.DrawLine( x
, top
, x
, top
+ch
);
5304 case WXGRID_CURSOR_SELECT_COL
:
5306 if ( (col
= XToCol( x
)) >= 0 )
5310 m_selection
->SelectCol( col
,
5311 event
.ControlDown(),
5320 // default label to suppress warnings about "enumeration value
5321 // 'xxx' not handled in switch
5329 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5334 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5335 m_isDragging
= false;
5338 // ------------ Entering or leaving the window
5340 if ( event
.Entering() || event
.Leaving() )
5342 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5345 // ------------ Left button pressed
5347 else if ( event
.LeftDown() )
5349 // don't send a label click event for a hit on the
5350 // edge of the col label - this is probably the user
5351 // wanting to resize the col
5353 if ( XToEdgeOfCol(x
) < 0 )
5357 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5359 if ( !event
.ShiftDown() && !event
.CmdDown() )
5363 if ( event
.ShiftDown() )
5365 m_selection
->SelectBlock( 0,
5366 m_currentCellCoords
.GetCol(),
5367 GetNumberRows() - 1, col
,
5368 event
.ControlDown(),
5375 m_selection
->SelectCol( col
,
5376 event
.ControlDown(),
5383 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5388 // starting to drag-resize a col
5390 if ( CanDragColSize() )
5391 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5395 // ------------ Left double click
5397 if ( event
.LeftDClick() )
5399 col
= XToEdgeOfCol(x
);
5404 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5406 // no default action at the moment
5411 // adjust column width depending on label text
5412 AutoSizeColLabelSize( col
);
5414 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5419 // ------------ Left button released
5421 else if ( event
.LeftUp() )
5423 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5425 DoEndDragResizeCol();
5427 // Note: we are ending the event *after* doing
5428 // default processing in this case
5430 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5433 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5437 // ------------ Right button down
5439 else if ( event
.RightDown() )
5443 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5445 // no default action at the moment
5449 // ------------ Right double click
5451 else if ( event
.RightDClick() )
5455 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5457 // no default action at the moment
5461 // ------------ No buttons down and mouse moving
5463 else if ( event
.Moving() )
5465 m_dragRowOrCol
= XToEdgeOfCol( x
);
5466 if ( m_dragRowOrCol
>= 0 )
5468 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5470 // don't capture the cursor yet
5471 if ( CanDragColSize() )
5472 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5475 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5477 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5482 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5484 if ( event
.LeftDown() )
5486 // indicate corner label by having both row and
5489 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5494 else if ( event
.LeftDClick() )
5496 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5498 else if ( event
.RightDown() )
5500 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5502 // no default action at the moment
5505 else if ( event
.RightDClick() )
5507 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5509 // no default action at the moment
5514 void wxGrid::ChangeCursorMode(CursorMode mode
,
5519 static const wxChar
*cursorModes
[] =
5528 wxLogTrace(_T("grid"),
5529 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5530 win
== m_colLabelWin
? _T("colLabelWin")
5531 : win
? _T("rowLabelWin")
5533 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5536 if ( mode
== m_cursorMode
&&
5537 win
== m_winCapture
&&
5538 captureMouse
== (m_winCapture
!= NULL
))
5543 // by default use the grid itself
5549 if (m_winCapture
->HasCapture())
5550 m_winCapture
->ReleaseMouse();
5551 m_winCapture
= (wxWindow
*)NULL
;
5554 m_cursorMode
= mode
;
5556 switch ( m_cursorMode
)
5558 case WXGRID_CURSOR_RESIZE_ROW
:
5559 win
->SetCursor( m_rowResizeCursor
);
5562 case WXGRID_CURSOR_RESIZE_COL
:
5563 win
->SetCursor( m_colResizeCursor
);
5567 win
->SetCursor( *wxSTANDARD_CURSOR
);
5571 // we need to capture mouse when resizing
5572 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5573 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5575 if ( captureMouse
&& resize
)
5577 win
->CaptureMouse();
5582 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5585 wxPoint
pos( event
.GetPosition() );
5586 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5588 wxGridCellCoords coords
;
5589 XYToCell( x
, y
, coords
);
5591 int cell_rows
, cell_cols
;
5592 bool isFirstDrag
= !m_isDragging
;
5593 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5594 if ((cell_rows
< 0) || (cell_cols
< 0))
5596 coords
.SetRow(coords
.GetRow() + cell_rows
);
5597 coords
.SetCol(coords
.GetCol() + cell_cols
);
5600 if ( event
.Dragging() )
5602 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5604 // Don't start doing anything until the mouse has been dragged at
5605 // least 3 pixels in any direction...
5608 if (m_startDragPos
== wxDefaultPosition
)
5610 m_startDragPos
= pos
;
5613 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5617 m_isDragging
= true;
5618 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5620 // Hide the edit control, so it
5621 // won't interfere with drag-shrinking.
5622 if ( IsCellEditControlShown() )
5624 HideCellEditControl();
5625 SaveEditControlValue();
5628 // Have we captured the mouse yet?
5631 m_winCapture
= m_gridWin
;
5632 m_winCapture
->CaptureMouse();
5635 if ( coords
!= wxGridNoCellCoords
)
5637 if ( event
.CmdDown() )
5639 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5640 m_selectingKeyboard
= coords
;
5641 HighlightBlock ( m_selectingKeyboard
, coords
);
5643 else if ( CanDragCell() )
5647 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5648 m_selectingKeyboard
= coords
;
5650 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5658 if ( !IsSelection() )
5660 HighlightBlock( coords
, coords
);
5664 HighlightBlock( m_currentCellCoords
, coords
);
5668 if (! IsVisible(coords
))
5670 MakeCellVisible(coords
);
5671 // TODO: need to introduce a delay or something here. The
5672 // scrolling is way to fast, at least on MSW - also on GTK.
5676 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5678 int cw
, ch
, left
, dummy
;
5679 m_gridWin
->GetClientSize( &cw
, &ch
);
5680 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5682 wxClientDC
dc( m_gridWin
);
5684 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5685 GetRowMinimalHeight(m_dragRowOrCol
) );
5686 dc
.SetLogicalFunction(wxINVERT
);
5687 if ( m_dragLastPos
>= 0 )
5689 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5691 dc
.DrawLine( left
, y
, left
+cw
, y
);
5694 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5696 int cw
, ch
, dummy
, top
;
5697 m_gridWin
->GetClientSize( &cw
, &ch
);
5698 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5700 wxClientDC
dc( m_gridWin
);
5702 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5703 GetColMinimalWidth(m_dragRowOrCol
) );
5704 dc
.SetLogicalFunction(wxINVERT
);
5705 if ( m_dragLastPos
>= 0 )
5707 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5709 dc
.DrawLine( x
, top
, x
, top
+ch
);
5716 m_isDragging
= false;
5717 m_startDragPos
= wxDefaultPosition
;
5719 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5720 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5723 if ( event
.Entering() || event
.Leaving() )
5725 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5726 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5731 // ------------ Left button pressed
5733 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5735 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5740 if ( !event
.CmdDown() )
5742 if ( event
.ShiftDown() )
5746 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5747 m_currentCellCoords
.GetCol(),
5750 event
.ControlDown(),
5756 else if ( XToEdgeOfCol(x
) < 0 &&
5757 YToEdgeOfRow(y
) < 0 )
5759 DisableCellEditControl();
5760 MakeCellVisible( coords
);
5762 if ( event
.CmdDown() )
5766 m_selection
->ToggleCellSelection( coords
.GetRow(),
5768 event
.ControlDown(),
5773 m_selectingTopLeft
= wxGridNoCellCoords
;
5774 m_selectingBottomRight
= wxGridNoCellCoords
;
5775 m_selectingKeyboard
= coords
;
5779 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5780 SetCurrentCell( coords
);
5783 if ( m_selection
->GetSelectionMode() !=
5784 wxGrid::wxGridSelectCells
)
5786 HighlightBlock( coords
, coords
);
5795 // ------------ Left double click
5797 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5799 DisableCellEditControl();
5801 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5803 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5808 // we want double click to select a cell and start editing
5809 // (i.e. to behave in same way as sequence of two slow clicks):
5810 m_waitForSlowClick
= true;
5816 // ------------ Left button released
5818 else if ( event
.LeftUp() )
5820 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5824 if (m_winCapture
->HasCapture())
5825 m_winCapture
->ReleaseMouse();
5826 m_winCapture
= NULL
;
5829 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl() )
5832 EnableCellEditControl();
5834 wxGridCellAttr
*attr
= GetCellAttr(coords
);
5835 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5836 editor
->StartingClick();
5840 m_waitForSlowClick
= false;
5842 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5843 m_selectingBottomRight
!= wxGridNoCellCoords
)
5847 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5848 m_selectingTopLeft
.GetCol(),
5849 m_selectingBottomRight
.GetRow(),
5850 m_selectingBottomRight
.GetCol(),
5851 event
.ControlDown(),
5857 m_selectingTopLeft
= wxGridNoCellCoords
;
5858 m_selectingBottomRight
= wxGridNoCellCoords
;
5860 // Show the edit control, if it has been hidden for
5862 ShowCellEditControl();
5865 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5867 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5868 DoEndDragResizeRow();
5870 // Note: we are ending the event *after* doing
5871 // default processing in this case
5873 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5875 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5877 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5878 DoEndDragResizeCol();
5880 // Note: we are ending the event *after* doing
5881 // default processing in this case
5883 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5889 // ------------ Right button down
5891 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5893 DisableCellEditControl();
5894 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5899 // no default action at the moment
5903 // ------------ Right double click
5905 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5907 DisableCellEditControl();
5908 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5913 // no default action at the moment
5917 // ------------ Moving and no button action
5919 else if ( event
.Moving() && !event
.IsButton() )
5921 if ( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5923 // out of grid cell area
5924 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5928 int dragRow
= YToEdgeOfRow( y
);
5929 int dragCol
= XToEdgeOfCol( x
);
5931 // Dragging on the corner of a cell to resize in both
5932 // directions is not implemented yet...
5934 if ( dragRow
>= 0 && dragCol
>= 0 )
5936 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5942 m_dragRowOrCol
= dragRow
;
5944 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5946 if ( CanDragRowSize() && CanDragGridSize() )
5947 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5950 else if ( dragCol
>= 0 )
5952 m_dragRowOrCol
= dragCol
;
5954 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5956 if ( CanDragColSize() && CanDragGridSize() )
5957 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5960 else // Neither on a row or col edge
5962 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5964 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5970 void wxGrid::DoEndDragResizeRow()
5972 if ( m_dragLastPos
>= 0 )
5974 // erase the last line and resize the row
5976 int cw
, ch
, left
, dummy
;
5977 m_gridWin
->GetClientSize( &cw
, &ch
);
5978 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5980 wxClientDC
dc( m_gridWin
);
5982 dc
.SetLogicalFunction( wxINVERT
);
5983 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5984 HideCellEditControl();
5985 SaveEditControlValue();
5987 int rowTop
= GetRowTop(m_dragRowOrCol
);
5988 SetRowSize( m_dragRowOrCol
,
5989 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5991 if ( !GetBatchCount() )
5993 // Only needed to get the correct rect.y:
5994 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5996 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5997 rect
.width
= m_rowLabelWidth
;
5998 rect
.height
= ch
- rect
.y
;
5999 m_rowLabelWin
->Refresh( true, &rect
);
6001 // if there is a multicell block, paint all of it
6004 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
6005 int leftCol
= XToCol(left
);
6006 int rightCol
= internalXToCol(left
+cw
);
6009 for (i
=leftCol
; i
<rightCol
; i
++)
6011 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
6012 if (cell_rows
< subtract_rows
)
6013 subtract_rows
= cell_rows
;
6015 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
6016 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
6017 rect
.height
= ch
- rect
.y
;
6020 m_gridWin
->Refresh( false, &rect
);
6023 ShowCellEditControl();
6028 void wxGrid::DoEndDragResizeCol()
6030 if ( m_dragLastPos
>= 0 )
6032 // erase the last line and resize the col
6034 int cw
, ch
, dummy
, top
;
6035 m_gridWin
->GetClientSize( &cw
, &ch
);
6036 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
6038 wxClientDC
dc( m_gridWin
);
6040 dc
.SetLogicalFunction( wxINVERT
);
6041 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
6042 HideCellEditControl();
6043 SaveEditControlValue();
6045 int colLeft
= GetColLeft(m_dragRowOrCol
);
6046 SetColSize( m_dragRowOrCol
,
6047 wxMax( m_dragLastPos
- colLeft
,
6048 GetColMinimalWidth(m_dragRowOrCol
) ) );
6050 if ( !GetBatchCount() )
6052 // Only needed to get the correct rect.x:
6053 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
6055 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6056 rect
.width
= cw
- rect
.x
;
6057 rect
.height
= m_colLabelHeight
;
6058 m_colLabelWin
->Refresh( true, &rect
);
6061 // if there is a multicell block, paint all of it
6064 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
6065 int topRow
= YToRow(top
);
6066 int bottomRow
= internalYToRow(top
+cw
);
6069 for (i
=topRow
; i
<bottomRow
; i
++)
6071 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
6072 if (cell_cols
< subtract_cols
)
6073 subtract_cols
= cell_cols
;
6075 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
6076 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
6077 rect
.width
= cw
- rect
.x
;
6081 m_gridWin
->Refresh( false, &rect
);
6084 ShowCellEditControl();
6089 // ------ interaction with data model
6091 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
6093 switch ( msg
.GetId() )
6095 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
6096 return GetModelValues();
6098 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
6099 return SetModelValues();
6101 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
6102 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
6103 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
6104 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
6105 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
6106 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
6107 return Redimension( msg
);
6114 // The behaviour of this function depends on the grid table class
6115 // Clear() function. For the default wxGridStringTable class the
6116 // behavious is to replace all cell contents with wxEmptyString but
6117 // not to change the number of rows or cols.
6119 void wxGrid::ClearGrid()
6123 if (IsCellEditControlEnabled())
6124 DisableCellEditControl();
6127 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6131 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6133 // TODO: something with updateLabels flag
6137 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6143 if (IsCellEditControlEnabled())
6144 DisableCellEditControl();
6146 bool done
= m_table
->InsertRows( pos
, numRows
);
6149 // the table will have sent the results of the insert row
6150 // operation to this view object as a grid table message
6155 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6157 // TODO: something with updateLabels flag
6161 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6167 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6169 // the table will have sent the results of the append row
6170 // operation to this view object as a grid table message
6175 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6177 // TODO: something with updateLabels flag
6181 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6187 if (IsCellEditControlEnabled())
6188 DisableCellEditControl();
6190 bool done
= m_table
->DeleteRows( pos
, numRows
);
6192 // the table will have sent the results of the delete row
6193 // operation to this view object as a grid table message
6198 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6200 // TODO: something with updateLabels flag
6204 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6210 if (IsCellEditControlEnabled())
6211 DisableCellEditControl();
6213 bool done
= m_table
->InsertCols( pos
, numCols
);
6215 // the table will have sent the results of the insert col
6216 // operation to this view object as a grid table message
6222 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6224 // TODO: something with updateLabels flag
6228 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6234 bool done
= m_table
->AppendCols( numCols
);
6236 // the table will have sent the results of the append col
6237 // operation to this view object as a grid table message
6243 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6245 // TODO: something with updateLabels flag
6249 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6255 if (IsCellEditControlEnabled())
6256 DisableCellEditControl();
6258 bool done
= m_table
->DeleteCols( pos
, numCols
);
6260 // the table will have sent the results of the delete col
6261 // operation to this view object as a grid table message
6268 // ----- event handlers
6271 // Generate a grid event based on a mouse event and
6272 // return the result of ProcessEvent()
6274 int wxGrid::SendEvent( const wxEventType type
,
6276 wxMouseEvent
& mouseEv
)
6278 bool claimed
, vetoed
;
6280 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6282 int rowOrCol
= (row
== -1 ? col
: row
);
6284 wxGridSizeEvent
gridEvt( GetId(),
6288 mouseEv
.GetX() + GetRowLabelSize(),
6289 mouseEv
.GetY() + GetColLabelSize(),
6290 mouseEv
.ControlDown(),
6291 mouseEv
.ShiftDown(),
6293 mouseEv
.MetaDown() );
6295 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6296 vetoed
= !gridEvt
.IsAllowed();
6298 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6300 // Right now, it should _never_ end up here!
6301 wxGridRangeSelectEvent
gridEvt( GetId(),
6305 m_selectingBottomRight
,
6307 mouseEv
.ControlDown(),
6308 mouseEv
.ShiftDown(),
6310 mouseEv
.MetaDown() );
6312 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6313 vetoed
= !gridEvt
.IsAllowed();
6317 wxGridEvent
gridEvt( GetId(),
6321 mouseEv
.GetX() + GetRowLabelSize(),
6322 mouseEv
.GetY() + GetColLabelSize(),
6324 mouseEv
.ControlDown(),
6325 mouseEv
.ShiftDown(),
6327 mouseEv
.MetaDown() );
6328 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6329 vetoed
= !gridEvt
.IsAllowed();
6332 // A Veto'd event may not be `claimed' so test this first
6336 return claimed
? 1 : 0;
6339 // Generate a grid event of specified type and return the result
6340 // of ProcessEvent().
6342 int wxGrid::SendEvent( const wxEventType type
,
6348 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6350 int rowOrCol
= (row
== -1 ? col
: row
);
6352 wxGridSizeEvent
gridEvt( GetId(),
6357 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6358 vetoed
= !gridEvt
.IsAllowed();
6362 wxGridEvent
gridEvt( GetId(),
6367 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6368 vetoed
= !gridEvt
.IsAllowed();
6371 // A Veto'd event may not be `claimed' so test this first
6375 return claimed
? 1 : 0;
6378 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6380 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6383 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6385 // Don't do anything if between Begin/EndBatch...
6386 // EndBatch() will do all this on the last nested one anyway.
6387 if (! GetBatchCount())
6389 // Refresh to get correct scrolled position:
6390 wxScrolledWindow::Refresh(eraseb
, rect
);
6394 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6395 int width_label
, width_cell
, height_label
, height_cell
;
6398 // Copy rectangle can get scroll offsets..
6399 rect_x
= rect
->GetX();
6400 rect_y
= rect
->GetY();
6401 rectWidth
= rect
->GetWidth();
6402 rectHeight
= rect
->GetHeight();
6404 width_label
= m_rowLabelWidth
- rect_x
;
6405 if (width_label
> rectWidth
)
6406 width_label
= rectWidth
;
6408 height_label
= m_colLabelHeight
- rect_y
;
6409 if (height_label
> rectHeight
) height_label
= rectHeight
;
6411 if (rect_x
> m_rowLabelWidth
)
6413 x
= rect_x
- m_rowLabelWidth
;
6414 width_cell
= rectWidth
;
6419 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6422 if (rect_y
> m_colLabelHeight
)
6424 y
= rect_y
- m_colLabelHeight
;
6425 height_cell
= rectHeight
;
6430 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6433 // Paint corner label part intersecting rect.
6434 if ( width_label
> 0 && height_label
> 0 )
6436 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6437 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6440 // Paint col labels part intersecting rect.
6441 if ( width_cell
> 0 && height_label
> 0 )
6443 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6444 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6447 // Paint row labels part intersecting rect.
6448 if ( width_label
> 0 && height_cell
> 0 )
6450 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6451 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6454 // Paint cell area part intersecting rect.
6455 if ( width_cell
> 0 && height_cell
> 0 )
6457 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6458 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6463 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6464 m_colLabelWin
->Refresh(eraseb
, NULL
);
6465 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6466 m_gridWin
->Refresh(eraseb
, NULL
);
6471 void wxGrid::OnSize( wxSizeEvent
& event
)
6473 // position the child windows
6476 // don't call CalcDimensions() from here, the base class handles the size
6481 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6483 if ( m_inOnKeyDown
)
6485 // shouldn't be here - we are going round in circles...
6487 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6490 m_inOnKeyDown
= true;
6492 // propagate the event up and see if it gets processed
6494 wxWindow
*parent
= GetParent();
6495 wxKeyEvent
keyEvt( event
);
6496 keyEvt
.SetEventObject( parent
);
6498 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6501 // try local handlers
6503 switch ( event
.GetKeyCode() )
6506 if ( event
.ControlDown() )
6508 MoveCursorUpBlock( event
.ShiftDown() );
6512 MoveCursorUp( event
.ShiftDown() );
6517 if ( event
.ControlDown() )
6519 MoveCursorDownBlock( event
.ShiftDown() );
6523 MoveCursorDown( event
.ShiftDown() );
6528 if ( event
.ControlDown() )
6530 MoveCursorLeftBlock( event
.ShiftDown() );
6534 MoveCursorLeft( event
.ShiftDown() );
6539 if ( event
.ControlDown() )
6541 MoveCursorRightBlock( event
.ShiftDown() );
6545 MoveCursorRight( event
.ShiftDown() );
6550 case WXK_NUMPAD_ENTER
:
6551 if ( event
.ControlDown() )
6553 event
.Skip(); // to let the edit control have the return
6557 if ( GetGridCursorRow() < GetNumberRows()-1 )
6559 MoveCursorDown( event
.ShiftDown() );
6563 // at the bottom of a column
6564 DisableCellEditControl();
6574 if (event
.ShiftDown())
6576 if ( GetGridCursorCol() > 0 )
6578 MoveCursorLeft( false );
6583 DisableCellEditControl();
6588 if ( GetGridCursorCol() < GetNumberCols()-1 )
6590 MoveCursorRight( false );
6595 DisableCellEditControl();
6601 if ( event
.ControlDown() )
6603 MakeCellVisible( 0, 0 );
6604 SetCurrentCell( 0, 0 );
6613 if ( event
.ControlDown() )
6615 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6616 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6633 if ( event
.ControlDown() )
6637 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6638 m_currentCellCoords
.GetCol(),
6639 event
.ControlDown(),
6646 if ( !IsEditable() )
6648 MoveCursorRight( false );
6651 // Otherwise fall through to default
6659 m_inOnKeyDown
= false;
6662 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6664 // try local handlers
6666 if ( event
.GetKeyCode() == WXK_SHIFT
)
6668 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6669 m_selectingBottomRight
!= wxGridNoCellCoords
)
6673 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6674 m_selectingTopLeft
.GetCol(),
6675 m_selectingBottomRight
.GetRow(),
6676 m_selectingBottomRight
.GetCol(),
6677 event
.ControlDown(),
6684 m_selectingTopLeft
= wxGridNoCellCoords
;
6685 m_selectingBottomRight
= wxGridNoCellCoords
;
6686 m_selectingKeyboard
= wxGridNoCellCoords
;
6690 void wxGrid::OnChar( wxKeyEvent
& event
)
6692 // is it possible to edit the current cell at all?
6693 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6695 // yes, now check whether the cells editor accepts the key
6696 int row
= m_currentCellCoords
.GetRow();
6697 int col
= m_currentCellCoords
.GetCol();
6698 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
6699 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6701 // <F2> is special and will always start editing, for
6702 // other keys - ask the editor itself
6703 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6704 || editor
->IsAcceptedKey(event
) )
6706 // ensure cell is visble
6707 MakeCellVisible(row
, col
);
6708 EnableCellEditControl();
6710 // a problem can arise if the cell is not completely
6711 // visible (even after calling MakeCellVisible the
6712 // control is not created and calling StartingKey will
6714 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6715 editor
->StartingKey(event
);
6731 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6735 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6737 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6739 // the event has been intercepted - do nothing
6743 wxClientDC
dc( m_gridWin
);
6746 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6748 DisableCellEditControl();
6750 if ( IsVisible( m_currentCellCoords
, false ) )
6753 r
= BlockToDeviceRect( m_currentCellCoords
, m_currentCellCoords
);
6754 if ( !m_gridLinesEnabled
)
6762 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6764 // Otherwise refresh redraws the highlight!
6765 m_currentCellCoords
= coords
;
6767 DrawGridCellArea( dc
, cells
);
6768 DrawAllGridLines( dc
, r
);
6772 m_currentCellCoords
= coords
;
6774 wxGridCellAttr
*attr
= GetCellAttr( coords
);
6775 DrawCellHighlight( dc
, attr
);
6779 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6782 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6786 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6789 rightCol
= GetNumberCols() - 1;
6791 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6794 bottomRow
= GetNumberRows() - 1;
6798 if ( topRow
> bottomRow
)
6805 if ( leftCol
> rightCol
)
6812 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6813 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6815 // First the case that we selected a completely new area
6816 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6817 m_selectingBottomRight
== wxGridNoCellCoords
)
6820 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6821 wxGridCellCoords ( bottomRow
, rightCol
) );
6822 m_gridWin
->Refresh( false, &rect
);
6825 // Now handle changing an existing selection area.
6826 else if ( m_selectingTopLeft
!= updateTopLeft
||
6827 m_selectingBottomRight
!= updateBottomRight
)
6829 // Compute two optimal update rectangles:
6830 // Either one rectangle is a real subset of the
6831 // other, or they are (almost) disjoint!
6833 bool need_refresh
[4];
6837 need_refresh
[3] = false;
6840 // Store intermediate values
6841 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6842 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6843 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6844 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6846 // Determine the outer/inner coordinates.
6847 if (oldLeft
> leftCol
)
6853 if (oldTop
> topRow
)
6859 if (oldRight
< rightCol
)
6862 oldRight
= rightCol
;
6865 if (oldBottom
< bottomRow
)
6868 oldBottom
= bottomRow
;
6872 // Now, either the stuff marked old is the outer
6873 // rectangle or we don't have a situation where one
6874 // is contained in the other.
6876 if ( oldLeft
< leftCol
)
6878 // Refresh the newly selected or deselected
6879 // area to the left of the old or new selection.
6880 need_refresh
[0] = true;
6881 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6883 wxGridCellCoords ( oldBottom
,
6887 if ( oldTop
< topRow
)
6889 // Refresh the newly selected or deselected
6890 // area above the old or new selection.
6891 need_refresh
[1] = true;
6892 rect
[1] = BlockToDeviceRect(
6893 wxGridCellCoords( oldTop
, leftCol
),
6894 wxGridCellCoords( topRow
- 1, rightCol
) );
6897 if ( oldRight
> rightCol
)
6899 // Refresh the newly selected or deselected
6900 // area to the right of the old or new selection.
6901 need_refresh
[2] = true;
6902 rect
[2] = BlockToDeviceRect(
6903 wxGridCellCoords ( oldTop
, rightCol
+ 1 ),
6904 wxGridCellCoords ( oldBottom
, oldRight
) );
6907 if ( oldBottom
> bottomRow
)
6909 // Refresh the newly selected or deselected
6910 // area below the old or new selection.
6911 need_refresh
[3] = true;
6912 rect
[3] = BlockToDeviceRect(
6913 wxGridCellCoords ( bottomRow
+ 1, leftCol
),
6914 wxGridCellCoords ( oldBottom
, rightCol
) );
6917 // various Refresh() calls
6918 for (i
= 0; i
< 4; i
++ )
6919 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6920 m_gridWin
->Refresh( false, &(rect
[i
]) );
6924 m_selectingTopLeft
= updateTopLeft
;
6925 m_selectingBottomRight
= updateBottomRight
;
6929 // ------ functions to get/send data (see also public functions)
6932 bool wxGrid::GetModelValues()
6934 // Hide the editor, so it won't hide a changed value.
6935 HideCellEditControl();
6939 // all we need to do is repaint the grid
6941 m_gridWin
->Refresh();
6948 bool wxGrid::SetModelValues()
6952 // Disable the editor, so it won't hide a changed value.
6953 // Do we also want to save the current value of the editor first?
6955 DisableCellEditControl();
6959 for ( row
= 0; row
< m_numRows
; row
++ )
6961 for ( col
= 0; col
< m_numCols
; col
++ )
6963 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6973 // Note - this function only draws cells that are in the list of
6974 // exposed cells (usually set from the update region by
6975 // CalcExposedCells)
6977 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6979 if ( !m_numRows
|| !m_numCols
)
6982 int i
, numCells
= cells
.GetCount();
6983 int row
, col
, cell_rows
, cell_cols
;
6984 wxGridCellCoordsArray redrawCells
;
6986 for ( i
= numCells
-1; i
>= 0; i
-- )
6988 row
= cells
[i
].GetRow();
6989 col
= cells
[i
].GetCol();
6990 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6992 // If this cell is part of a multicell block, find owner for repaint
6993 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6995 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6996 bool marked
= false;
6997 for ( int j
= 0; j
< numCells
; j
++ )
6999 if ( cell
== cells
[j
] )
7008 int count
= redrawCells
.GetCount();
7009 for (int j
= 0; j
< count
; j
++)
7011 if ( cell
== redrawCells
[j
] )
7019 redrawCells
.Add( cell
);
7022 // don't bother drawing this cell
7026 // If this cell is empty, find cell to left that might want to overflow
7027 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
7029 for ( int l
= 0; l
< cell_rows
; l
++ )
7031 // find a cell in this row to left alreay marked for repaint
7033 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
7034 if ((redrawCells
[k
].GetCol() < left
) &&
7035 (redrawCells
[k
].GetRow() == row
))
7037 left
= redrawCells
[k
].GetCol();
7041 left
= 0; // oh well
7043 for (int j
= col
- 1; j
>= left
; j
--)
7045 if (!m_table
->IsEmptyCell(row
+ l
, j
))
7047 if (GetCellOverflow(row
+ l
, j
))
7049 wxGridCellCoords
cell(row
+ l
, j
);
7050 bool marked
= false;
7052 for (int k
= 0; k
< numCells
; k
++)
7054 if ( cell
== cells
[k
] )
7063 int count
= redrawCells
.GetCount();
7064 for (int k
= 0; k
< count
; k
++)
7066 if ( cell
== redrawCells
[k
] )
7073 redrawCells
.Add( cell
);
7082 DrawCell( dc
, cells
[i
] );
7085 numCells
= redrawCells
.GetCount();
7087 for ( i
= numCells
- 1; i
>= 0; i
-- )
7089 DrawCell( dc
, redrawCells
[i
] );
7093 void wxGrid::DrawGridSpace( wxDC
& dc
)
7096 m_gridWin
->GetClientSize( &cw
, &ch
);
7099 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7101 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
7102 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0;
7104 if ( right
> rightCol
|| bottom
> bottomRow
)
7107 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7109 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
7110 dc
.SetPen( *wxTRANSPARENT_PEN
);
7112 if ( right
> rightCol
)
7114 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
7117 if ( bottom
> bottomRow
)
7119 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
7124 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
7126 int row
= coords
.GetRow();
7127 int col
= coords
.GetCol();
7129 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7132 // we draw the cell border ourselves
7133 #if !WXGRID_DRAW_LINES
7134 if ( m_gridLinesEnabled
)
7135 DrawCellBorder( dc
, coords
);
7138 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7140 bool isCurrent
= coords
== m_currentCellCoords
;
7142 wxRect rect
= CellToRect( row
, col
);
7144 // if the editor is shown, we should use it and not the renderer
7145 // Note: However, only if it is really _shown_, i.e. not hidden!
7146 if ( isCurrent
&& IsCellEditControlShown() )
7148 // OSAF NB: this "#if..." is temporary and fixes a problem where the
7149 // edit control is erased by this code after being rendered.
7150 // On wxMac (QD build only), the cell editor is a wxTextCntl and is rendered
7151 // implicitly, causing this out-of order render.
7152 #if !defined(__WXMAC__) || wxMAC_USE_CORE_GRAPHICS
7153 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7154 editor
->PaintBackground(rect
, attr
);
7160 // but all the rest is drawn by the cell renderer and hence may be
7162 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7163 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7170 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7172 int row
= m_currentCellCoords
.GetRow();
7173 int col
= m_currentCellCoords
.GetCol();
7175 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7178 wxRect rect
= CellToRect(row
, col
);
7180 // hmmm... what could we do here to show that the cell is disabled?
7181 // for now, I just draw a thinner border than for the other ones, but
7182 // it doesn't look really good
7184 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7188 // The center of th drawn line is where the position/width/height of
7189 // the rectangle is actually at, (on wxMSW atr least,) so we will
7190 // reduce the size of the rectangle to compensate for the thickness of
7191 // the line. If this is too strange on non wxMSW platforms then
7192 // please #ifdef this appropriately.
7193 rect
.x
+= penWidth
/ 2;
7194 rect
.y
+= penWidth
/ 2;
7195 rect
.width
-= penWidth
- 1;
7196 rect
.height
-= penWidth
- 1;
7198 // Now draw the rectangle
7199 // use the cellHighlightColour if the cell is inside a selection, this
7200 // will ensure the cell is always visible.
7201 dc
.SetPen(wxPen(IsInSelection(row
,col
) ? m_selectionForeground
: m_cellHighlightColour
, penWidth
, wxSOLID
));
7202 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7203 dc
.DrawRectangle(rect
);
7207 // VZ: my experiments with 3D borders...
7209 // how to properly set colours for arbitrary bg?
7210 wxCoord x1
= rect
.x
,
7212 x2
= rect
.x
+ rect
.width
- 1,
7213 y2
= rect
.y
+ rect
.height
- 1;
7215 dc
.SetPen(*wxWHITE_PEN
);
7216 dc
.DrawLine(x1
, y1
, x2
, y1
);
7217 dc
.DrawLine(x1
, y1
, x1
, y2
);
7219 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7220 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7222 dc
.SetPen(*wxBLACK_PEN
);
7223 dc
.DrawLine(x1
, y2
, x2
, y2
);
7224 dc
.DrawLine(x2
, y1
, x2
, y2
+ 1);
7228 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7230 int row
= coords
.GetRow();
7231 int col
= coords
.GetCol();
7232 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7235 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7237 wxRect rect
= CellToRect( row
, col
);
7239 // right hand border
7241 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7242 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7246 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7247 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7250 void wxGrid::DrawHighlight(wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
7252 // This if block was previously in wxGrid::OnPaint but that doesn't
7253 // seem to get called under wxGTK - MB
7255 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7256 m_numRows
&& m_numCols
)
7258 m_currentCellCoords
.Set(0, 0);
7261 if ( IsCellEditControlShown() )
7263 // don't show highlight when the edit control is shown
7267 // if the active cell was repainted, repaint its highlight too because it
7268 // might have been damaged by the grid lines
7269 size_t count
= cells
.GetCount();
7270 for ( size_t n
= 0; n
< count
; n
++ )
7272 if ( cells
[n
] == m_currentCellCoords
)
7274 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7275 DrawCellHighlight(dc
, attr
);
7283 // TODO: remove this ???
7284 // This is used to redraw all grid lines e.g. when the grid line colour
7287 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7289 #if !WXGRID_DRAW_LINES
7293 if ( !m_gridLinesEnabled
|| !m_numRows
|| !m_numCols
)
7296 int top
, bottom
, left
, right
;
7298 #if 0 //#ifndef __WXGTK__
7302 m_gridWin
->GetClientSize(&cw
, &ch
);
7304 // virtual coords of visible area
7306 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7307 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7312 reg
.GetBox(x
, y
, w
, h
);
7313 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7314 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7318 m_gridWin
->GetClientSize(&cw
, &ch
);
7319 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7320 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7323 // avoid drawing grid lines past the last row and col
7325 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7326 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7328 // no gridlines inside multicells, clip them out
7329 int leftCol
= internalXToCol(left
);
7330 int topRow
= internalYToRow(top
);
7331 int rightCol
= internalXToCol(right
);
7332 int bottomRow
= internalYToRow(bottom
);
7335 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7336 wxRegion
clippedcells(0, 0, cw
, ch
);
7338 int i
, j
, cell_rows
, cell_cols
;
7341 for (j
=topRow
; j
<bottomRow
; j
++)
7343 for (i
=leftCol
; i
<rightCol
; i
++)
7345 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7346 if ((cell_rows
> 1) || (cell_cols
> 1))
7348 rect
= CellToRect(j
,i
);
7349 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7350 clippedcells
.Subtract(rect
);
7352 else if ((cell_rows
< 0) || (cell_cols
< 0))
7354 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7355 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7356 clippedcells
.Subtract(rect
);
7361 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7363 int i
, j
, cell_rows
, cell_cols
;
7366 for (j
=topRow
; j
<bottomRow
; j
++)
7368 for (i
=leftCol
; i
<rightCol
; i
++)
7370 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7371 if ((cell_rows
> 1) || (cell_cols
> 1))
7373 rect
= CellToRect(j
, i
);
7374 clippedcells
.Subtract(rect
);
7376 else if ((cell_rows
< 0) || (cell_cols
< 0))
7378 rect
= CellToRect(j
+ cell_rows
, i
+ cell_cols
);
7379 clippedcells
.Subtract(rect
);
7384 dc
.SetClippingRegion( clippedcells
);
7386 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7388 // horizontal grid lines
7390 // already declared above - int i;
7391 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7393 int bot
= GetRowBottom(i
) - 1;
7402 dc
.DrawLine( left
, bot
, right
, bot
);
7406 // vertical grid lines
7408 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7410 int colRight
= GetColRight(i
) - 1;
7411 if ( colRight
> right
)
7416 if ( colRight
>= left
)
7418 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7421 dc
.DestroyClippingRegion();
7424 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7430 size_t numLabels
= rows
.GetCount();
7432 for ( i
= 0; i
< numLabels
; i
++ )
7434 DrawRowLabel( dc
, rows
[i
] );
7438 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7440 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7447 rect
.SetY( GetRowTop(row
) + 1 );
7448 rect
.SetWidth( m_rowLabelWidth
- 2 );
7449 rect
.SetHeight( GetRowHeight(row
) - 2 );
7451 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7453 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7455 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7457 int rowTop
= GetRowTop(row
),
7458 rowBottom
= GetRowBottom(row
) - 1;
7460 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7461 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7462 m_rowLabelWidth
-1, rowBottom
);
7464 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7466 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7468 dc
.SetPen( *wxWHITE_PEN
);
7469 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7470 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7473 dc
.SetBackgroundMode( wxTRANSPARENT
);
7474 dc
.SetTextForeground( GetLabelTextColour() );
7475 dc
.SetFont( GetLabelFont() );
7478 GetRowLabelAlignment( &hAlign
, &vAlign
);
7481 rect
.SetY( GetRowTop(row
) + 2 );
7482 rect
.SetWidth( m_rowLabelWidth
- 4 );
7483 rect
.SetHeight( GetRowHeight(row
) - 4 );
7484 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7487 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7493 size_t numLabels
= cols
.GetCount();
7495 for ( i
= 0; i
< numLabels
; i
++ )
7497 DrawColLabel( dc
, cols
[i
] );
7501 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7503 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7506 int colLeft
= GetColLeft(col
);
7511 rect
.SetX( colLeft
+ 1 );
7513 rect
.SetWidth( GetColWidth(col
) - 2 );
7514 rect
.SetHeight( m_colLabelHeight
- 2 );
7516 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7518 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7520 int colRight
= GetColRight(col
) - 1;
7522 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
), 1, wxSOLID
) );
7523 dc
.DrawLine( colRight
, 0,
7524 colRight
, m_colLabelHeight
-1 );
7526 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7528 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7529 colRight
+1, m_colLabelHeight
-1 );
7531 dc
.SetPen( *wxWHITE_PEN
);
7532 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7533 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7536 dc
.SetBackgroundMode( wxTRANSPARENT
);
7537 dc
.SetTextForeground( GetLabelTextColour() );
7538 dc
.SetFont( GetLabelFont() );
7540 int hAlign
, vAlign
, orient
;
7541 GetColLabelAlignment( &hAlign
, &vAlign
);
7542 orient
= GetColLabelTextOrientation();
7544 rect
.SetX( colLeft
+ 2 );
7546 rect
.SetWidth( GetColWidth(col
) - 4 );
7547 rect
.SetHeight( m_colLabelHeight
- 4 );
7548 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7551 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7552 const wxString
& value
,
7556 int textOrientation
)
7558 wxArrayString lines
;
7560 StringToLines( value
, lines
);
7562 // Forward to new API.
7563 DrawTextRectangle( dc
,
7571 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7572 const wxArrayString
& lines
,
7576 int textOrientation
)
7578 long textWidth
= 0, textHeight
= 0;
7579 long lineWidth
= 0, lineHeight
= 0;
7582 dc
.SetClippingRegion( rect
);
7584 nLines
= lines
.GetCount();
7588 float x
= 0.0, y
= 0.0;
7590 if ( textOrientation
== wxHORIZONTAL
)
7591 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7593 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7595 switch ( vertAlign
)
7597 case wxALIGN_BOTTOM
:
7598 if ( textOrientation
== wxHORIZONTAL
)
7599 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7601 x
= rect
.x
+ rect
.width
- textWidth
;
7604 case wxALIGN_CENTRE
:
7605 if ( textOrientation
== wxHORIZONTAL
)
7606 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7608 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7613 if ( textOrientation
== wxHORIZONTAL
)
7620 // Align each line of a multi-line label
7621 for ( l
= 0; l
< nLines
; l
++ )
7623 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7625 switch ( horizAlign
)
7628 if ( textOrientation
== wxHORIZONTAL
)
7629 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7631 y
= rect
.y
+ lineWidth
+ 1;
7634 case wxALIGN_CENTRE
:
7635 if ( textOrientation
== wxHORIZONTAL
)
7636 x
= rect
.x
+ ((rect
.width
- lineWidth
) / 2);
7638 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
) / 2);
7643 if ( textOrientation
== wxHORIZONTAL
)
7646 y
= rect
.y
+ rect
.height
- 1;
7650 if ( textOrientation
== wxHORIZONTAL
)
7652 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7657 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7663 dc
.DestroyClippingRegion();
7666 // Split multi-line text up into an array of strings.
7667 // Any existing contents of the string array are preserved.
7669 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7673 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7674 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7676 while ( startPos
< (int)tVal
.length() )
7678 pos
= tVal
.Mid(startPos
).Find( eol
);
7683 else if ( pos
== 0 )
7685 lines
.Add( wxEmptyString
);
7689 lines
.Add( value
.Mid(startPos
, pos
) );
7691 startPos
+= pos
+ 1;
7694 if ( startPos
< (int)value
.length() )
7696 lines
.Add( value
.Mid( startPos
) );
7700 void wxGrid::GetTextBoxSize( const wxDC
& dc
,
7701 const wxArrayString
& lines
,
7702 long *width
, long *height
)
7706 long lineW
= 0, lineH
= 0;
7709 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7711 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7712 w
= wxMax( w
, lineW
);
7721 // ------ Batch processing.
7723 void wxGrid::EndBatch()
7725 if ( m_batchCount
> 0 )
7728 if ( !m_batchCount
)
7731 m_rowLabelWin
->Refresh();
7732 m_colLabelWin
->Refresh();
7733 m_cornerLabelWin
->Refresh();
7734 m_gridWin
->Refresh();
7739 // Use this, rather than wxWindow::Refresh(), to force an immediate
7740 // repainting of the grid. Has no effect if you are already inside a
7741 // BeginBatch / EndBatch block.
7743 void wxGrid::ForceRefresh()
7749 bool wxGrid::Enable(bool enable
)
7751 if ( !wxScrolledWindow::Enable(enable
) )
7754 // redraw in the new state
7755 m_gridWin
->Refresh();
7761 // ------ Edit control functions
7764 void wxGrid::EnableEditing( bool edit
)
7766 // TODO: improve this ?
7768 if ( edit
!= m_editable
)
7771 EnableCellEditControl(edit
);
7776 void wxGrid::EnableCellEditControl( bool enable
)
7781 if ( enable
!= m_cellEditCtrlEnabled
)
7785 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7788 // this should be checked by the caller!
7789 wxASSERT_MSG( CanEnableCellControl(),
7790 _T("can't enable editing for this cell!") );
7792 // do it before ShowCellEditControl()
7793 m_cellEditCtrlEnabled
= enable
;
7795 ShowCellEditControl();
7799 //FIXME:add veto support
7800 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7802 HideCellEditControl();
7803 SaveEditControlValue();
7805 // do it after HideCellEditControl()
7806 m_cellEditCtrlEnabled
= enable
;
7811 bool wxGrid::IsCurrentCellReadOnly() const
7814 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7815 bool readonly
= attr
->IsReadOnly();
7821 bool wxGrid::CanEnableCellControl() const
7823 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7824 !IsCurrentCellReadOnly();
7828 bool wxGrid::IsCellEditControlEnabled() const
7830 // the cell edit control might be disable for all cells or just for the
7831 // current one if it's read only
7832 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7835 bool wxGrid::IsCellEditControlShown() const
7837 bool isShown
= false;
7839 if ( m_cellEditCtrlEnabled
)
7841 int row
= m_currentCellCoords
.GetRow();
7842 int col
= m_currentCellCoords
.GetCol();
7843 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7844 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7849 if ( editor
->IsCreated() )
7851 isShown
= editor
->GetControl()->IsShown();
7861 void wxGrid::ShowCellEditControl()
7863 if ( IsCellEditControlEnabled() )
7865 if ( !IsVisible( m_currentCellCoords
, false ) )
7867 m_cellEditCtrlEnabled
= false;
7872 wxRect rect
= CellToRect( m_currentCellCoords
);
7873 int row
= m_currentCellCoords
.GetRow();
7874 int col
= m_currentCellCoords
.GetCol();
7876 // if this is part of a multicell, find owner (topleft)
7877 int cell_rows
, cell_cols
;
7878 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7879 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7883 m_currentCellCoords
.SetRow( row
);
7884 m_currentCellCoords
.SetCol( col
);
7887 // convert to scrolled coords
7889 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7895 // done in PaintBackground()
7897 // erase the highlight and the cell contents because the editor
7898 // might not cover the entire cell
7899 wxClientDC
dc( m_gridWin
);
7901 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7902 dc
.SetPen(*wxTRANSPARENT_PEN
);
7903 dc
.DrawRectangle(rect
);
7906 // cell is shifted by one pixel
7907 // However, don't allow x or y to become negative
7908 // since the SetSize() method interprets that as
7915 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7916 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7917 if ( !editor
->IsCreated() )
7919 editor
->Create(m_gridWin
, wxID_ANY
,
7920 new wxGridCellEditorEvtHandler(this, editor
));
7922 wxGridEditorCreatedEvent
evt(GetId(),
7923 wxEVT_GRID_EDITOR_CREATED
,
7927 editor
->GetControl());
7928 GetEventHandler()->ProcessEvent(evt
);
7931 // resize editor to overflow into righthand cells if allowed
7932 int maxWidth
= rect
.width
;
7933 wxString value
= GetCellValue(row
, col
);
7934 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7937 GetTextExtent(value
, &maxWidth
, &y
, NULL
, NULL
, &attr
->GetFont());
7938 if (maxWidth
< rect
.width
)
7939 maxWidth
= rect
.width
;
7942 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7943 if (rect
.x
+ maxWidth
> client_right
)
7944 maxWidth
= client_right
- rect
.x
;
7946 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7948 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7949 // may have changed earlier
7950 for (int i
= col
+ cell_cols
; i
< m_numCols
; i
++)
7953 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7955 // looks weird going over a multicell
7956 if (m_table
->IsEmptyCell( row
, i
) &&
7957 (rect
.width
< maxWidth
) && (c_rows
== 1))
7959 rect
.width
+= GetColWidth( i
);
7965 if (rect
.GetRight() > client_right
)
7966 rect
.SetRight( client_right
- 1 );
7969 editor
->SetCellAttr( attr
);
7970 editor
->SetSize( rect
);
7971 editor
->GetControl()->Move(
7972 editor
->GetControl()->GetPosition().x
+ nXMove
,
7973 editor
->GetControl()->GetPosition().y
);
7974 editor
->Show( true, attr
);
7977 for (int i
= 0; i
< m_currentCellCoords
.GetCol(); i
++)
7979 colXPos
+= GetColSize( i
);
7982 int xUnit
= 1, yUnit
= 1;
7983 GetScrollPixelsPerUnit( &xUnit
, &yUnit
);
7984 if (m_currentCellCoords
.GetCol() != 0)
7985 Scroll( colXPos
/ xUnit
- 1, GetScrollPos( wxVERTICAL
) );
7987 Scroll( colXPos
/ xUnit
, GetScrollPos( wxVERTICAL
) );
7989 // recalc dimensions in case we need to
7990 // expand the scrolled window to account for editor
7993 editor
->BeginEdit(row
, col
, this);
7994 editor
->SetCellAttr(NULL
);
8002 void wxGrid::HideCellEditControl()
8004 if ( IsCellEditControlEnabled() )
8006 int row
= m_currentCellCoords
.GetRow();
8007 int col
= m_currentCellCoords
.GetCol();
8009 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
8010 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
8011 editor
->Show( false );
8015 m_gridWin
->SetFocus();
8017 // refresh whole row to the right
8018 wxRect
rect( CellToRect(row
, col
) );
8019 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
8020 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
8023 // ensure that the pixels under the focus ring get refreshed as well
8024 rect
.Inflate(10, 10);
8027 m_gridWin
->Refresh( false, &rect
);
8031 void wxGrid::SaveEditControlValue()
8033 if ( IsCellEditControlEnabled() )
8035 int row
= m_currentCellCoords
.GetRow();
8036 int col
= m_currentCellCoords
.GetCol();
8038 wxString oldval
= GetCellValue(row
, col
);
8040 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
8041 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
8042 bool changed
= editor
->EndEdit(row
, col
, this);
8049 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
8050 m_currentCellCoords
.GetRow(),
8051 m_currentCellCoords
.GetCol() ) < 0 )
8053 // Event has been vetoed, set the data back.
8054 SetCellValue(row
, col
, oldval
);
8061 // ------ Grid location functions
8062 // Note that all of these functions work with the logical coordinates of
8063 // grid cells and labels so you will need to convert from device
8064 // coordinates for mouse events etc.
8067 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
8069 int row
= YToRow(y
);
8070 int col
= XToCol(x
);
8072 if ( row
== -1 || col
== -1 )
8074 coords
= wxGridNoCellCoords
;
8078 coords
.Set( row
, col
);
8082 // Internal Helper function for computing row or column from some
8083 // (unscrolled) coordinate value, using either
8084 // m_defaultRowHeight/m_defaultColWidth or binary search on array
8085 // of m_rowBottoms/m_ColRights to speed up the search!
8087 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
8088 const wxArrayInt
& BorderArray
, int nMax
,
8092 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
8097 size_t i_max
= coord
/ defaultDist
,
8100 if (BorderArray
.IsEmpty())
8102 if ((int) i_max
< nMax
)
8104 return clipToMinMax
? nMax
- 1 : -1;
8107 if ( i_max
>= BorderArray
.GetCount())
8109 i_max
= BorderArray
.GetCount() - 1;
8113 if ( coord
>= BorderArray
[i_max
])
8117 i_max
= coord
/ minDist
;
8119 i_max
= BorderArray
.GetCount() - 1;
8122 if ( i_max
>= BorderArray
.GetCount())
8123 i_max
= BorderArray
.GetCount() - 1;
8126 if ( coord
>= BorderArray
[i_max
])
8127 return clipToMinMax
? (int)i_max
: -1;
8128 if ( coord
< BorderArray
[0] )
8131 while ( i_max
- i_min
> 0 )
8133 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
8134 0, _T("wxGrid: internal error in CoordToRowOrCol"));
8135 if (coord
>= BorderArray
[ i_max
- 1])
8139 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
8140 if (coord
< BorderArray
[median
])
8149 int wxGrid::YToRow( int y
)
8151 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8152 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8155 int wxGrid::XToCol( int x
)
8157 return CoordToRowOrCol(x
, m_defaultColWidth
,
8158 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8161 // return the row number that that the y coord is near the edge of, or
8162 // -1 if not near an edge
8164 int wxGrid::YToEdgeOfRow( int y
)
8167 i
= internalYToRow(y
);
8169 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8171 // We know that we are in row i, test whether we are
8172 // close enough to lower or upper border, respectively.
8173 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8175 else if ( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8182 // return the col number that that the x coord is near the edge of, or
8183 // -1 if not near an edge
8185 int wxGrid::XToEdgeOfCol( int x
)
8188 i
= internalXToCol(x
);
8190 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8192 // We know that we are in column i, test whether we are
8193 // close enough to right or left border, respectively.
8194 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8196 else if ( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8203 wxRect
wxGrid::CellToRect( int row
, int col
)
8205 wxRect
rect( -1, -1, -1, -1 );
8207 if ( row
>= 0 && row
< m_numRows
&&
8208 col
>= 0 && col
< m_numCols
)
8210 int i
, cell_rows
, cell_cols
;
8211 rect
.width
= rect
.height
= 0;
8212 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8213 // if negative then find multicell owner
8218 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8220 rect
.x
= GetColLeft(col
);
8221 rect
.y
= GetRowTop(row
);
8222 for (i
=col
; i
< col
+ cell_cols
; i
++)
8223 rect
.width
+= GetColWidth(i
);
8224 for (i
=row
; i
< row
+ cell_rows
; i
++)
8225 rect
.height
+= GetRowHeight(i
);
8228 // if grid lines are enabled, then the area of the cell is a bit smaller
8229 if (m_gridLinesEnabled
)
8238 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8240 // get the cell rectangle in logical coords
8242 wxRect
r( CellToRect( row
, col
) );
8244 // convert to device coords
8246 int left
, top
, right
, bottom
;
8247 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8248 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8250 // check against the client area of the grid window
8252 m_gridWin
->GetClientSize( &cw
, &ch
);
8254 if ( wholeCellVisible
)
8256 // is the cell wholly visible ?
8258 return ( left
>= 0 && right
<= cw
&&
8259 top
>= 0 && bottom
<= ch
);
8263 // is the cell partly visible ?
8265 return ( ((left
>= 0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8266 ((top
>= 0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8270 // make the specified cell location visible by doing a minimal amount
8273 void wxGrid::MakeCellVisible( int row
, int col
)
8276 int xpos
= -1, ypos
= -1;
8278 if ( row
>= 0 && row
< m_numRows
&&
8279 col
>= 0 && col
< m_numCols
)
8281 // get the cell rectangle in logical coords
8282 wxRect
r( CellToRect( row
, col
) );
8284 // convert to device coords
8285 int left
, top
, right
, bottom
;
8286 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8287 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8290 m_gridWin
->GetClientSize( &cw
, &ch
);
8296 else if ( bottom
> ch
)
8298 int h
= r
.GetHeight();
8300 for ( i
= row
-1; i
>= 0; i
-- )
8302 int rowHeight
= GetRowHeight(i
);
8303 if ( h
+ rowHeight
> ch
)
8310 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8311 // have rounding errors (this is important, because if we do, we
8312 // might not scroll at all and some cells won't be redrawn)
8314 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8316 ypos
+= m_scrollLineY
;
8319 // special handling for wide cells - show always left part of the cell!
8320 // Otherwise, e.g. when stepping from row to row, it would jump between
8321 // left and right part of the cell on every step!
8323 if ( left
< 0 || (right
-left
) >= cw
)
8327 else if ( right
> cw
)
8329 // position the view so that the cell is on the right
8331 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8332 xpos
= x0
+ (right
- cw
);
8334 // see comment for ypos above
8335 xpos
+= m_scrollLineX
;
8338 if ( xpos
!= -1 || ypos
!= -1 )
8341 xpos
/= m_scrollLineX
;
8343 ypos
/= m_scrollLineY
;
8344 Scroll( xpos
, ypos
);
8351 // ------ Grid cursor movement functions
8354 bool wxGrid::MoveCursorUp( bool expandSelection
)
8356 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8357 m_currentCellCoords
.GetRow() >= 0 )
8359 if ( expandSelection
)
8361 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8362 m_selectingKeyboard
= m_currentCellCoords
;
8363 if ( m_selectingKeyboard
.GetRow() > 0 )
8365 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8366 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8367 m_selectingKeyboard
.GetCol() );
8368 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8371 else if ( m_currentCellCoords
.GetRow() > 0 )
8373 int row
= m_currentCellCoords
.GetRow() - 1;
8374 int col
= m_currentCellCoords
.GetCol();
8376 MakeCellVisible( row
, col
);
8377 SetCurrentCell( row
, col
);
8388 bool wxGrid::MoveCursorDown( bool expandSelection
)
8390 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8391 m_currentCellCoords
.GetRow() < m_numRows
)
8393 if ( expandSelection
)
8395 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8396 m_selectingKeyboard
= m_currentCellCoords
;
8397 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8399 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8400 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8401 m_selectingKeyboard
.GetCol() );
8402 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8405 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8407 int row
= m_currentCellCoords
.GetRow() + 1;
8408 int col
= m_currentCellCoords
.GetCol();
8410 MakeCellVisible( row
, col
);
8411 SetCurrentCell( row
, col
);
8422 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8424 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8425 m_currentCellCoords
.GetCol() >= 0 )
8427 if ( expandSelection
)
8429 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8430 m_selectingKeyboard
= m_currentCellCoords
;
8431 if ( m_selectingKeyboard
.GetCol() > 0 )
8433 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8434 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8435 m_selectingKeyboard
.GetCol() );
8436 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8439 else if ( m_currentCellCoords
.GetCol() > 0 )
8441 int row
= m_currentCellCoords
.GetRow();
8442 int col
= m_currentCellCoords
.GetCol() - 1;
8444 MakeCellVisible( row
, col
);
8445 SetCurrentCell( row
, col
);
8456 bool wxGrid::MoveCursorRight( bool expandSelection
)
8458 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8459 m_currentCellCoords
.GetCol() < m_numCols
)
8461 if ( expandSelection
)
8463 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8464 m_selectingKeyboard
= m_currentCellCoords
;
8465 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8467 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8468 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8469 m_selectingKeyboard
.GetCol() );
8470 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8473 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8475 int row
= m_currentCellCoords
.GetRow();
8476 int col
= m_currentCellCoords
.GetCol() + 1;
8478 MakeCellVisible( row
, col
);
8479 SetCurrentCell( row
, col
);
8490 bool wxGrid::MovePageUp()
8492 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8495 int row
= m_currentCellCoords
.GetRow();
8499 m_gridWin
->GetClientSize( &cw
, &ch
);
8501 int y
= GetRowTop(row
);
8502 int newRow
= internalYToRow( y
- ch
+ 1 );
8504 if ( newRow
== row
)
8506 //row > 0 , so newrow can never be less than 0 here.
8510 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8511 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8519 bool wxGrid::MovePageDown()
8521 if ( m_currentCellCoords
== wxGridNoCellCoords
)
8524 int row
= m_currentCellCoords
.GetRow();
8525 if ( (row
+1) < m_numRows
)
8528 m_gridWin
->GetClientSize( &cw
, &ch
);
8530 int y
= GetRowTop(row
);
8531 int newRow
= internalYToRow( y
+ ch
);
8532 if ( newRow
== row
)
8534 // row < m_numRows , so newrow can't overflow here.
8538 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8539 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8547 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8550 m_currentCellCoords
!= wxGridNoCellCoords
&&
8551 m_currentCellCoords
.GetRow() > 0 )
8553 int row
= m_currentCellCoords
.GetRow();
8554 int col
= m_currentCellCoords
.GetCol();
8556 if ( m_table
->IsEmptyCell(row
, col
) )
8558 // starting in an empty cell: find the next block of
8564 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8568 else if ( m_table
->IsEmptyCell(row
- 1, col
) )
8570 // starting at the top of a block: find the next block
8576 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8582 // starting within a block: find the top of the block
8587 if ( m_table
->IsEmptyCell(row
, col
) )
8595 MakeCellVisible( row
, col
);
8596 if ( expandSelection
)
8598 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8599 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8604 SetCurrentCell( row
, col
);
8613 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8616 m_currentCellCoords
!= wxGridNoCellCoords
&&
8617 m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8619 int row
= m_currentCellCoords
.GetRow();
8620 int col
= m_currentCellCoords
.GetCol();
8622 if ( m_table
->IsEmptyCell(row
, col
) )
8624 // starting in an empty cell: find the next block of
8627 while ( row
< m_numRows
- 1 )
8630 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8634 else if ( m_table
->IsEmptyCell(row
+ 1, col
) )
8636 // starting at the bottom of a block: find the next block
8639 while ( row
< m_numRows
- 1 )
8642 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8648 // starting within a block: find the bottom of the block
8650 while ( row
< m_numRows
- 1 )
8653 if ( m_table
->IsEmptyCell(row
, col
) )
8661 MakeCellVisible( row
, col
);
8662 if ( expandSelection
)
8664 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8665 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8670 SetCurrentCell( row
, col
);
8679 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8682 m_currentCellCoords
!= wxGridNoCellCoords
&&
8683 m_currentCellCoords
.GetCol() > 0 )
8685 int row
= m_currentCellCoords
.GetRow();
8686 int col
= m_currentCellCoords
.GetCol();
8688 if ( m_table
->IsEmptyCell(row
, col
) )
8690 // starting in an empty cell: find the next block of
8696 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8700 else if ( m_table
->IsEmptyCell(row
, col
- 1) )
8702 // starting at the left of a block: find the next block
8708 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8714 // starting within a block: find the left of the block
8719 if ( m_table
->IsEmptyCell(row
, col
) )
8727 MakeCellVisible( row
, col
);
8728 if ( expandSelection
)
8730 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8731 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8736 SetCurrentCell( row
, col
);
8745 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8748 m_currentCellCoords
!= wxGridNoCellCoords
&&
8749 m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8751 int row
= m_currentCellCoords
.GetRow();
8752 int col
= m_currentCellCoords
.GetCol();
8754 if ( m_table
->IsEmptyCell(row
, col
) )
8756 // starting in an empty cell: find the next block of
8759 while ( col
< m_numCols
- 1 )
8762 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8766 else if ( m_table
->IsEmptyCell(row
, col
+ 1) )
8768 // starting at the right of a block: find the next block
8771 while ( col
< m_numCols
- 1 )
8774 if ( !(m_table
->IsEmptyCell(row
, col
)) )
8780 // starting within a block: find the right of the block
8782 while ( col
< m_numCols
- 1 )
8785 if ( m_table
->IsEmptyCell(row
, col
) )
8793 MakeCellVisible( row
, col
);
8794 if ( expandSelection
)
8796 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8797 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8802 SetCurrentCell( row
, col
);
8812 // ------ Label values and formatting
8815 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8818 *horiz
= m_rowLabelHorizAlign
;
8820 *vert
= m_rowLabelVertAlign
;
8823 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8826 *horiz
= m_colLabelHorizAlign
;
8828 *vert
= m_colLabelVertAlign
;
8831 int wxGrid::GetColLabelTextOrientation()
8833 return m_colLabelTextOrientation
;
8836 wxString
wxGrid::GetRowLabelValue( int row
)
8840 return m_table
->GetRowLabelValue( row
);
8850 wxString
wxGrid::GetColLabelValue( int col
)
8854 return m_table
->GetColLabelValue( col
);
8864 void wxGrid::SetRowLabelSize( int width
)
8866 width
= wxMax( width
, 0 );
8867 if ( width
!= m_rowLabelWidth
)
8871 m_rowLabelWin
->Show( false );
8872 m_cornerLabelWin
->Show( false );
8874 else if ( m_rowLabelWidth
== 0 )
8876 m_rowLabelWin
->Show( true );
8877 if ( m_colLabelHeight
> 0 )
8878 m_cornerLabelWin
->Show( true );
8881 m_rowLabelWidth
= width
;
8883 wxScrolledWindow::Refresh( true );
8887 void wxGrid::SetColLabelSize( int height
)
8889 height
= wxMax( height
, 0 );
8890 if ( height
!= m_colLabelHeight
)
8894 m_colLabelWin
->Show( false );
8895 m_cornerLabelWin
->Show( false );
8897 else if ( m_colLabelHeight
== 0 )
8899 m_colLabelWin
->Show( true );
8900 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8903 m_colLabelHeight
= height
;
8905 wxScrolledWindow::Refresh( true );
8909 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8911 if ( m_labelBackgroundColour
!= colour
)
8913 m_labelBackgroundColour
= colour
;
8914 m_rowLabelWin
->SetBackgroundColour( colour
);
8915 m_colLabelWin
->SetBackgroundColour( colour
);
8916 m_cornerLabelWin
->SetBackgroundColour( colour
);
8918 if ( !GetBatchCount() )
8920 m_rowLabelWin
->Refresh();
8921 m_colLabelWin
->Refresh();
8922 m_cornerLabelWin
->Refresh();
8927 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8929 if ( m_labelTextColour
!= colour
)
8931 m_labelTextColour
= colour
;
8932 if ( !GetBatchCount() )
8934 m_rowLabelWin
->Refresh();
8935 m_colLabelWin
->Refresh();
8940 void wxGrid::SetLabelFont( const wxFont
& font
)
8943 if ( !GetBatchCount() )
8945 m_rowLabelWin
->Refresh();
8946 m_colLabelWin
->Refresh();
8950 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8952 // allow old (incorrect) defs to be used
8955 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8956 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8957 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8962 case wxTOP
: vert
= wxALIGN_TOP
; break;
8963 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8964 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8967 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8969 m_rowLabelHorizAlign
= horiz
;
8972 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8974 m_rowLabelVertAlign
= vert
;
8977 if ( !GetBatchCount() )
8979 m_rowLabelWin
->Refresh();
8983 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8985 // allow old (incorrect) defs to be used
8988 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8989 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8990 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8995 case wxTOP
: vert
= wxALIGN_TOP
; break;
8996 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8997 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
9000 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
9002 m_colLabelHorizAlign
= horiz
;
9005 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
9007 m_colLabelVertAlign
= vert
;
9010 if ( !GetBatchCount() )
9012 m_colLabelWin
->Refresh();
9016 // Note: under MSW, the default column label font must be changed because it
9017 // does not support vertical printing
9019 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
9020 // pGrid->SetLabelFont(font);
9021 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
9023 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
9025 if ( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
9027 m_colLabelTextOrientation
= textOrientation
;
9030 if ( !GetBatchCount() )
9032 m_colLabelWin
->Refresh();
9036 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
9040 m_table
->SetRowLabelValue( row
, s
);
9041 if ( !GetBatchCount() )
9043 wxRect rect
= CellToRect( row
, 0);
9044 if ( rect
.height
> 0 )
9046 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
9048 rect
.width
= m_rowLabelWidth
;
9049 m_rowLabelWin
->Refresh( true, &rect
);
9055 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
9059 m_table
->SetColLabelValue( col
, s
);
9060 if ( !GetBatchCount() )
9062 wxRect rect
= CellToRect( 0, col
);
9063 if ( rect
.width
> 0 )
9065 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
9067 rect
.height
= m_colLabelHeight
;
9068 m_colLabelWin
->Refresh( true, &rect
);
9074 void wxGrid::SetGridLineColour( const wxColour
& colour
)
9076 if ( m_gridLineColour
!= colour
)
9078 m_gridLineColour
= colour
;
9080 wxClientDC
dc( m_gridWin
);
9082 DrawAllGridLines( dc
, wxRegion() );
9086 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
9088 if ( m_cellHighlightColour
!= colour
)
9090 m_cellHighlightColour
= colour
;
9092 wxClientDC
dc( m_gridWin
);
9094 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
9095 DrawCellHighlight(dc
, attr
);
9100 void wxGrid::SetCellHighlightPenWidth(int width
)
9102 if (m_cellHighlightPenWidth
!= width
)
9104 m_cellHighlightPenWidth
= width
;
9106 // Just redrawing the cell highlight is not enough since that won't
9107 // make any visible change if the the thickness is getting smaller.
9108 int row
= m_currentCellCoords
.GetRow();
9109 int col
= m_currentCellCoords
.GetCol();
9110 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9113 wxRect rect
= CellToRect(row
, col
);
9114 m_gridWin
->Refresh(true, &rect
);
9118 void wxGrid::SetCellHighlightROPenWidth(int width
)
9120 if (m_cellHighlightROPenWidth
!= width
)
9122 m_cellHighlightROPenWidth
= width
;
9124 // Just redrawing the cell highlight is not enough since that won't
9125 // make any visible change if the the thickness is getting smaller.
9126 int row
= m_currentCellCoords
.GetRow();
9127 int col
= m_currentCellCoords
.GetCol();
9128 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
9130 wxRect rect
= CellToRect(row
, col
);
9131 m_gridWin
->Refresh(true, &rect
);
9135 void wxGrid::EnableGridLines( bool enable
)
9137 if ( enable
!= m_gridLinesEnabled
)
9139 m_gridLinesEnabled
= enable
;
9141 if ( !GetBatchCount() )
9145 wxClientDC
dc( m_gridWin
);
9147 DrawAllGridLines( dc
, wxRegion() );
9151 m_gridWin
->Refresh();
9157 int wxGrid::GetDefaultRowSize()
9159 return m_defaultRowHeight
;
9162 int wxGrid::GetRowSize( int row
)
9164 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9166 return GetRowHeight(row
);
9169 int wxGrid::GetDefaultColSize()
9171 return m_defaultColWidth
;
9174 int wxGrid::GetColSize( int col
)
9176 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9178 return GetColWidth(col
);
9181 // ============================================================================
9182 // access to the grid attributes: each of them has a default value in the grid
9183 // itself and may be overidden on a per-cell basis
9184 // ============================================================================
9186 // ----------------------------------------------------------------------------
9187 // setting default attributes
9188 // ----------------------------------------------------------------------------
9190 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9192 m_defaultCellAttr
->SetBackgroundColour(col
);
9194 m_gridWin
->SetBackgroundColour(col
);
9198 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9200 m_defaultCellAttr
->SetTextColour(col
);
9203 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9205 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9208 void wxGrid::SetDefaultCellOverflow( bool allow
)
9210 m_defaultCellAttr
->SetOverflow(allow
);
9213 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9215 m_defaultCellAttr
->SetFont(font
);
9218 // For editors and renderers the type registry takes precedence over the
9219 // default attr, so we need to register the new editor/renderer for the string
9220 // data type in order to make setting a default editor/renderer appear to
9223 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9225 RegisterDataType(wxGRID_VALUE_STRING
,
9227 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9230 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9232 RegisterDataType(wxGRID_VALUE_STRING
,
9233 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9237 // ----------------------------------------------------------------------------
9238 // access to the default attrbiutes
9239 // ----------------------------------------------------------------------------
9241 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9243 return m_defaultCellAttr
->GetBackgroundColour();
9246 wxColour
wxGrid::GetDefaultCellTextColour()
9248 return m_defaultCellAttr
->GetTextColour();
9251 wxFont
wxGrid::GetDefaultCellFont()
9253 return m_defaultCellAttr
->GetFont();
9256 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9258 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9261 bool wxGrid::GetDefaultCellOverflow()
9263 return m_defaultCellAttr
->GetOverflow();
9266 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9268 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9271 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9273 return m_defaultCellAttr
->GetEditor(NULL
, 0, 0);
9276 // ----------------------------------------------------------------------------
9277 // access to cell attributes
9278 // ----------------------------------------------------------------------------
9280 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9282 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9283 wxColour colour
= attr
->GetBackgroundColour();
9289 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9291 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9292 wxColour colour
= attr
->GetTextColour();
9298 wxFont
wxGrid::GetCellFont( int row
, int col
)
9300 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9301 wxFont font
= attr
->GetFont();
9307 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9309 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9310 attr
->GetAlignment(horiz
, vert
);
9314 bool wxGrid::GetCellOverflow( int row
, int col
)
9316 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9317 bool allow
= attr
->GetOverflow();
9323 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9325 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9326 attr
->GetSize( num_rows
, num_cols
);
9330 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9332 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9333 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9339 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9341 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9342 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9348 bool wxGrid::IsReadOnly(int row
, int col
) const
9350 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9351 bool isReadOnly
= attr
->IsReadOnly();
9357 // ----------------------------------------------------------------------------
9358 // attribute support: cache, automatic provider creation, ...
9359 // ----------------------------------------------------------------------------
9361 bool wxGrid::CanHaveAttributes()
9368 return m_table
->CanHaveAttributes();
9371 void wxGrid::ClearAttrCache()
9373 if ( m_attrCache
.row
!= -1 )
9375 wxSafeDecRef(m_attrCache
.attr
);
9376 m_attrCache
.attr
= NULL
;
9377 m_attrCache
.row
= -1;
9381 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9385 wxGrid
*self
= (wxGrid
*)this; // const_cast
9387 self
->ClearAttrCache();
9388 self
->m_attrCache
.row
= row
;
9389 self
->m_attrCache
.col
= col
;
9390 self
->m_attrCache
.attr
= attr
;
9395 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9397 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9399 *attr
= m_attrCache
.attr
;
9400 wxSafeIncRef(m_attrCache
.attr
);
9402 #ifdef DEBUG_ATTR_CACHE
9403 gs_nAttrCacheHits
++;
9410 #ifdef DEBUG_ATTR_CACHE
9411 gs_nAttrCacheMisses
++;
9418 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9420 wxGridCellAttr
*attr
= NULL
;
9421 // Additional test to avoid looking at the cache e.g. for
9422 // wxNoCellCoords, as this will confuse memory management.
9425 if ( !LookupAttr(row
, col
, &attr
) )
9427 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9428 : (wxGridCellAttr
*)NULL
;
9429 CacheAttr(row
, col
, attr
);
9435 attr
->SetDefAttr(m_defaultCellAttr
);
9439 attr
= m_defaultCellAttr
;
9446 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9448 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9449 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9451 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9452 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9454 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9457 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9459 // artificially inc the ref count to match DecRef() in caller
9461 m_table
->SetAttr(attr
, row
, col
);
9467 // ----------------------------------------------------------------------------
9468 // setting column attributes (wrappers around SetColAttr)
9469 // ----------------------------------------------------------------------------
9471 void wxGrid::SetColFormatBool(int col
)
9473 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9476 void wxGrid::SetColFormatNumber(int col
)
9478 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9481 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9483 wxString typeName
= wxGRID_VALUE_FLOAT
;
9484 if ( (width
!= -1) || (precision
!= -1) )
9486 typeName
<< _T(':') << width
<< _T(',') << precision
;
9489 SetColFormatCustom(col
, typeName
);
9492 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9494 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9496 attr
= new wxGridCellAttr
;
9497 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9498 attr
->SetRenderer(renderer
);
9500 SetColAttr(col
, attr
);
9504 // ----------------------------------------------------------------------------
9505 // setting cell attributes: this is forwarded to the table
9506 // ----------------------------------------------------------------------------
9508 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9510 if ( CanHaveAttributes() )
9512 m_table
->SetAttr(attr
, row
, col
);
9521 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9523 if ( CanHaveAttributes() )
9525 m_table
->SetRowAttr(attr
, row
);
9534 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9536 if ( CanHaveAttributes() )
9538 m_table
->SetColAttr(attr
, col
);
9547 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9549 if ( CanHaveAttributes() )
9551 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9552 attr
->SetBackgroundColour(colour
);
9557 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9559 if ( CanHaveAttributes() )
9561 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9562 attr
->SetTextColour(colour
);
9567 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9569 if ( CanHaveAttributes() )
9571 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9572 attr
->SetFont(font
);
9577 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9579 if ( CanHaveAttributes() )
9581 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9582 attr
->SetAlignment(horiz
, vert
);
9587 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9589 if ( CanHaveAttributes() )
9591 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9592 attr
->SetOverflow(allow
);
9597 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9599 if ( CanHaveAttributes() )
9601 int cell_rows
, cell_cols
;
9603 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9604 attr
->GetSize(&cell_rows
, &cell_cols
);
9605 attr
->SetSize(num_rows
, num_cols
);
9608 // Cannot set the size of a cell to 0 or negative values
9609 // While it is perfectly legal to do that, this function cannot
9610 // handle all the possibilies, do it by hand by getting the CellAttr.
9611 // You can only set the size of a cell to 1,1 or greater with this fn
9612 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9613 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9614 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9615 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9617 // if this was already a multicell then "turn off" the other cells first
9618 if ((cell_rows
> 1) || (cell_rows
> 1))
9621 for (j
=row
; j
< row
+ cell_rows
; j
++)
9623 for (i
=col
; i
< col
+ cell_cols
; i
++)
9625 if ((i
!= col
) || (j
!= row
))
9627 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9628 attr_stub
->SetSize( 1, 1 );
9629 attr_stub
->DecRef();
9635 // mark the cells that will be covered by this cell to
9636 // negative or zero values to point back at this cell
9637 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9640 for (j
=row
; j
< row
+ num_rows
; j
++)
9642 for (i
=col
; i
< col
+ num_cols
; i
++)
9644 if ((i
!= col
) || (j
!= row
))
9646 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9647 attr_stub
->SetSize( row
- j
, col
- i
);
9648 attr_stub
->DecRef();
9656 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9658 if ( CanHaveAttributes() )
9660 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9661 attr
->SetRenderer(renderer
);
9666 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9668 if ( CanHaveAttributes() )
9670 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9671 attr
->SetEditor(editor
);
9676 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9678 if ( CanHaveAttributes() )
9680 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9681 attr
->SetReadOnly(isReadOnly
);
9686 // ----------------------------------------------------------------------------
9687 // Data type registration
9688 // ----------------------------------------------------------------------------
9690 void wxGrid::RegisterDataType(const wxString
& typeName
,
9691 wxGridCellRenderer
* renderer
,
9692 wxGridCellEditor
* editor
)
9694 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9698 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9700 wxString typeName
= m_table
->GetTypeName(row
, col
);
9701 return GetDefaultEditorForType(typeName
);
9704 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9706 wxString typeName
= m_table
->GetTypeName(row
, col
);
9707 return GetDefaultRendererForType(typeName
);
9710 wxGridCellEditor
* wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9712 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9713 if ( index
== wxNOT_FOUND
)
9717 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9718 wxFAIL_MSG(errStr
.c_str());
9723 return m_typeRegistry
->GetEditor(index
);
9726 wxGridCellRenderer
* wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9728 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9729 if ( index
== wxNOT_FOUND
)
9733 errStr
.Printf(wxT("Unknown data type name [%s]"), typeName
.c_str());
9734 wxFAIL_MSG(errStr
.c_str());
9739 return m_typeRegistry
->GetRenderer(index
);
9742 // ----------------------------------------------------------------------------
9744 // ----------------------------------------------------------------------------
9746 void wxGrid::EnableDragRowSize( bool enable
)
9748 m_canDragRowSize
= enable
;
9751 void wxGrid::EnableDragColSize( bool enable
)
9753 m_canDragColSize
= enable
;
9756 void wxGrid::EnableDragGridSize( bool enable
)
9758 m_canDragGridSize
= enable
;
9761 void wxGrid::EnableDragCell( bool enable
)
9763 m_canDragCell
= enable
;
9766 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9768 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9770 if ( resizeExistingRows
)
9772 // since we are resizing all rows to the default row size,
9773 // we can simply clear the row heights and row bottoms
9774 // arrays (which also allows us to take advantage of
9775 // some speed optimisations)
9776 m_rowHeights
.Empty();
9777 m_rowBottoms
.Empty();
9778 if ( !GetBatchCount() )
9783 void wxGrid::SetRowSize( int row
, int height
)
9785 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9787 // See comment in SetColSize
9788 if ( height
< GetRowMinimalAcceptableHeight())
9791 if ( m_rowHeights
.IsEmpty() )
9793 // need to really create the array
9797 int h
= wxMax( 0, height
);
9798 int diff
= h
- m_rowHeights
[row
];
9800 m_rowHeights
[row
] = h
;
9802 for ( i
= row
; i
< m_numRows
; i
++ )
9804 m_rowBottoms
[i
] += diff
;
9807 if ( !GetBatchCount() )
9811 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9813 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9815 if ( resizeExistingCols
)
9817 // since we are resizing all columns to the default column size,
9818 // we can simply clear the col widths and col rights
9819 // arrays (which also allows us to take advantage of
9820 // some speed optimisations)
9821 m_colWidths
.Empty();
9822 m_colRights
.Empty();
9823 if ( !GetBatchCount() )
9828 void wxGrid::SetColSize( int col
, int width
)
9830 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9832 // should we check that it's bigger than GetColMinimalWidth(col) here?
9834 // No, because it is reasonable to assume the library user know's
9835 // what he is doing. However whe should test against the weaker
9836 // constariant of minimalAcceptableWidth, as this breaks rendering
9838 // This test then fixes sf.net bug #645734
9840 if ( width
< GetColMinimalAcceptableWidth() )
9843 if ( m_colWidths
.IsEmpty() )
9845 // need to really create the array
9849 // if < 0 calc new width from label
9853 wxArrayString lines
;
9854 wxClientDC
dc(m_colLabelWin
);
9855 dc
.SetFont(GetLabelFont());
9856 StringToLines(GetColLabelValue(col
), lines
);
9857 GetTextBoxSize(dc
, lines
, &w
, &h
);
9861 int w
= wxMax( 0, width
);
9862 int diff
= w
- m_colWidths
[col
];
9863 m_colWidths
[col
] = w
;
9866 for ( i
= col
; i
< m_numCols
; i
++ )
9868 m_colRights
[i
] += diff
;
9871 if ( !GetBatchCount() )
9875 void wxGrid::SetColMinimalWidth( int col
, int width
)
9877 if (width
> GetColMinimalAcceptableWidth())
9879 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9880 m_colMinWidths
[key
] = width
;
9884 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9886 if (width
> GetRowMinimalAcceptableHeight())
9888 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9889 m_rowMinHeights
[key
] = width
;
9893 int wxGrid::GetColMinimalWidth(int col
) const
9895 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9896 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9898 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9901 int wxGrid::GetRowMinimalHeight(int row
) const
9903 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9904 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9906 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9909 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9911 // We do allow a width of 0 since this gives us
9912 // an easy way to temporarily hiding columns.
9914 m_minAcceptableColWidth
= width
;
9917 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9919 // We do allow a height of 0 since this gives us
9920 // an easy way to temporarily hiding rows.
9922 m_minAcceptableRowHeight
= height
;
9925 int wxGrid::GetColMinimalAcceptableWidth() const
9927 return m_minAcceptableColWidth
;
9930 int wxGrid::GetRowMinimalAcceptableHeight() const
9932 return m_minAcceptableRowHeight
;
9935 // ----------------------------------------------------------------------------
9937 // ----------------------------------------------------------------------------
9939 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9941 wxClientDC
dc(m_gridWin
);
9943 // cancel editing of cell
9944 HideCellEditControl();
9945 SaveEditControlValue();
9947 // init both of them to avoid compiler warnings, even if we only need one
9955 wxCoord extent
, extentMax
= 0;
9956 int max
= column
? m_numRows
: m_numCols
;
9957 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9964 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9965 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
9968 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9969 extent
= column
? size
.x
: size
.y
;
9970 if ( extent
> extentMax
)
9981 // now also compare with the column label extent
9983 dc
.SetFont( GetLabelFont() );
9987 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9988 if ( GetColLabelTextOrientation() == wxVERTICAL
)
9992 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9994 extent
= column
? w
: h
;
9995 if ( extent
> extentMax
)
10002 // empty column - give default extent (notice that if extentMax is less
10003 // than default extent but != 0, it's OK)
10004 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
10009 // leave some space around text
10017 SetColSize( col
, extentMax
);
10018 if ( !GetBatchCount() )
10021 m_gridWin
->GetClientSize( &cw
, &ch
);
10022 wxRect
rect ( CellToRect( 0, col
) );
10024 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
10025 rect
.width
= cw
- rect
.x
;
10026 rect
.height
= m_colLabelHeight
;
10027 m_colLabelWin
->Refresh( true, &rect
);
10032 SetRowSize(row
, extentMax
);
10033 if ( !GetBatchCount() )
10036 m_gridWin
->GetClientSize( &cw
, &ch
);
10037 wxRect
rect ( CellToRect( row
, 0 ) );
10039 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10040 rect
.width
= m_rowLabelWidth
;
10041 rect
.height
= ch
- rect
.y
;
10042 m_rowLabelWin
->Refresh( true, &rect
);
10049 SetColMinimalWidth(col
, extentMax
);
10051 SetRowMinimalHeight(row
, extentMax
);
10055 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
10057 int width
= m_rowLabelWidth
;
10062 for ( int col
= 0; col
< m_numCols
; col
++ )
10066 AutoSizeColumn(col
, setAsMin
);
10069 width
+= GetColWidth(col
);
10078 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
10080 int height
= m_colLabelHeight
;
10085 for ( int row
= 0; row
< m_numRows
; row
++ )
10088 AutoSizeRow(row
, setAsMin
);
10090 height
+= GetRowHeight(row
);
10099 void wxGrid::AutoSize()
10103 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
10105 // round up the size to a multiple of scroll step - this ensures that we
10106 // won't get the scrollbars if we're sized exactly to this width
10107 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
10110 GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
10111 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
10113 // distribute the extra space between the columns/rows to avoid having
10114 // extra white space
10116 // Remove the extra m_extraWidth + 1 added above
10117 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
10118 if ( diff
&& m_numCols
)
10120 // try to resize the columns uniformly
10121 wxCoord diffPerCol
= diff
/ m_numCols
;
10124 for ( int col
= 0; col
< m_numCols
; col
++ )
10126 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
10130 // add remaining amount to the last columns
10131 diff
-= diffPerCol
* m_numCols
;
10134 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
10136 SetColSize(col
, GetColWidth(col
) + 1);
10142 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
10143 if ( diff
&& m_numRows
)
10145 // try to resize the columns uniformly
10146 wxCoord diffPerRow
= diff
/ m_numRows
;
10149 for ( int row
= 0; row
< m_numRows
; row
++ )
10151 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
10155 // add remaining amount to the last rows
10156 diff
-= diffPerRow
* m_numRows
;
10159 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
10161 SetRowSize(row
, GetRowHeight(row
) + 1);
10168 SetClientSize(sizeFit
);
10171 void wxGrid::AutoSizeRowLabelSize( int row
)
10173 wxArrayString lines
;
10176 // Hide the edit control, so it
10177 // won't interfere with drag-shrinking.
10178 if ( IsCellEditControlShown() )
10180 HideCellEditControl();
10181 SaveEditControlValue();
10184 // autosize row height depending on label text
10185 StringToLines( GetRowLabelValue( row
), lines
);
10186 wxClientDC
dc( m_rowLabelWin
);
10187 GetTextBoxSize( dc
, lines
, &w
, &h
);
10188 if ( h
< m_defaultRowHeight
)
10189 h
= m_defaultRowHeight
;
10190 SetRowSize(row
, h
);
10194 void wxGrid::AutoSizeColLabelSize( int col
)
10196 wxArrayString lines
;
10199 // Hide the edit control, so it
10200 // won't interfer with drag-shrinking.
10201 if ( IsCellEditControlShown() )
10203 HideCellEditControl();
10204 SaveEditControlValue();
10207 // autosize column width depending on label text
10208 StringToLines( GetColLabelValue( col
), lines
);
10209 wxClientDC
dc( m_colLabelWin
);
10210 if ( GetColLabelTextOrientation() == wxHORIZONTAL
)
10211 GetTextBoxSize( dc
, lines
, &w
, &h
);
10213 GetTextBoxSize( dc
, lines
, &h
, &w
);
10214 if ( w
< m_defaultColWidth
)
10215 w
= m_defaultColWidth
;
10216 SetColSize(col
, w
);
10220 wxSize
wxGrid::DoGetBestSize() const
10222 // don't set sizes, only calculate them
10223 wxGrid
*self
= (wxGrid
*)this; // const_cast
10226 width
= self
->SetOrCalcColumnSizes(true);
10227 height
= self
->SetOrCalcRowSizes(true);
10234 // Round up to a multiple the scroll rate
10235 // NOTE: this still doesn't get rid of the scrollbars;
10236 // is there any magic incantation for that?
10238 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10240 width
+= 1 + xpu
- (width
% xpu
);
10242 height
+= 1 + ypu
- (height
% ypu
);
10244 // limit to 1/4 of the screen size
10245 int maxwidth
, maxheight
;
10246 wxDisplaySize( &maxwidth
, &maxheight
);
10249 if ( width
> maxwidth
)
10251 if ( height
> maxheight
)
10252 height
= maxheight
;
10254 wxSize
best(width
, height
);
10256 // NOTE: This size should be cached, but first we need to add calls to
10257 // InvalidateBestSize everywhere that could change the results of this
10259 // CacheBestSize(size);
10269 wxPen
& wxGrid::GetDividerPen() const
10274 // ----------------------------------------------------------------------------
10275 // cell value accessor functions
10276 // ----------------------------------------------------------------------------
10278 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10282 m_table
->SetValue( row
, col
, s
);
10283 if ( !GetBatchCount() )
10286 wxRect
rect( CellToRect( row
, col
) );
10288 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10289 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10290 m_gridWin
->Refresh( false, &rect
);
10293 if ( m_currentCellCoords
.GetRow() == row
&&
10294 m_currentCellCoords
.GetCol() == col
&&
10295 IsCellEditControlShown())
10296 // Note: If we are using IsCellEditControlEnabled,
10297 // this interacts badly with calling SetCellValue from
10298 // an EVT_GRID_CELL_CHANGE handler.
10300 HideCellEditControl();
10301 ShowCellEditControl(); // will reread data from table
10306 // ----------------------------------------------------------------------------
10307 // block, row and column selection
10308 // ----------------------------------------------------------------------------
10310 void wxGrid::SelectRow( int row
, bool addToSelected
)
10312 if ( IsSelection() && !addToSelected
)
10316 m_selection
->SelectRow( row
, false, addToSelected
);
10319 void wxGrid::SelectCol( int col
, bool addToSelected
)
10321 if ( IsSelection() && !addToSelected
)
10325 m_selection
->SelectCol( col
, false, addToSelected
);
10328 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10329 bool addToSelected
)
10331 if ( IsSelection() && !addToSelected
)
10335 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10336 false, addToSelected
);
10339 void wxGrid::SelectAll()
10341 if ( m_numRows
> 0 && m_numCols
> 0 )
10344 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10348 // ----------------------------------------------------------------------------
10349 // cell, row and col deselection
10350 // ----------------------------------------------------------------------------
10352 void wxGrid::DeselectRow( int row
)
10354 if ( !m_selection
)
10357 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10359 if ( m_selection
->IsInSelection(row
, 0 ) )
10360 m_selection
->ToggleCellSelection(row
, 0);
10364 int nCols
= GetNumberCols();
10365 for ( int i
= 0; i
< nCols
; i
++ )
10367 if ( m_selection
->IsInSelection(row
, i
) )
10368 m_selection
->ToggleCellSelection(row
, i
);
10373 void wxGrid::DeselectCol( int col
)
10375 if ( !m_selection
)
10378 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10380 if ( m_selection
->IsInSelection(0, col
) )
10381 m_selection
->ToggleCellSelection(0, col
);
10385 int nRows
= GetNumberRows();
10386 for ( int i
= 0; i
< nRows
; i
++ )
10388 if ( m_selection
->IsInSelection(i
, col
) )
10389 m_selection
->ToggleCellSelection(i
, col
);
10394 void wxGrid::DeselectCell( int row
, int col
)
10396 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10397 m_selection
->ToggleCellSelection(row
, col
);
10400 bool wxGrid::IsSelection()
10402 return ( m_selection
&& (m_selection
->IsSelection() ||
10403 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10404 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10407 bool wxGrid::IsInSelection( int row
, int col
) const
10409 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10410 ( row
>= m_selectingTopLeft
.GetRow() &&
10411 col
>= m_selectingTopLeft
.GetCol() &&
10412 row
<= m_selectingBottomRight
.GetRow() &&
10413 col
<= m_selectingBottomRight
.GetCol() )) );
10416 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10420 wxGridCellCoordsArray a
;
10424 return m_selection
->m_cellSelection
;
10427 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10431 wxGridCellCoordsArray a
;
10435 return m_selection
->m_blockSelectionTopLeft
;
10438 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10442 wxGridCellCoordsArray a
;
10446 return m_selection
->m_blockSelectionBottomRight
;
10449 wxArrayInt
wxGrid::GetSelectedRows() const
10457 return m_selection
->m_rowSelection
;
10460 wxArrayInt
wxGrid::GetSelectedCols() const
10468 return m_selection
->m_colSelection
;
10471 void wxGrid::ClearSelection()
10473 m_selectingTopLeft
= wxGridNoCellCoords
;
10474 m_selectingBottomRight
= wxGridNoCellCoords
;
10476 m_selection
->ClearSelection();
10479 // This function returns the rectangle that encloses the given block
10480 // in device coords clipped to the client size of the grid window.
10482 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10483 const wxGridCellCoords
&bottomRight
)
10485 wxRect
rect( wxGridNoCellRect
);
10488 cellRect
= CellToRect( topLeft
);
10489 if ( cellRect
!= wxGridNoCellRect
)
10495 rect
= wxRect(0, 0, 0, 0);
10498 cellRect
= CellToRect( bottomRight
);
10499 if ( cellRect
!= wxGridNoCellRect
)
10505 return wxGridNoCellRect
;
10509 int left
= rect
.GetLeft();
10510 int top
= rect
.GetTop();
10511 int right
= rect
.GetRight();
10512 int bottom
= rect
.GetBottom();
10514 int leftCol
= topLeft
.GetCol();
10515 int topRow
= topLeft
.GetRow();
10516 int rightCol
= bottomRight
.GetCol();
10517 int bottomRow
= bottomRight
.GetRow();
10525 leftCol
= rightCol
;
10535 topRow
= bottomRow
;
10539 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10541 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10543 if ((j
== topRow
) || (j
== bottomRow
) || (i
== leftCol
) || (i
== rightCol
))
10545 cellRect
= CellToRect( j
, i
);
10547 if (cellRect
.x
< left
)
10549 if (cellRect
.y
< top
)
10551 if (cellRect
.x
+ cellRect
.width
> right
)
10552 right
= cellRect
.x
+ cellRect
.width
;
10553 if (cellRect
.y
+ cellRect
.height
> bottom
)
10554 bottom
= cellRect
.y
+ cellRect
.height
;
10558 i
= rightCol
; // jump over inner cells.
10563 // convert to scrolled coords
10565 CalcScrolledPosition( left
, top
, &left
, &top
);
10566 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10569 m_gridWin
->GetClientSize( &cw
, &ch
);
10571 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10572 return wxRect(0,0,0,0);
10574 rect
.SetLeft( wxMax(0, left
) );
10575 rect
.SetTop( wxMax(0, top
) );
10576 rect
.SetRight( wxMin(cw
, right
) );
10577 rect
.SetBottom( wxMin(ch
, bottom
) );
10582 // ----------------------------------------------------------------------------
10583 // grid event classes
10584 // ----------------------------------------------------------------------------
10586 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10588 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10589 int row
, int col
, int x
, int y
, bool sel
,
10590 bool control
, bool shift
, bool alt
, bool meta
)
10591 : wxNotifyEvent( type
, id
)
10598 m_control
= control
;
10603 SetEventObject(obj
);
10607 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10609 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10610 int rowOrCol
, int x
, int y
,
10611 bool control
, bool shift
, bool alt
, bool meta
)
10612 : wxNotifyEvent( type
, id
)
10614 m_rowOrCol
= rowOrCol
;
10617 m_control
= control
;
10622 SetEventObject(obj
);
10626 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10628 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10629 const wxGridCellCoords
& topLeft
,
10630 const wxGridCellCoords
& bottomRight
,
10631 bool sel
, bool control
,
10632 bool shift
, bool alt
, bool meta
)
10633 : wxNotifyEvent( type
, id
)
10635 m_topLeft
= topLeft
;
10636 m_bottomRight
= bottomRight
;
10638 m_control
= control
;
10643 SetEventObject(obj
);
10647 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10649 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10650 wxObject
* obj
, int row
,
10651 int col
, wxControl
* ctrl
)
10652 : wxCommandEvent(type
, id
)
10654 SetEventObject(obj
);
10660 #endif // wxUSE_GRID