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
);
1096 bool is_decimal_point
= ( strbuf
==
1097 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1099 bool is_decimal_point
= ( strbuf
== _T(".") );
1101 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1102 || is_decimal_point
)
1104 wxGridCellTextEditor::StartingKey(event
);
1106 // skip Skip() below
1113 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1124 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1128 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1130 m_precision
= (int)tmp
;
1132 // skip the error message below
1137 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1141 wxString
wxGridCellFloatEditor::GetString() const
1144 if ( m_width
== -1 )
1146 // default width/precision
1149 else if ( m_precision
== -1 )
1151 // default precision
1152 fmt
.Printf(_T("%%%d.f"), m_width
);
1156 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1159 return wxString::Format(fmt
, m_valueOld
);
1162 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1164 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1166 int keycode
= event
.GetKeyCode();
1167 printf("%d\n", keycode
);
1168 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1170 tmpbuf
[0] = (char) keycode
;
1172 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1174 bool is_decimal_point
=
1175 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1176 wxLOCALE_CAT_NUMBER
) );
1178 bool is_decimal_point
= ( strbuf
== _T(".") );
1180 if ( (keycode
< 128) &&
1181 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1182 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1189 #endif // wxUSE_TEXTCTRL
1193 // ----------------------------------------------------------------------------
1194 // wxGridCellBoolEditor
1195 // ----------------------------------------------------------------------------
1197 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1199 wxEvtHandler
* evtHandler
)
1201 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1202 wxDefaultPosition
, wxDefaultSize
,
1205 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1208 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1210 bool resize
= false;
1211 wxSize size
= m_control
->GetSize();
1212 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1214 // check if the checkbox is not too big/small for this cell
1215 wxSize sizeBest
= m_control
->GetBestSize();
1216 if ( !(size
== sizeBest
) )
1218 // reset to default size if it had been made smaller
1224 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1226 // leave 1 pixel margin
1227 size
.x
= size
.y
= minSize
- 2;
1234 m_control
->SetSize(size
);
1237 // position it in the centre of the rectangle (TODO: support alignment?)
1239 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1240 // the checkbox without label still has some space to the right in wxGTK,
1241 // so shift it to the right
1243 #elif defined(__WXMSW__)
1244 // here too, but in other way
1249 int hAlign
= wxALIGN_CENTRE
;
1250 int vAlign
= wxALIGN_CENTRE
;
1252 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1255 if (hAlign
== wxALIGN_LEFT
)
1261 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1263 else if (hAlign
== wxALIGN_RIGHT
)
1265 x
= r
.x
+ r
.width
- size
.x
- 2;
1266 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1268 else if (hAlign
== wxALIGN_CENTRE
)
1270 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1271 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1274 m_control
->Move(x
, y
);
1277 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1279 m_control
->Show(show
);
1283 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1284 CBox()->SetBackgroundColour(colBg
);
1288 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1290 wxASSERT_MSG(m_control
,
1291 wxT("The wxGridCellEditor must be Created first!"));
1293 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1294 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1297 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1298 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1300 CBox()->SetValue(m_startValue
);
1304 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1307 wxASSERT_MSG(m_control
,
1308 wxT("The wxGridCellEditor must be Created first!"));
1310 bool changed
= false;
1311 bool value
= CBox()->GetValue();
1312 if ( value
!= m_startValue
)
1317 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1318 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1320 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1326 void wxGridCellBoolEditor::Reset()
1328 wxASSERT_MSG(m_control
,
1329 wxT("The wxGridCellEditor must be Created first!"));
1331 CBox()->SetValue(m_startValue
);
1334 void wxGridCellBoolEditor::StartingClick()
1336 CBox()->SetValue(!CBox()->GetValue());
1339 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1341 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1343 int keycode
= event
.GetKeyCode();
1356 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1358 int keycode
= event
.GetKeyCode();
1362 CBox()->SetValue(!CBox()->GetValue());
1366 CBox()->SetValue(true);
1370 CBox()->SetValue(false);
1376 // return the value as "1" for true and the empty string for false
1377 wxString
wxGridCellBoolEditor::GetValue() const
1379 bool bSet
= CBox()->GetValue();
1380 return bSet
? _T("1") : wxEmptyString
;
1383 #endif // wxUSE_CHECKBOX
1387 // ----------------------------------------------------------------------------
1388 // wxGridCellChoiceEditor
1389 // ----------------------------------------------------------------------------
1391 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1393 : m_choices(choices
),
1394 m_allowOthers(allowOthers
) { }
1396 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1397 const wxString choices
[],
1399 : m_allowOthers(allowOthers
)
1403 m_choices
.Alloc(count
);
1404 for ( size_t n
= 0; n
< count
; n
++ )
1406 m_choices
.Add(choices
[n
]);
1411 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1413 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1414 editor
->m_allowOthers
= m_allowOthers
;
1415 editor
->m_choices
= m_choices
;
1420 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1422 wxEvtHandler
* evtHandler
)
1424 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1425 wxDefaultPosition
, wxDefaultSize
,
1427 m_allowOthers
? 0 : wxCB_READONLY
);
1429 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1432 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1433 wxGridCellAttr
* attr
)
1435 // as we fill the entire client area, don't do anything here to minimize
1438 // TODO: It doesn't actually fill the client area since the height of a
1439 // combo always defaults to the standard... Until someone has time to
1440 // figure out the right rectangle to paint, just do it the normal way...
1441 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1444 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1446 wxASSERT_MSG(m_control
,
1447 wxT("The wxGridCellEditor must be Created first!"));
1449 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1452 Combo()->SetValue(m_startValue
);
1455 // find the right position, or default to the first if not found
1456 int pos
= Combo()->FindString(m_startValue
);
1459 Combo()->SetSelection(pos
);
1461 Combo()->SetInsertionPointEnd();
1462 Combo()->SetFocus();
1465 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1468 wxString value
= Combo()->GetValue();
1469 if ( value
== m_startValue
)
1472 grid
->GetTable()->SetValue(row
, col
, value
);
1477 void wxGridCellChoiceEditor::Reset()
1479 Combo()->SetValue(m_startValue
);
1480 Combo()->SetInsertionPointEnd();
1483 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1493 wxStringTokenizer
tk(params
, _T(','));
1494 while ( tk
.HasMoreTokens() )
1496 m_choices
.Add(tk
.GetNextToken());
1500 // return the value in the text control
1501 wxString
wxGridCellChoiceEditor::GetValue() const
1503 return Combo()->GetValue();
1506 #endif // wxUSE_COMBOBOX
1508 // ----------------------------------------------------------------------------
1509 // wxGridCellEditorEvtHandler
1510 // ----------------------------------------------------------------------------
1512 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1514 switch ( event
.GetKeyCode() )
1518 m_grid
->DisableCellEditControl();
1522 m_grid
->GetEventHandler()->ProcessEvent( event
);
1526 case WXK_NUMPAD_ENTER
:
1527 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1528 m_editor
->HandleReturn(event
);
1536 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1538 switch ( event
.GetKeyCode() )
1543 case WXK_NUMPAD_ENTER
:
1551 // ----------------------------------------------------------------------------
1552 // wxGridCellWorker is an (almost) empty common base class for
1553 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1554 // ----------------------------------------------------------------------------
1556 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1561 wxGridCellWorker::~wxGridCellWorker()
1565 // ============================================================================
1567 // ============================================================================
1569 // ----------------------------------------------------------------------------
1570 // wxGridCellRenderer
1571 // ----------------------------------------------------------------------------
1573 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1574 wxGridCellAttr
& attr
,
1577 int WXUNUSED(row
), int WXUNUSED(col
),
1580 dc
.SetBackgroundMode( wxSOLID
);
1582 // grey out fields if the grid is disabled
1583 if( grid
.IsEnabled() )
1587 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1591 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1596 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1599 dc
.SetPen( *wxTRANSPARENT_PEN
);
1600 dc
.DrawRectangle(rect
);
1603 // ----------------------------------------------------------------------------
1604 // wxGridCellStringRenderer
1605 // ----------------------------------------------------------------------------
1607 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1608 wxGridCellAttr
& attr
,
1612 dc
.SetBackgroundMode( wxTRANSPARENT
);
1614 // TODO some special colours for attr.IsReadOnly() case?
1616 // different coloured text when the grid is disabled
1617 if( grid
.IsEnabled() )
1621 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1622 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1626 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1627 dc
.SetTextForeground( attr
.GetTextColour() );
1632 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1633 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1636 dc
.SetFont( attr
.GetFont() );
1639 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1641 const wxString
& text
)
1643 wxCoord x
= 0, y
= 0, max_x
= 0;
1644 dc
.SetFont(attr
.GetFont());
1645 wxStringTokenizer
tk(text
, _T('\n'));
1646 while ( tk
.HasMoreTokens() )
1648 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1649 max_x
= wxMax(max_x
, x
);
1652 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1654 return wxSize(max_x
, y
);
1657 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1658 wxGridCellAttr
& attr
,
1662 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1665 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1666 wxGridCellAttr
& attr
,
1668 const wxRect
& rectCell
,
1672 wxRect rect
= rectCell
;
1675 // erase only this cells background, overflow cells should have been erased
1676 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1679 attr
.GetAlignment(&hAlign
, &vAlign
);
1681 int overflowCols
= 0;
1683 if (attr
.GetOverflow())
1685 int cols
= grid
.GetNumberCols();
1686 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1687 int cell_rows
, cell_cols
;
1688 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1689 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1691 int i
, c_cols
, c_rows
;
1692 for (i
= col
+cell_cols
; i
< cols
; i
++)
1694 bool is_empty
= true;
1695 for (int j
=row
; j
<row
+cell_rows
; j
++)
1697 // check w/ anchor cell for multicell block
1698 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1699 if (c_rows
> 0) c_rows
= 0;
1700 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1707 rect
.width
+= grid
.GetColSize(i
);
1713 if (rect
.width
>= best_width
) break;
1715 overflowCols
= i
- col
- cell_cols
+ 1;
1716 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1719 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1721 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1723 clip
.x
+= rectCell
.width
;
1724 // draw each overflow cell individually
1725 int col_end
= col
+cell_cols
+overflowCols
;
1726 if (col_end
>= grid
.GetNumberCols())
1727 col_end
= grid
.GetNumberCols() - 1;
1728 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1730 clip
.width
= grid
.GetColSize(i
) - 1;
1731 dc
.DestroyClippingRegion();
1732 dc
.SetClippingRegion(clip
);
1734 SetTextColoursAndFont(grid
, attr
, dc
,
1735 grid
.IsInSelection(row
,i
));
1737 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1738 rect
, hAlign
, vAlign
);
1739 clip
.x
+= grid
.GetColSize(i
) - 1;
1745 dc
.DestroyClippingRegion();
1749 // now we only have to draw the text
1750 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1752 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1753 rect
, hAlign
, vAlign
);
1756 // ----------------------------------------------------------------------------
1757 // wxGridCellNumberRenderer
1758 // ----------------------------------------------------------------------------
1760 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1762 wxGridTableBase
*table
= grid
.GetTable();
1764 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1766 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1770 text
= table
->GetValue(row
, col
);
1776 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1777 wxGridCellAttr
& attr
,
1779 const wxRect
& rectCell
,
1783 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1785 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1787 // draw the text right aligned by default
1789 attr
.GetAlignment(&hAlign
, &vAlign
);
1790 hAlign
= wxALIGN_RIGHT
;
1792 wxRect rect
= rectCell
;
1795 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1798 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1799 wxGridCellAttr
& attr
,
1803 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1806 // ----------------------------------------------------------------------------
1807 // wxGridCellFloatRenderer
1808 // ----------------------------------------------------------------------------
1810 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1813 SetPrecision(precision
);
1816 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1818 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1819 renderer
->m_width
= m_width
;
1820 renderer
->m_precision
= m_precision
;
1821 renderer
->m_format
= m_format
;
1826 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1828 wxGridTableBase
*table
= grid
.GetTable();
1833 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1835 val
= table
->GetValueAsDouble(row
, col
);
1840 text
= table
->GetValue(row
, col
);
1841 hasDouble
= text
.ToDouble(&val
);
1848 if ( m_width
== -1 )
1850 if ( m_precision
== -1 )
1852 // default width/precision
1853 m_format
= _T("%f");
1857 m_format
.Printf(_T("%%.%df"), m_precision
);
1860 else if ( m_precision
== -1 )
1862 // default precision
1863 m_format
.Printf(_T("%%%d.f"), m_width
);
1867 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1871 text
.Printf(m_format
, val
);
1874 //else: text already contains the string
1879 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1880 wxGridCellAttr
& attr
,
1882 const wxRect
& rectCell
,
1886 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1888 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1890 // draw the text right aligned by default
1892 attr
.GetAlignment(&hAlign
, &vAlign
);
1893 hAlign
= wxALIGN_RIGHT
;
1895 wxRect rect
= rectCell
;
1898 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1901 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1902 wxGridCellAttr
& attr
,
1906 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1909 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1913 // reset to defaults
1919 wxString tmp
= params
.BeforeFirst(_T(','));
1923 if ( tmp
.ToLong(&width
) )
1925 SetWidth((int)width
);
1929 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1933 tmp
= params
.AfterFirst(_T(','));
1937 if ( tmp
.ToLong(&precision
) )
1939 SetPrecision((int)precision
);
1943 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1951 // ----------------------------------------------------------------------------
1952 // wxGridCellBoolRenderer
1953 // ----------------------------------------------------------------------------
1955 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1957 // FIXME these checkbox size calculations are really ugly...
1959 // between checkmark and box
1960 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1962 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1963 wxGridCellAttr
& WXUNUSED(attr
),
1968 // compute it only once (no locks for MT safeness in GUI thread...)
1969 if ( !ms_sizeCheckMark
.x
)
1971 // get checkbox size
1972 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1973 wxSize size
= checkbox
->GetBestSize();
1974 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1976 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1977 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1978 checkSize
-= size
.y
/ 2;
1983 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1986 return ms_sizeCheckMark
;
1989 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
1990 wxGridCellAttr
& attr
,
1996 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
1998 // draw a check mark in the centre (ignoring alignment - TODO)
1999 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2001 // don't draw outside the cell
2002 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2003 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2005 // and even leave (at least) 1 pixel margin
2006 size
.x
= size
.y
= minSize
- 2;
2009 // draw a border around checkmark
2011 attr
.GetAlignment(& hAlign
, &vAlign
);
2014 if (hAlign
== wxALIGN_CENTRE
)
2016 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2017 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2018 rectBorder
.width
= size
.x
;
2019 rectBorder
.height
= size
.y
;
2021 else if (hAlign
== wxALIGN_LEFT
)
2023 rectBorder
.x
= rect
.x
+ 2;
2024 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2025 rectBorder
.width
= size
.x
;
2026 rectBorder
.height
= size
.y
;
2028 else if (hAlign
== wxALIGN_RIGHT
)
2030 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2031 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2032 rectBorder
.width
= size
.x
;
2033 rectBorder
.height
= size
.y
;
2037 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2038 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2041 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2042 value
= !( !cellval
|| (cellval
== wxT("0")) );
2047 wxRect rectMark
= rectBorder
;
2049 // MSW DrawCheckMark() is weird (and should probably be changed...)
2050 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2054 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2057 dc
.SetTextForeground(attr
.GetTextColour());
2058 dc
.DrawCheckMark(rectMark
);
2061 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2062 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2063 dc
.DrawRectangle(rectBorder
);
2066 // ----------------------------------------------------------------------------
2068 // ----------------------------------------------------------------------------
2070 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2074 m_isReadOnly
= Unset
;
2079 m_attrkind
= wxGridCellAttr::Cell
;
2081 m_sizeRows
= m_sizeCols
= 1;
2082 m_overflow
= UnsetOverflow
;
2084 SetDefAttr(attrDefault
);
2087 wxGridCellAttr
*wxGridCellAttr::Clone() const
2089 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2091 if ( HasTextColour() )
2092 attr
->SetTextColour(GetTextColour());
2093 if ( HasBackgroundColour() )
2094 attr
->SetBackgroundColour(GetBackgroundColour());
2096 attr
->SetFont(GetFont());
2097 if ( HasAlignment() )
2098 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2100 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2104 attr
->SetRenderer(m_renderer
);
2105 m_renderer
->IncRef();
2109 attr
->SetEditor(m_editor
);
2114 attr
->SetReadOnly();
2116 attr
->SetKind( m_attrkind
);
2121 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2123 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2124 SetTextColour(mergefrom
->GetTextColour());
2125 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2126 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2127 if ( !HasFont() && mergefrom
->HasFont() )
2128 SetFont(mergefrom
->GetFont());
2129 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2131 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2132 SetAlignment(hAlign
, vAlign
);
2135 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2137 // Directly access member functions as GetRender/Editor don't just return
2138 // m_renderer/m_editor
2140 // Maybe add support for merge of Render and Editor?
2141 if (!HasRenderer() && mergefrom
->HasRenderer() )
2143 m_renderer
= mergefrom
->m_renderer
;
2144 m_renderer
->IncRef();
2146 if ( !HasEditor() && mergefrom
->HasEditor() )
2148 m_editor
= mergefrom
->m_editor
;
2151 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2152 SetReadOnly(mergefrom
->IsReadOnly());
2154 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2155 SetOverflow(mergefrom
->GetOverflow());
2157 SetDefAttr(mergefrom
->m_defGridAttr
);
2160 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2162 // The size of a cell is normally 1,1
2164 // If this cell is larger (2,2) then this is the top left cell
2165 // the other cells that will be covered (lower right cells) must be
2166 // set to negative or zero values such that
2167 // row + num_rows of the covered cell points to the larger cell (this cell)
2168 // same goes for the col + num_cols.
2170 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2172 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2173 !((num_rows
<=0)&&(num_cols
>0)) ||
2174 !((num_rows
==0)&&(num_cols
==0))),
2175 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2177 m_sizeRows
= num_rows
;
2178 m_sizeCols
= num_cols
;
2181 const wxColour
& wxGridCellAttr::GetTextColour() const
2183 if (HasTextColour())
2187 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2189 return m_defGridAttr
->GetTextColour();
2193 wxFAIL_MSG(wxT("Missing default cell attribute"));
2194 return wxNullColour
;
2199 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2201 if (HasBackgroundColour())
2203 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2204 return m_defGridAttr
->GetBackgroundColour();
2207 wxFAIL_MSG(wxT("Missing default cell attribute"));
2208 return wxNullColour
;
2213 const wxFont
& wxGridCellAttr::GetFont() const
2217 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2218 return m_defGridAttr
->GetFont();
2221 wxFAIL_MSG(wxT("Missing default cell attribute"));
2227 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2231 if ( hAlign
) *hAlign
= m_hAlign
;
2232 if ( vAlign
) *vAlign
= m_vAlign
;
2234 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2235 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2238 wxFAIL_MSG(wxT("Missing default cell attribute"));
2242 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2244 if ( num_rows
) *num_rows
= m_sizeRows
;
2245 if ( num_cols
) *num_cols
= m_sizeCols
;
2248 // GetRenderer and GetEditor use a slightly different decision path about
2249 // which attribute to use. If a non-default attr object has one then it is
2250 // used, otherwise the default editor or renderer is fetched from the grid and
2251 // used. It should be the default for the data type of the cell. If it is
2252 // NULL (because the table has a type that the grid does not have in its
2253 // registry,) then the grid's default editor or renderer is used.
2255 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2257 wxGridCellRenderer
*renderer
;
2259 if ( m_renderer
&& this != m_defGridAttr
)
2261 // use the cells renderer if it has one
2262 renderer
= m_renderer
;
2265 else // no non default cell renderer
2267 // get default renderer for the data type
2270 // GetDefaultRendererForCell() will do IncRef() for us
2271 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2280 if (m_defGridAttr
&& this != m_defGridAttr
)
2282 // if we still don't have one then use the grid default
2283 // (no need for IncRef() here neither)
2284 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2286 else // default grid attr
2288 // use m_renderer which we had decided not to use initially
2289 renderer
= m_renderer
;
2296 // we're supposed to always find something
2297 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2302 // same as above, except for s/renderer/editor/g
2303 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2305 wxGridCellEditor
*editor
;
2307 if ( m_editor
&& this != m_defGridAttr
)
2309 // use the cells editor if it has one
2313 else // no non default cell editor
2315 // get default editor for the data type
2318 // GetDefaultEditorForCell() will do IncRef() for us
2319 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2328 if ( m_defGridAttr
&& this != m_defGridAttr
)
2330 // if we still don't have one then use the grid default
2331 // (no need for IncRef() here neither)
2332 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2334 else // default grid attr
2336 // use m_editor which we had decided not to use initially
2344 // we're supposed to always find something
2345 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2350 // ----------------------------------------------------------------------------
2351 // wxGridCellAttrData
2352 // ----------------------------------------------------------------------------
2354 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2356 int n
= FindIndex(row
, col
);
2357 if ( n
== wxNOT_FOUND
)
2359 // add the attribute
2360 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2364 // free the old attribute
2365 m_attrs
[(size_t)n
].attr
->DecRef();
2369 // change the attribute
2370 m_attrs
[(size_t)n
].attr
= attr
;
2374 // remove this attribute
2375 m_attrs
.RemoveAt((size_t)n
);
2380 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2382 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2384 int n
= FindIndex(row
, col
);
2385 if ( n
!= wxNOT_FOUND
)
2387 attr
= m_attrs
[(size_t)n
].attr
;
2394 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2396 size_t count
= m_attrs
.GetCount();
2397 for ( size_t n
= 0; n
< count
; n
++ )
2399 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2400 wxCoord row
= coords
.GetRow();
2401 if ((size_t)row
>= pos
)
2405 // If rows inserted, include row counter where necessary
2406 coords
.SetRow(row
+ numRows
);
2408 else if (numRows
< 0)
2410 // If rows deleted ...
2411 if ((size_t)row
>= pos
- numRows
)
2413 // ...either decrement row counter (if row still exists)...
2414 coords
.SetRow(row
+ numRows
);
2418 // ...or remove the attribute
2419 // No need to DecRef the attribute itself since this is
2420 // done be wxGridCellWithAttr's destructor!
2421 m_attrs
.RemoveAt(n
);
2429 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2431 size_t count
= m_attrs
.GetCount();
2432 for ( size_t n
= 0; n
< count
; n
++ )
2434 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2435 wxCoord col
= coords
.GetCol();
2436 if ( (size_t)col
>= pos
)
2440 // If rows inserted, include row counter where necessary
2441 coords
.SetCol(col
+ numCols
);
2443 else if (numCols
< 0)
2445 // If rows deleted ...
2446 if ((size_t)col
>= pos
- numCols
)
2448 // ...either decrement row counter (if row still exists)...
2449 coords
.SetCol(col
+ numCols
);
2453 // ...or remove the attribute
2454 // No need to DecRef the attribute itself since this is
2455 // done be wxGridCellWithAttr's destructor!
2456 m_attrs
.RemoveAt(n
);
2464 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2466 size_t count
= m_attrs
.GetCount();
2467 for ( size_t n
= 0; n
< count
; n
++ )
2469 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2470 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2479 // ----------------------------------------------------------------------------
2480 // wxGridRowOrColAttrData
2481 // ----------------------------------------------------------------------------
2483 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2485 size_t count
= m_attrs
.Count();
2486 for ( size_t n
= 0; n
< count
; n
++ )
2488 m_attrs
[n
]->DecRef();
2492 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2494 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2496 int n
= m_rowsOrCols
.Index(rowOrCol
);
2497 if ( n
!= wxNOT_FOUND
)
2499 attr
= m_attrs
[(size_t)n
];
2506 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2508 int i
= m_rowsOrCols
.Index(rowOrCol
);
2509 if ( i
== wxNOT_FOUND
)
2511 // add the attribute
2512 m_rowsOrCols
.Add(rowOrCol
);
2517 size_t n
= (size_t)i
;
2520 // change the attribute
2521 m_attrs
[n
]->DecRef();
2526 // remove this attribute
2527 m_attrs
[n
]->DecRef();
2528 m_rowsOrCols
.RemoveAt(n
);
2529 m_attrs
.RemoveAt(n
);
2534 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2536 size_t count
= m_attrs
.GetCount();
2537 for ( size_t n
= 0; n
< count
; n
++ )
2539 int & rowOrCol
= m_rowsOrCols
[n
];
2540 if ( (size_t)rowOrCol
>= pos
)
2542 if ( numRowsOrCols
> 0 )
2544 // If rows inserted, include row counter where necessary
2545 rowOrCol
+= numRowsOrCols
;
2547 else if ( numRowsOrCols
< 0)
2549 // If rows deleted, either decrement row counter (if row still exists)
2550 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2551 rowOrCol
+= numRowsOrCols
;
2554 m_rowsOrCols
.RemoveAt(n
);
2555 m_attrs
[n
]->DecRef();
2556 m_attrs
.RemoveAt(n
);
2564 // ----------------------------------------------------------------------------
2565 // wxGridCellAttrProvider
2566 // ----------------------------------------------------------------------------
2568 wxGridCellAttrProvider::wxGridCellAttrProvider()
2570 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2573 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2578 void wxGridCellAttrProvider::InitData()
2580 m_data
= new wxGridCellAttrProviderData
;
2583 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2584 wxGridCellAttr::wxAttrKind kind
) const
2586 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2591 case (wxGridCellAttr::Any
):
2592 //Get cached merge attributes.
2593 // Currenlty not used as no cache implemented as not mutiable
2594 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2597 //Basicaly implement old version.
2598 //Also check merge cache, so we don't have to re-merge every time..
2599 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2600 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2601 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2603 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2604 // Two or more are non NULL
2605 attr
= new wxGridCellAttr
;
2606 attr
->SetKind(wxGridCellAttr::Merged
);
2610 attr
->MergeWith(attrcell
);
2614 attr
->MergeWith(attrcol
);
2618 attr
->MergeWith(attrrow
);
2621 //store merge attr if cache implemented
2623 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2627 // one or none is non null return it or null.
2628 if(attrrow
) attr
= attrrow
;
2644 case (wxGridCellAttr::Cell
):
2645 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2647 case (wxGridCellAttr::Col
):
2648 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2650 case (wxGridCellAttr::Row
):
2651 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2655 // (wxGridCellAttr::Default):
2656 // (wxGridCellAttr::Merged):
2663 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2669 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2672 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2677 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2680 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2685 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2688 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2692 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2694 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2698 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2702 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2704 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2708 // ----------------------------------------------------------------------------
2709 // wxGridTypeRegistry
2710 // ----------------------------------------------------------------------------
2712 wxGridTypeRegistry::~wxGridTypeRegistry()
2714 size_t count
= m_typeinfo
.Count();
2715 for ( size_t i
= 0; i
< count
; i
++ )
2716 delete m_typeinfo
[i
];
2720 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2721 wxGridCellRenderer
* renderer
,
2722 wxGridCellEditor
* editor
)
2724 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2726 // is it already registered?
2727 int loc
= FindRegisteredDataType(typeName
);
2728 if ( loc
!= wxNOT_FOUND
)
2730 delete m_typeinfo
[loc
];
2731 m_typeinfo
[loc
] = info
;
2735 m_typeinfo
.Add(info
);
2739 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2741 size_t count
= m_typeinfo
.GetCount();
2742 for ( size_t i
= 0; i
< count
; i
++ )
2744 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2753 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2755 int index
= FindRegisteredDataType(typeName
);
2756 if ( index
== wxNOT_FOUND
)
2758 // check whether this is one of the standard ones, in which case
2759 // register it "on the fly"
2761 if ( typeName
== wxGRID_VALUE_STRING
)
2763 RegisterDataType(wxGRID_VALUE_STRING
,
2764 new wxGridCellStringRenderer
,
2765 new wxGridCellTextEditor
);
2767 #endif // wxUSE_TEXTCTRL
2769 if ( typeName
== wxGRID_VALUE_BOOL
)
2771 RegisterDataType(wxGRID_VALUE_BOOL
,
2772 new wxGridCellBoolRenderer
,
2773 new wxGridCellBoolEditor
);
2775 #endif // wxUSE_CHECKBOX
2777 if ( typeName
== wxGRID_VALUE_NUMBER
)
2779 RegisterDataType(wxGRID_VALUE_NUMBER
,
2780 new wxGridCellNumberRenderer
,
2781 new wxGridCellNumberEditor
);
2783 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2785 RegisterDataType(wxGRID_VALUE_FLOAT
,
2786 new wxGridCellFloatRenderer
,
2787 new wxGridCellFloatEditor
);
2789 #endif // wxUSE_TEXTCTRL
2791 if ( typeName
== wxGRID_VALUE_CHOICE
)
2793 RegisterDataType(wxGRID_VALUE_CHOICE
,
2794 new wxGridCellStringRenderer
,
2795 new wxGridCellChoiceEditor
);
2797 #endif // wxUSE_COMBOBOX
2802 // we get here only if just added the entry for this type, so return
2804 index
= m_typeinfo
.GetCount() - 1;
2810 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2812 int index
= FindDataType(typeName
);
2813 if ( index
== wxNOT_FOUND
)
2815 // the first part of the typename is the "real" type, anything after ':'
2816 // are the parameters for the renderer
2817 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2818 if ( index
== wxNOT_FOUND
)
2823 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2824 wxGridCellRenderer
*rendererOld
= renderer
;
2825 renderer
= renderer
->Clone();
2826 rendererOld
->DecRef();
2828 wxGridCellEditor
*editor
= GetEditor(index
);
2829 wxGridCellEditor
*editorOld
= editor
;
2830 editor
= editor
->Clone();
2831 editorOld
->DecRef();
2833 // do it even if there are no parameters to reset them to defaults
2834 wxString params
= typeName
.AfterFirst(_T(':'));
2835 renderer
->SetParameters(params
);
2836 editor
->SetParameters(params
);
2838 // register the new typename
2839 RegisterDataType(typeName
, renderer
, editor
);
2841 // we just registered it, it's the last one
2842 index
= m_typeinfo
.GetCount() - 1;
2848 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2850 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2856 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2858 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2864 // ----------------------------------------------------------------------------
2866 // ----------------------------------------------------------------------------
2868 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2871 wxGridTableBase::wxGridTableBase()
2873 m_view
= (wxGrid
*) NULL
;
2874 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2877 wxGridTableBase::~wxGridTableBase()
2879 delete m_attrProvider
;
2882 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2884 delete m_attrProvider
;
2885 m_attrProvider
= attrProvider
;
2888 bool wxGridTableBase::CanHaveAttributes()
2890 if ( ! GetAttrProvider() )
2892 // use the default attr provider by default
2893 SetAttrProvider(new wxGridCellAttrProvider
);
2898 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2900 if ( m_attrProvider
)
2901 return m_attrProvider
->GetAttr(row
, col
, kind
);
2903 return (wxGridCellAttr
*)NULL
;
2906 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2908 if ( m_attrProvider
)
2910 attr
->SetKind(wxGridCellAttr::Cell
);
2911 m_attrProvider
->SetAttr(attr
, row
, col
);
2915 // as we take ownership of the pointer and don't store it, we must
2921 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2923 if ( m_attrProvider
)
2925 attr
->SetKind(wxGridCellAttr::Row
);
2926 m_attrProvider
->SetRowAttr(attr
, row
);
2930 // as we take ownership of the pointer and don't store it, we must
2936 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2938 if ( m_attrProvider
)
2940 attr
->SetKind(wxGridCellAttr::Col
);
2941 m_attrProvider
->SetColAttr(attr
, col
);
2945 // as we take ownership of the pointer and don't store it, we must
2951 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2952 size_t WXUNUSED(numRows
) )
2954 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2959 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2961 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2966 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2967 size_t WXUNUSED(numRows
) )
2969 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2974 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2975 size_t WXUNUSED(numCols
) )
2977 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2982 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2984 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
2989 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
2990 size_t WXUNUSED(numCols
) )
2992 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
2998 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3001 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3002 // how much it makes sense to us geeks.
3006 wxString
wxGridTableBase::GetColLabelValue( int col
)
3008 // default col labels are:
3009 // cols 0 to 25 : A-Z
3010 // cols 26 to 675 : AA-ZZ
3015 for ( n
= 1; ; n
++ )
3017 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3019 if ( col
< 0 ) break;
3022 // reverse the string...
3024 for ( i
= 0; i
< n
; i
++ )
3033 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3035 return wxGRID_VALUE_STRING
;
3038 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3039 const wxString
& typeName
)
3041 return typeName
== wxGRID_VALUE_STRING
;
3044 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3046 return CanGetValueAs(row
, col
, typeName
);
3049 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3054 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3059 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3064 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3065 long WXUNUSED(value
) )
3069 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3070 double WXUNUSED(value
) )
3074 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3075 bool WXUNUSED(value
) )
3080 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3081 const wxString
& WXUNUSED(typeName
) )
3086 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3087 const wxString
& WXUNUSED(typeName
),
3088 void* WXUNUSED(value
) )
3092 //////////////////////////////////////////////////////////////////////
3094 // Message class for the grid table to send requests and notifications
3098 wxGridTableMessage::wxGridTableMessage()
3100 m_table
= (wxGridTableBase
*) NULL
;
3106 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3107 int commandInt1
, int commandInt2
)
3111 m_comInt1
= commandInt1
;
3112 m_comInt2
= commandInt2
;
3117 //////////////////////////////////////////////////////////////////////
3119 // A basic grid table for string data. An object of this class will
3120 // created by wxGrid if you don't specify an alternative table class.
3123 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3125 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3127 wxGridStringTable::wxGridStringTable()
3132 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3135 m_data
.Alloc( numRows
);
3138 sa
.Alloc( numCols
);
3139 sa
.Add( wxEmptyString
, numCols
);
3141 m_data
.Add( sa
, numRows
);
3144 wxGridStringTable::~wxGridStringTable()
3148 int wxGridStringTable::GetNumberRows()
3150 return m_data
.GetCount();
3153 int wxGridStringTable::GetNumberCols()
3155 if ( m_data
.GetCount() > 0 )
3156 return m_data
[0].GetCount();
3161 wxString
wxGridStringTable::GetValue( int row
, int col
)
3163 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3165 _T("invalid row or column index in wxGridStringTable") );
3167 return m_data
[row
][col
];
3170 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3172 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3173 _T("invalid row or column index in wxGridStringTable") );
3175 m_data
[row
][col
] = value
;
3178 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3180 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3182 _T("invalid row or column index in wxGridStringTable") );
3184 return (m_data
[row
][col
] == wxEmptyString
);
3187 void wxGridStringTable::Clear()
3190 int numRows
, numCols
;
3192 numRows
= m_data
.GetCount();
3195 numCols
= m_data
[0].GetCount();
3197 for ( row
= 0; row
< numRows
; row
++ )
3199 for ( col
= 0; col
< numCols
; col
++ )
3201 m_data
[row
][col
] = wxEmptyString
;
3208 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3210 size_t curNumRows
= m_data
.GetCount();
3211 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3212 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3214 if ( pos
>= curNumRows
)
3216 return AppendRows( numRows
);
3220 sa
.Alloc( curNumCols
);
3221 sa
.Add( wxEmptyString
, curNumCols
);
3222 m_data
.Insert( sa
, pos
, numRows
);
3225 wxGridTableMessage
msg( this,
3226 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3230 GetView()->ProcessTableMessage( msg
);
3236 bool wxGridStringTable::AppendRows( size_t numRows
)
3238 size_t curNumRows
= m_data
.GetCount();
3239 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3240 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3243 if ( curNumCols
> 0 )
3245 sa
.Alloc( curNumCols
);
3246 sa
.Add( wxEmptyString
, curNumCols
);
3249 m_data
.Add( sa
, numRows
);
3253 wxGridTableMessage
msg( this,
3254 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3257 GetView()->ProcessTableMessage( msg
);
3263 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3265 size_t curNumRows
= m_data
.GetCount();
3267 if ( pos
>= curNumRows
)
3269 wxFAIL_MSG( wxString::Format
3271 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3273 (unsigned long)numRows
,
3274 (unsigned long)curNumRows
3280 if ( numRows
> curNumRows
- pos
)
3282 numRows
= curNumRows
- pos
;
3285 if ( numRows
>= curNumRows
)
3291 m_data
.RemoveAt( pos
, numRows
);
3295 wxGridTableMessage
msg( this,
3296 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3300 GetView()->ProcessTableMessage( msg
);
3306 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3310 size_t curNumRows
= m_data
.GetCount();
3311 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3312 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3314 if ( pos
>= curNumCols
)
3316 return AppendCols( numCols
);
3319 for ( row
= 0; row
< curNumRows
; row
++ )
3321 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3323 m_data
[row
].Insert( wxEmptyString
, col
);
3328 wxGridTableMessage
msg( this,
3329 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3333 GetView()->ProcessTableMessage( msg
);
3339 bool wxGridStringTable::AppendCols( size_t numCols
)
3343 size_t curNumRows
= m_data
.GetCount();
3347 // TODO: something better than this ?
3349 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3354 for ( row
= 0; row
< curNumRows
; row
++ )
3356 m_data
[row
].Add( wxEmptyString
, numCols
);
3361 wxGridTableMessage
msg( this,
3362 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3365 GetView()->ProcessTableMessage( msg
);
3371 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3375 size_t curNumRows
= m_data
.GetCount();
3376 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3377 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3379 if ( pos
>= curNumCols
)
3381 wxFAIL_MSG( wxString::Format
3383 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3385 (unsigned long)numCols
,
3386 (unsigned long)curNumCols
3391 if ( numCols
> curNumCols
- pos
)
3393 numCols
= curNumCols
- pos
;
3396 for ( row
= 0; row
< curNumRows
; row
++ )
3398 if ( numCols
>= curNumCols
)
3400 m_data
[row
].Clear();
3404 m_data
[row
].RemoveAt( pos
, numCols
);
3409 wxGridTableMessage
msg( this,
3410 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3414 GetView()->ProcessTableMessage( msg
);
3420 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3422 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3424 // using default label
3426 return wxGridTableBase::GetRowLabelValue( row
);
3430 return m_rowLabels
[ row
];
3434 wxString
wxGridStringTable::GetColLabelValue( int col
)
3436 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3438 // using default label
3440 return wxGridTableBase::GetColLabelValue( col
);
3444 return m_colLabels
[ col
];
3448 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3450 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3452 int n
= m_rowLabels
.GetCount();
3454 for ( i
= n
; i
<= row
; i
++ )
3456 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3460 m_rowLabels
[row
] = value
;
3463 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3465 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3467 int n
= m_colLabels
.GetCount();
3469 for ( i
= n
; i
<= col
; i
++ )
3471 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3475 m_colLabels
[col
] = value
;
3480 //////////////////////////////////////////////////////////////////////
3481 //////////////////////////////////////////////////////////////////////
3483 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3485 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3486 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3487 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3488 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3489 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3490 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3491 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3494 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3496 const wxPoint
&pos
, const wxSize
&size
)
3497 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3502 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3506 // NO - don't do this because it will set both the x and y origin
3507 // coords to match the parent scrolled window and we just want to
3508 // set the y coord - MB
3510 // m_owner->PrepareDC( dc );
3513 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3514 dc
.SetDeviceOrigin( 0, -y
);
3516 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3517 m_owner
->DrawRowLabels( dc
, rows
);
3521 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3523 m_owner
->ProcessRowLabelMouseEvent( event
);
3527 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3529 m_owner
->GetEventHandler()->ProcessEvent(event
);
3533 // This seems to be required for wxMotif otherwise the mouse
3534 // cursor must be in the cell edit control to get key events
3536 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3538 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3541 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3543 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3546 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3548 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3553 //////////////////////////////////////////////////////////////////////
3555 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3557 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3558 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3559 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3560 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3561 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3562 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3563 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3566 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3568 const wxPoint
&pos
, const wxSize
&size
)
3569 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3574 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3578 // NO - don't do this because it will set both the x and y origin
3579 // coords to match the parent scrolled window and we just want to
3580 // set the x coord - MB
3582 // m_owner->PrepareDC( dc );
3585 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3586 dc
.SetDeviceOrigin( -x
, 0 );
3588 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3589 m_owner
->DrawColLabels( dc
, cols
);
3593 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3595 m_owner
->ProcessColLabelMouseEvent( event
);
3598 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3600 m_owner
->GetEventHandler()->ProcessEvent(event
);
3604 // This seems to be required for wxMotif otherwise the mouse
3605 // cursor must be in the cell edit control to get key events
3607 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3609 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3612 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3614 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3617 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3619 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3623 //////////////////////////////////////////////////////////////////////
3625 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3627 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3628 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3629 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3630 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3631 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3632 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3633 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3636 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3638 const wxPoint
&pos
, const wxSize
&size
)
3639 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3644 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3648 int client_height
= 0;
3649 int client_width
= 0;
3650 GetClientSize( &client_width
, &client_height
);
3656 rect
.SetWidth( client_width
- 2 );
3657 rect
.SetHeight( client_height
- 2 );
3659 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3661 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3662 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3663 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3664 dc
.DrawLine( 0, 0, client_width
, 0 );
3665 dc
.DrawLine( 0, 0, 0, client_height
);
3667 dc
.SetPen( *wxWHITE_PEN
);
3668 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3669 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3674 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3676 m_owner
->ProcessCornerLabelMouseEvent( event
);
3680 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3682 m_owner
->GetEventHandler()->ProcessEvent(event
);
3685 // This seems to be required for wxMotif otherwise the mouse
3686 // cursor must be in the cell edit control to get key events
3688 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3690 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3693 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3695 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3698 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3700 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3704 //////////////////////////////////////////////////////////////////////
3706 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3708 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3709 EVT_PAINT( wxGridWindow::OnPaint
)
3710 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3711 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3712 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3713 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3714 EVT_CHAR ( wxGridWindow::OnChar
)
3715 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3716 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3717 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3720 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3721 wxGridRowLabelWindow
*rowLblWin
,
3722 wxGridColLabelWindow
*colLblWin
,
3725 const wxSize
&size
)
3726 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3727 wxT("grid window") )
3731 m_rowLabelWin
= rowLblWin
;
3732 m_colLabelWin
= colLblWin
;
3736 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3738 wxPaintDC
dc( this );
3739 m_owner
->PrepareDC( dc
);
3740 wxRegion reg
= GetUpdateRegion();
3741 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3742 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3743 #if WXGRID_DRAW_LINES
3744 m_owner
->DrawAllGridLines( dc
, reg
);
3746 m_owner
->DrawGridSpace( dc
);
3747 m_owner
->DrawHighlight( dc
, DirtyCells
);
3751 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3753 wxWindow::ScrollWindow( dx
, dy
, rect
);
3754 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3755 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3759 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3761 m_owner
->ProcessGridCellMouseEvent( event
);
3764 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3766 m_owner
->GetEventHandler()->ProcessEvent(event
);
3769 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3770 // cursor must be in the cell edit control to get key events
3772 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3774 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3777 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3779 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3782 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3784 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3787 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3791 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3793 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3797 //////////////////////////////////////////////////////////////////////
3799 // Internal Helper function for computing row or column from some
3800 // (unscrolled) coordinate value, using either
3801 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3802 // of m_rowBottoms/m_ColRights to speed up the search!
3804 // Internal helper macros for simpler use of that function
3806 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3807 const wxArrayInt
& BorderArray
, int nMax
,
3810 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3811 m_minAcceptableColWidth, \
3812 m_colRights, m_numCols, true)
3813 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3814 m_minAcceptableRowHeight, \
3815 m_rowBottoms, m_numRows, true)
3816 /////////////////////////////////////////////////////////////////////
3818 #if wxUSE_EXTENDED_RTTI
3819 WX_DEFINE_FLAGS( wxGridStyle
)
3821 wxBEGIN_FLAGS( wxGridStyle
)
3822 // new style border flags, we put them first to
3823 // use them for streaming out
3824 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3825 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3826 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3827 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3828 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3829 wxFLAGS_MEMBER(wxBORDER_NONE
)
3831 // old style border flags
3832 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3833 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3834 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3835 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3836 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3837 wxFLAGS_MEMBER(wxBORDER
)
3839 // standard window styles
3840 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3841 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3842 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3843 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3844 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3845 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3846 wxFLAGS_MEMBER(wxVSCROLL
)
3847 wxFLAGS_MEMBER(wxHSCROLL
)
3849 wxEND_FLAGS( wxGridStyle
)
3851 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3853 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3854 wxHIDE_PROPERTY( Children
)
3855 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3856 wxEND_PROPERTIES_TABLE()
3858 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3859 wxEND_HANDLERS_TABLE()
3861 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3864 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3867 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3870 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3871 EVT_PAINT( wxGrid::OnPaint
)
3872 EVT_SIZE( wxGrid::OnSize
)
3873 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3874 EVT_KEY_UP( wxGrid::OnKeyUp
)
3875 EVT_CHAR ( wxGrid::OnChar
)
3876 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3881 // in order to make sure that a size event is not
3882 // trigerred in a unfinished state
3883 m_cornerLabelWin
= NULL
;
3884 m_rowLabelWin
= NULL
;
3885 m_colLabelWin
= NULL
;
3889 wxGrid::wxGrid( wxWindow
*parent
,
3894 const wxString
& name
)
3895 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3896 m_colMinWidths(GRID_HASH_SIZE
),
3897 m_rowMinHeights(GRID_HASH_SIZE
)
3900 SetBestFittingSize(size
);
3903 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3904 const wxPoint
& pos
, const wxSize
& size
,
3905 long style
, const wxString
& name
)
3907 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3908 style
| wxWANTS_CHARS
, name
))
3911 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3912 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3915 SetBestFittingSize(size
);
3923 // Must do this or ~wxScrollHelper will pop the wrong event handler
3924 SetTargetWindow(this);
3926 wxSafeDecRef(m_defaultCellAttr
);
3928 #ifdef DEBUG_ATTR_CACHE
3929 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3930 wxPrintf(_T("wxGrid attribute cache statistics: "
3931 "total: %u, hits: %u (%u%%)\n"),
3932 total
, gs_nAttrCacheHits
,
3933 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3939 delete m_typeRegistry
;
3945 // ----- internal init and update functions
3948 // NOTE: If using the default visual attributes works everywhere then this can
3949 // be removed as well as the #else cases below.
3950 #define _USE_VISATTR 0
3953 #include "wx/listbox.h"
3956 void wxGrid::Create()
3958 m_created
= false; // set to true by CreateGrid
3960 m_table
= (wxGridTableBase
*) NULL
;
3963 m_cellEditCtrlEnabled
= false;
3965 m_defaultCellAttr
= new wxGridCellAttr();
3967 // Set default cell attributes
3968 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3969 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3970 m_defaultCellAttr
->SetFont(GetFont());
3971 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3972 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3973 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3976 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3977 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3979 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3980 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3983 m_defaultCellAttr
->SetTextColour(
3984 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3985 m_defaultCellAttr
->SetBackgroundColour(
3986 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3991 m_currentCellCoords
= wxGridNoCellCoords
;
3993 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3994 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3996 // create the type registry
3997 m_typeRegistry
= new wxGridTypeRegistry
;
4000 // subwindow components that make up the wxGrid
4001 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4006 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4011 m_colLabelWin
= new wxGridColLabelWindow( this,
4016 m_gridWin
= new wxGridWindow( this,
4023 SetTargetWindow( m_gridWin
);
4026 wxColour gfg
= gva
.colFg
;
4027 wxColour gbg
= gva
.colBg
;
4028 wxColour lfg
= lva
.colFg
;
4029 wxColour lbg
= lva
.colBg
;
4031 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4032 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4033 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4034 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4036 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4037 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4038 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4039 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4040 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4041 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4043 m_gridWin
->SetOwnForegroundColour(gfg
);
4044 m_gridWin
->SetOwnBackgroundColour(gbg
);
4050 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4051 wxGrid::wxGridSelectionModes selmode
)
4053 wxCHECK_MSG( !m_created
,
4055 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4057 m_numRows
= numRows
;
4058 m_numCols
= numCols
;
4060 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4061 m_table
->SetView( this );
4063 m_selection
= new wxGridSelection( this, selmode
);
4072 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4074 wxCHECK_RET( m_created
,
4075 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4077 m_selection
->SetSelectionMode( selmode
);
4080 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4082 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4083 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4085 return m_selection
->GetSelectionMode();
4088 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4089 wxGrid::wxGridSelectionModes selmode
)
4093 // stop all processing
4098 wxGridTableBase
*t
=m_table
;
4111 m_numRows
= table
->GetNumberRows();
4112 m_numCols
= table
->GetNumberCols();
4115 m_table
->SetView( this );
4118 m_selection
= new wxGridSelection( this, selmode
);
4131 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4132 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4134 if ( m_rowLabelWin
)
4136 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4140 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4143 m_labelTextColour
= wxColour( _T("BLACK") );
4146 m_attrCache
.row
= -1;
4147 m_attrCache
.col
= -1;
4148 m_attrCache
.attr
= NULL
;
4150 // TODO: something better than this ?
4152 m_labelFont
= this->GetFont();
4153 m_labelFont
.SetWeight( wxBOLD
);
4155 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4156 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4158 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4159 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4160 m_colLabelTextOrientation
= wxHORIZONTAL
;
4162 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4163 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4165 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4166 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4168 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4169 m_defaultRowHeight
+= 8;
4171 m_defaultRowHeight
+= 4;
4174 m_gridLineColour
= wxColour( 192,192,192 );
4175 m_gridLinesEnabled
= true;
4176 m_cellHighlightColour
= *wxBLACK
;
4177 m_cellHighlightPenWidth
= 2;
4178 m_cellHighlightROPenWidth
= 1;
4180 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4181 m_winCapture
= (wxWindow
*)NULL
;
4182 m_canDragRowSize
= true;
4183 m_canDragColSize
= true;
4184 m_canDragGridSize
= true;
4185 m_canDragCell
= false;
4187 m_dragRowOrCol
= -1;
4188 m_isDragging
= false;
4189 m_startDragPos
= wxDefaultPosition
;
4191 m_waitForSlowClick
= false;
4193 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4194 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4196 m_currentCellCoords
= wxGridNoCellCoords
;
4198 m_selectingTopLeft
= wxGridNoCellCoords
;
4199 m_selectingBottomRight
= wxGridNoCellCoords
;
4200 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4201 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4203 m_editable
= true; // default for whole grid
4205 m_inOnKeyDown
= false;
4211 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4212 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4215 // ----------------------------------------------------------------------------
4216 // the idea is to call these functions only when necessary because they create
4217 // quite big arrays which eat memory mostly unnecessary - in particular, if
4218 // default widths/heights are used for all rows/columns, we may not use these
4221 // with some extra code, it should be possible to only store the
4222 // widths/heights different from default ones but this will be done later...
4223 // ----------------------------------------------------------------------------
4225 void wxGrid::InitRowHeights()
4227 m_rowHeights
.Empty();
4228 m_rowBottoms
.Empty();
4230 m_rowHeights
.Alloc( m_numRows
);
4231 m_rowBottoms
.Alloc( m_numRows
);
4235 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4237 for ( int i
= 0; i
< m_numRows
; i
++ )
4239 rowBottom
+= m_defaultRowHeight
;
4240 m_rowBottoms
.Add( rowBottom
);
4244 void wxGrid::InitColWidths()
4246 m_colWidths
.Empty();
4247 m_colRights
.Empty();
4249 m_colWidths
.Alloc( m_numCols
);
4250 m_colRights
.Alloc( m_numCols
);
4253 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4255 for ( int i
= 0; i
< m_numCols
; i
++ )
4257 colRight
+= m_defaultColWidth
;
4258 m_colRights
.Add( colRight
);
4262 int wxGrid::GetColWidth(int col
) const
4264 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4267 int wxGrid::GetColLeft(int col
) const
4269 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4270 : m_colRights
[col
] - m_colWidths
[col
];
4273 int wxGrid::GetColRight(int col
) const
4275 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4279 int wxGrid::GetRowHeight(int row
) const
4281 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4284 int wxGrid::GetRowTop(int row
) const
4286 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4287 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4290 int wxGrid::GetRowBottom(int row
) const
4292 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4293 : m_rowBottoms
[row
];
4296 void wxGrid::CalcDimensions()
4299 GetClientSize( &cw
, &ch
);
4301 if ( m_rowLabelWin
->IsShown() )
4302 cw
-= m_rowLabelWidth
;
4303 if ( m_colLabelWin
->IsShown() )
4304 ch
-= m_colLabelHeight
;
4307 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4308 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4310 // take into account editor if shown
4311 if( IsCellEditControlShown() )
4314 int r
= m_currentCellCoords
.GetRow();
4315 int c
= m_currentCellCoords
.GetCol();
4316 int x
= GetColLeft(c
);
4317 int y
= GetRowTop(r
);
4319 // how big is the editor
4320 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4321 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4322 editor
->GetControl()->GetSize(&w2
, &h2
);
4325 if( w2
> w
) w
= w2
;
4326 if( h2
> h
) h
= h2
;
4331 // preserve (more or less) the previous position
4333 GetViewStart( &x
, &y
);
4335 // ensure the position is valid for the new scroll ranges
4337 x
= wxMax( w
- 1, 0 );
4339 y
= wxMax( h
- 1, 0 );
4341 // do set scrollbar parameters
4342 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4343 GetScrollX(w
), GetScrollY(h
), x
, y
,
4344 GetBatchCount() != 0);
4346 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4347 // still must reposition the children
4352 void wxGrid::CalcWindowSizes()
4354 // escape if the window is has not been fully created yet
4356 if ( m_cornerLabelWin
== NULL
)
4360 GetClientSize( &cw
, &ch
);
4362 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4363 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4365 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4366 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4368 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4369 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4371 if ( m_gridWin
&& m_gridWin
->IsShown() )
4372 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4376 // this is called when the grid table sends a message to say that it
4377 // has been redimensioned
4379 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4382 bool result
= false;
4384 // Clear the attribute cache as the attribute might refer to a different
4385 // cell than stored in the cache after adding/removing rows/columns.
4387 // By the same reasoning, the editor should be dismissed if columns are
4388 // added or removed. And for consistency, it should IMHO always be
4389 // removed, not only if the cell "underneath" it actually changes.
4390 // For now, I intentionally do not save the editor's content as the
4391 // cell it might want to save that stuff to might no longer exist.
4392 HideCellEditControl();
4394 // if we were using the default widths/heights so far, we must change them
4396 if ( m_colWidths
.IsEmpty() )
4401 if ( m_rowHeights
.IsEmpty() )
4407 switch ( msg
.GetId() )
4409 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4411 size_t pos
= msg
.GetCommandInt();
4412 int numRows
= msg
.GetCommandInt2();
4414 m_numRows
+= numRows
;
4416 if ( !m_rowHeights
.IsEmpty() )
4418 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4419 m_rowBottoms
.Insert( 0, pos
, numRows
);
4422 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4424 for ( i
= pos
; i
< m_numRows
; i
++ )
4426 bottom
+= m_rowHeights
[i
];
4427 m_rowBottoms
[i
] = bottom
;
4430 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4432 // if we have just inserted cols into an empty grid the current
4433 // cell will be undefined...
4435 SetCurrentCell( 0, 0 );
4439 m_selection
->UpdateRows( pos
, numRows
);
4440 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4442 attrProvider
->UpdateAttrRows( pos
, numRows
);
4444 if ( !GetBatchCount() )
4447 m_rowLabelWin
->Refresh();
4453 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4455 int numRows
= msg
.GetCommandInt();
4456 int oldNumRows
= m_numRows
;
4457 m_numRows
+= numRows
;
4459 if ( !m_rowHeights
.IsEmpty() )
4461 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4462 m_rowBottoms
.Add( 0, numRows
);
4465 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4467 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4469 bottom
+= m_rowHeights
[i
];
4470 m_rowBottoms
[i
] = bottom
;
4473 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4475 // if we have just inserted cols into an empty grid the current
4476 // cell will be undefined...
4478 SetCurrentCell( 0, 0 );
4480 if ( !GetBatchCount() )
4483 m_rowLabelWin
->Refresh();
4489 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4491 size_t pos
= msg
.GetCommandInt();
4492 int numRows
= msg
.GetCommandInt2();
4493 m_numRows
-= numRows
;
4495 if ( !m_rowHeights
.IsEmpty() )
4497 m_rowHeights
.RemoveAt( pos
, numRows
);
4498 m_rowBottoms
.RemoveAt( pos
, numRows
);
4501 for ( i
= 0; i
< m_numRows
; i
++ )
4503 h
+= m_rowHeights
[i
];
4504 m_rowBottoms
[i
] = h
;
4509 m_currentCellCoords
= wxGridNoCellCoords
;
4513 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4514 m_currentCellCoords
.Set( 0, 0 );
4518 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4519 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4521 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4522 // ifdef'd out following patch from Paul Gammans
4524 // No need to touch column attributes, unless we
4525 // removed _all_ rows, in this case, we remove
4526 // all column attributes.
4527 // I hate to do this here, but the
4528 // needed data is not available inside UpdateAttrRows.
4529 if ( !GetNumberRows() )
4530 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4533 if ( !GetBatchCount() )
4536 m_rowLabelWin
->Refresh();
4542 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4544 size_t pos
= msg
.GetCommandInt();
4545 int numCols
= msg
.GetCommandInt2();
4546 m_numCols
+= numCols
;
4548 if ( !m_colWidths
.IsEmpty() )
4550 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4551 m_colRights
.Insert( 0, pos
, numCols
);
4554 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4556 for ( i
= pos
; i
< m_numCols
; i
++ )
4558 right
+= m_colWidths
[i
];
4559 m_colRights
[i
] = right
;
4562 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4564 // if we have just inserted cols into an empty grid the current
4565 // cell will be undefined...
4567 SetCurrentCell( 0, 0 );
4571 m_selection
->UpdateCols( pos
, numCols
);
4572 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4574 attrProvider
->UpdateAttrCols( pos
, numCols
);
4575 if ( !GetBatchCount() )
4578 m_colLabelWin
->Refresh();
4585 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4587 int numCols
= msg
.GetCommandInt();
4588 int oldNumCols
= m_numCols
;
4589 m_numCols
+= numCols
;
4590 if ( !m_colWidths
.IsEmpty() )
4592 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4593 m_colRights
.Add( 0, numCols
);
4596 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4598 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4600 right
+= m_colWidths
[i
];
4601 m_colRights
[i
] = right
;
4604 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4606 // if we have just inserted cols into an empty grid the current
4607 // cell will be undefined...
4609 SetCurrentCell( 0, 0 );
4611 if ( !GetBatchCount() )
4614 m_colLabelWin
->Refresh();
4620 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4622 size_t pos
= msg
.GetCommandInt();
4623 int numCols
= msg
.GetCommandInt2();
4624 m_numCols
-= numCols
;
4626 if ( !m_colWidths
.IsEmpty() )
4628 m_colWidths
.RemoveAt( pos
, numCols
);
4629 m_colRights
.RemoveAt( pos
, numCols
);
4632 for ( i
= 0; i
< m_numCols
; i
++ )
4634 w
+= m_colWidths
[i
];
4640 m_currentCellCoords
= wxGridNoCellCoords
;
4644 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4645 m_currentCellCoords
.Set( 0, 0 );
4649 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4650 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4652 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4653 // ifdef'd out following patch from Paul Gammans
4655 // No need to touch row attributes, unless we
4656 // removed _all_ columns, in this case, we remove
4657 // all row attributes.
4658 // I hate to do this here, but the
4659 // needed data is not available inside UpdateAttrCols.
4660 if ( !GetNumberCols() )
4661 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4664 if ( !GetBatchCount() )
4667 m_colLabelWin
->Refresh();
4674 if (result
&& !GetBatchCount() )
4675 m_gridWin
->Refresh();
4680 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4682 wxRegionIterator
iter( reg
);
4685 wxArrayInt rowlabels
;
4692 // TODO: remove this when we can...
4693 // There is a bug in wxMotif that gives garbage update
4694 // rectangles if you jump-scroll a long way by clicking the
4695 // scrollbar with middle button. This is a work-around
4697 #if defined(__WXMOTIF__)
4699 m_gridWin
->GetClientSize( &cw
, &ch
);
4700 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4701 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4704 // logical bounds of update region
4707 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4708 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4710 // find the row labels within these bounds
4713 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4715 if ( GetRowBottom(row
) < top
)
4718 if ( GetRowTop(row
) > bottom
)
4721 rowlabels
.Add( row
);
4731 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4733 wxRegionIterator
iter( reg
);
4736 wxArrayInt colLabels
;
4743 // TODO: remove this when we can...
4744 // There is a bug in wxMotif that gives garbage update
4745 // rectangles if you jump-scroll a long way by clicking the
4746 // scrollbar with middle button. This is a work-around
4748 #if defined(__WXMOTIF__)
4750 m_gridWin
->GetClientSize( &cw
, &ch
);
4751 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4752 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4755 // logical bounds of update region
4758 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4759 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4761 // find the cells within these bounds
4764 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4766 if ( GetColRight(col
) < left
)
4769 if ( GetColLeft(col
) > right
)
4772 colLabels
.Add( col
);
4781 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4783 wxRegionIterator
iter( reg
);
4786 wxGridCellCoordsArray cellsExposed
;
4788 int left
, top
, right
, bottom
;
4793 // TODO: remove this when we can...
4794 // There is a bug in wxMotif that gives garbage update
4795 // rectangles if you jump-scroll a long way by clicking the
4796 // scrollbar with middle button. This is a work-around
4798 #if defined(__WXMOTIF__)
4800 m_gridWin
->GetClientSize( &cw
, &ch
);
4801 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4802 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4803 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4804 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4807 // logical bounds of update region
4809 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4810 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4812 // find the cells within these bounds
4815 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4817 if ( GetRowBottom(row
) <= top
)
4820 if ( GetRowTop(row
) > bottom
)
4823 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4825 if ( GetColRight(col
) <= left
)
4828 if ( GetColLeft(col
) > right
)
4831 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4838 return cellsExposed
;
4842 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4845 wxPoint
pos( event
.GetPosition() );
4846 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4848 if ( event
.Dragging() )
4852 m_isDragging
= true;
4853 m_rowLabelWin
->CaptureMouse();
4856 if ( event
.LeftIsDown() )
4858 switch( m_cursorMode
)
4860 case WXGRID_CURSOR_RESIZE_ROW
:
4862 int cw
, ch
, left
, dummy
;
4863 m_gridWin
->GetClientSize( &cw
, &ch
);
4864 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4866 wxClientDC
dc( m_gridWin
);
4869 GetRowTop(m_dragRowOrCol
) +
4870 GetRowMinimalHeight(m_dragRowOrCol
) );
4871 dc
.SetLogicalFunction(wxINVERT
);
4872 if ( m_dragLastPos
>= 0 )
4874 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4876 dc
.DrawLine( left
, y
, left
+cw
, y
);
4881 case WXGRID_CURSOR_SELECT_ROW
:
4882 if ( (row
= YToRow( y
)) >= 0 )
4886 m_selection
->SelectRow( row
,
4887 event
.ControlDown(),
4894 // default label to suppress warnings about "enumeration value
4895 // 'xxx' not handled in switch
4903 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4908 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4909 m_isDragging
= false;
4912 // ------------ Entering or leaving the window
4914 if ( event
.Entering() || event
.Leaving() )
4916 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4920 // ------------ Left button pressed
4922 else if ( event
.LeftDown() )
4924 // don't send a label click event for a hit on the
4925 // edge of the row label - this is probably the user
4926 // wanting to resize the row
4928 if ( YToEdgeOfRow(y
) < 0 )
4932 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4934 if ( !event
.ShiftDown() && !event
.ControlDown() )
4938 if ( event
.ShiftDown() )
4940 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4943 GetNumberCols() - 1,
4944 event
.ControlDown(),
4951 m_selection
->SelectRow( row
,
4952 event
.ControlDown(),
4959 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4964 // starting to drag-resize a row
4966 if ( CanDragRowSize() )
4967 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4972 // ------------ Left double click
4974 else if (event
.LeftDClick() )
4976 int row
= YToEdgeOfRow(y
);
4981 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4983 // no default action at the moment
4988 // adjust row height depending on label text
4989 AutoSizeRowLabelSize( row
);
4991 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4997 // ------------ Left button released
4999 else if ( event
.LeftUp() )
5001 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5003 DoEndDragResizeRow();
5005 // Note: we are ending the event *after* doing
5006 // default processing in this case
5008 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5011 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5016 // ------------ Right button down
5018 else if ( event
.RightDown() )
5022 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5024 // no default action at the moment
5029 // ------------ Right double click
5031 else if ( event
.RightDClick() )
5035 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5037 // no default action at the moment
5042 // ------------ No buttons down and mouse moving
5044 else if ( event
.Moving() )
5046 m_dragRowOrCol
= YToEdgeOfRow( y
);
5047 if ( m_dragRowOrCol
>= 0 )
5049 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5051 // don't capture the mouse yet
5052 if ( CanDragRowSize() )
5053 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5056 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5058 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5064 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5067 wxPoint
pos( event
.GetPosition() );
5068 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5070 if ( event
.Dragging() )
5074 m_isDragging
= true;
5075 m_colLabelWin
->CaptureMouse();
5078 if ( event
.LeftIsDown() )
5080 switch( m_cursorMode
)
5082 case WXGRID_CURSOR_RESIZE_COL
:
5084 int cw
, ch
, dummy
, top
;
5085 m_gridWin
->GetClientSize( &cw
, &ch
);
5086 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5088 wxClientDC
dc( m_gridWin
);
5091 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5092 GetColMinimalWidth(m_dragRowOrCol
));
5093 dc
.SetLogicalFunction(wxINVERT
);
5094 if ( m_dragLastPos
>= 0 )
5096 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5098 dc
.DrawLine( x
, top
, x
, top
+ch
);
5103 case WXGRID_CURSOR_SELECT_COL
:
5104 if ( (col
= XToCol( x
)) >= 0 )
5108 m_selection
->SelectCol( col
,
5109 event
.ControlDown(),
5116 // default label to suppress warnings about "enumeration value
5117 // 'xxx' not handled in switch
5125 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5130 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5131 m_isDragging
= false;
5134 // ------------ Entering or leaving the window
5136 if ( event
.Entering() || event
.Leaving() )
5138 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5142 // ------------ Left button pressed
5144 else if ( event
.LeftDown() )
5146 // don't send a label click event for a hit on the
5147 // edge of the col label - this is probably the user
5148 // wanting to resize the col
5150 if ( XToEdgeOfCol(x
) < 0 )
5154 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5156 if ( !event
.ShiftDown() && !event
.ControlDown() )
5160 if ( event
.ShiftDown() )
5162 m_selection
->SelectBlock( 0,
5163 m_currentCellCoords
.GetCol(),
5164 GetNumberRows() - 1, col
,
5165 event
.ControlDown(),
5172 m_selection
->SelectCol( col
,
5173 event
.ControlDown(),
5180 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5185 // starting to drag-resize a col
5187 if ( CanDragColSize() )
5188 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5193 // ------------ Left double click
5195 if ( event
.LeftDClick() )
5197 int col
= XToEdgeOfCol(x
);
5202 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5204 // no default action at the moment
5209 // adjust column width depending on label text
5210 AutoSizeColLabelSize( col
);
5212 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5218 // ------------ Left button released
5220 else if ( event
.LeftUp() )
5222 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5224 DoEndDragResizeCol();
5226 // Note: we are ending the event *after* doing
5227 // default processing in this case
5229 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5232 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5237 // ------------ Right button down
5239 else if ( event
.RightDown() )
5243 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5245 // no default action at the moment
5250 // ------------ Right double click
5252 else if ( event
.RightDClick() )
5256 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5258 // no default action at the moment
5263 // ------------ No buttons down and mouse moving
5265 else if ( event
.Moving() )
5267 m_dragRowOrCol
= XToEdgeOfCol( x
);
5268 if ( m_dragRowOrCol
>= 0 )
5270 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5272 // don't capture the cursor yet
5273 if ( CanDragColSize() )
5274 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5277 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5279 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5285 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5287 if ( event
.LeftDown() )
5289 // indicate corner label by having both row and
5292 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5298 else if ( event
.LeftDClick() )
5300 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5303 else if ( event
.RightDown() )
5305 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5307 // no default action at the moment
5311 else if ( event
.RightDClick() )
5313 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5315 // no default action at the moment
5320 void wxGrid::ChangeCursorMode(CursorMode mode
,
5325 static const wxChar
*cursorModes
[] =
5334 wxLogTrace(_T("grid"),
5335 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5336 win
== m_colLabelWin
? _T("colLabelWin")
5337 : win
? _T("rowLabelWin")
5339 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5340 #endif // __WXDEBUG__
5342 if ( mode
== m_cursorMode
&&
5343 win
== m_winCapture
&&
5344 captureMouse
== (m_winCapture
!= NULL
))
5349 // by default use the grid itself
5355 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5356 m_winCapture
= (wxWindow
*)NULL
;
5359 m_cursorMode
= mode
;
5361 switch ( m_cursorMode
)
5363 case WXGRID_CURSOR_RESIZE_ROW
:
5364 win
->SetCursor( m_rowResizeCursor
);
5367 case WXGRID_CURSOR_RESIZE_COL
:
5368 win
->SetCursor( m_colResizeCursor
);
5372 win
->SetCursor( *wxSTANDARD_CURSOR
);
5375 // we need to capture mouse when resizing
5376 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5377 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5379 if ( captureMouse
&& resize
)
5381 win
->CaptureMouse();
5386 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5389 wxPoint
pos( event
.GetPosition() );
5390 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5392 wxGridCellCoords coords
;
5393 XYToCell( x
, y
, coords
);
5395 int cell_rows
, cell_cols
;
5396 bool isFirstDrag
= !m_isDragging
;
5397 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5398 if ((cell_rows
< 0) || (cell_cols
< 0))
5400 coords
.SetRow(coords
.GetRow() + cell_rows
);
5401 coords
.SetCol(coords
.GetCol() + cell_cols
);
5404 if ( event
.Dragging() )
5406 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5408 // Don't start doing anything until the mouse has been drug at
5409 // least 3 pixels in any direction...
5412 if (m_startDragPos
== wxDefaultPosition
)
5414 m_startDragPos
= pos
;
5417 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5421 m_isDragging
= true;
5422 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5424 // Hide the edit control, so it
5425 // won't interfer with drag-shrinking.
5426 if ( IsCellEditControlShown() )
5428 HideCellEditControl();
5429 SaveEditControlValue();
5432 // Have we captured the mouse yet?
5435 m_winCapture
= m_gridWin
;
5436 m_winCapture
->CaptureMouse();
5439 if ( coords
!= wxGridNoCellCoords
)
5441 if ( event
.ControlDown() )
5443 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5444 m_selectingKeyboard
= coords
;
5445 HighlightBlock ( m_selectingKeyboard
, coords
);
5447 else if ( CanDragCell() )
5451 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5452 m_selectingKeyboard
= coords
;
5454 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5462 if ( !IsSelection() )
5464 HighlightBlock( coords
, coords
);
5468 HighlightBlock( m_currentCellCoords
, coords
);
5472 if (! IsVisible(coords
))
5474 MakeCellVisible(coords
);
5475 // TODO: need to introduce a delay or something here. The
5476 // scrolling is way to fast, at least on MSW - also on GTK.
5480 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5482 int cw
, ch
, left
, dummy
;
5483 m_gridWin
->GetClientSize( &cw
, &ch
);
5484 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5486 wxClientDC
dc( m_gridWin
);
5488 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5489 GetRowMinimalHeight(m_dragRowOrCol
) );
5490 dc
.SetLogicalFunction(wxINVERT
);
5491 if ( m_dragLastPos
>= 0 )
5493 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5495 dc
.DrawLine( left
, y
, left
+cw
, y
);
5498 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5500 int cw
, ch
, dummy
, top
;
5501 m_gridWin
->GetClientSize( &cw
, &ch
);
5502 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5504 wxClientDC
dc( m_gridWin
);
5506 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5507 GetColMinimalWidth(m_dragRowOrCol
) );
5508 dc
.SetLogicalFunction(wxINVERT
);
5509 if ( m_dragLastPos
>= 0 )
5511 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5513 dc
.DrawLine( x
, top
, x
, top
+ch
);
5520 m_isDragging
= false;
5521 m_startDragPos
= wxDefaultPosition
;
5523 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5524 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5527 if ( event
.Entering() || event
.Leaving() )
5529 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5530 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5535 // ------------ Left button pressed
5537 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5539 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5544 if ( !event
.ControlDown() )
5546 if ( event
.ShiftDown() )
5550 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5551 m_currentCellCoords
.GetCol(),
5554 event
.ControlDown(),
5560 else if ( XToEdgeOfCol(x
) < 0 &&
5561 YToEdgeOfRow(y
) < 0 )
5563 DisableCellEditControl();
5564 MakeCellVisible( coords
);
5566 if ( event
.ControlDown() )
5570 m_selection
->ToggleCellSelection( coords
.GetRow(),
5572 event
.ControlDown(),
5577 m_selectingTopLeft
= wxGridNoCellCoords
;
5578 m_selectingBottomRight
= wxGridNoCellCoords
;
5579 m_selectingKeyboard
= coords
;
5583 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5584 SetCurrentCell( coords
);
5587 if ( m_selection
->GetSelectionMode() !=
5588 wxGrid::wxGridSelectCells
)
5590 HighlightBlock( coords
, coords
);
5599 // ------------ Left double click
5601 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5603 DisableCellEditControl();
5605 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5607 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5612 // we want double click to select a cell and start editing
5613 // (i.e. to behave in same way as sequence of two slow clicks):
5614 m_waitForSlowClick
= true;
5621 // ------------ Left button released
5623 else if ( event
.LeftUp() )
5625 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5629 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5630 m_winCapture
= NULL
;
5633 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5636 EnableCellEditControl();
5638 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5639 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5640 editor
->StartingClick();
5644 m_waitForSlowClick
= false;
5646 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5647 m_selectingBottomRight
!= wxGridNoCellCoords
)
5651 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5652 m_selectingTopLeft
.GetCol(),
5653 m_selectingBottomRight
.GetRow(),
5654 m_selectingBottomRight
.GetCol(),
5655 event
.ControlDown(),
5661 m_selectingTopLeft
= wxGridNoCellCoords
;
5662 m_selectingBottomRight
= wxGridNoCellCoords
;
5664 // Show the edit control, if it has been hidden for
5666 ShowCellEditControl();
5669 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5671 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5672 DoEndDragResizeRow();
5674 // Note: we are ending the event *after* doing
5675 // default processing in this case
5677 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5679 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5681 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5682 DoEndDragResizeCol();
5684 // Note: we are ending the event *after* doing
5685 // default processing in this case
5687 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5694 // ------------ Right button down
5696 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5698 DisableCellEditControl();
5699 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5704 // no default action at the moment
5709 // ------------ Right double click
5711 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5713 DisableCellEditControl();
5714 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5719 // no default action at the moment
5723 // ------------ Moving and no button action
5725 else if ( event
.Moving() && !event
.IsButton() )
5727 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5729 // out of grid cell area
5730 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5734 int dragRow
= YToEdgeOfRow( y
);
5735 int dragCol
= XToEdgeOfCol( x
);
5737 // Dragging on the corner of a cell to resize in both
5738 // directions is not implemented yet...
5740 if ( dragRow
>= 0 && dragCol
>= 0 )
5742 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5748 m_dragRowOrCol
= dragRow
;
5750 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5752 if ( CanDragRowSize() && CanDragGridSize() )
5753 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5758 m_dragRowOrCol
= dragCol
;
5766 m_dragRowOrCol
= dragCol
;
5768 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5770 if ( CanDragColSize() && CanDragGridSize() )
5771 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5777 // Neither on a row or col edge
5779 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5781 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5787 void wxGrid::DoEndDragResizeRow()
5789 if ( m_dragLastPos
>= 0 )
5791 // erase the last line and resize the row
5793 int cw
, ch
, left
, dummy
;
5794 m_gridWin
->GetClientSize( &cw
, &ch
);
5795 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5797 wxClientDC
dc( m_gridWin
);
5799 dc
.SetLogicalFunction( wxINVERT
);
5800 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5801 HideCellEditControl();
5802 SaveEditControlValue();
5804 int rowTop
= GetRowTop(m_dragRowOrCol
);
5805 SetRowSize( m_dragRowOrCol
,
5806 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5808 if ( !GetBatchCount() )
5810 // Only needed to get the correct rect.y:
5811 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5813 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5814 rect
.width
= m_rowLabelWidth
;
5815 rect
.height
= ch
- rect
.y
;
5816 m_rowLabelWin
->Refresh( true, &rect
);
5818 // if there is a multicell block, paint all of it
5821 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5822 int leftCol
= XToCol(left
);
5823 int rightCol
= internalXToCol(left
+cw
);
5826 for (i
=leftCol
; i
<rightCol
; i
++)
5828 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5829 if (cell_rows
< subtract_rows
)
5830 subtract_rows
= cell_rows
;
5832 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5833 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5834 rect
.height
= ch
- rect
.y
;
5837 m_gridWin
->Refresh( false, &rect
);
5840 ShowCellEditControl();
5845 void wxGrid::DoEndDragResizeCol()
5847 if ( m_dragLastPos
>= 0 )
5849 // erase the last line and resize the col
5851 int cw
, ch
, dummy
, top
;
5852 m_gridWin
->GetClientSize( &cw
, &ch
);
5853 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5855 wxClientDC
dc( m_gridWin
);
5857 dc
.SetLogicalFunction( wxINVERT
);
5858 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5859 HideCellEditControl();
5860 SaveEditControlValue();
5862 int colLeft
= GetColLeft(m_dragRowOrCol
);
5863 SetColSize( m_dragRowOrCol
,
5864 wxMax( m_dragLastPos
- colLeft
,
5865 GetColMinimalWidth(m_dragRowOrCol
) ) );
5867 if ( !GetBatchCount() )
5869 // Only needed to get the correct rect.x:
5870 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5872 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5873 rect
.width
= cw
- rect
.x
;
5874 rect
.height
= m_colLabelHeight
;
5875 m_colLabelWin
->Refresh( true, &rect
);
5877 // if there is a multicell block, paint all of it
5880 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5881 int topRow
= YToRow(top
);
5882 int bottomRow
= internalYToRow(top
+cw
);
5885 for (i
=topRow
; i
<bottomRow
; i
++)
5887 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5888 if (cell_cols
< subtract_cols
)
5889 subtract_cols
= cell_cols
;
5891 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5892 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5893 rect
.width
= cw
- rect
.x
;
5896 m_gridWin
->Refresh( false, &rect
);
5899 ShowCellEditControl();
5906 // ------ interaction with data model
5908 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5910 switch ( msg
.GetId() )
5912 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5913 return GetModelValues();
5915 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5916 return SetModelValues();
5918 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5919 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5920 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5921 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5922 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5923 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5924 return Redimension( msg
);
5933 // The behaviour of this function depends on the grid table class
5934 // Clear() function. For the default wxGridStringTable class the
5935 // behavious is to replace all cell contents with wxEmptyString but
5936 // not to change the number of rows or cols.
5938 void wxGrid::ClearGrid()
5942 if (IsCellEditControlEnabled())
5943 DisableCellEditControl();
5946 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5951 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5953 // TODO: something with updateLabels flag
5957 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5963 if (IsCellEditControlEnabled())
5964 DisableCellEditControl();
5966 bool done
= m_table
->InsertRows( pos
, numRows
);
5969 // the table will have sent the results of the insert row
5970 // operation to this view object as a grid table message
5976 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5978 // TODO: something with updateLabels flag
5982 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5988 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5990 // the table will have sent the results of the append row
5991 // operation to this view object as a grid table message
5997 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5999 // TODO: something with updateLabels flag
6003 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6009 if (IsCellEditControlEnabled())
6010 DisableCellEditControl();
6012 bool done
= m_table
->DeleteRows( pos
, numRows
);
6014 // the table will have sent the results of the delete row
6015 // operation to this view object as a grid table message
6021 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6023 // TODO: something with updateLabels flag
6027 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6033 if (IsCellEditControlEnabled())
6034 DisableCellEditControl();
6036 bool done
= m_table
->InsertCols( pos
, numCols
);
6038 // the table will have sent the results of the insert col
6039 // operation to this view object as a grid table message
6045 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6047 // TODO: something with updateLabels flag
6051 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6057 bool done
= m_table
->AppendCols( numCols
);
6059 // the table will have sent the results of the append col
6060 // operation to this view object as a grid table message
6066 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6068 // TODO: something with updateLabels flag
6072 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6078 if (IsCellEditControlEnabled())
6079 DisableCellEditControl();
6081 bool done
= m_table
->DeleteCols( pos
, numCols
);
6083 // the table will have sent the results of the delete col
6084 // operation to this view object as a grid table message
6092 // ----- event handlers
6095 // Generate a grid event based on a mouse event and
6096 // return the result of ProcessEvent()
6098 int wxGrid::SendEvent( const wxEventType type
,
6100 wxMouseEvent
& mouseEv
)
6105 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6107 int rowOrCol
= (row
== -1 ? col
: row
);
6109 wxGridSizeEvent
gridEvt( GetId(),
6113 mouseEv
.GetX() + GetRowLabelSize(),
6114 mouseEv
.GetY() + GetColLabelSize(),
6115 mouseEv
.ControlDown(),
6116 mouseEv
.ShiftDown(),
6118 mouseEv
.MetaDown() );
6120 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6121 vetoed
= !gridEvt
.IsAllowed();
6123 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6125 // Right now, it should _never_ end up here!
6126 wxGridRangeSelectEvent
gridEvt( GetId(),
6130 m_selectingBottomRight
,
6132 mouseEv
.ControlDown(),
6133 mouseEv
.ShiftDown(),
6135 mouseEv
.MetaDown() );
6137 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6138 vetoed
= !gridEvt
.IsAllowed();
6142 wxGridEvent
gridEvt( GetId(),
6146 mouseEv
.GetX() + GetRowLabelSize(),
6147 mouseEv
.GetY() + GetColLabelSize(),
6149 mouseEv
.ControlDown(),
6150 mouseEv
.ShiftDown(),
6152 mouseEv
.MetaDown() );
6153 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6154 vetoed
= !gridEvt
.IsAllowed();
6157 // A Veto'd event may not be `claimed' so test this first
6158 if (vetoed
) return -1;
6159 return claimed
? 1 : 0;
6163 // Generate a grid event of specified type and return the result
6164 // of ProcessEvent().
6166 int wxGrid::SendEvent( const wxEventType type
,
6172 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6174 int rowOrCol
= (row
== -1 ? col
: row
);
6176 wxGridSizeEvent
gridEvt( GetId(),
6181 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6182 vetoed
= !gridEvt
.IsAllowed();
6186 wxGridEvent
gridEvt( GetId(),
6191 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6192 vetoed
= !gridEvt
.IsAllowed();
6195 // A Veto'd event may not be `claimed' so test this first
6196 if (vetoed
) return -1;
6197 return claimed
? 1 : 0;
6201 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6203 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6206 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6208 // Don't do anything if between Begin/EndBatch...
6209 // EndBatch() will do all this on the last nested one anyway.
6210 if (! GetBatchCount())
6212 // Refresh to get correct scrolled position:
6213 wxScrolledWindow::Refresh(eraseb
,rect
);
6217 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6218 int width_label
, width_cell
, height_label
, height_cell
;
6221 //Copy rectangle can get scroll offsets..
6222 rect_x
= rect
->GetX();
6223 rect_y
= rect
->GetY();
6224 rectWidth
= rect
->GetWidth();
6225 rectHeight
= rect
->GetHeight();
6227 width_label
= m_rowLabelWidth
- rect_x
;
6228 if (width_label
> rectWidth
) width_label
= rectWidth
;
6230 height_label
= m_colLabelHeight
- rect_y
;
6231 if (height_label
> rectHeight
) height_label
= rectHeight
;
6233 if (rect_x
> m_rowLabelWidth
)
6235 x
= rect_x
- m_rowLabelWidth
;
6236 width_cell
= rectWidth
;
6241 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6244 if (rect_y
> m_colLabelHeight
)
6246 y
= rect_y
- m_colLabelHeight
;
6247 height_cell
= rectHeight
;
6252 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6255 // Paint corner label part intersecting rect.
6256 if ( width_label
> 0 && height_label
> 0 )
6258 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6259 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6262 // Paint col labels part intersecting rect.
6263 if ( width_cell
> 0 && height_label
> 0 )
6265 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6266 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6269 // Paint row labels part intersecting rect.
6270 if ( width_label
> 0 && height_cell
> 0 )
6272 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6273 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6276 // Paint cell area part intersecting rect.
6277 if ( width_cell
> 0 && height_cell
> 0 )
6279 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6280 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6285 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6286 m_colLabelWin
->Refresh(eraseb
, NULL
);
6287 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6288 m_gridWin
->Refresh(eraseb
, NULL
);
6293 void wxGrid::OnSize( wxSizeEvent
& event
)
6295 // position the child windows
6298 // don't call CalcDimensions() from here, the base class handles the size
6304 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6306 if ( m_inOnKeyDown
)
6308 // shouldn't be here - we are going round in circles...
6310 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6313 m_inOnKeyDown
= true;
6315 // propagate the event up and see if it gets processed
6317 wxWindow
*parent
= GetParent();
6318 wxKeyEvent
keyEvt( event
);
6319 keyEvt
.SetEventObject( parent
);
6321 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6324 // try local handlers
6326 switch ( event
.GetKeyCode() )
6329 if ( event
.ControlDown() )
6331 MoveCursorUpBlock( event
.ShiftDown() );
6335 MoveCursorUp( event
.ShiftDown() );
6340 if ( event
.ControlDown() )
6342 MoveCursorDownBlock( event
.ShiftDown() );
6346 MoveCursorDown( event
.ShiftDown() );
6351 if ( event
.ControlDown() )
6353 MoveCursorLeftBlock( event
.ShiftDown() );
6357 MoveCursorLeft( event
.ShiftDown() );
6362 if ( event
.ControlDown() )
6364 MoveCursorRightBlock( event
.ShiftDown() );
6368 MoveCursorRight( event
.ShiftDown() );
6373 case WXK_NUMPAD_ENTER
:
6374 if ( event
.ControlDown() )
6376 event
.Skip(); // to let the edit control have the return
6380 if ( GetGridCursorRow() < GetNumberRows()-1 )
6382 MoveCursorDown( event
.ShiftDown() );
6386 // at the bottom of a column
6387 DisableCellEditControl();
6397 if (event
.ShiftDown())
6399 if ( GetGridCursorCol() > 0 )
6401 MoveCursorLeft( false );
6406 DisableCellEditControl();
6411 if ( GetGridCursorCol() < GetNumberCols()-1 )
6413 MoveCursorRight( false );
6418 DisableCellEditControl();
6424 if ( event
.ControlDown() )
6426 MakeCellVisible( 0, 0 );
6427 SetCurrentCell( 0, 0 );
6436 if ( event
.ControlDown() )
6438 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6439 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6456 if ( event
.ControlDown() )
6460 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6461 m_currentCellCoords
.GetCol(),
6462 event
.ControlDown(),
6469 if ( !IsEditable() )
6471 MoveCursorRight( false );
6474 // Otherwise fall through to default
6482 m_inOnKeyDown
= false;
6485 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6487 // try local handlers
6489 if ( event
.GetKeyCode() == WXK_SHIFT
)
6491 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6492 m_selectingBottomRight
!= wxGridNoCellCoords
)
6496 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6497 m_selectingTopLeft
.GetCol(),
6498 m_selectingBottomRight
.GetRow(),
6499 m_selectingBottomRight
.GetCol(),
6500 event
.ControlDown(),
6507 m_selectingTopLeft
= wxGridNoCellCoords
;
6508 m_selectingBottomRight
= wxGridNoCellCoords
;
6509 m_selectingKeyboard
= wxGridNoCellCoords
;
6513 void wxGrid::OnChar( wxKeyEvent
& event
)
6515 // is it possible to edit the current cell at all?
6516 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6518 // yes, now check whether the cells editor accepts the key
6519 int row
= m_currentCellCoords
.GetRow();
6520 int col
= m_currentCellCoords
.GetCol();
6521 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6522 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6524 // <F2> is special and will always start editing, for
6525 // other keys - ask the editor itself
6526 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6527 || editor
->IsAcceptedKey(event
) )
6529 // ensure cell is visble
6530 MakeCellVisible(row
, col
);
6531 EnableCellEditControl();
6533 // a problem can arise if the cell is not completely
6534 // visible (even after calling MakeCellVisible the
6535 // control is not created and calling StartingKey will
6537 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6538 editor
->StartingKey(event
);
6555 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6559 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6561 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6563 // the event has been intercepted - do nothing
6567 wxClientDC
dc(m_gridWin
);
6570 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6572 DisableCellEditControl();
6574 if ( IsVisible( m_currentCellCoords
, false ) )
6577 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6578 if ( !m_gridLinesEnabled
)
6586 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6588 // Otherwise refresh redraws the highlight!
6589 m_currentCellCoords
= coords
;
6591 DrawGridCellArea(dc
,cells
);
6592 DrawAllGridLines( dc
, r
);
6596 m_currentCellCoords
= coords
;
6598 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6599 DrawCellHighlight(dc
, attr
);
6604 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6607 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6611 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6614 rightCol
= GetNumberCols() - 1;
6616 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6619 bottomRow
= GetNumberRows() - 1;
6623 if ( topRow
> bottomRow
)
6630 if ( leftCol
> rightCol
)
6637 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6638 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6640 // First the case that we selected a completely new area
6641 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6642 m_selectingBottomRight
== wxGridNoCellCoords
)
6645 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6646 wxGridCellCoords ( bottomRow
, rightCol
) );
6647 m_gridWin
->Refresh( false, &rect
);
6649 // Now handle changing an existing selection area.
6650 else if ( m_selectingTopLeft
!= updateTopLeft
||
6651 m_selectingBottomRight
!= updateBottomRight
)
6653 // Compute two optimal update rectangles:
6654 // Either one rectangle is a real subset of the
6655 // other, or they are (almost) disjoint!
6657 bool need_refresh
[4];
6661 need_refresh
[3] = false;
6664 // Store intermediate values
6665 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6666 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6667 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6668 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6670 // Determine the outer/inner coordinates.
6671 if (oldLeft
> leftCol
)
6677 if (oldTop
> topRow
)
6683 if (oldRight
< rightCol
)
6686 oldRight
= rightCol
;
6689 if (oldBottom
< bottomRow
)
6692 oldBottom
= bottomRow
;
6696 // Now, either the stuff marked old is the outer
6697 // rectangle or we don't have a situation where one
6698 // is contained in the other.
6700 if ( oldLeft
< leftCol
)
6702 // Refresh the newly selected or deselected
6703 // area to the left of the old or new selection.
6704 need_refresh
[0] = true;
6705 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6707 wxGridCellCoords ( oldBottom
,
6711 if ( oldTop
< topRow
)
6713 // Refresh the newly selected or deselected
6714 // area above the old or new selection.
6715 need_refresh
[1] = true;
6716 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6718 wxGridCellCoords ( topRow
- 1,
6722 if ( oldRight
> rightCol
)
6724 // Refresh the newly selected or deselected
6725 // area to the right of the old or new selection.
6726 need_refresh
[2] = true;
6727 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6729 wxGridCellCoords ( oldBottom
,
6733 if ( oldBottom
> bottomRow
)
6735 // Refresh the newly selected or deselected
6736 // area below the old or new selection.
6737 need_refresh
[3] = true;
6738 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6740 wxGridCellCoords ( oldBottom
,
6744 // various Refresh() calls
6745 for (i
= 0; i
< 4; i
++ )
6746 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6747 m_gridWin
->Refresh( false, &(rect
[i
]) );
6750 m_selectingTopLeft
= updateTopLeft
;
6751 m_selectingBottomRight
= updateBottomRight
;
6755 // ------ functions to get/send data (see also public functions)
6758 bool wxGrid::GetModelValues()
6760 // Hide the editor, so it won't hide a changed value.
6761 HideCellEditControl();
6765 // all we need to do is repaint the grid
6767 m_gridWin
->Refresh();
6775 bool wxGrid::SetModelValues()
6779 // Disable the editor, so it won't hide a changed value.
6780 // Do we also want to save the current value of the editor first?
6782 DisableCellEditControl();
6786 for ( row
= 0; row
< m_numRows
; row
++ )
6788 for ( col
= 0; col
< m_numCols
; col
++ )
6790 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6802 // Note - this function only draws cells that are in the list of
6803 // exposed cells (usually set from the update region by
6804 // CalcExposedCells)
6806 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6808 if ( !m_numRows
|| !m_numCols
) return;
6810 int i
, numCells
= cells
.GetCount();
6811 int row
, col
, cell_rows
, cell_cols
;
6812 wxGridCellCoordsArray redrawCells
;
6814 for ( i
= numCells
-1; i
>= 0; i
-- )
6816 row
= cells
[i
].GetRow();
6817 col
= cells
[i
].GetCol();
6818 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6820 // If this cell is part of a multicell block, find owner for repaint
6821 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6823 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6824 bool marked
= false;
6825 for ( int j
= 0; j
< numCells
; j
++ )
6827 if ( cell
== cells
[j
] )
6835 int count
= redrawCells
.GetCount();
6836 for (int j
= 0; j
< count
; j
++)
6838 if ( cell
== redrawCells
[j
] )
6844 if (!marked
) redrawCells
.Add( cell
);
6846 continue; // don't bother drawing this cell
6849 // If this cell is empty, find cell to left that might want to overflow
6850 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6852 for ( int l
= 0; l
< cell_rows
; l
++ )
6854 // find a cell in this row to left alreay marked for repaint
6856 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6857 if ((redrawCells
[k
].GetCol() < left
) &&
6858 (redrawCells
[k
].GetRow() == row
))
6859 left
=redrawCells
[k
].GetCol();
6861 if (left
== col
) left
= 0; // oh well
6863 for (int j
= col
-1; j
>= left
; j
--)
6865 if (!m_table
->IsEmptyCell(row
+l
, j
))
6867 if (GetCellOverflow(row
+l
, j
))
6869 wxGridCellCoords
cell(row
+l
, j
);
6870 bool marked
= false;
6872 for (int k
= 0; k
< numCells
; k
++)
6874 if ( cell
== cells
[k
] )
6882 int count
= redrawCells
.GetCount();
6883 for (int k
= 0; k
< count
; k
++)
6885 if ( cell
== redrawCells
[k
] )
6891 if (!marked
) redrawCells
.Add( cell
);
6899 DrawCell( dc
, cells
[i
] );
6902 numCells
= redrawCells
.GetCount();
6904 for ( i
= numCells
- 1; i
>= 0; i
-- )
6906 DrawCell( dc
, redrawCells
[i
] );
6911 void wxGrid::DrawGridSpace( wxDC
& dc
)
6914 m_gridWin
->GetClientSize( &cw
, &ch
);
6917 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6919 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6920 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6922 if ( right
> rightCol
|| bottom
> bottomRow
)
6925 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6927 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6928 dc
.SetPen( *wxTRANSPARENT_PEN
);
6930 if ( right
> rightCol
)
6932 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6935 if ( bottom
> bottomRow
)
6937 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6943 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6945 int row
= coords
.GetRow();
6946 int col
= coords
.GetCol();
6948 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6951 // we draw the cell border ourselves
6952 #if !WXGRID_DRAW_LINES
6953 if ( m_gridLinesEnabled
)
6954 DrawCellBorder( dc
, coords
);
6957 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6959 bool isCurrent
= coords
== m_currentCellCoords
;
6961 wxRect rect
= CellToRect( row
, col
);
6963 // if the editor is shown, we should use it and not the renderer
6964 // Note: However, only if it is really _shown_, i.e. not hidden!
6965 if ( isCurrent
&& IsCellEditControlShown() )
6967 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6968 editor
->PaintBackground(rect
, attr
);
6973 // but all the rest is drawn by the cell renderer and hence may be
6975 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6976 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6983 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6985 int row
= m_currentCellCoords
.GetRow();
6986 int col
= m_currentCellCoords
.GetCol();
6988 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6991 wxRect rect
= CellToRect(row
, col
);
6993 // hmmm... what could we do here to show that the cell is disabled?
6994 // for now, I just draw a thinner border than for the other ones, but
6995 // it doesn't look really good
6997 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7001 // The center of th drawn line is where the position/width/height of
7002 // the rectangle is actually at, (on wxMSW atr least,) so we will
7003 // reduce the size of the rectangle to compensate for the thickness of
7004 // the line. If this is too strange on non wxMSW platforms then
7005 // please #ifdef this appropriately.
7006 rect
.x
+= penWidth
/2;
7007 rect
.y
+= penWidth
/2;
7008 rect
.width
-= penWidth
-1;
7009 rect
.height
-= penWidth
-1;
7012 // Now draw the rectangle
7013 // use the cellHighlightColour if the cell is inside a selection, this
7014 // will ensure the cell is always visible.
7015 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7016 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7017 dc
.DrawRectangle(rect
);
7021 // VZ: my experiments with 3d borders...
7023 // how to properly set colours for arbitrary bg?
7024 wxCoord x1
= rect
.x
,
7026 x2
= rect
.x
+ rect
.width
-1,
7027 y2
= rect
.y
+ rect
.height
-1;
7029 dc
.SetPen(*wxWHITE_PEN
);
7030 dc
.DrawLine(x1
, y1
, x2
, y1
);
7031 dc
.DrawLine(x1
, y1
, x1
, y2
);
7033 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7034 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7036 dc
.SetPen(*wxBLACK_PEN
);
7037 dc
.DrawLine(x1
, y2
, x2
, y2
);
7038 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7043 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7045 int row
= coords
.GetRow();
7046 int col
= coords
.GetCol();
7047 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7050 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7052 wxRect rect
= CellToRect( row
, col
);
7054 // right hand border
7056 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7057 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7061 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7062 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7065 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7067 // This if block was previously in wxGrid::OnPaint but that doesn't
7068 // seem to get called under wxGTK - MB
7070 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7071 m_numRows
&& m_numCols
)
7073 m_currentCellCoords
.Set(0, 0);
7076 if ( IsCellEditControlShown() )
7078 // don't show highlight when the edit control is shown
7082 // if the active cell was repainted, repaint its highlight too because it
7083 // might have been damaged by the grid lines
7084 size_t count
= cells
.GetCount();
7085 for ( size_t n
= 0; n
< count
; n
++ )
7087 if ( cells
[n
] == m_currentCellCoords
)
7089 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7090 DrawCellHighlight(dc
, attr
);
7098 // TODO: remove this ???
7099 // This is used to redraw all grid lines e.g. when the grid line colour
7102 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7104 #if !WXGRID_DRAW_LINES
7108 if ( !m_gridLinesEnabled
||
7110 !m_numCols
) return;
7112 int top
, bottom
, left
, right
;
7114 #if 0 //#ifndef __WXGTK__
7118 m_gridWin
->GetClientSize(&cw
, &ch
);
7120 // virtual coords of visible area
7122 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7123 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7128 reg
.GetBox(x
, y
, w
, h
);
7129 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7130 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7134 m_gridWin
->GetClientSize(&cw
, &ch
);
7135 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7136 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7139 // avoid drawing grid lines past the last row and col
7141 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7142 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7144 // no gridlines inside multicells, clip them out
7145 int leftCol
= internalXToCol(left
);
7146 int topRow
= internalYToRow(top
);
7147 int rightCol
= internalXToCol(right
);
7148 int bottomRow
= internalYToRow(bottom
);
7151 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7152 wxRegion
clippedcells(0, 0, cw
, ch
);
7154 int i
, j
, cell_rows
, cell_cols
;
7157 for (j
=topRow
; j
<bottomRow
; j
++)
7159 for (i
=leftCol
; i
<rightCol
; i
++)
7161 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7162 if ((cell_rows
> 1) || (cell_cols
> 1))
7164 rect
= CellToRect(j
,i
);
7165 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7166 clippedcells
.Subtract(rect
);
7168 else if ((cell_rows
< 0) || (cell_cols
< 0))
7170 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7171 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7172 clippedcells
.Subtract(rect
);
7177 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7179 int i
, j
, cell_rows
, cell_cols
;
7182 for (j
=topRow
; j
<bottomRow
; j
++)
7184 for (i
=leftCol
; i
<rightCol
; i
++)
7186 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7187 if ((cell_rows
> 1) || (cell_cols
> 1))
7189 rect
= CellToRect(j
,i
);
7190 clippedcells
.Subtract(rect
);
7192 else if ((cell_rows
< 0) || (cell_cols
< 0))
7194 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7195 clippedcells
.Subtract(rect
);
7200 dc
.SetClippingRegion( clippedcells
);
7202 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7204 // horizontal grid lines
7206 // already declared above - int i;
7207 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7209 int bot
= GetRowBottom(i
) - 1;
7218 dc
.DrawLine( left
, bot
, right
, bot
);
7223 // vertical grid lines
7225 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7227 int colRight
= GetColRight(i
) - 1;
7228 if ( colRight
> right
)
7233 if ( colRight
>= left
)
7235 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7238 dc
.DestroyClippingRegion();
7242 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7244 if ( !m_numRows
) return;
7247 size_t numLabels
= rows
.GetCount();
7249 for ( i
= 0; i
< numLabels
; i
++ )
7251 DrawRowLabel( dc
, rows
[i
] );
7256 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7258 if ( GetRowHeight(row
) <= 0 )
7264 rect
.SetY( GetRowTop(row
) + 1 );
7265 rect
.SetWidth( m_rowLabelWidth
- 2 );
7266 rect
.SetHeight( GetRowHeight(row
) - 2 );
7268 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7270 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7272 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7274 int rowTop
= GetRowTop(row
),
7275 rowBottom
= GetRowBottom(row
) - 1;
7277 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7278 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7279 m_rowLabelWidth
-1, rowBottom
);
7281 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7283 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7285 dc
.SetPen( *wxWHITE_PEN
);
7286 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7287 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7289 dc
.SetBackgroundMode( wxTRANSPARENT
);
7290 dc
.SetTextForeground( GetLabelTextColour() );
7291 dc
.SetFont( GetLabelFont() );
7294 GetRowLabelAlignment( &hAlign
, &vAlign
);
7297 rect
.SetY( GetRowTop(row
) + 2 );
7298 rect
.SetWidth( m_rowLabelWidth
- 4 );
7299 rect
.SetHeight( GetRowHeight(row
) - 4 );
7300 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7304 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7306 if ( !m_numCols
) return;
7309 size_t numLabels
= cols
.GetCount();
7311 for ( i
= 0; i
< numLabels
; i
++ )
7313 DrawColLabel( dc
, cols
[i
] );
7318 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7320 if ( GetColWidth(col
) <= 0 )
7323 int colLeft
= GetColLeft(col
);
7327 rect
.SetX( colLeft
+ 1 );
7329 rect
.SetWidth( GetColWidth(col
) - 2 );
7330 rect
.SetHeight( m_colLabelHeight
- 2 );
7332 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7334 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7336 int colRight
= GetColRight(col
) - 1;
7338 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7339 dc
.DrawLine( colRight
, 0,
7340 colRight
, m_colLabelHeight
-1 );
7342 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7344 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7345 colRight
+1, m_colLabelHeight
-1 );
7347 dc
.SetPen( *wxWHITE_PEN
);
7348 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7349 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7351 dc
.SetBackgroundMode( wxTRANSPARENT
);
7352 dc
.SetTextForeground( GetLabelTextColour() );
7353 dc
.SetFont( GetLabelFont() );
7355 int hAlign
, vAlign
, orient
;
7356 GetColLabelAlignment( &hAlign
, &vAlign
);
7357 orient
= GetColLabelTextOrientation();
7359 rect
.SetX( colLeft
+ 2 );
7361 rect
.SetWidth( GetColWidth(col
) - 4 );
7362 rect
.SetHeight( m_colLabelHeight
- 4 );
7363 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7366 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7367 const wxString
& value
,
7371 int textOrientation
)
7373 wxArrayString lines
;
7375 StringToLines( value
, lines
);
7378 //Forward to new API.
7379 DrawTextRectangle( dc
,
7388 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7389 const wxArrayString
& lines
,
7393 int textOrientation
)
7395 long textWidth
, textHeight
;
7396 long lineWidth
, lineHeight
;
7399 dc
.SetClippingRegion( rect
);
7401 nLines
= lines
.GetCount();
7405 float x
= 0.0, y
= 0.0;
7407 if( textOrientation
== wxHORIZONTAL
)
7408 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7410 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7414 case wxALIGN_BOTTOM
:
7415 if( textOrientation
== wxHORIZONTAL
)
7416 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7418 x
= rect
.x
+ rect
.width
- textWidth
;
7421 case wxALIGN_CENTRE
:
7422 if( textOrientation
== wxHORIZONTAL
)
7423 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7425 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7430 if( textOrientation
== wxHORIZONTAL
)
7437 // Align each line of a multi-line label
7438 for( l
= 0; l
< nLines
; l
++ )
7440 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7442 switch( horizAlign
)
7445 if( textOrientation
== wxHORIZONTAL
)
7446 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7448 y
= rect
.y
+ lineWidth
+ 1;
7451 case wxALIGN_CENTRE
:
7452 if( textOrientation
== wxHORIZONTAL
)
7453 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7455 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7460 if( textOrientation
== wxHORIZONTAL
)
7463 y
= rect
.y
+ rect
.height
- 1;
7467 if( textOrientation
== wxHORIZONTAL
)
7469 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7474 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7479 dc
.DestroyClippingRegion();
7483 // Split multi line text up into an array of strings. Any existing
7484 // contents of the string array are preserved.
7486 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7490 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7491 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7493 while ( startPos
< (int)tVal
.Length() )
7495 pos
= tVal
.Mid(startPos
).Find( eol
);
7500 else if ( pos
== 0 )
7502 lines
.Add( wxEmptyString
);
7506 lines
.Add( value
.Mid(startPos
, pos
) );
7510 if ( startPos
< (int)value
.Length() )
7512 lines
.Add( value
.Mid( startPos
) );
7517 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7518 const wxArrayString
& lines
,
7519 long *width
, long *height
)
7526 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7528 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7529 w
= wxMax( w
, lineW
);
7538 // ------ Batch processing.
7540 void wxGrid::EndBatch()
7542 if ( m_batchCount
> 0 )
7545 if ( !m_batchCount
)
7548 m_rowLabelWin
->Refresh();
7549 m_colLabelWin
->Refresh();
7550 m_cornerLabelWin
->Refresh();
7551 m_gridWin
->Refresh();
7556 // Use this, rather than wxWindow::Refresh(), to force an immediate
7557 // repainting of the grid. Has no effect if you are already inside a
7558 // BeginBatch / EndBatch block.
7560 void wxGrid::ForceRefresh()
7566 bool wxGrid::Enable(bool enable
)
7568 if ( !wxScrolledWindow::Enable(enable
) )
7571 // redraw in the new state
7572 m_gridWin
->Refresh();
7578 // ------ Edit control functions
7582 void wxGrid::EnableEditing( bool edit
)
7584 // TODO: improve this ?
7586 if ( edit
!= m_editable
)
7588 if(!edit
) EnableCellEditControl(edit
);
7594 void wxGrid::EnableCellEditControl( bool enable
)
7599 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7600 SetCurrentCell( 0, 0 );
7602 if ( enable
!= m_cellEditCtrlEnabled
)
7606 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7609 // this should be checked by the caller!
7610 wxASSERT_MSG( CanEnableCellControl(),
7611 _T("can't enable editing for this cell!") );
7613 // do it before ShowCellEditControl()
7614 m_cellEditCtrlEnabled
= enable
;
7616 ShowCellEditControl();
7620 //FIXME:add veto support
7621 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7623 HideCellEditControl();
7624 SaveEditControlValue();
7626 // do it after HideCellEditControl()
7627 m_cellEditCtrlEnabled
= enable
;
7632 bool wxGrid::IsCurrentCellReadOnly() const
7635 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7636 bool readonly
= attr
->IsReadOnly();
7642 bool wxGrid::CanEnableCellControl() const
7644 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7645 !IsCurrentCellReadOnly();
7649 bool wxGrid::IsCellEditControlEnabled() const
7651 // the cell edit control might be disable for all cells or just for the
7652 // current one if it's read only
7653 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7656 bool wxGrid::IsCellEditControlShown() const
7658 bool isShown
= false;
7660 if ( m_cellEditCtrlEnabled
)
7662 int row
= m_currentCellCoords
.GetRow();
7663 int col
= m_currentCellCoords
.GetCol();
7664 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7665 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7670 if ( editor
->IsCreated() )
7672 isShown
= editor
->GetControl()->IsShown();
7682 void wxGrid::ShowCellEditControl()
7684 if ( IsCellEditControlEnabled() )
7686 if ( !IsVisible( m_currentCellCoords
) )
7688 m_cellEditCtrlEnabled
= false;
7693 wxRect rect
= CellToRect( m_currentCellCoords
);
7694 int row
= m_currentCellCoords
.GetRow();
7695 int col
= m_currentCellCoords
.GetCol();
7697 // if this is part of a multicell, find owner (topleft)
7698 int cell_rows
, cell_cols
;
7699 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7700 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7704 m_currentCellCoords
.SetRow( row
);
7705 m_currentCellCoords
.SetCol( col
);
7708 // convert to scrolled coords
7710 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7712 // done in PaintBackground()
7714 // erase the highlight and the cell contents because the editor
7715 // might not cover the entire cell
7716 wxClientDC
dc( m_gridWin
);
7718 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7719 dc
.SetPen(*wxTRANSPARENT_PEN
);
7720 dc
.DrawRectangle(rect
);
7723 // cell is shifted by one pixel
7724 // However, don't allow x or y to become negative
7725 // since the SetSize() method interprets that as
7732 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7733 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7734 if ( !editor
->IsCreated() )
7736 editor
->Create(m_gridWin
, wxID_ANY
,
7737 new wxGridCellEditorEvtHandler(this, editor
));
7739 wxGridEditorCreatedEvent
evt(GetId(),
7740 wxEVT_GRID_EDITOR_CREATED
,
7744 editor
->GetControl());
7745 GetEventHandler()->ProcessEvent(evt
);
7749 // resize editor to overflow into righthand cells if allowed
7750 int maxWidth
= rect
.width
;
7751 wxString value
= GetCellValue(row
, col
);
7752 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7755 GetTextExtent(value
, &maxWidth
, &y
,
7756 NULL
, NULL
, &attr
->GetFont());
7757 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7759 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7760 if (rect
.x
+maxWidth
> client_right
)
7761 maxWidth
= client_right
- rect
.x
;
7763 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7765 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7766 // may have changed earlier
7767 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7770 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7771 // looks weird going over a multicell
7772 if (m_table
->IsEmptyCell(row
,i
) &&
7773 (rect
.width
< maxWidth
) && (c_rows
== 1))
7774 rect
.width
+= GetColWidth(i
);
7778 if (rect
.GetRight() > client_right
)
7779 rect
.SetRight(client_right
-1);
7782 editor
->SetCellAttr(attr
);
7783 editor
->SetSize( rect
);
7784 editor
->Show( true, attr
);
7786 // recalc dimensions in case we need to
7787 // expand the scrolled window to account for editor
7790 editor
->BeginEdit(row
, col
, this);
7791 editor
->SetCellAttr(NULL
);
7800 void wxGrid::HideCellEditControl()
7802 if ( IsCellEditControlEnabled() )
7804 int row
= m_currentCellCoords
.GetRow();
7805 int col
= m_currentCellCoords
.GetCol();
7807 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7808 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7809 editor
->Show( false );
7812 m_gridWin
->SetFocus();
7813 // refresh whole row to the right
7814 wxRect
rect( CellToRect(row
, col
) );
7815 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7816 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7818 // ensure that the pixels under the focus ring get refreshed as well
7819 rect
.Inflate(10,10);
7821 m_gridWin
->Refresh( false, &rect
);
7826 void wxGrid::SaveEditControlValue()
7828 if ( IsCellEditControlEnabled() )
7830 int row
= m_currentCellCoords
.GetRow();
7831 int col
= m_currentCellCoords
.GetCol();
7833 wxString oldval
= GetCellValue(row
,col
);
7835 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7836 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7837 bool changed
= editor
->EndEdit(row
, col
, this);
7844 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7845 m_currentCellCoords
.GetRow(),
7846 m_currentCellCoords
.GetCol() ) < 0 ) {
7848 // Event has been vetoed, set the data back.
7849 SetCellValue(row
,col
,oldval
);
7857 // ------ Grid location functions
7858 // Note that all of these functions work with the logical coordinates of
7859 // grid cells and labels so you will need to convert from device
7860 // coordinates for mouse events etc.
7863 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7865 int row
= YToRow(y
);
7866 int col
= XToCol(x
);
7868 if ( row
== -1 || col
== -1 )
7870 coords
= wxGridNoCellCoords
;
7874 coords
.Set( row
, col
);
7879 // Internal Helper function for computing row or column from some
7880 // (unscrolled) coordinate value, using either
7881 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7882 // of m_rowBottoms/m_ColRights to speed up the search!
7884 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7885 const wxArrayInt
& BorderArray
, int nMax
,
7890 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7896 size_t i_max
= coord
/ defaultDist
,
7899 if (BorderArray
.IsEmpty())
7901 if((int) i_max
< nMax
)
7903 return clipToMinMax
? nMax
- 1 : -1;
7906 if ( i_max
>= BorderArray
.GetCount())
7907 i_max
= BorderArray
.GetCount() - 1;
7910 if ( coord
>= BorderArray
[i_max
])
7914 i_max
= coord
/ minDist
;
7916 i_max
= BorderArray
.GetCount() - 1;
7918 if ( i_max
>= BorderArray
.GetCount())
7919 i_max
= BorderArray
.GetCount() - 1;
7921 if ( coord
>= BorderArray
[i_max
])
7922 return clipToMinMax
? (int)i_max
: -1;
7923 if ( coord
< BorderArray
[0] )
7926 while ( i_max
- i_min
> 0 )
7928 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7929 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7930 if (coord
>= BorderArray
[ i_max
- 1])
7934 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7935 if (coord
< BorderArray
[median
])
7943 int wxGrid::YToRow( int y
)
7945 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7946 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7950 int wxGrid::XToCol( int x
)
7952 return CoordToRowOrCol(x
, m_defaultColWidth
,
7953 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7957 // return the row number that that the y coord is near the edge of, or
7958 // -1 if not near an edge
7960 int wxGrid::YToEdgeOfRow( int y
)
7963 i
= internalYToRow(y
);
7965 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7967 // We know that we are in row i, test whether we are
7968 // close enough to lower or upper border, respectively.
7969 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7971 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7979 // return the col number that that the x coord is near the edge of, or
7980 // -1 if not near an edge
7982 int wxGrid::XToEdgeOfCol( int x
)
7985 i
= internalXToCol(x
);
7987 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7989 // We know that we are in column i, test whether we are
7990 // close enough to right or left border, respectively.
7991 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7993 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8001 wxRect
wxGrid::CellToRect( int row
, int col
)
8003 wxRect
rect( -1, -1, -1, -1 );
8005 if ( row
>= 0 && row
< m_numRows
&&
8006 col
>= 0 && col
< m_numCols
)
8008 int i
, cell_rows
, cell_cols
;
8009 rect
.width
= rect
.height
= 0;
8010 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8011 // if negative then find multicell owner
8012 if (cell_rows
< 0) row
+= cell_rows
;
8013 if (cell_cols
< 0) col
+= cell_cols
;
8014 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8016 rect
.x
= GetColLeft(col
);
8017 rect
.y
= GetRowTop(row
);
8018 for (i
=col
; i
<col
+cell_cols
; i
++)
8019 rect
.width
+= GetColWidth(i
);
8020 for (i
=row
; i
<row
+cell_rows
; i
++)
8021 rect
.height
+= GetRowHeight(i
);
8024 // if grid lines are enabled, then the area of the cell is a bit smaller
8025 if (m_gridLinesEnabled
) {
8033 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8035 // get the cell rectangle in logical coords
8037 wxRect
r( CellToRect( row
, col
) );
8039 // convert to device coords
8041 int left
, top
, right
, bottom
;
8042 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8043 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8045 // check against the client area of the grid window
8048 m_gridWin
->GetClientSize( &cw
, &ch
);
8050 if ( wholeCellVisible
)
8052 // is the cell wholly visible ?
8054 return ( left
>= 0 && right
<= cw
&&
8055 top
>= 0 && bottom
<= ch
);
8059 // is the cell partly visible ?
8061 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8062 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8067 // make the specified cell location visible by doing a minimal amount
8070 void wxGrid::MakeCellVisible( int row
, int col
)
8074 int xpos
= -1, ypos
= -1;
8076 if ( row
>= 0 && row
< m_numRows
&&
8077 col
>= 0 && col
< m_numCols
)
8079 // get the cell rectangle in logical coords
8081 wxRect
r( CellToRect( row
, col
) );
8083 // convert to device coords
8085 int left
, top
, right
, bottom
;
8086 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8087 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8090 m_gridWin
->GetClientSize( &cw
, &ch
);
8096 else if ( bottom
> ch
)
8098 int h
= r
.GetHeight();
8100 for ( i
= row
-1; i
>= 0; i
-- )
8102 int rowHeight
= GetRowHeight(i
);
8103 if ( h
+ rowHeight
> ch
)
8110 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8111 // have rounding errors (this is important, because if we do, we
8112 // might not scroll at all and some cells won't be redrawn)
8114 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8116 ypos
+= m_scrollLineY
;
8123 else if ( right
> cw
)
8125 // position the view so that the cell is on the right
8127 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8128 xpos
= x0
+ (right
- cw
);
8130 // see comment for ypos above
8131 xpos
+= m_scrollLineX
;
8134 if ( xpos
!= -1 || ypos
!= -1 )
8137 xpos
/= m_scrollLineX
;
8139 ypos
/= m_scrollLineY
;
8140 Scroll( xpos
, ypos
);
8148 // ------ Grid cursor movement functions
8151 bool wxGrid::MoveCursorUp( bool expandSelection
)
8153 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8154 m_currentCellCoords
.GetRow() >= 0 )
8156 if ( expandSelection
)
8158 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8159 m_selectingKeyboard
= m_currentCellCoords
;
8160 if ( m_selectingKeyboard
.GetRow() > 0 )
8162 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8163 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8164 m_selectingKeyboard
.GetCol() );
8165 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8168 else if ( m_currentCellCoords
.GetRow() > 0 )
8171 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8172 m_currentCellCoords
.GetCol() );
8173 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8174 m_currentCellCoords
.GetCol() );
8185 bool wxGrid::MoveCursorDown( bool expandSelection
)
8187 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8188 m_currentCellCoords
.GetRow() < m_numRows
)
8190 if ( expandSelection
)
8192 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8193 m_selectingKeyboard
= m_currentCellCoords
;
8194 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8196 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8197 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8198 m_selectingKeyboard
.GetCol() );
8199 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8202 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8205 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8206 m_currentCellCoords
.GetCol() );
8207 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8208 m_currentCellCoords
.GetCol() );
8219 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8221 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8222 m_currentCellCoords
.GetCol() >= 0 )
8224 if ( expandSelection
)
8226 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8227 m_selectingKeyboard
= m_currentCellCoords
;
8228 if ( m_selectingKeyboard
.GetCol() > 0 )
8230 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8231 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8232 m_selectingKeyboard
.GetCol() );
8233 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8236 else if ( m_currentCellCoords
.GetCol() > 0 )
8239 MakeCellVisible( m_currentCellCoords
.GetRow(),
8240 m_currentCellCoords
.GetCol() - 1 );
8241 SetCurrentCell( m_currentCellCoords
.GetRow(),
8242 m_currentCellCoords
.GetCol() - 1 );
8253 bool wxGrid::MoveCursorRight( bool expandSelection
)
8255 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8256 m_currentCellCoords
.GetCol() < m_numCols
)
8258 if ( expandSelection
)
8260 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8261 m_selectingKeyboard
= m_currentCellCoords
;
8262 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8264 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8265 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8266 m_selectingKeyboard
.GetCol() );
8267 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8270 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8273 MakeCellVisible( m_currentCellCoords
.GetRow(),
8274 m_currentCellCoords
.GetCol() + 1 );
8275 SetCurrentCell( m_currentCellCoords
.GetRow(),
8276 m_currentCellCoords
.GetCol() + 1 );
8287 bool wxGrid::MovePageUp()
8289 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8291 int row
= m_currentCellCoords
.GetRow();
8295 m_gridWin
->GetClientSize( &cw
, &ch
);
8297 int y
= GetRowTop(row
);
8298 int newRow
= internalYToRow( y
- ch
+ 1 );
8300 if ( newRow
== row
)
8302 //row > 0 , so newrow can never be less than 0 here.
8306 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8307 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8315 bool wxGrid::MovePageDown()
8317 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8319 int row
= m_currentCellCoords
.GetRow();
8320 if ( (row
+1) < m_numRows
)
8323 m_gridWin
->GetClientSize( &cw
, &ch
);
8325 int y
= GetRowTop(row
);
8326 int newRow
= internalYToRow( y
+ ch
);
8327 if ( newRow
== row
)
8329 // row < m_numRows , so newrow can't overflow here.
8333 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8334 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8342 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8345 m_currentCellCoords
!= wxGridNoCellCoords
&&
8346 m_currentCellCoords
.GetRow() > 0 )
8348 int row
= m_currentCellCoords
.GetRow();
8349 int col
= m_currentCellCoords
.GetCol();
8351 if ( m_table
->IsEmptyCell(row
, col
) )
8353 // starting in an empty cell: find the next block of
8359 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8362 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8364 // starting at the top of a block: find the next block
8370 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8375 // starting within a block: find the top of the block
8380 if ( m_table
->IsEmptyCell(row
, col
) )
8388 MakeCellVisible( row
, col
);
8389 if ( expandSelection
)
8391 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8392 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8397 SetCurrentCell( row
, col
);
8405 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8408 m_currentCellCoords
!= wxGridNoCellCoords
&&
8409 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8411 int row
= m_currentCellCoords
.GetRow();
8412 int col
= m_currentCellCoords
.GetCol();
8414 if ( m_table
->IsEmptyCell(row
, col
) )
8416 // starting in an empty cell: find the next block of
8419 while ( row
< m_numRows
-1 )
8422 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8425 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8427 // starting at the bottom of a block: find the next block
8430 while ( row
< m_numRows
-1 )
8433 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8438 // starting within a block: find the bottom of the block
8440 while ( row
< m_numRows
-1 )
8443 if ( m_table
->IsEmptyCell(row
, col
) )
8451 MakeCellVisible( row
, col
);
8452 if ( expandSelection
)
8454 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8455 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8460 SetCurrentCell( row
, col
);
8469 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8472 m_currentCellCoords
!= wxGridNoCellCoords
&&
8473 m_currentCellCoords
.GetCol() > 0 )
8475 int row
= m_currentCellCoords
.GetRow();
8476 int col
= m_currentCellCoords
.GetCol();
8478 if ( m_table
->IsEmptyCell(row
, col
) )
8480 // starting in an empty cell: find the next block of
8486 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8489 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8491 // starting at the left of a block: find the next block
8497 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8502 // starting within a block: find the left of the block
8507 if ( m_table
->IsEmptyCell(row
, col
) )
8515 MakeCellVisible( row
, col
);
8516 if ( expandSelection
)
8518 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8519 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8524 SetCurrentCell( row
, col
);
8533 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8536 m_currentCellCoords
!= wxGridNoCellCoords
&&
8537 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8539 int row
= m_currentCellCoords
.GetRow();
8540 int col
= m_currentCellCoords
.GetCol();
8542 if ( m_table
->IsEmptyCell(row
, col
) )
8544 // starting in an empty cell: find the next block of
8547 while ( col
< m_numCols
-1 )
8550 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8553 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8555 // starting at the right of a block: find the next block
8558 while ( col
< m_numCols
-1 )
8561 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8566 // starting within a block: find the right of the block
8568 while ( col
< m_numCols
-1 )
8571 if ( m_table
->IsEmptyCell(row
, col
) )
8579 MakeCellVisible( row
, col
);
8580 if ( expandSelection
)
8582 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8583 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8588 SetCurrentCell( row
, col
);
8600 // ------ Label values and formatting
8603 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8605 *horiz
= m_rowLabelHorizAlign
;
8606 *vert
= m_rowLabelVertAlign
;
8609 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8611 *horiz
= m_colLabelHorizAlign
;
8612 *vert
= m_colLabelVertAlign
;
8615 int wxGrid::GetColLabelTextOrientation()
8617 return m_colLabelTextOrientation
;
8620 wxString
wxGrid::GetRowLabelValue( int row
)
8624 return m_table
->GetRowLabelValue( row
);
8634 wxString
wxGrid::GetColLabelValue( int col
)
8638 return m_table
->GetColLabelValue( col
);
8649 void wxGrid::SetRowLabelSize( int width
)
8651 width
= wxMax( width
, 0 );
8652 if ( width
!= m_rowLabelWidth
)
8656 m_rowLabelWin
->Show( false );
8657 m_cornerLabelWin
->Show( false );
8659 else if ( m_rowLabelWidth
== 0 )
8661 m_rowLabelWin
->Show( true );
8662 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8665 m_rowLabelWidth
= width
;
8667 wxScrolledWindow::Refresh( true );
8672 void wxGrid::SetColLabelSize( int height
)
8674 height
= wxMax( height
, 0 );
8675 if ( height
!= m_colLabelHeight
)
8679 m_colLabelWin
->Show( false );
8680 m_cornerLabelWin
->Show( false );
8682 else if ( m_colLabelHeight
== 0 )
8684 m_colLabelWin
->Show( true );
8685 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8688 m_colLabelHeight
= height
;
8690 wxScrolledWindow::Refresh( true );
8695 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8697 if ( m_labelBackgroundColour
!= colour
)
8699 m_labelBackgroundColour
= colour
;
8700 m_rowLabelWin
->SetBackgroundColour( colour
);
8701 m_colLabelWin
->SetBackgroundColour( colour
);
8702 m_cornerLabelWin
->SetBackgroundColour( colour
);
8704 if ( !GetBatchCount() )
8706 m_rowLabelWin
->Refresh();
8707 m_colLabelWin
->Refresh();
8708 m_cornerLabelWin
->Refresh();
8713 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8715 if ( m_labelTextColour
!= colour
)
8717 m_labelTextColour
= colour
;
8718 if ( !GetBatchCount() )
8720 m_rowLabelWin
->Refresh();
8721 m_colLabelWin
->Refresh();
8726 void wxGrid::SetLabelFont( const wxFont
& font
)
8729 if ( !GetBatchCount() )
8731 m_rowLabelWin
->Refresh();
8732 m_colLabelWin
->Refresh();
8736 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8738 // allow old (incorrect) defs to be used
8741 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8742 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8743 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8748 case wxTOP
: vert
= wxALIGN_TOP
; break;
8749 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8750 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8753 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8755 m_rowLabelHorizAlign
= horiz
;
8758 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8760 m_rowLabelVertAlign
= vert
;
8763 if ( !GetBatchCount() )
8765 m_rowLabelWin
->Refresh();
8769 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8771 // allow old (incorrect) defs to be used
8774 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8775 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8776 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8781 case wxTOP
: vert
= wxALIGN_TOP
; break;
8782 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8783 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8786 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8788 m_colLabelHorizAlign
= horiz
;
8791 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8793 m_colLabelVertAlign
= vert
;
8796 if ( !GetBatchCount() )
8798 m_colLabelWin
->Refresh();
8802 // Note: under MSW, the default column label font must be changed because it
8803 // does not support vertical printing
8805 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8806 // pGrid->SetLabelFont(font);
8807 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8809 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8811 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8813 m_colLabelTextOrientation
= textOrientation
;
8816 if ( !GetBatchCount() )
8818 m_colLabelWin
->Refresh();
8822 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8826 m_table
->SetRowLabelValue( row
, s
);
8827 if ( !GetBatchCount() )
8829 wxRect rect
= CellToRect( row
, 0);
8830 if ( rect
.height
> 0 )
8832 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8834 rect
.width
= m_rowLabelWidth
;
8835 m_rowLabelWin
->Refresh( true, &rect
);
8841 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8845 m_table
->SetColLabelValue( col
, s
);
8846 if ( !GetBatchCount() )
8848 wxRect rect
= CellToRect( 0, col
);
8849 if ( rect
.width
> 0 )
8851 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8853 rect
.height
= m_colLabelHeight
;
8854 m_colLabelWin
->Refresh( true, &rect
);
8860 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8862 if ( m_gridLineColour
!= colour
)
8864 m_gridLineColour
= colour
;
8866 wxClientDC
dc( m_gridWin
);
8868 DrawAllGridLines( dc
, wxRegion() );
8873 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8875 if ( m_cellHighlightColour
!= colour
)
8877 m_cellHighlightColour
= colour
;
8879 wxClientDC
dc( m_gridWin
);
8881 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8882 DrawCellHighlight(dc
, attr
);
8887 void wxGrid::SetCellHighlightPenWidth(int width
)
8889 if (m_cellHighlightPenWidth
!= width
) {
8890 m_cellHighlightPenWidth
= width
;
8892 // Just redrawing the cell highlight is not enough since that won't
8893 // make any visible change if the the thickness is getting smaller.
8894 int row
= m_currentCellCoords
.GetRow();
8895 int col
= m_currentCellCoords
.GetCol();
8896 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8898 wxRect rect
= CellToRect(row
, col
);
8899 m_gridWin
->Refresh(true, &rect
);
8903 void wxGrid::SetCellHighlightROPenWidth(int width
)
8905 if (m_cellHighlightROPenWidth
!= width
) {
8906 m_cellHighlightROPenWidth
= width
;
8908 // Just redrawing the cell highlight is not enough since that won't
8909 // make any visible change if the the thickness is getting smaller.
8910 int row
= m_currentCellCoords
.GetRow();
8911 int col
= m_currentCellCoords
.GetCol();
8912 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8914 wxRect rect
= CellToRect(row
, col
);
8915 m_gridWin
->Refresh(true, &rect
);
8919 void wxGrid::EnableGridLines( bool enable
)
8921 if ( enable
!= m_gridLinesEnabled
)
8923 m_gridLinesEnabled
= enable
;
8925 if ( !GetBatchCount() )
8929 wxClientDC
dc( m_gridWin
);
8931 DrawAllGridLines( dc
, wxRegion() );
8935 m_gridWin
->Refresh();
8942 int wxGrid::GetDefaultRowSize()
8944 return m_defaultRowHeight
;
8947 int wxGrid::GetRowSize( int row
)
8949 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8951 return GetRowHeight(row
);
8954 int wxGrid::GetDefaultColSize()
8956 return m_defaultColWidth
;
8959 int wxGrid::GetColSize( int col
)
8961 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8963 return GetColWidth(col
);
8966 // ============================================================================
8967 // access to the grid attributes: each of them has a default value in the grid
8968 // itself and may be overidden on a per-cell basis
8969 // ============================================================================
8971 // ----------------------------------------------------------------------------
8972 // setting default attributes
8973 // ----------------------------------------------------------------------------
8975 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8977 m_defaultCellAttr
->SetBackgroundColour(col
);
8979 m_gridWin
->SetBackgroundColour(col
);
8983 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8985 m_defaultCellAttr
->SetTextColour(col
);
8988 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8990 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8993 void wxGrid::SetDefaultCellOverflow( bool allow
)
8995 m_defaultCellAttr
->SetOverflow(allow
);
8998 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9000 m_defaultCellAttr
->SetFont(font
);
9004 // For editors and renderers the type registry takes precedence over the
9005 // default attr, so we need to register the new editor/renderer for the string
9006 // data type in order to make setting a default editor/renderer appear to
9009 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9011 RegisterDataType(wxGRID_VALUE_STRING
,
9013 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9016 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9018 RegisterDataType(wxGRID_VALUE_STRING
,
9019 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9023 // ----------------------------------------------------------------------------
9024 // access to the default attrbiutes
9025 // ----------------------------------------------------------------------------
9027 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9029 return m_defaultCellAttr
->GetBackgroundColour();
9032 wxColour
wxGrid::GetDefaultCellTextColour()
9034 return m_defaultCellAttr
->GetTextColour();
9037 wxFont
wxGrid::GetDefaultCellFont()
9039 return m_defaultCellAttr
->GetFont();
9042 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9044 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9047 bool wxGrid::GetDefaultCellOverflow()
9049 return m_defaultCellAttr
->GetOverflow();
9052 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9054 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9057 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9059 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9062 // ----------------------------------------------------------------------------
9063 // access to cell attributes
9064 // ----------------------------------------------------------------------------
9066 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9068 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9069 wxColour colour
= attr
->GetBackgroundColour();
9074 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9076 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9077 wxColour colour
= attr
->GetTextColour();
9082 wxFont
wxGrid::GetCellFont( int row
, int col
)
9084 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9085 wxFont font
= attr
->GetFont();
9090 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9092 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9093 attr
->GetAlignment(horiz
, vert
);
9097 bool wxGrid::GetCellOverflow( int row
, int col
)
9099 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9100 bool allow
= attr
->GetOverflow();
9105 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9107 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9108 attr
->GetSize( num_rows
, num_cols
);
9112 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9114 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9115 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9121 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9123 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9124 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9130 bool wxGrid::IsReadOnly(int row
, int col
) const
9132 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9133 bool isReadOnly
= attr
->IsReadOnly();
9138 // ----------------------------------------------------------------------------
9139 // attribute support: cache, automatic provider creation, ...
9140 // ----------------------------------------------------------------------------
9142 bool wxGrid::CanHaveAttributes()
9149 return m_table
->CanHaveAttributes();
9152 void wxGrid::ClearAttrCache()
9154 if ( m_attrCache
.row
!= -1 )
9156 wxSafeDecRef(m_attrCache
.attr
);
9157 m_attrCache
.attr
= NULL
;
9158 m_attrCache
.row
= -1;
9162 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9166 wxGrid
*self
= (wxGrid
*)this; // const_cast
9168 self
->ClearAttrCache();
9169 self
->m_attrCache
.row
= row
;
9170 self
->m_attrCache
.col
= col
;
9171 self
->m_attrCache
.attr
= attr
;
9176 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9178 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9180 *attr
= m_attrCache
.attr
;
9181 wxSafeIncRef(m_attrCache
.attr
);
9183 #ifdef DEBUG_ATTR_CACHE
9184 gs_nAttrCacheHits
++;
9191 #ifdef DEBUG_ATTR_CACHE
9192 gs_nAttrCacheMisses
++;
9198 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9200 wxGridCellAttr
*attr
= NULL
;
9201 // Additional test to avoid looking at the cache e.g. for
9202 // wxNoCellCoords, as this will confuse memory management.
9205 if ( !LookupAttr(row
, col
, &attr
) )
9207 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9208 : (wxGridCellAttr
*)NULL
;
9209 CacheAttr(row
, col
, attr
);
9214 attr
->SetDefAttr(m_defaultCellAttr
);
9218 attr
= m_defaultCellAttr
;
9225 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9227 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9228 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9230 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9231 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9233 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9236 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9238 // artificially inc the ref count to match DecRef() in caller
9240 m_table
->SetAttr(attr
, row
, col
);
9246 // ----------------------------------------------------------------------------
9247 // setting column attributes (wrappers around SetColAttr)
9248 // ----------------------------------------------------------------------------
9250 void wxGrid::SetColFormatBool(int col
)
9252 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9255 void wxGrid::SetColFormatNumber(int col
)
9257 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9260 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9262 wxString typeName
= wxGRID_VALUE_FLOAT
;
9263 if ( (width
!= -1) || (precision
!= -1) )
9265 typeName
<< _T(':') << width
<< _T(',') << precision
;
9268 SetColFormatCustom(col
, typeName
);
9271 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9273 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9275 attr
= new wxGridCellAttr
;
9276 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9277 attr
->SetRenderer(renderer
);
9279 SetColAttr(col
, attr
);
9283 // ----------------------------------------------------------------------------
9284 // setting cell attributes: this is forwarded to the table
9285 // ----------------------------------------------------------------------------
9287 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9289 if ( CanHaveAttributes() )
9291 m_table
->SetAttr(attr
, row
, col
);
9300 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9302 if ( CanHaveAttributes() )
9304 m_table
->SetRowAttr(attr
, row
);
9313 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9315 if ( CanHaveAttributes() )
9317 m_table
->SetColAttr(attr
, col
);
9326 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9328 if ( CanHaveAttributes() )
9330 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9331 attr
->SetBackgroundColour(colour
);
9336 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9338 if ( CanHaveAttributes() )
9340 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9341 attr
->SetTextColour(colour
);
9346 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9348 if ( CanHaveAttributes() )
9350 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9351 attr
->SetFont(font
);
9356 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9358 if ( CanHaveAttributes() )
9360 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9361 attr
->SetAlignment(horiz
, vert
);
9366 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9368 if ( CanHaveAttributes() )
9370 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9371 attr
->SetOverflow(allow
);
9376 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9378 if ( CanHaveAttributes() )
9380 int cell_rows
, cell_cols
;
9382 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9383 attr
->GetSize(&cell_rows
, &cell_cols
);
9384 attr
->SetSize(num_rows
, num_cols
);
9387 // Cannot set the size of a cell to 0 or negative values
9388 // While it is perfectly legal to do that, this function cannot
9389 // handle all the possibilies, do it by hand by getting the CellAttr.
9390 // You can only set the size of a cell to 1,1 or greater with this fn
9391 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9392 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9393 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9394 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9396 // if this was already a multicell then "turn off" the other cells first
9397 if ((cell_rows
> 1) || (cell_rows
> 1))
9400 for (j
=row
; j
<row
+cell_rows
; j
++)
9402 for (i
=col
; i
<col
+cell_cols
; i
++)
9404 if ((i
!= col
) || (j
!= row
))
9406 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9407 attr_stub
->SetSize( 1, 1 );
9408 attr_stub
->DecRef();
9414 // mark the cells that will be covered by this cell to
9415 // negative or zero values to point back at this cell
9416 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9419 for (j
=row
; j
<row
+num_rows
; j
++)
9421 for (i
=col
; i
<col
+num_cols
; i
++)
9423 if ((i
!= col
) || (j
!= row
))
9425 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9426 attr_stub
->SetSize( row
-j
, col
-i
);
9427 attr_stub
->DecRef();
9435 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9437 if ( CanHaveAttributes() )
9439 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9440 attr
->SetRenderer(renderer
);
9445 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9447 if ( CanHaveAttributes() )
9449 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9450 attr
->SetEditor(editor
);
9455 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9457 if ( CanHaveAttributes() )
9459 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9460 attr
->SetReadOnly(isReadOnly
);
9465 // ----------------------------------------------------------------------------
9466 // Data type registration
9467 // ----------------------------------------------------------------------------
9469 void wxGrid::RegisterDataType(const wxString
& typeName
,
9470 wxGridCellRenderer
* renderer
,
9471 wxGridCellEditor
* editor
)
9473 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9477 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9479 wxString typeName
= m_table
->GetTypeName(row
, col
);
9480 return GetDefaultEditorForType(typeName
);
9483 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9485 wxString typeName
= m_table
->GetTypeName(row
, col
);
9486 return GetDefaultRendererForType(typeName
);
9490 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9492 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9493 if ( index
== wxNOT_FOUND
)
9495 wxFAIL_MSG(wxT("Unknown data type name"));
9500 return m_typeRegistry
->GetEditor(index
);
9504 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9506 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9507 if ( index
== wxNOT_FOUND
)
9509 wxFAIL_MSG(wxT("Unknown data type name"));
9514 return m_typeRegistry
->GetRenderer(index
);
9518 // ----------------------------------------------------------------------------
9520 // ----------------------------------------------------------------------------
9522 void wxGrid::EnableDragRowSize( bool enable
)
9524 m_canDragRowSize
= enable
;
9528 void wxGrid::EnableDragColSize( bool enable
)
9530 m_canDragColSize
= enable
;
9533 void wxGrid::EnableDragGridSize( bool enable
)
9535 m_canDragGridSize
= enable
;
9538 void wxGrid::EnableDragCell( bool enable
)
9540 m_canDragCell
= enable
;
9543 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9545 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9547 if ( resizeExistingRows
)
9549 // since we are resizing all rows to the default row size,
9550 // we can simply clear the row heights and row bottoms
9551 // arrays (which also allows us to take advantage of
9552 // some speed optimisations)
9553 m_rowHeights
.Empty();
9554 m_rowBottoms
.Empty();
9555 if ( !GetBatchCount() )
9560 void wxGrid::SetRowSize( int row
, int height
)
9562 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9564 // See comment in SetColSize
9565 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9567 if ( m_rowHeights
.IsEmpty() )
9569 // need to really create the array
9573 int h
= wxMax( 0, height
);
9574 int diff
= h
- m_rowHeights
[row
];
9576 m_rowHeights
[row
] = h
;
9578 for ( i
= row
; i
< m_numRows
; i
++ )
9580 m_rowBottoms
[i
] += diff
;
9582 if ( !GetBatchCount() )
9586 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9588 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9590 if ( resizeExistingCols
)
9592 // since we are resizing all columns to the default column size,
9593 // we can simply clear the col widths and col rights
9594 // arrays (which also allows us to take advantage of
9595 // some speed optimisations)
9596 m_colWidths
.Empty();
9597 m_colRights
.Empty();
9598 if ( !GetBatchCount() )
9603 void wxGrid::SetColSize( int col
, int width
)
9605 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9607 // should we check that it's bigger than GetColMinimalWidth(col) here?
9609 // No, because it is reasonable to assume the library user know's
9610 // what he is doing. However whe should test against the weaker
9611 // constariant of minimalAcceptableWidth, as this breaks rendering
9613 // This test then fixes sf.net bug #645734
9615 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9617 if ( m_colWidths
.IsEmpty() )
9619 // need to really create the array
9623 // if < 0 calc new width from label
9627 wxArrayString lines
;
9628 wxClientDC
dc(m_colLabelWin
);
9629 dc
.SetFont(GetLabelFont());
9630 StringToLines(GetColLabelValue(col
), lines
);
9631 GetTextBoxSize(dc
, lines
, &w
, &h
);
9634 int w
= wxMax( 0, width
);
9635 int diff
= w
- m_colWidths
[col
];
9636 m_colWidths
[col
] = w
;
9639 for ( i
= col
; i
< m_numCols
; i
++ )
9641 m_colRights
[i
] += diff
;
9643 if ( !GetBatchCount() )
9648 void wxGrid::SetColMinimalWidth( int col
, int width
)
9650 if (width
> GetColMinimalAcceptableWidth()) {
9651 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9652 m_colMinWidths
[key
] = width
;
9656 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9658 if (width
> GetRowMinimalAcceptableHeight()) {
9659 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9660 m_rowMinHeights
[key
] = width
;
9664 int wxGrid::GetColMinimalWidth(int col
) const
9666 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9667 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9668 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9671 int wxGrid::GetRowMinimalHeight(int row
) const
9673 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9674 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9675 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9678 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9680 // We do allow a width of 0 since this gives us
9681 // an easy way to temporarily hidding columns.
9684 m_minAcceptableColWidth
= width
;
9687 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9689 // We do allow a height of 0 since this gives us
9690 // an easy way to temporarily hidding rows.
9693 m_minAcceptableRowHeight
= height
;
9696 int wxGrid::GetColMinimalAcceptableWidth() const
9698 return m_minAcceptableColWidth
;
9701 int wxGrid::GetRowMinimalAcceptableHeight() const
9703 return m_minAcceptableRowHeight
;
9706 // ----------------------------------------------------------------------------
9708 // ----------------------------------------------------------------------------
9710 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9712 wxClientDC
dc(m_gridWin
);
9714 //Cancel editting of cell
9715 HideCellEditControl();
9716 SaveEditControlValue();
9718 // init both of them to avoid compiler warnings, even if weo nly need one
9726 wxCoord extent
, extentMax
= 0;
9727 int max
= column
? m_numRows
: m_numCols
;
9728 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9735 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9736 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9739 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9740 extent
= column
? size
.x
: size
.y
;
9741 if ( extent
> extentMax
)
9752 // now also compare with the column label extent
9754 dc
.SetFont( GetLabelFont() );
9758 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9759 if( GetColLabelTextOrientation() == wxVERTICAL
)
9763 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9765 extent
= column
? w
: h
;
9766 if ( extent
> extentMax
)
9773 // empty column - give default extent (notice that if extentMax is less
9774 // than default extent but != 0, it's ok)
9775 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9781 // leave some space around text
9792 SetColSize(col
, extentMax
);
9793 if ( !GetBatchCount() )
9796 m_gridWin
->GetClientSize( &cw
, &ch
);
9797 wxRect
rect ( CellToRect( 0, col
) );
9799 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9800 rect
.width
= cw
- rect
.x
;
9801 rect
.height
= m_colLabelHeight
;
9802 m_colLabelWin
->Refresh( true, &rect
);
9807 SetRowSize(row
, extentMax
);
9808 if ( !GetBatchCount() )
9811 m_gridWin
->GetClientSize( &cw
, &ch
);
9812 wxRect
rect ( CellToRect( row
, 0 ) );
9814 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9815 rect
.width
= m_rowLabelWidth
;
9816 rect
.height
= ch
- rect
.y
;
9817 m_rowLabelWin
->Refresh( true, &rect
);
9823 SetColMinimalWidth(col
, extentMax
);
9825 SetRowMinimalHeight(row
, extentMax
);
9829 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9831 int width
= m_rowLabelWidth
;
9836 for ( int col
= 0; col
< m_numCols
; col
++ )
9840 AutoSizeColumn(col
, setAsMin
);
9843 width
+= GetColWidth(col
);
9852 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9854 int height
= m_colLabelHeight
;
9859 for ( int row
= 0; row
< m_numRows
; row
++ )
9863 AutoSizeRow(row
, setAsMin
);
9866 height
+= GetRowHeight(row
);
9875 void wxGrid::AutoSize()
9879 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9881 // round up the size to a multiple of scroll step - this ensures that we
9882 // won't get the scrollbars if we're sized exactly to this width
9883 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9885 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9886 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9888 // distribute the extra space between the columns/rows to avoid having
9889 // extra white space
9891 // Remove the extra m_extraWidth + 1 added above
9892 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9893 if ( diff
&& m_numCols
)
9895 // try to resize the columns uniformly
9896 wxCoord diffPerCol
= diff
/ m_numCols
;
9899 for ( int col
= 0; col
< m_numCols
; col
++ )
9901 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9905 // add remaining amount to the last columns
9906 diff
-= diffPerCol
* m_numCols
;
9909 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9911 SetColSize(col
, GetColWidth(col
) + 1);
9917 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9918 if ( diff
&& m_numRows
)
9920 // try to resize the columns uniformly
9921 wxCoord diffPerRow
= diff
/ m_numRows
;
9924 for ( int row
= 0; row
< m_numRows
; row
++ )
9926 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9930 // add remaining amount to the last rows
9931 diff
-= diffPerRow
* m_numRows
;
9934 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9936 SetRowSize(row
, GetRowHeight(row
) + 1);
9943 SetClientSize(sizeFit
);
9946 void wxGrid::AutoSizeRowLabelSize( int row
)
9948 wxArrayString lines
;
9951 // Hide the edit control, so it
9952 // won't interfer with drag-shrinking.
9953 if( IsCellEditControlShown() )
9955 HideCellEditControl();
9956 SaveEditControlValue();
9959 // autosize row height depending on label text
9960 StringToLines( GetRowLabelValue( row
), lines
);
9961 wxClientDC
dc( m_rowLabelWin
);
9962 GetTextBoxSize( dc
, lines
, &w
, &h
);
9963 if( h
< m_defaultRowHeight
)
9964 h
= m_defaultRowHeight
;
9969 void wxGrid::AutoSizeColLabelSize( int col
)
9971 wxArrayString lines
;
9974 // Hide the edit control, so it
9975 // won't interfer with drag-shrinking.
9976 if( IsCellEditControlShown() )
9978 HideCellEditControl();
9979 SaveEditControlValue();
9982 // autosize column width depending on label text
9983 StringToLines( GetColLabelValue( col
), lines
);
9984 wxClientDC
dc( m_colLabelWin
);
9985 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9986 GetTextBoxSize( dc
, lines
, &w
, &h
);
9988 GetTextBoxSize( dc
, lines
, &h
, &w
);
9989 if( w
< m_defaultColWidth
)
9990 w
= m_defaultColWidth
;
9995 wxSize
wxGrid::DoGetBestSize() const
9997 // don't set sizes, only calculate them
9998 wxGrid
*self
= (wxGrid
*)this; // const_cast
10001 width
= self
->SetOrCalcColumnSizes(true);
10002 height
= self
->SetOrCalcRowSizes(true);
10004 if (!width
) width
=100;
10005 if (!height
) height
=80;
10007 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10008 // of the scrollbars, is there any magic incantaion for that?
10010 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10012 width
+= 1 + xpu
- (width
% xpu
);
10014 height
+= 1 + ypu
- (height
% ypu
);
10016 // limit to 1/4 of the screen size
10017 int maxwidth
, maxheight
;
10018 wxDisplaySize( & maxwidth
, & maxheight
);
10021 if ( width
> maxwidth
) width
= maxwidth
;
10022 if ( height
> maxheight
) height
= maxheight
;
10025 wxSize
best(width
, height
);
10026 // NOTE: This size should be cached, but first we need to add calls to
10027 // InvalidateBestSize everywhere that could change the results of this
10029 // CacheBestSize(size);
10039 wxPen
& wxGrid::GetDividerPen() const
10044 // ----------------------------------------------------------------------------
10045 // cell value accessor functions
10046 // ----------------------------------------------------------------------------
10048 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10052 m_table
->SetValue( row
, col
, s
);
10053 if ( !GetBatchCount() )
10056 wxRect
rect( CellToRect( row
, col
) );
10058 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10059 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10060 m_gridWin
->Refresh( false, &rect
);
10063 if ( m_currentCellCoords
.GetRow() == row
&&
10064 m_currentCellCoords
.GetCol() == col
&&
10065 IsCellEditControlShown())
10066 // Note: If we are using IsCellEditControlEnabled,
10067 // this interacts badly with calling SetCellValue from
10068 // an EVT_GRID_CELL_CHANGE handler.
10070 HideCellEditControl();
10071 ShowCellEditControl(); // will reread data from table
10078 // ------ Block, row and col selection
10081 void wxGrid::SelectRow( int row
, bool addToSelected
)
10083 if ( IsSelection() && !addToSelected
)
10087 m_selection
->SelectRow( row
, false, addToSelected
);
10091 void wxGrid::SelectCol( int col
, bool addToSelected
)
10093 if ( IsSelection() && !addToSelected
)
10097 m_selection
->SelectCol( col
, false, addToSelected
);
10101 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10102 bool addToSelected
)
10104 if ( IsSelection() && !addToSelected
)
10108 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10109 false, addToSelected
);
10113 void wxGrid::SelectAll()
10115 if ( m_numRows
> 0 && m_numCols
> 0 )
10118 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10123 // ------ Cell, row and col deselection
10126 void wxGrid::DeselectRow( int row
)
10128 if ( !m_selection
)
10131 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10133 if ( m_selection
->IsInSelection(row
, 0 ) )
10134 m_selection
->ToggleCellSelection( row
, 0);
10138 int nCols
= GetNumberCols();
10139 for ( int i
= 0; i
< nCols
; i
++ )
10141 if ( m_selection
->IsInSelection(row
, i
) )
10142 m_selection
->ToggleCellSelection( row
, i
);
10147 void wxGrid::DeselectCol( int col
)
10149 if ( !m_selection
)
10152 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10154 if ( m_selection
->IsInSelection(0, col
) )
10155 m_selection
->ToggleCellSelection( 0, col
);
10159 int nRows
= GetNumberRows();
10160 for ( int i
= 0; i
< nRows
; i
++ )
10162 if ( m_selection
->IsInSelection(i
, col
) )
10163 m_selection
->ToggleCellSelection(i
, col
);
10168 void wxGrid::DeselectCell( int row
, int col
)
10170 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10171 m_selection
->ToggleCellSelection(row
, col
);
10174 bool wxGrid::IsSelection()
10176 return ( m_selection
&& (m_selection
->IsSelection() ||
10177 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10178 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10181 bool wxGrid::IsInSelection( int row
, int col
) const
10183 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10184 ( row
>= m_selectingTopLeft
.GetRow() &&
10185 col
>= m_selectingTopLeft
.GetCol() &&
10186 row
<= m_selectingBottomRight
.GetRow() &&
10187 col
<= m_selectingBottomRight
.GetCol() )) );
10190 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10192 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10193 return m_selection
->m_cellSelection
;
10195 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10197 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10198 return m_selection
->m_blockSelectionTopLeft
;
10200 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10202 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10203 return m_selection
->m_blockSelectionBottomRight
;
10205 wxArrayInt
wxGrid::GetSelectedRows() const
10207 if (!m_selection
) { wxArrayInt a
; return a
; }
10208 return m_selection
->m_rowSelection
;
10210 wxArrayInt
wxGrid::GetSelectedCols() const
10212 if (!m_selection
) { wxArrayInt a
; return a
; }
10213 return m_selection
->m_colSelection
;
10217 void wxGrid::ClearSelection()
10219 m_selectingTopLeft
= wxGridNoCellCoords
;
10220 m_selectingBottomRight
= wxGridNoCellCoords
;
10222 m_selection
->ClearSelection();
10226 // This function returns the rectangle that encloses the given block
10227 // in device coords clipped to the client size of the grid window.
10229 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10230 const wxGridCellCoords
&bottomRight
)
10232 wxRect
rect( wxGridNoCellRect
);
10235 cellRect
= CellToRect( topLeft
);
10236 if ( cellRect
!= wxGridNoCellRect
)
10242 rect
= wxRect(0,0,0,0);
10245 cellRect
= CellToRect( bottomRight
);
10246 if ( cellRect
!= wxGridNoCellRect
)
10252 return wxGridNoCellRect
;
10256 int left
= rect
.GetLeft();
10257 int top
= rect
.GetTop();
10258 int right
= rect
.GetRight();
10259 int bottom
= rect
.GetBottom();
10261 int leftCol
= topLeft
.GetCol();
10262 int topRow
= topLeft
.GetRow();
10263 int rightCol
= bottomRight
.GetCol();
10264 int bottomRow
= bottomRight
.GetRow();
10282 topRow
= bottomRow
;
10287 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10289 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10291 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10293 cellRect
= CellToRect( j
, i
);
10295 if (cellRect
.x
< left
)
10297 if (cellRect
.y
< top
)
10299 if (cellRect
.x
+ cellRect
.width
> right
)
10300 right
= cellRect
.x
+ cellRect
.width
;
10301 if (cellRect
.y
+ cellRect
.height
> bottom
)
10302 bottom
= cellRect
.y
+ cellRect
.height
;
10304 else i
= rightCol
; // jump over inner cells.
10308 // convert to scrolled coords
10310 CalcScrolledPosition( left
, top
, &left
, &top
);
10311 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10314 m_gridWin
->GetClientSize( &cw
, &ch
);
10316 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10317 return wxRect(0,0,0,0);
10319 rect
.SetLeft( wxMax(0, left
) );
10320 rect
.SetTop( wxMax(0, top
) );
10321 rect
.SetRight( wxMin(cw
, right
) );
10322 rect
.SetBottom( wxMin(ch
, bottom
) );
10328 // ------ Grid event classes
10331 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10333 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10334 int row
, int col
, int x
, int y
, bool sel
,
10335 bool control
, bool shift
, bool alt
, bool meta
)
10336 : wxNotifyEvent( type
, id
)
10343 m_control
= control
;
10348 SetEventObject(obj
);
10352 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10354 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10355 int rowOrCol
, int x
, int y
,
10356 bool control
, bool shift
, bool alt
, bool meta
)
10357 : wxNotifyEvent( type
, id
)
10359 m_rowOrCol
= rowOrCol
;
10362 m_control
= control
;
10367 SetEventObject(obj
);
10371 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10373 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10374 const wxGridCellCoords
& topLeft
,
10375 const wxGridCellCoords
& bottomRight
,
10376 bool sel
, bool control
,
10377 bool shift
, bool alt
, bool meta
)
10378 : wxNotifyEvent( type
, id
)
10380 m_topLeft
= topLeft
;
10381 m_bottomRight
= bottomRight
;
10383 m_control
= control
;
10388 SetEventObject(obj
);
10392 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10394 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10395 wxObject
* obj
, int row
,
10396 int col
, wxControl
* ctrl
)
10397 : wxCommandEvent(type
, id
)
10399 SetEventObject(obj
);
10405 #endif // wxUSE_GRID