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()
244 : m_grid(0), m_editor(0)
246 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
247 : m_grid(grid
), m_editor(editor
)
250 void OnKeyDown(wxKeyEvent
& event
);
251 void OnChar(wxKeyEvent
& event
);
255 wxGridCellEditor
* m_editor
;
256 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
257 DECLARE_EVENT_TABLE()
258 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
262 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
263 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
264 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
265 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
270 // ----------------------------------------------------------------------------
271 // the internal data representation used by wxGridCellAttrProvider
272 // ----------------------------------------------------------------------------
274 // this class stores attributes set for cells
275 class WXDLLIMPEXP_ADV wxGridCellAttrData
278 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
279 wxGridCellAttr
*GetAttr(int row
, int col
) const;
280 void UpdateAttrRows( size_t pos
, int numRows
);
281 void UpdateAttrCols( size_t pos
, int numCols
);
284 // searches for the attr for given cell, returns wxNOT_FOUND if not found
285 int FindIndex(int row
, int col
) const;
287 wxGridCellWithAttrArray m_attrs
;
290 // this class stores attributes set for rows or columns
291 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
294 // empty ctor to suppress warnings
295 wxGridRowOrColAttrData() { }
296 ~wxGridRowOrColAttrData();
298 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
299 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
300 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
303 wxArrayInt m_rowsOrCols
;
304 wxArrayAttrs m_attrs
;
307 // NB: this is just a wrapper around 3 objects: one which stores cell
308 // attributes, and 2 others for row/col ones
309 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
312 wxGridCellAttrData m_cellAttrs
;
313 wxGridRowOrColAttrData m_rowAttrs
,
318 // ----------------------------------------------------------------------------
319 // data structures used for the data type registry
320 // ----------------------------------------------------------------------------
322 struct wxGridDataTypeInfo
324 wxGridDataTypeInfo(const wxString
& typeName
,
325 wxGridCellRenderer
* renderer
,
326 wxGridCellEditor
* editor
)
327 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
330 ~wxGridDataTypeInfo()
332 wxSafeDecRef(m_renderer
);
333 wxSafeDecRef(m_editor
);
337 wxGridCellRenderer
* m_renderer
;
338 wxGridCellEditor
* m_editor
;
340 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
344 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
345 class WXDLLIMPEXP_ADV
);
348 class WXDLLIMPEXP_ADV wxGridTypeRegistry
351 wxGridTypeRegistry() {}
352 ~wxGridTypeRegistry();
354 void RegisterDataType(const wxString
& typeName
,
355 wxGridCellRenderer
* renderer
,
356 wxGridCellEditor
* editor
);
358 // find one of already registered data types
359 int FindRegisteredDataType(const wxString
& typeName
);
361 // try to FindRegisteredDataType(), if this fails and typeName is one of
362 // standard typenames, register it and return its index
363 int FindDataType(const wxString
& typeName
);
365 // try to FindDataType(), if it fails see if it is not one of already
366 // registered data types with some params in which case clone the
367 // registered data type and set params for it
368 int FindOrCloneDataType(const wxString
& typeName
);
370 wxGridCellRenderer
* GetRenderer(int index
);
371 wxGridCellEditor
* GetEditor(int index
);
374 wxGridDataTypeInfoArray m_typeinfo
;
377 // ----------------------------------------------------------------------------
378 // conditional compilation
379 // ----------------------------------------------------------------------------
381 #ifndef WXGRID_DRAW_LINES
382 #define WXGRID_DRAW_LINES 1
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 //#define DEBUG_ATTR_CACHE
390 #ifdef DEBUG_ATTR_CACHE
391 static size_t gs_nAttrCacheHits
= 0;
392 static size_t gs_nAttrCacheMisses
= 0;
393 #endif // DEBUG_ATTR_CACHE
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
400 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
403 // TODO: this doesn't work at all, grid cells have different sizes and approx
404 // calculations don't work as because of the size mismatch scrollbars
405 // sometimes fail to be shown when they should be or vice versa
407 // The scroll bars may be a little flakey once in a while, but that is
408 // surely much less horrible than having scroll lines of only 1!!!
411 // Well, it's still seriously broken so it might be better but needs
414 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
415 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
417 // the size of hash tables used a bit everywhere (the max number of elements
418 // in these hash tables is the number of rows/columns)
419 static const int GRID_HASH_SIZE
= 100;
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 static inline int GetScrollX(int x
)
428 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
431 static inline int GetScrollY(int y
)
433 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
437 // ============================================================================
439 // ============================================================================
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 wxGridCellEditor::wxGridCellEditor()
452 wxGridCellEditor::~wxGridCellEditor()
457 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
458 wxWindowID
WXUNUSED(id
),
459 wxEvtHandler
* evtHandler
)
462 m_control
->PushEventHandler(evtHandler
);
465 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
466 wxGridCellAttr
*attr
)
468 // erase the background because we might not fill the cell
469 wxClientDC
dc(m_control
->GetParent());
470 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
472 gridWindow
->GetOwner()->PrepareDC(dc
);
474 dc
.SetPen(*wxTRANSPARENT_PEN
);
475 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
476 dc
.DrawRectangle(rectCell
);
478 // redraw the control we just painted over
479 m_control
->Refresh();
482 void wxGridCellEditor::Destroy()
486 m_control
->PopEventHandler(true /* delete it*/);
488 m_control
->Destroy();
493 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
495 wxASSERT_MSG(m_control
,
496 wxT("The wxGridCellEditor must be Created first!"));
497 m_control
->Show(show
);
501 // set the colours/fonts if we have any
504 m_colFgOld
= m_control
->GetForegroundColour();
505 m_control
->SetForegroundColour(attr
->GetTextColour());
507 m_colBgOld
= m_control
->GetBackgroundColour();
508 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
510 // Workaround for GTK+1 font setting problem on some platforms
511 #if !defined(__WXGTK__) || defined(__WXGTK20__)
512 m_fontOld
= m_control
->GetFont();
513 m_control
->SetFont(attr
->GetFont());
515 // can't do anything more in the base class version, the other
516 // attributes may only be used by the derived classes
521 // restore the standard colours fonts
522 if ( m_colFgOld
.Ok() )
524 m_control
->SetForegroundColour(m_colFgOld
);
525 m_colFgOld
= wxNullColour
;
528 if ( m_colBgOld
.Ok() )
530 m_control
->SetBackgroundColour(m_colBgOld
);
531 m_colBgOld
= wxNullColour
;
533 // Workaround for GTK+1 font setting problem on some platforms
534 #if !defined(__WXGTK__) || defined(__WXGTK20__)
535 if ( m_fontOld
.Ok() )
537 m_control
->SetFont(m_fontOld
);
538 m_fontOld
= wxNullFont
;
544 void wxGridCellEditor::SetSize(const wxRect
& rect
)
546 wxASSERT_MSG(m_control
,
547 wxT("The wxGridCellEditor must be Created first!"));
548 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
551 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
556 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
558 bool ctrl
= event
.ControlDown();
559 bool alt
= event
.AltDown();
561 // On the Mac the Alt key is more like shift and is used for entry of
562 // valid characters, so check for Ctrl and Meta instead.
563 alt
= event
.MetaDown();
566 // Assume it's not a valid char if ctrl or alt is down, but if both are
567 // down then it may be because of an AltGr key combination, so let them
568 // through in that case.
569 if ((ctrl
|| alt
) && !(ctrl
&& alt
))
573 int key
= event
.GetUnicodeKey();
576 // if the unicode key code is not really a unicode character (it may
577 // be a function key or etc., the platforms appear to always give us a
578 // small value in this case) then fallback to the ascii key code but
579 // don't do anything for function keys or etc.
582 key
= event
.GetKeyCode();
583 keyOk
= (key
<= 127);
586 #else // !wxUSE_UNICODE
587 int key
= event
.GetKeyCode();
591 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
594 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
599 void wxGridCellEditor::StartingClick()
605 // ----------------------------------------------------------------------------
606 // wxGridCellTextEditor
607 // ----------------------------------------------------------------------------
609 wxGridCellTextEditor::wxGridCellTextEditor()
614 void wxGridCellTextEditor::Create(wxWindow
* parent
,
616 wxEvtHandler
* evtHandler
)
618 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
619 wxDefaultPosition
, wxDefaultSize
620 #if defined(__WXMSW__)
621 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
625 // set max length allowed in the textctrl, if the parameter was set
628 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
631 wxGridCellEditor::Create(parent
, id
, evtHandler
);
634 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
635 wxGridCellAttr
* WXUNUSED(attr
))
637 // as we fill the entire client area, don't do anything here to minimize
641 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
643 wxRect
rect(rectOrig
);
645 // Make the edit control large enough to allow for internal
648 // TODO: remove this if the text ctrl sizing is improved esp. for
651 #if defined(__WXGTK__)
660 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
662 // MB: treat MSW separately here otherwise the caret doesn't show
663 // when the editor is in the first row.
664 #if defined(__WXMSW__)
667 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
670 #if defined(__WXMOTIF__)
674 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
675 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
676 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
677 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
680 wxGridCellEditor::SetSize(rect
);
683 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
685 wxASSERT_MSG(m_control
,
686 wxT("The wxGridCellEditor must be Created first!"));
688 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
690 DoBeginEdit(m_startValue
);
693 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
695 Text()->SetValue(startValue
);
696 Text()->SetInsertionPointEnd();
697 Text()->SetSelection(-1,-1);
701 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
704 wxASSERT_MSG(m_control
,
705 wxT("The wxGridCellEditor must be Created first!"));
707 bool changed
= false;
708 wxString value
= Text()->GetValue();
709 if (value
!= m_startValue
)
713 grid
->GetTable()->SetValue(row
, col
, value
);
715 m_startValue
= wxEmptyString
;
716 // No point in setting the text of the hidden control
717 //Text()->SetValue(m_startValue);
723 void wxGridCellTextEditor::Reset()
725 wxASSERT_MSG(m_control
,
726 wxT("The wxGridCellEditor must be Created first!"));
728 DoReset(m_startValue
);
731 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
733 Text()->SetValue(startValue
);
734 Text()->SetInsertionPointEnd();
737 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
739 return wxGridCellEditor::IsAcceptedKey(event
);
742 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
744 // Since this is now happening in the EVT_CHAR event EmulateKeyPress is no
745 // longer an appropriate way to get the character into the text control.
746 // Do it ourselves instead. We know that if we get this far that we have
747 // a valid character, so not a whole lot of testing needs to be done.
749 wxTextCtrl
* tc
= Text();
754 ch
= event
.GetUnicodeKey();
756 ch
= (wxChar
)event
.GetKeyCode();
758 ch
= (wxChar
)event
.GetKeyCode();
763 // delete the character at the cursor
764 pos
= tc
->GetInsertionPoint();
765 if (pos
< tc
->GetLastPosition())
766 tc
->Remove(pos
, pos
+1);
770 // delete the character before the cursor
771 pos
= tc
->GetInsertionPoint();
773 tc
->Remove(pos
-1, pos
);
782 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
783 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
785 #if defined(__WXMOTIF__) || defined(__WXGTK__)
786 // wxMotif needs a little extra help...
787 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
788 wxString
s( Text()->GetValue() );
789 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
791 Text()->SetInsertionPoint( pos
);
793 // the other ports can handle a Return key press
799 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
809 if ( !params
.ToLong(&tmp
) )
811 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
815 m_maxChars
= (size_t)tmp
;
820 // return the value in the text control
821 wxString
wxGridCellTextEditor::GetValue() const
823 return Text()->GetValue();
826 // ----------------------------------------------------------------------------
827 // wxGridCellNumberEditor
828 // ----------------------------------------------------------------------------
830 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
836 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
838 wxEvtHandler
* evtHandler
)
843 // create a spin ctrl
844 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
845 wxDefaultPosition
, wxDefaultSize
,
849 wxGridCellEditor::Create(parent
, id
, evtHandler
);
854 // just a text control
855 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
858 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
859 #endif // wxUSE_VALIDATORS
863 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
865 // first get the value
866 wxGridTableBase
*table
= grid
->GetTable();
867 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
869 m_valueOld
= table
->GetValueAsLong(row
, col
);
874 wxString sValue
= table
->GetValue(row
, col
);
875 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
877 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
885 Spin()->SetValue((int)m_valueOld
);
891 DoBeginEdit(GetString());
895 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
905 value
= Spin()->GetValue();
906 changed
= value
!= m_valueOld
;
908 text
= wxString::Format(wxT("%ld"), value
);
913 text
= Text()->GetValue();
914 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
919 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
920 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
922 grid
->GetTable()->SetValue(row
, col
, text
);
928 void wxGridCellNumberEditor::Reset()
933 Spin()->SetValue((int)m_valueOld
);
938 DoReset(GetString());
942 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
944 if ( wxGridCellEditor::IsAcceptedKey(event
) )
946 int keycode
= event
.GetKeyCode();
947 if ( (keycode
< 128) &&
948 (wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'))
957 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
961 int keycode
= event
.GetKeyCode();
962 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
964 wxGridCellTextEditor::StartingKey(event
);
974 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
985 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
989 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
993 // skip the error message below
998 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1002 // return the value in the spin control if it is there (the text control otherwise)
1003 wxString
wxGridCellNumberEditor::GetValue() const
1010 long value
= Spin()->GetValue();
1011 s
.Printf(wxT("%ld"), value
);
1016 s
= Text()->GetValue();
1022 // ----------------------------------------------------------------------------
1023 // wxGridCellFloatEditor
1024 // ----------------------------------------------------------------------------
1026 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1029 m_precision
= precision
;
1032 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1034 wxEvtHandler
* evtHandler
)
1036 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1038 #if wxUSE_VALIDATORS
1039 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1040 #endif // wxUSE_VALIDATORS
1043 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1045 // first get the value
1046 wxGridTableBase
*table
= grid
->GetTable();
1047 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1049 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1054 wxString sValue
= table
->GetValue(row
, col
);
1055 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1057 wxFAIL_MSG( _T("this cell doesn't have float value") );
1062 DoBeginEdit(GetString());
1065 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1069 wxString
text(Text()->GetValue());
1071 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1073 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1074 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1076 grid
->GetTable()->SetValue(row
, col
, text
);
1083 void wxGridCellFloatEditor::Reset()
1085 DoReset(GetString());
1088 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1090 int keycode
= event
.GetKeyCode();
1092 tmpbuf
[0] = (char) keycode
;
1094 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1095 bool is_decimal_point
= ( strbuf
==
1096 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1097 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1098 || is_decimal_point
)
1100 wxGridCellTextEditor::StartingKey(event
);
1102 // skip Skip() below
1109 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1120 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1124 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1126 m_precision
= (int)tmp
;
1128 // skip the error message below
1133 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1137 wxString
wxGridCellFloatEditor::GetString() const
1140 if ( m_width
== -1 )
1142 // default width/precision
1145 else if ( m_precision
== -1 )
1147 // default precision
1148 fmt
.Printf(_T("%%%d.f"), m_width
);
1152 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1155 return wxString::Format(fmt
, m_valueOld
);
1158 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1160 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1162 int keycode
= event
.GetKeyCode();
1163 printf("%d\n", keycode
);
1164 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1166 tmpbuf
[0] = (char) keycode
;
1168 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1169 bool is_decimal_point
=
1170 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1171 wxLOCALE_CAT_NUMBER
) );
1172 if ( (keycode
< 128) &&
1173 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1174 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1181 #endif // wxUSE_TEXTCTRL
1185 // ----------------------------------------------------------------------------
1186 // wxGridCellBoolEditor
1187 // ----------------------------------------------------------------------------
1189 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1191 wxEvtHandler
* evtHandler
)
1193 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1194 wxDefaultPosition
, wxDefaultSize
,
1197 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1200 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1202 bool resize
= false;
1203 wxSize size
= m_control
->GetSize();
1204 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1206 // check if the checkbox is not too big/small for this cell
1207 wxSize sizeBest
= m_control
->GetBestSize();
1208 if ( !(size
== sizeBest
) )
1210 // reset to default size if it had been made smaller
1216 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1218 // leave 1 pixel margin
1219 size
.x
= size
.y
= minSize
- 2;
1226 m_control
->SetSize(size
);
1229 // position it in the centre of the rectangle (TODO: support alignment?)
1231 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1232 // the checkbox without label still has some space to the right in wxGTK,
1233 // so shift it to the right
1235 #elif defined(__WXMSW__)
1236 // here too, but in other way
1241 int hAlign
= wxALIGN_CENTRE
;
1242 int vAlign
= wxALIGN_CENTRE
;
1244 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1247 if (hAlign
== wxALIGN_LEFT
)
1253 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1255 else if (hAlign
== wxALIGN_RIGHT
)
1257 x
= r
.x
+ r
.width
- size
.x
- 2;
1258 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1260 else if (hAlign
== wxALIGN_CENTRE
)
1262 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1263 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1266 m_control
->Move(x
, y
);
1269 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1271 m_control
->Show(show
);
1275 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1276 CBox()->SetBackgroundColour(colBg
);
1280 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1282 wxASSERT_MSG(m_control
,
1283 wxT("The wxGridCellEditor must be Created first!"));
1285 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1286 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1289 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1290 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1292 CBox()->SetValue(m_startValue
);
1296 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1299 wxASSERT_MSG(m_control
,
1300 wxT("The wxGridCellEditor must be Created first!"));
1302 bool changed
= false;
1303 bool value
= CBox()->GetValue();
1304 if ( value
!= m_startValue
)
1309 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1310 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1312 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1318 void wxGridCellBoolEditor::Reset()
1320 wxASSERT_MSG(m_control
,
1321 wxT("The wxGridCellEditor must be Created first!"));
1323 CBox()->SetValue(m_startValue
);
1326 void wxGridCellBoolEditor::StartingClick()
1328 CBox()->SetValue(!CBox()->GetValue());
1331 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1333 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1335 int keycode
= event
.GetKeyCode();
1348 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1350 int keycode
= event
.GetKeyCode();
1354 CBox()->SetValue(!CBox()->GetValue());
1358 CBox()->SetValue(true);
1362 CBox()->SetValue(false);
1368 // return the value as "1" for true and the empty string for false
1369 wxString
wxGridCellBoolEditor::GetValue() const
1371 bool bSet
= CBox()->GetValue();
1372 return bSet
? _T("1") : wxEmptyString
;
1375 #endif // wxUSE_CHECKBOX
1379 // ----------------------------------------------------------------------------
1380 // wxGridCellChoiceEditor
1381 // ----------------------------------------------------------------------------
1383 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1385 : m_choices(choices
),
1386 m_allowOthers(allowOthers
) { }
1388 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1389 const wxString choices
[],
1391 : m_allowOthers(allowOthers
)
1395 m_choices
.Alloc(count
);
1396 for ( size_t n
= 0; n
< count
; n
++ )
1398 m_choices
.Add(choices
[n
]);
1403 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1405 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1406 editor
->m_allowOthers
= m_allowOthers
;
1407 editor
->m_choices
= m_choices
;
1412 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1414 wxEvtHandler
* evtHandler
)
1416 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1417 wxDefaultPosition
, wxDefaultSize
,
1419 m_allowOthers
? 0 : wxCB_READONLY
);
1421 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1424 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1425 wxGridCellAttr
* attr
)
1427 // as we fill the entire client area, don't do anything here to minimize
1430 // TODO: It doesn't actually fill the client area since the height of a
1431 // combo always defaults to the standard... Until someone has time to
1432 // figure out the right rectangle to paint, just do it the normal way...
1433 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1436 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1438 wxASSERT_MSG(m_control
,
1439 wxT("The wxGridCellEditor must be Created first!"));
1441 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1444 Combo()->SetValue(m_startValue
);
1447 // find the right position, or default to the first if not found
1448 int pos
= Combo()->FindString(m_startValue
);
1451 Combo()->SetSelection(pos
);
1453 Combo()->SetInsertionPointEnd();
1454 Combo()->SetFocus();
1457 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1460 wxString value
= Combo()->GetValue();
1461 if ( value
== m_startValue
)
1464 grid
->GetTable()->SetValue(row
, col
, value
);
1469 void wxGridCellChoiceEditor::Reset()
1471 Combo()->SetValue(m_startValue
);
1472 Combo()->SetInsertionPointEnd();
1475 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1485 wxStringTokenizer
tk(params
, _T(','));
1486 while ( tk
.HasMoreTokens() )
1488 m_choices
.Add(tk
.GetNextToken());
1492 // return the value in the text control
1493 wxString
wxGridCellChoiceEditor::GetValue() const
1495 return Combo()->GetValue();
1498 #endif // wxUSE_COMBOBOX
1500 // ----------------------------------------------------------------------------
1501 // wxGridCellEditorEvtHandler
1502 // ----------------------------------------------------------------------------
1504 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1506 switch ( event
.GetKeyCode() )
1510 m_grid
->DisableCellEditControl();
1514 m_grid
->GetEventHandler()->ProcessEvent( event
);
1518 case WXK_NUMPAD_ENTER
:
1519 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1520 m_editor
->HandleReturn(event
);
1528 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1530 switch ( event
.GetKeyCode() )
1535 case WXK_NUMPAD_ENTER
:
1543 // ----------------------------------------------------------------------------
1544 // wxGridCellWorker is an (almost) empty common base class for
1545 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1546 // ----------------------------------------------------------------------------
1548 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1553 wxGridCellWorker::~wxGridCellWorker()
1557 // ============================================================================
1559 // ============================================================================
1561 // ----------------------------------------------------------------------------
1562 // wxGridCellRenderer
1563 // ----------------------------------------------------------------------------
1565 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1566 wxGridCellAttr
& attr
,
1569 int WXUNUSED(row
), int WXUNUSED(col
),
1572 dc
.SetBackgroundMode( wxSOLID
);
1574 // grey out fields if the grid is disabled
1575 if( grid
.IsEnabled() )
1579 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1583 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1588 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1591 dc
.SetPen( *wxTRANSPARENT_PEN
);
1592 dc
.DrawRectangle(rect
);
1595 // ----------------------------------------------------------------------------
1596 // wxGridCellStringRenderer
1597 // ----------------------------------------------------------------------------
1599 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1600 wxGridCellAttr
& attr
,
1604 dc
.SetBackgroundMode( wxTRANSPARENT
);
1606 // TODO some special colours for attr.IsReadOnly() case?
1608 // different coloured text when the grid is disabled
1609 if( grid
.IsEnabled() )
1613 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1614 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1618 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1619 dc
.SetTextForeground( attr
.GetTextColour() );
1624 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1625 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1628 dc
.SetFont( attr
.GetFont() );
1631 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1633 const wxString
& text
)
1635 wxCoord x
= 0, y
= 0, max_x
= 0;
1636 dc
.SetFont(attr
.GetFont());
1637 wxStringTokenizer
tk(text
, _T('\n'));
1638 while ( tk
.HasMoreTokens() )
1640 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1641 max_x
= wxMax(max_x
, x
);
1644 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1646 return wxSize(max_x
, y
);
1649 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1650 wxGridCellAttr
& attr
,
1654 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1657 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1658 wxGridCellAttr
& attr
,
1660 const wxRect
& rectCell
,
1664 wxRect rect
= rectCell
;
1667 // erase only this cells background, overflow cells should have been erased
1668 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1671 attr
.GetAlignment(&hAlign
, &vAlign
);
1673 int overflowCols
= 0;
1675 if (attr
.GetOverflow())
1677 int cols
= grid
.GetNumberCols();
1678 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1679 int cell_rows
, cell_cols
;
1680 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1681 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1683 int i
, c_cols
, c_rows
;
1684 for (i
= col
+cell_cols
; i
< cols
; i
++)
1686 bool is_empty
= true;
1687 for (int j
=row
; j
<row
+cell_rows
; j
++)
1689 // check w/ anchor cell for multicell block
1690 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1691 if (c_rows
> 0) c_rows
= 0;
1692 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1699 rect
.width
+= grid
.GetColSize(i
);
1705 if (rect
.width
>= best_width
) break;
1707 overflowCols
= i
- col
- cell_cols
+ 1;
1708 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1711 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1713 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1715 clip
.x
+= rectCell
.width
;
1716 // draw each overflow cell individually
1717 int col_end
= col
+cell_cols
+overflowCols
;
1718 if (col_end
>= grid
.GetNumberCols())
1719 col_end
= grid
.GetNumberCols() - 1;
1720 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1722 clip
.width
= grid
.GetColSize(i
) - 1;
1723 dc
.DestroyClippingRegion();
1724 dc
.SetClippingRegion(clip
);
1726 SetTextColoursAndFont(grid
, attr
, dc
,
1727 grid
.IsInSelection(row
,i
));
1729 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1730 rect
, hAlign
, vAlign
);
1731 clip
.x
+= grid
.GetColSize(i
) - 1;
1737 dc
.DestroyClippingRegion();
1741 // now we only have to draw the text
1742 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1744 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1745 rect
, hAlign
, vAlign
);
1748 // ----------------------------------------------------------------------------
1749 // wxGridCellNumberRenderer
1750 // ----------------------------------------------------------------------------
1752 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1754 wxGridTableBase
*table
= grid
.GetTable();
1756 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1758 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1762 text
= table
->GetValue(row
, col
);
1768 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1769 wxGridCellAttr
& attr
,
1771 const wxRect
& rectCell
,
1775 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1777 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1779 // draw the text right aligned by default
1781 attr
.GetAlignment(&hAlign
, &vAlign
);
1782 hAlign
= wxALIGN_RIGHT
;
1784 wxRect rect
= rectCell
;
1787 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1790 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1791 wxGridCellAttr
& attr
,
1795 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1798 // ----------------------------------------------------------------------------
1799 // wxGridCellFloatRenderer
1800 // ----------------------------------------------------------------------------
1802 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1805 SetPrecision(precision
);
1808 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1810 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1811 renderer
->m_width
= m_width
;
1812 renderer
->m_precision
= m_precision
;
1813 renderer
->m_format
= m_format
;
1818 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1820 wxGridTableBase
*table
= grid
.GetTable();
1825 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1827 val
= table
->GetValueAsDouble(row
, col
);
1832 text
= table
->GetValue(row
, col
);
1833 hasDouble
= text
.ToDouble(&val
);
1840 if ( m_width
== -1 )
1842 if ( m_precision
== -1 )
1844 // default width/precision
1845 m_format
= _T("%f");
1849 m_format
.Printf(_T("%%.%df"), m_precision
);
1852 else if ( m_precision
== -1 )
1854 // default precision
1855 m_format
.Printf(_T("%%%d.f"), m_width
);
1859 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1863 text
.Printf(m_format
, val
);
1866 //else: text already contains the string
1871 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1872 wxGridCellAttr
& attr
,
1874 const wxRect
& rectCell
,
1878 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1880 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1882 // draw the text right aligned by default
1884 attr
.GetAlignment(&hAlign
, &vAlign
);
1885 hAlign
= wxALIGN_RIGHT
;
1887 wxRect rect
= rectCell
;
1890 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1893 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1894 wxGridCellAttr
& attr
,
1898 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1901 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1905 // reset to defaults
1911 wxString tmp
= params
.BeforeFirst(_T(','));
1915 if ( tmp
.ToLong(&width
) )
1917 SetWidth((int)width
);
1921 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1925 tmp
= params
.AfterFirst(_T(','));
1929 if ( tmp
.ToLong(&precision
) )
1931 SetPrecision((int)precision
);
1935 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1943 // ----------------------------------------------------------------------------
1944 // wxGridCellBoolRenderer
1945 // ----------------------------------------------------------------------------
1947 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1949 // FIXME these checkbox size calculations are really ugly...
1951 // between checkmark and box
1952 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1954 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1955 wxGridCellAttr
& WXUNUSED(attr
),
1960 // compute it only once (no locks for MT safeness in GUI thread...)
1961 if ( !ms_sizeCheckMark
.x
)
1963 // get checkbox size
1964 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1965 wxSize size
= checkbox
->GetBestSize();
1966 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1968 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1969 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1970 checkSize
-= size
.y
/ 2;
1975 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1978 return ms_sizeCheckMark
;
1981 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1982 wxGridCellAttr
& attr
,
1988 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1990 // draw a check mark in the centre (ignoring alignment - TODO)
1991 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
1993 // don't draw outside the cell
1994 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
1995 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1997 // and even leave (at least) 1 pixel margin
1998 size
.x
= size
.y
= minSize
- 2;
2001 // draw a border around checkmark
2003 attr
.GetAlignment(& hAlign
, &vAlign
);
2006 if (hAlign
== wxALIGN_CENTRE
)
2008 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2009 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2010 rectBorder
.width
= size
.x
;
2011 rectBorder
.height
= size
.y
;
2013 else if (hAlign
== wxALIGN_LEFT
)
2015 rectBorder
.x
= rect
.x
+ 2;
2016 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2017 rectBorder
.width
= size
.x
;
2018 rectBorder
.height
= size
.y
;
2020 else if (hAlign
== wxALIGN_RIGHT
)
2022 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2023 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2024 rectBorder
.width
= size
.x
;
2025 rectBorder
.height
= size
.y
;
2029 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2030 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2033 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2034 value
= !( !cellval
|| (cellval
== wxT("0")) );
2039 wxRect rectMark
= rectBorder
;
2041 // MSW DrawCheckMark() is weird (and should probably be changed...)
2042 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2046 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2049 dc
.SetTextForeground(attr
.GetTextColour());
2050 dc
.DrawCheckMark(rectMark
);
2053 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2054 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2055 dc
.DrawRectangle(rectBorder
);
2058 // ----------------------------------------------------------------------------
2060 // ----------------------------------------------------------------------------
2062 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2066 m_isReadOnly
= Unset
;
2071 m_attrkind
= wxGridCellAttr::Cell
;
2073 m_sizeRows
= m_sizeCols
= 1;
2074 m_overflow
= UnsetOverflow
;
2076 SetDefAttr(attrDefault
);
2079 wxGridCellAttr
*wxGridCellAttr::Clone() const
2081 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2083 if ( HasTextColour() )
2084 attr
->SetTextColour(GetTextColour());
2085 if ( HasBackgroundColour() )
2086 attr
->SetBackgroundColour(GetBackgroundColour());
2088 attr
->SetFont(GetFont());
2089 if ( HasAlignment() )
2090 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2092 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2096 attr
->SetRenderer(m_renderer
);
2097 m_renderer
->IncRef();
2101 attr
->SetEditor(m_editor
);
2106 attr
->SetReadOnly();
2108 attr
->SetKind( m_attrkind
);
2113 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2115 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2116 SetTextColour(mergefrom
->GetTextColour());
2117 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2118 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2119 if ( !HasFont() && mergefrom
->HasFont() )
2120 SetFont(mergefrom
->GetFont());
2121 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2123 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2124 SetAlignment(hAlign
, vAlign
);
2127 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2129 // Directly access member functions as GetRender/Editor don't just return
2130 // m_renderer/m_editor
2132 // Maybe add support for merge of Render and Editor?
2133 if (!HasRenderer() && mergefrom
->HasRenderer() )
2135 m_renderer
= mergefrom
->m_renderer
;
2136 m_renderer
->IncRef();
2138 if ( !HasEditor() && mergefrom
->HasEditor() )
2140 m_editor
= mergefrom
->m_editor
;
2143 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2144 SetReadOnly(mergefrom
->IsReadOnly());
2146 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2147 SetOverflow(mergefrom
->GetOverflow());
2149 SetDefAttr(mergefrom
->m_defGridAttr
);
2152 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2154 // The size of a cell is normally 1,1
2156 // If this cell is larger (2,2) then this is the top left cell
2157 // the other cells that will be covered (lower right cells) must be
2158 // set to negative or zero values such that
2159 // row + num_rows of the covered cell points to the larger cell (this cell)
2160 // same goes for the col + num_cols.
2162 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2164 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2165 !((num_rows
<=0)&&(num_cols
>0)) ||
2166 !((num_rows
==0)&&(num_cols
==0))),
2167 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2169 m_sizeRows
= num_rows
;
2170 m_sizeCols
= num_cols
;
2173 const wxColour
& wxGridCellAttr::GetTextColour() const
2175 if (HasTextColour())
2179 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2181 return m_defGridAttr
->GetTextColour();
2185 wxFAIL_MSG(wxT("Missing default cell attribute"));
2186 return wxNullColour
;
2191 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2193 if (HasBackgroundColour())
2195 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2196 return m_defGridAttr
->GetBackgroundColour();
2199 wxFAIL_MSG(wxT("Missing default cell attribute"));
2200 return wxNullColour
;
2205 const wxFont
& wxGridCellAttr::GetFont() const
2209 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2210 return m_defGridAttr
->GetFont();
2213 wxFAIL_MSG(wxT("Missing default cell attribute"));
2219 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2223 if ( hAlign
) *hAlign
= m_hAlign
;
2224 if ( vAlign
) *vAlign
= m_vAlign
;
2226 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2227 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2230 wxFAIL_MSG(wxT("Missing default cell attribute"));
2234 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2236 if ( num_rows
) *num_rows
= m_sizeRows
;
2237 if ( num_cols
) *num_cols
= m_sizeCols
;
2240 // GetRenderer and GetEditor use a slightly different decision path about
2241 // which attribute to use. If a non-default attr object has one then it is
2242 // used, otherwise the default editor or renderer is fetched from the grid and
2243 // used. It should be the default for the data type of the cell. If it is
2244 // NULL (because the table has a type that the grid does not have in its
2245 // registry,) then the grid's default editor or renderer is used.
2247 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2249 wxGridCellRenderer
*renderer
;
2251 if ( m_renderer
&& this != m_defGridAttr
)
2253 // use the cells renderer if it has one
2254 renderer
= m_renderer
;
2257 else // no non default cell renderer
2259 // get default renderer for the data type
2262 // GetDefaultRendererForCell() will do IncRef() for us
2263 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2272 if (m_defGridAttr
&& this != m_defGridAttr
)
2274 // if we still don't have one then use the grid default
2275 // (no need for IncRef() here neither)
2276 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2278 else // default grid attr
2280 // use m_renderer which we had decided not to use initially
2281 renderer
= m_renderer
;
2288 // we're supposed to always find something
2289 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2294 // same as above, except for s/renderer/editor/g
2295 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2297 wxGridCellEditor
*editor
;
2299 if ( m_editor
&& this != m_defGridAttr
)
2301 // use the cells editor if it has one
2305 else // no non default cell editor
2307 // get default editor for the data type
2310 // GetDefaultEditorForCell() will do IncRef() for us
2311 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2320 if ( m_defGridAttr
&& this != m_defGridAttr
)
2322 // if we still don't have one then use the grid default
2323 // (no need for IncRef() here neither)
2324 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2326 else // default grid attr
2328 // use m_editor which we had decided not to use initially
2336 // we're supposed to always find something
2337 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2342 // ----------------------------------------------------------------------------
2343 // wxGridCellAttrData
2344 // ----------------------------------------------------------------------------
2346 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2348 int n
= FindIndex(row
, col
);
2349 if ( n
== wxNOT_FOUND
)
2351 // add the attribute
2352 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2356 // free the old attribute
2357 m_attrs
[(size_t)n
].attr
->DecRef();
2361 // change the attribute
2362 m_attrs
[(size_t)n
].attr
= attr
;
2366 // remove this attribute
2367 m_attrs
.RemoveAt((size_t)n
);
2372 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2374 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2376 int n
= FindIndex(row
, col
);
2377 if ( n
!= wxNOT_FOUND
)
2379 attr
= m_attrs
[(size_t)n
].attr
;
2386 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2388 size_t count
= m_attrs
.GetCount();
2389 for ( size_t n
= 0; n
< count
; n
++ )
2391 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2392 wxCoord row
= coords
.GetRow();
2393 if ((size_t)row
>= pos
)
2397 // If rows inserted, include row counter where necessary
2398 coords
.SetRow(row
+ numRows
);
2400 else if (numRows
< 0)
2402 // If rows deleted ...
2403 if ((size_t)row
>= pos
- numRows
)
2405 // ...either decrement row counter (if row still exists)...
2406 coords
.SetRow(row
+ numRows
);
2410 // ...or remove the attribute
2411 // No need to DecRef the attribute itself since this is
2412 // done be wxGridCellWithAttr's destructor!
2413 m_attrs
.RemoveAt(n
);
2421 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2423 size_t count
= m_attrs
.GetCount();
2424 for ( size_t n
= 0; n
< count
; n
++ )
2426 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2427 wxCoord col
= coords
.GetCol();
2428 if ( (size_t)col
>= pos
)
2432 // If rows inserted, include row counter where necessary
2433 coords
.SetCol(col
+ numCols
);
2435 else if (numCols
< 0)
2437 // If rows deleted ...
2438 if ((size_t)col
>= pos
- numCols
)
2440 // ...either decrement row counter (if row still exists)...
2441 coords
.SetCol(col
+ numCols
);
2445 // ...or remove the attribute
2446 // No need to DecRef the attribute itself since this is
2447 // done be wxGridCellWithAttr's destructor!
2448 m_attrs
.RemoveAt(n
);
2456 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2458 size_t count
= m_attrs
.GetCount();
2459 for ( size_t n
= 0; n
< count
; n
++ )
2461 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2462 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2471 // ----------------------------------------------------------------------------
2472 // wxGridRowOrColAttrData
2473 // ----------------------------------------------------------------------------
2475 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2477 size_t count
= m_attrs
.Count();
2478 for ( size_t n
= 0; n
< count
; n
++ )
2480 m_attrs
[n
]->DecRef();
2484 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2486 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2488 int n
= m_rowsOrCols
.Index(rowOrCol
);
2489 if ( n
!= wxNOT_FOUND
)
2491 attr
= m_attrs
[(size_t)n
];
2498 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2500 int i
= m_rowsOrCols
.Index(rowOrCol
);
2501 if ( i
== wxNOT_FOUND
)
2503 // add the attribute
2504 m_rowsOrCols
.Add(rowOrCol
);
2509 size_t n
= (size_t)i
;
2512 // change the attribute
2513 m_attrs
[n
]->DecRef();
2518 // remove this attribute
2519 m_attrs
[n
]->DecRef();
2520 m_rowsOrCols
.RemoveAt(n
);
2521 m_attrs
.RemoveAt(n
);
2526 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2528 size_t count
= m_attrs
.GetCount();
2529 for ( size_t n
= 0; n
< count
; n
++ )
2531 int & rowOrCol
= m_rowsOrCols
[n
];
2532 if ( (size_t)rowOrCol
>= pos
)
2534 if ( numRowsOrCols
> 0 )
2536 // If rows inserted, include row counter where necessary
2537 rowOrCol
+= numRowsOrCols
;
2539 else if ( numRowsOrCols
< 0)
2541 // If rows deleted, either decrement row counter (if row still exists)
2542 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2543 rowOrCol
+= numRowsOrCols
;
2546 m_rowsOrCols
.RemoveAt(n
);
2547 m_attrs
[n
]->DecRef();
2548 m_attrs
.RemoveAt(n
);
2556 // ----------------------------------------------------------------------------
2557 // wxGridCellAttrProvider
2558 // ----------------------------------------------------------------------------
2560 wxGridCellAttrProvider::wxGridCellAttrProvider()
2562 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2565 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2570 void wxGridCellAttrProvider::InitData()
2572 m_data
= new wxGridCellAttrProviderData
;
2575 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2576 wxGridCellAttr::wxAttrKind kind
) const
2578 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2583 case (wxGridCellAttr::Any
):
2584 //Get cached merge attributes.
2585 // Currenlty not used as no cache implemented as not mutiable
2586 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2589 //Basicaly implement old version.
2590 //Also check merge cache, so we don't have to re-merge every time..
2591 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2592 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2593 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2595 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2596 // Two or more are non NULL
2597 attr
= new wxGridCellAttr
;
2598 attr
->SetKind(wxGridCellAttr::Merged
);
2602 attr
->MergeWith(attrcell
);
2606 attr
->MergeWith(attrcol
);
2610 attr
->MergeWith(attrrow
);
2613 //store merge attr if cache implemented
2615 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2619 // one or none is non null return it or null.
2620 if(attrrow
) attr
= attrrow
;
2636 case (wxGridCellAttr::Cell
):
2637 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2639 case (wxGridCellAttr::Col
):
2640 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2642 case (wxGridCellAttr::Row
):
2643 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2647 // (wxGridCellAttr::Default):
2648 // (wxGridCellAttr::Merged):
2655 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2661 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2664 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2669 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2672 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2677 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2680 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2684 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2686 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2690 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2694 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2696 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2700 // ----------------------------------------------------------------------------
2701 // wxGridTypeRegistry
2702 // ----------------------------------------------------------------------------
2704 wxGridTypeRegistry::~wxGridTypeRegistry()
2706 size_t count
= m_typeinfo
.Count();
2707 for ( size_t i
= 0; i
< count
; i
++ )
2708 delete m_typeinfo
[i
];
2712 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2713 wxGridCellRenderer
* renderer
,
2714 wxGridCellEditor
* editor
)
2716 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2718 // is it already registered?
2719 int loc
= FindRegisteredDataType(typeName
);
2720 if ( loc
!= wxNOT_FOUND
)
2722 delete m_typeinfo
[loc
];
2723 m_typeinfo
[loc
] = info
;
2727 m_typeinfo
.Add(info
);
2731 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2733 size_t count
= m_typeinfo
.GetCount();
2734 for ( size_t i
= 0; i
< count
; i
++ )
2736 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2745 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2747 int index
= FindRegisteredDataType(typeName
);
2748 if ( index
== wxNOT_FOUND
)
2750 // check whether this is one of the standard ones, in which case
2751 // register it "on the fly"
2753 if ( typeName
== wxGRID_VALUE_STRING
)
2755 RegisterDataType(wxGRID_VALUE_STRING
,
2756 new wxGridCellStringRenderer
,
2757 new wxGridCellTextEditor
);
2759 #endif // wxUSE_TEXTCTRL
2761 if ( typeName
== wxGRID_VALUE_BOOL
)
2763 RegisterDataType(wxGRID_VALUE_BOOL
,
2764 new wxGridCellBoolRenderer
,
2765 new wxGridCellBoolEditor
);
2767 #endif // wxUSE_CHECKBOX
2769 if ( typeName
== wxGRID_VALUE_NUMBER
)
2771 RegisterDataType(wxGRID_VALUE_NUMBER
,
2772 new wxGridCellNumberRenderer
,
2773 new wxGridCellNumberEditor
);
2775 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2777 RegisterDataType(wxGRID_VALUE_FLOAT
,
2778 new wxGridCellFloatRenderer
,
2779 new wxGridCellFloatEditor
);
2781 #endif // wxUSE_TEXTCTRL
2783 if ( typeName
== wxGRID_VALUE_CHOICE
)
2785 RegisterDataType(wxGRID_VALUE_CHOICE
,
2786 new wxGridCellStringRenderer
,
2787 new wxGridCellChoiceEditor
);
2789 #endif // wxUSE_COMBOBOX
2794 // we get here only if just added the entry for this type, so return
2796 index
= m_typeinfo
.GetCount() - 1;
2802 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2804 int index
= FindDataType(typeName
);
2805 if ( index
== wxNOT_FOUND
)
2807 // the first part of the typename is the "real" type, anything after ':'
2808 // are the parameters for the renderer
2809 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2810 if ( index
== wxNOT_FOUND
)
2815 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2816 wxGridCellRenderer
*rendererOld
= renderer
;
2817 renderer
= renderer
->Clone();
2818 rendererOld
->DecRef();
2820 wxGridCellEditor
*editor
= GetEditor(index
);
2821 wxGridCellEditor
*editorOld
= editor
;
2822 editor
= editor
->Clone();
2823 editorOld
->DecRef();
2825 // do it even if there are no parameters to reset them to defaults
2826 wxString params
= typeName
.AfterFirst(_T(':'));
2827 renderer
->SetParameters(params
);
2828 editor
->SetParameters(params
);
2830 // register the new typename
2831 RegisterDataType(typeName
, renderer
, editor
);
2833 // we just registered it, it's the last one
2834 index
= m_typeinfo
.GetCount() - 1;
2840 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2842 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2848 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2850 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2856 // ----------------------------------------------------------------------------
2858 // ----------------------------------------------------------------------------
2860 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2863 wxGridTableBase::wxGridTableBase()
2865 m_view
= (wxGrid
*) NULL
;
2866 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2869 wxGridTableBase::~wxGridTableBase()
2871 delete m_attrProvider
;
2874 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2876 delete m_attrProvider
;
2877 m_attrProvider
= attrProvider
;
2880 bool wxGridTableBase::CanHaveAttributes()
2882 if ( ! GetAttrProvider() )
2884 // use the default attr provider by default
2885 SetAttrProvider(new wxGridCellAttrProvider
);
2890 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2892 if ( m_attrProvider
)
2893 return m_attrProvider
->GetAttr(row
, col
, kind
);
2895 return (wxGridCellAttr
*)NULL
;
2898 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2900 if ( m_attrProvider
)
2902 attr
->SetKind(wxGridCellAttr::Cell
);
2903 m_attrProvider
->SetAttr(attr
, row
, col
);
2907 // as we take ownership of the pointer and don't store it, we must
2913 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2915 if ( m_attrProvider
)
2917 attr
->SetKind(wxGridCellAttr::Row
);
2918 m_attrProvider
->SetRowAttr(attr
, row
);
2922 // as we take ownership of the pointer and don't store it, we must
2928 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2930 if ( m_attrProvider
)
2932 attr
->SetKind(wxGridCellAttr::Col
);
2933 m_attrProvider
->SetColAttr(attr
, col
);
2937 // as we take ownership of the pointer and don't store it, we must
2943 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2944 size_t WXUNUSED(numRows
) )
2946 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2951 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2953 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2958 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2959 size_t WXUNUSED(numRows
) )
2961 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2966 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2967 size_t WXUNUSED(numCols
) )
2969 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2974 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2976 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2981 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2982 size_t WXUNUSED(numCols
) )
2984 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2990 wxString
wxGridTableBase::GetRowLabelValue( int row
)
2993 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
2994 // how much it makes sense to us geeks.
2998 wxString
wxGridTableBase::GetColLabelValue( int col
)
3000 // default col labels are:
3001 // cols 0 to 25 : A-Z
3002 // cols 26 to 675 : AA-ZZ
3007 for ( n
= 1; ; n
++ )
3009 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3011 if ( col
< 0 ) break;
3014 // reverse the string...
3016 for ( i
= 0; i
< n
; i
++ )
3025 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3027 return wxGRID_VALUE_STRING
;
3030 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3031 const wxString
& typeName
)
3033 return typeName
== wxGRID_VALUE_STRING
;
3036 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3038 return CanGetValueAs(row
, col
, typeName
);
3041 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3046 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3051 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3056 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3057 long WXUNUSED(value
) )
3061 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3062 double WXUNUSED(value
) )
3066 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3067 bool WXUNUSED(value
) )
3072 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3073 const wxString
& WXUNUSED(typeName
) )
3078 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3079 const wxString
& WXUNUSED(typeName
),
3080 void* WXUNUSED(value
) )
3084 //////////////////////////////////////////////////////////////////////
3086 // Message class for the grid table to send requests and notifications
3090 wxGridTableMessage::wxGridTableMessage()
3092 m_table
= (wxGridTableBase
*) NULL
;
3098 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3099 int commandInt1
, int commandInt2
)
3103 m_comInt1
= commandInt1
;
3104 m_comInt2
= commandInt2
;
3109 //////////////////////////////////////////////////////////////////////
3111 // A basic grid table for string data. An object of this class will
3112 // created by wxGrid if you don't specify an alternative table class.
3115 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3117 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3119 wxGridStringTable::wxGridStringTable()
3124 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3127 m_data
.Alloc( numRows
);
3130 sa
.Alloc( numCols
);
3131 sa
.Add( wxEmptyString
, numCols
);
3133 m_data
.Add( sa
, numRows
);
3136 wxGridStringTable::~wxGridStringTable()
3140 int wxGridStringTable::GetNumberRows()
3142 return m_data
.GetCount();
3145 int wxGridStringTable::GetNumberCols()
3147 if ( m_data
.GetCount() > 0 )
3148 return m_data
[0].GetCount();
3153 wxString
wxGridStringTable::GetValue( int row
, int col
)
3155 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3157 _T("invalid row or column index in wxGridStringTable") );
3159 return m_data
[row
][col
];
3162 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3164 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3165 _T("invalid row or column index in wxGridStringTable") );
3167 m_data
[row
][col
] = value
;
3170 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3172 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3174 _T("invalid row or column index in wxGridStringTable") );
3176 return (m_data
[row
][col
] == wxEmptyString
);
3179 void wxGridStringTable::Clear()
3182 int numRows
, numCols
;
3184 numRows
= m_data
.GetCount();
3187 numCols
= m_data
[0].GetCount();
3189 for ( row
= 0; row
< numRows
; row
++ )
3191 for ( col
= 0; col
< numCols
; col
++ )
3193 m_data
[row
][col
] = wxEmptyString
;
3200 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3202 size_t curNumRows
= m_data
.GetCount();
3203 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3204 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3206 if ( pos
>= curNumRows
)
3208 return AppendRows( numRows
);
3212 sa
.Alloc( curNumCols
);
3213 sa
.Add( wxEmptyString
, curNumCols
);
3214 m_data
.Insert( sa
, pos
, numRows
);
3217 wxGridTableMessage
msg( this,
3218 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3222 GetView()->ProcessTableMessage( msg
);
3228 bool wxGridStringTable::AppendRows( size_t numRows
)
3230 size_t curNumRows
= m_data
.GetCount();
3231 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3232 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3235 if ( curNumCols
> 0 )
3237 sa
.Alloc( curNumCols
);
3238 sa
.Add( wxEmptyString
, curNumCols
);
3241 m_data
.Add( sa
, numRows
);
3245 wxGridTableMessage
msg( this,
3246 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3249 GetView()->ProcessTableMessage( msg
);
3255 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3257 size_t curNumRows
= m_data
.GetCount();
3259 if ( pos
>= curNumRows
)
3261 wxFAIL_MSG( wxString::Format
3263 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3265 (unsigned long)numRows
,
3266 (unsigned long)curNumRows
3272 if ( numRows
> curNumRows
- pos
)
3274 numRows
= curNumRows
- pos
;
3277 if ( numRows
>= curNumRows
)
3283 m_data
.RemoveAt( pos
, numRows
);
3287 wxGridTableMessage
msg( this,
3288 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3292 GetView()->ProcessTableMessage( msg
);
3298 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3302 size_t curNumRows
= m_data
.GetCount();
3303 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3304 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3306 if ( pos
>= curNumCols
)
3308 return AppendCols( numCols
);
3311 for ( row
= 0; row
< curNumRows
; row
++ )
3313 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3315 m_data
[row
].Insert( wxEmptyString
, col
);
3320 wxGridTableMessage
msg( this,
3321 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3325 GetView()->ProcessTableMessage( msg
);
3331 bool wxGridStringTable::AppendCols( size_t numCols
)
3335 size_t curNumRows
= m_data
.GetCount();
3339 // TODO: something better than this ?
3341 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3346 for ( row
= 0; row
< curNumRows
; row
++ )
3348 m_data
[row
].Add( wxEmptyString
, numCols
);
3353 wxGridTableMessage
msg( this,
3354 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3357 GetView()->ProcessTableMessage( msg
);
3363 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3367 size_t curNumRows
= m_data
.GetCount();
3368 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3369 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3371 if ( pos
>= curNumCols
)
3373 wxFAIL_MSG( wxString::Format
3375 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3377 (unsigned long)numCols
,
3378 (unsigned long)curNumCols
3383 if ( numCols
> curNumCols
- pos
)
3385 numCols
= curNumCols
- pos
;
3388 for ( row
= 0; row
< curNumRows
; row
++ )
3390 if ( numCols
>= curNumCols
)
3392 m_data
[row
].Clear();
3396 m_data
[row
].RemoveAt( pos
, numCols
);
3401 wxGridTableMessage
msg( this,
3402 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3406 GetView()->ProcessTableMessage( msg
);
3412 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3414 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3416 // using default label
3418 return wxGridTableBase::GetRowLabelValue( row
);
3422 return m_rowLabels
[ row
];
3426 wxString
wxGridStringTable::GetColLabelValue( int col
)
3428 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3430 // using default label
3432 return wxGridTableBase::GetColLabelValue( col
);
3436 return m_colLabels
[ col
];
3440 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3442 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3444 int n
= m_rowLabels
.GetCount();
3446 for ( i
= n
; i
<= row
; i
++ )
3448 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3452 m_rowLabels
[row
] = value
;
3455 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3457 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3459 int n
= m_colLabels
.GetCount();
3461 for ( i
= n
; i
<= col
; i
++ )
3463 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3467 m_colLabels
[col
] = value
;
3472 //////////////////////////////////////////////////////////////////////
3473 //////////////////////////////////////////////////////////////////////
3475 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3477 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3478 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3479 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3480 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3481 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3482 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3483 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3486 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3488 const wxPoint
&pos
, const wxSize
&size
)
3489 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3494 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3498 // NO - don't do this because it will set both the x and y origin
3499 // coords to match the parent scrolled window and we just want to
3500 // set the y coord - MB
3502 // m_owner->PrepareDC( dc );
3505 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3506 dc
.SetDeviceOrigin( 0, -y
);
3508 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3509 m_owner
->DrawRowLabels( dc
, rows
);
3513 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3515 m_owner
->ProcessRowLabelMouseEvent( event
);
3519 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3521 m_owner
->GetEventHandler()->ProcessEvent(event
);
3525 // This seems to be required for wxMotif otherwise the mouse
3526 // cursor must be in the cell edit control to get key events
3528 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3530 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3533 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3535 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3538 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3540 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3545 //////////////////////////////////////////////////////////////////////
3547 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3549 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3550 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3551 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3552 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3553 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3554 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3555 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3558 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3560 const wxPoint
&pos
, const wxSize
&size
)
3561 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3566 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3570 // NO - don't do this because it will set both the x and y origin
3571 // coords to match the parent scrolled window and we just want to
3572 // set the x coord - MB
3574 // m_owner->PrepareDC( dc );
3577 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3578 dc
.SetDeviceOrigin( -x
, 0 );
3580 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3581 m_owner
->DrawColLabels( dc
, cols
);
3585 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3587 m_owner
->ProcessColLabelMouseEvent( event
);
3590 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3592 m_owner
->GetEventHandler()->ProcessEvent(event
);
3596 // This seems to be required for wxMotif otherwise the mouse
3597 // cursor must be in the cell edit control to get key events
3599 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3601 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3604 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3606 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3609 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3611 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3615 //////////////////////////////////////////////////////////////////////
3617 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3619 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3620 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3621 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3622 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3623 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3624 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3625 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3628 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3630 const wxPoint
&pos
, const wxSize
&size
)
3631 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3636 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3640 int client_height
= 0;
3641 int client_width
= 0;
3642 GetClientSize( &client_width
, &client_height
);
3648 rect
.SetWidth( client_width
- 2 );
3649 rect
.SetHeight( client_height
- 2 );
3651 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3653 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3654 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3655 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3656 dc
.DrawLine( 0, 0, client_width
, 0 );
3657 dc
.DrawLine( 0, 0, 0, client_height
);
3659 dc
.SetPen( *wxWHITE_PEN
);
3660 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3661 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3666 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3668 m_owner
->ProcessCornerLabelMouseEvent( event
);
3672 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3674 m_owner
->GetEventHandler()->ProcessEvent(event
);
3677 // This seems to be required for wxMotif otherwise the mouse
3678 // cursor must be in the cell edit control to get key events
3680 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3682 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3685 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3687 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3690 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3692 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3696 //////////////////////////////////////////////////////////////////////
3698 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3700 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3701 EVT_PAINT( wxGridWindow::OnPaint
)
3702 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3703 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3704 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3705 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3706 EVT_CHAR ( wxGridWindow::OnChar
)
3707 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3708 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3709 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3712 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3713 wxGridRowLabelWindow
*rowLblWin
,
3714 wxGridColLabelWindow
*colLblWin
,
3717 const wxSize
&size
)
3718 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3719 wxT("grid window") )
3723 m_rowLabelWin
= rowLblWin
;
3724 m_colLabelWin
= colLblWin
;
3728 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3730 wxPaintDC
dc( this );
3731 m_owner
->PrepareDC( dc
);
3732 wxRegion reg
= GetUpdateRegion();
3733 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3734 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3735 #if WXGRID_DRAW_LINES
3736 m_owner
->DrawAllGridLines( dc
, reg
);
3738 m_owner
->DrawGridSpace( dc
);
3739 m_owner
->DrawHighlight( dc
, DirtyCells
);
3743 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3745 wxWindow::ScrollWindow( dx
, dy
, rect
);
3746 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3747 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3751 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3753 m_owner
->ProcessGridCellMouseEvent( event
);
3756 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3758 m_owner
->GetEventHandler()->ProcessEvent(event
);
3761 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3762 // cursor must be in the cell edit control to get key events
3764 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3766 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3769 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3771 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3774 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3776 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3779 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3783 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3785 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3789 //////////////////////////////////////////////////////////////////////
3791 // Internal Helper function for computing row or column from some
3792 // (unscrolled) coordinate value, using either
3793 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3794 // of m_rowBottoms/m_ColRights to speed up the search!
3796 // Internal helper macros for simpler use of that function
3798 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3799 const wxArrayInt
& BorderArray
, int nMax
,
3802 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3803 m_minAcceptableColWidth, \
3804 m_colRights, m_numCols, true)
3805 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3806 m_minAcceptableRowHeight, \
3807 m_rowBottoms, m_numRows, true)
3808 /////////////////////////////////////////////////////////////////////
3810 #if wxUSE_EXTENDED_RTTI
3811 WX_DEFINE_FLAGS( wxGridStyle
)
3813 wxBEGIN_FLAGS( wxGridStyle
)
3814 // new style border flags, we put them first to
3815 // use them for streaming out
3816 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3817 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3818 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3819 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3820 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3821 wxFLAGS_MEMBER(wxBORDER_NONE
)
3823 // old style border flags
3824 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3825 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3826 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3827 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3828 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3829 wxFLAGS_MEMBER(wxBORDER
)
3831 // standard window styles
3832 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3833 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3834 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3835 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3836 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3837 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3838 wxFLAGS_MEMBER(wxVSCROLL
)
3839 wxFLAGS_MEMBER(wxHSCROLL
)
3841 wxEND_FLAGS( wxGridStyle
)
3843 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3845 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3846 wxHIDE_PROPERTY( Children
)
3847 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3848 wxEND_PROPERTIES_TABLE()
3850 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3851 wxEND_HANDLERS_TABLE()
3853 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3856 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3859 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3862 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3863 EVT_PAINT( wxGrid::OnPaint
)
3864 EVT_SIZE( wxGrid::OnSize
)
3865 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3866 EVT_KEY_UP( wxGrid::OnKeyUp
)
3867 EVT_CHAR ( wxGrid::OnChar
)
3868 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3873 // in order to make sure that a size event is not
3874 // trigerred in a unfinished state
3875 m_cornerLabelWin
= NULL
;
3876 m_rowLabelWin
= NULL
;
3877 m_colLabelWin
= NULL
;
3881 wxGrid::wxGrid( wxWindow
*parent
,
3886 const wxString
& name
)
3887 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3888 m_colMinWidths(GRID_HASH_SIZE
),
3889 m_rowMinHeights(GRID_HASH_SIZE
)
3892 SetBestFittingSize(size
);
3895 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3896 const wxPoint
& pos
, const wxSize
& size
,
3897 long style
, const wxString
& name
)
3899 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3900 style
| wxWANTS_CHARS
, name
))
3903 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3904 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3907 SetBestFittingSize(size
);
3915 // Must do this or ~wxScrollHelper will pop the wrong event handler
3916 SetTargetWindow(this);
3918 wxSafeDecRef(m_defaultCellAttr
);
3920 #ifdef DEBUG_ATTR_CACHE
3921 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3922 wxPrintf(_T("wxGrid attribute cache statistics: "
3923 "total: %u, hits: %u (%u%%)\n"),
3924 total
, gs_nAttrCacheHits
,
3925 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3931 delete m_typeRegistry
;
3937 // ----- internal init and update functions
3940 // NOTE: If using the default visual attributes works everywhere then this can
3941 // be removed as well as the #else cases below.
3942 #define _USE_VISATTR 0
3945 #include "wx/listbox.h"
3948 void wxGrid::Create()
3950 m_created
= false; // set to true by CreateGrid
3952 m_table
= (wxGridTableBase
*) NULL
;
3955 m_cellEditCtrlEnabled
= false;
3957 m_defaultCellAttr
= new wxGridCellAttr();
3959 // Set default cell attributes
3960 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3961 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3962 m_defaultCellAttr
->SetFont(GetFont());
3963 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3964 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3965 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3968 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3969 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3971 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3972 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3975 m_defaultCellAttr
->SetTextColour(
3976 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3977 m_defaultCellAttr
->SetBackgroundColour(
3978 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3983 m_currentCellCoords
= wxGridNoCellCoords
;
3985 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3986 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3988 // create the type registry
3989 m_typeRegistry
= new wxGridTypeRegistry
;
3992 // subwindow components that make up the wxGrid
3993 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3998 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4003 m_colLabelWin
= new wxGridColLabelWindow( this,
4008 m_gridWin
= new wxGridWindow( this,
4015 SetTargetWindow( m_gridWin
);
4018 wxColour gfg
= gva
.colFg
;
4019 wxColour gbg
= gva
.colBg
;
4020 wxColour lfg
= lva
.colFg
;
4021 wxColour lbg
= lva
.colBg
;
4023 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4024 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4025 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4026 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4028 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4029 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4030 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4031 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4032 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4033 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4035 m_gridWin
->SetOwnForegroundColour(gfg
);
4036 m_gridWin
->SetOwnBackgroundColour(gbg
);
4042 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4043 wxGrid::wxGridSelectionModes selmode
)
4045 wxCHECK_MSG( !m_created
,
4047 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4049 m_numRows
= numRows
;
4050 m_numCols
= numCols
;
4052 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4053 m_table
->SetView( this );
4055 m_selection
= new wxGridSelection( this, selmode
);
4064 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4066 wxCHECK_RET( m_created
,
4067 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4069 m_selection
->SetSelectionMode( selmode
);
4072 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4074 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4075 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4077 return m_selection
->GetSelectionMode();
4080 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4081 wxGrid::wxGridSelectionModes selmode
)
4085 // stop all processing
4090 wxGridTableBase
*t
=m_table
;
4103 m_numRows
= table
->GetNumberRows();
4104 m_numCols
= table
->GetNumberCols();
4107 m_table
->SetView( this );
4110 m_selection
= new wxGridSelection( this, selmode
);
4123 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4124 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4126 if ( m_rowLabelWin
)
4128 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4132 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4135 m_labelTextColour
= wxColour( _T("BLACK") );
4138 m_attrCache
.row
= -1;
4139 m_attrCache
.col
= -1;
4140 m_attrCache
.attr
= NULL
;
4142 // TODO: something better than this ?
4144 m_labelFont
= this->GetFont();
4145 m_labelFont
.SetWeight( wxBOLD
);
4147 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4148 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4150 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4151 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4152 m_colLabelTextOrientation
= wxHORIZONTAL
;
4154 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4155 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4157 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4158 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4160 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4161 m_defaultRowHeight
+= 8;
4163 m_defaultRowHeight
+= 4;
4166 m_gridLineColour
= wxColour( 192,192,192 );
4167 m_gridLinesEnabled
= true;
4168 m_cellHighlightColour
= *wxBLACK
;
4169 m_cellHighlightPenWidth
= 2;
4170 m_cellHighlightROPenWidth
= 1;
4172 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4173 m_winCapture
= (wxWindow
*)NULL
;
4174 m_canDragRowSize
= true;
4175 m_canDragColSize
= true;
4176 m_canDragGridSize
= true;
4177 m_canDragCell
= false;
4179 m_dragRowOrCol
= -1;
4180 m_isDragging
= false;
4181 m_startDragPos
= wxDefaultPosition
;
4183 m_waitForSlowClick
= false;
4185 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4186 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4188 m_currentCellCoords
= wxGridNoCellCoords
;
4190 m_selectingTopLeft
= wxGridNoCellCoords
;
4191 m_selectingBottomRight
= wxGridNoCellCoords
;
4192 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4193 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4195 m_editable
= true; // default for whole grid
4197 m_inOnKeyDown
= false;
4203 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4204 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4207 // ----------------------------------------------------------------------------
4208 // the idea is to call these functions only when necessary because they create
4209 // quite big arrays which eat memory mostly unnecessary - in particular, if
4210 // default widths/heights are used for all rows/columns, we may not use these
4213 // with some extra code, it should be possible to only store the
4214 // widths/heights different from default ones but this will be done later...
4215 // ----------------------------------------------------------------------------
4217 void wxGrid::InitRowHeights()
4219 m_rowHeights
.Empty();
4220 m_rowBottoms
.Empty();
4222 m_rowHeights
.Alloc( m_numRows
);
4223 m_rowBottoms
.Alloc( m_numRows
);
4227 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4229 for ( int i
= 0; i
< m_numRows
; i
++ )
4231 rowBottom
+= m_defaultRowHeight
;
4232 m_rowBottoms
.Add( rowBottom
);
4236 void wxGrid::InitColWidths()
4238 m_colWidths
.Empty();
4239 m_colRights
.Empty();
4241 m_colWidths
.Alloc( m_numCols
);
4242 m_colRights
.Alloc( m_numCols
);
4245 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4247 for ( int i
= 0; i
< m_numCols
; i
++ )
4249 colRight
+= m_defaultColWidth
;
4250 m_colRights
.Add( colRight
);
4254 int wxGrid::GetColWidth(int col
) const
4256 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4259 int wxGrid::GetColLeft(int col
) const
4261 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4262 : m_colRights
[col
] - m_colWidths
[col
];
4265 int wxGrid::GetColRight(int col
) const
4267 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4271 int wxGrid::GetRowHeight(int row
) const
4273 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4276 int wxGrid::GetRowTop(int row
) const
4278 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4279 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4282 int wxGrid::GetRowBottom(int row
) const
4284 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4285 : m_rowBottoms
[row
];
4288 void wxGrid::CalcDimensions()
4291 GetClientSize( &cw
, &ch
);
4293 if ( m_rowLabelWin
->IsShown() )
4294 cw
-= m_rowLabelWidth
;
4295 if ( m_colLabelWin
->IsShown() )
4296 ch
-= m_colLabelHeight
;
4299 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4300 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4302 // take into account editor if shown
4303 if( IsCellEditControlShown() )
4306 int r
= m_currentCellCoords
.GetRow();
4307 int c
= m_currentCellCoords
.GetCol();
4308 int x
= GetColLeft(c
);
4309 int y
= GetRowTop(r
);
4311 // how big is the editor
4312 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4313 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4314 editor
->GetControl()->GetSize(&w2
, &h2
);
4317 if( w2
> w
) w
= w2
;
4318 if( h2
> h
) h
= h2
;
4323 // preserve (more or less) the previous position
4325 GetViewStart( &x
, &y
);
4327 // ensure the position is valid for the new scroll ranges
4329 x
= wxMax( w
- 1, 0 );
4331 y
= wxMax( h
- 1, 0 );
4333 // do set scrollbar parameters
4334 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4335 GetScrollX(w
), GetScrollY(h
), x
, y
,
4336 GetBatchCount() != 0);
4338 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4339 // still must reposition the children
4344 void wxGrid::CalcWindowSizes()
4346 // escape if the window is has not been fully created yet
4348 if ( m_cornerLabelWin
== NULL
)
4352 GetClientSize( &cw
, &ch
);
4354 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4355 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4357 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4358 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4360 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4361 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4363 if ( m_gridWin
&& m_gridWin
->IsShown() )
4364 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4368 // this is called when the grid table sends a message to say that it
4369 // has been redimensioned
4371 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4374 bool result
= false;
4376 // Clear the attribute cache as the attribute might refer to a different
4377 // cell than stored in the cache after adding/removing rows/columns.
4379 // By the same reasoning, the editor should be dismissed if columns are
4380 // added or removed. And for consistency, it should IMHO always be
4381 // removed, not only if the cell "underneath" it actually changes.
4382 // For now, I intentionally do not save the editor's content as the
4383 // cell it might want to save that stuff to might no longer exist.
4384 HideCellEditControl();
4386 // if we were using the default widths/heights so far, we must change them
4388 if ( m_colWidths
.IsEmpty() )
4393 if ( m_rowHeights
.IsEmpty() )
4399 switch ( msg
.GetId() )
4401 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4403 size_t pos
= msg
.GetCommandInt();
4404 int numRows
= msg
.GetCommandInt2();
4406 m_numRows
+= numRows
;
4408 if ( !m_rowHeights
.IsEmpty() )
4410 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4411 m_rowBottoms
.Insert( 0, pos
, numRows
);
4414 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4416 for ( i
= pos
; i
< m_numRows
; i
++ )
4418 bottom
+= m_rowHeights
[i
];
4419 m_rowBottoms
[i
] = bottom
;
4422 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4424 // if we have just inserted cols into an empty grid the current
4425 // cell will be undefined...
4427 SetCurrentCell( 0, 0 );
4431 m_selection
->UpdateRows( pos
, numRows
);
4432 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4434 attrProvider
->UpdateAttrRows( pos
, numRows
);
4436 if ( !GetBatchCount() )
4439 m_rowLabelWin
->Refresh();
4445 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4447 int numRows
= msg
.GetCommandInt();
4448 int oldNumRows
= m_numRows
;
4449 m_numRows
+= numRows
;
4451 if ( !m_rowHeights
.IsEmpty() )
4453 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4454 m_rowBottoms
.Add( 0, numRows
);
4457 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4459 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4461 bottom
+= m_rowHeights
[i
];
4462 m_rowBottoms
[i
] = bottom
;
4465 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4467 // if we have just inserted cols into an empty grid the current
4468 // cell will be undefined...
4470 SetCurrentCell( 0, 0 );
4472 if ( !GetBatchCount() )
4475 m_rowLabelWin
->Refresh();
4481 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4483 size_t pos
= msg
.GetCommandInt();
4484 int numRows
= msg
.GetCommandInt2();
4485 m_numRows
-= numRows
;
4487 if ( !m_rowHeights
.IsEmpty() )
4489 m_rowHeights
.RemoveAt( pos
, numRows
);
4490 m_rowBottoms
.RemoveAt( pos
, numRows
);
4493 for ( i
= 0; i
< m_numRows
; i
++ )
4495 h
+= m_rowHeights
[i
];
4496 m_rowBottoms
[i
] = h
;
4501 m_currentCellCoords
= wxGridNoCellCoords
;
4505 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4506 m_currentCellCoords
.Set( 0, 0 );
4510 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4511 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4513 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4514 // ifdef'd out following patch from Paul Gammans
4516 // No need to touch column attributes, unless we
4517 // removed _all_ rows, in this case, we remove
4518 // all column attributes.
4519 // I hate to do this here, but the
4520 // needed data is not available inside UpdateAttrRows.
4521 if ( !GetNumberRows() )
4522 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4525 if ( !GetBatchCount() )
4528 m_rowLabelWin
->Refresh();
4534 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4536 size_t pos
= msg
.GetCommandInt();
4537 int numCols
= msg
.GetCommandInt2();
4538 m_numCols
+= numCols
;
4540 if ( !m_colWidths
.IsEmpty() )
4542 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4543 m_colRights
.Insert( 0, pos
, numCols
);
4546 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4548 for ( i
= pos
; i
< m_numCols
; i
++ )
4550 right
+= m_colWidths
[i
];
4551 m_colRights
[i
] = right
;
4554 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4556 // if we have just inserted cols into an empty grid the current
4557 // cell will be undefined...
4559 SetCurrentCell( 0, 0 );
4563 m_selection
->UpdateCols( pos
, numCols
);
4564 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4566 attrProvider
->UpdateAttrCols( pos
, numCols
);
4567 if ( !GetBatchCount() )
4570 m_colLabelWin
->Refresh();
4577 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4579 int numCols
= msg
.GetCommandInt();
4580 int oldNumCols
= m_numCols
;
4581 m_numCols
+= numCols
;
4582 if ( !m_colWidths
.IsEmpty() )
4584 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4585 m_colRights
.Add( 0, numCols
);
4588 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4590 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4592 right
+= m_colWidths
[i
];
4593 m_colRights
[i
] = right
;
4596 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4598 // if we have just inserted cols into an empty grid the current
4599 // cell will be undefined...
4601 SetCurrentCell( 0, 0 );
4603 if ( !GetBatchCount() )
4606 m_colLabelWin
->Refresh();
4612 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4614 size_t pos
= msg
.GetCommandInt();
4615 int numCols
= msg
.GetCommandInt2();
4616 m_numCols
-= numCols
;
4618 if ( !m_colWidths
.IsEmpty() )
4620 m_colWidths
.RemoveAt( pos
, numCols
);
4621 m_colRights
.RemoveAt( pos
, numCols
);
4624 for ( i
= 0; i
< m_numCols
; i
++ )
4626 w
+= m_colWidths
[i
];
4632 m_currentCellCoords
= wxGridNoCellCoords
;
4636 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4637 m_currentCellCoords
.Set( 0, 0 );
4641 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4642 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4644 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4645 // ifdef'd out following patch from Paul Gammans
4647 // No need to touch row attributes, unless we
4648 // removed _all_ columns, in this case, we remove
4649 // all row attributes.
4650 // I hate to do this here, but the
4651 // needed data is not available inside UpdateAttrCols.
4652 if ( !GetNumberCols() )
4653 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4656 if ( !GetBatchCount() )
4659 m_colLabelWin
->Refresh();
4666 if (result
&& !GetBatchCount() )
4667 m_gridWin
->Refresh();
4672 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4674 wxRegionIterator
iter( reg
);
4677 wxArrayInt rowlabels
;
4684 // TODO: remove this when we can...
4685 // There is a bug in wxMotif that gives garbage update
4686 // rectangles if you jump-scroll a long way by clicking the
4687 // scrollbar with middle button. This is a work-around
4689 #if defined(__WXMOTIF__)
4691 m_gridWin
->GetClientSize( &cw
, &ch
);
4692 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4693 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4696 // logical bounds of update region
4699 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4700 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4702 // find the row labels within these bounds
4705 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4707 if ( GetRowBottom(row
) < top
)
4710 if ( GetRowTop(row
) > bottom
)
4713 rowlabels
.Add( row
);
4723 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4725 wxRegionIterator
iter( reg
);
4728 wxArrayInt colLabels
;
4735 // TODO: remove this when we can...
4736 // There is a bug in wxMotif that gives garbage update
4737 // rectangles if you jump-scroll a long way by clicking the
4738 // scrollbar with middle button. This is a work-around
4740 #if defined(__WXMOTIF__)
4742 m_gridWin
->GetClientSize( &cw
, &ch
);
4743 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4744 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4747 // logical bounds of update region
4750 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4751 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4753 // find the cells within these bounds
4756 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4758 if ( GetColRight(col
) < left
)
4761 if ( GetColLeft(col
) > right
)
4764 colLabels
.Add( col
);
4773 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4775 wxRegionIterator
iter( reg
);
4778 wxGridCellCoordsArray cellsExposed
;
4780 int left
, top
, right
, bottom
;
4785 // TODO: remove this when we can...
4786 // There is a bug in wxMotif that gives garbage update
4787 // rectangles if you jump-scroll a long way by clicking the
4788 // scrollbar with middle button. This is a work-around
4790 #if defined(__WXMOTIF__)
4792 m_gridWin
->GetClientSize( &cw
, &ch
);
4793 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4794 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4795 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4796 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4799 // logical bounds of update region
4801 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4802 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4804 // find the cells within these bounds
4807 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4809 if ( GetRowBottom(row
) <= top
)
4812 if ( GetRowTop(row
) > bottom
)
4815 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4817 if ( GetColRight(col
) <= left
)
4820 if ( GetColLeft(col
) > right
)
4823 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4830 return cellsExposed
;
4834 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4837 wxPoint
pos( event
.GetPosition() );
4838 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4840 if ( event
.Dragging() )
4844 m_isDragging
= true;
4845 m_rowLabelWin
->CaptureMouse();
4848 if ( event
.LeftIsDown() )
4850 switch( m_cursorMode
)
4852 case WXGRID_CURSOR_RESIZE_ROW
:
4854 int cw
, ch
, left
, dummy
;
4855 m_gridWin
->GetClientSize( &cw
, &ch
);
4856 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4858 wxClientDC
dc( m_gridWin
);
4861 GetRowTop(m_dragRowOrCol
) +
4862 GetRowMinimalHeight(m_dragRowOrCol
) );
4863 dc
.SetLogicalFunction(wxINVERT
);
4864 if ( m_dragLastPos
>= 0 )
4866 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4868 dc
.DrawLine( left
, y
, left
+cw
, y
);
4873 case WXGRID_CURSOR_SELECT_ROW
:
4874 if ( (row
= YToRow( y
)) >= 0 )
4878 m_selection
->SelectRow( row
,
4879 event
.ControlDown(),
4886 // default label to suppress warnings about "enumeration value
4887 // 'xxx' not handled in switch
4895 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4900 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4901 m_isDragging
= false;
4904 // ------------ Entering or leaving the window
4906 if ( event
.Entering() || event
.Leaving() )
4908 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4912 // ------------ Left button pressed
4914 else if ( event
.LeftDown() )
4916 // don't send a label click event for a hit on the
4917 // edge of the row label - this is probably the user
4918 // wanting to resize the row
4920 if ( YToEdgeOfRow(y
) < 0 )
4924 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4926 if ( !event
.ShiftDown() && !event
.ControlDown() )
4930 if ( event
.ShiftDown() )
4932 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4935 GetNumberCols() - 1,
4936 event
.ControlDown(),
4943 m_selection
->SelectRow( row
,
4944 event
.ControlDown(),
4951 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4956 // starting to drag-resize a row
4958 if ( CanDragRowSize() )
4959 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4964 // ------------ Left double click
4966 else if (event
.LeftDClick() )
4968 int row
= YToEdgeOfRow(y
);
4973 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4975 // no default action at the moment
4980 // adjust row height depending on label text
4981 AutoSizeRowLabelSize( row
);
4983 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4989 // ------------ Left button released
4991 else if ( event
.LeftUp() )
4993 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4995 DoEndDragResizeRow();
4997 // Note: we are ending the event *after* doing
4998 // default processing in this case
5000 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5003 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5008 // ------------ Right button down
5010 else if ( event
.RightDown() )
5014 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5016 // no default action at the moment
5021 // ------------ Right double click
5023 else if ( event
.RightDClick() )
5027 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5029 // no default action at the moment
5034 // ------------ No buttons down and mouse moving
5036 else if ( event
.Moving() )
5038 m_dragRowOrCol
= YToEdgeOfRow( y
);
5039 if ( m_dragRowOrCol
>= 0 )
5041 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5043 // don't capture the mouse yet
5044 if ( CanDragRowSize() )
5045 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5048 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5050 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5056 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5059 wxPoint
pos( event
.GetPosition() );
5060 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5062 if ( event
.Dragging() )
5066 m_isDragging
= true;
5067 m_colLabelWin
->CaptureMouse();
5070 if ( event
.LeftIsDown() )
5072 switch( m_cursorMode
)
5074 case WXGRID_CURSOR_RESIZE_COL
:
5076 int cw
, ch
, dummy
, top
;
5077 m_gridWin
->GetClientSize( &cw
, &ch
);
5078 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5080 wxClientDC
dc( m_gridWin
);
5083 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5084 GetColMinimalWidth(m_dragRowOrCol
));
5085 dc
.SetLogicalFunction(wxINVERT
);
5086 if ( m_dragLastPos
>= 0 )
5088 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5090 dc
.DrawLine( x
, top
, x
, top
+ch
);
5095 case WXGRID_CURSOR_SELECT_COL
:
5096 if ( (col
= XToCol( x
)) >= 0 )
5100 m_selection
->SelectCol( col
,
5101 event
.ControlDown(),
5108 // default label to suppress warnings about "enumeration value
5109 // 'xxx' not handled in switch
5117 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5122 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5123 m_isDragging
= false;
5126 // ------------ Entering or leaving the window
5128 if ( event
.Entering() || event
.Leaving() )
5130 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5134 // ------------ Left button pressed
5136 else if ( event
.LeftDown() )
5138 // don't send a label click event for a hit on the
5139 // edge of the col label - this is probably the user
5140 // wanting to resize the col
5142 if ( XToEdgeOfCol(x
) < 0 )
5146 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5148 if ( !event
.ShiftDown() && !event
.ControlDown() )
5152 if ( event
.ShiftDown() )
5154 m_selection
->SelectBlock( 0,
5155 m_currentCellCoords
.GetCol(),
5156 GetNumberRows() - 1, col
,
5157 event
.ControlDown(),
5164 m_selection
->SelectCol( col
,
5165 event
.ControlDown(),
5172 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5177 // starting to drag-resize a col
5179 if ( CanDragColSize() )
5180 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5185 // ------------ Left double click
5187 if ( event
.LeftDClick() )
5189 int col
= XToEdgeOfCol(x
);
5194 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5196 // no default action at the moment
5201 // adjust column width depending on label text
5202 AutoSizeColLabelSize( col
);
5204 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5210 // ------------ Left button released
5212 else if ( event
.LeftUp() )
5214 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5216 DoEndDragResizeCol();
5218 // Note: we are ending the event *after* doing
5219 // default processing in this case
5221 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5224 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5229 // ------------ Right button down
5231 else if ( event
.RightDown() )
5235 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5237 // no default action at the moment
5242 // ------------ Right double click
5244 else if ( event
.RightDClick() )
5248 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5250 // no default action at the moment
5255 // ------------ No buttons down and mouse moving
5257 else if ( event
.Moving() )
5259 m_dragRowOrCol
= XToEdgeOfCol( x
);
5260 if ( m_dragRowOrCol
>= 0 )
5262 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5264 // don't capture the cursor yet
5265 if ( CanDragColSize() )
5266 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5269 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5271 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5277 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5279 if ( event
.LeftDown() )
5281 // indicate corner label by having both row and
5284 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5290 else if ( event
.LeftDClick() )
5292 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5295 else if ( event
.RightDown() )
5297 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5299 // no default action at the moment
5303 else if ( event
.RightDClick() )
5305 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5307 // no default action at the moment
5312 void wxGrid::ChangeCursorMode(CursorMode mode
,
5317 static const wxChar
*cursorModes
[] =
5326 wxLogTrace(_T("grid"),
5327 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5328 win
== m_colLabelWin
? _T("colLabelWin")
5329 : win
? _T("rowLabelWin")
5331 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5332 #endif // __WXDEBUG__
5334 if ( mode
== m_cursorMode
&&
5335 win
== m_winCapture
&&
5336 captureMouse
== (m_winCapture
!= NULL
))
5341 // by default use the grid itself
5347 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5348 m_winCapture
= (wxWindow
*)NULL
;
5351 m_cursorMode
= mode
;
5353 switch ( m_cursorMode
)
5355 case WXGRID_CURSOR_RESIZE_ROW
:
5356 win
->SetCursor( m_rowResizeCursor
);
5359 case WXGRID_CURSOR_RESIZE_COL
:
5360 win
->SetCursor( m_colResizeCursor
);
5364 win
->SetCursor( *wxSTANDARD_CURSOR
);
5367 // we need to capture mouse when resizing
5368 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5369 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5371 if ( captureMouse
&& resize
)
5373 win
->CaptureMouse();
5378 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5381 wxPoint
pos( event
.GetPosition() );
5382 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5384 wxGridCellCoords coords
;
5385 XYToCell( x
, y
, coords
);
5387 int cell_rows
, cell_cols
;
5388 bool isFirstDrag
= !m_isDragging
;
5389 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5390 if ((cell_rows
< 0) || (cell_cols
< 0))
5392 coords
.SetRow(coords
.GetRow() + cell_rows
);
5393 coords
.SetCol(coords
.GetCol() + cell_cols
);
5396 if ( event
.Dragging() )
5398 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5400 // Don't start doing anything until the mouse has been drug at
5401 // least 3 pixels in any direction...
5404 if (m_startDragPos
== wxDefaultPosition
)
5406 m_startDragPos
= pos
;
5409 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5413 m_isDragging
= true;
5414 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5416 // Hide the edit control, so it
5417 // won't interfer with drag-shrinking.
5418 if ( IsCellEditControlShown() )
5420 HideCellEditControl();
5421 SaveEditControlValue();
5424 // Have we captured the mouse yet?
5427 m_winCapture
= m_gridWin
;
5428 m_winCapture
->CaptureMouse();
5431 if ( coords
!= wxGridNoCellCoords
)
5433 if ( event
.ControlDown() )
5435 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5436 m_selectingKeyboard
= coords
;
5437 HighlightBlock ( m_selectingKeyboard
, coords
);
5439 else if ( CanDragCell() )
5443 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5444 m_selectingKeyboard
= coords
;
5446 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5454 if ( !IsSelection() )
5456 HighlightBlock( coords
, coords
);
5460 HighlightBlock( m_currentCellCoords
, coords
);
5464 if (! IsVisible(coords
))
5466 MakeCellVisible(coords
);
5467 // TODO: need to introduce a delay or something here. The
5468 // scrolling is way to fast, at least on MSW - also on GTK.
5472 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5474 int cw
, ch
, left
, dummy
;
5475 m_gridWin
->GetClientSize( &cw
, &ch
);
5476 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5478 wxClientDC
dc( m_gridWin
);
5480 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5481 GetRowMinimalHeight(m_dragRowOrCol
) );
5482 dc
.SetLogicalFunction(wxINVERT
);
5483 if ( m_dragLastPos
>= 0 )
5485 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5487 dc
.DrawLine( left
, y
, left
+cw
, y
);
5490 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5492 int cw
, ch
, dummy
, top
;
5493 m_gridWin
->GetClientSize( &cw
, &ch
);
5494 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5496 wxClientDC
dc( m_gridWin
);
5498 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5499 GetColMinimalWidth(m_dragRowOrCol
) );
5500 dc
.SetLogicalFunction(wxINVERT
);
5501 if ( m_dragLastPos
>= 0 )
5503 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5505 dc
.DrawLine( x
, top
, x
, top
+ch
);
5512 m_isDragging
= false;
5513 m_startDragPos
= wxDefaultPosition
;
5515 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5516 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5519 if ( event
.Entering() || event
.Leaving() )
5521 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5522 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5527 // ------------ Left button pressed
5529 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5531 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5536 if ( !event
.ControlDown() )
5538 if ( event
.ShiftDown() )
5542 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5543 m_currentCellCoords
.GetCol(),
5546 event
.ControlDown(),
5552 else if ( XToEdgeOfCol(x
) < 0 &&
5553 YToEdgeOfRow(y
) < 0 )
5555 DisableCellEditControl();
5556 MakeCellVisible( coords
);
5558 if ( event
.ControlDown() )
5562 m_selection
->ToggleCellSelection( coords
.GetRow(),
5564 event
.ControlDown(),
5569 m_selectingTopLeft
= wxGridNoCellCoords
;
5570 m_selectingBottomRight
= wxGridNoCellCoords
;
5571 m_selectingKeyboard
= coords
;
5575 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5576 SetCurrentCell( coords
);
5579 if ( m_selection
->GetSelectionMode() !=
5580 wxGrid::wxGridSelectCells
)
5582 HighlightBlock( coords
, coords
);
5591 // ------------ Left double click
5593 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5595 DisableCellEditControl();
5597 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5599 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5604 // we want double click to select a cell and start editing
5605 // (i.e. to behave in same way as sequence of two slow clicks):
5606 m_waitForSlowClick
= true;
5613 // ------------ Left button released
5615 else if ( event
.LeftUp() )
5617 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5621 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5622 m_winCapture
= NULL
;
5625 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5628 EnableCellEditControl();
5630 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5631 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5632 editor
->StartingClick();
5636 m_waitForSlowClick
= false;
5638 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5639 m_selectingBottomRight
!= wxGridNoCellCoords
)
5643 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5644 m_selectingTopLeft
.GetCol(),
5645 m_selectingBottomRight
.GetRow(),
5646 m_selectingBottomRight
.GetCol(),
5647 event
.ControlDown(),
5653 m_selectingTopLeft
= wxGridNoCellCoords
;
5654 m_selectingBottomRight
= wxGridNoCellCoords
;
5656 // Show the edit control, if it has been hidden for
5658 ShowCellEditControl();
5661 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5663 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5664 DoEndDragResizeRow();
5666 // Note: we are ending the event *after* doing
5667 // default processing in this case
5669 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5671 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5673 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5674 DoEndDragResizeCol();
5676 // Note: we are ending the event *after* doing
5677 // default processing in this case
5679 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5686 // ------------ Right button down
5688 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5690 DisableCellEditControl();
5691 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5696 // no default action at the moment
5701 // ------------ Right double click
5703 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5705 DisableCellEditControl();
5706 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5711 // no default action at the moment
5715 // ------------ Moving and no button action
5717 else if ( event
.Moving() && !event
.IsButton() )
5719 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5721 // out of grid cell area
5722 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5726 int dragRow
= YToEdgeOfRow( y
);
5727 int dragCol
= XToEdgeOfCol( x
);
5729 // Dragging on the corner of a cell to resize in both
5730 // directions is not implemented yet...
5732 if ( dragRow
>= 0 && dragCol
>= 0 )
5734 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5740 m_dragRowOrCol
= dragRow
;
5742 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5744 if ( CanDragRowSize() && CanDragGridSize() )
5745 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5750 m_dragRowOrCol
= dragCol
;
5758 m_dragRowOrCol
= dragCol
;
5760 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5762 if ( CanDragColSize() && CanDragGridSize() )
5763 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5769 // Neither on a row or col edge
5771 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5773 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5779 void wxGrid::DoEndDragResizeRow()
5781 if ( m_dragLastPos
>= 0 )
5783 // erase the last line and resize the row
5785 int cw
, ch
, left
, dummy
;
5786 m_gridWin
->GetClientSize( &cw
, &ch
);
5787 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5789 wxClientDC
dc( m_gridWin
);
5791 dc
.SetLogicalFunction( wxINVERT
);
5792 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5793 HideCellEditControl();
5794 SaveEditControlValue();
5796 int rowTop
= GetRowTop(m_dragRowOrCol
);
5797 SetRowSize( m_dragRowOrCol
,
5798 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5800 if ( !GetBatchCount() )
5802 // Only needed to get the correct rect.y:
5803 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5805 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5806 rect
.width
= m_rowLabelWidth
;
5807 rect
.height
= ch
- rect
.y
;
5808 m_rowLabelWin
->Refresh( true, &rect
);
5810 // if there is a multicell block, paint all of it
5813 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5814 int leftCol
= XToCol(left
);
5815 int rightCol
= internalXToCol(left
+cw
);
5818 for (i
=leftCol
; i
<rightCol
; i
++)
5820 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5821 if (cell_rows
< subtract_rows
)
5822 subtract_rows
= cell_rows
;
5824 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5825 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5826 rect
.height
= ch
- rect
.y
;
5829 m_gridWin
->Refresh( false, &rect
);
5832 ShowCellEditControl();
5837 void wxGrid::DoEndDragResizeCol()
5839 if ( m_dragLastPos
>= 0 )
5841 // erase the last line and resize the col
5843 int cw
, ch
, dummy
, top
;
5844 m_gridWin
->GetClientSize( &cw
, &ch
);
5845 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5847 wxClientDC
dc( m_gridWin
);
5849 dc
.SetLogicalFunction( wxINVERT
);
5850 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5851 HideCellEditControl();
5852 SaveEditControlValue();
5854 int colLeft
= GetColLeft(m_dragRowOrCol
);
5855 SetColSize( m_dragRowOrCol
,
5856 wxMax( m_dragLastPos
- colLeft
,
5857 GetColMinimalWidth(m_dragRowOrCol
) ) );
5859 if ( !GetBatchCount() )
5861 // Only needed to get the correct rect.x:
5862 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5864 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5865 rect
.width
= cw
- rect
.x
;
5866 rect
.height
= m_colLabelHeight
;
5867 m_colLabelWin
->Refresh( true, &rect
);
5869 // if there is a multicell block, paint all of it
5872 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5873 int topRow
= YToRow(top
);
5874 int bottomRow
= internalYToRow(top
+cw
);
5877 for (i
=topRow
; i
<bottomRow
; i
++)
5879 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5880 if (cell_cols
< subtract_cols
)
5881 subtract_cols
= cell_cols
;
5883 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5884 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5885 rect
.width
= cw
- rect
.x
;
5888 m_gridWin
->Refresh( false, &rect
);
5891 ShowCellEditControl();
5898 // ------ interaction with data model
5900 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5902 switch ( msg
.GetId() )
5904 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5905 return GetModelValues();
5907 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5908 return SetModelValues();
5910 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5911 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5912 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5913 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5914 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5915 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5916 return Redimension( msg
);
5925 // The behaviour of this function depends on the grid table class
5926 // Clear() function. For the default wxGridStringTable class the
5927 // behavious is to replace all cell contents with wxEmptyString but
5928 // not to change the number of rows or cols.
5930 void wxGrid::ClearGrid()
5934 if (IsCellEditControlEnabled())
5935 DisableCellEditControl();
5938 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5943 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5945 // TODO: something with updateLabels flag
5949 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5955 if (IsCellEditControlEnabled())
5956 DisableCellEditControl();
5958 bool done
= m_table
->InsertRows( pos
, numRows
);
5961 // the table will have sent the results of the insert row
5962 // operation to this view object as a grid table message
5968 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5970 // TODO: something with updateLabels flag
5974 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5980 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5982 // the table will have sent the results of the append row
5983 // operation to this view object as a grid table message
5989 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5991 // TODO: something with updateLabels flag
5995 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6001 if (IsCellEditControlEnabled())
6002 DisableCellEditControl();
6004 bool done
= m_table
->DeleteRows( pos
, numRows
);
6006 // the table will have sent the results of the delete row
6007 // operation to this view object as a grid table message
6013 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6015 // TODO: something with updateLabels flag
6019 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6025 if (IsCellEditControlEnabled())
6026 DisableCellEditControl();
6028 bool done
= m_table
->InsertCols( pos
, numCols
);
6030 // the table will have sent the results of the insert col
6031 // operation to this view object as a grid table message
6037 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6039 // TODO: something with updateLabels flag
6043 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6049 bool done
= m_table
->AppendCols( numCols
);
6051 // the table will have sent the results of the append col
6052 // operation to this view object as a grid table message
6058 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6060 // TODO: something with updateLabels flag
6064 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6070 if (IsCellEditControlEnabled())
6071 DisableCellEditControl();
6073 bool done
= m_table
->DeleteCols( pos
, numCols
);
6075 // the table will have sent the results of the delete col
6076 // operation to this view object as a grid table message
6084 // ----- event handlers
6087 // Generate a grid event based on a mouse event and
6088 // return the result of ProcessEvent()
6090 int wxGrid::SendEvent( const wxEventType type
,
6092 wxMouseEvent
& mouseEv
)
6097 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6099 int rowOrCol
= (row
== -1 ? col
: row
);
6101 wxGridSizeEvent
gridEvt( GetId(),
6105 mouseEv
.GetX() + GetRowLabelSize(),
6106 mouseEv
.GetY() + GetColLabelSize(),
6107 mouseEv
.ControlDown(),
6108 mouseEv
.ShiftDown(),
6110 mouseEv
.MetaDown() );
6112 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6113 vetoed
= !gridEvt
.IsAllowed();
6115 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6117 // Right now, it should _never_ end up here!
6118 wxGridRangeSelectEvent
gridEvt( GetId(),
6122 m_selectingBottomRight
,
6124 mouseEv
.ControlDown(),
6125 mouseEv
.ShiftDown(),
6127 mouseEv
.MetaDown() );
6129 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6130 vetoed
= !gridEvt
.IsAllowed();
6134 wxGridEvent
gridEvt( GetId(),
6138 mouseEv
.GetX() + GetRowLabelSize(),
6139 mouseEv
.GetY() + GetColLabelSize(),
6141 mouseEv
.ControlDown(),
6142 mouseEv
.ShiftDown(),
6144 mouseEv
.MetaDown() );
6145 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6146 vetoed
= !gridEvt
.IsAllowed();
6149 // A Veto'd event may not be `claimed' so test this first
6150 if (vetoed
) return -1;
6151 return claimed
? 1 : 0;
6155 // Generate a grid event of specified type and return the result
6156 // of ProcessEvent().
6158 int wxGrid::SendEvent( const wxEventType type
,
6164 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6166 int rowOrCol
= (row
== -1 ? col
: row
);
6168 wxGridSizeEvent
gridEvt( GetId(),
6173 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6174 vetoed
= !gridEvt
.IsAllowed();
6178 wxGridEvent
gridEvt( GetId(),
6183 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6184 vetoed
= !gridEvt
.IsAllowed();
6187 // A Veto'd event may not be `claimed' so test this first
6188 if (vetoed
) return -1;
6189 return claimed
? 1 : 0;
6193 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6195 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6198 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6200 // Don't do anything if between Begin/EndBatch...
6201 // EndBatch() will do all this on the last nested one anyway.
6202 if (! GetBatchCount())
6204 // Refresh to get correct scrolled position:
6205 wxScrolledWindow::Refresh(eraseb
,rect
);
6209 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6210 int width_label
, width_cell
, height_label
, height_cell
;
6213 //Copy rectangle can get scroll offsets..
6214 rect_x
= rect
->GetX();
6215 rect_y
= rect
->GetY();
6216 rectWidth
= rect
->GetWidth();
6217 rectHeight
= rect
->GetHeight();
6219 width_label
= m_rowLabelWidth
- rect_x
;
6220 if (width_label
> rectWidth
) width_label
= rectWidth
;
6222 height_label
= m_colLabelHeight
- rect_y
;
6223 if (height_label
> rectHeight
) height_label
= rectHeight
;
6225 if (rect_x
> m_rowLabelWidth
)
6227 x
= rect_x
- m_rowLabelWidth
;
6228 width_cell
= rectWidth
;
6233 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6236 if (rect_y
> m_colLabelHeight
)
6238 y
= rect_y
- m_colLabelHeight
;
6239 height_cell
= rectHeight
;
6244 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6247 // Paint corner label part intersecting rect.
6248 if ( width_label
> 0 && height_label
> 0 )
6250 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6251 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6254 // Paint col labels part intersecting rect.
6255 if ( width_cell
> 0 && height_label
> 0 )
6257 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6258 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6261 // Paint row labels part intersecting rect.
6262 if ( width_label
> 0 && height_cell
> 0 )
6264 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6265 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6268 // Paint cell area part intersecting rect.
6269 if ( width_cell
> 0 && height_cell
> 0 )
6271 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6272 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6277 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6278 m_colLabelWin
->Refresh(eraseb
, NULL
);
6279 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6280 m_gridWin
->Refresh(eraseb
, NULL
);
6285 void wxGrid::OnSize( wxSizeEvent
& event
)
6287 // position the child windows
6290 // don't call CalcDimensions() from here, the base class handles the size
6296 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6298 if ( m_inOnKeyDown
)
6300 // shouldn't be here - we are going round in circles...
6302 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6305 m_inOnKeyDown
= true;
6307 // propagate the event up and see if it gets processed
6309 wxWindow
*parent
= GetParent();
6310 wxKeyEvent
keyEvt( event
);
6311 keyEvt
.SetEventObject( parent
);
6313 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6316 // try local handlers
6318 switch ( event
.GetKeyCode() )
6321 if ( event
.ControlDown() )
6323 MoveCursorUpBlock( event
.ShiftDown() );
6327 MoveCursorUp( event
.ShiftDown() );
6332 if ( event
.ControlDown() )
6334 MoveCursorDownBlock( event
.ShiftDown() );
6338 MoveCursorDown( event
.ShiftDown() );
6343 if ( event
.ControlDown() )
6345 MoveCursorLeftBlock( event
.ShiftDown() );
6349 MoveCursorLeft( event
.ShiftDown() );
6354 if ( event
.ControlDown() )
6356 MoveCursorRightBlock( event
.ShiftDown() );
6360 MoveCursorRight( event
.ShiftDown() );
6365 case WXK_NUMPAD_ENTER
:
6366 if ( event
.ControlDown() )
6368 event
.Skip(); // to let the edit control have the return
6372 if ( GetGridCursorRow() < GetNumberRows()-1 )
6374 MoveCursorDown( event
.ShiftDown() );
6378 // at the bottom of a column
6379 DisableCellEditControl();
6389 if (event
.ShiftDown())
6391 if ( GetGridCursorCol() > 0 )
6393 MoveCursorLeft( false );
6398 DisableCellEditControl();
6403 if ( GetGridCursorCol() < GetNumberCols()-1 )
6405 MoveCursorRight( false );
6410 DisableCellEditControl();
6416 if ( event
.ControlDown() )
6418 MakeCellVisible( 0, 0 );
6419 SetCurrentCell( 0, 0 );
6428 if ( event
.ControlDown() )
6430 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6431 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6448 if ( event
.ControlDown() )
6452 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6453 m_currentCellCoords
.GetCol(),
6454 event
.ControlDown(),
6461 if ( !IsEditable() )
6463 MoveCursorRight( false );
6466 // Otherwise fall through to default
6474 m_inOnKeyDown
= false;
6477 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6479 // try local handlers
6481 if ( event
.GetKeyCode() == WXK_SHIFT
)
6483 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6484 m_selectingBottomRight
!= wxGridNoCellCoords
)
6488 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6489 m_selectingTopLeft
.GetCol(),
6490 m_selectingBottomRight
.GetRow(),
6491 m_selectingBottomRight
.GetCol(),
6492 event
.ControlDown(),
6499 m_selectingTopLeft
= wxGridNoCellCoords
;
6500 m_selectingBottomRight
= wxGridNoCellCoords
;
6501 m_selectingKeyboard
= wxGridNoCellCoords
;
6505 void wxGrid::OnChar( wxKeyEvent
& event
)
6507 // is it possible to edit the current cell at all?
6508 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6510 // yes, now check whether the cells editor accepts the key
6511 int row
= m_currentCellCoords
.GetRow();
6512 int col
= m_currentCellCoords
.GetCol();
6513 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6514 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6516 // <F2> is special and will always start editing, for
6517 // other keys - ask the editor itself
6518 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6519 || editor
->IsAcceptedKey(event
) )
6521 // ensure cell is visble
6522 MakeCellVisible(row
, col
);
6523 EnableCellEditControl();
6525 // a problem can arise if the cell is not completely
6526 // visible (even after calling MakeCellVisible the
6527 // control is not created and calling StartingKey will
6529 if ( editor
->IsCreated() && m_cellEditCtrlEnabled
)
6530 editor
->StartingKey(event
);
6547 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6551 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6553 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6555 // the event has been intercepted - do nothing
6559 wxClientDC
dc(m_gridWin
);
6562 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6564 DisableCellEditControl();
6566 if ( IsVisible( m_currentCellCoords
, false ) )
6569 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6570 if ( !m_gridLinesEnabled
)
6578 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6580 // Otherwise refresh redraws the highlight!
6581 m_currentCellCoords
= coords
;
6583 DrawGridCellArea(dc
,cells
);
6584 DrawAllGridLines( dc
, r
);
6588 m_currentCellCoords
= coords
;
6590 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6591 DrawCellHighlight(dc
, attr
);
6596 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6599 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6603 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6606 rightCol
= GetNumberCols() - 1;
6608 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6611 bottomRow
= GetNumberRows() - 1;
6615 if ( topRow
> bottomRow
)
6622 if ( leftCol
> rightCol
)
6629 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6630 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6632 // First the case that we selected a completely new area
6633 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6634 m_selectingBottomRight
== wxGridNoCellCoords
)
6637 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6638 wxGridCellCoords ( bottomRow
, rightCol
) );
6639 m_gridWin
->Refresh( false, &rect
);
6641 // Now handle changing an existing selection area.
6642 else if ( m_selectingTopLeft
!= updateTopLeft
||
6643 m_selectingBottomRight
!= updateBottomRight
)
6645 // Compute two optimal update rectangles:
6646 // Either one rectangle is a real subset of the
6647 // other, or they are (almost) disjoint!
6649 bool need_refresh
[4];
6653 need_refresh
[3] = false;
6656 // Store intermediate values
6657 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6658 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6659 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6660 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6662 // Determine the outer/inner coordinates.
6663 if (oldLeft
> leftCol
)
6669 if (oldTop
> topRow
)
6675 if (oldRight
< rightCol
)
6678 oldRight
= rightCol
;
6681 if (oldBottom
< bottomRow
)
6684 oldBottom
= bottomRow
;
6688 // Now, either the stuff marked old is the outer
6689 // rectangle or we don't have a situation where one
6690 // is contained in the other.
6692 if ( oldLeft
< leftCol
)
6694 // Refresh the newly selected or deselected
6695 // area to the left of the old or new selection.
6696 need_refresh
[0] = true;
6697 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6699 wxGridCellCoords ( oldBottom
,
6703 if ( oldTop
< topRow
)
6705 // Refresh the newly selected or deselected
6706 // area above the old or new selection.
6707 need_refresh
[1] = true;
6708 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6710 wxGridCellCoords ( topRow
- 1,
6714 if ( oldRight
> rightCol
)
6716 // Refresh the newly selected or deselected
6717 // area to the right of the old or new selection.
6718 need_refresh
[2] = true;
6719 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6721 wxGridCellCoords ( oldBottom
,
6725 if ( oldBottom
> bottomRow
)
6727 // Refresh the newly selected or deselected
6728 // area below the old or new selection.
6729 need_refresh
[3] = true;
6730 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6732 wxGridCellCoords ( oldBottom
,
6736 // various Refresh() calls
6737 for (i
= 0; i
< 4; i
++ )
6738 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6739 m_gridWin
->Refresh( false, &(rect
[i
]) );
6742 m_selectingTopLeft
= updateTopLeft
;
6743 m_selectingBottomRight
= updateBottomRight
;
6747 // ------ functions to get/send data (see also public functions)
6750 bool wxGrid::GetModelValues()
6752 // Hide the editor, so it won't hide a changed value.
6753 HideCellEditControl();
6757 // all we need to do is repaint the grid
6759 m_gridWin
->Refresh();
6767 bool wxGrid::SetModelValues()
6771 // Disable the editor, so it won't hide a changed value.
6772 // Do we also want to save the current value of the editor first?
6774 DisableCellEditControl();
6778 for ( row
= 0; row
< m_numRows
; row
++ )
6780 for ( col
= 0; col
< m_numCols
; col
++ )
6782 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6794 // Note - this function only draws cells that are in the list of
6795 // exposed cells (usually set from the update region by
6796 // CalcExposedCells)
6798 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6800 if ( !m_numRows
|| !m_numCols
) return;
6802 int i
, numCells
= cells
.GetCount();
6803 int row
, col
, cell_rows
, cell_cols
;
6804 wxGridCellCoordsArray redrawCells
;
6806 for ( i
= numCells
-1; i
>= 0; i
-- )
6808 row
= cells
[i
].GetRow();
6809 col
= cells
[i
].GetCol();
6810 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6812 // If this cell is part of a multicell block, find owner for repaint
6813 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6815 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6816 bool marked
= false;
6817 for ( int j
= 0; j
< numCells
; j
++ )
6819 if ( cell
== cells
[j
] )
6827 int count
= redrawCells
.GetCount();
6828 for (int j
= 0; j
< count
; j
++)
6830 if ( cell
== redrawCells
[j
] )
6836 if (!marked
) redrawCells
.Add( cell
);
6838 continue; // don't bother drawing this cell
6841 // If this cell is empty, find cell to left that might want to overflow
6842 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6844 for ( int l
= 0; l
< cell_rows
; l
++ )
6846 // find a cell in this row to left alreay marked for repaint
6848 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6849 if ((redrawCells
[k
].GetCol() < left
) &&
6850 (redrawCells
[k
].GetRow() == row
))
6851 left
=redrawCells
[k
].GetCol();
6853 if (left
== col
) left
= 0; // oh well
6855 for (int j
= col
-1; j
>= left
; j
--)
6857 if (!m_table
->IsEmptyCell(row
+l
, j
))
6859 if (GetCellOverflow(row
+l
, j
))
6861 wxGridCellCoords
cell(row
+l
, j
);
6862 bool marked
= false;
6864 for (int k
= 0; k
< numCells
; k
++)
6866 if ( cell
== cells
[k
] )
6874 int count
= redrawCells
.GetCount();
6875 for (int k
= 0; k
< count
; k
++)
6877 if ( cell
== redrawCells
[k
] )
6883 if (!marked
) redrawCells
.Add( cell
);
6891 DrawCell( dc
, cells
[i
] );
6894 numCells
= redrawCells
.GetCount();
6896 for ( i
= numCells
- 1; i
>= 0; i
-- )
6898 DrawCell( dc
, redrawCells
[i
] );
6903 void wxGrid::DrawGridSpace( wxDC
& dc
)
6906 m_gridWin
->GetClientSize( &cw
, &ch
);
6909 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6911 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6912 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6914 if ( right
> rightCol
|| bottom
> bottomRow
)
6917 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6919 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6920 dc
.SetPen( *wxTRANSPARENT_PEN
);
6922 if ( right
> rightCol
)
6924 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6927 if ( bottom
> bottomRow
)
6929 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6935 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6937 int row
= coords
.GetRow();
6938 int col
= coords
.GetCol();
6940 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6943 // we draw the cell border ourselves
6944 #if !WXGRID_DRAW_LINES
6945 if ( m_gridLinesEnabled
)
6946 DrawCellBorder( dc
, coords
);
6949 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6951 bool isCurrent
= coords
== m_currentCellCoords
;
6953 wxRect rect
= CellToRect( row
, col
);
6955 // if the editor is shown, we should use it and not the renderer
6956 // Note: However, only if it is really _shown_, i.e. not hidden!
6957 if ( isCurrent
&& IsCellEditControlShown() )
6959 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6960 editor
->PaintBackground(rect
, attr
);
6965 // but all the rest is drawn by the cell renderer and hence may be
6967 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6968 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6975 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6977 int row
= m_currentCellCoords
.GetRow();
6978 int col
= m_currentCellCoords
.GetCol();
6980 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6983 wxRect rect
= CellToRect(row
, col
);
6985 // hmmm... what could we do here to show that the cell is disabled?
6986 // for now, I just draw a thinner border than for the other ones, but
6987 // it doesn't look really good
6989 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6993 // The center of th drawn line is where the position/width/height of
6994 // the rectangle is actually at, (on wxMSW atr least,) so we will
6995 // reduce the size of the rectangle to compensate for the thickness of
6996 // the line. If this is too strange on non wxMSW platforms then
6997 // please #ifdef this appropriately.
6998 rect
.x
+= penWidth
/2;
6999 rect
.y
+= penWidth
/2;
7000 rect
.width
-= penWidth
-1;
7001 rect
.height
-= penWidth
-1;
7004 // Now draw the rectangle
7005 // use the cellHighlightColour if the cell is inside a selection, this
7006 // will ensure the cell is always visible.
7007 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7008 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7009 dc
.DrawRectangle(rect
);
7013 // VZ: my experiments with 3d borders...
7015 // how to properly set colours for arbitrary bg?
7016 wxCoord x1
= rect
.x
,
7018 x2
= rect
.x
+ rect
.width
-1,
7019 y2
= rect
.y
+ rect
.height
-1;
7021 dc
.SetPen(*wxWHITE_PEN
);
7022 dc
.DrawLine(x1
, y1
, x2
, y1
);
7023 dc
.DrawLine(x1
, y1
, x1
, y2
);
7025 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7026 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7028 dc
.SetPen(*wxBLACK_PEN
);
7029 dc
.DrawLine(x1
, y2
, x2
, y2
);
7030 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7035 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7037 int row
= coords
.GetRow();
7038 int col
= coords
.GetCol();
7039 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7042 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7044 wxRect rect
= CellToRect( row
, col
);
7046 // right hand border
7048 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7049 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7053 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7054 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7057 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7059 // This if block was previously in wxGrid::OnPaint but that doesn't
7060 // seem to get called under wxGTK - MB
7062 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7063 m_numRows
&& m_numCols
)
7065 m_currentCellCoords
.Set(0, 0);
7068 if ( IsCellEditControlShown() )
7070 // don't show highlight when the edit control is shown
7074 // if the active cell was repainted, repaint its highlight too because it
7075 // might have been damaged by the grid lines
7076 size_t count
= cells
.GetCount();
7077 for ( size_t n
= 0; n
< count
; n
++ )
7079 if ( cells
[n
] == m_currentCellCoords
)
7081 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7082 DrawCellHighlight(dc
, attr
);
7090 // TODO: remove this ???
7091 // This is used to redraw all grid lines e.g. when the grid line colour
7094 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7096 #if !WXGRID_DRAW_LINES
7100 if ( !m_gridLinesEnabled
||
7102 !m_numCols
) return;
7104 int top
, bottom
, left
, right
;
7106 #if 0 //#ifndef __WXGTK__
7110 m_gridWin
->GetClientSize(&cw
, &ch
);
7112 // virtual coords of visible area
7114 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7115 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7120 reg
.GetBox(x
, y
, w
, h
);
7121 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7122 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7126 m_gridWin
->GetClientSize(&cw
, &ch
);
7127 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7128 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7131 // avoid drawing grid lines past the last row and col
7133 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7134 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7136 // no gridlines inside multicells, clip them out
7137 int leftCol
= internalXToCol(left
);
7138 int topRow
= internalYToRow(top
);
7139 int rightCol
= internalXToCol(right
);
7140 int bottomRow
= internalYToRow(bottom
);
7141 wxRegion
clippedcells(0, 0, cw
, ch
);
7144 int i
, j
, cell_rows
, cell_cols
;
7147 for (j
=topRow
; j
<bottomRow
; j
++)
7149 for (i
=leftCol
; i
<rightCol
; i
++)
7151 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7152 if ((cell_rows
> 1) || (cell_cols
> 1))
7154 rect
= CellToRect(j
,i
);
7155 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7156 clippedcells
.Subtract(rect
);
7158 else if ((cell_rows
< 0) || (cell_cols
< 0))
7160 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7161 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7162 clippedcells
.Subtract(rect
);
7166 dc
.SetClippingRegion( clippedcells
);
7168 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7170 // horizontal grid lines
7172 // already declared above - int i;
7173 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7175 int bot
= GetRowBottom(i
) - 1;
7184 dc
.DrawLine( left
, bot
, right
, bot
);
7189 // vertical grid lines
7191 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7193 int colRight
= GetColRight(i
) - 1;
7194 if ( colRight
> right
)
7199 if ( colRight
>= left
)
7201 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7204 dc
.DestroyClippingRegion();
7208 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7210 if ( !m_numRows
) return;
7213 size_t numLabels
= rows
.GetCount();
7215 for ( i
= 0; i
< numLabels
; i
++ )
7217 DrawRowLabel( dc
, rows
[i
] );
7222 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7224 if ( GetRowHeight(row
) <= 0 )
7230 rect
.SetY( GetRowTop(row
) + 1 );
7231 rect
.SetWidth( m_rowLabelWidth
- 2 );
7232 rect
.SetHeight( GetRowHeight(row
) - 2 );
7234 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7236 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7238 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7240 int rowTop
= GetRowTop(row
),
7241 rowBottom
= GetRowBottom(row
) - 1;
7243 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7244 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7245 m_rowLabelWidth
-1, rowBottom
);
7247 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7249 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7251 dc
.SetPen( *wxWHITE_PEN
);
7252 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7253 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7255 dc
.SetBackgroundMode( wxTRANSPARENT
);
7256 dc
.SetTextForeground( GetLabelTextColour() );
7257 dc
.SetFont( GetLabelFont() );
7260 GetRowLabelAlignment( &hAlign
, &vAlign
);
7263 rect
.SetY( GetRowTop(row
) + 2 );
7264 rect
.SetWidth( m_rowLabelWidth
- 4 );
7265 rect
.SetHeight( GetRowHeight(row
) - 4 );
7266 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7270 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7272 if ( !m_numCols
) return;
7275 size_t numLabels
= cols
.GetCount();
7277 for ( i
= 0; i
< numLabels
; i
++ )
7279 DrawColLabel( dc
, cols
[i
] );
7284 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7286 if ( GetColWidth(col
) <= 0 )
7289 int colLeft
= GetColLeft(col
);
7293 rect
.SetX( colLeft
+ 1 );
7295 rect
.SetWidth( GetColWidth(col
) - 2 );
7296 rect
.SetHeight( m_colLabelHeight
- 2 );
7298 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7300 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7302 int colRight
= GetColRight(col
) - 1;
7304 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7305 dc
.DrawLine( colRight
, 0,
7306 colRight
, m_colLabelHeight
-1 );
7308 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7310 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7311 colRight
+1, m_colLabelHeight
-1 );
7313 dc
.SetPen( *wxWHITE_PEN
);
7314 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7315 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7317 dc
.SetBackgroundMode( wxTRANSPARENT
);
7318 dc
.SetTextForeground( GetLabelTextColour() );
7319 dc
.SetFont( GetLabelFont() );
7321 int hAlign
, vAlign
, orient
;
7322 GetColLabelAlignment( &hAlign
, &vAlign
);
7323 orient
= GetColLabelTextOrientation();
7325 rect
.SetX( colLeft
+ 2 );
7327 rect
.SetWidth( GetColWidth(col
) - 4 );
7328 rect
.SetHeight( m_colLabelHeight
- 4 );
7329 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7332 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7333 const wxString
& value
,
7337 int textOrientation
)
7339 wxArrayString lines
;
7341 StringToLines( value
, lines
);
7344 //Forward to new API.
7345 DrawTextRectangle( dc
,
7354 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7355 const wxArrayString
& lines
,
7359 int textOrientation
)
7361 long textWidth
, textHeight
;
7362 long lineWidth
, lineHeight
;
7365 dc
.SetClippingRegion( rect
);
7367 nLines
= lines
.GetCount();
7371 float x
= 0.0, y
= 0.0;
7373 if( textOrientation
== wxHORIZONTAL
)
7374 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7376 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7380 case wxALIGN_BOTTOM
:
7381 if( textOrientation
== wxHORIZONTAL
)
7382 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7384 x
= rect
.x
+ rect
.width
- textWidth
;
7387 case wxALIGN_CENTRE
:
7388 if( textOrientation
== wxHORIZONTAL
)
7389 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7391 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7396 if( textOrientation
== wxHORIZONTAL
)
7403 // Align each line of a multi-line label
7404 for( l
= 0; l
< nLines
; l
++ )
7406 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7408 switch( horizAlign
)
7411 if( textOrientation
== wxHORIZONTAL
)
7412 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7414 y
= rect
.y
+ lineWidth
+ 1;
7417 case wxALIGN_CENTRE
:
7418 if( textOrientation
== wxHORIZONTAL
)
7419 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7421 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7426 if( textOrientation
== wxHORIZONTAL
)
7429 y
= rect
.y
+ rect
.height
- 1;
7433 if( textOrientation
== wxHORIZONTAL
)
7435 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7440 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7445 dc
.DestroyClippingRegion();
7449 // Split multi line text up into an array of strings. Any existing
7450 // contents of the string array are preserved.
7452 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7456 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7457 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7459 while ( startPos
< (int)tVal
.Length() )
7461 pos
= tVal
.Mid(startPos
).Find( eol
);
7466 else if ( pos
== 0 )
7468 lines
.Add( wxEmptyString
);
7472 lines
.Add( value
.Mid(startPos
, pos
) );
7476 if ( startPos
< (int)value
.Length() )
7478 lines
.Add( value
.Mid( startPos
) );
7483 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7484 const wxArrayString
& lines
,
7485 long *width
, long *height
)
7492 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7494 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7495 w
= wxMax( w
, lineW
);
7504 // ------ Batch processing.
7506 void wxGrid::EndBatch()
7508 if ( m_batchCount
> 0 )
7511 if ( !m_batchCount
)
7514 m_rowLabelWin
->Refresh();
7515 m_colLabelWin
->Refresh();
7516 m_cornerLabelWin
->Refresh();
7517 m_gridWin
->Refresh();
7522 // Use this, rather than wxWindow::Refresh(), to force an immediate
7523 // repainting of the grid. Has no effect if you are already inside a
7524 // BeginBatch / EndBatch block.
7526 void wxGrid::ForceRefresh()
7532 bool wxGrid::Enable(bool enable
)
7534 if ( !wxScrolledWindow::Enable(enable
) )
7537 // redraw in the new state
7538 m_gridWin
->Refresh();
7544 // ------ Edit control functions
7548 void wxGrid::EnableEditing( bool edit
)
7550 // TODO: improve this ?
7552 if ( edit
!= m_editable
)
7554 if(!edit
) EnableCellEditControl(edit
);
7560 void wxGrid::EnableCellEditControl( bool enable
)
7565 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7566 SetCurrentCell( 0, 0 );
7568 if ( enable
!= m_cellEditCtrlEnabled
)
7572 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7575 // this should be checked by the caller!
7576 wxASSERT_MSG( CanEnableCellControl(),
7577 _T("can't enable editing for this cell!") );
7579 // do it before ShowCellEditControl()
7580 m_cellEditCtrlEnabled
= enable
;
7582 ShowCellEditControl();
7586 //FIXME:add veto support
7587 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7589 HideCellEditControl();
7590 SaveEditControlValue();
7592 // do it after HideCellEditControl()
7593 m_cellEditCtrlEnabled
= enable
;
7598 bool wxGrid::IsCurrentCellReadOnly() const
7601 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7602 bool readonly
= attr
->IsReadOnly();
7608 bool wxGrid::CanEnableCellControl() const
7610 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7611 !IsCurrentCellReadOnly();
7615 bool wxGrid::IsCellEditControlEnabled() const
7617 // the cell edit control might be disable for all cells or just for the
7618 // current one if it's read only
7619 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7622 bool wxGrid::IsCellEditControlShown() const
7624 bool isShown
= false;
7626 if ( m_cellEditCtrlEnabled
)
7628 int row
= m_currentCellCoords
.GetRow();
7629 int col
= m_currentCellCoords
.GetCol();
7630 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7631 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7636 if ( editor
->IsCreated() )
7638 isShown
= editor
->GetControl()->IsShown();
7648 void wxGrid::ShowCellEditControl()
7650 if ( IsCellEditControlEnabled() )
7652 if ( !IsVisible( m_currentCellCoords
) )
7654 m_cellEditCtrlEnabled
= false;
7659 wxRect rect
= CellToRect( m_currentCellCoords
);
7660 int row
= m_currentCellCoords
.GetRow();
7661 int col
= m_currentCellCoords
.GetCol();
7663 // if this is part of a multicell, find owner (topleft)
7664 int cell_rows
, cell_cols
;
7665 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7666 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7670 m_currentCellCoords
.SetRow( row
);
7671 m_currentCellCoords
.SetCol( col
);
7674 // convert to scrolled coords
7676 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7678 // done in PaintBackground()
7680 // erase the highlight and the cell contents because the editor
7681 // might not cover the entire cell
7682 wxClientDC
dc( m_gridWin
);
7684 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7685 dc
.SetPen(*wxTRANSPARENT_PEN
);
7686 dc
.DrawRectangle(rect
);
7689 // cell is shifted by one pixel
7690 // However, don't allow x or y to become negative
7691 // since the SetSize() method interprets that as
7698 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7699 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7700 if ( !editor
->IsCreated() )
7702 editor
->Create(m_gridWin
, wxID_ANY
,
7703 new wxGridCellEditorEvtHandler(this, editor
));
7705 wxGridEditorCreatedEvent
evt(GetId(),
7706 wxEVT_GRID_EDITOR_CREATED
,
7710 editor
->GetControl());
7711 GetEventHandler()->ProcessEvent(evt
);
7715 // resize editor to overflow into righthand cells if allowed
7716 int maxWidth
= rect
.width
;
7717 wxString value
= GetCellValue(row
, col
);
7718 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7721 GetTextExtent(value
, &maxWidth
, &y
,
7722 NULL
, NULL
, &attr
->GetFont());
7723 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7725 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7726 if (rect
.x
+maxWidth
> client_right
)
7727 maxWidth
= client_right
- rect
.x
;
7729 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7731 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7732 // may have changed earlier
7733 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7736 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7737 // looks weird going over a multicell
7738 if (m_table
->IsEmptyCell(row
,i
) &&
7739 (rect
.width
< maxWidth
) && (c_rows
== 1))
7740 rect
.width
+= GetColWidth(i
);
7744 if (rect
.GetRight() > client_right
)
7745 rect
.SetRight(client_right
-1);
7748 editor
->SetCellAttr(attr
);
7749 editor
->SetSize( rect
);
7750 editor
->Show( true, attr
);
7752 // recalc dimensions in case we need to
7753 // expand the scrolled window to account for editor
7756 editor
->BeginEdit(row
, col
, this);
7757 editor
->SetCellAttr(NULL
);
7766 void wxGrid::HideCellEditControl()
7768 if ( IsCellEditControlEnabled() )
7770 int row
= m_currentCellCoords
.GetRow();
7771 int col
= m_currentCellCoords
.GetCol();
7773 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7774 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7775 editor
->Show( false );
7778 m_gridWin
->SetFocus();
7779 // refresh whole row to the right
7780 wxRect
rect( CellToRect(row
, col
) );
7781 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7782 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7783 m_gridWin
->Refresh( false, &rect
);
7788 void wxGrid::SaveEditControlValue()
7790 if ( IsCellEditControlEnabled() )
7792 int row
= m_currentCellCoords
.GetRow();
7793 int col
= m_currentCellCoords
.GetCol();
7795 wxString oldval
= GetCellValue(row
,col
);
7797 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7798 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7799 bool changed
= editor
->EndEdit(row
, col
, this);
7806 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7807 m_currentCellCoords
.GetRow(),
7808 m_currentCellCoords
.GetCol() ) < 0 ) {
7810 // Event has been vetoed, set the data back.
7811 SetCellValue(row
,col
,oldval
);
7819 // ------ Grid location functions
7820 // Note that all of these functions work with the logical coordinates of
7821 // grid cells and labels so you will need to convert from device
7822 // coordinates for mouse events etc.
7825 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7827 int row
= YToRow(y
);
7828 int col
= XToCol(x
);
7830 if ( row
== -1 || col
== -1 )
7832 coords
= wxGridNoCellCoords
;
7836 coords
.Set( row
, col
);
7841 // Internal Helper function for computing row or column from some
7842 // (unscrolled) coordinate value, using either
7843 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7844 // of m_rowBottoms/m_ColRights to speed up the search!
7846 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7847 const wxArrayInt
& BorderArray
, int nMax
,
7852 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7858 size_t i_max
= coord
/ defaultDist
,
7861 if (BorderArray
.IsEmpty())
7863 if((int) i_max
< nMax
)
7865 return clipToMinMax
? nMax
- 1 : -1;
7868 if ( i_max
>= BorderArray
.GetCount())
7869 i_max
= BorderArray
.GetCount() - 1;
7872 if ( coord
>= BorderArray
[i_max
])
7876 i_max
= coord
/ minDist
;
7878 i_max
= BorderArray
.GetCount() - 1;
7880 if ( i_max
>= BorderArray
.GetCount())
7881 i_max
= BorderArray
.GetCount() - 1;
7883 if ( coord
>= BorderArray
[i_max
])
7884 return clipToMinMax
? (int)i_max
: -1;
7885 if ( coord
< BorderArray
[0] )
7888 while ( i_max
- i_min
> 0 )
7890 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7891 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7892 if (coord
>= BorderArray
[ i_max
- 1])
7896 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7897 if (coord
< BorderArray
[median
])
7905 int wxGrid::YToRow( int y
)
7907 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7908 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7912 int wxGrid::XToCol( int x
)
7914 return CoordToRowOrCol(x
, m_defaultColWidth
,
7915 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7919 // return the row number that that the y coord is near the edge of, or
7920 // -1 if not near an edge
7922 int wxGrid::YToEdgeOfRow( int y
)
7925 i
= internalYToRow(y
);
7927 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7929 // We know that we are in row i, test whether we are
7930 // close enough to lower or upper border, respectively.
7931 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7933 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7941 // return the col number that that the x coord is near the edge of, or
7942 // -1 if not near an edge
7944 int wxGrid::XToEdgeOfCol( int x
)
7947 i
= internalXToCol(x
);
7949 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7951 // We know that we are in column i, test whether we are
7952 // close enough to right or left border, respectively.
7953 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7955 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7963 wxRect
wxGrid::CellToRect( int row
, int col
)
7965 wxRect
rect( -1, -1, -1, -1 );
7967 if ( row
>= 0 && row
< m_numRows
&&
7968 col
>= 0 && col
< m_numCols
)
7970 int i
, cell_rows
, cell_cols
;
7971 rect
.width
= rect
.height
= 0;
7972 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7973 // if negative then find multicell owner
7974 if (cell_rows
< 0) row
+= cell_rows
;
7975 if (cell_cols
< 0) col
+= cell_cols
;
7976 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7978 rect
.x
= GetColLeft(col
);
7979 rect
.y
= GetRowTop(row
);
7980 for (i
=col
; i
<col
+cell_cols
; i
++)
7981 rect
.width
+= GetColWidth(i
);
7982 for (i
=row
; i
<row
+cell_rows
; i
++)
7983 rect
.height
+= GetRowHeight(i
);
7986 // if grid lines are enabled, then the area of the cell is a bit smaller
7987 if (m_gridLinesEnabled
) {
7995 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7997 // get the cell rectangle in logical coords
7999 wxRect
r( CellToRect( row
, col
) );
8001 // convert to device coords
8003 int left
, top
, right
, bottom
;
8004 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8005 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8007 // check against the client area of the grid window
8010 m_gridWin
->GetClientSize( &cw
, &ch
);
8012 if ( wholeCellVisible
)
8014 // is the cell wholly visible ?
8016 return ( left
>= 0 && right
<= cw
&&
8017 top
>= 0 && bottom
<= ch
);
8021 // is the cell partly visible ?
8023 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8024 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8029 // make the specified cell location visible by doing a minimal amount
8032 void wxGrid::MakeCellVisible( int row
, int col
)
8036 int xpos
= -1, ypos
= -1;
8038 if ( row
>= 0 && row
< m_numRows
&&
8039 col
>= 0 && col
< m_numCols
)
8041 // get the cell rectangle in logical coords
8043 wxRect
r( CellToRect( row
, col
) );
8045 // convert to device coords
8047 int left
, top
, right
, bottom
;
8048 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8049 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8052 m_gridWin
->GetClientSize( &cw
, &ch
);
8058 else if ( bottom
> ch
)
8060 int h
= r
.GetHeight();
8062 for ( i
= row
-1; i
>= 0; i
-- )
8064 int rowHeight
= GetRowHeight(i
);
8065 if ( h
+ rowHeight
> ch
)
8072 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8073 // have rounding errors (this is important, because if we do, we
8074 // might not scroll at all and some cells won't be redrawn)
8076 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8078 ypos
+= GRID_SCROLL_LINE_Y
;
8085 else if ( right
> cw
)
8087 // position the view so that the cell is on the right
8089 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8090 xpos
= x0
+ (right
- cw
);
8092 // see comment for ypos above
8093 xpos
+= GRID_SCROLL_LINE_X
;
8096 if ( xpos
!= -1 || ypos
!= -1 )
8099 xpos
/= GRID_SCROLL_LINE_X
;
8101 ypos
/= GRID_SCROLL_LINE_Y
;
8102 Scroll( xpos
, ypos
);
8110 // ------ Grid cursor movement functions
8113 bool wxGrid::MoveCursorUp( bool expandSelection
)
8115 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8116 m_currentCellCoords
.GetRow() >= 0 )
8118 if ( expandSelection
)
8120 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8121 m_selectingKeyboard
= m_currentCellCoords
;
8122 if ( m_selectingKeyboard
.GetRow() > 0 )
8124 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8125 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8126 m_selectingKeyboard
.GetCol() );
8127 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8130 else if ( m_currentCellCoords
.GetRow() > 0 )
8133 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8134 m_currentCellCoords
.GetCol() );
8135 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8136 m_currentCellCoords
.GetCol() );
8147 bool wxGrid::MoveCursorDown( bool expandSelection
)
8149 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8150 m_currentCellCoords
.GetRow() < m_numRows
)
8152 if ( expandSelection
)
8154 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8155 m_selectingKeyboard
= m_currentCellCoords
;
8156 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8158 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8159 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8160 m_selectingKeyboard
.GetCol() );
8161 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8164 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8167 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8168 m_currentCellCoords
.GetCol() );
8169 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8170 m_currentCellCoords
.GetCol() );
8181 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8183 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8184 m_currentCellCoords
.GetCol() >= 0 )
8186 if ( expandSelection
)
8188 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8189 m_selectingKeyboard
= m_currentCellCoords
;
8190 if ( m_selectingKeyboard
.GetCol() > 0 )
8192 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8193 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8194 m_selectingKeyboard
.GetCol() );
8195 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8198 else if ( m_currentCellCoords
.GetCol() > 0 )
8201 MakeCellVisible( m_currentCellCoords
.GetRow(),
8202 m_currentCellCoords
.GetCol() - 1 );
8203 SetCurrentCell( m_currentCellCoords
.GetRow(),
8204 m_currentCellCoords
.GetCol() - 1 );
8215 bool wxGrid::MoveCursorRight( bool expandSelection
)
8217 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8218 m_currentCellCoords
.GetCol() < m_numCols
)
8220 if ( expandSelection
)
8222 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8223 m_selectingKeyboard
= m_currentCellCoords
;
8224 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8226 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8227 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8228 m_selectingKeyboard
.GetCol() );
8229 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8232 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8235 MakeCellVisible( m_currentCellCoords
.GetRow(),
8236 m_currentCellCoords
.GetCol() + 1 );
8237 SetCurrentCell( m_currentCellCoords
.GetRow(),
8238 m_currentCellCoords
.GetCol() + 1 );
8249 bool wxGrid::MovePageUp()
8251 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8253 int row
= m_currentCellCoords
.GetRow();
8257 m_gridWin
->GetClientSize( &cw
, &ch
);
8259 int y
= GetRowTop(row
);
8260 int newRow
= internalYToRow( y
- ch
+ 1 );
8262 if ( newRow
== row
)
8264 //row > 0 , so newrow can never be less than 0 here.
8268 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8269 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8277 bool wxGrid::MovePageDown()
8279 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8281 int row
= m_currentCellCoords
.GetRow();
8282 if ( (row
+1) < m_numRows
)
8285 m_gridWin
->GetClientSize( &cw
, &ch
);
8287 int y
= GetRowTop(row
);
8288 int newRow
= internalYToRow( y
+ ch
);
8289 if ( newRow
== row
)
8291 // row < m_numRows , so newrow can't overflow here.
8295 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8296 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8304 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8307 m_currentCellCoords
!= wxGridNoCellCoords
&&
8308 m_currentCellCoords
.GetRow() > 0 )
8310 int row
= m_currentCellCoords
.GetRow();
8311 int col
= m_currentCellCoords
.GetCol();
8313 if ( m_table
->IsEmptyCell(row
, col
) )
8315 // starting in an empty cell: find the next block of
8321 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8324 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8326 // starting at the top of a block: find the next block
8332 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8337 // starting within a block: find the top of the block
8342 if ( m_table
->IsEmptyCell(row
, col
) )
8350 MakeCellVisible( row
, col
);
8351 if ( expandSelection
)
8353 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8354 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8359 SetCurrentCell( row
, col
);
8367 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8370 m_currentCellCoords
!= wxGridNoCellCoords
&&
8371 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8373 int row
= m_currentCellCoords
.GetRow();
8374 int col
= m_currentCellCoords
.GetCol();
8376 if ( m_table
->IsEmptyCell(row
, col
) )
8378 // starting in an empty cell: find the next block of
8381 while ( row
< m_numRows
-1 )
8384 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8387 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8389 // starting at the bottom of a block: find the next block
8392 while ( row
< m_numRows
-1 )
8395 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8400 // starting within a block: find the bottom of the block
8402 while ( row
< m_numRows
-1 )
8405 if ( m_table
->IsEmptyCell(row
, col
) )
8413 MakeCellVisible( row
, col
);
8414 if ( expandSelection
)
8416 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8417 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8422 SetCurrentCell( row
, col
);
8431 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8434 m_currentCellCoords
!= wxGridNoCellCoords
&&
8435 m_currentCellCoords
.GetCol() > 0 )
8437 int row
= m_currentCellCoords
.GetRow();
8438 int col
= m_currentCellCoords
.GetCol();
8440 if ( m_table
->IsEmptyCell(row
, col
) )
8442 // starting in an empty cell: find the next block of
8448 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8451 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8453 // starting at the left of a block: find the next block
8459 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8464 // starting within a block: find the left of the block
8469 if ( m_table
->IsEmptyCell(row
, col
) )
8477 MakeCellVisible( row
, col
);
8478 if ( expandSelection
)
8480 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8481 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8486 SetCurrentCell( row
, col
);
8495 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8498 m_currentCellCoords
!= wxGridNoCellCoords
&&
8499 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8501 int row
= m_currentCellCoords
.GetRow();
8502 int col
= m_currentCellCoords
.GetCol();
8504 if ( m_table
->IsEmptyCell(row
, col
) )
8506 // starting in an empty cell: find the next block of
8509 while ( col
< m_numCols
-1 )
8512 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8515 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8517 // starting at the right of a block: find the next block
8520 while ( col
< m_numCols
-1 )
8523 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8528 // starting within a block: find the right of the block
8530 while ( col
< m_numCols
-1 )
8533 if ( m_table
->IsEmptyCell(row
, col
) )
8541 MakeCellVisible( row
, col
);
8542 if ( expandSelection
)
8544 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8545 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8550 SetCurrentCell( row
, col
);
8562 // ------ Label values and formatting
8565 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8567 *horiz
= m_rowLabelHorizAlign
;
8568 *vert
= m_rowLabelVertAlign
;
8571 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8573 *horiz
= m_colLabelHorizAlign
;
8574 *vert
= m_colLabelVertAlign
;
8577 int wxGrid::GetColLabelTextOrientation()
8579 return m_colLabelTextOrientation
;
8582 wxString
wxGrid::GetRowLabelValue( int row
)
8586 return m_table
->GetRowLabelValue( row
);
8596 wxString
wxGrid::GetColLabelValue( int col
)
8600 return m_table
->GetColLabelValue( col
);
8611 void wxGrid::SetRowLabelSize( int width
)
8613 width
= wxMax( width
, 0 );
8614 if ( width
!= m_rowLabelWidth
)
8618 m_rowLabelWin
->Show( false );
8619 m_cornerLabelWin
->Show( false );
8621 else if ( m_rowLabelWidth
== 0 )
8623 m_rowLabelWin
->Show( true );
8624 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8627 m_rowLabelWidth
= width
;
8629 wxScrolledWindow::Refresh( true );
8634 void wxGrid::SetColLabelSize( int height
)
8636 height
= wxMax( height
, 0 );
8637 if ( height
!= m_colLabelHeight
)
8641 m_colLabelWin
->Show( false );
8642 m_cornerLabelWin
->Show( false );
8644 else if ( m_colLabelHeight
== 0 )
8646 m_colLabelWin
->Show( true );
8647 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8650 m_colLabelHeight
= height
;
8652 wxScrolledWindow::Refresh( true );
8657 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8659 if ( m_labelBackgroundColour
!= colour
)
8661 m_labelBackgroundColour
= colour
;
8662 m_rowLabelWin
->SetBackgroundColour( colour
);
8663 m_colLabelWin
->SetBackgroundColour( colour
);
8664 m_cornerLabelWin
->SetBackgroundColour( colour
);
8666 if ( !GetBatchCount() )
8668 m_rowLabelWin
->Refresh();
8669 m_colLabelWin
->Refresh();
8670 m_cornerLabelWin
->Refresh();
8675 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8677 if ( m_labelTextColour
!= colour
)
8679 m_labelTextColour
= colour
;
8680 if ( !GetBatchCount() )
8682 m_rowLabelWin
->Refresh();
8683 m_colLabelWin
->Refresh();
8688 void wxGrid::SetLabelFont( const wxFont
& font
)
8691 if ( !GetBatchCount() )
8693 m_rowLabelWin
->Refresh();
8694 m_colLabelWin
->Refresh();
8698 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8700 // allow old (incorrect) defs to be used
8703 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8704 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8705 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8710 case wxTOP
: vert
= wxALIGN_TOP
; break;
8711 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8712 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8715 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8717 m_rowLabelHorizAlign
= horiz
;
8720 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8722 m_rowLabelVertAlign
= vert
;
8725 if ( !GetBatchCount() )
8727 m_rowLabelWin
->Refresh();
8731 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8733 // allow old (incorrect) defs to be used
8736 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8737 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8738 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8743 case wxTOP
: vert
= wxALIGN_TOP
; break;
8744 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8745 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8748 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8750 m_colLabelHorizAlign
= horiz
;
8753 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8755 m_colLabelVertAlign
= vert
;
8758 if ( !GetBatchCount() )
8760 m_colLabelWin
->Refresh();
8764 // Note: under MSW, the default column label font must be changed because it
8765 // does not support vertical printing
8767 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8768 // pGrid->SetLabelFont(font);
8769 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8771 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8773 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8775 m_colLabelTextOrientation
= textOrientation
;
8778 if ( !GetBatchCount() )
8780 m_colLabelWin
->Refresh();
8784 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8788 m_table
->SetRowLabelValue( row
, s
);
8789 if ( !GetBatchCount() )
8791 wxRect rect
= CellToRect( row
, 0);
8792 if ( rect
.height
> 0 )
8794 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8796 rect
.width
= m_rowLabelWidth
;
8797 m_rowLabelWin
->Refresh( true, &rect
);
8803 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8807 m_table
->SetColLabelValue( col
, s
);
8808 if ( !GetBatchCount() )
8810 wxRect rect
= CellToRect( 0, col
);
8811 if ( rect
.width
> 0 )
8813 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8815 rect
.height
= m_colLabelHeight
;
8816 m_colLabelWin
->Refresh( true, &rect
);
8822 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8824 if ( m_gridLineColour
!= colour
)
8826 m_gridLineColour
= colour
;
8828 wxClientDC
dc( m_gridWin
);
8830 DrawAllGridLines( dc
, wxRegion() );
8835 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8837 if ( m_cellHighlightColour
!= colour
)
8839 m_cellHighlightColour
= colour
;
8841 wxClientDC
dc( m_gridWin
);
8843 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8844 DrawCellHighlight(dc
, attr
);
8849 void wxGrid::SetCellHighlightPenWidth(int width
)
8851 if (m_cellHighlightPenWidth
!= width
) {
8852 m_cellHighlightPenWidth
= width
;
8854 // Just redrawing the cell highlight is not enough since that won't
8855 // make any visible change if the the thickness is getting smaller.
8856 int row
= m_currentCellCoords
.GetRow();
8857 int col
= m_currentCellCoords
.GetCol();
8858 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8860 wxRect rect
= CellToRect(row
, col
);
8861 m_gridWin
->Refresh(true, &rect
);
8865 void wxGrid::SetCellHighlightROPenWidth(int width
)
8867 if (m_cellHighlightROPenWidth
!= width
) {
8868 m_cellHighlightROPenWidth
= width
;
8870 // Just redrawing the cell highlight is not enough since that won't
8871 // make any visible change if the the thickness is getting smaller.
8872 int row
= m_currentCellCoords
.GetRow();
8873 int col
= m_currentCellCoords
.GetCol();
8874 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8876 wxRect rect
= CellToRect(row
, col
);
8877 m_gridWin
->Refresh(true, &rect
);
8881 void wxGrid::EnableGridLines( bool enable
)
8883 if ( enable
!= m_gridLinesEnabled
)
8885 m_gridLinesEnabled
= enable
;
8887 if ( !GetBatchCount() )
8891 wxClientDC
dc( m_gridWin
);
8893 DrawAllGridLines( dc
, wxRegion() );
8897 m_gridWin
->Refresh();
8904 int wxGrid::GetDefaultRowSize()
8906 return m_defaultRowHeight
;
8909 int wxGrid::GetRowSize( int row
)
8911 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8913 return GetRowHeight(row
);
8916 int wxGrid::GetDefaultColSize()
8918 return m_defaultColWidth
;
8921 int wxGrid::GetColSize( int col
)
8923 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8925 return GetColWidth(col
);
8928 // ============================================================================
8929 // access to the grid attributes: each of them has a default value in the grid
8930 // itself and may be overidden on a per-cell basis
8931 // ============================================================================
8933 // ----------------------------------------------------------------------------
8934 // setting default attributes
8935 // ----------------------------------------------------------------------------
8937 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8939 m_defaultCellAttr
->SetBackgroundColour(col
);
8941 m_gridWin
->SetBackgroundColour(col
);
8945 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8947 m_defaultCellAttr
->SetTextColour(col
);
8950 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8952 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8955 void wxGrid::SetDefaultCellOverflow( bool allow
)
8957 m_defaultCellAttr
->SetOverflow(allow
);
8960 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8962 m_defaultCellAttr
->SetFont(font
);
8966 // For editors and renderers the type registry takes precedence over the
8967 // default attr, so we need to register the new editor/renderer for the string
8968 // data type in order to make setting a default editor/renderer appear to
8971 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8973 RegisterDataType(wxGRID_VALUE_STRING
,
8975 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8978 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8980 RegisterDataType(wxGRID_VALUE_STRING
,
8981 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8985 // ----------------------------------------------------------------------------
8986 // access to the default attrbiutes
8987 // ----------------------------------------------------------------------------
8989 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8991 return m_defaultCellAttr
->GetBackgroundColour();
8994 wxColour
wxGrid::GetDefaultCellTextColour()
8996 return m_defaultCellAttr
->GetTextColour();
8999 wxFont
wxGrid::GetDefaultCellFont()
9001 return m_defaultCellAttr
->GetFont();
9004 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9006 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9009 bool wxGrid::GetDefaultCellOverflow()
9011 return m_defaultCellAttr
->GetOverflow();
9014 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9016 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9019 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9021 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9024 // ----------------------------------------------------------------------------
9025 // access to cell attributes
9026 // ----------------------------------------------------------------------------
9028 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9030 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9031 wxColour colour
= attr
->GetBackgroundColour();
9036 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9038 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9039 wxColour colour
= attr
->GetTextColour();
9044 wxFont
wxGrid::GetCellFont( int row
, int col
)
9046 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9047 wxFont font
= attr
->GetFont();
9052 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9054 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9055 attr
->GetAlignment(horiz
, vert
);
9059 bool wxGrid::GetCellOverflow( int row
, int col
)
9061 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9062 bool allow
= attr
->GetOverflow();
9067 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9069 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9070 attr
->GetSize( num_rows
, num_cols
);
9074 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9076 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9077 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9083 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9085 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9086 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9092 bool wxGrid::IsReadOnly(int row
, int col
) const
9094 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9095 bool isReadOnly
= attr
->IsReadOnly();
9100 // ----------------------------------------------------------------------------
9101 // attribute support: cache, automatic provider creation, ...
9102 // ----------------------------------------------------------------------------
9104 bool wxGrid::CanHaveAttributes()
9111 return m_table
->CanHaveAttributes();
9114 void wxGrid::ClearAttrCache()
9116 if ( m_attrCache
.row
!= -1 )
9118 wxSafeDecRef(m_attrCache
.attr
);
9119 m_attrCache
.attr
= NULL
;
9120 m_attrCache
.row
= -1;
9124 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9128 wxGrid
*self
= (wxGrid
*)this; // const_cast
9130 self
->ClearAttrCache();
9131 self
->m_attrCache
.row
= row
;
9132 self
->m_attrCache
.col
= col
;
9133 self
->m_attrCache
.attr
= attr
;
9138 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9140 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9142 *attr
= m_attrCache
.attr
;
9143 wxSafeIncRef(m_attrCache
.attr
);
9145 #ifdef DEBUG_ATTR_CACHE
9146 gs_nAttrCacheHits
++;
9153 #ifdef DEBUG_ATTR_CACHE
9154 gs_nAttrCacheMisses
++;
9160 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9162 wxGridCellAttr
*attr
= NULL
;
9163 // Additional test to avoid looking at the cache e.g. for
9164 // wxNoCellCoords, as this will confuse memory management.
9167 if ( !LookupAttr(row
, col
, &attr
) )
9169 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9170 : (wxGridCellAttr
*)NULL
;
9171 CacheAttr(row
, col
, attr
);
9176 attr
->SetDefAttr(m_defaultCellAttr
);
9180 attr
= m_defaultCellAttr
;
9187 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9189 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9190 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9192 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9193 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9195 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9198 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9200 // artificially inc the ref count to match DecRef() in caller
9202 m_table
->SetAttr(attr
, row
, col
);
9208 // ----------------------------------------------------------------------------
9209 // setting column attributes (wrappers around SetColAttr)
9210 // ----------------------------------------------------------------------------
9212 void wxGrid::SetColFormatBool(int col
)
9214 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9217 void wxGrid::SetColFormatNumber(int col
)
9219 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9222 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9224 wxString typeName
= wxGRID_VALUE_FLOAT
;
9225 if ( (width
!= -1) || (precision
!= -1) )
9227 typeName
<< _T(':') << width
<< _T(',') << precision
;
9230 SetColFormatCustom(col
, typeName
);
9233 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9235 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9237 attr
= new wxGridCellAttr
;
9238 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9239 attr
->SetRenderer(renderer
);
9241 SetColAttr(col
, attr
);
9245 // ----------------------------------------------------------------------------
9246 // setting cell attributes: this is forwarded to the table
9247 // ----------------------------------------------------------------------------
9249 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9251 if ( CanHaveAttributes() )
9253 m_table
->SetAttr(attr
, row
, col
);
9262 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9264 if ( CanHaveAttributes() )
9266 m_table
->SetRowAttr(attr
, row
);
9275 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9277 if ( CanHaveAttributes() )
9279 m_table
->SetColAttr(attr
, col
);
9288 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9290 if ( CanHaveAttributes() )
9292 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9293 attr
->SetBackgroundColour(colour
);
9298 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9300 if ( CanHaveAttributes() )
9302 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9303 attr
->SetTextColour(colour
);
9308 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9310 if ( CanHaveAttributes() )
9312 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9313 attr
->SetFont(font
);
9318 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9320 if ( CanHaveAttributes() )
9322 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9323 attr
->SetAlignment(horiz
, vert
);
9328 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9330 if ( CanHaveAttributes() )
9332 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9333 attr
->SetOverflow(allow
);
9338 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9340 if ( CanHaveAttributes() )
9342 int cell_rows
, cell_cols
;
9344 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9345 attr
->GetSize(&cell_rows
, &cell_cols
);
9346 attr
->SetSize(num_rows
, num_cols
);
9349 // Cannot set the size of a cell to 0 or negative values
9350 // While it is perfectly legal to do that, this function cannot
9351 // handle all the possibilies, do it by hand by getting the CellAttr.
9352 // You can only set the size of a cell to 1,1 or greater with this fn
9353 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9354 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9355 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9356 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9358 // if this was already a multicell then "turn off" the other cells first
9359 if ((cell_rows
> 1) || (cell_rows
> 1))
9362 for (j
=row
; j
<row
+cell_rows
; j
++)
9364 for (i
=col
; i
<col
+cell_cols
; i
++)
9366 if ((i
!= col
) || (j
!= row
))
9368 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9369 attr_stub
->SetSize( 1, 1 );
9370 attr_stub
->DecRef();
9376 // mark the cells that will be covered by this cell to
9377 // negative or zero values to point back at this cell
9378 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9381 for (j
=row
; j
<row
+num_rows
; j
++)
9383 for (i
=col
; i
<col
+num_cols
; i
++)
9385 if ((i
!= col
) || (j
!= row
))
9387 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9388 attr_stub
->SetSize( row
-j
, col
-i
);
9389 attr_stub
->DecRef();
9397 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9399 if ( CanHaveAttributes() )
9401 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9402 attr
->SetRenderer(renderer
);
9407 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9409 if ( CanHaveAttributes() )
9411 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9412 attr
->SetEditor(editor
);
9417 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9419 if ( CanHaveAttributes() )
9421 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9422 attr
->SetReadOnly(isReadOnly
);
9427 // ----------------------------------------------------------------------------
9428 // Data type registration
9429 // ----------------------------------------------------------------------------
9431 void wxGrid::RegisterDataType(const wxString
& typeName
,
9432 wxGridCellRenderer
* renderer
,
9433 wxGridCellEditor
* editor
)
9435 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9439 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9441 wxString typeName
= m_table
->GetTypeName(row
, col
);
9442 return GetDefaultEditorForType(typeName
);
9445 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9447 wxString typeName
= m_table
->GetTypeName(row
, col
);
9448 return GetDefaultRendererForType(typeName
);
9452 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9454 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9455 if ( index
== wxNOT_FOUND
)
9457 wxFAIL_MSG(wxT("Unknown data type name"));
9462 return m_typeRegistry
->GetEditor(index
);
9466 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9468 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9469 if ( index
== wxNOT_FOUND
)
9471 wxFAIL_MSG(wxT("Unknown data type name"));
9476 return m_typeRegistry
->GetRenderer(index
);
9480 // ----------------------------------------------------------------------------
9482 // ----------------------------------------------------------------------------
9484 void wxGrid::EnableDragRowSize( bool enable
)
9486 m_canDragRowSize
= enable
;
9490 void wxGrid::EnableDragColSize( bool enable
)
9492 m_canDragColSize
= enable
;
9495 void wxGrid::EnableDragGridSize( bool enable
)
9497 m_canDragGridSize
= enable
;
9500 void wxGrid::EnableDragCell( bool enable
)
9502 m_canDragCell
= enable
;
9505 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9507 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9509 if ( resizeExistingRows
)
9511 // since we are resizing all rows to the default row size,
9512 // we can simply clear the row heights and row bottoms
9513 // arrays (which also allows us to take advantage of
9514 // some speed optimisations)
9515 m_rowHeights
.Empty();
9516 m_rowBottoms
.Empty();
9517 if ( !GetBatchCount() )
9522 void wxGrid::SetRowSize( int row
, int height
)
9524 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9526 // See comment in SetColSize
9527 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9529 if ( m_rowHeights
.IsEmpty() )
9531 // need to really create the array
9535 int h
= wxMax( 0, height
);
9536 int diff
= h
- m_rowHeights
[row
];
9538 m_rowHeights
[row
] = h
;
9540 for ( i
= row
; i
< m_numRows
; i
++ )
9542 m_rowBottoms
[i
] += diff
;
9544 if ( !GetBatchCount() )
9548 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9550 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9552 if ( resizeExistingCols
)
9554 // since we are resizing all columns to the default column size,
9555 // we can simply clear the col widths and col rights
9556 // arrays (which also allows us to take advantage of
9557 // some speed optimisations)
9558 m_colWidths
.Empty();
9559 m_colRights
.Empty();
9560 if ( !GetBatchCount() )
9565 void wxGrid::SetColSize( int col
, int width
)
9567 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9569 // should we check that it's bigger than GetColMinimalWidth(col) here?
9571 // No, because it is reasonable to assume the library user know's
9572 // what he is doing. However whe should test against the weaker
9573 // constariant of minimalAcceptableWidth, as this breaks rendering
9575 // This test then fixes sf.net bug #645734
9577 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9579 if ( m_colWidths
.IsEmpty() )
9581 // need to really create the array
9585 // if < 0 calc new width from label
9589 wxArrayString lines
;
9590 wxClientDC
dc(m_colLabelWin
);
9591 dc
.SetFont(GetLabelFont());
9592 StringToLines(GetColLabelValue(col
), lines
);
9593 GetTextBoxSize(dc
, lines
, &w
, &h
);
9596 int w
= wxMax( 0, width
);
9597 int diff
= w
- m_colWidths
[col
];
9598 m_colWidths
[col
] = w
;
9601 for ( i
= col
; i
< m_numCols
; i
++ )
9603 m_colRights
[i
] += diff
;
9605 if ( !GetBatchCount() )
9610 void wxGrid::SetColMinimalWidth( int col
, int width
)
9612 if (width
> GetColMinimalAcceptableWidth()) {
9613 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9614 m_colMinWidths
[key
] = width
;
9618 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9620 if (width
> GetRowMinimalAcceptableHeight()) {
9621 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9622 m_rowMinHeights
[key
] = width
;
9626 int wxGrid::GetColMinimalWidth(int col
) const
9628 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9629 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9630 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9633 int wxGrid::GetRowMinimalHeight(int row
) const
9635 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9636 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9637 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9640 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9642 // We do allow a width of 0 since this gives us
9643 // an easy way to temporarily hidding columns.
9646 m_minAcceptableColWidth
= width
;
9649 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9651 // We do allow a height of 0 since this gives us
9652 // an easy way to temporarily hidding rows.
9655 m_minAcceptableRowHeight
= height
;
9658 int wxGrid::GetColMinimalAcceptableWidth() const
9660 return m_minAcceptableColWidth
;
9663 int wxGrid::GetRowMinimalAcceptableHeight() const
9665 return m_minAcceptableRowHeight
;
9668 // ----------------------------------------------------------------------------
9670 // ----------------------------------------------------------------------------
9672 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9674 wxClientDC
dc(m_gridWin
);
9676 //Cancel editting of cell
9677 HideCellEditControl();
9678 SaveEditControlValue();
9680 // init both of them to avoid compiler warnings, even if weo nly need one
9688 wxCoord extent
, extentMax
= 0;
9689 int max
= column
? m_numRows
: m_numCols
;
9690 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9697 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9698 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9701 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9702 extent
= column
? size
.x
: size
.y
;
9703 if ( extent
> extentMax
)
9714 // now also compare with the column label extent
9716 dc
.SetFont( GetLabelFont() );
9720 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9721 if( GetColLabelTextOrientation() == wxVERTICAL
)
9725 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9727 extent
= column
? w
: h
;
9728 if ( extent
> extentMax
)
9735 // empty column - give default extent (notice that if extentMax is less
9736 // than default extent but != 0, it's ok)
9737 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9743 // leave some space around text
9754 SetColSize(col
, extentMax
);
9755 if ( !GetBatchCount() )
9758 m_gridWin
->GetClientSize( &cw
, &ch
);
9759 wxRect
rect ( CellToRect( 0, col
) );
9761 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9762 rect
.width
= cw
- rect
.x
;
9763 rect
.height
= m_colLabelHeight
;
9764 m_colLabelWin
->Refresh( true, &rect
);
9769 SetRowSize(row
, extentMax
);
9770 if ( !GetBatchCount() )
9773 m_gridWin
->GetClientSize( &cw
, &ch
);
9774 wxRect
rect ( CellToRect( row
, 0 ) );
9776 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9777 rect
.width
= m_rowLabelWidth
;
9778 rect
.height
= ch
- rect
.y
;
9779 m_rowLabelWin
->Refresh( true, &rect
);
9785 SetColMinimalWidth(col
, extentMax
);
9787 SetRowMinimalHeight(row
, extentMax
);
9791 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9793 int width
= m_rowLabelWidth
;
9798 for ( int col
= 0; col
< m_numCols
; col
++ )
9802 AutoSizeColumn(col
, setAsMin
);
9805 width
+= GetColWidth(col
);
9814 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9816 int height
= m_colLabelHeight
;
9821 for ( int row
= 0; row
< m_numRows
; row
++ )
9825 AutoSizeRow(row
, setAsMin
);
9828 height
+= GetRowHeight(row
);
9837 void wxGrid::AutoSize()
9841 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9843 // round up the size to a multiple of scroll step - this ensures that we
9844 // won't get the scrollbars if we're sized exactly to this width
9845 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9847 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9848 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9850 // distribute the extra space between the columns/rows to avoid having
9851 // extra white space
9853 // Remove the extra m_extraWidth + 1 added above
9854 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9855 if ( diff
&& m_numCols
)
9857 // try to resize the columns uniformly
9858 wxCoord diffPerCol
= diff
/ m_numCols
;
9861 for ( int col
= 0; col
< m_numCols
; col
++ )
9863 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9867 // add remaining amount to the last columns
9868 diff
-= diffPerCol
* m_numCols
;
9871 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9873 SetColSize(col
, GetColWidth(col
) + 1);
9879 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9880 if ( diff
&& m_numRows
)
9882 // try to resize the columns uniformly
9883 wxCoord diffPerRow
= diff
/ m_numRows
;
9886 for ( int row
= 0; row
< m_numRows
; row
++ )
9888 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9892 // add remaining amount to the last rows
9893 diff
-= diffPerRow
* m_numRows
;
9896 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9898 SetRowSize(row
, GetRowHeight(row
) + 1);
9905 SetClientSize(sizeFit
);
9908 void wxGrid::AutoSizeRowLabelSize( int row
)
9910 wxArrayString lines
;
9913 // Hide the edit control, so it
9914 // won't interfer with drag-shrinking.
9915 if( IsCellEditControlShown() )
9917 HideCellEditControl();
9918 SaveEditControlValue();
9921 // autosize row height depending on label text
9922 StringToLines( GetRowLabelValue( row
), lines
);
9923 wxClientDC
dc( m_rowLabelWin
);
9924 GetTextBoxSize( dc
, lines
, &w
, &h
);
9925 if( h
< m_defaultRowHeight
)
9926 h
= m_defaultRowHeight
;
9931 void wxGrid::AutoSizeColLabelSize( int col
)
9933 wxArrayString lines
;
9936 // Hide the edit control, so it
9937 // won't interfer with drag-shrinking.
9938 if( IsCellEditControlShown() )
9940 HideCellEditControl();
9941 SaveEditControlValue();
9944 // autosize column width depending on label text
9945 StringToLines( GetColLabelValue( col
), lines
);
9946 wxClientDC
dc( m_colLabelWin
);
9947 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9948 GetTextBoxSize( dc
, lines
, &w
, &h
);
9950 GetTextBoxSize( dc
, lines
, &h
, &w
);
9951 if( w
< m_defaultColWidth
)
9952 w
= m_defaultColWidth
;
9957 wxSize
wxGrid::DoGetBestSize() const
9959 // don't set sizes, only calculate them
9960 wxGrid
*self
= (wxGrid
*)this; // const_cast
9963 width
= self
->SetOrCalcColumnSizes(true);
9964 height
= self
->SetOrCalcRowSizes(true);
9966 if (!width
) width
=100;
9967 if (!height
) height
=80;
9969 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9970 // of the scrollbars, is there any magic incantaion for that?
9972 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9974 width
+= 1 + xpu
- (width
% xpu
);
9976 height
+= 1 + ypu
- (height
% ypu
);
9978 // limit to 1/4 of the screen size
9979 int maxwidth
, maxheight
;
9980 wxDisplaySize( & maxwidth
, & maxheight
);
9983 if ( width
> maxwidth
) width
= maxwidth
;
9984 if ( height
> maxheight
) height
= maxheight
;
9987 wxSize
best(width
, height
);
9988 // NOTE: This size should be cached, but first we need to add calls to
9989 // InvalidateBestSize everywhere that could change the results of this
9991 // CacheBestSize(size);
10001 wxPen
& wxGrid::GetDividerPen() const
10006 // ----------------------------------------------------------------------------
10007 // cell value accessor functions
10008 // ----------------------------------------------------------------------------
10010 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10014 m_table
->SetValue( row
, col
, s
);
10015 if ( !GetBatchCount() )
10018 wxRect
rect( CellToRect( row
, col
) );
10020 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10021 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10022 m_gridWin
->Refresh( false, &rect
);
10025 if ( m_currentCellCoords
.GetRow() == row
&&
10026 m_currentCellCoords
.GetCol() == col
&&
10027 IsCellEditControlShown())
10028 // Note: If we are using IsCellEditControlEnabled,
10029 // this interacts badly with calling SetCellValue from
10030 // an EVT_GRID_CELL_CHANGE handler.
10032 HideCellEditControl();
10033 ShowCellEditControl(); // will reread data from table
10040 // ------ Block, row and col selection
10043 void wxGrid::SelectRow( int row
, bool addToSelected
)
10045 if ( IsSelection() && !addToSelected
)
10049 m_selection
->SelectRow( row
, false, addToSelected
);
10053 void wxGrid::SelectCol( int col
, bool addToSelected
)
10055 if ( IsSelection() && !addToSelected
)
10059 m_selection
->SelectCol( col
, false, addToSelected
);
10063 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10064 bool addToSelected
)
10066 if ( IsSelection() && !addToSelected
)
10070 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10071 false, addToSelected
);
10075 void wxGrid::SelectAll()
10077 if ( m_numRows
> 0 && m_numCols
> 0 )
10080 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10085 // ------ Cell, row and col deselection
10088 void wxGrid::DeselectRow( int row
)
10090 if ( !m_selection
)
10093 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10095 if ( m_selection
->IsInSelection(row
, 0 ) )
10096 m_selection
->ToggleCellSelection( row
, 0);
10100 int nCols
= GetNumberCols();
10101 for ( int i
= 0; i
< nCols
; i
++ )
10103 if ( m_selection
->IsInSelection(row
, i
) )
10104 m_selection
->ToggleCellSelection( row
, i
);
10109 void wxGrid::DeselectCol( int col
)
10111 if ( !m_selection
)
10114 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10116 if ( m_selection
->IsInSelection(0, col
) )
10117 m_selection
->ToggleCellSelection( 0, col
);
10121 int nRows
= GetNumberRows();
10122 for ( int i
= 0; i
< nRows
; i
++ )
10124 if ( m_selection
->IsInSelection(i
, col
) )
10125 m_selection
->ToggleCellSelection(i
, col
);
10130 void wxGrid::DeselectCell( int row
, int col
)
10132 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10133 m_selection
->ToggleCellSelection(row
, col
);
10136 bool wxGrid::IsSelection()
10138 return ( m_selection
&& (m_selection
->IsSelection() ||
10139 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10140 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10143 bool wxGrid::IsInSelection( int row
, int col
) const
10145 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10146 ( row
>= m_selectingTopLeft
.GetRow() &&
10147 col
>= m_selectingTopLeft
.GetCol() &&
10148 row
<= m_selectingBottomRight
.GetRow() &&
10149 col
<= m_selectingBottomRight
.GetCol() )) );
10152 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10154 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10155 return m_selection
->m_cellSelection
;
10157 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10159 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10160 return m_selection
->m_blockSelectionTopLeft
;
10162 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10164 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10165 return m_selection
->m_blockSelectionBottomRight
;
10167 wxArrayInt
wxGrid::GetSelectedRows() const
10169 if (!m_selection
) { wxArrayInt a
; return a
; }
10170 return m_selection
->m_rowSelection
;
10172 wxArrayInt
wxGrid::GetSelectedCols() const
10174 if (!m_selection
) { wxArrayInt a
; return a
; }
10175 return m_selection
->m_colSelection
;
10179 void wxGrid::ClearSelection()
10181 m_selectingTopLeft
= wxGridNoCellCoords
;
10182 m_selectingBottomRight
= wxGridNoCellCoords
;
10184 m_selection
->ClearSelection();
10188 // This function returns the rectangle that encloses the given block
10189 // in device coords clipped to the client size of the grid window.
10191 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10192 const wxGridCellCoords
&bottomRight
)
10194 wxRect
rect( wxGridNoCellRect
);
10197 cellRect
= CellToRect( topLeft
);
10198 if ( cellRect
!= wxGridNoCellRect
)
10204 rect
= wxRect(0,0,0,0);
10207 cellRect
= CellToRect( bottomRight
);
10208 if ( cellRect
!= wxGridNoCellRect
)
10214 return wxGridNoCellRect
;
10218 int left
= rect
.GetLeft();
10219 int top
= rect
.GetTop();
10220 int right
= rect
.GetRight();
10221 int bottom
= rect
.GetBottom();
10223 int leftCol
= topLeft
.GetCol();
10224 int topRow
= topLeft
.GetRow();
10225 int rightCol
= bottomRight
.GetCol();
10226 int bottomRow
= bottomRight
.GetRow();
10244 topRow
= bottomRow
;
10249 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10251 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10253 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10255 cellRect
= CellToRect( j
, i
);
10257 if (cellRect
.x
< left
)
10259 if (cellRect
.y
< top
)
10261 if (cellRect
.x
+ cellRect
.width
> right
)
10262 right
= cellRect
.x
+ cellRect
.width
;
10263 if (cellRect
.y
+ cellRect
.height
> bottom
)
10264 bottom
= cellRect
.y
+ cellRect
.height
;
10266 else i
= rightCol
; // jump over inner cells.
10270 // convert to scrolled coords
10272 CalcScrolledPosition( left
, top
, &left
, &top
);
10273 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10276 m_gridWin
->GetClientSize( &cw
, &ch
);
10278 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10279 return wxRect(0,0,0,0);
10281 rect
.SetLeft( wxMax(0, left
) );
10282 rect
.SetTop( wxMax(0, top
) );
10283 rect
.SetRight( wxMin(cw
, right
) );
10284 rect
.SetBottom( wxMin(ch
, bottom
) );
10290 // ------ Grid event classes
10293 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10295 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10296 int row
, int col
, int x
, int y
, bool sel
,
10297 bool control
, bool shift
, bool alt
, bool meta
)
10298 : wxNotifyEvent( type
, id
)
10305 m_control
= control
;
10310 SetEventObject(obj
);
10314 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10316 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10317 int rowOrCol
, int x
, int y
,
10318 bool control
, bool shift
, bool alt
, bool meta
)
10319 : wxNotifyEvent( type
, id
)
10321 m_rowOrCol
= rowOrCol
;
10324 m_control
= control
;
10329 SetEventObject(obj
);
10333 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10335 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10336 const wxGridCellCoords
& topLeft
,
10337 const wxGridCellCoords
& bottomRight
,
10338 bool sel
, bool control
,
10339 bool shift
, bool alt
, bool meta
)
10340 : wxNotifyEvent( type
, id
)
10342 m_topLeft
= topLeft
;
10343 m_bottomRight
= bottomRight
;
10345 m_control
= control
;
10350 SetEventObject(obj
);
10354 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10356 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10357 wxObject
* obj
, int row
,
10358 int col
, wxControl
* ctrl
)
10359 : wxCommandEvent(type
, id
)
10361 SetEventObject(obj
);
10367 #endif // wxUSE_GRID