1 ///////////////////////////////////////////////////////////////////////////
2 // Name: generic/grid.cpp
3 // Purpose: wxGrid and related classes
4 // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
5 // Modified by: Robin Dunn, Vadim Zeitlin
8 // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "grid.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/dcclient.h"
38 #include "wx/settings.h"
40 #include "wx/textctrl.h"
41 #include "wx/checkbox.h"
42 #include "wx/combobox.h"
43 #include "wx/valtext.h"
47 #include "wx/textfile.h"
48 #include "wx/spinctrl.h"
49 #include "wx/tokenzr.h"
50 #include "wx/renderer.h"
53 #include "wx/generic/gridsel.h"
55 #if defined(__WXMOTIF__)
56 #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier)
58 #define WXUNUSED_MOTIF(identifier) identifier
61 #if defined(__WXGTK__)
62 #define WXUNUSED_GTK(identifier) WXUNUSED(identifier)
64 #define WXUNUSED_GTK(identifier) identifier
67 // Required for wxIs... functions
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr
*, wxArrayAttrs
,
75 class WXDLLIMPEXP_ADV
);
77 struct wxGridCellWithAttr
79 wxGridCellWithAttr(int row
, int col
, wxGridCellAttr
*attr_
)
80 : coords(row
, col
), attr(attr_
)
89 wxGridCellCoords coords
;
93 // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr)
94 // without rewriting the macros, which require a public copy constructor.
97 WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr
, wxGridCellWithAttrArray
,
98 class WXDLLIMPEXP_ADV
);
100 #include "wx/arrimpl.cpp"
102 WX_DEFINE_OBJARRAY(wxGridCellCoordsArray
)
103 WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK
)
110 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK
)
113 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG
)
114 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK
)
115 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK
)
116 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK
)
117 DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK
)
118 DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE
)
119 DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE
)
120 DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT
)
121 DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE
)
122 DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL
)
123 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN
)
124 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN
)
125 DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED
)
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 class WXDLLIMPEXP_ADV wxGridRowLabelWindow
: public wxWindow
134 wxGridRowLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
135 wxGridRowLabelWindow( wxGrid
*parent
, wxWindowID id
,
136 const wxPoint
&pos
, const wxSize
&size
);
141 void OnPaint( wxPaintEvent
& event
);
142 void OnMouseEvent( wxMouseEvent
& event
);
143 void OnMouseWheel( wxMouseEvent
& event
);
144 void OnKeyDown( wxKeyEvent
& event
);
145 void OnKeyUp( wxKeyEvent
& );
146 void OnChar( wxKeyEvent
& );
148 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
149 DECLARE_EVENT_TABLE()
150 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
154 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
157 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
158 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
159 const wxPoint
&pos
, const wxSize
&size
);
164 void OnPaint( wxPaintEvent
&event
);
165 void OnMouseEvent( wxMouseEvent
& event
);
166 void OnMouseWheel( wxMouseEvent
& event
);
167 void OnKeyDown( wxKeyEvent
& event
);
168 void OnKeyUp( wxKeyEvent
& );
169 void OnChar( wxKeyEvent
& );
171 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
172 DECLARE_EVENT_TABLE()
173 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
177 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
180 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
181 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
182 const wxPoint
&pos
, const wxSize
&size
);
187 void OnMouseEvent( wxMouseEvent
& event
);
188 void OnMouseWheel( wxMouseEvent
& event
);
189 void OnKeyDown( wxKeyEvent
& event
);
190 void OnKeyUp( wxKeyEvent
& );
191 void OnChar( wxKeyEvent
& );
192 void OnPaint( wxPaintEvent
& event
);
194 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
195 DECLARE_EVENT_TABLE()
196 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
199 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
204 m_owner
= (wxGrid
*)NULL
;
205 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
206 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
209 wxGridWindow( wxGrid
*parent
,
210 wxGridRowLabelWindow
*rowLblWin
,
211 wxGridColLabelWindow
*colLblWin
,
212 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
215 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
217 wxGrid
* GetOwner() { return m_owner
; }
221 wxGridRowLabelWindow
*m_rowLabelWin
;
222 wxGridColLabelWindow
*m_colLabelWin
;
224 void OnPaint( wxPaintEvent
&event
);
225 void OnMouseWheel( wxMouseEvent
& event
);
226 void OnMouseEvent( wxMouseEvent
& event
);
227 void OnKeyDown( wxKeyEvent
& );
228 void OnKeyUp( wxKeyEvent
& );
229 void OnChar( wxKeyEvent
& );
230 void OnEraseBackground( wxEraseEvent
& );
231 void OnFocus( wxFocusEvent
& );
233 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
234 DECLARE_EVENT_TABLE()
235 DECLARE_NO_COPY_CLASS(wxGridWindow
)
240 class wxGridCellEditorEvtHandler
: public wxEvtHandler
243 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
250 void OnKillFocus(wxFocusEvent
& event
);
251 void OnKeyDown(wxKeyEvent
& event
);
252 void OnChar(wxKeyEvent
& event
);
254 void SetInSetFocus(bool inSetFocus
) { m_inSetFocus
= inSetFocus
; }
258 wxGridCellEditor
* m_editor
;
260 // Work around the fact that a focus kill event can be sent to
261 // a combobox within a set focus event.
264 DECLARE_EVENT_TABLE()
265 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
266 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
270 IMPLEMENT_ABSTRACT_CLASS(wxGridCellEditorEvtHandler
, wxEvtHandler
)
272 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
273 EVT_KILL_FOCUS( wxGridCellEditorEvtHandler::OnKillFocus
)
274 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
275 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
280 // ----------------------------------------------------------------------------
281 // the internal data representation used by wxGridCellAttrProvider
282 // ----------------------------------------------------------------------------
284 // this class stores attributes set for cells
285 class WXDLLIMPEXP_ADV wxGridCellAttrData
288 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
289 wxGridCellAttr
*GetAttr(int row
, int col
) const;
290 void UpdateAttrRows( size_t pos
, int numRows
);
291 void UpdateAttrCols( size_t pos
, int numCols
);
294 // searches for the attr for given cell, returns wxNOT_FOUND if not found
295 int FindIndex(int row
, int col
) const;
297 wxGridCellWithAttrArray m_attrs
;
300 // this class stores attributes set for rows or columns
301 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
304 // empty ctor to suppress warnings
305 wxGridRowOrColAttrData() { }
306 ~wxGridRowOrColAttrData();
308 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
309 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
310 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
313 wxArrayInt m_rowsOrCols
;
314 wxArrayAttrs m_attrs
;
317 // NB: this is just a wrapper around 3 objects: one which stores cell
318 // attributes, and 2 others for row/col ones
319 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
322 wxGridCellAttrData m_cellAttrs
;
323 wxGridRowOrColAttrData m_rowAttrs
,
328 // ----------------------------------------------------------------------------
329 // data structures used for the data type registry
330 // ----------------------------------------------------------------------------
332 struct wxGridDataTypeInfo
334 wxGridDataTypeInfo(const wxString
& typeName
,
335 wxGridCellRenderer
* renderer
,
336 wxGridCellEditor
* editor
)
337 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
340 ~wxGridDataTypeInfo()
342 wxSafeDecRef(m_renderer
);
343 wxSafeDecRef(m_editor
);
347 wxGridCellRenderer
* m_renderer
;
348 wxGridCellEditor
* m_editor
;
350 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
354 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
355 class WXDLLIMPEXP_ADV
);
358 class WXDLLIMPEXP_ADV wxGridTypeRegistry
361 wxGridTypeRegistry() {}
362 ~wxGridTypeRegistry();
364 void RegisterDataType(const wxString
& typeName
,
365 wxGridCellRenderer
* renderer
,
366 wxGridCellEditor
* editor
);
368 // find one of already registered data types
369 int FindRegisteredDataType(const wxString
& typeName
);
371 // try to FindRegisteredDataType(), if this fails and typeName is one of
372 // standard typenames, register it and return its index
373 int FindDataType(const wxString
& typeName
);
375 // try to FindDataType(), if it fails see if it is not one of already
376 // registered data types with some params in which case clone the
377 // registered data type and set params for it
378 int FindOrCloneDataType(const wxString
& typeName
);
380 wxGridCellRenderer
* GetRenderer(int index
);
381 wxGridCellEditor
* GetEditor(int index
);
384 wxGridDataTypeInfoArray m_typeinfo
;
387 // ----------------------------------------------------------------------------
388 // conditional compilation
389 // ----------------------------------------------------------------------------
391 #ifndef WXGRID_DRAW_LINES
392 #define WXGRID_DRAW_LINES 1
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 //#define DEBUG_ATTR_CACHE
400 #ifdef DEBUG_ATTR_CACHE
401 static size_t gs_nAttrCacheHits
= 0;
402 static size_t gs_nAttrCacheMisses
= 0;
403 #endif // DEBUG_ATTR_CACHE
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
410 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
413 // TODO: this doesn't work at all, grid cells have different sizes and approx
414 // calculations don't work as because of the size mismatch scrollbars
415 // sometimes fail to be shown when they should be or vice versa
417 // The scroll bars may be a little flakey once in a while, but that is
418 // surely much less horrible than having scroll lines of only 1!!!
421 // Well, it's still seriously broken so it might be better but needs
424 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
425 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
427 // the size of hash tables used a bit everywhere (the max number of elements
428 // in these hash tables is the number of rows/columns)
429 static const int GRID_HASH_SIZE
= 100;
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 static inline int GetScrollX(int x
)
438 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
441 static inline int GetScrollY(int y
)
443 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
447 // ============================================================================
449 // ============================================================================
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 wxGridCellEditor::wxGridCellEditor()
462 wxGridCellEditor::~wxGridCellEditor()
467 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
468 wxWindowID
WXUNUSED(id
),
469 wxEvtHandler
* evtHandler
)
472 m_control
->PushEventHandler(evtHandler
);
475 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
476 wxGridCellAttr
*attr
)
478 // erase the background because we might not fill the cell
479 wxClientDC
dc(m_control
->GetParent());
480 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
482 gridWindow
->GetOwner()->PrepareDC(dc
);
484 dc
.SetPen(*wxTRANSPARENT_PEN
);
485 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
486 dc
.DrawRectangle(rectCell
);
488 // redraw the control we just painted over
489 m_control
->Refresh();
492 void wxGridCellEditor::Destroy()
496 m_control
->PopEventHandler(true /* delete it*/);
498 m_control
->Destroy();
503 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
505 wxASSERT_MSG(m_control
,
506 wxT("The wxGridCellEditor must be Created first!"));
507 m_control
->Show(show
);
511 // set the colours/fonts if we have any
514 m_colFgOld
= m_control
->GetForegroundColour();
515 m_control
->SetForegroundColour(attr
->GetTextColour());
517 m_colBgOld
= m_control
->GetBackgroundColour();
518 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
520 // Workaround for GTK+1 font setting problem on some platforms
521 #if !defined(__WXGTK__) || defined(__WXGTK20__)
522 m_fontOld
= m_control
->GetFont();
523 m_control
->SetFont(attr
->GetFont());
525 // can't do anything more in the base class version, the other
526 // attributes may only be used by the derived classes
531 // restore the standard colours fonts
532 if ( m_colFgOld
.Ok() )
534 m_control
->SetForegroundColour(m_colFgOld
);
535 m_colFgOld
= wxNullColour
;
538 if ( m_colBgOld
.Ok() )
540 m_control
->SetBackgroundColour(m_colBgOld
);
541 m_colBgOld
= wxNullColour
;
543 // Workaround for GTK+1 font setting problem on some platforms
544 #if !defined(__WXGTK__) || defined(__WXGTK20__)
545 if ( m_fontOld
.Ok() )
547 m_control
->SetFont(m_fontOld
);
548 m_fontOld
= wxNullFont
;
554 void wxGridCellEditor::SetSize(const wxRect
& rect
)
556 wxASSERT_MSG(m_control
,
557 wxT("The wxGridCellEditor must be Created first!"));
558 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
561 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
566 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
568 bool ctrl
= event
.ControlDown();
569 bool alt
= event
.AltDown();
571 // On the Mac the Alt key is more like shift and is used for entry of
572 // valid characters, so check for Ctrl and Meta instead.
573 alt
= event
.MetaDown();
576 // Assume it's not a valid char if ctrl or alt is down, but if both are
577 // down then it may be because of an AltGr key combination, so let them
578 // through in that case.
579 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
583 int key
= event
.GetUnicodeKey();
586 // if the unicode key code is not really a unicode character (it may
587 // be a function key or etc., the platforms appear to always give us a
588 // small value in this case) then fallback to the ascii key code but
589 // don't do anything for function keys or etc.
592 key
= event
.GetKeyCode();
593 keyOk
= (key
<= 127);
596 #else // !wxUSE_UNICODE
597 int key
= event
.GetKeyCode();
601 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
604 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
609 void wxGridCellEditor::StartingClick()
615 // ----------------------------------------------------------------------------
616 // wxGridCellTextEditor
617 // ----------------------------------------------------------------------------
619 wxGridCellTextEditor::wxGridCellTextEditor()
624 void wxGridCellTextEditor::Create(wxWindow
* parent
,
626 wxEvtHandler
* evtHandler
)
628 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
629 wxDefaultPosition
, wxDefaultSize
630 #if defined(__WXMSW__)
631 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
635 // set max length allowed in the textctrl, if the parameter was set
638 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
641 wxGridCellEditor::Create(parent
, id
, evtHandler
);
644 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
645 wxGridCellAttr
* WXUNUSED(attr
))
647 // as we fill the entire client area, don't do anything here to minimize
651 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
653 wxRect
rect(rectOrig
);
655 // Make the edit control large enough to allow for internal
658 // TODO: remove this if the text ctrl sizing is improved esp. for
661 #if defined(__WXGTK__)
670 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
672 // MB: treat MSW separately here otherwise the caret doesn't show
673 // when the editor is in the first row.
674 #if defined(__WXMSW__)
677 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
680 #if defined(__WXMOTIF__)
684 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
685 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
686 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
687 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
690 wxGridCellEditor::SetSize(rect
);
693 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
695 wxASSERT_MSG(m_control
,
696 wxT("The wxGridCellEditor must be Created first!"));
698 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
700 DoBeginEdit(m_startValue
);
703 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
705 Text()->SetValue(startValue
);
706 Text()->SetInsertionPointEnd();
707 Text()->SetSelection(-1,-1);
711 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
714 wxASSERT_MSG(m_control
,
715 wxT("The wxGridCellEditor must be Created first!"));
717 bool changed
= false;
718 wxString value
= Text()->GetValue();
719 if (value
!= m_startValue
)
723 grid
->GetTable()->SetValue(row
, col
, value
);
725 m_startValue
= wxEmptyString
;
726 // No point in setting the text of the hidden control
727 //Text()->SetValue(m_startValue);
733 void wxGridCellTextEditor::Reset()
735 wxASSERT_MSG(m_control
,
736 wxT("The wxGridCellEditor must be Created first!"));
738 DoReset(m_startValue
);
741 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
743 Text()->SetValue(startValue
);
744 Text()->SetInsertionPointEnd();
747 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
749 return wxGridCellEditor::IsAcceptedKey(event
);
752 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
754 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
755 // longer an appropriate way to get the character into the text control.
756 // Do it ourselves instead. We know that if we get this far that we have
757 // a valid character, so not a whole lot of testing needs to be done.
759 wxTextCtrl
* tc
= Text();
764 ch
= event
.GetUnicodeKey();
766 ch
= (wxChar
)event
.GetKeyCode();
768 ch
= (wxChar
)event
.GetKeyCode();
773 // delete the character at the cursor
774 pos
= tc
->GetInsertionPoint();
775 if (pos
< tc
->GetLastPosition())
776 tc
->Remove(pos
, pos
+1);
780 // delete the character before the cursor
781 pos
= tc
->GetInsertionPoint();
783 tc
->Remove(pos
-1, pos
);
792 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
793 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
795 #if defined(__WXMOTIF__) || defined(__WXGTK__)
796 // wxMotif needs a little extra help...
797 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
798 wxString
s( Text()->GetValue() );
799 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
801 Text()->SetInsertionPoint( pos
);
803 // the other ports can handle a Return key press
809 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
819 if ( !params
.ToLong(&tmp
) )
821 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
825 m_maxChars
= (size_t)tmp
;
830 // return the value in the text control
831 wxString
wxGridCellTextEditor::GetValue() const
833 return Text()->GetValue();
836 // ----------------------------------------------------------------------------
837 // wxGridCellNumberEditor
838 // ----------------------------------------------------------------------------
840 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
846 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
848 wxEvtHandler
* evtHandler
)
853 // create a spin ctrl
854 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
855 wxDefaultPosition
, wxDefaultSize
,
859 wxGridCellEditor::Create(parent
, id
, evtHandler
);
864 // just a text control
865 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
868 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
869 #endif // wxUSE_VALIDATORS
873 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
875 // first get the value
876 wxGridTableBase
*table
= grid
->GetTable();
877 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
879 m_valueOld
= table
->GetValueAsLong(row
, col
);
884 wxString sValue
= table
->GetValue(row
, col
);
885 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
887 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
895 Spin()->SetValue((int)m_valueOld
);
901 DoBeginEdit(GetString());
905 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
915 value
= Spin()->GetValue();
916 changed
= value
!= m_valueOld
;
918 text
= wxString::Format(wxT("%ld"), value
);
923 text
= Text()->GetValue();
924 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
929 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
930 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
932 grid
->GetTable()->SetValue(row
, col
, text
);
938 void wxGridCellNumberEditor::Reset()
943 Spin()->SetValue((int)m_valueOld
);
948 DoReset(GetString());
952 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
954 if ( wxGridCellEditor::IsAcceptedKey(event
) )
956 int keycode
= event
.GetKeyCode();
957 if ( (keycode
< 128) &&
958 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
967 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
969 int keycode
= event
.GetKeyCode();
972 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
974 wxGridCellTextEditor::StartingKey(event
);
983 if ( wxIsdigit(keycode
) )
985 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
986 spin
->SetValue(keycode
- '0');
987 spin
->SetSelection(1,1);
995 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
1006 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1010 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1014 // skip the error message below
1019 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1023 // return the value in the spin control if it is there (the text control otherwise)
1024 wxString
wxGridCellNumberEditor::GetValue() const
1031 long value
= Spin()->GetValue();
1032 s
.Printf(wxT("%ld"), value
);
1037 s
= Text()->GetValue();
1043 // ----------------------------------------------------------------------------
1044 // wxGridCellFloatEditor
1045 // ----------------------------------------------------------------------------
1047 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1050 m_precision
= precision
;
1053 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1055 wxEvtHandler
* evtHandler
)
1057 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1059 #if wxUSE_VALIDATORS
1060 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1061 #endif // wxUSE_VALIDATORS
1064 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1066 // first get the value
1067 wxGridTableBase
*table
= grid
->GetTable();
1068 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1070 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1075 wxString sValue
= table
->GetValue(row
, col
);
1076 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1078 wxFAIL_MSG( _T("this cell doesn't have float value") );
1083 DoBeginEdit(GetString());
1086 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1090 wxString
text(Text()->GetValue());
1092 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1094 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1095 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1097 grid
->GetTable()->SetValue(row
, col
, text
);
1104 void wxGridCellFloatEditor::Reset()
1106 DoReset(GetString());
1109 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1111 int keycode
= event
.GetKeyCode();
1113 tmpbuf
[0] = (char) keycode
;
1115 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1117 bool is_decimal_point
= ( strbuf
==
1118 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1120 bool is_decimal_point
= ( strbuf
== _T(".") );
1122 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1123 || is_decimal_point
)
1125 wxGridCellTextEditor::StartingKey(event
);
1127 // skip Skip() below
1134 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1145 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1149 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1151 m_precision
= (int)tmp
;
1153 // skip the error message below
1158 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1162 wxString
wxGridCellFloatEditor::GetString() const
1165 if ( m_width
== -1 )
1167 // default width/precision
1170 else if ( m_precision
== -1 )
1172 // default precision
1173 fmt
.Printf(_T("%%%d.f"), m_width
);
1177 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1180 return wxString::Format(fmt
, m_valueOld
);
1183 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1185 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1187 int keycode
= event
.GetKeyCode();
1188 printf("%d\n", keycode
);
1189 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1191 tmpbuf
[0] = (char) keycode
;
1193 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1195 bool is_decimal_point
=
1196 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1197 wxLOCALE_CAT_NUMBER
) );
1199 bool is_decimal_point
= ( strbuf
== _T(".") );
1201 if ( (keycode
< 128) &&
1202 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1203 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1210 #endif // wxUSE_TEXTCTRL
1214 // ----------------------------------------------------------------------------
1215 // wxGridCellBoolEditor
1216 // ----------------------------------------------------------------------------
1218 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1220 wxEvtHandler
* evtHandler
)
1222 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1223 wxDefaultPosition
, wxDefaultSize
,
1226 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1229 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1231 bool resize
= false;
1232 wxSize size
= m_control
->GetSize();
1233 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1235 // check if the checkbox is not too big/small for this cell
1236 wxSize sizeBest
= m_control
->GetBestSize();
1237 if ( !(size
== sizeBest
) )
1239 // reset to default size if it had been made smaller
1245 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1247 // leave 1 pixel margin
1248 size
.x
= size
.y
= minSize
- 2;
1255 m_control
->SetSize(size
);
1258 // position it in the centre of the rectangle (TODO: support alignment?)
1260 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1261 // the checkbox without label still has some space to the right in wxGTK,
1262 // so shift it to the right
1264 #elif defined(__WXMSW__)
1265 // here too, but in other way
1270 int hAlign
= wxALIGN_CENTRE
;
1271 int vAlign
= wxALIGN_CENTRE
;
1273 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1276 if (hAlign
== wxALIGN_LEFT
)
1282 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1284 else if (hAlign
== wxALIGN_RIGHT
)
1286 x
= r
.x
+ r
.width
- size
.x
- 2;
1287 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1289 else if (hAlign
== wxALIGN_CENTRE
)
1291 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1292 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1295 m_control
->Move(x
, y
);
1298 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1300 m_control
->Show(show
);
1304 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1305 CBox()->SetBackgroundColour(colBg
);
1309 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1311 wxASSERT_MSG(m_control
,
1312 wxT("The wxGridCellEditor must be Created first!"));
1314 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1315 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1318 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1319 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1321 CBox()->SetValue(m_startValue
);
1325 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1328 wxASSERT_MSG(m_control
,
1329 wxT("The wxGridCellEditor must be Created first!"));
1331 bool changed
= false;
1332 bool value
= CBox()->GetValue();
1333 if ( value
!= m_startValue
)
1338 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1339 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1341 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1347 void wxGridCellBoolEditor::Reset()
1349 wxASSERT_MSG(m_control
,
1350 wxT("The wxGridCellEditor must be Created first!"));
1352 CBox()->SetValue(m_startValue
);
1355 void wxGridCellBoolEditor::StartingClick()
1357 CBox()->SetValue(!CBox()->GetValue());
1360 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1362 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1364 int keycode
= event
.GetKeyCode();
1377 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1379 int keycode
= event
.GetKeyCode();
1383 CBox()->SetValue(!CBox()->GetValue());
1387 CBox()->SetValue(true);
1391 CBox()->SetValue(false);
1397 // return the value as "1" for true and the empty string for false
1398 wxString
wxGridCellBoolEditor::GetValue() const
1400 bool bSet
= CBox()->GetValue();
1401 return bSet
? _T("1") : wxEmptyString
;
1404 #endif // wxUSE_CHECKBOX
1408 // ----------------------------------------------------------------------------
1409 // wxGridCellChoiceEditor
1410 // ----------------------------------------------------------------------------
1412 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1414 : m_choices(choices
),
1415 m_allowOthers(allowOthers
) { }
1417 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1418 const wxString choices
[],
1420 : m_allowOthers(allowOthers
)
1424 m_choices
.Alloc(count
);
1425 for ( size_t n
= 0; n
< count
; n
++ )
1427 m_choices
.Add(choices
[n
]);
1432 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1434 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1435 editor
->m_allowOthers
= m_allowOthers
;
1436 editor
->m_choices
= m_choices
;
1441 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1443 wxEvtHandler
* evtHandler
)
1445 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1446 wxDefaultPosition
, wxDefaultSize
,
1448 m_allowOthers
? 0 : wxCB_READONLY
);
1450 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1453 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1454 wxGridCellAttr
* attr
)
1456 // as we fill the entire client area, don't do anything here to minimize
1459 // TODO: It doesn't actually fill the client area since the height of a
1460 // combo always defaults to the standard... Until someone has time to
1461 // figure out the right rectangle to paint, just do it the normal way...
1462 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1465 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1467 wxASSERT_MSG(m_control
,
1468 wxT("The wxGridCellEditor must be Created first!"));
1470 wxGridCellEditorEvtHandler
* evtHandler
= NULL
;
1472 evtHandler
= wxDynamicCast(m_control
->GetEventHandler(), wxGridCellEditorEvtHandler
);
1474 // Don't immediately end if we get a kill focus event within BeginEdit
1476 evtHandler
->SetInSetFocus(true);
1478 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1481 Combo()->SetValue(m_startValue
);
1484 // find the right position, or default to the first if not found
1485 int pos
= Combo()->FindString(m_startValue
);
1486 if (pos
== wxNOT_FOUND
)
1488 Combo()->SetSelection(pos
);
1490 Combo()->SetInsertionPointEnd();
1491 Combo()->SetFocus();
1494 evtHandler
->SetInSetFocus(false);
1497 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1500 wxString value
= Combo()->GetValue();
1501 if ( value
== m_startValue
)
1504 grid
->GetTable()->SetValue(row
, col
, value
);
1509 void wxGridCellChoiceEditor::Reset()
1511 Combo()->SetValue(m_startValue
);
1512 Combo()->SetInsertionPointEnd();
1515 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1525 wxStringTokenizer
tk(params
, _T(','));
1526 while ( tk
.HasMoreTokens() )
1528 m_choices
.Add(tk
.GetNextToken());
1532 // return the value in the text control
1533 wxString
wxGridCellChoiceEditor::GetValue() const
1535 return Combo()->GetValue();
1538 #endif // wxUSE_COMBOBOX
1540 // ----------------------------------------------------------------------------
1541 // wxGridCellEditorEvtHandler
1542 // ----------------------------------------------------------------------------
1544 void wxGridCellEditorEvtHandler::OnKillFocus(wxFocusEvent
& event
)
1546 // Don't disable the cell if we're just starting to edit it
1551 m_grid
->DisableCellEditControl();
1556 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1558 switch ( event
.GetKeyCode() )
1562 m_grid
->DisableCellEditControl();
1566 m_grid
->GetEventHandler()->ProcessEvent( event
);
1570 case WXK_NUMPAD_ENTER
:
1571 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1572 m_editor
->HandleReturn(event
);
1580 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1582 switch ( event
.GetKeyCode() )
1587 case WXK_NUMPAD_ENTER
:
1595 // ----------------------------------------------------------------------------
1596 // wxGridCellWorker is an (almost) empty common base class for
1597 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1598 // ----------------------------------------------------------------------------
1600 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1605 wxGridCellWorker::~wxGridCellWorker()
1609 // ============================================================================
1611 // ============================================================================
1613 // ----------------------------------------------------------------------------
1614 // wxGridCellRenderer
1615 // ----------------------------------------------------------------------------
1617 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1618 wxGridCellAttr
& attr
,
1621 int WXUNUSED(row
), int WXUNUSED(col
),
1624 dc
.SetBackgroundMode( wxSOLID
);
1626 // grey out fields if the grid is disabled
1627 if( grid
.IsEnabled() )
1631 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1635 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1640 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1643 dc
.SetPen( *wxTRANSPARENT_PEN
);
1644 dc
.DrawRectangle(rect
);
1647 // ----------------------------------------------------------------------------
1648 // wxGridCellStringRenderer
1649 // ----------------------------------------------------------------------------
1651 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1652 wxGridCellAttr
& attr
,
1656 dc
.SetBackgroundMode( wxTRANSPARENT
);
1658 // TODO some special colours for attr.IsReadOnly() case?
1660 // different coloured text when the grid is disabled
1661 if( grid
.IsEnabled() )
1665 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1666 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1670 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1671 dc
.SetTextForeground( attr
.GetTextColour() );
1676 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1677 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1680 dc
.SetFont( attr
.GetFont() );
1683 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1685 const wxString
& text
)
1687 wxCoord x
= 0, y
= 0, max_x
= 0;
1688 dc
.SetFont(attr
.GetFont());
1689 wxStringTokenizer
tk(text
, _T('\n'));
1690 while ( tk
.HasMoreTokens() )
1692 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1693 max_x
= wxMax(max_x
, x
);
1696 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1698 return wxSize(max_x
, y
);
1701 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1702 wxGridCellAttr
& attr
,
1706 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1709 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1710 wxGridCellAttr
& attr
,
1712 const wxRect
& rectCell
,
1716 wxRect rect
= rectCell
;
1719 // erase only this cells background, overflow cells should have been erased
1720 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1723 attr
.GetAlignment(&hAlign
, &vAlign
);
1725 int overflowCols
= 0;
1727 if (attr
.GetOverflow())
1729 int cols
= grid
.GetNumberCols();
1730 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1731 int cell_rows
, cell_cols
;
1732 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1733 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1735 int i
, c_cols
, c_rows
;
1736 for (i
= col
+cell_cols
; i
< cols
; i
++)
1738 bool is_empty
= true;
1739 for (int j
=row
; j
<row
+cell_rows
; j
++)
1741 // check w/ anchor cell for multicell block
1742 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1743 if (c_rows
> 0) c_rows
= 0;
1744 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1751 rect
.width
+= grid
.GetColSize(i
);
1757 if (rect
.width
>= best_width
) break;
1759 overflowCols
= i
- col
- cell_cols
+ 1;
1760 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1763 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1765 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1767 clip
.x
+= rectCell
.width
;
1768 // draw each overflow cell individually
1769 int col_end
= col
+cell_cols
+overflowCols
;
1770 if (col_end
>= grid
.GetNumberCols())
1771 col_end
= grid
.GetNumberCols() - 1;
1772 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1774 clip
.width
= grid
.GetColSize(i
) - 1;
1775 dc
.DestroyClippingRegion();
1776 dc
.SetClippingRegion(clip
);
1778 SetTextColoursAndFont(grid
, attr
, dc
,
1779 grid
.IsInSelection(row
,i
));
1781 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1782 rect
, hAlign
, vAlign
);
1783 clip
.x
+= grid
.GetColSize(i
) - 1;
1789 dc
.DestroyClippingRegion();
1793 // now we only have to draw the text
1794 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1796 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1797 rect
, hAlign
, vAlign
);
1800 // ----------------------------------------------------------------------------
1801 // wxGridCellNumberRenderer
1802 // ----------------------------------------------------------------------------
1804 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1806 wxGridTableBase
*table
= grid
.GetTable();
1808 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1810 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1814 text
= table
->GetValue(row
, col
);
1820 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1821 wxGridCellAttr
& attr
,
1823 const wxRect
& rectCell
,
1827 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1829 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1831 // draw the text right aligned by default
1833 attr
.GetAlignment(&hAlign
, &vAlign
);
1834 hAlign
= wxALIGN_RIGHT
;
1836 wxRect rect
= rectCell
;
1839 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1842 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1843 wxGridCellAttr
& attr
,
1847 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1850 // ----------------------------------------------------------------------------
1851 // wxGridCellFloatRenderer
1852 // ----------------------------------------------------------------------------
1854 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1857 SetPrecision(precision
);
1860 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1862 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1863 renderer
->m_width
= m_width
;
1864 renderer
->m_precision
= m_precision
;
1865 renderer
->m_format
= m_format
;
1870 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1872 wxGridTableBase
*table
= grid
.GetTable();
1877 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1879 val
= table
->GetValueAsDouble(row
, col
);
1884 text
= table
->GetValue(row
, col
);
1885 hasDouble
= text
.ToDouble(&val
);
1892 if ( m_width
== -1 )
1894 if ( m_precision
== -1 )
1896 // default width/precision
1897 m_format
= _T("%f");
1901 m_format
.Printf(_T("%%.%df"), m_precision
);
1904 else if ( m_precision
== -1 )
1906 // default precision
1907 m_format
.Printf(_T("%%%d.f"), m_width
);
1911 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1915 text
.Printf(m_format
, val
);
1918 //else: text already contains the string
1923 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1924 wxGridCellAttr
& attr
,
1926 const wxRect
& rectCell
,
1930 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1932 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1934 // draw the text right aligned by default
1936 attr
.GetAlignment(&hAlign
, &vAlign
);
1937 hAlign
= wxALIGN_RIGHT
;
1939 wxRect rect
= rectCell
;
1942 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1945 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1946 wxGridCellAttr
& attr
,
1950 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1953 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1957 // reset to defaults
1963 wxString tmp
= params
.BeforeFirst(_T(','));
1967 if ( tmp
.ToLong(&width
) )
1969 SetWidth((int)width
);
1973 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1976 tmp
= params
.AfterFirst(_T(','));
1980 if ( tmp
.ToLong(&precision
) )
1982 SetPrecision((int)precision
);
1986 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1993 // ----------------------------------------------------------------------------
1994 // wxGridCellBoolRenderer
1995 // ----------------------------------------------------------------------------
1997 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1999 // FIXME these checkbox size calculations are really ugly...
2001 // between checkmark and box
2002 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
2004 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
2005 wxGridCellAttr
& WXUNUSED(attr
),
2010 // compute it only once (no locks for MT safeness in GUI thread...)
2011 if ( !ms_sizeCheckMark
.x
)
2013 // get checkbox size
2014 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
2015 wxSize size
= checkbox
->GetBestSize();
2016 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
2018 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
2019 #if defined(__WXGTK__) || defined(__WXMOTIF__)
2020 checkSize
-= size
.y
/ 2;
2025 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2028 return ms_sizeCheckMark
;
2031 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2032 wxGridCellAttr
& attr
,
2038 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2040 // draw a check mark in the centre (ignoring alignment - TODO)
2041 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2043 // don't draw outside the cell
2044 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2045 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2047 // and even leave (at least) 1 pixel margin
2048 size
.x
= size
.y
= minSize
- 2;
2051 // draw a border around checkmark
2053 attr
.GetAlignment(& hAlign
, &vAlign
);
2056 if (hAlign
== wxALIGN_CENTRE
)
2058 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2059 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2060 rectBorder
.width
= size
.x
;
2061 rectBorder
.height
= size
.y
;
2063 else if (hAlign
== wxALIGN_LEFT
)
2065 rectBorder
.x
= rect
.x
+ 2;
2066 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2067 rectBorder
.width
= size
.x
;
2068 rectBorder
.height
= size
.y
;
2070 else if (hAlign
== wxALIGN_RIGHT
)
2072 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2073 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2074 rectBorder
.width
= size
.x
;
2075 rectBorder
.height
= size
.y
;
2079 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2080 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2083 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2084 value
= !( !cellval
|| (cellval
== wxT("0")) );
2089 wxRect rectMark
= rectBorder
;
2091 // MSW DrawCheckMark() is weird (and should probably be changed...)
2092 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2096 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2099 dc
.SetTextForeground(attr
.GetTextColour());
2100 dc
.DrawCheckMark(rectMark
);
2103 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2104 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2105 dc
.DrawRectangle(rectBorder
);
2108 // ----------------------------------------------------------------------------
2110 // ----------------------------------------------------------------------------
2112 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2116 m_isReadOnly
= Unset
;
2121 m_attrkind
= wxGridCellAttr::Cell
;
2123 m_sizeRows
= m_sizeCols
= 1;
2124 m_overflow
= UnsetOverflow
;
2126 SetDefAttr(attrDefault
);
2129 wxGridCellAttr
*wxGridCellAttr::Clone() const
2131 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2133 if ( HasTextColour() )
2134 attr
->SetTextColour(GetTextColour());
2135 if ( HasBackgroundColour() )
2136 attr
->SetBackgroundColour(GetBackgroundColour());
2138 attr
->SetFont(GetFont());
2139 if ( HasAlignment() )
2140 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2142 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2146 attr
->SetRenderer(m_renderer
);
2147 m_renderer
->IncRef();
2151 attr
->SetEditor(m_editor
);
2156 attr
->SetReadOnly();
2158 attr
->SetKind( m_attrkind
);
2163 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2165 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2166 SetTextColour(mergefrom
->GetTextColour());
2167 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2168 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2169 if ( !HasFont() && mergefrom
->HasFont() )
2170 SetFont(mergefrom
->GetFont());
2171 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2173 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2174 SetAlignment(hAlign
, vAlign
);
2176 if ( !HasSize() && mergefrom
->HasSize() )
2177 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2179 // Directly access member functions as GetRender/Editor don't just return
2180 // m_renderer/m_editor
2182 // Maybe add support for merge of Render and Editor?
2183 if (!HasRenderer() && mergefrom
->HasRenderer() )
2185 m_renderer
= mergefrom
->m_renderer
;
2186 m_renderer
->IncRef();
2188 if ( !HasEditor() && mergefrom
->HasEditor() )
2190 m_editor
= mergefrom
->m_editor
;
2193 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2194 SetReadOnly(mergefrom
->IsReadOnly());
2196 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2197 SetOverflow(mergefrom
->GetOverflow());
2199 SetDefAttr(mergefrom
->m_defGridAttr
);
2202 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2204 // The size of a cell is normally 1,1
2206 // If this cell is larger (2,2) then this is the top left cell
2207 // the other cells that will be covered (lower right cells) must be
2208 // set to negative or zero values such that
2209 // row + num_rows of the covered cell points to the larger cell (this cell)
2210 // same goes for the col + num_cols.
2212 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2214 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2215 !((num_rows
<=0)&&(num_cols
>0)) ||
2216 !((num_rows
==0)&&(num_cols
==0))),
2217 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2219 m_sizeRows
= num_rows
;
2220 m_sizeCols
= num_cols
;
2223 const wxColour
& wxGridCellAttr::GetTextColour() const
2225 if (HasTextColour())
2229 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2231 return m_defGridAttr
->GetTextColour();
2235 wxFAIL_MSG(wxT("Missing default cell attribute"));
2236 return wxNullColour
;
2241 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2243 if (HasBackgroundColour())
2245 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2246 return m_defGridAttr
->GetBackgroundColour();
2249 wxFAIL_MSG(wxT("Missing default cell attribute"));
2250 return wxNullColour
;
2255 const wxFont
& wxGridCellAttr::GetFont() const
2259 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2260 return m_defGridAttr
->GetFont();
2263 wxFAIL_MSG(wxT("Missing default cell attribute"));
2269 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2273 if ( hAlign
) *hAlign
= m_hAlign
;
2274 if ( vAlign
) *vAlign
= m_vAlign
;
2276 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2277 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2280 wxFAIL_MSG(wxT("Missing default cell attribute"));
2284 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2286 if ( num_rows
) *num_rows
= m_sizeRows
;
2287 if ( num_cols
) *num_cols
= m_sizeCols
;
2290 // GetRenderer and GetEditor use a slightly different decision path about
2291 // which attribute to use. If a non-default attr object has one then it is
2292 // used, otherwise the default editor or renderer is fetched from the grid and
2293 // used. It should be the default for the data type of the cell. If it is
2294 // NULL (because the table has a type that the grid does not have in its
2295 // registry,) then the grid's default editor or renderer is used.
2297 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2299 wxGridCellRenderer
*renderer
;
2301 if ( m_renderer
&& this != m_defGridAttr
)
2303 // use the cells renderer if it has one
2304 renderer
= m_renderer
;
2307 else // no non default cell renderer
2309 // get default renderer for the data type
2312 // GetDefaultRendererForCell() will do IncRef() for us
2313 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2322 if (m_defGridAttr
&& this != m_defGridAttr
)
2324 // if we still don't have one then use the grid default
2325 // (no need for IncRef() here neither)
2326 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2328 else // default grid attr
2330 // use m_renderer which we had decided not to use initially
2331 renderer
= m_renderer
;
2338 // we're supposed to always find something
2339 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2344 // same as above, except for s/renderer/editor/g
2345 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2347 wxGridCellEditor
*editor
;
2349 if ( m_editor
&& this != m_defGridAttr
)
2351 // use the cells editor if it has one
2355 else // no non default cell editor
2357 // get default editor for the data type
2360 // GetDefaultEditorForCell() will do IncRef() for us
2361 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2370 if ( m_defGridAttr
&& this != m_defGridAttr
)
2372 // if we still don't have one then use the grid default
2373 // (no need for IncRef() here neither)
2374 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2376 else // default grid attr
2378 // use m_editor which we had decided not to use initially
2386 // we're supposed to always find something
2387 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2392 // ----------------------------------------------------------------------------
2393 // wxGridCellAttrData
2394 // ----------------------------------------------------------------------------
2396 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2398 int n
= FindIndex(row
, col
);
2399 if ( n
== wxNOT_FOUND
)
2401 // add the attribute
2402 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2406 // free the old attribute
2407 m_attrs
[(size_t)n
].attr
->DecRef();
2411 // change the attribute
2412 m_attrs
[(size_t)n
].attr
= attr
;
2416 // remove this attribute
2417 m_attrs
.RemoveAt((size_t)n
);
2422 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2424 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2426 int n
= FindIndex(row
, col
);
2427 if ( n
!= wxNOT_FOUND
)
2429 attr
= m_attrs
[(size_t)n
].attr
;
2436 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2438 size_t count
= m_attrs
.GetCount();
2439 for ( size_t n
= 0; n
< count
; n
++ )
2441 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2442 wxCoord row
= coords
.GetRow();
2443 if ((size_t)row
>= pos
)
2447 // If rows inserted, include row counter where necessary
2448 coords
.SetRow(row
+ numRows
);
2450 else if (numRows
< 0)
2452 // If rows deleted ...
2453 if ((size_t)row
>= pos
- numRows
)
2455 // ...either decrement row counter (if row still exists)...
2456 coords
.SetRow(row
+ numRows
);
2460 // ...or remove the attribute
2461 // No need to DecRef the attribute itself since this is
2462 // done be wxGridCellWithAttr's destructor!
2463 m_attrs
.RemoveAt(n
);
2471 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2473 size_t count
= m_attrs
.GetCount();
2474 for ( size_t n
= 0; n
< count
; n
++ )
2476 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2477 wxCoord col
= coords
.GetCol();
2478 if ( (size_t)col
>= pos
)
2482 // If rows inserted, include row counter where necessary
2483 coords
.SetCol(col
+ numCols
);
2485 else if (numCols
< 0)
2487 // If rows deleted ...
2488 if ((size_t)col
>= pos
- numCols
)
2490 // ...either decrement row counter (if row still exists)...
2491 coords
.SetCol(col
+ numCols
);
2495 // ...or remove the attribute
2496 // No need to DecRef the attribute itself since this is
2497 // done be wxGridCellWithAttr's destructor!
2498 m_attrs
.RemoveAt(n
);
2506 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2508 size_t count
= m_attrs
.GetCount();
2509 for ( size_t n
= 0; n
< count
; n
++ )
2511 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2512 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2521 // ----------------------------------------------------------------------------
2522 // wxGridRowOrColAttrData
2523 // ----------------------------------------------------------------------------
2525 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2527 size_t count
= m_attrs
.Count();
2528 for ( size_t n
= 0; n
< count
; n
++ )
2530 m_attrs
[n
]->DecRef();
2534 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2536 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2538 int n
= m_rowsOrCols
.Index(rowOrCol
);
2539 if ( n
!= wxNOT_FOUND
)
2541 attr
= m_attrs
[(size_t)n
];
2548 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2550 int i
= m_rowsOrCols
.Index(rowOrCol
);
2551 if ( i
== wxNOT_FOUND
)
2553 // add the attribute
2554 m_rowsOrCols
.Add(rowOrCol
);
2559 size_t n
= (size_t)i
;
2562 // change the attribute
2563 m_attrs
[n
]->DecRef();
2568 // remove this attribute
2569 m_attrs
[n
]->DecRef();
2570 m_rowsOrCols
.RemoveAt(n
);
2571 m_attrs
.RemoveAt(n
);
2576 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2578 size_t count
= m_attrs
.GetCount();
2579 for ( size_t n
= 0; n
< count
; n
++ )
2581 int & rowOrCol
= m_rowsOrCols
[n
];
2582 if ( (size_t)rowOrCol
>= pos
)
2584 if ( numRowsOrCols
> 0 )
2586 // If rows inserted, include row counter where necessary
2587 rowOrCol
+= numRowsOrCols
;
2589 else if ( numRowsOrCols
< 0)
2591 // If rows deleted, either decrement row counter (if row still exists)
2592 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2593 rowOrCol
+= numRowsOrCols
;
2596 m_rowsOrCols
.RemoveAt(n
);
2597 m_attrs
[n
]->DecRef();
2598 m_attrs
.RemoveAt(n
);
2606 // ----------------------------------------------------------------------------
2607 // wxGridCellAttrProvider
2608 // ----------------------------------------------------------------------------
2610 wxGridCellAttrProvider::wxGridCellAttrProvider()
2612 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2615 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2620 void wxGridCellAttrProvider::InitData()
2622 m_data
= new wxGridCellAttrProviderData
;
2625 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2626 wxGridCellAttr::wxAttrKind kind
) const
2628 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2633 case (wxGridCellAttr::Any
):
2634 //Get cached merge attributes.
2635 // Currenlty not used as no cache implemented as not mutiable
2636 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2639 //Basicaly implement old version.
2640 //Also check merge cache, so we don't have to re-merge every time..
2641 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2642 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2643 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2645 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2646 // Two or more are non NULL
2647 attr
= new wxGridCellAttr
;
2648 attr
->SetKind(wxGridCellAttr::Merged
);
2652 attr
->MergeWith(attrcell
);
2656 attr
->MergeWith(attrcol
);
2660 attr
->MergeWith(attrrow
);
2663 //store merge attr if cache implemented
2665 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2669 // one or none is non null return it or null.
2670 if(attrrow
) attr
= attrrow
;
2686 case (wxGridCellAttr::Cell
):
2687 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2689 case (wxGridCellAttr::Col
):
2690 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2692 case (wxGridCellAttr::Row
):
2693 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2697 // (wxGridCellAttr::Default):
2698 // (wxGridCellAttr::Merged):
2705 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2711 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2714 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2719 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2722 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2727 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2730 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2734 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2736 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2740 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2744 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2746 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2750 // ----------------------------------------------------------------------------
2751 // wxGridTypeRegistry
2752 // ----------------------------------------------------------------------------
2754 wxGridTypeRegistry::~wxGridTypeRegistry()
2756 size_t count
= m_typeinfo
.Count();
2757 for ( size_t i
= 0; i
< count
; i
++ )
2758 delete m_typeinfo
[i
];
2762 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2763 wxGridCellRenderer
* renderer
,
2764 wxGridCellEditor
* editor
)
2766 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2768 // is it already registered?
2769 int loc
= FindRegisteredDataType(typeName
);
2770 if ( loc
!= wxNOT_FOUND
)
2772 delete m_typeinfo
[loc
];
2773 m_typeinfo
[loc
] = info
;
2777 m_typeinfo
.Add(info
);
2781 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2783 size_t count
= m_typeinfo
.GetCount();
2784 for ( size_t i
= 0; i
< count
; i
++ )
2786 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2795 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2797 int index
= FindRegisteredDataType(typeName
);
2798 if ( index
== wxNOT_FOUND
)
2800 // check whether this is one of the standard ones, in which case
2801 // register it "on the fly"
2803 if ( typeName
== wxGRID_VALUE_STRING
)
2805 RegisterDataType(wxGRID_VALUE_STRING
,
2806 new wxGridCellStringRenderer
,
2807 new wxGridCellTextEditor
);
2809 #endif // wxUSE_TEXTCTRL
2811 if ( typeName
== wxGRID_VALUE_BOOL
)
2813 RegisterDataType(wxGRID_VALUE_BOOL
,
2814 new wxGridCellBoolRenderer
,
2815 new wxGridCellBoolEditor
);
2817 #endif // wxUSE_CHECKBOX
2819 if ( typeName
== wxGRID_VALUE_NUMBER
)
2821 RegisterDataType(wxGRID_VALUE_NUMBER
,
2822 new wxGridCellNumberRenderer
,
2823 new wxGridCellNumberEditor
);
2825 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2827 RegisterDataType(wxGRID_VALUE_FLOAT
,
2828 new wxGridCellFloatRenderer
,
2829 new wxGridCellFloatEditor
);
2831 #endif // wxUSE_TEXTCTRL
2833 if ( typeName
== wxGRID_VALUE_CHOICE
)
2835 RegisterDataType(wxGRID_VALUE_CHOICE
,
2836 new wxGridCellStringRenderer
,
2837 new wxGridCellChoiceEditor
);
2839 #endif // wxUSE_COMBOBOX
2844 // we get here only if just added the entry for this type, so return
2846 index
= m_typeinfo
.GetCount() - 1;
2852 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2854 int index
= FindDataType(typeName
);
2855 if ( index
== wxNOT_FOUND
)
2857 // the first part of the typename is the "real" type, anything after ':'
2858 // are the parameters for the renderer
2859 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2860 if ( index
== wxNOT_FOUND
)
2865 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2866 wxGridCellRenderer
*rendererOld
= renderer
;
2867 renderer
= renderer
->Clone();
2868 rendererOld
->DecRef();
2870 wxGridCellEditor
*editor
= GetEditor(index
);
2871 wxGridCellEditor
*editorOld
= editor
;
2872 editor
= editor
->Clone();
2873 editorOld
->DecRef();
2875 // do it even if there are no parameters to reset them to defaults
2876 wxString params
= typeName
.AfterFirst(_T(':'));
2877 renderer
->SetParameters(params
);
2878 editor
->SetParameters(params
);
2880 // register the new typename
2881 RegisterDataType(typeName
, renderer
, editor
);
2883 // we just registered it, it's the last one
2884 index
= m_typeinfo
.GetCount() - 1;
2890 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2892 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2898 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2900 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2906 // ----------------------------------------------------------------------------
2908 // ----------------------------------------------------------------------------
2910 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2913 wxGridTableBase::wxGridTableBase()
2915 m_view
= (wxGrid
*) NULL
;
2916 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2919 wxGridTableBase::~wxGridTableBase()
2921 delete m_attrProvider
;
2924 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2926 delete m_attrProvider
;
2927 m_attrProvider
= attrProvider
;
2930 bool wxGridTableBase::CanHaveAttributes()
2932 if ( ! GetAttrProvider() )
2934 // use the default attr provider by default
2935 SetAttrProvider(new wxGridCellAttrProvider
);
2940 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2942 if ( m_attrProvider
)
2943 return m_attrProvider
->GetAttr(row
, col
, kind
);
2945 return (wxGridCellAttr
*)NULL
;
2949 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2951 if ( m_attrProvider
)
2953 attr
->SetKind(wxGridCellAttr::Cell
);
2954 m_attrProvider
->SetAttr(attr
, row
, col
);
2958 // as we take ownership of the pointer and don't store it, we must
2964 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2966 if ( m_attrProvider
)
2968 attr
->SetKind(wxGridCellAttr::Row
);
2969 m_attrProvider
->SetRowAttr(attr
, row
);
2973 // as we take ownership of the pointer and don't store it, we must
2979 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2981 if ( m_attrProvider
)
2983 attr
->SetKind(wxGridCellAttr::Col
);
2984 m_attrProvider
->SetColAttr(attr
, col
);
2988 // as we take ownership of the pointer and don't store it, we must
2994 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2995 size_t WXUNUSED(numRows
) )
2997 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
3002 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
3004 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
3009 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
3010 size_t WXUNUSED(numRows
) )
3012 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
3017 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
3018 size_t WXUNUSED(numCols
) )
3020 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
3025 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
3027 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3032 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3033 size_t WXUNUSED(numCols
) )
3035 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3041 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3044 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3045 // how much it makes sense to us geeks.
3049 wxString
wxGridTableBase::GetColLabelValue( int col
)
3051 // default col labels are:
3052 // cols 0 to 25 : A-Z
3053 // cols 26 to 675 : AA-ZZ
3058 for ( n
= 1; ; n
++ )
3060 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3062 if ( col
< 0 ) break;
3065 // reverse the string...
3067 for ( i
= 0; i
< n
; i
++ )
3076 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3078 return wxGRID_VALUE_STRING
;
3081 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3082 const wxString
& typeName
)
3084 return typeName
== wxGRID_VALUE_STRING
;
3087 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3089 return CanGetValueAs(row
, col
, typeName
);
3092 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3097 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3102 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3107 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3108 long WXUNUSED(value
) )
3112 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3113 double WXUNUSED(value
) )
3117 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3118 bool WXUNUSED(value
) )
3123 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3124 const wxString
& WXUNUSED(typeName
) )
3129 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3130 const wxString
& WXUNUSED(typeName
),
3131 void* WXUNUSED(value
) )
3135 //////////////////////////////////////////////////////////////////////
3137 // Message class for the grid table to send requests and notifications
3141 wxGridTableMessage::wxGridTableMessage()
3143 m_table
= (wxGridTableBase
*) NULL
;
3149 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3150 int commandInt1
, int commandInt2
)
3154 m_comInt1
= commandInt1
;
3155 m_comInt2
= commandInt2
;
3160 //////////////////////////////////////////////////////////////////////
3162 // A basic grid table for string data. An object of this class will
3163 // created by wxGrid if you don't specify an alternative table class.
3166 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3168 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3170 wxGridStringTable::wxGridStringTable()
3175 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3178 m_data
.Alloc( numRows
);
3181 sa
.Alloc( numCols
);
3182 sa
.Add( wxEmptyString
, numCols
);
3184 m_data
.Add( sa
, numRows
);
3187 wxGridStringTable::~wxGridStringTable()
3191 int wxGridStringTable::GetNumberRows()
3193 return m_data
.GetCount();
3196 int wxGridStringTable::GetNumberCols()
3198 if ( m_data
.GetCount() > 0 )
3199 return m_data
[0].GetCount();
3204 wxString
wxGridStringTable::GetValue( int row
, int col
)
3206 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3208 _T("invalid row or column index in wxGridStringTable") );
3210 return m_data
[row
][col
];
3213 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3215 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3216 _T("invalid row or column index in wxGridStringTable") );
3218 m_data
[row
][col
] = value
;
3221 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3223 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3225 _T("invalid row or column index in wxGridStringTable") );
3227 return (m_data
[row
][col
] == wxEmptyString
);
3230 void wxGridStringTable::Clear()
3233 int numRows
, numCols
;
3235 numRows
= m_data
.GetCount();
3238 numCols
= m_data
[0].GetCount();
3240 for ( row
= 0; row
< numRows
; row
++ )
3242 for ( col
= 0; col
< numCols
; col
++ )
3244 m_data
[row
][col
] = wxEmptyString
;
3251 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3253 size_t curNumRows
= m_data
.GetCount();
3254 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3255 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3257 if ( pos
>= curNumRows
)
3259 return AppendRows( numRows
);
3263 sa
.Alloc( curNumCols
);
3264 sa
.Add( wxEmptyString
, curNumCols
);
3265 m_data
.Insert( sa
, pos
, numRows
);
3268 wxGridTableMessage
msg( this,
3269 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3273 GetView()->ProcessTableMessage( msg
);
3279 bool wxGridStringTable::AppendRows( size_t numRows
)
3281 size_t curNumRows
= m_data
.GetCount();
3282 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3283 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3286 if ( curNumCols
> 0 )
3288 sa
.Alloc( curNumCols
);
3289 sa
.Add( wxEmptyString
, curNumCols
);
3292 m_data
.Add( sa
, numRows
);
3296 wxGridTableMessage
msg( this,
3297 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3300 GetView()->ProcessTableMessage( msg
);
3306 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3308 size_t curNumRows
= m_data
.GetCount();
3310 if ( pos
>= curNumRows
)
3312 wxFAIL_MSG( wxString::Format
3314 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3316 (unsigned long)numRows
,
3317 (unsigned long)curNumRows
3323 if ( numRows
> curNumRows
- pos
)
3325 numRows
= curNumRows
- pos
;
3328 if ( numRows
>= curNumRows
)
3334 m_data
.RemoveAt( pos
, numRows
);
3338 wxGridTableMessage
msg( this,
3339 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3343 GetView()->ProcessTableMessage( msg
);
3349 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3353 size_t curNumRows
= m_data
.GetCount();
3354 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3355 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3357 if ( pos
>= curNumCols
)
3359 return AppendCols( numCols
);
3362 for ( row
= 0; row
< curNumRows
; row
++ )
3364 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3366 m_data
[row
].Insert( wxEmptyString
, col
);
3371 wxGridTableMessage
msg( this,
3372 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3376 GetView()->ProcessTableMessage( msg
);
3382 bool wxGridStringTable::AppendCols( size_t numCols
)
3386 size_t curNumRows
= m_data
.GetCount();
3390 // TODO: something better than this ?
3392 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3397 for ( row
= 0; row
< curNumRows
; row
++ )
3399 m_data
[row
].Add( wxEmptyString
, numCols
);
3404 wxGridTableMessage
msg( this,
3405 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3408 GetView()->ProcessTableMessage( msg
);
3414 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3418 size_t curNumRows
= m_data
.GetCount();
3419 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3420 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3422 if ( pos
>= curNumCols
)
3424 wxFAIL_MSG( wxString::Format
3426 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3428 (unsigned long)numCols
,
3429 (unsigned long)curNumCols
3434 if ( numCols
> curNumCols
- pos
)
3436 numCols
= curNumCols
- pos
;
3439 for ( row
= 0; row
< curNumRows
; row
++ )
3441 if ( numCols
>= curNumCols
)
3443 m_data
[row
].Clear();
3447 m_data
[row
].RemoveAt( pos
, numCols
);
3452 wxGridTableMessage
msg( this,
3453 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3457 GetView()->ProcessTableMessage( msg
);
3463 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3465 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3467 // using default label
3469 return wxGridTableBase::GetRowLabelValue( row
);
3473 return m_rowLabels
[ row
];
3477 wxString
wxGridStringTable::GetColLabelValue( int col
)
3479 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3481 // using default label
3483 return wxGridTableBase::GetColLabelValue( col
);
3487 return m_colLabels
[ col
];
3491 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3493 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3495 int n
= m_rowLabels
.GetCount();
3497 for ( i
= n
; i
<= row
; i
++ )
3499 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3503 m_rowLabels
[row
] = value
;
3506 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3508 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3510 int n
= m_colLabels
.GetCount();
3512 for ( i
= n
; i
<= col
; i
++ )
3514 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3518 m_colLabels
[col
] = value
;
3523 //////////////////////////////////////////////////////////////////////
3524 //////////////////////////////////////////////////////////////////////
3526 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3528 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3529 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3530 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3531 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3532 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3533 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3534 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3537 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3539 const wxPoint
&pos
, const wxSize
&size
)
3540 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3545 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3549 // NO - don't do this because it will set both the x and y origin
3550 // coords to match the parent scrolled window and we just want to
3551 // set the y coord - MB
3553 // m_owner->PrepareDC( dc );
3556 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3557 dc
.SetDeviceOrigin( 0, -y
);
3559 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3560 m_owner
->DrawRowLabels( dc
, rows
);
3564 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3566 m_owner
->ProcessRowLabelMouseEvent( event
);
3570 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3572 m_owner
->GetEventHandler()->ProcessEvent(event
);
3576 // This seems to be required for wxMotif otherwise the mouse
3577 // cursor must be in the cell edit control to get key events
3579 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3581 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3584 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3586 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3589 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3591 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3596 //////////////////////////////////////////////////////////////////////
3598 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3600 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3601 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3602 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3603 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3604 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3605 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3606 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3609 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3611 const wxPoint
&pos
, const wxSize
&size
)
3612 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3617 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3621 // NO - don't do this because it will set both the x and y origin
3622 // coords to match the parent scrolled window and we just want to
3623 // set the x coord - MB
3625 // m_owner->PrepareDC( dc );
3628 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3629 dc
.SetDeviceOrigin( -x
, 0 );
3631 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3632 m_owner
->DrawColLabels( dc
, cols
);
3636 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3638 m_owner
->ProcessColLabelMouseEvent( event
);
3641 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3643 m_owner
->GetEventHandler()->ProcessEvent(event
);
3647 // This seems to be required for wxMotif otherwise the mouse
3648 // cursor must be in the cell edit control to get key events
3650 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3652 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3655 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3657 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3660 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3662 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3666 //////////////////////////////////////////////////////////////////////
3668 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3670 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3671 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3672 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3673 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3674 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3675 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3676 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3679 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3681 const wxPoint
&pos
, const wxSize
&size
)
3682 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3687 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3691 int client_height
= 0;
3692 int client_width
= 0;
3693 GetClientSize( &client_width
, &client_height
);
3699 rect
.SetWidth( client_width
- 2 );
3700 rect
.SetHeight( client_height
- 2 );
3702 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3704 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3705 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3706 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3707 dc
.DrawLine( 0, 0, client_width
, 0 );
3708 dc
.DrawLine( 0, 0, 0, client_height
);
3710 dc
.SetPen( *wxWHITE_PEN
);
3711 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3712 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3717 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3719 m_owner
->ProcessCornerLabelMouseEvent( event
);
3723 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3725 m_owner
->GetEventHandler()->ProcessEvent(event
);
3728 // This seems to be required for wxMotif otherwise the mouse
3729 // cursor must be in the cell edit control to get key events
3731 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3733 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3736 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3738 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3741 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3743 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3747 //////////////////////////////////////////////////////////////////////
3749 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3751 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3752 EVT_PAINT( wxGridWindow::OnPaint
)
3753 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3754 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3755 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3756 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3757 EVT_CHAR ( wxGridWindow::OnChar
)
3758 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3759 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3760 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3763 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3764 wxGridRowLabelWindow
*rowLblWin
,
3765 wxGridColLabelWindow
*colLblWin
,
3768 const wxSize
&size
)
3769 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3770 wxT("grid window") )
3774 m_rowLabelWin
= rowLblWin
;
3775 m_colLabelWin
= colLblWin
;
3779 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3781 wxPaintDC
dc( this );
3782 m_owner
->PrepareDC( dc
);
3783 wxRegion reg
= GetUpdateRegion();
3784 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3785 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3786 #if WXGRID_DRAW_LINES
3787 m_owner
->DrawAllGridLines( dc
, reg
);
3789 m_owner
->DrawGridSpace( dc
);
3790 m_owner
->DrawHighlight( dc
, DirtyCells
);
3794 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3796 wxWindow::ScrollWindow( dx
, dy
, rect
);
3797 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3798 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3802 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3804 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3807 m_owner
->ProcessGridCellMouseEvent( event
);
3810 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3812 m_owner
->GetEventHandler()->ProcessEvent(event
);
3815 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3816 // cursor must be in the cell edit control to get key events
3818 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3820 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3823 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3825 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3828 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3830 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3833 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3837 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3839 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3843 //////////////////////////////////////////////////////////////////////
3845 // Internal Helper function for computing row or column from some
3846 // (unscrolled) coordinate value, using either
3847 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3848 // of m_rowBottoms/m_ColRights to speed up the search!
3850 // Internal helper macros for simpler use of that function
3852 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3853 const wxArrayInt
& BorderArray
, int nMax
,
3856 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3857 m_minAcceptableColWidth, \
3858 m_colRights, m_numCols, true)
3859 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3860 m_minAcceptableRowHeight, \
3861 m_rowBottoms, m_numRows, true)
3862 /////////////////////////////////////////////////////////////////////
3864 #if wxUSE_EXTENDED_RTTI
3865 WX_DEFINE_FLAGS( wxGridStyle
)
3867 wxBEGIN_FLAGS( wxGridStyle
)
3868 // new style border flags, we put them first to
3869 // use them for streaming out
3870 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3871 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3872 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3873 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3874 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3875 wxFLAGS_MEMBER(wxBORDER_NONE
)
3877 // old style border flags
3878 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3879 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3880 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3881 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3882 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3883 wxFLAGS_MEMBER(wxBORDER
)
3885 // standard window styles
3886 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3887 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3888 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3889 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3890 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3891 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3892 wxFLAGS_MEMBER(wxVSCROLL
)
3893 wxFLAGS_MEMBER(wxHSCROLL
)
3895 wxEND_FLAGS( wxGridStyle
)
3897 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3899 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3900 wxHIDE_PROPERTY( Children
)
3901 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3902 wxEND_PROPERTIES_TABLE()
3904 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3905 wxEND_HANDLERS_TABLE()
3907 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3910 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3913 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3916 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3917 EVT_PAINT( wxGrid::OnPaint
)
3918 EVT_SIZE( wxGrid::OnSize
)
3919 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3920 EVT_KEY_UP( wxGrid::OnKeyUp
)
3921 EVT_CHAR ( wxGrid::OnChar
)
3922 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3927 // in order to make sure that a size event is not
3928 // trigerred in a unfinished state
3929 m_cornerLabelWin
= NULL
;
3930 m_rowLabelWin
= NULL
;
3931 m_colLabelWin
= NULL
;
3935 wxGrid::wxGrid( wxWindow
*parent
,
3940 const wxString
& name
)
3941 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3942 m_colMinWidths(GRID_HASH_SIZE
),
3943 m_rowMinHeights(GRID_HASH_SIZE
)
3946 SetBestFittingSize(size
);
3949 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3950 const wxPoint
& pos
, const wxSize
& size
,
3951 long style
, const wxString
& name
)
3953 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3954 style
| wxWANTS_CHARS
, name
))
3957 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3958 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3961 SetBestFittingSize(size
);
3969 // Must do this or ~wxScrollHelper will pop the wrong event handler
3970 SetTargetWindow(this);
3972 wxSafeDecRef(m_defaultCellAttr
);
3974 #ifdef DEBUG_ATTR_CACHE
3975 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3976 wxPrintf(_T("wxGrid attribute cache statistics: "
3977 "total: %u, hits: %u (%u%%)\n"),
3978 total
, gs_nAttrCacheHits
,
3979 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3985 delete m_typeRegistry
;
3991 // ----- internal init and update functions
3994 // NOTE: If using the default visual attributes works everywhere then this can
3995 // be removed as well as the #else cases below.
3996 #define _USE_VISATTR 0
3999 #include "wx/listbox.h"
4002 void wxGrid::Create()
4004 m_created
= false; // set to true by CreateGrid
4006 m_table
= (wxGridTableBase
*) NULL
;
4009 m_cellEditCtrlEnabled
= false;
4011 m_defaultCellAttr
= new wxGridCellAttr();
4013 // Set default cell attributes
4014 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
4015 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
4016 m_defaultCellAttr
->SetFont(GetFont());
4017 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
4018 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
4019 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
4022 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
4023 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
4025 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
4026 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
4029 m_defaultCellAttr
->SetTextColour(
4030 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4031 m_defaultCellAttr
->SetBackgroundColour(
4032 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4037 m_currentCellCoords
= wxGridNoCellCoords
;
4039 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4040 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4042 // create the type registry
4043 m_typeRegistry
= new wxGridTypeRegistry
;
4046 // subwindow components that make up the wxGrid
4047 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4052 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4057 m_colLabelWin
= new wxGridColLabelWindow( this,
4062 m_gridWin
= new wxGridWindow( this,
4069 SetTargetWindow( m_gridWin
);
4072 wxColour gfg
= gva
.colFg
;
4073 wxColour gbg
= gva
.colBg
;
4074 wxColour lfg
= lva
.colFg
;
4075 wxColour lbg
= lva
.colBg
;
4077 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4078 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4079 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4080 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4082 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4083 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4084 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4085 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4086 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4087 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4089 m_gridWin
->SetOwnForegroundColour(gfg
);
4090 m_gridWin
->SetOwnBackgroundColour(gbg
);
4096 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4097 wxGrid::wxGridSelectionModes selmode
)
4099 wxCHECK_MSG( !m_created
,
4101 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4103 m_numRows
= numRows
;
4104 m_numCols
= numCols
;
4106 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4107 m_table
->SetView( this );
4109 m_selection
= new wxGridSelection( this, selmode
);
4118 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4120 wxCHECK_RET( m_created
,
4121 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4123 m_selection
->SetSelectionMode( selmode
);
4126 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4128 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4129 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4131 return m_selection
->GetSelectionMode();
4134 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4135 wxGrid::wxGridSelectionModes selmode
)
4139 // stop all processing
4144 wxGridTableBase
*t
=m_table
;
4157 m_numRows
= table
->GetNumberRows();
4158 m_numCols
= table
->GetNumberCols();
4161 m_table
->SetView( this );
4164 m_selection
= new wxGridSelection( this, selmode
);
4177 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4178 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4180 if ( m_rowLabelWin
)
4182 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4186 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4189 m_labelTextColour
= wxColour( _T("BLACK") );
4192 m_attrCache
.row
= -1;
4193 m_attrCache
.col
= -1;
4194 m_attrCache
.attr
= NULL
;
4196 // TODO: something better than this ?
4198 m_labelFont
= this->GetFont();
4199 m_labelFont
.SetWeight( wxBOLD
);
4201 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4202 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4204 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4205 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4206 m_colLabelTextOrientation
= wxHORIZONTAL
;
4208 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4209 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4211 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4212 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4214 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4215 m_defaultRowHeight
+= 8;
4217 m_defaultRowHeight
+= 4;
4220 m_gridLineColour
= wxColour( 192,192,192 );
4221 m_gridLinesEnabled
= true;
4222 m_cellHighlightColour
= *wxBLACK
;
4223 m_cellHighlightPenWidth
= 2;
4224 m_cellHighlightROPenWidth
= 1;
4226 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4227 m_winCapture
= (wxWindow
*)NULL
;
4228 m_canDragRowSize
= true;
4229 m_canDragColSize
= true;
4230 m_canDragGridSize
= true;
4231 m_canDragCell
= false;
4233 m_dragRowOrCol
= -1;
4234 m_isDragging
= false;
4235 m_startDragPos
= wxDefaultPosition
;
4237 m_waitForSlowClick
= false;
4239 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4240 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4242 m_currentCellCoords
= wxGridNoCellCoords
;
4244 m_selectingTopLeft
= wxGridNoCellCoords
;
4245 m_selectingBottomRight
= wxGridNoCellCoords
;
4246 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4247 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4249 m_editable
= true; // default for whole grid
4251 m_inOnKeyDown
= false;
4257 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4258 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4261 // ----------------------------------------------------------------------------
4262 // the idea is to call these functions only when necessary because they create
4263 // quite big arrays which eat memory mostly unnecessary - in particular, if
4264 // default widths/heights are used for all rows/columns, we may not use these
4267 // with some extra code, it should be possible to only store the
4268 // widths/heights different from default ones but this will be done later...
4269 // ----------------------------------------------------------------------------
4271 void wxGrid::InitRowHeights()
4273 m_rowHeights
.Empty();
4274 m_rowBottoms
.Empty();
4276 m_rowHeights
.Alloc( m_numRows
);
4277 m_rowBottoms
.Alloc( m_numRows
);
4281 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4283 for ( int i
= 0; i
< m_numRows
; i
++ )
4285 rowBottom
+= m_defaultRowHeight
;
4286 m_rowBottoms
.Add( rowBottom
);
4290 void wxGrid::InitColWidths()
4292 m_colWidths
.Empty();
4293 m_colRights
.Empty();
4295 m_colWidths
.Alloc( m_numCols
);
4296 m_colRights
.Alloc( m_numCols
);
4299 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4301 for ( int i
= 0; i
< m_numCols
; i
++ )
4303 colRight
+= m_defaultColWidth
;
4304 m_colRights
.Add( colRight
);
4308 int wxGrid::GetColWidth(int col
) const
4310 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4313 int wxGrid::GetColLeft(int col
) const
4315 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4316 : m_colRights
[col
] - m_colWidths
[col
];
4319 int wxGrid::GetColRight(int col
) const
4321 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4325 int wxGrid::GetRowHeight(int row
) const
4327 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4330 int wxGrid::GetRowTop(int row
) const
4332 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4333 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4336 int wxGrid::GetRowBottom(int row
) const
4338 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4339 : m_rowBottoms
[row
];
4342 void wxGrid::CalcDimensions()
4345 GetClientSize( &cw
, &ch
);
4347 if ( m_rowLabelWin
->IsShown() )
4348 cw
-= m_rowLabelWidth
;
4349 if ( m_colLabelWin
->IsShown() )
4350 ch
-= m_colLabelHeight
;
4353 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4354 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4356 // take into account editor if shown
4357 if( IsCellEditControlShown() )
4360 int r
= m_currentCellCoords
.GetRow();
4361 int c
= m_currentCellCoords
.GetCol();
4362 int x
= GetColLeft(c
);
4363 int y
= GetRowTop(r
);
4365 // how big is the editor
4366 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4367 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4368 editor
->GetControl()->GetSize(&w2
, &h2
);
4371 if( w2
> w
) w
= w2
;
4372 if( h2
> h
) h
= h2
;
4377 // preserve (more or less) the previous position
4379 GetViewStart( &x
, &y
);
4381 // ensure the position is valid for the new scroll ranges
4383 x
= wxMax( w
- 1, 0 );
4385 y
= wxMax( h
- 1, 0 );
4387 // do set scrollbar parameters
4388 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4389 GetScrollX(w
), GetScrollY(h
), x
, y
,
4390 GetBatchCount() != 0);
4392 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4393 // still must reposition the children
4398 void wxGrid::CalcWindowSizes()
4400 // escape if the window is has not been fully created yet
4402 if ( m_cornerLabelWin
== NULL
)
4406 GetClientSize( &cw
, &ch
);
4408 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4409 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4411 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4412 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4414 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4415 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4417 if ( m_gridWin
&& m_gridWin
->IsShown() )
4418 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4422 // this is called when the grid table sends a message to say that it
4423 // has been redimensioned
4425 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4428 bool result
= false;
4430 // Clear the attribute cache as the attribute might refer to a different
4431 // cell than stored in the cache after adding/removing rows/columns.
4433 // By the same reasoning, the editor should be dismissed if columns are
4434 // added or removed. And for consistency, it should IMHO always be
4435 // removed, not only if the cell "underneath" it actually changes.
4436 // For now, I intentionally do not save the editor's content as the
4437 // cell it might want to save that stuff to might no longer exist.
4438 HideCellEditControl();
4440 // if we were using the default widths/heights so far, we must change them
4442 if ( m_colWidths
.IsEmpty() )
4447 if ( m_rowHeights
.IsEmpty() )
4453 switch ( msg
.GetId() )
4455 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4457 size_t pos
= msg
.GetCommandInt();
4458 int numRows
= msg
.GetCommandInt2();
4460 m_numRows
+= numRows
;
4462 if ( !m_rowHeights
.IsEmpty() )
4464 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4465 m_rowBottoms
.Insert( 0, pos
, numRows
);
4468 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4470 for ( i
= pos
; i
< m_numRows
; i
++ )
4472 bottom
+= m_rowHeights
[i
];
4473 m_rowBottoms
[i
] = bottom
;
4476 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4478 // if we have just inserted cols into an empty grid the current
4479 // cell will be undefined...
4481 SetCurrentCell( 0, 0 );
4485 m_selection
->UpdateRows( pos
, numRows
);
4486 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4488 attrProvider
->UpdateAttrRows( pos
, numRows
);
4490 if ( !GetBatchCount() )
4493 m_rowLabelWin
->Refresh();
4499 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4501 int numRows
= msg
.GetCommandInt();
4502 int oldNumRows
= m_numRows
;
4503 m_numRows
+= numRows
;
4505 if ( !m_rowHeights
.IsEmpty() )
4507 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4508 m_rowBottoms
.Add( 0, numRows
);
4511 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4513 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4515 bottom
+= m_rowHeights
[i
];
4516 m_rowBottoms
[i
] = bottom
;
4519 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4521 // if we have just inserted cols into an empty grid the current
4522 // cell will be undefined...
4524 SetCurrentCell( 0, 0 );
4526 if ( !GetBatchCount() )
4529 m_rowLabelWin
->Refresh();
4535 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4537 size_t pos
= msg
.GetCommandInt();
4538 int numRows
= msg
.GetCommandInt2();
4539 m_numRows
-= numRows
;
4541 if ( !m_rowHeights
.IsEmpty() )
4543 m_rowHeights
.RemoveAt( pos
, numRows
);
4544 m_rowBottoms
.RemoveAt( pos
, numRows
);
4547 for ( i
= 0; i
< m_numRows
; i
++ )
4549 h
+= m_rowHeights
[i
];
4550 m_rowBottoms
[i
] = h
;
4555 m_currentCellCoords
= wxGridNoCellCoords
;
4559 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4560 m_currentCellCoords
.Set( 0, 0 );
4564 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4565 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4567 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4568 // ifdef'd out following patch from Paul Gammans
4570 // No need to touch column attributes, unless we
4571 // removed _all_ rows, in this case, we remove
4572 // all column attributes.
4573 // I hate to do this here, but the
4574 // needed data is not available inside UpdateAttrRows.
4575 if ( !GetNumberRows() )
4576 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4579 if ( !GetBatchCount() )
4582 m_rowLabelWin
->Refresh();
4588 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4590 size_t pos
= msg
.GetCommandInt();
4591 int numCols
= msg
.GetCommandInt2();
4592 m_numCols
+= numCols
;
4594 if ( !m_colWidths
.IsEmpty() )
4596 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4597 m_colRights
.Insert( 0, pos
, numCols
);
4600 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4602 for ( i
= pos
; i
< m_numCols
; i
++ )
4604 right
+= m_colWidths
[i
];
4605 m_colRights
[i
] = right
;
4608 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4610 // if we have just inserted cols into an empty grid the current
4611 // cell will be undefined...
4613 SetCurrentCell( 0, 0 );
4617 m_selection
->UpdateCols( pos
, numCols
);
4618 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4620 attrProvider
->UpdateAttrCols( pos
, numCols
);
4621 if ( !GetBatchCount() )
4624 m_colLabelWin
->Refresh();
4631 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4633 int numCols
= msg
.GetCommandInt();
4634 int oldNumCols
= m_numCols
;
4635 m_numCols
+= numCols
;
4636 if ( !m_colWidths
.IsEmpty() )
4638 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4639 m_colRights
.Add( 0, numCols
);
4642 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4644 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4646 right
+= m_colWidths
[i
];
4647 m_colRights
[i
] = right
;
4650 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4652 // if we have just inserted cols into an empty grid the current
4653 // cell will be undefined...
4655 SetCurrentCell( 0, 0 );
4657 if ( !GetBatchCount() )
4660 m_colLabelWin
->Refresh();
4666 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4668 size_t pos
= msg
.GetCommandInt();
4669 int numCols
= msg
.GetCommandInt2();
4670 m_numCols
-= numCols
;
4672 if ( !m_colWidths
.IsEmpty() )
4674 m_colWidths
.RemoveAt( pos
, numCols
);
4675 m_colRights
.RemoveAt( pos
, numCols
);
4678 for ( i
= 0; i
< m_numCols
; i
++ )
4680 w
+= m_colWidths
[i
];
4686 m_currentCellCoords
= wxGridNoCellCoords
;
4690 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4691 m_currentCellCoords
.Set( 0, 0 );
4695 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4696 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4698 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4699 // ifdef'd out following patch from Paul Gammans
4701 // No need to touch row attributes, unless we
4702 // removed _all_ columns, in this case, we remove
4703 // all row attributes.
4704 // I hate to do this here, but the
4705 // needed data is not available inside UpdateAttrCols.
4706 if ( !GetNumberCols() )
4707 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4710 if ( !GetBatchCount() )
4713 m_colLabelWin
->Refresh();
4720 if (result
&& !GetBatchCount() )
4721 m_gridWin
->Refresh();
4726 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4728 wxRegionIterator
iter( reg
);
4731 wxArrayInt rowlabels
;
4738 // TODO: remove this when we can...
4739 // There is a bug in wxMotif that gives garbage update
4740 // rectangles if you jump-scroll a long way by clicking the
4741 // scrollbar with middle button. This is a work-around
4743 #if defined(__WXMOTIF__)
4745 m_gridWin
->GetClientSize( &cw
, &ch
);
4746 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4747 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4750 // logical bounds of update region
4753 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4754 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4756 // find the row labels within these bounds
4759 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4761 if ( GetRowBottom(row
) < top
)
4764 if ( GetRowTop(row
) > bottom
)
4767 rowlabels
.Add( row
);
4777 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4779 wxRegionIterator
iter( reg
);
4782 wxArrayInt colLabels
;
4789 // TODO: remove this when we can...
4790 // There is a bug in wxMotif that gives garbage update
4791 // rectangles if you jump-scroll a long way by clicking the
4792 // scrollbar with middle button. This is a work-around
4794 #if defined(__WXMOTIF__)
4796 m_gridWin
->GetClientSize( &cw
, &ch
);
4797 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4798 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4801 // logical bounds of update region
4804 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4805 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4807 // find the cells within these bounds
4810 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4812 if ( GetColRight(col
) < left
)
4815 if ( GetColLeft(col
) > right
)
4818 colLabels
.Add( col
);
4827 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4829 wxRegionIterator
iter( reg
);
4832 wxGridCellCoordsArray cellsExposed
;
4834 int left
, top
, right
, bottom
;
4839 // TODO: remove this when we can...
4840 // There is a bug in wxMotif that gives garbage update
4841 // rectangles if you jump-scroll a long way by clicking the
4842 // scrollbar with middle button. This is a work-around
4844 #if defined(__WXMOTIF__)
4846 m_gridWin
->GetClientSize( &cw
, &ch
);
4847 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4848 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4849 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4850 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4853 // logical bounds of update region
4855 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4856 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4858 // find the cells within these bounds
4861 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4863 if ( GetRowBottom(row
) <= top
)
4866 if ( GetRowTop(row
) > bottom
)
4869 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4871 if ( GetColRight(col
) <= left
)
4874 if ( GetColLeft(col
) > right
)
4877 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4884 return cellsExposed
;
4888 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4891 wxPoint
pos( event
.GetPosition() );
4892 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4894 if ( event
.Dragging() )
4898 m_isDragging
= true;
4899 m_rowLabelWin
->CaptureMouse();
4902 if ( event
.LeftIsDown() )
4904 switch( m_cursorMode
)
4906 case WXGRID_CURSOR_RESIZE_ROW
:
4908 int cw
, ch
, left
, dummy
;
4909 m_gridWin
->GetClientSize( &cw
, &ch
);
4910 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4912 wxClientDC
dc( m_gridWin
);
4915 GetRowTop(m_dragRowOrCol
) +
4916 GetRowMinimalHeight(m_dragRowOrCol
) );
4917 dc
.SetLogicalFunction(wxINVERT
);
4918 if ( m_dragLastPos
>= 0 )
4920 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4922 dc
.DrawLine( left
, y
, left
+cw
, y
);
4927 case WXGRID_CURSOR_SELECT_ROW
:
4929 if ( (row
= YToRow( y
)) >= 0 )
4933 m_selection
->SelectRow( row
,
4934 event
.ControlDown(),
4943 // default label to suppress warnings about "enumeration value
4944 // 'xxx' not handled in switch
4952 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4957 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4958 m_isDragging
= false;
4961 // ------------ Entering or leaving the window
4963 if ( event
.Entering() || event
.Leaving() )
4965 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4969 // ------------ Left button pressed
4971 else if ( event
.LeftDown() )
4973 // don't send a label click event for a hit on the
4974 // edge of the row label - this is probably the user
4975 // wanting to resize the row
4977 if ( YToEdgeOfRow(y
) < 0 )
4981 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4983 if ( !event
.ShiftDown() && !event
.ControlDown() )
4987 if ( event
.ShiftDown() )
4989 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4992 GetNumberCols() - 1,
4993 event
.ControlDown(),
5000 m_selection
->SelectRow( row
,
5001 event
.ControlDown(),
5008 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
5013 // starting to drag-resize a row
5015 if ( CanDragRowSize() )
5016 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
5021 // ------------ Left double click
5023 else if (event
.LeftDClick() )
5025 int row
= YToEdgeOfRow(y
);
5030 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
5032 // no default action at the moment
5037 // adjust row height depending on label text
5038 AutoSizeRowLabelSize( row
);
5040 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5046 // ------------ Left button released
5048 else if ( event
.LeftUp() )
5050 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5052 DoEndDragResizeRow();
5054 // Note: we are ending the event *after* doing
5055 // default processing in this case
5057 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5060 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5065 // ------------ Right button down
5067 else if ( event
.RightDown() )
5071 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5073 // no default action at the moment
5078 // ------------ Right double click
5080 else if ( event
.RightDClick() )
5084 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5086 // no default action at the moment
5091 // ------------ No buttons down and mouse moving
5093 else if ( event
.Moving() )
5095 m_dragRowOrCol
= YToEdgeOfRow( y
);
5096 if ( m_dragRowOrCol
>= 0 )
5098 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5100 // don't capture the mouse yet
5101 if ( CanDragRowSize() )
5102 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5105 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5107 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5113 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5116 wxPoint
pos( event
.GetPosition() );
5117 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5119 if ( event
.Dragging() )
5123 m_isDragging
= true;
5124 m_colLabelWin
->CaptureMouse();
5127 if ( event
.LeftIsDown() )
5129 switch( m_cursorMode
)
5131 case WXGRID_CURSOR_RESIZE_COL
:
5133 int cw
, ch
, dummy
, top
;
5134 m_gridWin
->GetClientSize( &cw
, &ch
);
5135 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5137 wxClientDC
dc( m_gridWin
);
5140 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5141 GetColMinimalWidth(m_dragRowOrCol
));
5142 dc
.SetLogicalFunction(wxINVERT
);
5143 if ( m_dragLastPos
>= 0 )
5145 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5147 dc
.DrawLine( x
, top
, x
, top
+ch
);
5152 case WXGRID_CURSOR_SELECT_COL
:
5154 if ( (col
= XToCol( x
)) >= 0 )
5158 m_selection
->SelectCol( col
,
5159 event
.ControlDown(),
5168 // default label to suppress warnings about "enumeration value
5169 // 'xxx' not handled in switch
5177 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5182 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5183 m_isDragging
= false;
5186 // ------------ Entering or leaving the window
5188 if ( event
.Entering() || event
.Leaving() )
5190 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5194 // ------------ Left button pressed
5196 else if ( event
.LeftDown() )
5198 // don't send a label click event for a hit on the
5199 // edge of the col label - this is probably the user
5200 // wanting to resize the col
5202 if ( XToEdgeOfCol(x
) < 0 )
5206 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5208 if ( !event
.ShiftDown() && !event
.ControlDown() )
5212 if ( event
.ShiftDown() )
5214 m_selection
->SelectBlock( 0,
5215 m_currentCellCoords
.GetCol(),
5216 GetNumberRows() - 1, col
,
5217 event
.ControlDown(),
5224 m_selection
->SelectCol( col
,
5225 event
.ControlDown(),
5232 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5237 // starting to drag-resize a col
5239 if ( CanDragColSize() )
5240 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5245 // ------------ Left double click
5247 if ( event
.LeftDClick() )
5249 int col
= XToEdgeOfCol(x
);
5254 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5256 // no default action at the moment
5261 // adjust column width depending on label text
5262 AutoSizeColLabelSize( col
);
5264 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5270 // ------------ Left button released
5272 else if ( event
.LeftUp() )
5274 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5276 DoEndDragResizeCol();
5278 // Note: we are ending the event *after* doing
5279 // default processing in this case
5281 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5284 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5289 // ------------ Right button down
5291 else if ( event
.RightDown() )
5295 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5297 // no default action at the moment
5302 // ------------ Right double click
5304 else if ( event
.RightDClick() )
5308 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5310 // no default action at the moment
5315 // ------------ No buttons down and mouse moving
5317 else if ( event
.Moving() )
5319 m_dragRowOrCol
= XToEdgeOfCol( x
);
5320 if ( m_dragRowOrCol
>= 0 )
5322 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5324 // don't capture the cursor yet
5325 if ( CanDragColSize() )
5326 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5329 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5331 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5337 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5339 if ( event
.LeftDown() )
5341 // indicate corner label by having both row and
5344 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5350 else if ( event
.LeftDClick() )
5352 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5355 else if ( event
.RightDown() )
5357 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5359 // no default action at the moment
5363 else if ( event
.RightDClick() )
5365 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5367 // no default action at the moment
5372 void wxGrid::ChangeCursorMode(CursorMode mode
,
5377 static const wxChar
*cursorModes
[] =
5386 wxLogTrace(_T("grid"),
5387 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5388 win
== m_colLabelWin
? _T("colLabelWin")
5389 : win
? _T("rowLabelWin")
5391 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5392 #endif // __WXDEBUG__
5394 if ( mode
== m_cursorMode
&&
5395 win
== m_winCapture
&&
5396 captureMouse
== (m_winCapture
!= NULL
))
5401 // by default use the grid itself
5407 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5408 m_winCapture
= (wxWindow
*)NULL
;
5411 m_cursorMode
= mode
;
5413 switch ( m_cursorMode
)
5415 case WXGRID_CURSOR_RESIZE_ROW
:
5416 win
->SetCursor( m_rowResizeCursor
);
5419 case WXGRID_CURSOR_RESIZE_COL
:
5420 win
->SetCursor( m_colResizeCursor
);
5424 win
->SetCursor( *wxSTANDARD_CURSOR
);
5427 // we need to capture mouse when resizing
5428 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5429 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5431 if ( captureMouse
&& resize
)
5433 win
->CaptureMouse();
5438 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5441 wxPoint
pos( event
.GetPosition() );
5442 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5444 wxGridCellCoords coords
;
5445 XYToCell( x
, y
, coords
);
5447 int cell_rows
, cell_cols
;
5448 bool isFirstDrag
= !m_isDragging
;
5449 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5450 if ((cell_rows
< 0) || (cell_cols
< 0))
5452 coords
.SetRow(coords
.GetRow() + cell_rows
);
5453 coords
.SetCol(coords
.GetCol() + cell_cols
);
5456 if ( event
.Dragging() )
5458 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5460 // Don't start doing anything until the mouse has been drug at
5461 // least 3 pixels in any direction...
5464 if (m_startDragPos
== wxDefaultPosition
)
5466 m_startDragPos
= pos
;
5469 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5473 m_isDragging
= true;
5474 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5476 // Hide the edit control, so it
5477 // won't interfer with drag-shrinking.
5478 if ( IsCellEditControlShown() )
5480 HideCellEditControl();
5481 SaveEditControlValue();
5484 // Have we captured the mouse yet?
5487 m_winCapture
= m_gridWin
;
5488 m_winCapture
->CaptureMouse();
5491 if ( coords
!= wxGridNoCellCoords
)
5493 if ( event
.ControlDown() )
5495 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5496 m_selectingKeyboard
= coords
;
5497 HighlightBlock ( m_selectingKeyboard
, coords
);
5499 else if ( CanDragCell() )
5503 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5504 m_selectingKeyboard
= coords
;
5506 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5514 if ( !IsSelection() )
5516 HighlightBlock( coords
, coords
);
5520 HighlightBlock( m_currentCellCoords
, coords
);
5524 if (! IsVisible(coords
))
5526 MakeCellVisible(coords
);
5527 // TODO: need to introduce a delay or something here. The
5528 // scrolling is way to fast, at least on MSW - also on GTK.
5532 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5534 int cw
, ch
, left
, dummy
;
5535 m_gridWin
->GetClientSize( &cw
, &ch
);
5536 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5538 wxClientDC
dc( m_gridWin
);
5540 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5541 GetRowMinimalHeight(m_dragRowOrCol
) );
5542 dc
.SetLogicalFunction(wxINVERT
);
5543 if ( m_dragLastPos
>= 0 )
5545 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5547 dc
.DrawLine( left
, y
, left
+cw
, y
);
5550 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5552 int cw
, ch
, dummy
, top
;
5553 m_gridWin
->GetClientSize( &cw
, &ch
);
5554 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5556 wxClientDC
dc( m_gridWin
);
5558 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5559 GetColMinimalWidth(m_dragRowOrCol
) );
5560 dc
.SetLogicalFunction(wxINVERT
);
5561 if ( m_dragLastPos
>= 0 )
5563 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5565 dc
.DrawLine( x
, top
, x
, top
+ch
);
5572 m_isDragging
= false;
5573 m_startDragPos
= wxDefaultPosition
;
5575 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5576 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5579 if ( event
.Entering() || event
.Leaving() )
5581 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5582 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5587 // ------------ Left button pressed
5589 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5591 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5596 if ( !event
.ControlDown() )
5598 if ( event
.ShiftDown() )
5602 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5603 m_currentCellCoords
.GetCol(),
5606 event
.ControlDown(),
5612 else if ( XToEdgeOfCol(x
) < 0 &&
5613 YToEdgeOfRow(y
) < 0 )
5615 DisableCellEditControl();
5616 MakeCellVisible( coords
);
5618 if ( event
.ControlDown() )
5622 m_selection
->ToggleCellSelection( coords
.GetRow(),
5624 event
.ControlDown(),
5629 m_selectingTopLeft
= wxGridNoCellCoords
;
5630 m_selectingBottomRight
= wxGridNoCellCoords
;
5631 m_selectingKeyboard
= coords
;
5635 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5636 SetCurrentCell( coords
);
5639 if ( m_selection
->GetSelectionMode() !=
5640 wxGrid::wxGridSelectCells
)
5642 HighlightBlock( coords
, coords
);
5651 // ------------ Left double click
5653 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5655 DisableCellEditControl();
5657 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5659 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5664 // we want double click to select a cell and start editing
5665 // (i.e. to behave in same way as sequence of two slow clicks):
5666 m_waitForSlowClick
= true;
5673 // ------------ Left button released
5675 else if ( event
.LeftUp() )
5677 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5681 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5682 m_winCapture
= NULL
;
5685 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5688 EnableCellEditControl();
5690 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5691 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5692 editor
->StartingClick();
5696 m_waitForSlowClick
= false;
5698 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5699 m_selectingBottomRight
!= wxGridNoCellCoords
)
5703 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5704 m_selectingTopLeft
.GetCol(),
5705 m_selectingBottomRight
.GetRow(),
5706 m_selectingBottomRight
.GetCol(),
5707 event
.ControlDown(),
5713 m_selectingTopLeft
= wxGridNoCellCoords
;
5714 m_selectingBottomRight
= wxGridNoCellCoords
;
5716 // Show the edit control, if it has been hidden for
5718 ShowCellEditControl();
5721 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5723 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5724 DoEndDragResizeRow();
5726 // Note: we are ending the event *after* doing
5727 // default processing in this case
5729 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5731 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5733 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5734 DoEndDragResizeCol();
5736 // Note: we are ending the event *after* doing
5737 // default processing in this case
5739 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5746 // ------------ Right button down
5748 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5750 DisableCellEditControl();
5751 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5756 // no default action at the moment
5761 // ------------ Right double click
5763 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5765 DisableCellEditControl();
5766 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5771 // no default action at the moment
5775 // ------------ Moving and no button action
5777 else if ( event
.Moving() && !event
.IsButton() )
5779 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5781 // out of grid cell area
5782 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5786 int dragRow
= YToEdgeOfRow( y
);
5787 int dragCol
= XToEdgeOfCol( x
);
5789 // Dragging on the corner of a cell to resize in both
5790 // directions is not implemented yet...
5792 if ( dragRow
>= 0 && dragCol
>= 0 )
5794 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5800 m_dragRowOrCol
= dragRow
;
5802 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5804 if ( CanDragRowSize() && CanDragGridSize() )
5805 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5810 m_dragRowOrCol
= dragCol
;
5818 m_dragRowOrCol
= dragCol
;
5820 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5822 if ( CanDragColSize() && CanDragGridSize() )
5823 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5829 // Neither on a row or col edge
5831 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5833 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5839 void wxGrid::DoEndDragResizeRow()
5841 if ( m_dragLastPos
>= 0 )
5843 // erase the last line and resize the row
5845 int cw
, ch
, left
, dummy
;
5846 m_gridWin
->GetClientSize( &cw
, &ch
);
5847 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5849 wxClientDC
dc( m_gridWin
);
5851 dc
.SetLogicalFunction( wxINVERT
);
5852 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5853 HideCellEditControl();
5854 SaveEditControlValue();
5856 int rowTop
= GetRowTop(m_dragRowOrCol
);
5857 SetRowSize( m_dragRowOrCol
,
5858 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5860 if ( !GetBatchCount() )
5862 // Only needed to get the correct rect.y:
5863 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5865 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5866 rect
.width
= m_rowLabelWidth
;
5867 rect
.height
= ch
- rect
.y
;
5868 m_rowLabelWin
->Refresh( true, &rect
);
5870 // if there is a multicell block, paint all of it
5873 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5874 int leftCol
= XToCol(left
);
5875 int rightCol
= internalXToCol(left
+cw
);
5878 for (i
=leftCol
; i
<rightCol
; i
++)
5880 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5881 if (cell_rows
< subtract_rows
)
5882 subtract_rows
= cell_rows
;
5884 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5885 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5886 rect
.height
= ch
- rect
.y
;
5889 m_gridWin
->Refresh( false, &rect
);
5892 ShowCellEditControl();
5897 void wxGrid::DoEndDragResizeCol()
5899 if ( m_dragLastPos
>= 0 )
5901 // erase the last line and resize the col
5903 int cw
, ch
, dummy
, top
;
5904 m_gridWin
->GetClientSize( &cw
, &ch
);
5905 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5907 wxClientDC
dc( m_gridWin
);
5909 dc
.SetLogicalFunction( wxINVERT
);
5910 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5911 HideCellEditControl();
5912 SaveEditControlValue();
5914 int colLeft
= GetColLeft(m_dragRowOrCol
);
5915 SetColSize( m_dragRowOrCol
,
5916 wxMax( m_dragLastPos
- colLeft
,
5917 GetColMinimalWidth(m_dragRowOrCol
) ) );
5919 if ( !GetBatchCount() )
5921 // Only needed to get the correct rect.x:
5922 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5924 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5925 rect
.width
= cw
- rect
.x
;
5926 rect
.height
= m_colLabelHeight
;
5927 m_colLabelWin
->Refresh( true, &rect
);
5929 // if there is a multicell block, paint all of it
5932 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5933 int topRow
= YToRow(top
);
5934 int bottomRow
= internalYToRow(top
+cw
);
5937 for (i
=topRow
; i
<bottomRow
; i
++)
5939 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5940 if (cell_cols
< subtract_cols
)
5941 subtract_cols
= cell_cols
;
5943 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5944 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5945 rect
.width
= cw
- rect
.x
;
5948 m_gridWin
->Refresh( false, &rect
);
5951 ShowCellEditControl();
5958 // ------ interaction with data model
5960 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5962 switch ( msg
.GetId() )
5964 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5965 return GetModelValues();
5967 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5968 return SetModelValues();
5970 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5971 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5972 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5973 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5974 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5975 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5976 return Redimension( msg
);
5985 // The behaviour of this function depends on the grid table class
5986 // Clear() function. For the default wxGridStringTable class the
5987 // behavious is to replace all cell contents with wxEmptyString but
5988 // not to change the number of rows or cols.
5990 void wxGrid::ClearGrid()
5994 if (IsCellEditControlEnabled())
5995 DisableCellEditControl();
5998 if ( !GetBatchCount() ) m_gridWin
->Refresh();
6003 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6005 // TODO: something with updateLabels flag
6009 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
6015 if (IsCellEditControlEnabled())
6016 DisableCellEditControl();
6018 bool done
= m_table
->InsertRows( pos
, numRows
);
6021 // the table will have sent the results of the insert row
6022 // operation to this view object as a grid table message
6028 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
6030 // TODO: something with updateLabels flag
6034 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6040 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6042 // the table will have sent the results of the append row
6043 // operation to this view object as a grid table message
6049 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6051 // TODO: something with updateLabels flag
6055 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6061 if (IsCellEditControlEnabled())
6062 DisableCellEditControl();
6064 bool done
= m_table
->DeleteRows( pos
, numRows
);
6066 // the table will have sent the results of the delete row
6067 // operation to this view object as a grid table message
6073 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6075 // TODO: something with updateLabels flag
6079 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6085 if (IsCellEditControlEnabled())
6086 DisableCellEditControl();
6088 bool done
= m_table
->InsertCols( pos
, numCols
);
6090 // the table will have sent the results of the insert col
6091 // operation to this view object as a grid table message
6097 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6099 // TODO: something with updateLabels flag
6103 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6109 bool done
= m_table
->AppendCols( numCols
);
6111 // the table will have sent the results of the append col
6112 // operation to this view object as a grid table message
6118 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6120 // TODO: something with updateLabels flag
6124 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6130 if (IsCellEditControlEnabled())
6131 DisableCellEditControl();
6133 bool done
= m_table
->DeleteCols( pos
, numCols
);
6135 // the table will have sent the results of the delete col
6136 // operation to this view object as a grid table message
6144 // ----- event handlers
6147 // Generate a grid event based on a mouse event and
6148 // return the result of ProcessEvent()
6150 int wxGrid::SendEvent( const wxEventType type
,
6152 wxMouseEvent
& mouseEv
)
6157 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6159 int rowOrCol
= (row
== -1 ? col
: row
);
6161 wxGridSizeEvent
gridEvt( GetId(),
6165 mouseEv
.GetX() + GetRowLabelSize(),
6166 mouseEv
.GetY() + GetColLabelSize(),
6167 mouseEv
.ControlDown(),
6168 mouseEv
.ShiftDown(),
6170 mouseEv
.MetaDown() );
6172 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6173 vetoed
= !gridEvt
.IsAllowed();
6175 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6177 // Right now, it should _never_ end up here!
6178 wxGridRangeSelectEvent
gridEvt( GetId(),
6182 m_selectingBottomRight
,
6184 mouseEv
.ControlDown(),
6185 mouseEv
.ShiftDown(),
6187 mouseEv
.MetaDown() );
6189 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6190 vetoed
= !gridEvt
.IsAllowed();
6194 wxGridEvent
gridEvt( GetId(),
6198 mouseEv
.GetX() + GetRowLabelSize(),
6199 mouseEv
.GetY() + GetColLabelSize(),
6201 mouseEv
.ControlDown(),
6202 mouseEv
.ShiftDown(),
6204 mouseEv
.MetaDown() );
6205 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6206 vetoed
= !gridEvt
.IsAllowed();
6209 // A Veto'd event may not be `claimed' so test this first
6210 if (vetoed
) return -1;
6211 return claimed
? 1 : 0;
6215 // Generate a grid event of specified type and return the result
6216 // of ProcessEvent().
6218 int wxGrid::SendEvent( const wxEventType type
,
6224 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6226 int rowOrCol
= (row
== -1 ? col
: row
);
6228 wxGridSizeEvent
gridEvt( GetId(),
6233 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6234 vetoed
= !gridEvt
.IsAllowed();
6238 wxGridEvent
gridEvt( GetId(),
6243 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6244 vetoed
= !gridEvt
.IsAllowed();
6247 // A Veto'd event may not be `claimed' so test this first
6248 if (vetoed
) return -1;
6249 return claimed
? 1 : 0;
6253 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6255 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6258 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6260 // Don't do anything if between Begin/EndBatch...
6261 // EndBatch() will do all this on the last nested one anyway.
6262 if (! GetBatchCount())
6264 // Refresh to get correct scrolled position:
6265 wxScrolledWindow::Refresh(eraseb
,rect
);
6269 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6270 int width_label
, width_cell
, height_label
, height_cell
;
6273 //Copy rectangle can get scroll offsets..
6274 rect_x
= rect
->GetX();
6275 rect_y
= rect
->GetY();
6276 rectWidth
= rect
->GetWidth();
6277 rectHeight
= rect
->GetHeight();
6279 width_label
= m_rowLabelWidth
- rect_x
;
6280 if (width_label
> rectWidth
) width_label
= rectWidth
;
6282 height_label
= m_colLabelHeight
- rect_y
;
6283 if (height_label
> rectHeight
) height_label
= rectHeight
;
6285 if (rect_x
> m_rowLabelWidth
)
6287 x
= rect_x
- m_rowLabelWidth
;
6288 width_cell
= rectWidth
;
6293 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6296 if (rect_y
> m_colLabelHeight
)
6298 y
= rect_y
- m_colLabelHeight
;
6299 height_cell
= rectHeight
;
6304 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6307 // Paint corner label part intersecting rect.
6308 if ( width_label
> 0 && height_label
> 0 )
6310 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6311 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6314 // Paint col labels part intersecting rect.
6315 if ( width_cell
> 0 && height_label
> 0 )
6317 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6318 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6321 // Paint row labels part intersecting rect.
6322 if ( width_label
> 0 && height_cell
> 0 )
6324 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6325 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6328 // Paint cell area part intersecting rect.
6329 if ( width_cell
> 0 && height_cell
> 0 )
6331 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6332 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6337 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6338 m_colLabelWin
->Refresh(eraseb
, NULL
);
6339 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6340 m_gridWin
->Refresh(eraseb
, NULL
);
6345 void wxGrid::OnSize( wxSizeEvent
& event
)
6347 // position the child windows
6350 // don't call CalcDimensions() from here, the base class handles the size
6356 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6358 if ( m_inOnKeyDown
)
6360 // shouldn't be here - we are going round in circles...
6362 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6365 m_inOnKeyDown
= true;
6367 // propagate the event up and see if it gets processed
6369 wxWindow
*parent
= GetParent();
6370 wxKeyEvent
keyEvt( event
);
6371 keyEvt
.SetEventObject( parent
);
6373 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6376 // try local handlers
6378 switch ( event
.GetKeyCode() )
6381 if ( event
.ControlDown() )
6383 MoveCursorUpBlock( event
.ShiftDown() );
6387 MoveCursorUp( event
.ShiftDown() );
6392 if ( event
.ControlDown() )
6394 MoveCursorDownBlock( event
.ShiftDown() );
6398 MoveCursorDown( event
.ShiftDown() );
6403 if ( event
.ControlDown() )
6405 MoveCursorLeftBlock( event
.ShiftDown() );
6409 MoveCursorLeft( event
.ShiftDown() );
6414 if ( event
.ControlDown() )
6416 MoveCursorRightBlock( event
.ShiftDown() );
6420 MoveCursorRight( event
.ShiftDown() );
6425 case WXK_NUMPAD_ENTER
:
6426 if ( event
.ControlDown() )
6428 event
.Skip(); // to let the edit control have the return
6432 if ( GetGridCursorRow() < GetNumberRows()-1 )
6434 MoveCursorDown( event
.ShiftDown() );
6438 // at the bottom of a column
6439 DisableCellEditControl();
6449 if (event
.ShiftDown())
6451 if ( GetGridCursorCol() > 0 )
6453 MoveCursorLeft( false );
6458 DisableCellEditControl();
6463 if ( GetGridCursorCol() < GetNumberCols()-1 )
6465 MoveCursorRight( false );
6470 DisableCellEditControl();
6476 if ( event
.ControlDown() )
6478 MakeCellVisible( 0, 0 );
6479 SetCurrentCell( 0, 0 );
6488 if ( event
.ControlDown() )
6490 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6491 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6508 if ( event
.ControlDown() )
6512 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6513 m_currentCellCoords
.GetCol(),
6514 event
.ControlDown(),
6521 if ( !IsEditable() )
6523 MoveCursorRight( false );
6526 // Otherwise fall through to default
6534 m_inOnKeyDown
= false;
6537 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6539 // try local handlers
6541 if ( event
.GetKeyCode() == WXK_SHIFT
)
6543 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6544 m_selectingBottomRight
!= wxGridNoCellCoords
)
6548 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6549 m_selectingTopLeft
.GetCol(),
6550 m_selectingBottomRight
.GetRow(),
6551 m_selectingBottomRight
.GetCol(),
6552 event
.ControlDown(),
6559 m_selectingTopLeft
= wxGridNoCellCoords
;
6560 m_selectingBottomRight
= wxGridNoCellCoords
;
6561 m_selectingKeyboard
= wxGridNoCellCoords
;
6565 void wxGrid::OnChar( wxKeyEvent
& event
)
6567 // is it possible to edit the current cell at all?
6568 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6570 // yes, now check whether the cells editor accepts the key
6571 int row
= m_currentCellCoords
.GetRow();
6572 int col
= m_currentCellCoords
.GetCol();
6573 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6574 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6576 // <F2> is special and will always start editing, for
6577 // other keys - ask the editor itself
6578 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6579 || editor
->IsAcceptedKey(event
) )
6581 // ensure cell is visble
6582 MakeCellVisible(row
, col
);
6583 EnableCellEditControl();
6585 // a problem can arise if the cell is not completely
6586 // visible (even after calling MakeCellVisible the
6587 // control is not created and calling StartingKey will
6589 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6590 editor
->StartingKey(event
);
6607 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6611 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6613 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6615 // the event has been intercepted - do nothing
6619 wxClientDC
dc(m_gridWin
);
6622 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6624 DisableCellEditControl();
6626 if ( IsVisible( m_currentCellCoords
, false ) )
6629 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6630 if ( !m_gridLinesEnabled
)
6638 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6640 // Otherwise refresh redraws the highlight!
6641 m_currentCellCoords
= coords
;
6643 DrawGridCellArea(dc
,cells
);
6644 DrawAllGridLines( dc
, r
);
6648 m_currentCellCoords
= coords
;
6650 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6651 DrawCellHighlight(dc
, attr
);
6656 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6659 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6663 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6666 rightCol
= GetNumberCols() - 1;
6668 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6671 bottomRow
= GetNumberRows() - 1;
6675 if ( topRow
> bottomRow
)
6682 if ( leftCol
> rightCol
)
6689 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6690 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6692 // First the case that we selected a completely new area
6693 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6694 m_selectingBottomRight
== wxGridNoCellCoords
)
6697 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6698 wxGridCellCoords ( bottomRow
, rightCol
) );
6699 m_gridWin
->Refresh( false, &rect
);
6701 // Now handle changing an existing selection area.
6702 else if ( m_selectingTopLeft
!= updateTopLeft
||
6703 m_selectingBottomRight
!= updateBottomRight
)
6705 // Compute two optimal update rectangles:
6706 // Either one rectangle is a real subset of the
6707 // other, or they are (almost) disjoint!
6709 bool need_refresh
[4];
6713 need_refresh
[3] = false;
6716 // Store intermediate values
6717 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6718 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6719 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6720 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6722 // Determine the outer/inner coordinates.
6723 if (oldLeft
> leftCol
)
6729 if (oldTop
> topRow
)
6735 if (oldRight
< rightCol
)
6738 oldRight
= rightCol
;
6741 if (oldBottom
< bottomRow
)
6744 oldBottom
= bottomRow
;
6748 // Now, either the stuff marked old is the outer
6749 // rectangle or we don't have a situation where one
6750 // is contained in the other.
6752 if ( oldLeft
< leftCol
)
6754 // Refresh the newly selected or deselected
6755 // area to the left of the old or new selection.
6756 need_refresh
[0] = true;
6757 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6759 wxGridCellCoords ( oldBottom
,
6763 if ( oldTop
< topRow
)
6765 // Refresh the newly selected or deselected
6766 // area above the old or new selection.
6767 need_refresh
[1] = true;
6768 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6770 wxGridCellCoords ( topRow
- 1,
6774 if ( oldRight
> rightCol
)
6776 // Refresh the newly selected or deselected
6777 // area to the right of the old or new selection.
6778 need_refresh
[2] = true;
6779 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6781 wxGridCellCoords ( oldBottom
,
6785 if ( oldBottom
> bottomRow
)
6787 // Refresh the newly selected or deselected
6788 // area below the old or new selection.
6789 need_refresh
[3] = true;
6790 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6792 wxGridCellCoords ( oldBottom
,
6796 // various Refresh() calls
6797 for (i
= 0; i
< 4; i
++ )
6798 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6799 m_gridWin
->Refresh( false, &(rect
[i
]) );
6802 m_selectingTopLeft
= updateTopLeft
;
6803 m_selectingBottomRight
= updateBottomRight
;
6807 // ------ functions to get/send data (see also public functions)
6810 bool wxGrid::GetModelValues()
6812 // Hide the editor, so it won't hide a changed value.
6813 HideCellEditControl();
6817 // all we need to do is repaint the grid
6819 m_gridWin
->Refresh();
6827 bool wxGrid::SetModelValues()
6831 // Disable the editor, so it won't hide a changed value.
6832 // Do we also want to save the current value of the editor first?
6834 DisableCellEditControl();
6838 for ( row
= 0; row
< m_numRows
; row
++ )
6840 for ( col
= 0; col
< m_numCols
; col
++ )
6842 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6854 // Note - this function only draws cells that are in the list of
6855 // exposed cells (usually set from the update region by
6856 // CalcExposedCells)
6858 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6860 if ( !m_numRows
|| !m_numCols
) return;
6862 int i
, numCells
= cells
.GetCount();
6863 int row
, col
, cell_rows
, cell_cols
;
6864 wxGridCellCoordsArray redrawCells
;
6866 for ( i
= numCells
-1; i
>= 0; i
-- )
6868 row
= cells
[i
].GetRow();
6869 col
= cells
[i
].GetCol();
6870 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6872 // If this cell is part of a multicell block, find owner for repaint
6873 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6875 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6876 bool marked
= false;
6877 for ( int j
= 0; j
< numCells
; j
++ )
6879 if ( cell
== cells
[j
] )
6887 int count
= redrawCells
.GetCount();
6888 for (int j
= 0; j
< count
; j
++)
6890 if ( cell
== redrawCells
[j
] )
6896 if (!marked
) redrawCells
.Add( cell
);
6898 continue; // don't bother drawing this cell
6901 // If this cell is empty, find cell to left that might want to overflow
6902 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6904 for ( int l
= 0; l
< cell_rows
; l
++ )
6906 // find a cell in this row to left alreay marked for repaint
6908 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6909 if ((redrawCells
[k
].GetCol() < left
) &&
6910 (redrawCells
[k
].GetRow() == row
))
6911 left
=redrawCells
[k
].GetCol();
6913 if (left
== col
) left
= 0; // oh well
6915 for (int j
= col
-1; j
>= left
; j
--)
6917 if (!m_table
->IsEmptyCell(row
+l
, j
))
6919 if (GetCellOverflow(row
+l
, j
))
6921 wxGridCellCoords
cell(row
+l
, j
);
6922 bool marked
= false;
6924 for (int k
= 0; k
< numCells
; k
++)
6926 if ( cell
== cells
[k
] )
6934 int count
= redrawCells
.GetCount();
6935 for (int k
= 0; k
< count
; k
++)
6937 if ( cell
== redrawCells
[k
] )
6943 if (!marked
) redrawCells
.Add( cell
);
6951 DrawCell( dc
, cells
[i
] );
6954 numCells
= redrawCells
.GetCount();
6956 for ( i
= numCells
- 1; i
>= 0; i
-- )
6958 DrawCell( dc
, redrawCells
[i
] );
6963 void wxGrid::DrawGridSpace( wxDC
& dc
)
6966 m_gridWin
->GetClientSize( &cw
, &ch
);
6969 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6971 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6972 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6974 if ( right
> rightCol
|| bottom
> bottomRow
)
6977 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6979 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6980 dc
.SetPen( *wxTRANSPARENT_PEN
);
6982 if ( right
> rightCol
)
6984 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6987 if ( bottom
> bottomRow
)
6989 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6995 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6997 int row
= coords
.GetRow();
6998 int col
= coords
.GetCol();
7000 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7003 // we draw the cell border ourselves
7004 #if !WXGRID_DRAW_LINES
7005 if ( m_gridLinesEnabled
)
7006 DrawCellBorder( dc
, coords
);
7009 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7011 bool isCurrent
= coords
== m_currentCellCoords
;
7013 wxRect rect
= CellToRect( row
, col
);
7015 // if the editor is shown, we should use it and not the renderer
7016 // Note: However, only if it is really _shown_, i.e. not hidden!
7017 if ( isCurrent
&& IsCellEditControlShown() )
7019 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7020 editor
->PaintBackground(rect
, attr
);
7025 // but all the rest is drawn by the cell renderer and hence may be
7027 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
7028 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
7035 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7037 int row
= m_currentCellCoords
.GetRow();
7038 int col
= m_currentCellCoords
.GetCol();
7040 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7043 wxRect rect
= CellToRect(row
, col
);
7045 // hmmm... what could we do here to show that the cell is disabled?
7046 // for now, I just draw a thinner border than for the other ones, but
7047 // it doesn't look really good
7049 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7053 // The center of th drawn line is where the position/width/height of
7054 // the rectangle is actually at, (on wxMSW atr least,) so we will
7055 // reduce the size of the rectangle to compensate for the thickness of
7056 // the line. If this is too strange on non wxMSW platforms then
7057 // please #ifdef this appropriately.
7058 rect
.x
+= penWidth
/2;
7059 rect
.y
+= penWidth
/2;
7060 rect
.width
-= penWidth
-1;
7061 rect
.height
-= penWidth
-1;
7064 // Now draw the rectangle
7065 // use the cellHighlightColour if the cell is inside a selection, this
7066 // will ensure the cell is always visible.
7067 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7068 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7069 dc
.DrawRectangle(rect
);
7073 // VZ: my experiments with 3d borders...
7075 // how to properly set colours for arbitrary bg?
7076 wxCoord x1
= rect
.x
,
7078 x2
= rect
.x
+ rect
.width
-1,
7079 y2
= rect
.y
+ rect
.height
-1;
7081 dc
.SetPen(*wxWHITE_PEN
);
7082 dc
.DrawLine(x1
, y1
, x2
, y1
);
7083 dc
.DrawLine(x1
, y1
, x1
, y2
);
7085 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7086 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7088 dc
.SetPen(*wxBLACK_PEN
);
7089 dc
.DrawLine(x1
, y2
, x2
, y2
);
7090 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7095 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7097 int row
= coords
.GetRow();
7098 int col
= coords
.GetCol();
7099 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7102 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7104 wxRect rect
= CellToRect( row
, col
);
7106 // right hand border
7108 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7109 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7113 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7114 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7117 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7119 // This if block was previously in wxGrid::OnPaint but that doesn't
7120 // seem to get called under wxGTK - MB
7122 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7123 m_numRows
&& m_numCols
)
7125 m_currentCellCoords
.Set(0, 0);
7128 if ( IsCellEditControlShown() )
7130 // don't show highlight when the edit control is shown
7134 // if the active cell was repainted, repaint its highlight too because it
7135 // might have been damaged by the grid lines
7136 size_t count
= cells
.GetCount();
7137 for ( size_t n
= 0; n
< count
; n
++ )
7139 if ( cells
[n
] == m_currentCellCoords
)
7141 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7142 DrawCellHighlight(dc
, attr
);
7150 // TODO: remove this ???
7151 // This is used to redraw all grid lines e.g. when the grid line colour
7154 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7156 #if !WXGRID_DRAW_LINES
7160 if ( !m_gridLinesEnabled
||
7162 !m_numCols
) return;
7164 int top
, bottom
, left
, right
;
7166 #if 0 //#ifndef __WXGTK__
7170 m_gridWin
->GetClientSize(&cw
, &ch
);
7172 // virtual coords of visible area
7174 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7175 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7180 reg
.GetBox(x
, y
, w
, h
);
7181 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7182 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7186 m_gridWin
->GetClientSize(&cw
, &ch
);
7187 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7188 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7191 // avoid drawing grid lines past the last row and col
7193 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7194 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7196 // no gridlines inside multicells, clip them out
7197 int leftCol
= internalXToCol(left
);
7198 int topRow
= internalYToRow(top
);
7199 int rightCol
= internalXToCol(right
);
7200 int bottomRow
= internalYToRow(bottom
);
7203 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7204 wxRegion
clippedcells(0, 0, cw
, ch
);
7206 int i
, j
, cell_rows
, cell_cols
;
7209 for (j
=topRow
; j
<bottomRow
; j
++)
7211 for (i
=leftCol
; i
<rightCol
; i
++)
7213 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7214 if ((cell_rows
> 1) || (cell_cols
> 1))
7216 rect
= CellToRect(j
,i
);
7217 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7218 clippedcells
.Subtract(rect
);
7220 else if ((cell_rows
< 0) || (cell_cols
< 0))
7222 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7223 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7224 clippedcells
.Subtract(rect
);
7229 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7231 int i
, j
, cell_rows
, cell_cols
;
7234 for (j
=topRow
; j
<bottomRow
; j
++)
7236 for (i
=leftCol
; i
<rightCol
; i
++)
7238 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7239 if ((cell_rows
> 1) || (cell_cols
> 1))
7241 rect
= CellToRect(j
,i
);
7242 clippedcells
.Subtract(rect
);
7244 else if ((cell_rows
< 0) || (cell_cols
< 0))
7246 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7247 clippedcells
.Subtract(rect
);
7252 dc
.SetClippingRegion( clippedcells
);
7254 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7256 // horizontal grid lines
7258 // already declared above - int i;
7259 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7261 int bot
= GetRowBottom(i
) - 1;
7270 dc
.DrawLine( left
, bot
, right
, bot
);
7275 // vertical grid lines
7277 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7279 int colRight
= GetColRight(i
) - 1;
7280 if ( colRight
> right
)
7285 if ( colRight
>= left
)
7287 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7290 dc
.DestroyClippingRegion();
7294 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7296 if ( !m_numRows
) return;
7299 size_t numLabels
= rows
.GetCount();
7301 for ( i
= 0; i
< numLabels
; i
++ )
7303 DrawRowLabel( dc
, rows
[i
] );
7308 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7310 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7316 rect
.SetY( GetRowTop(row
) + 1 );
7317 rect
.SetWidth( m_rowLabelWidth
- 2 );
7318 rect
.SetHeight( GetRowHeight(row
) - 2 );
7320 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7322 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7324 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7326 int rowTop
= GetRowTop(row
),
7327 rowBottom
= GetRowBottom(row
) - 1;
7329 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7330 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7331 m_rowLabelWidth
-1, rowBottom
);
7333 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7335 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7337 dc
.SetPen( *wxWHITE_PEN
);
7338 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7339 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7341 dc
.SetBackgroundMode( wxTRANSPARENT
);
7342 dc
.SetTextForeground( GetLabelTextColour() );
7343 dc
.SetFont( GetLabelFont() );
7346 GetRowLabelAlignment( &hAlign
, &vAlign
);
7349 rect
.SetY( GetRowTop(row
) + 2 );
7350 rect
.SetWidth( m_rowLabelWidth
- 4 );
7351 rect
.SetHeight( GetRowHeight(row
) - 4 );
7352 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7356 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7358 if ( !m_numCols
) return;
7361 size_t numLabels
= cols
.GetCount();
7363 for ( i
= 0; i
< numLabels
; i
++ )
7365 DrawColLabel( dc
, cols
[i
] );
7370 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7372 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7375 int colLeft
= GetColLeft(col
);
7379 rect
.SetX( colLeft
+ 1 );
7381 rect
.SetWidth( GetColWidth(col
) - 2 );
7382 rect
.SetHeight( m_colLabelHeight
- 2 );
7384 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7386 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7388 int colRight
= GetColRight(col
) - 1;
7390 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7391 dc
.DrawLine( colRight
, 0,
7392 colRight
, m_colLabelHeight
-1 );
7394 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7396 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7397 colRight
+1, m_colLabelHeight
-1 );
7399 dc
.SetPen( *wxWHITE_PEN
);
7400 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7401 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7403 dc
.SetBackgroundMode( wxTRANSPARENT
);
7404 dc
.SetTextForeground( GetLabelTextColour() );
7405 dc
.SetFont( GetLabelFont() );
7407 int hAlign
, vAlign
, orient
;
7408 GetColLabelAlignment( &hAlign
, &vAlign
);
7409 orient
= GetColLabelTextOrientation();
7411 rect
.SetX( colLeft
+ 2 );
7413 rect
.SetWidth( GetColWidth(col
) - 4 );
7414 rect
.SetHeight( m_colLabelHeight
- 4 );
7415 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7418 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7419 const wxString
& value
,
7423 int textOrientation
)
7425 wxArrayString lines
;
7427 StringToLines( value
, lines
);
7430 //Forward to new API.
7431 DrawTextRectangle( dc
,
7440 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7441 const wxArrayString
& lines
,
7445 int textOrientation
)
7447 long textWidth
, textHeight
;
7448 long lineWidth
, lineHeight
;
7451 dc
.SetClippingRegion( rect
);
7453 nLines
= lines
.GetCount();
7457 float x
= 0.0, y
= 0.0;
7459 if( textOrientation
== wxHORIZONTAL
)
7460 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7462 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7466 case wxALIGN_BOTTOM
:
7467 if( textOrientation
== wxHORIZONTAL
)
7468 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7470 x
= rect
.x
+ rect
.width
- textWidth
;
7473 case wxALIGN_CENTRE
:
7474 if( textOrientation
== wxHORIZONTAL
)
7475 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7477 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7482 if( textOrientation
== wxHORIZONTAL
)
7489 // Align each line of a multi-line label
7490 for( l
= 0; l
< nLines
; l
++ )
7492 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7494 switch( horizAlign
)
7497 if( textOrientation
== wxHORIZONTAL
)
7498 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7500 y
= rect
.y
+ lineWidth
+ 1;
7503 case wxALIGN_CENTRE
:
7504 if( textOrientation
== wxHORIZONTAL
)
7505 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7507 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7512 if( textOrientation
== wxHORIZONTAL
)
7515 y
= rect
.y
+ rect
.height
- 1;
7519 if( textOrientation
== wxHORIZONTAL
)
7521 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7526 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7531 dc
.DestroyClippingRegion();
7535 // Split multi line text up into an array of strings. Any existing
7536 // contents of the string array are preserved.
7538 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7542 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7543 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7545 while ( startPos
< (int)tVal
.Length() )
7547 pos
= tVal
.Mid(startPos
).Find( eol
);
7552 else if ( pos
== 0 )
7554 lines
.Add( wxEmptyString
);
7558 lines
.Add( value
.Mid(startPos
, pos
) );
7562 if ( startPos
< (int)value
.Length() )
7564 lines
.Add( value
.Mid( startPos
) );
7569 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7570 const wxArrayString
& lines
,
7571 long *width
, long *height
)
7578 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7580 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7581 w
= wxMax( w
, lineW
);
7590 // ------ Batch processing.
7592 void wxGrid::EndBatch()
7594 if ( m_batchCount
> 0 )
7597 if ( !m_batchCount
)
7600 m_rowLabelWin
->Refresh();
7601 m_colLabelWin
->Refresh();
7602 m_cornerLabelWin
->Refresh();
7603 m_gridWin
->Refresh();
7608 // Use this, rather than wxWindow::Refresh(), to force an immediate
7609 // repainting of the grid. Has no effect if you are already inside a
7610 // BeginBatch / EndBatch block.
7612 void wxGrid::ForceRefresh()
7618 bool wxGrid::Enable(bool enable
)
7620 if ( !wxScrolledWindow::Enable(enable
) )
7623 // redraw in the new state
7624 m_gridWin
->Refresh();
7630 // ------ Edit control functions
7634 void wxGrid::EnableEditing( bool edit
)
7636 // TODO: improve this ?
7638 if ( edit
!= m_editable
)
7640 if(!edit
) EnableCellEditControl(edit
);
7646 void wxGrid::EnableCellEditControl( bool enable
)
7651 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7652 SetCurrentCell( 0, 0 );
7654 if ( enable
!= m_cellEditCtrlEnabled
)
7658 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7661 // this should be checked by the caller!
7662 wxASSERT_MSG( CanEnableCellControl(),
7663 _T("can't enable editing for this cell!") );
7665 // do it before ShowCellEditControl()
7666 m_cellEditCtrlEnabled
= enable
;
7668 ShowCellEditControl();
7672 //FIXME:add veto support
7673 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7675 HideCellEditControl();
7676 SaveEditControlValue();
7678 // do it after HideCellEditControl()
7679 m_cellEditCtrlEnabled
= enable
;
7684 bool wxGrid::IsCurrentCellReadOnly() const
7687 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7688 bool readonly
= attr
->IsReadOnly();
7694 bool wxGrid::CanEnableCellControl() const
7696 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7697 !IsCurrentCellReadOnly();
7701 bool wxGrid::IsCellEditControlEnabled() const
7703 // the cell edit control might be disable for all cells or just for the
7704 // current one if it's read only
7705 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7708 bool wxGrid::IsCellEditControlShown() const
7710 bool isShown
= false;
7712 if ( m_cellEditCtrlEnabled
)
7714 int row
= m_currentCellCoords
.GetRow();
7715 int col
= m_currentCellCoords
.GetCol();
7716 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7717 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7722 if ( editor
->IsCreated() )
7724 isShown
= editor
->GetControl()->IsShown();
7734 void wxGrid::ShowCellEditControl()
7736 if ( IsCellEditControlEnabled() )
7738 if ( !IsVisible( m_currentCellCoords
) )
7740 m_cellEditCtrlEnabled
= false;
7745 wxRect rect
= CellToRect( m_currentCellCoords
);
7746 int row
= m_currentCellCoords
.GetRow();
7747 int col
= m_currentCellCoords
.GetCol();
7749 // if this is part of a multicell, find owner (topleft)
7750 int cell_rows
, cell_cols
;
7751 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7752 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7756 m_currentCellCoords
.SetRow( row
);
7757 m_currentCellCoords
.SetCol( col
);
7760 // convert to scrolled coords
7762 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7764 // done in PaintBackground()
7766 // erase the highlight and the cell contents because the editor
7767 // might not cover the entire cell
7768 wxClientDC
dc( m_gridWin
);
7770 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7771 dc
.SetPen(*wxTRANSPARENT_PEN
);
7772 dc
.DrawRectangle(rect
);
7775 // cell is shifted by one pixel
7776 // However, don't allow x or y to become negative
7777 // since the SetSize() method interprets that as
7784 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7785 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7786 if ( !editor
->IsCreated() )
7788 editor
->Create(m_gridWin
, wxID_ANY
,
7789 new wxGridCellEditorEvtHandler(this, editor
));
7791 wxGridEditorCreatedEvent
evt(GetId(),
7792 wxEVT_GRID_EDITOR_CREATED
,
7796 editor
->GetControl());
7797 GetEventHandler()->ProcessEvent(evt
);
7801 // resize editor to overflow into righthand cells if allowed
7802 int maxWidth
= rect
.width
;
7803 wxString value
= GetCellValue(row
, col
);
7804 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7807 GetTextExtent(value
, &maxWidth
, &y
,
7808 NULL
, NULL
, &attr
->GetFont());
7809 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7811 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7812 if (rect
.x
+maxWidth
> client_right
)
7813 maxWidth
= client_right
- rect
.x
;
7815 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7817 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7818 // may have changed earlier
7819 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7822 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7823 // looks weird going over a multicell
7824 if (m_table
->IsEmptyCell(row
,i
) &&
7825 (rect
.width
< maxWidth
) && (c_rows
== 1))
7826 rect
.width
+= GetColWidth(i
);
7830 if (rect
.GetRight() > client_right
)
7831 rect
.SetRight(client_right
-1);
7834 editor
->SetCellAttr(attr
);
7835 editor
->SetSize( rect
);
7836 editor
->Show( true, attr
);
7838 // recalc dimensions in case we need to
7839 // expand the scrolled window to account for editor
7842 editor
->BeginEdit(row
, col
, this);
7843 editor
->SetCellAttr(NULL
);
7852 void wxGrid::HideCellEditControl()
7854 if ( IsCellEditControlEnabled() )
7856 int row
= m_currentCellCoords
.GetRow();
7857 int col
= m_currentCellCoords
.GetCol();
7859 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7860 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7861 editor
->Show( false );
7865 m_gridWin
->SetFocus();
7867 // refresh whole row to the right
7868 wxRect
rect( CellToRect(row
, col
) );
7869 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7870 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7872 // ensure that the pixels under the focus ring get refreshed as well
7873 rect
.Inflate(10,10);
7875 m_gridWin
->Refresh( false, &rect
);
7880 void wxGrid::SaveEditControlValue()
7882 if ( IsCellEditControlEnabled() )
7884 int row
= m_currentCellCoords
.GetRow();
7885 int col
= m_currentCellCoords
.GetCol();
7887 wxString oldval
= GetCellValue(row
,col
);
7889 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7890 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7891 bool changed
= editor
->EndEdit(row
, col
, this);
7898 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7899 m_currentCellCoords
.GetRow(),
7900 m_currentCellCoords
.GetCol() ) < 0 ) {
7902 // Event has been vetoed, set the data back.
7903 SetCellValue(row
,col
,oldval
);
7911 // ------ Grid location functions
7912 // Note that all of these functions work with the logical coordinates of
7913 // grid cells and labels so you will need to convert from device
7914 // coordinates for mouse events etc.
7917 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7919 int row
= YToRow(y
);
7920 int col
= XToCol(x
);
7922 if ( row
== -1 || col
== -1 )
7924 coords
= wxGridNoCellCoords
;
7928 coords
.Set( row
, col
);
7933 // Internal Helper function for computing row or column from some
7934 // (unscrolled) coordinate value, using either
7935 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7936 // of m_rowBottoms/m_ColRights to speed up the search!
7938 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7939 const wxArrayInt
& BorderArray
, int nMax
,
7944 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7950 size_t i_max
= coord
/ defaultDist
,
7953 if (BorderArray
.IsEmpty())
7955 if((int) i_max
< nMax
)
7957 return clipToMinMax
? nMax
- 1 : -1;
7960 if ( i_max
>= BorderArray
.GetCount())
7961 i_max
= BorderArray
.GetCount() - 1;
7964 if ( coord
>= BorderArray
[i_max
])
7968 i_max
= coord
/ minDist
;
7970 i_max
= BorderArray
.GetCount() - 1;
7972 if ( i_max
>= BorderArray
.GetCount())
7973 i_max
= BorderArray
.GetCount() - 1;
7975 if ( coord
>= BorderArray
[i_max
])
7976 return clipToMinMax
? (int)i_max
: -1;
7977 if ( coord
< BorderArray
[0] )
7980 while ( i_max
- i_min
> 0 )
7982 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7983 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7984 if (coord
>= BorderArray
[ i_max
- 1])
7988 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7989 if (coord
< BorderArray
[median
])
7997 int wxGrid::YToRow( int y
)
7999 return CoordToRowOrCol(y
, m_defaultRowHeight
,
8000 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
8004 int wxGrid::XToCol( int x
)
8006 return CoordToRowOrCol(x
, m_defaultColWidth
,
8007 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
8011 // return the row number that that the y coord is near the edge of, or
8012 // -1 if not near an edge
8014 int wxGrid::YToEdgeOfRow( int y
)
8017 i
= internalYToRow(y
);
8019 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
8021 // We know that we are in row i, test whether we are
8022 // close enough to lower or upper border, respectively.
8023 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
8025 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
8033 // return the col number that that the x coord is near the edge of, or
8034 // -1 if not near an edge
8036 int wxGrid::XToEdgeOfCol( int x
)
8039 i
= internalXToCol(x
);
8041 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8043 // We know that we are in column i, test whether we are
8044 // close enough to right or left border, respectively.
8045 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8047 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8055 wxRect
wxGrid::CellToRect( int row
, int col
)
8057 wxRect
rect( -1, -1, -1, -1 );
8059 if ( row
>= 0 && row
< m_numRows
&&
8060 col
>= 0 && col
< m_numCols
)
8062 int i
, cell_rows
, cell_cols
;
8063 rect
.width
= rect
.height
= 0;
8064 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8065 // if negative then find multicell owner
8066 if (cell_rows
< 0) row
+= cell_rows
;
8067 if (cell_cols
< 0) col
+= cell_cols
;
8068 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8070 rect
.x
= GetColLeft(col
);
8071 rect
.y
= GetRowTop(row
);
8072 for (i
=col
; i
<col
+cell_cols
; i
++)
8073 rect
.width
+= GetColWidth(i
);
8074 for (i
=row
; i
<row
+cell_rows
; i
++)
8075 rect
.height
+= GetRowHeight(i
);
8078 // if grid lines are enabled, then the area of the cell is a bit smaller
8079 if (m_gridLinesEnabled
) {
8087 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8089 // get the cell rectangle in logical coords
8091 wxRect
r( CellToRect( row
, col
) );
8093 // convert to device coords
8095 int left
, top
, right
, bottom
;
8096 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8097 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8099 // check against the client area of the grid window
8102 m_gridWin
->GetClientSize( &cw
, &ch
);
8104 if ( wholeCellVisible
)
8106 // is the cell wholly visible ?
8108 return ( left
>= 0 && right
<= cw
&&
8109 top
>= 0 && bottom
<= ch
);
8113 // is the cell partly visible ?
8115 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8116 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8121 // make the specified cell location visible by doing a minimal amount
8124 void wxGrid::MakeCellVisible( int row
, int col
)
8128 int xpos
= -1, ypos
= -1;
8130 if ( row
>= 0 && row
< m_numRows
&&
8131 col
>= 0 && col
< m_numCols
)
8133 // get the cell rectangle in logical coords
8135 wxRect
r( CellToRect( row
, col
) );
8137 // convert to device coords
8139 int left
, top
, right
, bottom
;
8140 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8141 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8144 m_gridWin
->GetClientSize( &cw
, &ch
);
8150 else if ( bottom
> ch
)
8152 int h
= r
.GetHeight();
8154 for ( i
= row
-1; i
>= 0; i
-- )
8156 int rowHeight
= GetRowHeight(i
);
8157 if ( h
+ rowHeight
> ch
)
8164 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8165 // have rounding errors (this is important, because if we do, we
8166 // might not scroll at all and some cells won't be redrawn)
8168 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8170 ypos
+= m_scrollLineY
;
8177 else if ( right
> cw
)
8179 // position the view so that the cell is on the right
8181 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8182 xpos
= x0
+ (right
- cw
);
8184 // see comment for ypos above
8185 xpos
+= m_scrollLineX
;
8188 if ( xpos
!= -1 || ypos
!= -1 )
8191 xpos
/= m_scrollLineX
;
8193 ypos
/= m_scrollLineY
;
8194 Scroll( xpos
, ypos
);
8202 // ------ Grid cursor movement functions
8205 bool wxGrid::MoveCursorUp( bool expandSelection
)
8207 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8208 m_currentCellCoords
.GetRow() >= 0 )
8210 if ( expandSelection
)
8212 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8213 m_selectingKeyboard
= m_currentCellCoords
;
8214 if ( m_selectingKeyboard
.GetRow() > 0 )
8216 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8217 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8218 m_selectingKeyboard
.GetCol() );
8219 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8222 else if ( m_currentCellCoords
.GetRow() > 0 )
8225 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8226 m_currentCellCoords
.GetCol() );
8227 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8228 m_currentCellCoords
.GetCol() );
8239 bool wxGrid::MoveCursorDown( bool expandSelection
)
8241 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8242 m_currentCellCoords
.GetRow() < m_numRows
)
8244 if ( expandSelection
)
8246 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8247 m_selectingKeyboard
= m_currentCellCoords
;
8248 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8250 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8251 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8252 m_selectingKeyboard
.GetCol() );
8253 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8256 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8259 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8260 m_currentCellCoords
.GetCol() );
8261 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8262 m_currentCellCoords
.GetCol() );
8273 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8275 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8276 m_currentCellCoords
.GetCol() >= 0 )
8278 if ( expandSelection
)
8280 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8281 m_selectingKeyboard
= m_currentCellCoords
;
8282 if ( m_selectingKeyboard
.GetCol() > 0 )
8284 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8285 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8286 m_selectingKeyboard
.GetCol() );
8287 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8290 else if ( m_currentCellCoords
.GetCol() > 0 )
8293 MakeCellVisible( m_currentCellCoords
.GetRow(),
8294 m_currentCellCoords
.GetCol() - 1 );
8295 SetCurrentCell( m_currentCellCoords
.GetRow(),
8296 m_currentCellCoords
.GetCol() - 1 );
8307 bool wxGrid::MoveCursorRight( bool expandSelection
)
8309 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8310 m_currentCellCoords
.GetCol() < m_numCols
)
8312 if ( expandSelection
)
8314 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8315 m_selectingKeyboard
= m_currentCellCoords
;
8316 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8318 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8319 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8320 m_selectingKeyboard
.GetCol() );
8321 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8324 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8327 MakeCellVisible( m_currentCellCoords
.GetRow(),
8328 m_currentCellCoords
.GetCol() + 1 );
8329 SetCurrentCell( m_currentCellCoords
.GetRow(),
8330 m_currentCellCoords
.GetCol() + 1 );
8341 bool wxGrid::MovePageUp()
8343 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8345 int row
= m_currentCellCoords
.GetRow();
8349 m_gridWin
->GetClientSize( &cw
, &ch
);
8351 int y
= GetRowTop(row
);
8352 int newRow
= internalYToRow( y
- ch
+ 1 );
8354 if ( newRow
== row
)
8356 //row > 0 , so newrow can never be less than 0 here.
8360 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8361 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8369 bool wxGrid::MovePageDown()
8371 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8373 int row
= m_currentCellCoords
.GetRow();
8374 if ( (row
+1) < m_numRows
)
8377 m_gridWin
->GetClientSize( &cw
, &ch
);
8379 int y
= GetRowTop(row
);
8380 int newRow
= internalYToRow( y
+ ch
);
8381 if ( newRow
== row
)
8383 // row < m_numRows , so newrow can't overflow here.
8387 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8388 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8396 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8399 m_currentCellCoords
!= wxGridNoCellCoords
&&
8400 m_currentCellCoords
.GetRow() > 0 )
8402 int row
= m_currentCellCoords
.GetRow();
8403 int col
= m_currentCellCoords
.GetCol();
8405 if ( m_table
->IsEmptyCell(row
, col
) )
8407 // starting in an empty cell: find the next block of
8413 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8416 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8418 // starting at the top of a block: find the next block
8424 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8429 // starting within a block: find the top of the block
8434 if ( m_table
->IsEmptyCell(row
, col
) )
8442 MakeCellVisible( row
, col
);
8443 if ( expandSelection
)
8445 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8446 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8451 SetCurrentCell( row
, col
);
8459 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8462 m_currentCellCoords
!= wxGridNoCellCoords
&&
8463 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8465 int row
= m_currentCellCoords
.GetRow();
8466 int col
= m_currentCellCoords
.GetCol();
8468 if ( m_table
->IsEmptyCell(row
, col
) )
8470 // starting in an empty cell: find the next block of
8473 while ( row
< m_numRows
-1 )
8476 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8479 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8481 // starting at the bottom of a block: find the next block
8484 while ( row
< m_numRows
-1 )
8487 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8492 // starting within a block: find the bottom of the block
8494 while ( row
< m_numRows
-1 )
8497 if ( m_table
->IsEmptyCell(row
, col
) )
8505 MakeCellVisible( row
, col
);
8506 if ( expandSelection
)
8508 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8509 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8514 SetCurrentCell( row
, col
);
8523 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8526 m_currentCellCoords
!= wxGridNoCellCoords
&&
8527 m_currentCellCoords
.GetCol() > 0 )
8529 int row
= m_currentCellCoords
.GetRow();
8530 int col
= m_currentCellCoords
.GetCol();
8532 if ( m_table
->IsEmptyCell(row
, col
) )
8534 // starting in an empty cell: find the next block of
8540 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8543 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8545 // starting at the left of a block: find the next block
8551 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8556 // starting within a block: find the left of the block
8561 if ( m_table
->IsEmptyCell(row
, col
) )
8569 MakeCellVisible( row
, col
);
8570 if ( expandSelection
)
8572 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8573 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8578 SetCurrentCell( row
, col
);
8587 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8590 m_currentCellCoords
!= wxGridNoCellCoords
&&
8591 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8593 int row
= m_currentCellCoords
.GetRow();
8594 int col
= m_currentCellCoords
.GetCol();
8596 if ( m_table
->IsEmptyCell(row
, col
) )
8598 // starting in an empty cell: find the next block of
8601 while ( col
< m_numCols
-1 )
8604 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8607 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8609 // starting at the right of a block: find the next block
8612 while ( col
< m_numCols
-1 )
8615 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8620 // starting within a block: find the right of the block
8622 while ( col
< m_numCols
-1 )
8625 if ( m_table
->IsEmptyCell(row
, col
) )
8633 MakeCellVisible( row
, col
);
8634 if ( expandSelection
)
8636 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8637 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8642 SetCurrentCell( row
, col
);
8654 // ------ Label values and formatting
8657 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8659 *horiz
= m_rowLabelHorizAlign
;
8660 *vert
= m_rowLabelVertAlign
;
8663 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8665 *horiz
= m_colLabelHorizAlign
;
8666 *vert
= m_colLabelVertAlign
;
8669 int wxGrid::GetColLabelTextOrientation()
8671 return m_colLabelTextOrientation
;
8674 wxString
wxGrid::GetRowLabelValue( int row
)
8678 return m_table
->GetRowLabelValue( row
);
8688 wxString
wxGrid::GetColLabelValue( int col
)
8692 return m_table
->GetColLabelValue( col
);
8703 void wxGrid::SetRowLabelSize( int width
)
8705 width
= wxMax( width
, 0 );
8706 if ( width
!= m_rowLabelWidth
)
8710 m_rowLabelWin
->Show( false );
8711 m_cornerLabelWin
->Show( false );
8713 else if ( m_rowLabelWidth
== 0 )
8715 m_rowLabelWin
->Show( true );
8716 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8719 m_rowLabelWidth
= width
;
8721 wxScrolledWindow::Refresh( true );
8726 void wxGrid::SetColLabelSize( int height
)
8728 height
= wxMax( height
, 0 );
8729 if ( height
!= m_colLabelHeight
)
8733 m_colLabelWin
->Show( false );
8734 m_cornerLabelWin
->Show( false );
8736 else if ( m_colLabelHeight
== 0 )
8738 m_colLabelWin
->Show( true );
8739 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8742 m_colLabelHeight
= height
;
8744 wxScrolledWindow::Refresh( true );
8749 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8751 if ( m_labelBackgroundColour
!= colour
)
8753 m_labelBackgroundColour
= colour
;
8754 m_rowLabelWin
->SetBackgroundColour( colour
);
8755 m_colLabelWin
->SetBackgroundColour( colour
);
8756 m_cornerLabelWin
->SetBackgroundColour( colour
);
8758 if ( !GetBatchCount() )
8760 m_rowLabelWin
->Refresh();
8761 m_colLabelWin
->Refresh();
8762 m_cornerLabelWin
->Refresh();
8767 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8769 if ( m_labelTextColour
!= colour
)
8771 m_labelTextColour
= colour
;
8772 if ( !GetBatchCount() )
8774 m_rowLabelWin
->Refresh();
8775 m_colLabelWin
->Refresh();
8780 void wxGrid::SetLabelFont( const wxFont
& font
)
8783 if ( !GetBatchCount() )
8785 m_rowLabelWin
->Refresh();
8786 m_colLabelWin
->Refresh();
8790 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8792 // allow old (incorrect) defs to be used
8795 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8796 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8797 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8802 case wxTOP
: vert
= wxALIGN_TOP
; break;
8803 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8804 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8807 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8809 m_rowLabelHorizAlign
= horiz
;
8812 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8814 m_rowLabelVertAlign
= vert
;
8817 if ( !GetBatchCount() )
8819 m_rowLabelWin
->Refresh();
8823 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8825 // allow old (incorrect) defs to be used
8828 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8829 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8830 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8835 case wxTOP
: vert
= wxALIGN_TOP
; break;
8836 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8837 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8840 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8842 m_colLabelHorizAlign
= horiz
;
8845 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8847 m_colLabelVertAlign
= vert
;
8850 if ( !GetBatchCount() )
8852 m_colLabelWin
->Refresh();
8856 // Note: under MSW, the default column label font must be changed because it
8857 // does not support vertical printing
8859 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8860 // pGrid->SetLabelFont(font);
8861 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8863 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8865 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8867 m_colLabelTextOrientation
= textOrientation
;
8870 if ( !GetBatchCount() )
8872 m_colLabelWin
->Refresh();
8876 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8880 m_table
->SetRowLabelValue( row
, s
);
8881 if ( !GetBatchCount() )
8883 wxRect rect
= CellToRect( row
, 0);
8884 if ( rect
.height
> 0 )
8886 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8888 rect
.width
= m_rowLabelWidth
;
8889 m_rowLabelWin
->Refresh( true, &rect
);
8895 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8899 m_table
->SetColLabelValue( col
, s
);
8900 if ( !GetBatchCount() )
8902 wxRect rect
= CellToRect( 0, col
);
8903 if ( rect
.width
> 0 )
8905 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8907 rect
.height
= m_colLabelHeight
;
8908 m_colLabelWin
->Refresh( true, &rect
);
8914 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8916 if ( m_gridLineColour
!= colour
)
8918 m_gridLineColour
= colour
;
8920 wxClientDC
dc( m_gridWin
);
8922 DrawAllGridLines( dc
, wxRegion() );
8927 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8929 if ( m_cellHighlightColour
!= colour
)
8931 m_cellHighlightColour
= colour
;
8933 wxClientDC
dc( m_gridWin
);
8935 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8936 DrawCellHighlight(dc
, attr
);
8941 void wxGrid::SetCellHighlightPenWidth(int width
)
8943 if (m_cellHighlightPenWidth
!= width
) {
8944 m_cellHighlightPenWidth
= width
;
8946 // Just redrawing the cell highlight is not enough since that won't
8947 // make any visible change if the the thickness is getting smaller.
8948 int row
= m_currentCellCoords
.GetRow();
8949 int col
= m_currentCellCoords
.GetCol();
8950 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8952 wxRect rect
= CellToRect(row
, col
);
8953 m_gridWin
->Refresh(true, &rect
);
8957 void wxGrid::SetCellHighlightROPenWidth(int width
)
8959 if (m_cellHighlightROPenWidth
!= width
) {
8960 m_cellHighlightROPenWidth
= width
;
8962 // Just redrawing the cell highlight is not enough since that won't
8963 // make any visible change if the the thickness is getting smaller.
8964 int row
= m_currentCellCoords
.GetRow();
8965 int col
= m_currentCellCoords
.GetCol();
8966 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8968 wxRect rect
= CellToRect(row
, col
);
8969 m_gridWin
->Refresh(true, &rect
);
8973 void wxGrid::EnableGridLines( bool enable
)
8975 if ( enable
!= m_gridLinesEnabled
)
8977 m_gridLinesEnabled
= enable
;
8979 if ( !GetBatchCount() )
8983 wxClientDC
dc( m_gridWin
);
8985 DrawAllGridLines( dc
, wxRegion() );
8989 m_gridWin
->Refresh();
8996 int wxGrid::GetDefaultRowSize()
8998 return m_defaultRowHeight
;
9001 int wxGrid::GetRowSize( int row
)
9003 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
9005 return GetRowHeight(row
);
9008 int wxGrid::GetDefaultColSize()
9010 return m_defaultColWidth
;
9013 int wxGrid::GetColSize( int col
)
9015 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
9017 return GetColWidth(col
);
9020 // ============================================================================
9021 // access to the grid attributes: each of them has a default value in the grid
9022 // itself and may be overidden on a per-cell basis
9023 // ============================================================================
9025 // ----------------------------------------------------------------------------
9026 // setting default attributes
9027 // ----------------------------------------------------------------------------
9029 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
9031 m_defaultCellAttr
->SetBackgroundColour(col
);
9033 m_gridWin
->SetBackgroundColour(col
);
9037 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9039 m_defaultCellAttr
->SetTextColour(col
);
9042 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9044 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9047 void wxGrid::SetDefaultCellOverflow( bool allow
)
9049 m_defaultCellAttr
->SetOverflow(allow
);
9052 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9054 m_defaultCellAttr
->SetFont(font
);
9058 // For editors and renderers the type registry takes precedence over the
9059 // default attr, so we need to register the new editor/renderer for the string
9060 // data type in order to make setting a default editor/renderer appear to
9063 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9065 RegisterDataType(wxGRID_VALUE_STRING
,
9067 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9070 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9072 RegisterDataType(wxGRID_VALUE_STRING
,
9073 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9077 // ----------------------------------------------------------------------------
9078 // access to the default attrbiutes
9079 // ----------------------------------------------------------------------------
9081 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9083 return m_defaultCellAttr
->GetBackgroundColour();
9086 wxColour
wxGrid::GetDefaultCellTextColour()
9088 return m_defaultCellAttr
->GetTextColour();
9091 wxFont
wxGrid::GetDefaultCellFont()
9093 return m_defaultCellAttr
->GetFont();
9096 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9098 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9101 bool wxGrid::GetDefaultCellOverflow()
9103 return m_defaultCellAttr
->GetOverflow();
9106 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9108 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9111 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9113 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9116 // ----------------------------------------------------------------------------
9117 // access to cell attributes
9118 // ----------------------------------------------------------------------------
9120 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9122 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9123 wxColour colour
= attr
->GetBackgroundColour();
9128 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9130 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9131 wxColour colour
= attr
->GetTextColour();
9136 wxFont
wxGrid::GetCellFont( int row
, int col
)
9138 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9139 wxFont font
= attr
->GetFont();
9144 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9146 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9147 attr
->GetAlignment(horiz
, vert
);
9151 bool wxGrid::GetCellOverflow( int row
, int col
)
9153 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9154 bool allow
= attr
->GetOverflow();
9159 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9161 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9162 attr
->GetSize( num_rows
, num_cols
);
9166 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9168 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9169 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9175 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9177 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9178 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9184 bool wxGrid::IsReadOnly(int row
, int col
) const
9186 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9187 bool isReadOnly
= attr
->IsReadOnly();
9192 // ----------------------------------------------------------------------------
9193 // attribute support: cache, automatic provider creation, ...
9194 // ----------------------------------------------------------------------------
9196 bool wxGrid::CanHaveAttributes()
9203 return m_table
->CanHaveAttributes();
9206 void wxGrid::ClearAttrCache()
9208 if ( m_attrCache
.row
!= -1 )
9210 wxSafeDecRef(m_attrCache
.attr
);
9211 m_attrCache
.attr
= NULL
;
9212 m_attrCache
.row
= -1;
9216 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9220 wxGrid
*self
= (wxGrid
*)this; // const_cast
9222 self
->ClearAttrCache();
9223 self
->m_attrCache
.row
= row
;
9224 self
->m_attrCache
.col
= col
;
9225 self
->m_attrCache
.attr
= attr
;
9230 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9232 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9234 *attr
= m_attrCache
.attr
;
9235 wxSafeIncRef(m_attrCache
.attr
);
9237 #ifdef DEBUG_ATTR_CACHE
9238 gs_nAttrCacheHits
++;
9245 #ifdef DEBUG_ATTR_CACHE
9246 gs_nAttrCacheMisses
++;
9252 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9254 wxGridCellAttr
*attr
= NULL
;
9255 // Additional test to avoid looking at the cache e.g. for
9256 // wxNoCellCoords, as this will confuse memory management.
9259 if ( !LookupAttr(row
, col
, &attr
) )
9261 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9262 : (wxGridCellAttr
*)NULL
;
9263 CacheAttr(row
, col
, attr
);
9268 attr
->SetDefAttr(m_defaultCellAttr
);
9272 attr
= m_defaultCellAttr
;
9279 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9281 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9282 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9284 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9285 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9287 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9290 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9292 // artificially inc the ref count to match DecRef() in caller
9294 m_table
->SetAttr(attr
, row
, col
);
9300 // ----------------------------------------------------------------------------
9301 // setting column attributes (wrappers around SetColAttr)
9302 // ----------------------------------------------------------------------------
9304 void wxGrid::SetColFormatBool(int col
)
9306 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9309 void wxGrid::SetColFormatNumber(int col
)
9311 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9314 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9316 wxString typeName
= wxGRID_VALUE_FLOAT
;
9317 if ( (width
!= -1) || (precision
!= -1) )
9319 typeName
<< _T(':') << width
<< _T(',') << precision
;
9322 SetColFormatCustom(col
, typeName
);
9325 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9327 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9329 attr
= new wxGridCellAttr
;
9330 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9331 attr
->SetRenderer(renderer
);
9333 SetColAttr(col
, attr
);
9337 // ----------------------------------------------------------------------------
9338 // setting cell attributes: this is forwarded to the table
9339 // ----------------------------------------------------------------------------
9341 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9343 if ( CanHaveAttributes() )
9345 m_table
->SetAttr(attr
, row
, col
);
9354 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9356 if ( CanHaveAttributes() )
9358 m_table
->SetRowAttr(attr
, row
);
9367 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9369 if ( CanHaveAttributes() )
9371 m_table
->SetColAttr(attr
, col
);
9380 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9382 if ( CanHaveAttributes() )
9384 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9385 attr
->SetBackgroundColour(colour
);
9390 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9392 if ( CanHaveAttributes() )
9394 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9395 attr
->SetTextColour(colour
);
9400 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9402 if ( CanHaveAttributes() )
9404 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9405 attr
->SetFont(font
);
9410 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9412 if ( CanHaveAttributes() )
9414 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9415 attr
->SetAlignment(horiz
, vert
);
9420 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9422 if ( CanHaveAttributes() )
9424 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9425 attr
->SetOverflow(allow
);
9430 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9432 if ( CanHaveAttributes() )
9434 int cell_rows
, cell_cols
;
9436 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9437 attr
->GetSize(&cell_rows
, &cell_cols
);
9438 attr
->SetSize(num_rows
, num_cols
);
9441 // Cannot set the size of a cell to 0 or negative values
9442 // While it is perfectly legal to do that, this function cannot
9443 // handle all the possibilies, do it by hand by getting the CellAttr.
9444 // You can only set the size of a cell to 1,1 or greater with this fn
9445 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9446 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9447 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9448 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9450 // if this was already a multicell then "turn off" the other cells first
9451 if ((cell_rows
> 1) || (cell_rows
> 1))
9454 for (j
=row
; j
<row
+cell_rows
; j
++)
9456 for (i
=col
; i
<col
+cell_cols
; i
++)
9458 if ((i
!= col
) || (j
!= row
))
9460 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9461 attr_stub
->SetSize( 1, 1 );
9462 attr_stub
->DecRef();
9468 // mark the cells that will be covered by this cell to
9469 // negative or zero values to point back at this cell
9470 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9473 for (j
=row
; j
<row
+num_rows
; j
++)
9475 for (i
=col
; i
<col
+num_cols
; i
++)
9477 if ((i
!= col
) || (j
!= row
))
9479 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9480 attr_stub
->SetSize( row
-j
, col
-i
);
9481 attr_stub
->DecRef();
9489 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9491 if ( CanHaveAttributes() )
9493 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9494 attr
->SetRenderer(renderer
);
9499 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9501 if ( CanHaveAttributes() )
9503 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9504 attr
->SetEditor(editor
);
9509 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9511 if ( CanHaveAttributes() )
9513 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9514 attr
->SetReadOnly(isReadOnly
);
9519 // ----------------------------------------------------------------------------
9520 // Data type registration
9521 // ----------------------------------------------------------------------------
9523 void wxGrid::RegisterDataType(const wxString
& typeName
,
9524 wxGridCellRenderer
* renderer
,
9525 wxGridCellEditor
* editor
)
9527 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9531 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9533 wxString typeName
= m_table
->GetTypeName(row
, col
);
9534 return GetDefaultEditorForType(typeName
);
9537 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9539 wxString typeName
= m_table
->GetTypeName(row
, col
);
9540 return GetDefaultRendererForType(typeName
);
9544 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9546 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9547 if ( index
== wxNOT_FOUND
)
9549 wxFAIL_MSG(wxT("Unknown data type name"));
9554 return m_typeRegistry
->GetEditor(index
);
9558 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9560 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9561 if ( index
== wxNOT_FOUND
)
9563 wxFAIL_MSG(wxT("Unknown data type name"));
9568 return m_typeRegistry
->GetRenderer(index
);
9572 // ----------------------------------------------------------------------------
9574 // ----------------------------------------------------------------------------
9576 void wxGrid::EnableDragRowSize( bool enable
)
9578 m_canDragRowSize
= enable
;
9582 void wxGrid::EnableDragColSize( bool enable
)
9584 m_canDragColSize
= enable
;
9587 void wxGrid::EnableDragGridSize( bool enable
)
9589 m_canDragGridSize
= enable
;
9592 void wxGrid::EnableDragCell( bool enable
)
9594 m_canDragCell
= enable
;
9597 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9599 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9601 if ( resizeExistingRows
)
9603 // since we are resizing all rows to the default row size,
9604 // we can simply clear the row heights and row bottoms
9605 // arrays (which also allows us to take advantage of
9606 // some speed optimisations)
9607 m_rowHeights
.Empty();
9608 m_rowBottoms
.Empty();
9609 if ( !GetBatchCount() )
9614 void wxGrid::SetRowSize( int row
, int height
)
9616 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9618 // See comment in SetColSize
9619 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9621 if ( m_rowHeights
.IsEmpty() )
9623 // need to really create the array
9627 int h
= wxMax( 0, height
);
9628 int diff
= h
- m_rowHeights
[row
];
9630 m_rowHeights
[row
] = h
;
9632 for ( i
= row
; i
< m_numRows
; i
++ )
9634 m_rowBottoms
[i
] += diff
;
9636 if ( !GetBatchCount() )
9640 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9642 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9644 if ( resizeExistingCols
)
9646 // since we are resizing all columns to the default column size,
9647 // we can simply clear the col widths and col rights
9648 // arrays (which also allows us to take advantage of
9649 // some speed optimisations)
9650 m_colWidths
.Empty();
9651 m_colRights
.Empty();
9652 if ( !GetBatchCount() )
9657 void wxGrid::SetColSize( int col
, int width
)
9659 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9661 // should we check that it's bigger than GetColMinimalWidth(col) here?
9663 // No, because it is reasonable to assume the library user know's
9664 // what he is doing. However whe should test against the weaker
9665 // constariant of minimalAcceptableWidth, as this breaks rendering
9667 // This test then fixes sf.net bug #645734
9669 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9671 if ( m_colWidths
.IsEmpty() )
9673 // need to really create the array
9677 // if < 0 calc new width from label
9681 wxArrayString lines
;
9682 wxClientDC
dc(m_colLabelWin
);
9683 dc
.SetFont(GetLabelFont());
9684 StringToLines(GetColLabelValue(col
), lines
);
9685 GetTextBoxSize(dc
, lines
, &w
, &h
);
9688 int w
= wxMax( 0, width
);
9689 int diff
= w
- m_colWidths
[col
];
9690 m_colWidths
[col
] = w
;
9693 for ( i
= col
; i
< m_numCols
; i
++ )
9695 m_colRights
[i
] += diff
;
9697 if ( !GetBatchCount() )
9702 void wxGrid::SetColMinimalWidth( int col
, int width
)
9704 if (width
> GetColMinimalAcceptableWidth()) {
9705 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9706 m_colMinWidths
[key
] = width
;
9710 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9712 if (width
> GetRowMinimalAcceptableHeight()) {
9713 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9714 m_rowMinHeights
[key
] = width
;
9718 int wxGrid::GetColMinimalWidth(int col
) const
9720 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9721 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9722 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9725 int wxGrid::GetRowMinimalHeight(int row
) const
9727 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9728 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9729 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9732 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9734 // We do allow a width of 0 since this gives us
9735 // an easy way to temporarily hidding columns.
9738 m_minAcceptableColWidth
= width
;
9741 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9743 // We do allow a height of 0 since this gives us
9744 // an easy way to temporarily hidding rows.
9747 m_minAcceptableRowHeight
= height
;
9750 int wxGrid::GetColMinimalAcceptableWidth() const
9752 return m_minAcceptableColWidth
;
9755 int wxGrid::GetRowMinimalAcceptableHeight() const
9757 return m_minAcceptableRowHeight
;
9760 // ----------------------------------------------------------------------------
9762 // ----------------------------------------------------------------------------
9764 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9766 wxClientDC
dc(m_gridWin
);
9768 //Cancel editting of cell
9769 HideCellEditControl();
9770 SaveEditControlValue();
9772 // init both of them to avoid compiler warnings, even if weo nly need one
9780 wxCoord extent
, extentMax
= 0;
9781 int max
= column
? m_numRows
: m_numCols
;
9782 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9789 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9790 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9793 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9794 extent
= column
? size
.x
: size
.y
;
9795 if ( extent
> extentMax
)
9806 // now also compare with the column label extent
9808 dc
.SetFont( GetLabelFont() );
9812 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9813 if( GetColLabelTextOrientation() == wxVERTICAL
)
9817 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9819 extent
= column
? w
: h
;
9820 if ( extent
> extentMax
)
9827 // empty column - give default extent (notice that if extentMax is less
9828 // than default extent but != 0, it's ok)
9829 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9835 // leave some space around text
9846 SetColSize(col
, extentMax
);
9847 if ( !GetBatchCount() )
9850 m_gridWin
->GetClientSize( &cw
, &ch
);
9851 wxRect
rect ( CellToRect( 0, col
) );
9853 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9854 rect
.width
= cw
- rect
.x
;
9855 rect
.height
= m_colLabelHeight
;
9856 m_colLabelWin
->Refresh( true, &rect
);
9861 SetRowSize(row
, extentMax
);
9862 if ( !GetBatchCount() )
9865 m_gridWin
->GetClientSize( &cw
, &ch
);
9866 wxRect
rect ( CellToRect( row
, 0 ) );
9868 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9869 rect
.width
= m_rowLabelWidth
;
9870 rect
.height
= ch
- rect
.y
;
9871 m_rowLabelWin
->Refresh( true, &rect
);
9877 SetColMinimalWidth(col
, extentMax
);
9879 SetRowMinimalHeight(row
, extentMax
);
9883 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9885 int width
= m_rowLabelWidth
;
9890 for ( int col
= 0; col
< m_numCols
; col
++ )
9894 AutoSizeColumn(col
, setAsMin
);
9897 width
+= GetColWidth(col
);
9906 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9908 int height
= m_colLabelHeight
;
9913 for ( int row
= 0; row
< m_numRows
; row
++ )
9917 AutoSizeRow(row
, setAsMin
);
9920 height
+= GetRowHeight(row
);
9929 void wxGrid::AutoSize()
9933 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9935 // round up the size to a multiple of scroll step - this ensures that we
9936 // won't get the scrollbars if we're sized exactly to this width
9937 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9939 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9940 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9942 // distribute the extra space between the columns/rows to avoid having
9943 // extra white space
9945 // Remove the extra m_extraWidth + 1 added above
9946 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9947 if ( diff
&& m_numCols
)
9949 // try to resize the columns uniformly
9950 wxCoord diffPerCol
= diff
/ m_numCols
;
9953 for ( int col
= 0; col
< m_numCols
; col
++ )
9955 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9959 // add remaining amount to the last columns
9960 diff
-= diffPerCol
* m_numCols
;
9963 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9965 SetColSize(col
, GetColWidth(col
) + 1);
9971 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9972 if ( diff
&& m_numRows
)
9974 // try to resize the columns uniformly
9975 wxCoord diffPerRow
= diff
/ m_numRows
;
9978 for ( int row
= 0; row
< m_numRows
; row
++ )
9980 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9984 // add remaining amount to the last rows
9985 diff
-= diffPerRow
* m_numRows
;
9988 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9990 SetRowSize(row
, GetRowHeight(row
) + 1);
9997 SetClientSize(sizeFit
);
10000 void wxGrid::AutoSizeRowLabelSize( int row
)
10002 wxArrayString lines
;
10005 // Hide the edit control, so it
10006 // won't interfer with drag-shrinking.
10007 if( IsCellEditControlShown() )
10009 HideCellEditControl();
10010 SaveEditControlValue();
10013 // autosize row height depending on label text
10014 StringToLines( GetRowLabelValue( row
), lines
);
10015 wxClientDC
dc( m_rowLabelWin
);
10016 GetTextBoxSize( dc
, lines
, &w
, &h
);
10017 if( h
< m_defaultRowHeight
)
10018 h
= m_defaultRowHeight
;
10019 SetRowSize(row
, h
);
10023 void wxGrid::AutoSizeColLabelSize( int col
)
10025 wxArrayString lines
;
10028 // Hide the edit control, so it
10029 // won't interfer with drag-shrinking.
10030 if( IsCellEditControlShown() )
10032 HideCellEditControl();
10033 SaveEditControlValue();
10036 // autosize column width depending on label text
10037 StringToLines( GetColLabelValue( col
), lines
);
10038 wxClientDC
dc( m_colLabelWin
);
10039 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
10040 GetTextBoxSize( dc
, lines
, &w
, &h
);
10042 GetTextBoxSize( dc
, lines
, &h
, &w
);
10043 if( w
< m_defaultColWidth
)
10044 w
= m_defaultColWidth
;
10045 SetColSize(col
, w
);
10049 wxSize
wxGrid::DoGetBestSize() const
10051 // don't set sizes, only calculate them
10052 wxGrid
*self
= (wxGrid
*)this; // const_cast
10055 width
= self
->SetOrCalcColumnSizes(true);
10056 height
= self
->SetOrCalcRowSizes(true);
10058 if (!width
) width
=100;
10059 if (!height
) height
=80;
10061 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10062 // of the scrollbars, is there any magic incantaion for that?
10064 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10066 width
+= 1 + xpu
- (width
% xpu
);
10068 height
+= 1 + ypu
- (height
% ypu
);
10070 // limit to 1/4 of the screen size
10071 int maxwidth
, maxheight
;
10072 wxDisplaySize( & maxwidth
, & maxheight
);
10075 if ( width
> maxwidth
) width
= maxwidth
;
10076 if ( height
> maxheight
) height
= maxheight
;
10079 wxSize
best(width
, height
);
10080 // NOTE: This size should be cached, but first we need to add calls to
10081 // InvalidateBestSize everywhere that could change the results of this
10083 // CacheBestSize(size);
10093 wxPen
& wxGrid::GetDividerPen() const
10098 // ----------------------------------------------------------------------------
10099 // cell value accessor functions
10100 // ----------------------------------------------------------------------------
10102 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10106 m_table
->SetValue( row
, col
, s
);
10107 if ( !GetBatchCount() )
10110 wxRect
rect( CellToRect( row
, col
) );
10112 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10113 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10114 m_gridWin
->Refresh( false, &rect
);
10117 if ( m_currentCellCoords
.GetRow() == row
&&
10118 m_currentCellCoords
.GetCol() == col
&&
10119 IsCellEditControlShown())
10120 // Note: If we are using IsCellEditControlEnabled,
10121 // this interacts badly with calling SetCellValue from
10122 // an EVT_GRID_CELL_CHANGE handler.
10124 HideCellEditControl();
10125 ShowCellEditControl(); // will reread data from table
10132 // ------ Block, row and col selection
10135 void wxGrid::SelectRow( int row
, bool addToSelected
)
10137 if ( IsSelection() && !addToSelected
)
10141 m_selection
->SelectRow( row
, false, addToSelected
);
10145 void wxGrid::SelectCol( int col
, bool addToSelected
)
10147 if ( IsSelection() && !addToSelected
)
10151 m_selection
->SelectCol( col
, false, addToSelected
);
10155 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10156 bool addToSelected
)
10158 if ( IsSelection() && !addToSelected
)
10162 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10163 false, addToSelected
);
10167 void wxGrid::SelectAll()
10169 if ( m_numRows
> 0 && m_numCols
> 0 )
10172 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10177 // ------ Cell, row and col deselection
10180 void wxGrid::DeselectRow( int row
)
10182 if ( !m_selection
)
10185 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10187 if ( m_selection
->IsInSelection(row
, 0 ) )
10188 m_selection
->ToggleCellSelection( row
, 0);
10192 int nCols
= GetNumberCols();
10193 for ( int i
= 0; i
< nCols
; i
++ )
10195 if ( m_selection
->IsInSelection(row
, i
) )
10196 m_selection
->ToggleCellSelection( row
, i
);
10201 void wxGrid::DeselectCol( int col
)
10203 if ( !m_selection
)
10206 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10208 if ( m_selection
->IsInSelection(0, col
) )
10209 m_selection
->ToggleCellSelection( 0, col
);
10213 int nRows
= GetNumberRows();
10214 for ( int i
= 0; i
< nRows
; i
++ )
10216 if ( m_selection
->IsInSelection(i
, col
) )
10217 m_selection
->ToggleCellSelection(i
, col
);
10222 void wxGrid::DeselectCell( int row
, int col
)
10224 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10225 m_selection
->ToggleCellSelection(row
, col
);
10228 bool wxGrid::IsSelection()
10230 return ( m_selection
&& (m_selection
->IsSelection() ||
10231 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10232 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10235 bool wxGrid::IsInSelection( int row
, int col
) const
10237 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10238 ( row
>= m_selectingTopLeft
.GetRow() &&
10239 col
>= m_selectingTopLeft
.GetCol() &&
10240 row
<= m_selectingBottomRight
.GetRow() &&
10241 col
<= m_selectingBottomRight
.GetCol() )) );
10244 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10246 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10247 return m_selection
->m_cellSelection
;
10249 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10251 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10252 return m_selection
->m_blockSelectionTopLeft
;
10254 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10256 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10257 return m_selection
->m_blockSelectionBottomRight
;
10259 wxArrayInt
wxGrid::GetSelectedRows() const
10261 if (!m_selection
) { wxArrayInt a
; return a
; }
10262 return m_selection
->m_rowSelection
;
10264 wxArrayInt
wxGrid::GetSelectedCols() const
10266 if (!m_selection
) { wxArrayInt a
; return a
; }
10267 return m_selection
->m_colSelection
;
10271 void wxGrid::ClearSelection()
10273 m_selectingTopLeft
= wxGridNoCellCoords
;
10274 m_selectingBottomRight
= wxGridNoCellCoords
;
10276 m_selection
->ClearSelection();
10280 // This function returns the rectangle that encloses the given block
10281 // in device coords clipped to the client size of the grid window.
10283 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10284 const wxGridCellCoords
&bottomRight
)
10286 wxRect
rect( wxGridNoCellRect
);
10289 cellRect
= CellToRect( topLeft
);
10290 if ( cellRect
!= wxGridNoCellRect
)
10296 rect
= wxRect(0,0,0,0);
10299 cellRect
= CellToRect( bottomRight
);
10300 if ( cellRect
!= wxGridNoCellRect
)
10306 return wxGridNoCellRect
;
10310 int left
= rect
.GetLeft();
10311 int top
= rect
.GetTop();
10312 int right
= rect
.GetRight();
10313 int bottom
= rect
.GetBottom();
10315 int leftCol
= topLeft
.GetCol();
10316 int topRow
= topLeft
.GetRow();
10317 int rightCol
= bottomRight
.GetCol();
10318 int bottomRow
= bottomRight
.GetRow();
10336 topRow
= bottomRow
;
10341 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10343 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10345 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10347 cellRect
= CellToRect( j
, i
);
10349 if (cellRect
.x
< left
)
10351 if (cellRect
.y
< top
)
10353 if (cellRect
.x
+ cellRect
.width
> right
)
10354 right
= cellRect
.x
+ cellRect
.width
;
10355 if (cellRect
.y
+ cellRect
.height
> bottom
)
10356 bottom
= cellRect
.y
+ cellRect
.height
;
10358 else i
= rightCol
; // jump over inner cells.
10362 // convert to scrolled coords
10364 CalcScrolledPosition( left
, top
, &left
, &top
);
10365 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10368 m_gridWin
->GetClientSize( &cw
, &ch
);
10370 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10371 return wxRect(0,0,0,0);
10373 rect
.SetLeft( wxMax(0, left
) );
10374 rect
.SetTop( wxMax(0, top
) );
10375 rect
.SetRight( wxMin(cw
, right
) );
10376 rect
.SetBottom( wxMin(ch
, bottom
) );
10382 // ------ Grid event classes
10385 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10387 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10388 int row
, int col
, int x
, int y
, bool sel
,
10389 bool control
, bool shift
, bool alt
, bool meta
)
10390 : wxNotifyEvent( type
, id
)
10397 m_control
= control
;
10402 SetEventObject(obj
);
10406 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10408 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10409 int rowOrCol
, int x
, int y
,
10410 bool control
, bool shift
, bool alt
, bool meta
)
10411 : wxNotifyEvent( type
, id
)
10413 m_rowOrCol
= rowOrCol
;
10416 m_control
= control
;
10421 SetEventObject(obj
);
10425 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10427 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10428 const wxGridCellCoords
& topLeft
,
10429 const wxGridCellCoords
& bottomRight
,
10430 bool sel
, bool control
,
10431 bool shift
, bool alt
, bool meta
)
10432 : wxNotifyEvent( type
, id
)
10434 m_topLeft
= topLeft
;
10435 m_bottomRight
= bottomRight
;
10437 m_control
= control
;
10442 SetEventObject(obj
);
10446 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10448 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10449 wxObject
* obj
, int row
,
10450 int col
, wxControl
* ctrl
)
10451 : wxCommandEvent(type
, id
)
10453 SetEventObject(obj
);
10459 #endif // wxUSE_GRID