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 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3764 m_owner
->ProcessGridCellMouseEvent( event
);
3767 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3769 m_owner
->GetEventHandler()->ProcessEvent(event
);
3772 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3773 // cursor must be in the cell edit control to get key events
3775 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3777 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3780 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3782 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3785 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3787 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3790 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3794 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3796 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3800 //////////////////////////////////////////////////////////////////////
3802 // Internal Helper function for computing row or column from some
3803 // (unscrolled) coordinate value, using either
3804 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3805 // of m_rowBottoms/m_ColRights to speed up the search!
3807 // Internal helper macros for simpler use of that function
3809 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3810 const wxArrayInt
& BorderArray
, int nMax
,
3813 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3814 m_minAcceptableColWidth, \
3815 m_colRights, m_numCols, true)
3816 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3817 m_minAcceptableRowHeight, \
3818 m_rowBottoms, m_numRows, true)
3819 /////////////////////////////////////////////////////////////////////
3821 #if wxUSE_EXTENDED_RTTI
3822 WX_DEFINE_FLAGS( wxGridStyle
)
3824 wxBEGIN_FLAGS( wxGridStyle
)
3825 // new style border flags, we put them first to
3826 // use them for streaming out
3827 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3828 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3829 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3830 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3831 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3832 wxFLAGS_MEMBER(wxBORDER_NONE
)
3834 // old style border flags
3835 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3836 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3837 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3838 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3839 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3840 wxFLAGS_MEMBER(wxBORDER
)
3842 // standard window styles
3843 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3844 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3845 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3846 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3847 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3848 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3849 wxFLAGS_MEMBER(wxVSCROLL
)
3850 wxFLAGS_MEMBER(wxHSCROLL
)
3852 wxEND_FLAGS( wxGridStyle
)
3854 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3856 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3857 wxHIDE_PROPERTY( Children
)
3858 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3859 wxEND_PROPERTIES_TABLE()
3861 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3862 wxEND_HANDLERS_TABLE()
3864 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3867 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3870 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3873 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3874 EVT_PAINT( wxGrid::OnPaint
)
3875 EVT_SIZE( wxGrid::OnSize
)
3876 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3877 EVT_KEY_UP( wxGrid::OnKeyUp
)
3878 EVT_CHAR ( wxGrid::OnChar
)
3879 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3884 // in order to make sure that a size event is not
3885 // trigerred in a unfinished state
3886 m_cornerLabelWin
= NULL
;
3887 m_rowLabelWin
= NULL
;
3888 m_colLabelWin
= NULL
;
3892 wxGrid::wxGrid( wxWindow
*parent
,
3897 const wxString
& name
)
3898 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3899 m_colMinWidths(GRID_HASH_SIZE
),
3900 m_rowMinHeights(GRID_HASH_SIZE
)
3903 SetBestFittingSize(size
);
3906 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3907 const wxPoint
& pos
, const wxSize
& size
,
3908 long style
, const wxString
& name
)
3910 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3911 style
| wxWANTS_CHARS
, name
))
3914 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3915 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3918 SetBestFittingSize(size
);
3926 // Must do this or ~wxScrollHelper will pop the wrong event handler
3927 SetTargetWindow(this);
3929 wxSafeDecRef(m_defaultCellAttr
);
3931 #ifdef DEBUG_ATTR_CACHE
3932 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3933 wxPrintf(_T("wxGrid attribute cache statistics: "
3934 "total: %u, hits: %u (%u%%)\n"),
3935 total
, gs_nAttrCacheHits
,
3936 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3942 delete m_typeRegistry
;
3948 // ----- internal init and update functions
3951 // NOTE: If using the default visual attributes works everywhere then this can
3952 // be removed as well as the #else cases below.
3953 #define _USE_VISATTR 0
3956 #include "wx/listbox.h"
3959 void wxGrid::Create()
3961 m_created
= false; // set to true by CreateGrid
3963 m_table
= (wxGridTableBase
*) NULL
;
3966 m_cellEditCtrlEnabled
= false;
3968 m_defaultCellAttr
= new wxGridCellAttr();
3970 // Set default cell attributes
3971 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3972 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3973 m_defaultCellAttr
->SetFont(GetFont());
3974 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3975 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3976 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3979 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3980 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3982 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3983 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3986 m_defaultCellAttr
->SetTextColour(
3987 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3988 m_defaultCellAttr
->SetBackgroundColour(
3989 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3994 m_currentCellCoords
= wxGridNoCellCoords
;
3996 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3997 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3999 // create the type registry
4000 m_typeRegistry
= new wxGridTypeRegistry
;
4003 // subwindow components that make up the wxGrid
4004 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4009 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4014 m_colLabelWin
= new wxGridColLabelWindow( this,
4019 m_gridWin
= new wxGridWindow( this,
4026 SetTargetWindow( m_gridWin
);
4029 wxColour gfg
= gva
.colFg
;
4030 wxColour gbg
= gva
.colBg
;
4031 wxColour lfg
= lva
.colFg
;
4032 wxColour lbg
= lva
.colBg
;
4034 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4035 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4036 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4037 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4039 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4040 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4041 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4042 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4043 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4044 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4046 m_gridWin
->SetOwnForegroundColour(gfg
);
4047 m_gridWin
->SetOwnBackgroundColour(gbg
);
4053 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4054 wxGrid::wxGridSelectionModes selmode
)
4056 wxCHECK_MSG( !m_created
,
4058 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4060 m_numRows
= numRows
;
4061 m_numCols
= numCols
;
4063 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4064 m_table
->SetView( this );
4066 m_selection
= new wxGridSelection( this, selmode
);
4075 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4077 wxCHECK_RET( m_created
,
4078 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4080 m_selection
->SetSelectionMode( selmode
);
4083 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4085 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4086 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4088 return m_selection
->GetSelectionMode();
4091 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4092 wxGrid::wxGridSelectionModes selmode
)
4096 // stop all processing
4101 wxGridTableBase
*t
=m_table
;
4114 m_numRows
= table
->GetNumberRows();
4115 m_numCols
= table
->GetNumberCols();
4118 m_table
->SetView( this );
4121 m_selection
= new wxGridSelection( this, selmode
);
4134 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4135 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4137 if ( m_rowLabelWin
)
4139 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4143 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4146 m_labelTextColour
= wxColour( _T("BLACK") );
4149 m_attrCache
.row
= -1;
4150 m_attrCache
.col
= -1;
4151 m_attrCache
.attr
= NULL
;
4153 // TODO: something better than this ?
4155 m_labelFont
= this->GetFont();
4156 m_labelFont
.SetWeight( wxBOLD
);
4158 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4159 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4161 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4162 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4163 m_colLabelTextOrientation
= wxHORIZONTAL
;
4165 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4166 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4168 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4169 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4171 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4172 m_defaultRowHeight
+= 8;
4174 m_defaultRowHeight
+= 4;
4177 m_gridLineColour
= wxColour( 192,192,192 );
4178 m_gridLinesEnabled
= true;
4179 m_cellHighlightColour
= *wxBLACK
;
4180 m_cellHighlightPenWidth
= 2;
4181 m_cellHighlightROPenWidth
= 1;
4183 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4184 m_winCapture
= (wxWindow
*)NULL
;
4185 m_canDragRowSize
= true;
4186 m_canDragColSize
= true;
4187 m_canDragGridSize
= true;
4188 m_canDragCell
= false;
4190 m_dragRowOrCol
= -1;
4191 m_isDragging
= false;
4192 m_startDragPos
= wxDefaultPosition
;
4194 m_waitForSlowClick
= false;
4196 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4197 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4199 m_currentCellCoords
= wxGridNoCellCoords
;
4201 m_selectingTopLeft
= wxGridNoCellCoords
;
4202 m_selectingBottomRight
= wxGridNoCellCoords
;
4203 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4204 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4206 m_editable
= true; // default for whole grid
4208 m_inOnKeyDown
= false;
4214 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4215 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4218 // ----------------------------------------------------------------------------
4219 // the idea is to call these functions only when necessary because they create
4220 // quite big arrays which eat memory mostly unnecessary - in particular, if
4221 // default widths/heights are used for all rows/columns, we may not use these
4224 // with some extra code, it should be possible to only store the
4225 // widths/heights different from default ones but this will be done later...
4226 // ----------------------------------------------------------------------------
4228 void wxGrid::InitRowHeights()
4230 m_rowHeights
.Empty();
4231 m_rowBottoms
.Empty();
4233 m_rowHeights
.Alloc( m_numRows
);
4234 m_rowBottoms
.Alloc( m_numRows
);
4238 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4240 for ( int i
= 0; i
< m_numRows
; i
++ )
4242 rowBottom
+= m_defaultRowHeight
;
4243 m_rowBottoms
.Add( rowBottom
);
4247 void wxGrid::InitColWidths()
4249 m_colWidths
.Empty();
4250 m_colRights
.Empty();
4252 m_colWidths
.Alloc( m_numCols
);
4253 m_colRights
.Alloc( m_numCols
);
4256 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4258 for ( int i
= 0; i
< m_numCols
; i
++ )
4260 colRight
+= m_defaultColWidth
;
4261 m_colRights
.Add( colRight
);
4265 int wxGrid::GetColWidth(int col
) const
4267 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4270 int wxGrid::GetColLeft(int col
) const
4272 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4273 : m_colRights
[col
] - m_colWidths
[col
];
4276 int wxGrid::GetColRight(int col
) const
4278 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4282 int wxGrid::GetRowHeight(int row
) const
4284 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4287 int wxGrid::GetRowTop(int row
) const
4289 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4290 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4293 int wxGrid::GetRowBottom(int row
) const
4295 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4296 : m_rowBottoms
[row
];
4299 void wxGrid::CalcDimensions()
4302 GetClientSize( &cw
, &ch
);
4304 if ( m_rowLabelWin
->IsShown() )
4305 cw
-= m_rowLabelWidth
;
4306 if ( m_colLabelWin
->IsShown() )
4307 ch
-= m_colLabelHeight
;
4310 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4311 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4313 // take into account editor if shown
4314 if( IsCellEditControlShown() )
4317 int r
= m_currentCellCoords
.GetRow();
4318 int c
= m_currentCellCoords
.GetCol();
4319 int x
= GetColLeft(c
);
4320 int y
= GetRowTop(r
);
4322 // how big is the editor
4323 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4324 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4325 editor
->GetControl()->GetSize(&w2
, &h2
);
4328 if( w2
> w
) w
= w2
;
4329 if( h2
> h
) h
= h2
;
4334 // preserve (more or less) the previous position
4336 GetViewStart( &x
, &y
);
4338 // ensure the position is valid for the new scroll ranges
4340 x
= wxMax( w
- 1, 0 );
4342 y
= wxMax( h
- 1, 0 );
4344 // do set scrollbar parameters
4345 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4346 GetScrollX(w
), GetScrollY(h
), x
, y
,
4347 GetBatchCount() != 0);
4349 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4350 // still must reposition the children
4355 void wxGrid::CalcWindowSizes()
4357 // escape if the window is has not been fully created yet
4359 if ( m_cornerLabelWin
== NULL
)
4363 GetClientSize( &cw
, &ch
);
4365 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4366 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4368 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4369 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4371 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4372 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4374 if ( m_gridWin
&& m_gridWin
->IsShown() )
4375 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4379 // this is called when the grid table sends a message to say that it
4380 // has been redimensioned
4382 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4385 bool result
= false;
4387 // Clear the attribute cache as the attribute might refer to a different
4388 // cell than stored in the cache after adding/removing rows/columns.
4390 // By the same reasoning, the editor should be dismissed if columns are
4391 // added or removed. And for consistency, it should IMHO always be
4392 // removed, not only if the cell "underneath" it actually changes.
4393 // For now, I intentionally do not save the editor's content as the
4394 // cell it might want to save that stuff to might no longer exist.
4395 HideCellEditControl();
4397 // if we were using the default widths/heights so far, we must change them
4399 if ( m_colWidths
.IsEmpty() )
4404 if ( m_rowHeights
.IsEmpty() )
4410 switch ( msg
.GetId() )
4412 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4414 size_t pos
= msg
.GetCommandInt();
4415 int numRows
= msg
.GetCommandInt2();
4417 m_numRows
+= numRows
;
4419 if ( !m_rowHeights
.IsEmpty() )
4421 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4422 m_rowBottoms
.Insert( 0, pos
, numRows
);
4425 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4427 for ( i
= pos
; i
< m_numRows
; i
++ )
4429 bottom
+= m_rowHeights
[i
];
4430 m_rowBottoms
[i
] = bottom
;
4433 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4435 // if we have just inserted cols into an empty grid the current
4436 // cell will be undefined...
4438 SetCurrentCell( 0, 0 );
4442 m_selection
->UpdateRows( pos
, numRows
);
4443 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4445 attrProvider
->UpdateAttrRows( pos
, numRows
);
4447 if ( !GetBatchCount() )
4450 m_rowLabelWin
->Refresh();
4456 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4458 int numRows
= msg
.GetCommandInt();
4459 int oldNumRows
= m_numRows
;
4460 m_numRows
+= numRows
;
4462 if ( !m_rowHeights
.IsEmpty() )
4464 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4465 m_rowBottoms
.Add( 0, numRows
);
4468 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4470 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4472 bottom
+= m_rowHeights
[i
];
4473 m_rowBottoms
[i
] = bottom
;
4476 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4478 // if we have just inserted cols into an empty grid the current
4479 // cell will be undefined...
4481 SetCurrentCell( 0, 0 );
4483 if ( !GetBatchCount() )
4486 m_rowLabelWin
->Refresh();
4492 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4494 size_t pos
= msg
.GetCommandInt();
4495 int numRows
= msg
.GetCommandInt2();
4496 m_numRows
-= numRows
;
4498 if ( !m_rowHeights
.IsEmpty() )
4500 m_rowHeights
.RemoveAt( pos
, numRows
);
4501 m_rowBottoms
.RemoveAt( pos
, numRows
);
4504 for ( i
= 0; i
< m_numRows
; i
++ )
4506 h
+= m_rowHeights
[i
];
4507 m_rowBottoms
[i
] = h
;
4512 m_currentCellCoords
= wxGridNoCellCoords
;
4516 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4517 m_currentCellCoords
.Set( 0, 0 );
4521 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4522 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4524 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4525 // ifdef'd out following patch from Paul Gammans
4527 // No need to touch column attributes, unless we
4528 // removed _all_ rows, in this case, we remove
4529 // all column attributes.
4530 // I hate to do this here, but the
4531 // needed data is not available inside UpdateAttrRows.
4532 if ( !GetNumberRows() )
4533 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4536 if ( !GetBatchCount() )
4539 m_rowLabelWin
->Refresh();
4545 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4547 size_t pos
= msg
.GetCommandInt();
4548 int numCols
= msg
.GetCommandInt2();
4549 m_numCols
+= numCols
;
4551 if ( !m_colWidths
.IsEmpty() )
4553 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4554 m_colRights
.Insert( 0, pos
, numCols
);
4557 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4559 for ( i
= pos
; i
< m_numCols
; i
++ )
4561 right
+= m_colWidths
[i
];
4562 m_colRights
[i
] = right
;
4565 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4567 // if we have just inserted cols into an empty grid the current
4568 // cell will be undefined...
4570 SetCurrentCell( 0, 0 );
4574 m_selection
->UpdateCols( pos
, numCols
);
4575 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4577 attrProvider
->UpdateAttrCols( pos
, numCols
);
4578 if ( !GetBatchCount() )
4581 m_colLabelWin
->Refresh();
4588 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4590 int numCols
= msg
.GetCommandInt();
4591 int oldNumCols
= m_numCols
;
4592 m_numCols
+= numCols
;
4593 if ( !m_colWidths
.IsEmpty() )
4595 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4596 m_colRights
.Add( 0, numCols
);
4599 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4601 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4603 right
+= m_colWidths
[i
];
4604 m_colRights
[i
] = right
;
4607 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4609 // if we have just inserted cols into an empty grid the current
4610 // cell will be undefined...
4612 SetCurrentCell( 0, 0 );
4614 if ( !GetBatchCount() )
4617 m_colLabelWin
->Refresh();
4623 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4625 size_t pos
= msg
.GetCommandInt();
4626 int numCols
= msg
.GetCommandInt2();
4627 m_numCols
-= numCols
;
4629 if ( !m_colWidths
.IsEmpty() )
4631 m_colWidths
.RemoveAt( pos
, numCols
);
4632 m_colRights
.RemoveAt( pos
, numCols
);
4635 for ( i
= 0; i
< m_numCols
; i
++ )
4637 w
+= m_colWidths
[i
];
4643 m_currentCellCoords
= wxGridNoCellCoords
;
4647 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4648 m_currentCellCoords
.Set( 0, 0 );
4652 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4653 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4655 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4656 // ifdef'd out following patch from Paul Gammans
4658 // No need to touch row attributes, unless we
4659 // removed _all_ columns, in this case, we remove
4660 // all row attributes.
4661 // I hate to do this here, but the
4662 // needed data is not available inside UpdateAttrCols.
4663 if ( !GetNumberCols() )
4664 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4667 if ( !GetBatchCount() )
4670 m_colLabelWin
->Refresh();
4677 if (result
&& !GetBatchCount() )
4678 m_gridWin
->Refresh();
4683 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4685 wxRegionIterator
iter( reg
);
4688 wxArrayInt rowlabels
;
4695 // TODO: remove this when we can...
4696 // There is a bug in wxMotif that gives garbage update
4697 // rectangles if you jump-scroll a long way by clicking the
4698 // scrollbar with middle button. This is a work-around
4700 #if defined(__WXMOTIF__)
4702 m_gridWin
->GetClientSize( &cw
, &ch
);
4703 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4704 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4707 // logical bounds of update region
4710 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4711 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4713 // find the row labels within these bounds
4716 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4718 if ( GetRowBottom(row
) < top
)
4721 if ( GetRowTop(row
) > bottom
)
4724 rowlabels
.Add( row
);
4734 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4736 wxRegionIterator
iter( reg
);
4739 wxArrayInt colLabels
;
4746 // TODO: remove this when we can...
4747 // There is a bug in wxMotif that gives garbage update
4748 // rectangles if you jump-scroll a long way by clicking the
4749 // scrollbar with middle button. This is a work-around
4751 #if defined(__WXMOTIF__)
4753 m_gridWin
->GetClientSize( &cw
, &ch
);
4754 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4755 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4758 // logical bounds of update region
4761 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4762 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4764 // find the cells within these bounds
4767 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4769 if ( GetColRight(col
) < left
)
4772 if ( GetColLeft(col
) > right
)
4775 colLabels
.Add( col
);
4784 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4786 wxRegionIterator
iter( reg
);
4789 wxGridCellCoordsArray cellsExposed
;
4791 int left
, top
, right
, bottom
;
4796 // TODO: remove this when we can...
4797 // There is a bug in wxMotif that gives garbage update
4798 // rectangles if you jump-scroll a long way by clicking the
4799 // scrollbar with middle button. This is a work-around
4801 #if defined(__WXMOTIF__)
4803 m_gridWin
->GetClientSize( &cw
, &ch
);
4804 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4805 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4806 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4807 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4810 // logical bounds of update region
4812 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4813 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4815 // find the cells within these bounds
4818 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4820 if ( GetRowBottom(row
) <= top
)
4823 if ( GetRowTop(row
) > bottom
)
4826 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4828 if ( GetColRight(col
) <= left
)
4831 if ( GetColLeft(col
) > right
)
4834 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4841 return cellsExposed
;
4845 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4848 wxPoint
pos( event
.GetPosition() );
4849 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4851 if ( event
.Dragging() )
4855 m_isDragging
= true;
4856 m_rowLabelWin
->CaptureMouse();
4859 if ( event
.LeftIsDown() )
4861 switch( m_cursorMode
)
4863 case WXGRID_CURSOR_RESIZE_ROW
:
4865 int cw
, ch
, left
, dummy
;
4866 m_gridWin
->GetClientSize( &cw
, &ch
);
4867 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4869 wxClientDC
dc( m_gridWin
);
4872 GetRowTop(m_dragRowOrCol
) +
4873 GetRowMinimalHeight(m_dragRowOrCol
) );
4874 dc
.SetLogicalFunction(wxINVERT
);
4875 if ( m_dragLastPos
>= 0 )
4877 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4879 dc
.DrawLine( left
, y
, left
+cw
, y
);
4884 case WXGRID_CURSOR_SELECT_ROW
:
4885 if ( (row
= YToRow( y
)) >= 0 )
4889 m_selection
->SelectRow( row
,
4890 event
.ControlDown(),
4897 // default label to suppress warnings about "enumeration value
4898 // 'xxx' not handled in switch
4906 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4911 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4912 m_isDragging
= false;
4915 // ------------ Entering or leaving the window
4917 if ( event
.Entering() || event
.Leaving() )
4919 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4923 // ------------ Left button pressed
4925 else if ( event
.LeftDown() )
4927 // don't send a label click event for a hit on the
4928 // edge of the row label - this is probably the user
4929 // wanting to resize the row
4931 if ( YToEdgeOfRow(y
) < 0 )
4935 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4937 if ( !event
.ShiftDown() && !event
.ControlDown() )
4941 if ( event
.ShiftDown() )
4943 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4946 GetNumberCols() - 1,
4947 event
.ControlDown(),
4954 m_selection
->SelectRow( row
,
4955 event
.ControlDown(),
4962 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4967 // starting to drag-resize a row
4969 if ( CanDragRowSize() )
4970 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4975 // ------------ Left double click
4977 else if (event
.LeftDClick() )
4979 int row
= YToEdgeOfRow(y
);
4984 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4986 // no default action at the moment
4991 // adjust row height depending on label text
4992 AutoSizeRowLabelSize( row
);
4994 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5000 // ------------ Left button released
5002 else if ( event
.LeftUp() )
5004 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5006 DoEndDragResizeRow();
5008 // Note: we are ending the event *after* doing
5009 // default processing in this case
5011 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5014 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5019 // ------------ Right button down
5021 else if ( event
.RightDown() )
5025 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5027 // no default action at the moment
5032 // ------------ Right double click
5034 else if ( event
.RightDClick() )
5038 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5040 // no default action at the moment
5045 // ------------ No buttons down and mouse moving
5047 else if ( event
.Moving() )
5049 m_dragRowOrCol
= YToEdgeOfRow( y
);
5050 if ( m_dragRowOrCol
>= 0 )
5052 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5054 // don't capture the mouse yet
5055 if ( CanDragRowSize() )
5056 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5059 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5061 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5067 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5070 wxPoint
pos( event
.GetPosition() );
5071 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5073 if ( event
.Dragging() )
5077 m_isDragging
= true;
5078 m_colLabelWin
->CaptureMouse();
5081 if ( event
.LeftIsDown() )
5083 switch( m_cursorMode
)
5085 case WXGRID_CURSOR_RESIZE_COL
:
5087 int cw
, ch
, dummy
, top
;
5088 m_gridWin
->GetClientSize( &cw
, &ch
);
5089 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5091 wxClientDC
dc( m_gridWin
);
5094 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5095 GetColMinimalWidth(m_dragRowOrCol
));
5096 dc
.SetLogicalFunction(wxINVERT
);
5097 if ( m_dragLastPos
>= 0 )
5099 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5101 dc
.DrawLine( x
, top
, x
, top
+ch
);
5106 case WXGRID_CURSOR_SELECT_COL
:
5107 if ( (col
= XToCol( x
)) >= 0 )
5111 m_selection
->SelectCol( col
,
5112 event
.ControlDown(),
5119 // default label to suppress warnings about "enumeration value
5120 // 'xxx' not handled in switch
5128 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5133 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5134 m_isDragging
= false;
5137 // ------------ Entering or leaving the window
5139 if ( event
.Entering() || event
.Leaving() )
5141 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5145 // ------------ Left button pressed
5147 else if ( event
.LeftDown() )
5149 // don't send a label click event for a hit on the
5150 // edge of the col label - this is probably the user
5151 // wanting to resize the col
5153 if ( XToEdgeOfCol(x
) < 0 )
5157 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5159 if ( !event
.ShiftDown() && !event
.ControlDown() )
5163 if ( event
.ShiftDown() )
5165 m_selection
->SelectBlock( 0,
5166 m_currentCellCoords
.GetCol(),
5167 GetNumberRows() - 1, col
,
5168 event
.ControlDown(),
5175 m_selection
->SelectCol( col
,
5176 event
.ControlDown(),
5183 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5188 // starting to drag-resize a col
5190 if ( CanDragColSize() )
5191 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5196 // ------------ Left double click
5198 if ( event
.LeftDClick() )
5200 int col
= XToEdgeOfCol(x
);
5205 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5207 // no default action at the moment
5212 // adjust column width depending on label text
5213 AutoSizeColLabelSize( col
);
5215 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5221 // ------------ Left button released
5223 else if ( event
.LeftUp() )
5225 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5227 DoEndDragResizeCol();
5229 // Note: we are ending the event *after* doing
5230 // default processing in this case
5232 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5235 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5240 // ------------ Right button down
5242 else if ( event
.RightDown() )
5246 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5248 // no default action at the moment
5253 // ------------ Right double click
5255 else if ( event
.RightDClick() )
5259 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5261 // no default action at the moment
5266 // ------------ No buttons down and mouse moving
5268 else if ( event
.Moving() )
5270 m_dragRowOrCol
= XToEdgeOfCol( x
);
5271 if ( m_dragRowOrCol
>= 0 )
5273 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5275 // don't capture the cursor yet
5276 if ( CanDragColSize() )
5277 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5280 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5282 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5288 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5290 if ( event
.LeftDown() )
5292 // indicate corner label by having both row and
5295 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5301 else if ( event
.LeftDClick() )
5303 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5306 else if ( event
.RightDown() )
5308 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5310 // no default action at the moment
5314 else if ( event
.RightDClick() )
5316 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5318 // no default action at the moment
5323 void wxGrid::ChangeCursorMode(CursorMode mode
,
5328 static const wxChar
*cursorModes
[] =
5337 wxLogTrace(_T("grid"),
5338 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5339 win
== m_colLabelWin
? _T("colLabelWin")
5340 : win
? _T("rowLabelWin")
5342 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5343 #endif // __WXDEBUG__
5345 if ( mode
== m_cursorMode
&&
5346 win
== m_winCapture
&&
5347 captureMouse
== (m_winCapture
!= NULL
))
5352 // by default use the grid itself
5358 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5359 m_winCapture
= (wxWindow
*)NULL
;
5362 m_cursorMode
= mode
;
5364 switch ( m_cursorMode
)
5366 case WXGRID_CURSOR_RESIZE_ROW
:
5367 win
->SetCursor( m_rowResizeCursor
);
5370 case WXGRID_CURSOR_RESIZE_COL
:
5371 win
->SetCursor( m_colResizeCursor
);
5375 win
->SetCursor( *wxSTANDARD_CURSOR
);
5378 // we need to capture mouse when resizing
5379 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5380 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5382 if ( captureMouse
&& resize
)
5384 win
->CaptureMouse();
5389 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5392 wxPoint
pos( event
.GetPosition() );
5393 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5395 wxGridCellCoords coords
;
5396 XYToCell( x
, y
, coords
);
5398 int cell_rows
, cell_cols
;
5399 bool isFirstDrag
= !m_isDragging
;
5400 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5401 if ((cell_rows
< 0) || (cell_cols
< 0))
5403 coords
.SetRow(coords
.GetRow() + cell_rows
);
5404 coords
.SetCol(coords
.GetCol() + cell_cols
);
5407 if ( event
.Dragging() )
5409 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5411 // Don't start doing anything until the mouse has been drug at
5412 // least 3 pixels in any direction...
5415 if (m_startDragPos
== wxDefaultPosition
)
5417 m_startDragPos
= pos
;
5420 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5424 m_isDragging
= true;
5425 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5427 // Hide the edit control, so it
5428 // won't interfer with drag-shrinking.
5429 if ( IsCellEditControlShown() )
5431 HideCellEditControl();
5432 SaveEditControlValue();
5435 // Have we captured the mouse yet?
5438 m_winCapture
= m_gridWin
;
5439 m_winCapture
->CaptureMouse();
5442 if ( coords
!= wxGridNoCellCoords
)
5444 if ( event
.ControlDown() )
5446 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5447 m_selectingKeyboard
= coords
;
5448 HighlightBlock ( m_selectingKeyboard
, coords
);
5450 else if ( CanDragCell() )
5454 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5455 m_selectingKeyboard
= coords
;
5457 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5465 if ( !IsSelection() )
5467 HighlightBlock( coords
, coords
);
5471 HighlightBlock( m_currentCellCoords
, coords
);
5475 if (! IsVisible(coords
))
5477 MakeCellVisible(coords
);
5478 // TODO: need to introduce a delay or something here. The
5479 // scrolling is way to fast, at least on MSW - also on GTK.
5483 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5485 int cw
, ch
, left
, dummy
;
5486 m_gridWin
->GetClientSize( &cw
, &ch
);
5487 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5489 wxClientDC
dc( m_gridWin
);
5491 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5492 GetRowMinimalHeight(m_dragRowOrCol
) );
5493 dc
.SetLogicalFunction(wxINVERT
);
5494 if ( m_dragLastPos
>= 0 )
5496 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5498 dc
.DrawLine( left
, y
, left
+cw
, y
);
5501 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5503 int cw
, ch
, dummy
, top
;
5504 m_gridWin
->GetClientSize( &cw
, &ch
);
5505 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5507 wxClientDC
dc( m_gridWin
);
5509 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5510 GetColMinimalWidth(m_dragRowOrCol
) );
5511 dc
.SetLogicalFunction(wxINVERT
);
5512 if ( m_dragLastPos
>= 0 )
5514 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5516 dc
.DrawLine( x
, top
, x
, top
+ch
);
5523 m_isDragging
= false;
5524 m_startDragPos
= wxDefaultPosition
;
5526 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5527 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5530 if ( event
.Entering() || event
.Leaving() )
5532 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5533 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5538 // ------------ Left button pressed
5540 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5542 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5547 if ( !event
.ControlDown() )
5549 if ( event
.ShiftDown() )
5553 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5554 m_currentCellCoords
.GetCol(),
5557 event
.ControlDown(),
5563 else if ( XToEdgeOfCol(x
) < 0 &&
5564 YToEdgeOfRow(y
) < 0 )
5566 DisableCellEditControl();
5567 MakeCellVisible( coords
);
5569 if ( event
.ControlDown() )
5573 m_selection
->ToggleCellSelection( coords
.GetRow(),
5575 event
.ControlDown(),
5580 m_selectingTopLeft
= wxGridNoCellCoords
;
5581 m_selectingBottomRight
= wxGridNoCellCoords
;
5582 m_selectingKeyboard
= coords
;
5586 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5587 SetCurrentCell( coords
);
5590 if ( m_selection
->GetSelectionMode() !=
5591 wxGrid::wxGridSelectCells
)
5593 HighlightBlock( coords
, coords
);
5602 // ------------ Left double click
5604 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5606 DisableCellEditControl();
5608 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5610 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5615 // we want double click to select a cell and start editing
5616 // (i.e. to behave in same way as sequence of two slow clicks):
5617 m_waitForSlowClick
= true;
5624 // ------------ Left button released
5626 else if ( event
.LeftUp() )
5628 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5632 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5633 m_winCapture
= NULL
;
5636 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5639 EnableCellEditControl();
5641 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5642 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5643 editor
->StartingClick();
5647 m_waitForSlowClick
= false;
5649 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5650 m_selectingBottomRight
!= wxGridNoCellCoords
)
5654 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5655 m_selectingTopLeft
.GetCol(),
5656 m_selectingBottomRight
.GetRow(),
5657 m_selectingBottomRight
.GetCol(),
5658 event
.ControlDown(),
5664 m_selectingTopLeft
= wxGridNoCellCoords
;
5665 m_selectingBottomRight
= wxGridNoCellCoords
;
5667 // Show the edit control, if it has been hidden for
5669 ShowCellEditControl();
5672 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5674 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5675 DoEndDragResizeRow();
5677 // Note: we are ending the event *after* doing
5678 // default processing in this case
5680 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5682 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5684 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5685 DoEndDragResizeCol();
5687 // Note: we are ending the event *after* doing
5688 // default processing in this case
5690 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5697 // ------------ Right button down
5699 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5701 DisableCellEditControl();
5702 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5707 // no default action at the moment
5712 // ------------ Right double click
5714 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5716 DisableCellEditControl();
5717 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5722 // no default action at the moment
5726 // ------------ Moving and no button action
5728 else if ( event
.Moving() && !event
.IsButton() )
5730 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5732 // out of grid cell area
5733 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5737 int dragRow
= YToEdgeOfRow( y
);
5738 int dragCol
= XToEdgeOfCol( x
);
5740 // Dragging on the corner of a cell to resize in both
5741 // directions is not implemented yet...
5743 if ( dragRow
>= 0 && dragCol
>= 0 )
5745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5751 m_dragRowOrCol
= dragRow
;
5753 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5755 if ( CanDragRowSize() && CanDragGridSize() )
5756 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5761 m_dragRowOrCol
= dragCol
;
5769 m_dragRowOrCol
= dragCol
;
5771 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5773 if ( CanDragColSize() && CanDragGridSize() )
5774 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5780 // Neither on a row or col edge
5782 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5784 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5790 void wxGrid::DoEndDragResizeRow()
5792 if ( m_dragLastPos
>= 0 )
5794 // erase the last line and resize the row
5796 int cw
, ch
, left
, dummy
;
5797 m_gridWin
->GetClientSize( &cw
, &ch
);
5798 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5800 wxClientDC
dc( m_gridWin
);
5802 dc
.SetLogicalFunction( wxINVERT
);
5803 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5804 HideCellEditControl();
5805 SaveEditControlValue();
5807 int rowTop
= GetRowTop(m_dragRowOrCol
);
5808 SetRowSize( m_dragRowOrCol
,
5809 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5811 if ( !GetBatchCount() )
5813 // Only needed to get the correct rect.y:
5814 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5816 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5817 rect
.width
= m_rowLabelWidth
;
5818 rect
.height
= ch
- rect
.y
;
5819 m_rowLabelWin
->Refresh( true, &rect
);
5821 // if there is a multicell block, paint all of it
5824 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5825 int leftCol
= XToCol(left
);
5826 int rightCol
= internalXToCol(left
+cw
);
5829 for (i
=leftCol
; i
<rightCol
; i
++)
5831 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5832 if (cell_rows
< subtract_rows
)
5833 subtract_rows
= cell_rows
;
5835 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5836 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5837 rect
.height
= ch
- rect
.y
;
5840 m_gridWin
->Refresh( false, &rect
);
5843 ShowCellEditControl();
5848 void wxGrid::DoEndDragResizeCol()
5850 if ( m_dragLastPos
>= 0 )
5852 // erase the last line and resize the col
5854 int cw
, ch
, dummy
, top
;
5855 m_gridWin
->GetClientSize( &cw
, &ch
);
5856 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5858 wxClientDC
dc( m_gridWin
);
5860 dc
.SetLogicalFunction( wxINVERT
);
5861 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5862 HideCellEditControl();
5863 SaveEditControlValue();
5865 int colLeft
= GetColLeft(m_dragRowOrCol
);
5866 SetColSize( m_dragRowOrCol
,
5867 wxMax( m_dragLastPos
- colLeft
,
5868 GetColMinimalWidth(m_dragRowOrCol
) ) );
5870 if ( !GetBatchCount() )
5872 // Only needed to get the correct rect.x:
5873 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5875 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5876 rect
.width
= cw
- rect
.x
;
5877 rect
.height
= m_colLabelHeight
;
5878 m_colLabelWin
->Refresh( true, &rect
);
5880 // if there is a multicell block, paint all of it
5883 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5884 int topRow
= YToRow(top
);
5885 int bottomRow
= internalYToRow(top
+cw
);
5888 for (i
=topRow
; i
<bottomRow
; i
++)
5890 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5891 if (cell_cols
< subtract_cols
)
5892 subtract_cols
= cell_cols
;
5894 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5895 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5896 rect
.width
= cw
- rect
.x
;
5899 m_gridWin
->Refresh( false, &rect
);
5902 ShowCellEditControl();
5909 // ------ interaction with data model
5911 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5913 switch ( msg
.GetId() )
5915 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5916 return GetModelValues();
5918 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5919 return SetModelValues();
5921 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5922 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5923 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5924 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5925 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5926 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5927 return Redimension( msg
);
5936 // The behaviour of this function depends on the grid table class
5937 // Clear() function. For the default wxGridStringTable class the
5938 // behavious is to replace all cell contents with wxEmptyString but
5939 // not to change the number of rows or cols.
5941 void wxGrid::ClearGrid()
5945 if (IsCellEditControlEnabled())
5946 DisableCellEditControl();
5949 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5954 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5956 // TODO: something with updateLabels flag
5960 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5966 if (IsCellEditControlEnabled())
5967 DisableCellEditControl();
5969 bool done
= m_table
->InsertRows( pos
, numRows
);
5972 // the table will have sent the results of the insert row
5973 // operation to this view object as a grid table message
5979 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5981 // TODO: something with updateLabels flag
5985 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5991 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5993 // the table will have sent the results of the append row
5994 // operation to this view object as a grid table message
6000 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6002 // TODO: something with updateLabels flag
6006 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6012 if (IsCellEditControlEnabled())
6013 DisableCellEditControl();
6015 bool done
= m_table
->DeleteRows( pos
, numRows
);
6017 // the table will have sent the results of the delete row
6018 // operation to this view object as a grid table message
6024 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6026 // TODO: something with updateLabels flag
6030 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6036 if (IsCellEditControlEnabled())
6037 DisableCellEditControl();
6039 bool done
= m_table
->InsertCols( pos
, numCols
);
6041 // the table will have sent the results of the insert col
6042 // operation to this view object as a grid table message
6048 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6050 // TODO: something with updateLabels flag
6054 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6060 bool done
= m_table
->AppendCols( numCols
);
6062 // the table will have sent the results of the append col
6063 // operation to this view object as a grid table message
6069 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6071 // TODO: something with updateLabels flag
6075 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6081 if (IsCellEditControlEnabled())
6082 DisableCellEditControl();
6084 bool done
= m_table
->DeleteCols( pos
, numCols
);
6086 // the table will have sent the results of the delete col
6087 // operation to this view object as a grid table message
6095 // ----- event handlers
6098 // Generate a grid event based on a mouse event and
6099 // return the result of ProcessEvent()
6101 int wxGrid::SendEvent( const wxEventType type
,
6103 wxMouseEvent
& mouseEv
)
6108 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6110 int rowOrCol
= (row
== -1 ? col
: row
);
6112 wxGridSizeEvent
gridEvt( GetId(),
6116 mouseEv
.GetX() + GetRowLabelSize(),
6117 mouseEv
.GetY() + GetColLabelSize(),
6118 mouseEv
.ControlDown(),
6119 mouseEv
.ShiftDown(),
6121 mouseEv
.MetaDown() );
6123 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6124 vetoed
= !gridEvt
.IsAllowed();
6126 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6128 // Right now, it should _never_ end up here!
6129 wxGridRangeSelectEvent
gridEvt( GetId(),
6133 m_selectingBottomRight
,
6135 mouseEv
.ControlDown(),
6136 mouseEv
.ShiftDown(),
6138 mouseEv
.MetaDown() );
6140 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6141 vetoed
= !gridEvt
.IsAllowed();
6145 wxGridEvent
gridEvt( GetId(),
6149 mouseEv
.GetX() + GetRowLabelSize(),
6150 mouseEv
.GetY() + GetColLabelSize(),
6152 mouseEv
.ControlDown(),
6153 mouseEv
.ShiftDown(),
6155 mouseEv
.MetaDown() );
6156 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6157 vetoed
= !gridEvt
.IsAllowed();
6160 // A Veto'd event may not be `claimed' so test this first
6161 if (vetoed
) return -1;
6162 return claimed
? 1 : 0;
6166 // Generate a grid event of specified type and return the result
6167 // of ProcessEvent().
6169 int wxGrid::SendEvent( const wxEventType type
,
6175 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6177 int rowOrCol
= (row
== -1 ? col
: row
);
6179 wxGridSizeEvent
gridEvt( GetId(),
6184 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6185 vetoed
= !gridEvt
.IsAllowed();
6189 wxGridEvent
gridEvt( GetId(),
6194 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6195 vetoed
= !gridEvt
.IsAllowed();
6198 // A Veto'd event may not be `claimed' so test this first
6199 if (vetoed
) return -1;
6200 return claimed
? 1 : 0;
6204 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6206 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6209 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6211 // Don't do anything if between Begin/EndBatch...
6212 // EndBatch() will do all this on the last nested one anyway.
6213 if (! GetBatchCount())
6215 // Refresh to get correct scrolled position:
6216 wxScrolledWindow::Refresh(eraseb
,rect
);
6220 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6221 int width_label
, width_cell
, height_label
, height_cell
;
6224 //Copy rectangle can get scroll offsets..
6225 rect_x
= rect
->GetX();
6226 rect_y
= rect
->GetY();
6227 rectWidth
= rect
->GetWidth();
6228 rectHeight
= rect
->GetHeight();
6230 width_label
= m_rowLabelWidth
- rect_x
;
6231 if (width_label
> rectWidth
) width_label
= rectWidth
;
6233 height_label
= m_colLabelHeight
- rect_y
;
6234 if (height_label
> rectHeight
) height_label
= rectHeight
;
6236 if (rect_x
> m_rowLabelWidth
)
6238 x
= rect_x
- m_rowLabelWidth
;
6239 width_cell
= rectWidth
;
6244 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6247 if (rect_y
> m_colLabelHeight
)
6249 y
= rect_y
- m_colLabelHeight
;
6250 height_cell
= rectHeight
;
6255 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6258 // Paint corner label part intersecting rect.
6259 if ( width_label
> 0 && height_label
> 0 )
6261 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6262 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6265 // Paint col labels part intersecting rect.
6266 if ( width_cell
> 0 && height_label
> 0 )
6268 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6269 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6272 // Paint row labels part intersecting rect.
6273 if ( width_label
> 0 && height_cell
> 0 )
6275 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6276 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6279 // Paint cell area part intersecting rect.
6280 if ( width_cell
> 0 && height_cell
> 0 )
6282 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6283 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6288 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6289 m_colLabelWin
->Refresh(eraseb
, NULL
);
6290 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6291 m_gridWin
->Refresh(eraseb
, NULL
);
6296 void wxGrid::OnSize( wxSizeEvent
& event
)
6298 // position the child windows
6301 // don't call CalcDimensions() from here, the base class handles the size
6307 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6309 if ( m_inOnKeyDown
)
6311 // shouldn't be here - we are going round in circles...
6313 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6316 m_inOnKeyDown
= true;
6318 // propagate the event up and see if it gets processed
6320 wxWindow
*parent
= GetParent();
6321 wxKeyEvent
keyEvt( event
);
6322 keyEvt
.SetEventObject( parent
);
6324 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6327 // try local handlers
6329 switch ( event
.GetKeyCode() )
6332 if ( event
.ControlDown() )
6334 MoveCursorUpBlock( event
.ShiftDown() );
6338 MoveCursorUp( event
.ShiftDown() );
6343 if ( event
.ControlDown() )
6345 MoveCursorDownBlock( event
.ShiftDown() );
6349 MoveCursorDown( event
.ShiftDown() );
6354 if ( event
.ControlDown() )
6356 MoveCursorLeftBlock( event
.ShiftDown() );
6360 MoveCursorLeft( event
.ShiftDown() );
6365 if ( event
.ControlDown() )
6367 MoveCursorRightBlock( event
.ShiftDown() );
6371 MoveCursorRight( event
.ShiftDown() );
6376 case WXK_NUMPAD_ENTER
:
6377 if ( event
.ControlDown() )
6379 event
.Skip(); // to let the edit control have the return
6383 if ( GetGridCursorRow() < GetNumberRows()-1 )
6385 MoveCursorDown( event
.ShiftDown() );
6389 // at the bottom of a column
6390 DisableCellEditControl();
6400 if (event
.ShiftDown())
6402 if ( GetGridCursorCol() > 0 )
6404 MoveCursorLeft( false );
6409 DisableCellEditControl();
6414 if ( GetGridCursorCol() < GetNumberCols()-1 )
6416 MoveCursorRight( false );
6421 DisableCellEditControl();
6427 if ( event
.ControlDown() )
6429 MakeCellVisible( 0, 0 );
6430 SetCurrentCell( 0, 0 );
6439 if ( event
.ControlDown() )
6441 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6442 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6459 if ( event
.ControlDown() )
6463 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6464 m_currentCellCoords
.GetCol(),
6465 event
.ControlDown(),
6472 if ( !IsEditable() )
6474 MoveCursorRight( false );
6477 // Otherwise fall through to default
6485 m_inOnKeyDown
= false;
6488 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6490 // try local handlers
6492 if ( event
.GetKeyCode() == WXK_SHIFT
)
6494 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6495 m_selectingBottomRight
!= wxGridNoCellCoords
)
6499 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6500 m_selectingTopLeft
.GetCol(),
6501 m_selectingBottomRight
.GetRow(),
6502 m_selectingBottomRight
.GetCol(),
6503 event
.ControlDown(),
6510 m_selectingTopLeft
= wxGridNoCellCoords
;
6511 m_selectingBottomRight
= wxGridNoCellCoords
;
6512 m_selectingKeyboard
= wxGridNoCellCoords
;
6516 void wxGrid::OnChar( wxKeyEvent
& event
)
6518 // is it possible to edit the current cell at all?
6519 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6521 // yes, now check whether the cells editor accepts the key
6522 int row
= m_currentCellCoords
.GetRow();
6523 int col
= m_currentCellCoords
.GetCol();
6524 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6525 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6527 // <F2> is special and will always start editing, for
6528 // other keys - ask the editor itself
6529 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6530 || editor
->IsAcceptedKey(event
) )
6532 // ensure cell is visble
6533 MakeCellVisible(row
, col
);
6534 EnableCellEditControl();
6536 // a problem can arise if the cell is not completely
6537 // visible (even after calling MakeCellVisible the
6538 // control is not created and calling StartingKey will
6540 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6541 editor
->StartingKey(event
);
6558 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6562 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6564 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6566 // the event has been intercepted - do nothing
6570 wxClientDC
dc(m_gridWin
);
6573 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6575 DisableCellEditControl();
6577 if ( IsVisible( m_currentCellCoords
, false ) )
6580 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6581 if ( !m_gridLinesEnabled
)
6589 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6591 // Otherwise refresh redraws the highlight!
6592 m_currentCellCoords
= coords
;
6594 DrawGridCellArea(dc
,cells
);
6595 DrawAllGridLines( dc
, r
);
6599 m_currentCellCoords
= coords
;
6601 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6602 DrawCellHighlight(dc
, attr
);
6607 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6610 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6614 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6617 rightCol
= GetNumberCols() - 1;
6619 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6622 bottomRow
= GetNumberRows() - 1;
6626 if ( topRow
> bottomRow
)
6633 if ( leftCol
> rightCol
)
6640 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6641 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6643 // First the case that we selected a completely new area
6644 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6645 m_selectingBottomRight
== wxGridNoCellCoords
)
6648 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6649 wxGridCellCoords ( bottomRow
, rightCol
) );
6650 m_gridWin
->Refresh( false, &rect
);
6652 // Now handle changing an existing selection area.
6653 else if ( m_selectingTopLeft
!= updateTopLeft
||
6654 m_selectingBottomRight
!= updateBottomRight
)
6656 // Compute two optimal update rectangles:
6657 // Either one rectangle is a real subset of the
6658 // other, or they are (almost) disjoint!
6660 bool need_refresh
[4];
6664 need_refresh
[3] = false;
6667 // Store intermediate values
6668 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6669 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6670 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6671 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6673 // Determine the outer/inner coordinates.
6674 if (oldLeft
> leftCol
)
6680 if (oldTop
> topRow
)
6686 if (oldRight
< rightCol
)
6689 oldRight
= rightCol
;
6692 if (oldBottom
< bottomRow
)
6695 oldBottom
= bottomRow
;
6699 // Now, either the stuff marked old is the outer
6700 // rectangle or we don't have a situation where one
6701 // is contained in the other.
6703 if ( oldLeft
< leftCol
)
6705 // Refresh the newly selected or deselected
6706 // area to the left of the old or new selection.
6707 need_refresh
[0] = true;
6708 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6710 wxGridCellCoords ( oldBottom
,
6714 if ( oldTop
< topRow
)
6716 // Refresh the newly selected or deselected
6717 // area above the old or new selection.
6718 need_refresh
[1] = true;
6719 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6721 wxGridCellCoords ( topRow
- 1,
6725 if ( oldRight
> rightCol
)
6727 // Refresh the newly selected or deselected
6728 // area to the right of the old or new selection.
6729 need_refresh
[2] = true;
6730 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6732 wxGridCellCoords ( oldBottom
,
6736 if ( oldBottom
> bottomRow
)
6738 // Refresh the newly selected or deselected
6739 // area below the old or new selection.
6740 need_refresh
[3] = true;
6741 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6743 wxGridCellCoords ( oldBottom
,
6747 // various Refresh() calls
6748 for (i
= 0; i
< 4; i
++ )
6749 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6750 m_gridWin
->Refresh( false, &(rect
[i
]) );
6753 m_selectingTopLeft
= updateTopLeft
;
6754 m_selectingBottomRight
= updateBottomRight
;
6758 // ------ functions to get/send data (see also public functions)
6761 bool wxGrid::GetModelValues()
6763 // Hide the editor, so it won't hide a changed value.
6764 HideCellEditControl();
6768 // all we need to do is repaint the grid
6770 m_gridWin
->Refresh();
6778 bool wxGrid::SetModelValues()
6782 // Disable the editor, so it won't hide a changed value.
6783 // Do we also want to save the current value of the editor first?
6785 DisableCellEditControl();
6789 for ( row
= 0; row
< m_numRows
; row
++ )
6791 for ( col
= 0; col
< m_numCols
; col
++ )
6793 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6805 // Note - this function only draws cells that are in the list of
6806 // exposed cells (usually set from the update region by
6807 // CalcExposedCells)
6809 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6811 if ( !m_numRows
|| !m_numCols
) return;
6813 int i
, numCells
= cells
.GetCount();
6814 int row
, col
, cell_rows
, cell_cols
;
6815 wxGridCellCoordsArray redrawCells
;
6817 for ( i
= numCells
-1; i
>= 0; i
-- )
6819 row
= cells
[i
].GetRow();
6820 col
= cells
[i
].GetCol();
6821 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6823 // If this cell is part of a multicell block, find owner for repaint
6824 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6826 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6827 bool marked
= false;
6828 for ( int j
= 0; j
< numCells
; j
++ )
6830 if ( cell
== cells
[j
] )
6838 int count
= redrawCells
.GetCount();
6839 for (int j
= 0; j
< count
; j
++)
6841 if ( cell
== redrawCells
[j
] )
6847 if (!marked
) redrawCells
.Add( cell
);
6849 continue; // don't bother drawing this cell
6852 // If this cell is empty, find cell to left that might want to overflow
6853 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6855 for ( int l
= 0; l
< cell_rows
; l
++ )
6857 // find a cell in this row to left alreay marked for repaint
6859 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6860 if ((redrawCells
[k
].GetCol() < left
) &&
6861 (redrawCells
[k
].GetRow() == row
))
6862 left
=redrawCells
[k
].GetCol();
6864 if (left
== col
) left
= 0; // oh well
6866 for (int j
= col
-1; j
>= left
; j
--)
6868 if (!m_table
->IsEmptyCell(row
+l
, j
))
6870 if (GetCellOverflow(row
+l
, j
))
6872 wxGridCellCoords
cell(row
+l
, j
);
6873 bool marked
= false;
6875 for (int k
= 0; k
< numCells
; k
++)
6877 if ( cell
== cells
[k
] )
6885 int count
= redrawCells
.GetCount();
6886 for (int k
= 0; k
< count
; k
++)
6888 if ( cell
== redrawCells
[k
] )
6894 if (!marked
) redrawCells
.Add( cell
);
6902 DrawCell( dc
, cells
[i
] );
6905 numCells
= redrawCells
.GetCount();
6907 for ( i
= numCells
- 1; i
>= 0; i
-- )
6909 DrawCell( dc
, redrawCells
[i
] );
6914 void wxGrid::DrawGridSpace( wxDC
& dc
)
6917 m_gridWin
->GetClientSize( &cw
, &ch
);
6920 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6922 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6923 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6925 if ( right
> rightCol
|| bottom
> bottomRow
)
6928 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6930 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6931 dc
.SetPen( *wxTRANSPARENT_PEN
);
6933 if ( right
> rightCol
)
6935 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6938 if ( bottom
> bottomRow
)
6940 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6946 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6948 int row
= coords
.GetRow();
6949 int col
= coords
.GetCol();
6951 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6954 // we draw the cell border ourselves
6955 #if !WXGRID_DRAW_LINES
6956 if ( m_gridLinesEnabled
)
6957 DrawCellBorder( dc
, coords
);
6960 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6962 bool isCurrent
= coords
== m_currentCellCoords
;
6964 wxRect rect
= CellToRect( row
, col
);
6966 // if the editor is shown, we should use it and not the renderer
6967 // Note: However, only if it is really _shown_, i.e. not hidden!
6968 if ( isCurrent
&& IsCellEditControlShown() )
6970 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6971 editor
->PaintBackground(rect
, attr
);
6976 // but all the rest is drawn by the cell renderer and hence may be
6978 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6979 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6986 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6988 int row
= m_currentCellCoords
.GetRow();
6989 int col
= m_currentCellCoords
.GetCol();
6991 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6994 wxRect rect
= CellToRect(row
, col
);
6996 // hmmm... what could we do here to show that the cell is disabled?
6997 // for now, I just draw a thinner border than for the other ones, but
6998 // it doesn't look really good
7000 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7004 // The center of th drawn line is where the position/width/height of
7005 // the rectangle is actually at, (on wxMSW atr least,) so we will
7006 // reduce the size of the rectangle to compensate for the thickness of
7007 // the line. If this is too strange on non wxMSW platforms then
7008 // please #ifdef this appropriately.
7009 rect
.x
+= penWidth
/2;
7010 rect
.y
+= penWidth
/2;
7011 rect
.width
-= penWidth
-1;
7012 rect
.height
-= penWidth
-1;
7015 // Now draw the rectangle
7016 // use the cellHighlightColour if the cell is inside a selection, this
7017 // will ensure the cell is always visible.
7018 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7019 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7020 dc
.DrawRectangle(rect
);
7024 // VZ: my experiments with 3d borders...
7026 // how to properly set colours for arbitrary bg?
7027 wxCoord x1
= rect
.x
,
7029 x2
= rect
.x
+ rect
.width
-1,
7030 y2
= rect
.y
+ rect
.height
-1;
7032 dc
.SetPen(*wxWHITE_PEN
);
7033 dc
.DrawLine(x1
, y1
, x2
, y1
);
7034 dc
.DrawLine(x1
, y1
, x1
, y2
);
7036 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7037 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7039 dc
.SetPen(*wxBLACK_PEN
);
7040 dc
.DrawLine(x1
, y2
, x2
, y2
);
7041 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7046 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7048 int row
= coords
.GetRow();
7049 int col
= coords
.GetCol();
7050 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7053 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7055 wxRect rect
= CellToRect( row
, col
);
7057 // right hand border
7059 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7060 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7064 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7065 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7068 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7070 // This if block was previously in wxGrid::OnPaint but that doesn't
7071 // seem to get called under wxGTK - MB
7073 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7074 m_numRows
&& m_numCols
)
7076 m_currentCellCoords
.Set(0, 0);
7079 if ( IsCellEditControlShown() )
7081 // don't show highlight when the edit control is shown
7085 // if the active cell was repainted, repaint its highlight too because it
7086 // might have been damaged by the grid lines
7087 size_t count
= cells
.GetCount();
7088 for ( size_t n
= 0; n
< count
; n
++ )
7090 if ( cells
[n
] == m_currentCellCoords
)
7092 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7093 DrawCellHighlight(dc
, attr
);
7101 // TODO: remove this ???
7102 // This is used to redraw all grid lines e.g. when the grid line colour
7105 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7107 #if !WXGRID_DRAW_LINES
7111 if ( !m_gridLinesEnabled
||
7113 !m_numCols
) return;
7115 int top
, bottom
, left
, right
;
7117 #if 0 //#ifndef __WXGTK__
7121 m_gridWin
->GetClientSize(&cw
, &ch
);
7123 // virtual coords of visible area
7125 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7126 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7131 reg
.GetBox(x
, y
, w
, h
);
7132 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7133 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7137 m_gridWin
->GetClientSize(&cw
, &ch
);
7138 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7139 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7142 // avoid drawing grid lines past the last row and col
7144 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7145 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7147 // no gridlines inside multicells, clip them out
7148 int leftCol
= internalXToCol(left
);
7149 int topRow
= internalYToRow(top
);
7150 int rightCol
= internalXToCol(right
);
7151 int bottomRow
= internalYToRow(bottom
);
7154 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7155 wxRegion
clippedcells(0, 0, cw
, ch
);
7157 int i
, j
, cell_rows
, cell_cols
;
7160 for (j
=topRow
; j
<bottomRow
; j
++)
7162 for (i
=leftCol
; i
<rightCol
; i
++)
7164 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7165 if ((cell_rows
> 1) || (cell_cols
> 1))
7167 rect
= CellToRect(j
,i
);
7168 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7169 clippedcells
.Subtract(rect
);
7171 else if ((cell_rows
< 0) || (cell_cols
< 0))
7173 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7174 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7175 clippedcells
.Subtract(rect
);
7180 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7182 int i
, j
, cell_rows
, cell_cols
;
7185 for (j
=topRow
; j
<bottomRow
; j
++)
7187 for (i
=leftCol
; i
<rightCol
; i
++)
7189 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7190 if ((cell_rows
> 1) || (cell_cols
> 1))
7192 rect
= CellToRect(j
,i
);
7193 clippedcells
.Subtract(rect
);
7195 else if ((cell_rows
< 0) || (cell_cols
< 0))
7197 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7198 clippedcells
.Subtract(rect
);
7203 dc
.SetClippingRegion( clippedcells
);
7205 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7207 // horizontal grid lines
7209 // already declared above - int i;
7210 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7212 int bot
= GetRowBottom(i
) - 1;
7221 dc
.DrawLine( left
, bot
, right
, bot
);
7226 // vertical grid lines
7228 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7230 int colRight
= GetColRight(i
) - 1;
7231 if ( colRight
> right
)
7236 if ( colRight
>= left
)
7238 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7241 dc
.DestroyClippingRegion();
7245 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7247 if ( !m_numRows
) return;
7250 size_t numLabels
= rows
.GetCount();
7252 for ( i
= 0; i
< numLabels
; i
++ )
7254 DrawRowLabel( dc
, rows
[i
] );
7259 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7261 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7267 rect
.SetY( GetRowTop(row
) + 1 );
7268 rect
.SetWidth( m_rowLabelWidth
- 2 );
7269 rect
.SetHeight( GetRowHeight(row
) - 2 );
7271 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7273 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7275 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7277 int rowTop
= GetRowTop(row
),
7278 rowBottom
= GetRowBottom(row
) - 1;
7280 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7281 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7282 m_rowLabelWidth
-1, rowBottom
);
7284 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7286 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7288 dc
.SetPen( *wxWHITE_PEN
);
7289 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7290 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7292 dc
.SetBackgroundMode( wxTRANSPARENT
);
7293 dc
.SetTextForeground( GetLabelTextColour() );
7294 dc
.SetFont( GetLabelFont() );
7297 GetRowLabelAlignment( &hAlign
, &vAlign
);
7300 rect
.SetY( GetRowTop(row
) + 2 );
7301 rect
.SetWidth( m_rowLabelWidth
- 4 );
7302 rect
.SetHeight( GetRowHeight(row
) - 4 );
7303 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7307 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7309 if ( !m_numCols
) return;
7312 size_t numLabels
= cols
.GetCount();
7314 for ( i
= 0; i
< numLabels
; i
++ )
7316 DrawColLabel( dc
, cols
[i
] );
7321 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7323 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7326 int colLeft
= GetColLeft(col
);
7330 rect
.SetX( colLeft
+ 1 );
7332 rect
.SetWidth( GetColWidth(col
) - 2 );
7333 rect
.SetHeight( m_colLabelHeight
- 2 );
7335 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7337 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7339 int colRight
= GetColRight(col
) - 1;
7341 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7342 dc
.DrawLine( colRight
, 0,
7343 colRight
, m_colLabelHeight
-1 );
7345 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7347 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7348 colRight
+1, m_colLabelHeight
-1 );
7350 dc
.SetPen( *wxWHITE_PEN
);
7351 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7352 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7354 dc
.SetBackgroundMode( wxTRANSPARENT
);
7355 dc
.SetTextForeground( GetLabelTextColour() );
7356 dc
.SetFont( GetLabelFont() );
7358 int hAlign
, vAlign
, orient
;
7359 GetColLabelAlignment( &hAlign
, &vAlign
);
7360 orient
= GetColLabelTextOrientation();
7362 rect
.SetX( colLeft
+ 2 );
7364 rect
.SetWidth( GetColWidth(col
) - 4 );
7365 rect
.SetHeight( m_colLabelHeight
- 4 );
7366 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7369 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7370 const wxString
& value
,
7374 int textOrientation
)
7376 wxArrayString lines
;
7378 StringToLines( value
, lines
);
7381 //Forward to new API.
7382 DrawTextRectangle( dc
,
7391 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7392 const wxArrayString
& lines
,
7396 int textOrientation
)
7398 long textWidth
, textHeight
;
7399 long lineWidth
, lineHeight
;
7402 dc
.SetClippingRegion( rect
);
7404 nLines
= lines
.GetCount();
7408 float x
= 0.0, y
= 0.0;
7410 if( textOrientation
== wxHORIZONTAL
)
7411 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7413 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7417 case wxALIGN_BOTTOM
:
7418 if( textOrientation
== wxHORIZONTAL
)
7419 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7421 x
= rect
.x
+ rect
.width
- textWidth
;
7424 case wxALIGN_CENTRE
:
7425 if( textOrientation
== wxHORIZONTAL
)
7426 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7428 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7433 if( textOrientation
== wxHORIZONTAL
)
7440 // Align each line of a multi-line label
7441 for( l
= 0; l
< nLines
; l
++ )
7443 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7445 switch( horizAlign
)
7448 if( textOrientation
== wxHORIZONTAL
)
7449 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7451 y
= rect
.y
+ lineWidth
+ 1;
7454 case wxALIGN_CENTRE
:
7455 if( textOrientation
== wxHORIZONTAL
)
7456 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7458 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7463 if( textOrientation
== wxHORIZONTAL
)
7466 y
= rect
.y
+ rect
.height
- 1;
7470 if( textOrientation
== wxHORIZONTAL
)
7472 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7477 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7482 dc
.DestroyClippingRegion();
7486 // Split multi line text up into an array of strings. Any existing
7487 // contents of the string array are preserved.
7489 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7493 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7494 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7496 while ( startPos
< (int)tVal
.Length() )
7498 pos
= tVal
.Mid(startPos
).Find( eol
);
7503 else if ( pos
== 0 )
7505 lines
.Add( wxEmptyString
);
7509 lines
.Add( value
.Mid(startPos
, pos
) );
7513 if ( startPos
< (int)value
.Length() )
7515 lines
.Add( value
.Mid( startPos
) );
7520 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7521 const wxArrayString
& lines
,
7522 long *width
, long *height
)
7529 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7531 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7532 w
= wxMax( w
, lineW
);
7541 // ------ Batch processing.
7543 void wxGrid::EndBatch()
7545 if ( m_batchCount
> 0 )
7548 if ( !m_batchCount
)
7551 m_rowLabelWin
->Refresh();
7552 m_colLabelWin
->Refresh();
7553 m_cornerLabelWin
->Refresh();
7554 m_gridWin
->Refresh();
7559 // Use this, rather than wxWindow::Refresh(), to force an immediate
7560 // repainting of the grid. Has no effect if you are already inside a
7561 // BeginBatch / EndBatch block.
7563 void wxGrid::ForceRefresh()
7569 bool wxGrid::Enable(bool enable
)
7571 if ( !wxScrolledWindow::Enable(enable
) )
7574 // redraw in the new state
7575 m_gridWin
->Refresh();
7581 // ------ Edit control functions
7585 void wxGrid::EnableEditing( bool edit
)
7587 // TODO: improve this ?
7589 if ( edit
!= m_editable
)
7591 if(!edit
) EnableCellEditControl(edit
);
7597 void wxGrid::EnableCellEditControl( bool enable
)
7602 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7603 SetCurrentCell( 0, 0 );
7605 if ( enable
!= m_cellEditCtrlEnabled
)
7609 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7612 // this should be checked by the caller!
7613 wxASSERT_MSG( CanEnableCellControl(),
7614 _T("can't enable editing for this cell!") );
7616 // do it before ShowCellEditControl()
7617 m_cellEditCtrlEnabled
= enable
;
7619 ShowCellEditControl();
7623 //FIXME:add veto support
7624 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7626 HideCellEditControl();
7627 SaveEditControlValue();
7629 // do it after HideCellEditControl()
7630 m_cellEditCtrlEnabled
= enable
;
7635 bool wxGrid::IsCurrentCellReadOnly() const
7638 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7639 bool readonly
= attr
->IsReadOnly();
7645 bool wxGrid::CanEnableCellControl() const
7647 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7648 !IsCurrentCellReadOnly();
7652 bool wxGrid::IsCellEditControlEnabled() const
7654 // the cell edit control might be disable for all cells or just for the
7655 // current one if it's read only
7656 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7659 bool wxGrid::IsCellEditControlShown() const
7661 bool isShown
= false;
7663 if ( m_cellEditCtrlEnabled
)
7665 int row
= m_currentCellCoords
.GetRow();
7666 int col
= m_currentCellCoords
.GetCol();
7667 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7668 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7673 if ( editor
->IsCreated() )
7675 isShown
= editor
->GetControl()->IsShown();
7685 void wxGrid::ShowCellEditControl()
7687 if ( IsCellEditControlEnabled() )
7689 if ( !IsVisible( m_currentCellCoords
) )
7691 m_cellEditCtrlEnabled
= false;
7696 wxRect rect
= CellToRect( m_currentCellCoords
);
7697 int row
= m_currentCellCoords
.GetRow();
7698 int col
= m_currentCellCoords
.GetCol();
7700 // if this is part of a multicell, find owner (topleft)
7701 int cell_rows
, cell_cols
;
7702 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7703 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7707 m_currentCellCoords
.SetRow( row
);
7708 m_currentCellCoords
.SetCol( col
);
7711 // convert to scrolled coords
7713 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7715 // done in PaintBackground()
7717 // erase the highlight and the cell contents because the editor
7718 // might not cover the entire cell
7719 wxClientDC
dc( m_gridWin
);
7721 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7722 dc
.SetPen(*wxTRANSPARENT_PEN
);
7723 dc
.DrawRectangle(rect
);
7726 // cell is shifted by one pixel
7727 // However, don't allow x or y to become negative
7728 // since the SetSize() method interprets that as
7735 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7736 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7737 if ( !editor
->IsCreated() )
7739 editor
->Create(m_gridWin
, wxID_ANY
,
7740 new wxGridCellEditorEvtHandler(this, editor
));
7742 wxGridEditorCreatedEvent
evt(GetId(),
7743 wxEVT_GRID_EDITOR_CREATED
,
7747 editor
->GetControl());
7748 GetEventHandler()->ProcessEvent(evt
);
7752 // resize editor to overflow into righthand cells if allowed
7753 int maxWidth
= rect
.width
;
7754 wxString value
= GetCellValue(row
, col
);
7755 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7758 GetTextExtent(value
, &maxWidth
, &y
,
7759 NULL
, NULL
, &attr
->GetFont());
7760 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7762 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7763 if (rect
.x
+maxWidth
> client_right
)
7764 maxWidth
= client_right
- rect
.x
;
7766 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7768 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7769 // may have changed earlier
7770 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7773 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7774 // looks weird going over a multicell
7775 if (m_table
->IsEmptyCell(row
,i
) &&
7776 (rect
.width
< maxWidth
) && (c_rows
== 1))
7777 rect
.width
+= GetColWidth(i
);
7781 if (rect
.GetRight() > client_right
)
7782 rect
.SetRight(client_right
-1);
7785 editor
->SetCellAttr(attr
);
7786 editor
->SetSize( rect
);
7787 editor
->Show( true, attr
);
7789 // recalc dimensions in case we need to
7790 // expand the scrolled window to account for editor
7793 editor
->BeginEdit(row
, col
, this);
7794 editor
->SetCellAttr(NULL
);
7803 void wxGrid::HideCellEditControl()
7805 if ( IsCellEditControlEnabled() )
7807 int row
= m_currentCellCoords
.GetRow();
7808 int col
= m_currentCellCoords
.GetCol();
7810 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7811 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7812 editor
->Show( false );
7815 m_gridWin
->SetFocus();
7816 // refresh whole row to the right
7817 wxRect
rect( CellToRect(row
, col
) );
7818 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7819 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7821 // ensure that the pixels under the focus ring get refreshed as well
7822 rect
.Inflate(10,10);
7824 m_gridWin
->Refresh( false, &rect
);
7829 void wxGrid::SaveEditControlValue()
7831 if ( IsCellEditControlEnabled() )
7833 int row
= m_currentCellCoords
.GetRow();
7834 int col
= m_currentCellCoords
.GetCol();
7836 wxString oldval
= GetCellValue(row
,col
);
7838 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7839 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7840 bool changed
= editor
->EndEdit(row
, col
, this);
7847 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7848 m_currentCellCoords
.GetRow(),
7849 m_currentCellCoords
.GetCol() ) < 0 ) {
7851 // Event has been vetoed, set the data back.
7852 SetCellValue(row
,col
,oldval
);
7860 // ------ Grid location functions
7861 // Note that all of these functions work with the logical coordinates of
7862 // grid cells and labels so you will need to convert from device
7863 // coordinates for mouse events etc.
7866 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7868 int row
= YToRow(y
);
7869 int col
= XToCol(x
);
7871 if ( row
== -1 || col
== -1 )
7873 coords
= wxGridNoCellCoords
;
7877 coords
.Set( row
, col
);
7882 // Internal Helper function for computing row or column from some
7883 // (unscrolled) coordinate value, using either
7884 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7885 // of m_rowBottoms/m_ColRights to speed up the search!
7887 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7888 const wxArrayInt
& BorderArray
, int nMax
,
7893 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7899 size_t i_max
= coord
/ defaultDist
,
7902 if (BorderArray
.IsEmpty())
7904 if((int) i_max
< nMax
)
7906 return clipToMinMax
? nMax
- 1 : -1;
7909 if ( i_max
>= BorderArray
.GetCount())
7910 i_max
= BorderArray
.GetCount() - 1;
7913 if ( coord
>= BorderArray
[i_max
])
7917 i_max
= coord
/ minDist
;
7919 i_max
= BorderArray
.GetCount() - 1;
7921 if ( i_max
>= BorderArray
.GetCount())
7922 i_max
= BorderArray
.GetCount() - 1;
7924 if ( coord
>= BorderArray
[i_max
])
7925 return clipToMinMax
? (int)i_max
: -1;
7926 if ( coord
< BorderArray
[0] )
7929 while ( i_max
- i_min
> 0 )
7931 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7932 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7933 if (coord
>= BorderArray
[ i_max
- 1])
7937 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7938 if (coord
< BorderArray
[median
])
7946 int wxGrid::YToRow( int y
)
7948 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7949 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7953 int wxGrid::XToCol( int x
)
7955 return CoordToRowOrCol(x
, m_defaultColWidth
,
7956 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7960 // return the row number that that the y coord is near the edge of, or
7961 // -1 if not near an edge
7963 int wxGrid::YToEdgeOfRow( int y
)
7966 i
= internalYToRow(y
);
7968 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7970 // We know that we are in row i, test whether we are
7971 // close enough to lower or upper border, respectively.
7972 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7974 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7982 // return the col number that that the x coord is near the edge of, or
7983 // -1 if not near an edge
7985 int wxGrid::XToEdgeOfCol( int x
)
7988 i
= internalXToCol(x
);
7990 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7992 // We know that we are in column i, test whether we are
7993 // close enough to right or left border, respectively.
7994 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7996 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8004 wxRect
wxGrid::CellToRect( int row
, int col
)
8006 wxRect
rect( -1, -1, -1, -1 );
8008 if ( row
>= 0 && row
< m_numRows
&&
8009 col
>= 0 && col
< m_numCols
)
8011 int i
, cell_rows
, cell_cols
;
8012 rect
.width
= rect
.height
= 0;
8013 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8014 // if negative then find multicell owner
8015 if (cell_rows
< 0) row
+= cell_rows
;
8016 if (cell_cols
< 0) col
+= cell_cols
;
8017 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8019 rect
.x
= GetColLeft(col
);
8020 rect
.y
= GetRowTop(row
);
8021 for (i
=col
; i
<col
+cell_cols
; i
++)
8022 rect
.width
+= GetColWidth(i
);
8023 for (i
=row
; i
<row
+cell_rows
; i
++)
8024 rect
.height
+= GetRowHeight(i
);
8027 // if grid lines are enabled, then the area of the cell is a bit smaller
8028 if (m_gridLinesEnabled
) {
8036 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8038 // get the cell rectangle in logical coords
8040 wxRect
r( CellToRect( row
, col
) );
8042 // convert to device coords
8044 int left
, top
, right
, bottom
;
8045 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8046 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8048 // check against the client area of the grid window
8051 m_gridWin
->GetClientSize( &cw
, &ch
);
8053 if ( wholeCellVisible
)
8055 // is the cell wholly visible ?
8057 return ( left
>= 0 && right
<= cw
&&
8058 top
>= 0 && bottom
<= ch
);
8062 // is the cell partly visible ?
8064 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8065 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8070 // make the specified cell location visible by doing a minimal amount
8073 void wxGrid::MakeCellVisible( int row
, int col
)
8077 int xpos
= -1, ypos
= -1;
8079 if ( row
>= 0 && row
< m_numRows
&&
8080 col
>= 0 && col
< m_numCols
)
8082 // get the cell rectangle in logical coords
8084 wxRect
r( CellToRect( row
, col
) );
8086 // convert to device coords
8088 int left
, top
, right
, bottom
;
8089 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8090 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8093 m_gridWin
->GetClientSize( &cw
, &ch
);
8099 else if ( bottom
> ch
)
8101 int h
= r
.GetHeight();
8103 for ( i
= row
-1; i
>= 0; i
-- )
8105 int rowHeight
= GetRowHeight(i
);
8106 if ( h
+ rowHeight
> ch
)
8113 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8114 // have rounding errors (this is important, because if we do, we
8115 // might not scroll at all and some cells won't be redrawn)
8117 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8119 ypos
+= m_scrollLineY
;
8126 else if ( right
> cw
)
8128 // position the view so that the cell is on the right
8130 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8131 xpos
= x0
+ (right
- cw
);
8133 // see comment for ypos above
8134 xpos
+= m_scrollLineX
;
8137 if ( xpos
!= -1 || ypos
!= -1 )
8140 xpos
/= m_scrollLineX
;
8142 ypos
/= m_scrollLineY
;
8143 Scroll( xpos
, ypos
);
8151 // ------ Grid cursor movement functions
8154 bool wxGrid::MoveCursorUp( bool expandSelection
)
8156 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8157 m_currentCellCoords
.GetRow() >= 0 )
8159 if ( expandSelection
)
8161 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8162 m_selectingKeyboard
= m_currentCellCoords
;
8163 if ( m_selectingKeyboard
.GetRow() > 0 )
8165 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8166 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8167 m_selectingKeyboard
.GetCol() );
8168 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8171 else if ( m_currentCellCoords
.GetRow() > 0 )
8174 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8175 m_currentCellCoords
.GetCol() );
8176 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8177 m_currentCellCoords
.GetCol() );
8188 bool wxGrid::MoveCursorDown( bool expandSelection
)
8190 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8191 m_currentCellCoords
.GetRow() < m_numRows
)
8193 if ( expandSelection
)
8195 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8196 m_selectingKeyboard
= m_currentCellCoords
;
8197 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8199 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8200 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8201 m_selectingKeyboard
.GetCol() );
8202 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8205 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8208 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8209 m_currentCellCoords
.GetCol() );
8210 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8211 m_currentCellCoords
.GetCol() );
8222 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8224 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8225 m_currentCellCoords
.GetCol() >= 0 )
8227 if ( expandSelection
)
8229 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8230 m_selectingKeyboard
= m_currentCellCoords
;
8231 if ( m_selectingKeyboard
.GetCol() > 0 )
8233 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8234 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8235 m_selectingKeyboard
.GetCol() );
8236 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8239 else if ( m_currentCellCoords
.GetCol() > 0 )
8242 MakeCellVisible( m_currentCellCoords
.GetRow(),
8243 m_currentCellCoords
.GetCol() - 1 );
8244 SetCurrentCell( m_currentCellCoords
.GetRow(),
8245 m_currentCellCoords
.GetCol() - 1 );
8256 bool wxGrid::MoveCursorRight( bool expandSelection
)
8258 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8259 m_currentCellCoords
.GetCol() < m_numCols
)
8261 if ( expandSelection
)
8263 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8264 m_selectingKeyboard
= m_currentCellCoords
;
8265 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8267 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8268 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8269 m_selectingKeyboard
.GetCol() );
8270 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8273 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8276 MakeCellVisible( m_currentCellCoords
.GetRow(),
8277 m_currentCellCoords
.GetCol() + 1 );
8278 SetCurrentCell( m_currentCellCoords
.GetRow(),
8279 m_currentCellCoords
.GetCol() + 1 );
8290 bool wxGrid::MovePageUp()
8292 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8294 int row
= m_currentCellCoords
.GetRow();
8298 m_gridWin
->GetClientSize( &cw
, &ch
);
8300 int y
= GetRowTop(row
);
8301 int newRow
= internalYToRow( y
- ch
+ 1 );
8303 if ( newRow
== row
)
8305 //row > 0 , so newrow can never be less than 0 here.
8309 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8310 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8318 bool wxGrid::MovePageDown()
8320 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8322 int row
= m_currentCellCoords
.GetRow();
8323 if ( (row
+1) < m_numRows
)
8326 m_gridWin
->GetClientSize( &cw
, &ch
);
8328 int y
= GetRowTop(row
);
8329 int newRow
= internalYToRow( y
+ ch
);
8330 if ( newRow
== row
)
8332 // row < m_numRows , so newrow can't overflow here.
8336 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8337 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8345 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8348 m_currentCellCoords
!= wxGridNoCellCoords
&&
8349 m_currentCellCoords
.GetRow() > 0 )
8351 int row
= m_currentCellCoords
.GetRow();
8352 int col
= m_currentCellCoords
.GetCol();
8354 if ( m_table
->IsEmptyCell(row
, col
) )
8356 // starting in an empty cell: find the next block of
8362 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8365 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8367 // starting at the top of a block: find the next block
8373 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8378 // starting within a block: find the top of the block
8383 if ( m_table
->IsEmptyCell(row
, col
) )
8391 MakeCellVisible( row
, col
);
8392 if ( expandSelection
)
8394 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8395 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8400 SetCurrentCell( row
, col
);
8408 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8411 m_currentCellCoords
!= wxGridNoCellCoords
&&
8412 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8414 int row
= m_currentCellCoords
.GetRow();
8415 int col
= m_currentCellCoords
.GetCol();
8417 if ( m_table
->IsEmptyCell(row
, col
) )
8419 // starting in an empty cell: find the next block of
8422 while ( row
< m_numRows
-1 )
8425 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8428 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8430 // starting at the bottom of a block: find the next block
8433 while ( row
< m_numRows
-1 )
8436 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8441 // starting within a block: find the bottom of the block
8443 while ( row
< m_numRows
-1 )
8446 if ( m_table
->IsEmptyCell(row
, col
) )
8454 MakeCellVisible( row
, col
);
8455 if ( expandSelection
)
8457 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8458 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8463 SetCurrentCell( row
, col
);
8472 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8475 m_currentCellCoords
!= wxGridNoCellCoords
&&
8476 m_currentCellCoords
.GetCol() > 0 )
8478 int row
= m_currentCellCoords
.GetRow();
8479 int col
= m_currentCellCoords
.GetCol();
8481 if ( m_table
->IsEmptyCell(row
, col
) )
8483 // starting in an empty cell: find the next block of
8489 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8492 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8494 // starting at the left of a block: find the next block
8500 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8505 // starting within a block: find the left of the block
8510 if ( m_table
->IsEmptyCell(row
, col
) )
8518 MakeCellVisible( row
, col
);
8519 if ( expandSelection
)
8521 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8522 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8527 SetCurrentCell( row
, col
);
8536 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8539 m_currentCellCoords
!= wxGridNoCellCoords
&&
8540 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8542 int row
= m_currentCellCoords
.GetRow();
8543 int col
= m_currentCellCoords
.GetCol();
8545 if ( m_table
->IsEmptyCell(row
, col
) )
8547 // starting in an empty cell: find the next block of
8550 while ( col
< m_numCols
-1 )
8553 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8556 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8558 // starting at the right of a block: find the next block
8561 while ( col
< m_numCols
-1 )
8564 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8569 // starting within a block: find the right of the block
8571 while ( col
< m_numCols
-1 )
8574 if ( m_table
->IsEmptyCell(row
, col
) )
8582 MakeCellVisible( row
, col
);
8583 if ( expandSelection
)
8585 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8586 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8591 SetCurrentCell( row
, col
);
8603 // ------ Label values and formatting
8606 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8608 *horiz
= m_rowLabelHorizAlign
;
8609 *vert
= m_rowLabelVertAlign
;
8612 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8614 *horiz
= m_colLabelHorizAlign
;
8615 *vert
= m_colLabelVertAlign
;
8618 int wxGrid::GetColLabelTextOrientation()
8620 return m_colLabelTextOrientation
;
8623 wxString
wxGrid::GetRowLabelValue( int row
)
8627 return m_table
->GetRowLabelValue( row
);
8637 wxString
wxGrid::GetColLabelValue( int col
)
8641 return m_table
->GetColLabelValue( col
);
8652 void wxGrid::SetRowLabelSize( int width
)
8654 width
= wxMax( width
, 0 );
8655 if ( width
!= m_rowLabelWidth
)
8659 m_rowLabelWin
->Show( false );
8660 m_cornerLabelWin
->Show( false );
8662 else if ( m_rowLabelWidth
== 0 )
8664 m_rowLabelWin
->Show( true );
8665 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8668 m_rowLabelWidth
= width
;
8670 wxScrolledWindow::Refresh( true );
8675 void wxGrid::SetColLabelSize( int height
)
8677 height
= wxMax( height
, 0 );
8678 if ( height
!= m_colLabelHeight
)
8682 m_colLabelWin
->Show( false );
8683 m_cornerLabelWin
->Show( false );
8685 else if ( m_colLabelHeight
== 0 )
8687 m_colLabelWin
->Show( true );
8688 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8691 m_colLabelHeight
= height
;
8693 wxScrolledWindow::Refresh( true );
8698 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8700 if ( m_labelBackgroundColour
!= colour
)
8702 m_labelBackgroundColour
= colour
;
8703 m_rowLabelWin
->SetBackgroundColour( colour
);
8704 m_colLabelWin
->SetBackgroundColour( colour
);
8705 m_cornerLabelWin
->SetBackgroundColour( colour
);
8707 if ( !GetBatchCount() )
8709 m_rowLabelWin
->Refresh();
8710 m_colLabelWin
->Refresh();
8711 m_cornerLabelWin
->Refresh();
8716 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8718 if ( m_labelTextColour
!= colour
)
8720 m_labelTextColour
= colour
;
8721 if ( !GetBatchCount() )
8723 m_rowLabelWin
->Refresh();
8724 m_colLabelWin
->Refresh();
8729 void wxGrid::SetLabelFont( const wxFont
& font
)
8732 if ( !GetBatchCount() )
8734 m_rowLabelWin
->Refresh();
8735 m_colLabelWin
->Refresh();
8739 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8741 // allow old (incorrect) defs to be used
8744 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8745 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8746 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8751 case wxTOP
: vert
= wxALIGN_TOP
; break;
8752 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8753 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8756 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8758 m_rowLabelHorizAlign
= horiz
;
8761 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8763 m_rowLabelVertAlign
= vert
;
8766 if ( !GetBatchCount() )
8768 m_rowLabelWin
->Refresh();
8772 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8774 // allow old (incorrect) defs to be used
8777 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8778 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8779 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8784 case wxTOP
: vert
= wxALIGN_TOP
; break;
8785 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8786 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8789 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8791 m_colLabelHorizAlign
= horiz
;
8794 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8796 m_colLabelVertAlign
= vert
;
8799 if ( !GetBatchCount() )
8801 m_colLabelWin
->Refresh();
8805 // Note: under MSW, the default column label font must be changed because it
8806 // does not support vertical printing
8808 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8809 // pGrid->SetLabelFont(font);
8810 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8812 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8814 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8816 m_colLabelTextOrientation
= textOrientation
;
8819 if ( !GetBatchCount() )
8821 m_colLabelWin
->Refresh();
8825 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8829 m_table
->SetRowLabelValue( row
, s
);
8830 if ( !GetBatchCount() )
8832 wxRect rect
= CellToRect( row
, 0);
8833 if ( rect
.height
> 0 )
8835 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8837 rect
.width
= m_rowLabelWidth
;
8838 m_rowLabelWin
->Refresh( true, &rect
);
8844 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8848 m_table
->SetColLabelValue( col
, s
);
8849 if ( !GetBatchCount() )
8851 wxRect rect
= CellToRect( 0, col
);
8852 if ( rect
.width
> 0 )
8854 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8856 rect
.height
= m_colLabelHeight
;
8857 m_colLabelWin
->Refresh( true, &rect
);
8863 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8865 if ( m_gridLineColour
!= colour
)
8867 m_gridLineColour
= colour
;
8869 wxClientDC
dc( m_gridWin
);
8871 DrawAllGridLines( dc
, wxRegion() );
8876 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8878 if ( m_cellHighlightColour
!= colour
)
8880 m_cellHighlightColour
= colour
;
8882 wxClientDC
dc( m_gridWin
);
8884 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8885 DrawCellHighlight(dc
, attr
);
8890 void wxGrid::SetCellHighlightPenWidth(int width
)
8892 if (m_cellHighlightPenWidth
!= width
) {
8893 m_cellHighlightPenWidth
= width
;
8895 // Just redrawing the cell highlight is not enough since that won't
8896 // make any visible change if the the thickness is getting smaller.
8897 int row
= m_currentCellCoords
.GetRow();
8898 int col
= m_currentCellCoords
.GetCol();
8899 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8901 wxRect rect
= CellToRect(row
, col
);
8902 m_gridWin
->Refresh(true, &rect
);
8906 void wxGrid::SetCellHighlightROPenWidth(int width
)
8908 if (m_cellHighlightROPenWidth
!= width
) {
8909 m_cellHighlightROPenWidth
= width
;
8911 // Just redrawing the cell highlight is not enough since that won't
8912 // make any visible change if the the thickness is getting smaller.
8913 int row
= m_currentCellCoords
.GetRow();
8914 int col
= m_currentCellCoords
.GetCol();
8915 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8917 wxRect rect
= CellToRect(row
, col
);
8918 m_gridWin
->Refresh(true, &rect
);
8922 void wxGrid::EnableGridLines( bool enable
)
8924 if ( enable
!= m_gridLinesEnabled
)
8926 m_gridLinesEnabled
= enable
;
8928 if ( !GetBatchCount() )
8932 wxClientDC
dc( m_gridWin
);
8934 DrawAllGridLines( dc
, wxRegion() );
8938 m_gridWin
->Refresh();
8945 int wxGrid::GetDefaultRowSize()
8947 return m_defaultRowHeight
;
8950 int wxGrid::GetRowSize( int row
)
8952 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8954 return GetRowHeight(row
);
8957 int wxGrid::GetDefaultColSize()
8959 return m_defaultColWidth
;
8962 int wxGrid::GetColSize( int col
)
8964 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8966 return GetColWidth(col
);
8969 // ============================================================================
8970 // access to the grid attributes: each of them has a default value in the grid
8971 // itself and may be overidden on a per-cell basis
8972 // ============================================================================
8974 // ----------------------------------------------------------------------------
8975 // setting default attributes
8976 // ----------------------------------------------------------------------------
8978 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8980 m_defaultCellAttr
->SetBackgroundColour(col
);
8982 m_gridWin
->SetBackgroundColour(col
);
8986 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8988 m_defaultCellAttr
->SetTextColour(col
);
8991 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8993 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8996 void wxGrid::SetDefaultCellOverflow( bool allow
)
8998 m_defaultCellAttr
->SetOverflow(allow
);
9001 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9003 m_defaultCellAttr
->SetFont(font
);
9007 // For editors and renderers the type registry takes precedence over the
9008 // default attr, so we need to register the new editor/renderer for the string
9009 // data type in order to make setting a default editor/renderer appear to
9012 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9014 RegisterDataType(wxGRID_VALUE_STRING
,
9016 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9019 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9021 RegisterDataType(wxGRID_VALUE_STRING
,
9022 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9026 // ----------------------------------------------------------------------------
9027 // access to the default attrbiutes
9028 // ----------------------------------------------------------------------------
9030 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9032 return m_defaultCellAttr
->GetBackgroundColour();
9035 wxColour
wxGrid::GetDefaultCellTextColour()
9037 return m_defaultCellAttr
->GetTextColour();
9040 wxFont
wxGrid::GetDefaultCellFont()
9042 return m_defaultCellAttr
->GetFont();
9045 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9047 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9050 bool wxGrid::GetDefaultCellOverflow()
9052 return m_defaultCellAttr
->GetOverflow();
9055 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9057 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9060 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9062 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9065 // ----------------------------------------------------------------------------
9066 // access to cell attributes
9067 // ----------------------------------------------------------------------------
9069 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9071 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9072 wxColour colour
= attr
->GetBackgroundColour();
9077 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9079 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9080 wxColour colour
= attr
->GetTextColour();
9085 wxFont
wxGrid::GetCellFont( int row
, int col
)
9087 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9088 wxFont font
= attr
->GetFont();
9093 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9095 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9096 attr
->GetAlignment(horiz
, vert
);
9100 bool wxGrid::GetCellOverflow( int row
, int col
)
9102 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9103 bool allow
= attr
->GetOverflow();
9108 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9110 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9111 attr
->GetSize( num_rows
, num_cols
);
9115 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9117 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9118 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9124 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9126 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9127 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9133 bool wxGrid::IsReadOnly(int row
, int col
) const
9135 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9136 bool isReadOnly
= attr
->IsReadOnly();
9141 // ----------------------------------------------------------------------------
9142 // attribute support: cache, automatic provider creation, ...
9143 // ----------------------------------------------------------------------------
9145 bool wxGrid::CanHaveAttributes()
9152 return m_table
->CanHaveAttributes();
9155 void wxGrid::ClearAttrCache()
9157 if ( m_attrCache
.row
!= -1 )
9159 wxSafeDecRef(m_attrCache
.attr
);
9160 m_attrCache
.attr
= NULL
;
9161 m_attrCache
.row
= -1;
9165 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9169 wxGrid
*self
= (wxGrid
*)this; // const_cast
9171 self
->ClearAttrCache();
9172 self
->m_attrCache
.row
= row
;
9173 self
->m_attrCache
.col
= col
;
9174 self
->m_attrCache
.attr
= attr
;
9179 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9181 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9183 *attr
= m_attrCache
.attr
;
9184 wxSafeIncRef(m_attrCache
.attr
);
9186 #ifdef DEBUG_ATTR_CACHE
9187 gs_nAttrCacheHits
++;
9194 #ifdef DEBUG_ATTR_CACHE
9195 gs_nAttrCacheMisses
++;
9201 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9203 wxGridCellAttr
*attr
= NULL
;
9204 // Additional test to avoid looking at the cache e.g. for
9205 // wxNoCellCoords, as this will confuse memory management.
9208 if ( !LookupAttr(row
, col
, &attr
) )
9210 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9211 : (wxGridCellAttr
*)NULL
;
9212 CacheAttr(row
, col
, attr
);
9217 attr
->SetDefAttr(m_defaultCellAttr
);
9221 attr
= m_defaultCellAttr
;
9228 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9230 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9231 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9233 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9234 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9236 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9239 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9241 // artificially inc the ref count to match DecRef() in caller
9243 m_table
->SetAttr(attr
, row
, col
);
9249 // ----------------------------------------------------------------------------
9250 // setting column attributes (wrappers around SetColAttr)
9251 // ----------------------------------------------------------------------------
9253 void wxGrid::SetColFormatBool(int col
)
9255 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9258 void wxGrid::SetColFormatNumber(int col
)
9260 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9263 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9265 wxString typeName
= wxGRID_VALUE_FLOAT
;
9266 if ( (width
!= -1) || (precision
!= -1) )
9268 typeName
<< _T(':') << width
<< _T(',') << precision
;
9271 SetColFormatCustom(col
, typeName
);
9274 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9276 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9278 attr
= new wxGridCellAttr
;
9279 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9280 attr
->SetRenderer(renderer
);
9282 SetColAttr(col
, attr
);
9286 // ----------------------------------------------------------------------------
9287 // setting cell attributes: this is forwarded to the table
9288 // ----------------------------------------------------------------------------
9290 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9292 if ( CanHaveAttributes() )
9294 m_table
->SetAttr(attr
, row
, col
);
9303 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9305 if ( CanHaveAttributes() )
9307 m_table
->SetRowAttr(attr
, row
);
9316 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9318 if ( CanHaveAttributes() )
9320 m_table
->SetColAttr(attr
, col
);
9329 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9331 if ( CanHaveAttributes() )
9333 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9334 attr
->SetBackgroundColour(colour
);
9339 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9341 if ( CanHaveAttributes() )
9343 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9344 attr
->SetTextColour(colour
);
9349 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9351 if ( CanHaveAttributes() )
9353 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9354 attr
->SetFont(font
);
9359 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9361 if ( CanHaveAttributes() )
9363 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9364 attr
->SetAlignment(horiz
, vert
);
9369 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9371 if ( CanHaveAttributes() )
9373 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9374 attr
->SetOverflow(allow
);
9379 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9381 if ( CanHaveAttributes() )
9383 int cell_rows
, cell_cols
;
9385 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9386 attr
->GetSize(&cell_rows
, &cell_cols
);
9387 attr
->SetSize(num_rows
, num_cols
);
9390 // Cannot set the size of a cell to 0 or negative values
9391 // While it is perfectly legal to do that, this function cannot
9392 // handle all the possibilies, do it by hand by getting the CellAttr.
9393 // You can only set the size of a cell to 1,1 or greater with this fn
9394 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9395 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9396 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9397 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9399 // if this was already a multicell then "turn off" the other cells first
9400 if ((cell_rows
> 1) || (cell_rows
> 1))
9403 for (j
=row
; j
<row
+cell_rows
; j
++)
9405 for (i
=col
; i
<col
+cell_cols
; i
++)
9407 if ((i
!= col
) || (j
!= row
))
9409 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9410 attr_stub
->SetSize( 1, 1 );
9411 attr_stub
->DecRef();
9417 // mark the cells that will be covered by this cell to
9418 // negative or zero values to point back at this cell
9419 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9422 for (j
=row
; j
<row
+num_rows
; j
++)
9424 for (i
=col
; i
<col
+num_cols
; i
++)
9426 if ((i
!= col
) || (j
!= row
))
9428 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9429 attr_stub
->SetSize( row
-j
, col
-i
);
9430 attr_stub
->DecRef();
9438 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9440 if ( CanHaveAttributes() )
9442 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9443 attr
->SetRenderer(renderer
);
9448 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9450 if ( CanHaveAttributes() )
9452 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9453 attr
->SetEditor(editor
);
9458 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9460 if ( CanHaveAttributes() )
9462 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9463 attr
->SetReadOnly(isReadOnly
);
9468 // ----------------------------------------------------------------------------
9469 // Data type registration
9470 // ----------------------------------------------------------------------------
9472 void wxGrid::RegisterDataType(const wxString
& typeName
,
9473 wxGridCellRenderer
* renderer
,
9474 wxGridCellEditor
* editor
)
9476 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9480 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9482 wxString typeName
= m_table
->GetTypeName(row
, col
);
9483 return GetDefaultEditorForType(typeName
);
9486 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9488 wxString typeName
= m_table
->GetTypeName(row
, col
);
9489 return GetDefaultRendererForType(typeName
);
9493 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9495 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9496 if ( index
== wxNOT_FOUND
)
9498 wxFAIL_MSG(wxT("Unknown data type name"));
9503 return m_typeRegistry
->GetEditor(index
);
9507 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9509 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9510 if ( index
== wxNOT_FOUND
)
9512 wxFAIL_MSG(wxT("Unknown data type name"));
9517 return m_typeRegistry
->GetRenderer(index
);
9521 // ----------------------------------------------------------------------------
9523 // ----------------------------------------------------------------------------
9525 void wxGrid::EnableDragRowSize( bool enable
)
9527 m_canDragRowSize
= enable
;
9531 void wxGrid::EnableDragColSize( bool enable
)
9533 m_canDragColSize
= enable
;
9536 void wxGrid::EnableDragGridSize( bool enable
)
9538 m_canDragGridSize
= enable
;
9541 void wxGrid::EnableDragCell( bool enable
)
9543 m_canDragCell
= enable
;
9546 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9548 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9550 if ( resizeExistingRows
)
9552 // since we are resizing all rows to the default row size,
9553 // we can simply clear the row heights and row bottoms
9554 // arrays (which also allows us to take advantage of
9555 // some speed optimisations)
9556 m_rowHeights
.Empty();
9557 m_rowBottoms
.Empty();
9558 if ( !GetBatchCount() )
9563 void wxGrid::SetRowSize( int row
, int height
)
9565 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9567 // See comment in SetColSize
9568 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9570 if ( m_rowHeights
.IsEmpty() )
9572 // need to really create the array
9576 int h
= wxMax( 0, height
);
9577 int diff
= h
- m_rowHeights
[row
];
9579 m_rowHeights
[row
] = h
;
9581 for ( i
= row
; i
< m_numRows
; i
++ )
9583 m_rowBottoms
[i
] += diff
;
9585 if ( !GetBatchCount() )
9589 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9591 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9593 if ( resizeExistingCols
)
9595 // since we are resizing all columns to the default column size,
9596 // we can simply clear the col widths and col rights
9597 // arrays (which also allows us to take advantage of
9598 // some speed optimisations)
9599 m_colWidths
.Empty();
9600 m_colRights
.Empty();
9601 if ( !GetBatchCount() )
9606 void wxGrid::SetColSize( int col
, int width
)
9608 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9610 // should we check that it's bigger than GetColMinimalWidth(col) here?
9612 // No, because it is reasonable to assume the library user know's
9613 // what he is doing. However whe should test against the weaker
9614 // constariant of minimalAcceptableWidth, as this breaks rendering
9616 // This test then fixes sf.net bug #645734
9618 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9620 if ( m_colWidths
.IsEmpty() )
9622 // need to really create the array
9626 // if < 0 calc new width from label
9630 wxArrayString lines
;
9631 wxClientDC
dc(m_colLabelWin
);
9632 dc
.SetFont(GetLabelFont());
9633 StringToLines(GetColLabelValue(col
), lines
);
9634 GetTextBoxSize(dc
, lines
, &w
, &h
);
9637 int w
= wxMax( 0, width
);
9638 int diff
= w
- m_colWidths
[col
];
9639 m_colWidths
[col
] = w
;
9642 for ( i
= col
; i
< m_numCols
; i
++ )
9644 m_colRights
[i
] += diff
;
9646 if ( !GetBatchCount() )
9651 void wxGrid::SetColMinimalWidth( int col
, int width
)
9653 if (width
> GetColMinimalAcceptableWidth()) {
9654 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9655 m_colMinWidths
[key
] = width
;
9659 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9661 if (width
> GetRowMinimalAcceptableHeight()) {
9662 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9663 m_rowMinHeights
[key
] = width
;
9667 int wxGrid::GetColMinimalWidth(int col
) const
9669 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9670 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9671 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9674 int wxGrid::GetRowMinimalHeight(int row
) const
9676 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9677 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9678 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9681 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9683 // We do allow a width of 0 since this gives us
9684 // an easy way to temporarily hidding columns.
9687 m_minAcceptableColWidth
= width
;
9690 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9692 // We do allow a height of 0 since this gives us
9693 // an easy way to temporarily hidding rows.
9696 m_minAcceptableRowHeight
= height
;
9699 int wxGrid::GetColMinimalAcceptableWidth() const
9701 return m_minAcceptableColWidth
;
9704 int wxGrid::GetRowMinimalAcceptableHeight() const
9706 return m_minAcceptableRowHeight
;
9709 // ----------------------------------------------------------------------------
9711 // ----------------------------------------------------------------------------
9713 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9715 wxClientDC
dc(m_gridWin
);
9717 //Cancel editting of cell
9718 HideCellEditControl();
9719 SaveEditControlValue();
9721 // init both of them to avoid compiler warnings, even if weo nly need one
9729 wxCoord extent
, extentMax
= 0;
9730 int max
= column
? m_numRows
: m_numCols
;
9731 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9738 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9739 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9742 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9743 extent
= column
? size
.x
: size
.y
;
9744 if ( extent
> extentMax
)
9755 // now also compare with the column label extent
9757 dc
.SetFont( GetLabelFont() );
9761 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9762 if( GetColLabelTextOrientation() == wxVERTICAL
)
9766 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9768 extent
= column
? w
: h
;
9769 if ( extent
> extentMax
)
9776 // empty column - give default extent (notice that if extentMax is less
9777 // than default extent but != 0, it's ok)
9778 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9784 // leave some space around text
9795 SetColSize(col
, extentMax
);
9796 if ( !GetBatchCount() )
9799 m_gridWin
->GetClientSize( &cw
, &ch
);
9800 wxRect
rect ( CellToRect( 0, col
) );
9802 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9803 rect
.width
= cw
- rect
.x
;
9804 rect
.height
= m_colLabelHeight
;
9805 m_colLabelWin
->Refresh( true, &rect
);
9810 SetRowSize(row
, extentMax
);
9811 if ( !GetBatchCount() )
9814 m_gridWin
->GetClientSize( &cw
, &ch
);
9815 wxRect
rect ( CellToRect( row
, 0 ) );
9817 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9818 rect
.width
= m_rowLabelWidth
;
9819 rect
.height
= ch
- rect
.y
;
9820 m_rowLabelWin
->Refresh( true, &rect
);
9826 SetColMinimalWidth(col
, extentMax
);
9828 SetRowMinimalHeight(row
, extentMax
);
9832 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9834 int width
= m_rowLabelWidth
;
9839 for ( int col
= 0; col
< m_numCols
; col
++ )
9843 AutoSizeColumn(col
, setAsMin
);
9846 width
+= GetColWidth(col
);
9855 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9857 int height
= m_colLabelHeight
;
9862 for ( int row
= 0; row
< m_numRows
; row
++ )
9866 AutoSizeRow(row
, setAsMin
);
9869 height
+= GetRowHeight(row
);
9878 void wxGrid::AutoSize()
9882 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9884 // round up the size to a multiple of scroll step - this ensures that we
9885 // won't get the scrollbars if we're sized exactly to this width
9886 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9888 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9889 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9891 // distribute the extra space between the columns/rows to avoid having
9892 // extra white space
9894 // Remove the extra m_extraWidth + 1 added above
9895 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9896 if ( diff
&& m_numCols
)
9898 // try to resize the columns uniformly
9899 wxCoord diffPerCol
= diff
/ m_numCols
;
9902 for ( int col
= 0; col
< m_numCols
; col
++ )
9904 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9908 // add remaining amount to the last columns
9909 diff
-= diffPerCol
* m_numCols
;
9912 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9914 SetColSize(col
, GetColWidth(col
) + 1);
9920 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9921 if ( diff
&& m_numRows
)
9923 // try to resize the columns uniformly
9924 wxCoord diffPerRow
= diff
/ m_numRows
;
9927 for ( int row
= 0; row
< m_numRows
; row
++ )
9929 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9933 // add remaining amount to the last rows
9934 diff
-= diffPerRow
* m_numRows
;
9937 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9939 SetRowSize(row
, GetRowHeight(row
) + 1);
9946 SetClientSize(sizeFit
);
9949 void wxGrid::AutoSizeRowLabelSize( int row
)
9951 wxArrayString lines
;
9954 // Hide the edit control, so it
9955 // won't interfer with drag-shrinking.
9956 if( IsCellEditControlShown() )
9958 HideCellEditControl();
9959 SaveEditControlValue();
9962 // autosize row height depending on label text
9963 StringToLines( GetRowLabelValue( row
), lines
);
9964 wxClientDC
dc( m_rowLabelWin
);
9965 GetTextBoxSize( dc
, lines
, &w
, &h
);
9966 if( h
< m_defaultRowHeight
)
9967 h
= m_defaultRowHeight
;
9972 void wxGrid::AutoSizeColLabelSize( int col
)
9974 wxArrayString lines
;
9977 // Hide the edit control, so it
9978 // won't interfer with drag-shrinking.
9979 if( IsCellEditControlShown() )
9981 HideCellEditControl();
9982 SaveEditControlValue();
9985 // autosize column width depending on label text
9986 StringToLines( GetColLabelValue( col
), lines
);
9987 wxClientDC
dc( m_colLabelWin
);
9988 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9989 GetTextBoxSize( dc
, lines
, &w
, &h
);
9991 GetTextBoxSize( dc
, lines
, &h
, &w
);
9992 if( w
< m_defaultColWidth
)
9993 w
= m_defaultColWidth
;
9998 wxSize
wxGrid::DoGetBestSize() const
10000 // don't set sizes, only calculate them
10001 wxGrid
*self
= (wxGrid
*)this; // const_cast
10004 width
= self
->SetOrCalcColumnSizes(true);
10005 height
= self
->SetOrCalcRowSizes(true);
10007 if (!width
) width
=100;
10008 if (!height
) height
=80;
10010 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10011 // of the scrollbars, is there any magic incantaion for that?
10013 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10015 width
+= 1 + xpu
- (width
% xpu
);
10017 height
+= 1 + ypu
- (height
% ypu
);
10019 // limit to 1/4 of the screen size
10020 int maxwidth
, maxheight
;
10021 wxDisplaySize( & maxwidth
, & maxheight
);
10024 if ( width
> maxwidth
) width
= maxwidth
;
10025 if ( height
> maxheight
) height
= maxheight
;
10028 wxSize
best(width
, height
);
10029 // NOTE: This size should be cached, but first we need to add calls to
10030 // InvalidateBestSize everywhere that could change the results of this
10032 // CacheBestSize(size);
10042 wxPen
& wxGrid::GetDividerPen() const
10047 // ----------------------------------------------------------------------------
10048 // cell value accessor functions
10049 // ----------------------------------------------------------------------------
10051 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10055 m_table
->SetValue( row
, col
, s
);
10056 if ( !GetBatchCount() )
10059 wxRect
rect( CellToRect( row
, col
) );
10061 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10062 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10063 m_gridWin
->Refresh( false, &rect
);
10066 if ( m_currentCellCoords
.GetRow() == row
&&
10067 m_currentCellCoords
.GetCol() == col
&&
10068 IsCellEditControlShown())
10069 // Note: If we are using IsCellEditControlEnabled,
10070 // this interacts badly with calling SetCellValue from
10071 // an EVT_GRID_CELL_CHANGE handler.
10073 HideCellEditControl();
10074 ShowCellEditControl(); // will reread data from table
10081 // ------ Block, row and col selection
10084 void wxGrid::SelectRow( int row
, bool addToSelected
)
10086 if ( IsSelection() && !addToSelected
)
10090 m_selection
->SelectRow( row
, false, addToSelected
);
10094 void wxGrid::SelectCol( int col
, bool addToSelected
)
10096 if ( IsSelection() && !addToSelected
)
10100 m_selection
->SelectCol( col
, false, addToSelected
);
10104 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10105 bool addToSelected
)
10107 if ( IsSelection() && !addToSelected
)
10111 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10112 false, addToSelected
);
10116 void wxGrid::SelectAll()
10118 if ( m_numRows
> 0 && m_numCols
> 0 )
10121 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10126 // ------ Cell, row and col deselection
10129 void wxGrid::DeselectRow( int row
)
10131 if ( !m_selection
)
10134 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10136 if ( m_selection
->IsInSelection(row
, 0 ) )
10137 m_selection
->ToggleCellSelection( row
, 0);
10141 int nCols
= GetNumberCols();
10142 for ( int i
= 0; i
< nCols
; i
++ )
10144 if ( m_selection
->IsInSelection(row
, i
) )
10145 m_selection
->ToggleCellSelection( row
, i
);
10150 void wxGrid::DeselectCol( int col
)
10152 if ( !m_selection
)
10155 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10157 if ( m_selection
->IsInSelection(0, col
) )
10158 m_selection
->ToggleCellSelection( 0, col
);
10162 int nRows
= GetNumberRows();
10163 for ( int i
= 0; i
< nRows
; i
++ )
10165 if ( m_selection
->IsInSelection(i
, col
) )
10166 m_selection
->ToggleCellSelection(i
, col
);
10171 void wxGrid::DeselectCell( int row
, int col
)
10173 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10174 m_selection
->ToggleCellSelection(row
, col
);
10177 bool wxGrid::IsSelection()
10179 return ( m_selection
&& (m_selection
->IsSelection() ||
10180 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10181 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10184 bool wxGrid::IsInSelection( int row
, int col
) const
10186 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10187 ( row
>= m_selectingTopLeft
.GetRow() &&
10188 col
>= m_selectingTopLeft
.GetCol() &&
10189 row
<= m_selectingBottomRight
.GetRow() &&
10190 col
<= m_selectingBottomRight
.GetCol() )) );
10193 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10195 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10196 return m_selection
->m_cellSelection
;
10198 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10200 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10201 return m_selection
->m_blockSelectionTopLeft
;
10203 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10205 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10206 return m_selection
->m_blockSelectionBottomRight
;
10208 wxArrayInt
wxGrid::GetSelectedRows() const
10210 if (!m_selection
) { wxArrayInt a
; return a
; }
10211 return m_selection
->m_rowSelection
;
10213 wxArrayInt
wxGrid::GetSelectedCols() const
10215 if (!m_selection
) { wxArrayInt a
; return a
; }
10216 return m_selection
->m_colSelection
;
10220 void wxGrid::ClearSelection()
10222 m_selectingTopLeft
= wxGridNoCellCoords
;
10223 m_selectingBottomRight
= wxGridNoCellCoords
;
10225 m_selection
->ClearSelection();
10229 // This function returns the rectangle that encloses the given block
10230 // in device coords clipped to the client size of the grid window.
10232 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10233 const wxGridCellCoords
&bottomRight
)
10235 wxRect
rect( wxGridNoCellRect
);
10238 cellRect
= CellToRect( topLeft
);
10239 if ( cellRect
!= wxGridNoCellRect
)
10245 rect
= wxRect(0,0,0,0);
10248 cellRect
= CellToRect( bottomRight
);
10249 if ( cellRect
!= wxGridNoCellRect
)
10255 return wxGridNoCellRect
;
10259 int left
= rect
.GetLeft();
10260 int top
= rect
.GetTop();
10261 int right
= rect
.GetRight();
10262 int bottom
= rect
.GetBottom();
10264 int leftCol
= topLeft
.GetCol();
10265 int topRow
= topLeft
.GetRow();
10266 int rightCol
= bottomRight
.GetCol();
10267 int bottomRow
= bottomRight
.GetRow();
10285 topRow
= bottomRow
;
10290 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10292 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10294 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10296 cellRect
= CellToRect( j
, i
);
10298 if (cellRect
.x
< left
)
10300 if (cellRect
.y
< top
)
10302 if (cellRect
.x
+ cellRect
.width
> right
)
10303 right
= cellRect
.x
+ cellRect
.width
;
10304 if (cellRect
.y
+ cellRect
.height
> bottom
)
10305 bottom
= cellRect
.y
+ cellRect
.height
;
10307 else i
= rightCol
; // jump over inner cells.
10311 // convert to scrolled coords
10313 CalcScrolledPosition( left
, top
, &left
, &top
);
10314 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10317 m_gridWin
->GetClientSize( &cw
, &ch
);
10319 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10320 return wxRect(0,0,0,0);
10322 rect
.SetLeft( wxMax(0, left
) );
10323 rect
.SetTop( wxMax(0, top
) );
10324 rect
.SetRight( wxMin(cw
, right
) );
10325 rect
.SetBottom( wxMin(ch
, bottom
) );
10331 // ------ Grid event classes
10334 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10336 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10337 int row
, int col
, int x
, int y
, bool sel
,
10338 bool control
, bool shift
, bool alt
, bool meta
)
10339 : wxNotifyEvent( type
, id
)
10346 m_control
= control
;
10351 SetEventObject(obj
);
10355 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10357 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10358 int rowOrCol
, int x
, int y
,
10359 bool control
, bool shift
, bool alt
, bool meta
)
10360 : wxNotifyEvent( type
, id
)
10362 m_rowOrCol
= rowOrCol
;
10365 m_control
= control
;
10370 SetEventObject(obj
);
10374 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10376 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10377 const wxGridCellCoords
& topLeft
,
10378 const wxGridCellCoords
& bottomRight
,
10379 bool sel
, bool control
,
10380 bool shift
, bool alt
, bool meta
)
10381 : wxNotifyEvent( type
, id
)
10383 m_topLeft
= topLeft
;
10384 m_bottomRight
= bottomRight
;
10386 m_control
= control
;
10391 SetEventObject(obj
);
10395 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10397 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10398 wxObject
* obj
, int row
,
10399 int col
, wxControl
* ctrl
)
10400 : wxCommandEvent(type
, id
)
10402 SetEventObject(obj
);
10408 #endif // wxUSE_GRID