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
)
959 int keycode
= event
.GetKeyCode();
962 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-')
964 wxGridCellTextEditor::StartingKey(event
);
973 if ( wxIsdigit(keycode
) )
975 wxSpinCtrl
* spin
= (wxSpinCtrl
*)m_control
;
976 spin
->SetValue(keycode
- '0');
977 spin
->SetSelection(1,1);
985 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
996 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1000 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1004 // skip the error message below
1009 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1013 // return the value in the spin control if it is there (the text control otherwise)
1014 wxString
wxGridCellNumberEditor::GetValue() const
1021 long value
= Spin()->GetValue();
1022 s
.Printf(wxT("%ld"), value
);
1027 s
= Text()->GetValue();
1033 // ----------------------------------------------------------------------------
1034 // wxGridCellFloatEditor
1035 // ----------------------------------------------------------------------------
1037 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1040 m_precision
= precision
;
1043 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1045 wxEvtHandler
* evtHandler
)
1047 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1049 #if wxUSE_VALIDATORS
1050 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1051 #endif // wxUSE_VALIDATORS
1054 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1056 // first get the value
1057 wxGridTableBase
*table
= grid
->GetTable();
1058 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1060 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1065 wxString sValue
= table
->GetValue(row
, col
);
1066 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1068 wxFAIL_MSG( _T("this cell doesn't have float value") );
1073 DoBeginEdit(GetString());
1076 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1080 wxString
text(Text()->GetValue());
1082 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1084 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1085 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1087 grid
->GetTable()->SetValue(row
, col
, text
);
1094 void wxGridCellFloatEditor::Reset()
1096 DoReset(GetString());
1099 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1101 int keycode
= event
.GetKeyCode();
1103 tmpbuf
[0] = (char) keycode
;
1105 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1107 bool is_decimal_point
= ( strbuf
==
1108 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1110 bool is_decimal_point
= ( strbuf
== _T(".") );
1112 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1113 || is_decimal_point
)
1115 wxGridCellTextEditor::StartingKey(event
);
1117 // skip Skip() below
1124 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1135 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1139 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1141 m_precision
= (int)tmp
;
1143 // skip the error message below
1148 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1152 wxString
wxGridCellFloatEditor::GetString() const
1155 if ( m_width
== -1 )
1157 // default width/precision
1160 else if ( m_precision
== -1 )
1162 // default precision
1163 fmt
.Printf(_T("%%%d.f"), m_width
);
1167 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1170 return wxString::Format(fmt
, m_valueOld
);
1173 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1175 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1177 int keycode
= event
.GetKeyCode();
1178 printf("%d\n", keycode
);
1179 // accept digits, 'e' as in '1e+6', also '-', '+', and '.'
1181 tmpbuf
[0] = (char) keycode
;
1183 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1185 bool is_decimal_point
=
1186 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1187 wxLOCALE_CAT_NUMBER
) );
1189 bool is_decimal_point
= ( strbuf
== _T(".") );
1191 if ( (keycode
< 128) &&
1192 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1193 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1200 #endif // wxUSE_TEXTCTRL
1204 // ----------------------------------------------------------------------------
1205 // wxGridCellBoolEditor
1206 // ----------------------------------------------------------------------------
1208 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1210 wxEvtHandler
* evtHandler
)
1212 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1213 wxDefaultPosition
, wxDefaultSize
,
1216 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1219 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1221 bool resize
= false;
1222 wxSize size
= m_control
->GetSize();
1223 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1225 // check if the checkbox is not too big/small for this cell
1226 wxSize sizeBest
= m_control
->GetBestSize();
1227 if ( !(size
== sizeBest
) )
1229 // reset to default size if it had been made smaller
1235 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1237 // leave 1 pixel margin
1238 size
.x
= size
.y
= minSize
- 2;
1245 m_control
->SetSize(size
);
1248 // position it in the centre of the rectangle (TODO: support alignment?)
1250 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1251 // the checkbox without label still has some space to the right in wxGTK,
1252 // so shift it to the right
1254 #elif defined(__WXMSW__)
1255 // here too, but in other way
1260 int hAlign
= wxALIGN_CENTRE
;
1261 int vAlign
= wxALIGN_CENTRE
;
1263 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1266 if (hAlign
== wxALIGN_LEFT
)
1272 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1274 else if (hAlign
== wxALIGN_RIGHT
)
1276 x
= r
.x
+ r
.width
- size
.x
- 2;
1277 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1279 else if (hAlign
== wxALIGN_CENTRE
)
1281 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1282 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1285 m_control
->Move(x
, y
);
1288 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1290 m_control
->Show(show
);
1294 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1295 CBox()->SetBackgroundColour(colBg
);
1299 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1301 wxASSERT_MSG(m_control
,
1302 wxT("The wxGridCellEditor must be Created first!"));
1304 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1305 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1308 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1309 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1311 CBox()->SetValue(m_startValue
);
1315 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1318 wxASSERT_MSG(m_control
,
1319 wxT("The wxGridCellEditor must be Created first!"));
1321 bool changed
= false;
1322 bool value
= CBox()->GetValue();
1323 if ( value
!= m_startValue
)
1328 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1329 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1331 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1337 void wxGridCellBoolEditor::Reset()
1339 wxASSERT_MSG(m_control
,
1340 wxT("The wxGridCellEditor must be Created first!"));
1342 CBox()->SetValue(m_startValue
);
1345 void wxGridCellBoolEditor::StartingClick()
1347 CBox()->SetValue(!CBox()->GetValue());
1350 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1352 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1354 int keycode
= event
.GetKeyCode();
1367 void wxGridCellBoolEditor::StartingKey(wxKeyEvent
& event
)
1369 int keycode
= event
.GetKeyCode();
1373 CBox()->SetValue(!CBox()->GetValue());
1377 CBox()->SetValue(true);
1381 CBox()->SetValue(false);
1387 // return the value as "1" for true and the empty string for false
1388 wxString
wxGridCellBoolEditor::GetValue() const
1390 bool bSet
= CBox()->GetValue();
1391 return bSet
? _T("1") : wxEmptyString
;
1394 #endif // wxUSE_CHECKBOX
1398 // ----------------------------------------------------------------------------
1399 // wxGridCellChoiceEditor
1400 // ----------------------------------------------------------------------------
1402 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1404 : m_choices(choices
),
1405 m_allowOthers(allowOthers
) { }
1407 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1408 const wxString choices
[],
1410 : m_allowOthers(allowOthers
)
1414 m_choices
.Alloc(count
);
1415 for ( size_t n
= 0; n
< count
; n
++ )
1417 m_choices
.Add(choices
[n
]);
1422 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1424 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1425 editor
->m_allowOthers
= m_allowOthers
;
1426 editor
->m_choices
= m_choices
;
1431 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1433 wxEvtHandler
* evtHandler
)
1435 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1436 wxDefaultPosition
, wxDefaultSize
,
1438 m_allowOthers
? 0 : wxCB_READONLY
);
1440 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1443 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1444 wxGridCellAttr
* attr
)
1446 // as we fill the entire client area, don't do anything here to minimize
1449 // TODO: It doesn't actually fill the client area since the height of a
1450 // combo always defaults to the standard... Until someone has time to
1451 // figure out the right rectangle to paint, just do it the normal way...
1452 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1455 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1457 wxASSERT_MSG(m_control
,
1458 wxT("The wxGridCellEditor must be Created first!"));
1460 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1463 Combo()->SetValue(m_startValue
);
1466 // find the right position, or default to the first if not found
1467 int pos
= Combo()->FindString(m_startValue
);
1470 Combo()->SetSelection(pos
);
1472 Combo()->SetInsertionPointEnd();
1473 Combo()->SetFocus();
1476 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1479 wxString value
= Combo()->GetValue();
1480 if ( value
== m_startValue
)
1483 grid
->GetTable()->SetValue(row
, col
, value
);
1488 void wxGridCellChoiceEditor::Reset()
1490 Combo()->SetValue(m_startValue
);
1491 Combo()->SetInsertionPointEnd();
1494 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1504 wxStringTokenizer
tk(params
, _T(','));
1505 while ( tk
.HasMoreTokens() )
1507 m_choices
.Add(tk
.GetNextToken());
1511 // return the value in the text control
1512 wxString
wxGridCellChoiceEditor::GetValue() const
1514 return Combo()->GetValue();
1517 #endif // wxUSE_COMBOBOX
1519 // ----------------------------------------------------------------------------
1520 // wxGridCellEditorEvtHandler
1521 // ----------------------------------------------------------------------------
1523 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1525 switch ( event
.GetKeyCode() )
1529 m_grid
->DisableCellEditControl();
1533 m_grid
->GetEventHandler()->ProcessEvent( event
);
1537 case WXK_NUMPAD_ENTER
:
1538 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1539 m_editor
->HandleReturn(event
);
1547 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1549 switch ( event
.GetKeyCode() )
1554 case WXK_NUMPAD_ENTER
:
1562 // ----------------------------------------------------------------------------
1563 // wxGridCellWorker is an (almost) empty common base class for
1564 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1565 // ----------------------------------------------------------------------------
1567 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1572 wxGridCellWorker::~wxGridCellWorker()
1576 // ============================================================================
1578 // ============================================================================
1580 // ----------------------------------------------------------------------------
1581 // wxGridCellRenderer
1582 // ----------------------------------------------------------------------------
1584 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1585 wxGridCellAttr
& attr
,
1588 int WXUNUSED(row
), int WXUNUSED(col
),
1591 dc
.SetBackgroundMode( wxSOLID
);
1593 // grey out fields if the grid is disabled
1594 if( grid
.IsEnabled() )
1598 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1602 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1607 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1610 dc
.SetPen( *wxTRANSPARENT_PEN
);
1611 dc
.DrawRectangle(rect
);
1614 // ----------------------------------------------------------------------------
1615 // wxGridCellStringRenderer
1616 // ----------------------------------------------------------------------------
1618 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1619 wxGridCellAttr
& attr
,
1623 dc
.SetBackgroundMode( wxTRANSPARENT
);
1625 // TODO some special colours for attr.IsReadOnly() case?
1627 // different coloured text when the grid is disabled
1628 if( grid
.IsEnabled() )
1632 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1633 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1637 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1638 dc
.SetTextForeground( attr
.GetTextColour() );
1643 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1644 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1647 dc
.SetFont( attr
.GetFont() );
1650 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1652 const wxString
& text
)
1654 wxCoord x
= 0, y
= 0, max_x
= 0;
1655 dc
.SetFont(attr
.GetFont());
1656 wxStringTokenizer
tk(text
, _T('\n'));
1657 while ( tk
.HasMoreTokens() )
1659 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1660 max_x
= wxMax(max_x
, x
);
1663 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1665 return wxSize(max_x
, y
);
1668 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1669 wxGridCellAttr
& attr
,
1673 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1676 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1677 wxGridCellAttr
& attr
,
1679 const wxRect
& rectCell
,
1683 wxRect rect
= rectCell
;
1686 // erase only this cells background, overflow cells should have been erased
1687 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1690 attr
.GetAlignment(&hAlign
, &vAlign
);
1692 int overflowCols
= 0;
1694 if (attr
.GetOverflow())
1696 int cols
= grid
.GetNumberCols();
1697 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1698 int cell_rows
, cell_cols
;
1699 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1700 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1702 int i
, c_cols
, c_rows
;
1703 for (i
= col
+cell_cols
; i
< cols
; i
++)
1705 bool is_empty
= true;
1706 for (int j
=row
; j
<row
+cell_rows
; j
++)
1708 // check w/ anchor cell for multicell block
1709 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1710 if (c_rows
> 0) c_rows
= 0;
1711 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1718 rect
.width
+= grid
.GetColSize(i
);
1724 if (rect
.width
>= best_width
) break;
1726 overflowCols
= i
- col
- cell_cols
+ 1;
1727 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1730 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1732 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1734 clip
.x
+= rectCell
.width
;
1735 // draw each overflow cell individually
1736 int col_end
= col
+cell_cols
+overflowCols
;
1737 if (col_end
>= grid
.GetNumberCols())
1738 col_end
= grid
.GetNumberCols() - 1;
1739 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1741 clip
.width
= grid
.GetColSize(i
) - 1;
1742 dc
.DestroyClippingRegion();
1743 dc
.SetClippingRegion(clip
);
1745 SetTextColoursAndFont(grid
, attr
, dc
,
1746 grid
.IsInSelection(row
,i
));
1748 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1749 rect
, hAlign
, vAlign
);
1750 clip
.x
+= grid
.GetColSize(i
) - 1;
1756 dc
.DestroyClippingRegion();
1760 // now we only have to draw the text
1761 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1763 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1764 rect
, hAlign
, vAlign
);
1767 // ----------------------------------------------------------------------------
1768 // wxGridCellNumberRenderer
1769 // ----------------------------------------------------------------------------
1771 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1773 wxGridTableBase
*table
= grid
.GetTable();
1775 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1777 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1781 text
= table
->GetValue(row
, col
);
1787 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1788 wxGridCellAttr
& attr
,
1790 const wxRect
& rectCell
,
1794 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1796 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1798 // draw the text right aligned by default
1800 attr
.GetAlignment(&hAlign
, &vAlign
);
1801 hAlign
= wxALIGN_RIGHT
;
1803 wxRect rect
= rectCell
;
1806 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1809 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1810 wxGridCellAttr
& attr
,
1814 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1817 // ----------------------------------------------------------------------------
1818 // wxGridCellFloatRenderer
1819 // ----------------------------------------------------------------------------
1821 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1824 SetPrecision(precision
);
1827 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1829 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1830 renderer
->m_width
= m_width
;
1831 renderer
->m_precision
= m_precision
;
1832 renderer
->m_format
= m_format
;
1837 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1839 wxGridTableBase
*table
= grid
.GetTable();
1844 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1846 val
= table
->GetValueAsDouble(row
, col
);
1851 text
= table
->GetValue(row
, col
);
1852 hasDouble
= text
.ToDouble(&val
);
1859 if ( m_width
== -1 )
1861 if ( m_precision
== -1 )
1863 // default width/precision
1864 m_format
= _T("%f");
1868 m_format
.Printf(_T("%%.%df"), m_precision
);
1871 else if ( m_precision
== -1 )
1873 // default precision
1874 m_format
.Printf(_T("%%%d.f"), m_width
);
1878 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1882 text
.Printf(m_format
, val
);
1885 //else: text already contains the string
1890 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1891 wxGridCellAttr
& attr
,
1893 const wxRect
& rectCell
,
1897 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1899 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1901 // draw the text right aligned by default
1903 attr
.GetAlignment(&hAlign
, &vAlign
);
1904 hAlign
= wxALIGN_RIGHT
;
1906 wxRect rect
= rectCell
;
1909 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1912 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1913 wxGridCellAttr
& attr
,
1917 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1920 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1924 // reset to defaults
1930 wxString tmp
= params
.BeforeFirst(_T(','));
1934 if ( tmp
.ToLong(&width
) )
1936 SetWidth((int)width
);
1940 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1944 tmp
= params
.AfterFirst(_T(','));
1948 if ( tmp
.ToLong(&precision
) )
1950 SetPrecision((int)precision
);
1954 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1962 // ----------------------------------------------------------------------------
1963 // wxGridCellBoolRenderer
1964 // ----------------------------------------------------------------------------
1966 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1968 // FIXME these checkbox size calculations are really ugly...
1970 // between checkmark and box
1971 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1973 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1974 wxGridCellAttr
& WXUNUSED(attr
),
1979 // compute it only once (no locks for MT safeness in GUI thread...)
1980 if ( !ms_sizeCheckMark
.x
)
1982 // get checkbox size
1983 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1984 wxSize size
= checkbox
->GetBestSize();
1985 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1987 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1988 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1989 checkSize
-= size
.y
/ 2;
1994 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
1997 return ms_sizeCheckMark
;
2000 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2001 wxGridCellAttr
& attr
,
2007 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2009 // draw a check mark in the centre (ignoring alignment - TODO)
2010 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2012 // don't draw outside the cell
2013 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2014 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2016 // and even leave (at least) 1 pixel margin
2017 size
.x
= size
.y
= minSize
- 2;
2020 // draw a border around checkmark
2022 attr
.GetAlignment(& hAlign
, &vAlign
);
2025 if (hAlign
== wxALIGN_CENTRE
)
2027 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2028 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2029 rectBorder
.width
= size
.x
;
2030 rectBorder
.height
= size
.y
;
2032 else if (hAlign
== wxALIGN_LEFT
)
2034 rectBorder
.x
= rect
.x
+ 2;
2035 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2036 rectBorder
.width
= size
.x
;
2037 rectBorder
.height
= size
.y
;
2039 else if (hAlign
== wxALIGN_RIGHT
)
2041 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2042 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2043 rectBorder
.width
= size
.x
;
2044 rectBorder
.height
= size
.y
;
2048 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2049 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2052 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2053 value
= !( !cellval
|| (cellval
== wxT("0")) );
2058 wxRect rectMark
= rectBorder
;
2060 // MSW DrawCheckMark() is weird (and should probably be changed...)
2061 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2065 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2068 dc
.SetTextForeground(attr
.GetTextColour());
2069 dc
.DrawCheckMark(rectMark
);
2072 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2073 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2074 dc
.DrawRectangle(rectBorder
);
2077 // ----------------------------------------------------------------------------
2079 // ----------------------------------------------------------------------------
2081 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2085 m_isReadOnly
= Unset
;
2090 m_attrkind
= wxGridCellAttr::Cell
;
2092 m_sizeRows
= m_sizeCols
= 1;
2093 m_overflow
= UnsetOverflow
;
2095 SetDefAttr(attrDefault
);
2098 wxGridCellAttr
*wxGridCellAttr::Clone() const
2100 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2102 if ( HasTextColour() )
2103 attr
->SetTextColour(GetTextColour());
2104 if ( HasBackgroundColour() )
2105 attr
->SetBackgroundColour(GetBackgroundColour());
2107 attr
->SetFont(GetFont());
2108 if ( HasAlignment() )
2109 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2111 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2115 attr
->SetRenderer(m_renderer
);
2116 m_renderer
->IncRef();
2120 attr
->SetEditor(m_editor
);
2125 attr
->SetReadOnly();
2127 attr
->SetKind( m_attrkind
);
2132 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2134 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2135 SetTextColour(mergefrom
->GetTextColour());
2136 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2137 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2138 if ( !HasFont() && mergefrom
->HasFont() )
2139 SetFont(mergefrom
->GetFont());
2140 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2142 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2143 SetAlignment(hAlign
, vAlign
);
2145 if ( !HasSize() && mergefrom
->HasSize() )
2146 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2148 // Directly access member functions as GetRender/Editor don't just return
2149 // m_renderer/m_editor
2151 // Maybe add support for merge of Render and Editor?
2152 if (!HasRenderer() && mergefrom
->HasRenderer() )
2154 m_renderer
= mergefrom
->m_renderer
;
2155 m_renderer
->IncRef();
2157 if ( !HasEditor() && mergefrom
->HasEditor() )
2159 m_editor
= mergefrom
->m_editor
;
2162 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2163 SetReadOnly(mergefrom
->IsReadOnly());
2165 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2166 SetOverflow(mergefrom
->GetOverflow());
2168 SetDefAttr(mergefrom
->m_defGridAttr
);
2171 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2173 // The size of a cell is normally 1,1
2175 // If this cell is larger (2,2) then this is the top left cell
2176 // the other cells that will be covered (lower right cells) must be
2177 // set to negative or zero values such that
2178 // row + num_rows of the covered cell points to the larger cell (this cell)
2179 // same goes for the col + num_cols.
2181 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2183 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2184 !((num_rows
<=0)&&(num_cols
>0)) ||
2185 !((num_rows
==0)&&(num_cols
==0))),
2186 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2188 m_sizeRows
= num_rows
;
2189 m_sizeCols
= num_cols
;
2192 const wxColour
& wxGridCellAttr::GetTextColour() const
2194 if (HasTextColour())
2198 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2200 return m_defGridAttr
->GetTextColour();
2204 wxFAIL_MSG(wxT("Missing default cell attribute"));
2205 return wxNullColour
;
2210 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2212 if (HasBackgroundColour())
2214 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2215 return m_defGridAttr
->GetBackgroundColour();
2218 wxFAIL_MSG(wxT("Missing default cell attribute"));
2219 return wxNullColour
;
2224 const wxFont
& wxGridCellAttr::GetFont() const
2228 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2229 return m_defGridAttr
->GetFont();
2232 wxFAIL_MSG(wxT("Missing default cell attribute"));
2238 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2242 if ( hAlign
) *hAlign
= m_hAlign
;
2243 if ( vAlign
) *vAlign
= m_vAlign
;
2245 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2246 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2249 wxFAIL_MSG(wxT("Missing default cell attribute"));
2253 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2255 if ( num_rows
) *num_rows
= m_sizeRows
;
2256 if ( num_cols
) *num_cols
= m_sizeCols
;
2259 // GetRenderer and GetEditor use a slightly different decision path about
2260 // which attribute to use. If a non-default attr object has one then it is
2261 // used, otherwise the default editor or renderer is fetched from the grid and
2262 // used. It should be the default for the data type of the cell. If it is
2263 // NULL (because the table has a type that the grid does not have in its
2264 // registry,) then the grid's default editor or renderer is used.
2266 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2268 wxGridCellRenderer
*renderer
;
2270 if ( m_renderer
&& this != m_defGridAttr
)
2272 // use the cells renderer if it has one
2273 renderer
= m_renderer
;
2276 else // no non default cell renderer
2278 // get default renderer for the data type
2281 // GetDefaultRendererForCell() will do IncRef() for us
2282 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2291 if (m_defGridAttr
&& this != m_defGridAttr
)
2293 // if we still don't have one then use the grid default
2294 // (no need for IncRef() here neither)
2295 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2297 else // default grid attr
2299 // use m_renderer which we had decided not to use initially
2300 renderer
= m_renderer
;
2307 // we're supposed to always find something
2308 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2313 // same as above, except for s/renderer/editor/g
2314 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2316 wxGridCellEditor
*editor
;
2318 if ( m_editor
&& this != m_defGridAttr
)
2320 // use the cells editor if it has one
2324 else // no non default cell editor
2326 // get default editor for the data type
2329 // GetDefaultEditorForCell() will do IncRef() for us
2330 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2339 if ( m_defGridAttr
&& this != m_defGridAttr
)
2341 // if we still don't have one then use the grid default
2342 // (no need for IncRef() here neither)
2343 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2345 else // default grid attr
2347 // use m_editor which we had decided not to use initially
2355 // we're supposed to always find something
2356 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2361 // ----------------------------------------------------------------------------
2362 // wxGridCellAttrData
2363 // ----------------------------------------------------------------------------
2365 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2367 int n
= FindIndex(row
, col
);
2368 if ( n
== wxNOT_FOUND
)
2370 // add the attribute
2371 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2375 // free the old attribute
2376 m_attrs
[(size_t)n
].attr
->DecRef();
2380 // change the attribute
2381 m_attrs
[(size_t)n
].attr
= attr
;
2385 // remove this attribute
2386 m_attrs
.RemoveAt((size_t)n
);
2391 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2393 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2395 int n
= FindIndex(row
, col
);
2396 if ( n
!= wxNOT_FOUND
)
2398 attr
= m_attrs
[(size_t)n
].attr
;
2405 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2407 size_t count
= m_attrs
.GetCount();
2408 for ( size_t n
= 0; n
< count
; n
++ )
2410 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2411 wxCoord row
= coords
.GetRow();
2412 if ((size_t)row
>= pos
)
2416 // If rows inserted, include row counter where necessary
2417 coords
.SetRow(row
+ numRows
);
2419 else if (numRows
< 0)
2421 // If rows deleted ...
2422 if ((size_t)row
>= pos
- numRows
)
2424 // ...either decrement row counter (if row still exists)...
2425 coords
.SetRow(row
+ numRows
);
2429 // ...or remove the attribute
2430 // No need to DecRef the attribute itself since this is
2431 // done be wxGridCellWithAttr's destructor!
2432 m_attrs
.RemoveAt(n
);
2440 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2442 size_t count
= m_attrs
.GetCount();
2443 for ( size_t n
= 0; n
< count
; n
++ )
2445 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2446 wxCoord col
= coords
.GetCol();
2447 if ( (size_t)col
>= pos
)
2451 // If rows inserted, include row counter where necessary
2452 coords
.SetCol(col
+ numCols
);
2454 else if (numCols
< 0)
2456 // If rows deleted ...
2457 if ((size_t)col
>= pos
- numCols
)
2459 // ...either decrement row counter (if row still exists)...
2460 coords
.SetCol(col
+ numCols
);
2464 // ...or remove the attribute
2465 // No need to DecRef the attribute itself since this is
2466 // done be wxGridCellWithAttr's destructor!
2467 m_attrs
.RemoveAt(n
);
2475 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2477 size_t count
= m_attrs
.GetCount();
2478 for ( size_t n
= 0; n
< count
; n
++ )
2480 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2481 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2490 // ----------------------------------------------------------------------------
2491 // wxGridRowOrColAttrData
2492 // ----------------------------------------------------------------------------
2494 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2496 size_t count
= m_attrs
.Count();
2497 for ( size_t n
= 0; n
< count
; n
++ )
2499 m_attrs
[n
]->DecRef();
2503 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2505 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2507 int n
= m_rowsOrCols
.Index(rowOrCol
);
2508 if ( n
!= wxNOT_FOUND
)
2510 attr
= m_attrs
[(size_t)n
];
2517 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2519 int i
= m_rowsOrCols
.Index(rowOrCol
);
2520 if ( i
== wxNOT_FOUND
)
2522 // add the attribute
2523 m_rowsOrCols
.Add(rowOrCol
);
2528 size_t n
= (size_t)i
;
2531 // change the attribute
2532 m_attrs
[n
]->DecRef();
2537 // remove this attribute
2538 m_attrs
[n
]->DecRef();
2539 m_rowsOrCols
.RemoveAt(n
);
2540 m_attrs
.RemoveAt(n
);
2545 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2547 size_t count
= m_attrs
.GetCount();
2548 for ( size_t n
= 0; n
< count
; n
++ )
2550 int & rowOrCol
= m_rowsOrCols
[n
];
2551 if ( (size_t)rowOrCol
>= pos
)
2553 if ( numRowsOrCols
> 0 )
2555 // If rows inserted, include row counter where necessary
2556 rowOrCol
+= numRowsOrCols
;
2558 else if ( numRowsOrCols
< 0)
2560 // If rows deleted, either decrement row counter (if row still exists)
2561 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2562 rowOrCol
+= numRowsOrCols
;
2565 m_rowsOrCols
.RemoveAt(n
);
2566 m_attrs
[n
]->DecRef();
2567 m_attrs
.RemoveAt(n
);
2575 // ----------------------------------------------------------------------------
2576 // wxGridCellAttrProvider
2577 // ----------------------------------------------------------------------------
2579 wxGridCellAttrProvider::wxGridCellAttrProvider()
2581 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2584 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2589 void wxGridCellAttrProvider::InitData()
2591 m_data
= new wxGridCellAttrProviderData
;
2594 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2595 wxGridCellAttr::wxAttrKind kind
) const
2597 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2602 case (wxGridCellAttr::Any
):
2603 //Get cached merge attributes.
2604 // Currenlty not used as no cache implemented as not mutiable
2605 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2608 //Basicaly implement old version.
2609 //Also check merge cache, so we don't have to re-merge every time..
2610 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2611 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2612 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2614 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2615 // Two or more are non NULL
2616 attr
= new wxGridCellAttr
;
2617 attr
->SetKind(wxGridCellAttr::Merged
);
2621 attr
->MergeWith(attrcell
);
2625 attr
->MergeWith(attrcol
);
2629 attr
->MergeWith(attrrow
);
2632 //store merge attr if cache implemented
2634 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2638 // one or none is non null return it or null.
2639 if(attrrow
) attr
= attrrow
;
2655 case (wxGridCellAttr::Cell
):
2656 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2658 case (wxGridCellAttr::Col
):
2659 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2661 case (wxGridCellAttr::Row
):
2662 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2666 // (wxGridCellAttr::Default):
2667 // (wxGridCellAttr::Merged):
2674 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2680 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2683 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2688 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2691 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2696 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2699 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2703 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2705 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2709 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2713 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2715 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2719 // ----------------------------------------------------------------------------
2720 // wxGridTypeRegistry
2721 // ----------------------------------------------------------------------------
2723 wxGridTypeRegistry::~wxGridTypeRegistry()
2725 size_t count
= m_typeinfo
.Count();
2726 for ( size_t i
= 0; i
< count
; i
++ )
2727 delete m_typeinfo
[i
];
2731 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2732 wxGridCellRenderer
* renderer
,
2733 wxGridCellEditor
* editor
)
2735 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2737 // is it already registered?
2738 int loc
= FindRegisteredDataType(typeName
);
2739 if ( loc
!= wxNOT_FOUND
)
2741 delete m_typeinfo
[loc
];
2742 m_typeinfo
[loc
] = info
;
2746 m_typeinfo
.Add(info
);
2750 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2752 size_t count
= m_typeinfo
.GetCount();
2753 for ( size_t i
= 0; i
< count
; i
++ )
2755 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2764 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2766 int index
= FindRegisteredDataType(typeName
);
2767 if ( index
== wxNOT_FOUND
)
2769 // check whether this is one of the standard ones, in which case
2770 // register it "on the fly"
2772 if ( typeName
== wxGRID_VALUE_STRING
)
2774 RegisterDataType(wxGRID_VALUE_STRING
,
2775 new wxGridCellStringRenderer
,
2776 new wxGridCellTextEditor
);
2778 #endif // wxUSE_TEXTCTRL
2780 if ( typeName
== wxGRID_VALUE_BOOL
)
2782 RegisterDataType(wxGRID_VALUE_BOOL
,
2783 new wxGridCellBoolRenderer
,
2784 new wxGridCellBoolEditor
);
2786 #endif // wxUSE_CHECKBOX
2788 if ( typeName
== wxGRID_VALUE_NUMBER
)
2790 RegisterDataType(wxGRID_VALUE_NUMBER
,
2791 new wxGridCellNumberRenderer
,
2792 new wxGridCellNumberEditor
);
2794 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2796 RegisterDataType(wxGRID_VALUE_FLOAT
,
2797 new wxGridCellFloatRenderer
,
2798 new wxGridCellFloatEditor
);
2800 #endif // wxUSE_TEXTCTRL
2802 if ( typeName
== wxGRID_VALUE_CHOICE
)
2804 RegisterDataType(wxGRID_VALUE_CHOICE
,
2805 new wxGridCellStringRenderer
,
2806 new wxGridCellChoiceEditor
);
2808 #endif // wxUSE_COMBOBOX
2813 // we get here only if just added the entry for this type, so return
2815 index
= m_typeinfo
.GetCount() - 1;
2821 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2823 int index
= FindDataType(typeName
);
2824 if ( index
== wxNOT_FOUND
)
2826 // the first part of the typename is the "real" type, anything after ':'
2827 // are the parameters for the renderer
2828 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2829 if ( index
== wxNOT_FOUND
)
2834 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2835 wxGridCellRenderer
*rendererOld
= renderer
;
2836 renderer
= renderer
->Clone();
2837 rendererOld
->DecRef();
2839 wxGridCellEditor
*editor
= GetEditor(index
);
2840 wxGridCellEditor
*editorOld
= editor
;
2841 editor
= editor
->Clone();
2842 editorOld
->DecRef();
2844 // do it even if there are no parameters to reset them to defaults
2845 wxString params
= typeName
.AfterFirst(_T(':'));
2846 renderer
->SetParameters(params
);
2847 editor
->SetParameters(params
);
2849 // register the new typename
2850 RegisterDataType(typeName
, renderer
, editor
);
2852 // we just registered it, it's the last one
2853 index
= m_typeinfo
.GetCount() - 1;
2859 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2861 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2867 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2869 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2875 // ----------------------------------------------------------------------------
2877 // ----------------------------------------------------------------------------
2879 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2882 wxGridTableBase::wxGridTableBase()
2884 m_view
= (wxGrid
*) NULL
;
2885 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2888 wxGridTableBase::~wxGridTableBase()
2890 delete m_attrProvider
;
2893 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2895 delete m_attrProvider
;
2896 m_attrProvider
= attrProvider
;
2899 bool wxGridTableBase::CanHaveAttributes()
2901 if ( ! GetAttrProvider() )
2903 // use the default attr provider by default
2904 SetAttrProvider(new wxGridCellAttrProvider
);
2909 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2911 if ( m_attrProvider
)
2912 return m_attrProvider
->GetAttr(row
, col
, kind
);
2914 return (wxGridCellAttr
*)NULL
;
2918 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2920 if ( m_attrProvider
)
2922 attr
->SetKind(wxGridCellAttr::Cell
);
2923 m_attrProvider
->SetAttr(attr
, row
, col
);
2927 // as we take ownership of the pointer and don't store it, we must
2933 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2935 if ( m_attrProvider
)
2937 attr
->SetKind(wxGridCellAttr::Row
);
2938 m_attrProvider
->SetRowAttr(attr
, row
);
2942 // as we take ownership of the pointer and don't store it, we must
2948 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2950 if ( m_attrProvider
)
2952 attr
->SetKind(wxGridCellAttr::Col
);
2953 m_attrProvider
->SetColAttr(attr
, col
);
2957 // as we take ownership of the pointer and don't store it, we must
2963 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2964 size_t WXUNUSED(numRows
) )
2966 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2971 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2973 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2978 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2979 size_t WXUNUSED(numRows
) )
2981 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2986 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2987 size_t WXUNUSED(numCols
) )
2989 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2994 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2996 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3001 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3002 size_t WXUNUSED(numCols
) )
3004 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3010 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3013 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3014 // how much it makes sense to us geeks.
3018 wxString
wxGridTableBase::GetColLabelValue( int col
)
3020 // default col labels are:
3021 // cols 0 to 25 : A-Z
3022 // cols 26 to 675 : AA-ZZ
3027 for ( n
= 1; ; n
++ )
3029 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3031 if ( col
< 0 ) break;
3034 // reverse the string...
3036 for ( i
= 0; i
< n
; i
++ )
3045 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3047 return wxGRID_VALUE_STRING
;
3050 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3051 const wxString
& typeName
)
3053 return typeName
== wxGRID_VALUE_STRING
;
3056 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3058 return CanGetValueAs(row
, col
, typeName
);
3061 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3066 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3071 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3076 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3077 long WXUNUSED(value
) )
3081 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3082 double WXUNUSED(value
) )
3086 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3087 bool WXUNUSED(value
) )
3092 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3093 const wxString
& WXUNUSED(typeName
) )
3098 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3099 const wxString
& WXUNUSED(typeName
),
3100 void* WXUNUSED(value
) )
3104 //////////////////////////////////////////////////////////////////////
3106 // Message class for the grid table to send requests and notifications
3110 wxGridTableMessage::wxGridTableMessage()
3112 m_table
= (wxGridTableBase
*) NULL
;
3118 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3119 int commandInt1
, int commandInt2
)
3123 m_comInt1
= commandInt1
;
3124 m_comInt2
= commandInt2
;
3129 //////////////////////////////////////////////////////////////////////
3131 // A basic grid table for string data. An object of this class will
3132 // created by wxGrid if you don't specify an alternative table class.
3135 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3137 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3139 wxGridStringTable::wxGridStringTable()
3144 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3147 m_data
.Alloc( numRows
);
3150 sa
.Alloc( numCols
);
3151 sa
.Add( wxEmptyString
, numCols
);
3153 m_data
.Add( sa
, numRows
);
3156 wxGridStringTable::~wxGridStringTable()
3160 int wxGridStringTable::GetNumberRows()
3162 return m_data
.GetCount();
3165 int wxGridStringTable::GetNumberCols()
3167 if ( m_data
.GetCount() > 0 )
3168 return m_data
[0].GetCount();
3173 wxString
wxGridStringTable::GetValue( int row
, int col
)
3175 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3177 _T("invalid row or column index in wxGridStringTable") );
3179 return m_data
[row
][col
];
3182 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3184 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3185 _T("invalid row or column index in wxGridStringTable") );
3187 m_data
[row
][col
] = value
;
3190 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3192 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3194 _T("invalid row or column index in wxGridStringTable") );
3196 return (m_data
[row
][col
] == wxEmptyString
);
3199 void wxGridStringTable::Clear()
3202 int numRows
, numCols
;
3204 numRows
= m_data
.GetCount();
3207 numCols
= m_data
[0].GetCount();
3209 for ( row
= 0; row
< numRows
; row
++ )
3211 for ( col
= 0; col
< numCols
; col
++ )
3213 m_data
[row
][col
] = wxEmptyString
;
3220 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3222 size_t curNumRows
= m_data
.GetCount();
3223 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3224 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3226 if ( pos
>= curNumRows
)
3228 return AppendRows( numRows
);
3232 sa
.Alloc( curNumCols
);
3233 sa
.Add( wxEmptyString
, curNumCols
);
3234 m_data
.Insert( sa
, pos
, numRows
);
3237 wxGridTableMessage
msg( this,
3238 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3242 GetView()->ProcessTableMessage( msg
);
3248 bool wxGridStringTable::AppendRows( size_t numRows
)
3250 size_t curNumRows
= m_data
.GetCount();
3251 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3252 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3255 if ( curNumCols
> 0 )
3257 sa
.Alloc( curNumCols
);
3258 sa
.Add( wxEmptyString
, curNumCols
);
3261 m_data
.Add( sa
, numRows
);
3265 wxGridTableMessage
msg( this,
3266 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3269 GetView()->ProcessTableMessage( msg
);
3275 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3277 size_t curNumRows
= m_data
.GetCount();
3279 if ( pos
>= curNumRows
)
3281 wxFAIL_MSG( wxString::Format
3283 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3285 (unsigned long)numRows
,
3286 (unsigned long)curNumRows
3292 if ( numRows
> curNumRows
- pos
)
3294 numRows
= curNumRows
- pos
;
3297 if ( numRows
>= curNumRows
)
3303 m_data
.RemoveAt( pos
, numRows
);
3307 wxGridTableMessage
msg( this,
3308 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3312 GetView()->ProcessTableMessage( msg
);
3318 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3322 size_t curNumRows
= m_data
.GetCount();
3323 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3324 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3326 if ( pos
>= curNumCols
)
3328 return AppendCols( numCols
);
3331 for ( row
= 0; row
< curNumRows
; row
++ )
3333 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3335 m_data
[row
].Insert( wxEmptyString
, col
);
3340 wxGridTableMessage
msg( this,
3341 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3345 GetView()->ProcessTableMessage( msg
);
3351 bool wxGridStringTable::AppendCols( size_t numCols
)
3355 size_t curNumRows
= m_data
.GetCount();
3359 // TODO: something better than this ?
3361 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3366 for ( row
= 0; row
< curNumRows
; row
++ )
3368 m_data
[row
].Add( wxEmptyString
, numCols
);
3373 wxGridTableMessage
msg( this,
3374 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3377 GetView()->ProcessTableMessage( msg
);
3383 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3387 size_t curNumRows
= m_data
.GetCount();
3388 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3389 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3391 if ( pos
>= curNumCols
)
3393 wxFAIL_MSG( wxString::Format
3395 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3397 (unsigned long)numCols
,
3398 (unsigned long)curNumCols
3403 if ( numCols
> curNumCols
- pos
)
3405 numCols
= curNumCols
- pos
;
3408 for ( row
= 0; row
< curNumRows
; row
++ )
3410 if ( numCols
>= curNumCols
)
3412 m_data
[row
].Clear();
3416 m_data
[row
].RemoveAt( pos
, numCols
);
3421 wxGridTableMessage
msg( this,
3422 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3426 GetView()->ProcessTableMessage( msg
);
3432 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3434 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3436 // using default label
3438 return wxGridTableBase::GetRowLabelValue( row
);
3442 return m_rowLabels
[ row
];
3446 wxString
wxGridStringTable::GetColLabelValue( int col
)
3448 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3450 // using default label
3452 return wxGridTableBase::GetColLabelValue( col
);
3456 return m_colLabels
[ col
];
3460 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3462 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3464 int n
= m_rowLabels
.GetCount();
3466 for ( i
= n
; i
<= row
; i
++ )
3468 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3472 m_rowLabels
[row
] = value
;
3475 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3477 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3479 int n
= m_colLabels
.GetCount();
3481 for ( i
= n
; i
<= col
; i
++ )
3483 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3487 m_colLabels
[col
] = value
;
3492 //////////////////////////////////////////////////////////////////////
3493 //////////////////////////////////////////////////////////////////////
3495 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3497 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3498 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3499 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3500 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3501 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3502 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
3503 EVT_CHAR ( wxGridRowLabelWindow::OnChar
)
3506 wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid
*parent
,
3508 const wxPoint
&pos
, const wxSize
&size
)
3509 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3514 void wxGridRowLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3518 // NO - don't do this because it will set both the x and y origin
3519 // coords to match the parent scrolled window and we just want to
3520 // set the y coord - MB
3522 // m_owner->PrepareDC( dc );
3525 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3526 dc
.SetDeviceOrigin( 0, -y
);
3528 wxArrayInt rows
= m_owner
->CalcRowLabelsExposed( GetUpdateRegion() );
3529 m_owner
->DrawRowLabels( dc
, rows
);
3533 void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3535 m_owner
->ProcessRowLabelMouseEvent( event
);
3539 void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3541 m_owner
->GetEventHandler()->ProcessEvent(event
);
3545 // This seems to be required for wxMotif otherwise the mouse
3546 // cursor must be in the cell edit control to get key events
3548 void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3550 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3553 void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3555 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3558 void wxGridRowLabelWindow::OnChar( wxKeyEvent
& event
)
3560 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3565 //////////////////////////////////////////////////////////////////////
3567 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3569 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3570 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3571 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3572 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3573 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3574 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3575 EVT_CHAR ( wxGridColLabelWindow::OnChar
)
3578 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3580 const wxPoint
&pos
, const wxSize
&size
)
3581 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3586 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3590 // NO - don't do this because it will set both the x and y origin
3591 // coords to match the parent scrolled window and we just want to
3592 // set the x coord - MB
3594 // m_owner->PrepareDC( dc );
3597 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3598 dc
.SetDeviceOrigin( -x
, 0 );
3600 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3601 m_owner
->DrawColLabels( dc
, cols
);
3605 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3607 m_owner
->ProcessColLabelMouseEvent( event
);
3610 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3612 m_owner
->GetEventHandler()->ProcessEvent(event
);
3616 // This seems to be required for wxMotif otherwise the mouse
3617 // cursor must be in the cell edit control to get key events
3619 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3621 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3624 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3626 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3629 void wxGridColLabelWindow::OnChar( wxKeyEvent
& event
)
3631 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3635 //////////////////////////////////////////////////////////////////////
3637 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3639 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3640 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3641 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3642 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3643 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3644 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3645 EVT_CHAR ( wxGridCornerLabelWindow::OnChar
)
3648 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3650 const wxPoint
&pos
, const wxSize
&size
)
3651 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3656 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3660 int client_height
= 0;
3661 int client_width
= 0;
3662 GetClientSize( &client_width
, &client_height
);
3668 rect
.SetWidth( client_width
- 2 );
3669 rect
.SetHeight( client_height
- 2 );
3671 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3673 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3674 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3675 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3676 dc
.DrawLine( 0, 0, client_width
, 0 );
3677 dc
.DrawLine( 0, 0, 0, client_height
);
3679 dc
.SetPen( *wxWHITE_PEN
);
3680 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3681 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3686 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3688 m_owner
->ProcessCornerLabelMouseEvent( event
);
3692 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3694 m_owner
->GetEventHandler()->ProcessEvent(event
);
3697 // This seems to be required for wxMotif otherwise the mouse
3698 // cursor must be in the cell edit control to get key events
3700 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3702 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3705 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3707 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3710 void wxGridCornerLabelWindow::OnChar( wxKeyEvent
& event
)
3712 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3716 //////////////////////////////////////////////////////////////////////
3718 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3720 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3721 EVT_PAINT( wxGridWindow::OnPaint
)
3722 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3723 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3724 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3725 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3726 EVT_CHAR ( wxGridWindow::OnChar
)
3727 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3728 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3729 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3732 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3733 wxGridRowLabelWindow
*rowLblWin
,
3734 wxGridColLabelWindow
*colLblWin
,
3737 const wxSize
&size
)
3738 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3739 wxT("grid window") )
3743 m_rowLabelWin
= rowLblWin
;
3744 m_colLabelWin
= colLblWin
;
3748 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3750 wxPaintDC
dc( this );
3751 m_owner
->PrepareDC( dc
);
3752 wxRegion reg
= GetUpdateRegion();
3753 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3754 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3755 #if WXGRID_DRAW_LINES
3756 m_owner
->DrawAllGridLines( dc
, reg
);
3758 m_owner
->DrawGridSpace( dc
);
3759 m_owner
->DrawHighlight( dc
, DirtyCells
);
3763 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3765 wxWindow::ScrollWindow( dx
, dy
, rect
);
3766 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3767 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3771 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3773 if (event
.ButtonDown(wxMOUSE_BTN_LEFT
) && FindFocus() != this)
3776 m_owner
->ProcessGridCellMouseEvent( event
);
3779 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3781 m_owner
->GetEventHandler()->ProcessEvent(event
);
3784 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3785 // cursor must be in the cell edit control to get key events
3787 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3789 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3792 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3794 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3797 void wxGridWindow::OnChar( wxKeyEvent
& event
)
3799 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3802 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3806 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3808 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3812 //////////////////////////////////////////////////////////////////////
3814 // Internal Helper function for computing row or column from some
3815 // (unscrolled) coordinate value, using either
3816 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3817 // of m_rowBottoms/m_ColRights to speed up the search!
3819 // Internal helper macros for simpler use of that function
3821 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3822 const wxArrayInt
& BorderArray
, int nMax
,
3825 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3826 m_minAcceptableColWidth, \
3827 m_colRights, m_numCols, true)
3828 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3829 m_minAcceptableRowHeight, \
3830 m_rowBottoms, m_numRows, true)
3831 /////////////////////////////////////////////////////////////////////
3833 #if wxUSE_EXTENDED_RTTI
3834 WX_DEFINE_FLAGS( wxGridStyle
)
3836 wxBEGIN_FLAGS( wxGridStyle
)
3837 // new style border flags, we put them first to
3838 // use them for streaming out
3839 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3840 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3841 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3842 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3843 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3844 wxFLAGS_MEMBER(wxBORDER_NONE
)
3846 // old style border flags
3847 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3848 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3849 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3850 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3851 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3852 wxFLAGS_MEMBER(wxBORDER
)
3854 // standard window styles
3855 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3856 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3857 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3858 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3859 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3860 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3861 wxFLAGS_MEMBER(wxVSCROLL
)
3862 wxFLAGS_MEMBER(wxHSCROLL
)
3864 wxEND_FLAGS( wxGridStyle
)
3866 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3868 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3869 wxHIDE_PROPERTY( Children
)
3870 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3871 wxEND_PROPERTIES_TABLE()
3873 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3874 wxEND_HANDLERS_TABLE()
3876 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3879 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3882 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3885 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3886 EVT_PAINT( wxGrid::OnPaint
)
3887 EVT_SIZE( wxGrid::OnSize
)
3888 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3889 EVT_KEY_UP( wxGrid::OnKeyUp
)
3890 EVT_CHAR ( wxGrid::OnChar
)
3891 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3896 // in order to make sure that a size event is not
3897 // trigerred in a unfinished state
3898 m_cornerLabelWin
= NULL
;
3899 m_rowLabelWin
= NULL
;
3900 m_colLabelWin
= NULL
;
3904 wxGrid::wxGrid( wxWindow
*parent
,
3909 const wxString
& name
)
3910 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3911 m_colMinWidths(GRID_HASH_SIZE
),
3912 m_rowMinHeights(GRID_HASH_SIZE
)
3915 SetBestFittingSize(size
);
3918 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3919 const wxPoint
& pos
, const wxSize
& size
,
3920 long style
, const wxString
& name
)
3922 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3923 style
| wxWANTS_CHARS
, name
))
3926 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3927 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3930 SetBestFittingSize(size
);
3938 // Must do this or ~wxScrollHelper will pop the wrong event handler
3939 SetTargetWindow(this);
3941 wxSafeDecRef(m_defaultCellAttr
);
3943 #ifdef DEBUG_ATTR_CACHE
3944 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3945 wxPrintf(_T("wxGrid attribute cache statistics: "
3946 "total: %u, hits: %u (%u%%)\n"),
3947 total
, gs_nAttrCacheHits
,
3948 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3954 delete m_typeRegistry
;
3960 // ----- internal init and update functions
3963 // NOTE: If using the default visual attributes works everywhere then this can
3964 // be removed as well as the #else cases below.
3965 #define _USE_VISATTR 0
3968 #include "wx/listbox.h"
3971 void wxGrid::Create()
3973 m_created
= false; // set to true by CreateGrid
3975 m_table
= (wxGridTableBase
*) NULL
;
3978 m_cellEditCtrlEnabled
= false;
3980 m_defaultCellAttr
= new wxGridCellAttr();
3982 // Set default cell attributes
3983 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3984 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3985 m_defaultCellAttr
->SetFont(GetFont());
3986 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3987 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3988 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3991 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3992 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3994 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3995 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3998 m_defaultCellAttr
->SetTextColour(
3999 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
4000 m_defaultCellAttr
->SetBackgroundColour(
4001 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
4006 m_currentCellCoords
= wxGridNoCellCoords
;
4008 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4009 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4011 // create the type registry
4012 m_typeRegistry
= new wxGridTypeRegistry
;
4015 // subwindow components that make up the wxGrid
4016 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
4021 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4026 m_colLabelWin
= new wxGridColLabelWindow( this,
4031 m_gridWin
= new wxGridWindow( this,
4038 SetTargetWindow( m_gridWin
);
4041 wxColour gfg
= gva
.colFg
;
4042 wxColour gbg
= gva
.colBg
;
4043 wxColour lfg
= lva
.colFg
;
4044 wxColour lbg
= lva
.colBg
;
4046 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4047 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4048 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4049 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4051 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4052 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4053 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4054 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4055 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4056 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4058 m_gridWin
->SetOwnForegroundColour(gfg
);
4059 m_gridWin
->SetOwnBackgroundColour(gbg
);
4065 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4066 wxGrid::wxGridSelectionModes selmode
)
4068 wxCHECK_MSG( !m_created
,
4070 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4072 m_numRows
= numRows
;
4073 m_numCols
= numCols
;
4075 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4076 m_table
->SetView( this );
4078 m_selection
= new wxGridSelection( this, selmode
);
4087 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4089 wxCHECK_RET( m_created
,
4090 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4092 m_selection
->SetSelectionMode( selmode
);
4095 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4097 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4098 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4100 return m_selection
->GetSelectionMode();
4103 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4104 wxGrid::wxGridSelectionModes selmode
)
4108 // stop all processing
4113 wxGridTableBase
*t
=m_table
;
4126 m_numRows
= table
->GetNumberRows();
4127 m_numCols
= table
->GetNumberCols();
4130 m_table
->SetView( this );
4133 m_selection
= new wxGridSelection( this, selmode
);
4146 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4147 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4149 if ( m_rowLabelWin
)
4151 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4155 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4158 m_labelTextColour
= wxColour( _T("BLACK") );
4161 m_attrCache
.row
= -1;
4162 m_attrCache
.col
= -1;
4163 m_attrCache
.attr
= NULL
;
4165 // TODO: something better than this ?
4167 m_labelFont
= this->GetFont();
4168 m_labelFont
.SetWeight( wxBOLD
);
4170 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4171 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4173 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4174 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4175 m_colLabelTextOrientation
= wxHORIZONTAL
;
4177 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4178 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4180 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4181 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4183 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4184 m_defaultRowHeight
+= 8;
4186 m_defaultRowHeight
+= 4;
4189 m_gridLineColour
= wxColour( 192,192,192 );
4190 m_gridLinesEnabled
= true;
4191 m_cellHighlightColour
= *wxBLACK
;
4192 m_cellHighlightPenWidth
= 2;
4193 m_cellHighlightROPenWidth
= 1;
4195 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4196 m_winCapture
= (wxWindow
*)NULL
;
4197 m_canDragRowSize
= true;
4198 m_canDragColSize
= true;
4199 m_canDragGridSize
= true;
4200 m_canDragCell
= false;
4202 m_dragRowOrCol
= -1;
4203 m_isDragging
= false;
4204 m_startDragPos
= wxDefaultPosition
;
4206 m_waitForSlowClick
= false;
4208 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4209 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4211 m_currentCellCoords
= wxGridNoCellCoords
;
4213 m_selectingTopLeft
= wxGridNoCellCoords
;
4214 m_selectingBottomRight
= wxGridNoCellCoords
;
4215 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4216 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4218 m_editable
= true; // default for whole grid
4220 m_inOnKeyDown
= false;
4226 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4227 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4230 // ----------------------------------------------------------------------------
4231 // the idea is to call these functions only when necessary because they create
4232 // quite big arrays which eat memory mostly unnecessary - in particular, if
4233 // default widths/heights are used for all rows/columns, we may not use these
4236 // with some extra code, it should be possible to only store the
4237 // widths/heights different from default ones but this will be done later...
4238 // ----------------------------------------------------------------------------
4240 void wxGrid::InitRowHeights()
4242 m_rowHeights
.Empty();
4243 m_rowBottoms
.Empty();
4245 m_rowHeights
.Alloc( m_numRows
);
4246 m_rowBottoms
.Alloc( m_numRows
);
4250 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4252 for ( int i
= 0; i
< m_numRows
; i
++ )
4254 rowBottom
+= m_defaultRowHeight
;
4255 m_rowBottoms
.Add( rowBottom
);
4259 void wxGrid::InitColWidths()
4261 m_colWidths
.Empty();
4262 m_colRights
.Empty();
4264 m_colWidths
.Alloc( m_numCols
);
4265 m_colRights
.Alloc( m_numCols
);
4268 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4270 for ( int i
= 0; i
< m_numCols
; i
++ )
4272 colRight
+= m_defaultColWidth
;
4273 m_colRights
.Add( colRight
);
4277 int wxGrid::GetColWidth(int col
) const
4279 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4282 int wxGrid::GetColLeft(int col
) const
4284 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4285 : m_colRights
[col
] - m_colWidths
[col
];
4288 int wxGrid::GetColRight(int col
) const
4290 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4294 int wxGrid::GetRowHeight(int row
) const
4296 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4299 int wxGrid::GetRowTop(int row
) const
4301 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4302 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4305 int wxGrid::GetRowBottom(int row
) const
4307 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4308 : m_rowBottoms
[row
];
4311 void wxGrid::CalcDimensions()
4314 GetClientSize( &cw
, &ch
);
4316 if ( m_rowLabelWin
->IsShown() )
4317 cw
-= m_rowLabelWidth
;
4318 if ( m_colLabelWin
->IsShown() )
4319 ch
-= m_colLabelHeight
;
4322 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4323 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4325 // take into account editor if shown
4326 if( IsCellEditControlShown() )
4329 int r
= m_currentCellCoords
.GetRow();
4330 int c
= m_currentCellCoords
.GetCol();
4331 int x
= GetColLeft(c
);
4332 int y
= GetRowTop(r
);
4334 // how big is the editor
4335 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4336 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4337 editor
->GetControl()->GetSize(&w2
, &h2
);
4340 if( w2
> w
) w
= w2
;
4341 if( h2
> h
) h
= h2
;
4346 // preserve (more or less) the previous position
4348 GetViewStart( &x
, &y
);
4350 // ensure the position is valid for the new scroll ranges
4352 x
= wxMax( w
- 1, 0 );
4354 y
= wxMax( h
- 1, 0 );
4356 // do set scrollbar parameters
4357 SetScrollbars( m_scrollLineX
, m_scrollLineY
,
4358 GetScrollX(w
), GetScrollY(h
), x
, y
,
4359 GetBatchCount() != 0);
4361 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4362 // still must reposition the children
4367 void wxGrid::CalcWindowSizes()
4369 // escape if the window is has not been fully created yet
4371 if ( m_cornerLabelWin
== NULL
)
4375 GetClientSize( &cw
, &ch
);
4377 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4378 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4380 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4381 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4383 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4384 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4386 if ( m_gridWin
&& m_gridWin
->IsShown() )
4387 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4391 // this is called when the grid table sends a message to say that it
4392 // has been redimensioned
4394 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4397 bool result
= false;
4399 // Clear the attribute cache as the attribute might refer to a different
4400 // cell than stored in the cache after adding/removing rows/columns.
4402 // By the same reasoning, the editor should be dismissed if columns are
4403 // added or removed. And for consistency, it should IMHO always be
4404 // removed, not only if the cell "underneath" it actually changes.
4405 // For now, I intentionally do not save the editor's content as the
4406 // cell it might want to save that stuff to might no longer exist.
4407 HideCellEditControl();
4409 // if we were using the default widths/heights so far, we must change them
4411 if ( m_colWidths
.IsEmpty() )
4416 if ( m_rowHeights
.IsEmpty() )
4422 switch ( msg
.GetId() )
4424 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4426 size_t pos
= msg
.GetCommandInt();
4427 int numRows
= msg
.GetCommandInt2();
4429 m_numRows
+= numRows
;
4431 if ( !m_rowHeights
.IsEmpty() )
4433 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4434 m_rowBottoms
.Insert( 0, pos
, numRows
);
4437 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4439 for ( i
= pos
; i
< m_numRows
; i
++ )
4441 bottom
+= m_rowHeights
[i
];
4442 m_rowBottoms
[i
] = bottom
;
4445 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4447 // if we have just inserted cols into an empty grid the current
4448 // cell will be undefined...
4450 SetCurrentCell( 0, 0 );
4454 m_selection
->UpdateRows( pos
, numRows
);
4455 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4457 attrProvider
->UpdateAttrRows( pos
, numRows
);
4459 if ( !GetBatchCount() )
4462 m_rowLabelWin
->Refresh();
4468 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4470 int numRows
= msg
.GetCommandInt();
4471 int oldNumRows
= m_numRows
;
4472 m_numRows
+= numRows
;
4474 if ( !m_rowHeights
.IsEmpty() )
4476 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4477 m_rowBottoms
.Add( 0, numRows
);
4480 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4482 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4484 bottom
+= m_rowHeights
[i
];
4485 m_rowBottoms
[i
] = bottom
;
4488 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4490 // if we have just inserted cols into an empty grid the current
4491 // cell will be undefined...
4493 SetCurrentCell( 0, 0 );
4495 if ( !GetBatchCount() )
4498 m_rowLabelWin
->Refresh();
4504 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4506 size_t pos
= msg
.GetCommandInt();
4507 int numRows
= msg
.GetCommandInt2();
4508 m_numRows
-= numRows
;
4510 if ( !m_rowHeights
.IsEmpty() )
4512 m_rowHeights
.RemoveAt( pos
, numRows
);
4513 m_rowBottoms
.RemoveAt( pos
, numRows
);
4516 for ( i
= 0; i
< m_numRows
; i
++ )
4518 h
+= m_rowHeights
[i
];
4519 m_rowBottoms
[i
] = h
;
4524 m_currentCellCoords
= wxGridNoCellCoords
;
4528 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4529 m_currentCellCoords
.Set( 0, 0 );
4533 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4534 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4536 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4537 // ifdef'd out following patch from Paul Gammans
4539 // No need to touch column attributes, unless we
4540 // removed _all_ rows, in this case, we remove
4541 // all column attributes.
4542 // I hate to do this here, but the
4543 // needed data is not available inside UpdateAttrRows.
4544 if ( !GetNumberRows() )
4545 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4548 if ( !GetBatchCount() )
4551 m_rowLabelWin
->Refresh();
4557 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4559 size_t pos
= msg
.GetCommandInt();
4560 int numCols
= msg
.GetCommandInt2();
4561 m_numCols
+= numCols
;
4563 if ( !m_colWidths
.IsEmpty() )
4565 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4566 m_colRights
.Insert( 0, pos
, numCols
);
4569 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4571 for ( i
= pos
; i
< m_numCols
; i
++ )
4573 right
+= m_colWidths
[i
];
4574 m_colRights
[i
] = right
;
4577 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4579 // if we have just inserted cols into an empty grid the current
4580 // cell will be undefined...
4582 SetCurrentCell( 0, 0 );
4586 m_selection
->UpdateCols( pos
, numCols
);
4587 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4589 attrProvider
->UpdateAttrCols( pos
, numCols
);
4590 if ( !GetBatchCount() )
4593 m_colLabelWin
->Refresh();
4600 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4602 int numCols
= msg
.GetCommandInt();
4603 int oldNumCols
= m_numCols
;
4604 m_numCols
+= numCols
;
4605 if ( !m_colWidths
.IsEmpty() )
4607 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4608 m_colRights
.Add( 0, numCols
);
4611 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4613 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4615 right
+= m_colWidths
[i
];
4616 m_colRights
[i
] = right
;
4619 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4621 // if we have just inserted cols into an empty grid the current
4622 // cell will be undefined...
4624 SetCurrentCell( 0, 0 );
4626 if ( !GetBatchCount() )
4629 m_colLabelWin
->Refresh();
4635 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4637 size_t pos
= msg
.GetCommandInt();
4638 int numCols
= msg
.GetCommandInt2();
4639 m_numCols
-= numCols
;
4641 if ( !m_colWidths
.IsEmpty() )
4643 m_colWidths
.RemoveAt( pos
, numCols
);
4644 m_colRights
.RemoveAt( pos
, numCols
);
4647 for ( i
= 0; i
< m_numCols
; i
++ )
4649 w
+= m_colWidths
[i
];
4655 m_currentCellCoords
= wxGridNoCellCoords
;
4659 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4660 m_currentCellCoords
.Set( 0, 0 );
4664 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4665 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4667 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4668 // ifdef'd out following patch from Paul Gammans
4670 // No need to touch row attributes, unless we
4671 // removed _all_ columns, in this case, we remove
4672 // all row attributes.
4673 // I hate to do this here, but the
4674 // needed data is not available inside UpdateAttrCols.
4675 if ( !GetNumberCols() )
4676 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4679 if ( !GetBatchCount() )
4682 m_colLabelWin
->Refresh();
4689 if (result
&& !GetBatchCount() )
4690 m_gridWin
->Refresh();
4695 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4697 wxRegionIterator
iter( reg
);
4700 wxArrayInt rowlabels
;
4707 // TODO: remove this when we can...
4708 // There is a bug in wxMotif that gives garbage update
4709 // rectangles if you jump-scroll a long way by clicking the
4710 // scrollbar with middle button. This is a work-around
4712 #if defined(__WXMOTIF__)
4714 m_gridWin
->GetClientSize( &cw
, &ch
);
4715 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4716 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4719 // logical bounds of update region
4722 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4723 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4725 // find the row labels within these bounds
4728 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4730 if ( GetRowBottom(row
) < top
)
4733 if ( GetRowTop(row
) > bottom
)
4736 rowlabels
.Add( row
);
4746 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4748 wxRegionIterator
iter( reg
);
4751 wxArrayInt colLabels
;
4758 // TODO: remove this when we can...
4759 // There is a bug in wxMotif that gives garbage update
4760 // rectangles if you jump-scroll a long way by clicking the
4761 // scrollbar with middle button. This is a work-around
4763 #if defined(__WXMOTIF__)
4765 m_gridWin
->GetClientSize( &cw
, &ch
);
4766 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4767 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4770 // logical bounds of update region
4773 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4774 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4776 // find the cells within these bounds
4779 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4781 if ( GetColRight(col
) < left
)
4784 if ( GetColLeft(col
) > right
)
4787 colLabels
.Add( col
);
4796 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4798 wxRegionIterator
iter( reg
);
4801 wxGridCellCoordsArray cellsExposed
;
4803 int left
, top
, right
, bottom
;
4808 // TODO: remove this when we can...
4809 // There is a bug in wxMotif that gives garbage update
4810 // rectangles if you jump-scroll a long way by clicking the
4811 // scrollbar with middle button. This is a work-around
4813 #if defined(__WXMOTIF__)
4815 m_gridWin
->GetClientSize( &cw
, &ch
);
4816 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4817 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4818 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4819 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4822 // logical bounds of update region
4824 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4825 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4827 // find the cells within these bounds
4830 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4832 if ( GetRowBottom(row
) <= top
)
4835 if ( GetRowTop(row
) > bottom
)
4838 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4840 if ( GetColRight(col
) <= left
)
4843 if ( GetColLeft(col
) > right
)
4846 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4853 return cellsExposed
;
4857 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4860 wxPoint
pos( event
.GetPosition() );
4861 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4863 if ( event
.Dragging() )
4867 m_isDragging
= true;
4868 m_rowLabelWin
->CaptureMouse();
4871 if ( event
.LeftIsDown() )
4873 switch( m_cursorMode
)
4875 case WXGRID_CURSOR_RESIZE_ROW
:
4877 int cw
, ch
, left
, dummy
;
4878 m_gridWin
->GetClientSize( &cw
, &ch
);
4879 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4881 wxClientDC
dc( m_gridWin
);
4884 GetRowTop(m_dragRowOrCol
) +
4885 GetRowMinimalHeight(m_dragRowOrCol
) );
4886 dc
.SetLogicalFunction(wxINVERT
);
4887 if ( m_dragLastPos
>= 0 )
4889 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4891 dc
.DrawLine( left
, y
, left
+cw
, y
);
4896 case WXGRID_CURSOR_SELECT_ROW
:
4897 if ( (row
= YToRow( y
)) >= 0 )
4901 m_selection
->SelectRow( row
,
4902 event
.ControlDown(),
4909 // default label to suppress warnings about "enumeration value
4910 // 'xxx' not handled in switch
4918 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4923 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4924 m_isDragging
= false;
4927 // ------------ Entering or leaving the window
4929 if ( event
.Entering() || event
.Leaving() )
4931 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4935 // ------------ Left button pressed
4937 else if ( event
.LeftDown() )
4939 // don't send a label click event for a hit on the
4940 // edge of the row label - this is probably the user
4941 // wanting to resize the row
4943 if ( YToEdgeOfRow(y
) < 0 )
4947 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4949 if ( !event
.ShiftDown() && !event
.ControlDown() )
4953 if ( event
.ShiftDown() )
4955 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4958 GetNumberCols() - 1,
4959 event
.ControlDown(),
4966 m_selection
->SelectRow( row
,
4967 event
.ControlDown(),
4974 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4979 // starting to drag-resize a row
4981 if ( CanDragRowSize() )
4982 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4987 // ------------ Left double click
4989 else if (event
.LeftDClick() )
4991 int row
= YToEdgeOfRow(y
);
4996 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4998 // no default action at the moment
5003 // adjust row height depending on label text
5004 AutoSizeRowLabelSize( row
);
5006 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5012 // ------------ Left button released
5014 else if ( event
.LeftUp() )
5016 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5018 DoEndDragResizeRow();
5020 // Note: we are ending the event *after* doing
5021 // default processing in this case
5023 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5026 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5031 // ------------ Right button down
5033 else if ( event
.RightDown() )
5037 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5039 // no default action at the moment
5044 // ------------ Right double click
5046 else if ( event
.RightDClick() )
5050 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5052 // no default action at the moment
5057 // ------------ No buttons down and mouse moving
5059 else if ( event
.Moving() )
5061 m_dragRowOrCol
= YToEdgeOfRow( y
);
5062 if ( m_dragRowOrCol
>= 0 )
5064 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5066 // don't capture the mouse yet
5067 if ( CanDragRowSize() )
5068 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5071 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5073 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5079 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5082 wxPoint
pos( event
.GetPosition() );
5083 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5085 if ( event
.Dragging() )
5089 m_isDragging
= true;
5090 m_colLabelWin
->CaptureMouse();
5093 if ( event
.LeftIsDown() )
5095 switch( m_cursorMode
)
5097 case WXGRID_CURSOR_RESIZE_COL
:
5099 int cw
, ch
, dummy
, top
;
5100 m_gridWin
->GetClientSize( &cw
, &ch
);
5101 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5103 wxClientDC
dc( m_gridWin
);
5106 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5107 GetColMinimalWidth(m_dragRowOrCol
));
5108 dc
.SetLogicalFunction(wxINVERT
);
5109 if ( m_dragLastPos
>= 0 )
5111 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5113 dc
.DrawLine( x
, top
, x
, top
+ch
);
5118 case WXGRID_CURSOR_SELECT_COL
:
5119 if ( (col
= XToCol( x
)) >= 0 )
5123 m_selection
->SelectCol( col
,
5124 event
.ControlDown(),
5131 // default label to suppress warnings about "enumeration value
5132 // 'xxx' not handled in switch
5140 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5145 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5146 m_isDragging
= false;
5149 // ------------ Entering or leaving the window
5151 if ( event
.Entering() || event
.Leaving() )
5153 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5157 // ------------ Left button pressed
5159 else if ( event
.LeftDown() )
5161 // don't send a label click event for a hit on the
5162 // edge of the col label - this is probably the user
5163 // wanting to resize the col
5165 if ( XToEdgeOfCol(x
) < 0 )
5169 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5171 if ( !event
.ShiftDown() && !event
.ControlDown() )
5175 if ( event
.ShiftDown() )
5177 m_selection
->SelectBlock( 0,
5178 m_currentCellCoords
.GetCol(),
5179 GetNumberRows() - 1, col
,
5180 event
.ControlDown(),
5187 m_selection
->SelectCol( col
,
5188 event
.ControlDown(),
5195 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5200 // starting to drag-resize a col
5202 if ( CanDragColSize() )
5203 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5208 // ------------ Left double click
5210 if ( event
.LeftDClick() )
5212 int col
= XToEdgeOfCol(x
);
5217 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5219 // no default action at the moment
5224 // adjust column width depending on label text
5225 AutoSizeColLabelSize( col
);
5227 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5233 // ------------ Left button released
5235 else if ( event
.LeftUp() )
5237 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5239 DoEndDragResizeCol();
5241 // Note: we are ending the event *after* doing
5242 // default processing in this case
5244 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5247 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5252 // ------------ Right button down
5254 else if ( event
.RightDown() )
5258 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5260 // no default action at the moment
5265 // ------------ Right double click
5267 else if ( event
.RightDClick() )
5271 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5273 // no default action at the moment
5278 // ------------ No buttons down and mouse moving
5280 else if ( event
.Moving() )
5282 m_dragRowOrCol
= XToEdgeOfCol( x
);
5283 if ( m_dragRowOrCol
>= 0 )
5285 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5287 // don't capture the cursor yet
5288 if ( CanDragColSize() )
5289 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5292 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5294 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5300 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5302 if ( event
.LeftDown() )
5304 // indicate corner label by having both row and
5307 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5313 else if ( event
.LeftDClick() )
5315 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5318 else if ( event
.RightDown() )
5320 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5322 // no default action at the moment
5326 else if ( event
.RightDClick() )
5328 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5330 // no default action at the moment
5335 void wxGrid::ChangeCursorMode(CursorMode mode
,
5340 static const wxChar
*cursorModes
[] =
5349 wxLogTrace(_T("grid"),
5350 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5351 win
== m_colLabelWin
? _T("colLabelWin")
5352 : win
? _T("rowLabelWin")
5354 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5355 #endif // __WXDEBUG__
5357 if ( mode
== m_cursorMode
&&
5358 win
== m_winCapture
&&
5359 captureMouse
== (m_winCapture
!= NULL
))
5364 // by default use the grid itself
5370 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5371 m_winCapture
= (wxWindow
*)NULL
;
5374 m_cursorMode
= mode
;
5376 switch ( m_cursorMode
)
5378 case WXGRID_CURSOR_RESIZE_ROW
:
5379 win
->SetCursor( m_rowResizeCursor
);
5382 case WXGRID_CURSOR_RESIZE_COL
:
5383 win
->SetCursor( m_colResizeCursor
);
5387 win
->SetCursor( *wxSTANDARD_CURSOR
);
5390 // we need to capture mouse when resizing
5391 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5392 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5394 if ( captureMouse
&& resize
)
5396 win
->CaptureMouse();
5401 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5404 wxPoint
pos( event
.GetPosition() );
5405 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5407 wxGridCellCoords coords
;
5408 XYToCell( x
, y
, coords
);
5410 int cell_rows
, cell_cols
;
5411 bool isFirstDrag
= !m_isDragging
;
5412 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5413 if ((cell_rows
< 0) || (cell_cols
< 0))
5415 coords
.SetRow(coords
.GetRow() + cell_rows
);
5416 coords
.SetCol(coords
.GetCol() + cell_cols
);
5419 if ( event
.Dragging() )
5421 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5423 // Don't start doing anything until the mouse has been drug at
5424 // least 3 pixels in any direction...
5427 if (m_startDragPos
== wxDefaultPosition
)
5429 m_startDragPos
= pos
;
5432 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5436 m_isDragging
= true;
5437 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5439 // Hide the edit control, so it
5440 // won't interfer with drag-shrinking.
5441 if ( IsCellEditControlShown() )
5443 HideCellEditControl();
5444 SaveEditControlValue();
5447 // Have we captured the mouse yet?
5450 m_winCapture
= m_gridWin
;
5451 m_winCapture
->CaptureMouse();
5454 if ( coords
!= wxGridNoCellCoords
)
5456 if ( event
.ControlDown() )
5458 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5459 m_selectingKeyboard
= coords
;
5460 HighlightBlock ( m_selectingKeyboard
, coords
);
5462 else if ( CanDragCell() )
5466 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5467 m_selectingKeyboard
= coords
;
5469 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5477 if ( !IsSelection() )
5479 HighlightBlock( coords
, coords
);
5483 HighlightBlock( m_currentCellCoords
, coords
);
5487 if (! IsVisible(coords
))
5489 MakeCellVisible(coords
);
5490 // TODO: need to introduce a delay or something here. The
5491 // scrolling is way to fast, at least on MSW - also on GTK.
5495 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5497 int cw
, ch
, left
, dummy
;
5498 m_gridWin
->GetClientSize( &cw
, &ch
);
5499 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5501 wxClientDC
dc( m_gridWin
);
5503 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5504 GetRowMinimalHeight(m_dragRowOrCol
) );
5505 dc
.SetLogicalFunction(wxINVERT
);
5506 if ( m_dragLastPos
>= 0 )
5508 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5510 dc
.DrawLine( left
, y
, left
+cw
, y
);
5513 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5515 int cw
, ch
, dummy
, top
;
5516 m_gridWin
->GetClientSize( &cw
, &ch
);
5517 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5519 wxClientDC
dc( m_gridWin
);
5521 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5522 GetColMinimalWidth(m_dragRowOrCol
) );
5523 dc
.SetLogicalFunction(wxINVERT
);
5524 if ( m_dragLastPos
>= 0 )
5526 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5528 dc
.DrawLine( x
, top
, x
, top
+ch
);
5535 m_isDragging
= false;
5536 m_startDragPos
= wxDefaultPosition
;
5538 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5539 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5542 if ( event
.Entering() || event
.Leaving() )
5544 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5545 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5550 // ------------ Left button pressed
5552 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5554 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5559 if ( !event
.ControlDown() )
5561 if ( event
.ShiftDown() )
5565 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5566 m_currentCellCoords
.GetCol(),
5569 event
.ControlDown(),
5575 else if ( XToEdgeOfCol(x
) < 0 &&
5576 YToEdgeOfRow(y
) < 0 )
5578 DisableCellEditControl();
5579 MakeCellVisible( coords
);
5581 if ( event
.ControlDown() )
5585 m_selection
->ToggleCellSelection( coords
.GetRow(),
5587 event
.ControlDown(),
5592 m_selectingTopLeft
= wxGridNoCellCoords
;
5593 m_selectingBottomRight
= wxGridNoCellCoords
;
5594 m_selectingKeyboard
= coords
;
5598 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5599 SetCurrentCell( coords
);
5602 if ( m_selection
->GetSelectionMode() !=
5603 wxGrid::wxGridSelectCells
)
5605 HighlightBlock( coords
, coords
);
5614 // ------------ Left double click
5616 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5618 DisableCellEditControl();
5620 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5622 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5627 // we want double click to select a cell and start editing
5628 // (i.e. to behave in same way as sequence of two slow clicks):
5629 m_waitForSlowClick
= true;
5636 // ------------ Left button released
5638 else if ( event
.LeftUp() )
5640 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5644 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5645 m_winCapture
= NULL
;
5648 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5651 EnableCellEditControl();
5653 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5654 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5655 editor
->StartingClick();
5659 m_waitForSlowClick
= false;
5661 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5662 m_selectingBottomRight
!= wxGridNoCellCoords
)
5666 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5667 m_selectingTopLeft
.GetCol(),
5668 m_selectingBottomRight
.GetRow(),
5669 m_selectingBottomRight
.GetCol(),
5670 event
.ControlDown(),
5676 m_selectingTopLeft
= wxGridNoCellCoords
;
5677 m_selectingBottomRight
= wxGridNoCellCoords
;
5679 // Show the edit control, if it has been hidden for
5681 ShowCellEditControl();
5684 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5686 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5687 DoEndDragResizeRow();
5689 // Note: we are ending the event *after* doing
5690 // default processing in this case
5692 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5694 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5696 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5697 DoEndDragResizeCol();
5699 // Note: we are ending the event *after* doing
5700 // default processing in this case
5702 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5709 // ------------ Right button down
5711 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5713 DisableCellEditControl();
5714 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5719 // no default action at the moment
5724 // ------------ Right double click
5726 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5728 DisableCellEditControl();
5729 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5734 // no default action at the moment
5738 // ------------ Moving and no button action
5740 else if ( event
.Moving() && !event
.IsButton() )
5742 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5744 // out of grid cell area
5745 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5749 int dragRow
= YToEdgeOfRow( y
);
5750 int dragCol
= XToEdgeOfCol( x
);
5752 // Dragging on the corner of a cell to resize in both
5753 // directions is not implemented yet...
5755 if ( dragRow
>= 0 && dragCol
>= 0 )
5757 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5763 m_dragRowOrCol
= dragRow
;
5765 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5767 if ( CanDragRowSize() && CanDragGridSize() )
5768 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5773 m_dragRowOrCol
= dragCol
;
5781 m_dragRowOrCol
= dragCol
;
5783 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5785 if ( CanDragColSize() && CanDragGridSize() )
5786 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5792 // Neither on a row or col edge
5794 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5796 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5802 void wxGrid::DoEndDragResizeRow()
5804 if ( m_dragLastPos
>= 0 )
5806 // erase the last line and resize the row
5808 int cw
, ch
, left
, dummy
;
5809 m_gridWin
->GetClientSize( &cw
, &ch
);
5810 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5812 wxClientDC
dc( m_gridWin
);
5814 dc
.SetLogicalFunction( wxINVERT
);
5815 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5816 HideCellEditControl();
5817 SaveEditControlValue();
5819 int rowTop
= GetRowTop(m_dragRowOrCol
);
5820 SetRowSize( m_dragRowOrCol
,
5821 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5823 if ( !GetBatchCount() )
5825 // Only needed to get the correct rect.y:
5826 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5828 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5829 rect
.width
= m_rowLabelWidth
;
5830 rect
.height
= ch
- rect
.y
;
5831 m_rowLabelWin
->Refresh( true, &rect
);
5833 // if there is a multicell block, paint all of it
5836 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5837 int leftCol
= XToCol(left
);
5838 int rightCol
= internalXToCol(left
+cw
);
5841 for (i
=leftCol
; i
<rightCol
; i
++)
5843 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5844 if (cell_rows
< subtract_rows
)
5845 subtract_rows
= cell_rows
;
5847 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5848 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5849 rect
.height
= ch
- rect
.y
;
5852 m_gridWin
->Refresh( false, &rect
);
5855 ShowCellEditControl();
5860 void wxGrid::DoEndDragResizeCol()
5862 if ( m_dragLastPos
>= 0 )
5864 // erase the last line and resize the col
5866 int cw
, ch
, dummy
, top
;
5867 m_gridWin
->GetClientSize( &cw
, &ch
);
5868 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5870 wxClientDC
dc( m_gridWin
);
5872 dc
.SetLogicalFunction( wxINVERT
);
5873 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5874 HideCellEditControl();
5875 SaveEditControlValue();
5877 int colLeft
= GetColLeft(m_dragRowOrCol
);
5878 SetColSize( m_dragRowOrCol
,
5879 wxMax( m_dragLastPos
- colLeft
,
5880 GetColMinimalWidth(m_dragRowOrCol
) ) );
5882 if ( !GetBatchCount() )
5884 // Only needed to get the correct rect.x:
5885 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5887 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5888 rect
.width
= cw
- rect
.x
;
5889 rect
.height
= m_colLabelHeight
;
5890 m_colLabelWin
->Refresh( true, &rect
);
5892 // if there is a multicell block, paint all of it
5895 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5896 int topRow
= YToRow(top
);
5897 int bottomRow
= internalYToRow(top
+cw
);
5900 for (i
=topRow
; i
<bottomRow
; i
++)
5902 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5903 if (cell_cols
< subtract_cols
)
5904 subtract_cols
= cell_cols
;
5906 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5907 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5908 rect
.width
= cw
- rect
.x
;
5911 m_gridWin
->Refresh( false, &rect
);
5914 ShowCellEditControl();
5921 // ------ interaction with data model
5923 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5925 switch ( msg
.GetId() )
5927 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5928 return GetModelValues();
5930 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5931 return SetModelValues();
5933 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5934 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5935 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5936 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5937 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5938 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5939 return Redimension( msg
);
5948 // The behaviour of this function depends on the grid table class
5949 // Clear() function. For the default wxGridStringTable class the
5950 // behavious is to replace all cell contents with wxEmptyString but
5951 // not to change the number of rows or cols.
5953 void wxGrid::ClearGrid()
5957 if (IsCellEditControlEnabled())
5958 DisableCellEditControl();
5961 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5966 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5968 // TODO: something with updateLabels flag
5972 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5978 if (IsCellEditControlEnabled())
5979 DisableCellEditControl();
5981 bool done
= m_table
->InsertRows( pos
, numRows
);
5984 // the table will have sent the results of the insert row
5985 // operation to this view object as a grid table message
5991 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5993 // TODO: something with updateLabels flag
5997 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
6003 bool done
= m_table
&& m_table
->AppendRows( numRows
);
6005 // the table will have sent the results of the append row
6006 // operation to this view object as a grid table message
6012 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
6014 // TODO: something with updateLabels flag
6018 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
6024 if (IsCellEditControlEnabled())
6025 DisableCellEditControl();
6027 bool done
= m_table
->DeleteRows( pos
, numRows
);
6029 // the table will have sent the results of the delete row
6030 // operation to this view object as a grid table message
6036 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6038 // TODO: something with updateLabels flag
6042 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6048 if (IsCellEditControlEnabled())
6049 DisableCellEditControl();
6051 bool done
= m_table
->InsertCols( pos
, numCols
);
6053 // the table will have sent the results of the insert col
6054 // operation to this view object as a grid table message
6060 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6062 // TODO: something with updateLabels flag
6066 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6072 bool done
= m_table
->AppendCols( numCols
);
6074 // the table will have sent the results of the append col
6075 // operation to this view object as a grid table message
6081 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6083 // TODO: something with updateLabels flag
6087 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6093 if (IsCellEditControlEnabled())
6094 DisableCellEditControl();
6096 bool done
= m_table
->DeleteCols( pos
, numCols
);
6098 // the table will have sent the results of the delete col
6099 // operation to this view object as a grid table message
6107 // ----- event handlers
6110 // Generate a grid event based on a mouse event and
6111 // return the result of ProcessEvent()
6113 int wxGrid::SendEvent( const wxEventType type
,
6115 wxMouseEvent
& mouseEv
)
6120 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6122 int rowOrCol
= (row
== -1 ? col
: row
);
6124 wxGridSizeEvent
gridEvt( GetId(),
6128 mouseEv
.GetX() + GetRowLabelSize(),
6129 mouseEv
.GetY() + GetColLabelSize(),
6130 mouseEv
.ControlDown(),
6131 mouseEv
.ShiftDown(),
6133 mouseEv
.MetaDown() );
6135 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6136 vetoed
= !gridEvt
.IsAllowed();
6138 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6140 // Right now, it should _never_ end up here!
6141 wxGridRangeSelectEvent
gridEvt( GetId(),
6145 m_selectingBottomRight
,
6147 mouseEv
.ControlDown(),
6148 mouseEv
.ShiftDown(),
6150 mouseEv
.MetaDown() );
6152 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6153 vetoed
= !gridEvt
.IsAllowed();
6157 wxGridEvent
gridEvt( GetId(),
6161 mouseEv
.GetX() + GetRowLabelSize(),
6162 mouseEv
.GetY() + GetColLabelSize(),
6164 mouseEv
.ControlDown(),
6165 mouseEv
.ShiftDown(),
6167 mouseEv
.MetaDown() );
6168 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6169 vetoed
= !gridEvt
.IsAllowed();
6172 // A Veto'd event may not be `claimed' so test this first
6173 if (vetoed
) return -1;
6174 return claimed
? 1 : 0;
6178 // Generate a grid event of specified type and return the result
6179 // of ProcessEvent().
6181 int wxGrid::SendEvent( const wxEventType type
,
6187 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6189 int rowOrCol
= (row
== -1 ? col
: row
);
6191 wxGridSizeEvent
gridEvt( GetId(),
6196 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6197 vetoed
= !gridEvt
.IsAllowed();
6201 wxGridEvent
gridEvt( GetId(),
6206 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6207 vetoed
= !gridEvt
.IsAllowed();
6210 // A Veto'd event may not be `claimed' so test this first
6211 if (vetoed
) return -1;
6212 return claimed
? 1 : 0;
6216 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6218 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6221 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6223 // Don't do anything if between Begin/EndBatch...
6224 // EndBatch() will do all this on the last nested one anyway.
6225 if (! GetBatchCount())
6227 // Refresh to get correct scrolled position:
6228 wxScrolledWindow::Refresh(eraseb
,rect
);
6232 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6233 int width_label
, width_cell
, height_label
, height_cell
;
6236 //Copy rectangle can get scroll offsets..
6237 rect_x
= rect
->GetX();
6238 rect_y
= rect
->GetY();
6239 rectWidth
= rect
->GetWidth();
6240 rectHeight
= rect
->GetHeight();
6242 width_label
= m_rowLabelWidth
- rect_x
;
6243 if (width_label
> rectWidth
) width_label
= rectWidth
;
6245 height_label
= m_colLabelHeight
- rect_y
;
6246 if (height_label
> rectHeight
) height_label
= rectHeight
;
6248 if (rect_x
> m_rowLabelWidth
)
6250 x
= rect_x
- m_rowLabelWidth
;
6251 width_cell
= rectWidth
;
6256 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6259 if (rect_y
> m_colLabelHeight
)
6261 y
= rect_y
- m_colLabelHeight
;
6262 height_cell
= rectHeight
;
6267 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6270 // Paint corner label part intersecting rect.
6271 if ( width_label
> 0 && height_label
> 0 )
6273 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6274 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6277 // Paint col labels part intersecting rect.
6278 if ( width_cell
> 0 && height_label
> 0 )
6280 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6281 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6284 // Paint row labels part intersecting rect.
6285 if ( width_label
> 0 && height_cell
> 0 )
6287 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6288 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6291 // Paint cell area part intersecting rect.
6292 if ( width_cell
> 0 && height_cell
> 0 )
6294 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6295 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6300 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6301 m_colLabelWin
->Refresh(eraseb
, NULL
);
6302 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6303 m_gridWin
->Refresh(eraseb
, NULL
);
6308 void wxGrid::OnSize( wxSizeEvent
& event
)
6310 // position the child windows
6313 // don't call CalcDimensions() from here, the base class handles the size
6319 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6321 if ( m_inOnKeyDown
)
6323 // shouldn't be here - we are going round in circles...
6325 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6328 m_inOnKeyDown
= true;
6330 // propagate the event up and see if it gets processed
6332 wxWindow
*parent
= GetParent();
6333 wxKeyEvent
keyEvt( event
);
6334 keyEvt
.SetEventObject( parent
);
6336 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6339 // try local handlers
6341 switch ( event
.GetKeyCode() )
6344 if ( event
.ControlDown() )
6346 MoveCursorUpBlock( event
.ShiftDown() );
6350 MoveCursorUp( event
.ShiftDown() );
6355 if ( event
.ControlDown() )
6357 MoveCursorDownBlock( event
.ShiftDown() );
6361 MoveCursorDown( event
.ShiftDown() );
6366 if ( event
.ControlDown() )
6368 MoveCursorLeftBlock( event
.ShiftDown() );
6372 MoveCursorLeft( event
.ShiftDown() );
6377 if ( event
.ControlDown() )
6379 MoveCursorRightBlock( event
.ShiftDown() );
6383 MoveCursorRight( event
.ShiftDown() );
6388 case WXK_NUMPAD_ENTER
:
6389 if ( event
.ControlDown() )
6391 event
.Skip(); // to let the edit control have the return
6395 if ( GetGridCursorRow() < GetNumberRows()-1 )
6397 MoveCursorDown( event
.ShiftDown() );
6401 // at the bottom of a column
6402 DisableCellEditControl();
6412 if (event
.ShiftDown())
6414 if ( GetGridCursorCol() > 0 )
6416 MoveCursorLeft( false );
6421 DisableCellEditControl();
6426 if ( GetGridCursorCol() < GetNumberCols()-1 )
6428 MoveCursorRight( false );
6433 DisableCellEditControl();
6439 if ( event
.ControlDown() )
6441 MakeCellVisible( 0, 0 );
6442 SetCurrentCell( 0, 0 );
6451 if ( event
.ControlDown() )
6453 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6454 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6471 if ( event
.ControlDown() )
6475 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6476 m_currentCellCoords
.GetCol(),
6477 event
.ControlDown(),
6484 if ( !IsEditable() )
6486 MoveCursorRight( false );
6489 // Otherwise fall through to default
6497 m_inOnKeyDown
= false;
6500 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6502 // try local handlers
6504 if ( event
.GetKeyCode() == WXK_SHIFT
)
6506 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6507 m_selectingBottomRight
!= wxGridNoCellCoords
)
6511 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6512 m_selectingTopLeft
.GetCol(),
6513 m_selectingBottomRight
.GetRow(),
6514 m_selectingBottomRight
.GetCol(),
6515 event
.ControlDown(),
6522 m_selectingTopLeft
= wxGridNoCellCoords
;
6523 m_selectingBottomRight
= wxGridNoCellCoords
;
6524 m_selectingKeyboard
= wxGridNoCellCoords
;
6528 void wxGrid::OnChar( wxKeyEvent
& event
)
6530 // is it possible to edit the current cell at all?
6531 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6533 // yes, now check whether the cells editor accepts the key
6534 int row
= m_currentCellCoords
.GetRow();
6535 int col
= m_currentCellCoords
.GetCol();
6536 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6537 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6539 // <F2> is special and will always start editing, for
6540 // other keys - ask the editor itself
6541 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6542 || editor
->IsAcceptedKey(event
) )
6544 // ensure cell is visble
6545 MakeCellVisible(row
, col
);
6546 EnableCellEditControl();
6548 // a problem can arise if the cell is not completely
6549 // visible (even after calling MakeCellVisible the
6550 // control is not created and calling StartingKey will
6552 if ( event
.GetKeyCode() != WXK_F2
&& editor
->IsCreated() && m_cellEditCtrlEnabled
)
6553 editor
->StartingKey(event
);
6570 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6574 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6576 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6578 // the event has been intercepted - do nothing
6582 wxClientDC
dc(m_gridWin
);
6585 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6587 DisableCellEditControl();
6589 if ( IsVisible( m_currentCellCoords
, false ) )
6592 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6593 if ( !m_gridLinesEnabled
)
6601 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6603 // Otherwise refresh redraws the highlight!
6604 m_currentCellCoords
= coords
;
6606 DrawGridCellArea(dc
,cells
);
6607 DrawAllGridLines( dc
, r
);
6611 m_currentCellCoords
= coords
;
6613 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6614 DrawCellHighlight(dc
, attr
);
6619 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6622 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6626 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6629 rightCol
= GetNumberCols() - 1;
6631 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6634 bottomRow
= GetNumberRows() - 1;
6638 if ( topRow
> bottomRow
)
6645 if ( leftCol
> rightCol
)
6652 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6653 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6655 // First the case that we selected a completely new area
6656 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6657 m_selectingBottomRight
== wxGridNoCellCoords
)
6660 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6661 wxGridCellCoords ( bottomRow
, rightCol
) );
6662 m_gridWin
->Refresh( false, &rect
);
6664 // Now handle changing an existing selection area.
6665 else if ( m_selectingTopLeft
!= updateTopLeft
||
6666 m_selectingBottomRight
!= updateBottomRight
)
6668 // Compute two optimal update rectangles:
6669 // Either one rectangle is a real subset of the
6670 // other, or they are (almost) disjoint!
6672 bool need_refresh
[4];
6676 need_refresh
[3] = false;
6679 // Store intermediate values
6680 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6681 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6682 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6683 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6685 // Determine the outer/inner coordinates.
6686 if (oldLeft
> leftCol
)
6692 if (oldTop
> topRow
)
6698 if (oldRight
< rightCol
)
6701 oldRight
= rightCol
;
6704 if (oldBottom
< bottomRow
)
6707 oldBottom
= bottomRow
;
6711 // Now, either the stuff marked old is the outer
6712 // rectangle or we don't have a situation where one
6713 // is contained in the other.
6715 if ( oldLeft
< leftCol
)
6717 // Refresh the newly selected or deselected
6718 // area to the left of the old or new selection.
6719 need_refresh
[0] = true;
6720 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6722 wxGridCellCoords ( oldBottom
,
6726 if ( oldTop
< topRow
)
6728 // Refresh the newly selected or deselected
6729 // area above the old or new selection.
6730 need_refresh
[1] = true;
6731 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6733 wxGridCellCoords ( topRow
- 1,
6737 if ( oldRight
> rightCol
)
6739 // Refresh the newly selected or deselected
6740 // area to the right of the old or new selection.
6741 need_refresh
[2] = true;
6742 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6744 wxGridCellCoords ( oldBottom
,
6748 if ( oldBottom
> bottomRow
)
6750 // Refresh the newly selected or deselected
6751 // area below the old or new selection.
6752 need_refresh
[3] = true;
6753 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6755 wxGridCellCoords ( oldBottom
,
6759 // various Refresh() calls
6760 for (i
= 0; i
< 4; i
++ )
6761 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6762 m_gridWin
->Refresh( false, &(rect
[i
]) );
6765 m_selectingTopLeft
= updateTopLeft
;
6766 m_selectingBottomRight
= updateBottomRight
;
6770 // ------ functions to get/send data (see also public functions)
6773 bool wxGrid::GetModelValues()
6775 // Hide the editor, so it won't hide a changed value.
6776 HideCellEditControl();
6780 // all we need to do is repaint the grid
6782 m_gridWin
->Refresh();
6790 bool wxGrid::SetModelValues()
6794 // Disable the editor, so it won't hide a changed value.
6795 // Do we also want to save the current value of the editor first?
6797 DisableCellEditControl();
6801 for ( row
= 0; row
< m_numRows
; row
++ )
6803 for ( col
= 0; col
< m_numCols
; col
++ )
6805 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6817 // Note - this function only draws cells that are in the list of
6818 // exposed cells (usually set from the update region by
6819 // CalcExposedCells)
6821 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6823 if ( !m_numRows
|| !m_numCols
) return;
6825 int i
, numCells
= cells
.GetCount();
6826 int row
, col
, cell_rows
, cell_cols
;
6827 wxGridCellCoordsArray redrawCells
;
6829 for ( i
= numCells
-1; i
>= 0; i
-- )
6831 row
= cells
[i
].GetRow();
6832 col
= cells
[i
].GetCol();
6833 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6835 // If this cell is part of a multicell block, find owner for repaint
6836 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6838 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6839 bool marked
= false;
6840 for ( int j
= 0; j
< numCells
; j
++ )
6842 if ( cell
== cells
[j
] )
6850 int count
= redrawCells
.GetCount();
6851 for (int j
= 0; j
< count
; j
++)
6853 if ( cell
== redrawCells
[j
] )
6859 if (!marked
) redrawCells
.Add( cell
);
6861 continue; // don't bother drawing this cell
6864 // If this cell is empty, find cell to left that might want to overflow
6865 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6867 for ( int l
= 0; l
< cell_rows
; l
++ )
6869 // find a cell in this row to left alreay marked for repaint
6871 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6872 if ((redrawCells
[k
].GetCol() < left
) &&
6873 (redrawCells
[k
].GetRow() == row
))
6874 left
=redrawCells
[k
].GetCol();
6876 if (left
== col
) left
= 0; // oh well
6878 for (int j
= col
-1; j
>= left
; j
--)
6880 if (!m_table
->IsEmptyCell(row
+l
, j
))
6882 if (GetCellOverflow(row
+l
, j
))
6884 wxGridCellCoords
cell(row
+l
, j
);
6885 bool marked
= false;
6887 for (int k
= 0; k
< numCells
; k
++)
6889 if ( cell
== cells
[k
] )
6897 int count
= redrawCells
.GetCount();
6898 for (int k
= 0; k
< count
; k
++)
6900 if ( cell
== redrawCells
[k
] )
6906 if (!marked
) redrawCells
.Add( cell
);
6914 DrawCell( dc
, cells
[i
] );
6917 numCells
= redrawCells
.GetCount();
6919 for ( i
= numCells
- 1; i
>= 0; i
-- )
6921 DrawCell( dc
, redrawCells
[i
] );
6926 void wxGrid::DrawGridSpace( wxDC
& dc
)
6929 m_gridWin
->GetClientSize( &cw
, &ch
);
6932 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6934 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6935 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6937 if ( right
> rightCol
|| bottom
> bottomRow
)
6940 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6942 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6943 dc
.SetPen( *wxTRANSPARENT_PEN
);
6945 if ( right
> rightCol
)
6947 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6950 if ( bottom
> bottomRow
)
6952 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6958 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6960 int row
= coords
.GetRow();
6961 int col
= coords
.GetCol();
6963 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6966 // we draw the cell border ourselves
6967 #if !WXGRID_DRAW_LINES
6968 if ( m_gridLinesEnabled
)
6969 DrawCellBorder( dc
, coords
);
6972 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6974 bool isCurrent
= coords
== m_currentCellCoords
;
6976 wxRect rect
= CellToRect( row
, col
);
6978 // if the editor is shown, we should use it and not the renderer
6979 // Note: However, only if it is really _shown_, i.e. not hidden!
6980 if ( isCurrent
&& IsCellEditControlShown() )
6982 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6983 editor
->PaintBackground(rect
, attr
);
6988 // but all the rest is drawn by the cell renderer and hence may be
6990 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6991 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6998 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
7000 int row
= m_currentCellCoords
.GetRow();
7001 int col
= m_currentCellCoords
.GetCol();
7003 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7006 wxRect rect
= CellToRect(row
, col
);
7008 // hmmm... what could we do here to show that the cell is disabled?
7009 // for now, I just draw a thinner border than for the other ones, but
7010 // it doesn't look really good
7012 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
7016 // The center of th drawn line is where the position/width/height of
7017 // the rectangle is actually at, (on wxMSW atr least,) so we will
7018 // reduce the size of the rectangle to compensate for the thickness of
7019 // the line. If this is too strange on non wxMSW platforms then
7020 // please #ifdef this appropriately.
7021 rect
.x
+= penWidth
/2;
7022 rect
.y
+= penWidth
/2;
7023 rect
.width
-= penWidth
-1;
7024 rect
.height
-= penWidth
-1;
7027 // Now draw the rectangle
7028 // use the cellHighlightColour if the cell is inside a selection, this
7029 // will ensure the cell is always visible.
7030 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7031 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7032 dc
.DrawRectangle(rect
);
7036 // VZ: my experiments with 3d borders...
7038 // how to properly set colours for arbitrary bg?
7039 wxCoord x1
= rect
.x
,
7041 x2
= rect
.x
+ rect
.width
-1,
7042 y2
= rect
.y
+ rect
.height
-1;
7044 dc
.SetPen(*wxWHITE_PEN
);
7045 dc
.DrawLine(x1
, y1
, x2
, y1
);
7046 dc
.DrawLine(x1
, y1
, x1
, y2
);
7048 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7049 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7051 dc
.SetPen(*wxBLACK_PEN
);
7052 dc
.DrawLine(x1
, y2
, x2
, y2
);
7053 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7058 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7060 int row
= coords
.GetRow();
7061 int col
= coords
.GetCol();
7062 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7065 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7067 wxRect rect
= CellToRect( row
, col
);
7069 // right hand border
7071 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7072 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7076 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7077 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7080 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7082 // This if block was previously in wxGrid::OnPaint but that doesn't
7083 // seem to get called under wxGTK - MB
7085 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7086 m_numRows
&& m_numCols
)
7088 m_currentCellCoords
.Set(0, 0);
7091 if ( IsCellEditControlShown() )
7093 // don't show highlight when the edit control is shown
7097 // if the active cell was repainted, repaint its highlight too because it
7098 // might have been damaged by the grid lines
7099 size_t count
= cells
.GetCount();
7100 for ( size_t n
= 0; n
< count
; n
++ )
7102 if ( cells
[n
] == m_currentCellCoords
)
7104 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7105 DrawCellHighlight(dc
, attr
);
7113 // TODO: remove this ???
7114 // This is used to redraw all grid lines e.g. when the grid line colour
7117 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7119 #if !WXGRID_DRAW_LINES
7123 if ( !m_gridLinesEnabled
||
7125 !m_numCols
) return;
7127 int top
, bottom
, left
, right
;
7129 #if 0 //#ifndef __WXGTK__
7133 m_gridWin
->GetClientSize(&cw
, &ch
);
7135 // virtual coords of visible area
7137 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7138 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7143 reg
.GetBox(x
, y
, w
, h
);
7144 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7145 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7149 m_gridWin
->GetClientSize(&cw
, &ch
);
7150 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7151 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7154 // avoid drawing grid lines past the last row and col
7156 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7157 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7159 // no gridlines inside multicells, clip them out
7160 int leftCol
= internalXToCol(left
);
7161 int topRow
= internalYToRow(top
);
7162 int rightCol
= internalXToCol(right
);
7163 int bottomRow
= internalYToRow(bottom
);
7166 // CS: I don't know why suddenly unscrolled coordinates are used for clipping
7167 wxRegion
clippedcells(0, 0, cw
, ch
);
7169 int i
, j
, cell_rows
, cell_cols
;
7172 for (j
=topRow
; j
<bottomRow
; j
++)
7174 for (i
=leftCol
; i
<rightCol
; i
++)
7176 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7177 if ((cell_rows
> 1) || (cell_cols
> 1))
7179 rect
= CellToRect(j
,i
);
7180 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7181 clippedcells
.Subtract(rect
);
7183 else if ((cell_rows
< 0) || (cell_cols
< 0))
7185 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7186 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7187 clippedcells
.Subtract(rect
);
7192 wxRegion
clippedcells( left
, top
, right
- left
, bottom
- top
);
7194 int i
, j
, cell_rows
, cell_cols
;
7197 for (j
=topRow
; j
<bottomRow
; j
++)
7199 for (i
=leftCol
; i
<rightCol
; i
++)
7201 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7202 if ((cell_rows
> 1) || (cell_cols
> 1))
7204 rect
= CellToRect(j
,i
);
7205 clippedcells
.Subtract(rect
);
7207 else if ((cell_rows
< 0) || (cell_cols
< 0))
7209 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7210 clippedcells
.Subtract(rect
);
7215 dc
.SetClippingRegion( clippedcells
);
7217 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7219 // horizontal grid lines
7221 // already declared above - int i;
7222 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7224 int bot
= GetRowBottom(i
) - 1;
7233 dc
.DrawLine( left
, bot
, right
, bot
);
7238 // vertical grid lines
7240 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7242 int colRight
= GetColRight(i
) - 1;
7243 if ( colRight
> right
)
7248 if ( colRight
>= left
)
7250 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7253 dc
.DestroyClippingRegion();
7257 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7259 if ( !m_numRows
) return;
7262 size_t numLabels
= rows
.GetCount();
7264 for ( i
= 0; i
< numLabels
; i
++ )
7266 DrawRowLabel( dc
, rows
[i
] );
7271 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7273 if ( GetRowHeight(row
) <= 0 || m_rowLabelWidth
<= 0 )
7279 rect
.SetY( GetRowTop(row
) + 1 );
7280 rect
.SetWidth( m_rowLabelWidth
- 2 );
7281 rect
.SetHeight( GetRowHeight(row
) - 2 );
7283 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7285 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7287 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7289 int rowTop
= GetRowTop(row
),
7290 rowBottom
= GetRowBottom(row
) - 1;
7292 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7293 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7294 m_rowLabelWidth
-1, rowBottom
);
7296 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7298 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7300 dc
.SetPen( *wxWHITE_PEN
);
7301 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7302 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7304 dc
.SetBackgroundMode( wxTRANSPARENT
);
7305 dc
.SetTextForeground( GetLabelTextColour() );
7306 dc
.SetFont( GetLabelFont() );
7309 GetRowLabelAlignment( &hAlign
, &vAlign
);
7312 rect
.SetY( GetRowTop(row
) + 2 );
7313 rect
.SetWidth( m_rowLabelWidth
- 4 );
7314 rect
.SetHeight( GetRowHeight(row
) - 4 );
7315 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7319 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7321 if ( !m_numCols
) return;
7324 size_t numLabels
= cols
.GetCount();
7326 for ( i
= 0; i
< numLabels
; i
++ )
7328 DrawColLabel( dc
, cols
[i
] );
7333 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7335 if ( GetColWidth(col
) <= 0 || m_colLabelHeight
<= 0 )
7338 int colLeft
= GetColLeft(col
);
7342 rect
.SetX( colLeft
+ 1 );
7344 rect
.SetWidth( GetColWidth(col
) - 2 );
7345 rect
.SetHeight( m_colLabelHeight
- 2 );
7347 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7349 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7351 int colRight
= GetColRight(col
) - 1;
7353 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7354 dc
.DrawLine( colRight
, 0,
7355 colRight
, m_colLabelHeight
-1 );
7357 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7359 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7360 colRight
+1, m_colLabelHeight
-1 );
7362 dc
.SetPen( *wxWHITE_PEN
);
7363 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7364 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7366 dc
.SetBackgroundMode( wxTRANSPARENT
);
7367 dc
.SetTextForeground( GetLabelTextColour() );
7368 dc
.SetFont( GetLabelFont() );
7370 int hAlign
, vAlign
, orient
;
7371 GetColLabelAlignment( &hAlign
, &vAlign
);
7372 orient
= GetColLabelTextOrientation();
7374 rect
.SetX( colLeft
+ 2 );
7376 rect
.SetWidth( GetColWidth(col
) - 4 );
7377 rect
.SetHeight( m_colLabelHeight
- 4 );
7378 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7381 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7382 const wxString
& value
,
7386 int textOrientation
)
7388 wxArrayString lines
;
7390 StringToLines( value
, lines
);
7393 //Forward to new API.
7394 DrawTextRectangle( dc
,
7403 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7404 const wxArrayString
& lines
,
7408 int textOrientation
)
7410 long textWidth
, textHeight
;
7411 long lineWidth
, lineHeight
;
7414 dc
.SetClippingRegion( rect
);
7416 nLines
= lines
.GetCount();
7420 float x
= 0.0, y
= 0.0;
7422 if( textOrientation
== wxHORIZONTAL
)
7423 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7425 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7429 case wxALIGN_BOTTOM
:
7430 if( textOrientation
== wxHORIZONTAL
)
7431 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7433 x
= rect
.x
+ rect
.width
- textWidth
;
7436 case wxALIGN_CENTRE
:
7437 if( textOrientation
== wxHORIZONTAL
)
7438 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7440 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7445 if( textOrientation
== wxHORIZONTAL
)
7452 // Align each line of a multi-line label
7453 for( l
= 0; l
< nLines
; l
++ )
7455 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7457 switch( horizAlign
)
7460 if( textOrientation
== wxHORIZONTAL
)
7461 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7463 y
= rect
.y
+ lineWidth
+ 1;
7466 case wxALIGN_CENTRE
:
7467 if( textOrientation
== wxHORIZONTAL
)
7468 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7470 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7475 if( textOrientation
== wxHORIZONTAL
)
7478 y
= rect
.y
+ rect
.height
- 1;
7482 if( textOrientation
== wxHORIZONTAL
)
7484 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7489 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7494 dc
.DestroyClippingRegion();
7498 // Split multi line text up into an array of strings. Any existing
7499 // contents of the string array are preserved.
7501 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7505 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7506 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7508 while ( startPos
< (int)tVal
.Length() )
7510 pos
= tVal
.Mid(startPos
).Find( eol
);
7515 else if ( pos
== 0 )
7517 lines
.Add( wxEmptyString
);
7521 lines
.Add( value
.Mid(startPos
, pos
) );
7525 if ( startPos
< (int)value
.Length() )
7527 lines
.Add( value
.Mid( startPos
) );
7532 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7533 const wxArrayString
& lines
,
7534 long *width
, long *height
)
7541 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7543 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7544 w
= wxMax( w
, lineW
);
7553 // ------ Batch processing.
7555 void wxGrid::EndBatch()
7557 if ( m_batchCount
> 0 )
7560 if ( !m_batchCount
)
7563 m_rowLabelWin
->Refresh();
7564 m_colLabelWin
->Refresh();
7565 m_cornerLabelWin
->Refresh();
7566 m_gridWin
->Refresh();
7571 // Use this, rather than wxWindow::Refresh(), to force an immediate
7572 // repainting of the grid. Has no effect if you are already inside a
7573 // BeginBatch / EndBatch block.
7575 void wxGrid::ForceRefresh()
7581 bool wxGrid::Enable(bool enable
)
7583 if ( !wxScrolledWindow::Enable(enable
) )
7586 // redraw in the new state
7587 m_gridWin
->Refresh();
7593 // ------ Edit control functions
7597 void wxGrid::EnableEditing( bool edit
)
7599 // TODO: improve this ?
7601 if ( edit
!= m_editable
)
7603 if(!edit
) EnableCellEditControl(edit
);
7609 void wxGrid::EnableCellEditControl( bool enable
)
7614 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7615 SetCurrentCell( 0, 0 );
7617 if ( enable
!= m_cellEditCtrlEnabled
)
7621 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7624 // this should be checked by the caller!
7625 wxASSERT_MSG( CanEnableCellControl(),
7626 _T("can't enable editing for this cell!") );
7628 // do it before ShowCellEditControl()
7629 m_cellEditCtrlEnabled
= enable
;
7631 ShowCellEditControl();
7635 //FIXME:add veto support
7636 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7638 HideCellEditControl();
7639 SaveEditControlValue();
7641 // do it after HideCellEditControl()
7642 m_cellEditCtrlEnabled
= enable
;
7647 bool wxGrid::IsCurrentCellReadOnly() const
7650 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7651 bool readonly
= attr
->IsReadOnly();
7657 bool wxGrid::CanEnableCellControl() const
7659 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7660 !IsCurrentCellReadOnly();
7664 bool wxGrid::IsCellEditControlEnabled() const
7666 // the cell edit control might be disable for all cells or just for the
7667 // current one if it's read only
7668 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7671 bool wxGrid::IsCellEditControlShown() const
7673 bool isShown
= false;
7675 if ( m_cellEditCtrlEnabled
)
7677 int row
= m_currentCellCoords
.GetRow();
7678 int col
= m_currentCellCoords
.GetCol();
7679 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7680 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7685 if ( editor
->IsCreated() )
7687 isShown
= editor
->GetControl()->IsShown();
7697 void wxGrid::ShowCellEditControl()
7699 if ( IsCellEditControlEnabled() )
7701 if ( !IsVisible( m_currentCellCoords
) )
7703 m_cellEditCtrlEnabled
= false;
7708 wxRect rect
= CellToRect( m_currentCellCoords
);
7709 int row
= m_currentCellCoords
.GetRow();
7710 int col
= m_currentCellCoords
.GetCol();
7712 // if this is part of a multicell, find owner (topleft)
7713 int cell_rows
, cell_cols
;
7714 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7715 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7719 m_currentCellCoords
.SetRow( row
);
7720 m_currentCellCoords
.SetCol( col
);
7723 // convert to scrolled coords
7725 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7727 // done in PaintBackground()
7729 // erase the highlight and the cell contents because the editor
7730 // might not cover the entire cell
7731 wxClientDC
dc( m_gridWin
);
7733 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7734 dc
.SetPen(*wxTRANSPARENT_PEN
);
7735 dc
.DrawRectangle(rect
);
7738 // cell is shifted by one pixel
7739 // However, don't allow x or y to become negative
7740 // since the SetSize() method interprets that as
7747 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7748 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7749 if ( !editor
->IsCreated() )
7751 editor
->Create(m_gridWin
, wxID_ANY
,
7752 new wxGridCellEditorEvtHandler(this, editor
));
7754 wxGridEditorCreatedEvent
evt(GetId(),
7755 wxEVT_GRID_EDITOR_CREATED
,
7759 editor
->GetControl());
7760 GetEventHandler()->ProcessEvent(evt
);
7764 // resize editor to overflow into righthand cells if allowed
7765 int maxWidth
= rect
.width
;
7766 wxString value
= GetCellValue(row
, col
);
7767 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7770 GetTextExtent(value
, &maxWidth
, &y
,
7771 NULL
, NULL
, &attr
->GetFont());
7772 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7774 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7775 if (rect
.x
+maxWidth
> client_right
)
7776 maxWidth
= client_right
- rect
.x
;
7778 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7780 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7781 // may have changed earlier
7782 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7785 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7786 // looks weird going over a multicell
7787 if (m_table
->IsEmptyCell(row
,i
) &&
7788 (rect
.width
< maxWidth
) && (c_rows
== 1))
7789 rect
.width
+= GetColWidth(i
);
7793 if (rect
.GetRight() > client_right
)
7794 rect
.SetRight(client_right
-1);
7797 editor
->SetCellAttr(attr
);
7798 editor
->SetSize( rect
);
7799 editor
->Show( true, attr
);
7801 // recalc dimensions in case we need to
7802 // expand the scrolled window to account for editor
7805 editor
->BeginEdit(row
, col
, this);
7806 editor
->SetCellAttr(NULL
);
7815 void wxGrid::HideCellEditControl()
7817 if ( IsCellEditControlEnabled() )
7819 int row
= m_currentCellCoords
.GetRow();
7820 int col
= m_currentCellCoords
.GetCol();
7822 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7823 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7824 editor
->Show( false );
7827 m_gridWin
->SetFocus();
7828 // refresh whole row to the right
7829 wxRect
rect( CellToRect(row
, col
) );
7830 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7831 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7833 // ensure that the pixels under the focus ring get refreshed as well
7834 rect
.Inflate(10,10);
7836 m_gridWin
->Refresh( false, &rect
);
7841 void wxGrid::SaveEditControlValue()
7843 if ( IsCellEditControlEnabled() )
7845 int row
= m_currentCellCoords
.GetRow();
7846 int col
= m_currentCellCoords
.GetCol();
7848 wxString oldval
= GetCellValue(row
,col
);
7850 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7851 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7852 bool changed
= editor
->EndEdit(row
, col
, this);
7859 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7860 m_currentCellCoords
.GetRow(),
7861 m_currentCellCoords
.GetCol() ) < 0 ) {
7863 // Event has been vetoed, set the data back.
7864 SetCellValue(row
,col
,oldval
);
7872 // ------ Grid location functions
7873 // Note that all of these functions work with the logical coordinates of
7874 // grid cells and labels so you will need to convert from device
7875 // coordinates for mouse events etc.
7878 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7880 int row
= YToRow(y
);
7881 int col
= XToCol(x
);
7883 if ( row
== -1 || col
== -1 )
7885 coords
= wxGridNoCellCoords
;
7889 coords
.Set( row
, col
);
7894 // Internal Helper function for computing row or column from some
7895 // (unscrolled) coordinate value, using either
7896 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7897 // of m_rowBottoms/m_ColRights to speed up the search!
7899 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7900 const wxArrayInt
& BorderArray
, int nMax
,
7905 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7911 size_t i_max
= coord
/ defaultDist
,
7914 if (BorderArray
.IsEmpty())
7916 if((int) i_max
< nMax
)
7918 return clipToMinMax
? nMax
- 1 : -1;
7921 if ( i_max
>= BorderArray
.GetCount())
7922 i_max
= BorderArray
.GetCount() - 1;
7925 if ( coord
>= BorderArray
[i_max
])
7929 i_max
= coord
/ minDist
;
7931 i_max
= BorderArray
.GetCount() - 1;
7933 if ( i_max
>= BorderArray
.GetCount())
7934 i_max
= BorderArray
.GetCount() - 1;
7936 if ( coord
>= BorderArray
[i_max
])
7937 return clipToMinMax
? (int)i_max
: -1;
7938 if ( coord
< BorderArray
[0] )
7941 while ( i_max
- i_min
> 0 )
7943 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7944 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7945 if (coord
>= BorderArray
[ i_max
- 1])
7949 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7950 if (coord
< BorderArray
[median
])
7958 int wxGrid::YToRow( int y
)
7960 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7961 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7965 int wxGrid::XToCol( int x
)
7967 return CoordToRowOrCol(x
, m_defaultColWidth
,
7968 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7972 // return the row number that that the y coord is near the edge of, or
7973 // -1 if not near an edge
7975 int wxGrid::YToEdgeOfRow( int y
)
7978 i
= internalYToRow(y
);
7980 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7982 // We know that we are in row i, test whether we are
7983 // close enough to lower or upper border, respectively.
7984 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7986 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7994 // return the col number that that the x coord is near the edge of, or
7995 // -1 if not near an edge
7997 int wxGrid::XToEdgeOfCol( int x
)
8000 i
= internalXToCol(x
);
8002 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
8004 // We know that we are in column i, test whether we are
8005 // close enough to right or left border, respectively.
8006 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
8008 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
8016 wxRect
wxGrid::CellToRect( int row
, int col
)
8018 wxRect
rect( -1, -1, -1, -1 );
8020 if ( row
>= 0 && row
< m_numRows
&&
8021 col
>= 0 && col
< m_numCols
)
8023 int i
, cell_rows
, cell_cols
;
8024 rect
.width
= rect
.height
= 0;
8025 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8026 // if negative then find multicell owner
8027 if (cell_rows
< 0) row
+= cell_rows
;
8028 if (cell_cols
< 0) col
+= cell_cols
;
8029 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
8031 rect
.x
= GetColLeft(col
);
8032 rect
.y
= GetRowTop(row
);
8033 for (i
=col
; i
<col
+cell_cols
; i
++)
8034 rect
.width
+= GetColWidth(i
);
8035 for (i
=row
; i
<row
+cell_rows
; i
++)
8036 rect
.height
+= GetRowHeight(i
);
8039 // if grid lines are enabled, then the area of the cell is a bit smaller
8040 if (m_gridLinesEnabled
) {
8048 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
8050 // get the cell rectangle in logical coords
8052 wxRect
r( CellToRect( row
, col
) );
8054 // convert to device coords
8056 int left
, top
, right
, bottom
;
8057 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8058 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8060 // check against the client area of the grid window
8063 m_gridWin
->GetClientSize( &cw
, &ch
);
8065 if ( wholeCellVisible
)
8067 // is the cell wholly visible ?
8069 return ( left
>= 0 && right
<= cw
&&
8070 top
>= 0 && bottom
<= ch
);
8074 // is the cell partly visible ?
8076 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8077 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8082 // make the specified cell location visible by doing a minimal amount
8085 void wxGrid::MakeCellVisible( int row
, int col
)
8089 int xpos
= -1, ypos
= -1;
8091 if ( row
>= 0 && row
< m_numRows
&&
8092 col
>= 0 && col
< m_numCols
)
8094 // get the cell rectangle in logical coords
8096 wxRect
r( CellToRect( row
, col
) );
8098 // convert to device coords
8100 int left
, top
, right
, bottom
;
8101 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8102 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8105 m_gridWin
->GetClientSize( &cw
, &ch
);
8111 else if ( bottom
> ch
)
8113 int h
= r
.GetHeight();
8115 for ( i
= row
-1; i
>= 0; i
-- )
8117 int rowHeight
= GetRowHeight(i
);
8118 if ( h
+ rowHeight
> ch
)
8125 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8126 // have rounding errors (this is important, because if we do, we
8127 // might not scroll at all and some cells won't be redrawn)
8129 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8131 ypos
+= m_scrollLineY
;
8138 else if ( right
> cw
)
8140 // position the view so that the cell is on the right
8142 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8143 xpos
= x0
+ (right
- cw
);
8145 // see comment for ypos above
8146 xpos
+= m_scrollLineX
;
8149 if ( xpos
!= -1 || ypos
!= -1 )
8152 xpos
/= m_scrollLineX
;
8154 ypos
/= m_scrollLineY
;
8155 Scroll( xpos
, ypos
);
8163 // ------ Grid cursor movement functions
8166 bool wxGrid::MoveCursorUp( bool expandSelection
)
8168 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8169 m_currentCellCoords
.GetRow() >= 0 )
8171 if ( expandSelection
)
8173 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8174 m_selectingKeyboard
= m_currentCellCoords
;
8175 if ( m_selectingKeyboard
.GetRow() > 0 )
8177 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8178 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8179 m_selectingKeyboard
.GetCol() );
8180 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8183 else if ( m_currentCellCoords
.GetRow() > 0 )
8186 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8187 m_currentCellCoords
.GetCol() );
8188 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8189 m_currentCellCoords
.GetCol() );
8200 bool wxGrid::MoveCursorDown( bool expandSelection
)
8202 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8203 m_currentCellCoords
.GetRow() < m_numRows
)
8205 if ( expandSelection
)
8207 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8208 m_selectingKeyboard
= m_currentCellCoords
;
8209 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8211 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8212 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8213 m_selectingKeyboard
.GetCol() );
8214 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8217 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8220 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8221 m_currentCellCoords
.GetCol() );
8222 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8223 m_currentCellCoords
.GetCol() );
8234 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8236 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8237 m_currentCellCoords
.GetCol() >= 0 )
8239 if ( expandSelection
)
8241 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8242 m_selectingKeyboard
= m_currentCellCoords
;
8243 if ( m_selectingKeyboard
.GetCol() > 0 )
8245 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8246 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8247 m_selectingKeyboard
.GetCol() );
8248 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8251 else if ( m_currentCellCoords
.GetCol() > 0 )
8254 MakeCellVisible( m_currentCellCoords
.GetRow(),
8255 m_currentCellCoords
.GetCol() - 1 );
8256 SetCurrentCell( m_currentCellCoords
.GetRow(),
8257 m_currentCellCoords
.GetCol() - 1 );
8268 bool wxGrid::MoveCursorRight( bool expandSelection
)
8270 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8271 m_currentCellCoords
.GetCol() < m_numCols
)
8273 if ( expandSelection
)
8275 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8276 m_selectingKeyboard
= m_currentCellCoords
;
8277 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8279 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8280 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8281 m_selectingKeyboard
.GetCol() );
8282 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8285 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8288 MakeCellVisible( m_currentCellCoords
.GetRow(),
8289 m_currentCellCoords
.GetCol() + 1 );
8290 SetCurrentCell( m_currentCellCoords
.GetRow(),
8291 m_currentCellCoords
.GetCol() + 1 );
8302 bool wxGrid::MovePageUp()
8304 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8306 int row
= m_currentCellCoords
.GetRow();
8310 m_gridWin
->GetClientSize( &cw
, &ch
);
8312 int y
= GetRowTop(row
);
8313 int newRow
= internalYToRow( y
- ch
+ 1 );
8315 if ( newRow
== row
)
8317 //row > 0 , so newrow can never be less than 0 here.
8321 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8322 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8330 bool wxGrid::MovePageDown()
8332 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8334 int row
= m_currentCellCoords
.GetRow();
8335 if ( (row
+1) < m_numRows
)
8338 m_gridWin
->GetClientSize( &cw
, &ch
);
8340 int y
= GetRowTop(row
);
8341 int newRow
= internalYToRow( y
+ ch
);
8342 if ( newRow
== row
)
8344 // row < m_numRows , so newrow can't overflow here.
8348 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8349 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8357 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8360 m_currentCellCoords
!= wxGridNoCellCoords
&&
8361 m_currentCellCoords
.GetRow() > 0 )
8363 int row
= m_currentCellCoords
.GetRow();
8364 int col
= m_currentCellCoords
.GetCol();
8366 if ( m_table
->IsEmptyCell(row
, col
) )
8368 // starting in an empty cell: find the next block of
8374 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8377 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8379 // starting at the top of a block: find the next block
8385 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8390 // starting within a block: find the top of the block
8395 if ( m_table
->IsEmptyCell(row
, col
) )
8403 MakeCellVisible( row
, col
);
8404 if ( expandSelection
)
8406 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8407 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8412 SetCurrentCell( row
, col
);
8420 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8423 m_currentCellCoords
!= wxGridNoCellCoords
&&
8424 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8426 int row
= m_currentCellCoords
.GetRow();
8427 int col
= m_currentCellCoords
.GetCol();
8429 if ( m_table
->IsEmptyCell(row
, col
) )
8431 // starting in an empty cell: find the next block of
8434 while ( row
< m_numRows
-1 )
8437 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8440 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8442 // starting at the bottom of a block: find the next block
8445 while ( row
< m_numRows
-1 )
8448 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8453 // starting within a block: find the bottom of the block
8455 while ( row
< m_numRows
-1 )
8458 if ( m_table
->IsEmptyCell(row
, col
) )
8466 MakeCellVisible( row
, col
);
8467 if ( expandSelection
)
8469 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8470 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8475 SetCurrentCell( row
, col
);
8484 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8487 m_currentCellCoords
!= wxGridNoCellCoords
&&
8488 m_currentCellCoords
.GetCol() > 0 )
8490 int row
= m_currentCellCoords
.GetRow();
8491 int col
= m_currentCellCoords
.GetCol();
8493 if ( m_table
->IsEmptyCell(row
, col
) )
8495 // starting in an empty cell: find the next block of
8501 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8504 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8506 // starting at the left of a block: find the next block
8512 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8517 // starting within a block: find the left of the block
8522 if ( m_table
->IsEmptyCell(row
, col
) )
8530 MakeCellVisible( row
, col
);
8531 if ( expandSelection
)
8533 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8534 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8539 SetCurrentCell( row
, col
);
8548 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8551 m_currentCellCoords
!= wxGridNoCellCoords
&&
8552 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8554 int row
= m_currentCellCoords
.GetRow();
8555 int col
= m_currentCellCoords
.GetCol();
8557 if ( m_table
->IsEmptyCell(row
, col
) )
8559 // starting in an empty cell: find the next block of
8562 while ( col
< m_numCols
-1 )
8565 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8568 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8570 // starting at the right of a block: find the next block
8573 while ( col
< m_numCols
-1 )
8576 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8581 // starting within a block: find the right of the block
8583 while ( col
< m_numCols
-1 )
8586 if ( m_table
->IsEmptyCell(row
, col
) )
8594 MakeCellVisible( row
, col
);
8595 if ( expandSelection
)
8597 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8598 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8603 SetCurrentCell( row
, col
);
8615 // ------ Label values and formatting
8618 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8620 *horiz
= m_rowLabelHorizAlign
;
8621 *vert
= m_rowLabelVertAlign
;
8624 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8626 *horiz
= m_colLabelHorizAlign
;
8627 *vert
= m_colLabelVertAlign
;
8630 int wxGrid::GetColLabelTextOrientation()
8632 return m_colLabelTextOrientation
;
8635 wxString
wxGrid::GetRowLabelValue( int row
)
8639 return m_table
->GetRowLabelValue( row
);
8649 wxString
wxGrid::GetColLabelValue( int col
)
8653 return m_table
->GetColLabelValue( col
);
8664 void wxGrid::SetRowLabelSize( int width
)
8666 width
= wxMax( width
, 0 );
8667 if ( width
!= m_rowLabelWidth
)
8671 m_rowLabelWin
->Show( false );
8672 m_cornerLabelWin
->Show( false );
8674 else if ( m_rowLabelWidth
== 0 )
8676 m_rowLabelWin
->Show( true );
8677 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8680 m_rowLabelWidth
= width
;
8682 wxScrolledWindow::Refresh( true );
8687 void wxGrid::SetColLabelSize( int height
)
8689 height
= wxMax( height
, 0 );
8690 if ( height
!= m_colLabelHeight
)
8694 m_colLabelWin
->Show( false );
8695 m_cornerLabelWin
->Show( false );
8697 else if ( m_colLabelHeight
== 0 )
8699 m_colLabelWin
->Show( true );
8700 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8703 m_colLabelHeight
= height
;
8705 wxScrolledWindow::Refresh( true );
8710 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8712 if ( m_labelBackgroundColour
!= colour
)
8714 m_labelBackgroundColour
= colour
;
8715 m_rowLabelWin
->SetBackgroundColour( colour
);
8716 m_colLabelWin
->SetBackgroundColour( colour
);
8717 m_cornerLabelWin
->SetBackgroundColour( colour
);
8719 if ( !GetBatchCount() )
8721 m_rowLabelWin
->Refresh();
8722 m_colLabelWin
->Refresh();
8723 m_cornerLabelWin
->Refresh();
8728 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8730 if ( m_labelTextColour
!= colour
)
8732 m_labelTextColour
= colour
;
8733 if ( !GetBatchCount() )
8735 m_rowLabelWin
->Refresh();
8736 m_colLabelWin
->Refresh();
8741 void wxGrid::SetLabelFont( const wxFont
& font
)
8744 if ( !GetBatchCount() )
8746 m_rowLabelWin
->Refresh();
8747 m_colLabelWin
->Refresh();
8751 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8753 // allow old (incorrect) defs to be used
8756 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8757 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8758 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8763 case wxTOP
: vert
= wxALIGN_TOP
; break;
8764 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8765 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8768 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8770 m_rowLabelHorizAlign
= horiz
;
8773 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8775 m_rowLabelVertAlign
= vert
;
8778 if ( !GetBatchCount() )
8780 m_rowLabelWin
->Refresh();
8784 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8786 // allow old (incorrect) defs to be used
8789 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8790 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8791 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8796 case wxTOP
: vert
= wxALIGN_TOP
; break;
8797 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8798 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8801 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8803 m_colLabelHorizAlign
= horiz
;
8806 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8808 m_colLabelVertAlign
= vert
;
8811 if ( !GetBatchCount() )
8813 m_colLabelWin
->Refresh();
8817 // Note: under MSW, the default column label font must be changed because it
8818 // does not support vertical printing
8820 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8821 // pGrid->SetLabelFont(font);
8822 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8824 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8826 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8828 m_colLabelTextOrientation
= textOrientation
;
8831 if ( !GetBatchCount() )
8833 m_colLabelWin
->Refresh();
8837 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8841 m_table
->SetRowLabelValue( row
, s
);
8842 if ( !GetBatchCount() )
8844 wxRect rect
= CellToRect( row
, 0);
8845 if ( rect
.height
> 0 )
8847 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8849 rect
.width
= m_rowLabelWidth
;
8850 m_rowLabelWin
->Refresh( true, &rect
);
8856 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8860 m_table
->SetColLabelValue( col
, s
);
8861 if ( !GetBatchCount() )
8863 wxRect rect
= CellToRect( 0, col
);
8864 if ( rect
.width
> 0 )
8866 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8868 rect
.height
= m_colLabelHeight
;
8869 m_colLabelWin
->Refresh( true, &rect
);
8875 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8877 if ( m_gridLineColour
!= colour
)
8879 m_gridLineColour
= colour
;
8881 wxClientDC
dc( m_gridWin
);
8883 DrawAllGridLines( dc
, wxRegion() );
8888 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8890 if ( m_cellHighlightColour
!= colour
)
8892 m_cellHighlightColour
= colour
;
8894 wxClientDC
dc( m_gridWin
);
8896 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8897 DrawCellHighlight(dc
, attr
);
8902 void wxGrid::SetCellHighlightPenWidth(int width
)
8904 if (m_cellHighlightPenWidth
!= width
) {
8905 m_cellHighlightPenWidth
= width
;
8907 // Just redrawing the cell highlight is not enough since that won't
8908 // make any visible change if the the thickness is getting smaller.
8909 int row
= m_currentCellCoords
.GetRow();
8910 int col
= m_currentCellCoords
.GetCol();
8911 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8913 wxRect rect
= CellToRect(row
, col
);
8914 m_gridWin
->Refresh(true, &rect
);
8918 void wxGrid::SetCellHighlightROPenWidth(int width
)
8920 if (m_cellHighlightROPenWidth
!= width
) {
8921 m_cellHighlightROPenWidth
= width
;
8923 // Just redrawing the cell highlight is not enough since that won't
8924 // make any visible change if the the thickness is getting smaller.
8925 int row
= m_currentCellCoords
.GetRow();
8926 int col
= m_currentCellCoords
.GetCol();
8927 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8929 wxRect rect
= CellToRect(row
, col
);
8930 m_gridWin
->Refresh(true, &rect
);
8934 void wxGrid::EnableGridLines( bool enable
)
8936 if ( enable
!= m_gridLinesEnabled
)
8938 m_gridLinesEnabled
= enable
;
8940 if ( !GetBatchCount() )
8944 wxClientDC
dc( m_gridWin
);
8946 DrawAllGridLines( dc
, wxRegion() );
8950 m_gridWin
->Refresh();
8957 int wxGrid::GetDefaultRowSize()
8959 return m_defaultRowHeight
;
8962 int wxGrid::GetRowSize( int row
)
8964 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8966 return GetRowHeight(row
);
8969 int wxGrid::GetDefaultColSize()
8971 return m_defaultColWidth
;
8974 int wxGrid::GetColSize( int col
)
8976 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8978 return GetColWidth(col
);
8981 // ============================================================================
8982 // access to the grid attributes: each of them has a default value in the grid
8983 // itself and may be overidden on a per-cell basis
8984 // ============================================================================
8986 // ----------------------------------------------------------------------------
8987 // setting default attributes
8988 // ----------------------------------------------------------------------------
8990 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8992 m_defaultCellAttr
->SetBackgroundColour(col
);
8994 m_gridWin
->SetBackgroundColour(col
);
8998 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
9000 m_defaultCellAttr
->SetTextColour(col
);
9003 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
9005 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
9008 void wxGrid::SetDefaultCellOverflow( bool allow
)
9010 m_defaultCellAttr
->SetOverflow(allow
);
9013 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
9015 m_defaultCellAttr
->SetFont(font
);
9019 // For editors and renderers the type registry takes precedence over the
9020 // default attr, so we need to register the new editor/renderer for the string
9021 // data type in order to make setting a default editor/renderer appear to
9024 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
9026 RegisterDataType(wxGRID_VALUE_STRING
,
9028 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
9031 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
9033 RegisterDataType(wxGRID_VALUE_STRING
,
9034 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
9038 // ----------------------------------------------------------------------------
9039 // access to the default attrbiutes
9040 // ----------------------------------------------------------------------------
9042 wxColour
wxGrid::GetDefaultCellBackgroundColour()
9044 return m_defaultCellAttr
->GetBackgroundColour();
9047 wxColour
wxGrid::GetDefaultCellTextColour()
9049 return m_defaultCellAttr
->GetTextColour();
9052 wxFont
wxGrid::GetDefaultCellFont()
9054 return m_defaultCellAttr
->GetFont();
9057 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
9059 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
9062 bool wxGrid::GetDefaultCellOverflow()
9064 return m_defaultCellAttr
->GetOverflow();
9067 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
9069 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9072 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9074 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9077 // ----------------------------------------------------------------------------
9078 // access to cell attributes
9079 // ----------------------------------------------------------------------------
9081 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9083 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9084 wxColour colour
= attr
->GetBackgroundColour();
9089 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9091 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9092 wxColour colour
= attr
->GetTextColour();
9097 wxFont
wxGrid::GetCellFont( int row
, int col
)
9099 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9100 wxFont font
= attr
->GetFont();
9105 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9107 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9108 attr
->GetAlignment(horiz
, vert
);
9112 bool wxGrid::GetCellOverflow( int row
, int col
)
9114 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9115 bool allow
= attr
->GetOverflow();
9120 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9122 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9123 attr
->GetSize( num_rows
, num_cols
);
9127 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9129 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9130 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9136 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9138 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9139 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9145 bool wxGrid::IsReadOnly(int row
, int col
) const
9147 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9148 bool isReadOnly
= attr
->IsReadOnly();
9153 // ----------------------------------------------------------------------------
9154 // attribute support: cache, automatic provider creation, ...
9155 // ----------------------------------------------------------------------------
9157 bool wxGrid::CanHaveAttributes()
9164 return m_table
->CanHaveAttributes();
9167 void wxGrid::ClearAttrCache()
9169 if ( m_attrCache
.row
!= -1 )
9171 wxSafeDecRef(m_attrCache
.attr
);
9172 m_attrCache
.attr
= NULL
;
9173 m_attrCache
.row
= -1;
9177 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9181 wxGrid
*self
= (wxGrid
*)this; // const_cast
9183 self
->ClearAttrCache();
9184 self
->m_attrCache
.row
= row
;
9185 self
->m_attrCache
.col
= col
;
9186 self
->m_attrCache
.attr
= attr
;
9191 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9193 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9195 *attr
= m_attrCache
.attr
;
9196 wxSafeIncRef(m_attrCache
.attr
);
9198 #ifdef DEBUG_ATTR_CACHE
9199 gs_nAttrCacheHits
++;
9206 #ifdef DEBUG_ATTR_CACHE
9207 gs_nAttrCacheMisses
++;
9213 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9215 wxGridCellAttr
*attr
= NULL
;
9216 // Additional test to avoid looking at the cache e.g. for
9217 // wxNoCellCoords, as this will confuse memory management.
9220 if ( !LookupAttr(row
, col
, &attr
) )
9222 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9223 : (wxGridCellAttr
*)NULL
;
9224 CacheAttr(row
, col
, attr
);
9229 attr
->SetDefAttr(m_defaultCellAttr
);
9233 attr
= m_defaultCellAttr
;
9240 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9242 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9243 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9245 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9246 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9248 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9251 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9253 // artificially inc the ref count to match DecRef() in caller
9255 m_table
->SetAttr(attr
, row
, col
);
9261 // ----------------------------------------------------------------------------
9262 // setting column attributes (wrappers around SetColAttr)
9263 // ----------------------------------------------------------------------------
9265 void wxGrid::SetColFormatBool(int col
)
9267 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9270 void wxGrid::SetColFormatNumber(int col
)
9272 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9275 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9277 wxString typeName
= wxGRID_VALUE_FLOAT
;
9278 if ( (width
!= -1) || (precision
!= -1) )
9280 typeName
<< _T(':') << width
<< _T(',') << precision
;
9283 SetColFormatCustom(col
, typeName
);
9286 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9288 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9290 attr
= new wxGridCellAttr
;
9291 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9292 attr
->SetRenderer(renderer
);
9294 SetColAttr(col
, attr
);
9298 // ----------------------------------------------------------------------------
9299 // setting cell attributes: this is forwarded to the table
9300 // ----------------------------------------------------------------------------
9302 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9304 if ( CanHaveAttributes() )
9306 m_table
->SetAttr(attr
, row
, col
);
9315 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9317 if ( CanHaveAttributes() )
9319 m_table
->SetRowAttr(attr
, row
);
9328 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9330 if ( CanHaveAttributes() )
9332 m_table
->SetColAttr(attr
, col
);
9341 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9343 if ( CanHaveAttributes() )
9345 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9346 attr
->SetBackgroundColour(colour
);
9351 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9353 if ( CanHaveAttributes() )
9355 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9356 attr
->SetTextColour(colour
);
9361 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9363 if ( CanHaveAttributes() )
9365 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9366 attr
->SetFont(font
);
9371 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9373 if ( CanHaveAttributes() )
9375 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9376 attr
->SetAlignment(horiz
, vert
);
9381 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9383 if ( CanHaveAttributes() )
9385 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9386 attr
->SetOverflow(allow
);
9391 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9393 if ( CanHaveAttributes() )
9395 int cell_rows
, cell_cols
;
9397 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9398 attr
->GetSize(&cell_rows
, &cell_cols
);
9399 attr
->SetSize(num_rows
, num_cols
);
9402 // Cannot set the size of a cell to 0 or negative values
9403 // While it is perfectly legal to do that, this function cannot
9404 // handle all the possibilies, do it by hand by getting the CellAttr.
9405 // You can only set the size of a cell to 1,1 or greater with this fn
9406 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9407 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9408 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9409 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9411 // if this was already a multicell then "turn off" the other cells first
9412 if ((cell_rows
> 1) || (cell_rows
> 1))
9415 for (j
=row
; j
<row
+cell_rows
; j
++)
9417 for (i
=col
; i
<col
+cell_cols
; i
++)
9419 if ((i
!= col
) || (j
!= row
))
9421 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9422 attr_stub
->SetSize( 1, 1 );
9423 attr_stub
->DecRef();
9429 // mark the cells that will be covered by this cell to
9430 // negative or zero values to point back at this cell
9431 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9434 for (j
=row
; j
<row
+num_rows
; j
++)
9436 for (i
=col
; i
<col
+num_cols
; i
++)
9438 if ((i
!= col
) || (j
!= row
))
9440 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9441 attr_stub
->SetSize( row
-j
, col
-i
);
9442 attr_stub
->DecRef();
9450 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9452 if ( CanHaveAttributes() )
9454 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9455 attr
->SetRenderer(renderer
);
9460 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9462 if ( CanHaveAttributes() )
9464 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9465 attr
->SetEditor(editor
);
9470 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9472 if ( CanHaveAttributes() )
9474 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9475 attr
->SetReadOnly(isReadOnly
);
9480 // ----------------------------------------------------------------------------
9481 // Data type registration
9482 // ----------------------------------------------------------------------------
9484 void wxGrid::RegisterDataType(const wxString
& typeName
,
9485 wxGridCellRenderer
* renderer
,
9486 wxGridCellEditor
* editor
)
9488 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9492 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9494 wxString typeName
= m_table
->GetTypeName(row
, col
);
9495 return GetDefaultEditorForType(typeName
);
9498 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9500 wxString typeName
= m_table
->GetTypeName(row
, col
);
9501 return GetDefaultRendererForType(typeName
);
9505 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9507 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9508 if ( index
== wxNOT_FOUND
)
9510 wxFAIL_MSG(wxT("Unknown data type name"));
9515 return m_typeRegistry
->GetEditor(index
);
9519 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9521 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9522 if ( index
== wxNOT_FOUND
)
9524 wxFAIL_MSG(wxT("Unknown data type name"));
9529 return m_typeRegistry
->GetRenderer(index
);
9533 // ----------------------------------------------------------------------------
9535 // ----------------------------------------------------------------------------
9537 void wxGrid::EnableDragRowSize( bool enable
)
9539 m_canDragRowSize
= enable
;
9543 void wxGrid::EnableDragColSize( bool enable
)
9545 m_canDragColSize
= enable
;
9548 void wxGrid::EnableDragGridSize( bool enable
)
9550 m_canDragGridSize
= enable
;
9553 void wxGrid::EnableDragCell( bool enable
)
9555 m_canDragCell
= enable
;
9558 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9560 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9562 if ( resizeExistingRows
)
9564 // since we are resizing all rows to the default row size,
9565 // we can simply clear the row heights and row bottoms
9566 // arrays (which also allows us to take advantage of
9567 // some speed optimisations)
9568 m_rowHeights
.Empty();
9569 m_rowBottoms
.Empty();
9570 if ( !GetBatchCount() )
9575 void wxGrid::SetRowSize( int row
, int height
)
9577 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9579 // See comment in SetColSize
9580 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9582 if ( m_rowHeights
.IsEmpty() )
9584 // need to really create the array
9588 int h
= wxMax( 0, height
);
9589 int diff
= h
- m_rowHeights
[row
];
9591 m_rowHeights
[row
] = h
;
9593 for ( i
= row
; i
< m_numRows
; i
++ )
9595 m_rowBottoms
[i
] += diff
;
9597 if ( !GetBatchCount() )
9601 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9603 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9605 if ( resizeExistingCols
)
9607 // since we are resizing all columns to the default column size,
9608 // we can simply clear the col widths and col rights
9609 // arrays (which also allows us to take advantage of
9610 // some speed optimisations)
9611 m_colWidths
.Empty();
9612 m_colRights
.Empty();
9613 if ( !GetBatchCount() )
9618 void wxGrid::SetColSize( int col
, int width
)
9620 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9622 // should we check that it's bigger than GetColMinimalWidth(col) here?
9624 // No, because it is reasonable to assume the library user know's
9625 // what he is doing. However whe should test against the weaker
9626 // constariant of minimalAcceptableWidth, as this breaks rendering
9628 // This test then fixes sf.net bug #645734
9630 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9632 if ( m_colWidths
.IsEmpty() )
9634 // need to really create the array
9638 // if < 0 calc new width from label
9642 wxArrayString lines
;
9643 wxClientDC
dc(m_colLabelWin
);
9644 dc
.SetFont(GetLabelFont());
9645 StringToLines(GetColLabelValue(col
), lines
);
9646 GetTextBoxSize(dc
, lines
, &w
, &h
);
9649 int w
= wxMax( 0, width
);
9650 int diff
= w
- m_colWidths
[col
];
9651 m_colWidths
[col
] = w
;
9654 for ( i
= col
; i
< m_numCols
; i
++ )
9656 m_colRights
[i
] += diff
;
9658 if ( !GetBatchCount() )
9663 void wxGrid::SetColMinimalWidth( int col
, int width
)
9665 if (width
> GetColMinimalAcceptableWidth()) {
9666 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9667 m_colMinWidths
[key
] = width
;
9671 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9673 if (width
> GetRowMinimalAcceptableHeight()) {
9674 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9675 m_rowMinHeights
[key
] = width
;
9679 int wxGrid::GetColMinimalWidth(int col
) const
9681 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9682 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9683 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9686 int wxGrid::GetRowMinimalHeight(int row
) const
9688 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9689 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9690 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9693 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9695 // We do allow a width of 0 since this gives us
9696 // an easy way to temporarily hidding columns.
9699 m_minAcceptableColWidth
= width
;
9702 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9704 // We do allow a height of 0 since this gives us
9705 // an easy way to temporarily hidding rows.
9708 m_minAcceptableRowHeight
= height
;
9711 int wxGrid::GetColMinimalAcceptableWidth() const
9713 return m_minAcceptableColWidth
;
9716 int wxGrid::GetRowMinimalAcceptableHeight() const
9718 return m_minAcceptableRowHeight
;
9721 // ----------------------------------------------------------------------------
9723 // ----------------------------------------------------------------------------
9725 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9727 wxClientDC
dc(m_gridWin
);
9729 //Cancel editting of cell
9730 HideCellEditControl();
9731 SaveEditControlValue();
9733 // init both of them to avoid compiler warnings, even if weo nly need one
9741 wxCoord extent
, extentMax
= 0;
9742 int max
= column
? m_numRows
: m_numCols
;
9743 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9750 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9751 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9754 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9755 extent
= column
? size
.x
: size
.y
;
9756 if ( extent
> extentMax
)
9767 // now also compare with the column label extent
9769 dc
.SetFont( GetLabelFont() );
9773 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9774 if( GetColLabelTextOrientation() == wxVERTICAL
)
9778 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9780 extent
= column
? w
: h
;
9781 if ( extent
> extentMax
)
9788 // empty column - give default extent (notice that if extentMax is less
9789 // than default extent but != 0, it's ok)
9790 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9796 // leave some space around text
9807 SetColSize(col
, extentMax
);
9808 if ( !GetBatchCount() )
9811 m_gridWin
->GetClientSize( &cw
, &ch
);
9812 wxRect
rect ( CellToRect( 0, col
) );
9814 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9815 rect
.width
= cw
- rect
.x
;
9816 rect
.height
= m_colLabelHeight
;
9817 m_colLabelWin
->Refresh( true, &rect
);
9822 SetRowSize(row
, extentMax
);
9823 if ( !GetBatchCount() )
9826 m_gridWin
->GetClientSize( &cw
, &ch
);
9827 wxRect
rect ( CellToRect( row
, 0 ) );
9829 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9830 rect
.width
= m_rowLabelWidth
;
9831 rect
.height
= ch
- rect
.y
;
9832 m_rowLabelWin
->Refresh( true, &rect
);
9838 SetColMinimalWidth(col
, extentMax
);
9840 SetRowMinimalHeight(row
, extentMax
);
9844 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9846 int width
= m_rowLabelWidth
;
9851 for ( int col
= 0; col
< m_numCols
; col
++ )
9855 AutoSizeColumn(col
, setAsMin
);
9858 width
+= GetColWidth(col
);
9867 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9869 int height
= m_colLabelHeight
;
9874 for ( int row
= 0; row
< m_numRows
; row
++ )
9878 AutoSizeRow(row
, setAsMin
);
9881 height
+= GetRowHeight(row
);
9890 void wxGrid::AutoSize()
9894 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9896 // round up the size to a multiple of scroll step - this ensures that we
9897 // won't get the scrollbars if we're sized exactly to this width
9898 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9900 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * m_scrollLineX
,
9901 GetScrollY(size
.y
+ m_extraHeight
+ 1) * m_scrollLineY
);
9903 // distribute the extra space between the columns/rows to avoid having
9904 // extra white space
9906 // Remove the extra m_extraWidth + 1 added above
9907 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9908 if ( diff
&& m_numCols
)
9910 // try to resize the columns uniformly
9911 wxCoord diffPerCol
= diff
/ m_numCols
;
9914 for ( int col
= 0; col
< m_numCols
; col
++ )
9916 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9920 // add remaining amount to the last columns
9921 diff
-= diffPerCol
* m_numCols
;
9924 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9926 SetColSize(col
, GetColWidth(col
) + 1);
9932 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9933 if ( diff
&& m_numRows
)
9935 // try to resize the columns uniformly
9936 wxCoord diffPerRow
= diff
/ m_numRows
;
9939 for ( int row
= 0; row
< m_numRows
; row
++ )
9941 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9945 // add remaining amount to the last rows
9946 diff
-= diffPerRow
* m_numRows
;
9949 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9951 SetRowSize(row
, GetRowHeight(row
) + 1);
9958 SetClientSize(sizeFit
);
9961 void wxGrid::AutoSizeRowLabelSize( int row
)
9963 wxArrayString lines
;
9966 // Hide the edit control, so it
9967 // won't interfer with drag-shrinking.
9968 if( IsCellEditControlShown() )
9970 HideCellEditControl();
9971 SaveEditControlValue();
9974 // autosize row height depending on label text
9975 StringToLines( GetRowLabelValue( row
), lines
);
9976 wxClientDC
dc( m_rowLabelWin
);
9977 GetTextBoxSize( dc
, lines
, &w
, &h
);
9978 if( h
< m_defaultRowHeight
)
9979 h
= m_defaultRowHeight
;
9984 void wxGrid::AutoSizeColLabelSize( int col
)
9986 wxArrayString lines
;
9989 // Hide the edit control, so it
9990 // won't interfer with drag-shrinking.
9991 if( IsCellEditControlShown() )
9993 HideCellEditControl();
9994 SaveEditControlValue();
9997 // autosize column width depending on label text
9998 StringToLines( GetColLabelValue( col
), lines
);
9999 wxClientDC
dc( m_colLabelWin
);
10000 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
10001 GetTextBoxSize( dc
, lines
, &w
, &h
);
10003 GetTextBoxSize( dc
, lines
, &h
, &w
);
10004 if( w
< m_defaultColWidth
)
10005 w
= m_defaultColWidth
;
10006 SetColSize(col
, w
);
10010 wxSize
wxGrid::DoGetBestSize() const
10012 // don't set sizes, only calculate them
10013 wxGrid
*self
= (wxGrid
*)this; // const_cast
10016 width
= self
->SetOrCalcColumnSizes(true);
10017 height
= self
->SetOrCalcRowSizes(true);
10019 if (!width
) width
=100;
10020 if (!height
) height
=80;
10022 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
10023 // of the scrollbars, is there any magic incantaion for that?
10025 GetScrollPixelsPerUnit(&xpu
, &ypu
);
10027 width
+= 1 + xpu
- (width
% xpu
);
10029 height
+= 1 + ypu
- (height
% ypu
);
10031 // limit to 1/4 of the screen size
10032 int maxwidth
, maxheight
;
10033 wxDisplaySize( & maxwidth
, & maxheight
);
10036 if ( width
> maxwidth
) width
= maxwidth
;
10037 if ( height
> maxheight
) height
= maxheight
;
10040 wxSize
best(width
, height
);
10041 // NOTE: This size should be cached, but first we need to add calls to
10042 // InvalidateBestSize everywhere that could change the results of this
10044 // CacheBestSize(size);
10054 wxPen
& wxGrid::GetDividerPen() const
10059 // ----------------------------------------------------------------------------
10060 // cell value accessor functions
10061 // ----------------------------------------------------------------------------
10063 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
10067 m_table
->SetValue( row
, col
, s
);
10068 if ( !GetBatchCount() )
10071 wxRect
rect( CellToRect( row
, col
) );
10073 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10074 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10075 m_gridWin
->Refresh( false, &rect
);
10078 if ( m_currentCellCoords
.GetRow() == row
&&
10079 m_currentCellCoords
.GetCol() == col
&&
10080 IsCellEditControlShown())
10081 // Note: If we are using IsCellEditControlEnabled,
10082 // this interacts badly with calling SetCellValue from
10083 // an EVT_GRID_CELL_CHANGE handler.
10085 HideCellEditControl();
10086 ShowCellEditControl(); // will reread data from table
10093 // ------ Block, row and col selection
10096 void wxGrid::SelectRow( int row
, bool addToSelected
)
10098 if ( IsSelection() && !addToSelected
)
10102 m_selection
->SelectRow( row
, false, addToSelected
);
10106 void wxGrid::SelectCol( int col
, bool addToSelected
)
10108 if ( IsSelection() && !addToSelected
)
10112 m_selection
->SelectCol( col
, false, addToSelected
);
10116 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10117 bool addToSelected
)
10119 if ( IsSelection() && !addToSelected
)
10123 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10124 false, addToSelected
);
10128 void wxGrid::SelectAll()
10130 if ( m_numRows
> 0 && m_numCols
> 0 )
10133 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10138 // ------ Cell, row and col deselection
10141 void wxGrid::DeselectRow( int row
)
10143 if ( !m_selection
)
10146 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10148 if ( m_selection
->IsInSelection(row
, 0 ) )
10149 m_selection
->ToggleCellSelection( row
, 0);
10153 int nCols
= GetNumberCols();
10154 for ( int i
= 0; i
< nCols
; i
++ )
10156 if ( m_selection
->IsInSelection(row
, i
) )
10157 m_selection
->ToggleCellSelection( row
, i
);
10162 void wxGrid::DeselectCol( int col
)
10164 if ( !m_selection
)
10167 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10169 if ( m_selection
->IsInSelection(0, col
) )
10170 m_selection
->ToggleCellSelection( 0, col
);
10174 int nRows
= GetNumberRows();
10175 for ( int i
= 0; i
< nRows
; i
++ )
10177 if ( m_selection
->IsInSelection(i
, col
) )
10178 m_selection
->ToggleCellSelection(i
, col
);
10183 void wxGrid::DeselectCell( int row
, int col
)
10185 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10186 m_selection
->ToggleCellSelection(row
, col
);
10189 bool wxGrid::IsSelection()
10191 return ( m_selection
&& (m_selection
->IsSelection() ||
10192 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10193 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10196 bool wxGrid::IsInSelection( int row
, int col
) const
10198 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10199 ( row
>= m_selectingTopLeft
.GetRow() &&
10200 col
>= m_selectingTopLeft
.GetCol() &&
10201 row
<= m_selectingBottomRight
.GetRow() &&
10202 col
<= m_selectingBottomRight
.GetCol() )) );
10205 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10207 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10208 return m_selection
->m_cellSelection
;
10210 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10212 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10213 return m_selection
->m_blockSelectionTopLeft
;
10215 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10217 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10218 return m_selection
->m_blockSelectionBottomRight
;
10220 wxArrayInt
wxGrid::GetSelectedRows() const
10222 if (!m_selection
) { wxArrayInt a
; return a
; }
10223 return m_selection
->m_rowSelection
;
10225 wxArrayInt
wxGrid::GetSelectedCols() const
10227 if (!m_selection
) { wxArrayInt a
; return a
; }
10228 return m_selection
->m_colSelection
;
10232 void wxGrid::ClearSelection()
10234 m_selectingTopLeft
= wxGridNoCellCoords
;
10235 m_selectingBottomRight
= wxGridNoCellCoords
;
10237 m_selection
->ClearSelection();
10241 // This function returns the rectangle that encloses the given block
10242 // in device coords clipped to the client size of the grid window.
10244 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10245 const wxGridCellCoords
&bottomRight
)
10247 wxRect
rect( wxGridNoCellRect
);
10250 cellRect
= CellToRect( topLeft
);
10251 if ( cellRect
!= wxGridNoCellRect
)
10257 rect
= wxRect(0,0,0,0);
10260 cellRect
= CellToRect( bottomRight
);
10261 if ( cellRect
!= wxGridNoCellRect
)
10267 return wxGridNoCellRect
;
10271 int left
= rect
.GetLeft();
10272 int top
= rect
.GetTop();
10273 int right
= rect
.GetRight();
10274 int bottom
= rect
.GetBottom();
10276 int leftCol
= topLeft
.GetCol();
10277 int topRow
= topLeft
.GetRow();
10278 int rightCol
= bottomRight
.GetCol();
10279 int bottomRow
= bottomRight
.GetRow();
10297 topRow
= bottomRow
;
10302 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10304 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10306 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10308 cellRect
= CellToRect( j
, i
);
10310 if (cellRect
.x
< left
)
10312 if (cellRect
.y
< top
)
10314 if (cellRect
.x
+ cellRect
.width
> right
)
10315 right
= cellRect
.x
+ cellRect
.width
;
10316 if (cellRect
.y
+ cellRect
.height
> bottom
)
10317 bottom
= cellRect
.y
+ cellRect
.height
;
10319 else i
= rightCol
; // jump over inner cells.
10323 // convert to scrolled coords
10325 CalcScrolledPosition( left
, top
, &left
, &top
);
10326 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10329 m_gridWin
->GetClientSize( &cw
, &ch
);
10331 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10332 return wxRect(0,0,0,0);
10334 rect
.SetLeft( wxMax(0, left
) );
10335 rect
.SetTop( wxMax(0, top
) );
10336 rect
.SetRight( wxMin(cw
, right
) );
10337 rect
.SetBottom( wxMin(ch
, bottom
) );
10343 // ------ Grid event classes
10346 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10348 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10349 int row
, int col
, int x
, int y
, bool sel
,
10350 bool control
, bool shift
, bool alt
, bool meta
)
10351 : wxNotifyEvent( type
, id
)
10358 m_control
= control
;
10363 SetEventObject(obj
);
10367 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10369 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10370 int rowOrCol
, int x
, int y
,
10371 bool control
, bool shift
, bool alt
, bool meta
)
10372 : wxNotifyEvent( type
, id
)
10374 m_rowOrCol
= rowOrCol
;
10377 m_control
= control
;
10382 SetEventObject(obj
);
10386 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10388 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10389 const wxGridCellCoords
& topLeft
,
10390 const wxGridCellCoords
& bottomRight
,
10391 bool sel
, bool control
,
10392 bool shift
, bool alt
, bool meta
)
10393 : wxNotifyEvent( type
, id
)
10395 m_topLeft
= topLeft
;
10396 m_bottomRight
= bottomRight
;
10398 m_control
= control
;
10403 SetEventObject(obj
);
10407 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10409 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10410 wxObject
* obj
, int row
,
10411 int col
, wxControl
* ctrl
)
10412 : wxCommandEvent(type
, id
)
10414 SetEventObject(obj
);
10420 #endif // wxUSE_GRID