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
& );
147 DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow
)
148 DECLARE_EVENT_TABLE()
149 DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow
)
153 class WXDLLIMPEXP_ADV wxGridColLabelWindow
: public wxWindow
156 wxGridColLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
157 wxGridColLabelWindow( wxGrid
*parent
, wxWindowID id
,
158 const wxPoint
&pos
, const wxSize
&size
);
163 void OnPaint( wxPaintEvent
&event
);
164 void OnMouseEvent( wxMouseEvent
& event
);
165 void OnMouseWheel( wxMouseEvent
& event
);
166 void OnKeyDown( wxKeyEvent
& event
);
167 void OnKeyUp( wxKeyEvent
& );
169 DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow
)
170 DECLARE_EVENT_TABLE()
171 DECLARE_NO_COPY_CLASS(wxGridColLabelWindow
)
175 class WXDLLIMPEXP_ADV wxGridCornerLabelWindow
: public wxWindow
178 wxGridCornerLabelWindow() { m_owner
= (wxGrid
*)NULL
; }
179 wxGridCornerLabelWindow( wxGrid
*parent
, wxWindowID id
,
180 const wxPoint
&pos
, const wxSize
&size
);
185 void OnMouseEvent( wxMouseEvent
& event
);
186 void OnMouseWheel( wxMouseEvent
& event
);
187 void OnKeyDown( wxKeyEvent
& event
);
188 void OnKeyUp( wxKeyEvent
& );
189 void OnPaint( wxPaintEvent
& event
);
191 DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow
)
192 DECLARE_EVENT_TABLE()
193 DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow
)
196 class WXDLLIMPEXP_ADV wxGridWindow
: public wxWindow
201 m_owner
= (wxGrid
*)NULL
;
202 m_rowLabelWin
= (wxGridRowLabelWindow
*)NULL
;
203 m_colLabelWin
= (wxGridColLabelWindow
*)NULL
;
206 wxGridWindow( wxGrid
*parent
,
207 wxGridRowLabelWindow
*rowLblWin
,
208 wxGridColLabelWindow
*colLblWin
,
209 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
212 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
214 wxGrid
* GetOwner() { return m_owner
; }
218 wxGridRowLabelWindow
*m_rowLabelWin
;
219 wxGridColLabelWindow
*m_colLabelWin
;
221 void OnPaint( wxPaintEvent
&event
);
222 void OnMouseWheel( wxMouseEvent
& event
);
223 void OnMouseEvent( wxMouseEvent
& event
);
224 void OnKeyDown( wxKeyEvent
& );
225 void OnKeyUp( wxKeyEvent
& );
226 void OnEraseBackground( wxEraseEvent
& );
227 void OnFocus( wxFocusEvent
& );
229 DECLARE_DYNAMIC_CLASS(wxGridWindow
)
230 DECLARE_EVENT_TABLE()
231 DECLARE_NO_COPY_CLASS(wxGridWindow
)
236 class wxGridCellEditorEvtHandler
: public wxEvtHandler
239 wxGridCellEditorEvtHandler()
240 : m_grid(0), m_editor(0)
242 wxGridCellEditorEvtHandler(wxGrid
* grid
, wxGridCellEditor
* editor
)
243 : m_grid(grid
), m_editor(editor
)
246 void OnKeyDown(wxKeyEvent
& event
);
247 void OnChar(wxKeyEvent
& event
);
251 wxGridCellEditor
* m_editor
;
252 DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler
)
253 DECLARE_EVENT_TABLE()
254 DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler
)
258 IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler
, wxEvtHandler
)
259 BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler
, wxEvtHandler
)
260 EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown
)
261 EVT_CHAR( wxGridCellEditorEvtHandler::OnChar
)
266 // ----------------------------------------------------------------------------
267 // the internal data representation used by wxGridCellAttrProvider
268 // ----------------------------------------------------------------------------
270 // this class stores attributes set for cells
271 class WXDLLIMPEXP_ADV wxGridCellAttrData
274 void SetAttr(wxGridCellAttr
*attr
, int row
, int col
);
275 wxGridCellAttr
*GetAttr(int row
, int col
) const;
276 void UpdateAttrRows( size_t pos
, int numRows
);
277 void UpdateAttrCols( size_t pos
, int numCols
);
280 // searches for the attr for given cell, returns wxNOT_FOUND if not found
281 int FindIndex(int row
, int col
) const;
283 wxGridCellWithAttrArray m_attrs
;
286 // this class stores attributes set for rows or columns
287 class WXDLLIMPEXP_ADV wxGridRowOrColAttrData
290 // empty ctor to suppress warnings
291 wxGridRowOrColAttrData() { }
292 ~wxGridRowOrColAttrData();
294 void SetAttr(wxGridCellAttr
*attr
, int rowOrCol
);
295 wxGridCellAttr
*GetAttr(int rowOrCol
) const;
296 void UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
);
299 wxArrayInt m_rowsOrCols
;
300 wxArrayAttrs m_attrs
;
303 // NB: this is just a wrapper around 3 objects: one which stores cell
304 // attributes, and 2 others for row/col ones
305 class WXDLLIMPEXP_ADV wxGridCellAttrProviderData
308 wxGridCellAttrData m_cellAttrs
;
309 wxGridRowOrColAttrData m_rowAttrs
,
314 // ----------------------------------------------------------------------------
315 // data structures used for the data type registry
316 // ----------------------------------------------------------------------------
318 struct wxGridDataTypeInfo
320 wxGridDataTypeInfo(const wxString
& typeName
,
321 wxGridCellRenderer
* renderer
,
322 wxGridCellEditor
* editor
)
323 : m_typeName(typeName
), m_renderer(renderer
), m_editor(editor
)
326 ~wxGridDataTypeInfo()
328 wxSafeDecRef(m_renderer
);
329 wxSafeDecRef(m_editor
);
333 wxGridCellRenderer
* m_renderer
;
334 wxGridCellEditor
* m_editor
;
336 DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo
)
340 WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo
*, wxGridDataTypeInfoArray
,
341 class WXDLLIMPEXP_ADV
);
344 class WXDLLIMPEXP_ADV wxGridTypeRegistry
347 wxGridTypeRegistry() {}
348 ~wxGridTypeRegistry();
350 void RegisterDataType(const wxString
& typeName
,
351 wxGridCellRenderer
* renderer
,
352 wxGridCellEditor
* editor
);
354 // find one of already registered data types
355 int FindRegisteredDataType(const wxString
& typeName
);
357 // try to FindRegisteredDataType(), if this fails and typeName is one of
358 // standard typenames, register it and return its index
359 int FindDataType(const wxString
& typeName
);
361 // try to FindDataType(), if it fails see if it is not one of already
362 // registered data types with some params in which case clone the
363 // registered data type and set params for it
364 int FindOrCloneDataType(const wxString
& typeName
);
366 wxGridCellRenderer
* GetRenderer(int index
);
367 wxGridCellEditor
* GetEditor(int index
);
370 wxGridDataTypeInfoArray m_typeinfo
;
373 // ----------------------------------------------------------------------------
374 // conditional compilation
375 // ----------------------------------------------------------------------------
377 #ifndef WXGRID_DRAW_LINES
378 #define WXGRID_DRAW_LINES 1
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 //#define DEBUG_ATTR_CACHE
386 #ifdef DEBUG_ATTR_CACHE
387 static size_t gs_nAttrCacheHits
= 0;
388 static size_t gs_nAttrCacheMisses
= 0;
389 #endif // DEBUG_ATTR_CACHE
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 wxGridCellCoords
wxGridNoCellCoords( -1, -1 );
396 wxRect
wxGridNoCellRect( -1, -1, -1, -1 );
399 // TODO: this doesn't work at all, grid cells have different sizes and approx
400 // calculations don't work as because of the size mismatch scrollbars
401 // sometimes fail to be shown when they should be or vice versa
403 // The scroll bars may be a little flakey once in a while, but that is
404 // surely much less horrible than having scroll lines of only 1!!!
407 // Well, it's still seriously broken so it might be better but needs
410 static const size_t GRID_SCROLL_LINE_X
= 15; // 1;
411 static const size_t GRID_SCROLL_LINE_Y
= GRID_SCROLL_LINE_X
;
413 // the size of hash tables used a bit everywhere (the max number of elements
414 // in these hash tables is the number of rows/columns)
415 static const int GRID_HASH_SIZE
= 100;
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 static inline int GetScrollX(int x
)
424 return (x
+ GRID_SCROLL_LINE_X
- 1) / GRID_SCROLL_LINE_X
;
427 static inline int GetScrollY(int y
)
429 return (y
+ GRID_SCROLL_LINE_Y
- 1) / GRID_SCROLL_LINE_Y
;
433 // ============================================================================
435 // ============================================================================
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 wxGridCellEditor::wxGridCellEditor()
448 wxGridCellEditor::~wxGridCellEditor()
453 void wxGridCellEditor::Create(wxWindow
* WXUNUSED(parent
),
454 wxWindowID
WXUNUSED(id
),
455 wxEvtHandler
* evtHandler
)
458 m_control
->PushEventHandler(evtHandler
);
461 void wxGridCellEditor::PaintBackground(const wxRect
& rectCell
,
462 wxGridCellAttr
*attr
)
464 // erase the background because we might not fill the cell
465 wxClientDC
dc(m_control
->GetParent());
466 wxGridWindow
* gridWindow
= wxDynamicCast(m_control
->GetParent(), wxGridWindow
);
468 gridWindow
->GetOwner()->PrepareDC(dc
);
470 dc
.SetPen(*wxTRANSPARENT_PEN
);
471 dc
.SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
472 dc
.DrawRectangle(rectCell
);
474 // redraw the control we just painted over
475 m_control
->Refresh();
478 void wxGridCellEditor::Destroy()
482 m_control
->PopEventHandler(true /* delete it*/);
484 m_control
->Destroy();
489 void wxGridCellEditor::Show(bool show
, wxGridCellAttr
*attr
)
491 wxASSERT_MSG(m_control
,
492 wxT("The wxGridCellEditor must be Created first!"));
493 m_control
->Show(show
);
497 // set the colours/fonts if we have any
500 m_colFgOld
= m_control
->GetForegroundColour();
501 m_control
->SetForegroundColour(attr
->GetTextColour());
503 m_colBgOld
= m_control
->GetBackgroundColour();
504 m_control
->SetBackgroundColour(attr
->GetBackgroundColour());
506 // Workaround for GTK+1 font setting problem on some platforms
507 #if !defined(__WXGTK__) || defined(__WXGTK20__)
508 m_fontOld
= m_control
->GetFont();
509 m_control
->SetFont(attr
->GetFont());
511 // can't do anything more in the base class version, the other
512 // attributes may only be used by the derived classes
517 // restore the standard colours fonts
518 if ( m_colFgOld
.Ok() )
520 m_control
->SetForegroundColour(m_colFgOld
);
521 m_colFgOld
= wxNullColour
;
524 if ( m_colBgOld
.Ok() )
526 m_control
->SetBackgroundColour(m_colBgOld
);
527 m_colBgOld
= wxNullColour
;
529 // Workaround for GTK+1 font setting problem on some platforms
530 #if !defined(__WXGTK__) || defined(__WXGTK20__)
531 if ( m_fontOld
.Ok() )
533 m_control
->SetFont(m_fontOld
);
534 m_fontOld
= wxNullFont
;
540 void wxGridCellEditor::SetSize(const wxRect
& rect
)
542 wxASSERT_MSG(m_control
,
543 wxT("The wxGridCellEditor must be Created first!"));
544 m_control
->SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
547 void wxGridCellEditor::HandleReturn(wxKeyEvent
& event
)
552 bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent
& event
)
554 // accept the simple key presses, not anything with Ctrl/Alt/Meta
555 return !(event
.ControlDown() || event
.AltDown());
558 void wxGridCellEditor::StartingKey(wxKeyEvent
& event
)
563 void wxGridCellEditor::StartingClick()
569 // ----------------------------------------------------------------------------
570 // wxGridCellTextEditor
571 // ----------------------------------------------------------------------------
573 wxGridCellTextEditor::wxGridCellTextEditor()
578 void wxGridCellTextEditor::Create(wxWindow
* parent
,
580 wxEvtHandler
* evtHandler
)
582 m_control
= new wxTextCtrl(parent
, id
, wxEmptyString
,
583 wxDefaultPosition
, wxDefaultSize
584 #if defined(__WXMSW__)
585 , wxTE_PROCESS_TAB
| wxTE_AUTO_SCROLL
589 // set max length allowed in the textctrl, if the parameter was set
592 ((wxTextCtrl
*)m_control
)->SetMaxLength(m_maxChars
);
595 wxGridCellEditor::Create(parent
, id
, evtHandler
);
598 void wxGridCellTextEditor::PaintBackground(const wxRect
& WXUNUSED(rectCell
),
599 wxGridCellAttr
* WXUNUSED(attr
))
601 // as we fill the entire client area, don't do anything here to minimize
605 void wxGridCellTextEditor::SetSize(const wxRect
& rectOrig
)
607 wxRect
rect(rectOrig
);
609 // Make the edit control large enough to allow for internal
612 // TODO: remove this if the text ctrl sizing is improved esp. for
615 #if defined(__WXGTK__)
624 int extra_x
= ( rect
.x
> 2 )? 2 : 1;
626 // MB: treat MSW separately here otherwise the caret doesn't show
627 // when the editor is in the first row.
628 #if defined(__WXMSW__)
631 int extra_y
= ( rect
.y
> 2 )? 2 : 1;
634 #if defined(__WXMOTIF__)
638 rect
.SetLeft( wxMax(0, rect
.x
- extra_x
) );
639 rect
.SetTop( wxMax(0, rect
.y
- extra_y
) );
640 rect
.SetRight( rect
.GetRight() + 2*extra_x
);
641 rect
.SetBottom( rect
.GetBottom() + 2*extra_y
);
644 wxGridCellEditor::SetSize(rect
);
647 void wxGridCellTextEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
649 wxASSERT_MSG(m_control
,
650 wxT("The wxGridCellEditor must be Created first!"));
652 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
654 DoBeginEdit(m_startValue
);
657 void wxGridCellTextEditor::DoBeginEdit(const wxString
& startValue
)
659 Text()->SetValue(startValue
);
660 Text()->SetInsertionPointEnd();
661 Text()->SetSelection(-1,-1);
665 bool wxGridCellTextEditor::EndEdit(int row
, int col
,
668 wxASSERT_MSG(m_control
,
669 wxT("The wxGridCellEditor must be Created first!"));
671 bool changed
= false;
672 wxString value
= Text()->GetValue();
673 if (value
!= m_startValue
)
677 grid
->GetTable()->SetValue(row
, col
, value
);
679 m_startValue
= wxEmptyString
;
680 // No point in setting the text of the hidden control
681 //Text()->SetValue(m_startValue);
687 void wxGridCellTextEditor::Reset()
689 wxASSERT_MSG(m_control
,
690 wxT("The wxGridCellEditor must be Created first!"));
692 DoReset(m_startValue
);
695 void wxGridCellTextEditor::DoReset(const wxString
& startValue
)
697 Text()->SetValue(startValue
);
698 Text()->SetInsertionPointEnd();
701 bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent
& event
)
703 if ( wxGridCellEditor::IsAcceptedKey(event
) )
705 int keycode
= event
.GetKeyCode();
719 case WXK_NUMPAD_MULTIPLY
:
723 case WXK_NUMPAD_SUBTRACT
:
725 case WXK_NUMPAD_DECIMAL
:
727 case WXK_NUMPAD_DIVIDE
:
731 // accept 8 bit chars too if isprint() agrees
732 if ( (keycode
< 255) && (wxIsprint(keycode
)) )
740 void wxGridCellTextEditor::StartingKey(wxKeyEvent
& event
)
742 if ( !Text()->EmulateKeyPress(event
) )
748 void wxGridCellTextEditor::HandleReturn( wxKeyEvent
&
749 WXUNUSED_GTK(WXUNUSED_MOTIF(event
)) )
751 #if defined(__WXMOTIF__) || defined(__WXGTK__)
752 // wxMotif needs a little extra help...
753 size_t pos
= (size_t)( Text()->GetInsertionPoint() );
754 wxString
s( Text()->GetValue() );
755 s
= s
.Left(pos
) + wxT("\n") + s
.Mid(pos
);
757 Text()->SetInsertionPoint( pos
);
759 // the other ports can handle a Return key press
765 void wxGridCellTextEditor::SetParameters(const wxString
& params
)
775 if ( !params
.ToLong(&tmp
) )
777 wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params
.c_str());
781 m_maxChars
= (size_t)tmp
;
786 // return the value in the text control
787 wxString
wxGridCellTextEditor::GetValue() const
789 return Text()->GetValue();
792 // ----------------------------------------------------------------------------
793 // wxGridCellNumberEditor
794 // ----------------------------------------------------------------------------
796 wxGridCellNumberEditor::wxGridCellNumberEditor(int min
, int max
)
802 void wxGridCellNumberEditor::Create(wxWindow
* parent
,
804 wxEvtHandler
* evtHandler
)
809 // create a spin ctrl
810 m_control
= new wxSpinCtrl(parent
, wxID_ANY
, wxEmptyString
,
811 wxDefaultPosition
, wxDefaultSize
,
815 wxGridCellEditor::Create(parent
, id
, evtHandler
);
820 // just a text control
821 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
824 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
825 #endif // wxUSE_VALIDATORS
829 void wxGridCellNumberEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
831 // first get the value
832 wxGridTableBase
*table
= grid
->GetTable();
833 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
835 m_valueOld
= table
->GetValueAsLong(row
, col
);
840 wxString sValue
= table
->GetValue(row
, col
);
841 if (! sValue
.ToLong(&m_valueOld
) && ! sValue
.empty())
843 wxFAIL_MSG( _T("this cell doesn't have numeric value") );
851 Spin()->SetValue((int)m_valueOld
);
857 DoBeginEdit(GetString());
861 bool wxGridCellNumberEditor::EndEdit(int row
, int col
,
871 value
= Spin()->GetValue();
872 changed
= value
!= m_valueOld
;
874 text
= wxString::Format(wxT("%ld"), value
);
879 text
= Text()->GetValue();
880 changed
= (text
.empty() || text
.ToLong(&value
)) && (value
!= m_valueOld
);
885 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_NUMBER
))
886 grid
->GetTable()->SetValueAsLong(row
, col
, value
);
888 grid
->GetTable()->SetValue(row
, col
, text
);
894 void wxGridCellNumberEditor::Reset()
899 Spin()->SetValue((int)m_valueOld
);
904 DoReset(GetString());
908 bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent
& event
)
910 if ( wxGridCellEditor::IsAcceptedKey(event
) )
912 int keycode
= event
.GetKeyCode();
928 case WXK_NUMPAD_SUBTRACT
:
934 if ( (keycode
< 128) && wxIsdigit(keycode
) )
942 void wxGridCellNumberEditor::StartingKey(wxKeyEvent
& event
)
946 int keycode
= event
.GetKeyCode();
947 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
948 || keycode
== WXK_NUMPAD0
949 || keycode
== WXK_NUMPAD1
950 || keycode
== WXK_NUMPAD2
951 || keycode
== WXK_NUMPAD3
952 || keycode
== WXK_NUMPAD4
953 || keycode
== WXK_NUMPAD5
954 || keycode
== WXK_NUMPAD6
955 || keycode
== WXK_NUMPAD7
956 || keycode
== WXK_NUMPAD8
957 || keycode
== WXK_NUMPAD9
958 || keycode
== WXK_ADD
959 || keycode
== WXK_NUMPAD_ADD
960 || keycode
== WXK_SUBTRACT
961 || keycode
== WXK_NUMPAD_SUBTRACT
)
963 wxGridCellTextEditor::StartingKey(event
);
973 void wxGridCellNumberEditor::SetParameters(const wxString
& params
)
984 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
988 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
992 // skip the error message below
997 wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params
.c_str());
1001 // return the value in the spin control if it is there (the text control otherwise)
1002 wxString
wxGridCellNumberEditor::GetValue() const
1009 long value
= Spin()->GetValue();
1010 s
.Printf(wxT("%ld"), value
);
1015 s
= Text()->GetValue();
1021 // ----------------------------------------------------------------------------
1022 // wxGridCellFloatEditor
1023 // ----------------------------------------------------------------------------
1025 wxGridCellFloatEditor::wxGridCellFloatEditor(int width
, int precision
)
1028 m_precision
= precision
;
1031 void wxGridCellFloatEditor::Create(wxWindow
* parent
,
1033 wxEvtHandler
* evtHandler
)
1035 wxGridCellTextEditor::Create(parent
, id
, evtHandler
);
1037 #if wxUSE_VALIDATORS
1038 Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC
));
1039 #endif // wxUSE_VALIDATORS
1042 void wxGridCellFloatEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1044 // first get the value
1045 wxGridTableBase
*table
= grid
->GetTable();
1046 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1048 m_valueOld
= table
->GetValueAsDouble(row
, col
);
1053 wxString sValue
= table
->GetValue(row
, col
);
1054 if (! sValue
.ToDouble(&m_valueOld
) && ! sValue
.empty())
1056 wxFAIL_MSG( _T("this cell doesn't have float value") );
1061 DoBeginEdit(GetString());
1064 bool wxGridCellFloatEditor::EndEdit(int row
, int col
,
1068 wxString
text(Text()->GetValue());
1070 if ( (text
.empty() || text
.ToDouble(&value
)) && (value
!= m_valueOld
) )
1072 if (grid
->GetTable()->CanSetValueAs(row
, col
, wxGRID_VALUE_FLOAT
))
1073 grid
->GetTable()->SetValueAsDouble(row
, col
, value
);
1075 grid
->GetTable()->SetValue(row
, col
, text
);
1082 void wxGridCellFloatEditor::Reset()
1084 DoReset(GetString());
1087 void wxGridCellFloatEditor::StartingKey(wxKeyEvent
& event
)
1089 int keycode
= event
.GetKeyCode();
1091 tmpbuf
[0] = (char) keycode
;
1093 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1094 bool is_decimal_point
= ( strbuf
==
1095 wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
, wxLOCALE_CAT_NUMBER
) );
1096 if ( wxIsdigit(keycode
) || keycode
== '+' || keycode
== '-'
1098 || keycode
== WXK_NUMPAD0
1099 || keycode
== WXK_NUMPAD1
1100 || keycode
== WXK_NUMPAD2
1101 || keycode
== WXK_NUMPAD3
1102 || keycode
== WXK_NUMPAD4
1103 || keycode
== WXK_NUMPAD5
1104 || keycode
== WXK_NUMPAD6
1105 || keycode
== WXK_NUMPAD7
1106 || keycode
== WXK_NUMPAD8
1107 || keycode
== WXK_NUMPAD9
1108 || keycode
== WXK_ADD
1109 || keycode
== WXK_NUMPAD_ADD
1110 || keycode
== WXK_SUBTRACT
1111 || keycode
== WXK_NUMPAD_SUBTRACT
)
1113 wxGridCellTextEditor::StartingKey(event
);
1115 // skip Skip() below
1122 void wxGridCellFloatEditor::SetParameters(const wxString
& params
)
1133 if ( params
.BeforeFirst(_T(',')).ToLong(&tmp
) )
1137 if ( params
.AfterFirst(_T(',')).ToLong(&tmp
) )
1139 m_precision
= (int)tmp
;
1141 // skip the error message below
1146 wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params
.c_str());
1150 wxString
wxGridCellFloatEditor::GetString() const
1153 if ( m_width
== -1 )
1155 // default width/precision
1158 else if ( m_precision
== -1 )
1160 // default precision
1161 fmt
.Printf(_T("%%%d.f"), m_width
);
1165 fmt
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1168 return wxString::Format(fmt
, m_valueOld
);
1171 bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent
& event
)
1173 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1175 int keycode
= event
.GetKeyCode();
1189 case WXK_NUMPAD_ADD
:
1191 case WXK_NUMPAD_SUBTRACT
:
1193 case WXK_NUMPAD_DECIMAL
:
1198 // additionally accept 'e' as in '1e+6', also '-', '+', and '.'
1200 tmpbuf
[0] = (char) keycode
;
1202 wxString
strbuf(tmpbuf
, *wxConvCurrent
);
1203 bool is_decimal_point
=
1204 ( strbuf
== wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1205 wxLOCALE_CAT_NUMBER
) );
1206 if ( (keycode
< 128) &&
1207 (wxIsdigit(keycode
) || tolower(keycode
) == 'e' ||
1208 is_decimal_point
|| keycode
== '+' || keycode
== '-') )
1217 #endif // wxUSE_TEXTCTRL
1221 // ----------------------------------------------------------------------------
1222 // wxGridCellBoolEditor
1223 // ----------------------------------------------------------------------------
1225 void wxGridCellBoolEditor::Create(wxWindow
* parent
,
1227 wxEvtHandler
* evtHandler
)
1229 m_control
= new wxCheckBox(parent
, id
, wxEmptyString
,
1230 wxDefaultPosition
, wxDefaultSize
,
1233 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1236 void wxGridCellBoolEditor::SetSize(const wxRect
& r
)
1238 bool resize
= false;
1239 wxSize size
= m_control
->GetSize();
1240 wxCoord minSize
= wxMin(r
.width
, r
.height
);
1242 // check if the checkbox is not too big/small for this cell
1243 wxSize sizeBest
= m_control
->GetBestSize();
1244 if ( !(size
== sizeBest
) )
1246 // reset to default size if it had been made smaller
1252 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
1254 // leave 1 pixel margin
1255 size
.x
= size
.y
= minSize
- 2;
1262 m_control
->SetSize(size
);
1265 // position it in the centre of the rectangle (TODO: support alignment?)
1267 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1268 // the checkbox without label still has some space to the right in wxGTK,
1269 // so shift it to the right
1271 #elif defined(__WXMSW__)
1272 // here too, but in other way
1277 int hAlign
= wxALIGN_CENTRE
;
1278 int vAlign
= wxALIGN_CENTRE
;
1280 GetCellAttr()->GetAlignment(& hAlign
, & vAlign
);
1283 if (hAlign
== wxALIGN_LEFT
)
1289 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1291 else if (hAlign
== wxALIGN_RIGHT
)
1293 x
= r
.x
+ r
.width
- size
.x
- 2;
1294 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1296 else if (hAlign
== wxALIGN_CENTRE
)
1298 x
= r
.x
+ r
.width
/2 - size
.x
/2;
1299 y
= r
.y
+ r
.height
/2 - size
.y
/2;
1302 m_control
->Move(x
, y
);
1305 void wxGridCellBoolEditor::Show(bool show
, wxGridCellAttr
*attr
)
1307 m_control
->Show(show
);
1311 wxColour colBg
= attr
? attr
->GetBackgroundColour() : *wxLIGHT_GREY
;
1312 CBox()->SetBackgroundColour(colBg
);
1316 void wxGridCellBoolEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1318 wxASSERT_MSG(m_control
,
1319 wxT("The wxGridCellEditor must be Created first!"));
1321 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1322 m_startValue
= grid
->GetTable()->GetValueAsBool(row
, col
);
1325 wxString
cellval( grid
->GetTable()->GetValue(row
, col
) );
1326 m_startValue
= !( !cellval
|| (cellval
== wxT("0")) );
1328 CBox()->SetValue(m_startValue
);
1332 bool wxGridCellBoolEditor::EndEdit(int row
, int col
,
1335 wxASSERT_MSG(m_control
,
1336 wxT("The wxGridCellEditor must be Created first!"));
1338 bool changed
= false;
1339 bool value
= CBox()->GetValue();
1340 if ( value
!= m_startValue
)
1345 if (grid
->GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
))
1346 grid
->GetTable()->SetValueAsBool(row
, col
, value
);
1348 grid
->GetTable()->SetValue(row
, col
, value
? _T("1") : wxEmptyString
);
1354 void wxGridCellBoolEditor::Reset()
1356 wxASSERT_MSG(m_control
,
1357 wxT("The wxGridCellEditor must be Created first!"));
1359 CBox()->SetValue(m_startValue
);
1362 void wxGridCellBoolEditor::StartingClick()
1364 CBox()->SetValue(!CBox()->GetValue());
1367 bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent
& event
)
1369 if ( wxGridCellEditor::IsAcceptedKey(event
) )
1371 int keycode
= event
.GetKeyCode();
1375 case WXK_NUMPAD_MULTIPLY
:
1377 case WXK_NUMPAD_ADD
:
1379 case WXK_NUMPAD_SUBTRACT
:
1390 // return the value as "1" for true and the empty string for false
1391 wxString
wxGridCellBoolEditor::GetValue() const
1393 bool bSet
= CBox()->GetValue();
1394 return bSet
? _T("1") : wxEmptyString
;
1397 #endif // wxUSE_CHECKBOX
1401 // ----------------------------------------------------------------------------
1402 // wxGridCellChoiceEditor
1403 // ----------------------------------------------------------------------------
1405 wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString
& choices
,
1407 : m_choices(choices
),
1408 m_allowOthers(allowOthers
) { }
1410 wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count
,
1411 const wxString choices
[],
1413 : m_allowOthers(allowOthers
)
1417 m_choices
.Alloc(count
);
1418 for ( size_t n
= 0; n
< count
; n
++ )
1420 m_choices
.Add(choices
[n
]);
1425 wxGridCellEditor
*wxGridCellChoiceEditor::Clone() const
1427 wxGridCellChoiceEditor
*editor
= new wxGridCellChoiceEditor
;
1428 editor
->m_allowOthers
= m_allowOthers
;
1429 editor
->m_choices
= m_choices
;
1434 void wxGridCellChoiceEditor::Create(wxWindow
* parent
,
1436 wxEvtHandler
* evtHandler
)
1438 m_control
= new wxComboBox(parent
, id
, wxEmptyString
,
1439 wxDefaultPosition
, wxDefaultSize
,
1441 m_allowOthers
? 0 : wxCB_READONLY
);
1443 wxGridCellEditor::Create(parent
, id
, evtHandler
);
1446 void wxGridCellChoiceEditor::PaintBackground(const wxRect
& rectCell
,
1447 wxGridCellAttr
* attr
)
1449 // as we fill the entire client area, don't do anything here to minimize
1452 // TODO: It doesn't actually fill the client area since the height of a
1453 // combo always defaults to the standard... Until someone has time to
1454 // figure out the right rectangle to paint, just do it the normal way...
1455 wxGridCellEditor::PaintBackground(rectCell
, attr
);
1458 void wxGridCellChoiceEditor::BeginEdit(int row
, int col
, wxGrid
* grid
)
1460 wxASSERT_MSG(m_control
,
1461 wxT("The wxGridCellEditor must be Created first!"));
1463 m_startValue
= grid
->GetTable()->GetValue(row
, col
);
1466 Combo()->SetValue(m_startValue
);
1469 // find the right position, or default to the first if not found
1470 int pos
= Combo()->FindString(m_startValue
);
1473 Combo()->SetSelection(pos
);
1475 Combo()->SetInsertionPointEnd();
1476 Combo()->SetFocus();
1479 bool wxGridCellChoiceEditor::EndEdit(int row
, int col
,
1482 wxString value
= Combo()->GetValue();
1483 if ( value
== m_startValue
)
1486 grid
->GetTable()->SetValue(row
, col
, value
);
1491 void wxGridCellChoiceEditor::Reset()
1493 Combo()->SetValue(m_startValue
);
1494 Combo()->SetInsertionPointEnd();
1497 void wxGridCellChoiceEditor::SetParameters(const wxString
& params
)
1507 wxStringTokenizer
tk(params
, _T(','));
1508 while ( tk
.HasMoreTokens() )
1510 m_choices
.Add(tk
.GetNextToken());
1514 // return the value in the text control
1515 wxString
wxGridCellChoiceEditor::GetValue() const
1517 return Combo()->GetValue();
1520 #endif // wxUSE_COMBOBOX
1522 // ----------------------------------------------------------------------------
1523 // wxGridCellEditorEvtHandler
1524 // ----------------------------------------------------------------------------
1526 void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent
& event
)
1528 switch ( event
.GetKeyCode() )
1532 m_grid
->DisableCellEditControl();
1536 m_grid
->GetEventHandler()->ProcessEvent( event
);
1540 case WXK_NUMPAD_ENTER
:
1541 if (!m_grid
->GetEventHandler()->ProcessEvent(event
))
1542 m_editor
->HandleReturn(event
);
1551 void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent
& event
)
1553 switch ( event
.GetKeyCode() )
1558 case WXK_NUMPAD_ENTER
:
1566 // ----------------------------------------------------------------------------
1567 // wxGridCellWorker is an (almost) empty common base class for
1568 // wxGridCellRenderer and wxGridCellEditor managing ref counting
1569 // ----------------------------------------------------------------------------
1571 void wxGridCellWorker::SetParameters(const wxString
& WXUNUSED(params
))
1576 wxGridCellWorker::~wxGridCellWorker()
1580 // ============================================================================
1582 // ============================================================================
1584 // ----------------------------------------------------------------------------
1585 // wxGridCellRenderer
1586 // ----------------------------------------------------------------------------
1588 void wxGridCellRenderer::Draw(wxGrid
& grid
,
1589 wxGridCellAttr
& attr
,
1592 int WXUNUSED(row
), int WXUNUSED(col
),
1595 dc
.SetBackgroundMode( wxSOLID
);
1597 // grey out fields if the grid is disabled
1598 if( grid
.IsEnabled() )
1602 dc
.SetBrush( wxBrush(grid
.GetSelectionBackground(), wxSOLID
) );
1606 dc
.SetBrush( wxBrush(attr
.GetBackgroundColour(), wxSOLID
) );
1611 dc
.SetBrush(wxBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
), wxSOLID
));
1614 dc
.SetPen( *wxTRANSPARENT_PEN
);
1615 dc
.DrawRectangle(rect
);
1618 // ----------------------------------------------------------------------------
1619 // wxGridCellStringRenderer
1620 // ----------------------------------------------------------------------------
1622 void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid
& grid
,
1623 wxGridCellAttr
& attr
,
1627 dc
.SetBackgroundMode( wxTRANSPARENT
);
1629 // TODO some special colours for attr.IsReadOnly() case?
1631 // different coloured text when the grid is disabled
1632 if( grid
.IsEnabled() )
1636 dc
.SetTextBackground( grid
.GetSelectionBackground() );
1637 dc
.SetTextForeground( grid
.GetSelectionForeground() );
1641 dc
.SetTextBackground( attr
.GetBackgroundColour() );
1642 dc
.SetTextForeground( attr
.GetTextColour() );
1647 dc
.SetTextBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1648 dc
.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
));
1651 dc
.SetFont( attr
.GetFont() );
1654 wxSize
wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr
& attr
,
1656 const wxString
& text
)
1658 wxCoord x
= 0, y
= 0, max_x
= 0;
1659 dc
.SetFont(attr
.GetFont());
1660 wxStringTokenizer
tk(text
, _T('\n'));
1661 while ( tk
.HasMoreTokens() )
1663 dc
.GetTextExtent(tk
.GetNextToken(), &x
, &y
);
1664 max_x
= wxMax(max_x
, x
);
1667 y
*= 1 + text
.Freq(wxT('\n')); // multiply by the number of lines.
1669 return wxSize(max_x
, y
);
1672 wxSize
wxGridCellStringRenderer::GetBestSize(wxGrid
& grid
,
1673 wxGridCellAttr
& attr
,
1677 return DoGetBestSize(attr
, dc
, grid
.GetCellValue(row
, col
));
1680 void wxGridCellStringRenderer::Draw(wxGrid
& grid
,
1681 wxGridCellAttr
& attr
,
1683 const wxRect
& rectCell
,
1687 wxRect rect
= rectCell
;
1690 // erase only this cells background, overflow cells should have been erased
1691 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1694 attr
.GetAlignment(&hAlign
, &vAlign
);
1696 int overflowCols
= 0;
1698 if (attr
.GetOverflow())
1700 int cols
= grid
.GetNumberCols();
1701 int best_width
= GetBestSize(grid
,attr
,dc
,row
,col
).GetWidth();
1702 int cell_rows
, cell_cols
;
1703 attr
.GetSize( &cell_rows
, &cell_cols
); // shouldn't get here if <=0
1704 if ((best_width
> rectCell
.width
) && (col
< cols
) && grid
.GetTable())
1706 int i
, c_cols
, c_rows
;
1707 for (i
= col
+cell_cols
; i
< cols
; i
++)
1709 bool is_empty
= true;
1710 for (int j
=row
; j
<row
+cell_rows
; j
++)
1712 // check w/ anchor cell for multicell block
1713 grid
.GetCellSize(j
, i
, &c_rows
, &c_cols
);
1714 if (c_rows
> 0) c_rows
= 0;
1715 if (!grid
.GetTable()->IsEmptyCell(j
+c_rows
, i
))
1722 rect
.width
+= grid
.GetColSize(i
);
1728 if (rect
.width
>= best_width
) break;
1730 overflowCols
= i
- col
- cell_cols
+ 1;
1731 if (overflowCols
>= cols
) overflowCols
= cols
- 1;
1734 if (overflowCols
> 0) // redraw overflow cells w/ proper hilight
1736 hAlign
= wxALIGN_LEFT
; // if oveflowed then it's left aligned
1738 clip
.x
+= rectCell
.width
;
1739 // draw each overflow cell individually
1740 int col_end
= col
+cell_cols
+overflowCols
;
1741 if (col_end
>= grid
.GetNumberCols())
1742 col_end
= grid
.GetNumberCols() - 1;
1743 for (int i
= col
+cell_cols
; i
<= col_end
; i
++)
1745 clip
.width
= grid
.GetColSize(i
) - 1;
1746 dc
.DestroyClippingRegion();
1747 dc
.SetClippingRegion(clip
);
1749 SetTextColoursAndFont(grid
, attr
, dc
,
1750 grid
.IsInSelection(row
,i
));
1752 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1753 rect
, hAlign
, vAlign
);
1754 clip
.x
+= grid
.GetColSize(i
) - 1;
1760 dc
.DestroyClippingRegion();
1764 // now we only have to draw the text
1765 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1767 grid
.DrawTextRectangle(dc
, grid
.GetCellValue(row
, col
),
1768 rect
, hAlign
, vAlign
);
1771 // ----------------------------------------------------------------------------
1772 // wxGridCellNumberRenderer
1773 // ----------------------------------------------------------------------------
1775 wxString
wxGridCellNumberRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1777 wxGridTableBase
*table
= grid
.GetTable();
1779 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_NUMBER
) )
1781 text
.Printf(_T("%ld"), table
->GetValueAsLong(row
, col
));
1785 text
= table
->GetValue(row
, col
);
1791 void wxGridCellNumberRenderer::Draw(wxGrid
& grid
,
1792 wxGridCellAttr
& attr
,
1794 const wxRect
& rectCell
,
1798 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1800 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1802 // draw the text right aligned by default
1804 attr
.GetAlignment(&hAlign
, &vAlign
);
1805 hAlign
= wxALIGN_RIGHT
;
1807 wxRect rect
= rectCell
;
1810 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1813 wxSize
wxGridCellNumberRenderer::GetBestSize(wxGrid
& grid
,
1814 wxGridCellAttr
& attr
,
1818 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1821 // ----------------------------------------------------------------------------
1822 // wxGridCellFloatRenderer
1823 // ----------------------------------------------------------------------------
1825 wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width
, int precision
)
1828 SetPrecision(precision
);
1831 wxGridCellRenderer
*wxGridCellFloatRenderer::Clone() const
1833 wxGridCellFloatRenderer
*renderer
= new wxGridCellFloatRenderer
;
1834 renderer
->m_width
= m_width
;
1835 renderer
->m_precision
= m_precision
;
1836 renderer
->m_format
= m_format
;
1841 wxString
wxGridCellFloatRenderer::GetString(wxGrid
& grid
, int row
, int col
)
1843 wxGridTableBase
*table
= grid
.GetTable();
1848 if ( table
->CanGetValueAs(row
, col
, wxGRID_VALUE_FLOAT
) )
1850 val
= table
->GetValueAsDouble(row
, col
);
1855 text
= table
->GetValue(row
, col
);
1856 hasDouble
= text
.ToDouble(&val
);
1863 if ( m_width
== -1 )
1865 if ( m_precision
== -1 )
1867 // default width/precision
1868 m_format
= _T("%f");
1872 m_format
.Printf(_T("%%.%df"), m_precision
);
1875 else if ( m_precision
== -1 )
1877 // default precision
1878 m_format
.Printf(_T("%%%d.f"), m_width
);
1882 m_format
.Printf(_T("%%%d.%df"), m_width
, m_precision
);
1886 text
.Printf(m_format
, val
);
1889 //else: text already contains the string
1894 void wxGridCellFloatRenderer::Draw(wxGrid
& grid
,
1895 wxGridCellAttr
& attr
,
1897 const wxRect
& rectCell
,
1901 wxGridCellRenderer::Draw(grid
, attr
, dc
, rectCell
, row
, col
, isSelected
);
1903 SetTextColoursAndFont(grid
, attr
, dc
, isSelected
);
1905 // draw the text right aligned by default
1907 attr
.GetAlignment(&hAlign
, &vAlign
);
1908 hAlign
= wxALIGN_RIGHT
;
1910 wxRect rect
= rectCell
;
1913 grid
.DrawTextRectangle(dc
, GetString(grid
, row
, col
), rect
, hAlign
, vAlign
);
1916 wxSize
wxGridCellFloatRenderer::GetBestSize(wxGrid
& grid
,
1917 wxGridCellAttr
& attr
,
1921 return DoGetBestSize(attr
, dc
, GetString(grid
, row
, col
));
1924 void wxGridCellFloatRenderer::SetParameters(const wxString
& params
)
1928 // reset to defaults
1934 wxString tmp
= params
.BeforeFirst(_T(','));
1938 if ( tmp
.ToLong(&width
) )
1940 SetWidth((int)width
);
1944 wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params
.c_str());
1948 tmp
= params
.AfterFirst(_T(','));
1952 if ( tmp
.ToLong(&precision
) )
1954 SetPrecision((int)precision
);
1958 wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params
.c_str());
1966 // ----------------------------------------------------------------------------
1967 // wxGridCellBoolRenderer
1968 // ----------------------------------------------------------------------------
1970 wxSize
wxGridCellBoolRenderer::ms_sizeCheckMark
;
1972 // FIXME these checkbox size calculations are really ugly...
1974 // between checkmark and box
1975 static const wxCoord wxGRID_CHECKMARK_MARGIN
= 2;
1977 wxSize
wxGridCellBoolRenderer::GetBestSize(wxGrid
& grid
,
1978 wxGridCellAttr
& WXUNUSED(attr
),
1983 // compute it only once (no locks for MT safeness in GUI thread...)
1984 if ( !ms_sizeCheckMark
.x
)
1986 // get checkbox size
1987 wxCheckBox
*checkbox
= new wxCheckBox(&grid
, wxID_ANY
, wxEmptyString
);
1988 wxSize size
= checkbox
->GetBestSize();
1989 wxCoord checkSize
= size
.y
+ 2*wxGRID_CHECKMARK_MARGIN
;
1991 // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result
1992 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1993 checkSize
-= size
.y
/ 2;
1998 ms_sizeCheckMark
.x
= ms_sizeCheckMark
.y
= checkSize
;
2001 return ms_sizeCheckMark
;
2004 void wxGridCellBoolRenderer::Draw(wxGrid
& grid
,
2005 wxGridCellAttr
& attr
,
2011 wxGridCellRenderer::Draw(grid
, attr
, dc
, rect
, row
, col
, isSelected
);
2013 // draw a check mark in the centre (ignoring alignment - TODO)
2014 wxSize size
= GetBestSize(grid
, attr
, dc
, row
, col
);
2016 // don't draw outside the cell
2017 wxCoord minSize
= wxMin(rect
.width
, rect
.height
);
2018 if ( size
.x
>= minSize
|| size
.y
>= minSize
)
2020 // and even leave (at least) 1 pixel margin
2021 size
.x
= size
.y
= minSize
- 2;
2024 // draw a border around checkmark
2026 attr
.GetAlignment(& hAlign
, &vAlign
);
2029 if (hAlign
== wxALIGN_CENTRE
)
2031 rectBorder
.x
= rect
.x
+ rect
.width
/2 - size
.x
/2;
2032 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2033 rectBorder
.width
= size
.x
;
2034 rectBorder
.height
= size
.y
;
2036 else if (hAlign
== wxALIGN_LEFT
)
2038 rectBorder
.x
= rect
.x
+ 2;
2039 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2040 rectBorder
.width
= size
.x
;
2041 rectBorder
.height
= size
.y
;
2043 else if (hAlign
== wxALIGN_RIGHT
)
2045 rectBorder
.x
= rect
.x
+ rect
.width
- size
.x
- 2;
2046 rectBorder
.y
= rect
.y
+ rect
.height
/2 - size
.y
/2;
2047 rectBorder
.width
= size
.x
;
2048 rectBorder
.height
= size
.y
;
2052 if ( grid
.GetTable()->CanGetValueAs(row
, col
, wxGRID_VALUE_BOOL
) )
2053 value
= grid
.GetTable()->GetValueAsBool(row
, col
);
2056 wxString
cellval( grid
.GetTable()->GetValue(row
, col
) );
2057 value
= !( !cellval
|| (cellval
== wxT("0")) );
2062 wxRect rectMark
= rectBorder
;
2064 // MSW DrawCheckMark() is weird (and should probably be changed...)
2065 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
/2);
2069 rectMark
.Inflate(-wxGRID_CHECKMARK_MARGIN
);
2072 dc
.SetTextForeground(attr
.GetTextColour());
2073 dc
.DrawCheckMark(rectMark
);
2076 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2077 dc
.SetPen(wxPen(attr
.GetTextColour(), 1, wxSOLID
));
2078 dc
.DrawRectangle(rectBorder
);
2081 // ----------------------------------------------------------------------------
2083 // ----------------------------------------------------------------------------
2085 void wxGridCellAttr::Init(wxGridCellAttr
*attrDefault
)
2089 m_isReadOnly
= Unset
;
2094 m_attrkind
= wxGridCellAttr::Cell
;
2096 m_sizeRows
= m_sizeCols
= 1;
2097 m_overflow
= UnsetOverflow
;
2099 SetDefAttr(attrDefault
);
2102 wxGridCellAttr
*wxGridCellAttr::Clone() const
2104 wxGridCellAttr
*attr
= new wxGridCellAttr(m_defGridAttr
);
2106 if ( HasTextColour() )
2107 attr
->SetTextColour(GetTextColour());
2108 if ( HasBackgroundColour() )
2109 attr
->SetBackgroundColour(GetBackgroundColour());
2111 attr
->SetFont(GetFont());
2112 if ( HasAlignment() )
2113 attr
->SetAlignment(m_hAlign
, m_vAlign
);
2115 attr
->SetSize( m_sizeRows
, m_sizeCols
);
2119 attr
->SetRenderer(m_renderer
);
2120 m_renderer
->IncRef();
2124 attr
->SetEditor(m_editor
);
2129 attr
->SetReadOnly();
2131 attr
->SetKind( m_attrkind
);
2136 void wxGridCellAttr::MergeWith(wxGridCellAttr
*mergefrom
)
2138 if ( !HasTextColour() && mergefrom
->HasTextColour() )
2139 SetTextColour(mergefrom
->GetTextColour());
2140 if ( !HasBackgroundColour() && mergefrom
->HasBackgroundColour() )
2141 SetBackgroundColour(mergefrom
->GetBackgroundColour());
2142 if ( !HasFont() && mergefrom
->HasFont() )
2143 SetFont(mergefrom
->GetFont());
2144 if ( !HasAlignment() && mergefrom
->HasAlignment() ){
2146 mergefrom
->GetAlignment( &hAlign
, &vAlign
);
2147 SetAlignment(hAlign
, vAlign
);
2150 mergefrom
->GetSize( &m_sizeRows
, &m_sizeCols
);
2152 // Directly access member functions as GetRender/Editor don't just return
2153 // m_renderer/m_editor
2155 // Maybe add support for merge of Render and Editor?
2156 if (!HasRenderer() && mergefrom
->HasRenderer() )
2158 m_renderer
= mergefrom
->m_renderer
;
2159 m_renderer
->IncRef();
2161 if ( !HasEditor() && mergefrom
->HasEditor() )
2163 m_editor
= mergefrom
->m_editor
;
2166 if ( !HasReadWriteMode() && mergefrom
->HasReadWriteMode() )
2167 SetReadOnly(mergefrom
->IsReadOnly());
2169 if (!HasOverflowMode() && mergefrom
->HasOverflowMode() )
2170 SetOverflow(mergefrom
->GetOverflow());
2172 SetDefAttr(mergefrom
->m_defGridAttr
);
2175 void wxGridCellAttr::SetSize(int num_rows
, int num_cols
)
2177 // The size of a cell is normally 1,1
2179 // If this cell is larger (2,2) then this is the top left cell
2180 // the other cells that will be covered (lower right cells) must be
2181 // set to negative or zero values such that
2182 // row + num_rows of the covered cell points to the larger cell (this cell)
2183 // same goes for the col + num_cols.
2185 // Size of 0,0 is NOT valid, neither is <=0 and any positive value
2187 wxASSERT_MSG( (!((num_rows
>0)&&(num_cols
<=0)) ||
2188 !((num_rows
<=0)&&(num_cols
>0)) ||
2189 !((num_rows
==0)&&(num_cols
==0))),
2190 wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values"));
2192 m_sizeRows
= num_rows
;
2193 m_sizeCols
= num_cols
;
2196 const wxColour
& wxGridCellAttr::GetTextColour() const
2198 if (HasTextColour())
2202 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2204 return m_defGridAttr
->GetTextColour();
2208 wxFAIL_MSG(wxT("Missing default cell attribute"));
2209 return wxNullColour
;
2214 const wxColour
& wxGridCellAttr::GetBackgroundColour() const
2216 if (HasBackgroundColour())
2218 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2219 return m_defGridAttr
->GetBackgroundColour();
2222 wxFAIL_MSG(wxT("Missing default cell attribute"));
2223 return wxNullColour
;
2228 const wxFont
& wxGridCellAttr::GetFont() const
2232 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2233 return m_defGridAttr
->GetFont();
2236 wxFAIL_MSG(wxT("Missing default cell attribute"));
2242 void wxGridCellAttr::GetAlignment(int *hAlign
, int *vAlign
) const
2246 if ( hAlign
) *hAlign
= m_hAlign
;
2247 if ( vAlign
) *vAlign
= m_vAlign
;
2249 else if (m_defGridAttr
&& m_defGridAttr
!= this)
2250 m_defGridAttr
->GetAlignment(hAlign
, vAlign
);
2253 wxFAIL_MSG(wxT("Missing default cell attribute"));
2257 void wxGridCellAttr::GetSize( int *num_rows
, int *num_cols
) const
2259 if ( num_rows
) *num_rows
= m_sizeRows
;
2260 if ( num_cols
) *num_cols
= m_sizeCols
;
2263 // GetRenderer and GetEditor use a slightly different decision path about
2264 // which attribute to use. If a non-default attr object has one then it is
2265 // used, otherwise the default editor or renderer is fetched from the grid and
2266 // used. It should be the default for the data type of the cell. If it is
2267 // NULL (because the table has a type that the grid does not have in its
2268 // registry,) then the grid's default editor or renderer is used.
2270 wxGridCellRenderer
* wxGridCellAttr::GetRenderer(wxGrid
* grid
, int row
, int col
) const
2272 wxGridCellRenderer
*renderer
;
2274 if ( m_renderer
&& this != m_defGridAttr
)
2276 // use the cells renderer if it has one
2277 renderer
= m_renderer
;
2280 else // no non default cell renderer
2282 // get default renderer for the data type
2285 // GetDefaultRendererForCell() will do IncRef() for us
2286 renderer
= grid
->GetDefaultRendererForCell(row
, col
);
2295 if (m_defGridAttr
&& this != m_defGridAttr
)
2297 // if we still don't have one then use the grid default
2298 // (no need for IncRef() here neither)
2299 renderer
= m_defGridAttr
->GetRenderer(NULL
, 0, 0);
2301 else // default grid attr
2303 // use m_renderer which we had decided not to use initially
2304 renderer
= m_renderer
;
2311 // we're supposed to always find something
2312 wxASSERT_MSG(renderer
, wxT("Missing default cell renderer"));
2317 // same as above, except for s/renderer/editor/g
2318 wxGridCellEditor
* wxGridCellAttr::GetEditor(wxGrid
* grid
, int row
, int col
) const
2320 wxGridCellEditor
*editor
;
2322 if ( m_editor
&& this != m_defGridAttr
)
2324 // use the cells editor if it has one
2328 else // no non default cell editor
2330 // get default editor for the data type
2333 // GetDefaultEditorForCell() will do IncRef() for us
2334 editor
= grid
->GetDefaultEditorForCell(row
, col
);
2343 if ( m_defGridAttr
&& this != m_defGridAttr
)
2345 // if we still don't have one then use the grid default
2346 // (no need for IncRef() here neither)
2347 editor
= m_defGridAttr
->GetEditor(NULL
, 0, 0);
2349 else // default grid attr
2351 // use m_editor which we had decided not to use initially
2359 // we're supposed to always find something
2360 wxASSERT_MSG(editor
, wxT("Missing default cell editor"));
2365 // ----------------------------------------------------------------------------
2366 // wxGridCellAttrData
2367 // ----------------------------------------------------------------------------
2369 void wxGridCellAttrData::SetAttr(wxGridCellAttr
*attr
, int row
, int col
)
2371 int n
= FindIndex(row
, col
);
2372 if ( n
== wxNOT_FOUND
)
2374 // add the attribute
2375 m_attrs
.Add(new wxGridCellWithAttr(row
, col
, attr
));
2379 // free the old attribute
2380 m_attrs
[(size_t)n
].attr
->DecRef();
2384 // change the attribute
2385 m_attrs
[(size_t)n
].attr
= attr
;
2389 // remove this attribute
2390 m_attrs
.RemoveAt((size_t)n
);
2395 wxGridCellAttr
*wxGridCellAttrData::GetAttr(int row
, int col
) const
2397 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2399 int n
= FindIndex(row
, col
);
2400 if ( n
!= wxNOT_FOUND
)
2402 attr
= m_attrs
[(size_t)n
].attr
;
2409 void wxGridCellAttrData::UpdateAttrRows( size_t pos
, int numRows
)
2411 size_t count
= m_attrs
.GetCount();
2412 for ( size_t n
= 0; n
< count
; n
++ )
2414 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2415 wxCoord row
= coords
.GetRow();
2416 if ((size_t)row
>= pos
)
2420 // If rows inserted, include row counter where necessary
2421 coords
.SetRow(row
+ numRows
);
2423 else if (numRows
< 0)
2425 // If rows deleted ...
2426 if ((size_t)row
>= pos
- numRows
)
2428 // ...either decrement row counter (if row still exists)...
2429 coords
.SetRow(row
+ numRows
);
2433 // ...or remove the attribute
2434 m_attrs
[n
].attr
->DecRef();
2435 m_attrs
.RemoveAt(n
);
2443 void wxGridCellAttrData::UpdateAttrCols( size_t pos
, int numCols
)
2445 size_t count
= m_attrs
.GetCount();
2446 for ( size_t n
= 0; n
< count
; n
++ )
2448 wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2449 wxCoord col
= coords
.GetCol();
2450 if ( (size_t)col
>= pos
)
2454 // If rows inserted, include row counter where necessary
2455 coords
.SetCol(col
+ numCols
);
2457 else if (numCols
< 0)
2459 // If rows deleted ...
2460 if ((size_t)col
>= pos
- numCols
)
2462 // ...either decrement row counter (if row still exists)...
2463 coords
.SetCol(col
+ numCols
);
2467 // ...or remove the attribute
2468 m_attrs
[n
].attr
->DecRef();
2469 m_attrs
.RemoveAt(n
);
2477 int wxGridCellAttrData::FindIndex(int row
, int col
) const
2479 size_t count
= m_attrs
.GetCount();
2480 for ( size_t n
= 0; n
< count
; n
++ )
2482 const wxGridCellCoords
& coords
= m_attrs
[n
].coords
;
2483 if ( (coords
.GetRow() == row
) && (coords
.GetCol() == col
) )
2492 // ----------------------------------------------------------------------------
2493 // wxGridRowOrColAttrData
2494 // ----------------------------------------------------------------------------
2496 wxGridRowOrColAttrData::~wxGridRowOrColAttrData()
2498 size_t count
= m_attrs
.Count();
2499 for ( size_t n
= 0; n
< count
; n
++ )
2501 m_attrs
[n
]->DecRef();
2505 wxGridCellAttr
*wxGridRowOrColAttrData::GetAttr(int rowOrCol
) const
2507 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2509 int n
= m_rowsOrCols
.Index(rowOrCol
);
2510 if ( n
!= wxNOT_FOUND
)
2512 attr
= m_attrs
[(size_t)n
];
2519 void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr
*attr
, int rowOrCol
)
2521 int i
= m_rowsOrCols
.Index(rowOrCol
);
2522 if ( i
== wxNOT_FOUND
)
2524 // add the attribute
2525 m_rowsOrCols
.Add(rowOrCol
);
2530 size_t n
= (size_t)i
;
2533 // change the attribute
2534 m_attrs
[n
]->DecRef();
2539 // remove this attribute
2540 m_attrs
[n
]->DecRef();
2541 m_rowsOrCols
.RemoveAt(n
);
2542 m_attrs
.RemoveAt(n
);
2547 void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos
, int numRowsOrCols
)
2549 size_t count
= m_attrs
.GetCount();
2550 for ( size_t n
= 0; n
< count
; n
++ )
2552 int & rowOrCol
= m_rowsOrCols
[n
];
2553 if ( (size_t)rowOrCol
>= pos
)
2555 if ( numRowsOrCols
> 0 )
2557 // If rows inserted, include row counter where necessary
2558 rowOrCol
+= numRowsOrCols
;
2560 else if ( numRowsOrCols
< 0)
2562 // If rows deleted, either decrement row counter (if row still exists)
2563 if ((size_t)rowOrCol
>= pos
- numRowsOrCols
)
2564 rowOrCol
+= numRowsOrCols
;
2567 m_rowsOrCols
.RemoveAt(n
);
2568 m_attrs
[n
]->DecRef();
2569 m_attrs
.RemoveAt(n
);
2577 // ----------------------------------------------------------------------------
2578 // wxGridCellAttrProvider
2579 // ----------------------------------------------------------------------------
2581 wxGridCellAttrProvider::wxGridCellAttrProvider()
2583 m_data
= (wxGridCellAttrProviderData
*)NULL
;
2586 wxGridCellAttrProvider::~wxGridCellAttrProvider()
2591 void wxGridCellAttrProvider::InitData()
2593 m_data
= new wxGridCellAttrProviderData
;
2596 wxGridCellAttr
*wxGridCellAttrProvider::GetAttr(int row
, int col
,
2597 wxGridCellAttr::wxAttrKind kind
) const
2599 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
2604 case (wxGridCellAttr::Any
):
2605 //Get cached merge attributes.
2606 // Currenlty not used as no cache implemented as not mutiable
2607 // attr = m_data->m_mergeAttr.GetAttr(row, col);
2610 //Basicaly implement old version.
2611 //Also check merge cache, so we don't have to re-merge every time..
2612 wxGridCellAttr
*attrcell
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2613 wxGridCellAttr
*attrrow
= m_data
->m_rowAttrs
.GetAttr(row
);
2614 wxGridCellAttr
*attrcol
= m_data
->m_colAttrs
.GetAttr(col
);
2616 if((attrcell
!= attrrow
) && (attrrow
!= attrcol
) && (attrcell
!= attrcol
)){
2617 // Two or more are non NULL
2618 attr
= new wxGridCellAttr
;
2619 attr
->SetKind(wxGridCellAttr::Merged
);
2623 attr
->MergeWith(attrcell
);
2627 attr
->MergeWith(attrcol
);
2631 attr
->MergeWith(attrrow
);
2634 //store merge attr if cache implemented
2636 //m_data->m_mergeAttr.SetAttr(attr, row, col);
2640 // one or none is non null return it or null.
2641 if(attrrow
) attr
= attrrow
;
2657 case (wxGridCellAttr::Cell
):
2658 attr
= m_data
->m_cellAttrs
.GetAttr(row
, col
);
2660 case (wxGridCellAttr::Col
):
2661 attr
= m_data
->m_colAttrs
.GetAttr(col
);
2663 case (wxGridCellAttr::Row
):
2664 attr
= m_data
->m_rowAttrs
.GetAttr(row
);
2668 // (wxGridCellAttr::Default):
2669 // (wxGridCellAttr::Merged):
2676 void wxGridCellAttrProvider::SetAttr(wxGridCellAttr
*attr
,
2682 m_data
->m_cellAttrs
.SetAttr(attr
, row
, col
);
2685 void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2690 m_data
->m_rowAttrs
.SetAttr(attr
, row
);
2693 void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr
*attr
, int col
)
2698 m_data
->m_colAttrs
.SetAttr(attr
, col
);
2701 void wxGridCellAttrProvider::UpdateAttrRows( size_t pos
, int numRows
)
2705 m_data
->m_cellAttrs
.UpdateAttrRows( pos
, numRows
);
2707 m_data
->m_rowAttrs
.UpdateAttrRowsOrCols( pos
, numRows
);
2711 void wxGridCellAttrProvider::UpdateAttrCols( size_t pos
, int numCols
)
2715 m_data
->m_cellAttrs
.UpdateAttrCols( pos
, numCols
);
2717 m_data
->m_colAttrs
.UpdateAttrRowsOrCols( pos
, numCols
);
2721 // ----------------------------------------------------------------------------
2722 // wxGridTypeRegistry
2723 // ----------------------------------------------------------------------------
2725 wxGridTypeRegistry::~wxGridTypeRegistry()
2727 size_t count
= m_typeinfo
.Count();
2728 for ( size_t i
= 0; i
< count
; i
++ )
2729 delete m_typeinfo
[i
];
2733 void wxGridTypeRegistry::RegisterDataType(const wxString
& typeName
,
2734 wxGridCellRenderer
* renderer
,
2735 wxGridCellEditor
* editor
)
2737 wxGridDataTypeInfo
* info
= new wxGridDataTypeInfo(typeName
, renderer
, editor
);
2739 // is it already registered?
2740 int loc
= FindRegisteredDataType(typeName
);
2741 if ( loc
!= wxNOT_FOUND
)
2743 delete m_typeinfo
[loc
];
2744 m_typeinfo
[loc
] = info
;
2748 m_typeinfo
.Add(info
);
2752 int wxGridTypeRegistry::FindRegisteredDataType(const wxString
& typeName
)
2754 size_t count
= m_typeinfo
.GetCount();
2755 for ( size_t i
= 0; i
< count
; i
++ )
2757 if ( typeName
== m_typeinfo
[i
]->m_typeName
)
2766 int wxGridTypeRegistry::FindDataType(const wxString
& typeName
)
2768 int index
= FindRegisteredDataType(typeName
);
2769 if ( index
== wxNOT_FOUND
)
2771 // check whether this is one of the standard ones, in which case
2772 // register it "on the fly"
2774 if ( typeName
== wxGRID_VALUE_STRING
)
2776 RegisterDataType(wxGRID_VALUE_STRING
,
2777 new wxGridCellStringRenderer
,
2778 new wxGridCellTextEditor
);
2780 #endif // wxUSE_TEXTCTRL
2782 if ( typeName
== wxGRID_VALUE_BOOL
)
2784 RegisterDataType(wxGRID_VALUE_BOOL
,
2785 new wxGridCellBoolRenderer
,
2786 new wxGridCellBoolEditor
);
2788 #endif // wxUSE_CHECKBOX
2790 if ( typeName
== wxGRID_VALUE_NUMBER
)
2792 RegisterDataType(wxGRID_VALUE_NUMBER
,
2793 new wxGridCellNumberRenderer
,
2794 new wxGridCellNumberEditor
);
2796 else if ( typeName
== wxGRID_VALUE_FLOAT
)
2798 RegisterDataType(wxGRID_VALUE_FLOAT
,
2799 new wxGridCellFloatRenderer
,
2800 new wxGridCellFloatEditor
);
2802 #endif // wxUSE_TEXTCTRL
2804 if ( typeName
== wxGRID_VALUE_CHOICE
)
2806 RegisterDataType(wxGRID_VALUE_CHOICE
,
2807 new wxGridCellStringRenderer
,
2808 new wxGridCellChoiceEditor
);
2810 #endif // wxUSE_COMBOBOX
2815 // we get here only if just added the entry for this type, so return
2817 index
= m_typeinfo
.GetCount() - 1;
2823 int wxGridTypeRegistry::FindOrCloneDataType(const wxString
& typeName
)
2825 int index
= FindDataType(typeName
);
2826 if ( index
== wxNOT_FOUND
)
2828 // the first part of the typename is the "real" type, anything after ':'
2829 // are the parameters for the renderer
2830 index
= FindDataType(typeName
.BeforeFirst(_T(':')));
2831 if ( index
== wxNOT_FOUND
)
2836 wxGridCellRenderer
*renderer
= GetRenderer(index
);
2837 wxGridCellRenderer
*rendererOld
= renderer
;
2838 renderer
= renderer
->Clone();
2839 rendererOld
->DecRef();
2841 wxGridCellEditor
*editor
= GetEditor(index
);
2842 wxGridCellEditor
*editorOld
= editor
;
2843 editor
= editor
->Clone();
2844 editorOld
->DecRef();
2846 // do it even if there are no parameters to reset them to defaults
2847 wxString params
= typeName
.AfterFirst(_T(':'));
2848 renderer
->SetParameters(params
);
2849 editor
->SetParameters(params
);
2851 // register the new typename
2852 RegisterDataType(typeName
, renderer
, editor
);
2854 // we just registered it, it's the last one
2855 index
= m_typeinfo
.GetCount() - 1;
2861 wxGridCellRenderer
* wxGridTypeRegistry::GetRenderer(int index
)
2863 wxGridCellRenderer
* renderer
= m_typeinfo
[index
]->m_renderer
;
2869 wxGridCellEditor
* wxGridTypeRegistry::GetEditor(int index
)
2871 wxGridCellEditor
* editor
= m_typeinfo
[index
]->m_editor
;
2877 // ----------------------------------------------------------------------------
2879 // ----------------------------------------------------------------------------
2881 IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase
, wxObject
)
2884 wxGridTableBase::wxGridTableBase()
2886 m_view
= (wxGrid
*) NULL
;
2887 m_attrProvider
= (wxGridCellAttrProvider
*) NULL
;
2890 wxGridTableBase::~wxGridTableBase()
2892 delete m_attrProvider
;
2895 void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider
*attrProvider
)
2897 delete m_attrProvider
;
2898 m_attrProvider
= attrProvider
;
2901 bool wxGridTableBase::CanHaveAttributes()
2903 if ( ! GetAttrProvider() )
2905 // use the default attr provider by default
2906 SetAttrProvider(new wxGridCellAttrProvider
);
2911 wxGridCellAttr
*wxGridTableBase::GetAttr(int row
, int col
, wxGridCellAttr::wxAttrKind kind
)
2913 if ( m_attrProvider
)
2914 return m_attrProvider
->GetAttr(row
, col
, kind
);
2916 return (wxGridCellAttr
*)NULL
;
2919 void wxGridTableBase::SetAttr(wxGridCellAttr
* attr
, int row
, int col
)
2921 if ( m_attrProvider
)
2923 attr
->SetKind(wxGridCellAttr::Cell
);
2924 m_attrProvider
->SetAttr(attr
, row
, col
);
2928 // as we take ownership of the pointer and don't store it, we must
2934 void wxGridTableBase::SetRowAttr(wxGridCellAttr
*attr
, int row
)
2936 if ( m_attrProvider
)
2938 attr
->SetKind(wxGridCellAttr::Row
);
2939 m_attrProvider
->SetRowAttr(attr
, row
);
2943 // as we take ownership of the pointer and don't store it, we must
2949 void wxGridTableBase::SetColAttr(wxGridCellAttr
*attr
, int col
)
2951 if ( m_attrProvider
)
2953 attr
->SetKind(wxGridCellAttr::Col
);
2954 m_attrProvider
->SetColAttr(attr
, col
);
2958 // as we take ownership of the pointer and don't store it, we must
2964 bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos
),
2965 size_t WXUNUSED(numRows
) )
2967 wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") );
2972 bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows
) )
2974 wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function"));
2979 bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos
),
2980 size_t WXUNUSED(numRows
) )
2982 wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function"));
2987 bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos
),
2988 size_t WXUNUSED(numCols
) )
2990 wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function"));
2995 bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols
) )
2997 wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function"));
3002 bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos
),
3003 size_t WXUNUSED(numCols
) )
3005 wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function"));
3011 wxString
wxGridTableBase::GetRowLabelValue( int row
)
3014 s
<< row
+ 1; // RD: Starting the rows at zero confuses users, no matter
3015 // how much it makes sense to us geeks.
3019 wxString
wxGridTableBase::GetColLabelValue( int col
)
3021 // default col labels are:
3022 // cols 0 to 25 : A-Z
3023 // cols 26 to 675 : AA-ZZ
3028 for ( n
= 1; ; n
++ )
3030 s
+= (wxChar
) (_T('A') + (wxChar
)( col%26
));
3032 if ( col
< 0 ) break;
3035 // reverse the string...
3037 for ( i
= 0; i
< n
; i
++ )
3046 wxString
wxGridTableBase::GetTypeName( int WXUNUSED(row
), int WXUNUSED(col
) )
3048 return wxGRID_VALUE_STRING
;
3051 bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row
), int WXUNUSED(col
),
3052 const wxString
& typeName
)
3054 return typeName
== wxGRID_VALUE_STRING
;
3057 bool wxGridTableBase::CanSetValueAs( int row
, int col
, const wxString
& typeName
)
3059 return CanGetValueAs(row
, col
, typeName
);
3062 long wxGridTableBase::GetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
) )
3067 double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
) )
3072 bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
) )
3077 void wxGridTableBase::SetValueAsLong( int WXUNUSED(row
), int WXUNUSED(col
),
3078 long WXUNUSED(value
) )
3082 void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row
), int WXUNUSED(col
),
3083 double WXUNUSED(value
) )
3087 void wxGridTableBase::SetValueAsBool( int WXUNUSED(row
), int WXUNUSED(col
),
3088 bool WXUNUSED(value
) )
3093 void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3094 const wxString
& WXUNUSED(typeName
) )
3099 void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row
), int WXUNUSED(col
),
3100 const wxString
& WXUNUSED(typeName
),
3101 void* WXUNUSED(value
) )
3105 //////////////////////////////////////////////////////////////////////
3107 // Message class for the grid table to send requests and notifications
3111 wxGridTableMessage::wxGridTableMessage()
3113 m_table
= (wxGridTableBase
*) NULL
;
3119 wxGridTableMessage::wxGridTableMessage( wxGridTableBase
*table
, int id
,
3120 int commandInt1
, int commandInt2
)
3124 m_comInt1
= commandInt1
;
3125 m_comInt2
= commandInt2
;
3130 //////////////////////////////////////////////////////////////////////
3132 // A basic grid table for string data. An object of this class will
3133 // created by wxGrid if you don't specify an alternative table class.
3136 WX_DEFINE_OBJARRAY(wxGridStringArray
)
3138 IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable
, wxGridTableBase
)
3140 wxGridStringTable::wxGridStringTable()
3145 wxGridStringTable::wxGridStringTable( int numRows
, int numCols
)
3148 m_data
.Alloc( numRows
);
3151 sa
.Alloc( numCols
);
3152 sa
.Add( wxEmptyString
, numCols
);
3154 m_data
.Add( sa
, numRows
);
3157 wxGridStringTable::~wxGridStringTable()
3161 int wxGridStringTable::GetNumberRows()
3163 return m_data
.GetCount();
3166 int wxGridStringTable::GetNumberCols()
3168 if ( m_data
.GetCount() > 0 )
3169 return m_data
[0].GetCount();
3174 wxString
wxGridStringTable::GetValue( int row
, int col
)
3176 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3178 _T("invalid row or column index in wxGridStringTable") );
3180 return m_data
[row
][col
];
3183 void wxGridStringTable::SetValue( int row
, int col
, const wxString
& value
)
3185 wxCHECK_RET( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3186 _T("invalid row or column index in wxGridStringTable") );
3188 m_data
[row
][col
] = value
;
3191 bool wxGridStringTable::IsEmptyCell( int row
, int col
)
3193 wxCHECK_MSG( (row
< GetNumberRows()) && (col
< GetNumberCols()),
3195 _T("invalid row or column index in wxGridStringTable") );
3197 return (m_data
[row
][col
] == wxEmptyString
);
3200 void wxGridStringTable::Clear()
3203 int numRows
, numCols
;
3205 numRows
= m_data
.GetCount();
3208 numCols
= m_data
[0].GetCount();
3210 for ( row
= 0; row
< numRows
; row
++ )
3212 for ( col
= 0; col
< numCols
; col
++ )
3214 m_data
[row
][col
] = wxEmptyString
;
3221 bool wxGridStringTable::InsertRows( size_t pos
, size_t numRows
)
3223 size_t curNumRows
= m_data
.GetCount();
3224 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3225 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3227 if ( pos
>= curNumRows
)
3229 return AppendRows( numRows
);
3233 sa
.Alloc( curNumCols
);
3234 sa
.Add( wxEmptyString
, curNumCols
);
3235 m_data
.Insert( sa
, pos
, numRows
);
3238 wxGridTableMessage
msg( this,
3239 wxGRIDTABLE_NOTIFY_ROWS_INSERTED
,
3243 GetView()->ProcessTableMessage( msg
);
3249 bool wxGridStringTable::AppendRows( size_t numRows
)
3251 size_t curNumRows
= m_data
.GetCount();
3252 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3253 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3256 if ( curNumCols
> 0 )
3258 sa
.Alloc( curNumCols
);
3259 sa
.Add( wxEmptyString
, curNumCols
);
3262 m_data
.Add( sa
, numRows
);
3266 wxGridTableMessage
msg( this,
3267 wxGRIDTABLE_NOTIFY_ROWS_APPENDED
,
3270 GetView()->ProcessTableMessage( msg
);
3276 bool wxGridStringTable::DeleteRows( size_t pos
, size_t numRows
)
3278 size_t curNumRows
= m_data
.GetCount();
3280 if ( pos
>= curNumRows
)
3282 wxFAIL_MSG( wxString::Format
3284 wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"),
3286 (unsigned long)numRows
,
3287 (unsigned long)curNumRows
3293 if ( numRows
> curNumRows
- pos
)
3295 numRows
= curNumRows
- pos
;
3298 if ( numRows
>= curNumRows
)
3304 m_data
.RemoveAt( pos
, numRows
);
3308 wxGridTableMessage
msg( this,
3309 wxGRIDTABLE_NOTIFY_ROWS_DELETED
,
3313 GetView()->ProcessTableMessage( msg
);
3319 bool wxGridStringTable::InsertCols( size_t pos
, size_t numCols
)
3323 size_t curNumRows
= m_data
.GetCount();
3324 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3325 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3327 if ( pos
>= curNumCols
)
3329 return AppendCols( numCols
);
3332 for ( row
= 0; row
< curNumRows
; row
++ )
3334 for ( col
= pos
; col
< pos
+ numCols
; col
++ )
3336 m_data
[row
].Insert( wxEmptyString
, col
);
3341 wxGridTableMessage
msg( this,
3342 wxGRIDTABLE_NOTIFY_COLS_INSERTED
,
3346 GetView()->ProcessTableMessage( msg
);
3352 bool wxGridStringTable::AppendCols( size_t numCols
)
3356 size_t curNumRows
= m_data
.GetCount();
3360 // TODO: something better than this ?
3362 wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") );
3367 for ( row
= 0; row
< curNumRows
; row
++ )
3369 m_data
[row
].Add( wxEmptyString
, numCols
);
3374 wxGridTableMessage
msg( this,
3375 wxGRIDTABLE_NOTIFY_COLS_APPENDED
,
3378 GetView()->ProcessTableMessage( msg
);
3384 bool wxGridStringTable::DeleteCols( size_t pos
, size_t numCols
)
3388 size_t curNumRows
= m_data
.GetCount();
3389 size_t curNumCols
= ( curNumRows
> 0 ? m_data
[0].GetCount() :
3390 ( GetView() ? GetView()->GetNumberCols() : 0 ) );
3392 if ( pos
>= curNumCols
)
3394 wxFAIL_MSG( wxString::Format
3396 wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"),
3398 (unsigned long)numCols
,
3399 (unsigned long)curNumCols
3404 if ( numCols
> curNumCols
- pos
)
3406 numCols
= curNumCols
- pos
;
3409 for ( row
= 0; row
< curNumRows
; row
++ )
3411 if ( numCols
>= curNumCols
)
3413 m_data
[row
].Clear();
3417 m_data
[row
].RemoveAt( pos
, numCols
);
3422 wxGridTableMessage
msg( this,
3423 wxGRIDTABLE_NOTIFY_COLS_DELETED
,
3427 GetView()->ProcessTableMessage( msg
);
3433 wxString
wxGridStringTable::GetRowLabelValue( int row
)
3435 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3437 // using default label
3439 return wxGridTableBase::GetRowLabelValue( row
);
3443 return m_rowLabels
[ row
];
3447 wxString
wxGridStringTable::GetColLabelValue( int col
)
3449 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3451 // using default label
3453 return wxGridTableBase::GetColLabelValue( col
);
3457 return m_colLabels
[ col
];
3461 void wxGridStringTable::SetRowLabelValue( int row
, const wxString
& value
)
3463 if ( row
> (int)(m_rowLabels
.GetCount()) - 1 )
3465 int n
= m_rowLabels
.GetCount();
3467 for ( i
= n
; i
<= row
; i
++ )
3469 m_rowLabels
.Add( wxGridTableBase::GetRowLabelValue(i
) );
3473 m_rowLabels
[row
] = value
;
3476 void wxGridStringTable::SetColLabelValue( int col
, const wxString
& value
)
3478 if ( col
> (int)(m_colLabels
.GetCount()) - 1 )
3480 int n
= m_colLabels
.GetCount();
3482 for ( i
= n
; i
<= col
; i
++ )
3484 m_colLabels
.Add( wxGridTableBase::GetColLabelValue(i
) );
3488 m_colLabels
[col
] = value
;
3493 //////////////////////////////////////////////////////////////////////
3494 //////////////////////////////////////////////////////////////////////
3496 IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow
, wxWindow
)
3498 BEGIN_EVENT_TABLE( wxGridRowLabelWindow
, wxWindow
)
3499 EVT_PAINT( wxGridRowLabelWindow::OnPaint
)
3500 EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel
)
3501 EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent
)
3502 EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown
)
3503 EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp
)
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();
3560 //////////////////////////////////////////////////////////////////////
3562 IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow
, wxWindow
)
3564 BEGIN_EVENT_TABLE( wxGridColLabelWindow
, wxWindow
)
3565 EVT_PAINT( wxGridColLabelWindow::OnPaint
)
3566 EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel
)
3567 EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent
)
3568 EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown
)
3569 EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp
)
3572 wxGridColLabelWindow::wxGridColLabelWindow( wxGrid
*parent
,
3574 const wxPoint
&pos
, const wxSize
&size
)
3575 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3580 void wxGridColLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3584 // NO - don't do this because it will set both the x and y origin
3585 // coords to match the parent scrolled window and we just want to
3586 // set the x coord - MB
3588 // m_owner->PrepareDC( dc );
3591 m_owner
->CalcUnscrolledPosition( 0, 0, &x
, &y
);
3592 dc
.SetDeviceOrigin( -x
, 0 );
3594 wxArrayInt cols
= m_owner
->CalcColLabelsExposed( GetUpdateRegion() );
3595 m_owner
->DrawColLabels( dc
, cols
);
3599 void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3601 m_owner
->ProcessColLabelMouseEvent( event
);
3604 void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3606 m_owner
->GetEventHandler()->ProcessEvent(event
);
3610 // This seems to be required for wxMotif otherwise the mouse
3611 // cursor must be in the cell edit control to get key events
3613 void wxGridColLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3615 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3618 void wxGridColLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3620 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3625 //////////////////////////////////////////////////////////////////////
3627 IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow
, wxWindow
)
3629 BEGIN_EVENT_TABLE( wxGridCornerLabelWindow
, wxWindow
)
3630 EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel
)
3631 EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent
)
3632 EVT_PAINT( wxGridCornerLabelWindow::OnPaint
)
3633 EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown
)
3634 EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp
)
3637 wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid
*parent
,
3639 const wxPoint
&pos
, const wxSize
&size
)
3640 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
|wxFULL_REPAINT_ON_RESIZE
)
3645 void wxGridCornerLabelWindow::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
3649 int client_height
= 0;
3650 int client_width
= 0;
3651 GetClientSize( &client_width
, &client_height
);
3657 rect
.SetWidth( client_width
- 2 );
3658 rect
.SetHeight( client_height
- 2 );
3660 wxRendererNative::Get().DrawHeaderButton( this, dc
, rect
, 0 );
3662 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
3663 dc
.DrawLine( client_width
-1, client_height
-1, client_width
-1, 0 );
3664 dc
.DrawLine( client_width
-1, client_height
-1, 0, client_height
-1 );
3665 dc
.DrawLine( 0, 0, client_width
, 0 );
3666 dc
.DrawLine( 0, 0, 0, client_height
);
3668 dc
.SetPen( *wxWHITE_PEN
);
3669 dc
.DrawLine( 1, 1, client_width
-1, 1 );
3670 dc
.DrawLine( 1, 1, 1, client_height
-1 );
3675 void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent
& event
)
3677 m_owner
->ProcessCornerLabelMouseEvent( event
);
3681 void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent
& event
)
3683 m_owner
->GetEventHandler()->ProcessEvent(event
);
3686 // This seems to be required for wxMotif otherwise the mouse
3687 // cursor must be in the cell edit control to get key events
3689 void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent
& event
)
3691 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3694 void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent
& event
)
3696 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3701 //////////////////////////////////////////////////////////////////////
3703 IMPLEMENT_DYNAMIC_CLASS( wxGridWindow
, wxWindow
)
3705 BEGIN_EVENT_TABLE( wxGridWindow
, wxWindow
)
3706 EVT_PAINT( wxGridWindow::OnPaint
)
3707 EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel
)
3708 EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent
)
3709 EVT_KEY_DOWN( wxGridWindow::OnKeyDown
)
3710 EVT_KEY_UP( wxGridWindow::OnKeyUp
)
3711 EVT_SET_FOCUS( wxGridWindow::OnFocus
)
3712 EVT_KILL_FOCUS( wxGridWindow::OnFocus
)
3713 EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground
)
3716 wxGridWindow::wxGridWindow( wxGrid
*parent
,
3717 wxGridRowLabelWindow
*rowLblWin
,
3718 wxGridColLabelWindow
*colLblWin
,
3721 const wxSize
&size
)
3722 : wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
| wxBORDER_NONE
| wxCLIP_CHILDREN
|wxFULL_REPAINT_ON_RESIZE
,
3723 wxT("grid window") )
3727 m_rowLabelWin
= rowLblWin
;
3728 m_colLabelWin
= colLblWin
;
3732 void wxGridWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
3734 wxPaintDC
dc( this );
3735 m_owner
->PrepareDC( dc
);
3736 wxRegion reg
= GetUpdateRegion();
3737 wxGridCellCoordsArray DirtyCells
= m_owner
->CalcCellsExposed( reg
);
3738 m_owner
->DrawGridCellArea( dc
, DirtyCells
);
3739 #if WXGRID_DRAW_LINES
3740 m_owner
->DrawAllGridLines( dc
, reg
);
3742 m_owner
->DrawGridSpace( dc
);
3743 m_owner
->DrawHighlight( dc
, DirtyCells
);
3747 void wxGridWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
3749 wxWindow::ScrollWindow( dx
, dy
, rect
);
3750 m_rowLabelWin
->ScrollWindow( 0, dy
, rect
);
3751 m_colLabelWin
->ScrollWindow( dx
, 0, rect
);
3755 void wxGridWindow::OnMouseEvent( wxMouseEvent
& event
)
3757 m_owner
->ProcessGridCellMouseEvent( event
);
3760 void wxGridWindow::OnMouseWheel( wxMouseEvent
& event
)
3762 m_owner
->GetEventHandler()->ProcessEvent(event
);
3765 // This seems to be required for wxMotif/wxGTK otherwise the mouse
3766 // cursor must be in the cell edit control to get key events
3768 void wxGridWindow::OnKeyDown( wxKeyEvent
& event
)
3770 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3773 void wxGridWindow::OnKeyUp( wxKeyEvent
& event
)
3775 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) ) event
.Skip();
3778 void wxGridWindow::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
3782 void wxGridWindow::OnFocus(wxFocusEvent
& event
)
3784 if ( !m_owner
->GetEventHandler()->ProcessEvent( event
) )
3788 //////////////////////////////////////////////////////////////////////
3790 // Internal Helper function for computing row or column from some
3791 // (unscrolled) coordinate value, using either
3792 // m_defaultRowHeight/m_defaultColWidth or binary search on array
3793 // of m_rowBottoms/m_ColRights to speed up the search!
3795 // Internal helper macros for simpler use of that function
3797 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
3798 const wxArrayInt
& BorderArray
, int nMax
,
3801 #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \
3802 m_minAcceptableColWidth, \
3803 m_colRights, m_numCols, true)
3804 #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \
3805 m_minAcceptableRowHeight, \
3806 m_rowBottoms, m_numRows, true)
3807 /////////////////////////////////////////////////////////////////////
3809 #if wxUSE_EXTENDED_RTTI
3810 WX_DEFINE_FLAGS( wxGridStyle
)
3812 wxBEGIN_FLAGS( wxGridStyle
)
3813 // new style border flags, we put them first to
3814 // use them for streaming out
3815 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
3816 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
3817 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
3818 wxFLAGS_MEMBER(wxBORDER_RAISED
)
3819 wxFLAGS_MEMBER(wxBORDER_STATIC
)
3820 wxFLAGS_MEMBER(wxBORDER_NONE
)
3822 // old style border flags
3823 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
3824 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
3825 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
3826 wxFLAGS_MEMBER(wxRAISED_BORDER
)
3827 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
3828 wxFLAGS_MEMBER(wxBORDER
)
3830 // standard window styles
3831 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
3832 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
3833 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
3834 wxFLAGS_MEMBER(wxWANTS_CHARS
)
3835 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
3836 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
3837 wxFLAGS_MEMBER(wxVSCROLL
)
3838 wxFLAGS_MEMBER(wxHSCROLL
)
3840 wxEND_FLAGS( wxGridStyle
)
3842 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid
, wxScrolledWindow
,"wx/grid.h")
3844 wxBEGIN_PROPERTIES_TABLE(wxGrid
)
3845 wxHIDE_PROPERTY( Children
)
3846 wxPROPERTY_FLAGS( WindowStyle
, wxGridStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3847 wxEND_PROPERTIES_TABLE()
3849 wxBEGIN_HANDLERS_TABLE(wxGrid
)
3850 wxEND_HANDLERS_TABLE()
3852 wxCONSTRUCTOR_5( wxGrid
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
3855 TODO : Expose more information of a list's layout etc. via appropriate objects (\81Ã la NotebookPageInfo)
3858 IMPLEMENT_DYNAMIC_CLASS( wxGrid
, wxScrolledWindow
)
3861 BEGIN_EVENT_TABLE( wxGrid
, wxScrolledWindow
)
3862 EVT_PAINT( wxGrid::OnPaint
)
3863 EVT_SIZE( wxGrid::OnSize
)
3864 EVT_KEY_DOWN( wxGrid::OnKeyDown
)
3865 EVT_KEY_UP( wxGrid::OnKeyUp
)
3866 EVT_ERASE_BACKGROUND( wxGrid::OnEraseBackground
)
3871 // in order to make sure that a size event is not
3872 // trigerred in a unfinished state
3873 m_cornerLabelWin
= NULL
;
3874 m_rowLabelWin
= NULL
;
3875 m_colLabelWin
= NULL
;
3879 wxGrid::wxGrid( wxWindow
*parent
,
3884 const wxString
& name
)
3885 : wxScrolledWindow( parent
, id
, pos
, size
, (style
| wxWANTS_CHARS
), name
),
3886 m_colMinWidths(GRID_HASH_SIZE
),
3887 m_rowMinHeights(GRID_HASH_SIZE
)
3890 SetBestFittingSize(size
);
3893 bool wxGrid::Create(wxWindow
*parent
, wxWindowID id
,
3894 const wxPoint
& pos
, const wxSize
& size
,
3895 long style
, const wxString
& name
)
3897 if (!wxScrolledWindow::Create(parent
, id
, pos
, size
,
3898 style
| wxWANTS_CHARS
, name
))
3901 m_colMinWidths
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3902 m_rowMinHeights
= wxLongToLongHashMap(GRID_HASH_SIZE
) ;
3905 SetBestFittingSize(size
);
3913 // Must do this or ~wxScrollHelper will pop the wrong event handler
3914 SetTargetWindow(this);
3916 wxSafeDecRef(m_defaultCellAttr
);
3918 #ifdef DEBUG_ATTR_CACHE
3919 size_t total
= gs_nAttrCacheHits
+ gs_nAttrCacheMisses
;
3920 wxPrintf(_T("wxGrid attribute cache statistics: "
3921 "total: %u, hits: %u (%u%%)\n"),
3922 total
, gs_nAttrCacheHits
,
3923 total
? (gs_nAttrCacheHits
*100) / total
: 0);
3929 delete m_typeRegistry
;
3935 // ----- internal init and update functions
3938 // NOTE: If using the default visual attributes works everywhere then this can
3939 // be removed as well as the #else cases below.
3940 #define _USE_VISATTR 0
3943 #include "wx/listbox.h"
3946 void wxGrid::Create()
3948 m_created
= false; // set to true by CreateGrid
3950 m_table
= (wxGridTableBase
*) NULL
;
3953 m_cellEditCtrlEnabled
= false;
3955 m_defaultCellAttr
= new wxGridCellAttr();
3957 // Set default cell attributes
3958 m_defaultCellAttr
->SetDefAttr(m_defaultCellAttr
);
3959 m_defaultCellAttr
->SetKind(wxGridCellAttr::Default
);
3960 m_defaultCellAttr
->SetFont(GetFont());
3961 m_defaultCellAttr
->SetAlignment(wxALIGN_LEFT
, wxALIGN_TOP
);
3962 m_defaultCellAttr
->SetRenderer(new wxGridCellStringRenderer
);
3963 m_defaultCellAttr
->SetEditor(new wxGridCellTextEditor
);
3966 wxVisualAttributes gva
= wxListBox::GetClassDefaultAttributes();
3967 wxVisualAttributes lva
= wxPanel::GetClassDefaultAttributes();
3969 m_defaultCellAttr
->SetTextColour(gva
.colFg
);
3970 m_defaultCellAttr
->SetBackgroundColour(gva
.colBg
);
3973 m_defaultCellAttr
->SetTextColour(
3974 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
3975 m_defaultCellAttr
->SetBackgroundColour(
3976 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
3981 m_currentCellCoords
= wxGridNoCellCoords
;
3983 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
3984 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
3986 // create the type registry
3987 m_typeRegistry
= new wxGridTypeRegistry
;
3990 // subwindow components that make up the wxGrid
3991 m_cornerLabelWin
= new wxGridCornerLabelWindow( this,
3996 m_rowLabelWin
= new wxGridRowLabelWindow( this,
4001 m_colLabelWin
= new wxGridColLabelWindow( this,
4006 m_gridWin
= new wxGridWindow( this,
4013 SetTargetWindow( m_gridWin
);
4016 wxColour gfg
= gva
.colFg
;
4017 wxColour gbg
= gva
.colBg
;
4018 wxColour lfg
= lva
.colFg
;
4019 wxColour lbg
= lva
.colBg
;
4021 wxColour gfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4022 wxColour gbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW
);
4023 wxColour lfg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
4024 wxColour lbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
4026 m_cornerLabelWin
->SetOwnForegroundColour(lfg
);
4027 m_cornerLabelWin
->SetOwnBackgroundColour(lbg
);
4028 m_rowLabelWin
->SetOwnForegroundColour(lfg
);
4029 m_rowLabelWin
->SetOwnBackgroundColour(lbg
);
4030 m_colLabelWin
->SetOwnForegroundColour(lfg
);
4031 m_colLabelWin
->SetOwnBackgroundColour(lbg
);
4033 m_gridWin
->SetOwnForegroundColour(gfg
);
4034 m_gridWin
->SetOwnBackgroundColour(gbg
);
4040 bool wxGrid::CreateGrid( int numRows
, int numCols
,
4041 wxGrid::wxGridSelectionModes selmode
)
4043 wxCHECK_MSG( !m_created
,
4045 wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
4047 m_numRows
= numRows
;
4048 m_numCols
= numCols
;
4050 m_table
= new wxGridStringTable( m_numRows
, m_numCols
);
4051 m_table
->SetView( this );
4053 m_selection
= new wxGridSelection( this, selmode
);
4062 void wxGrid::SetSelectionMode(wxGrid::wxGridSelectionModes selmode
)
4064 wxCHECK_RET( m_created
,
4065 wxT("Called wxGrid::SetSelectionMode() before calling CreateGrid()") );
4067 m_selection
->SetSelectionMode( selmode
);
4070 wxGrid::wxGridSelectionModes
wxGrid::GetSelectionMode() const
4072 wxCHECK_MSG( m_created
, wxGrid::wxGridSelectCells
,
4073 wxT("Called wxGrid::GetSelectionMode() before calling CreateGrid()") );
4075 return m_selection
->GetSelectionMode();
4078 bool wxGrid::SetTable( wxGridTableBase
*table
, bool takeOwnership
,
4079 wxGrid::wxGridSelectionModes selmode
)
4083 // stop all processing
4088 wxGridTableBase
*t
=m_table
;
4101 m_numRows
= table
->GetNumberRows();
4102 m_numCols
= table
->GetNumberCols();
4105 m_table
->SetView( this );
4108 m_selection
= new wxGridSelection( this, selmode
);
4121 m_rowLabelWidth
= WXGRID_DEFAULT_ROW_LABEL_WIDTH
;
4122 m_colLabelHeight
= WXGRID_DEFAULT_COL_LABEL_HEIGHT
;
4124 if ( m_rowLabelWin
)
4126 m_labelBackgroundColour
= m_rowLabelWin
->GetBackgroundColour();
4130 m_labelBackgroundColour
= wxColour( _T("WHITE") );
4133 m_labelTextColour
= wxColour( _T("BLACK") );
4136 m_attrCache
.row
= -1;
4137 m_attrCache
.col
= -1;
4138 m_attrCache
.attr
= NULL
;
4140 // TODO: something better than this ?
4142 m_labelFont
= this->GetFont();
4143 m_labelFont
.SetWeight( wxBOLD
);
4145 m_rowLabelHorizAlign
= wxALIGN_CENTRE
;
4146 m_rowLabelVertAlign
= wxALIGN_CENTRE
;
4148 m_colLabelHorizAlign
= wxALIGN_CENTRE
;
4149 m_colLabelVertAlign
= wxALIGN_CENTRE
;
4150 m_colLabelTextOrientation
= wxHORIZONTAL
;
4152 m_defaultColWidth
= WXGRID_DEFAULT_COL_WIDTH
;
4153 m_defaultRowHeight
= m_gridWin
->GetCharHeight();
4155 m_minAcceptableColWidth
= WXGRID_MIN_COL_WIDTH
;
4156 m_minAcceptableRowHeight
= WXGRID_MIN_ROW_HEIGHT
;
4158 #if defined(__WXMOTIF__) || defined(__WXGTK__) // see also text ctrl sizing in ShowCellEditControl()
4159 m_defaultRowHeight
+= 8;
4161 m_defaultRowHeight
+= 4;
4164 m_gridLineColour
= wxColour( 192,192,192 );
4165 m_gridLinesEnabled
= true;
4166 m_cellHighlightColour
= *wxBLACK
;
4167 m_cellHighlightPenWidth
= 2;
4168 m_cellHighlightROPenWidth
= 1;
4170 m_cursorMode
= WXGRID_CURSOR_SELECT_CELL
;
4171 m_winCapture
= (wxWindow
*)NULL
;
4172 m_canDragRowSize
= true;
4173 m_canDragColSize
= true;
4174 m_canDragGridSize
= true;
4175 m_canDragCell
= false;
4177 m_dragRowOrCol
= -1;
4178 m_isDragging
= false;
4179 m_startDragPos
= wxDefaultPosition
;
4181 m_waitForSlowClick
= false;
4183 m_rowResizeCursor
= wxCursor( wxCURSOR_SIZENS
);
4184 m_colResizeCursor
= wxCursor( wxCURSOR_SIZEWE
);
4186 m_currentCellCoords
= wxGridNoCellCoords
;
4188 m_selectingTopLeft
= wxGridNoCellCoords
;
4189 m_selectingBottomRight
= wxGridNoCellCoords
;
4190 m_selectionBackground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
4191 m_selectionForeground
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
4193 m_editable
= true; // default for whole grid
4195 m_inOnKeyDown
= false;
4201 m_scrollLineX
= GRID_SCROLL_LINE_X
;
4202 m_scrollLineY
= GRID_SCROLL_LINE_Y
;
4205 // ----------------------------------------------------------------------------
4206 // the idea is to call these functions only when necessary because they create
4207 // quite big arrays which eat memory mostly unnecessary - in particular, if
4208 // default widths/heights are used for all rows/columns, we may not use these
4211 // with some extra code, it should be possible to only store the
4212 // widths/heights different from default ones but this will be done later...
4213 // ----------------------------------------------------------------------------
4215 void wxGrid::InitRowHeights()
4217 m_rowHeights
.Empty();
4218 m_rowBottoms
.Empty();
4220 m_rowHeights
.Alloc( m_numRows
);
4221 m_rowBottoms
.Alloc( m_numRows
);
4225 m_rowHeights
.Add( m_defaultRowHeight
, m_numRows
);
4227 for ( int i
= 0; i
< m_numRows
; i
++ )
4229 rowBottom
+= m_defaultRowHeight
;
4230 m_rowBottoms
.Add( rowBottom
);
4234 void wxGrid::InitColWidths()
4236 m_colWidths
.Empty();
4237 m_colRights
.Empty();
4239 m_colWidths
.Alloc( m_numCols
);
4240 m_colRights
.Alloc( m_numCols
);
4243 m_colWidths
.Add( m_defaultColWidth
, m_numCols
);
4245 for ( int i
= 0; i
< m_numCols
; i
++ )
4247 colRight
+= m_defaultColWidth
;
4248 m_colRights
.Add( colRight
);
4252 int wxGrid::GetColWidth(int col
) const
4254 return m_colWidths
.IsEmpty() ? m_defaultColWidth
: m_colWidths
[col
];
4257 int wxGrid::GetColLeft(int col
) const
4259 return m_colRights
.IsEmpty() ? col
* m_defaultColWidth
4260 : m_colRights
[col
] - m_colWidths
[col
];
4263 int wxGrid::GetColRight(int col
) const
4265 return m_colRights
.IsEmpty() ? (col
+ 1) * m_defaultColWidth
4269 int wxGrid::GetRowHeight(int row
) const
4271 return m_rowHeights
.IsEmpty() ? m_defaultRowHeight
: m_rowHeights
[row
];
4274 int wxGrid::GetRowTop(int row
) const
4276 return m_rowBottoms
.IsEmpty() ? row
* m_defaultRowHeight
4277 : m_rowBottoms
[row
] - m_rowHeights
[row
];
4280 int wxGrid::GetRowBottom(int row
) const
4282 return m_rowBottoms
.IsEmpty() ? (row
+ 1) * m_defaultRowHeight
4283 : m_rowBottoms
[row
];
4286 void wxGrid::CalcDimensions()
4289 GetClientSize( &cw
, &ch
);
4291 if ( m_rowLabelWin
->IsShown() )
4292 cw
-= m_rowLabelWidth
;
4293 if ( m_colLabelWin
->IsShown() )
4294 ch
-= m_colLabelHeight
;
4297 int w
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) + m_extraWidth
+ 1 : 0;
4298 int h
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) + m_extraHeight
+ 1 : 0;
4300 // take into account editor if shown
4301 if( IsCellEditControlShown() )
4304 int r
= m_currentCellCoords
.GetRow();
4305 int c
= m_currentCellCoords
.GetCol();
4306 int x
= GetColLeft(c
);
4307 int y
= GetRowTop(r
);
4309 // how big is the editor
4310 wxGridCellAttr
* attr
= GetCellAttr(r
, c
);
4311 wxGridCellEditor
* editor
= attr
->GetEditor(this, r
, c
);
4312 editor
->GetControl()->GetSize(&w2
, &h2
);
4315 if( w2
> w
) w
= w2
;
4316 if( h2
> h
) h
= h2
;
4321 // preserve (more or less) the previous position
4323 GetViewStart( &x
, &y
);
4325 // ensure the position is valid for the new scroll ranges
4327 x
= wxMax( w
- 1, 0 );
4329 y
= wxMax( h
- 1, 0 );
4331 // do set scrollbar parameters
4332 SetScrollbars( GRID_SCROLL_LINE_X
, GRID_SCROLL_LINE_Y
,
4333 GetScrollX(w
), GetScrollY(h
), x
, y
,
4334 GetBatchCount() != 0);
4336 // if our OnSize() hadn't been called (it would if we have scrollbars), we
4337 // still must reposition the children
4342 void wxGrid::CalcWindowSizes()
4344 // escape if the window is has not been fully created yet
4346 if ( m_cornerLabelWin
== NULL
)
4350 GetClientSize( &cw
, &ch
);
4352 if ( m_cornerLabelWin
&& m_cornerLabelWin
->IsShown() )
4353 m_cornerLabelWin
->SetSize( 0, 0, m_rowLabelWidth
, m_colLabelHeight
);
4355 if ( m_colLabelWin
&& m_colLabelWin
->IsShown() )
4356 m_colLabelWin
->SetSize( m_rowLabelWidth
, 0, cw
-m_rowLabelWidth
, m_colLabelHeight
);
4358 if ( m_rowLabelWin
&& m_rowLabelWin
->IsShown() )
4359 m_rowLabelWin
->SetSize( 0, m_colLabelHeight
, m_rowLabelWidth
, ch
-m_colLabelHeight
);
4361 if ( m_gridWin
&& m_gridWin
->IsShown() )
4362 m_gridWin
->SetSize( m_rowLabelWidth
, m_colLabelHeight
, cw
-m_rowLabelWidth
, ch
-m_colLabelHeight
);
4366 // this is called when the grid table sends a message to say that it
4367 // has been redimensioned
4369 bool wxGrid::Redimension( wxGridTableMessage
& msg
)
4372 bool result
= false;
4374 // Clear the attribute cache as the attribute might refer to a different
4375 // cell than stored in the cache after adding/removing rows/columns.
4377 // By the same reasoning, the editor should be dismissed if columns are
4378 // added or removed. And for consistency, it should IMHO always be
4379 // removed, not only if the cell "underneath" it actually changes.
4380 // For now, I intentionally do not save the editor's content as the
4381 // cell it might want to save that stuff to might no longer exist.
4382 HideCellEditControl();
4384 // if we were using the default widths/heights so far, we must change them
4386 if ( m_colWidths
.IsEmpty() )
4391 if ( m_rowHeights
.IsEmpty() )
4397 switch ( msg
.GetId() )
4399 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
4401 size_t pos
= msg
.GetCommandInt();
4402 int numRows
= msg
.GetCommandInt2();
4404 m_numRows
+= numRows
;
4406 if ( !m_rowHeights
.IsEmpty() )
4408 m_rowHeights
.Insert( m_defaultRowHeight
, pos
, numRows
);
4409 m_rowBottoms
.Insert( 0, pos
, numRows
);
4412 if ( pos
> 0 ) bottom
= m_rowBottoms
[pos
-1];
4414 for ( i
= pos
; i
< m_numRows
; i
++ )
4416 bottom
+= m_rowHeights
[i
];
4417 m_rowBottoms
[i
] = bottom
;
4420 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4422 // if we have just inserted cols into an empty grid the current
4423 // cell will be undefined...
4425 SetCurrentCell( 0, 0 );
4429 m_selection
->UpdateRows( pos
, numRows
);
4430 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4432 attrProvider
->UpdateAttrRows( pos
, numRows
);
4434 if ( !GetBatchCount() )
4437 m_rowLabelWin
->Refresh();
4443 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
4445 int numRows
= msg
.GetCommandInt();
4446 int oldNumRows
= m_numRows
;
4447 m_numRows
+= numRows
;
4449 if ( !m_rowHeights
.IsEmpty() )
4451 m_rowHeights
.Add( m_defaultRowHeight
, numRows
);
4452 m_rowBottoms
.Add( 0, numRows
);
4455 if ( oldNumRows
> 0 ) bottom
= m_rowBottoms
[oldNumRows
-1];
4457 for ( i
= oldNumRows
; i
< m_numRows
; i
++ )
4459 bottom
+= m_rowHeights
[i
];
4460 m_rowBottoms
[i
] = bottom
;
4463 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4465 // if we have just inserted cols into an empty grid the current
4466 // cell will be undefined...
4468 SetCurrentCell( 0, 0 );
4470 if ( !GetBatchCount() )
4473 m_rowLabelWin
->Refresh();
4479 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
4481 size_t pos
= msg
.GetCommandInt();
4482 int numRows
= msg
.GetCommandInt2();
4483 m_numRows
-= numRows
;
4485 if ( !m_rowHeights
.IsEmpty() )
4487 m_rowHeights
.RemoveAt( pos
, numRows
);
4488 m_rowBottoms
.RemoveAt( pos
, numRows
);
4491 for ( i
= 0; i
< m_numRows
; i
++ )
4493 h
+= m_rowHeights
[i
];
4494 m_rowBottoms
[i
] = h
;
4499 m_currentCellCoords
= wxGridNoCellCoords
;
4503 if ( m_currentCellCoords
.GetRow() >= m_numRows
)
4504 m_currentCellCoords
.Set( 0, 0 );
4508 m_selection
->UpdateRows( pos
, -((int)numRows
) );
4509 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4511 attrProvider
->UpdateAttrRows( pos
, -((int)numRows
) );
4512 // ifdef'd out following patch from Paul Gammans
4514 // No need to touch column attributes, unless we
4515 // removed _all_ rows, in this case, we remove
4516 // all column attributes.
4517 // I hate to do this here, but the
4518 // needed data is not available inside UpdateAttrRows.
4519 if ( !GetNumberRows() )
4520 attrProvider
->UpdateAttrCols( 0, -GetNumberCols() );
4523 if ( !GetBatchCount() )
4526 m_rowLabelWin
->Refresh();
4532 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
4534 size_t pos
= msg
.GetCommandInt();
4535 int numCols
= msg
.GetCommandInt2();
4536 m_numCols
+= numCols
;
4538 if ( !m_colWidths
.IsEmpty() )
4540 m_colWidths
.Insert( m_defaultColWidth
, pos
, numCols
);
4541 m_colRights
.Insert( 0, pos
, numCols
);
4544 if ( pos
> 0 ) right
= m_colRights
[pos
-1];
4546 for ( i
= pos
; i
< m_numCols
; i
++ )
4548 right
+= m_colWidths
[i
];
4549 m_colRights
[i
] = right
;
4552 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4554 // if we have just inserted cols into an empty grid the current
4555 // cell will be undefined...
4557 SetCurrentCell( 0, 0 );
4561 m_selection
->UpdateCols( pos
, numCols
);
4562 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4564 attrProvider
->UpdateAttrCols( pos
, numCols
);
4565 if ( !GetBatchCount() )
4568 m_colLabelWin
->Refresh();
4575 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
4577 int numCols
= msg
.GetCommandInt();
4578 int oldNumCols
= m_numCols
;
4579 m_numCols
+= numCols
;
4580 if ( !m_colWidths
.IsEmpty() )
4582 m_colWidths
.Add( m_defaultColWidth
, numCols
);
4583 m_colRights
.Add( 0, numCols
);
4586 if ( oldNumCols
> 0 ) right
= m_colRights
[oldNumCols
-1];
4588 for ( i
= oldNumCols
; i
< m_numCols
; i
++ )
4590 right
+= m_colWidths
[i
];
4591 m_colRights
[i
] = right
;
4594 if ( m_currentCellCoords
== wxGridNoCellCoords
)
4596 // if we have just inserted cols into an empty grid the current
4597 // cell will be undefined...
4599 SetCurrentCell( 0, 0 );
4601 if ( !GetBatchCount() )
4604 m_colLabelWin
->Refresh();
4610 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
4612 size_t pos
= msg
.GetCommandInt();
4613 int numCols
= msg
.GetCommandInt2();
4614 m_numCols
-= numCols
;
4616 if ( !m_colWidths
.IsEmpty() )
4618 m_colWidths
.RemoveAt( pos
, numCols
);
4619 m_colRights
.RemoveAt( pos
, numCols
);
4622 for ( i
= 0; i
< m_numCols
; i
++ )
4624 w
+= m_colWidths
[i
];
4630 m_currentCellCoords
= wxGridNoCellCoords
;
4634 if ( m_currentCellCoords
.GetCol() >= m_numCols
)
4635 m_currentCellCoords
.Set( 0, 0 );
4639 m_selection
->UpdateCols( pos
, -((int)numCols
) );
4640 wxGridCellAttrProvider
* attrProvider
= m_table
->GetAttrProvider();
4642 attrProvider
->UpdateAttrCols( pos
, -((int)numCols
) );
4643 // ifdef'd out following patch from Paul Gammans
4645 // No need to touch row attributes, unless we
4646 // removed _all_ columns, in this case, we remove
4647 // all row attributes.
4648 // I hate to do this here, but the
4649 // needed data is not available inside UpdateAttrCols.
4650 if ( !GetNumberCols() )
4651 attrProvider
->UpdateAttrRows( 0, -GetNumberRows() );
4654 if ( !GetBatchCount() )
4657 m_colLabelWin
->Refresh();
4664 if (result
&& !GetBatchCount() )
4665 m_gridWin
->Refresh();
4670 wxArrayInt
wxGrid::CalcRowLabelsExposed( const wxRegion
& reg
)
4672 wxRegionIterator
iter( reg
);
4675 wxArrayInt rowlabels
;
4682 // TODO: remove this when we can...
4683 // There is a bug in wxMotif that gives garbage update
4684 // rectangles if you jump-scroll a long way by clicking the
4685 // scrollbar with middle button. This is a work-around
4687 #if defined(__WXMOTIF__)
4689 m_gridWin
->GetClientSize( &cw
, &ch
);
4690 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4691 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4694 // logical bounds of update region
4697 CalcUnscrolledPosition( 0, r
.GetTop(), &dummy
, &top
);
4698 CalcUnscrolledPosition( 0, r
.GetBottom(), &dummy
, &bottom
);
4700 // find the row labels within these bounds
4703 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4705 if ( GetRowBottom(row
) < top
)
4708 if ( GetRowTop(row
) > bottom
)
4711 rowlabels
.Add( row
);
4721 wxArrayInt
wxGrid::CalcColLabelsExposed( const wxRegion
& reg
)
4723 wxRegionIterator
iter( reg
);
4726 wxArrayInt colLabels
;
4733 // TODO: remove this when we can...
4734 // There is a bug in wxMotif that gives garbage update
4735 // rectangles if you jump-scroll a long way by clicking the
4736 // scrollbar with middle button. This is a work-around
4738 #if defined(__WXMOTIF__)
4740 m_gridWin
->GetClientSize( &cw
, &ch
);
4741 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4742 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4745 // logical bounds of update region
4748 CalcUnscrolledPosition( r
.GetLeft(), 0, &left
, &dummy
);
4749 CalcUnscrolledPosition( r
.GetRight(), 0, &right
, &dummy
);
4751 // find the cells within these bounds
4754 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4756 if ( GetColRight(col
) < left
)
4759 if ( GetColLeft(col
) > right
)
4762 colLabels
.Add( col
);
4771 wxGridCellCoordsArray
wxGrid::CalcCellsExposed( const wxRegion
& reg
)
4773 wxRegionIterator
iter( reg
);
4776 wxGridCellCoordsArray cellsExposed
;
4778 int left
, top
, right
, bottom
;
4783 // TODO: remove this when we can...
4784 // There is a bug in wxMotif that gives garbage update
4785 // rectangles if you jump-scroll a long way by clicking the
4786 // scrollbar with middle button. This is a work-around
4788 #if defined(__WXMOTIF__)
4790 m_gridWin
->GetClientSize( &cw
, &ch
);
4791 if ( r
.GetTop() > ch
) r
.SetTop( 0 );
4792 if ( r
.GetLeft() > cw
) r
.SetLeft( 0 );
4793 r
.SetRight( wxMin( r
.GetRight(), cw
) );
4794 r
.SetBottom( wxMin( r
.GetBottom(), ch
) );
4797 // logical bounds of update region
4799 CalcUnscrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
4800 CalcUnscrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
4802 // find the cells within these bounds
4805 for ( row
= internalYToRow(top
); row
< m_numRows
; row
++ )
4807 if ( GetRowBottom(row
) <= top
)
4810 if ( GetRowTop(row
) > bottom
)
4813 for ( col
= internalXToCol(left
); col
< m_numCols
; col
++ )
4815 if ( GetColRight(col
) <= left
)
4818 if ( GetColLeft(col
) > right
)
4821 cellsExposed
.Add( wxGridCellCoords( row
, col
) );
4828 return cellsExposed
;
4832 void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent
& event
)
4835 wxPoint
pos( event
.GetPosition() );
4836 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
4838 if ( event
.Dragging() )
4842 m_isDragging
= true;
4843 m_rowLabelWin
->CaptureMouse();
4846 if ( event
.LeftIsDown() )
4848 switch( m_cursorMode
)
4850 case WXGRID_CURSOR_RESIZE_ROW
:
4852 int cw
, ch
, left
, dummy
;
4853 m_gridWin
->GetClientSize( &cw
, &ch
);
4854 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
4856 wxClientDC
dc( m_gridWin
);
4859 GetRowTop(m_dragRowOrCol
) +
4860 GetRowMinimalHeight(m_dragRowOrCol
) );
4861 dc
.SetLogicalFunction(wxINVERT
);
4862 if ( m_dragLastPos
>= 0 )
4864 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
4866 dc
.DrawLine( left
, y
, left
+cw
, y
);
4871 case WXGRID_CURSOR_SELECT_ROW
:
4872 if ( (row
= YToRow( y
)) >= 0 )
4876 m_selection
->SelectRow( row
,
4877 event
.ControlDown(),
4884 // default label to suppress warnings about "enumeration value
4885 // 'xxx' not handled in switch
4893 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
4898 if (m_rowLabelWin
->HasCapture()) m_rowLabelWin
->ReleaseMouse();
4899 m_isDragging
= false;
4902 // ------------ Entering or leaving the window
4904 if ( event
.Entering() || event
.Leaving() )
4906 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
4910 // ------------ Left button pressed
4912 else if ( event
.LeftDown() )
4914 // don't send a label click event for a hit on the
4915 // edge of the row label - this is probably the user
4916 // wanting to resize the row
4918 if ( YToEdgeOfRow(y
) < 0 )
4922 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, row
, -1, event
) )
4924 if ( !event
.ShiftDown() && !event
.ControlDown() )
4928 if ( event
.ShiftDown() )
4930 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
4933 GetNumberCols() - 1,
4934 event
.ControlDown(),
4941 m_selection
->SelectRow( row
,
4942 event
.ControlDown(),
4949 ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW
, m_rowLabelWin
);
4954 // starting to drag-resize a row
4956 if ( CanDragRowSize() )
4957 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
);
4962 // ------------ Left double click
4964 else if (event
.LeftDClick() )
4966 int row
= YToEdgeOfRow(y
);
4971 !SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, row
, -1, event
) )
4973 // no default action at the moment
4978 // adjust row height depending on label text
4979 AutoSizeRowLabelSize( row
);
4981 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
4987 // ------------ Left button released
4989 else if ( event
.LeftUp() )
4991 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
4993 DoEndDragResizeRow();
4995 // Note: we are ending the event *after* doing
4996 // default processing in this case
4998 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5001 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
);
5006 // ------------ Right button down
5008 else if ( event
.RightDown() )
5012 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, row
, -1, event
) )
5014 // no default action at the moment
5019 // ------------ Right double click
5021 else if ( event
.RightDClick() )
5025 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, row
, -1, event
) )
5027 // no default action at the moment
5032 // ------------ No buttons down and mouse moving
5034 else if ( event
.Moving() )
5036 m_dragRowOrCol
= YToEdgeOfRow( y
);
5037 if ( m_dragRowOrCol
>= 0 )
5039 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5041 // don't capture the mouse yet
5042 if ( CanDragRowSize() )
5043 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
, m_rowLabelWin
, false);
5046 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5048 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_rowLabelWin
, false);
5054 void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent
& event
)
5057 wxPoint
pos( event
.GetPosition() );
5058 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5060 if ( event
.Dragging() )
5064 m_isDragging
= true;
5065 m_colLabelWin
->CaptureMouse();
5068 if ( event
.LeftIsDown() )
5070 switch( m_cursorMode
)
5072 case WXGRID_CURSOR_RESIZE_COL
:
5074 int cw
, ch
, dummy
, top
;
5075 m_gridWin
->GetClientSize( &cw
, &ch
);
5076 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5078 wxClientDC
dc( m_gridWin
);
5081 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5082 GetColMinimalWidth(m_dragRowOrCol
));
5083 dc
.SetLogicalFunction(wxINVERT
);
5084 if ( m_dragLastPos
>= 0 )
5086 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5088 dc
.DrawLine( x
, top
, x
, top
+ch
);
5093 case WXGRID_CURSOR_SELECT_COL
:
5094 if ( (col
= XToCol( x
)) >= 0 )
5098 m_selection
->SelectCol( col
,
5099 event
.ControlDown(),
5106 // default label to suppress warnings about "enumeration value
5107 // 'xxx' not handled in switch
5115 if ( m_isDragging
&& (event
.Entering() || event
.Leaving()) )
5120 if (m_colLabelWin
->HasCapture()) m_colLabelWin
->ReleaseMouse();
5121 m_isDragging
= false;
5124 // ------------ Entering or leaving the window
5126 if ( event
.Entering() || event
.Leaving() )
5128 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5132 // ------------ Left button pressed
5134 else if ( event
.LeftDown() )
5136 // don't send a label click event for a hit on the
5137 // edge of the col label - this is probably the user
5138 // wanting to resize the col
5140 if ( XToEdgeOfCol(x
) < 0 )
5144 !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, col
, event
) )
5146 if ( !event
.ShiftDown() && !event
.ControlDown() )
5150 if ( event
.ShiftDown() )
5152 m_selection
->SelectBlock( 0,
5153 m_currentCellCoords
.GetCol(),
5154 GetNumberRows() - 1, col
,
5155 event
.ControlDown(),
5162 m_selection
->SelectCol( col
,
5163 event
.ControlDown(),
5170 ChangeCursorMode(WXGRID_CURSOR_SELECT_COL
, m_colLabelWin
);
5175 // starting to drag-resize a col
5177 if ( CanDragColSize() )
5178 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
);
5183 // ------------ Left double click
5185 if ( event
.LeftDClick() )
5187 int col
= XToEdgeOfCol(x
);
5192 ! SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, col
, event
) )
5194 // no default action at the moment
5199 // adjust column width depending on label text
5200 AutoSizeColLabelSize( col
);
5202 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5208 // ------------ Left button released
5210 else if ( event
.LeftUp() )
5212 if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5214 DoEndDragResizeCol();
5216 // Note: we are ending the event *after* doing
5217 // default processing in this case
5219 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5222 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
);
5227 // ------------ Right button down
5229 else if ( event
.RightDown() )
5233 !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, col
, event
) )
5235 // no default action at the moment
5240 // ------------ Right double click
5242 else if ( event
.RightDClick() )
5246 !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, col
, event
) )
5248 // no default action at the moment
5253 // ------------ No buttons down and mouse moving
5255 else if ( event
.Moving() )
5257 m_dragRowOrCol
= XToEdgeOfCol( x
);
5258 if ( m_dragRowOrCol
>= 0 )
5260 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5262 // don't capture the cursor yet
5263 if ( CanDragColSize() )
5264 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
, m_colLabelWin
, false);
5267 else if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5269 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
, m_colLabelWin
, false);
5275 void wxGrid::ProcessCornerLabelMouseEvent( wxMouseEvent
& event
)
5277 if ( event
.LeftDown() )
5279 // indicate corner label by having both row and
5282 if ( !SendEvent( wxEVT_GRID_LABEL_LEFT_CLICK
, -1, -1, event
) )
5288 else if ( event
.LeftDClick() )
5290 SendEvent( wxEVT_GRID_LABEL_LEFT_DCLICK
, -1, -1, event
);
5293 else if ( event
.RightDown() )
5295 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_CLICK
, -1, -1, event
) )
5297 // no default action at the moment
5301 else if ( event
.RightDClick() )
5303 if ( !SendEvent( wxEVT_GRID_LABEL_RIGHT_DCLICK
, -1, -1, event
) )
5305 // no default action at the moment
5310 void wxGrid::ChangeCursorMode(CursorMode mode
,
5315 static const wxChar
*cursorModes
[] =
5324 wxLogTrace(_T("grid"),
5325 _T("wxGrid cursor mode (mouse capture for %s): %s -> %s"),
5326 win
== m_colLabelWin
? _T("colLabelWin")
5327 : win
? _T("rowLabelWin")
5329 cursorModes
[m_cursorMode
], cursorModes
[mode
]);
5330 #endif // __WXDEBUG__
5332 if ( mode
== m_cursorMode
&&
5333 win
== m_winCapture
&&
5334 captureMouse
== (m_winCapture
!= NULL
))
5339 // by default use the grid itself
5345 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5346 m_winCapture
= (wxWindow
*)NULL
;
5349 m_cursorMode
= mode
;
5351 switch ( m_cursorMode
)
5353 case WXGRID_CURSOR_RESIZE_ROW
:
5354 win
->SetCursor( m_rowResizeCursor
);
5357 case WXGRID_CURSOR_RESIZE_COL
:
5358 win
->SetCursor( m_colResizeCursor
);
5362 win
->SetCursor( *wxSTANDARD_CURSOR
);
5365 // we need to capture mouse when resizing
5366 bool resize
= m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
||
5367 m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
;
5369 if ( captureMouse
&& resize
)
5371 win
->CaptureMouse();
5376 void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent
& event
)
5379 wxPoint
pos( event
.GetPosition() );
5380 CalcUnscrolledPosition( pos
.x
, pos
.y
, &x
, &y
);
5382 wxGridCellCoords coords
;
5383 XYToCell( x
, y
, coords
);
5385 int cell_rows
, cell_cols
;
5386 bool isFirstDrag
= !m_isDragging
;
5387 GetCellSize( coords
.GetRow(), coords
.GetCol(), &cell_rows
, &cell_cols
);
5388 if ((cell_rows
< 0) || (cell_cols
< 0))
5390 coords
.SetRow(coords
.GetRow() + cell_rows
);
5391 coords
.SetCol(coords
.GetCol() + cell_cols
);
5394 if ( event
.Dragging() )
5396 //wxLogDebug("pos(%d, %d) coords(%d, %d)", pos.x, pos.y, coords.GetRow(), coords.GetCol());
5398 // Don't start doing anything until the mouse has been drug at
5399 // least 3 pixels in any direction...
5402 if (m_startDragPos
== wxDefaultPosition
)
5404 m_startDragPos
= pos
;
5407 if (abs(m_startDragPos
.x
- pos
.x
) < 4 && abs(m_startDragPos
.y
- pos
.y
) < 4)
5411 m_isDragging
= true;
5412 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5414 // Hide the edit control, so it
5415 // won't interfer with drag-shrinking.
5416 if ( IsCellEditControlShown() )
5418 HideCellEditControl();
5419 SaveEditControlValue();
5422 // Have we captured the mouse yet?
5425 m_winCapture
= m_gridWin
;
5426 m_winCapture
->CaptureMouse();
5429 if ( coords
!= wxGridNoCellCoords
)
5431 if ( event
.ControlDown() )
5433 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5434 m_selectingKeyboard
= coords
;
5435 HighlightBlock ( m_selectingKeyboard
, coords
);
5437 else if ( CanDragCell() )
5441 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
5442 m_selectingKeyboard
= coords
;
5444 SendEvent( wxEVT_GRID_CELL_BEGIN_DRAG
,
5452 if ( !IsSelection() )
5454 HighlightBlock( coords
, coords
);
5458 HighlightBlock( m_currentCellCoords
, coords
);
5462 if (! IsVisible(coords
))
5464 MakeCellVisible(coords
);
5465 // TODO: need to introduce a delay or something here. The
5466 // scrolling is way to fast, at least on MSW - also on GTK.
5470 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5472 int cw
, ch
, left
, dummy
;
5473 m_gridWin
->GetClientSize( &cw
, &ch
);
5474 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5476 wxClientDC
dc( m_gridWin
);
5478 y
= wxMax( y
, GetRowTop(m_dragRowOrCol
) +
5479 GetRowMinimalHeight(m_dragRowOrCol
) );
5480 dc
.SetLogicalFunction(wxINVERT
);
5481 if ( m_dragLastPos
>= 0 )
5483 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5485 dc
.DrawLine( left
, y
, left
+cw
, y
);
5488 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5490 int cw
, ch
, dummy
, top
;
5491 m_gridWin
->GetClientSize( &cw
, &ch
);
5492 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5494 wxClientDC
dc( m_gridWin
);
5496 x
= wxMax( x
, GetColLeft(m_dragRowOrCol
) +
5497 GetColMinimalWidth(m_dragRowOrCol
) );
5498 dc
.SetLogicalFunction(wxINVERT
);
5499 if ( m_dragLastPos
>= 0 )
5501 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5503 dc
.DrawLine( x
, top
, x
, top
+ch
);
5510 m_isDragging
= false;
5511 m_startDragPos
= wxDefaultPosition
;
5513 // VZ: if we do this, the mode is reset to WXGRID_CURSOR_SELECT_CELL
5514 // immediately after it becomes WXGRID_CURSOR_RESIZE_ROW/COL under
5517 if ( event
.Entering() || event
.Leaving() )
5519 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5520 m_gridWin
->SetCursor( *wxSTANDARD_CURSOR
);
5525 // ------------ Left button pressed
5527 if ( event
.LeftDown() && coords
!= wxGridNoCellCoords
)
5529 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_CLICK
,
5534 if ( !event
.ControlDown() )
5536 if ( event
.ShiftDown() )
5540 m_selection
->SelectBlock( m_currentCellCoords
.GetRow(),
5541 m_currentCellCoords
.GetCol(),
5544 event
.ControlDown(),
5550 else if ( XToEdgeOfCol(x
) < 0 &&
5551 YToEdgeOfRow(y
) < 0 )
5553 DisableCellEditControl();
5554 MakeCellVisible( coords
);
5556 if ( event
.ControlDown() )
5560 m_selection
->ToggleCellSelection( coords
.GetRow(),
5562 event
.ControlDown(),
5567 m_selectingTopLeft
= wxGridNoCellCoords
;
5568 m_selectingBottomRight
= wxGridNoCellCoords
;
5569 m_selectingKeyboard
= coords
;
5573 m_waitForSlowClick
= m_currentCellCoords
== coords
&& coords
!= wxGridNoCellCoords
;
5574 SetCurrentCell( coords
);
5577 if ( m_selection
->GetSelectionMode() !=
5578 wxGrid::wxGridSelectCells
)
5580 HighlightBlock( coords
, coords
);
5589 // ------------ Left double click
5591 else if ( event
.LeftDClick() && coords
!= wxGridNoCellCoords
)
5593 DisableCellEditControl();
5595 if ( XToEdgeOfCol(x
) < 0 && YToEdgeOfRow(y
) < 0 )
5597 if ( !SendEvent( wxEVT_GRID_CELL_LEFT_DCLICK
,
5602 // we want double click to select a cell and start editing
5603 // (i.e. to behave in same way as sequence of two slow clicks):
5604 m_waitForSlowClick
= true;
5611 // ------------ Left button released
5613 else if ( event
.LeftUp() )
5615 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5619 if (m_winCapture
->HasCapture()) m_winCapture
->ReleaseMouse();
5620 m_winCapture
= NULL
;
5623 if ( coords
== m_currentCellCoords
&& m_waitForSlowClick
&& CanEnableCellControl())
5626 EnableCellEditControl();
5628 wxGridCellAttr
* attr
= GetCellAttr(coords
);
5629 wxGridCellEditor
*editor
= attr
->GetEditor(this, coords
.GetRow(), coords
.GetCol());
5630 editor
->StartingClick();
5634 m_waitForSlowClick
= false;
5636 else if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
5637 m_selectingBottomRight
!= wxGridNoCellCoords
)
5641 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
5642 m_selectingTopLeft
.GetCol(),
5643 m_selectingBottomRight
.GetRow(),
5644 m_selectingBottomRight
.GetCol(),
5645 event
.ControlDown(),
5651 m_selectingTopLeft
= wxGridNoCellCoords
;
5652 m_selectingBottomRight
= wxGridNoCellCoords
;
5654 // Show the edit control, if it has been hidden for
5656 ShowCellEditControl();
5659 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_ROW
)
5661 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5662 DoEndDragResizeRow();
5664 // Note: we are ending the event *after* doing
5665 // default processing in this case
5667 SendEvent( wxEVT_GRID_ROW_SIZE
, m_dragRowOrCol
, -1, event
);
5669 else if ( m_cursorMode
== WXGRID_CURSOR_RESIZE_COL
)
5671 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5672 DoEndDragResizeCol();
5674 // Note: we are ending the event *after* doing
5675 // default processing in this case
5677 SendEvent( wxEVT_GRID_COL_SIZE
, -1, m_dragRowOrCol
, event
);
5684 // ------------ Right button down
5686 else if ( event
.RightDown() && coords
!= wxGridNoCellCoords
)
5688 DisableCellEditControl();
5689 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_CLICK
,
5694 // no default action at the moment
5699 // ------------ Right double click
5701 else if ( event
.RightDClick() && coords
!= wxGridNoCellCoords
)
5703 DisableCellEditControl();
5704 if ( !SendEvent( wxEVT_GRID_CELL_RIGHT_DCLICK
,
5709 // no default action at the moment
5713 // ------------ Moving and no button action
5715 else if ( event
.Moving() && !event
.IsButton() )
5717 if( coords
.GetRow() < 0 || coords
.GetCol() < 0 )
5719 // out of grid cell area
5720 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5724 int dragRow
= YToEdgeOfRow( y
);
5725 int dragCol
= XToEdgeOfCol( x
);
5727 // Dragging on the corner of a cell to resize in both
5728 // directions is not implemented yet...
5730 if ( dragRow
>= 0 && dragCol
>= 0 )
5732 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5738 m_dragRowOrCol
= dragRow
;
5740 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5742 if ( CanDragRowSize() && CanDragGridSize() )
5743 ChangeCursorMode(WXGRID_CURSOR_RESIZE_ROW
);
5748 m_dragRowOrCol
= dragCol
;
5756 m_dragRowOrCol
= dragCol
;
5758 if ( m_cursorMode
== WXGRID_CURSOR_SELECT_CELL
)
5760 if ( CanDragColSize() && CanDragGridSize() )
5761 ChangeCursorMode(WXGRID_CURSOR_RESIZE_COL
);
5767 // Neither on a row or col edge
5769 if ( m_cursorMode
!= WXGRID_CURSOR_SELECT_CELL
)
5771 ChangeCursorMode(WXGRID_CURSOR_SELECT_CELL
);
5777 void wxGrid::DoEndDragResizeRow()
5779 if ( m_dragLastPos
>= 0 )
5781 // erase the last line and resize the row
5783 int cw
, ch
, left
, dummy
;
5784 m_gridWin
->GetClientSize( &cw
, &ch
);
5785 CalcUnscrolledPosition( 0, 0, &left
, &dummy
);
5787 wxClientDC
dc( m_gridWin
);
5789 dc
.SetLogicalFunction( wxINVERT
);
5790 dc
.DrawLine( left
, m_dragLastPos
, left
+cw
, m_dragLastPos
);
5791 HideCellEditControl();
5792 SaveEditControlValue();
5794 int rowTop
= GetRowTop(m_dragRowOrCol
);
5795 SetRowSize( m_dragRowOrCol
,
5796 wxMax( m_dragLastPos
- rowTop
, m_minAcceptableRowHeight
) );
5798 if ( !GetBatchCount() )
5800 // Only needed to get the correct rect.y:
5801 wxRect
rect ( CellToRect( m_dragRowOrCol
, 0 ) );
5803 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5804 rect
.width
= m_rowLabelWidth
;
5805 rect
.height
= ch
- rect
.y
;
5806 m_rowLabelWin
->Refresh( true, &rect
);
5808 // if there is a multicell block, paint all of it
5811 int i
, cell_rows
, cell_cols
, subtract_rows
= 0;
5812 int leftCol
= XToCol(left
);
5813 int rightCol
= internalXToCol(left
+cw
);
5816 for (i
=leftCol
; i
<rightCol
; i
++)
5818 GetCellSize(m_dragRowOrCol
, i
, &cell_rows
, &cell_cols
);
5819 if (cell_rows
< subtract_rows
)
5820 subtract_rows
= cell_rows
;
5822 rect
.y
= GetRowTop(m_dragRowOrCol
+ subtract_rows
);
5823 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
5824 rect
.height
= ch
- rect
.y
;
5827 m_gridWin
->Refresh( false, &rect
);
5830 ShowCellEditControl();
5835 void wxGrid::DoEndDragResizeCol()
5837 if ( m_dragLastPos
>= 0 )
5839 // erase the last line and resize the col
5841 int cw
, ch
, dummy
, top
;
5842 m_gridWin
->GetClientSize( &cw
, &ch
);
5843 CalcUnscrolledPosition( 0, 0, &dummy
, &top
);
5845 wxClientDC
dc( m_gridWin
);
5847 dc
.SetLogicalFunction( wxINVERT
);
5848 dc
.DrawLine( m_dragLastPos
, top
, m_dragLastPos
, top
+ch
);
5849 HideCellEditControl();
5850 SaveEditControlValue();
5852 int colLeft
= GetColLeft(m_dragRowOrCol
);
5853 SetColSize( m_dragRowOrCol
,
5854 wxMax( m_dragLastPos
- colLeft
,
5855 GetColMinimalWidth(m_dragRowOrCol
) ) );
5857 if ( !GetBatchCount() )
5859 // Only needed to get the correct rect.x:
5860 wxRect
rect ( CellToRect( 0, m_dragRowOrCol
) );
5862 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5863 rect
.width
= cw
- rect
.x
;
5864 rect
.height
= m_colLabelHeight
;
5865 m_colLabelWin
->Refresh( true, &rect
);
5867 // if there is a multicell block, paint all of it
5870 int i
, cell_rows
, cell_cols
, subtract_cols
= 0;
5871 int topRow
= YToRow(top
);
5872 int bottomRow
= internalYToRow(top
+cw
);
5875 for (i
=topRow
; i
<bottomRow
; i
++)
5877 GetCellSize(i
, m_dragRowOrCol
, &cell_rows
, &cell_cols
);
5878 if (cell_cols
< subtract_cols
)
5879 subtract_cols
= cell_cols
;
5881 rect
.x
= GetColLeft(m_dragRowOrCol
+ subtract_cols
);
5882 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
5883 rect
.width
= cw
- rect
.x
;
5886 m_gridWin
->Refresh( false, &rect
);
5889 ShowCellEditControl();
5896 // ------ interaction with data model
5898 bool wxGrid::ProcessTableMessage( wxGridTableMessage
& msg
)
5900 switch ( msg
.GetId() )
5902 case wxGRIDTABLE_REQUEST_VIEW_GET_VALUES
:
5903 return GetModelValues();
5905 case wxGRIDTABLE_REQUEST_VIEW_SEND_VALUES
:
5906 return SetModelValues();
5908 case wxGRIDTABLE_NOTIFY_ROWS_INSERTED
:
5909 case wxGRIDTABLE_NOTIFY_ROWS_APPENDED
:
5910 case wxGRIDTABLE_NOTIFY_ROWS_DELETED
:
5911 case wxGRIDTABLE_NOTIFY_COLS_INSERTED
:
5912 case wxGRIDTABLE_NOTIFY_COLS_APPENDED
:
5913 case wxGRIDTABLE_NOTIFY_COLS_DELETED
:
5914 return Redimension( msg
);
5923 // The behaviour of this function depends on the grid table class
5924 // Clear() function. For the default wxGridStringTable class the
5925 // behavious is to replace all cell contents with wxEmptyString but
5926 // not to change the number of rows or cols.
5928 void wxGrid::ClearGrid()
5932 if (IsCellEditControlEnabled())
5933 DisableCellEditControl();
5936 if ( !GetBatchCount() ) m_gridWin
->Refresh();
5941 bool wxGrid::InsertRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5943 // TODO: something with updateLabels flag
5947 wxFAIL_MSG( wxT("Called wxGrid::InsertRows() before calling CreateGrid()") );
5953 if (IsCellEditControlEnabled())
5954 DisableCellEditControl();
5956 bool done
= m_table
->InsertRows( pos
, numRows
);
5959 // the table will have sent the results of the insert row
5960 // operation to this view object as a grid table message
5966 bool wxGrid::AppendRows( int numRows
, bool WXUNUSED(updateLabels
) )
5968 // TODO: something with updateLabels flag
5972 wxFAIL_MSG( wxT("Called wxGrid::AppendRows() before calling CreateGrid()") );
5978 bool done
= m_table
&& m_table
->AppendRows( numRows
);
5980 // the table will have sent the results of the append row
5981 // operation to this view object as a grid table message
5987 bool wxGrid::DeleteRows( int pos
, int numRows
, bool WXUNUSED(updateLabels
) )
5989 // TODO: something with updateLabels flag
5993 wxFAIL_MSG( wxT("Called wxGrid::DeleteRows() before calling CreateGrid()") );
5999 if (IsCellEditControlEnabled())
6000 DisableCellEditControl();
6002 bool done
= m_table
->DeleteRows( pos
, numRows
);
6004 // the table will have sent the results of the delete row
6005 // operation to this view object as a grid table message
6011 bool wxGrid::InsertCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6013 // TODO: something with updateLabels flag
6017 wxFAIL_MSG( wxT("Called wxGrid::InsertCols() before calling CreateGrid()") );
6023 if (IsCellEditControlEnabled())
6024 DisableCellEditControl();
6026 bool done
= m_table
->InsertCols( pos
, numCols
);
6028 // the table will have sent the results of the insert col
6029 // operation to this view object as a grid table message
6035 bool wxGrid::AppendCols( int numCols
, bool WXUNUSED(updateLabels
) )
6037 // TODO: something with updateLabels flag
6041 wxFAIL_MSG( wxT("Called wxGrid::AppendCols() before calling CreateGrid()") );
6047 bool done
= m_table
->AppendCols( numCols
);
6049 // the table will have sent the results of the append col
6050 // operation to this view object as a grid table message
6056 bool wxGrid::DeleteCols( int pos
, int numCols
, bool WXUNUSED(updateLabels
) )
6058 // TODO: something with updateLabels flag
6062 wxFAIL_MSG( wxT("Called wxGrid::DeleteCols() before calling CreateGrid()") );
6068 if (IsCellEditControlEnabled())
6069 DisableCellEditControl();
6071 bool done
= m_table
->DeleteCols( pos
, numCols
);
6073 // the table will have sent the results of the delete col
6074 // operation to this view object as a grid table message
6082 // ----- event handlers
6085 // Generate a grid event based on a mouse event and
6086 // return the result of ProcessEvent()
6088 int wxGrid::SendEvent( const wxEventType type
,
6090 wxMouseEvent
& mouseEv
)
6095 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6097 int rowOrCol
= (row
== -1 ? col
: row
);
6099 wxGridSizeEvent
gridEvt( GetId(),
6103 mouseEv
.GetX() + GetRowLabelSize(),
6104 mouseEv
.GetY() + GetColLabelSize(),
6105 mouseEv
.ControlDown(),
6106 mouseEv
.ShiftDown(),
6108 mouseEv
.MetaDown() );
6110 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6111 vetoed
= !gridEvt
.IsAllowed();
6113 else if ( type
== wxEVT_GRID_RANGE_SELECT
)
6115 // Right now, it should _never_ end up here!
6116 wxGridRangeSelectEvent
gridEvt( GetId(),
6120 m_selectingBottomRight
,
6122 mouseEv
.ControlDown(),
6123 mouseEv
.ShiftDown(),
6125 mouseEv
.MetaDown() );
6127 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6128 vetoed
= !gridEvt
.IsAllowed();
6132 wxGridEvent
gridEvt( GetId(),
6136 mouseEv
.GetX() + GetRowLabelSize(),
6137 mouseEv
.GetY() + GetColLabelSize(),
6139 mouseEv
.ControlDown(),
6140 mouseEv
.ShiftDown(),
6142 mouseEv
.MetaDown() );
6143 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6144 vetoed
= !gridEvt
.IsAllowed();
6147 // A Veto'd event may not be `claimed' so test this first
6148 if (vetoed
) return -1;
6149 return claimed
? 1 : 0;
6153 // Generate a grid event of specified type and return the result
6154 // of ProcessEvent().
6156 int wxGrid::SendEvent( const wxEventType type
,
6162 if ( type
== wxEVT_GRID_ROW_SIZE
|| type
== wxEVT_GRID_COL_SIZE
)
6164 int rowOrCol
= (row
== -1 ? col
: row
);
6166 wxGridSizeEvent
gridEvt( GetId(),
6171 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6172 vetoed
= !gridEvt
.IsAllowed();
6176 wxGridEvent
gridEvt( GetId(),
6181 claimed
= GetEventHandler()->ProcessEvent(gridEvt
);
6182 vetoed
= !gridEvt
.IsAllowed();
6185 // A Veto'd event may not be `claimed' so test this first
6186 if (vetoed
) return -1;
6187 return claimed
? 1 : 0;
6191 void wxGrid::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
6193 wxPaintDC
dc(this); // needed to prevent zillions of paint events on MSW
6196 void wxGrid::Refresh(bool eraseb
, const wxRect
* rect
)
6198 // Don't do anything if between Begin/EndBatch...
6199 // EndBatch() will do all this on the last nested one anyway.
6200 if (! GetBatchCount())
6202 // Refresh to get correct scrolled position:
6203 wxScrolledWindow::Refresh(eraseb
,rect
);
6207 int rect_x
, rect_y
, rectWidth
, rectHeight
;
6208 int width_label
, width_cell
, height_label
, height_cell
;
6211 //Copy rectangle can get scroll offsets..
6212 rect_x
= rect
->GetX();
6213 rect_y
= rect
->GetY();
6214 rectWidth
= rect
->GetWidth();
6215 rectHeight
= rect
->GetHeight();
6217 width_label
= m_rowLabelWidth
- rect_x
;
6218 if (width_label
> rectWidth
) width_label
= rectWidth
;
6220 height_label
= m_colLabelHeight
- rect_y
;
6221 if (height_label
> rectHeight
) height_label
= rectHeight
;
6223 if (rect_x
> m_rowLabelWidth
)
6225 x
= rect_x
- m_rowLabelWidth
;
6226 width_cell
= rectWidth
;
6231 width_cell
= rectWidth
- (m_rowLabelWidth
- rect_x
);
6234 if (rect_y
> m_colLabelHeight
)
6236 y
= rect_y
- m_colLabelHeight
;
6237 height_cell
= rectHeight
;
6242 height_cell
= rectHeight
- (m_colLabelHeight
- rect_y
);
6245 // Paint corner label part intersecting rect.
6246 if ( width_label
> 0 && height_label
> 0 )
6248 wxRect
anotherrect(rect_x
, rect_y
, width_label
, height_label
);
6249 m_cornerLabelWin
->Refresh(eraseb
, &anotherrect
);
6252 // Paint col labels part intersecting rect.
6253 if ( width_cell
> 0 && height_label
> 0 )
6255 wxRect
anotherrect(x
, rect_y
, width_cell
, height_label
);
6256 m_colLabelWin
->Refresh(eraseb
, &anotherrect
);
6259 // Paint row labels part intersecting rect.
6260 if ( width_label
> 0 && height_cell
> 0 )
6262 wxRect
anotherrect(rect_x
, y
, width_label
, height_cell
);
6263 m_rowLabelWin
->Refresh(eraseb
, &anotherrect
);
6266 // Paint cell area part intersecting rect.
6267 if ( width_cell
> 0 && height_cell
> 0 )
6269 wxRect
anotherrect(x
, y
, width_cell
, height_cell
);
6270 m_gridWin
->Refresh(eraseb
, &anotherrect
);
6275 m_cornerLabelWin
->Refresh(eraseb
, NULL
);
6276 m_colLabelWin
->Refresh(eraseb
, NULL
);
6277 m_rowLabelWin
->Refresh(eraseb
, NULL
);
6278 m_gridWin
->Refresh(eraseb
, NULL
);
6283 void wxGrid::OnSize( wxSizeEvent
& event
)
6285 // position the child windows
6288 // don't call CalcDimensions() from here, the base class handles the size
6294 void wxGrid::OnKeyDown( wxKeyEvent
& event
)
6296 if ( m_inOnKeyDown
)
6298 // shouldn't be here - we are going round in circles...
6300 wxFAIL_MSG( wxT("wxGrid::OnKeyDown called while already active") );
6303 m_inOnKeyDown
= true;
6305 // propagate the event up and see if it gets processed
6307 wxWindow
*parent
= GetParent();
6308 wxKeyEvent
keyEvt( event
);
6309 keyEvt
.SetEventObject( parent
);
6311 if ( !parent
->GetEventHandler()->ProcessEvent( keyEvt
) )
6314 // try local handlers
6316 switch ( event
.GetKeyCode() )
6319 if ( event
.ControlDown() )
6321 MoveCursorUpBlock( event
.ShiftDown() );
6325 MoveCursorUp( event
.ShiftDown() );
6330 if ( event
.ControlDown() )
6332 MoveCursorDownBlock( event
.ShiftDown() );
6336 MoveCursorDown( event
.ShiftDown() );
6341 if ( event
.ControlDown() )
6343 MoveCursorLeftBlock( event
.ShiftDown() );
6347 MoveCursorLeft( event
.ShiftDown() );
6352 if ( event
.ControlDown() )
6354 MoveCursorRightBlock( event
.ShiftDown() );
6358 MoveCursorRight( event
.ShiftDown() );
6363 case WXK_NUMPAD_ENTER
:
6364 if ( event
.ControlDown() )
6366 event
.Skip(); // to let the edit control have the return
6370 if ( GetGridCursorRow() < GetNumberRows()-1 )
6372 MoveCursorDown( event
.ShiftDown() );
6376 // at the bottom of a column
6377 DisableCellEditControl();
6387 if (event
.ShiftDown())
6389 if ( GetGridCursorCol() > 0 )
6391 MoveCursorLeft( false );
6396 DisableCellEditControl();
6401 if ( GetGridCursorCol() < GetNumberCols()-1 )
6403 MoveCursorRight( false );
6408 DisableCellEditControl();
6414 if ( event
.ControlDown() )
6416 MakeCellVisible( 0, 0 );
6417 SetCurrentCell( 0, 0 );
6426 if ( event
.ControlDown() )
6428 MakeCellVisible( m_numRows
-1, m_numCols
-1 );
6429 SetCurrentCell( m_numRows
-1, m_numCols
-1 );
6446 if ( event
.ControlDown() )
6450 m_selection
->ToggleCellSelection( m_currentCellCoords
.GetRow(),
6451 m_currentCellCoords
.GetCol(),
6452 event
.ControlDown(),
6459 if ( !IsEditable() )
6461 MoveCursorRight( false );
6464 // Otherwise fall through to default
6467 // is it possible to edit the current cell at all?
6468 if ( !IsCellEditControlEnabled() && CanEnableCellControl() )
6470 // yes, now check whether the cells editor accepts the key
6471 int row
= m_currentCellCoords
.GetRow();
6472 int col
= m_currentCellCoords
.GetCol();
6473 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6474 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6476 // <F2> is special and will always start editing, for
6477 // other keys - ask the editor itself
6478 if ( (event
.GetKeyCode() == WXK_F2
&& !event
.HasModifiers())
6479 || editor
->IsAcceptedKey(event
) )
6481 // ensure cell is visble
6482 MakeCellVisible(row
, col
);
6483 EnableCellEditControl();
6485 // a problem can arise if the cell is not completely
6486 // visible (even after calling MakeCellVisible the
6487 // control is not created and calling StartingKey will
6489 if( editor
->IsCreated() && m_cellEditCtrlEnabled
) editor
->StartingKey(event
);
6501 // let others process char events with modifiers or all
6502 // char events for readonly cells
6509 m_inOnKeyDown
= false;
6512 void wxGrid::OnKeyUp( wxKeyEvent
& event
)
6514 // try local handlers
6516 if ( event
.GetKeyCode() == WXK_SHIFT
)
6518 if ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
6519 m_selectingBottomRight
!= wxGridNoCellCoords
)
6523 m_selection
->SelectBlock( m_selectingTopLeft
.GetRow(),
6524 m_selectingTopLeft
.GetCol(),
6525 m_selectingBottomRight
.GetRow(),
6526 m_selectingBottomRight
.GetCol(),
6527 event
.ControlDown(),
6534 m_selectingTopLeft
= wxGridNoCellCoords
;
6535 m_selectingBottomRight
= wxGridNoCellCoords
;
6536 m_selectingKeyboard
= wxGridNoCellCoords
;
6540 void wxGrid::OnEraseBackground(wxEraseEvent
&)
6544 void wxGrid::SetCurrentCell( const wxGridCellCoords
& coords
)
6546 if ( SendEvent( wxEVT_GRID_SELECT_CELL
, coords
.GetRow(), coords
.GetCol() ) )
6548 // the event has been intercepted - do nothing
6552 wxClientDC
dc(m_gridWin
);
6555 if ( m_currentCellCoords
!= wxGridNoCellCoords
)
6557 DisableCellEditControl();
6559 if ( IsVisible( m_currentCellCoords
, false ) )
6562 r
= BlockToDeviceRect(m_currentCellCoords
, m_currentCellCoords
);
6563 if ( !m_gridLinesEnabled
)
6571 wxGridCellCoordsArray cells
= CalcCellsExposed( r
);
6573 // Otherwise refresh redraws the highlight!
6574 m_currentCellCoords
= coords
;
6576 DrawGridCellArea(dc
,cells
);
6577 DrawAllGridLines( dc
, r
);
6581 m_currentCellCoords
= coords
;
6583 wxGridCellAttr
* attr
= GetCellAttr(coords
);
6584 DrawCellHighlight(dc
, attr
);
6589 void wxGrid::HighlightBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
)
6592 wxGridCellCoords updateTopLeft
, updateBottomRight
;
6596 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
6599 rightCol
= GetNumberCols() - 1;
6601 else if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
6604 bottomRow
= GetNumberRows() - 1;
6608 if ( topRow
> bottomRow
)
6615 if ( leftCol
> rightCol
)
6622 updateTopLeft
= wxGridCellCoords( topRow
, leftCol
);
6623 updateBottomRight
= wxGridCellCoords( bottomRow
, rightCol
);
6625 // First the case that we selected a completely new area
6626 if ( m_selectingTopLeft
== wxGridNoCellCoords
||
6627 m_selectingBottomRight
== wxGridNoCellCoords
)
6630 rect
= BlockToDeviceRect( wxGridCellCoords ( topRow
, leftCol
),
6631 wxGridCellCoords ( bottomRow
, rightCol
) );
6632 m_gridWin
->Refresh( false, &rect
);
6634 // Now handle changing an existing selection area.
6635 else if ( m_selectingTopLeft
!= updateTopLeft
||
6636 m_selectingBottomRight
!= updateBottomRight
)
6638 // Compute two optimal update rectangles:
6639 // Either one rectangle is a real subset of the
6640 // other, or they are (almost) disjoint!
6642 bool need_refresh
[4];
6646 need_refresh
[3] = false;
6649 // Store intermediate values
6650 wxCoord oldLeft
= m_selectingTopLeft
.GetCol();
6651 wxCoord oldTop
= m_selectingTopLeft
.GetRow();
6652 wxCoord oldRight
= m_selectingBottomRight
.GetCol();
6653 wxCoord oldBottom
= m_selectingBottomRight
.GetRow();
6655 // Determine the outer/inner coordinates.
6656 if (oldLeft
> leftCol
)
6662 if (oldTop
> topRow
)
6668 if (oldRight
< rightCol
)
6671 oldRight
= rightCol
;
6674 if (oldBottom
< bottomRow
)
6677 oldBottom
= bottomRow
;
6681 // Now, either the stuff marked old is the outer
6682 // rectangle or we don't have a situation where one
6683 // is contained in the other.
6685 if ( oldLeft
< leftCol
)
6687 // Refresh the newly selected or deselected
6688 // area to the left of the old or new selection.
6689 need_refresh
[0] = true;
6690 rect
[0] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6692 wxGridCellCoords ( oldBottom
,
6696 if ( oldTop
< topRow
)
6698 // Refresh the newly selected or deselected
6699 // area above the old or new selection.
6700 need_refresh
[1] = true;
6701 rect
[1] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6703 wxGridCellCoords ( topRow
- 1,
6707 if ( oldRight
> rightCol
)
6709 // Refresh the newly selected or deselected
6710 // area to the right of the old or new selection.
6711 need_refresh
[2] = true;
6712 rect
[2] = BlockToDeviceRect( wxGridCellCoords ( oldTop
,
6714 wxGridCellCoords ( oldBottom
,
6718 if ( oldBottom
> bottomRow
)
6720 // Refresh the newly selected or deselected
6721 // area below the old or new selection.
6722 need_refresh
[3] = true;
6723 rect
[3] = BlockToDeviceRect( wxGridCellCoords ( bottomRow
+ 1,
6725 wxGridCellCoords ( oldBottom
,
6729 // various Refresh() calls
6730 for (i
= 0; i
< 4; i
++ )
6731 if ( need_refresh
[i
] && rect
[i
] != wxGridNoCellRect
)
6732 m_gridWin
->Refresh( false, &(rect
[i
]) );
6735 m_selectingTopLeft
= updateTopLeft
;
6736 m_selectingBottomRight
= updateBottomRight
;
6740 // ------ functions to get/send data (see also public functions)
6743 bool wxGrid::GetModelValues()
6745 // Hide the editor, so it won't hide a changed value.
6746 HideCellEditControl();
6750 // all we need to do is repaint the grid
6752 m_gridWin
->Refresh();
6760 bool wxGrid::SetModelValues()
6764 // Disable the editor, so it won't hide a changed value.
6765 // Do we also want to save the current value of the editor first?
6767 DisableCellEditControl();
6771 for ( row
= 0; row
< m_numRows
; row
++ )
6773 for ( col
= 0; col
< m_numCols
; col
++ )
6775 m_table
->SetValue( row
, col
, GetCellValue(row
, col
) );
6787 // Note - this function only draws cells that are in the list of
6788 // exposed cells (usually set from the update region by
6789 // CalcExposedCells)
6791 void wxGrid::DrawGridCellArea( wxDC
& dc
, const wxGridCellCoordsArray
& cells
)
6793 if ( !m_numRows
|| !m_numCols
) return;
6795 int i
, numCells
= cells
.GetCount();
6796 int row
, col
, cell_rows
, cell_cols
;
6797 wxGridCellCoordsArray redrawCells
;
6799 for ( i
= numCells
-1; i
>= 0; i
-- )
6801 row
= cells
[i
].GetRow();
6802 col
= cells
[i
].GetCol();
6803 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
6805 // If this cell is part of a multicell block, find owner for repaint
6806 if ( cell_rows
<= 0 || cell_cols
<= 0 )
6808 wxGridCellCoords
cell(row
+cell_rows
, col
+cell_cols
);
6809 bool marked
= false;
6810 for ( int j
= 0; j
< numCells
; j
++ )
6812 if ( cell
== cells
[j
] )
6820 int count
= redrawCells
.GetCount();
6821 for (int j
= 0; j
< count
; j
++)
6823 if ( cell
== redrawCells
[j
] )
6829 if (!marked
) redrawCells
.Add( cell
);
6831 continue; // don't bother drawing this cell
6834 // If this cell is empty, find cell to left that might want to overflow
6835 if (m_table
&& m_table
->IsEmptyCell(row
, col
))
6837 for ( int l
= 0; l
< cell_rows
; l
++ )
6839 // find a cell in this row to left alreay marked for repaint
6841 for (int k
= 0; k
< int(redrawCells
.GetCount()); k
++)
6842 if ((redrawCells
[k
].GetCol() < left
) &&
6843 (redrawCells
[k
].GetRow() == row
))
6844 left
=redrawCells
[k
].GetCol();
6846 if (left
== col
) left
= 0; // oh well
6848 for (int j
= col
-1; j
>= left
; j
--)
6850 if (!m_table
->IsEmptyCell(row
+l
, j
))
6852 if (GetCellOverflow(row
+l
, j
))
6854 wxGridCellCoords
cell(row
+l
, j
);
6855 bool marked
= false;
6857 for (int k
= 0; k
< numCells
; k
++)
6859 if ( cell
== cells
[k
] )
6867 int count
= redrawCells
.GetCount();
6868 for (int k
= 0; k
< count
; k
++)
6870 if ( cell
== redrawCells
[k
] )
6876 if (!marked
) redrawCells
.Add( cell
);
6884 DrawCell( dc
, cells
[i
] );
6887 numCells
= redrawCells
.GetCount();
6889 for ( i
= numCells
- 1; i
>= 0; i
-- )
6891 DrawCell( dc
, redrawCells
[i
] );
6896 void wxGrid::DrawGridSpace( wxDC
& dc
)
6899 m_gridWin
->GetClientSize( &cw
, &ch
);
6902 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
6904 int rightCol
= m_numCols
> 0 ? GetColRight(m_numCols
- 1) : 0;
6905 int bottomRow
= m_numRows
> 0 ? GetRowBottom(m_numRows
- 1) : 0 ;
6907 if ( right
> rightCol
|| bottom
> bottomRow
)
6910 CalcUnscrolledPosition( 0, 0, &left
, &top
);
6912 dc
.SetBrush( wxBrush(GetDefaultCellBackgroundColour(), wxSOLID
) );
6913 dc
.SetPen( *wxTRANSPARENT_PEN
);
6915 if ( right
> rightCol
)
6917 dc
.DrawRectangle( rightCol
, top
, right
- rightCol
, ch
);
6920 if ( bottom
> bottomRow
)
6922 dc
.DrawRectangle( left
, bottomRow
, cw
, bottom
- bottomRow
);
6928 void wxGrid::DrawCell( wxDC
& dc
, const wxGridCellCoords
& coords
)
6930 int row
= coords
.GetRow();
6931 int col
= coords
.GetCol();
6933 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6936 // we draw the cell border ourselves
6937 #if !WXGRID_DRAW_LINES
6938 if ( m_gridLinesEnabled
)
6939 DrawCellBorder( dc
, coords
);
6942 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
6944 bool isCurrent
= coords
== m_currentCellCoords
;
6946 wxRect rect
= CellToRect( row
, col
);
6948 // if the editor is shown, we should use it and not the renderer
6949 // Note: However, only if it is really _shown_, i.e. not hidden!
6950 if ( isCurrent
&& IsCellEditControlShown() )
6952 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
6953 editor
->PaintBackground(rect
, attr
);
6958 // but all the rest is drawn by the cell renderer and hence may be
6960 wxGridCellRenderer
*renderer
= attr
->GetRenderer(this, row
, col
);
6961 renderer
->Draw(*this, *attr
, dc
, rect
, row
, col
, IsInSelection(coords
));
6968 void wxGrid::DrawCellHighlight( wxDC
& dc
, const wxGridCellAttr
*attr
)
6970 int row
= m_currentCellCoords
.GetRow();
6971 int col
= m_currentCellCoords
.GetCol();
6973 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
6976 wxRect rect
= CellToRect(row
, col
);
6978 // hmmm... what could we do here to show that the cell is disabled?
6979 // for now, I just draw a thinner border than for the other ones, but
6980 // it doesn't look really good
6982 int penWidth
= attr
->IsReadOnly() ? m_cellHighlightROPenWidth
: m_cellHighlightPenWidth
;
6986 // The center of th drawn line is where the position/width/height of
6987 // the rectangle is actually at, (on wxMSW atr least,) so we will
6988 // reduce the size of the rectangle to compensate for the thickness of
6989 // the line. If this is too strange on non wxMSW platforms then
6990 // please #ifdef this appropriately.
6991 rect
.x
+= penWidth
/2;
6992 rect
.y
+= penWidth
/2;
6993 rect
.width
-= penWidth
-1;
6994 rect
.height
-= penWidth
-1;
6997 // Now draw the rectangle
6998 // use the cellHighlightColour if the cell is inside a selection, this
6999 // will ensure the cell is always visible.
7000 dc
.SetPen(wxPen(IsInSelection(row
,col
)?m_selectionForeground
:m_cellHighlightColour
, penWidth
, wxSOLID
));
7001 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
7002 dc
.DrawRectangle(rect
);
7006 // VZ: my experiments with 3d borders...
7008 // how to properly set colours for arbitrary bg?
7009 wxCoord x1
= rect
.x
,
7011 x2
= rect
.x
+ rect
.width
-1,
7012 y2
= rect
.y
+ rect
.height
-1;
7014 dc
.SetPen(*wxWHITE_PEN
);
7015 dc
.DrawLine(x1
, y1
, x2
, y1
);
7016 dc
.DrawLine(x1
, y1
, x1
, y2
);
7018 dc
.DrawLine(x1
+ 1, y2
- 1, x2
- 1, y2
- 1);
7019 dc
.DrawLine(x2
- 1, y1
+ 1, x2
- 1, y2
);
7021 dc
.SetPen(*wxBLACK_PEN
);
7022 dc
.DrawLine(x1
, y2
, x2
, y2
);
7023 dc
.DrawLine(x2
, y1
, x2
, y2
+1);
7028 void wxGrid::DrawCellBorder( wxDC
& dc
, const wxGridCellCoords
& coords
)
7030 int row
= coords
.GetRow();
7031 int col
= coords
.GetCol();
7032 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
7035 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7037 wxRect rect
= CellToRect( row
, col
);
7039 // right hand border
7041 dc
.DrawLine( rect
.x
+ rect
.width
, rect
.y
,
7042 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
+ 1 );
7046 dc
.DrawLine( rect
.x
, rect
.y
+ rect
.height
,
7047 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
7050 void wxGrid::DrawHighlight(wxDC
& dc
,const wxGridCellCoordsArray
& cells
)
7052 // This if block was previously in wxGrid::OnPaint but that doesn't
7053 // seem to get called under wxGTK - MB
7055 if ( m_currentCellCoords
== wxGridNoCellCoords
&&
7056 m_numRows
&& m_numCols
)
7058 m_currentCellCoords
.Set(0, 0);
7061 if ( IsCellEditControlShown() )
7063 // don't show highlight when the edit control is shown
7067 // if the active cell was repainted, repaint its highlight too because it
7068 // might have been damaged by the grid lines
7069 size_t count
= cells
.GetCount();
7070 for ( size_t n
= 0; n
< count
; n
++ )
7072 if ( cells
[n
] == m_currentCellCoords
)
7074 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
7075 DrawCellHighlight(dc
, attr
);
7083 // TODO: remove this ???
7084 // This is used to redraw all grid lines e.g. when the grid line colour
7087 void wxGrid::DrawAllGridLines( wxDC
& dc
, const wxRegion
& WXUNUSED(reg
) )
7089 #if !WXGRID_DRAW_LINES
7093 if ( !m_gridLinesEnabled
||
7095 !m_numCols
) return;
7097 int top
, bottom
, left
, right
;
7099 #if 0 //#ifndef __WXGTK__
7103 m_gridWin
->GetClientSize(&cw
, &ch
);
7105 // virtual coords of visible area
7107 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7108 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7113 reg
.GetBox(x
, y
, w
, h
);
7114 CalcUnscrolledPosition( x
, y
, &left
, &top
);
7115 CalcUnscrolledPosition( x
+ w
, y
+ h
, &right
, &bottom
);
7119 m_gridWin
->GetClientSize(&cw
, &ch
);
7120 CalcUnscrolledPosition( 0, 0, &left
, &top
);
7121 CalcUnscrolledPosition( cw
, ch
, &right
, &bottom
);
7124 // avoid drawing grid lines past the last row and col
7126 right
= wxMin( right
, GetColRight(m_numCols
- 1) );
7127 bottom
= wxMin( bottom
, GetRowBottom(m_numRows
- 1) );
7129 // no gridlines inside multicells, clip them out
7130 int leftCol
= internalXToCol(left
);
7131 int topRow
= internalYToRow(top
);
7132 int rightCol
= internalXToCol(right
);
7133 int bottomRow
= internalYToRow(bottom
);
7134 wxRegion
clippedcells(0, 0, cw
, ch
);
7137 int i
, j
, cell_rows
, cell_cols
;
7140 for (j
=topRow
; j
<bottomRow
; j
++)
7142 for (i
=leftCol
; i
<rightCol
; i
++)
7144 GetCellSize( j
, i
, &cell_rows
, &cell_cols
);
7145 if ((cell_rows
> 1) || (cell_cols
> 1))
7147 rect
= CellToRect(j
,i
);
7148 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7149 clippedcells
.Subtract(rect
);
7151 else if ((cell_rows
< 0) || (cell_cols
< 0))
7153 rect
= CellToRect(j
+cell_rows
, i
+cell_cols
);
7154 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7155 clippedcells
.Subtract(rect
);
7159 dc
.SetClippingRegion( clippedcells
);
7161 dc
.SetPen( wxPen(GetGridLineColour(), 1, wxSOLID
) );
7163 // horizontal grid lines
7165 // already declared above - int i;
7166 for ( i
= internalYToRow(top
); i
< m_numRows
; i
++ )
7168 int bot
= GetRowBottom(i
) - 1;
7177 dc
.DrawLine( left
, bot
, right
, bot
);
7182 // vertical grid lines
7184 for ( i
= internalXToCol(left
); i
< m_numCols
; i
++ )
7186 int colRight
= GetColRight(i
) - 1;
7187 if ( colRight
> right
)
7192 if ( colRight
>= left
)
7194 dc
.DrawLine( colRight
, top
, colRight
, bottom
);
7197 dc
.DestroyClippingRegion();
7201 void wxGrid::DrawRowLabels( wxDC
& dc
,const wxArrayInt
& rows
)
7203 if ( !m_numRows
) return;
7206 size_t numLabels
= rows
.GetCount();
7208 for ( i
= 0; i
< numLabels
; i
++ )
7210 DrawRowLabel( dc
, rows
[i
] );
7215 void wxGrid::DrawRowLabel( wxDC
& dc
, int row
)
7217 if ( GetRowHeight(row
) <= 0 )
7223 rect
.SetY( GetRowTop(row
) + 1 );
7224 rect
.SetWidth( m_rowLabelWidth
- 2 );
7225 rect
.SetHeight( GetRowHeight(row
) - 2 );
7227 CalcScrolledPosition( 0, rect
.y
, NULL
, &rect
.y
);
7229 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7231 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7233 int rowTop
= GetRowTop(row
),
7234 rowBottom
= GetRowBottom(row
) - 1;
7236 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7237 dc
.DrawLine( m_rowLabelWidth
-1, rowTop
,
7238 m_rowLabelWidth
-1, rowBottom
);
7240 dc
.DrawLine( 0, rowTop
, 0, rowBottom
);
7242 dc
.DrawLine( 0, rowBottom
, m_rowLabelWidth
, rowBottom
);
7244 dc
.SetPen( *wxWHITE_PEN
);
7245 dc
.DrawLine( 1, rowTop
, 1, rowBottom
);
7246 dc
.DrawLine( 1, rowTop
, m_rowLabelWidth
-1, rowTop
);
7248 dc
.SetBackgroundMode( wxTRANSPARENT
);
7249 dc
.SetTextForeground( GetLabelTextColour() );
7250 dc
.SetFont( GetLabelFont() );
7253 GetRowLabelAlignment( &hAlign
, &vAlign
);
7256 rect
.SetY( GetRowTop(row
) + 2 );
7257 rect
.SetWidth( m_rowLabelWidth
- 4 );
7258 rect
.SetHeight( GetRowHeight(row
) - 4 );
7259 DrawTextRectangle( dc
, GetRowLabelValue( row
), rect
, hAlign
, vAlign
);
7263 void wxGrid::DrawColLabels( wxDC
& dc
,const wxArrayInt
& cols
)
7265 if ( !m_numCols
) return;
7268 size_t numLabels
= cols
.GetCount();
7270 for ( i
= 0; i
< numLabels
; i
++ )
7272 DrawColLabel( dc
, cols
[i
] );
7277 void wxGrid::DrawColLabel( wxDC
& dc
, int col
)
7279 if ( GetColWidth(col
) <= 0 )
7282 int colLeft
= GetColLeft(col
);
7286 rect
.SetX( colLeft
+ 1 );
7288 rect
.SetWidth( GetColWidth(col
) - 2 );
7289 rect
.SetHeight( m_colLabelHeight
- 2 );
7291 wxWindowDC
*win_dc
= (wxWindowDC
*) &dc
;
7293 wxRendererNative::Get().DrawHeaderButton( win_dc
->m_owner
, dc
, rect
, 0 );
7295 int colRight
= GetColRight(col
) - 1;
7297 dc
.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW
),1, wxSOLID
) );
7298 dc
.DrawLine( colRight
, 0,
7299 colRight
, m_colLabelHeight
-1 );
7301 dc
.DrawLine( colLeft
, 0, colRight
, 0 );
7303 dc
.DrawLine( colLeft
, m_colLabelHeight
-1,
7304 colRight
+1, m_colLabelHeight
-1 );
7306 dc
.SetPen( *wxWHITE_PEN
);
7307 dc
.DrawLine( colLeft
, 1, colLeft
, m_colLabelHeight
-1 );
7308 dc
.DrawLine( colLeft
, 1, colRight
, 1 );
7310 dc
.SetBackgroundMode( wxTRANSPARENT
);
7311 dc
.SetTextForeground( GetLabelTextColour() );
7312 dc
.SetFont( GetLabelFont() );
7314 int hAlign
, vAlign
, orient
;
7315 GetColLabelAlignment( &hAlign
, &vAlign
);
7316 orient
= GetColLabelTextOrientation();
7318 rect
.SetX( colLeft
+ 2 );
7320 rect
.SetWidth( GetColWidth(col
) - 4 );
7321 rect
.SetHeight( m_colLabelHeight
- 4 );
7322 DrawTextRectangle( dc
, GetColLabelValue( col
), rect
, hAlign
, vAlign
, orient
);
7325 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7326 const wxString
& value
,
7330 int textOrientation
)
7332 wxArrayString lines
;
7334 StringToLines( value
, lines
);
7337 //Forward to new API.
7338 DrawTextRectangle( dc
,
7347 void wxGrid::DrawTextRectangle( wxDC
& dc
,
7348 const wxArrayString
& lines
,
7352 int textOrientation
)
7354 long textWidth
, textHeight
;
7355 long lineWidth
, lineHeight
;
7358 dc
.SetClippingRegion( rect
);
7360 nLines
= lines
.GetCount();
7364 float x
= 0.0, y
= 0.0;
7366 if( textOrientation
== wxHORIZONTAL
)
7367 GetTextBoxSize(dc
, lines
, &textWidth
, &textHeight
);
7369 GetTextBoxSize( dc
, lines
, &textHeight
, &textWidth
);
7373 case wxALIGN_BOTTOM
:
7374 if( textOrientation
== wxHORIZONTAL
)
7375 y
= rect
.y
+ (rect
.height
- textHeight
- 1);
7377 x
= rect
.x
+ rect
.width
- textWidth
;
7380 case wxALIGN_CENTRE
:
7381 if( textOrientation
== wxHORIZONTAL
)
7382 y
= rect
.y
+ ((rect
.height
- textHeight
)/2);
7384 x
= rect
.x
+ ((rect
.width
- textWidth
)/2);
7389 if( textOrientation
== wxHORIZONTAL
)
7396 // Align each line of a multi-line label
7397 for( l
= 0; l
< nLines
; l
++ )
7399 dc
.GetTextExtent(lines
[l
], &lineWidth
, &lineHeight
);
7401 switch( horizAlign
)
7404 if( textOrientation
== wxHORIZONTAL
)
7405 x
= rect
.x
+ (rect
.width
- lineWidth
- 1);
7407 y
= rect
.y
+ lineWidth
+ 1;
7410 case wxALIGN_CENTRE
:
7411 if( textOrientation
== wxHORIZONTAL
)
7412 x
= rect
.x
+ ((rect
.width
- lineWidth
)/2);
7414 y
= rect
.y
+ rect
.height
- ((rect
.height
- lineWidth
)/2);
7419 if( textOrientation
== wxHORIZONTAL
)
7422 y
= rect
.y
+ rect
.height
- 1;
7426 if( textOrientation
== wxHORIZONTAL
)
7428 dc
.DrawText( lines
[l
], (int)x
, (int)y
);
7433 dc
.DrawRotatedText( lines
[l
], (int)x
, (int)y
, 90.0 );
7438 dc
.DestroyClippingRegion();
7442 // Split multi line text up into an array of strings. Any existing
7443 // contents of the string array are preserved.
7445 void wxGrid::StringToLines( const wxString
& value
, wxArrayString
& lines
)
7449 wxString eol
= wxTextFile::GetEOL( wxTextFileType_Unix
);
7450 wxString tVal
= wxTextFile::Translate( value
, wxTextFileType_Unix
);
7452 while ( startPos
< (int)tVal
.Length() )
7454 pos
= tVal
.Mid(startPos
).Find( eol
);
7459 else if ( pos
== 0 )
7461 lines
.Add( wxEmptyString
);
7465 lines
.Add( value
.Mid(startPos
, pos
) );
7469 if ( startPos
< (int)value
.Length() )
7471 lines
.Add( value
.Mid( startPos
) );
7476 void wxGrid::GetTextBoxSize( wxDC
& dc
,
7477 const wxArrayString
& lines
,
7478 long *width
, long *height
)
7485 for ( i
= 0; i
< lines
.GetCount(); i
++ )
7487 dc
.GetTextExtent( lines
[i
], &lineW
, &lineH
);
7488 w
= wxMax( w
, lineW
);
7497 // ------ Batch processing.
7499 void wxGrid::EndBatch()
7501 if ( m_batchCount
> 0 )
7504 if ( !m_batchCount
)
7507 m_rowLabelWin
->Refresh();
7508 m_colLabelWin
->Refresh();
7509 m_cornerLabelWin
->Refresh();
7510 m_gridWin
->Refresh();
7515 // Use this, rather than wxWindow::Refresh(), to force an immediate
7516 // repainting of the grid. Has no effect if you are already inside a
7517 // BeginBatch / EndBatch block.
7519 void wxGrid::ForceRefresh()
7527 // ------ Edit control functions
7531 void wxGrid::EnableEditing( bool edit
)
7533 // TODO: improve this ?
7535 if ( edit
!= m_editable
)
7537 if(!edit
) EnableCellEditControl(edit
);
7543 void wxGrid::EnableCellEditControl( bool enable
)
7548 if ( m_currentCellCoords
== wxGridNoCellCoords
)
7549 SetCurrentCell( 0, 0 );
7551 if ( enable
!= m_cellEditCtrlEnabled
)
7555 if (SendEvent( wxEVT_GRID_EDITOR_SHOWN
) <0)
7558 // this should be checked by the caller!
7559 wxASSERT_MSG( CanEnableCellControl(),
7560 _T("can't enable editing for this cell!") );
7562 // do it before ShowCellEditControl()
7563 m_cellEditCtrlEnabled
= enable
;
7565 ShowCellEditControl();
7569 //FIXME:add veto support
7570 SendEvent( wxEVT_GRID_EDITOR_HIDDEN
);
7572 HideCellEditControl();
7573 SaveEditControlValue();
7575 // do it after HideCellEditControl()
7576 m_cellEditCtrlEnabled
= enable
;
7581 bool wxGrid::IsCurrentCellReadOnly() const
7584 wxGridCellAttr
* attr
= ((wxGrid
*)this)->GetCellAttr(m_currentCellCoords
);
7585 bool readonly
= attr
->IsReadOnly();
7591 bool wxGrid::CanEnableCellControl() const
7593 return m_editable
&& (m_currentCellCoords
!= wxGridNoCellCoords
) &&
7594 !IsCurrentCellReadOnly();
7598 bool wxGrid::IsCellEditControlEnabled() const
7600 // the cell edit control might be disable for all cells or just for the
7601 // current one if it's read only
7602 return m_cellEditCtrlEnabled
? !IsCurrentCellReadOnly() : false;
7605 bool wxGrid::IsCellEditControlShown() const
7607 bool isShown
= false;
7609 if ( m_cellEditCtrlEnabled
)
7611 int row
= m_currentCellCoords
.GetRow();
7612 int col
= m_currentCellCoords
.GetCol();
7613 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7614 wxGridCellEditor
* editor
= attr
->GetEditor((wxGrid
*) this, row
, col
);
7619 if ( editor
->IsCreated() )
7621 isShown
= editor
->GetControl()->IsShown();
7631 void wxGrid::ShowCellEditControl()
7633 if ( IsCellEditControlEnabled() )
7635 if ( !IsVisible( m_currentCellCoords
) )
7637 m_cellEditCtrlEnabled
= false;
7642 wxRect rect
= CellToRect( m_currentCellCoords
);
7643 int row
= m_currentCellCoords
.GetRow();
7644 int col
= m_currentCellCoords
.GetCol();
7646 // if this is part of a multicell, find owner (topleft)
7647 int cell_rows
, cell_cols
;
7648 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7649 if ( cell_rows
<= 0 || cell_cols
<= 0 )
7653 m_currentCellCoords
.SetRow( row
);
7654 m_currentCellCoords
.SetCol( col
);
7657 // convert to scrolled coords
7659 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7661 // done in PaintBackground()
7663 // erase the highlight and the cell contents because the editor
7664 // might not cover the entire cell
7665 wxClientDC
dc( m_gridWin
);
7667 dc
.SetBrush(*wxLIGHT_GREY_BRUSH
); //wxBrush(attr->GetBackgroundColour(), wxSOLID));
7668 dc
.SetPen(*wxTRANSPARENT_PEN
);
7669 dc
.DrawRectangle(rect
);
7672 // cell is shifted by one pixel
7673 // However, don't allow x or y to become negative
7674 // since the SetSize() method interprets that as
7681 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7682 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7683 if ( !editor
->IsCreated() )
7685 editor
->Create(m_gridWin
, wxID_ANY
,
7686 new wxGridCellEditorEvtHandler(this, editor
));
7688 wxGridEditorCreatedEvent
evt(GetId(),
7689 wxEVT_GRID_EDITOR_CREATED
,
7693 editor
->GetControl());
7694 GetEventHandler()->ProcessEvent(evt
);
7698 // resize editor to overflow into righthand cells if allowed
7699 int maxWidth
= rect
.width
;
7700 wxString value
= GetCellValue(row
, col
);
7701 if ( (value
!= wxEmptyString
) && (attr
->GetOverflow()) )
7704 GetTextExtent(value
, &maxWidth
, &y
,
7705 NULL
, NULL
, &attr
->GetFont());
7706 if (maxWidth
< rect
.width
) maxWidth
= rect
.width
;
7708 int client_right
= m_gridWin
->GetClientSize().GetWidth();
7709 if (rect
.x
+maxWidth
> client_right
)
7710 maxWidth
= client_right
- rect
.x
;
7712 if ((maxWidth
> rect
.width
) && (col
< m_numCols
) && m_table
)
7714 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7715 // may have changed earlier
7716 for (int i
= col
+cell_cols
; i
< m_numCols
; i
++)
7719 GetCellSize( row
, i
, &c_rows
, &c_cols
);
7720 // looks weird going over a multicell
7721 if (m_table
->IsEmptyCell(row
,i
) &&
7722 (rect
.width
< maxWidth
) && (c_rows
== 1))
7723 rect
.width
+= GetColWidth(i
);
7727 if (rect
.GetRight() > client_right
)
7728 rect
.SetRight(client_right
-1);
7731 editor
->SetCellAttr(attr
);
7732 editor
->SetSize( rect
);
7733 editor
->Show( true, attr
);
7735 // recalc dimensions in case we need to
7736 // expand the scrolled window to account for editor
7739 editor
->BeginEdit(row
, col
, this);
7740 editor
->SetCellAttr(NULL
);
7749 void wxGrid::HideCellEditControl()
7751 if ( IsCellEditControlEnabled() )
7753 int row
= m_currentCellCoords
.GetRow();
7754 int col
= m_currentCellCoords
.GetCol();
7756 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7757 wxGridCellEditor
*editor
= attr
->GetEditor(this, row
, col
);
7758 editor
->Show( false );
7761 m_gridWin
->SetFocus();
7762 // refresh whole row to the right
7763 wxRect
rect( CellToRect(row
, col
) );
7764 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
7765 rect
.width
= m_gridWin
->GetClientSize().GetWidth() - rect
.x
;
7766 m_gridWin
->Refresh( false, &rect
);
7771 void wxGrid::SaveEditControlValue()
7773 if ( IsCellEditControlEnabled() )
7775 int row
= m_currentCellCoords
.GetRow();
7776 int col
= m_currentCellCoords
.GetCol();
7778 wxString oldval
= GetCellValue(row
,col
);
7780 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
7781 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
7782 bool changed
= editor
->EndEdit(row
, col
, this);
7789 if ( SendEvent( wxEVT_GRID_CELL_CHANGE
,
7790 m_currentCellCoords
.GetRow(),
7791 m_currentCellCoords
.GetCol() ) < 0 ) {
7793 // Event has been vetoed, set the data back.
7794 SetCellValue(row
,col
,oldval
);
7802 // ------ Grid location functions
7803 // Note that all of these functions work with the logical coordinates of
7804 // grid cells and labels so you will need to convert from device
7805 // coordinates for mouse events etc.
7808 void wxGrid::XYToCell( int x
, int y
, wxGridCellCoords
& coords
)
7810 int row
= YToRow(y
);
7811 int col
= XToCol(x
);
7813 if ( row
== -1 || col
== -1 )
7815 coords
= wxGridNoCellCoords
;
7819 coords
.Set( row
, col
);
7824 // Internal Helper function for computing row or column from some
7825 // (unscrolled) coordinate value, using either
7826 // m_defaultRowHeight/m_defaultColWidth or binary search on array
7827 // of m_rowBottoms/m_ColRights to speed up the search!
7829 static int CoordToRowOrCol(int coord
, int defaultDist
, int minDist
,
7830 const wxArrayInt
& BorderArray
, int nMax
,
7835 return clipToMinMax
&& (nMax
> 0) ? 0 : -1;
7841 size_t i_max
= coord
/ defaultDist
,
7844 if (BorderArray
.IsEmpty())
7846 if((int) i_max
< nMax
)
7848 return clipToMinMax
? nMax
- 1 : -1;
7851 if ( i_max
>= BorderArray
.GetCount())
7852 i_max
= BorderArray
.GetCount() - 1;
7855 if ( coord
>= BorderArray
[i_max
])
7859 i_max
= coord
/ minDist
;
7861 i_max
= BorderArray
.GetCount() - 1;
7863 if ( i_max
>= BorderArray
.GetCount())
7864 i_max
= BorderArray
.GetCount() - 1;
7866 if ( coord
>= BorderArray
[i_max
])
7867 return clipToMinMax
? (int)i_max
: -1;
7868 if ( coord
< BorderArray
[0] )
7871 while ( i_max
- i_min
> 0 )
7873 wxCHECK_MSG(BorderArray
[i_min
] <= coord
&& coord
< BorderArray
[i_max
],
7874 0, _T("wxGrid: internal error in CoordToRowOrCol"));
7875 if (coord
>= BorderArray
[ i_max
- 1])
7879 int median
= i_min
+ (i_max
- i_min
+ 1) / 2;
7880 if (coord
< BorderArray
[median
])
7888 int wxGrid::YToRow( int y
)
7890 return CoordToRowOrCol(y
, m_defaultRowHeight
,
7891 m_minAcceptableRowHeight
, m_rowBottoms
, m_numRows
, false);
7895 int wxGrid::XToCol( int x
)
7897 return CoordToRowOrCol(x
, m_defaultColWidth
,
7898 m_minAcceptableColWidth
, m_colRights
, m_numCols
, false);
7902 // return the row number that that the y coord is near the edge of, or
7903 // -1 if not near an edge
7905 int wxGrid::YToEdgeOfRow( int y
)
7908 i
= internalYToRow(y
);
7910 if ( GetRowHeight(i
) > WXGRID_LABEL_EDGE_ZONE
)
7912 // We know that we are in row i, test whether we are
7913 // close enough to lower or upper border, respectively.
7914 if ( abs(GetRowBottom(i
) - y
) < WXGRID_LABEL_EDGE_ZONE
)
7916 else if( i
> 0 && y
- GetRowTop(i
) < WXGRID_LABEL_EDGE_ZONE
)
7924 // return the col number that that the x coord is near the edge of, or
7925 // -1 if not near an edge
7927 int wxGrid::XToEdgeOfCol( int x
)
7930 i
= internalXToCol(x
);
7932 if ( GetColWidth(i
) > WXGRID_LABEL_EDGE_ZONE
)
7934 // We know that we are in column i, test whether we are
7935 // close enough to right or left border, respectively.
7936 if ( abs(GetColRight(i
) - x
) < WXGRID_LABEL_EDGE_ZONE
)
7938 else if( i
> 0 && x
- GetColLeft(i
) < WXGRID_LABEL_EDGE_ZONE
)
7946 wxRect
wxGrid::CellToRect( int row
, int col
)
7948 wxRect
rect( -1, -1, -1, -1 );
7950 if ( row
>= 0 && row
< m_numRows
&&
7951 col
>= 0 && col
< m_numCols
)
7953 int i
, cell_rows
, cell_cols
;
7954 rect
.width
= rect
.height
= 0;
7955 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7956 // if negative then find multicell owner
7957 if (cell_rows
< 0) row
+= cell_rows
;
7958 if (cell_cols
< 0) col
+= cell_cols
;
7959 GetCellSize( row
, col
, &cell_rows
, &cell_cols
);
7961 rect
.x
= GetColLeft(col
);
7962 rect
.y
= GetRowTop(row
);
7963 for (i
=col
; i
<col
+cell_cols
; i
++)
7964 rect
.width
+= GetColWidth(i
);
7965 for (i
=row
; i
<row
+cell_rows
; i
++)
7966 rect
.height
+= GetRowHeight(i
);
7969 // if grid lines are enabled, then the area of the cell is a bit smaller
7970 if (m_gridLinesEnabled
) {
7978 bool wxGrid::IsVisible( int row
, int col
, bool wholeCellVisible
)
7980 // get the cell rectangle in logical coords
7982 wxRect
r( CellToRect( row
, col
) );
7984 // convert to device coords
7986 int left
, top
, right
, bottom
;
7987 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
7988 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
7990 // check against the client area of the grid window
7993 m_gridWin
->GetClientSize( &cw
, &ch
);
7995 if ( wholeCellVisible
)
7997 // is the cell wholly visible ?
7999 return ( left
>= 0 && right
<= cw
&&
8000 top
>= 0 && bottom
<= ch
);
8004 // is the cell partly visible ?
8006 return ( ((left
>=0 && left
< cw
) || (right
> 0 && right
<= cw
)) &&
8007 ((top
>=0 && top
< ch
) || (bottom
> 0 && bottom
<= ch
)) );
8012 // make the specified cell location visible by doing a minimal amount
8015 void wxGrid::MakeCellVisible( int row
, int col
)
8019 int xpos
= -1, ypos
= -1;
8021 if ( row
>= 0 && row
< m_numRows
&&
8022 col
>= 0 && col
< m_numCols
)
8024 // get the cell rectangle in logical coords
8026 wxRect
r( CellToRect( row
, col
) );
8028 // convert to device coords
8030 int left
, top
, right
, bottom
;
8031 CalcScrolledPosition( r
.GetLeft(), r
.GetTop(), &left
, &top
);
8032 CalcScrolledPosition( r
.GetRight(), r
.GetBottom(), &right
, &bottom
);
8035 m_gridWin
->GetClientSize( &cw
, &ch
);
8041 else if ( bottom
> ch
)
8043 int h
= r
.GetHeight();
8045 for ( i
= row
-1; i
>= 0; i
-- )
8047 int rowHeight
= GetRowHeight(i
);
8048 if ( h
+ rowHeight
> ch
)
8055 // we divide it later by GRID_SCROLL_LINE, make sure that we don't
8056 // have rounding errors (this is important, because if we do, we
8057 // might not scroll at all and some cells won't be redrawn)
8059 // Sometimes GRID_SCROLL_LINE/2 is not enough, so just add a full
8061 ypos
+= GRID_SCROLL_LINE_Y
;
8068 else if ( right
> cw
)
8070 // position the view so that the cell is on the right
8072 CalcUnscrolledPosition(0, 0, &x0
, &y0
);
8073 xpos
= x0
+ (right
- cw
);
8075 // see comment for ypos above
8076 xpos
+= GRID_SCROLL_LINE_X
;
8079 if ( xpos
!= -1 || ypos
!= -1 )
8082 xpos
/= GRID_SCROLL_LINE_X
;
8084 ypos
/= GRID_SCROLL_LINE_Y
;
8085 Scroll( xpos
, ypos
);
8093 // ------ Grid cursor movement functions
8096 bool wxGrid::MoveCursorUp( bool expandSelection
)
8098 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8099 m_currentCellCoords
.GetRow() >= 0 )
8101 if ( expandSelection
)
8103 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8104 m_selectingKeyboard
= m_currentCellCoords
;
8105 if ( m_selectingKeyboard
.GetRow() > 0 )
8107 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() - 1 );
8108 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8109 m_selectingKeyboard
.GetCol() );
8110 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8113 else if ( m_currentCellCoords
.GetRow() > 0 )
8116 MakeCellVisible( m_currentCellCoords
.GetRow() - 1,
8117 m_currentCellCoords
.GetCol() );
8118 SetCurrentCell( m_currentCellCoords
.GetRow() - 1,
8119 m_currentCellCoords
.GetCol() );
8130 bool wxGrid::MoveCursorDown( bool expandSelection
)
8132 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8133 m_currentCellCoords
.GetRow() < m_numRows
)
8135 if ( expandSelection
)
8137 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8138 m_selectingKeyboard
= m_currentCellCoords
;
8139 if ( m_selectingKeyboard
.GetRow() < m_numRows
-1 )
8141 m_selectingKeyboard
.SetRow( m_selectingKeyboard
.GetRow() + 1 );
8142 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8143 m_selectingKeyboard
.GetCol() );
8144 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8147 else if ( m_currentCellCoords
.GetRow() < m_numRows
- 1 )
8150 MakeCellVisible( m_currentCellCoords
.GetRow() + 1,
8151 m_currentCellCoords
.GetCol() );
8152 SetCurrentCell( m_currentCellCoords
.GetRow() + 1,
8153 m_currentCellCoords
.GetCol() );
8164 bool wxGrid::MoveCursorLeft( bool expandSelection
)
8166 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8167 m_currentCellCoords
.GetCol() >= 0 )
8169 if ( expandSelection
)
8171 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8172 m_selectingKeyboard
= m_currentCellCoords
;
8173 if ( m_selectingKeyboard
.GetCol() > 0 )
8175 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() - 1 );
8176 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8177 m_selectingKeyboard
.GetCol() );
8178 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8181 else if ( m_currentCellCoords
.GetCol() > 0 )
8184 MakeCellVisible( m_currentCellCoords
.GetRow(),
8185 m_currentCellCoords
.GetCol() - 1 );
8186 SetCurrentCell( m_currentCellCoords
.GetRow(),
8187 m_currentCellCoords
.GetCol() - 1 );
8198 bool wxGrid::MoveCursorRight( bool expandSelection
)
8200 if ( m_currentCellCoords
!= wxGridNoCellCoords
&&
8201 m_currentCellCoords
.GetCol() < m_numCols
)
8203 if ( expandSelection
)
8205 if ( m_selectingKeyboard
== wxGridNoCellCoords
)
8206 m_selectingKeyboard
= m_currentCellCoords
;
8207 if ( m_selectingKeyboard
.GetCol() < m_numCols
- 1 )
8209 m_selectingKeyboard
.SetCol( m_selectingKeyboard
.GetCol() + 1 );
8210 MakeCellVisible( m_selectingKeyboard
.GetRow(),
8211 m_selectingKeyboard
.GetCol() );
8212 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8215 else if ( m_currentCellCoords
.GetCol() < m_numCols
- 1 )
8218 MakeCellVisible( m_currentCellCoords
.GetRow(),
8219 m_currentCellCoords
.GetCol() + 1 );
8220 SetCurrentCell( m_currentCellCoords
.GetRow(),
8221 m_currentCellCoords
.GetCol() + 1 );
8232 bool wxGrid::MovePageUp()
8234 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8236 int row
= m_currentCellCoords
.GetRow();
8240 m_gridWin
->GetClientSize( &cw
, &ch
);
8242 int y
= GetRowTop(row
);
8243 int newRow
= internalYToRow( y
- ch
+ 1 );
8245 if ( newRow
== row
)
8247 //row > 0 , so newrow can never be less than 0 here.
8251 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8252 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8260 bool wxGrid::MovePageDown()
8262 if ( m_currentCellCoords
== wxGridNoCellCoords
) return false;
8264 int row
= m_currentCellCoords
.GetRow();
8265 if ( (row
+1) < m_numRows
)
8268 m_gridWin
->GetClientSize( &cw
, &ch
);
8270 int y
= GetRowTop(row
);
8271 int newRow
= internalYToRow( y
+ ch
);
8272 if ( newRow
== row
)
8274 // row < m_numRows , so newrow can't overflow here.
8278 MakeCellVisible( newRow
, m_currentCellCoords
.GetCol() );
8279 SetCurrentCell( newRow
, m_currentCellCoords
.GetCol() );
8287 bool wxGrid::MoveCursorUpBlock( bool expandSelection
)
8290 m_currentCellCoords
!= wxGridNoCellCoords
&&
8291 m_currentCellCoords
.GetRow() > 0 )
8293 int row
= m_currentCellCoords
.GetRow();
8294 int col
= m_currentCellCoords
.GetCol();
8296 if ( m_table
->IsEmptyCell(row
, col
) )
8298 // starting in an empty cell: find the next block of
8304 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8307 else if ( m_table
->IsEmptyCell(row
-1, col
) )
8309 // starting at the top of a block: find the next block
8315 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8320 // starting within a block: find the top of the block
8325 if ( m_table
->IsEmptyCell(row
, col
) )
8333 MakeCellVisible( row
, col
);
8334 if ( expandSelection
)
8336 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8337 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8342 SetCurrentCell( row
, col
);
8350 bool wxGrid::MoveCursorDownBlock( bool expandSelection
)
8353 m_currentCellCoords
!= wxGridNoCellCoords
&&
8354 m_currentCellCoords
.GetRow() < m_numRows
-1 )
8356 int row
= m_currentCellCoords
.GetRow();
8357 int col
= m_currentCellCoords
.GetCol();
8359 if ( m_table
->IsEmptyCell(row
, col
) )
8361 // starting in an empty cell: find the next block of
8364 while ( row
< m_numRows
-1 )
8367 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8370 else if ( m_table
->IsEmptyCell(row
+1, col
) )
8372 // starting at the bottom of a block: find the next block
8375 while ( row
< m_numRows
-1 )
8378 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8383 // starting within a block: find the bottom of the block
8385 while ( row
< m_numRows
-1 )
8388 if ( m_table
->IsEmptyCell(row
, col
) )
8396 MakeCellVisible( row
, col
);
8397 if ( expandSelection
)
8399 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8400 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8405 SetCurrentCell( row
, col
);
8414 bool wxGrid::MoveCursorLeftBlock( bool expandSelection
)
8417 m_currentCellCoords
!= wxGridNoCellCoords
&&
8418 m_currentCellCoords
.GetCol() > 0 )
8420 int row
= m_currentCellCoords
.GetRow();
8421 int col
= m_currentCellCoords
.GetCol();
8423 if ( m_table
->IsEmptyCell(row
, col
) )
8425 // starting in an empty cell: find the next block of
8431 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8434 else if ( m_table
->IsEmptyCell(row
, col
-1) )
8436 // starting at the left of a block: find the next block
8442 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8447 // starting within a block: find the left of the block
8452 if ( m_table
->IsEmptyCell(row
, col
) )
8460 MakeCellVisible( row
, col
);
8461 if ( expandSelection
)
8463 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8464 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8469 SetCurrentCell( row
, col
);
8478 bool wxGrid::MoveCursorRightBlock( bool expandSelection
)
8481 m_currentCellCoords
!= wxGridNoCellCoords
&&
8482 m_currentCellCoords
.GetCol() < m_numCols
-1 )
8484 int row
= m_currentCellCoords
.GetRow();
8485 int col
= m_currentCellCoords
.GetCol();
8487 if ( m_table
->IsEmptyCell(row
, col
) )
8489 // starting in an empty cell: find the next block of
8492 while ( col
< m_numCols
-1 )
8495 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8498 else if ( m_table
->IsEmptyCell(row
, col
+1) )
8500 // starting at the right of a block: find the next block
8503 while ( col
< m_numCols
-1 )
8506 if ( !(m_table
->IsEmptyCell(row
, col
)) ) break;
8511 // starting within a block: find the right of the block
8513 while ( col
< m_numCols
-1 )
8516 if ( m_table
->IsEmptyCell(row
, col
) )
8524 MakeCellVisible( row
, col
);
8525 if ( expandSelection
)
8527 m_selectingKeyboard
= wxGridCellCoords( row
, col
);
8528 HighlightBlock( m_currentCellCoords
, m_selectingKeyboard
);
8533 SetCurrentCell( row
, col
);
8545 // ------ Label values and formatting
8548 void wxGrid::GetRowLabelAlignment( int *horiz
, int *vert
)
8550 *horiz
= m_rowLabelHorizAlign
;
8551 *vert
= m_rowLabelVertAlign
;
8554 void wxGrid::GetColLabelAlignment( int *horiz
, int *vert
)
8556 *horiz
= m_colLabelHorizAlign
;
8557 *vert
= m_colLabelVertAlign
;
8560 int wxGrid::GetColLabelTextOrientation()
8562 return m_colLabelTextOrientation
;
8565 wxString
wxGrid::GetRowLabelValue( int row
)
8569 return m_table
->GetRowLabelValue( row
);
8579 wxString
wxGrid::GetColLabelValue( int col
)
8583 return m_table
->GetColLabelValue( col
);
8594 void wxGrid::SetRowLabelSize( int width
)
8596 width
= wxMax( width
, 0 );
8597 if ( width
!= m_rowLabelWidth
)
8601 m_rowLabelWin
->Show( false );
8602 m_cornerLabelWin
->Show( false );
8604 else if ( m_rowLabelWidth
== 0 )
8606 m_rowLabelWin
->Show( true );
8607 if ( m_colLabelHeight
> 0 ) m_cornerLabelWin
->Show( true );
8610 m_rowLabelWidth
= width
;
8612 wxScrolledWindow::Refresh( true );
8617 void wxGrid::SetColLabelSize( int height
)
8619 height
= wxMax( height
, 0 );
8620 if ( height
!= m_colLabelHeight
)
8624 m_colLabelWin
->Show( false );
8625 m_cornerLabelWin
->Show( false );
8627 else if ( m_colLabelHeight
== 0 )
8629 m_colLabelWin
->Show( true );
8630 if ( m_rowLabelWidth
> 0 ) m_cornerLabelWin
->Show( true );
8633 m_colLabelHeight
= height
;
8635 wxScrolledWindow::Refresh( true );
8640 void wxGrid::SetLabelBackgroundColour( const wxColour
& colour
)
8642 if ( m_labelBackgroundColour
!= colour
)
8644 m_labelBackgroundColour
= colour
;
8645 m_rowLabelWin
->SetBackgroundColour( colour
);
8646 m_colLabelWin
->SetBackgroundColour( colour
);
8647 m_cornerLabelWin
->SetBackgroundColour( colour
);
8649 if ( !GetBatchCount() )
8651 m_rowLabelWin
->Refresh();
8652 m_colLabelWin
->Refresh();
8653 m_cornerLabelWin
->Refresh();
8658 void wxGrid::SetLabelTextColour( const wxColour
& colour
)
8660 if ( m_labelTextColour
!= colour
)
8662 m_labelTextColour
= colour
;
8663 if ( !GetBatchCount() )
8665 m_rowLabelWin
->Refresh();
8666 m_colLabelWin
->Refresh();
8671 void wxGrid::SetLabelFont( const wxFont
& font
)
8674 if ( !GetBatchCount() )
8676 m_rowLabelWin
->Refresh();
8677 m_colLabelWin
->Refresh();
8681 void wxGrid::SetRowLabelAlignment( int horiz
, int vert
)
8683 // allow old (incorrect) defs to be used
8686 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8687 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8688 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8693 case wxTOP
: vert
= wxALIGN_TOP
; break;
8694 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8695 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8698 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8700 m_rowLabelHorizAlign
= horiz
;
8703 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8705 m_rowLabelVertAlign
= vert
;
8708 if ( !GetBatchCount() )
8710 m_rowLabelWin
->Refresh();
8714 void wxGrid::SetColLabelAlignment( int horiz
, int vert
)
8716 // allow old (incorrect) defs to be used
8719 case wxLEFT
: horiz
= wxALIGN_LEFT
; break;
8720 case wxRIGHT
: horiz
= wxALIGN_RIGHT
; break;
8721 case wxCENTRE
: horiz
= wxALIGN_CENTRE
; break;
8726 case wxTOP
: vert
= wxALIGN_TOP
; break;
8727 case wxBOTTOM
: vert
= wxALIGN_BOTTOM
; break;
8728 case wxCENTRE
: vert
= wxALIGN_CENTRE
; break;
8731 if ( horiz
== wxALIGN_LEFT
|| horiz
== wxALIGN_CENTRE
|| horiz
== wxALIGN_RIGHT
)
8733 m_colLabelHorizAlign
= horiz
;
8736 if ( vert
== wxALIGN_TOP
|| vert
== wxALIGN_CENTRE
|| vert
== wxALIGN_BOTTOM
)
8738 m_colLabelVertAlign
= vert
;
8741 if ( !GetBatchCount() )
8743 m_colLabelWin
->Refresh();
8747 // Note: under MSW, the default column label font must be changed because it
8748 // does not support vertical printing
8750 // Example: wxFont font(9, wxSWISS, wxNORMAL, wxBOLD);
8751 // pGrid->SetLabelFont(font);
8752 // pGrid->SetColLabelTextOrientation(wxVERTICAL);
8754 void wxGrid::SetColLabelTextOrientation( int textOrientation
)
8756 if( textOrientation
== wxHORIZONTAL
|| textOrientation
== wxVERTICAL
)
8758 m_colLabelTextOrientation
= textOrientation
;
8761 if ( !GetBatchCount() )
8763 m_colLabelWin
->Refresh();
8767 void wxGrid::SetRowLabelValue( int row
, const wxString
& s
)
8771 m_table
->SetRowLabelValue( row
, s
);
8772 if ( !GetBatchCount() )
8774 wxRect rect
= CellToRect( row
, 0);
8775 if ( rect
.height
> 0 )
8777 CalcScrolledPosition(0, rect
.y
, &rect
.x
, &rect
.y
);
8779 rect
.width
= m_rowLabelWidth
;
8780 m_rowLabelWin
->Refresh( true, &rect
);
8786 void wxGrid::SetColLabelValue( int col
, const wxString
& s
)
8790 m_table
->SetColLabelValue( col
, s
);
8791 if ( !GetBatchCount() )
8793 wxRect rect
= CellToRect( 0, col
);
8794 if ( rect
.width
> 0 )
8796 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &rect
.y
);
8798 rect
.height
= m_colLabelHeight
;
8799 m_colLabelWin
->Refresh( true, &rect
);
8805 void wxGrid::SetGridLineColour( const wxColour
& colour
)
8807 if ( m_gridLineColour
!= colour
)
8809 m_gridLineColour
= colour
;
8811 wxClientDC
dc( m_gridWin
);
8813 DrawAllGridLines( dc
, wxRegion() );
8818 void wxGrid::SetCellHighlightColour( const wxColour
& colour
)
8820 if ( m_cellHighlightColour
!= colour
)
8822 m_cellHighlightColour
= colour
;
8824 wxClientDC
dc( m_gridWin
);
8826 wxGridCellAttr
* attr
= GetCellAttr(m_currentCellCoords
);
8827 DrawCellHighlight(dc
, attr
);
8832 void wxGrid::SetCellHighlightPenWidth(int width
)
8834 if (m_cellHighlightPenWidth
!= width
) {
8835 m_cellHighlightPenWidth
= width
;
8837 // Just redrawing the cell highlight is not enough since that won't
8838 // make any visible change if the the thickness is getting smaller.
8839 int row
= m_currentCellCoords
.GetRow();
8840 int col
= m_currentCellCoords
.GetCol();
8841 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8843 wxRect rect
= CellToRect(row
, col
);
8844 m_gridWin
->Refresh(true, &rect
);
8848 void wxGrid::SetCellHighlightROPenWidth(int width
)
8850 if (m_cellHighlightROPenWidth
!= width
) {
8851 m_cellHighlightROPenWidth
= width
;
8853 // Just redrawing the cell highlight is not enough since that won't
8854 // make any visible change if the the thickness is getting smaller.
8855 int row
= m_currentCellCoords
.GetRow();
8856 int col
= m_currentCellCoords
.GetCol();
8857 if ( GetColWidth(col
) <= 0 || GetRowHeight(row
) <= 0 )
8859 wxRect rect
= CellToRect(row
, col
);
8860 m_gridWin
->Refresh(true, &rect
);
8864 void wxGrid::EnableGridLines( bool enable
)
8866 if ( enable
!= m_gridLinesEnabled
)
8868 m_gridLinesEnabled
= enable
;
8870 if ( !GetBatchCount() )
8874 wxClientDC
dc( m_gridWin
);
8876 DrawAllGridLines( dc
, wxRegion() );
8880 m_gridWin
->Refresh();
8887 int wxGrid::GetDefaultRowSize()
8889 return m_defaultRowHeight
;
8892 int wxGrid::GetRowSize( int row
)
8894 wxCHECK_MSG( row
>= 0 && row
< m_numRows
, 0, _T("invalid row index") );
8896 return GetRowHeight(row
);
8899 int wxGrid::GetDefaultColSize()
8901 return m_defaultColWidth
;
8904 int wxGrid::GetColSize( int col
)
8906 wxCHECK_MSG( col
>= 0 && col
< m_numCols
, 0, _T("invalid column index") );
8908 return GetColWidth(col
);
8911 // ============================================================================
8912 // access to the grid attributes: each of them has a default value in the grid
8913 // itself and may be overidden on a per-cell basis
8914 // ============================================================================
8916 // ----------------------------------------------------------------------------
8917 // setting default attributes
8918 // ----------------------------------------------------------------------------
8920 void wxGrid::SetDefaultCellBackgroundColour( const wxColour
& col
)
8922 m_defaultCellAttr
->SetBackgroundColour(col
);
8924 m_gridWin
->SetBackgroundColour(col
);
8928 void wxGrid::SetDefaultCellTextColour( const wxColour
& col
)
8930 m_defaultCellAttr
->SetTextColour(col
);
8933 void wxGrid::SetDefaultCellAlignment( int horiz
, int vert
)
8935 m_defaultCellAttr
->SetAlignment(horiz
, vert
);
8938 void wxGrid::SetDefaultCellOverflow( bool allow
)
8940 m_defaultCellAttr
->SetOverflow(allow
);
8943 void wxGrid::SetDefaultCellFont( const wxFont
& font
)
8945 m_defaultCellAttr
->SetFont(font
);
8949 // For editors and renderers the type registry takes precedence over the
8950 // default attr, so we need to register the new editor/renderer for the string
8951 // data type in order to make setting a default editor/renderer appear to
8954 void wxGrid::SetDefaultRenderer(wxGridCellRenderer
*renderer
)
8956 RegisterDataType(wxGRID_VALUE_STRING
,
8958 GetDefaultEditorForType(wxGRID_VALUE_STRING
));
8961 void wxGrid::SetDefaultEditor(wxGridCellEditor
*editor
)
8963 RegisterDataType(wxGRID_VALUE_STRING
,
8964 GetDefaultRendererForType(wxGRID_VALUE_STRING
),
8968 // ----------------------------------------------------------------------------
8969 // access to the default attrbiutes
8970 // ----------------------------------------------------------------------------
8972 wxColour
wxGrid::GetDefaultCellBackgroundColour()
8974 return m_defaultCellAttr
->GetBackgroundColour();
8977 wxColour
wxGrid::GetDefaultCellTextColour()
8979 return m_defaultCellAttr
->GetTextColour();
8982 wxFont
wxGrid::GetDefaultCellFont()
8984 return m_defaultCellAttr
->GetFont();
8987 void wxGrid::GetDefaultCellAlignment( int *horiz
, int *vert
)
8989 m_defaultCellAttr
->GetAlignment(horiz
, vert
);
8992 bool wxGrid::GetDefaultCellOverflow()
8994 return m_defaultCellAttr
->GetOverflow();
8997 wxGridCellRenderer
*wxGrid::GetDefaultRenderer() const
8999 return m_defaultCellAttr
->GetRenderer(NULL
, 0, 0);
9002 wxGridCellEditor
*wxGrid::GetDefaultEditor() const
9004 return m_defaultCellAttr
->GetEditor(NULL
,0,0);
9007 // ----------------------------------------------------------------------------
9008 // access to cell attributes
9009 // ----------------------------------------------------------------------------
9011 wxColour
wxGrid::GetCellBackgroundColour(int row
, int col
)
9013 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9014 wxColour colour
= attr
->GetBackgroundColour();
9019 wxColour
wxGrid::GetCellTextColour( int row
, int col
)
9021 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9022 wxColour colour
= attr
->GetTextColour();
9027 wxFont
wxGrid::GetCellFont( int row
, int col
)
9029 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9030 wxFont font
= attr
->GetFont();
9035 void wxGrid::GetCellAlignment( int row
, int col
, int *horiz
, int *vert
)
9037 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9038 attr
->GetAlignment(horiz
, vert
);
9042 bool wxGrid::GetCellOverflow( int row
, int col
)
9044 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9045 bool allow
= attr
->GetOverflow();
9050 void wxGrid::GetCellSize( int row
, int col
, int *num_rows
, int *num_cols
)
9052 wxGridCellAttr
*attr
= GetCellAttr(row
, col
);
9053 attr
->GetSize( num_rows
, num_cols
);
9057 wxGridCellRenderer
* wxGrid::GetCellRenderer(int row
, int col
)
9059 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9060 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9066 wxGridCellEditor
* wxGrid::GetCellEditor(int row
, int col
)
9068 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9069 wxGridCellEditor
* editor
= attr
->GetEditor(this, row
, col
);
9075 bool wxGrid::IsReadOnly(int row
, int col
) const
9077 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9078 bool isReadOnly
= attr
->IsReadOnly();
9083 // ----------------------------------------------------------------------------
9084 // attribute support: cache, automatic provider creation, ...
9085 // ----------------------------------------------------------------------------
9087 bool wxGrid::CanHaveAttributes()
9094 return m_table
->CanHaveAttributes();
9097 void wxGrid::ClearAttrCache()
9099 if ( m_attrCache
.row
!= -1 )
9101 wxSafeDecRef(m_attrCache
.attr
);
9102 m_attrCache
.attr
= NULL
;
9103 m_attrCache
.row
= -1;
9107 void wxGrid::CacheAttr(int row
, int col
, wxGridCellAttr
*attr
) const
9111 wxGrid
*self
= (wxGrid
*)this; // const_cast
9113 self
->ClearAttrCache();
9114 self
->m_attrCache
.row
= row
;
9115 self
->m_attrCache
.col
= col
;
9116 self
->m_attrCache
.attr
= attr
;
9121 bool wxGrid::LookupAttr(int row
, int col
, wxGridCellAttr
**attr
) const
9123 if ( row
== m_attrCache
.row
&& col
== m_attrCache
.col
)
9125 *attr
= m_attrCache
.attr
;
9126 wxSafeIncRef(m_attrCache
.attr
);
9128 #ifdef DEBUG_ATTR_CACHE
9129 gs_nAttrCacheHits
++;
9136 #ifdef DEBUG_ATTR_CACHE
9137 gs_nAttrCacheMisses
++;
9143 wxGridCellAttr
*wxGrid::GetCellAttr(int row
, int col
) const
9145 wxGridCellAttr
*attr
= NULL
;
9146 // Additional test to avoid looking at the cache e.g. for
9147 // wxNoCellCoords, as this will confuse memory management.
9150 if ( !LookupAttr(row
, col
, &attr
) )
9152 attr
= m_table
? m_table
->GetAttr(row
, col
, wxGridCellAttr::Any
)
9153 : (wxGridCellAttr
*)NULL
;
9154 CacheAttr(row
, col
, attr
);
9159 attr
->SetDefAttr(m_defaultCellAttr
);
9163 attr
= m_defaultCellAttr
;
9170 wxGridCellAttr
*wxGrid::GetOrCreateCellAttr(int row
, int col
) const
9172 wxGridCellAttr
*attr
= (wxGridCellAttr
*)NULL
;
9173 bool canHave
= ((wxGrid
*)this)->CanHaveAttributes();
9175 wxCHECK_MSG( canHave
, attr
, _T("Cell attributes not allowed"));
9176 wxCHECK_MSG( m_table
, attr
, _T("must have a table") );
9178 attr
= m_table
->GetAttr(row
, col
, wxGridCellAttr::Cell
);
9181 attr
= new wxGridCellAttr(m_defaultCellAttr
);
9183 // artificially inc the ref count to match DecRef() in caller
9185 m_table
->SetAttr(attr
, row
, col
);
9191 // ----------------------------------------------------------------------------
9192 // setting column attributes (wrappers around SetColAttr)
9193 // ----------------------------------------------------------------------------
9195 void wxGrid::SetColFormatBool(int col
)
9197 SetColFormatCustom(col
, wxGRID_VALUE_BOOL
);
9200 void wxGrid::SetColFormatNumber(int col
)
9202 SetColFormatCustom(col
, wxGRID_VALUE_NUMBER
);
9205 void wxGrid::SetColFormatFloat(int col
, int width
, int precision
)
9207 wxString typeName
= wxGRID_VALUE_FLOAT
;
9208 if ( (width
!= -1) || (precision
!= -1) )
9210 typeName
<< _T(':') << width
<< _T(',') << precision
;
9213 SetColFormatCustom(col
, typeName
);
9216 void wxGrid::SetColFormatCustom(int col
, const wxString
& typeName
)
9218 wxGridCellAttr
*attr
= m_table
->GetAttr(-1, col
, wxGridCellAttr::Col
);
9220 attr
= new wxGridCellAttr
;
9221 wxGridCellRenderer
*renderer
= GetDefaultRendererForType(typeName
);
9222 attr
->SetRenderer(renderer
);
9224 SetColAttr(col
, attr
);
9228 // ----------------------------------------------------------------------------
9229 // setting cell attributes: this is forwarded to the table
9230 // ----------------------------------------------------------------------------
9232 void wxGrid::SetAttr(int row
, int col
, wxGridCellAttr
*attr
)
9234 if ( CanHaveAttributes() )
9236 m_table
->SetAttr(attr
, row
, col
);
9245 void wxGrid::SetRowAttr(int row
, wxGridCellAttr
*attr
)
9247 if ( CanHaveAttributes() )
9249 m_table
->SetRowAttr(attr
, row
);
9258 void wxGrid::SetColAttr(int col
, wxGridCellAttr
*attr
)
9260 if ( CanHaveAttributes() )
9262 m_table
->SetColAttr(attr
, col
);
9271 void wxGrid::SetCellBackgroundColour( int row
, int col
, const wxColour
& colour
)
9273 if ( CanHaveAttributes() )
9275 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9276 attr
->SetBackgroundColour(colour
);
9281 void wxGrid::SetCellTextColour( int row
, int col
, const wxColour
& colour
)
9283 if ( CanHaveAttributes() )
9285 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9286 attr
->SetTextColour(colour
);
9291 void wxGrid::SetCellFont( int row
, int col
, const wxFont
& font
)
9293 if ( CanHaveAttributes() )
9295 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9296 attr
->SetFont(font
);
9301 void wxGrid::SetCellAlignment( int row
, int col
, int horiz
, int vert
)
9303 if ( CanHaveAttributes() )
9305 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9306 attr
->SetAlignment(horiz
, vert
);
9311 void wxGrid::SetCellOverflow( int row
, int col
, bool allow
)
9313 if ( CanHaveAttributes() )
9315 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9316 attr
->SetOverflow(allow
);
9321 void wxGrid::SetCellSize( int row
, int col
, int num_rows
, int num_cols
)
9323 if ( CanHaveAttributes() )
9325 int cell_rows
, cell_cols
;
9327 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9328 attr
->GetSize(&cell_rows
, &cell_cols
);
9329 attr
->SetSize(num_rows
, num_cols
);
9332 // Cannot set the size of a cell to 0 or negative values
9333 // While it is perfectly legal to do that, this function cannot
9334 // handle all the possibilies, do it by hand by getting the CellAttr.
9335 // You can only set the size of a cell to 1,1 or greater with this fn
9336 wxASSERT_MSG( !((cell_rows
< 1) || (cell_cols
< 1)),
9337 wxT("wxGrid::SetCellSize setting cell size that is already part of another cell"));
9338 wxASSERT_MSG( !((num_rows
< 1) || (num_cols
< 1)),
9339 wxT("wxGrid::SetCellSize setting cell size to < 1"));
9341 // if this was already a multicell then "turn off" the other cells first
9342 if ((cell_rows
> 1) || (cell_rows
> 1))
9345 for (j
=row
; j
<row
+cell_rows
; j
++)
9347 for (i
=col
; i
<col
+cell_cols
; i
++)
9349 if ((i
!= col
) || (j
!= row
))
9351 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9352 attr_stub
->SetSize( 1, 1 );
9353 attr_stub
->DecRef();
9359 // mark the cells that will be covered by this cell to
9360 // negative or zero values to point back at this cell
9361 if (((num_rows
> 1) || (num_cols
> 1)) && (num_rows
>= 1) && (num_cols
>= 1))
9364 for (j
=row
; j
<row
+num_rows
; j
++)
9366 for (i
=col
; i
<col
+num_cols
; i
++)
9368 if ((i
!= col
) || (j
!= row
))
9370 wxGridCellAttr
*attr_stub
= GetOrCreateCellAttr(j
, i
);
9371 attr_stub
->SetSize( row
-j
, col
-i
);
9372 attr_stub
->DecRef();
9380 void wxGrid::SetCellRenderer(int row
, int col
, wxGridCellRenderer
*renderer
)
9382 if ( CanHaveAttributes() )
9384 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9385 attr
->SetRenderer(renderer
);
9390 void wxGrid::SetCellEditor(int row
, int col
, wxGridCellEditor
* editor
)
9392 if ( CanHaveAttributes() )
9394 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9395 attr
->SetEditor(editor
);
9400 void wxGrid::SetReadOnly(int row
, int col
, bool isReadOnly
)
9402 if ( CanHaveAttributes() )
9404 wxGridCellAttr
*attr
= GetOrCreateCellAttr(row
, col
);
9405 attr
->SetReadOnly(isReadOnly
);
9410 // ----------------------------------------------------------------------------
9411 // Data type registration
9412 // ----------------------------------------------------------------------------
9414 void wxGrid::RegisterDataType(const wxString
& typeName
,
9415 wxGridCellRenderer
* renderer
,
9416 wxGridCellEditor
* editor
)
9418 m_typeRegistry
->RegisterDataType(typeName
, renderer
, editor
);
9422 wxGridCellEditor
* wxGrid::GetDefaultEditorForCell(int row
, int col
) const
9424 wxString typeName
= m_table
->GetTypeName(row
, col
);
9425 return GetDefaultEditorForType(typeName
);
9428 wxGridCellRenderer
* wxGrid::GetDefaultRendererForCell(int row
, int col
) const
9430 wxString typeName
= m_table
->GetTypeName(row
, col
);
9431 return GetDefaultRendererForType(typeName
);
9435 wxGrid::GetDefaultEditorForType(const wxString
& typeName
) const
9437 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9438 if ( index
== wxNOT_FOUND
)
9440 wxFAIL_MSG(wxT("Unknown data type name"));
9445 return m_typeRegistry
->GetEditor(index
);
9449 wxGrid::GetDefaultRendererForType(const wxString
& typeName
) const
9451 int index
= m_typeRegistry
->FindOrCloneDataType(typeName
);
9452 if ( index
== wxNOT_FOUND
)
9454 wxFAIL_MSG(wxT("Unknown data type name"));
9459 return m_typeRegistry
->GetRenderer(index
);
9463 // ----------------------------------------------------------------------------
9465 // ----------------------------------------------------------------------------
9467 void wxGrid::EnableDragRowSize( bool enable
)
9469 m_canDragRowSize
= enable
;
9473 void wxGrid::EnableDragColSize( bool enable
)
9475 m_canDragColSize
= enable
;
9478 void wxGrid::EnableDragGridSize( bool enable
)
9480 m_canDragGridSize
= enable
;
9483 void wxGrid::EnableDragCell( bool enable
)
9485 m_canDragCell
= enable
;
9488 void wxGrid::SetDefaultRowSize( int height
, bool resizeExistingRows
)
9490 m_defaultRowHeight
= wxMax( height
, m_minAcceptableRowHeight
);
9492 if ( resizeExistingRows
)
9494 // since we are resizing all rows to the default row size,
9495 // we can simply clear the row heights and row bottoms
9496 // arrays (which also allows us to take advantage of
9497 // some speed optimisations)
9498 m_rowHeights
.Empty();
9499 m_rowBottoms
.Empty();
9500 if ( !GetBatchCount() )
9505 void wxGrid::SetRowSize( int row
, int height
)
9507 wxCHECK_RET( row
>= 0 && row
< m_numRows
, _T("invalid row index") );
9509 // See comment in SetColSize
9510 if ( height
< GetRowMinimalAcceptableHeight()) { return; }
9512 if ( m_rowHeights
.IsEmpty() )
9514 // need to really create the array
9518 int h
= wxMax( 0, height
);
9519 int diff
= h
- m_rowHeights
[row
];
9521 m_rowHeights
[row
] = h
;
9523 for ( i
= row
; i
< m_numRows
; i
++ )
9525 m_rowBottoms
[i
] += diff
;
9527 if ( !GetBatchCount() )
9531 void wxGrid::SetDefaultColSize( int width
, bool resizeExistingCols
)
9533 m_defaultColWidth
= wxMax( width
, m_minAcceptableColWidth
);
9535 if ( resizeExistingCols
)
9537 // since we are resizing all columns to the default column size,
9538 // we can simply clear the col widths and col rights
9539 // arrays (which also allows us to take advantage of
9540 // some speed optimisations)
9541 m_colWidths
.Empty();
9542 m_colRights
.Empty();
9543 if ( !GetBatchCount() )
9548 void wxGrid::SetColSize( int col
, int width
)
9550 wxCHECK_RET( col
>= 0 && col
< m_numCols
, _T("invalid column index") );
9552 // should we check that it's bigger than GetColMinimalWidth(col) here?
9554 // No, because it is reasonable to assume the library user know's
9555 // what he is doing. However whe should test against the weaker
9556 // constariant of minimalAcceptableWidth, as this breaks rendering
9558 // This test then fixes sf.net bug #645734
9560 if ( width
< GetColMinimalAcceptableWidth()) { return; }
9562 if ( m_colWidths
.IsEmpty() )
9564 // need to really create the array
9568 // if < 0 calc new width from label
9572 wxArrayString lines
;
9573 wxClientDC
dc(m_colLabelWin
);
9574 dc
.SetFont(GetLabelFont());
9575 StringToLines(GetColLabelValue(col
), lines
);
9576 GetTextBoxSize(dc
, lines
, &w
, &h
);
9579 int w
= wxMax( 0, width
);
9580 int diff
= w
- m_colWidths
[col
];
9581 m_colWidths
[col
] = w
;
9584 for ( i
= col
; i
< m_numCols
; i
++ )
9586 m_colRights
[i
] += diff
;
9588 if ( !GetBatchCount() )
9593 void wxGrid::SetColMinimalWidth( int col
, int width
)
9595 if (width
> GetColMinimalAcceptableWidth()) {
9596 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9597 m_colMinWidths
[key
] = width
;
9601 void wxGrid::SetRowMinimalHeight( int row
, int width
)
9603 if (width
> GetRowMinimalAcceptableHeight()) {
9604 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9605 m_rowMinHeights
[key
] = width
;
9609 int wxGrid::GetColMinimalWidth(int col
) const
9611 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)col
;
9612 wxLongToLongHashMap::const_iterator it
= m_colMinWidths
.find(key
);
9613 return it
!= m_colMinWidths
.end() ? (int)it
->second
: m_minAcceptableColWidth
;
9616 int wxGrid::GetRowMinimalHeight(int row
) const
9618 wxLongToLongHashMap::key_type key
= (wxLongToLongHashMap::key_type
)row
;
9619 wxLongToLongHashMap::const_iterator it
= m_rowMinHeights
.find(key
);
9620 return it
!= m_rowMinHeights
.end() ? (int)it
->second
: m_minAcceptableRowHeight
;
9623 void wxGrid::SetColMinimalAcceptableWidth( int width
)
9625 // We do allow a width of 0 since this gives us
9626 // an easy way to temporarily hidding columns.
9629 m_minAcceptableColWidth
= width
;
9632 void wxGrid::SetRowMinimalAcceptableHeight( int height
)
9634 // We do allow a height of 0 since this gives us
9635 // an easy way to temporarily hidding rows.
9638 m_minAcceptableRowHeight
= height
;
9641 int wxGrid::GetColMinimalAcceptableWidth() const
9643 return m_minAcceptableColWidth
;
9646 int wxGrid::GetRowMinimalAcceptableHeight() const
9648 return m_minAcceptableRowHeight
;
9651 // ----------------------------------------------------------------------------
9653 // ----------------------------------------------------------------------------
9655 void wxGrid::AutoSizeColOrRow( int colOrRow
, bool setAsMin
, bool column
)
9657 wxClientDC
dc(m_gridWin
);
9659 //Cancel editting of cell
9660 HideCellEditControl();
9661 SaveEditControlValue();
9663 // init both of them to avoid compiler warnings, even if weo nly need one
9671 wxCoord extent
, extentMax
= 0;
9672 int max
= column
? m_numRows
: m_numCols
;
9673 for ( int rowOrCol
= 0; rowOrCol
< max
; rowOrCol
++ )
9680 wxGridCellAttr
* attr
= GetCellAttr(row
, col
);
9681 wxGridCellRenderer
* renderer
= attr
->GetRenderer(this, row
, col
);
9684 wxSize size
= renderer
->GetBestSize(*this, *attr
, dc
, row
, col
);
9685 extent
= column
? size
.x
: size
.y
;
9686 if ( extent
> extentMax
)
9697 // now also compare with the column label extent
9699 dc
.SetFont( GetLabelFont() );
9703 dc
.GetTextExtent( GetColLabelValue(col
), &w
, &h
);
9704 if( GetColLabelTextOrientation() == wxVERTICAL
)
9708 dc
.GetTextExtent( GetRowLabelValue(row
), &w
, &h
);
9710 extent
= column
? w
: h
;
9711 if ( extent
> extentMax
)
9718 // empty column - give default extent (notice that if extentMax is less
9719 // than default extent but != 0, it's ok)
9720 extentMax
= column
? m_defaultColWidth
: m_defaultRowHeight
;
9726 // leave some space around text
9737 SetColSize(col
, extentMax
);
9738 if ( !GetBatchCount() )
9741 m_gridWin
->GetClientSize( &cw
, &ch
);
9742 wxRect
rect ( CellToRect( 0, col
) );
9744 CalcScrolledPosition(rect
.x
, 0, &rect
.x
, &dummy
);
9745 rect
.width
= cw
- rect
.x
;
9746 rect
.height
= m_colLabelHeight
;
9747 m_colLabelWin
->Refresh( true, &rect
);
9752 SetRowSize(row
, extentMax
);
9753 if ( !GetBatchCount() )
9756 m_gridWin
->GetClientSize( &cw
, &ch
);
9757 wxRect
rect ( CellToRect( row
, 0 ) );
9759 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
9760 rect
.width
= m_rowLabelWidth
;
9761 rect
.height
= ch
- rect
.y
;
9762 m_rowLabelWin
->Refresh( true, &rect
);
9768 SetColMinimalWidth(col
, extentMax
);
9770 SetRowMinimalHeight(row
, extentMax
);
9774 int wxGrid::SetOrCalcColumnSizes(bool calcOnly
, bool setAsMin
)
9776 int width
= m_rowLabelWidth
;
9781 for ( int col
= 0; col
< m_numCols
; col
++ )
9785 AutoSizeColumn(col
, setAsMin
);
9788 width
+= GetColWidth(col
);
9797 int wxGrid::SetOrCalcRowSizes(bool calcOnly
, bool setAsMin
)
9799 int height
= m_colLabelHeight
;
9804 for ( int row
= 0; row
< m_numRows
; row
++ )
9808 AutoSizeRow(row
, setAsMin
);
9811 height
+= GetRowHeight(row
);
9820 void wxGrid::AutoSize()
9824 wxSize
size(SetOrCalcColumnSizes(false), SetOrCalcRowSizes(false));
9826 // round up the size to a multiple of scroll step - this ensures that we
9827 // won't get the scrollbars if we're sized exactly to this width
9828 // CalcDimension adds m_extraWidth + 1 etc. to calculate the necessary
9830 wxSize
sizeFit(GetScrollX(size
.x
+ m_extraWidth
+ 1) * GRID_SCROLL_LINE_X
,
9831 GetScrollY(size
.y
+ m_extraHeight
+ 1) * GRID_SCROLL_LINE_Y
);
9833 // distribute the extra space between the columns/rows to avoid having
9834 // extra white space
9836 // Remove the extra m_extraWidth + 1 added above
9837 wxCoord diff
= sizeFit
.x
- size
.x
+ (m_extraWidth
+ 1);
9838 if ( diff
&& m_numCols
)
9840 // try to resize the columns uniformly
9841 wxCoord diffPerCol
= diff
/ m_numCols
;
9844 for ( int col
= 0; col
< m_numCols
; col
++ )
9846 SetColSize(col
, GetColWidth(col
) + diffPerCol
);
9850 // add remaining amount to the last columns
9851 diff
-= diffPerCol
* m_numCols
;
9854 for ( int col
= m_numCols
- 1; col
>= m_numCols
- diff
; col
-- )
9856 SetColSize(col
, GetColWidth(col
) + 1);
9862 diff
= sizeFit
.y
- size
.y
- (m_extraHeight
+ 1);
9863 if ( diff
&& m_numRows
)
9865 // try to resize the columns uniformly
9866 wxCoord diffPerRow
= diff
/ m_numRows
;
9869 for ( int row
= 0; row
< m_numRows
; row
++ )
9871 SetRowSize(row
, GetRowHeight(row
) + diffPerRow
);
9875 // add remaining amount to the last rows
9876 diff
-= diffPerRow
* m_numRows
;
9879 for ( int row
= m_numRows
- 1; row
>= m_numRows
- diff
; row
-- )
9881 SetRowSize(row
, GetRowHeight(row
) + 1);
9888 SetClientSize(sizeFit
);
9891 void wxGrid::AutoSizeRowLabelSize( int row
)
9893 wxArrayString lines
;
9896 // Hide the edit control, so it
9897 // won't interfer with drag-shrinking.
9898 if( IsCellEditControlShown() )
9900 HideCellEditControl();
9901 SaveEditControlValue();
9904 // autosize row height depending on label text
9905 StringToLines( GetRowLabelValue( row
), lines
);
9906 wxClientDC
dc( m_rowLabelWin
);
9907 GetTextBoxSize( dc
, lines
, &w
, &h
);
9908 if( h
< m_defaultRowHeight
)
9909 h
= m_defaultRowHeight
;
9914 void wxGrid::AutoSizeColLabelSize( int col
)
9916 wxArrayString lines
;
9919 // Hide the edit control, so it
9920 // won't interfer with drag-shrinking.
9921 if( IsCellEditControlShown() )
9923 HideCellEditControl();
9924 SaveEditControlValue();
9927 // autosize column width depending on label text
9928 StringToLines( GetColLabelValue( col
), lines
);
9929 wxClientDC
dc( m_colLabelWin
);
9930 if( GetColLabelTextOrientation() == wxHORIZONTAL
)
9931 GetTextBoxSize( dc
, lines
, &w
, &h
);
9933 GetTextBoxSize( dc
, lines
, &h
, &w
);
9934 if( w
< m_defaultColWidth
)
9935 w
= m_defaultColWidth
;
9940 wxSize
wxGrid::DoGetBestSize() const
9942 // don't set sizes, only calculate them
9943 wxGrid
*self
= (wxGrid
*)this; // const_cast
9946 width
= self
->SetOrCalcColumnSizes(true);
9947 height
= self
->SetOrCalcRowSizes(true);
9949 if (!width
) width
=100;
9950 if (!height
) height
=80;
9952 // Round up to a multiple the scroll rate NOTE: this still doesn't get rid
9953 // of the scrollbars, is there any magic incantaion for that?
9955 GetScrollPixelsPerUnit(&xpu
, &ypu
);
9957 width
+= 1 + xpu
- (width
% xpu
);
9959 height
+= 1 + ypu
- (height
% ypu
);
9961 // limit to 1/4 of the screen size
9962 int maxwidth
, maxheight
;
9963 wxDisplaySize( & maxwidth
, & maxheight
);
9966 if ( width
> maxwidth
) width
= maxwidth
;
9967 if ( height
> maxheight
) height
= maxheight
;
9970 wxSize
best(width
, height
);
9971 // NOTE: This size should be cached, but first we need to add calls to
9972 // InvalidateBestSize everywhere that could change the results of this
9974 // CacheBestSize(size);
9984 wxPen
& wxGrid::GetDividerPen() const
9989 // ----------------------------------------------------------------------------
9990 // cell value accessor functions
9991 // ----------------------------------------------------------------------------
9993 void wxGrid::SetCellValue( int row
, int col
, const wxString
& s
)
9997 m_table
->SetValue( row
, col
, s
);
9998 if ( !GetBatchCount() )
10001 wxRect
rect( CellToRect( row
, col
) );
10003 rect
.width
= m_gridWin
->GetClientSize().GetWidth();
10004 CalcScrolledPosition(0, rect
.y
, &dummy
, &rect
.y
);
10005 m_gridWin
->Refresh( false, &rect
);
10008 if ( m_currentCellCoords
.GetRow() == row
&&
10009 m_currentCellCoords
.GetCol() == col
&&
10010 IsCellEditControlShown())
10011 // Note: If we are using IsCellEditControlEnabled,
10012 // this interacts badly with calling SetCellValue from
10013 // an EVT_GRID_CELL_CHANGE handler.
10015 HideCellEditControl();
10016 ShowCellEditControl(); // will reread data from table
10023 // ------ Block, row and col selection
10026 void wxGrid::SelectRow( int row
, bool addToSelected
)
10028 if ( IsSelection() && !addToSelected
)
10032 m_selection
->SelectRow( row
, false, addToSelected
);
10036 void wxGrid::SelectCol( int col
, bool addToSelected
)
10038 if ( IsSelection() && !addToSelected
)
10042 m_selection
->SelectCol( col
, false, addToSelected
);
10046 void wxGrid::SelectBlock( int topRow
, int leftCol
, int bottomRow
, int rightCol
,
10047 bool addToSelected
)
10049 if ( IsSelection() && !addToSelected
)
10053 m_selection
->SelectBlock( topRow
, leftCol
, bottomRow
, rightCol
,
10054 false, addToSelected
);
10058 void wxGrid::SelectAll()
10060 if ( m_numRows
> 0 && m_numCols
> 0 )
10063 m_selection
->SelectBlock( 0, 0, m_numRows
-1, m_numCols
-1 );
10068 // ------ Cell, row and col deselection
10071 void wxGrid::DeselectRow( int row
)
10073 if ( !m_selection
)
10076 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectRows
)
10078 if ( m_selection
->IsInSelection(row
, 0 ) )
10079 m_selection
->ToggleCellSelection( row
, 0);
10083 int nCols
= GetNumberCols();
10084 for ( int i
= 0; i
< nCols
; i
++ )
10086 if ( m_selection
->IsInSelection(row
, i
) )
10087 m_selection
->ToggleCellSelection( row
, i
);
10092 void wxGrid::DeselectCol( int col
)
10094 if ( !m_selection
)
10097 if ( m_selection
->GetSelectionMode() == wxGrid::wxGridSelectColumns
)
10099 if ( m_selection
->IsInSelection(0, col
) )
10100 m_selection
->ToggleCellSelection( 0, col
);
10104 int nRows
= GetNumberRows();
10105 for ( int i
= 0; i
< nRows
; i
++ )
10107 if ( m_selection
->IsInSelection(i
, col
) )
10108 m_selection
->ToggleCellSelection(i
, col
);
10113 void wxGrid::DeselectCell( int row
, int col
)
10115 if ( m_selection
&& m_selection
->IsInSelection(row
, col
) )
10116 m_selection
->ToggleCellSelection(row
, col
);
10119 bool wxGrid::IsSelection()
10121 return ( m_selection
&& (m_selection
->IsSelection() ||
10122 ( m_selectingTopLeft
!= wxGridNoCellCoords
&&
10123 m_selectingBottomRight
!= wxGridNoCellCoords
) ) );
10126 bool wxGrid::IsInSelection( int row
, int col
) const
10128 return ( m_selection
&& (m_selection
->IsInSelection( row
, col
) ||
10129 ( row
>= m_selectingTopLeft
.GetRow() &&
10130 col
>= m_selectingTopLeft
.GetCol() &&
10131 row
<= m_selectingBottomRight
.GetRow() &&
10132 col
<= m_selectingBottomRight
.GetCol() )) );
10135 wxGridCellCoordsArray
wxGrid::GetSelectedCells() const
10137 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10138 return m_selection
->m_cellSelection
;
10140 wxGridCellCoordsArray
wxGrid::GetSelectionBlockTopLeft() const
10142 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10143 return m_selection
->m_blockSelectionTopLeft
;
10145 wxGridCellCoordsArray
wxGrid::GetSelectionBlockBottomRight() const
10147 if (!m_selection
) { wxGridCellCoordsArray a
; return a
; }
10148 return m_selection
->m_blockSelectionBottomRight
;
10150 wxArrayInt
wxGrid::GetSelectedRows() const
10152 if (!m_selection
) { wxArrayInt a
; return a
; }
10153 return m_selection
->m_rowSelection
;
10155 wxArrayInt
wxGrid::GetSelectedCols() const
10157 if (!m_selection
) { wxArrayInt a
; return a
; }
10158 return m_selection
->m_colSelection
;
10162 void wxGrid::ClearSelection()
10164 m_selectingTopLeft
= wxGridNoCellCoords
;
10165 m_selectingBottomRight
= wxGridNoCellCoords
;
10167 m_selection
->ClearSelection();
10171 // This function returns the rectangle that encloses the given block
10172 // in device coords clipped to the client size of the grid window.
10174 wxRect
wxGrid::BlockToDeviceRect( const wxGridCellCoords
&topLeft
,
10175 const wxGridCellCoords
&bottomRight
)
10177 wxRect
rect( wxGridNoCellRect
);
10180 cellRect
= CellToRect( topLeft
);
10181 if ( cellRect
!= wxGridNoCellRect
)
10187 rect
= wxRect( 0, 0, 0, 0 );
10190 cellRect
= CellToRect( bottomRight
);
10191 if ( cellRect
!= wxGridNoCellRect
)
10197 return wxGridNoCellRect
;
10201 int left
= rect
.GetLeft();
10202 int top
= rect
.GetTop();
10203 int right
= rect
.GetRight();
10204 int bottom
= rect
.GetBottom();
10206 int leftCol
= topLeft
.GetCol();
10207 int topRow
= topLeft
.GetRow();
10208 int rightCol
= bottomRight
.GetCol();
10209 int bottomRow
= bottomRight
.GetRow();
10227 topRow
= bottomRow
;
10232 for ( j
= topRow
; j
<= bottomRow
; j
++ )
10234 for ( i
= leftCol
; i
<= rightCol
; i
++ )
10236 if ((j
==topRow
) || (j
==bottomRow
) || (i
==leftCol
) || (i
==rightCol
))
10238 cellRect
= CellToRect( j
, i
);
10240 if (cellRect
.x
< left
)
10242 if (cellRect
.y
< top
)
10244 if (cellRect
.x
+ cellRect
.width
> right
)
10245 right
= cellRect
.x
+ cellRect
.width
;
10246 if (cellRect
.y
+ cellRect
.height
> bottom
)
10247 bottom
= cellRect
.y
+ cellRect
.height
;
10249 else i
= rightCol
; // jump over inner cells.
10253 // convert to scrolled coords
10255 CalcScrolledPosition( left
, top
, &left
, &top
);
10256 CalcScrolledPosition( right
, bottom
, &right
, &bottom
);
10259 m_gridWin
->GetClientSize( &cw
, &ch
);
10261 if (right
< 0 || bottom
< 0 || left
> cw
|| top
> ch
)
10262 return wxRect( 0, 0, 0, 0);
10264 rect
.SetLeft( wxMax(0, left
) );
10265 rect
.SetTop( wxMax(0, top
) );
10266 rect
.SetRight( wxMin(cw
, right
) );
10267 rect
.SetBottom( wxMin(ch
, bottom
) );
10273 // ------ Grid event classes
10276 IMPLEMENT_DYNAMIC_CLASS( wxGridEvent
, wxNotifyEvent
)
10278 wxGridEvent::wxGridEvent( int id
, wxEventType type
, wxObject
* obj
,
10279 int row
, int col
, int x
, int y
, bool sel
,
10280 bool control
, bool shift
, bool alt
, bool meta
)
10281 : wxNotifyEvent( type
, id
)
10288 m_control
= control
;
10293 SetEventObject(obj
);
10297 IMPLEMENT_DYNAMIC_CLASS( wxGridSizeEvent
, wxNotifyEvent
)
10299 wxGridSizeEvent::wxGridSizeEvent( int id
, wxEventType type
, wxObject
* obj
,
10300 int rowOrCol
, int x
, int y
,
10301 bool control
, bool shift
, bool alt
, bool meta
)
10302 : wxNotifyEvent( type
, id
)
10304 m_rowOrCol
= rowOrCol
;
10307 m_control
= control
;
10312 SetEventObject(obj
);
10316 IMPLEMENT_DYNAMIC_CLASS( wxGridRangeSelectEvent
, wxNotifyEvent
)
10318 wxGridRangeSelectEvent::wxGridRangeSelectEvent(int id
, wxEventType type
, wxObject
* obj
,
10319 const wxGridCellCoords
& topLeft
,
10320 const wxGridCellCoords
& bottomRight
,
10321 bool sel
, bool control
,
10322 bool shift
, bool alt
, bool meta
)
10323 : wxNotifyEvent( type
, id
)
10325 m_topLeft
= topLeft
;
10326 m_bottomRight
= bottomRight
;
10328 m_control
= control
;
10333 SetEventObject(obj
);
10337 IMPLEMENT_DYNAMIC_CLASS(wxGridEditorCreatedEvent
, wxCommandEvent
)
10339 wxGridEditorCreatedEvent::wxGridEditorCreatedEvent(int id
, wxEventType type
,
10340 wxObject
* obj
, int row
,
10341 int col
, wxControl
* ctrl
)
10342 : wxCommandEvent(type
, id
)
10344 SetEventObject(obj
);
10350 #endif // wxUSE_GRID